1 /* Data References Analysis and Manipulation Utilities for Vectorization.
2 Copyright (C) 2003-2014 Free Software Foundation, Inc.
3 Contributed by Dorit Naishlos <dorit@il.ibm.com>
4 and Ira Rosen <irar@il.ibm.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
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
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/>. */
24 #include "coretypes.h"
28 #include "stor-layout.h"
31 #include "basic-block.h"
32 #include "gimple-pretty-print.h"
33 #include "tree-ssa-alias.h"
34 #include "internal-fn.h"
36 #include "gimple-expr.h"
40 #include "gimple-iterator.h"
41 #include "gimplify-me.h"
42 #include "gimple-ssa.h"
43 #include "tree-phinodes.h"
44 #include "ssa-iterators.h"
45 #include "stringpool.h"
46 #include "tree-ssanames.h"
47 #include "tree-ssa-loop-ivopts.h"
48 #include "tree-ssa-loop-manip.h"
49 #include "tree-ssa-loop.h"
52 #include "tree-chrec.h"
53 #include "tree-scalar-evolution.h"
54 #include "tree-vectorizer.h"
55 #include "diagnostic-core.h"
57 /* Need to include rtl.h, expr.h, etc. for optabs. */
61 /* Return true if load- or store-lanes optab OPTAB is implemented for
62 COUNT vectors of type VECTYPE. NAME is the name of OPTAB. */
65 vect_lanes_optab_supported_p (const char *name
, convert_optab optab
,
66 tree vectype
, unsigned HOST_WIDE_INT count
)
68 enum machine_mode mode
, array_mode
;
71 mode
= TYPE_MODE (vectype
);
72 limit_p
= !targetm
.array_mode_supported_p (mode
, count
);
73 array_mode
= mode_for_size (count
* GET_MODE_BITSIZE (mode
),
76 if (array_mode
== BLKmode
)
78 if (dump_enabled_p ())
79 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
80 "no array mode for %s[" HOST_WIDE_INT_PRINT_DEC
"]\n",
81 GET_MODE_NAME (mode
), count
);
85 if (convert_optab_handler (optab
, array_mode
, mode
) == CODE_FOR_nothing
)
87 if (dump_enabled_p ())
88 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
89 "cannot use %s<%s><%s>\n", name
,
90 GET_MODE_NAME (array_mode
), GET_MODE_NAME (mode
));
94 if (dump_enabled_p ())
95 dump_printf_loc (MSG_NOTE
, vect_location
,
96 "can use %s<%s><%s>\n", name
, GET_MODE_NAME (array_mode
),
97 GET_MODE_NAME (mode
));
103 /* Return the smallest scalar part of STMT.
104 This is used to determine the vectype of the stmt. We generally set the
105 vectype according to the type of the result (lhs). For stmts whose
106 result-type is different than the type of the arguments (e.g., demotion,
107 promotion), vectype will be reset appropriately (later). Note that we have
108 to visit the smallest datatype in this function, because that determines the
109 VF. If the smallest datatype in the loop is present only as the rhs of a
110 promotion operation - we'd miss it.
111 Such a case, where a variable of this datatype does not appear in the lhs
112 anywhere in the loop, can only occur if it's an invariant: e.g.:
113 'int_x = (int) short_inv', which we'd expect to have been optimized away by
114 invariant motion. However, we cannot rely on invariant motion to always
115 take invariants out of the loop, and so in the case of promotion we also
116 have to check the rhs.
117 LHS_SIZE_UNIT and RHS_SIZE_UNIT contain the sizes of the corresponding
121 vect_get_smallest_scalar_type (gimple stmt
, HOST_WIDE_INT
*lhs_size_unit
,
122 HOST_WIDE_INT
*rhs_size_unit
)
124 tree scalar_type
= gimple_expr_type (stmt
);
125 HOST_WIDE_INT lhs
, rhs
;
127 lhs
= rhs
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (scalar_type
));
129 if (is_gimple_assign (stmt
)
130 && (gimple_assign_cast_p (stmt
)
131 || gimple_assign_rhs_code (stmt
) == WIDEN_MULT_EXPR
132 || gimple_assign_rhs_code (stmt
) == WIDEN_LSHIFT_EXPR
133 || gimple_assign_rhs_code (stmt
) == FLOAT_EXPR
))
135 tree rhs_type
= TREE_TYPE (gimple_assign_rhs1 (stmt
));
137 rhs
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (rhs_type
));
139 scalar_type
= rhs_type
;
142 *lhs_size_unit
= lhs
;
143 *rhs_size_unit
= rhs
;
148 /* Insert DDR into LOOP_VINFO list of ddrs that may alias and need to be
149 tested at run-time. Return TRUE if DDR was successfully inserted.
150 Return false if versioning is not supported. */
153 vect_mark_for_runtime_alias_test (ddr_p ddr
, loop_vec_info loop_vinfo
)
155 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
157 if ((unsigned) PARAM_VALUE (PARAM_VECT_MAX_VERSION_FOR_ALIAS_CHECKS
) == 0)
160 if (dump_enabled_p ())
162 dump_printf_loc (MSG_NOTE
, vect_location
,
163 "mark for run-time aliasing test between ");
164 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, DR_REF (DDR_A (ddr
)));
165 dump_printf (MSG_NOTE
, " and ");
166 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, DR_REF (DDR_B (ddr
)));
167 dump_printf (MSG_NOTE
, "\n");
170 if (optimize_loop_nest_for_size_p (loop
))
172 if (dump_enabled_p ())
173 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
174 "versioning not supported when optimizing"
179 /* FORNOW: We don't support versioning with outer-loop vectorization. */
182 if (dump_enabled_p ())
183 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
184 "versioning not yet supported for outer-loops.\n");
188 /* FORNOW: We don't support creating runtime alias tests for non-constant
190 if (TREE_CODE (DR_STEP (DDR_A (ddr
))) != INTEGER_CST
191 || TREE_CODE (DR_STEP (DDR_B (ddr
))) != INTEGER_CST
)
193 if (dump_enabled_p ())
194 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
195 "versioning not yet supported for non-constant "
200 LOOP_VINFO_MAY_ALIAS_DDRS (loop_vinfo
).safe_push (ddr
);
205 /* Function vect_analyze_data_ref_dependence.
207 Return TRUE if there (might) exist a dependence between a memory-reference
208 DRA and a memory-reference DRB. When versioning for alias may check a
209 dependence at run-time, return FALSE. Adjust *MAX_VF according to
210 the data dependence. */
213 vect_analyze_data_ref_dependence (struct data_dependence_relation
*ddr
,
214 loop_vec_info loop_vinfo
, int *max_vf
)
217 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
218 struct data_reference
*dra
= DDR_A (ddr
);
219 struct data_reference
*drb
= DDR_B (ddr
);
220 stmt_vec_info stmtinfo_a
= vinfo_for_stmt (DR_STMT (dra
));
221 stmt_vec_info stmtinfo_b
= vinfo_for_stmt (DR_STMT (drb
));
222 lambda_vector dist_v
;
223 unsigned int loop_depth
;
225 /* In loop analysis all data references should be vectorizable. */
226 if (!STMT_VINFO_VECTORIZABLE (stmtinfo_a
)
227 || !STMT_VINFO_VECTORIZABLE (stmtinfo_b
))
230 /* Independent data accesses. */
231 if (DDR_ARE_DEPENDENT (ddr
) == chrec_known
)
235 || (DR_IS_READ (dra
) && DR_IS_READ (drb
)))
238 /* Even if we have an anti-dependence then, as the vectorized loop covers at
239 least two scalar iterations, there is always also a true dependence.
240 As the vectorizer does not re-order loads and stores we can ignore
241 the anti-dependence if TBAA can disambiguate both DRs similar to the
242 case with known negative distance anti-dependences (positive
243 distance anti-dependences would violate TBAA constraints). */
244 if (((DR_IS_READ (dra
) && DR_IS_WRITE (drb
))
245 || (DR_IS_WRITE (dra
) && DR_IS_READ (drb
)))
246 && !alias_sets_conflict_p (get_alias_set (DR_REF (dra
)),
247 get_alias_set (DR_REF (drb
))))
250 /* Unknown data dependence. */
251 if (DDR_ARE_DEPENDENT (ddr
) == chrec_dont_know
)
253 /* If user asserted safelen consecutive iterations can be
254 executed concurrently, assume independence. */
255 if (loop
->safelen
>= 2)
257 if (loop
->safelen
< *max_vf
)
258 *max_vf
= loop
->safelen
;
259 LOOP_VINFO_NO_DATA_DEPENDENCIES (loop_vinfo
) = false;
263 if (STMT_VINFO_GATHER_P (stmtinfo_a
)
264 || STMT_VINFO_GATHER_P (stmtinfo_b
))
266 if (dump_enabled_p ())
268 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
269 "versioning for alias not supported for: "
270 "can't determine dependence between ");
271 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
,
273 dump_printf (MSG_MISSED_OPTIMIZATION
, " and ");
274 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
,
276 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
281 if (dump_enabled_p ())
283 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
284 "versioning for alias required: "
285 "can't determine dependence between ");
286 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
,
288 dump_printf (MSG_MISSED_OPTIMIZATION
, " and ");
289 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
,
291 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
294 /* Add to list of ddrs that need to be tested at run-time. */
295 return !vect_mark_for_runtime_alias_test (ddr
, loop_vinfo
);
298 /* Known data dependence. */
299 if (DDR_NUM_DIST_VECTS (ddr
) == 0)
301 /* If user asserted safelen consecutive iterations can be
302 executed concurrently, assume independence. */
303 if (loop
->safelen
>= 2)
305 if (loop
->safelen
< *max_vf
)
306 *max_vf
= loop
->safelen
;
307 LOOP_VINFO_NO_DATA_DEPENDENCIES (loop_vinfo
) = false;
311 if (STMT_VINFO_GATHER_P (stmtinfo_a
)
312 || STMT_VINFO_GATHER_P (stmtinfo_b
))
314 if (dump_enabled_p ())
316 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
317 "versioning for alias not supported for: "
318 "bad dist vector for ");
319 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
,
321 dump_printf (MSG_MISSED_OPTIMIZATION
, " and ");
322 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
,
324 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
329 if (dump_enabled_p ())
331 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
332 "versioning for alias required: "
333 "bad dist vector for ");
334 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
, DR_REF (dra
));
335 dump_printf (MSG_MISSED_OPTIMIZATION
, " and ");
336 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
, DR_REF (drb
));
337 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
339 /* Add to list of ddrs that need to be tested at run-time. */
340 return !vect_mark_for_runtime_alias_test (ddr
, loop_vinfo
);
343 loop_depth
= index_in_loop_nest (loop
->num
, DDR_LOOP_NEST (ddr
));
344 FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr
), i
, dist_v
)
346 int dist
= dist_v
[loop_depth
];
348 if (dump_enabled_p ())
349 dump_printf_loc (MSG_NOTE
, vect_location
,
350 "dependence distance = %d.\n", dist
);
354 if (dump_enabled_p ())
356 dump_printf_loc (MSG_NOTE
, vect_location
,
357 "dependence distance == 0 between ");
358 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, DR_REF (dra
));
359 dump_printf (MSG_NOTE
, " and ");
360 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, DR_REF (drb
));
361 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
364 /* When we perform grouped accesses and perform implicit CSE
365 by detecting equal accesses and doing disambiguation with
366 runtime alias tests like for
374 where we will end up loading { a[i], a[i+1] } once, make
375 sure that inserting group loads before the first load and
376 stores after the last store will do the right thing.
377 Similar for groups like
381 where loads from the group interleave with the store. */
382 if (STMT_VINFO_GROUPED_ACCESS (stmtinfo_a
)
383 || STMT_VINFO_GROUPED_ACCESS (stmtinfo_b
))
386 earlier_stmt
= get_earlier_stmt (DR_STMT (dra
), DR_STMT (drb
));
388 (STMT_VINFO_DATA_REF (vinfo_for_stmt (earlier_stmt
))))
390 if (dump_enabled_p ())
391 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
392 "READ_WRITE dependence in interleaving."
401 if (dist
> 0 && DDR_REVERSED_P (ddr
))
403 /* If DDR_REVERSED_P the order of the data-refs in DDR was
404 reversed (to make distance vector positive), and the actual
405 distance is negative. */
406 if (dump_enabled_p ())
407 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
408 "dependence distance negative.\n");
409 /* Record a negative dependence distance to later limit the
410 amount of stmt copying / unrolling we can perform.
411 Only need to handle read-after-write dependence. */
413 && (STMT_VINFO_MIN_NEG_DIST (stmtinfo_b
) == 0
414 || STMT_VINFO_MIN_NEG_DIST (stmtinfo_b
) > (unsigned)dist
))
415 STMT_VINFO_MIN_NEG_DIST (stmtinfo_b
) = dist
;
420 && abs (dist
) < *max_vf
)
422 /* The dependence distance requires reduction of the maximal
423 vectorization factor. */
424 *max_vf
= abs (dist
);
425 if (dump_enabled_p ())
426 dump_printf_loc (MSG_NOTE
, vect_location
,
427 "adjusting maximal vectorization factor to %i\n",
431 if (abs (dist
) >= *max_vf
)
433 /* Dependence distance does not create dependence, as far as
434 vectorization is concerned, in this case. */
435 if (dump_enabled_p ())
436 dump_printf_loc (MSG_NOTE
, vect_location
,
437 "dependence distance >= VF.\n");
441 if (dump_enabled_p ())
443 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
444 "not vectorized, possible dependence "
445 "between data-refs ");
446 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, DR_REF (dra
));
447 dump_printf (MSG_NOTE
, " and ");
448 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, DR_REF (drb
));
449 dump_printf (MSG_NOTE
, "\n");
458 /* Function vect_analyze_data_ref_dependences.
460 Examine all the data references in the loop, and make sure there do not
461 exist any data dependences between them. Set *MAX_VF according to
462 the maximum vectorization factor the data dependences allow. */
465 vect_analyze_data_ref_dependences (loop_vec_info loop_vinfo
, int *max_vf
)
468 struct data_dependence_relation
*ddr
;
470 if (dump_enabled_p ())
471 dump_printf_loc (MSG_NOTE
, vect_location
,
472 "=== vect_analyze_data_ref_dependences ===\n");
474 LOOP_VINFO_NO_DATA_DEPENDENCIES (loop_vinfo
) = true;
475 if (!compute_all_dependences (LOOP_VINFO_DATAREFS (loop_vinfo
),
476 &LOOP_VINFO_DDRS (loop_vinfo
),
477 LOOP_VINFO_LOOP_NEST (loop_vinfo
), true))
480 FOR_EACH_VEC_ELT (LOOP_VINFO_DDRS (loop_vinfo
), i
, ddr
)
481 if (vect_analyze_data_ref_dependence (ddr
, loop_vinfo
, max_vf
))
488 /* Function vect_slp_analyze_data_ref_dependence.
490 Return TRUE if there (might) exist a dependence between a memory-reference
491 DRA and a memory-reference DRB. When versioning for alias may check a
492 dependence at run-time, return FALSE. Adjust *MAX_VF according to
493 the data dependence. */
496 vect_slp_analyze_data_ref_dependence (struct data_dependence_relation
*ddr
)
498 struct data_reference
*dra
= DDR_A (ddr
);
499 struct data_reference
*drb
= DDR_B (ddr
);
501 /* We need to check dependences of statements marked as unvectorizable
502 as well, they still can prohibit vectorization. */
504 /* Independent data accesses. */
505 if (DDR_ARE_DEPENDENT (ddr
) == chrec_known
)
511 /* Read-read is OK. */
512 if (DR_IS_READ (dra
) && DR_IS_READ (drb
))
515 /* If dra and drb are part of the same interleaving chain consider
517 if (STMT_VINFO_GROUPED_ACCESS (vinfo_for_stmt (DR_STMT (dra
)))
518 && (GROUP_FIRST_ELEMENT (vinfo_for_stmt (DR_STMT (dra
)))
519 == GROUP_FIRST_ELEMENT (vinfo_for_stmt (DR_STMT (drb
)))))
522 /* Unknown data dependence. */
523 if (DDR_ARE_DEPENDENT (ddr
) == chrec_dont_know
)
525 if (dump_enabled_p ())
527 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
528 "can't determine dependence between ");
529 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
, DR_REF (dra
));
530 dump_printf (MSG_MISSED_OPTIMIZATION
, " and ");
531 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
, DR_REF (drb
));
532 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
535 else if (dump_enabled_p ())
537 dump_printf_loc (MSG_NOTE
, vect_location
,
538 "determined dependence between ");
539 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, DR_REF (dra
));
540 dump_printf (MSG_NOTE
, " and ");
541 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, DR_REF (drb
));
542 dump_printf (MSG_NOTE
, "\n");
545 /* We do not vectorize basic blocks with write-write dependencies. */
546 if (DR_IS_WRITE (dra
) && DR_IS_WRITE (drb
))
549 /* If we have a read-write dependence check that the load is before the store.
550 When we vectorize basic blocks, vector load can be only before
551 corresponding scalar load, and vector store can be only after its
552 corresponding scalar store. So the order of the acceses is preserved in
553 case the load is before the store. */
554 gimple earlier_stmt
= get_earlier_stmt (DR_STMT (dra
), DR_STMT (drb
));
555 if (DR_IS_READ (STMT_VINFO_DATA_REF (vinfo_for_stmt (earlier_stmt
))))
557 /* That only holds for load-store pairs taking part in vectorization. */
558 if (STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dra
)))
559 && STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (drb
))))
567 /* Function vect_analyze_data_ref_dependences.
569 Examine all the data references in the basic-block, and make sure there
570 do not exist any data dependences between them. Set *MAX_VF according to
571 the maximum vectorization factor the data dependences allow. */
574 vect_slp_analyze_data_ref_dependences (bb_vec_info bb_vinfo
)
576 struct data_dependence_relation
*ddr
;
579 if (dump_enabled_p ())
580 dump_printf_loc (MSG_NOTE
, vect_location
,
581 "=== vect_slp_analyze_data_ref_dependences ===\n");
583 if (!compute_all_dependences (BB_VINFO_DATAREFS (bb_vinfo
),
584 &BB_VINFO_DDRS (bb_vinfo
),
588 FOR_EACH_VEC_ELT (BB_VINFO_DDRS (bb_vinfo
), i
, ddr
)
589 if (vect_slp_analyze_data_ref_dependence (ddr
))
596 /* Function vect_compute_data_ref_alignment
598 Compute the misalignment of the data reference DR.
601 1. If during the misalignment computation it is found that the data reference
602 cannot be vectorized then false is returned.
603 2. DR_MISALIGNMENT (DR) is defined.
605 FOR NOW: No analysis is actually performed. Misalignment is calculated
606 only for trivial cases. TODO. */
609 vect_compute_data_ref_alignment (struct data_reference
*dr
)
611 gimple stmt
= DR_STMT (dr
);
612 stmt_vec_info stmt_info
= vinfo_for_stmt (stmt
);
613 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_info
);
614 struct loop
*loop
= NULL
;
615 tree ref
= DR_REF (dr
);
617 tree base
, base_addr
;
620 tree aligned_to
, alignment
;
622 if (dump_enabled_p ())
623 dump_printf_loc (MSG_NOTE
, vect_location
,
624 "vect_compute_data_ref_alignment:\n");
627 loop
= LOOP_VINFO_LOOP (loop_vinfo
);
629 /* Initialize misalignment to unknown. */
630 SET_DR_MISALIGNMENT (dr
, -1);
632 /* Strided loads perform only component accesses, misalignment information
633 is irrelevant for them. */
634 if (STMT_VINFO_STRIDE_LOAD_P (stmt_info
))
637 misalign
= DR_INIT (dr
);
638 aligned_to
= DR_ALIGNED_TO (dr
);
639 base_addr
= DR_BASE_ADDRESS (dr
);
640 vectype
= STMT_VINFO_VECTYPE (stmt_info
);
642 /* In case the dataref is in an inner-loop of the loop that is being
643 vectorized (LOOP), we use the base and misalignment information
644 relative to the outer-loop (LOOP). This is ok only if the misalignment
645 stays the same throughout the execution of the inner-loop, which is why
646 we have to check that the stride of the dataref in the inner-loop evenly
647 divides by the vector size. */
648 if (loop
&& nested_in_vect_loop_p (loop
, stmt
))
650 tree step
= DR_STEP (dr
);
651 HOST_WIDE_INT dr_step
= TREE_INT_CST_LOW (step
);
653 if (dr_step
% GET_MODE_SIZE (TYPE_MODE (vectype
)) == 0)
655 if (dump_enabled_p ())
656 dump_printf_loc (MSG_NOTE
, vect_location
,
657 "inner step divides the vector-size.\n");
658 misalign
= STMT_VINFO_DR_INIT (stmt_info
);
659 aligned_to
= STMT_VINFO_DR_ALIGNED_TO (stmt_info
);
660 base_addr
= STMT_VINFO_DR_BASE_ADDRESS (stmt_info
);
664 if (dump_enabled_p ())
665 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
666 "inner step doesn't divide the vector-size.\n");
667 misalign
= NULL_TREE
;
671 /* Similarly, if we're doing basic-block vectorization, we can only use
672 base and misalignment information relative to an innermost loop if the
673 misalignment stays the same throughout the execution of the loop.
674 As above, this is the case if the stride of the dataref evenly divides
675 by the vector size. */
678 tree step
= DR_STEP (dr
);
679 HOST_WIDE_INT dr_step
= TREE_INT_CST_LOW (step
);
681 if (dr_step
% GET_MODE_SIZE (TYPE_MODE (vectype
)) != 0)
683 if (dump_enabled_p ())
684 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
685 "SLP: step doesn't divide the vector-size.\n");
686 misalign
= NULL_TREE
;
690 base
= build_fold_indirect_ref (base_addr
);
691 alignment
= ssize_int (TYPE_ALIGN (vectype
)/BITS_PER_UNIT
);
693 if ((aligned_to
&& tree_int_cst_compare (aligned_to
, alignment
) < 0)
696 if (dump_enabled_p ())
698 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
699 "Unknown alignment for access: ");
700 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
, base
);
701 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
707 && tree_int_cst_compare (ssize_int (DECL_ALIGN_UNIT (base
)),
709 || (TREE_CODE (base_addr
) == SSA_NAME
710 && tree_int_cst_compare (ssize_int (TYPE_ALIGN_UNIT (TREE_TYPE (
711 TREE_TYPE (base_addr
)))),
713 || (get_pointer_alignment (base_addr
) >= TYPE_ALIGN (vectype
)))
716 base_aligned
= false;
720 /* Do not change the alignment of global variables here if
721 flag_section_anchors is enabled as we already generated
722 RTL for other functions. Most global variables should
723 have been aligned during the IPA increase_alignment pass. */
724 if (!vect_can_force_dr_alignment_p (base
, TYPE_ALIGN (vectype
))
725 || (TREE_STATIC (base
) && flag_section_anchors
))
727 if (dump_enabled_p ())
729 dump_printf_loc (MSG_NOTE
, vect_location
,
730 "can't force alignment of ref: ");
731 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, ref
);
732 dump_printf (MSG_NOTE
, "\n");
737 /* Force the alignment of the decl.
738 NOTE: This is the only change to the code we make during
739 the analysis phase, before deciding to vectorize the loop. */
740 if (dump_enabled_p ())
742 dump_printf_loc (MSG_NOTE
, vect_location
, "force alignment of ");
743 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, ref
);
744 dump_printf (MSG_NOTE
, "\n");
747 ((dataref_aux
*)dr
->aux
)->base_decl
= base
;
748 ((dataref_aux
*)dr
->aux
)->base_misaligned
= true;
751 /* If this is a backward running DR then first access in the larger
752 vectype actually is N-1 elements before the address in the DR.
753 Adjust misalign accordingly. */
754 if (tree_int_cst_compare (DR_STEP (dr
), size_zero_node
) < 0)
756 tree offset
= ssize_int (TYPE_VECTOR_SUBPARTS (vectype
) - 1);
757 /* DR_STEP(dr) is the same as -TYPE_SIZE of the scalar type,
758 otherwise we wouldn't be here. */
759 offset
= fold_build2 (MULT_EXPR
, ssizetype
, offset
, DR_STEP (dr
));
760 /* PLUS because DR_STEP was negative. */
761 misalign
= size_binop (PLUS_EXPR
, misalign
, offset
);
764 /* Modulo alignment. */
765 misalign
= size_binop (FLOOR_MOD_EXPR
, misalign
, alignment
);
767 if (!tree_fits_uhwi_p (misalign
))
769 /* Negative or overflowed misalignment value. */
770 if (dump_enabled_p ())
771 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
772 "unexpected misalign value\n");
776 SET_DR_MISALIGNMENT (dr
, tree_to_uhwi (misalign
));
778 if (dump_enabled_p ())
780 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
781 "misalign = %d bytes of ref ", DR_MISALIGNMENT (dr
));
782 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
, ref
);
783 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
790 /* Function vect_compute_data_refs_alignment
792 Compute the misalignment of data references in the loop.
793 Return FALSE if a data reference is found that cannot be vectorized. */
796 vect_compute_data_refs_alignment (loop_vec_info loop_vinfo
,
797 bb_vec_info bb_vinfo
)
799 vec
<data_reference_p
> datarefs
;
800 struct data_reference
*dr
;
804 datarefs
= LOOP_VINFO_DATAREFS (loop_vinfo
);
806 datarefs
= BB_VINFO_DATAREFS (bb_vinfo
);
808 FOR_EACH_VEC_ELT (datarefs
, i
, dr
)
809 if (STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dr
)))
810 && !vect_compute_data_ref_alignment (dr
))
814 /* Mark unsupported statement as unvectorizable. */
815 STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dr
))) = false;
826 /* Function vect_update_misalignment_for_peel
828 DR - the data reference whose misalignment is to be adjusted.
829 DR_PEEL - the data reference whose misalignment is being made
830 zero in the vector loop by the peel.
831 NPEEL - the number of iterations in the peel loop if the misalignment
832 of DR_PEEL is known at compile time. */
835 vect_update_misalignment_for_peel (struct data_reference
*dr
,
836 struct data_reference
*dr_peel
, int npeel
)
839 vec
<dr_p
> same_align_drs
;
840 struct data_reference
*current_dr
;
841 int dr_size
= GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (DR_REF (dr
))));
842 int dr_peel_size
= GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (DR_REF (dr_peel
))));
843 stmt_vec_info stmt_info
= vinfo_for_stmt (DR_STMT (dr
));
844 stmt_vec_info peel_stmt_info
= vinfo_for_stmt (DR_STMT (dr_peel
));
846 /* For interleaved data accesses the step in the loop must be multiplied by
847 the size of the interleaving group. */
848 if (STMT_VINFO_GROUPED_ACCESS (stmt_info
))
849 dr_size
*= GROUP_SIZE (vinfo_for_stmt (GROUP_FIRST_ELEMENT (stmt_info
)));
850 if (STMT_VINFO_GROUPED_ACCESS (peel_stmt_info
))
851 dr_peel_size
*= GROUP_SIZE (peel_stmt_info
);
853 /* It can be assumed that the data refs with the same alignment as dr_peel
854 are aligned in the vector loop. */
856 = STMT_VINFO_SAME_ALIGN_REFS (vinfo_for_stmt (DR_STMT (dr_peel
)));
857 FOR_EACH_VEC_ELT (same_align_drs
, i
, current_dr
)
859 if (current_dr
!= dr
)
861 gcc_assert (DR_MISALIGNMENT (dr
) / dr_size
==
862 DR_MISALIGNMENT (dr_peel
) / dr_peel_size
);
863 SET_DR_MISALIGNMENT (dr
, 0);
867 if (known_alignment_for_access_p (dr
)
868 && known_alignment_for_access_p (dr_peel
))
870 bool negative
= tree_int_cst_compare (DR_STEP (dr
), size_zero_node
) < 0;
871 int misal
= DR_MISALIGNMENT (dr
);
872 tree vectype
= STMT_VINFO_VECTYPE (stmt_info
);
873 misal
+= negative
? -npeel
* dr_size
: npeel
* dr_size
;
874 misal
&= (TYPE_ALIGN (vectype
) / BITS_PER_UNIT
) - 1;
875 SET_DR_MISALIGNMENT (dr
, misal
);
879 if (dump_enabled_p ())
880 dump_printf_loc (MSG_NOTE
, vect_location
, "Setting misalignment to -1.\n");
881 SET_DR_MISALIGNMENT (dr
, -1);
885 /* Function vect_verify_datarefs_alignment
887 Return TRUE if all data references in the loop can be
888 handled with respect to alignment. */
891 vect_verify_datarefs_alignment (loop_vec_info loop_vinfo
, bb_vec_info bb_vinfo
)
893 vec
<data_reference_p
> datarefs
;
894 struct data_reference
*dr
;
895 enum dr_alignment_support supportable_dr_alignment
;
899 datarefs
= LOOP_VINFO_DATAREFS (loop_vinfo
);
901 datarefs
= BB_VINFO_DATAREFS (bb_vinfo
);
903 FOR_EACH_VEC_ELT (datarefs
, i
, dr
)
905 gimple stmt
= DR_STMT (dr
);
906 stmt_vec_info stmt_info
= vinfo_for_stmt (stmt
);
908 if (!STMT_VINFO_RELEVANT_P (stmt_info
))
911 /* For interleaving, only the alignment of the first access matters.
912 Skip statements marked as not vectorizable. */
913 if ((STMT_VINFO_GROUPED_ACCESS (stmt_info
)
914 && GROUP_FIRST_ELEMENT (stmt_info
) != stmt
)
915 || !STMT_VINFO_VECTORIZABLE (stmt_info
))
918 /* Strided loads perform only component accesses, alignment is
919 irrelevant for them. */
920 if (STMT_VINFO_STRIDE_LOAD_P (stmt_info
))
923 supportable_dr_alignment
= vect_supportable_dr_alignment (dr
, false);
924 if (!supportable_dr_alignment
)
926 if (dump_enabled_p ())
929 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
930 "not vectorized: unsupported unaligned load.");
932 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
933 "not vectorized: unsupported unaligned "
936 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
,
938 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
942 if (supportable_dr_alignment
!= dr_aligned
&& dump_enabled_p ())
943 dump_printf_loc (MSG_NOTE
, vect_location
,
944 "Vectorizing an unaligned access.\n");
949 /* Given an memory reference EXP return whether its alignment is less
953 not_size_aligned (tree exp
)
955 if (!tree_fits_uhwi_p (TYPE_SIZE (TREE_TYPE (exp
))))
958 return (tree_to_uhwi (TYPE_SIZE (TREE_TYPE (exp
)))
959 > get_object_alignment (exp
));
962 /* Function vector_alignment_reachable_p
964 Return true if vector alignment for DR is reachable by peeling
965 a few loop iterations. Return false otherwise. */
968 vector_alignment_reachable_p (struct data_reference
*dr
)
970 gimple stmt
= DR_STMT (dr
);
971 stmt_vec_info stmt_info
= vinfo_for_stmt (stmt
);
972 tree vectype
= STMT_VINFO_VECTYPE (stmt_info
);
974 if (STMT_VINFO_GROUPED_ACCESS (stmt_info
))
976 /* For interleaved access we peel only if number of iterations in
977 the prolog loop ({VF - misalignment}), is a multiple of the
978 number of the interleaved accesses. */
979 int elem_size
, mis_in_elements
;
980 int nelements
= TYPE_VECTOR_SUBPARTS (vectype
);
982 /* FORNOW: handle only known alignment. */
983 if (!known_alignment_for_access_p (dr
))
986 elem_size
= GET_MODE_SIZE (TYPE_MODE (vectype
)) / nelements
;
987 mis_in_elements
= DR_MISALIGNMENT (dr
) / elem_size
;
989 if ((nelements
- mis_in_elements
) % GROUP_SIZE (stmt_info
))
993 /* If misalignment is known at the compile time then allow peeling
994 only if natural alignment is reachable through peeling. */
995 if (known_alignment_for_access_p (dr
) && !aligned_access_p (dr
))
997 HOST_WIDE_INT elmsize
=
998 int_cst_value (TYPE_SIZE_UNIT (TREE_TYPE (vectype
)));
999 if (dump_enabled_p ())
1001 dump_printf_loc (MSG_NOTE
, vect_location
,
1002 "data size =" HOST_WIDE_INT_PRINT_DEC
, elmsize
);
1003 dump_printf (MSG_NOTE
,
1004 ". misalignment = %d.\n", DR_MISALIGNMENT (dr
));
1006 if (DR_MISALIGNMENT (dr
) % elmsize
)
1008 if (dump_enabled_p ())
1009 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1010 "data size does not divide the misalignment.\n");
1015 if (!known_alignment_for_access_p (dr
))
1017 tree type
= TREE_TYPE (DR_REF (dr
));
1018 bool is_packed
= not_size_aligned (DR_REF (dr
));
1019 if (dump_enabled_p ())
1020 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1021 "Unknown misalignment, is_packed = %d\n",is_packed
);
1022 if ((TYPE_USER_ALIGN (type
) && !is_packed
)
1023 || targetm
.vectorize
.vector_alignment_reachable (type
, is_packed
))
1033 /* Calculate the cost of the memory access represented by DR. */
1036 vect_get_data_access_cost (struct data_reference
*dr
,
1037 unsigned int *inside_cost
,
1038 unsigned int *outside_cost
,
1039 stmt_vector_for_cost
*body_cost_vec
)
1041 gimple stmt
= DR_STMT (dr
);
1042 stmt_vec_info stmt_info
= vinfo_for_stmt (stmt
);
1043 int nunits
= TYPE_VECTOR_SUBPARTS (STMT_VINFO_VECTYPE (stmt_info
));
1044 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_info
);
1045 int vf
= LOOP_VINFO_VECT_FACTOR (loop_vinfo
);
1046 int ncopies
= vf
/ nunits
;
1048 if (DR_IS_READ (dr
))
1049 vect_get_load_cost (dr
, ncopies
, true, inside_cost
, outside_cost
,
1050 NULL
, body_cost_vec
, false);
1052 vect_get_store_cost (dr
, ncopies
, inside_cost
, body_cost_vec
);
1054 if (dump_enabled_p ())
1055 dump_printf_loc (MSG_NOTE
, vect_location
,
1056 "vect_get_data_access_cost: inside_cost = %d, "
1057 "outside_cost = %d.\n", *inside_cost
, *outside_cost
);
1061 /* Insert DR into peeling hash table with NPEEL as key. */
1064 vect_peeling_hash_insert (loop_vec_info loop_vinfo
, struct data_reference
*dr
,
1067 struct _vect_peel_info elem
, *slot
;
1068 _vect_peel_info
**new_slot
;
1069 bool supportable_dr_alignment
= vect_supportable_dr_alignment (dr
, true);
1072 slot
= LOOP_VINFO_PEELING_HTAB (loop_vinfo
).find (&elem
);
1077 slot
= XNEW (struct _vect_peel_info
);
1078 slot
->npeel
= npeel
;
1081 new_slot
= LOOP_VINFO_PEELING_HTAB (loop_vinfo
).find_slot (slot
, INSERT
);
1085 if (!supportable_dr_alignment
1086 && unlimited_cost_model (LOOP_VINFO_LOOP (loop_vinfo
)))
1087 slot
->count
+= VECT_MAX_COST
;
1091 /* Traverse peeling hash table to find peeling option that aligns maximum
1092 number of data accesses. */
1095 vect_peeling_hash_get_most_frequent (_vect_peel_info
**slot
,
1096 _vect_peel_extended_info
*max
)
1098 vect_peel_info elem
= *slot
;
1100 if (elem
->count
> max
->peel_info
.count
1101 || (elem
->count
== max
->peel_info
.count
1102 && max
->peel_info
.npeel
> elem
->npeel
))
1104 max
->peel_info
.npeel
= elem
->npeel
;
1105 max
->peel_info
.count
= elem
->count
;
1106 max
->peel_info
.dr
= elem
->dr
;
1113 /* Traverse peeling hash table and calculate cost for each peeling option.
1114 Find the one with the lowest cost. */
1117 vect_peeling_hash_get_lowest_cost (_vect_peel_info
**slot
,
1118 _vect_peel_extended_info
*min
)
1120 vect_peel_info elem
= *slot
;
1121 int save_misalignment
, dummy
;
1122 unsigned int inside_cost
= 0, outside_cost
= 0, i
;
1123 gimple stmt
= DR_STMT (elem
->dr
);
1124 stmt_vec_info stmt_info
= vinfo_for_stmt (stmt
);
1125 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_info
);
1126 vec
<data_reference_p
> datarefs
= LOOP_VINFO_DATAREFS (loop_vinfo
);
1127 struct data_reference
*dr
;
1128 stmt_vector_for_cost prologue_cost_vec
, body_cost_vec
, epilogue_cost_vec
;
1130 prologue_cost_vec
.create (2);
1131 body_cost_vec
.create (2);
1132 epilogue_cost_vec
.create (2);
1134 FOR_EACH_VEC_ELT (datarefs
, i
, dr
)
1136 stmt
= DR_STMT (dr
);
1137 stmt_info
= vinfo_for_stmt (stmt
);
1138 /* For interleaving, only the alignment of the first access
1140 if (STMT_VINFO_GROUPED_ACCESS (stmt_info
)
1141 && GROUP_FIRST_ELEMENT (stmt_info
) != stmt
)
1144 save_misalignment
= DR_MISALIGNMENT (dr
);
1145 vect_update_misalignment_for_peel (dr
, elem
->dr
, elem
->npeel
);
1146 vect_get_data_access_cost (dr
, &inside_cost
, &outside_cost
,
1148 SET_DR_MISALIGNMENT (dr
, save_misalignment
);
1151 auto_vec
<stmt_info_for_cost
> scalar_cost_vec
;
1152 vect_get_single_scalar_iteration_cost (loop_vinfo
, &scalar_cost_vec
);
1153 outside_cost
+= vect_get_known_peeling_cost
1154 (loop_vinfo
, elem
->npeel
, &dummy
,
1155 &scalar_cost_vec
, &prologue_cost_vec
, &epilogue_cost_vec
);
1157 /* Prologue and epilogue costs are added to the target model later.
1158 These costs depend only on the scalar iteration cost, the
1159 number of peeling iterations finally chosen, and the number of
1160 misaligned statements. So discard the information found here. */
1161 prologue_cost_vec
.release ();
1162 epilogue_cost_vec
.release ();
1164 if (inside_cost
< min
->inside_cost
1165 || (inside_cost
== min
->inside_cost
&& outside_cost
< min
->outside_cost
))
1167 min
->inside_cost
= inside_cost
;
1168 min
->outside_cost
= outside_cost
;
1169 min
->body_cost_vec
.release ();
1170 min
->body_cost_vec
= body_cost_vec
;
1171 min
->peel_info
.dr
= elem
->dr
;
1172 min
->peel_info
.npeel
= elem
->npeel
;
1175 body_cost_vec
.release ();
1181 /* Choose best peeling option by traversing peeling hash table and either
1182 choosing an option with the lowest cost (if cost model is enabled) or the
1183 option that aligns as many accesses as possible. */
1185 static struct data_reference
*
1186 vect_peeling_hash_choose_best_peeling (loop_vec_info loop_vinfo
,
1187 unsigned int *npeel
,
1188 stmt_vector_for_cost
*body_cost_vec
)
1190 struct _vect_peel_extended_info res
;
1192 res
.peel_info
.dr
= NULL
;
1193 res
.body_cost_vec
= stmt_vector_for_cost ();
1195 if (!unlimited_cost_model (LOOP_VINFO_LOOP (loop_vinfo
)))
1197 res
.inside_cost
= INT_MAX
;
1198 res
.outside_cost
= INT_MAX
;
1199 LOOP_VINFO_PEELING_HTAB (loop_vinfo
)
1200 .traverse
<_vect_peel_extended_info
*,
1201 vect_peeling_hash_get_lowest_cost
> (&res
);
1205 res
.peel_info
.count
= 0;
1206 LOOP_VINFO_PEELING_HTAB (loop_vinfo
)
1207 .traverse
<_vect_peel_extended_info
*,
1208 vect_peeling_hash_get_most_frequent
> (&res
);
1211 *npeel
= res
.peel_info
.npeel
;
1212 *body_cost_vec
= res
.body_cost_vec
;
1213 return res
.peel_info
.dr
;
1217 /* Function vect_enhance_data_refs_alignment
1219 This pass will use loop versioning and loop peeling in order to enhance
1220 the alignment of data references in the loop.
1222 FOR NOW: we assume that whatever versioning/peeling takes place, only the
1223 original loop is to be vectorized. Any other loops that are created by
1224 the transformations performed in this pass - are not supposed to be
1225 vectorized. This restriction will be relaxed.
1227 This pass will require a cost model to guide it whether to apply peeling
1228 or versioning or a combination of the two. For example, the scheme that
1229 intel uses when given a loop with several memory accesses, is as follows:
1230 choose one memory access ('p') which alignment you want to force by doing
1231 peeling. Then, either (1) generate a loop in which 'p' is aligned and all
1232 other accesses are not necessarily aligned, or (2) use loop versioning to
1233 generate one loop in which all accesses are aligned, and another loop in
1234 which only 'p' is necessarily aligned.
1236 ("Automatic Intra-Register Vectorization for the Intel Architecture",
1237 Aart J.C. Bik, Milind Girkar, Paul M. Grey and Ximmin Tian, International
1238 Journal of Parallel Programming, Vol. 30, No. 2, April 2002.)
1240 Devising a cost model is the most critical aspect of this work. It will
1241 guide us on which access to peel for, whether to use loop versioning, how
1242 many versions to create, etc. The cost model will probably consist of
1243 generic considerations as well as target specific considerations (on
1244 powerpc for example, misaligned stores are more painful than misaligned
1247 Here are the general steps involved in alignment enhancements:
1249 -- original loop, before alignment analysis:
1250 for (i=0; i<N; i++){
1251 x = q[i]; # DR_MISALIGNMENT(q) = unknown
1252 p[i] = y; # DR_MISALIGNMENT(p) = unknown
1255 -- After vect_compute_data_refs_alignment:
1256 for (i=0; i<N; i++){
1257 x = q[i]; # DR_MISALIGNMENT(q) = 3
1258 p[i] = y; # DR_MISALIGNMENT(p) = unknown
1261 -- Possibility 1: we do loop versioning:
1263 for (i=0; i<N; i++){ # loop 1A
1264 x = q[i]; # DR_MISALIGNMENT(q) = 3
1265 p[i] = y; # DR_MISALIGNMENT(p) = 0
1269 for (i=0; i<N; i++){ # loop 1B
1270 x = q[i]; # DR_MISALIGNMENT(q) = 3
1271 p[i] = y; # DR_MISALIGNMENT(p) = unaligned
1275 -- Possibility 2: we do loop peeling:
1276 for (i = 0; i < 3; i++){ # (scalar loop, not to be vectorized).
1280 for (i = 3; i < N; i++){ # loop 2A
1281 x = q[i]; # DR_MISALIGNMENT(q) = 0
1282 p[i] = y; # DR_MISALIGNMENT(p) = unknown
1285 -- Possibility 3: combination of loop peeling and versioning:
1286 for (i = 0; i < 3; i++){ # (scalar loop, not to be vectorized).
1291 for (i = 3; i<N; i++){ # loop 3A
1292 x = q[i]; # DR_MISALIGNMENT(q) = 0
1293 p[i] = y; # DR_MISALIGNMENT(p) = 0
1297 for (i = 3; i<N; i++){ # loop 3B
1298 x = q[i]; # DR_MISALIGNMENT(q) = 0
1299 p[i] = y; # DR_MISALIGNMENT(p) = unaligned
1303 These loops are later passed to loop_transform to be vectorized. The
1304 vectorizer will use the alignment information to guide the transformation
1305 (whether to generate regular loads/stores, or with special handling for
1309 vect_enhance_data_refs_alignment (loop_vec_info loop_vinfo
)
1311 vec
<data_reference_p
> datarefs
= LOOP_VINFO_DATAREFS (loop_vinfo
);
1312 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
1313 enum dr_alignment_support supportable_dr_alignment
;
1314 struct data_reference
*dr0
= NULL
, *first_store
= NULL
;
1315 struct data_reference
*dr
;
1317 bool do_peeling
= false;
1318 bool do_versioning
= false;
1321 stmt_vec_info stmt_info
;
1322 unsigned int npeel
= 0;
1323 bool all_misalignments_unknown
= true;
1324 unsigned int vf
= LOOP_VINFO_VECT_FACTOR (loop_vinfo
);
1325 unsigned possible_npeel_number
= 1;
1327 unsigned int nelements
, mis
, same_align_drs_max
= 0;
1328 stmt_vector_for_cost body_cost_vec
= stmt_vector_for_cost ();
1330 if (dump_enabled_p ())
1331 dump_printf_loc (MSG_NOTE
, vect_location
,
1332 "=== vect_enhance_data_refs_alignment ===\n");
1334 /* While cost model enhancements are expected in the future, the high level
1335 view of the code at this time is as follows:
1337 A) If there is a misaligned access then see if peeling to align
1338 this access can make all data references satisfy
1339 vect_supportable_dr_alignment. If so, update data structures
1340 as needed and return true.
1342 B) If peeling wasn't possible and there is a data reference with an
1343 unknown misalignment that does not satisfy vect_supportable_dr_alignment
1344 then see if loop versioning checks can be used to make all data
1345 references satisfy vect_supportable_dr_alignment. If so, update
1346 data structures as needed and return true.
1348 C) If neither peeling nor versioning were successful then return false if
1349 any data reference does not satisfy vect_supportable_dr_alignment.
1351 D) Return true (all data references satisfy vect_supportable_dr_alignment).
1353 Note, Possibility 3 above (which is peeling and versioning together) is not
1354 being done at this time. */
1356 /* (1) Peeling to force alignment. */
1358 /* (1.1) Decide whether to perform peeling, and how many iterations to peel:
1360 + How many accesses will become aligned due to the peeling
1361 - How many accesses will become unaligned due to the peeling,
1362 and the cost of misaligned accesses.
1363 - The cost of peeling (the extra runtime checks, the increase
1366 FOR_EACH_VEC_ELT (datarefs
, i
, dr
)
1368 stmt
= DR_STMT (dr
);
1369 stmt_info
= vinfo_for_stmt (stmt
);
1371 if (!STMT_VINFO_RELEVANT_P (stmt_info
))
1374 /* For interleaving, only the alignment of the first access
1376 if (STMT_VINFO_GROUPED_ACCESS (stmt_info
)
1377 && GROUP_FIRST_ELEMENT (stmt_info
) != stmt
)
1380 /* For invariant accesses there is nothing to enhance. */
1381 if (integer_zerop (DR_STEP (dr
)))
1384 /* Strided loads perform only component accesses, alignment is
1385 irrelevant for them. */
1386 if (STMT_VINFO_STRIDE_LOAD_P (stmt_info
))
1389 supportable_dr_alignment
= vect_supportable_dr_alignment (dr
, true);
1390 do_peeling
= vector_alignment_reachable_p (dr
);
1393 if (known_alignment_for_access_p (dr
))
1395 unsigned int npeel_tmp
;
1396 bool negative
= tree_int_cst_compare (DR_STEP (dr
),
1397 size_zero_node
) < 0;
1399 /* Save info about DR in the hash table. */
1400 if (!LOOP_VINFO_PEELING_HTAB (loop_vinfo
).is_created ())
1401 LOOP_VINFO_PEELING_HTAB (loop_vinfo
).create (1);
1403 vectype
= STMT_VINFO_VECTYPE (stmt_info
);
1404 nelements
= TYPE_VECTOR_SUBPARTS (vectype
);
1405 mis
= DR_MISALIGNMENT (dr
) / GET_MODE_SIZE (TYPE_MODE (
1406 TREE_TYPE (DR_REF (dr
))));
1407 npeel_tmp
= (negative
1408 ? (mis
- nelements
) : (nelements
- mis
))
1411 /* For multiple types, it is possible that the bigger type access
1412 will have more than one peeling option. E.g., a loop with two
1413 types: one of size (vector size / 4), and the other one of
1414 size (vector size / 8). Vectorization factor will 8. If both
1415 access are misaligned by 3, the first one needs one scalar
1416 iteration to be aligned, and the second one needs 5. But the
1417 the first one will be aligned also by peeling 5 scalar
1418 iterations, and in that case both accesses will be aligned.
1419 Hence, except for the immediate peeling amount, we also want
1420 to try to add full vector size, while we don't exceed
1421 vectorization factor.
1422 We do this automtically for cost model, since we calculate cost
1423 for every peeling option. */
1424 if (unlimited_cost_model (LOOP_VINFO_LOOP (loop_vinfo
)))
1425 possible_npeel_number
= vf
/nelements
;
1427 /* Handle the aligned case. We may decide to align some other
1428 access, making DR unaligned. */
1429 if (DR_MISALIGNMENT (dr
) == 0)
1432 if (unlimited_cost_model (LOOP_VINFO_LOOP (loop_vinfo
)))
1433 possible_npeel_number
++;
1436 for (j
= 0; j
< possible_npeel_number
; j
++)
1438 gcc_assert (npeel_tmp
<= vf
);
1439 vect_peeling_hash_insert (loop_vinfo
, dr
, npeel_tmp
);
1440 npeel_tmp
+= nelements
;
1443 all_misalignments_unknown
= false;
1444 /* Data-ref that was chosen for the case that all the
1445 misalignments are unknown is not relevant anymore, since we
1446 have a data-ref with known alignment. */
1451 /* If we don't know any misalignment values, we prefer
1452 peeling for data-ref that has the maximum number of data-refs
1453 with the same alignment, unless the target prefers to align
1454 stores over load. */
1455 if (all_misalignments_unknown
)
1457 unsigned same_align_drs
1458 = STMT_VINFO_SAME_ALIGN_REFS (stmt_info
).length ();
1460 || same_align_drs_max
< same_align_drs
)
1462 same_align_drs_max
= same_align_drs
;
1465 /* For data-refs with the same number of related
1466 accesses prefer the one where the misalign
1467 computation will be invariant in the outermost loop. */
1468 else if (same_align_drs_max
== same_align_drs
)
1470 struct loop
*ivloop0
, *ivloop
;
1471 ivloop0
= outermost_invariant_loop_for_expr
1472 (loop
, DR_BASE_ADDRESS (dr0
));
1473 ivloop
= outermost_invariant_loop_for_expr
1474 (loop
, DR_BASE_ADDRESS (dr
));
1475 if ((ivloop
&& !ivloop0
)
1476 || (ivloop
&& ivloop0
1477 && flow_loop_nested_p (ivloop
, ivloop0
)))
1481 if (!first_store
&& DR_IS_WRITE (dr
))
1485 /* If there are both known and unknown misaligned accesses in the
1486 loop, we choose peeling amount according to the known
1488 if (!supportable_dr_alignment
)
1491 if (!first_store
&& DR_IS_WRITE (dr
))
1498 if (!aligned_access_p (dr
))
1500 if (dump_enabled_p ())
1501 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1502 "vector alignment may not be reachable\n");
1508 /* Check if we can possibly peel the loop. */
1509 if (!vect_can_advance_ivs_p (loop_vinfo
)
1510 || !slpeel_can_duplicate_loop_p (loop
, single_exit (loop
)))
1513 if (do_peeling
&& all_misalignments_unknown
1514 && vect_supportable_dr_alignment (dr0
, false))
1517 /* Check if the target requires to prefer stores over loads, i.e., if
1518 misaligned stores are more expensive than misaligned loads (taking
1519 drs with same alignment into account). */
1520 if (first_store
&& DR_IS_READ (dr0
))
1522 unsigned int load_inside_cost
= 0, load_outside_cost
= 0;
1523 unsigned int store_inside_cost
= 0, store_outside_cost
= 0;
1524 unsigned int load_inside_penalty
= 0, load_outside_penalty
= 0;
1525 unsigned int store_inside_penalty
= 0, store_outside_penalty
= 0;
1526 stmt_vector_for_cost dummy
;
1529 vect_get_data_access_cost (dr0
, &load_inside_cost
, &load_outside_cost
,
1531 vect_get_data_access_cost (first_store
, &store_inside_cost
,
1532 &store_outside_cost
, &dummy
);
1536 /* Calculate the penalty for leaving FIRST_STORE unaligned (by
1537 aligning the load DR0). */
1538 load_inside_penalty
= store_inside_cost
;
1539 load_outside_penalty
= store_outside_cost
;
1541 STMT_VINFO_SAME_ALIGN_REFS (vinfo_for_stmt (
1542 DR_STMT (first_store
))).iterate (i
, &dr
);
1544 if (DR_IS_READ (dr
))
1546 load_inside_penalty
+= load_inside_cost
;
1547 load_outside_penalty
+= load_outside_cost
;
1551 load_inside_penalty
+= store_inside_cost
;
1552 load_outside_penalty
+= store_outside_cost
;
1555 /* Calculate the penalty for leaving DR0 unaligned (by
1556 aligning the FIRST_STORE). */
1557 store_inside_penalty
= load_inside_cost
;
1558 store_outside_penalty
= load_outside_cost
;
1560 STMT_VINFO_SAME_ALIGN_REFS (vinfo_for_stmt (
1561 DR_STMT (dr0
))).iterate (i
, &dr
);
1563 if (DR_IS_READ (dr
))
1565 store_inside_penalty
+= load_inside_cost
;
1566 store_outside_penalty
+= load_outside_cost
;
1570 store_inside_penalty
+= store_inside_cost
;
1571 store_outside_penalty
+= store_outside_cost
;
1574 if (load_inside_penalty
> store_inside_penalty
1575 || (load_inside_penalty
== store_inside_penalty
1576 && load_outside_penalty
> store_outside_penalty
))
1580 /* In case there are only loads with different unknown misalignments, use
1581 peeling only if it may help to align other accesses in the loop. */
1583 && !STMT_VINFO_SAME_ALIGN_REFS (
1584 vinfo_for_stmt (DR_STMT (dr0
))).length ()
1585 && vect_supportable_dr_alignment (dr0
, false)
1586 != dr_unaligned_supported
)
1590 if (do_peeling
&& !dr0
)
1592 /* Peeling is possible, but there is no data access that is not supported
1593 unless aligned. So we try to choose the best possible peeling. */
1595 /* We should get here only if there are drs with known misalignment. */
1596 gcc_assert (!all_misalignments_unknown
);
1598 /* Choose the best peeling from the hash table. */
1599 dr0
= vect_peeling_hash_choose_best_peeling (loop_vinfo
, &npeel
,
1607 stmt
= DR_STMT (dr0
);
1608 stmt_info
= vinfo_for_stmt (stmt
);
1609 vectype
= STMT_VINFO_VECTYPE (stmt_info
);
1610 nelements
= TYPE_VECTOR_SUBPARTS (vectype
);
1612 if (known_alignment_for_access_p (dr0
))
1614 bool negative
= tree_int_cst_compare (DR_STEP (dr0
),
1615 size_zero_node
) < 0;
1618 /* Since it's known at compile time, compute the number of
1619 iterations in the peeled loop (the peeling factor) for use in
1620 updating DR_MISALIGNMENT values. The peeling factor is the
1621 vectorization factor minus the misalignment as an element
1623 mis
= DR_MISALIGNMENT (dr0
);
1624 mis
/= GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (DR_REF (dr0
))));
1625 npeel
= ((negative
? mis
- nelements
: nelements
- mis
)
1629 /* For interleaved data access every iteration accesses all the
1630 members of the group, therefore we divide the number of iterations
1631 by the group size. */
1632 stmt_info
= vinfo_for_stmt (DR_STMT (dr0
));
1633 if (STMT_VINFO_GROUPED_ACCESS (stmt_info
))
1634 npeel
/= GROUP_SIZE (stmt_info
);
1636 if (dump_enabled_p ())
1637 dump_printf_loc (MSG_NOTE
, vect_location
,
1638 "Try peeling by %d\n", npeel
);
1641 /* Ensure that all data refs can be vectorized after the peel. */
1642 FOR_EACH_VEC_ELT (datarefs
, i
, dr
)
1644 int save_misalignment
;
1649 stmt
= DR_STMT (dr
);
1650 stmt_info
= vinfo_for_stmt (stmt
);
1651 /* For interleaving, only the alignment of the first access
1653 if (STMT_VINFO_GROUPED_ACCESS (stmt_info
)
1654 && GROUP_FIRST_ELEMENT (stmt_info
) != stmt
)
1657 /* Strided loads perform only component accesses, alignment is
1658 irrelevant for them. */
1659 if (STMT_VINFO_STRIDE_LOAD_P (stmt_info
))
1662 save_misalignment
= DR_MISALIGNMENT (dr
);
1663 vect_update_misalignment_for_peel (dr
, dr0
, npeel
);
1664 supportable_dr_alignment
= vect_supportable_dr_alignment (dr
, false);
1665 SET_DR_MISALIGNMENT (dr
, save_misalignment
);
1667 if (!supportable_dr_alignment
)
1674 if (do_peeling
&& known_alignment_for_access_p (dr0
) && npeel
== 0)
1676 stat
= vect_verify_datarefs_alignment (loop_vinfo
, NULL
);
1681 body_cost_vec
.release ();
1688 unsigned max_allowed_peel
1689 = PARAM_VALUE (PARAM_VECT_MAX_PEELING_FOR_ALIGNMENT
);
1690 if (max_allowed_peel
!= (unsigned)-1)
1692 unsigned max_peel
= npeel
;
1695 gimple dr_stmt
= DR_STMT (dr0
);
1696 stmt_vec_info vinfo
= vinfo_for_stmt (dr_stmt
);
1697 tree vtype
= STMT_VINFO_VECTYPE (vinfo
);
1698 max_peel
= TYPE_VECTOR_SUBPARTS (vtype
) - 1;
1700 if (max_peel
> max_allowed_peel
)
1703 if (dump_enabled_p ())
1704 dump_printf_loc (MSG_NOTE
, vect_location
,
1705 "Disable peeling, max peels reached: %d\n", max_peel
);
1712 stmt_info_for_cost
*si
;
1713 void *data
= LOOP_VINFO_TARGET_COST_DATA (loop_vinfo
);
1715 /* (1.2) Update the DR_MISALIGNMENT of each data reference DR_i.
1716 If the misalignment of DR_i is identical to that of dr0 then set
1717 DR_MISALIGNMENT (DR_i) to zero. If the misalignment of DR_i and
1718 dr0 are known at compile time then increment DR_MISALIGNMENT (DR_i)
1719 by the peeling factor times the element size of DR_i (MOD the
1720 vectorization factor times the size). Otherwise, the
1721 misalignment of DR_i must be set to unknown. */
1722 FOR_EACH_VEC_ELT (datarefs
, i
, dr
)
1724 vect_update_misalignment_for_peel (dr
, dr0
, npeel
);
1726 LOOP_VINFO_UNALIGNED_DR (loop_vinfo
) = dr0
;
1728 LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo
) = npeel
;
1730 LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo
)
1731 = DR_MISALIGNMENT (dr0
);
1732 SET_DR_MISALIGNMENT (dr0
, 0);
1733 if (dump_enabled_p ())
1735 dump_printf_loc (MSG_NOTE
, vect_location
,
1736 "Alignment of access forced using peeling.\n");
1737 dump_printf_loc (MSG_NOTE
, vect_location
,
1738 "Peeling for alignment will be applied.\n");
1740 /* We've delayed passing the inside-loop peeling costs to the
1741 target cost model until we were sure peeling would happen.
1743 if (body_cost_vec
.exists ())
1745 FOR_EACH_VEC_ELT (body_cost_vec
, i
, si
)
1747 struct _stmt_vec_info
*stmt_info
1748 = si
->stmt
? vinfo_for_stmt (si
->stmt
) : NULL
;
1749 (void) add_stmt_cost (data
, si
->count
, si
->kind
, stmt_info
,
1750 si
->misalign
, vect_body
);
1752 body_cost_vec
.release ();
1755 stat
= vect_verify_datarefs_alignment (loop_vinfo
, NULL
);
1761 body_cost_vec
.release ();
1763 /* (2) Versioning to force alignment. */
1765 /* Try versioning if:
1766 1) optimize loop for speed
1767 2) there is at least one unsupported misaligned data ref with an unknown
1769 3) all misaligned data refs with a known misalignment are supported, and
1770 4) the number of runtime alignment checks is within reason. */
1773 optimize_loop_nest_for_speed_p (loop
)
1774 && (!loop
->inner
); /* FORNOW */
1778 FOR_EACH_VEC_ELT (datarefs
, i
, dr
)
1780 stmt
= DR_STMT (dr
);
1781 stmt_info
= vinfo_for_stmt (stmt
);
1783 /* For interleaving, only the alignment of the first access
1785 if (aligned_access_p (dr
)
1786 || (STMT_VINFO_GROUPED_ACCESS (stmt_info
)
1787 && GROUP_FIRST_ELEMENT (stmt_info
) != stmt
))
1790 /* Strided loads perform only component accesses, alignment is
1791 irrelevant for them. */
1792 if (STMT_VINFO_STRIDE_LOAD_P (stmt_info
))
1795 supportable_dr_alignment
= vect_supportable_dr_alignment (dr
, false);
1797 if (!supportable_dr_alignment
)
1803 if (known_alignment_for_access_p (dr
)
1804 || LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo
).length ()
1805 >= (unsigned) PARAM_VALUE (PARAM_VECT_MAX_VERSION_FOR_ALIGNMENT_CHECKS
))
1807 do_versioning
= false;
1811 stmt
= DR_STMT (dr
);
1812 vectype
= STMT_VINFO_VECTYPE (vinfo_for_stmt (stmt
));
1813 gcc_assert (vectype
);
1815 /* The rightmost bits of an aligned address must be zeros.
1816 Construct the mask needed for this test. For example,
1817 GET_MODE_SIZE for the vector mode V4SI is 16 bytes so the
1818 mask must be 15 = 0xf. */
1819 mask
= GET_MODE_SIZE (TYPE_MODE (vectype
)) - 1;
1821 /* FORNOW: use the same mask to test all potentially unaligned
1822 references in the loop. The vectorizer currently supports
1823 a single vector size, see the reference to
1824 GET_MODE_NUNITS (TYPE_MODE (vectype)) where the
1825 vectorization factor is computed. */
1826 gcc_assert (!LOOP_VINFO_PTR_MASK (loop_vinfo
)
1827 || LOOP_VINFO_PTR_MASK (loop_vinfo
) == mask
);
1828 LOOP_VINFO_PTR_MASK (loop_vinfo
) = mask
;
1829 LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo
).safe_push (
1834 /* Versioning requires at least one misaligned data reference. */
1835 if (!LOOP_REQUIRES_VERSIONING_FOR_ALIGNMENT (loop_vinfo
))
1836 do_versioning
= false;
1837 else if (!do_versioning
)
1838 LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo
).truncate (0);
1843 vec
<gimple
> may_misalign_stmts
1844 = LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo
);
1847 /* It can now be assumed that the data references in the statements
1848 in LOOP_VINFO_MAY_MISALIGN_STMTS will be aligned in the version
1849 of the loop being vectorized. */
1850 FOR_EACH_VEC_ELT (may_misalign_stmts
, i
, stmt
)
1852 stmt_vec_info stmt_info
= vinfo_for_stmt (stmt
);
1853 dr
= STMT_VINFO_DATA_REF (stmt_info
);
1854 SET_DR_MISALIGNMENT (dr
, 0);
1855 if (dump_enabled_p ())
1856 dump_printf_loc (MSG_NOTE
, vect_location
,
1857 "Alignment of access forced using versioning.\n");
1860 if (dump_enabled_p ())
1861 dump_printf_loc (MSG_NOTE
, vect_location
,
1862 "Versioning for alignment will be applied.\n");
1864 /* Peeling and versioning can't be done together at this time. */
1865 gcc_assert (! (do_peeling
&& do_versioning
));
1867 stat
= vect_verify_datarefs_alignment (loop_vinfo
, NULL
);
1872 /* This point is reached if neither peeling nor versioning is being done. */
1873 gcc_assert (! (do_peeling
|| do_versioning
));
1875 stat
= vect_verify_datarefs_alignment (loop_vinfo
, NULL
);
1880 /* Function vect_find_same_alignment_drs.
1882 Update group and alignment relations according to the chosen
1883 vectorization factor. */
1886 vect_find_same_alignment_drs (struct data_dependence_relation
*ddr
,
1887 loop_vec_info loop_vinfo
)
1890 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
1891 int vectorization_factor
= LOOP_VINFO_VECT_FACTOR (loop_vinfo
);
1892 struct data_reference
*dra
= DDR_A (ddr
);
1893 struct data_reference
*drb
= DDR_B (ddr
);
1894 stmt_vec_info stmtinfo_a
= vinfo_for_stmt (DR_STMT (dra
));
1895 stmt_vec_info stmtinfo_b
= vinfo_for_stmt (DR_STMT (drb
));
1896 int dra_size
= GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (DR_REF (dra
))));
1897 int drb_size
= GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (DR_REF (drb
))));
1898 lambda_vector dist_v
;
1899 unsigned int loop_depth
;
1901 if (DDR_ARE_DEPENDENT (ddr
) == chrec_known
)
1907 if (DDR_ARE_DEPENDENT (ddr
) == chrec_dont_know
)
1910 /* Loop-based vectorization and known data dependence. */
1911 if (DDR_NUM_DIST_VECTS (ddr
) == 0)
1914 /* Data-dependence analysis reports a distance vector of zero
1915 for data-references that overlap only in the first iteration
1916 but have different sign step (see PR45764).
1917 So as a sanity check require equal DR_STEP. */
1918 if (!operand_equal_p (DR_STEP (dra
), DR_STEP (drb
), 0))
1921 loop_depth
= index_in_loop_nest (loop
->num
, DDR_LOOP_NEST (ddr
));
1922 FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr
), i
, dist_v
)
1924 int dist
= dist_v
[loop_depth
];
1926 if (dump_enabled_p ())
1927 dump_printf_loc (MSG_NOTE
, vect_location
,
1928 "dependence distance = %d.\n", dist
);
1930 /* Same loop iteration. */
1932 || (dist
% vectorization_factor
== 0 && dra_size
== drb_size
))
1934 /* Two references with distance zero have the same alignment. */
1935 STMT_VINFO_SAME_ALIGN_REFS (stmtinfo_a
).safe_push (drb
);
1936 STMT_VINFO_SAME_ALIGN_REFS (stmtinfo_b
).safe_push (dra
);
1937 if (dump_enabled_p ())
1939 dump_printf_loc (MSG_NOTE
, vect_location
,
1940 "accesses have the same alignment.\n");
1941 dump_printf (MSG_NOTE
,
1942 "dependence distance modulo vf == 0 between ");
1943 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, DR_REF (dra
));
1944 dump_printf (MSG_NOTE
, " and ");
1945 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, DR_REF (drb
));
1946 dump_printf (MSG_NOTE
, "\n");
1953 /* Function vect_analyze_data_refs_alignment
1955 Analyze the alignment of the data-references in the loop.
1956 Return FALSE if a data reference is found that cannot be vectorized. */
1959 vect_analyze_data_refs_alignment (loop_vec_info loop_vinfo
,
1960 bb_vec_info bb_vinfo
)
1962 if (dump_enabled_p ())
1963 dump_printf_loc (MSG_NOTE
, vect_location
,
1964 "=== vect_analyze_data_refs_alignment ===\n");
1966 /* Mark groups of data references with same alignment using
1967 data dependence information. */
1970 vec
<ddr_p
> ddrs
= LOOP_VINFO_DDRS (loop_vinfo
);
1971 struct data_dependence_relation
*ddr
;
1974 FOR_EACH_VEC_ELT (ddrs
, i
, ddr
)
1975 vect_find_same_alignment_drs (ddr
, loop_vinfo
);
1978 if (!vect_compute_data_refs_alignment (loop_vinfo
, bb_vinfo
))
1980 if (dump_enabled_p ())
1981 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1982 "not vectorized: can't calculate alignment "
1991 /* Analyze groups of accesses: check that DR belongs to a group of
1992 accesses of legal size, step, etc. Detect gaps, single element
1993 interleaving, and other special cases. Set grouped access info.
1994 Collect groups of strided stores for further use in SLP analysis. */
1997 vect_analyze_group_access (struct data_reference
*dr
)
1999 tree step
= DR_STEP (dr
);
2000 tree scalar_type
= TREE_TYPE (DR_REF (dr
));
2001 HOST_WIDE_INT type_size
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (scalar_type
));
2002 gimple stmt
= DR_STMT (dr
);
2003 stmt_vec_info stmt_info
= vinfo_for_stmt (stmt
);
2004 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_info
);
2005 bb_vec_info bb_vinfo
= STMT_VINFO_BB_VINFO (stmt_info
);
2006 HOST_WIDE_INT dr_step
= TREE_INT_CST_LOW (step
);
2007 HOST_WIDE_INT groupsize
, last_accessed_element
= 1;
2008 bool slp_impossible
= false;
2009 struct loop
*loop
= NULL
;
2012 loop
= LOOP_VINFO_LOOP (loop_vinfo
);
2014 /* For interleaving, GROUPSIZE is STEP counted in elements, i.e., the
2015 size of the interleaving group (including gaps). */
2016 groupsize
= absu_hwi (dr_step
) / type_size
;
2018 /* Not consecutive access is possible only if it is a part of interleaving. */
2019 if (!GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt
)))
2021 /* Check if it this DR is a part of interleaving, and is a single
2022 element of the group that is accessed in the loop. */
2024 /* Gaps are supported only for loads. STEP must be a multiple of the type
2025 size. The size of the group must be a power of 2. */
2027 && (dr_step
% type_size
) == 0
2029 && exact_log2 (groupsize
) != -1)
2031 GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt
)) = stmt
;
2032 GROUP_SIZE (vinfo_for_stmt (stmt
)) = groupsize
;
2033 if (dump_enabled_p ())
2035 dump_printf_loc (MSG_NOTE
, vect_location
,
2036 "Detected single element interleaving ");
2037 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, DR_REF (dr
));
2038 dump_printf (MSG_NOTE
, " step ");
2039 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, step
);
2040 dump_printf (MSG_NOTE
, "\n");
2045 if (dump_enabled_p ())
2046 dump_printf_loc (MSG_NOTE
, vect_location
,
2047 "Data access with gaps requires scalar "
2051 if (dump_enabled_p ())
2052 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2053 "Peeling for outer loop is not"
2058 LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo
) = true;
2064 if (dump_enabled_p ())
2066 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2067 "not consecutive access ");
2068 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
, stmt
, 0);
2069 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
2074 /* Mark the statement as unvectorizable. */
2075 STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dr
))) = false;
2082 if (GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt
)) == stmt
)
2084 /* First stmt in the interleaving chain. Check the chain. */
2085 gimple next
= GROUP_NEXT_ELEMENT (vinfo_for_stmt (stmt
));
2086 struct data_reference
*data_ref
= dr
;
2087 unsigned int count
= 1;
2088 tree prev_init
= DR_INIT (data_ref
);
2090 HOST_WIDE_INT diff
, gaps
= 0;
2091 unsigned HOST_WIDE_INT count_in_bytes
;
2095 /* Skip same data-refs. In case that two or more stmts share
2096 data-ref (supported only for loads), we vectorize only the first
2097 stmt, and the rest get their vectorized loads from the first
2099 if (!tree_int_cst_compare (DR_INIT (data_ref
),
2100 DR_INIT (STMT_VINFO_DATA_REF (
2101 vinfo_for_stmt (next
)))))
2103 if (DR_IS_WRITE (data_ref
))
2105 if (dump_enabled_p ())
2106 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2107 "Two store stmts share the same dr.\n");
2111 /* For load use the same data-ref load. */
2112 GROUP_SAME_DR_STMT (vinfo_for_stmt (next
)) = prev
;
2115 next
= GROUP_NEXT_ELEMENT (vinfo_for_stmt (next
));
2120 data_ref
= STMT_VINFO_DATA_REF (vinfo_for_stmt (next
));
2122 /* All group members have the same STEP by construction. */
2123 gcc_checking_assert (operand_equal_p (DR_STEP (data_ref
), step
, 0));
2125 /* Check that the distance between two accesses is equal to the type
2126 size. Otherwise, we have gaps. */
2127 diff
= (TREE_INT_CST_LOW (DR_INIT (data_ref
))
2128 - TREE_INT_CST_LOW (prev_init
)) / type_size
;
2131 /* FORNOW: SLP of accesses with gaps is not supported. */
2132 slp_impossible
= true;
2133 if (DR_IS_WRITE (data_ref
))
2135 if (dump_enabled_p ())
2136 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2137 "interleaved store with gaps\n");
2144 last_accessed_element
+= diff
;
2146 /* Store the gap from the previous member of the group. If there is no
2147 gap in the access, GROUP_GAP is always 1. */
2148 GROUP_GAP (vinfo_for_stmt (next
)) = diff
;
2150 prev_init
= DR_INIT (data_ref
);
2151 next
= GROUP_NEXT_ELEMENT (vinfo_for_stmt (next
));
2152 /* Count the number of data-refs in the chain. */
2156 /* COUNT is the number of accesses found, we multiply it by the size of
2157 the type to get COUNT_IN_BYTES. */
2158 count_in_bytes
= type_size
* count
;
2160 /* Check that the size of the interleaving (including gaps) is not
2161 greater than STEP. */
2163 && absu_hwi (dr_step
) < count_in_bytes
+ gaps
* type_size
)
2165 if (dump_enabled_p ())
2167 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2168 "interleaving size is greater than step for ");
2169 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
,
2171 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
2176 /* Check that the size of the interleaving is equal to STEP for stores,
2177 i.e., that there are no gaps. */
2179 && absu_hwi (dr_step
) != count_in_bytes
)
2181 if (DR_IS_READ (dr
))
2183 slp_impossible
= true;
2184 /* There is a gap after the last load in the group. This gap is a
2185 difference between the groupsize and the number of elements.
2186 When there is no gap, this difference should be 0. */
2187 GROUP_GAP (vinfo_for_stmt (stmt
)) = groupsize
- count
;
2191 if (dump_enabled_p ())
2192 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2193 "interleaved store with gaps\n");
2198 /* Check that STEP is a multiple of type size. */
2200 && (dr_step
% type_size
) != 0)
2202 if (dump_enabled_p ())
2204 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2205 "step is not a multiple of type size: step ");
2206 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
, step
);
2207 dump_printf (MSG_MISSED_OPTIMIZATION
, " size ");
2208 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
,
2209 TYPE_SIZE_UNIT (scalar_type
));
2210 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
2218 GROUP_SIZE (vinfo_for_stmt (stmt
)) = groupsize
;
2219 if (dump_enabled_p ())
2220 dump_printf_loc (MSG_NOTE
, vect_location
,
2221 "Detected interleaving of size %d\n", (int)groupsize
);
2223 /* SLP: create an SLP data structure for every interleaving group of
2224 stores for further analysis in vect_analyse_slp. */
2225 if (DR_IS_WRITE (dr
) && !slp_impossible
)
2228 LOOP_VINFO_GROUPED_STORES (loop_vinfo
).safe_push (stmt
);
2230 BB_VINFO_GROUPED_STORES (bb_vinfo
).safe_push (stmt
);
2233 /* There is a gap in the end of the group. */
2234 if (groupsize
- last_accessed_element
> 0 && loop_vinfo
)
2236 if (dump_enabled_p ())
2237 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2238 "Data access with gaps requires scalar "
2242 if (dump_enabled_p ())
2243 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2244 "Peeling for outer loop is not supported\n");
2248 LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo
) = true;
2256 /* Analyze the access pattern of the data-reference DR.
2257 In case of non-consecutive accesses call vect_analyze_group_access() to
2258 analyze groups of accesses. */
2261 vect_analyze_data_ref_access (struct data_reference
*dr
)
2263 tree step
= DR_STEP (dr
);
2264 tree scalar_type
= TREE_TYPE (DR_REF (dr
));
2265 gimple stmt
= DR_STMT (dr
);
2266 stmt_vec_info stmt_info
= vinfo_for_stmt (stmt
);
2267 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_info
);
2268 struct loop
*loop
= NULL
;
2271 loop
= LOOP_VINFO_LOOP (loop_vinfo
);
2273 if (loop_vinfo
&& !step
)
2275 if (dump_enabled_p ())
2276 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2277 "bad data-ref access in loop\n");
2281 /* Allow invariant loads in not nested loops. */
2282 if (loop_vinfo
&& integer_zerop (step
))
2284 GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt
)) = NULL
;
2285 if (nested_in_vect_loop_p (loop
, stmt
))
2287 if (dump_enabled_p ())
2288 dump_printf_loc (MSG_NOTE
, vect_location
,
2289 "zero step in inner loop of nest\n");
2292 return DR_IS_READ (dr
);
2295 if (loop
&& nested_in_vect_loop_p (loop
, stmt
))
2297 /* Interleaved accesses are not yet supported within outer-loop
2298 vectorization for references in the inner-loop. */
2299 GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt
)) = NULL
;
2301 /* For the rest of the analysis we use the outer-loop step. */
2302 step
= STMT_VINFO_DR_STEP (stmt_info
);
2303 if (integer_zerop (step
))
2305 if (dump_enabled_p ())
2306 dump_printf_loc (MSG_NOTE
, vect_location
,
2307 "zero step in outer loop.\n");
2308 if (DR_IS_READ (dr
))
2316 if (TREE_CODE (step
) == INTEGER_CST
)
2318 HOST_WIDE_INT dr_step
= TREE_INT_CST_LOW (step
);
2319 if (!tree_int_cst_compare (step
, TYPE_SIZE_UNIT (scalar_type
))
2321 && !compare_tree_int (TYPE_SIZE_UNIT (scalar_type
), -dr_step
)))
2323 /* Mark that it is not interleaving. */
2324 GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt
)) = NULL
;
2329 if (loop
&& nested_in_vect_loop_p (loop
, stmt
))
2331 if (dump_enabled_p ())
2332 dump_printf_loc (MSG_NOTE
, vect_location
,
2333 "grouped access in outer loop.\n");
2337 /* Assume this is a DR handled by non-constant strided load case. */
2338 if (TREE_CODE (step
) != INTEGER_CST
)
2339 return STMT_VINFO_STRIDE_LOAD_P (stmt_info
);
2341 /* Not consecutive access - check if it's a part of interleaving group. */
2342 return vect_analyze_group_access (dr
);
2347 /* A helper function used in the comparator function to sort data
2348 references. T1 and T2 are two data references to be compared.
2349 The function returns -1, 0, or 1. */
2352 compare_tree (tree t1
, tree t2
)
2355 enum tree_code code
;
2366 if (TREE_CODE (t1
) != TREE_CODE (t2
))
2367 return TREE_CODE (t1
) < TREE_CODE (t2
) ? -1 : 1;
2369 code
= TREE_CODE (t1
);
2372 /* For const values, we can just use hash values for comparisons. */
2380 hashval_t h1
= iterative_hash_expr (t1
, 0);
2381 hashval_t h2
= iterative_hash_expr (t2
, 0);
2383 return h1
< h2
? -1 : 1;
2388 cmp
= compare_tree (SSA_NAME_VAR (t1
), SSA_NAME_VAR (t2
));
2392 if (SSA_NAME_VERSION (t1
) != SSA_NAME_VERSION (t2
))
2393 return SSA_NAME_VERSION (t1
) < SSA_NAME_VERSION (t2
) ? -1 : 1;
2397 tclass
= TREE_CODE_CLASS (code
);
2399 /* For var-decl, we could compare their UIDs. */
2400 if (tclass
== tcc_declaration
)
2402 if (DECL_UID (t1
) != DECL_UID (t2
))
2403 return DECL_UID (t1
) < DECL_UID (t2
) ? -1 : 1;
2407 /* For expressions with operands, compare their operands recursively. */
2408 for (i
= TREE_OPERAND_LENGTH (t1
) - 1; i
>= 0; --i
)
2410 cmp
= compare_tree (TREE_OPERAND (t1
, i
), TREE_OPERAND (t2
, i
));
2420 /* Compare two data-references DRA and DRB to group them into chunks
2421 suitable for grouping. */
2424 dr_group_sort_cmp (const void *dra_
, const void *drb_
)
2426 data_reference_p dra
= *(data_reference_p
*)const_cast<void *>(dra_
);
2427 data_reference_p drb
= *(data_reference_p
*)const_cast<void *>(drb_
);
2430 /* Stabilize sort. */
2434 /* Ordering of DRs according to base. */
2435 if (!operand_equal_p (DR_BASE_ADDRESS (dra
), DR_BASE_ADDRESS (drb
), 0))
2437 cmp
= compare_tree (DR_BASE_ADDRESS (dra
), DR_BASE_ADDRESS (drb
));
2442 /* And according to DR_OFFSET. */
2443 if (!dr_equal_offsets_p (dra
, drb
))
2445 cmp
= compare_tree (DR_OFFSET (dra
), DR_OFFSET (drb
));
2450 /* Put reads before writes. */
2451 if (DR_IS_READ (dra
) != DR_IS_READ (drb
))
2452 return DR_IS_READ (dra
) ? -1 : 1;
2454 /* Then sort after access size. */
2455 if (!operand_equal_p (TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dra
))),
2456 TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (drb
))), 0))
2458 cmp
= compare_tree (TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dra
))),
2459 TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (drb
))));
2464 /* And after step. */
2465 if (!operand_equal_p (DR_STEP (dra
), DR_STEP (drb
), 0))
2467 cmp
= compare_tree (DR_STEP (dra
), DR_STEP (drb
));
2472 /* Then sort after DR_INIT. In case of identical DRs sort after stmt UID. */
2473 cmp
= tree_int_cst_compare (DR_INIT (dra
), DR_INIT (drb
));
2475 return gimple_uid (DR_STMT (dra
)) < gimple_uid (DR_STMT (drb
)) ? -1 : 1;
2479 /* Function vect_analyze_data_ref_accesses.
2481 Analyze the access pattern of all the data references in the loop.
2483 FORNOW: the only access pattern that is considered vectorizable is a
2484 simple step 1 (consecutive) access.
2486 FORNOW: handle only arrays and pointer accesses. */
2489 vect_analyze_data_ref_accesses (loop_vec_info loop_vinfo
, bb_vec_info bb_vinfo
)
2492 vec
<data_reference_p
> datarefs
;
2493 struct data_reference
*dr
;
2495 if (dump_enabled_p ())
2496 dump_printf_loc (MSG_NOTE
, vect_location
,
2497 "=== vect_analyze_data_ref_accesses ===\n");
2500 datarefs
= LOOP_VINFO_DATAREFS (loop_vinfo
);
2502 datarefs
= BB_VINFO_DATAREFS (bb_vinfo
);
2504 if (datarefs
.is_empty ())
2507 /* Sort the array of datarefs to make building the interleaving chains
2508 linear. Don't modify the original vector's order, it is needed for
2509 determining what dependencies are reversed. */
2510 vec
<data_reference_p
> datarefs_copy
= datarefs
.copy ();
2511 qsort (datarefs_copy
.address (), datarefs_copy
.length (),
2512 sizeof (data_reference_p
), dr_group_sort_cmp
);
2514 /* Build the interleaving chains. */
2515 for (i
= 0; i
< datarefs_copy
.length () - 1;)
2517 data_reference_p dra
= datarefs_copy
[i
];
2518 stmt_vec_info stmtinfo_a
= vinfo_for_stmt (DR_STMT (dra
));
2519 stmt_vec_info lastinfo
= NULL
;
2520 for (i
= i
+ 1; i
< datarefs_copy
.length (); ++i
)
2522 data_reference_p drb
= datarefs_copy
[i
];
2523 stmt_vec_info stmtinfo_b
= vinfo_for_stmt (DR_STMT (drb
));
2525 /* ??? Imperfect sorting (non-compatible types, non-modulo
2526 accesses, same accesses) can lead to a group to be artificially
2527 split here as we don't just skip over those. If it really
2528 matters we can push those to a worklist and re-iterate
2529 over them. The we can just skip ahead to the next DR here. */
2531 /* Check that the data-refs have same first location (except init)
2532 and they are both either store or load (not load and store,
2533 not masked loads or stores). */
2534 if (DR_IS_READ (dra
) != DR_IS_READ (drb
)
2535 || !operand_equal_p (DR_BASE_ADDRESS (dra
),
2536 DR_BASE_ADDRESS (drb
), 0)
2537 || !dr_equal_offsets_p (dra
, drb
)
2538 || !gimple_assign_single_p (DR_STMT (dra
))
2539 || !gimple_assign_single_p (DR_STMT (drb
)))
2542 /* Check that the data-refs have the same constant size and step. */
2543 tree sza
= TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dra
)));
2544 tree szb
= TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (drb
)));
2545 if (!tree_fits_uhwi_p (sza
)
2546 || !tree_fits_uhwi_p (szb
)
2547 || !tree_int_cst_equal (sza
, szb
)
2548 || !tree_fits_shwi_p (DR_STEP (dra
))
2549 || !tree_fits_shwi_p (DR_STEP (drb
))
2550 || !tree_int_cst_equal (DR_STEP (dra
), DR_STEP (drb
)))
2553 /* Do not place the same access in the interleaving chain twice. */
2554 if (tree_int_cst_compare (DR_INIT (dra
), DR_INIT (drb
)) == 0)
2557 /* Check the types are compatible.
2558 ??? We don't distinguish this during sorting. */
2559 if (!types_compatible_p (TREE_TYPE (DR_REF (dra
)),
2560 TREE_TYPE (DR_REF (drb
))))
2563 /* Sorting has ensured that DR_INIT (dra) <= DR_INIT (drb). */
2564 HOST_WIDE_INT init_a
= TREE_INT_CST_LOW (DR_INIT (dra
));
2565 HOST_WIDE_INT init_b
= TREE_INT_CST_LOW (DR_INIT (drb
));
2566 gcc_assert (init_a
< init_b
);
2568 /* If init_b == init_a + the size of the type * k, we have an
2569 interleaving, and DRA is accessed before DRB. */
2570 HOST_WIDE_INT type_size_a
= tree_to_uhwi (sza
);
2571 if ((init_b
- init_a
) % type_size_a
!= 0)
2574 /* The step (if not zero) is greater than the difference between
2575 data-refs' inits. This splits groups into suitable sizes. */
2576 HOST_WIDE_INT step
= tree_to_shwi (DR_STEP (dra
));
2577 if (step
!= 0 && step
<= (init_b
- init_a
))
2580 if (dump_enabled_p ())
2582 dump_printf_loc (MSG_NOTE
, vect_location
,
2583 "Detected interleaving ");
2584 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, DR_REF (dra
));
2585 dump_printf (MSG_NOTE
, " and ");
2586 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, DR_REF (drb
));
2587 dump_printf (MSG_NOTE
, "\n");
2590 /* Link the found element into the group list. */
2591 if (!GROUP_FIRST_ELEMENT (stmtinfo_a
))
2593 GROUP_FIRST_ELEMENT (stmtinfo_a
) = DR_STMT (dra
);
2594 lastinfo
= stmtinfo_a
;
2596 GROUP_FIRST_ELEMENT (stmtinfo_b
) = DR_STMT (dra
);
2597 GROUP_NEXT_ELEMENT (lastinfo
) = DR_STMT (drb
);
2598 lastinfo
= stmtinfo_b
;
2602 FOR_EACH_VEC_ELT (datarefs_copy
, i
, dr
)
2603 if (STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dr
)))
2604 && !vect_analyze_data_ref_access (dr
))
2606 if (dump_enabled_p ())
2607 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2608 "not vectorized: complicated access pattern.\n");
2612 /* Mark the statement as not vectorizable. */
2613 STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dr
))) = false;
2618 datarefs_copy
.release ();
2623 datarefs_copy
.release ();
2628 /* Operator == between two dr_with_seg_len objects.
2630 This equality operator is used to make sure two data refs
2631 are the same one so that we will consider to combine the
2632 aliasing checks of those two pairs of data dependent data
2636 operator == (const dr_with_seg_len
& d1
,
2637 const dr_with_seg_len
& d2
)
2639 return operand_equal_p (DR_BASE_ADDRESS (d1
.dr
),
2640 DR_BASE_ADDRESS (d2
.dr
), 0)
2641 && compare_tree (d1
.offset
, d2
.offset
) == 0
2642 && compare_tree (d1
.seg_len
, d2
.seg_len
) == 0;
2645 /* Function comp_dr_with_seg_len_pair.
2647 Comparison function for sorting objects of dr_with_seg_len_pair_t
2648 so that we can combine aliasing checks in one scan. */
2651 comp_dr_with_seg_len_pair (const void *p1_
, const void *p2_
)
2653 const dr_with_seg_len_pair_t
* p1
= (const dr_with_seg_len_pair_t
*) p1_
;
2654 const dr_with_seg_len_pair_t
* p2
= (const dr_with_seg_len_pair_t
*) p2_
;
2656 const dr_with_seg_len
&p11
= p1
->first
,
2661 /* For DR pairs (a, b) and (c, d), we only consider to merge the alias checks
2662 if a and c have the same basic address snd step, and b and d have the same
2663 address and step. Therefore, if any a&c or b&d don't have the same address
2664 and step, we don't care the order of those two pairs after sorting. */
2667 if ((comp_res
= compare_tree (DR_BASE_ADDRESS (p11
.dr
),
2668 DR_BASE_ADDRESS (p21
.dr
))) != 0)
2670 if ((comp_res
= compare_tree (DR_BASE_ADDRESS (p12
.dr
),
2671 DR_BASE_ADDRESS (p22
.dr
))) != 0)
2673 if ((comp_res
= compare_tree (DR_STEP (p11
.dr
), DR_STEP (p21
.dr
))) != 0)
2675 if ((comp_res
= compare_tree (DR_STEP (p12
.dr
), DR_STEP (p22
.dr
))) != 0)
2677 if ((comp_res
= compare_tree (p11
.offset
, p21
.offset
)) != 0)
2679 if ((comp_res
= compare_tree (p12
.offset
, p22
.offset
)) != 0)
2685 template <class T
> static void
2693 /* Function vect_vfa_segment_size.
2695 Create an expression that computes the size of segment
2696 that will be accessed for a data reference. The functions takes into
2697 account that realignment loads may access one more vector.
2700 DR: The data reference.
2701 LENGTH_FACTOR: segment length to consider.
2703 Return an expression whose value is the size of segment which will be
2707 vect_vfa_segment_size (struct data_reference
*dr
, tree length_factor
)
2709 tree segment_length
;
2711 if (integer_zerop (DR_STEP (dr
)))
2712 segment_length
= TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dr
)));
2714 segment_length
= size_binop (MULT_EXPR
,
2715 fold_convert (sizetype
, DR_STEP (dr
)),
2716 fold_convert (sizetype
, length_factor
));
2718 if (vect_supportable_dr_alignment (dr
, false)
2719 == dr_explicit_realign_optimized
)
2721 tree vector_size
= TYPE_SIZE_UNIT
2722 (STMT_VINFO_VECTYPE (vinfo_for_stmt (DR_STMT (dr
))));
2724 segment_length
= size_binop (PLUS_EXPR
, segment_length
, vector_size
);
2726 return segment_length
;
2729 /* Function vect_prune_runtime_alias_test_list.
2731 Prune a list of ddrs to be tested at run-time by versioning for alias.
2732 Merge several alias checks into one if possible.
2733 Return FALSE if resulting list of ddrs is longer then allowed by
2734 PARAM_VECT_MAX_VERSION_FOR_ALIAS_CHECKS, otherwise return TRUE. */
2737 vect_prune_runtime_alias_test_list (loop_vec_info loop_vinfo
)
2739 vec
<ddr_p
> may_alias_ddrs
=
2740 LOOP_VINFO_MAY_ALIAS_DDRS (loop_vinfo
);
2741 vec
<dr_with_seg_len_pair_t
>& comp_alias_ddrs
=
2742 LOOP_VINFO_COMP_ALIAS_DDRS (loop_vinfo
);
2743 int vect_factor
= LOOP_VINFO_VECT_FACTOR (loop_vinfo
);
2744 tree scalar_loop_iters
= LOOP_VINFO_NITERS (loop_vinfo
);
2750 if (dump_enabled_p ())
2751 dump_printf_loc (MSG_NOTE
, vect_location
,
2752 "=== vect_prune_runtime_alias_test_list ===\n");
2754 if (may_alias_ddrs
.is_empty ())
2757 /* Basically, for each pair of dependent data refs store_ptr_0
2758 and load_ptr_0, we create an expression:
2760 ((store_ptr_0 + store_segment_length_0) <= load_ptr_0)
2761 || (load_ptr_0 + load_segment_length_0) <= store_ptr_0))
2763 for aliasing checks. However, in some cases we can decrease
2764 the number of checks by combining two checks into one. For
2765 example, suppose we have another pair of data refs store_ptr_0
2766 and load_ptr_1, and if the following condition is satisfied:
2768 load_ptr_0 < load_ptr_1 &&
2769 load_ptr_1 - load_ptr_0 - load_segment_length_0 < store_segment_length_0
2771 (this condition means, in each iteration of vectorized loop,
2772 the accessed memory of store_ptr_0 cannot be between the memory
2773 of load_ptr_0 and load_ptr_1.)
2775 we then can use only the following expression to finish the
2776 alising checks between store_ptr_0 & load_ptr_0 and
2777 store_ptr_0 & load_ptr_1:
2779 ((store_ptr_0 + store_segment_length_0) <= load_ptr_0)
2780 || (load_ptr_1 + load_segment_length_1 <= store_ptr_0))
2782 Note that we only consider that load_ptr_0 and load_ptr_1 have the
2783 same basic address. */
2785 comp_alias_ddrs
.create (may_alias_ddrs
.length ());
2787 /* First, we collect all data ref pairs for aliasing checks. */
2788 FOR_EACH_VEC_ELT (may_alias_ddrs
, i
, ddr
)
2790 struct data_reference
*dr_a
, *dr_b
;
2791 gimple dr_group_first_a
, dr_group_first_b
;
2792 tree segment_length_a
, segment_length_b
;
2793 gimple stmt_a
, stmt_b
;
2796 stmt_a
= DR_STMT (DDR_A (ddr
));
2797 dr_group_first_a
= GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt_a
));
2798 if (dr_group_first_a
)
2800 stmt_a
= dr_group_first_a
;
2801 dr_a
= STMT_VINFO_DATA_REF (vinfo_for_stmt (stmt_a
));
2805 stmt_b
= DR_STMT (DDR_B (ddr
));
2806 dr_group_first_b
= GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt_b
));
2807 if (dr_group_first_b
)
2809 stmt_b
= dr_group_first_b
;
2810 dr_b
= STMT_VINFO_DATA_REF (vinfo_for_stmt (stmt_b
));
2813 if (!operand_equal_p (DR_STEP (dr_a
), DR_STEP (dr_b
), 0))
2814 length_factor
= scalar_loop_iters
;
2816 length_factor
= size_int (vect_factor
);
2817 segment_length_a
= vect_vfa_segment_size (dr_a
, length_factor
);
2818 segment_length_b
= vect_vfa_segment_size (dr_b
, length_factor
);
2820 dr_with_seg_len_pair_t dr_with_seg_len_pair
2821 (dr_with_seg_len (dr_a
, segment_length_a
),
2822 dr_with_seg_len (dr_b
, segment_length_b
));
2824 if (compare_tree (DR_BASE_ADDRESS (dr_a
), DR_BASE_ADDRESS (dr_b
)) > 0)
2825 swap (dr_with_seg_len_pair
.first
, dr_with_seg_len_pair
.second
);
2827 comp_alias_ddrs
.safe_push (dr_with_seg_len_pair
);
2830 /* Second, we sort the collected data ref pairs so that we can scan
2831 them once to combine all possible aliasing checks. */
2832 comp_alias_ddrs
.qsort (comp_dr_with_seg_len_pair
);
2834 /* Third, we scan the sorted dr pairs and check if we can combine
2835 alias checks of two neighbouring dr pairs. */
2836 for (size_t i
= 1; i
< comp_alias_ddrs
.length (); ++i
)
2838 /* Deal with two ddrs (dr_a1, dr_b1) and (dr_a2, dr_b2). */
2839 dr_with_seg_len
*dr_a1
= &comp_alias_ddrs
[i
-1].first
,
2840 *dr_b1
= &comp_alias_ddrs
[i
-1].second
,
2841 *dr_a2
= &comp_alias_ddrs
[i
].first
,
2842 *dr_b2
= &comp_alias_ddrs
[i
].second
;
2844 /* Remove duplicate data ref pairs. */
2845 if (*dr_a1
== *dr_a2
&& *dr_b1
== *dr_b2
)
2847 if (dump_enabled_p ())
2849 dump_printf_loc (MSG_NOTE
, vect_location
,
2850 "found equal ranges ");
2851 dump_generic_expr (MSG_NOTE
, TDF_SLIM
,
2852 DR_REF (dr_a1
->dr
));
2853 dump_printf (MSG_NOTE
, ", ");
2854 dump_generic_expr (MSG_NOTE
, TDF_SLIM
,
2855 DR_REF (dr_b1
->dr
));
2856 dump_printf (MSG_NOTE
, " and ");
2857 dump_generic_expr (MSG_NOTE
, TDF_SLIM
,
2858 DR_REF (dr_a2
->dr
));
2859 dump_printf (MSG_NOTE
, ", ");
2860 dump_generic_expr (MSG_NOTE
, TDF_SLIM
,
2861 DR_REF (dr_b2
->dr
));
2862 dump_printf (MSG_NOTE
, "\n");
2865 comp_alias_ddrs
.ordered_remove (i
--);
2869 if (*dr_a1
== *dr_a2
|| *dr_b1
== *dr_b2
)
2871 /* We consider the case that DR_B1 and DR_B2 are same memrefs,
2872 and DR_A1 and DR_A2 are two consecutive memrefs. */
2873 if (*dr_a1
== *dr_a2
)
2875 swap (dr_a1
, dr_b1
);
2876 swap (dr_a2
, dr_b2
);
2879 if (!operand_equal_p (DR_BASE_ADDRESS (dr_a1
->dr
),
2880 DR_BASE_ADDRESS (dr_a2
->dr
),
2882 || !tree_fits_shwi_p (dr_a1
->offset
)
2883 || !tree_fits_shwi_p (dr_a2
->offset
))
2886 HOST_WIDE_INT diff
= (tree_to_shwi (dr_a2
->offset
)
2887 - tree_to_shwi (dr_a1
->offset
));
2890 /* Now we check if the following condition is satisfied:
2892 DIFF - SEGMENT_LENGTH_A < SEGMENT_LENGTH_B
2894 where DIFF = DR_A2->OFFSET - DR_A1->OFFSET. However,
2895 SEGMENT_LENGTH_A or SEGMENT_LENGTH_B may not be constant so we
2896 have to make a best estimation. We can get the minimum value
2897 of SEGMENT_LENGTH_B as a constant, represented by MIN_SEG_LEN_B,
2898 then either of the following two conditions can guarantee the
2901 1: DIFF <= MIN_SEG_LEN_B
2902 2: DIFF - SEGMENT_LENGTH_A < MIN_SEG_LEN_B
2907 min_seg_len_b
= (TREE_CODE (dr_b1
->seg_len
) == INTEGER_CST
) ?
2908 TREE_INT_CST_LOW (dr_b1
->seg_len
) :
2911 if (diff
<= min_seg_len_b
2912 || (TREE_CODE (dr_a1
->seg_len
) == INTEGER_CST
2913 && diff
- (HOST_WIDE_INT
) TREE_INT_CST_LOW (dr_a1
->seg_len
) <
2916 if (dump_enabled_p ())
2918 dump_printf_loc (MSG_NOTE
, vect_location
,
2919 "merging ranges for ");
2920 dump_generic_expr (MSG_NOTE
, TDF_SLIM
,
2921 DR_REF (dr_a1
->dr
));
2922 dump_printf (MSG_NOTE
, ", ");
2923 dump_generic_expr (MSG_NOTE
, TDF_SLIM
,
2924 DR_REF (dr_b1
->dr
));
2925 dump_printf (MSG_NOTE
, " and ");
2926 dump_generic_expr (MSG_NOTE
, TDF_SLIM
,
2927 DR_REF (dr_a2
->dr
));
2928 dump_printf (MSG_NOTE
, ", ");
2929 dump_generic_expr (MSG_NOTE
, TDF_SLIM
,
2930 DR_REF (dr_b2
->dr
));
2931 dump_printf (MSG_NOTE
, "\n");
2934 dr_a1
->seg_len
= size_binop (PLUS_EXPR
,
2935 dr_a2
->seg_len
, size_int (diff
));
2936 comp_alias_ddrs
.ordered_remove (i
--);
2941 dump_printf_loc (MSG_NOTE
, vect_location
,
2942 "improved number of alias checks from %d to %d\n",
2943 may_alias_ddrs
.length (), comp_alias_ddrs
.length ());
2944 if ((int) comp_alias_ddrs
.length () >
2945 PARAM_VALUE (PARAM_VECT_MAX_VERSION_FOR_ALIAS_CHECKS
))
2951 /* Check whether a non-affine read in stmt is suitable for gather load
2952 and if so, return a builtin decl for that operation. */
2955 vect_check_gather (gimple stmt
, loop_vec_info loop_vinfo
, tree
*basep
,
2956 tree
*offp
, int *scalep
)
2958 HOST_WIDE_INT scale
= 1, pbitpos
, pbitsize
;
2959 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
2960 stmt_vec_info stmt_info
= vinfo_for_stmt (stmt
);
2961 struct data_reference
*dr
= STMT_VINFO_DATA_REF (stmt_info
);
2962 tree offtype
= NULL_TREE
;
2963 tree decl
, base
, off
;
2964 enum machine_mode pmode
;
2965 int punsignedp
, pvolatilep
;
2968 /* For masked loads/stores, DR_REF (dr) is an artificial MEM_REF,
2969 see if we can use the def stmt of the address. */
2970 if (is_gimple_call (stmt
)
2971 && gimple_call_internal_p (stmt
)
2972 && (gimple_call_internal_fn (stmt
) == IFN_MASK_LOAD
2973 || gimple_call_internal_fn (stmt
) == IFN_MASK_STORE
)
2974 && TREE_CODE (base
) == MEM_REF
2975 && TREE_CODE (TREE_OPERAND (base
, 0)) == SSA_NAME
2976 && integer_zerop (TREE_OPERAND (base
, 1))
2977 && !expr_invariant_in_loop_p (loop
, TREE_OPERAND (base
, 0)))
2979 gimple def_stmt
= SSA_NAME_DEF_STMT (TREE_OPERAND (base
, 0));
2980 if (is_gimple_assign (def_stmt
)
2981 && gimple_assign_rhs_code (def_stmt
) == ADDR_EXPR
)
2982 base
= TREE_OPERAND (gimple_assign_rhs1 (def_stmt
), 0);
2985 /* The gather builtins need address of the form
2986 loop_invariant + vector * {1, 2, 4, 8}
2988 loop_invariant + sign_extend (vector) * { 1, 2, 4, 8 }.
2989 Unfortunately DR_BASE_ADDRESS/DR_OFFSET can be a mixture
2990 of loop invariants/SSA_NAMEs defined in the loop, with casts,
2991 multiplications and additions in it. To get a vector, we need
2992 a single SSA_NAME that will be defined in the loop and will
2993 contain everything that is not loop invariant and that can be
2994 vectorized. The following code attempts to find such a preexistng
2995 SSA_NAME OFF and put the loop invariants into a tree BASE
2996 that can be gimplified before the loop. */
2997 base
= get_inner_reference (base
, &pbitsize
, &pbitpos
, &off
,
2998 &pmode
, &punsignedp
, &pvolatilep
, false);
2999 gcc_assert (base
!= NULL_TREE
&& (pbitpos
% BITS_PER_UNIT
) == 0);
3001 if (TREE_CODE (base
) == MEM_REF
)
3003 if (!integer_zerop (TREE_OPERAND (base
, 1)))
3005 if (off
== NULL_TREE
)
3007 double_int moff
= mem_ref_offset (base
);
3008 off
= double_int_to_tree (sizetype
, moff
);
3011 off
= size_binop (PLUS_EXPR
, off
,
3012 fold_convert (sizetype
, TREE_OPERAND (base
, 1)));
3014 base
= TREE_OPERAND (base
, 0);
3017 base
= build_fold_addr_expr (base
);
3019 if (off
== NULL_TREE
)
3020 off
= size_zero_node
;
3022 /* If base is not loop invariant, either off is 0, then we start with just
3023 the constant offset in the loop invariant BASE and continue with base
3024 as OFF, otherwise give up.
3025 We could handle that case by gimplifying the addition of base + off
3026 into some SSA_NAME and use that as off, but for now punt. */
3027 if (!expr_invariant_in_loop_p (loop
, base
))
3029 if (!integer_zerop (off
))
3032 base
= size_int (pbitpos
/ BITS_PER_UNIT
);
3034 /* Otherwise put base + constant offset into the loop invariant BASE
3035 and continue with OFF. */
3038 base
= fold_convert (sizetype
, base
);
3039 base
= size_binop (PLUS_EXPR
, base
, size_int (pbitpos
/ BITS_PER_UNIT
));
3042 /* OFF at this point may be either a SSA_NAME or some tree expression
3043 from get_inner_reference. Try to peel off loop invariants from it
3044 into BASE as long as possible. */
3046 while (offtype
== NULL_TREE
)
3048 enum tree_code code
;
3049 tree op0
, op1
, add
= NULL_TREE
;
3051 if (TREE_CODE (off
) == SSA_NAME
)
3053 gimple def_stmt
= SSA_NAME_DEF_STMT (off
);
3055 if (expr_invariant_in_loop_p (loop
, off
))
3058 if (gimple_code (def_stmt
) != GIMPLE_ASSIGN
)
3061 op0
= gimple_assign_rhs1 (def_stmt
);
3062 code
= gimple_assign_rhs_code (def_stmt
);
3063 op1
= gimple_assign_rhs2 (def_stmt
);
3067 if (get_gimple_rhs_class (TREE_CODE (off
)) == GIMPLE_TERNARY_RHS
)
3069 code
= TREE_CODE (off
);
3070 extract_ops_from_tree (off
, &code
, &op0
, &op1
);
3074 case POINTER_PLUS_EXPR
:
3076 if (expr_invariant_in_loop_p (loop
, op0
))
3081 add
= fold_convert (sizetype
, add
);
3083 add
= size_binop (MULT_EXPR
, add
, size_int (scale
));
3084 base
= size_binop (PLUS_EXPR
, base
, add
);
3087 if (expr_invariant_in_loop_p (loop
, op1
))
3095 if (expr_invariant_in_loop_p (loop
, op1
))
3097 add
= fold_convert (sizetype
, op1
);
3098 add
= size_binop (MINUS_EXPR
, size_zero_node
, add
);
3104 if (scale
== 1 && tree_fits_shwi_p (op1
))
3106 scale
= tree_to_shwi (op1
);
3115 if (!POINTER_TYPE_P (TREE_TYPE (op0
))
3116 && !INTEGRAL_TYPE_P (TREE_TYPE (op0
)))
3118 if (TYPE_PRECISION (TREE_TYPE (op0
))
3119 == TYPE_PRECISION (TREE_TYPE (off
)))
3124 if (TYPE_PRECISION (TREE_TYPE (op0
))
3125 < TYPE_PRECISION (TREE_TYPE (off
)))
3128 offtype
= TREE_TYPE (off
);
3139 /* If at the end OFF still isn't a SSA_NAME or isn't
3140 defined in the loop, punt. */
3141 if (TREE_CODE (off
) != SSA_NAME
3142 || expr_invariant_in_loop_p (loop
, off
))
3145 if (offtype
== NULL_TREE
)
3146 offtype
= TREE_TYPE (off
);
3148 decl
= targetm
.vectorize
.builtin_gather (STMT_VINFO_VECTYPE (stmt_info
),
3150 if (decl
== NULL_TREE
)
3162 /* Function vect_analyze_data_refs.
3164 Find all the data references in the loop or basic block.
3166 The general structure of the analysis of data refs in the vectorizer is as
3168 1- vect_analyze_data_refs(loop/bb): call
3169 compute_data_dependences_for_loop/bb to find and analyze all data-refs
3170 in the loop/bb and their dependences.
3171 2- vect_analyze_dependences(): apply dependence testing using ddrs.
3172 3- vect_analyze_drs_alignment(): check that ref_stmt.alignment is ok.
3173 4- vect_analyze_drs_access(): check that ref_stmt.step is ok.
3178 vect_analyze_data_refs (loop_vec_info loop_vinfo
,
3179 bb_vec_info bb_vinfo
,
3180 int *min_vf
, unsigned *n_stmts
)
3182 struct loop
*loop
= NULL
;
3183 basic_block bb
= NULL
;
3185 vec
<data_reference_p
> datarefs
;
3186 struct data_reference
*dr
;
3189 if (dump_enabled_p ())
3190 dump_printf_loc (MSG_NOTE
, vect_location
,
3191 "=== vect_analyze_data_refs ===\n");
3195 basic_block
*bbs
= LOOP_VINFO_BBS (loop_vinfo
);
3197 loop
= LOOP_VINFO_LOOP (loop_vinfo
);
3198 datarefs
= LOOP_VINFO_DATAREFS (loop_vinfo
);
3199 if (!find_loop_nest (loop
, &LOOP_VINFO_LOOP_NEST (loop_vinfo
)))
3201 if (dump_enabled_p ())
3202 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
3203 "not vectorized: loop contains function calls"
3204 " or data references that cannot be analyzed\n");
3208 for (i
= 0; i
< loop
->num_nodes
; i
++)
3210 gimple_stmt_iterator gsi
;
3212 for (gsi
= gsi_start_bb (bbs
[i
]); !gsi_end_p (gsi
); gsi_next (&gsi
))
3214 gimple stmt
= gsi_stmt (gsi
);
3215 if (is_gimple_debug (stmt
))
3218 if (!find_data_references_in_stmt (loop
, stmt
, &datarefs
))
3220 if (is_gimple_call (stmt
) && loop
->safelen
)
3222 tree fndecl
= gimple_call_fndecl (stmt
), op
;
3223 if (fndecl
!= NULL_TREE
)
3225 struct cgraph_node
*node
= cgraph_get_node (fndecl
);
3226 if (node
!= NULL
&& node
->simd_clones
!= NULL
)
3228 unsigned int j
, n
= gimple_call_num_args (stmt
);
3229 for (j
= 0; j
< n
; j
++)
3231 op
= gimple_call_arg (stmt
, j
);
3233 || (REFERENCE_CLASS_P (op
)
3234 && get_base_address (op
)))
3237 op
= gimple_call_lhs (stmt
);
3238 /* Ignore #pragma omp declare simd functions
3239 if they don't have data references in the
3240 call stmt itself. */
3244 || (REFERENCE_CLASS_P (op
)
3245 && get_base_address (op
)))))
3250 LOOP_VINFO_DATAREFS (loop_vinfo
) = datarefs
;
3251 if (dump_enabled_p ())
3252 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
3253 "not vectorized: loop contains function "
3254 "calls or data references that cannot "
3261 LOOP_VINFO_DATAREFS (loop_vinfo
) = datarefs
;
3265 gimple_stmt_iterator gsi
;
3267 bb
= BB_VINFO_BB (bb_vinfo
);
3268 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
3270 gimple stmt
= gsi_stmt (gsi
);
3271 if (is_gimple_debug (stmt
))
3274 if (!find_data_references_in_stmt (NULL
, stmt
,
3275 &BB_VINFO_DATAREFS (bb_vinfo
)))
3277 /* Mark the rest of the basic-block as unvectorizable. */
3278 for (; !gsi_end_p (gsi
); gsi_next (&gsi
))
3280 stmt
= gsi_stmt (gsi
);
3281 STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (stmt
)) = false;
3287 datarefs
= BB_VINFO_DATAREFS (bb_vinfo
);
3290 /* Go through the data-refs, check that the analysis succeeded. Update
3291 pointer from stmt_vec_info struct to DR and vectype. */
3293 FOR_EACH_VEC_ELT (datarefs
, i
, dr
)
3296 stmt_vec_info stmt_info
;
3297 tree base
, offset
, init
;
3298 bool gather
= false;
3299 bool simd_lane_access
= false;
3303 if (!dr
|| !DR_REF (dr
))
3305 if (dump_enabled_p ())
3306 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
3307 "not vectorized: unhandled data-ref\n");
3311 stmt
= DR_STMT (dr
);
3312 stmt_info
= vinfo_for_stmt (stmt
);
3314 /* Discard clobbers from the dataref vector. We will remove
3315 clobber stmts during vectorization. */
3316 if (gimple_clobber_p (stmt
))
3319 if (i
== datarefs
.length () - 1)
3324 datarefs
.ordered_remove (i
);
3329 /* Check that analysis of the data-ref succeeded. */
3330 if (!DR_BASE_ADDRESS (dr
) || !DR_OFFSET (dr
) || !DR_INIT (dr
)
3335 && !TREE_THIS_VOLATILE (DR_REF (dr
))
3336 && targetm
.vectorize
.builtin_gather
!= NULL
;
3337 bool maybe_simd_lane_access
3338 = loop_vinfo
&& loop
->simduid
;
3340 /* If target supports vector gather loads, or if this might be
3341 a SIMD lane access, see if they can't be used. */
3343 && (maybe_gather
|| maybe_simd_lane_access
)
3344 && !nested_in_vect_loop_p (loop
, stmt
))
3346 struct data_reference
*newdr
3347 = create_data_ref (NULL
, loop_containing_stmt (stmt
),
3348 DR_REF (dr
), stmt
, true);
3349 gcc_assert (newdr
!= NULL
&& DR_REF (newdr
));
3350 if (DR_BASE_ADDRESS (newdr
)
3351 && DR_OFFSET (newdr
)
3354 && integer_zerop (DR_STEP (newdr
)))
3356 if (maybe_simd_lane_access
)
3358 tree off
= DR_OFFSET (newdr
);
3360 if (TREE_CODE (DR_INIT (newdr
)) == INTEGER_CST
3361 && TREE_CODE (off
) == MULT_EXPR
3362 && tree_fits_uhwi_p (TREE_OPERAND (off
, 1)))
3364 tree step
= TREE_OPERAND (off
, 1);
3365 off
= TREE_OPERAND (off
, 0);
3367 if (CONVERT_EXPR_P (off
)
3368 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (off
,
3370 < TYPE_PRECISION (TREE_TYPE (off
)))
3371 off
= TREE_OPERAND (off
, 0);
3372 if (TREE_CODE (off
) == SSA_NAME
)
3374 gimple def
= SSA_NAME_DEF_STMT (off
);
3375 tree reft
= TREE_TYPE (DR_REF (newdr
));
3376 if (is_gimple_call (def
)
3377 && gimple_call_internal_p (def
)
3378 && (gimple_call_internal_fn (def
)
3379 == IFN_GOMP_SIMD_LANE
))
3381 tree arg
= gimple_call_arg (def
, 0);
3382 gcc_assert (TREE_CODE (arg
) == SSA_NAME
);
3383 arg
= SSA_NAME_VAR (arg
);
3384 if (arg
== loop
->simduid
3386 && tree_int_cst_equal
3387 (TYPE_SIZE_UNIT (reft
),
3390 DR_OFFSET (newdr
) = ssize_int (0);
3391 DR_STEP (newdr
) = step
;
3392 DR_ALIGNED_TO (newdr
)
3393 = size_int (BIGGEST_ALIGNMENT
);
3395 simd_lane_access
= true;
3401 if (!simd_lane_access
&& maybe_gather
)
3407 if (!gather
&& !simd_lane_access
)
3408 free_data_ref (newdr
);
3411 if (!gather
&& !simd_lane_access
)
3413 if (dump_enabled_p ())
3415 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
3416 "not vectorized: data ref analysis "
3418 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
, stmt
, 0);
3419 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
3429 if (TREE_CODE (DR_BASE_ADDRESS (dr
)) == INTEGER_CST
)
3431 if (dump_enabled_p ())
3432 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
3433 "not vectorized: base addr of dr is a "
3439 if (gather
|| simd_lane_access
)
3444 if (TREE_THIS_VOLATILE (DR_REF (dr
)))
3446 if (dump_enabled_p ())
3448 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
3449 "not vectorized: volatile type ");
3450 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
, stmt
, 0);
3451 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
3460 if (stmt_can_throw_internal (stmt
))
3462 if (dump_enabled_p ())
3464 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
3465 "not vectorized: statement can throw an "
3467 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
, stmt
, 0);
3468 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
3474 if (gather
|| simd_lane_access
)
3479 if (TREE_CODE (DR_REF (dr
)) == COMPONENT_REF
3480 && DECL_BIT_FIELD (TREE_OPERAND (DR_REF (dr
), 1)))
3482 if (dump_enabled_p ())
3484 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
3485 "not vectorized: statement is bitfield "
3487 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
, stmt
, 0);
3488 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
3494 if (gather
|| simd_lane_access
)
3499 base
= unshare_expr (DR_BASE_ADDRESS (dr
));
3500 offset
= unshare_expr (DR_OFFSET (dr
));
3501 init
= unshare_expr (DR_INIT (dr
));
3503 if (is_gimple_call (stmt
)
3504 && (!gimple_call_internal_p (stmt
)
3505 || (gimple_call_internal_fn (stmt
) != IFN_MASK_LOAD
3506 && gimple_call_internal_fn (stmt
) != IFN_MASK_STORE
)))
3508 if (dump_enabled_p ())
3510 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
3511 "not vectorized: dr in a call ");
3512 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
, stmt
, 0);
3513 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
3519 if (gather
|| simd_lane_access
)
3524 /* Update DR field in stmt_vec_info struct. */
3526 /* If the dataref is in an inner-loop of the loop that is considered for
3527 for vectorization, we also want to analyze the access relative to
3528 the outer-loop (DR contains information only relative to the
3529 inner-most enclosing loop). We do that by building a reference to the
3530 first location accessed by the inner-loop, and analyze it relative to
3532 if (loop
&& nested_in_vect_loop_p (loop
, stmt
))
3534 tree outer_step
, outer_base
, outer_init
;
3535 HOST_WIDE_INT pbitsize
, pbitpos
;
3537 enum machine_mode pmode
;
3538 int punsignedp
, pvolatilep
;
3539 affine_iv base_iv
, offset_iv
;
3542 /* Build a reference to the first location accessed by the
3543 inner-loop: *(BASE+INIT). (The first location is actually
3544 BASE+INIT+OFFSET, but we add OFFSET separately later). */
3545 tree inner_base
= build_fold_indirect_ref
3546 (fold_build_pointer_plus (base
, init
));
3548 if (dump_enabled_p ())
3550 dump_printf_loc (MSG_NOTE
, vect_location
,
3551 "analyze in outer-loop: ");
3552 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, inner_base
);
3553 dump_printf (MSG_NOTE
, "\n");
3556 outer_base
= get_inner_reference (inner_base
, &pbitsize
, &pbitpos
,
3557 &poffset
, &pmode
, &punsignedp
, &pvolatilep
, false);
3558 gcc_assert (outer_base
!= NULL_TREE
);
3560 if (pbitpos
% BITS_PER_UNIT
!= 0)
3562 if (dump_enabled_p ())
3563 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
3564 "failed: bit offset alignment.\n");
3568 outer_base
= build_fold_addr_expr (outer_base
);
3569 if (!simple_iv (loop
, loop_containing_stmt (stmt
), outer_base
,
3572 if (dump_enabled_p ())
3573 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
3574 "failed: evolution of base is not affine.\n");
3581 poffset
= fold_build2 (PLUS_EXPR
, TREE_TYPE (offset
), offset
,
3589 offset_iv
.base
= ssize_int (0);
3590 offset_iv
.step
= ssize_int (0);
3592 else if (!simple_iv (loop
, loop_containing_stmt (stmt
), poffset
,
3595 if (dump_enabled_p ())
3596 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
3597 "evolution of offset is not affine.\n");
3601 outer_init
= ssize_int (pbitpos
/ BITS_PER_UNIT
);
3602 split_constant_offset (base_iv
.base
, &base_iv
.base
, &dinit
);
3603 outer_init
= size_binop (PLUS_EXPR
, outer_init
, dinit
);
3604 split_constant_offset (offset_iv
.base
, &offset_iv
.base
, &dinit
);
3605 outer_init
= size_binop (PLUS_EXPR
, outer_init
, dinit
);
3607 outer_step
= size_binop (PLUS_EXPR
,
3608 fold_convert (ssizetype
, base_iv
.step
),
3609 fold_convert (ssizetype
, offset_iv
.step
));
3611 STMT_VINFO_DR_STEP (stmt_info
) = outer_step
;
3612 /* FIXME: Use canonicalize_base_object_address (base_iv.base); */
3613 STMT_VINFO_DR_BASE_ADDRESS (stmt_info
) = base_iv
.base
;
3614 STMT_VINFO_DR_INIT (stmt_info
) = outer_init
;
3615 STMT_VINFO_DR_OFFSET (stmt_info
) =
3616 fold_convert (ssizetype
, offset_iv
.base
);
3617 STMT_VINFO_DR_ALIGNED_TO (stmt_info
) =
3618 size_int (highest_pow2_factor (offset_iv
.base
));
3620 if (dump_enabled_p ())
3622 dump_printf_loc (MSG_NOTE
, vect_location
,
3623 "\touter base_address: ");
3624 dump_generic_expr (MSG_NOTE
, TDF_SLIM
,
3625 STMT_VINFO_DR_BASE_ADDRESS (stmt_info
));
3626 dump_printf (MSG_NOTE
, "\n\touter offset from base address: ");
3627 dump_generic_expr (MSG_NOTE
, TDF_SLIM
,
3628 STMT_VINFO_DR_OFFSET (stmt_info
));
3629 dump_printf (MSG_NOTE
,
3630 "\n\touter constant offset from base address: ");
3631 dump_generic_expr (MSG_NOTE
, TDF_SLIM
,
3632 STMT_VINFO_DR_INIT (stmt_info
));
3633 dump_printf (MSG_NOTE
, "\n\touter step: ");
3634 dump_generic_expr (MSG_NOTE
, TDF_SLIM
,
3635 STMT_VINFO_DR_STEP (stmt_info
));
3636 dump_printf (MSG_NOTE
, "\n\touter aligned to: ");
3637 dump_generic_expr (MSG_NOTE
, TDF_SLIM
,
3638 STMT_VINFO_DR_ALIGNED_TO (stmt_info
));
3639 dump_printf (MSG_NOTE
, "\n");
3643 if (STMT_VINFO_DATA_REF (stmt_info
))
3645 if (dump_enabled_p ())
3647 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
3648 "not vectorized: more than one data ref "
3650 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
, stmt
, 0);
3651 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
3657 if (gather
|| simd_lane_access
)
3662 STMT_VINFO_DATA_REF (stmt_info
) = dr
;
3663 if (simd_lane_access
)
3665 STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info
) = true;
3666 free_data_ref (datarefs
[i
]);
3670 /* Set vectype for STMT. */
3671 scalar_type
= TREE_TYPE (DR_REF (dr
));
3672 STMT_VINFO_VECTYPE (stmt_info
)
3673 = get_vectype_for_scalar_type (scalar_type
);
3674 if (!STMT_VINFO_VECTYPE (stmt_info
))
3676 if (dump_enabled_p ())
3678 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
3679 "not vectorized: no vectype for stmt: ");
3680 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
, stmt
, 0);
3681 dump_printf (MSG_MISSED_OPTIMIZATION
, " scalar_type: ");
3682 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_DETAILS
,
3684 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
3690 if (gather
|| simd_lane_access
)
3692 STMT_VINFO_DATA_REF (stmt_info
) = NULL
;
3700 if (dump_enabled_p ())
3702 dump_printf_loc (MSG_NOTE
, vect_location
,
3703 "got vectype for stmt: ");
3704 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
, stmt
, 0);
3705 dump_generic_expr (MSG_NOTE
, TDF_SLIM
,
3706 STMT_VINFO_VECTYPE (stmt_info
));
3707 dump_printf (MSG_NOTE
, "\n");
3711 /* Adjust the minimal vectorization factor according to the
3713 vf
= TYPE_VECTOR_SUBPARTS (STMT_VINFO_VECTYPE (stmt_info
));
3721 gather
= 0 != vect_check_gather (stmt
, loop_vinfo
, NULL
, &off
, NULL
);
3723 && get_vectype_for_scalar_type (TREE_TYPE (off
)) == NULL_TREE
)
3727 STMT_VINFO_DATA_REF (stmt_info
) = NULL
;
3729 if (dump_enabled_p ())
3731 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
3732 "not vectorized: not suitable for gather "
3734 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
, stmt
, 0);
3735 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
3741 STMT_VINFO_GATHER_P (stmt_info
) = true;
3744 && TREE_CODE (DR_STEP (dr
)) != INTEGER_CST
)
3746 if (nested_in_vect_loop_p (loop
, stmt
)
3747 || !DR_IS_READ (dr
))
3749 if (dump_enabled_p ())
3751 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
3752 "not vectorized: not suitable for strided "
3754 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
, stmt
, 0);
3755 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
3759 STMT_VINFO_STRIDE_LOAD_P (stmt_info
) = true;
3763 /* If we stopped analysis at the first dataref we could not analyze
3764 when trying to vectorize a basic-block mark the rest of the datarefs
3765 as not vectorizable and truncate the vector of datarefs. That
3766 avoids spending useless time in analyzing their dependence. */
3767 if (i
!= datarefs
.length ())
3769 gcc_assert (bb_vinfo
!= NULL
);
3770 for (unsigned j
= i
; j
< datarefs
.length (); ++j
)
3772 data_reference_p dr
= datarefs
[j
];
3773 STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dr
))) = false;
3776 datarefs
.truncate (i
);
3783 /* Function vect_get_new_vect_var.
3785 Returns a name for a new variable. The current naming scheme appends the
3786 prefix "vect_" or "vect_p" (depending on the value of VAR_KIND) to
3787 the name of vectorizer generated variables, and appends that to NAME if
3791 vect_get_new_vect_var (tree type
, enum vect_var_kind var_kind
, const char *name
)
3798 case vect_simple_var
:
3801 case vect_scalar_var
:
3804 case vect_pointer_var
:
3813 char* tmp
= concat (prefix
, "_", name
, NULL
);
3814 new_vect_var
= create_tmp_reg (type
, tmp
);
3818 new_vect_var
= create_tmp_reg (type
, prefix
);
3820 return new_vect_var
;
3824 /* Function vect_create_addr_base_for_vector_ref.
3826 Create an expression that computes the address of the first memory location
3827 that will be accessed for a data reference.
3830 STMT: The statement containing the data reference.
3831 NEW_STMT_LIST: Must be initialized to NULL_TREE or a statement list.
3832 OFFSET: Optional. If supplied, it is be added to the initial address.
3833 LOOP: Specify relative to which loop-nest should the address be computed.
3834 For example, when the dataref is in an inner-loop nested in an
3835 outer-loop that is now being vectorized, LOOP can be either the
3836 outer-loop, or the inner-loop. The first memory location accessed
3837 by the following dataref ('in' points to short):
3844 if LOOP=i_loop: &in (relative to i_loop)
3845 if LOOP=j_loop: &in+i*2B (relative to j_loop)
3846 BYTE_OFFSET: Optional, defaulted to NULL. If supplied, it is added to the
3847 initial address. Unlike OFFSET, which is number of elements to
3848 be added, BYTE_OFFSET is measured in bytes.
3851 1. Return an SSA_NAME whose value is the address of the memory location of
3852 the first vector of the data reference.
3853 2. If new_stmt_list is not NULL_TREE after return then the caller must insert
3854 these statement(s) which define the returned SSA_NAME.
3856 FORNOW: We are only handling array accesses with step 1. */
3859 vect_create_addr_base_for_vector_ref (gimple stmt
,
3860 gimple_seq
*new_stmt_list
,
3865 stmt_vec_info stmt_info
= vinfo_for_stmt (stmt
);
3866 struct data_reference
*dr
= STMT_VINFO_DATA_REF (stmt_info
);
3868 const char *base_name
;
3871 gimple_seq seq
= NULL
;
3875 tree step
= TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dr
)));
3876 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_info
);
3878 if (loop_vinfo
&& loop
&& loop
!= (gimple_bb (stmt
))->loop_father
)
3880 struct loop
*outer_loop
= LOOP_VINFO_LOOP (loop_vinfo
);
3882 gcc_assert (nested_in_vect_loop_p (outer_loop
, stmt
));
3884 data_ref_base
= unshare_expr (STMT_VINFO_DR_BASE_ADDRESS (stmt_info
));
3885 base_offset
= unshare_expr (STMT_VINFO_DR_OFFSET (stmt_info
));
3886 init
= unshare_expr (STMT_VINFO_DR_INIT (stmt_info
));
3890 data_ref_base
= unshare_expr (DR_BASE_ADDRESS (dr
));
3891 base_offset
= unshare_expr (DR_OFFSET (dr
));
3892 init
= unshare_expr (DR_INIT (dr
));
3896 base_name
= get_name (data_ref_base
);
3899 base_offset
= ssize_int (0);
3900 init
= ssize_int (0);
3901 base_name
= get_name (DR_REF (dr
));
3904 /* Create base_offset */
3905 base_offset
= size_binop (PLUS_EXPR
,
3906 fold_convert (sizetype
, base_offset
),
3907 fold_convert (sizetype
, init
));
3911 offset
= fold_build2 (MULT_EXPR
, sizetype
,
3912 fold_convert (sizetype
, offset
), step
);
3913 base_offset
= fold_build2 (PLUS_EXPR
, sizetype
,
3914 base_offset
, offset
);
3918 byte_offset
= fold_convert (sizetype
, byte_offset
);
3919 base_offset
= fold_build2 (PLUS_EXPR
, sizetype
,
3920 base_offset
, byte_offset
);
3923 /* base + base_offset */
3925 addr_base
= fold_build_pointer_plus (data_ref_base
, base_offset
);
3928 addr_base
= build1 (ADDR_EXPR
,
3929 build_pointer_type (TREE_TYPE (DR_REF (dr
))),
3930 unshare_expr (DR_REF (dr
)));
3933 vect_ptr_type
= build_pointer_type (STMT_VINFO_VECTYPE (stmt_info
));
3934 addr_base
= fold_convert (vect_ptr_type
, addr_base
);
3935 dest
= vect_get_new_vect_var (vect_ptr_type
, vect_pointer_var
, base_name
);
3936 addr_base
= force_gimple_operand (addr_base
, &seq
, false, dest
);
3937 gimple_seq_add_seq (new_stmt_list
, seq
);
3939 if (DR_PTR_INFO (dr
)
3940 && TREE_CODE (addr_base
) == SSA_NAME
)
3942 duplicate_ssa_name_ptr_info (addr_base
, DR_PTR_INFO (dr
));
3943 unsigned int align
= TYPE_ALIGN_UNIT (STMT_VINFO_VECTYPE (stmt_info
));
3944 int misalign
= DR_MISALIGNMENT (dr
);
3945 if (offset
|| byte_offset
|| (misalign
== -1))
3946 mark_ptr_info_alignment_unknown (SSA_NAME_PTR_INFO (addr_base
));
3948 set_ptr_info_alignment (SSA_NAME_PTR_INFO (addr_base
), align
, misalign
);
3951 if (dump_enabled_p ())
3953 dump_printf_loc (MSG_NOTE
, vect_location
, "created ");
3954 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, addr_base
);
3955 dump_printf (MSG_NOTE
, "\n");
3962 /* Function vect_create_data_ref_ptr.
3964 Create a new pointer-to-AGGR_TYPE variable (ap), that points to the first
3965 location accessed in the loop by STMT, along with the def-use update
3966 chain to appropriately advance the pointer through the loop iterations.
3967 Also set aliasing information for the pointer. This pointer is used by
3968 the callers to this function to create a memory reference expression for
3969 vector load/store access.
3972 1. STMT: a stmt that references memory. Expected to be of the form
3973 GIMPLE_ASSIGN <name, data-ref> or
3974 GIMPLE_ASSIGN <data-ref, name>.
3975 2. AGGR_TYPE: the type of the reference, which should be either a vector
3977 3. AT_LOOP: the loop where the vector memref is to be created.
3978 4. OFFSET (optional): an offset to be added to the initial address accessed
3979 by the data-ref in STMT.
3980 5. BSI: location where the new stmts are to be placed if there is no loop
3981 6. ONLY_INIT: indicate if ap is to be updated in the loop, or remain
3982 pointing to the initial address.
3983 7. BYTE_OFFSET (optional, defaults to NULL): a byte offset to be added
3984 to the initial address accessed by the data-ref in STMT. This is
3985 similar to OFFSET, but OFFSET is counted in elements, while BYTE_OFFSET
3989 1. Declare a new ptr to vector_type, and have it point to the base of the
3990 data reference (initial addressed accessed by the data reference).
3991 For example, for vector of type V8HI, the following code is generated:
3994 ap = (v8hi *)initial_address;
3996 if OFFSET is not supplied:
3997 initial_address = &a[init];
3998 if OFFSET is supplied:
3999 initial_address = &a[init + OFFSET];
4000 if BYTE_OFFSET is supplied:
4001 initial_address = &a[init] + BYTE_OFFSET;
4003 Return the initial_address in INITIAL_ADDRESS.
4005 2. If ONLY_INIT is true, just return the initial pointer. Otherwise, also
4006 update the pointer in each iteration of the loop.
4008 Return the increment stmt that updates the pointer in PTR_INCR.
4010 3. Set INV_P to true if the access pattern of the data reference in the
4011 vectorized loop is invariant. Set it to false otherwise.
4013 4. Return the pointer. */
4016 vect_create_data_ref_ptr (gimple stmt
, tree aggr_type
, struct loop
*at_loop
,
4017 tree offset
, tree
*initial_address
,
4018 gimple_stmt_iterator
*gsi
, gimple
*ptr_incr
,
4019 bool only_init
, bool *inv_p
, tree byte_offset
)
4021 const char *base_name
;
4022 stmt_vec_info stmt_info
= vinfo_for_stmt (stmt
);
4023 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_info
);
4024 struct loop
*loop
= NULL
;
4025 bool nested_in_vect_loop
= false;
4026 struct loop
*containing_loop
= NULL
;
4031 gimple_seq new_stmt_list
= NULL
;
4035 struct data_reference
*dr
= STMT_VINFO_DATA_REF (stmt_info
);
4037 gimple_stmt_iterator incr_gsi
;
4039 tree indx_before_incr
, indx_after_incr
;
4042 bb_vec_info bb_vinfo
= STMT_VINFO_BB_VINFO (stmt_info
);
4044 gcc_assert (TREE_CODE (aggr_type
) == ARRAY_TYPE
4045 || TREE_CODE (aggr_type
) == VECTOR_TYPE
);
4049 loop
= LOOP_VINFO_LOOP (loop_vinfo
);
4050 nested_in_vect_loop
= nested_in_vect_loop_p (loop
, stmt
);
4051 containing_loop
= (gimple_bb (stmt
))->loop_father
;
4052 pe
= loop_preheader_edge (loop
);
4056 gcc_assert (bb_vinfo
);
4061 /* Check the step (evolution) of the load in LOOP, and record
4062 whether it's invariant. */
4063 if (nested_in_vect_loop
)
4064 step
= STMT_VINFO_DR_STEP (stmt_info
);
4066 step
= DR_STEP (STMT_VINFO_DATA_REF (stmt_info
));
4068 if (integer_zerop (step
))
4073 /* Create an expression for the first address accessed by this load
4075 base_name
= get_name (DR_BASE_ADDRESS (dr
));
4077 if (dump_enabled_p ())
4079 tree dr_base_type
= TREE_TYPE (DR_BASE_OBJECT (dr
));
4080 dump_printf_loc (MSG_NOTE
, vect_location
,
4081 "create %s-pointer variable to type: ",
4082 get_tree_code_name (TREE_CODE (aggr_type
)));
4083 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, aggr_type
);
4084 if (TREE_CODE (dr_base_type
) == ARRAY_TYPE
)
4085 dump_printf (MSG_NOTE
, " vectorizing an array ref: ");
4086 else if (TREE_CODE (dr_base_type
) == VECTOR_TYPE
)
4087 dump_printf (MSG_NOTE
, " vectorizing a vector ref: ");
4088 else if (TREE_CODE (dr_base_type
) == RECORD_TYPE
)
4089 dump_printf (MSG_NOTE
, " vectorizing a record based array ref: ");
4091 dump_printf (MSG_NOTE
, " vectorizing a pointer ref: ");
4092 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, DR_BASE_OBJECT (dr
));
4093 dump_printf (MSG_NOTE
, "\n");
4096 /* (1) Create the new aggregate-pointer variable.
4097 Vector and array types inherit the alias set of their component
4098 type by default so we need to use a ref-all pointer if the data
4099 reference does not conflict with the created aggregated data
4100 reference because it is not addressable. */
4101 bool need_ref_all
= false;
4102 if (!alias_sets_conflict_p (get_alias_set (aggr_type
),
4103 get_alias_set (DR_REF (dr
))))
4104 need_ref_all
= true;
4105 /* Likewise for any of the data references in the stmt group. */
4106 else if (STMT_VINFO_GROUP_SIZE (stmt_info
) > 1)
4108 gimple orig_stmt
= STMT_VINFO_GROUP_FIRST_ELEMENT (stmt_info
);
4111 stmt_vec_info sinfo
= vinfo_for_stmt (orig_stmt
);
4112 struct data_reference
*sdr
= STMT_VINFO_DATA_REF (sinfo
);
4113 if (!alias_sets_conflict_p (get_alias_set (aggr_type
),
4114 get_alias_set (DR_REF (sdr
))))
4116 need_ref_all
= true;
4119 orig_stmt
= STMT_VINFO_GROUP_NEXT_ELEMENT (sinfo
);
4123 aggr_ptr_type
= build_pointer_type_for_mode (aggr_type
, ptr_mode
,
4125 aggr_ptr
= vect_get_new_vect_var (aggr_ptr_type
, vect_pointer_var
, base_name
);
4128 /* Note: If the dataref is in an inner-loop nested in LOOP, and we are
4129 vectorizing LOOP (i.e., outer-loop vectorization), we need to create two
4130 def-use update cycles for the pointer: one relative to the outer-loop
4131 (LOOP), which is what steps (3) and (4) below do. The other is relative
4132 to the inner-loop (which is the inner-most loop containing the dataref),
4133 and this is done be step (5) below.
4135 When vectorizing inner-most loops, the vectorized loop (LOOP) is also the
4136 inner-most loop, and so steps (3),(4) work the same, and step (5) is
4137 redundant. Steps (3),(4) create the following:
4140 LOOP: vp1 = phi(vp0,vp2)
4146 If there is an inner-loop nested in loop, then step (5) will also be
4147 applied, and an additional update in the inner-loop will be created:
4150 LOOP: vp1 = phi(vp0,vp2)
4152 inner: vp3 = phi(vp1,vp4)
4153 vp4 = vp3 + inner_step
4159 /* (2) Calculate the initial address of the aggregate-pointer, and set
4160 the aggregate-pointer to point to it before the loop. */
4162 /* Create: (&(base[init_val+offset]+byte_offset) in the loop preheader. */
4164 new_temp
= vect_create_addr_base_for_vector_ref (stmt
, &new_stmt_list
,
4165 offset
, loop
, byte_offset
);
4170 new_bb
= gsi_insert_seq_on_edge_immediate (pe
, new_stmt_list
);
4171 gcc_assert (!new_bb
);
4174 gsi_insert_seq_before (gsi
, new_stmt_list
, GSI_SAME_STMT
);
4177 *initial_address
= new_temp
;
4179 /* Create: p = (aggr_type *) initial_base */
4180 if (TREE_CODE (new_temp
) != SSA_NAME
4181 || !useless_type_conversion_p (aggr_ptr_type
, TREE_TYPE (new_temp
)))
4183 vec_stmt
= gimple_build_assign (aggr_ptr
,
4184 fold_convert (aggr_ptr_type
, new_temp
));
4185 aggr_ptr_init
= make_ssa_name (aggr_ptr
, vec_stmt
);
4186 /* Copy the points-to information if it exists. */
4187 if (DR_PTR_INFO (dr
))
4188 duplicate_ssa_name_ptr_info (aggr_ptr_init
, DR_PTR_INFO (dr
));
4189 gimple_assign_set_lhs (vec_stmt
, aggr_ptr_init
);
4192 new_bb
= gsi_insert_on_edge_immediate (pe
, vec_stmt
);
4193 gcc_assert (!new_bb
);
4196 gsi_insert_before (gsi
, vec_stmt
, GSI_SAME_STMT
);
4199 aggr_ptr_init
= new_temp
;
4201 /* (3) Handle the updating of the aggregate-pointer inside the loop.
4202 This is needed when ONLY_INIT is false, and also when AT_LOOP is the
4203 inner-loop nested in LOOP (during outer-loop vectorization). */
4205 /* No update in loop is required. */
4206 if (only_init
&& (!loop_vinfo
|| at_loop
== loop
))
4207 aptr
= aggr_ptr_init
;
4210 /* The step of the aggregate pointer is the type size. */
4211 tree iv_step
= TYPE_SIZE_UNIT (aggr_type
);
4212 /* One exception to the above is when the scalar step of the load in
4213 LOOP is zero. In this case the step here is also zero. */
4215 iv_step
= size_zero_node
;
4216 else if (tree_int_cst_sgn (step
) == -1)
4217 iv_step
= fold_build1 (NEGATE_EXPR
, TREE_TYPE (iv_step
), iv_step
);
4219 standard_iv_increment_position (loop
, &incr_gsi
, &insert_after
);
4221 create_iv (aggr_ptr_init
,
4222 fold_convert (aggr_ptr_type
, iv_step
),
4223 aggr_ptr
, loop
, &incr_gsi
, insert_after
,
4224 &indx_before_incr
, &indx_after_incr
);
4225 incr
= gsi_stmt (incr_gsi
);
4226 set_vinfo_for_stmt (incr
, new_stmt_vec_info (incr
, loop_vinfo
, NULL
));
4228 /* Copy the points-to information if it exists. */
4229 if (DR_PTR_INFO (dr
))
4231 duplicate_ssa_name_ptr_info (indx_before_incr
, DR_PTR_INFO (dr
));
4232 duplicate_ssa_name_ptr_info (indx_after_incr
, DR_PTR_INFO (dr
));
4237 aptr
= indx_before_incr
;
4240 if (!nested_in_vect_loop
|| only_init
)
4244 /* (4) Handle the updating of the aggregate-pointer inside the inner-loop
4245 nested in LOOP, if exists. */
4247 gcc_assert (nested_in_vect_loop
);
4250 standard_iv_increment_position (containing_loop
, &incr_gsi
,
4252 create_iv (aptr
, fold_convert (aggr_ptr_type
, DR_STEP (dr
)), aggr_ptr
,
4253 containing_loop
, &incr_gsi
, insert_after
, &indx_before_incr
,
4255 incr
= gsi_stmt (incr_gsi
);
4256 set_vinfo_for_stmt (incr
, new_stmt_vec_info (incr
, loop_vinfo
, NULL
));
4258 /* Copy the points-to information if it exists. */
4259 if (DR_PTR_INFO (dr
))
4261 duplicate_ssa_name_ptr_info (indx_before_incr
, DR_PTR_INFO (dr
));
4262 duplicate_ssa_name_ptr_info (indx_after_incr
, DR_PTR_INFO (dr
));
4267 return indx_before_incr
;
4274 /* Function bump_vector_ptr
4276 Increment a pointer (to a vector type) by vector-size. If requested,
4277 i.e. if PTR-INCR is given, then also connect the new increment stmt
4278 to the existing def-use update-chain of the pointer, by modifying
4279 the PTR_INCR as illustrated below:
4281 The pointer def-use update-chain before this function:
4282 DATAREF_PTR = phi (p_0, p_2)
4284 PTR_INCR: p_2 = DATAREF_PTR + step
4286 The pointer def-use update-chain after this function:
4287 DATAREF_PTR = phi (p_0, p_2)
4289 NEW_DATAREF_PTR = DATAREF_PTR + BUMP
4291 PTR_INCR: p_2 = NEW_DATAREF_PTR + step
4294 DATAREF_PTR - ssa_name of a pointer (to vector type) that is being updated
4296 PTR_INCR - optional. The stmt that updates the pointer in each iteration of
4297 the loop. The increment amount across iterations is expected
4299 BSI - location where the new update stmt is to be placed.
4300 STMT - the original scalar memory-access stmt that is being vectorized.
4301 BUMP - optional. The offset by which to bump the pointer. If not given,
4302 the offset is assumed to be vector_size.
4304 Output: Return NEW_DATAREF_PTR as illustrated above.
4309 bump_vector_ptr (tree dataref_ptr
, gimple ptr_incr
, gimple_stmt_iterator
*gsi
,
4310 gimple stmt
, tree bump
)
4312 stmt_vec_info stmt_info
= vinfo_for_stmt (stmt
);
4313 struct data_reference
*dr
= STMT_VINFO_DATA_REF (stmt_info
);
4314 tree vectype
= STMT_VINFO_VECTYPE (stmt_info
);
4315 tree update
= TYPE_SIZE_UNIT (vectype
);
4318 use_operand_p use_p
;
4319 tree new_dataref_ptr
;
4324 new_dataref_ptr
= copy_ssa_name (dataref_ptr
, NULL
);
4325 incr_stmt
= gimple_build_assign_with_ops (POINTER_PLUS_EXPR
, new_dataref_ptr
,
4326 dataref_ptr
, update
);
4327 vect_finish_stmt_generation (stmt
, incr_stmt
, gsi
);
4329 /* Copy the points-to information if it exists. */
4330 if (DR_PTR_INFO (dr
))
4332 duplicate_ssa_name_ptr_info (new_dataref_ptr
, DR_PTR_INFO (dr
));
4333 mark_ptr_info_alignment_unknown (SSA_NAME_PTR_INFO (new_dataref_ptr
));
4337 return new_dataref_ptr
;
4339 /* Update the vector-pointer's cross-iteration increment. */
4340 FOR_EACH_SSA_USE_OPERAND (use_p
, ptr_incr
, iter
, SSA_OP_USE
)
4342 tree use
= USE_FROM_PTR (use_p
);
4344 if (use
== dataref_ptr
)
4345 SET_USE (use_p
, new_dataref_ptr
);
4347 gcc_assert (tree_int_cst_compare (use
, update
) == 0);
4350 return new_dataref_ptr
;
4354 /* Function vect_create_destination_var.
4356 Create a new temporary of type VECTYPE. */
4359 vect_create_destination_var (tree scalar_dest
, tree vectype
)
4365 enum vect_var_kind kind
;
4367 kind
= vectype
? vect_simple_var
: vect_scalar_var
;
4368 type
= vectype
? vectype
: TREE_TYPE (scalar_dest
);
4370 gcc_assert (TREE_CODE (scalar_dest
) == SSA_NAME
);
4372 name
= get_name (scalar_dest
);
4374 asprintf (&new_name
, "%s_%u", name
, SSA_NAME_VERSION (scalar_dest
));
4376 asprintf (&new_name
, "_%u", SSA_NAME_VERSION (scalar_dest
));
4377 vec_dest
= vect_get_new_vect_var (type
, kind
, new_name
);
4383 /* Function vect_grouped_store_supported.
4385 Returns TRUE if interleave high and interleave low permutations
4386 are supported, and FALSE otherwise. */
4389 vect_grouped_store_supported (tree vectype
, unsigned HOST_WIDE_INT count
)
4391 enum machine_mode mode
= TYPE_MODE (vectype
);
4393 /* vect_permute_store_chain requires the group size to be a power of two. */
4394 if (exact_log2 (count
) == -1)
4396 if (dump_enabled_p ())
4397 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
4398 "the size of the group of accesses"
4399 " is not a power of 2\n");
4403 /* Check that the permutation is supported. */
4404 if (VECTOR_MODE_P (mode
))
4406 unsigned int i
, nelt
= GET_MODE_NUNITS (mode
);
4407 unsigned char *sel
= XALLOCAVEC (unsigned char, nelt
);
4408 for (i
= 0; i
< nelt
/ 2; i
++)
4411 sel
[i
* 2 + 1] = i
+ nelt
;
4413 if (can_vec_perm_p (mode
, false, sel
))
4415 for (i
= 0; i
< nelt
; i
++)
4417 if (can_vec_perm_p (mode
, false, sel
))
4422 if (dump_enabled_p ())
4423 dump_printf (MSG_MISSED_OPTIMIZATION
,
4424 "interleave op not supported by target.\n");
4429 /* Return TRUE if vec_store_lanes is available for COUNT vectors of
4433 vect_store_lanes_supported (tree vectype
, unsigned HOST_WIDE_INT count
)
4435 return vect_lanes_optab_supported_p ("vec_store_lanes",
4436 vec_store_lanes_optab
,
4441 /* Function vect_permute_store_chain.
4443 Given a chain of interleaved stores in DR_CHAIN of LENGTH that must be
4444 a power of 2, generate interleave_high/low stmts to reorder the data
4445 correctly for the stores. Return the final references for stores in
4448 E.g., LENGTH is 4 and the scalar type is short, i.e., VF is 8.
4449 The input is 4 vectors each containing 8 elements. We assign a number to
4450 each element, the input sequence is:
4452 1st vec: 0 1 2 3 4 5 6 7
4453 2nd vec: 8 9 10 11 12 13 14 15
4454 3rd vec: 16 17 18 19 20 21 22 23
4455 4th vec: 24 25 26 27 28 29 30 31
4457 The output sequence should be:
4459 1st vec: 0 8 16 24 1 9 17 25
4460 2nd vec: 2 10 18 26 3 11 19 27
4461 3rd vec: 4 12 20 28 5 13 21 30
4462 4th vec: 6 14 22 30 7 15 23 31
4464 i.e., we interleave the contents of the four vectors in their order.
4466 We use interleave_high/low instructions to create such output. The input of
4467 each interleave_high/low operation is two vectors:
4470 the even elements of the result vector are obtained left-to-right from the
4471 high/low elements of the first vector. The odd elements of the result are
4472 obtained left-to-right from the high/low elements of the second vector.
4473 The output of interleave_high will be: 0 4 1 5
4474 and of interleave_low: 2 6 3 7
4477 The permutation is done in log LENGTH stages. In each stage interleave_high
4478 and interleave_low stmts are created for each pair of vectors in DR_CHAIN,
4479 where the first argument is taken from the first half of DR_CHAIN and the
4480 second argument from it's second half.
4483 I1: interleave_high (1st vec, 3rd vec)
4484 I2: interleave_low (1st vec, 3rd vec)
4485 I3: interleave_high (2nd vec, 4th vec)
4486 I4: interleave_low (2nd vec, 4th vec)
4488 The output for the first stage is:
4490 I1: 0 16 1 17 2 18 3 19
4491 I2: 4 20 5 21 6 22 7 23
4492 I3: 8 24 9 25 10 26 11 27
4493 I4: 12 28 13 29 14 30 15 31
4495 The output of the second stage, i.e. the final result is:
4497 I1: 0 8 16 24 1 9 17 25
4498 I2: 2 10 18 26 3 11 19 27
4499 I3: 4 12 20 28 5 13 21 30
4500 I4: 6 14 22 30 7 15 23 31. */
4503 vect_permute_store_chain (vec
<tree
> dr_chain
,
4504 unsigned int length
,
4506 gimple_stmt_iterator
*gsi
,
4507 vec
<tree
> *result_chain
)
4509 tree vect1
, vect2
, high
, low
;
4511 tree vectype
= STMT_VINFO_VECTYPE (vinfo_for_stmt (stmt
));
4512 tree perm_mask_low
, perm_mask_high
;
4514 unsigned int j
, nelt
= TYPE_VECTOR_SUBPARTS (vectype
);
4515 unsigned char *sel
= XALLOCAVEC (unsigned char, nelt
);
4517 result_chain
->quick_grow (length
);
4518 memcpy (result_chain
->address (), dr_chain
.address (),
4519 length
* sizeof (tree
));
4521 for (i
= 0, n
= nelt
/ 2; i
< n
; i
++)
4524 sel
[i
* 2 + 1] = i
+ nelt
;
4526 perm_mask_high
= vect_gen_perm_mask (vectype
, sel
);
4527 gcc_assert (perm_mask_high
!= NULL
);
4529 for (i
= 0; i
< nelt
; i
++)
4531 perm_mask_low
= vect_gen_perm_mask (vectype
, sel
);
4532 gcc_assert (perm_mask_low
!= NULL
);
4534 for (i
= 0, n
= exact_log2 (length
); i
< n
; i
++)
4536 for (j
= 0; j
< length
/2; j
++)
4538 vect1
= dr_chain
[j
];
4539 vect2
= dr_chain
[j
+length
/2];
4541 /* Create interleaving stmt:
4542 high = VEC_PERM_EXPR <vect1, vect2, {0, nelt, 1, nelt+1, ...}> */
4543 high
= make_temp_ssa_name (vectype
, NULL
, "vect_inter_high");
4545 = gimple_build_assign_with_ops (VEC_PERM_EXPR
, high
,
4546 vect1
, vect2
, perm_mask_high
);
4547 vect_finish_stmt_generation (stmt
, perm_stmt
, gsi
);
4548 (*result_chain
)[2*j
] = high
;
4550 /* Create interleaving stmt:
4551 low = VEC_PERM_EXPR <vect1, vect2, {nelt/2, nelt*3/2, nelt/2+1,
4552 nelt*3/2+1, ...}> */
4553 low
= make_temp_ssa_name (vectype
, NULL
, "vect_inter_low");
4555 = gimple_build_assign_with_ops (VEC_PERM_EXPR
, low
,
4556 vect1
, vect2
, perm_mask_low
);
4557 vect_finish_stmt_generation (stmt
, perm_stmt
, gsi
);
4558 (*result_chain
)[2*j
+1] = low
;
4560 memcpy (dr_chain
.address (), result_chain
->address (),
4561 length
* sizeof (tree
));
4565 /* Function vect_setup_realignment
4567 This function is called when vectorizing an unaligned load using
4568 the dr_explicit_realign[_optimized] scheme.
4569 This function generates the following code at the loop prolog:
4572 x msq_init = *(floor(p)); # prolog load
4573 realignment_token = call target_builtin;
4575 x msq = phi (msq_init, ---)
4577 The stmts marked with x are generated only for the case of
4578 dr_explicit_realign_optimized.
4580 The code above sets up a new (vector) pointer, pointing to the first
4581 location accessed by STMT, and a "floor-aligned" load using that pointer.
4582 It also generates code to compute the "realignment-token" (if the relevant
4583 target hook was defined), and creates a phi-node at the loop-header bb
4584 whose arguments are the result of the prolog-load (created by this
4585 function) and the result of a load that takes place in the loop (to be
4586 created by the caller to this function).
4588 For the case of dr_explicit_realign_optimized:
4589 The caller to this function uses the phi-result (msq) to create the
4590 realignment code inside the loop, and sets up the missing phi argument,
4593 msq = phi (msq_init, lsq)
4594 lsq = *(floor(p')); # load in loop
4595 result = realign_load (msq, lsq, realignment_token);
4597 For the case of dr_explicit_realign:
4599 msq = *(floor(p)); # load in loop
4601 lsq = *(floor(p')); # load in loop
4602 result = realign_load (msq, lsq, realignment_token);
4605 STMT - (scalar) load stmt to be vectorized. This load accesses
4606 a memory location that may be unaligned.
4607 BSI - place where new code is to be inserted.
4608 ALIGNMENT_SUPPORT_SCHEME - which of the two misalignment handling schemes
4612 REALIGNMENT_TOKEN - the result of a call to the builtin_mask_for_load
4613 target hook, if defined.
4614 Return value - the result of the loop-header phi node. */
4617 vect_setup_realignment (gimple stmt
, gimple_stmt_iterator
*gsi
,
4618 tree
*realignment_token
,
4619 enum dr_alignment_support alignment_support_scheme
,
4621 struct loop
**at_loop
)
4623 stmt_vec_info stmt_info
= vinfo_for_stmt (stmt
);
4624 tree vectype
= STMT_VINFO_VECTYPE (stmt_info
);
4625 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_info
);
4626 struct data_reference
*dr
= STMT_VINFO_DATA_REF (stmt_info
);
4627 struct loop
*loop
= NULL
;
4629 tree scalar_dest
= gimple_assign_lhs (stmt
);
4636 tree msq_init
= NULL_TREE
;
4639 tree msq
= NULL_TREE
;
4640 gimple_seq stmts
= NULL
;
4642 bool compute_in_loop
= false;
4643 bool nested_in_vect_loop
= false;
4644 struct loop
*containing_loop
= (gimple_bb (stmt
))->loop_father
;
4645 struct loop
*loop_for_initial_load
= NULL
;
4649 loop
= LOOP_VINFO_LOOP (loop_vinfo
);
4650 nested_in_vect_loop
= nested_in_vect_loop_p (loop
, stmt
);
4653 gcc_assert (alignment_support_scheme
== dr_explicit_realign
4654 || alignment_support_scheme
== dr_explicit_realign_optimized
);
4656 /* We need to generate three things:
4657 1. the misalignment computation
4658 2. the extra vector load (for the optimized realignment scheme).
4659 3. the phi node for the two vectors from which the realignment is
4660 done (for the optimized realignment scheme). */
4662 /* 1. Determine where to generate the misalignment computation.
4664 If INIT_ADDR is NULL_TREE, this indicates that the misalignment
4665 calculation will be generated by this function, outside the loop (in the
4666 preheader). Otherwise, INIT_ADDR had already been computed for us by the
4667 caller, inside the loop.
4669 Background: If the misalignment remains fixed throughout the iterations of
4670 the loop, then both realignment schemes are applicable, and also the
4671 misalignment computation can be done outside LOOP. This is because we are
4672 vectorizing LOOP, and so the memory accesses in LOOP advance in steps that
4673 are a multiple of VS (the Vector Size), and therefore the misalignment in
4674 different vectorized LOOP iterations is always the same.
4675 The problem arises only if the memory access is in an inner-loop nested
4676 inside LOOP, which is now being vectorized using outer-loop vectorization.
4677 This is the only case when the misalignment of the memory access may not
4678 remain fixed throughout the iterations of the inner-loop (as explained in
4679 detail in vect_supportable_dr_alignment). In this case, not only is the
4680 optimized realignment scheme not applicable, but also the misalignment
4681 computation (and generation of the realignment token that is passed to
4682 REALIGN_LOAD) have to be done inside the loop.
4684 In short, INIT_ADDR indicates whether we are in a COMPUTE_IN_LOOP mode
4685 or not, which in turn determines if the misalignment is computed inside
4686 the inner-loop, or outside LOOP. */
4688 if (init_addr
!= NULL_TREE
|| !loop_vinfo
)
4690 compute_in_loop
= true;
4691 gcc_assert (alignment_support_scheme
== dr_explicit_realign
);
4695 /* 2. Determine where to generate the extra vector load.
4697 For the optimized realignment scheme, instead of generating two vector
4698 loads in each iteration, we generate a single extra vector load in the
4699 preheader of the loop, and in each iteration reuse the result of the
4700 vector load from the previous iteration. In case the memory access is in
4701 an inner-loop nested inside LOOP, which is now being vectorized using
4702 outer-loop vectorization, we need to determine whether this initial vector
4703 load should be generated at the preheader of the inner-loop, or can be
4704 generated at the preheader of LOOP. If the memory access has no evolution
4705 in LOOP, it can be generated in the preheader of LOOP. Otherwise, it has
4706 to be generated inside LOOP (in the preheader of the inner-loop). */
4708 if (nested_in_vect_loop
)
4710 tree outerloop_step
= STMT_VINFO_DR_STEP (stmt_info
);
4711 bool invariant_in_outerloop
=
4712 (tree_int_cst_compare (outerloop_step
, size_zero_node
) == 0);
4713 loop_for_initial_load
= (invariant_in_outerloop
? loop
: loop
->inner
);
4716 loop_for_initial_load
= loop
;
4718 *at_loop
= loop_for_initial_load
;
4720 if (loop_for_initial_load
)
4721 pe
= loop_preheader_edge (loop_for_initial_load
);
4723 /* 3. For the case of the optimized realignment, create the first vector
4724 load at the loop preheader. */
4726 if (alignment_support_scheme
== dr_explicit_realign_optimized
)
4728 /* Create msq_init = *(floor(p1)) in the loop preheader */
4730 gcc_assert (!compute_in_loop
);
4731 vec_dest
= vect_create_destination_var (scalar_dest
, vectype
);
4732 ptr
= vect_create_data_ref_ptr (stmt
, vectype
, loop_for_initial_load
,
4733 NULL_TREE
, &init_addr
, NULL
, &inc
,
4735 new_temp
= copy_ssa_name (ptr
, NULL
);
4736 new_stmt
= gimple_build_assign_with_ops
4737 (BIT_AND_EXPR
, new_temp
, ptr
,
4738 build_int_cst (TREE_TYPE (ptr
),
4739 -(HOST_WIDE_INT
)TYPE_ALIGN_UNIT (vectype
)));
4740 new_bb
= gsi_insert_on_edge_immediate (pe
, new_stmt
);
4741 gcc_assert (!new_bb
);
4743 = build2 (MEM_REF
, TREE_TYPE (vec_dest
), new_temp
,
4744 build_int_cst (reference_alias_ptr_type (DR_REF (dr
)), 0));
4745 new_stmt
= gimple_build_assign (vec_dest
, data_ref
);
4746 new_temp
= make_ssa_name (vec_dest
, new_stmt
);
4747 gimple_assign_set_lhs (new_stmt
, new_temp
);
4750 new_bb
= gsi_insert_on_edge_immediate (pe
, new_stmt
);
4751 gcc_assert (!new_bb
);
4754 gsi_insert_before (gsi
, new_stmt
, GSI_SAME_STMT
);
4756 msq_init
= gimple_assign_lhs (new_stmt
);
4759 /* 4. Create realignment token using a target builtin, if available.
4760 It is done either inside the containing loop, or before LOOP (as
4761 determined above). */
4763 if (targetm
.vectorize
.builtin_mask_for_load
)
4767 /* Compute INIT_ADDR - the initial addressed accessed by this memref. */
4770 /* Generate the INIT_ADDR computation outside LOOP. */
4771 init_addr
= vect_create_addr_base_for_vector_ref (stmt
, &stmts
,
4775 pe
= loop_preheader_edge (loop
);
4776 new_bb
= gsi_insert_seq_on_edge_immediate (pe
, stmts
);
4777 gcc_assert (!new_bb
);
4780 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
4783 builtin_decl
= targetm
.vectorize
.builtin_mask_for_load ();
4784 new_stmt
= gimple_build_call (builtin_decl
, 1, init_addr
);
4786 vect_create_destination_var (scalar_dest
,
4787 gimple_call_return_type (new_stmt
));
4788 new_temp
= make_ssa_name (vec_dest
, new_stmt
);
4789 gimple_call_set_lhs (new_stmt
, new_temp
);
4791 if (compute_in_loop
)
4792 gsi_insert_before (gsi
, new_stmt
, GSI_SAME_STMT
);
4795 /* Generate the misalignment computation outside LOOP. */
4796 pe
= loop_preheader_edge (loop
);
4797 new_bb
= gsi_insert_on_edge_immediate (pe
, new_stmt
);
4798 gcc_assert (!new_bb
);
4801 *realignment_token
= gimple_call_lhs (new_stmt
);
4803 /* The result of the CALL_EXPR to this builtin is determined from
4804 the value of the parameter and no global variables are touched
4805 which makes the builtin a "const" function. Requiring the
4806 builtin to have the "const" attribute makes it unnecessary
4807 to call mark_call_clobbered. */
4808 gcc_assert (TREE_READONLY (builtin_decl
));
4811 if (alignment_support_scheme
== dr_explicit_realign
)
4814 gcc_assert (!compute_in_loop
);
4815 gcc_assert (alignment_support_scheme
== dr_explicit_realign_optimized
);
4818 /* 5. Create msq = phi <msq_init, lsq> in loop */
4820 pe
= loop_preheader_edge (containing_loop
);
4821 vec_dest
= vect_create_destination_var (scalar_dest
, vectype
);
4822 msq
= make_ssa_name (vec_dest
, NULL
);
4823 phi_stmt
= create_phi_node (msq
, containing_loop
->header
);
4824 add_phi_arg (phi_stmt
, msq_init
, pe
, UNKNOWN_LOCATION
);
4830 /* Function vect_grouped_load_supported.
4832 Returns TRUE if even and odd permutations are supported,
4833 and FALSE otherwise. */
4836 vect_grouped_load_supported (tree vectype
, unsigned HOST_WIDE_INT count
)
4838 enum machine_mode mode
= TYPE_MODE (vectype
);
4840 /* vect_permute_load_chain requires the group size to be a power of two. */
4841 if (exact_log2 (count
) == -1)
4843 if (dump_enabled_p ())
4844 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
4845 "the size of the group of accesses"
4846 " is not a power of 2\n");
4850 /* Check that the permutation is supported. */
4851 if (VECTOR_MODE_P (mode
))
4853 unsigned int i
, nelt
= GET_MODE_NUNITS (mode
);
4854 unsigned char *sel
= XALLOCAVEC (unsigned char, nelt
);
4856 for (i
= 0; i
< nelt
; i
++)
4858 if (can_vec_perm_p (mode
, false, sel
))
4860 for (i
= 0; i
< nelt
; i
++)
4862 if (can_vec_perm_p (mode
, false, sel
))
4867 if (dump_enabled_p ())
4868 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
4869 "extract even/odd not supported by target\n");
4873 /* Return TRUE if vec_load_lanes is available for COUNT vectors of
4877 vect_load_lanes_supported (tree vectype
, unsigned HOST_WIDE_INT count
)
4879 return vect_lanes_optab_supported_p ("vec_load_lanes",
4880 vec_load_lanes_optab
,
4884 /* Function vect_permute_load_chain.
4886 Given a chain of interleaved loads in DR_CHAIN of LENGTH that must be
4887 a power of 2, generate extract_even/odd stmts to reorder the input data
4888 correctly. Return the final references for loads in RESULT_CHAIN.
4890 E.g., LENGTH is 4 and the scalar type is short, i.e., VF is 8.
4891 The input is 4 vectors each containing 8 elements. We assign a number to each
4892 element, the input sequence is:
4894 1st vec: 0 1 2 3 4 5 6 7
4895 2nd vec: 8 9 10 11 12 13 14 15
4896 3rd vec: 16 17 18 19 20 21 22 23
4897 4th vec: 24 25 26 27 28 29 30 31
4899 The output sequence should be:
4901 1st vec: 0 4 8 12 16 20 24 28
4902 2nd vec: 1 5 9 13 17 21 25 29
4903 3rd vec: 2 6 10 14 18 22 26 30
4904 4th vec: 3 7 11 15 19 23 27 31
4906 i.e., the first output vector should contain the first elements of each
4907 interleaving group, etc.
4909 We use extract_even/odd instructions to create such output. The input of
4910 each extract_even/odd operation is two vectors
4914 and the output is the vector of extracted even/odd elements. The output of
4915 extract_even will be: 0 2 4 6
4916 and of extract_odd: 1 3 5 7
4919 The permutation is done in log LENGTH stages. In each stage extract_even
4920 and extract_odd stmts are created for each pair of vectors in DR_CHAIN in
4921 their order. In our example,
4923 E1: extract_even (1st vec, 2nd vec)
4924 E2: extract_odd (1st vec, 2nd vec)
4925 E3: extract_even (3rd vec, 4th vec)
4926 E4: extract_odd (3rd vec, 4th vec)
4928 The output for the first stage will be:
4930 E1: 0 2 4 6 8 10 12 14
4931 E2: 1 3 5 7 9 11 13 15
4932 E3: 16 18 20 22 24 26 28 30
4933 E4: 17 19 21 23 25 27 29 31
4935 In order to proceed and create the correct sequence for the next stage (or
4936 for the correct output, if the second stage is the last one, as in our
4937 example), we first put the output of extract_even operation and then the
4938 output of extract_odd in RESULT_CHAIN (which is then copied to DR_CHAIN).
4939 The input for the second stage is:
4941 1st vec (E1): 0 2 4 6 8 10 12 14
4942 2nd vec (E3): 16 18 20 22 24 26 28 30
4943 3rd vec (E2): 1 3 5 7 9 11 13 15
4944 4th vec (E4): 17 19 21 23 25 27 29 31
4946 The output of the second stage:
4948 E1: 0 4 8 12 16 20 24 28
4949 E2: 2 6 10 14 18 22 26 30
4950 E3: 1 5 9 13 17 21 25 29
4951 E4: 3 7 11 15 19 23 27 31
4953 And RESULT_CHAIN after reordering:
4955 1st vec (E1): 0 4 8 12 16 20 24 28
4956 2nd vec (E3): 1 5 9 13 17 21 25 29
4957 3rd vec (E2): 2 6 10 14 18 22 26 30
4958 4th vec (E4): 3 7 11 15 19 23 27 31. */
4961 vect_permute_load_chain (vec
<tree
> dr_chain
,
4962 unsigned int length
,
4964 gimple_stmt_iterator
*gsi
,
4965 vec
<tree
> *result_chain
)
4967 tree data_ref
, first_vect
, second_vect
;
4968 tree perm_mask_even
, perm_mask_odd
;
4970 tree vectype
= STMT_VINFO_VECTYPE (vinfo_for_stmt (stmt
));
4971 unsigned int i
, j
, log_length
= exact_log2 (length
);
4972 unsigned nelt
= TYPE_VECTOR_SUBPARTS (vectype
);
4973 unsigned char *sel
= XALLOCAVEC (unsigned char, nelt
);
4975 result_chain
->quick_grow (length
);
4976 memcpy (result_chain
->address (), dr_chain
.address (),
4977 length
* sizeof (tree
));
4979 for (i
= 0; i
< nelt
; ++i
)
4981 perm_mask_even
= vect_gen_perm_mask (vectype
, sel
);
4982 gcc_assert (perm_mask_even
!= NULL
);
4984 for (i
= 0; i
< nelt
; ++i
)
4986 perm_mask_odd
= vect_gen_perm_mask (vectype
, sel
);
4987 gcc_assert (perm_mask_odd
!= NULL
);
4989 for (i
= 0; i
< log_length
; i
++)
4991 for (j
= 0; j
< length
; j
+= 2)
4993 first_vect
= dr_chain
[j
];
4994 second_vect
= dr_chain
[j
+1];
4996 /* data_ref = permute_even (first_data_ref, second_data_ref); */
4997 data_ref
= make_temp_ssa_name (vectype
, NULL
, "vect_perm_even");
4998 perm_stmt
= gimple_build_assign_with_ops (VEC_PERM_EXPR
, data_ref
,
4999 first_vect
, second_vect
,
5001 vect_finish_stmt_generation (stmt
, perm_stmt
, gsi
);
5002 (*result_chain
)[j
/2] = data_ref
;
5004 /* data_ref = permute_odd (first_data_ref, second_data_ref); */
5005 data_ref
= make_temp_ssa_name (vectype
, NULL
, "vect_perm_odd");
5006 perm_stmt
= gimple_build_assign_with_ops (VEC_PERM_EXPR
, data_ref
,
5007 first_vect
, second_vect
,
5009 vect_finish_stmt_generation (stmt
, perm_stmt
, gsi
);
5010 (*result_chain
)[j
/2+length
/2] = data_ref
;
5012 memcpy (dr_chain
.address (), result_chain
->address (),
5013 length
* sizeof (tree
));
5018 /* Function vect_transform_grouped_load.
5020 Given a chain of input interleaved data-refs (in DR_CHAIN), build statements
5021 to perform their permutation and ascribe the result vectorized statements to
5022 the scalar statements.
5026 vect_transform_grouped_load (gimple stmt
, vec
<tree
> dr_chain
, int size
,
5027 gimple_stmt_iterator
*gsi
)
5029 vec
<tree
> result_chain
= vNULL
;
5031 /* DR_CHAIN contains input data-refs that are a part of the interleaving.
5032 RESULT_CHAIN is the output of vect_permute_load_chain, it contains permuted
5033 vectors, that are ready for vector computation. */
5034 result_chain
.create (size
);
5035 vect_permute_load_chain (dr_chain
, size
, stmt
, gsi
, &result_chain
);
5036 vect_record_grouped_load_vectors (stmt
, result_chain
);
5037 result_chain
.release ();
5040 /* RESULT_CHAIN contains the output of a group of grouped loads that were
5041 generated as part of the vectorization of STMT. Assign the statement
5042 for each vector to the associated scalar statement. */
5045 vect_record_grouped_load_vectors (gimple stmt
, vec
<tree
> result_chain
)
5047 gimple first_stmt
= GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt
));
5048 gimple next_stmt
, new_stmt
;
5049 unsigned int i
, gap_count
;
5052 /* Put a permuted data-ref in the VECTORIZED_STMT field.
5053 Since we scan the chain starting from it's first node, their order
5054 corresponds the order of data-refs in RESULT_CHAIN. */
5055 next_stmt
= first_stmt
;
5057 FOR_EACH_VEC_ELT (result_chain
, i
, tmp_data_ref
)
5062 /* Skip the gaps. Loads created for the gaps will be removed by dead
5063 code elimination pass later. No need to check for the first stmt in
5064 the group, since it always exists.
5065 GROUP_GAP is the number of steps in elements from the previous
5066 access (if there is no gap GROUP_GAP is 1). We skip loads that
5067 correspond to the gaps. */
5068 if (next_stmt
!= first_stmt
5069 && gap_count
< GROUP_GAP (vinfo_for_stmt (next_stmt
)))
5077 new_stmt
= SSA_NAME_DEF_STMT (tmp_data_ref
);
5078 /* We assume that if VEC_STMT is not NULL, this is a case of multiple
5079 copies, and we put the new vector statement in the first available
5081 if (!STMT_VINFO_VEC_STMT (vinfo_for_stmt (next_stmt
)))
5082 STMT_VINFO_VEC_STMT (vinfo_for_stmt (next_stmt
)) = new_stmt
;
5085 if (!GROUP_SAME_DR_STMT (vinfo_for_stmt (next_stmt
)))
5088 STMT_VINFO_VEC_STMT (vinfo_for_stmt (next_stmt
));
5090 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (prev_stmt
));
5093 prev_stmt
= rel_stmt
;
5095 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (rel_stmt
));
5098 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (prev_stmt
)) =
5103 next_stmt
= GROUP_NEXT_ELEMENT (vinfo_for_stmt (next_stmt
));
5105 /* If NEXT_STMT accesses the same DR as the previous statement,
5106 put the same TMP_DATA_REF as its vectorized statement; otherwise
5107 get the next data-ref from RESULT_CHAIN. */
5108 if (!next_stmt
|| !GROUP_SAME_DR_STMT (vinfo_for_stmt (next_stmt
)))
5114 /* Function vect_force_dr_alignment_p.
5116 Returns whether the alignment of a DECL can be forced to be aligned
5117 on ALIGNMENT bit boundary. */
5120 vect_can_force_dr_alignment_p (const_tree decl
, unsigned int alignment
)
5122 if (TREE_CODE (decl
) != VAR_DECL
)
5125 /* We cannot change alignment of common or external symbols as another
5126 translation unit may contain a definition with lower alignment.
5127 The rules of common symbol linking mean that the definition
5128 will override the common symbol. The same is true for constant
5129 pool entries which may be shared and are not properly merged
5131 if (DECL_EXTERNAL (decl
)
5132 || DECL_COMMON (decl
)
5133 || DECL_IN_CONSTANT_POOL (decl
))
5136 if (TREE_ASM_WRITTEN (decl
))
5139 /* Do not override the alignment as specified by the ABI when the used
5140 attribute is set. */
5141 if (DECL_PRESERVE_P (decl
))
5144 /* Do not override explicit alignment set by the user when an explicit
5145 section name is also used. This is a common idiom used by many
5146 software projects. */
5147 if (DECL_SECTION_NAME (decl
) != NULL_TREE
5148 && !DECL_HAS_IMPLICIT_SECTION_NAME_P (decl
))
5151 if (TREE_STATIC (decl
))
5152 return (alignment
<= MAX_OFILE_ALIGNMENT
);
5154 return (alignment
<= MAX_STACK_ALIGNMENT
);
5158 /* Return whether the data reference DR is supported with respect to its
5160 If CHECK_ALIGNED_ACCESSES is TRUE, check if the access is supported even
5161 it is aligned, i.e., check if it is possible to vectorize it with different
5164 enum dr_alignment_support
5165 vect_supportable_dr_alignment (struct data_reference
*dr
,
5166 bool check_aligned_accesses
)
5168 gimple stmt
= DR_STMT (dr
);
5169 stmt_vec_info stmt_info
= vinfo_for_stmt (stmt
);
5170 tree vectype
= STMT_VINFO_VECTYPE (stmt_info
);
5171 enum machine_mode mode
= TYPE_MODE (vectype
);
5172 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_info
);
5173 struct loop
*vect_loop
= NULL
;
5174 bool nested_in_vect_loop
= false;
5176 if (aligned_access_p (dr
) && !check_aligned_accesses
)
5179 /* For now assume all conditional loads/stores support unaligned
5180 access without any special code. */
5181 if (is_gimple_call (stmt
)
5182 && gimple_call_internal_p (stmt
)
5183 && (gimple_call_internal_fn (stmt
) == IFN_MASK_LOAD
5184 || gimple_call_internal_fn (stmt
) == IFN_MASK_STORE
))
5185 return dr_unaligned_supported
;
5189 vect_loop
= LOOP_VINFO_LOOP (loop_vinfo
);
5190 nested_in_vect_loop
= nested_in_vect_loop_p (vect_loop
, stmt
);
5193 /* Possibly unaligned access. */
5195 /* We can choose between using the implicit realignment scheme (generating
5196 a misaligned_move stmt) and the explicit realignment scheme (generating
5197 aligned loads with a REALIGN_LOAD). There are two variants to the
5198 explicit realignment scheme: optimized, and unoptimized.
5199 We can optimize the realignment only if the step between consecutive
5200 vector loads is equal to the vector size. Since the vector memory
5201 accesses advance in steps of VS (Vector Size) in the vectorized loop, it
5202 is guaranteed that the misalignment amount remains the same throughout the
5203 execution of the vectorized loop. Therefore, we can create the
5204 "realignment token" (the permutation mask that is passed to REALIGN_LOAD)
5205 at the loop preheader.
5207 However, in the case of outer-loop vectorization, when vectorizing a
5208 memory access in the inner-loop nested within the LOOP that is now being
5209 vectorized, while it is guaranteed that the misalignment of the
5210 vectorized memory access will remain the same in different outer-loop
5211 iterations, it is *not* guaranteed that is will remain the same throughout
5212 the execution of the inner-loop. This is because the inner-loop advances
5213 with the original scalar step (and not in steps of VS). If the inner-loop
5214 step happens to be a multiple of VS, then the misalignment remains fixed
5215 and we can use the optimized realignment scheme. For example:
5221 When vectorizing the i-loop in the above example, the step between
5222 consecutive vector loads is 1, and so the misalignment does not remain
5223 fixed across the execution of the inner-loop, and the realignment cannot
5224 be optimized (as illustrated in the following pseudo vectorized loop):
5226 for (i=0; i<N; i+=4)
5227 for (j=0; j<M; j++){
5228 vs += vp[i+j]; // misalignment of &vp[i+j] is {0,1,2,3,0,1,2,3,...}
5229 // when j is {0,1,2,3,4,5,6,7,...} respectively.
5230 // (assuming that we start from an aligned address).
5233 We therefore have to use the unoptimized realignment scheme:
5235 for (i=0; i<N; i+=4)
5236 for (j=k; j<M; j+=4)
5237 vs += vp[i+j]; // misalignment of &vp[i+j] is always k (assuming
5238 // that the misalignment of the initial address is
5241 The loop can then be vectorized as follows:
5243 for (k=0; k<4; k++){
5244 rt = get_realignment_token (&vp[k]);
5245 for (i=0; i<N; i+=4){
5247 for (j=k; j<M; j+=4){
5249 va = REALIGN_LOAD <v1,v2,rt>;
5256 if (DR_IS_READ (dr
))
5258 bool is_packed
= false;
5259 tree type
= (TREE_TYPE (DR_REF (dr
)));
5261 if (optab_handler (vec_realign_load_optab
, mode
) != CODE_FOR_nothing
5262 && (!targetm
.vectorize
.builtin_mask_for_load
5263 || targetm
.vectorize
.builtin_mask_for_load ()))
5265 tree vectype
= STMT_VINFO_VECTYPE (stmt_info
);
5266 if ((nested_in_vect_loop
5267 && (TREE_INT_CST_LOW (DR_STEP (dr
))
5268 != GET_MODE_SIZE (TYPE_MODE (vectype
))))
5270 return dr_explicit_realign
;
5272 return dr_explicit_realign_optimized
;
5274 if (!known_alignment_for_access_p (dr
))
5275 is_packed
= not_size_aligned (DR_REF (dr
));
5277 if ((TYPE_USER_ALIGN (type
) && !is_packed
)
5278 || targetm
.vectorize
.
5279 support_vector_misalignment (mode
, type
,
5280 DR_MISALIGNMENT (dr
), is_packed
))
5281 /* Can't software pipeline the loads, but can at least do them. */
5282 return dr_unaligned_supported
;
5286 bool is_packed
= false;
5287 tree type
= (TREE_TYPE (DR_REF (dr
)));
5289 if (!known_alignment_for_access_p (dr
))
5290 is_packed
= not_size_aligned (DR_REF (dr
));
5292 if ((TYPE_USER_ALIGN (type
) && !is_packed
)
5293 || targetm
.vectorize
.
5294 support_vector_misalignment (mode
, type
,
5295 DR_MISALIGNMENT (dr
), is_packed
))
5296 return dr_unaligned_supported
;
5300 return dr_unaligned_unsupported
;