1 /* Analysis Utilities for Loop Vectorization.
2 Copyright (C) 2003,2004,2005 Free Software Foundation, Inc.
3 Contributed by Dorit Naishlos <dorit@il.ibm.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 #include "coretypes.h"
29 #include "basic-block.h"
30 #include "diagnostic.h"
31 #include "tree-flow.h"
32 #include "tree-dump.h"
37 #include "tree-chrec.h"
38 #include "tree-data-ref.h"
39 #include "tree-scalar-evolution.h"
40 #include "tree-vectorizer.h"
42 /* Main analysis functions. */
43 static loop_vec_info vect_analyze_loop_form (struct loop *);
44 static bool vect_analyze_data_refs (loop_vec_info);
45 static bool vect_mark_stmts_to_be_vectorized (loop_vec_info);
46 static bool vect_analyze_scalar_cycles (loop_vec_info);
47 static bool vect_analyze_data_ref_accesses (loop_vec_info);
48 static bool vect_analyze_data_ref_dependences (loop_vec_info);
49 static bool vect_analyze_data_refs_alignment (loop_vec_info);
50 static bool vect_compute_data_refs_alignment (loop_vec_info);
51 static void vect_enhance_data_refs_alignment (loop_vec_info);
52 static bool vect_analyze_operations (loop_vec_info);
53 static bool vect_determine_vectorization_factor (loop_vec_info);
55 /* Utility functions for the analyses. */
56 static bool exist_non_indexing_operands_for_use_p (tree, tree);
57 static void vect_mark_relevant (varray_type *, tree);
58 static bool vect_stmt_relevant_p (tree, loop_vec_info);
59 static tree vect_get_loop_niters (struct loop *, tree *);
60 static bool vect_analyze_data_ref_dependence
61 (struct data_reference *, struct data_reference *, loop_vec_info);
62 static bool vect_compute_data_ref_alignment (struct data_reference *);
63 static bool vect_analyze_data_ref_access (struct data_reference *);
64 static struct data_reference * vect_analyze_pointer_ref_access
65 (tree, tree, bool, tree, tree *, tree *);
66 static bool vect_can_advance_ivs_p (loop_vec_info);
67 static tree vect_get_ptr_offset (tree, tree, tree *);
68 static bool vect_analyze_offset_expr (tree, struct loop *, tree, tree *,
70 static bool vect_base_addr_differ_p (struct data_reference *,
71 struct data_reference *drb, bool *);
72 static tree vect_object_analysis (tree, tree, bool, tree,
73 struct data_reference **, tree *, tree *,
74 tree *, bool *, tree *, subvar_t *);
75 static tree vect_address_analysis (tree, tree, bool, tree,
76 struct data_reference *, tree *, tree *,
80 /* Function vect_get_ptr_offset
82 Compute the OFFSET modulo vector-type alignment of pointer REF in bits. */
85 vect_get_ptr_offset (tree ref ATTRIBUTE_UNUSED,
86 tree vectype ATTRIBUTE_UNUSED,
87 tree *offset ATTRIBUTE_UNUSED)
89 /* TODO: Use alignment information. */
94 /* Function vect_analyze_offset_expr
96 Given an offset expression EXPR received from get_inner_reference, analyze
97 it and create an expression for INITIAL_OFFSET by substituting the variables
98 of EXPR with initial_condition of the corresponding access_fn in the loop.
101 for (j = 3; j < N; j++)
104 For a[j].b[i][j], EXPR will be 'i * C_i + j * C_j + C'. 'i' cannot be
105 substituted, since its access_fn in the inner loop is i. 'j' will be
106 substituted with 3. An INITIAL_OFFSET will be 'i * C_i + C`', where
109 Compute MISALIGN (the misalignment of the data reference initial access from
110 its base) if possible. Misalignment can be calculated only if all the
111 variables can be substituted with constants, or if a variable is multiplied
112 by a multiple of VECTYPE_ALIGNMENT. In the above example, since 'i' cannot
113 be substituted, MISALIGN will be NULL_TREE in case that C_i is not a multiple
114 of VECTYPE_ALIGNMENT, and C` otherwise. (We perform MISALIGN modulo
115 VECTYPE_ALIGNMENT computation in the caller of this function).
117 STEP is an evolution of the data reference in this loop in bytes.
118 In the above example, STEP is C_j.
120 Return FALSE, if the analysis fails, e.g., there is no access_fn for a
121 variable. In this case, all the outputs (INITIAL_OFFSET, MISALIGN and STEP)
122 are NULL_TREEs. Otherwise, return TRUE.
127 vect_analyze_offset_expr (tree expr,
129 tree vectype_alignment,
130 tree *initial_offset,
136 tree left_offset = ssize_int (0);
137 tree right_offset = ssize_int (0);
138 tree left_misalign = ssize_int (0);
139 tree right_misalign = ssize_int (0);
140 tree left_step = ssize_int (0);
141 tree right_step = ssize_int (0);
143 tree init, evolution;
146 *misalign = NULL_TREE;
147 *initial_offset = NULL_TREE;
149 /* Strip conversions that don't narrow the mode. */
150 expr = vect_strip_conversion (expr);
156 if (TREE_CODE (expr) == INTEGER_CST)
158 *initial_offset = fold_convert (ssizetype, expr);
159 *misalign = fold_convert (ssizetype, expr);
160 *step = ssize_int (0);
164 /* 2. Variable. Try to substitute with initial_condition of the corresponding
165 access_fn in the current loop. */
166 if (SSA_VAR_P (expr))
168 tree access_fn = analyze_scalar_evolution (loop, expr);
170 if (access_fn == chrec_dont_know)
174 init = initial_condition_in_loop_num (access_fn, loop->num);
175 if (init == expr && !expr_invariant_in_loop_p (loop, init))
176 /* Not enough information: may be not loop invariant.
177 E.g., for a[b[i]], we get a[D], where D=b[i]. EXPR is D, its
178 initial_condition is D, but it depends on i - loop's induction
182 evolution = evolution_part_in_loop_num (access_fn, loop->num);
183 if (evolution && TREE_CODE (evolution) != INTEGER_CST)
184 /* Evolution is not constant. */
187 if (TREE_CODE (init) == INTEGER_CST)
188 *misalign = fold_convert (ssizetype, init);
190 /* Not constant, misalignment cannot be calculated. */
191 *misalign = NULL_TREE;
193 *initial_offset = fold_convert (ssizetype, init);
195 *step = evolution ? fold_convert (ssizetype, evolution) : ssize_int (0);
199 /* Recursive computation. */
200 if (!BINARY_CLASS_P (expr))
202 /* We expect to get binary expressions (PLUS/MINUS and MULT). */
203 if (vect_print_dump_info (REPORT_DETAILS, UNKNOWN_LOC))
205 fprintf (vect_dump, "Not binary expression ");
206 print_generic_expr (vect_dump, expr, TDF_SLIM);
210 oprnd0 = TREE_OPERAND (expr, 0);
211 oprnd1 = TREE_OPERAND (expr, 1);
213 if (!vect_analyze_offset_expr (oprnd0, loop, vectype_alignment, &left_offset,
214 &left_misalign, &left_step)
215 || !vect_analyze_offset_expr (oprnd1, loop, vectype_alignment,
216 &right_offset, &right_misalign, &right_step))
219 /* The type of the operation: plus, minus or mult. */
220 code = TREE_CODE (expr);
224 if (TREE_CODE (right_offset) != INTEGER_CST)
225 /* RIGHT_OFFSET can be not constant. For example, for arrays of variable
227 FORNOW: We don't support such cases. */
230 /* Strip conversions that don't narrow the mode. */
231 left_offset = vect_strip_conversion (left_offset);
234 /* Misalignment computation. */
235 if (SSA_VAR_P (left_offset))
237 /* If the left side contains variables that can't be substituted with
238 constants, we check if the right side is a multiple of ALIGNMENT.
240 if (integer_zerop (size_binop (TRUNC_MOD_EXPR, right_offset,
241 fold_convert (ssizetype, vectype_alignment))))
242 *misalign = ssize_int (0);
244 /* If the remainder is not zero or the right side isn't constant,
245 we can't compute misalignment. */
246 *misalign = NULL_TREE;
250 /* The left operand was successfully substituted with constant. */
252 /* In case of EXPR '(i * C1 + j) * C2', LEFT_MISALIGN is
254 *misalign = size_binop (code, left_misalign, right_misalign);
256 *misalign = NULL_TREE;
259 /* Step calculation. */
260 /* Multiply the step by the right operand. */
261 *step = size_binop (MULT_EXPR, left_step, right_offset);
266 /* Combine the recursive calculations for step and misalignment. */
267 *step = size_binop (code, left_step, right_step);
269 if (left_misalign && right_misalign)
270 *misalign = size_binop (code, left_misalign, right_misalign);
272 *misalign = NULL_TREE;
280 /* Compute offset. */
281 *initial_offset = fold_convert (ssizetype,
282 fold (build2 (code, TREE_TYPE (left_offset),
289 /* Function vect_determine_vectorization_factor
291 Determine the vectorization factor (VF). VF is the number of data elements
292 that are operated upon in parallel in a single iteration of the vectorized
293 loop. For example, when vectorizing a loop that operates on 4byte elements,
294 on a target with vector size (VS) 16byte, the VF is set to 4, since 4
295 elements can fit in a single vector register.
297 We currently support vectorization of loops in which all types operated upon
298 are of the same size. Therefore this function currently sets VF according to
299 the size of the types operated upon, and fails if there are multiple sizes
302 VF is also the factor by which the loop iterations are strip-mined, e.g.:
309 for (i=0; i<N; i+=VF){
310 a[i:VF] = b[i:VF] + c[i:VF];
315 vect_determine_vectorization_factor (loop_vec_info loop_vinfo)
317 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
318 basic_block *bbs = LOOP_VINFO_BBS (loop_vinfo);
319 int nbbs = loop->num_nodes;
320 block_stmt_iterator si;
321 unsigned int vectorization_factor = 0;
325 if (vect_print_dump_info (REPORT_DETAILS, UNKNOWN_LOC))
326 fprintf (vect_dump, "=== vect_determine_vectorization_factor ===");
328 for (i = 0; i < nbbs; i++)
330 basic_block bb = bbs[i];
332 for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
334 tree stmt = bsi_stmt (si);
336 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
339 if (vect_print_dump_info (REPORT_DETAILS, UNKNOWN_LOC))
341 fprintf (vect_dump, "==> examining statement: ");
342 print_generic_expr (vect_dump, stmt, TDF_SLIM);
345 gcc_assert (stmt_info);
346 /* skip stmts which do not need to be vectorized. */
347 if (!STMT_VINFO_RELEVANT_P (stmt_info))
350 if (VECTOR_MODE_P (TYPE_MODE (TREE_TYPE (stmt))))
352 if (vect_print_dump_info (REPORT_UNVECTORIZED_LOOPS,
353 LOOP_LOC (loop_vinfo)))
355 fprintf (vect_dump, "not vectorized: vector stmt in loop:");
356 print_generic_expr (vect_dump, stmt, TDF_SLIM);
361 if (STMT_VINFO_DATA_REF (stmt_info))
362 scalar_type = TREE_TYPE (DR_REF (STMT_VINFO_DATA_REF (stmt_info)));
363 else if (TREE_CODE (stmt) == MODIFY_EXPR)
364 scalar_type = TREE_TYPE (TREE_OPERAND (stmt, 0));
366 scalar_type = TREE_TYPE (stmt);
368 if (vect_print_dump_info (REPORT_DETAILS, UNKNOWN_LOC))
370 fprintf (vect_dump, "get vectype for scalar type: ");
371 print_generic_expr (vect_dump, scalar_type, TDF_SLIM);
374 vectype = get_vectype_for_scalar_type (scalar_type);
377 if (vect_print_dump_info (REPORT_UNVECTORIZED_LOOPS,
378 LOOP_LOC (loop_vinfo)))
380 fprintf (vect_dump, "not vectorized: unsupported data-type ");
381 print_generic_expr (vect_dump, scalar_type, TDF_SLIM);
385 if (vect_print_dump_info (REPORT_DETAILS, UNKNOWN_LOC))
387 fprintf (vect_dump, "vectype: ");
388 print_generic_expr (vect_dump, vectype, TDF_SLIM);
390 STMT_VINFO_VECTYPE (stmt_info) = vectype;
392 nunits = GET_MODE_NUNITS (TYPE_MODE (vectype));
393 if (vect_print_dump_info (REPORT_DETAILS, UNKNOWN_LOC))
394 fprintf (vect_dump, "nunits = %d", nunits);
396 if (vectorization_factor)
398 /* FORNOW: don't allow mixed units.
399 This restriction will be relaxed in the future. */
400 if (nunits != vectorization_factor)
402 if (vect_print_dump_info (REPORT_UNVECTORIZED_LOOPS,
403 LOOP_LOC (loop_vinfo)))
404 fprintf (vect_dump, "not vectorized: mixed data-types");
409 vectorization_factor = nunits;
411 #ifdef ENABLE_CHECKING
412 gcc_assert (GET_MODE_SIZE (TYPE_MODE (scalar_type))
413 * vectorization_factor == UNITS_PER_SIMD_WORD);
418 /* TODO: Analyze cost. Decide if worth while to vectorize. */
420 if (vectorization_factor <= 1)
422 if (vect_print_dump_info (REPORT_UNVECTORIZED_LOOPS,
423 LOOP_LOC (loop_vinfo)))
424 fprintf (vect_dump, "not vectorized: unsupported data-type");
427 LOOP_VINFO_VECT_FACTOR (loop_vinfo) = vectorization_factor;
433 /* Function vect_analyze_operations.
435 Scan the loop stmts and make sure they are all vectorizable. */
438 vect_analyze_operations (loop_vec_info loop_vinfo)
440 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
441 basic_block *bbs = LOOP_VINFO_BBS (loop_vinfo);
442 int nbbs = loop->num_nodes;
443 block_stmt_iterator si;
444 unsigned int vectorization_factor = 0;
448 if (vect_print_dump_info (REPORT_DETAILS, UNKNOWN_LOC))
449 fprintf (vect_dump, "=== vect_analyze_operations ===");
451 gcc_assert (LOOP_VINFO_VECT_FACTOR (loop_vinfo));
452 vectorization_factor = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
454 for (i = 0; i < nbbs; i++)
456 basic_block bb = bbs[i];
458 for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
460 tree stmt = bsi_stmt (si);
461 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
463 if (vect_print_dump_info (REPORT_DETAILS, UNKNOWN_LOC))
465 fprintf (vect_dump, "==> examining statement: ");
466 print_generic_expr (vect_dump, stmt, TDF_SLIM);
469 gcc_assert (stmt_info);
471 /* skip stmts which do not need to be vectorized.
472 this is expected to include:
473 - the COND_EXPR which is the loop exit condition
474 - any LABEL_EXPRs in the loop
475 - computations that are used only for array indexing or loop
478 if (!STMT_VINFO_RELEVANT_P (stmt_info))
480 if (vect_print_dump_info (REPORT_DETAILS, UNKNOWN_LOC))
481 fprintf (vect_dump, "irrelevant.");
485 #ifdef ENABLE_CHECKING
486 if (STMT_VINFO_RELEVANT_P (stmt_info))
488 gcc_assert (!VECTOR_MODE_P (TYPE_MODE (TREE_TYPE (stmt))));
489 gcc_assert (STMT_VINFO_VECTYPE (stmt_info));
493 ok = (vectorizable_operation (stmt, NULL, NULL)
494 || vectorizable_assignment (stmt, NULL, NULL)
495 || vectorizable_load (stmt, NULL, NULL)
496 || vectorizable_store (stmt, NULL, NULL));
500 if (vect_print_dump_info (REPORT_UNVECTORIZED_LOOPS,
501 LOOP_LOC (loop_vinfo)))
503 fprintf (vect_dump, "not vectorized: stmt not supported: ");
504 print_generic_expr (vect_dump, stmt, TDF_SLIM);
511 /* TODO: Analyze cost. Decide if worth while to vectorize. */
513 if (LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo)
514 && vect_print_dump_info (REPORT_DETAILS, UNKNOWN_LOC))
516 "vectorization_factor = %d, niters = " HOST_WIDE_INT_PRINT_DEC,
517 vectorization_factor, LOOP_VINFO_INT_NITERS (loop_vinfo));
519 if (LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo)
520 && LOOP_VINFO_INT_NITERS (loop_vinfo) < vectorization_factor)
522 if (vect_print_dump_info (REPORT_UNVECTORIZED_LOOPS,
523 LOOP_LOC (loop_vinfo)))
524 fprintf (vect_dump, "not vectorized: iteration count too small.");
528 if (!LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo)
529 || LOOP_VINFO_INT_NITERS (loop_vinfo) % vectorization_factor != 0)
531 if (vect_print_dump_info (REPORT_DETAILS, LOOP_LOC (loop_vinfo)))
532 fprintf (vect_dump, "epilog loop required.");
533 if (!vect_can_advance_ivs_p (loop_vinfo))
535 if (vect_print_dump_info (REPORT_UNVECTORIZED_LOOPS,
536 LOOP_LOC (loop_vinfo)))
538 "not vectorized: can't create epilog loop 1.");
541 if (!slpeel_can_duplicate_loop_p (loop, loop->single_exit))
543 if (vect_print_dump_info (REPORT_UNVECTORIZED_LOOPS,
544 LOOP_LOC (loop_vinfo)))
546 "not vectorized: can't create epilog loop 2.");
555 /* Function exist_non_indexing_operands_for_use_p
557 USE is one of the uses attached to STMT. Check if USE is
558 used in STMT for anything other than indexing an array. */
561 exist_non_indexing_operands_for_use_p (tree use, tree stmt)
564 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
566 /* USE corresponds to some operand in STMT. If there is no data
567 reference in STMT, then any operand that corresponds to USE
568 is not indexing an array. */
569 if (!STMT_VINFO_DATA_REF (stmt_info))
572 /* STMT has a data_ref. FORNOW this means that its of one of
576 (This should have been verified in analyze_data_refs).
578 'var' in the second case corresponds to a def, not a use,
579 so USE cannot correspond to any operands that are not used
582 Therefore, all we need to check is if STMT falls into the
583 first case, and whether var corresponds to USE. */
585 if (TREE_CODE (TREE_OPERAND (stmt, 0)) == SSA_NAME)
588 operand = TREE_OPERAND (stmt, 1);
590 if (TREE_CODE (operand) != SSA_NAME)
600 /* Function vect_analyze_scalar_cycles.
602 Examine the cross iteration def-use cycles of scalar variables, by
603 analyzing the loop (scalar) PHIs; verify that the cross iteration def-use
604 cycles that they represent do not impede vectorization.
606 FORNOW: Reduction as in the following loop, is not supported yet:
610 The cross-iteration cycle corresponding to variable 'sum' will be
611 considered too complicated and will impede vectorization.
613 FORNOW: Induction as in the following loop, is not supported yet:
618 However, the following loop *is* vectorizable:
623 In both loops there exists a def-use cycle for the variable i:
624 loop: i_2 = PHI (i_0, i_1)
629 The evolution of the above cycle is considered simple enough,
630 however, we also check that the cycle does not need to be
631 vectorized, i.e - we check that the variable that this cycle
632 defines is only used for array indexing or in stmts that do not
633 need to be vectorized. This is not the case in loop2, but it
634 *is* the case in loop3. */
637 vect_analyze_scalar_cycles (loop_vec_info loop_vinfo)
640 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
641 basic_block bb = loop->header;
644 if (vect_print_dump_info (REPORT_DETAILS, UNKNOWN_LOC))
645 fprintf (vect_dump, "=== vect_analyze_scalar_cycles ===");
647 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
649 tree access_fn = NULL;
651 if (vect_print_dump_info (REPORT_DETAILS, UNKNOWN_LOC))
653 fprintf (vect_dump, "Analyze phi: ");
654 print_generic_expr (vect_dump, phi, TDF_SLIM);
657 /* Skip virtual phi's. The data dependences that are associated with
658 virtual defs/uses (i.e., memory accesses) are analyzed elsewhere. */
660 if (!is_gimple_reg (SSA_NAME_VAR (PHI_RESULT (phi))))
662 if (vect_print_dump_info (REPORT_DETAILS, UNKNOWN_LOC))
663 fprintf (vect_dump, "virtual phi. skip.");
667 /* Analyze the evolution function. */
669 /* FORNOW: The only scalar cross-iteration cycles that we allow are
670 those of loop induction variables; This property is verified here.
672 Furthermore, if that induction variable is used in an operation
673 that needs to be vectorized (i.e, is not solely used to index
674 arrays and check the exit condition) - we do not support its
675 vectorization yet. This property is verified in vect_is_simple_use,
676 during vect_analyze_operations. */
678 access_fn = /* instantiate_parameters
680 analyze_scalar_evolution (loop, PHI_RESULT (phi));
684 if (vect_print_dump_info (REPORT_UNVECTORIZED_LOOPS,
685 LOOP_LOC (loop_vinfo)))
686 fprintf (vect_dump, "not vectorized: unsupported scalar cycle.");
690 if (vect_print_dump_info (REPORT_DETAILS,
691 LOOP_LOC (loop_vinfo)))
693 fprintf (vect_dump, "Access function of PHI: ");
694 print_generic_expr (vect_dump, access_fn, TDF_SLIM);
697 if (!vect_is_simple_iv_evolution (loop->num, access_fn, &dummy, &dummy))
699 if (vect_print_dump_info (REPORT_UNVECTORIZED_LOOPS,
700 LOOP_LOC (loop_vinfo)))
701 fprintf (vect_dump, "not vectorized: unsupported scalar cycle.");
710 /* Function vect_base_addr_differ_p.
712 This is the simplest data dependence test: determines whether the
713 data references A and B access the same array/region. Returns
714 false when the property is not computable at compile time.
715 Otherwise return true, and DIFFER_P will record the result. This
716 utility will not be necessary when alias_sets_conflict_p will be
717 less conservative. */
720 vect_base_addr_differ_p (struct data_reference *dra,
721 struct data_reference *drb,
724 tree stmt_a = DR_STMT (dra);
725 stmt_vec_info stmt_info_a = vinfo_for_stmt (stmt_a);
726 tree stmt_b = DR_STMT (drb);
727 stmt_vec_info stmt_info_b = vinfo_for_stmt (stmt_b);
728 tree addr_a = STMT_VINFO_VECT_DR_BASE_ADDRESS (stmt_info_a);
729 tree addr_b = STMT_VINFO_VECT_DR_BASE_ADDRESS (stmt_info_b);
730 tree type_a = TREE_TYPE (addr_a);
731 tree type_b = TREE_TYPE (addr_b);
732 HOST_WIDE_INT alias_set_a, alias_set_b;
734 gcc_assert (POINTER_TYPE_P (type_a) && POINTER_TYPE_P (type_b));
736 /* Both references are ADDR_EXPR, i.e., we have the objects. */
737 if (TREE_CODE (addr_a) == ADDR_EXPR && TREE_CODE (addr_b) == ADDR_EXPR)
738 return array_base_name_differ_p (dra, drb, differ_p);
740 alias_set_a = (TREE_CODE (addr_a) == ADDR_EXPR) ?
741 get_alias_set (TREE_OPERAND (addr_a, 0)) : get_alias_set (addr_a);
742 alias_set_b = (TREE_CODE (addr_b) == ADDR_EXPR) ?
743 get_alias_set (TREE_OPERAND (addr_b, 0)) : get_alias_set (addr_b);
745 if (!alias_sets_conflict_p (alias_set_a, alias_set_b))
751 /* An instruction writing through a restricted pointer is "independent" of any
752 instruction reading or writing through a different pointer, in the same
754 else if ((TYPE_RESTRICT (type_a) && !DR_IS_READ (dra))
755 || (TYPE_RESTRICT (type_b) && !DR_IS_READ (drb)))
764 /* Function vect_analyze_data_ref_dependence.
766 Return TRUE if there (might) exist a dependence between a memory-reference
767 DRA and a memory-reference DRB. */
770 vect_analyze_data_ref_dependence (struct data_reference *dra,
771 struct data_reference *drb,
772 loop_vec_info loop_vinfo)
775 struct data_dependence_relation *ddr;
777 if (!vect_base_addr_differ_p (dra, drb, &differ_p))
779 if (vect_print_dump_info (REPORT_UNVECTORIZED_LOOPS,
780 LOOP_LOC (loop_vinfo)))
783 "not vectorized: can't determine dependence between: ");
784 print_generic_expr (vect_dump, DR_REF (dra), TDF_SLIM);
785 fprintf (vect_dump, " and ");
786 print_generic_expr (vect_dump, DR_REF (drb), TDF_SLIM);
794 ddr = initialize_data_dependence_relation (dra, drb);
795 compute_affine_dependence (ddr);
797 if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
800 if (vect_print_dump_info (REPORT_UNVECTORIZED_LOOPS,
801 LOOP_LOC (loop_vinfo)))
804 "not vectorized: possible dependence between data-refs ");
805 print_generic_expr (vect_dump, DR_REF (dra), TDF_SLIM);
806 fprintf (vect_dump, " and ");
807 print_generic_expr (vect_dump, DR_REF (drb), TDF_SLIM);
814 /* Function vect_analyze_data_ref_dependences.
816 Examine all the data references in the loop, and make sure there do not
817 exist any data dependences between them.
819 TODO: dependences which distance is greater than the vectorization factor
823 vect_analyze_data_ref_dependences (loop_vec_info loop_vinfo)
826 varray_type loop_write_refs = LOOP_VINFO_DATAREF_WRITES (loop_vinfo);
827 varray_type loop_read_refs = LOOP_VINFO_DATAREF_READS (loop_vinfo);
829 /* Examine store-store (output) dependences. */
831 if (vect_print_dump_info (REPORT_DETAILS, UNKNOWN_LOC))
832 fprintf (vect_dump, "=== vect_analyze_dependences ===");
834 if (vect_print_dump_info (REPORT_DETAILS, UNKNOWN_LOC))
835 fprintf (vect_dump, "compare all store-store pairs.");
837 for (i = 0; i < VARRAY_ACTIVE_SIZE (loop_write_refs); i++)
839 for (j = i + 1; j < VARRAY_ACTIVE_SIZE (loop_write_refs); j++)
841 struct data_reference *dra =
842 VARRAY_GENERIC_PTR (loop_write_refs, i);
843 struct data_reference *drb =
844 VARRAY_GENERIC_PTR (loop_write_refs, j);
845 if (vect_analyze_data_ref_dependence (dra, drb, loop_vinfo))
850 /* Examine load-store (true/anti) dependences. */
852 if (vect_print_dump_info (REPORT_DETAILS, UNKNOWN_LOC))
853 fprintf (vect_dump, "compare all load-store pairs.");
855 for (i = 0; i < VARRAY_ACTIVE_SIZE (loop_read_refs); i++)
857 for (j = 0; j < VARRAY_ACTIVE_SIZE (loop_write_refs); j++)
859 struct data_reference *dra = VARRAY_GENERIC_PTR (loop_read_refs, i);
860 struct data_reference *drb =
861 VARRAY_GENERIC_PTR (loop_write_refs, j);
862 if (vect_analyze_data_ref_dependence (dra, drb, loop_vinfo))
871 /* Function vect_compute_data_ref_alignment
873 Compute the misalignment of the data reference DR.
876 1. If during the misalignment computation it is found that the data reference
877 cannot be vectorized then false is returned.
878 2. DR_MISALIGNMENT (DR) is defined.
880 FOR NOW: No analysis is actually performed. Misalignment is calculated
881 only for trivial cases. TODO. */
884 vect_compute_data_ref_alignment (struct data_reference *dr)
886 tree stmt = DR_STMT (dr);
887 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
888 tree ref = DR_REF (dr);
890 tree base, alignment;
894 if (vect_print_dump_info (REPORT_DETAILS, UNKNOWN_LOC))
895 fprintf (vect_dump, "vect_compute_data_ref_alignment:");
897 /* Initialize misalignment to unknown. */
898 DR_MISALIGNMENT (dr) = -1;
900 misalign = STMT_VINFO_VECT_MISALIGNMENT (stmt_info);
901 base_aligned_p = STMT_VINFO_VECT_BASE_ALIGNED_P (stmt_info);
902 base = build_fold_indirect_ref (STMT_VINFO_VECT_DR_BASE_ADDRESS (stmt_info));
903 vectype = STMT_VINFO_VECTYPE (stmt_info);
907 if (vect_print_dump_info (REPORT_DETAILS, UNKNOWN_LOC))
909 fprintf (vect_dump, "Unknown alignment for access: ");
910 print_generic_expr (vect_dump, base, TDF_SLIM);
917 if (!vect_can_force_dr_alignment_p (base, TYPE_ALIGN (vectype)))
919 if (vect_print_dump_info (REPORT_DETAILS, UNKNOWN_LOC))
921 fprintf (vect_dump, "can't force alignment of ref: ");
922 print_generic_expr (vect_dump, ref, TDF_SLIM);
927 /* Force the alignment of the decl.
928 NOTE: This is the only change to the code we make during
929 the analysis phase, before deciding to vectorize the loop. */
930 if (vect_print_dump_info (REPORT_DETAILS, UNKNOWN_LOC))
931 fprintf (vect_dump, "force alignment");
932 DECL_ALIGN (base) = TYPE_ALIGN (vectype);
933 DECL_USER_ALIGN (base) = 1;
936 /* At this point we assume that the base is aligned. */
937 gcc_assert (base_aligned_p
938 || (TREE_CODE (base) == VAR_DECL
939 && DECL_ALIGN (base) >= TYPE_ALIGN (vectype)));
941 /* Alignment required, in bytes: */
942 alignment = ssize_int (TYPE_ALIGN (vectype)/BITS_PER_UNIT);
944 /* Modulo alignment. */
945 misalign = size_binop (TRUNC_MOD_EXPR, misalign, alignment);
946 if (tree_int_cst_sgn (misalign) < 0)
948 /* Negative misalignment value. */
949 if (vect_print_dump_info (REPORT_DETAILS, UNKNOWN_LOC))
950 fprintf (vect_dump, "unexpected misalign value");
954 DR_MISALIGNMENT (dr) = tree_low_cst (misalign, 1);
956 if (vect_print_dump_info (REPORT_DETAILS, UNKNOWN_LOC))
957 fprintf (vect_dump, "misalign = %d bytes", DR_MISALIGNMENT (dr));
963 /* Function vect_compute_data_refs_alignment
965 Compute the misalignment of data references in the loop.
966 This pass may take place at function granularity instead of at loop
969 FOR NOW: No analysis is actually performed. Misalignment is calculated
970 only for trivial cases. TODO. */
973 vect_compute_data_refs_alignment (loop_vec_info loop_vinfo)
975 varray_type loop_write_datarefs = LOOP_VINFO_DATAREF_WRITES (loop_vinfo);
976 varray_type loop_read_datarefs = LOOP_VINFO_DATAREF_READS (loop_vinfo);
979 for (i = 0; i < VARRAY_ACTIVE_SIZE (loop_write_datarefs); i++)
981 struct data_reference *dr = VARRAY_GENERIC_PTR (loop_write_datarefs, i);
982 if (!vect_compute_data_ref_alignment (dr))
986 for (i = 0; i < VARRAY_ACTIVE_SIZE (loop_read_datarefs); i++)
988 struct data_reference *dr = VARRAY_GENERIC_PTR (loop_read_datarefs, i);
989 if (!vect_compute_data_ref_alignment (dr))
997 /* Function vect_enhance_data_refs_alignment
999 This pass will use loop versioning and loop peeling in order to enhance
1000 the alignment of data references in the loop.
1002 FOR NOW: we assume that whatever versioning/peeling takes place, only the
1003 original loop is to be vectorized; Any other loops that are created by
1004 the transformations performed in this pass - are not supposed to be
1005 vectorized. This restriction will be relaxed. */
1008 vect_enhance_data_refs_alignment (loop_vec_info loop_vinfo)
1010 varray_type loop_read_datarefs = LOOP_VINFO_DATAREF_READS (loop_vinfo);
1011 varray_type loop_write_datarefs = LOOP_VINFO_DATAREF_WRITES (loop_vinfo);
1012 varray_type datarefs;
1013 struct data_reference *dr0 = NULL;
1017 This pass will require a cost model to guide it whether to apply peeling
1018 or versioning or a combination of the two. For example, the scheme that
1019 intel uses when given a loop with several memory accesses, is as follows:
1020 choose one memory access ('p') which alignment you want to force by doing
1021 peeling. Then, either (1) generate a loop in which 'p' is aligned and all
1022 other accesses are not necessarily aligned, or (2) use loop versioning to
1023 generate one loop in which all accesses are aligned, and another loop in
1024 which only 'p' is necessarily aligned.
1026 ("Automatic Intra-Register Vectorization for the Intel Architecture",
1027 Aart J.C. Bik, Milind Girkar, Paul M. Grey and Ximmin Tian, International
1028 Journal of Parallel Programming, Vol. 30, No. 2, April 2002.)
1030 Devising a cost model is the most critical aspect of this work. It will
1031 guide us on which access to peel for, whether to use loop versioning, how
1032 many versions to create, etc. The cost model will probably consist of
1033 generic considerations as well as target specific considerations (on
1034 powerpc for example, misaligned stores are more painful than misaligned
1037 Here is the general steps involved in alignment enhancements:
1039 -- original loop, before alignment analysis:
1040 for (i=0; i<N; i++){
1041 x = q[i]; # DR_MISALIGNMENT(q) = unknown
1042 p[i] = y; # DR_MISALIGNMENT(p) = unknown
1045 -- After vect_compute_data_refs_alignment:
1046 for (i=0; i<N; i++){
1047 x = q[i]; # DR_MISALIGNMENT(q) = 3
1048 p[i] = y; # DR_MISALIGNMENT(p) = unknown
1051 -- Possibility 1: we do loop versioning:
1053 for (i=0; i<N; i++){ # loop 1A
1054 x = q[i]; # DR_MISALIGNMENT(q) = 3
1055 p[i] = y; # DR_MISALIGNMENT(p) = 0
1059 for (i=0; i<N; i++){ # loop 1B
1060 x = q[i]; # DR_MISALIGNMENT(q) = 3
1061 p[i] = y; # DR_MISALIGNMENT(p) = unaligned
1065 -- Possibility 2: we do loop peeling:
1066 for (i = 0; i < 3; i++){ # (scalar loop, not to be vectorized).
1070 for (i = 3; i < N; i++){ # loop 2A
1071 x = q[i]; # DR_MISALIGNMENT(q) = 0
1072 p[i] = y; # DR_MISALIGNMENT(p) = unknown
1075 -- Possibility 3: combination of loop peeling and versioning:
1076 for (i = 0; i < 3; i++){ # (scalar loop, not to be vectorized).
1081 for (i = 3; i<N; i++){ # loop 3A
1082 x = q[i]; # DR_MISALIGNMENT(q) = 0
1083 p[i] = y; # DR_MISALIGNMENT(p) = 0
1087 for (i = 3; i<N; i++){ # loop 3B
1088 x = q[i]; # DR_MISALIGNMENT(q) = 0
1089 p[i] = y; # DR_MISALIGNMENT(p) = unaligned
1093 These loops are later passed to loop_transform to be vectorized. The
1094 vectorizer will use the alignment information to guide the transformation
1095 (whether to generate regular loads/stores, or with special handling for
1099 /* (1) Peeling to force alignment. */
1101 /* (1.1) Decide whether to perform peeling, and how many iterations to peel:
1103 + How many accesses will become aligned due to the peeling
1104 - How many accesses will become unaligned due to the peeling,
1105 and the cost of misaligned accesses.
1106 - The cost of peeling (the extra runtime checks, the increase
1109 The scheme we use FORNOW: peel to force the alignment of the first
1110 misaligned store in the loop.
1111 Rationale: misaligned stores are not yet supported.
1113 TODO: Use a better cost model. */
1115 for (i = 0; i < VARRAY_ACTIVE_SIZE (loop_write_datarefs); i++)
1117 dr0 = VARRAY_GENERIC_PTR (loop_write_datarefs, i);
1118 if (!aligned_access_p (dr0))
1120 LOOP_VINFO_UNALIGNED_DR (loop_vinfo) = dr0;
1121 LOOP_PEELING_FOR_ALIGNMENT (loop_vinfo) = DR_MISALIGNMENT (dr0);
1126 /* (1.2) Update the alignment info according to the peeling factor.
1127 If the misalignment of the DR we peel for is M, then the
1128 peeling factor is VF - M, and the misalignment of each access DR_i
1129 in the loop is DR_MISALIGNMENT (DR_i) + VF - M.
1130 If the misalignment of the DR we peel for is unknown, then the
1131 misalignment of each access DR_i in the loop is also unknown.
1133 TODO: - consider accesses that are known to have the same
1134 alignment, even if that alignment is unknown. */
1136 if (LOOP_PEELING_FOR_ALIGNMENT (loop_vinfo))
1141 if (known_alignment_for_access_p (dr0))
1143 /* Since it's known at compile time, compute the number of iterations
1144 in the peeled loop (the peeling factor) for use in updating
1145 DR_MISALIGNMENT values. The peeling factor is the vectorization
1146 factor minus the misalignment as an element count. */
1147 mis = DR_MISALIGNMENT (dr0);
1148 mis /= GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (DR_REF (dr0))));
1149 npeel = LOOP_VINFO_VECT_FACTOR (loop_vinfo) - mis;
1152 datarefs = loop_write_datarefs;
1153 for (j = 0; j < 2; j++)
1155 for (i = 0; i < VARRAY_ACTIVE_SIZE (datarefs); i++)
1157 struct data_reference *dr = VARRAY_GENERIC_PTR (datarefs, i);
1161 if (known_alignment_for_access_p (dr)
1162 && DR_MISALIGNMENT (dr) == DR_MISALIGNMENT (dr0))
1163 DR_MISALIGNMENT (dr) = 0;
1164 else if (known_alignment_for_access_p (dr)
1165 && known_alignment_for_access_p (dr0))
1167 int drsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (DR_REF (dr))));
1169 DR_MISALIGNMENT (dr) += npeel * drsize;
1170 DR_MISALIGNMENT (dr) %= UNITS_PER_SIMD_WORD;
1173 DR_MISALIGNMENT (dr) = -1;
1175 datarefs = loop_read_datarefs;
1178 DR_MISALIGNMENT (dr0) = 0;
1179 if (vect_print_dump_info (REPORT_ALIGNMENT, LOOP_LOC (loop_vinfo)))
1180 fprintf (vect_dump, "Alignment of access forced using peeling.");
1185 /* Function vect_analyze_data_refs_alignment
1187 Analyze the alignment of the data-references in the loop.
1188 FOR NOW: Until support for misliagned accesses is in place, only if all
1189 accesses are aligned can the loop be vectorized. This restriction will be
1193 vect_analyze_data_refs_alignment (loop_vec_info loop_vinfo)
1195 varray_type loop_read_datarefs = LOOP_VINFO_DATAREF_READS (loop_vinfo);
1196 varray_type loop_write_datarefs = LOOP_VINFO_DATAREF_WRITES (loop_vinfo);
1197 enum dr_alignment_support supportable_dr_alignment;
1200 if (vect_print_dump_info (REPORT_DETAILS, UNKNOWN_LOC))
1201 fprintf (vect_dump, "=== vect_analyze_data_refs_alignment ===");
1204 /* This pass may take place at function granularity instead of at loop
1207 if (!vect_compute_data_refs_alignment (loop_vinfo))
1209 if (vect_print_dump_info (REPORT_UNVECTORIZED_LOOPS,
1210 LOOP_LOC (loop_vinfo)))
1212 "not vectorized: can't calculate alignment for data ref.");
1217 /* This pass will decide on using loop versioning and/or loop peeling in
1218 order to enhance the alignment of data references in the loop. */
1220 vect_enhance_data_refs_alignment (loop_vinfo);
1223 /* Finally, check that all the data references in the loop can be
1224 handled with respect to their alignment. */
1226 for (i = 0; i < VARRAY_ACTIVE_SIZE (loop_read_datarefs); i++)
1228 struct data_reference *dr = VARRAY_GENERIC_PTR (loop_read_datarefs, i);
1229 supportable_dr_alignment = vect_supportable_dr_alignment (dr);
1230 if (!supportable_dr_alignment)
1232 if (vect_print_dump_info (REPORT_UNVECTORIZED_LOOPS,
1233 LOOP_LOC (loop_vinfo)))
1234 fprintf (vect_dump, "not vectorized: unsupported unaligned load.");
1237 if (supportable_dr_alignment != dr_aligned
1238 && (vect_print_dump_info (REPORT_ALIGNMENT, LOOP_LOC (loop_vinfo))))
1239 fprintf (vect_dump, "Vectorizing an unaligned access.");
1241 for (i = 0; i < VARRAY_ACTIVE_SIZE (loop_write_datarefs); i++)
1243 struct data_reference *dr = VARRAY_GENERIC_PTR (loop_write_datarefs, i);
1244 supportable_dr_alignment = vect_supportable_dr_alignment (dr);
1245 if (!supportable_dr_alignment)
1247 if (vect_print_dump_info (REPORT_UNVECTORIZED_LOOPS,
1248 LOOP_LOC (loop_vinfo)))
1249 fprintf (vect_dump, "not vectorized: unsupported unaligned store.");
1252 if (supportable_dr_alignment != dr_aligned
1253 && (vect_print_dump_info (REPORT_ALIGNMENT, LOOP_LOC (loop_vinfo))))
1254 fprintf (vect_dump, "Vectorizing an unaligned access.");
1261 /* Function vect_analyze_data_ref_access.
1263 Analyze the access pattern of the data-reference DR. For now, a data access
1264 has to consecutive to be considered vectorizable. */
1267 vect_analyze_data_ref_access (struct data_reference *dr)
1269 tree stmt = DR_STMT (dr);
1270 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1271 tree step = STMT_VINFO_VECT_STEP (stmt_info);
1272 tree scalar_type = TREE_TYPE (DR_REF (dr));
1274 if (!step || tree_int_cst_compare (step, TYPE_SIZE_UNIT (scalar_type)))
1276 if (vect_print_dump_info (REPORT_DETAILS, UNKNOWN_LOC))
1277 fprintf (vect_dump, "not consecutive access");
1284 /* Function vect_analyze_data_ref_accesses.
1286 Analyze the access pattern of all the data references in the loop.
1288 FORNOW: the only access pattern that is considered vectorizable is a
1289 simple step 1 (consecutive) access.
1291 FORNOW: handle only arrays and pointer accesses. */
1294 vect_analyze_data_ref_accesses (loop_vec_info loop_vinfo)
1297 varray_type loop_write_datarefs = LOOP_VINFO_DATAREF_WRITES (loop_vinfo);
1298 varray_type loop_read_datarefs = LOOP_VINFO_DATAREF_READS (loop_vinfo);
1300 if (vect_print_dump_info (REPORT_DETAILS, UNKNOWN_LOC))
1301 fprintf (vect_dump, "=== vect_analyze_data_ref_accesses ===");
1303 for (i = 0; i < VARRAY_ACTIVE_SIZE (loop_write_datarefs); i++)
1305 struct data_reference *dr = VARRAY_GENERIC_PTR (loop_write_datarefs, i);
1306 bool ok = vect_analyze_data_ref_access (dr);
1309 if (vect_print_dump_info (REPORT_UNVECTORIZED_LOOPS,
1310 LOOP_LOC (loop_vinfo)))
1311 fprintf (vect_dump, "not vectorized: complicated access pattern.");
1316 for (i = 0; i < VARRAY_ACTIVE_SIZE (loop_read_datarefs); i++)
1318 struct data_reference *dr = VARRAY_GENERIC_PTR (loop_read_datarefs, i);
1319 bool ok = vect_analyze_data_ref_access (dr);
1322 if (vect_print_dump_info (REPORT_UNVECTORIZED_LOOPS,
1323 LOOP_LOC (loop_vinfo)))
1324 fprintf (vect_dump, "not vectorized: complicated access pattern.");
1333 /* Function vect_analyze_pointer_ref_access.
1336 STMT - a stmt that contains a data-ref.
1337 MEMREF - a data-ref in STMT, which is an INDIRECT_REF.
1338 ACCESS_FN - the access function of MEMREF.
1341 If the data-ref access is vectorizable, return a data_reference structure
1342 that represents it (DR). Otherwise - return NULL.
1343 STEP - the stride of MEMREF in the loop.
1344 INIT - the initial condition of MEMREF in the loop.
1347 static struct data_reference *
1348 vect_analyze_pointer_ref_access (tree memref, tree stmt, bool is_read,
1349 tree access_fn, tree *ptr_init, tree *ptr_step)
1351 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1352 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
1353 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
1355 tree reftype, innertype;
1356 tree indx_access_fn;
1357 int loopnum = loop->num;
1358 struct data_reference *dr;
1360 if (!vect_is_simple_iv_evolution (loopnum, access_fn, &init, &step))
1362 if (vect_print_dump_info (REPORT_UNVECTORIZED_LOOPS,
1363 LOOP_LOC (loop_vinfo)))
1364 fprintf (vect_dump, "not vectorized: pointer access is not simple.");
1370 if (!expr_invariant_in_loop_p (loop, init))
1372 if (vect_print_dump_info (REPORT_UNVECTORIZED_LOOPS,
1373 LOOP_LOC (loop_vinfo)))
1375 "not vectorized: initial condition is not loop invariant.");
1379 if (TREE_CODE (step) != INTEGER_CST)
1381 if (vect_print_dump_info (REPORT_UNVECTORIZED_LOOPS,
1382 LOOP_LOC (loop_vinfo)))
1384 "not vectorized: non constant step for pointer access.");
1388 reftype = TREE_TYPE (TREE_OPERAND (memref, 0));
1389 if (!POINTER_TYPE_P (reftype))
1391 if (vect_print_dump_info (REPORT_UNVECTORIZED_LOOPS,
1392 LOOP_LOC (loop_vinfo)))
1393 fprintf (vect_dump, "not vectorized: unexpected pointer access form.");
1397 reftype = TREE_TYPE (init);
1398 if (!POINTER_TYPE_P (reftype))
1400 if (vect_print_dump_info (REPORT_UNVECTORIZED_LOOPS,
1401 LOOP_LOC (loop_vinfo)))
1402 fprintf (vect_dump, "not vectorized: unexpected pointer access form.");
1406 *ptr_step = fold_convert (ssizetype, step);
1407 innertype = TREE_TYPE (reftype);
1408 /* Check that STEP is a multiple of type size. */
1409 if (!integer_zerop (size_binop (TRUNC_MOD_EXPR, *ptr_step,
1410 fold_convert (ssizetype, TYPE_SIZE_UNIT (innertype)))))
1412 if (vect_print_dump_info (REPORT_UNVECTORIZED_LOOPS,
1413 LOOP_LOC (loop_vinfo)))
1414 fprintf (vect_dump, "not vectorized: non consecutive access.");
1419 build_polynomial_chrec (loopnum, integer_zero_node, integer_one_node);
1420 if (vect_print_dump_info (REPORT_DETAILS, UNKNOWN_LOC))
1422 fprintf (vect_dump, "Access function of ptr indx: ");
1423 print_generic_expr (vect_dump, indx_access_fn, TDF_SLIM);
1425 dr = init_data_ref (stmt, memref, NULL_TREE, indx_access_fn, is_read);
1431 /* Function vect_address_analysis
1433 Return the BASE of the address expression EXPR.
1434 Also compute the INITIAL_OFFSET from BASE, MISALIGN and STEP.
1437 EXPR - the address expression that is being analyzed
1438 STMT - the statement that contains EXPR or its original memory reference
1439 IS_READ - TRUE if STMT reads from EXPR, FALSE if writes to EXPR
1440 VECTYPE - the type that defines the alignment (i.e, we compute
1441 alignment relative to TYPE_ALIGN(VECTYPE))
1442 DR - data_reference struct for the original memory reference
1445 BASE (returned value) - the base of the data reference EXPR.
1446 INITIAL_OFFSET - initial offset of EXPR from BASE (an expression)
1447 MISALIGN - offset of EXPR from BASE in bytes (a constant) or NULL_TREE if the
1448 computation is impossible
1449 STEP - evolution of EXPR in the loop
1450 BASE_ALIGNED - indicates if BASE is aligned
1452 If something unexpected is encountered (an unsupported form of data-ref),
1453 then NULL_TREE is returned.
1457 vect_address_analysis (tree expr, tree stmt, bool is_read, tree vectype,
1458 struct data_reference *dr, tree *offset, tree *misalign,
1459 tree *step, bool *base_aligned)
1461 tree oprnd0, oprnd1, base_address, offset_expr, base_addr0, base_addr1;
1462 tree address_offset = ssize_int (0), address_misalign = ssize_int (0);
1466 switch (TREE_CODE (expr))
1470 /* EXPR is of form {base +/- offset} (or {offset +/- base}). */
1471 oprnd0 = TREE_OPERAND (expr, 0);
1472 oprnd1 = TREE_OPERAND (expr, 1);
1474 STRIP_NOPS (oprnd0);
1475 STRIP_NOPS (oprnd1);
1477 /* Recursively try to find the base of the address contained in EXPR.
1478 For offset, the returned base will be NULL. */
1479 base_addr0 = vect_address_analysis (oprnd0, stmt, is_read, vectype, dr,
1480 &address_offset, &address_misalign, step,
1483 base_addr1 = vect_address_analysis (oprnd1, stmt, is_read, vectype, dr,
1484 &address_offset, &address_misalign, step,
1487 /* We support cases where only one of the operands contains an
1489 if ((base_addr0 && base_addr1) || (!base_addr0 && !base_addr1))
1492 /* To revert STRIP_NOPS. */
1493 oprnd0 = TREE_OPERAND (expr, 0);
1494 oprnd1 = TREE_OPERAND (expr, 1);
1496 offset_expr = base_addr0 ?
1497 fold_convert (ssizetype, oprnd1) : fold_convert (ssizetype, oprnd0);
1499 /* EXPR is of form {base +/- offset} (or {offset +/- base}). If offset is
1500 a number, we can add it to the misalignment value calculated for base,
1501 otherwise, misalignment is NULL. */
1502 if (TREE_CODE (offset_expr) == INTEGER_CST && address_misalign)
1503 *misalign = size_binop (TREE_CODE (expr), address_misalign,
1506 *misalign = NULL_TREE;
1508 /* Combine offset (from EXPR {base + offset}) with the offset calculated
1510 *offset = size_binop (TREE_CODE (expr), address_offset, offset_expr);
1511 return base_addr0 ? base_addr0 : base_addr1;
1514 base_address = vect_object_analysis (TREE_OPERAND (expr, 0), stmt,
1515 is_read, vectype, &dr, offset,
1516 misalign, step, base_aligned,
1518 return base_address;
1521 if (!POINTER_TYPE_P (TREE_TYPE (expr)))
1524 if (TYPE_ALIGN (TREE_TYPE (TREE_TYPE (expr))) < TYPE_ALIGN (vectype))
1526 if (vect_get_ptr_offset (expr, vectype, misalign))
1527 *base_aligned = true;
1529 *base_aligned = false;
1533 *base_aligned = true;
1534 *misalign = ssize_int (0);
1536 *offset = ssize_int (0);
1537 *step = ssize_int (0);
1546 /* Function vect_object_analysis
1548 Return the BASE of the data reference MEMREF.
1549 Also compute the INITIAL_OFFSET from BASE, MISALIGN and STEP.
1550 E.g., for EXPR a.b[i] + 4B, BASE is a, and OFFSET is the overall offset
1551 'a.b[i] + 4B' from a (can be an expression), MISALIGN is an OFFSET
1552 instantiated with initial_conditions of access_functions of variables,
1553 modulo alignment, and STEP is the evolution of the DR_REF in this loop.
1555 Function get_inner_reference is used for the above in case of ARRAY_REF and
1558 The structure of the function is as follows:
1560 Case 1. For handled_component_p refs
1561 1.1 call get_inner_reference
1562 1.1.1 analyze offset expr received from get_inner_reference
1563 1.2. build data-reference structure for MEMREF
1564 (fall through with BASE)
1565 Case 2. For declarations
1567 2.2 update DR_BASE_NAME if necessary for alias
1568 Case 3. For INDIRECT_REFs
1569 3.1 get the access function
1570 3.2 analyze evolution of MEMREF
1571 3.3 set data-reference structure for MEMREF
1572 3.4 call vect_address_analysis to analyze INIT of the access function
1575 Combine the results of object and address analysis to calculate
1576 INITIAL_OFFSET, STEP and misalignment info.
1579 MEMREF - the memory reference that is being analyzed
1580 STMT - the statement that contains MEMREF
1581 IS_READ - TRUE if STMT reads from MEMREF, FALSE if writes to MEMREF
1582 VECTYPE - the type that defines the alignment (i.e, we compute
1583 alignment relative to TYPE_ALIGN(VECTYPE))
1586 BASE_ADDRESS (returned value) - the base address of the data reference MEMREF
1587 E.g, if MEMREF is a.b[k].c[i][j] the returned
1589 DR - data_reference struct for MEMREF
1590 INITIAL_OFFSET - initial offset of MEMREF from BASE (an expression)
1591 MISALIGN - offset of MEMREF from BASE in bytes (a constant) or NULL_TREE if
1592 the computation is impossible
1593 STEP - evolution of the DR_REF in the loop
1594 BASE_ALIGNED - indicates if BASE is aligned
1595 MEMTAG - memory tag for aliasing purposes
1596 SUBVAR - Sub-variables of the variable
1598 If something unexpected is encountered (an unsupported form of data-ref),
1599 then NULL_TREE is returned. */
1602 vect_object_analysis (tree memref, tree stmt, bool is_read,
1603 tree vectype, struct data_reference **dr,
1604 tree *offset, tree *misalign, tree *step,
1605 bool *base_aligned, tree *memtag,
1608 tree base = NULL_TREE, base_address = NULL_TREE;
1609 tree object_offset = ssize_int (0), object_misalign = ssize_int (0);
1610 tree object_step = ssize_int (0), address_step = ssize_int (0);
1611 bool object_base_aligned = true, address_base_aligned = true;
1612 tree address_offset = ssize_int (0), address_misalign = ssize_int (0);
1613 HOST_WIDE_INT pbitsize, pbitpos;
1614 tree poffset, bit_pos_in_bytes;
1615 enum machine_mode pmode;
1616 int punsignedp, pvolatilep;
1617 tree ptr_step = ssize_int (0), ptr_init = NULL_TREE;
1618 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1619 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
1620 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
1621 struct data_reference *ptr_dr = NULL;
1622 tree access_fn, evolution_part, address_to_analyze;
1625 /* Case 1. handled_component_p refs. */
1626 if (handled_component_p (memref))
1628 /* 1.1 call get_inner_reference. */
1629 /* Find the base and the offset from it. */
1630 base = get_inner_reference (memref, &pbitsize, &pbitpos, &poffset,
1631 &pmode, &punsignedp, &pvolatilep, false);
1635 /* 1.1.1 analyze offset expr received from get_inner_reference. */
1637 && !vect_analyze_offset_expr (poffset, loop, TYPE_SIZE_UNIT (vectype),
1638 &object_offset, &object_misalign, &object_step))
1640 if (vect_print_dump_info (REPORT_DETAILS, UNKNOWN_LOC))
1642 fprintf (vect_dump, "failed to compute offset or step for ");
1643 print_generic_expr (vect_dump, memref, TDF_SLIM);
1648 /* Add bit position to OFFSET and MISALIGN. */
1650 bit_pos_in_bytes = ssize_int (pbitpos/BITS_PER_UNIT);
1651 /* Check that there is no remainder in bits. */
1652 if (pbitpos%BITS_PER_UNIT)
1654 if (vect_print_dump_info (REPORT_DETAILS, UNKNOWN_LOC))
1655 fprintf (vect_dump, "bit offset alignment.");
1658 object_offset = size_binop (PLUS_EXPR, bit_pos_in_bytes, object_offset);
1659 if (object_misalign)
1660 object_misalign = size_binop (PLUS_EXPR, object_misalign,
1663 /* Create data-reference for MEMREF. TODO: handle COMPONENT_REFs. */
1666 if (TREE_CODE (memref) == ARRAY_REF)
1667 *dr = analyze_array (stmt, memref, is_read);
1672 memref = base; /* To continue analysis of BASE. */
1676 /* Part 1: Case 2. Declarations. */
1677 if (DECL_P (memref))
1679 /* We expect to get a decl only if we already have a DR. */
1682 if (vect_print_dump_info (REPORT_DETAILS, UNKNOWN_LOC))
1684 fprintf (vect_dump, "unhandled decl ");
1685 print_generic_expr (vect_dump, memref, TDF_SLIM);
1690 /* 2.1 check the alignment. */
1691 if (DECL_ALIGN (memref) >= TYPE_ALIGN (vectype))
1692 object_base_aligned = true;
1694 object_base_aligned = false;
1696 /* 2.2 update DR_BASE_NAME if necessary. */
1697 if (!DR_BASE_NAME ((*dr)))
1698 /* For alias analysis. In case the analysis of INDIRECT_REF brought
1700 DR_BASE_NAME ((*dr)) = memref;
1702 if (SSA_VAR_P (memref) && var_can_have_subvars (memref))
1703 *subvars = get_subvars_for_var (memref);
1704 base_address = build_fold_addr_expr (memref);
1708 /* Part 1: Case 3. INDIRECT_REFs. */
1709 else if (TREE_CODE (memref) == INDIRECT_REF)
1711 /* 3.1 get the access function. */
1712 access_fn = analyze_scalar_evolution (loop, TREE_OPERAND (memref, 0));
1715 if (vect_print_dump_info (REPORT_UNVECTORIZED_LOOPS,
1716 LOOP_LOC (loop_vinfo)))
1717 fprintf (vect_dump, "not vectorized: complicated pointer access.");
1720 if (vect_print_dump_info (REPORT_DETAILS, UNKNOWN_LOC))
1722 fprintf (vect_dump, "Access function of ptr: ");
1723 print_generic_expr (vect_dump, access_fn, TDF_SLIM);
1726 /* 3.2 analyze evolution of MEMREF. */
1727 evolution_part = evolution_part_in_loop_num (access_fn, loop->num);
1730 ptr_dr = vect_analyze_pointer_ref_access (memref, stmt, is_read,
1731 access_fn, &ptr_init, &ptr_step);
1735 object_step = size_binop (PLUS_EXPR, object_step, ptr_step);
1736 address_to_analyze = ptr_init;
1742 if (vect_print_dump_info (REPORT_UNVECTORIZED_LOOPS,
1743 LOOP_LOC (loop_vinfo)))
1744 fprintf (vect_dump, "not vectorized: ptr is loop invariant.");
1747 /* Since there exists DR for MEMREF, we are analyzing the init of
1748 the access function, which not necessary has evolution in the
1750 address_to_analyze = initial_condition_in_loop_num (access_fn,
1754 /* 3.3 set data-reference structure for MEMREF. */
1755 *dr = (*dr) ? *dr : ptr_dr;
1757 /* 3.4 call vect_address_analysis to analyze INIT of the access
1759 base_address = vect_address_analysis (address_to_analyze, stmt, is_read,
1760 vectype, *dr, &address_offset, &address_misalign,
1761 &address_step, &address_base_aligned);
1765 switch (TREE_CODE (base_address))
1768 *memtag = get_var_ann (SSA_NAME_VAR (base_address))->type_mem_tag;
1769 if (!(*memtag) && TREE_CODE (TREE_OPERAND (memref, 0)) == SSA_NAME)
1770 *memtag = get_var_ann (
1771 SSA_NAME_VAR (TREE_OPERAND (memref, 0)))->type_mem_tag;
1774 *memtag = TREE_OPERAND (base_address, 0);
1777 if (vect_print_dump_info (REPORT_UNVECTORIZED_LOOPS,
1778 LOOP_LOC (loop_vinfo)))
1780 fprintf (vect_dump, "not vectorized: no memtag ref: ");
1781 print_generic_expr (vect_dump, memref, TDF_SLIM);
1788 /* MEMREF cannot be analyzed. */
1791 if (SSA_VAR_P (*memtag) && var_can_have_subvars (*memtag))
1792 *subvars = get_subvars_for_var (*memtag);
1794 /* Part 2: Combine the results of object and address analysis to calculate
1795 INITIAL_OFFSET, STEP and misalignment info. */
1796 *offset = size_binop (PLUS_EXPR, object_offset, address_offset);
1797 if (object_misalign && address_misalign)
1798 *misalign = size_binop (PLUS_EXPR, object_misalign, address_misalign);
1800 *misalign = NULL_TREE;
1801 *step = size_binop (PLUS_EXPR, object_step, address_step);
1802 *base_aligned = object_base_aligned && address_base_aligned;
1804 if (vect_print_dump_info (REPORT_DETAILS, UNKNOWN_LOC))
1806 fprintf (vect_dump, "Results of object analysis for: ");
1807 print_generic_expr (vect_dump, memref, TDF_SLIM);
1808 fprintf (vect_dump, "\n\tbase_address: ");
1809 print_generic_expr (vect_dump, base_address, TDF_SLIM);
1810 fprintf (vect_dump, "\n\toffset: ");
1811 print_generic_expr (vect_dump, *offset, TDF_SLIM);
1812 fprintf (vect_dump, "\n\tstep: ");
1813 print_generic_expr (vect_dump, *step, TDF_SLIM);
1814 fprintf (vect_dump, "\n\tbase aligned %d\n\tmisalign: ", *base_aligned);
1815 print_generic_expr (vect_dump, *misalign, TDF_SLIM);
1817 return base_address;
1821 /* Function vect_analyze_data_refs.
1823 Find all the data references in the loop.
1825 The general structure of the analysis of data refs in the vectorizer is as
1827 1- vect_analyze_data_refs(loop):
1828 Find and analyze all data-refs in the loop:
1830 base_address = vect_object_analysis(ref)
1831 1.1- vect_object_analysis(ref):
1832 Analyze ref, and build a DR (data_referece struct) for it;
1833 compute base, initial_offset, step and alignment.
1834 Call get_inner_reference for refs handled in this function.
1835 Call vect_addr_analysis(addr) to analyze pointer type expressions.
1836 Set ref_stmt.base, ref_stmt.initial_offset, ref_stmt.alignment,
1837 ref_stmt.memtag and ref_stmt.step accordingly.
1838 2- vect_analyze_dependences(): apply dependence testing using ref_stmt.DR
1839 3- vect_analyze_drs_alignment(): check that ref_stmt.alignment is ok.
1840 4- vect_analyze_drs_access(): check that ref_stmt.step is ok.
1842 FORNOW: Handle aligned INDIRECT_REFs and ARRAY_REFs
1843 which base is really an array (not a pointer) and which alignment
1844 can be forced. This restriction will be relaxed. */
1847 vect_analyze_data_refs (loop_vec_info loop_vinfo)
1849 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
1850 basic_block *bbs = LOOP_VINFO_BBS (loop_vinfo);
1851 int nbbs = loop->num_nodes;
1852 block_stmt_iterator si;
1854 struct data_reference *dr;
1856 if (vect_print_dump_info (REPORT_DETAILS, UNKNOWN_LOC))
1857 fprintf (vect_dump, "=== vect_analyze_data_refs ===");
1859 for (j = 0; j < nbbs; j++)
1861 basic_block bb = bbs[j];
1862 for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
1864 bool is_read = false;
1865 tree stmt = bsi_stmt (si);
1866 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1867 v_may_def_optype v_may_defs = STMT_V_MAY_DEF_OPS (stmt);
1868 v_must_def_optype v_must_defs = STMT_V_MUST_DEF_OPS (stmt);
1869 vuse_optype vuses = STMT_VUSE_OPS (stmt);
1870 varray_type *datarefs = NULL;
1871 int nvuses, nv_may_defs, nv_must_defs;
1873 tree scalar_type, vectype;
1874 tree base, offset, misalign, step, tag;
1878 /* Assumption: there exists a data-ref in stmt, if and only if
1879 it has vuses/vdefs. */
1881 if (!vuses && !v_may_defs && !v_must_defs)
1884 nvuses = NUM_VUSES (vuses);
1885 nv_may_defs = NUM_V_MAY_DEFS (v_may_defs);
1886 nv_must_defs = NUM_V_MUST_DEFS (v_must_defs);
1888 if (nvuses && (nv_may_defs || nv_must_defs))
1890 if (vect_print_dump_info (REPORT_DETAILS, UNKNOWN_LOC))
1892 fprintf (vect_dump, "unexpected vdefs and vuses in stmt: ");
1893 print_generic_expr (vect_dump, stmt, TDF_SLIM);
1898 if (TREE_CODE (stmt) != MODIFY_EXPR)
1900 if (vect_print_dump_info (REPORT_DETAILS, UNKNOWN_LOC))
1902 fprintf (vect_dump, "unexpected vops in stmt: ");
1903 print_generic_expr (vect_dump, stmt, TDF_SLIM);
1910 memref = TREE_OPERAND (stmt, 1);
1911 datarefs = &(LOOP_VINFO_DATAREF_READS (loop_vinfo));
1916 memref = TREE_OPERAND (stmt, 0);
1917 datarefs = &(LOOP_VINFO_DATAREF_WRITES (loop_vinfo));
1921 scalar_type = TREE_TYPE (memref);
1922 vectype = get_vectype_for_scalar_type (scalar_type);
1925 if (vect_print_dump_info (REPORT_DETAILS, UNKNOWN_LOC))
1927 fprintf (vect_dump, "no vectype for stmt: ");
1928 print_generic_expr (vect_dump, stmt, TDF_SLIM);
1929 fprintf (vect_dump, " scalar_type: ");
1930 print_generic_expr (vect_dump, scalar_type, TDF_DETAILS);
1932 /* It is not possible to vectorize this data reference. */
1935 /* Analyze MEMREF. If it is of a supported form, build data_reference
1936 struct for it (DR). */
1938 base = vect_object_analysis (memref, stmt, is_read, vectype, &dr,
1939 &offset, &misalign, &step,
1940 &base_aligned, &tag, &subvars);
1943 if (vect_print_dump_info (REPORT_UNVECTORIZED_LOOPS,
1944 LOOP_LOC (loop_vinfo)))
1946 fprintf (vect_dump, "not vectorized: unhandled data ref: ");
1947 print_generic_expr (vect_dump, stmt, TDF_SLIM);
1951 STMT_VINFO_VECT_DR_BASE_ADDRESS (stmt_info) = base;
1952 STMT_VINFO_VECT_INIT_OFFSET (stmt_info) = offset;
1953 STMT_VINFO_VECT_STEP (stmt_info) = step;
1954 STMT_VINFO_VECT_MISALIGNMENT (stmt_info) = misalign;
1955 STMT_VINFO_VECT_BASE_ALIGNED_P (stmt_info) = base_aligned;
1956 STMT_VINFO_MEMTAG (stmt_info) = tag;
1957 STMT_VINFO_SUBVARS (stmt_info) = subvars;
1958 STMT_VINFO_VECTYPE (stmt_info) = vectype;
1959 VARRAY_PUSH_GENERIC_PTR (*datarefs, dr);
1960 STMT_VINFO_DATA_REF (stmt_info) = dr;
1968 /* Utility functions used by vect_mark_stmts_to_be_vectorized. */
1970 /* Function vect_mark_relevant.
1972 Mark STMT as "relevant for vectorization" and add it to WORKLIST. */
1975 vect_mark_relevant (varray_type *worklist, tree stmt)
1977 stmt_vec_info stmt_info;
1979 if (vect_print_dump_info (REPORT_DETAILS, UNKNOWN_LOC))
1980 fprintf (vect_dump, "mark relevant.");
1982 if (TREE_CODE (stmt) == PHI_NODE)
1984 VARRAY_PUSH_TREE (*worklist, stmt);
1988 stmt_info = vinfo_for_stmt (stmt);
1992 if (vect_print_dump_info (REPORT_DETAILS, UNKNOWN_LOC))
1994 fprintf (vect_dump, "mark relevant: no stmt info!!.");
1995 print_generic_expr (vect_dump, stmt, TDF_SLIM);
2000 if (STMT_VINFO_RELEVANT_P (stmt_info))
2002 if (vect_print_dump_info (REPORT_DETAILS, UNKNOWN_LOC))
2003 fprintf (vect_dump, "already marked relevant.");
2007 STMT_VINFO_RELEVANT_P (stmt_info) = 1;
2008 VARRAY_PUSH_TREE (*worklist, stmt);
2012 /* Function vect_stmt_relevant_p.
2014 Return true if STMT in loop that is represented by LOOP_VINFO is
2015 "relevant for vectorization".
2017 A stmt is considered "relevant for vectorization" if:
2018 - it has uses outside the loop.
2019 - it has vdefs (it alters memory).
2020 - control stmts in the loop (except for the exit condition).
2022 CHECKME: what other side effects would the vectorizer allow? */
2025 vect_stmt_relevant_p (tree stmt, loop_vec_info loop_vinfo)
2027 v_may_def_optype v_may_defs;
2028 v_must_def_optype v_must_defs;
2029 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
2034 /* cond stmt other than loop exit cond. */
2035 if (is_ctrl_stmt (stmt) && (stmt != LOOP_VINFO_EXIT_COND (loop_vinfo)))
2038 /* changing memory. */
2039 if (TREE_CODE (stmt) != PHI_NODE)
2041 v_may_defs = STMT_V_MAY_DEF_OPS (stmt);
2042 v_must_defs = STMT_V_MUST_DEF_OPS (stmt);
2043 if (v_may_defs || v_must_defs)
2045 if (vect_print_dump_info (REPORT_DETAILS, UNKNOWN_LOC))
2046 fprintf (vect_dump, "vec_stmt_relevant_p: stmt has vdefs.");
2051 /* uses outside the loop. */
2052 df = get_immediate_uses (stmt);
2053 num_uses = num_immediate_uses (df);
2054 for (i = 0; i < num_uses; i++)
2056 tree use = immediate_use (df, i);
2057 basic_block bb = bb_for_stmt (use);
2058 if (!flow_bb_inside_loop_p (loop, bb))
2060 if (vect_print_dump_info (REPORT_DETAILS, UNKNOWN_LOC))
2061 fprintf (vect_dump, "vec_stmt_relevant_p: used out of loop.");
2070 /* Function vect_mark_stmts_to_be_vectorized.
2072 Not all stmts in the loop need to be vectorized. For example:
2081 Stmt 1 and 3 do not need to be vectorized, because loop control and
2082 addressing of vectorized data-refs are handled differently.
2084 This pass detects such stmts. */
2087 vect_mark_stmts_to_be_vectorized (loop_vec_info loop_vinfo)
2089 varray_type worklist;
2090 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
2091 basic_block *bbs = LOOP_VINFO_BBS (loop_vinfo);
2092 unsigned int nbbs = loop->num_nodes;
2093 block_stmt_iterator si;
2099 stmt_vec_info stmt_info;
2103 if (vect_print_dump_info (REPORT_DETAILS, UNKNOWN_LOC))
2104 fprintf (vect_dump, "=== vect_mark_stmts_to_be_vectorized ===");
2107 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
2109 if (vect_print_dump_info (REPORT_DETAILS, UNKNOWN_LOC))
2111 fprintf (vect_dump, "init: phi relevant? ");
2112 print_generic_expr (vect_dump, phi, TDF_SLIM);
2115 if (vect_stmt_relevant_p (phi, loop_vinfo))
2117 if (vect_print_dump_info (REPORT_UNVECTORIZED_LOOPS,
2118 LOOP_LOC (loop_vinfo)))
2119 fprintf (vect_dump, "unsupported reduction/induction.");
2124 VARRAY_TREE_INIT (worklist, 64, "work list");
2126 /* 1. Init worklist. */
2128 for (i = 0; i < nbbs; i++)
2131 for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
2133 stmt = bsi_stmt (si);
2135 if (vect_print_dump_info (REPORT_DETAILS, UNKNOWN_LOC))
2137 fprintf (vect_dump, "init: stmt relevant? ");
2138 print_generic_expr (vect_dump, stmt, TDF_SLIM);
2141 stmt_info = vinfo_for_stmt (stmt);
2142 STMT_VINFO_RELEVANT_P (stmt_info) = 0;
2144 if (vect_stmt_relevant_p (stmt, loop_vinfo))
2145 vect_mark_relevant (&worklist, stmt);
2150 /* 2. Process_worklist */
2152 while (VARRAY_ACTIVE_SIZE (worklist) > 0)
2154 stmt = VARRAY_TOP_TREE (worklist);
2155 VARRAY_POP (worklist);
2157 if (vect_print_dump_info (REPORT_DETAILS, UNKNOWN_LOC))
2159 fprintf (vect_dump, "worklist: examine stmt: ");
2160 print_generic_expr (vect_dump, stmt, TDF_SLIM);
2163 /* Examine the USES in this statement. Mark all the statements which
2164 feed this statement's uses as "relevant", unless the USE is used as
2167 if (TREE_CODE (stmt) == PHI_NODE)
2169 /* follow the def-use chain inside the loop. */
2170 for (j = 0; j < PHI_NUM_ARGS (stmt); j++)
2172 tree arg = PHI_ARG_DEF (stmt, j);
2173 tree def_stmt = NULL_TREE;
2175 if (!vect_is_simple_use (arg, loop_vinfo, &def_stmt))
2177 if (vect_print_dump_info (REPORT_UNVECTORIZED_LOOPS,
2178 LOOP_LOC (loop_vinfo)))
2179 fprintf (vect_dump, "not vectorized: unsupported use in stmt.");
2180 varray_clear (worklist);
2186 if (vect_print_dump_info (REPORT_DETAILS, UNKNOWN_LOC))
2188 fprintf (vect_dump, "worklist: def_stmt: ");
2189 print_generic_expr (vect_dump, def_stmt, TDF_SLIM);
2192 bb = bb_for_stmt (def_stmt);
2193 if (flow_bb_inside_loop_p (loop, bb))
2194 vect_mark_relevant (&worklist, def_stmt);
2198 ann = stmt_ann (stmt);
2199 use_ops = USE_OPS (ann);
2201 for (i = 0; i < NUM_USES (use_ops); i++)
2203 tree use = USE_OP (use_ops, i);
2205 /* We are only interested in uses that need to be vectorized. Uses
2206 that are used for address computation are not considered relevant.
2208 if (exist_non_indexing_operands_for_use_p (use, stmt))
2210 tree def_stmt = NULL_TREE;
2212 if (!vect_is_simple_use (use, loop_vinfo, &def_stmt))
2214 if (vect_print_dump_info (REPORT_UNVECTORIZED_LOOPS,
2215 LOOP_LOC (loop_vinfo)))
2216 fprintf (vect_dump, "not vectorized: unsupported use in stmt.");
2217 varray_clear (worklist);
2224 if (vect_print_dump_info (REPORT_DETAILS, UNKNOWN_LOC))
2226 fprintf (vect_dump, "worklist: examine use %d: ", i);
2227 print_generic_expr (vect_dump, use, TDF_SLIM);
2230 bb = bb_for_stmt (def_stmt);
2231 if (flow_bb_inside_loop_p (loop, bb))
2232 vect_mark_relevant (&worklist, def_stmt);
2235 } /* while worklist */
2237 varray_clear (worklist);
2242 /* Function vect_can_advance_ivs_p
2244 In case the number of iterations that LOOP iterates in unknown at compile
2245 time, an epilog loop will be generated, and the loop induction variables
2246 (IVs) will be "advanced" to the value they are supposed to take just before
2247 the epilog loop. Here we check that the access function of the loop IVs
2248 and the expression that represents the loop bound are simple enough.
2249 These restrictions will be relaxed in the future. */
2252 vect_can_advance_ivs_p (loop_vec_info loop_vinfo)
2254 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
2255 basic_block bb = loop->header;
2258 /* Analyze phi functions of the loop header. */
2260 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
2262 tree access_fn = NULL;
2263 tree evolution_part;
2265 if (vect_print_dump_info (REPORT_DETAILS, UNKNOWN_LOC))
2267 fprintf (vect_dump, "Analyze phi: ");
2268 print_generic_expr (vect_dump, phi, TDF_SLIM);
2271 /* Skip virtual phi's. The data dependences that are associated with
2272 virtual defs/uses (i.e., memory accesses) are analyzed elsewhere. */
2274 if (!is_gimple_reg (SSA_NAME_VAR (PHI_RESULT (phi))))
2276 if (vect_print_dump_info (REPORT_DETAILS, UNKNOWN_LOC))
2277 fprintf (vect_dump, "virtual phi. skip.");
2281 /* Analyze the evolution function. */
2283 access_fn = instantiate_parameters
2284 (loop, analyze_scalar_evolution (loop, PHI_RESULT (phi)));
2288 if (vect_print_dump_info (REPORT_DETAILS, UNKNOWN_LOC))
2289 fprintf (vect_dump, "No Access function.");
2293 if (vect_print_dump_info (REPORT_DETAILS, UNKNOWN_LOC))
2295 fprintf (vect_dump, "Access function of PHI: ");
2296 print_generic_expr (vect_dump, access_fn, TDF_SLIM);
2299 evolution_part = evolution_part_in_loop_num (access_fn, loop->num);
2301 if (evolution_part == NULL_TREE)
2304 /* FORNOW: We do not transform initial conditions of IVs
2305 which evolution functions are a polynomial of degree >= 2. */
2307 if (tree_is_chrec (evolution_part))
2315 /* Function vect_get_loop_niters.
2317 Determine how many iterations the loop is executed.
2318 If an expression that represents the number of iterations
2319 can be constructed, place it in NUMBER_OF_ITERATIONS.
2320 Return the loop exit condition. */
2323 vect_get_loop_niters (struct loop *loop, tree *number_of_iterations)
2327 if (vect_print_dump_info (REPORT_DETAILS, UNKNOWN_LOC))
2328 fprintf (vect_dump, "=== get_loop_niters ===");
2330 niters = number_of_iterations_in_loop (loop);
2332 if (niters != NULL_TREE
2333 && niters != chrec_dont_know)
2335 *number_of_iterations = niters;
2337 if (vect_print_dump_info (REPORT_DETAILS, UNKNOWN_LOC))
2339 fprintf (vect_dump, "==> get_loop_niters:" );
2340 print_generic_expr (vect_dump, *number_of_iterations, TDF_SLIM);
2344 return get_loop_exit_condition (loop);
2348 /* Function vect_analyze_loop_form.
2350 Verify the following restrictions (some may be relaxed in the future):
2351 - it's an inner-most loop
2352 - number of BBs = 2 (which are the loop header and the latch)
2353 - the loop has a pre-header
2354 - the loop has a single entry and exit
2355 - the loop exit condition is simple enough, and the number of iterations
2356 can be analyzed (a countable loop). */
2358 static loop_vec_info
2359 vect_analyze_loop_form (struct loop *loop)
2361 loop_vec_info loop_vinfo;
2363 tree number_of_iterations = NULL;
2366 loop_loc = find_loop_location (loop);
2368 if (vect_print_dump_info (REPORT_DETAILS, loop_loc))
2369 fprintf (vect_dump, "=== vect_analyze_loop_form ===");
2373 if (vect_print_dump_info (REPORT_OUTER_LOOPS, loop_loc))
2374 fprintf (vect_dump, "not vectorized: nested loop.");
2378 if (!loop->single_exit
2379 || loop->num_nodes != 2
2380 || EDGE_COUNT (loop->header->preds) != 2)
2382 if (vect_print_dump_info (REPORT_BAD_FORM_LOOPS, loop_loc))
2384 if (!loop->single_exit)
2385 fprintf (vect_dump, "not vectorized: multiple exits.");
2386 else if (loop->num_nodes != 2)
2387 fprintf (vect_dump, "not vectorized: too many BBs in loop.");
2388 else if (EDGE_COUNT (loop->header->preds) != 2)
2389 fprintf (vect_dump, "not vectorized: too many incoming edges.");
2395 /* We assume that the loop exit condition is at the end of the loop. i.e,
2396 that the loop is represented as a do-while (with a proper if-guard
2397 before the loop if needed), where the loop header contains all the
2398 executable statements, and the latch is empty. */
2399 if (!empty_block_p (loop->latch))
2401 if (vect_print_dump_info (REPORT_BAD_FORM_LOOPS, loop_loc))
2402 fprintf (vect_dump, "not vectorized: unexpectd loop form.");
2406 /* Make sure there exists a single-predecessor exit bb: */
2407 if (!single_pred_p (loop->single_exit->dest))
2409 edge e = loop->single_exit;
2410 if (!(e->flags & EDGE_ABNORMAL))
2412 loop_split_edge_with (e, NULL);
2413 if (vect_print_dump_info (REPORT_DETAILS, loop_loc))
2414 fprintf (vect_dump, "split exit edge.");
2418 if (vect_print_dump_info (REPORT_BAD_FORM_LOOPS, loop_loc))
2419 fprintf (vect_dump, "not vectorized: abnormal loop exit edge.");
2424 if (empty_block_p (loop->header))
2426 if (vect_print_dump_info (REPORT_BAD_FORM_LOOPS, loop_loc))
2427 fprintf (vect_dump, "not vectorized: empty loop.");
2431 loop_cond = vect_get_loop_niters (loop, &number_of_iterations);
2434 if (vect_print_dump_info (REPORT_BAD_FORM_LOOPS, loop_loc))
2435 fprintf (vect_dump, "not vectorized: complicated exit condition.");
2439 if (!number_of_iterations)
2441 if (vect_print_dump_info (REPORT_BAD_FORM_LOOPS, loop_loc))
2443 "not vectorized: number of iterations cannot be computed.");
2447 if (chrec_contains_undetermined (number_of_iterations))
2449 if (vect_print_dump_info (REPORT_BAD_FORM_LOOPS, loop_loc))
2450 fprintf (vect_dump, "Infinite number of iterations.");
2454 loop_vinfo = new_loop_vec_info (loop);
2455 LOOP_VINFO_NITERS (loop_vinfo) = number_of_iterations;
2457 if (!LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo))
2459 if (vect_print_dump_info (REPORT_DETAILS, loop_loc))
2461 fprintf (vect_dump, "Symbolic number of iterations is ");
2462 print_generic_expr (vect_dump, number_of_iterations, TDF_DETAILS);
2466 if (LOOP_VINFO_INT_NITERS (loop_vinfo) == 0)
2468 if (vect_print_dump_info (REPORT_UNVECTORIZED_LOOPS, loop_loc))
2469 fprintf (vect_dump, "not vectorized: number of iterations = 0.");
2473 LOOP_VINFO_EXIT_COND (loop_vinfo) = loop_cond;
2474 LOOP_VINFO_LOC (loop_vinfo) = loop_loc;
2480 /* Function vect_analyze_loop.
2482 Apply a set of analyses on LOOP, and create a loop_vec_info struct
2483 for it. The different analyses will record information in the
2484 loop_vec_info struct. */
2486 vect_analyze_loop (struct loop *loop)
2489 loop_vec_info loop_vinfo;
2491 if (vect_print_dump_info (REPORT_DETAILS, UNKNOWN_LOC))
2492 fprintf (vect_dump, "===== analyze_loop_nest =====");
2494 /* Check the CFG characteristics of the loop (nesting, entry/exit, etc. */
2496 loop_vinfo = vect_analyze_loop_form (loop);
2499 if (vect_print_dump_info (REPORT_DETAILS, UNKNOWN_LOC))
2500 fprintf (vect_dump, "bad loop form.");
2504 /* Find all data references in the loop (which correspond to vdefs/vuses)
2505 and analyze their evolution in the loop.
2507 FORNOW: Handle only simple, array references, which
2508 alignment can be forced, and aligned pointer-references. */
2510 ok = vect_analyze_data_refs (loop_vinfo);
2513 if (vect_print_dump_info (REPORT_DETAILS, LOOP_LOC (loop_vinfo)))
2514 fprintf (vect_dump, "bad data references.");
2515 destroy_loop_vec_info (loop_vinfo);
2519 /* Data-flow analysis to detect stmts that do not need to be vectorized. */
2521 ok = vect_mark_stmts_to_be_vectorized (loop_vinfo);
2524 if (vect_print_dump_info (REPORT_DETAILS, LOOP_LOC (loop_vinfo)))
2525 fprintf (vect_dump, "unexpected pattern.");
2526 destroy_loop_vec_info (loop_vinfo);
2530 /* Check that all cross-iteration scalar data-flow cycles are OK.
2531 Cross-iteration cycles caused by virtual phis are analyzed separately. */
2533 ok = vect_analyze_scalar_cycles (loop_vinfo);
2536 if (vect_print_dump_info (REPORT_DETAILS, LOOP_LOC (loop_vinfo)))
2537 fprintf (vect_dump, "bad scalar cycle.");
2538 destroy_loop_vec_info (loop_vinfo);
2542 /* Analyze data dependences between the data-refs in the loop.
2543 FORNOW: fail at the first data dependence that we encounter. */
2545 ok = vect_analyze_data_ref_dependences (loop_vinfo);
2548 if (vect_print_dump_info (REPORT_DETAILS, LOOP_LOC (loop_vinfo)))
2549 fprintf (vect_dump, "bad data dependence.");
2550 destroy_loop_vec_info (loop_vinfo);
2554 /* Analyze the access patterns of the data-refs in the loop (consecutive,
2555 complex, etc.). FORNOW: Only handle consecutive access pattern. */
2557 ok = vect_analyze_data_ref_accesses (loop_vinfo);
2560 if (vect_print_dump_info (REPORT_DETAILS, LOOP_LOC (loop_vinfo)))
2561 fprintf (vect_dump, "bad data access.");
2562 destroy_loop_vec_info (loop_vinfo);
2566 ok = vect_determine_vectorization_factor (loop_vinfo);
2569 if (vect_print_dump_info (REPORT_DETAILS, LOOP_LOC (loop_vinfo)))
2570 fprintf (vect_dump, "can't determine vectorization factor.");
2571 destroy_loop_vec_info (loop_vinfo);
2575 /* Analyze the alignment of the data-refs in the loop.
2576 FORNOW: Only aligned accesses are handled. */
2578 ok = vect_analyze_data_refs_alignment (loop_vinfo);
2581 if (vect_print_dump_info (REPORT_DETAILS, LOOP_LOC (loop_vinfo)))
2582 fprintf (vect_dump, "bad data alignment.");
2583 destroy_loop_vec_info (loop_vinfo);
2587 /* Scan all the operations in the loop and make sure they are
2590 ok = vect_analyze_operations (loop_vinfo);
2593 if (vect_print_dump_info (REPORT_DETAILS, LOOP_LOC (loop_vinfo)))
2594 fprintf (vect_dump, "bad operation or unsupported loop bound.");
2595 destroy_loop_vec_info (loop_vinfo);
2599 LOOP_VINFO_VECTORIZABLE_P (loop_vinfo) = 1;