2 Copyright (C) 2003-2017 Free Software Foundation, Inc.
3 Contributed by Dorit Naishlos <dorit@il.ibm.com> and
4 Ira Rosen <irar@il.ibm.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
31 #include "tree-pass.h"
33 #include "optabs-tree.h"
34 #include "diagnostic-core.h"
35 #include "fold-const.h"
36 #include "stor-layout.h"
39 #include "gimple-iterator.h"
40 #include "gimplify-me.h"
41 #include "tree-ssa-loop-ivopts.h"
42 #include "tree-ssa-loop-manip.h"
43 #include "tree-ssa-loop-niter.h"
44 #include "tree-ssa-loop.h"
47 #include "tree-scalar-evolution.h"
48 #include "tree-vectorizer.h"
49 #include "gimple-fold.h"
52 #include "tree-if-conv.h"
54 /* Loop Vectorization Pass.
56 This pass tries to vectorize loops.
58 For example, the vectorizer transforms the following simple loop:
60 short a[N]; short b[N]; short c[N]; int i;
66 as if it was manually vectorized by rewriting the source code into:
68 typedef int __attribute__((mode(V8HI))) v8hi;
69 short a[N]; short b[N]; short c[N]; int i;
70 v8hi *pa = (v8hi*)a, *pb = (v8hi*)b, *pc = (v8hi*)c;
73 for (i=0; i<N/8; i++){
80 The main entry to this pass is vectorize_loops(), in which
81 the vectorizer applies a set of analyses on a given set of loops,
82 followed by the actual vectorization transformation for the loops that
83 had successfully passed the analysis phase.
84 Throughout this pass we make a distinction between two types of
85 data: scalars (which are represented by SSA_NAMES), and memory references
86 ("data-refs"). These two types of data require different handling both
87 during analysis and transformation. The types of data-refs that the
88 vectorizer currently supports are ARRAY_REFS which base is an array DECL
89 (not a pointer), and INDIRECT_REFS through pointers; both array and pointer
90 accesses are required to have a simple (consecutive) access pattern.
94 The driver for the analysis phase is vect_analyze_loop().
95 It applies a set of analyses, some of which rely on the scalar evolution
96 analyzer (scev) developed by Sebastian Pop.
98 During the analysis phase the vectorizer records some information
99 per stmt in a "stmt_vec_info" struct which is attached to each stmt in the
100 loop, as well as general information about the loop as a whole, which is
101 recorded in a "loop_vec_info" struct attached to each loop.
103 Transformation phase:
104 =====================
105 The loop transformation phase scans all the stmts in the loop, and
106 creates a vector stmt (or a sequence of stmts) for each scalar stmt S in
107 the loop that needs to be vectorized. It inserts the vector code sequence
108 just before the scalar stmt S, and records a pointer to the vector code
109 in STMT_VINFO_VEC_STMT (stmt_info) (stmt_info is the stmt_vec_info struct
110 attached to S). This pointer will be used for the vectorization of following
111 stmts which use the def of stmt S. Stmt S is removed if it writes to memory;
112 otherwise, we rely on dead code elimination for removing it.
114 For example, say stmt S1 was vectorized into stmt VS1:
117 S1: b = x[i]; STMT_VINFO_VEC_STMT (stmt_info (S1)) = VS1
120 To vectorize stmt S2, the vectorizer first finds the stmt that defines
121 the operand 'b' (S1), and gets the relevant vector def 'vb' from the
122 vector stmt VS1 pointed to by STMT_VINFO_VEC_STMT (stmt_info (S1)). The
123 resulting sequence would be:
126 S1: b = x[i]; STMT_VINFO_VEC_STMT (stmt_info (S1)) = VS1
128 S2: a = b; STMT_VINFO_VEC_STMT (stmt_info (S2)) = VS2
130 Operands that are not SSA_NAMEs, are data-refs that appear in
131 load/store operations (like 'x[i]' in S1), and are handled differently.
135 Currently the only target specific information that is used is the
136 size of the vector (in bytes) - "TARGET_VECTORIZE_UNITS_PER_SIMD_WORD".
137 Targets that can support different sizes of vectors, for now will need
138 to specify one value for "TARGET_VECTORIZE_UNITS_PER_SIMD_WORD". More
139 flexibility will be added in the future.
141 Since we only vectorize operations which vector form can be
142 expressed using existing tree codes, to verify that an operation is
143 supported, the vectorizer checks the relevant optab at the relevant
144 machine_mode (e.g, optab_handler (add_optab, V8HImode)). If
145 the value found is CODE_FOR_nothing, then there's no target support, and
146 we can't vectorize the stmt.
148 For additional information on this project see:
149 http://gcc.gnu.org/projects/tree-ssa/vectorization.html
152 static void vect_estimate_min_profitable_iters (loop_vec_info
, int *, int *);
154 /* Function vect_determine_vectorization_factor
156 Determine the vectorization factor (VF). VF is the number of data elements
157 that are operated upon in parallel in a single iteration of the vectorized
158 loop. For example, when vectorizing a loop that operates on 4byte elements,
159 on a target with vector size (VS) 16byte, the VF is set to 4, since 4
160 elements can fit in a single vector register.
162 We currently support vectorization of loops in which all types operated upon
163 are of the same size. Therefore this function currently sets VF according to
164 the size of the types operated upon, and fails if there are multiple sizes
167 VF is also the factor by which the loop iterations are strip-mined, e.g.:
174 for (i=0; i<N; i+=VF){
175 a[i:VF] = b[i:VF] + c[i:VF];
180 vect_determine_vectorization_factor (loop_vec_info loop_vinfo
)
182 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
183 basic_block
*bbs
= LOOP_VINFO_BBS (loop_vinfo
);
184 unsigned nbbs
= loop
->num_nodes
;
185 unsigned int vectorization_factor
= 0;
186 tree scalar_type
= NULL_TREE
;
190 stmt_vec_info stmt_info
;
193 gimple
*stmt
, *pattern_stmt
= NULL
;
194 gimple_seq pattern_def_seq
= NULL
;
195 gimple_stmt_iterator pattern_def_si
= gsi_none ();
196 bool analyze_pattern_stmt
= false;
198 auto_vec
<stmt_vec_info
> mask_producers
;
200 if (dump_enabled_p ())
201 dump_printf_loc (MSG_NOTE
, vect_location
,
202 "=== vect_determine_vectorization_factor ===\n");
204 for (i
= 0; i
< nbbs
; i
++)
206 basic_block bb
= bbs
[i
];
208 for (gphi_iterator si
= gsi_start_phis (bb
); !gsi_end_p (si
);
212 stmt_info
= vinfo_for_stmt (phi
);
213 if (dump_enabled_p ())
215 dump_printf_loc (MSG_NOTE
, vect_location
, "==> examining phi: ");
216 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
, phi
, 0);
219 gcc_assert (stmt_info
);
221 if (STMT_VINFO_RELEVANT_P (stmt_info
)
222 || STMT_VINFO_LIVE_P (stmt_info
))
224 gcc_assert (!STMT_VINFO_VECTYPE (stmt_info
));
225 scalar_type
= TREE_TYPE (PHI_RESULT (phi
));
227 if (dump_enabled_p ())
229 dump_printf_loc (MSG_NOTE
, vect_location
,
230 "get vectype for scalar type: ");
231 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, scalar_type
);
232 dump_printf (MSG_NOTE
, "\n");
235 vectype
= get_vectype_for_scalar_type (scalar_type
);
238 if (dump_enabled_p ())
240 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
241 "not vectorized: unsupported "
243 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
,
245 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
249 STMT_VINFO_VECTYPE (stmt_info
) = vectype
;
251 if (dump_enabled_p ())
253 dump_printf_loc (MSG_NOTE
, vect_location
, "vectype: ");
254 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, vectype
);
255 dump_printf (MSG_NOTE
, "\n");
258 nunits
= TYPE_VECTOR_SUBPARTS (vectype
);
259 if (dump_enabled_p ())
260 dump_printf_loc (MSG_NOTE
, vect_location
, "nunits = %d\n",
263 if (!vectorization_factor
264 || (nunits
> vectorization_factor
))
265 vectorization_factor
= nunits
;
269 for (gimple_stmt_iterator si
= gsi_start_bb (bb
);
270 !gsi_end_p (si
) || analyze_pattern_stmt
;)
274 if (analyze_pattern_stmt
)
277 stmt
= gsi_stmt (si
);
279 stmt_info
= vinfo_for_stmt (stmt
);
281 if (dump_enabled_p ())
283 dump_printf_loc (MSG_NOTE
, vect_location
,
284 "==> examining statement: ");
285 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
, stmt
, 0);
288 gcc_assert (stmt_info
);
290 /* Skip stmts which do not need to be vectorized. */
291 if ((!STMT_VINFO_RELEVANT_P (stmt_info
)
292 && !STMT_VINFO_LIVE_P (stmt_info
))
293 || gimple_clobber_p (stmt
))
295 if (STMT_VINFO_IN_PATTERN_P (stmt_info
)
296 && (pattern_stmt
= STMT_VINFO_RELATED_STMT (stmt_info
))
297 && (STMT_VINFO_RELEVANT_P (vinfo_for_stmt (pattern_stmt
))
298 || STMT_VINFO_LIVE_P (vinfo_for_stmt (pattern_stmt
))))
301 stmt_info
= vinfo_for_stmt (pattern_stmt
);
302 if (dump_enabled_p ())
304 dump_printf_loc (MSG_NOTE
, vect_location
,
305 "==> examining pattern statement: ");
306 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
, stmt
, 0);
311 if (dump_enabled_p ())
312 dump_printf_loc (MSG_NOTE
, vect_location
, "skip.\n");
317 else if (STMT_VINFO_IN_PATTERN_P (stmt_info
)
318 && (pattern_stmt
= STMT_VINFO_RELATED_STMT (stmt_info
))
319 && (STMT_VINFO_RELEVANT_P (vinfo_for_stmt (pattern_stmt
))
320 || STMT_VINFO_LIVE_P (vinfo_for_stmt (pattern_stmt
))))
321 analyze_pattern_stmt
= true;
323 /* If a pattern statement has def stmts, analyze them too. */
324 if (is_pattern_stmt_p (stmt_info
))
326 if (pattern_def_seq
== NULL
)
328 pattern_def_seq
= STMT_VINFO_PATTERN_DEF_SEQ (stmt_info
);
329 pattern_def_si
= gsi_start (pattern_def_seq
);
331 else if (!gsi_end_p (pattern_def_si
))
332 gsi_next (&pattern_def_si
);
333 if (pattern_def_seq
!= NULL
)
335 gimple
*pattern_def_stmt
= NULL
;
336 stmt_vec_info pattern_def_stmt_info
= NULL
;
338 while (!gsi_end_p (pattern_def_si
))
340 pattern_def_stmt
= gsi_stmt (pattern_def_si
);
341 pattern_def_stmt_info
342 = vinfo_for_stmt (pattern_def_stmt
);
343 if (STMT_VINFO_RELEVANT_P (pattern_def_stmt_info
)
344 || STMT_VINFO_LIVE_P (pattern_def_stmt_info
))
346 gsi_next (&pattern_def_si
);
349 if (!gsi_end_p (pattern_def_si
))
351 if (dump_enabled_p ())
353 dump_printf_loc (MSG_NOTE
, vect_location
,
354 "==> examining pattern def stmt: ");
355 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
,
356 pattern_def_stmt
, 0);
359 stmt
= pattern_def_stmt
;
360 stmt_info
= pattern_def_stmt_info
;
364 pattern_def_si
= gsi_none ();
365 analyze_pattern_stmt
= false;
369 analyze_pattern_stmt
= false;
372 if (gimple_get_lhs (stmt
) == NULL_TREE
373 /* MASK_STORE has no lhs, but is ok. */
374 && (!is_gimple_call (stmt
)
375 || !gimple_call_internal_p (stmt
)
376 || gimple_call_internal_fn (stmt
) != IFN_MASK_STORE
))
378 if (is_gimple_call (stmt
))
380 /* Ignore calls with no lhs. These must be calls to
381 #pragma omp simd functions, and what vectorization factor
382 it really needs can't be determined until
383 vectorizable_simd_clone_call. */
384 if (!analyze_pattern_stmt
&& gsi_end_p (pattern_def_si
))
386 pattern_def_seq
= NULL
;
391 if (dump_enabled_p ())
393 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
394 "not vectorized: irregular stmt.");
395 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
, stmt
,
401 if (VECTOR_MODE_P (TYPE_MODE (gimple_expr_type (stmt
))))
403 if (dump_enabled_p ())
405 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
406 "not vectorized: vector stmt in loop:");
407 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
, stmt
, 0);
414 if (STMT_VINFO_VECTYPE (stmt_info
))
416 /* The only case when a vectype had been already set is for stmts
417 that contain a dataref, or for "pattern-stmts" (stmts
418 generated by the vectorizer to represent/replace a certain
420 gcc_assert (STMT_VINFO_DATA_REF (stmt_info
)
421 || is_pattern_stmt_p (stmt_info
)
422 || !gsi_end_p (pattern_def_si
));
423 vectype
= STMT_VINFO_VECTYPE (stmt_info
);
427 gcc_assert (!STMT_VINFO_DATA_REF (stmt_info
));
428 if (gimple_call_internal_p (stmt
, IFN_MASK_STORE
))
429 scalar_type
= TREE_TYPE (gimple_call_arg (stmt
, 3));
431 scalar_type
= TREE_TYPE (gimple_get_lhs (stmt
));
433 /* Bool ops don't participate in vectorization factor
434 computation. For comparison use compared types to
436 if (VECT_SCALAR_BOOLEAN_TYPE_P (scalar_type
)
437 && is_gimple_assign (stmt
)
438 && gimple_assign_rhs_code (stmt
) != COND_EXPR
)
440 if (STMT_VINFO_RELEVANT_P (stmt_info
)
441 || STMT_VINFO_LIVE_P (stmt_info
))
442 mask_producers
.safe_push (stmt_info
);
445 if (TREE_CODE_CLASS (gimple_assign_rhs_code (stmt
))
447 && !VECT_SCALAR_BOOLEAN_TYPE_P
448 (TREE_TYPE (gimple_assign_rhs1 (stmt
))))
449 scalar_type
= TREE_TYPE (gimple_assign_rhs1 (stmt
));
452 if (!analyze_pattern_stmt
&& gsi_end_p (pattern_def_si
))
454 pattern_def_seq
= NULL
;
461 if (dump_enabled_p ())
463 dump_printf_loc (MSG_NOTE
, vect_location
,
464 "get vectype for scalar type: ");
465 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, scalar_type
);
466 dump_printf (MSG_NOTE
, "\n");
468 vectype
= get_vectype_for_scalar_type (scalar_type
);
471 if (dump_enabled_p ())
473 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
474 "not vectorized: unsupported "
476 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
,
478 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
484 STMT_VINFO_VECTYPE (stmt_info
) = vectype
;
486 if (dump_enabled_p ())
488 dump_printf_loc (MSG_NOTE
, vect_location
, "vectype: ");
489 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, vectype
);
490 dump_printf (MSG_NOTE
, "\n");
494 /* Don't try to compute VF out scalar types if we stmt
495 produces boolean vector. Use result vectype instead. */
496 if (VECTOR_BOOLEAN_TYPE_P (vectype
))
497 vf_vectype
= vectype
;
500 /* The vectorization factor is according to the smallest
501 scalar type (or the largest vector size, but we only
502 support one vector size per loop). */
504 scalar_type
= vect_get_smallest_scalar_type (stmt
, &dummy
,
506 if (dump_enabled_p ())
508 dump_printf_loc (MSG_NOTE
, vect_location
,
509 "get vectype for scalar type: ");
510 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, scalar_type
);
511 dump_printf (MSG_NOTE
, "\n");
513 vf_vectype
= get_vectype_for_scalar_type (scalar_type
);
517 if (dump_enabled_p ())
519 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
520 "not vectorized: unsupported data-type ");
521 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
,
523 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
528 if ((GET_MODE_SIZE (TYPE_MODE (vectype
))
529 != GET_MODE_SIZE (TYPE_MODE (vf_vectype
))))
531 if (dump_enabled_p ())
533 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
534 "not vectorized: different sized vector "
535 "types in statement, ");
536 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
,
538 dump_printf (MSG_MISSED_OPTIMIZATION
, " and ");
539 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
,
541 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
546 if (dump_enabled_p ())
548 dump_printf_loc (MSG_NOTE
, vect_location
, "vectype: ");
549 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, vf_vectype
);
550 dump_printf (MSG_NOTE
, "\n");
553 nunits
= TYPE_VECTOR_SUBPARTS (vf_vectype
);
554 if (dump_enabled_p ())
555 dump_printf_loc (MSG_NOTE
, vect_location
, "nunits = %d\n", nunits
);
556 if (!vectorization_factor
557 || (nunits
> vectorization_factor
))
558 vectorization_factor
= nunits
;
560 if (!analyze_pattern_stmt
&& gsi_end_p (pattern_def_si
))
562 pattern_def_seq
= NULL
;
568 /* TODO: Analyze cost. Decide if worth while to vectorize. */
569 if (dump_enabled_p ())
570 dump_printf_loc (MSG_NOTE
, vect_location
, "vectorization factor = %d\n",
571 vectorization_factor
);
572 if (vectorization_factor
<= 1)
574 if (dump_enabled_p ())
575 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
576 "not vectorized: unsupported data-type\n");
579 LOOP_VINFO_VECT_FACTOR (loop_vinfo
) = vectorization_factor
;
581 for (i
= 0; i
< mask_producers
.length (); i
++)
583 tree mask_type
= NULL
;
585 stmt
= STMT_VINFO_STMT (mask_producers
[i
]);
587 if (is_gimple_assign (stmt
)
588 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt
)) == tcc_comparison
589 && !VECT_SCALAR_BOOLEAN_TYPE_P
590 (TREE_TYPE (gimple_assign_rhs1 (stmt
))))
592 scalar_type
= TREE_TYPE (gimple_assign_rhs1 (stmt
));
593 mask_type
= get_mask_type_for_scalar_type (scalar_type
);
597 if (dump_enabled_p ())
598 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
599 "not vectorized: unsupported mask\n");
608 enum vect_def_type dt
;
610 FOR_EACH_SSA_TREE_OPERAND (rhs
, stmt
, iter
, SSA_OP_USE
)
612 if (!vect_is_simple_use (rhs
, mask_producers
[i
]->vinfo
,
613 &def_stmt
, &dt
, &vectype
))
615 if (dump_enabled_p ())
617 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
618 "not vectorized: can't compute mask type "
620 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
, stmt
,
626 /* No vectype probably means external definition.
627 Allow it in case there is another operand which
628 allows to determine mask type. */
634 else if (TYPE_VECTOR_SUBPARTS (mask_type
)
635 != TYPE_VECTOR_SUBPARTS (vectype
))
637 if (dump_enabled_p ())
639 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
640 "not vectorized: different sized masks "
641 "types in statement, ");
642 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
,
644 dump_printf (MSG_MISSED_OPTIMIZATION
, " and ");
645 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
,
647 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
651 else if (VECTOR_BOOLEAN_TYPE_P (mask_type
)
652 != VECTOR_BOOLEAN_TYPE_P (vectype
))
654 if (dump_enabled_p ())
656 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
657 "not vectorized: mixed mask and "
658 "nonmask vector types in statement, ");
659 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
,
661 dump_printf (MSG_MISSED_OPTIMIZATION
, " and ");
662 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
,
664 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
670 /* We may compare boolean value loaded as vector of integers.
671 Fix mask_type in such case. */
673 && !VECTOR_BOOLEAN_TYPE_P (mask_type
)
674 && gimple_code (stmt
) == GIMPLE_ASSIGN
675 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt
)) == tcc_comparison
)
676 mask_type
= build_same_sized_truth_vector_type (mask_type
);
679 /* No mask_type should mean loop invariant predicate.
680 This is probably a subject for optimization in
684 if (dump_enabled_p ())
686 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
687 "not vectorized: can't compute mask type "
689 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
, stmt
,
695 STMT_VINFO_VECTYPE (mask_producers
[i
]) = mask_type
;
702 /* Function vect_is_simple_iv_evolution.
704 FORNOW: A simple evolution of an induction variables in the loop is
705 considered a polynomial evolution. */
708 vect_is_simple_iv_evolution (unsigned loop_nb
, tree access_fn
, tree
* init
,
713 tree evolution_part
= evolution_part_in_loop_num (access_fn
, loop_nb
);
716 /* When there is no evolution in this loop, the evolution function
718 if (evolution_part
== NULL_TREE
)
721 /* When the evolution is a polynomial of degree >= 2
722 the evolution function is not "simple". */
723 if (tree_is_chrec (evolution_part
))
726 step_expr
= evolution_part
;
727 init_expr
= unshare_expr (initial_condition_in_loop_num (access_fn
, loop_nb
));
729 if (dump_enabled_p ())
731 dump_printf_loc (MSG_NOTE
, vect_location
, "step: ");
732 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, step_expr
);
733 dump_printf (MSG_NOTE
, ", init: ");
734 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, init_expr
);
735 dump_printf (MSG_NOTE
, "\n");
741 if (TREE_CODE (step_expr
) != INTEGER_CST
742 && (TREE_CODE (step_expr
) != SSA_NAME
743 || ((bb
= gimple_bb (SSA_NAME_DEF_STMT (step_expr
)))
744 && flow_bb_inside_loop_p (get_loop (cfun
, loop_nb
), bb
))
745 || (!INTEGRAL_TYPE_P (TREE_TYPE (step_expr
))
746 && (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (step_expr
))
747 || !flag_associative_math
)))
748 && (TREE_CODE (step_expr
) != REAL_CST
749 || !flag_associative_math
))
751 if (dump_enabled_p ())
752 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
760 /* Function vect_analyze_scalar_cycles_1.
762 Examine the cross iteration def-use cycles of scalar variables
763 in LOOP. LOOP_VINFO represents the loop that is now being
764 considered for vectorization (can be LOOP, or an outer-loop
768 vect_analyze_scalar_cycles_1 (loop_vec_info loop_vinfo
, struct loop
*loop
)
770 basic_block bb
= loop
->header
;
772 auto_vec
<gimple
*, 64> worklist
;
776 if (dump_enabled_p ())
777 dump_printf_loc (MSG_NOTE
, vect_location
,
778 "=== vect_analyze_scalar_cycles ===\n");
780 /* First - identify all inductions. Reduction detection assumes that all the
781 inductions have been identified, therefore, this order must not be
783 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
785 gphi
*phi
= gsi
.phi ();
786 tree access_fn
= NULL
;
787 tree def
= PHI_RESULT (phi
);
788 stmt_vec_info stmt_vinfo
= vinfo_for_stmt (phi
);
790 if (dump_enabled_p ())
792 dump_printf_loc (MSG_NOTE
, vect_location
, "Analyze phi: ");
793 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
, phi
, 0);
796 /* Skip virtual phi's. The data dependences that are associated with
797 virtual defs/uses (i.e., memory accesses) are analyzed elsewhere. */
798 if (virtual_operand_p (def
))
801 STMT_VINFO_DEF_TYPE (stmt_vinfo
) = vect_unknown_def_type
;
803 /* Analyze the evolution function. */
804 access_fn
= analyze_scalar_evolution (loop
, def
);
807 STRIP_NOPS (access_fn
);
808 if (dump_enabled_p ())
810 dump_printf_loc (MSG_NOTE
, vect_location
,
811 "Access function of PHI: ");
812 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, access_fn
);
813 dump_printf (MSG_NOTE
, "\n");
815 STMT_VINFO_LOOP_PHI_EVOLUTION_BASE_UNCHANGED (stmt_vinfo
)
816 = initial_condition_in_loop_num (access_fn
, loop
->num
);
817 STMT_VINFO_LOOP_PHI_EVOLUTION_PART (stmt_vinfo
)
818 = evolution_part_in_loop_num (access_fn
, loop
->num
);
822 || !vect_is_simple_iv_evolution (loop
->num
, access_fn
, &init
, &step
)
823 || (LOOP_VINFO_LOOP (loop_vinfo
) != loop
824 && TREE_CODE (step
) != INTEGER_CST
))
826 worklist
.safe_push (phi
);
830 gcc_assert (STMT_VINFO_LOOP_PHI_EVOLUTION_BASE_UNCHANGED (stmt_vinfo
)
832 gcc_assert (STMT_VINFO_LOOP_PHI_EVOLUTION_PART (stmt_vinfo
) != NULL_TREE
);
834 if (dump_enabled_p ())
835 dump_printf_loc (MSG_NOTE
, vect_location
, "Detected induction.\n");
836 STMT_VINFO_DEF_TYPE (stmt_vinfo
) = vect_induction_def
;
840 /* Second - identify all reductions and nested cycles. */
841 while (worklist
.length () > 0)
843 gimple
*phi
= worklist
.pop ();
844 tree def
= PHI_RESULT (phi
);
845 stmt_vec_info stmt_vinfo
= vinfo_for_stmt (phi
);
848 if (dump_enabled_p ())
850 dump_printf_loc (MSG_NOTE
, vect_location
, "Analyze phi: ");
851 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
, phi
, 0);
854 gcc_assert (!virtual_operand_p (def
)
855 && STMT_VINFO_DEF_TYPE (stmt_vinfo
) == vect_unknown_def_type
);
857 reduc_stmt
= vect_force_simple_reduction (loop_vinfo
, phi
,
858 &double_reduc
, false);
863 if (dump_enabled_p ())
864 dump_printf_loc (MSG_NOTE
, vect_location
,
865 "Detected double reduction.\n");
867 STMT_VINFO_DEF_TYPE (stmt_vinfo
) = vect_double_reduction_def
;
868 STMT_VINFO_DEF_TYPE (vinfo_for_stmt (reduc_stmt
)) =
869 vect_double_reduction_def
;
873 if (loop
!= LOOP_VINFO_LOOP (loop_vinfo
))
875 if (dump_enabled_p ())
876 dump_printf_loc (MSG_NOTE
, vect_location
,
877 "Detected vectorizable nested cycle.\n");
879 STMT_VINFO_DEF_TYPE (stmt_vinfo
) = vect_nested_cycle
;
880 STMT_VINFO_DEF_TYPE (vinfo_for_stmt (reduc_stmt
)) =
885 if (dump_enabled_p ())
886 dump_printf_loc (MSG_NOTE
, vect_location
,
887 "Detected reduction.\n");
889 STMT_VINFO_DEF_TYPE (stmt_vinfo
) = vect_reduction_def
;
890 STMT_VINFO_DEF_TYPE (vinfo_for_stmt (reduc_stmt
)) =
892 /* Store the reduction cycles for possible vectorization in
894 LOOP_VINFO_REDUCTIONS (loop_vinfo
).safe_push (reduc_stmt
);
899 if (dump_enabled_p ())
900 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
901 "Unknown def-use cycle pattern.\n");
906 /* Function vect_analyze_scalar_cycles.
908 Examine the cross iteration def-use cycles of scalar variables, by
909 analyzing the loop-header PHIs of scalar variables. Classify each
910 cycle as one of the following: invariant, induction, reduction, unknown.
911 We do that for the loop represented by LOOP_VINFO, and also to its
912 inner-loop, if exists.
913 Examples for scalar cycles:
928 vect_analyze_scalar_cycles (loop_vec_info loop_vinfo
)
930 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
932 vect_analyze_scalar_cycles_1 (loop_vinfo
, loop
);
934 /* When vectorizing an outer-loop, the inner-loop is executed sequentially.
935 Reductions in such inner-loop therefore have different properties than
936 the reductions in the nest that gets vectorized:
937 1. When vectorized, they are executed in the same order as in the original
938 scalar loop, so we can't change the order of computation when
940 2. FIXME: Inner-loop reductions can be used in the inner-loop, so the
941 current checks are too strict. */
944 vect_analyze_scalar_cycles_1 (loop_vinfo
, loop
->inner
);
947 /* Transfer group and reduction information from STMT to its pattern stmt. */
950 vect_fixup_reduc_chain (gimple
*stmt
)
952 gimple
*firstp
= STMT_VINFO_RELATED_STMT (vinfo_for_stmt (stmt
));
954 gcc_assert (!GROUP_FIRST_ELEMENT (vinfo_for_stmt (firstp
))
955 && GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt
)));
956 GROUP_SIZE (vinfo_for_stmt (firstp
)) = GROUP_SIZE (vinfo_for_stmt (stmt
));
959 stmtp
= STMT_VINFO_RELATED_STMT (vinfo_for_stmt (stmt
));
960 GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmtp
)) = firstp
;
961 stmt
= GROUP_NEXT_ELEMENT (vinfo_for_stmt (stmt
));
963 GROUP_NEXT_ELEMENT (vinfo_for_stmt (stmtp
))
964 = STMT_VINFO_RELATED_STMT (vinfo_for_stmt (stmt
));
967 STMT_VINFO_DEF_TYPE (vinfo_for_stmt (stmtp
)) = vect_reduction_def
;
970 /* Fixup scalar cycles that now have their stmts detected as patterns. */
973 vect_fixup_scalar_cycles_with_patterns (loop_vec_info loop_vinfo
)
978 FOR_EACH_VEC_ELT (LOOP_VINFO_REDUCTION_CHAINS (loop_vinfo
), i
, first
)
979 if (STMT_VINFO_IN_PATTERN_P (vinfo_for_stmt (first
)))
981 gimple
*next
= GROUP_NEXT_ELEMENT (vinfo_for_stmt (first
));
984 if (! STMT_VINFO_IN_PATTERN_P (vinfo_for_stmt (next
)))
986 next
= GROUP_NEXT_ELEMENT (vinfo_for_stmt (next
));
988 /* If not all stmt in the chain are patterns try to handle
989 the chain without patterns. */
992 vect_fixup_reduc_chain (first
);
993 LOOP_VINFO_REDUCTION_CHAINS (loop_vinfo
)[i
]
994 = STMT_VINFO_RELATED_STMT (vinfo_for_stmt (first
));
999 /* Function vect_get_loop_niters.
1001 Determine how many iterations the loop is executed and place it
1002 in NUMBER_OF_ITERATIONS. Place the number of latch iterations
1003 in NUMBER_OF_ITERATIONSM1. Place the condition under which the
1004 niter information holds in ASSUMPTIONS.
1006 Return the loop exit condition. */
1010 vect_get_loop_niters (struct loop
*loop
, tree
*assumptions
,
1011 tree
*number_of_iterations
, tree
*number_of_iterationsm1
)
1013 edge exit
= single_exit (loop
);
1014 struct tree_niter_desc niter_desc
;
1015 tree niter_assumptions
, niter
, may_be_zero
;
1016 gcond
*cond
= get_loop_exit_condition (loop
);
1018 *assumptions
= boolean_true_node
;
1019 *number_of_iterationsm1
= chrec_dont_know
;
1020 *number_of_iterations
= chrec_dont_know
;
1021 if (dump_enabled_p ())
1022 dump_printf_loc (MSG_NOTE
, vect_location
,
1023 "=== get_loop_niters ===\n");
1028 niter
= chrec_dont_know
;
1029 may_be_zero
= NULL_TREE
;
1030 niter_assumptions
= boolean_true_node
;
1031 if (!number_of_iterations_exit_assumptions (loop
, exit
, &niter_desc
, NULL
)
1032 || chrec_contains_undetermined (niter_desc
.niter
))
1035 niter_assumptions
= niter_desc
.assumptions
;
1036 may_be_zero
= niter_desc
.may_be_zero
;
1037 niter
= niter_desc
.niter
;
1039 if (may_be_zero
&& integer_zerop (may_be_zero
))
1040 may_be_zero
= NULL_TREE
;
1044 if (COMPARISON_CLASS_P (may_be_zero
))
1046 /* Try to combine may_be_zero with assumptions, this can simplify
1047 computation of niter expression. */
1048 if (niter_assumptions
&& !integer_nonzerop (niter_assumptions
))
1049 niter_assumptions
= fold_build2 (TRUTH_AND_EXPR
, boolean_type_node
,
1051 fold_build1 (TRUTH_NOT_EXPR
,
1055 niter
= fold_build3 (COND_EXPR
, TREE_TYPE (niter
), may_be_zero
,
1056 build_int_cst (TREE_TYPE (niter
), 0), niter
);
1058 may_be_zero
= NULL_TREE
;
1060 else if (integer_nonzerop (may_be_zero
))
1062 *number_of_iterationsm1
= build_int_cst (TREE_TYPE (niter
), 0);
1063 *number_of_iterations
= build_int_cst (TREE_TYPE (niter
), 1);
1070 *assumptions
= niter_assumptions
;
1071 *number_of_iterationsm1
= niter
;
1073 /* We want the number of loop header executions which is the number
1074 of latch executions plus one.
1075 ??? For UINT_MAX latch executions this number overflows to zero
1076 for loops like do { n++; } while (n != 0); */
1077 if (niter
&& !chrec_contains_undetermined (niter
))
1078 niter
= fold_build2 (PLUS_EXPR
, TREE_TYPE (niter
), unshare_expr (niter
),
1079 build_int_cst (TREE_TYPE (niter
), 1));
1080 *number_of_iterations
= niter
;
1085 /* Function bb_in_loop_p
1087 Used as predicate for dfs order traversal of the loop bbs. */
1090 bb_in_loop_p (const_basic_block bb
, const void *data
)
1092 const struct loop
*const loop
= (const struct loop
*)data
;
1093 if (flow_bb_inside_loop_p (loop
, bb
))
1099 /* Function new_loop_vec_info.
1101 Create and initialize a new loop_vec_info struct for LOOP, as well as
1102 stmt_vec_info structs for all the stmts in LOOP. */
1104 static loop_vec_info
1105 new_loop_vec_info (struct loop
*loop
)
1109 gimple_stmt_iterator si
;
1110 unsigned int i
, nbbs
;
1112 res
= (loop_vec_info
) xcalloc (1, sizeof (struct _loop_vec_info
));
1113 res
->kind
= vec_info::loop
;
1114 LOOP_VINFO_LOOP (res
) = loop
;
1116 bbs
= get_loop_body (loop
);
1118 /* Create/Update stmt_info for all stmts in the loop. */
1119 for (i
= 0; i
< loop
->num_nodes
; i
++)
1121 basic_block bb
= bbs
[i
];
1123 for (si
= gsi_start_phis (bb
); !gsi_end_p (si
); gsi_next (&si
))
1125 gimple
*phi
= gsi_stmt (si
);
1126 gimple_set_uid (phi
, 0);
1127 set_vinfo_for_stmt (phi
, new_stmt_vec_info (phi
, res
));
1130 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); gsi_next (&si
))
1132 gimple
*stmt
= gsi_stmt (si
);
1133 gimple_set_uid (stmt
, 0);
1134 set_vinfo_for_stmt (stmt
, new_stmt_vec_info (stmt
, res
));
1138 /* CHECKME: We want to visit all BBs before their successors (except for
1139 latch blocks, for which this assertion wouldn't hold). In the simple
1140 case of the loop forms we allow, a dfs order of the BBs would the same
1141 as reversed postorder traversal, so we are safe. */
1144 bbs
= XCNEWVEC (basic_block
, loop
->num_nodes
);
1145 nbbs
= dfs_enumerate_from (loop
->header
, 0, bb_in_loop_p
,
1146 bbs
, loop
->num_nodes
, loop
);
1147 gcc_assert (nbbs
== loop
->num_nodes
);
1149 LOOP_VINFO_BBS (res
) = bbs
;
1150 LOOP_VINFO_NITERSM1 (res
) = NULL
;
1151 LOOP_VINFO_NITERS (res
) = NULL
;
1152 LOOP_VINFO_NITERS_UNCHANGED (res
) = NULL
;
1153 LOOP_VINFO_NITERS_ASSUMPTIONS (res
) = NULL
;
1154 LOOP_VINFO_COST_MODEL_THRESHOLD (res
) = 0;
1155 LOOP_VINFO_VECTORIZABLE_P (res
) = 0;
1156 LOOP_VINFO_PEELING_FOR_ALIGNMENT (res
) = 0;
1157 LOOP_VINFO_VECT_FACTOR (res
) = 0;
1158 LOOP_VINFO_LOOP_NEST (res
) = vNULL
;
1159 LOOP_VINFO_DATAREFS (res
) = vNULL
;
1160 LOOP_VINFO_DDRS (res
) = vNULL
;
1161 LOOP_VINFO_UNALIGNED_DR (res
) = NULL
;
1162 LOOP_VINFO_MAY_MISALIGN_STMTS (res
) = vNULL
;
1163 LOOP_VINFO_MAY_ALIAS_DDRS (res
) = vNULL
;
1164 LOOP_VINFO_GROUPED_STORES (res
) = vNULL
;
1165 LOOP_VINFO_REDUCTIONS (res
) = vNULL
;
1166 LOOP_VINFO_REDUCTION_CHAINS (res
) = vNULL
;
1167 LOOP_VINFO_SLP_INSTANCES (res
) = vNULL
;
1168 LOOP_VINFO_SLP_UNROLLING_FACTOR (res
) = 1;
1169 LOOP_VINFO_TARGET_COST_DATA (res
) = init_cost (loop
);
1170 LOOP_VINFO_PEELING_FOR_GAPS (res
) = false;
1171 LOOP_VINFO_PEELING_FOR_NITER (res
) = false;
1172 LOOP_VINFO_OPERANDS_SWAPPED (res
) = false;
1173 LOOP_VINFO_ORIG_LOOP_INFO (res
) = NULL
;
1179 /* Function destroy_loop_vec_info.
1181 Free LOOP_VINFO struct, as well as all the stmt_vec_info structs of all the
1182 stmts in the loop. */
1185 destroy_loop_vec_info (loop_vec_info loop_vinfo
, bool clean_stmts
)
1190 gimple_stmt_iterator si
;
1192 vec
<slp_instance
> slp_instances
;
1193 slp_instance instance
;
1199 loop
= LOOP_VINFO_LOOP (loop_vinfo
);
1201 bbs
= LOOP_VINFO_BBS (loop_vinfo
);
1202 nbbs
= clean_stmts
? loop
->num_nodes
: 0;
1203 swapped
= LOOP_VINFO_OPERANDS_SWAPPED (loop_vinfo
);
1205 for (j
= 0; j
< nbbs
; j
++)
1207 basic_block bb
= bbs
[j
];
1208 for (si
= gsi_start_phis (bb
); !gsi_end_p (si
); gsi_next (&si
))
1209 free_stmt_vec_info (gsi_stmt (si
));
1211 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); )
1213 gimple
*stmt
= gsi_stmt (si
);
1215 /* We may have broken canonical form by moving a constant
1216 into RHS1 of a commutative op. Fix such occurrences. */
1217 if (swapped
&& is_gimple_assign (stmt
))
1219 enum tree_code code
= gimple_assign_rhs_code (stmt
);
1221 if ((code
== PLUS_EXPR
1222 || code
== POINTER_PLUS_EXPR
1223 || code
== MULT_EXPR
)
1224 && CONSTANT_CLASS_P (gimple_assign_rhs1 (stmt
)))
1225 swap_ssa_operands (stmt
,
1226 gimple_assign_rhs1_ptr (stmt
),
1227 gimple_assign_rhs2_ptr (stmt
));
1228 else if (code
== COND_EXPR
1229 && CONSTANT_CLASS_P (gimple_assign_rhs2 (stmt
)))
1231 tree cond_expr
= gimple_assign_rhs1 (stmt
);
1232 enum tree_code cond_code
= TREE_CODE (cond_expr
);
1234 if (TREE_CODE_CLASS (cond_code
) == tcc_comparison
)
1236 bool honor_nans
= HONOR_NANS (TREE_OPERAND (cond_expr
,
1238 cond_code
= invert_tree_comparison (cond_code
,
1240 if (cond_code
!= ERROR_MARK
)
1242 TREE_SET_CODE (cond_expr
, cond_code
);
1243 swap_ssa_operands (stmt
,
1244 gimple_assign_rhs2_ptr (stmt
),
1245 gimple_assign_rhs3_ptr (stmt
));
1251 /* Free stmt_vec_info. */
1252 free_stmt_vec_info (stmt
);
1257 free (LOOP_VINFO_BBS (loop_vinfo
));
1258 vect_destroy_datarefs (loop_vinfo
);
1259 free_dependence_relations (LOOP_VINFO_DDRS (loop_vinfo
));
1260 LOOP_VINFO_LOOP_NEST (loop_vinfo
).release ();
1261 LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo
).release ();
1262 LOOP_VINFO_COMP_ALIAS_DDRS (loop_vinfo
).release ();
1263 LOOP_VINFO_MAY_ALIAS_DDRS (loop_vinfo
).release ();
1264 slp_instances
= LOOP_VINFO_SLP_INSTANCES (loop_vinfo
);
1265 FOR_EACH_VEC_ELT (slp_instances
, j
, instance
)
1266 vect_free_slp_instance (instance
);
1268 LOOP_VINFO_SLP_INSTANCES (loop_vinfo
).release ();
1269 LOOP_VINFO_GROUPED_STORES (loop_vinfo
).release ();
1270 LOOP_VINFO_REDUCTIONS (loop_vinfo
).release ();
1271 LOOP_VINFO_REDUCTION_CHAINS (loop_vinfo
).release ();
1273 destroy_cost_data (LOOP_VINFO_TARGET_COST_DATA (loop_vinfo
));
1274 loop_vinfo
->scalar_cost_vec
.release ();
1281 /* Calculate the cost of one scalar iteration of the loop. */
1283 vect_compute_single_scalar_iteration_cost (loop_vec_info loop_vinfo
)
1285 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
1286 basic_block
*bbs
= LOOP_VINFO_BBS (loop_vinfo
);
1287 int nbbs
= loop
->num_nodes
, factor
, scalar_single_iter_cost
= 0;
1288 int innerloop_iters
, i
;
1290 /* Count statements in scalar loop. Using this as scalar cost for a single
1293 TODO: Add outer loop support.
1295 TODO: Consider assigning different costs to different scalar
1299 innerloop_iters
= 1;
1301 innerloop_iters
= 50; /* FIXME */
1303 for (i
= 0; i
< nbbs
; i
++)
1305 gimple_stmt_iterator si
;
1306 basic_block bb
= bbs
[i
];
1308 if (bb
->loop_father
== loop
->inner
)
1309 factor
= innerloop_iters
;
1313 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); gsi_next (&si
))
1315 gimple
*stmt
= gsi_stmt (si
);
1316 stmt_vec_info stmt_info
= vinfo_for_stmt (stmt
);
1318 if (!is_gimple_assign (stmt
) && !is_gimple_call (stmt
))
1321 /* Skip stmts that are not vectorized inside the loop. */
1323 && !STMT_VINFO_RELEVANT_P (stmt_info
)
1324 && (!STMT_VINFO_LIVE_P (stmt_info
)
1325 || !VECTORIZABLE_CYCLE_DEF (STMT_VINFO_DEF_TYPE (stmt_info
)))
1326 && !STMT_VINFO_IN_PATTERN_P (stmt_info
))
1329 vect_cost_for_stmt kind
;
1330 if (STMT_VINFO_DATA_REF (stmt_info
))
1332 if (DR_IS_READ (STMT_VINFO_DATA_REF (stmt_info
)))
1335 kind
= scalar_store
;
1340 scalar_single_iter_cost
1341 += record_stmt_cost (&LOOP_VINFO_SCALAR_ITERATION_COST (loop_vinfo
),
1342 factor
, kind
, stmt_info
, 0, vect_prologue
);
1345 LOOP_VINFO_SINGLE_SCALAR_ITERATION_COST (loop_vinfo
)
1346 = scalar_single_iter_cost
;
1350 /* Function vect_analyze_loop_form_1.
1352 Verify that certain CFG restrictions hold, including:
1353 - the loop has a pre-header
1354 - the loop has a single entry and exit
1355 - the loop exit condition is simple enough
1356 - the number of iterations can be analyzed, i.e, a countable loop. The
1357 niter could be analyzed under some assumptions. */
1360 vect_analyze_loop_form_1 (struct loop
*loop
, gcond
**loop_cond
,
1361 tree
*assumptions
, tree
*number_of_iterationsm1
,
1362 tree
*number_of_iterations
, gcond
**inner_loop_cond
)
1364 if (dump_enabled_p ())
1365 dump_printf_loc (MSG_NOTE
, vect_location
,
1366 "=== vect_analyze_loop_form ===\n");
1368 /* Different restrictions apply when we are considering an inner-most loop,
1369 vs. an outer (nested) loop.
1370 (FORNOW. May want to relax some of these restrictions in the future). */
1374 /* Inner-most loop. We currently require that the number of BBs is
1375 exactly 2 (the header and latch). Vectorizable inner-most loops
1386 if (loop
->num_nodes
!= 2)
1388 if (dump_enabled_p ())
1389 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1390 "not vectorized: control flow in loop.\n");
1394 if (empty_block_p (loop
->header
))
1396 if (dump_enabled_p ())
1397 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1398 "not vectorized: empty loop.\n");
1404 struct loop
*innerloop
= loop
->inner
;
1407 /* Nested loop. We currently require that the loop is doubly-nested,
1408 contains a single inner loop, and the number of BBs is exactly 5.
1409 Vectorizable outer-loops look like this:
1421 The inner-loop has the properties expected of inner-most loops
1422 as described above. */
1424 if ((loop
->inner
)->inner
|| (loop
->inner
)->next
)
1426 if (dump_enabled_p ())
1427 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1428 "not vectorized: multiple nested loops.\n");
1432 if (loop
->num_nodes
!= 5)
1434 if (dump_enabled_p ())
1435 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1436 "not vectorized: control flow in loop.\n");
1440 entryedge
= loop_preheader_edge (innerloop
);
1441 if (entryedge
->src
!= loop
->header
1442 || !single_exit (innerloop
)
1443 || single_exit (innerloop
)->dest
!= EDGE_PRED (loop
->latch
, 0)->src
)
1445 if (dump_enabled_p ())
1446 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1447 "not vectorized: unsupported outerloop form.\n");
1451 /* Analyze the inner-loop. */
1452 tree inner_niterm1
, inner_niter
, inner_assumptions
;
1453 if (! vect_analyze_loop_form_1 (loop
->inner
, inner_loop_cond
,
1454 &inner_assumptions
, &inner_niterm1
,
1456 /* Don't support analyzing niter under assumptions for inner
1458 || !integer_onep (inner_assumptions
))
1460 if (dump_enabled_p ())
1461 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1462 "not vectorized: Bad inner loop.\n");
1466 if (!expr_invariant_in_loop_p (loop
, inner_niter
))
1468 if (dump_enabled_p ())
1469 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1470 "not vectorized: inner-loop count not"
1475 if (dump_enabled_p ())
1476 dump_printf_loc (MSG_NOTE
, vect_location
,
1477 "Considering outer-loop vectorization.\n");
1480 if (!single_exit (loop
)
1481 || EDGE_COUNT (loop
->header
->preds
) != 2)
1483 if (dump_enabled_p ())
1485 if (!single_exit (loop
))
1486 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1487 "not vectorized: multiple exits.\n");
1488 else if (EDGE_COUNT (loop
->header
->preds
) != 2)
1489 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1490 "not vectorized: too many incoming edges.\n");
1495 /* We assume that the loop exit condition is at the end of the loop. i.e,
1496 that the loop is represented as a do-while (with a proper if-guard
1497 before the loop if needed), where the loop header contains all the
1498 executable statements, and the latch is empty. */
1499 if (!empty_block_p (loop
->latch
)
1500 || !gimple_seq_empty_p (phi_nodes (loop
->latch
)))
1502 if (dump_enabled_p ())
1503 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1504 "not vectorized: latch block not empty.\n");
1508 /* Make sure the exit is not abnormal. */
1509 edge e
= single_exit (loop
);
1510 if (e
->flags
& EDGE_ABNORMAL
)
1512 if (dump_enabled_p ())
1513 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1514 "not vectorized: abnormal loop exit edge.\n");
1518 *loop_cond
= vect_get_loop_niters (loop
, assumptions
, number_of_iterations
,
1519 number_of_iterationsm1
);
1522 if (dump_enabled_p ())
1523 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1524 "not vectorized: complicated exit condition.\n");
1528 if (integer_zerop (*assumptions
)
1529 || !*number_of_iterations
1530 || chrec_contains_undetermined (*number_of_iterations
))
1532 if (dump_enabled_p ())
1533 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1534 "not vectorized: number of iterations cannot be "
1539 if (integer_zerop (*number_of_iterations
))
1541 if (dump_enabled_p ())
1542 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1543 "not vectorized: number of iterations = 0.\n");
1550 /* Analyze LOOP form and return a loop_vec_info if it is of suitable form. */
1553 vect_analyze_loop_form (struct loop
*loop
)
1555 tree assumptions
, number_of_iterations
, number_of_iterationsm1
;
1556 gcond
*loop_cond
, *inner_loop_cond
= NULL
;
1558 if (! vect_analyze_loop_form_1 (loop
, &loop_cond
,
1559 &assumptions
, &number_of_iterationsm1
,
1560 &number_of_iterations
, &inner_loop_cond
))
1563 loop_vec_info loop_vinfo
= new_loop_vec_info (loop
);
1564 LOOP_VINFO_NITERSM1 (loop_vinfo
) = number_of_iterationsm1
;
1565 LOOP_VINFO_NITERS (loop_vinfo
) = number_of_iterations
;
1566 LOOP_VINFO_NITERS_UNCHANGED (loop_vinfo
) = number_of_iterations
;
1567 if (!integer_onep (assumptions
))
1569 /* We consider to vectorize this loop by versioning it under
1570 some assumptions. In order to do this, we need to clear
1571 existing information computed by scev and niter analyzer. */
1573 free_numbers_of_iterations_estimates_loop (loop
);
1574 /* Also set flag for this loop so that following scev and niter
1575 analysis are done under the assumptions. */
1576 loop_constraint_set (loop
, LOOP_C_FINITE
);
1577 /* Also record the assumptions for versioning. */
1578 LOOP_VINFO_NITERS_ASSUMPTIONS (loop_vinfo
) = assumptions
;
1581 if (!LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo
))
1583 if (dump_enabled_p ())
1585 dump_printf_loc (MSG_NOTE
, vect_location
,
1586 "Symbolic number of iterations is ");
1587 dump_generic_expr (MSG_NOTE
, TDF_DETAILS
, number_of_iterations
);
1588 dump_printf (MSG_NOTE
, "\n");
1592 STMT_VINFO_TYPE (vinfo_for_stmt (loop_cond
)) = loop_exit_ctrl_vec_info_type
;
1593 if (inner_loop_cond
)
1594 STMT_VINFO_TYPE (vinfo_for_stmt (inner_loop_cond
))
1595 = loop_exit_ctrl_vec_info_type
;
1597 gcc_assert (!loop
->aux
);
1598 loop
->aux
= loop_vinfo
;
1604 /* Scan the loop stmts and dependent on whether there are any (non-)SLP
1605 statements update the vectorization factor. */
1608 vect_update_vf_for_slp (loop_vec_info loop_vinfo
)
1610 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
1611 basic_block
*bbs
= LOOP_VINFO_BBS (loop_vinfo
);
1612 int nbbs
= loop
->num_nodes
;
1613 unsigned int vectorization_factor
;
1616 if (dump_enabled_p ())
1617 dump_printf_loc (MSG_NOTE
, vect_location
,
1618 "=== vect_update_vf_for_slp ===\n");
1620 vectorization_factor
= LOOP_VINFO_VECT_FACTOR (loop_vinfo
);
1621 gcc_assert (vectorization_factor
!= 0);
1623 /* If all the stmts in the loop can be SLPed, we perform only SLP, and
1624 vectorization factor of the loop is the unrolling factor required by
1625 the SLP instances. If that unrolling factor is 1, we say, that we
1626 perform pure SLP on loop - cross iteration parallelism is not
1628 bool only_slp_in_loop
= true;
1629 for (i
= 0; i
< nbbs
; i
++)
1631 basic_block bb
= bbs
[i
];
1632 for (gimple_stmt_iterator si
= gsi_start_bb (bb
); !gsi_end_p (si
);
1635 gimple
*stmt
= gsi_stmt (si
);
1636 stmt_vec_info stmt_info
= vinfo_for_stmt (stmt
);
1637 if (STMT_VINFO_IN_PATTERN_P (stmt_info
)
1638 && STMT_VINFO_RELATED_STMT (stmt_info
))
1640 stmt
= STMT_VINFO_RELATED_STMT (stmt_info
);
1641 stmt_info
= vinfo_for_stmt (stmt
);
1643 if ((STMT_VINFO_RELEVANT_P (stmt_info
)
1644 || VECTORIZABLE_CYCLE_DEF (STMT_VINFO_DEF_TYPE (stmt_info
)))
1645 && !PURE_SLP_STMT (stmt_info
))
1646 /* STMT needs both SLP and loop-based vectorization. */
1647 only_slp_in_loop
= false;
1651 if (only_slp_in_loop
)
1653 dump_printf_loc (MSG_NOTE
, vect_location
,
1654 "Loop contains only SLP stmts\n");
1655 vectorization_factor
= LOOP_VINFO_SLP_UNROLLING_FACTOR (loop_vinfo
);
1659 dump_printf_loc (MSG_NOTE
, vect_location
,
1660 "Loop contains SLP and non-SLP stmts\n");
1661 vectorization_factor
1662 = least_common_multiple (vectorization_factor
,
1663 LOOP_VINFO_SLP_UNROLLING_FACTOR (loop_vinfo
));
1666 LOOP_VINFO_VECT_FACTOR (loop_vinfo
) = vectorization_factor
;
1667 if (dump_enabled_p ())
1668 dump_printf_loc (MSG_NOTE
, vect_location
,
1669 "Updating vectorization factor to %d\n",
1670 vectorization_factor
);
1673 /* Function vect_analyze_loop_operations.
1675 Scan the loop stmts and make sure they are all vectorizable. */
1678 vect_analyze_loop_operations (loop_vec_info loop_vinfo
)
1680 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
1681 basic_block
*bbs
= LOOP_VINFO_BBS (loop_vinfo
);
1682 int nbbs
= loop
->num_nodes
;
1684 stmt_vec_info stmt_info
;
1685 bool need_to_vectorize
= false;
1688 if (dump_enabled_p ())
1689 dump_printf_loc (MSG_NOTE
, vect_location
,
1690 "=== vect_analyze_loop_operations ===\n");
1692 for (i
= 0; i
< nbbs
; i
++)
1694 basic_block bb
= bbs
[i
];
1696 for (gphi_iterator si
= gsi_start_phis (bb
); !gsi_end_p (si
);
1699 gphi
*phi
= si
.phi ();
1702 stmt_info
= vinfo_for_stmt (phi
);
1703 if (dump_enabled_p ())
1705 dump_printf_loc (MSG_NOTE
, vect_location
, "examining phi: ");
1706 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
, phi
, 0);
1708 if (virtual_operand_p (gimple_phi_result (phi
)))
1711 /* Inner-loop loop-closed exit phi in outer-loop vectorization
1712 (i.e., a phi in the tail of the outer-loop). */
1713 if (! is_loop_header_bb_p (bb
))
1715 /* FORNOW: we currently don't support the case that these phis
1716 are not used in the outerloop (unless it is double reduction,
1717 i.e., this phi is vect_reduction_def), cause this case
1718 requires to actually do something here. */
1719 if (STMT_VINFO_LIVE_P (stmt_info
)
1720 && STMT_VINFO_DEF_TYPE (stmt_info
)
1721 != vect_double_reduction_def
)
1723 if (dump_enabled_p ())
1724 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1725 "Unsupported loop-closed phi in "
1730 /* If PHI is used in the outer loop, we check that its operand
1731 is defined in the inner loop. */
1732 if (STMT_VINFO_RELEVANT_P (stmt_info
))
1735 gimple
*op_def_stmt
;
1737 if (gimple_phi_num_args (phi
) != 1)
1740 phi_op
= PHI_ARG_DEF (phi
, 0);
1741 if (TREE_CODE (phi_op
) != SSA_NAME
)
1744 op_def_stmt
= SSA_NAME_DEF_STMT (phi_op
);
1745 if (gimple_nop_p (op_def_stmt
)
1746 || !flow_bb_inside_loop_p (loop
, gimple_bb (op_def_stmt
))
1747 || !vinfo_for_stmt (op_def_stmt
))
1750 if (STMT_VINFO_RELEVANT (vinfo_for_stmt (op_def_stmt
))
1751 != vect_used_in_outer
1752 && STMT_VINFO_RELEVANT (vinfo_for_stmt (op_def_stmt
))
1753 != vect_used_in_outer_by_reduction
)
1760 gcc_assert (stmt_info
);
1762 if ((STMT_VINFO_RELEVANT (stmt_info
) == vect_used_in_scope
1763 || STMT_VINFO_LIVE_P (stmt_info
))
1764 && STMT_VINFO_DEF_TYPE (stmt_info
) != vect_induction_def
)
1766 /* A scalar-dependence cycle that we don't support. */
1767 if (dump_enabled_p ())
1768 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1769 "not vectorized: scalar dependence cycle.\n");
1773 if (STMT_VINFO_RELEVANT_P (stmt_info
))
1775 need_to_vectorize
= true;
1776 if (STMT_VINFO_DEF_TYPE (stmt_info
) == vect_induction_def
1777 && ! PURE_SLP_STMT (stmt_info
))
1778 ok
= vectorizable_induction (phi
, NULL
, NULL
, NULL
);
1781 if (ok
&& STMT_VINFO_LIVE_P (stmt_info
))
1782 ok
= vectorizable_live_operation (phi
, NULL
, NULL
, -1, NULL
);
1786 if (dump_enabled_p ())
1788 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1789 "not vectorized: relevant phi not "
1791 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
, phi
, 0);
1797 for (gimple_stmt_iterator si
= gsi_start_bb (bb
); !gsi_end_p (si
);
1800 gimple
*stmt
= gsi_stmt (si
);
1801 if (!gimple_clobber_p (stmt
)
1802 && !vect_analyze_stmt (stmt
, &need_to_vectorize
, NULL
))
1807 /* All operations in the loop are either irrelevant (deal with loop
1808 control, or dead), or only used outside the loop and can be moved
1809 out of the loop (e.g. invariants, inductions). The loop can be
1810 optimized away by scalar optimizations. We're better off not
1811 touching this loop. */
1812 if (!need_to_vectorize
)
1814 if (dump_enabled_p ())
1815 dump_printf_loc (MSG_NOTE
, vect_location
,
1816 "All the computation can be taken out of the loop.\n");
1817 if (dump_enabled_p ())
1818 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1819 "not vectorized: redundant loop. no profit to "
1828 /* Function vect_analyze_loop_2.
1830 Apply a set of analyses on LOOP, and create a loop_vec_info struct
1831 for it. The different analyses will record information in the
1832 loop_vec_info struct. */
1834 vect_analyze_loop_2 (loop_vec_info loop_vinfo
, bool &fatal
)
1837 int max_vf
= MAX_VECTORIZATION_FACTOR
;
1839 unsigned int n_stmts
= 0;
1841 /* The first group of checks is independent of the vector size. */
1844 /* Find all data references in the loop (which correspond to vdefs/vuses)
1845 and analyze their evolution in the loop. */
1847 basic_block
*bbs
= LOOP_VINFO_BBS (loop_vinfo
);
1849 loop_p loop
= LOOP_VINFO_LOOP (loop_vinfo
);
1850 if (!find_loop_nest (loop
, &LOOP_VINFO_LOOP_NEST (loop_vinfo
)))
1852 if (dump_enabled_p ())
1853 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1854 "not vectorized: loop nest containing two "
1855 "or more consecutive inner loops cannot be "
1860 for (unsigned i
= 0; i
< loop
->num_nodes
; i
++)
1861 for (gimple_stmt_iterator gsi
= gsi_start_bb (bbs
[i
]);
1862 !gsi_end_p (gsi
); gsi_next (&gsi
))
1864 gimple
*stmt
= gsi_stmt (gsi
);
1865 if (is_gimple_debug (stmt
))
1868 if (!find_data_references_in_stmt (loop
, stmt
,
1869 &LOOP_VINFO_DATAREFS (loop_vinfo
)))
1871 if (is_gimple_call (stmt
) && loop
->safelen
)
1873 tree fndecl
= gimple_call_fndecl (stmt
), op
;
1874 if (fndecl
!= NULL_TREE
)
1876 cgraph_node
*node
= cgraph_node::get (fndecl
);
1877 if (node
!= NULL
&& node
->simd_clones
!= NULL
)
1879 unsigned int j
, n
= gimple_call_num_args (stmt
);
1880 for (j
= 0; j
< n
; j
++)
1882 op
= gimple_call_arg (stmt
, j
);
1884 || (REFERENCE_CLASS_P (op
)
1885 && get_base_address (op
)))
1888 op
= gimple_call_lhs (stmt
);
1889 /* Ignore #pragma omp declare simd functions
1890 if they don't have data references in the
1891 call stmt itself. */
1895 || (REFERENCE_CLASS_P (op
)
1896 && get_base_address (op
)))))
1901 if (dump_enabled_p ())
1902 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1903 "not vectorized: loop contains function "
1904 "calls or data references that cannot "
1910 /* Analyze the data references and also adjust the minimal
1911 vectorization factor according to the loads and stores. */
1913 ok
= vect_analyze_data_refs (loop_vinfo
, &min_vf
);
1916 if (dump_enabled_p ())
1917 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1918 "bad data references.\n");
1922 /* Classify all cross-iteration scalar data-flow cycles.
1923 Cross-iteration cycles caused by virtual phis are analyzed separately. */
1924 vect_analyze_scalar_cycles (loop_vinfo
);
1926 vect_pattern_recog (loop_vinfo
);
1928 vect_fixup_scalar_cycles_with_patterns (loop_vinfo
);
1930 /* Analyze the access patterns of the data-refs in the loop (consecutive,
1931 complex, etc.). FORNOW: Only handle consecutive access pattern. */
1933 ok
= vect_analyze_data_ref_accesses (loop_vinfo
);
1936 if (dump_enabled_p ())
1937 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1938 "bad data access.\n");
1942 /* Data-flow analysis to detect stmts that do not need to be vectorized. */
1944 ok
= vect_mark_stmts_to_be_vectorized (loop_vinfo
);
1947 if (dump_enabled_p ())
1948 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1949 "unexpected pattern.\n");
1953 /* While the rest of the analysis below depends on it in some way. */
1956 /* Analyze data dependences between the data-refs in the loop
1957 and adjust the maximum vectorization factor according to
1959 FORNOW: fail at the first data dependence that we encounter. */
1961 ok
= vect_analyze_data_ref_dependences (loop_vinfo
, &max_vf
);
1965 if (dump_enabled_p ())
1966 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1967 "bad data dependence.\n");
1971 ok
= vect_determine_vectorization_factor (loop_vinfo
);
1974 if (dump_enabled_p ())
1975 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1976 "can't determine vectorization factor.\n");
1979 if (max_vf
< LOOP_VINFO_VECT_FACTOR (loop_vinfo
))
1981 if (dump_enabled_p ())
1982 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1983 "bad data dependence.\n");
1987 /* Compute the scalar iteration cost. */
1988 vect_compute_single_scalar_iteration_cost (loop_vinfo
);
1990 int saved_vectorization_factor
= LOOP_VINFO_VECT_FACTOR (loop_vinfo
);
1991 HOST_WIDE_INT estimated_niter
;
1993 int min_scalar_loop_bound
;
1995 /* Check the SLP opportunities in the loop, analyze and build SLP trees. */
1996 ok
= vect_analyze_slp (loop_vinfo
, n_stmts
);
2000 /* If there are any SLP instances mark them as pure_slp. */
2001 bool slp
= vect_make_slp_decision (loop_vinfo
);
2004 /* Find stmts that need to be both vectorized and SLPed. */
2005 vect_detect_hybrid_slp (loop_vinfo
);
2007 /* Update the vectorization factor based on the SLP decision. */
2008 vect_update_vf_for_slp (loop_vinfo
);
2011 /* This is the point where we can re-start analysis with SLP forced off. */
2014 /* Now the vectorization factor is final. */
2015 unsigned vectorization_factor
= LOOP_VINFO_VECT_FACTOR (loop_vinfo
);
2016 gcc_assert (vectorization_factor
!= 0);
2018 if (LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo
) && dump_enabled_p ())
2019 dump_printf_loc (MSG_NOTE
, vect_location
,
2020 "vectorization_factor = %d, niters = "
2021 HOST_WIDE_INT_PRINT_DEC
"\n", vectorization_factor
,
2022 LOOP_VINFO_INT_NITERS (loop_vinfo
));
2024 HOST_WIDE_INT max_niter
2025 = likely_max_stmt_executions_int (LOOP_VINFO_LOOP (loop_vinfo
));
2026 if ((LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo
)
2027 && (LOOP_VINFO_INT_NITERS (loop_vinfo
) < vectorization_factor
))
2029 && (unsigned HOST_WIDE_INT
) max_niter
< vectorization_factor
))
2031 if (dump_enabled_p ())
2032 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2033 "not vectorized: iteration count smaller than "
2034 "vectorization factor.\n");
2038 /* Analyze the alignment of the data-refs in the loop.
2039 Fail if a data reference is found that cannot be vectorized. */
2041 ok
= vect_analyze_data_refs_alignment (loop_vinfo
);
2044 if (dump_enabled_p ())
2045 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2046 "bad data alignment.\n");
2050 /* Prune the list of ddrs to be tested at run-time by versioning for alias.
2051 It is important to call pruning after vect_analyze_data_ref_accesses,
2052 since we use grouping information gathered by interleaving analysis. */
2053 ok
= vect_prune_runtime_alias_test_list (loop_vinfo
);
2057 /* Do not invoke vect_enhance_data_refs_alignment for eplilogue
2059 if (!LOOP_VINFO_EPILOGUE_P (loop_vinfo
))
2061 /* This pass will decide on using loop versioning and/or loop peeling in
2062 order to enhance the alignment of data references in the loop. */
2063 ok
= vect_enhance_data_refs_alignment (loop_vinfo
);
2066 if (dump_enabled_p ())
2067 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2068 "bad data alignment.\n");
2075 /* Analyze operations in the SLP instances. Note this may
2076 remove unsupported SLP instances which makes the above
2077 SLP kind detection invalid. */
2078 unsigned old_size
= LOOP_VINFO_SLP_INSTANCES (loop_vinfo
).length ();
2079 vect_slp_analyze_operations (LOOP_VINFO_SLP_INSTANCES (loop_vinfo
),
2080 LOOP_VINFO_TARGET_COST_DATA (loop_vinfo
));
2081 if (LOOP_VINFO_SLP_INSTANCES (loop_vinfo
).length () != old_size
)
2085 /* Scan all the remaining operations in the loop that are not subject
2086 to SLP and make sure they are vectorizable. */
2087 ok
= vect_analyze_loop_operations (loop_vinfo
);
2090 if (dump_enabled_p ())
2091 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2092 "bad operation or unsupported loop bound.\n");
2096 /* If epilog loop is required because of data accesses with gaps,
2097 one additional iteration needs to be peeled. Check if there is
2098 enough iterations for vectorization. */
2099 if (LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo
)
2100 && LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo
))
2102 int vf
= LOOP_VINFO_VECT_FACTOR (loop_vinfo
);
2103 tree scalar_niters
= LOOP_VINFO_NITERSM1 (loop_vinfo
);
2105 if (wi::to_widest (scalar_niters
) < vf
)
2107 if (dump_enabled_p ())
2108 dump_printf_loc (MSG_NOTE
, vect_location
,
2109 "loop has no enough iterations to support"
2110 " peeling for gaps.\n");
2115 /* Analyze cost. Decide if worth while to vectorize. */
2116 int min_profitable_estimate
, min_profitable_iters
;
2117 vect_estimate_min_profitable_iters (loop_vinfo
, &min_profitable_iters
,
2118 &min_profitable_estimate
);
2120 if (min_profitable_iters
< 0)
2122 if (dump_enabled_p ())
2123 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2124 "not vectorized: vectorization not profitable.\n");
2125 if (dump_enabled_p ())
2126 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2127 "not vectorized: vector version will never be "
2132 min_scalar_loop_bound
= ((PARAM_VALUE (PARAM_MIN_VECT_LOOP_BOUND
)
2133 * vectorization_factor
) - 1);
2135 /* Use the cost model only if it is more conservative than user specified
2137 th
= (unsigned) min_scalar_loop_bound
;
2138 if (min_profitable_iters
2139 && (!min_scalar_loop_bound
2140 || min_profitable_iters
> min_scalar_loop_bound
))
2141 th
= (unsigned) min_profitable_iters
;
2143 LOOP_VINFO_COST_MODEL_THRESHOLD (loop_vinfo
) = th
;
2145 if (LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo
)
2146 && LOOP_VINFO_INT_NITERS (loop_vinfo
) <= th
)
2148 if (dump_enabled_p ())
2149 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2150 "not vectorized: vectorization not profitable.\n");
2151 if (dump_enabled_p ())
2152 dump_printf_loc (MSG_NOTE
, vect_location
,
2153 "not vectorized: iteration count smaller than user "
2154 "specified loop bound parameter or minimum profitable "
2155 "iterations (whichever is more conservative).\n");
2160 = estimated_stmt_executions_int (LOOP_VINFO_LOOP (loop_vinfo
));
2161 if (estimated_niter
== -1)
2162 estimated_niter
= max_niter
;
2163 if (estimated_niter
!= -1
2164 && ((unsigned HOST_WIDE_INT
) estimated_niter
2165 <= MAX (th
, (unsigned)min_profitable_estimate
)))
2167 if (dump_enabled_p ())
2168 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2169 "not vectorized: estimated iteration count too "
2171 if (dump_enabled_p ())
2172 dump_printf_loc (MSG_NOTE
, vect_location
,
2173 "not vectorized: estimated iteration count smaller "
2174 "than specified loop bound parameter or minimum "
2175 "profitable iterations (whichever is more "
2176 "conservative).\n");
2180 /* Decide whether we need to create an epilogue loop to handle
2181 remaining scalar iterations. */
2182 th
= ((LOOP_VINFO_COST_MODEL_THRESHOLD (loop_vinfo
) + 1)
2183 / LOOP_VINFO_VECT_FACTOR (loop_vinfo
))
2184 * LOOP_VINFO_VECT_FACTOR (loop_vinfo
);
2186 if (LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo
)
2187 && LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo
) > 0)
2189 if (ctz_hwi (LOOP_VINFO_INT_NITERS (loop_vinfo
)
2190 - LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo
))
2191 < exact_log2 (LOOP_VINFO_VECT_FACTOR (loop_vinfo
)))
2192 LOOP_VINFO_PEELING_FOR_NITER (loop_vinfo
) = true;
2194 else if (LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo
)
2195 || (tree_ctz (LOOP_VINFO_NITERS (loop_vinfo
))
2196 < (unsigned)exact_log2 (LOOP_VINFO_VECT_FACTOR (loop_vinfo
))
2197 /* In case of versioning, check if the maximum number of
2198 iterations is greater than th. If they are identical,
2199 the epilogue is unnecessary. */
2200 && (!LOOP_REQUIRES_VERSIONING (loop_vinfo
)
2201 || (unsigned HOST_WIDE_INT
) max_niter
> th
)))
2202 LOOP_VINFO_PEELING_FOR_NITER (loop_vinfo
) = true;
2204 /* If an epilogue loop is required make sure we can create one. */
2205 if (LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo
)
2206 || LOOP_VINFO_PEELING_FOR_NITER (loop_vinfo
))
2208 if (dump_enabled_p ())
2209 dump_printf_loc (MSG_NOTE
, vect_location
, "epilog loop required\n");
2210 if (!vect_can_advance_ivs_p (loop_vinfo
)
2211 || !slpeel_can_duplicate_loop_p (LOOP_VINFO_LOOP (loop_vinfo
),
2212 single_exit (LOOP_VINFO_LOOP
2215 if (dump_enabled_p ())
2216 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2217 "not vectorized: can't create required "
2223 gcc_assert (vectorization_factor
2224 == (unsigned)LOOP_VINFO_VECT_FACTOR (loop_vinfo
));
2226 /* Ok to vectorize! */
2230 /* Try again with SLP forced off but if we didn't do any SLP there is
2231 no point in re-trying. */
2235 /* If there are reduction chains re-trying will fail anyway. */
2236 if (! LOOP_VINFO_REDUCTION_CHAINS (loop_vinfo
).is_empty ())
2239 /* Likewise if the grouped loads or stores in the SLP cannot be handled
2240 via interleaving or lane instructions. */
2241 slp_instance instance
;
2244 FOR_EACH_VEC_ELT (LOOP_VINFO_SLP_INSTANCES (loop_vinfo
), i
, instance
)
2246 stmt_vec_info vinfo
;
2247 vinfo
= vinfo_for_stmt
2248 (SLP_TREE_SCALAR_STMTS (SLP_INSTANCE_TREE (instance
))[0]);
2249 if (! STMT_VINFO_GROUPED_ACCESS (vinfo
))
2251 vinfo
= vinfo_for_stmt (STMT_VINFO_GROUP_FIRST_ELEMENT (vinfo
));
2252 unsigned int size
= STMT_VINFO_GROUP_SIZE (vinfo
);
2253 tree vectype
= STMT_VINFO_VECTYPE (vinfo
);
2254 if (! vect_store_lanes_supported (vectype
, size
)
2255 && ! vect_grouped_store_supported (vectype
, size
))
2257 FOR_EACH_VEC_ELT (SLP_INSTANCE_LOADS (instance
), j
, node
)
2259 vinfo
= vinfo_for_stmt (SLP_TREE_SCALAR_STMTS (node
)[0]);
2260 vinfo
= vinfo_for_stmt (STMT_VINFO_GROUP_FIRST_ELEMENT (vinfo
));
2261 bool single_element_p
= !STMT_VINFO_GROUP_NEXT_ELEMENT (vinfo
);
2262 size
= STMT_VINFO_GROUP_SIZE (vinfo
);
2263 vectype
= STMT_VINFO_VECTYPE (vinfo
);
2264 if (! vect_load_lanes_supported (vectype
, size
)
2265 && ! vect_grouped_load_supported (vectype
, single_element_p
,
2271 if (dump_enabled_p ())
2272 dump_printf_loc (MSG_NOTE
, vect_location
,
2273 "re-trying with SLP disabled\n");
2275 /* Roll back state appropriately. No SLP this time. */
2277 /* Restore vectorization factor as it were without SLP. */
2278 LOOP_VINFO_VECT_FACTOR (loop_vinfo
) = saved_vectorization_factor
;
2279 /* Free the SLP instances. */
2280 FOR_EACH_VEC_ELT (LOOP_VINFO_SLP_INSTANCES (loop_vinfo
), j
, instance
)
2281 vect_free_slp_instance (instance
);
2282 LOOP_VINFO_SLP_INSTANCES (loop_vinfo
).release ();
2283 /* Reset SLP type to loop_vect on all stmts. */
2284 for (i
= 0; i
< LOOP_VINFO_LOOP (loop_vinfo
)->num_nodes
; ++i
)
2286 basic_block bb
= LOOP_VINFO_BBS (loop_vinfo
)[i
];
2287 for (gimple_stmt_iterator si
= gsi_start_phis (bb
);
2288 !gsi_end_p (si
); gsi_next (&si
))
2290 stmt_vec_info stmt_info
= vinfo_for_stmt (gsi_stmt (si
));
2291 STMT_SLP_TYPE (stmt_info
) = loop_vect
;
2293 for (gimple_stmt_iterator si
= gsi_start_bb (bb
);
2294 !gsi_end_p (si
); gsi_next (&si
))
2296 stmt_vec_info stmt_info
= vinfo_for_stmt (gsi_stmt (si
));
2297 STMT_SLP_TYPE (stmt_info
) = loop_vect
;
2298 if (STMT_VINFO_IN_PATTERN_P (stmt_info
))
2300 stmt_info
= vinfo_for_stmt (STMT_VINFO_RELATED_STMT (stmt_info
));
2301 STMT_SLP_TYPE (stmt_info
) = loop_vect
;
2302 for (gimple_stmt_iterator pi
2303 = gsi_start (STMT_VINFO_PATTERN_DEF_SEQ (stmt_info
));
2304 !gsi_end_p (pi
); gsi_next (&pi
))
2306 gimple
*pstmt
= gsi_stmt (pi
);
2307 STMT_SLP_TYPE (vinfo_for_stmt (pstmt
)) = loop_vect
;
2312 /* Free optimized alias test DDRS. */
2313 LOOP_VINFO_COMP_ALIAS_DDRS (loop_vinfo
).release ();
2314 /* Reset target cost data. */
2315 destroy_cost_data (LOOP_VINFO_TARGET_COST_DATA (loop_vinfo
));
2316 LOOP_VINFO_TARGET_COST_DATA (loop_vinfo
)
2317 = init_cost (LOOP_VINFO_LOOP (loop_vinfo
));
2318 /* Reset assorted flags. */
2319 LOOP_VINFO_PEELING_FOR_NITER (loop_vinfo
) = false;
2320 LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo
) = false;
2321 LOOP_VINFO_COST_MODEL_THRESHOLD (loop_vinfo
) = 0;
2326 /* Function vect_analyze_loop.
2328 Apply a set of analyses on LOOP, and create a loop_vec_info struct
2329 for it. The different analyses will record information in the
2330 loop_vec_info struct. If ORIG_LOOP_VINFO is not NULL epilogue must
2333 vect_analyze_loop (struct loop
*loop
, loop_vec_info orig_loop_vinfo
)
2335 loop_vec_info loop_vinfo
;
2336 unsigned int vector_sizes
;
2338 /* Autodetect first vector size we try. */
2339 current_vector_size
= 0;
2340 vector_sizes
= targetm
.vectorize
.autovectorize_vector_sizes ();
2342 if (dump_enabled_p ())
2343 dump_printf_loc (MSG_NOTE
, vect_location
,
2344 "===== analyze_loop_nest =====\n");
2346 if (loop_outer (loop
)
2347 && loop_vec_info_for_loop (loop_outer (loop
))
2348 && LOOP_VINFO_VECTORIZABLE_P (loop_vec_info_for_loop (loop_outer (loop
))))
2350 if (dump_enabled_p ())
2351 dump_printf_loc (MSG_NOTE
, vect_location
,
2352 "outer-loop already vectorized.\n");
2358 /* Check the CFG characteristics of the loop (nesting, entry/exit). */
2359 loop_vinfo
= vect_analyze_loop_form (loop
);
2362 if (dump_enabled_p ())
2363 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2364 "bad loop form.\n");
2370 if (orig_loop_vinfo
)
2371 LOOP_VINFO_ORIG_LOOP_INFO (loop_vinfo
) = orig_loop_vinfo
;
2373 if (vect_analyze_loop_2 (loop_vinfo
, fatal
))
2375 LOOP_VINFO_VECTORIZABLE_P (loop_vinfo
) = 1;
2380 destroy_loop_vec_info (loop_vinfo
, true);
2382 vector_sizes
&= ~current_vector_size
;
2384 || vector_sizes
== 0
2385 || current_vector_size
== 0)
2388 /* Try the next biggest vector size. */
2389 current_vector_size
= 1 << floor_log2 (vector_sizes
);
2390 if (dump_enabled_p ())
2391 dump_printf_loc (MSG_NOTE
, vect_location
,
2392 "***** Re-trying analysis with "
2393 "vector size %d\n", current_vector_size
);
2398 /* Function reduction_code_for_scalar_code
2401 CODE - tree_code of a reduction operations.
2404 REDUC_CODE - the corresponding tree-code to be used to reduce the
2405 vector of partial results into a single scalar result, or ERROR_MARK
2406 if the operation is a supported reduction operation, but does not have
2409 Return FALSE if CODE currently cannot be vectorized as reduction. */
2412 reduction_code_for_scalar_code (enum tree_code code
,
2413 enum tree_code
*reduc_code
)
2418 *reduc_code
= REDUC_MAX_EXPR
;
2422 *reduc_code
= REDUC_MIN_EXPR
;
2426 *reduc_code
= REDUC_PLUS_EXPR
;
2434 *reduc_code
= ERROR_MARK
;
2443 /* Error reporting helper for vect_is_simple_reduction below. GIMPLE statement
2444 STMT is printed with a message MSG. */
2447 report_vect_op (dump_flags_t msg_type
, gimple
*stmt
, const char *msg
)
2449 dump_printf_loc (msg_type
, vect_location
, "%s", msg
);
2450 dump_gimple_stmt (msg_type
, TDF_SLIM
, stmt
, 0);
2454 /* Detect SLP reduction of the form:
2464 PHI is the reduction phi node (#a1 = phi <a5, a0> above)
2465 FIRST_STMT is the first reduction stmt in the chain
2466 (a2 = operation (a1)).
2468 Return TRUE if a reduction chain was detected. */
2471 vect_is_slp_reduction (loop_vec_info loop_info
, gimple
*phi
,
2474 struct loop
*loop
= (gimple_bb (phi
))->loop_father
;
2475 struct loop
*vect_loop
= LOOP_VINFO_LOOP (loop_info
);
2476 enum tree_code code
;
2477 gimple
*current_stmt
= NULL
, *loop_use_stmt
= NULL
, *first
, *next_stmt
;
2478 stmt_vec_info use_stmt_info
, current_stmt_info
;
2480 imm_use_iterator imm_iter
;
2481 use_operand_p use_p
;
2482 int nloop_uses
, size
= 0, n_out_of_loop_uses
;
2485 if (loop
!= vect_loop
)
2488 lhs
= PHI_RESULT (phi
);
2489 code
= gimple_assign_rhs_code (first_stmt
);
2493 n_out_of_loop_uses
= 0;
2494 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, lhs
)
2496 gimple
*use_stmt
= USE_STMT (use_p
);
2497 if (is_gimple_debug (use_stmt
))
2500 /* Check if we got back to the reduction phi. */
2501 if (use_stmt
== phi
)
2503 loop_use_stmt
= use_stmt
;
2508 if (flow_bb_inside_loop_p (loop
, gimple_bb (use_stmt
)))
2510 loop_use_stmt
= use_stmt
;
2514 n_out_of_loop_uses
++;
2516 /* There are can be either a single use in the loop or two uses in
2518 if (nloop_uses
> 1 || (n_out_of_loop_uses
&& nloop_uses
))
2525 /* We reached a statement with no loop uses. */
2526 if (nloop_uses
== 0)
2529 /* This is a loop exit phi, and we haven't reached the reduction phi. */
2530 if (gimple_code (loop_use_stmt
) == GIMPLE_PHI
)
2533 if (!is_gimple_assign (loop_use_stmt
)
2534 || code
!= gimple_assign_rhs_code (loop_use_stmt
)
2535 || !flow_bb_inside_loop_p (loop
, gimple_bb (loop_use_stmt
)))
2538 /* Insert USE_STMT into reduction chain. */
2539 use_stmt_info
= vinfo_for_stmt (loop_use_stmt
);
2542 current_stmt_info
= vinfo_for_stmt (current_stmt
);
2543 GROUP_NEXT_ELEMENT (current_stmt_info
) = loop_use_stmt
;
2544 GROUP_FIRST_ELEMENT (use_stmt_info
)
2545 = GROUP_FIRST_ELEMENT (current_stmt_info
);
2548 GROUP_FIRST_ELEMENT (use_stmt_info
) = loop_use_stmt
;
2550 lhs
= gimple_assign_lhs (loop_use_stmt
);
2551 current_stmt
= loop_use_stmt
;
2555 if (!found
|| loop_use_stmt
!= phi
|| size
< 2)
2558 /* Swap the operands, if needed, to make the reduction operand be the second
2560 lhs
= PHI_RESULT (phi
);
2561 next_stmt
= GROUP_FIRST_ELEMENT (vinfo_for_stmt (current_stmt
));
2564 if (gimple_assign_rhs2 (next_stmt
) == lhs
)
2566 tree op
= gimple_assign_rhs1 (next_stmt
);
2567 gimple
*def_stmt
= NULL
;
2569 if (TREE_CODE (op
) == SSA_NAME
)
2570 def_stmt
= SSA_NAME_DEF_STMT (op
);
2572 /* Check that the other def is either defined in the loop
2573 ("vect_internal_def"), or it's an induction (defined by a
2574 loop-header phi-node). */
2576 && gimple_bb (def_stmt
)
2577 && flow_bb_inside_loop_p (loop
, gimple_bb (def_stmt
))
2578 && (is_gimple_assign (def_stmt
)
2579 || is_gimple_call (def_stmt
)
2580 || STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def_stmt
))
2581 == vect_induction_def
2582 || (gimple_code (def_stmt
) == GIMPLE_PHI
2583 && STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def_stmt
))
2584 == vect_internal_def
2585 && !is_loop_header_bb_p (gimple_bb (def_stmt
)))))
2587 lhs
= gimple_assign_lhs (next_stmt
);
2588 next_stmt
= GROUP_NEXT_ELEMENT (vinfo_for_stmt (next_stmt
));
2596 tree op
= gimple_assign_rhs2 (next_stmt
);
2597 gimple
*def_stmt
= NULL
;
2599 if (TREE_CODE (op
) == SSA_NAME
)
2600 def_stmt
= SSA_NAME_DEF_STMT (op
);
2602 /* Check that the other def is either defined in the loop
2603 ("vect_internal_def"), or it's an induction (defined by a
2604 loop-header phi-node). */
2606 && gimple_bb (def_stmt
)
2607 && flow_bb_inside_loop_p (loop
, gimple_bb (def_stmt
))
2608 && (is_gimple_assign (def_stmt
)
2609 || is_gimple_call (def_stmt
)
2610 || STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def_stmt
))
2611 == vect_induction_def
2612 || (gimple_code (def_stmt
) == GIMPLE_PHI
2613 && STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def_stmt
))
2614 == vect_internal_def
2615 && !is_loop_header_bb_p (gimple_bb (def_stmt
)))))
2617 if (dump_enabled_p ())
2619 dump_printf_loc (MSG_NOTE
, vect_location
, "swapping oprnds: ");
2620 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
, next_stmt
, 0);
2623 swap_ssa_operands (next_stmt
,
2624 gimple_assign_rhs1_ptr (next_stmt
),
2625 gimple_assign_rhs2_ptr (next_stmt
));
2626 update_stmt (next_stmt
);
2628 if (CONSTANT_CLASS_P (gimple_assign_rhs1 (next_stmt
)))
2629 LOOP_VINFO_OPERANDS_SWAPPED (loop_info
) = true;
2635 lhs
= gimple_assign_lhs (next_stmt
);
2636 next_stmt
= GROUP_NEXT_ELEMENT (vinfo_for_stmt (next_stmt
));
2639 /* Save the chain for further analysis in SLP detection. */
2640 first
= GROUP_FIRST_ELEMENT (vinfo_for_stmt (current_stmt
));
2641 LOOP_VINFO_REDUCTION_CHAINS (loop_info
).safe_push (first
);
2642 GROUP_SIZE (vinfo_for_stmt (first
)) = size
;
2648 /* Function vect_is_simple_reduction
2650 (1) Detect a cross-iteration def-use cycle that represents a simple
2651 reduction computation. We look for the following pattern:
2656 a2 = operation (a3, a1)
2663 a2 = operation (a3, a1)
2666 1. operation is commutative and associative and it is safe to
2667 change the order of the computation
2668 2. no uses for a2 in the loop (a2 is used out of the loop)
2669 3. no uses of a1 in the loop besides the reduction operation
2670 4. no uses of a1 outside the loop.
2672 Conditions 1,4 are tested here.
2673 Conditions 2,3 are tested in vect_mark_stmts_to_be_vectorized.
2675 (2) Detect a cross-iteration def-use cycle in nested loops, i.e.,
2678 (3) Detect cycles of phi nodes in outer-loop vectorization, i.e., double
2682 inner loop (def of a3)
2685 (4) Detect condition expressions, ie:
2686 for (int i = 0; i < N; i++)
2693 vect_is_simple_reduction (loop_vec_info loop_info
, gimple
*phi
,
2695 bool need_wrapping_integral_overflow
,
2696 enum vect_reduction_type
*v_reduc_type
)
2698 struct loop
*loop
= (gimple_bb (phi
))->loop_father
;
2699 struct loop
*vect_loop
= LOOP_VINFO_LOOP (loop_info
);
2700 edge latch_e
= loop_latch_edge (loop
);
2701 tree loop_arg
= PHI_ARG_DEF_FROM_EDGE (phi
, latch_e
);
2702 gimple
*def_stmt
, *def1
= NULL
, *def2
= NULL
, *phi_use_stmt
= NULL
;
2703 enum tree_code orig_code
, code
;
2704 tree op1
, op2
, op3
= NULL_TREE
, op4
= NULL_TREE
;
2708 imm_use_iterator imm_iter
;
2709 use_operand_p use_p
;
2712 *double_reduc
= false;
2713 *v_reduc_type
= TREE_CODE_REDUCTION
;
2715 /* Check validity of the reduction only for the innermost loop. */
2716 bool check_reduction
= ! flow_loop_nested_p (vect_loop
, loop
);
2717 gcc_assert ((check_reduction
&& loop
== vect_loop
)
2718 || (!check_reduction
&& flow_loop_nested_p (vect_loop
, loop
)));
2720 name
= PHI_RESULT (phi
);
2721 /* ??? If there are no uses of the PHI result the inner loop reduction
2722 won't be detected as possibly double-reduction by vectorizable_reduction
2723 because that tries to walk the PHI arg from the preheader edge which
2724 can be constant. See PR60382. */
2725 if (has_zero_uses (name
))
2728 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, name
)
2730 gimple
*use_stmt
= USE_STMT (use_p
);
2731 if (is_gimple_debug (use_stmt
))
2734 if (!flow_bb_inside_loop_p (loop
, gimple_bb (use_stmt
)))
2736 if (dump_enabled_p ())
2737 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2738 "intermediate value used outside loop.\n");
2746 if (dump_enabled_p ())
2747 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2748 "reduction used in loop.\n");
2752 phi_use_stmt
= use_stmt
;
2755 if (TREE_CODE (loop_arg
) != SSA_NAME
)
2757 if (dump_enabled_p ())
2759 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2760 "reduction: not ssa_name: ");
2761 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
, loop_arg
);
2762 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
2767 def_stmt
= SSA_NAME_DEF_STMT (loop_arg
);
2770 if (dump_enabled_p ())
2771 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2772 "reduction: no def_stmt.\n");
2776 if (!is_gimple_assign (def_stmt
) && gimple_code (def_stmt
) != GIMPLE_PHI
)
2778 if (dump_enabled_p ())
2779 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
, def_stmt
, 0);
2783 if (is_gimple_assign (def_stmt
))
2785 name
= gimple_assign_lhs (def_stmt
);
2790 name
= PHI_RESULT (def_stmt
);
2795 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, name
)
2797 gimple
*use_stmt
= USE_STMT (use_p
);
2798 if (is_gimple_debug (use_stmt
))
2800 if (flow_bb_inside_loop_p (loop
, gimple_bb (use_stmt
)))
2804 if (dump_enabled_p ())
2805 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2806 "reduction used in loop.\n");
2811 /* If DEF_STMT is a phi node itself, we expect it to have a single argument
2812 defined in the inner loop. */
2815 op1
= PHI_ARG_DEF (def_stmt
, 0);
2817 if (gimple_phi_num_args (def_stmt
) != 1
2818 || TREE_CODE (op1
) != SSA_NAME
)
2820 if (dump_enabled_p ())
2821 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2822 "unsupported phi node definition.\n");
2827 def1
= SSA_NAME_DEF_STMT (op1
);
2828 if (gimple_bb (def1
)
2829 && flow_bb_inside_loop_p (loop
, gimple_bb (def_stmt
))
2831 && flow_bb_inside_loop_p (loop
->inner
, gimple_bb (def1
))
2832 && is_gimple_assign (def1
)
2833 && flow_bb_inside_loop_p (loop
->inner
, gimple_bb (phi_use_stmt
)))
2835 if (dump_enabled_p ())
2836 report_vect_op (MSG_NOTE
, def_stmt
,
2837 "detected double reduction: ");
2839 *double_reduc
= true;
2846 code
= orig_code
= gimple_assign_rhs_code (def_stmt
);
2848 /* We can handle "res -= x[i]", which is non-associative by
2849 simply rewriting this into "res += -x[i]". Avoid changing
2850 gimple instruction for the first simple tests and only do this
2851 if we're allowed to change code at all. */
2852 if (code
== MINUS_EXPR
2853 && (op1
= gimple_assign_rhs1 (def_stmt
))
2854 && TREE_CODE (op1
) == SSA_NAME
2855 && SSA_NAME_DEF_STMT (op1
) == phi
)
2858 if (code
== COND_EXPR
)
2860 if (check_reduction
)
2861 *v_reduc_type
= COND_REDUCTION
;
2863 else if (!commutative_tree_code (code
) || !associative_tree_code (code
))
2865 if (dump_enabled_p ())
2866 report_vect_op (MSG_MISSED_OPTIMIZATION
, def_stmt
,
2867 "reduction: not commutative/associative: ");
2871 if (get_gimple_rhs_class (code
) != GIMPLE_BINARY_RHS
)
2873 if (code
!= COND_EXPR
)
2875 if (dump_enabled_p ())
2876 report_vect_op (MSG_MISSED_OPTIMIZATION
, def_stmt
,
2877 "reduction: not binary operation: ");
2882 op3
= gimple_assign_rhs1 (def_stmt
);
2883 if (COMPARISON_CLASS_P (op3
))
2885 op4
= TREE_OPERAND (op3
, 1);
2886 op3
= TREE_OPERAND (op3
, 0);
2889 op1
= gimple_assign_rhs2 (def_stmt
);
2890 op2
= gimple_assign_rhs3 (def_stmt
);
2892 if (TREE_CODE (op1
) != SSA_NAME
&& TREE_CODE (op2
) != SSA_NAME
)
2894 if (dump_enabled_p ())
2895 report_vect_op (MSG_MISSED_OPTIMIZATION
, def_stmt
,
2896 "reduction: uses not ssa_names: ");
2903 op1
= gimple_assign_rhs1 (def_stmt
);
2904 op2
= gimple_assign_rhs2 (def_stmt
);
2906 if (TREE_CODE (op1
) != SSA_NAME
&& TREE_CODE (op2
) != SSA_NAME
)
2908 if (dump_enabled_p ())
2909 report_vect_op (MSG_MISSED_OPTIMIZATION
, def_stmt
,
2910 "reduction: uses not ssa_names: ");
2916 type
= TREE_TYPE (gimple_assign_lhs (def_stmt
));
2917 if ((TREE_CODE (op1
) == SSA_NAME
2918 && !types_compatible_p (type
,TREE_TYPE (op1
)))
2919 || (TREE_CODE (op2
) == SSA_NAME
2920 && !types_compatible_p (type
, TREE_TYPE (op2
)))
2921 || (op3
&& TREE_CODE (op3
) == SSA_NAME
2922 && !types_compatible_p (type
, TREE_TYPE (op3
)))
2923 || (op4
&& TREE_CODE (op4
) == SSA_NAME
2924 && !types_compatible_p (type
, TREE_TYPE (op4
))))
2926 if (dump_enabled_p ())
2928 dump_printf_loc (MSG_NOTE
, vect_location
,
2929 "reduction: multiple types: operation type: ");
2930 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, type
);
2931 dump_printf (MSG_NOTE
, ", operands types: ");
2932 dump_generic_expr (MSG_NOTE
, TDF_SLIM
,
2934 dump_printf (MSG_NOTE
, ",");
2935 dump_generic_expr (MSG_NOTE
, TDF_SLIM
,
2939 dump_printf (MSG_NOTE
, ",");
2940 dump_generic_expr (MSG_NOTE
, TDF_SLIM
,
2946 dump_printf (MSG_NOTE
, ",");
2947 dump_generic_expr (MSG_NOTE
, TDF_SLIM
,
2950 dump_printf (MSG_NOTE
, "\n");
2956 /* Check that it's ok to change the order of the computation.
2957 Generally, when vectorizing a reduction we change the order of the
2958 computation. This may change the behavior of the program in some
2959 cases, so we need to check that this is ok. One exception is when
2960 vectorizing an outer-loop: the inner-loop is executed sequentially,
2961 and therefore vectorizing reductions in the inner-loop during
2962 outer-loop vectorization is safe. */
2964 if (*v_reduc_type
!= COND_REDUCTION
2967 /* CHECKME: check for !flag_finite_math_only too? */
2968 if (SCALAR_FLOAT_TYPE_P (type
) && !flag_associative_math
)
2970 /* Changing the order of operations changes the semantics. */
2971 if (dump_enabled_p ())
2972 report_vect_op (MSG_MISSED_OPTIMIZATION
, def_stmt
,
2973 "reduction: unsafe fp math optimization: ");
2976 else if (INTEGRAL_TYPE_P (type
))
2978 if (!operation_no_trapping_overflow (type
, code
))
2980 /* Changing the order of operations changes the semantics. */
2981 if (dump_enabled_p ())
2982 report_vect_op (MSG_MISSED_OPTIMIZATION
, def_stmt
,
2983 "reduction: unsafe int math optimization"
2984 " (overflow traps): ");
2987 if (need_wrapping_integral_overflow
2988 && !TYPE_OVERFLOW_WRAPS (type
)
2989 && operation_can_overflow (code
))
2991 /* Changing the order of operations changes the semantics. */
2992 if (dump_enabled_p ())
2993 report_vect_op (MSG_MISSED_OPTIMIZATION
, def_stmt
,
2994 "reduction: unsafe int math optimization"
2995 " (overflow doesn't wrap): ");
2999 else if (SAT_FIXED_POINT_TYPE_P (type
))
3001 /* Changing the order of operations changes the semantics. */
3002 if (dump_enabled_p ())
3003 report_vect_op (MSG_MISSED_OPTIMIZATION
, def_stmt
,
3004 "reduction: unsafe fixed-point math optimization: ");
3009 /* Reduction is safe. We're dealing with one of the following:
3010 1) integer arithmetic and no trapv
3011 2) floating point arithmetic, and special flags permit this optimization
3012 3) nested cycle (i.e., outer loop vectorization). */
3013 if (TREE_CODE (op1
) == SSA_NAME
)
3014 def1
= SSA_NAME_DEF_STMT (op1
);
3016 if (TREE_CODE (op2
) == SSA_NAME
)
3017 def2
= SSA_NAME_DEF_STMT (op2
);
3019 if (code
!= COND_EXPR
3020 && ((!def1
|| gimple_nop_p (def1
)) && (!def2
|| gimple_nop_p (def2
))))
3022 if (dump_enabled_p ())
3023 report_vect_op (MSG_NOTE
, def_stmt
, "reduction: no defs for operands: ");
3027 /* Check that one def is the reduction def, defined by PHI,
3028 the other def is either defined in the loop ("vect_internal_def"),
3029 or it's an induction (defined by a loop-header phi-node). */
3031 if (def2
&& def2
== phi
3032 && (code
== COND_EXPR
3033 || !def1
|| gimple_nop_p (def1
)
3034 || !flow_bb_inside_loop_p (loop
, gimple_bb (def1
))
3035 || (def1
&& flow_bb_inside_loop_p (loop
, gimple_bb (def1
))
3036 && (is_gimple_assign (def1
)
3037 || is_gimple_call (def1
)
3038 || STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def1
))
3039 == vect_induction_def
3040 || (gimple_code (def1
) == GIMPLE_PHI
3041 && STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def1
))
3042 == vect_internal_def
3043 && !is_loop_header_bb_p (gimple_bb (def1
)))))))
3045 if (dump_enabled_p ())
3046 report_vect_op (MSG_NOTE
, def_stmt
, "detected reduction: ");
3050 if (def1
&& def1
== phi
3051 && (code
== COND_EXPR
3052 || !def2
|| gimple_nop_p (def2
)
3053 || !flow_bb_inside_loop_p (loop
, gimple_bb (def2
))
3054 || (def2
&& flow_bb_inside_loop_p (loop
, gimple_bb (def2
))
3055 && (is_gimple_assign (def2
)
3056 || is_gimple_call (def2
)
3057 || STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def2
))
3058 == vect_induction_def
3059 || (gimple_code (def2
) == GIMPLE_PHI
3060 && STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def2
))
3061 == vect_internal_def
3062 && !is_loop_header_bb_p (gimple_bb (def2
)))))))
3064 if (check_reduction
&& orig_code
!= MINUS_EXPR
)
3066 /* Check if we can swap operands (just for simplicity - so that
3067 the rest of the code can assume that the reduction variable
3068 is always the last (second) argument). */
3069 if (code
== COND_EXPR
)
3071 /* Swap cond_expr by inverting the condition. */
3072 tree cond_expr
= gimple_assign_rhs1 (def_stmt
);
3073 enum tree_code invert_code
= ERROR_MARK
;
3074 enum tree_code cond_code
= TREE_CODE (cond_expr
);
3076 if (TREE_CODE_CLASS (cond_code
) == tcc_comparison
)
3078 bool honor_nans
= HONOR_NANS (TREE_OPERAND (cond_expr
, 0));
3079 invert_code
= invert_tree_comparison (cond_code
, honor_nans
);
3081 if (invert_code
!= ERROR_MARK
)
3083 TREE_SET_CODE (cond_expr
, invert_code
);
3084 swap_ssa_operands (def_stmt
,
3085 gimple_assign_rhs2_ptr (def_stmt
),
3086 gimple_assign_rhs3_ptr (def_stmt
));
3090 if (dump_enabled_p ())
3091 report_vect_op (MSG_NOTE
, def_stmt
,
3092 "detected reduction: cannot swap operands "
3098 swap_ssa_operands (def_stmt
, gimple_assign_rhs1_ptr (def_stmt
),
3099 gimple_assign_rhs2_ptr (def_stmt
));
3101 if (dump_enabled_p ())
3102 report_vect_op (MSG_NOTE
, def_stmt
,
3103 "detected reduction: need to swap operands: ");
3105 if (CONSTANT_CLASS_P (gimple_assign_rhs1 (def_stmt
)))
3106 LOOP_VINFO_OPERANDS_SWAPPED (loop_info
) = true;
3110 if (dump_enabled_p ())
3111 report_vect_op (MSG_NOTE
, def_stmt
, "detected reduction: ");
3117 /* Try to find SLP reduction chain. */
3118 if (check_reduction
&& code
!= COND_EXPR
3119 && vect_is_slp_reduction (loop_info
, phi
, def_stmt
))
3121 if (dump_enabled_p ())
3122 report_vect_op (MSG_NOTE
, def_stmt
,
3123 "reduction: detected reduction chain: ");
3128 if (dump_enabled_p ())
3129 report_vect_op (MSG_MISSED_OPTIMIZATION
, def_stmt
,
3130 "reduction: unknown pattern: ");
3135 /* Wrapper around vect_is_simple_reduction, which will modify code
3136 in-place if it enables detection of more reductions. Arguments
3140 vect_force_simple_reduction (loop_vec_info loop_info
, gimple
*phi
,
3142 bool need_wrapping_integral_overflow
)
3144 enum vect_reduction_type v_reduc_type
;
3145 gimple
*def
= vect_is_simple_reduction (loop_info
, phi
, double_reduc
,
3146 need_wrapping_integral_overflow
,
3150 stmt_vec_info reduc_def_info
= vinfo_for_stmt (phi
);
3151 STMT_VINFO_REDUC_TYPE (reduc_def_info
) = v_reduc_type
;
3152 STMT_VINFO_REDUC_DEF (reduc_def_info
) = def
;
3157 /* Calculate cost of peeling the loop PEEL_ITERS_PROLOGUE times. */
3159 vect_get_known_peeling_cost (loop_vec_info loop_vinfo
, int peel_iters_prologue
,
3160 int *peel_iters_epilogue
,
3161 stmt_vector_for_cost
*scalar_cost_vec
,
3162 stmt_vector_for_cost
*prologue_cost_vec
,
3163 stmt_vector_for_cost
*epilogue_cost_vec
)
3166 int vf
= LOOP_VINFO_VECT_FACTOR (loop_vinfo
);
3168 if (!LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo
))
3170 *peel_iters_epilogue
= vf
/2;
3171 if (dump_enabled_p ())
3172 dump_printf_loc (MSG_NOTE
, vect_location
,
3173 "cost model: epilogue peel iters set to vf/2 "
3174 "because loop iterations are unknown .\n");
3176 /* If peeled iterations are known but number of scalar loop
3177 iterations are unknown, count a taken branch per peeled loop. */
3178 retval
= record_stmt_cost (prologue_cost_vec
, 1, cond_branch_taken
,
3179 NULL
, 0, vect_prologue
);
3180 retval
= record_stmt_cost (prologue_cost_vec
, 1, cond_branch_taken
,
3181 NULL
, 0, vect_epilogue
);
3185 int niters
= LOOP_VINFO_INT_NITERS (loop_vinfo
);
3186 peel_iters_prologue
= niters
< peel_iters_prologue
?
3187 niters
: peel_iters_prologue
;
3188 *peel_iters_epilogue
= (niters
- peel_iters_prologue
) % vf
;
3189 /* If we need to peel for gaps, but no peeling is required, we have to
3190 peel VF iterations. */
3191 if (LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo
) && !*peel_iters_epilogue
)
3192 *peel_iters_epilogue
= vf
;
3195 stmt_info_for_cost
*si
;
3197 if (peel_iters_prologue
)
3198 FOR_EACH_VEC_ELT (*scalar_cost_vec
, j
, si
)
3200 stmt_vec_info stmt_info
3201 = si
->stmt
? vinfo_for_stmt (si
->stmt
) : NULL
;
3202 retval
+= record_stmt_cost (prologue_cost_vec
,
3203 si
->count
* peel_iters_prologue
,
3204 si
->kind
, stmt_info
, si
->misalign
,
3207 if (*peel_iters_epilogue
)
3208 FOR_EACH_VEC_ELT (*scalar_cost_vec
, j
, si
)
3210 stmt_vec_info stmt_info
3211 = si
->stmt
? vinfo_for_stmt (si
->stmt
) : NULL
;
3212 retval
+= record_stmt_cost (epilogue_cost_vec
,
3213 si
->count
* *peel_iters_epilogue
,
3214 si
->kind
, stmt_info
, si
->misalign
,
3221 /* Function vect_estimate_min_profitable_iters
3223 Return the number of iterations required for the vector version of the
3224 loop to be profitable relative to the cost of the scalar version of the
3227 *RET_MIN_PROFITABLE_NITERS is a cost model profitability threshold
3228 of iterations for vectorization. -1 value means loop vectorization
3229 is not profitable. This returned value may be used for dynamic
3230 profitability check.
3232 *RET_MIN_PROFITABLE_ESTIMATE is a profitability threshold to be used
3233 for static check against estimated number of iterations. */
3236 vect_estimate_min_profitable_iters (loop_vec_info loop_vinfo
,
3237 int *ret_min_profitable_niters
,
3238 int *ret_min_profitable_estimate
)
3240 int min_profitable_iters
;
3241 int min_profitable_estimate
;
3242 int peel_iters_prologue
;
3243 int peel_iters_epilogue
;
3244 unsigned vec_inside_cost
= 0;
3245 int vec_outside_cost
= 0;
3246 unsigned vec_prologue_cost
= 0;
3247 unsigned vec_epilogue_cost
= 0;
3248 int scalar_single_iter_cost
= 0;
3249 int scalar_outside_cost
= 0;
3250 int vf
= LOOP_VINFO_VECT_FACTOR (loop_vinfo
);
3251 int npeel
= LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo
);
3252 void *target_cost_data
= LOOP_VINFO_TARGET_COST_DATA (loop_vinfo
);
3254 /* Cost model disabled. */
3255 if (unlimited_cost_model (LOOP_VINFO_LOOP (loop_vinfo
)))
3257 dump_printf_loc (MSG_NOTE
, vect_location
, "cost model disabled.\n");
3258 *ret_min_profitable_niters
= 0;
3259 *ret_min_profitable_estimate
= 0;
3263 /* Requires loop versioning tests to handle misalignment. */
3264 if (LOOP_REQUIRES_VERSIONING_FOR_ALIGNMENT (loop_vinfo
))
3266 /* FIXME: Make cost depend on complexity of individual check. */
3267 unsigned len
= LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo
).length ();
3268 (void) add_stmt_cost (target_cost_data
, len
, vector_stmt
, NULL
, 0,
3270 dump_printf (MSG_NOTE
,
3271 "cost model: Adding cost of checks for loop "
3272 "versioning to treat misalignment.\n");
3275 /* Requires loop versioning with alias checks. */
3276 if (LOOP_REQUIRES_VERSIONING_FOR_ALIAS (loop_vinfo
))
3278 /* FIXME: Make cost depend on complexity of individual check. */
3279 unsigned len
= LOOP_VINFO_COMP_ALIAS_DDRS (loop_vinfo
).length ();
3280 (void) add_stmt_cost (target_cost_data
, len
, vector_stmt
, NULL
, 0,
3282 dump_printf (MSG_NOTE
,
3283 "cost model: Adding cost of checks for loop "
3284 "versioning aliasing.\n");
3287 /* Requires loop versioning with niter checks. */
3288 if (LOOP_REQUIRES_VERSIONING_FOR_NITERS (loop_vinfo
))
3290 /* FIXME: Make cost depend on complexity of individual check. */
3291 (void) add_stmt_cost (target_cost_data
, 1, vector_stmt
, NULL
, 0,
3293 dump_printf (MSG_NOTE
,
3294 "cost model: Adding cost of checks for loop "
3295 "versioning niters.\n");
3298 if (LOOP_REQUIRES_VERSIONING (loop_vinfo
))
3299 (void) add_stmt_cost (target_cost_data
, 1, cond_branch_taken
, NULL
, 0,
3302 /* Count statements in scalar loop. Using this as scalar cost for a single
3305 TODO: Add outer loop support.
3307 TODO: Consider assigning different costs to different scalar
3310 scalar_single_iter_cost
3311 = LOOP_VINFO_SINGLE_SCALAR_ITERATION_COST (loop_vinfo
);
3313 /* Add additional cost for the peeled instructions in prologue and epilogue
3316 FORNOW: If we don't know the value of peel_iters for prologue or epilogue
3317 at compile-time - we assume it's vf/2 (the worst would be vf-1).
3319 TODO: Build an expression that represents peel_iters for prologue and
3320 epilogue to be used in a run-time test. */
3324 peel_iters_prologue
= vf
/2;
3325 dump_printf (MSG_NOTE
, "cost model: "
3326 "prologue peel iters set to vf/2.\n");
3328 /* If peeling for alignment is unknown, loop bound of main loop becomes
3330 peel_iters_epilogue
= vf
/2;
3331 dump_printf (MSG_NOTE
, "cost model: "
3332 "epilogue peel iters set to vf/2 because "
3333 "peeling for alignment is unknown.\n");
3335 /* If peeled iterations are unknown, count a taken branch and a not taken
3336 branch per peeled loop. Even if scalar loop iterations are known,
3337 vector iterations are not known since peeled prologue iterations are
3338 not known. Hence guards remain the same. */
3339 (void) add_stmt_cost (target_cost_data
, 1, cond_branch_taken
,
3340 NULL
, 0, vect_prologue
);
3341 (void) add_stmt_cost (target_cost_data
, 1, cond_branch_not_taken
,
3342 NULL
, 0, vect_prologue
);
3343 (void) add_stmt_cost (target_cost_data
, 1, cond_branch_taken
,
3344 NULL
, 0, vect_epilogue
);
3345 (void) add_stmt_cost (target_cost_data
, 1, cond_branch_not_taken
,
3346 NULL
, 0, vect_epilogue
);
3347 stmt_info_for_cost
*si
;
3349 FOR_EACH_VEC_ELT (LOOP_VINFO_SCALAR_ITERATION_COST (loop_vinfo
), j
, si
)
3351 struct _stmt_vec_info
*stmt_info
3352 = si
->stmt
? vinfo_for_stmt (si
->stmt
) : NULL
;
3353 (void) add_stmt_cost (target_cost_data
,
3354 si
->count
* peel_iters_prologue
,
3355 si
->kind
, stmt_info
, si
->misalign
,
3357 (void) add_stmt_cost (target_cost_data
,
3358 si
->count
* peel_iters_epilogue
,
3359 si
->kind
, stmt_info
, si
->misalign
,
3365 stmt_vector_for_cost prologue_cost_vec
, epilogue_cost_vec
;
3366 stmt_info_for_cost
*si
;
3368 void *data
= LOOP_VINFO_TARGET_COST_DATA (loop_vinfo
);
3370 prologue_cost_vec
.create (2);
3371 epilogue_cost_vec
.create (2);
3372 peel_iters_prologue
= npeel
;
3374 (void) vect_get_known_peeling_cost (loop_vinfo
, peel_iters_prologue
,
3375 &peel_iters_epilogue
,
3376 &LOOP_VINFO_SCALAR_ITERATION_COST
3379 &epilogue_cost_vec
);
3381 FOR_EACH_VEC_ELT (prologue_cost_vec
, j
, si
)
3383 struct _stmt_vec_info
*stmt_info
3384 = si
->stmt
? vinfo_for_stmt (si
->stmt
) : NULL
;
3385 (void) add_stmt_cost (data
, si
->count
, si
->kind
, stmt_info
,
3386 si
->misalign
, vect_prologue
);
3389 FOR_EACH_VEC_ELT (epilogue_cost_vec
, j
, si
)
3391 struct _stmt_vec_info
*stmt_info
3392 = si
->stmt
? vinfo_for_stmt (si
->stmt
) : NULL
;
3393 (void) add_stmt_cost (data
, si
->count
, si
->kind
, stmt_info
,
3394 si
->misalign
, vect_epilogue
);
3397 prologue_cost_vec
.release ();
3398 epilogue_cost_vec
.release ();
3401 /* FORNOW: The scalar outside cost is incremented in one of the
3404 1. The vectorizer checks for alignment and aliasing and generates
3405 a condition that allows dynamic vectorization. A cost model
3406 check is ANDED with the versioning condition. Hence scalar code
3407 path now has the added cost of the versioning check.
3409 if (cost > th & versioning_check)
3412 Hence run-time scalar is incremented by not-taken branch cost.
3414 2. The vectorizer then checks if a prologue is required. If the
3415 cost model check was not done before during versioning, it has to
3416 be done before the prologue check.
3419 prologue = scalar_iters
3424 if (prologue == num_iters)
3427 Hence the run-time scalar cost is incremented by a taken branch,
3428 plus a not-taken branch, plus a taken branch cost.
3430 3. The vectorizer then checks if an epilogue is required. If the
3431 cost model check was not done before during prologue check, it
3432 has to be done with the epilogue check.
3438 if (prologue == num_iters)
3441 if ((cost <= th) | (scalar_iters-prologue-epilogue == 0))
3444 Hence the run-time scalar cost should be incremented by 2 taken
3447 TODO: The back end may reorder the BBS's differently and reverse
3448 conditions/branch directions. Change the estimates below to
3449 something more reasonable. */
3451 /* If the number of iterations is known and we do not do versioning, we can
3452 decide whether to vectorize at compile time. Hence the scalar version
3453 do not carry cost model guard costs. */
3454 if (!LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo
)
3455 || LOOP_REQUIRES_VERSIONING (loop_vinfo
))
3457 /* Cost model check occurs at versioning. */
3458 if (LOOP_REQUIRES_VERSIONING (loop_vinfo
))
3459 scalar_outside_cost
+= vect_get_stmt_cost (cond_branch_not_taken
);
3462 /* Cost model check occurs at prologue generation. */
3463 if (LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo
) < 0)
3464 scalar_outside_cost
+= 2 * vect_get_stmt_cost (cond_branch_taken
)
3465 + vect_get_stmt_cost (cond_branch_not_taken
);
3466 /* Cost model check occurs at epilogue generation. */
3468 scalar_outside_cost
+= 2 * vect_get_stmt_cost (cond_branch_taken
);
3472 /* Complete the target-specific cost calculations. */
3473 finish_cost (LOOP_VINFO_TARGET_COST_DATA (loop_vinfo
), &vec_prologue_cost
,
3474 &vec_inside_cost
, &vec_epilogue_cost
);
3476 vec_outside_cost
= (int)(vec_prologue_cost
+ vec_epilogue_cost
);
3478 if (dump_enabled_p ())
3480 dump_printf_loc (MSG_NOTE
, vect_location
, "Cost model analysis: \n");
3481 dump_printf (MSG_NOTE
, " Vector inside of loop cost: %d\n",
3483 dump_printf (MSG_NOTE
, " Vector prologue cost: %d\n",
3485 dump_printf (MSG_NOTE
, " Vector epilogue cost: %d\n",
3487 dump_printf (MSG_NOTE
, " Scalar iteration cost: %d\n",
3488 scalar_single_iter_cost
);
3489 dump_printf (MSG_NOTE
, " Scalar outside cost: %d\n",
3490 scalar_outside_cost
);
3491 dump_printf (MSG_NOTE
, " Vector outside cost: %d\n",
3493 dump_printf (MSG_NOTE
, " prologue iterations: %d\n",
3494 peel_iters_prologue
);
3495 dump_printf (MSG_NOTE
, " epilogue iterations: %d\n",
3496 peel_iters_epilogue
);
3499 /* Calculate number of iterations required to make the vector version
3500 profitable, relative to the loop bodies only. The following condition
3502 SIC * niters + SOC > VIC * ((niters-PL_ITERS-EP_ITERS)/VF) + VOC
3504 SIC = scalar iteration cost, VIC = vector iteration cost,
3505 VOC = vector outside cost, VF = vectorization factor,
3506 PL_ITERS = prologue iterations, EP_ITERS= epilogue iterations
3507 SOC = scalar outside cost for run time cost model check. */
3509 if ((scalar_single_iter_cost
* vf
) > (int) vec_inside_cost
)
3511 if (vec_outside_cost
<= 0)
3512 min_profitable_iters
= 1;
3515 min_profitable_iters
= ((vec_outside_cost
- scalar_outside_cost
) * vf
3516 - vec_inside_cost
* peel_iters_prologue
3517 - vec_inside_cost
* peel_iters_epilogue
)
3518 / ((scalar_single_iter_cost
* vf
)
3521 if ((scalar_single_iter_cost
* vf
* min_profitable_iters
)
3522 <= (((int) vec_inside_cost
* min_profitable_iters
)
3523 + (((int) vec_outside_cost
- scalar_outside_cost
) * vf
)))
3524 min_profitable_iters
++;
3527 /* vector version will never be profitable. */
3530 if (LOOP_VINFO_LOOP (loop_vinfo
)->force_vectorize
)
3531 warning_at (vect_location
, OPT_Wopenmp_simd
, "vectorization "
3532 "did not happen for a simd loop");
3534 if (dump_enabled_p ())
3535 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
3536 "cost model: the vector iteration cost = %d "
3537 "divided by the scalar iteration cost = %d "
3538 "is greater or equal to the vectorization factor = %d"
3540 vec_inside_cost
, scalar_single_iter_cost
, vf
);
3541 *ret_min_profitable_niters
= -1;
3542 *ret_min_profitable_estimate
= -1;
3546 dump_printf (MSG_NOTE
,
3547 " Calculated minimum iters for profitability: %d\n",
3548 min_profitable_iters
);
3550 min_profitable_iters
=
3551 min_profitable_iters
< vf
? vf
: min_profitable_iters
;
3553 /* Because the condition we create is:
3554 if (niters <= min_profitable_iters)
3555 then skip the vectorized loop. */
3556 min_profitable_iters
--;
3558 if (dump_enabled_p ())
3559 dump_printf_loc (MSG_NOTE
, vect_location
,
3560 " Runtime profitability threshold = %d\n",
3561 min_profitable_iters
);
3563 *ret_min_profitable_niters
= min_profitable_iters
;
3565 /* Calculate number of iterations required to make the vector version
3566 profitable, relative to the loop bodies only.
3568 Non-vectorized variant is SIC * niters and it must win over vector
3569 variant on the expected loop trip count. The following condition must hold true:
3570 SIC * niters > VIC * ((niters-PL_ITERS-EP_ITERS)/VF) + VOC + SOC */
3572 if (vec_outside_cost
<= 0)
3573 min_profitable_estimate
= 1;
3576 min_profitable_estimate
= ((vec_outside_cost
+ scalar_outside_cost
) * vf
3577 - vec_inside_cost
* peel_iters_prologue
3578 - vec_inside_cost
* peel_iters_epilogue
)
3579 / ((scalar_single_iter_cost
* vf
)
3582 min_profitable_estimate
--;
3583 min_profitable_estimate
= MAX (min_profitable_estimate
, min_profitable_iters
);
3584 if (dump_enabled_p ())
3585 dump_printf_loc (MSG_NOTE
, vect_location
,
3586 " Static estimate profitability threshold = %d\n",
3587 min_profitable_estimate
);
3589 *ret_min_profitable_estimate
= min_profitable_estimate
;
3592 /* Writes into SEL a mask for a vec_perm, equivalent to a vec_shr by OFFSET
3593 vector elements (not bits) for a vector of mode MODE. */
3595 calc_vec_perm_mask_for_shift (enum machine_mode mode
, unsigned int offset
,
3598 unsigned int i
, nelt
= GET_MODE_NUNITS (mode
);
3600 for (i
= 0; i
< nelt
; i
++)
3601 sel
[i
] = (i
+ offset
) & (2*nelt
- 1);
3604 /* Checks whether the target supports whole-vector shifts for vectors of mode
3605 MODE. This is the case if _either_ the platform handles vec_shr_optab, _or_
3606 it supports vec_perm_const with masks for all necessary shift amounts. */
3608 have_whole_vector_shift (enum machine_mode mode
)
3610 if (optab_handler (vec_shr_optab
, mode
) != CODE_FOR_nothing
)
3613 if (direct_optab_handler (vec_perm_const_optab
, mode
) == CODE_FOR_nothing
)
3616 unsigned int i
, nelt
= GET_MODE_NUNITS (mode
);
3617 unsigned char *sel
= XALLOCAVEC (unsigned char, nelt
);
3619 for (i
= nelt
/2; i
>= 1; i
/=2)
3621 calc_vec_perm_mask_for_shift (mode
, i
, sel
);
3622 if (!can_vec_perm_p (mode
, false, sel
))
3628 /* Return the reduction operand (with index REDUC_INDEX) of STMT. */
3631 get_reduction_op (gimple
*stmt
, int reduc_index
)
3633 switch (get_gimple_rhs_class (gimple_assign_rhs_code (stmt
)))
3635 case GIMPLE_SINGLE_RHS
:
3636 gcc_assert (TREE_OPERAND_LENGTH (gimple_assign_rhs1 (stmt
))
3638 return TREE_OPERAND (gimple_assign_rhs1 (stmt
), reduc_index
);
3639 case GIMPLE_UNARY_RHS
:
3640 return gimple_assign_rhs1 (stmt
);
3641 case GIMPLE_BINARY_RHS
:
3643 ? gimple_assign_rhs2 (stmt
) : gimple_assign_rhs1 (stmt
));
3644 case GIMPLE_TERNARY_RHS
:
3645 return gimple_op (stmt
, reduc_index
+ 1);
3651 /* TODO: Close dependency between vect_model_*_cost and vectorizable_*
3652 functions. Design better to avoid maintenance issues. */
3654 /* Function vect_model_reduction_cost.
3656 Models cost for a reduction operation, including the vector ops
3657 generated within the strip-mine loop, the initial definition before
3658 the loop, and the epilogue code that must be generated. */
3661 vect_model_reduction_cost (stmt_vec_info stmt_info
, enum tree_code reduc_code
,
3662 int ncopies
, int reduc_index
)
3664 int prologue_cost
= 0, epilogue_cost
= 0;
3665 enum tree_code code
;
3668 gimple
*stmt
, *orig_stmt
;
3671 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_info
);
3672 struct loop
*loop
= NULL
;
3673 void *target_cost_data
;
3677 loop
= LOOP_VINFO_LOOP (loop_vinfo
);
3678 target_cost_data
= LOOP_VINFO_TARGET_COST_DATA (loop_vinfo
);
3681 target_cost_data
= BB_VINFO_TARGET_COST_DATA (STMT_VINFO_BB_VINFO (stmt_info
));
3683 /* Condition reductions generate two reductions in the loop. */
3684 if (STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
) == COND_REDUCTION
)
3687 /* Cost of reduction op inside loop. */
3688 unsigned inside_cost
= add_stmt_cost (target_cost_data
, ncopies
, vector_stmt
,
3689 stmt_info
, 0, vect_body
);
3690 stmt
= STMT_VINFO_STMT (stmt_info
);
3692 reduction_op
= get_reduction_op (stmt
, reduc_index
);
3694 vectype
= get_vectype_for_scalar_type (TREE_TYPE (reduction_op
));
3697 if (dump_enabled_p ())
3699 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
3700 "unsupported data-type ");
3701 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
,
3702 TREE_TYPE (reduction_op
));
3703 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
3708 mode
= TYPE_MODE (vectype
);
3709 orig_stmt
= STMT_VINFO_RELATED_STMT (stmt_info
);
3712 orig_stmt
= STMT_VINFO_STMT (stmt_info
);
3714 code
= gimple_assign_rhs_code (orig_stmt
);
3716 /* Add in cost for initial definition.
3717 For cond reduction we have four vectors: initial index, step, initial
3718 result of the data reduction, initial value of the index reduction. */
3719 int prologue_stmts
= STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
)
3720 == COND_REDUCTION
? 4 : 1;
3721 prologue_cost
+= add_stmt_cost (target_cost_data
, prologue_stmts
,
3722 scalar_to_vec
, stmt_info
, 0,
3725 /* Determine cost of epilogue code.
3727 We have a reduction operator that will reduce the vector in one statement.
3728 Also requires scalar extract. */
3730 if (!loop
|| !nested_in_vect_loop_p (loop
, orig_stmt
))
3732 if (reduc_code
!= ERROR_MARK
)
3734 if (STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
) == COND_REDUCTION
)
3736 /* An EQ stmt and an COND_EXPR stmt. */
3737 epilogue_cost
+= add_stmt_cost (target_cost_data
, 2,
3738 vector_stmt
, stmt_info
, 0,
3740 /* Reduction of the max index and a reduction of the found
3742 epilogue_cost
+= add_stmt_cost (target_cost_data
, 2,
3743 vec_to_scalar
, stmt_info
, 0,
3745 /* A broadcast of the max value. */
3746 epilogue_cost
+= add_stmt_cost (target_cost_data
, 1,
3747 scalar_to_vec
, stmt_info
, 0,
3752 epilogue_cost
+= add_stmt_cost (target_cost_data
, 1, vector_stmt
,
3753 stmt_info
, 0, vect_epilogue
);
3754 epilogue_cost
+= add_stmt_cost (target_cost_data
, 1,
3755 vec_to_scalar
, stmt_info
, 0,
3761 int vec_size_in_bits
= tree_to_uhwi (TYPE_SIZE (vectype
));
3763 TYPE_SIZE (TREE_TYPE (gimple_assign_lhs (orig_stmt
)));
3764 int element_bitsize
= tree_to_uhwi (bitsize
);
3765 int nelements
= vec_size_in_bits
/ element_bitsize
;
3767 optab
= optab_for_tree_code (code
, vectype
, optab_default
);
3769 /* We have a whole vector shift available. */
3770 if (VECTOR_MODE_P (mode
)
3771 && optab_handler (optab
, mode
) != CODE_FOR_nothing
3772 && have_whole_vector_shift (mode
))
3774 /* Final reduction via vector shifts and the reduction operator.
3775 Also requires scalar extract. */
3776 epilogue_cost
+= add_stmt_cost (target_cost_data
,
3777 exact_log2 (nelements
) * 2,
3778 vector_stmt
, stmt_info
, 0,
3780 epilogue_cost
+= add_stmt_cost (target_cost_data
, 1,
3781 vec_to_scalar
, stmt_info
, 0,
3785 /* Use extracts and reduction op for final reduction. For N
3786 elements, we have N extracts and N-1 reduction ops. */
3787 epilogue_cost
+= add_stmt_cost (target_cost_data
,
3788 nelements
+ nelements
- 1,
3789 vector_stmt
, stmt_info
, 0,
3794 if (dump_enabled_p ())
3795 dump_printf (MSG_NOTE
,
3796 "vect_model_reduction_cost: inside_cost = %d, "
3797 "prologue_cost = %d, epilogue_cost = %d .\n", inside_cost
,
3798 prologue_cost
, epilogue_cost
);
3804 /* Function vect_model_induction_cost.
3806 Models cost for induction operations. */
3809 vect_model_induction_cost (stmt_vec_info stmt_info
, int ncopies
)
3811 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_info
);
3812 void *target_cost_data
= LOOP_VINFO_TARGET_COST_DATA (loop_vinfo
);
3813 unsigned inside_cost
, prologue_cost
;
3815 if (PURE_SLP_STMT (stmt_info
))
3818 /* loop cost for vec_loop. */
3819 inside_cost
= add_stmt_cost (target_cost_data
, ncopies
, vector_stmt
,
3820 stmt_info
, 0, vect_body
);
3822 /* prologue cost for vec_init and vec_step. */
3823 prologue_cost
= add_stmt_cost (target_cost_data
, 2, scalar_to_vec
,
3824 stmt_info
, 0, vect_prologue
);
3826 if (dump_enabled_p ())
3827 dump_printf_loc (MSG_NOTE
, vect_location
,
3828 "vect_model_induction_cost: inside_cost = %d, "
3829 "prologue_cost = %d .\n", inside_cost
, prologue_cost
);
3834 /* Function get_initial_def_for_reduction
3837 STMT - a stmt that performs a reduction operation in the loop.
3838 INIT_VAL - the initial value of the reduction variable
3841 ADJUSTMENT_DEF - a tree that holds a value to be added to the final result
3842 of the reduction (used for adjusting the epilog - see below).
3843 Return a vector variable, initialized according to the operation that STMT
3844 performs. This vector will be used as the initial value of the
3845 vector of partial results.
3847 Option1 (adjust in epilog): Initialize the vector as follows:
3848 add/bit or/xor: [0,0,...,0,0]
3849 mult/bit and: [1,1,...,1,1]
3850 min/max/cond_expr: [init_val,init_val,..,init_val,init_val]
3851 and when necessary (e.g. add/mult case) let the caller know
3852 that it needs to adjust the result by init_val.
3854 Option2: Initialize the vector as follows:
3855 add/bit or/xor: [init_val,0,0,...,0]
3856 mult/bit and: [init_val,1,1,...,1]
3857 min/max/cond_expr: [init_val,init_val,...,init_val]
3858 and no adjustments are needed.
3860 For example, for the following code:
3866 STMT is 's = s + a[i]', and the reduction variable is 's'.
3867 For a vector of 4 units, we want to return either [0,0,0,init_val],
3868 or [0,0,0,0] and let the caller know that it needs to adjust
3869 the result at the end by 'init_val'.
3871 FORNOW, we are using the 'adjust in epilog' scheme, because this way the
3872 initialization vector is simpler (same element in all entries), if
3873 ADJUSTMENT_DEF is not NULL, and Option2 otherwise.
3875 A cost model should help decide between these two schemes. */
3878 get_initial_def_for_reduction (gimple
*stmt
, tree init_val
,
3879 tree
*adjustment_def
)
3881 stmt_vec_info stmt_vinfo
= vinfo_for_stmt (stmt
);
3882 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_vinfo
);
3883 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
3884 tree scalar_type
= TREE_TYPE (init_val
);
3885 tree vectype
= get_vectype_for_scalar_type (scalar_type
);
3887 enum tree_code code
= gimple_assign_rhs_code (stmt
);
3892 bool nested_in_vect_loop
= false;
3893 REAL_VALUE_TYPE real_init_val
= dconst0
;
3894 int int_init_val
= 0;
3895 gimple
*def_stmt
= NULL
;
3896 gimple_seq stmts
= NULL
;
3898 gcc_assert (vectype
);
3899 nunits
= TYPE_VECTOR_SUBPARTS (vectype
);
3901 gcc_assert (POINTER_TYPE_P (scalar_type
) || INTEGRAL_TYPE_P (scalar_type
)
3902 || SCALAR_FLOAT_TYPE_P (scalar_type
));
3904 if (nested_in_vect_loop_p (loop
, stmt
))
3905 nested_in_vect_loop
= true;
3907 gcc_assert (loop
== (gimple_bb (stmt
))->loop_father
);
3909 /* In case of double reduction we only create a vector variable to be put
3910 in the reduction phi node. The actual statement creation is done in
3911 vect_create_epilog_for_reduction. */
3912 if (adjustment_def
&& nested_in_vect_loop
3913 && TREE_CODE (init_val
) == SSA_NAME
3914 && (def_stmt
= SSA_NAME_DEF_STMT (init_val
))
3915 && gimple_code (def_stmt
) == GIMPLE_PHI
3916 && flow_bb_inside_loop_p (loop
, gimple_bb (def_stmt
))
3917 && vinfo_for_stmt (def_stmt
)
3918 && STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def_stmt
))
3919 == vect_double_reduction_def
)
3921 *adjustment_def
= NULL
;
3922 return vect_create_destination_var (init_val
, vectype
);
3925 /* In case of a nested reduction do not use an adjustment def as
3926 that case is not supported by the epilogue generation correctly
3927 if ncopies is not one. */
3928 if (adjustment_def
&& nested_in_vect_loop
)
3930 *adjustment_def
= NULL
;
3931 return vect_get_vec_def_for_operand (init_val
, stmt
);
3936 case WIDEN_SUM_EXPR
:
3945 /* ADJUSMENT_DEF is NULL when called from
3946 vect_create_epilog_for_reduction to vectorize double reduction. */
3948 *adjustment_def
= init_val
;
3950 if (code
== MULT_EXPR
)
3952 real_init_val
= dconst1
;
3956 if (code
== BIT_AND_EXPR
)
3959 if (SCALAR_FLOAT_TYPE_P (scalar_type
))
3960 def_for_init
= build_real (scalar_type
, real_init_val
);
3962 def_for_init
= build_int_cst (scalar_type
, int_init_val
);
3964 /* Create a vector of '0' or '1' except the first element. */
3965 elts
= XALLOCAVEC (tree
, nunits
);
3966 for (i
= nunits
- 2; i
>= 0; --i
)
3967 elts
[i
+ 1] = def_for_init
;
3969 /* Option1: the first element is '0' or '1' as well. */
3972 elts
[0] = def_for_init
;
3973 init_def
= build_vector (vectype
, elts
);
3977 /* Option2: the first element is INIT_VAL. */
3979 if (TREE_CONSTANT (init_val
))
3980 init_def
= build_vector (vectype
, elts
);
3983 vec
<constructor_elt
, va_gc
> *v
;
3984 vec_alloc (v
, nunits
);
3985 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, init_val
);
3986 for (i
= 1; i
< nunits
; ++i
)
3987 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, elts
[i
]);
3988 init_def
= build_constructor (vectype
, v
);
3998 *adjustment_def
= NULL_TREE
;
3999 if (STMT_VINFO_VEC_REDUCTION_TYPE (stmt_vinfo
) != COND_REDUCTION
)
4001 init_def
= vect_get_vec_def_for_operand (init_val
, stmt
);
4005 init_val
= gimple_convert (&stmts
, TREE_TYPE (vectype
), init_val
);
4006 if (! gimple_seq_empty_p (stmts
))
4007 gsi_insert_seq_on_edge_immediate (loop_preheader_edge (loop
), stmts
);
4008 init_def
= build_vector_from_val (vectype
, init_val
);
4018 /* Function vect_create_epilog_for_reduction
4020 Create code at the loop-epilog to finalize the result of a reduction
4023 VECT_DEFS is list of vector of partial results, i.e., the lhs's of vector
4024 reduction statements.
4025 STMT is the scalar reduction stmt that is being vectorized.
4026 NCOPIES is > 1 in case the vectorization factor (VF) is bigger than the
4027 number of elements that we can fit in a vectype (nunits). In this case
4028 we have to generate more than one vector stmt - i.e - we need to "unroll"
4029 the vector stmt by a factor VF/nunits. For more details see documentation
4030 in vectorizable_operation.
4031 REDUC_CODE is the tree-code for the epilog reduction.
4032 REDUCTION_PHIS is a list of the phi-nodes that carry the reduction
4034 REDUC_INDEX is the index of the operand in the right hand side of the
4035 statement that is defined by REDUCTION_PHI.
4036 DOUBLE_REDUC is TRUE if double reduction phi nodes should be handled.
4037 SLP_NODE is an SLP node containing a group of reduction statements. The
4038 first one in this group is STMT.
4039 INDUCTION_INDEX is the index of the loop for condition reductions.
4040 Otherwise it is undefined.
4043 1. Creates the reduction def-use cycles: sets the arguments for
4045 The loop-entry argument is the vectorized initial-value of the reduction.
4046 The loop-latch argument is taken from VECT_DEFS - the vector of partial
4048 2. "Reduces" each vector of partial results VECT_DEFS into a single result,
4049 by applying the operation specified by REDUC_CODE if available, or by
4050 other means (whole-vector shifts or a scalar loop).
4051 The function also creates a new phi node at the loop exit to preserve
4052 loop-closed form, as illustrated below.
4054 The flow at the entry to this function:
4057 vec_def = phi <null, null> # REDUCTION_PHI
4058 VECT_DEF = vector_stmt # vectorized form of STMT
4059 s_loop = scalar_stmt # (scalar) STMT
4061 s_out0 = phi <s_loop> # (scalar) EXIT_PHI
4065 The above is transformed by this function into:
4068 vec_def = phi <vec_init, VECT_DEF> # REDUCTION_PHI
4069 VECT_DEF = vector_stmt # vectorized form of STMT
4070 s_loop = scalar_stmt # (scalar) STMT
4072 s_out0 = phi <s_loop> # (scalar) EXIT_PHI
4073 v_out1 = phi <VECT_DEF> # NEW_EXIT_PHI
4074 v_out2 = reduce <v_out1>
4075 s_out3 = extract_field <v_out2, 0>
4076 s_out4 = adjust_result <s_out3>
4082 vect_create_epilog_for_reduction (vec
<tree
> vect_defs
, gimple
*stmt
,
4083 int ncopies
, enum tree_code reduc_code
,
4084 vec
<gimple
*> reduction_phis
,
4085 int reduc_index
, bool double_reduc
,
4086 slp_tree slp_node
, tree induction_index
)
4088 stmt_vec_info stmt_info
= vinfo_for_stmt (stmt
);
4089 stmt_vec_info prev_phi_info
;
4092 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_info
);
4093 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
), *outer_loop
= NULL
;
4094 basic_block exit_bb
;
4097 gimple
*new_phi
= NULL
, *phi
;
4098 gimple_stmt_iterator exit_gsi
;
4100 tree new_temp
= NULL_TREE
, new_dest
, new_name
, new_scalar_dest
;
4101 gimple
*epilog_stmt
= NULL
;
4102 enum tree_code code
= gimple_assign_rhs_code (stmt
);
4105 tree adjustment_def
= NULL
;
4106 tree vec_initial_def
= NULL
;
4107 tree reduction_op
, expr
, def
, initial_def
= NULL
;
4108 tree orig_name
, scalar_result
;
4109 imm_use_iterator imm_iter
, phi_imm_iter
;
4110 use_operand_p use_p
, phi_use_p
;
4111 gimple
*use_stmt
, *orig_stmt
, *reduction_phi
= NULL
;
4112 bool nested_in_vect_loop
= false;
4113 auto_vec
<gimple
*> new_phis
;
4114 auto_vec
<gimple
*> inner_phis
;
4115 enum vect_def_type dt
= vect_unknown_def_type
;
4117 auto_vec
<tree
> scalar_results
;
4118 unsigned int group_size
= 1, k
, ratio
;
4119 auto_vec
<tree
> vec_initial_defs
;
4120 auto_vec
<gimple
*> phis
;
4121 bool slp_reduc
= false;
4122 tree new_phi_result
;
4123 gimple
*inner_phi
= NULL
;
4126 group_size
= SLP_TREE_SCALAR_STMTS (slp_node
).length ();
4128 if (nested_in_vect_loop_p (loop
, stmt
))
4132 nested_in_vect_loop
= true;
4133 gcc_assert (!slp_node
);
4136 reduction_op
= get_reduction_op (stmt
, reduc_index
);
4138 vectype
= get_vectype_for_scalar_type (TREE_TYPE (reduction_op
));
4139 gcc_assert (vectype
);
4140 mode
= TYPE_MODE (vectype
);
4142 /* 1. Create the reduction def-use cycle:
4143 Set the arguments of REDUCTION_PHIS, i.e., transform
4146 vec_def = phi <null, null> # REDUCTION_PHI
4147 VECT_DEF = vector_stmt # vectorized form of STMT
4153 vec_def = phi <vec_init, VECT_DEF> # REDUCTION_PHI
4154 VECT_DEF = vector_stmt # vectorized form of STMT
4157 (in case of SLP, do it for all the phis). */
4159 /* Get the loop-entry arguments. */
4160 enum vect_def_type initial_def_dt
= vect_unknown_def_type
;
4162 vect_get_vec_defs (reduction_op
, NULL_TREE
, stmt
, &vec_initial_defs
,
4163 NULL
, slp_node
, reduc_index
);
4166 /* Get at the scalar def before the loop, that defines the initial value
4167 of the reduction variable. */
4168 gimple
*def_stmt
= SSA_NAME_DEF_STMT (reduction_op
);
4169 initial_def
= PHI_ARG_DEF_FROM_EDGE (def_stmt
,
4170 loop_preheader_edge (loop
));
4171 vect_is_simple_use (initial_def
, loop_vinfo
, &def_stmt
, &initial_def_dt
);
4172 vec_initial_def
= get_initial_def_for_reduction (stmt
, initial_def
,
4174 vec_initial_defs
.create (1);
4175 vec_initial_defs
.quick_push (vec_initial_def
);
4178 /* Set phi nodes arguments. */
4179 FOR_EACH_VEC_ELT (reduction_phis
, i
, phi
)
4181 tree vec_init_def
, def
;
4183 vec_init_def
= force_gimple_operand (vec_initial_defs
[i
], &stmts
,
4186 gsi_insert_seq_on_edge_immediate (loop_preheader_edge (loop
), stmts
);
4189 for (j
= 0; j
< ncopies
; j
++)
4193 phi
= STMT_VINFO_RELATED_STMT (vinfo_for_stmt (phi
));
4194 if (nested_in_vect_loop
)
4196 = vect_get_vec_def_for_stmt_copy (initial_def_dt
,
4200 /* Set the loop-entry arg of the reduction-phi. */
4202 if (STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
)
4203 == INTEGER_INDUC_COND_REDUCTION
)
4205 /* Initialise the reduction phi to zero. This prevents initial
4206 values of non-zero interferring with the reduction op. */
4207 gcc_assert (ncopies
== 1);
4208 gcc_assert (i
== 0);
4210 tree vec_init_def_type
= TREE_TYPE (vec_init_def
);
4211 tree zero_vec
= build_zero_cst (vec_init_def_type
);
4213 add_phi_arg (as_a
<gphi
*> (phi
), zero_vec
,
4214 loop_preheader_edge (loop
), UNKNOWN_LOCATION
);
4217 add_phi_arg (as_a
<gphi
*> (phi
), vec_init_def
,
4218 loop_preheader_edge (loop
), UNKNOWN_LOCATION
);
4220 /* Set the loop-latch arg for the reduction-phi. */
4222 def
= vect_get_vec_def_for_stmt_copy (vect_unknown_def_type
, def
);
4224 add_phi_arg (as_a
<gphi
*> (phi
), def
, loop_latch_edge (loop
),
4227 if (dump_enabled_p ())
4229 dump_printf_loc (MSG_NOTE
, vect_location
,
4230 "transform reduction: created def-use cycle: ");
4231 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
, phi
, 0);
4232 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
, SSA_NAME_DEF_STMT (def
), 0);
4237 /* 2. Create epilog code.
4238 The reduction epilog code operates across the elements of the vector
4239 of partial results computed by the vectorized loop.
4240 The reduction epilog code consists of:
4242 step 1: compute the scalar result in a vector (v_out2)
4243 step 2: extract the scalar result (s_out3) from the vector (v_out2)
4244 step 3: adjust the scalar result (s_out3) if needed.
4246 Step 1 can be accomplished using one the following three schemes:
4247 (scheme 1) using reduc_code, if available.
4248 (scheme 2) using whole-vector shifts, if available.
4249 (scheme 3) using a scalar loop. In this case steps 1+2 above are
4252 The overall epilog code looks like this:
4254 s_out0 = phi <s_loop> # original EXIT_PHI
4255 v_out1 = phi <VECT_DEF> # NEW_EXIT_PHI
4256 v_out2 = reduce <v_out1> # step 1
4257 s_out3 = extract_field <v_out2, 0> # step 2
4258 s_out4 = adjust_result <s_out3> # step 3
4260 (step 3 is optional, and steps 1 and 2 may be combined).
4261 Lastly, the uses of s_out0 are replaced by s_out4. */
4264 /* 2.1 Create new loop-exit-phis to preserve loop-closed form:
4265 v_out1 = phi <VECT_DEF>
4266 Store them in NEW_PHIS. */
4268 exit_bb
= single_exit (loop
)->dest
;
4269 prev_phi_info
= NULL
;
4270 new_phis
.create (vect_defs
.length ());
4271 FOR_EACH_VEC_ELT (vect_defs
, i
, def
)
4273 for (j
= 0; j
< ncopies
; j
++)
4275 tree new_def
= copy_ssa_name (def
);
4276 phi
= create_phi_node (new_def
, exit_bb
);
4277 set_vinfo_for_stmt (phi
, new_stmt_vec_info (phi
, loop_vinfo
));
4279 new_phis
.quick_push (phi
);
4282 def
= vect_get_vec_def_for_stmt_copy (dt
, def
);
4283 STMT_VINFO_RELATED_STMT (prev_phi_info
) = phi
;
4286 SET_PHI_ARG_DEF (phi
, single_exit (loop
)->dest_idx
, def
);
4287 prev_phi_info
= vinfo_for_stmt (phi
);
4291 /* The epilogue is created for the outer-loop, i.e., for the loop being
4292 vectorized. Create exit phis for the outer loop. */
4296 exit_bb
= single_exit (loop
)->dest
;
4297 inner_phis
.create (vect_defs
.length ());
4298 FOR_EACH_VEC_ELT (new_phis
, i
, phi
)
4300 tree new_result
= copy_ssa_name (PHI_RESULT (phi
));
4301 gphi
*outer_phi
= create_phi_node (new_result
, exit_bb
);
4302 SET_PHI_ARG_DEF (outer_phi
, single_exit (loop
)->dest_idx
,
4304 set_vinfo_for_stmt (outer_phi
, new_stmt_vec_info (outer_phi
,
4306 inner_phis
.quick_push (phi
);
4307 new_phis
[i
] = outer_phi
;
4308 prev_phi_info
= vinfo_for_stmt (outer_phi
);
4309 while (STMT_VINFO_RELATED_STMT (vinfo_for_stmt (phi
)))
4311 phi
= STMT_VINFO_RELATED_STMT (vinfo_for_stmt (phi
));
4312 new_result
= copy_ssa_name (PHI_RESULT (phi
));
4313 outer_phi
= create_phi_node (new_result
, exit_bb
);
4314 SET_PHI_ARG_DEF (outer_phi
, single_exit (loop
)->dest_idx
,
4316 set_vinfo_for_stmt (outer_phi
, new_stmt_vec_info (outer_phi
,
4318 STMT_VINFO_RELATED_STMT (prev_phi_info
) = outer_phi
;
4319 prev_phi_info
= vinfo_for_stmt (outer_phi
);
4324 exit_gsi
= gsi_after_labels (exit_bb
);
4326 /* 2.2 Get the relevant tree-code to use in the epilog for schemes 2,3
4327 (i.e. when reduc_code is not available) and in the final adjustment
4328 code (if needed). Also get the original scalar reduction variable as
4329 defined in the loop. In case STMT is a "pattern-stmt" (i.e. - it
4330 represents a reduction pattern), the tree-code and scalar-def are
4331 taken from the original stmt that the pattern-stmt (STMT) replaces.
4332 Otherwise (it is a regular reduction) - the tree-code and scalar-def
4333 are taken from STMT. */
4335 orig_stmt
= STMT_VINFO_RELATED_STMT (stmt_info
);
4338 /* Regular reduction */
4343 /* Reduction pattern */
4344 stmt_vec_info stmt_vinfo
= vinfo_for_stmt (orig_stmt
);
4345 gcc_assert (STMT_VINFO_IN_PATTERN_P (stmt_vinfo
));
4346 gcc_assert (STMT_VINFO_RELATED_STMT (stmt_vinfo
) == stmt
);
4349 code
= gimple_assign_rhs_code (orig_stmt
);
4350 /* For MINUS_EXPR the initial vector is [init_val,0,...,0], therefore,
4351 partial results are added and not subtracted. */
4352 if (code
== MINUS_EXPR
)
4355 scalar_dest
= gimple_assign_lhs (orig_stmt
);
4356 scalar_type
= TREE_TYPE (scalar_dest
);
4357 scalar_results
.create (group_size
);
4358 new_scalar_dest
= vect_create_destination_var (scalar_dest
, NULL
);
4359 bitsize
= TYPE_SIZE (scalar_type
);
4361 /* In case this is a reduction in an inner-loop while vectorizing an outer
4362 loop - we don't need to extract a single scalar result at the end of the
4363 inner-loop (unless it is double reduction, i.e., the use of reduction is
4364 outside the outer-loop). The final vector of partial results will be used
4365 in the vectorized outer-loop, or reduced to a scalar result at the end of
4367 if (nested_in_vect_loop
&& !double_reduc
)
4368 goto vect_finalize_reduction
;
4370 /* SLP reduction without reduction chain, e.g.,
4374 b2 = operation (b1) */
4375 slp_reduc
= (slp_node
&& !GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt
)));
4377 /* In case of reduction chain, e.g.,
4380 a3 = operation (a2),
4382 we may end up with more than one vector result. Here we reduce them to
4384 if (GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt
)))
4386 tree first_vect
= PHI_RESULT (new_phis
[0]);
4388 gassign
*new_vec_stmt
= NULL
;
4390 vec_dest
= vect_create_destination_var (scalar_dest
, vectype
);
4391 for (k
= 1; k
< new_phis
.length (); k
++)
4393 gimple
*next_phi
= new_phis
[k
];
4394 tree second_vect
= PHI_RESULT (next_phi
);
4396 tmp
= build2 (code
, vectype
, first_vect
, second_vect
);
4397 new_vec_stmt
= gimple_build_assign (vec_dest
, tmp
);
4398 first_vect
= make_ssa_name (vec_dest
, new_vec_stmt
);
4399 gimple_assign_set_lhs (new_vec_stmt
, first_vect
);
4400 gsi_insert_before (&exit_gsi
, new_vec_stmt
, GSI_SAME_STMT
);
4403 new_phi_result
= first_vect
;
4406 new_phis
.truncate (0);
4407 new_phis
.safe_push (new_vec_stmt
);
4411 new_phi_result
= PHI_RESULT (new_phis
[0]);
4413 if (STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
) == COND_REDUCTION
)
4415 /* For condition reductions, we have a vector (NEW_PHI_RESULT) containing
4416 various data values where the condition matched and another vector
4417 (INDUCTION_INDEX) containing all the indexes of those matches. We
4418 need to extract the last matching index (which will be the index with
4419 highest value) and use this to index into the data vector.
4420 For the case where there were no matches, the data vector will contain
4421 all default values and the index vector will be all zeros. */
4423 /* Get various versions of the type of the vector of indexes. */
4424 tree index_vec_type
= TREE_TYPE (induction_index
);
4425 gcc_checking_assert (TYPE_UNSIGNED (index_vec_type
));
4426 tree index_scalar_type
= TREE_TYPE (index_vec_type
);
4427 tree index_vec_cmp_type
= build_same_sized_truth_vector_type
4430 /* Get an unsigned integer version of the type of the data vector. */
4431 int scalar_precision
= GET_MODE_PRECISION (TYPE_MODE (scalar_type
));
4432 tree scalar_type_unsigned
= make_unsigned_type (scalar_precision
);
4433 tree vectype_unsigned
= build_vector_type
4434 (scalar_type_unsigned
, TYPE_VECTOR_SUBPARTS (vectype
));
4436 /* First we need to create a vector (ZERO_VEC) of zeros and another
4437 vector (MAX_INDEX_VEC) filled with the last matching index, which we
4438 can create using a MAX reduction and then expanding.
4439 In the case where the loop never made any matches, the max index will
4442 /* Vector of {0, 0, 0,...}. */
4443 tree zero_vec
= make_ssa_name (vectype
);
4444 tree zero_vec_rhs
= build_zero_cst (vectype
);
4445 gimple
*zero_vec_stmt
= gimple_build_assign (zero_vec
, zero_vec_rhs
);
4446 gsi_insert_before (&exit_gsi
, zero_vec_stmt
, GSI_SAME_STMT
);
4448 /* Find maximum value from the vector of found indexes. */
4449 tree max_index
= make_ssa_name (index_scalar_type
);
4450 gimple
*max_index_stmt
= gimple_build_assign (max_index
, REDUC_MAX_EXPR
,
4452 gsi_insert_before (&exit_gsi
, max_index_stmt
, GSI_SAME_STMT
);
4454 /* Vector of {max_index, max_index, max_index,...}. */
4455 tree max_index_vec
= make_ssa_name (index_vec_type
);
4456 tree max_index_vec_rhs
= build_vector_from_val (index_vec_type
,
4458 gimple
*max_index_vec_stmt
= gimple_build_assign (max_index_vec
,
4460 gsi_insert_before (&exit_gsi
, max_index_vec_stmt
, GSI_SAME_STMT
);
4462 /* Next we compare the new vector (MAX_INDEX_VEC) full of max indexes
4463 with the vector (INDUCTION_INDEX) of found indexes, choosing values
4464 from the data vector (NEW_PHI_RESULT) for matches, 0 (ZERO_VEC)
4465 otherwise. Only one value should match, resulting in a vector
4466 (VEC_COND) with one data value and the rest zeros.
4467 In the case where the loop never made any matches, every index will
4468 match, resulting in a vector with all data values (which will all be
4469 the default value). */
4471 /* Compare the max index vector to the vector of found indexes to find
4472 the position of the max value. */
4473 tree vec_compare
= make_ssa_name (index_vec_cmp_type
);
4474 gimple
*vec_compare_stmt
= gimple_build_assign (vec_compare
, EQ_EXPR
,
4477 gsi_insert_before (&exit_gsi
, vec_compare_stmt
, GSI_SAME_STMT
);
4479 /* Use the compare to choose either values from the data vector or
4481 tree vec_cond
= make_ssa_name (vectype
);
4482 gimple
*vec_cond_stmt
= gimple_build_assign (vec_cond
, VEC_COND_EXPR
,
4483 vec_compare
, new_phi_result
,
4485 gsi_insert_before (&exit_gsi
, vec_cond_stmt
, GSI_SAME_STMT
);
4487 /* Finally we need to extract the data value from the vector (VEC_COND)
4488 into a scalar (MATCHED_DATA_REDUC). Logically we want to do a OR
4489 reduction, but because this doesn't exist, we can use a MAX reduction
4490 instead. The data value might be signed or a float so we need to cast
4492 In the case where the loop never made any matches, the data values are
4493 all identical, and so will reduce down correctly. */
4495 /* Make the matched data values unsigned. */
4496 tree vec_cond_cast
= make_ssa_name (vectype_unsigned
);
4497 tree vec_cond_cast_rhs
= build1 (VIEW_CONVERT_EXPR
, vectype_unsigned
,
4499 gimple
*vec_cond_cast_stmt
= gimple_build_assign (vec_cond_cast
,
4502 gsi_insert_before (&exit_gsi
, vec_cond_cast_stmt
, GSI_SAME_STMT
);
4504 /* Reduce down to a scalar value. */
4505 tree data_reduc
= make_ssa_name (scalar_type_unsigned
);
4506 optab ot
= optab_for_tree_code (REDUC_MAX_EXPR
, vectype_unsigned
,
4508 gcc_assert (optab_handler (ot
, TYPE_MODE (vectype_unsigned
))
4509 != CODE_FOR_nothing
);
4510 gimple
*data_reduc_stmt
= gimple_build_assign (data_reduc
,
4513 gsi_insert_before (&exit_gsi
, data_reduc_stmt
, GSI_SAME_STMT
);
4515 /* Convert the reduced value back to the result type and set as the
4517 tree data_reduc_cast
= build1 (VIEW_CONVERT_EXPR
, scalar_type
,
4519 epilog_stmt
= gimple_build_assign (new_scalar_dest
, data_reduc_cast
);
4520 new_temp
= make_ssa_name (new_scalar_dest
, epilog_stmt
);
4521 gimple_assign_set_lhs (epilog_stmt
, new_temp
);
4522 gsi_insert_before (&exit_gsi
, epilog_stmt
, GSI_SAME_STMT
);
4523 scalar_results
.safe_push (new_temp
);
4526 /* 2.3 Create the reduction code, using one of the three schemes described
4527 above. In SLP we simply need to extract all the elements from the
4528 vector (without reducing them), so we use scalar shifts. */
4529 else if (reduc_code
!= ERROR_MARK
&& !slp_reduc
)
4535 v_out2 = reduc_expr <v_out1> */
4537 if (dump_enabled_p ())
4538 dump_printf_loc (MSG_NOTE
, vect_location
,
4539 "Reduce using direct vector reduction.\n");
4541 vec_elem_type
= TREE_TYPE (TREE_TYPE (new_phi_result
));
4542 if (!useless_type_conversion_p (scalar_type
, vec_elem_type
))
4545 vect_create_destination_var (scalar_dest
, vec_elem_type
);
4546 tmp
= build1 (reduc_code
, vec_elem_type
, new_phi_result
);
4547 epilog_stmt
= gimple_build_assign (tmp_dest
, tmp
);
4548 new_temp
= make_ssa_name (tmp_dest
, epilog_stmt
);
4549 gimple_assign_set_lhs (epilog_stmt
, new_temp
);
4550 gsi_insert_before (&exit_gsi
, epilog_stmt
, GSI_SAME_STMT
);
4552 tmp
= build1 (NOP_EXPR
, scalar_type
, new_temp
);
4555 tmp
= build1 (reduc_code
, scalar_type
, new_phi_result
);
4557 epilog_stmt
= gimple_build_assign (new_scalar_dest
, tmp
);
4558 new_temp
= make_ssa_name (new_scalar_dest
, epilog_stmt
);
4559 gimple_assign_set_lhs (epilog_stmt
, new_temp
);
4560 gsi_insert_before (&exit_gsi
, epilog_stmt
, GSI_SAME_STMT
);
4562 if (STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
)
4563 == INTEGER_INDUC_COND_REDUCTION
)
4565 /* Earlier we set the initial value to be zero. Check the result
4566 and if it is zero then replace with the original initial
4568 tree zero
= build_zero_cst (scalar_type
);
4569 tree zcompare
= build2 (EQ_EXPR
, boolean_type_node
, new_temp
, zero
);
4571 tmp
= make_ssa_name (new_scalar_dest
);
4572 epilog_stmt
= gimple_build_assign (tmp
, COND_EXPR
, zcompare
,
4573 initial_def
, new_temp
);
4574 gsi_insert_before (&exit_gsi
, epilog_stmt
, GSI_SAME_STMT
);
4578 scalar_results
.safe_push (new_temp
);
4582 bool reduce_with_shift
= have_whole_vector_shift (mode
);
4583 int element_bitsize
= tree_to_uhwi (bitsize
);
4584 int vec_size_in_bits
= tree_to_uhwi (TYPE_SIZE (vectype
));
4587 /* Regardless of whether we have a whole vector shift, if we're
4588 emulating the operation via tree-vect-generic, we don't want
4589 to use it. Only the first round of the reduction is likely
4590 to still be profitable via emulation. */
4591 /* ??? It might be better to emit a reduction tree code here, so that
4592 tree-vect-generic can expand the first round via bit tricks. */
4593 if (!VECTOR_MODE_P (mode
))
4594 reduce_with_shift
= false;
4597 optab optab
= optab_for_tree_code (code
, vectype
, optab_default
);
4598 if (optab_handler (optab
, mode
) == CODE_FOR_nothing
)
4599 reduce_with_shift
= false;
4602 if (reduce_with_shift
&& !slp_reduc
)
4604 int nelements
= vec_size_in_bits
/ element_bitsize
;
4605 unsigned char *sel
= XALLOCAVEC (unsigned char, nelements
);
4609 tree zero_vec
= build_zero_cst (vectype
);
4611 for (offset = nelements/2; offset >= 1; offset/=2)
4613 Create: va' = vec_shift <va, offset>
4614 Create: va = vop <va, va'>
4619 if (dump_enabled_p ())
4620 dump_printf_loc (MSG_NOTE
, vect_location
,
4621 "Reduce using vector shifts\n");
4623 vec_dest
= vect_create_destination_var (scalar_dest
, vectype
);
4624 new_temp
= new_phi_result
;
4625 for (elt_offset
= nelements
/ 2;
4629 calc_vec_perm_mask_for_shift (mode
, elt_offset
, sel
);
4630 tree mask
= vect_gen_perm_mask_any (vectype
, sel
);
4631 epilog_stmt
= gimple_build_assign (vec_dest
, VEC_PERM_EXPR
,
4632 new_temp
, zero_vec
, mask
);
4633 new_name
= make_ssa_name (vec_dest
, epilog_stmt
);
4634 gimple_assign_set_lhs (epilog_stmt
, new_name
);
4635 gsi_insert_before (&exit_gsi
, epilog_stmt
, GSI_SAME_STMT
);
4637 epilog_stmt
= gimple_build_assign (vec_dest
, code
, new_name
,
4639 new_temp
= make_ssa_name (vec_dest
, epilog_stmt
);
4640 gimple_assign_set_lhs (epilog_stmt
, new_temp
);
4641 gsi_insert_before (&exit_gsi
, epilog_stmt
, GSI_SAME_STMT
);
4644 /* 2.4 Extract the final scalar result. Create:
4645 s_out3 = extract_field <v_out2, bitpos> */
4647 if (dump_enabled_p ())
4648 dump_printf_loc (MSG_NOTE
, vect_location
,
4649 "extract scalar result\n");
4651 rhs
= build3 (BIT_FIELD_REF
, scalar_type
, new_temp
,
4652 bitsize
, bitsize_zero_node
);
4653 epilog_stmt
= gimple_build_assign (new_scalar_dest
, rhs
);
4654 new_temp
= make_ssa_name (new_scalar_dest
, epilog_stmt
);
4655 gimple_assign_set_lhs (epilog_stmt
, new_temp
);
4656 gsi_insert_before (&exit_gsi
, epilog_stmt
, GSI_SAME_STMT
);
4657 scalar_results
.safe_push (new_temp
);
4662 s = extract_field <v_out2, 0>
4663 for (offset = element_size;
4664 offset < vector_size;
4665 offset += element_size;)
4667 Create: s' = extract_field <v_out2, offset>
4668 Create: s = op <s, s'> // For non SLP cases
4671 if (dump_enabled_p ())
4672 dump_printf_loc (MSG_NOTE
, vect_location
,
4673 "Reduce using scalar code.\n");
4675 vec_size_in_bits
= tree_to_uhwi (TYPE_SIZE (vectype
));
4676 FOR_EACH_VEC_ELT (new_phis
, i
, new_phi
)
4679 if (gimple_code (new_phi
) == GIMPLE_PHI
)
4680 vec_temp
= PHI_RESULT (new_phi
);
4682 vec_temp
= gimple_assign_lhs (new_phi
);
4683 tree rhs
= build3 (BIT_FIELD_REF
, scalar_type
, vec_temp
, bitsize
,
4685 epilog_stmt
= gimple_build_assign (new_scalar_dest
, rhs
);
4686 new_temp
= make_ssa_name (new_scalar_dest
, epilog_stmt
);
4687 gimple_assign_set_lhs (epilog_stmt
, new_temp
);
4688 gsi_insert_before (&exit_gsi
, epilog_stmt
, GSI_SAME_STMT
);
4690 /* In SLP we don't need to apply reduction operation, so we just
4691 collect s' values in SCALAR_RESULTS. */
4693 scalar_results
.safe_push (new_temp
);
4695 for (bit_offset
= element_bitsize
;
4696 bit_offset
< vec_size_in_bits
;
4697 bit_offset
+= element_bitsize
)
4699 tree bitpos
= bitsize_int (bit_offset
);
4700 tree rhs
= build3 (BIT_FIELD_REF
, scalar_type
, vec_temp
,
4703 epilog_stmt
= gimple_build_assign (new_scalar_dest
, rhs
);
4704 new_name
= make_ssa_name (new_scalar_dest
, epilog_stmt
);
4705 gimple_assign_set_lhs (epilog_stmt
, new_name
);
4706 gsi_insert_before (&exit_gsi
, epilog_stmt
, GSI_SAME_STMT
);
4710 /* In SLP we don't need to apply reduction operation, so
4711 we just collect s' values in SCALAR_RESULTS. */
4712 new_temp
= new_name
;
4713 scalar_results
.safe_push (new_name
);
4717 epilog_stmt
= gimple_build_assign (new_scalar_dest
, code
,
4718 new_name
, new_temp
);
4719 new_temp
= make_ssa_name (new_scalar_dest
, epilog_stmt
);
4720 gimple_assign_set_lhs (epilog_stmt
, new_temp
);
4721 gsi_insert_before (&exit_gsi
, epilog_stmt
, GSI_SAME_STMT
);
4726 /* The only case where we need to reduce scalar results in SLP, is
4727 unrolling. If the size of SCALAR_RESULTS is greater than
4728 GROUP_SIZE, we reduce them combining elements modulo
4732 tree res
, first_res
, new_res
;
4735 /* Reduce multiple scalar results in case of SLP unrolling. */
4736 for (j
= group_size
; scalar_results
.iterate (j
, &res
);
4739 first_res
= scalar_results
[j
% group_size
];
4740 new_stmt
= gimple_build_assign (new_scalar_dest
, code
,
4742 new_res
= make_ssa_name (new_scalar_dest
, new_stmt
);
4743 gimple_assign_set_lhs (new_stmt
, new_res
);
4744 gsi_insert_before (&exit_gsi
, new_stmt
, GSI_SAME_STMT
);
4745 scalar_results
[j
% group_size
] = new_res
;
4749 /* Not SLP - we have one scalar to keep in SCALAR_RESULTS. */
4750 scalar_results
.safe_push (new_temp
);
4754 vect_finalize_reduction
:
4759 /* 2.5 Adjust the final result by the initial value of the reduction
4760 variable. (When such adjustment is not needed, then
4761 'adjustment_def' is zero). For example, if code is PLUS we create:
4762 new_temp = loop_exit_def + adjustment_def */
4766 gcc_assert (!slp_reduc
);
4767 if (nested_in_vect_loop
)
4769 new_phi
= new_phis
[0];
4770 gcc_assert (TREE_CODE (TREE_TYPE (adjustment_def
)) == VECTOR_TYPE
);
4771 expr
= build2 (code
, vectype
, PHI_RESULT (new_phi
), adjustment_def
);
4772 new_dest
= vect_create_destination_var (scalar_dest
, vectype
);
4776 new_temp
= scalar_results
[0];
4777 gcc_assert (TREE_CODE (TREE_TYPE (adjustment_def
)) != VECTOR_TYPE
);
4778 expr
= build2 (code
, scalar_type
, new_temp
, adjustment_def
);
4779 new_dest
= vect_create_destination_var (scalar_dest
, scalar_type
);
4782 epilog_stmt
= gimple_build_assign (new_dest
, expr
);
4783 new_temp
= make_ssa_name (new_dest
, epilog_stmt
);
4784 gimple_assign_set_lhs (epilog_stmt
, new_temp
);
4785 gsi_insert_before (&exit_gsi
, epilog_stmt
, GSI_SAME_STMT
);
4786 if (nested_in_vect_loop
)
4788 set_vinfo_for_stmt (epilog_stmt
,
4789 new_stmt_vec_info (epilog_stmt
, loop_vinfo
));
4790 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (epilog_stmt
)) =
4791 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (new_phi
));
4794 scalar_results
.quick_push (new_temp
);
4796 scalar_results
[0] = new_temp
;
4799 scalar_results
[0] = new_temp
;
4801 new_phis
[0] = epilog_stmt
;
4804 /* 2.6 Handle the loop-exit phis. Replace the uses of scalar loop-exit
4805 phis with new adjusted scalar results, i.e., replace use <s_out0>
4810 s_out0 = phi <s_loop> # (scalar) EXIT_PHI
4811 v_out1 = phi <VECT_DEF> # NEW_EXIT_PHI
4812 v_out2 = reduce <v_out1>
4813 s_out3 = extract_field <v_out2, 0>
4814 s_out4 = adjust_result <s_out3>
4821 s_out0 = phi <s_loop> # (scalar) EXIT_PHI
4822 v_out1 = phi <VECT_DEF> # NEW_EXIT_PHI
4823 v_out2 = reduce <v_out1>
4824 s_out3 = extract_field <v_out2, 0>
4825 s_out4 = adjust_result <s_out3>
4830 /* In SLP reduction chain we reduce vector results into one vector if
4831 necessary, hence we set here GROUP_SIZE to 1. SCALAR_DEST is the LHS of
4832 the last stmt in the reduction chain, since we are looking for the loop
4834 if (GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt
)))
4836 gimple
*dest_stmt
= SLP_TREE_SCALAR_STMTS (slp_node
)[group_size
- 1];
4837 /* Handle reduction patterns. */
4838 if (STMT_VINFO_RELATED_STMT (vinfo_for_stmt (dest_stmt
)))
4839 dest_stmt
= STMT_VINFO_RELATED_STMT (vinfo_for_stmt (dest_stmt
));
4841 scalar_dest
= gimple_assign_lhs (dest_stmt
);
4845 /* In SLP we may have several statements in NEW_PHIS and REDUCTION_PHIS (in
4846 case that GROUP_SIZE is greater than vectorization factor). Therefore, we
4847 need to match SCALAR_RESULTS with corresponding statements. The first
4848 (GROUP_SIZE / number of new vector stmts) scalar results correspond to
4849 the first vector stmt, etc.
4850 (RATIO is equal to (GROUP_SIZE / number of new vector stmts)). */
4851 if (group_size
> new_phis
.length ())
4853 ratio
= group_size
/ new_phis
.length ();
4854 gcc_assert (!(group_size
% new_phis
.length ()));
4859 for (k
= 0; k
< group_size
; k
++)
4863 epilog_stmt
= new_phis
[k
/ ratio
];
4864 reduction_phi
= reduction_phis
[k
/ ratio
];
4866 inner_phi
= inner_phis
[k
/ ratio
];
4871 gimple
*current_stmt
= SLP_TREE_SCALAR_STMTS (slp_node
)[k
];
4873 orig_stmt
= STMT_VINFO_RELATED_STMT (vinfo_for_stmt (current_stmt
));
4874 /* SLP statements can't participate in patterns. */
4875 gcc_assert (!orig_stmt
);
4876 scalar_dest
= gimple_assign_lhs (current_stmt
);
4880 /* Find the loop-closed-use at the loop exit of the original scalar
4881 result. (The reduction result is expected to have two immediate uses -
4882 one at the latch block, and one at the loop exit). */
4883 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, scalar_dest
)
4884 if (!flow_bb_inside_loop_p (loop
, gimple_bb (USE_STMT (use_p
)))
4885 && !is_gimple_debug (USE_STMT (use_p
)))
4886 phis
.safe_push (USE_STMT (use_p
));
4888 /* While we expect to have found an exit_phi because of loop-closed-ssa
4889 form we can end up without one if the scalar cycle is dead. */
4891 FOR_EACH_VEC_ELT (phis
, i
, exit_phi
)
4895 stmt_vec_info exit_phi_vinfo
= vinfo_for_stmt (exit_phi
);
4898 /* FORNOW. Currently not supporting the case that an inner-loop
4899 reduction is not used in the outer-loop (but only outside the
4900 outer-loop), unless it is double reduction. */
4901 gcc_assert ((STMT_VINFO_RELEVANT_P (exit_phi_vinfo
)
4902 && !STMT_VINFO_LIVE_P (exit_phi_vinfo
))
4906 STMT_VINFO_VEC_STMT (exit_phi_vinfo
) = inner_phi
;
4908 STMT_VINFO_VEC_STMT (exit_phi_vinfo
) = epilog_stmt
;
4910 || STMT_VINFO_DEF_TYPE (exit_phi_vinfo
)
4911 != vect_double_reduction_def
)
4914 /* Handle double reduction:
4916 stmt1: s1 = phi <s0, s2> - double reduction phi (outer loop)
4917 stmt2: s3 = phi <s1, s4> - (regular) reduc phi (inner loop)
4918 stmt3: s4 = use (s3) - (regular) reduc stmt (inner loop)
4919 stmt4: s2 = phi <s4> - double reduction stmt (outer loop)
4921 At that point the regular reduction (stmt2 and stmt3) is
4922 already vectorized, as well as the exit phi node, stmt4.
4923 Here we vectorize the phi node of double reduction, stmt1, and
4924 update all relevant statements. */
4926 /* Go through all the uses of s2 to find double reduction phi
4927 node, i.e., stmt1 above. */
4928 orig_name
= PHI_RESULT (exit_phi
);
4929 FOR_EACH_IMM_USE_STMT (use_stmt
, imm_iter
, orig_name
)
4931 stmt_vec_info use_stmt_vinfo
;
4932 stmt_vec_info new_phi_vinfo
;
4933 tree vect_phi_init
, preheader_arg
, vect_phi_res
, init_def
;
4934 basic_block bb
= gimple_bb (use_stmt
);
4937 /* Check that USE_STMT is really double reduction phi
4939 if (gimple_code (use_stmt
) != GIMPLE_PHI
4940 || gimple_phi_num_args (use_stmt
) != 2
4941 || bb
->loop_father
!= outer_loop
)
4943 use_stmt_vinfo
= vinfo_for_stmt (use_stmt
);
4945 || STMT_VINFO_DEF_TYPE (use_stmt_vinfo
)
4946 != vect_double_reduction_def
)
4949 /* Create vector phi node for double reduction:
4950 vs1 = phi <vs0, vs2>
4951 vs1 was created previously in this function by a call to
4952 vect_get_vec_def_for_operand and is stored in
4954 vs2 is defined by INNER_PHI, the vectorized EXIT_PHI;
4955 vs0 is created here. */
4957 /* Create vector phi node. */
4958 vect_phi
= create_phi_node (vec_initial_def
, bb
);
4959 new_phi_vinfo
= new_stmt_vec_info (vect_phi
,
4960 loop_vec_info_for_loop (outer_loop
));
4961 set_vinfo_for_stmt (vect_phi
, new_phi_vinfo
);
4963 /* Create vs0 - initial def of the double reduction phi. */
4964 preheader_arg
= PHI_ARG_DEF_FROM_EDGE (use_stmt
,
4965 loop_preheader_edge (outer_loop
));
4966 init_def
= get_initial_def_for_reduction (stmt
,
4967 preheader_arg
, NULL
);
4968 vect_phi_init
= vect_init_vector (use_stmt
, init_def
,
4971 /* Update phi node arguments with vs0 and vs2. */
4972 add_phi_arg (vect_phi
, vect_phi_init
,
4973 loop_preheader_edge (outer_loop
),
4975 add_phi_arg (vect_phi
, PHI_RESULT (inner_phi
),
4976 loop_latch_edge (outer_loop
), UNKNOWN_LOCATION
);
4977 if (dump_enabled_p ())
4979 dump_printf_loc (MSG_NOTE
, vect_location
,
4980 "created double reduction phi node: ");
4981 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
, vect_phi
, 0);
4984 vect_phi_res
= PHI_RESULT (vect_phi
);
4986 /* Replace the use, i.e., set the correct vs1 in the regular
4987 reduction phi node. FORNOW, NCOPIES is always 1, so the
4988 loop is redundant. */
4989 use
= reduction_phi
;
4990 for (j
= 0; j
< ncopies
; j
++)
4992 edge pr_edge
= loop_preheader_edge (loop
);
4993 SET_PHI_ARG_DEF (use
, pr_edge
->dest_idx
, vect_phi_res
);
4994 use
= STMT_VINFO_RELATED_STMT (vinfo_for_stmt (use
));
5001 if (nested_in_vect_loop
)
5010 /* Find the loop-closed-use at the loop exit of the original scalar
5011 result. (The reduction result is expected to have two immediate uses,
5012 one at the latch block, and one at the loop exit). For double
5013 reductions we are looking for exit phis of the outer loop. */
5014 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, scalar_dest
)
5016 if (!flow_bb_inside_loop_p (loop
, gimple_bb (USE_STMT (use_p
))))
5018 if (!is_gimple_debug (USE_STMT (use_p
)))
5019 phis
.safe_push (USE_STMT (use_p
));
5023 if (double_reduc
&& gimple_code (USE_STMT (use_p
)) == GIMPLE_PHI
)
5025 tree phi_res
= PHI_RESULT (USE_STMT (use_p
));
5027 FOR_EACH_IMM_USE_FAST (phi_use_p
, phi_imm_iter
, phi_res
)
5029 if (!flow_bb_inside_loop_p (loop
,
5030 gimple_bb (USE_STMT (phi_use_p
)))
5031 && !is_gimple_debug (USE_STMT (phi_use_p
)))
5032 phis
.safe_push (USE_STMT (phi_use_p
));
5038 FOR_EACH_VEC_ELT (phis
, i
, exit_phi
)
5040 /* Replace the uses: */
5041 orig_name
= PHI_RESULT (exit_phi
);
5042 scalar_result
= scalar_results
[k
];
5043 FOR_EACH_IMM_USE_STMT (use_stmt
, imm_iter
, orig_name
)
5044 FOR_EACH_IMM_USE_ON_STMT (use_p
, imm_iter
)
5045 SET_USE (use_p
, scalar_result
);
5053 /* Function is_nonwrapping_integer_induction.
5055 Check if STMT (which is part of loop LOOP) both increments and
5056 does not cause overflow. */
5059 is_nonwrapping_integer_induction (gimple
*stmt
, struct loop
*loop
)
5061 stmt_vec_info stmt_vinfo
= vinfo_for_stmt (stmt
);
5062 tree base
= STMT_VINFO_LOOP_PHI_EVOLUTION_BASE_UNCHANGED (stmt_vinfo
);
5063 tree step
= STMT_VINFO_LOOP_PHI_EVOLUTION_PART (stmt_vinfo
);
5064 tree lhs_type
= TREE_TYPE (gimple_phi_result (stmt
));
5065 widest_int ni
, max_loop_value
, lhs_max
;
5066 bool overflow
= false;
5068 /* Make sure the loop is integer based. */
5069 if (TREE_CODE (base
) != INTEGER_CST
5070 || TREE_CODE (step
) != INTEGER_CST
)
5073 /* Check that the induction increments. */
5074 if (tree_int_cst_sgn (step
) == -1)
5077 /* Check that the max size of the loop will not wrap. */
5079 if (TYPE_OVERFLOW_UNDEFINED (lhs_type
))
5082 if (! max_stmt_executions (loop
, &ni
))
5085 max_loop_value
= wi::mul (wi::to_widest (step
), ni
, TYPE_SIGN (lhs_type
),
5090 max_loop_value
= wi::add (wi::to_widest (base
), max_loop_value
,
5091 TYPE_SIGN (lhs_type
), &overflow
);
5095 return (wi::min_precision (max_loop_value
, TYPE_SIGN (lhs_type
))
5096 <= TYPE_PRECISION (lhs_type
));
5099 /* Function vectorizable_reduction.
5101 Check if STMT performs a reduction operation that can be vectorized.
5102 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
5103 stmt to replace it, put it in VEC_STMT, and insert it at GSI.
5104 Return FALSE if not a vectorizable STMT, TRUE otherwise.
5106 This function also handles reduction idioms (patterns) that have been
5107 recognized in advance during vect_pattern_recog. In this case, STMT may be
5109 X = pattern_expr (arg0, arg1, ..., X)
5110 and it's STMT_VINFO_RELATED_STMT points to the last stmt in the original
5111 sequence that had been detected and replaced by the pattern-stmt (STMT).
5113 This function also handles reduction of condition expressions, for example:
5114 for (int i = 0; i < N; i++)
5117 This is handled by vectorising the loop and creating an additional vector
5118 containing the loop indexes for which "a[i] < value" was true. In the
5119 function epilogue this is reduced to a single max value and then used to
5120 index into the vector of results.
5122 In some cases of reduction patterns, the type of the reduction variable X is
5123 different than the type of the other arguments of STMT.
5124 In such cases, the vectype that is used when transforming STMT into a vector
5125 stmt is different than the vectype that is used to determine the
5126 vectorization factor, because it consists of a different number of elements
5127 than the actual number of elements that are being operated upon in parallel.
5129 For example, consider an accumulation of shorts into an int accumulator.
5130 On some targets it's possible to vectorize this pattern operating on 8
5131 shorts at a time (hence, the vectype for purposes of determining the
5132 vectorization factor should be V8HI); on the other hand, the vectype that
5133 is used to create the vector form is actually V4SI (the type of the result).
5135 Upon entry to this function, STMT_VINFO_VECTYPE records the vectype that
5136 indicates what is the actual level of parallelism (V8HI in the example), so
5137 that the right vectorization factor would be derived. This vectype
5138 corresponds to the type of arguments to the reduction stmt, and should *NOT*
5139 be used to create the vectorized stmt. The right vectype for the vectorized
5140 stmt is obtained from the type of the result X:
5141 get_vectype_for_scalar_type (TREE_TYPE (X))
5143 This means that, contrary to "regular" reductions (or "regular" stmts in
5144 general), the following equation:
5145 STMT_VINFO_VECTYPE == get_vectype_for_scalar_type (TREE_TYPE (X))
5146 does *NOT* necessarily hold for reduction patterns. */
5149 vectorizable_reduction (gimple
*stmt
, gimple_stmt_iterator
*gsi
,
5150 gimple
**vec_stmt
, slp_tree slp_node
)
5154 tree loop_vec_def0
= NULL_TREE
, loop_vec_def1
= NULL_TREE
;
5155 stmt_vec_info stmt_info
= vinfo_for_stmt (stmt
);
5156 tree vectype_out
= STMT_VINFO_VECTYPE (stmt_info
);
5157 tree vectype_in
= NULL_TREE
;
5158 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_info
);
5159 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
5160 enum tree_code code
, orig_code
, epilog_reduc_code
;
5161 machine_mode vec_mode
;
5163 optab optab
, reduc_optab
;
5164 tree new_temp
= NULL_TREE
;
5166 enum vect_def_type dt
, cond_reduc_dt
= vect_unknown_def_type
;
5167 gphi
*new_phi
= NULL
;
5171 stmt_vec_info orig_stmt_info
;
5172 tree expr
= NULL_TREE
;
5176 stmt_vec_info prev_stmt_info
, prev_phi_info
;
5177 bool single_defuse_cycle
= false;
5178 tree reduc_def
= NULL_TREE
;
5179 gimple
*new_stmt
= NULL
;
5182 bool nested_cycle
= false, found_nested_cycle_def
= false;
5183 gimple
*reduc_def_stmt
= NULL
;
5184 bool double_reduc
= false;
5186 struct loop
* def_stmt_loop
, *outer_loop
= NULL
;
5188 gimple
*def_arg_stmt
;
5189 auto_vec
<tree
> vec_oprnds0
;
5190 auto_vec
<tree
> vec_oprnds1
;
5191 auto_vec
<tree
> vect_defs
;
5192 auto_vec
<gimple
*> phis
;
5194 tree def0
, def1
, tem
, op1
= NULL_TREE
;
5195 bool first_p
= true;
5196 tree cr_index_scalar_type
= NULL_TREE
, cr_index_vector_type
= NULL_TREE
;
5197 tree cond_reduc_val
= NULL_TREE
;
5199 /* In case of reduction chain we switch to the first stmt in the chain, but
5200 we don't update STMT_INFO, since only the last stmt is marked as reduction
5201 and has reduction properties. */
5202 if (GROUP_FIRST_ELEMENT (stmt_info
)
5203 && GROUP_FIRST_ELEMENT (stmt_info
) != stmt
)
5205 stmt
= GROUP_FIRST_ELEMENT (stmt_info
);
5209 if (nested_in_vect_loop_p (loop
, stmt
))
5213 nested_cycle
= true;
5216 /* 1. Is vectorizable reduction? */
5217 /* Not supportable if the reduction variable is used in the loop, unless
5218 it's a reduction chain. */
5219 if (STMT_VINFO_RELEVANT (stmt_info
) > vect_used_in_outer
5220 && !GROUP_FIRST_ELEMENT (stmt_info
))
5223 /* Reductions that are not used even in an enclosing outer-loop,
5224 are expected to be "live" (used out of the loop). */
5225 if (STMT_VINFO_RELEVANT (stmt_info
) == vect_unused_in_scope
5226 && !STMT_VINFO_LIVE_P (stmt_info
))
5229 /* Make sure it was already recognized as a reduction computation. */
5230 if (STMT_VINFO_DEF_TYPE (vinfo_for_stmt (stmt
)) != vect_reduction_def
5231 && STMT_VINFO_DEF_TYPE (vinfo_for_stmt (stmt
)) != vect_nested_cycle
)
5234 /* 2. Has this been recognized as a reduction pattern?
5236 Check if STMT represents a pattern that has been recognized
5237 in earlier analysis stages. For stmts that represent a pattern,
5238 the STMT_VINFO_RELATED_STMT field records the last stmt in
5239 the original sequence that constitutes the pattern. */
5241 orig_stmt
= STMT_VINFO_RELATED_STMT (vinfo_for_stmt (stmt
));
5244 orig_stmt_info
= vinfo_for_stmt (orig_stmt
);
5245 gcc_assert (STMT_VINFO_IN_PATTERN_P (orig_stmt_info
));
5246 gcc_assert (!STMT_VINFO_IN_PATTERN_P (stmt_info
));
5249 /* 3. Check the operands of the operation. The first operands are defined
5250 inside the loop body. The last operand is the reduction variable,
5251 which is defined by the loop-header-phi. */
5253 gcc_assert (is_gimple_assign (stmt
));
5256 switch (get_gimple_rhs_class (gimple_assign_rhs_code (stmt
)))
5258 case GIMPLE_SINGLE_RHS
:
5259 op_type
= TREE_OPERAND_LENGTH (gimple_assign_rhs1 (stmt
));
5260 if (op_type
== ternary_op
)
5262 tree rhs
= gimple_assign_rhs1 (stmt
);
5263 ops
[0] = TREE_OPERAND (rhs
, 0);
5264 ops
[1] = TREE_OPERAND (rhs
, 1);
5265 ops
[2] = TREE_OPERAND (rhs
, 2);
5266 code
= TREE_CODE (rhs
);
5272 case GIMPLE_BINARY_RHS
:
5273 code
= gimple_assign_rhs_code (stmt
);
5274 op_type
= TREE_CODE_LENGTH (code
);
5275 gcc_assert (op_type
== binary_op
);
5276 ops
[0] = gimple_assign_rhs1 (stmt
);
5277 ops
[1] = gimple_assign_rhs2 (stmt
);
5280 case GIMPLE_TERNARY_RHS
:
5281 code
= gimple_assign_rhs_code (stmt
);
5282 op_type
= TREE_CODE_LENGTH (code
);
5283 gcc_assert (op_type
== ternary_op
);
5284 ops
[0] = gimple_assign_rhs1 (stmt
);
5285 ops
[1] = gimple_assign_rhs2 (stmt
);
5286 ops
[2] = gimple_assign_rhs3 (stmt
);
5289 case GIMPLE_UNARY_RHS
:
5295 /* The default is that the reduction variable is the last in statement. */
5296 int reduc_index
= op_type
- 1;
5297 if (code
== MINUS_EXPR
)
5300 if (code
== COND_EXPR
&& slp_node
)
5303 scalar_dest
= gimple_assign_lhs (stmt
);
5304 scalar_type
= TREE_TYPE (scalar_dest
);
5305 if (!POINTER_TYPE_P (scalar_type
) && !INTEGRAL_TYPE_P (scalar_type
)
5306 && !SCALAR_FLOAT_TYPE_P (scalar_type
))
5309 /* Do not try to vectorize bit-precision reductions. */
5310 if ((TYPE_PRECISION (scalar_type
)
5311 != GET_MODE_PRECISION (TYPE_MODE (scalar_type
))))
5314 /* All uses but the last are expected to be defined in the loop.
5315 The last use is the reduction variable. In case of nested cycle this
5316 assumption is not true: we use reduc_index to record the index of the
5317 reduction variable. */
5318 for (i
= 0; i
< op_type
; i
++)
5320 if (i
== reduc_index
)
5323 /* The condition of COND_EXPR is checked in vectorizable_condition(). */
5324 if (i
== 0 && code
== COND_EXPR
)
5327 is_simple_use
= vect_is_simple_use (ops
[i
], loop_vinfo
,
5328 &def_stmt
, &dt
, &tem
);
5331 gcc_assert (is_simple_use
);
5333 if (dt
!= vect_internal_def
5334 && dt
!= vect_external_def
5335 && dt
!= vect_constant_def
5336 && dt
!= vect_induction_def
5337 && !(dt
== vect_nested_cycle
&& nested_cycle
))
5340 if (dt
== vect_nested_cycle
)
5342 found_nested_cycle_def
= true;
5343 reduc_def_stmt
= def_stmt
;
5347 if (i
== 1 && code
== COND_EXPR
)
5349 /* Record how value of COND_EXPR is defined. */
5350 if (dt
== vect_constant_def
)
5353 cond_reduc_val
= ops
[i
];
5355 if (dt
== vect_induction_def
&& def_stmt
!= NULL
5356 && is_nonwrapping_integer_induction (def_stmt
, loop
))
5361 is_simple_use
= vect_is_simple_use (ops
[reduc_index
], loop_vinfo
,
5362 &def_stmt
, &dt
, &tem
);
5365 gcc_assert (is_simple_use
);
5366 if (!found_nested_cycle_def
)
5367 reduc_def_stmt
= def_stmt
;
5369 if (reduc_def_stmt
&& gimple_code (reduc_def_stmt
) != GIMPLE_PHI
)
5372 if (!(dt
== vect_reduction_def
5373 || dt
== vect_nested_cycle
5374 || ((dt
== vect_internal_def
|| dt
== vect_external_def
5375 || dt
== vect_constant_def
|| dt
== vect_induction_def
)
5376 && nested_cycle
&& found_nested_cycle_def
)))
5378 /* For pattern recognized stmts, orig_stmt might be a reduction,
5379 but some helper statements for the pattern might not, or
5380 might be COND_EXPRs with reduction uses in the condition. */
5381 gcc_assert (orig_stmt
);
5385 stmt_vec_info reduc_def_info
= vinfo_for_stmt (reduc_def_stmt
);
5386 enum vect_reduction_type v_reduc_type
5387 = STMT_VINFO_REDUC_TYPE (reduc_def_info
);
5388 gimple
*tmp
= STMT_VINFO_REDUC_DEF (reduc_def_info
);
5390 STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
) = v_reduc_type
;
5391 /* If we have a condition reduction, see if we can simplify it further. */
5392 if (v_reduc_type
== COND_REDUCTION
)
5394 if (cond_reduc_dt
== vect_induction_def
)
5396 if (dump_enabled_p ())
5397 dump_printf_loc (MSG_NOTE
, vect_location
,
5398 "condition expression based on "
5399 "integer induction.\n");
5400 STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
)
5401 = INTEGER_INDUC_COND_REDUCTION
;
5404 /* Loop peeling modifies initial value of reduction PHI, which
5405 makes the reduction stmt to be transformed different to the
5406 original stmt analyzed. We need to record reduction code for
5407 CONST_COND_REDUCTION type reduction at analyzing stage, thus
5408 it can be used directly at transform stage. */
5409 if (STMT_VINFO_VEC_CONST_COND_REDUC_CODE (stmt_info
) == MAX_EXPR
5410 || STMT_VINFO_VEC_CONST_COND_REDUC_CODE (stmt_info
) == MIN_EXPR
)
5412 /* Also set the reduction type to CONST_COND_REDUCTION. */
5413 gcc_assert (cond_reduc_dt
== vect_constant_def
);
5414 STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
) = CONST_COND_REDUCTION
;
5416 else if (cond_reduc_dt
== vect_constant_def
)
5418 enum vect_def_type cond_initial_dt
;
5419 gimple
*def_stmt
= SSA_NAME_DEF_STMT (ops
[reduc_index
]);
5420 tree cond_initial_val
5421 = PHI_ARG_DEF_FROM_EDGE (def_stmt
, loop_preheader_edge (loop
));
5423 gcc_assert (cond_reduc_val
!= NULL_TREE
);
5424 vect_is_simple_use (cond_initial_val
, loop_vinfo
,
5425 &def_stmt
, &cond_initial_dt
);
5426 if (cond_initial_dt
== vect_constant_def
5427 && types_compatible_p (TREE_TYPE (cond_initial_val
),
5428 TREE_TYPE (cond_reduc_val
)))
5430 tree e
= fold_build2 (LE_EXPR
, boolean_type_node
,
5431 cond_initial_val
, cond_reduc_val
);
5432 if (e
&& (integer_onep (e
) || integer_zerop (e
)))
5434 if (dump_enabled_p ())
5435 dump_printf_loc (MSG_NOTE
, vect_location
,
5436 "condition expression based on "
5437 "compile time constant.\n");
5438 /* Record reduction code at analysis stage. */
5439 STMT_VINFO_VEC_CONST_COND_REDUC_CODE (stmt_info
)
5440 = integer_onep (e
) ? MAX_EXPR
: MIN_EXPR
;
5441 STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
)
5442 = CONST_COND_REDUCTION
;
5449 gcc_assert (tmp
== orig_stmt
5450 || GROUP_FIRST_ELEMENT (vinfo_for_stmt (tmp
)) == orig_stmt
);
5452 /* We changed STMT to be the first stmt in reduction chain, hence we
5453 check that in this case the first element in the chain is STMT. */
5454 gcc_assert (stmt
== tmp
5455 || GROUP_FIRST_ELEMENT (vinfo_for_stmt (tmp
)) == stmt
);
5457 if (STMT_VINFO_LIVE_P (vinfo_for_stmt (reduc_def_stmt
)))
5463 ncopies
= (LOOP_VINFO_VECT_FACTOR (loop_vinfo
)
5464 / TYPE_VECTOR_SUBPARTS (vectype_in
));
5466 gcc_assert (ncopies
>= 1);
5468 vec_mode
= TYPE_MODE (vectype_in
);
5470 if (code
== COND_EXPR
)
5472 /* Only call during the analysis stage, otherwise we'll lose
5474 if (!vec_stmt
&& !vectorizable_condition (stmt
, gsi
, NULL
,
5475 ops
[reduc_index
], 0, NULL
))
5477 if (dump_enabled_p ())
5478 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
5479 "unsupported condition in reduction\n");
5485 /* 4. Supportable by target? */
5487 if (code
== LSHIFT_EXPR
|| code
== RSHIFT_EXPR
5488 || code
== LROTATE_EXPR
|| code
== RROTATE_EXPR
)
5490 /* Shifts and rotates are only supported by vectorizable_shifts,
5491 not vectorizable_reduction. */
5492 if (dump_enabled_p ())
5493 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
5494 "unsupported shift or rotation.\n");
5498 /* 4.1. check support for the operation in the loop */
5499 optab
= optab_for_tree_code (code
, vectype_in
, optab_default
);
5502 if (dump_enabled_p ())
5503 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
5509 if (optab_handler (optab
, vec_mode
) == CODE_FOR_nothing
)
5511 if (dump_enabled_p ())
5512 dump_printf (MSG_NOTE
, "op not supported by target.\n");
5514 if (GET_MODE_SIZE (vec_mode
) != UNITS_PER_WORD
5515 || LOOP_VINFO_VECT_FACTOR (loop_vinfo
)
5516 < vect_min_worthwhile_factor (code
))
5519 if (dump_enabled_p ())
5520 dump_printf (MSG_NOTE
, "proceeding using word mode.\n");
5523 /* Worthwhile without SIMD support? */
5524 if (!VECTOR_MODE_P (TYPE_MODE (vectype_in
))
5525 && LOOP_VINFO_VECT_FACTOR (loop_vinfo
)
5526 < vect_min_worthwhile_factor (code
))
5528 if (dump_enabled_p ())
5529 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
5530 "not worthwhile without SIMD support.\n");
5536 /* 4.2. Check support for the epilog operation.
5538 If STMT represents a reduction pattern, then the type of the
5539 reduction variable may be different than the type of the rest
5540 of the arguments. For example, consider the case of accumulation
5541 of shorts into an int accumulator; The original code:
5542 S1: int_a = (int) short_a;
5543 orig_stmt-> S2: int_acc = plus <int_a ,int_acc>;
5546 STMT: int_acc = widen_sum <short_a, int_acc>
5549 1. The tree-code that is used to create the vector operation in the
5550 epilog code (that reduces the partial results) is not the
5551 tree-code of STMT, but is rather the tree-code of the original
5552 stmt from the pattern that STMT is replacing. I.e, in the example
5553 above we want to use 'widen_sum' in the loop, but 'plus' in the
5555 2. The type (mode) we use to check available target support
5556 for the vector operation to be created in the *epilog*, is
5557 determined by the type of the reduction variable (in the example
5558 above we'd check this: optab_handler (plus_optab, vect_int_mode])).
5559 However the type (mode) we use to check available target support
5560 for the vector operation to be created *inside the loop*, is
5561 determined by the type of the other arguments to STMT (in the
5562 example we'd check this: optab_handler (widen_sum_optab,
5565 This is contrary to "regular" reductions, in which the types of all
5566 the arguments are the same as the type of the reduction variable.
5567 For "regular" reductions we can therefore use the same vector type
5568 (and also the same tree-code) when generating the epilog code and
5569 when generating the code inside the loop. */
5573 /* This is a reduction pattern: get the vectype from the type of the
5574 reduction variable, and get the tree-code from orig_stmt. */
5575 gcc_assert (STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
)
5576 == TREE_CODE_REDUCTION
);
5577 orig_code
= gimple_assign_rhs_code (orig_stmt
);
5578 gcc_assert (vectype_out
);
5579 vec_mode
= TYPE_MODE (vectype_out
);
5583 /* Regular reduction: use the same vectype and tree-code as used for
5584 the vector code inside the loop can be used for the epilog code. */
5587 if (code
== MINUS_EXPR
)
5588 orig_code
= PLUS_EXPR
;
5590 /* For simple condition reductions, replace with the actual expression
5591 we want to base our reduction around. */
5592 if (STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
) == CONST_COND_REDUCTION
)
5594 orig_code
= STMT_VINFO_VEC_CONST_COND_REDUC_CODE (stmt_info
);
5595 gcc_assert (orig_code
== MAX_EXPR
|| orig_code
== MIN_EXPR
);
5597 else if (STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
)
5598 == INTEGER_INDUC_COND_REDUCTION
)
5599 orig_code
= MAX_EXPR
;
5604 def_bb
= gimple_bb (reduc_def_stmt
);
5605 def_stmt_loop
= def_bb
->loop_father
;
5606 def_arg
= PHI_ARG_DEF_FROM_EDGE (reduc_def_stmt
,
5607 loop_preheader_edge (def_stmt_loop
));
5608 if (TREE_CODE (def_arg
) == SSA_NAME
5609 && (def_arg_stmt
= SSA_NAME_DEF_STMT (def_arg
))
5610 && gimple_code (def_arg_stmt
) == GIMPLE_PHI
5611 && flow_bb_inside_loop_p (outer_loop
, gimple_bb (def_arg_stmt
))
5612 && vinfo_for_stmt (def_arg_stmt
)
5613 && STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def_arg_stmt
))
5614 == vect_double_reduction_def
)
5615 double_reduc
= true;
5618 epilog_reduc_code
= ERROR_MARK
;
5620 if (STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
) != COND_REDUCTION
)
5622 if (reduction_code_for_scalar_code (orig_code
, &epilog_reduc_code
))
5624 reduc_optab
= optab_for_tree_code (epilog_reduc_code
, vectype_out
,
5628 if (dump_enabled_p ())
5629 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
5630 "no optab for reduction.\n");
5632 epilog_reduc_code
= ERROR_MARK
;
5634 else if (optab_handler (reduc_optab
, vec_mode
) == CODE_FOR_nothing
)
5636 if (dump_enabled_p ())
5637 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
5638 "reduc op not supported by target.\n");
5640 epilog_reduc_code
= ERROR_MARK
;
5643 /* When epilog_reduc_code is ERROR_MARK then a reduction will be
5644 generated in the epilog using multiple expressions. This does not
5645 work for condition reductions. */
5646 if (epilog_reduc_code
== ERROR_MARK
5647 && (STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
)
5648 == INTEGER_INDUC_COND_REDUCTION
5649 || STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
)
5650 == CONST_COND_REDUCTION
))
5652 if (dump_enabled_p ())
5653 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
5654 "no reduc code for scalar code.\n");
5660 if (!nested_cycle
|| double_reduc
)
5662 if (dump_enabled_p ())
5663 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
5664 "no reduc code for scalar code.\n");
5672 int scalar_precision
= GET_MODE_PRECISION (TYPE_MODE (scalar_type
));
5673 cr_index_scalar_type
= make_unsigned_type (scalar_precision
);
5674 cr_index_vector_type
= build_vector_type
5675 (cr_index_scalar_type
, TYPE_VECTOR_SUBPARTS (vectype_out
));
5677 epilog_reduc_code
= REDUC_MAX_EXPR
;
5678 optab
= optab_for_tree_code (REDUC_MAX_EXPR
, cr_index_vector_type
,
5680 if (optab_handler (optab
, TYPE_MODE (cr_index_vector_type
))
5681 == CODE_FOR_nothing
)
5683 if (dump_enabled_p ())
5684 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
5685 "reduc max op not supported by target.\n");
5691 || STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
) != TREE_CODE_REDUCTION
)
5694 if (dump_enabled_p ())
5695 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
5696 "multiple types in double reduction or condition "
5701 /* In case of widenning multiplication by a constant, we update the type
5702 of the constant to be the type of the other operand. We check that the
5703 constant fits the type in the pattern recognition pass. */
5704 if (code
== DOT_PROD_EXPR
5705 && !types_compatible_p (TREE_TYPE (ops
[0]), TREE_TYPE (ops
[1])))
5707 if (TREE_CODE (ops
[0]) == INTEGER_CST
)
5708 ops
[0] = fold_convert (TREE_TYPE (ops
[1]), ops
[0]);
5709 else if (TREE_CODE (ops
[1]) == INTEGER_CST
)
5710 ops
[1] = fold_convert (TREE_TYPE (ops
[0]), ops
[1]);
5713 if (dump_enabled_p ())
5714 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
5715 "invalid types in dot-prod\n");
5721 if (STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
) == COND_REDUCTION
)
5725 if (! max_loop_iterations (loop
, &ni
))
5727 if (dump_enabled_p ())
5728 dump_printf_loc (MSG_NOTE
, vect_location
,
5729 "loop count not known, cannot create cond "
5733 /* Convert backedges to iterations. */
5736 /* The additional index will be the same type as the condition. Check
5737 that the loop can fit into this less one (because we'll use up the
5738 zero slot for when there are no matches). */
5739 tree max_index
= TYPE_MAX_VALUE (cr_index_scalar_type
);
5740 if (wi::geu_p (ni
, wi::to_widest (max_index
)))
5742 if (dump_enabled_p ())
5743 dump_printf_loc (MSG_NOTE
, vect_location
,
5744 "loop size is greater than data size.\n");
5749 if (!vec_stmt
) /* transformation not required. */
5752 && !vect_model_reduction_cost (stmt_info
, epilog_reduc_code
, ncopies
,
5755 STMT_VINFO_TYPE (stmt_info
) = reduc_vec_info_type
;
5761 if (dump_enabled_p ())
5762 dump_printf_loc (MSG_NOTE
, vect_location
, "transform reduction.\n");
5764 /* FORNOW: Multiple types are not supported for condition. */
5765 if (code
== COND_EXPR
)
5766 gcc_assert (ncopies
== 1);
5768 /* Create the destination vector */
5769 vec_dest
= vect_create_destination_var (scalar_dest
, vectype_out
);
5771 /* In case the vectorization factor (VF) is bigger than the number
5772 of elements that we can fit in a vectype (nunits), we have to generate
5773 more than one vector stmt - i.e - we need to "unroll" the
5774 vector stmt by a factor VF/nunits. For more details see documentation
5775 in vectorizable_operation. */
5777 /* If the reduction is used in an outer loop we need to generate
5778 VF intermediate results, like so (e.g. for ncopies=2):
5783 (i.e. we generate VF results in 2 registers).
5784 In this case we have a separate def-use cycle for each copy, and therefore
5785 for each copy we get the vector def for the reduction variable from the
5786 respective phi node created for this copy.
5788 Otherwise (the reduction is unused in the loop nest), we can combine
5789 together intermediate results, like so (e.g. for ncopies=2):
5793 (i.e. we generate VF/2 results in a single register).
5794 In this case for each copy we get the vector def for the reduction variable
5795 from the vectorized reduction operation generated in the previous iteration.
5798 if (STMT_VINFO_RELEVANT (stmt_info
) <= vect_used_only_live
)
5800 single_defuse_cycle
= true;
5804 epilog_copies
= ncopies
;
5806 prev_stmt_info
= NULL
;
5807 prev_phi_info
= NULL
;
5809 vec_num
= SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node
);
5813 vec_oprnds0
.create (1);
5814 if (op_type
== ternary_op
)
5815 vec_oprnds1
.create (1);
5818 phis
.create (vec_num
);
5819 vect_defs
.create (vec_num
);
5821 vect_defs
.quick_push (NULL_TREE
);
5823 for (j
= 0; j
< ncopies
; j
++)
5825 if (j
== 0 || !single_defuse_cycle
)
5827 for (i
= 0; i
< vec_num
; i
++)
5829 /* Create the reduction-phi that defines the reduction
5831 new_phi
= create_phi_node (vec_dest
, loop
->header
);
5832 set_vinfo_for_stmt (new_phi
,
5833 new_stmt_vec_info (new_phi
, loop_vinfo
));
5834 if (j
== 0 || slp_node
)
5835 phis
.quick_push (new_phi
);
5839 if (code
== COND_EXPR
)
5841 gcc_assert (!slp_node
);
5842 vectorizable_condition (stmt
, gsi
, vec_stmt
,
5843 PHI_RESULT (phis
[0]),
5845 /* Multiple types are not supported for condition. */
5854 /* Get vec defs for all the operands except the reduction index,
5855 ensuring the ordering of the ops in the vector is kept. */
5856 auto_vec
<tree
, 3> slp_ops
;
5857 auto_vec
<vec
<tree
>, 3> vec_defs
;
5859 slp_ops
.quick_push (reduc_index
== 0 ? NULL
: ops
[0]);
5860 slp_ops
.quick_push (reduc_index
== 1 ? NULL
: ops
[1]);
5861 if (op_type
== ternary_op
)
5862 slp_ops
.quick_push (reduc_index
== 2 ? NULL
: ops
[2]);
5864 vect_get_slp_defs (slp_ops
, slp_node
, &vec_defs
, -1);
5866 vec_oprnds0
.safe_splice (vec_defs
[reduc_index
== 0 ? 1 : 0]);
5867 vec_defs
[reduc_index
== 0 ? 1 : 0].release ();
5868 if (op_type
== ternary_op
)
5870 vec_oprnds1
.safe_splice (vec_defs
[reduc_index
== 2 ? 1 : 2]);
5871 vec_defs
[reduc_index
== 2 ? 1 : 2].release ();
5876 loop_vec_def0
= vect_get_vec_def_for_operand (ops
[!reduc_index
],
5878 vec_oprnds0
.quick_push (loop_vec_def0
);
5879 if (op_type
== ternary_op
)
5881 op1
= reduc_index
== 0 ? ops
[2] : ops
[1];
5882 loop_vec_def1
= vect_get_vec_def_for_operand (op1
, stmt
);
5883 vec_oprnds1
.quick_push (loop_vec_def1
);
5891 enum vect_def_type dt
;
5894 vect_is_simple_use (ops
[!reduc_index
], loop_vinfo
,
5896 loop_vec_def0
= vect_get_vec_def_for_stmt_copy (dt
,
5898 vec_oprnds0
[0] = loop_vec_def0
;
5899 if (op_type
== ternary_op
)
5901 vect_is_simple_use (op1
, loop_vinfo
, &dummy_stmt
, &dt
);
5902 loop_vec_def1
= vect_get_vec_def_for_stmt_copy (dt
,
5904 vec_oprnds1
[0] = loop_vec_def1
;
5908 if (single_defuse_cycle
)
5909 reduc_def
= gimple_assign_lhs (new_stmt
);
5911 STMT_VINFO_RELATED_STMT (prev_phi_info
) = new_phi
;
5914 FOR_EACH_VEC_ELT (vec_oprnds0
, i
, def0
)
5917 reduc_def
= PHI_RESULT (phis
[i
]);
5920 if (!single_defuse_cycle
|| j
== 0)
5921 reduc_def
= PHI_RESULT (new_phi
);
5924 def1
= ((op_type
== ternary_op
)
5925 ? vec_oprnds1
[i
] : NULL
);
5926 if (op_type
== binary_op
)
5928 if (reduc_index
== 0)
5929 expr
= build2 (code
, vectype_out
, reduc_def
, def0
);
5931 expr
= build2 (code
, vectype_out
, def0
, reduc_def
);
5935 if (reduc_index
== 0)
5936 expr
= build3 (code
, vectype_out
, reduc_def
, def0
, def1
);
5939 if (reduc_index
== 1)
5940 expr
= build3 (code
, vectype_out
, def0
, reduc_def
, def1
);
5942 expr
= build3 (code
, vectype_out
, def0
, def1
, reduc_def
);
5946 new_stmt
= gimple_build_assign (vec_dest
, expr
);
5947 new_temp
= make_ssa_name (vec_dest
, new_stmt
);
5948 gimple_assign_set_lhs (new_stmt
, new_temp
);
5949 vect_finish_stmt_generation (stmt
, new_stmt
, gsi
);
5953 SLP_TREE_VEC_STMTS (slp_node
).quick_push (new_stmt
);
5954 vect_defs
.quick_push (new_temp
);
5957 vect_defs
[0] = new_temp
;
5964 STMT_VINFO_VEC_STMT (stmt_info
) = *vec_stmt
= new_stmt
;
5966 STMT_VINFO_RELATED_STMT (prev_stmt_info
) = new_stmt
;
5968 prev_stmt_info
= vinfo_for_stmt (new_stmt
);
5969 prev_phi_info
= vinfo_for_stmt (new_phi
);
5972 tree indx_before_incr
, indx_after_incr
, cond_name
= NULL
;
5974 /* Finalize the reduction-phi (set its arguments) and create the
5975 epilog reduction code. */
5976 if ((!single_defuse_cycle
|| code
== COND_EXPR
) && !slp_node
)
5978 new_temp
= gimple_assign_lhs (*vec_stmt
);
5979 vect_defs
[0] = new_temp
;
5981 /* For cond reductions we want to create a new vector (INDEX_COND_EXPR)
5982 which is updated with the current index of the loop for every match of
5983 the original loop's cond_expr (VEC_STMT). This results in a vector
5984 containing the last time the condition passed for that vector lane.
5985 The first match will be a 1 to allow 0 to be used for non-matching
5986 indexes. If there are no matches at all then the vector will be all
5988 if (STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
) == COND_REDUCTION
)
5990 int nunits_out
= TYPE_VECTOR_SUBPARTS (vectype_out
);
5993 gcc_assert (gimple_assign_rhs_code (*vec_stmt
) == VEC_COND_EXPR
);
5995 /* First we create a simple vector induction variable which starts
5996 with the values {1,2,3,...} (SERIES_VECT) and increments by the
5997 vector size (STEP). */
5999 /* Create a {1,2,3,...} vector. */
6000 tree
*vtemp
= XALLOCAVEC (tree
, nunits_out
);
6001 for (k
= 0; k
< nunits_out
; ++k
)
6002 vtemp
[k
] = build_int_cst (cr_index_scalar_type
, k
+ 1);
6003 tree series_vect
= build_vector (cr_index_vector_type
, vtemp
);
6005 /* Create a vector of the step value. */
6006 tree step
= build_int_cst (cr_index_scalar_type
, nunits_out
);
6007 tree vec_step
= build_vector_from_val (cr_index_vector_type
, step
);
6009 /* Create an induction variable. */
6010 gimple_stmt_iterator incr_gsi
;
6012 standard_iv_increment_position (loop
, &incr_gsi
, &insert_after
);
6013 create_iv (series_vect
, vec_step
, NULL_TREE
, loop
, &incr_gsi
,
6014 insert_after
, &indx_before_incr
, &indx_after_incr
);
6016 /* Next create a new phi node vector (NEW_PHI_TREE) which starts
6017 filled with zeros (VEC_ZERO). */
6019 /* Create a vector of 0s. */
6020 tree zero
= build_zero_cst (cr_index_scalar_type
);
6021 tree vec_zero
= build_vector_from_val (cr_index_vector_type
, zero
);
6023 /* Create a vector phi node. */
6024 tree new_phi_tree
= make_ssa_name (cr_index_vector_type
);
6025 new_phi
= create_phi_node (new_phi_tree
, loop
->header
);
6026 set_vinfo_for_stmt (new_phi
,
6027 new_stmt_vec_info (new_phi
, loop_vinfo
));
6028 add_phi_arg (new_phi
, vec_zero
, loop_preheader_edge (loop
),
6031 /* Now take the condition from the loops original cond_expr
6032 (VEC_STMT) and produce a new cond_expr (INDEX_COND_EXPR) which for
6033 every match uses values from the induction variable
6034 (INDEX_BEFORE_INCR) otherwise uses values from the phi node
6036 Finally, we update the phi (NEW_PHI_TREE) to take the value of
6037 the new cond_expr (INDEX_COND_EXPR). */
6039 /* Duplicate the condition from vec_stmt. */
6040 tree ccompare
= unshare_expr (gimple_assign_rhs1 (*vec_stmt
));
6042 /* Create a conditional, where the condition is taken from vec_stmt
6043 (CCOMPARE), then is the induction index (INDEX_BEFORE_INCR) and
6044 else is the phi (NEW_PHI_TREE). */
6045 tree index_cond_expr
= build3 (VEC_COND_EXPR
, cr_index_vector_type
,
6046 ccompare
, indx_before_incr
,
6048 cond_name
= make_ssa_name (cr_index_vector_type
);
6049 gimple
*index_condition
= gimple_build_assign (cond_name
,
6051 gsi_insert_before (&incr_gsi
, index_condition
, GSI_SAME_STMT
);
6052 stmt_vec_info index_vec_info
= new_stmt_vec_info (index_condition
,
6054 STMT_VINFO_VECTYPE (index_vec_info
) = cr_index_vector_type
;
6055 set_vinfo_for_stmt (index_condition
, index_vec_info
);
6057 /* Update the phi with the vec cond. */
6058 add_phi_arg (new_phi
, cond_name
, loop_latch_edge (loop
),
6063 vect_create_epilog_for_reduction (vect_defs
, stmt
, epilog_copies
,
6064 epilog_reduc_code
, phis
, reduc_index
,
6065 double_reduc
, slp_node
, cond_name
);
6070 /* Function vect_min_worthwhile_factor.
6072 For a loop where we could vectorize the operation indicated by CODE,
6073 return the minimum vectorization factor that makes it worthwhile
6074 to use generic vectors. */
6076 vect_min_worthwhile_factor (enum tree_code code
)
6097 /* Function vectorizable_induction
6099 Check if PHI performs an induction computation that can be vectorized.
6100 If VEC_STMT is also passed, vectorize the induction PHI: create a vectorized
6101 phi to replace it, put it in VEC_STMT, and add it to the same basic block.
6102 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
6105 vectorizable_induction (gimple
*phi
,
6106 gimple_stmt_iterator
*gsi ATTRIBUTE_UNUSED
,
6107 gimple
**vec_stmt
, slp_tree slp_node
)
6109 stmt_vec_info stmt_info
= vinfo_for_stmt (phi
);
6110 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_info
);
6111 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
6113 bool nested_in_vect_loop
= false;
6114 struct loop
*iv_loop
;
6116 edge pe
= loop_preheader_edge (loop
);
6118 tree new_vec
, vec_init
, vec_step
, t
;
6121 gphi
*induction_phi
;
6122 tree induc_def
, vec_dest
;
6123 tree init_expr
, step_expr
;
6124 int vf
= LOOP_VINFO_VECT_FACTOR (loop_vinfo
);
6128 imm_use_iterator imm_iter
;
6129 use_operand_p use_p
;
6133 gimple_stmt_iterator si
;
6134 basic_block bb
= gimple_bb (phi
);
6136 if (gimple_code (phi
) != GIMPLE_PHI
)
6139 if (!STMT_VINFO_RELEVANT_P (stmt_info
))
6142 /* Make sure it was recognized as induction computation. */
6143 if (STMT_VINFO_DEF_TYPE (stmt_info
) != vect_induction_def
)
6146 tree vectype
= STMT_VINFO_VECTYPE (stmt_info
);
6147 unsigned nunits
= TYPE_VECTOR_SUBPARTS (vectype
);
6152 ncopies
= vf
/ nunits
;
6153 gcc_assert (ncopies
>= 1);
6155 /* FORNOW. These restrictions should be relaxed. */
6156 if (nested_in_vect_loop_p (loop
, phi
))
6158 imm_use_iterator imm_iter
;
6159 use_operand_p use_p
;
6166 if (dump_enabled_p ())
6167 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
6168 "multiple types in nested loop.\n");
6172 /* FORNOW: outer loop induction with SLP not supported. */
6173 if (STMT_SLP_TYPE (stmt_info
))
6177 latch_e
= loop_latch_edge (loop
->inner
);
6178 loop_arg
= PHI_ARG_DEF_FROM_EDGE (phi
, latch_e
);
6179 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, loop_arg
)
6181 gimple
*use_stmt
= USE_STMT (use_p
);
6182 if (is_gimple_debug (use_stmt
))
6185 if (!flow_bb_inside_loop_p (loop
->inner
, gimple_bb (use_stmt
)))
6187 exit_phi
= use_stmt
;
6193 stmt_vec_info exit_phi_vinfo
= vinfo_for_stmt (exit_phi
);
6194 if (!(STMT_VINFO_RELEVANT_P (exit_phi_vinfo
)
6195 && !STMT_VINFO_LIVE_P (exit_phi_vinfo
)))
6197 if (dump_enabled_p ())
6198 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
6199 "inner-loop induction only used outside "
6200 "of the outer vectorized loop.\n");
6205 nested_in_vect_loop
= true;
6206 iv_loop
= loop
->inner
;
6210 gcc_assert (iv_loop
== (gimple_bb (phi
))->loop_father
);
6212 if (!vec_stmt
) /* transformation not required. */
6214 STMT_VINFO_TYPE (stmt_info
) = induc_vec_info_type
;
6215 if (dump_enabled_p ())
6216 dump_printf_loc (MSG_NOTE
, vect_location
,
6217 "=== vectorizable_induction ===\n");
6218 vect_model_induction_cost (stmt_info
, ncopies
);
6224 /* Compute a vector variable, initialized with the first VF values of
6225 the induction variable. E.g., for an iv with IV_PHI='X' and
6226 evolution S, for a vector of 4 units, we want to compute:
6227 [X, X + S, X + 2*S, X + 3*S]. */
6229 if (dump_enabled_p ())
6230 dump_printf_loc (MSG_NOTE
, vect_location
, "transform induction phi.\n");
6232 latch_e
= loop_latch_edge (iv_loop
);
6233 loop_arg
= PHI_ARG_DEF_FROM_EDGE (phi
, latch_e
);
6235 step_expr
= STMT_VINFO_LOOP_PHI_EVOLUTION_PART (stmt_info
);
6236 gcc_assert (step_expr
!= NULL_TREE
);
6238 pe
= loop_preheader_edge (iv_loop
);
6239 init_expr
= PHI_ARG_DEF_FROM_EDGE (phi
,
6240 loop_preheader_edge (iv_loop
));
6242 /* Convert the step to the desired type. */
6244 step_expr
= gimple_convert (&stmts
, TREE_TYPE (vectype
), step_expr
);
6247 new_bb
= gsi_insert_seq_on_edge_immediate (pe
, stmts
);
6248 gcc_assert (!new_bb
);
6251 /* Find the first insertion point in the BB. */
6252 si
= gsi_after_labels (bb
);
6254 /* For SLP induction we have to generate several IVs as for example
6255 with group size 3 we need [i, i, i, i + S] [i + S, i + S, i + 2*S, i + 2*S]
6256 [i + 2*S, i + 3*S, i + 3*S, i + 3*S]. The step is the same uniform
6257 [VF*S, VF*S, VF*S, VF*S] for all. */
6260 /* Convert the init to the desired type. */
6262 init_expr
= gimple_convert (&stmts
, TREE_TYPE (vectype
), init_expr
);
6265 new_bb
= gsi_insert_seq_on_edge_immediate (pe
, stmts
);
6266 gcc_assert (!new_bb
);
6269 /* Generate [VF*S, VF*S, ... ]. */
6270 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (step_expr
)))
6272 expr
= build_int_cst (integer_type_node
, vf
);
6273 expr
= fold_convert (TREE_TYPE (step_expr
), expr
);
6276 expr
= build_int_cst (TREE_TYPE (step_expr
), vf
);
6277 new_name
= fold_build2 (MULT_EXPR
, TREE_TYPE (step_expr
),
6279 if (! CONSTANT_CLASS_P (new_name
))
6280 new_name
= vect_init_vector (phi
, new_name
,
6281 TREE_TYPE (step_expr
), NULL
);
6282 new_vec
= build_vector_from_val (vectype
, new_name
);
6283 vec_step
= vect_init_vector (phi
, new_vec
, vectype
, NULL
);
6285 /* Now generate the IVs. */
6286 unsigned group_size
= SLP_TREE_SCALAR_STMTS (slp_node
).length ();
6287 unsigned nvects
= SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node
);
6288 unsigned elts
= nunits
* nvects
;
6289 unsigned nivs
= least_common_multiple (group_size
, nunits
) / nunits
;
6290 gcc_assert (elts
% group_size
== 0);
6291 tree elt
= init_expr
;
6293 for (ivn
= 0; ivn
< nivs
; ++ivn
)
6295 tree
*elts
= XALLOCAVEC (tree
, nunits
);
6296 bool constant_p
= true;
6297 for (unsigned eltn
= 0; eltn
< nunits
; ++eltn
)
6299 if (ivn
*nunits
+ eltn
>= group_size
6300 && (ivn
*nunits
+ eltn
) % group_size
== 0)
6303 elt
= gimple_build (&stmts
, PLUS_EXPR
, TREE_TYPE (elt
),
6307 new_bb
= gsi_insert_seq_on_edge_immediate (pe
, stmts
);
6308 gcc_assert (!new_bb
);
6311 if (! CONSTANT_CLASS_P (elt
))
6316 new_vec
= build_vector (vectype
, elts
);
6319 vec
<constructor_elt
, va_gc
> *v
;
6320 vec_alloc (v
, nunits
);
6321 for (i
= 0; i
< nunits
; ++i
)
6322 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, elts
[i
]);
6323 new_vec
= build_constructor (vectype
, v
);
6325 vec_init
= vect_init_vector (phi
, new_vec
, vectype
, NULL
);
6327 /* Create the induction-phi that defines the induction-operand. */
6328 vec_dest
= vect_get_new_vect_var (vectype
, vect_simple_var
, "vec_iv_");
6329 induction_phi
= create_phi_node (vec_dest
, iv_loop
->header
);
6330 set_vinfo_for_stmt (induction_phi
,
6331 new_stmt_vec_info (induction_phi
, loop_vinfo
));
6332 induc_def
= PHI_RESULT (induction_phi
);
6334 /* Create the iv update inside the loop */
6335 vec_def
= make_ssa_name (vec_dest
);
6336 new_stmt
= gimple_build_assign (vec_def
, PLUS_EXPR
, induc_def
, vec_step
);
6337 gsi_insert_before (&si
, new_stmt
, GSI_SAME_STMT
);
6338 set_vinfo_for_stmt (new_stmt
, new_stmt_vec_info (new_stmt
, loop_vinfo
));
6340 /* Set the arguments of the phi node: */
6341 add_phi_arg (induction_phi
, vec_init
, pe
, UNKNOWN_LOCATION
);
6342 add_phi_arg (induction_phi
, vec_def
, loop_latch_edge (iv_loop
),
6345 SLP_TREE_VEC_STMTS (slp_node
).quick_push (induction_phi
);
6348 /* Re-use IVs when we can. */
6352 = least_common_multiple (group_size
, nunits
) / group_size
;
6353 /* Generate [VF'*S, VF'*S, ... ]. */
6354 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (step_expr
)))
6356 expr
= build_int_cst (integer_type_node
, vfp
);
6357 expr
= fold_convert (TREE_TYPE (step_expr
), expr
);
6360 expr
= build_int_cst (TREE_TYPE (step_expr
), vfp
);
6361 new_name
= fold_build2 (MULT_EXPR
, TREE_TYPE (step_expr
),
6363 if (! CONSTANT_CLASS_P (new_name
))
6364 new_name
= vect_init_vector (phi
, new_name
,
6365 TREE_TYPE (step_expr
), NULL
);
6366 new_vec
= build_vector_from_val (vectype
, new_name
);
6367 vec_step
= vect_init_vector (phi
, new_vec
, vectype
, NULL
);
6368 for (; ivn
< nvects
; ++ivn
)
6370 gimple
*iv
= SLP_TREE_VEC_STMTS (slp_node
)[ivn
- nivs
];
6372 if (gimple_code (iv
) == GIMPLE_PHI
)
6373 def
= gimple_phi_result (iv
);
6375 def
= gimple_assign_lhs (iv
);
6376 new_stmt
= gimple_build_assign (make_ssa_name (vectype
),
6379 if (gimple_code (iv
) == GIMPLE_PHI
)
6380 gsi_insert_before (&si
, new_stmt
, GSI_SAME_STMT
);
6383 gimple_stmt_iterator tgsi
= gsi_for_stmt (iv
);
6384 gsi_insert_after (&tgsi
, new_stmt
, GSI_CONTINUE_LINKING
);
6386 set_vinfo_for_stmt (new_stmt
,
6387 new_stmt_vec_info (new_stmt
, loop_vinfo
));
6388 SLP_TREE_VEC_STMTS (slp_node
).quick_push (new_stmt
);
6395 /* Create the vector that holds the initial_value of the induction. */
6396 if (nested_in_vect_loop
)
6398 /* iv_loop is nested in the loop to be vectorized. init_expr had already
6399 been created during vectorization of previous stmts. We obtain it
6400 from the STMT_VINFO_VEC_STMT of the defining stmt. */
6401 vec_init
= vect_get_vec_def_for_operand (init_expr
, phi
);
6402 /* If the initial value is not of proper type, convert it. */
6403 if (!useless_type_conversion_p (vectype
, TREE_TYPE (vec_init
)))
6406 = gimple_build_assign (vect_get_new_ssa_name (vectype
,
6410 build1 (VIEW_CONVERT_EXPR
, vectype
,
6412 vec_init
= gimple_assign_lhs (new_stmt
);
6413 new_bb
= gsi_insert_on_edge_immediate (loop_preheader_edge (iv_loop
),
6415 gcc_assert (!new_bb
);
6416 set_vinfo_for_stmt (new_stmt
,
6417 new_stmt_vec_info (new_stmt
, loop_vinfo
));
6422 vec
<constructor_elt
, va_gc
> *v
;
6424 /* iv_loop is the loop to be vectorized. Create:
6425 vec_init = [X, X+S, X+2*S, X+3*S] (S = step_expr, X = init_expr) */
6427 new_name
= gimple_convert (&stmts
, TREE_TYPE (vectype
), init_expr
);
6429 vec_alloc (v
, nunits
);
6430 bool constant_p
= is_gimple_min_invariant (new_name
);
6431 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, new_name
);
6432 for (i
= 1; i
< nunits
; i
++)
6434 /* Create: new_name_i = new_name + step_expr */
6435 new_name
= gimple_build (&stmts
, PLUS_EXPR
, TREE_TYPE (new_name
),
6436 new_name
, step_expr
);
6437 if (!is_gimple_min_invariant (new_name
))
6439 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, new_name
);
6443 new_bb
= gsi_insert_seq_on_edge_immediate (pe
, stmts
);
6444 gcc_assert (!new_bb
);
6447 /* Create a vector from [new_name_0, new_name_1, ..., new_name_nunits-1] */
6449 new_vec
= build_vector_from_ctor (vectype
, v
);
6451 new_vec
= build_constructor (vectype
, v
);
6452 vec_init
= vect_init_vector (phi
, new_vec
, vectype
, NULL
);
6456 /* Create the vector that holds the step of the induction. */
6457 if (nested_in_vect_loop
)
6458 /* iv_loop is nested in the loop to be vectorized. Generate:
6459 vec_step = [S, S, S, S] */
6460 new_name
= step_expr
;
6463 /* iv_loop is the loop to be vectorized. Generate:
6464 vec_step = [VF*S, VF*S, VF*S, VF*S] */
6465 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (step_expr
)))
6467 expr
= build_int_cst (integer_type_node
, vf
);
6468 expr
= fold_convert (TREE_TYPE (step_expr
), expr
);
6471 expr
= build_int_cst (TREE_TYPE (step_expr
), vf
);
6472 new_name
= fold_build2 (MULT_EXPR
, TREE_TYPE (step_expr
),
6474 if (TREE_CODE (step_expr
) == SSA_NAME
)
6475 new_name
= vect_init_vector (phi
, new_name
,
6476 TREE_TYPE (step_expr
), NULL
);
6479 t
= unshare_expr (new_name
);
6480 gcc_assert (CONSTANT_CLASS_P (new_name
)
6481 || TREE_CODE (new_name
) == SSA_NAME
);
6482 new_vec
= build_vector_from_val (vectype
, t
);
6483 vec_step
= vect_init_vector (phi
, new_vec
, vectype
, NULL
);
6486 /* Create the following def-use cycle:
6491 vec_iv = PHI <vec_init, vec_loop>
6495 vec_loop = vec_iv + vec_step; */
6497 /* Create the induction-phi that defines the induction-operand. */
6498 vec_dest
= vect_get_new_vect_var (vectype
, vect_simple_var
, "vec_iv_");
6499 induction_phi
= create_phi_node (vec_dest
, iv_loop
->header
);
6500 set_vinfo_for_stmt (induction_phi
,
6501 new_stmt_vec_info (induction_phi
, loop_vinfo
));
6502 induc_def
= PHI_RESULT (induction_phi
);
6504 /* Create the iv update inside the loop */
6505 vec_def
= make_ssa_name (vec_dest
);
6506 new_stmt
= gimple_build_assign (vec_def
, PLUS_EXPR
, induc_def
, vec_step
);
6507 gsi_insert_before (&si
, new_stmt
, GSI_SAME_STMT
);
6508 set_vinfo_for_stmt (new_stmt
, new_stmt_vec_info (new_stmt
, loop_vinfo
));
6510 /* Set the arguments of the phi node: */
6511 add_phi_arg (induction_phi
, vec_init
, pe
, UNKNOWN_LOCATION
);
6512 add_phi_arg (induction_phi
, vec_def
, loop_latch_edge (iv_loop
),
6515 STMT_VINFO_VEC_STMT (stmt_info
) = *vec_stmt
= induction_phi
;
6517 /* In case that vectorization factor (VF) is bigger than the number
6518 of elements that we can fit in a vectype (nunits), we have to generate
6519 more than one vector stmt - i.e - we need to "unroll" the
6520 vector stmt by a factor VF/nunits. For more details see documentation
6521 in vectorizable_operation. */
6525 stmt_vec_info prev_stmt_vinfo
;
6526 /* FORNOW. This restriction should be relaxed. */
6527 gcc_assert (!nested_in_vect_loop
);
6529 /* Create the vector that holds the step of the induction. */
6530 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (step_expr
)))
6532 expr
= build_int_cst (integer_type_node
, nunits
);
6533 expr
= fold_convert (TREE_TYPE (step_expr
), expr
);
6536 expr
= build_int_cst (TREE_TYPE (step_expr
), nunits
);
6537 new_name
= fold_build2 (MULT_EXPR
, TREE_TYPE (step_expr
),
6539 if (TREE_CODE (step_expr
) == SSA_NAME
)
6540 new_name
= vect_init_vector (phi
, new_name
,
6541 TREE_TYPE (step_expr
), NULL
);
6542 t
= unshare_expr (new_name
);
6543 gcc_assert (CONSTANT_CLASS_P (new_name
)
6544 || TREE_CODE (new_name
) == SSA_NAME
);
6545 new_vec
= build_vector_from_val (vectype
, t
);
6546 vec_step
= vect_init_vector (phi
, new_vec
, vectype
, NULL
);
6548 vec_def
= induc_def
;
6549 prev_stmt_vinfo
= vinfo_for_stmt (induction_phi
);
6550 for (i
= 1; i
< ncopies
; i
++)
6552 /* vec_i = vec_prev + vec_step */
6553 new_stmt
= gimple_build_assign (vec_dest
, PLUS_EXPR
,
6555 vec_def
= make_ssa_name (vec_dest
, new_stmt
);
6556 gimple_assign_set_lhs (new_stmt
, vec_def
);
6558 gsi_insert_before (&si
, new_stmt
, GSI_SAME_STMT
);
6559 set_vinfo_for_stmt (new_stmt
,
6560 new_stmt_vec_info (new_stmt
, loop_vinfo
));
6561 STMT_VINFO_RELATED_STMT (prev_stmt_vinfo
) = new_stmt
;
6562 prev_stmt_vinfo
= vinfo_for_stmt (new_stmt
);
6566 if (nested_in_vect_loop
)
6568 /* Find the loop-closed exit-phi of the induction, and record
6569 the final vector of induction results: */
6571 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, loop_arg
)
6573 gimple
*use_stmt
= USE_STMT (use_p
);
6574 if (is_gimple_debug (use_stmt
))
6577 if (!flow_bb_inside_loop_p (iv_loop
, gimple_bb (use_stmt
)))
6579 exit_phi
= use_stmt
;
6585 stmt_vec_info stmt_vinfo
= vinfo_for_stmt (exit_phi
);
6586 /* FORNOW. Currently not supporting the case that an inner-loop induction
6587 is not used in the outer-loop (i.e. only outside the outer-loop). */
6588 gcc_assert (STMT_VINFO_RELEVANT_P (stmt_vinfo
)
6589 && !STMT_VINFO_LIVE_P (stmt_vinfo
));
6591 STMT_VINFO_VEC_STMT (stmt_vinfo
) = new_stmt
;
6592 if (dump_enabled_p ())
6594 dump_printf_loc (MSG_NOTE
, vect_location
,
6595 "vector of inductions after inner-loop:");
6596 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
, new_stmt
, 0);
6602 if (dump_enabled_p ())
6604 dump_printf_loc (MSG_NOTE
, vect_location
,
6605 "transform induction: created def-use cycle: ");
6606 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
, induction_phi
, 0);
6607 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
,
6608 SSA_NAME_DEF_STMT (vec_def
), 0);
6614 /* Function vectorizable_live_operation.
6616 STMT computes a value that is used outside the loop. Check if
6617 it can be supported. */
6620 vectorizable_live_operation (gimple
*stmt
,
6621 gimple_stmt_iterator
*gsi ATTRIBUTE_UNUSED
,
6622 slp_tree slp_node
, int slp_index
,
6625 stmt_vec_info stmt_info
= vinfo_for_stmt (stmt
);
6626 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_info
);
6627 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
6628 imm_use_iterator imm_iter
;
6629 tree lhs
, lhs_type
, bitsize
, vec_bitsize
;
6630 tree vectype
= STMT_VINFO_VECTYPE (stmt_info
);
6631 int nunits
= TYPE_VECTOR_SUBPARTS (vectype
);
6632 int ncopies
= LOOP_VINFO_VECT_FACTOR (loop_vinfo
) / nunits
;
6634 auto_vec
<tree
> vec_oprnds
;
6636 gcc_assert (STMT_VINFO_LIVE_P (stmt_info
));
6638 if (STMT_VINFO_DEF_TYPE (stmt_info
) == vect_reduction_def
)
6641 /* FORNOW. CHECKME. */
6642 if (nested_in_vect_loop_p (loop
, stmt
))
6645 /* If STMT is not relevant and it is a simple assignment and its inputs are
6646 invariant then it can remain in place, unvectorized. The original last
6647 scalar value that it computes will be used. */
6648 if (!STMT_VINFO_RELEVANT_P (stmt_info
))
6650 gcc_assert (is_simple_and_all_uses_invariant (stmt
, loop_vinfo
));
6651 if (dump_enabled_p ())
6652 dump_printf_loc (MSG_NOTE
, vect_location
,
6653 "statement is simple and uses invariant. Leaving in "
6659 /* No transformation required. */
6662 /* If stmt has a related stmt, then use that for getting the lhs. */
6663 if (is_pattern_stmt_p (stmt_info
))
6664 stmt
= STMT_VINFO_RELATED_STMT (stmt_info
);
6666 lhs
= (is_a
<gphi
*> (stmt
)) ? gimple_phi_result (stmt
)
6667 : gimple_get_lhs (stmt
);
6668 lhs_type
= TREE_TYPE (lhs
);
6670 bitsize
= TYPE_SIZE (TREE_TYPE (vectype
));
6671 vec_bitsize
= TYPE_SIZE (vectype
);
6673 /* Get the vectorized lhs of STMT and the lane to use (counted in bits). */
6674 tree vec_lhs
, bitstart
;
6677 gcc_assert (slp_index
>= 0);
6679 int num_scalar
= SLP_TREE_SCALAR_STMTS (slp_node
).length ();
6680 int num_vec
= SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node
);
6682 /* Get the last occurrence of the scalar index from the concatenation of
6683 all the slp vectors. Calculate which slp vector it is and the index
6685 int pos
= (num_vec
* nunits
) - num_scalar
+ slp_index
;
6686 int vec_entry
= pos
/ nunits
;
6687 int vec_index
= pos
% nunits
;
6689 /* Get the correct slp vectorized stmt. */
6690 vec_lhs
= gimple_get_lhs (SLP_TREE_VEC_STMTS (slp_node
)[vec_entry
]);
6692 /* Get entry to use. */
6693 bitstart
= build_int_cst (unsigned_type_node
, vec_index
);
6694 bitstart
= int_const_binop (MULT_EXPR
, bitsize
, bitstart
);
6698 enum vect_def_type dt
= STMT_VINFO_DEF_TYPE (stmt_info
);
6699 vec_lhs
= vect_get_vec_def_for_operand_1 (stmt
, dt
);
6701 /* For multiple copies, get the last copy. */
6702 for (int i
= 1; i
< ncopies
; ++i
)
6703 vec_lhs
= vect_get_vec_def_for_stmt_copy (vect_unknown_def_type
,
6706 /* Get the last lane in the vector. */
6707 bitstart
= int_const_binop (MINUS_EXPR
, vec_bitsize
, bitsize
);
6710 /* Create a new vectorized stmt for the uses of STMT and insert outside the
6712 gimple_seq stmts
= NULL
;
6713 tree bftype
= TREE_TYPE (vectype
);
6714 if (VECTOR_BOOLEAN_TYPE_P (vectype
))
6715 bftype
= build_nonstandard_integer_type (tree_to_uhwi (bitsize
), 1);
6716 tree new_tree
= build3 (BIT_FIELD_REF
, bftype
, vec_lhs
, bitsize
, bitstart
);
6717 new_tree
= force_gimple_operand (fold_convert (lhs_type
, new_tree
), &stmts
,
6720 gsi_insert_seq_on_edge_immediate (single_exit (loop
), stmts
);
6722 /* Replace use of lhs with newly computed result. If the use stmt is a
6723 single arg PHI, just replace all uses of PHI result. It's necessary
6724 because lcssa PHI defining lhs may be before newly inserted stmt. */
6725 use_operand_p use_p
;
6726 FOR_EACH_IMM_USE_STMT (use_stmt
, imm_iter
, lhs
)
6727 if (!flow_bb_inside_loop_p (loop
, gimple_bb (use_stmt
))
6728 && !is_gimple_debug (use_stmt
))
6730 if (gimple_code (use_stmt
) == GIMPLE_PHI
6731 && gimple_phi_num_args (use_stmt
) == 1)
6733 replace_uses_by (gimple_phi_result (use_stmt
), new_tree
);
6737 FOR_EACH_IMM_USE_ON_STMT (use_p
, imm_iter
)
6738 SET_USE (use_p
, new_tree
);
6740 update_stmt (use_stmt
);
6746 /* Kill any debug uses outside LOOP of SSA names defined in STMT. */
6749 vect_loop_kill_debug_uses (struct loop
*loop
, gimple
*stmt
)
6751 ssa_op_iter op_iter
;
6752 imm_use_iterator imm_iter
;
6753 def_operand_p def_p
;
6756 FOR_EACH_PHI_OR_STMT_DEF (def_p
, stmt
, op_iter
, SSA_OP_DEF
)
6758 FOR_EACH_IMM_USE_STMT (ustmt
, imm_iter
, DEF_FROM_PTR (def_p
))
6762 if (!is_gimple_debug (ustmt
))
6765 bb
= gimple_bb (ustmt
);
6767 if (!flow_bb_inside_loop_p (loop
, bb
))
6769 if (gimple_debug_bind_p (ustmt
))
6771 if (dump_enabled_p ())
6772 dump_printf_loc (MSG_NOTE
, vect_location
,
6773 "killing debug use\n");
6775 gimple_debug_bind_reset_value (ustmt
);
6776 update_stmt (ustmt
);
6785 /* Given loop represented by LOOP_VINFO, return true if computation of
6786 LOOP_VINFO_NITERS (= LOOP_VINFO_NITERSM1 + 1) doesn't overflow, false
6790 loop_niters_no_overflow (loop_vec_info loop_vinfo
)
6792 /* Constant case. */
6793 if (LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo
))
6795 tree cst_niters
= LOOP_VINFO_NITERS (loop_vinfo
);
6796 tree cst_nitersm1
= LOOP_VINFO_NITERSM1 (loop_vinfo
);
6798 gcc_assert (TREE_CODE (cst_niters
) == INTEGER_CST
);
6799 gcc_assert (TREE_CODE (cst_nitersm1
) == INTEGER_CST
);
6800 if (wi::to_widest (cst_nitersm1
) < wi::to_widest (cst_niters
))
6805 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
6806 /* Check the upper bound of loop niters. */
6807 if (get_max_loop_iterations (loop
, &max
))
6809 tree type
= TREE_TYPE (LOOP_VINFO_NITERS (loop_vinfo
));
6810 signop sgn
= TYPE_SIGN (type
);
6811 widest_int type_max
= widest_int::from (wi::max_value (type
), sgn
);
6818 /* Scale profiling counters by estimation for LOOP which is vectorized
6822 scale_profile_for_vect_loop (struct loop
*loop
, unsigned vf
)
6824 edge preheader
= loop_preheader_edge (loop
);
6825 /* Reduce loop iterations by the vectorization factor. */
6826 gcov_type new_est_niter
= niter_for_unrolled_loop (loop
, vf
);
6827 profile_count freq_h
= loop
->header
->count
, freq_e
= preheader
->count
;
6829 /* Use frequency only if counts are zero. */
6830 if (!(freq_h
> 0) && !(freq_e
> 0))
6832 freq_h
= profile_count::from_gcov_type (loop
->header
->frequency
);
6833 freq_e
= profile_count::from_gcov_type (EDGE_FREQUENCY (preheader
));
6839 /* Avoid dropping loop body profile counter to 0 because of zero count
6840 in loop's preheader. */
6841 if (!(freq_e
> profile_count::from_gcov_type (1)))
6842 freq_e
= profile_count::from_gcov_type (1);
6843 /* This should not overflow. */
6844 scale
= freq_e
.apply_scale (new_est_niter
+ 1, 1).probability_in (freq_h
);
6845 scale_loop_frequencies (loop
, scale
, REG_BR_PROB_BASE
);
6848 basic_block exit_bb
= single_pred (loop
->latch
);
6849 edge exit_e
= single_exit (loop
);
6850 exit_e
->count
= loop_preheader_edge (loop
)->count
;
6851 exit_e
->probability
= REG_BR_PROB_BASE
/ (new_est_niter
+ 1);
6853 edge exit_l
= single_pred_edge (loop
->latch
);
6854 int prob
= exit_l
->probability
;
6855 exit_l
->probability
= REG_BR_PROB_BASE
- exit_e
->probability
;
6856 exit_l
->count
= exit_bb
->count
- exit_e
->count
;
6858 scale_bbs_frequencies_int (&loop
->latch
, 1, exit_l
->probability
, prob
);
6861 /* Function vect_transform_loop.
6863 The analysis phase has determined that the loop is vectorizable.
6864 Vectorize the loop - created vectorized stmts to replace the scalar
6865 stmts in the loop, and update the loop exit condition.
6866 Returns scalar epilogue loop if any. */
6869 vect_transform_loop (loop_vec_info loop_vinfo
)
6871 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
6872 struct loop
*epilogue
= NULL
;
6873 basic_block
*bbs
= LOOP_VINFO_BBS (loop_vinfo
);
6874 int nbbs
= loop
->num_nodes
;
6876 tree niters_vector
= NULL
;
6877 int vf
= LOOP_VINFO_VECT_FACTOR (loop_vinfo
);
6879 bool slp_scheduled
= false;
6880 gimple
*stmt
, *pattern_stmt
;
6881 gimple_seq pattern_def_seq
= NULL
;
6882 gimple_stmt_iterator pattern_def_si
= gsi_none ();
6883 bool transform_pattern_stmt
= false;
6884 bool check_profitability
= false;
6887 if (dump_enabled_p ())
6888 dump_printf_loc (MSG_NOTE
, vect_location
, "=== vec_transform_loop ===\n");
6890 /* Use the more conservative vectorization threshold. If the number
6891 of iterations is constant assume the cost check has been performed
6892 by our caller. If the threshold makes all loops profitable that
6893 run at least the vectorization factor number of times checking
6894 is pointless, too. */
6895 th
= LOOP_VINFO_COST_MODEL_THRESHOLD (loop_vinfo
);
6896 if (th
>= LOOP_VINFO_VECT_FACTOR (loop_vinfo
) - 1
6897 && !LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo
))
6899 if (dump_enabled_p ())
6900 dump_printf_loc (MSG_NOTE
, vect_location
,
6901 "Profitability threshold is %d loop iterations.\n",
6903 check_profitability
= true;
6906 /* Make sure there exists a single-predecessor exit bb. Do this before
6908 edge e
= single_exit (loop
);
6909 if (! single_pred_p (e
->dest
))
6911 split_loop_exit_edge (e
);
6912 if (dump_enabled_p ())
6913 dump_printf (MSG_NOTE
, "split exit edge\n");
6916 /* Version the loop first, if required, so the profitability check
6919 if (LOOP_REQUIRES_VERSIONING (loop_vinfo
))
6921 vect_loop_versioning (loop_vinfo
, th
, check_profitability
);
6922 check_profitability
= false;
6925 /* Make sure there exists a single-predecessor exit bb also on the
6926 scalar loop copy. Do this after versioning but before peeling
6927 so CFG structure is fine for both scalar and if-converted loop
6928 to make slpeel_duplicate_current_defs_from_edges face matched
6929 loop closed PHI nodes on the exit. */
6930 if (LOOP_VINFO_SCALAR_LOOP (loop_vinfo
))
6932 e
= single_exit (LOOP_VINFO_SCALAR_LOOP (loop_vinfo
));
6933 if (! single_pred_p (e
->dest
))
6935 split_loop_exit_edge (e
);
6936 if (dump_enabled_p ())
6937 dump_printf (MSG_NOTE
, "split exit edge of scalar loop\n");
6941 tree niters
= vect_build_loop_niters (loop_vinfo
);
6942 LOOP_VINFO_NITERS_UNCHANGED (loop_vinfo
) = niters
;
6943 tree nitersm1
= unshare_expr (LOOP_VINFO_NITERSM1 (loop_vinfo
));
6944 bool niters_no_overflow
= loop_niters_no_overflow (loop_vinfo
);
6945 epilogue
= vect_do_peeling (loop_vinfo
, niters
, nitersm1
, &niters_vector
, th
,
6946 check_profitability
, niters_no_overflow
);
6947 if (niters_vector
== NULL_TREE
)
6949 if (LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo
))
6951 = build_int_cst (TREE_TYPE (LOOP_VINFO_NITERS (loop_vinfo
)),
6952 LOOP_VINFO_INT_NITERS (loop_vinfo
) / vf
);
6954 vect_gen_vector_loop_niters (loop_vinfo
, niters
, &niters_vector
,
6955 niters_no_overflow
);
6958 /* 1) Make sure the loop header has exactly two entries
6959 2) Make sure we have a preheader basic block. */
6961 gcc_assert (EDGE_COUNT (loop
->header
->preds
) == 2);
6963 split_edge (loop_preheader_edge (loop
));
6965 /* FORNOW: the vectorizer supports only loops which body consist
6966 of one basic block (header + empty latch). When the vectorizer will
6967 support more involved loop forms, the order by which the BBs are
6968 traversed need to be reconsidered. */
6970 for (i
= 0; i
< nbbs
; i
++)
6972 basic_block bb
= bbs
[i
];
6973 stmt_vec_info stmt_info
;
6975 for (gphi_iterator si
= gsi_start_phis (bb
); !gsi_end_p (si
);
6978 gphi
*phi
= si
.phi ();
6979 if (dump_enabled_p ())
6981 dump_printf_loc (MSG_NOTE
, vect_location
,
6982 "------>vectorizing phi: ");
6983 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
, phi
, 0);
6985 stmt_info
= vinfo_for_stmt (phi
);
6989 if (MAY_HAVE_DEBUG_STMTS
&& !STMT_VINFO_LIVE_P (stmt_info
))
6990 vect_loop_kill_debug_uses (loop
, phi
);
6992 if (!STMT_VINFO_RELEVANT_P (stmt_info
)
6993 && !STMT_VINFO_LIVE_P (stmt_info
))
6996 if (STMT_VINFO_VECTYPE (stmt_info
)
6997 && (TYPE_VECTOR_SUBPARTS (STMT_VINFO_VECTYPE (stmt_info
))
6998 != (unsigned HOST_WIDE_INT
) vf
)
6999 && dump_enabled_p ())
7000 dump_printf_loc (MSG_NOTE
, vect_location
, "multiple-types.\n");
7002 if (STMT_VINFO_DEF_TYPE (stmt_info
) == vect_induction_def
7003 && ! PURE_SLP_STMT (stmt_info
))
7005 if (dump_enabled_p ())
7006 dump_printf_loc (MSG_NOTE
, vect_location
, "transform phi.\n");
7007 vect_transform_stmt (phi
, NULL
, NULL
, NULL
, NULL
);
7011 pattern_stmt
= NULL
;
7012 for (gimple_stmt_iterator si
= gsi_start_bb (bb
);
7013 !gsi_end_p (si
) || transform_pattern_stmt
;)
7017 if (transform_pattern_stmt
)
7018 stmt
= pattern_stmt
;
7021 stmt
= gsi_stmt (si
);
7022 /* During vectorization remove existing clobber stmts. */
7023 if (gimple_clobber_p (stmt
))
7025 unlink_stmt_vdef (stmt
);
7026 gsi_remove (&si
, true);
7027 release_defs (stmt
);
7032 if (dump_enabled_p ())
7034 dump_printf_loc (MSG_NOTE
, vect_location
,
7035 "------>vectorizing statement: ");
7036 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
, stmt
, 0);
7039 stmt_info
= vinfo_for_stmt (stmt
);
7041 /* vector stmts created in the outer-loop during vectorization of
7042 stmts in an inner-loop may not have a stmt_info, and do not
7043 need to be vectorized. */
7050 if (MAY_HAVE_DEBUG_STMTS
&& !STMT_VINFO_LIVE_P (stmt_info
))
7051 vect_loop_kill_debug_uses (loop
, stmt
);
7053 if (!STMT_VINFO_RELEVANT_P (stmt_info
)
7054 && !STMT_VINFO_LIVE_P (stmt_info
))
7056 if (STMT_VINFO_IN_PATTERN_P (stmt_info
)
7057 && (pattern_stmt
= STMT_VINFO_RELATED_STMT (stmt_info
))
7058 && (STMT_VINFO_RELEVANT_P (vinfo_for_stmt (pattern_stmt
))
7059 || STMT_VINFO_LIVE_P (vinfo_for_stmt (pattern_stmt
))))
7061 stmt
= pattern_stmt
;
7062 stmt_info
= vinfo_for_stmt (stmt
);
7070 else if (STMT_VINFO_IN_PATTERN_P (stmt_info
)
7071 && (pattern_stmt
= STMT_VINFO_RELATED_STMT (stmt_info
))
7072 && (STMT_VINFO_RELEVANT_P (vinfo_for_stmt (pattern_stmt
))
7073 || STMT_VINFO_LIVE_P (vinfo_for_stmt (pattern_stmt
))))
7074 transform_pattern_stmt
= true;
7076 /* If pattern statement has def stmts, vectorize them too. */
7077 if (is_pattern_stmt_p (stmt_info
))
7079 if (pattern_def_seq
== NULL
)
7081 pattern_def_seq
= STMT_VINFO_PATTERN_DEF_SEQ (stmt_info
);
7082 pattern_def_si
= gsi_start (pattern_def_seq
);
7084 else if (!gsi_end_p (pattern_def_si
))
7085 gsi_next (&pattern_def_si
);
7086 if (pattern_def_seq
!= NULL
)
7088 gimple
*pattern_def_stmt
= NULL
;
7089 stmt_vec_info pattern_def_stmt_info
= NULL
;
7091 while (!gsi_end_p (pattern_def_si
))
7093 pattern_def_stmt
= gsi_stmt (pattern_def_si
);
7094 pattern_def_stmt_info
7095 = vinfo_for_stmt (pattern_def_stmt
);
7096 if (STMT_VINFO_RELEVANT_P (pattern_def_stmt_info
)
7097 || STMT_VINFO_LIVE_P (pattern_def_stmt_info
))
7099 gsi_next (&pattern_def_si
);
7102 if (!gsi_end_p (pattern_def_si
))
7104 if (dump_enabled_p ())
7106 dump_printf_loc (MSG_NOTE
, vect_location
,
7107 "==> vectorizing pattern def "
7109 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
,
7110 pattern_def_stmt
, 0);
7113 stmt
= pattern_def_stmt
;
7114 stmt_info
= pattern_def_stmt_info
;
7118 pattern_def_si
= gsi_none ();
7119 transform_pattern_stmt
= false;
7123 transform_pattern_stmt
= false;
7126 if (STMT_VINFO_VECTYPE (stmt_info
))
7130 TYPE_VECTOR_SUBPARTS (STMT_VINFO_VECTYPE (stmt_info
));
7131 if (!STMT_SLP_TYPE (stmt_info
)
7132 && nunits
!= (unsigned int) vf
7133 && dump_enabled_p ())
7134 /* For SLP VF is set according to unrolling factor, and not
7135 to vector size, hence for SLP this print is not valid. */
7136 dump_printf_loc (MSG_NOTE
, vect_location
, "multiple-types.\n");
7139 /* SLP. Schedule all the SLP instances when the first SLP stmt is
7141 if (STMT_SLP_TYPE (stmt_info
))
7145 slp_scheduled
= true;
7147 if (dump_enabled_p ())
7148 dump_printf_loc (MSG_NOTE
, vect_location
,
7149 "=== scheduling SLP instances ===\n");
7151 vect_schedule_slp (loop_vinfo
);
7154 /* Hybrid SLP stmts must be vectorized in addition to SLP. */
7155 if (!vinfo_for_stmt (stmt
) || PURE_SLP_STMT (stmt_info
))
7157 if (!transform_pattern_stmt
&& gsi_end_p (pattern_def_si
))
7159 pattern_def_seq
= NULL
;
7166 /* -------- vectorize statement ------------ */
7167 if (dump_enabled_p ())
7168 dump_printf_loc (MSG_NOTE
, vect_location
, "transform statement.\n");
7170 grouped_store
= false;
7171 is_store
= vect_transform_stmt (stmt
, &si
, &grouped_store
, NULL
, NULL
);
7174 if (STMT_VINFO_GROUPED_ACCESS (stmt_info
))
7176 /* Interleaving. If IS_STORE is TRUE, the vectorization of the
7177 interleaving chain was completed - free all the stores in
7180 vect_remove_stores (GROUP_FIRST_ELEMENT (stmt_info
));
7184 /* Free the attached stmt_vec_info and remove the stmt. */
7185 gimple
*store
= gsi_stmt (si
);
7186 free_stmt_vec_info (store
);
7187 unlink_stmt_vdef (store
);
7188 gsi_remove (&si
, true);
7189 release_defs (store
);
7192 /* Stores can only appear at the end of pattern statements. */
7193 gcc_assert (!transform_pattern_stmt
);
7194 pattern_def_seq
= NULL
;
7196 else if (!transform_pattern_stmt
&& gsi_end_p (pattern_def_si
))
7198 pattern_def_seq
= NULL
;
7204 slpeel_make_loop_iterate_ntimes (loop
, niters_vector
);
7206 scale_profile_for_vect_loop (loop
, vf
);
7208 /* The minimum number of iterations performed by the epilogue. This
7209 is 1 when peeling for gaps because we always need a final scalar
7211 int min_epilogue_iters
= LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo
) ? 1 : 0;
7212 /* +1 to convert latch counts to loop iteration counts,
7213 -min_epilogue_iters to remove iterations that cannot be performed
7214 by the vector code. */
7215 int bias
= 1 - min_epilogue_iters
;
7216 /* In these calculations the "- 1" converts loop iteration counts
7217 back to latch counts. */
7218 if (loop
->any_upper_bound
)
7219 loop
->nb_iterations_upper_bound
7220 = wi::udiv_floor (loop
->nb_iterations_upper_bound
+ bias
, vf
) - 1;
7221 if (loop
->any_likely_upper_bound
)
7222 loop
->nb_iterations_likely_upper_bound
7223 = wi::udiv_floor (loop
->nb_iterations_likely_upper_bound
+ bias
, vf
) - 1;
7224 if (loop
->any_estimate
)
7225 loop
->nb_iterations_estimate
7226 = wi::udiv_floor (loop
->nb_iterations_estimate
+ bias
, vf
) - 1;
7228 if (dump_enabled_p ())
7230 if (!LOOP_VINFO_EPILOGUE_P (loop_vinfo
))
7232 dump_printf_loc (MSG_NOTE
, vect_location
,
7233 "LOOP VECTORIZED\n");
7235 dump_printf_loc (MSG_NOTE
, vect_location
,
7236 "OUTER LOOP VECTORIZED\n");
7237 dump_printf (MSG_NOTE
, "\n");
7240 dump_printf_loc (MSG_NOTE
, vect_location
,
7241 "LOOP EPILOGUE VECTORIZED (VS=%d)\n",
7242 current_vector_size
);
7245 /* Free SLP instances here because otherwise stmt reference counting
7247 slp_instance instance
;
7248 FOR_EACH_VEC_ELT (LOOP_VINFO_SLP_INSTANCES (loop_vinfo
), i
, instance
)
7249 vect_free_slp_instance (instance
);
7250 LOOP_VINFO_SLP_INSTANCES (loop_vinfo
).release ();
7251 /* Clear-up safelen field since its value is invalid after vectorization
7252 since vectorized loop can have loop-carried dependencies. */
7255 /* Don't vectorize epilogue for epilogue. */
7256 if (LOOP_VINFO_EPILOGUE_P (loop_vinfo
))
7261 unsigned int vector_sizes
7262 = targetm
.vectorize
.autovectorize_vector_sizes ();
7263 vector_sizes
&= current_vector_size
- 1;
7265 if (!PARAM_VALUE (PARAM_VECT_EPILOGUES_NOMASK
))
7267 else if (!vector_sizes
)
7269 else if (LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo
)
7270 && LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo
) >= 0)
7272 int smallest_vec_size
= 1 << ctz_hwi (vector_sizes
);
7273 int ratio
= current_vector_size
/ smallest_vec_size
;
7274 int eiters
= LOOP_VINFO_INT_NITERS (loop_vinfo
)
7275 - LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo
);
7276 eiters
= eiters
% vf
;
7278 epilogue
->nb_iterations_upper_bound
= eiters
- 1;
7280 if (eiters
< vf
/ ratio
)
7287 epilogue
->force_vectorize
= loop
->force_vectorize
;
7288 epilogue
->safelen
= loop
->safelen
;
7289 epilogue
->dont_vectorize
= false;
7291 /* We may need to if-convert epilogue to vectorize it. */
7292 if (LOOP_VINFO_SCALAR_LOOP (loop_vinfo
))
7293 tree_if_conversion (epilogue
);
7299 /* The code below is trying to perform simple optimization - revert
7300 if-conversion for masked stores, i.e. if the mask of a store is zero
7301 do not perform it and all stored value producers also if possible.
7309 this transformation will produce the following semi-hammock:
7311 if (!mask__ifc__42.18_165 == { 0, 0, 0, 0, 0, 0, 0, 0 })
7313 vect__11.19_170 = MASK_LOAD (vectp_p1.20_168, 0B, mask__ifc__42.18_165);
7314 vect__12.22_172 = vect__11.19_170 + vect_cst__171;
7315 MASK_STORE (vectp_p1.23_175, 0B, mask__ifc__42.18_165, vect__12.22_172);
7316 vect__18.25_182 = MASK_LOAD (vectp_p3.26_180, 0B, mask__ifc__42.18_165);
7317 vect__19.28_184 = vect__18.25_182 + vect_cst__183;
7318 MASK_STORE (vectp_p2.29_187, 0B, mask__ifc__42.18_165, vect__19.28_184);
7323 optimize_mask_stores (struct loop
*loop
)
7325 basic_block
*bbs
= get_loop_body (loop
);
7326 unsigned nbbs
= loop
->num_nodes
;
7329 struct loop
*bb_loop
;
7330 gimple_stmt_iterator gsi
;
7332 auto_vec
<gimple
*> worklist
;
7334 vect_location
= find_loop_location (loop
);
7335 /* Pick up all masked stores in loop if any. */
7336 for (i
= 0; i
< nbbs
; i
++)
7339 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
);
7342 stmt
= gsi_stmt (gsi
);
7343 if (gimple_call_internal_p (stmt
, IFN_MASK_STORE
))
7344 worklist
.safe_push (stmt
);
7349 if (worklist
.is_empty ())
7352 /* Loop has masked stores. */
7353 while (!worklist
.is_empty ())
7355 gimple
*last
, *last_store
;
7358 basic_block store_bb
, join_bb
;
7359 gimple_stmt_iterator gsi_to
;
7360 tree vdef
, new_vdef
;
7365 last
= worklist
.pop ();
7366 mask
= gimple_call_arg (last
, 2);
7367 bb
= gimple_bb (last
);
7368 /* Create then_bb and if-then structure in CFG, then_bb belongs to
7369 the same loop as if_bb. It could be different to LOOP when two
7370 level loop-nest is vectorized and mask_store belongs to the inner
7372 e
= split_block (bb
, last
);
7373 bb_loop
= bb
->loop_father
;
7374 gcc_assert (loop
== bb_loop
|| flow_loop_nested_p (loop
, bb_loop
));
7376 store_bb
= create_empty_bb (bb
);
7377 add_bb_to_loop (store_bb
, bb_loop
);
7378 e
->flags
= EDGE_TRUE_VALUE
;
7379 efalse
= make_edge (bb
, store_bb
, EDGE_FALSE_VALUE
);
7380 /* Put STORE_BB to likely part. */
7381 efalse
->probability
= PROB_UNLIKELY
;
7382 store_bb
->frequency
= PROB_ALWAYS
- EDGE_FREQUENCY (efalse
);
7383 make_edge (store_bb
, join_bb
, EDGE_FALLTHRU
);
7384 if (dom_info_available_p (CDI_DOMINATORS
))
7385 set_immediate_dominator (CDI_DOMINATORS
, store_bb
, bb
);
7386 if (dump_enabled_p ())
7387 dump_printf_loc (MSG_NOTE
, vect_location
,
7388 "Create new block %d to sink mask stores.",
7390 /* Create vector comparison with boolean result. */
7391 vectype
= TREE_TYPE (mask
);
7392 zero
= build_zero_cst (vectype
);
7393 stmt
= gimple_build_cond (EQ_EXPR
, mask
, zero
, NULL_TREE
, NULL_TREE
);
7394 gsi
= gsi_last_bb (bb
);
7395 gsi_insert_after (&gsi
, stmt
, GSI_SAME_STMT
);
7396 /* Create new PHI node for vdef of the last masked store:
7397 .MEM_2 = VDEF <.MEM_1>
7398 will be converted to
7399 .MEM.3 = VDEF <.MEM_1>
7400 and new PHI node will be created in join bb
7401 .MEM_2 = PHI <.MEM_1, .MEM_3>
7403 vdef
= gimple_vdef (last
);
7404 new_vdef
= make_ssa_name (gimple_vop (cfun
), last
);
7405 gimple_set_vdef (last
, new_vdef
);
7406 phi
= create_phi_node (vdef
, join_bb
);
7407 add_phi_arg (phi
, new_vdef
, EDGE_SUCC (store_bb
, 0), UNKNOWN_LOCATION
);
7409 /* Put all masked stores with the same mask to STORE_BB if possible. */
7412 gimple_stmt_iterator gsi_from
;
7413 gimple
*stmt1
= NULL
;
7415 /* Move masked store to STORE_BB. */
7417 gsi
= gsi_for_stmt (last
);
7419 /* Shift GSI to the previous stmt for further traversal. */
7421 gsi_to
= gsi_start_bb (store_bb
);
7422 gsi_move_before (&gsi_from
, &gsi_to
);
7423 /* Setup GSI_TO to the non-empty block start. */
7424 gsi_to
= gsi_start_bb (store_bb
);
7425 if (dump_enabled_p ())
7427 dump_printf_loc (MSG_NOTE
, vect_location
,
7428 "Move stmt to created bb\n");
7429 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
, last
, 0);
7431 /* Move all stored value producers if possible. */
7432 while (!gsi_end_p (gsi
))
7435 imm_use_iterator imm_iter
;
7436 use_operand_p use_p
;
7439 /* Skip debug statements. */
7440 if (is_gimple_debug (gsi_stmt (gsi
)))
7445 stmt1
= gsi_stmt (gsi
);
7446 /* Do not consider statements writing to memory or having
7447 volatile operand. */
7448 if (gimple_vdef (stmt1
)
7449 || gimple_has_volatile_ops (stmt1
))
7453 lhs
= gimple_get_lhs (stmt1
);
7457 /* LHS of vectorized stmt must be SSA_NAME. */
7458 if (TREE_CODE (lhs
) != SSA_NAME
)
7461 if (!VECTOR_TYPE_P (TREE_TYPE (lhs
)))
7463 /* Remove dead scalar statement. */
7464 if (has_zero_uses (lhs
))
7466 gsi_remove (&gsi_from
, true);
7471 /* Check that LHS does not have uses outside of STORE_BB. */
7473 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, lhs
)
7476 use_stmt
= USE_STMT (use_p
);
7477 if (is_gimple_debug (use_stmt
))
7479 if (gimple_bb (use_stmt
) != store_bb
)
7488 if (gimple_vuse (stmt1
)
7489 && gimple_vuse (stmt1
) != gimple_vuse (last_store
))
7492 /* Can move STMT1 to STORE_BB. */
7493 if (dump_enabled_p ())
7495 dump_printf_loc (MSG_NOTE
, vect_location
,
7496 "Move stmt to created bb\n");
7497 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
, stmt1
, 0);
7499 gsi_move_before (&gsi_from
, &gsi_to
);
7500 /* Shift GSI_TO for further insertion. */
7503 /* Put other masked stores with the same mask to STORE_BB. */
7504 if (worklist
.is_empty ()
7505 || gimple_call_arg (worklist
.last (), 2) != mask
7506 || worklist
.last () != stmt1
)
7508 last
= worklist
.pop ();
7510 add_phi_arg (phi
, gimple_vuse (last_store
), e
, UNKNOWN_LOCATION
);