vectorizer cost model enhancement
[official-gcc.git] / gcc / tree-vect-data-refs.c
blobb8988d9d264a06312029ebbf8a9af0df61212f04
1 /* Data References Analysis and Manipulation Utilities for Vectorization.
2 Copyright (C) 2003-2013 Free Software Foundation, Inc.
3 Contributed by Dorit Naishlos <dorit@il.ibm.com>
4 and Ira Rosen <irar@il.ibm.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "dumpfile.h"
26 #include "tm.h"
27 #include "ggc.h"
28 #include "tree.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.h"
34 #include "dumpfile.h"
35 #include "cfgloop.h"
36 #include "tree-chrec.h"
37 #include "tree-scalar-evolution.h"
38 #include "tree-vectorizer.h"
39 #include "diagnostic-core.h"
40 /* Need to include rtl.h, expr.h, etc. for optabs. */
41 #include "expr.h"
42 #include "optabs.h"
44 /* Return true if load- or store-lanes optab OPTAB is implemented for
45 COUNT vectors of type VECTYPE. NAME is the name of OPTAB. */
47 static bool
48 vect_lanes_optab_supported_p (const char *name, convert_optab optab,
49 tree vectype, unsigned HOST_WIDE_INT count)
51 enum machine_mode mode, array_mode;
52 bool limit_p;
54 mode = TYPE_MODE (vectype);
55 limit_p = !targetm.array_mode_supported_p (mode, count);
56 array_mode = mode_for_size (count * GET_MODE_BITSIZE (mode),
57 MODE_INT, limit_p);
59 if (array_mode == BLKmode)
61 if (dump_enabled_p ())
62 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
63 "no array mode for %s[" HOST_WIDE_INT_PRINT_DEC "]\n",
64 GET_MODE_NAME (mode), count);
65 return false;
68 if (convert_optab_handler (optab, array_mode, mode) == CODE_FOR_nothing)
70 if (dump_enabled_p ())
71 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
72 "cannot use %s<%s><%s>\n", name,
73 GET_MODE_NAME (array_mode), GET_MODE_NAME (mode));
74 return false;
77 if (dump_enabled_p ())
78 dump_printf_loc (MSG_NOTE, vect_location,
79 "can use %s<%s><%s>\n", name, GET_MODE_NAME (array_mode),
80 GET_MODE_NAME (mode));
82 return true;
86 /* Return the smallest scalar part of STMT.
87 This is used to determine the vectype of the stmt. We generally set the
88 vectype according to the type of the result (lhs). For stmts whose
89 result-type is different than the type of the arguments (e.g., demotion,
90 promotion), vectype will be reset appropriately (later). Note that we have
91 to visit the smallest datatype in this function, because that determines the
92 VF. If the smallest datatype in the loop is present only as the rhs of a
93 promotion operation - we'd miss it.
94 Such a case, where a variable of this datatype does not appear in the lhs
95 anywhere in the loop, can only occur if it's an invariant: e.g.:
96 'int_x = (int) short_inv', which we'd expect to have been optimized away by
97 invariant motion. However, we cannot rely on invariant motion to always
98 take invariants out of the loop, and so in the case of promotion we also
99 have to check the rhs.
100 LHS_SIZE_UNIT and RHS_SIZE_UNIT contain the sizes of the corresponding
101 types. */
103 tree
104 vect_get_smallest_scalar_type (gimple stmt, HOST_WIDE_INT *lhs_size_unit,
105 HOST_WIDE_INT *rhs_size_unit)
107 tree scalar_type = gimple_expr_type (stmt);
108 HOST_WIDE_INT lhs, rhs;
110 lhs = rhs = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (scalar_type));
112 if (is_gimple_assign (stmt)
113 && (gimple_assign_cast_p (stmt)
114 || gimple_assign_rhs_code (stmt) == WIDEN_MULT_EXPR
115 || gimple_assign_rhs_code (stmt) == WIDEN_LSHIFT_EXPR
116 || gimple_assign_rhs_code (stmt) == FLOAT_EXPR))
118 tree rhs_type = TREE_TYPE (gimple_assign_rhs1 (stmt));
120 rhs = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (rhs_type));
121 if (rhs < lhs)
122 scalar_type = rhs_type;
125 *lhs_size_unit = lhs;
126 *rhs_size_unit = rhs;
127 return scalar_type;
131 /* Check if data references pointed by DR_I and DR_J are same or
132 belong to same interleaving group. Return FALSE if drs are
133 different, otherwise return TRUE. */
135 static bool
136 vect_same_range_drs (data_reference_p dr_i, data_reference_p dr_j)
138 gimple stmt_i = DR_STMT (dr_i);
139 gimple stmt_j = DR_STMT (dr_j);
141 if (operand_equal_p (DR_REF (dr_i), DR_REF (dr_j), 0)
142 || (GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt_i))
143 && GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt_j))
144 && (GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt_i))
145 == GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt_j)))))
146 return true;
147 else
148 return false;
151 /* If address ranges represented by DDR_I and DDR_J are equal,
152 return TRUE, otherwise return FALSE. */
154 static bool
155 vect_vfa_range_equal (ddr_p ddr_i, ddr_p ddr_j)
157 if ((vect_same_range_drs (DDR_A (ddr_i), DDR_A (ddr_j))
158 && vect_same_range_drs (DDR_B (ddr_i), DDR_B (ddr_j)))
159 || (vect_same_range_drs (DDR_A (ddr_i), DDR_B (ddr_j))
160 && vect_same_range_drs (DDR_B (ddr_i), DDR_A (ddr_j))))
161 return true;
162 else
163 return false;
166 /* Insert DDR into LOOP_VINFO list of ddrs that may alias and need to be
167 tested at run-time. Return TRUE if DDR was successfully inserted.
168 Return false if versioning is not supported. */
170 static bool
171 vect_mark_for_runtime_alias_test (ddr_p ddr, loop_vec_info loop_vinfo)
173 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
175 if ((unsigned) PARAM_VALUE (PARAM_VECT_MAX_VERSION_FOR_ALIAS_CHECKS) == 0)
176 return false;
178 if (dump_enabled_p ())
180 dump_printf_loc (MSG_NOTE, vect_location,
181 "mark for run-time aliasing test between ");
182 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (DDR_A (ddr)));
183 dump_printf (MSG_NOTE, " and ");
184 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (DDR_B (ddr)));
185 dump_printf (MSG_NOTE, "\n");
188 if (optimize_loop_nest_for_size_p (loop))
190 if (dump_enabled_p ())
191 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
192 "versioning not supported when optimizing"
193 " for size.\n");
194 return false;
197 /* FORNOW: We don't support versioning with outer-loop vectorization. */
198 if (loop->inner)
200 if (dump_enabled_p ())
201 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
202 "versioning not yet supported for outer-loops.\n");
203 return false;
206 /* FORNOW: We don't support creating runtime alias tests for non-constant
207 step. */
208 if (TREE_CODE (DR_STEP (DDR_A (ddr))) != INTEGER_CST
209 || TREE_CODE (DR_STEP (DDR_B (ddr))) != INTEGER_CST)
211 if (dump_enabled_p ())
212 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
213 "versioning not yet supported for non-constant "
214 "step\n");
215 return false;
218 LOOP_VINFO_MAY_ALIAS_DDRS (loop_vinfo).safe_push (ddr);
219 return true;
223 /* Function vect_analyze_data_ref_dependence.
225 Return TRUE if there (might) exist a dependence between a memory-reference
226 DRA and a memory-reference DRB. When versioning for alias may check a
227 dependence at run-time, return FALSE. Adjust *MAX_VF according to
228 the data dependence. */
230 static bool
231 vect_analyze_data_ref_dependence (struct data_dependence_relation *ddr,
232 loop_vec_info loop_vinfo, int *max_vf)
234 unsigned int i;
235 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
236 struct data_reference *dra = DDR_A (ddr);
237 struct data_reference *drb = DDR_B (ddr);
238 stmt_vec_info stmtinfo_a = vinfo_for_stmt (DR_STMT (dra));
239 stmt_vec_info stmtinfo_b = vinfo_for_stmt (DR_STMT (drb));
240 lambda_vector dist_v;
241 unsigned int loop_depth;
243 /* In loop analysis all data references should be vectorizable. */
244 if (!STMT_VINFO_VECTORIZABLE (stmtinfo_a)
245 || !STMT_VINFO_VECTORIZABLE (stmtinfo_b))
246 gcc_unreachable ();
248 /* Independent data accesses. */
249 if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
250 return false;
252 if (dra == drb
253 || (DR_IS_READ (dra) && DR_IS_READ (drb)))
254 return false;
256 /* Unknown data dependence. */
257 if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
259 /* If user asserted safelen consecutive iterations can be
260 executed concurrently, assume independence. */
261 if (loop->safelen >= 2)
263 if (loop->safelen < *max_vf)
264 *max_vf = loop->safelen;
265 return false;
268 if (STMT_VINFO_GATHER_P (stmtinfo_a)
269 || STMT_VINFO_GATHER_P (stmtinfo_b))
271 if (dump_enabled_p ())
273 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
274 "versioning for alias not supported for: "
275 "can't determine dependence between ");
276 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
277 DR_REF (dra));
278 dump_printf (MSG_MISSED_OPTIMIZATION, " and ");
279 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
280 DR_REF (drb));
281 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
283 return true;
286 if (dump_enabled_p ())
288 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
289 "versioning for alias required: "
290 "can't determine dependence between ");
291 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
292 DR_REF (dra));
293 dump_printf (MSG_MISSED_OPTIMIZATION, " and ");
294 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
295 DR_REF (drb));
296 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
299 /* Add to list of ddrs that need to be tested at run-time. */
300 return !vect_mark_for_runtime_alias_test (ddr, loop_vinfo);
303 /* Known data dependence. */
304 if (DDR_NUM_DIST_VECTS (ddr) == 0)
306 /* If user asserted safelen consecutive iterations can be
307 executed concurrently, assume independence. */
308 if (loop->safelen >= 2)
310 if (loop->safelen < *max_vf)
311 *max_vf = loop->safelen;
312 return false;
315 if (STMT_VINFO_GATHER_P (stmtinfo_a)
316 || STMT_VINFO_GATHER_P (stmtinfo_b))
318 if (dump_enabled_p ())
320 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
321 "versioning for alias not supported for: "
322 "bad dist vector for ");
323 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
324 DR_REF (dra));
325 dump_printf (MSG_MISSED_OPTIMIZATION, " and ");
326 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
327 DR_REF (drb));
328 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
330 return true;
333 if (dump_enabled_p ())
335 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
336 "versioning for alias required: "
337 "bad dist vector for ");
338 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, DR_REF (dra));
339 dump_printf (MSG_MISSED_OPTIMIZATION, " and ");
340 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, DR_REF (drb));
341 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
343 /* Add to list of ddrs that need to be tested at run-time. */
344 return !vect_mark_for_runtime_alias_test (ddr, loop_vinfo);
347 loop_depth = index_in_loop_nest (loop->num, DDR_LOOP_NEST (ddr));
348 FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), i, dist_v)
350 int dist = dist_v[loop_depth];
352 if (dump_enabled_p ())
353 dump_printf_loc (MSG_NOTE, vect_location,
354 "dependence distance = %d.\n", dist);
356 if (dist == 0)
358 if (dump_enabled_p ())
360 dump_printf_loc (MSG_NOTE, vect_location,
361 "dependence distance == 0 between ");
362 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (dra));
363 dump_printf (MSG_NOTE, " and ");
364 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (drb));
365 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
368 /* When we perform grouped accesses and perform implicit CSE
369 by detecting equal accesses and doing disambiguation with
370 runtime alias tests like for
371 .. = a[i];
372 .. = a[i+1];
373 a[i] = ..;
374 a[i+1] = ..;
375 *p = ..;
376 .. = a[i];
377 .. = a[i+1];
378 where we will end up loading { a[i], a[i+1] } once, make
379 sure that inserting group loads before the first load and
380 stores after the last store will do the right thing. */
381 if ((STMT_VINFO_GROUPED_ACCESS (stmtinfo_a)
382 && GROUP_SAME_DR_STMT (stmtinfo_a))
383 || (STMT_VINFO_GROUPED_ACCESS (stmtinfo_b)
384 && GROUP_SAME_DR_STMT (stmtinfo_b)))
386 gimple earlier_stmt;
387 earlier_stmt = get_earlier_stmt (DR_STMT (dra), DR_STMT (drb));
388 if (DR_IS_WRITE
389 (STMT_VINFO_DATA_REF (vinfo_for_stmt (earlier_stmt))))
391 if (dump_enabled_p ())
392 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
393 "READ_WRITE dependence in interleaving."
394 "\n");
395 return true;
399 continue;
402 if (dist > 0 && DDR_REVERSED_P (ddr))
404 /* If DDR_REVERSED_P the order of the data-refs in DDR was
405 reversed (to make distance vector positive), and the actual
406 distance is negative. */
407 if (dump_enabled_p ())
408 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
409 "dependence distance negative.\n");
410 continue;
413 if (abs (dist) >= 2
414 && abs (dist) < *max_vf)
416 /* The dependence distance requires reduction of the maximal
417 vectorization factor. */
418 *max_vf = abs (dist);
419 if (dump_enabled_p ())
420 dump_printf_loc (MSG_NOTE, vect_location,
421 "adjusting maximal vectorization factor to %i\n",
422 *max_vf);
425 if (abs (dist) >= *max_vf)
427 /* Dependence distance does not create dependence, as far as
428 vectorization is concerned, in this case. */
429 if (dump_enabled_p ())
430 dump_printf_loc (MSG_NOTE, vect_location,
431 "dependence distance >= VF.\n");
432 continue;
435 if (dump_enabled_p ())
437 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
438 "not vectorized, possible dependence "
439 "between data-refs ");
440 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (dra));
441 dump_printf (MSG_NOTE, " and ");
442 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (drb));
443 dump_printf (MSG_NOTE, "\n");
446 return true;
449 return false;
452 /* Function vect_analyze_data_ref_dependences.
454 Examine all the data references in the loop, and make sure there do not
455 exist any data dependences between them. Set *MAX_VF according to
456 the maximum vectorization factor the data dependences allow. */
458 bool
459 vect_analyze_data_ref_dependences (loop_vec_info loop_vinfo, int *max_vf)
461 unsigned int i;
462 struct data_dependence_relation *ddr;
464 if (dump_enabled_p ())
465 dump_printf_loc (MSG_NOTE, vect_location,
466 "=== vect_analyze_data_ref_dependences ===\n");
468 if (!compute_all_dependences (LOOP_VINFO_DATAREFS (loop_vinfo),
469 &LOOP_VINFO_DDRS (loop_vinfo),
470 LOOP_VINFO_LOOP_NEST (loop_vinfo), true))
471 return false;
473 FOR_EACH_VEC_ELT (LOOP_VINFO_DDRS (loop_vinfo), i, ddr)
474 if (vect_analyze_data_ref_dependence (ddr, loop_vinfo, max_vf))
475 return false;
477 return true;
481 /* Function vect_slp_analyze_data_ref_dependence.
483 Return TRUE if there (might) exist a dependence between a memory-reference
484 DRA and a memory-reference DRB. When versioning for alias may check a
485 dependence at run-time, return FALSE. Adjust *MAX_VF according to
486 the data dependence. */
488 static bool
489 vect_slp_analyze_data_ref_dependence (struct data_dependence_relation *ddr)
491 struct data_reference *dra = DDR_A (ddr);
492 struct data_reference *drb = DDR_B (ddr);
494 /* We need to check dependences of statements marked as unvectorizable
495 as well, they still can prohibit vectorization. */
497 /* Independent data accesses. */
498 if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
499 return false;
501 if (dra == drb)
502 return false;
504 /* Read-read is OK. */
505 if (DR_IS_READ (dra) && DR_IS_READ (drb))
506 return false;
508 /* If dra and drb are part of the same interleaving chain consider
509 them independent. */
510 if (STMT_VINFO_GROUPED_ACCESS (vinfo_for_stmt (DR_STMT (dra)))
511 && (GROUP_FIRST_ELEMENT (vinfo_for_stmt (DR_STMT (dra)))
512 == GROUP_FIRST_ELEMENT (vinfo_for_stmt (DR_STMT (drb)))))
513 return false;
515 /* Unknown data dependence. */
516 if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
518 gimple earlier_stmt;
520 if (dump_enabled_p ())
522 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
523 "can't determine dependence between ");
524 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, DR_REF (dra));
525 dump_printf (MSG_MISSED_OPTIMIZATION, " and ");
526 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, DR_REF (drb));
527 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
530 /* We do not vectorize basic blocks with write-write dependencies. */
531 if (DR_IS_WRITE (dra) && DR_IS_WRITE (drb))
532 return true;
534 /* Check that it's not a load-after-store dependence. */
535 earlier_stmt = get_earlier_stmt (DR_STMT (dra), DR_STMT (drb));
536 if (DR_IS_WRITE (STMT_VINFO_DATA_REF (vinfo_for_stmt (earlier_stmt))))
537 return true;
539 return false;
542 if (dump_enabled_p ())
544 dump_printf_loc (MSG_NOTE, vect_location,
545 "determined dependence between ");
546 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (dra));
547 dump_printf (MSG_NOTE, " and ");
548 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (drb));
549 dump_printf (MSG_NOTE, "\n");
552 /* Do not vectorize basic blocks with write-write dependences. */
553 if (DR_IS_WRITE (dra) && DR_IS_WRITE (drb))
554 return true;
556 /* Check dependence between DRA and DRB for basic block vectorization.
557 If the accesses share same bases and offsets, we can compare their initial
558 constant offsets to decide whether they differ or not. In case of a read-
559 write dependence we check that the load is before the store to ensure that
560 vectorization will not change the order of the accesses. */
562 HOST_WIDE_INT type_size_a, type_size_b, init_a, init_b;
563 gimple earlier_stmt;
565 /* Check that the data-refs have same bases and offsets. If not, we can't
566 determine if they are dependent. */
567 if (!operand_equal_p (DR_BASE_ADDRESS (dra), DR_BASE_ADDRESS (drb), 0)
568 || !dr_equal_offsets_p (dra, drb))
569 return true;
571 /* Check the types. */
572 type_size_a = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dra))));
573 type_size_b = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (drb))));
575 if (type_size_a != type_size_b
576 || !types_compatible_p (TREE_TYPE (DR_REF (dra)),
577 TREE_TYPE (DR_REF (drb))))
578 return true;
580 init_a = TREE_INT_CST_LOW (DR_INIT (dra));
581 init_b = TREE_INT_CST_LOW (DR_INIT (drb));
583 /* Two different locations - no dependence. */
584 if (init_a != init_b)
585 return false;
587 /* We have a read-write dependence. Check that the load is before the store.
588 When we vectorize basic blocks, vector load can be only before
589 corresponding scalar load, and vector store can be only after its
590 corresponding scalar store. So the order of the acceses is preserved in
591 case the load is before the store. */
592 earlier_stmt = get_earlier_stmt (DR_STMT (dra), DR_STMT (drb));
593 if (DR_IS_READ (STMT_VINFO_DATA_REF (vinfo_for_stmt (earlier_stmt))))
594 return false;
596 return true;
600 /* Function vect_analyze_data_ref_dependences.
602 Examine all the data references in the basic-block, and make sure there
603 do not exist any data dependences between them. Set *MAX_VF according to
604 the maximum vectorization factor the data dependences allow. */
606 bool
607 vect_slp_analyze_data_ref_dependences (bb_vec_info bb_vinfo)
609 struct data_dependence_relation *ddr;
610 unsigned int i;
612 if (dump_enabled_p ())
613 dump_printf_loc (MSG_NOTE, vect_location,
614 "=== vect_slp_analyze_data_ref_dependences ===\n");
616 if (!compute_all_dependences (BB_VINFO_DATAREFS (bb_vinfo),
617 &BB_VINFO_DDRS (bb_vinfo),
618 vNULL, true))
619 return false;
621 FOR_EACH_VEC_ELT (BB_VINFO_DDRS (bb_vinfo), i, ddr)
622 if (vect_slp_analyze_data_ref_dependence (ddr))
623 return false;
625 return true;
629 /* Function vect_compute_data_ref_alignment
631 Compute the misalignment of the data reference DR.
633 Output:
634 1. If during the misalignment computation it is found that the data reference
635 cannot be vectorized then false is returned.
636 2. DR_MISALIGNMENT (DR) is defined.
638 FOR NOW: No analysis is actually performed. Misalignment is calculated
639 only for trivial cases. TODO. */
641 static bool
642 vect_compute_data_ref_alignment (struct data_reference *dr)
644 gimple stmt = DR_STMT (dr);
645 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
646 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
647 struct loop *loop = NULL;
648 tree ref = DR_REF (dr);
649 tree vectype;
650 tree base, base_addr;
651 bool base_aligned;
652 tree misalign;
653 tree aligned_to, alignment;
655 if (dump_enabled_p ())
656 dump_printf_loc (MSG_NOTE, vect_location,
657 "vect_compute_data_ref_alignment:\n");
659 if (loop_vinfo)
660 loop = LOOP_VINFO_LOOP (loop_vinfo);
662 /* Initialize misalignment to unknown. */
663 SET_DR_MISALIGNMENT (dr, -1);
665 /* Strided loads perform only component accesses, misalignment information
666 is irrelevant for them. */
667 if (STMT_VINFO_STRIDE_LOAD_P (stmt_info))
668 return true;
670 misalign = DR_INIT (dr);
671 aligned_to = DR_ALIGNED_TO (dr);
672 base_addr = DR_BASE_ADDRESS (dr);
673 vectype = STMT_VINFO_VECTYPE (stmt_info);
675 /* In case the dataref is in an inner-loop of the loop that is being
676 vectorized (LOOP), we use the base and misalignment information
677 relative to the outer-loop (LOOP). This is ok only if the misalignment
678 stays the same throughout the execution of the inner-loop, which is why
679 we have to check that the stride of the dataref in the inner-loop evenly
680 divides by the vector size. */
681 if (loop && nested_in_vect_loop_p (loop, stmt))
683 tree step = DR_STEP (dr);
684 HOST_WIDE_INT dr_step = TREE_INT_CST_LOW (step);
686 if (dr_step % GET_MODE_SIZE (TYPE_MODE (vectype)) == 0)
688 if (dump_enabled_p ())
689 dump_printf_loc (MSG_NOTE, vect_location,
690 "inner step divides the vector-size.\n");
691 misalign = STMT_VINFO_DR_INIT (stmt_info);
692 aligned_to = STMT_VINFO_DR_ALIGNED_TO (stmt_info);
693 base_addr = STMT_VINFO_DR_BASE_ADDRESS (stmt_info);
695 else
697 if (dump_enabled_p ())
698 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
699 "inner step doesn't divide the vector-size.\n");
700 misalign = NULL_TREE;
704 /* Similarly, if we're doing basic-block vectorization, we can only use
705 base and misalignment information relative to an innermost loop if the
706 misalignment stays the same throughout the execution of the loop.
707 As above, this is the case if the stride of the dataref evenly divides
708 by the vector size. */
709 if (!loop)
711 tree step = DR_STEP (dr);
712 HOST_WIDE_INT dr_step = TREE_INT_CST_LOW (step);
714 if (dr_step % GET_MODE_SIZE (TYPE_MODE (vectype)) != 0)
716 if (dump_enabled_p ())
717 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
718 "SLP: step doesn't divide the vector-size.\n");
719 misalign = NULL_TREE;
723 base = build_fold_indirect_ref (base_addr);
724 alignment = ssize_int (TYPE_ALIGN (vectype)/BITS_PER_UNIT);
726 if ((aligned_to && tree_int_cst_compare (aligned_to, alignment) < 0)
727 || !misalign)
729 if (dump_enabled_p ())
731 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
732 "Unknown alignment for access: ");
733 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, base);
734 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
736 return true;
739 if ((DECL_P (base)
740 && tree_int_cst_compare (ssize_int (DECL_ALIGN_UNIT (base)),
741 alignment) >= 0)
742 || (TREE_CODE (base_addr) == SSA_NAME
743 && tree_int_cst_compare (ssize_int (TYPE_ALIGN_UNIT (TREE_TYPE (
744 TREE_TYPE (base_addr)))),
745 alignment) >= 0)
746 || (get_pointer_alignment (base_addr) >= TYPE_ALIGN (vectype)))
747 base_aligned = true;
748 else
749 base_aligned = false;
751 if (!base_aligned)
753 /* Do not change the alignment of global variables here if
754 flag_section_anchors is enabled as we already generated
755 RTL for other functions. Most global variables should
756 have been aligned during the IPA increase_alignment pass. */
757 if (!vect_can_force_dr_alignment_p (base, TYPE_ALIGN (vectype))
758 || (TREE_STATIC (base) && flag_section_anchors))
760 if (dump_enabled_p ())
762 dump_printf_loc (MSG_NOTE, vect_location,
763 "can't force alignment of ref: ");
764 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
765 dump_printf (MSG_NOTE, "\n");
767 return true;
770 /* Force the alignment of the decl.
771 NOTE: This is the only change to the code we make during
772 the analysis phase, before deciding to vectorize the loop. */
773 if (dump_enabled_p ())
775 dump_printf_loc (MSG_NOTE, vect_location, "force alignment of ");
776 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
777 dump_printf (MSG_NOTE, "\n");
780 ((dataref_aux *)dr->aux)->base_decl = base;
781 ((dataref_aux *)dr->aux)->base_misaligned = true;
784 /* If this is a backward running DR then first access in the larger
785 vectype actually is N-1 elements before the address in the DR.
786 Adjust misalign accordingly. */
787 if (tree_int_cst_compare (DR_STEP (dr), size_zero_node) < 0)
789 tree offset = ssize_int (TYPE_VECTOR_SUBPARTS (vectype) - 1);
790 /* DR_STEP(dr) is the same as -TYPE_SIZE of the scalar type,
791 otherwise we wouldn't be here. */
792 offset = fold_build2 (MULT_EXPR, ssizetype, offset, DR_STEP (dr));
793 /* PLUS because DR_STEP was negative. */
794 misalign = size_binop (PLUS_EXPR, misalign, offset);
797 /* Modulo alignment. */
798 misalign = size_binop (FLOOR_MOD_EXPR, misalign, alignment);
800 if (!host_integerp (misalign, 1))
802 /* Negative or overflowed misalignment value. */
803 if (dump_enabled_p ())
804 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
805 "unexpected misalign value\n");
806 return false;
809 SET_DR_MISALIGNMENT (dr, TREE_INT_CST_LOW (misalign));
811 if (dump_enabled_p ())
813 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
814 "misalign = %d bytes of ref ", DR_MISALIGNMENT (dr));
815 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, ref);
816 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
819 return true;
823 /* Function vect_compute_data_refs_alignment
825 Compute the misalignment of data references in the loop.
826 Return FALSE if a data reference is found that cannot be vectorized. */
828 static bool
829 vect_compute_data_refs_alignment (loop_vec_info loop_vinfo,
830 bb_vec_info bb_vinfo)
832 vec<data_reference_p> datarefs;
833 struct data_reference *dr;
834 unsigned int i;
836 if (loop_vinfo)
837 datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
838 else
839 datarefs = BB_VINFO_DATAREFS (bb_vinfo);
841 FOR_EACH_VEC_ELT (datarefs, i, dr)
842 if (STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dr)))
843 && !vect_compute_data_ref_alignment (dr))
845 if (bb_vinfo)
847 /* Mark unsupported statement as unvectorizable. */
848 STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dr))) = false;
849 continue;
851 else
852 return false;
855 return true;
859 /* Function vect_update_misalignment_for_peel
861 DR - the data reference whose misalignment is to be adjusted.
862 DR_PEEL - the data reference whose misalignment is being made
863 zero in the vector loop by the peel.
864 NPEEL - the number of iterations in the peel loop if the misalignment
865 of DR_PEEL is known at compile time. */
867 static void
868 vect_update_misalignment_for_peel (struct data_reference *dr,
869 struct data_reference *dr_peel, int npeel)
871 unsigned int i;
872 vec<dr_p> same_align_drs;
873 struct data_reference *current_dr;
874 int dr_size = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (DR_REF (dr))));
875 int dr_peel_size = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (DR_REF (dr_peel))));
876 stmt_vec_info stmt_info = vinfo_for_stmt (DR_STMT (dr));
877 stmt_vec_info peel_stmt_info = vinfo_for_stmt (DR_STMT (dr_peel));
879 /* For interleaved data accesses the step in the loop must be multiplied by
880 the size of the interleaving group. */
881 if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
882 dr_size *= GROUP_SIZE (vinfo_for_stmt (GROUP_FIRST_ELEMENT (stmt_info)));
883 if (STMT_VINFO_GROUPED_ACCESS (peel_stmt_info))
884 dr_peel_size *= GROUP_SIZE (peel_stmt_info);
886 /* It can be assumed that the data refs with the same alignment as dr_peel
887 are aligned in the vector loop. */
888 same_align_drs
889 = STMT_VINFO_SAME_ALIGN_REFS (vinfo_for_stmt (DR_STMT (dr_peel)));
890 FOR_EACH_VEC_ELT (same_align_drs, i, current_dr)
892 if (current_dr != dr)
893 continue;
894 gcc_assert (DR_MISALIGNMENT (dr) / dr_size ==
895 DR_MISALIGNMENT (dr_peel) / dr_peel_size);
896 SET_DR_MISALIGNMENT (dr, 0);
897 return;
900 if (known_alignment_for_access_p (dr)
901 && known_alignment_for_access_p (dr_peel))
903 bool negative = tree_int_cst_compare (DR_STEP (dr), size_zero_node) < 0;
904 int misal = DR_MISALIGNMENT (dr);
905 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
906 misal += negative ? -npeel * dr_size : npeel * dr_size;
907 misal &= (TYPE_ALIGN (vectype) / BITS_PER_UNIT) - 1;
908 SET_DR_MISALIGNMENT (dr, misal);
909 return;
912 if (dump_enabled_p ())
913 dump_printf_loc (MSG_NOTE, vect_location, "Setting misalignment to -1.\n");
914 SET_DR_MISALIGNMENT (dr, -1);
918 /* Function vect_verify_datarefs_alignment
920 Return TRUE if all data references in the loop can be
921 handled with respect to alignment. */
923 bool
924 vect_verify_datarefs_alignment (loop_vec_info loop_vinfo, bb_vec_info bb_vinfo)
926 vec<data_reference_p> datarefs;
927 struct data_reference *dr;
928 enum dr_alignment_support supportable_dr_alignment;
929 unsigned int i;
931 if (loop_vinfo)
932 datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
933 else
934 datarefs = BB_VINFO_DATAREFS (bb_vinfo);
936 FOR_EACH_VEC_ELT (datarefs, i, dr)
938 gimple stmt = DR_STMT (dr);
939 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
941 if (!STMT_VINFO_RELEVANT_P (stmt_info))
942 continue;
944 /* For interleaving, only the alignment of the first access matters.
945 Skip statements marked as not vectorizable. */
946 if ((STMT_VINFO_GROUPED_ACCESS (stmt_info)
947 && GROUP_FIRST_ELEMENT (stmt_info) != stmt)
948 || !STMT_VINFO_VECTORIZABLE (stmt_info))
949 continue;
951 /* Strided loads perform only component accesses, alignment is
952 irrelevant for them. */
953 if (STMT_VINFO_STRIDE_LOAD_P (stmt_info))
954 continue;
956 supportable_dr_alignment = vect_supportable_dr_alignment (dr, false);
957 if (!supportable_dr_alignment)
959 if (dump_enabled_p ())
961 if (DR_IS_READ (dr))
962 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
963 "not vectorized: unsupported unaligned load.");
964 else
965 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
966 "not vectorized: unsupported unaligned "
967 "store.");
969 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
970 DR_REF (dr));
971 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
973 return false;
975 if (supportable_dr_alignment != dr_aligned && dump_enabled_p ())
976 dump_printf_loc (MSG_NOTE, vect_location,
977 "Vectorizing an unaligned access.\n");
979 return true;
982 /* Given an memory reference EXP return whether its alignment is less
983 than its size. */
985 static bool
986 not_size_aligned (tree exp)
988 if (!host_integerp (TYPE_SIZE (TREE_TYPE (exp)), 1))
989 return true;
991 return (TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (exp)))
992 > get_object_alignment (exp));
995 /* Function vector_alignment_reachable_p
997 Return true if vector alignment for DR is reachable by peeling
998 a few loop iterations. Return false otherwise. */
1000 static bool
1001 vector_alignment_reachable_p (struct data_reference *dr)
1003 gimple stmt = DR_STMT (dr);
1004 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1005 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
1007 if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
1009 /* For interleaved access we peel only if number of iterations in
1010 the prolog loop ({VF - misalignment}), is a multiple of the
1011 number of the interleaved accesses. */
1012 int elem_size, mis_in_elements;
1013 int nelements = TYPE_VECTOR_SUBPARTS (vectype);
1015 /* FORNOW: handle only known alignment. */
1016 if (!known_alignment_for_access_p (dr))
1017 return false;
1019 elem_size = GET_MODE_SIZE (TYPE_MODE (vectype)) / nelements;
1020 mis_in_elements = DR_MISALIGNMENT (dr) / elem_size;
1022 if ((nelements - mis_in_elements) % GROUP_SIZE (stmt_info))
1023 return false;
1026 /* If misalignment is known at the compile time then allow peeling
1027 only if natural alignment is reachable through peeling. */
1028 if (known_alignment_for_access_p (dr) && !aligned_access_p (dr))
1030 HOST_WIDE_INT elmsize =
1031 int_cst_value (TYPE_SIZE_UNIT (TREE_TYPE (vectype)));
1032 if (dump_enabled_p ())
1034 dump_printf_loc (MSG_NOTE, vect_location,
1035 "data size =" HOST_WIDE_INT_PRINT_DEC, elmsize);
1036 dump_printf (MSG_NOTE,
1037 ". misalignment = %d.\n", DR_MISALIGNMENT (dr));
1039 if (DR_MISALIGNMENT (dr) % elmsize)
1041 if (dump_enabled_p ())
1042 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1043 "data size does not divide the misalignment.\n");
1044 return false;
1048 if (!known_alignment_for_access_p (dr))
1050 tree type = TREE_TYPE (DR_REF (dr));
1051 bool is_packed = not_size_aligned (DR_REF (dr));
1052 if (dump_enabled_p ())
1053 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1054 "Unknown misalignment, is_packed = %d\n",is_packed);
1055 if ((TYPE_USER_ALIGN (type) && !is_packed)
1056 || targetm.vectorize.vector_alignment_reachable (type, is_packed))
1057 return true;
1058 else
1059 return false;
1062 return true;
1066 /* Calculate the cost of the memory access represented by DR. */
1068 static void
1069 vect_get_data_access_cost (struct data_reference *dr,
1070 unsigned int *inside_cost,
1071 unsigned int *outside_cost,
1072 stmt_vector_for_cost *body_cost_vec)
1074 gimple stmt = DR_STMT (dr);
1075 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1076 int nunits = TYPE_VECTOR_SUBPARTS (STMT_VINFO_VECTYPE (stmt_info));
1077 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
1078 int vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
1079 int ncopies = vf / nunits;
1081 if (DR_IS_READ (dr))
1082 vect_get_load_cost (dr, ncopies, true, inside_cost, outside_cost,
1083 NULL, body_cost_vec, false);
1084 else
1085 vect_get_store_cost (dr, ncopies, inside_cost, body_cost_vec);
1087 if (dump_enabled_p ())
1088 dump_printf_loc (MSG_NOTE, vect_location,
1089 "vect_get_data_access_cost: inside_cost = %d, "
1090 "outside_cost = %d.\n", *inside_cost, *outside_cost);
1094 /* Insert DR into peeling hash table with NPEEL as key. */
1096 static void
1097 vect_peeling_hash_insert (loop_vec_info loop_vinfo, struct data_reference *dr,
1098 int npeel)
1100 struct _vect_peel_info elem, *slot;
1101 _vect_peel_info **new_slot;
1102 bool supportable_dr_alignment = vect_supportable_dr_alignment (dr, true);
1104 elem.npeel = npeel;
1105 slot = LOOP_VINFO_PEELING_HTAB (loop_vinfo).find (&elem);
1106 if (slot)
1107 slot->count++;
1108 else
1110 slot = XNEW (struct _vect_peel_info);
1111 slot->npeel = npeel;
1112 slot->dr = dr;
1113 slot->count = 1;
1114 new_slot = LOOP_VINFO_PEELING_HTAB (loop_vinfo).find_slot (slot, INSERT);
1115 *new_slot = slot;
1118 if (!supportable_dr_alignment && unlimited_cost_model ())
1119 slot->count += VECT_MAX_COST;
1123 /* Traverse peeling hash table to find peeling option that aligns maximum
1124 number of data accesses. */
1127 vect_peeling_hash_get_most_frequent (_vect_peel_info **slot,
1128 _vect_peel_extended_info *max)
1130 vect_peel_info elem = *slot;
1132 if (elem->count > max->peel_info.count
1133 || (elem->count == max->peel_info.count
1134 && max->peel_info.npeel > elem->npeel))
1136 max->peel_info.npeel = elem->npeel;
1137 max->peel_info.count = elem->count;
1138 max->peel_info.dr = elem->dr;
1141 return 1;
1145 /* Traverse peeling hash table and calculate cost for each peeling option.
1146 Find the one with the lowest cost. */
1149 vect_peeling_hash_get_lowest_cost (_vect_peel_info **slot,
1150 _vect_peel_extended_info *min)
1152 vect_peel_info elem = *slot;
1153 int save_misalignment, dummy;
1154 unsigned int inside_cost = 0, outside_cost = 0, i;
1155 gimple stmt = DR_STMT (elem->dr);
1156 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1157 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
1158 vec<data_reference_p> datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
1159 struct data_reference *dr;
1160 stmt_vector_for_cost prologue_cost_vec, body_cost_vec, epilogue_cost_vec;
1161 int single_iter_cost;
1163 prologue_cost_vec.create (2);
1164 body_cost_vec.create (2);
1165 epilogue_cost_vec.create (2);
1167 FOR_EACH_VEC_ELT (datarefs, i, dr)
1169 stmt = DR_STMT (dr);
1170 stmt_info = vinfo_for_stmt (stmt);
1171 /* For interleaving, only the alignment of the first access
1172 matters. */
1173 if (STMT_VINFO_GROUPED_ACCESS (stmt_info)
1174 && GROUP_FIRST_ELEMENT (stmt_info) != stmt)
1175 continue;
1177 save_misalignment = DR_MISALIGNMENT (dr);
1178 vect_update_misalignment_for_peel (dr, elem->dr, elem->npeel);
1179 vect_get_data_access_cost (dr, &inside_cost, &outside_cost,
1180 &body_cost_vec);
1181 SET_DR_MISALIGNMENT (dr, save_misalignment);
1184 single_iter_cost = vect_get_single_scalar_iteration_cost (loop_vinfo);
1185 outside_cost += vect_get_known_peeling_cost (loop_vinfo, elem->npeel,
1186 &dummy, single_iter_cost,
1187 &prologue_cost_vec,
1188 &epilogue_cost_vec);
1190 /* Prologue and epilogue costs are added to the target model later.
1191 These costs depend only on the scalar iteration cost, the
1192 number of peeling iterations finally chosen, and the number of
1193 misaligned statements. So discard the information found here. */
1194 prologue_cost_vec.release ();
1195 epilogue_cost_vec.release ();
1197 if (inside_cost < min->inside_cost
1198 || (inside_cost == min->inside_cost && outside_cost < min->outside_cost))
1200 min->inside_cost = inside_cost;
1201 min->outside_cost = outside_cost;
1202 min->body_cost_vec.release ();
1203 min->body_cost_vec = body_cost_vec;
1204 min->peel_info.dr = elem->dr;
1205 min->peel_info.npeel = elem->npeel;
1207 else
1208 body_cost_vec.release ();
1210 return 1;
1214 /* Choose best peeling option by traversing peeling hash table and either
1215 choosing an option with the lowest cost (if cost model is enabled) or the
1216 option that aligns as many accesses as possible. */
1218 static struct data_reference *
1219 vect_peeling_hash_choose_best_peeling (loop_vec_info loop_vinfo,
1220 unsigned int *npeel,
1221 stmt_vector_for_cost *body_cost_vec)
1223 struct _vect_peel_extended_info res;
1225 res.peel_info.dr = NULL;
1226 res.body_cost_vec = stmt_vector_for_cost();
1228 if (!unlimited_cost_model ())
1230 res.inside_cost = INT_MAX;
1231 res.outside_cost = INT_MAX;
1232 LOOP_VINFO_PEELING_HTAB (loop_vinfo)
1233 .traverse <_vect_peel_extended_info *,
1234 vect_peeling_hash_get_lowest_cost> (&res);
1236 else
1238 res.peel_info.count = 0;
1239 LOOP_VINFO_PEELING_HTAB (loop_vinfo)
1240 .traverse <_vect_peel_extended_info *,
1241 vect_peeling_hash_get_most_frequent> (&res);
1244 *npeel = res.peel_info.npeel;
1245 *body_cost_vec = res.body_cost_vec;
1246 return res.peel_info.dr;
1250 /* Function vect_enhance_data_refs_alignment
1252 This pass will use loop versioning and loop peeling in order to enhance
1253 the alignment of data references in the loop.
1255 FOR NOW: we assume that whatever versioning/peeling takes place, only the
1256 original loop is to be vectorized. Any other loops that are created by
1257 the transformations performed in this pass - are not supposed to be
1258 vectorized. This restriction will be relaxed.
1260 This pass will require a cost model to guide it whether to apply peeling
1261 or versioning or a combination of the two. For example, the scheme that
1262 intel uses when given a loop with several memory accesses, is as follows:
1263 choose one memory access ('p') which alignment you want to force by doing
1264 peeling. Then, either (1) generate a loop in which 'p' is aligned and all
1265 other accesses are not necessarily aligned, or (2) use loop versioning to
1266 generate one loop in which all accesses are aligned, and another loop in
1267 which only 'p' is necessarily aligned.
1269 ("Automatic Intra-Register Vectorization for the Intel Architecture",
1270 Aart J.C. Bik, Milind Girkar, Paul M. Grey and Ximmin Tian, International
1271 Journal of Parallel Programming, Vol. 30, No. 2, April 2002.)
1273 Devising a cost model is the most critical aspect of this work. It will
1274 guide us on which access to peel for, whether to use loop versioning, how
1275 many versions to create, etc. The cost model will probably consist of
1276 generic considerations as well as target specific considerations (on
1277 powerpc for example, misaligned stores are more painful than misaligned
1278 loads).
1280 Here are the general steps involved in alignment enhancements:
1282 -- original loop, before alignment analysis:
1283 for (i=0; i<N; i++){
1284 x = q[i]; # DR_MISALIGNMENT(q) = unknown
1285 p[i] = y; # DR_MISALIGNMENT(p) = unknown
1288 -- After vect_compute_data_refs_alignment:
1289 for (i=0; i<N; i++){
1290 x = q[i]; # DR_MISALIGNMENT(q) = 3
1291 p[i] = y; # DR_MISALIGNMENT(p) = unknown
1294 -- Possibility 1: we do loop versioning:
1295 if (p is aligned) {
1296 for (i=0; i<N; i++){ # loop 1A
1297 x = q[i]; # DR_MISALIGNMENT(q) = 3
1298 p[i] = y; # DR_MISALIGNMENT(p) = 0
1301 else {
1302 for (i=0; i<N; i++){ # loop 1B
1303 x = q[i]; # DR_MISALIGNMENT(q) = 3
1304 p[i] = y; # DR_MISALIGNMENT(p) = unaligned
1308 -- Possibility 2: we do loop peeling:
1309 for (i = 0; i < 3; i++){ # (scalar loop, not to be vectorized).
1310 x = q[i];
1311 p[i] = y;
1313 for (i = 3; i < N; i++){ # loop 2A
1314 x = q[i]; # DR_MISALIGNMENT(q) = 0
1315 p[i] = y; # DR_MISALIGNMENT(p) = unknown
1318 -- Possibility 3: combination of loop peeling and versioning:
1319 for (i = 0; i < 3; i++){ # (scalar loop, not to be vectorized).
1320 x = q[i];
1321 p[i] = y;
1323 if (p is aligned) {
1324 for (i = 3; i<N; i++){ # loop 3A
1325 x = q[i]; # DR_MISALIGNMENT(q) = 0
1326 p[i] = y; # DR_MISALIGNMENT(p) = 0
1329 else {
1330 for (i = 3; i<N; i++){ # loop 3B
1331 x = q[i]; # DR_MISALIGNMENT(q) = 0
1332 p[i] = y; # DR_MISALIGNMENT(p) = unaligned
1336 These loops are later passed to loop_transform to be vectorized. The
1337 vectorizer will use the alignment information to guide the transformation
1338 (whether to generate regular loads/stores, or with special handling for
1339 misalignment). */
1341 bool
1342 vect_enhance_data_refs_alignment (loop_vec_info loop_vinfo)
1344 vec<data_reference_p> datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
1345 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
1346 enum dr_alignment_support supportable_dr_alignment;
1347 struct data_reference *dr0 = NULL, *first_store = NULL;
1348 struct data_reference *dr;
1349 unsigned int i, j;
1350 bool do_peeling = false;
1351 bool do_versioning = false;
1352 bool stat;
1353 gimple stmt;
1354 stmt_vec_info stmt_info;
1355 unsigned int npeel = 0;
1356 bool all_misalignments_unknown = true;
1357 unsigned int vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
1358 unsigned possible_npeel_number = 1;
1359 tree vectype;
1360 unsigned int nelements, mis, same_align_drs_max = 0;
1361 stmt_vector_for_cost body_cost_vec = stmt_vector_for_cost();
1363 if (dump_enabled_p ())
1364 dump_printf_loc (MSG_NOTE, vect_location,
1365 "=== vect_enhance_data_refs_alignment ===\n");
1367 /* While cost model enhancements are expected in the future, the high level
1368 view of the code at this time is as follows:
1370 A) If there is a misaligned access then see if peeling to align
1371 this access can make all data references satisfy
1372 vect_supportable_dr_alignment. If so, update data structures
1373 as needed and return true.
1375 B) If peeling wasn't possible and there is a data reference with an
1376 unknown misalignment that does not satisfy vect_supportable_dr_alignment
1377 then see if loop versioning checks can be used to make all data
1378 references satisfy vect_supportable_dr_alignment. If so, update
1379 data structures as needed and return true.
1381 C) If neither peeling nor versioning were successful then return false if
1382 any data reference does not satisfy vect_supportable_dr_alignment.
1384 D) Return true (all data references satisfy vect_supportable_dr_alignment).
1386 Note, Possibility 3 above (which is peeling and versioning together) is not
1387 being done at this time. */
1389 /* (1) Peeling to force alignment. */
1391 /* (1.1) Decide whether to perform peeling, and how many iterations to peel:
1392 Considerations:
1393 + How many accesses will become aligned due to the peeling
1394 - How many accesses will become unaligned due to the peeling,
1395 and the cost of misaligned accesses.
1396 - The cost of peeling (the extra runtime checks, the increase
1397 in code size). */
1399 FOR_EACH_VEC_ELT (datarefs, i, dr)
1401 stmt = DR_STMT (dr);
1402 stmt_info = vinfo_for_stmt (stmt);
1404 if (!STMT_VINFO_RELEVANT_P (stmt_info))
1405 continue;
1407 /* For interleaving, only the alignment of the first access
1408 matters. */
1409 if (STMT_VINFO_GROUPED_ACCESS (stmt_info)
1410 && GROUP_FIRST_ELEMENT (stmt_info) != stmt)
1411 continue;
1413 /* For invariant accesses there is nothing to enhance. */
1414 if (integer_zerop (DR_STEP (dr)))
1415 continue;
1417 /* Strided loads perform only component accesses, alignment is
1418 irrelevant for them. */
1419 if (STMT_VINFO_STRIDE_LOAD_P (stmt_info))
1420 continue;
1422 supportable_dr_alignment = vect_supportable_dr_alignment (dr, true);
1423 do_peeling = vector_alignment_reachable_p (dr);
1424 if (do_peeling)
1426 if (known_alignment_for_access_p (dr))
1428 unsigned int npeel_tmp;
1429 bool negative = tree_int_cst_compare (DR_STEP (dr),
1430 size_zero_node) < 0;
1432 /* Save info about DR in the hash table. */
1433 if (!LOOP_VINFO_PEELING_HTAB (loop_vinfo).is_created ())
1434 LOOP_VINFO_PEELING_HTAB (loop_vinfo).create (1);
1436 vectype = STMT_VINFO_VECTYPE (stmt_info);
1437 nelements = TYPE_VECTOR_SUBPARTS (vectype);
1438 mis = DR_MISALIGNMENT (dr) / GET_MODE_SIZE (TYPE_MODE (
1439 TREE_TYPE (DR_REF (dr))));
1440 npeel_tmp = (negative
1441 ? (mis - nelements) : (nelements - mis))
1442 & (nelements - 1);
1444 /* For multiple types, it is possible that the bigger type access
1445 will have more than one peeling option. E.g., a loop with two
1446 types: one of size (vector size / 4), and the other one of
1447 size (vector size / 8). Vectorization factor will 8. If both
1448 access are misaligned by 3, the first one needs one scalar
1449 iteration to be aligned, and the second one needs 5. But the
1450 the first one will be aligned also by peeling 5 scalar
1451 iterations, and in that case both accesses will be aligned.
1452 Hence, except for the immediate peeling amount, we also want
1453 to try to add full vector size, while we don't exceed
1454 vectorization factor.
1455 We do this automtically for cost model, since we calculate cost
1456 for every peeling option. */
1457 if (unlimited_cost_model ())
1458 possible_npeel_number = vf /nelements;
1460 /* Handle the aligned case. We may decide to align some other
1461 access, making DR unaligned. */
1462 if (DR_MISALIGNMENT (dr) == 0)
1464 npeel_tmp = 0;
1465 if (unlimited_cost_model ())
1466 possible_npeel_number++;
1469 for (j = 0; j < possible_npeel_number; j++)
1471 gcc_assert (npeel_tmp <= vf);
1472 vect_peeling_hash_insert (loop_vinfo, dr, npeel_tmp);
1473 npeel_tmp += nelements;
1476 all_misalignments_unknown = false;
1477 /* Data-ref that was chosen for the case that all the
1478 misalignments are unknown is not relevant anymore, since we
1479 have a data-ref with known alignment. */
1480 dr0 = NULL;
1482 else
1484 /* If we don't know any misalignment values, we prefer
1485 peeling for data-ref that has the maximum number of data-refs
1486 with the same alignment, unless the target prefers to align
1487 stores over load. */
1488 if (all_misalignments_unknown)
1490 unsigned same_align_drs
1491 = STMT_VINFO_SAME_ALIGN_REFS (stmt_info).length ();
1492 if (!dr0
1493 || same_align_drs_max < same_align_drs)
1495 same_align_drs_max = same_align_drs;
1496 dr0 = dr;
1498 /* For data-refs with the same number of related
1499 accesses prefer the one where the misalign
1500 computation will be invariant in the outermost loop. */
1501 else if (same_align_drs_max == same_align_drs)
1503 struct loop *ivloop0, *ivloop;
1504 ivloop0 = outermost_invariant_loop_for_expr
1505 (loop, DR_BASE_ADDRESS (dr0));
1506 ivloop = outermost_invariant_loop_for_expr
1507 (loop, DR_BASE_ADDRESS (dr));
1508 if ((ivloop && !ivloop0)
1509 || (ivloop && ivloop0
1510 && flow_loop_nested_p (ivloop, ivloop0)))
1511 dr0 = dr;
1514 if (!first_store && DR_IS_WRITE (dr))
1515 first_store = dr;
1518 /* If there are both known and unknown misaligned accesses in the
1519 loop, we choose peeling amount according to the known
1520 accesses. */
1521 if (!supportable_dr_alignment)
1523 dr0 = dr;
1524 if (!first_store && DR_IS_WRITE (dr))
1525 first_store = dr;
1529 else
1531 if (!aligned_access_p (dr))
1533 if (dump_enabled_p ())
1534 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1535 "vector alignment may not be reachable\n");
1536 break;
1541 /* Check if we can possibly peel the loop. */
1542 if (!vect_can_advance_ivs_p (loop_vinfo)
1543 || !slpeel_can_duplicate_loop_p (loop, single_exit (loop)))
1544 do_peeling = false;
1546 if (do_peeling && all_misalignments_unknown
1547 && vect_supportable_dr_alignment (dr0, false))
1550 /* Check if the target requires to prefer stores over loads, i.e., if
1551 misaligned stores are more expensive than misaligned loads (taking
1552 drs with same alignment into account). */
1553 if (first_store && DR_IS_READ (dr0))
1555 unsigned int load_inside_cost = 0, load_outside_cost = 0;
1556 unsigned int store_inside_cost = 0, store_outside_cost = 0;
1557 unsigned int load_inside_penalty = 0, load_outside_penalty = 0;
1558 unsigned int store_inside_penalty = 0, store_outside_penalty = 0;
1559 stmt_vector_for_cost dummy;
1560 dummy.create (2);
1562 vect_get_data_access_cost (dr0, &load_inside_cost, &load_outside_cost,
1563 &dummy);
1564 vect_get_data_access_cost (first_store, &store_inside_cost,
1565 &store_outside_cost, &dummy);
1567 dummy.release ();
1569 /* Calculate the penalty for leaving FIRST_STORE unaligned (by
1570 aligning the load DR0). */
1571 load_inside_penalty = store_inside_cost;
1572 load_outside_penalty = store_outside_cost;
1573 for (i = 0;
1574 STMT_VINFO_SAME_ALIGN_REFS (vinfo_for_stmt (
1575 DR_STMT (first_store))).iterate (i, &dr);
1576 i++)
1577 if (DR_IS_READ (dr))
1579 load_inside_penalty += load_inside_cost;
1580 load_outside_penalty += load_outside_cost;
1582 else
1584 load_inside_penalty += store_inside_cost;
1585 load_outside_penalty += store_outside_cost;
1588 /* Calculate the penalty for leaving DR0 unaligned (by
1589 aligning the FIRST_STORE). */
1590 store_inside_penalty = load_inside_cost;
1591 store_outside_penalty = load_outside_cost;
1592 for (i = 0;
1593 STMT_VINFO_SAME_ALIGN_REFS (vinfo_for_stmt (
1594 DR_STMT (dr0))).iterate (i, &dr);
1595 i++)
1596 if (DR_IS_READ (dr))
1598 store_inside_penalty += load_inside_cost;
1599 store_outside_penalty += load_outside_cost;
1601 else
1603 store_inside_penalty += store_inside_cost;
1604 store_outside_penalty += store_outside_cost;
1607 if (load_inside_penalty > store_inside_penalty
1608 || (load_inside_penalty == store_inside_penalty
1609 && load_outside_penalty > store_outside_penalty))
1610 dr0 = first_store;
1613 /* In case there are only loads with different unknown misalignments, use
1614 peeling only if it may help to align other accesses in the loop. */
1615 if (!first_store
1616 && !STMT_VINFO_SAME_ALIGN_REFS (
1617 vinfo_for_stmt (DR_STMT (dr0))).length ()
1618 && vect_supportable_dr_alignment (dr0, false)
1619 != dr_unaligned_supported)
1620 do_peeling = false;
1623 if (do_peeling && !dr0)
1625 /* Peeling is possible, but there is no data access that is not supported
1626 unless aligned. So we try to choose the best possible peeling. */
1628 /* We should get here only if there are drs with known misalignment. */
1629 gcc_assert (!all_misalignments_unknown);
1631 /* Choose the best peeling from the hash table. */
1632 dr0 = vect_peeling_hash_choose_best_peeling (loop_vinfo, &npeel,
1633 &body_cost_vec);
1634 if (!dr0 || !npeel)
1635 do_peeling = false;
1638 if (do_peeling)
1640 stmt = DR_STMT (dr0);
1641 stmt_info = vinfo_for_stmt (stmt);
1642 vectype = STMT_VINFO_VECTYPE (stmt_info);
1643 nelements = TYPE_VECTOR_SUBPARTS (vectype);
1645 if (known_alignment_for_access_p (dr0))
1647 bool negative = tree_int_cst_compare (DR_STEP (dr0),
1648 size_zero_node) < 0;
1649 if (!npeel)
1651 /* Since it's known at compile time, compute the number of
1652 iterations in the peeled loop (the peeling factor) for use in
1653 updating DR_MISALIGNMENT values. The peeling factor is the
1654 vectorization factor minus the misalignment as an element
1655 count. */
1656 mis = DR_MISALIGNMENT (dr0);
1657 mis /= GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (DR_REF (dr0))));
1658 npeel = ((negative ? mis - nelements : nelements - mis)
1659 & (nelements - 1));
1662 /* For interleaved data access every iteration accesses all the
1663 members of the group, therefore we divide the number of iterations
1664 by the group size. */
1665 stmt_info = vinfo_for_stmt (DR_STMT (dr0));
1666 if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
1667 npeel /= GROUP_SIZE (stmt_info);
1669 if (dump_enabled_p ())
1670 dump_printf_loc (MSG_NOTE, vect_location,
1671 "Try peeling by %d\n", npeel);
1674 /* Ensure that all data refs can be vectorized after the peel. */
1675 FOR_EACH_VEC_ELT (datarefs, i, dr)
1677 int save_misalignment;
1679 if (dr == dr0)
1680 continue;
1682 stmt = DR_STMT (dr);
1683 stmt_info = vinfo_for_stmt (stmt);
1684 /* For interleaving, only the alignment of the first access
1685 matters. */
1686 if (STMT_VINFO_GROUPED_ACCESS (stmt_info)
1687 && GROUP_FIRST_ELEMENT (stmt_info) != stmt)
1688 continue;
1690 /* Strided loads perform only component accesses, alignment is
1691 irrelevant for them. */
1692 if (STMT_VINFO_STRIDE_LOAD_P (stmt_info))
1693 continue;
1695 save_misalignment = DR_MISALIGNMENT (dr);
1696 vect_update_misalignment_for_peel (dr, dr0, npeel);
1697 supportable_dr_alignment = vect_supportable_dr_alignment (dr, false);
1698 SET_DR_MISALIGNMENT (dr, save_misalignment);
1700 if (!supportable_dr_alignment)
1702 do_peeling = false;
1703 break;
1707 if (do_peeling && known_alignment_for_access_p (dr0) && npeel == 0)
1709 stat = vect_verify_datarefs_alignment (loop_vinfo, NULL);
1710 if (!stat)
1711 do_peeling = false;
1712 else
1714 body_cost_vec.release ();
1715 return stat;
1719 if (do_peeling)
1721 unsigned max_allowed_peel
1722 = PARAM_VALUE (PARAM_VECT_MAX_PEELING_FOR_ALIGNMENT);
1723 if (max_allowed_peel != (unsigned)-1)
1725 unsigned max_peel = npeel;
1726 if (max_peel == 0)
1728 gimple dr_stmt = DR_STMT (dr0);
1729 stmt_vec_info vinfo = vinfo_for_stmt (dr_stmt);
1730 tree vtype = STMT_VINFO_VECTYPE (vinfo);
1731 max_peel = TYPE_VECTOR_SUBPARTS (vtype) - 1;
1733 if (max_peel > max_allowed_peel)
1735 do_peeling = false;
1736 if (dump_enabled_p ())
1737 dump_printf_loc (MSG_NOTE, vect_location,
1738 "Disable peeling, max peels reached: %d\n", max_peel);
1743 if (do_peeling)
1745 stmt_info_for_cost *si;
1746 void *data = LOOP_VINFO_TARGET_COST_DATA (loop_vinfo);
1748 /* (1.2) Update the DR_MISALIGNMENT of each data reference DR_i.
1749 If the misalignment of DR_i is identical to that of dr0 then set
1750 DR_MISALIGNMENT (DR_i) to zero. If the misalignment of DR_i and
1751 dr0 are known at compile time then increment DR_MISALIGNMENT (DR_i)
1752 by the peeling factor times the element size of DR_i (MOD the
1753 vectorization factor times the size). Otherwise, the
1754 misalignment of DR_i must be set to unknown. */
1755 FOR_EACH_VEC_ELT (datarefs, i, dr)
1756 if (dr != dr0)
1757 vect_update_misalignment_for_peel (dr, dr0, npeel);
1759 LOOP_VINFO_UNALIGNED_DR (loop_vinfo) = dr0;
1760 if (npeel)
1761 LOOP_PEELING_FOR_ALIGNMENT (loop_vinfo) = npeel;
1762 else
1763 LOOP_PEELING_FOR_ALIGNMENT (loop_vinfo) = DR_MISALIGNMENT (dr0);
1764 SET_DR_MISALIGNMENT (dr0, 0);
1765 if (dump_enabled_p ())
1767 dump_printf_loc (MSG_NOTE, vect_location,
1768 "Alignment of access forced using peeling.\n");
1769 dump_printf_loc (MSG_NOTE, vect_location,
1770 "Peeling for alignment will be applied.\n");
1772 /* We've delayed passing the inside-loop peeling costs to the
1773 target cost model until we were sure peeling would happen.
1774 Do so now. */
1775 if (body_cost_vec.exists ())
1777 FOR_EACH_VEC_ELT (body_cost_vec, i, si)
1779 struct _stmt_vec_info *stmt_info
1780 = si->stmt ? vinfo_for_stmt (si->stmt) : NULL;
1781 (void) add_stmt_cost (data, si->count, si->kind, stmt_info,
1782 si->misalign, vect_body);
1784 body_cost_vec.release ();
1787 stat = vect_verify_datarefs_alignment (loop_vinfo, NULL);
1788 gcc_assert (stat);
1789 return stat;
1793 body_cost_vec.release ();
1795 /* (2) Versioning to force alignment. */
1797 /* Try versioning if:
1798 1) optimize loop for speed
1799 2) there is at least one unsupported misaligned data ref with an unknown
1800 misalignment, and
1801 3) all misaligned data refs with a known misalignment are supported, and
1802 4) the number of runtime alignment checks is within reason. */
1804 do_versioning =
1805 optimize_loop_nest_for_speed_p (loop)
1806 && (!loop->inner); /* FORNOW */
1808 if (do_versioning)
1810 FOR_EACH_VEC_ELT (datarefs, i, dr)
1812 stmt = DR_STMT (dr);
1813 stmt_info = vinfo_for_stmt (stmt);
1815 /* For interleaving, only the alignment of the first access
1816 matters. */
1817 if (aligned_access_p (dr)
1818 || (STMT_VINFO_GROUPED_ACCESS (stmt_info)
1819 && GROUP_FIRST_ELEMENT (stmt_info) != stmt))
1820 continue;
1822 /* Strided loads perform only component accesses, alignment is
1823 irrelevant for them. */
1824 if (STMT_VINFO_STRIDE_LOAD_P (stmt_info))
1825 continue;
1827 supportable_dr_alignment = vect_supportable_dr_alignment (dr, false);
1829 if (!supportable_dr_alignment)
1831 gimple stmt;
1832 int mask;
1833 tree vectype;
1835 if (known_alignment_for_access_p (dr)
1836 || LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo).length ()
1837 >= (unsigned) PARAM_VALUE (PARAM_VECT_MAX_VERSION_FOR_ALIGNMENT_CHECKS))
1839 do_versioning = false;
1840 break;
1843 stmt = DR_STMT (dr);
1844 vectype = STMT_VINFO_VECTYPE (vinfo_for_stmt (stmt));
1845 gcc_assert (vectype);
1847 /* The rightmost bits of an aligned address must be zeros.
1848 Construct the mask needed for this test. For example,
1849 GET_MODE_SIZE for the vector mode V4SI is 16 bytes so the
1850 mask must be 15 = 0xf. */
1851 mask = GET_MODE_SIZE (TYPE_MODE (vectype)) - 1;
1853 /* FORNOW: use the same mask to test all potentially unaligned
1854 references in the loop. The vectorizer currently supports
1855 a single vector size, see the reference to
1856 GET_MODE_NUNITS (TYPE_MODE (vectype)) where the
1857 vectorization factor is computed. */
1858 gcc_assert (!LOOP_VINFO_PTR_MASK (loop_vinfo)
1859 || LOOP_VINFO_PTR_MASK (loop_vinfo) == mask);
1860 LOOP_VINFO_PTR_MASK (loop_vinfo) = mask;
1861 LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo).safe_push (
1862 DR_STMT (dr));
1866 /* Versioning requires at least one misaligned data reference. */
1867 if (!LOOP_REQUIRES_VERSIONING_FOR_ALIGNMENT (loop_vinfo))
1868 do_versioning = false;
1869 else if (!do_versioning)
1870 LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo).truncate (0);
1873 if (do_versioning)
1875 vec<gimple> may_misalign_stmts
1876 = LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo);
1877 gimple stmt;
1879 /* It can now be assumed that the data references in the statements
1880 in LOOP_VINFO_MAY_MISALIGN_STMTS will be aligned in the version
1881 of the loop being vectorized. */
1882 FOR_EACH_VEC_ELT (may_misalign_stmts, i, stmt)
1884 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1885 dr = STMT_VINFO_DATA_REF (stmt_info);
1886 SET_DR_MISALIGNMENT (dr, 0);
1887 if (dump_enabled_p ())
1888 dump_printf_loc (MSG_NOTE, vect_location,
1889 "Alignment of access forced using versioning.\n");
1892 if (dump_enabled_p ())
1893 dump_printf_loc (MSG_NOTE, vect_location,
1894 "Versioning for alignment will be applied.\n");
1896 /* Peeling and versioning can't be done together at this time. */
1897 gcc_assert (! (do_peeling && do_versioning));
1899 stat = vect_verify_datarefs_alignment (loop_vinfo, NULL);
1900 gcc_assert (stat);
1901 return stat;
1904 /* This point is reached if neither peeling nor versioning is being done. */
1905 gcc_assert (! (do_peeling || do_versioning));
1907 stat = vect_verify_datarefs_alignment (loop_vinfo, NULL);
1908 return stat;
1912 /* Function vect_find_same_alignment_drs.
1914 Update group and alignment relations according to the chosen
1915 vectorization factor. */
1917 static void
1918 vect_find_same_alignment_drs (struct data_dependence_relation *ddr,
1919 loop_vec_info loop_vinfo)
1921 unsigned int i;
1922 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
1923 int vectorization_factor = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
1924 struct data_reference *dra = DDR_A (ddr);
1925 struct data_reference *drb = DDR_B (ddr);
1926 stmt_vec_info stmtinfo_a = vinfo_for_stmt (DR_STMT (dra));
1927 stmt_vec_info stmtinfo_b = vinfo_for_stmt (DR_STMT (drb));
1928 int dra_size = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (DR_REF (dra))));
1929 int drb_size = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (DR_REF (drb))));
1930 lambda_vector dist_v;
1931 unsigned int loop_depth;
1933 if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
1934 return;
1936 if (dra == drb)
1937 return;
1939 if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
1940 return;
1942 /* Loop-based vectorization and known data dependence. */
1943 if (DDR_NUM_DIST_VECTS (ddr) == 0)
1944 return;
1946 /* Data-dependence analysis reports a distance vector of zero
1947 for data-references that overlap only in the first iteration
1948 but have different sign step (see PR45764).
1949 So as a sanity check require equal DR_STEP. */
1950 if (!operand_equal_p (DR_STEP (dra), DR_STEP (drb), 0))
1951 return;
1953 loop_depth = index_in_loop_nest (loop->num, DDR_LOOP_NEST (ddr));
1954 FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr), i, dist_v)
1956 int dist = dist_v[loop_depth];
1958 if (dump_enabled_p ())
1959 dump_printf_loc (MSG_NOTE, vect_location,
1960 "dependence distance = %d.\n", dist);
1962 /* Same loop iteration. */
1963 if (dist == 0
1964 || (dist % vectorization_factor == 0 && dra_size == drb_size))
1966 /* Two references with distance zero have the same alignment. */
1967 STMT_VINFO_SAME_ALIGN_REFS (stmtinfo_a).safe_push (drb);
1968 STMT_VINFO_SAME_ALIGN_REFS (stmtinfo_b).safe_push (dra);
1969 if (dump_enabled_p ())
1971 dump_printf_loc (MSG_NOTE, vect_location,
1972 "accesses have the same alignment.\n");
1973 dump_printf (MSG_NOTE,
1974 "dependence distance modulo vf == 0 between ");
1975 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (dra));
1976 dump_printf (MSG_NOTE, " and ");
1977 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (drb));
1978 dump_printf (MSG_NOTE, "\n");
1985 /* Function vect_analyze_data_refs_alignment
1987 Analyze the alignment of the data-references in the loop.
1988 Return FALSE if a data reference is found that cannot be vectorized. */
1990 bool
1991 vect_analyze_data_refs_alignment (loop_vec_info loop_vinfo,
1992 bb_vec_info bb_vinfo)
1994 if (dump_enabled_p ())
1995 dump_printf_loc (MSG_NOTE, vect_location,
1996 "=== vect_analyze_data_refs_alignment ===\n");
1998 /* Mark groups of data references with same alignment using
1999 data dependence information. */
2000 if (loop_vinfo)
2002 vec<ddr_p> ddrs = LOOP_VINFO_DDRS (loop_vinfo);
2003 struct data_dependence_relation *ddr;
2004 unsigned int i;
2006 FOR_EACH_VEC_ELT (ddrs, i, ddr)
2007 vect_find_same_alignment_drs (ddr, loop_vinfo);
2010 if (!vect_compute_data_refs_alignment (loop_vinfo, bb_vinfo))
2012 if (dump_enabled_p ())
2013 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2014 "not vectorized: can't calculate alignment "
2015 "for data ref.\n");
2016 return false;
2019 return true;
2023 /* Analyze groups of accesses: check that DR belongs to a group of
2024 accesses of legal size, step, etc. Detect gaps, single element
2025 interleaving, and other special cases. Set grouped access info.
2026 Collect groups of strided stores for further use in SLP analysis. */
2028 static bool
2029 vect_analyze_group_access (struct data_reference *dr)
2031 tree step = DR_STEP (dr);
2032 tree scalar_type = TREE_TYPE (DR_REF (dr));
2033 HOST_WIDE_INT type_size = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (scalar_type));
2034 gimple stmt = DR_STMT (dr);
2035 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
2036 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
2037 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
2038 HOST_WIDE_INT dr_step = TREE_INT_CST_LOW (step);
2039 HOST_WIDE_INT groupsize, last_accessed_element = 1;
2040 bool slp_impossible = false;
2041 struct loop *loop = NULL;
2043 if (loop_vinfo)
2044 loop = LOOP_VINFO_LOOP (loop_vinfo);
2046 /* For interleaving, GROUPSIZE is STEP counted in elements, i.e., the
2047 size of the interleaving group (including gaps). */
2048 groupsize = absu_hwi (dr_step) / type_size;
2050 /* Not consecutive access is possible only if it is a part of interleaving. */
2051 if (!GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)))
2053 /* Check if it this DR is a part of interleaving, and is a single
2054 element of the group that is accessed in the loop. */
2056 /* Gaps are supported only for loads. STEP must be a multiple of the type
2057 size. The size of the group must be a power of 2. */
2058 if (DR_IS_READ (dr)
2059 && (dr_step % type_size) == 0
2060 && groupsize > 0
2061 && exact_log2 (groupsize) != -1)
2063 GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)) = stmt;
2064 GROUP_SIZE (vinfo_for_stmt (stmt)) = groupsize;
2065 if (dump_enabled_p ())
2067 dump_printf_loc (MSG_NOTE, vect_location,
2068 "Detected single element interleaving ");
2069 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (dr));
2070 dump_printf (MSG_NOTE, " step ");
2071 dump_generic_expr (MSG_NOTE, TDF_SLIM, step);
2072 dump_printf (MSG_NOTE, "\n");
2075 if (loop_vinfo)
2077 if (dump_enabled_p ())
2078 dump_printf_loc (MSG_NOTE, vect_location,
2079 "Data access with gaps requires scalar "
2080 "epilogue loop\n");
2081 if (loop->inner)
2083 if (dump_enabled_p ())
2084 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2085 "Peeling for outer loop is not"
2086 " supported\n");
2087 return false;
2090 LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo) = true;
2093 return true;
2096 if (dump_enabled_p ())
2098 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2099 "not consecutive access ");
2100 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
2101 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
2104 if (bb_vinfo)
2106 /* Mark the statement as unvectorizable. */
2107 STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dr))) = false;
2108 return true;
2111 return false;
2114 if (GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)) == stmt)
2116 /* First stmt in the interleaving chain. Check the chain. */
2117 gimple next = GROUP_NEXT_ELEMENT (vinfo_for_stmt (stmt));
2118 struct data_reference *data_ref = dr;
2119 unsigned int count = 1;
2120 tree prev_init = DR_INIT (data_ref);
2121 gimple prev = stmt;
2122 HOST_WIDE_INT diff, gaps = 0;
2123 unsigned HOST_WIDE_INT count_in_bytes;
2125 while (next)
2127 /* Skip same data-refs. In case that two or more stmts share
2128 data-ref (supported only for loads), we vectorize only the first
2129 stmt, and the rest get their vectorized loads from the first
2130 one. */
2131 if (!tree_int_cst_compare (DR_INIT (data_ref),
2132 DR_INIT (STMT_VINFO_DATA_REF (
2133 vinfo_for_stmt (next)))))
2135 if (DR_IS_WRITE (data_ref))
2137 if (dump_enabled_p ())
2138 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2139 "Two store stmts share the same dr.\n");
2140 return false;
2143 /* For load use the same data-ref load. */
2144 GROUP_SAME_DR_STMT (vinfo_for_stmt (next)) = prev;
2146 prev = next;
2147 next = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next));
2148 continue;
2151 prev = next;
2152 data_ref = STMT_VINFO_DATA_REF (vinfo_for_stmt (next));
2154 /* All group members have the same STEP by construction. */
2155 gcc_checking_assert (operand_equal_p (DR_STEP (data_ref), step, 0));
2157 /* Check that the distance between two accesses is equal to the type
2158 size. Otherwise, we have gaps. */
2159 diff = (TREE_INT_CST_LOW (DR_INIT (data_ref))
2160 - TREE_INT_CST_LOW (prev_init)) / type_size;
2161 if (diff != 1)
2163 /* FORNOW: SLP of accesses with gaps is not supported. */
2164 slp_impossible = true;
2165 if (DR_IS_WRITE (data_ref))
2167 if (dump_enabled_p ())
2168 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2169 "interleaved store with gaps\n");
2170 return false;
2173 gaps += diff - 1;
2176 last_accessed_element += diff;
2178 /* Store the gap from the previous member of the group. If there is no
2179 gap in the access, GROUP_GAP is always 1. */
2180 GROUP_GAP (vinfo_for_stmt (next)) = diff;
2182 prev_init = DR_INIT (data_ref);
2183 next = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next));
2184 /* Count the number of data-refs in the chain. */
2185 count++;
2188 /* COUNT is the number of accesses found, we multiply it by the size of
2189 the type to get COUNT_IN_BYTES. */
2190 count_in_bytes = type_size * count;
2192 /* Check that the size of the interleaving (including gaps) is not
2193 greater than STEP. */
2194 if (dr_step != 0
2195 && absu_hwi (dr_step) < count_in_bytes + gaps * type_size)
2197 if (dump_enabled_p ())
2199 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2200 "interleaving size is greater than step for ");
2201 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
2202 DR_REF (dr));
2203 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
2205 return false;
2208 /* Check that the size of the interleaving is equal to STEP for stores,
2209 i.e., that there are no gaps. */
2210 if (dr_step != 0
2211 && absu_hwi (dr_step) != count_in_bytes)
2213 if (DR_IS_READ (dr))
2215 slp_impossible = true;
2216 /* There is a gap after the last load in the group. This gap is a
2217 difference between the groupsize and the number of elements.
2218 When there is no gap, this difference should be 0. */
2219 GROUP_GAP (vinfo_for_stmt (stmt)) = groupsize - count;
2221 else
2223 if (dump_enabled_p ())
2224 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2225 "interleaved store with gaps\n");
2226 return false;
2230 /* Check that STEP is a multiple of type size. */
2231 if (dr_step != 0
2232 && (dr_step % type_size) != 0)
2234 if (dump_enabled_p ())
2236 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2237 "step is not a multiple of type size: step ");
2238 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, step);
2239 dump_printf (MSG_MISSED_OPTIMIZATION, " size ");
2240 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
2241 TYPE_SIZE_UNIT (scalar_type));
2242 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
2244 return false;
2247 if (groupsize == 0)
2248 groupsize = count;
2250 GROUP_SIZE (vinfo_for_stmt (stmt)) = groupsize;
2251 if (dump_enabled_p ())
2252 dump_printf_loc (MSG_NOTE, vect_location,
2253 "Detected interleaving of size %d\n", (int)groupsize);
2255 /* SLP: create an SLP data structure for every interleaving group of
2256 stores for further analysis in vect_analyse_slp. */
2257 if (DR_IS_WRITE (dr) && !slp_impossible)
2259 if (loop_vinfo)
2260 LOOP_VINFO_GROUPED_STORES (loop_vinfo).safe_push (stmt);
2261 if (bb_vinfo)
2262 BB_VINFO_GROUPED_STORES (bb_vinfo).safe_push (stmt);
2265 /* There is a gap in the end of the group. */
2266 if (groupsize - last_accessed_element > 0 && loop_vinfo)
2268 if (dump_enabled_p ())
2269 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2270 "Data access with gaps requires scalar "
2271 "epilogue loop\n");
2272 if (loop->inner)
2274 if (dump_enabled_p ())
2275 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2276 "Peeling for outer loop is not supported\n");
2277 return false;
2280 LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo) = true;
2284 return true;
2288 /* Analyze the access pattern of the data-reference DR.
2289 In case of non-consecutive accesses call vect_analyze_group_access() to
2290 analyze groups of accesses. */
2292 static bool
2293 vect_analyze_data_ref_access (struct data_reference *dr)
2295 tree step = DR_STEP (dr);
2296 tree scalar_type = TREE_TYPE (DR_REF (dr));
2297 gimple stmt = DR_STMT (dr);
2298 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
2299 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
2300 struct loop *loop = NULL;
2302 if (loop_vinfo)
2303 loop = LOOP_VINFO_LOOP (loop_vinfo);
2305 if (loop_vinfo && !step)
2307 if (dump_enabled_p ())
2308 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2309 "bad data-ref access in loop\n");
2310 return false;
2313 /* Allow invariant loads in not nested loops. */
2314 if (loop_vinfo && integer_zerop (step))
2316 GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)) = NULL;
2317 if (nested_in_vect_loop_p (loop, stmt))
2319 if (dump_enabled_p ())
2320 dump_printf_loc (MSG_NOTE, vect_location,
2321 "zero step in inner loop of nest\n");
2322 return false;
2324 return DR_IS_READ (dr);
2327 if (loop && nested_in_vect_loop_p (loop, stmt))
2329 /* Interleaved accesses are not yet supported within outer-loop
2330 vectorization for references in the inner-loop. */
2331 GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)) = NULL;
2333 /* For the rest of the analysis we use the outer-loop step. */
2334 step = STMT_VINFO_DR_STEP (stmt_info);
2335 if (integer_zerop (step))
2337 if (dump_enabled_p ())
2338 dump_printf_loc (MSG_NOTE, vect_location,
2339 "zero step in outer loop.\n");
2340 if (DR_IS_READ (dr))
2341 return true;
2342 else
2343 return false;
2347 /* Consecutive? */
2348 if (TREE_CODE (step) == INTEGER_CST)
2350 HOST_WIDE_INT dr_step = TREE_INT_CST_LOW (step);
2351 if (!tree_int_cst_compare (step, TYPE_SIZE_UNIT (scalar_type))
2352 || (dr_step < 0
2353 && !compare_tree_int (TYPE_SIZE_UNIT (scalar_type), -dr_step)))
2355 /* Mark that it is not interleaving. */
2356 GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)) = NULL;
2357 return true;
2361 if (loop && nested_in_vect_loop_p (loop, stmt))
2363 if (dump_enabled_p ())
2364 dump_printf_loc (MSG_NOTE, vect_location,
2365 "grouped access in outer loop.\n");
2366 return false;
2369 /* Assume this is a DR handled by non-constant strided load case. */
2370 if (TREE_CODE (step) != INTEGER_CST)
2371 return STMT_VINFO_STRIDE_LOAD_P (stmt_info);
2373 /* Not consecutive access - check if it's a part of interleaving group. */
2374 return vect_analyze_group_access (dr);
2379 /* A helper function used in the comparator function to sort data
2380 references. T1 and T2 are two data references to be compared.
2381 The function returns -1, 0, or 1. */
2383 static int
2384 compare_tree (tree t1, tree t2)
2386 int i, cmp;
2387 enum tree_code code;
2388 char tclass;
2390 if (t1 == t2)
2391 return 0;
2392 if (t1 == NULL)
2393 return -1;
2394 if (t2 == NULL)
2395 return 1;
2398 if (TREE_CODE (t1) != TREE_CODE (t2))
2399 return TREE_CODE (t1) < TREE_CODE (t2) ? -1 : 1;
2401 code = TREE_CODE (t1);
2402 switch (code)
2404 /* For const values, we can just use hash values for comparisons. */
2405 case INTEGER_CST:
2406 case REAL_CST:
2407 case FIXED_CST:
2408 case STRING_CST:
2409 case COMPLEX_CST:
2410 case VECTOR_CST:
2412 hashval_t h1 = iterative_hash_expr (t1, 0);
2413 hashval_t h2 = iterative_hash_expr (t2, 0);
2414 if (h1 != h2)
2415 return h1 < h2 ? -1 : 1;
2416 break;
2419 case SSA_NAME:
2420 cmp = compare_tree (SSA_NAME_VAR (t1), SSA_NAME_VAR (t2));
2421 if (cmp != 0)
2422 return cmp;
2424 if (SSA_NAME_VERSION (t1) != SSA_NAME_VERSION (t2))
2425 return SSA_NAME_VERSION (t1) < SSA_NAME_VERSION (t2) ? -1 : 1;
2426 break;
2428 default:
2429 tclass = TREE_CODE_CLASS (code);
2431 /* For var-decl, we could compare their UIDs. */
2432 if (tclass == tcc_declaration)
2434 if (DECL_UID (t1) != DECL_UID (t2))
2435 return DECL_UID (t1) < DECL_UID (t2) ? -1 : 1;
2436 break;
2439 /* For expressions with operands, compare their operands recursively. */
2440 for (i = TREE_OPERAND_LENGTH (t1) - 1; i >= 0; --i)
2442 cmp = compare_tree (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
2443 if (cmp != 0)
2444 return cmp;
2448 return 0;
2452 /* Compare two data-references DRA and DRB to group them into chunks
2453 suitable for grouping. */
2455 static int
2456 dr_group_sort_cmp (const void *dra_, const void *drb_)
2458 data_reference_p dra = *(data_reference_p *)const_cast<void *>(dra_);
2459 data_reference_p drb = *(data_reference_p *)const_cast<void *>(drb_);
2460 int cmp;
2462 /* Stabilize sort. */
2463 if (dra == drb)
2464 return 0;
2466 /* Ordering of DRs according to base. */
2467 if (!operand_equal_p (DR_BASE_ADDRESS (dra), DR_BASE_ADDRESS (drb), 0))
2469 cmp = compare_tree (DR_BASE_ADDRESS (dra), DR_BASE_ADDRESS (drb));
2470 if (cmp != 0)
2471 return cmp;
2474 /* And according to DR_OFFSET. */
2475 if (!dr_equal_offsets_p (dra, drb))
2477 cmp = compare_tree (DR_OFFSET (dra), DR_OFFSET (drb));
2478 if (cmp != 0)
2479 return cmp;
2482 /* Put reads before writes. */
2483 if (DR_IS_READ (dra) != DR_IS_READ (drb))
2484 return DR_IS_READ (dra) ? -1 : 1;
2486 /* Then sort after access size. */
2487 if (!operand_equal_p (TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dra))),
2488 TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (drb))), 0))
2490 cmp = compare_tree (TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dra))),
2491 TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (drb))));
2492 if (cmp != 0)
2493 return cmp;
2496 /* And after step. */
2497 if (!operand_equal_p (DR_STEP (dra), DR_STEP (drb), 0))
2499 cmp = compare_tree (DR_STEP (dra), DR_STEP (drb));
2500 if (cmp != 0)
2501 return cmp;
2504 /* Then sort after DR_INIT. In case of identical DRs sort after stmt UID. */
2505 cmp = tree_int_cst_compare (DR_INIT (dra), DR_INIT (drb));
2506 if (cmp == 0)
2507 return gimple_uid (DR_STMT (dra)) < gimple_uid (DR_STMT (drb)) ? -1 : 1;
2508 return cmp;
2511 /* Function vect_analyze_data_ref_accesses.
2513 Analyze the access pattern of all the data references in the loop.
2515 FORNOW: the only access pattern that is considered vectorizable is a
2516 simple step 1 (consecutive) access.
2518 FORNOW: handle only arrays and pointer accesses. */
2520 bool
2521 vect_analyze_data_ref_accesses (loop_vec_info loop_vinfo, bb_vec_info bb_vinfo)
2523 unsigned int i;
2524 vec<data_reference_p> datarefs;
2525 struct data_reference *dr;
2527 if (dump_enabled_p ())
2528 dump_printf_loc (MSG_NOTE, vect_location,
2529 "=== vect_analyze_data_ref_accesses ===\n");
2531 if (loop_vinfo)
2532 datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
2533 else
2534 datarefs = BB_VINFO_DATAREFS (bb_vinfo);
2536 if (datarefs.is_empty ())
2537 return true;
2539 /* Sort the array of datarefs to make building the interleaving chains
2540 linear. */
2541 qsort (datarefs.address(), datarefs.length (),
2542 sizeof (data_reference_p), dr_group_sort_cmp);
2544 /* Build the interleaving chains. */
2545 for (i = 0; i < datarefs.length () - 1;)
2547 data_reference_p dra = datarefs[i];
2548 stmt_vec_info stmtinfo_a = vinfo_for_stmt (DR_STMT (dra));
2549 stmt_vec_info lastinfo = NULL;
2550 for (i = i + 1; i < datarefs.length (); ++i)
2552 data_reference_p drb = datarefs[i];
2553 stmt_vec_info stmtinfo_b = vinfo_for_stmt (DR_STMT (drb));
2555 /* ??? Imperfect sorting (non-compatible types, non-modulo
2556 accesses, same accesses) can lead to a group to be artificially
2557 split here as we don't just skip over those. If it really
2558 matters we can push those to a worklist and re-iterate
2559 over them. The we can just skip ahead to the next DR here. */
2561 /* Check that the data-refs have same first location (except init)
2562 and they are both either store or load (not load and store). */
2563 if (DR_IS_READ (dra) != DR_IS_READ (drb)
2564 || !operand_equal_p (DR_BASE_ADDRESS (dra),
2565 DR_BASE_ADDRESS (drb), 0)
2566 || !dr_equal_offsets_p (dra, drb))
2567 break;
2569 /* Check that the data-refs have the same constant size and step. */
2570 tree sza = TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dra)));
2571 tree szb = TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (drb)));
2572 if (!host_integerp (sza, 1)
2573 || !host_integerp (szb, 1)
2574 || !tree_int_cst_equal (sza, szb)
2575 || !host_integerp (DR_STEP (dra), 0)
2576 || !host_integerp (DR_STEP (drb), 0)
2577 || !tree_int_cst_equal (DR_STEP (dra), DR_STEP (drb)))
2578 break;
2580 /* Do not place the same access in the interleaving chain twice. */
2581 if (tree_int_cst_compare (DR_INIT (dra), DR_INIT (drb)) == 0)
2582 break;
2584 /* Check the types are compatible.
2585 ??? We don't distinguish this during sorting. */
2586 if (!types_compatible_p (TREE_TYPE (DR_REF (dra)),
2587 TREE_TYPE (DR_REF (drb))))
2588 break;
2590 /* Sorting has ensured that DR_INIT (dra) <= DR_INIT (drb). */
2591 HOST_WIDE_INT init_a = TREE_INT_CST_LOW (DR_INIT (dra));
2592 HOST_WIDE_INT init_b = TREE_INT_CST_LOW (DR_INIT (drb));
2593 gcc_assert (init_a < init_b);
2595 /* If init_b == init_a + the size of the type * k, we have an
2596 interleaving, and DRA is accessed before DRB. */
2597 HOST_WIDE_INT type_size_a = TREE_INT_CST_LOW (sza);
2598 if ((init_b - init_a) % type_size_a != 0)
2599 break;
2601 /* The step (if not zero) is greater than the difference between
2602 data-refs' inits. This splits groups into suitable sizes. */
2603 HOST_WIDE_INT step = TREE_INT_CST_LOW (DR_STEP (dra));
2604 if (step != 0 && step <= (init_b - init_a))
2605 break;
2607 if (dump_enabled_p ())
2609 dump_printf_loc (MSG_NOTE, vect_location,
2610 "Detected interleaving ");
2611 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (dra));
2612 dump_printf (MSG_NOTE, " and ");
2613 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_REF (drb));
2614 dump_printf (MSG_NOTE, "\n");
2617 /* Link the found element into the group list. */
2618 if (!GROUP_FIRST_ELEMENT (stmtinfo_a))
2620 GROUP_FIRST_ELEMENT (stmtinfo_a) = DR_STMT (dra);
2621 lastinfo = stmtinfo_a;
2623 GROUP_FIRST_ELEMENT (stmtinfo_b) = DR_STMT (dra);
2624 GROUP_NEXT_ELEMENT (lastinfo) = DR_STMT (drb);
2625 lastinfo = stmtinfo_b;
2629 FOR_EACH_VEC_ELT (datarefs, i, dr)
2630 if (STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dr)))
2631 && !vect_analyze_data_ref_access (dr))
2633 if (dump_enabled_p ())
2634 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2635 "not vectorized: complicated access pattern.\n");
2637 if (bb_vinfo)
2639 /* Mark the statement as not vectorizable. */
2640 STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dr))) = false;
2641 continue;
2643 else
2644 return false;
2647 return true;
2650 /* Function vect_prune_runtime_alias_test_list.
2652 Prune a list of ddrs to be tested at run-time by versioning for alias.
2653 Return FALSE if resulting list of ddrs is longer then allowed by
2654 PARAM_VECT_MAX_VERSION_FOR_ALIAS_CHECKS, otherwise return TRUE. */
2656 bool
2657 vect_prune_runtime_alias_test_list (loop_vec_info loop_vinfo)
2659 vec<ddr_p> ddrs =
2660 LOOP_VINFO_MAY_ALIAS_DDRS (loop_vinfo);
2661 unsigned i, j;
2663 if (dump_enabled_p ())
2664 dump_printf_loc (MSG_NOTE, vect_location,
2665 "=== vect_prune_runtime_alias_test_list ===\n");
2667 for (i = 0; i < ddrs.length (); )
2669 bool found;
2670 ddr_p ddr_i;
2672 ddr_i = ddrs[i];
2673 found = false;
2675 for (j = 0; j < i; j++)
2677 ddr_p ddr_j = ddrs[j];
2679 if (vect_vfa_range_equal (ddr_i, ddr_j))
2681 if (dump_enabled_p ())
2683 dump_printf_loc (MSG_NOTE, vect_location,
2684 "found equal ranges ");
2685 dump_generic_expr (MSG_NOTE, TDF_SLIM,
2686 DR_REF (DDR_A (ddr_i)));
2687 dump_printf (MSG_NOTE, ", ");
2688 dump_generic_expr (MSG_NOTE, TDF_SLIM,
2689 DR_REF (DDR_B (ddr_i)));
2690 dump_printf (MSG_NOTE, " and ");
2691 dump_generic_expr (MSG_NOTE, TDF_SLIM,
2692 DR_REF (DDR_A (ddr_j)));
2693 dump_printf (MSG_NOTE, ", ");
2694 dump_generic_expr (MSG_NOTE, TDF_SLIM,
2695 DR_REF (DDR_B (ddr_j)));
2696 dump_printf (MSG_NOTE, "\n");
2698 found = true;
2699 break;
2703 if (found)
2705 ddrs.ordered_remove (i);
2706 continue;
2708 i++;
2711 if (ddrs.length () >
2712 (unsigned) PARAM_VALUE (PARAM_VECT_MAX_VERSION_FOR_ALIAS_CHECKS))
2714 if (dump_enabled_p ())
2716 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2717 "disable versioning for alias - max number of "
2718 "generated checks exceeded.\n");
2721 LOOP_VINFO_MAY_ALIAS_DDRS (loop_vinfo).truncate (0);
2723 return false;
2726 return true;
2729 /* Check whether a non-affine read in stmt is suitable for gather load
2730 and if so, return a builtin decl for that operation. */
2732 tree
2733 vect_check_gather (gimple stmt, loop_vec_info loop_vinfo, tree *basep,
2734 tree *offp, int *scalep)
2736 HOST_WIDE_INT scale = 1, pbitpos, pbitsize;
2737 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
2738 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
2739 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
2740 tree offtype = NULL_TREE;
2741 tree decl, base, off;
2742 enum machine_mode pmode;
2743 int punsignedp, pvolatilep;
2745 /* The gather builtins need address of the form
2746 loop_invariant + vector * {1, 2, 4, 8}
2748 loop_invariant + sign_extend (vector) * { 1, 2, 4, 8 }.
2749 Unfortunately DR_BASE_ADDRESS/DR_OFFSET can be a mixture
2750 of loop invariants/SSA_NAMEs defined in the loop, with casts,
2751 multiplications and additions in it. To get a vector, we need
2752 a single SSA_NAME that will be defined in the loop and will
2753 contain everything that is not loop invariant and that can be
2754 vectorized. The following code attempts to find such a preexistng
2755 SSA_NAME OFF and put the loop invariants into a tree BASE
2756 that can be gimplified before the loop. */
2757 base = get_inner_reference (DR_REF (dr), &pbitsize, &pbitpos, &off,
2758 &pmode, &punsignedp, &pvolatilep, false);
2759 gcc_assert (base != NULL_TREE && (pbitpos % BITS_PER_UNIT) == 0);
2761 if (TREE_CODE (base) == MEM_REF)
2763 if (!integer_zerop (TREE_OPERAND (base, 1)))
2765 if (off == NULL_TREE)
2767 double_int moff = mem_ref_offset (base);
2768 off = double_int_to_tree (sizetype, moff);
2770 else
2771 off = size_binop (PLUS_EXPR, off,
2772 fold_convert (sizetype, TREE_OPERAND (base, 1)));
2774 base = TREE_OPERAND (base, 0);
2776 else
2777 base = build_fold_addr_expr (base);
2779 if (off == NULL_TREE)
2780 off = size_zero_node;
2782 /* If base is not loop invariant, either off is 0, then we start with just
2783 the constant offset in the loop invariant BASE and continue with base
2784 as OFF, otherwise give up.
2785 We could handle that case by gimplifying the addition of base + off
2786 into some SSA_NAME and use that as off, but for now punt. */
2787 if (!expr_invariant_in_loop_p (loop, base))
2789 if (!integer_zerop (off))
2790 return NULL_TREE;
2791 off = base;
2792 base = size_int (pbitpos / BITS_PER_UNIT);
2794 /* Otherwise put base + constant offset into the loop invariant BASE
2795 and continue with OFF. */
2796 else
2798 base = fold_convert (sizetype, base);
2799 base = size_binop (PLUS_EXPR, base, size_int (pbitpos / BITS_PER_UNIT));
2802 /* OFF at this point may be either a SSA_NAME or some tree expression
2803 from get_inner_reference. Try to peel off loop invariants from it
2804 into BASE as long as possible. */
2805 STRIP_NOPS (off);
2806 while (offtype == NULL_TREE)
2808 enum tree_code code;
2809 tree op0, op1, add = NULL_TREE;
2811 if (TREE_CODE (off) == SSA_NAME)
2813 gimple def_stmt = SSA_NAME_DEF_STMT (off);
2815 if (expr_invariant_in_loop_p (loop, off))
2816 return NULL_TREE;
2818 if (gimple_code (def_stmt) != GIMPLE_ASSIGN)
2819 break;
2821 op0 = gimple_assign_rhs1 (def_stmt);
2822 code = gimple_assign_rhs_code (def_stmt);
2823 op1 = gimple_assign_rhs2 (def_stmt);
2825 else
2827 if (get_gimple_rhs_class (TREE_CODE (off)) == GIMPLE_TERNARY_RHS)
2828 return NULL_TREE;
2829 code = TREE_CODE (off);
2830 extract_ops_from_tree (off, &code, &op0, &op1);
2832 switch (code)
2834 case POINTER_PLUS_EXPR:
2835 case PLUS_EXPR:
2836 if (expr_invariant_in_loop_p (loop, op0))
2838 add = op0;
2839 off = op1;
2840 do_add:
2841 add = fold_convert (sizetype, add);
2842 if (scale != 1)
2843 add = size_binop (MULT_EXPR, add, size_int (scale));
2844 base = size_binop (PLUS_EXPR, base, add);
2845 continue;
2847 if (expr_invariant_in_loop_p (loop, op1))
2849 add = op1;
2850 off = op0;
2851 goto do_add;
2853 break;
2854 case MINUS_EXPR:
2855 if (expr_invariant_in_loop_p (loop, op1))
2857 add = fold_convert (sizetype, op1);
2858 add = size_binop (MINUS_EXPR, size_zero_node, add);
2859 off = op0;
2860 goto do_add;
2862 break;
2863 case MULT_EXPR:
2864 if (scale == 1 && host_integerp (op1, 0))
2866 scale = tree_low_cst (op1, 0);
2867 off = op0;
2868 continue;
2870 break;
2871 case SSA_NAME:
2872 off = op0;
2873 continue;
2874 CASE_CONVERT:
2875 if (!POINTER_TYPE_P (TREE_TYPE (op0))
2876 && !INTEGRAL_TYPE_P (TREE_TYPE (op0)))
2877 break;
2878 if (TYPE_PRECISION (TREE_TYPE (op0))
2879 == TYPE_PRECISION (TREE_TYPE (off)))
2881 off = op0;
2882 continue;
2884 if (TYPE_PRECISION (TREE_TYPE (op0))
2885 < TYPE_PRECISION (TREE_TYPE (off)))
2887 off = op0;
2888 offtype = TREE_TYPE (off);
2889 STRIP_NOPS (off);
2890 continue;
2892 break;
2893 default:
2894 break;
2896 break;
2899 /* If at the end OFF still isn't a SSA_NAME or isn't
2900 defined in the loop, punt. */
2901 if (TREE_CODE (off) != SSA_NAME
2902 || expr_invariant_in_loop_p (loop, off))
2903 return NULL_TREE;
2905 if (offtype == NULL_TREE)
2906 offtype = TREE_TYPE (off);
2908 decl = targetm.vectorize.builtin_gather (STMT_VINFO_VECTYPE (stmt_info),
2909 offtype, scale);
2910 if (decl == NULL_TREE)
2911 return NULL_TREE;
2913 if (basep)
2914 *basep = base;
2915 if (offp)
2916 *offp = off;
2917 if (scalep)
2918 *scalep = scale;
2919 return decl;
2922 /* Function vect_analyze_data_refs.
2924 Find all the data references in the loop or basic block.
2926 The general structure of the analysis of data refs in the vectorizer is as
2927 follows:
2928 1- vect_analyze_data_refs(loop/bb): call
2929 compute_data_dependences_for_loop/bb to find and analyze all data-refs
2930 in the loop/bb and their dependences.
2931 2- vect_analyze_dependences(): apply dependence testing using ddrs.
2932 3- vect_analyze_drs_alignment(): check that ref_stmt.alignment is ok.
2933 4- vect_analyze_drs_access(): check that ref_stmt.step is ok.
2937 bool
2938 vect_analyze_data_refs (loop_vec_info loop_vinfo,
2939 bb_vec_info bb_vinfo,
2940 int *min_vf)
2942 struct loop *loop = NULL;
2943 basic_block bb = NULL;
2944 unsigned int i;
2945 vec<data_reference_p> datarefs;
2946 struct data_reference *dr;
2947 tree scalar_type;
2949 if (dump_enabled_p ())
2950 dump_printf_loc (MSG_NOTE, vect_location,
2951 "=== vect_analyze_data_refs ===\n");
2953 if (loop_vinfo)
2955 loop = LOOP_VINFO_LOOP (loop_vinfo);
2956 if (!find_loop_nest (loop, &LOOP_VINFO_LOOP_NEST (loop_vinfo))
2957 || find_data_references_in_loop
2958 (loop, &LOOP_VINFO_DATAREFS (loop_vinfo)))
2960 if (dump_enabled_p ())
2961 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2962 "not vectorized: loop contains function calls"
2963 " or data references that cannot be analyzed\n");
2964 return false;
2967 datarefs = LOOP_VINFO_DATAREFS (loop_vinfo);
2969 else
2971 gimple_stmt_iterator gsi;
2973 bb = BB_VINFO_BB (bb_vinfo);
2974 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2976 gimple stmt = gsi_stmt (gsi);
2977 if (!find_data_references_in_stmt (NULL, stmt,
2978 &BB_VINFO_DATAREFS (bb_vinfo)))
2980 /* Mark the rest of the basic-block as unvectorizable. */
2981 for (; !gsi_end_p (gsi); gsi_next (&gsi))
2983 stmt = gsi_stmt (gsi);
2984 STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (stmt)) = false;
2986 break;
2990 datarefs = BB_VINFO_DATAREFS (bb_vinfo);
2993 /* Go through the data-refs, check that the analysis succeeded. Update
2994 pointer from stmt_vec_info struct to DR and vectype. */
2996 FOR_EACH_VEC_ELT (datarefs, i, dr)
2998 gimple stmt;
2999 stmt_vec_info stmt_info;
3000 tree base, offset, init;
3001 bool gather = false;
3002 bool simd_lane_access = false;
3003 int vf;
3005 again:
3006 if (!dr || !DR_REF (dr))
3008 if (dump_enabled_p ())
3009 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3010 "not vectorized: unhandled data-ref\n");
3011 return false;
3014 stmt = DR_STMT (dr);
3015 stmt_info = vinfo_for_stmt (stmt);
3017 /* Discard clobbers from the dataref vector. We will remove
3018 clobber stmts during vectorization. */
3019 if (gimple_clobber_p (stmt))
3021 if (i == datarefs.length () - 1)
3023 datarefs.pop ();
3024 break;
3026 datarefs[i] = datarefs.pop ();
3027 goto again;
3030 /* Check that analysis of the data-ref succeeded. */
3031 if (!DR_BASE_ADDRESS (dr) || !DR_OFFSET (dr) || !DR_INIT (dr)
3032 || !DR_STEP (dr))
3034 bool maybe_gather
3035 = DR_IS_READ (dr)
3036 && !TREE_THIS_VOLATILE (DR_REF (dr))
3037 && targetm.vectorize.builtin_gather != NULL;
3038 bool maybe_simd_lane_access
3039 = loop_vinfo && loop->simduid;
3041 /* If target supports vector gather loads, or if this might be
3042 a SIMD lane access, see if they can't be used. */
3043 if (loop_vinfo
3044 && (maybe_gather || maybe_simd_lane_access)
3045 && !nested_in_vect_loop_p (loop, stmt))
3047 struct data_reference *newdr
3048 = create_data_ref (NULL, loop_containing_stmt (stmt),
3049 DR_REF (dr), stmt, true);
3050 gcc_assert (newdr != NULL && DR_REF (newdr));
3051 if (DR_BASE_ADDRESS (newdr)
3052 && DR_OFFSET (newdr)
3053 && DR_INIT (newdr)
3054 && DR_STEP (newdr)
3055 && integer_zerop (DR_STEP (newdr)))
3057 if (maybe_simd_lane_access)
3059 tree off = DR_OFFSET (newdr);
3060 STRIP_NOPS (off);
3061 if (TREE_CODE (DR_INIT (newdr)) == INTEGER_CST
3062 && TREE_CODE (off) == MULT_EXPR
3063 && host_integerp (TREE_OPERAND (off, 1), 1))
3065 tree step = TREE_OPERAND (off, 1);
3066 off = TREE_OPERAND (off, 0);
3067 STRIP_NOPS (off);
3068 if (CONVERT_EXPR_P (off)
3069 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (off,
3070 0)))
3071 < TYPE_PRECISION (TREE_TYPE (off)))
3072 off = TREE_OPERAND (off, 0);
3073 if (TREE_CODE (off) == SSA_NAME)
3075 gimple def = SSA_NAME_DEF_STMT (off);
3076 tree reft = TREE_TYPE (DR_REF (newdr));
3077 if (gimple_call_internal_p (def)
3078 && gimple_call_internal_fn (def)
3079 == IFN_GOMP_SIMD_LANE)
3081 tree arg = gimple_call_arg (def, 0);
3082 gcc_assert (TREE_CODE (arg) == SSA_NAME);
3083 arg = SSA_NAME_VAR (arg);
3084 if (arg == loop->simduid
3085 /* For now. */
3086 && tree_int_cst_equal
3087 (TYPE_SIZE_UNIT (reft),
3088 step))
3090 DR_OFFSET (newdr) = ssize_int (0);
3091 DR_STEP (newdr) = step;
3092 DR_ALIGNED_TO (newdr)
3093 = size_int (BIGGEST_ALIGNMENT);
3094 dr = newdr;
3095 simd_lane_access = true;
3101 if (!simd_lane_access && maybe_gather)
3103 dr = newdr;
3104 gather = true;
3107 if (!gather && !simd_lane_access)
3108 free_data_ref (newdr);
3111 if (!gather && !simd_lane_access)
3113 if (dump_enabled_p ())
3115 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3116 "not vectorized: data ref analysis "
3117 "failed ");
3118 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3119 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3122 if (bb_vinfo)
3123 break;
3125 return false;
3129 if (TREE_CODE (DR_BASE_ADDRESS (dr)) == INTEGER_CST)
3131 if (dump_enabled_p ())
3132 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3133 "not vectorized: base addr of dr is a "
3134 "constant\n");
3136 if (bb_vinfo)
3137 break;
3139 if (gather || simd_lane_access)
3140 free_data_ref (dr);
3141 return false;
3144 if (TREE_THIS_VOLATILE (DR_REF (dr)))
3146 if (dump_enabled_p ())
3148 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3149 "not vectorized: volatile type ");
3150 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3151 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3154 if (bb_vinfo)
3155 break;
3157 return false;
3160 if (stmt_can_throw_internal (stmt))
3162 if (dump_enabled_p ())
3164 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3165 "not vectorized: statement can throw an "
3166 "exception ");
3167 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3168 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3171 if (bb_vinfo)
3172 break;
3174 if (gather || simd_lane_access)
3175 free_data_ref (dr);
3176 return false;
3179 if (TREE_CODE (DR_REF (dr)) == COMPONENT_REF
3180 && DECL_BIT_FIELD (TREE_OPERAND (DR_REF (dr), 1)))
3182 if (dump_enabled_p ())
3184 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3185 "not vectorized: statement is bitfield "
3186 "access ");
3187 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3188 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3191 if (bb_vinfo)
3192 break;
3194 if (gather || simd_lane_access)
3195 free_data_ref (dr);
3196 return false;
3199 base = unshare_expr (DR_BASE_ADDRESS (dr));
3200 offset = unshare_expr (DR_OFFSET (dr));
3201 init = unshare_expr (DR_INIT (dr));
3203 if (is_gimple_call (stmt))
3205 if (dump_enabled_p ())
3207 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3208 "not vectorized: dr in a call ");
3209 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3210 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3213 if (bb_vinfo)
3214 break;
3216 if (gather || simd_lane_access)
3217 free_data_ref (dr);
3218 return false;
3221 /* Update DR field in stmt_vec_info struct. */
3223 /* If the dataref is in an inner-loop of the loop that is considered for
3224 for vectorization, we also want to analyze the access relative to
3225 the outer-loop (DR contains information only relative to the
3226 inner-most enclosing loop). We do that by building a reference to the
3227 first location accessed by the inner-loop, and analyze it relative to
3228 the outer-loop. */
3229 if (loop && nested_in_vect_loop_p (loop, stmt))
3231 tree outer_step, outer_base, outer_init;
3232 HOST_WIDE_INT pbitsize, pbitpos;
3233 tree poffset;
3234 enum machine_mode pmode;
3235 int punsignedp, pvolatilep;
3236 affine_iv base_iv, offset_iv;
3237 tree dinit;
3239 /* Build a reference to the first location accessed by the
3240 inner-loop: *(BASE+INIT). (The first location is actually
3241 BASE+INIT+OFFSET, but we add OFFSET separately later). */
3242 tree inner_base = build_fold_indirect_ref
3243 (fold_build_pointer_plus (base, init));
3245 if (dump_enabled_p ())
3247 dump_printf_loc (MSG_NOTE, vect_location,
3248 "analyze in outer-loop: ");
3249 dump_generic_expr (MSG_NOTE, TDF_SLIM, inner_base);
3250 dump_printf (MSG_NOTE, "\n");
3253 outer_base = get_inner_reference (inner_base, &pbitsize, &pbitpos,
3254 &poffset, &pmode, &punsignedp, &pvolatilep, false);
3255 gcc_assert (outer_base != NULL_TREE);
3257 if (pbitpos % BITS_PER_UNIT != 0)
3259 if (dump_enabled_p ())
3260 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3261 "failed: bit offset alignment.\n");
3262 return false;
3265 outer_base = build_fold_addr_expr (outer_base);
3266 if (!simple_iv (loop, loop_containing_stmt (stmt), outer_base,
3267 &base_iv, false))
3269 if (dump_enabled_p ())
3270 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3271 "failed: evolution of base is not affine.\n");
3272 return false;
3275 if (offset)
3277 if (poffset)
3278 poffset = fold_build2 (PLUS_EXPR, TREE_TYPE (offset), offset,
3279 poffset);
3280 else
3281 poffset = offset;
3284 if (!poffset)
3286 offset_iv.base = ssize_int (0);
3287 offset_iv.step = ssize_int (0);
3289 else if (!simple_iv (loop, loop_containing_stmt (stmt), poffset,
3290 &offset_iv, false))
3292 if (dump_enabled_p ())
3293 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3294 "evolution of offset is not affine.\n");
3295 return false;
3298 outer_init = ssize_int (pbitpos / BITS_PER_UNIT);
3299 split_constant_offset (base_iv.base, &base_iv.base, &dinit);
3300 outer_init = size_binop (PLUS_EXPR, outer_init, dinit);
3301 split_constant_offset (offset_iv.base, &offset_iv.base, &dinit);
3302 outer_init = size_binop (PLUS_EXPR, outer_init, dinit);
3304 outer_step = size_binop (PLUS_EXPR,
3305 fold_convert (ssizetype, base_iv.step),
3306 fold_convert (ssizetype, offset_iv.step));
3308 STMT_VINFO_DR_STEP (stmt_info) = outer_step;
3309 /* FIXME: Use canonicalize_base_object_address (base_iv.base); */
3310 STMT_VINFO_DR_BASE_ADDRESS (stmt_info) = base_iv.base;
3311 STMT_VINFO_DR_INIT (stmt_info) = outer_init;
3312 STMT_VINFO_DR_OFFSET (stmt_info) =
3313 fold_convert (ssizetype, offset_iv.base);
3314 STMT_VINFO_DR_ALIGNED_TO (stmt_info) =
3315 size_int (highest_pow2_factor (offset_iv.base));
3317 if (dump_enabled_p ())
3319 dump_printf_loc (MSG_NOTE, vect_location,
3320 "\touter base_address: ");
3321 dump_generic_expr (MSG_NOTE, TDF_SLIM,
3322 STMT_VINFO_DR_BASE_ADDRESS (stmt_info));
3323 dump_printf (MSG_NOTE, "\n\touter offset from base address: ");
3324 dump_generic_expr (MSG_NOTE, TDF_SLIM,
3325 STMT_VINFO_DR_OFFSET (stmt_info));
3326 dump_printf (MSG_NOTE,
3327 "\n\touter constant offset from base address: ");
3328 dump_generic_expr (MSG_NOTE, TDF_SLIM,
3329 STMT_VINFO_DR_INIT (stmt_info));
3330 dump_printf (MSG_NOTE, "\n\touter step: ");
3331 dump_generic_expr (MSG_NOTE, TDF_SLIM,
3332 STMT_VINFO_DR_STEP (stmt_info));
3333 dump_printf (MSG_NOTE, "\n\touter aligned to: ");
3334 dump_generic_expr (MSG_NOTE, TDF_SLIM,
3335 STMT_VINFO_DR_ALIGNED_TO (stmt_info));
3336 dump_printf (MSG_NOTE, "\n");
3340 if (STMT_VINFO_DATA_REF (stmt_info))
3342 if (dump_enabled_p ())
3344 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3345 "not vectorized: more than one data ref "
3346 "in stmt: ");
3347 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3348 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3351 if (bb_vinfo)
3352 break;
3354 if (gather || simd_lane_access)
3355 free_data_ref (dr);
3356 return false;
3359 STMT_VINFO_DATA_REF (stmt_info) = dr;
3360 if (simd_lane_access)
3362 STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info) = true;
3363 datarefs[i] = dr;
3366 /* Set vectype for STMT. */
3367 scalar_type = TREE_TYPE (DR_REF (dr));
3368 STMT_VINFO_VECTYPE (stmt_info) =
3369 get_vectype_for_scalar_type (scalar_type);
3370 if (!STMT_VINFO_VECTYPE (stmt_info))
3372 if (dump_enabled_p ())
3374 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3375 "not vectorized: no vectype for stmt: ");
3376 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3377 dump_printf (MSG_MISSED_OPTIMIZATION, " scalar_type: ");
3378 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_DETAILS,
3379 scalar_type);
3380 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3383 if (bb_vinfo)
3384 break;
3386 if (gather || simd_lane_access)
3388 STMT_VINFO_DATA_REF (stmt_info) = NULL;
3389 free_data_ref (dr);
3391 return false;
3393 else
3395 if (dump_enabled_p ())
3397 dump_printf_loc (MSG_NOTE, vect_location,
3398 "got vectype for stmt: ");
3399 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
3400 dump_generic_expr (MSG_NOTE, TDF_SLIM,
3401 STMT_VINFO_VECTYPE (stmt_info));
3402 dump_printf (MSG_NOTE, "\n");
3406 /* Adjust the minimal vectorization factor according to the
3407 vector type. */
3408 vf = TYPE_VECTOR_SUBPARTS (STMT_VINFO_VECTYPE (stmt_info));
3409 if (vf > *min_vf)
3410 *min_vf = vf;
3412 if (gather)
3414 tree off;
3416 gather = 0 != vect_check_gather (stmt, loop_vinfo, NULL, &off, NULL);
3417 if (gather
3418 && get_vectype_for_scalar_type (TREE_TYPE (off)) == NULL_TREE)
3419 gather = false;
3420 if (!gather)
3422 STMT_VINFO_DATA_REF (stmt_info) = NULL;
3423 free_data_ref (dr);
3424 if (dump_enabled_p ())
3426 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3427 "not vectorized: not suitable for gather "
3428 "load ");
3429 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3430 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3432 return false;
3435 datarefs[i] = dr;
3436 STMT_VINFO_GATHER_P (stmt_info) = true;
3438 else if (loop_vinfo
3439 && TREE_CODE (DR_STEP (dr)) != INTEGER_CST)
3441 if (nested_in_vect_loop_p (loop, stmt)
3442 || !DR_IS_READ (dr))
3444 if (dump_enabled_p ())
3446 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3447 "not vectorized: not suitable for strided "
3448 "load ");
3449 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
3450 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3452 return false;
3454 STMT_VINFO_STRIDE_LOAD_P (stmt_info) = true;
3458 /* If we stopped analysis at the first dataref we could not analyze
3459 when trying to vectorize a basic-block mark the rest of the datarefs
3460 as not vectorizable and truncate the vector of datarefs. That
3461 avoids spending useless time in analyzing their dependence. */
3462 if (i != datarefs.length ())
3464 gcc_assert (bb_vinfo != NULL);
3465 for (unsigned j = i; j < datarefs.length (); ++j)
3467 data_reference_p dr = datarefs[j];
3468 STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dr))) = false;
3469 free_data_ref (dr);
3471 datarefs.truncate (i);
3474 return true;
3478 /* Function vect_get_new_vect_var.
3480 Returns a name for a new variable. The current naming scheme appends the
3481 prefix "vect_" or "vect_p" (depending on the value of VAR_KIND) to
3482 the name of vectorizer generated variables, and appends that to NAME if
3483 provided. */
3485 tree
3486 vect_get_new_vect_var (tree type, enum vect_var_kind var_kind, const char *name)
3488 const char *prefix;
3489 tree new_vect_var;
3491 switch (var_kind)
3493 case vect_simple_var:
3494 prefix = "vect";
3495 break;
3496 case vect_scalar_var:
3497 prefix = "stmp";
3498 break;
3499 case vect_pointer_var:
3500 prefix = "vectp";
3501 break;
3502 default:
3503 gcc_unreachable ();
3506 if (name)
3508 char* tmp = concat (prefix, "_", name, NULL);
3509 new_vect_var = create_tmp_reg (type, tmp);
3510 free (tmp);
3512 else
3513 new_vect_var = create_tmp_reg (type, prefix);
3515 return new_vect_var;
3519 /* Function vect_create_addr_base_for_vector_ref.
3521 Create an expression that computes the address of the first memory location
3522 that will be accessed for a data reference.
3524 Input:
3525 STMT: The statement containing the data reference.
3526 NEW_STMT_LIST: Must be initialized to NULL_TREE or a statement list.
3527 OFFSET: Optional. If supplied, it is be added to the initial address.
3528 LOOP: Specify relative to which loop-nest should the address be computed.
3529 For example, when the dataref is in an inner-loop nested in an
3530 outer-loop that is now being vectorized, LOOP can be either the
3531 outer-loop, or the inner-loop. The first memory location accessed
3532 by the following dataref ('in' points to short):
3534 for (i=0; i<N; i++)
3535 for (j=0; j<M; j++)
3536 s += in[i+j]
3538 is as follows:
3539 if LOOP=i_loop: &in (relative to i_loop)
3540 if LOOP=j_loop: &in+i*2B (relative to j_loop)
3542 Output:
3543 1. Return an SSA_NAME whose value is the address of the memory location of
3544 the first vector of the data reference.
3545 2. If new_stmt_list is not NULL_TREE after return then the caller must insert
3546 these statement(s) which define the returned SSA_NAME.
3548 FORNOW: We are only handling array accesses with step 1. */
3550 tree
3551 vect_create_addr_base_for_vector_ref (gimple stmt,
3552 gimple_seq *new_stmt_list,
3553 tree offset,
3554 struct loop *loop)
3556 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
3557 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
3558 tree data_ref_base;
3559 const char *base_name;
3560 tree addr_base;
3561 tree dest;
3562 gimple_seq seq = NULL;
3563 tree base_offset;
3564 tree init;
3565 tree vect_ptr_type;
3566 tree step = TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dr)));
3567 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
3569 if (loop_vinfo && loop && loop != (gimple_bb (stmt))->loop_father)
3571 struct loop *outer_loop = LOOP_VINFO_LOOP (loop_vinfo);
3573 gcc_assert (nested_in_vect_loop_p (outer_loop, stmt));
3575 data_ref_base = unshare_expr (STMT_VINFO_DR_BASE_ADDRESS (stmt_info));
3576 base_offset = unshare_expr (STMT_VINFO_DR_OFFSET (stmt_info));
3577 init = unshare_expr (STMT_VINFO_DR_INIT (stmt_info));
3579 else
3581 data_ref_base = unshare_expr (DR_BASE_ADDRESS (dr));
3582 base_offset = unshare_expr (DR_OFFSET (dr));
3583 init = unshare_expr (DR_INIT (dr));
3586 if (loop_vinfo)
3587 base_name = get_name (data_ref_base);
3588 else
3590 base_offset = ssize_int (0);
3591 init = ssize_int (0);
3592 base_name = get_name (DR_REF (dr));
3595 /* Create base_offset */
3596 base_offset = size_binop (PLUS_EXPR,
3597 fold_convert (sizetype, base_offset),
3598 fold_convert (sizetype, init));
3600 if (offset)
3602 offset = fold_build2 (MULT_EXPR, sizetype,
3603 fold_convert (sizetype, offset), step);
3604 base_offset = fold_build2 (PLUS_EXPR, sizetype,
3605 base_offset, offset);
3608 /* base + base_offset */
3609 if (loop_vinfo)
3610 addr_base = fold_build_pointer_plus (data_ref_base, base_offset);
3611 else
3613 addr_base = build1 (ADDR_EXPR,
3614 build_pointer_type (TREE_TYPE (DR_REF (dr))),
3615 unshare_expr (DR_REF (dr)));
3618 vect_ptr_type = build_pointer_type (STMT_VINFO_VECTYPE (stmt_info));
3619 addr_base = fold_convert (vect_ptr_type, addr_base);
3620 dest = vect_get_new_vect_var (vect_ptr_type, vect_pointer_var, base_name);
3621 addr_base = force_gimple_operand (addr_base, &seq, false, dest);
3622 gimple_seq_add_seq (new_stmt_list, seq);
3624 if (DR_PTR_INFO (dr)
3625 && TREE_CODE (addr_base) == SSA_NAME)
3627 duplicate_ssa_name_ptr_info (addr_base, DR_PTR_INFO (dr));
3628 if (offset)
3629 mark_ptr_info_alignment_unknown (SSA_NAME_PTR_INFO (addr_base));
3632 if (dump_enabled_p ())
3634 dump_printf_loc (MSG_NOTE, vect_location, "created ");
3635 dump_generic_expr (MSG_NOTE, TDF_SLIM, addr_base);
3636 dump_printf (MSG_NOTE, "\n");
3639 return addr_base;
3643 /* Function vect_create_data_ref_ptr.
3645 Create a new pointer-to-AGGR_TYPE variable (ap), that points to the first
3646 location accessed in the loop by STMT, along with the def-use update
3647 chain to appropriately advance the pointer through the loop iterations.
3648 Also set aliasing information for the pointer. This pointer is used by
3649 the callers to this function to create a memory reference expression for
3650 vector load/store access.
3652 Input:
3653 1. STMT: a stmt that references memory. Expected to be of the form
3654 GIMPLE_ASSIGN <name, data-ref> or
3655 GIMPLE_ASSIGN <data-ref, name>.
3656 2. AGGR_TYPE: the type of the reference, which should be either a vector
3657 or an array.
3658 3. AT_LOOP: the loop where the vector memref is to be created.
3659 4. OFFSET (optional): an offset to be added to the initial address accessed
3660 by the data-ref in STMT.
3661 5. BSI: location where the new stmts are to be placed if there is no loop
3662 6. ONLY_INIT: indicate if ap is to be updated in the loop, or remain
3663 pointing to the initial address.
3665 Output:
3666 1. Declare a new ptr to vector_type, and have it point to the base of the
3667 data reference (initial addressed accessed by the data reference).
3668 For example, for vector of type V8HI, the following code is generated:
3670 v8hi *ap;
3671 ap = (v8hi *)initial_address;
3673 if OFFSET is not supplied:
3674 initial_address = &a[init];
3675 if OFFSET is supplied:
3676 initial_address = &a[init + OFFSET];
3678 Return the initial_address in INITIAL_ADDRESS.
3680 2. If ONLY_INIT is true, just return the initial pointer. Otherwise, also
3681 update the pointer in each iteration of the loop.
3683 Return the increment stmt that updates the pointer in PTR_INCR.
3685 3. Set INV_P to true if the access pattern of the data reference in the
3686 vectorized loop is invariant. Set it to false otherwise.
3688 4. Return the pointer. */
3690 tree
3691 vect_create_data_ref_ptr (gimple stmt, tree aggr_type, struct loop *at_loop,
3692 tree offset, tree *initial_address,
3693 gimple_stmt_iterator *gsi, gimple *ptr_incr,
3694 bool only_init, bool *inv_p)
3696 const char *base_name;
3697 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
3698 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
3699 struct loop *loop = NULL;
3700 bool nested_in_vect_loop = false;
3701 struct loop *containing_loop = NULL;
3702 tree aggr_ptr_type;
3703 tree aggr_ptr;
3704 tree new_temp;
3705 gimple vec_stmt;
3706 gimple_seq new_stmt_list = NULL;
3707 edge pe = NULL;
3708 basic_block new_bb;
3709 tree aggr_ptr_init;
3710 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
3711 tree aptr;
3712 gimple_stmt_iterator incr_gsi;
3713 bool insert_after;
3714 tree indx_before_incr, indx_after_incr;
3715 gimple incr;
3716 tree step;
3717 bb_vec_info bb_vinfo = STMT_VINFO_BB_VINFO (stmt_info);
3719 gcc_assert (TREE_CODE (aggr_type) == ARRAY_TYPE
3720 || TREE_CODE (aggr_type) == VECTOR_TYPE);
3722 if (loop_vinfo)
3724 loop = LOOP_VINFO_LOOP (loop_vinfo);
3725 nested_in_vect_loop = nested_in_vect_loop_p (loop, stmt);
3726 containing_loop = (gimple_bb (stmt))->loop_father;
3727 pe = loop_preheader_edge (loop);
3729 else
3731 gcc_assert (bb_vinfo);
3732 only_init = true;
3733 *ptr_incr = NULL;
3736 /* Check the step (evolution) of the load in LOOP, and record
3737 whether it's invariant. */
3738 if (nested_in_vect_loop)
3739 step = STMT_VINFO_DR_STEP (stmt_info);
3740 else
3741 step = DR_STEP (STMT_VINFO_DATA_REF (stmt_info));
3743 if (integer_zerop (step))
3744 *inv_p = true;
3745 else
3746 *inv_p = false;
3748 /* Create an expression for the first address accessed by this load
3749 in LOOP. */
3750 base_name = get_name (DR_BASE_ADDRESS (dr));
3752 if (dump_enabled_p ())
3754 tree dr_base_type = TREE_TYPE (DR_BASE_OBJECT (dr));
3755 dump_printf_loc (MSG_NOTE, vect_location,
3756 "create %s-pointer variable to type: ",
3757 tree_code_name[(int) TREE_CODE (aggr_type)]);
3758 dump_generic_expr (MSG_NOTE, TDF_SLIM, aggr_type);
3759 if (TREE_CODE (dr_base_type) == ARRAY_TYPE)
3760 dump_printf (MSG_NOTE, " vectorizing an array ref: ");
3761 else if (TREE_CODE (dr_base_type) == VECTOR_TYPE)
3762 dump_printf (MSG_NOTE, " vectorizing a vector ref: ");
3763 else if (TREE_CODE (dr_base_type) == RECORD_TYPE)
3764 dump_printf (MSG_NOTE, " vectorizing a record based array ref: ");
3765 else
3766 dump_printf (MSG_NOTE, " vectorizing a pointer ref: ");
3767 dump_generic_expr (MSG_NOTE, TDF_SLIM, DR_BASE_OBJECT (dr));
3768 dump_printf (MSG_NOTE, "\n");
3771 /* (1) Create the new aggregate-pointer variable.
3772 Vector and array types inherit the alias set of their component
3773 type by default so we need to use a ref-all pointer if the data
3774 reference does not conflict with the created aggregated data
3775 reference because it is not addressable. */
3776 bool need_ref_all = false;
3777 if (!alias_sets_conflict_p (get_alias_set (aggr_type),
3778 get_alias_set (DR_REF (dr))))
3779 need_ref_all = true;
3780 /* Likewise for any of the data references in the stmt group. */
3781 else if (STMT_VINFO_GROUP_SIZE (stmt_info) > 1)
3783 gimple orig_stmt = STMT_VINFO_GROUP_FIRST_ELEMENT (stmt_info);
3786 stmt_vec_info sinfo = vinfo_for_stmt (orig_stmt);
3787 struct data_reference *sdr = STMT_VINFO_DATA_REF (sinfo);
3788 if (!alias_sets_conflict_p (get_alias_set (aggr_type),
3789 get_alias_set (DR_REF (sdr))))
3791 need_ref_all = true;
3792 break;
3794 orig_stmt = STMT_VINFO_GROUP_NEXT_ELEMENT (sinfo);
3796 while (orig_stmt);
3798 aggr_ptr_type = build_pointer_type_for_mode (aggr_type, ptr_mode,
3799 need_ref_all);
3800 aggr_ptr = vect_get_new_vect_var (aggr_ptr_type, vect_pointer_var, base_name);
3803 /* Note: If the dataref is in an inner-loop nested in LOOP, and we are
3804 vectorizing LOOP (i.e., outer-loop vectorization), we need to create two
3805 def-use update cycles for the pointer: one relative to the outer-loop
3806 (LOOP), which is what steps (3) and (4) below do. The other is relative
3807 to the inner-loop (which is the inner-most loop containing the dataref),
3808 and this is done be step (5) below.
3810 When vectorizing inner-most loops, the vectorized loop (LOOP) is also the
3811 inner-most loop, and so steps (3),(4) work the same, and step (5) is
3812 redundant. Steps (3),(4) create the following:
3814 vp0 = &base_addr;
3815 LOOP: vp1 = phi(vp0,vp2)
3818 vp2 = vp1 + step
3819 goto LOOP
3821 If there is an inner-loop nested in loop, then step (5) will also be
3822 applied, and an additional update in the inner-loop will be created:
3824 vp0 = &base_addr;
3825 LOOP: vp1 = phi(vp0,vp2)
3827 inner: vp3 = phi(vp1,vp4)
3828 vp4 = vp3 + inner_step
3829 if () goto inner
3831 vp2 = vp1 + step
3832 if () goto LOOP */
3834 /* (2) Calculate the initial address of the aggregate-pointer, and set
3835 the aggregate-pointer to point to it before the loop. */
3837 /* Create: (&(base[init_val+offset]) in the loop preheader. */
3839 new_temp = vect_create_addr_base_for_vector_ref (stmt, &new_stmt_list,
3840 offset, loop);
3841 if (new_stmt_list)
3843 if (pe)
3845 new_bb = gsi_insert_seq_on_edge_immediate (pe, new_stmt_list);
3846 gcc_assert (!new_bb);
3848 else
3849 gsi_insert_seq_before (gsi, new_stmt_list, GSI_SAME_STMT);
3852 *initial_address = new_temp;
3854 /* Create: p = (aggr_type *) initial_base */
3855 if (TREE_CODE (new_temp) != SSA_NAME
3856 || !useless_type_conversion_p (aggr_ptr_type, TREE_TYPE (new_temp)))
3858 vec_stmt = gimple_build_assign (aggr_ptr,
3859 fold_convert (aggr_ptr_type, new_temp));
3860 aggr_ptr_init = make_ssa_name (aggr_ptr, vec_stmt);
3861 /* Copy the points-to information if it exists. */
3862 if (DR_PTR_INFO (dr))
3863 duplicate_ssa_name_ptr_info (aggr_ptr_init, DR_PTR_INFO (dr));
3864 gimple_assign_set_lhs (vec_stmt, aggr_ptr_init);
3865 if (pe)
3867 new_bb = gsi_insert_on_edge_immediate (pe, vec_stmt);
3868 gcc_assert (!new_bb);
3870 else
3871 gsi_insert_before (gsi, vec_stmt, GSI_SAME_STMT);
3873 else
3874 aggr_ptr_init = new_temp;
3876 /* (3) Handle the updating of the aggregate-pointer inside the loop.
3877 This is needed when ONLY_INIT is false, and also when AT_LOOP is the
3878 inner-loop nested in LOOP (during outer-loop vectorization). */
3880 /* No update in loop is required. */
3881 if (only_init && (!loop_vinfo || at_loop == loop))
3882 aptr = aggr_ptr_init;
3883 else
3885 /* The step of the aggregate pointer is the type size. */
3886 tree iv_step = TYPE_SIZE_UNIT (aggr_type);
3887 /* One exception to the above is when the scalar step of the load in
3888 LOOP is zero. In this case the step here is also zero. */
3889 if (*inv_p)
3890 iv_step = size_zero_node;
3891 else if (tree_int_cst_sgn (step) == -1)
3892 iv_step = fold_build1 (NEGATE_EXPR, TREE_TYPE (iv_step), iv_step);
3894 standard_iv_increment_position (loop, &incr_gsi, &insert_after);
3896 create_iv (aggr_ptr_init,
3897 fold_convert (aggr_ptr_type, iv_step),
3898 aggr_ptr, loop, &incr_gsi, insert_after,
3899 &indx_before_incr, &indx_after_incr);
3900 incr = gsi_stmt (incr_gsi);
3901 set_vinfo_for_stmt (incr, new_stmt_vec_info (incr, loop_vinfo, NULL));
3903 /* Copy the points-to information if it exists. */
3904 if (DR_PTR_INFO (dr))
3906 duplicate_ssa_name_ptr_info (indx_before_incr, DR_PTR_INFO (dr));
3907 duplicate_ssa_name_ptr_info (indx_after_incr, DR_PTR_INFO (dr));
3909 if (ptr_incr)
3910 *ptr_incr = incr;
3912 aptr = indx_before_incr;
3915 if (!nested_in_vect_loop || only_init)
3916 return aptr;
3919 /* (4) Handle the updating of the aggregate-pointer inside the inner-loop
3920 nested in LOOP, if exists. */
3922 gcc_assert (nested_in_vect_loop);
3923 if (!only_init)
3925 standard_iv_increment_position (containing_loop, &incr_gsi,
3926 &insert_after);
3927 create_iv (aptr, fold_convert (aggr_ptr_type, DR_STEP (dr)), aggr_ptr,
3928 containing_loop, &incr_gsi, insert_after, &indx_before_incr,
3929 &indx_after_incr);
3930 incr = gsi_stmt (incr_gsi);
3931 set_vinfo_for_stmt (incr, new_stmt_vec_info (incr, loop_vinfo, NULL));
3933 /* Copy the points-to information if it exists. */
3934 if (DR_PTR_INFO (dr))
3936 duplicate_ssa_name_ptr_info (indx_before_incr, DR_PTR_INFO (dr));
3937 duplicate_ssa_name_ptr_info (indx_after_incr, DR_PTR_INFO (dr));
3939 if (ptr_incr)
3940 *ptr_incr = incr;
3942 return indx_before_incr;
3944 else
3945 gcc_unreachable ();
3949 /* Function bump_vector_ptr
3951 Increment a pointer (to a vector type) by vector-size. If requested,
3952 i.e. if PTR-INCR is given, then also connect the new increment stmt
3953 to the existing def-use update-chain of the pointer, by modifying
3954 the PTR_INCR as illustrated below:
3956 The pointer def-use update-chain before this function:
3957 DATAREF_PTR = phi (p_0, p_2)
3958 ....
3959 PTR_INCR: p_2 = DATAREF_PTR + step
3961 The pointer def-use update-chain after this function:
3962 DATAREF_PTR = phi (p_0, p_2)
3963 ....
3964 NEW_DATAREF_PTR = DATAREF_PTR + BUMP
3965 ....
3966 PTR_INCR: p_2 = NEW_DATAREF_PTR + step
3968 Input:
3969 DATAREF_PTR - ssa_name of a pointer (to vector type) that is being updated
3970 in the loop.
3971 PTR_INCR - optional. The stmt that updates the pointer in each iteration of
3972 the loop. The increment amount across iterations is expected
3973 to be vector_size.
3974 BSI - location where the new update stmt is to be placed.
3975 STMT - the original scalar memory-access stmt that is being vectorized.
3976 BUMP - optional. The offset by which to bump the pointer. If not given,
3977 the offset is assumed to be vector_size.
3979 Output: Return NEW_DATAREF_PTR as illustrated above.
3983 tree
3984 bump_vector_ptr (tree dataref_ptr, gimple ptr_incr, gimple_stmt_iterator *gsi,
3985 gimple stmt, tree bump)
3987 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
3988 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
3989 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
3990 tree update = TYPE_SIZE_UNIT (vectype);
3991 gimple incr_stmt;
3992 ssa_op_iter iter;
3993 use_operand_p use_p;
3994 tree new_dataref_ptr;
3996 if (bump)
3997 update = bump;
3999 new_dataref_ptr = copy_ssa_name (dataref_ptr, NULL);
4000 incr_stmt = gimple_build_assign_with_ops (POINTER_PLUS_EXPR, new_dataref_ptr,
4001 dataref_ptr, update);
4002 vect_finish_stmt_generation (stmt, incr_stmt, gsi);
4004 /* Copy the points-to information if it exists. */
4005 if (DR_PTR_INFO (dr))
4007 duplicate_ssa_name_ptr_info (new_dataref_ptr, DR_PTR_INFO (dr));
4008 mark_ptr_info_alignment_unknown (SSA_NAME_PTR_INFO (new_dataref_ptr));
4011 if (!ptr_incr)
4012 return new_dataref_ptr;
4014 /* Update the vector-pointer's cross-iteration increment. */
4015 FOR_EACH_SSA_USE_OPERAND (use_p, ptr_incr, iter, SSA_OP_USE)
4017 tree use = USE_FROM_PTR (use_p);
4019 if (use == dataref_ptr)
4020 SET_USE (use_p, new_dataref_ptr);
4021 else
4022 gcc_assert (tree_int_cst_compare (use, update) == 0);
4025 return new_dataref_ptr;
4029 /* Function vect_create_destination_var.
4031 Create a new temporary of type VECTYPE. */
4033 tree
4034 vect_create_destination_var (tree scalar_dest, tree vectype)
4036 tree vec_dest;
4037 const char *name;
4038 char *new_name;
4039 tree type;
4040 enum vect_var_kind kind;
4042 kind = vectype ? vect_simple_var : vect_scalar_var;
4043 type = vectype ? vectype : TREE_TYPE (scalar_dest);
4045 gcc_assert (TREE_CODE (scalar_dest) == SSA_NAME);
4047 name = get_name (scalar_dest);
4048 if (name)
4049 asprintf (&new_name, "%s_%u", name, SSA_NAME_VERSION (scalar_dest));
4050 else
4051 asprintf (&new_name, "_%u", SSA_NAME_VERSION (scalar_dest));
4052 vec_dest = vect_get_new_vect_var (type, kind, new_name);
4053 free (new_name);
4055 return vec_dest;
4058 /* Function vect_grouped_store_supported.
4060 Returns TRUE if interleave high and interleave low permutations
4061 are supported, and FALSE otherwise. */
4063 bool
4064 vect_grouped_store_supported (tree vectype, unsigned HOST_WIDE_INT count)
4066 enum machine_mode mode = TYPE_MODE (vectype);
4068 /* vect_permute_store_chain requires the group size to be a power of two. */
4069 if (exact_log2 (count) == -1)
4071 if (dump_enabled_p ())
4072 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4073 "the size of the group of accesses"
4074 " is not a power of 2\n");
4075 return false;
4078 /* Check that the permutation is supported. */
4079 if (VECTOR_MODE_P (mode))
4081 unsigned int i, nelt = GET_MODE_NUNITS (mode);
4082 unsigned char *sel = XALLOCAVEC (unsigned char, nelt);
4083 for (i = 0; i < nelt / 2; i++)
4085 sel[i * 2] = i;
4086 sel[i * 2 + 1] = i + nelt;
4088 if (can_vec_perm_p (mode, false, sel))
4090 for (i = 0; i < nelt; i++)
4091 sel[i] += nelt / 2;
4092 if (can_vec_perm_p (mode, false, sel))
4093 return true;
4097 if (dump_enabled_p ())
4098 dump_printf (MSG_MISSED_OPTIMIZATION,
4099 "interleave op not supported by target.\n");
4100 return false;
4104 /* Return TRUE if vec_store_lanes is available for COUNT vectors of
4105 type VECTYPE. */
4107 bool
4108 vect_store_lanes_supported (tree vectype, unsigned HOST_WIDE_INT count)
4110 return vect_lanes_optab_supported_p ("vec_store_lanes",
4111 vec_store_lanes_optab,
4112 vectype, count);
4116 /* Function vect_permute_store_chain.
4118 Given a chain of interleaved stores in DR_CHAIN of LENGTH that must be
4119 a power of 2, generate interleave_high/low stmts to reorder the data
4120 correctly for the stores. Return the final references for stores in
4121 RESULT_CHAIN.
4123 E.g., LENGTH is 4 and the scalar type is short, i.e., VF is 8.
4124 The input is 4 vectors each containing 8 elements. We assign a number to
4125 each element, the input sequence is:
4127 1st vec: 0 1 2 3 4 5 6 7
4128 2nd vec: 8 9 10 11 12 13 14 15
4129 3rd vec: 16 17 18 19 20 21 22 23
4130 4th vec: 24 25 26 27 28 29 30 31
4132 The output sequence should be:
4134 1st vec: 0 8 16 24 1 9 17 25
4135 2nd vec: 2 10 18 26 3 11 19 27
4136 3rd vec: 4 12 20 28 5 13 21 30
4137 4th vec: 6 14 22 30 7 15 23 31
4139 i.e., we interleave the contents of the four vectors in their order.
4141 We use interleave_high/low instructions to create such output. The input of
4142 each interleave_high/low operation is two vectors:
4143 1st vec 2nd vec
4144 0 1 2 3 4 5 6 7
4145 the even elements of the result vector are obtained left-to-right from the
4146 high/low elements of the first vector. The odd elements of the result are
4147 obtained left-to-right from the high/low elements of the second vector.
4148 The output of interleave_high will be: 0 4 1 5
4149 and of interleave_low: 2 6 3 7
4152 The permutation is done in log LENGTH stages. In each stage interleave_high
4153 and interleave_low stmts are created for each pair of vectors in DR_CHAIN,
4154 where the first argument is taken from the first half of DR_CHAIN and the
4155 second argument from it's second half.
4156 In our example,
4158 I1: interleave_high (1st vec, 3rd vec)
4159 I2: interleave_low (1st vec, 3rd vec)
4160 I3: interleave_high (2nd vec, 4th vec)
4161 I4: interleave_low (2nd vec, 4th vec)
4163 The output for the first stage is:
4165 I1: 0 16 1 17 2 18 3 19
4166 I2: 4 20 5 21 6 22 7 23
4167 I3: 8 24 9 25 10 26 11 27
4168 I4: 12 28 13 29 14 30 15 31
4170 The output of the second stage, i.e. the final result is:
4172 I1: 0 8 16 24 1 9 17 25
4173 I2: 2 10 18 26 3 11 19 27
4174 I3: 4 12 20 28 5 13 21 30
4175 I4: 6 14 22 30 7 15 23 31. */
4177 void
4178 vect_permute_store_chain (vec<tree> dr_chain,
4179 unsigned int length,
4180 gimple stmt,
4181 gimple_stmt_iterator *gsi,
4182 vec<tree> *result_chain)
4184 tree vect1, vect2, high, low;
4185 gimple perm_stmt;
4186 tree vectype = STMT_VINFO_VECTYPE (vinfo_for_stmt (stmt));
4187 tree perm_mask_low, perm_mask_high;
4188 unsigned int i, n;
4189 unsigned int j, nelt = TYPE_VECTOR_SUBPARTS (vectype);
4190 unsigned char *sel = XALLOCAVEC (unsigned char, nelt);
4192 result_chain->quick_grow (length);
4193 memcpy (result_chain->address (), dr_chain.address (),
4194 length * sizeof (tree));
4196 for (i = 0, n = nelt / 2; i < n; i++)
4198 sel[i * 2] = i;
4199 sel[i * 2 + 1] = i + nelt;
4201 perm_mask_high = vect_gen_perm_mask (vectype, sel);
4202 gcc_assert (perm_mask_high != NULL);
4204 for (i = 0; i < nelt; i++)
4205 sel[i] += nelt / 2;
4206 perm_mask_low = vect_gen_perm_mask (vectype, sel);
4207 gcc_assert (perm_mask_low != NULL);
4209 for (i = 0, n = exact_log2 (length); i < n; i++)
4211 for (j = 0; j < length/2; j++)
4213 vect1 = dr_chain[j];
4214 vect2 = dr_chain[j+length/2];
4216 /* Create interleaving stmt:
4217 high = VEC_PERM_EXPR <vect1, vect2, {0, nelt, 1, nelt+1, ...}> */
4218 high = make_temp_ssa_name (vectype, NULL, "vect_inter_high");
4219 perm_stmt
4220 = gimple_build_assign_with_ops (VEC_PERM_EXPR, high,
4221 vect1, vect2, perm_mask_high);
4222 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
4223 (*result_chain)[2*j] = high;
4225 /* Create interleaving stmt:
4226 low = VEC_PERM_EXPR <vect1, vect2, {nelt/2, nelt*3/2, nelt/2+1,
4227 nelt*3/2+1, ...}> */
4228 low = make_temp_ssa_name (vectype, NULL, "vect_inter_low");
4229 perm_stmt
4230 = gimple_build_assign_with_ops (VEC_PERM_EXPR, low,
4231 vect1, vect2, perm_mask_low);
4232 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
4233 (*result_chain)[2*j+1] = low;
4235 memcpy (dr_chain.address (), result_chain->address (),
4236 length * sizeof (tree));
4240 /* Function vect_setup_realignment
4242 This function is called when vectorizing an unaligned load using
4243 the dr_explicit_realign[_optimized] scheme.
4244 This function generates the following code at the loop prolog:
4246 p = initial_addr;
4247 x msq_init = *(floor(p)); # prolog load
4248 realignment_token = call target_builtin;
4249 loop:
4250 x msq = phi (msq_init, ---)
4252 The stmts marked with x are generated only for the case of
4253 dr_explicit_realign_optimized.
4255 The code above sets up a new (vector) pointer, pointing to the first
4256 location accessed by STMT, and a "floor-aligned" load using that pointer.
4257 It also generates code to compute the "realignment-token" (if the relevant
4258 target hook was defined), and creates a phi-node at the loop-header bb
4259 whose arguments are the result of the prolog-load (created by this
4260 function) and the result of a load that takes place in the loop (to be
4261 created by the caller to this function).
4263 For the case of dr_explicit_realign_optimized:
4264 The caller to this function uses the phi-result (msq) to create the
4265 realignment code inside the loop, and sets up the missing phi argument,
4266 as follows:
4267 loop:
4268 msq = phi (msq_init, lsq)
4269 lsq = *(floor(p')); # load in loop
4270 result = realign_load (msq, lsq, realignment_token);
4272 For the case of dr_explicit_realign:
4273 loop:
4274 msq = *(floor(p)); # load in loop
4275 p' = p + (VS-1);
4276 lsq = *(floor(p')); # load in loop
4277 result = realign_load (msq, lsq, realignment_token);
4279 Input:
4280 STMT - (scalar) load stmt to be vectorized. This load accesses
4281 a memory location that may be unaligned.
4282 BSI - place where new code is to be inserted.
4283 ALIGNMENT_SUPPORT_SCHEME - which of the two misalignment handling schemes
4284 is used.
4286 Output:
4287 REALIGNMENT_TOKEN - the result of a call to the builtin_mask_for_load
4288 target hook, if defined.
4289 Return value - the result of the loop-header phi node. */
4291 tree
4292 vect_setup_realignment (gimple stmt, gimple_stmt_iterator *gsi,
4293 tree *realignment_token,
4294 enum dr_alignment_support alignment_support_scheme,
4295 tree init_addr,
4296 struct loop **at_loop)
4298 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
4299 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
4300 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
4301 struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
4302 struct loop *loop = NULL;
4303 edge pe = NULL;
4304 tree scalar_dest = gimple_assign_lhs (stmt);
4305 tree vec_dest;
4306 gimple inc;
4307 tree ptr;
4308 tree data_ref;
4309 gimple new_stmt;
4310 basic_block new_bb;
4311 tree msq_init = NULL_TREE;
4312 tree new_temp;
4313 gimple phi_stmt;
4314 tree msq = NULL_TREE;
4315 gimple_seq stmts = NULL;
4316 bool inv_p;
4317 bool compute_in_loop = false;
4318 bool nested_in_vect_loop = false;
4319 struct loop *containing_loop = (gimple_bb (stmt))->loop_father;
4320 struct loop *loop_for_initial_load = NULL;
4322 if (loop_vinfo)
4324 loop = LOOP_VINFO_LOOP (loop_vinfo);
4325 nested_in_vect_loop = nested_in_vect_loop_p (loop, stmt);
4328 gcc_assert (alignment_support_scheme == dr_explicit_realign
4329 || alignment_support_scheme == dr_explicit_realign_optimized);
4331 /* We need to generate three things:
4332 1. the misalignment computation
4333 2. the extra vector load (for the optimized realignment scheme).
4334 3. the phi node for the two vectors from which the realignment is
4335 done (for the optimized realignment scheme). */
4337 /* 1. Determine where to generate the misalignment computation.
4339 If INIT_ADDR is NULL_TREE, this indicates that the misalignment
4340 calculation will be generated by this function, outside the loop (in the
4341 preheader). Otherwise, INIT_ADDR had already been computed for us by the
4342 caller, inside the loop.
4344 Background: If the misalignment remains fixed throughout the iterations of
4345 the loop, then both realignment schemes are applicable, and also the
4346 misalignment computation can be done outside LOOP. This is because we are
4347 vectorizing LOOP, and so the memory accesses in LOOP advance in steps that
4348 are a multiple of VS (the Vector Size), and therefore the misalignment in
4349 different vectorized LOOP iterations is always the same.
4350 The problem arises only if the memory access is in an inner-loop nested
4351 inside LOOP, which is now being vectorized using outer-loop vectorization.
4352 This is the only case when the misalignment of the memory access may not
4353 remain fixed throughout the iterations of the inner-loop (as explained in
4354 detail in vect_supportable_dr_alignment). In this case, not only is the
4355 optimized realignment scheme not applicable, but also the misalignment
4356 computation (and generation of the realignment token that is passed to
4357 REALIGN_LOAD) have to be done inside the loop.
4359 In short, INIT_ADDR indicates whether we are in a COMPUTE_IN_LOOP mode
4360 or not, which in turn determines if the misalignment is computed inside
4361 the inner-loop, or outside LOOP. */
4363 if (init_addr != NULL_TREE || !loop_vinfo)
4365 compute_in_loop = true;
4366 gcc_assert (alignment_support_scheme == dr_explicit_realign);
4370 /* 2. Determine where to generate the extra vector load.
4372 For the optimized realignment scheme, instead of generating two vector
4373 loads in each iteration, we generate a single extra vector load in the
4374 preheader of the loop, and in each iteration reuse the result of the
4375 vector load from the previous iteration. In case the memory access is in
4376 an inner-loop nested inside LOOP, which is now being vectorized using
4377 outer-loop vectorization, we need to determine whether this initial vector
4378 load should be generated at the preheader of the inner-loop, or can be
4379 generated at the preheader of LOOP. If the memory access has no evolution
4380 in LOOP, it can be generated in the preheader of LOOP. Otherwise, it has
4381 to be generated inside LOOP (in the preheader of the inner-loop). */
4383 if (nested_in_vect_loop)
4385 tree outerloop_step = STMT_VINFO_DR_STEP (stmt_info);
4386 bool invariant_in_outerloop =
4387 (tree_int_cst_compare (outerloop_step, size_zero_node) == 0);
4388 loop_for_initial_load = (invariant_in_outerloop ? loop : loop->inner);
4390 else
4391 loop_for_initial_load = loop;
4392 if (at_loop)
4393 *at_loop = loop_for_initial_load;
4395 if (loop_for_initial_load)
4396 pe = loop_preheader_edge (loop_for_initial_load);
4398 /* 3. For the case of the optimized realignment, create the first vector
4399 load at the loop preheader. */
4401 if (alignment_support_scheme == dr_explicit_realign_optimized)
4403 /* Create msq_init = *(floor(p1)) in the loop preheader */
4405 gcc_assert (!compute_in_loop);
4406 vec_dest = vect_create_destination_var (scalar_dest, vectype);
4407 ptr = vect_create_data_ref_ptr (stmt, vectype, loop_for_initial_load,
4408 NULL_TREE, &init_addr, NULL, &inc,
4409 true, &inv_p);
4410 new_temp = copy_ssa_name (ptr, NULL);
4411 new_stmt = gimple_build_assign_with_ops
4412 (BIT_AND_EXPR, new_temp, ptr,
4413 build_int_cst (TREE_TYPE (ptr),
4414 -(HOST_WIDE_INT)TYPE_ALIGN_UNIT (vectype)));
4415 new_bb = gsi_insert_on_edge_immediate (pe, new_stmt);
4416 gcc_assert (!new_bb);
4417 data_ref
4418 = build2 (MEM_REF, TREE_TYPE (vec_dest), new_temp,
4419 build_int_cst (reference_alias_ptr_type (DR_REF (dr)), 0));
4420 new_stmt = gimple_build_assign (vec_dest, data_ref);
4421 new_temp = make_ssa_name (vec_dest, new_stmt);
4422 gimple_assign_set_lhs (new_stmt, new_temp);
4423 if (pe)
4425 new_bb = gsi_insert_on_edge_immediate (pe, new_stmt);
4426 gcc_assert (!new_bb);
4428 else
4429 gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
4431 msq_init = gimple_assign_lhs (new_stmt);
4434 /* 4. Create realignment token using a target builtin, if available.
4435 It is done either inside the containing loop, or before LOOP (as
4436 determined above). */
4438 if (targetm.vectorize.builtin_mask_for_load)
4440 tree builtin_decl;
4442 /* Compute INIT_ADDR - the initial addressed accessed by this memref. */
4443 if (!init_addr)
4445 /* Generate the INIT_ADDR computation outside LOOP. */
4446 init_addr = vect_create_addr_base_for_vector_ref (stmt, &stmts,
4447 NULL_TREE, loop);
4448 if (loop)
4450 pe = loop_preheader_edge (loop);
4451 new_bb = gsi_insert_seq_on_edge_immediate (pe, stmts);
4452 gcc_assert (!new_bb);
4454 else
4455 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
4458 builtin_decl = targetm.vectorize.builtin_mask_for_load ();
4459 new_stmt = gimple_build_call (builtin_decl, 1, init_addr);
4460 vec_dest =
4461 vect_create_destination_var (scalar_dest,
4462 gimple_call_return_type (new_stmt));
4463 new_temp = make_ssa_name (vec_dest, new_stmt);
4464 gimple_call_set_lhs (new_stmt, new_temp);
4466 if (compute_in_loop)
4467 gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
4468 else
4470 /* Generate the misalignment computation outside LOOP. */
4471 pe = loop_preheader_edge (loop);
4472 new_bb = gsi_insert_on_edge_immediate (pe, new_stmt);
4473 gcc_assert (!new_bb);
4476 *realignment_token = gimple_call_lhs (new_stmt);
4478 /* The result of the CALL_EXPR to this builtin is determined from
4479 the value of the parameter and no global variables are touched
4480 which makes the builtin a "const" function. Requiring the
4481 builtin to have the "const" attribute makes it unnecessary
4482 to call mark_call_clobbered. */
4483 gcc_assert (TREE_READONLY (builtin_decl));
4486 if (alignment_support_scheme == dr_explicit_realign)
4487 return msq;
4489 gcc_assert (!compute_in_loop);
4490 gcc_assert (alignment_support_scheme == dr_explicit_realign_optimized);
4493 /* 5. Create msq = phi <msq_init, lsq> in loop */
4495 pe = loop_preheader_edge (containing_loop);
4496 vec_dest = vect_create_destination_var (scalar_dest, vectype);
4497 msq = make_ssa_name (vec_dest, NULL);
4498 phi_stmt = create_phi_node (msq, containing_loop->header);
4499 add_phi_arg (phi_stmt, msq_init, pe, UNKNOWN_LOCATION);
4501 return msq;
4505 /* Function vect_grouped_load_supported.
4507 Returns TRUE if even and odd permutations are supported,
4508 and FALSE otherwise. */
4510 bool
4511 vect_grouped_load_supported (tree vectype, unsigned HOST_WIDE_INT count)
4513 enum machine_mode mode = TYPE_MODE (vectype);
4515 /* vect_permute_load_chain requires the group size to be a power of two. */
4516 if (exact_log2 (count) == -1)
4518 if (dump_enabled_p ())
4519 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4520 "the size of the group of accesses"
4521 " is not a power of 2\n");
4522 return false;
4525 /* Check that the permutation is supported. */
4526 if (VECTOR_MODE_P (mode))
4528 unsigned int i, nelt = GET_MODE_NUNITS (mode);
4529 unsigned char *sel = XALLOCAVEC (unsigned char, nelt);
4531 for (i = 0; i < nelt; i++)
4532 sel[i] = i * 2;
4533 if (can_vec_perm_p (mode, false, sel))
4535 for (i = 0; i < nelt; i++)
4536 sel[i] = i * 2 + 1;
4537 if (can_vec_perm_p (mode, false, sel))
4538 return true;
4542 if (dump_enabled_p ())
4543 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4544 "extract even/odd not supported by target\n");
4545 return false;
4548 /* Return TRUE if vec_load_lanes is available for COUNT vectors of
4549 type VECTYPE. */
4551 bool
4552 vect_load_lanes_supported (tree vectype, unsigned HOST_WIDE_INT count)
4554 return vect_lanes_optab_supported_p ("vec_load_lanes",
4555 vec_load_lanes_optab,
4556 vectype, count);
4559 /* Function vect_permute_load_chain.
4561 Given a chain of interleaved loads in DR_CHAIN of LENGTH that must be
4562 a power of 2, generate extract_even/odd stmts to reorder the input data
4563 correctly. Return the final references for loads in RESULT_CHAIN.
4565 E.g., LENGTH is 4 and the scalar type is short, i.e., VF is 8.
4566 The input is 4 vectors each containing 8 elements. We assign a number to each
4567 element, the input sequence is:
4569 1st vec: 0 1 2 3 4 5 6 7
4570 2nd vec: 8 9 10 11 12 13 14 15
4571 3rd vec: 16 17 18 19 20 21 22 23
4572 4th vec: 24 25 26 27 28 29 30 31
4574 The output sequence should be:
4576 1st vec: 0 4 8 12 16 20 24 28
4577 2nd vec: 1 5 9 13 17 21 25 29
4578 3rd vec: 2 6 10 14 18 22 26 30
4579 4th vec: 3 7 11 15 19 23 27 31
4581 i.e., the first output vector should contain the first elements of each
4582 interleaving group, etc.
4584 We use extract_even/odd instructions to create such output. The input of
4585 each extract_even/odd operation is two vectors
4586 1st vec 2nd vec
4587 0 1 2 3 4 5 6 7
4589 and the output is the vector of extracted even/odd elements. The output of
4590 extract_even will be: 0 2 4 6
4591 and of extract_odd: 1 3 5 7
4594 The permutation is done in log LENGTH stages. In each stage extract_even
4595 and extract_odd stmts are created for each pair of vectors in DR_CHAIN in
4596 their order. In our example,
4598 E1: extract_even (1st vec, 2nd vec)
4599 E2: extract_odd (1st vec, 2nd vec)
4600 E3: extract_even (3rd vec, 4th vec)
4601 E4: extract_odd (3rd vec, 4th vec)
4603 The output for the first stage will be:
4605 E1: 0 2 4 6 8 10 12 14
4606 E2: 1 3 5 7 9 11 13 15
4607 E3: 16 18 20 22 24 26 28 30
4608 E4: 17 19 21 23 25 27 29 31
4610 In order to proceed and create the correct sequence for the next stage (or
4611 for the correct output, if the second stage is the last one, as in our
4612 example), we first put the output of extract_even operation and then the
4613 output of extract_odd in RESULT_CHAIN (which is then copied to DR_CHAIN).
4614 The input for the second stage is:
4616 1st vec (E1): 0 2 4 6 8 10 12 14
4617 2nd vec (E3): 16 18 20 22 24 26 28 30
4618 3rd vec (E2): 1 3 5 7 9 11 13 15
4619 4th vec (E4): 17 19 21 23 25 27 29 31
4621 The output of the second stage:
4623 E1: 0 4 8 12 16 20 24 28
4624 E2: 2 6 10 14 18 22 26 30
4625 E3: 1 5 9 13 17 21 25 29
4626 E4: 3 7 11 15 19 23 27 31
4628 And RESULT_CHAIN after reordering:
4630 1st vec (E1): 0 4 8 12 16 20 24 28
4631 2nd vec (E3): 1 5 9 13 17 21 25 29
4632 3rd vec (E2): 2 6 10 14 18 22 26 30
4633 4th vec (E4): 3 7 11 15 19 23 27 31. */
4635 static void
4636 vect_permute_load_chain (vec<tree> dr_chain,
4637 unsigned int length,
4638 gimple stmt,
4639 gimple_stmt_iterator *gsi,
4640 vec<tree> *result_chain)
4642 tree data_ref, first_vect, second_vect;
4643 tree perm_mask_even, perm_mask_odd;
4644 gimple perm_stmt;
4645 tree vectype = STMT_VINFO_VECTYPE (vinfo_for_stmt (stmt));
4646 unsigned int i, j, log_length = exact_log2 (length);
4647 unsigned nelt = TYPE_VECTOR_SUBPARTS (vectype);
4648 unsigned char *sel = XALLOCAVEC (unsigned char, nelt);
4650 result_chain->quick_grow (length);
4651 memcpy (result_chain->address (), dr_chain.address (),
4652 length * sizeof (tree));
4654 for (i = 0; i < nelt; ++i)
4655 sel[i] = i * 2;
4656 perm_mask_even = vect_gen_perm_mask (vectype, sel);
4657 gcc_assert (perm_mask_even != NULL);
4659 for (i = 0; i < nelt; ++i)
4660 sel[i] = i * 2 + 1;
4661 perm_mask_odd = vect_gen_perm_mask (vectype, sel);
4662 gcc_assert (perm_mask_odd != NULL);
4664 for (i = 0; i < log_length; i++)
4666 for (j = 0; j < length; j += 2)
4668 first_vect = dr_chain[j];
4669 second_vect = dr_chain[j+1];
4671 /* data_ref = permute_even (first_data_ref, second_data_ref); */
4672 data_ref = make_temp_ssa_name (vectype, NULL, "vect_perm_even");
4673 perm_stmt = gimple_build_assign_with_ops (VEC_PERM_EXPR, data_ref,
4674 first_vect, second_vect,
4675 perm_mask_even);
4676 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
4677 (*result_chain)[j/2] = data_ref;
4679 /* data_ref = permute_odd (first_data_ref, second_data_ref); */
4680 data_ref = make_temp_ssa_name (vectype, NULL, "vect_perm_odd");
4681 perm_stmt = gimple_build_assign_with_ops (VEC_PERM_EXPR, data_ref,
4682 first_vect, second_vect,
4683 perm_mask_odd);
4684 vect_finish_stmt_generation (stmt, perm_stmt, gsi);
4685 (*result_chain)[j/2+length/2] = data_ref;
4687 memcpy (dr_chain.address (), result_chain->address (),
4688 length * sizeof (tree));
4693 /* Function vect_transform_grouped_load.
4695 Given a chain of input interleaved data-refs (in DR_CHAIN), build statements
4696 to perform their permutation and ascribe the result vectorized statements to
4697 the scalar statements.
4700 void
4701 vect_transform_grouped_load (gimple stmt, vec<tree> dr_chain, int size,
4702 gimple_stmt_iterator *gsi)
4704 vec<tree> result_chain = vNULL;
4706 /* DR_CHAIN contains input data-refs that are a part of the interleaving.
4707 RESULT_CHAIN is the output of vect_permute_load_chain, it contains permuted
4708 vectors, that are ready for vector computation. */
4709 result_chain.create (size);
4710 vect_permute_load_chain (dr_chain, size, stmt, gsi, &result_chain);
4711 vect_record_grouped_load_vectors (stmt, result_chain);
4712 result_chain.release ();
4715 /* RESULT_CHAIN contains the output of a group of grouped loads that were
4716 generated as part of the vectorization of STMT. Assign the statement
4717 for each vector to the associated scalar statement. */
4719 void
4720 vect_record_grouped_load_vectors (gimple stmt, vec<tree> result_chain)
4722 gimple first_stmt = GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt));
4723 gimple next_stmt, new_stmt;
4724 unsigned int i, gap_count;
4725 tree tmp_data_ref;
4727 /* Put a permuted data-ref in the VECTORIZED_STMT field.
4728 Since we scan the chain starting from it's first node, their order
4729 corresponds the order of data-refs in RESULT_CHAIN. */
4730 next_stmt = first_stmt;
4731 gap_count = 1;
4732 FOR_EACH_VEC_ELT (result_chain, i, tmp_data_ref)
4734 if (!next_stmt)
4735 break;
4737 /* Skip the gaps. Loads created for the gaps will be removed by dead
4738 code elimination pass later. No need to check for the first stmt in
4739 the group, since it always exists.
4740 GROUP_GAP is the number of steps in elements from the previous
4741 access (if there is no gap GROUP_GAP is 1). We skip loads that
4742 correspond to the gaps. */
4743 if (next_stmt != first_stmt
4744 && gap_count < GROUP_GAP (vinfo_for_stmt (next_stmt)))
4746 gap_count++;
4747 continue;
4750 while (next_stmt)
4752 new_stmt = SSA_NAME_DEF_STMT (tmp_data_ref);
4753 /* We assume that if VEC_STMT is not NULL, this is a case of multiple
4754 copies, and we put the new vector statement in the first available
4755 RELATED_STMT. */
4756 if (!STMT_VINFO_VEC_STMT (vinfo_for_stmt (next_stmt)))
4757 STMT_VINFO_VEC_STMT (vinfo_for_stmt (next_stmt)) = new_stmt;
4758 else
4760 if (!GROUP_SAME_DR_STMT (vinfo_for_stmt (next_stmt)))
4762 gimple prev_stmt =
4763 STMT_VINFO_VEC_STMT (vinfo_for_stmt (next_stmt));
4764 gimple rel_stmt =
4765 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (prev_stmt));
4766 while (rel_stmt)
4768 prev_stmt = rel_stmt;
4769 rel_stmt =
4770 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (rel_stmt));
4773 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (prev_stmt)) =
4774 new_stmt;
4778 next_stmt = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next_stmt));
4779 gap_count = 1;
4780 /* If NEXT_STMT accesses the same DR as the previous statement,
4781 put the same TMP_DATA_REF as its vectorized statement; otherwise
4782 get the next data-ref from RESULT_CHAIN. */
4783 if (!next_stmt || !GROUP_SAME_DR_STMT (vinfo_for_stmt (next_stmt)))
4784 break;
4789 /* Function vect_force_dr_alignment_p.
4791 Returns whether the alignment of a DECL can be forced to be aligned
4792 on ALIGNMENT bit boundary. */
4794 bool
4795 vect_can_force_dr_alignment_p (const_tree decl, unsigned int alignment)
4797 if (TREE_CODE (decl) != VAR_DECL)
4798 return false;
4800 /* We cannot change alignment of common or external symbols as another
4801 translation unit may contain a definition with lower alignment.
4802 The rules of common symbol linking mean that the definition
4803 will override the common symbol. The same is true for constant
4804 pool entries which may be shared and are not properly merged
4805 by LTO. */
4806 if (DECL_EXTERNAL (decl)
4807 || DECL_COMMON (decl)
4808 || DECL_IN_CONSTANT_POOL (decl))
4809 return false;
4811 if (TREE_ASM_WRITTEN (decl))
4812 return false;
4814 /* Do not override the alignment as specified by the ABI when the used
4815 attribute is set. */
4816 if (DECL_PRESERVE_P (decl))
4817 return false;
4819 /* Do not override explicit alignment set by the user when an explicit
4820 section name is also used. This is a common idiom used by many
4821 software projects. */
4822 if (DECL_SECTION_NAME (decl) != NULL_TREE
4823 && !DECL_HAS_IMPLICIT_SECTION_NAME_P (decl))
4824 return false;
4826 if (TREE_STATIC (decl))
4827 return (alignment <= MAX_OFILE_ALIGNMENT);
4828 else
4829 return (alignment <= MAX_STACK_ALIGNMENT);
4833 /* Return whether the data reference DR is supported with respect to its
4834 alignment.
4835 If CHECK_ALIGNED_ACCESSES is TRUE, check if the access is supported even
4836 it is aligned, i.e., check if it is possible to vectorize it with different
4837 alignment. */
4839 enum dr_alignment_support
4840 vect_supportable_dr_alignment (struct data_reference *dr,
4841 bool check_aligned_accesses)
4843 gimple stmt = DR_STMT (dr);
4844 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
4845 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
4846 enum machine_mode mode = TYPE_MODE (vectype);
4847 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
4848 struct loop *vect_loop = NULL;
4849 bool nested_in_vect_loop = false;
4851 if (aligned_access_p (dr) && !check_aligned_accesses)
4852 return dr_aligned;
4854 if (loop_vinfo)
4856 vect_loop = LOOP_VINFO_LOOP (loop_vinfo);
4857 nested_in_vect_loop = nested_in_vect_loop_p (vect_loop, stmt);
4860 /* Possibly unaligned access. */
4862 /* We can choose between using the implicit realignment scheme (generating
4863 a misaligned_move stmt) and the explicit realignment scheme (generating
4864 aligned loads with a REALIGN_LOAD). There are two variants to the
4865 explicit realignment scheme: optimized, and unoptimized.
4866 We can optimize the realignment only if the step between consecutive
4867 vector loads is equal to the vector size. Since the vector memory
4868 accesses advance in steps of VS (Vector Size) in the vectorized loop, it
4869 is guaranteed that the misalignment amount remains the same throughout the
4870 execution of the vectorized loop. Therefore, we can create the
4871 "realignment token" (the permutation mask that is passed to REALIGN_LOAD)
4872 at the loop preheader.
4874 However, in the case of outer-loop vectorization, when vectorizing a
4875 memory access in the inner-loop nested within the LOOP that is now being
4876 vectorized, while it is guaranteed that the misalignment of the
4877 vectorized memory access will remain the same in different outer-loop
4878 iterations, it is *not* guaranteed that is will remain the same throughout
4879 the execution of the inner-loop. This is because the inner-loop advances
4880 with the original scalar step (and not in steps of VS). If the inner-loop
4881 step happens to be a multiple of VS, then the misalignment remains fixed
4882 and we can use the optimized realignment scheme. For example:
4884 for (i=0; i<N; i++)
4885 for (j=0; j<M; j++)
4886 s += a[i+j];
4888 When vectorizing the i-loop in the above example, the step between
4889 consecutive vector loads is 1, and so the misalignment does not remain
4890 fixed across the execution of the inner-loop, and the realignment cannot
4891 be optimized (as illustrated in the following pseudo vectorized loop):
4893 for (i=0; i<N; i+=4)
4894 for (j=0; j<M; j++){
4895 vs += vp[i+j]; // misalignment of &vp[i+j] is {0,1,2,3,0,1,2,3,...}
4896 // when j is {0,1,2,3,4,5,6,7,...} respectively.
4897 // (assuming that we start from an aligned address).
4900 We therefore have to use the unoptimized realignment scheme:
4902 for (i=0; i<N; i+=4)
4903 for (j=k; j<M; j+=4)
4904 vs += vp[i+j]; // misalignment of &vp[i+j] is always k (assuming
4905 // that the misalignment of the initial address is
4906 // 0).
4908 The loop can then be vectorized as follows:
4910 for (k=0; k<4; k++){
4911 rt = get_realignment_token (&vp[k]);
4912 for (i=0; i<N; i+=4){
4913 v1 = vp[i+k];
4914 for (j=k; j<M; j+=4){
4915 v2 = vp[i+j+VS-1];
4916 va = REALIGN_LOAD <v1,v2,rt>;
4917 vs += va;
4918 v1 = v2;
4921 } */
4923 if (DR_IS_READ (dr))
4925 bool is_packed = false;
4926 tree type = (TREE_TYPE (DR_REF (dr)));
4928 if (optab_handler (vec_realign_load_optab, mode) != CODE_FOR_nothing
4929 && (!targetm.vectorize.builtin_mask_for_load
4930 || targetm.vectorize.builtin_mask_for_load ()))
4932 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
4933 if ((nested_in_vect_loop
4934 && (TREE_INT_CST_LOW (DR_STEP (dr))
4935 != GET_MODE_SIZE (TYPE_MODE (vectype))))
4936 || !loop_vinfo)
4937 return dr_explicit_realign;
4938 else
4939 return dr_explicit_realign_optimized;
4941 if (!known_alignment_for_access_p (dr))
4942 is_packed = not_size_aligned (DR_REF (dr));
4944 if ((TYPE_USER_ALIGN (type) && !is_packed)
4945 || targetm.vectorize.
4946 support_vector_misalignment (mode, type,
4947 DR_MISALIGNMENT (dr), is_packed))
4948 /* Can't software pipeline the loads, but can at least do them. */
4949 return dr_unaligned_supported;
4951 else
4953 bool is_packed = false;
4954 tree type = (TREE_TYPE (DR_REF (dr)));
4956 if (!known_alignment_for_access_p (dr))
4957 is_packed = not_size_aligned (DR_REF (dr));
4959 if ((TYPE_USER_ALIGN (type) && !is_packed)
4960 || targetm.vectorize.
4961 support_vector_misalignment (mode, type,
4962 DR_MISALIGNMENT (dr), is_packed))
4963 return dr_unaligned_supported;
4966 /* Unsupported. */
4967 return dr_unaligned_unsupported;