2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009 Free Software
4 Contributed by Dorit Naishlos <dorit@il.ibm.com> and
5 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"
29 #include "basic-block.h"
30 #include "diagnostic.h"
31 #include "tree-flow.h"
32 #include "tree-dump.h"
34 #include "cfglayout.h"
40 #include "tree-chrec.h"
41 #include "tree-scalar-evolution.h"
42 #include "tree-vectorizer.h"
44 /* Loop Vectorization Pass.
46 This pass tries to vectorize loops.
48 For example, the vectorizer transforms the following simple loop:
50 short a[N]; short b[N]; short c[N]; int i;
56 as if it was manually vectorized by rewriting the source code into:
58 typedef int __attribute__((mode(V8HI))) v8hi;
59 short a[N]; short b[N]; short c[N]; int i;
60 v8hi *pa = (v8hi*)a, *pb = (v8hi*)b, *pc = (v8hi*)c;
63 for (i=0; i<N/8; i++){
70 The main entry to this pass is vectorize_loops(), in which
71 the vectorizer applies a set of analyses on a given set of loops,
72 followed by the actual vectorization transformation for the loops that
73 had successfully passed the analysis phase.
74 Throughout this pass we make a distinction between two types of
75 data: scalars (which are represented by SSA_NAMES), and memory references
76 ("data-refs"). These two types of data require different handling both
77 during analysis and transformation. The types of data-refs that the
78 vectorizer currently supports are ARRAY_REFS which base is an array DECL
79 (not a pointer), and INDIRECT_REFS through pointers; both array and pointer
80 accesses are required to have a simple (consecutive) access pattern.
84 The driver for the analysis phase is vect_analyze_loop().
85 It applies a set of analyses, some of which rely on the scalar evolution
86 analyzer (scev) developed by Sebastian Pop.
88 During the analysis phase the vectorizer records some information
89 per stmt in a "stmt_vec_info" struct which is attached to each stmt in the
90 loop, as well as general information about the loop as a whole, which is
91 recorded in a "loop_vec_info" struct attached to each loop.
95 The loop transformation phase scans all the stmts in the loop, and
96 creates a vector stmt (or a sequence of stmts) for each scalar stmt S in
97 the loop that needs to be vectorized. It inserts the vector code sequence
98 just before the scalar stmt S, and records a pointer to the vector code
99 in STMT_VINFO_VEC_STMT (stmt_info) (stmt_info is the stmt_vec_info struct
100 attached to S). This pointer will be used for the vectorization of following
101 stmts which use the def of stmt S. Stmt S is removed if it writes to memory;
102 otherwise, we rely on dead code elimination for removing it.
104 For example, say stmt S1 was vectorized into stmt VS1:
107 S1: b = x[i]; STMT_VINFO_VEC_STMT (stmt_info (S1)) = VS1
110 To vectorize stmt S2, the vectorizer first finds the stmt that defines
111 the operand 'b' (S1), and gets the relevant vector def 'vb' from the
112 vector stmt VS1 pointed to by STMT_VINFO_VEC_STMT (stmt_info (S1)). The
113 resulting sequence would be:
116 S1: b = x[i]; STMT_VINFO_VEC_STMT (stmt_info (S1)) = VS1
118 S2: a = b; STMT_VINFO_VEC_STMT (stmt_info (S2)) = VS2
120 Operands that are not SSA_NAMEs, are data-refs that appear in
121 load/store operations (like 'x[i]' in S1), and are handled differently.
125 Currently the only target specific information that is used is the
126 size of the vector (in bytes) - "UNITS_PER_SIMD_WORD". Targets that can
127 support different sizes of vectors, for now will need to specify one value
128 for "UNITS_PER_SIMD_WORD". More flexibility will be added in the future.
130 Since we only vectorize operations which vector form can be
131 expressed using existing tree codes, to verify that an operation is
132 supported, the vectorizer checks the relevant optab at the relevant
133 machine_mode (e.g, optab_handler (add_optab, V8HImode)->insn_code). If
134 the value found is CODE_FOR_nothing, then there's no target support, and
135 we can't vectorize the stmt.
137 For additional information on this project see:
138 http://gcc.gnu.org/projects/tree-ssa/vectorization.html
141 /* Function vect_determine_vectorization_factor
143 Determine the vectorization factor (VF). VF is the number of data elements
144 that are operated upon in parallel in a single iteration of the vectorized
145 loop. For example, when vectorizing a loop that operates on 4byte elements,
146 on a target with vector size (VS) 16byte, the VF is set to 4, since 4
147 elements can fit in a single vector register.
149 We currently support vectorization of loops in which all types operated upon
150 are of the same size. Therefore this function currently sets VF according to
151 the size of the types operated upon, and fails if there are multiple sizes
154 VF is also the factor by which the loop iterations are strip-mined, e.g.:
161 for (i=0; i<N; i+=VF){
162 a[i:VF] = b[i:VF] + c[i:VF];
167 vect_determine_vectorization_factor (loop_vec_info loop_vinfo
)
169 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
170 basic_block
*bbs
= LOOP_VINFO_BBS (loop_vinfo
);
171 int nbbs
= loop
->num_nodes
;
172 gimple_stmt_iterator si
;
173 unsigned int vectorization_factor
= 0;
178 stmt_vec_info stmt_info
;
182 if (vect_print_dump_info (REPORT_DETAILS
))
183 fprintf (vect_dump
, "=== vect_determine_vectorization_factor ===");
185 for (i
= 0; i
< nbbs
; i
++)
187 basic_block bb
= bbs
[i
];
189 for (si
= gsi_start_phis (bb
); !gsi_end_p (si
); gsi_next (&si
))
192 stmt_info
= vinfo_for_stmt (phi
);
193 if (vect_print_dump_info (REPORT_DETAILS
))
195 fprintf (vect_dump
, "==> examining phi: ");
196 print_gimple_stmt (vect_dump
, phi
, 0, TDF_SLIM
);
199 gcc_assert (stmt_info
);
201 if (STMT_VINFO_RELEVANT_P (stmt_info
))
203 gcc_assert (!STMT_VINFO_VECTYPE (stmt_info
));
204 scalar_type
= TREE_TYPE (PHI_RESULT (phi
));
206 if (vect_print_dump_info (REPORT_DETAILS
))
208 fprintf (vect_dump
, "get vectype for scalar type: ");
209 print_generic_expr (vect_dump
, scalar_type
, TDF_SLIM
);
212 vectype
= get_vectype_for_scalar_type (scalar_type
);
215 if (vect_print_dump_info (REPORT_UNVECTORIZED_LOCATIONS
))
218 "not vectorized: unsupported data-type ");
219 print_generic_expr (vect_dump
, scalar_type
, TDF_SLIM
);
223 STMT_VINFO_VECTYPE (stmt_info
) = vectype
;
225 if (vect_print_dump_info (REPORT_DETAILS
))
227 fprintf (vect_dump
, "vectype: ");
228 print_generic_expr (vect_dump
, vectype
, TDF_SLIM
);
231 nunits
= TYPE_VECTOR_SUBPARTS (vectype
);
232 if (vect_print_dump_info (REPORT_DETAILS
))
233 fprintf (vect_dump
, "nunits = %d", nunits
);
235 if (!vectorization_factor
236 || (nunits
> vectorization_factor
))
237 vectorization_factor
= nunits
;
241 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); gsi_next (&si
))
243 gimple stmt
= gsi_stmt (si
);
244 stmt_info
= vinfo_for_stmt (stmt
);
246 if (vect_print_dump_info (REPORT_DETAILS
))
248 fprintf (vect_dump
, "==> examining statement: ");
249 print_gimple_stmt (vect_dump
, stmt
, 0, TDF_SLIM
);
252 gcc_assert (stmt_info
);
254 /* skip stmts which do not need to be vectorized. */
255 if (!STMT_VINFO_RELEVANT_P (stmt_info
)
256 && !STMT_VINFO_LIVE_P (stmt_info
))
258 if (vect_print_dump_info (REPORT_DETAILS
))
259 fprintf (vect_dump
, "skip.");
263 if (gimple_get_lhs (stmt
) == NULL_TREE
)
265 if (vect_print_dump_info (REPORT_UNVECTORIZED_LOCATIONS
))
267 fprintf (vect_dump
, "not vectorized: irregular stmt.");
268 print_gimple_stmt (vect_dump
, stmt
, 0, TDF_SLIM
);
273 if (VECTOR_MODE_P (TYPE_MODE (gimple_expr_type (stmt
))))
275 if (vect_print_dump_info (REPORT_UNVECTORIZED_LOCATIONS
))
277 fprintf (vect_dump
, "not vectorized: vector stmt in loop:");
278 print_gimple_stmt (vect_dump
, stmt
, 0, TDF_SLIM
);
283 if (STMT_VINFO_VECTYPE (stmt_info
))
285 /* The only case when a vectype had been already set is for stmts
286 that contain a dataref, or for "pattern-stmts" (stmts generated
287 by the vectorizer to represent/replace a certain idiom). */
288 gcc_assert (STMT_VINFO_DATA_REF (stmt_info
)
289 || is_pattern_stmt_p (stmt_info
));
290 vectype
= STMT_VINFO_VECTYPE (stmt_info
);
294 gcc_assert (!STMT_VINFO_DATA_REF (stmt_info
)
295 && !is_pattern_stmt_p (stmt_info
));
297 scalar_type
= vect_get_smallest_scalar_type (stmt
, &dummy
,
299 if (vect_print_dump_info (REPORT_DETAILS
))
301 fprintf (vect_dump
, "get vectype for scalar type: ");
302 print_generic_expr (vect_dump
, scalar_type
, TDF_SLIM
);
305 vectype
= get_vectype_for_scalar_type (scalar_type
);
308 if (vect_print_dump_info (REPORT_UNVECTORIZED_LOCATIONS
))
311 "not vectorized: unsupported data-type ");
312 print_generic_expr (vect_dump
, scalar_type
, TDF_SLIM
);
316 STMT_VINFO_VECTYPE (stmt_info
) = vectype
;
319 if (vect_print_dump_info (REPORT_DETAILS
))
321 fprintf (vect_dump
, "vectype: ");
322 print_generic_expr (vect_dump
, vectype
, TDF_SLIM
);
325 nunits
= TYPE_VECTOR_SUBPARTS (vectype
);
326 if (vect_print_dump_info (REPORT_DETAILS
))
327 fprintf (vect_dump
, "nunits = %d", nunits
);
329 if (!vectorization_factor
330 || (nunits
> vectorization_factor
))
331 vectorization_factor
= nunits
;
336 /* TODO: Analyze cost. Decide if worth while to vectorize. */
337 if (vect_print_dump_info (REPORT_DETAILS
))
338 fprintf (vect_dump
, "vectorization factor = %d", vectorization_factor
);
339 if (vectorization_factor
<= 1)
341 if (vect_print_dump_info (REPORT_UNVECTORIZED_LOCATIONS
))
342 fprintf (vect_dump
, "not vectorized: unsupported data-type");
345 LOOP_VINFO_VECT_FACTOR (loop_vinfo
) = vectorization_factor
;
351 /* Function vect_is_simple_iv_evolution.
353 FORNOW: A simple evolution of an induction variables in the loop is
354 considered a polynomial evolution with constant step. */
357 vect_is_simple_iv_evolution (unsigned loop_nb
, tree access_fn
, tree
* init
,
362 tree evolution_part
= evolution_part_in_loop_num (access_fn
, loop_nb
);
364 /* When there is no evolution in this loop, the evolution function
366 if (evolution_part
== NULL_TREE
)
369 /* When the evolution is a polynomial of degree >= 2
370 the evolution function is not "simple". */
371 if (tree_is_chrec (evolution_part
))
374 step_expr
= evolution_part
;
375 init_expr
= unshare_expr (initial_condition_in_loop_num (access_fn
, loop_nb
));
377 if (vect_print_dump_info (REPORT_DETAILS
))
379 fprintf (vect_dump
, "step: ");
380 print_generic_expr (vect_dump
, step_expr
, TDF_SLIM
);
381 fprintf (vect_dump
, ", init: ");
382 print_generic_expr (vect_dump
, init_expr
, TDF_SLIM
);
388 if (TREE_CODE (step_expr
) != INTEGER_CST
)
390 if (vect_print_dump_info (REPORT_DETAILS
))
391 fprintf (vect_dump
, "step unknown.");
398 /* Function vect_analyze_scalar_cycles_1.
400 Examine the cross iteration def-use cycles of scalar variables
401 in LOOP. LOOP_VINFO represents the loop that is now being
402 considered for vectorization (can be LOOP, or an outer-loop
406 vect_analyze_scalar_cycles_1 (loop_vec_info loop_vinfo
, struct loop
*loop
)
408 basic_block bb
= loop
->header
;
410 VEC(gimple
,heap
) *worklist
= VEC_alloc (gimple
, heap
, 64);
411 gimple_stmt_iterator gsi
;
414 if (vect_print_dump_info (REPORT_DETAILS
))
415 fprintf (vect_dump
, "=== vect_analyze_scalar_cycles ===");
417 /* First - identify all inductions. Reduction detection assumes that all the
418 inductions have been identified, therefore, this order must not be
420 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
422 gimple phi
= gsi_stmt (gsi
);
423 tree access_fn
= NULL
;
424 tree def
= PHI_RESULT (phi
);
425 stmt_vec_info stmt_vinfo
= vinfo_for_stmt (phi
);
427 if (vect_print_dump_info (REPORT_DETAILS
))
429 fprintf (vect_dump
, "Analyze phi: ");
430 print_gimple_stmt (vect_dump
, phi
, 0, TDF_SLIM
);
433 /* Skip virtual phi's. The data dependences that are associated with
434 virtual defs/uses (i.e., memory accesses) are analyzed elsewhere. */
435 if (!is_gimple_reg (SSA_NAME_VAR (def
)))
438 STMT_VINFO_DEF_TYPE (stmt_vinfo
) = vect_unknown_def_type
;
440 /* Analyze the evolution function. */
441 access_fn
= analyze_scalar_evolution (loop
, def
);
442 if (access_fn
&& vect_print_dump_info (REPORT_DETAILS
))
444 fprintf (vect_dump
, "Access function of PHI: ");
445 print_generic_expr (vect_dump
, access_fn
, TDF_SLIM
);
449 || !vect_is_simple_iv_evolution (loop
->num
, access_fn
, &dumy
, &dumy
))
451 VEC_safe_push (gimple
, heap
, worklist
, phi
);
455 if (vect_print_dump_info (REPORT_DETAILS
))
456 fprintf (vect_dump
, "Detected induction.");
457 STMT_VINFO_DEF_TYPE (stmt_vinfo
) = vect_induction_def
;
461 /* Second - identify all reductions and nested cycles. */
462 while (VEC_length (gimple
, worklist
) > 0)
464 gimple phi
= VEC_pop (gimple
, worklist
);
465 tree def
= PHI_RESULT (phi
);
466 stmt_vec_info stmt_vinfo
= vinfo_for_stmt (phi
);
470 if (vect_print_dump_info (REPORT_DETAILS
))
472 fprintf (vect_dump
, "Analyze phi: ");
473 print_gimple_stmt (vect_dump
, phi
, 0, TDF_SLIM
);
476 gcc_assert (is_gimple_reg (SSA_NAME_VAR (def
)));
477 gcc_assert (STMT_VINFO_DEF_TYPE (stmt_vinfo
) == vect_unknown_def_type
);
479 nested_cycle
= (loop
!= LOOP_VINFO_LOOP (loop_vinfo
));
480 reduc_stmt
= vect_is_simple_reduction (loop_vinfo
, phi
, !nested_cycle
,
486 if (vect_print_dump_info (REPORT_DETAILS
))
487 fprintf (vect_dump
, "Detected double reduction.");
489 STMT_VINFO_DEF_TYPE (stmt_vinfo
) = vect_double_reduction_def
;
490 STMT_VINFO_DEF_TYPE (vinfo_for_stmt (reduc_stmt
)) =
491 vect_double_reduction_def
;
497 if (vect_print_dump_info (REPORT_DETAILS
))
498 fprintf (vect_dump
, "Detected vectorizable nested cycle.");
500 STMT_VINFO_DEF_TYPE (stmt_vinfo
) = vect_nested_cycle
;
501 STMT_VINFO_DEF_TYPE (vinfo_for_stmt (reduc_stmt
)) =
506 if (vect_print_dump_info (REPORT_DETAILS
))
507 fprintf (vect_dump
, "Detected reduction.");
509 STMT_VINFO_DEF_TYPE (stmt_vinfo
) = vect_reduction_def
;
510 STMT_VINFO_DEF_TYPE (vinfo_for_stmt (reduc_stmt
)) =
516 if (vect_print_dump_info (REPORT_DETAILS
))
517 fprintf (vect_dump
, "Unknown def-use cycle pattern.");
520 VEC_free (gimple
, heap
, worklist
);
524 /* Function vect_analyze_scalar_cycles.
526 Examine the cross iteration def-use cycles of scalar variables, by
527 analyzing the loop-header PHIs of scalar variables; Classify each
528 cycle as one of the following: invariant, induction, reduction, unknown.
529 We do that for the loop represented by LOOP_VINFO, and also to its
530 inner-loop, if exists.
531 Examples for scalar cycles:
546 vect_analyze_scalar_cycles (loop_vec_info loop_vinfo
)
548 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
550 vect_analyze_scalar_cycles_1 (loop_vinfo
, loop
);
552 /* When vectorizing an outer-loop, the inner-loop is executed sequentially.
553 Reductions in such inner-loop therefore have different properties than
554 the reductions in the nest that gets vectorized:
555 1. When vectorized, they are executed in the same order as in the original
556 scalar loop, so we can't change the order of computation when
558 2. FIXME: Inner-loop reductions can be used in the inner-loop, so the
559 current checks are too strict. */
562 vect_analyze_scalar_cycles_1 (loop_vinfo
, loop
->inner
);
565 /* Function vect_get_loop_niters.
567 Determine how many iterations the loop is executed.
568 If an expression that represents the number of iterations
569 can be constructed, place it in NUMBER_OF_ITERATIONS.
570 Return the loop exit condition. */
573 vect_get_loop_niters (struct loop
*loop
, tree
*number_of_iterations
)
577 if (vect_print_dump_info (REPORT_DETAILS
))
578 fprintf (vect_dump
, "=== get_loop_niters ===");
580 niters
= number_of_exit_cond_executions (loop
);
582 if (niters
!= NULL_TREE
583 && niters
!= chrec_dont_know
)
585 *number_of_iterations
= niters
;
587 if (vect_print_dump_info (REPORT_DETAILS
))
589 fprintf (vect_dump
, "==> get_loop_niters:" );
590 print_generic_expr (vect_dump
, *number_of_iterations
, TDF_SLIM
);
594 return get_loop_exit_condition (loop
);
598 /* Function bb_in_loop_p
600 Used as predicate for dfs order traversal of the loop bbs. */
603 bb_in_loop_p (const_basic_block bb
, const void *data
)
605 const struct loop
*const loop
= (const struct loop
*)data
;
606 if (flow_bb_inside_loop_p (loop
, bb
))
612 /* Function new_loop_vec_info.
614 Create and initialize a new loop_vec_info struct for LOOP, as well as
615 stmt_vec_info structs for all the stmts in LOOP. */
618 new_loop_vec_info (struct loop
*loop
)
622 gimple_stmt_iterator si
;
623 unsigned int i
, nbbs
;
625 res
= (loop_vec_info
) xcalloc (1, sizeof (struct _loop_vec_info
));
626 LOOP_VINFO_LOOP (res
) = loop
;
628 bbs
= get_loop_body (loop
);
630 /* Create/Update stmt_info for all stmts in the loop. */
631 for (i
= 0; i
< loop
->num_nodes
; i
++)
633 basic_block bb
= bbs
[i
];
635 /* BBs in a nested inner-loop will have been already processed (because
636 we will have called vect_analyze_loop_form for any nested inner-loop).
637 Therefore, for stmts in an inner-loop we just want to update the
638 STMT_VINFO_LOOP_VINFO field of their stmt_info to point to the new
639 loop_info of the outer-loop we are currently considering to vectorize
640 (instead of the loop_info of the inner-loop).
641 For stmts in other BBs we need to create a stmt_info from scratch. */
642 if (bb
->loop_father
!= loop
)
645 gcc_assert (loop
->inner
&& bb
->loop_father
== loop
->inner
);
646 for (si
= gsi_start_phis (bb
); !gsi_end_p (si
); gsi_next (&si
))
648 gimple phi
= gsi_stmt (si
);
649 stmt_vec_info stmt_info
= vinfo_for_stmt (phi
);
650 loop_vec_info inner_loop_vinfo
=
651 STMT_VINFO_LOOP_VINFO (stmt_info
);
652 gcc_assert (loop
->inner
== LOOP_VINFO_LOOP (inner_loop_vinfo
));
653 STMT_VINFO_LOOP_VINFO (stmt_info
) = res
;
655 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); gsi_next (&si
))
657 gimple stmt
= gsi_stmt (si
);
658 stmt_vec_info stmt_info
= vinfo_for_stmt (stmt
);
659 loop_vec_info inner_loop_vinfo
=
660 STMT_VINFO_LOOP_VINFO (stmt_info
);
661 gcc_assert (loop
->inner
== LOOP_VINFO_LOOP (inner_loop_vinfo
));
662 STMT_VINFO_LOOP_VINFO (stmt_info
) = res
;
667 /* bb in current nest. */
668 for (si
= gsi_start_phis (bb
); !gsi_end_p (si
); gsi_next (&si
))
670 gimple phi
= gsi_stmt (si
);
671 gimple_set_uid (phi
, 0);
672 set_vinfo_for_stmt (phi
, new_stmt_vec_info (phi
, res
, NULL
));
675 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); gsi_next (&si
))
677 gimple stmt
= gsi_stmt (si
);
678 gimple_set_uid (stmt
, 0);
679 set_vinfo_for_stmt (stmt
, new_stmt_vec_info (stmt
, res
, NULL
));
684 /* CHECKME: We want to visit all BBs before their successors (except for
685 latch blocks, for which this assertion wouldn't hold). In the simple
686 case of the loop forms we allow, a dfs order of the BBs would the same
687 as reversed postorder traversal, so we are safe. */
690 bbs
= XCNEWVEC (basic_block
, loop
->num_nodes
);
691 nbbs
= dfs_enumerate_from (loop
->header
, 0, bb_in_loop_p
,
692 bbs
, loop
->num_nodes
, loop
);
693 gcc_assert (nbbs
== loop
->num_nodes
);
695 LOOP_VINFO_BBS (res
) = bbs
;
696 LOOP_VINFO_NITERS (res
) = NULL
;
697 LOOP_VINFO_NITERS_UNCHANGED (res
) = NULL
;
698 LOOP_VINFO_COST_MODEL_MIN_ITERS (res
) = 0;
699 LOOP_VINFO_VECTORIZABLE_P (res
) = 0;
700 LOOP_PEELING_FOR_ALIGNMENT (res
) = 0;
701 LOOP_VINFO_VECT_FACTOR (res
) = 0;
702 LOOP_VINFO_DATAREFS (res
) = VEC_alloc (data_reference_p
, heap
, 10);
703 LOOP_VINFO_DDRS (res
) = VEC_alloc (ddr_p
, heap
, 10 * 10);
704 LOOP_VINFO_UNALIGNED_DR (res
) = NULL
;
705 LOOP_VINFO_MAY_MISALIGN_STMTS (res
) =
706 VEC_alloc (gimple
, heap
,
707 PARAM_VALUE (PARAM_VECT_MAX_VERSION_FOR_ALIGNMENT_CHECKS
));
708 LOOP_VINFO_MAY_ALIAS_DDRS (res
) =
709 VEC_alloc (ddr_p
, heap
,
710 PARAM_VALUE (PARAM_VECT_MAX_VERSION_FOR_ALIAS_CHECKS
));
711 LOOP_VINFO_STRIDED_STORES (res
) = VEC_alloc (gimple
, heap
, 10);
712 LOOP_VINFO_SLP_INSTANCES (res
) = VEC_alloc (slp_instance
, heap
, 10);
713 LOOP_VINFO_SLP_UNROLLING_FACTOR (res
) = 1;
719 /* Function destroy_loop_vec_info.
721 Free LOOP_VINFO struct, as well as all the stmt_vec_info structs of all the
722 stmts in the loop. */
725 destroy_loop_vec_info (loop_vec_info loop_vinfo
, bool clean_stmts
)
730 gimple_stmt_iterator si
;
732 VEC (slp_instance
, heap
) *slp_instances
;
733 slp_instance instance
;
738 loop
= LOOP_VINFO_LOOP (loop_vinfo
);
740 bbs
= LOOP_VINFO_BBS (loop_vinfo
);
741 nbbs
= loop
->num_nodes
;
745 free (LOOP_VINFO_BBS (loop_vinfo
));
746 free_data_refs (LOOP_VINFO_DATAREFS (loop_vinfo
));
747 free_dependence_relations (LOOP_VINFO_DDRS (loop_vinfo
));
748 VEC_free (gimple
, heap
, LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo
));
755 for (j
= 0; j
< nbbs
; j
++)
757 basic_block bb
= bbs
[j
];
758 for (si
= gsi_start_phis (bb
); !gsi_end_p (si
); gsi_next (&si
))
759 free_stmt_vec_info (gsi_stmt (si
));
761 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); )
763 gimple stmt
= gsi_stmt (si
);
764 stmt_vec_info stmt_info
= vinfo_for_stmt (stmt
);
768 /* Check if this is a "pattern stmt" (introduced by the
769 vectorizer during the pattern recognition pass). */
770 bool remove_stmt_p
= false;
771 gimple orig_stmt
= STMT_VINFO_RELATED_STMT (stmt_info
);
774 stmt_vec_info orig_stmt_info
= vinfo_for_stmt (orig_stmt
);
776 && STMT_VINFO_IN_PATTERN_P (orig_stmt_info
))
777 remove_stmt_p
= true;
780 /* Free stmt_vec_info. */
781 free_stmt_vec_info (stmt
);
783 /* Remove dead "pattern stmts". */
785 gsi_remove (&si
, true);
791 free (LOOP_VINFO_BBS (loop_vinfo
));
792 free_data_refs (LOOP_VINFO_DATAREFS (loop_vinfo
));
793 free_dependence_relations (LOOP_VINFO_DDRS (loop_vinfo
));
794 VEC_free (gimple
, heap
, LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo
));
795 VEC_free (ddr_p
, heap
, LOOP_VINFO_MAY_ALIAS_DDRS (loop_vinfo
));
796 slp_instances
= LOOP_VINFO_SLP_INSTANCES (loop_vinfo
);
797 for (j
= 0; VEC_iterate (slp_instance
, slp_instances
, j
, instance
); j
++)
798 vect_free_slp_instance (instance
);
800 VEC_free (slp_instance
, heap
, LOOP_VINFO_SLP_INSTANCES (loop_vinfo
));
801 VEC_free (gimple
, heap
, LOOP_VINFO_STRIDED_STORES (loop_vinfo
));
808 /* Function vect_analyze_loop_1.
810 Apply a set of analyses on LOOP, and create a loop_vec_info struct
811 for it. The different analyses will record information in the
812 loop_vec_info struct. This is a subset of the analyses applied in
813 vect_analyze_loop, to be applied on an inner-loop nested in the loop
814 that is now considered for (outer-loop) vectorization. */
817 vect_analyze_loop_1 (struct loop
*loop
)
819 loop_vec_info loop_vinfo
;
821 if (vect_print_dump_info (REPORT_DETAILS
))
822 fprintf (vect_dump
, "===== analyze_loop_nest_1 =====");
824 /* Check the CFG characteristics of the loop (nesting, entry/exit, etc. */
826 loop_vinfo
= vect_analyze_loop_form (loop
);
829 if (vect_print_dump_info (REPORT_DETAILS
))
830 fprintf (vect_dump
, "bad inner-loop form.");
838 /* Function vect_analyze_loop_form.
840 Verify that certain CFG restrictions hold, including:
841 - the loop has a pre-header
842 - the loop has a single entry and exit
843 - the loop exit condition is simple enough, and the number of iterations
844 can be analyzed (a countable loop). */
847 vect_analyze_loop_form (struct loop
*loop
)
849 loop_vec_info loop_vinfo
;
851 tree number_of_iterations
= NULL
;
852 loop_vec_info inner_loop_vinfo
= NULL
;
854 if (vect_print_dump_info (REPORT_DETAILS
))
855 fprintf (vect_dump
, "=== vect_analyze_loop_form ===");
857 /* Different restrictions apply when we are considering an inner-most loop,
858 vs. an outer (nested) loop.
859 (FORNOW. May want to relax some of these restrictions in the future). */
863 /* Inner-most loop. We currently require that the number of BBs is
864 exactly 2 (the header and latch). Vectorizable inner-most loops
875 if (loop
->num_nodes
!= 2)
877 if (vect_print_dump_info (REPORT_BAD_FORM_LOOPS
))
878 fprintf (vect_dump
, "not vectorized: control flow in loop.");
882 if (empty_block_p (loop
->header
))
884 if (vect_print_dump_info (REPORT_BAD_FORM_LOOPS
))
885 fprintf (vect_dump
, "not vectorized: empty loop.");
891 struct loop
*innerloop
= loop
->inner
;
892 edge backedge
, entryedge
;
894 /* Nested loop. We currently require that the loop is doubly-nested,
895 contains a single inner loop, and the number of BBs is exactly 5.
896 Vectorizable outer-loops look like this:
908 The inner-loop has the properties expected of inner-most loops
909 as described above. */
911 if ((loop
->inner
)->inner
|| (loop
->inner
)->next
)
913 if (vect_print_dump_info (REPORT_BAD_FORM_LOOPS
))
914 fprintf (vect_dump
, "not vectorized: multiple nested loops.");
918 /* Analyze the inner-loop. */
919 inner_loop_vinfo
= vect_analyze_loop_1 (loop
->inner
);
920 if (!inner_loop_vinfo
)
922 if (vect_print_dump_info (REPORT_BAD_FORM_LOOPS
))
923 fprintf (vect_dump
, "not vectorized: Bad inner loop.");
927 if (!expr_invariant_in_loop_p (loop
,
928 LOOP_VINFO_NITERS (inner_loop_vinfo
)))
930 if (vect_print_dump_info (REPORT_BAD_FORM_LOOPS
))
932 "not vectorized: inner-loop count not invariant.");
933 destroy_loop_vec_info (inner_loop_vinfo
, true);
937 if (loop
->num_nodes
!= 5)
939 if (vect_print_dump_info (REPORT_BAD_FORM_LOOPS
))
940 fprintf (vect_dump
, "not vectorized: control flow in loop.");
941 destroy_loop_vec_info (inner_loop_vinfo
, true);
945 gcc_assert (EDGE_COUNT (innerloop
->header
->preds
) == 2);
946 backedge
= EDGE_PRED (innerloop
->header
, 1);
947 entryedge
= EDGE_PRED (innerloop
->header
, 0);
948 if (EDGE_PRED (innerloop
->header
, 0)->src
== innerloop
->latch
)
950 backedge
= EDGE_PRED (innerloop
->header
, 0);
951 entryedge
= EDGE_PRED (innerloop
->header
, 1);
954 if (entryedge
->src
!= loop
->header
955 || !single_exit (innerloop
)
956 || single_exit (innerloop
)->dest
!= EDGE_PRED (loop
->latch
, 0)->src
)
958 if (vect_print_dump_info (REPORT_BAD_FORM_LOOPS
))
959 fprintf (vect_dump
, "not vectorized: unsupported outerloop form.");
960 destroy_loop_vec_info (inner_loop_vinfo
, true);
964 if (vect_print_dump_info (REPORT_DETAILS
))
965 fprintf (vect_dump
, "Considering outer-loop vectorization.");
968 if (!single_exit (loop
)
969 || EDGE_COUNT (loop
->header
->preds
) != 2)
971 if (vect_print_dump_info (REPORT_BAD_FORM_LOOPS
))
973 if (!single_exit (loop
))
974 fprintf (vect_dump
, "not vectorized: multiple exits.");
975 else if (EDGE_COUNT (loop
->header
->preds
) != 2)
976 fprintf (vect_dump
, "not vectorized: too many incoming edges.");
978 if (inner_loop_vinfo
)
979 destroy_loop_vec_info (inner_loop_vinfo
, true);
983 /* We assume that the loop exit condition is at the end of the loop. i.e,
984 that the loop is represented as a do-while (with a proper if-guard
985 before the loop if needed), where the loop header contains all the
986 executable statements, and the latch is empty. */
987 if (!empty_block_p (loop
->latch
)
988 || phi_nodes (loop
->latch
))
990 if (vect_print_dump_info (REPORT_BAD_FORM_LOOPS
))
991 fprintf (vect_dump
, "not vectorized: unexpected loop form.");
992 if (inner_loop_vinfo
)
993 destroy_loop_vec_info (inner_loop_vinfo
, true);
997 /* Make sure there exists a single-predecessor exit bb: */
998 if (!single_pred_p (single_exit (loop
)->dest
))
1000 edge e
= single_exit (loop
);
1001 if (!(e
->flags
& EDGE_ABNORMAL
))
1003 split_loop_exit_edge (e
);
1004 if (vect_print_dump_info (REPORT_DETAILS
))
1005 fprintf (vect_dump
, "split exit edge.");
1009 if (vect_print_dump_info (REPORT_BAD_FORM_LOOPS
))
1010 fprintf (vect_dump
, "not vectorized: abnormal loop exit edge.");
1011 if (inner_loop_vinfo
)
1012 destroy_loop_vec_info (inner_loop_vinfo
, true);
1017 loop_cond
= vect_get_loop_niters (loop
, &number_of_iterations
);
1020 if (vect_print_dump_info (REPORT_BAD_FORM_LOOPS
))
1021 fprintf (vect_dump
, "not vectorized: complicated exit condition.");
1022 if (inner_loop_vinfo
)
1023 destroy_loop_vec_info (inner_loop_vinfo
, true);
1027 if (!number_of_iterations
)
1029 if (vect_print_dump_info (REPORT_BAD_FORM_LOOPS
))
1031 "not vectorized: number of iterations cannot be computed.");
1032 if (inner_loop_vinfo
)
1033 destroy_loop_vec_info (inner_loop_vinfo
, true);
1037 if (chrec_contains_undetermined (number_of_iterations
))
1039 if (vect_print_dump_info (REPORT_BAD_FORM_LOOPS
))
1040 fprintf (vect_dump
, "Infinite number of iterations.");
1041 if (inner_loop_vinfo
)
1042 destroy_loop_vec_info (inner_loop_vinfo
, true);
1046 if (!NITERS_KNOWN_P (number_of_iterations
))
1048 if (vect_print_dump_info (REPORT_DETAILS
))
1050 fprintf (vect_dump
, "Symbolic number of iterations is ");
1051 print_generic_expr (vect_dump
, number_of_iterations
, TDF_DETAILS
);
1054 else if (TREE_INT_CST_LOW (number_of_iterations
) == 0)
1056 if (vect_print_dump_info (REPORT_UNVECTORIZED_LOCATIONS
))
1057 fprintf (vect_dump
, "not vectorized: number of iterations = 0.");
1058 if (inner_loop_vinfo
)
1059 destroy_loop_vec_info (inner_loop_vinfo
, false);
1063 loop_vinfo
= new_loop_vec_info (loop
);
1064 LOOP_VINFO_NITERS (loop_vinfo
) = number_of_iterations
;
1065 LOOP_VINFO_NITERS_UNCHANGED (loop_vinfo
) = number_of_iterations
;
1067 STMT_VINFO_TYPE (vinfo_for_stmt (loop_cond
)) = loop_exit_ctrl_vec_info_type
;
1069 /* CHECKME: May want to keep it around it in the future. */
1070 if (inner_loop_vinfo
)
1071 destroy_loop_vec_info (inner_loop_vinfo
, false);
1073 gcc_assert (!loop
->aux
);
1074 loop
->aux
= loop_vinfo
;
1079 /* Function vect_analyze_loop_operations.
1081 Scan the loop stmts and make sure they are all vectorizable. */
1084 vect_analyze_loop_operations (loop_vec_info loop_vinfo
)
1086 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
1087 basic_block
*bbs
= LOOP_VINFO_BBS (loop_vinfo
);
1088 int nbbs
= loop
->num_nodes
;
1089 gimple_stmt_iterator si
;
1090 unsigned int vectorization_factor
= 0;
1093 stmt_vec_info stmt_info
;
1094 bool need_to_vectorize
= false;
1095 int min_profitable_iters
;
1096 int min_scalar_loop_bound
;
1098 bool only_slp_in_loop
= true, ok
;
1100 if (vect_print_dump_info (REPORT_DETAILS
))
1101 fprintf (vect_dump
, "=== vect_analyze_loop_operations ===");
1103 gcc_assert (LOOP_VINFO_VECT_FACTOR (loop_vinfo
));
1104 vectorization_factor
= LOOP_VINFO_VECT_FACTOR (loop_vinfo
);
1106 for (i
= 0; i
< nbbs
; i
++)
1108 basic_block bb
= bbs
[i
];
1110 for (si
= gsi_start_phis (bb
); !gsi_end_p (si
); gsi_next (&si
))
1112 phi
= gsi_stmt (si
);
1115 stmt_info
= vinfo_for_stmt (phi
);
1116 if (vect_print_dump_info (REPORT_DETAILS
))
1118 fprintf (vect_dump
, "examining phi: ");
1119 print_gimple_stmt (vect_dump
, phi
, 0, TDF_SLIM
);
1122 if (! is_loop_header_bb_p (bb
))
1124 /* inner-loop loop-closed exit phi in outer-loop vectorization
1125 (i.e. a phi in the tail of the outer-loop).
1126 FORNOW: we currently don't support the case that these phis
1127 are not used in the outerloop (unless it is double reduction,
1128 i.e., this phi is vect_reduction_def), cause this case
1129 requires to actually do something here. */
1130 if ((!STMT_VINFO_RELEVANT_P (stmt_info
)
1131 || STMT_VINFO_LIVE_P (stmt_info
))
1132 && STMT_VINFO_DEF_TYPE (stmt_info
)
1133 != vect_double_reduction_def
)
1135 if (vect_print_dump_info (REPORT_DETAILS
))
1137 "Unsupported loop-closed phi in outer-loop.");
1143 gcc_assert (stmt_info
);
1145 if (STMT_VINFO_LIVE_P (stmt_info
))
1147 /* FORNOW: not yet supported. */
1148 if (vect_print_dump_info (REPORT_UNVECTORIZED_LOCATIONS
))
1149 fprintf (vect_dump
, "not vectorized: value used after loop.");
1153 if (STMT_VINFO_RELEVANT (stmt_info
) == vect_used_in_scope
1154 && STMT_VINFO_DEF_TYPE (stmt_info
) != vect_induction_def
)
1156 /* A scalar-dependence cycle that we don't support. */
1157 if (vect_print_dump_info (REPORT_UNVECTORIZED_LOCATIONS
))
1158 fprintf (vect_dump
, "not vectorized: scalar dependence cycle.");
1162 if (STMT_VINFO_RELEVANT_P (stmt_info
))
1164 need_to_vectorize
= true;
1165 if (STMT_VINFO_DEF_TYPE (stmt_info
) == vect_induction_def
)
1166 ok
= vectorizable_induction (phi
, NULL
, NULL
);
1171 if (vect_print_dump_info (REPORT_UNVECTORIZED_LOCATIONS
))
1174 "not vectorized: relevant phi not supported: ");
1175 print_gimple_stmt (vect_dump
, phi
, 0, TDF_SLIM
);
1181 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); gsi_next (&si
))
1183 gimple stmt
= gsi_stmt (si
);
1184 stmt_vec_info stmt_info
= vinfo_for_stmt (stmt
);
1186 gcc_assert (stmt_info
);
1188 if (!vect_analyze_stmt (stmt
, &need_to_vectorize
, NULL
))
1191 if (STMT_VINFO_RELEVANT_P (stmt_info
) && !PURE_SLP_STMT (stmt_info
))
1192 /* STMT needs both SLP and loop-based vectorization. */
1193 only_slp_in_loop
= false;
1197 /* All operations in the loop are either irrelevant (deal with loop
1198 control, or dead), or only used outside the loop and can be moved
1199 out of the loop (e.g. invariants, inductions). The loop can be
1200 optimized away by scalar optimizations. We're better off not
1201 touching this loop. */
1202 if (!need_to_vectorize
)
1204 if (vect_print_dump_info (REPORT_DETAILS
))
1206 "All the computation can be taken out of the loop.");
1207 if (vect_print_dump_info (REPORT_UNVECTORIZED_LOCATIONS
))
1209 "not vectorized: redundant loop. no profit to vectorize.");
1213 /* If all the stmts in the loop can be SLPed, we perform only SLP, and
1214 vectorization factor of the loop is the unrolling factor required by the
1215 SLP instances. If that unrolling factor is 1, we say, that we perform
1216 pure SLP on loop - cross iteration parallelism is not exploited. */
1217 if (only_slp_in_loop
)
1218 vectorization_factor
= LOOP_VINFO_SLP_UNROLLING_FACTOR (loop_vinfo
);
1220 vectorization_factor
= least_common_multiple (vectorization_factor
,
1221 LOOP_VINFO_SLP_UNROLLING_FACTOR (loop_vinfo
));
1223 LOOP_VINFO_VECT_FACTOR (loop_vinfo
) = vectorization_factor
;
1225 if (LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo
)
1226 && vect_print_dump_info (REPORT_DETAILS
))
1228 "vectorization_factor = %d, niters = " HOST_WIDE_INT_PRINT_DEC
,
1229 vectorization_factor
, LOOP_VINFO_INT_NITERS (loop_vinfo
));
1231 if (LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo
)
1232 && (LOOP_VINFO_INT_NITERS (loop_vinfo
) < vectorization_factor
))
1234 if (vect_print_dump_info (REPORT_UNVECTORIZED_LOCATIONS
))
1235 fprintf (vect_dump
, "not vectorized: iteration count too small.");
1236 if (vect_print_dump_info (REPORT_DETAILS
))
1237 fprintf (vect_dump
,"not vectorized: iteration count smaller than "
1238 "vectorization factor.");
1242 /* Analyze cost. Decide if worth while to vectorize. */
1244 /* Once VF is set, SLP costs should be updated since the number of created
1245 vector stmts depends on VF. */
1246 vect_update_slp_costs_according_to_vf (loop_vinfo
);
1248 min_profitable_iters
= vect_estimate_min_profitable_iters (loop_vinfo
);
1249 LOOP_VINFO_COST_MODEL_MIN_ITERS (loop_vinfo
) = min_profitable_iters
;
1251 if (min_profitable_iters
< 0)
1253 if (vect_print_dump_info (REPORT_UNVECTORIZED_LOCATIONS
))
1254 fprintf (vect_dump
, "not vectorized: vectorization not profitable.");
1255 if (vect_print_dump_info (REPORT_DETAILS
))
1256 fprintf (vect_dump
, "not vectorized: vector version will never be "
1261 min_scalar_loop_bound
= ((PARAM_VALUE (PARAM_MIN_VECT_LOOP_BOUND
)
1262 * vectorization_factor
) - 1);
1264 /* Use the cost model only if it is more conservative than user specified
1267 th
= (unsigned) min_scalar_loop_bound
;
1268 if (min_profitable_iters
1269 && (!min_scalar_loop_bound
1270 || min_profitable_iters
> min_scalar_loop_bound
))
1271 th
= (unsigned) min_profitable_iters
;
1273 if (LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo
)
1274 && LOOP_VINFO_INT_NITERS (loop_vinfo
) <= th
)
1276 if (vect_print_dump_info (REPORT_UNVECTORIZED_LOCATIONS
))
1277 fprintf (vect_dump
, "not vectorized: vectorization not "
1279 if (vect_print_dump_info (REPORT_DETAILS
))
1280 fprintf (vect_dump
, "not vectorized: iteration count smaller than "
1281 "user specified loop bound parameter or minimum "
1282 "profitable iterations (whichever is more conservative).");
1286 if (!LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo
)
1287 || LOOP_VINFO_INT_NITERS (loop_vinfo
) % vectorization_factor
!= 0
1288 || LOOP_PEELING_FOR_ALIGNMENT (loop_vinfo
))
1290 if (vect_print_dump_info (REPORT_DETAILS
))
1291 fprintf (vect_dump
, "epilog loop required.");
1292 if (!vect_can_advance_ivs_p (loop_vinfo
))
1294 if (vect_print_dump_info (REPORT_UNVECTORIZED_LOCATIONS
))
1296 "not vectorized: can't create epilog loop 1.");
1299 if (!slpeel_can_duplicate_loop_p (loop
, single_exit (loop
)))
1301 if (vect_print_dump_info (REPORT_UNVECTORIZED_LOCATIONS
))
1303 "not vectorized: can't create epilog loop 2.");
1312 /* Function vect_analyze_loop.
1314 Apply a set of analyses on LOOP, and create a loop_vec_info struct
1315 for it. The different analyses will record information in the
1316 loop_vec_info struct. */
1318 vect_analyze_loop (struct loop
*loop
)
1321 loop_vec_info loop_vinfo
;
1323 if (vect_print_dump_info (REPORT_DETAILS
))
1324 fprintf (vect_dump
, "===== analyze_loop_nest =====");
1326 if (loop_outer (loop
)
1327 && loop_vec_info_for_loop (loop_outer (loop
))
1328 && LOOP_VINFO_VECTORIZABLE_P (loop_vec_info_for_loop (loop_outer (loop
))))
1330 if (vect_print_dump_info (REPORT_DETAILS
))
1331 fprintf (vect_dump
, "outer-loop already vectorized.");
1335 /* Check the CFG characteristics of the loop (nesting, entry/exit, etc. */
1337 loop_vinfo
= vect_analyze_loop_form (loop
);
1340 if (vect_print_dump_info (REPORT_DETAILS
))
1341 fprintf (vect_dump
, "bad loop form.");
1345 /* Find all data references in the loop (which correspond to vdefs/vuses)
1346 and analyze their evolution in the loop.
1348 FORNOW: Handle only simple, array references, which
1349 alignment can be forced, and aligned pointer-references. */
1351 ok
= vect_analyze_data_refs (loop_vinfo
, NULL
);
1354 if (vect_print_dump_info (REPORT_DETAILS
))
1355 fprintf (vect_dump
, "bad data references.");
1356 destroy_loop_vec_info (loop_vinfo
, true);
1360 /* Classify all cross-iteration scalar data-flow cycles.
1361 Cross-iteration cycles caused by virtual phis are analyzed separately. */
1363 vect_analyze_scalar_cycles (loop_vinfo
);
1365 vect_pattern_recog (loop_vinfo
);
1367 /* Data-flow analysis to detect stmts that do not need to be vectorized. */
1369 ok
= vect_mark_stmts_to_be_vectorized (loop_vinfo
);
1372 if (vect_print_dump_info (REPORT_DETAILS
))
1373 fprintf (vect_dump
, "unexpected pattern.");
1374 destroy_loop_vec_info (loop_vinfo
, true);
1378 /* Analyze the alignment of the data-refs in the loop.
1379 Fail if a data reference is found that cannot be vectorized. */
1381 ok
= vect_analyze_data_refs_alignment (loop_vinfo
, NULL
);
1384 if (vect_print_dump_info (REPORT_DETAILS
))
1385 fprintf (vect_dump
, "bad data alignment.");
1386 destroy_loop_vec_info (loop_vinfo
, true);
1390 ok
= vect_determine_vectorization_factor (loop_vinfo
);
1393 if (vect_print_dump_info (REPORT_DETAILS
))
1394 fprintf (vect_dump
, "can't determine vectorization factor.");
1395 destroy_loop_vec_info (loop_vinfo
, true);
1399 /* Analyze data dependences between the data-refs in the loop.
1400 FORNOW: fail at the first data dependence that we encounter. */
1402 ok
= vect_analyze_data_ref_dependences (loop_vinfo
, NULL
);
1405 if (vect_print_dump_info (REPORT_DETAILS
))
1406 fprintf (vect_dump
, "bad data dependence.");
1407 destroy_loop_vec_info (loop_vinfo
, true);
1411 /* Analyze the access patterns of the data-refs in the loop (consecutive,
1412 complex, etc.). FORNOW: Only handle consecutive access pattern. */
1414 ok
= vect_analyze_data_ref_accesses (loop_vinfo
, NULL
);
1417 if (vect_print_dump_info (REPORT_DETAILS
))
1418 fprintf (vect_dump
, "bad data access.");
1419 destroy_loop_vec_info (loop_vinfo
, true);
1423 /* Prune the list of ddrs to be tested at run-time by versioning for alias.
1424 It is important to call pruning after vect_analyze_data_ref_accesses,
1425 since we use grouping information gathered by interleaving analysis. */
1426 ok
= vect_prune_runtime_alias_test_list (loop_vinfo
);
1429 if (vect_print_dump_info (REPORT_DETAILS
))
1430 fprintf (vect_dump
, "too long list of versioning for alias "
1432 destroy_loop_vec_info (loop_vinfo
, true);
1436 /* Check the SLP opportunities in the loop, analyze and build SLP trees. */
1437 ok
= vect_analyze_slp (loop_vinfo
, NULL
);
1440 /* Decide which possible SLP instances to SLP. */
1441 vect_make_slp_decision (loop_vinfo
);
1443 /* Find stmts that need to be both vectorized and SLPed. */
1444 vect_detect_hybrid_slp (loop_vinfo
);
1447 /* This pass will decide on using loop versioning and/or loop peeling in
1448 order to enhance the alignment of data references in the loop. */
1450 ok
= vect_enhance_data_refs_alignment (loop_vinfo
);
1453 if (vect_print_dump_info (REPORT_DETAILS
))
1454 fprintf (vect_dump
, "bad data alignment.");
1455 destroy_loop_vec_info (loop_vinfo
, true);
1459 /* Scan all the operations in the loop and make sure they are
1462 ok
= vect_analyze_loop_operations (loop_vinfo
);
1465 if (vect_print_dump_info (REPORT_DETAILS
))
1466 fprintf (vect_dump
, "bad operation or unsupported loop bound.");
1467 destroy_loop_vec_info (loop_vinfo
, true);
1471 LOOP_VINFO_VECTORIZABLE_P (loop_vinfo
) = 1;
1477 /* Function reduction_code_for_scalar_code
1480 CODE - tree_code of a reduction operations.
1483 REDUC_CODE - the corresponding tree-code to be used to reduce the
1484 vector of partial results into a single scalar result (which
1485 will also reside in a vector) or ERROR_MARK if the operation is
1486 a supported reduction operation, but does not have such tree-code.
1488 Return FALSE if CODE currently cannot be vectorized as reduction. */
1491 reduction_code_for_scalar_code (enum tree_code code
,
1492 enum tree_code
*reduc_code
)
1497 *reduc_code
= REDUC_MAX_EXPR
;
1501 *reduc_code
= REDUC_MIN_EXPR
;
1505 *reduc_code
= REDUC_PLUS_EXPR
;
1513 *reduc_code
= ERROR_MARK
;
1522 /* Error reporting helper for vect_is_simple_reduction below. GIMPLE statement
1523 STMT is printed with a message MSG. */
1526 report_vect_op (gimple stmt
, const char *msg
)
1528 fprintf (vect_dump
, "%s", msg
);
1529 print_gimple_stmt (vect_dump
, stmt
, 0, TDF_SLIM
);
1533 /* Function vect_is_simple_reduction
1535 (1) Detect a cross-iteration def-use cycle that represents a simple
1536 reduction computation. We look for the following pattern:
1541 a2 = operation (a3, a1)
1544 1. operation is commutative and associative and it is safe to
1545 change the order of the computation (if CHECK_REDUCTION is true)
1546 2. no uses for a2 in the loop (a2 is used out of the loop)
1547 3. no uses of a1 in the loop besides the reduction operation.
1549 Condition 1 is tested here.
1550 Conditions 2,3 are tested in vect_mark_stmts_to_be_vectorized.
1552 (2) Detect a cross-iteration def-use cycle in nested loops, i.e.,
1553 nested cycles, if CHECK_REDUCTION is false.
1555 (3) Detect cycles of phi nodes in outer-loop vectorization, i.e., double
1559 inner loop (def of a3)
1564 vect_is_simple_reduction (loop_vec_info loop_info
, gimple phi
,
1565 bool check_reduction
, bool *double_reduc
)
1567 struct loop
*loop
= (gimple_bb (phi
))->loop_father
;
1568 struct loop
*vect_loop
= LOOP_VINFO_LOOP (loop_info
);
1569 edge latch_e
= loop_latch_edge (loop
);
1570 tree loop_arg
= PHI_ARG_DEF_FROM_EDGE (phi
, latch_e
);
1571 gimple def_stmt
, def1
= NULL
, def2
= NULL
;
1572 enum tree_code code
;
1573 tree op1
, op2
, op3
= NULL_TREE
, op4
= NULL_TREE
;
1577 imm_use_iterator imm_iter
;
1578 use_operand_p use_p
;
1581 *double_reduc
= false;
1583 /* If CHECK_REDUCTION is true, we assume inner-most loop vectorization,
1584 otherwise, we assume outer loop vectorization. */
1585 gcc_assert ((check_reduction
&& loop
== vect_loop
)
1586 || (!check_reduction
&& flow_loop_nested_p (vect_loop
, loop
)));
1588 name
= PHI_RESULT (phi
);
1590 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, name
)
1592 gimple use_stmt
= USE_STMT (use_p
);
1593 if (flow_bb_inside_loop_p (loop
, gimple_bb (use_stmt
))
1594 && vinfo_for_stmt (use_stmt
)
1595 && !is_pattern_stmt_p (vinfo_for_stmt (use_stmt
)))
1599 if (vect_print_dump_info (REPORT_DETAILS
))
1600 fprintf (vect_dump
, "reduction used in loop.");
1605 if (TREE_CODE (loop_arg
) != SSA_NAME
)
1607 if (vect_print_dump_info (REPORT_DETAILS
))
1609 fprintf (vect_dump
, "reduction: not ssa_name: ");
1610 print_generic_expr (vect_dump
, loop_arg
, TDF_SLIM
);
1615 def_stmt
= SSA_NAME_DEF_STMT (loop_arg
);
1618 if (vect_print_dump_info (REPORT_DETAILS
))
1619 fprintf (vect_dump
, "reduction: no def_stmt.");
1623 if (!is_gimple_assign (def_stmt
) && gimple_code (def_stmt
) != GIMPLE_PHI
)
1625 if (vect_print_dump_info (REPORT_DETAILS
))
1626 print_gimple_stmt (vect_dump
, def_stmt
, 0, TDF_SLIM
);
1630 if (is_gimple_assign (def_stmt
))
1632 name
= gimple_assign_lhs (def_stmt
);
1637 name
= PHI_RESULT (def_stmt
);
1642 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, name
)
1644 gimple use_stmt
= USE_STMT (use_p
);
1645 if (flow_bb_inside_loop_p (loop
, gimple_bb (use_stmt
))
1646 && vinfo_for_stmt (use_stmt
)
1647 && !is_pattern_stmt_p (vinfo_for_stmt (use_stmt
)))
1651 if (vect_print_dump_info (REPORT_DETAILS
))
1652 fprintf (vect_dump
, "reduction used in loop.");
1657 /* If DEF_STMT is a phi node itself, we expect it to have a single argument
1658 defined in the inner loop. */
1661 op1
= PHI_ARG_DEF (def_stmt
, 0);
1663 if (gimple_phi_num_args (def_stmt
) != 1
1664 || TREE_CODE (op1
) != SSA_NAME
)
1666 if (vect_print_dump_info (REPORT_DETAILS
))
1667 fprintf (vect_dump
, "unsupported phi node definition.");
1672 def1
= SSA_NAME_DEF_STMT (op1
);
1673 if (flow_bb_inside_loop_p (loop
, gimple_bb (def_stmt
))
1675 && flow_bb_inside_loop_p (loop
->inner
, gimple_bb (def1
))
1676 && is_gimple_assign (def1
))
1678 if (vect_print_dump_info (REPORT_DETAILS
))
1679 report_vect_op (def_stmt
, "detected double reduction: ");
1681 *double_reduc
= true;
1688 code
= gimple_assign_rhs_code (def_stmt
);
1691 && (!commutative_tree_code (code
) || !associative_tree_code (code
)))
1693 if (vect_print_dump_info (REPORT_DETAILS
))
1694 report_vect_op (def_stmt
, "reduction: not commutative/associative: ");
1698 if (get_gimple_rhs_class (code
) != GIMPLE_BINARY_RHS
)
1700 if (code
!= COND_EXPR
)
1702 if (vect_print_dump_info (REPORT_DETAILS
))
1703 report_vect_op (def_stmt
, "reduction: not binary operation: ");
1708 op3
= TREE_OPERAND (TREE_OPERAND (gimple_assign_rhs1 (def_stmt
), 0), 0);
1709 op4
= TREE_OPERAND (TREE_OPERAND (gimple_assign_rhs1 (def_stmt
), 0), 1);
1710 op1
= TREE_OPERAND (gimple_assign_rhs1 (def_stmt
), 1);
1711 op2
= TREE_OPERAND (gimple_assign_rhs1 (def_stmt
), 2);
1713 if (TREE_CODE (op1
) != SSA_NAME
&& TREE_CODE (op2
) != SSA_NAME
)
1715 if (vect_print_dump_info (REPORT_DETAILS
))
1716 report_vect_op (def_stmt
, "reduction: uses not ssa_names: ");
1723 op1
= gimple_assign_rhs1 (def_stmt
);
1724 op2
= gimple_assign_rhs2 (def_stmt
);
1726 if (TREE_CODE (op1
) != SSA_NAME
|| TREE_CODE (op2
) != SSA_NAME
)
1728 if (vect_print_dump_info (REPORT_DETAILS
))
1729 report_vect_op (def_stmt
, "reduction: uses not ssa_names: ");
1735 type
= TREE_TYPE (gimple_assign_lhs (def_stmt
));
1736 if ((TREE_CODE (op1
) == SSA_NAME
1737 && TYPE_MAIN_VARIANT (type
) != TYPE_MAIN_VARIANT (TREE_TYPE (op1
)))
1738 || (TREE_CODE (op2
) == SSA_NAME
1739 && TYPE_MAIN_VARIANT (type
) != TYPE_MAIN_VARIANT (TREE_TYPE (op2
)))
1740 || (op3
&& TREE_CODE (op3
) == SSA_NAME
1741 && TYPE_MAIN_VARIANT (type
) != TYPE_MAIN_VARIANT (TREE_TYPE (op3
)))
1742 || (op4
&& TREE_CODE (op4
) == SSA_NAME
1743 && TYPE_MAIN_VARIANT (type
) != TYPE_MAIN_VARIANT (TREE_TYPE (op4
))))
1745 if (vect_print_dump_info (REPORT_DETAILS
))
1747 fprintf (vect_dump
, "reduction: multiple types: operation type: ");
1748 print_generic_expr (vect_dump
, type
, TDF_SLIM
);
1749 fprintf (vect_dump
, ", operands types: ");
1750 print_generic_expr (vect_dump
, TREE_TYPE (op1
), TDF_SLIM
);
1751 fprintf (vect_dump
, ",");
1752 print_generic_expr (vect_dump
, TREE_TYPE (op2
), TDF_SLIM
);
1755 fprintf (vect_dump
, ",");
1756 print_generic_expr (vect_dump
, TREE_TYPE (op3
), TDF_SLIM
);
1757 fprintf (vect_dump
, ",");
1758 print_generic_expr (vect_dump
, TREE_TYPE (op4
), TDF_SLIM
);
1765 /* Check that it's ok to change the order of the computation.
1766 Generally, when vectorizing a reduction we change the order of the
1767 computation. This may change the behavior of the program in some
1768 cases, so we need to check that this is ok. One exception is when
1769 vectorizing an outer-loop: the inner-loop is executed sequentially,
1770 and therefore vectorizing reductions in the inner-loop during
1771 outer-loop vectorization is safe. */
1773 /* CHECKME: check for !flag_finite_math_only too? */
1774 if (SCALAR_FLOAT_TYPE_P (type
) && !flag_associative_math
1777 /* Changing the order of operations changes the semantics. */
1778 if (vect_print_dump_info (REPORT_DETAILS
))
1779 report_vect_op (def_stmt
, "reduction: unsafe fp math optimization: ");
1782 else if (INTEGRAL_TYPE_P (type
) && TYPE_OVERFLOW_TRAPS (type
)
1785 /* Changing the order of operations changes the semantics. */
1786 if (vect_print_dump_info (REPORT_DETAILS
))
1787 report_vect_op (def_stmt
, "reduction: unsafe int math optimization: ");
1790 else if (SAT_FIXED_POINT_TYPE_P (type
) && check_reduction
)
1792 /* Changing the order of operations changes the semantics. */
1793 if (vect_print_dump_info (REPORT_DETAILS
))
1794 report_vect_op (def_stmt
,
1795 "reduction: unsafe fixed-point math optimization: ");
1799 /* Reduction is safe. We're dealing with one of the following:
1800 1) integer arithmetic and no trapv
1801 2) floating point arithmetic, and special flags permit this optimization
1802 3) nested cycle (i.e., outer loop vectorization). */
1803 if (TREE_CODE (op1
) == SSA_NAME
)
1804 def1
= SSA_NAME_DEF_STMT (op1
);
1806 if (TREE_CODE (op2
) == SSA_NAME
)
1807 def2
= SSA_NAME_DEF_STMT (op2
);
1809 if (code
!= COND_EXPR
1810 && (!def1
|| !def2
|| gimple_nop_p (def1
) || gimple_nop_p (def2
)))
1812 if (vect_print_dump_info (REPORT_DETAILS
))
1813 report_vect_op (def_stmt
, "reduction: no defs for operands: ");
1817 /* Check that one def is the reduction def, defined by PHI,
1818 the other def is either defined in the loop ("vect_internal_def"),
1819 or it's an induction (defined by a loop-header phi-node). */
1821 if (def2
&& def2
== phi
1822 && (code
== COND_EXPR
1823 || (def1
&& flow_bb_inside_loop_p (loop
, gimple_bb (def1
))
1824 && (is_gimple_assign (def1
)
1825 || STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def1
))
1826 == vect_induction_def
1827 || (gimple_code (def1
) == GIMPLE_PHI
1828 && STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def1
))
1829 == vect_internal_def
1830 && !is_loop_header_bb_p (gimple_bb (def1
)))))))
1832 if (vect_print_dump_info (REPORT_DETAILS
))
1833 report_vect_op (def_stmt
, "detected reduction: ");
1836 else if (def1
&& def1
== phi
1837 && (code
== COND_EXPR
1838 || (def2
&& flow_bb_inside_loop_p (loop
, gimple_bb (def2
))
1839 && (is_gimple_assign (def2
)
1840 || STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def2
))
1841 == vect_induction_def
1842 || (gimple_code (def2
) == GIMPLE_PHI
1843 && STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def2
))
1844 == vect_internal_def
1845 && !is_loop_header_bb_p (gimple_bb (def2
)))))))
1847 if (check_reduction
)
1849 /* Swap operands (just for simplicity - so that the rest of the code
1850 can assume that the reduction variable is always the last (second)
1852 if (vect_print_dump_info (REPORT_DETAILS
))
1853 report_vect_op (def_stmt
,
1854 "detected reduction: need to swap operands: ");
1856 swap_tree_operands (def_stmt
, gimple_assign_rhs1_ptr (def_stmt
),
1857 gimple_assign_rhs2_ptr (def_stmt
));
1861 if (vect_print_dump_info (REPORT_DETAILS
))
1862 report_vect_op (def_stmt
, "detected reduction: ");
1869 if (vect_print_dump_info (REPORT_DETAILS
))
1870 report_vect_op (def_stmt
, "reduction: unknown pattern: ");
1877 /* Function vect_estimate_min_profitable_iters
1879 Return the number of iterations required for the vector version of the
1880 loop to be profitable relative to the cost of the scalar version of the
1883 TODO: Take profile info into account before making vectorization
1884 decisions, if available. */
1887 vect_estimate_min_profitable_iters (loop_vec_info loop_vinfo
)
1890 int min_profitable_iters
;
1891 int peel_iters_prologue
;
1892 int peel_iters_epilogue
;
1893 int vec_inside_cost
= 0;
1894 int vec_outside_cost
= 0;
1895 int scalar_single_iter_cost
= 0;
1896 int scalar_outside_cost
= 0;
1897 int vf
= LOOP_VINFO_VECT_FACTOR (loop_vinfo
);
1898 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
1899 basic_block
*bbs
= LOOP_VINFO_BBS (loop_vinfo
);
1900 int nbbs
= loop
->num_nodes
;
1901 int byte_misalign
= LOOP_PEELING_FOR_ALIGNMENT (loop_vinfo
);
1902 int peel_guard_costs
= 0;
1903 int innerloop_iters
= 0, factor
;
1904 VEC (slp_instance
, heap
) *slp_instances
;
1905 slp_instance instance
;
1907 /* Cost model disabled. */
1908 if (!flag_vect_cost_model
)
1910 if (vect_print_dump_info (REPORT_COST
))
1911 fprintf (vect_dump
, "cost model disabled.");
1915 /* Requires loop versioning tests to handle misalignment. */
1916 if (LOOP_REQUIRES_VERSIONING_FOR_ALIGNMENT (loop_vinfo
))
1918 /* FIXME: Make cost depend on complexity of individual check. */
1920 VEC_length (gimple
, LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo
));
1921 if (vect_print_dump_info (REPORT_COST
))
1922 fprintf (vect_dump
, "cost model: Adding cost of checks for loop "
1923 "versioning to treat misalignment.\n");
1926 /* Requires loop versioning with alias checks. */
1927 if (LOOP_REQUIRES_VERSIONING_FOR_ALIAS (loop_vinfo
))
1929 /* FIXME: Make cost depend on complexity of individual check. */
1931 VEC_length (ddr_p
, LOOP_VINFO_MAY_ALIAS_DDRS (loop_vinfo
));
1932 if (vect_print_dump_info (REPORT_COST
))
1933 fprintf (vect_dump
, "cost model: Adding cost of checks for loop "
1934 "versioning aliasing.\n");
1937 if (LOOP_REQUIRES_VERSIONING_FOR_ALIGNMENT (loop_vinfo
)
1938 || LOOP_REQUIRES_VERSIONING_FOR_ALIAS (loop_vinfo
))
1939 vec_outside_cost
+= TARG_COND_TAKEN_BRANCH_COST
;
1941 /* Count statements in scalar loop. Using this as scalar cost for a single
1944 TODO: Add outer loop support.
1946 TODO: Consider assigning different costs to different scalar
1951 innerloop_iters
= 50; /* FIXME */
1953 for (i
= 0; i
< nbbs
; i
++)
1955 gimple_stmt_iterator si
;
1956 basic_block bb
= bbs
[i
];
1958 if (bb
->loop_father
== loop
->inner
)
1959 factor
= innerloop_iters
;
1963 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); gsi_next (&si
))
1965 gimple stmt
= gsi_stmt (si
);
1966 stmt_vec_info stmt_info
= vinfo_for_stmt (stmt
);
1967 /* Skip stmts that are not vectorized inside the loop. */
1968 if (!STMT_VINFO_RELEVANT_P (stmt_info
)
1969 && (!STMT_VINFO_LIVE_P (stmt_info
)
1970 || STMT_VINFO_DEF_TYPE (stmt_info
) != vect_reduction_def
))
1972 scalar_single_iter_cost
+= cost_for_stmt (stmt
) * factor
;
1973 vec_inside_cost
+= STMT_VINFO_INSIDE_OF_LOOP_COST (stmt_info
) * factor
;
1974 /* FIXME: for stmts in the inner-loop in outer-loop vectorization,
1975 some of the "outside" costs are generated inside the outer-loop. */
1976 vec_outside_cost
+= STMT_VINFO_OUTSIDE_OF_LOOP_COST (stmt_info
);
1980 /* Add additional cost for the peeled instructions in prologue and epilogue
1983 FORNOW: If we don't know the value of peel_iters for prologue or epilogue
1984 at compile-time - we assume it's vf/2 (the worst would be vf-1).
1986 TODO: Build an expression that represents peel_iters for prologue and
1987 epilogue to be used in a run-time test. */
1989 if (byte_misalign
< 0)
1991 peel_iters_prologue
= vf
/2;
1992 if (vect_print_dump_info (REPORT_COST
))
1993 fprintf (vect_dump
, "cost model: "
1994 "prologue peel iters set to vf/2.");
1996 /* If peeling for alignment is unknown, loop bound of main loop becomes
1998 peel_iters_epilogue
= vf
/2;
1999 if (vect_print_dump_info (REPORT_COST
))
2000 fprintf (vect_dump
, "cost model: "
2001 "epilogue peel iters set to vf/2 because "
2002 "peeling for alignment is unknown .");
2004 /* If peeled iterations are unknown, count a taken branch and a not taken
2005 branch per peeled loop. Even if scalar loop iterations are known,
2006 vector iterations are not known since peeled prologue iterations are
2007 not known. Hence guards remain the same. */
2008 peel_guard_costs
+= 2 * (TARG_COND_TAKEN_BRANCH_COST
2009 + TARG_COND_NOT_TAKEN_BRANCH_COST
);
2015 struct data_reference
*dr
= LOOP_VINFO_UNALIGNED_DR (loop_vinfo
);
2016 int element_size
= GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (DR_REF (dr
))));
2017 tree vectype
= STMT_VINFO_VECTYPE (vinfo_for_stmt (DR_STMT (dr
)));
2018 int nelements
= TYPE_VECTOR_SUBPARTS (vectype
);
2020 peel_iters_prologue
= nelements
- (byte_misalign
/ element_size
);
2023 peel_iters_prologue
= 0;
2025 if (!LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo
))
2027 peel_iters_epilogue
= vf
/2;
2028 if (vect_print_dump_info (REPORT_COST
))
2029 fprintf (vect_dump
, "cost model: "
2030 "epilogue peel iters set to vf/2 because "
2031 "loop iterations are unknown .");
2033 /* If peeled iterations are known but number of scalar loop
2034 iterations are unknown, count a taken branch per peeled loop. */
2035 peel_guard_costs
+= 2 * TARG_COND_TAKEN_BRANCH_COST
;
2040 int niters
= LOOP_VINFO_INT_NITERS (loop_vinfo
);
2041 peel_iters_prologue
= niters
< peel_iters_prologue
?
2042 niters
: peel_iters_prologue
;
2043 peel_iters_epilogue
= (niters
- peel_iters_prologue
) % vf
;
2047 vec_outside_cost
+= (peel_iters_prologue
* scalar_single_iter_cost
)
2048 + (peel_iters_epilogue
* scalar_single_iter_cost
)
2051 /* FORNOW: The scalar outside cost is incremented in one of the
2054 1. The vectorizer checks for alignment and aliasing and generates
2055 a condition that allows dynamic vectorization. A cost model
2056 check is ANDED with the versioning condition. Hence scalar code
2057 path now has the added cost of the versioning check.
2059 if (cost > th & versioning_check)
2062 Hence run-time scalar is incremented by not-taken branch cost.
2064 2. The vectorizer then checks if a prologue is required. If the
2065 cost model check was not done before during versioning, it has to
2066 be done before the prologue check.
2069 prologue = scalar_iters
2074 if (prologue == num_iters)
2077 Hence the run-time scalar cost is incremented by a taken branch,
2078 plus a not-taken branch, plus a taken branch cost.
2080 3. The vectorizer then checks if an epilogue is required. If the
2081 cost model check was not done before during prologue check, it
2082 has to be done with the epilogue check.
2088 if (prologue == num_iters)
2091 if ((cost <= th) | (scalar_iters-prologue-epilogue == 0))
2094 Hence the run-time scalar cost should be incremented by 2 taken
2097 TODO: The back end may reorder the BBS's differently and reverse
2098 conditions/branch directions. Change the estimates below to
2099 something more reasonable. */
2101 /* If the number of iterations is known and we do not do versioning, we can
2102 decide whether to vectorize at compile time. Hence the scalar version
2103 do not carry cost model guard costs. */
2104 if (!LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo
)
2105 || LOOP_REQUIRES_VERSIONING_FOR_ALIGNMENT (loop_vinfo
)
2106 || LOOP_REQUIRES_VERSIONING_FOR_ALIAS (loop_vinfo
))
2108 /* Cost model check occurs at versioning. */
2109 if (LOOP_REQUIRES_VERSIONING_FOR_ALIGNMENT (loop_vinfo
)
2110 || LOOP_REQUIRES_VERSIONING_FOR_ALIAS (loop_vinfo
))
2111 scalar_outside_cost
+= TARG_COND_NOT_TAKEN_BRANCH_COST
;
2114 /* Cost model check occurs at prologue generation. */
2115 if (LOOP_PEELING_FOR_ALIGNMENT (loop_vinfo
) < 0)
2116 scalar_outside_cost
+= 2 * TARG_COND_TAKEN_BRANCH_COST
2117 + TARG_COND_NOT_TAKEN_BRANCH_COST
;
2118 /* Cost model check occurs at epilogue generation. */
2120 scalar_outside_cost
+= 2 * TARG_COND_TAKEN_BRANCH_COST
;
2124 /* Add SLP costs. */
2125 slp_instances
= LOOP_VINFO_SLP_INSTANCES (loop_vinfo
);
2126 for (i
= 0; VEC_iterate (slp_instance
, slp_instances
, i
, instance
); i
++)
2128 vec_outside_cost
+= SLP_INSTANCE_OUTSIDE_OF_LOOP_COST (instance
);
2129 vec_inside_cost
+= SLP_INSTANCE_INSIDE_OF_LOOP_COST (instance
);
2132 /* Calculate number of iterations required to make the vector version
2133 profitable, relative to the loop bodies only. The following condition
2135 SIC * niters + SOC > VIC * ((niters-PL_ITERS-EP_ITERS)/VF) + VOC
2137 SIC = scalar iteration cost, VIC = vector iteration cost,
2138 VOC = vector outside cost, VF = vectorization factor,
2139 PL_ITERS = prologue iterations, EP_ITERS= epilogue iterations
2140 SOC = scalar outside cost for run time cost model check. */
2142 if ((scalar_single_iter_cost
* vf
) > vec_inside_cost
)
2144 if (vec_outside_cost
<= 0)
2145 min_profitable_iters
= 1;
2148 min_profitable_iters
= ((vec_outside_cost
- scalar_outside_cost
) * vf
2149 - vec_inside_cost
* peel_iters_prologue
2150 - vec_inside_cost
* peel_iters_epilogue
)
2151 / ((scalar_single_iter_cost
* vf
)
2154 if ((scalar_single_iter_cost
* vf
* min_profitable_iters
)
2155 <= ((vec_inside_cost
* min_profitable_iters
)
2156 + ((vec_outside_cost
- scalar_outside_cost
) * vf
)))
2157 min_profitable_iters
++;
2160 /* vector version will never be profitable. */
2163 if (vect_print_dump_info (REPORT_COST
))
2164 fprintf (vect_dump
, "cost model: vector iteration cost = %d "
2165 "is divisible by scalar iteration cost = %d by a factor "
2166 "greater than or equal to the vectorization factor = %d .",
2167 vec_inside_cost
, scalar_single_iter_cost
, vf
);
2171 if (vect_print_dump_info (REPORT_COST
))
2173 fprintf (vect_dump
, "Cost model analysis: \n");
2174 fprintf (vect_dump
, " Vector inside of loop cost: %d\n",
2176 fprintf (vect_dump
, " Vector outside of loop cost: %d\n",
2178 fprintf (vect_dump
, " Scalar iteration cost: %d\n",
2179 scalar_single_iter_cost
);
2180 fprintf (vect_dump
, " Scalar outside cost: %d\n", scalar_outside_cost
);
2181 fprintf (vect_dump
, " prologue iterations: %d\n",
2182 peel_iters_prologue
);
2183 fprintf (vect_dump
, " epilogue iterations: %d\n",
2184 peel_iters_epilogue
);
2185 fprintf (vect_dump
, " Calculated minimum iters for profitability: %d\n",
2186 min_profitable_iters
);
2189 min_profitable_iters
=
2190 min_profitable_iters
< vf
? vf
: min_profitable_iters
;
2192 /* Because the condition we create is:
2193 if (niters <= min_profitable_iters)
2194 then skip the vectorized loop. */
2195 min_profitable_iters
--;
2197 if (vect_print_dump_info (REPORT_COST
))
2198 fprintf (vect_dump
, " Profitability threshold = %d\n",
2199 min_profitable_iters
);
2201 return min_profitable_iters
;
2205 /* TODO: Close dependency between vect_model_*_cost and vectorizable_*
2206 functions. Design better to avoid maintenance issues. */
2208 /* Function vect_model_reduction_cost.
2210 Models cost for a reduction operation, including the vector ops
2211 generated within the strip-mine loop, the initial definition before
2212 the loop, and the epilogue code that must be generated. */
2215 vect_model_reduction_cost (stmt_vec_info stmt_info
, enum tree_code reduc_code
,
2219 enum tree_code code
;
2222 gimple stmt
, orig_stmt
;
2224 enum machine_mode mode
;
2225 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_info
);
2226 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
2229 /* Cost of reduction op inside loop. */
2230 STMT_VINFO_INSIDE_OF_LOOP_COST (stmt_info
) += ncopies
* TARG_VEC_STMT_COST
;
2232 stmt
= STMT_VINFO_STMT (stmt_info
);
2234 switch (get_gimple_rhs_class (gimple_assign_rhs_code (stmt
)))
2236 case GIMPLE_SINGLE_RHS
:
2237 gcc_assert (TREE_OPERAND_LENGTH (gimple_assign_rhs1 (stmt
)) == ternary_op
);
2238 reduction_op
= TREE_OPERAND (gimple_assign_rhs1 (stmt
), 2);
2240 case GIMPLE_UNARY_RHS
:
2241 reduction_op
= gimple_assign_rhs1 (stmt
);
2243 case GIMPLE_BINARY_RHS
:
2244 reduction_op
= gimple_assign_rhs2 (stmt
);
2250 vectype
= get_vectype_for_scalar_type (TREE_TYPE (reduction_op
));
2253 if (vect_print_dump_info (REPORT_COST
))
2255 fprintf (vect_dump
, "unsupported data-type ");
2256 print_generic_expr (vect_dump
, TREE_TYPE (reduction_op
), TDF_SLIM
);
2261 mode
= TYPE_MODE (vectype
);
2262 orig_stmt
= STMT_VINFO_RELATED_STMT (stmt_info
);
2265 orig_stmt
= STMT_VINFO_STMT (stmt_info
);
2267 code
= gimple_assign_rhs_code (orig_stmt
);
2269 /* Add in cost for initial definition. */
2270 outer_cost
+= TARG_SCALAR_TO_VEC_COST
;
2272 /* Determine cost of epilogue code.
2274 We have a reduction operator that will reduce the vector in one statement.
2275 Also requires scalar extract. */
2277 if (!nested_in_vect_loop_p (loop
, orig_stmt
))
2279 if (reduc_code
!= ERROR_MARK
)
2280 outer_cost
+= TARG_VEC_STMT_COST
+ TARG_VEC_TO_SCALAR_COST
;
2283 int vec_size_in_bits
= tree_low_cst (TYPE_SIZE (vectype
), 1);
2285 TYPE_SIZE (TREE_TYPE (gimple_assign_lhs (orig_stmt
)));
2286 int element_bitsize
= tree_low_cst (bitsize
, 1);
2287 int nelements
= vec_size_in_bits
/ element_bitsize
;
2289 optab
= optab_for_tree_code (code
, vectype
, optab_default
);
2291 /* We have a whole vector shift available. */
2292 if (VECTOR_MODE_P (mode
)
2293 && optab_handler (optab
, mode
)->insn_code
!= CODE_FOR_nothing
2294 && optab_handler (vec_shr_optab
, mode
)->insn_code
!= CODE_FOR_nothing
)
2295 /* Final reduction via vector shifts and the reduction operator. Also
2296 requires scalar extract. */
2297 outer_cost
+= ((exact_log2(nelements
) * 2) * TARG_VEC_STMT_COST
2298 + TARG_VEC_TO_SCALAR_COST
);
2300 /* Use extracts and reduction op for final reduction. For N elements,
2301 we have N extracts and N-1 reduction ops. */
2302 outer_cost
+= ((nelements
+ nelements
- 1) * TARG_VEC_STMT_COST
);
2306 STMT_VINFO_OUTSIDE_OF_LOOP_COST (stmt_info
) = outer_cost
;
2308 if (vect_print_dump_info (REPORT_COST
))
2309 fprintf (vect_dump
, "vect_model_reduction_cost: inside_cost = %d, "
2310 "outside_cost = %d .", STMT_VINFO_INSIDE_OF_LOOP_COST (stmt_info
),
2311 STMT_VINFO_OUTSIDE_OF_LOOP_COST (stmt_info
));
2317 /* Function vect_model_induction_cost.
2319 Models cost for induction operations. */
2322 vect_model_induction_cost (stmt_vec_info stmt_info
, int ncopies
)
2324 /* loop cost for vec_loop. */
2325 STMT_VINFO_INSIDE_OF_LOOP_COST (stmt_info
) = ncopies
* TARG_VEC_STMT_COST
;
2326 /* prologue cost for vec_init and vec_step. */
2327 STMT_VINFO_OUTSIDE_OF_LOOP_COST (stmt_info
) = 2 * TARG_SCALAR_TO_VEC_COST
;
2329 if (vect_print_dump_info (REPORT_COST
))
2330 fprintf (vect_dump
, "vect_model_induction_cost: inside_cost = %d, "
2331 "outside_cost = %d .", STMT_VINFO_INSIDE_OF_LOOP_COST (stmt_info
),
2332 STMT_VINFO_OUTSIDE_OF_LOOP_COST (stmt_info
));
2336 /* Function get_initial_def_for_induction
2339 STMT - a stmt that performs an induction operation in the loop.
2340 IV_PHI - the initial value of the induction variable
2343 Return a vector variable, initialized with the first VF values of
2344 the induction variable. E.g., for an iv with IV_PHI='X' and
2345 evolution S, for a vector of 4 units, we want to return:
2346 [X, X + S, X + 2*S, X + 3*S]. */
2349 get_initial_def_for_induction (gimple iv_phi
)
2351 stmt_vec_info stmt_vinfo
= vinfo_for_stmt (iv_phi
);
2352 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_vinfo
);
2353 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
2354 tree scalar_type
= TREE_TYPE (gimple_phi_result (iv_phi
));
2357 edge pe
= loop_preheader_edge (loop
);
2358 struct loop
*iv_loop
;
2360 tree vec
, vec_init
, vec_step
, t
;
2364 gimple init_stmt
, induction_phi
, new_stmt
;
2365 tree induc_def
, vec_def
, vec_dest
;
2366 tree init_expr
, step_expr
;
2367 int vf
= LOOP_VINFO_VECT_FACTOR (loop_vinfo
);
2372 stmt_vec_info phi_info
= vinfo_for_stmt (iv_phi
);
2373 bool nested_in_vect_loop
= false;
2374 gimple_seq stmts
= NULL
;
2375 imm_use_iterator imm_iter
;
2376 use_operand_p use_p
;
2380 gimple_stmt_iterator si
;
2381 basic_block bb
= gimple_bb (iv_phi
);
2384 vectype
= get_vectype_for_scalar_type (scalar_type
);
2385 gcc_assert (vectype
);
2386 nunits
= TYPE_VECTOR_SUBPARTS (vectype
);
2387 ncopies
= vf
/ nunits
;
2389 gcc_assert (phi_info
);
2390 gcc_assert (ncopies
>= 1);
2392 /* Find the first insertion point in the BB. */
2393 si
= gsi_after_labels (bb
);
2395 if (INTEGRAL_TYPE_P (scalar_type
))
2396 step_expr
= build_int_cst (scalar_type
, 0);
2397 else if (POINTER_TYPE_P (scalar_type
))
2398 step_expr
= build_int_cst (sizetype
, 0);
2400 step_expr
= build_real (scalar_type
, dconst0
);
2402 /* Is phi in an inner-loop, while vectorizing an enclosing outer-loop? */
2403 if (nested_in_vect_loop_p (loop
, iv_phi
))
2405 nested_in_vect_loop
= true;
2406 iv_loop
= loop
->inner
;
2410 gcc_assert (iv_loop
== (gimple_bb (iv_phi
))->loop_father
);
2412 latch_e
= loop_latch_edge (iv_loop
);
2413 loop_arg
= PHI_ARG_DEF_FROM_EDGE (iv_phi
, latch_e
);
2415 access_fn
= analyze_scalar_evolution (iv_loop
, PHI_RESULT (iv_phi
));
2416 gcc_assert (access_fn
);
2417 ok
= vect_is_simple_iv_evolution (iv_loop
->num
, access_fn
,
2418 &init_expr
, &step_expr
);
2420 pe
= loop_preheader_edge (iv_loop
);
2422 /* Create the vector that holds the initial_value of the induction. */
2423 if (nested_in_vect_loop
)
2425 /* iv_loop is nested in the loop to be vectorized. init_expr had already
2426 been created during vectorization of previous stmts; We obtain it from
2427 the STMT_VINFO_VEC_STMT of the defining stmt. */
2428 tree iv_def
= PHI_ARG_DEF_FROM_EDGE (iv_phi
,
2429 loop_preheader_edge (iv_loop
));
2430 vec_init
= vect_get_vec_def_for_operand (iv_def
, iv_phi
, NULL
);
2434 /* iv_loop is the loop to be vectorized. Create:
2435 vec_init = [X, X+S, X+2*S, X+3*S] (S = step_expr, X = init_expr) */
2436 new_var
= vect_get_new_vect_var (scalar_type
, vect_scalar_var
, "var_");
2437 add_referenced_var (new_var
);
2439 new_name
= force_gimple_operand (init_expr
, &stmts
, false, new_var
);
2442 new_bb
= gsi_insert_seq_on_edge_immediate (pe
, stmts
);
2443 gcc_assert (!new_bb
);
2447 t
= tree_cons (NULL_TREE
, init_expr
, t
);
2448 for (i
= 1; i
< nunits
; i
++)
2450 /* Create: new_name_i = new_name + step_expr */
2451 enum tree_code code
= POINTER_TYPE_P (scalar_type
)
2452 ? POINTER_PLUS_EXPR
: PLUS_EXPR
;
2453 init_stmt
= gimple_build_assign_with_ops (code
, new_var
,
2454 new_name
, step_expr
);
2455 new_name
= make_ssa_name (new_var
, init_stmt
);
2456 gimple_assign_set_lhs (init_stmt
, new_name
);
2458 new_bb
= gsi_insert_on_edge_immediate (pe
, init_stmt
);
2459 gcc_assert (!new_bb
);
2461 if (vect_print_dump_info (REPORT_DETAILS
))
2463 fprintf (vect_dump
, "created new init_stmt: ");
2464 print_gimple_stmt (vect_dump
, init_stmt
, 0, TDF_SLIM
);
2466 t
= tree_cons (NULL_TREE
, new_name
, t
);
2468 /* Create a vector from [new_name_0, new_name_1, ..., new_name_nunits-1] */
2469 vec
= build_constructor_from_list (vectype
, nreverse (t
));
2470 vec_init
= vect_init_vector (iv_phi
, vec
, vectype
, NULL
);
2474 /* Create the vector that holds the step of the induction. */
2475 if (nested_in_vect_loop
)
2476 /* iv_loop is nested in the loop to be vectorized. Generate:
2477 vec_step = [S, S, S, S] */
2478 new_name
= step_expr
;
2481 /* iv_loop is the loop to be vectorized. Generate:
2482 vec_step = [VF*S, VF*S, VF*S, VF*S] */
2483 expr
= build_int_cst (TREE_TYPE (step_expr
), vf
);
2484 new_name
= fold_build2 (MULT_EXPR
, TREE_TYPE (step_expr
),
2489 for (i
= 0; i
< nunits
; i
++)
2490 t
= tree_cons (NULL_TREE
, unshare_expr (new_name
), t
);
2491 gcc_assert (CONSTANT_CLASS_P (new_name
));
2492 stepvectype
= get_vectype_for_scalar_type (TREE_TYPE (new_name
));
2493 gcc_assert (stepvectype
);
2494 vec
= build_vector (stepvectype
, t
);
2495 vec_step
= vect_init_vector (iv_phi
, vec
, stepvectype
, NULL
);
2498 /* Create the following def-use cycle:
2503 vec_iv = PHI <vec_init, vec_loop>
2507 vec_loop = vec_iv + vec_step; */
2509 /* Create the induction-phi that defines the induction-operand. */
2510 vec_dest
= vect_get_new_vect_var (vectype
, vect_simple_var
, "vec_iv_");
2511 add_referenced_var (vec_dest
);
2512 induction_phi
= create_phi_node (vec_dest
, iv_loop
->header
);
2513 set_vinfo_for_stmt (induction_phi
,
2514 new_stmt_vec_info (induction_phi
, loop_vinfo
, NULL
));
2515 induc_def
= PHI_RESULT (induction_phi
);
2517 /* Create the iv update inside the loop */
2518 new_stmt
= gimple_build_assign_with_ops (PLUS_EXPR
, vec_dest
,
2519 induc_def
, vec_step
);
2520 vec_def
= make_ssa_name (vec_dest
, new_stmt
);
2521 gimple_assign_set_lhs (new_stmt
, vec_def
);
2522 gsi_insert_before (&si
, new_stmt
, GSI_SAME_STMT
);
2523 set_vinfo_for_stmt (new_stmt
, new_stmt_vec_info (new_stmt
, loop_vinfo
,
2526 /* Set the arguments of the phi node: */
2527 add_phi_arg (induction_phi
, vec_init
, pe
, UNKNOWN_LOCATION
);
2528 add_phi_arg (induction_phi
, vec_def
, loop_latch_edge (iv_loop
),
2532 /* In case that vectorization factor (VF) is bigger than the number
2533 of elements that we can fit in a vectype (nunits), we have to generate
2534 more than one vector stmt - i.e - we need to "unroll" the
2535 vector stmt by a factor VF/nunits. For more details see documentation
2536 in vectorizable_operation. */
2540 stmt_vec_info prev_stmt_vinfo
;
2541 /* FORNOW. This restriction should be relaxed. */
2542 gcc_assert (!nested_in_vect_loop
);
2544 /* Create the vector that holds the step of the induction. */
2545 expr
= build_int_cst (TREE_TYPE (step_expr
), nunits
);
2546 new_name
= fold_build2 (MULT_EXPR
, TREE_TYPE (step_expr
),
2549 for (i
= 0; i
< nunits
; i
++)
2550 t
= tree_cons (NULL_TREE
, unshare_expr (new_name
), t
);
2551 gcc_assert (CONSTANT_CLASS_P (new_name
));
2552 vec
= build_vector (stepvectype
, t
);
2553 vec_step
= vect_init_vector (iv_phi
, vec
, stepvectype
, NULL
);
2555 vec_def
= induc_def
;
2556 prev_stmt_vinfo
= vinfo_for_stmt (induction_phi
);
2557 for (i
= 1; i
< ncopies
; i
++)
2559 /* vec_i = vec_prev + vec_step */
2560 new_stmt
= gimple_build_assign_with_ops (PLUS_EXPR
, vec_dest
,
2562 vec_def
= make_ssa_name (vec_dest
, new_stmt
);
2563 gimple_assign_set_lhs (new_stmt
, vec_def
);
2565 gsi_insert_before (&si
, new_stmt
, GSI_SAME_STMT
);
2566 set_vinfo_for_stmt (new_stmt
,
2567 new_stmt_vec_info (new_stmt
, loop_vinfo
, NULL
));
2568 STMT_VINFO_RELATED_STMT (prev_stmt_vinfo
) = new_stmt
;
2569 prev_stmt_vinfo
= vinfo_for_stmt (new_stmt
);
2573 if (nested_in_vect_loop
)
2575 /* Find the loop-closed exit-phi of the induction, and record
2576 the final vector of induction results: */
2578 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, loop_arg
)
2580 if (!flow_bb_inside_loop_p (iv_loop
, gimple_bb (USE_STMT (use_p
))))
2582 exit_phi
= USE_STMT (use_p
);
2588 stmt_vec_info stmt_vinfo
= vinfo_for_stmt (exit_phi
);
2589 /* FORNOW. Currently not supporting the case that an inner-loop induction
2590 is not used in the outer-loop (i.e. only outside the outer-loop). */
2591 gcc_assert (STMT_VINFO_RELEVANT_P (stmt_vinfo
)
2592 && !STMT_VINFO_LIVE_P (stmt_vinfo
));
2594 STMT_VINFO_VEC_STMT (stmt_vinfo
) = new_stmt
;
2595 if (vect_print_dump_info (REPORT_DETAILS
))
2597 fprintf (vect_dump
, "vector of inductions after inner-loop:");
2598 print_gimple_stmt (vect_dump
, new_stmt
, 0, TDF_SLIM
);
2604 if (vect_print_dump_info (REPORT_DETAILS
))
2606 fprintf (vect_dump
, "transform induction: created def-use cycle: ");
2607 print_gimple_stmt (vect_dump
, induction_phi
, 0, TDF_SLIM
);
2608 fprintf (vect_dump
, "\n");
2609 print_gimple_stmt (vect_dump
, SSA_NAME_DEF_STMT (vec_def
), 0, TDF_SLIM
);
2612 STMT_VINFO_VEC_STMT (phi_info
) = induction_phi
;
2617 /* Function get_initial_def_for_reduction
2620 STMT - a stmt that performs a reduction operation in the loop.
2621 INIT_VAL - the initial value of the reduction variable
2624 ADJUSTMENT_DEF - a tree that holds a value to be added to the final result
2625 of the reduction (used for adjusting the epilog - see below).
2626 Return a vector variable, initialized according to the operation that STMT
2627 performs. This vector will be used as the initial value of the
2628 vector of partial results.
2630 Option1 (adjust in epilog): Initialize the vector as follows:
2631 add/bit or/xor: [0,0,...,0,0]
2632 mult/bit and: [1,1,...,1,1]
2633 min/max/cond_expr: [init_val,init_val,..,init_val,init_val]
2634 and when necessary (e.g. add/mult case) let the caller know
2635 that it needs to adjust the result by init_val.
2637 Option2: Initialize the vector as follows:
2638 add/bit or/xor: [init_val,0,0,...,0]
2639 mult/bit and: [init_val,1,1,...,1]
2640 min/max/cond_expr: [init_val,init_val,...,init_val]
2641 and no adjustments are needed.
2643 For example, for the following code:
2649 STMT is 's = s + a[i]', and the reduction variable is 's'.
2650 For a vector of 4 units, we want to return either [0,0,0,init_val],
2651 or [0,0,0,0] and let the caller know that it needs to adjust
2652 the result at the end by 'init_val'.
2654 FORNOW, we are using the 'adjust in epilog' scheme, because this way the
2655 initialization vector is simpler (same element in all entries), if
2656 ADJUSTMENT_DEF is not NULL, and Option2 otherwise.
2658 A cost model should help decide between these two schemes. */
2661 get_initial_def_for_reduction (gimple stmt
, tree init_val
,
2662 tree
*adjustment_def
)
2664 stmt_vec_info stmt_vinfo
= vinfo_for_stmt (stmt
);
2665 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_vinfo
);
2666 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
2667 tree scalar_type
= TREE_TYPE (init_val
);
2668 tree vectype
= get_vectype_for_scalar_type (scalar_type
);
2670 enum tree_code code
= gimple_assign_rhs_code (stmt
);
2675 bool nested_in_vect_loop
= false;
2677 REAL_VALUE_TYPE real_init_val
= dconst0
;
2678 int int_init_val
= 0;
2679 gimple def_stmt
= NULL
;
2681 gcc_assert (vectype
);
2682 nunits
= TYPE_VECTOR_SUBPARTS (vectype
);
2684 gcc_assert (POINTER_TYPE_P (scalar_type
) || INTEGRAL_TYPE_P (scalar_type
)
2685 || SCALAR_FLOAT_TYPE_P (scalar_type
));
2687 if (nested_in_vect_loop_p (loop
, stmt
))
2688 nested_in_vect_loop
= true;
2690 gcc_assert (loop
== (gimple_bb (stmt
))->loop_father
);
2692 /* In case of double reduction we only create a vector variable to be put
2693 in the reduction phi node. The actual statement creation is done in
2694 vect_create_epilog_for_reduction. */
2695 if (adjustment_def
&& nested_in_vect_loop
2696 && TREE_CODE (init_val
) == SSA_NAME
2697 && (def_stmt
= SSA_NAME_DEF_STMT (init_val
))
2698 && gimple_code (def_stmt
) == GIMPLE_PHI
2699 && flow_bb_inside_loop_p (loop
, gimple_bb (def_stmt
))
2700 && vinfo_for_stmt (def_stmt
)
2701 && STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def_stmt
))
2702 == vect_double_reduction_def
)
2704 *adjustment_def
= NULL
;
2705 return vect_create_destination_var (init_val
, vectype
);
2708 if (TREE_CONSTANT (init_val
))
2710 if (SCALAR_FLOAT_TYPE_P (scalar_type
))
2711 init_value
= build_real (scalar_type
, TREE_REAL_CST (init_val
));
2713 init_value
= build_int_cst (scalar_type
, TREE_INT_CST_LOW (init_val
));
2716 init_value
= init_val
;
2720 case WIDEN_SUM_EXPR
:
2728 /* ADJUSMENT_DEF is NULL when called from
2729 vect_create_epilog_for_reduction to vectorize double reduction. */
2732 if (nested_in_vect_loop
)
2733 *adjustment_def
= vect_get_vec_def_for_operand (init_val
, stmt
,
2736 *adjustment_def
= init_val
;
2739 if (code
== MULT_EXPR
|| code
== BIT_AND_EXPR
)
2741 real_init_val
= dconst1
;
2745 if (SCALAR_FLOAT_TYPE_P (scalar_type
))
2746 def_for_init
= build_real (scalar_type
, real_init_val
);
2748 def_for_init
= build_int_cst (scalar_type
, int_init_val
);
2750 /* Create a vector of '0' or '1' except the first element. */
2751 for (i
= nunits
- 2; i
>= 0; --i
)
2752 t
= tree_cons (NULL_TREE
, def_for_init
, t
);
2754 /* Option1: the first element is '0' or '1' as well. */
2757 t
= tree_cons (NULL_TREE
, def_for_init
, t
);
2758 init_def
= build_vector (vectype
, t
);
2762 /* Option2: the first element is INIT_VAL. */
2763 t
= tree_cons (NULL_TREE
, init_value
, t
);
2764 if (TREE_CONSTANT (init_val
))
2765 init_def
= build_vector (vectype
, t
);
2767 init_def
= build_constructor_from_list (vectype
, t
);
2776 *adjustment_def
= NULL_TREE
;
2777 init_def
= vect_get_vec_def_for_operand (init_val
, stmt
, NULL
);
2781 for (i
= nunits
- 1; i
>= 0; --i
)
2782 t
= tree_cons (NULL_TREE
, init_value
, t
);
2784 if (TREE_CONSTANT (init_val
))
2785 init_def
= build_vector (vectype
, t
);
2787 init_def
= build_constructor_from_list (vectype
, t
);
2799 /* Function vect_create_epilog_for_reduction
2801 Create code at the loop-epilog to finalize the result of a reduction
2804 VECT_DEF is a vector of partial results.
2805 REDUC_CODE is the tree-code for the epilog reduction.
2806 NCOPIES is > 1 in case the vectorization factor (VF) is bigger than the
2807 number of elements that we can fit in a vectype (nunits). In this case
2808 we have to generate more than one vector stmt - i.e - we need to "unroll"
2809 the vector stmt by a factor VF/nunits. For more details see documentation
2810 in vectorizable_operation.
2811 STMT is the scalar reduction stmt that is being vectorized.
2812 REDUCTION_PHI is the phi-node that carries the reduction computation.
2813 REDUC_INDEX is the index of the operand in the right hand side of the
2814 statement that is defined by REDUCTION_PHI.
2815 DOUBLE_REDUC is TRUE if double reduction phi nodes should be handled.
2818 1. Creates the reduction def-use cycle: sets the arguments for
2820 The loop-entry argument is the vectorized initial-value of the reduction.
2821 The loop-latch argument is VECT_DEF - the vector of partial sums.
2822 2. "Reduces" the vector of partial results VECT_DEF into a single result,
2823 by applying the operation specified by REDUC_CODE if available, or by
2824 other means (whole-vector shifts or a scalar loop).
2825 The function also creates a new phi node at the loop exit to preserve
2826 loop-closed form, as illustrated below.
2828 The flow at the entry to this function:
2831 vec_def = phi <null, null> # REDUCTION_PHI
2832 VECT_DEF = vector_stmt # vectorized form of STMT
2833 s_loop = scalar_stmt # (scalar) STMT
2835 s_out0 = phi <s_loop> # (scalar) EXIT_PHI
2839 The above is transformed by this function into:
2842 vec_def = phi <vec_init, VECT_DEF> # REDUCTION_PHI
2843 VECT_DEF = vector_stmt # vectorized form of STMT
2844 s_loop = scalar_stmt # (scalar) STMT
2846 s_out0 = phi <s_loop> # (scalar) EXIT_PHI
2847 v_out1 = phi <VECT_DEF> # NEW_EXIT_PHI
2848 v_out2 = reduce <v_out1>
2849 s_out3 = extract_field <v_out2, 0>
2850 s_out4 = adjust_result <s_out3>
2856 vect_create_epilog_for_reduction (tree vect_def
, gimple stmt
,
2858 enum tree_code reduc_code
,
2859 gimple reduction_phi
,
2863 stmt_vec_info stmt_info
= vinfo_for_stmt (stmt
);
2864 stmt_vec_info prev_phi_info
;
2866 enum machine_mode mode
;
2867 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_info
);
2868 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
), *outer_loop
= NULL
;
2869 basic_block exit_bb
;
2872 gimple new_phi
= NULL
, phi
;
2873 gimple_stmt_iterator exit_gsi
;
2875 tree new_temp
= NULL_TREE
;
2877 gimple epilog_stmt
= NULL
;
2878 tree new_scalar_dest
, new_dest
;
2880 tree bitsize
, bitpos
, bytesize
;
2881 enum tree_code code
= gimple_assign_rhs_code (stmt
);
2882 tree adjustment_def
;
2883 tree vec_initial_def
, def
;
2885 imm_use_iterator imm_iter
;
2886 use_operand_p use_p
;
2887 bool extract_scalar_result
= false;
2888 tree reduction_op
, expr
;
2891 bool nested_in_vect_loop
= false;
2892 VEC(gimple
,heap
) *phis
= NULL
;
2893 enum vect_def_type dt
= vect_unknown_def_type
;
2896 if (nested_in_vect_loop_p (loop
, stmt
))
2900 nested_in_vect_loop
= true;
2903 switch (get_gimple_rhs_class (gimple_assign_rhs_code (stmt
)))
2905 case GIMPLE_SINGLE_RHS
:
2906 gcc_assert (TREE_OPERAND_LENGTH (gimple_assign_rhs1 (stmt
))
2908 reduction_op
= TREE_OPERAND (gimple_assign_rhs1 (stmt
), reduc_index
);
2910 case GIMPLE_UNARY_RHS
:
2911 reduction_op
= gimple_assign_rhs1 (stmt
);
2913 case GIMPLE_BINARY_RHS
:
2914 reduction_op
= reduc_index
?
2915 gimple_assign_rhs2 (stmt
) : gimple_assign_rhs1 (stmt
);
2921 vectype
= get_vectype_for_scalar_type (TREE_TYPE (reduction_op
));
2922 gcc_assert (vectype
);
2923 mode
= TYPE_MODE (vectype
);
2925 /*** 1. Create the reduction def-use cycle ***/
2927 /* For the case of reduction, vect_get_vec_def_for_operand returns
2928 the scalar def before the loop, that defines the initial value
2929 of the reduction variable. */
2930 vec_initial_def
= vect_get_vec_def_for_operand (reduction_op
, stmt
,
2933 phi
= reduction_phi
;
2935 for (j
= 0; j
< ncopies
; j
++)
2937 /* 1.1 set the loop-entry arg of the reduction-phi: */
2938 add_phi_arg (phi
, vec_initial_def
, loop_preheader_edge (loop
),
2941 /* 1.2 set the loop-latch arg for the reduction-phi: */
2943 def
= vect_get_vec_def_for_stmt_copy (dt
, def
);
2944 add_phi_arg (phi
, def
, loop_latch_edge (loop
), UNKNOWN_LOCATION
);
2946 if (vect_print_dump_info (REPORT_DETAILS
))
2948 fprintf (vect_dump
, "transform reduction: created def-use cycle: ");
2949 print_gimple_stmt (vect_dump
, phi
, 0, TDF_SLIM
);
2950 fprintf (vect_dump
, "\n");
2951 print_gimple_stmt (vect_dump
, SSA_NAME_DEF_STMT (def
), 0, TDF_SLIM
);
2954 phi
= STMT_VINFO_RELATED_STMT (vinfo_for_stmt (phi
));
2957 /*** 2. Create epilog code
2958 The reduction epilog code operates across the elements of the vector
2959 of partial results computed by the vectorized loop.
2960 The reduction epilog code consists of:
2961 step 1: compute the scalar result in a vector (v_out2)
2962 step 2: extract the scalar result (s_out3) from the vector (v_out2)
2963 step 3: adjust the scalar result (s_out3) if needed.
2965 Step 1 can be accomplished using one the following three schemes:
2966 (scheme 1) using reduc_code, if available.
2967 (scheme 2) using whole-vector shifts, if available.
2968 (scheme 3) using a scalar loop. In this case steps 1+2 above are
2971 The overall epilog code looks like this:
2973 s_out0 = phi <s_loop> # original EXIT_PHI
2974 v_out1 = phi <VECT_DEF> # NEW_EXIT_PHI
2975 v_out2 = reduce <v_out1> # step 1
2976 s_out3 = extract_field <v_out2, 0> # step 2
2977 s_out4 = adjust_result <s_out3> # step 3
2979 (step 3 is optional, and steps 1 and 2 may be combined).
2980 Lastly, the uses of s_out0 are replaced by s_out4.
2984 /* 2.1 Create new loop-exit-phi to preserve loop-closed form:
2985 v_out1 = phi <v_loop> */
2987 exit_bb
= single_exit (loop
)->dest
;
2989 prev_phi_info
= NULL
;
2990 for (j
= 0; j
< ncopies
; j
++)
2992 phi
= create_phi_node (SSA_NAME_VAR (vect_def
), exit_bb
);
2993 set_vinfo_for_stmt (phi
, new_stmt_vec_info (phi
, loop_vinfo
, NULL
));
2998 def
= vect_get_vec_def_for_stmt_copy (dt
, def
);
2999 STMT_VINFO_RELATED_STMT (prev_phi_info
) = phi
;
3001 SET_PHI_ARG_DEF (phi
, single_exit (loop
)->dest_idx
, def
);
3002 prev_phi_info
= vinfo_for_stmt (phi
);
3005 exit_gsi
= gsi_after_labels (exit_bb
);
3007 /* 2.2 Get the relevant tree-code to use in the epilog for schemes 2,3
3008 (i.e. when reduc_code is not available) and in the final adjustment
3009 code (if needed). Also get the original scalar reduction variable as
3010 defined in the loop. In case STMT is a "pattern-stmt" (i.e. - it
3011 represents a reduction pattern), the tree-code and scalar-def are
3012 taken from the original stmt that the pattern-stmt (STMT) replaces.
3013 Otherwise (it is a regular reduction) - the tree-code and scalar-def
3014 are taken from STMT. */
3016 orig_stmt
= STMT_VINFO_RELATED_STMT (stmt_info
);
3019 /* Regular reduction */
3024 /* Reduction pattern */
3025 stmt_vec_info stmt_vinfo
= vinfo_for_stmt (orig_stmt
);
3026 gcc_assert (STMT_VINFO_IN_PATTERN_P (stmt_vinfo
));
3027 gcc_assert (STMT_VINFO_RELATED_STMT (stmt_vinfo
) == stmt
);
3030 code
= gimple_assign_rhs_code (orig_stmt
);
3031 scalar_dest
= gimple_assign_lhs (orig_stmt
);
3032 scalar_type
= TREE_TYPE (scalar_dest
);
3033 new_scalar_dest
= vect_create_destination_var (scalar_dest
, NULL
);
3034 bitsize
= TYPE_SIZE (scalar_type
);
3035 bytesize
= TYPE_SIZE_UNIT (scalar_type
);
3037 /* For MINUS_EXPR the initial vector is [init_val,0,...,0], therefore,
3038 partial results are added and not subtracted. */
3039 if (code
== MINUS_EXPR
)
3042 /* In case this is a reduction in an inner-loop while vectorizing an outer
3043 loop - we don't need to extract a single scalar result at the end of the
3044 inner-loop (unless it is double reduction, i.e., the use of reduction is
3045 outside the outer-loop). The final vector of partial results will be used
3046 in the vectorized outer-loop, or reduced to a scalar result at the end of
3048 if (nested_in_vect_loop
&& !double_reduc
)
3049 goto vect_finalize_reduction
;
3051 /* The epilogue is created for the outer-loop, i.e., for the loop being
3057 gcc_assert (ncopies
== 1);
3059 /* 2.3 Create the reduction code, using one of the three schemes described
3062 if (reduc_code
!= ERROR_MARK
)
3066 /*** Case 1: Create:
3067 v_out2 = reduc_expr <v_out1> */
3069 if (vect_print_dump_info (REPORT_DETAILS
))
3070 fprintf (vect_dump
, "Reduce using direct vector reduction.");
3072 vec_dest
= vect_create_destination_var (scalar_dest
, vectype
);
3073 tmp
= build1 (reduc_code
, vectype
, PHI_RESULT (new_phi
));
3074 epilog_stmt
= gimple_build_assign (vec_dest
, tmp
);
3075 new_temp
= make_ssa_name (vec_dest
, epilog_stmt
);
3076 gimple_assign_set_lhs (epilog_stmt
, new_temp
);
3077 gsi_insert_before (&exit_gsi
, epilog_stmt
, GSI_SAME_STMT
);
3079 extract_scalar_result
= true;
3083 enum tree_code shift_code
= ERROR_MARK
;
3084 bool have_whole_vector_shift
= true;
3086 int element_bitsize
= tree_low_cst (bitsize
, 1);
3087 int vec_size_in_bits
= tree_low_cst (TYPE_SIZE (vectype
), 1);
3090 if (optab_handler (vec_shr_optab
, mode
)->insn_code
!= CODE_FOR_nothing
)
3091 shift_code
= VEC_RSHIFT_EXPR
;
3093 have_whole_vector_shift
= false;
3095 /* Regardless of whether we have a whole vector shift, if we're
3096 emulating the operation via tree-vect-generic, we don't want
3097 to use it. Only the first round of the reduction is likely
3098 to still be profitable via emulation. */
3099 /* ??? It might be better to emit a reduction tree code here, so that
3100 tree-vect-generic can expand the first round via bit tricks. */
3101 if (!VECTOR_MODE_P (mode
))
3102 have_whole_vector_shift
= false;
3105 optab optab
= optab_for_tree_code (code
, vectype
, optab_default
);
3106 if (optab_handler (optab
, mode
)->insn_code
== CODE_FOR_nothing
)
3107 have_whole_vector_shift
= false;
3110 if (have_whole_vector_shift
)
3112 /*** Case 2: Create:
3113 for (offset = VS/2; offset >= element_size; offset/=2)
3115 Create: va' = vec_shift <va, offset>
3116 Create: va = vop <va, va'>
3119 if (vect_print_dump_info (REPORT_DETAILS
))
3120 fprintf (vect_dump
, "Reduce using vector shifts");
3122 vec_dest
= vect_create_destination_var (scalar_dest
, vectype
);
3123 new_temp
= PHI_RESULT (new_phi
);
3125 for (bit_offset
= vec_size_in_bits
/2;
3126 bit_offset
>= element_bitsize
;
3129 tree bitpos
= size_int (bit_offset
);
3131 epilog_stmt
= gimple_build_assign_with_ops (shift_code
, vec_dest
,
3133 new_name
= make_ssa_name (vec_dest
, epilog_stmt
);
3134 gimple_assign_set_lhs (epilog_stmt
, new_name
);
3135 gsi_insert_before (&exit_gsi
, epilog_stmt
, GSI_SAME_STMT
);
3137 epilog_stmt
= gimple_build_assign_with_ops (code
, vec_dest
,
3138 new_name
, new_temp
);
3139 new_temp
= make_ssa_name (vec_dest
, epilog_stmt
);
3140 gimple_assign_set_lhs (epilog_stmt
, new_temp
);
3141 gsi_insert_before (&exit_gsi
, epilog_stmt
, GSI_SAME_STMT
);
3144 extract_scalar_result
= true;
3150 /*** Case 3: Create:
3151 s = extract_field <v_out2, 0>
3152 for (offset = element_size;
3153 offset < vector_size;
3154 offset += element_size;)
3156 Create: s' = extract_field <v_out2, offset>
3157 Create: s = op <s, s'>
3160 if (vect_print_dump_info (REPORT_DETAILS
))
3161 fprintf (vect_dump
, "Reduce using scalar code. ");
3163 vec_temp
= PHI_RESULT (new_phi
);
3164 vec_size_in_bits
= tree_low_cst (TYPE_SIZE (vectype
), 1);
3165 rhs
= build3 (BIT_FIELD_REF
, scalar_type
, vec_temp
, bitsize
,
3167 epilog_stmt
= gimple_build_assign (new_scalar_dest
, rhs
);
3168 new_temp
= make_ssa_name (new_scalar_dest
, epilog_stmt
);
3169 gimple_assign_set_lhs (epilog_stmt
, new_temp
);
3170 gsi_insert_before (&exit_gsi
, epilog_stmt
, GSI_SAME_STMT
);
3172 for (bit_offset
= element_bitsize
;
3173 bit_offset
< vec_size_in_bits
;
3174 bit_offset
+= element_bitsize
)
3176 tree bitpos
= bitsize_int (bit_offset
);
3177 tree rhs
= build3 (BIT_FIELD_REF
, scalar_type
, vec_temp
, bitsize
,
3180 epilog_stmt
= gimple_build_assign (new_scalar_dest
, rhs
);
3181 new_name
= make_ssa_name (new_scalar_dest
, epilog_stmt
);
3182 gimple_assign_set_lhs (epilog_stmt
, new_name
);
3183 gsi_insert_before (&exit_gsi
, epilog_stmt
, GSI_SAME_STMT
);
3185 epilog_stmt
= gimple_build_assign_with_ops (code
,
3187 new_name
, new_temp
);
3188 new_temp
= make_ssa_name (new_scalar_dest
, epilog_stmt
);
3189 gimple_assign_set_lhs (epilog_stmt
, new_temp
);
3190 gsi_insert_before (&exit_gsi
, epilog_stmt
, GSI_SAME_STMT
);
3193 extract_scalar_result
= false;
3197 /* 2.4 Extract the final scalar result. Create:
3198 s_out3 = extract_field <v_out2, bitpos> */
3200 if (extract_scalar_result
)
3204 gcc_assert (!nested_in_vect_loop
|| double_reduc
);
3205 if (vect_print_dump_info (REPORT_DETAILS
))
3206 fprintf (vect_dump
, "extract scalar result");
3208 if (BYTES_BIG_ENDIAN
)
3209 bitpos
= size_binop (MULT_EXPR
,
3210 bitsize_int (TYPE_VECTOR_SUBPARTS (vectype
) - 1),
3211 TYPE_SIZE (scalar_type
));
3213 bitpos
= bitsize_zero_node
;
3215 rhs
= build3 (BIT_FIELD_REF
, scalar_type
, new_temp
, bitsize
, bitpos
);
3216 epilog_stmt
= gimple_build_assign (new_scalar_dest
, rhs
);
3217 new_temp
= make_ssa_name (new_scalar_dest
, epilog_stmt
);
3218 gimple_assign_set_lhs (epilog_stmt
, new_temp
);
3219 gsi_insert_before (&exit_gsi
, epilog_stmt
, GSI_SAME_STMT
);
3222 vect_finalize_reduction
:
3227 /* 2.5 Adjust the final result by the initial value of the reduction
3228 variable. (When such adjustment is not needed, then
3229 'adjustment_def' is zero). For example, if code is PLUS we create:
3230 new_temp = loop_exit_def + adjustment_def */
3234 if (nested_in_vect_loop
)
3236 gcc_assert (TREE_CODE (TREE_TYPE (adjustment_def
)) == VECTOR_TYPE
);
3237 expr
= build2 (code
, vectype
, PHI_RESULT (new_phi
), adjustment_def
);
3238 new_dest
= vect_create_destination_var (scalar_dest
, vectype
);
3242 gcc_assert (TREE_CODE (TREE_TYPE (adjustment_def
)) != VECTOR_TYPE
);
3243 expr
= build2 (code
, scalar_type
, new_temp
, adjustment_def
);
3244 new_dest
= vect_create_destination_var (scalar_dest
, scalar_type
);
3247 epilog_stmt
= gimple_build_assign (new_dest
, expr
);
3248 new_temp
= make_ssa_name (new_dest
, epilog_stmt
);
3249 gimple_assign_set_lhs (epilog_stmt
, new_temp
);
3250 SSA_NAME_DEF_STMT (new_temp
) = epilog_stmt
;
3251 gsi_insert_before (&exit_gsi
, epilog_stmt
, GSI_SAME_STMT
);
3255 /* 2.6 Handle the loop-exit phi */
3257 /* Replace uses of s_out0 with uses of s_out3:
3258 Find the loop-closed-use at the loop exit of the original scalar result.
3259 (The reduction result is expected to have two immediate uses - one at the
3260 latch block, and one at the loop exit). */
3261 phis
= VEC_alloc (gimple
, heap
, 10);
3262 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, scalar_dest
)
3264 if (!flow_bb_inside_loop_p (loop
, gimple_bb (USE_STMT (use_p
))))
3266 exit_phi
= USE_STMT (use_p
);
3267 VEC_quick_push (gimple
, phis
, exit_phi
);
3271 /* We expect to have found an exit_phi because of loop-closed-ssa form. */
3272 gcc_assert (!VEC_empty (gimple
, phis
));
3274 for (i
= 0; VEC_iterate (gimple
, phis
, i
, exit_phi
); i
++)
3276 if (nested_in_vect_loop
)
3278 stmt_vec_info stmt_vinfo
= vinfo_for_stmt (exit_phi
);
3281 /* FORNOW. Currently not supporting the case that an inner-loop
3282 reduction is not used in the outer-loop (but only outside the
3283 outer-loop), unless it is double reduction. */
3284 gcc_assert ((STMT_VINFO_RELEVANT_P (stmt_vinfo
)
3285 && !STMT_VINFO_LIVE_P (stmt_vinfo
)) || double_reduc
);
3287 epilog_stmt
= adjustment_def
? epilog_stmt
: new_phi
;
3288 STMT_VINFO_VEC_STMT (stmt_vinfo
) = epilog_stmt
;
3289 set_vinfo_for_stmt (epilog_stmt
,
3290 new_stmt_vec_info (epilog_stmt
, loop_vinfo
,
3293 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (epilog_stmt
)) =
3294 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (new_phi
));
3297 || STMT_VINFO_DEF_TYPE (stmt_vinfo
) != vect_double_reduction_def
)
3300 /* Handle double reduction:
3302 stmt1: s1 = phi <s0, s2> - double reduction phi (outer loop)
3303 stmt2: s3 = phi <s1, s4> - (regular) reduction phi (inner loop)
3304 stmt3: s4 = use (s3) - (regular) reduction stmt (inner loop)
3305 stmt4: s2 = phi <s4> - double reduction stmt (outer loop)
3307 At that point the regular reduction (stmt2 and stmt3) is already
3308 vectorized, as well as the exit phi node, stmt4.
3309 Here we vectorize the phi node of double reduction, stmt1, and
3310 update all relevant statements. */
3312 /* Go through all the uses of s2 to find double reduction phi node,
3313 i.e., stmt1 above. */
3314 orig_name
= PHI_RESULT (exit_phi
);
3315 FOR_EACH_IMM_USE_STMT (use_stmt
, imm_iter
, orig_name
)
3317 stmt_vec_info use_stmt_vinfo
= vinfo_for_stmt (use_stmt
);
3318 stmt_vec_info new_phi_vinfo
;
3319 tree vect_phi_init
, preheader_arg
, vect_phi_res
, init_def
;
3320 basic_block bb
= gimple_bb (use_stmt
);
3323 /* Check that USE_STMT is really double reduction phi node. */
3324 if (gimple_code (use_stmt
) != GIMPLE_PHI
3325 || gimple_phi_num_args (use_stmt
) != 2
3327 || STMT_VINFO_DEF_TYPE (use_stmt_vinfo
)
3328 != vect_double_reduction_def
3329 || bb
->loop_father
!= outer_loop
)
3332 /* Create vector phi node for double reduction:
3333 vs1 = phi <vs0, vs2>
3334 vs1 was created previously in this function by a call to
3335 vect_get_vec_def_for_operand and is stored in vec_initial_def;
3336 vs2 is defined by EPILOG_STMT, the vectorized EXIT_PHI;
3337 vs0 is created here. */
3339 /* Create vector phi node. */
3340 vect_phi
= create_phi_node (vec_initial_def
, bb
);
3341 new_phi_vinfo
= new_stmt_vec_info (vect_phi
,
3342 loop_vec_info_for_loop (outer_loop
), NULL
);
3343 set_vinfo_for_stmt (vect_phi
, new_phi_vinfo
);
3345 /* Create vs0 - initial def of the double reduction phi. */
3346 preheader_arg
= PHI_ARG_DEF_FROM_EDGE (use_stmt
,
3347 loop_preheader_edge (outer_loop
));
3348 init_def
= get_initial_def_for_reduction (stmt
, preheader_arg
,
3350 vect_phi_init
= vect_init_vector (use_stmt
, init_def
, vectype
,
3353 /* Update phi node arguments with vs0 and vs2. */
3354 add_phi_arg (vect_phi
, vect_phi_init
,
3355 loop_preheader_edge (outer_loop
), UNKNOWN_LOCATION
);
3356 add_phi_arg (vect_phi
, PHI_RESULT (epilog_stmt
),
3357 loop_latch_edge (outer_loop
), UNKNOWN_LOCATION
);
3358 if (vect_print_dump_info (REPORT_DETAILS
))
3360 fprintf (vect_dump
, "created double reduction phi node: ");
3361 print_gimple_stmt (vect_dump
, vect_phi
, 0, TDF_SLIM
);
3364 vect_phi_res
= PHI_RESULT (vect_phi
);
3366 /* Replace the use, i.e., set the correct vs1 in the regular
3367 reduction phi node. FORNOW, NCOPIES is always 1, so the loop
3369 use
= reduction_phi
;
3370 for (j
= 0; j
< ncopies
; j
++)
3372 edge pr_edge
= loop_preheader_edge (loop
);
3373 SET_PHI_ARG_DEF (use
, pr_edge
->dest_idx
, vect_phi_res
);
3374 use
= STMT_VINFO_RELATED_STMT (vinfo_for_stmt (use
));
3379 /* Replace the uses: */
3380 orig_name
= PHI_RESULT (exit_phi
);
3381 FOR_EACH_IMM_USE_STMT (use_stmt
, imm_iter
, orig_name
)
3382 FOR_EACH_IMM_USE_ON_STMT (use_p
, imm_iter
)
3383 SET_USE (use_p
, new_temp
);
3386 VEC_free (gimple
, heap
, phis
);
3390 /* Function vectorizable_reduction.
3392 Check if STMT performs a reduction operation that can be vectorized.
3393 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
3394 stmt to replace it, put it in VEC_STMT, and insert it at GSI.
3395 Return FALSE if not a vectorizable STMT, TRUE otherwise.
3397 This function also handles reduction idioms (patterns) that have been
3398 recognized in advance during vect_pattern_recog. In this case, STMT may be
3400 X = pattern_expr (arg0, arg1, ..., X)
3401 and it's STMT_VINFO_RELATED_STMT points to the last stmt in the original
3402 sequence that had been detected and replaced by the pattern-stmt (STMT).
3404 In some cases of reduction patterns, the type of the reduction variable X is
3405 different than the type of the other arguments of STMT.
3406 In such cases, the vectype that is used when transforming STMT into a vector
3407 stmt is different than the vectype that is used to determine the
3408 vectorization factor, because it consists of a different number of elements
3409 than the actual number of elements that are being operated upon in parallel.
3411 For example, consider an accumulation of shorts into an int accumulator.
3412 On some targets it's possible to vectorize this pattern operating on 8
3413 shorts at a time (hence, the vectype for purposes of determining the
3414 vectorization factor should be V8HI); on the other hand, the vectype that
3415 is used to create the vector form is actually V4SI (the type of the result).
3417 Upon entry to this function, STMT_VINFO_VECTYPE records the vectype that
3418 indicates what is the actual level of parallelism (V8HI in the example), so
3419 that the right vectorization factor would be derived. This vectype
3420 corresponds to the type of arguments to the reduction stmt, and should *NOT*
3421 be used to create the vectorized stmt. The right vectype for the vectorized
3422 stmt is obtained from the type of the result X:
3423 get_vectype_for_scalar_type (TREE_TYPE (X))
3425 This means that, contrary to "regular" reductions (or "regular" stmts in
3426 general), the following equation:
3427 STMT_VINFO_VECTYPE == get_vectype_for_scalar_type (TREE_TYPE (X))
3428 does *NOT* necessarily hold for reduction patterns. */
3431 vectorizable_reduction (gimple stmt
, gimple_stmt_iterator
*gsi
,
3436 tree loop_vec_def0
= NULL_TREE
, loop_vec_def1
= NULL_TREE
;
3437 stmt_vec_info stmt_info
= vinfo_for_stmt (stmt
);
3438 tree vectype
= STMT_VINFO_VECTYPE (stmt_info
);
3439 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_info
);
3440 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
3441 enum tree_code code
, orig_code
, epilog_reduc_code
;
3442 enum machine_mode vec_mode
;
3444 optab optab
, reduc_optab
;
3445 tree new_temp
= NULL_TREE
;
3448 enum vect_def_type dt
;
3449 gimple new_phi
= NULL
;
3453 stmt_vec_info orig_stmt_info
;
3454 tree expr
= NULL_TREE
;
3456 int nunits
= TYPE_VECTOR_SUBPARTS (vectype
);
3457 int ncopies
= LOOP_VINFO_VECT_FACTOR (loop_vinfo
) / nunits
;
3459 stmt_vec_info prev_stmt_info
, prev_phi_info
;
3460 gimple first_phi
= NULL
;
3461 bool single_defuse_cycle
= false;
3462 tree reduc_def
= NULL_TREE
;
3463 gimple new_stmt
= NULL
;
3466 bool nested_cycle
= false, found_nested_cycle_def
= false;
3467 gimple reduc_def_stmt
= NULL
;
3468 /* The default is that the reduction variable is the last in statement. */
3469 int reduc_index
= 2;
3470 bool double_reduc
= false, dummy
;
3472 struct loop
* def_stmt_loop
, *outer_loop
= NULL
;
3474 gimple def_arg_stmt
;
3476 if (nested_in_vect_loop_p (loop
, stmt
))
3480 nested_cycle
= true;
3483 gcc_assert (ncopies
>= 1);
3485 /* FORNOW: SLP not supported. */
3486 if (STMT_SLP_TYPE (stmt_info
))
3489 /* 1. Is vectorizable reduction? */
3490 /* Not supportable if the reduction variable is used in the loop. */
3491 if (STMT_VINFO_RELEVANT (stmt_info
) > vect_used_in_outer
)
3494 /* Reductions that are not used even in an enclosing outer-loop,
3495 are expected to be "live" (used out of the loop). */
3496 if (STMT_VINFO_RELEVANT (stmt_info
) == vect_unused_in_scope
3497 && !STMT_VINFO_LIVE_P (stmt_info
))
3500 /* Make sure it was already recognized as a reduction computation. */
3501 if (STMT_VINFO_DEF_TYPE (stmt_info
) != vect_reduction_def
3502 && STMT_VINFO_DEF_TYPE (stmt_info
) != vect_nested_cycle
)
3505 /* 2. Has this been recognized as a reduction pattern?
3507 Check if STMT represents a pattern that has been recognized
3508 in earlier analysis stages. For stmts that represent a pattern,
3509 the STMT_VINFO_RELATED_STMT field records the last stmt in
3510 the original sequence that constitutes the pattern. */
3512 orig_stmt
= STMT_VINFO_RELATED_STMT (stmt_info
);
3515 orig_stmt_info
= vinfo_for_stmt (orig_stmt
);
3516 gcc_assert (STMT_VINFO_RELATED_STMT (orig_stmt_info
) == stmt
);
3517 gcc_assert (STMT_VINFO_IN_PATTERN_P (orig_stmt_info
));
3518 gcc_assert (!STMT_VINFO_IN_PATTERN_P (stmt_info
));
3521 /* 3. Check the operands of the operation. The first operands are defined
3522 inside the loop body. The last operand is the reduction variable,
3523 which is defined by the loop-header-phi. */
3525 gcc_assert (is_gimple_assign (stmt
));
3528 switch (get_gimple_rhs_class (gimple_assign_rhs_code (stmt
)))
3530 case GIMPLE_SINGLE_RHS
:
3531 op_type
= TREE_OPERAND_LENGTH (gimple_assign_rhs1 (stmt
));
3532 if (op_type
== ternary_op
)
3534 tree rhs
= gimple_assign_rhs1 (stmt
);
3535 ops
[0] = TREE_OPERAND (rhs
, 0);
3536 ops
[1] = TREE_OPERAND (rhs
, 1);
3537 ops
[2] = TREE_OPERAND (rhs
, 2);
3538 code
= TREE_CODE (rhs
);
3544 case GIMPLE_BINARY_RHS
:
3545 code
= gimple_assign_rhs_code (stmt
);
3546 op_type
= TREE_CODE_LENGTH (code
);
3547 gcc_assert (op_type
== binary_op
);
3548 ops
[0] = gimple_assign_rhs1 (stmt
);
3549 ops
[1] = gimple_assign_rhs2 (stmt
);
3552 case GIMPLE_UNARY_RHS
:
3559 scalar_dest
= gimple_assign_lhs (stmt
);
3560 scalar_type
= TREE_TYPE (scalar_dest
);
3561 if (!POINTER_TYPE_P (scalar_type
) && !INTEGRAL_TYPE_P (scalar_type
)
3562 && !SCALAR_FLOAT_TYPE_P (scalar_type
))
3565 /* All uses but the last are expected to be defined in the loop.
3566 The last use is the reduction variable. In case of nested cycle this
3567 assumption is not true: we use reduc_index to record the index of the
3568 reduction variable. */
3569 for (i
= 0; i
< op_type
-1; i
++)
3571 /* The condition of COND_EXPR is checked in vectorizable_condition(). */
3572 if (i
== 0 && code
== COND_EXPR
)
3575 is_simple_use
= vect_is_simple_use (ops
[i
], loop_vinfo
, NULL
, &def_stmt
,
3577 gcc_assert (is_simple_use
);
3578 if (dt
!= vect_internal_def
3579 && dt
!= vect_external_def
3580 && dt
!= vect_constant_def
3581 && dt
!= vect_induction_def
3582 && !(dt
== vect_nested_cycle
&& nested_cycle
))
3585 if (dt
== vect_nested_cycle
)
3587 found_nested_cycle_def
= true;
3588 reduc_def_stmt
= def_stmt
;
3593 is_simple_use
= vect_is_simple_use (ops
[i
], loop_vinfo
, NULL
, &def_stmt
,
3595 gcc_assert (is_simple_use
);
3596 gcc_assert (dt
== vect_reduction_def
3597 || dt
== vect_nested_cycle
3598 || ((dt
== vect_internal_def
|| dt
== vect_external_def
3599 || dt
== vect_constant_def
|| dt
== vect_induction_def
)
3600 && nested_cycle
&& found_nested_cycle_def
));
3601 if (!found_nested_cycle_def
)
3602 reduc_def_stmt
= def_stmt
;
3604 gcc_assert (gimple_code (reduc_def_stmt
) == GIMPLE_PHI
);
3606 gcc_assert (orig_stmt
== vect_is_simple_reduction (loop_vinfo
,
3611 gcc_assert (stmt
== vect_is_simple_reduction (loop_vinfo
, reduc_def_stmt
,
3612 !nested_cycle
, &dummy
));
3614 if (STMT_VINFO_LIVE_P (vinfo_for_stmt (reduc_def_stmt
)))
3617 vec_mode
= TYPE_MODE (vectype
);
3619 if (code
== COND_EXPR
)
3621 if (!vectorizable_condition (stmt
, gsi
, NULL
, ops
[reduc_index
], 0))
3623 if (vect_print_dump_info (REPORT_DETAILS
))
3624 fprintf (vect_dump
, "unsupported condition in reduction");
3631 /* 4. Supportable by target? */
3633 /* 4.1. check support for the operation in the loop */
3634 optab
= optab_for_tree_code (code
, vectype
, optab_default
);
3637 if (vect_print_dump_info (REPORT_DETAILS
))
3638 fprintf (vect_dump
, "no optab.");
3643 if (optab_handler (optab
, vec_mode
)->insn_code
== CODE_FOR_nothing
)
3645 if (vect_print_dump_info (REPORT_DETAILS
))
3646 fprintf (vect_dump
, "op not supported by target.");
3648 if (GET_MODE_SIZE (vec_mode
) != UNITS_PER_WORD
3649 || LOOP_VINFO_VECT_FACTOR (loop_vinfo
)
3650 < vect_min_worthwhile_factor (code
))
3653 if (vect_print_dump_info (REPORT_DETAILS
))
3654 fprintf (vect_dump
, "proceeding using word mode.");
3657 /* Worthwhile without SIMD support? */
3658 if (!VECTOR_MODE_P (TYPE_MODE (vectype
))
3659 && LOOP_VINFO_VECT_FACTOR (loop_vinfo
)
3660 < vect_min_worthwhile_factor (code
))
3662 if (vect_print_dump_info (REPORT_DETAILS
))
3663 fprintf (vect_dump
, "not worthwhile without SIMD support.");
3669 /* 4.2. Check support for the epilog operation.
3671 If STMT represents a reduction pattern, then the type of the
3672 reduction variable may be different than the type of the rest
3673 of the arguments. For example, consider the case of accumulation
3674 of shorts into an int accumulator; The original code:
3675 S1: int_a = (int) short_a;
3676 orig_stmt-> S2: int_acc = plus <int_a ,int_acc>;
3679 STMT: int_acc = widen_sum <short_a, int_acc>
3682 1. The tree-code that is used to create the vector operation in the
3683 epilog code (that reduces the partial results) is not the
3684 tree-code of STMT, but is rather the tree-code of the original
3685 stmt from the pattern that STMT is replacing. I.e, in the example
3686 above we want to use 'widen_sum' in the loop, but 'plus' in the
3688 2. The type (mode) we use to check available target support
3689 for the vector operation to be created in the *epilog*, is
3690 determined by the type of the reduction variable (in the example
3691 above we'd check this: plus_optab[vect_int_mode]).
3692 However the type (mode) we use to check available target support
3693 for the vector operation to be created *inside the loop*, is
3694 determined by the type of the other arguments to STMT (in the
3695 example we'd check this: widen_sum_optab[vect_short_mode]).
3697 This is contrary to "regular" reductions, in which the types of all
3698 the arguments are the same as the type of the reduction variable.
3699 For "regular" reductions we can therefore use the same vector type
3700 (and also the same tree-code) when generating the epilog code and
3701 when generating the code inside the loop. */
3705 /* This is a reduction pattern: get the vectype from the type of the
3706 reduction variable, and get the tree-code from orig_stmt. */
3707 orig_code
= gimple_assign_rhs_code (orig_stmt
);
3708 vectype
= get_vectype_for_scalar_type (TREE_TYPE (def
));
3711 if (vect_print_dump_info (REPORT_DETAILS
))
3713 fprintf (vect_dump
, "unsupported data-type ");
3714 print_generic_expr (vect_dump
, TREE_TYPE (def
), TDF_SLIM
);
3719 vec_mode
= TYPE_MODE (vectype
);
3723 /* Regular reduction: use the same vectype and tree-code as used for
3724 the vector code inside the loop can be used for the epilog code. */
3730 def_bb
= gimple_bb (reduc_def_stmt
);
3731 def_stmt_loop
= def_bb
->loop_father
;
3732 def_arg
= PHI_ARG_DEF_FROM_EDGE (reduc_def_stmt
,
3733 loop_preheader_edge (def_stmt_loop
));
3734 if (TREE_CODE (def_arg
) == SSA_NAME
3735 && (def_arg_stmt
= SSA_NAME_DEF_STMT (def_arg
))
3736 && gimple_code (def_arg_stmt
) == GIMPLE_PHI
3737 && flow_bb_inside_loop_p (outer_loop
, gimple_bb (def_arg_stmt
))
3738 && vinfo_for_stmt (def_arg_stmt
)
3739 && STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def_arg_stmt
))
3740 == vect_double_reduction_def
)
3741 double_reduc
= true;
3744 epilog_reduc_code
= ERROR_MARK
;
3745 if (reduction_code_for_scalar_code (orig_code
, &epilog_reduc_code
))
3747 reduc_optab
= optab_for_tree_code (epilog_reduc_code
, vectype
,
3751 if (vect_print_dump_info (REPORT_DETAILS
))
3752 fprintf (vect_dump
, "no optab for reduction.");
3754 epilog_reduc_code
= ERROR_MARK
;
3758 && optab_handler (reduc_optab
, vec_mode
)->insn_code
3759 == CODE_FOR_nothing
)
3761 if (vect_print_dump_info (REPORT_DETAILS
))
3762 fprintf (vect_dump
, "reduc op not supported by target.");
3764 epilog_reduc_code
= ERROR_MARK
;
3769 if (!nested_cycle
|| double_reduc
)
3771 if (vect_print_dump_info (REPORT_DETAILS
))
3772 fprintf (vect_dump
, "no reduc code for scalar code.");
3778 if (double_reduc
&& ncopies
> 1)
3780 if (vect_print_dump_info (REPORT_DETAILS
))
3781 fprintf (vect_dump
, "multiple types in double reduction");
3786 if (!vec_stmt
) /* transformation not required. */
3788 STMT_VINFO_TYPE (stmt_info
) = reduc_vec_info_type
;
3789 if (!vect_model_reduction_cost (stmt_info
, epilog_reduc_code
, ncopies
))
3796 if (vect_print_dump_info (REPORT_DETAILS
))
3797 fprintf (vect_dump
, "transform reduction.");
3799 /* FORNOW: Multiple types are not supported for condition. */
3800 if (code
== COND_EXPR
)
3801 gcc_assert (ncopies
== 1);
3803 /* Create the destination vector */
3804 vec_dest
= vect_create_destination_var (scalar_dest
, vectype
);
3806 /* In case the vectorization factor (VF) is bigger than the number
3807 of elements that we can fit in a vectype (nunits), we have to generate
3808 more than one vector stmt - i.e - we need to "unroll" the
3809 vector stmt by a factor VF/nunits. For more details see documentation
3810 in vectorizable_operation. */
3812 /* If the reduction is used in an outer loop we need to generate
3813 VF intermediate results, like so (e.g. for ncopies=2):
3818 (i.e. we generate VF results in 2 registers).
3819 In this case we have a separate def-use cycle for each copy, and therefore
3820 for each copy we get the vector def for the reduction variable from the
3821 respective phi node created for this copy.
3823 Otherwise (the reduction is unused in the loop nest), we can combine
3824 together intermediate results, like so (e.g. for ncopies=2):
3828 (i.e. we generate VF/2 results in a single register).
3829 In this case for each copy we get the vector def for the reduction variable
3830 from the vectorized reduction operation generated in the previous iteration.
3833 if (STMT_VINFO_RELEVANT (stmt_info
) == vect_unused_in_scope
)
3835 single_defuse_cycle
= true;
3839 epilog_copies
= ncopies
;
3841 prev_stmt_info
= NULL
;
3842 prev_phi_info
= NULL
;
3843 for (j
= 0; j
< ncopies
; j
++)
3845 if (j
== 0 || !single_defuse_cycle
)
3847 /* Create the reduction-phi that defines the reduction-operand. */
3848 new_phi
= create_phi_node (vec_dest
, loop
->header
);
3849 set_vinfo_for_stmt (new_phi
, new_stmt_vec_info (new_phi
, loop_vinfo
,
3851 /* Get the vector def for the reduction variable from the phi
3853 reduc_def
= PHI_RESULT (new_phi
);
3856 if (code
== COND_EXPR
)
3858 first_phi
= new_phi
;
3859 vectorizable_condition (stmt
, gsi
, vec_stmt
, reduc_def
, reduc_index
);
3860 /* Multiple types are not supported for condition. */
3867 loop_vec_def0
= vect_get_vec_def_for_operand (ops
[!reduc_index
],
3869 if (op_type
== ternary_op
)
3871 if (reduc_index
== 0)
3872 loop_vec_def1
= vect_get_vec_def_for_operand (ops
[2], stmt
,
3875 loop_vec_def1
= vect_get_vec_def_for_operand (ops
[1], stmt
,
3879 /* Get the vector def for the reduction variable from the phi
3881 first_phi
= new_phi
;
3885 enum vect_def_type dt
= vect_unknown_def_type
; /* Dummy */
3886 loop_vec_def0
= vect_get_vec_def_for_stmt_copy (dt
, loop_vec_def0
);
3887 if (op_type
== ternary_op
)
3888 loop_vec_def1
= vect_get_vec_def_for_stmt_copy (dt
, loop_vec_def1
);
3890 if (single_defuse_cycle
)
3891 reduc_def
= gimple_assign_lhs (new_stmt
);
3893 reduc_def
= PHI_RESULT (new_phi
);
3895 STMT_VINFO_RELATED_STMT (prev_phi_info
) = new_phi
;
3898 /* Arguments are ready. Create the new vector stmt. */
3899 if (op_type
== binary_op
)
3901 if (reduc_index
== 0)
3902 expr
= build2 (code
, vectype
, reduc_def
, loop_vec_def0
);
3904 expr
= build2 (code
, vectype
, loop_vec_def0
, reduc_def
);
3908 if (reduc_index
== 0)
3909 expr
= build3 (code
, vectype
, reduc_def
, loop_vec_def0
,
3913 if (reduc_index
== 1)
3914 expr
= build3 (code
, vectype
, loop_vec_def0
, reduc_def
,
3917 expr
= build3 (code
, vectype
, loop_vec_def0
, loop_vec_def1
,
3922 new_stmt
= gimple_build_assign (vec_dest
, expr
);
3923 new_temp
= make_ssa_name (vec_dest
, new_stmt
);
3924 gimple_assign_set_lhs (new_stmt
, new_temp
);
3925 vect_finish_stmt_generation (stmt
, new_stmt
, gsi
);
3928 STMT_VINFO_VEC_STMT (stmt_info
) = *vec_stmt
= new_stmt
;
3930 STMT_VINFO_RELATED_STMT (prev_stmt_info
) = new_stmt
;
3932 prev_stmt_info
= vinfo_for_stmt (new_stmt
);
3933 prev_phi_info
= vinfo_for_stmt (new_phi
);
3936 /* Finalize the reduction-phi (set its arguments) and create the
3937 epilog reduction code. */
3938 if (!single_defuse_cycle
|| code
== COND_EXPR
)
3939 new_temp
= gimple_assign_lhs (*vec_stmt
);
3941 vect_create_epilog_for_reduction (new_temp
, stmt
, epilog_copies
,
3942 epilog_reduc_code
, first_phi
, reduc_index
,
3947 /* Function vect_min_worthwhile_factor.
3949 For a loop where we could vectorize the operation indicated by CODE,
3950 return the minimum vectorization factor that makes it worthwhile
3951 to use generic vectors. */
3953 vect_min_worthwhile_factor (enum tree_code code
)
3974 /* Function vectorizable_induction
3976 Check if PHI performs an induction computation that can be vectorized.
3977 If VEC_STMT is also passed, vectorize the induction PHI: create a vectorized
3978 phi to replace it, put it in VEC_STMT, and add it to the same basic block.
3979 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
3982 vectorizable_induction (gimple phi
, gimple_stmt_iterator
*gsi ATTRIBUTE_UNUSED
,
3985 stmt_vec_info stmt_info
= vinfo_for_stmt (phi
);
3986 tree vectype
= STMT_VINFO_VECTYPE (stmt_info
);
3987 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_info
);
3988 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
3989 int nunits
= TYPE_VECTOR_SUBPARTS (vectype
);
3990 int ncopies
= LOOP_VINFO_VECT_FACTOR (loop_vinfo
) / nunits
;
3993 gcc_assert (ncopies
>= 1);
3994 /* FORNOW. This restriction should be relaxed. */
3995 if (nested_in_vect_loop_p (loop
, phi
) && ncopies
> 1)
3997 if (vect_print_dump_info (REPORT_DETAILS
))
3998 fprintf (vect_dump
, "multiple types in nested loop.");
4002 if (!STMT_VINFO_RELEVANT_P (stmt_info
))
4005 /* FORNOW: SLP not supported. */
4006 if (STMT_SLP_TYPE (stmt_info
))
4009 gcc_assert (STMT_VINFO_DEF_TYPE (stmt_info
) == vect_induction_def
);
4011 if (gimple_code (phi
) != GIMPLE_PHI
)
4014 if (!vec_stmt
) /* transformation not required. */
4016 STMT_VINFO_TYPE (stmt_info
) = induc_vec_info_type
;
4017 if (vect_print_dump_info (REPORT_DETAILS
))
4018 fprintf (vect_dump
, "=== vectorizable_induction ===");
4019 vect_model_induction_cost (stmt_info
, ncopies
);
4025 if (vect_print_dump_info (REPORT_DETAILS
))
4026 fprintf (vect_dump
, "transform induction phi.");
4028 vec_def
= get_initial_def_for_induction (phi
);
4029 *vec_stmt
= SSA_NAME_DEF_STMT (vec_def
);
4033 /* Function vectorizable_live_operation.
4035 STMT computes a value that is used outside the loop. Check if
4036 it can be supported. */
4039 vectorizable_live_operation (gimple stmt
,
4040 gimple_stmt_iterator
*gsi ATTRIBUTE_UNUSED
,
4041 gimple
*vec_stmt ATTRIBUTE_UNUSED
)
4043 stmt_vec_info stmt_info
= vinfo_for_stmt (stmt
);
4044 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_info
);
4045 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
4051 enum vect_def_type dt
;
4052 enum tree_code code
;
4053 enum gimple_rhs_class rhs_class
;
4055 gcc_assert (STMT_VINFO_LIVE_P (stmt_info
));
4057 if (STMT_VINFO_DEF_TYPE (stmt_info
) == vect_reduction_def
)
4060 if (!is_gimple_assign (stmt
))
4063 if (TREE_CODE (gimple_assign_lhs (stmt
)) != SSA_NAME
)
4066 /* FORNOW. CHECKME. */
4067 if (nested_in_vect_loop_p (loop
, stmt
))
4070 code
= gimple_assign_rhs_code (stmt
);
4071 op_type
= TREE_CODE_LENGTH (code
);
4072 rhs_class
= get_gimple_rhs_class (code
);
4073 gcc_assert (rhs_class
!= GIMPLE_UNARY_RHS
|| op_type
== unary_op
);
4074 gcc_assert (rhs_class
!= GIMPLE_BINARY_RHS
|| op_type
== binary_op
);
4076 /* FORNOW: support only if all uses are invariant. This means
4077 that the scalar operations can remain in place, unvectorized.
4078 The original last scalar value that they compute will be used. */
4080 for (i
= 0; i
< op_type
; i
++)
4082 if (rhs_class
== GIMPLE_SINGLE_RHS
)
4083 op
= TREE_OPERAND (gimple_op (stmt
, 1), i
);
4085 op
= gimple_op (stmt
, i
+ 1);
4087 && !vect_is_simple_use (op
, loop_vinfo
, NULL
, &def_stmt
, &def
, &dt
))
4089 if (vect_print_dump_info (REPORT_DETAILS
))
4090 fprintf (vect_dump
, "use not simple.");
4094 if (dt
!= vect_external_def
&& dt
!= vect_constant_def
)
4098 /* No transformation is required for the cases we currently support. */
4102 /* Function vect_transform_loop.
4104 The analysis phase has determined that the loop is vectorizable.
4105 Vectorize the loop - created vectorized stmts to replace the scalar
4106 stmts in the loop, and update the loop exit condition. */
4109 vect_transform_loop (loop_vec_info loop_vinfo
)
4111 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
4112 basic_block
*bbs
= LOOP_VINFO_BBS (loop_vinfo
);
4113 int nbbs
= loop
->num_nodes
;
4114 gimple_stmt_iterator si
;
4117 int vectorization_factor
= LOOP_VINFO_VECT_FACTOR (loop_vinfo
);
4119 bool slp_scheduled
= false;
4120 unsigned int nunits
;
4121 tree cond_expr
= NULL_TREE
;
4122 gimple_seq cond_expr_stmt_list
= NULL
;
4123 bool do_peeling_for_loop_bound
;
4125 if (vect_print_dump_info (REPORT_DETAILS
))
4126 fprintf (vect_dump
, "=== vec_transform_loop ===");
4128 /* Peel the loop if there are data refs with unknown alignment.
4129 Only one data ref with unknown store is allowed. */
4131 if (LOOP_PEELING_FOR_ALIGNMENT (loop_vinfo
))
4132 vect_do_peeling_for_alignment (loop_vinfo
);
4134 do_peeling_for_loop_bound
4135 = (!LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo
)
4136 || (LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo
)
4137 && LOOP_VINFO_INT_NITERS (loop_vinfo
) % vectorization_factor
!= 0));
4139 if (LOOP_REQUIRES_VERSIONING_FOR_ALIGNMENT (loop_vinfo
)
4140 || LOOP_REQUIRES_VERSIONING_FOR_ALIAS (loop_vinfo
))
4141 vect_loop_versioning (loop_vinfo
,
4142 !do_peeling_for_loop_bound
,
4143 &cond_expr
, &cond_expr_stmt_list
);
4145 /* If the loop has a symbolic number of iterations 'n' (i.e. it's not a
4146 compile time constant), or it is a constant that doesn't divide by the
4147 vectorization factor, then an epilog loop needs to be created.
4148 We therefore duplicate the loop: the original loop will be vectorized,
4149 and will compute the first (n/VF) iterations. The second copy of the loop
4150 will remain scalar and will compute the remaining (n%VF) iterations.
4151 (VF is the vectorization factor). */
4153 if (do_peeling_for_loop_bound
)
4154 vect_do_peeling_for_loop_bound (loop_vinfo
, &ratio
,
4155 cond_expr
, cond_expr_stmt_list
);
4157 ratio
= build_int_cst (TREE_TYPE (LOOP_VINFO_NITERS (loop_vinfo
)),
4158 LOOP_VINFO_INT_NITERS (loop_vinfo
) / vectorization_factor
);
4160 /* 1) Make sure the loop header has exactly two entries
4161 2) Make sure we have a preheader basic block. */
4163 gcc_assert (EDGE_COUNT (loop
->header
->preds
) == 2);
4165 split_edge (loop_preheader_edge (loop
));
4167 /* FORNOW: the vectorizer supports only loops which body consist
4168 of one basic block (header + empty latch). When the vectorizer will
4169 support more involved loop forms, the order by which the BBs are
4170 traversed need to be reconsidered. */
4172 for (i
= 0; i
< nbbs
; i
++)
4174 basic_block bb
= bbs
[i
];
4175 stmt_vec_info stmt_info
;
4178 for (si
= gsi_start_phis (bb
); !gsi_end_p (si
); gsi_next (&si
))
4180 phi
= gsi_stmt (si
);
4181 if (vect_print_dump_info (REPORT_DETAILS
))
4183 fprintf (vect_dump
, "------>vectorizing phi: ");
4184 print_gimple_stmt (vect_dump
, phi
, 0, TDF_SLIM
);
4186 stmt_info
= vinfo_for_stmt (phi
);
4190 if (!STMT_VINFO_RELEVANT_P (stmt_info
)
4191 && !STMT_VINFO_LIVE_P (stmt_info
))
4194 if ((TYPE_VECTOR_SUBPARTS (STMT_VINFO_VECTYPE (stmt_info
))
4195 != (unsigned HOST_WIDE_INT
) vectorization_factor
)
4196 && vect_print_dump_info (REPORT_DETAILS
))
4197 fprintf (vect_dump
, "multiple-types.");
4199 if (STMT_VINFO_DEF_TYPE (stmt_info
) == vect_induction_def
)
4201 if (vect_print_dump_info (REPORT_DETAILS
))
4202 fprintf (vect_dump
, "transform phi.");
4203 vect_transform_stmt (phi
, NULL
, NULL
, NULL
, NULL
);
4207 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
);)
4209 gimple stmt
= gsi_stmt (si
);
4212 if (vect_print_dump_info (REPORT_DETAILS
))
4214 fprintf (vect_dump
, "------>vectorizing statement: ");
4215 print_gimple_stmt (vect_dump
, stmt
, 0, TDF_SLIM
);
4218 stmt_info
= vinfo_for_stmt (stmt
);
4220 /* vector stmts created in the outer-loop during vectorization of
4221 stmts in an inner-loop may not have a stmt_info, and do not
4222 need to be vectorized. */
4229 if (!STMT_VINFO_RELEVANT_P (stmt_info
)
4230 && !STMT_VINFO_LIVE_P (stmt_info
))
4236 gcc_assert (STMT_VINFO_VECTYPE (stmt_info
));
4238 (unsigned int) TYPE_VECTOR_SUBPARTS (STMT_VINFO_VECTYPE (stmt_info
));
4239 if (!STMT_SLP_TYPE (stmt_info
)
4240 && nunits
!= (unsigned int) vectorization_factor
4241 && vect_print_dump_info (REPORT_DETAILS
))
4242 /* For SLP VF is set according to unrolling factor, and not to
4243 vector size, hence for SLP this print is not valid. */
4244 fprintf (vect_dump
, "multiple-types.");
4246 /* SLP. Schedule all the SLP instances when the first SLP stmt is
4248 if (STMT_SLP_TYPE (stmt_info
))
4252 slp_scheduled
= true;
4254 if (vect_print_dump_info (REPORT_DETAILS
))
4255 fprintf (vect_dump
, "=== scheduling SLP instances ===");
4257 vect_schedule_slp (loop_vinfo
, NULL
);
4260 /* Hybrid SLP stmts must be vectorized in addition to SLP. */
4261 if (!vinfo_for_stmt (stmt
) || PURE_SLP_STMT (stmt_info
))
4268 /* -------- vectorize statement ------------ */
4269 if (vect_print_dump_info (REPORT_DETAILS
))
4270 fprintf (vect_dump
, "transform statement.");
4272 strided_store
= false;
4273 is_store
= vect_transform_stmt (stmt
, &si
, &strided_store
, NULL
, NULL
);
4276 if (STMT_VINFO_STRIDED_ACCESS (stmt_info
))
4278 /* Interleaving. If IS_STORE is TRUE, the vectorization of the
4279 interleaving chain was completed - free all the stores in
4281 vect_remove_stores (DR_GROUP_FIRST_DR (stmt_info
));
4282 gsi_remove (&si
, true);
4287 /* Free the attached stmt_vec_info and remove the stmt. */
4288 free_stmt_vec_info (stmt
);
4289 gsi_remove (&si
, true);
4297 slpeel_make_loop_iterate_ntimes (loop
, ratio
);
4299 /* The memory tags and pointers in vectorized statements need to
4300 have their SSA forms updated. FIXME, why can't this be delayed
4301 until all the loops have been transformed? */
4302 update_ssa (TODO_update_ssa
);
4304 if (vect_print_dump_info (REPORT_VECTORIZED_LOCATIONS
))
4305 fprintf (vect_dump
, "LOOP VECTORIZED.");
4306 if (loop
->inner
&& vect_print_dump_info (REPORT_VECTORIZED_LOCATIONS
))
4307 fprintf (vect_dump
, "OUTER LOOP VECTORIZED.");