1 /* Data References Analysis and Manipulation Utilities for Vectorization.
2 Copyright (C) 2003-2013 Free Software Foundation, Inc.
3 Contributed by Dorit Naishlos <dorit@il.ibm.com>
4 and Ira Rosen <irar@il.ibm.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
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 /* Unknown data dependence. */
239 if (DDR_ARE_DEPENDENT (ddr
) == chrec_dont_know
)
241 /* If user asserted safelen consecutive iterations can be
242 executed concurrently, assume independence. */
243 if (loop
->safelen
>= 2)
245 if (loop
->safelen
< *max_vf
)
246 *max_vf
= loop
->safelen
;
250 if (STMT_VINFO_GATHER_P (stmtinfo_a
)
251 || STMT_VINFO_GATHER_P (stmtinfo_b
))
253 if (dump_enabled_p ())
255 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
256 "versioning for alias not supported for: "
257 "can't determine dependence between ");
258 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
,
260 dump_printf (MSG_MISSED_OPTIMIZATION
, " and ");
261 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
,
263 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
268 if (dump_enabled_p ())
270 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
271 "versioning for alias required: "
272 "can't determine dependence between ");
273 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
,
275 dump_printf (MSG_MISSED_OPTIMIZATION
, " and ");
276 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
,
278 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
281 /* Add to list of ddrs that need to be tested at run-time. */
282 return !vect_mark_for_runtime_alias_test (ddr
, loop_vinfo
);
285 /* Known data dependence. */
286 if (DDR_NUM_DIST_VECTS (ddr
) == 0)
288 /* If user asserted safelen consecutive iterations can be
289 executed concurrently, assume independence. */
290 if (loop
->safelen
>= 2)
292 if (loop
->safelen
< *max_vf
)
293 *max_vf
= loop
->safelen
;
297 if (STMT_VINFO_GATHER_P (stmtinfo_a
)
298 || STMT_VINFO_GATHER_P (stmtinfo_b
))
300 if (dump_enabled_p ())
302 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
303 "versioning for alias not supported for: "
304 "bad dist vector for ");
305 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
,
307 dump_printf (MSG_MISSED_OPTIMIZATION
, " and ");
308 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
,
310 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
315 if (dump_enabled_p ())
317 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
318 "versioning for alias required: "
319 "bad dist vector for ");
320 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
, DR_REF (dra
));
321 dump_printf (MSG_MISSED_OPTIMIZATION
, " and ");
322 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
, DR_REF (drb
));
323 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
325 /* Add to list of ddrs that need to be tested at run-time. */
326 return !vect_mark_for_runtime_alias_test (ddr
, loop_vinfo
);
329 loop_depth
= index_in_loop_nest (loop
->num
, DDR_LOOP_NEST (ddr
));
330 FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr
), i
, dist_v
)
332 int dist
= dist_v
[loop_depth
];
334 if (dump_enabled_p ())
335 dump_printf_loc (MSG_NOTE
, vect_location
,
336 "dependence distance = %d.\n", dist
);
340 if (dump_enabled_p ())
342 dump_printf_loc (MSG_NOTE
, vect_location
,
343 "dependence distance == 0 between ");
344 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, DR_REF (dra
));
345 dump_printf (MSG_NOTE
, " and ");
346 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, DR_REF (drb
));
347 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
350 /* When we perform grouped accesses and perform implicit CSE
351 by detecting equal accesses and doing disambiguation with
352 runtime alias tests like for
360 where we will end up loading { a[i], a[i+1] } once, make
361 sure that inserting group loads before the first load and
362 stores after the last store will do the right thing. */
363 if ((STMT_VINFO_GROUPED_ACCESS (stmtinfo_a
)
364 && GROUP_SAME_DR_STMT (stmtinfo_a
))
365 || (STMT_VINFO_GROUPED_ACCESS (stmtinfo_b
)
366 && GROUP_SAME_DR_STMT (stmtinfo_b
)))
369 earlier_stmt
= get_earlier_stmt (DR_STMT (dra
), DR_STMT (drb
));
371 (STMT_VINFO_DATA_REF (vinfo_for_stmt (earlier_stmt
))))
373 if (dump_enabled_p ())
374 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
375 "READ_WRITE dependence in interleaving."
384 if (dist
> 0 && DDR_REVERSED_P (ddr
))
386 /* If DDR_REVERSED_P the order of the data-refs in DDR was
387 reversed (to make distance vector positive), and the actual
388 distance is negative. */
389 if (dump_enabled_p ())
390 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
391 "dependence distance negative.\n");
396 && abs (dist
) < *max_vf
)
398 /* The dependence distance requires reduction of the maximal
399 vectorization factor. */
400 *max_vf
= abs (dist
);
401 if (dump_enabled_p ())
402 dump_printf_loc (MSG_NOTE
, vect_location
,
403 "adjusting maximal vectorization factor to %i\n",
407 if (abs (dist
) >= *max_vf
)
409 /* Dependence distance does not create dependence, as far as
410 vectorization is concerned, in this case. */
411 if (dump_enabled_p ())
412 dump_printf_loc (MSG_NOTE
, vect_location
,
413 "dependence distance >= VF.\n");
417 if (dump_enabled_p ())
419 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
420 "not vectorized, possible dependence "
421 "between data-refs ");
422 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, DR_REF (dra
));
423 dump_printf (MSG_NOTE
, " and ");
424 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, DR_REF (drb
));
425 dump_printf (MSG_NOTE
, "\n");
434 /* Function vect_analyze_data_ref_dependences.
436 Examine all the data references in the loop, and make sure there do not
437 exist any data dependences between them. Set *MAX_VF according to
438 the maximum vectorization factor the data dependences allow. */
441 vect_analyze_data_ref_dependences (loop_vec_info loop_vinfo
, int *max_vf
)
444 struct data_dependence_relation
*ddr
;
446 if (dump_enabled_p ())
447 dump_printf_loc (MSG_NOTE
, vect_location
,
448 "=== vect_analyze_data_ref_dependences ===\n");
450 if (!compute_all_dependences (LOOP_VINFO_DATAREFS (loop_vinfo
),
451 &LOOP_VINFO_DDRS (loop_vinfo
),
452 LOOP_VINFO_LOOP_NEST (loop_vinfo
), true))
455 FOR_EACH_VEC_ELT (LOOP_VINFO_DDRS (loop_vinfo
), i
, ddr
)
456 if (vect_analyze_data_ref_dependence (ddr
, loop_vinfo
, max_vf
))
463 /* Function vect_slp_analyze_data_ref_dependence.
465 Return TRUE if there (might) exist a dependence between a memory-reference
466 DRA and a memory-reference DRB. When versioning for alias may check a
467 dependence at run-time, return FALSE. Adjust *MAX_VF according to
468 the data dependence. */
471 vect_slp_analyze_data_ref_dependence (struct data_dependence_relation
*ddr
)
473 struct data_reference
*dra
= DDR_A (ddr
);
474 struct data_reference
*drb
= DDR_B (ddr
);
476 /* We need to check dependences of statements marked as unvectorizable
477 as well, they still can prohibit vectorization. */
479 /* Independent data accesses. */
480 if (DDR_ARE_DEPENDENT (ddr
) == chrec_known
)
486 /* Read-read is OK. */
487 if (DR_IS_READ (dra
) && DR_IS_READ (drb
))
490 /* If dra and drb are part of the same interleaving chain consider
492 if (STMT_VINFO_GROUPED_ACCESS (vinfo_for_stmt (DR_STMT (dra
)))
493 && (GROUP_FIRST_ELEMENT (vinfo_for_stmt (DR_STMT (dra
)))
494 == GROUP_FIRST_ELEMENT (vinfo_for_stmt (DR_STMT (drb
)))))
497 /* Unknown data dependence. */
498 if (DDR_ARE_DEPENDENT (ddr
) == chrec_dont_know
)
500 if (dump_enabled_p ())
502 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
503 "can't determine dependence between ");
504 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
, DR_REF (dra
));
505 dump_printf (MSG_MISSED_OPTIMIZATION
, " and ");
506 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
, DR_REF (drb
));
507 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
510 else if (dump_enabled_p ())
512 dump_printf_loc (MSG_NOTE
, vect_location
,
513 "determined dependence between ");
514 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, DR_REF (dra
));
515 dump_printf (MSG_NOTE
, " and ");
516 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, DR_REF (drb
));
517 dump_printf (MSG_NOTE
, "\n");
520 /* We do not vectorize basic blocks with write-write dependencies. */
521 if (DR_IS_WRITE (dra
) && DR_IS_WRITE (drb
))
524 /* If we have a read-write dependence check that the load is before the store.
525 When we vectorize basic blocks, vector load can be only before
526 corresponding scalar load, and vector store can be only after its
527 corresponding scalar store. So the order of the acceses is preserved in
528 case the load is before the store. */
529 gimple earlier_stmt
= get_earlier_stmt (DR_STMT (dra
), DR_STMT (drb
));
530 if (DR_IS_READ (STMT_VINFO_DATA_REF (vinfo_for_stmt (earlier_stmt
))))
532 /* That only holds for load-store pairs taking part in vectorization. */
533 if (STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dra
)))
534 && STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (drb
))))
542 /* Function vect_analyze_data_ref_dependences.
544 Examine all the data references in the basic-block, and make sure there
545 do not exist any data dependences between them. Set *MAX_VF according to
546 the maximum vectorization factor the data dependences allow. */
549 vect_slp_analyze_data_ref_dependences (bb_vec_info bb_vinfo
)
551 struct data_dependence_relation
*ddr
;
554 if (dump_enabled_p ())
555 dump_printf_loc (MSG_NOTE
, vect_location
,
556 "=== vect_slp_analyze_data_ref_dependences ===\n");
558 if (!compute_all_dependences (BB_VINFO_DATAREFS (bb_vinfo
),
559 &BB_VINFO_DDRS (bb_vinfo
),
563 FOR_EACH_VEC_ELT (BB_VINFO_DDRS (bb_vinfo
), i
, ddr
)
564 if (vect_slp_analyze_data_ref_dependence (ddr
))
571 /* Function vect_compute_data_ref_alignment
573 Compute the misalignment of the data reference DR.
576 1. If during the misalignment computation it is found that the data reference
577 cannot be vectorized then false is returned.
578 2. DR_MISALIGNMENT (DR) is defined.
580 FOR NOW: No analysis is actually performed. Misalignment is calculated
581 only for trivial cases. TODO. */
584 vect_compute_data_ref_alignment (struct data_reference
*dr
)
586 gimple stmt
= DR_STMT (dr
);
587 stmt_vec_info stmt_info
= vinfo_for_stmt (stmt
);
588 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_info
);
589 struct loop
*loop
= NULL
;
590 tree ref
= DR_REF (dr
);
592 tree base
, base_addr
;
595 tree aligned_to
, alignment
;
597 if (dump_enabled_p ())
598 dump_printf_loc (MSG_NOTE
, vect_location
,
599 "vect_compute_data_ref_alignment:\n");
602 loop
= LOOP_VINFO_LOOP (loop_vinfo
);
604 /* Initialize misalignment to unknown. */
605 SET_DR_MISALIGNMENT (dr
, -1);
607 /* Strided loads perform only component accesses, misalignment information
608 is irrelevant for them. */
609 if (STMT_VINFO_STRIDE_LOAD_P (stmt_info
))
612 misalign
= DR_INIT (dr
);
613 aligned_to
= DR_ALIGNED_TO (dr
);
614 base_addr
= DR_BASE_ADDRESS (dr
);
615 vectype
= STMT_VINFO_VECTYPE (stmt_info
);
617 /* In case the dataref is in an inner-loop of the loop that is being
618 vectorized (LOOP), we use the base and misalignment information
619 relative to the outer-loop (LOOP). This is ok only if the misalignment
620 stays the same throughout the execution of the inner-loop, which is why
621 we have to check that the stride of the dataref in the inner-loop evenly
622 divides by the vector size. */
623 if (loop
&& nested_in_vect_loop_p (loop
, stmt
))
625 tree step
= DR_STEP (dr
);
626 HOST_WIDE_INT dr_step
= TREE_INT_CST_LOW (step
);
628 if (dr_step
% GET_MODE_SIZE (TYPE_MODE (vectype
)) == 0)
630 if (dump_enabled_p ())
631 dump_printf_loc (MSG_NOTE
, vect_location
,
632 "inner step divides the vector-size.\n");
633 misalign
= STMT_VINFO_DR_INIT (stmt_info
);
634 aligned_to
= STMT_VINFO_DR_ALIGNED_TO (stmt_info
);
635 base_addr
= STMT_VINFO_DR_BASE_ADDRESS (stmt_info
);
639 if (dump_enabled_p ())
640 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
641 "inner step doesn't divide the vector-size.\n");
642 misalign
= NULL_TREE
;
646 /* Similarly, if we're doing basic-block vectorization, we can only use
647 base and misalignment information relative to an innermost loop if the
648 misalignment stays the same throughout the execution of the loop.
649 As above, this is the case if the stride of the dataref evenly divides
650 by the vector size. */
653 tree step
= DR_STEP (dr
);
654 HOST_WIDE_INT dr_step
= TREE_INT_CST_LOW (step
);
656 if (dr_step
% GET_MODE_SIZE (TYPE_MODE (vectype
)) != 0)
658 if (dump_enabled_p ())
659 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
660 "SLP: step doesn't divide the vector-size.\n");
661 misalign
= NULL_TREE
;
665 base
= build_fold_indirect_ref (base_addr
);
666 alignment
= ssize_int (TYPE_ALIGN (vectype
)/BITS_PER_UNIT
);
668 if ((aligned_to
&& tree_int_cst_compare (aligned_to
, alignment
) < 0)
671 if (dump_enabled_p ())
673 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
674 "Unknown alignment for access: ");
675 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
, base
);
676 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
682 && tree_int_cst_compare (ssize_int (DECL_ALIGN_UNIT (base
)),
684 || (TREE_CODE (base_addr
) == SSA_NAME
685 && tree_int_cst_compare (ssize_int (TYPE_ALIGN_UNIT (TREE_TYPE (
686 TREE_TYPE (base_addr
)))),
688 || (get_pointer_alignment (base_addr
) >= TYPE_ALIGN (vectype
)))
691 base_aligned
= false;
695 /* Do not change the alignment of global variables here if
696 flag_section_anchors is enabled as we already generated
697 RTL for other functions. Most global variables should
698 have been aligned during the IPA increase_alignment pass. */
699 if (!vect_can_force_dr_alignment_p (base
, TYPE_ALIGN (vectype
))
700 || (TREE_STATIC (base
) && flag_section_anchors
))
702 if (dump_enabled_p ())
704 dump_printf_loc (MSG_NOTE
, vect_location
,
705 "can't force alignment of ref: ");
706 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, ref
);
707 dump_printf (MSG_NOTE
, "\n");
712 /* Force the alignment of the decl.
713 NOTE: This is the only change to the code we make during
714 the analysis phase, before deciding to vectorize the loop. */
715 if (dump_enabled_p ())
717 dump_printf_loc (MSG_NOTE
, vect_location
, "force alignment of ");
718 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, ref
);
719 dump_printf (MSG_NOTE
, "\n");
722 ((dataref_aux
*)dr
->aux
)->base_decl
= base
;
723 ((dataref_aux
*)dr
->aux
)->base_misaligned
= true;
726 /* If this is a backward running DR then first access in the larger
727 vectype actually is N-1 elements before the address in the DR.
728 Adjust misalign accordingly. */
729 if (tree_int_cst_compare (DR_STEP (dr
), size_zero_node
) < 0)
731 tree offset
= ssize_int (TYPE_VECTOR_SUBPARTS (vectype
) - 1);
732 /* DR_STEP(dr) is the same as -TYPE_SIZE of the scalar type,
733 otherwise we wouldn't be here. */
734 offset
= fold_build2 (MULT_EXPR
, ssizetype
, offset
, DR_STEP (dr
));
735 /* PLUS because DR_STEP was negative. */
736 misalign
= size_binop (PLUS_EXPR
, misalign
, offset
);
739 /* Modulo alignment. */
740 misalign
= size_binop (FLOOR_MOD_EXPR
, misalign
, alignment
);
742 if (!tree_fits_uhwi_p (misalign
))
744 /* Negative or overflowed misalignment value. */
745 if (dump_enabled_p ())
746 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
747 "unexpected misalign value\n");
751 SET_DR_MISALIGNMENT (dr
, tree_to_uhwi (misalign
));
753 if (dump_enabled_p ())
755 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
756 "misalign = %d bytes of ref ", DR_MISALIGNMENT (dr
));
757 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
, ref
);
758 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
765 /* Function vect_compute_data_refs_alignment
767 Compute the misalignment of data references in the loop.
768 Return FALSE if a data reference is found that cannot be vectorized. */
771 vect_compute_data_refs_alignment (loop_vec_info loop_vinfo
,
772 bb_vec_info bb_vinfo
)
774 vec
<data_reference_p
> datarefs
;
775 struct data_reference
*dr
;
779 datarefs
= LOOP_VINFO_DATAREFS (loop_vinfo
);
781 datarefs
= BB_VINFO_DATAREFS (bb_vinfo
);
783 FOR_EACH_VEC_ELT (datarefs
, i
, dr
)
784 if (STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dr
)))
785 && !vect_compute_data_ref_alignment (dr
))
789 /* Mark unsupported statement as unvectorizable. */
790 STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dr
))) = false;
801 /* Function vect_update_misalignment_for_peel
803 DR - the data reference whose misalignment is to be adjusted.
804 DR_PEEL - the data reference whose misalignment is being made
805 zero in the vector loop by the peel.
806 NPEEL - the number of iterations in the peel loop if the misalignment
807 of DR_PEEL is known at compile time. */
810 vect_update_misalignment_for_peel (struct data_reference
*dr
,
811 struct data_reference
*dr_peel
, int npeel
)
814 vec
<dr_p
> same_align_drs
;
815 struct data_reference
*current_dr
;
816 int dr_size
= GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (DR_REF (dr
))));
817 int dr_peel_size
= GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (DR_REF (dr_peel
))));
818 stmt_vec_info stmt_info
= vinfo_for_stmt (DR_STMT (dr
));
819 stmt_vec_info peel_stmt_info
= vinfo_for_stmt (DR_STMT (dr_peel
));
821 /* For interleaved data accesses the step in the loop must be multiplied by
822 the size of the interleaving group. */
823 if (STMT_VINFO_GROUPED_ACCESS (stmt_info
))
824 dr_size
*= GROUP_SIZE (vinfo_for_stmt (GROUP_FIRST_ELEMENT (stmt_info
)));
825 if (STMT_VINFO_GROUPED_ACCESS (peel_stmt_info
))
826 dr_peel_size
*= GROUP_SIZE (peel_stmt_info
);
828 /* It can be assumed that the data refs with the same alignment as dr_peel
829 are aligned in the vector loop. */
831 = STMT_VINFO_SAME_ALIGN_REFS (vinfo_for_stmt (DR_STMT (dr_peel
)));
832 FOR_EACH_VEC_ELT (same_align_drs
, i
, current_dr
)
834 if (current_dr
!= dr
)
836 gcc_assert (DR_MISALIGNMENT (dr
) / dr_size
==
837 DR_MISALIGNMENT (dr_peel
) / dr_peel_size
);
838 SET_DR_MISALIGNMENT (dr
, 0);
842 if (known_alignment_for_access_p (dr
)
843 && known_alignment_for_access_p (dr_peel
))
845 bool negative
= tree_int_cst_compare (DR_STEP (dr
), size_zero_node
) < 0;
846 int misal
= DR_MISALIGNMENT (dr
);
847 tree vectype
= STMT_VINFO_VECTYPE (stmt_info
);
848 misal
+= negative
? -npeel
* dr_size
: npeel
* dr_size
;
849 misal
&= (TYPE_ALIGN (vectype
) / BITS_PER_UNIT
) - 1;
850 SET_DR_MISALIGNMENT (dr
, misal
);
854 if (dump_enabled_p ())
855 dump_printf_loc (MSG_NOTE
, vect_location
, "Setting misalignment to -1.\n");
856 SET_DR_MISALIGNMENT (dr
, -1);
860 /* Function vect_verify_datarefs_alignment
862 Return TRUE if all data references in the loop can be
863 handled with respect to alignment. */
866 vect_verify_datarefs_alignment (loop_vec_info loop_vinfo
, bb_vec_info bb_vinfo
)
868 vec
<data_reference_p
> datarefs
;
869 struct data_reference
*dr
;
870 enum dr_alignment_support supportable_dr_alignment
;
874 datarefs
= LOOP_VINFO_DATAREFS (loop_vinfo
);
876 datarefs
= BB_VINFO_DATAREFS (bb_vinfo
);
878 FOR_EACH_VEC_ELT (datarefs
, i
, dr
)
880 gimple stmt
= DR_STMT (dr
);
881 stmt_vec_info stmt_info
= vinfo_for_stmt (stmt
);
883 if (!STMT_VINFO_RELEVANT_P (stmt_info
))
886 /* For interleaving, only the alignment of the first access matters.
887 Skip statements marked as not vectorizable. */
888 if ((STMT_VINFO_GROUPED_ACCESS (stmt_info
)
889 && GROUP_FIRST_ELEMENT (stmt_info
) != stmt
)
890 || !STMT_VINFO_VECTORIZABLE (stmt_info
))
893 /* Strided loads perform only component accesses, alignment is
894 irrelevant for them. */
895 if (STMT_VINFO_STRIDE_LOAD_P (stmt_info
))
898 supportable_dr_alignment
= vect_supportable_dr_alignment (dr
, false);
899 if (!supportable_dr_alignment
)
901 if (dump_enabled_p ())
904 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
905 "not vectorized: unsupported unaligned load.");
907 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
908 "not vectorized: unsupported unaligned "
911 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
,
913 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
917 if (supportable_dr_alignment
!= dr_aligned
&& dump_enabled_p ())
918 dump_printf_loc (MSG_NOTE
, vect_location
,
919 "Vectorizing an unaligned access.\n");
924 /* Given an memory reference EXP return whether its alignment is less
928 not_size_aligned (tree exp
)
930 if (!tree_fits_uhwi_p (TYPE_SIZE (TREE_TYPE (exp
))))
933 return (tree_to_uhwi (TYPE_SIZE (TREE_TYPE (exp
)))
934 > get_object_alignment (exp
));
937 /* Function vector_alignment_reachable_p
939 Return true if vector alignment for DR is reachable by peeling
940 a few loop iterations. Return false otherwise. */
943 vector_alignment_reachable_p (struct data_reference
*dr
)
945 gimple stmt
= DR_STMT (dr
);
946 stmt_vec_info stmt_info
= vinfo_for_stmt (stmt
);
947 tree vectype
= STMT_VINFO_VECTYPE (stmt_info
);
949 if (STMT_VINFO_GROUPED_ACCESS (stmt_info
))
951 /* For interleaved access we peel only if number of iterations in
952 the prolog loop ({VF - misalignment}), is a multiple of the
953 number of the interleaved accesses. */
954 int elem_size
, mis_in_elements
;
955 int nelements
= TYPE_VECTOR_SUBPARTS (vectype
);
957 /* FORNOW: handle only known alignment. */
958 if (!known_alignment_for_access_p (dr
))
961 elem_size
= GET_MODE_SIZE (TYPE_MODE (vectype
)) / nelements
;
962 mis_in_elements
= DR_MISALIGNMENT (dr
) / elem_size
;
964 if ((nelements
- mis_in_elements
) % GROUP_SIZE (stmt_info
))
968 /* If misalignment is known at the compile time then allow peeling
969 only if natural alignment is reachable through peeling. */
970 if (known_alignment_for_access_p (dr
) && !aligned_access_p (dr
))
972 HOST_WIDE_INT elmsize
=
973 int_cst_value (TYPE_SIZE_UNIT (TREE_TYPE (vectype
)));
974 if (dump_enabled_p ())
976 dump_printf_loc (MSG_NOTE
, vect_location
,
977 "data size =" HOST_WIDE_INT_PRINT_DEC
, elmsize
);
978 dump_printf (MSG_NOTE
,
979 ". misalignment = %d.\n", DR_MISALIGNMENT (dr
));
981 if (DR_MISALIGNMENT (dr
) % elmsize
)
983 if (dump_enabled_p ())
984 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
985 "data size does not divide the misalignment.\n");
990 if (!known_alignment_for_access_p (dr
))
992 tree type
= TREE_TYPE (DR_REF (dr
));
993 bool is_packed
= not_size_aligned (DR_REF (dr
));
994 if (dump_enabled_p ())
995 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
996 "Unknown misalignment, is_packed = %d\n",is_packed
);
997 if ((TYPE_USER_ALIGN (type
) && !is_packed
)
998 || targetm
.vectorize
.vector_alignment_reachable (type
, is_packed
))
1008 /* Calculate the cost of the memory access represented by DR. */
1011 vect_get_data_access_cost (struct data_reference
*dr
,
1012 unsigned int *inside_cost
,
1013 unsigned int *outside_cost
,
1014 stmt_vector_for_cost
*body_cost_vec
)
1016 gimple stmt
= DR_STMT (dr
);
1017 stmt_vec_info stmt_info
= vinfo_for_stmt (stmt
);
1018 int nunits
= TYPE_VECTOR_SUBPARTS (STMT_VINFO_VECTYPE (stmt_info
));
1019 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_info
);
1020 int vf
= LOOP_VINFO_VECT_FACTOR (loop_vinfo
);
1021 int ncopies
= vf
/ nunits
;
1023 if (DR_IS_READ (dr
))
1024 vect_get_load_cost (dr
, ncopies
, true, inside_cost
, outside_cost
,
1025 NULL
, body_cost_vec
, false);
1027 vect_get_store_cost (dr
, ncopies
, inside_cost
, body_cost_vec
);
1029 if (dump_enabled_p ())
1030 dump_printf_loc (MSG_NOTE
, vect_location
,
1031 "vect_get_data_access_cost: inside_cost = %d, "
1032 "outside_cost = %d.\n", *inside_cost
, *outside_cost
);
1036 /* Insert DR into peeling hash table with NPEEL as key. */
1039 vect_peeling_hash_insert (loop_vec_info loop_vinfo
, struct data_reference
*dr
,
1042 struct _vect_peel_info elem
, *slot
;
1043 _vect_peel_info
**new_slot
;
1044 bool supportable_dr_alignment
= vect_supportable_dr_alignment (dr
, true);
1047 slot
= LOOP_VINFO_PEELING_HTAB (loop_vinfo
).find (&elem
);
1052 slot
= XNEW (struct _vect_peel_info
);
1053 slot
->npeel
= npeel
;
1056 new_slot
= LOOP_VINFO_PEELING_HTAB (loop_vinfo
).find_slot (slot
, INSERT
);
1060 if (!supportable_dr_alignment
1061 && unlimited_cost_model (LOOP_VINFO_LOOP (loop_vinfo
)))
1062 slot
->count
+= VECT_MAX_COST
;
1066 /* Traverse peeling hash table to find peeling option that aligns maximum
1067 number of data accesses. */
1070 vect_peeling_hash_get_most_frequent (_vect_peel_info
**slot
,
1071 _vect_peel_extended_info
*max
)
1073 vect_peel_info elem
= *slot
;
1075 if (elem
->count
> max
->peel_info
.count
1076 || (elem
->count
== max
->peel_info
.count
1077 && max
->peel_info
.npeel
> elem
->npeel
))
1079 max
->peel_info
.npeel
= elem
->npeel
;
1080 max
->peel_info
.count
= elem
->count
;
1081 max
->peel_info
.dr
= elem
->dr
;
1088 /* Traverse peeling hash table and calculate cost for each peeling option.
1089 Find the one with the lowest cost. */
1092 vect_peeling_hash_get_lowest_cost (_vect_peel_info
**slot
,
1093 _vect_peel_extended_info
*min
)
1095 vect_peel_info elem
= *slot
;
1096 int save_misalignment
, dummy
;
1097 unsigned int inside_cost
= 0, outside_cost
= 0, i
;
1098 gimple stmt
= DR_STMT (elem
->dr
);
1099 stmt_vec_info stmt_info
= vinfo_for_stmt (stmt
);
1100 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_info
);
1101 vec
<data_reference_p
> datarefs
= LOOP_VINFO_DATAREFS (loop_vinfo
);
1102 struct data_reference
*dr
;
1103 stmt_vector_for_cost prologue_cost_vec
, body_cost_vec
, epilogue_cost_vec
;
1104 int single_iter_cost
;
1106 prologue_cost_vec
.create (2);
1107 body_cost_vec
.create (2);
1108 epilogue_cost_vec
.create (2);
1110 FOR_EACH_VEC_ELT (datarefs
, i
, dr
)
1112 stmt
= DR_STMT (dr
);
1113 stmt_info
= vinfo_for_stmt (stmt
);
1114 /* For interleaving, only the alignment of the first access
1116 if (STMT_VINFO_GROUPED_ACCESS (stmt_info
)
1117 && GROUP_FIRST_ELEMENT (stmt_info
) != stmt
)
1120 save_misalignment
= DR_MISALIGNMENT (dr
);
1121 vect_update_misalignment_for_peel (dr
, elem
->dr
, elem
->npeel
);
1122 vect_get_data_access_cost (dr
, &inside_cost
, &outside_cost
,
1124 SET_DR_MISALIGNMENT (dr
, save_misalignment
);
1127 single_iter_cost
= vect_get_single_scalar_iteration_cost (loop_vinfo
);
1128 outside_cost
+= vect_get_known_peeling_cost (loop_vinfo
, elem
->npeel
,
1129 &dummy
, single_iter_cost
,
1131 &epilogue_cost_vec
);
1133 /* Prologue and epilogue costs are added to the target model later.
1134 These costs depend only on the scalar iteration cost, the
1135 number of peeling iterations finally chosen, and the number of
1136 misaligned statements. So discard the information found here. */
1137 prologue_cost_vec
.release ();
1138 epilogue_cost_vec
.release ();
1140 if (inside_cost
< min
->inside_cost
1141 || (inside_cost
== min
->inside_cost
&& outside_cost
< min
->outside_cost
))
1143 min
->inside_cost
= inside_cost
;
1144 min
->outside_cost
= outside_cost
;
1145 min
->body_cost_vec
.release ();
1146 min
->body_cost_vec
= body_cost_vec
;
1147 min
->peel_info
.dr
= elem
->dr
;
1148 min
->peel_info
.npeel
= elem
->npeel
;
1151 body_cost_vec
.release ();
1157 /* Choose best peeling option by traversing peeling hash table and either
1158 choosing an option with the lowest cost (if cost model is enabled) or the
1159 option that aligns as many accesses as possible. */
1161 static struct data_reference
*
1162 vect_peeling_hash_choose_best_peeling (loop_vec_info loop_vinfo
,
1163 unsigned int *npeel
,
1164 stmt_vector_for_cost
*body_cost_vec
)
1166 struct _vect_peel_extended_info res
;
1168 res
.peel_info
.dr
= NULL
;
1169 res
.body_cost_vec
= stmt_vector_for_cost ();
1171 if (!unlimited_cost_model (LOOP_VINFO_LOOP (loop_vinfo
)))
1173 res
.inside_cost
= INT_MAX
;
1174 res
.outside_cost
= INT_MAX
;
1175 LOOP_VINFO_PEELING_HTAB (loop_vinfo
)
1176 .traverse
<_vect_peel_extended_info
*,
1177 vect_peeling_hash_get_lowest_cost
> (&res
);
1181 res
.peel_info
.count
= 0;
1182 LOOP_VINFO_PEELING_HTAB (loop_vinfo
)
1183 .traverse
<_vect_peel_extended_info
*,
1184 vect_peeling_hash_get_most_frequent
> (&res
);
1187 *npeel
= res
.peel_info
.npeel
;
1188 *body_cost_vec
= res
.body_cost_vec
;
1189 return res
.peel_info
.dr
;
1193 /* Function vect_enhance_data_refs_alignment
1195 This pass will use loop versioning and loop peeling in order to enhance
1196 the alignment of data references in the loop.
1198 FOR NOW: we assume that whatever versioning/peeling takes place, only the
1199 original loop is to be vectorized. Any other loops that are created by
1200 the transformations performed in this pass - are not supposed to be
1201 vectorized. This restriction will be relaxed.
1203 This pass will require a cost model to guide it whether to apply peeling
1204 or versioning or a combination of the two. For example, the scheme that
1205 intel uses when given a loop with several memory accesses, is as follows:
1206 choose one memory access ('p') which alignment you want to force by doing
1207 peeling. Then, either (1) generate a loop in which 'p' is aligned and all
1208 other accesses are not necessarily aligned, or (2) use loop versioning to
1209 generate one loop in which all accesses are aligned, and another loop in
1210 which only 'p' is necessarily aligned.
1212 ("Automatic Intra-Register Vectorization for the Intel Architecture",
1213 Aart J.C. Bik, Milind Girkar, Paul M. Grey and Ximmin Tian, International
1214 Journal of Parallel Programming, Vol. 30, No. 2, April 2002.)
1216 Devising a cost model is the most critical aspect of this work. It will
1217 guide us on which access to peel for, whether to use loop versioning, how
1218 many versions to create, etc. The cost model will probably consist of
1219 generic considerations as well as target specific considerations (on
1220 powerpc for example, misaligned stores are more painful than misaligned
1223 Here are the general steps involved in alignment enhancements:
1225 -- original loop, before alignment analysis:
1226 for (i=0; i<N; i++){
1227 x = q[i]; # DR_MISALIGNMENT(q) = unknown
1228 p[i] = y; # DR_MISALIGNMENT(p) = unknown
1231 -- After vect_compute_data_refs_alignment:
1232 for (i=0; i<N; i++){
1233 x = q[i]; # DR_MISALIGNMENT(q) = 3
1234 p[i] = y; # DR_MISALIGNMENT(p) = unknown
1237 -- Possibility 1: we do loop versioning:
1239 for (i=0; i<N; i++){ # loop 1A
1240 x = q[i]; # DR_MISALIGNMENT(q) = 3
1241 p[i] = y; # DR_MISALIGNMENT(p) = 0
1245 for (i=0; i<N; i++){ # loop 1B
1246 x = q[i]; # DR_MISALIGNMENT(q) = 3
1247 p[i] = y; # DR_MISALIGNMENT(p) = unaligned
1251 -- Possibility 2: we do loop peeling:
1252 for (i = 0; i < 3; i++){ # (scalar loop, not to be vectorized).
1256 for (i = 3; i < N; i++){ # loop 2A
1257 x = q[i]; # DR_MISALIGNMENT(q) = 0
1258 p[i] = y; # DR_MISALIGNMENT(p) = unknown
1261 -- Possibility 3: combination of loop peeling and versioning:
1262 for (i = 0; i < 3; i++){ # (scalar loop, not to be vectorized).
1267 for (i = 3; i<N; i++){ # loop 3A
1268 x = q[i]; # DR_MISALIGNMENT(q) = 0
1269 p[i] = y; # DR_MISALIGNMENT(p) = 0
1273 for (i = 3; i<N; i++){ # loop 3B
1274 x = q[i]; # DR_MISALIGNMENT(q) = 0
1275 p[i] = y; # DR_MISALIGNMENT(p) = unaligned
1279 These loops are later passed to loop_transform to be vectorized. The
1280 vectorizer will use the alignment information to guide the transformation
1281 (whether to generate regular loads/stores, or with special handling for
1285 vect_enhance_data_refs_alignment (loop_vec_info loop_vinfo
)
1287 vec
<data_reference_p
> datarefs
= LOOP_VINFO_DATAREFS (loop_vinfo
);
1288 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
1289 enum dr_alignment_support supportable_dr_alignment
;
1290 struct data_reference
*dr0
= NULL
, *first_store
= NULL
;
1291 struct data_reference
*dr
;
1293 bool do_peeling
= false;
1294 bool do_versioning
= false;
1297 stmt_vec_info stmt_info
;
1298 unsigned int npeel
= 0;
1299 bool all_misalignments_unknown
= true;
1300 unsigned int vf
= LOOP_VINFO_VECT_FACTOR (loop_vinfo
);
1301 unsigned possible_npeel_number
= 1;
1303 unsigned int nelements
, mis
, same_align_drs_max
= 0;
1304 stmt_vector_for_cost body_cost_vec
= stmt_vector_for_cost ();
1306 if (dump_enabled_p ())
1307 dump_printf_loc (MSG_NOTE
, vect_location
,
1308 "=== vect_enhance_data_refs_alignment ===\n");
1310 /* While cost model enhancements are expected in the future, the high level
1311 view of the code at this time is as follows:
1313 A) If there is a misaligned access then see if peeling to align
1314 this access can make all data references satisfy
1315 vect_supportable_dr_alignment. If so, update data structures
1316 as needed and return true.
1318 B) If peeling wasn't possible and there is a data reference with an
1319 unknown misalignment that does not satisfy vect_supportable_dr_alignment
1320 then see if loop versioning checks can be used to make all data
1321 references satisfy vect_supportable_dr_alignment. If so, update
1322 data structures as needed and return true.
1324 C) If neither peeling nor versioning were successful then return false if
1325 any data reference does not satisfy vect_supportable_dr_alignment.
1327 D) Return true (all data references satisfy vect_supportable_dr_alignment).
1329 Note, Possibility 3 above (which is peeling and versioning together) is not
1330 being done at this time. */
1332 /* (1) Peeling to force alignment. */
1334 /* (1.1) Decide whether to perform peeling, and how many iterations to peel:
1336 + How many accesses will become aligned due to the peeling
1337 - How many accesses will become unaligned due to the peeling,
1338 and the cost of misaligned accesses.
1339 - The cost of peeling (the extra runtime checks, the increase
1342 FOR_EACH_VEC_ELT (datarefs
, i
, dr
)
1344 stmt
= DR_STMT (dr
);
1345 stmt_info
= vinfo_for_stmt (stmt
);
1347 if (!STMT_VINFO_RELEVANT_P (stmt_info
))
1350 /* For interleaving, only the alignment of the first access
1352 if (STMT_VINFO_GROUPED_ACCESS (stmt_info
)
1353 && GROUP_FIRST_ELEMENT (stmt_info
) != stmt
)
1356 /* For invariant accesses there is nothing to enhance. */
1357 if (integer_zerop (DR_STEP (dr
)))
1360 /* Strided loads perform only component accesses, alignment is
1361 irrelevant for them. */
1362 if (STMT_VINFO_STRIDE_LOAD_P (stmt_info
))
1365 supportable_dr_alignment
= vect_supportable_dr_alignment (dr
, true);
1366 do_peeling
= vector_alignment_reachable_p (dr
);
1369 if (known_alignment_for_access_p (dr
))
1371 unsigned int npeel_tmp
;
1372 bool negative
= tree_int_cst_compare (DR_STEP (dr
),
1373 size_zero_node
) < 0;
1375 /* Save info about DR in the hash table. */
1376 if (!LOOP_VINFO_PEELING_HTAB (loop_vinfo
).is_created ())
1377 LOOP_VINFO_PEELING_HTAB (loop_vinfo
).create (1);
1379 vectype
= STMT_VINFO_VECTYPE (stmt_info
);
1380 nelements
= TYPE_VECTOR_SUBPARTS (vectype
);
1381 mis
= DR_MISALIGNMENT (dr
) / GET_MODE_SIZE (TYPE_MODE (
1382 TREE_TYPE (DR_REF (dr
))));
1383 npeel_tmp
= (negative
1384 ? (mis
- nelements
) : (nelements
- mis
))
1387 /* For multiple types, it is possible that the bigger type access
1388 will have more than one peeling option. E.g., a loop with two
1389 types: one of size (vector size / 4), and the other one of
1390 size (vector size / 8). Vectorization factor will 8. If both
1391 access are misaligned by 3, the first one needs one scalar
1392 iteration to be aligned, and the second one needs 5. But the
1393 the first one will be aligned also by peeling 5 scalar
1394 iterations, and in that case both accesses will be aligned.
1395 Hence, except for the immediate peeling amount, we also want
1396 to try to add full vector size, while we don't exceed
1397 vectorization factor.
1398 We do this automtically for cost model, since we calculate cost
1399 for every peeling option. */
1400 if (unlimited_cost_model (LOOP_VINFO_LOOP (loop_vinfo
)))
1401 possible_npeel_number
= vf
/nelements
;
1403 /* Handle the aligned case. We may decide to align some other
1404 access, making DR unaligned. */
1405 if (DR_MISALIGNMENT (dr
) == 0)
1408 if (unlimited_cost_model (LOOP_VINFO_LOOP (loop_vinfo
)))
1409 possible_npeel_number
++;
1412 for (j
= 0; j
< possible_npeel_number
; j
++)
1414 gcc_assert (npeel_tmp
<= vf
);
1415 vect_peeling_hash_insert (loop_vinfo
, dr
, npeel_tmp
);
1416 npeel_tmp
+= nelements
;
1419 all_misalignments_unknown
= false;
1420 /* Data-ref that was chosen for the case that all the
1421 misalignments are unknown is not relevant anymore, since we
1422 have a data-ref with known alignment. */
1427 /* If we don't know any misalignment values, we prefer
1428 peeling for data-ref that has the maximum number of data-refs
1429 with the same alignment, unless the target prefers to align
1430 stores over load. */
1431 if (all_misalignments_unknown
)
1433 unsigned same_align_drs
1434 = STMT_VINFO_SAME_ALIGN_REFS (stmt_info
).length ();
1436 || same_align_drs_max
< same_align_drs
)
1438 same_align_drs_max
= same_align_drs
;
1441 /* For data-refs with the same number of related
1442 accesses prefer the one where the misalign
1443 computation will be invariant in the outermost loop. */
1444 else if (same_align_drs_max
== same_align_drs
)
1446 struct loop
*ivloop0
, *ivloop
;
1447 ivloop0
= outermost_invariant_loop_for_expr
1448 (loop
, DR_BASE_ADDRESS (dr0
));
1449 ivloop
= outermost_invariant_loop_for_expr
1450 (loop
, DR_BASE_ADDRESS (dr
));
1451 if ((ivloop
&& !ivloop0
)
1452 || (ivloop
&& ivloop0
1453 && flow_loop_nested_p (ivloop
, ivloop0
)))
1457 if (!first_store
&& DR_IS_WRITE (dr
))
1461 /* If there are both known and unknown misaligned accesses in the
1462 loop, we choose peeling amount according to the known
1464 if (!supportable_dr_alignment
)
1467 if (!first_store
&& DR_IS_WRITE (dr
))
1474 if (!aligned_access_p (dr
))
1476 if (dump_enabled_p ())
1477 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1478 "vector alignment may not be reachable\n");
1484 /* Check if we can possibly peel the loop. */
1485 if (!vect_can_advance_ivs_p (loop_vinfo
)
1486 || !slpeel_can_duplicate_loop_p (loop
, single_exit (loop
)))
1489 if (do_peeling
&& all_misalignments_unknown
1490 && vect_supportable_dr_alignment (dr0
, false))
1493 /* Check if the target requires to prefer stores over loads, i.e., if
1494 misaligned stores are more expensive than misaligned loads (taking
1495 drs with same alignment into account). */
1496 if (first_store
&& DR_IS_READ (dr0
))
1498 unsigned int load_inside_cost
= 0, load_outside_cost
= 0;
1499 unsigned int store_inside_cost
= 0, store_outside_cost
= 0;
1500 unsigned int load_inside_penalty
= 0, load_outside_penalty
= 0;
1501 unsigned int store_inside_penalty
= 0, store_outside_penalty
= 0;
1502 stmt_vector_for_cost dummy
;
1505 vect_get_data_access_cost (dr0
, &load_inside_cost
, &load_outside_cost
,
1507 vect_get_data_access_cost (first_store
, &store_inside_cost
,
1508 &store_outside_cost
, &dummy
);
1512 /* Calculate the penalty for leaving FIRST_STORE unaligned (by
1513 aligning the load DR0). */
1514 load_inside_penalty
= store_inside_cost
;
1515 load_outside_penalty
= store_outside_cost
;
1517 STMT_VINFO_SAME_ALIGN_REFS (vinfo_for_stmt (
1518 DR_STMT (first_store
))).iterate (i
, &dr
);
1520 if (DR_IS_READ (dr
))
1522 load_inside_penalty
+= load_inside_cost
;
1523 load_outside_penalty
+= load_outside_cost
;
1527 load_inside_penalty
+= store_inside_cost
;
1528 load_outside_penalty
+= store_outside_cost
;
1531 /* Calculate the penalty for leaving DR0 unaligned (by
1532 aligning the FIRST_STORE). */
1533 store_inside_penalty
= load_inside_cost
;
1534 store_outside_penalty
= load_outside_cost
;
1536 STMT_VINFO_SAME_ALIGN_REFS (vinfo_for_stmt (
1537 DR_STMT (dr0
))).iterate (i
, &dr
);
1539 if (DR_IS_READ (dr
))
1541 store_inside_penalty
+= load_inside_cost
;
1542 store_outside_penalty
+= load_outside_cost
;
1546 store_inside_penalty
+= store_inside_cost
;
1547 store_outside_penalty
+= store_outside_cost
;
1550 if (load_inside_penalty
> store_inside_penalty
1551 || (load_inside_penalty
== store_inside_penalty
1552 && load_outside_penalty
> store_outside_penalty
))
1556 /* In case there are only loads with different unknown misalignments, use
1557 peeling only if it may help to align other accesses in the loop. */
1559 && !STMT_VINFO_SAME_ALIGN_REFS (
1560 vinfo_for_stmt (DR_STMT (dr0
))).length ()
1561 && vect_supportable_dr_alignment (dr0
, false)
1562 != dr_unaligned_supported
)
1566 if (do_peeling
&& !dr0
)
1568 /* Peeling is possible, but there is no data access that is not supported
1569 unless aligned. So we try to choose the best possible peeling. */
1571 /* We should get here only if there are drs with known misalignment. */
1572 gcc_assert (!all_misalignments_unknown
);
1574 /* Choose the best peeling from the hash table. */
1575 dr0
= vect_peeling_hash_choose_best_peeling (loop_vinfo
, &npeel
,
1583 stmt
= DR_STMT (dr0
);
1584 stmt_info
= vinfo_for_stmt (stmt
);
1585 vectype
= STMT_VINFO_VECTYPE (stmt_info
);
1586 nelements
= TYPE_VECTOR_SUBPARTS (vectype
);
1588 if (known_alignment_for_access_p (dr0
))
1590 bool negative
= tree_int_cst_compare (DR_STEP (dr0
),
1591 size_zero_node
) < 0;
1594 /* Since it's known at compile time, compute the number of
1595 iterations in the peeled loop (the peeling factor) for use in
1596 updating DR_MISALIGNMENT values. The peeling factor is the
1597 vectorization factor minus the misalignment as an element
1599 mis
= DR_MISALIGNMENT (dr0
);
1600 mis
/= GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (DR_REF (dr0
))));
1601 npeel
= ((negative
? mis
- nelements
: nelements
- mis
)
1605 /* For interleaved data access every iteration accesses all the
1606 members of the group, therefore we divide the number of iterations
1607 by the group size. */
1608 stmt_info
= vinfo_for_stmt (DR_STMT (dr0
));
1609 if (STMT_VINFO_GROUPED_ACCESS (stmt_info
))
1610 npeel
/= GROUP_SIZE (stmt_info
);
1612 if (dump_enabled_p ())
1613 dump_printf_loc (MSG_NOTE
, vect_location
,
1614 "Try peeling by %d\n", npeel
);
1617 /* Ensure that all data refs can be vectorized after the peel. */
1618 FOR_EACH_VEC_ELT (datarefs
, i
, dr
)
1620 int save_misalignment
;
1625 stmt
= DR_STMT (dr
);
1626 stmt_info
= vinfo_for_stmt (stmt
);
1627 /* For interleaving, only the alignment of the first access
1629 if (STMT_VINFO_GROUPED_ACCESS (stmt_info
)
1630 && GROUP_FIRST_ELEMENT (stmt_info
) != stmt
)
1633 /* Strided loads perform only component accesses, alignment is
1634 irrelevant for them. */
1635 if (STMT_VINFO_STRIDE_LOAD_P (stmt_info
))
1638 save_misalignment
= DR_MISALIGNMENT (dr
);
1639 vect_update_misalignment_for_peel (dr
, dr0
, npeel
);
1640 supportable_dr_alignment
= vect_supportable_dr_alignment (dr
, false);
1641 SET_DR_MISALIGNMENT (dr
, save_misalignment
);
1643 if (!supportable_dr_alignment
)
1650 if (do_peeling
&& known_alignment_for_access_p (dr0
) && npeel
== 0)
1652 stat
= vect_verify_datarefs_alignment (loop_vinfo
, NULL
);
1657 body_cost_vec
.release ();
1664 unsigned max_allowed_peel
1665 = PARAM_VALUE (PARAM_VECT_MAX_PEELING_FOR_ALIGNMENT
);
1666 if (max_allowed_peel
!= (unsigned)-1)
1668 unsigned max_peel
= npeel
;
1671 gimple dr_stmt
= DR_STMT (dr0
);
1672 stmt_vec_info vinfo
= vinfo_for_stmt (dr_stmt
);
1673 tree vtype
= STMT_VINFO_VECTYPE (vinfo
);
1674 max_peel
= TYPE_VECTOR_SUBPARTS (vtype
) - 1;
1676 if (max_peel
> max_allowed_peel
)
1679 if (dump_enabled_p ())
1680 dump_printf_loc (MSG_NOTE
, vect_location
,
1681 "Disable peeling, max peels reached: %d\n", max_peel
);
1688 stmt_info_for_cost
*si
;
1689 void *data
= LOOP_VINFO_TARGET_COST_DATA (loop_vinfo
);
1691 /* (1.2) Update the DR_MISALIGNMENT of each data reference DR_i.
1692 If the misalignment of DR_i is identical to that of dr0 then set
1693 DR_MISALIGNMENT (DR_i) to zero. If the misalignment of DR_i and
1694 dr0 are known at compile time then increment DR_MISALIGNMENT (DR_i)
1695 by the peeling factor times the element size of DR_i (MOD the
1696 vectorization factor times the size). Otherwise, the
1697 misalignment of DR_i must be set to unknown. */
1698 FOR_EACH_VEC_ELT (datarefs
, i
, dr
)
1700 vect_update_misalignment_for_peel (dr
, dr0
, npeel
);
1702 LOOP_VINFO_UNALIGNED_DR (loop_vinfo
) = dr0
;
1704 LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo
) = npeel
;
1706 LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo
)
1707 = DR_MISALIGNMENT (dr0
);
1708 SET_DR_MISALIGNMENT (dr0
, 0);
1709 if (dump_enabled_p ())
1711 dump_printf_loc (MSG_NOTE
, vect_location
,
1712 "Alignment of access forced using peeling.\n");
1713 dump_printf_loc (MSG_NOTE
, vect_location
,
1714 "Peeling for alignment will be applied.\n");
1716 /* We've delayed passing the inside-loop peeling costs to the
1717 target cost model until we were sure peeling would happen.
1719 if (body_cost_vec
.exists ())
1721 FOR_EACH_VEC_ELT (body_cost_vec
, i
, si
)
1723 struct _stmt_vec_info
*stmt_info
1724 = si
->stmt
? vinfo_for_stmt (si
->stmt
) : NULL
;
1725 (void) add_stmt_cost (data
, si
->count
, si
->kind
, stmt_info
,
1726 si
->misalign
, vect_body
);
1728 body_cost_vec
.release ();
1731 stat
= vect_verify_datarefs_alignment (loop_vinfo
, NULL
);
1737 body_cost_vec
.release ();
1739 /* (2) Versioning to force alignment. */
1741 /* Try versioning if:
1742 1) optimize loop for speed
1743 2) there is at least one unsupported misaligned data ref with an unknown
1745 3) all misaligned data refs with a known misalignment are supported, and
1746 4) the number of runtime alignment checks is within reason. */
1749 optimize_loop_nest_for_speed_p (loop
)
1750 && (!loop
->inner
); /* FORNOW */
1754 FOR_EACH_VEC_ELT (datarefs
, i
, dr
)
1756 stmt
= DR_STMT (dr
);
1757 stmt_info
= vinfo_for_stmt (stmt
);
1759 /* For interleaving, only the alignment of the first access
1761 if (aligned_access_p (dr
)
1762 || (STMT_VINFO_GROUPED_ACCESS (stmt_info
)
1763 && GROUP_FIRST_ELEMENT (stmt_info
) != stmt
))
1766 /* Strided loads perform only component accesses, alignment is
1767 irrelevant for them. */
1768 if (STMT_VINFO_STRIDE_LOAD_P (stmt_info
))
1771 supportable_dr_alignment
= vect_supportable_dr_alignment (dr
, false);
1773 if (!supportable_dr_alignment
)
1779 if (known_alignment_for_access_p (dr
)
1780 || LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo
).length ()
1781 >= (unsigned) PARAM_VALUE (PARAM_VECT_MAX_VERSION_FOR_ALIGNMENT_CHECKS
))
1783 do_versioning
= false;
1787 stmt
= DR_STMT (dr
);
1788 vectype
= STMT_VINFO_VECTYPE (vinfo_for_stmt (stmt
));
1789 gcc_assert (vectype
);
1791 /* The rightmost bits of an aligned address must be zeros.
1792 Construct the mask needed for this test. For example,
1793 GET_MODE_SIZE for the vector mode V4SI is 16 bytes so the
1794 mask must be 15 = 0xf. */
1795 mask
= GET_MODE_SIZE (TYPE_MODE (vectype
)) - 1;
1797 /* FORNOW: use the same mask to test all potentially unaligned
1798 references in the loop. The vectorizer currently supports
1799 a single vector size, see the reference to
1800 GET_MODE_NUNITS (TYPE_MODE (vectype)) where the
1801 vectorization factor is computed. */
1802 gcc_assert (!LOOP_VINFO_PTR_MASK (loop_vinfo
)
1803 || LOOP_VINFO_PTR_MASK (loop_vinfo
) == mask
);
1804 LOOP_VINFO_PTR_MASK (loop_vinfo
) = mask
;
1805 LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo
).safe_push (
1810 /* Versioning requires at least one misaligned data reference. */
1811 if (!LOOP_REQUIRES_VERSIONING_FOR_ALIGNMENT (loop_vinfo
))
1812 do_versioning
= false;
1813 else if (!do_versioning
)
1814 LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo
).truncate (0);
1819 vec
<gimple
> may_misalign_stmts
1820 = LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo
);
1823 /* It can now be assumed that the data references in the statements
1824 in LOOP_VINFO_MAY_MISALIGN_STMTS will be aligned in the version
1825 of the loop being vectorized. */
1826 FOR_EACH_VEC_ELT (may_misalign_stmts
, i
, stmt
)
1828 stmt_vec_info stmt_info
= vinfo_for_stmt (stmt
);
1829 dr
= STMT_VINFO_DATA_REF (stmt_info
);
1830 SET_DR_MISALIGNMENT (dr
, 0);
1831 if (dump_enabled_p ())
1832 dump_printf_loc (MSG_NOTE
, vect_location
,
1833 "Alignment of access forced using versioning.\n");
1836 if (dump_enabled_p ())
1837 dump_printf_loc (MSG_NOTE
, vect_location
,
1838 "Versioning for alignment will be applied.\n");
1840 /* Peeling and versioning can't be done together at this time. */
1841 gcc_assert (! (do_peeling
&& do_versioning
));
1843 stat
= vect_verify_datarefs_alignment (loop_vinfo
, NULL
);
1848 /* This point is reached if neither peeling nor versioning is being done. */
1849 gcc_assert (! (do_peeling
|| do_versioning
));
1851 stat
= vect_verify_datarefs_alignment (loop_vinfo
, NULL
);
1856 /* Function vect_find_same_alignment_drs.
1858 Update group and alignment relations according to the chosen
1859 vectorization factor. */
1862 vect_find_same_alignment_drs (struct data_dependence_relation
*ddr
,
1863 loop_vec_info loop_vinfo
)
1866 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
1867 int vectorization_factor
= LOOP_VINFO_VECT_FACTOR (loop_vinfo
);
1868 struct data_reference
*dra
= DDR_A (ddr
);
1869 struct data_reference
*drb
= DDR_B (ddr
);
1870 stmt_vec_info stmtinfo_a
= vinfo_for_stmt (DR_STMT (dra
));
1871 stmt_vec_info stmtinfo_b
= vinfo_for_stmt (DR_STMT (drb
));
1872 int dra_size
= GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (DR_REF (dra
))));
1873 int drb_size
= GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (DR_REF (drb
))));
1874 lambda_vector dist_v
;
1875 unsigned int loop_depth
;
1877 if (DDR_ARE_DEPENDENT (ddr
) == chrec_known
)
1883 if (DDR_ARE_DEPENDENT (ddr
) == chrec_dont_know
)
1886 /* Loop-based vectorization and known data dependence. */
1887 if (DDR_NUM_DIST_VECTS (ddr
) == 0)
1890 /* Data-dependence analysis reports a distance vector of zero
1891 for data-references that overlap only in the first iteration
1892 but have different sign step (see PR45764).
1893 So as a sanity check require equal DR_STEP. */
1894 if (!operand_equal_p (DR_STEP (dra
), DR_STEP (drb
), 0))
1897 loop_depth
= index_in_loop_nest (loop
->num
, DDR_LOOP_NEST (ddr
));
1898 FOR_EACH_VEC_ELT (DDR_DIST_VECTS (ddr
), i
, dist_v
)
1900 int dist
= dist_v
[loop_depth
];
1902 if (dump_enabled_p ())
1903 dump_printf_loc (MSG_NOTE
, vect_location
,
1904 "dependence distance = %d.\n", dist
);
1906 /* Same loop iteration. */
1908 || (dist
% vectorization_factor
== 0 && dra_size
== drb_size
))
1910 /* Two references with distance zero have the same alignment. */
1911 STMT_VINFO_SAME_ALIGN_REFS (stmtinfo_a
).safe_push (drb
);
1912 STMT_VINFO_SAME_ALIGN_REFS (stmtinfo_b
).safe_push (dra
);
1913 if (dump_enabled_p ())
1915 dump_printf_loc (MSG_NOTE
, vect_location
,
1916 "accesses have the same alignment.\n");
1917 dump_printf (MSG_NOTE
,
1918 "dependence distance modulo vf == 0 between ");
1919 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, DR_REF (dra
));
1920 dump_printf (MSG_NOTE
, " and ");
1921 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, DR_REF (drb
));
1922 dump_printf (MSG_NOTE
, "\n");
1929 /* Function vect_analyze_data_refs_alignment
1931 Analyze the alignment of the data-references in the loop.
1932 Return FALSE if a data reference is found that cannot be vectorized. */
1935 vect_analyze_data_refs_alignment (loop_vec_info loop_vinfo
,
1936 bb_vec_info bb_vinfo
)
1938 if (dump_enabled_p ())
1939 dump_printf_loc (MSG_NOTE
, vect_location
,
1940 "=== vect_analyze_data_refs_alignment ===\n");
1942 /* Mark groups of data references with same alignment using
1943 data dependence information. */
1946 vec
<ddr_p
> ddrs
= LOOP_VINFO_DDRS (loop_vinfo
);
1947 struct data_dependence_relation
*ddr
;
1950 FOR_EACH_VEC_ELT (ddrs
, i
, ddr
)
1951 vect_find_same_alignment_drs (ddr
, loop_vinfo
);
1954 if (!vect_compute_data_refs_alignment (loop_vinfo
, bb_vinfo
))
1956 if (dump_enabled_p ())
1957 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1958 "not vectorized: can't calculate alignment "
1967 /* Analyze groups of accesses: check that DR belongs to a group of
1968 accesses of legal size, step, etc. Detect gaps, single element
1969 interleaving, and other special cases. Set grouped access info.
1970 Collect groups of strided stores for further use in SLP analysis. */
1973 vect_analyze_group_access (struct data_reference
*dr
)
1975 tree step
= DR_STEP (dr
);
1976 tree scalar_type
= TREE_TYPE (DR_REF (dr
));
1977 HOST_WIDE_INT type_size
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (scalar_type
));
1978 gimple stmt
= DR_STMT (dr
);
1979 stmt_vec_info stmt_info
= vinfo_for_stmt (stmt
);
1980 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_info
);
1981 bb_vec_info bb_vinfo
= STMT_VINFO_BB_VINFO (stmt_info
);
1982 HOST_WIDE_INT dr_step
= TREE_INT_CST_LOW (step
);
1983 HOST_WIDE_INT groupsize
, last_accessed_element
= 1;
1984 bool slp_impossible
= false;
1985 struct loop
*loop
= NULL
;
1988 loop
= LOOP_VINFO_LOOP (loop_vinfo
);
1990 /* For interleaving, GROUPSIZE is STEP counted in elements, i.e., the
1991 size of the interleaving group (including gaps). */
1992 groupsize
= absu_hwi (dr_step
) / type_size
;
1994 /* Not consecutive access is possible only if it is a part of interleaving. */
1995 if (!GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt
)))
1997 /* Check if it this DR is a part of interleaving, and is a single
1998 element of the group that is accessed in the loop. */
2000 /* Gaps are supported only for loads. STEP must be a multiple of the type
2001 size. The size of the group must be a power of 2. */
2003 && (dr_step
% type_size
) == 0
2005 && exact_log2 (groupsize
) != -1)
2007 GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt
)) = stmt
;
2008 GROUP_SIZE (vinfo_for_stmt (stmt
)) = groupsize
;
2009 if (dump_enabled_p ())
2011 dump_printf_loc (MSG_NOTE
, vect_location
,
2012 "Detected single element interleaving ");
2013 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, DR_REF (dr
));
2014 dump_printf (MSG_NOTE
, " step ");
2015 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, step
);
2016 dump_printf (MSG_NOTE
, "\n");
2021 if (dump_enabled_p ())
2022 dump_printf_loc (MSG_NOTE
, vect_location
,
2023 "Data access with gaps requires scalar "
2027 if (dump_enabled_p ())
2028 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2029 "Peeling for outer loop is not"
2034 LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo
) = true;
2040 if (dump_enabled_p ())
2042 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2043 "not consecutive access ");
2044 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
, stmt
, 0);
2045 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
2050 /* Mark the statement as unvectorizable. */
2051 STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dr
))) = false;
2058 if (GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt
)) == stmt
)
2060 /* First stmt in the interleaving chain. Check the chain. */
2061 gimple next
= GROUP_NEXT_ELEMENT (vinfo_for_stmt (stmt
));
2062 struct data_reference
*data_ref
= dr
;
2063 unsigned int count
= 1;
2064 tree prev_init
= DR_INIT (data_ref
);
2066 HOST_WIDE_INT diff
, gaps
= 0;
2067 unsigned HOST_WIDE_INT count_in_bytes
;
2071 /* Skip same data-refs. In case that two or more stmts share
2072 data-ref (supported only for loads), we vectorize only the first
2073 stmt, and the rest get their vectorized loads from the first
2075 if (!tree_int_cst_compare (DR_INIT (data_ref
),
2076 DR_INIT (STMT_VINFO_DATA_REF (
2077 vinfo_for_stmt (next
)))))
2079 if (DR_IS_WRITE (data_ref
))
2081 if (dump_enabled_p ())
2082 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2083 "Two store stmts share the same dr.\n");
2087 /* For load use the same data-ref load. */
2088 GROUP_SAME_DR_STMT (vinfo_for_stmt (next
)) = prev
;
2091 next
= GROUP_NEXT_ELEMENT (vinfo_for_stmt (next
));
2096 data_ref
= STMT_VINFO_DATA_REF (vinfo_for_stmt (next
));
2098 /* All group members have the same STEP by construction. */
2099 gcc_checking_assert (operand_equal_p (DR_STEP (data_ref
), step
, 0));
2101 /* Check that the distance between two accesses is equal to the type
2102 size. Otherwise, we have gaps. */
2103 diff
= (TREE_INT_CST_LOW (DR_INIT (data_ref
))
2104 - TREE_INT_CST_LOW (prev_init
)) / type_size
;
2107 /* FORNOW: SLP of accesses with gaps is not supported. */
2108 slp_impossible
= true;
2109 if (DR_IS_WRITE (data_ref
))
2111 if (dump_enabled_p ())
2112 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2113 "interleaved store with gaps\n");
2120 last_accessed_element
+= diff
;
2122 /* Store the gap from the previous member of the group. If there is no
2123 gap in the access, GROUP_GAP is always 1. */
2124 GROUP_GAP (vinfo_for_stmt (next
)) = diff
;
2126 prev_init
= DR_INIT (data_ref
);
2127 next
= GROUP_NEXT_ELEMENT (vinfo_for_stmt (next
));
2128 /* Count the number of data-refs in the chain. */
2132 /* COUNT is the number of accesses found, we multiply it by the size of
2133 the type to get COUNT_IN_BYTES. */
2134 count_in_bytes
= type_size
* count
;
2136 /* Check that the size of the interleaving (including gaps) is not
2137 greater than STEP. */
2139 && absu_hwi (dr_step
) < count_in_bytes
+ gaps
* type_size
)
2141 if (dump_enabled_p ())
2143 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2144 "interleaving size is greater than step for ");
2145 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
,
2147 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
2152 /* Check that the size of the interleaving is equal to STEP for stores,
2153 i.e., that there are no gaps. */
2155 && absu_hwi (dr_step
) != count_in_bytes
)
2157 if (DR_IS_READ (dr
))
2159 slp_impossible
= true;
2160 /* There is a gap after the last load in the group. This gap is a
2161 difference between the groupsize and the number of elements.
2162 When there is no gap, this difference should be 0. */
2163 GROUP_GAP (vinfo_for_stmt (stmt
)) = groupsize
- count
;
2167 if (dump_enabled_p ())
2168 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2169 "interleaved store with gaps\n");
2174 /* Check that STEP is a multiple of type size. */
2176 && (dr_step
% type_size
) != 0)
2178 if (dump_enabled_p ())
2180 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2181 "step is not a multiple of type size: step ");
2182 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
, step
);
2183 dump_printf (MSG_MISSED_OPTIMIZATION
, " size ");
2184 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
,
2185 TYPE_SIZE_UNIT (scalar_type
));
2186 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
2194 GROUP_SIZE (vinfo_for_stmt (stmt
)) = groupsize
;
2195 if (dump_enabled_p ())
2196 dump_printf_loc (MSG_NOTE
, vect_location
,
2197 "Detected interleaving of size %d\n", (int)groupsize
);
2199 /* SLP: create an SLP data structure for every interleaving group of
2200 stores for further analysis in vect_analyse_slp. */
2201 if (DR_IS_WRITE (dr
) && !slp_impossible
)
2204 LOOP_VINFO_GROUPED_STORES (loop_vinfo
).safe_push (stmt
);
2206 BB_VINFO_GROUPED_STORES (bb_vinfo
).safe_push (stmt
);
2209 /* There is a gap in the end of the group. */
2210 if (groupsize
- last_accessed_element
> 0 && loop_vinfo
)
2212 if (dump_enabled_p ())
2213 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2214 "Data access with gaps requires scalar "
2218 if (dump_enabled_p ())
2219 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2220 "Peeling for outer loop is not supported\n");
2224 LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo
) = true;
2232 /* Analyze the access pattern of the data-reference DR.
2233 In case of non-consecutive accesses call vect_analyze_group_access() to
2234 analyze groups of accesses. */
2237 vect_analyze_data_ref_access (struct data_reference
*dr
)
2239 tree step
= DR_STEP (dr
);
2240 tree scalar_type
= TREE_TYPE (DR_REF (dr
));
2241 gimple stmt
= DR_STMT (dr
);
2242 stmt_vec_info stmt_info
= vinfo_for_stmt (stmt
);
2243 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_info
);
2244 struct loop
*loop
= NULL
;
2247 loop
= LOOP_VINFO_LOOP (loop_vinfo
);
2249 if (loop_vinfo
&& !step
)
2251 if (dump_enabled_p ())
2252 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2253 "bad data-ref access in loop\n");
2257 /* Allow invariant loads in not nested loops. */
2258 if (loop_vinfo
&& integer_zerop (step
))
2260 GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt
)) = NULL
;
2261 if (nested_in_vect_loop_p (loop
, stmt
))
2263 if (dump_enabled_p ())
2264 dump_printf_loc (MSG_NOTE
, vect_location
,
2265 "zero step in inner loop of nest\n");
2268 return DR_IS_READ (dr
);
2271 if (loop
&& nested_in_vect_loop_p (loop
, stmt
))
2273 /* Interleaved accesses are not yet supported within outer-loop
2274 vectorization for references in the inner-loop. */
2275 GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt
)) = NULL
;
2277 /* For the rest of the analysis we use the outer-loop step. */
2278 step
= STMT_VINFO_DR_STEP (stmt_info
);
2279 if (integer_zerop (step
))
2281 if (dump_enabled_p ())
2282 dump_printf_loc (MSG_NOTE
, vect_location
,
2283 "zero step in outer loop.\n");
2284 if (DR_IS_READ (dr
))
2292 if (TREE_CODE (step
) == INTEGER_CST
)
2294 HOST_WIDE_INT dr_step
= TREE_INT_CST_LOW (step
);
2295 if (!tree_int_cst_compare (step
, TYPE_SIZE_UNIT (scalar_type
))
2297 && !compare_tree_int (TYPE_SIZE_UNIT (scalar_type
), -dr_step
)))
2299 /* Mark that it is not interleaving. */
2300 GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt
)) = NULL
;
2305 if (loop
&& nested_in_vect_loop_p (loop
, stmt
))
2307 if (dump_enabled_p ())
2308 dump_printf_loc (MSG_NOTE
, vect_location
,
2309 "grouped access in outer loop.\n");
2313 /* Assume this is a DR handled by non-constant strided load case. */
2314 if (TREE_CODE (step
) != INTEGER_CST
)
2315 return STMT_VINFO_STRIDE_LOAD_P (stmt_info
);
2317 /* Not consecutive access - check if it's a part of interleaving group. */
2318 return vect_analyze_group_access (dr
);
2323 /* A helper function used in the comparator function to sort data
2324 references. T1 and T2 are two data references to be compared.
2325 The function returns -1, 0, or 1. */
2328 compare_tree (tree t1
, tree t2
)
2331 enum tree_code code
;
2342 if (TREE_CODE (t1
) != TREE_CODE (t2
))
2343 return TREE_CODE (t1
) < TREE_CODE (t2
) ? -1 : 1;
2345 code
= TREE_CODE (t1
);
2348 /* For const values, we can just use hash values for comparisons. */
2356 hashval_t h1
= iterative_hash_expr (t1
, 0);
2357 hashval_t h2
= iterative_hash_expr (t2
, 0);
2359 return h1
< h2
? -1 : 1;
2364 cmp
= compare_tree (SSA_NAME_VAR (t1
), SSA_NAME_VAR (t2
));
2368 if (SSA_NAME_VERSION (t1
) != SSA_NAME_VERSION (t2
))
2369 return SSA_NAME_VERSION (t1
) < SSA_NAME_VERSION (t2
) ? -1 : 1;
2373 tclass
= TREE_CODE_CLASS (code
);
2375 /* For var-decl, we could compare their UIDs. */
2376 if (tclass
== tcc_declaration
)
2378 if (DECL_UID (t1
) != DECL_UID (t2
))
2379 return DECL_UID (t1
) < DECL_UID (t2
) ? -1 : 1;
2383 /* For expressions with operands, compare their operands recursively. */
2384 for (i
= TREE_OPERAND_LENGTH (t1
) - 1; i
>= 0; --i
)
2386 cmp
= compare_tree (TREE_OPERAND (t1
, i
), TREE_OPERAND (t2
, i
));
2396 /* Compare two data-references DRA and DRB to group them into chunks
2397 suitable for grouping. */
2400 dr_group_sort_cmp (const void *dra_
, const void *drb_
)
2402 data_reference_p dra
= *(data_reference_p
*)const_cast<void *>(dra_
);
2403 data_reference_p drb
= *(data_reference_p
*)const_cast<void *>(drb_
);
2406 /* Stabilize sort. */
2410 /* Ordering of DRs according to base. */
2411 if (!operand_equal_p (DR_BASE_ADDRESS (dra
), DR_BASE_ADDRESS (drb
), 0))
2413 cmp
= compare_tree (DR_BASE_ADDRESS (dra
), DR_BASE_ADDRESS (drb
));
2418 /* And according to DR_OFFSET. */
2419 if (!dr_equal_offsets_p (dra
, drb
))
2421 cmp
= compare_tree (DR_OFFSET (dra
), DR_OFFSET (drb
));
2426 /* Put reads before writes. */
2427 if (DR_IS_READ (dra
) != DR_IS_READ (drb
))
2428 return DR_IS_READ (dra
) ? -1 : 1;
2430 /* Then sort after access size. */
2431 if (!operand_equal_p (TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dra
))),
2432 TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (drb
))), 0))
2434 cmp
= compare_tree (TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dra
))),
2435 TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (drb
))));
2440 /* And after step. */
2441 if (!operand_equal_p (DR_STEP (dra
), DR_STEP (drb
), 0))
2443 cmp
= compare_tree (DR_STEP (dra
), DR_STEP (drb
));
2448 /* Then sort after DR_INIT. In case of identical DRs sort after stmt UID. */
2449 cmp
= tree_int_cst_compare (DR_INIT (dra
), DR_INIT (drb
));
2451 return gimple_uid (DR_STMT (dra
)) < gimple_uid (DR_STMT (drb
)) ? -1 : 1;
2455 /* Function vect_analyze_data_ref_accesses.
2457 Analyze the access pattern of all the data references in the loop.
2459 FORNOW: the only access pattern that is considered vectorizable is a
2460 simple step 1 (consecutive) access.
2462 FORNOW: handle only arrays and pointer accesses. */
2465 vect_analyze_data_ref_accesses (loop_vec_info loop_vinfo
, bb_vec_info bb_vinfo
)
2468 vec
<data_reference_p
> datarefs
;
2469 struct data_reference
*dr
;
2471 if (dump_enabled_p ())
2472 dump_printf_loc (MSG_NOTE
, vect_location
,
2473 "=== vect_analyze_data_ref_accesses ===\n");
2476 datarefs
= LOOP_VINFO_DATAREFS (loop_vinfo
);
2478 datarefs
= BB_VINFO_DATAREFS (bb_vinfo
);
2480 if (datarefs
.is_empty ())
2483 /* Sort the array of datarefs to make building the interleaving chains
2485 qsort (datarefs
.address (), datarefs
.length (),
2486 sizeof (data_reference_p
), dr_group_sort_cmp
);
2488 /* Build the interleaving chains. */
2489 for (i
= 0; i
< datarefs
.length () - 1;)
2491 data_reference_p dra
= datarefs
[i
];
2492 stmt_vec_info stmtinfo_a
= vinfo_for_stmt (DR_STMT (dra
));
2493 stmt_vec_info lastinfo
= NULL
;
2494 for (i
= i
+ 1; i
< datarefs
.length (); ++i
)
2496 data_reference_p drb
= datarefs
[i
];
2497 stmt_vec_info stmtinfo_b
= vinfo_for_stmt (DR_STMT (drb
));
2499 /* ??? Imperfect sorting (non-compatible types, non-modulo
2500 accesses, same accesses) can lead to a group to be artificially
2501 split here as we don't just skip over those. If it really
2502 matters we can push those to a worklist and re-iterate
2503 over them. The we can just skip ahead to the next DR here. */
2505 /* Check that the data-refs have same first location (except init)
2506 and they are both either store or load (not load and store). */
2507 if (DR_IS_READ (dra
) != DR_IS_READ (drb
)
2508 || !operand_equal_p (DR_BASE_ADDRESS (dra
),
2509 DR_BASE_ADDRESS (drb
), 0)
2510 || !dr_equal_offsets_p (dra
, drb
))
2513 /* Check that the data-refs have the same constant size and step. */
2514 tree sza
= TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dra
)));
2515 tree szb
= TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (drb
)));
2516 if (!tree_fits_uhwi_p (sza
)
2517 || !tree_fits_uhwi_p (szb
)
2518 || !tree_int_cst_equal (sza
, szb
)
2519 || !tree_fits_shwi_p (DR_STEP (dra
))
2520 || !tree_fits_shwi_p (DR_STEP (drb
))
2521 || !tree_int_cst_equal (DR_STEP (dra
), DR_STEP (drb
)))
2524 /* Do not place the same access in the interleaving chain twice. */
2525 if (tree_int_cst_compare (DR_INIT (dra
), DR_INIT (drb
)) == 0)
2528 /* Check the types are compatible.
2529 ??? We don't distinguish this during sorting. */
2530 if (!types_compatible_p (TREE_TYPE (DR_REF (dra
)),
2531 TREE_TYPE (DR_REF (drb
))))
2534 /* Sorting has ensured that DR_INIT (dra) <= DR_INIT (drb). */
2535 HOST_WIDE_INT init_a
= TREE_INT_CST_LOW (DR_INIT (dra
));
2536 HOST_WIDE_INT init_b
= TREE_INT_CST_LOW (DR_INIT (drb
));
2537 gcc_assert (init_a
< init_b
);
2539 /* If init_b == init_a + the size of the type * k, we have an
2540 interleaving, and DRA is accessed before DRB. */
2541 HOST_WIDE_INT type_size_a
= tree_to_uhwi (sza
);
2542 if ((init_b
- init_a
) % type_size_a
!= 0)
2545 /* The step (if not zero) is greater than the difference between
2546 data-refs' inits. This splits groups into suitable sizes. */
2547 HOST_WIDE_INT step
= tree_to_shwi (DR_STEP (dra
));
2548 if (step
!= 0 && step
<= (init_b
- init_a
))
2551 if (dump_enabled_p ())
2553 dump_printf_loc (MSG_NOTE
, vect_location
,
2554 "Detected interleaving ");
2555 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, DR_REF (dra
));
2556 dump_printf (MSG_NOTE
, " and ");
2557 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, DR_REF (drb
));
2558 dump_printf (MSG_NOTE
, "\n");
2561 /* Link the found element into the group list. */
2562 if (!GROUP_FIRST_ELEMENT (stmtinfo_a
))
2564 GROUP_FIRST_ELEMENT (stmtinfo_a
) = DR_STMT (dra
);
2565 lastinfo
= stmtinfo_a
;
2567 GROUP_FIRST_ELEMENT (stmtinfo_b
) = DR_STMT (dra
);
2568 GROUP_NEXT_ELEMENT (lastinfo
) = DR_STMT (drb
);
2569 lastinfo
= stmtinfo_b
;
2573 FOR_EACH_VEC_ELT (datarefs
, i
, dr
)
2574 if (STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dr
)))
2575 && !vect_analyze_data_ref_access (dr
))
2577 if (dump_enabled_p ())
2578 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2579 "not vectorized: complicated access pattern.\n");
2583 /* Mark the statement as not vectorizable. */
2584 STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dr
))) = false;
2595 /* Operator == between two dr_with_seg_len objects.
2597 This equality operator is used to make sure two data refs
2598 are the same one so that we will consider to combine the
2599 aliasing checks of those two pairs of data dependent data
2603 operator == (const dr_with_seg_len
& d1
,
2604 const dr_with_seg_len
& d2
)
2606 return operand_equal_p (DR_BASE_ADDRESS (d1
.dr
),
2607 DR_BASE_ADDRESS (d2
.dr
), 0)
2608 && compare_tree (d1
.offset
, d2
.offset
) == 0
2609 && compare_tree (d1
.seg_len
, d2
.seg_len
) == 0;
2612 /* Function comp_dr_with_seg_len_pair.
2614 Comparison function for sorting objects of dr_with_seg_len_pair_t
2615 so that we can combine aliasing checks in one scan. */
2618 comp_dr_with_seg_len_pair (const void *p1_
, const void *p2_
)
2620 const dr_with_seg_len_pair_t
* p1
= (const dr_with_seg_len_pair_t
*) p1_
;
2621 const dr_with_seg_len_pair_t
* p2
= (const dr_with_seg_len_pair_t
*) p2_
;
2623 const dr_with_seg_len
&p11
= p1
->first
,
2628 /* For DR pairs (a, b) and (c, d), we only consider to merge the alias checks
2629 if a and c have the same basic address snd step, and b and d have the same
2630 address and step. Therefore, if any a&c or b&d don't have the same address
2631 and step, we don't care the order of those two pairs after sorting. */
2634 if ((comp_res
= compare_tree (DR_BASE_ADDRESS (p11
.dr
),
2635 DR_BASE_ADDRESS (p21
.dr
))) != 0)
2637 if ((comp_res
= compare_tree (DR_BASE_ADDRESS (p12
.dr
),
2638 DR_BASE_ADDRESS (p22
.dr
))) != 0)
2640 if ((comp_res
= compare_tree (DR_STEP (p11
.dr
), DR_STEP (p21
.dr
))) != 0)
2642 if ((comp_res
= compare_tree (DR_STEP (p12
.dr
), DR_STEP (p22
.dr
))) != 0)
2644 if ((comp_res
= compare_tree (p11
.offset
, p21
.offset
)) != 0)
2646 if ((comp_res
= compare_tree (p12
.offset
, p22
.offset
)) != 0)
2652 template <class T
> static void
2660 /* Function vect_vfa_segment_size.
2662 Create an expression that computes the size of segment
2663 that will be accessed for a data reference. The functions takes into
2664 account that realignment loads may access one more vector.
2667 DR: The data reference.
2668 LENGTH_FACTOR: segment length to consider.
2670 Return an expression whose value is the size of segment which will be
2674 vect_vfa_segment_size (struct data_reference
*dr
, tree length_factor
)
2676 tree segment_length
;
2678 if (integer_zerop (DR_STEP (dr
)))
2679 segment_length
= TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dr
)));
2681 segment_length
= size_binop (MULT_EXPR
,
2682 fold_convert (sizetype
, DR_STEP (dr
)),
2683 fold_convert (sizetype
, length_factor
));
2685 if (vect_supportable_dr_alignment (dr
, false)
2686 == dr_explicit_realign_optimized
)
2688 tree vector_size
= TYPE_SIZE_UNIT
2689 (STMT_VINFO_VECTYPE (vinfo_for_stmt (DR_STMT (dr
))));
2691 segment_length
= size_binop (PLUS_EXPR
, segment_length
, vector_size
);
2693 return segment_length
;
2696 /* Function vect_prune_runtime_alias_test_list.
2698 Prune a list of ddrs to be tested at run-time by versioning for alias.
2699 Merge several alias checks into one if possible.
2700 Return FALSE if resulting list of ddrs is longer then allowed by
2701 PARAM_VECT_MAX_VERSION_FOR_ALIAS_CHECKS, otherwise return TRUE. */
2704 vect_prune_runtime_alias_test_list (loop_vec_info loop_vinfo
)
2706 vec
<ddr_p
> may_alias_ddrs
=
2707 LOOP_VINFO_MAY_ALIAS_DDRS (loop_vinfo
);
2708 vec
<dr_with_seg_len_pair_t
>& comp_alias_ddrs
=
2709 LOOP_VINFO_COMP_ALIAS_DDRS (loop_vinfo
);
2710 int vect_factor
= LOOP_VINFO_VECT_FACTOR (loop_vinfo
);
2711 tree scalar_loop_iters
= LOOP_VINFO_NITERS (loop_vinfo
);
2717 if (dump_enabled_p ())
2718 dump_printf_loc (MSG_NOTE
, vect_location
,
2719 "=== vect_prune_runtime_alias_test_list ===\n");
2721 if (may_alias_ddrs
.is_empty ())
2724 /* Basically, for each pair of dependent data refs store_ptr_0
2725 and load_ptr_0, we create an expression:
2727 ((store_ptr_0 + store_segment_length_0) <= load_ptr_0)
2728 || (load_ptr_0 + load_segment_length_0) <= store_ptr_0))
2730 for aliasing checks. However, in some cases we can decrease
2731 the number of checks by combining two checks into one. For
2732 example, suppose we have another pair of data refs store_ptr_0
2733 and load_ptr_1, and if the following condition is satisfied:
2735 load_ptr_0 < load_ptr_1 &&
2736 load_ptr_1 - load_ptr_0 - load_segment_length_0 < store_segment_length_0
2738 (this condition means, in each iteration of vectorized loop,
2739 the accessed memory of store_ptr_0 cannot be between the memory
2740 of load_ptr_0 and load_ptr_1.)
2742 we then can use only the following expression to finish the
2743 alising checks between store_ptr_0 & load_ptr_0 and
2744 store_ptr_0 & load_ptr_1:
2746 ((store_ptr_0 + store_segment_length_0) <= load_ptr_0)
2747 || (load_ptr_1 + load_segment_length_1 <= store_ptr_0))
2749 Note that we only consider that load_ptr_0 and load_ptr_1 have the
2750 same basic address. */
2752 comp_alias_ddrs
.create (may_alias_ddrs
.length ());
2754 /* First, we collect all data ref pairs for aliasing checks. */
2755 FOR_EACH_VEC_ELT (may_alias_ddrs
, i
, ddr
)
2757 struct data_reference
*dr_a
, *dr_b
;
2758 gimple dr_group_first_a
, dr_group_first_b
;
2759 tree segment_length_a
, segment_length_b
;
2760 gimple stmt_a
, stmt_b
;
2763 stmt_a
= DR_STMT (DDR_A (ddr
));
2764 dr_group_first_a
= GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt_a
));
2765 if (dr_group_first_a
)
2767 stmt_a
= dr_group_first_a
;
2768 dr_a
= STMT_VINFO_DATA_REF (vinfo_for_stmt (stmt_a
));
2772 stmt_b
= DR_STMT (DDR_B (ddr
));
2773 dr_group_first_b
= GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt_b
));
2774 if (dr_group_first_b
)
2776 stmt_b
= dr_group_first_b
;
2777 dr_b
= STMT_VINFO_DATA_REF (vinfo_for_stmt (stmt_b
));
2780 if (!operand_equal_p (DR_STEP (dr_a
), DR_STEP (dr_b
), 0))
2781 length_factor
= scalar_loop_iters
;
2783 length_factor
= size_int (vect_factor
);
2784 segment_length_a
= vect_vfa_segment_size (dr_a
, length_factor
);
2785 segment_length_b
= vect_vfa_segment_size (dr_b
, length_factor
);
2787 dr_with_seg_len_pair_t dr_with_seg_len_pair
2788 (dr_with_seg_len (dr_a
, segment_length_a
),
2789 dr_with_seg_len (dr_b
, segment_length_b
));
2791 if (compare_tree (DR_BASE_ADDRESS (dr_a
), DR_BASE_ADDRESS (dr_b
)) > 0)
2792 swap (dr_with_seg_len_pair
.first
, dr_with_seg_len_pair
.second
);
2794 comp_alias_ddrs
.safe_push (dr_with_seg_len_pair
);
2797 /* Second, we sort the collected data ref pairs so that we can scan
2798 them once to combine all possible aliasing checks. */
2799 comp_alias_ddrs
.qsort (comp_dr_with_seg_len_pair
);
2801 /* Third, we scan the sorted dr pairs and check if we can combine
2802 alias checks of two neighbouring dr pairs. */
2803 for (size_t i
= 1; i
< comp_alias_ddrs
.length (); ++i
)
2805 /* Deal with two ddrs (dr_a1, dr_b1) and (dr_a2, dr_b2). */
2806 dr_with_seg_len
*dr_a1
= &comp_alias_ddrs
[i
-1].first
,
2807 *dr_b1
= &comp_alias_ddrs
[i
-1].second
,
2808 *dr_a2
= &comp_alias_ddrs
[i
].first
,
2809 *dr_b2
= &comp_alias_ddrs
[i
].second
;
2811 /* Remove duplicate data ref pairs. */
2812 if (*dr_a1
== *dr_a2
&& *dr_b1
== *dr_b2
)
2814 if (dump_enabled_p ())
2816 dump_printf_loc (MSG_NOTE
, vect_location
,
2817 "found equal ranges ");
2818 dump_generic_expr (MSG_NOTE
, TDF_SLIM
,
2819 DR_REF (dr_a1
->dr
));
2820 dump_printf (MSG_NOTE
, ", ");
2821 dump_generic_expr (MSG_NOTE
, TDF_SLIM
,
2822 DR_REF (dr_b1
->dr
));
2823 dump_printf (MSG_NOTE
, " and ");
2824 dump_generic_expr (MSG_NOTE
, TDF_SLIM
,
2825 DR_REF (dr_a2
->dr
));
2826 dump_printf (MSG_NOTE
, ", ");
2827 dump_generic_expr (MSG_NOTE
, TDF_SLIM
,
2828 DR_REF (dr_b2
->dr
));
2829 dump_printf (MSG_NOTE
, "\n");
2832 comp_alias_ddrs
.ordered_remove (i
--);
2836 if (*dr_a1
== *dr_a2
|| *dr_b1
== *dr_b2
)
2838 /* We consider the case that DR_B1 and DR_B2 are same memrefs,
2839 and DR_A1 and DR_A2 are two consecutive memrefs. */
2840 if (*dr_a1
== *dr_a2
)
2842 swap (dr_a1
, dr_b1
);
2843 swap (dr_a2
, dr_b2
);
2846 if (!operand_equal_p (DR_BASE_ADDRESS (dr_a1
->dr
),
2847 DR_BASE_ADDRESS (dr_a2
->dr
),
2849 || !tree_fits_shwi_p (dr_a1
->offset
)
2850 || !tree_fits_shwi_p (dr_a2
->offset
))
2853 HOST_WIDE_INT diff
= (tree_to_shwi (dr_a2
->offset
)
2854 - tree_to_shwi (dr_a1
->offset
));
2857 /* Now we check if the following condition is satisfied:
2859 DIFF - SEGMENT_LENGTH_A < SEGMENT_LENGTH_B
2861 where DIFF = DR_A2->OFFSET - DR_A1->OFFSET. However,
2862 SEGMENT_LENGTH_A or SEGMENT_LENGTH_B may not be constant so we
2863 have to make a best estimation. We can get the minimum value
2864 of SEGMENT_LENGTH_B as a constant, represented by MIN_SEG_LEN_B,
2865 then either of the following two conditions can guarantee the
2868 1: DIFF <= MIN_SEG_LEN_B
2869 2: DIFF - SEGMENT_LENGTH_A < MIN_SEG_LEN_B
2874 min_seg_len_b
= (TREE_CODE (dr_b1
->seg_len
) == INTEGER_CST
) ?
2875 TREE_INT_CST_LOW (dr_b1
->seg_len
) :
2878 if (diff
<= min_seg_len_b
2879 || (TREE_CODE (dr_a1
->seg_len
) == INTEGER_CST
2880 && diff
- (HOST_WIDE_INT
) TREE_INT_CST_LOW (dr_a1
->seg_len
) <
2883 dr_a1
->seg_len
= size_binop (PLUS_EXPR
,
2884 dr_a2
->seg_len
, size_int (diff
));
2885 comp_alias_ddrs
.ordered_remove (i
--);
2890 if ((int) comp_alias_ddrs
.length () >
2891 PARAM_VALUE (PARAM_VECT_MAX_VERSION_FOR_ALIAS_CHECKS
))
2893 if (dump_enabled_p ())
2895 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2896 "disable versioning for alias - max number of "
2897 "generated checks exceeded.\n");
2906 /* Check whether a non-affine read in stmt is suitable for gather load
2907 and if so, return a builtin decl for that operation. */
2910 vect_check_gather (gimple stmt
, loop_vec_info loop_vinfo
, tree
*basep
,
2911 tree
*offp
, int *scalep
)
2913 HOST_WIDE_INT scale
= 1, pbitpos
, pbitsize
;
2914 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
2915 stmt_vec_info stmt_info
= vinfo_for_stmt (stmt
);
2916 struct data_reference
*dr
= STMT_VINFO_DATA_REF (stmt_info
);
2917 tree offtype
= NULL_TREE
;
2918 tree decl
, base
, off
;
2919 enum machine_mode pmode
;
2920 int punsignedp
, pvolatilep
;
2923 /* For masked loads/stores, DR_REF (dr) is an artificial MEM_REF,
2924 see if we can use the def stmt of the address. */
2925 if (is_gimple_call (stmt
)
2926 && gimple_call_internal_p (stmt
)
2927 && (gimple_call_internal_fn (stmt
) == IFN_MASK_LOAD
2928 || gimple_call_internal_fn (stmt
) == IFN_MASK_STORE
)
2929 && TREE_CODE (base
) == MEM_REF
2930 && TREE_CODE (TREE_OPERAND (base
, 0)) == SSA_NAME
2931 && integer_zerop (TREE_OPERAND (base
, 1))
2932 && !expr_invariant_in_loop_p (loop
, TREE_OPERAND (base
, 0)))
2934 gimple def_stmt
= SSA_NAME_DEF_STMT (TREE_OPERAND (base
, 0));
2935 if (is_gimple_assign (def_stmt
)
2936 && gimple_assign_rhs_code (def_stmt
) == ADDR_EXPR
)
2937 base
= TREE_OPERAND (gimple_assign_rhs1 (def_stmt
), 0);
2940 /* The gather builtins need address of the form
2941 loop_invariant + vector * {1, 2, 4, 8}
2943 loop_invariant + sign_extend (vector) * { 1, 2, 4, 8 }.
2944 Unfortunately DR_BASE_ADDRESS/DR_OFFSET can be a mixture
2945 of loop invariants/SSA_NAMEs defined in the loop, with casts,
2946 multiplications and additions in it. To get a vector, we need
2947 a single SSA_NAME that will be defined in the loop and will
2948 contain everything that is not loop invariant and that can be
2949 vectorized. The following code attempts to find such a preexistng
2950 SSA_NAME OFF and put the loop invariants into a tree BASE
2951 that can be gimplified before the loop. */
2952 base
= get_inner_reference (base
, &pbitsize
, &pbitpos
, &off
,
2953 &pmode
, &punsignedp
, &pvolatilep
, false);
2954 gcc_assert (base
!= NULL_TREE
&& (pbitpos
% BITS_PER_UNIT
) == 0);
2956 if (TREE_CODE (base
) == MEM_REF
)
2958 if (!integer_zerop (TREE_OPERAND (base
, 1)))
2960 if (off
== NULL_TREE
)
2962 double_int moff
= mem_ref_offset (base
);
2963 off
= double_int_to_tree (sizetype
, moff
);
2966 off
= size_binop (PLUS_EXPR
, off
,
2967 fold_convert (sizetype
, TREE_OPERAND (base
, 1)));
2969 base
= TREE_OPERAND (base
, 0);
2972 base
= build_fold_addr_expr (base
);
2974 if (off
== NULL_TREE
)
2975 off
= size_zero_node
;
2977 /* If base is not loop invariant, either off is 0, then we start with just
2978 the constant offset in the loop invariant BASE and continue with base
2979 as OFF, otherwise give up.
2980 We could handle that case by gimplifying the addition of base + off
2981 into some SSA_NAME and use that as off, but for now punt. */
2982 if (!expr_invariant_in_loop_p (loop
, base
))
2984 if (!integer_zerop (off
))
2987 base
= size_int (pbitpos
/ BITS_PER_UNIT
);
2989 /* Otherwise put base + constant offset into the loop invariant BASE
2990 and continue with OFF. */
2993 base
= fold_convert (sizetype
, base
);
2994 base
= size_binop (PLUS_EXPR
, base
, size_int (pbitpos
/ BITS_PER_UNIT
));
2997 /* OFF at this point may be either a SSA_NAME or some tree expression
2998 from get_inner_reference. Try to peel off loop invariants from it
2999 into BASE as long as possible. */
3001 while (offtype
== NULL_TREE
)
3003 enum tree_code code
;
3004 tree op0
, op1
, add
= NULL_TREE
;
3006 if (TREE_CODE (off
) == SSA_NAME
)
3008 gimple def_stmt
= SSA_NAME_DEF_STMT (off
);
3010 if (expr_invariant_in_loop_p (loop
, off
))
3013 if (gimple_code (def_stmt
) != GIMPLE_ASSIGN
)
3016 op0
= gimple_assign_rhs1 (def_stmt
);
3017 code
= gimple_assign_rhs_code (def_stmt
);
3018 op1
= gimple_assign_rhs2 (def_stmt
);
3022 if (get_gimple_rhs_class (TREE_CODE (off
)) == GIMPLE_TERNARY_RHS
)
3024 code
= TREE_CODE (off
);
3025 extract_ops_from_tree (off
, &code
, &op0
, &op1
);
3029 case POINTER_PLUS_EXPR
:
3031 if (expr_invariant_in_loop_p (loop
, op0
))
3036 add
= fold_convert (sizetype
, add
);
3038 add
= size_binop (MULT_EXPR
, add
, size_int (scale
));
3039 base
= size_binop (PLUS_EXPR
, base
, add
);
3042 if (expr_invariant_in_loop_p (loop
, op1
))
3050 if (expr_invariant_in_loop_p (loop
, op1
))
3052 add
= fold_convert (sizetype
, op1
);
3053 add
= size_binop (MINUS_EXPR
, size_zero_node
, add
);
3059 if (scale
== 1 && tree_fits_shwi_p (op1
))
3061 scale
= tree_to_shwi (op1
);
3070 if (!POINTER_TYPE_P (TREE_TYPE (op0
))
3071 && !INTEGRAL_TYPE_P (TREE_TYPE (op0
)))
3073 if (TYPE_PRECISION (TREE_TYPE (op0
))
3074 == TYPE_PRECISION (TREE_TYPE (off
)))
3079 if (TYPE_PRECISION (TREE_TYPE (op0
))
3080 < TYPE_PRECISION (TREE_TYPE (off
)))
3083 offtype
= TREE_TYPE (off
);
3094 /* If at the end OFF still isn't a SSA_NAME or isn't
3095 defined in the loop, punt. */
3096 if (TREE_CODE (off
) != SSA_NAME
3097 || expr_invariant_in_loop_p (loop
, off
))
3100 if (offtype
== NULL_TREE
)
3101 offtype
= TREE_TYPE (off
);
3103 decl
= targetm
.vectorize
.builtin_gather (STMT_VINFO_VECTYPE (stmt_info
),
3105 if (decl
== NULL_TREE
)
3117 /* Function vect_analyze_data_refs.
3119 Find all the data references in the loop or basic block.
3121 The general structure of the analysis of data refs in the vectorizer is as
3123 1- vect_analyze_data_refs(loop/bb): call
3124 compute_data_dependences_for_loop/bb to find and analyze all data-refs
3125 in the loop/bb and their dependences.
3126 2- vect_analyze_dependences(): apply dependence testing using ddrs.
3127 3- vect_analyze_drs_alignment(): check that ref_stmt.alignment is ok.
3128 4- vect_analyze_drs_access(): check that ref_stmt.step is ok.
3133 vect_analyze_data_refs (loop_vec_info loop_vinfo
,
3134 bb_vec_info bb_vinfo
,
3137 struct loop
*loop
= NULL
;
3138 basic_block bb
= NULL
;
3140 vec
<data_reference_p
> datarefs
;
3141 struct data_reference
*dr
;
3144 if (dump_enabled_p ())
3145 dump_printf_loc (MSG_NOTE
, vect_location
,
3146 "=== vect_analyze_data_refs ===\n");
3150 basic_block
*bbs
= LOOP_VINFO_BBS (loop_vinfo
);
3152 loop
= LOOP_VINFO_LOOP (loop_vinfo
);
3153 datarefs
= LOOP_VINFO_DATAREFS (loop_vinfo
);
3154 if (!find_loop_nest (loop
, &LOOP_VINFO_LOOP_NEST (loop_vinfo
)))
3156 if (dump_enabled_p ())
3157 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
3158 "not vectorized: loop contains function calls"
3159 " or data references that cannot be analyzed\n");
3163 for (i
= 0; i
< loop
->num_nodes
; i
++)
3165 gimple_stmt_iterator gsi
;
3167 for (gsi
= gsi_start_bb (bbs
[i
]); !gsi_end_p (gsi
); gsi_next (&gsi
))
3169 gimple stmt
= gsi_stmt (gsi
);
3170 if (!find_data_references_in_stmt (loop
, stmt
, &datarefs
))
3172 if (is_gimple_call (stmt
) && loop
->safelen
)
3174 tree fndecl
= gimple_call_fndecl (stmt
), op
;
3175 if (fndecl
!= NULL_TREE
)
3177 struct cgraph_node
*node
= cgraph_get_node (fndecl
);
3178 if (node
!= NULL
&& node
->simd_clones
!= NULL
)
3180 unsigned int j
, n
= gimple_call_num_args (stmt
);
3181 for (j
= 0; j
< n
; j
++)
3183 op
= gimple_call_arg (stmt
, j
);
3185 || (REFERENCE_CLASS_P (op
)
3186 && get_base_address (op
)))
3189 op
= gimple_call_lhs (stmt
);
3190 /* Ignore #pragma omp declare simd functions
3191 if they don't have data references in the
3192 call stmt itself. */
3196 || (REFERENCE_CLASS_P (op
)
3197 && get_base_address (op
)))))
3202 LOOP_VINFO_DATAREFS (loop_vinfo
) = datarefs
;
3203 if (dump_enabled_p ())
3204 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
3205 "not vectorized: loop contains function "
3206 "calls or data references that cannot "
3213 LOOP_VINFO_DATAREFS (loop_vinfo
) = datarefs
;
3217 gimple_stmt_iterator gsi
;
3219 bb
= BB_VINFO_BB (bb_vinfo
);
3220 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
3222 gimple stmt
= gsi_stmt (gsi
);
3223 if (!find_data_references_in_stmt (NULL
, stmt
,
3224 &BB_VINFO_DATAREFS (bb_vinfo
)))
3226 /* Mark the rest of the basic-block as unvectorizable. */
3227 for (; !gsi_end_p (gsi
); gsi_next (&gsi
))
3229 stmt
= gsi_stmt (gsi
);
3230 STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (stmt
)) = false;
3236 datarefs
= BB_VINFO_DATAREFS (bb_vinfo
);
3239 /* Go through the data-refs, check that the analysis succeeded. Update
3240 pointer from stmt_vec_info struct to DR and vectype. */
3242 FOR_EACH_VEC_ELT (datarefs
, i
, dr
)
3245 stmt_vec_info stmt_info
;
3246 tree base
, offset
, init
;
3247 bool gather
= false;
3248 bool simd_lane_access
= false;
3252 if (!dr
|| !DR_REF (dr
))
3254 if (dump_enabled_p ())
3255 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
3256 "not vectorized: unhandled data-ref\n");
3260 stmt
= DR_STMT (dr
);
3261 stmt_info
= vinfo_for_stmt (stmt
);
3263 /* Discard clobbers from the dataref vector. We will remove
3264 clobber stmts during vectorization. */
3265 if (gimple_clobber_p (stmt
))
3267 if (i
== datarefs
.length () - 1)
3272 datarefs
[i
] = datarefs
.pop ();
3276 /* Check that analysis of the data-ref succeeded. */
3277 if (!DR_BASE_ADDRESS (dr
) || !DR_OFFSET (dr
) || !DR_INIT (dr
)
3282 && !TREE_THIS_VOLATILE (DR_REF (dr
))
3283 && targetm
.vectorize
.builtin_gather
!= NULL
;
3284 bool maybe_simd_lane_access
3285 = loop_vinfo
&& loop
->simduid
;
3287 /* If target supports vector gather loads, or if this might be
3288 a SIMD lane access, see if they can't be used. */
3290 && (maybe_gather
|| maybe_simd_lane_access
)
3291 && !nested_in_vect_loop_p (loop
, stmt
))
3293 struct data_reference
*newdr
3294 = create_data_ref (NULL
, loop_containing_stmt (stmt
),
3295 DR_REF (dr
), stmt
, true);
3296 gcc_assert (newdr
!= NULL
&& DR_REF (newdr
));
3297 if (DR_BASE_ADDRESS (newdr
)
3298 && DR_OFFSET (newdr
)
3301 && integer_zerop (DR_STEP (newdr
)))
3303 if (maybe_simd_lane_access
)
3305 tree off
= DR_OFFSET (newdr
);
3307 if (TREE_CODE (DR_INIT (newdr
)) == INTEGER_CST
3308 && TREE_CODE (off
) == MULT_EXPR
3309 && tree_fits_uhwi_p (TREE_OPERAND (off
, 1)))
3311 tree step
= TREE_OPERAND (off
, 1);
3312 off
= TREE_OPERAND (off
, 0);
3314 if (CONVERT_EXPR_P (off
)
3315 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (off
,
3317 < TYPE_PRECISION (TREE_TYPE (off
)))
3318 off
= TREE_OPERAND (off
, 0);
3319 if (TREE_CODE (off
) == SSA_NAME
)
3321 gimple def
= SSA_NAME_DEF_STMT (off
);
3322 tree reft
= TREE_TYPE (DR_REF (newdr
));
3323 if (gimple_call_internal_p (def
)
3324 && gimple_call_internal_fn (def
)
3325 == IFN_GOMP_SIMD_LANE
)
3327 tree arg
= gimple_call_arg (def
, 0);
3328 gcc_assert (TREE_CODE (arg
) == SSA_NAME
);
3329 arg
= SSA_NAME_VAR (arg
);
3330 if (arg
== loop
->simduid
3332 && tree_int_cst_equal
3333 (TYPE_SIZE_UNIT (reft
),
3336 DR_OFFSET (newdr
) = ssize_int (0);
3337 DR_STEP (newdr
) = step
;
3338 DR_ALIGNED_TO (newdr
)
3339 = size_int (BIGGEST_ALIGNMENT
);
3341 simd_lane_access
= true;
3347 if (!simd_lane_access
&& maybe_gather
)
3353 if (!gather
&& !simd_lane_access
)
3354 free_data_ref (newdr
);
3357 if (!gather
&& !simd_lane_access
)
3359 if (dump_enabled_p ())
3361 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
3362 "not vectorized: data ref analysis "
3364 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
, stmt
, 0);
3365 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
3375 if (TREE_CODE (DR_BASE_ADDRESS (dr
)) == INTEGER_CST
)
3377 if (dump_enabled_p ())
3378 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
3379 "not vectorized: base addr of dr is a "
3385 if (gather
|| simd_lane_access
)
3390 if (TREE_THIS_VOLATILE (DR_REF (dr
)))
3392 if (dump_enabled_p ())
3394 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
3395 "not vectorized: volatile type ");
3396 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
, stmt
, 0);
3397 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
3406 if (stmt_can_throw_internal (stmt
))
3408 if (dump_enabled_p ())
3410 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
3411 "not vectorized: statement can throw an "
3413 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
, stmt
, 0);
3414 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
3420 if (gather
|| simd_lane_access
)
3425 if (TREE_CODE (DR_REF (dr
)) == COMPONENT_REF
3426 && DECL_BIT_FIELD (TREE_OPERAND (DR_REF (dr
), 1)))
3428 if (dump_enabled_p ())
3430 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
3431 "not vectorized: statement is bitfield "
3433 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
, stmt
, 0);
3434 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
3440 if (gather
|| simd_lane_access
)
3445 base
= unshare_expr (DR_BASE_ADDRESS (dr
));
3446 offset
= unshare_expr (DR_OFFSET (dr
));
3447 init
= unshare_expr (DR_INIT (dr
));
3449 if (is_gimple_call (stmt
)
3450 && (!gimple_call_internal_p (stmt
)
3451 || (gimple_call_internal_fn (stmt
) != IFN_MASK_LOAD
3452 && gimple_call_internal_fn (stmt
) != IFN_MASK_STORE
)))
3454 if (dump_enabled_p ())
3456 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
3457 "not vectorized: dr in a call ");
3458 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
, stmt
, 0);
3459 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
3465 if (gather
|| simd_lane_access
)
3470 /* Update DR field in stmt_vec_info struct. */
3472 /* If the dataref is in an inner-loop of the loop that is considered for
3473 for vectorization, we also want to analyze the access relative to
3474 the outer-loop (DR contains information only relative to the
3475 inner-most enclosing loop). We do that by building a reference to the
3476 first location accessed by the inner-loop, and analyze it relative to
3478 if (loop
&& nested_in_vect_loop_p (loop
, stmt
))
3480 tree outer_step
, outer_base
, outer_init
;
3481 HOST_WIDE_INT pbitsize
, pbitpos
;
3483 enum machine_mode pmode
;
3484 int punsignedp
, pvolatilep
;
3485 affine_iv base_iv
, offset_iv
;
3488 /* Build a reference to the first location accessed by the
3489 inner-loop: *(BASE+INIT). (The first location is actually
3490 BASE+INIT+OFFSET, but we add OFFSET separately later). */
3491 tree inner_base
= build_fold_indirect_ref
3492 (fold_build_pointer_plus (base
, init
));
3494 if (dump_enabled_p ())
3496 dump_printf_loc (MSG_NOTE
, vect_location
,
3497 "analyze in outer-loop: ");
3498 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, inner_base
);
3499 dump_printf (MSG_NOTE
, "\n");
3502 outer_base
= get_inner_reference (inner_base
, &pbitsize
, &pbitpos
,
3503 &poffset
, &pmode
, &punsignedp
, &pvolatilep
, false);
3504 gcc_assert (outer_base
!= NULL_TREE
);
3506 if (pbitpos
% BITS_PER_UNIT
!= 0)
3508 if (dump_enabled_p ())
3509 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
3510 "failed: bit offset alignment.\n");
3514 outer_base
= build_fold_addr_expr (outer_base
);
3515 if (!simple_iv (loop
, loop_containing_stmt (stmt
), outer_base
,
3518 if (dump_enabled_p ())
3519 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
3520 "failed: evolution of base is not affine.\n");
3527 poffset
= fold_build2 (PLUS_EXPR
, TREE_TYPE (offset
), offset
,
3535 offset_iv
.base
= ssize_int (0);
3536 offset_iv
.step
= ssize_int (0);
3538 else if (!simple_iv (loop
, loop_containing_stmt (stmt
), poffset
,
3541 if (dump_enabled_p ())
3542 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
3543 "evolution of offset is not affine.\n");
3547 outer_init
= ssize_int (pbitpos
/ BITS_PER_UNIT
);
3548 split_constant_offset (base_iv
.base
, &base_iv
.base
, &dinit
);
3549 outer_init
= size_binop (PLUS_EXPR
, outer_init
, dinit
);
3550 split_constant_offset (offset_iv
.base
, &offset_iv
.base
, &dinit
);
3551 outer_init
= size_binop (PLUS_EXPR
, outer_init
, dinit
);
3553 outer_step
= size_binop (PLUS_EXPR
,
3554 fold_convert (ssizetype
, base_iv
.step
),
3555 fold_convert (ssizetype
, offset_iv
.step
));
3557 STMT_VINFO_DR_STEP (stmt_info
) = outer_step
;
3558 /* FIXME: Use canonicalize_base_object_address (base_iv.base); */
3559 STMT_VINFO_DR_BASE_ADDRESS (stmt_info
) = base_iv
.base
;
3560 STMT_VINFO_DR_INIT (stmt_info
) = outer_init
;
3561 STMT_VINFO_DR_OFFSET (stmt_info
) =
3562 fold_convert (ssizetype
, offset_iv
.base
);
3563 STMT_VINFO_DR_ALIGNED_TO (stmt_info
) =
3564 size_int (highest_pow2_factor (offset_iv
.base
));
3566 if (dump_enabled_p ())
3568 dump_printf_loc (MSG_NOTE
, vect_location
,
3569 "\touter base_address: ");
3570 dump_generic_expr (MSG_NOTE
, TDF_SLIM
,
3571 STMT_VINFO_DR_BASE_ADDRESS (stmt_info
));
3572 dump_printf (MSG_NOTE
, "\n\touter offset from base address: ");
3573 dump_generic_expr (MSG_NOTE
, TDF_SLIM
,
3574 STMT_VINFO_DR_OFFSET (stmt_info
));
3575 dump_printf (MSG_NOTE
,
3576 "\n\touter constant offset from base address: ");
3577 dump_generic_expr (MSG_NOTE
, TDF_SLIM
,
3578 STMT_VINFO_DR_INIT (stmt_info
));
3579 dump_printf (MSG_NOTE
, "\n\touter step: ");
3580 dump_generic_expr (MSG_NOTE
, TDF_SLIM
,
3581 STMT_VINFO_DR_STEP (stmt_info
));
3582 dump_printf (MSG_NOTE
, "\n\touter aligned to: ");
3583 dump_generic_expr (MSG_NOTE
, TDF_SLIM
,
3584 STMT_VINFO_DR_ALIGNED_TO (stmt_info
));
3585 dump_printf (MSG_NOTE
, "\n");
3589 if (STMT_VINFO_DATA_REF (stmt_info
))
3591 if (dump_enabled_p ())
3593 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
3594 "not vectorized: more than one data ref "
3596 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
, stmt
, 0);
3597 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
3603 if (gather
|| simd_lane_access
)
3608 STMT_VINFO_DATA_REF (stmt_info
) = dr
;
3609 if (simd_lane_access
)
3611 STMT_VINFO_SIMD_LANE_ACCESS_P (stmt_info
) = true;
3615 /* Set vectype for STMT. */
3616 scalar_type
= TREE_TYPE (DR_REF (dr
));
3617 STMT_VINFO_VECTYPE (stmt_info
) =
3618 get_vectype_for_scalar_type (scalar_type
);
3619 if (!STMT_VINFO_VECTYPE (stmt_info
))
3621 if (dump_enabled_p ())
3623 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
3624 "not vectorized: no vectype for stmt: ");
3625 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
, stmt
, 0);
3626 dump_printf (MSG_MISSED_OPTIMIZATION
, " scalar_type: ");
3627 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_DETAILS
,
3629 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
3635 if (gather
|| simd_lane_access
)
3637 STMT_VINFO_DATA_REF (stmt_info
) = NULL
;
3644 if (dump_enabled_p ())
3646 dump_printf_loc (MSG_NOTE
, vect_location
,
3647 "got vectype for stmt: ");
3648 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
, stmt
, 0);
3649 dump_generic_expr (MSG_NOTE
, TDF_SLIM
,
3650 STMT_VINFO_VECTYPE (stmt_info
));
3651 dump_printf (MSG_NOTE
, "\n");
3655 /* Adjust the minimal vectorization factor according to the
3657 vf
= TYPE_VECTOR_SUBPARTS (STMT_VINFO_VECTYPE (stmt_info
));
3665 gather
= 0 != vect_check_gather (stmt
, loop_vinfo
, NULL
, &off
, NULL
);
3667 && get_vectype_for_scalar_type (TREE_TYPE (off
)) == NULL_TREE
)
3671 STMT_VINFO_DATA_REF (stmt_info
) = NULL
;
3673 if (dump_enabled_p ())
3675 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
3676 "not vectorized: not suitable for gather "
3678 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
, stmt
, 0);
3679 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
3685 STMT_VINFO_GATHER_P (stmt_info
) = true;
3688 && TREE_CODE (DR_STEP (dr
)) != INTEGER_CST
)
3690 if (nested_in_vect_loop_p (loop
, stmt
)
3691 || !DR_IS_READ (dr
))
3693 if (dump_enabled_p ())
3695 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
3696 "not vectorized: not suitable for strided "
3698 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
, stmt
, 0);
3699 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
3703 STMT_VINFO_STRIDE_LOAD_P (stmt_info
) = true;
3707 /* If we stopped analysis at the first dataref we could not analyze
3708 when trying to vectorize a basic-block mark the rest of the datarefs
3709 as not vectorizable and truncate the vector of datarefs. That
3710 avoids spending useless time in analyzing their dependence. */
3711 if (i
!= datarefs
.length ())
3713 gcc_assert (bb_vinfo
!= NULL
);
3714 for (unsigned j
= i
; j
< datarefs
.length (); ++j
)
3716 data_reference_p dr
= datarefs
[j
];
3717 STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dr
))) = false;
3720 datarefs
.truncate (i
);
3727 /* Function vect_get_new_vect_var.
3729 Returns a name for a new variable. The current naming scheme appends the
3730 prefix "vect_" or "vect_p" (depending on the value of VAR_KIND) to
3731 the name of vectorizer generated variables, and appends that to NAME if
3735 vect_get_new_vect_var (tree type
, enum vect_var_kind var_kind
, const char *name
)
3742 case vect_simple_var
:
3745 case vect_scalar_var
:
3748 case vect_pointer_var
:
3757 char* tmp
= concat (prefix
, "_", name
, NULL
);
3758 new_vect_var
= create_tmp_reg (type
, tmp
);
3762 new_vect_var
= create_tmp_reg (type
, prefix
);
3764 return new_vect_var
;
3768 /* Function vect_create_addr_base_for_vector_ref.
3770 Create an expression that computes the address of the first memory location
3771 that will be accessed for a data reference.
3774 STMT: The statement containing the data reference.
3775 NEW_STMT_LIST: Must be initialized to NULL_TREE or a statement list.
3776 OFFSET: Optional. If supplied, it is be added to the initial address.
3777 LOOP: Specify relative to which loop-nest should the address be computed.
3778 For example, when the dataref is in an inner-loop nested in an
3779 outer-loop that is now being vectorized, LOOP can be either the
3780 outer-loop, or the inner-loop. The first memory location accessed
3781 by the following dataref ('in' points to short):
3788 if LOOP=i_loop: &in (relative to i_loop)
3789 if LOOP=j_loop: &in+i*2B (relative to j_loop)
3792 1. Return an SSA_NAME whose value is the address of the memory location of
3793 the first vector of the data reference.
3794 2. If new_stmt_list is not NULL_TREE after return then the caller must insert
3795 these statement(s) which define the returned SSA_NAME.
3797 FORNOW: We are only handling array accesses with step 1. */
3800 vect_create_addr_base_for_vector_ref (gimple stmt
,
3801 gimple_seq
*new_stmt_list
,
3805 stmt_vec_info stmt_info
= vinfo_for_stmt (stmt
);
3806 struct data_reference
*dr
= STMT_VINFO_DATA_REF (stmt_info
);
3808 const char *base_name
;
3811 gimple_seq seq
= NULL
;
3815 tree step
= TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dr
)));
3816 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_info
);
3818 if (loop_vinfo
&& loop
&& loop
!= (gimple_bb (stmt
))->loop_father
)
3820 struct loop
*outer_loop
= LOOP_VINFO_LOOP (loop_vinfo
);
3822 gcc_assert (nested_in_vect_loop_p (outer_loop
, stmt
));
3824 data_ref_base
= unshare_expr (STMT_VINFO_DR_BASE_ADDRESS (stmt_info
));
3825 base_offset
= unshare_expr (STMT_VINFO_DR_OFFSET (stmt_info
));
3826 init
= unshare_expr (STMT_VINFO_DR_INIT (stmt_info
));
3830 data_ref_base
= unshare_expr (DR_BASE_ADDRESS (dr
));
3831 base_offset
= unshare_expr (DR_OFFSET (dr
));
3832 init
= unshare_expr (DR_INIT (dr
));
3836 base_name
= get_name (data_ref_base
);
3839 base_offset
= ssize_int (0);
3840 init
= ssize_int (0);
3841 base_name
= get_name (DR_REF (dr
));
3844 /* Create base_offset */
3845 base_offset
= size_binop (PLUS_EXPR
,
3846 fold_convert (sizetype
, base_offset
),
3847 fold_convert (sizetype
, init
));
3851 offset
= fold_build2 (MULT_EXPR
, sizetype
,
3852 fold_convert (sizetype
, offset
), step
);
3853 base_offset
= fold_build2 (PLUS_EXPR
, sizetype
,
3854 base_offset
, offset
);
3857 /* base + base_offset */
3859 addr_base
= fold_build_pointer_plus (data_ref_base
, base_offset
);
3862 addr_base
= build1 (ADDR_EXPR
,
3863 build_pointer_type (TREE_TYPE (DR_REF (dr
))),
3864 unshare_expr (DR_REF (dr
)));
3867 vect_ptr_type
= build_pointer_type (STMT_VINFO_VECTYPE (stmt_info
));
3868 addr_base
= fold_convert (vect_ptr_type
, addr_base
);
3869 dest
= vect_get_new_vect_var (vect_ptr_type
, vect_pointer_var
, base_name
);
3870 addr_base
= force_gimple_operand (addr_base
, &seq
, false, dest
);
3871 gimple_seq_add_seq (new_stmt_list
, seq
);
3873 if (DR_PTR_INFO (dr
)
3874 && TREE_CODE (addr_base
) == SSA_NAME
)
3876 duplicate_ssa_name_ptr_info (addr_base
, DR_PTR_INFO (dr
));
3878 mark_ptr_info_alignment_unknown (SSA_NAME_PTR_INFO (addr_base
));
3881 if (dump_enabled_p ())
3883 dump_printf_loc (MSG_NOTE
, vect_location
, "created ");
3884 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, addr_base
);
3885 dump_printf (MSG_NOTE
, "\n");
3892 /* Function vect_create_data_ref_ptr.
3894 Create a new pointer-to-AGGR_TYPE variable (ap), that points to the first
3895 location accessed in the loop by STMT, along with the def-use update
3896 chain to appropriately advance the pointer through the loop iterations.
3897 Also set aliasing information for the pointer. This pointer is used by
3898 the callers to this function to create a memory reference expression for
3899 vector load/store access.
3902 1. STMT: a stmt that references memory. Expected to be of the form
3903 GIMPLE_ASSIGN <name, data-ref> or
3904 GIMPLE_ASSIGN <data-ref, name>.
3905 2. AGGR_TYPE: the type of the reference, which should be either a vector
3907 3. AT_LOOP: the loop where the vector memref is to be created.
3908 4. OFFSET (optional): an offset to be added to the initial address accessed
3909 by the data-ref in STMT.
3910 5. BSI: location where the new stmts are to be placed if there is no loop
3911 6. ONLY_INIT: indicate if ap is to be updated in the loop, or remain
3912 pointing to the initial address.
3915 1. Declare a new ptr to vector_type, and have it point to the base of the
3916 data reference (initial addressed accessed by the data reference).
3917 For example, for vector of type V8HI, the following code is generated:
3920 ap = (v8hi *)initial_address;
3922 if OFFSET is not supplied:
3923 initial_address = &a[init];
3924 if OFFSET is supplied:
3925 initial_address = &a[init + OFFSET];
3927 Return the initial_address in INITIAL_ADDRESS.
3929 2. If ONLY_INIT is true, just return the initial pointer. Otherwise, also
3930 update the pointer in each iteration of the loop.
3932 Return the increment stmt that updates the pointer in PTR_INCR.
3934 3. Set INV_P to true if the access pattern of the data reference in the
3935 vectorized loop is invariant. Set it to false otherwise.
3937 4. Return the pointer. */
3940 vect_create_data_ref_ptr (gimple stmt
, tree aggr_type
, struct loop
*at_loop
,
3941 tree offset
, tree
*initial_address
,
3942 gimple_stmt_iterator
*gsi
, gimple
*ptr_incr
,
3943 bool only_init
, bool *inv_p
)
3945 const char *base_name
;
3946 stmt_vec_info stmt_info
= vinfo_for_stmt (stmt
);
3947 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_info
);
3948 struct loop
*loop
= NULL
;
3949 bool nested_in_vect_loop
= false;
3950 struct loop
*containing_loop
= NULL
;
3955 gimple_seq new_stmt_list
= NULL
;
3959 struct data_reference
*dr
= STMT_VINFO_DATA_REF (stmt_info
);
3961 gimple_stmt_iterator incr_gsi
;
3963 tree indx_before_incr
, indx_after_incr
;
3966 bb_vec_info bb_vinfo
= STMT_VINFO_BB_VINFO (stmt_info
);
3968 gcc_assert (TREE_CODE (aggr_type
) == ARRAY_TYPE
3969 || TREE_CODE (aggr_type
) == VECTOR_TYPE
);
3973 loop
= LOOP_VINFO_LOOP (loop_vinfo
);
3974 nested_in_vect_loop
= nested_in_vect_loop_p (loop
, stmt
);
3975 containing_loop
= (gimple_bb (stmt
))->loop_father
;
3976 pe
= loop_preheader_edge (loop
);
3980 gcc_assert (bb_vinfo
);
3985 /* Check the step (evolution) of the load in LOOP, and record
3986 whether it's invariant. */
3987 if (nested_in_vect_loop
)
3988 step
= STMT_VINFO_DR_STEP (stmt_info
);
3990 step
= DR_STEP (STMT_VINFO_DATA_REF (stmt_info
));
3992 if (integer_zerop (step
))
3997 /* Create an expression for the first address accessed by this load
3999 base_name
= get_name (DR_BASE_ADDRESS (dr
));
4001 if (dump_enabled_p ())
4003 tree dr_base_type
= TREE_TYPE (DR_BASE_OBJECT (dr
));
4004 dump_printf_loc (MSG_NOTE
, vect_location
,
4005 "create %s-pointer variable to type: ",
4006 get_tree_code_name (TREE_CODE (aggr_type
)));
4007 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, aggr_type
);
4008 if (TREE_CODE (dr_base_type
) == ARRAY_TYPE
)
4009 dump_printf (MSG_NOTE
, " vectorizing an array ref: ");
4010 else if (TREE_CODE (dr_base_type
) == VECTOR_TYPE
)
4011 dump_printf (MSG_NOTE
, " vectorizing a vector ref: ");
4012 else if (TREE_CODE (dr_base_type
) == RECORD_TYPE
)
4013 dump_printf (MSG_NOTE
, " vectorizing a record based array ref: ");
4015 dump_printf (MSG_NOTE
, " vectorizing a pointer ref: ");
4016 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, DR_BASE_OBJECT (dr
));
4017 dump_printf (MSG_NOTE
, "\n");
4020 /* (1) Create the new aggregate-pointer variable.
4021 Vector and array types inherit the alias set of their component
4022 type by default so we need to use a ref-all pointer if the data
4023 reference does not conflict with the created aggregated data
4024 reference because it is not addressable. */
4025 bool need_ref_all
= false;
4026 if (!alias_sets_conflict_p (get_alias_set (aggr_type
),
4027 get_alias_set (DR_REF (dr
))))
4028 need_ref_all
= true;
4029 /* Likewise for any of the data references in the stmt group. */
4030 else if (STMT_VINFO_GROUP_SIZE (stmt_info
) > 1)
4032 gimple orig_stmt
= STMT_VINFO_GROUP_FIRST_ELEMENT (stmt_info
);
4035 stmt_vec_info sinfo
= vinfo_for_stmt (orig_stmt
);
4036 struct data_reference
*sdr
= STMT_VINFO_DATA_REF (sinfo
);
4037 if (!alias_sets_conflict_p (get_alias_set (aggr_type
),
4038 get_alias_set (DR_REF (sdr
))))
4040 need_ref_all
= true;
4043 orig_stmt
= STMT_VINFO_GROUP_NEXT_ELEMENT (sinfo
);
4047 aggr_ptr_type
= build_pointer_type_for_mode (aggr_type
, ptr_mode
,
4049 aggr_ptr
= vect_get_new_vect_var (aggr_ptr_type
, vect_pointer_var
, base_name
);
4052 /* Note: If the dataref is in an inner-loop nested in LOOP, and we are
4053 vectorizing LOOP (i.e., outer-loop vectorization), we need to create two
4054 def-use update cycles for the pointer: one relative to the outer-loop
4055 (LOOP), which is what steps (3) and (4) below do. The other is relative
4056 to the inner-loop (which is the inner-most loop containing the dataref),
4057 and this is done be step (5) below.
4059 When vectorizing inner-most loops, the vectorized loop (LOOP) is also the
4060 inner-most loop, and so steps (3),(4) work the same, and step (5) is
4061 redundant. Steps (3),(4) create the following:
4064 LOOP: vp1 = phi(vp0,vp2)
4070 If there is an inner-loop nested in loop, then step (5) will also be
4071 applied, and an additional update in the inner-loop will be created:
4074 LOOP: vp1 = phi(vp0,vp2)
4076 inner: vp3 = phi(vp1,vp4)
4077 vp4 = vp3 + inner_step
4083 /* (2) Calculate the initial address of the aggregate-pointer, and set
4084 the aggregate-pointer to point to it before the loop. */
4086 /* Create: (&(base[init_val+offset]) in the loop preheader. */
4088 new_temp
= vect_create_addr_base_for_vector_ref (stmt
, &new_stmt_list
,
4094 new_bb
= gsi_insert_seq_on_edge_immediate (pe
, new_stmt_list
);
4095 gcc_assert (!new_bb
);
4098 gsi_insert_seq_before (gsi
, new_stmt_list
, GSI_SAME_STMT
);
4101 *initial_address
= new_temp
;
4103 /* Create: p = (aggr_type *) initial_base */
4104 if (TREE_CODE (new_temp
) != SSA_NAME
4105 || !useless_type_conversion_p (aggr_ptr_type
, TREE_TYPE (new_temp
)))
4107 vec_stmt
= gimple_build_assign (aggr_ptr
,
4108 fold_convert (aggr_ptr_type
, new_temp
));
4109 aggr_ptr_init
= make_ssa_name (aggr_ptr
, vec_stmt
);
4110 /* Copy the points-to information if it exists. */
4111 if (DR_PTR_INFO (dr
))
4112 duplicate_ssa_name_ptr_info (aggr_ptr_init
, DR_PTR_INFO (dr
));
4113 gimple_assign_set_lhs (vec_stmt
, aggr_ptr_init
);
4116 new_bb
= gsi_insert_on_edge_immediate (pe
, vec_stmt
);
4117 gcc_assert (!new_bb
);
4120 gsi_insert_before (gsi
, vec_stmt
, GSI_SAME_STMT
);
4123 aggr_ptr_init
= new_temp
;
4125 /* (3) Handle the updating of the aggregate-pointer inside the loop.
4126 This is needed when ONLY_INIT is false, and also when AT_LOOP is the
4127 inner-loop nested in LOOP (during outer-loop vectorization). */
4129 /* No update in loop is required. */
4130 if (only_init
&& (!loop_vinfo
|| at_loop
== loop
))
4131 aptr
= aggr_ptr_init
;
4134 /* The step of the aggregate pointer is the type size. */
4135 tree iv_step
= TYPE_SIZE_UNIT (aggr_type
);
4136 /* One exception to the above is when the scalar step of the load in
4137 LOOP is zero. In this case the step here is also zero. */
4139 iv_step
= size_zero_node
;
4140 else if (tree_int_cst_sgn (step
) == -1)
4141 iv_step
= fold_build1 (NEGATE_EXPR
, TREE_TYPE (iv_step
), iv_step
);
4143 standard_iv_increment_position (loop
, &incr_gsi
, &insert_after
);
4145 create_iv (aggr_ptr_init
,
4146 fold_convert (aggr_ptr_type
, iv_step
),
4147 aggr_ptr
, loop
, &incr_gsi
, insert_after
,
4148 &indx_before_incr
, &indx_after_incr
);
4149 incr
= gsi_stmt (incr_gsi
);
4150 set_vinfo_for_stmt (incr
, new_stmt_vec_info (incr
, loop_vinfo
, NULL
));
4152 /* Copy the points-to information if it exists. */
4153 if (DR_PTR_INFO (dr
))
4155 duplicate_ssa_name_ptr_info (indx_before_incr
, DR_PTR_INFO (dr
));
4156 duplicate_ssa_name_ptr_info (indx_after_incr
, DR_PTR_INFO (dr
));
4161 aptr
= indx_before_incr
;
4164 if (!nested_in_vect_loop
|| only_init
)
4168 /* (4) Handle the updating of the aggregate-pointer inside the inner-loop
4169 nested in LOOP, if exists. */
4171 gcc_assert (nested_in_vect_loop
);
4174 standard_iv_increment_position (containing_loop
, &incr_gsi
,
4176 create_iv (aptr
, fold_convert (aggr_ptr_type
, DR_STEP (dr
)), aggr_ptr
,
4177 containing_loop
, &incr_gsi
, insert_after
, &indx_before_incr
,
4179 incr
= gsi_stmt (incr_gsi
);
4180 set_vinfo_for_stmt (incr
, new_stmt_vec_info (incr
, loop_vinfo
, NULL
));
4182 /* Copy the points-to information if it exists. */
4183 if (DR_PTR_INFO (dr
))
4185 duplicate_ssa_name_ptr_info (indx_before_incr
, DR_PTR_INFO (dr
));
4186 duplicate_ssa_name_ptr_info (indx_after_incr
, DR_PTR_INFO (dr
));
4191 return indx_before_incr
;
4198 /* Function bump_vector_ptr
4200 Increment a pointer (to a vector type) by vector-size. If requested,
4201 i.e. if PTR-INCR is given, then also connect the new increment stmt
4202 to the existing def-use update-chain of the pointer, by modifying
4203 the PTR_INCR as illustrated below:
4205 The pointer def-use update-chain before this function:
4206 DATAREF_PTR = phi (p_0, p_2)
4208 PTR_INCR: p_2 = DATAREF_PTR + step
4210 The pointer def-use update-chain after this function:
4211 DATAREF_PTR = phi (p_0, p_2)
4213 NEW_DATAREF_PTR = DATAREF_PTR + BUMP
4215 PTR_INCR: p_2 = NEW_DATAREF_PTR + step
4218 DATAREF_PTR - ssa_name of a pointer (to vector type) that is being updated
4220 PTR_INCR - optional. The stmt that updates the pointer in each iteration of
4221 the loop. The increment amount across iterations is expected
4223 BSI - location where the new update stmt is to be placed.
4224 STMT - the original scalar memory-access stmt that is being vectorized.
4225 BUMP - optional. The offset by which to bump the pointer. If not given,
4226 the offset is assumed to be vector_size.
4228 Output: Return NEW_DATAREF_PTR as illustrated above.
4233 bump_vector_ptr (tree dataref_ptr
, gimple ptr_incr
, gimple_stmt_iterator
*gsi
,
4234 gimple stmt
, tree bump
)
4236 stmt_vec_info stmt_info
= vinfo_for_stmt (stmt
);
4237 struct data_reference
*dr
= STMT_VINFO_DATA_REF (stmt_info
);
4238 tree vectype
= STMT_VINFO_VECTYPE (stmt_info
);
4239 tree update
= TYPE_SIZE_UNIT (vectype
);
4242 use_operand_p use_p
;
4243 tree new_dataref_ptr
;
4248 new_dataref_ptr
= copy_ssa_name (dataref_ptr
, NULL
);
4249 incr_stmt
= gimple_build_assign_with_ops (POINTER_PLUS_EXPR
, new_dataref_ptr
,
4250 dataref_ptr
, update
);
4251 vect_finish_stmt_generation (stmt
, incr_stmt
, gsi
);
4253 /* Copy the points-to information if it exists. */
4254 if (DR_PTR_INFO (dr
))
4256 duplicate_ssa_name_ptr_info (new_dataref_ptr
, DR_PTR_INFO (dr
));
4257 mark_ptr_info_alignment_unknown (SSA_NAME_PTR_INFO (new_dataref_ptr
));
4261 return new_dataref_ptr
;
4263 /* Update the vector-pointer's cross-iteration increment. */
4264 FOR_EACH_SSA_USE_OPERAND (use_p
, ptr_incr
, iter
, SSA_OP_USE
)
4266 tree use
= USE_FROM_PTR (use_p
);
4268 if (use
== dataref_ptr
)
4269 SET_USE (use_p
, new_dataref_ptr
);
4271 gcc_assert (tree_int_cst_compare (use
, update
) == 0);
4274 return new_dataref_ptr
;
4278 /* Function vect_create_destination_var.
4280 Create a new temporary of type VECTYPE. */
4283 vect_create_destination_var (tree scalar_dest
, tree vectype
)
4289 enum vect_var_kind kind
;
4291 kind
= vectype
? vect_simple_var
: vect_scalar_var
;
4292 type
= vectype
? vectype
: TREE_TYPE (scalar_dest
);
4294 gcc_assert (TREE_CODE (scalar_dest
) == SSA_NAME
);
4296 name
= get_name (scalar_dest
);
4298 asprintf (&new_name
, "%s_%u", name
, SSA_NAME_VERSION (scalar_dest
));
4300 asprintf (&new_name
, "_%u", SSA_NAME_VERSION (scalar_dest
));
4301 vec_dest
= vect_get_new_vect_var (type
, kind
, new_name
);
4307 /* Function vect_grouped_store_supported.
4309 Returns TRUE if interleave high and interleave low permutations
4310 are supported, and FALSE otherwise. */
4313 vect_grouped_store_supported (tree vectype
, unsigned HOST_WIDE_INT count
)
4315 enum machine_mode mode
= TYPE_MODE (vectype
);
4317 /* vect_permute_store_chain requires the group size to be a power of two. */
4318 if (exact_log2 (count
) == -1)
4320 if (dump_enabled_p ())
4321 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
4322 "the size of the group of accesses"
4323 " is not a power of 2\n");
4327 /* Check that the permutation is supported. */
4328 if (VECTOR_MODE_P (mode
))
4330 unsigned int i
, nelt
= GET_MODE_NUNITS (mode
);
4331 unsigned char *sel
= XALLOCAVEC (unsigned char, nelt
);
4332 for (i
= 0; i
< nelt
/ 2; i
++)
4335 sel
[i
* 2 + 1] = i
+ nelt
;
4337 if (can_vec_perm_p (mode
, false, sel
))
4339 for (i
= 0; i
< nelt
; i
++)
4341 if (can_vec_perm_p (mode
, false, sel
))
4346 if (dump_enabled_p ())
4347 dump_printf (MSG_MISSED_OPTIMIZATION
,
4348 "interleave op not supported by target.\n");
4353 /* Return TRUE if vec_store_lanes is available for COUNT vectors of
4357 vect_store_lanes_supported (tree vectype
, unsigned HOST_WIDE_INT count
)
4359 return vect_lanes_optab_supported_p ("vec_store_lanes",
4360 vec_store_lanes_optab
,
4365 /* Function vect_permute_store_chain.
4367 Given a chain of interleaved stores in DR_CHAIN of LENGTH that must be
4368 a power of 2, generate interleave_high/low stmts to reorder the data
4369 correctly for the stores. Return the final references for stores in
4372 E.g., LENGTH is 4 and the scalar type is short, i.e., VF is 8.
4373 The input is 4 vectors each containing 8 elements. We assign a number to
4374 each element, the input sequence is:
4376 1st vec: 0 1 2 3 4 5 6 7
4377 2nd vec: 8 9 10 11 12 13 14 15
4378 3rd vec: 16 17 18 19 20 21 22 23
4379 4th vec: 24 25 26 27 28 29 30 31
4381 The output sequence should be:
4383 1st vec: 0 8 16 24 1 9 17 25
4384 2nd vec: 2 10 18 26 3 11 19 27
4385 3rd vec: 4 12 20 28 5 13 21 30
4386 4th vec: 6 14 22 30 7 15 23 31
4388 i.e., we interleave the contents of the four vectors in their order.
4390 We use interleave_high/low instructions to create such output. The input of
4391 each interleave_high/low operation is two vectors:
4394 the even elements of the result vector are obtained left-to-right from the
4395 high/low elements of the first vector. The odd elements of the result are
4396 obtained left-to-right from the high/low elements of the second vector.
4397 The output of interleave_high will be: 0 4 1 5
4398 and of interleave_low: 2 6 3 7
4401 The permutation is done in log LENGTH stages. In each stage interleave_high
4402 and interleave_low stmts are created for each pair of vectors in DR_CHAIN,
4403 where the first argument is taken from the first half of DR_CHAIN and the
4404 second argument from it's second half.
4407 I1: interleave_high (1st vec, 3rd vec)
4408 I2: interleave_low (1st vec, 3rd vec)
4409 I3: interleave_high (2nd vec, 4th vec)
4410 I4: interleave_low (2nd vec, 4th vec)
4412 The output for the first stage is:
4414 I1: 0 16 1 17 2 18 3 19
4415 I2: 4 20 5 21 6 22 7 23
4416 I3: 8 24 9 25 10 26 11 27
4417 I4: 12 28 13 29 14 30 15 31
4419 The output of the second stage, i.e. the final result is:
4421 I1: 0 8 16 24 1 9 17 25
4422 I2: 2 10 18 26 3 11 19 27
4423 I3: 4 12 20 28 5 13 21 30
4424 I4: 6 14 22 30 7 15 23 31. */
4427 vect_permute_store_chain (vec
<tree
> dr_chain
,
4428 unsigned int length
,
4430 gimple_stmt_iterator
*gsi
,
4431 vec
<tree
> *result_chain
)
4433 tree vect1
, vect2
, high
, low
;
4435 tree vectype
= STMT_VINFO_VECTYPE (vinfo_for_stmt (stmt
));
4436 tree perm_mask_low
, perm_mask_high
;
4438 unsigned int j
, nelt
= TYPE_VECTOR_SUBPARTS (vectype
);
4439 unsigned char *sel
= XALLOCAVEC (unsigned char, nelt
);
4441 result_chain
->quick_grow (length
);
4442 memcpy (result_chain
->address (), dr_chain
.address (),
4443 length
* sizeof (tree
));
4445 for (i
= 0, n
= nelt
/ 2; i
< n
; i
++)
4448 sel
[i
* 2 + 1] = i
+ nelt
;
4450 perm_mask_high
= vect_gen_perm_mask (vectype
, sel
);
4451 gcc_assert (perm_mask_high
!= NULL
);
4453 for (i
= 0; i
< nelt
; i
++)
4455 perm_mask_low
= vect_gen_perm_mask (vectype
, sel
);
4456 gcc_assert (perm_mask_low
!= NULL
);
4458 for (i
= 0, n
= exact_log2 (length
); i
< n
; i
++)
4460 for (j
= 0; j
< length
/2; j
++)
4462 vect1
= dr_chain
[j
];
4463 vect2
= dr_chain
[j
+length
/2];
4465 /* Create interleaving stmt:
4466 high = VEC_PERM_EXPR <vect1, vect2, {0, nelt, 1, nelt+1, ...}> */
4467 high
= make_temp_ssa_name (vectype
, NULL
, "vect_inter_high");
4469 = gimple_build_assign_with_ops (VEC_PERM_EXPR
, high
,
4470 vect1
, vect2
, perm_mask_high
);
4471 vect_finish_stmt_generation (stmt
, perm_stmt
, gsi
);
4472 (*result_chain
)[2*j
] = high
;
4474 /* Create interleaving stmt:
4475 low = VEC_PERM_EXPR <vect1, vect2, {nelt/2, nelt*3/2, nelt/2+1,
4476 nelt*3/2+1, ...}> */
4477 low
= make_temp_ssa_name (vectype
, NULL
, "vect_inter_low");
4479 = gimple_build_assign_with_ops (VEC_PERM_EXPR
, low
,
4480 vect1
, vect2
, perm_mask_low
);
4481 vect_finish_stmt_generation (stmt
, perm_stmt
, gsi
);
4482 (*result_chain
)[2*j
+1] = low
;
4484 memcpy (dr_chain
.address (), result_chain
->address (),
4485 length
* sizeof (tree
));
4489 /* Function vect_setup_realignment
4491 This function is called when vectorizing an unaligned load using
4492 the dr_explicit_realign[_optimized] scheme.
4493 This function generates the following code at the loop prolog:
4496 x msq_init = *(floor(p)); # prolog load
4497 realignment_token = call target_builtin;
4499 x msq = phi (msq_init, ---)
4501 The stmts marked with x are generated only for the case of
4502 dr_explicit_realign_optimized.
4504 The code above sets up a new (vector) pointer, pointing to the first
4505 location accessed by STMT, and a "floor-aligned" load using that pointer.
4506 It also generates code to compute the "realignment-token" (if the relevant
4507 target hook was defined), and creates a phi-node at the loop-header bb
4508 whose arguments are the result of the prolog-load (created by this
4509 function) and the result of a load that takes place in the loop (to be
4510 created by the caller to this function).
4512 For the case of dr_explicit_realign_optimized:
4513 The caller to this function uses the phi-result (msq) to create the
4514 realignment code inside the loop, and sets up the missing phi argument,
4517 msq = phi (msq_init, lsq)
4518 lsq = *(floor(p')); # load in loop
4519 result = realign_load (msq, lsq, realignment_token);
4521 For the case of dr_explicit_realign:
4523 msq = *(floor(p)); # load in loop
4525 lsq = *(floor(p')); # load in loop
4526 result = realign_load (msq, lsq, realignment_token);
4529 STMT - (scalar) load stmt to be vectorized. This load accesses
4530 a memory location that may be unaligned.
4531 BSI - place where new code is to be inserted.
4532 ALIGNMENT_SUPPORT_SCHEME - which of the two misalignment handling schemes
4536 REALIGNMENT_TOKEN - the result of a call to the builtin_mask_for_load
4537 target hook, if defined.
4538 Return value - the result of the loop-header phi node. */
4541 vect_setup_realignment (gimple stmt
, gimple_stmt_iterator
*gsi
,
4542 tree
*realignment_token
,
4543 enum dr_alignment_support alignment_support_scheme
,
4545 struct loop
**at_loop
)
4547 stmt_vec_info stmt_info
= vinfo_for_stmt (stmt
);
4548 tree vectype
= STMT_VINFO_VECTYPE (stmt_info
);
4549 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_info
);
4550 struct data_reference
*dr
= STMT_VINFO_DATA_REF (stmt_info
);
4551 struct loop
*loop
= NULL
;
4553 tree scalar_dest
= gimple_assign_lhs (stmt
);
4560 tree msq_init
= NULL_TREE
;
4563 tree msq
= NULL_TREE
;
4564 gimple_seq stmts
= NULL
;
4566 bool compute_in_loop
= false;
4567 bool nested_in_vect_loop
= false;
4568 struct loop
*containing_loop
= (gimple_bb (stmt
))->loop_father
;
4569 struct loop
*loop_for_initial_load
= NULL
;
4573 loop
= LOOP_VINFO_LOOP (loop_vinfo
);
4574 nested_in_vect_loop
= nested_in_vect_loop_p (loop
, stmt
);
4577 gcc_assert (alignment_support_scheme
== dr_explicit_realign
4578 || alignment_support_scheme
== dr_explicit_realign_optimized
);
4580 /* We need to generate three things:
4581 1. the misalignment computation
4582 2. the extra vector load (for the optimized realignment scheme).
4583 3. the phi node for the two vectors from which the realignment is
4584 done (for the optimized realignment scheme). */
4586 /* 1. Determine where to generate the misalignment computation.
4588 If INIT_ADDR is NULL_TREE, this indicates that the misalignment
4589 calculation will be generated by this function, outside the loop (in the
4590 preheader). Otherwise, INIT_ADDR had already been computed for us by the
4591 caller, inside the loop.
4593 Background: If the misalignment remains fixed throughout the iterations of
4594 the loop, then both realignment schemes are applicable, and also the
4595 misalignment computation can be done outside LOOP. This is because we are
4596 vectorizing LOOP, and so the memory accesses in LOOP advance in steps that
4597 are a multiple of VS (the Vector Size), and therefore the misalignment in
4598 different vectorized LOOP iterations is always the same.
4599 The problem arises only if the memory access is in an inner-loop nested
4600 inside LOOP, which is now being vectorized using outer-loop vectorization.
4601 This is the only case when the misalignment of the memory access may not
4602 remain fixed throughout the iterations of the inner-loop (as explained in
4603 detail in vect_supportable_dr_alignment). In this case, not only is the
4604 optimized realignment scheme not applicable, but also the misalignment
4605 computation (and generation of the realignment token that is passed to
4606 REALIGN_LOAD) have to be done inside the loop.
4608 In short, INIT_ADDR indicates whether we are in a COMPUTE_IN_LOOP mode
4609 or not, which in turn determines if the misalignment is computed inside
4610 the inner-loop, or outside LOOP. */
4612 if (init_addr
!= NULL_TREE
|| !loop_vinfo
)
4614 compute_in_loop
= true;
4615 gcc_assert (alignment_support_scheme
== dr_explicit_realign
);
4619 /* 2. Determine where to generate the extra vector load.
4621 For the optimized realignment scheme, instead of generating two vector
4622 loads in each iteration, we generate a single extra vector load in the
4623 preheader of the loop, and in each iteration reuse the result of the
4624 vector load from the previous iteration. In case the memory access is in
4625 an inner-loop nested inside LOOP, which is now being vectorized using
4626 outer-loop vectorization, we need to determine whether this initial vector
4627 load should be generated at the preheader of the inner-loop, or can be
4628 generated at the preheader of LOOP. If the memory access has no evolution
4629 in LOOP, it can be generated in the preheader of LOOP. Otherwise, it has
4630 to be generated inside LOOP (in the preheader of the inner-loop). */
4632 if (nested_in_vect_loop
)
4634 tree outerloop_step
= STMT_VINFO_DR_STEP (stmt_info
);
4635 bool invariant_in_outerloop
=
4636 (tree_int_cst_compare (outerloop_step
, size_zero_node
) == 0);
4637 loop_for_initial_load
= (invariant_in_outerloop
? loop
: loop
->inner
);
4640 loop_for_initial_load
= loop
;
4642 *at_loop
= loop_for_initial_load
;
4644 if (loop_for_initial_load
)
4645 pe
= loop_preheader_edge (loop_for_initial_load
);
4647 /* 3. For the case of the optimized realignment, create the first vector
4648 load at the loop preheader. */
4650 if (alignment_support_scheme
== dr_explicit_realign_optimized
)
4652 /* Create msq_init = *(floor(p1)) in the loop preheader */
4654 gcc_assert (!compute_in_loop
);
4655 vec_dest
= vect_create_destination_var (scalar_dest
, vectype
);
4656 ptr
= vect_create_data_ref_ptr (stmt
, vectype
, loop_for_initial_load
,
4657 NULL_TREE
, &init_addr
, NULL
, &inc
,
4659 new_temp
= copy_ssa_name (ptr
, NULL
);
4660 new_stmt
= gimple_build_assign_with_ops
4661 (BIT_AND_EXPR
, new_temp
, ptr
,
4662 build_int_cst (TREE_TYPE (ptr
),
4663 -(HOST_WIDE_INT
)TYPE_ALIGN_UNIT (vectype
)));
4664 new_bb
= gsi_insert_on_edge_immediate (pe
, new_stmt
);
4665 gcc_assert (!new_bb
);
4667 = build2 (MEM_REF
, TREE_TYPE (vec_dest
), new_temp
,
4668 build_int_cst (reference_alias_ptr_type (DR_REF (dr
)), 0));
4669 new_stmt
= gimple_build_assign (vec_dest
, data_ref
);
4670 new_temp
= make_ssa_name (vec_dest
, new_stmt
);
4671 gimple_assign_set_lhs (new_stmt
, new_temp
);
4674 new_bb
= gsi_insert_on_edge_immediate (pe
, new_stmt
);
4675 gcc_assert (!new_bb
);
4678 gsi_insert_before (gsi
, new_stmt
, GSI_SAME_STMT
);
4680 msq_init
= gimple_assign_lhs (new_stmt
);
4683 /* 4. Create realignment token using a target builtin, if available.
4684 It is done either inside the containing loop, or before LOOP (as
4685 determined above). */
4687 if (targetm
.vectorize
.builtin_mask_for_load
)
4691 /* Compute INIT_ADDR - the initial addressed accessed by this memref. */
4694 /* Generate the INIT_ADDR computation outside LOOP. */
4695 init_addr
= vect_create_addr_base_for_vector_ref (stmt
, &stmts
,
4699 pe
= loop_preheader_edge (loop
);
4700 new_bb
= gsi_insert_seq_on_edge_immediate (pe
, stmts
);
4701 gcc_assert (!new_bb
);
4704 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
4707 builtin_decl
= targetm
.vectorize
.builtin_mask_for_load ();
4708 new_stmt
= gimple_build_call (builtin_decl
, 1, init_addr
);
4710 vect_create_destination_var (scalar_dest
,
4711 gimple_call_return_type (new_stmt
));
4712 new_temp
= make_ssa_name (vec_dest
, new_stmt
);
4713 gimple_call_set_lhs (new_stmt
, new_temp
);
4715 if (compute_in_loop
)
4716 gsi_insert_before (gsi
, new_stmt
, GSI_SAME_STMT
);
4719 /* Generate the misalignment computation outside LOOP. */
4720 pe
= loop_preheader_edge (loop
);
4721 new_bb
= gsi_insert_on_edge_immediate (pe
, new_stmt
);
4722 gcc_assert (!new_bb
);
4725 *realignment_token
= gimple_call_lhs (new_stmt
);
4727 /* The result of the CALL_EXPR to this builtin is determined from
4728 the value of the parameter and no global variables are touched
4729 which makes the builtin a "const" function. Requiring the
4730 builtin to have the "const" attribute makes it unnecessary
4731 to call mark_call_clobbered. */
4732 gcc_assert (TREE_READONLY (builtin_decl
));
4735 if (alignment_support_scheme
== dr_explicit_realign
)
4738 gcc_assert (!compute_in_loop
);
4739 gcc_assert (alignment_support_scheme
== dr_explicit_realign_optimized
);
4742 /* 5. Create msq = phi <msq_init, lsq> in loop */
4744 pe
= loop_preheader_edge (containing_loop
);
4745 vec_dest
= vect_create_destination_var (scalar_dest
, vectype
);
4746 msq
= make_ssa_name (vec_dest
, NULL
);
4747 phi_stmt
= create_phi_node (msq
, containing_loop
->header
);
4748 add_phi_arg (phi_stmt
, msq_init
, pe
, UNKNOWN_LOCATION
);
4754 /* Function vect_grouped_load_supported.
4756 Returns TRUE if even and odd permutations are supported,
4757 and FALSE otherwise. */
4760 vect_grouped_load_supported (tree vectype
, unsigned HOST_WIDE_INT count
)
4762 enum machine_mode mode
= TYPE_MODE (vectype
);
4764 /* vect_permute_load_chain requires the group size to be a power of two. */
4765 if (exact_log2 (count
) == -1)
4767 if (dump_enabled_p ())
4768 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
4769 "the size of the group of accesses"
4770 " is not a power of 2\n");
4774 /* Check that the permutation is supported. */
4775 if (VECTOR_MODE_P (mode
))
4777 unsigned int i
, nelt
= GET_MODE_NUNITS (mode
);
4778 unsigned char *sel
= XALLOCAVEC (unsigned char, nelt
);
4780 for (i
= 0; i
< nelt
; i
++)
4782 if (can_vec_perm_p (mode
, false, sel
))
4784 for (i
= 0; i
< nelt
; i
++)
4786 if (can_vec_perm_p (mode
, false, sel
))
4791 if (dump_enabled_p ())
4792 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
4793 "extract even/odd not supported by target\n");
4797 /* Return TRUE if vec_load_lanes is available for COUNT vectors of
4801 vect_load_lanes_supported (tree vectype
, unsigned HOST_WIDE_INT count
)
4803 return vect_lanes_optab_supported_p ("vec_load_lanes",
4804 vec_load_lanes_optab
,
4808 /* Function vect_permute_load_chain.
4810 Given a chain of interleaved loads in DR_CHAIN of LENGTH that must be
4811 a power of 2, generate extract_even/odd stmts to reorder the input data
4812 correctly. Return the final references for loads in RESULT_CHAIN.
4814 E.g., LENGTH is 4 and the scalar type is short, i.e., VF is 8.
4815 The input is 4 vectors each containing 8 elements. We assign a number to each
4816 element, the input sequence is:
4818 1st vec: 0 1 2 3 4 5 6 7
4819 2nd vec: 8 9 10 11 12 13 14 15
4820 3rd vec: 16 17 18 19 20 21 22 23
4821 4th vec: 24 25 26 27 28 29 30 31
4823 The output sequence should be:
4825 1st vec: 0 4 8 12 16 20 24 28
4826 2nd vec: 1 5 9 13 17 21 25 29
4827 3rd vec: 2 6 10 14 18 22 26 30
4828 4th vec: 3 7 11 15 19 23 27 31
4830 i.e., the first output vector should contain the first elements of each
4831 interleaving group, etc.
4833 We use extract_even/odd instructions to create such output. The input of
4834 each extract_even/odd operation is two vectors
4838 and the output is the vector of extracted even/odd elements. The output of
4839 extract_even will be: 0 2 4 6
4840 and of extract_odd: 1 3 5 7
4843 The permutation is done in log LENGTH stages. In each stage extract_even
4844 and extract_odd stmts are created for each pair of vectors in DR_CHAIN in
4845 their order. In our example,
4847 E1: extract_even (1st vec, 2nd vec)
4848 E2: extract_odd (1st vec, 2nd vec)
4849 E3: extract_even (3rd vec, 4th vec)
4850 E4: extract_odd (3rd vec, 4th vec)
4852 The output for the first stage will be:
4854 E1: 0 2 4 6 8 10 12 14
4855 E2: 1 3 5 7 9 11 13 15
4856 E3: 16 18 20 22 24 26 28 30
4857 E4: 17 19 21 23 25 27 29 31
4859 In order to proceed and create the correct sequence for the next stage (or
4860 for the correct output, if the second stage is the last one, as in our
4861 example), we first put the output of extract_even operation and then the
4862 output of extract_odd in RESULT_CHAIN (which is then copied to DR_CHAIN).
4863 The input for the second stage is:
4865 1st vec (E1): 0 2 4 6 8 10 12 14
4866 2nd vec (E3): 16 18 20 22 24 26 28 30
4867 3rd vec (E2): 1 3 5 7 9 11 13 15
4868 4th vec (E4): 17 19 21 23 25 27 29 31
4870 The output of the second stage:
4872 E1: 0 4 8 12 16 20 24 28
4873 E2: 2 6 10 14 18 22 26 30
4874 E3: 1 5 9 13 17 21 25 29
4875 E4: 3 7 11 15 19 23 27 31
4877 And RESULT_CHAIN after reordering:
4879 1st vec (E1): 0 4 8 12 16 20 24 28
4880 2nd vec (E3): 1 5 9 13 17 21 25 29
4881 3rd vec (E2): 2 6 10 14 18 22 26 30
4882 4th vec (E4): 3 7 11 15 19 23 27 31. */
4885 vect_permute_load_chain (vec
<tree
> dr_chain
,
4886 unsigned int length
,
4888 gimple_stmt_iterator
*gsi
,
4889 vec
<tree
> *result_chain
)
4891 tree data_ref
, first_vect
, second_vect
;
4892 tree perm_mask_even
, perm_mask_odd
;
4894 tree vectype
= STMT_VINFO_VECTYPE (vinfo_for_stmt (stmt
));
4895 unsigned int i
, j
, log_length
= exact_log2 (length
);
4896 unsigned nelt
= TYPE_VECTOR_SUBPARTS (vectype
);
4897 unsigned char *sel
= XALLOCAVEC (unsigned char, nelt
);
4899 result_chain
->quick_grow (length
);
4900 memcpy (result_chain
->address (), dr_chain
.address (),
4901 length
* sizeof (tree
));
4903 for (i
= 0; i
< nelt
; ++i
)
4905 perm_mask_even
= vect_gen_perm_mask (vectype
, sel
);
4906 gcc_assert (perm_mask_even
!= NULL
);
4908 for (i
= 0; i
< nelt
; ++i
)
4910 perm_mask_odd
= vect_gen_perm_mask (vectype
, sel
);
4911 gcc_assert (perm_mask_odd
!= NULL
);
4913 for (i
= 0; i
< log_length
; i
++)
4915 for (j
= 0; j
< length
; j
+= 2)
4917 first_vect
= dr_chain
[j
];
4918 second_vect
= dr_chain
[j
+1];
4920 /* data_ref = permute_even (first_data_ref, second_data_ref); */
4921 data_ref
= make_temp_ssa_name (vectype
, NULL
, "vect_perm_even");
4922 perm_stmt
= gimple_build_assign_with_ops (VEC_PERM_EXPR
, data_ref
,
4923 first_vect
, second_vect
,
4925 vect_finish_stmt_generation (stmt
, perm_stmt
, gsi
);
4926 (*result_chain
)[j
/2] = data_ref
;
4928 /* data_ref = permute_odd (first_data_ref, second_data_ref); */
4929 data_ref
= make_temp_ssa_name (vectype
, NULL
, "vect_perm_odd");
4930 perm_stmt
= gimple_build_assign_with_ops (VEC_PERM_EXPR
, data_ref
,
4931 first_vect
, second_vect
,
4933 vect_finish_stmt_generation (stmt
, perm_stmt
, gsi
);
4934 (*result_chain
)[j
/2+length
/2] = data_ref
;
4936 memcpy (dr_chain
.address (), result_chain
->address (),
4937 length
* sizeof (tree
));
4942 /* Function vect_transform_grouped_load.
4944 Given a chain of input interleaved data-refs (in DR_CHAIN), build statements
4945 to perform their permutation and ascribe the result vectorized statements to
4946 the scalar statements.
4950 vect_transform_grouped_load (gimple stmt
, vec
<tree
> dr_chain
, int size
,
4951 gimple_stmt_iterator
*gsi
)
4953 vec
<tree
> result_chain
= vNULL
;
4955 /* DR_CHAIN contains input data-refs that are a part of the interleaving.
4956 RESULT_CHAIN is the output of vect_permute_load_chain, it contains permuted
4957 vectors, that are ready for vector computation. */
4958 result_chain
.create (size
);
4959 vect_permute_load_chain (dr_chain
, size
, stmt
, gsi
, &result_chain
);
4960 vect_record_grouped_load_vectors (stmt
, result_chain
);
4961 result_chain
.release ();
4964 /* RESULT_CHAIN contains the output of a group of grouped loads that were
4965 generated as part of the vectorization of STMT. Assign the statement
4966 for each vector to the associated scalar statement. */
4969 vect_record_grouped_load_vectors (gimple stmt
, vec
<tree
> result_chain
)
4971 gimple first_stmt
= GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt
));
4972 gimple next_stmt
, new_stmt
;
4973 unsigned int i
, gap_count
;
4976 /* Put a permuted data-ref in the VECTORIZED_STMT field.
4977 Since we scan the chain starting from it's first node, their order
4978 corresponds the order of data-refs in RESULT_CHAIN. */
4979 next_stmt
= first_stmt
;
4981 FOR_EACH_VEC_ELT (result_chain
, i
, tmp_data_ref
)
4986 /* Skip the gaps. Loads created for the gaps will be removed by dead
4987 code elimination pass later. No need to check for the first stmt in
4988 the group, since it always exists.
4989 GROUP_GAP is the number of steps in elements from the previous
4990 access (if there is no gap GROUP_GAP is 1). We skip loads that
4991 correspond to the gaps. */
4992 if (next_stmt
!= first_stmt
4993 && gap_count
< GROUP_GAP (vinfo_for_stmt (next_stmt
)))
5001 new_stmt
= SSA_NAME_DEF_STMT (tmp_data_ref
);
5002 /* We assume that if VEC_STMT is not NULL, this is a case of multiple
5003 copies, and we put the new vector statement in the first available
5005 if (!STMT_VINFO_VEC_STMT (vinfo_for_stmt (next_stmt
)))
5006 STMT_VINFO_VEC_STMT (vinfo_for_stmt (next_stmt
)) = new_stmt
;
5009 if (!GROUP_SAME_DR_STMT (vinfo_for_stmt (next_stmt
)))
5012 STMT_VINFO_VEC_STMT (vinfo_for_stmt (next_stmt
));
5014 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (prev_stmt
));
5017 prev_stmt
= rel_stmt
;
5019 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (rel_stmt
));
5022 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (prev_stmt
)) =
5027 next_stmt
= GROUP_NEXT_ELEMENT (vinfo_for_stmt (next_stmt
));
5029 /* If NEXT_STMT accesses the same DR as the previous statement,
5030 put the same TMP_DATA_REF as its vectorized statement; otherwise
5031 get the next data-ref from RESULT_CHAIN. */
5032 if (!next_stmt
|| !GROUP_SAME_DR_STMT (vinfo_for_stmt (next_stmt
)))
5038 /* Function vect_force_dr_alignment_p.
5040 Returns whether the alignment of a DECL can be forced to be aligned
5041 on ALIGNMENT bit boundary. */
5044 vect_can_force_dr_alignment_p (const_tree decl
, unsigned int alignment
)
5046 if (TREE_CODE (decl
) != VAR_DECL
)
5049 /* We cannot change alignment of common or external symbols as another
5050 translation unit may contain a definition with lower alignment.
5051 The rules of common symbol linking mean that the definition
5052 will override the common symbol. The same is true for constant
5053 pool entries which may be shared and are not properly merged
5055 if (DECL_EXTERNAL (decl
)
5056 || DECL_COMMON (decl
)
5057 || DECL_IN_CONSTANT_POOL (decl
))
5060 if (TREE_ASM_WRITTEN (decl
))
5063 /* Do not override the alignment as specified by the ABI when the used
5064 attribute is set. */
5065 if (DECL_PRESERVE_P (decl
))
5068 /* Do not override explicit alignment set by the user when an explicit
5069 section name is also used. This is a common idiom used by many
5070 software projects. */
5071 if (DECL_SECTION_NAME (decl
) != NULL_TREE
5072 && !DECL_HAS_IMPLICIT_SECTION_NAME_P (decl
))
5075 if (TREE_STATIC (decl
))
5076 return (alignment
<= MAX_OFILE_ALIGNMENT
);
5078 return (alignment
<= MAX_STACK_ALIGNMENT
);
5082 /* Return whether the data reference DR is supported with respect to its
5084 If CHECK_ALIGNED_ACCESSES is TRUE, check if the access is supported even
5085 it is aligned, i.e., check if it is possible to vectorize it with different
5088 enum dr_alignment_support
5089 vect_supportable_dr_alignment (struct data_reference
*dr
,
5090 bool check_aligned_accesses
)
5092 gimple stmt
= DR_STMT (dr
);
5093 stmt_vec_info stmt_info
= vinfo_for_stmt (stmt
);
5094 tree vectype
= STMT_VINFO_VECTYPE (stmt_info
);
5095 enum machine_mode mode
= TYPE_MODE (vectype
);
5096 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_info
);
5097 struct loop
*vect_loop
= NULL
;
5098 bool nested_in_vect_loop
= false;
5100 if (aligned_access_p (dr
) && !check_aligned_accesses
)
5103 /* For now assume all conditional loads/stores support unaligned
5104 access without any special code. */
5105 if (is_gimple_call (stmt
)
5106 && gimple_call_internal_p (stmt
)
5107 && (gimple_call_internal_fn (stmt
) == IFN_MASK_LOAD
5108 || gimple_call_internal_fn (stmt
) == IFN_MASK_STORE
))
5109 return dr_unaligned_supported
;
5113 vect_loop
= LOOP_VINFO_LOOP (loop_vinfo
);
5114 nested_in_vect_loop
= nested_in_vect_loop_p (vect_loop
, stmt
);
5117 /* Possibly unaligned access. */
5119 /* We can choose between using the implicit realignment scheme (generating
5120 a misaligned_move stmt) and the explicit realignment scheme (generating
5121 aligned loads with a REALIGN_LOAD). There are two variants to the
5122 explicit realignment scheme: optimized, and unoptimized.
5123 We can optimize the realignment only if the step between consecutive
5124 vector loads is equal to the vector size. Since the vector memory
5125 accesses advance in steps of VS (Vector Size) in the vectorized loop, it
5126 is guaranteed that the misalignment amount remains the same throughout the
5127 execution of the vectorized loop. Therefore, we can create the
5128 "realignment token" (the permutation mask that is passed to REALIGN_LOAD)
5129 at the loop preheader.
5131 However, in the case of outer-loop vectorization, when vectorizing a
5132 memory access in the inner-loop nested within the LOOP that is now being
5133 vectorized, while it is guaranteed that the misalignment of the
5134 vectorized memory access will remain the same in different outer-loop
5135 iterations, it is *not* guaranteed that is will remain the same throughout
5136 the execution of the inner-loop. This is because the inner-loop advances
5137 with the original scalar step (and not in steps of VS). If the inner-loop
5138 step happens to be a multiple of VS, then the misalignment remains fixed
5139 and we can use the optimized realignment scheme. For example:
5145 When vectorizing the i-loop in the above example, the step between
5146 consecutive vector loads is 1, and so the misalignment does not remain
5147 fixed across the execution of the inner-loop, and the realignment cannot
5148 be optimized (as illustrated in the following pseudo vectorized loop):
5150 for (i=0; i<N; i+=4)
5151 for (j=0; j<M; j++){
5152 vs += vp[i+j]; // misalignment of &vp[i+j] is {0,1,2,3,0,1,2,3,...}
5153 // when j is {0,1,2,3,4,5,6,7,...} respectively.
5154 // (assuming that we start from an aligned address).
5157 We therefore have to use the unoptimized realignment scheme:
5159 for (i=0; i<N; i+=4)
5160 for (j=k; j<M; j+=4)
5161 vs += vp[i+j]; // misalignment of &vp[i+j] is always k (assuming
5162 // that the misalignment of the initial address is
5165 The loop can then be vectorized as follows:
5167 for (k=0; k<4; k++){
5168 rt = get_realignment_token (&vp[k]);
5169 for (i=0; i<N; i+=4){
5171 for (j=k; j<M; j+=4){
5173 va = REALIGN_LOAD <v1,v2,rt>;
5180 if (DR_IS_READ (dr
))
5182 bool is_packed
= false;
5183 tree type
= (TREE_TYPE (DR_REF (dr
)));
5185 if (optab_handler (vec_realign_load_optab
, mode
) != CODE_FOR_nothing
5186 && (!targetm
.vectorize
.builtin_mask_for_load
5187 || targetm
.vectorize
.builtin_mask_for_load ()))
5189 tree vectype
= STMT_VINFO_VECTYPE (stmt_info
);
5190 if ((nested_in_vect_loop
5191 && (TREE_INT_CST_LOW (DR_STEP (dr
))
5192 != GET_MODE_SIZE (TYPE_MODE (vectype
))))
5194 return dr_explicit_realign
;
5196 return dr_explicit_realign_optimized
;
5198 if (!known_alignment_for_access_p (dr
))
5199 is_packed
= not_size_aligned (DR_REF (dr
));
5201 if ((TYPE_USER_ALIGN (type
) && !is_packed
)
5202 || targetm
.vectorize
.
5203 support_vector_misalignment (mode
, type
,
5204 DR_MISALIGNMENT (dr
), is_packed
))
5205 /* Can't software pipeline the loads, but can at least do them. */
5206 return dr_unaligned_supported
;
5210 bool is_packed
= false;
5211 tree type
= (TREE_TYPE (DR_REF (dr
)));
5213 if (!known_alignment_for_access_p (dr
))
5214 is_packed
= not_size_aligned (DR_REF (dr
));
5216 if ((TYPE_USER_ALIGN (type
) && !is_packed
)
5217 || targetm
.vectorize
.
5218 support_vector_misalignment (mode
, type
,
5219 DR_MISALIGNMENT (dr
), is_packed
))
5220 return dr_unaligned_supported
;
5224 return dr_unaligned_unsupported
;