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"
53 #include "internal-fn.h"
54 #include "tree-vector-builder.h"
55 #include "vec-perm-indices.h"
57 /* Loop Vectorization Pass.
59 This pass tries to vectorize loops.
61 For example, the vectorizer transforms the following simple loop:
63 short a[N]; short b[N]; short c[N]; int i;
69 as if it was manually vectorized by rewriting the source code into:
71 typedef int __attribute__((mode(V8HI))) v8hi;
72 short a[N]; short b[N]; short c[N]; int i;
73 v8hi *pa = (v8hi*)a, *pb = (v8hi*)b, *pc = (v8hi*)c;
76 for (i=0; i<N/8; i++){
83 The main entry to this pass is vectorize_loops(), in which
84 the vectorizer applies a set of analyses on a given set of loops,
85 followed by the actual vectorization transformation for the loops that
86 had successfully passed the analysis phase.
87 Throughout this pass we make a distinction between two types of
88 data: scalars (which are represented by SSA_NAMES), and memory references
89 ("data-refs"). These two types of data require different handling both
90 during analysis and transformation. The types of data-refs that the
91 vectorizer currently supports are ARRAY_REFS which base is an array DECL
92 (not a pointer), and INDIRECT_REFS through pointers; both array and pointer
93 accesses are required to have a simple (consecutive) access pattern.
97 The driver for the analysis phase is vect_analyze_loop().
98 It applies a set of analyses, some of which rely on the scalar evolution
99 analyzer (scev) developed by Sebastian Pop.
101 During the analysis phase the vectorizer records some information
102 per stmt in a "stmt_vec_info" struct which is attached to each stmt in the
103 loop, as well as general information about the loop as a whole, which is
104 recorded in a "loop_vec_info" struct attached to each loop.
106 Transformation phase:
107 =====================
108 The loop transformation phase scans all the stmts in the loop, and
109 creates a vector stmt (or a sequence of stmts) for each scalar stmt S in
110 the loop that needs to be vectorized. It inserts the vector code sequence
111 just before the scalar stmt S, and records a pointer to the vector code
112 in STMT_VINFO_VEC_STMT (stmt_info) (stmt_info is the stmt_vec_info struct
113 attached to S). This pointer will be used for the vectorization of following
114 stmts which use the def of stmt S. Stmt S is removed if it writes to memory;
115 otherwise, we rely on dead code elimination for removing it.
117 For example, say stmt S1 was vectorized into stmt VS1:
120 S1: b = x[i]; STMT_VINFO_VEC_STMT (stmt_info (S1)) = VS1
123 To vectorize stmt S2, the vectorizer first finds the stmt that defines
124 the operand 'b' (S1), and gets the relevant vector def 'vb' from the
125 vector stmt VS1 pointed to by STMT_VINFO_VEC_STMT (stmt_info (S1)). The
126 resulting sequence would be:
129 S1: b = x[i]; STMT_VINFO_VEC_STMT (stmt_info (S1)) = VS1
131 S2: a = b; STMT_VINFO_VEC_STMT (stmt_info (S2)) = VS2
133 Operands that are not SSA_NAMEs, are data-refs that appear in
134 load/store operations (like 'x[i]' in S1), and are handled differently.
138 Currently the only target specific information that is used is the
139 size of the vector (in bytes) - "TARGET_VECTORIZE_UNITS_PER_SIMD_WORD".
140 Targets that can support different sizes of vectors, for now will need
141 to specify one value for "TARGET_VECTORIZE_UNITS_PER_SIMD_WORD". More
142 flexibility will be added in the future.
144 Since we only vectorize operations which vector form can be
145 expressed using existing tree codes, to verify that an operation is
146 supported, the vectorizer checks the relevant optab at the relevant
147 machine_mode (e.g, optab_handler (add_optab, V8HImode)). If
148 the value found is CODE_FOR_nothing, then there's no target support, and
149 we can't vectorize the stmt.
151 For additional information on this project see:
152 http://gcc.gnu.org/projects/tree-ssa/vectorization.html
155 static void vect_estimate_min_profitable_iters (loop_vec_info
, int *, int *);
157 /* Function vect_determine_vectorization_factor
159 Determine the vectorization factor (VF). VF is the number of data elements
160 that are operated upon in parallel in a single iteration of the vectorized
161 loop. For example, when vectorizing a loop that operates on 4byte elements,
162 on a target with vector size (VS) 16byte, the VF is set to 4, since 4
163 elements can fit in a single vector register.
165 We currently support vectorization of loops in which all types operated upon
166 are of the same size. Therefore this function currently sets VF according to
167 the size of the types operated upon, and fails if there are multiple sizes
170 VF is also the factor by which the loop iterations are strip-mined, e.g.:
177 for (i=0; i<N; i+=VF){
178 a[i:VF] = b[i:VF] + c[i:VF];
183 vect_determine_vectorization_factor (loop_vec_info loop_vinfo
)
185 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
186 basic_block
*bbs
= LOOP_VINFO_BBS (loop_vinfo
);
187 unsigned nbbs
= loop
->num_nodes
;
188 unsigned int vectorization_factor
= 0;
189 tree scalar_type
= NULL_TREE
;
193 stmt_vec_info stmt_info
;
196 gimple
*stmt
, *pattern_stmt
= NULL
;
197 gimple_seq pattern_def_seq
= NULL
;
198 gimple_stmt_iterator pattern_def_si
= gsi_none ();
199 bool analyze_pattern_stmt
= false;
201 auto_vec
<stmt_vec_info
> mask_producers
;
203 if (dump_enabled_p ())
204 dump_printf_loc (MSG_NOTE
, vect_location
,
205 "=== vect_determine_vectorization_factor ===\n");
207 for (i
= 0; i
< nbbs
; i
++)
209 basic_block bb
= bbs
[i
];
211 for (gphi_iterator si
= gsi_start_phis (bb
); !gsi_end_p (si
);
215 stmt_info
= vinfo_for_stmt (phi
);
216 if (dump_enabled_p ())
218 dump_printf_loc (MSG_NOTE
, vect_location
, "==> examining phi: ");
219 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
, phi
, 0);
222 gcc_assert (stmt_info
);
224 if (STMT_VINFO_RELEVANT_P (stmt_info
)
225 || STMT_VINFO_LIVE_P (stmt_info
))
227 gcc_assert (!STMT_VINFO_VECTYPE (stmt_info
));
228 scalar_type
= TREE_TYPE (PHI_RESULT (phi
));
230 if (dump_enabled_p ())
232 dump_printf_loc (MSG_NOTE
, vect_location
,
233 "get vectype for scalar type: ");
234 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, scalar_type
);
235 dump_printf (MSG_NOTE
, "\n");
238 vectype
= get_vectype_for_scalar_type (scalar_type
);
241 if (dump_enabled_p ())
243 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
244 "not vectorized: unsupported "
246 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
,
248 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
252 STMT_VINFO_VECTYPE (stmt_info
) = vectype
;
254 if (dump_enabled_p ())
256 dump_printf_loc (MSG_NOTE
, vect_location
, "vectype: ");
257 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, vectype
);
258 dump_printf (MSG_NOTE
, "\n");
261 nunits
= TYPE_VECTOR_SUBPARTS (vectype
);
262 if (dump_enabled_p ())
263 dump_printf_loc (MSG_NOTE
, vect_location
, "nunits = %d\n",
266 if (!vectorization_factor
267 || (nunits
> vectorization_factor
))
268 vectorization_factor
= nunits
;
272 for (gimple_stmt_iterator si
= gsi_start_bb (bb
);
273 !gsi_end_p (si
) || analyze_pattern_stmt
;)
277 if (analyze_pattern_stmt
)
280 stmt
= gsi_stmt (si
);
282 stmt_info
= vinfo_for_stmt (stmt
);
284 if (dump_enabled_p ())
286 dump_printf_loc (MSG_NOTE
, vect_location
,
287 "==> examining statement: ");
288 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
, stmt
, 0);
291 gcc_assert (stmt_info
);
293 /* Skip stmts which do not need to be vectorized. */
294 if ((!STMT_VINFO_RELEVANT_P (stmt_info
)
295 && !STMT_VINFO_LIVE_P (stmt_info
))
296 || gimple_clobber_p (stmt
))
298 if (STMT_VINFO_IN_PATTERN_P (stmt_info
)
299 && (pattern_stmt
= STMT_VINFO_RELATED_STMT (stmt_info
))
300 && (STMT_VINFO_RELEVANT_P (vinfo_for_stmt (pattern_stmt
))
301 || STMT_VINFO_LIVE_P (vinfo_for_stmt (pattern_stmt
))))
304 stmt_info
= vinfo_for_stmt (pattern_stmt
);
305 if (dump_enabled_p ())
307 dump_printf_loc (MSG_NOTE
, vect_location
,
308 "==> examining pattern statement: ");
309 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
, stmt
, 0);
314 if (dump_enabled_p ())
315 dump_printf_loc (MSG_NOTE
, vect_location
, "skip.\n");
320 else if (STMT_VINFO_IN_PATTERN_P (stmt_info
)
321 && (pattern_stmt
= STMT_VINFO_RELATED_STMT (stmt_info
))
322 && (STMT_VINFO_RELEVANT_P (vinfo_for_stmt (pattern_stmt
))
323 || STMT_VINFO_LIVE_P (vinfo_for_stmt (pattern_stmt
))))
324 analyze_pattern_stmt
= true;
326 /* If a pattern statement has def stmts, analyze them too. */
327 if (is_pattern_stmt_p (stmt_info
))
329 if (pattern_def_seq
== NULL
)
331 pattern_def_seq
= STMT_VINFO_PATTERN_DEF_SEQ (stmt_info
);
332 pattern_def_si
= gsi_start (pattern_def_seq
);
334 else if (!gsi_end_p (pattern_def_si
))
335 gsi_next (&pattern_def_si
);
336 if (pattern_def_seq
!= NULL
)
338 gimple
*pattern_def_stmt
= NULL
;
339 stmt_vec_info pattern_def_stmt_info
= NULL
;
341 while (!gsi_end_p (pattern_def_si
))
343 pattern_def_stmt
= gsi_stmt (pattern_def_si
);
344 pattern_def_stmt_info
345 = vinfo_for_stmt (pattern_def_stmt
);
346 if (STMT_VINFO_RELEVANT_P (pattern_def_stmt_info
)
347 || STMT_VINFO_LIVE_P (pattern_def_stmt_info
))
349 gsi_next (&pattern_def_si
);
352 if (!gsi_end_p (pattern_def_si
))
354 if (dump_enabled_p ())
356 dump_printf_loc (MSG_NOTE
, vect_location
,
357 "==> examining pattern def stmt: ");
358 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
,
359 pattern_def_stmt
, 0);
362 stmt
= pattern_def_stmt
;
363 stmt_info
= pattern_def_stmt_info
;
367 pattern_def_si
= gsi_none ();
368 analyze_pattern_stmt
= false;
372 analyze_pattern_stmt
= false;
375 if (gimple_get_lhs (stmt
) == NULL_TREE
376 /* MASK_STORE has no lhs, but is ok. */
377 && (!is_gimple_call (stmt
)
378 || !gimple_call_internal_p (stmt
)
379 || gimple_call_internal_fn (stmt
) != IFN_MASK_STORE
))
381 if (is_gimple_call (stmt
))
383 /* Ignore calls with no lhs. These must be calls to
384 #pragma omp simd functions, and what vectorization factor
385 it really needs can't be determined until
386 vectorizable_simd_clone_call. */
387 if (!analyze_pattern_stmt
&& gsi_end_p (pattern_def_si
))
389 pattern_def_seq
= NULL
;
394 if (dump_enabled_p ())
396 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
397 "not vectorized: irregular stmt.");
398 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
, stmt
,
404 if (VECTOR_MODE_P (TYPE_MODE (gimple_expr_type (stmt
))))
406 if (dump_enabled_p ())
408 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
409 "not vectorized: vector stmt in loop:");
410 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
, stmt
, 0);
417 if (STMT_VINFO_VECTYPE (stmt_info
))
419 /* The only case when a vectype had been already set is for stmts
420 that contain a dataref, or for "pattern-stmts" (stmts
421 generated by the vectorizer to represent/replace a certain
423 gcc_assert (STMT_VINFO_DATA_REF (stmt_info
)
424 || is_pattern_stmt_p (stmt_info
)
425 || !gsi_end_p (pattern_def_si
));
426 vectype
= STMT_VINFO_VECTYPE (stmt_info
);
430 gcc_assert (!STMT_VINFO_DATA_REF (stmt_info
));
431 if (gimple_call_internal_p (stmt
, IFN_MASK_STORE
))
432 scalar_type
= TREE_TYPE (gimple_call_arg (stmt
, 3));
434 scalar_type
= TREE_TYPE (gimple_get_lhs (stmt
));
436 /* Bool ops don't participate in vectorization factor
437 computation. For comparison use compared types to
439 if (VECT_SCALAR_BOOLEAN_TYPE_P (scalar_type
)
440 && is_gimple_assign (stmt
)
441 && gimple_assign_rhs_code (stmt
) != COND_EXPR
)
443 if (STMT_VINFO_RELEVANT_P (stmt_info
)
444 || STMT_VINFO_LIVE_P (stmt_info
))
445 mask_producers
.safe_push (stmt_info
);
448 if (TREE_CODE_CLASS (gimple_assign_rhs_code (stmt
))
450 && !VECT_SCALAR_BOOLEAN_TYPE_P
451 (TREE_TYPE (gimple_assign_rhs1 (stmt
))))
452 scalar_type
= TREE_TYPE (gimple_assign_rhs1 (stmt
));
455 if (!analyze_pattern_stmt
&& gsi_end_p (pattern_def_si
))
457 pattern_def_seq
= NULL
;
464 if (dump_enabled_p ())
466 dump_printf_loc (MSG_NOTE
, vect_location
,
467 "get vectype for scalar type: ");
468 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, scalar_type
);
469 dump_printf (MSG_NOTE
, "\n");
471 vectype
= get_vectype_for_scalar_type (scalar_type
);
474 if (dump_enabled_p ())
476 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
477 "not vectorized: unsupported "
479 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
,
481 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
487 STMT_VINFO_VECTYPE (stmt_info
) = vectype
;
489 if (dump_enabled_p ())
491 dump_printf_loc (MSG_NOTE
, vect_location
, "vectype: ");
492 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, vectype
);
493 dump_printf (MSG_NOTE
, "\n");
497 /* Don't try to compute VF out scalar types if we stmt
498 produces boolean vector. Use result vectype instead. */
499 if (VECTOR_BOOLEAN_TYPE_P (vectype
))
500 vf_vectype
= vectype
;
503 /* The vectorization factor is according to the smallest
504 scalar type (or the largest vector size, but we only
505 support one vector size per loop). */
507 scalar_type
= vect_get_smallest_scalar_type (stmt
, &dummy
,
509 if (dump_enabled_p ())
511 dump_printf_loc (MSG_NOTE
, vect_location
,
512 "get vectype for scalar type: ");
513 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, scalar_type
);
514 dump_printf (MSG_NOTE
, "\n");
516 vf_vectype
= get_vectype_for_scalar_type (scalar_type
);
520 if (dump_enabled_p ())
522 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
523 "not vectorized: unsupported data-type ");
524 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
,
526 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
531 if ((GET_MODE_SIZE (TYPE_MODE (vectype
))
532 != GET_MODE_SIZE (TYPE_MODE (vf_vectype
))))
534 if (dump_enabled_p ())
536 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
537 "not vectorized: different sized vector "
538 "types in statement, ");
539 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
,
541 dump_printf (MSG_MISSED_OPTIMIZATION
, " and ");
542 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
,
544 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
549 if (dump_enabled_p ())
551 dump_printf_loc (MSG_NOTE
, vect_location
, "vectype: ");
552 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, vf_vectype
);
553 dump_printf (MSG_NOTE
, "\n");
556 nunits
= TYPE_VECTOR_SUBPARTS (vf_vectype
);
557 if (dump_enabled_p ())
558 dump_printf_loc (MSG_NOTE
, vect_location
, "nunits = %d\n", nunits
);
559 if (!vectorization_factor
560 || (nunits
> vectorization_factor
))
561 vectorization_factor
= nunits
;
563 if (!analyze_pattern_stmt
&& gsi_end_p (pattern_def_si
))
565 pattern_def_seq
= NULL
;
571 /* TODO: Analyze cost. Decide if worth while to vectorize. */
572 if (dump_enabled_p ())
573 dump_printf_loc (MSG_NOTE
, vect_location
, "vectorization factor = %d\n",
574 vectorization_factor
);
575 if (vectorization_factor
<= 1)
577 if (dump_enabled_p ())
578 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
579 "not vectorized: unsupported data-type\n");
582 LOOP_VINFO_VECT_FACTOR (loop_vinfo
) = vectorization_factor
;
584 for (i
= 0; i
< mask_producers
.length (); i
++)
586 tree mask_type
= NULL
;
588 stmt
= STMT_VINFO_STMT (mask_producers
[i
]);
590 if (is_gimple_assign (stmt
)
591 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt
)) == tcc_comparison
592 && !VECT_SCALAR_BOOLEAN_TYPE_P
593 (TREE_TYPE (gimple_assign_rhs1 (stmt
))))
595 scalar_type
= TREE_TYPE (gimple_assign_rhs1 (stmt
));
596 mask_type
= get_mask_type_for_scalar_type (scalar_type
);
600 if (dump_enabled_p ())
601 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
602 "not vectorized: unsupported mask\n");
611 enum vect_def_type dt
;
613 FOR_EACH_SSA_TREE_OPERAND (rhs
, stmt
, iter
, SSA_OP_USE
)
615 if (!vect_is_simple_use (rhs
, mask_producers
[i
]->vinfo
,
616 &def_stmt
, &dt
, &vectype
))
618 if (dump_enabled_p ())
620 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
621 "not vectorized: can't compute mask type "
623 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
, stmt
,
629 /* No vectype probably means external definition.
630 Allow it in case there is another operand which
631 allows to determine mask type. */
637 else if (TYPE_VECTOR_SUBPARTS (mask_type
)
638 != TYPE_VECTOR_SUBPARTS (vectype
))
640 if (dump_enabled_p ())
642 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
643 "not vectorized: different sized masks "
644 "types in statement, ");
645 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
,
647 dump_printf (MSG_MISSED_OPTIMIZATION
, " and ");
648 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
,
650 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
654 else if (VECTOR_BOOLEAN_TYPE_P (mask_type
)
655 != VECTOR_BOOLEAN_TYPE_P (vectype
))
657 if (dump_enabled_p ())
659 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
660 "not vectorized: mixed mask and "
661 "nonmask vector types in statement, ");
662 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
,
664 dump_printf (MSG_MISSED_OPTIMIZATION
, " and ");
665 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
,
667 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
673 /* We may compare boolean value loaded as vector of integers.
674 Fix mask_type in such case. */
676 && !VECTOR_BOOLEAN_TYPE_P (mask_type
)
677 && gimple_code (stmt
) == GIMPLE_ASSIGN
678 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt
)) == tcc_comparison
)
679 mask_type
= build_same_sized_truth_vector_type (mask_type
);
682 /* No mask_type should mean loop invariant predicate.
683 This is probably a subject for optimization in
687 if (dump_enabled_p ())
689 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
690 "not vectorized: can't compute mask type "
692 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
, stmt
,
698 STMT_VINFO_VECTYPE (mask_producers
[i
]) = mask_type
;
705 /* Function vect_is_simple_iv_evolution.
707 FORNOW: A simple evolution of an induction variables in the loop is
708 considered a polynomial evolution. */
711 vect_is_simple_iv_evolution (unsigned loop_nb
, tree access_fn
, tree
* init
,
716 tree evolution_part
= evolution_part_in_loop_num (access_fn
, loop_nb
);
719 /* When there is no evolution in this loop, the evolution function
721 if (evolution_part
== NULL_TREE
)
724 /* When the evolution is a polynomial of degree >= 2
725 the evolution function is not "simple". */
726 if (tree_is_chrec (evolution_part
))
729 step_expr
= evolution_part
;
730 init_expr
= unshare_expr (initial_condition_in_loop_num (access_fn
, loop_nb
));
732 if (dump_enabled_p ())
734 dump_printf_loc (MSG_NOTE
, vect_location
, "step: ");
735 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, step_expr
);
736 dump_printf (MSG_NOTE
, ", init: ");
737 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, init_expr
);
738 dump_printf (MSG_NOTE
, "\n");
744 if (TREE_CODE (step_expr
) != INTEGER_CST
745 && (TREE_CODE (step_expr
) != SSA_NAME
746 || ((bb
= gimple_bb (SSA_NAME_DEF_STMT (step_expr
)))
747 && flow_bb_inside_loop_p (get_loop (cfun
, loop_nb
), bb
))
748 || (!INTEGRAL_TYPE_P (TREE_TYPE (step_expr
))
749 && (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (step_expr
))
750 || !flag_associative_math
)))
751 && (TREE_CODE (step_expr
) != REAL_CST
752 || !flag_associative_math
))
754 if (dump_enabled_p ())
755 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
763 /* Function vect_analyze_scalar_cycles_1.
765 Examine the cross iteration def-use cycles of scalar variables
766 in LOOP. LOOP_VINFO represents the loop that is now being
767 considered for vectorization (can be LOOP, or an outer-loop
771 vect_analyze_scalar_cycles_1 (loop_vec_info loop_vinfo
, struct loop
*loop
)
773 basic_block bb
= loop
->header
;
775 auto_vec
<gimple
*, 64> worklist
;
779 if (dump_enabled_p ())
780 dump_printf_loc (MSG_NOTE
, vect_location
,
781 "=== vect_analyze_scalar_cycles ===\n");
783 /* First - identify all inductions. Reduction detection assumes that all the
784 inductions have been identified, therefore, this order must not be
786 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
788 gphi
*phi
= gsi
.phi ();
789 tree access_fn
= NULL
;
790 tree def
= PHI_RESULT (phi
);
791 stmt_vec_info stmt_vinfo
= vinfo_for_stmt (phi
);
793 if (dump_enabled_p ())
795 dump_printf_loc (MSG_NOTE
, vect_location
, "Analyze phi: ");
796 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
, phi
, 0);
799 /* Skip virtual phi's. The data dependences that are associated with
800 virtual defs/uses (i.e., memory accesses) are analyzed elsewhere. */
801 if (virtual_operand_p (def
))
804 STMT_VINFO_DEF_TYPE (stmt_vinfo
) = vect_unknown_def_type
;
806 /* Analyze the evolution function. */
807 access_fn
= analyze_scalar_evolution (loop
, def
);
810 STRIP_NOPS (access_fn
);
811 if (dump_enabled_p ())
813 dump_printf_loc (MSG_NOTE
, vect_location
,
814 "Access function of PHI: ");
815 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, access_fn
);
816 dump_printf (MSG_NOTE
, "\n");
818 STMT_VINFO_LOOP_PHI_EVOLUTION_BASE_UNCHANGED (stmt_vinfo
)
819 = initial_condition_in_loop_num (access_fn
, loop
->num
);
820 STMT_VINFO_LOOP_PHI_EVOLUTION_PART (stmt_vinfo
)
821 = evolution_part_in_loop_num (access_fn
, loop
->num
);
825 || !vect_is_simple_iv_evolution (loop
->num
, access_fn
, &init
, &step
)
826 || (LOOP_VINFO_LOOP (loop_vinfo
) != loop
827 && TREE_CODE (step
) != INTEGER_CST
))
829 worklist
.safe_push (phi
);
833 gcc_assert (STMT_VINFO_LOOP_PHI_EVOLUTION_BASE_UNCHANGED (stmt_vinfo
)
835 gcc_assert (STMT_VINFO_LOOP_PHI_EVOLUTION_PART (stmt_vinfo
) != NULL_TREE
);
837 if (dump_enabled_p ())
838 dump_printf_loc (MSG_NOTE
, vect_location
, "Detected induction.\n");
839 STMT_VINFO_DEF_TYPE (stmt_vinfo
) = vect_induction_def
;
843 /* Second - identify all reductions and nested cycles. */
844 while (worklist
.length () > 0)
846 gimple
*phi
= worklist
.pop ();
847 tree def
= PHI_RESULT (phi
);
848 stmt_vec_info stmt_vinfo
= vinfo_for_stmt (phi
);
851 if (dump_enabled_p ())
853 dump_printf_loc (MSG_NOTE
, vect_location
, "Analyze phi: ");
854 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
, phi
, 0);
857 gcc_assert (!virtual_operand_p (def
)
858 && STMT_VINFO_DEF_TYPE (stmt_vinfo
) == vect_unknown_def_type
);
860 reduc_stmt
= vect_force_simple_reduction (loop_vinfo
, phi
,
861 &double_reduc
, false);
866 if (dump_enabled_p ())
867 dump_printf_loc (MSG_NOTE
, vect_location
,
868 "Detected double reduction.\n");
870 STMT_VINFO_DEF_TYPE (stmt_vinfo
) = vect_double_reduction_def
;
871 STMT_VINFO_DEF_TYPE (vinfo_for_stmt (reduc_stmt
)) =
872 vect_double_reduction_def
;
876 if (loop
!= LOOP_VINFO_LOOP (loop_vinfo
))
878 if (dump_enabled_p ())
879 dump_printf_loc (MSG_NOTE
, vect_location
,
880 "Detected vectorizable nested cycle.\n");
882 STMT_VINFO_DEF_TYPE (stmt_vinfo
) = vect_nested_cycle
;
883 STMT_VINFO_DEF_TYPE (vinfo_for_stmt (reduc_stmt
)) =
888 if (dump_enabled_p ())
889 dump_printf_loc (MSG_NOTE
, vect_location
,
890 "Detected reduction.\n");
892 STMT_VINFO_DEF_TYPE (stmt_vinfo
) = vect_reduction_def
;
893 STMT_VINFO_DEF_TYPE (vinfo_for_stmt (reduc_stmt
)) =
895 /* Store the reduction cycles for possible vectorization in
896 loop-aware SLP if it was not detected as reduction
898 if (! GROUP_FIRST_ELEMENT (vinfo_for_stmt (reduc_stmt
)))
899 LOOP_VINFO_REDUCTIONS (loop_vinfo
).safe_push (reduc_stmt
);
904 if (dump_enabled_p ())
905 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
906 "Unknown def-use cycle pattern.\n");
911 /* Function vect_analyze_scalar_cycles.
913 Examine the cross iteration def-use cycles of scalar variables, by
914 analyzing the loop-header PHIs of scalar variables. Classify each
915 cycle as one of the following: invariant, induction, reduction, unknown.
916 We do that for the loop represented by LOOP_VINFO, and also to its
917 inner-loop, if exists.
918 Examples for scalar cycles:
933 vect_analyze_scalar_cycles (loop_vec_info loop_vinfo
)
935 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
937 vect_analyze_scalar_cycles_1 (loop_vinfo
, loop
);
939 /* When vectorizing an outer-loop, the inner-loop is executed sequentially.
940 Reductions in such inner-loop therefore have different properties than
941 the reductions in the nest that gets vectorized:
942 1. When vectorized, they are executed in the same order as in the original
943 scalar loop, so we can't change the order of computation when
945 2. FIXME: Inner-loop reductions can be used in the inner-loop, so the
946 current checks are too strict. */
949 vect_analyze_scalar_cycles_1 (loop_vinfo
, loop
->inner
);
952 /* Transfer group and reduction information from STMT to its pattern stmt. */
955 vect_fixup_reduc_chain (gimple
*stmt
)
957 gimple
*firstp
= STMT_VINFO_RELATED_STMT (vinfo_for_stmt (stmt
));
959 gcc_assert (!GROUP_FIRST_ELEMENT (vinfo_for_stmt (firstp
))
960 && GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt
)));
961 GROUP_SIZE (vinfo_for_stmt (firstp
)) = GROUP_SIZE (vinfo_for_stmt (stmt
));
964 stmtp
= STMT_VINFO_RELATED_STMT (vinfo_for_stmt (stmt
));
965 GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmtp
)) = firstp
;
966 stmt
= GROUP_NEXT_ELEMENT (vinfo_for_stmt (stmt
));
968 GROUP_NEXT_ELEMENT (vinfo_for_stmt (stmtp
))
969 = STMT_VINFO_RELATED_STMT (vinfo_for_stmt (stmt
));
972 STMT_VINFO_DEF_TYPE (vinfo_for_stmt (stmtp
)) = vect_reduction_def
;
975 /* Fixup scalar cycles that now have their stmts detected as patterns. */
978 vect_fixup_scalar_cycles_with_patterns (loop_vec_info loop_vinfo
)
983 FOR_EACH_VEC_ELT (LOOP_VINFO_REDUCTION_CHAINS (loop_vinfo
), i
, first
)
984 if (STMT_VINFO_IN_PATTERN_P (vinfo_for_stmt (first
)))
986 gimple
*next
= GROUP_NEXT_ELEMENT (vinfo_for_stmt (first
));
989 if (! STMT_VINFO_IN_PATTERN_P (vinfo_for_stmt (next
)))
991 next
= GROUP_NEXT_ELEMENT (vinfo_for_stmt (next
));
993 /* If not all stmt in the chain are patterns try to handle
994 the chain without patterns. */
997 vect_fixup_reduc_chain (first
);
998 LOOP_VINFO_REDUCTION_CHAINS (loop_vinfo
)[i
]
999 = STMT_VINFO_RELATED_STMT (vinfo_for_stmt (first
));
1004 /* Function vect_get_loop_niters.
1006 Determine how many iterations the loop is executed and place it
1007 in NUMBER_OF_ITERATIONS. Place the number of latch iterations
1008 in NUMBER_OF_ITERATIONSM1. Place the condition under which the
1009 niter information holds in ASSUMPTIONS.
1011 Return the loop exit condition. */
1015 vect_get_loop_niters (struct loop
*loop
, tree
*assumptions
,
1016 tree
*number_of_iterations
, tree
*number_of_iterationsm1
)
1018 edge exit
= single_exit (loop
);
1019 struct tree_niter_desc niter_desc
;
1020 tree niter_assumptions
, niter
, may_be_zero
;
1021 gcond
*cond
= get_loop_exit_condition (loop
);
1023 *assumptions
= boolean_true_node
;
1024 *number_of_iterationsm1
= chrec_dont_know
;
1025 *number_of_iterations
= chrec_dont_know
;
1026 if (dump_enabled_p ())
1027 dump_printf_loc (MSG_NOTE
, vect_location
,
1028 "=== get_loop_niters ===\n");
1033 niter
= chrec_dont_know
;
1034 may_be_zero
= NULL_TREE
;
1035 niter_assumptions
= boolean_true_node
;
1036 if (!number_of_iterations_exit_assumptions (loop
, exit
, &niter_desc
, NULL
)
1037 || chrec_contains_undetermined (niter_desc
.niter
))
1040 niter_assumptions
= niter_desc
.assumptions
;
1041 may_be_zero
= niter_desc
.may_be_zero
;
1042 niter
= niter_desc
.niter
;
1044 if (may_be_zero
&& integer_zerop (may_be_zero
))
1045 may_be_zero
= NULL_TREE
;
1049 if (COMPARISON_CLASS_P (may_be_zero
))
1051 /* Try to combine may_be_zero with assumptions, this can simplify
1052 computation of niter expression. */
1053 if (niter_assumptions
&& !integer_nonzerop (niter_assumptions
))
1054 niter_assumptions
= fold_build2 (TRUTH_AND_EXPR
, boolean_type_node
,
1056 fold_build1 (TRUTH_NOT_EXPR
,
1060 niter
= fold_build3 (COND_EXPR
, TREE_TYPE (niter
), may_be_zero
,
1061 build_int_cst (TREE_TYPE (niter
), 0), niter
);
1063 may_be_zero
= NULL_TREE
;
1065 else if (integer_nonzerop (may_be_zero
))
1067 *number_of_iterationsm1
= build_int_cst (TREE_TYPE (niter
), 0);
1068 *number_of_iterations
= build_int_cst (TREE_TYPE (niter
), 1);
1075 *assumptions
= niter_assumptions
;
1076 *number_of_iterationsm1
= niter
;
1078 /* We want the number of loop header executions which is the number
1079 of latch executions plus one.
1080 ??? For UINT_MAX latch executions this number overflows to zero
1081 for loops like do { n++; } while (n != 0); */
1082 if (niter
&& !chrec_contains_undetermined (niter
))
1083 niter
= fold_build2 (PLUS_EXPR
, TREE_TYPE (niter
), unshare_expr (niter
),
1084 build_int_cst (TREE_TYPE (niter
), 1));
1085 *number_of_iterations
= niter
;
1090 /* Function bb_in_loop_p
1092 Used as predicate for dfs order traversal of the loop bbs. */
1095 bb_in_loop_p (const_basic_block bb
, const void *data
)
1097 const struct loop
*const loop
= (const struct loop
*)data
;
1098 if (flow_bb_inside_loop_p (loop
, bb
))
1104 /* Create and initialize a new loop_vec_info struct for LOOP_IN, as well as
1105 stmt_vec_info structs for all the stmts in LOOP_IN. */
1107 _loop_vec_info::_loop_vec_info (struct loop
*loop_in
)
1108 : vec_info (vec_info::loop
, init_cost (loop_in
)),
1110 bbs (XCNEWVEC (basic_block
, loop
->num_nodes
)),
1111 num_itersm1 (NULL_TREE
),
1112 num_iters (NULL_TREE
),
1113 num_iters_unchanged (NULL_TREE
),
1114 num_iters_assumptions (NULL_TREE
),
1116 versioning_threshold (0),
1117 vectorization_factor (0),
1118 max_vectorization_factor (0),
1119 unaligned_dr (NULL
),
1120 peeling_for_alignment (0),
1122 slp_unrolling_factor (1),
1123 single_scalar_iteration_cost (0),
1124 vectorizable (false),
1125 peeling_for_gaps (false),
1126 peeling_for_niter (false),
1127 operands_swapped (false),
1128 no_data_dependencies (false),
1129 has_mask_store (false),
1131 orig_loop_info (NULL
)
1133 /* Create/Update stmt_info for all stmts in the loop. */
1134 basic_block
*body
= get_loop_body (loop
);
1135 for (unsigned int i
= 0; i
< loop
->num_nodes
; i
++)
1137 basic_block bb
= body
[i
];
1138 gimple_stmt_iterator si
;
1140 for (si
= gsi_start_phis (bb
); !gsi_end_p (si
); gsi_next (&si
))
1142 gimple
*phi
= gsi_stmt (si
);
1143 gimple_set_uid (phi
, 0);
1144 set_vinfo_for_stmt (phi
, new_stmt_vec_info (phi
, this));
1147 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); gsi_next (&si
))
1149 gimple
*stmt
= gsi_stmt (si
);
1150 gimple_set_uid (stmt
, 0);
1151 set_vinfo_for_stmt (stmt
, new_stmt_vec_info (stmt
, this));
1156 /* CHECKME: We want to visit all BBs before their successors (except for
1157 latch blocks, for which this assertion wouldn't hold). In the simple
1158 case of the loop forms we allow, a dfs order of the BBs would the same
1159 as reversed postorder traversal, so we are safe. */
1161 unsigned int nbbs
= dfs_enumerate_from (loop
->header
, 0, bb_in_loop_p
,
1162 bbs
, loop
->num_nodes
, loop
);
1163 gcc_assert (nbbs
== loop
->num_nodes
);
1167 /* Free all memory used by the _loop_vec_info, as well as all the
1168 stmt_vec_info structs of all the stmts in the loop. */
1170 _loop_vec_info::~_loop_vec_info ()
1173 gimple_stmt_iterator si
;
1176 nbbs
= loop
->num_nodes
;
1177 for (j
= 0; j
< nbbs
; j
++)
1179 basic_block bb
= bbs
[j
];
1180 for (si
= gsi_start_phis (bb
); !gsi_end_p (si
); gsi_next (&si
))
1181 free_stmt_vec_info (gsi_stmt (si
));
1183 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); )
1185 gimple
*stmt
= gsi_stmt (si
);
1187 /* We may have broken canonical form by moving a constant
1188 into RHS1 of a commutative op. Fix such occurrences. */
1189 if (operands_swapped
&& is_gimple_assign (stmt
))
1191 enum tree_code code
= gimple_assign_rhs_code (stmt
);
1193 if ((code
== PLUS_EXPR
1194 || code
== POINTER_PLUS_EXPR
1195 || code
== MULT_EXPR
)
1196 && CONSTANT_CLASS_P (gimple_assign_rhs1 (stmt
)))
1197 swap_ssa_operands (stmt
,
1198 gimple_assign_rhs1_ptr (stmt
),
1199 gimple_assign_rhs2_ptr (stmt
));
1200 else if (code
== COND_EXPR
1201 && CONSTANT_CLASS_P (gimple_assign_rhs2 (stmt
)))
1203 tree cond_expr
= gimple_assign_rhs1 (stmt
);
1204 enum tree_code cond_code
= TREE_CODE (cond_expr
);
1206 if (TREE_CODE_CLASS (cond_code
) == tcc_comparison
)
1208 bool honor_nans
= HONOR_NANS (TREE_OPERAND (cond_expr
,
1210 cond_code
= invert_tree_comparison (cond_code
,
1212 if (cond_code
!= ERROR_MARK
)
1214 TREE_SET_CODE (cond_expr
, cond_code
);
1215 swap_ssa_operands (stmt
,
1216 gimple_assign_rhs2_ptr (stmt
),
1217 gimple_assign_rhs3_ptr (stmt
));
1223 /* Free stmt_vec_info. */
1224 free_stmt_vec_info (stmt
);
1235 /* Calculate the cost of one scalar iteration of the loop. */
1237 vect_compute_single_scalar_iteration_cost (loop_vec_info loop_vinfo
)
1239 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
1240 basic_block
*bbs
= LOOP_VINFO_BBS (loop_vinfo
);
1241 int nbbs
= loop
->num_nodes
, factor
, scalar_single_iter_cost
= 0;
1242 int innerloop_iters
, i
;
1244 /* Count statements in scalar loop. Using this as scalar cost for a single
1247 TODO: Add outer loop support.
1249 TODO: Consider assigning different costs to different scalar
1253 innerloop_iters
= 1;
1255 innerloop_iters
= 50; /* FIXME */
1257 for (i
= 0; i
< nbbs
; i
++)
1259 gimple_stmt_iterator si
;
1260 basic_block bb
= bbs
[i
];
1262 if (bb
->loop_father
== loop
->inner
)
1263 factor
= innerloop_iters
;
1267 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); gsi_next (&si
))
1269 gimple
*stmt
= gsi_stmt (si
);
1270 stmt_vec_info stmt_info
= vinfo_for_stmt (stmt
);
1272 if (!is_gimple_assign (stmt
) && !is_gimple_call (stmt
))
1275 /* Skip stmts that are not vectorized inside the loop. */
1277 && !STMT_VINFO_RELEVANT_P (stmt_info
)
1278 && (!STMT_VINFO_LIVE_P (stmt_info
)
1279 || !VECTORIZABLE_CYCLE_DEF (STMT_VINFO_DEF_TYPE (stmt_info
)))
1280 && !STMT_VINFO_IN_PATTERN_P (stmt_info
))
1283 vect_cost_for_stmt kind
;
1284 if (STMT_VINFO_DATA_REF (stmt_info
))
1286 if (DR_IS_READ (STMT_VINFO_DATA_REF (stmt_info
)))
1289 kind
= scalar_store
;
1294 scalar_single_iter_cost
1295 += record_stmt_cost (&LOOP_VINFO_SCALAR_ITERATION_COST (loop_vinfo
),
1296 factor
, kind
, stmt_info
, 0, vect_prologue
);
1299 LOOP_VINFO_SINGLE_SCALAR_ITERATION_COST (loop_vinfo
)
1300 = scalar_single_iter_cost
;
1304 /* Function vect_analyze_loop_form_1.
1306 Verify that certain CFG restrictions hold, including:
1307 - the loop has a pre-header
1308 - the loop has a single entry and exit
1309 - the loop exit condition is simple enough
1310 - the number of iterations can be analyzed, i.e, a countable loop. The
1311 niter could be analyzed under some assumptions. */
1314 vect_analyze_loop_form_1 (struct loop
*loop
, gcond
**loop_cond
,
1315 tree
*assumptions
, tree
*number_of_iterationsm1
,
1316 tree
*number_of_iterations
, gcond
**inner_loop_cond
)
1318 if (dump_enabled_p ())
1319 dump_printf_loc (MSG_NOTE
, vect_location
,
1320 "=== vect_analyze_loop_form ===\n");
1322 /* Different restrictions apply when we are considering an inner-most loop,
1323 vs. an outer (nested) loop.
1324 (FORNOW. May want to relax some of these restrictions in the future). */
1328 /* Inner-most loop. We currently require that the number of BBs is
1329 exactly 2 (the header and latch). Vectorizable inner-most loops
1340 if (loop
->num_nodes
!= 2)
1342 if (dump_enabled_p ())
1343 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1344 "not vectorized: control flow in loop.\n");
1348 if (empty_block_p (loop
->header
))
1350 if (dump_enabled_p ())
1351 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1352 "not vectorized: empty loop.\n");
1358 struct loop
*innerloop
= loop
->inner
;
1361 /* Nested loop. We currently require that the loop is doubly-nested,
1362 contains a single inner loop, and the number of BBs is exactly 5.
1363 Vectorizable outer-loops look like this:
1375 The inner-loop has the properties expected of inner-most loops
1376 as described above. */
1378 if ((loop
->inner
)->inner
|| (loop
->inner
)->next
)
1380 if (dump_enabled_p ())
1381 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1382 "not vectorized: multiple nested loops.\n");
1386 if (loop
->num_nodes
!= 5)
1388 if (dump_enabled_p ())
1389 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1390 "not vectorized: control flow in loop.\n");
1394 entryedge
= loop_preheader_edge (innerloop
);
1395 if (entryedge
->src
!= loop
->header
1396 || !single_exit (innerloop
)
1397 || single_exit (innerloop
)->dest
!= EDGE_PRED (loop
->latch
, 0)->src
)
1399 if (dump_enabled_p ())
1400 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1401 "not vectorized: unsupported outerloop form.\n");
1405 /* Analyze the inner-loop. */
1406 tree inner_niterm1
, inner_niter
, inner_assumptions
;
1407 if (! vect_analyze_loop_form_1 (loop
->inner
, inner_loop_cond
,
1408 &inner_assumptions
, &inner_niterm1
,
1410 /* Don't support analyzing niter under assumptions for inner
1412 || !integer_onep (inner_assumptions
))
1414 if (dump_enabled_p ())
1415 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1416 "not vectorized: Bad inner loop.\n");
1420 if (!expr_invariant_in_loop_p (loop
, inner_niter
))
1422 if (dump_enabled_p ())
1423 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1424 "not vectorized: inner-loop count not"
1429 if (dump_enabled_p ())
1430 dump_printf_loc (MSG_NOTE
, vect_location
,
1431 "Considering outer-loop vectorization.\n");
1434 if (!single_exit (loop
)
1435 || EDGE_COUNT (loop
->header
->preds
) != 2)
1437 if (dump_enabled_p ())
1439 if (!single_exit (loop
))
1440 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1441 "not vectorized: multiple exits.\n");
1442 else if (EDGE_COUNT (loop
->header
->preds
) != 2)
1443 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1444 "not vectorized: too many incoming edges.\n");
1449 /* We assume that the loop exit condition is at the end of the loop. i.e,
1450 that the loop is represented as a do-while (with a proper if-guard
1451 before the loop if needed), where the loop header contains all the
1452 executable statements, and the latch is empty. */
1453 if (!empty_block_p (loop
->latch
)
1454 || !gimple_seq_empty_p (phi_nodes (loop
->latch
)))
1456 if (dump_enabled_p ())
1457 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1458 "not vectorized: latch block not empty.\n");
1462 /* Make sure the exit is not abnormal. */
1463 edge e
= single_exit (loop
);
1464 if (e
->flags
& EDGE_ABNORMAL
)
1466 if (dump_enabled_p ())
1467 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1468 "not vectorized: abnormal loop exit edge.\n");
1472 *loop_cond
= vect_get_loop_niters (loop
, assumptions
, number_of_iterations
,
1473 number_of_iterationsm1
);
1476 if (dump_enabled_p ())
1477 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1478 "not vectorized: complicated exit condition.\n");
1482 if (integer_zerop (*assumptions
)
1483 || !*number_of_iterations
1484 || chrec_contains_undetermined (*number_of_iterations
))
1486 if (dump_enabled_p ())
1487 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1488 "not vectorized: number of iterations cannot be "
1493 if (integer_zerop (*number_of_iterations
))
1495 if (dump_enabled_p ())
1496 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1497 "not vectorized: number of iterations = 0.\n");
1504 /* Analyze LOOP form and return a loop_vec_info if it is of suitable form. */
1507 vect_analyze_loop_form (struct loop
*loop
)
1509 tree assumptions
, number_of_iterations
, number_of_iterationsm1
;
1510 gcond
*loop_cond
, *inner_loop_cond
= NULL
;
1512 if (! vect_analyze_loop_form_1 (loop
, &loop_cond
,
1513 &assumptions
, &number_of_iterationsm1
,
1514 &number_of_iterations
, &inner_loop_cond
))
1517 loop_vec_info loop_vinfo
= new _loop_vec_info (loop
);
1518 LOOP_VINFO_NITERSM1 (loop_vinfo
) = number_of_iterationsm1
;
1519 LOOP_VINFO_NITERS (loop_vinfo
) = number_of_iterations
;
1520 LOOP_VINFO_NITERS_UNCHANGED (loop_vinfo
) = number_of_iterations
;
1521 if (!integer_onep (assumptions
))
1523 /* We consider to vectorize this loop by versioning it under
1524 some assumptions. In order to do this, we need to clear
1525 existing information computed by scev and niter analyzer. */
1527 free_numbers_of_iterations_estimates (loop
);
1528 /* Also set flag for this loop so that following scev and niter
1529 analysis are done under the assumptions. */
1530 loop_constraint_set (loop
, LOOP_C_FINITE
);
1531 /* Also record the assumptions for versioning. */
1532 LOOP_VINFO_NITERS_ASSUMPTIONS (loop_vinfo
) = assumptions
;
1535 if (!LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo
))
1537 if (dump_enabled_p ())
1539 dump_printf_loc (MSG_NOTE
, vect_location
,
1540 "Symbolic number of iterations is ");
1541 dump_generic_expr (MSG_NOTE
, TDF_DETAILS
, number_of_iterations
);
1542 dump_printf (MSG_NOTE
, "\n");
1546 STMT_VINFO_TYPE (vinfo_for_stmt (loop_cond
)) = loop_exit_ctrl_vec_info_type
;
1547 if (inner_loop_cond
)
1548 STMT_VINFO_TYPE (vinfo_for_stmt (inner_loop_cond
))
1549 = loop_exit_ctrl_vec_info_type
;
1551 gcc_assert (!loop
->aux
);
1552 loop
->aux
= loop_vinfo
;
1558 /* Scan the loop stmts and dependent on whether there are any (non-)SLP
1559 statements update the vectorization factor. */
1562 vect_update_vf_for_slp (loop_vec_info loop_vinfo
)
1564 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
1565 basic_block
*bbs
= LOOP_VINFO_BBS (loop_vinfo
);
1566 int nbbs
= loop
->num_nodes
;
1567 unsigned int vectorization_factor
;
1570 if (dump_enabled_p ())
1571 dump_printf_loc (MSG_NOTE
, vect_location
,
1572 "=== vect_update_vf_for_slp ===\n");
1574 vectorization_factor
= LOOP_VINFO_VECT_FACTOR (loop_vinfo
);
1575 gcc_assert (vectorization_factor
!= 0);
1577 /* If all the stmts in the loop can be SLPed, we perform only SLP, and
1578 vectorization factor of the loop is the unrolling factor required by
1579 the SLP instances. If that unrolling factor is 1, we say, that we
1580 perform pure SLP on loop - cross iteration parallelism is not
1582 bool only_slp_in_loop
= true;
1583 for (i
= 0; i
< nbbs
; i
++)
1585 basic_block bb
= bbs
[i
];
1586 for (gimple_stmt_iterator si
= gsi_start_bb (bb
); !gsi_end_p (si
);
1589 gimple
*stmt
= gsi_stmt (si
);
1590 stmt_vec_info stmt_info
= vinfo_for_stmt (stmt
);
1591 if (STMT_VINFO_IN_PATTERN_P (stmt_info
)
1592 && STMT_VINFO_RELATED_STMT (stmt_info
))
1594 stmt
= STMT_VINFO_RELATED_STMT (stmt_info
);
1595 stmt_info
= vinfo_for_stmt (stmt
);
1597 if ((STMT_VINFO_RELEVANT_P (stmt_info
)
1598 || VECTORIZABLE_CYCLE_DEF (STMT_VINFO_DEF_TYPE (stmt_info
)))
1599 && !PURE_SLP_STMT (stmt_info
))
1600 /* STMT needs both SLP and loop-based vectorization. */
1601 only_slp_in_loop
= false;
1605 if (only_slp_in_loop
)
1607 dump_printf_loc (MSG_NOTE
, vect_location
,
1608 "Loop contains only SLP stmts\n");
1609 vectorization_factor
= LOOP_VINFO_SLP_UNROLLING_FACTOR (loop_vinfo
);
1613 dump_printf_loc (MSG_NOTE
, vect_location
,
1614 "Loop contains SLP and non-SLP stmts\n");
1615 vectorization_factor
1616 = least_common_multiple (vectorization_factor
,
1617 LOOP_VINFO_SLP_UNROLLING_FACTOR (loop_vinfo
));
1620 LOOP_VINFO_VECT_FACTOR (loop_vinfo
) = vectorization_factor
;
1621 if (dump_enabled_p ())
1622 dump_printf_loc (MSG_NOTE
, vect_location
,
1623 "Updating vectorization factor to %d\n",
1624 vectorization_factor
);
1627 /* Function vect_analyze_loop_operations.
1629 Scan the loop stmts and make sure they are all vectorizable. */
1632 vect_analyze_loop_operations (loop_vec_info loop_vinfo
)
1634 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
1635 basic_block
*bbs
= LOOP_VINFO_BBS (loop_vinfo
);
1636 int nbbs
= loop
->num_nodes
;
1638 stmt_vec_info stmt_info
;
1639 bool need_to_vectorize
= false;
1642 if (dump_enabled_p ())
1643 dump_printf_loc (MSG_NOTE
, vect_location
,
1644 "=== vect_analyze_loop_operations ===\n");
1646 for (i
= 0; i
< nbbs
; i
++)
1648 basic_block bb
= bbs
[i
];
1650 for (gphi_iterator si
= gsi_start_phis (bb
); !gsi_end_p (si
);
1653 gphi
*phi
= si
.phi ();
1656 stmt_info
= vinfo_for_stmt (phi
);
1657 if (dump_enabled_p ())
1659 dump_printf_loc (MSG_NOTE
, vect_location
, "examining phi: ");
1660 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
, phi
, 0);
1662 if (virtual_operand_p (gimple_phi_result (phi
)))
1665 /* Inner-loop loop-closed exit phi in outer-loop vectorization
1666 (i.e., a phi in the tail of the outer-loop). */
1667 if (! is_loop_header_bb_p (bb
))
1669 /* FORNOW: we currently don't support the case that these phis
1670 are not used in the outerloop (unless it is double reduction,
1671 i.e., this phi is vect_reduction_def), cause this case
1672 requires to actually do something here. */
1673 if (STMT_VINFO_LIVE_P (stmt_info
)
1674 && STMT_VINFO_DEF_TYPE (stmt_info
)
1675 != vect_double_reduction_def
)
1677 if (dump_enabled_p ())
1678 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1679 "Unsupported loop-closed phi in "
1684 /* If PHI is used in the outer loop, we check that its operand
1685 is defined in the inner loop. */
1686 if (STMT_VINFO_RELEVANT_P (stmt_info
))
1689 gimple
*op_def_stmt
;
1691 if (gimple_phi_num_args (phi
) != 1)
1694 phi_op
= PHI_ARG_DEF (phi
, 0);
1695 if (TREE_CODE (phi_op
) != SSA_NAME
)
1698 op_def_stmt
= SSA_NAME_DEF_STMT (phi_op
);
1699 if (gimple_nop_p (op_def_stmt
)
1700 || !flow_bb_inside_loop_p (loop
, gimple_bb (op_def_stmt
))
1701 || !vinfo_for_stmt (op_def_stmt
))
1704 if (STMT_VINFO_RELEVANT (vinfo_for_stmt (op_def_stmt
))
1705 != vect_used_in_outer
1706 && STMT_VINFO_RELEVANT (vinfo_for_stmt (op_def_stmt
))
1707 != vect_used_in_outer_by_reduction
)
1714 gcc_assert (stmt_info
);
1716 if ((STMT_VINFO_RELEVANT (stmt_info
) == vect_used_in_scope
1717 || STMT_VINFO_LIVE_P (stmt_info
))
1718 && STMT_VINFO_DEF_TYPE (stmt_info
) != vect_induction_def
)
1720 /* A scalar-dependence cycle that we don't support. */
1721 if (dump_enabled_p ())
1722 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1723 "not vectorized: scalar dependence cycle.\n");
1727 if (STMT_VINFO_RELEVANT_P (stmt_info
))
1729 need_to_vectorize
= true;
1730 if (STMT_VINFO_DEF_TYPE (stmt_info
) == vect_induction_def
1731 && ! PURE_SLP_STMT (stmt_info
))
1732 ok
= vectorizable_induction (phi
, NULL
, NULL
, NULL
);
1733 else if ((STMT_VINFO_DEF_TYPE (stmt_info
) == vect_reduction_def
1734 || STMT_VINFO_DEF_TYPE (stmt_info
) == vect_nested_cycle
)
1735 && ! PURE_SLP_STMT (stmt_info
))
1736 ok
= vectorizable_reduction (phi
, NULL
, NULL
, NULL
, NULL
);
1739 if (ok
&& STMT_VINFO_LIVE_P (stmt_info
))
1740 ok
= vectorizable_live_operation (phi
, NULL
, NULL
, -1, NULL
);
1744 if (dump_enabled_p ())
1746 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1747 "not vectorized: relevant phi not "
1749 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
, phi
, 0);
1755 for (gimple_stmt_iterator si
= gsi_start_bb (bb
); !gsi_end_p (si
);
1758 gimple
*stmt
= gsi_stmt (si
);
1759 if (!gimple_clobber_p (stmt
)
1760 && !vect_analyze_stmt (stmt
, &need_to_vectorize
, NULL
, NULL
))
1765 /* All operations in the loop are either irrelevant (deal with loop
1766 control, or dead), or only used outside the loop and can be moved
1767 out of the loop (e.g. invariants, inductions). The loop can be
1768 optimized away by scalar optimizations. We're better off not
1769 touching this loop. */
1770 if (!need_to_vectorize
)
1772 if (dump_enabled_p ())
1773 dump_printf_loc (MSG_NOTE
, vect_location
,
1774 "All the computation can be taken out of the loop.\n");
1775 if (dump_enabled_p ())
1776 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1777 "not vectorized: redundant loop. no profit to "
1786 /* Function vect_analyze_loop_2.
1788 Apply a set of analyses on LOOP, and create a loop_vec_info struct
1789 for it. The different analyses will record information in the
1790 loop_vec_info struct. */
1792 vect_analyze_loop_2 (loop_vec_info loop_vinfo
, bool &fatal
)
1795 int max_vf
= MAX_VECTORIZATION_FACTOR
;
1797 unsigned int n_stmts
= 0;
1799 /* The first group of checks is independent of the vector size. */
1802 /* Find all data references in the loop (which correspond to vdefs/vuses)
1803 and analyze their evolution in the loop. */
1805 basic_block
*bbs
= LOOP_VINFO_BBS (loop_vinfo
);
1807 loop_p loop
= LOOP_VINFO_LOOP (loop_vinfo
);
1808 if (!find_loop_nest (loop
, &LOOP_VINFO_LOOP_NEST (loop_vinfo
)))
1810 if (dump_enabled_p ())
1811 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1812 "not vectorized: loop nest containing two "
1813 "or more consecutive inner loops cannot be "
1818 for (unsigned i
= 0; i
< loop
->num_nodes
; i
++)
1819 for (gimple_stmt_iterator gsi
= gsi_start_bb (bbs
[i
]);
1820 !gsi_end_p (gsi
); gsi_next (&gsi
))
1822 gimple
*stmt
= gsi_stmt (gsi
);
1823 if (is_gimple_debug (stmt
))
1826 if (!find_data_references_in_stmt (loop
, stmt
,
1827 &LOOP_VINFO_DATAREFS (loop_vinfo
)))
1829 if (is_gimple_call (stmt
) && loop
->safelen
)
1831 tree fndecl
= gimple_call_fndecl (stmt
), op
;
1832 if (fndecl
!= NULL_TREE
)
1834 cgraph_node
*node
= cgraph_node::get (fndecl
);
1835 if (node
!= NULL
&& node
->simd_clones
!= NULL
)
1837 unsigned int j
, n
= gimple_call_num_args (stmt
);
1838 for (j
= 0; j
< n
; j
++)
1840 op
= gimple_call_arg (stmt
, j
);
1842 || (REFERENCE_CLASS_P (op
)
1843 && get_base_address (op
)))
1846 op
= gimple_call_lhs (stmt
);
1847 /* Ignore #pragma omp declare simd functions
1848 if they don't have data references in the
1849 call stmt itself. */
1853 || (REFERENCE_CLASS_P (op
)
1854 && get_base_address (op
)))))
1859 if (dump_enabled_p ())
1860 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1861 "not vectorized: loop contains function "
1862 "calls or data references that cannot "
1868 /* Analyze the data references and also adjust the minimal
1869 vectorization factor according to the loads and stores. */
1871 ok
= vect_analyze_data_refs (loop_vinfo
, &min_vf
);
1874 if (dump_enabled_p ())
1875 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1876 "bad data references.\n");
1880 /* Classify all cross-iteration scalar data-flow cycles.
1881 Cross-iteration cycles caused by virtual phis are analyzed separately. */
1882 vect_analyze_scalar_cycles (loop_vinfo
);
1884 vect_pattern_recog (loop_vinfo
);
1886 vect_fixup_scalar_cycles_with_patterns (loop_vinfo
);
1888 /* Analyze the access patterns of the data-refs in the loop (consecutive,
1889 complex, etc.). FORNOW: Only handle consecutive access pattern. */
1891 ok
= vect_analyze_data_ref_accesses (loop_vinfo
);
1894 if (dump_enabled_p ())
1895 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1896 "bad data access.\n");
1900 /* Data-flow analysis to detect stmts that do not need to be vectorized. */
1902 ok
= vect_mark_stmts_to_be_vectorized (loop_vinfo
);
1905 if (dump_enabled_p ())
1906 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1907 "unexpected pattern.\n");
1911 /* While the rest of the analysis below depends on it in some way. */
1914 /* Analyze data dependences between the data-refs in the loop
1915 and adjust the maximum vectorization factor according to
1917 FORNOW: fail at the first data dependence that we encounter. */
1919 ok
= vect_analyze_data_ref_dependences (loop_vinfo
, &max_vf
);
1923 if (dump_enabled_p ())
1924 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1925 "bad data dependence.\n");
1928 LOOP_VINFO_MAX_VECT_FACTOR (loop_vinfo
) = max_vf
;
1930 ok
= vect_determine_vectorization_factor (loop_vinfo
);
1933 if (dump_enabled_p ())
1934 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1935 "can't determine vectorization factor.\n");
1938 if (max_vf
< LOOP_VINFO_VECT_FACTOR (loop_vinfo
))
1940 if (dump_enabled_p ())
1941 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1942 "bad data dependence.\n");
1946 /* Compute the scalar iteration cost. */
1947 vect_compute_single_scalar_iteration_cost (loop_vinfo
);
1949 int saved_vectorization_factor
= LOOP_VINFO_VECT_FACTOR (loop_vinfo
);
1950 HOST_WIDE_INT estimated_niter
;
1952 int min_scalar_loop_bound
;
1954 /* Check the SLP opportunities in the loop, analyze and build SLP trees. */
1955 ok
= vect_analyze_slp (loop_vinfo
, n_stmts
);
1959 /* If there are any SLP instances mark them as pure_slp. */
1960 bool slp
= vect_make_slp_decision (loop_vinfo
);
1963 /* Find stmts that need to be both vectorized and SLPed. */
1964 vect_detect_hybrid_slp (loop_vinfo
);
1966 /* Update the vectorization factor based on the SLP decision. */
1967 vect_update_vf_for_slp (loop_vinfo
);
1970 /* This is the point where we can re-start analysis with SLP forced off. */
1973 /* Now the vectorization factor is final. */
1974 unsigned vectorization_factor
= LOOP_VINFO_VECT_FACTOR (loop_vinfo
);
1975 gcc_assert (vectorization_factor
!= 0);
1977 if (LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo
) && dump_enabled_p ())
1978 dump_printf_loc (MSG_NOTE
, vect_location
,
1979 "vectorization_factor = %d, niters = "
1980 HOST_WIDE_INT_PRINT_DEC
"\n", vectorization_factor
,
1981 LOOP_VINFO_INT_NITERS (loop_vinfo
));
1983 HOST_WIDE_INT max_niter
1984 = likely_max_stmt_executions_int (LOOP_VINFO_LOOP (loop_vinfo
));
1985 if ((LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo
)
1986 && (LOOP_VINFO_INT_NITERS (loop_vinfo
) < vectorization_factor
))
1988 && (unsigned HOST_WIDE_INT
) max_niter
< vectorization_factor
))
1990 if (dump_enabled_p ())
1991 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1992 "not vectorized: iteration count smaller than "
1993 "vectorization factor.\n");
1997 /* Analyze the alignment of the data-refs in the loop.
1998 Fail if a data reference is found that cannot be vectorized. */
2000 ok
= vect_analyze_data_refs_alignment (loop_vinfo
);
2003 if (dump_enabled_p ())
2004 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2005 "bad data alignment.\n");
2009 /* Prune the list of ddrs to be tested at run-time by versioning for alias.
2010 It is important to call pruning after vect_analyze_data_ref_accesses,
2011 since we use grouping information gathered by interleaving analysis. */
2012 ok
= vect_prune_runtime_alias_test_list (loop_vinfo
);
2016 /* Do not invoke vect_enhance_data_refs_alignment for eplilogue
2018 if (!LOOP_VINFO_EPILOGUE_P (loop_vinfo
))
2020 /* This pass will decide on using loop versioning and/or loop peeling in
2021 order to enhance the alignment of data references in the loop. */
2022 ok
= vect_enhance_data_refs_alignment (loop_vinfo
);
2025 if (dump_enabled_p ())
2026 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2027 "bad data alignment.\n");
2034 /* Analyze operations in the SLP instances. Note this may
2035 remove unsupported SLP instances which makes the above
2036 SLP kind detection invalid. */
2037 unsigned old_size
= LOOP_VINFO_SLP_INSTANCES (loop_vinfo
).length ();
2038 vect_slp_analyze_operations (loop_vinfo
);
2039 if (LOOP_VINFO_SLP_INSTANCES (loop_vinfo
).length () != old_size
)
2043 /* Scan all the remaining operations in the loop that are not subject
2044 to SLP and make sure they are vectorizable. */
2045 ok
= vect_analyze_loop_operations (loop_vinfo
);
2048 if (dump_enabled_p ())
2049 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2050 "bad operation or unsupported loop bound.\n");
2054 /* If epilog loop is required because of data accesses with gaps,
2055 one additional iteration needs to be peeled. Check if there is
2056 enough iterations for vectorization. */
2057 if (LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo
)
2058 && LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo
))
2060 int vf
= LOOP_VINFO_VECT_FACTOR (loop_vinfo
);
2061 tree scalar_niters
= LOOP_VINFO_NITERSM1 (loop_vinfo
);
2063 if (wi::to_widest (scalar_niters
) < vf
)
2065 if (dump_enabled_p ())
2066 dump_printf_loc (MSG_NOTE
, vect_location
,
2067 "loop has no enough iterations to support"
2068 " peeling for gaps.\n");
2073 /* Analyze cost. Decide if worth while to vectorize. */
2074 int min_profitable_estimate
, min_profitable_iters
;
2075 vect_estimate_min_profitable_iters (loop_vinfo
, &min_profitable_iters
,
2076 &min_profitable_estimate
);
2078 if (min_profitable_iters
< 0)
2080 if (dump_enabled_p ())
2081 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2082 "not vectorized: vectorization not profitable.\n");
2083 if (dump_enabled_p ())
2084 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2085 "not vectorized: vector version will never be "
2090 min_scalar_loop_bound
= (PARAM_VALUE (PARAM_MIN_VECT_LOOP_BOUND
)
2091 * vectorization_factor
);
2093 /* Use the cost model only if it is more conservative than user specified
2095 th
= (unsigned) MAX (min_scalar_loop_bound
, min_profitable_iters
);
2097 LOOP_VINFO_COST_MODEL_THRESHOLD (loop_vinfo
) = th
;
2099 if (LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo
)
2100 && LOOP_VINFO_INT_NITERS (loop_vinfo
) < th
)
2102 if (dump_enabled_p ())
2103 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2104 "not vectorized: vectorization not profitable.\n");
2105 if (dump_enabled_p ())
2106 dump_printf_loc (MSG_NOTE
, vect_location
,
2107 "not vectorized: iteration count smaller than user "
2108 "specified loop bound parameter or minimum profitable "
2109 "iterations (whichever is more conservative).\n");
2114 = estimated_stmt_executions_int (LOOP_VINFO_LOOP (loop_vinfo
));
2115 if (estimated_niter
== -1)
2116 estimated_niter
= max_niter
;
2117 if (estimated_niter
!= -1
2118 && ((unsigned HOST_WIDE_INT
) estimated_niter
2119 < MAX (th
, (unsigned) min_profitable_estimate
)))
2121 if (dump_enabled_p ())
2122 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2123 "not vectorized: estimated iteration count too "
2125 if (dump_enabled_p ())
2126 dump_printf_loc (MSG_NOTE
, vect_location
,
2127 "not vectorized: estimated iteration count smaller "
2128 "than specified loop bound parameter or minimum "
2129 "profitable iterations (whichever is more "
2130 "conservative).\n");
2134 /* Decide whether we need to create an epilogue loop to handle
2135 remaining scalar iterations. */
2136 th
= ((LOOP_VINFO_COST_MODEL_THRESHOLD (loop_vinfo
)
2137 / LOOP_VINFO_VECT_FACTOR (loop_vinfo
))
2138 * LOOP_VINFO_VECT_FACTOR (loop_vinfo
));
2140 if (LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo
)
2141 && LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo
) > 0)
2143 if (ctz_hwi (LOOP_VINFO_INT_NITERS (loop_vinfo
)
2144 - LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo
))
2145 < exact_log2 (LOOP_VINFO_VECT_FACTOR (loop_vinfo
)))
2146 LOOP_VINFO_PEELING_FOR_NITER (loop_vinfo
) = true;
2148 else if (LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo
)
2149 || (tree_ctz (LOOP_VINFO_NITERS (loop_vinfo
))
2150 < (unsigned)exact_log2 (LOOP_VINFO_VECT_FACTOR (loop_vinfo
))
2151 /* In case of versioning, check if the maximum number of
2152 iterations is greater than th. If they are identical,
2153 the epilogue is unnecessary. */
2154 && (!LOOP_REQUIRES_VERSIONING (loop_vinfo
)
2155 || (unsigned HOST_WIDE_INT
) max_niter
> th
)))
2156 LOOP_VINFO_PEELING_FOR_NITER (loop_vinfo
) = true;
2158 /* If an epilogue loop is required make sure we can create one. */
2159 if (LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo
)
2160 || LOOP_VINFO_PEELING_FOR_NITER (loop_vinfo
))
2162 if (dump_enabled_p ())
2163 dump_printf_loc (MSG_NOTE
, vect_location
, "epilog loop required\n");
2164 if (!vect_can_advance_ivs_p (loop_vinfo
)
2165 || !slpeel_can_duplicate_loop_p (LOOP_VINFO_LOOP (loop_vinfo
),
2166 single_exit (LOOP_VINFO_LOOP
2169 if (dump_enabled_p ())
2170 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2171 "not vectorized: can't create required "
2177 /* During peeling, we need to check if number of loop iterations is
2178 enough for both peeled prolog loop and vector loop. This check
2179 can be merged along with threshold check of loop versioning, so
2180 increase threshold for this case if necessary. */
2181 if (LOOP_REQUIRES_VERSIONING (loop_vinfo
))
2183 poly_uint64 niters_th
;
2185 /* Niters for peeled prolog loop. */
2186 if (LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo
) < 0)
2188 struct data_reference
*dr
= LOOP_VINFO_UNALIGNED_DR (loop_vinfo
);
2189 tree vectype
= STMT_VINFO_VECTYPE (vinfo_for_stmt (DR_STMT (dr
)));
2191 niters_th
= TYPE_VECTOR_SUBPARTS (vectype
) - 1;
2194 niters_th
= LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo
);
2196 /* Niters for at least one iteration of vectorized loop. */
2197 niters_th
+= LOOP_VINFO_VECT_FACTOR (loop_vinfo
);
2198 /* One additional iteration because of peeling for gap. */
2199 if (LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo
))
2201 LOOP_VINFO_VERSIONING_THRESHOLD (loop_vinfo
) = niters_th
;
2204 gcc_assert (vectorization_factor
2205 == (unsigned)LOOP_VINFO_VECT_FACTOR (loop_vinfo
));
2207 /* Ok to vectorize! */
2211 /* Try again with SLP forced off but if we didn't do any SLP there is
2212 no point in re-trying. */
2216 /* If there are reduction chains re-trying will fail anyway. */
2217 if (! LOOP_VINFO_REDUCTION_CHAINS (loop_vinfo
).is_empty ())
2220 /* Likewise if the grouped loads or stores in the SLP cannot be handled
2221 via interleaving or lane instructions. */
2222 slp_instance instance
;
2225 FOR_EACH_VEC_ELT (LOOP_VINFO_SLP_INSTANCES (loop_vinfo
), i
, instance
)
2227 stmt_vec_info vinfo
;
2228 vinfo
= vinfo_for_stmt
2229 (SLP_TREE_SCALAR_STMTS (SLP_INSTANCE_TREE (instance
))[0]);
2230 if (! STMT_VINFO_GROUPED_ACCESS (vinfo
))
2232 vinfo
= vinfo_for_stmt (STMT_VINFO_GROUP_FIRST_ELEMENT (vinfo
));
2233 unsigned int size
= STMT_VINFO_GROUP_SIZE (vinfo
);
2234 tree vectype
= STMT_VINFO_VECTYPE (vinfo
);
2235 if (! vect_store_lanes_supported (vectype
, size
)
2236 && ! vect_grouped_store_supported (vectype
, size
))
2238 FOR_EACH_VEC_ELT (SLP_INSTANCE_LOADS (instance
), j
, node
)
2240 vinfo
= vinfo_for_stmt (SLP_TREE_SCALAR_STMTS (node
)[0]);
2241 vinfo
= vinfo_for_stmt (STMT_VINFO_GROUP_FIRST_ELEMENT (vinfo
));
2242 bool single_element_p
= !STMT_VINFO_GROUP_NEXT_ELEMENT (vinfo
);
2243 size
= STMT_VINFO_GROUP_SIZE (vinfo
);
2244 vectype
= STMT_VINFO_VECTYPE (vinfo
);
2245 if (! vect_load_lanes_supported (vectype
, size
)
2246 && ! vect_grouped_load_supported (vectype
, single_element_p
,
2252 if (dump_enabled_p ())
2253 dump_printf_loc (MSG_NOTE
, vect_location
,
2254 "re-trying with SLP disabled\n");
2256 /* Roll back state appropriately. No SLP this time. */
2258 /* Restore vectorization factor as it were without SLP. */
2259 LOOP_VINFO_VECT_FACTOR (loop_vinfo
) = saved_vectorization_factor
;
2260 /* Free the SLP instances. */
2261 FOR_EACH_VEC_ELT (LOOP_VINFO_SLP_INSTANCES (loop_vinfo
), j
, instance
)
2262 vect_free_slp_instance (instance
);
2263 LOOP_VINFO_SLP_INSTANCES (loop_vinfo
).release ();
2264 /* Reset SLP type to loop_vect on all stmts. */
2265 for (i
= 0; i
< LOOP_VINFO_LOOP (loop_vinfo
)->num_nodes
; ++i
)
2267 basic_block bb
= LOOP_VINFO_BBS (loop_vinfo
)[i
];
2268 for (gimple_stmt_iterator si
= gsi_start_phis (bb
);
2269 !gsi_end_p (si
); gsi_next (&si
))
2271 stmt_vec_info stmt_info
= vinfo_for_stmt (gsi_stmt (si
));
2272 STMT_SLP_TYPE (stmt_info
) = loop_vect
;
2274 for (gimple_stmt_iterator si
= gsi_start_bb (bb
);
2275 !gsi_end_p (si
); gsi_next (&si
))
2277 stmt_vec_info stmt_info
= vinfo_for_stmt (gsi_stmt (si
));
2278 STMT_SLP_TYPE (stmt_info
) = loop_vect
;
2279 if (STMT_VINFO_IN_PATTERN_P (stmt_info
))
2281 stmt_info
= vinfo_for_stmt (STMT_VINFO_RELATED_STMT (stmt_info
));
2282 STMT_SLP_TYPE (stmt_info
) = loop_vect
;
2283 for (gimple_stmt_iterator pi
2284 = gsi_start (STMT_VINFO_PATTERN_DEF_SEQ (stmt_info
));
2285 !gsi_end_p (pi
); gsi_next (&pi
))
2287 gimple
*pstmt
= gsi_stmt (pi
);
2288 STMT_SLP_TYPE (vinfo_for_stmt (pstmt
)) = loop_vect
;
2293 /* Free optimized alias test DDRS. */
2294 LOOP_VINFO_COMP_ALIAS_DDRS (loop_vinfo
).release ();
2295 LOOP_VINFO_CHECK_UNEQUAL_ADDRS (loop_vinfo
).release ();
2296 /* Reset target cost data. */
2297 destroy_cost_data (LOOP_VINFO_TARGET_COST_DATA (loop_vinfo
));
2298 LOOP_VINFO_TARGET_COST_DATA (loop_vinfo
)
2299 = init_cost (LOOP_VINFO_LOOP (loop_vinfo
));
2300 /* Reset assorted flags. */
2301 LOOP_VINFO_PEELING_FOR_NITER (loop_vinfo
) = false;
2302 LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo
) = false;
2303 LOOP_VINFO_COST_MODEL_THRESHOLD (loop_vinfo
) = 0;
2304 LOOP_VINFO_VERSIONING_THRESHOLD (loop_vinfo
) = 0;
2309 /* Function vect_analyze_loop.
2311 Apply a set of analyses on LOOP, and create a loop_vec_info struct
2312 for it. The different analyses will record information in the
2313 loop_vec_info struct. If ORIG_LOOP_VINFO is not NULL epilogue must
2316 vect_analyze_loop (struct loop
*loop
, loop_vec_info orig_loop_vinfo
)
2318 loop_vec_info loop_vinfo
;
2319 unsigned int vector_sizes
;
2321 /* Autodetect first vector size we try. */
2322 current_vector_size
= 0;
2323 vector_sizes
= targetm
.vectorize
.autovectorize_vector_sizes ();
2325 if (dump_enabled_p ())
2326 dump_printf_loc (MSG_NOTE
, vect_location
,
2327 "===== analyze_loop_nest =====\n");
2329 if (loop_outer (loop
)
2330 && loop_vec_info_for_loop (loop_outer (loop
))
2331 && LOOP_VINFO_VECTORIZABLE_P (loop_vec_info_for_loop (loop_outer (loop
))))
2333 if (dump_enabled_p ())
2334 dump_printf_loc (MSG_NOTE
, vect_location
,
2335 "outer-loop already vectorized.\n");
2341 /* Check the CFG characteristics of the loop (nesting, entry/exit). */
2342 loop_vinfo
= vect_analyze_loop_form (loop
);
2345 if (dump_enabled_p ())
2346 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2347 "bad loop form.\n");
2353 if (orig_loop_vinfo
)
2354 LOOP_VINFO_ORIG_LOOP_INFO (loop_vinfo
) = orig_loop_vinfo
;
2356 if (vect_analyze_loop_2 (loop_vinfo
, fatal
))
2358 LOOP_VINFO_VECTORIZABLE_P (loop_vinfo
) = 1;
2365 vector_sizes
&= ~current_vector_size
;
2367 || vector_sizes
== 0
2368 || current_vector_size
== 0)
2371 /* Try the next biggest vector size. */
2372 current_vector_size
= 1 << floor_log2 (vector_sizes
);
2373 if (dump_enabled_p ())
2374 dump_printf_loc (MSG_NOTE
, vect_location
,
2375 "***** Re-trying analysis with "
2376 "vector size %d\n", current_vector_size
);
2381 /* Function reduction_fn_for_scalar_code
2384 CODE - tree_code of a reduction operations.
2387 REDUC_FN - the corresponding internal function to be used to reduce the
2388 vector of partial results into a single scalar result, or IFN_LAST
2389 if the operation is a supported reduction operation, but does not have
2390 such an internal function.
2392 Return FALSE if CODE currently cannot be vectorized as reduction. */
2395 reduction_fn_for_scalar_code (enum tree_code code
, internal_fn
*reduc_fn
)
2400 *reduc_fn
= IFN_REDUC_MAX
;
2404 *reduc_fn
= IFN_REDUC_MIN
;
2408 *reduc_fn
= IFN_REDUC_PLUS
;
2416 *reduc_fn
= IFN_LAST
;
2425 /* Error reporting helper for vect_is_simple_reduction below. GIMPLE statement
2426 STMT is printed with a message MSG. */
2429 report_vect_op (dump_flags_t msg_type
, gimple
*stmt
, const char *msg
)
2431 dump_printf_loc (msg_type
, vect_location
, "%s", msg
);
2432 dump_gimple_stmt (msg_type
, TDF_SLIM
, stmt
, 0);
2436 /* Detect SLP reduction of the form:
2446 PHI is the reduction phi node (#a1 = phi <a5, a0> above)
2447 FIRST_STMT is the first reduction stmt in the chain
2448 (a2 = operation (a1)).
2450 Return TRUE if a reduction chain was detected. */
2453 vect_is_slp_reduction (loop_vec_info loop_info
, gimple
*phi
,
2456 struct loop
*loop
= (gimple_bb (phi
))->loop_father
;
2457 struct loop
*vect_loop
= LOOP_VINFO_LOOP (loop_info
);
2458 enum tree_code code
;
2459 gimple
*current_stmt
= NULL
, *loop_use_stmt
= NULL
, *first
, *next_stmt
;
2460 stmt_vec_info use_stmt_info
, current_stmt_info
;
2462 imm_use_iterator imm_iter
;
2463 use_operand_p use_p
;
2464 int nloop_uses
, size
= 0, n_out_of_loop_uses
;
2467 if (loop
!= vect_loop
)
2470 lhs
= PHI_RESULT (phi
);
2471 code
= gimple_assign_rhs_code (first_stmt
);
2475 n_out_of_loop_uses
= 0;
2476 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, lhs
)
2478 gimple
*use_stmt
= USE_STMT (use_p
);
2479 if (is_gimple_debug (use_stmt
))
2482 /* Check if we got back to the reduction phi. */
2483 if (use_stmt
== phi
)
2485 loop_use_stmt
= use_stmt
;
2490 if (flow_bb_inside_loop_p (loop
, gimple_bb (use_stmt
)))
2492 loop_use_stmt
= use_stmt
;
2496 n_out_of_loop_uses
++;
2498 /* There are can be either a single use in the loop or two uses in
2500 if (nloop_uses
> 1 || (n_out_of_loop_uses
&& nloop_uses
))
2507 /* We reached a statement with no loop uses. */
2508 if (nloop_uses
== 0)
2511 /* This is a loop exit phi, and we haven't reached the reduction phi. */
2512 if (gimple_code (loop_use_stmt
) == GIMPLE_PHI
)
2515 if (!is_gimple_assign (loop_use_stmt
)
2516 || code
!= gimple_assign_rhs_code (loop_use_stmt
)
2517 || !flow_bb_inside_loop_p (loop
, gimple_bb (loop_use_stmt
)))
2520 /* Insert USE_STMT into reduction chain. */
2521 use_stmt_info
= vinfo_for_stmt (loop_use_stmt
);
2524 current_stmt_info
= vinfo_for_stmt (current_stmt
);
2525 GROUP_NEXT_ELEMENT (current_stmt_info
) = loop_use_stmt
;
2526 GROUP_FIRST_ELEMENT (use_stmt_info
)
2527 = GROUP_FIRST_ELEMENT (current_stmt_info
);
2530 GROUP_FIRST_ELEMENT (use_stmt_info
) = loop_use_stmt
;
2532 lhs
= gimple_assign_lhs (loop_use_stmt
);
2533 current_stmt
= loop_use_stmt
;
2537 if (!found
|| loop_use_stmt
!= phi
|| size
< 2)
2540 /* Swap the operands, if needed, to make the reduction operand be the second
2542 lhs
= PHI_RESULT (phi
);
2543 next_stmt
= GROUP_FIRST_ELEMENT (vinfo_for_stmt (current_stmt
));
2546 if (gimple_assign_rhs2 (next_stmt
) == lhs
)
2548 tree op
= gimple_assign_rhs1 (next_stmt
);
2549 gimple
*def_stmt
= NULL
;
2551 if (TREE_CODE (op
) == SSA_NAME
)
2552 def_stmt
= SSA_NAME_DEF_STMT (op
);
2554 /* Check that the other def is either defined in the loop
2555 ("vect_internal_def"), or it's an induction (defined by a
2556 loop-header phi-node). */
2558 && gimple_bb (def_stmt
)
2559 && flow_bb_inside_loop_p (loop
, gimple_bb (def_stmt
))
2560 && (is_gimple_assign (def_stmt
)
2561 || is_gimple_call (def_stmt
)
2562 || STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def_stmt
))
2563 == vect_induction_def
2564 || (gimple_code (def_stmt
) == GIMPLE_PHI
2565 && STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def_stmt
))
2566 == vect_internal_def
2567 && !is_loop_header_bb_p (gimple_bb (def_stmt
)))))
2569 lhs
= gimple_assign_lhs (next_stmt
);
2570 next_stmt
= GROUP_NEXT_ELEMENT (vinfo_for_stmt (next_stmt
));
2578 tree op
= gimple_assign_rhs2 (next_stmt
);
2579 gimple
*def_stmt
= NULL
;
2581 if (TREE_CODE (op
) == SSA_NAME
)
2582 def_stmt
= SSA_NAME_DEF_STMT (op
);
2584 /* Check that the other def is either defined in the loop
2585 ("vect_internal_def"), or it's an induction (defined by a
2586 loop-header phi-node). */
2588 && gimple_bb (def_stmt
)
2589 && flow_bb_inside_loop_p (loop
, gimple_bb (def_stmt
))
2590 && (is_gimple_assign (def_stmt
)
2591 || is_gimple_call (def_stmt
)
2592 || STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def_stmt
))
2593 == vect_induction_def
2594 || (gimple_code (def_stmt
) == GIMPLE_PHI
2595 && STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def_stmt
))
2596 == vect_internal_def
2597 && !is_loop_header_bb_p (gimple_bb (def_stmt
)))))
2599 if (dump_enabled_p ())
2601 dump_printf_loc (MSG_NOTE
, vect_location
, "swapping oprnds: ");
2602 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
, next_stmt
, 0);
2605 swap_ssa_operands (next_stmt
,
2606 gimple_assign_rhs1_ptr (next_stmt
),
2607 gimple_assign_rhs2_ptr (next_stmt
));
2608 update_stmt (next_stmt
);
2610 if (CONSTANT_CLASS_P (gimple_assign_rhs1 (next_stmt
)))
2611 LOOP_VINFO_OPERANDS_SWAPPED (loop_info
) = true;
2617 lhs
= gimple_assign_lhs (next_stmt
);
2618 next_stmt
= GROUP_NEXT_ELEMENT (vinfo_for_stmt (next_stmt
));
2621 /* Save the chain for further analysis in SLP detection. */
2622 first
= GROUP_FIRST_ELEMENT (vinfo_for_stmt (current_stmt
));
2623 LOOP_VINFO_REDUCTION_CHAINS (loop_info
).safe_push (first
);
2624 GROUP_SIZE (vinfo_for_stmt (first
)) = size
;
2630 /* Return true if the reduction PHI in LOOP with latch arg LOOP_ARG and
2631 reduction operation CODE has a handled computation expression. */
2634 check_reduction_path (location_t loc
, loop_p loop
, gphi
*phi
, tree loop_arg
,
2635 enum tree_code code
)
2637 auto_vec
<std::pair
<ssa_op_iter
, use_operand_p
> > path
;
2638 auto_bitmap visited
;
2639 tree lookfor
= PHI_RESULT (phi
);
2641 use_operand_p curr
= op_iter_init_phiuse (&curri
, phi
, SSA_OP_USE
);
2642 while (USE_FROM_PTR (curr
) != loop_arg
)
2643 curr
= op_iter_next_use (&curri
);
2644 curri
.i
= curri
.numops
;
2647 path
.safe_push (std::make_pair (curri
, curr
));
2648 tree use
= USE_FROM_PTR (curr
);
2651 gimple
*def
= SSA_NAME_DEF_STMT (use
);
2652 if (gimple_nop_p (def
)
2653 || ! flow_bb_inside_loop_p (loop
, gimple_bb (def
)))
2658 std::pair
<ssa_op_iter
, use_operand_p
> x
= path
.pop ();
2662 curr
= op_iter_next_use (&curri
);
2663 /* Skip already visited or non-SSA operands (from iterating
2665 while (curr
!= NULL_USE_OPERAND_P
2666 && (TREE_CODE (USE_FROM_PTR (curr
)) != SSA_NAME
2667 || ! bitmap_set_bit (visited
,
2669 (USE_FROM_PTR (curr
)))));
2671 while (curr
== NULL_USE_OPERAND_P
&& ! path
.is_empty ());
2672 if (curr
== NULL_USE_OPERAND_P
)
2677 if (gimple_code (def
) == GIMPLE_PHI
)
2678 curr
= op_iter_init_phiuse (&curri
, as_a
<gphi
*>(def
), SSA_OP_USE
);
2680 curr
= op_iter_init_use (&curri
, def
, SSA_OP_USE
);
2681 while (curr
!= NULL_USE_OPERAND_P
2682 && (TREE_CODE (USE_FROM_PTR (curr
)) != SSA_NAME
2683 || ! bitmap_set_bit (visited
,
2685 (USE_FROM_PTR (curr
)))))
2686 curr
= op_iter_next_use (&curri
);
2687 if (curr
== NULL_USE_OPERAND_P
)
2692 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2694 dump_printf_loc (MSG_NOTE
, loc
, "reduction path: ");
2696 std::pair
<ssa_op_iter
, use_operand_p
> *x
;
2697 FOR_EACH_VEC_ELT (path
, i
, x
)
2699 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, USE_FROM_PTR (x
->second
));
2700 dump_printf (MSG_NOTE
, " ");
2702 dump_printf (MSG_NOTE
, "\n");
2705 /* Check whether the reduction path detected is valid. */
2706 bool fail
= path
.length () == 0;
2708 for (unsigned i
= 1; i
< path
.length (); ++i
)
2710 gimple
*use_stmt
= USE_STMT (path
[i
].second
);
2711 tree op
= USE_FROM_PTR (path
[i
].second
);
2712 if (! has_single_use (op
)
2713 || ! is_gimple_assign (use_stmt
))
2718 if (gimple_assign_rhs_code (use_stmt
) != code
)
2720 if (code
== PLUS_EXPR
2721 && gimple_assign_rhs_code (use_stmt
) == MINUS_EXPR
)
2723 /* Track whether we negate the reduction value each iteration. */
2724 if (gimple_assign_rhs2 (use_stmt
) == op
)
2734 return ! fail
&& ! neg
;
2738 /* Function vect_is_simple_reduction
2740 (1) Detect a cross-iteration def-use cycle that represents a simple
2741 reduction computation. We look for the following pattern:
2746 a2 = operation (a3, a1)
2753 a2 = operation (a3, a1)
2756 1. operation is commutative and associative and it is safe to
2757 change the order of the computation
2758 2. no uses for a2 in the loop (a2 is used out of the loop)
2759 3. no uses of a1 in the loop besides the reduction operation
2760 4. no uses of a1 outside the loop.
2762 Conditions 1,4 are tested here.
2763 Conditions 2,3 are tested in vect_mark_stmts_to_be_vectorized.
2765 (2) Detect a cross-iteration def-use cycle in nested loops, i.e.,
2768 (3) Detect cycles of phi nodes in outer-loop vectorization, i.e., double
2772 inner loop (def of a3)
2775 (4) Detect condition expressions, ie:
2776 for (int i = 0; i < N; i++)
2783 vect_is_simple_reduction (loop_vec_info loop_info
, gimple
*phi
,
2785 bool need_wrapping_integral_overflow
,
2786 enum vect_reduction_type
*v_reduc_type
)
2788 struct loop
*loop
= (gimple_bb (phi
))->loop_father
;
2789 struct loop
*vect_loop
= LOOP_VINFO_LOOP (loop_info
);
2790 gimple
*def_stmt
, *def1
= NULL
, *def2
= NULL
, *phi_use_stmt
= NULL
;
2791 enum tree_code orig_code
, code
;
2792 tree op1
, op2
, op3
= NULL_TREE
, op4
= NULL_TREE
;
2796 imm_use_iterator imm_iter
;
2797 use_operand_p use_p
;
2800 *double_reduc
= false;
2801 *v_reduc_type
= TREE_CODE_REDUCTION
;
2803 tree phi_name
= PHI_RESULT (phi
);
2804 /* ??? If there are no uses of the PHI result the inner loop reduction
2805 won't be detected as possibly double-reduction by vectorizable_reduction
2806 because that tries to walk the PHI arg from the preheader edge which
2807 can be constant. See PR60382. */
2808 if (has_zero_uses (phi_name
))
2811 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, phi_name
)
2813 gimple
*use_stmt
= USE_STMT (use_p
);
2814 if (is_gimple_debug (use_stmt
))
2817 if (!flow_bb_inside_loop_p (loop
, gimple_bb (use_stmt
)))
2819 if (dump_enabled_p ())
2820 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2821 "intermediate value used outside loop.\n");
2829 if (dump_enabled_p ())
2830 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2831 "reduction value used in loop.\n");
2835 phi_use_stmt
= use_stmt
;
2838 edge latch_e
= loop_latch_edge (loop
);
2839 tree loop_arg
= PHI_ARG_DEF_FROM_EDGE (phi
, latch_e
);
2840 if (TREE_CODE (loop_arg
) != SSA_NAME
)
2842 if (dump_enabled_p ())
2844 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2845 "reduction: not ssa_name: ");
2846 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
, loop_arg
);
2847 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
2852 def_stmt
= SSA_NAME_DEF_STMT (loop_arg
);
2853 if (is_gimple_assign (def_stmt
))
2855 name
= gimple_assign_lhs (def_stmt
);
2858 else if (gimple_code (def_stmt
) == GIMPLE_PHI
)
2860 name
= PHI_RESULT (def_stmt
);
2865 if (dump_enabled_p ())
2867 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2868 "reduction: unhandled reduction operation: ");
2869 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
, def_stmt
, 0);
2874 if (! flow_bb_inside_loop_p (loop
, gimple_bb (def_stmt
)))
2878 auto_vec
<gphi
*, 3> lcphis
;
2879 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, name
)
2881 gimple
*use_stmt
= USE_STMT (use_p
);
2882 if (is_gimple_debug (use_stmt
))
2884 if (flow_bb_inside_loop_p (loop
, gimple_bb (use_stmt
)))
2887 /* We can have more than one loop-closed PHI. */
2888 lcphis
.safe_push (as_a
<gphi
*> (use_stmt
));
2891 if (dump_enabled_p ())
2892 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2893 "reduction used in loop.\n");
2898 /* If DEF_STMT is a phi node itself, we expect it to have a single argument
2899 defined in the inner loop. */
2902 op1
= PHI_ARG_DEF (def_stmt
, 0);
2904 if (gimple_phi_num_args (def_stmt
) != 1
2905 || TREE_CODE (op1
) != SSA_NAME
)
2907 if (dump_enabled_p ())
2908 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2909 "unsupported phi node definition.\n");
2914 def1
= SSA_NAME_DEF_STMT (op1
);
2915 if (gimple_bb (def1
)
2916 && flow_bb_inside_loop_p (loop
, gimple_bb (def_stmt
))
2918 && flow_bb_inside_loop_p (loop
->inner
, gimple_bb (def1
))
2919 && is_gimple_assign (def1
)
2920 && flow_bb_inside_loop_p (loop
->inner
, gimple_bb (phi_use_stmt
)))
2922 if (dump_enabled_p ())
2923 report_vect_op (MSG_NOTE
, def_stmt
,
2924 "detected double reduction: ");
2926 *double_reduc
= true;
2933 /* If we are vectorizing an inner reduction we are executing that
2934 in the original order only in case we are not dealing with a
2935 double reduction. */
2936 bool check_reduction
= true;
2937 if (flow_loop_nested_p (vect_loop
, loop
))
2941 check_reduction
= false;
2942 FOR_EACH_VEC_ELT (lcphis
, i
, lcphi
)
2943 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, gimple_phi_result (lcphi
))
2945 gimple
*use_stmt
= USE_STMT (use_p
);
2946 if (is_gimple_debug (use_stmt
))
2948 if (! flow_bb_inside_loop_p (vect_loop
, gimple_bb (use_stmt
)))
2949 check_reduction
= true;
2953 bool nested_in_vect_loop
= flow_loop_nested_p (vect_loop
, loop
);
2954 code
= orig_code
= gimple_assign_rhs_code (def_stmt
);
2956 /* We can handle "res -= x[i]", which is non-associative by
2957 simply rewriting this into "res += -x[i]". Avoid changing
2958 gimple instruction for the first simple tests and only do this
2959 if we're allowed to change code at all. */
2960 if (code
== MINUS_EXPR
&& gimple_assign_rhs2 (def_stmt
) != phi_name
)
2963 if (code
== COND_EXPR
)
2965 if (! nested_in_vect_loop
)
2966 *v_reduc_type
= COND_REDUCTION
;
2968 op3
= gimple_assign_rhs1 (def_stmt
);
2969 if (COMPARISON_CLASS_P (op3
))
2971 op4
= TREE_OPERAND (op3
, 1);
2972 op3
= TREE_OPERAND (op3
, 0);
2974 if (op3
== phi_name
|| op4
== phi_name
)
2976 if (dump_enabled_p ())
2977 report_vect_op (MSG_MISSED_OPTIMIZATION
, def_stmt
,
2978 "reduction: condition depends on previous"
2983 op1
= gimple_assign_rhs2 (def_stmt
);
2984 op2
= gimple_assign_rhs3 (def_stmt
);
2986 else if (!commutative_tree_code (code
) || !associative_tree_code (code
))
2988 if (dump_enabled_p ())
2989 report_vect_op (MSG_MISSED_OPTIMIZATION
, def_stmt
,
2990 "reduction: not commutative/associative: ");
2993 else if (get_gimple_rhs_class (code
) == GIMPLE_BINARY_RHS
)
2995 op1
= gimple_assign_rhs1 (def_stmt
);
2996 op2
= gimple_assign_rhs2 (def_stmt
);
3000 if (dump_enabled_p ())
3001 report_vect_op (MSG_MISSED_OPTIMIZATION
, def_stmt
,
3002 "reduction: not handled operation: ");
3006 if (TREE_CODE (op1
) != SSA_NAME
&& TREE_CODE (op2
) != SSA_NAME
)
3008 if (dump_enabled_p ())
3009 report_vect_op (MSG_MISSED_OPTIMIZATION
, def_stmt
,
3010 "reduction: both uses not ssa_names: ");
3015 type
= TREE_TYPE (gimple_assign_lhs (def_stmt
));
3016 if ((TREE_CODE (op1
) == SSA_NAME
3017 && !types_compatible_p (type
,TREE_TYPE (op1
)))
3018 || (TREE_CODE (op2
) == SSA_NAME
3019 && !types_compatible_p (type
, TREE_TYPE (op2
)))
3020 || (op3
&& TREE_CODE (op3
) == SSA_NAME
3021 && !types_compatible_p (type
, TREE_TYPE (op3
)))
3022 || (op4
&& TREE_CODE (op4
) == SSA_NAME
3023 && !types_compatible_p (type
, TREE_TYPE (op4
))))
3025 if (dump_enabled_p ())
3027 dump_printf_loc (MSG_NOTE
, vect_location
,
3028 "reduction: multiple types: operation type: ");
3029 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, type
);
3030 dump_printf (MSG_NOTE
, ", operands types: ");
3031 dump_generic_expr (MSG_NOTE
, TDF_SLIM
,
3033 dump_printf (MSG_NOTE
, ",");
3034 dump_generic_expr (MSG_NOTE
, TDF_SLIM
,
3038 dump_printf (MSG_NOTE
, ",");
3039 dump_generic_expr (MSG_NOTE
, TDF_SLIM
,
3045 dump_printf (MSG_NOTE
, ",");
3046 dump_generic_expr (MSG_NOTE
, TDF_SLIM
,
3049 dump_printf (MSG_NOTE
, "\n");
3055 /* Check that it's ok to change the order of the computation.
3056 Generally, when vectorizing a reduction we change the order of the
3057 computation. This may change the behavior of the program in some
3058 cases, so we need to check that this is ok. One exception is when
3059 vectorizing an outer-loop: the inner-loop is executed sequentially,
3060 and therefore vectorizing reductions in the inner-loop during
3061 outer-loop vectorization is safe. */
3063 if (*v_reduc_type
!= COND_REDUCTION
3066 /* CHECKME: check for !flag_finite_math_only too? */
3067 if (SCALAR_FLOAT_TYPE_P (type
) && !flag_associative_math
)
3069 /* Changing the order of operations changes the semantics. */
3070 if (dump_enabled_p ())
3071 report_vect_op (MSG_MISSED_OPTIMIZATION
, def_stmt
,
3072 "reduction: unsafe fp math optimization: ");
3075 else if (INTEGRAL_TYPE_P (type
))
3077 if (!operation_no_trapping_overflow (type
, code
))
3079 /* Changing the order of operations changes the semantics. */
3080 if (dump_enabled_p ())
3081 report_vect_op (MSG_MISSED_OPTIMIZATION
, def_stmt
,
3082 "reduction: unsafe int math optimization"
3083 " (overflow traps): ");
3086 if (need_wrapping_integral_overflow
3087 && !TYPE_OVERFLOW_WRAPS (type
)
3088 && operation_can_overflow (code
))
3090 /* Changing the order of operations changes the semantics. */
3091 if (dump_enabled_p ())
3092 report_vect_op (MSG_MISSED_OPTIMIZATION
, def_stmt
,
3093 "reduction: unsafe int math optimization"
3094 " (overflow doesn't wrap): ");
3098 else if (SAT_FIXED_POINT_TYPE_P (type
))
3100 /* Changing the order of operations changes the semantics. */
3101 if (dump_enabled_p ())
3102 report_vect_op (MSG_MISSED_OPTIMIZATION
, def_stmt
,
3103 "reduction: unsafe fixed-point math optimization: ");
3108 /* Reduction is safe. We're dealing with one of the following:
3109 1) integer arithmetic and no trapv
3110 2) floating point arithmetic, and special flags permit this optimization
3111 3) nested cycle (i.e., outer loop vectorization). */
3112 if (TREE_CODE (op1
) == SSA_NAME
)
3113 def1
= SSA_NAME_DEF_STMT (op1
);
3115 if (TREE_CODE (op2
) == SSA_NAME
)
3116 def2
= SSA_NAME_DEF_STMT (op2
);
3118 if (code
!= COND_EXPR
3119 && ((!def1
|| gimple_nop_p (def1
)) && (!def2
|| gimple_nop_p (def2
))))
3121 if (dump_enabled_p ())
3122 report_vect_op (MSG_NOTE
, def_stmt
, "reduction: no defs for operands: ");
3126 /* Check that one def is the reduction def, defined by PHI,
3127 the other def is either defined in the loop ("vect_internal_def"),
3128 or it's an induction (defined by a loop-header phi-node). */
3130 if (def2
&& def2
== phi
3131 && (code
== COND_EXPR
3132 || !def1
|| gimple_nop_p (def1
)
3133 || !flow_bb_inside_loop_p (loop
, gimple_bb (def1
))
3134 || (def1
&& flow_bb_inside_loop_p (loop
, gimple_bb (def1
))
3135 && (is_gimple_assign (def1
)
3136 || is_gimple_call (def1
)
3137 || STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def1
))
3138 == vect_induction_def
3139 || (gimple_code (def1
) == GIMPLE_PHI
3140 && STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def1
))
3141 == vect_internal_def
3142 && !is_loop_header_bb_p (gimple_bb (def1
)))))))
3144 if (dump_enabled_p ())
3145 report_vect_op (MSG_NOTE
, def_stmt
, "detected reduction: ");
3149 if (def1
&& def1
== phi
3150 && (code
== COND_EXPR
3151 || !def2
|| gimple_nop_p (def2
)
3152 || !flow_bb_inside_loop_p (loop
, gimple_bb (def2
))
3153 || (def2
&& flow_bb_inside_loop_p (loop
, gimple_bb (def2
))
3154 && (is_gimple_assign (def2
)
3155 || is_gimple_call (def2
)
3156 || STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def2
))
3157 == vect_induction_def
3158 || (gimple_code (def2
) == GIMPLE_PHI
3159 && STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def2
))
3160 == vect_internal_def
3161 && !is_loop_header_bb_p (gimple_bb (def2
)))))))
3163 if (! nested_in_vect_loop
&& orig_code
!= MINUS_EXPR
)
3165 /* Check if we can swap operands (just for simplicity - so that
3166 the rest of the code can assume that the reduction variable
3167 is always the last (second) argument). */
3168 if (code
== COND_EXPR
)
3170 /* Swap cond_expr by inverting the condition. */
3171 tree cond_expr
= gimple_assign_rhs1 (def_stmt
);
3172 enum tree_code invert_code
= ERROR_MARK
;
3173 enum tree_code cond_code
= TREE_CODE (cond_expr
);
3175 if (TREE_CODE_CLASS (cond_code
) == tcc_comparison
)
3177 bool honor_nans
= HONOR_NANS (TREE_OPERAND (cond_expr
, 0));
3178 invert_code
= invert_tree_comparison (cond_code
, honor_nans
);
3180 if (invert_code
!= ERROR_MARK
)
3182 TREE_SET_CODE (cond_expr
, invert_code
);
3183 swap_ssa_operands (def_stmt
,
3184 gimple_assign_rhs2_ptr (def_stmt
),
3185 gimple_assign_rhs3_ptr (def_stmt
));
3189 if (dump_enabled_p ())
3190 report_vect_op (MSG_NOTE
, def_stmt
,
3191 "detected reduction: cannot swap operands "
3197 swap_ssa_operands (def_stmt
, gimple_assign_rhs1_ptr (def_stmt
),
3198 gimple_assign_rhs2_ptr (def_stmt
));
3200 if (dump_enabled_p ())
3201 report_vect_op (MSG_NOTE
, def_stmt
,
3202 "detected reduction: need to swap operands: ");
3204 if (CONSTANT_CLASS_P (gimple_assign_rhs1 (def_stmt
)))
3205 LOOP_VINFO_OPERANDS_SWAPPED (loop_info
) = true;
3209 if (dump_enabled_p ())
3210 report_vect_op (MSG_NOTE
, def_stmt
, "detected reduction: ");
3216 /* Try to find SLP reduction chain. */
3217 if (! nested_in_vect_loop
3218 && code
!= COND_EXPR
3219 && orig_code
!= MINUS_EXPR
3220 && vect_is_slp_reduction (loop_info
, phi
, def_stmt
))
3222 if (dump_enabled_p ())
3223 report_vect_op (MSG_NOTE
, def_stmt
,
3224 "reduction: detected reduction chain: ");
3229 /* Dissolve group eventually half-built by vect_is_slp_reduction. */
3230 gimple
*first
= GROUP_FIRST_ELEMENT (vinfo_for_stmt (def_stmt
));
3233 gimple
*next
= GROUP_NEXT_ELEMENT (vinfo_for_stmt (first
));
3234 GROUP_FIRST_ELEMENT (vinfo_for_stmt (first
)) = NULL
;
3235 GROUP_NEXT_ELEMENT (vinfo_for_stmt (first
)) = NULL
;
3239 /* Look for the expression computing loop_arg from loop PHI result. */
3240 if (check_reduction_path (vect_location
, loop
, as_a
<gphi
*> (phi
), loop_arg
,
3244 if (dump_enabled_p ())
3246 report_vect_op (MSG_MISSED_OPTIMIZATION
, def_stmt
,
3247 "reduction: unknown pattern: ");
3253 /* Wrapper around vect_is_simple_reduction, which will modify code
3254 in-place if it enables detection of more reductions. Arguments
3258 vect_force_simple_reduction (loop_vec_info loop_info
, gimple
*phi
,
3260 bool need_wrapping_integral_overflow
)
3262 enum vect_reduction_type v_reduc_type
;
3263 gimple
*def
= vect_is_simple_reduction (loop_info
, phi
, double_reduc
,
3264 need_wrapping_integral_overflow
,
3268 stmt_vec_info reduc_def_info
= vinfo_for_stmt (phi
);
3269 STMT_VINFO_REDUC_TYPE (reduc_def_info
) = v_reduc_type
;
3270 STMT_VINFO_REDUC_DEF (reduc_def_info
) = def
;
3271 reduc_def_info
= vinfo_for_stmt (def
);
3272 STMT_VINFO_REDUC_DEF (reduc_def_info
) = phi
;
3277 /* Calculate cost of peeling the loop PEEL_ITERS_PROLOGUE times. */
3279 vect_get_known_peeling_cost (loop_vec_info loop_vinfo
, int peel_iters_prologue
,
3280 int *peel_iters_epilogue
,
3281 stmt_vector_for_cost
*scalar_cost_vec
,
3282 stmt_vector_for_cost
*prologue_cost_vec
,
3283 stmt_vector_for_cost
*epilogue_cost_vec
)
3286 int vf
= LOOP_VINFO_VECT_FACTOR (loop_vinfo
);
3288 if (!LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo
))
3290 *peel_iters_epilogue
= vf
/2;
3291 if (dump_enabled_p ())
3292 dump_printf_loc (MSG_NOTE
, vect_location
,
3293 "cost model: epilogue peel iters set to vf/2 "
3294 "because loop iterations are unknown .\n");
3296 /* If peeled iterations are known but number of scalar loop
3297 iterations are unknown, count a taken branch per peeled loop. */
3298 retval
= record_stmt_cost (prologue_cost_vec
, 1, cond_branch_taken
,
3299 NULL
, 0, vect_prologue
);
3300 retval
= record_stmt_cost (prologue_cost_vec
, 1, cond_branch_taken
,
3301 NULL
, 0, vect_epilogue
);
3305 int niters
= LOOP_VINFO_INT_NITERS (loop_vinfo
);
3306 peel_iters_prologue
= niters
< peel_iters_prologue
?
3307 niters
: peel_iters_prologue
;
3308 *peel_iters_epilogue
= (niters
- peel_iters_prologue
) % vf
;
3309 /* If we need to peel for gaps, but no peeling is required, we have to
3310 peel VF iterations. */
3311 if (LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo
) && !*peel_iters_epilogue
)
3312 *peel_iters_epilogue
= vf
;
3315 stmt_info_for_cost
*si
;
3317 if (peel_iters_prologue
)
3318 FOR_EACH_VEC_ELT (*scalar_cost_vec
, j
, si
)
3320 stmt_vec_info stmt_info
3321 = si
->stmt
? vinfo_for_stmt (si
->stmt
) : NULL
;
3322 retval
+= record_stmt_cost (prologue_cost_vec
,
3323 si
->count
* peel_iters_prologue
,
3324 si
->kind
, stmt_info
, si
->misalign
,
3327 if (*peel_iters_epilogue
)
3328 FOR_EACH_VEC_ELT (*scalar_cost_vec
, j
, si
)
3330 stmt_vec_info stmt_info
3331 = si
->stmt
? vinfo_for_stmt (si
->stmt
) : NULL
;
3332 retval
+= record_stmt_cost (epilogue_cost_vec
,
3333 si
->count
* *peel_iters_epilogue
,
3334 si
->kind
, stmt_info
, si
->misalign
,
3341 /* Function vect_estimate_min_profitable_iters
3343 Return the number of iterations required for the vector version of the
3344 loop to be profitable relative to the cost of the scalar version of the
3347 *RET_MIN_PROFITABLE_NITERS is a cost model profitability threshold
3348 of iterations for vectorization. -1 value means loop vectorization
3349 is not profitable. This returned value may be used for dynamic
3350 profitability check.
3352 *RET_MIN_PROFITABLE_ESTIMATE is a profitability threshold to be used
3353 for static check against estimated number of iterations. */
3356 vect_estimate_min_profitable_iters (loop_vec_info loop_vinfo
,
3357 int *ret_min_profitable_niters
,
3358 int *ret_min_profitable_estimate
)
3360 int min_profitable_iters
;
3361 int min_profitable_estimate
;
3362 int peel_iters_prologue
;
3363 int peel_iters_epilogue
;
3364 unsigned vec_inside_cost
= 0;
3365 int vec_outside_cost
= 0;
3366 unsigned vec_prologue_cost
= 0;
3367 unsigned vec_epilogue_cost
= 0;
3368 int scalar_single_iter_cost
= 0;
3369 int scalar_outside_cost
= 0;
3370 int vf
= LOOP_VINFO_VECT_FACTOR (loop_vinfo
);
3371 int npeel
= LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo
);
3372 void *target_cost_data
= LOOP_VINFO_TARGET_COST_DATA (loop_vinfo
);
3374 /* Cost model disabled. */
3375 if (unlimited_cost_model (LOOP_VINFO_LOOP (loop_vinfo
)))
3377 dump_printf_loc (MSG_NOTE
, vect_location
, "cost model disabled.\n");
3378 *ret_min_profitable_niters
= 0;
3379 *ret_min_profitable_estimate
= 0;
3383 /* Requires loop versioning tests to handle misalignment. */
3384 if (LOOP_REQUIRES_VERSIONING_FOR_ALIGNMENT (loop_vinfo
))
3386 /* FIXME: Make cost depend on complexity of individual check. */
3387 unsigned len
= LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo
).length ();
3388 (void) add_stmt_cost (target_cost_data
, len
, vector_stmt
, NULL
, 0,
3390 dump_printf (MSG_NOTE
,
3391 "cost model: Adding cost of checks for loop "
3392 "versioning to treat misalignment.\n");
3395 /* Requires loop versioning with alias checks. */
3396 if (LOOP_REQUIRES_VERSIONING_FOR_ALIAS (loop_vinfo
))
3398 /* FIXME: Make cost depend on complexity of individual check. */
3399 unsigned len
= LOOP_VINFO_COMP_ALIAS_DDRS (loop_vinfo
).length ();
3400 (void) add_stmt_cost (target_cost_data
, len
, vector_stmt
, NULL
, 0,
3402 len
= LOOP_VINFO_CHECK_UNEQUAL_ADDRS (loop_vinfo
).length ();
3404 /* Count LEN - 1 ANDs and LEN comparisons. */
3405 (void) add_stmt_cost (target_cost_data
, len
* 2 - 1, scalar_stmt
,
3406 NULL
, 0, vect_prologue
);
3407 dump_printf (MSG_NOTE
,
3408 "cost model: Adding cost of checks for loop "
3409 "versioning aliasing.\n");
3412 /* Requires loop versioning with niter checks. */
3413 if (LOOP_REQUIRES_VERSIONING_FOR_NITERS (loop_vinfo
))
3415 /* FIXME: Make cost depend on complexity of individual check. */
3416 (void) add_stmt_cost (target_cost_data
, 1, vector_stmt
, NULL
, 0,
3418 dump_printf (MSG_NOTE
,
3419 "cost model: Adding cost of checks for loop "
3420 "versioning niters.\n");
3423 if (LOOP_REQUIRES_VERSIONING (loop_vinfo
))
3424 (void) add_stmt_cost (target_cost_data
, 1, cond_branch_taken
, NULL
, 0,
3427 /* Count statements in scalar loop. Using this as scalar cost for a single
3430 TODO: Add outer loop support.
3432 TODO: Consider assigning different costs to different scalar
3435 scalar_single_iter_cost
3436 = LOOP_VINFO_SINGLE_SCALAR_ITERATION_COST (loop_vinfo
);
3438 /* Add additional cost for the peeled instructions in prologue and epilogue
3441 FORNOW: If we don't know the value of peel_iters for prologue or epilogue
3442 at compile-time - we assume it's vf/2 (the worst would be vf-1).
3444 TODO: Build an expression that represents peel_iters for prologue and
3445 epilogue to be used in a run-time test. */
3449 peel_iters_prologue
= vf
/2;
3450 dump_printf (MSG_NOTE
, "cost model: "
3451 "prologue peel iters set to vf/2.\n");
3453 /* If peeling for alignment is unknown, loop bound of main loop becomes
3455 peel_iters_epilogue
= vf
/2;
3456 dump_printf (MSG_NOTE
, "cost model: "
3457 "epilogue peel iters set to vf/2 because "
3458 "peeling for alignment is unknown.\n");
3460 /* If peeled iterations are unknown, count a taken branch and a not taken
3461 branch per peeled loop. Even if scalar loop iterations are known,
3462 vector iterations are not known since peeled prologue iterations are
3463 not known. Hence guards remain the same. */
3464 (void) add_stmt_cost (target_cost_data
, 1, cond_branch_taken
,
3465 NULL
, 0, vect_prologue
);
3466 (void) add_stmt_cost (target_cost_data
, 1, cond_branch_not_taken
,
3467 NULL
, 0, vect_prologue
);
3468 (void) add_stmt_cost (target_cost_data
, 1, cond_branch_taken
,
3469 NULL
, 0, vect_epilogue
);
3470 (void) add_stmt_cost (target_cost_data
, 1, cond_branch_not_taken
,
3471 NULL
, 0, vect_epilogue
);
3472 stmt_info_for_cost
*si
;
3474 FOR_EACH_VEC_ELT (LOOP_VINFO_SCALAR_ITERATION_COST (loop_vinfo
), j
, si
)
3476 struct _stmt_vec_info
*stmt_info
3477 = si
->stmt
? vinfo_for_stmt (si
->stmt
) : NULL
;
3478 (void) add_stmt_cost (target_cost_data
,
3479 si
->count
* peel_iters_prologue
,
3480 si
->kind
, stmt_info
, si
->misalign
,
3482 (void) add_stmt_cost (target_cost_data
,
3483 si
->count
* peel_iters_epilogue
,
3484 si
->kind
, stmt_info
, si
->misalign
,
3490 stmt_vector_for_cost prologue_cost_vec
, epilogue_cost_vec
;
3491 stmt_info_for_cost
*si
;
3493 void *data
= LOOP_VINFO_TARGET_COST_DATA (loop_vinfo
);
3495 prologue_cost_vec
.create (2);
3496 epilogue_cost_vec
.create (2);
3497 peel_iters_prologue
= npeel
;
3499 (void) vect_get_known_peeling_cost (loop_vinfo
, peel_iters_prologue
,
3500 &peel_iters_epilogue
,
3501 &LOOP_VINFO_SCALAR_ITERATION_COST
3504 &epilogue_cost_vec
);
3506 FOR_EACH_VEC_ELT (prologue_cost_vec
, j
, si
)
3508 struct _stmt_vec_info
*stmt_info
3509 = si
->stmt
? vinfo_for_stmt (si
->stmt
) : NULL
;
3510 (void) add_stmt_cost (data
, si
->count
, si
->kind
, stmt_info
,
3511 si
->misalign
, vect_prologue
);
3514 FOR_EACH_VEC_ELT (epilogue_cost_vec
, j
, si
)
3516 struct _stmt_vec_info
*stmt_info
3517 = si
->stmt
? vinfo_for_stmt (si
->stmt
) : NULL
;
3518 (void) add_stmt_cost (data
, si
->count
, si
->kind
, stmt_info
,
3519 si
->misalign
, vect_epilogue
);
3522 prologue_cost_vec
.release ();
3523 epilogue_cost_vec
.release ();
3526 /* FORNOW: The scalar outside cost is incremented in one of the
3529 1. The vectorizer checks for alignment and aliasing and generates
3530 a condition that allows dynamic vectorization. A cost model
3531 check is ANDED with the versioning condition. Hence scalar code
3532 path now has the added cost of the versioning check.
3534 if (cost > th & versioning_check)
3537 Hence run-time scalar is incremented by not-taken branch cost.
3539 2. The vectorizer then checks if a prologue is required. If the
3540 cost model check was not done before during versioning, it has to
3541 be done before the prologue check.
3544 prologue = scalar_iters
3549 if (prologue == num_iters)
3552 Hence the run-time scalar cost is incremented by a taken branch,
3553 plus a not-taken branch, plus a taken branch cost.
3555 3. The vectorizer then checks if an epilogue is required. If the
3556 cost model check was not done before during prologue check, it
3557 has to be done with the epilogue check.
3563 if (prologue == num_iters)
3566 if ((cost <= th) | (scalar_iters-prologue-epilogue == 0))
3569 Hence the run-time scalar cost should be incremented by 2 taken
3572 TODO: The back end may reorder the BBS's differently and reverse
3573 conditions/branch directions. Change the estimates below to
3574 something more reasonable. */
3576 /* If the number of iterations is known and we do not do versioning, we can
3577 decide whether to vectorize at compile time. Hence the scalar version
3578 do not carry cost model guard costs. */
3579 if (!LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo
)
3580 || LOOP_REQUIRES_VERSIONING (loop_vinfo
))
3582 /* Cost model check occurs at versioning. */
3583 if (LOOP_REQUIRES_VERSIONING (loop_vinfo
))
3584 scalar_outside_cost
+= vect_get_stmt_cost (cond_branch_not_taken
);
3587 /* Cost model check occurs at prologue generation. */
3588 if (LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo
) < 0)
3589 scalar_outside_cost
+= 2 * vect_get_stmt_cost (cond_branch_taken
)
3590 + vect_get_stmt_cost (cond_branch_not_taken
);
3591 /* Cost model check occurs at epilogue generation. */
3593 scalar_outside_cost
+= 2 * vect_get_stmt_cost (cond_branch_taken
);
3597 /* Complete the target-specific cost calculations. */
3598 finish_cost (LOOP_VINFO_TARGET_COST_DATA (loop_vinfo
), &vec_prologue_cost
,
3599 &vec_inside_cost
, &vec_epilogue_cost
);
3601 vec_outside_cost
= (int)(vec_prologue_cost
+ vec_epilogue_cost
);
3603 if (dump_enabled_p ())
3605 dump_printf_loc (MSG_NOTE
, vect_location
, "Cost model analysis: \n");
3606 dump_printf (MSG_NOTE
, " Vector inside of loop cost: %d\n",
3608 dump_printf (MSG_NOTE
, " Vector prologue cost: %d\n",
3610 dump_printf (MSG_NOTE
, " Vector epilogue cost: %d\n",
3612 dump_printf (MSG_NOTE
, " Scalar iteration cost: %d\n",
3613 scalar_single_iter_cost
);
3614 dump_printf (MSG_NOTE
, " Scalar outside cost: %d\n",
3615 scalar_outside_cost
);
3616 dump_printf (MSG_NOTE
, " Vector outside cost: %d\n",
3618 dump_printf (MSG_NOTE
, " prologue iterations: %d\n",
3619 peel_iters_prologue
);
3620 dump_printf (MSG_NOTE
, " epilogue iterations: %d\n",
3621 peel_iters_epilogue
);
3624 /* Calculate number of iterations required to make the vector version
3625 profitable, relative to the loop bodies only. The following condition
3627 SIC * niters + SOC > VIC * ((niters-PL_ITERS-EP_ITERS)/VF) + VOC
3629 SIC = scalar iteration cost, VIC = vector iteration cost,
3630 VOC = vector outside cost, VF = vectorization factor,
3631 PL_ITERS = prologue iterations, EP_ITERS= epilogue iterations
3632 SOC = scalar outside cost for run time cost model check. */
3634 if ((scalar_single_iter_cost
* vf
) > (int) vec_inside_cost
)
3636 if (vec_outside_cost
<= 0)
3637 min_profitable_iters
= 0;
3640 min_profitable_iters
= ((vec_outside_cost
- scalar_outside_cost
) * vf
3641 - vec_inside_cost
* peel_iters_prologue
3642 - vec_inside_cost
* peel_iters_epilogue
)
3643 / ((scalar_single_iter_cost
* vf
)
3646 if ((scalar_single_iter_cost
* vf
* min_profitable_iters
)
3647 <= (((int) vec_inside_cost
* min_profitable_iters
)
3648 + (((int) vec_outside_cost
- scalar_outside_cost
) * vf
)))
3649 min_profitable_iters
++;
3652 /* vector version will never be profitable. */
3655 if (LOOP_VINFO_LOOP (loop_vinfo
)->force_vectorize
)
3656 warning_at (vect_location
, OPT_Wopenmp_simd
, "vectorization "
3657 "did not happen for a simd loop");
3659 if (dump_enabled_p ())
3660 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
3661 "cost model: the vector iteration cost = %d "
3662 "divided by the scalar iteration cost = %d "
3663 "is greater or equal to the vectorization factor = %d"
3665 vec_inside_cost
, scalar_single_iter_cost
, vf
);
3666 *ret_min_profitable_niters
= -1;
3667 *ret_min_profitable_estimate
= -1;
3671 dump_printf (MSG_NOTE
,
3672 " Calculated minimum iters for profitability: %d\n",
3673 min_profitable_iters
);
3675 /* We want the vectorized loop to execute at least once. */
3676 if (min_profitable_iters
< (vf
+ peel_iters_prologue
))
3677 min_profitable_iters
= vf
+ peel_iters_prologue
;
3679 if (dump_enabled_p ())
3680 dump_printf_loc (MSG_NOTE
, vect_location
,
3681 " Runtime profitability threshold = %d\n",
3682 min_profitable_iters
);
3684 *ret_min_profitable_niters
= min_profitable_iters
;
3686 /* Calculate number of iterations required to make the vector version
3687 profitable, relative to the loop bodies only.
3689 Non-vectorized variant is SIC * niters and it must win over vector
3690 variant on the expected loop trip count. The following condition must hold true:
3691 SIC * niters > VIC * ((niters-PL_ITERS-EP_ITERS)/VF) + VOC + SOC */
3693 if (vec_outside_cost
<= 0)
3694 min_profitable_estimate
= 0;
3697 min_profitable_estimate
= ((vec_outside_cost
+ scalar_outside_cost
) * vf
3698 - vec_inside_cost
* peel_iters_prologue
3699 - vec_inside_cost
* peel_iters_epilogue
)
3700 / ((scalar_single_iter_cost
* vf
)
3703 min_profitable_estimate
= MAX (min_profitable_estimate
, min_profitable_iters
);
3704 if (dump_enabled_p ())
3705 dump_printf_loc (MSG_NOTE
, vect_location
,
3706 " Static estimate profitability threshold = %d\n",
3707 min_profitable_estimate
);
3709 *ret_min_profitable_estimate
= min_profitable_estimate
;
3712 /* Writes into SEL a mask for a vec_perm, equivalent to a vec_shr by OFFSET
3713 vector elements (not bits) for a vector with NELT elements. */
3715 calc_vec_perm_mask_for_shift (unsigned int offset
, unsigned int nelt
,
3716 vec_perm_builder
*sel
)
3718 /* The encoding is a single stepped pattern. Any wrap-around is handled
3719 by vec_perm_indices. */
3720 sel
->new_vector (nelt
, 1, 3);
3721 for (unsigned int i
= 0; i
< 3; i
++)
3722 sel
->quick_push (i
+ offset
);
3725 /* Checks whether the target supports whole-vector shifts for vectors of mode
3726 MODE. This is the case if _either_ the platform handles vec_shr_optab, _or_
3727 it supports vec_perm_const with masks for all necessary shift amounts. */
3729 have_whole_vector_shift (machine_mode mode
)
3731 if (optab_handler (vec_shr_optab
, mode
) != CODE_FOR_nothing
)
3734 unsigned int i
, nelt
= GET_MODE_NUNITS (mode
);
3735 vec_perm_builder sel
;
3736 vec_perm_indices indices
;
3737 for (i
= nelt
/2; i
>= 1; i
/=2)
3739 calc_vec_perm_mask_for_shift (i
, nelt
, &sel
);
3740 indices
.new_vector (sel
, 2, nelt
);
3741 if (!can_vec_perm_const_p (mode
, indices
, false))
3747 /* TODO: Close dependency between vect_model_*_cost and vectorizable_*
3748 functions. Design better to avoid maintenance issues. */
3750 /* Function vect_model_reduction_cost.
3752 Models cost for a reduction operation, including the vector ops
3753 generated within the strip-mine loop, the initial definition before
3754 the loop, and the epilogue code that must be generated. */
3757 vect_model_reduction_cost (stmt_vec_info stmt_info
, internal_fn reduc_fn
,
3760 int prologue_cost
= 0, epilogue_cost
= 0;
3761 enum tree_code code
;
3766 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_info
);
3767 struct loop
*loop
= NULL
;
3768 void *target_cost_data
;
3772 loop
= LOOP_VINFO_LOOP (loop_vinfo
);
3773 target_cost_data
= LOOP_VINFO_TARGET_COST_DATA (loop_vinfo
);
3776 target_cost_data
= BB_VINFO_TARGET_COST_DATA (STMT_VINFO_BB_VINFO (stmt_info
));
3778 /* Condition reductions generate two reductions in the loop. */
3779 if (STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
) == COND_REDUCTION
)
3782 /* Cost of reduction op inside loop. */
3783 unsigned inside_cost
= add_stmt_cost (target_cost_data
, ncopies
, vector_stmt
,
3784 stmt_info
, 0, vect_body
);
3786 vectype
= STMT_VINFO_VECTYPE (stmt_info
);
3787 mode
= TYPE_MODE (vectype
);
3788 orig_stmt
= STMT_VINFO_RELATED_STMT (stmt_info
);
3791 orig_stmt
= STMT_VINFO_STMT (stmt_info
);
3793 code
= gimple_assign_rhs_code (orig_stmt
);
3795 /* Add in cost for initial definition.
3796 For cond reduction we have four vectors: initial index, step, initial
3797 result of the data reduction, initial value of the index reduction. */
3798 int prologue_stmts
= STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
)
3799 == COND_REDUCTION
? 4 : 1;
3800 prologue_cost
+= add_stmt_cost (target_cost_data
, prologue_stmts
,
3801 scalar_to_vec
, stmt_info
, 0,
3804 /* Determine cost of epilogue code.
3806 We have a reduction operator that will reduce the vector in one statement.
3807 Also requires scalar extract. */
3809 if (!loop
|| !nested_in_vect_loop_p (loop
, orig_stmt
))
3811 if (reduc_fn
!= IFN_LAST
)
3813 if (STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
) == COND_REDUCTION
)
3815 /* An EQ stmt and an COND_EXPR stmt. */
3816 epilogue_cost
+= add_stmt_cost (target_cost_data
, 2,
3817 vector_stmt
, stmt_info
, 0,
3819 /* Reduction of the max index and a reduction of the found
3821 epilogue_cost
+= add_stmt_cost (target_cost_data
, 2,
3822 vec_to_scalar
, stmt_info
, 0,
3824 /* A broadcast of the max value. */
3825 epilogue_cost
+= add_stmt_cost (target_cost_data
, 1,
3826 scalar_to_vec
, stmt_info
, 0,
3831 epilogue_cost
+= add_stmt_cost (target_cost_data
, 1, vector_stmt
,
3832 stmt_info
, 0, vect_epilogue
);
3833 epilogue_cost
+= add_stmt_cost (target_cost_data
, 1,
3834 vec_to_scalar
, stmt_info
, 0,
3838 else if (STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
) == COND_REDUCTION
)
3840 unsigned nunits
= TYPE_VECTOR_SUBPARTS (vectype
);
3841 /* Extraction of scalar elements. */
3842 epilogue_cost
+= add_stmt_cost (target_cost_data
, 2 * nunits
,
3843 vec_to_scalar
, stmt_info
, 0,
3845 /* Scalar max reductions via COND_EXPR / MAX_EXPR. */
3846 epilogue_cost
+= add_stmt_cost (target_cost_data
, 2 * nunits
- 3,
3847 scalar_stmt
, stmt_info
, 0,
3852 int vec_size_in_bits
= tree_to_uhwi (TYPE_SIZE (vectype
));
3854 TYPE_SIZE (TREE_TYPE (gimple_assign_lhs (orig_stmt
)));
3855 int element_bitsize
= tree_to_uhwi (bitsize
);
3856 int nelements
= vec_size_in_bits
/ element_bitsize
;
3858 if (code
== COND_EXPR
)
3861 optab
= optab_for_tree_code (code
, vectype
, optab_default
);
3863 /* We have a whole vector shift available. */
3864 if (optab
!= unknown_optab
3865 && VECTOR_MODE_P (mode
)
3866 && optab_handler (optab
, mode
) != CODE_FOR_nothing
3867 && have_whole_vector_shift (mode
))
3869 /* Final reduction via vector shifts and the reduction operator.
3870 Also requires scalar extract. */
3871 epilogue_cost
+= add_stmt_cost (target_cost_data
,
3872 exact_log2 (nelements
) * 2,
3873 vector_stmt
, stmt_info
, 0,
3875 epilogue_cost
+= add_stmt_cost (target_cost_data
, 1,
3876 vec_to_scalar
, stmt_info
, 0,
3880 /* Use extracts and reduction op for final reduction. For N
3881 elements, we have N extracts and N-1 reduction ops. */
3882 epilogue_cost
+= add_stmt_cost (target_cost_data
,
3883 nelements
+ nelements
- 1,
3884 vector_stmt
, stmt_info
, 0,
3889 if (dump_enabled_p ())
3890 dump_printf (MSG_NOTE
,
3891 "vect_model_reduction_cost: inside_cost = %d, "
3892 "prologue_cost = %d, epilogue_cost = %d .\n", inside_cost
,
3893 prologue_cost
, epilogue_cost
);
3897 /* Function vect_model_induction_cost.
3899 Models cost for induction operations. */
3902 vect_model_induction_cost (stmt_vec_info stmt_info
, int ncopies
)
3904 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_info
);
3905 void *target_cost_data
= LOOP_VINFO_TARGET_COST_DATA (loop_vinfo
);
3906 unsigned inside_cost
, prologue_cost
;
3908 if (PURE_SLP_STMT (stmt_info
))
3911 /* loop cost for vec_loop. */
3912 inside_cost
= add_stmt_cost (target_cost_data
, ncopies
, vector_stmt
,
3913 stmt_info
, 0, vect_body
);
3915 /* prologue cost for vec_init and vec_step. */
3916 prologue_cost
= add_stmt_cost (target_cost_data
, 2, scalar_to_vec
,
3917 stmt_info
, 0, vect_prologue
);
3919 if (dump_enabled_p ())
3920 dump_printf_loc (MSG_NOTE
, vect_location
,
3921 "vect_model_induction_cost: inside_cost = %d, "
3922 "prologue_cost = %d .\n", inside_cost
, prologue_cost
);
3927 /* Function get_initial_def_for_reduction
3930 STMT - a stmt that performs a reduction operation in the loop.
3931 INIT_VAL - the initial value of the reduction variable
3934 ADJUSTMENT_DEF - a tree that holds a value to be added to the final result
3935 of the reduction (used for adjusting the epilog - see below).
3936 Return a vector variable, initialized according to the operation that STMT
3937 performs. This vector will be used as the initial value of the
3938 vector of partial results.
3940 Option1 (adjust in epilog): Initialize the vector as follows:
3941 add/bit or/xor: [0,0,...,0,0]
3942 mult/bit and: [1,1,...,1,1]
3943 min/max/cond_expr: [init_val,init_val,..,init_val,init_val]
3944 and when necessary (e.g. add/mult case) let the caller know
3945 that it needs to adjust the result by init_val.
3947 Option2: Initialize the vector as follows:
3948 add/bit or/xor: [init_val,0,0,...,0]
3949 mult/bit and: [init_val,1,1,...,1]
3950 min/max/cond_expr: [init_val,init_val,...,init_val]
3951 and no adjustments are needed.
3953 For example, for the following code:
3959 STMT is 's = s + a[i]', and the reduction variable is 's'.
3960 For a vector of 4 units, we want to return either [0,0,0,init_val],
3961 or [0,0,0,0] and let the caller know that it needs to adjust
3962 the result at the end by 'init_val'.
3964 FORNOW, we are using the 'adjust in epilog' scheme, because this way the
3965 initialization vector is simpler (same element in all entries), if
3966 ADJUSTMENT_DEF is not NULL, and Option2 otherwise.
3968 A cost model should help decide between these two schemes. */
3971 get_initial_def_for_reduction (gimple
*stmt
, tree init_val
,
3972 tree
*adjustment_def
)
3974 stmt_vec_info stmt_vinfo
= vinfo_for_stmt (stmt
);
3975 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_vinfo
);
3976 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
3977 tree scalar_type
= TREE_TYPE (init_val
);
3978 tree vectype
= get_vectype_for_scalar_type (scalar_type
);
3979 enum tree_code code
= gimple_assign_rhs_code (stmt
);
3982 bool nested_in_vect_loop
= false;
3983 REAL_VALUE_TYPE real_init_val
= dconst0
;
3984 int int_init_val
= 0;
3985 gimple
*def_stmt
= NULL
;
3986 gimple_seq stmts
= NULL
;
3988 gcc_assert (vectype
);
3990 gcc_assert (POINTER_TYPE_P (scalar_type
) || INTEGRAL_TYPE_P (scalar_type
)
3991 || SCALAR_FLOAT_TYPE_P (scalar_type
));
3993 if (nested_in_vect_loop_p (loop
, stmt
))
3994 nested_in_vect_loop
= true;
3996 gcc_assert (loop
== (gimple_bb (stmt
))->loop_father
);
3998 /* In case of double reduction we only create a vector variable to be put
3999 in the reduction phi node. The actual statement creation is done in
4000 vect_create_epilog_for_reduction. */
4001 if (adjustment_def
&& nested_in_vect_loop
4002 && TREE_CODE (init_val
) == SSA_NAME
4003 && (def_stmt
= SSA_NAME_DEF_STMT (init_val
))
4004 && gimple_code (def_stmt
) == GIMPLE_PHI
4005 && flow_bb_inside_loop_p (loop
, gimple_bb (def_stmt
))
4006 && vinfo_for_stmt (def_stmt
)
4007 && STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def_stmt
))
4008 == vect_double_reduction_def
)
4010 *adjustment_def
= NULL
;
4011 return vect_create_destination_var (init_val
, vectype
);
4014 /* In case of a nested reduction do not use an adjustment def as
4015 that case is not supported by the epilogue generation correctly
4016 if ncopies is not one. */
4017 if (adjustment_def
&& nested_in_vect_loop
)
4019 *adjustment_def
= NULL
;
4020 return vect_get_vec_def_for_operand (init_val
, stmt
);
4025 case WIDEN_SUM_EXPR
:
4035 /* ADJUSTMENT_DEF is NULL when called from
4036 vect_create_epilog_for_reduction to vectorize double reduction. */
4038 *adjustment_def
= init_val
;
4040 if (code
== MULT_EXPR
)
4042 real_init_val
= dconst1
;
4046 if (code
== BIT_AND_EXPR
)
4049 if (SCALAR_FLOAT_TYPE_P (scalar_type
))
4050 def_for_init
= build_real (scalar_type
, real_init_val
);
4052 def_for_init
= build_int_cst (scalar_type
, int_init_val
);
4055 /* Option1: the first element is '0' or '1' as well. */
4056 init_def
= gimple_build_vector_from_val (&stmts
, vectype
,
4060 /* Option2: the first element is INIT_VAL. */
4061 tree_vector_builder
elts (vectype
, 1, 2);
4062 elts
.quick_push (init_val
);
4063 elts
.quick_push (def_for_init
);
4064 init_def
= gimple_build_vector (&stmts
, &elts
);
4075 *adjustment_def
= NULL_TREE
;
4076 if (STMT_VINFO_VEC_REDUCTION_TYPE (stmt_vinfo
) != COND_REDUCTION
)
4078 init_def
= vect_get_vec_def_for_operand (init_val
, stmt
);
4082 init_val
= gimple_convert (&stmts
, TREE_TYPE (vectype
), init_val
);
4083 init_def
= gimple_build_vector_from_val (&stmts
, vectype
, init_val
);
4092 gsi_insert_seq_on_edge_immediate (loop_preheader_edge (loop
), stmts
);
4096 /* Get at the initial defs for the reduction PHIs in SLP_NODE.
4097 NUMBER_OF_VECTORS is the number of vector defs to create. */
4100 get_initial_defs_for_reduction (slp_tree slp_node
,
4101 vec
<tree
> *vec_oprnds
,
4102 unsigned int number_of_vectors
,
4103 enum tree_code code
, bool reduc_chain
)
4105 vec
<gimple
*> stmts
= SLP_TREE_SCALAR_STMTS (slp_node
);
4106 gimple
*stmt
= stmts
[0];
4107 stmt_vec_info stmt_vinfo
= vinfo_for_stmt (stmt
);
4109 unsigned j
, number_of_places_left_in_vector
;
4110 tree vector_type
, scalar_type
;
4112 int group_size
= stmts
.length ();
4113 unsigned int vec_num
, i
;
4114 unsigned number_of_copies
= 1;
4116 voprnds
.create (number_of_vectors
);
4117 tree neutral_op
= NULL
;
4120 vector_type
= STMT_VINFO_VECTYPE (stmt_vinfo
);
4121 scalar_type
= TREE_TYPE (vector_type
);
4122 nunits
= TYPE_VECTOR_SUBPARTS (vector_type
);
4124 gcc_assert (STMT_VINFO_DEF_TYPE (stmt_vinfo
) == vect_reduction_def
);
4126 loop
= (gimple_bb (stmt
))->loop_father
;
4128 edge pe
= loop_preheader_edge (loop
);
4130 /* op is the reduction operand of the first stmt already. */
4131 /* For additional copies (see the explanation of NUMBER_OF_COPIES below)
4132 we need either neutral operands or the original operands. See
4133 get_initial_def_for_reduction() for details. */
4136 case WIDEN_SUM_EXPR
:
4143 neutral_op
= build_zero_cst (scalar_type
);
4147 neutral_op
= build_one_cst (scalar_type
);
4151 neutral_op
= build_all_ones_cst (scalar_type
);
4154 /* For MIN/MAX we don't have an easy neutral operand but
4155 the initial values can be used fine here. Only for
4156 a reduction chain we have to force a neutral element. */
4162 neutral_op
= PHI_ARG_DEF_FROM_EDGE (stmt
, pe
);
4166 gcc_assert (! reduc_chain
);
4170 /* NUMBER_OF_COPIES is the number of times we need to use the same values in
4171 created vectors. It is greater than 1 if unrolling is performed.
4173 For example, we have two scalar operands, s1 and s2 (e.g., group of
4174 strided accesses of size two), while NUNITS is four (i.e., four scalars
4175 of this type can be packed in a vector). The output vector will contain
4176 two copies of each scalar operand: {s1, s2, s1, s2}. (NUMBER_OF_COPIES
4179 If GROUP_SIZE > NUNITS, the scalars will be split into several vectors
4180 containing the operands.
4182 For example, NUNITS is four as before, and the group size is 8
4183 (s1, s2, ..., s8). We will create two vectors {s1, s2, s3, s4} and
4184 {s5, s6, s7, s8}. */
4186 number_of_copies
= nunits
* number_of_vectors
/ group_size
;
4188 number_of_places_left_in_vector
= nunits
;
4189 tree_vector_builder
elts (vector_type
, nunits
, 1);
4190 elts
.quick_grow (nunits
);
4191 for (j
= 0; j
< number_of_copies
; j
++)
4193 for (i
= group_size
- 1; stmts
.iterate (i
, &stmt
); i
--)
4196 /* Get the def before the loop. In reduction chain we have only
4197 one initial value. */
4198 if ((j
!= (number_of_copies
- 1)
4199 || (reduc_chain
&& i
!= 0))
4203 op
= PHI_ARG_DEF_FROM_EDGE (stmt
, pe
);
4205 /* Create 'vect_ = {op0,op1,...,opn}'. */
4206 number_of_places_left_in_vector
--;
4207 elts
[number_of_places_left_in_vector
] = op
;
4209 if (number_of_places_left_in_vector
== 0)
4211 gimple_seq ctor_seq
= NULL
;
4212 tree init
= gimple_build_vector (&ctor_seq
, &elts
);
4213 if (ctor_seq
!= NULL
)
4214 gsi_insert_seq_on_edge_immediate (pe
, ctor_seq
);
4215 voprnds
.quick_push (init
);
4217 number_of_places_left_in_vector
= nunits
;
4218 elts
.new_vector (vector_type
, nunits
, 1);
4219 elts
.quick_grow (nunits
);
4224 /* Since the vectors are created in the reverse order, we should invert
4226 vec_num
= voprnds
.length ();
4227 for (j
= vec_num
; j
!= 0; j
--)
4229 vop
= voprnds
[j
- 1];
4230 vec_oprnds
->quick_push (vop
);
4235 /* In case that VF is greater than the unrolling factor needed for the SLP
4236 group of stmts, NUMBER_OF_VECTORS to be created is greater than
4237 NUMBER_OF_SCALARS/NUNITS or NUNITS/NUMBER_OF_SCALARS, and hence we have
4238 to replicate the vectors. */
4239 tree neutral_vec
= NULL
;
4240 while (number_of_vectors
> vec_oprnds
->length ())
4246 gimple_seq ctor_seq
= NULL
;
4247 neutral_vec
= gimple_build_vector_from_val
4248 (&ctor_seq
, vector_type
, neutral_op
);
4249 if (ctor_seq
!= NULL
)
4250 gsi_insert_seq_on_edge_immediate (pe
, ctor_seq
);
4252 vec_oprnds
->quick_push (neutral_vec
);
4256 for (i
= 0; vec_oprnds
->iterate (i
, &vop
) && i
< vec_num
; i
++)
4257 vec_oprnds
->quick_push (vop
);
4263 /* Function vect_create_epilog_for_reduction
4265 Create code at the loop-epilog to finalize the result of a reduction
4268 VECT_DEFS is list of vector of partial results, i.e., the lhs's of vector
4269 reduction statements.
4270 STMT is the scalar reduction stmt that is being vectorized.
4271 NCOPIES is > 1 in case the vectorization factor (VF) is bigger than the
4272 number of elements that we can fit in a vectype (nunits). In this case
4273 we have to generate more than one vector stmt - i.e - we need to "unroll"
4274 the vector stmt by a factor VF/nunits. For more details see documentation
4275 in vectorizable_operation.
4276 REDUC_FN is the internal function for the epilog reduction.
4277 REDUCTION_PHIS is a list of the phi-nodes that carry the reduction
4279 REDUC_INDEX is the index of the operand in the right hand side of the
4280 statement that is defined by REDUCTION_PHI.
4281 DOUBLE_REDUC is TRUE if double reduction phi nodes should be handled.
4282 SLP_NODE is an SLP node containing a group of reduction statements. The
4283 first one in this group is STMT.
4284 INDUC_VAL is for INTEGER_INDUC_COND_REDUCTION the value to use for the case
4285 when the COND_EXPR is never true in the loop. For MAX_EXPR, it needs to
4286 be smaller than any value of the IV in the loop, for MIN_EXPR larger than
4287 any value of the IV in the loop.
4288 INDUC_CODE is the code for epilog reduction if INTEGER_INDUC_COND_REDUCTION.
4291 1. Creates the reduction def-use cycles: sets the arguments for
4293 The loop-entry argument is the vectorized initial-value of the reduction.
4294 The loop-latch argument is taken from VECT_DEFS - the vector of partial
4296 2. "Reduces" each vector of partial results VECT_DEFS into a single result,
4297 by calling the function specified by REDUC_FN if available, or by
4298 other means (whole-vector shifts or a scalar loop).
4299 The function also creates a new phi node at the loop exit to preserve
4300 loop-closed form, as illustrated below.
4302 The flow at the entry to this function:
4305 vec_def = phi <null, null> # REDUCTION_PHI
4306 VECT_DEF = vector_stmt # vectorized form of STMT
4307 s_loop = scalar_stmt # (scalar) STMT
4309 s_out0 = phi <s_loop> # (scalar) EXIT_PHI
4313 The above is transformed by this function into:
4316 vec_def = phi <vec_init, VECT_DEF> # REDUCTION_PHI
4317 VECT_DEF = vector_stmt # vectorized form of STMT
4318 s_loop = scalar_stmt # (scalar) STMT
4320 s_out0 = phi <s_loop> # (scalar) EXIT_PHI
4321 v_out1 = phi <VECT_DEF> # NEW_EXIT_PHI
4322 v_out2 = reduce <v_out1>
4323 s_out3 = extract_field <v_out2, 0>
4324 s_out4 = adjust_result <s_out3>
4330 vect_create_epilog_for_reduction (vec
<tree
> vect_defs
, gimple
*stmt
,
4331 gimple
*reduc_def_stmt
,
4332 int ncopies
, internal_fn reduc_fn
,
4333 vec
<gimple
*> reduction_phis
,
4336 slp_instance slp_node_instance
,
4337 tree induc_val
, enum tree_code induc_code
)
4339 stmt_vec_info stmt_info
= vinfo_for_stmt (stmt
);
4340 stmt_vec_info prev_phi_info
;
4343 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_info
);
4344 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
), *outer_loop
= NULL
;
4345 basic_block exit_bb
;
4348 gimple
*new_phi
= NULL
, *phi
;
4349 gimple_stmt_iterator exit_gsi
;
4351 tree new_temp
= NULL_TREE
, new_dest
, new_name
, new_scalar_dest
;
4352 gimple
*epilog_stmt
= NULL
;
4353 enum tree_code code
= gimple_assign_rhs_code (stmt
);
4356 tree adjustment_def
= NULL
;
4357 tree vec_initial_def
= NULL
;
4358 tree expr
, def
, initial_def
= NULL
;
4359 tree orig_name
, scalar_result
;
4360 imm_use_iterator imm_iter
, phi_imm_iter
;
4361 use_operand_p use_p
, phi_use_p
;
4362 gimple
*use_stmt
, *orig_stmt
, *reduction_phi
= NULL
;
4363 bool nested_in_vect_loop
= false;
4364 auto_vec
<gimple
*> new_phis
;
4365 auto_vec
<gimple
*> inner_phis
;
4366 enum vect_def_type dt
= vect_unknown_def_type
;
4368 auto_vec
<tree
> scalar_results
;
4369 unsigned int group_size
= 1, k
, ratio
;
4370 auto_vec
<tree
> vec_initial_defs
;
4371 auto_vec
<gimple
*> phis
;
4372 bool slp_reduc
= false;
4373 tree new_phi_result
;
4374 gimple
*inner_phi
= NULL
;
4375 tree induction_index
= NULL_TREE
;
4378 group_size
= SLP_TREE_SCALAR_STMTS (slp_node
).length ();
4380 if (nested_in_vect_loop_p (loop
, stmt
))
4384 nested_in_vect_loop
= true;
4385 gcc_assert (!slp_node
);
4388 vectype
= STMT_VINFO_VECTYPE (stmt_info
);
4389 gcc_assert (vectype
);
4390 mode
= TYPE_MODE (vectype
);
4392 /* 1. Create the reduction def-use cycle:
4393 Set the arguments of REDUCTION_PHIS, i.e., transform
4396 vec_def = phi <null, null> # REDUCTION_PHI
4397 VECT_DEF = vector_stmt # vectorized form of STMT
4403 vec_def = phi <vec_init, VECT_DEF> # REDUCTION_PHI
4404 VECT_DEF = vector_stmt # vectorized form of STMT
4407 (in case of SLP, do it for all the phis). */
4409 /* Get the loop-entry arguments. */
4410 enum vect_def_type initial_def_dt
= vect_unknown_def_type
;
4413 unsigned vec_num
= SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node
);
4414 vec_initial_defs
.reserve (vec_num
);
4415 get_initial_defs_for_reduction (slp_node_instance
->reduc_phis
,
4416 &vec_initial_defs
, vec_num
, code
,
4417 GROUP_FIRST_ELEMENT (stmt_info
));
4421 /* Get at the scalar def before the loop, that defines the initial value
4422 of the reduction variable. */
4424 initial_def
= PHI_ARG_DEF_FROM_EDGE (reduc_def_stmt
,
4425 loop_preheader_edge (loop
));
4426 /* Optimize: if initial_def is for REDUC_MAX smaller than the base
4427 and we can't use zero for induc_val, use initial_def. Similarly
4428 for REDUC_MIN and initial_def larger than the base. */
4429 if (TREE_CODE (initial_def
) == INTEGER_CST
4430 && (STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
)
4431 == INTEGER_INDUC_COND_REDUCTION
)
4432 && !integer_zerop (induc_val
)
4433 && ((induc_code
== MAX_EXPR
4434 && tree_int_cst_lt (initial_def
, induc_val
))
4435 || (induc_code
== MIN_EXPR
4436 && tree_int_cst_lt (induc_val
, initial_def
))))
4437 induc_val
= initial_def
;
4438 vect_is_simple_use (initial_def
, loop_vinfo
, &def_stmt
, &initial_def_dt
);
4439 vec_initial_def
= get_initial_def_for_reduction (stmt
, initial_def
,
4441 vec_initial_defs
.create (1);
4442 vec_initial_defs
.quick_push (vec_initial_def
);
4445 /* Set phi nodes arguments. */
4446 FOR_EACH_VEC_ELT (reduction_phis
, i
, phi
)
4448 tree vec_init_def
= vec_initial_defs
[i
];
4449 tree def
= vect_defs
[i
];
4450 for (j
= 0; j
< ncopies
; j
++)
4454 phi
= STMT_VINFO_RELATED_STMT (vinfo_for_stmt (phi
));
4455 if (nested_in_vect_loop
)
4457 = vect_get_vec_def_for_stmt_copy (initial_def_dt
,
4461 /* Set the loop-entry arg of the reduction-phi. */
4463 if (STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
)
4464 == INTEGER_INDUC_COND_REDUCTION
)
4466 /* Initialise the reduction phi to zero. This prevents initial
4467 values of non-zero interferring with the reduction op. */
4468 gcc_assert (ncopies
== 1);
4469 gcc_assert (i
== 0);
4471 tree vec_init_def_type
= TREE_TYPE (vec_init_def
);
4473 = build_vector_from_val (vec_init_def_type
, induc_val
);
4475 add_phi_arg (as_a
<gphi
*> (phi
), induc_val_vec
,
4476 loop_preheader_edge (loop
), UNKNOWN_LOCATION
);
4479 add_phi_arg (as_a
<gphi
*> (phi
), vec_init_def
,
4480 loop_preheader_edge (loop
), UNKNOWN_LOCATION
);
4482 /* Set the loop-latch arg for the reduction-phi. */
4484 def
= vect_get_vec_def_for_stmt_copy (vect_unknown_def_type
, def
);
4486 add_phi_arg (as_a
<gphi
*> (phi
), def
, loop_latch_edge (loop
),
4489 if (dump_enabled_p ())
4491 dump_printf_loc (MSG_NOTE
, vect_location
,
4492 "transform reduction: created def-use cycle: ");
4493 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
, phi
, 0);
4494 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
, SSA_NAME_DEF_STMT (def
), 0);
4499 /* For cond reductions we want to create a new vector (INDEX_COND_EXPR)
4500 which is updated with the current index of the loop for every match of
4501 the original loop's cond_expr (VEC_STMT). This results in a vector
4502 containing the last time the condition passed for that vector lane.
4503 The first match will be a 1 to allow 0 to be used for non-matching
4504 indexes. If there are no matches at all then the vector will be all
4506 if (STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
) == COND_REDUCTION
)
4508 tree indx_before_incr
, indx_after_incr
;
4509 int nunits_out
= TYPE_VECTOR_SUBPARTS (vectype
);
4512 gimple
*vec_stmt
= STMT_VINFO_VEC_STMT (stmt_info
);
4513 gcc_assert (gimple_assign_rhs_code (vec_stmt
) == VEC_COND_EXPR
);
4515 int scalar_precision
4516 = GET_MODE_PRECISION (SCALAR_TYPE_MODE (TREE_TYPE (vectype
)));
4517 tree cr_index_scalar_type
= make_unsigned_type (scalar_precision
);
4518 tree cr_index_vector_type
= build_vector_type
4519 (cr_index_scalar_type
, TYPE_VECTOR_SUBPARTS (vectype
));
4521 /* First we create a simple vector induction variable which starts
4522 with the values {1,2,3,...} (SERIES_VECT) and increments by the
4523 vector size (STEP). */
4525 /* Create a {1,2,3,...} vector. */
4526 tree_vector_builder
vtemp (cr_index_vector_type
, 1, 3);
4527 for (k
= 0; k
< 3; ++k
)
4528 vtemp
.quick_push (build_int_cst (cr_index_scalar_type
, k
+ 1));
4529 tree series_vect
= vtemp
.build ();
4531 /* Create a vector of the step value. */
4532 tree step
= build_int_cst (cr_index_scalar_type
, nunits_out
);
4533 tree vec_step
= build_vector_from_val (cr_index_vector_type
, step
);
4535 /* Create an induction variable. */
4536 gimple_stmt_iterator incr_gsi
;
4538 standard_iv_increment_position (loop
, &incr_gsi
, &insert_after
);
4539 create_iv (series_vect
, vec_step
, NULL_TREE
, loop
, &incr_gsi
,
4540 insert_after
, &indx_before_incr
, &indx_after_incr
);
4542 /* Next create a new phi node vector (NEW_PHI_TREE) which starts
4543 filled with zeros (VEC_ZERO). */
4545 /* Create a vector of 0s. */
4546 tree zero
= build_zero_cst (cr_index_scalar_type
);
4547 tree vec_zero
= build_vector_from_val (cr_index_vector_type
, zero
);
4549 /* Create a vector phi node. */
4550 tree new_phi_tree
= make_ssa_name (cr_index_vector_type
);
4551 new_phi
= create_phi_node (new_phi_tree
, loop
->header
);
4552 set_vinfo_for_stmt (new_phi
,
4553 new_stmt_vec_info (new_phi
, loop_vinfo
));
4554 add_phi_arg (as_a
<gphi
*> (new_phi
), vec_zero
,
4555 loop_preheader_edge (loop
), UNKNOWN_LOCATION
);
4557 /* Now take the condition from the loops original cond_expr
4558 (VEC_STMT) and produce a new cond_expr (INDEX_COND_EXPR) which for
4559 every match uses values from the induction variable
4560 (INDEX_BEFORE_INCR) otherwise uses values from the phi node
4562 Finally, we update the phi (NEW_PHI_TREE) to take the value of
4563 the new cond_expr (INDEX_COND_EXPR). */
4565 /* Duplicate the condition from vec_stmt. */
4566 tree ccompare
= unshare_expr (gimple_assign_rhs1 (vec_stmt
));
4568 /* Create a conditional, where the condition is taken from vec_stmt
4569 (CCOMPARE), then is the induction index (INDEX_BEFORE_INCR) and
4570 else is the phi (NEW_PHI_TREE). */
4571 tree index_cond_expr
= build3 (VEC_COND_EXPR
, cr_index_vector_type
,
4572 ccompare
, indx_before_incr
,
4574 induction_index
= make_ssa_name (cr_index_vector_type
);
4575 gimple
*index_condition
= gimple_build_assign (induction_index
,
4577 gsi_insert_before (&incr_gsi
, index_condition
, GSI_SAME_STMT
);
4578 stmt_vec_info index_vec_info
= new_stmt_vec_info (index_condition
,
4580 STMT_VINFO_VECTYPE (index_vec_info
) = cr_index_vector_type
;
4581 set_vinfo_for_stmt (index_condition
, index_vec_info
);
4583 /* Update the phi with the vec cond. */
4584 add_phi_arg (as_a
<gphi
*> (new_phi
), induction_index
,
4585 loop_latch_edge (loop
), UNKNOWN_LOCATION
);
4588 /* 2. Create epilog code.
4589 The reduction epilog code operates across the elements of the vector
4590 of partial results computed by the vectorized loop.
4591 The reduction epilog code consists of:
4593 step 1: compute the scalar result in a vector (v_out2)
4594 step 2: extract the scalar result (s_out3) from the vector (v_out2)
4595 step 3: adjust the scalar result (s_out3) if needed.
4597 Step 1 can be accomplished using one the following three schemes:
4598 (scheme 1) using reduc_fn, if available.
4599 (scheme 2) using whole-vector shifts, if available.
4600 (scheme 3) using a scalar loop. In this case steps 1+2 above are
4603 The overall epilog code looks like this:
4605 s_out0 = phi <s_loop> # original EXIT_PHI
4606 v_out1 = phi <VECT_DEF> # NEW_EXIT_PHI
4607 v_out2 = reduce <v_out1> # step 1
4608 s_out3 = extract_field <v_out2, 0> # step 2
4609 s_out4 = adjust_result <s_out3> # step 3
4611 (step 3 is optional, and steps 1 and 2 may be combined).
4612 Lastly, the uses of s_out0 are replaced by s_out4. */
4615 /* 2.1 Create new loop-exit-phis to preserve loop-closed form:
4616 v_out1 = phi <VECT_DEF>
4617 Store them in NEW_PHIS. */
4619 exit_bb
= single_exit (loop
)->dest
;
4620 prev_phi_info
= NULL
;
4621 new_phis
.create (vect_defs
.length ());
4622 FOR_EACH_VEC_ELT (vect_defs
, i
, def
)
4624 for (j
= 0; j
< ncopies
; j
++)
4626 tree new_def
= copy_ssa_name (def
);
4627 phi
= create_phi_node (new_def
, exit_bb
);
4628 set_vinfo_for_stmt (phi
, new_stmt_vec_info (phi
, loop_vinfo
));
4630 new_phis
.quick_push (phi
);
4633 def
= vect_get_vec_def_for_stmt_copy (dt
, def
);
4634 STMT_VINFO_RELATED_STMT (prev_phi_info
) = phi
;
4637 SET_PHI_ARG_DEF (phi
, single_exit (loop
)->dest_idx
, def
);
4638 prev_phi_info
= vinfo_for_stmt (phi
);
4642 /* The epilogue is created for the outer-loop, i.e., for the loop being
4643 vectorized. Create exit phis for the outer loop. */
4647 exit_bb
= single_exit (loop
)->dest
;
4648 inner_phis
.create (vect_defs
.length ());
4649 FOR_EACH_VEC_ELT (new_phis
, i
, phi
)
4651 tree new_result
= copy_ssa_name (PHI_RESULT (phi
));
4652 gphi
*outer_phi
= create_phi_node (new_result
, exit_bb
);
4653 SET_PHI_ARG_DEF (outer_phi
, single_exit (loop
)->dest_idx
,
4655 set_vinfo_for_stmt (outer_phi
, new_stmt_vec_info (outer_phi
,
4657 inner_phis
.quick_push (phi
);
4658 new_phis
[i
] = outer_phi
;
4659 prev_phi_info
= vinfo_for_stmt (outer_phi
);
4660 while (STMT_VINFO_RELATED_STMT (vinfo_for_stmt (phi
)))
4662 phi
= STMT_VINFO_RELATED_STMT (vinfo_for_stmt (phi
));
4663 new_result
= copy_ssa_name (PHI_RESULT (phi
));
4664 outer_phi
= create_phi_node (new_result
, exit_bb
);
4665 SET_PHI_ARG_DEF (outer_phi
, single_exit (loop
)->dest_idx
,
4667 set_vinfo_for_stmt (outer_phi
, new_stmt_vec_info (outer_phi
,
4669 STMT_VINFO_RELATED_STMT (prev_phi_info
) = outer_phi
;
4670 prev_phi_info
= vinfo_for_stmt (outer_phi
);
4675 exit_gsi
= gsi_after_labels (exit_bb
);
4677 /* 2.2 Get the relevant tree-code to use in the epilog for schemes 2,3
4678 (i.e. when reduc_fn is not available) and in the final adjustment
4679 code (if needed). Also get the original scalar reduction variable as
4680 defined in the loop. In case STMT is a "pattern-stmt" (i.e. - it
4681 represents a reduction pattern), the tree-code and scalar-def are
4682 taken from the original stmt that the pattern-stmt (STMT) replaces.
4683 Otherwise (it is a regular reduction) - the tree-code and scalar-def
4684 are taken from STMT. */
4686 orig_stmt
= STMT_VINFO_RELATED_STMT (stmt_info
);
4689 /* Regular reduction */
4694 /* Reduction pattern */
4695 stmt_vec_info stmt_vinfo
= vinfo_for_stmt (orig_stmt
);
4696 gcc_assert (STMT_VINFO_IN_PATTERN_P (stmt_vinfo
));
4697 gcc_assert (STMT_VINFO_RELATED_STMT (stmt_vinfo
) == stmt
);
4700 code
= gimple_assign_rhs_code (orig_stmt
);
4701 /* For MINUS_EXPR the initial vector is [init_val,0,...,0], therefore,
4702 partial results are added and not subtracted. */
4703 if (code
== MINUS_EXPR
)
4706 scalar_dest
= gimple_assign_lhs (orig_stmt
);
4707 scalar_type
= TREE_TYPE (scalar_dest
);
4708 scalar_results
.create (group_size
);
4709 new_scalar_dest
= vect_create_destination_var (scalar_dest
, NULL
);
4710 bitsize
= TYPE_SIZE (scalar_type
);
4712 /* In case this is a reduction in an inner-loop while vectorizing an outer
4713 loop - we don't need to extract a single scalar result at the end of the
4714 inner-loop (unless it is double reduction, i.e., the use of reduction is
4715 outside the outer-loop). The final vector of partial results will be used
4716 in the vectorized outer-loop, or reduced to a scalar result at the end of
4718 if (nested_in_vect_loop
&& !double_reduc
)
4719 goto vect_finalize_reduction
;
4721 /* SLP reduction without reduction chain, e.g.,
4725 b2 = operation (b1) */
4726 slp_reduc
= (slp_node
&& !GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt
)));
4728 /* In case of reduction chain, e.g.,
4731 a3 = operation (a2),
4733 we may end up with more than one vector result. Here we reduce them to
4735 if (GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt
)))
4737 tree first_vect
= PHI_RESULT (new_phis
[0]);
4738 gassign
*new_vec_stmt
= NULL
;
4739 vec_dest
= vect_create_destination_var (scalar_dest
, vectype
);
4740 for (k
= 1; k
< new_phis
.length (); k
++)
4742 gimple
*next_phi
= new_phis
[k
];
4743 tree second_vect
= PHI_RESULT (next_phi
);
4744 tree tem
= make_ssa_name (vec_dest
, new_vec_stmt
);
4745 new_vec_stmt
= gimple_build_assign (tem
, code
,
4746 first_vect
, second_vect
);
4747 gsi_insert_before (&exit_gsi
, new_vec_stmt
, GSI_SAME_STMT
);
4751 new_phi_result
= first_vect
;
4754 new_phis
.truncate (0);
4755 new_phis
.safe_push (new_vec_stmt
);
4758 /* Likewise if we couldn't use a single defuse cycle. */
4759 else if (ncopies
> 1)
4761 gcc_assert (new_phis
.length () == 1);
4762 tree first_vect
= PHI_RESULT (new_phis
[0]);
4763 gassign
*new_vec_stmt
= NULL
;
4764 vec_dest
= vect_create_destination_var (scalar_dest
, vectype
);
4765 gimple
*next_phi
= new_phis
[0];
4766 for (int k
= 1; k
< ncopies
; ++k
)
4768 next_phi
= STMT_VINFO_RELATED_STMT (vinfo_for_stmt (next_phi
));
4769 tree second_vect
= PHI_RESULT (next_phi
);
4770 tree tem
= make_ssa_name (vec_dest
, new_vec_stmt
);
4771 new_vec_stmt
= gimple_build_assign (tem
, code
,
4772 first_vect
, second_vect
);
4773 gsi_insert_before (&exit_gsi
, new_vec_stmt
, GSI_SAME_STMT
);
4776 new_phi_result
= first_vect
;
4777 new_phis
.truncate (0);
4778 new_phis
.safe_push (new_vec_stmt
);
4781 new_phi_result
= PHI_RESULT (new_phis
[0]);
4783 if (STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
) == COND_REDUCTION
4784 && reduc_fn
!= IFN_LAST
)
4786 /* For condition reductions, we have a vector (NEW_PHI_RESULT) containing
4787 various data values where the condition matched and another vector
4788 (INDUCTION_INDEX) containing all the indexes of those matches. We
4789 need to extract the last matching index (which will be the index with
4790 highest value) and use this to index into the data vector.
4791 For the case where there were no matches, the data vector will contain
4792 all default values and the index vector will be all zeros. */
4794 /* Get various versions of the type of the vector of indexes. */
4795 tree index_vec_type
= TREE_TYPE (induction_index
);
4796 gcc_checking_assert (TYPE_UNSIGNED (index_vec_type
));
4797 tree index_scalar_type
= TREE_TYPE (index_vec_type
);
4798 tree index_vec_cmp_type
= build_same_sized_truth_vector_type
4801 /* Get an unsigned integer version of the type of the data vector. */
4802 int scalar_precision
4803 = GET_MODE_PRECISION (SCALAR_TYPE_MODE (scalar_type
));
4804 tree scalar_type_unsigned
= make_unsigned_type (scalar_precision
);
4805 tree vectype_unsigned
= build_vector_type
4806 (scalar_type_unsigned
, TYPE_VECTOR_SUBPARTS (vectype
));
4808 /* First we need to create a vector (ZERO_VEC) of zeros and another
4809 vector (MAX_INDEX_VEC) filled with the last matching index, which we
4810 can create using a MAX reduction and then expanding.
4811 In the case where the loop never made any matches, the max index will
4814 /* Vector of {0, 0, 0,...}. */
4815 tree zero_vec
= make_ssa_name (vectype
);
4816 tree zero_vec_rhs
= build_zero_cst (vectype
);
4817 gimple
*zero_vec_stmt
= gimple_build_assign (zero_vec
, zero_vec_rhs
);
4818 gsi_insert_before (&exit_gsi
, zero_vec_stmt
, GSI_SAME_STMT
);
4820 /* Find maximum value from the vector of found indexes. */
4821 tree max_index
= make_ssa_name (index_scalar_type
);
4822 gcall
*max_index_stmt
= gimple_build_call_internal (IFN_REDUC_MAX
,
4823 1, induction_index
);
4824 gimple_call_set_lhs (max_index_stmt
, max_index
);
4825 gsi_insert_before (&exit_gsi
, max_index_stmt
, GSI_SAME_STMT
);
4827 /* Vector of {max_index, max_index, max_index,...}. */
4828 tree max_index_vec
= make_ssa_name (index_vec_type
);
4829 tree max_index_vec_rhs
= build_vector_from_val (index_vec_type
,
4831 gimple
*max_index_vec_stmt
= gimple_build_assign (max_index_vec
,
4833 gsi_insert_before (&exit_gsi
, max_index_vec_stmt
, GSI_SAME_STMT
);
4835 /* Next we compare the new vector (MAX_INDEX_VEC) full of max indexes
4836 with the vector (INDUCTION_INDEX) of found indexes, choosing values
4837 from the data vector (NEW_PHI_RESULT) for matches, 0 (ZERO_VEC)
4838 otherwise. Only one value should match, resulting in a vector
4839 (VEC_COND) with one data value and the rest zeros.
4840 In the case where the loop never made any matches, every index will
4841 match, resulting in a vector with all data values (which will all be
4842 the default value). */
4844 /* Compare the max index vector to the vector of found indexes to find
4845 the position of the max value. */
4846 tree vec_compare
= make_ssa_name (index_vec_cmp_type
);
4847 gimple
*vec_compare_stmt
= gimple_build_assign (vec_compare
, EQ_EXPR
,
4850 gsi_insert_before (&exit_gsi
, vec_compare_stmt
, GSI_SAME_STMT
);
4852 /* Use the compare to choose either values from the data vector or
4854 tree vec_cond
= make_ssa_name (vectype
);
4855 gimple
*vec_cond_stmt
= gimple_build_assign (vec_cond
, VEC_COND_EXPR
,
4856 vec_compare
, new_phi_result
,
4858 gsi_insert_before (&exit_gsi
, vec_cond_stmt
, GSI_SAME_STMT
);
4860 /* Finally we need to extract the data value from the vector (VEC_COND)
4861 into a scalar (MATCHED_DATA_REDUC). Logically we want to do a OR
4862 reduction, but because this doesn't exist, we can use a MAX reduction
4863 instead. The data value might be signed or a float so we need to cast
4865 In the case where the loop never made any matches, the data values are
4866 all identical, and so will reduce down correctly. */
4868 /* Make the matched data values unsigned. */
4869 tree vec_cond_cast
= make_ssa_name (vectype_unsigned
);
4870 tree vec_cond_cast_rhs
= build1 (VIEW_CONVERT_EXPR
, vectype_unsigned
,
4872 gimple
*vec_cond_cast_stmt
= gimple_build_assign (vec_cond_cast
,
4875 gsi_insert_before (&exit_gsi
, vec_cond_cast_stmt
, GSI_SAME_STMT
);
4877 /* Reduce down to a scalar value. */
4878 tree data_reduc
= make_ssa_name (scalar_type_unsigned
);
4879 gcall
*data_reduc_stmt
= gimple_build_call_internal (IFN_REDUC_MAX
,
4881 gimple_call_set_lhs (data_reduc_stmt
, data_reduc
);
4882 gsi_insert_before (&exit_gsi
, data_reduc_stmt
, GSI_SAME_STMT
);
4884 /* Convert the reduced value back to the result type and set as the
4886 gimple_seq stmts
= NULL
;
4887 new_temp
= gimple_build (&stmts
, VIEW_CONVERT_EXPR
, scalar_type
,
4889 gsi_insert_seq_before (&exit_gsi
, stmts
, GSI_SAME_STMT
);
4890 scalar_results
.safe_push (new_temp
);
4892 else if (STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
) == COND_REDUCTION
4893 && reduc_fn
== IFN_LAST
)
4895 /* Condition reduction without supported IFN_REDUC_MAX. Generate
4897 idx_val = induction_index[0];
4898 val = data_reduc[0];
4899 for (idx = 0, val = init, i = 0; i < nelts; ++i)
4900 if (induction_index[i] > idx_val)
4901 val = data_reduc[i], idx_val = induction_index[i];
4904 tree data_eltype
= TREE_TYPE (TREE_TYPE (new_phi_result
));
4905 tree idx_eltype
= TREE_TYPE (TREE_TYPE (induction_index
));
4906 unsigned HOST_WIDE_INT el_size
= tree_to_uhwi (TYPE_SIZE (idx_eltype
));
4907 unsigned HOST_WIDE_INT v_size
4908 = el_size
* TYPE_VECTOR_SUBPARTS (TREE_TYPE (induction_index
));
4909 tree idx_val
= NULL_TREE
, val
= NULL_TREE
;
4910 for (unsigned HOST_WIDE_INT off
= 0; off
< v_size
; off
+= el_size
)
4912 tree old_idx_val
= idx_val
;
4914 idx_val
= make_ssa_name (idx_eltype
);
4915 epilog_stmt
= gimple_build_assign (idx_val
, BIT_FIELD_REF
,
4916 build3 (BIT_FIELD_REF
, idx_eltype
,
4918 bitsize_int (el_size
),
4919 bitsize_int (off
)));
4920 gsi_insert_before (&exit_gsi
, epilog_stmt
, GSI_SAME_STMT
);
4921 val
= make_ssa_name (data_eltype
);
4922 epilog_stmt
= gimple_build_assign (val
, BIT_FIELD_REF
,
4923 build3 (BIT_FIELD_REF
,
4926 bitsize_int (el_size
),
4927 bitsize_int (off
)));
4928 gsi_insert_before (&exit_gsi
, epilog_stmt
, GSI_SAME_STMT
);
4931 tree new_idx_val
= idx_val
;
4933 if (off
!= v_size
- el_size
)
4935 new_idx_val
= make_ssa_name (idx_eltype
);
4936 epilog_stmt
= gimple_build_assign (new_idx_val
,
4939 gsi_insert_before (&exit_gsi
, epilog_stmt
, GSI_SAME_STMT
);
4941 new_val
= make_ssa_name (data_eltype
);
4942 epilog_stmt
= gimple_build_assign (new_val
,
4949 gsi_insert_before (&exit_gsi
, epilog_stmt
, GSI_SAME_STMT
);
4950 idx_val
= new_idx_val
;
4954 /* Convert the reduced value back to the result type and set as the
4956 gimple_seq stmts
= NULL
;
4957 val
= gimple_convert (&stmts
, scalar_type
, val
);
4958 gsi_insert_seq_before (&exit_gsi
, stmts
, GSI_SAME_STMT
);
4959 scalar_results
.safe_push (val
);
4962 /* 2.3 Create the reduction code, using one of the three schemes described
4963 above. In SLP we simply need to extract all the elements from the
4964 vector (without reducing them), so we use scalar shifts. */
4965 else if (reduc_fn
!= IFN_LAST
&& !slp_reduc
)
4971 v_out2 = reduc_expr <v_out1> */
4973 if (dump_enabled_p ())
4974 dump_printf_loc (MSG_NOTE
, vect_location
,
4975 "Reduce using direct vector reduction.\n");
4977 vec_elem_type
= TREE_TYPE (TREE_TYPE (new_phi_result
));
4978 if (!useless_type_conversion_p (scalar_type
, vec_elem_type
))
4981 = vect_create_destination_var (scalar_dest
, vec_elem_type
);
4982 epilog_stmt
= gimple_build_call_internal (reduc_fn
, 1,
4984 gimple_set_lhs (epilog_stmt
, tmp_dest
);
4985 new_temp
= make_ssa_name (tmp_dest
, epilog_stmt
);
4986 gimple_set_lhs (epilog_stmt
, new_temp
);
4987 gsi_insert_before (&exit_gsi
, epilog_stmt
, GSI_SAME_STMT
);
4989 epilog_stmt
= gimple_build_assign (new_scalar_dest
, NOP_EXPR
,
4994 epilog_stmt
= gimple_build_call_internal (reduc_fn
, 1,
4996 gimple_set_lhs (epilog_stmt
, new_scalar_dest
);
4999 new_temp
= make_ssa_name (new_scalar_dest
, epilog_stmt
);
5000 gimple_set_lhs (epilog_stmt
, new_temp
);
5001 gsi_insert_before (&exit_gsi
, epilog_stmt
, GSI_SAME_STMT
);
5003 if ((STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
)
5004 == INTEGER_INDUC_COND_REDUCTION
)
5005 && !operand_equal_p (initial_def
, induc_val
, 0))
5007 /* Earlier we set the initial value to be a vector if induc_val
5008 values. Check the result and if it is induc_val then replace
5009 with the original initial value, unless induc_val is
5010 the same as initial_def already. */
5011 tree zcompare
= build2 (EQ_EXPR
, boolean_type_node
, new_temp
,
5014 tmp
= make_ssa_name (new_scalar_dest
);
5015 epilog_stmt
= gimple_build_assign (tmp
, COND_EXPR
, zcompare
,
5016 initial_def
, new_temp
);
5017 gsi_insert_before (&exit_gsi
, epilog_stmt
, GSI_SAME_STMT
);
5021 scalar_results
.safe_push (new_temp
);
5025 bool reduce_with_shift
= have_whole_vector_shift (mode
);
5026 int element_bitsize
= tree_to_uhwi (bitsize
);
5027 int vec_size_in_bits
= tree_to_uhwi (TYPE_SIZE (vectype
));
5030 /* COND reductions all do the final reduction with MAX_EXPR
5032 if (code
== COND_EXPR
)
5034 if (STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
)
5035 == INTEGER_INDUC_COND_REDUCTION
)
5041 /* Regardless of whether we have a whole vector shift, if we're
5042 emulating the operation via tree-vect-generic, we don't want
5043 to use it. Only the first round of the reduction is likely
5044 to still be profitable via emulation. */
5045 /* ??? It might be better to emit a reduction tree code here, so that
5046 tree-vect-generic can expand the first round via bit tricks. */
5047 if (!VECTOR_MODE_P (mode
))
5048 reduce_with_shift
= false;
5051 optab optab
= optab_for_tree_code (code
, vectype
, optab_default
);
5052 if (optab_handler (optab
, mode
) == CODE_FOR_nothing
)
5053 reduce_with_shift
= false;
5056 if (reduce_with_shift
&& !slp_reduc
)
5058 int nelements
= vec_size_in_bits
/ element_bitsize
;
5059 vec_perm_builder sel
;
5060 vec_perm_indices indices
;
5064 tree zero_vec
= build_zero_cst (vectype
);
5066 for (offset = nelements/2; offset >= 1; offset/=2)
5068 Create: va' = vec_shift <va, offset>
5069 Create: va = vop <va, va'>
5074 if (dump_enabled_p ())
5075 dump_printf_loc (MSG_NOTE
, vect_location
,
5076 "Reduce using vector shifts\n");
5078 vec_dest
= vect_create_destination_var (scalar_dest
, vectype
);
5079 new_temp
= new_phi_result
;
5080 for (elt_offset
= nelements
/ 2;
5084 calc_vec_perm_mask_for_shift (elt_offset
, nelements
, &sel
);
5085 indices
.new_vector (sel
, 2, nelements
);
5086 tree mask
= vect_gen_perm_mask_any (vectype
, indices
);
5087 epilog_stmt
= gimple_build_assign (vec_dest
, VEC_PERM_EXPR
,
5088 new_temp
, zero_vec
, mask
);
5089 new_name
= make_ssa_name (vec_dest
, epilog_stmt
);
5090 gimple_assign_set_lhs (epilog_stmt
, new_name
);
5091 gsi_insert_before (&exit_gsi
, epilog_stmt
, GSI_SAME_STMT
);
5093 epilog_stmt
= gimple_build_assign (vec_dest
, code
, new_name
,
5095 new_temp
= make_ssa_name (vec_dest
, epilog_stmt
);
5096 gimple_assign_set_lhs (epilog_stmt
, new_temp
);
5097 gsi_insert_before (&exit_gsi
, epilog_stmt
, GSI_SAME_STMT
);
5100 /* 2.4 Extract the final scalar result. Create:
5101 s_out3 = extract_field <v_out2, bitpos> */
5103 if (dump_enabled_p ())
5104 dump_printf_loc (MSG_NOTE
, vect_location
,
5105 "extract scalar result\n");
5107 rhs
= build3 (BIT_FIELD_REF
, scalar_type
, new_temp
,
5108 bitsize
, bitsize_zero_node
);
5109 epilog_stmt
= gimple_build_assign (new_scalar_dest
, rhs
);
5110 new_temp
= make_ssa_name (new_scalar_dest
, epilog_stmt
);
5111 gimple_assign_set_lhs (epilog_stmt
, new_temp
);
5112 gsi_insert_before (&exit_gsi
, epilog_stmt
, GSI_SAME_STMT
);
5113 scalar_results
.safe_push (new_temp
);
5118 s = extract_field <v_out2, 0>
5119 for (offset = element_size;
5120 offset < vector_size;
5121 offset += element_size;)
5123 Create: s' = extract_field <v_out2, offset>
5124 Create: s = op <s, s'> // For non SLP cases
5127 if (dump_enabled_p ())
5128 dump_printf_loc (MSG_NOTE
, vect_location
,
5129 "Reduce using scalar code.\n");
5131 vec_size_in_bits
= tree_to_uhwi (TYPE_SIZE (vectype
));
5132 FOR_EACH_VEC_ELT (new_phis
, i
, new_phi
)
5135 if (gimple_code (new_phi
) == GIMPLE_PHI
)
5136 vec_temp
= PHI_RESULT (new_phi
);
5138 vec_temp
= gimple_assign_lhs (new_phi
);
5139 tree rhs
= build3 (BIT_FIELD_REF
, scalar_type
, vec_temp
, bitsize
,
5141 epilog_stmt
= gimple_build_assign (new_scalar_dest
, rhs
);
5142 new_temp
= make_ssa_name (new_scalar_dest
, epilog_stmt
);
5143 gimple_assign_set_lhs (epilog_stmt
, new_temp
);
5144 gsi_insert_before (&exit_gsi
, epilog_stmt
, GSI_SAME_STMT
);
5146 /* In SLP we don't need to apply reduction operation, so we just
5147 collect s' values in SCALAR_RESULTS. */
5149 scalar_results
.safe_push (new_temp
);
5151 for (bit_offset
= element_bitsize
;
5152 bit_offset
< vec_size_in_bits
;
5153 bit_offset
+= element_bitsize
)
5155 tree bitpos
= bitsize_int (bit_offset
);
5156 tree rhs
= build3 (BIT_FIELD_REF
, scalar_type
, vec_temp
,
5159 epilog_stmt
= gimple_build_assign (new_scalar_dest
, rhs
);
5160 new_name
= make_ssa_name (new_scalar_dest
, epilog_stmt
);
5161 gimple_assign_set_lhs (epilog_stmt
, new_name
);
5162 gsi_insert_before (&exit_gsi
, epilog_stmt
, GSI_SAME_STMT
);
5166 /* In SLP we don't need to apply reduction operation, so
5167 we just collect s' values in SCALAR_RESULTS. */
5168 new_temp
= new_name
;
5169 scalar_results
.safe_push (new_name
);
5173 epilog_stmt
= gimple_build_assign (new_scalar_dest
, code
,
5174 new_name
, new_temp
);
5175 new_temp
= make_ssa_name (new_scalar_dest
, epilog_stmt
);
5176 gimple_assign_set_lhs (epilog_stmt
, new_temp
);
5177 gsi_insert_before (&exit_gsi
, epilog_stmt
, GSI_SAME_STMT
);
5182 /* The only case where we need to reduce scalar results in SLP, is
5183 unrolling. If the size of SCALAR_RESULTS is greater than
5184 GROUP_SIZE, we reduce them combining elements modulo
5188 tree res
, first_res
, new_res
;
5191 /* Reduce multiple scalar results in case of SLP unrolling. */
5192 for (j
= group_size
; scalar_results
.iterate (j
, &res
);
5195 first_res
= scalar_results
[j
% group_size
];
5196 new_stmt
= gimple_build_assign (new_scalar_dest
, code
,
5198 new_res
= make_ssa_name (new_scalar_dest
, new_stmt
);
5199 gimple_assign_set_lhs (new_stmt
, new_res
);
5200 gsi_insert_before (&exit_gsi
, new_stmt
, GSI_SAME_STMT
);
5201 scalar_results
[j
% group_size
] = new_res
;
5205 /* Not SLP - we have one scalar to keep in SCALAR_RESULTS. */
5206 scalar_results
.safe_push (new_temp
);
5209 if ((STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
)
5210 == INTEGER_INDUC_COND_REDUCTION
)
5211 && !operand_equal_p (initial_def
, induc_val
, 0))
5213 /* Earlier we set the initial value to be a vector if induc_val
5214 values. Check the result and if it is induc_val then replace
5215 with the original initial value, unless induc_val is
5216 the same as initial_def already. */
5217 tree zcompare
= build2 (EQ_EXPR
, boolean_type_node
, new_temp
,
5220 tree tmp
= make_ssa_name (new_scalar_dest
);
5221 epilog_stmt
= gimple_build_assign (tmp
, COND_EXPR
, zcompare
,
5222 initial_def
, new_temp
);
5223 gsi_insert_before (&exit_gsi
, epilog_stmt
, GSI_SAME_STMT
);
5224 scalar_results
[0] = tmp
;
5228 vect_finalize_reduction
:
5233 /* 2.5 Adjust the final result by the initial value of the reduction
5234 variable. (When such adjustment is not needed, then
5235 'adjustment_def' is zero). For example, if code is PLUS we create:
5236 new_temp = loop_exit_def + adjustment_def */
5240 gcc_assert (!slp_reduc
);
5241 if (nested_in_vect_loop
)
5243 new_phi
= new_phis
[0];
5244 gcc_assert (TREE_CODE (TREE_TYPE (adjustment_def
)) == VECTOR_TYPE
);
5245 expr
= build2 (code
, vectype
, PHI_RESULT (new_phi
), adjustment_def
);
5246 new_dest
= vect_create_destination_var (scalar_dest
, vectype
);
5250 new_temp
= scalar_results
[0];
5251 gcc_assert (TREE_CODE (TREE_TYPE (adjustment_def
)) != VECTOR_TYPE
);
5252 expr
= build2 (code
, scalar_type
, new_temp
, adjustment_def
);
5253 new_dest
= vect_create_destination_var (scalar_dest
, scalar_type
);
5256 epilog_stmt
= gimple_build_assign (new_dest
, expr
);
5257 new_temp
= make_ssa_name (new_dest
, epilog_stmt
);
5258 gimple_assign_set_lhs (epilog_stmt
, new_temp
);
5259 gsi_insert_before (&exit_gsi
, epilog_stmt
, GSI_SAME_STMT
);
5260 if (nested_in_vect_loop
)
5262 set_vinfo_for_stmt (epilog_stmt
,
5263 new_stmt_vec_info (epilog_stmt
, loop_vinfo
));
5264 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (epilog_stmt
)) =
5265 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (new_phi
));
5268 scalar_results
.quick_push (new_temp
);
5270 scalar_results
[0] = new_temp
;
5273 scalar_results
[0] = new_temp
;
5275 new_phis
[0] = epilog_stmt
;
5278 /* 2.6 Handle the loop-exit phis. Replace the uses of scalar loop-exit
5279 phis with new adjusted scalar results, i.e., replace use <s_out0>
5284 s_out0 = phi <s_loop> # (scalar) EXIT_PHI
5285 v_out1 = phi <VECT_DEF> # NEW_EXIT_PHI
5286 v_out2 = reduce <v_out1>
5287 s_out3 = extract_field <v_out2, 0>
5288 s_out4 = adjust_result <s_out3>
5295 s_out0 = phi <s_loop> # (scalar) EXIT_PHI
5296 v_out1 = phi <VECT_DEF> # NEW_EXIT_PHI
5297 v_out2 = reduce <v_out1>
5298 s_out3 = extract_field <v_out2, 0>
5299 s_out4 = adjust_result <s_out3>
5304 /* In SLP reduction chain we reduce vector results into one vector if
5305 necessary, hence we set here GROUP_SIZE to 1. SCALAR_DEST is the LHS of
5306 the last stmt in the reduction chain, since we are looking for the loop
5308 if (GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt
)))
5310 gimple
*dest_stmt
= SLP_TREE_SCALAR_STMTS (slp_node
)[group_size
- 1];
5311 /* Handle reduction patterns. */
5312 if (STMT_VINFO_RELATED_STMT (vinfo_for_stmt (dest_stmt
)))
5313 dest_stmt
= STMT_VINFO_RELATED_STMT (vinfo_for_stmt (dest_stmt
));
5315 scalar_dest
= gimple_assign_lhs (dest_stmt
);
5319 /* In SLP we may have several statements in NEW_PHIS and REDUCTION_PHIS (in
5320 case that GROUP_SIZE is greater than vectorization factor). Therefore, we
5321 need to match SCALAR_RESULTS with corresponding statements. The first
5322 (GROUP_SIZE / number of new vector stmts) scalar results correspond to
5323 the first vector stmt, etc.
5324 (RATIO is equal to (GROUP_SIZE / number of new vector stmts)). */
5325 if (group_size
> new_phis
.length ())
5327 ratio
= group_size
/ new_phis
.length ();
5328 gcc_assert (!(group_size
% new_phis
.length ()));
5333 for (k
= 0; k
< group_size
; k
++)
5337 epilog_stmt
= new_phis
[k
/ ratio
];
5338 reduction_phi
= reduction_phis
[k
/ ratio
];
5340 inner_phi
= inner_phis
[k
/ ratio
];
5345 gimple
*current_stmt
= SLP_TREE_SCALAR_STMTS (slp_node
)[k
];
5347 orig_stmt
= STMT_VINFO_RELATED_STMT (vinfo_for_stmt (current_stmt
));
5348 /* SLP statements can't participate in patterns. */
5349 gcc_assert (!orig_stmt
);
5350 scalar_dest
= gimple_assign_lhs (current_stmt
);
5354 /* Find the loop-closed-use at the loop exit of the original scalar
5355 result. (The reduction result is expected to have two immediate uses -
5356 one at the latch block, and one at the loop exit). */
5357 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, scalar_dest
)
5358 if (!flow_bb_inside_loop_p (loop
, gimple_bb (USE_STMT (use_p
)))
5359 && !is_gimple_debug (USE_STMT (use_p
)))
5360 phis
.safe_push (USE_STMT (use_p
));
5362 /* While we expect to have found an exit_phi because of loop-closed-ssa
5363 form we can end up without one if the scalar cycle is dead. */
5365 FOR_EACH_VEC_ELT (phis
, i
, exit_phi
)
5369 stmt_vec_info exit_phi_vinfo
= vinfo_for_stmt (exit_phi
);
5372 /* FORNOW. Currently not supporting the case that an inner-loop
5373 reduction is not used in the outer-loop (but only outside the
5374 outer-loop), unless it is double reduction. */
5375 gcc_assert ((STMT_VINFO_RELEVANT_P (exit_phi_vinfo
)
5376 && !STMT_VINFO_LIVE_P (exit_phi_vinfo
))
5380 STMT_VINFO_VEC_STMT (exit_phi_vinfo
) = inner_phi
;
5382 STMT_VINFO_VEC_STMT (exit_phi_vinfo
) = epilog_stmt
;
5384 || STMT_VINFO_DEF_TYPE (exit_phi_vinfo
)
5385 != vect_double_reduction_def
)
5388 /* Handle double reduction:
5390 stmt1: s1 = phi <s0, s2> - double reduction phi (outer loop)
5391 stmt2: s3 = phi <s1, s4> - (regular) reduc phi (inner loop)
5392 stmt3: s4 = use (s3) - (regular) reduc stmt (inner loop)
5393 stmt4: s2 = phi <s4> - double reduction stmt (outer loop)
5395 At that point the regular reduction (stmt2 and stmt3) is
5396 already vectorized, as well as the exit phi node, stmt4.
5397 Here we vectorize the phi node of double reduction, stmt1, and
5398 update all relevant statements. */
5400 /* Go through all the uses of s2 to find double reduction phi
5401 node, i.e., stmt1 above. */
5402 orig_name
= PHI_RESULT (exit_phi
);
5403 FOR_EACH_IMM_USE_STMT (use_stmt
, imm_iter
, orig_name
)
5405 stmt_vec_info use_stmt_vinfo
;
5406 stmt_vec_info new_phi_vinfo
;
5407 tree vect_phi_init
, preheader_arg
, vect_phi_res
;
5408 basic_block bb
= gimple_bb (use_stmt
);
5411 /* Check that USE_STMT is really double reduction phi
5413 if (gimple_code (use_stmt
) != GIMPLE_PHI
5414 || gimple_phi_num_args (use_stmt
) != 2
5415 || bb
->loop_father
!= outer_loop
)
5417 use_stmt_vinfo
= vinfo_for_stmt (use_stmt
);
5419 || STMT_VINFO_DEF_TYPE (use_stmt_vinfo
)
5420 != vect_double_reduction_def
)
5423 /* Create vector phi node for double reduction:
5424 vs1 = phi <vs0, vs2>
5425 vs1 was created previously in this function by a call to
5426 vect_get_vec_def_for_operand and is stored in
5428 vs2 is defined by INNER_PHI, the vectorized EXIT_PHI;
5429 vs0 is created here. */
5431 /* Create vector phi node. */
5432 vect_phi
= create_phi_node (vec_initial_def
, bb
);
5433 new_phi_vinfo
= new_stmt_vec_info (vect_phi
,
5434 loop_vec_info_for_loop (outer_loop
));
5435 set_vinfo_for_stmt (vect_phi
, new_phi_vinfo
);
5437 /* Create vs0 - initial def of the double reduction phi. */
5438 preheader_arg
= PHI_ARG_DEF_FROM_EDGE (use_stmt
,
5439 loop_preheader_edge (outer_loop
));
5440 vect_phi_init
= get_initial_def_for_reduction
5441 (stmt
, preheader_arg
, NULL
);
5443 /* Update phi node arguments with vs0 and vs2. */
5444 add_phi_arg (vect_phi
, vect_phi_init
,
5445 loop_preheader_edge (outer_loop
),
5447 add_phi_arg (vect_phi
, PHI_RESULT (inner_phi
),
5448 loop_latch_edge (outer_loop
), UNKNOWN_LOCATION
);
5449 if (dump_enabled_p ())
5451 dump_printf_loc (MSG_NOTE
, vect_location
,
5452 "created double reduction phi node: ");
5453 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
, vect_phi
, 0);
5456 vect_phi_res
= PHI_RESULT (vect_phi
);
5458 /* Replace the use, i.e., set the correct vs1 in the regular
5459 reduction phi node. FORNOW, NCOPIES is always 1, so the
5460 loop is redundant. */
5461 use
= reduction_phi
;
5462 for (j
= 0; j
< ncopies
; j
++)
5464 edge pr_edge
= loop_preheader_edge (loop
);
5465 SET_PHI_ARG_DEF (use
, pr_edge
->dest_idx
, vect_phi_res
);
5466 use
= STMT_VINFO_RELATED_STMT (vinfo_for_stmt (use
));
5473 if (nested_in_vect_loop
)
5482 /* Find the loop-closed-use at the loop exit of the original scalar
5483 result. (The reduction result is expected to have two immediate uses,
5484 one at the latch block, and one at the loop exit). For double
5485 reductions we are looking for exit phis of the outer loop. */
5486 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, scalar_dest
)
5488 if (!flow_bb_inside_loop_p (loop
, gimple_bb (USE_STMT (use_p
))))
5490 if (!is_gimple_debug (USE_STMT (use_p
)))
5491 phis
.safe_push (USE_STMT (use_p
));
5495 if (double_reduc
&& gimple_code (USE_STMT (use_p
)) == GIMPLE_PHI
)
5497 tree phi_res
= PHI_RESULT (USE_STMT (use_p
));
5499 FOR_EACH_IMM_USE_FAST (phi_use_p
, phi_imm_iter
, phi_res
)
5501 if (!flow_bb_inside_loop_p (loop
,
5502 gimple_bb (USE_STMT (phi_use_p
)))
5503 && !is_gimple_debug (USE_STMT (phi_use_p
)))
5504 phis
.safe_push (USE_STMT (phi_use_p
));
5510 FOR_EACH_VEC_ELT (phis
, i
, exit_phi
)
5512 /* Replace the uses: */
5513 orig_name
= PHI_RESULT (exit_phi
);
5514 scalar_result
= scalar_results
[k
];
5515 FOR_EACH_IMM_USE_STMT (use_stmt
, imm_iter
, orig_name
)
5516 FOR_EACH_IMM_USE_ON_STMT (use_p
, imm_iter
)
5517 SET_USE (use_p
, scalar_result
);
5525 /* Function is_nonwrapping_integer_induction.
5527 Check if STMT (which is part of loop LOOP) both increments and
5528 does not cause overflow. */
5531 is_nonwrapping_integer_induction (gimple
*stmt
, struct loop
*loop
)
5533 stmt_vec_info stmt_vinfo
= vinfo_for_stmt (stmt
);
5534 tree base
= STMT_VINFO_LOOP_PHI_EVOLUTION_BASE_UNCHANGED (stmt_vinfo
);
5535 tree step
= STMT_VINFO_LOOP_PHI_EVOLUTION_PART (stmt_vinfo
);
5536 tree lhs_type
= TREE_TYPE (gimple_phi_result (stmt
));
5537 widest_int ni
, max_loop_value
, lhs_max
;
5538 bool overflow
= false;
5540 /* Make sure the loop is integer based. */
5541 if (TREE_CODE (base
) != INTEGER_CST
5542 || TREE_CODE (step
) != INTEGER_CST
)
5545 /* Check that the max size of the loop will not wrap. */
5547 if (TYPE_OVERFLOW_UNDEFINED (lhs_type
))
5550 if (! max_stmt_executions (loop
, &ni
))
5553 max_loop_value
= wi::mul (wi::to_widest (step
), ni
, TYPE_SIGN (lhs_type
),
5558 max_loop_value
= wi::add (wi::to_widest (base
), max_loop_value
,
5559 TYPE_SIGN (lhs_type
), &overflow
);
5563 return (wi::min_precision (max_loop_value
, TYPE_SIGN (lhs_type
))
5564 <= TYPE_PRECISION (lhs_type
));
5567 /* Function vectorizable_reduction.
5569 Check if STMT performs a reduction operation that can be vectorized.
5570 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
5571 stmt to replace it, put it in VEC_STMT, and insert it at GSI.
5572 Return FALSE if not a vectorizable STMT, TRUE otherwise.
5574 This function also handles reduction idioms (patterns) that have been
5575 recognized in advance during vect_pattern_recog. In this case, STMT may be
5577 X = pattern_expr (arg0, arg1, ..., X)
5578 and it's STMT_VINFO_RELATED_STMT points to the last stmt in the original
5579 sequence that had been detected and replaced by the pattern-stmt (STMT).
5581 This function also handles reduction of condition expressions, for example:
5582 for (int i = 0; i < N; i++)
5585 This is handled by vectorising the loop and creating an additional vector
5586 containing the loop indexes for which "a[i] < value" was true. In the
5587 function epilogue this is reduced to a single max value and then used to
5588 index into the vector of results.
5590 In some cases of reduction patterns, the type of the reduction variable X is
5591 different than the type of the other arguments of STMT.
5592 In such cases, the vectype that is used when transforming STMT into a vector
5593 stmt is different than the vectype that is used to determine the
5594 vectorization factor, because it consists of a different number of elements
5595 than the actual number of elements that are being operated upon in parallel.
5597 For example, consider an accumulation of shorts into an int accumulator.
5598 On some targets it's possible to vectorize this pattern operating on 8
5599 shorts at a time (hence, the vectype for purposes of determining the
5600 vectorization factor should be V8HI); on the other hand, the vectype that
5601 is used to create the vector form is actually V4SI (the type of the result).
5603 Upon entry to this function, STMT_VINFO_VECTYPE records the vectype that
5604 indicates what is the actual level of parallelism (V8HI in the example), so
5605 that the right vectorization factor would be derived. This vectype
5606 corresponds to the type of arguments to the reduction stmt, and should *NOT*
5607 be used to create the vectorized stmt. The right vectype for the vectorized
5608 stmt is obtained from the type of the result X:
5609 get_vectype_for_scalar_type (TREE_TYPE (X))
5611 This means that, contrary to "regular" reductions (or "regular" stmts in
5612 general), the following equation:
5613 STMT_VINFO_VECTYPE == get_vectype_for_scalar_type (TREE_TYPE (X))
5614 does *NOT* necessarily hold for reduction patterns. */
5617 vectorizable_reduction (gimple
*stmt
, gimple_stmt_iterator
*gsi
,
5618 gimple
**vec_stmt
, slp_tree slp_node
,
5619 slp_instance slp_node_instance
)
5623 stmt_vec_info stmt_info
= vinfo_for_stmt (stmt
);
5624 tree vectype_out
= STMT_VINFO_VECTYPE (stmt_info
);
5625 tree vectype_in
= NULL_TREE
;
5626 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_info
);
5627 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
5628 enum tree_code code
, orig_code
;
5629 internal_fn reduc_fn
;
5630 machine_mode vec_mode
;
5633 tree new_temp
= NULL_TREE
;
5635 enum vect_def_type dt
, cond_reduc_dt
= vect_unknown_def_type
;
5636 gimple
*cond_reduc_def_stmt
= NULL
;
5637 enum tree_code cond_reduc_op_code
= ERROR_MARK
;
5641 stmt_vec_info orig_stmt_info
= NULL
;
5645 stmt_vec_info prev_stmt_info
, prev_phi_info
;
5646 bool single_defuse_cycle
= false;
5647 gimple
*new_stmt
= NULL
;
5650 enum vect_def_type dts
[3];
5651 bool nested_cycle
= false, found_nested_cycle_def
= false;
5652 bool double_reduc
= false;
5654 struct loop
* def_stmt_loop
, *outer_loop
= NULL
;
5656 gimple
*def_arg_stmt
;
5657 auto_vec
<tree
> vec_oprnds0
;
5658 auto_vec
<tree
> vec_oprnds1
;
5659 auto_vec
<tree
> vec_oprnds2
;
5660 auto_vec
<tree
> vect_defs
;
5661 auto_vec
<gimple
*> phis
;
5664 bool first_p
= true;
5665 tree cr_index_scalar_type
= NULL_TREE
, cr_index_vector_type
= NULL_TREE
;
5666 tree cond_reduc_val
= NULL_TREE
;
5668 /* Make sure it was already recognized as a reduction computation. */
5669 if (STMT_VINFO_DEF_TYPE (vinfo_for_stmt (stmt
)) != vect_reduction_def
5670 && STMT_VINFO_DEF_TYPE (vinfo_for_stmt (stmt
)) != vect_nested_cycle
)
5673 if (nested_in_vect_loop_p (loop
, stmt
))
5677 nested_cycle
= true;
5680 /* In case of reduction chain we switch to the first stmt in the chain, but
5681 we don't update STMT_INFO, since only the last stmt is marked as reduction
5682 and has reduction properties. */
5683 if (GROUP_FIRST_ELEMENT (stmt_info
)
5684 && GROUP_FIRST_ELEMENT (stmt_info
) != stmt
)
5686 stmt
= GROUP_FIRST_ELEMENT (stmt_info
);
5690 if (gimple_code (stmt
) == GIMPLE_PHI
)
5692 /* Analysis is fully done on the reduction stmt invocation. */
5696 slp_node_instance
->reduc_phis
= slp_node
;
5698 STMT_VINFO_TYPE (stmt_info
) = reduc_vec_info_type
;
5702 gimple
*reduc_stmt
= STMT_VINFO_REDUC_DEF (stmt_info
);
5703 if (STMT_VINFO_IN_PATTERN_P (vinfo_for_stmt (reduc_stmt
)))
5704 reduc_stmt
= STMT_VINFO_RELATED_STMT (vinfo_for_stmt (reduc_stmt
));
5706 gcc_assert (is_gimple_assign (reduc_stmt
));
5707 for (unsigned k
= 1; k
< gimple_num_ops (reduc_stmt
); ++k
)
5709 tree op
= gimple_op (reduc_stmt
, k
);
5710 if (op
== gimple_phi_result (stmt
))
5713 && gimple_assign_rhs_code (reduc_stmt
) == COND_EXPR
)
5715 tem
= get_vectype_for_scalar_type (TREE_TYPE (op
));
5717 || TYPE_VECTOR_SUBPARTS (tem
) < TYPE_VECTOR_SUBPARTS (vectype_in
))
5721 gcc_assert (vectype_in
);
5726 ncopies
= vect_get_num_copies (loop_vinfo
, vectype_in
);
5728 use_operand_p use_p
;
5731 && (STMT_VINFO_RELEVANT (vinfo_for_stmt (reduc_stmt
))
5732 <= vect_used_only_live
)
5733 && single_imm_use (gimple_phi_result (stmt
), &use_p
, &use_stmt
)
5734 && (use_stmt
== reduc_stmt
5735 || (STMT_VINFO_RELATED_STMT (vinfo_for_stmt (use_stmt
))
5737 single_defuse_cycle
= true;
5739 /* Create the destination vector */
5740 scalar_dest
= gimple_assign_lhs (reduc_stmt
);
5741 vec_dest
= vect_create_destination_var (scalar_dest
, vectype_out
);
5744 /* The size vect_schedule_slp_instance computes is off for us. */
5745 vec_num
= ((LOOP_VINFO_VECT_FACTOR (loop_vinfo
)
5746 * SLP_TREE_SCALAR_STMTS (slp_node
).length ())
5747 / TYPE_VECTOR_SUBPARTS (vectype_in
));
5751 /* Generate the reduction PHIs upfront. */
5752 prev_phi_info
= NULL
;
5753 for (j
= 0; j
< ncopies
; j
++)
5755 if (j
== 0 || !single_defuse_cycle
)
5757 for (i
= 0; i
< vec_num
; i
++)
5759 /* Create the reduction-phi that defines the reduction
5761 gimple
*new_phi
= create_phi_node (vec_dest
, loop
->header
);
5762 set_vinfo_for_stmt (new_phi
,
5763 new_stmt_vec_info (new_phi
, loop_vinfo
));
5766 SLP_TREE_VEC_STMTS (slp_node
).quick_push (new_phi
);
5770 STMT_VINFO_VEC_STMT (stmt_info
) = *vec_stmt
= new_phi
;
5772 STMT_VINFO_RELATED_STMT (prev_phi_info
) = new_phi
;
5773 prev_phi_info
= vinfo_for_stmt (new_phi
);
5782 /* 1. Is vectorizable reduction? */
5783 /* Not supportable if the reduction variable is used in the loop, unless
5784 it's a reduction chain. */
5785 if (STMT_VINFO_RELEVANT (stmt_info
) > vect_used_in_outer
5786 && !GROUP_FIRST_ELEMENT (stmt_info
))
5789 /* Reductions that are not used even in an enclosing outer-loop,
5790 are expected to be "live" (used out of the loop). */
5791 if (STMT_VINFO_RELEVANT (stmt_info
) == vect_unused_in_scope
5792 && !STMT_VINFO_LIVE_P (stmt_info
))
5795 /* 2. Has this been recognized as a reduction pattern?
5797 Check if STMT represents a pattern that has been recognized
5798 in earlier analysis stages. For stmts that represent a pattern,
5799 the STMT_VINFO_RELATED_STMT field records the last stmt in
5800 the original sequence that constitutes the pattern. */
5802 orig_stmt
= STMT_VINFO_RELATED_STMT (vinfo_for_stmt (stmt
));
5805 orig_stmt_info
= vinfo_for_stmt (orig_stmt
);
5806 gcc_assert (STMT_VINFO_IN_PATTERN_P (orig_stmt_info
));
5807 gcc_assert (!STMT_VINFO_IN_PATTERN_P (stmt_info
));
5810 /* 3. Check the operands of the operation. The first operands are defined
5811 inside the loop body. The last operand is the reduction variable,
5812 which is defined by the loop-header-phi. */
5814 gcc_assert (is_gimple_assign (stmt
));
5817 switch (get_gimple_rhs_class (gimple_assign_rhs_code (stmt
)))
5819 case GIMPLE_BINARY_RHS
:
5820 code
= gimple_assign_rhs_code (stmt
);
5821 op_type
= TREE_CODE_LENGTH (code
);
5822 gcc_assert (op_type
== binary_op
);
5823 ops
[0] = gimple_assign_rhs1 (stmt
);
5824 ops
[1] = gimple_assign_rhs2 (stmt
);
5827 case GIMPLE_TERNARY_RHS
:
5828 code
= gimple_assign_rhs_code (stmt
);
5829 op_type
= TREE_CODE_LENGTH (code
);
5830 gcc_assert (op_type
== ternary_op
);
5831 ops
[0] = gimple_assign_rhs1 (stmt
);
5832 ops
[1] = gimple_assign_rhs2 (stmt
);
5833 ops
[2] = gimple_assign_rhs3 (stmt
);
5836 case GIMPLE_UNARY_RHS
:
5843 if (code
== COND_EXPR
&& slp_node
)
5846 scalar_dest
= gimple_assign_lhs (stmt
);
5847 scalar_type
= TREE_TYPE (scalar_dest
);
5848 if (!POINTER_TYPE_P (scalar_type
) && !INTEGRAL_TYPE_P (scalar_type
)
5849 && !SCALAR_FLOAT_TYPE_P (scalar_type
))
5852 /* Do not try to vectorize bit-precision reductions. */
5853 if (!type_has_mode_precision_p (scalar_type
))
5856 /* All uses but the last are expected to be defined in the loop.
5857 The last use is the reduction variable. In case of nested cycle this
5858 assumption is not true: we use reduc_index to record the index of the
5859 reduction variable. */
5860 gimple
*reduc_def_stmt
= NULL
;
5861 int reduc_index
= -1;
5862 for (i
= 0; i
< op_type
; i
++)
5864 /* The condition of COND_EXPR is checked in vectorizable_condition(). */
5865 if (i
== 0 && code
== COND_EXPR
)
5868 is_simple_use
= vect_is_simple_use (ops
[i
], loop_vinfo
,
5869 &def_stmt
, &dts
[i
], &tem
);
5871 gcc_assert (is_simple_use
);
5872 if (dt
== vect_reduction_def
)
5874 reduc_def_stmt
= def_stmt
;
5880 /* To properly compute ncopies we are interested in the widest
5881 input type in case we're looking at a widening accumulation. */
5883 || TYPE_VECTOR_SUBPARTS (vectype_in
) > TYPE_VECTOR_SUBPARTS (tem
))
5887 if (dt
!= vect_internal_def
5888 && dt
!= vect_external_def
5889 && dt
!= vect_constant_def
5890 && dt
!= vect_induction_def
5891 && !(dt
== vect_nested_cycle
&& nested_cycle
))
5894 if (dt
== vect_nested_cycle
)
5896 found_nested_cycle_def
= true;
5897 reduc_def_stmt
= def_stmt
;
5901 if (i
== 1 && code
== COND_EXPR
)
5903 /* Record how value of COND_EXPR is defined. */
5904 if (dt
== vect_constant_def
)
5907 cond_reduc_val
= ops
[i
];
5909 if (dt
== vect_induction_def
5911 && is_nonwrapping_integer_induction (def_stmt
, loop
))
5914 cond_reduc_def_stmt
= def_stmt
;
5920 vectype_in
= vectype_out
;
5922 /* When vectorizing a reduction chain w/o SLP the reduction PHI is not
5923 directy used in stmt. */
5924 if (reduc_index
== -1)
5927 reduc_def_stmt
= STMT_VINFO_REDUC_DEF (orig_stmt_info
);
5929 reduc_def_stmt
= STMT_VINFO_REDUC_DEF (stmt_info
);
5932 if (! reduc_def_stmt
|| gimple_code (reduc_def_stmt
) != GIMPLE_PHI
)
5935 if (!(reduc_index
== -1
5936 || dts
[reduc_index
] == vect_reduction_def
5937 || dts
[reduc_index
] == vect_nested_cycle
5938 || ((dts
[reduc_index
] == vect_internal_def
5939 || dts
[reduc_index
] == vect_external_def
5940 || dts
[reduc_index
] == vect_constant_def
5941 || dts
[reduc_index
] == vect_induction_def
)
5942 && nested_cycle
&& found_nested_cycle_def
)))
5944 /* For pattern recognized stmts, orig_stmt might be a reduction,
5945 but some helper statements for the pattern might not, or
5946 might be COND_EXPRs with reduction uses in the condition. */
5947 gcc_assert (orig_stmt
);
5951 stmt_vec_info reduc_def_info
= vinfo_for_stmt (reduc_def_stmt
);
5952 enum vect_reduction_type v_reduc_type
5953 = STMT_VINFO_REDUC_TYPE (reduc_def_info
);
5954 gimple
*tmp
= STMT_VINFO_REDUC_DEF (reduc_def_info
);
5956 STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
) = v_reduc_type
;
5957 /* If we have a condition reduction, see if we can simplify it further. */
5958 if (v_reduc_type
== COND_REDUCTION
)
5960 if (cond_reduc_dt
== vect_induction_def
)
5962 stmt_vec_info cond_stmt_vinfo
= vinfo_for_stmt (cond_reduc_def_stmt
);
5964 = STMT_VINFO_LOOP_PHI_EVOLUTION_BASE_UNCHANGED (cond_stmt_vinfo
);
5965 tree step
= STMT_VINFO_LOOP_PHI_EVOLUTION_PART (cond_stmt_vinfo
);
5967 gcc_assert (TREE_CODE (base
) == INTEGER_CST
5968 && TREE_CODE (step
) == INTEGER_CST
);
5969 cond_reduc_val
= NULL_TREE
;
5970 /* Find a suitable value, for MAX_EXPR below base, for MIN_EXPR
5971 above base; punt if base is the minimum value of the type for
5972 MAX_EXPR or maximum value of the type for MIN_EXPR for now. */
5973 if (tree_int_cst_sgn (step
) == -1)
5975 cond_reduc_op_code
= MIN_EXPR
;
5976 if (tree_int_cst_sgn (base
) == -1)
5977 cond_reduc_val
= build_int_cst (TREE_TYPE (base
), 0);
5978 else if (tree_int_cst_lt (base
,
5979 TYPE_MAX_VALUE (TREE_TYPE (base
))))
5981 = int_const_binop (PLUS_EXPR
, base
, integer_one_node
);
5985 cond_reduc_op_code
= MAX_EXPR
;
5986 if (tree_int_cst_sgn (base
) == 1)
5987 cond_reduc_val
= build_int_cst (TREE_TYPE (base
), 0);
5988 else if (tree_int_cst_lt (TYPE_MIN_VALUE (TREE_TYPE (base
)),
5991 = int_const_binop (MINUS_EXPR
, base
, integer_one_node
);
5995 if (dump_enabled_p ())
5996 dump_printf_loc (MSG_NOTE
, vect_location
,
5997 "condition expression based on "
5998 "integer induction.\n");
5999 STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
)
6000 = INTEGER_INDUC_COND_REDUCTION
;
6004 /* Loop peeling modifies initial value of reduction PHI, which
6005 makes the reduction stmt to be transformed different to the
6006 original stmt analyzed. We need to record reduction code for
6007 CONST_COND_REDUCTION type reduction at analyzing stage, thus
6008 it can be used directly at transform stage. */
6009 if (STMT_VINFO_VEC_CONST_COND_REDUC_CODE (stmt_info
) == MAX_EXPR
6010 || STMT_VINFO_VEC_CONST_COND_REDUC_CODE (stmt_info
) == MIN_EXPR
)
6012 /* Also set the reduction type to CONST_COND_REDUCTION. */
6013 gcc_assert (cond_reduc_dt
== vect_constant_def
);
6014 STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
) = CONST_COND_REDUCTION
;
6016 else if (cond_reduc_dt
== vect_constant_def
)
6018 enum vect_def_type cond_initial_dt
;
6019 gimple
*def_stmt
= SSA_NAME_DEF_STMT (ops
[reduc_index
]);
6020 tree cond_initial_val
6021 = PHI_ARG_DEF_FROM_EDGE (def_stmt
, loop_preheader_edge (loop
));
6023 gcc_assert (cond_reduc_val
!= NULL_TREE
);
6024 vect_is_simple_use (cond_initial_val
, loop_vinfo
,
6025 &def_stmt
, &cond_initial_dt
);
6026 if (cond_initial_dt
== vect_constant_def
6027 && types_compatible_p (TREE_TYPE (cond_initial_val
),
6028 TREE_TYPE (cond_reduc_val
)))
6030 tree e
= fold_binary (LE_EXPR
, boolean_type_node
,
6031 cond_initial_val
, cond_reduc_val
);
6032 if (e
&& (integer_onep (e
) || integer_zerop (e
)))
6034 if (dump_enabled_p ())
6035 dump_printf_loc (MSG_NOTE
, vect_location
,
6036 "condition expression based on "
6037 "compile time constant.\n");
6038 /* Record reduction code at analysis stage. */
6039 STMT_VINFO_VEC_CONST_COND_REDUC_CODE (stmt_info
)
6040 = integer_onep (e
) ? MAX_EXPR
: MIN_EXPR
;
6041 STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
)
6042 = CONST_COND_REDUCTION
;
6049 gcc_assert (tmp
== orig_stmt
6050 || GROUP_FIRST_ELEMENT (vinfo_for_stmt (tmp
)) == orig_stmt
);
6052 /* We changed STMT to be the first stmt in reduction chain, hence we
6053 check that in this case the first element in the chain is STMT. */
6054 gcc_assert (stmt
== tmp
6055 || GROUP_FIRST_ELEMENT (vinfo_for_stmt (tmp
)) == stmt
);
6057 if (STMT_VINFO_LIVE_P (vinfo_for_stmt (reduc_def_stmt
)))
6063 ncopies
= vect_get_num_copies (loop_vinfo
, vectype_in
);
6065 gcc_assert (ncopies
>= 1);
6067 vec_mode
= TYPE_MODE (vectype_in
);
6069 if (code
== COND_EXPR
)
6071 /* Only call during the analysis stage, otherwise we'll lose
6073 if (!vec_stmt
&& !vectorizable_condition (stmt
, gsi
, NULL
,
6074 ops
[reduc_index
], 0, NULL
))
6076 if (dump_enabled_p ())
6077 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
6078 "unsupported condition in reduction\n");
6084 /* 4. Supportable by target? */
6086 if (code
== LSHIFT_EXPR
|| code
== RSHIFT_EXPR
6087 || code
== LROTATE_EXPR
|| code
== RROTATE_EXPR
)
6089 /* Shifts and rotates are only supported by vectorizable_shifts,
6090 not vectorizable_reduction. */
6091 if (dump_enabled_p ())
6092 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
6093 "unsupported shift or rotation.\n");
6097 /* 4.1. check support for the operation in the loop */
6098 optab
= optab_for_tree_code (code
, vectype_in
, optab_default
);
6101 if (dump_enabled_p ())
6102 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
6108 if (optab_handler (optab
, vec_mode
) == CODE_FOR_nothing
)
6110 if (dump_enabled_p ())
6111 dump_printf (MSG_NOTE
, "op not supported by target.\n");
6113 if (GET_MODE_SIZE (vec_mode
) != UNITS_PER_WORD
6114 || !vect_worthwhile_without_simd_p (loop_vinfo
, code
))
6117 if (dump_enabled_p ())
6118 dump_printf (MSG_NOTE
, "proceeding using word mode.\n");
6121 /* Worthwhile without SIMD support? */
6122 if (!VECTOR_MODE_P (TYPE_MODE (vectype_in
))
6123 && !vect_worthwhile_without_simd_p (loop_vinfo
, code
))
6125 if (dump_enabled_p ())
6126 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
6127 "not worthwhile without SIMD support.\n");
6133 /* 4.2. Check support for the epilog operation.
6135 If STMT represents a reduction pattern, then the type of the
6136 reduction variable may be different than the type of the rest
6137 of the arguments. For example, consider the case of accumulation
6138 of shorts into an int accumulator; The original code:
6139 S1: int_a = (int) short_a;
6140 orig_stmt-> S2: int_acc = plus <int_a ,int_acc>;
6143 STMT: int_acc = widen_sum <short_a, int_acc>
6146 1. The tree-code that is used to create the vector operation in the
6147 epilog code (that reduces the partial results) is not the
6148 tree-code of STMT, but is rather the tree-code of the original
6149 stmt from the pattern that STMT is replacing. I.e, in the example
6150 above we want to use 'widen_sum' in the loop, but 'plus' in the
6152 2. The type (mode) we use to check available target support
6153 for the vector operation to be created in the *epilog*, is
6154 determined by the type of the reduction variable (in the example
6155 above we'd check this: optab_handler (plus_optab, vect_int_mode])).
6156 However the type (mode) we use to check available target support
6157 for the vector operation to be created *inside the loop*, is
6158 determined by the type of the other arguments to STMT (in the
6159 example we'd check this: optab_handler (widen_sum_optab,
6162 This is contrary to "regular" reductions, in which the types of all
6163 the arguments are the same as the type of the reduction variable.
6164 For "regular" reductions we can therefore use the same vector type
6165 (and also the same tree-code) when generating the epilog code and
6166 when generating the code inside the loop. */
6170 /* This is a reduction pattern: get the vectype from the type of the
6171 reduction variable, and get the tree-code from orig_stmt. */
6172 gcc_assert (STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
)
6173 == TREE_CODE_REDUCTION
);
6174 orig_code
= gimple_assign_rhs_code (orig_stmt
);
6175 gcc_assert (vectype_out
);
6176 vec_mode
= TYPE_MODE (vectype_out
);
6180 /* Regular reduction: use the same vectype and tree-code as used for
6181 the vector code inside the loop can be used for the epilog code. */
6184 if (code
== MINUS_EXPR
)
6185 orig_code
= PLUS_EXPR
;
6187 /* For simple condition reductions, replace with the actual expression
6188 we want to base our reduction around. */
6189 if (STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
) == CONST_COND_REDUCTION
)
6191 orig_code
= STMT_VINFO_VEC_CONST_COND_REDUC_CODE (stmt_info
);
6192 gcc_assert (orig_code
== MAX_EXPR
|| orig_code
== MIN_EXPR
);
6194 else if (STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
)
6195 == INTEGER_INDUC_COND_REDUCTION
)
6196 orig_code
= cond_reduc_op_code
;
6201 def_bb
= gimple_bb (reduc_def_stmt
);
6202 def_stmt_loop
= def_bb
->loop_father
;
6203 def_arg
= PHI_ARG_DEF_FROM_EDGE (reduc_def_stmt
,
6204 loop_preheader_edge (def_stmt_loop
));
6205 if (TREE_CODE (def_arg
) == SSA_NAME
6206 && (def_arg_stmt
= SSA_NAME_DEF_STMT (def_arg
))
6207 && gimple_code (def_arg_stmt
) == GIMPLE_PHI
6208 && flow_bb_inside_loop_p (outer_loop
, gimple_bb (def_arg_stmt
))
6209 && vinfo_for_stmt (def_arg_stmt
)
6210 && STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def_arg_stmt
))
6211 == vect_double_reduction_def
)
6212 double_reduc
= true;
6215 reduc_fn
= IFN_LAST
;
6217 if (STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
) != COND_REDUCTION
)
6219 if (reduction_fn_for_scalar_code (orig_code
, &reduc_fn
))
6221 if (reduc_fn
!= IFN_LAST
6222 && !direct_internal_fn_supported_p (reduc_fn
, vectype_out
,
6223 OPTIMIZE_FOR_SPEED
))
6225 if (dump_enabled_p ())
6226 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
6227 "reduc op not supported by target.\n");
6229 reduc_fn
= IFN_LAST
;
6234 if (!nested_cycle
|| double_reduc
)
6236 if (dump_enabled_p ())
6237 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
6238 "no reduc code for scalar code.\n");
6246 int scalar_precision
6247 = GET_MODE_PRECISION (SCALAR_TYPE_MODE (scalar_type
));
6248 cr_index_scalar_type
= make_unsigned_type (scalar_precision
);
6249 cr_index_vector_type
= build_vector_type
6250 (cr_index_scalar_type
, TYPE_VECTOR_SUBPARTS (vectype_out
));
6252 if (direct_internal_fn_supported_p (IFN_REDUC_MAX
, cr_index_vector_type
,
6253 OPTIMIZE_FOR_SPEED
))
6254 reduc_fn
= IFN_REDUC_MAX
;
6258 || STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
) != TREE_CODE_REDUCTION
)
6261 if (dump_enabled_p ())
6262 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
6263 "multiple types in double reduction or condition "
6268 /* In case of widenning multiplication by a constant, we update the type
6269 of the constant to be the type of the other operand. We check that the
6270 constant fits the type in the pattern recognition pass. */
6271 if (code
== DOT_PROD_EXPR
6272 && !types_compatible_p (TREE_TYPE (ops
[0]), TREE_TYPE (ops
[1])))
6274 if (TREE_CODE (ops
[0]) == INTEGER_CST
)
6275 ops
[0] = fold_convert (TREE_TYPE (ops
[1]), ops
[0]);
6276 else if (TREE_CODE (ops
[1]) == INTEGER_CST
)
6277 ops
[1] = fold_convert (TREE_TYPE (ops
[0]), ops
[1]);
6280 if (dump_enabled_p ())
6281 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
6282 "invalid types in dot-prod\n");
6288 if (STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
) == COND_REDUCTION
)
6292 if (! max_loop_iterations (loop
, &ni
))
6294 if (dump_enabled_p ())
6295 dump_printf_loc (MSG_NOTE
, vect_location
,
6296 "loop count not known, cannot create cond "
6300 /* Convert backedges to iterations. */
6303 /* The additional index will be the same type as the condition. Check
6304 that the loop can fit into this less one (because we'll use up the
6305 zero slot for when there are no matches). */
6306 tree max_index
= TYPE_MAX_VALUE (cr_index_scalar_type
);
6307 if (wi::geu_p (ni
, wi::to_widest (max_index
)))
6309 if (dump_enabled_p ())
6310 dump_printf_loc (MSG_NOTE
, vect_location
,
6311 "loop size is greater than data size.\n");
6316 /* In case the vectorization factor (VF) is bigger than the number
6317 of elements that we can fit in a vectype (nunits), we have to generate
6318 more than one vector stmt - i.e - we need to "unroll" the
6319 vector stmt by a factor VF/nunits. For more details see documentation
6320 in vectorizable_operation. */
6322 /* If the reduction is used in an outer loop we need to generate
6323 VF intermediate results, like so (e.g. for ncopies=2):
6328 (i.e. we generate VF results in 2 registers).
6329 In this case we have a separate def-use cycle for each copy, and therefore
6330 for each copy we get the vector def for the reduction variable from the
6331 respective phi node created for this copy.
6333 Otherwise (the reduction is unused in the loop nest), we can combine
6334 together intermediate results, like so (e.g. for ncopies=2):
6338 (i.e. we generate VF/2 results in a single register).
6339 In this case for each copy we get the vector def for the reduction variable
6340 from the vectorized reduction operation generated in the previous iteration.
6342 This only works when we see both the reduction PHI and its only consumer
6343 in vectorizable_reduction and there are no intermediate stmts
6345 use_operand_p use_p
;
6348 && (STMT_VINFO_RELEVANT (stmt_info
) <= vect_used_only_live
)
6349 && single_imm_use (gimple_phi_result (reduc_def_stmt
), &use_p
, &use_stmt
)
6350 && (use_stmt
== stmt
6351 || STMT_VINFO_RELATED_STMT (vinfo_for_stmt (use_stmt
)) == stmt
))
6353 single_defuse_cycle
= true;
6357 epilog_copies
= ncopies
;
6359 /* If the reduction stmt is one of the patterns that have lane
6360 reduction embedded we cannot handle the case of ! single_defuse_cycle. */
6362 && ! single_defuse_cycle
)
6363 && (code
== DOT_PROD_EXPR
6364 || code
== WIDEN_SUM_EXPR
6365 || code
== SAD_EXPR
))
6367 if (dump_enabled_p ())
6368 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
6369 "multi def-use cycle not possible for lane-reducing "
6370 "reduction operation\n");
6374 if (!vec_stmt
) /* transformation not required. */
6377 vect_model_reduction_cost (stmt_info
, reduc_fn
, ncopies
);
6378 STMT_VINFO_TYPE (stmt_info
) = reduc_vec_info_type
;
6384 if (dump_enabled_p ())
6385 dump_printf_loc (MSG_NOTE
, vect_location
, "transform reduction.\n");
6387 /* FORNOW: Multiple types are not supported for condition. */
6388 if (code
== COND_EXPR
)
6389 gcc_assert (ncopies
== 1);
6391 /* Create the destination vector */
6392 vec_dest
= vect_create_destination_var (scalar_dest
, vectype_out
);
6394 prev_stmt_info
= NULL
;
6395 prev_phi_info
= NULL
;
6397 vec_num
= SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node
);
6401 vec_oprnds0
.create (1);
6402 vec_oprnds1
.create (1);
6403 if (op_type
== ternary_op
)
6404 vec_oprnds2
.create (1);
6407 phis
.create (vec_num
);
6408 vect_defs
.create (vec_num
);
6410 vect_defs
.quick_push (NULL_TREE
);
6413 phis
.splice (SLP_TREE_VEC_STMTS (slp_node_instance
->reduc_phis
));
6415 phis
.quick_push (STMT_VINFO_VEC_STMT (vinfo_for_stmt (reduc_def_stmt
)));
6417 for (j
= 0; j
< ncopies
; j
++)
6419 if (code
== COND_EXPR
)
6421 gcc_assert (!slp_node
);
6422 vectorizable_condition (stmt
, gsi
, vec_stmt
,
6423 PHI_RESULT (phis
[0]),
6425 /* Multiple types are not supported for condition. */
6434 /* Get vec defs for all the operands except the reduction index,
6435 ensuring the ordering of the ops in the vector is kept. */
6436 auto_vec
<tree
, 3> slp_ops
;
6437 auto_vec
<vec
<tree
>, 3> vec_defs
;
6439 slp_ops
.quick_push (ops
[0]);
6440 slp_ops
.quick_push (ops
[1]);
6441 if (op_type
== ternary_op
)
6442 slp_ops
.quick_push (ops
[2]);
6444 vect_get_slp_defs (slp_ops
, slp_node
, &vec_defs
);
6446 vec_oprnds0
.safe_splice (vec_defs
[0]);
6447 vec_defs
[0].release ();
6448 vec_oprnds1
.safe_splice (vec_defs
[1]);
6449 vec_defs
[1].release ();
6450 if (op_type
== ternary_op
)
6452 vec_oprnds2
.safe_splice (vec_defs
[2]);
6453 vec_defs
[2].release ();
6458 vec_oprnds0
.quick_push
6459 (vect_get_vec_def_for_operand (ops
[0], stmt
));
6460 vec_oprnds1
.quick_push
6461 (vect_get_vec_def_for_operand (ops
[1], stmt
));
6462 if (op_type
== ternary_op
)
6463 vec_oprnds2
.quick_push
6464 (vect_get_vec_def_for_operand (ops
[2], stmt
));
6471 gcc_assert (reduc_index
!= -1 || ! single_defuse_cycle
);
6473 if (single_defuse_cycle
&& reduc_index
== 0)
6474 vec_oprnds0
[0] = gimple_assign_lhs (new_stmt
);
6477 = vect_get_vec_def_for_stmt_copy (dts
[0], vec_oprnds0
[0]);
6478 if (single_defuse_cycle
&& reduc_index
== 1)
6479 vec_oprnds1
[0] = gimple_assign_lhs (new_stmt
);
6482 = vect_get_vec_def_for_stmt_copy (dts
[1], vec_oprnds1
[0]);
6483 if (op_type
== ternary_op
)
6485 if (single_defuse_cycle
&& reduc_index
== 2)
6486 vec_oprnds2
[0] = gimple_assign_lhs (new_stmt
);
6489 = vect_get_vec_def_for_stmt_copy (dts
[2], vec_oprnds2
[0]);
6494 FOR_EACH_VEC_ELT (vec_oprnds0
, i
, def0
)
6496 tree vop
[3] = { def0
, vec_oprnds1
[i
], NULL_TREE
};
6497 if (op_type
== ternary_op
)
6498 vop
[2] = vec_oprnds2
[i
];
6500 new_temp
= make_ssa_name (vec_dest
, new_stmt
);
6501 new_stmt
= gimple_build_assign (new_temp
, code
,
6502 vop
[0], vop
[1], vop
[2]);
6503 vect_finish_stmt_generation (stmt
, new_stmt
, gsi
);
6507 SLP_TREE_VEC_STMTS (slp_node
).quick_push (new_stmt
);
6508 vect_defs
.quick_push (new_temp
);
6511 vect_defs
[0] = new_temp
;
6518 STMT_VINFO_VEC_STMT (stmt_info
) = *vec_stmt
= new_stmt
;
6520 STMT_VINFO_RELATED_STMT (prev_stmt_info
) = new_stmt
;
6522 prev_stmt_info
= vinfo_for_stmt (new_stmt
);
6525 /* Finalize the reduction-phi (set its arguments) and create the
6526 epilog reduction code. */
6527 if ((!single_defuse_cycle
|| code
== COND_EXPR
) && !slp_node
)
6528 vect_defs
[0] = gimple_assign_lhs (*vec_stmt
);
6530 vect_create_epilog_for_reduction (vect_defs
, stmt
, reduc_def_stmt
,
6531 epilog_copies
, reduc_fn
, phis
,
6532 double_reduc
, slp_node
, slp_node_instance
,
6533 cond_reduc_val
, cond_reduc_op_code
);
6538 /* Function vect_min_worthwhile_factor.
6540 For a loop where we could vectorize the operation indicated by CODE,
6541 return the minimum vectorization factor that makes it worthwhile
6542 to use generic vectors. */
6544 vect_min_worthwhile_factor (enum tree_code code
)
6564 /* Return true if VINFO indicates we are doing loop vectorization and if
6565 it is worth decomposing CODE operations into scalar operations for
6566 that loop's vectorization factor. */
6569 vect_worthwhile_without_simd_p (vec_info
*vinfo
, tree_code code
)
6571 loop_vec_info loop_vinfo
= dyn_cast
<loop_vec_info
> (vinfo
);
6573 && (LOOP_VINFO_VECT_FACTOR (loop_vinfo
)
6574 >= vect_min_worthwhile_factor (code
)));
6577 /* Function vectorizable_induction
6579 Check if PHI performs an induction computation that can be vectorized.
6580 If VEC_STMT is also passed, vectorize the induction PHI: create a vectorized
6581 phi to replace it, put it in VEC_STMT, and add it to the same basic block.
6582 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
6585 vectorizable_induction (gimple
*phi
,
6586 gimple_stmt_iterator
*gsi ATTRIBUTE_UNUSED
,
6587 gimple
**vec_stmt
, slp_tree slp_node
)
6589 stmt_vec_info stmt_info
= vinfo_for_stmt (phi
);
6590 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_info
);
6591 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
6593 bool nested_in_vect_loop
= false;
6594 struct loop
*iv_loop
;
6596 edge pe
= loop_preheader_edge (loop
);
6598 tree new_vec
, vec_init
, vec_step
, t
;
6601 gphi
*induction_phi
;
6602 tree induc_def
, vec_dest
;
6603 tree init_expr
, step_expr
;
6604 int vf
= LOOP_VINFO_VECT_FACTOR (loop_vinfo
);
6608 imm_use_iterator imm_iter
;
6609 use_operand_p use_p
;
6613 gimple_stmt_iterator si
;
6614 basic_block bb
= gimple_bb (phi
);
6616 if (gimple_code (phi
) != GIMPLE_PHI
)
6619 if (!STMT_VINFO_RELEVANT_P (stmt_info
))
6622 /* Make sure it was recognized as induction computation. */
6623 if (STMT_VINFO_DEF_TYPE (stmt_info
) != vect_induction_def
)
6626 tree vectype
= STMT_VINFO_VECTYPE (stmt_info
);
6627 unsigned nunits
= TYPE_VECTOR_SUBPARTS (vectype
);
6632 ncopies
= vect_get_num_copies (loop_vinfo
, vectype
);
6633 gcc_assert (ncopies
>= 1);
6635 /* FORNOW. These restrictions should be relaxed. */
6636 if (nested_in_vect_loop_p (loop
, phi
))
6638 imm_use_iterator imm_iter
;
6639 use_operand_p use_p
;
6646 if (dump_enabled_p ())
6647 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
6648 "multiple types in nested loop.\n");
6652 /* FORNOW: outer loop induction with SLP not supported. */
6653 if (STMT_SLP_TYPE (stmt_info
))
6657 latch_e
= loop_latch_edge (loop
->inner
);
6658 loop_arg
= PHI_ARG_DEF_FROM_EDGE (phi
, latch_e
);
6659 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, loop_arg
)
6661 gimple
*use_stmt
= USE_STMT (use_p
);
6662 if (is_gimple_debug (use_stmt
))
6665 if (!flow_bb_inside_loop_p (loop
->inner
, gimple_bb (use_stmt
)))
6667 exit_phi
= use_stmt
;
6673 stmt_vec_info exit_phi_vinfo
= vinfo_for_stmt (exit_phi
);
6674 if (!(STMT_VINFO_RELEVANT_P (exit_phi_vinfo
)
6675 && !STMT_VINFO_LIVE_P (exit_phi_vinfo
)))
6677 if (dump_enabled_p ())
6678 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
6679 "inner-loop induction only used outside "
6680 "of the outer vectorized loop.\n");
6685 nested_in_vect_loop
= true;
6686 iv_loop
= loop
->inner
;
6690 gcc_assert (iv_loop
== (gimple_bb (phi
))->loop_father
);
6692 if (!vec_stmt
) /* transformation not required. */
6694 STMT_VINFO_TYPE (stmt_info
) = induc_vec_info_type
;
6695 if (dump_enabled_p ())
6696 dump_printf_loc (MSG_NOTE
, vect_location
,
6697 "=== vectorizable_induction ===\n");
6698 vect_model_induction_cost (stmt_info
, ncopies
);
6704 /* Compute a vector variable, initialized with the first VF values of
6705 the induction variable. E.g., for an iv with IV_PHI='X' and
6706 evolution S, for a vector of 4 units, we want to compute:
6707 [X, X + S, X + 2*S, X + 3*S]. */
6709 if (dump_enabled_p ())
6710 dump_printf_loc (MSG_NOTE
, vect_location
, "transform induction phi.\n");
6712 latch_e
= loop_latch_edge (iv_loop
);
6713 loop_arg
= PHI_ARG_DEF_FROM_EDGE (phi
, latch_e
);
6715 step_expr
= STMT_VINFO_LOOP_PHI_EVOLUTION_PART (stmt_info
);
6716 gcc_assert (step_expr
!= NULL_TREE
);
6718 pe
= loop_preheader_edge (iv_loop
);
6719 init_expr
= PHI_ARG_DEF_FROM_EDGE (phi
,
6720 loop_preheader_edge (iv_loop
));
6722 /* Convert the step to the desired type. */
6724 step_expr
= gimple_convert (&stmts
, TREE_TYPE (vectype
), step_expr
);
6727 new_bb
= gsi_insert_seq_on_edge_immediate (pe
, stmts
);
6728 gcc_assert (!new_bb
);
6731 /* Find the first insertion point in the BB. */
6732 si
= gsi_after_labels (bb
);
6734 /* For SLP induction we have to generate several IVs as for example
6735 with group size 3 we need [i, i, i, i + S] [i + S, i + S, i + 2*S, i + 2*S]
6736 [i + 2*S, i + 3*S, i + 3*S, i + 3*S]. The step is the same uniform
6737 [VF*S, VF*S, VF*S, VF*S] for all. */
6740 /* Convert the init to the desired type. */
6742 init_expr
= gimple_convert (&stmts
, TREE_TYPE (vectype
), init_expr
);
6745 new_bb
= gsi_insert_seq_on_edge_immediate (pe
, stmts
);
6746 gcc_assert (!new_bb
);
6749 /* Generate [VF*S, VF*S, ... ]. */
6750 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (step_expr
)))
6752 expr
= build_int_cst (integer_type_node
, vf
);
6753 expr
= fold_convert (TREE_TYPE (step_expr
), expr
);
6756 expr
= build_int_cst (TREE_TYPE (step_expr
), vf
);
6757 new_name
= fold_build2 (MULT_EXPR
, TREE_TYPE (step_expr
),
6759 if (! CONSTANT_CLASS_P (new_name
))
6760 new_name
= vect_init_vector (phi
, new_name
,
6761 TREE_TYPE (step_expr
), NULL
);
6762 new_vec
= build_vector_from_val (vectype
, new_name
);
6763 vec_step
= vect_init_vector (phi
, new_vec
, vectype
, NULL
);
6765 /* Now generate the IVs. */
6766 unsigned group_size
= SLP_TREE_SCALAR_STMTS (slp_node
).length ();
6767 unsigned nvects
= SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node
);
6768 unsigned elts
= nunits
* nvects
;
6769 unsigned nivs
= least_common_multiple (group_size
, nunits
) / nunits
;
6770 gcc_assert (elts
% group_size
== 0);
6771 tree elt
= init_expr
;
6773 for (ivn
= 0; ivn
< nivs
; ++ivn
)
6775 tree_vector_builder
elts (vectype
, nunits
, 1);
6777 for (unsigned eltn
= 0; eltn
< nunits
; ++eltn
)
6779 if (ivn
*nunits
+ eltn
>= group_size
6780 && (ivn
*nunits
+ eltn
) % group_size
== 0)
6781 elt
= gimple_build (&stmts
, PLUS_EXPR
, TREE_TYPE (elt
),
6783 elts
.quick_push (elt
);
6785 vec_init
= gimple_build_vector (&stmts
, &elts
);
6788 new_bb
= gsi_insert_seq_on_edge_immediate (pe
, stmts
);
6789 gcc_assert (!new_bb
);
6792 /* Create the induction-phi that defines the induction-operand. */
6793 vec_dest
= vect_get_new_vect_var (vectype
, vect_simple_var
, "vec_iv_");
6794 induction_phi
= create_phi_node (vec_dest
, iv_loop
->header
);
6795 set_vinfo_for_stmt (induction_phi
,
6796 new_stmt_vec_info (induction_phi
, loop_vinfo
));
6797 induc_def
= PHI_RESULT (induction_phi
);
6799 /* Create the iv update inside the loop */
6800 vec_def
= make_ssa_name (vec_dest
);
6801 new_stmt
= gimple_build_assign (vec_def
, PLUS_EXPR
, induc_def
, vec_step
);
6802 gsi_insert_before (&si
, new_stmt
, GSI_SAME_STMT
);
6803 set_vinfo_for_stmt (new_stmt
, new_stmt_vec_info (new_stmt
, loop_vinfo
));
6805 /* Set the arguments of the phi node: */
6806 add_phi_arg (induction_phi
, vec_init
, pe
, UNKNOWN_LOCATION
);
6807 add_phi_arg (induction_phi
, vec_def
, loop_latch_edge (iv_loop
),
6810 SLP_TREE_VEC_STMTS (slp_node
).quick_push (induction_phi
);
6813 /* Re-use IVs when we can. */
6817 = least_common_multiple (group_size
, nunits
) / group_size
;
6818 /* Generate [VF'*S, VF'*S, ... ]. */
6819 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (step_expr
)))
6821 expr
= build_int_cst (integer_type_node
, vfp
);
6822 expr
= fold_convert (TREE_TYPE (step_expr
), expr
);
6825 expr
= build_int_cst (TREE_TYPE (step_expr
), vfp
);
6826 new_name
= fold_build2 (MULT_EXPR
, TREE_TYPE (step_expr
),
6828 if (! CONSTANT_CLASS_P (new_name
))
6829 new_name
= vect_init_vector (phi
, new_name
,
6830 TREE_TYPE (step_expr
), NULL
);
6831 new_vec
= build_vector_from_val (vectype
, new_name
);
6832 vec_step
= vect_init_vector (phi
, new_vec
, vectype
, NULL
);
6833 for (; ivn
< nvects
; ++ivn
)
6835 gimple
*iv
= SLP_TREE_VEC_STMTS (slp_node
)[ivn
- nivs
];
6837 if (gimple_code (iv
) == GIMPLE_PHI
)
6838 def
= gimple_phi_result (iv
);
6840 def
= gimple_assign_lhs (iv
);
6841 new_stmt
= gimple_build_assign (make_ssa_name (vectype
),
6844 if (gimple_code (iv
) == GIMPLE_PHI
)
6845 gsi_insert_before (&si
, new_stmt
, GSI_SAME_STMT
);
6848 gimple_stmt_iterator tgsi
= gsi_for_stmt (iv
);
6849 gsi_insert_after (&tgsi
, new_stmt
, GSI_CONTINUE_LINKING
);
6851 set_vinfo_for_stmt (new_stmt
,
6852 new_stmt_vec_info (new_stmt
, loop_vinfo
));
6853 SLP_TREE_VEC_STMTS (slp_node
).quick_push (new_stmt
);
6860 /* Create the vector that holds the initial_value of the induction. */
6861 if (nested_in_vect_loop
)
6863 /* iv_loop is nested in the loop to be vectorized. init_expr had already
6864 been created during vectorization of previous stmts. We obtain it
6865 from the STMT_VINFO_VEC_STMT of the defining stmt. */
6866 vec_init
= vect_get_vec_def_for_operand (init_expr
, phi
);
6867 /* If the initial value is not of proper type, convert it. */
6868 if (!useless_type_conversion_p (vectype
, TREE_TYPE (vec_init
)))
6871 = gimple_build_assign (vect_get_new_ssa_name (vectype
,
6875 build1 (VIEW_CONVERT_EXPR
, vectype
,
6877 vec_init
= gimple_assign_lhs (new_stmt
);
6878 new_bb
= gsi_insert_on_edge_immediate (loop_preheader_edge (iv_loop
),
6880 gcc_assert (!new_bb
);
6881 set_vinfo_for_stmt (new_stmt
,
6882 new_stmt_vec_info (new_stmt
, loop_vinfo
));
6887 /* iv_loop is the loop to be vectorized. Create:
6888 vec_init = [X, X+S, X+2*S, X+3*S] (S = step_expr, X = init_expr) */
6890 new_name
= gimple_convert (&stmts
, TREE_TYPE (vectype
), init_expr
);
6892 tree_vector_builder
elts (vectype
, nunits
, 1);
6893 elts
.quick_push (new_name
);
6894 for (i
= 1; i
< nunits
; i
++)
6896 /* Create: new_name_i = new_name + step_expr */
6897 new_name
= gimple_build (&stmts
, PLUS_EXPR
, TREE_TYPE (new_name
),
6898 new_name
, step_expr
);
6899 elts
.quick_push (new_name
);
6901 /* Create a vector from [new_name_0, new_name_1, ...,
6902 new_name_nunits-1] */
6903 vec_init
= gimple_build_vector (&stmts
, &elts
);
6906 new_bb
= gsi_insert_seq_on_edge_immediate (pe
, stmts
);
6907 gcc_assert (!new_bb
);
6912 /* Create the vector that holds the step of the induction. */
6913 if (nested_in_vect_loop
)
6914 /* iv_loop is nested in the loop to be vectorized. Generate:
6915 vec_step = [S, S, S, S] */
6916 new_name
= step_expr
;
6919 /* iv_loop is the loop to be vectorized. Generate:
6920 vec_step = [VF*S, VF*S, VF*S, VF*S] */
6921 gimple_seq seq
= NULL
;
6922 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (step_expr
)))
6924 expr
= build_int_cst (integer_type_node
, vf
);
6925 expr
= gimple_build (&seq
, FLOAT_EXPR
, TREE_TYPE (step_expr
), expr
);
6928 expr
= build_int_cst (TREE_TYPE (step_expr
), vf
);
6929 new_name
= gimple_build (&seq
, MULT_EXPR
, TREE_TYPE (step_expr
),
6933 new_bb
= gsi_insert_seq_on_edge_immediate (pe
, seq
);
6934 gcc_assert (!new_bb
);
6938 t
= unshare_expr (new_name
);
6939 gcc_assert (CONSTANT_CLASS_P (new_name
)
6940 || TREE_CODE (new_name
) == SSA_NAME
);
6941 new_vec
= build_vector_from_val (vectype
, t
);
6942 vec_step
= vect_init_vector (phi
, new_vec
, vectype
, NULL
);
6945 /* Create the following def-use cycle:
6950 vec_iv = PHI <vec_init, vec_loop>
6954 vec_loop = vec_iv + vec_step; */
6956 /* Create the induction-phi that defines the induction-operand. */
6957 vec_dest
= vect_get_new_vect_var (vectype
, vect_simple_var
, "vec_iv_");
6958 induction_phi
= create_phi_node (vec_dest
, iv_loop
->header
);
6959 set_vinfo_for_stmt (induction_phi
,
6960 new_stmt_vec_info (induction_phi
, loop_vinfo
));
6961 induc_def
= PHI_RESULT (induction_phi
);
6963 /* Create the iv update inside the loop */
6964 vec_def
= make_ssa_name (vec_dest
);
6965 new_stmt
= gimple_build_assign (vec_def
, PLUS_EXPR
, induc_def
, vec_step
);
6966 gsi_insert_before (&si
, new_stmt
, GSI_SAME_STMT
);
6967 set_vinfo_for_stmt (new_stmt
, new_stmt_vec_info (new_stmt
, loop_vinfo
));
6969 /* Set the arguments of the phi node: */
6970 add_phi_arg (induction_phi
, vec_init
, pe
, UNKNOWN_LOCATION
);
6971 add_phi_arg (induction_phi
, vec_def
, loop_latch_edge (iv_loop
),
6974 STMT_VINFO_VEC_STMT (stmt_info
) = *vec_stmt
= induction_phi
;
6976 /* In case that vectorization factor (VF) is bigger than the number
6977 of elements that we can fit in a vectype (nunits), we have to generate
6978 more than one vector stmt - i.e - we need to "unroll" the
6979 vector stmt by a factor VF/nunits. For more details see documentation
6980 in vectorizable_operation. */
6984 gimple_seq seq
= NULL
;
6985 stmt_vec_info prev_stmt_vinfo
;
6986 /* FORNOW. This restriction should be relaxed. */
6987 gcc_assert (!nested_in_vect_loop
);
6989 /* Create the vector that holds the step of the induction. */
6990 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (step_expr
)))
6992 expr
= build_int_cst (integer_type_node
, nunits
);
6993 expr
= gimple_build (&seq
, FLOAT_EXPR
, TREE_TYPE (step_expr
), expr
);
6996 expr
= build_int_cst (TREE_TYPE (step_expr
), nunits
);
6997 new_name
= gimple_build (&seq
, MULT_EXPR
, TREE_TYPE (step_expr
),
7001 new_bb
= gsi_insert_seq_on_edge_immediate (pe
, seq
);
7002 gcc_assert (!new_bb
);
7005 t
= unshare_expr (new_name
);
7006 gcc_assert (CONSTANT_CLASS_P (new_name
)
7007 || TREE_CODE (new_name
) == SSA_NAME
);
7008 new_vec
= build_vector_from_val (vectype
, t
);
7009 vec_step
= vect_init_vector (phi
, new_vec
, vectype
, NULL
);
7011 vec_def
= induc_def
;
7012 prev_stmt_vinfo
= vinfo_for_stmt (induction_phi
);
7013 for (i
= 1; i
< ncopies
; i
++)
7015 /* vec_i = vec_prev + vec_step */
7016 new_stmt
= gimple_build_assign (vec_dest
, PLUS_EXPR
,
7018 vec_def
= make_ssa_name (vec_dest
, new_stmt
);
7019 gimple_assign_set_lhs (new_stmt
, vec_def
);
7021 gsi_insert_before (&si
, new_stmt
, GSI_SAME_STMT
);
7022 set_vinfo_for_stmt (new_stmt
,
7023 new_stmt_vec_info (new_stmt
, loop_vinfo
));
7024 STMT_VINFO_RELATED_STMT (prev_stmt_vinfo
) = new_stmt
;
7025 prev_stmt_vinfo
= vinfo_for_stmt (new_stmt
);
7029 if (nested_in_vect_loop
)
7031 /* Find the loop-closed exit-phi of the induction, and record
7032 the final vector of induction results: */
7034 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, loop_arg
)
7036 gimple
*use_stmt
= USE_STMT (use_p
);
7037 if (is_gimple_debug (use_stmt
))
7040 if (!flow_bb_inside_loop_p (iv_loop
, gimple_bb (use_stmt
)))
7042 exit_phi
= use_stmt
;
7048 stmt_vec_info stmt_vinfo
= vinfo_for_stmt (exit_phi
);
7049 /* FORNOW. Currently not supporting the case that an inner-loop induction
7050 is not used in the outer-loop (i.e. only outside the outer-loop). */
7051 gcc_assert (STMT_VINFO_RELEVANT_P (stmt_vinfo
)
7052 && !STMT_VINFO_LIVE_P (stmt_vinfo
));
7054 STMT_VINFO_VEC_STMT (stmt_vinfo
) = new_stmt
;
7055 if (dump_enabled_p ())
7057 dump_printf_loc (MSG_NOTE
, vect_location
,
7058 "vector of inductions after inner-loop:");
7059 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
, new_stmt
, 0);
7065 if (dump_enabled_p ())
7067 dump_printf_loc (MSG_NOTE
, vect_location
,
7068 "transform induction: created def-use cycle: ");
7069 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
, induction_phi
, 0);
7070 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
,
7071 SSA_NAME_DEF_STMT (vec_def
), 0);
7077 /* Function vectorizable_live_operation.
7079 STMT computes a value that is used outside the loop. Check if
7080 it can be supported. */
7083 vectorizable_live_operation (gimple
*stmt
,
7084 gimple_stmt_iterator
*gsi ATTRIBUTE_UNUSED
,
7085 slp_tree slp_node
, int slp_index
,
7088 stmt_vec_info stmt_info
= vinfo_for_stmt (stmt
);
7089 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_info
);
7090 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
7091 imm_use_iterator imm_iter
;
7092 tree lhs
, lhs_type
, bitsize
, vec_bitsize
;
7093 tree vectype
= STMT_VINFO_VECTYPE (stmt_info
);
7094 int nunits
= TYPE_VECTOR_SUBPARTS (vectype
);
7097 auto_vec
<tree
> vec_oprnds
;
7099 gcc_assert (STMT_VINFO_LIVE_P (stmt_info
));
7101 if (STMT_VINFO_DEF_TYPE (stmt_info
) == vect_reduction_def
)
7104 /* FORNOW. CHECKME. */
7105 if (nested_in_vect_loop_p (loop
, stmt
))
7108 /* If STMT is not relevant and it is a simple assignment and its inputs are
7109 invariant then it can remain in place, unvectorized. The original last
7110 scalar value that it computes will be used. */
7111 if (!STMT_VINFO_RELEVANT_P (stmt_info
))
7113 gcc_assert (is_simple_and_all_uses_invariant (stmt
, loop_vinfo
));
7114 if (dump_enabled_p ())
7115 dump_printf_loc (MSG_NOTE
, vect_location
,
7116 "statement is simple and uses invariant. Leaving in "
7124 ncopies
= vect_get_num_copies (loop_vinfo
, vectype
);
7127 /* No transformation required. */
7130 /* If stmt has a related stmt, then use that for getting the lhs. */
7131 if (is_pattern_stmt_p (stmt_info
))
7132 stmt
= STMT_VINFO_RELATED_STMT (stmt_info
);
7134 lhs
= (is_a
<gphi
*> (stmt
)) ? gimple_phi_result (stmt
)
7135 : gimple_get_lhs (stmt
);
7136 lhs_type
= TREE_TYPE (lhs
);
7138 bitsize
= (VECTOR_BOOLEAN_TYPE_P (vectype
)
7139 ? bitsize_int (TYPE_PRECISION (TREE_TYPE (vectype
)))
7140 : TYPE_SIZE (TREE_TYPE (vectype
)));
7141 vec_bitsize
= TYPE_SIZE (vectype
);
7143 /* Get the vectorized lhs of STMT and the lane to use (counted in bits). */
7144 tree vec_lhs
, bitstart
;
7147 gcc_assert (slp_index
>= 0);
7149 int num_scalar
= SLP_TREE_SCALAR_STMTS (slp_node
).length ();
7150 int num_vec
= SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node
);
7152 /* Get the last occurrence of the scalar index from the concatenation of
7153 all the slp vectors. Calculate which slp vector it is and the index
7155 int pos
= (num_vec
* nunits
) - num_scalar
+ slp_index
;
7156 int vec_entry
= pos
/ nunits
;
7157 int vec_index
= pos
% nunits
;
7159 /* Get the correct slp vectorized stmt. */
7160 vec_lhs
= gimple_get_lhs (SLP_TREE_VEC_STMTS (slp_node
)[vec_entry
]);
7162 /* Get entry to use. */
7163 bitstart
= bitsize_int (vec_index
);
7164 bitstart
= int_const_binop (MULT_EXPR
, bitsize
, bitstart
);
7168 enum vect_def_type dt
= STMT_VINFO_DEF_TYPE (stmt_info
);
7169 vec_lhs
= vect_get_vec_def_for_operand_1 (stmt
, dt
);
7171 /* For multiple copies, get the last copy. */
7172 for (int i
= 1; i
< ncopies
; ++i
)
7173 vec_lhs
= vect_get_vec_def_for_stmt_copy (vect_unknown_def_type
,
7176 /* Get the last lane in the vector. */
7177 bitstart
= int_const_binop (MINUS_EXPR
, vec_bitsize
, bitsize
);
7180 /* Create a new vectorized stmt for the uses of STMT and insert outside the
7182 gimple_seq stmts
= NULL
;
7183 tree bftype
= TREE_TYPE (vectype
);
7184 if (VECTOR_BOOLEAN_TYPE_P (vectype
))
7185 bftype
= build_nonstandard_integer_type (tree_to_uhwi (bitsize
), 1);
7186 tree new_tree
= build3 (BIT_FIELD_REF
, bftype
, vec_lhs
, bitsize
, bitstart
);
7187 new_tree
= force_gimple_operand (fold_convert (lhs_type
, new_tree
), &stmts
,
7190 gsi_insert_seq_on_edge_immediate (single_exit (loop
), stmts
);
7192 /* Replace use of lhs with newly computed result. If the use stmt is a
7193 single arg PHI, just replace all uses of PHI result. It's necessary
7194 because lcssa PHI defining lhs may be before newly inserted stmt. */
7195 use_operand_p use_p
;
7196 FOR_EACH_IMM_USE_STMT (use_stmt
, imm_iter
, lhs
)
7197 if (!flow_bb_inside_loop_p (loop
, gimple_bb (use_stmt
))
7198 && !is_gimple_debug (use_stmt
))
7200 if (gimple_code (use_stmt
) == GIMPLE_PHI
7201 && gimple_phi_num_args (use_stmt
) == 1)
7203 replace_uses_by (gimple_phi_result (use_stmt
), new_tree
);
7207 FOR_EACH_IMM_USE_ON_STMT (use_p
, imm_iter
)
7208 SET_USE (use_p
, new_tree
);
7210 update_stmt (use_stmt
);
7216 /* Kill any debug uses outside LOOP of SSA names defined in STMT. */
7219 vect_loop_kill_debug_uses (struct loop
*loop
, gimple
*stmt
)
7221 ssa_op_iter op_iter
;
7222 imm_use_iterator imm_iter
;
7223 def_operand_p def_p
;
7226 FOR_EACH_PHI_OR_STMT_DEF (def_p
, stmt
, op_iter
, SSA_OP_DEF
)
7228 FOR_EACH_IMM_USE_STMT (ustmt
, imm_iter
, DEF_FROM_PTR (def_p
))
7232 if (!is_gimple_debug (ustmt
))
7235 bb
= gimple_bb (ustmt
);
7237 if (!flow_bb_inside_loop_p (loop
, bb
))
7239 if (gimple_debug_bind_p (ustmt
))
7241 if (dump_enabled_p ())
7242 dump_printf_loc (MSG_NOTE
, vect_location
,
7243 "killing debug use\n");
7245 gimple_debug_bind_reset_value (ustmt
);
7246 update_stmt (ustmt
);
7255 /* Given loop represented by LOOP_VINFO, return true if computation of
7256 LOOP_VINFO_NITERS (= LOOP_VINFO_NITERSM1 + 1) doesn't overflow, false
7260 loop_niters_no_overflow (loop_vec_info loop_vinfo
)
7262 /* Constant case. */
7263 if (LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo
))
7265 tree cst_niters
= LOOP_VINFO_NITERS (loop_vinfo
);
7266 tree cst_nitersm1
= LOOP_VINFO_NITERSM1 (loop_vinfo
);
7268 gcc_assert (TREE_CODE (cst_niters
) == INTEGER_CST
);
7269 gcc_assert (TREE_CODE (cst_nitersm1
) == INTEGER_CST
);
7270 if (wi::to_widest (cst_nitersm1
) < wi::to_widest (cst_niters
))
7275 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
7276 /* Check the upper bound of loop niters. */
7277 if (get_max_loop_iterations (loop
, &max
))
7279 tree type
= TREE_TYPE (LOOP_VINFO_NITERS (loop_vinfo
));
7280 signop sgn
= TYPE_SIGN (type
);
7281 widest_int type_max
= widest_int::from (wi::max_value (type
), sgn
);
7288 /* Scale profiling counters by estimation for LOOP which is vectorized
7292 scale_profile_for_vect_loop (struct loop
*loop
, unsigned vf
)
7294 edge preheader
= loop_preheader_edge (loop
);
7295 /* Reduce loop iterations by the vectorization factor. */
7296 gcov_type new_est_niter
= niter_for_unrolled_loop (loop
, vf
);
7297 profile_count freq_h
= loop
->header
->count
, freq_e
= preheader
->count ();
7299 if (freq_h
.nonzero_p ())
7301 profile_probability p
;
7303 /* Avoid dropping loop body profile counter to 0 because of zero count
7304 in loop's preheader. */
7305 if (!(freq_e
== profile_count::zero ()))
7306 freq_e
= freq_e
.force_nonzero ();
7307 p
= freq_e
.apply_scale (new_est_niter
+ 1, 1).probability_in (freq_h
);
7308 scale_loop_frequencies (loop
, p
);
7311 edge exit_e
= single_exit (loop
);
7312 exit_e
->probability
= profile_probability::always ()
7313 .apply_scale (1, new_est_niter
+ 1);
7315 edge exit_l
= single_pred_edge (loop
->latch
);
7316 profile_probability prob
= exit_l
->probability
;
7317 exit_l
->probability
= exit_e
->probability
.invert ();
7318 if (prob
.initialized_p () && exit_l
->probability
.initialized_p ())
7319 scale_bbs_frequencies (&loop
->latch
, 1, exit_l
->probability
/ prob
);
7322 /* Function vect_transform_loop.
7324 The analysis phase has determined that the loop is vectorizable.
7325 Vectorize the loop - created vectorized stmts to replace the scalar
7326 stmts in the loop, and update the loop exit condition.
7327 Returns scalar epilogue loop if any. */
7330 vect_transform_loop (loop_vec_info loop_vinfo
)
7332 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
7333 struct loop
*epilogue
= NULL
;
7334 basic_block
*bbs
= LOOP_VINFO_BBS (loop_vinfo
);
7335 int nbbs
= loop
->num_nodes
;
7337 tree niters_vector
= NULL
;
7338 int vf
= LOOP_VINFO_VECT_FACTOR (loop_vinfo
);
7340 bool slp_scheduled
= false;
7341 gimple
*stmt
, *pattern_stmt
;
7342 gimple_seq pattern_def_seq
= NULL
;
7343 gimple_stmt_iterator pattern_def_si
= gsi_none ();
7344 bool transform_pattern_stmt
= false;
7345 bool check_profitability
= false;
7348 if (dump_enabled_p ())
7349 dump_printf_loc (MSG_NOTE
, vect_location
, "=== vec_transform_loop ===\n");
7351 /* Use the more conservative vectorization threshold. If the number
7352 of iterations is constant assume the cost check has been performed
7353 by our caller. If the threshold makes all loops profitable that
7354 run at least the vectorization factor number of times checking
7355 is pointless, too. */
7356 th
= LOOP_VINFO_COST_MODEL_THRESHOLD (loop_vinfo
);
7357 if (th
>= LOOP_VINFO_VECT_FACTOR (loop_vinfo
)
7358 && !LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo
))
7360 if (dump_enabled_p ())
7361 dump_printf_loc (MSG_NOTE
, vect_location
,
7362 "Profitability threshold is %d loop iterations.\n",
7364 check_profitability
= true;
7367 /* Make sure there exists a single-predecessor exit bb. Do this before
7369 edge e
= single_exit (loop
);
7370 if (! single_pred_p (e
->dest
))
7372 split_loop_exit_edge (e
);
7373 if (dump_enabled_p ())
7374 dump_printf (MSG_NOTE
, "split exit edge\n");
7377 /* Version the loop first, if required, so the profitability check
7380 if (LOOP_REQUIRES_VERSIONING (loop_vinfo
))
7382 poly_uint64 versioning_threshold
7383 = LOOP_VINFO_VERSIONING_THRESHOLD (loop_vinfo
);
7384 if (check_profitability
7385 && ordered_p (poly_uint64 (th
), versioning_threshold
))
7387 versioning_threshold
= ordered_max (poly_uint64 (th
),
7388 versioning_threshold
);
7389 check_profitability
= false;
7391 vect_loop_versioning (loop_vinfo
, th
, check_profitability
,
7392 versioning_threshold
);
7393 check_profitability
= false;
7396 /* Make sure there exists a single-predecessor exit bb also on the
7397 scalar loop copy. Do this after versioning but before peeling
7398 so CFG structure is fine for both scalar and if-converted loop
7399 to make slpeel_duplicate_current_defs_from_edges face matched
7400 loop closed PHI nodes on the exit. */
7401 if (LOOP_VINFO_SCALAR_LOOP (loop_vinfo
))
7403 e
= single_exit (LOOP_VINFO_SCALAR_LOOP (loop_vinfo
));
7404 if (! single_pred_p (e
->dest
))
7406 split_loop_exit_edge (e
);
7407 if (dump_enabled_p ())
7408 dump_printf (MSG_NOTE
, "split exit edge of scalar loop\n");
7412 tree niters
= vect_build_loop_niters (loop_vinfo
);
7413 LOOP_VINFO_NITERS_UNCHANGED (loop_vinfo
) = niters
;
7414 tree nitersm1
= unshare_expr (LOOP_VINFO_NITERSM1 (loop_vinfo
));
7415 bool niters_no_overflow
= loop_niters_no_overflow (loop_vinfo
);
7416 epilogue
= vect_do_peeling (loop_vinfo
, niters
, nitersm1
, &niters_vector
, th
,
7417 check_profitability
, niters_no_overflow
);
7418 if (niters_vector
== NULL_TREE
)
7420 if (LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo
))
7422 = build_int_cst (TREE_TYPE (LOOP_VINFO_NITERS (loop_vinfo
)),
7423 LOOP_VINFO_INT_NITERS (loop_vinfo
) / vf
);
7425 vect_gen_vector_loop_niters (loop_vinfo
, niters
, &niters_vector
,
7426 niters_no_overflow
);
7429 /* 1) Make sure the loop header has exactly two entries
7430 2) Make sure we have a preheader basic block. */
7432 gcc_assert (EDGE_COUNT (loop
->header
->preds
) == 2);
7434 split_edge (loop_preheader_edge (loop
));
7436 /* FORNOW: the vectorizer supports only loops which body consist
7437 of one basic block (header + empty latch). When the vectorizer will
7438 support more involved loop forms, the order by which the BBs are
7439 traversed need to be reconsidered. */
7441 for (i
= 0; i
< nbbs
; i
++)
7443 basic_block bb
= bbs
[i
];
7444 stmt_vec_info stmt_info
;
7446 for (gphi_iterator si
= gsi_start_phis (bb
); !gsi_end_p (si
);
7449 gphi
*phi
= si
.phi ();
7450 if (dump_enabled_p ())
7452 dump_printf_loc (MSG_NOTE
, vect_location
,
7453 "------>vectorizing phi: ");
7454 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
, phi
, 0);
7456 stmt_info
= vinfo_for_stmt (phi
);
7460 if (MAY_HAVE_DEBUG_BIND_STMTS
&& !STMT_VINFO_LIVE_P (stmt_info
))
7461 vect_loop_kill_debug_uses (loop
, phi
);
7463 if (!STMT_VINFO_RELEVANT_P (stmt_info
)
7464 && !STMT_VINFO_LIVE_P (stmt_info
))
7467 if (STMT_VINFO_VECTYPE (stmt_info
)
7468 && (TYPE_VECTOR_SUBPARTS (STMT_VINFO_VECTYPE (stmt_info
))
7469 != (unsigned HOST_WIDE_INT
) vf
)
7470 && dump_enabled_p ())
7471 dump_printf_loc (MSG_NOTE
, vect_location
, "multiple-types.\n");
7473 if ((STMT_VINFO_DEF_TYPE (stmt_info
) == vect_induction_def
7474 || STMT_VINFO_DEF_TYPE (stmt_info
) == vect_reduction_def
7475 || STMT_VINFO_DEF_TYPE (stmt_info
) == vect_nested_cycle
)
7476 && ! PURE_SLP_STMT (stmt_info
))
7478 if (dump_enabled_p ())
7479 dump_printf_loc (MSG_NOTE
, vect_location
, "transform phi.\n");
7480 vect_transform_stmt (phi
, NULL
, NULL
, NULL
, NULL
);
7484 pattern_stmt
= NULL
;
7485 for (gimple_stmt_iterator si
= gsi_start_bb (bb
);
7486 !gsi_end_p (si
) || transform_pattern_stmt
;)
7490 if (transform_pattern_stmt
)
7491 stmt
= pattern_stmt
;
7494 stmt
= gsi_stmt (si
);
7495 /* During vectorization remove existing clobber stmts. */
7496 if (gimple_clobber_p (stmt
))
7498 unlink_stmt_vdef (stmt
);
7499 gsi_remove (&si
, true);
7500 release_defs (stmt
);
7505 if (dump_enabled_p ())
7507 dump_printf_loc (MSG_NOTE
, vect_location
,
7508 "------>vectorizing statement: ");
7509 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
, stmt
, 0);
7512 stmt_info
= vinfo_for_stmt (stmt
);
7514 /* vector stmts created in the outer-loop during vectorization of
7515 stmts in an inner-loop may not have a stmt_info, and do not
7516 need to be vectorized. */
7523 if (MAY_HAVE_DEBUG_BIND_STMTS
&& !STMT_VINFO_LIVE_P (stmt_info
))
7524 vect_loop_kill_debug_uses (loop
, stmt
);
7526 if (!STMT_VINFO_RELEVANT_P (stmt_info
)
7527 && !STMT_VINFO_LIVE_P (stmt_info
))
7529 if (STMT_VINFO_IN_PATTERN_P (stmt_info
)
7530 && (pattern_stmt
= STMT_VINFO_RELATED_STMT (stmt_info
))
7531 && (STMT_VINFO_RELEVANT_P (vinfo_for_stmt (pattern_stmt
))
7532 || STMT_VINFO_LIVE_P (vinfo_for_stmt (pattern_stmt
))))
7534 stmt
= pattern_stmt
;
7535 stmt_info
= vinfo_for_stmt (stmt
);
7543 else if (STMT_VINFO_IN_PATTERN_P (stmt_info
)
7544 && (pattern_stmt
= STMT_VINFO_RELATED_STMT (stmt_info
))
7545 && (STMT_VINFO_RELEVANT_P (vinfo_for_stmt (pattern_stmt
))
7546 || STMT_VINFO_LIVE_P (vinfo_for_stmt (pattern_stmt
))))
7547 transform_pattern_stmt
= true;
7549 /* If pattern statement has def stmts, vectorize them too. */
7550 if (is_pattern_stmt_p (stmt_info
))
7552 if (pattern_def_seq
== NULL
)
7554 pattern_def_seq
= STMT_VINFO_PATTERN_DEF_SEQ (stmt_info
);
7555 pattern_def_si
= gsi_start (pattern_def_seq
);
7557 else if (!gsi_end_p (pattern_def_si
))
7558 gsi_next (&pattern_def_si
);
7559 if (pattern_def_seq
!= NULL
)
7561 gimple
*pattern_def_stmt
= NULL
;
7562 stmt_vec_info pattern_def_stmt_info
= NULL
;
7564 while (!gsi_end_p (pattern_def_si
))
7566 pattern_def_stmt
= gsi_stmt (pattern_def_si
);
7567 pattern_def_stmt_info
7568 = vinfo_for_stmt (pattern_def_stmt
);
7569 if (STMT_VINFO_RELEVANT_P (pattern_def_stmt_info
)
7570 || STMT_VINFO_LIVE_P (pattern_def_stmt_info
))
7572 gsi_next (&pattern_def_si
);
7575 if (!gsi_end_p (pattern_def_si
))
7577 if (dump_enabled_p ())
7579 dump_printf_loc (MSG_NOTE
, vect_location
,
7580 "==> vectorizing pattern def "
7582 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
,
7583 pattern_def_stmt
, 0);
7586 stmt
= pattern_def_stmt
;
7587 stmt_info
= pattern_def_stmt_info
;
7591 pattern_def_si
= gsi_none ();
7592 transform_pattern_stmt
= false;
7596 transform_pattern_stmt
= false;
7599 if (STMT_VINFO_VECTYPE (stmt_info
))
7603 TYPE_VECTOR_SUBPARTS (STMT_VINFO_VECTYPE (stmt_info
));
7604 if (!STMT_SLP_TYPE (stmt_info
)
7605 && nunits
!= (unsigned int) vf
7606 && dump_enabled_p ())
7607 /* For SLP VF is set according to unrolling factor, and not
7608 to vector size, hence for SLP this print is not valid. */
7609 dump_printf_loc (MSG_NOTE
, vect_location
, "multiple-types.\n");
7612 /* SLP. Schedule all the SLP instances when the first SLP stmt is
7614 if (STMT_SLP_TYPE (stmt_info
))
7618 slp_scheduled
= true;
7620 if (dump_enabled_p ())
7621 dump_printf_loc (MSG_NOTE
, vect_location
,
7622 "=== scheduling SLP instances ===\n");
7624 vect_schedule_slp (loop_vinfo
);
7627 /* Hybrid SLP stmts must be vectorized in addition to SLP. */
7628 if (!vinfo_for_stmt (stmt
) || PURE_SLP_STMT (stmt_info
))
7630 if (!transform_pattern_stmt
&& gsi_end_p (pattern_def_si
))
7632 pattern_def_seq
= NULL
;
7639 /* -------- vectorize statement ------------ */
7640 if (dump_enabled_p ())
7641 dump_printf_loc (MSG_NOTE
, vect_location
, "transform statement.\n");
7643 grouped_store
= false;
7644 is_store
= vect_transform_stmt (stmt
, &si
, &grouped_store
, NULL
, NULL
);
7647 if (STMT_VINFO_GROUPED_ACCESS (stmt_info
))
7649 /* Interleaving. If IS_STORE is TRUE, the vectorization of the
7650 interleaving chain was completed - free all the stores in
7653 vect_remove_stores (GROUP_FIRST_ELEMENT (stmt_info
));
7657 /* Free the attached stmt_vec_info and remove the stmt. */
7658 gimple
*store
= gsi_stmt (si
);
7659 free_stmt_vec_info (store
);
7660 unlink_stmt_vdef (store
);
7661 gsi_remove (&si
, true);
7662 release_defs (store
);
7665 /* Stores can only appear at the end of pattern statements. */
7666 gcc_assert (!transform_pattern_stmt
);
7667 pattern_def_seq
= NULL
;
7669 else if (!transform_pattern_stmt
&& gsi_end_p (pattern_def_si
))
7671 pattern_def_seq
= NULL
;
7677 slpeel_make_loop_iterate_ntimes (loop
, niters_vector
);
7679 scale_profile_for_vect_loop (loop
, vf
);
7681 /* The minimum number of iterations performed by the epilogue. This
7682 is 1 when peeling for gaps because we always need a final scalar
7684 int min_epilogue_iters
= LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo
) ? 1 : 0;
7685 /* +1 to convert latch counts to loop iteration counts,
7686 -min_epilogue_iters to remove iterations that cannot be performed
7687 by the vector code. */
7688 int bias
= 1 - min_epilogue_iters
;
7689 /* In these calculations the "- 1" converts loop iteration counts
7690 back to latch counts. */
7691 if (loop
->any_upper_bound
)
7692 loop
->nb_iterations_upper_bound
7693 = wi::udiv_floor (loop
->nb_iterations_upper_bound
+ bias
, vf
) - 1;
7694 if (loop
->any_likely_upper_bound
)
7695 loop
->nb_iterations_likely_upper_bound
7696 = wi::udiv_floor (loop
->nb_iterations_likely_upper_bound
+ bias
, vf
) - 1;
7697 if (loop
->any_estimate
)
7698 loop
->nb_iterations_estimate
7699 = wi::udiv_floor (loop
->nb_iterations_estimate
+ bias
, vf
) - 1;
7701 if (dump_enabled_p ())
7703 if (!LOOP_VINFO_EPILOGUE_P (loop_vinfo
))
7705 dump_printf_loc (MSG_NOTE
, vect_location
,
7706 "LOOP VECTORIZED\n");
7708 dump_printf_loc (MSG_NOTE
, vect_location
,
7709 "OUTER LOOP VECTORIZED\n");
7710 dump_printf (MSG_NOTE
, "\n");
7713 dump_printf_loc (MSG_NOTE
, vect_location
,
7714 "LOOP EPILOGUE VECTORIZED (VS=%d)\n",
7715 current_vector_size
);
7718 /* Free SLP instances here because otherwise stmt reference counting
7720 slp_instance instance
;
7721 FOR_EACH_VEC_ELT (LOOP_VINFO_SLP_INSTANCES (loop_vinfo
), i
, instance
)
7722 vect_free_slp_instance (instance
);
7723 LOOP_VINFO_SLP_INSTANCES (loop_vinfo
).release ();
7724 /* Clear-up safelen field since its value is invalid after vectorization
7725 since vectorized loop can have loop-carried dependencies. */
7728 /* Don't vectorize epilogue for epilogue. */
7729 if (LOOP_VINFO_EPILOGUE_P (loop_vinfo
))
7734 unsigned int vector_sizes
7735 = targetm
.vectorize
.autovectorize_vector_sizes ();
7736 vector_sizes
&= current_vector_size
- 1;
7738 if (!PARAM_VALUE (PARAM_VECT_EPILOGUES_NOMASK
))
7740 else if (!vector_sizes
)
7742 else if (LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo
)
7743 && LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo
) >= 0)
7745 int smallest_vec_size
= 1 << ctz_hwi (vector_sizes
);
7746 int ratio
= current_vector_size
/ smallest_vec_size
;
7747 int eiters
= LOOP_VINFO_INT_NITERS (loop_vinfo
)
7748 - LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo
);
7749 eiters
= eiters
% vf
;
7751 epilogue
->nb_iterations_upper_bound
= eiters
- 1;
7753 if (eiters
< vf
/ ratio
)
7760 epilogue
->force_vectorize
= loop
->force_vectorize
;
7761 epilogue
->safelen
= loop
->safelen
;
7762 epilogue
->dont_vectorize
= false;
7764 /* We may need to if-convert epilogue to vectorize it. */
7765 if (LOOP_VINFO_SCALAR_LOOP (loop_vinfo
))
7766 tree_if_conversion (epilogue
);
7772 /* The code below is trying to perform simple optimization - revert
7773 if-conversion for masked stores, i.e. if the mask of a store is zero
7774 do not perform it and all stored value producers also if possible.
7782 this transformation will produce the following semi-hammock:
7784 if (!mask__ifc__42.18_165 == { 0, 0, 0, 0, 0, 0, 0, 0 })
7786 vect__11.19_170 = MASK_LOAD (vectp_p1.20_168, 0B, mask__ifc__42.18_165);
7787 vect__12.22_172 = vect__11.19_170 + vect_cst__171;
7788 MASK_STORE (vectp_p1.23_175, 0B, mask__ifc__42.18_165, vect__12.22_172);
7789 vect__18.25_182 = MASK_LOAD (vectp_p3.26_180, 0B, mask__ifc__42.18_165);
7790 vect__19.28_184 = vect__18.25_182 + vect_cst__183;
7791 MASK_STORE (vectp_p2.29_187, 0B, mask__ifc__42.18_165, vect__19.28_184);
7796 optimize_mask_stores (struct loop
*loop
)
7798 basic_block
*bbs
= get_loop_body (loop
);
7799 unsigned nbbs
= loop
->num_nodes
;
7802 struct loop
*bb_loop
;
7803 gimple_stmt_iterator gsi
;
7805 auto_vec
<gimple
*> worklist
;
7807 vect_location
= find_loop_location (loop
);
7808 /* Pick up all masked stores in loop if any. */
7809 for (i
= 0; i
< nbbs
; i
++)
7812 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
);
7815 stmt
= gsi_stmt (gsi
);
7816 if (gimple_call_internal_p (stmt
, IFN_MASK_STORE
))
7817 worklist
.safe_push (stmt
);
7822 if (worklist
.is_empty ())
7825 /* Loop has masked stores. */
7826 while (!worklist
.is_empty ())
7828 gimple
*last
, *last_store
;
7831 basic_block store_bb
, join_bb
;
7832 gimple_stmt_iterator gsi_to
;
7833 tree vdef
, new_vdef
;
7838 last
= worklist
.pop ();
7839 mask
= gimple_call_arg (last
, 2);
7840 bb
= gimple_bb (last
);
7841 /* Create then_bb and if-then structure in CFG, then_bb belongs to
7842 the same loop as if_bb. It could be different to LOOP when two
7843 level loop-nest is vectorized and mask_store belongs to the inner
7845 e
= split_block (bb
, last
);
7846 bb_loop
= bb
->loop_father
;
7847 gcc_assert (loop
== bb_loop
|| flow_loop_nested_p (loop
, bb_loop
));
7849 store_bb
= create_empty_bb (bb
);
7850 add_bb_to_loop (store_bb
, bb_loop
);
7851 e
->flags
= EDGE_TRUE_VALUE
;
7852 efalse
= make_edge (bb
, store_bb
, EDGE_FALSE_VALUE
);
7853 /* Put STORE_BB to likely part. */
7854 efalse
->probability
= profile_probability::unlikely ();
7855 store_bb
->count
= efalse
->count ();
7856 make_single_succ_edge (store_bb
, join_bb
, EDGE_FALLTHRU
);
7857 if (dom_info_available_p (CDI_DOMINATORS
))
7858 set_immediate_dominator (CDI_DOMINATORS
, store_bb
, bb
);
7859 if (dump_enabled_p ())
7860 dump_printf_loc (MSG_NOTE
, vect_location
,
7861 "Create new block %d to sink mask stores.",
7863 /* Create vector comparison with boolean result. */
7864 vectype
= TREE_TYPE (mask
);
7865 zero
= build_zero_cst (vectype
);
7866 stmt
= gimple_build_cond (EQ_EXPR
, mask
, zero
, NULL_TREE
, NULL_TREE
);
7867 gsi
= gsi_last_bb (bb
);
7868 gsi_insert_after (&gsi
, stmt
, GSI_SAME_STMT
);
7869 /* Create new PHI node for vdef of the last masked store:
7870 .MEM_2 = VDEF <.MEM_1>
7871 will be converted to
7872 .MEM.3 = VDEF <.MEM_1>
7873 and new PHI node will be created in join bb
7874 .MEM_2 = PHI <.MEM_1, .MEM_3>
7876 vdef
= gimple_vdef (last
);
7877 new_vdef
= make_ssa_name (gimple_vop (cfun
), last
);
7878 gimple_set_vdef (last
, new_vdef
);
7879 phi
= create_phi_node (vdef
, join_bb
);
7880 add_phi_arg (phi
, new_vdef
, EDGE_SUCC (store_bb
, 0), UNKNOWN_LOCATION
);
7882 /* Put all masked stores with the same mask to STORE_BB if possible. */
7885 gimple_stmt_iterator gsi_from
;
7886 gimple
*stmt1
= NULL
;
7888 /* Move masked store to STORE_BB. */
7890 gsi
= gsi_for_stmt (last
);
7892 /* Shift GSI to the previous stmt for further traversal. */
7894 gsi_to
= gsi_start_bb (store_bb
);
7895 gsi_move_before (&gsi_from
, &gsi_to
);
7896 /* Setup GSI_TO to the non-empty block start. */
7897 gsi_to
= gsi_start_bb (store_bb
);
7898 if (dump_enabled_p ())
7900 dump_printf_loc (MSG_NOTE
, vect_location
,
7901 "Move stmt to created bb\n");
7902 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
, last
, 0);
7904 /* Move all stored value producers if possible. */
7905 while (!gsi_end_p (gsi
))
7908 imm_use_iterator imm_iter
;
7909 use_operand_p use_p
;
7912 /* Skip debug statements. */
7913 if (is_gimple_debug (gsi_stmt (gsi
)))
7918 stmt1
= gsi_stmt (gsi
);
7919 /* Do not consider statements writing to memory or having
7920 volatile operand. */
7921 if (gimple_vdef (stmt1
)
7922 || gimple_has_volatile_ops (stmt1
))
7926 lhs
= gimple_get_lhs (stmt1
);
7930 /* LHS of vectorized stmt must be SSA_NAME. */
7931 if (TREE_CODE (lhs
) != SSA_NAME
)
7934 if (!VECTOR_TYPE_P (TREE_TYPE (lhs
)))
7936 /* Remove dead scalar statement. */
7937 if (has_zero_uses (lhs
))
7939 gsi_remove (&gsi_from
, true);
7944 /* Check that LHS does not have uses outside of STORE_BB. */
7946 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, lhs
)
7949 use_stmt
= USE_STMT (use_p
);
7950 if (is_gimple_debug (use_stmt
))
7952 if (gimple_bb (use_stmt
) != store_bb
)
7961 if (gimple_vuse (stmt1
)
7962 && gimple_vuse (stmt1
) != gimple_vuse (last_store
))
7965 /* Can move STMT1 to STORE_BB. */
7966 if (dump_enabled_p ())
7968 dump_printf_loc (MSG_NOTE
, vect_location
,
7969 "Move stmt to created bb\n");
7970 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
, stmt1
, 0);
7972 gsi_move_before (&gsi_from
, &gsi_to
);
7973 /* Shift GSI_TO for further insertion. */
7976 /* Put other masked stores with the same mask to STORE_BB. */
7977 if (worklist
.is_empty ()
7978 || gimple_call_arg (worklist
.last (), 2) != mask
7979 || worklist
.last () != stmt1
)
7981 last
= worklist
.pop ();
7983 add_phi_arg (phi
, gimple_vuse (last_store
), e
, UNKNOWN_LOCATION
);