1 /* Data References Analysis and Manipulation Utilities for Vectorization.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
4 Contributed by Dorit Naishlos <dorit@il.ibm.com>
5 and Ira Rosen <irar@il.ibm.com>
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
25 #include "coretypes.h"
30 #include "basic-block.h"
31 #include "tree-pretty-print.h"
32 #include "gimple-pretty-print.h"
33 #include "tree-flow.h"
34 #include "tree-dump.h"
36 #include "tree-chrec.h"
37 #include "tree-scalar-evolution.h"
38 #include "tree-vectorizer.h"
41 /* Need to include rtl.h, expr.h, etc. for optabs. */
45 /* Return the smallest scalar part of STMT.
46 This is used to determine the vectype of the stmt. We generally set the
47 vectype according to the type of the result (lhs). For stmts whose
48 result-type is different than the type of the arguments (e.g., demotion,
49 promotion), vectype will be reset appropriately (later). Note that we have
50 to visit the smallest datatype in this function, because that determines the
51 VF. If the smallest datatype in the loop is present only as the rhs of a
52 promotion operation - we'd miss it.
53 Such a case, where a variable of this datatype does not appear in the lhs
54 anywhere in the loop, can only occur if it's an invariant: e.g.:
55 'int_x = (int) short_inv', which we'd expect to have been optimized away by
56 invariant motion. However, we cannot rely on invariant motion to always take
57 invariants out of the loop, and so in the case of promotion we also have to
59 LHS_SIZE_UNIT and RHS_SIZE_UNIT contain the sizes of the corresponding
63 vect_get_smallest_scalar_type (gimple stmt
, HOST_WIDE_INT
*lhs_size_unit
,
64 HOST_WIDE_INT
*rhs_size_unit
)
66 tree scalar_type
= gimple_expr_type (stmt
);
67 HOST_WIDE_INT lhs
, rhs
;
69 lhs
= rhs
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (scalar_type
));
71 if (is_gimple_assign (stmt
)
72 && (gimple_assign_cast_p (stmt
)
73 || gimple_assign_rhs_code (stmt
) == WIDEN_MULT_EXPR
74 || gimple_assign_rhs_code (stmt
) == FLOAT_EXPR
))
76 tree rhs_type
= TREE_TYPE (gimple_assign_rhs1 (stmt
));
78 rhs
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (rhs_type
));
80 scalar_type
= rhs_type
;
89 /* Find the place of the data-ref in STMT in the interleaving chain that starts
90 from FIRST_STMT. Return -1 if the data-ref is not a part of the chain. */
93 vect_get_place_in_interleaving_chain (gimple stmt
, gimple first_stmt
)
95 gimple next_stmt
= first_stmt
;
98 if (first_stmt
!= DR_GROUP_FIRST_DR (vinfo_for_stmt (stmt
)))
101 while (next_stmt
&& next_stmt
!= stmt
)
104 next_stmt
= DR_GROUP_NEXT_DR (vinfo_for_stmt (next_stmt
));
114 /* Function vect_insert_into_interleaving_chain.
116 Insert DRA into the interleaving chain of DRB according to DRA's INIT. */
119 vect_insert_into_interleaving_chain (struct data_reference
*dra
,
120 struct data_reference
*drb
)
124 stmt_vec_info stmtinfo_a
= vinfo_for_stmt (DR_STMT (dra
));
125 stmt_vec_info stmtinfo_b
= vinfo_for_stmt (DR_STMT (drb
));
127 prev
= DR_GROUP_FIRST_DR (stmtinfo_b
);
128 next
= DR_GROUP_NEXT_DR (vinfo_for_stmt (prev
));
131 next_init
= DR_INIT (STMT_VINFO_DATA_REF (vinfo_for_stmt (next
)));
132 if (tree_int_cst_compare (next_init
, DR_INIT (dra
)) > 0)
135 DR_GROUP_NEXT_DR (vinfo_for_stmt (prev
)) = DR_STMT (dra
);
136 DR_GROUP_NEXT_DR (stmtinfo_a
) = next
;
140 next
= DR_GROUP_NEXT_DR (vinfo_for_stmt (prev
));
143 /* We got to the end of the list. Insert here. */
144 DR_GROUP_NEXT_DR (vinfo_for_stmt (prev
)) = DR_STMT (dra
);
145 DR_GROUP_NEXT_DR (stmtinfo_a
) = NULL
;
149 /* Function vect_update_interleaving_chain.
151 For two data-refs DRA and DRB that are a part of a chain interleaved data
152 accesses, update the interleaving chain. DRB's INIT is smaller than DRA's.
154 There are four possible cases:
155 1. New stmts - both DRA and DRB are not a part of any chain:
158 2. DRB is a part of a chain and DRA is not:
159 no need to update FIRST_DR
160 no need to insert DRB
161 insert DRA according to init
162 3. DRA is a part of a chain and DRB is not:
163 if (init of FIRST_DR > init of DRB)
165 NEXT(FIRST_DR) = previous FIRST_DR
167 insert DRB according to its init
168 4. both DRA and DRB are in some interleaving chains:
169 choose the chain with the smallest init of FIRST_DR
170 insert the nodes of the second chain into the first one. */
173 vect_update_interleaving_chain (struct data_reference
*drb
,
174 struct data_reference
*dra
)
176 stmt_vec_info stmtinfo_a
= vinfo_for_stmt (DR_STMT (dra
));
177 stmt_vec_info stmtinfo_b
= vinfo_for_stmt (DR_STMT (drb
));
178 tree next_init
, init_dra_chain
, init_drb_chain
;
179 gimple first_a
, first_b
;
181 gimple node
, prev
, next
, first_stmt
;
183 /* 1. New stmts - both DRA and DRB are not a part of any chain. */
184 if (!DR_GROUP_FIRST_DR (stmtinfo_a
) && !DR_GROUP_FIRST_DR (stmtinfo_b
))
186 DR_GROUP_FIRST_DR (stmtinfo_a
) = DR_STMT (drb
);
187 DR_GROUP_FIRST_DR (stmtinfo_b
) = DR_STMT (drb
);
188 DR_GROUP_NEXT_DR (stmtinfo_b
) = DR_STMT (dra
);
192 /* 2. DRB is a part of a chain and DRA is not. */
193 if (!DR_GROUP_FIRST_DR (stmtinfo_a
) && DR_GROUP_FIRST_DR (stmtinfo_b
))
195 DR_GROUP_FIRST_DR (stmtinfo_a
) = DR_GROUP_FIRST_DR (stmtinfo_b
);
196 /* Insert DRA into the chain of DRB. */
197 vect_insert_into_interleaving_chain (dra
, drb
);
201 /* 3. DRA is a part of a chain and DRB is not. */
202 if (DR_GROUP_FIRST_DR (stmtinfo_a
) && !DR_GROUP_FIRST_DR (stmtinfo_b
))
204 gimple old_first_stmt
= DR_GROUP_FIRST_DR (stmtinfo_a
);
205 tree init_old
= DR_INIT (STMT_VINFO_DATA_REF (vinfo_for_stmt (
209 if (tree_int_cst_compare (init_old
, DR_INIT (drb
)) > 0)
211 /* DRB's init is smaller than the init of the stmt previously marked
212 as the first stmt of the interleaving chain of DRA. Therefore, we
213 update FIRST_STMT and put DRB in the head of the list. */
214 DR_GROUP_FIRST_DR (stmtinfo_b
) = DR_STMT (drb
);
215 DR_GROUP_NEXT_DR (stmtinfo_b
) = old_first_stmt
;
217 /* Update all the stmts in the list to point to the new FIRST_STMT. */
218 tmp
= old_first_stmt
;
221 DR_GROUP_FIRST_DR (vinfo_for_stmt (tmp
)) = DR_STMT (drb
);
222 tmp
= DR_GROUP_NEXT_DR (vinfo_for_stmt (tmp
));
227 /* Insert DRB in the list of DRA. */
228 vect_insert_into_interleaving_chain (drb
, dra
);
229 DR_GROUP_FIRST_DR (stmtinfo_b
) = DR_GROUP_FIRST_DR (stmtinfo_a
);
234 /* 4. both DRA and DRB are in some interleaving chains. */
235 first_a
= DR_GROUP_FIRST_DR (stmtinfo_a
);
236 first_b
= DR_GROUP_FIRST_DR (stmtinfo_b
);
237 if (first_a
== first_b
)
239 init_dra_chain
= DR_INIT (STMT_VINFO_DATA_REF (vinfo_for_stmt (first_a
)));
240 init_drb_chain
= DR_INIT (STMT_VINFO_DATA_REF (vinfo_for_stmt (first_b
)));
242 if (tree_int_cst_compare (init_dra_chain
, init_drb_chain
) > 0)
244 /* Insert the nodes of DRA chain into the DRB chain.
245 After inserting a node, continue from this node of the DRB chain (don't
246 start from the beginning. */
247 node
= DR_GROUP_FIRST_DR (stmtinfo_a
);
248 prev
= DR_GROUP_FIRST_DR (stmtinfo_b
);
249 first_stmt
= first_b
;
253 /* Insert the nodes of DRB chain into the DRA chain.
254 After inserting a node, continue from this node of the DRA chain (don't
255 start from the beginning. */
256 node
= DR_GROUP_FIRST_DR (stmtinfo_b
);
257 prev
= DR_GROUP_FIRST_DR (stmtinfo_a
);
258 first_stmt
= first_a
;
263 node_init
= DR_INIT (STMT_VINFO_DATA_REF (vinfo_for_stmt (node
)));
264 next
= DR_GROUP_NEXT_DR (vinfo_for_stmt (prev
));
267 next_init
= DR_INIT (STMT_VINFO_DATA_REF (vinfo_for_stmt (next
)));
268 if (tree_int_cst_compare (next_init
, node_init
) > 0)
271 DR_GROUP_NEXT_DR (vinfo_for_stmt (prev
)) = node
;
272 DR_GROUP_NEXT_DR (vinfo_for_stmt (node
)) = next
;
277 next
= DR_GROUP_NEXT_DR (vinfo_for_stmt (prev
));
281 /* We got to the end of the list. Insert here. */
282 DR_GROUP_NEXT_DR (vinfo_for_stmt (prev
)) = node
;
283 DR_GROUP_NEXT_DR (vinfo_for_stmt (node
)) = NULL
;
286 DR_GROUP_FIRST_DR (vinfo_for_stmt (node
)) = first_stmt
;
287 node
= DR_GROUP_NEXT_DR (vinfo_for_stmt (node
));
292 /* Function vect_equal_offsets.
294 Check if OFFSET1 and OFFSET2 are identical expressions. */
297 vect_equal_offsets (tree offset1
, tree offset2
)
301 STRIP_NOPS (offset1
);
302 STRIP_NOPS (offset2
);
304 if (offset1
== offset2
)
307 if (TREE_CODE (offset1
) != TREE_CODE (offset2
)
308 || (!BINARY_CLASS_P (offset1
) && !UNARY_CLASS_P (offset1
)))
311 res
= vect_equal_offsets (TREE_OPERAND (offset1
, 0),
312 TREE_OPERAND (offset2
, 0));
314 if (!res
|| !BINARY_CLASS_P (offset1
))
317 res
= vect_equal_offsets (TREE_OPERAND (offset1
, 1),
318 TREE_OPERAND (offset2
, 1));
324 /* Function vect_check_interleaving.
326 Check if DRA and DRB are a part of interleaving. In case they are, insert
327 DRA and DRB in an interleaving chain. */
330 vect_check_interleaving (struct data_reference
*dra
,
331 struct data_reference
*drb
)
333 HOST_WIDE_INT type_size_a
, type_size_b
, diff_mod_size
, step
, init_a
, init_b
;
335 /* Check that the data-refs have same first location (except init) and they
336 are both either store or load (not load and store). */
337 if ((DR_BASE_ADDRESS (dra
) != DR_BASE_ADDRESS (drb
)
338 && (TREE_CODE (DR_BASE_ADDRESS (dra
)) != ADDR_EXPR
339 || TREE_CODE (DR_BASE_ADDRESS (drb
)) != ADDR_EXPR
340 || TREE_OPERAND (DR_BASE_ADDRESS (dra
), 0)
341 != TREE_OPERAND (DR_BASE_ADDRESS (drb
),0)))
342 || !vect_equal_offsets (DR_OFFSET (dra
), DR_OFFSET (drb
))
343 || !tree_int_cst_compare (DR_INIT (dra
), DR_INIT (drb
))
344 || DR_IS_READ (dra
) != DR_IS_READ (drb
))
348 1. data-refs are of the same type
349 2. their steps are equal
350 3. the step (if greater than zero) is greater than the difference between
352 type_size_a
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dra
))));
353 type_size_b
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (drb
))));
355 if (type_size_a
!= type_size_b
356 || tree_int_cst_compare (DR_STEP (dra
), DR_STEP (drb
))
357 || !types_compatible_p (TREE_TYPE (DR_REF (dra
)),
358 TREE_TYPE (DR_REF (drb
))))
361 init_a
= TREE_INT_CST_LOW (DR_INIT (dra
));
362 init_b
= TREE_INT_CST_LOW (DR_INIT (drb
));
363 step
= TREE_INT_CST_LOW (DR_STEP (dra
));
367 /* If init_a == init_b + the size of the type * k, we have an interleaving,
368 and DRB is accessed before DRA. */
369 diff_mod_size
= (init_a
- init_b
) % type_size_a
;
371 if (step
&& (init_a
- init_b
) > step
)
374 if (diff_mod_size
== 0)
376 vect_update_interleaving_chain (drb
, dra
);
377 if (vect_print_dump_info (REPORT_DR_DETAILS
))
379 fprintf (vect_dump
, "Detected interleaving ");
380 print_generic_expr (vect_dump
, DR_REF (dra
), TDF_SLIM
);
381 fprintf (vect_dump
, " and ");
382 print_generic_expr (vect_dump
, DR_REF (drb
), TDF_SLIM
);
389 /* If init_b == init_a + the size of the type * k, we have an
390 interleaving, and DRA is accessed before DRB. */
391 diff_mod_size
= (init_b
- init_a
) % type_size_a
;
393 if (step
&& (init_b
- init_a
) > step
)
396 if (diff_mod_size
== 0)
398 vect_update_interleaving_chain (dra
, drb
);
399 if (vect_print_dump_info (REPORT_DR_DETAILS
))
401 fprintf (vect_dump
, "Detected interleaving ");
402 print_generic_expr (vect_dump
, DR_REF (dra
), TDF_SLIM
);
403 fprintf (vect_dump
, " and ");
404 print_generic_expr (vect_dump
, DR_REF (drb
), TDF_SLIM
);
413 /* Check if data references pointed by DR_I and DR_J are same or
414 belong to same interleaving group. Return FALSE if drs are
415 different, otherwise return TRUE. */
418 vect_same_range_drs (data_reference_p dr_i
, data_reference_p dr_j
)
420 gimple stmt_i
= DR_STMT (dr_i
);
421 gimple stmt_j
= DR_STMT (dr_j
);
423 if (operand_equal_p (DR_REF (dr_i
), DR_REF (dr_j
), 0)
424 || (DR_GROUP_FIRST_DR (vinfo_for_stmt (stmt_i
))
425 && DR_GROUP_FIRST_DR (vinfo_for_stmt (stmt_j
))
426 && (DR_GROUP_FIRST_DR (vinfo_for_stmt (stmt_i
))
427 == DR_GROUP_FIRST_DR (vinfo_for_stmt (stmt_j
)))))
433 /* If address ranges represented by DDR_I and DDR_J are equal,
434 return TRUE, otherwise return FALSE. */
437 vect_vfa_range_equal (ddr_p ddr_i
, ddr_p ddr_j
)
439 if ((vect_same_range_drs (DDR_A (ddr_i
), DDR_A (ddr_j
))
440 && vect_same_range_drs (DDR_B (ddr_i
), DDR_B (ddr_j
)))
441 || (vect_same_range_drs (DDR_A (ddr_i
), DDR_B (ddr_j
))
442 && vect_same_range_drs (DDR_B (ddr_i
), DDR_A (ddr_j
))))
448 /* Insert DDR into LOOP_VINFO list of ddrs that may alias and need to be
449 tested at run-time. Return TRUE if DDR was successfully inserted.
450 Return false if versioning is not supported. */
453 vect_mark_for_runtime_alias_test (ddr_p ddr
, loop_vec_info loop_vinfo
)
455 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
457 if ((unsigned) PARAM_VALUE (PARAM_VECT_MAX_VERSION_FOR_ALIAS_CHECKS
) == 0)
460 if (vect_print_dump_info (REPORT_DR_DETAILS
))
462 fprintf (vect_dump
, "mark for run-time aliasing test between ");
463 print_generic_expr (vect_dump
, DR_REF (DDR_A (ddr
)), TDF_SLIM
);
464 fprintf (vect_dump
, " and ");
465 print_generic_expr (vect_dump
, DR_REF (DDR_B (ddr
)), TDF_SLIM
);
468 if (optimize_loop_nest_for_size_p (loop
))
470 if (vect_print_dump_info (REPORT_DR_DETAILS
))
471 fprintf (vect_dump
, "versioning not supported when optimizing for size.");
475 /* FORNOW: We don't support versioning with outer-loop vectorization. */
478 if (vect_print_dump_info (REPORT_DR_DETAILS
))
479 fprintf (vect_dump
, "versioning not yet supported for outer-loops.");
483 VEC_safe_push (ddr_p
, heap
, LOOP_VINFO_MAY_ALIAS_DDRS (loop_vinfo
), ddr
);
488 /* Function vect_analyze_data_ref_dependence.
490 Return TRUE if there (might) exist a dependence between a memory-reference
491 DRA and a memory-reference DRB. When versioning for alias may check a
492 dependence at run-time, return FALSE. Adjust *MAX_VF according to
493 the data dependence. */
496 vect_analyze_data_ref_dependence (struct data_dependence_relation
*ddr
,
497 loop_vec_info loop_vinfo
, int *max_vf
)
500 struct loop
*loop
= NULL
;
501 struct data_reference
*dra
= DDR_A (ddr
);
502 struct data_reference
*drb
= DDR_B (ddr
);
503 stmt_vec_info stmtinfo_a
= vinfo_for_stmt (DR_STMT (dra
));
504 stmt_vec_info stmtinfo_b
= vinfo_for_stmt (DR_STMT (drb
));
505 lambda_vector dist_v
;
506 unsigned int loop_depth
;
508 /* Don't bother to analyze statements marked as unvectorizable. */
509 if (!STMT_VINFO_VECTORIZABLE (stmtinfo_a
)
510 || !STMT_VINFO_VECTORIZABLE (stmtinfo_b
))
513 if (DDR_ARE_DEPENDENT (ddr
) == chrec_known
)
515 /* Independent data accesses. */
516 vect_check_interleaving (dra
, drb
);
521 loop
= LOOP_VINFO_LOOP (loop_vinfo
);
523 if ((DR_IS_READ (dra
) && DR_IS_READ (drb
) && loop_vinfo
) || dra
== drb
)
526 if (DDR_ARE_DEPENDENT (ddr
) == chrec_dont_know
)
530 if (vect_print_dump_info (REPORT_DR_DETAILS
))
532 fprintf (vect_dump
, "versioning for alias required: "
533 "can't determine dependence between ");
534 print_generic_expr (vect_dump
, DR_REF (dra
), TDF_SLIM
);
535 fprintf (vect_dump
, " and ");
536 print_generic_expr (vect_dump
, DR_REF (drb
), TDF_SLIM
);
539 /* Add to list of ddrs that need to be tested at run-time. */
540 return !vect_mark_for_runtime_alias_test (ddr
, loop_vinfo
);
543 /* When vectorizing a basic block unknown depnedence can still mean
545 if (vect_check_interleaving (dra
, drb
))
548 if (vect_print_dump_info (REPORT_DR_DETAILS
))
550 fprintf (vect_dump
, "can't determine dependence between ");
551 print_generic_expr (vect_dump
, DR_REF (dra
), TDF_SLIM
);
552 fprintf (vect_dump
, " and ");
553 print_generic_expr (vect_dump
, DR_REF (drb
), TDF_SLIM
);
556 /* Mark the statements as unvectorizable. */
557 STMT_VINFO_VECTORIZABLE (stmtinfo_a
) = false;
558 STMT_VINFO_VECTORIZABLE (stmtinfo_b
) = false;
563 /* Versioning for alias is not yet supported for basic block SLP, and
564 dependence distance is unapplicable, hence, in case of known data
565 dependence, basic block vectorization is impossible for now. */
568 if (dra
!= drb
&& vect_check_interleaving (dra
, drb
))
571 if (vect_print_dump_info (REPORT_DR_DETAILS
))
573 fprintf (vect_dump
, "determined dependence between ");
574 print_generic_expr (vect_dump
, DR_REF (dra
), TDF_SLIM
);
575 fprintf (vect_dump
, " and ");
576 print_generic_expr (vect_dump
, DR_REF (drb
), TDF_SLIM
);
582 /* Loop-based vectorization and known data dependence. */
583 if (DDR_NUM_DIST_VECTS (ddr
) == 0)
585 if (vect_print_dump_info (REPORT_DR_DETAILS
))
587 fprintf (vect_dump
, "versioning for alias required: bad dist vector for ");
588 print_generic_expr (vect_dump
, DR_REF (dra
), TDF_SLIM
);
589 fprintf (vect_dump
, " and ");
590 print_generic_expr (vect_dump
, DR_REF (drb
), TDF_SLIM
);
592 /* Add to list of ddrs that need to be tested at run-time. */
593 return !vect_mark_for_runtime_alias_test (ddr
, loop_vinfo
);
596 loop_depth
= index_in_loop_nest (loop
->num
, DDR_LOOP_NEST (ddr
));
597 for (i
= 0; VEC_iterate (lambda_vector
, DDR_DIST_VECTS (ddr
), i
, dist_v
); i
++)
599 int dist
= dist_v
[loop_depth
];
601 if (vect_print_dump_info (REPORT_DR_DETAILS
))
602 fprintf (vect_dump
, "dependence distance = %d.", dist
);
606 if (vect_print_dump_info (REPORT_DR_DETAILS
))
608 fprintf (vect_dump
, "dependence distance == 0 between ");
609 print_generic_expr (vect_dump
, DR_REF (dra
), TDF_SLIM
);
610 fprintf (vect_dump
, " and ");
611 print_generic_expr (vect_dump
, DR_REF (drb
), TDF_SLIM
);
614 /* For interleaving, mark that there is a read-write dependency if
615 necessary. We check before that one of the data-refs is store. */
616 if (DR_IS_READ (dra
))
617 DR_GROUP_READ_WRITE_DEPENDENCE (stmtinfo_a
) = true;
620 if (DR_IS_READ (drb
))
621 DR_GROUP_READ_WRITE_DEPENDENCE (stmtinfo_b
) = true;
627 if (dist
> 0 && DDR_REVERSED_P (ddr
))
629 /* If DDR_REVERSED_P the order of the data-refs in DDR was
630 reversed (to make distance vector positive), and the actual
631 distance is negative. */
632 if (vect_print_dump_info (REPORT_DR_DETAILS
))
633 fprintf (vect_dump
, "dependence distance negative.");
638 && abs (dist
) < *max_vf
)
640 /* The dependence distance requires reduction of the maximal
641 vectorization factor. */
642 *max_vf
= abs (dist
);
643 if (vect_print_dump_info (REPORT_DR_DETAILS
))
644 fprintf (vect_dump
, "adjusting maximal vectorization factor to %i",
648 if (abs (dist
) >= *max_vf
)
650 /* Dependence distance does not create dependence, as far as
651 vectorization is concerned, in this case. */
652 if (vect_print_dump_info (REPORT_DR_DETAILS
))
653 fprintf (vect_dump
, "dependence distance >= VF.");
657 if (vect_print_dump_info (REPORT_UNVECTORIZED_LOCATIONS
))
659 fprintf (vect_dump
, "not vectorized, possible dependence "
660 "between data-refs ");
661 print_generic_expr (vect_dump
, DR_REF (dra
), TDF_SLIM
);
662 fprintf (vect_dump
, " and ");
663 print_generic_expr (vect_dump
, DR_REF (drb
), TDF_SLIM
);
672 /* Function vect_analyze_data_ref_dependences.
674 Examine all the data references in the loop, and make sure there do not
675 exist any data dependences between them. Set *MAX_VF according to
676 the maximum vectorization factor the data dependences allow. */
679 vect_analyze_data_ref_dependences (loop_vec_info loop_vinfo
,
680 bb_vec_info bb_vinfo
, int *max_vf
)
683 VEC (ddr_p
, heap
) *ddrs
= NULL
;
684 struct data_dependence_relation
*ddr
;
686 if (vect_print_dump_info (REPORT_DETAILS
))
687 fprintf (vect_dump
, "=== vect_analyze_dependences ===");
690 ddrs
= LOOP_VINFO_DDRS (loop_vinfo
);
692 ddrs
= BB_VINFO_DDRS (bb_vinfo
);
694 for (i
= 0; VEC_iterate (ddr_p
, ddrs
, i
, ddr
); i
++)
695 if (vect_analyze_data_ref_dependence (ddr
, loop_vinfo
, max_vf
))
702 /* Function vect_compute_data_ref_alignment
704 Compute the misalignment of the data reference DR.
707 1. If during the misalignment computation it is found that the data reference
708 cannot be vectorized then false is returned.
709 2. DR_MISALIGNMENT (DR) is defined.
711 FOR NOW: No analysis is actually performed. Misalignment is calculated
712 only for trivial cases. TODO. */
715 vect_compute_data_ref_alignment (struct data_reference
*dr
)
717 gimple stmt
= DR_STMT (dr
);
718 stmt_vec_info stmt_info
= vinfo_for_stmt (stmt
);
719 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_info
);
720 struct loop
*loop
= NULL
;
721 tree ref
= DR_REF (dr
);
723 tree base
, base_addr
;
726 tree aligned_to
, alignment
;
728 if (vect_print_dump_info (REPORT_DETAILS
))
729 fprintf (vect_dump
, "vect_compute_data_ref_alignment:");
732 loop
= LOOP_VINFO_LOOP (loop_vinfo
);
734 /* Initialize misalignment to unknown. */
735 SET_DR_MISALIGNMENT (dr
, -1);
737 misalign
= DR_INIT (dr
);
738 aligned_to
= DR_ALIGNED_TO (dr
);
739 base_addr
= DR_BASE_ADDRESS (dr
);
740 vectype
= STMT_VINFO_VECTYPE (stmt_info
);
742 /* In case the dataref is in an inner-loop of the loop that is being
743 vectorized (LOOP), we use the base and misalignment information
744 relative to the outer-loop (LOOP). This is ok only if the misalignment
745 stays the same throughout the execution of the inner-loop, which is why
746 we have to check that the stride of the dataref in the inner-loop evenly
747 divides by the vector size. */
748 if (loop
&& nested_in_vect_loop_p (loop
, stmt
))
750 tree step
= DR_STEP (dr
);
751 HOST_WIDE_INT dr_step
= TREE_INT_CST_LOW (step
);
753 if (dr_step
% GET_MODE_SIZE (TYPE_MODE (vectype
)) == 0)
755 if (vect_print_dump_info (REPORT_ALIGNMENT
))
756 fprintf (vect_dump
, "inner step divides the vector-size.");
757 misalign
= STMT_VINFO_DR_INIT (stmt_info
);
758 aligned_to
= STMT_VINFO_DR_ALIGNED_TO (stmt_info
);
759 base_addr
= STMT_VINFO_DR_BASE_ADDRESS (stmt_info
);
763 if (vect_print_dump_info (REPORT_ALIGNMENT
))
764 fprintf (vect_dump
, "inner step doesn't divide the vector-size.");
765 misalign
= NULL_TREE
;
769 base
= build_fold_indirect_ref (base_addr
);
770 alignment
= ssize_int (TYPE_ALIGN (vectype
)/BITS_PER_UNIT
);
772 if ((aligned_to
&& tree_int_cst_compare (aligned_to
, alignment
) < 0)
775 if (vect_print_dump_info (REPORT_ALIGNMENT
))
777 fprintf (vect_dump
, "Unknown alignment for access: ");
778 print_generic_expr (vect_dump
, base
, TDF_SLIM
);
784 && tree_int_cst_compare (ssize_int (DECL_ALIGN_UNIT (base
)),
786 || (TREE_CODE (base_addr
) == SSA_NAME
787 && tree_int_cst_compare (ssize_int (TYPE_ALIGN_UNIT (TREE_TYPE (
788 TREE_TYPE (base_addr
)))),
792 base_aligned
= false;
796 /* Do not change the alignment of global variables if
797 flag_section_anchors is enabled. */
798 if (!vect_can_force_dr_alignment_p (base
, TYPE_ALIGN (vectype
))
799 || (TREE_STATIC (base
) && flag_section_anchors
))
801 if (vect_print_dump_info (REPORT_DETAILS
))
803 fprintf (vect_dump
, "can't force alignment of ref: ");
804 print_generic_expr (vect_dump
, ref
, TDF_SLIM
);
809 /* Force the alignment of the decl.
810 NOTE: This is the only change to the code we make during
811 the analysis phase, before deciding to vectorize the loop. */
812 if (vect_print_dump_info (REPORT_DETAILS
))
813 fprintf (vect_dump
, "force alignment");
814 DECL_ALIGN (base
) = TYPE_ALIGN (vectype
);
815 DECL_USER_ALIGN (base
) = 1;
818 /* At this point we assume that the base is aligned. */
819 gcc_assert (base_aligned
820 || (TREE_CODE (base
) == VAR_DECL
821 && DECL_ALIGN (base
) >= TYPE_ALIGN (vectype
)));
823 /* Modulo alignment. */
824 misalign
= size_binop (FLOOR_MOD_EXPR
, misalign
, alignment
);
826 if (!host_integerp (misalign
, 1))
828 /* Negative or overflowed misalignment value. */
829 if (vect_print_dump_info (REPORT_DETAILS
))
830 fprintf (vect_dump
, "unexpected misalign value");
834 SET_DR_MISALIGNMENT (dr
, TREE_INT_CST_LOW (misalign
));
836 if (vect_print_dump_info (REPORT_DETAILS
))
838 fprintf (vect_dump
, "misalign = %d bytes of ref ", DR_MISALIGNMENT (dr
));
839 print_generic_expr (vect_dump
, ref
, TDF_SLIM
);
846 /* Function vect_compute_data_refs_alignment
848 Compute the misalignment of data references in the loop.
849 Return FALSE if a data reference is found that cannot be vectorized. */
852 vect_compute_data_refs_alignment (loop_vec_info loop_vinfo
,
853 bb_vec_info bb_vinfo
)
855 VEC (data_reference_p
, heap
) *datarefs
;
856 struct data_reference
*dr
;
860 datarefs
= LOOP_VINFO_DATAREFS (loop_vinfo
);
862 datarefs
= BB_VINFO_DATAREFS (bb_vinfo
);
864 for (i
= 0; VEC_iterate (data_reference_p
, datarefs
, i
, dr
); i
++)
865 if (STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dr
)))
866 && !vect_compute_data_ref_alignment (dr
))
870 /* Mark unsupported statement as unvectorizable. */
871 STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dr
))) = false;
882 /* Function vect_update_misalignment_for_peel
884 DR - the data reference whose misalignment is to be adjusted.
885 DR_PEEL - the data reference whose misalignment is being made
886 zero in the vector loop by the peel.
887 NPEEL - the number of iterations in the peel loop if the misalignment
888 of DR_PEEL is known at compile time. */
891 vect_update_misalignment_for_peel (struct data_reference
*dr
,
892 struct data_reference
*dr_peel
, int npeel
)
895 VEC(dr_p
,heap
) *same_align_drs
;
896 struct data_reference
*current_dr
;
897 int dr_size
= GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (DR_REF (dr
))));
898 int dr_peel_size
= GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (DR_REF (dr_peel
))));
899 stmt_vec_info stmt_info
= vinfo_for_stmt (DR_STMT (dr
));
900 stmt_vec_info peel_stmt_info
= vinfo_for_stmt (DR_STMT (dr_peel
));
902 /* For interleaved data accesses the step in the loop must be multiplied by
903 the size of the interleaving group. */
904 if (STMT_VINFO_STRIDED_ACCESS (stmt_info
))
905 dr_size
*= DR_GROUP_SIZE (vinfo_for_stmt (DR_GROUP_FIRST_DR (stmt_info
)));
906 if (STMT_VINFO_STRIDED_ACCESS (peel_stmt_info
))
907 dr_peel_size
*= DR_GROUP_SIZE (peel_stmt_info
);
909 /* It can be assumed that the data refs with the same alignment as dr_peel
910 are aligned in the vector loop. */
912 = STMT_VINFO_SAME_ALIGN_REFS (vinfo_for_stmt (DR_STMT (dr_peel
)));
913 for (i
= 0; VEC_iterate (dr_p
, same_align_drs
, i
, current_dr
); i
++)
915 if (current_dr
!= dr
)
917 gcc_assert (DR_MISALIGNMENT (dr
) / dr_size
==
918 DR_MISALIGNMENT (dr_peel
) / dr_peel_size
);
919 SET_DR_MISALIGNMENT (dr
, 0);
923 if (known_alignment_for_access_p (dr
)
924 && known_alignment_for_access_p (dr_peel
))
926 int misal
= DR_MISALIGNMENT (dr
);
927 tree vectype
= STMT_VINFO_VECTYPE (stmt_info
);
928 misal
+= npeel
* dr_size
;
929 misal
%= GET_MODE_SIZE (TYPE_MODE (vectype
));
930 SET_DR_MISALIGNMENT (dr
, misal
);
934 if (vect_print_dump_info (REPORT_DETAILS
))
935 fprintf (vect_dump
, "Setting misalignment to -1.");
936 SET_DR_MISALIGNMENT (dr
, -1);
940 /* Function vect_verify_datarefs_alignment
942 Return TRUE if all data references in the loop can be
943 handled with respect to alignment. */
946 vect_verify_datarefs_alignment (loop_vec_info loop_vinfo
, bb_vec_info bb_vinfo
)
948 VEC (data_reference_p
, heap
) *datarefs
;
949 struct data_reference
*dr
;
950 enum dr_alignment_support supportable_dr_alignment
;
954 datarefs
= LOOP_VINFO_DATAREFS (loop_vinfo
);
956 datarefs
= BB_VINFO_DATAREFS (bb_vinfo
);
958 for (i
= 0; VEC_iterate (data_reference_p
, datarefs
, i
, dr
); i
++)
960 gimple stmt
= DR_STMT (dr
);
961 stmt_vec_info stmt_info
= vinfo_for_stmt (stmt
);
963 /* For interleaving, only the alignment of the first access matters.
964 Skip statements marked as not vectorizable. */
965 if ((STMT_VINFO_STRIDED_ACCESS (stmt_info
)
966 && DR_GROUP_FIRST_DR (stmt_info
) != stmt
)
967 || !STMT_VINFO_VECTORIZABLE (stmt_info
))
970 supportable_dr_alignment
= vect_supportable_dr_alignment (dr
);
971 if (!supportable_dr_alignment
)
973 if (vect_print_dump_info (REPORT_UNVECTORIZED_LOCATIONS
))
977 "not vectorized: unsupported unaligned load.");
980 "not vectorized: unsupported unaligned store.");
982 print_generic_expr (vect_dump
, DR_REF (dr
), TDF_SLIM
);
986 if (supportable_dr_alignment
!= dr_aligned
987 && vect_print_dump_info (REPORT_ALIGNMENT
))
988 fprintf (vect_dump
, "Vectorizing an unaligned access.");
994 /* Function vector_alignment_reachable_p
996 Return true if vector alignment for DR is reachable by peeling
997 a few loop iterations. Return false otherwise. */
1000 vector_alignment_reachable_p (struct data_reference
*dr
)
1002 gimple stmt
= DR_STMT (dr
);
1003 stmt_vec_info stmt_info
= vinfo_for_stmt (stmt
);
1004 tree vectype
= STMT_VINFO_VECTYPE (stmt_info
);
1006 if (STMT_VINFO_STRIDED_ACCESS (stmt_info
))
1008 /* For interleaved access we peel only if number of iterations in
1009 the prolog loop ({VF - misalignment}), is a multiple of the
1010 number of the interleaved accesses. */
1011 int elem_size
, mis_in_elements
;
1012 int nelements
= TYPE_VECTOR_SUBPARTS (vectype
);
1014 /* FORNOW: handle only known alignment. */
1015 if (!known_alignment_for_access_p (dr
))
1018 elem_size
= GET_MODE_SIZE (TYPE_MODE (vectype
)) / nelements
;
1019 mis_in_elements
= DR_MISALIGNMENT (dr
) / elem_size
;
1021 if ((nelements
- mis_in_elements
) % DR_GROUP_SIZE (stmt_info
))
1025 /* If misalignment is known at the compile time then allow peeling
1026 only if natural alignment is reachable through peeling. */
1027 if (known_alignment_for_access_p (dr
) && !aligned_access_p (dr
))
1029 HOST_WIDE_INT elmsize
=
1030 int_cst_value (TYPE_SIZE_UNIT (TREE_TYPE (vectype
)));
1031 if (vect_print_dump_info (REPORT_DETAILS
))
1033 fprintf (vect_dump
, "data size =" HOST_WIDE_INT_PRINT_DEC
, elmsize
);
1034 fprintf (vect_dump
, ". misalignment = %d. ", DR_MISALIGNMENT (dr
));
1036 if (DR_MISALIGNMENT (dr
) % elmsize
)
1038 if (vect_print_dump_info (REPORT_DETAILS
))
1039 fprintf (vect_dump
, "data size does not divide the misalignment.\n");
1044 if (!known_alignment_for_access_p (dr
))
1046 tree type
= (TREE_TYPE (DR_REF (dr
)));
1047 tree ba
= DR_BASE_OBJECT (dr
);
1048 bool is_packed
= false;
1051 is_packed
= contains_packed_reference (ba
);
1053 if (vect_print_dump_info (REPORT_DETAILS
))
1054 fprintf (vect_dump
, "Unknown misalignment, is_packed = %d",is_packed
);
1055 if (targetm
.vectorize
.vector_alignment_reachable (type
, is_packed
))
1064 /* Function vect_enhance_data_refs_alignment
1066 This pass will use loop versioning and loop peeling in order to enhance
1067 the alignment of data references in the loop.
1069 FOR NOW: we assume that whatever versioning/peeling takes place, only the
1070 original loop is to be vectorized; Any other loops that are created by
1071 the transformations performed in this pass - are not supposed to be
1072 vectorized. This restriction will be relaxed.
1074 This pass will require a cost model to guide it whether to apply peeling
1075 or versioning or a combination of the two. For example, the scheme that
1076 intel uses when given a loop with several memory accesses, is as follows:
1077 choose one memory access ('p') which alignment you want to force by doing
1078 peeling. Then, either (1) generate a loop in which 'p' is aligned and all
1079 other accesses are not necessarily aligned, or (2) use loop versioning to
1080 generate one loop in which all accesses are aligned, and another loop in
1081 which only 'p' is necessarily aligned.
1083 ("Automatic Intra-Register Vectorization for the Intel Architecture",
1084 Aart J.C. Bik, Milind Girkar, Paul M. Grey and Ximmin Tian, International
1085 Journal of Parallel Programming, Vol. 30, No. 2, April 2002.)
1087 Devising a cost model is the most critical aspect of this work. It will
1088 guide us on which access to peel for, whether to use loop versioning, how
1089 many versions to create, etc. The cost model will probably consist of
1090 generic considerations as well as target specific considerations (on
1091 powerpc for example, misaligned stores are more painful than misaligned
1094 Here are the general steps involved in alignment enhancements:
1096 -- original loop, before alignment analysis:
1097 for (i=0; i<N; i++){
1098 x = q[i]; # DR_MISALIGNMENT(q) = unknown
1099 p[i] = y; # DR_MISALIGNMENT(p) = unknown
1102 -- After vect_compute_data_refs_alignment:
1103 for (i=0; i<N; i++){
1104 x = q[i]; # DR_MISALIGNMENT(q) = 3
1105 p[i] = y; # DR_MISALIGNMENT(p) = unknown
1108 -- Possibility 1: we do loop versioning:
1110 for (i=0; i<N; i++){ # loop 1A
1111 x = q[i]; # DR_MISALIGNMENT(q) = 3
1112 p[i] = y; # DR_MISALIGNMENT(p) = 0
1116 for (i=0; i<N; i++){ # loop 1B
1117 x = q[i]; # DR_MISALIGNMENT(q) = 3
1118 p[i] = y; # DR_MISALIGNMENT(p) = unaligned
1122 -- Possibility 2: we do loop peeling:
1123 for (i = 0; i < 3; i++){ # (scalar loop, not to be vectorized).
1127 for (i = 3; i < N; i++){ # loop 2A
1128 x = q[i]; # DR_MISALIGNMENT(q) = 0
1129 p[i] = y; # DR_MISALIGNMENT(p) = unknown
1132 -- Possibility 3: combination of loop peeling and versioning:
1133 for (i = 0; i < 3; i++){ # (scalar loop, not to be vectorized).
1138 for (i = 3; i<N; i++){ # loop 3A
1139 x = q[i]; # DR_MISALIGNMENT(q) = 0
1140 p[i] = y; # DR_MISALIGNMENT(p) = 0
1144 for (i = 3; i<N; i++){ # loop 3B
1145 x = q[i]; # DR_MISALIGNMENT(q) = 0
1146 p[i] = y; # DR_MISALIGNMENT(p) = unaligned
1150 These loops are later passed to loop_transform to be vectorized. The
1151 vectorizer will use the alignment information to guide the transformation
1152 (whether to generate regular loads/stores, or with special handling for
1156 vect_enhance_data_refs_alignment (loop_vec_info loop_vinfo
)
1158 VEC (data_reference_p
, heap
) *datarefs
= LOOP_VINFO_DATAREFS (loop_vinfo
);
1159 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
1160 enum dr_alignment_support supportable_dr_alignment
;
1161 struct data_reference
*dr0
= NULL
;
1162 struct data_reference
*dr
;
1164 bool do_peeling
= false;
1165 bool do_versioning
= false;
1168 stmt_vec_info stmt_info
;
1169 int vect_versioning_for_alias_required
;
1171 if (vect_print_dump_info (REPORT_DETAILS
))
1172 fprintf (vect_dump
, "=== vect_enhance_data_refs_alignment ===");
1174 /* While cost model enhancements are expected in the future, the high level
1175 view of the code at this time is as follows:
1177 A) If there is a misaligned access then see if peeling to align
1178 this access can make all data references satisfy
1179 vect_supportable_dr_alignment. If so, update data structures
1180 as needed and return true.
1182 B) If peeling wasn't possible and there is a data reference with an
1183 unknown misalignment that does not satisfy vect_supportable_dr_alignment
1184 then see if loop versioning checks can be used to make all data
1185 references satisfy vect_supportable_dr_alignment. If so, update
1186 data structures as needed and return true.
1188 C) If neither peeling nor versioning were successful then return false if
1189 any data reference does not satisfy vect_supportable_dr_alignment.
1191 D) Return true (all data references satisfy vect_supportable_dr_alignment).
1193 Note, Possibility 3 above (which is peeling and versioning together) is not
1194 being done at this time. */
1196 /* (1) Peeling to force alignment. */
1198 /* (1.1) Decide whether to perform peeling, and how many iterations to peel:
1200 + How many accesses will become aligned due to the peeling
1201 - How many accesses will become unaligned due to the peeling,
1202 and the cost of misaligned accesses.
1203 - The cost of peeling (the extra runtime checks, the increase
1206 The scheme we use FORNOW: peel to force the alignment of the first
1207 unsupported misaligned access in the loop.
1209 TODO: Use a cost model. */
1211 for (i
= 0; VEC_iterate (data_reference_p
, datarefs
, i
, dr
); i
++)
1213 stmt
= DR_STMT (dr
);
1214 stmt_info
= vinfo_for_stmt (stmt
);
1216 /* For interleaving, only the alignment of the first access
1218 if (STMT_VINFO_STRIDED_ACCESS (stmt_info
)
1219 && DR_GROUP_FIRST_DR (stmt_info
) != stmt
)
1222 if (!DR_IS_READ (dr
) && !aligned_access_p (dr
))
1224 do_peeling
= vector_alignment_reachable_p (dr
);
1227 if (!do_peeling
&& vect_print_dump_info (REPORT_DETAILS
))
1228 fprintf (vect_dump
, "vector alignment may not be reachable");
1233 vect_versioning_for_alias_required
1234 = LOOP_REQUIRES_VERSIONING_FOR_ALIAS (loop_vinfo
);
1236 /* Temporarily, if versioning for alias is required, we disable peeling
1237 until we support peeling and versioning. Often peeling for alignment
1238 will require peeling for loop-bound, which in turn requires that we
1239 know how to adjust the loop ivs after the loop. */
1240 if (vect_versioning_for_alias_required
1241 || !vect_can_advance_ivs_p (loop_vinfo
)
1242 || !slpeel_can_duplicate_loop_p (loop
, single_exit (loop
)))
1249 gimple stmt
= DR_STMT (dr0
);
1250 stmt_vec_info stmt_info
= vinfo_for_stmt (stmt
);
1251 tree vectype
= STMT_VINFO_VECTYPE (stmt_info
);
1252 int nelements
= TYPE_VECTOR_SUBPARTS (vectype
);
1254 if (known_alignment_for_access_p (dr0
))
1256 /* Since it's known at compile time, compute the number of iterations
1257 in the peeled loop (the peeling factor) for use in updating
1258 DR_MISALIGNMENT values. The peeling factor is the vectorization
1259 factor minus the misalignment as an element count. */
1260 mis
= DR_MISALIGNMENT (dr0
);
1261 mis
/= GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (DR_REF (dr0
))));
1262 npeel
= nelements
- mis
;
1264 /* For interleaved data access every iteration accesses all the
1265 members of the group, therefore we divide the number of iterations
1266 by the group size. */
1267 stmt_info
= vinfo_for_stmt (DR_STMT (dr0
));
1268 if (STMT_VINFO_STRIDED_ACCESS (stmt_info
))
1269 npeel
/= DR_GROUP_SIZE (stmt_info
);
1271 if (vect_print_dump_info (REPORT_DETAILS
))
1272 fprintf (vect_dump
, "Try peeling by %d", npeel
);
1275 /* Ensure that all data refs can be vectorized after the peel. */
1276 for (i
= 0; VEC_iterate (data_reference_p
, datarefs
, i
, dr
); i
++)
1278 int save_misalignment
;
1283 stmt
= DR_STMT (dr
);
1284 stmt_info
= vinfo_for_stmt (stmt
);
1285 /* For interleaving, only the alignment of the first access
1287 if (STMT_VINFO_STRIDED_ACCESS (stmt_info
)
1288 && DR_GROUP_FIRST_DR (stmt_info
) != stmt
)
1291 save_misalignment
= DR_MISALIGNMENT (dr
);
1292 vect_update_misalignment_for_peel (dr
, dr0
, npeel
);
1293 supportable_dr_alignment
= vect_supportable_dr_alignment (dr
);
1294 SET_DR_MISALIGNMENT (dr
, save_misalignment
);
1296 if (!supportable_dr_alignment
)
1305 /* (1.2) Update the DR_MISALIGNMENT of each data reference DR_i.
1306 If the misalignment of DR_i is identical to that of dr0 then set
1307 DR_MISALIGNMENT (DR_i) to zero. If the misalignment of DR_i and
1308 dr0 are known at compile time then increment DR_MISALIGNMENT (DR_i)
1309 by the peeling factor times the element size of DR_i (MOD the
1310 vectorization factor times the size). Otherwise, the
1311 misalignment of DR_i must be set to unknown. */
1312 for (i
= 0; VEC_iterate (data_reference_p
, datarefs
, i
, dr
); i
++)
1314 vect_update_misalignment_for_peel (dr
, dr0
, npeel
);
1316 LOOP_VINFO_UNALIGNED_DR (loop_vinfo
) = dr0
;
1317 LOOP_PEELING_FOR_ALIGNMENT (loop_vinfo
) = DR_MISALIGNMENT (dr0
);
1318 SET_DR_MISALIGNMENT (dr0
, 0);
1319 if (vect_print_dump_info (REPORT_ALIGNMENT
))
1320 fprintf (vect_dump
, "Alignment of access forced using peeling.");
1322 if (vect_print_dump_info (REPORT_DETAILS
))
1323 fprintf (vect_dump
, "Peeling for alignment will be applied.");
1325 stat
= vect_verify_datarefs_alignment (loop_vinfo
, NULL
);
1332 /* (2) Versioning to force alignment. */
1334 /* Try versioning if:
1335 1) flag_tree_vect_loop_version is TRUE
1336 2) optimize loop for speed
1337 3) there is at least one unsupported misaligned data ref with an unknown
1339 4) all misaligned data refs with a known misalignment are supported, and
1340 5) the number of runtime alignment checks is within reason. */
1343 flag_tree_vect_loop_version
1344 && optimize_loop_nest_for_speed_p (loop
)
1345 && (!loop
->inner
); /* FORNOW */
1349 for (i
= 0; VEC_iterate (data_reference_p
, datarefs
, i
, dr
); i
++)
1351 stmt
= DR_STMT (dr
);
1352 stmt_info
= vinfo_for_stmt (stmt
);
1354 /* For interleaving, only the alignment of the first access
1356 if (aligned_access_p (dr
)
1357 || (STMT_VINFO_STRIDED_ACCESS (stmt_info
)
1358 && DR_GROUP_FIRST_DR (stmt_info
) != stmt
))
1361 supportable_dr_alignment
= vect_supportable_dr_alignment (dr
);
1363 if (!supportable_dr_alignment
)
1369 if (known_alignment_for_access_p (dr
)
1370 || VEC_length (gimple
,
1371 LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo
))
1372 >= (unsigned) PARAM_VALUE (PARAM_VECT_MAX_VERSION_FOR_ALIGNMENT_CHECKS
))
1374 do_versioning
= false;
1378 stmt
= DR_STMT (dr
);
1379 vectype
= STMT_VINFO_VECTYPE (vinfo_for_stmt (stmt
));
1380 gcc_assert (vectype
);
1382 /* The rightmost bits of an aligned address must be zeros.
1383 Construct the mask needed for this test. For example,
1384 GET_MODE_SIZE for the vector mode V4SI is 16 bytes so the
1385 mask must be 15 = 0xf. */
1386 mask
= GET_MODE_SIZE (TYPE_MODE (vectype
)) - 1;
1388 /* FORNOW: use the same mask to test all potentially unaligned
1389 references in the loop. The vectorizer currently supports
1390 a single vector size, see the reference to
1391 GET_MODE_NUNITS (TYPE_MODE (vectype)) where the
1392 vectorization factor is computed. */
1393 gcc_assert (!LOOP_VINFO_PTR_MASK (loop_vinfo
)
1394 || LOOP_VINFO_PTR_MASK (loop_vinfo
) == mask
);
1395 LOOP_VINFO_PTR_MASK (loop_vinfo
) = mask
;
1396 VEC_safe_push (gimple
, heap
,
1397 LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo
),
1402 /* Versioning requires at least one misaligned data reference. */
1403 if (!LOOP_REQUIRES_VERSIONING_FOR_ALIGNMENT (loop_vinfo
))
1404 do_versioning
= false;
1405 else if (!do_versioning
)
1406 VEC_truncate (gimple
, LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo
), 0);
1411 VEC(gimple
,heap
) *may_misalign_stmts
1412 = LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo
);
1415 /* It can now be assumed that the data references in the statements
1416 in LOOP_VINFO_MAY_MISALIGN_STMTS will be aligned in the version
1417 of the loop being vectorized. */
1418 for (i
= 0; VEC_iterate (gimple
, may_misalign_stmts
, i
, stmt
); i
++)
1420 stmt_vec_info stmt_info
= vinfo_for_stmt (stmt
);
1421 dr
= STMT_VINFO_DATA_REF (stmt_info
);
1422 SET_DR_MISALIGNMENT (dr
, 0);
1423 if (vect_print_dump_info (REPORT_ALIGNMENT
))
1424 fprintf (vect_dump
, "Alignment of access forced using versioning.");
1427 if (vect_print_dump_info (REPORT_DETAILS
))
1428 fprintf (vect_dump
, "Versioning for alignment will be applied.");
1430 /* Peeling and versioning can't be done together at this time. */
1431 gcc_assert (! (do_peeling
&& do_versioning
));
1433 stat
= vect_verify_datarefs_alignment (loop_vinfo
, NULL
);
1438 /* This point is reached if neither peeling nor versioning is being done. */
1439 gcc_assert (! (do_peeling
|| do_versioning
));
1441 stat
= vect_verify_datarefs_alignment (loop_vinfo
, NULL
);
1446 /* Function vect_find_same_alignment_drs.
1448 Update group and alignment relations according to the chosen
1449 vectorization factor. */
1452 vect_find_same_alignment_drs (struct data_dependence_relation
*ddr
,
1453 loop_vec_info loop_vinfo
)
1456 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
1457 int vectorization_factor
= LOOP_VINFO_VECT_FACTOR (loop_vinfo
);
1458 struct data_reference
*dra
= DDR_A (ddr
);
1459 struct data_reference
*drb
= DDR_B (ddr
);
1460 stmt_vec_info stmtinfo_a
= vinfo_for_stmt (DR_STMT (dra
));
1461 stmt_vec_info stmtinfo_b
= vinfo_for_stmt (DR_STMT (drb
));
1462 int dra_size
= GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (DR_REF (dra
))));
1463 int drb_size
= GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (DR_REF (drb
))));
1464 lambda_vector dist_v
;
1465 unsigned int loop_depth
;
1467 if (DDR_ARE_DEPENDENT (ddr
) == chrec_known
)
1470 if ((DR_IS_READ (dra
) && DR_IS_READ (drb
)) || dra
== drb
)
1473 if (DDR_ARE_DEPENDENT (ddr
) == chrec_dont_know
)
1476 /* Loop-based vectorization and known data dependence. */
1477 if (DDR_NUM_DIST_VECTS (ddr
) == 0)
1480 loop_depth
= index_in_loop_nest (loop
->num
, DDR_LOOP_NEST (ddr
));
1481 for (i
= 0; VEC_iterate (lambda_vector
, DDR_DIST_VECTS (ddr
), i
, dist_v
); i
++)
1483 int dist
= dist_v
[loop_depth
];
1485 if (vect_print_dump_info (REPORT_DR_DETAILS
))
1486 fprintf (vect_dump
, "dependence distance = %d.", dist
);
1488 /* Same loop iteration. */
1490 || (dist
% vectorization_factor
== 0 && dra_size
== drb_size
))
1492 /* Two references with distance zero have the same alignment. */
1493 VEC_safe_push (dr_p
, heap
, STMT_VINFO_SAME_ALIGN_REFS (stmtinfo_a
), drb
);
1494 VEC_safe_push (dr_p
, heap
, STMT_VINFO_SAME_ALIGN_REFS (stmtinfo_b
), dra
);
1495 if (vect_print_dump_info (REPORT_ALIGNMENT
))
1496 fprintf (vect_dump
, "accesses have the same alignment.");
1497 if (vect_print_dump_info (REPORT_DR_DETAILS
))
1499 fprintf (vect_dump
, "dependence distance modulo vf == 0 between ");
1500 print_generic_expr (vect_dump
, DR_REF (dra
), TDF_SLIM
);
1501 fprintf (vect_dump
, " and ");
1502 print_generic_expr (vect_dump
, DR_REF (drb
), TDF_SLIM
);
1509 /* Function vect_analyze_data_refs_alignment
1511 Analyze the alignment of the data-references in the loop.
1512 Return FALSE if a data reference is found that cannot be vectorized. */
1515 vect_analyze_data_refs_alignment (loop_vec_info loop_vinfo
,
1516 bb_vec_info bb_vinfo
)
1518 if (vect_print_dump_info (REPORT_DETAILS
))
1519 fprintf (vect_dump
, "=== vect_analyze_data_refs_alignment ===");
1521 /* Mark groups of data references with same alignment using
1522 data dependence information. */
1525 VEC (ddr_p
, heap
) *ddrs
= LOOP_VINFO_DDRS (loop_vinfo
);
1526 struct data_dependence_relation
*ddr
;
1529 for (i
= 0; VEC_iterate (ddr_p
, ddrs
, i
, ddr
); i
++)
1530 vect_find_same_alignment_drs (ddr
, loop_vinfo
);
1533 if (!vect_compute_data_refs_alignment (loop_vinfo
, bb_vinfo
))
1535 if (vect_print_dump_info (REPORT_UNVECTORIZED_LOCATIONS
))
1537 "not vectorized: can't calculate alignment for data ref.");
1545 /* Analyze groups of strided accesses: check that DR belongs to a group of
1546 strided accesses of legal size, step, etc. Detect gaps, single element
1547 interleaving, and other special cases. Set strided access info.
1548 Collect groups of strided stores for further use in SLP analysis. */
1551 vect_analyze_group_access (struct data_reference
*dr
)
1553 tree step
= DR_STEP (dr
);
1554 tree scalar_type
= TREE_TYPE (DR_REF (dr
));
1555 HOST_WIDE_INT type_size
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (scalar_type
));
1556 gimple stmt
= DR_STMT (dr
);
1557 stmt_vec_info stmt_info
= vinfo_for_stmt (stmt
);
1558 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_info
);
1559 bb_vec_info bb_vinfo
= STMT_VINFO_BB_VINFO (stmt_info
);
1560 HOST_WIDE_INT dr_step
= TREE_INT_CST_LOW (step
);
1561 HOST_WIDE_INT stride
;
1562 bool slp_impossible
= false;
1564 /* For interleaving, STRIDE is STEP counted in elements, i.e., the size of the
1565 interleaving group (including gaps). */
1566 stride
= dr_step
/ type_size
;
1568 /* Not consecutive access is possible only if it is a part of interleaving. */
1569 if (!DR_GROUP_FIRST_DR (vinfo_for_stmt (stmt
)))
1571 /* Check if it this DR is a part of interleaving, and is a single
1572 element of the group that is accessed in the loop. */
1574 /* Gaps are supported only for loads. STEP must be a multiple of the type
1575 size. The size of the group must be a power of 2. */
1577 && (dr_step
% type_size
) == 0
1579 && exact_log2 (stride
) != -1)
1581 DR_GROUP_FIRST_DR (vinfo_for_stmt (stmt
)) = stmt
;
1582 DR_GROUP_SIZE (vinfo_for_stmt (stmt
)) = stride
;
1583 if (vect_print_dump_info (REPORT_DR_DETAILS
))
1585 fprintf (vect_dump
, "Detected single element interleaving ");
1586 print_generic_expr (vect_dump
, DR_REF (dr
), TDF_SLIM
);
1587 fprintf (vect_dump
, " step ");
1588 print_generic_expr (vect_dump
, step
, TDF_SLIM
);
1593 if (vect_print_dump_info (REPORT_DETAILS
))
1595 fprintf (vect_dump
, "not consecutive access ");
1596 print_gimple_stmt (vect_dump
, stmt
, 0, TDF_SLIM
);
1601 /* Mark the statement as unvectorizable. */
1602 STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dr
))) = false;
1609 if (DR_GROUP_FIRST_DR (vinfo_for_stmt (stmt
)) == stmt
)
1611 /* First stmt in the interleaving chain. Check the chain. */
1612 gimple next
= DR_GROUP_NEXT_DR (vinfo_for_stmt (stmt
));
1613 struct data_reference
*data_ref
= dr
;
1614 unsigned int count
= 1;
1616 tree prev_init
= DR_INIT (data_ref
);
1618 HOST_WIDE_INT diff
, count_in_bytes
, gaps
= 0;
1622 /* Skip same data-refs. In case that two or more stmts share data-ref
1623 (supported only for loads), we vectorize only the first stmt, and
1624 the rest get their vectorized loads from the first one. */
1625 if (!tree_int_cst_compare (DR_INIT (data_ref
),
1626 DR_INIT (STMT_VINFO_DATA_REF (
1627 vinfo_for_stmt (next
)))))
1629 if (!DR_IS_READ (data_ref
))
1631 if (vect_print_dump_info (REPORT_DETAILS
))
1632 fprintf (vect_dump
, "Two store stmts share the same dr.");
1636 /* Check that there is no load-store dependencies for this loads
1637 to prevent a case of load-store-load to the same location. */
1638 if (DR_GROUP_READ_WRITE_DEPENDENCE (vinfo_for_stmt (next
))
1639 || DR_GROUP_READ_WRITE_DEPENDENCE (vinfo_for_stmt (prev
)))
1641 if (vect_print_dump_info (REPORT_DETAILS
))
1643 "READ_WRITE dependence in interleaving.");
1647 /* For load use the same data-ref load. */
1648 DR_GROUP_SAME_DR_STMT (vinfo_for_stmt (next
)) = prev
;
1651 next
= DR_GROUP_NEXT_DR (vinfo_for_stmt (next
));
1656 /* Check that all the accesses have the same STEP. */
1657 next_step
= DR_STEP (STMT_VINFO_DATA_REF (vinfo_for_stmt (next
)));
1658 if (tree_int_cst_compare (step
, next_step
))
1660 if (vect_print_dump_info (REPORT_DETAILS
))
1661 fprintf (vect_dump
, "not consecutive access in interleaving");
1665 data_ref
= STMT_VINFO_DATA_REF (vinfo_for_stmt (next
));
1666 /* Check that the distance between two accesses is equal to the type
1667 size. Otherwise, we have gaps. */
1668 diff
= (TREE_INT_CST_LOW (DR_INIT (data_ref
))
1669 - TREE_INT_CST_LOW (prev_init
)) / type_size
;
1672 /* FORNOW: SLP of accesses with gaps is not supported. */
1673 slp_impossible
= true;
1674 if (!DR_IS_READ (data_ref
))
1676 if (vect_print_dump_info (REPORT_DETAILS
))
1677 fprintf (vect_dump
, "interleaved store with gaps");
1684 /* Store the gap from the previous member of the group. If there is no
1685 gap in the access, DR_GROUP_GAP is always 1. */
1686 DR_GROUP_GAP (vinfo_for_stmt (next
)) = diff
;
1688 prev_init
= DR_INIT (data_ref
);
1689 next
= DR_GROUP_NEXT_DR (vinfo_for_stmt (next
));
1690 /* Count the number of data-refs in the chain. */
1694 /* COUNT is the number of accesses found, we multiply it by the size of
1695 the type to get COUNT_IN_BYTES. */
1696 count_in_bytes
= type_size
* count
;
1698 /* Check that the size of the interleaving (including gaps) is not
1699 greater than STEP. */
1700 if (dr_step
&& dr_step
< count_in_bytes
+ gaps
* type_size
)
1702 if (vect_print_dump_info (REPORT_DETAILS
))
1704 fprintf (vect_dump
, "interleaving size is greater than step for ");
1705 print_generic_expr (vect_dump
, DR_REF (dr
), TDF_SLIM
);
1710 /* Check that the size of the interleaving is equal to STEP for stores,
1711 i.e., that there are no gaps. */
1712 if (dr_step
&& dr_step
!= count_in_bytes
)
1714 if (DR_IS_READ (dr
))
1716 slp_impossible
= true;
1717 /* There is a gap after the last load in the group. This gap is a
1718 difference between the stride and the number of elements. When
1719 there is no gap, this difference should be 0. */
1720 DR_GROUP_GAP (vinfo_for_stmt (stmt
)) = stride
- count
;
1724 if (vect_print_dump_info (REPORT_DETAILS
))
1725 fprintf (vect_dump
, "interleaved store with gaps");
1730 /* Check that STEP is a multiple of type size. */
1731 if (dr_step
&& (dr_step
% type_size
) != 0)
1733 if (vect_print_dump_info (REPORT_DETAILS
))
1735 fprintf (vect_dump
, "step is not a multiple of type size: step ");
1736 print_generic_expr (vect_dump
, step
, TDF_SLIM
);
1737 fprintf (vect_dump
, " size ");
1738 print_generic_expr (vect_dump
, TYPE_SIZE_UNIT (scalar_type
),
1744 /* FORNOW: we handle only interleaving that is a power of 2.
1745 We don't fail here if it may be still possible to vectorize the
1746 group using SLP. If not, the size of the group will be checked in
1747 vect_analyze_operations, and the vectorization will fail. */
1748 if (exact_log2 (stride
) == -1)
1750 if (vect_print_dump_info (REPORT_DETAILS
))
1751 fprintf (vect_dump
, "interleaving is not a power of 2");
1760 DR_GROUP_SIZE (vinfo_for_stmt (stmt
)) = stride
;
1761 if (vect_print_dump_info (REPORT_DETAILS
))
1762 fprintf (vect_dump
, "Detected interleaving of size %d", (int)stride
);
1764 /* SLP: create an SLP data structure for every interleaving group of
1765 stores for further analysis in vect_analyse_slp. */
1766 if (!DR_IS_READ (dr
) && !slp_impossible
)
1769 VEC_safe_push (gimple
, heap
, LOOP_VINFO_STRIDED_STORES (loop_vinfo
),
1772 VEC_safe_push (gimple
, heap
, BB_VINFO_STRIDED_STORES (bb_vinfo
),
1781 /* Analyze the access pattern of the data-reference DR.
1782 In case of non-consecutive accesses call vect_analyze_group_access() to
1783 analyze groups of strided accesses. */
1786 vect_analyze_data_ref_access (struct data_reference
*dr
)
1788 tree step
= DR_STEP (dr
);
1789 tree scalar_type
= TREE_TYPE (DR_REF (dr
));
1790 gimple stmt
= DR_STMT (dr
);
1791 stmt_vec_info stmt_info
= vinfo_for_stmt (stmt
);
1792 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_info
);
1793 struct loop
*loop
= NULL
;
1794 HOST_WIDE_INT dr_step
= TREE_INT_CST_LOW (step
);
1797 loop
= LOOP_VINFO_LOOP (loop_vinfo
);
1799 if (loop_vinfo
&& !step
)
1801 if (vect_print_dump_info (REPORT_DETAILS
))
1802 fprintf (vect_dump
, "bad data-ref access in loop");
1806 /* Don't allow invariant accesses in loops. */
1807 if (loop_vinfo
&& dr_step
== 0)
1810 if (loop
&& nested_in_vect_loop_p (loop
, stmt
))
1812 /* Interleaved accesses are not yet supported within outer-loop
1813 vectorization for references in the inner-loop. */
1814 DR_GROUP_FIRST_DR (vinfo_for_stmt (stmt
)) = NULL
;
1816 /* For the rest of the analysis we use the outer-loop step. */
1817 step
= STMT_VINFO_DR_STEP (stmt_info
);
1818 dr_step
= TREE_INT_CST_LOW (step
);
1822 if (vect_print_dump_info (REPORT_ALIGNMENT
))
1823 fprintf (vect_dump
, "zero step in outer loop.");
1824 if (DR_IS_READ (dr
))
1832 if (!tree_int_cst_compare (step
, TYPE_SIZE_UNIT (scalar_type
)))
1834 /* Mark that it is not interleaving. */
1835 DR_GROUP_FIRST_DR (vinfo_for_stmt (stmt
)) = NULL
;
1839 if (loop
&& nested_in_vect_loop_p (loop
, stmt
))
1841 if (vect_print_dump_info (REPORT_ALIGNMENT
))
1842 fprintf (vect_dump
, "strided access in outer loop.");
1846 /* Not consecutive access - check if it's a part of interleaving group. */
1847 return vect_analyze_group_access (dr
);
1851 /* Function vect_analyze_data_ref_accesses.
1853 Analyze the access pattern of all the data references in the loop.
1855 FORNOW: the only access pattern that is considered vectorizable is a
1856 simple step 1 (consecutive) access.
1858 FORNOW: handle only arrays and pointer accesses. */
1861 vect_analyze_data_ref_accesses (loop_vec_info loop_vinfo
, bb_vec_info bb_vinfo
)
1864 VEC (data_reference_p
, heap
) *datarefs
;
1865 struct data_reference
*dr
;
1867 if (vect_print_dump_info (REPORT_DETAILS
))
1868 fprintf (vect_dump
, "=== vect_analyze_data_ref_accesses ===");
1871 datarefs
= LOOP_VINFO_DATAREFS (loop_vinfo
);
1873 datarefs
= BB_VINFO_DATAREFS (bb_vinfo
);
1875 for (i
= 0; VEC_iterate (data_reference_p
, datarefs
, i
, dr
); i
++)
1876 if (STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dr
)))
1877 && !vect_analyze_data_ref_access (dr
))
1879 if (vect_print_dump_info (REPORT_UNVECTORIZED_LOCATIONS
))
1880 fprintf (vect_dump
, "not vectorized: complicated access pattern.");
1884 /* Mark the statement as not vectorizable. */
1885 STMT_VINFO_VECTORIZABLE (vinfo_for_stmt (DR_STMT (dr
))) = false;
1895 /* Function vect_prune_runtime_alias_test_list.
1897 Prune a list of ddrs to be tested at run-time by versioning for alias.
1898 Return FALSE if resulting list of ddrs is longer then allowed by
1899 PARAM_VECT_MAX_VERSION_FOR_ALIAS_CHECKS, otherwise return TRUE. */
1902 vect_prune_runtime_alias_test_list (loop_vec_info loop_vinfo
)
1904 VEC (ddr_p
, heap
) * ddrs
=
1905 LOOP_VINFO_MAY_ALIAS_DDRS (loop_vinfo
);
1908 if (vect_print_dump_info (REPORT_DETAILS
))
1909 fprintf (vect_dump
, "=== vect_prune_runtime_alias_test_list ===");
1911 for (i
= 0; i
< VEC_length (ddr_p
, ddrs
); )
1916 ddr_i
= VEC_index (ddr_p
, ddrs
, i
);
1919 for (j
= 0; j
< i
; j
++)
1921 ddr_p ddr_j
= VEC_index (ddr_p
, ddrs
, j
);
1923 if (vect_vfa_range_equal (ddr_i
, ddr_j
))
1925 if (vect_print_dump_info (REPORT_DR_DETAILS
))
1927 fprintf (vect_dump
, "found equal ranges ");
1928 print_generic_expr (vect_dump
, DR_REF (DDR_A (ddr_i
)), TDF_SLIM
);
1929 fprintf (vect_dump
, ", ");
1930 print_generic_expr (vect_dump
, DR_REF (DDR_B (ddr_i
)), TDF_SLIM
);
1931 fprintf (vect_dump
, " and ");
1932 print_generic_expr (vect_dump
, DR_REF (DDR_A (ddr_j
)), TDF_SLIM
);
1933 fprintf (vect_dump
, ", ");
1934 print_generic_expr (vect_dump
, DR_REF (DDR_B (ddr_j
)), TDF_SLIM
);
1943 VEC_ordered_remove (ddr_p
, ddrs
, i
);
1949 if (VEC_length (ddr_p
, ddrs
) >
1950 (unsigned) PARAM_VALUE (PARAM_VECT_MAX_VERSION_FOR_ALIAS_CHECKS
))
1952 if (vect_print_dump_info (REPORT_DR_DETAILS
))
1955 "disable versioning for alias - max number of generated "
1956 "checks exceeded.");
1959 VEC_truncate (ddr_p
, LOOP_VINFO_MAY_ALIAS_DDRS (loop_vinfo
), 0);
1968 /* Function vect_analyze_data_refs.
1970 Find all the data references in the loop or basic block.
1972 The general structure of the analysis of data refs in the vectorizer is as
1974 1- vect_analyze_data_refs(loop/bb): call
1975 compute_data_dependences_for_loop/bb to find and analyze all data-refs
1976 in the loop/bb and their dependences.
1977 2- vect_analyze_dependences(): apply dependence testing using ddrs.
1978 3- vect_analyze_drs_alignment(): check that ref_stmt.alignment is ok.
1979 4- vect_analyze_drs_access(): check that ref_stmt.step is ok.
1984 vect_analyze_data_refs (loop_vec_info loop_vinfo
,
1985 bb_vec_info bb_vinfo
,
1988 struct loop
*loop
= NULL
;
1989 basic_block bb
= NULL
;
1991 VEC (data_reference_p
, heap
) *datarefs
;
1992 struct data_reference
*dr
;
1996 if (vect_print_dump_info (REPORT_DETAILS
))
1997 fprintf (vect_dump
, "=== vect_analyze_data_refs ===\n");
2001 loop
= LOOP_VINFO_LOOP (loop_vinfo
);
2002 res
= compute_data_dependences_for_loop
2003 (loop
, true, &LOOP_VINFO_DATAREFS (loop_vinfo
),
2004 &LOOP_VINFO_DDRS (loop_vinfo
));
2008 if (vect_print_dump_info (REPORT_UNVECTORIZED_LOCATIONS
))
2009 fprintf (vect_dump
, "not vectorized: loop contains function calls"
2010 " or data references that cannot be analyzed");
2014 datarefs
= LOOP_VINFO_DATAREFS (loop_vinfo
);
2018 bb
= BB_VINFO_BB (bb_vinfo
);
2019 res
= compute_data_dependences_for_bb (bb
, true,
2020 &BB_VINFO_DATAREFS (bb_vinfo
),
2021 &BB_VINFO_DDRS (bb_vinfo
));
2024 if (vect_print_dump_info (REPORT_UNVECTORIZED_LOCATIONS
))
2025 fprintf (vect_dump
, "not vectorized: basic block contains function"
2026 " calls or data references that cannot be analyzed");
2030 datarefs
= BB_VINFO_DATAREFS (bb_vinfo
);
2033 /* Go through the data-refs, check that the analysis succeeded. Update pointer
2034 from stmt_vec_info struct to DR and vectype. */
2036 for (i
= 0; VEC_iterate (data_reference_p
, datarefs
, i
, dr
); i
++)
2039 stmt_vec_info stmt_info
;
2040 tree base
, offset
, init
;
2043 if (!dr
|| !DR_REF (dr
))
2045 if (vect_print_dump_info (REPORT_UNVECTORIZED_LOCATIONS
))
2046 fprintf (vect_dump
, "not vectorized: unhandled data-ref ");
2050 stmt
= DR_STMT (dr
);
2051 stmt_info
= vinfo_for_stmt (stmt
);
2053 /* Check that analysis of the data-ref succeeded. */
2054 if (!DR_BASE_ADDRESS (dr
) || !DR_OFFSET (dr
) || !DR_INIT (dr
)
2057 if (vect_print_dump_info (REPORT_UNVECTORIZED_LOCATIONS
))
2059 fprintf (vect_dump
, "not vectorized: data ref analysis failed ");
2060 print_gimple_stmt (vect_dump
, stmt
, 0, TDF_SLIM
);
2065 /* Mark the statement as not vectorizable. */
2066 STMT_VINFO_VECTORIZABLE (stmt_info
) = false;
2073 if (TREE_CODE (DR_BASE_ADDRESS (dr
)) == INTEGER_CST
)
2075 if (vect_print_dump_info (REPORT_UNVECTORIZED_LOCATIONS
))
2076 fprintf (vect_dump
, "not vectorized: base addr of dr is a "
2080 /* Mark the statement as not vectorizable. */
2081 STMT_VINFO_VECTORIZABLE (stmt_info
) = false;
2088 base
= unshare_expr (DR_BASE_ADDRESS (dr
));
2089 offset
= unshare_expr (DR_OFFSET (dr
));
2090 init
= unshare_expr (DR_INIT (dr
));
2092 /* Update DR field in stmt_vec_info struct. */
2094 /* If the dataref is in an inner-loop of the loop that is considered for
2095 for vectorization, we also want to analyze the access relative to
2096 the outer-loop (DR contains information only relative to the
2097 inner-most enclosing loop). We do that by building a reference to the
2098 first location accessed by the inner-loop, and analyze it relative to
2100 if (loop
&& nested_in_vect_loop_p (loop
, stmt
))
2102 tree outer_step
, outer_base
, outer_init
;
2103 HOST_WIDE_INT pbitsize
, pbitpos
;
2105 enum machine_mode pmode
;
2106 int punsignedp
, pvolatilep
;
2107 affine_iv base_iv
, offset_iv
;
2110 /* Build a reference to the first location accessed by the
2111 inner-loop: *(BASE+INIT). (The first location is actually
2112 BASE+INIT+OFFSET, but we add OFFSET separately later). */
2113 tree inner_base
= build_fold_indirect_ref
2114 (fold_build2 (POINTER_PLUS_EXPR
,
2115 TREE_TYPE (base
), base
,
2116 fold_convert (sizetype
, init
)));
2118 if (vect_print_dump_info (REPORT_DETAILS
))
2120 fprintf (vect_dump
, "analyze in outer-loop: ");
2121 print_generic_expr (vect_dump
, inner_base
, TDF_SLIM
);
2124 outer_base
= get_inner_reference (inner_base
, &pbitsize
, &pbitpos
,
2125 &poffset
, &pmode
, &punsignedp
, &pvolatilep
, false);
2126 gcc_assert (outer_base
!= NULL_TREE
);
2128 if (pbitpos
% BITS_PER_UNIT
!= 0)
2130 if (vect_print_dump_info (REPORT_DETAILS
))
2131 fprintf (vect_dump
, "failed: bit offset alignment.\n");
2135 outer_base
= build_fold_addr_expr (outer_base
);
2136 if (!simple_iv (loop
, loop_containing_stmt (stmt
), outer_base
,
2139 if (vect_print_dump_info (REPORT_DETAILS
))
2140 fprintf (vect_dump
, "failed: evolution of base is not affine.\n");
2147 poffset
= fold_build2 (PLUS_EXPR
, TREE_TYPE (offset
), offset
,
2155 offset_iv
.base
= ssize_int (0);
2156 offset_iv
.step
= ssize_int (0);
2158 else if (!simple_iv (loop
, loop_containing_stmt (stmt
), poffset
,
2161 if (vect_print_dump_info (REPORT_DETAILS
))
2162 fprintf (vect_dump
, "evolution of offset is not affine.\n");
2166 outer_init
= ssize_int (pbitpos
/ BITS_PER_UNIT
);
2167 split_constant_offset (base_iv
.base
, &base_iv
.base
, &dinit
);
2168 outer_init
= size_binop (PLUS_EXPR
, outer_init
, dinit
);
2169 split_constant_offset (offset_iv
.base
, &offset_iv
.base
, &dinit
);
2170 outer_init
= size_binop (PLUS_EXPR
, outer_init
, dinit
);
2172 outer_step
= size_binop (PLUS_EXPR
,
2173 fold_convert (ssizetype
, base_iv
.step
),
2174 fold_convert (ssizetype
, offset_iv
.step
));
2176 STMT_VINFO_DR_STEP (stmt_info
) = outer_step
;
2177 /* FIXME: Use canonicalize_base_object_address (base_iv.base); */
2178 STMT_VINFO_DR_BASE_ADDRESS (stmt_info
) = base_iv
.base
;
2179 STMT_VINFO_DR_INIT (stmt_info
) = outer_init
;
2180 STMT_VINFO_DR_OFFSET (stmt_info
) =
2181 fold_convert (ssizetype
, offset_iv
.base
);
2182 STMT_VINFO_DR_ALIGNED_TO (stmt_info
) =
2183 size_int (highest_pow2_factor (offset_iv
.base
));
2185 if (vect_print_dump_info (REPORT_DETAILS
))
2187 fprintf (vect_dump
, "\touter base_address: ");
2188 print_generic_expr (vect_dump
, STMT_VINFO_DR_BASE_ADDRESS (stmt_info
), TDF_SLIM
);
2189 fprintf (vect_dump
, "\n\touter offset from base address: ");
2190 print_generic_expr (vect_dump
, STMT_VINFO_DR_OFFSET (stmt_info
), TDF_SLIM
);
2191 fprintf (vect_dump
, "\n\touter constant offset from base address: ");
2192 print_generic_expr (vect_dump
, STMT_VINFO_DR_INIT (stmt_info
), TDF_SLIM
);
2193 fprintf (vect_dump
, "\n\touter step: ");
2194 print_generic_expr (vect_dump
, STMT_VINFO_DR_STEP (stmt_info
), TDF_SLIM
);
2195 fprintf (vect_dump
, "\n\touter aligned to: ");
2196 print_generic_expr (vect_dump
, STMT_VINFO_DR_ALIGNED_TO (stmt_info
), TDF_SLIM
);
2200 if (STMT_VINFO_DATA_REF (stmt_info
))
2202 if (vect_print_dump_info (REPORT_UNVECTORIZED_LOCATIONS
))
2205 "not vectorized: more than one data ref in stmt: ");
2206 print_gimple_stmt (vect_dump
, stmt
, 0, TDF_SLIM
);
2211 STMT_VINFO_DATA_REF (stmt_info
) = dr
;
2213 /* Set vectype for STMT. */
2214 scalar_type
= TREE_TYPE (DR_REF (dr
));
2215 STMT_VINFO_VECTYPE (stmt_info
) =
2216 get_vectype_for_scalar_type (scalar_type
);
2217 if (!STMT_VINFO_VECTYPE (stmt_info
))
2219 if (vect_print_dump_info (REPORT_UNVECTORIZED_LOCATIONS
))
2222 "not vectorized: no vectype for stmt: ");
2223 print_gimple_stmt (vect_dump
, stmt
, 0, TDF_SLIM
);
2224 fprintf (vect_dump
, " scalar_type: ");
2225 print_generic_expr (vect_dump
, scalar_type
, TDF_DETAILS
);
2230 /* Mark the statement as not vectorizable. */
2231 STMT_VINFO_VECTORIZABLE (stmt_info
) = false;
2238 /* Adjust the minimal vectorization factor according to the
2240 vf
= TYPE_VECTOR_SUBPARTS (STMT_VINFO_VECTYPE (stmt_info
));
2249 /* Function vect_get_new_vect_var.
2251 Returns a name for a new variable. The current naming scheme appends the
2252 prefix "vect_" or "vect_p" (depending on the value of VAR_KIND) to
2253 the name of vectorizer generated variables, and appends that to NAME if
2257 vect_get_new_vect_var (tree type
, enum vect_var_kind var_kind
, const char *name
)
2264 case vect_simple_var
:
2267 case vect_scalar_var
:
2270 case vect_pointer_var
:
2279 char* tmp
= concat (prefix
, name
, NULL
);
2280 new_vect_var
= create_tmp_var (type
, tmp
);
2284 new_vect_var
= create_tmp_var (type
, prefix
);
2286 /* Mark vector typed variable as a gimple register variable. */
2287 if (TREE_CODE (type
) == VECTOR_TYPE
)
2288 DECL_GIMPLE_REG_P (new_vect_var
) = true;
2290 return new_vect_var
;
2294 /* Function vect_create_addr_base_for_vector_ref.
2296 Create an expression that computes the address of the first memory location
2297 that will be accessed for a data reference.
2300 STMT: The statement containing the data reference.
2301 NEW_STMT_LIST: Must be initialized to NULL_TREE or a statement list.
2302 OFFSET: Optional. If supplied, it is be added to the initial address.
2303 LOOP: Specify relative to which loop-nest should the address be computed.
2304 For example, when the dataref is in an inner-loop nested in an
2305 outer-loop that is now being vectorized, LOOP can be either the
2306 outer-loop, or the inner-loop. The first memory location accessed
2307 by the following dataref ('in' points to short):
2314 if LOOP=i_loop: &in (relative to i_loop)
2315 if LOOP=j_loop: &in+i*2B (relative to j_loop)
2318 1. Return an SSA_NAME whose value is the address of the memory location of
2319 the first vector of the data reference.
2320 2. If new_stmt_list is not NULL_TREE after return then the caller must insert
2321 these statement(s) which define the returned SSA_NAME.
2323 FORNOW: We are only handling array accesses with step 1. */
2326 vect_create_addr_base_for_vector_ref (gimple stmt
,
2327 gimple_seq
*new_stmt_list
,
2331 stmt_vec_info stmt_info
= vinfo_for_stmt (stmt
);
2332 struct data_reference
*dr
= STMT_VINFO_DATA_REF (stmt_info
);
2333 tree data_ref_base
= unshare_expr (DR_BASE_ADDRESS (dr
));
2335 tree data_ref_base_var
;
2337 tree addr_base
, addr_expr
;
2339 gimple_seq seq
= NULL
;
2340 tree base_offset
= unshare_expr (DR_OFFSET (dr
));
2341 tree init
= unshare_expr (DR_INIT (dr
));
2343 tree step
= TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dr
)));
2344 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_info
);
2347 if (loop_vinfo
&& loop
&& loop
!= (gimple_bb (stmt
))->loop_father
)
2349 struct loop
*outer_loop
= LOOP_VINFO_LOOP (loop_vinfo
);
2351 gcc_assert (nested_in_vect_loop_p (outer_loop
, stmt
));
2353 data_ref_base
= unshare_expr (STMT_VINFO_DR_BASE_ADDRESS (stmt_info
));
2354 base_offset
= unshare_expr (STMT_VINFO_DR_OFFSET (stmt_info
));
2355 init
= unshare_expr (STMT_VINFO_DR_INIT (stmt_info
));
2359 base_name
= build_fold_indirect_ref (data_ref_base
);
2362 base_offset
= ssize_int (0);
2363 init
= ssize_int (0);
2364 base_name
= build_fold_indirect_ref (unshare_expr (DR_REF (dr
)));
2367 data_ref_base_var
= create_tmp_var (TREE_TYPE (data_ref_base
), "batmp");
2368 add_referenced_var (data_ref_base_var
);
2369 data_ref_base
= force_gimple_operand (data_ref_base
, &seq
, true,
2371 gimple_seq_add_seq (new_stmt_list
, seq
);
2373 /* Create base_offset */
2374 base_offset
= size_binop (PLUS_EXPR
,
2375 fold_convert (sizetype
, base_offset
),
2376 fold_convert (sizetype
, init
));
2377 dest
= create_tmp_var (sizetype
, "base_off");
2378 add_referenced_var (dest
);
2379 base_offset
= force_gimple_operand (base_offset
, &seq
, true, dest
);
2380 gimple_seq_add_seq (new_stmt_list
, seq
);
2384 tree tmp
= create_tmp_var (sizetype
, "offset");
2386 add_referenced_var (tmp
);
2387 offset
= fold_build2 (MULT_EXPR
, sizetype
,
2388 fold_convert (sizetype
, offset
), step
);
2389 base_offset
= fold_build2 (PLUS_EXPR
, sizetype
,
2390 base_offset
, offset
);
2391 base_offset
= force_gimple_operand (base_offset
, &seq
, false, tmp
);
2392 gimple_seq_add_seq (new_stmt_list
, seq
);
2395 /* base + base_offset */
2397 addr_base
= fold_build2 (POINTER_PLUS_EXPR
, TREE_TYPE (data_ref_base
),
2398 data_ref_base
, base_offset
);
2401 addr_base
= build1 (ADDR_EXPR
,
2402 build_pointer_type (TREE_TYPE (DR_REF (dr
))),
2403 unshare_expr (DR_REF (dr
)));
2406 vect_ptr_type
= build_pointer_type (STMT_VINFO_VECTYPE (stmt_info
));
2407 base
= get_base_address (DR_REF (dr
));
2409 && INDIRECT_REF_P (base
))
2411 = build_qualified_type (vect_ptr_type
,
2412 TYPE_QUALS (TREE_TYPE (TREE_OPERAND (base
, 0))));
2414 vec_stmt
= fold_convert (vect_ptr_type
, addr_base
);
2415 addr_expr
= vect_get_new_vect_var (vect_ptr_type
, vect_pointer_var
,
2416 get_name (base_name
));
2417 add_referenced_var (addr_expr
);
2418 vec_stmt
= force_gimple_operand (vec_stmt
, &seq
, false, addr_expr
);
2419 gimple_seq_add_seq (new_stmt_list
, seq
);
2421 if (vect_print_dump_info (REPORT_DETAILS
))
2423 fprintf (vect_dump
, "created ");
2424 print_generic_expr (vect_dump
, vec_stmt
, TDF_SLIM
);
2431 /* Function vect_create_data_ref_ptr.
2433 Create a new pointer to vector type (vp), that points to the first location
2434 accessed in the loop by STMT, along with the def-use update chain to
2435 appropriately advance the pointer through the loop iterations. Also set
2436 aliasing information for the pointer. This vector pointer is used by the
2437 callers to this function to create a memory reference expression for vector
2441 1. STMT: a stmt that references memory. Expected to be of the form
2442 GIMPLE_ASSIGN <name, data-ref> or
2443 GIMPLE_ASSIGN <data-ref, name>.
2444 2. AT_LOOP: the loop where the vector memref is to be created.
2445 3. OFFSET (optional): an offset to be added to the initial address accessed
2446 by the data-ref in STMT.
2447 4. ONLY_INIT: indicate if vp is to be updated in the loop, or remain
2448 pointing to the initial address.
2449 5. TYPE: if not NULL indicates the required type of the data-ref.
2452 1. Declare a new ptr to vector_type, and have it point to the base of the
2453 data reference (initial addressed accessed by the data reference).
2454 For example, for vector of type V8HI, the following code is generated:
2457 vp = (v8hi *)initial_address;
2459 if OFFSET is not supplied:
2460 initial_address = &a[init];
2461 if OFFSET is supplied:
2462 initial_address = &a[init + OFFSET];
2464 Return the initial_address in INITIAL_ADDRESS.
2466 2. If ONLY_INIT is true, just return the initial pointer. Otherwise, also
2467 update the pointer in each iteration of the loop.
2469 Return the increment stmt that updates the pointer in PTR_INCR.
2471 3. Set INV_P to true if the access pattern of the data reference in the
2472 vectorized loop is invariant. Set it to false otherwise.
2474 4. Return the pointer. */
2477 vect_create_data_ref_ptr (gimple stmt
, struct loop
*at_loop
,
2478 tree offset
, tree
*initial_address
, gimple
*ptr_incr
,
2479 bool only_init
, bool *inv_p
)
2482 stmt_vec_info stmt_info
= vinfo_for_stmt (stmt
);
2483 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_info
);
2484 struct loop
*loop
= NULL
;
2485 bool nested_in_vect_loop
= false;
2486 struct loop
*containing_loop
= NULL
;
2487 tree vectype
= STMT_VINFO_VECTYPE (stmt_info
);
2492 gimple_seq new_stmt_list
= NULL
;
2496 struct data_reference
*dr
= STMT_VINFO_DATA_REF (stmt_info
);
2498 gimple_stmt_iterator incr_gsi
;
2500 tree indx_before_incr
, indx_after_incr
;
2503 bb_vec_info bb_vinfo
= STMT_VINFO_BB_VINFO (stmt_info
);
2504 gimple_stmt_iterator gsi
= gsi_for_stmt (stmt
);
2509 loop
= LOOP_VINFO_LOOP (loop_vinfo
);
2510 nested_in_vect_loop
= nested_in_vect_loop_p (loop
, stmt
);
2511 containing_loop
= (gimple_bb (stmt
))->loop_father
;
2512 pe
= loop_preheader_edge (loop
);
2516 gcc_assert (bb_vinfo
);
2521 /* Check the step (evolution) of the load in LOOP, and record
2522 whether it's invariant. */
2523 if (nested_in_vect_loop
)
2524 step
= STMT_VINFO_DR_STEP (stmt_info
);
2526 step
= DR_STEP (STMT_VINFO_DATA_REF (stmt_info
));
2528 if (tree_int_cst_compare (step
, size_zero_node
) == 0)
2533 /* Create an expression for the first address accessed by this load
2535 base_name
= build_fold_indirect_ref (unshare_expr (DR_BASE_ADDRESS (dr
)));
2537 if (vect_print_dump_info (REPORT_DETAILS
))
2539 tree data_ref_base
= base_name
;
2540 fprintf (vect_dump
, "create vector-pointer variable to type: ");
2541 print_generic_expr (vect_dump
, vectype
, TDF_SLIM
);
2542 if (TREE_CODE (data_ref_base
) == VAR_DECL
2543 || TREE_CODE (data_ref_base
) == ARRAY_REF
)
2544 fprintf (vect_dump
, " vectorizing an array ref: ");
2545 else if (TREE_CODE (data_ref_base
) == COMPONENT_REF
)
2546 fprintf (vect_dump
, " vectorizing a record based array ref: ");
2547 else if (TREE_CODE (data_ref_base
) == SSA_NAME
)
2548 fprintf (vect_dump
, " vectorizing a pointer ref: ");
2549 print_generic_expr (vect_dump
, base_name
, TDF_SLIM
);
2552 /** (1) Create the new vector-pointer variable: **/
2553 vect_ptr_type
= build_pointer_type (vectype
);
2554 base
= get_base_address (DR_REF (dr
));
2556 && INDIRECT_REF_P (base
))
2558 = build_qualified_type (vect_ptr_type
,
2559 TYPE_QUALS (TREE_TYPE (TREE_OPERAND (base
, 0))));
2560 vect_ptr
= vect_get_new_vect_var (vect_ptr_type
, vect_pointer_var
,
2561 get_name (base_name
));
2563 /* Vector types inherit the alias set of their component type by default so
2564 we need to use a ref-all pointer if the data reference does not conflict
2565 with the created vector data reference because it is not addressable. */
2566 if (!alias_sets_conflict_p (get_deref_alias_set (vect_ptr
),
2567 get_alias_set (DR_REF (dr
))))
2570 = build_pointer_type_for_mode (vectype
,
2571 TYPE_MODE (vect_ptr_type
), true);
2572 vect_ptr
= vect_get_new_vect_var (vect_ptr_type
, vect_pointer_var
,
2573 get_name (base_name
));
2576 /* Likewise for any of the data references in the stmt group. */
2577 else if (STMT_VINFO_DR_GROUP_SIZE (stmt_info
) > 1)
2579 gimple orig_stmt
= STMT_VINFO_DR_GROUP_FIRST_DR (stmt_info
);
2582 tree lhs
= gimple_assign_lhs (orig_stmt
);
2583 if (!alias_sets_conflict_p (get_deref_alias_set (vect_ptr
),
2584 get_alias_set (lhs
)))
2587 = build_pointer_type_for_mode (vectype
,
2588 TYPE_MODE (vect_ptr_type
), true);
2590 = vect_get_new_vect_var (vect_ptr_type
, vect_pointer_var
,
2591 get_name (base_name
));
2595 orig_stmt
= STMT_VINFO_DR_GROUP_NEXT_DR (vinfo_for_stmt (orig_stmt
));
2600 add_referenced_var (vect_ptr
);
2602 /** Note: If the dataref is in an inner-loop nested in LOOP, and we are
2603 vectorizing LOOP (i.e. outer-loop vectorization), we need to create two
2604 def-use update cycles for the pointer: One relative to the outer-loop
2605 (LOOP), which is what steps (3) and (4) below do. The other is relative
2606 to the inner-loop (which is the inner-most loop containing the dataref),
2607 and this is done be step (5) below.
2609 When vectorizing inner-most loops, the vectorized loop (LOOP) is also the
2610 inner-most loop, and so steps (3),(4) work the same, and step (5) is
2611 redundant. Steps (3),(4) create the following:
2614 LOOP: vp1 = phi(vp0,vp2)
2620 If there is an inner-loop nested in loop, then step (5) will also be
2621 applied, and an additional update in the inner-loop will be created:
2624 LOOP: vp1 = phi(vp0,vp2)
2626 inner: vp3 = phi(vp1,vp4)
2627 vp4 = vp3 + inner_step
2633 /** (3) Calculate the initial address the vector-pointer, and set
2634 the vector-pointer to point to it before the loop: **/
2636 /* Create: (&(base[init_val+offset]) in the loop preheader. */
2638 new_temp
= vect_create_addr_base_for_vector_ref (stmt
, &new_stmt_list
,
2644 new_bb
= gsi_insert_seq_on_edge_immediate (pe
, new_stmt_list
);
2645 gcc_assert (!new_bb
);
2648 gsi_insert_seq_before (&gsi
, new_stmt_list
, GSI_SAME_STMT
);
2651 *initial_address
= new_temp
;
2653 /* Create: p = (vectype *) initial_base */
2654 vec_stmt
= gimple_build_assign (vect_ptr
,
2655 fold_convert (vect_ptr_type
, new_temp
));
2656 vect_ptr_init
= make_ssa_name (vect_ptr
, vec_stmt
);
2657 gimple_assign_set_lhs (vec_stmt
, vect_ptr_init
);
2660 new_bb
= gsi_insert_on_edge_immediate (pe
, vec_stmt
);
2661 gcc_assert (!new_bb
);
2664 gsi_insert_before (&gsi
, vec_stmt
, GSI_SAME_STMT
);
2666 /** (4) Handle the updating of the vector-pointer inside the loop.
2667 This is needed when ONLY_INIT is false, and also when AT_LOOP
2668 is the inner-loop nested in LOOP (during outer-loop vectorization).
2671 /* No update in loop is required. */
2672 if (only_init
&& (!loop_vinfo
|| at_loop
== loop
))
2674 /* Copy the points-to information if it exists. */
2675 if (DR_PTR_INFO (dr
))
2676 duplicate_ssa_name_ptr_info (vect_ptr_init
, DR_PTR_INFO (dr
));
2677 vptr
= vect_ptr_init
;
2681 /* The step of the vector pointer is the Vector Size. */
2682 tree step
= TYPE_SIZE_UNIT (vectype
);
2683 /* One exception to the above is when the scalar step of the load in
2684 LOOP is zero. In this case the step here is also zero. */
2686 step
= size_zero_node
;
2688 standard_iv_increment_position (loop
, &incr_gsi
, &insert_after
);
2690 create_iv (vect_ptr_init
,
2691 fold_convert (vect_ptr_type
, step
),
2692 vect_ptr
, loop
, &incr_gsi
, insert_after
,
2693 &indx_before_incr
, &indx_after_incr
);
2694 incr
= gsi_stmt (incr_gsi
);
2695 set_vinfo_for_stmt (incr
, new_stmt_vec_info (incr
, loop_vinfo
, NULL
));
2697 /* Copy the points-to information if it exists. */
2698 if (DR_PTR_INFO (dr
))
2700 duplicate_ssa_name_ptr_info (indx_before_incr
, DR_PTR_INFO (dr
));
2701 duplicate_ssa_name_ptr_info (indx_after_incr
, DR_PTR_INFO (dr
));
2706 vptr
= indx_before_incr
;
2709 if (!nested_in_vect_loop
|| only_init
)
2713 /** (5) Handle the updating of the vector-pointer inside the inner-loop
2714 nested in LOOP, if exists: **/
2716 gcc_assert (nested_in_vect_loop
);
2719 standard_iv_increment_position (containing_loop
, &incr_gsi
,
2721 create_iv (vptr
, fold_convert (vect_ptr_type
, DR_STEP (dr
)), vect_ptr
,
2722 containing_loop
, &incr_gsi
, insert_after
, &indx_before_incr
,
2724 incr
= gsi_stmt (incr_gsi
);
2725 set_vinfo_for_stmt (incr
, new_stmt_vec_info (incr
, loop_vinfo
, NULL
));
2727 /* Copy the points-to information if it exists. */
2728 if (DR_PTR_INFO (dr
))
2730 duplicate_ssa_name_ptr_info (indx_before_incr
, DR_PTR_INFO (dr
));
2731 duplicate_ssa_name_ptr_info (indx_after_incr
, DR_PTR_INFO (dr
));
2736 return indx_before_incr
;
2743 /* Function bump_vector_ptr
2745 Increment a pointer (to a vector type) by vector-size. If requested,
2746 i.e. if PTR-INCR is given, then also connect the new increment stmt
2747 to the existing def-use update-chain of the pointer, by modifying
2748 the PTR_INCR as illustrated below:
2750 The pointer def-use update-chain before this function:
2751 DATAREF_PTR = phi (p_0, p_2)
2753 PTR_INCR: p_2 = DATAREF_PTR + step
2755 The pointer def-use update-chain after this function:
2756 DATAREF_PTR = phi (p_0, p_2)
2758 NEW_DATAREF_PTR = DATAREF_PTR + BUMP
2760 PTR_INCR: p_2 = NEW_DATAREF_PTR + step
2763 DATAREF_PTR - ssa_name of a pointer (to vector type) that is being updated
2765 PTR_INCR - optional. The stmt that updates the pointer in each iteration of
2766 the loop. The increment amount across iterations is expected
2768 BSI - location where the new update stmt is to be placed.
2769 STMT - the original scalar memory-access stmt that is being vectorized.
2770 BUMP - optional. The offset by which to bump the pointer. If not given,
2771 the offset is assumed to be vector_size.
2773 Output: Return NEW_DATAREF_PTR as illustrated above.
2778 bump_vector_ptr (tree dataref_ptr
, gimple ptr_incr
, gimple_stmt_iterator
*gsi
,
2779 gimple stmt
, tree bump
)
2781 stmt_vec_info stmt_info
= vinfo_for_stmt (stmt
);
2782 struct data_reference
*dr
= STMT_VINFO_DATA_REF (stmt_info
);
2783 tree vectype
= STMT_VINFO_VECTYPE (stmt_info
);
2784 tree ptr_var
= SSA_NAME_VAR (dataref_ptr
);
2785 tree update
= TYPE_SIZE_UNIT (vectype
);
2788 use_operand_p use_p
;
2789 tree new_dataref_ptr
;
2794 incr_stmt
= gimple_build_assign_with_ops (POINTER_PLUS_EXPR
, ptr_var
,
2795 dataref_ptr
, update
);
2796 new_dataref_ptr
= make_ssa_name (ptr_var
, incr_stmt
);
2797 gimple_assign_set_lhs (incr_stmt
, new_dataref_ptr
);
2798 vect_finish_stmt_generation (stmt
, incr_stmt
, gsi
);
2800 /* Copy the points-to information if it exists. */
2801 if (DR_PTR_INFO (dr
))
2802 duplicate_ssa_name_ptr_info (new_dataref_ptr
, DR_PTR_INFO (dr
));
2805 return new_dataref_ptr
;
2807 /* Update the vector-pointer's cross-iteration increment. */
2808 FOR_EACH_SSA_USE_OPERAND (use_p
, ptr_incr
, iter
, SSA_OP_USE
)
2810 tree use
= USE_FROM_PTR (use_p
);
2812 if (use
== dataref_ptr
)
2813 SET_USE (use_p
, new_dataref_ptr
);
2815 gcc_assert (tree_int_cst_compare (use
, update
) == 0);
2818 return new_dataref_ptr
;
2822 /* Function vect_create_destination_var.
2824 Create a new temporary of type VECTYPE. */
2827 vect_create_destination_var (tree scalar_dest
, tree vectype
)
2830 const char *new_name
;
2832 enum vect_var_kind kind
;
2834 kind
= vectype
? vect_simple_var
: vect_scalar_var
;
2835 type
= vectype
? vectype
: TREE_TYPE (scalar_dest
);
2837 gcc_assert (TREE_CODE (scalar_dest
) == SSA_NAME
);
2839 new_name
= get_name (scalar_dest
);
2842 vec_dest
= vect_get_new_vect_var (type
, kind
, new_name
);
2843 add_referenced_var (vec_dest
);
2848 /* Function vect_strided_store_supported.
2850 Returns TRUE is INTERLEAVE_HIGH and INTERLEAVE_LOW operations are supported,
2851 and FALSE otherwise. */
2854 vect_strided_store_supported (tree vectype
)
2856 optab interleave_high_optab
, interleave_low_optab
;
2859 mode
= (int) TYPE_MODE (vectype
);
2861 /* Check that the operation is supported. */
2862 interleave_high_optab
= optab_for_tree_code (VEC_INTERLEAVE_HIGH_EXPR
,
2863 vectype
, optab_default
);
2864 interleave_low_optab
= optab_for_tree_code (VEC_INTERLEAVE_LOW_EXPR
,
2865 vectype
, optab_default
);
2866 if (!interleave_high_optab
|| !interleave_low_optab
)
2868 if (vect_print_dump_info (REPORT_DETAILS
))
2869 fprintf (vect_dump
, "no optab for interleave.");
2873 if (optab_handler (interleave_high_optab
, mode
)->insn_code
2875 || optab_handler (interleave_low_optab
, mode
)->insn_code
2876 == CODE_FOR_nothing
)
2878 if (vect_print_dump_info (REPORT_DETAILS
))
2879 fprintf (vect_dump
, "interleave op not supported by target.");
2887 /* Function vect_permute_store_chain.
2889 Given a chain of interleaved stores in DR_CHAIN of LENGTH that must be
2890 a power of 2, generate interleave_high/low stmts to reorder the data
2891 correctly for the stores. Return the final references for stores in
2894 E.g., LENGTH is 4 and the scalar type is short, i.e., VF is 8.
2895 The input is 4 vectors each containing 8 elements. We assign a number to each
2896 element, the input sequence is:
2898 1st vec: 0 1 2 3 4 5 6 7
2899 2nd vec: 8 9 10 11 12 13 14 15
2900 3rd vec: 16 17 18 19 20 21 22 23
2901 4th vec: 24 25 26 27 28 29 30 31
2903 The output sequence should be:
2905 1st vec: 0 8 16 24 1 9 17 25
2906 2nd vec: 2 10 18 26 3 11 19 27
2907 3rd vec: 4 12 20 28 5 13 21 30
2908 4th vec: 6 14 22 30 7 15 23 31
2910 i.e., we interleave the contents of the four vectors in their order.
2912 We use interleave_high/low instructions to create such output. The input of
2913 each interleave_high/low operation is two vectors:
2916 the even elements of the result vector are obtained left-to-right from the
2917 high/low elements of the first vector. The odd elements of the result are
2918 obtained left-to-right from the high/low elements of the second vector.
2919 The output of interleave_high will be: 0 4 1 5
2920 and of interleave_low: 2 6 3 7
2923 The permutation is done in log LENGTH stages. In each stage interleave_high
2924 and interleave_low stmts are created for each pair of vectors in DR_CHAIN,
2925 where the first argument is taken from the first half of DR_CHAIN and the
2926 second argument from it's second half.
2929 I1: interleave_high (1st vec, 3rd vec)
2930 I2: interleave_low (1st vec, 3rd vec)
2931 I3: interleave_high (2nd vec, 4th vec)
2932 I4: interleave_low (2nd vec, 4th vec)
2934 The output for the first stage is:
2936 I1: 0 16 1 17 2 18 3 19
2937 I2: 4 20 5 21 6 22 7 23
2938 I3: 8 24 9 25 10 26 11 27
2939 I4: 12 28 13 29 14 30 15 31
2941 The output of the second stage, i.e. the final result is:
2943 I1: 0 8 16 24 1 9 17 25
2944 I2: 2 10 18 26 3 11 19 27
2945 I3: 4 12 20 28 5 13 21 30
2946 I4: 6 14 22 30 7 15 23 31. */
2949 vect_permute_store_chain (VEC(tree
,heap
) *dr_chain
,
2950 unsigned int length
,
2952 gimple_stmt_iterator
*gsi
,
2953 VEC(tree
,heap
) **result_chain
)
2955 tree perm_dest
, vect1
, vect2
, high
, low
;
2957 tree vectype
= STMT_VINFO_VECTYPE (vinfo_for_stmt (stmt
));
2960 enum tree_code high_code
, low_code
;
2962 /* Check that the operation is supported. */
2963 if (!vect_strided_store_supported (vectype
))
2966 *result_chain
= VEC_copy (tree
, heap
, dr_chain
);
2968 for (i
= 0; i
< exact_log2 (length
); i
++)
2970 for (j
= 0; j
< length
/2; j
++)
2972 vect1
= VEC_index (tree
, dr_chain
, j
);
2973 vect2
= VEC_index (tree
, dr_chain
, j
+length
/2);
2975 /* Create interleaving stmt:
2976 in the case of big endian:
2977 high = interleave_high (vect1, vect2)
2978 and in the case of little endian:
2979 high = interleave_low (vect1, vect2). */
2980 perm_dest
= create_tmp_var (vectype
, "vect_inter_high");
2981 DECL_GIMPLE_REG_P (perm_dest
) = 1;
2982 add_referenced_var (perm_dest
);
2983 if (BYTES_BIG_ENDIAN
)
2985 high_code
= VEC_INTERLEAVE_HIGH_EXPR
;
2986 low_code
= VEC_INTERLEAVE_LOW_EXPR
;
2990 low_code
= VEC_INTERLEAVE_HIGH_EXPR
;
2991 high_code
= VEC_INTERLEAVE_LOW_EXPR
;
2993 perm_stmt
= gimple_build_assign_with_ops (high_code
, perm_dest
,
2995 high
= make_ssa_name (perm_dest
, perm_stmt
);
2996 gimple_assign_set_lhs (perm_stmt
, high
);
2997 vect_finish_stmt_generation (stmt
, perm_stmt
, gsi
);
2998 VEC_replace (tree
, *result_chain
, 2*j
, high
);
3000 /* Create interleaving stmt:
3001 in the case of big endian:
3002 low = interleave_low (vect1, vect2)
3003 and in the case of little endian:
3004 low = interleave_high (vect1, vect2). */
3005 perm_dest
= create_tmp_var (vectype
, "vect_inter_low");
3006 DECL_GIMPLE_REG_P (perm_dest
) = 1;
3007 add_referenced_var (perm_dest
);
3008 perm_stmt
= gimple_build_assign_with_ops (low_code
, perm_dest
,
3010 low
= make_ssa_name (perm_dest
, perm_stmt
);
3011 gimple_assign_set_lhs (perm_stmt
, low
);
3012 vect_finish_stmt_generation (stmt
, perm_stmt
, gsi
);
3013 VEC_replace (tree
, *result_chain
, 2*j
+1, low
);
3015 dr_chain
= VEC_copy (tree
, heap
, *result_chain
);
3020 /* Function vect_setup_realignment
3022 This function is called when vectorizing an unaligned load using
3023 the dr_explicit_realign[_optimized] scheme.
3024 This function generates the following code at the loop prolog:
3027 x msq_init = *(floor(p)); # prolog load
3028 realignment_token = call target_builtin;
3030 x msq = phi (msq_init, ---)
3032 The stmts marked with x are generated only for the case of
3033 dr_explicit_realign_optimized.
3035 The code above sets up a new (vector) pointer, pointing to the first
3036 location accessed by STMT, and a "floor-aligned" load using that pointer.
3037 It also generates code to compute the "realignment-token" (if the relevant
3038 target hook was defined), and creates a phi-node at the loop-header bb
3039 whose arguments are the result of the prolog-load (created by this
3040 function) and the result of a load that takes place in the loop (to be
3041 created by the caller to this function).
3043 For the case of dr_explicit_realign_optimized:
3044 The caller to this function uses the phi-result (msq) to create the
3045 realignment code inside the loop, and sets up the missing phi argument,
3048 msq = phi (msq_init, lsq)
3049 lsq = *(floor(p')); # load in loop
3050 result = realign_load (msq, lsq, realignment_token);
3052 For the case of dr_explicit_realign:
3054 msq = *(floor(p)); # load in loop
3056 lsq = *(floor(p')); # load in loop
3057 result = realign_load (msq, lsq, realignment_token);
3060 STMT - (scalar) load stmt to be vectorized. This load accesses
3061 a memory location that may be unaligned.
3062 BSI - place where new code is to be inserted.
3063 ALIGNMENT_SUPPORT_SCHEME - which of the two misalignment handling schemes
3067 REALIGNMENT_TOKEN - the result of a call to the builtin_mask_for_load
3068 target hook, if defined.
3069 Return value - the result of the loop-header phi node. */
3072 vect_setup_realignment (gimple stmt
, gimple_stmt_iterator
*gsi
,
3073 tree
*realignment_token
,
3074 enum dr_alignment_support alignment_support_scheme
,
3076 struct loop
**at_loop
)
3078 stmt_vec_info stmt_info
= vinfo_for_stmt (stmt
);
3079 tree vectype
= STMT_VINFO_VECTYPE (stmt_info
);
3080 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_info
);
3081 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
3083 tree scalar_dest
= gimple_assign_lhs (stmt
);
3090 tree msq_init
= NULL_TREE
;
3093 tree msq
= NULL_TREE
;
3094 gimple_seq stmts
= NULL
;
3096 bool compute_in_loop
= false;
3097 bool nested_in_vect_loop
= nested_in_vect_loop_p (loop
, stmt
);
3098 struct loop
*containing_loop
= (gimple_bb (stmt
))->loop_father
;
3099 struct loop
*loop_for_initial_load
;
3101 gcc_assert (alignment_support_scheme
== dr_explicit_realign
3102 || alignment_support_scheme
== dr_explicit_realign_optimized
);
3104 /* We need to generate three things:
3105 1. the misalignment computation
3106 2. the extra vector load (for the optimized realignment scheme).
3107 3. the phi node for the two vectors from which the realignment is
3108 done (for the optimized realignment scheme).
3111 /* 1. Determine where to generate the misalignment computation.
3113 If INIT_ADDR is NULL_TREE, this indicates that the misalignment
3114 calculation will be generated by this function, outside the loop (in the
3115 preheader). Otherwise, INIT_ADDR had already been computed for us by the
3116 caller, inside the loop.
3118 Background: If the misalignment remains fixed throughout the iterations of
3119 the loop, then both realignment schemes are applicable, and also the
3120 misalignment computation can be done outside LOOP. This is because we are
3121 vectorizing LOOP, and so the memory accesses in LOOP advance in steps that
3122 are a multiple of VS (the Vector Size), and therefore the misalignment in
3123 different vectorized LOOP iterations is always the same.
3124 The problem arises only if the memory access is in an inner-loop nested
3125 inside LOOP, which is now being vectorized using outer-loop vectorization.
3126 This is the only case when the misalignment of the memory access may not
3127 remain fixed throughout the iterations of the inner-loop (as explained in
3128 detail in vect_supportable_dr_alignment). In this case, not only is the
3129 optimized realignment scheme not applicable, but also the misalignment
3130 computation (and generation of the realignment token that is passed to
3131 REALIGN_LOAD) have to be done inside the loop.
3133 In short, INIT_ADDR indicates whether we are in a COMPUTE_IN_LOOP mode
3134 or not, which in turn determines if the misalignment is computed inside
3135 the inner-loop, or outside LOOP. */
3137 if (init_addr
!= NULL_TREE
)
3139 compute_in_loop
= true;
3140 gcc_assert (alignment_support_scheme
== dr_explicit_realign
);
3144 /* 2. Determine where to generate the extra vector load.
3146 For the optimized realignment scheme, instead of generating two vector
3147 loads in each iteration, we generate a single extra vector load in the
3148 preheader of the loop, and in each iteration reuse the result of the
3149 vector load from the previous iteration. In case the memory access is in
3150 an inner-loop nested inside LOOP, which is now being vectorized using
3151 outer-loop vectorization, we need to determine whether this initial vector
3152 load should be generated at the preheader of the inner-loop, or can be
3153 generated at the preheader of LOOP. If the memory access has no evolution
3154 in LOOP, it can be generated in the preheader of LOOP. Otherwise, it has
3155 to be generated inside LOOP (in the preheader of the inner-loop). */
3157 if (nested_in_vect_loop
)
3159 tree outerloop_step
= STMT_VINFO_DR_STEP (stmt_info
);
3160 bool invariant_in_outerloop
=
3161 (tree_int_cst_compare (outerloop_step
, size_zero_node
) == 0);
3162 loop_for_initial_load
= (invariant_in_outerloop
? loop
: loop
->inner
);
3165 loop_for_initial_load
= loop
;
3167 *at_loop
= loop_for_initial_load
;
3169 /* 3. For the case of the optimized realignment, create the first vector
3170 load at the loop preheader. */
3172 if (alignment_support_scheme
== dr_explicit_realign_optimized
)
3174 /* Create msq_init = *(floor(p1)) in the loop preheader */
3176 gcc_assert (!compute_in_loop
);
3177 pe
= loop_preheader_edge (loop_for_initial_load
);
3178 vec_dest
= vect_create_destination_var (scalar_dest
, vectype
);
3179 ptr
= vect_create_data_ref_ptr (stmt
, loop_for_initial_load
, NULL_TREE
,
3180 &init_addr
, &inc
, true, &inv_p
);
3181 data_ref
= build1 (ALIGN_INDIRECT_REF
, vectype
, ptr
);
3182 new_stmt
= gimple_build_assign (vec_dest
, data_ref
);
3183 new_temp
= make_ssa_name (vec_dest
, new_stmt
);
3184 gimple_assign_set_lhs (new_stmt
, new_temp
);
3185 mark_symbols_for_renaming (new_stmt
);
3186 new_bb
= gsi_insert_on_edge_immediate (pe
, new_stmt
);
3187 gcc_assert (!new_bb
);
3188 msq_init
= gimple_assign_lhs (new_stmt
);
3191 /* 4. Create realignment token using a target builtin, if available.
3192 It is done either inside the containing loop, or before LOOP (as
3193 determined above). */
3195 if (targetm
.vectorize
.builtin_mask_for_load
)
3199 /* Compute INIT_ADDR - the initial addressed accessed by this memref. */
3200 if (compute_in_loop
)
3201 gcc_assert (init_addr
); /* already computed by the caller. */
3204 /* Generate the INIT_ADDR computation outside LOOP. */
3205 init_addr
= vect_create_addr_base_for_vector_ref (stmt
, &stmts
,
3207 pe
= loop_preheader_edge (loop
);
3208 new_bb
= gsi_insert_seq_on_edge_immediate (pe
, stmts
);
3209 gcc_assert (!new_bb
);
3212 builtin_decl
= targetm
.vectorize
.builtin_mask_for_load ();
3213 new_stmt
= gimple_build_call (builtin_decl
, 1, init_addr
);
3215 vect_create_destination_var (scalar_dest
,
3216 gimple_call_return_type (new_stmt
));
3217 new_temp
= make_ssa_name (vec_dest
, new_stmt
);
3218 gimple_call_set_lhs (new_stmt
, new_temp
);
3220 if (compute_in_loop
)
3221 gsi_insert_before (gsi
, new_stmt
, GSI_SAME_STMT
);
3224 /* Generate the misalignment computation outside LOOP. */
3225 pe
= loop_preheader_edge (loop
);
3226 new_bb
= gsi_insert_on_edge_immediate (pe
, new_stmt
);
3227 gcc_assert (!new_bb
);
3230 *realignment_token
= gimple_call_lhs (new_stmt
);
3232 /* The result of the CALL_EXPR to this builtin is determined from
3233 the value of the parameter and no global variables are touched
3234 which makes the builtin a "const" function. Requiring the
3235 builtin to have the "const" attribute makes it unnecessary
3236 to call mark_call_clobbered. */
3237 gcc_assert (TREE_READONLY (builtin_decl
));
3240 if (alignment_support_scheme
== dr_explicit_realign
)
3243 gcc_assert (!compute_in_loop
);
3244 gcc_assert (alignment_support_scheme
== dr_explicit_realign_optimized
);
3247 /* 5. Create msq = phi <msq_init, lsq> in loop */
3249 pe
= loop_preheader_edge (containing_loop
);
3250 vec_dest
= vect_create_destination_var (scalar_dest
, vectype
);
3251 msq
= make_ssa_name (vec_dest
, NULL
);
3252 phi_stmt
= create_phi_node (msq
, containing_loop
->header
);
3253 SSA_NAME_DEF_STMT (msq
) = phi_stmt
;
3254 add_phi_arg (phi_stmt
, msq_init
, pe
, UNKNOWN_LOCATION
);
3260 /* Function vect_strided_load_supported.
3262 Returns TRUE is EXTRACT_EVEN and EXTRACT_ODD operations are supported,
3263 and FALSE otherwise. */
3266 vect_strided_load_supported (tree vectype
)
3268 optab perm_even_optab
, perm_odd_optab
;
3271 mode
= (int) TYPE_MODE (vectype
);
3273 perm_even_optab
= optab_for_tree_code (VEC_EXTRACT_EVEN_EXPR
, vectype
,
3275 if (!perm_even_optab
)
3277 if (vect_print_dump_info (REPORT_DETAILS
))
3278 fprintf (vect_dump
, "no optab for perm_even.");
3282 if (optab_handler (perm_even_optab
, mode
)->insn_code
== CODE_FOR_nothing
)
3284 if (vect_print_dump_info (REPORT_DETAILS
))
3285 fprintf (vect_dump
, "perm_even op not supported by target.");
3289 perm_odd_optab
= optab_for_tree_code (VEC_EXTRACT_ODD_EXPR
, vectype
,
3291 if (!perm_odd_optab
)
3293 if (vect_print_dump_info (REPORT_DETAILS
))
3294 fprintf (vect_dump
, "no optab for perm_odd.");
3298 if (optab_handler (perm_odd_optab
, mode
)->insn_code
== CODE_FOR_nothing
)
3300 if (vect_print_dump_info (REPORT_DETAILS
))
3301 fprintf (vect_dump
, "perm_odd op not supported by target.");
3308 /* Function vect_permute_load_chain.
3310 Given a chain of interleaved loads in DR_CHAIN of LENGTH that must be
3311 a power of 2, generate extract_even/odd stmts to reorder the input data
3312 correctly. Return the final references for loads in RESULT_CHAIN.
3314 E.g., LENGTH is 4 and the scalar type is short, i.e., VF is 8.
3315 The input is 4 vectors each containing 8 elements. We assign a number to each
3316 element, the input sequence is:
3318 1st vec: 0 1 2 3 4 5 6 7
3319 2nd vec: 8 9 10 11 12 13 14 15
3320 3rd vec: 16 17 18 19 20 21 22 23
3321 4th vec: 24 25 26 27 28 29 30 31
3323 The output sequence should be:
3325 1st vec: 0 4 8 12 16 20 24 28
3326 2nd vec: 1 5 9 13 17 21 25 29
3327 3rd vec: 2 6 10 14 18 22 26 30
3328 4th vec: 3 7 11 15 19 23 27 31
3330 i.e., the first output vector should contain the first elements of each
3331 interleaving group, etc.
3333 We use extract_even/odd instructions to create such output. The input of each
3334 extract_even/odd operation is two vectors
3338 and the output is the vector of extracted even/odd elements. The output of
3339 extract_even will be: 0 2 4 6
3340 and of extract_odd: 1 3 5 7
3343 The permutation is done in log LENGTH stages. In each stage extract_even and
3344 extract_odd stmts are created for each pair of vectors in DR_CHAIN in their
3345 order. In our example,
3347 E1: extract_even (1st vec, 2nd vec)
3348 E2: extract_odd (1st vec, 2nd vec)
3349 E3: extract_even (3rd vec, 4th vec)
3350 E4: extract_odd (3rd vec, 4th vec)
3352 The output for the first stage will be:
3354 E1: 0 2 4 6 8 10 12 14
3355 E2: 1 3 5 7 9 11 13 15
3356 E3: 16 18 20 22 24 26 28 30
3357 E4: 17 19 21 23 25 27 29 31
3359 In order to proceed and create the correct sequence for the next stage (or
3360 for the correct output, if the second stage is the last one, as in our
3361 example), we first put the output of extract_even operation and then the
3362 output of extract_odd in RESULT_CHAIN (which is then copied to DR_CHAIN).
3363 The input for the second stage is:
3365 1st vec (E1): 0 2 4 6 8 10 12 14
3366 2nd vec (E3): 16 18 20 22 24 26 28 30
3367 3rd vec (E2): 1 3 5 7 9 11 13 15
3368 4th vec (E4): 17 19 21 23 25 27 29 31
3370 The output of the second stage:
3372 E1: 0 4 8 12 16 20 24 28
3373 E2: 2 6 10 14 18 22 26 30
3374 E3: 1 5 9 13 17 21 25 29
3375 E4: 3 7 11 15 19 23 27 31
3377 And RESULT_CHAIN after reordering:
3379 1st vec (E1): 0 4 8 12 16 20 24 28
3380 2nd vec (E3): 1 5 9 13 17 21 25 29
3381 3rd vec (E2): 2 6 10 14 18 22 26 30
3382 4th vec (E4): 3 7 11 15 19 23 27 31. */
3385 vect_permute_load_chain (VEC(tree
,heap
) *dr_chain
,
3386 unsigned int length
,
3388 gimple_stmt_iterator
*gsi
,
3389 VEC(tree
,heap
) **result_chain
)
3391 tree perm_dest
, data_ref
, first_vect
, second_vect
;
3393 tree vectype
= STMT_VINFO_VECTYPE (vinfo_for_stmt (stmt
));
3397 /* Check that the operation is supported. */
3398 if (!vect_strided_load_supported (vectype
))
3401 *result_chain
= VEC_copy (tree
, heap
, dr_chain
);
3402 for (i
= 0; i
< exact_log2 (length
); i
++)
3404 for (j
= 0; j
< length
; j
+=2)
3406 first_vect
= VEC_index (tree
, dr_chain
, j
);
3407 second_vect
= VEC_index (tree
, dr_chain
, j
+1);
3409 /* data_ref = permute_even (first_data_ref, second_data_ref); */
3410 perm_dest
= create_tmp_var (vectype
, "vect_perm_even");
3411 DECL_GIMPLE_REG_P (perm_dest
) = 1;
3412 add_referenced_var (perm_dest
);
3414 perm_stmt
= gimple_build_assign_with_ops (VEC_EXTRACT_EVEN_EXPR
,
3415 perm_dest
, first_vect
,
3418 data_ref
= make_ssa_name (perm_dest
, perm_stmt
);
3419 gimple_assign_set_lhs (perm_stmt
, data_ref
);
3420 vect_finish_stmt_generation (stmt
, perm_stmt
, gsi
);
3421 mark_symbols_for_renaming (perm_stmt
);
3423 VEC_replace (tree
, *result_chain
, j
/2, data_ref
);
3425 /* data_ref = permute_odd (first_data_ref, second_data_ref); */
3426 perm_dest
= create_tmp_var (vectype
, "vect_perm_odd");
3427 DECL_GIMPLE_REG_P (perm_dest
) = 1;
3428 add_referenced_var (perm_dest
);
3430 perm_stmt
= gimple_build_assign_with_ops (VEC_EXTRACT_ODD_EXPR
,
3431 perm_dest
, first_vect
,
3433 data_ref
= make_ssa_name (perm_dest
, perm_stmt
);
3434 gimple_assign_set_lhs (perm_stmt
, data_ref
);
3435 vect_finish_stmt_generation (stmt
, perm_stmt
, gsi
);
3436 mark_symbols_for_renaming (perm_stmt
);
3438 VEC_replace (tree
, *result_chain
, j
/2+length
/2, data_ref
);
3440 dr_chain
= VEC_copy (tree
, heap
, *result_chain
);
3446 /* Function vect_transform_strided_load.
3448 Given a chain of input interleaved data-refs (in DR_CHAIN), build statements
3449 to perform their permutation and ascribe the result vectorized statements to
3450 the scalar statements.
3454 vect_transform_strided_load (gimple stmt
, VEC(tree
,heap
) *dr_chain
, int size
,
3455 gimple_stmt_iterator
*gsi
)
3457 stmt_vec_info stmt_info
= vinfo_for_stmt (stmt
);
3458 gimple first_stmt
= DR_GROUP_FIRST_DR (stmt_info
);
3459 gimple next_stmt
, new_stmt
;
3460 VEC(tree
,heap
) *result_chain
= NULL
;
3461 unsigned int i
, gap_count
;
3464 /* DR_CHAIN contains input data-refs that are a part of the interleaving.
3465 RESULT_CHAIN is the output of vect_permute_load_chain, it contains permuted
3466 vectors, that are ready for vector computation. */
3467 result_chain
= VEC_alloc (tree
, heap
, size
);
3469 if (!vect_permute_load_chain (dr_chain
, size
, stmt
, gsi
, &result_chain
))
3472 /* Put a permuted data-ref in the VECTORIZED_STMT field.
3473 Since we scan the chain starting from it's first node, their order
3474 corresponds the order of data-refs in RESULT_CHAIN. */
3475 next_stmt
= first_stmt
;
3477 for (i
= 0; VEC_iterate (tree
, result_chain
, i
, tmp_data_ref
); i
++)
3482 /* Skip the gaps. Loads created for the gaps will be removed by dead
3483 code elimination pass later. No need to check for the first stmt in
3484 the group, since it always exists.
3485 DR_GROUP_GAP is the number of steps in elements from the previous
3486 access (if there is no gap DR_GROUP_GAP is 1). We skip loads that
3487 correspond to the gaps.
3489 if (next_stmt
!= first_stmt
3490 && gap_count
< DR_GROUP_GAP (vinfo_for_stmt (next_stmt
)))
3498 new_stmt
= SSA_NAME_DEF_STMT (tmp_data_ref
);
3499 /* We assume that if VEC_STMT is not NULL, this is a case of multiple
3500 copies, and we put the new vector statement in the first available
3502 if (!STMT_VINFO_VEC_STMT (vinfo_for_stmt (next_stmt
)))
3503 STMT_VINFO_VEC_STMT (vinfo_for_stmt (next_stmt
)) = new_stmt
;
3506 if (!DR_GROUP_SAME_DR_STMT (vinfo_for_stmt (next_stmt
)))
3509 STMT_VINFO_VEC_STMT (vinfo_for_stmt (next_stmt
));
3511 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (prev_stmt
));
3514 prev_stmt
= rel_stmt
;
3516 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (rel_stmt
));
3519 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (prev_stmt
)) =
3524 next_stmt
= DR_GROUP_NEXT_DR (vinfo_for_stmt (next_stmt
));
3526 /* If NEXT_STMT accesses the same DR as the previous statement,
3527 put the same TMP_DATA_REF as its vectorized statement; otherwise
3528 get the next data-ref from RESULT_CHAIN. */
3529 if (!next_stmt
|| !DR_GROUP_SAME_DR_STMT (vinfo_for_stmt (next_stmt
)))
3534 VEC_free (tree
, heap
, result_chain
);
3538 /* Function vect_force_dr_alignment_p.
3540 Returns whether the alignment of a DECL can be forced to be aligned
3541 on ALIGNMENT bit boundary. */
3544 vect_can_force_dr_alignment_p (const_tree decl
, unsigned int alignment
)
3546 if (TREE_CODE (decl
) != VAR_DECL
)
3549 if (DECL_EXTERNAL (decl
))
3552 if (TREE_ASM_WRITTEN (decl
))
3555 if (TREE_STATIC (decl
))
3556 return (alignment
<= MAX_OFILE_ALIGNMENT
);
3558 return (alignment
<= MAX_STACK_ALIGNMENT
);
3561 /* Function vect_supportable_dr_alignment
3563 Return whether the data reference DR is supported with respect to its
3566 enum dr_alignment_support
3567 vect_supportable_dr_alignment (struct data_reference
*dr
)
3569 gimple stmt
= DR_STMT (dr
);
3570 stmt_vec_info stmt_info
= vinfo_for_stmt (stmt
);
3571 tree vectype
= STMT_VINFO_VECTYPE (stmt_info
);
3572 enum machine_mode mode
= TYPE_MODE (vectype
);
3573 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_info
);
3574 struct loop
*vect_loop
= NULL
;
3575 bool nested_in_vect_loop
= false;
3577 if (aligned_access_p (dr
))
3581 /* FORNOW: Misaligned accesses are supported only in loops. */
3582 return dr_unaligned_unsupported
;
3584 vect_loop
= LOOP_VINFO_LOOP (loop_vinfo
);
3585 nested_in_vect_loop
= nested_in_vect_loop_p (vect_loop
, stmt
);
3587 /* Possibly unaligned access. */
3589 /* We can choose between using the implicit realignment scheme (generating
3590 a misaligned_move stmt) and the explicit realignment scheme (generating
3591 aligned loads with a REALIGN_LOAD). There are two variants to the explicit
3592 realignment scheme: optimized, and unoptimized.
3593 We can optimize the realignment only if the step between consecutive
3594 vector loads is equal to the vector size. Since the vector memory
3595 accesses advance in steps of VS (Vector Size) in the vectorized loop, it
3596 is guaranteed that the misalignment amount remains the same throughout the
3597 execution of the vectorized loop. Therefore, we can create the
3598 "realignment token" (the permutation mask that is passed to REALIGN_LOAD)
3599 at the loop preheader.
3601 However, in the case of outer-loop vectorization, when vectorizing a
3602 memory access in the inner-loop nested within the LOOP that is now being
3603 vectorized, while it is guaranteed that the misalignment of the
3604 vectorized memory access will remain the same in different outer-loop
3605 iterations, it is *not* guaranteed that is will remain the same throughout
3606 the execution of the inner-loop. This is because the inner-loop advances
3607 with the original scalar step (and not in steps of VS). If the inner-loop
3608 step happens to be a multiple of VS, then the misalignment remains fixed
3609 and we can use the optimized realignment scheme. For example:
3615 When vectorizing the i-loop in the above example, the step between
3616 consecutive vector loads is 1, and so the misalignment does not remain
3617 fixed across the execution of the inner-loop, and the realignment cannot
3618 be optimized (as illustrated in the following pseudo vectorized loop):
3620 for (i=0; i<N; i+=4)
3621 for (j=0; j<M; j++){
3622 vs += vp[i+j]; // misalignment of &vp[i+j] is {0,1,2,3,0,1,2,3,...}
3623 // when j is {0,1,2,3,4,5,6,7,...} respectively.
3624 // (assuming that we start from an aligned address).
3627 We therefore have to use the unoptimized realignment scheme:
3629 for (i=0; i<N; i+=4)
3630 for (j=k; j<M; j+=4)
3631 vs += vp[i+j]; // misalignment of &vp[i+j] is always k (assuming
3632 // that the misalignment of the initial address is
3635 The loop can then be vectorized as follows:
3637 for (k=0; k<4; k++){
3638 rt = get_realignment_token (&vp[k]);
3639 for (i=0; i<N; i+=4){
3641 for (j=k; j<M; j+=4){
3643 va = REALIGN_LOAD <v1,v2,rt>;
3650 if (DR_IS_READ (dr
))
3652 bool is_packed
= false;
3653 tree type
= (TREE_TYPE (DR_REF (dr
)));
3655 if (optab_handler (vec_realign_load_optab
, mode
)->insn_code
!=
3657 && (!targetm
.vectorize
.builtin_mask_for_load
3658 || targetm
.vectorize
.builtin_mask_for_load ()))
3660 tree vectype
= STMT_VINFO_VECTYPE (stmt_info
);
3661 if (nested_in_vect_loop
3662 && (TREE_INT_CST_LOW (DR_STEP (dr
))
3663 != GET_MODE_SIZE (TYPE_MODE (vectype
))))
3664 return dr_explicit_realign
;
3666 return dr_explicit_realign_optimized
;
3668 if (!known_alignment_for_access_p (dr
))
3670 tree ba
= DR_BASE_OBJECT (dr
);
3673 is_packed
= contains_packed_reference (ba
);
3676 if (targetm
.vectorize
.
3677 support_vector_misalignment (mode
, type
,
3678 DR_MISALIGNMENT (dr
), is_packed
))
3679 /* Can't software pipeline the loads, but can at least do them. */
3680 return dr_unaligned_supported
;
3684 bool is_packed
= false;
3685 tree type
= (TREE_TYPE (DR_REF (dr
)));
3687 if (!known_alignment_for_access_p (dr
))
3689 tree ba
= DR_BASE_OBJECT (dr
);
3692 is_packed
= contains_packed_reference (ba
);
3695 if (targetm
.vectorize
.
3696 support_vector_misalignment (mode
, type
,
3697 DR_MISALIGNMENT (dr
), is_packed
))
3698 return dr_unaligned_supported
;
3702 return dr_unaligned_unsupported
;