2 Copyright (C) 2003-2015 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"
46 #include "tree-scalar-evolution.h"
47 #include "tree-vectorizer.h"
48 #include "gimple-fold.h"
51 /* Loop Vectorization Pass.
53 This pass tries to vectorize loops.
55 For example, the vectorizer transforms the following simple loop:
57 short a[N]; short b[N]; short c[N]; int i;
63 as if it was manually vectorized by rewriting the source code into:
65 typedef int __attribute__((mode(V8HI))) v8hi;
66 short a[N]; short b[N]; short c[N]; int i;
67 v8hi *pa = (v8hi*)a, *pb = (v8hi*)b, *pc = (v8hi*)c;
70 for (i=0; i<N/8; i++){
77 The main entry to this pass is vectorize_loops(), in which
78 the vectorizer applies a set of analyses on a given set of loops,
79 followed by the actual vectorization transformation for the loops that
80 had successfully passed the analysis phase.
81 Throughout this pass we make a distinction between two types of
82 data: scalars (which are represented by SSA_NAMES), and memory references
83 ("data-refs"). These two types of data require different handling both
84 during analysis and transformation. The types of data-refs that the
85 vectorizer currently supports are ARRAY_REFS which base is an array DECL
86 (not a pointer), and INDIRECT_REFS through pointers; both array and pointer
87 accesses are required to have a simple (consecutive) access pattern.
91 The driver for the analysis phase is vect_analyze_loop().
92 It applies a set of analyses, some of which rely on the scalar evolution
93 analyzer (scev) developed by Sebastian Pop.
95 During the analysis phase the vectorizer records some information
96 per stmt in a "stmt_vec_info" struct which is attached to each stmt in the
97 loop, as well as general information about the loop as a whole, which is
98 recorded in a "loop_vec_info" struct attached to each loop.
100 Transformation phase:
101 =====================
102 The loop transformation phase scans all the stmts in the loop, and
103 creates a vector stmt (or a sequence of stmts) for each scalar stmt S in
104 the loop that needs to be vectorized. It inserts the vector code sequence
105 just before the scalar stmt S, and records a pointer to the vector code
106 in STMT_VINFO_VEC_STMT (stmt_info) (stmt_info is the stmt_vec_info struct
107 attached to S). This pointer will be used for the vectorization of following
108 stmts which use the def of stmt S. Stmt S is removed if it writes to memory;
109 otherwise, we rely on dead code elimination for removing it.
111 For example, say stmt S1 was vectorized into stmt VS1:
114 S1: b = x[i]; STMT_VINFO_VEC_STMT (stmt_info (S1)) = VS1
117 To vectorize stmt S2, the vectorizer first finds the stmt that defines
118 the operand 'b' (S1), and gets the relevant vector def 'vb' from the
119 vector stmt VS1 pointed to by STMT_VINFO_VEC_STMT (stmt_info (S1)). The
120 resulting sequence would be:
123 S1: b = x[i]; STMT_VINFO_VEC_STMT (stmt_info (S1)) = VS1
125 S2: a = b; STMT_VINFO_VEC_STMT (stmt_info (S2)) = VS2
127 Operands that are not SSA_NAMEs, are data-refs that appear in
128 load/store operations (like 'x[i]' in S1), and are handled differently.
132 Currently the only target specific information that is used is the
133 size of the vector (in bytes) - "TARGET_VECTORIZE_UNITS_PER_SIMD_WORD".
134 Targets that can support different sizes of vectors, for now will need
135 to specify one value for "TARGET_VECTORIZE_UNITS_PER_SIMD_WORD". More
136 flexibility will be added in the future.
138 Since we only vectorize operations which vector form can be
139 expressed using existing tree codes, to verify that an operation is
140 supported, the vectorizer checks the relevant optab at the relevant
141 machine_mode (e.g, optab_handler (add_optab, V8HImode)). If
142 the value found is CODE_FOR_nothing, then there's no target support, and
143 we can't vectorize the stmt.
145 For additional information on this project see:
146 http://gcc.gnu.org/projects/tree-ssa/vectorization.html
149 static void vect_estimate_min_profitable_iters (loop_vec_info
, int *, int *);
151 /* Function vect_determine_vectorization_factor
153 Determine the vectorization factor (VF). VF is the number of data elements
154 that are operated upon in parallel in a single iteration of the vectorized
155 loop. For example, when vectorizing a loop that operates on 4byte elements,
156 on a target with vector size (VS) 16byte, the VF is set to 4, since 4
157 elements can fit in a single vector register.
159 We currently support vectorization of loops in which all types operated upon
160 are of the same size. Therefore this function currently sets VF according to
161 the size of the types operated upon, and fails if there are multiple sizes
164 VF is also the factor by which the loop iterations are strip-mined, e.g.:
171 for (i=0; i<N; i+=VF){
172 a[i:VF] = b[i:VF] + c[i:VF];
177 vect_determine_vectorization_factor (loop_vec_info loop_vinfo
)
179 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
180 basic_block
*bbs
= LOOP_VINFO_BBS (loop_vinfo
);
181 unsigned nbbs
= loop
->num_nodes
;
182 unsigned int vectorization_factor
= 0;
187 stmt_vec_info stmt_info
;
190 gimple
*stmt
, *pattern_stmt
= NULL
;
191 gimple_seq pattern_def_seq
= NULL
;
192 gimple_stmt_iterator pattern_def_si
= gsi_none ();
193 bool analyze_pattern_stmt
= false;
195 auto_vec
<stmt_vec_info
> mask_producers
;
197 if (dump_enabled_p ())
198 dump_printf_loc (MSG_NOTE
, vect_location
,
199 "=== vect_determine_vectorization_factor ===\n");
201 for (i
= 0; i
< nbbs
; i
++)
203 basic_block bb
= bbs
[i
];
205 for (gphi_iterator si
= gsi_start_phis (bb
); !gsi_end_p (si
);
209 stmt_info
= vinfo_for_stmt (phi
);
210 if (dump_enabled_p ())
212 dump_printf_loc (MSG_NOTE
, vect_location
, "==> examining phi: ");
213 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
, phi
, 0);
214 dump_printf (MSG_NOTE
, "\n");
217 gcc_assert (stmt_info
);
219 if (STMT_VINFO_RELEVANT_P (stmt_info
))
221 gcc_assert (!STMT_VINFO_VECTYPE (stmt_info
));
222 scalar_type
= TREE_TYPE (PHI_RESULT (phi
));
224 if (dump_enabled_p ())
226 dump_printf_loc (MSG_NOTE
, vect_location
,
227 "get vectype for scalar type: ");
228 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, scalar_type
);
229 dump_printf (MSG_NOTE
, "\n");
232 vectype
= get_vectype_for_scalar_type (scalar_type
);
235 if (dump_enabled_p ())
237 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
238 "not vectorized: unsupported "
240 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
,
242 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
246 STMT_VINFO_VECTYPE (stmt_info
) = vectype
;
248 if (dump_enabled_p ())
250 dump_printf_loc (MSG_NOTE
, vect_location
, "vectype: ");
251 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, vectype
);
252 dump_printf (MSG_NOTE
, "\n");
255 nunits
= TYPE_VECTOR_SUBPARTS (vectype
);
256 if (dump_enabled_p ())
257 dump_printf_loc (MSG_NOTE
, vect_location
, "nunits = %d\n",
260 if (!vectorization_factor
261 || (nunits
> vectorization_factor
))
262 vectorization_factor
= nunits
;
266 for (gimple_stmt_iterator si
= gsi_start_bb (bb
);
267 !gsi_end_p (si
) || analyze_pattern_stmt
;)
271 if (analyze_pattern_stmt
)
274 stmt
= gsi_stmt (si
);
276 stmt_info
= vinfo_for_stmt (stmt
);
278 if (dump_enabled_p ())
280 dump_printf_loc (MSG_NOTE
, vect_location
,
281 "==> examining statement: ");
282 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
, stmt
, 0);
283 dump_printf (MSG_NOTE
, "\n");
286 gcc_assert (stmt_info
);
288 /* Skip stmts which do not need to be vectorized. */
289 if ((!STMT_VINFO_RELEVANT_P (stmt_info
)
290 && !STMT_VINFO_LIVE_P (stmt_info
))
291 || gimple_clobber_p (stmt
))
293 if (STMT_VINFO_IN_PATTERN_P (stmt_info
)
294 && (pattern_stmt
= STMT_VINFO_RELATED_STMT (stmt_info
))
295 && (STMT_VINFO_RELEVANT_P (vinfo_for_stmt (pattern_stmt
))
296 || STMT_VINFO_LIVE_P (vinfo_for_stmt (pattern_stmt
))))
299 stmt_info
= vinfo_for_stmt (pattern_stmt
);
300 if (dump_enabled_p ())
302 dump_printf_loc (MSG_NOTE
, vect_location
,
303 "==> examining pattern statement: ");
304 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
, stmt
, 0);
305 dump_printf (MSG_NOTE
, "\n");
310 if (dump_enabled_p ())
311 dump_printf_loc (MSG_NOTE
, vect_location
, "skip.\n");
316 else if (STMT_VINFO_IN_PATTERN_P (stmt_info
)
317 && (pattern_stmt
= STMT_VINFO_RELATED_STMT (stmt_info
))
318 && (STMT_VINFO_RELEVANT_P (vinfo_for_stmt (pattern_stmt
))
319 || STMT_VINFO_LIVE_P (vinfo_for_stmt (pattern_stmt
))))
320 analyze_pattern_stmt
= true;
322 /* If a pattern statement has def stmts, analyze them too. */
323 if (is_pattern_stmt_p (stmt_info
))
325 if (pattern_def_seq
== NULL
)
327 pattern_def_seq
= STMT_VINFO_PATTERN_DEF_SEQ (stmt_info
);
328 pattern_def_si
= gsi_start (pattern_def_seq
);
330 else if (!gsi_end_p (pattern_def_si
))
331 gsi_next (&pattern_def_si
);
332 if (pattern_def_seq
!= NULL
)
334 gimple
*pattern_def_stmt
= NULL
;
335 stmt_vec_info pattern_def_stmt_info
= NULL
;
337 while (!gsi_end_p (pattern_def_si
))
339 pattern_def_stmt
= gsi_stmt (pattern_def_si
);
340 pattern_def_stmt_info
341 = vinfo_for_stmt (pattern_def_stmt
);
342 if (STMT_VINFO_RELEVANT_P (pattern_def_stmt_info
)
343 || STMT_VINFO_LIVE_P (pattern_def_stmt_info
))
345 gsi_next (&pattern_def_si
);
348 if (!gsi_end_p (pattern_def_si
))
350 if (dump_enabled_p ())
352 dump_printf_loc (MSG_NOTE
, vect_location
,
353 "==> examining pattern def stmt: ");
354 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
,
355 pattern_def_stmt
, 0);
356 dump_printf (MSG_NOTE
, "\n");
359 stmt
= pattern_def_stmt
;
360 stmt_info
= pattern_def_stmt_info
;
364 pattern_def_si
= gsi_none ();
365 analyze_pattern_stmt
= false;
369 analyze_pattern_stmt
= false;
372 if (gimple_get_lhs (stmt
) == NULL_TREE
373 /* MASK_STORE has no lhs, but is ok. */
374 && (!is_gimple_call (stmt
)
375 || !gimple_call_internal_p (stmt
)
376 || gimple_call_internal_fn (stmt
) != IFN_MASK_STORE
))
378 if (is_gimple_call (stmt
))
380 /* Ignore calls with no lhs. These must be calls to
381 #pragma omp simd functions, and what vectorization factor
382 it really needs can't be determined until
383 vectorizable_simd_clone_call. */
384 if (!analyze_pattern_stmt
&& gsi_end_p (pattern_def_si
))
386 pattern_def_seq
= NULL
;
391 if (dump_enabled_p ())
393 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
394 "not vectorized: irregular stmt.");
395 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
, stmt
,
397 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
402 if (VECTOR_MODE_P (TYPE_MODE (gimple_expr_type (stmt
))))
404 if (dump_enabled_p ())
406 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
407 "not vectorized: vector stmt in loop:");
408 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
, stmt
, 0);
409 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
416 if (STMT_VINFO_VECTYPE (stmt_info
))
418 /* The only case when a vectype had been already set is for stmts
419 that contain a dataref, or for "pattern-stmts" (stmts
420 generated by the vectorizer to represent/replace a certain
422 gcc_assert (STMT_VINFO_DATA_REF (stmt_info
)
423 || is_pattern_stmt_p (stmt_info
)
424 || !gsi_end_p (pattern_def_si
));
425 vectype
= STMT_VINFO_VECTYPE (stmt_info
);
429 gcc_assert (!STMT_VINFO_DATA_REF (stmt_info
));
430 if (is_gimple_call (stmt
)
431 && gimple_call_internal_p (stmt
)
432 && gimple_call_internal_fn (stmt
) == IFN_MASK_STORE
)
433 scalar_type
= TREE_TYPE (gimple_call_arg (stmt
, 3));
435 scalar_type
= TREE_TYPE (gimple_get_lhs (stmt
));
437 /* Bool ops don't participate in vectorization factor
438 computation. For comparison use compared types to
440 if (TREE_CODE (scalar_type
) == BOOLEAN_TYPE
)
442 mask_producers
.safe_push (stmt_info
);
445 if (gimple_code (stmt
) == GIMPLE_ASSIGN
446 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt
))
448 && TREE_CODE (TREE_TYPE (gimple_assign_rhs1 (stmt
)))
450 scalar_type
= TREE_TYPE (gimple_assign_rhs1 (stmt
));
453 if (!analyze_pattern_stmt
&& gsi_end_p (pattern_def_si
))
455 pattern_def_seq
= NULL
;
462 if (dump_enabled_p ())
464 dump_printf_loc (MSG_NOTE
, vect_location
,
465 "get vectype for scalar type: ");
466 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, scalar_type
);
467 dump_printf (MSG_NOTE
, "\n");
469 vectype
= get_vectype_for_scalar_type (scalar_type
);
472 if (dump_enabled_p ())
474 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
475 "not vectorized: unsupported "
477 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
,
479 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
485 STMT_VINFO_VECTYPE (stmt_info
) = vectype
;
487 if (dump_enabled_p ())
489 dump_printf_loc (MSG_NOTE
, vect_location
, "vectype: ");
490 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, vectype
);
491 dump_printf (MSG_NOTE
, "\n");
495 /* Don't try to compute VF out scalar types if we stmt
496 produces boolean vector. Use result vectype instead. */
497 if (VECTOR_BOOLEAN_TYPE_P (vectype
))
498 vf_vectype
= vectype
;
501 /* The vectorization factor is according to the smallest
502 scalar type (or the largest vector size, but we only
503 support one vector size per loop). */
505 scalar_type
= vect_get_smallest_scalar_type (stmt
, &dummy
,
507 if (dump_enabled_p ())
509 dump_printf_loc (MSG_NOTE
, vect_location
,
510 "get vectype for scalar type: ");
511 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, scalar_type
);
512 dump_printf (MSG_NOTE
, "\n");
514 vf_vectype
= get_vectype_for_scalar_type (scalar_type
);
518 if (dump_enabled_p ())
520 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
521 "not vectorized: unsupported data-type ");
522 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
,
524 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
529 if ((GET_MODE_SIZE (TYPE_MODE (vectype
))
530 != GET_MODE_SIZE (TYPE_MODE (vf_vectype
))))
532 if (dump_enabled_p ())
534 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
535 "not vectorized: different sized vector "
536 "types in statement, ");
537 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
,
539 dump_printf (MSG_MISSED_OPTIMIZATION
, " and ");
540 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
,
542 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
547 if (dump_enabled_p ())
549 dump_printf_loc (MSG_NOTE
, vect_location
, "vectype: ");
550 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, vf_vectype
);
551 dump_printf (MSG_NOTE
, "\n");
554 nunits
= TYPE_VECTOR_SUBPARTS (vf_vectype
);
555 if (dump_enabled_p ())
556 dump_printf_loc (MSG_NOTE
, vect_location
, "nunits = %d\n", nunits
);
557 if (!vectorization_factor
558 || (nunits
> vectorization_factor
))
559 vectorization_factor
= nunits
;
561 if (!analyze_pattern_stmt
&& gsi_end_p (pattern_def_si
))
563 pattern_def_seq
= NULL
;
569 /* TODO: Analyze cost. Decide if worth while to vectorize. */
570 if (dump_enabled_p ())
571 dump_printf_loc (MSG_NOTE
, vect_location
, "vectorization factor = %d\n",
572 vectorization_factor
);
573 if (vectorization_factor
<= 1)
575 if (dump_enabled_p ())
576 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
577 "not vectorized: unsupported data-type\n");
580 LOOP_VINFO_VECT_FACTOR (loop_vinfo
) = vectorization_factor
;
582 for (i
= 0; i
< mask_producers
.length (); i
++)
584 tree mask_type
= NULL
;
586 stmt
= STMT_VINFO_STMT (mask_producers
[i
]);
588 if (gimple_code (stmt
) == GIMPLE_ASSIGN
589 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt
)) == tcc_comparison
590 && TREE_CODE (TREE_TYPE (gimple_assign_rhs1 (stmt
))) != BOOLEAN_TYPE
)
592 scalar_type
= TREE_TYPE (gimple_assign_rhs1 (stmt
));
593 mask_type
= get_mask_type_for_scalar_type (scalar_type
);
597 if (dump_enabled_p ())
598 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
599 "not vectorized: unsupported mask\n");
608 enum vect_def_type dt
;
610 FOR_EACH_SSA_TREE_OPERAND (rhs
, stmt
, iter
, SSA_OP_USE
)
612 if (!vect_is_simple_use (rhs
, mask_producers
[i
]->vinfo
,
613 &def_stmt
, &dt
, &vectype
))
615 if (dump_enabled_p ())
617 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
618 "not vectorized: can't compute mask type "
620 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
, stmt
,
622 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
627 /* No vectype probably means external definition.
628 Allow it in case there is another operand which
629 allows to determine mask type. */
635 else if (TYPE_VECTOR_SUBPARTS (mask_type
)
636 != TYPE_VECTOR_SUBPARTS (vectype
))
638 if (dump_enabled_p ())
640 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
641 "not vectorized: different sized masks "
642 "types in statement, ");
643 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
,
645 dump_printf (MSG_MISSED_OPTIMIZATION
, " and ");
646 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
,
648 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
652 else if (VECTOR_BOOLEAN_TYPE_P (mask_type
)
653 != VECTOR_BOOLEAN_TYPE_P (vectype
))
655 if (dump_enabled_p ())
657 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
658 "not vectorized: mixed mask and "
659 "nonmask vector types in statement, ");
660 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
,
662 dump_printf (MSG_MISSED_OPTIMIZATION
, " and ");
663 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
,
665 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
671 /* We may compare boolean value loaded as vector of integers.
672 Fix mask_type in such case. */
674 && !VECTOR_BOOLEAN_TYPE_P (mask_type
)
675 && gimple_code (stmt
) == GIMPLE_ASSIGN
676 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt
)) == tcc_comparison
)
677 mask_type
= build_same_sized_truth_vector_type (mask_type
);
680 /* No mask_type should mean loop invariant predicate.
681 This is probably a subject for optimization in
685 if (dump_enabled_p ())
687 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
688 "not vectorized: can't compute mask type "
690 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
, stmt
,
692 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
697 STMT_VINFO_VECTYPE (mask_producers
[i
]) = mask_type
;
704 /* Function vect_is_simple_iv_evolution.
706 FORNOW: A simple evolution of an induction variables in the loop is
707 considered a polynomial evolution. */
710 vect_is_simple_iv_evolution (unsigned loop_nb
, tree access_fn
, tree
* init
,
715 tree evolution_part
= evolution_part_in_loop_num (access_fn
, loop_nb
);
718 /* When there is no evolution in this loop, the evolution function
720 if (evolution_part
== NULL_TREE
)
723 /* When the evolution is a polynomial of degree >= 2
724 the evolution function is not "simple". */
725 if (tree_is_chrec (evolution_part
))
728 step_expr
= evolution_part
;
729 init_expr
= unshare_expr (initial_condition_in_loop_num (access_fn
, loop_nb
));
731 if (dump_enabled_p ())
733 dump_printf_loc (MSG_NOTE
, vect_location
, "step: ");
734 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, step_expr
);
735 dump_printf (MSG_NOTE
, ", init: ");
736 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, init_expr
);
737 dump_printf (MSG_NOTE
, "\n");
743 if (TREE_CODE (step_expr
) != INTEGER_CST
744 && (TREE_CODE (step_expr
) != SSA_NAME
745 || ((bb
= gimple_bb (SSA_NAME_DEF_STMT (step_expr
)))
746 && flow_bb_inside_loop_p (get_loop (cfun
, loop_nb
), bb
))
747 || (!INTEGRAL_TYPE_P (TREE_TYPE (step_expr
))
748 && (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (step_expr
))
749 || !flag_associative_math
)))
750 && (TREE_CODE (step_expr
) != REAL_CST
751 || !flag_associative_math
))
753 if (dump_enabled_p ())
754 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
762 /* Function vect_analyze_scalar_cycles_1.
764 Examine the cross iteration def-use cycles of scalar variables
765 in LOOP. LOOP_VINFO represents the loop that is now being
766 considered for vectorization (can be LOOP, or an outer-loop
770 vect_analyze_scalar_cycles_1 (loop_vec_info loop_vinfo
, struct loop
*loop
)
772 basic_block bb
= loop
->header
;
774 auto_vec
<gimple
*, 64> worklist
;
778 if (dump_enabled_p ())
779 dump_printf_loc (MSG_NOTE
, vect_location
,
780 "=== vect_analyze_scalar_cycles ===\n");
782 /* First - identify all inductions. Reduction detection assumes that all the
783 inductions have been identified, therefore, this order must not be
785 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
787 gphi
*phi
= gsi
.phi ();
788 tree access_fn
= NULL
;
789 tree def
= PHI_RESULT (phi
);
790 stmt_vec_info stmt_vinfo
= vinfo_for_stmt (phi
);
792 if (dump_enabled_p ())
794 dump_printf_loc (MSG_NOTE
, vect_location
, "Analyze phi: ");
795 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
, phi
, 0);
796 dump_printf (MSG_NOTE
, "\n");
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_PART (stmt_vinfo
)
819 = evolution_part_in_loop_num (access_fn
, loop
->num
);
823 || !vect_is_simple_iv_evolution (loop
->num
, access_fn
, &init
, &step
)
824 || (LOOP_VINFO_LOOP (loop_vinfo
) != loop
825 && TREE_CODE (step
) != INTEGER_CST
))
827 worklist
.safe_push (phi
);
831 gcc_assert (STMT_VINFO_LOOP_PHI_EVOLUTION_PART (stmt_vinfo
) != NULL_TREE
);
833 if (dump_enabled_p ())
834 dump_printf_loc (MSG_NOTE
, vect_location
, "Detected induction.\n");
835 STMT_VINFO_DEF_TYPE (stmt_vinfo
) = vect_induction_def
;
839 /* Second - identify all reductions and nested cycles. */
840 while (worklist
.length () > 0)
842 gimple
*phi
= worklist
.pop ();
843 tree def
= PHI_RESULT (phi
);
844 stmt_vec_info stmt_vinfo
= vinfo_for_stmt (phi
);
848 if (dump_enabled_p ())
850 dump_printf_loc (MSG_NOTE
, vect_location
, "Analyze phi: ");
851 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
, phi
, 0);
852 dump_printf (MSG_NOTE
, "\n");
855 gcc_assert (!virtual_operand_p (def
)
856 && STMT_VINFO_DEF_TYPE (stmt_vinfo
) == vect_unknown_def_type
);
858 nested_cycle
= (loop
!= LOOP_VINFO_LOOP (loop_vinfo
));
859 reduc_stmt
= vect_force_simple_reduction (loop_vinfo
, phi
, !nested_cycle
,
860 &double_reduc
, false);
865 if (dump_enabled_p ())
866 dump_printf_loc (MSG_NOTE
, vect_location
,
867 "Detected double reduction.\n");
869 STMT_VINFO_DEF_TYPE (stmt_vinfo
) = vect_double_reduction_def
;
870 STMT_VINFO_DEF_TYPE (vinfo_for_stmt (reduc_stmt
)) =
871 vect_double_reduction_def
;
877 if (dump_enabled_p ())
878 dump_printf_loc (MSG_NOTE
, vect_location
,
879 "Detected vectorizable nested cycle.\n");
881 STMT_VINFO_DEF_TYPE (stmt_vinfo
) = vect_nested_cycle
;
882 STMT_VINFO_DEF_TYPE (vinfo_for_stmt (reduc_stmt
)) =
887 if (dump_enabled_p ())
888 dump_printf_loc (MSG_NOTE
, vect_location
,
889 "Detected reduction.\n");
891 STMT_VINFO_DEF_TYPE (stmt_vinfo
) = vect_reduction_def
;
892 STMT_VINFO_DEF_TYPE (vinfo_for_stmt (reduc_stmt
)) =
894 /* Store the reduction cycles for possible vectorization in
896 LOOP_VINFO_REDUCTIONS (loop_vinfo
).safe_push (reduc_stmt
);
901 if (dump_enabled_p ())
902 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
903 "Unknown def-use cycle pattern.\n");
908 /* Function vect_analyze_scalar_cycles.
910 Examine the cross iteration def-use cycles of scalar variables, by
911 analyzing the loop-header PHIs of scalar variables. Classify each
912 cycle as one of the following: invariant, induction, reduction, unknown.
913 We do that for the loop represented by LOOP_VINFO, and also to its
914 inner-loop, if exists.
915 Examples for scalar cycles:
930 vect_analyze_scalar_cycles (loop_vec_info loop_vinfo
)
932 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
934 vect_analyze_scalar_cycles_1 (loop_vinfo
, loop
);
936 /* When vectorizing an outer-loop, the inner-loop is executed sequentially.
937 Reductions in such inner-loop therefore have different properties than
938 the reductions in the nest that gets vectorized:
939 1. When vectorized, they are executed in the same order as in the original
940 scalar loop, so we can't change the order of computation when
942 2. FIXME: Inner-loop reductions can be used in the inner-loop, so the
943 current checks are too strict. */
946 vect_analyze_scalar_cycles_1 (loop_vinfo
, loop
->inner
);
949 /* Transfer group and reduction information from STMT to its pattern stmt. */
952 vect_fixup_reduc_chain (gimple
*stmt
)
954 gimple
*firstp
= STMT_VINFO_RELATED_STMT (vinfo_for_stmt (stmt
));
956 gcc_assert (!GROUP_FIRST_ELEMENT (vinfo_for_stmt (firstp
))
957 && GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt
)));
958 GROUP_SIZE (vinfo_for_stmt (firstp
)) = GROUP_SIZE (vinfo_for_stmt (stmt
));
961 stmtp
= STMT_VINFO_RELATED_STMT (vinfo_for_stmt (stmt
));
962 GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmtp
)) = firstp
;
963 stmt
= GROUP_NEXT_ELEMENT (vinfo_for_stmt (stmt
));
965 GROUP_NEXT_ELEMENT (vinfo_for_stmt (stmtp
))
966 = STMT_VINFO_RELATED_STMT (vinfo_for_stmt (stmt
));
969 STMT_VINFO_DEF_TYPE (vinfo_for_stmt (stmtp
)) = vect_reduction_def
;
972 /* Fixup scalar cycles that now have their stmts detected as patterns. */
975 vect_fixup_scalar_cycles_with_patterns (loop_vec_info loop_vinfo
)
980 FOR_EACH_VEC_ELT (LOOP_VINFO_REDUCTION_CHAINS (loop_vinfo
), i
, first
)
981 if (STMT_VINFO_IN_PATTERN_P (vinfo_for_stmt (first
)))
983 vect_fixup_reduc_chain (first
);
984 LOOP_VINFO_REDUCTION_CHAINS (loop_vinfo
)[i
]
985 = STMT_VINFO_RELATED_STMT (vinfo_for_stmt (first
));
989 /* Function vect_get_loop_niters.
991 Determine how many iterations the loop is executed and place it
992 in NUMBER_OF_ITERATIONS. Place the number of latch iterations
993 in NUMBER_OF_ITERATIONSM1.
995 Return the loop exit condition. */
999 vect_get_loop_niters (struct loop
*loop
, tree
*number_of_iterations
,
1000 tree
*number_of_iterationsm1
)
1004 if (dump_enabled_p ())
1005 dump_printf_loc (MSG_NOTE
, vect_location
,
1006 "=== get_loop_niters ===\n");
1008 niters
= number_of_latch_executions (loop
);
1009 *number_of_iterationsm1
= niters
;
1011 /* We want the number of loop header executions which is the number
1012 of latch executions plus one.
1013 ??? For UINT_MAX latch executions this number overflows to zero
1014 for loops like do { n++; } while (n != 0); */
1015 if (niters
&& !chrec_contains_undetermined (niters
))
1016 niters
= fold_build2 (PLUS_EXPR
, TREE_TYPE (niters
), unshare_expr (niters
),
1017 build_int_cst (TREE_TYPE (niters
), 1));
1018 *number_of_iterations
= niters
;
1020 return get_loop_exit_condition (loop
);
1024 /* Function bb_in_loop_p
1026 Used as predicate for dfs order traversal of the loop bbs. */
1029 bb_in_loop_p (const_basic_block bb
, const void *data
)
1031 const struct loop
*const loop
= (const struct loop
*)data
;
1032 if (flow_bb_inside_loop_p (loop
, bb
))
1038 /* Function new_loop_vec_info.
1040 Create and initialize a new loop_vec_info struct for LOOP, as well as
1041 stmt_vec_info structs for all the stmts in LOOP. */
1043 static loop_vec_info
1044 new_loop_vec_info (struct loop
*loop
)
1048 gimple_stmt_iterator si
;
1049 unsigned int i
, nbbs
;
1051 res
= (loop_vec_info
) xcalloc (1, sizeof (struct _loop_vec_info
));
1052 res
->kind
= vec_info::loop
;
1053 LOOP_VINFO_LOOP (res
) = loop
;
1055 bbs
= get_loop_body (loop
);
1057 /* Create/Update stmt_info for all stmts in the loop. */
1058 for (i
= 0; i
< loop
->num_nodes
; i
++)
1060 basic_block bb
= bbs
[i
];
1062 for (si
= gsi_start_phis (bb
); !gsi_end_p (si
); gsi_next (&si
))
1064 gimple
*phi
= gsi_stmt (si
);
1065 gimple_set_uid (phi
, 0);
1066 set_vinfo_for_stmt (phi
, new_stmt_vec_info (phi
, res
));
1069 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); gsi_next (&si
))
1071 gimple
*stmt
= gsi_stmt (si
);
1072 gimple_set_uid (stmt
, 0);
1073 set_vinfo_for_stmt (stmt
, new_stmt_vec_info (stmt
, res
));
1077 /* CHECKME: We want to visit all BBs before their successors (except for
1078 latch blocks, for which this assertion wouldn't hold). In the simple
1079 case of the loop forms we allow, a dfs order of the BBs would the same
1080 as reversed postorder traversal, so we are safe. */
1083 bbs
= XCNEWVEC (basic_block
, loop
->num_nodes
);
1084 nbbs
= dfs_enumerate_from (loop
->header
, 0, bb_in_loop_p
,
1085 bbs
, loop
->num_nodes
, loop
);
1086 gcc_assert (nbbs
== loop
->num_nodes
);
1088 LOOP_VINFO_BBS (res
) = bbs
;
1089 LOOP_VINFO_NITERSM1 (res
) = NULL
;
1090 LOOP_VINFO_NITERS (res
) = NULL
;
1091 LOOP_VINFO_NITERS_UNCHANGED (res
) = NULL
;
1092 LOOP_VINFO_COST_MODEL_THRESHOLD (res
) = 0;
1093 LOOP_VINFO_VECTORIZABLE_P (res
) = 0;
1094 LOOP_VINFO_PEELING_FOR_ALIGNMENT (res
) = 0;
1095 LOOP_VINFO_VECT_FACTOR (res
) = 0;
1096 LOOP_VINFO_LOOP_NEST (res
) = vNULL
;
1097 LOOP_VINFO_DATAREFS (res
) = vNULL
;
1098 LOOP_VINFO_DDRS (res
) = vNULL
;
1099 LOOP_VINFO_UNALIGNED_DR (res
) = NULL
;
1100 LOOP_VINFO_MAY_MISALIGN_STMTS (res
) = vNULL
;
1101 LOOP_VINFO_MAY_ALIAS_DDRS (res
) = vNULL
;
1102 LOOP_VINFO_GROUPED_STORES (res
) = vNULL
;
1103 LOOP_VINFO_REDUCTIONS (res
) = vNULL
;
1104 LOOP_VINFO_REDUCTION_CHAINS (res
) = vNULL
;
1105 LOOP_VINFO_SLP_INSTANCES (res
) = vNULL
;
1106 LOOP_VINFO_SLP_UNROLLING_FACTOR (res
) = 1;
1107 LOOP_VINFO_TARGET_COST_DATA (res
) = init_cost (loop
);
1108 LOOP_VINFO_PEELING_FOR_GAPS (res
) = false;
1109 LOOP_VINFO_PEELING_FOR_NITER (res
) = false;
1110 LOOP_VINFO_OPERANDS_SWAPPED (res
) = false;
1116 /* Function destroy_loop_vec_info.
1118 Free LOOP_VINFO struct, as well as all the stmt_vec_info structs of all the
1119 stmts in the loop. */
1122 destroy_loop_vec_info (loop_vec_info loop_vinfo
, bool clean_stmts
)
1127 gimple_stmt_iterator si
;
1129 vec
<slp_instance
> slp_instances
;
1130 slp_instance instance
;
1136 loop
= LOOP_VINFO_LOOP (loop_vinfo
);
1138 bbs
= LOOP_VINFO_BBS (loop_vinfo
);
1139 nbbs
= clean_stmts
? loop
->num_nodes
: 0;
1140 swapped
= LOOP_VINFO_OPERANDS_SWAPPED (loop_vinfo
);
1142 for (j
= 0; j
< nbbs
; j
++)
1144 basic_block bb
= bbs
[j
];
1145 for (si
= gsi_start_phis (bb
); !gsi_end_p (si
); gsi_next (&si
))
1146 free_stmt_vec_info (gsi_stmt (si
));
1148 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); )
1150 gimple
*stmt
= gsi_stmt (si
);
1152 /* We may have broken canonical form by moving a constant
1153 into RHS1 of a commutative op. Fix such occurrences. */
1154 if (swapped
&& is_gimple_assign (stmt
))
1156 enum tree_code code
= gimple_assign_rhs_code (stmt
);
1158 if ((code
== PLUS_EXPR
1159 || code
== POINTER_PLUS_EXPR
1160 || code
== MULT_EXPR
)
1161 && CONSTANT_CLASS_P (gimple_assign_rhs1 (stmt
)))
1162 swap_ssa_operands (stmt
,
1163 gimple_assign_rhs1_ptr (stmt
),
1164 gimple_assign_rhs2_ptr (stmt
));
1167 /* Free stmt_vec_info. */
1168 free_stmt_vec_info (stmt
);
1173 free (LOOP_VINFO_BBS (loop_vinfo
));
1174 vect_destroy_datarefs (loop_vinfo
);
1175 free_dependence_relations (LOOP_VINFO_DDRS (loop_vinfo
));
1176 LOOP_VINFO_LOOP_NEST (loop_vinfo
).release ();
1177 LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo
).release ();
1178 LOOP_VINFO_MAY_ALIAS_DDRS (loop_vinfo
).release ();
1179 slp_instances
= LOOP_VINFO_SLP_INSTANCES (loop_vinfo
);
1180 FOR_EACH_VEC_ELT (slp_instances
, j
, instance
)
1181 vect_free_slp_instance (instance
);
1183 LOOP_VINFO_SLP_INSTANCES (loop_vinfo
).release ();
1184 LOOP_VINFO_GROUPED_STORES (loop_vinfo
).release ();
1185 LOOP_VINFO_REDUCTIONS (loop_vinfo
).release ();
1186 LOOP_VINFO_REDUCTION_CHAINS (loop_vinfo
).release ();
1188 destroy_cost_data (LOOP_VINFO_TARGET_COST_DATA (loop_vinfo
));
1189 loop_vinfo
->scalar_cost_vec
.release ();
1196 /* Calculate the cost of one scalar iteration of the loop. */
1198 vect_compute_single_scalar_iteration_cost (loop_vec_info loop_vinfo
)
1200 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
1201 basic_block
*bbs
= LOOP_VINFO_BBS (loop_vinfo
);
1202 int nbbs
= loop
->num_nodes
, factor
, scalar_single_iter_cost
= 0;
1203 int innerloop_iters
, i
;
1205 /* Count statements in scalar loop. Using this as scalar cost for a single
1208 TODO: Add outer loop support.
1210 TODO: Consider assigning different costs to different scalar
1214 innerloop_iters
= 1;
1216 innerloop_iters
= 50; /* FIXME */
1218 for (i
= 0; i
< nbbs
; i
++)
1220 gimple_stmt_iterator si
;
1221 basic_block bb
= bbs
[i
];
1223 if (bb
->loop_father
== loop
->inner
)
1224 factor
= innerloop_iters
;
1228 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); gsi_next (&si
))
1230 gimple
*stmt
= gsi_stmt (si
);
1231 stmt_vec_info stmt_info
= vinfo_for_stmt (stmt
);
1233 if (!is_gimple_assign (stmt
) && !is_gimple_call (stmt
))
1236 /* Skip stmts that are not vectorized inside the loop. */
1238 && !STMT_VINFO_RELEVANT_P (stmt_info
)
1239 && (!STMT_VINFO_LIVE_P (stmt_info
)
1240 || !VECTORIZABLE_CYCLE_DEF (STMT_VINFO_DEF_TYPE (stmt_info
)))
1241 && !STMT_VINFO_IN_PATTERN_P (stmt_info
))
1244 vect_cost_for_stmt kind
;
1245 if (STMT_VINFO_DATA_REF (vinfo_for_stmt (stmt
)))
1247 if (DR_IS_READ (STMT_VINFO_DATA_REF (vinfo_for_stmt (stmt
))))
1250 kind
= scalar_store
;
1255 scalar_single_iter_cost
1256 += record_stmt_cost (&LOOP_VINFO_SCALAR_ITERATION_COST (loop_vinfo
),
1257 factor
, kind
, NULL
, 0, vect_prologue
);
1260 LOOP_VINFO_SINGLE_SCALAR_ITERATION_COST (loop_vinfo
)
1261 = scalar_single_iter_cost
;
1265 /* Function vect_analyze_loop_form_1.
1267 Verify that certain CFG restrictions hold, including:
1268 - the loop has a pre-header
1269 - the loop has a single entry and exit
1270 - the loop exit condition is simple enough, and the number of iterations
1271 can be analyzed (a countable loop). */
1274 vect_analyze_loop_form_1 (struct loop
*loop
, gcond
**loop_cond
,
1275 tree
*number_of_iterationsm1
,
1276 tree
*number_of_iterations
, gcond
**inner_loop_cond
)
1278 if (dump_enabled_p ())
1279 dump_printf_loc (MSG_NOTE
, vect_location
,
1280 "=== vect_analyze_loop_form ===\n");
1282 /* Different restrictions apply when we are considering an inner-most loop,
1283 vs. an outer (nested) loop.
1284 (FORNOW. May want to relax some of these restrictions in the future). */
1288 /* Inner-most loop. We currently require that the number of BBs is
1289 exactly 2 (the header and latch). Vectorizable inner-most loops
1300 if (loop
->num_nodes
!= 2)
1302 if (dump_enabled_p ())
1303 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1304 "not vectorized: control flow in loop.\n");
1308 if (empty_block_p (loop
->header
))
1310 if (dump_enabled_p ())
1311 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1312 "not vectorized: empty loop.\n");
1318 struct loop
*innerloop
= loop
->inner
;
1321 /* Nested loop. We currently require that the loop is doubly-nested,
1322 contains a single inner loop, and the number of BBs is exactly 5.
1323 Vectorizable outer-loops look like this:
1335 The inner-loop has the properties expected of inner-most loops
1336 as described above. */
1338 if ((loop
->inner
)->inner
|| (loop
->inner
)->next
)
1340 if (dump_enabled_p ())
1341 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1342 "not vectorized: multiple nested loops.\n");
1346 if (loop
->num_nodes
!= 5)
1348 if (dump_enabled_p ())
1349 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1350 "not vectorized: control flow in loop.\n");
1354 entryedge
= loop_preheader_edge (innerloop
);
1355 if (entryedge
->src
!= loop
->header
1356 || !single_exit (innerloop
)
1357 || single_exit (innerloop
)->dest
!= EDGE_PRED (loop
->latch
, 0)->src
)
1359 if (dump_enabled_p ())
1360 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1361 "not vectorized: unsupported outerloop form.\n");
1365 /* Analyze the inner-loop. */
1366 tree inner_niterm1
, inner_niter
;
1367 if (! vect_analyze_loop_form_1 (loop
->inner
, inner_loop_cond
,
1368 &inner_niterm1
, &inner_niter
, NULL
))
1370 if (dump_enabled_p ())
1371 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1372 "not vectorized: Bad inner loop.\n");
1376 if (!expr_invariant_in_loop_p (loop
, inner_niter
))
1378 if (dump_enabled_p ())
1379 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1380 "not vectorized: inner-loop count not"
1385 if (dump_enabled_p ())
1386 dump_printf_loc (MSG_NOTE
, vect_location
,
1387 "Considering outer-loop vectorization.\n");
1390 if (!single_exit (loop
)
1391 || EDGE_COUNT (loop
->header
->preds
) != 2)
1393 if (dump_enabled_p ())
1395 if (!single_exit (loop
))
1396 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1397 "not vectorized: multiple exits.\n");
1398 else if (EDGE_COUNT (loop
->header
->preds
) != 2)
1399 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1400 "not vectorized: too many incoming edges.\n");
1405 /* We assume that the loop exit condition is at the end of the loop. i.e,
1406 that the loop is represented as a do-while (with a proper if-guard
1407 before the loop if needed), where the loop header contains all the
1408 executable statements, and the latch is empty. */
1409 if (!empty_block_p (loop
->latch
)
1410 || !gimple_seq_empty_p (phi_nodes (loop
->latch
)))
1412 if (dump_enabled_p ())
1413 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1414 "not vectorized: latch block not empty.\n");
1418 /* Make sure there exists a single-predecessor exit bb: */
1419 if (!single_pred_p (single_exit (loop
)->dest
))
1421 edge e
= single_exit (loop
);
1422 if (!(e
->flags
& EDGE_ABNORMAL
))
1424 split_loop_exit_edge (e
);
1425 if (dump_enabled_p ())
1426 dump_printf (MSG_NOTE
, "split exit edge.\n");
1430 if (dump_enabled_p ())
1431 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1432 "not vectorized: abnormal loop exit edge.\n");
1437 *loop_cond
= vect_get_loop_niters (loop
, number_of_iterations
,
1438 number_of_iterationsm1
);
1441 if (dump_enabled_p ())
1442 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1443 "not vectorized: complicated exit condition.\n");
1447 if (!*number_of_iterations
1448 || chrec_contains_undetermined (*number_of_iterations
))
1450 if (dump_enabled_p ())
1451 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1452 "not vectorized: number of iterations cannot be "
1457 if (integer_zerop (*number_of_iterations
))
1459 if (dump_enabled_p ())
1460 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1461 "not vectorized: number of iterations = 0.\n");
1468 /* Analyze LOOP form and return a loop_vec_info if it is of suitable form. */
1471 vect_analyze_loop_form (struct loop
*loop
)
1473 tree number_of_iterations
, number_of_iterationsm1
;
1474 gcond
*loop_cond
, *inner_loop_cond
= NULL
;
1476 if (! vect_analyze_loop_form_1 (loop
, &loop_cond
, &number_of_iterationsm1
,
1477 &number_of_iterations
, &inner_loop_cond
))
1480 loop_vec_info loop_vinfo
= new_loop_vec_info (loop
);
1481 LOOP_VINFO_NITERSM1 (loop_vinfo
) = number_of_iterationsm1
;
1482 LOOP_VINFO_NITERS (loop_vinfo
) = number_of_iterations
;
1483 LOOP_VINFO_NITERS_UNCHANGED (loop_vinfo
) = number_of_iterations
;
1485 if (!LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo
))
1487 if (dump_enabled_p ())
1489 dump_printf_loc (MSG_NOTE
, vect_location
,
1490 "Symbolic number of iterations is ");
1491 dump_generic_expr (MSG_NOTE
, TDF_DETAILS
, number_of_iterations
);
1492 dump_printf (MSG_NOTE
, "\n");
1496 STMT_VINFO_TYPE (vinfo_for_stmt (loop_cond
)) = loop_exit_ctrl_vec_info_type
;
1497 if (inner_loop_cond
)
1498 STMT_VINFO_TYPE (vinfo_for_stmt (inner_loop_cond
))
1499 = loop_exit_ctrl_vec_info_type
;
1501 gcc_assert (!loop
->aux
);
1502 loop
->aux
= loop_vinfo
;
1508 /* Scan the loop stmts and dependent on whether there are any (non-)SLP
1509 statements update the vectorization factor. */
1512 vect_update_vf_for_slp (loop_vec_info loop_vinfo
)
1514 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
1515 basic_block
*bbs
= LOOP_VINFO_BBS (loop_vinfo
);
1516 int nbbs
= loop
->num_nodes
;
1517 unsigned int vectorization_factor
;
1520 if (dump_enabled_p ())
1521 dump_printf_loc (MSG_NOTE
, vect_location
,
1522 "=== vect_update_vf_for_slp ===\n");
1524 vectorization_factor
= LOOP_VINFO_VECT_FACTOR (loop_vinfo
);
1525 gcc_assert (vectorization_factor
!= 0);
1527 /* If all the stmts in the loop can be SLPed, we perform only SLP, and
1528 vectorization factor of the loop is the unrolling factor required by
1529 the SLP instances. If that unrolling factor is 1, we say, that we
1530 perform pure SLP on loop - cross iteration parallelism is not
1532 bool only_slp_in_loop
= true;
1533 for (i
= 0; i
< nbbs
; i
++)
1535 basic_block bb
= bbs
[i
];
1536 for (gimple_stmt_iterator si
= gsi_start_bb (bb
); !gsi_end_p (si
);
1539 gimple
*stmt
= gsi_stmt (si
);
1540 stmt_vec_info stmt_info
= vinfo_for_stmt (stmt
);
1541 if (STMT_VINFO_IN_PATTERN_P (stmt_info
)
1542 && STMT_VINFO_RELATED_STMT (stmt_info
))
1544 stmt
= STMT_VINFO_RELATED_STMT (stmt_info
);
1545 stmt_info
= vinfo_for_stmt (stmt
);
1547 if ((STMT_VINFO_RELEVANT_P (stmt_info
)
1548 || VECTORIZABLE_CYCLE_DEF (STMT_VINFO_DEF_TYPE (stmt_info
)))
1549 && !PURE_SLP_STMT (stmt_info
))
1550 /* STMT needs both SLP and loop-based vectorization. */
1551 only_slp_in_loop
= false;
1555 if (only_slp_in_loop
)
1556 vectorization_factor
= LOOP_VINFO_SLP_UNROLLING_FACTOR (loop_vinfo
);
1558 vectorization_factor
1559 = least_common_multiple (vectorization_factor
,
1560 LOOP_VINFO_SLP_UNROLLING_FACTOR (loop_vinfo
));
1562 LOOP_VINFO_VECT_FACTOR (loop_vinfo
) = vectorization_factor
;
1563 if (dump_enabled_p ())
1564 dump_printf_loc (MSG_NOTE
, vect_location
,
1565 "Updating vectorization factor to %d\n",
1566 vectorization_factor
);
1569 /* Function vect_analyze_loop_operations.
1571 Scan the loop stmts and make sure they are all vectorizable. */
1574 vect_analyze_loop_operations (loop_vec_info loop_vinfo
)
1576 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
1577 basic_block
*bbs
= LOOP_VINFO_BBS (loop_vinfo
);
1578 int nbbs
= loop
->num_nodes
;
1580 stmt_vec_info stmt_info
;
1581 bool need_to_vectorize
= false;
1584 if (dump_enabled_p ())
1585 dump_printf_loc (MSG_NOTE
, vect_location
,
1586 "=== vect_analyze_loop_operations ===\n");
1588 for (i
= 0; i
< nbbs
; i
++)
1590 basic_block bb
= bbs
[i
];
1592 for (gphi_iterator si
= gsi_start_phis (bb
); !gsi_end_p (si
);
1595 gphi
*phi
= si
.phi ();
1598 stmt_info
= vinfo_for_stmt (phi
);
1599 if (dump_enabled_p ())
1601 dump_printf_loc (MSG_NOTE
, vect_location
, "examining phi: ");
1602 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
, phi
, 0);
1603 dump_printf (MSG_NOTE
, "\n");
1605 if (virtual_operand_p (gimple_phi_result (phi
)))
1608 /* Inner-loop loop-closed exit phi in outer-loop vectorization
1609 (i.e., a phi in the tail of the outer-loop). */
1610 if (! is_loop_header_bb_p (bb
))
1612 /* FORNOW: we currently don't support the case that these phis
1613 are not used in the outerloop (unless it is double reduction,
1614 i.e., this phi is vect_reduction_def), cause this case
1615 requires to actually do something here. */
1616 if ((!STMT_VINFO_RELEVANT_P (stmt_info
)
1617 || STMT_VINFO_LIVE_P (stmt_info
))
1618 && STMT_VINFO_DEF_TYPE (stmt_info
)
1619 != vect_double_reduction_def
)
1621 if (dump_enabled_p ())
1622 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1623 "Unsupported loop-closed phi in "
1628 /* If PHI is used in the outer loop, we check that its operand
1629 is defined in the inner loop. */
1630 if (STMT_VINFO_RELEVANT_P (stmt_info
))
1633 gimple
*op_def_stmt
;
1635 if (gimple_phi_num_args (phi
) != 1)
1638 phi_op
= PHI_ARG_DEF (phi
, 0);
1639 if (TREE_CODE (phi_op
) != SSA_NAME
)
1642 op_def_stmt
= SSA_NAME_DEF_STMT (phi_op
);
1643 if (gimple_nop_p (op_def_stmt
)
1644 || !flow_bb_inside_loop_p (loop
, gimple_bb (op_def_stmt
))
1645 || !vinfo_for_stmt (op_def_stmt
))
1648 if (STMT_VINFO_RELEVANT (vinfo_for_stmt (op_def_stmt
))
1649 != vect_used_in_outer
1650 && STMT_VINFO_RELEVANT (vinfo_for_stmt (op_def_stmt
))
1651 != vect_used_in_outer_by_reduction
)
1658 gcc_assert (stmt_info
);
1660 if (STMT_VINFO_LIVE_P (stmt_info
))
1662 /* FORNOW: not yet supported. */
1663 if (dump_enabled_p ())
1664 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1665 "not vectorized: value used after loop.\n");
1669 if (STMT_VINFO_RELEVANT (stmt_info
) == vect_used_in_scope
1670 && STMT_VINFO_DEF_TYPE (stmt_info
) != vect_induction_def
)
1672 /* A scalar-dependence cycle that we don't support. */
1673 if (dump_enabled_p ())
1674 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1675 "not vectorized: scalar dependence cycle.\n");
1679 if (STMT_VINFO_RELEVANT_P (stmt_info
))
1681 need_to_vectorize
= true;
1682 if (STMT_VINFO_DEF_TYPE (stmt_info
) == vect_induction_def
)
1683 ok
= vectorizable_induction (phi
, NULL
, NULL
);
1688 if (dump_enabled_p ())
1690 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1691 "not vectorized: relevant phi not "
1693 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
, phi
, 0);
1694 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
1700 for (gimple_stmt_iterator si
= gsi_start_bb (bb
); !gsi_end_p (si
);
1703 gimple
*stmt
= gsi_stmt (si
);
1704 if (!gimple_clobber_p (stmt
)
1705 && !vect_analyze_stmt (stmt
, &need_to_vectorize
, NULL
))
1710 /* All operations in the loop are either irrelevant (deal with loop
1711 control, or dead), or only used outside the loop and can be moved
1712 out of the loop (e.g. invariants, inductions). The loop can be
1713 optimized away by scalar optimizations. We're better off not
1714 touching this loop. */
1715 if (!need_to_vectorize
)
1717 if (dump_enabled_p ())
1718 dump_printf_loc (MSG_NOTE
, vect_location
,
1719 "All the computation can be taken out of the loop.\n");
1720 if (dump_enabled_p ())
1721 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1722 "not vectorized: redundant loop. no profit to "
1731 /* Function vect_analyze_loop_2.
1733 Apply a set of analyses on LOOP, and create a loop_vec_info struct
1734 for it. The different analyses will record information in the
1735 loop_vec_info struct. */
1737 vect_analyze_loop_2 (loop_vec_info loop_vinfo
, bool &fatal
)
1740 int max_vf
= MAX_VECTORIZATION_FACTOR
;
1742 unsigned int n_stmts
= 0;
1744 /* The first group of checks is independent of the vector size. */
1747 /* Find all data references in the loop (which correspond to vdefs/vuses)
1748 and analyze their evolution in the loop. */
1750 basic_block
*bbs
= LOOP_VINFO_BBS (loop_vinfo
);
1752 loop_p loop
= LOOP_VINFO_LOOP (loop_vinfo
);
1753 if (!find_loop_nest (loop
, &LOOP_VINFO_LOOP_NEST (loop_vinfo
)))
1755 if (dump_enabled_p ())
1756 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1757 "not vectorized: loop contains function calls"
1758 " or data references that cannot be analyzed\n");
1762 for (unsigned i
= 0; i
< loop
->num_nodes
; i
++)
1763 for (gimple_stmt_iterator gsi
= gsi_start_bb (bbs
[i
]);
1764 !gsi_end_p (gsi
); gsi_next (&gsi
))
1766 gimple
*stmt
= gsi_stmt (gsi
);
1767 if (is_gimple_debug (stmt
))
1770 if (!find_data_references_in_stmt (loop
, stmt
,
1771 &LOOP_VINFO_DATAREFS (loop_vinfo
)))
1773 if (is_gimple_call (stmt
) && loop
->safelen
)
1775 tree fndecl
= gimple_call_fndecl (stmt
), op
;
1776 if (fndecl
!= NULL_TREE
)
1778 cgraph_node
*node
= cgraph_node::get (fndecl
);
1779 if (node
!= NULL
&& node
->simd_clones
!= NULL
)
1781 unsigned int j
, n
= gimple_call_num_args (stmt
);
1782 for (j
= 0; j
< n
; j
++)
1784 op
= gimple_call_arg (stmt
, j
);
1786 || (REFERENCE_CLASS_P (op
)
1787 && get_base_address (op
)))
1790 op
= gimple_call_lhs (stmt
);
1791 /* Ignore #pragma omp declare simd functions
1792 if they don't have data references in the
1793 call stmt itself. */
1797 || (REFERENCE_CLASS_P (op
)
1798 && get_base_address (op
)))))
1803 if (dump_enabled_p ())
1804 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1805 "not vectorized: loop contains function "
1806 "calls or data references that cannot "
1812 /* Analyze the data references and also adjust the minimal
1813 vectorization factor according to the loads and stores. */
1815 ok
= vect_analyze_data_refs (loop_vinfo
, &min_vf
);
1818 if (dump_enabled_p ())
1819 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1820 "bad data references.\n");
1824 /* Classify all cross-iteration scalar data-flow cycles.
1825 Cross-iteration cycles caused by virtual phis are analyzed separately. */
1826 vect_analyze_scalar_cycles (loop_vinfo
);
1828 vect_pattern_recog (loop_vinfo
);
1830 vect_fixup_scalar_cycles_with_patterns (loop_vinfo
);
1832 /* Analyze the access patterns of the data-refs in the loop (consecutive,
1833 complex, etc.). FORNOW: Only handle consecutive access pattern. */
1835 ok
= vect_analyze_data_ref_accesses (loop_vinfo
);
1838 if (dump_enabled_p ())
1839 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1840 "bad data access.\n");
1844 /* Data-flow analysis to detect stmts that do not need to be vectorized. */
1846 ok
= vect_mark_stmts_to_be_vectorized (loop_vinfo
);
1849 if (dump_enabled_p ())
1850 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1851 "unexpected pattern.\n");
1855 /* While the rest of the analysis below depends on it in some way. */
1858 /* Analyze data dependences between the data-refs in the loop
1859 and adjust the maximum vectorization factor according to
1861 FORNOW: fail at the first data dependence that we encounter. */
1863 ok
= vect_analyze_data_ref_dependences (loop_vinfo
, &max_vf
);
1867 if (dump_enabled_p ())
1868 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1869 "bad data dependence.\n");
1873 ok
= vect_determine_vectorization_factor (loop_vinfo
);
1876 if (dump_enabled_p ())
1877 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1878 "can't determine vectorization factor.\n");
1881 if (max_vf
< LOOP_VINFO_VECT_FACTOR (loop_vinfo
))
1883 if (dump_enabled_p ())
1884 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1885 "bad data dependence.\n");
1889 /* Check the SLP opportunities in the loop, analyze and build SLP trees. */
1890 ok
= vect_analyze_slp (loop_vinfo
, n_stmts
);
1894 /* If there are any SLP instances mark them as pure_slp. */
1895 bool slp
= vect_make_slp_decision (loop_vinfo
);
1898 /* Find stmts that need to be both vectorized and SLPed. */
1899 vect_detect_hybrid_slp (loop_vinfo
);
1901 /* Update the vectorization factor based on the SLP decision. */
1902 vect_update_vf_for_slp (loop_vinfo
);
1905 /* Now the vectorization factor is final. */
1906 unsigned vectorization_factor
= LOOP_VINFO_VECT_FACTOR (loop_vinfo
);
1907 gcc_assert (vectorization_factor
!= 0);
1909 if (LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo
) && dump_enabled_p ())
1910 dump_printf_loc (MSG_NOTE
, vect_location
,
1911 "vectorization_factor = %d, niters = "
1912 HOST_WIDE_INT_PRINT_DEC
"\n", vectorization_factor
,
1913 LOOP_VINFO_INT_NITERS (loop_vinfo
));
1915 HOST_WIDE_INT max_niter
1916 = max_stmt_executions_int (LOOP_VINFO_LOOP (loop_vinfo
));
1917 if ((LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo
)
1918 && (LOOP_VINFO_INT_NITERS (loop_vinfo
) < vectorization_factor
))
1920 && (unsigned HOST_WIDE_INT
) max_niter
< vectorization_factor
))
1922 if (dump_enabled_p ())
1923 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1924 "not vectorized: iteration count too small.\n");
1925 if (dump_enabled_p ())
1926 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1927 "not vectorized: iteration count smaller than "
1928 "vectorization factor.\n");
1932 /* Analyze the alignment of the data-refs in the loop.
1933 Fail if a data reference is found that cannot be vectorized. */
1935 ok
= vect_analyze_data_refs_alignment (loop_vinfo
);
1938 if (dump_enabled_p ())
1939 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1940 "bad data alignment.\n");
1944 /* Prune the list of ddrs to be tested at run-time by versioning for alias.
1945 It is important to call pruning after vect_analyze_data_ref_accesses,
1946 since we use grouping information gathered by interleaving analysis. */
1947 ok
= vect_prune_runtime_alias_test_list (loop_vinfo
);
1950 if (dump_enabled_p ())
1951 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1952 "number of versioning for alias "
1953 "run-time tests exceeds %d "
1954 "(--param vect-max-version-for-alias-checks)\n",
1955 PARAM_VALUE (PARAM_VECT_MAX_VERSION_FOR_ALIAS_CHECKS
));
1959 /* Compute the scalar iteration cost. */
1960 vect_compute_single_scalar_iteration_cost (loop_vinfo
);
1962 /* This pass will decide on using loop versioning and/or loop peeling in
1963 order to enhance the alignment of data references in the loop. */
1965 ok
= vect_enhance_data_refs_alignment (loop_vinfo
);
1968 if (dump_enabled_p ())
1969 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1970 "bad data alignment.\n");
1976 /* Analyze operations in the SLP instances. Note this may
1977 remove unsupported SLP instances which makes the above
1978 SLP kind detection invalid. */
1979 unsigned old_size
= LOOP_VINFO_SLP_INSTANCES (loop_vinfo
).length ();
1980 vect_slp_analyze_operations (LOOP_VINFO_SLP_INSTANCES (loop_vinfo
),
1981 LOOP_VINFO_TARGET_COST_DATA (loop_vinfo
));
1982 if (LOOP_VINFO_SLP_INSTANCES (loop_vinfo
).length () != old_size
)
1986 /* Scan all the remaining operations in the loop that are not subject
1987 to SLP and make sure they are vectorizable. */
1988 ok
= vect_analyze_loop_operations (loop_vinfo
);
1991 if (dump_enabled_p ())
1992 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1993 "bad operation or unsupported loop bound.\n");
1997 /* Analyze cost. Decide if worth while to vectorize. */
1998 int min_profitable_estimate
, min_profitable_iters
;
1999 vect_estimate_min_profitable_iters (loop_vinfo
, &min_profitable_iters
,
2000 &min_profitable_estimate
);
2002 if (min_profitable_iters
< 0)
2004 if (dump_enabled_p ())
2005 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2006 "not vectorized: vectorization not profitable.\n");
2007 if (dump_enabled_p ())
2008 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2009 "not vectorized: vector version will never be "
2014 int min_scalar_loop_bound
= ((PARAM_VALUE (PARAM_MIN_VECT_LOOP_BOUND
)
2015 * vectorization_factor
) - 1);
2017 /* Use the cost model only if it is more conservative than user specified
2019 unsigned th
= (unsigned) min_scalar_loop_bound
;
2020 if (min_profitable_iters
2021 && (!min_scalar_loop_bound
2022 || min_profitable_iters
> min_scalar_loop_bound
))
2023 th
= (unsigned) min_profitable_iters
;
2025 LOOP_VINFO_COST_MODEL_THRESHOLD (loop_vinfo
) = th
;
2027 if (LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo
)
2028 && LOOP_VINFO_INT_NITERS (loop_vinfo
) <= th
)
2030 if (dump_enabled_p ())
2031 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2032 "not vectorized: vectorization not profitable.\n");
2033 if (dump_enabled_p ())
2034 dump_printf_loc (MSG_NOTE
, vect_location
,
2035 "not vectorized: iteration count smaller than user "
2036 "specified loop bound parameter or minimum profitable "
2037 "iterations (whichever is more conservative).\n");
2041 HOST_WIDE_INT estimated_niter
2042 = estimated_stmt_executions_int (LOOP_VINFO_LOOP (loop_vinfo
));
2043 if (estimated_niter
!= -1
2044 && ((unsigned HOST_WIDE_INT
) estimated_niter
2045 <= MAX (th
, (unsigned)min_profitable_estimate
)))
2047 if (dump_enabled_p ())
2048 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2049 "not vectorized: estimated iteration count too "
2051 if (dump_enabled_p ())
2052 dump_printf_loc (MSG_NOTE
, vect_location
,
2053 "not vectorized: estimated iteration count smaller "
2054 "than specified loop bound parameter or minimum "
2055 "profitable iterations (whichever is more "
2056 "conservative).\n");
2060 /* Decide whether we need to create an epilogue loop to handle
2061 remaining scalar iterations. */
2062 th
= ((LOOP_VINFO_COST_MODEL_THRESHOLD (loop_vinfo
) + 1)
2063 / LOOP_VINFO_VECT_FACTOR (loop_vinfo
))
2064 * LOOP_VINFO_VECT_FACTOR (loop_vinfo
);
2066 if (LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo
)
2067 && LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo
) > 0)
2069 if (ctz_hwi (LOOP_VINFO_INT_NITERS (loop_vinfo
)
2070 - LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo
))
2071 < exact_log2 (LOOP_VINFO_VECT_FACTOR (loop_vinfo
)))
2072 LOOP_VINFO_PEELING_FOR_NITER (loop_vinfo
) = true;
2074 else if (LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo
)
2075 || (tree_ctz (LOOP_VINFO_NITERS (loop_vinfo
))
2076 < (unsigned)exact_log2 (LOOP_VINFO_VECT_FACTOR (loop_vinfo
))
2077 /* In case of versioning, check if the maximum number of
2078 iterations is greater than th. If they are identical,
2079 the epilogue is unnecessary. */
2080 && ((!LOOP_REQUIRES_VERSIONING_FOR_ALIAS (loop_vinfo
)
2081 && !LOOP_REQUIRES_VERSIONING_FOR_ALIGNMENT (loop_vinfo
))
2082 || (unsigned HOST_WIDE_INT
) max_niter
> th
)))
2083 LOOP_VINFO_PEELING_FOR_NITER (loop_vinfo
) = true;
2085 /* If an epilogue loop is required make sure we can create one. */
2086 if (LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo
)
2087 || LOOP_VINFO_PEELING_FOR_NITER (loop_vinfo
))
2089 if (dump_enabled_p ())
2090 dump_printf_loc (MSG_NOTE
, vect_location
, "epilog loop required\n");
2091 if (!vect_can_advance_ivs_p (loop_vinfo
)
2092 || !slpeel_can_duplicate_loop_p (LOOP_VINFO_LOOP (loop_vinfo
),
2093 single_exit (LOOP_VINFO_LOOP
2096 if (dump_enabled_p ())
2097 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2098 "not vectorized: can't create required "
2104 gcc_assert (vectorization_factor
2105 == (unsigned)LOOP_VINFO_VECT_FACTOR (loop_vinfo
));
2110 /* Function vect_analyze_loop.
2112 Apply a set of analyses on LOOP, and create a loop_vec_info struct
2113 for it. The different analyses will record information in the
2114 loop_vec_info struct. */
2116 vect_analyze_loop (struct loop
*loop
)
2118 loop_vec_info loop_vinfo
;
2119 unsigned int vector_sizes
;
2121 /* Autodetect first vector size we try. */
2122 current_vector_size
= 0;
2123 vector_sizes
= targetm
.vectorize
.autovectorize_vector_sizes ();
2125 if (dump_enabled_p ())
2126 dump_printf_loc (MSG_NOTE
, vect_location
,
2127 "===== analyze_loop_nest =====\n");
2129 if (loop_outer (loop
)
2130 && loop_vec_info_for_loop (loop_outer (loop
))
2131 && LOOP_VINFO_VECTORIZABLE_P (loop_vec_info_for_loop (loop_outer (loop
))))
2133 if (dump_enabled_p ())
2134 dump_printf_loc (MSG_NOTE
, vect_location
,
2135 "outer-loop already vectorized.\n");
2141 /* Check the CFG characteristics of the loop (nesting, entry/exit). */
2142 loop_vinfo
= vect_analyze_loop_form (loop
);
2145 if (dump_enabled_p ())
2146 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2147 "bad loop form.\n");
2152 if (vect_analyze_loop_2 (loop_vinfo
, fatal
))
2154 LOOP_VINFO_VECTORIZABLE_P (loop_vinfo
) = 1;
2159 destroy_loop_vec_info (loop_vinfo
, true);
2161 vector_sizes
&= ~current_vector_size
;
2163 || vector_sizes
== 0
2164 || current_vector_size
== 0)
2167 /* Try the next biggest vector size. */
2168 current_vector_size
= 1 << floor_log2 (vector_sizes
);
2169 if (dump_enabled_p ())
2170 dump_printf_loc (MSG_NOTE
, vect_location
,
2171 "***** Re-trying analysis with "
2172 "vector size %d\n", current_vector_size
);
2177 /* Function reduction_code_for_scalar_code
2180 CODE - tree_code of a reduction operations.
2183 REDUC_CODE - the corresponding tree-code to be used to reduce the
2184 vector of partial results into a single scalar result, or ERROR_MARK
2185 if the operation is a supported reduction operation, but does not have
2188 Return FALSE if CODE currently cannot be vectorized as reduction. */
2191 reduction_code_for_scalar_code (enum tree_code code
,
2192 enum tree_code
*reduc_code
)
2197 *reduc_code
= REDUC_MAX_EXPR
;
2201 *reduc_code
= REDUC_MIN_EXPR
;
2205 *reduc_code
= REDUC_PLUS_EXPR
;
2213 *reduc_code
= ERROR_MARK
;
2222 /* Error reporting helper for vect_is_simple_reduction below. GIMPLE statement
2223 STMT is printed with a message MSG. */
2226 report_vect_op (int msg_type
, gimple
*stmt
, const char *msg
)
2228 dump_printf_loc (msg_type
, vect_location
, "%s", msg
);
2229 dump_gimple_stmt (msg_type
, TDF_SLIM
, stmt
, 0);
2230 dump_printf (msg_type
, "\n");
2234 /* Detect SLP reduction of the form:
2244 PHI is the reduction phi node (#a1 = phi <a5, a0> above)
2245 FIRST_STMT is the first reduction stmt in the chain
2246 (a2 = operation (a1)).
2248 Return TRUE if a reduction chain was detected. */
2251 vect_is_slp_reduction (loop_vec_info loop_info
, gimple
*phi
,
2254 struct loop
*loop
= (gimple_bb (phi
))->loop_father
;
2255 struct loop
*vect_loop
= LOOP_VINFO_LOOP (loop_info
);
2256 enum tree_code code
;
2257 gimple
*current_stmt
= NULL
, *loop_use_stmt
= NULL
, *first
, *next_stmt
;
2258 stmt_vec_info use_stmt_info
, current_stmt_info
;
2260 imm_use_iterator imm_iter
;
2261 use_operand_p use_p
;
2262 int nloop_uses
, size
= 0, n_out_of_loop_uses
;
2265 if (loop
!= vect_loop
)
2268 lhs
= PHI_RESULT (phi
);
2269 code
= gimple_assign_rhs_code (first_stmt
);
2273 n_out_of_loop_uses
= 0;
2274 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, lhs
)
2276 gimple
*use_stmt
= USE_STMT (use_p
);
2277 if (is_gimple_debug (use_stmt
))
2280 /* Check if we got back to the reduction phi. */
2281 if (use_stmt
== phi
)
2283 loop_use_stmt
= use_stmt
;
2288 if (flow_bb_inside_loop_p (loop
, gimple_bb (use_stmt
)))
2290 loop_use_stmt
= use_stmt
;
2294 n_out_of_loop_uses
++;
2296 /* There are can be either a single use in the loop or two uses in
2298 if (nloop_uses
> 1 || (n_out_of_loop_uses
&& nloop_uses
))
2305 /* We reached a statement with no loop uses. */
2306 if (nloop_uses
== 0)
2309 /* This is a loop exit phi, and we haven't reached the reduction phi. */
2310 if (gimple_code (loop_use_stmt
) == GIMPLE_PHI
)
2313 if (!is_gimple_assign (loop_use_stmt
)
2314 || code
!= gimple_assign_rhs_code (loop_use_stmt
)
2315 || !flow_bb_inside_loop_p (loop
, gimple_bb (loop_use_stmt
)))
2318 /* Insert USE_STMT into reduction chain. */
2319 use_stmt_info
= vinfo_for_stmt (loop_use_stmt
);
2322 current_stmt_info
= vinfo_for_stmt (current_stmt
);
2323 GROUP_NEXT_ELEMENT (current_stmt_info
) = loop_use_stmt
;
2324 GROUP_FIRST_ELEMENT (use_stmt_info
)
2325 = GROUP_FIRST_ELEMENT (current_stmt_info
);
2328 GROUP_FIRST_ELEMENT (use_stmt_info
) = loop_use_stmt
;
2330 lhs
= gimple_assign_lhs (loop_use_stmt
);
2331 current_stmt
= loop_use_stmt
;
2335 if (!found
|| loop_use_stmt
!= phi
|| size
< 2)
2338 /* Swap the operands, if needed, to make the reduction operand be the second
2340 lhs
= PHI_RESULT (phi
);
2341 next_stmt
= GROUP_FIRST_ELEMENT (vinfo_for_stmt (current_stmt
));
2344 if (gimple_assign_rhs2 (next_stmt
) == lhs
)
2346 tree op
= gimple_assign_rhs1 (next_stmt
);
2347 gimple
*def_stmt
= NULL
;
2349 if (TREE_CODE (op
) == SSA_NAME
)
2350 def_stmt
= SSA_NAME_DEF_STMT (op
);
2352 /* Check that the other def is either defined in the loop
2353 ("vect_internal_def"), or it's an induction (defined by a
2354 loop-header phi-node). */
2356 && gimple_bb (def_stmt
)
2357 && flow_bb_inside_loop_p (loop
, gimple_bb (def_stmt
))
2358 && (is_gimple_assign (def_stmt
)
2359 || is_gimple_call (def_stmt
)
2360 || STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def_stmt
))
2361 == vect_induction_def
2362 || (gimple_code (def_stmt
) == GIMPLE_PHI
2363 && STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def_stmt
))
2364 == vect_internal_def
2365 && !is_loop_header_bb_p (gimple_bb (def_stmt
)))))
2367 lhs
= gimple_assign_lhs (next_stmt
);
2368 next_stmt
= GROUP_NEXT_ELEMENT (vinfo_for_stmt (next_stmt
));
2376 tree op
= gimple_assign_rhs2 (next_stmt
);
2377 gimple
*def_stmt
= NULL
;
2379 if (TREE_CODE (op
) == SSA_NAME
)
2380 def_stmt
= SSA_NAME_DEF_STMT (op
);
2382 /* Check that the other def is either defined in the loop
2383 ("vect_internal_def"), or it's an induction (defined by a
2384 loop-header phi-node). */
2386 && gimple_bb (def_stmt
)
2387 && flow_bb_inside_loop_p (loop
, gimple_bb (def_stmt
))
2388 && (is_gimple_assign (def_stmt
)
2389 || is_gimple_call (def_stmt
)
2390 || STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def_stmt
))
2391 == vect_induction_def
2392 || (gimple_code (def_stmt
) == GIMPLE_PHI
2393 && STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def_stmt
))
2394 == vect_internal_def
2395 && !is_loop_header_bb_p (gimple_bb (def_stmt
)))))
2397 if (dump_enabled_p ())
2399 dump_printf_loc (MSG_NOTE
, vect_location
, "swapping oprnds: ");
2400 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
, next_stmt
, 0);
2401 dump_printf (MSG_NOTE
, "\n");
2404 swap_ssa_operands (next_stmt
,
2405 gimple_assign_rhs1_ptr (next_stmt
),
2406 gimple_assign_rhs2_ptr (next_stmt
));
2407 update_stmt (next_stmt
);
2409 if (CONSTANT_CLASS_P (gimple_assign_rhs1 (next_stmt
)))
2410 LOOP_VINFO_OPERANDS_SWAPPED (loop_info
) = true;
2416 lhs
= gimple_assign_lhs (next_stmt
);
2417 next_stmt
= GROUP_NEXT_ELEMENT (vinfo_for_stmt (next_stmt
));
2420 /* Save the chain for further analysis in SLP detection. */
2421 first
= GROUP_FIRST_ELEMENT (vinfo_for_stmt (current_stmt
));
2422 LOOP_VINFO_REDUCTION_CHAINS (loop_info
).safe_push (first
);
2423 GROUP_SIZE (vinfo_for_stmt (first
)) = size
;
2429 /* Function vect_is_simple_reduction_1
2431 (1) Detect a cross-iteration def-use cycle that represents a simple
2432 reduction computation. We look for the following pattern:
2437 a2 = operation (a3, a1)
2444 a2 = operation (a3, a1)
2447 1. operation is commutative and associative and it is safe to
2448 change the order of the computation (if CHECK_REDUCTION is true)
2449 2. no uses for a2 in the loop (a2 is used out of the loop)
2450 3. no uses of a1 in the loop besides the reduction operation
2451 4. no uses of a1 outside the loop.
2453 Conditions 1,4 are tested here.
2454 Conditions 2,3 are tested in vect_mark_stmts_to_be_vectorized.
2456 (2) Detect a cross-iteration def-use cycle in nested loops, i.e.,
2457 nested cycles, if CHECK_REDUCTION is false.
2459 (3) Detect cycles of phi nodes in outer-loop vectorization, i.e., double
2463 inner loop (def of a3)
2466 (4) Detect condition expressions, ie:
2467 for (int i = 0; i < N; i++)
2474 vect_is_simple_reduction (loop_vec_info loop_info
, gimple
*phi
,
2475 bool check_reduction
, bool *double_reduc
,
2476 bool need_wrapping_integral_overflow
,
2477 enum vect_reduction_type
*v_reduc_type
)
2479 struct loop
*loop
= (gimple_bb (phi
))->loop_father
;
2480 struct loop
*vect_loop
= LOOP_VINFO_LOOP (loop_info
);
2481 edge latch_e
= loop_latch_edge (loop
);
2482 tree loop_arg
= PHI_ARG_DEF_FROM_EDGE (phi
, latch_e
);
2483 gimple
*def_stmt
, *def1
= NULL
, *def2
= NULL
;
2484 enum tree_code orig_code
, code
;
2485 tree op1
, op2
, op3
= NULL_TREE
, op4
= NULL_TREE
;
2489 imm_use_iterator imm_iter
;
2490 use_operand_p use_p
;
2493 *double_reduc
= false;
2494 *v_reduc_type
= TREE_CODE_REDUCTION
;
2496 /* If CHECK_REDUCTION is true, we assume inner-most loop vectorization,
2497 otherwise, we assume outer loop vectorization. */
2498 gcc_assert ((check_reduction
&& loop
== vect_loop
)
2499 || (!check_reduction
&& flow_loop_nested_p (vect_loop
, loop
)));
2501 name
= PHI_RESULT (phi
);
2502 /* ??? If there are no uses of the PHI result the inner loop reduction
2503 won't be detected as possibly double-reduction by vectorizable_reduction
2504 because that tries to walk the PHI arg from the preheader edge which
2505 can be constant. See PR60382. */
2506 if (has_zero_uses (name
))
2509 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, name
)
2511 gimple
*use_stmt
= USE_STMT (use_p
);
2512 if (is_gimple_debug (use_stmt
))
2515 if (!flow_bb_inside_loop_p (loop
, gimple_bb (use_stmt
)))
2517 if (dump_enabled_p ())
2518 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2519 "intermediate value used outside loop.\n");
2527 if (dump_enabled_p ())
2528 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2529 "reduction used in loop.\n");
2534 if (TREE_CODE (loop_arg
) != SSA_NAME
)
2536 if (dump_enabled_p ())
2538 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2539 "reduction: not ssa_name: ");
2540 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
, loop_arg
);
2541 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
2546 def_stmt
= SSA_NAME_DEF_STMT (loop_arg
);
2549 if (dump_enabled_p ())
2550 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2551 "reduction: no def_stmt.\n");
2555 if (!is_gimple_assign (def_stmt
) && gimple_code (def_stmt
) != GIMPLE_PHI
)
2557 if (dump_enabled_p ())
2559 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
, def_stmt
, 0);
2560 dump_printf (MSG_NOTE
, "\n");
2565 if (is_gimple_assign (def_stmt
))
2567 name
= gimple_assign_lhs (def_stmt
);
2572 name
= PHI_RESULT (def_stmt
);
2577 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, name
)
2579 gimple
*use_stmt
= USE_STMT (use_p
);
2580 if (is_gimple_debug (use_stmt
))
2582 if (flow_bb_inside_loop_p (loop
, gimple_bb (use_stmt
)))
2586 if (dump_enabled_p ())
2587 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2588 "reduction used in loop.\n");
2593 /* If DEF_STMT is a phi node itself, we expect it to have a single argument
2594 defined in the inner loop. */
2597 op1
= PHI_ARG_DEF (def_stmt
, 0);
2599 if (gimple_phi_num_args (def_stmt
) != 1
2600 || TREE_CODE (op1
) != SSA_NAME
)
2602 if (dump_enabled_p ())
2603 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2604 "unsupported phi node definition.\n");
2609 def1
= SSA_NAME_DEF_STMT (op1
);
2610 if (gimple_bb (def1
)
2611 && flow_bb_inside_loop_p (loop
, gimple_bb (def_stmt
))
2613 && flow_bb_inside_loop_p (loop
->inner
, gimple_bb (def1
))
2614 && is_gimple_assign (def1
))
2616 if (dump_enabled_p ())
2617 report_vect_op (MSG_NOTE
, def_stmt
,
2618 "detected double reduction: ");
2620 *double_reduc
= true;
2627 code
= orig_code
= gimple_assign_rhs_code (def_stmt
);
2629 /* We can handle "res -= x[i]", which is non-associative by
2630 simply rewriting this into "res += -x[i]". Avoid changing
2631 gimple instruction for the first simple tests and only do this
2632 if we're allowed to change code at all. */
2633 if (code
== MINUS_EXPR
2634 && (op1
= gimple_assign_rhs1 (def_stmt
))
2635 && TREE_CODE (op1
) == SSA_NAME
2636 && SSA_NAME_DEF_STMT (op1
) == phi
)
2639 if (check_reduction
)
2641 if (code
== COND_EXPR
)
2642 *v_reduc_type
= COND_REDUCTION
;
2643 else if (!commutative_tree_code (code
) || !associative_tree_code (code
))
2645 if (dump_enabled_p ())
2646 report_vect_op (MSG_MISSED_OPTIMIZATION
, def_stmt
,
2647 "reduction: not commutative/associative: ");
2652 if (get_gimple_rhs_class (code
) != GIMPLE_BINARY_RHS
)
2654 if (code
!= COND_EXPR
)
2656 if (dump_enabled_p ())
2657 report_vect_op (MSG_MISSED_OPTIMIZATION
, def_stmt
,
2658 "reduction: not binary operation: ");
2663 op3
= gimple_assign_rhs1 (def_stmt
);
2664 if (COMPARISON_CLASS_P (op3
))
2666 op4
= TREE_OPERAND (op3
, 1);
2667 op3
= TREE_OPERAND (op3
, 0);
2670 op1
= gimple_assign_rhs2 (def_stmt
);
2671 op2
= gimple_assign_rhs3 (def_stmt
);
2673 if (TREE_CODE (op1
) != SSA_NAME
&& TREE_CODE (op2
) != SSA_NAME
)
2675 if (dump_enabled_p ())
2676 report_vect_op (MSG_MISSED_OPTIMIZATION
, def_stmt
,
2677 "reduction: uses not ssa_names: ");
2684 op1
= gimple_assign_rhs1 (def_stmt
);
2685 op2
= gimple_assign_rhs2 (def_stmt
);
2687 if (TREE_CODE (op1
) != SSA_NAME
&& TREE_CODE (op2
) != SSA_NAME
)
2689 if (dump_enabled_p ())
2690 report_vect_op (MSG_MISSED_OPTIMIZATION
, def_stmt
,
2691 "reduction: uses not ssa_names: ");
2697 type
= TREE_TYPE (gimple_assign_lhs (def_stmt
));
2698 if ((TREE_CODE (op1
) == SSA_NAME
2699 && !types_compatible_p (type
,TREE_TYPE (op1
)))
2700 || (TREE_CODE (op2
) == SSA_NAME
2701 && !types_compatible_p (type
, TREE_TYPE (op2
)))
2702 || (op3
&& TREE_CODE (op3
) == SSA_NAME
2703 && !types_compatible_p (type
, TREE_TYPE (op3
)))
2704 || (op4
&& TREE_CODE (op4
) == SSA_NAME
2705 && !types_compatible_p (type
, TREE_TYPE (op4
))))
2707 if (dump_enabled_p ())
2709 dump_printf_loc (MSG_NOTE
, vect_location
,
2710 "reduction: multiple types: operation type: ");
2711 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, type
);
2712 dump_printf (MSG_NOTE
, ", operands types: ");
2713 dump_generic_expr (MSG_NOTE
, TDF_SLIM
,
2715 dump_printf (MSG_NOTE
, ",");
2716 dump_generic_expr (MSG_NOTE
, TDF_SLIM
,
2720 dump_printf (MSG_NOTE
, ",");
2721 dump_generic_expr (MSG_NOTE
, TDF_SLIM
,
2727 dump_printf (MSG_NOTE
, ",");
2728 dump_generic_expr (MSG_NOTE
, TDF_SLIM
,
2731 dump_printf (MSG_NOTE
, "\n");
2737 /* Check that it's ok to change the order of the computation.
2738 Generally, when vectorizing a reduction we change the order of the
2739 computation. This may change the behavior of the program in some
2740 cases, so we need to check that this is ok. One exception is when
2741 vectorizing an outer-loop: the inner-loop is executed sequentially,
2742 and therefore vectorizing reductions in the inner-loop during
2743 outer-loop vectorization is safe. */
2745 if (*v_reduc_type
!= COND_REDUCTION
)
2747 /* CHECKME: check for !flag_finite_math_only too? */
2748 if (SCALAR_FLOAT_TYPE_P (type
) && !flag_associative_math
2751 /* Changing the order of operations changes the semantics. */
2752 if (dump_enabled_p ())
2753 report_vect_op (MSG_MISSED_OPTIMIZATION
, def_stmt
,
2754 "reduction: unsafe fp math optimization: ");
2757 else if (INTEGRAL_TYPE_P (type
) && check_reduction
)
2759 if (!operation_no_trapping_overflow (type
, code
))
2761 /* Changing the order of operations changes the semantics. */
2762 if (dump_enabled_p ())
2763 report_vect_op (MSG_MISSED_OPTIMIZATION
, def_stmt
,
2764 "reduction: unsafe int math optimization"
2765 " (overflow traps): ");
2768 if (need_wrapping_integral_overflow
2769 && !TYPE_OVERFLOW_WRAPS (type
)
2770 && operation_can_overflow (code
))
2772 /* Changing the order of operations changes the semantics. */
2773 if (dump_enabled_p ())
2774 report_vect_op (MSG_MISSED_OPTIMIZATION
, def_stmt
,
2775 "reduction: unsafe int math optimization"
2776 " (overflow doesn't wrap): ");
2780 else if (SAT_FIXED_POINT_TYPE_P (type
) && check_reduction
)
2782 /* Changing the order of operations changes the semantics. */
2783 if (dump_enabled_p ())
2784 report_vect_op (MSG_MISSED_OPTIMIZATION
, def_stmt
,
2785 "reduction: unsafe fixed-point math optimization: ");
2790 /* Reduction is safe. We're dealing with one of the following:
2791 1) integer arithmetic and no trapv
2792 2) floating point arithmetic, and special flags permit this optimization
2793 3) nested cycle (i.e., outer loop vectorization). */
2794 if (TREE_CODE (op1
) == SSA_NAME
)
2795 def1
= SSA_NAME_DEF_STMT (op1
);
2797 if (TREE_CODE (op2
) == SSA_NAME
)
2798 def2
= SSA_NAME_DEF_STMT (op2
);
2800 if (code
!= COND_EXPR
2801 && ((!def1
|| gimple_nop_p (def1
)) && (!def2
|| gimple_nop_p (def2
))))
2803 if (dump_enabled_p ())
2804 report_vect_op (MSG_NOTE
, def_stmt
, "reduction: no defs for operands: ");
2808 /* Check that one def is the reduction def, defined by PHI,
2809 the other def is either defined in the loop ("vect_internal_def"),
2810 or it's an induction (defined by a loop-header phi-node). */
2812 if (def2
&& def2
== phi
2813 && (code
== COND_EXPR
2814 || !def1
|| gimple_nop_p (def1
)
2815 || !flow_bb_inside_loop_p (loop
, gimple_bb (def1
))
2816 || (def1
&& flow_bb_inside_loop_p (loop
, gimple_bb (def1
))
2817 && (is_gimple_assign (def1
)
2818 || is_gimple_call (def1
)
2819 || STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def1
))
2820 == vect_induction_def
2821 || (gimple_code (def1
) == GIMPLE_PHI
2822 && STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def1
))
2823 == vect_internal_def
2824 && !is_loop_header_bb_p (gimple_bb (def1
)))))))
2826 if (dump_enabled_p ())
2827 report_vect_op (MSG_NOTE
, def_stmt
, "detected reduction: ");
2831 if (def1
&& def1
== phi
2832 && (code
== COND_EXPR
2833 || !def2
|| gimple_nop_p (def2
)
2834 || !flow_bb_inside_loop_p (loop
, gimple_bb (def2
))
2835 || (def2
&& flow_bb_inside_loop_p (loop
, gimple_bb (def2
))
2836 && (is_gimple_assign (def2
)
2837 || is_gimple_call (def2
)
2838 || STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def2
))
2839 == vect_induction_def
2840 || (gimple_code (def2
) == GIMPLE_PHI
2841 && STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def2
))
2842 == vect_internal_def
2843 && !is_loop_header_bb_p (gimple_bb (def2
)))))))
2846 && orig_code
!= MINUS_EXPR
)
2848 if (code
== COND_EXPR
)
2850 /* No current known use where this case would be useful. */
2851 if (dump_enabled_p ())
2852 report_vect_op (MSG_NOTE
, def_stmt
,
2853 "detected reduction: cannot currently swap "
2854 "operands for cond_expr");
2858 /* Swap operands (just for simplicity - so that the rest of the code
2859 can assume that the reduction variable is always the last (second)
2861 if (dump_enabled_p ())
2862 report_vect_op (MSG_NOTE
, def_stmt
,
2863 "detected reduction: need to swap operands: ");
2865 swap_ssa_operands (def_stmt
, gimple_assign_rhs1_ptr (def_stmt
),
2866 gimple_assign_rhs2_ptr (def_stmt
));
2868 if (CONSTANT_CLASS_P (gimple_assign_rhs1 (def_stmt
)))
2869 LOOP_VINFO_OPERANDS_SWAPPED (loop_info
) = true;
2873 if (dump_enabled_p ())
2874 report_vect_op (MSG_NOTE
, def_stmt
, "detected reduction: ");
2880 /* Try to find SLP reduction chain. */
2881 if (check_reduction
&& code
!= COND_EXPR
2882 && vect_is_slp_reduction (loop_info
, phi
, def_stmt
))
2884 if (dump_enabled_p ())
2885 report_vect_op (MSG_NOTE
, def_stmt
,
2886 "reduction: detected reduction chain: ");
2891 if (dump_enabled_p ())
2892 report_vect_op (MSG_MISSED_OPTIMIZATION
, def_stmt
,
2893 "reduction: unknown pattern: ");
2898 /* Wrapper around vect_is_simple_reduction_1, which will modify code
2899 in-place if it enables detection of more reductions. Arguments
2903 vect_force_simple_reduction (loop_vec_info loop_info
, gimple
*phi
,
2904 bool check_reduction
, bool *double_reduc
,
2905 bool need_wrapping_integral_overflow
)
2907 enum vect_reduction_type v_reduc_type
;
2908 return vect_is_simple_reduction (loop_info
, phi
, check_reduction
,
2910 need_wrapping_integral_overflow
,
2914 /* Calculate cost of peeling the loop PEEL_ITERS_PROLOGUE times. */
2916 vect_get_known_peeling_cost (loop_vec_info loop_vinfo
, int peel_iters_prologue
,
2917 int *peel_iters_epilogue
,
2918 stmt_vector_for_cost
*scalar_cost_vec
,
2919 stmt_vector_for_cost
*prologue_cost_vec
,
2920 stmt_vector_for_cost
*epilogue_cost_vec
)
2923 int vf
= LOOP_VINFO_VECT_FACTOR (loop_vinfo
);
2925 if (!LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo
))
2927 *peel_iters_epilogue
= vf
/2;
2928 if (dump_enabled_p ())
2929 dump_printf_loc (MSG_NOTE
, vect_location
,
2930 "cost model: epilogue peel iters set to vf/2 "
2931 "because loop iterations are unknown .\n");
2933 /* If peeled iterations are known but number of scalar loop
2934 iterations are unknown, count a taken branch per peeled loop. */
2935 retval
= record_stmt_cost (prologue_cost_vec
, 1, cond_branch_taken
,
2936 NULL
, 0, vect_prologue
);
2937 retval
= record_stmt_cost (prologue_cost_vec
, 1, cond_branch_taken
,
2938 NULL
, 0, vect_epilogue
);
2942 int niters
= LOOP_VINFO_INT_NITERS (loop_vinfo
);
2943 peel_iters_prologue
= niters
< peel_iters_prologue
?
2944 niters
: peel_iters_prologue
;
2945 *peel_iters_epilogue
= (niters
- peel_iters_prologue
) % vf
;
2946 /* If we need to peel for gaps, but no peeling is required, we have to
2947 peel VF iterations. */
2948 if (LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo
) && !*peel_iters_epilogue
)
2949 *peel_iters_epilogue
= vf
;
2952 stmt_info_for_cost
*si
;
2954 if (peel_iters_prologue
)
2955 FOR_EACH_VEC_ELT (*scalar_cost_vec
, j
, si
)
2956 retval
+= record_stmt_cost (prologue_cost_vec
,
2957 si
->count
* peel_iters_prologue
,
2958 si
->kind
, NULL
, si
->misalign
,
2960 if (*peel_iters_epilogue
)
2961 FOR_EACH_VEC_ELT (*scalar_cost_vec
, j
, si
)
2962 retval
+= record_stmt_cost (epilogue_cost_vec
,
2963 si
->count
* *peel_iters_epilogue
,
2964 si
->kind
, NULL
, si
->misalign
,
2970 /* Function vect_estimate_min_profitable_iters
2972 Return the number of iterations required for the vector version of the
2973 loop to be profitable relative to the cost of the scalar version of the
2977 vect_estimate_min_profitable_iters (loop_vec_info loop_vinfo
,
2978 int *ret_min_profitable_niters
,
2979 int *ret_min_profitable_estimate
)
2981 int min_profitable_iters
;
2982 int min_profitable_estimate
;
2983 int peel_iters_prologue
;
2984 int peel_iters_epilogue
;
2985 unsigned vec_inside_cost
= 0;
2986 int vec_outside_cost
= 0;
2987 unsigned vec_prologue_cost
= 0;
2988 unsigned vec_epilogue_cost
= 0;
2989 int scalar_single_iter_cost
= 0;
2990 int scalar_outside_cost
= 0;
2991 int vf
= LOOP_VINFO_VECT_FACTOR (loop_vinfo
);
2992 int npeel
= LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo
);
2993 void *target_cost_data
= LOOP_VINFO_TARGET_COST_DATA (loop_vinfo
);
2995 /* Cost model disabled. */
2996 if (unlimited_cost_model (LOOP_VINFO_LOOP (loop_vinfo
)))
2998 dump_printf_loc (MSG_NOTE
, vect_location
, "cost model disabled.\n");
2999 *ret_min_profitable_niters
= 0;
3000 *ret_min_profitable_estimate
= 0;
3004 /* Requires loop versioning tests to handle misalignment. */
3005 if (LOOP_REQUIRES_VERSIONING_FOR_ALIGNMENT (loop_vinfo
))
3007 /* FIXME: Make cost depend on complexity of individual check. */
3008 unsigned len
= LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo
).length ();
3009 (void) add_stmt_cost (target_cost_data
, len
, vector_stmt
, NULL
, 0,
3011 dump_printf (MSG_NOTE
,
3012 "cost model: Adding cost of checks for loop "
3013 "versioning to treat misalignment.\n");
3016 /* Requires loop versioning with alias checks. */
3017 if (LOOP_REQUIRES_VERSIONING_FOR_ALIAS (loop_vinfo
))
3019 /* FIXME: Make cost depend on complexity of individual check. */
3020 unsigned len
= LOOP_VINFO_COMP_ALIAS_DDRS (loop_vinfo
).length ();
3021 (void) add_stmt_cost (target_cost_data
, len
, vector_stmt
, NULL
, 0,
3023 dump_printf (MSG_NOTE
,
3024 "cost model: Adding cost of checks for loop "
3025 "versioning aliasing.\n");
3028 if (LOOP_REQUIRES_VERSIONING_FOR_ALIGNMENT (loop_vinfo
)
3029 || LOOP_REQUIRES_VERSIONING_FOR_ALIAS (loop_vinfo
))
3030 (void) add_stmt_cost (target_cost_data
, 1, cond_branch_taken
, NULL
, 0,
3033 /* Count statements in scalar loop. Using this as scalar cost for a single
3036 TODO: Add outer loop support.
3038 TODO: Consider assigning different costs to different scalar
3041 scalar_single_iter_cost
3042 = LOOP_VINFO_SINGLE_SCALAR_ITERATION_COST (loop_vinfo
);
3044 /* Add additional cost for the peeled instructions in prologue and epilogue
3047 FORNOW: If we don't know the value of peel_iters for prologue or epilogue
3048 at compile-time - we assume it's vf/2 (the worst would be vf-1).
3050 TODO: Build an expression that represents peel_iters for prologue and
3051 epilogue to be used in a run-time test. */
3055 peel_iters_prologue
= vf
/2;
3056 dump_printf (MSG_NOTE
, "cost model: "
3057 "prologue peel iters set to vf/2.\n");
3059 /* If peeling for alignment is unknown, loop bound of main loop becomes
3061 peel_iters_epilogue
= vf
/2;
3062 dump_printf (MSG_NOTE
, "cost model: "
3063 "epilogue peel iters set to vf/2 because "
3064 "peeling for alignment is unknown.\n");
3066 /* If peeled iterations are unknown, count a taken branch and a not taken
3067 branch per peeled loop. Even if scalar loop iterations are known,
3068 vector iterations are not known since peeled prologue iterations are
3069 not known. Hence guards remain the same. */
3070 (void) add_stmt_cost (target_cost_data
, 1, cond_branch_taken
,
3071 NULL
, 0, vect_prologue
);
3072 (void) add_stmt_cost (target_cost_data
, 1, cond_branch_not_taken
,
3073 NULL
, 0, vect_prologue
);
3074 (void) add_stmt_cost (target_cost_data
, 1, cond_branch_taken
,
3075 NULL
, 0, vect_epilogue
);
3076 (void) add_stmt_cost (target_cost_data
, 1, cond_branch_not_taken
,
3077 NULL
, 0, vect_epilogue
);
3078 stmt_info_for_cost
*si
;
3080 FOR_EACH_VEC_ELT (LOOP_VINFO_SCALAR_ITERATION_COST (loop_vinfo
), j
, si
)
3082 struct _stmt_vec_info
*stmt_info
3083 = si
->stmt
? vinfo_for_stmt (si
->stmt
) : NULL
;
3084 (void) add_stmt_cost (target_cost_data
,
3085 si
->count
* peel_iters_prologue
,
3086 si
->kind
, stmt_info
, si
->misalign
,
3088 (void) add_stmt_cost (target_cost_data
,
3089 si
->count
* peel_iters_epilogue
,
3090 si
->kind
, stmt_info
, si
->misalign
,
3096 stmt_vector_for_cost prologue_cost_vec
, epilogue_cost_vec
;
3097 stmt_info_for_cost
*si
;
3099 void *data
= LOOP_VINFO_TARGET_COST_DATA (loop_vinfo
);
3101 prologue_cost_vec
.create (2);
3102 epilogue_cost_vec
.create (2);
3103 peel_iters_prologue
= npeel
;
3105 (void) vect_get_known_peeling_cost (loop_vinfo
, peel_iters_prologue
,
3106 &peel_iters_epilogue
,
3107 &LOOP_VINFO_SCALAR_ITERATION_COST
3110 &epilogue_cost_vec
);
3112 FOR_EACH_VEC_ELT (prologue_cost_vec
, j
, si
)
3114 struct _stmt_vec_info
*stmt_info
3115 = si
->stmt
? vinfo_for_stmt (si
->stmt
) : NULL
;
3116 (void) add_stmt_cost (data
, si
->count
, si
->kind
, stmt_info
,
3117 si
->misalign
, vect_prologue
);
3120 FOR_EACH_VEC_ELT (epilogue_cost_vec
, j
, si
)
3122 struct _stmt_vec_info
*stmt_info
3123 = si
->stmt
? vinfo_for_stmt (si
->stmt
) : NULL
;
3124 (void) add_stmt_cost (data
, si
->count
, si
->kind
, stmt_info
,
3125 si
->misalign
, vect_epilogue
);
3128 prologue_cost_vec
.release ();
3129 epilogue_cost_vec
.release ();
3132 /* FORNOW: The scalar outside cost is incremented in one of the
3135 1. The vectorizer checks for alignment and aliasing and generates
3136 a condition that allows dynamic vectorization. A cost model
3137 check is ANDED with the versioning condition. Hence scalar code
3138 path now has the added cost of the versioning check.
3140 if (cost > th & versioning_check)
3143 Hence run-time scalar is incremented by not-taken branch cost.
3145 2. The vectorizer then checks if a prologue is required. If the
3146 cost model check was not done before during versioning, it has to
3147 be done before the prologue check.
3150 prologue = scalar_iters
3155 if (prologue == num_iters)
3158 Hence the run-time scalar cost is incremented by a taken branch,
3159 plus a not-taken branch, plus a taken branch cost.
3161 3. The vectorizer then checks if an epilogue is required. If the
3162 cost model check was not done before during prologue check, it
3163 has to be done with the epilogue check.
3169 if (prologue == num_iters)
3172 if ((cost <= th) | (scalar_iters-prologue-epilogue == 0))
3175 Hence the run-time scalar cost should be incremented by 2 taken
3178 TODO: The back end may reorder the BBS's differently and reverse
3179 conditions/branch directions. Change the estimates below to
3180 something more reasonable. */
3182 /* If the number of iterations is known and we do not do versioning, we can
3183 decide whether to vectorize at compile time. Hence the scalar version
3184 do not carry cost model guard costs. */
3185 if (!LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo
)
3186 || LOOP_REQUIRES_VERSIONING_FOR_ALIGNMENT (loop_vinfo
)
3187 || LOOP_REQUIRES_VERSIONING_FOR_ALIAS (loop_vinfo
))
3189 /* Cost model check occurs at versioning. */
3190 if (LOOP_REQUIRES_VERSIONING_FOR_ALIGNMENT (loop_vinfo
)
3191 || LOOP_REQUIRES_VERSIONING_FOR_ALIAS (loop_vinfo
))
3192 scalar_outside_cost
+= vect_get_stmt_cost (cond_branch_not_taken
);
3195 /* Cost model check occurs at prologue generation. */
3196 if (LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo
) < 0)
3197 scalar_outside_cost
+= 2 * vect_get_stmt_cost (cond_branch_taken
)
3198 + vect_get_stmt_cost (cond_branch_not_taken
);
3199 /* Cost model check occurs at epilogue generation. */
3201 scalar_outside_cost
+= 2 * vect_get_stmt_cost (cond_branch_taken
);
3205 /* Complete the target-specific cost calculations. */
3206 finish_cost (LOOP_VINFO_TARGET_COST_DATA (loop_vinfo
), &vec_prologue_cost
,
3207 &vec_inside_cost
, &vec_epilogue_cost
);
3209 vec_outside_cost
= (int)(vec_prologue_cost
+ vec_epilogue_cost
);
3211 if (dump_enabled_p ())
3213 dump_printf_loc (MSG_NOTE
, vect_location
, "Cost model analysis: \n");
3214 dump_printf (MSG_NOTE
, " Vector inside of loop cost: %d\n",
3216 dump_printf (MSG_NOTE
, " Vector prologue cost: %d\n",
3218 dump_printf (MSG_NOTE
, " Vector epilogue cost: %d\n",
3220 dump_printf (MSG_NOTE
, " Scalar iteration cost: %d\n",
3221 scalar_single_iter_cost
);
3222 dump_printf (MSG_NOTE
, " Scalar outside cost: %d\n",
3223 scalar_outside_cost
);
3224 dump_printf (MSG_NOTE
, " Vector outside cost: %d\n",
3226 dump_printf (MSG_NOTE
, " prologue iterations: %d\n",
3227 peel_iters_prologue
);
3228 dump_printf (MSG_NOTE
, " epilogue iterations: %d\n",
3229 peel_iters_epilogue
);
3232 /* Calculate number of iterations required to make the vector version
3233 profitable, relative to the loop bodies only. The following condition
3235 SIC * niters + SOC > VIC * ((niters-PL_ITERS-EP_ITERS)/VF) + VOC
3237 SIC = scalar iteration cost, VIC = vector iteration cost,
3238 VOC = vector outside cost, VF = vectorization factor,
3239 PL_ITERS = prologue iterations, EP_ITERS= epilogue iterations
3240 SOC = scalar outside cost for run time cost model check. */
3242 if ((scalar_single_iter_cost
* vf
) > (int) vec_inside_cost
)
3244 if (vec_outside_cost
<= 0)
3245 min_profitable_iters
= 1;
3248 min_profitable_iters
= ((vec_outside_cost
- scalar_outside_cost
) * vf
3249 - vec_inside_cost
* peel_iters_prologue
3250 - vec_inside_cost
* peel_iters_epilogue
)
3251 / ((scalar_single_iter_cost
* vf
)
3254 if ((scalar_single_iter_cost
* vf
* min_profitable_iters
)
3255 <= (((int) vec_inside_cost
* min_profitable_iters
)
3256 + (((int) vec_outside_cost
- scalar_outside_cost
) * vf
)))
3257 min_profitable_iters
++;
3260 /* vector version will never be profitable. */
3263 if (LOOP_VINFO_LOOP (loop_vinfo
)->force_vectorize
)
3264 warning_at (vect_location
, OPT_Wopenmp_simd
, "vectorization "
3265 "did not happen for a simd loop");
3267 if (dump_enabled_p ())
3268 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
3269 "cost model: the vector iteration cost = %d "
3270 "divided by the scalar iteration cost = %d "
3271 "is greater or equal to the vectorization factor = %d"
3273 vec_inside_cost
, scalar_single_iter_cost
, vf
);
3274 *ret_min_profitable_niters
= -1;
3275 *ret_min_profitable_estimate
= -1;
3279 dump_printf (MSG_NOTE
,
3280 " Calculated minimum iters for profitability: %d\n",
3281 min_profitable_iters
);
3283 min_profitable_iters
=
3284 min_profitable_iters
< vf
? vf
: min_profitable_iters
;
3286 /* Because the condition we create is:
3287 if (niters <= min_profitable_iters)
3288 then skip the vectorized loop. */
3289 min_profitable_iters
--;
3291 if (dump_enabled_p ())
3292 dump_printf_loc (MSG_NOTE
, vect_location
,
3293 " Runtime profitability threshold = %d\n",
3294 min_profitable_iters
);
3296 *ret_min_profitable_niters
= min_profitable_iters
;
3298 /* Calculate number of iterations required to make the vector version
3299 profitable, relative to the loop bodies only.
3301 Non-vectorized variant is SIC * niters and it must win over vector
3302 variant on the expected loop trip count. The following condition must hold true:
3303 SIC * niters > VIC * ((niters-PL_ITERS-EP_ITERS)/VF) + VOC + SOC */
3305 if (vec_outside_cost
<= 0)
3306 min_profitable_estimate
= 1;
3309 min_profitable_estimate
= ((vec_outside_cost
+ scalar_outside_cost
) * vf
3310 - vec_inside_cost
* peel_iters_prologue
3311 - vec_inside_cost
* peel_iters_epilogue
)
3312 / ((scalar_single_iter_cost
* vf
)
3315 min_profitable_estimate
--;
3316 min_profitable_estimate
= MAX (min_profitable_estimate
, min_profitable_iters
);
3317 if (dump_enabled_p ())
3318 dump_printf_loc (MSG_NOTE
, vect_location
,
3319 " Static estimate profitability threshold = %d\n",
3320 min_profitable_iters
);
3322 *ret_min_profitable_estimate
= min_profitable_estimate
;
3325 /* Writes into SEL a mask for a vec_perm, equivalent to a vec_shr by OFFSET
3326 vector elements (not bits) for a vector of mode MODE. */
3328 calc_vec_perm_mask_for_shift (enum machine_mode mode
, unsigned int offset
,
3331 unsigned int i
, nelt
= GET_MODE_NUNITS (mode
);
3333 for (i
= 0; i
< nelt
; i
++)
3334 sel
[i
] = (i
+ offset
) & (2*nelt
- 1);
3337 /* Checks whether the target supports whole-vector shifts for vectors of mode
3338 MODE. This is the case if _either_ the platform handles vec_shr_optab, _or_
3339 it supports vec_perm_const with masks for all necessary shift amounts. */
3341 have_whole_vector_shift (enum machine_mode mode
)
3343 if (optab_handler (vec_shr_optab
, mode
) != CODE_FOR_nothing
)
3346 if (direct_optab_handler (vec_perm_const_optab
, mode
) == CODE_FOR_nothing
)
3349 unsigned int i
, nelt
= GET_MODE_NUNITS (mode
);
3350 unsigned char *sel
= XALLOCAVEC (unsigned char, nelt
);
3352 for (i
= nelt
/2; i
>= 1; i
/=2)
3354 calc_vec_perm_mask_for_shift (mode
, i
, sel
);
3355 if (!can_vec_perm_p (mode
, false, sel
))
3361 /* Return the reduction operand (with index REDUC_INDEX) of STMT. */
3364 get_reduction_op (gimple
*stmt
, int reduc_index
)
3366 switch (get_gimple_rhs_class (gimple_assign_rhs_code (stmt
)))
3368 case GIMPLE_SINGLE_RHS
:
3369 gcc_assert (TREE_OPERAND_LENGTH (gimple_assign_rhs1 (stmt
))
3371 return TREE_OPERAND (gimple_assign_rhs1 (stmt
), reduc_index
);
3372 case GIMPLE_UNARY_RHS
:
3373 return gimple_assign_rhs1 (stmt
);
3374 case GIMPLE_BINARY_RHS
:
3376 ? gimple_assign_rhs2 (stmt
) : gimple_assign_rhs1 (stmt
));
3377 case GIMPLE_TERNARY_RHS
:
3378 return gimple_op (stmt
, reduc_index
+ 1);
3384 /* TODO: Close dependency between vect_model_*_cost and vectorizable_*
3385 functions. Design better to avoid maintenance issues. */
3387 /* Function vect_model_reduction_cost.
3389 Models cost for a reduction operation, including the vector ops
3390 generated within the strip-mine loop, the initial definition before
3391 the loop, and the epilogue code that must be generated. */
3394 vect_model_reduction_cost (stmt_vec_info stmt_info
, enum tree_code reduc_code
,
3395 int ncopies
, int reduc_index
)
3397 int prologue_cost
= 0, epilogue_cost
= 0;
3398 enum tree_code code
;
3401 gimple
*stmt
, *orig_stmt
;
3404 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_info
);
3405 struct loop
*loop
= NULL
;
3406 void *target_cost_data
;
3410 loop
= LOOP_VINFO_LOOP (loop_vinfo
);
3411 target_cost_data
= LOOP_VINFO_TARGET_COST_DATA (loop_vinfo
);
3414 target_cost_data
= BB_VINFO_TARGET_COST_DATA (STMT_VINFO_BB_VINFO (stmt_info
));
3416 /* Condition reductions generate two reductions in the loop. */
3417 if (STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
) == COND_REDUCTION
)
3420 /* Cost of reduction op inside loop. */
3421 unsigned inside_cost
= add_stmt_cost (target_cost_data
, ncopies
, vector_stmt
,
3422 stmt_info
, 0, vect_body
);
3423 stmt
= STMT_VINFO_STMT (stmt_info
);
3425 reduction_op
= get_reduction_op (stmt
, reduc_index
);
3427 vectype
= get_vectype_for_scalar_type (TREE_TYPE (reduction_op
));
3430 if (dump_enabled_p ())
3432 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
3433 "unsupported data-type ");
3434 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
,
3435 TREE_TYPE (reduction_op
));
3436 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
3441 mode
= TYPE_MODE (vectype
);
3442 orig_stmt
= STMT_VINFO_RELATED_STMT (stmt_info
);
3445 orig_stmt
= STMT_VINFO_STMT (stmt_info
);
3447 code
= gimple_assign_rhs_code (orig_stmt
);
3449 /* Add in cost for initial definition.
3450 For cond reduction we have four vectors: initial index, step, initial
3451 result of the data reduction, initial value of the index reduction. */
3452 int prologue_stmts
= STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
)
3453 == COND_REDUCTION
? 4 : 1;
3454 prologue_cost
+= add_stmt_cost (target_cost_data
, prologue_stmts
,
3455 scalar_to_vec
, stmt_info
, 0,
3458 /* Determine cost of epilogue code.
3460 We have a reduction operator that will reduce the vector in one statement.
3461 Also requires scalar extract. */
3463 if (!loop
|| !nested_in_vect_loop_p (loop
, orig_stmt
))
3465 if (reduc_code
!= ERROR_MARK
)
3467 if (STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
) == COND_REDUCTION
)
3469 /* An EQ stmt and an COND_EXPR stmt. */
3470 epilogue_cost
+= add_stmt_cost (target_cost_data
, 2,
3471 vector_stmt
, stmt_info
, 0,
3473 /* Reduction of the max index and a reduction of the found
3475 epilogue_cost
+= add_stmt_cost (target_cost_data
, 2,
3476 vec_to_scalar
, stmt_info
, 0,
3478 /* A broadcast of the max value. */
3479 epilogue_cost
+= add_stmt_cost (target_cost_data
, 1,
3480 scalar_to_vec
, stmt_info
, 0,
3485 epilogue_cost
+= add_stmt_cost (target_cost_data
, 1, vector_stmt
,
3486 stmt_info
, 0, vect_epilogue
);
3487 epilogue_cost
+= add_stmt_cost (target_cost_data
, 1,
3488 vec_to_scalar
, stmt_info
, 0,
3494 int vec_size_in_bits
= tree_to_uhwi (TYPE_SIZE (vectype
));
3496 TYPE_SIZE (TREE_TYPE (gimple_assign_lhs (orig_stmt
)));
3497 int element_bitsize
= tree_to_uhwi (bitsize
);
3498 int nelements
= vec_size_in_bits
/ element_bitsize
;
3500 optab
= optab_for_tree_code (code
, vectype
, optab_default
);
3502 /* We have a whole vector shift available. */
3503 if (VECTOR_MODE_P (mode
)
3504 && optab_handler (optab
, mode
) != CODE_FOR_nothing
3505 && have_whole_vector_shift (mode
))
3507 /* Final reduction via vector shifts and the reduction operator.
3508 Also requires scalar extract. */
3509 epilogue_cost
+= add_stmt_cost (target_cost_data
,
3510 exact_log2 (nelements
) * 2,
3511 vector_stmt
, stmt_info
, 0,
3513 epilogue_cost
+= add_stmt_cost (target_cost_data
, 1,
3514 vec_to_scalar
, stmt_info
, 0,
3518 /* Use extracts and reduction op for final reduction. For N
3519 elements, we have N extracts and N-1 reduction ops. */
3520 epilogue_cost
+= add_stmt_cost (target_cost_data
,
3521 nelements
+ nelements
- 1,
3522 vector_stmt
, stmt_info
, 0,
3527 if (dump_enabled_p ())
3528 dump_printf (MSG_NOTE
,
3529 "vect_model_reduction_cost: inside_cost = %d, "
3530 "prologue_cost = %d, epilogue_cost = %d .\n", inside_cost
,
3531 prologue_cost
, epilogue_cost
);
3537 /* Function vect_model_induction_cost.
3539 Models cost for induction operations. */
3542 vect_model_induction_cost (stmt_vec_info stmt_info
, int ncopies
)
3544 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_info
);
3545 void *target_cost_data
= LOOP_VINFO_TARGET_COST_DATA (loop_vinfo
);
3546 unsigned inside_cost
, prologue_cost
;
3548 /* loop cost for vec_loop. */
3549 inside_cost
= add_stmt_cost (target_cost_data
, ncopies
, vector_stmt
,
3550 stmt_info
, 0, vect_body
);
3552 /* prologue cost for vec_init and vec_step. */
3553 prologue_cost
= add_stmt_cost (target_cost_data
, 2, scalar_to_vec
,
3554 stmt_info
, 0, vect_prologue
);
3556 if (dump_enabled_p ())
3557 dump_printf_loc (MSG_NOTE
, vect_location
,
3558 "vect_model_induction_cost: inside_cost = %d, "
3559 "prologue_cost = %d .\n", inside_cost
, prologue_cost
);
3563 /* Function get_initial_def_for_induction
3566 STMT - a stmt that performs an induction operation in the loop.
3567 IV_PHI - the initial value of the induction variable
3570 Return a vector variable, initialized with the first VF values of
3571 the induction variable. E.g., for an iv with IV_PHI='X' and
3572 evolution S, for a vector of 4 units, we want to return:
3573 [X, X + S, X + 2*S, X + 3*S]. */
3576 get_initial_def_for_induction (gimple
*iv_phi
)
3578 stmt_vec_info stmt_vinfo
= vinfo_for_stmt (iv_phi
);
3579 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_vinfo
);
3580 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
3583 edge pe
= loop_preheader_edge (loop
);
3584 struct loop
*iv_loop
;
3586 tree new_vec
, vec_init
, vec_step
, t
;
3589 gphi
*induction_phi
;
3590 tree induc_def
, vec_def
, vec_dest
;
3591 tree init_expr
, step_expr
;
3592 int vf
= LOOP_VINFO_VECT_FACTOR (loop_vinfo
);
3596 stmt_vec_info phi_info
= vinfo_for_stmt (iv_phi
);
3597 bool nested_in_vect_loop
= false;
3599 imm_use_iterator imm_iter
;
3600 use_operand_p use_p
;
3604 gimple_stmt_iterator si
;
3605 basic_block bb
= gimple_bb (iv_phi
);
3609 /* Is phi in an inner-loop, while vectorizing an enclosing outer-loop? */
3610 if (nested_in_vect_loop_p (loop
, iv_phi
))
3612 nested_in_vect_loop
= true;
3613 iv_loop
= loop
->inner
;
3617 gcc_assert (iv_loop
== (gimple_bb (iv_phi
))->loop_father
);
3619 latch_e
= loop_latch_edge (iv_loop
);
3620 loop_arg
= PHI_ARG_DEF_FROM_EDGE (iv_phi
, latch_e
);
3622 step_expr
= STMT_VINFO_LOOP_PHI_EVOLUTION_PART (phi_info
);
3623 gcc_assert (step_expr
!= NULL_TREE
);
3625 pe
= loop_preheader_edge (iv_loop
);
3626 init_expr
= PHI_ARG_DEF_FROM_EDGE (iv_phi
,
3627 loop_preheader_edge (iv_loop
));
3629 vectype
= get_vectype_for_scalar_type (TREE_TYPE (init_expr
));
3630 resvectype
= get_vectype_for_scalar_type (TREE_TYPE (PHI_RESULT (iv_phi
)));
3631 gcc_assert (vectype
);
3632 nunits
= TYPE_VECTOR_SUBPARTS (vectype
);
3633 ncopies
= vf
/ nunits
;
3635 gcc_assert (phi_info
);
3636 gcc_assert (ncopies
>= 1);
3638 /* Convert the step to the desired type. */
3640 step_expr
= gimple_convert (&stmts
, TREE_TYPE (vectype
), step_expr
);
3643 new_bb
= gsi_insert_seq_on_edge_immediate (pe
, stmts
);
3644 gcc_assert (!new_bb
);
3647 /* Find the first insertion point in the BB. */
3648 si
= gsi_after_labels (bb
);
3650 /* Create the vector that holds the initial_value of the induction. */
3651 if (nested_in_vect_loop
)
3653 /* iv_loop is nested in the loop to be vectorized. init_expr had already
3654 been created during vectorization of previous stmts. We obtain it
3655 from the STMT_VINFO_VEC_STMT of the defining stmt. */
3656 vec_init
= vect_get_vec_def_for_operand (init_expr
, iv_phi
);
3657 /* If the initial value is not of proper type, convert it. */
3658 if (!useless_type_conversion_p (vectype
, TREE_TYPE (vec_init
)))
3661 = gimple_build_assign (vect_get_new_ssa_name (vectype
,
3665 build1 (VIEW_CONVERT_EXPR
, vectype
,
3667 vec_init
= gimple_assign_lhs (new_stmt
);
3668 new_bb
= gsi_insert_on_edge_immediate (loop_preheader_edge (iv_loop
),
3670 gcc_assert (!new_bb
);
3671 set_vinfo_for_stmt (new_stmt
,
3672 new_stmt_vec_info (new_stmt
, loop_vinfo
));
3677 vec
<constructor_elt
, va_gc
> *v
;
3679 /* iv_loop is the loop to be vectorized. Create:
3680 vec_init = [X, X+S, X+2*S, X+3*S] (S = step_expr, X = init_expr) */
3682 new_name
= gimple_convert (&stmts
, TREE_TYPE (vectype
), init_expr
);
3684 vec_alloc (v
, nunits
);
3685 bool constant_p
= is_gimple_min_invariant (new_name
);
3686 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, new_name
);
3687 for (i
= 1; i
< nunits
; i
++)
3689 /* Create: new_name_i = new_name + step_expr */
3690 new_name
= gimple_build (&stmts
, PLUS_EXPR
, TREE_TYPE (new_name
),
3691 new_name
, step_expr
);
3692 if (!is_gimple_min_invariant (new_name
))
3694 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, new_name
);
3698 new_bb
= gsi_insert_seq_on_edge_immediate (pe
, stmts
);
3699 gcc_assert (!new_bb
);
3702 /* Create a vector from [new_name_0, new_name_1, ..., new_name_nunits-1] */
3704 new_vec
= build_vector_from_ctor (vectype
, v
);
3706 new_vec
= build_constructor (vectype
, v
);
3707 vec_init
= vect_init_vector (iv_phi
, new_vec
, vectype
, NULL
);
3711 /* Create the vector that holds the step of the induction. */
3712 if (nested_in_vect_loop
)
3713 /* iv_loop is nested in the loop to be vectorized. Generate:
3714 vec_step = [S, S, S, S] */
3715 new_name
= step_expr
;
3718 /* iv_loop is the loop to be vectorized. Generate:
3719 vec_step = [VF*S, VF*S, VF*S, VF*S] */
3720 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (step_expr
)))
3722 expr
= build_int_cst (integer_type_node
, vf
);
3723 expr
= fold_convert (TREE_TYPE (step_expr
), expr
);
3726 expr
= build_int_cst (TREE_TYPE (step_expr
), vf
);
3727 new_name
= fold_build2 (MULT_EXPR
, TREE_TYPE (step_expr
),
3729 if (TREE_CODE (step_expr
) == SSA_NAME
)
3730 new_name
= vect_init_vector (iv_phi
, new_name
,
3731 TREE_TYPE (step_expr
), NULL
);
3734 t
= unshare_expr (new_name
);
3735 gcc_assert (CONSTANT_CLASS_P (new_name
)
3736 || TREE_CODE (new_name
) == SSA_NAME
);
3737 stepvectype
= get_vectype_for_scalar_type (TREE_TYPE (new_name
));
3738 gcc_assert (stepvectype
);
3739 new_vec
= build_vector_from_val (stepvectype
, t
);
3740 vec_step
= vect_init_vector (iv_phi
, new_vec
, stepvectype
, NULL
);
3743 /* Create the following def-use cycle:
3748 vec_iv = PHI <vec_init, vec_loop>
3752 vec_loop = vec_iv + vec_step; */
3754 /* Create the induction-phi that defines the induction-operand. */
3755 vec_dest
= vect_get_new_vect_var (vectype
, vect_simple_var
, "vec_iv_");
3756 induction_phi
= create_phi_node (vec_dest
, iv_loop
->header
);
3757 set_vinfo_for_stmt (induction_phi
,
3758 new_stmt_vec_info (induction_phi
, loop_vinfo
));
3759 induc_def
= PHI_RESULT (induction_phi
);
3761 /* Create the iv update inside the loop */
3762 new_stmt
= gimple_build_assign (vec_dest
, PLUS_EXPR
, induc_def
, vec_step
);
3763 vec_def
= make_ssa_name (vec_dest
, new_stmt
);
3764 gimple_assign_set_lhs (new_stmt
, vec_def
);
3765 gsi_insert_before (&si
, new_stmt
, GSI_SAME_STMT
);
3766 set_vinfo_for_stmt (new_stmt
, new_stmt_vec_info (new_stmt
, loop_vinfo
));
3768 /* Set the arguments of the phi node: */
3769 add_phi_arg (induction_phi
, vec_init
, pe
, UNKNOWN_LOCATION
);
3770 add_phi_arg (induction_phi
, vec_def
, loop_latch_edge (iv_loop
),
3774 /* In case that vectorization factor (VF) is bigger than the number
3775 of elements that we can fit in a vectype (nunits), we have to generate
3776 more than one vector stmt - i.e - we need to "unroll" the
3777 vector stmt by a factor VF/nunits. For more details see documentation
3778 in vectorizable_operation. */
3782 stmt_vec_info prev_stmt_vinfo
;
3783 /* FORNOW. This restriction should be relaxed. */
3784 gcc_assert (!nested_in_vect_loop
);
3786 /* Create the vector that holds the step of the induction. */
3787 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (step_expr
)))
3789 expr
= build_int_cst (integer_type_node
, nunits
);
3790 expr
= fold_convert (TREE_TYPE (step_expr
), expr
);
3793 expr
= build_int_cst (TREE_TYPE (step_expr
), nunits
);
3794 new_name
= fold_build2 (MULT_EXPR
, TREE_TYPE (step_expr
),
3796 if (TREE_CODE (step_expr
) == SSA_NAME
)
3797 new_name
= vect_init_vector (iv_phi
, new_name
,
3798 TREE_TYPE (step_expr
), NULL
);
3799 t
= unshare_expr (new_name
);
3800 gcc_assert (CONSTANT_CLASS_P (new_name
)
3801 || TREE_CODE (new_name
) == SSA_NAME
);
3802 new_vec
= build_vector_from_val (stepvectype
, t
);
3803 vec_step
= vect_init_vector (iv_phi
, new_vec
, stepvectype
, NULL
);
3805 vec_def
= induc_def
;
3806 prev_stmt_vinfo
= vinfo_for_stmt (induction_phi
);
3807 for (i
= 1; i
< ncopies
; i
++)
3809 /* vec_i = vec_prev + vec_step */
3810 new_stmt
= gimple_build_assign (vec_dest
, PLUS_EXPR
,
3812 vec_def
= make_ssa_name (vec_dest
, new_stmt
);
3813 gimple_assign_set_lhs (new_stmt
, vec_def
);
3815 gsi_insert_before (&si
, new_stmt
, GSI_SAME_STMT
);
3816 if (!useless_type_conversion_p (resvectype
, vectype
))
3819 = gimple_build_assign
3820 (vect_get_new_vect_var (resvectype
, vect_simple_var
,
3823 build1 (VIEW_CONVERT_EXPR
, resvectype
,
3824 gimple_assign_lhs (new_stmt
)));
3825 gimple_assign_set_lhs (new_stmt
,
3827 (gimple_assign_lhs (new_stmt
), new_stmt
));
3828 gsi_insert_before (&si
, new_stmt
, GSI_SAME_STMT
);
3830 set_vinfo_for_stmt (new_stmt
,
3831 new_stmt_vec_info (new_stmt
, loop_vinfo
));
3832 STMT_VINFO_RELATED_STMT (prev_stmt_vinfo
) = new_stmt
;
3833 prev_stmt_vinfo
= vinfo_for_stmt (new_stmt
);
3837 if (nested_in_vect_loop
)
3839 /* Find the loop-closed exit-phi of the induction, and record
3840 the final vector of induction results: */
3842 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, loop_arg
)
3844 gimple
*use_stmt
= USE_STMT (use_p
);
3845 if (is_gimple_debug (use_stmt
))
3848 if (!flow_bb_inside_loop_p (iv_loop
, gimple_bb (use_stmt
)))
3850 exit_phi
= use_stmt
;
3856 stmt_vec_info stmt_vinfo
= vinfo_for_stmt (exit_phi
);
3857 /* FORNOW. Currently not supporting the case that an inner-loop induction
3858 is not used in the outer-loop (i.e. only outside the outer-loop). */
3859 gcc_assert (STMT_VINFO_RELEVANT_P (stmt_vinfo
)
3860 && !STMT_VINFO_LIVE_P (stmt_vinfo
));
3862 STMT_VINFO_VEC_STMT (stmt_vinfo
) = new_stmt
;
3863 if (dump_enabled_p ())
3865 dump_printf_loc (MSG_NOTE
, vect_location
,
3866 "vector of inductions after inner-loop:");
3867 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
, new_stmt
, 0);
3868 dump_printf (MSG_NOTE
, "\n");
3874 if (dump_enabled_p ())
3876 dump_printf_loc (MSG_NOTE
, vect_location
,
3877 "transform induction: created def-use cycle: ");
3878 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
, induction_phi
, 0);
3879 dump_printf (MSG_NOTE
, "\n");
3880 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
,
3881 SSA_NAME_DEF_STMT (vec_def
), 0);
3882 dump_printf (MSG_NOTE
, "\n");
3885 STMT_VINFO_VEC_STMT (phi_info
) = induction_phi
;
3886 if (!useless_type_conversion_p (resvectype
, vectype
))
3888 new_stmt
= gimple_build_assign (vect_get_new_vect_var (resvectype
,
3892 build1 (VIEW_CONVERT_EXPR
, resvectype
,
3894 induc_def
= make_ssa_name (gimple_assign_lhs (new_stmt
), new_stmt
);
3895 gimple_assign_set_lhs (new_stmt
, induc_def
);
3896 si
= gsi_after_labels (bb
);
3897 gsi_insert_before (&si
, new_stmt
, GSI_SAME_STMT
);
3898 set_vinfo_for_stmt (new_stmt
,
3899 new_stmt_vec_info (new_stmt
, loop_vinfo
));
3900 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (new_stmt
))
3901 = STMT_VINFO_RELATED_STMT (vinfo_for_stmt (induction_phi
));
3908 /* Function get_initial_def_for_reduction
3911 STMT - a stmt that performs a reduction operation in the loop.
3912 INIT_VAL - the initial value of the reduction variable
3915 ADJUSTMENT_DEF - a tree that holds a value to be added to the final result
3916 of the reduction (used for adjusting the epilog - see below).
3917 Return a vector variable, initialized according to the operation that STMT
3918 performs. This vector will be used as the initial value of the
3919 vector of partial results.
3921 Option1 (adjust in epilog): Initialize the vector as follows:
3922 add/bit or/xor: [0,0,...,0,0]
3923 mult/bit and: [1,1,...,1,1]
3924 min/max/cond_expr: [init_val,init_val,..,init_val,init_val]
3925 and when necessary (e.g. add/mult case) let the caller know
3926 that it needs to adjust the result by init_val.
3928 Option2: Initialize the vector as follows:
3929 add/bit or/xor: [init_val,0,0,...,0]
3930 mult/bit and: [init_val,1,1,...,1]
3931 min/max/cond_expr: [init_val,init_val,...,init_val]
3932 and no adjustments are needed.
3934 For example, for the following code:
3940 STMT is 's = s + a[i]', and the reduction variable is 's'.
3941 For a vector of 4 units, we want to return either [0,0,0,init_val],
3942 or [0,0,0,0] and let the caller know that it needs to adjust
3943 the result at the end by 'init_val'.
3945 FORNOW, we are using the 'adjust in epilog' scheme, because this way the
3946 initialization vector is simpler (same element in all entries), if
3947 ADJUSTMENT_DEF is not NULL, and Option2 otherwise.
3949 A cost model should help decide between these two schemes. */
3952 get_initial_def_for_reduction (gimple
*stmt
, tree init_val
,
3953 tree
*adjustment_def
)
3955 stmt_vec_info stmt_vinfo
= vinfo_for_stmt (stmt
);
3956 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_vinfo
);
3957 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
3958 tree scalar_type
= TREE_TYPE (init_val
);
3959 tree vectype
= get_vectype_for_scalar_type (scalar_type
);
3961 enum tree_code code
= gimple_assign_rhs_code (stmt
);
3966 bool nested_in_vect_loop
= false;
3968 REAL_VALUE_TYPE real_init_val
= dconst0
;
3969 int int_init_val
= 0;
3970 gimple
*def_stmt
= NULL
;
3972 gcc_assert (vectype
);
3973 nunits
= TYPE_VECTOR_SUBPARTS (vectype
);
3975 gcc_assert (POINTER_TYPE_P (scalar_type
) || INTEGRAL_TYPE_P (scalar_type
)
3976 || SCALAR_FLOAT_TYPE_P (scalar_type
));
3978 if (nested_in_vect_loop_p (loop
, stmt
))
3979 nested_in_vect_loop
= true;
3981 gcc_assert (loop
== (gimple_bb (stmt
))->loop_father
);
3983 /* In case of double reduction we only create a vector variable to be put
3984 in the reduction phi node. The actual statement creation is done in
3985 vect_create_epilog_for_reduction. */
3986 if (adjustment_def
&& nested_in_vect_loop
3987 && TREE_CODE (init_val
) == SSA_NAME
3988 && (def_stmt
= SSA_NAME_DEF_STMT (init_val
))
3989 && gimple_code (def_stmt
) == GIMPLE_PHI
3990 && flow_bb_inside_loop_p (loop
, gimple_bb (def_stmt
))
3991 && vinfo_for_stmt (def_stmt
)
3992 && STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def_stmt
))
3993 == vect_double_reduction_def
)
3995 *adjustment_def
= NULL
;
3996 return vect_create_destination_var (init_val
, vectype
);
3999 if (TREE_CONSTANT (init_val
))
4001 if (SCALAR_FLOAT_TYPE_P (scalar_type
))
4002 init_value
= build_real (scalar_type
, TREE_REAL_CST (init_val
));
4004 init_value
= build_int_cst (scalar_type
, TREE_INT_CST_LOW (init_val
));
4007 init_value
= init_val
;
4011 case WIDEN_SUM_EXPR
:
4020 /* ADJUSMENT_DEF is NULL when called from
4021 vect_create_epilog_for_reduction to vectorize double reduction. */
4024 if (nested_in_vect_loop
)
4025 *adjustment_def
= vect_get_vec_def_for_operand (init_val
, stmt
);
4027 *adjustment_def
= init_val
;
4030 if (code
== MULT_EXPR
)
4032 real_init_val
= dconst1
;
4036 if (code
== BIT_AND_EXPR
)
4039 if (SCALAR_FLOAT_TYPE_P (scalar_type
))
4040 def_for_init
= build_real (scalar_type
, real_init_val
);
4042 def_for_init
= build_int_cst (scalar_type
, int_init_val
);
4044 /* Create a vector of '0' or '1' except the first element. */
4045 elts
= XALLOCAVEC (tree
, nunits
);
4046 for (i
= nunits
- 2; i
>= 0; --i
)
4047 elts
[i
+ 1] = def_for_init
;
4049 /* Option1: the first element is '0' or '1' as well. */
4052 elts
[0] = def_for_init
;
4053 init_def
= build_vector (vectype
, elts
);
4057 /* Option2: the first element is INIT_VAL. */
4059 if (TREE_CONSTANT (init_val
))
4060 init_def
= build_vector (vectype
, elts
);
4063 vec
<constructor_elt
, va_gc
> *v
;
4064 vec_alloc (v
, nunits
);
4065 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, init_val
);
4066 for (i
= 1; i
< nunits
; ++i
)
4067 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, elts
[i
]);
4068 init_def
= build_constructor (vectype
, v
);
4078 *adjustment_def
= NULL_TREE
;
4079 if (STMT_VINFO_VEC_REDUCTION_TYPE (stmt_vinfo
) != COND_REDUCTION
)
4081 init_def
= vect_get_vec_def_for_operand (init_val
, stmt
);
4085 init_def
= build_vector_from_val (vectype
, init_value
);
4095 /* Function vect_create_epilog_for_reduction
4097 Create code at the loop-epilog to finalize the result of a reduction
4100 VECT_DEFS is list of vector of partial results, i.e., the lhs's of vector
4101 reduction statements.
4102 STMT is the scalar reduction stmt that is being vectorized.
4103 NCOPIES is > 1 in case the vectorization factor (VF) is bigger than the
4104 number of elements that we can fit in a vectype (nunits). In this case
4105 we have to generate more than one vector stmt - i.e - we need to "unroll"
4106 the vector stmt by a factor VF/nunits. For more details see documentation
4107 in vectorizable_operation.
4108 REDUC_CODE is the tree-code for the epilog reduction.
4109 REDUCTION_PHIS is a list of the phi-nodes that carry the reduction
4111 REDUC_INDEX is the index of the operand in the right hand side of the
4112 statement that is defined by REDUCTION_PHI.
4113 DOUBLE_REDUC is TRUE if double reduction phi nodes should be handled.
4114 SLP_NODE is an SLP node containing a group of reduction statements. The
4115 first one in this group is STMT.
4116 INDUCTION_INDEX is the index of the loop for condition reductions.
4117 Otherwise it is undefined.
4120 1. Creates the reduction def-use cycles: sets the arguments for
4122 The loop-entry argument is the vectorized initial-value of the reduction.
4123 The loop-latch argument is taken from VECT_DEFS - the vector of partial
4125 2. "Reduces" each vector of partial results VECT_DEFS into a single result,
4126 by applying the operation specified by REDUC_CODE if available, or by
4127 other means (whole-vector shifts or a scalar loop).
4128 The function also creates a new phi node at the loop exit to preserve
4129 loop-closed form, as illustrated below.
4131 The flow at the entry to this function:
4134 vec_def = phi <null, null> # REDUCTION_PHI
4135 VECT_DEF = vector_stmt # vectorized form of STMT
4136 s_loop = scalar_stmt # (scalar) STMT
4138 s_out0 = phi <s_loop> # (scalar) EXIT_PHI
4142 The above is transformed by this function into:
4145 vec_def = phi <vec_init, VECT_DEF> # REDUCTION_PHI
4146 VECT_DEF = vector_stmt # vectorized form of STMT
4147 s_loop = scalar_stmt # (scalar) STMT
4149 s_out0 = phi <s_loop> # (scalar) EXIT_PHI
4150 v_out1 = phi <VECT_DEF> # NEW_EXIT_PHI
4151 v_out2 = reduce <v_out1>
4152 s_out3 = extract_field <v_out2, 0>
4153 s_out4 = adjust_result <s_out3>
4159 vect_create_epilog_for_reduction (vec
<tree
> vect_defs
, gimple
*stmt
,
4160 int ncopies
, enum tree_code reduc_code
,
4161 vec
<gimple
*> reduction_phis
,
4162 int reduc_index
, bool double_reduc
,
4163 slp_tree slp_node
, tree induction_index
)
4165 stmt_vec_info stmt_info
= vinfo_for_stmt (stmt
);
4166 stmt_vec_info prev_phi_info
;
4169 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_info
);
4170 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
), *outer_loop
= NULL
;
4171 basic_block exit_bb
;
4174 gimple
*new_phi
= NULL
, *phi
;
4175 gimple_stmt_iterator exit_gsi
;
4177 tree new_temp
= NULL_TREE
, new_dest
, new_name
, new_scalar_dest
;
4178 gimple
*epilog_stmt
= NULL
;
4179 enum tree_code code
= gimple_assign_rhs_code (stmt
);
4182 tree adjustment_def
= NULL
;
4183 tree vec_initial_def
= NULL
;
4184 tree reduction_op
, expr
, def
, initial_def
= NULL
;
4185 tree orig_name
, scalar_result
;
4186 imm_use_iterator imm_iter
, phi_imm_iter
;
4187 use_operand_p use_p
, phi_use_p
;
4188 gimple
*use_stmt
, *orig_stmt
, *reduction_phi
= NULL
;
4189 bool nested_in_vect_loop
= false;
4190 auto_vec
<gimple
*> new_phis
;
4191 auto_vec
<gimple
*> inner_phis
;
4192 enum vect_def_type dt
= vect_unknown_def_type
;
4194 auto_vec
<tree
> scalar_results
;
4195 unsigned int group_size
= 1, k
, ratio
;
4196 auto_vec
<tree
> vec_initial_defs
;
4197 auto_vec
<gimple
*> phis
;
4198 bool slp_reduc
= false;
4199 tree new_phi_result
;
4200 gimple
*inner_phi
= NULL
;
4203 group_size
= SLP_TREE_SCALAR_STMTS (slp_node
).length ();
4205 if (nested_in_vect_loop_p (loop
, stmt
))
4209 nested_in_vect_loop
= true;
4210 gcc_assert (!slp_node
);
4213 reduction_op
= get_reduction_op (stmt
, reduc_index
);
4215 vectype
= get_vectype_for_scalar_type (TREE_TYPE (reduction_op
));
4216 gcc_assert (vectype
);
4217 mode
= TYPE_MODE (vectype
);
4219 /* 1. Create the reduction def-use cycle:
4220 Set the arguments of REDUCTION_PHIS, i.e., transform
4223 vec_def = phi <null, null> # REDUCTION_PHI
4224 VECT_DEF = vector_stmt # vectorized form of STMT
4230 vec_def = phi <vec_init, VECT_DEF> # REDUCTION_PHI
4231 VECT_DEF = vector_stmt # vectorized form of STMT
4234 (in case of SLP, do it for all the phis). */
4236 /* Get the loop-entry arguments. */
4238 vect_get_vec_defs (reduction_op
, NULL_TREE
, stmt
, &vec_initial_defs
,
4239 NULL
, slp_node
, reduc_index
);
4242 /* Get at the scalar def before the loop, that defines the initial value
4243 of the reduction variable. */
4244 gimple
*def_stmt
= SSA_NAME_DEF_STMT (reduction_op
);
4245 initial_def
= PHI_ARG_DEF_FROM_EDGE (def_stmt
,
4246 loop_preheader_edge (loop
));
4247 vec_initial_defs
.create (1);
4248 vec_initial_def
= get_initial_def_for_reduction (stmt
, initial_def
,
4250 vec_initial_defs
.quick_push (vec_initial_def
);
4253 /* Set phi nodes arguments. */
4254 FOR_EACH_VEC_ELT (reduction_phis
, i
, phi
)
4256 tree vec_init_def
, def
;
4258 vec_init_def
= force_gimple_operand (vec_initial_defs
[i
], &stmts
,
4260 gsi_insert_seq_on_edge_immediate (loop_preheader_edge (loop
), stmts
);
4262 for (j
= 0; j
< ncopies
; j
++)
4264 /* Set the loop-entry arg of the reduction-phi. */
4266 if (STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
)
4267 == INTEGER_INDUC_COND_REDUCTION
)
4269 /* Initialise the reduction phi to zero. This prevents initial
4270 values of non-zero interferring with the reduction op. */
4271 gcc_assert (ncopies
== 1);
4272 gcc_assert (i
== 0);
4274 tree vec_init_def_type
= TREE_TYPE (vec_init_def
);
4275 tree zero_vec
= build_zero_cst (vec_init_def_type
);
4277 add_phi_arg (as_a
<gphi
*> (phi
), zero_vec
,
4278 loop_preheader_edge (loop
), UNKNOWN_LOCATION
);
4281 add_phi_arg (as_a
<gphi
*> (phi
), vec_init_def
,
4282 loop_preheader_edge (loop
), UNKNOWN_LOCATION
);
4284 /* Set the loop-latch arg for the reduction-phi. */
4286 def
= vect_get_vec_def_for_stmt_copy (vect_unknown_def_type
, def
);
4288 add_phi_arg (as_a
<gphi
*> (phi
), def
, loop_latch_edge (loop
),
4291 if (dump_enabled_p ())
4293 dump_printf_loc (MSG_NOTE
, vect_location
,
4294 "transform reduction: created def-use cycle: ");
4295 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
, phi
, 0);
4296 dump_printf (MSG_NOTE
, "\n");
4297 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
, SSA_NAME_DEF_STMT (def
), 0);
4298 dump_printf (MSG_NOTE
, "\n");
4301 phi
= STMT_VINFO_RELATED_STMT (vinfo_for_stmt (phi
));
4305 /* 2. Create epilog code.
4306 The reduction epilog code operates across the elements of the vector
4307 of partial results computed by the vectorized loop.
4308 The reduction epilog code consists of:
4310 step 1: compute the scalar result in a vector (v_out2)
4311 step 2: extract the scalar result (s_out3) from the vector (v_out2)
4312 step 3: adjust the scalar result (s_out3) if needed.
4314 Step 1 can be accomplished using one the following three schemes:
4315 (scheme 1) using reduc_code, if available.
4316 (scheme 2) using whole-vector shifts, if available.
4317 (scheme 3) using a scalar loop. In this case steps 1+2 above are
4320 The overall epilog code looks like this:
4322 s_out0 = phi <s_loop> # original EXIT_PHI
4323 v_out1 = phi <VECT_DEF> # NEW_EXIT_PHI
4324 v_out2 = reduce <v_out1> # step 1
4325 s_out3 = extract_field <v_out2, 0> # step 2
4326 s_out4 = adjust_result <s_out3> # step 3
4328 (step 3 is optional, and steps 1 and 2 may be combined).
4329 Lastly, the uses of s_out0 are replaced by s_out4. */
4332 /* 2.1 Create new loop-exit-phis to preserve loop-closed form:
4333 v_out1 = phi <VECT_DEF>
4334 Store them in NEW_PHIS. */
4336 exit_bb
= single_exit (loop
)->dest
;
4337 prev_phi_info
= NULL
;
4338 new_phis
.create (vect_defs
.length ());
4339 FOR_EACH_VEC_ELT (vect_defs
, i
, def
)
4341 for (j
= 0; j
< ncopies
; j
++)
4343 tree new_def
= copy_ssa_name (def
);
4344 phi
= create_phi_node (new_def
, exit_bb
);
4345 set_vinfo_for_stmt (phi
, new_stmt_vec_info (phi
, loop_vinfo
));
4347 new_phis
.quick_push (phi
);
4350 def
= vect_get_vec_def_for_stmt_copy (dt
, def
);
4351 STMT_VINFO_RELATED_STMT (prev_phi_info
) = phi
;
4354 SET_PHI_ARG_DEF (phi
, single_exit (loop
)->dest_idx
, def
);
4355 prev_phi_info
= vinfo_for_stmt (phi
);
4359 /* The epilogue is created for the outer-loop, i.e., for the loop being
4360 vectorized. Create exit phis for the outer loop. */
4364 exit_bb
= single_exit (loop
)->dest
;
4365 inner_phis
.create (vect_defs
.length ());
4366 FOR_EACH_VEC_ELT (new_phis
, i
, phi
)
4368 tree new_result
= copy_ssa_name (PHI_RESULT (phi
));
4369 gphi
*outer_phi
= create_phi_node (new_result
, exit_bb
);
4370 SET_PHI_ARG_DEF (outer_phi
, single_exit (loop
)->dest_idx
,
4372 set_vinfo_for_stmt (outer_phi
, new_stmt_vec_info (outer_phi
,
4374 inner_phis
.quick_push (phi
);
4375 new_phis
[i
] = outer_phi
;
4376 prev_phi_info
= vinfo_for_stmt (outer_phi
);
4377 while (STMT_VINFO_RELATED_STMT (vinfo_for_stmt (phi
)))
4379 phi
= STMT_VINFO_RELATED_STMT (vinfo_for_stmt (phi
));
4380 new_result
= copy_ssa_name (PHI_RESULT (phi
));
4381 outer_phi
= create_phi_node (new_result
, exit_bb
);
4382 SET_PHI_ARG_DEF (outer_phi
, single_exit (loop
)->dest_idx
,
4384 set_vinfo_for_stmt (outer_phi
, new_stmt_vec_info (outer_phi
,
4386 STMT_VINFO_RELATED_STMT (prev_phi_info
) = outer_phi
;
4387 prev_phi_info
= vinfo_for_stmt (outer_phi
);
4392 exit_gsi
= gsi_after_labels (exit_bb
);
4394 /* 2.2 Get the relevant tree-code to use in the epilog for schemes 2,3
4395 (i.e. when reduc_code is not available) and in the final adjustment
4396 code (if needed). Also get the original scalar reduction variable as
4397 defined in the loop. In case STMT is a "pattern-stmt" (i.e. - it
4398 represents a reduction pattern), the tree-code and scalar-def are
4399 taken from the original stmt that the pattern-stmt (STMT) replaces.
4400 Otherwise (it is a regular reduction) - the tree-code and scalar-def
4401 are taken from STMT. */
4403 orig_stmt
= STMT_VINFO_RELATED_STMT (stmt_info
);
4406 /* Regular reduction */
4411 /* Reduction pattern */
4412 stmt_vec_info stmt_vinfo
= vinfo_for_stmt (orig_stmt
);
4413 gcc_assert (STMT_VINFO_IN_PATTERN_P (stmt_vinfo
));
4414 gcc_assert (STMT_VINFO_RELATED_STMT (stmt_vinfo
) == stmt
);
4417 code
= gimple_assign_rhs_code (orig_stmt
);
4418 /* For MINUS_EXPR the initial vector is [init_val,0,...,0], therefore,
4419 partial results are added and not subtracted. */
4420 if (code
== MINUS_EXPR
)
4423 scalar_dest
= gimple_assign_lhs (orig_stmt
);
4424 scalar_type
= TREE_TYPE (scalar_dest
);
4425 scalar_results
.create (group_size
);
4426 new_scalar_dest
= vect_create_destination_var (scalar_dest
, NULL
);
4427 bitsize
= TYPE_SIZE (scalar_type
);
4429 /* In case this is a reduction in an inner-loop while vectorizing an outer
4430 loop - we don't need to extract a single scalar result at the end of the
4431 inner-loop (unless it is double reduction, i.e., the use of reduction is
4432 outside the outer-loop). The final vector of partial results will be used
4433 in the vectorized outer-loop, or reduced to a scalar result at the end of
4435 if (nested_in_vect_loop
&& !double_reduc
)
4436 goto vect_finalize_reduction
;
4438 /* SLP reduction without reduction chain, e.g.,
4442 b2 = operation (b1) */
4443 slp_reduc
= (slp_node
&& !GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt
)));
4445 /* In case of reduction chain, e.g.,
4448 a3 = operation (a2),
4450 we may end up with more than one vector result. Here we reduce them to
4452 if (GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt
)))
4454 tree first_vect
= PHI_RESULT (new_phis
[0]);
4456 gassign
*new_vec_stmt
= NULL
;
4458 vec_dest
= vect_create_destination_var (scalar_dest
, vectype
);
4459 for (k
= 1; k
< new_phis
.length (); k
++)
4461 gimple
*next_phi
= new_phis
[k
];
4462 tree second_vect
= PHI_RESULT (next_phi
);
4464 tmp
= build2 (code
, vectype
, first_vect
, second_vect
);
4465 new_vec_stmt
= gimple_build_assign (vec_dest
, tmp
);
4466 first_vect
= make_ssa_name (vec_dest
, new_vec_stmt
);
4467 gimple_assign_set_lhs (new_vec_stmt
, first_vect
);
4468 gsi_insert_before (&exit_gsi
, new_vec_stmt
, GSI_SAME_STMT
);
4471 new_phi_result
= first_vect
;
4474 new_phis
.truncate (0);
4475 new_phis
.safe_push (new_vec_stmt
);
4479 new_phi_result
= PHI_RESULT (new_phis
[0]);
4481 if (STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
) == COND_REDUCTION
)
4483 /* For condition reductions, we have a vector (NEW_PHI_RESULT) containing
4484 various data values where the condition matched and another vector
4485 (INDUCTION_INDEX) containing all the indexes of those matches. We
4486 need to extract the last matching index (which will be the index with
4487 highest value) and use this to index into the data vector.
4488 For the case where there were no matches, the data vector will contain
4489 all default values and the index vector will be all zeros. */
4491 /* Get various versions of the type of the vector of indexes. */
4492 tree index_vec_type
= TREE_TYPE (induction_index
);
4493 gcc_checking_assert (TYPE_UNSIGNED (index_vec_type
));
4494 tree index_scalar_type
= TREE_TYPE (index_vec_type
);
4495 tree index_vec_cmp_type
= build_same_sized_truth_vector_type
4498 /* Get an unsigned integer version of the type of the data vector. */
4499 int scalar_precision
= GET_MODE_PRECISION (TYPE_MODE (scalar_type
));
4500 tree scalar_type_unsigned
= make_unsigned_type (scalar_precision
);
4501 tree vectype_unsigned
= build_vector_type
4502 (scalar_type_unsigned
, TYPE_VECTOR_SUBPARTS (vectype
));
4504 /* First we need to create a vector (ZERO_VEC) of zeros and another
4505 vector (MAX_INDEX_VEC) filled with the last matching index, which we
4506 can create using a MAX reduction and then expanding.
4507 In the case where the loop never made any matches, the max index will
4510 /* Vector of {0, 0, 0,...}. */
4511 tree zero_vec
= make_ssa_name (vectype
);
4512 tree zero_vec_rhs
= build_zero_cst (vectype
);
4513 gimple
*zero_vec_stmt
= gimple_build_assign (zero_vec
, zero_vec_rhs
);
4514 gsi_insert_before (&exit_gsi
, zero_vec_stmt
, GSI_SAME_STMT
);
4516 /* Find maximum value from the vector of found indexes. */
4517 tree max_index
= make_ssa_name (index_scalar_type
);
4518 gimple
*max_index_stmt
= gimple_build_assign (max_index
, REDUC_MAX_EXPR
,
4520 gsi_insert_before (&exit_gsi
, max_index_stmt
, GSI_SAME_STMT
);
4522 /* Vector of {max_index, max_index, max_index,...}. */
4523 tree max_index_vec
= make_ssa_name (index_vec_type
);
4524 tree max_index_vec_rhs
= build_vector_from_val (index_vec_type
,
4526 gimple
*max_index_vec_stmt
= gimple_build_assign (max_index_vec
,
4528 gsi_insert_before (&exit_gsi
, max_index_vec_stmt
, GSI_SAME_STMT
);
4530 /* Next we compare the new vector (MAX_INDEX_VEC) full of max indexes
4531 with the vector (INDUCTION_INDEX) of found indexes, choosing values
4532 from the data vector (NEW_PHI_RESULT) for matches, 0 (ZERO_VEC)
4533 otherwise. Only one value should match, resulting in a vector
4534 (VEC_COND) with one data value and the rest zeros.
4535 In the case where the loop never made any matches, every index will
4536 match, resulting in a vector with all data values (which will all be
4537 the default value). */
4539 /* Compare the max index vector to the vector of found indexes to find
4540 the position of the max value. */
4541 tree vec_compare
= make_ssa_name (index_vec_cmp_type
);
4542 gimple
*vec_compare_stmt
= gimple_build_assign (vec_compare
, EQ_EXPR
,
4545 gsi_insert_before (&exit_gsi
, vec_compare_stmt
, GSI_SAME_STMT
);
4547 /* Use the compare to choose either values from the data vector or
4549 tree vec_cond
= make_ssa_name (vectype
);
4550 gimple
*vec_cond_stmt
= gimple_build_assign (vec_cond
, VEC_COND_EXPR
,
4551 vec_compare
, new_phi_result
,
4553 gsi_insert_before (&exit_gsi
, vec_cond_stmt
, GSI_SAME_STMT
);
4555 /* Finally we need to extract the data value from the vector (VEC_COND)
4556 into a scalar (MATCHED_DATA_REDUC). Logically we want to do a OR
4557 reduction, but because this doesn't exist, we can use a MAX reduction
4558 instead. The data value might be signed or a float so we need to cast
4560 In the case where the loop never made any matches, the data values are
4561 all identical, and so will reduce down correctly. */
4563 /* Make the matched data values unsigned. */
4564 tree vec_cond_cast
= make_ssa_name (vectype_unsigned
);
4565 tree vec_cond_cast_rhs
= build1 (VIEW_CONVERT_EXPR
, vectype_unsigned
,
4567 gimple
*vec_cond_cast_stmt
= gimple_build_assign (vec_cond_cast
,
4570 gsi_insert_before (&exit_gsi
, vec_cond_cast_stmt
, GSI_SAME_STMT
);
4572 /* Reduce down to a scalar value. */
4573 tree data_reduc
= make_ssa_name (scalar_type_unsigned
);
4574 optab ot
= optab_for_tree_code (REDUC_MAX_EXPR
, vectype_unsigned
,
4576 gcc_assert (optab_handler (ot
, TYPE_MODE (vectype_unsigned
))
4577 != CODE_FOR_nothing
);
4578 gimple
*data_reduc_stmt
= gimple_build_assign (data_reduc
,
4581 gsi_insert_before (&exit_gsi
, data_reduc_stmt
, GSI_SAME_STMT
);
4583 /* Convert the reduced value back to the result type and set as the
4585 tree data_reduc_cast
= build1 (VIEW_CONVERT_EXPR
, scalar_type
,
4587 epilog_stmt
= gimple_build_assign (new_scalar_dest
, data_reduc_cast
);
4588 new_temp
= make_ssa_name (new_scalar_dest
, epilog_stmt
);
4589 gimple_assign_set_lhs (epilog_stmt
, new_temp
);
4590 gsi_insert_before (&exit_gsi
, epilog_stmt
, GSI_SAME_STMT
);
4591 scalar_results
.safe_push (new_temp
);
4594 /* 2.3 Create the reduction code, using one of the three schemes described
4595 above. In SLP we simply need to extract all the elements from the
4596 vector (without reducing them), so we use scalar shifts. */
4597 else if (reduc_code
!= ERROR_MARK
&& !slp_reduc
)
4602 /*** Case 1: Create:
4603 v_out2 = reduc_expr <v_out1> */
4605 if (dump_enabled_p ())
4606 dump_printf_loc (MSG_NOTE
, vect_location
,
4607 "Reduce using direct vector reduction.\n");
4609 vec_elem_type
= TREE_TYPE (TREE_TYPE (new_phi_result
));
4610 if (!useless_type_conversion_p (scalar_type
, vec_elem_type
))
4613 vect_create_destination_var (scalar_dest
, vec_elem_type
);
4614 tmp
= build1 (reduc_code
, vec_elem_type
, new_phi_result
);
4615 epilog_stmt
= gimple_build_assign (tmp_dest
, tmp
);
4616 new_temp
= make_ssa_name (tmp_dest
, epilog_stmt
);
4617 gimple_assign_set_lhs (epilog_stmt
, new_temp
);
4618 gsi_insert_before (&exit_gsi
, epilog_stmt
, GSI_SAME_STMT
);
4620 tmp
= build1 (NOP_EXPR
, scalar_type
, new_temp
);
4623 tmp
= build1 (reduc_code
, scalar_type
, new_phi_result
);
4625 epilog_stmt
= gimple_build_assign (new_scalar_dest
, tmp
);
4626 new_temp
= make_ssa_name (new_scalar_dest
, epilog_stmt
);
4627 gimple_assign_set_lhs (epilog_stmt
, new_temp
);
4628 gsi_insert_before (&exit_gsi
, epilog_stmt
, GSI_SAME_STMT
);
4630 if (STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
)
4631 == INTEGER_INDUC_COND_REDUCTION
)
4633 /* Earlier we set the initial value to be zero. Check the result
4634 and if it is zero then replace with the original initial
4636 tree zero
= build_zero_cst (scalar_type
);
4637 tree zcompare
= build2 (EQ_EXPR
, boolean_type_node
, new_temp
, zero
);
4639 tmp
= make_ssa_name (new_scalar_dest
);
4640 epilog_stmt
= gimple_build_assign (tmp
, COND_EXPR
, zcompare
,
4641 initial_def
, new_temp
);
4642 gsi_insert_before (&exit_gsi
, epilog_stmt
, GSI_SAME_STMT
);
4646 scalar_results
.safe_push (new_temp
);
4650 bool reduce_with_shift
= have_whole_vector_shift (mode
);
4651 int element_bitsize
= tree_to_uhwi (bitsize
);
4652 int vec_size_in_bits
= tree_to_uhwi (TYPE_SIZE (vectype
));
4655 /* Regardless of whether we have a whole vector shift, if we're
4656 emulating the operation via tree-vect-generic, we don't want
4657 to use it. Only the first round of the reduction is likely
4658 to still be profitable via emulation. */
4659 /* ??? It might be better to emit a reduction tree code here, so that
4660 tree-vect-generic can expand the first round via bit tricks. */
4661 if (!VECTOR_MODE_P (mode
))
4662 reduce_with_shift
= false;
4665 optab optab
= optab_for_tree_code (code
, vectype
, optab_default
);
4666 if (optab_handler (optab
, mode
) == CODE_FOR_nothing
)
4667 reduce_with_shift
= false;
4670 if (reduce_with_shift
&& !slp_reduc
)
4672 int nelements
= vec_size_in_bits
/ element_bitsize
;
4673 unsigned char *sel
= XALLOCAVEC (unsigned char, nelements
);
4677 tree zero_vec
= build_zero_cst (vectype
);
4678 /*** Case 2: Create:
4679 for (offset = nelements/2; offset >= 1; offset/=2)
4681 Create: va' = vec_shift <va, offset>
4682 Create: va = vop <va, va'>
4687 if (dump_enabled_p ())
4688 dump_printf_loc (MSG_NOTE
, vect_location
,
4689 "Reduce using vector shifts\n");
4691 vec_dest
= vect_create_destination_var (scalar_dest
, vectype
);
4692 new_temp
= new_phi_result
;
4693 for (elt_offset
= nelements
/ 2;
4697 calc_vec_perm_mask_for_shift (mode
, elt_offset
, sel
);
4698 tree mask
= vect_gen_perm_mask_any (vectype
, sel
);
4699 epilog_stmt
= gimple_build_assign (vec_dest
, VEC_PERM_EXPR
,
4700 new_temp
, zero_vec
, mask
);
4701 new_name
= make_ssa_name (vec_dest
, epilog_stmt
);
4702 gimple_assign_set_lhs (epilog_stmt
, new_name
);
4703 gsi_insert_before (&exit_gsi
, epilog_stmt
, GSI_SAME_STMT
);
4705 epilog_stmt
= gimple_build_assign (vec_dest
, code
, new_name
,
4707 new_temp
= make_ssa_name (vec_dest
, epilog_stmt
);
4708 gimple_assign_set_lhs (epilog_stmt
, new_temp
);
4709 gsi_insert_before (&exit_gsi
, epilog_stmt
, GSI_SAME_STMT
);
4712 /* 2.4 Extract the final scalar result. Create:
4713 s_out3 = extract_field <v_out2, bitpos> */
4715 if (dump_enabled_p ())
4716 dump_printf_loc (MSG_NOTE
, vect_location
,
4717 "extract scalar result\n");
4719 rhs
= build3 (BIT_FIELD_REF
, scalar_type
, new_temp
,
4720 bitsize
, bitsize_zero_node
);
4721 epilog_stmt
= gimple_build_assign (new_scalar_dest
, rhs
);
4722 new_temp
= make_ssa_name (new_scalar_dest
, epilog_stmt
);
4723 gimple_assign_set_lhs (epilog_stmt
, new_temp
);
4724 gsi_insert_before (&exit_gsi
, epilog_stmt
, GSI_SAME_STMT
);
4725 scalar_results
.safe_push (new_temp
);
4729 /*** Case 3: Create:
4730 s = extract_field <v_out2, 0>
4731 for (offset = element_size;
4732 offset < vector_size;
4733 offset += element_size;)
4735 Create: s' = extract_field <v_out2, offset>
4736 Create: s = op <s, s'> // For non SLP cases
4739 if (dump_enabled_p ())
4740 dump_printf_loc (MSG_NOTE
, vect_location
,
4741 "Reduce using scalar code.\n");
4743 vec_size_in_bits
= tree_to_uhwi (TYPE_SIZE (vectype
));
4744 FOR_EACH_VEC_ELT (new_phis
, i
, new_phi
)
4747 if (gimple_code (new_phi
) == GIMPLE_PHI
)
4748 vec_temp
= PHI_RESULT (new_phi
);
4750 vec_temp
= gimple_assign_lhs (new_phi
);
4751 tree rhs
= build3 (BIT_FIELD_REF
, scalar_type
, vec_temp
, bitsize
,
4753 epilog_stmt
= gimple_build_assign (new_scalar_dest
, rhs
);
4754 new_temp
= make_ssa_name (new_scalar_dest
, epilog_stmt
);
4755 gimple_assign_set_lhs (epilog_stmt
, new_temp
);
4756 gsi_insert_before (&exit_gsi
, epilog_stmt
, GSI_SAME_STMT
);
4758 /* In SLP we don't need to apply reduction operation, so we just
4759 collect s' values in SCALAR_RESULTS. */
4761 scalar_results
.safe_push (new_temp
);
4763 for (bit_offset
= element_bitsize
;
4764 bit_offset
< vec_size_in_bits
;
4765 bit_offset
+= element_bitsize
)
4767 tree bitpos
= bitsize_int (bit_offset
);
4768 tree rhs
= build3 (BIT_FIELD_REF
, scalar_type
, vec_temp
,
4771 epilog_stmt
= gimple_build_assign (new_scalar_dest
, rhs
);
4772 new_name
= make_ssa_name (new_scalar_dest
, epilog_stmt
);
4773 gimple_assign_set_lhs (epilog_stmt
, new_name
);
4774 gsi_insert_before (&exit_gsi
, epilog_stmt
, GSI_SAME_STMT
);
4778 /* In SLP we don't need to apply reduction operation, so
4779 we just collect s' values in SCALAR_RESULTS. */
4780 new_temp
= new_name
;
4781 scalar_results
.safe_push (new_name
);
4785 epilog_stmt
= gimple_build_assign (new_scalar_dest
, code
,
4786 new_name
, new_temp
);
4787 new_temp
= make_ssa_name (new_scalar_dest
, epilog_stmt
);
4788 gimple_assign_set_lhs (epilog_stmt
, new_temp
);
4789 gsi_insert_before (&exit_gsi
, epilog_stmt
, GSI_SAME_STMT
);
4794 /* The only case where we need to reduce scalar results in SLP, is
4795 unrolling. If the size of SCALAR_RESULTS is greater than
4796 GROUP_SIZE, we reduce them combining elements modulo
4800 tree res
, first_res
, new_res
;
4803 /* Reduce multiple scalar results in case of SLP unrolling. */
4804 for (j
= group_size
; scalar_results
.iterate (j
, &res
);
4807 first_res
= scalar_results
[j
% group_size
];
4808 new_stmt
= gimple_build_assign (new_scalar_dest
, code
,
4810 new_res
= make_ssa_name (new_scalar_dest
, new_stmt
);
4811 gimple_assign_set_lhs (new_stmt
, new_res
);
4812 gsi_insert_before (&exit_gsi
, new_stmt
, GSI_SAME_STMT
);
4813 scalar_results
[j
% group_size
] = new_res
;
4817 /* Not SLP - we have one scalar to keep in SCALAR_RESULTS. */
4818 scalar_results
.safe_push (new_temp
);
4822 vect_finalize_reduction
:
4827 /* 2.5 Adjust the final result by the initial value of the reduction
4828 variable. (When such adjustment is not needed, then
4829 'adjustment_def' is zero). For example, if code is PLUS we create:
4830 new_temp = loop_exit_def + adjustment_def */
4834 gcc_assert (!slp_reduc
);
4835 if (nested_in_vect_loop
)
4837 new_phi
= new_phis
[0];
4838 gcc_assert (TREE_CODE (TREE_TYPE (adjustment_def
)) == VECTOR_TYPE
);
4839 expr
= build2 (code
, vectype
, PHI_RESULT (new_phi
), adjustment_def
);
4840 new_dest
= vect_create_destination_var (scalar_dest
, vectype
);
4844 new_temp
= scalar_results
[0];
4845 gcc_assert (TREE_CODE (TREE_TYPE (adjustment_def
)) != VECTOR_TYPE
);
4846 expr
= build2 (code
, scalar_type
, new_temp
, adjustment_def
);
4847 new_dest
= vect_create_destination_var (scalar_dest
, scalar_type
);
4850 epilog_stmt
= gimple_build_assign (new_dest
, expr
);
4851 new_temp
= make_ssa_name (new_dest
, epilog_stmt
);
4852 gimple_assign_set_lhs (epilog_stmt
, new_temp
);
4853 gsi_insert_before (&exit_gsi
, epilog_stmt
, GSI_SAME_STMT
);
4854 if (nested_in_vect_loop
)
4856 set_vinfo_for_stmt (epilog_stmt
,
4857 new_stmt_vec_info (epilog_stmt
, loop_vinfo
));
4858 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (epilog_stmt
)) =
4859 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (new_phi
));
4862 scalar_results
.quick_push (new_temp
);
4864 scalar_results
[0] = new_temp
;
4867 scalar_results
[0] = new_temp
;
4869 new_phis
[0] = epilog_stmt
;
4872 /* 2.6 Handle the loop-exit phis. Replace the uses of scalar loop-exit
4873 phis with new adjusted scalar results, i.e., replace use <s_out0>
4878 s_out0 = phi <s_loop> # (scalar) EXIT_PHI
4879 v_out1 = phi <VECT_DEF> # NEW_EXIT_PHI
4880 v_out2 = reduce <v_out1>
4881 s_out3 = extract_field <v_out2, 0>
4882 s_out4 = adjust_result <s_out3>
4889 s_out0 = phi <s_loop> # (scalar) EXIT_PHI
4890 v_out1 = phi <VECT_DEF> # NEW_EXIT_PHI
4891 v_out2 = reduce <v_out1>
4892 s_out3 = extract_field <v_out2, 0>
4893 s_out4 = adjust_result <s_out3>
4898 /* In SLP reduction chain we reduce vector results into one vector if
4899 necessary, hence we set here GROUP_SIZE to 1. SCALAR_DEST is the LHS of
4900 the last stmt in the reduction chain, since we are looking for the loop
4902 if (GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt
)))
4904 gimple
*dest_stmt
= SLP_TREE_SCALAR_STMTS (slp_node
)[group_size
- 1];
4905 /* Handle reduction patterns. */
4906 if (STMT_VINFO_RELATED_STMT (vinfo_for_stmt (dest_stmt
)))
4907 dest_stmt
= STMT_VINFO_RELATED_STMT (vinfo_for_stmt (dest_stmt
));
4909 scalar_dest
= gimple_assign_lhs (dest_stmt
);
4913 /* In SLP we may have several statements in NEW_PHIS and REDUCTION_PHIS (in
4914 case that GROUP_SIZE is greater than vectorization factor). Therefore, we
4915 need to match SCALAR_RESULTS with corresponding statements. The first
4916 (GROUP_SIZE / number of new vector stmts) scalar results correspond to
4917 the first vector stmt, etc.
4918 (RATIO is equal to (GROUP_SIZE / number of new vector stmts)). */
4919 if (group_size
> new_phis
.length ())
4921 ratio
= group_size
/ new_phis
.length ();
4922 gcc_assert (!(group_size
% new_phis
.length ()));
4927 for (k
= 0; k
< group_size
; k
++)
4931 epilog_stmt
= new_phis
[k
/ ratio
];
4932 reduction_phi
= reduction_phis
[k
/ ratio
];
4934 inner_phi
= inner_phis
[k
/ ratio
];
4939 gimple
*current_stmt
= SLP_TREE_SCALAR_STMTS (slp_node
)[k
];
4941 orig_stmt
= STMT_VINFO_RELATED_STMT (vinfo_for_stmt (current_stmt
));
4942 /* SLP statements can't participate in patterns. */
4943 gcc_assert (!orig_stmt
);
4944 scalar_dest
= gimple_assign_lhs (current_stmt
);
4948 /* Find the loop-closed-use at the loop exit of the original scalar
4949 result. (The reduction result is expected to have two immediate uses -
4950 one at the latch block, and one at the loop exit). */
4951 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, scalar_dest
)
4952 if (!flow_bb_inside_loop_p (loop
, gimple_bb (USE_STMT (use_p
)))
4953 && !is_gimple_debug (USE_STMT (use_p
)))
4954 phis
.safe_push (USE_STMT (use_p
));
4956 /* While we expect to have found an exit_phi because of loop-closed-ssa
4957 form we can end up without one if the scalar cycle is dead. */
4959 FOR_EACH_VEC_ELT (phis
, i
, exit_phi
)
4963 stmt_vec_info exit_phi_vinfo
= vinfo_for_stmt (exit_phi
);
4966 /* FORNOW. Currently not supporting the case that an inner-loop
4967 reduction is not used in the outer-loop (but only outside the
4968 outer-loop), unless it is double reduction. */
4969 gcc_assert ((STMT_VINFO_RELEVANT_P (exit_phi_vinfo
)
4970 && !STMT_VINFO_LIVE_P (exit_phi_vinfo
))
4974 STMT_VINFO_VEC_STMT (exit_phi_vinfo
) = inner_phi
;
4976 STMT_VINFO_VEC_STMT (exit_phi_vinfo
) = epilog_stmt
;
4978 || STMT_VINFO_DEF_TYPE (exit_phi_vinfo
)
4979 != vect_double_reduction_def
)
4982 /* Handle double reduction:
4984 stmt1: s1 = phi <s0, s2> - double reduction phi (outer loop)
4985 stmt2: s3 = phi <s1, s4> - (regular) reduc phi (inner loop)
4986 stmt3: s4 = use (s3) - (regular) reduc stmt (inner loop)
4987 stmt4: s2 = phi <s4> - double reduction stmt (outer loop)
4989 At that point the regular reduction (stmt2 and stmt3) is
4990 already vectorized, as well as the exit phi node, stmt4.
4991 Here we vectorize the phi node of double reduction, stmt1, and
4992 update all relevant statements. */
4994 /* Go through all the uses of s2 to find double reduction phi
4995 node, i.e., stmt1 above. */
4996 orig_name
= PHI_RESULT (exit_phi
);
4997 FOR_EACH_IMM_USE_STMT (use_stmt
, imm_iter
, orig_name
)
4999 stmt_vec_info use_stmt_vinfo
;
5000 stmt_vec_info new_phi_vinfo
;
5001 tree vect_phi_init
, preheader_arg
, vect_phi_res
, init_def
;
5002 basic_block bb
= gimple_bb (use_stmt
);
5005 /* Check that USE_STMT is really double reduction phi
5007 if (gimple_code (use_stmt
) != GIMPLE_PHI
5008 || gimple_phi_num_args (use_stmt
) != 2
5009 || bb
->loop_father
!= outer_loop
)
5011 use_stmt_vinfo
= vinfo_for_stmt (use_stmt
);
5013 || STMT_VINFO_DEF_TYPE (use_stmt_vinfo
)
5014 != vect_double_reduction_def
)
5017 /* Create vector phi node for double reduction:
5018 vs1 = phi <vs0, vs2>
5019 vs1 was created previously in this function by a call to
5020 vect_get_vec_def_for_operand and is stored in
5022 vs2 is defined by INNER_PHI, the vectorized EXIT_PHI;
5023 vs0 is created here. */
5025 /* Create vector phi node. */
5026 vect_phi
= create_phi_node (vec_initial_def
, bb
);
5027 new_phi_vinfo
= new_stmt_vec_info (vect_phi
,
5028 loop_vec_info_for_loop (outer_loop
));
5029 set_vinfo_for_stmt (vect_phi
, new_phi_vinfo
);
5031 /* Create vs0 - initial def of the double reduction phi. */
5032 preheader_arg
= PHI_ARG_DEF_FROM_EDGE (use_stmt
,
5033 loop_preheader_edge (outer_loop
));
5034 init_def
= get_initial_def_for_reduction (stmt
,
5035 preheader_arg
, NULL
);
5036 vect_phi_init
= vect_init_vector (use_stmt
, init_def
,
5039 /* Update phi node arguments with vs0 and vs2. */
5040 add_phi_arg (vect_phi
, vect_phi_init
,
5041 loop_preheader_edge (outer_loop
),
5043 add_phi_arg (vect_phi
, PHI_RESULT (inner_phi
),
5044 loop_latch_edge (outer_loop
), UNKNOWN_LOCATION
);
5045 if (dump_enabled_p ())
5047 dump_printf_loc (MSG_NOTE
, vect_location
,
5048 "created double reduction phi node: ");
5049 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
, vect_phi
, 0);
5050 dump_printf (MSG_NOTE
, "\n");
5053 vect_phi_res
= PHI_RESULT (vect_phi
);
5055 /* Replace the use, i.e., set the correct vs1 in the regular
5056 reduction phi node. FORNOW, NCOPIES is always 1, so the
5057 loop is redundant. */
5058 use
= reduction_phi
;
5059 for (j
= 0; j
< ncopies
; j
++)
5061 edge pr_edge
= loop_preheader_edge (loop
);
5062 SET_PHI_ARG_DEF (use
, pr_edge
->dest_idx
, vect_phi_res
);
5063 use
= STMT_VINFO_RELATED_STMT (vinfo_for_stmt (use
));
5070 if (nested_in_vect_loop
)
5079 /* Find the loop-closed-use at the loop exit of the original scalar
5080 result. (The reduction result is expected to have two immediate uses,
5081 one at the latch block, and one at the loop exit). For double
5082 reductions we are looking for exit phis of the outer loop. */
5083 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, scalar_dest
)
5085 if (!flow_bb_inside_loop_p (loop
, gimple_bb (USE_STMT (use_p
))))
5087 if (!is_gimple_debug (USE_STMT (use_p
)))
5088 phis
.safe_push (USE_STMT (use_p
));
5092 if (double_reduc
&& gimple_code (USE_STMT (use_p
)) == GIMPLE_PHI
)
5094 tree phi_res
= PHI_RESULT (USE_STMT (use_p
));
5096 FOR_EACH_IMM_USE_FAST (phi_use_p
, phi_imm_iter
, phi_res
)
5098 if (!flow_bb_inside_loop_p (loop
,
5099 gimple_bb (USE_STMT (phi_use_p
)))
5100 && !is_gimple_debug (USE_STMT (phi_use_p
)))
5101 phis
.safe_push (USE_STMT (phi_use_p
));
5107 FOR_EACH_VEC_ELT (phis
, i
, exit_phi
)
5109 /* Replace the uses: */
5110 orig_name
= PHI_RESULT (exit_phi
);
5111 scalar_result
= scalar_results
[k
];
5112 FOR_EACH_IMM_USE_STMT (use_stmt
, imm_iter
, orig_name
)
5113 FOR_EACH_IMM_USE_ON_STMT (use_p
, imm_iter
)
5114 SET_USE (use_p
, scalar_result
);
5122 /* Function is_nonwrapping_integer_induction.
5124 Check if STMT (which is part of loop LOOP) both increments and
5125 does not cause overflow. */
5128 is_nonwrapping_integer_induction (gimple
*stmt
, struct loop
*loop
)
5130 stmt_vec_info stmt_vinfo
= vinfo_for_stmt (stmt
);
5131 tree base
= PHI_ARG_DEF_FROM_EDGE (stmt
, loop_preheader_edge (loop
));
5132 tree step
= STMT_VINFO_LOOP_PHI_EVOLUTION_PART (stmt_vinfo
);
5133 tree lhs_type
= TREE_TYPE (gimple_phi_result (stmt
));
5134 widest_int ni
, max_loop_value
, lhs_max
;
5135 bool overflow
= false;
5137 /* Make sure the loop is integer based. */
5138 if (TREE_CODE (base
) != INTEGER_CST
5139 || TREE_CODE (step
) != INTEGER_CST
)
5142 /* Check that the induction increments. */
5143 if (tree_int_cst_sgn (step
) == -1)
5146 /* Check that the max size of the loop will not wrap. */
5148 if (TYPE_OVERFLOW_UNDEFINED (lhs_type
))
5151 if (! max_stmt_executions (loop
, &ni
))
5154 max_loop_value
= wi::mul (wi::to_widest (step
), ni
, TYPE_SIGN (lhs_type
),
5159 max_loop_value
= wi::add (wi::to_widest (base
), max_loop_value
,
5160 TYPE_SIGN (lhs_type
), &overflow
);
5164 return (wi::min_precision (max_loop_value
, TYPE_SIGN (lhs_type
))
5165 <= TYPE_PRECISION (lhs_type
));
5168 /* Function vectorizable_reduction.
5170 Check if STMT performs a reduction operation that can be vectorized.
5171 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
5172 stmt to replace it, put it in VEC_STMT, and insert it at GSI.
5173 Return FALSE if not a vectorizable STMT, TRUE otherwise.
5175 This function also handles reduction idioms (patterns) that have been
5176 recognized in advance during vect_pattern_recog. In this case, STMT may be
5178 X = pattern_expr (arg0, arg1, ..., X)
5179 and it's STMT_VINFO_RELATED_STMT points to the last stmt in the original
5180 sequence that had been detected and replaced by the pattern-stmt (STMT).
5182 This function also handles reduction of condition expressions, for example:
5183 for (int i = 0; i < N; i++)
5186 This is handled by vectorising the loop and creating an additional vector
5187 containing the loop indexes for which "a[i] < value" was true. In the
5188 function epilogue this is reduced to a single max value and then used to
5189 index into the vector of results.
5191 In some cases of reduction patterns, the type of the reduction variable X is
5192 different than the type of the other arguments of STMT.
5193 In such cases, the vectype that is used when transforming STMT into a vector
5194 stmt is different than the vectype that is used to determine the
5195 vectorization factor, because it consists of a different number of elements
5196 than the actual number of elements that are being operated upon in parallel.
5198 For example, consider an accumulation of shorts into an int accumulator.
5199 On some targets it's possible to vectorize this pattern operating on 8
5200 shorts at a time (hence, the vectype for purposes of determining the
5201 vectorization factor should be V8HI); on the other hand, the vectype that
5202 is used to create the vector form is actually V4SI (the type of the result).
5204 Upon entry to this function, STMT_VINFO_VECTYPE records the vectype that
5205 indicates what is the actual level of parallelism (V8HI in the example), so
5206 that the right vectorization factor would be derived. This vectype
5207 corresponds to the type of arguments to the reduction stmt, and should *NOT*
5208 be used to create the vectorized stmt. The right vectype for the vectorized
5209 stmt is obtained from the type of the result X:
5210 get_vectype_for_scalar_type (TREE_TYPE (X))
5212 This means that, contrary to "regular" reductions (or "regular" stmts in
5213 general), the following equation:
5214 STMT_VINFO_VECTYPE == get_vectype_for_scalar_type (TREE_TYPE (X))
5215 does *NOT* necessarily hold for reduction patterns. */
5218 vectorizable_reduction (gimple
*stmt
, gimple_stmt_iterator
*gsi
,
5219 gimple
**vec_stmt
, slp_tree slp_node
)
5223 tree loop_vec_def0
= NULL_TREE
, loop_vec_def1
= NULL_TREE
;
5224 stmt_vec_info stmt_info
= vinfo_for_stmt (stmt
);
5225 tree vectype_out
= STMT_VINFO_VECTYPE (stmt_info
);
5226 tree vectype_in
= NULL_TREE
;
5227 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_info
);
5228 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
5229 enum tree_code code
, orig_code
, epilog_reduc_code
;
5230 machine_mode vec_mode
;
5232 optab optab
, reduc_optab
;
5233 tree new_temp
= NULL_TREE
;
5235 enum vect_def_type dt
;
5236 gphi
*new_phi
= NULL
;
5240 stmt_vec_info orig_stmt_info
;
5241 tree expr
= NULL_TREE
;
5245 stmt_vec_info prev_stmt_info
, prev_phi_info
;
5246 bool single_defuse_cycle
= false;
5247 tree reduc_def
= NULL_TREE
;
5248 gimple
*new_stmt
= NULL
;
5251 bool nested_cycle
= false, found_nested_cycle_def
= false;
5252 gimple
*reduc_def_stmt
= NULL
;
5253 bool double_reduc
= false, dummy
;
5255 struct loop
* def_stmt_loop
, *outer_loop
= NULL
;
5257 gimple
*def_arg_stmt
;
5258 auto_vec
<tree
> vec_oprnds0
;
5259 auto_vec
<tree
> vec_oprnds1
;
5260 auto_vec
<tree
> vect_defs
;
5261 auto_vec
<gimple
*> phis
;
5263 tree def0
, def1
, tem
, op0
, op1
= NULL_TREE
;
5264 bool first_p
= true;
5265 tree cr_index_scalar_type
= NULL_TREE
, cr_index_vector_type
= NULL_TREE
;
5266 bool cond_expr_is_nonwrapping_integer_induction
= false;
5268 /* In case of reduction chain we switch to the first stmt in the chain, but
5269 we don't update STMT_INFO, since only the last stmt is marked as reduction
5270 and has reduction properties. */
5271 if (GROUP_FIRST_ELEMENT (stmt_info
)
5272 && GROUP_FIRST_ELEMENT (stmt_info
) != stmt
)
5274 stmt
= GROUP_FIRST_ELEMENT (stmt_info
);
5278 if (nested_in_vect_loop_p (loop
, stmt
))
5282 nested_cycle
= true;
5285 /* 1. Is vectorizable reduction? */
5286 /* Not supportable if the reduction variable is used in the loop, unless
5287 it's a reduction chain. */
5288 if (STMT_VINFO_RELEVANT (stmt_info
) > vect_used_in_outer
5289 && !GROUP_FIRST_ELEMENT (stmt_info
))
5292 /* Reductions that are not used even in an enclosing outer-loop,
5293 are expected to be "live" (used out of the loop). */
5294 if (STMT_VINFO_RELEVANT (stmt_info
) == vect_unused_in_scope
5295 && !STMT_VINFO_LIVE_P (stmt_info
))
5298 /* Make sure it was already recognized as a reduction computation. */
5299 if (STMT_VINFO_DEF_TYPE (vinfo_for_stmt (stmt
)) != vect_reduction_def
5300 && STMT_VINFO_DEF_TYPE (vinfo_for_stmt (stmt
)) != vect_nested_cycle
)
5303 /* 2. Has this been recognized as a reduction pattern?
5305 Check if STMT represents a pattern that has been recognized
5306 in earlier analysis stages. For stmts that represent a pattern,
5307 the STMT_VINFO_RELATED_STMT field records the last stmt in
5308 the original sequence that constitutes the pattern. */
5310 orig_stmt
= STMT_VINFO_RELATED_STMT (vinfo_for_stmt (stmt
));
5313 orig_stmt_info
= vinfo_for_stmt (orig_stmt
);
5314 gcc_assert (STMT_VINFO_IN_PATTERN_P (orig_stmt_info
));
5315 gcc_assert (!STMT_VINFO_IN_PATTERN_P (stmt_info
));
5318 /* 3. Check the operands of the operation. The first operands are defined
5319 inside the loop body. The last operand is the reduction variable,
5320 which is defined by the loop-header-phi. */
5322 gcc_assert (is_gimple_assign (stmt
));
5325 switch (get_gimple_rhs_class (gimple_assign_rhs_code (stmt
)))
5327 case GIMPLE_SINGLE_RHS
:
5328 op_type
= TREE_OPERAND_LENGTH (gimple_assign_rhs1 (stmt
));
5329 if (op_type
== ternary_op
)
5331 tree rhs
= gimple_assign_rhs1 (stmt
);
5332 ops
[0] = TREE_OPERAND (rhs
, 0);
5333 ops
[1] = TREE_OPERAND (rhs
, 1);
5334 ops
[2] = TREE_OPERAND (rhs
, 2);
5335 code
= TREE_CODE (rhs
);
5341 case GIMPLE_BINARY_RHS
:
5342 code
= gimple_assign_rhs_code (stmt
);
5343 op_type
= TREE_CODE_LENGTH (code
);
5344 gcc_assert (op_type
== binary_op
);
5345 ops
[0] = gimple_assign_rhs1 (stmt
);
5346 ops
[1] = gimple_assign_rhs2 (stmt
);
5349 case GIMPLE_TERNARY_RHS
:
5350 code
= gimple_assign_rhs_code (stmt
);
5351 op_type
= TREE_CODE_LENGTH (code
);
5352 gcc_assert (op_type
== ternary_op
);
5353 ops
[0] = gimple_assign_rhs1 (stmt
);
5354 ops
[1] = gimple_assign_rhs2 (stmt
);
5355 ops
[2] = gimple_assign_rhs3 (stmt
);
5358 case GIMPLE_UNARY_RHS
:
5364 /* The default is that the reduction variable is the last in statement. */
5365 int reduc_index
= op_type
- 1;
5366 if (code
== MINUS_EXPR
)
5369 if (code
== COND_EXPR
&& slp_node
)
5372 scalar_dest
= gimple_assign_lhs (stmt
);
5373 scalar_type
= TREE_TYPE (scalar_dest
);
5374 if (!POINTER_TYPE_P (scalar_type
) && !INTEGRAL_TYPE_P (scalar_type
)
5375 && !SCALAR_FLOAT_TYPE_P (scalar_type
))
5378 /* Do not try to vectorize bit-precision reductions. */
5379 if ((TYPE_PRECISION (scalar_type
)
5380 != GET_MODE_PRECISION (TYPE_MODE (scalar_type
))))
5383 /* All uses but the last are expected to be defined in the loop.
5384 The last use is the reduction variable. In case of nested cycle this
5385 assumption is not true: we use reduc_index to record the index of the
5386 reduction variable. */
5387 for (i
= 0; i
< op_type
; i
++)
5389 if (i
== reduc_index
)
5392 /* The condition of COND_EXPR is checked in vectorizable_condition(). */
5393 if (i
== 0 && code
== COND_EXPR
)
5396 is_simple_use
= vect_is_simple_use (ops
[i
], loop_vinfo
,
5397 &def_stmt
, &dt
, &tem
);
5400 gcc_assert (is_simple_use
);
5402 if (dt
!= vect_internal_def
5403 && dt
!= vect_external_def
5404 && dt
!= vect_constant_def
5405 && dt
!= vect_induction_def
5406 && !(dt
== vect_nested_cycle
&& nested_cycle
))
5409 if (dt
== vect_nested_cycle
)
5411 found_nested_cycle_def
= true;
5412 reduc_def_stmt
= def_stmt
;
5416 if (i
== 1 && code
== COND_EXPR
&& dt
== vect_induction_def
5417 && is_nonwrapping_integer_induction (def_stmt
, loop
))
5419 if (dump_enabled_p ())
5420 dump_printf_loc (MSG_NOTE
, vect_location
,
5421 "condition expression based on integer "
5423 cond_expr_is_nonwrapping_integer_induction
= true;
5427 is_simple_use
= vect_is_simple_use (ops
[reduc_index
], loop_vinfo
,
5428 &def_stmt
, &dt
, &tem
);
5431 gcc_assert (is_simple_use
);
5432 if (!found_nested_cycle_def
)
5433 reduc_def_stmt
= def_stmt
;
5435 if (reduc_def_stmt
&& gimple_code (reduc_def_stmt
) != GIMPLE_PHI
)
5438 if (!(dt
== vect_reduction_def
5439 || dt
== vect_nested_cycle
5440 || ((dt
== vect_internal_def
|| dt
== vect_external_def
5441 || dt
== vect_constant_def
|| dt
== vect_induction_def
)
5442 && nested_cycle
&& found_nested_cycle_def
)))
5444 /* For pattern recognized stmts, orig_stmt might be a reduction,
5445 but some helper statements for the pattern might not, or
5446 might be COND_EXPRs with reduction uses in the condition. */
5447 gcc_assert (orig_stmt
);
5451 gimple
*tmp
= vect_is_simple_reduction
5452 (loop_vinfo
, reduc_def_stmt
,
5453 !nested_cycle
, &dummy
, false,
5454 &STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
));
5456 if (cond_expr_is_nonwrapping_integer_induction
5457 && STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
) == COND_REDUCTION
)
5458 STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
) = INTEGER_INDUC_COND_REDUCTION
;
5461 gcc_assert (tmp
== orig_stmt
5462 || GROUP_FIRST_ELEMENT (vinfo_for_stmt (tmp
)) == orig_stmt
);
5464 /* We changed STMT to be the first stmt in reduction chain, hence we
5465 check that in this case the first element in the chain is STMT. */
5466 gcc_assert (stmt
== tmp
5467 || GROUP_FIRST_ELEMENT (vinfo_for_stmt (tmp
)) == stmt
);
5469 if (STMT_VINFO_LIVE_P (vinfo_for_stmt (reduc_def_stmt
)))
5472 if (slp_node
|| PURE_SLP_STMT (stmt_info
))
5475 ncopies
= (LOOP_VINFO_VECT_FACTOR (loop_vinfo
)
5476 / TYPE_VECTOR_SUBPARTS (vectype_in
));
5478 gcc_assert (ncopies
>= 1);
5480 vec_mode
= TYPE_MODE (vectype_in
);
5482 if (code
== COND_EXPR
)
5484 /* Only call during the analysis stage, otherwise we'll lose
5486 if (!vec_stmt
&& !vectorizable_condition (stmt
, gsi
, NULL
,
5487 ops
[reduc_index
], 0, NULL
))
5489 if (dump_enabled_p ())
5490 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
5491 "unsupported condition in reduction\n");
5497 /* 4. Supportable by target? */
5499 if (code
== LSHIFT_EXPR
|| code
== RSHIFT_EXPR
5500 || code
== LROTATE_EXPR
|| code
== RROTATE_EXPR
)
5502 /* Shifts and rotates are only supported by vectorizable_shifts,
5503 not vectorizable_reduction. */
5504 if (dump_enabled_p ())
5505 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
5506 "unsupported shift or rotation.\n");
5510 /* 4.1. check support for the operation in the loop */
5511 optab
= optab_for_tree_code (code
, vectype_in
, optab_default
);
5514 if (dump_enabled_p ())
5515 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
5521 if (optab_handler (optab
, vec_mode
) == CODE_FOR_nothing
)
5523 if (dump_enabled_p ())
5524 dump_printf (MSG_NOTE
, "op not supported by target.\n");
5526 if (GET_MODE_SIZE (vec_mode
) != UNITS_PER_WORD
5527 || LOOP_VINFO_VECT_FACTOR (loop_vinfo
)
5528 < vect_min_worthwhile_factor (code
))
5531 if (dump_enabled_p ())
5532 dump_printf (MSG_NOTE
, "proceeding using word mode.\n");
5535 /* Worthwhile without SIMD support? */
5536 if (!VECTOR_MODE_P (TYPE_MODE (vectype_in
))
5537 && LOOP_VINFO_VECT_FACTOR (loop_vinfo
)
5538 < vect_min_worthwhile_factor (code
))
5540 if (dump_enabled_p ())
5541 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
5542 "not worthwhile without SIMD support.\n");
5548 /* 4.2. Check support for the epilog operation.
5550 If STMT represents a reduction pattern, then the type of the
5551 reduction variable may be different than the type of the rest
5552 of the arguments. For example, consider the case of accumulation
5553 of shorts into an int accumulator; The original code:
5554 S1: int_a = (int) short_a;
5555 orig_stmt-> S2: int_acc = plus <int_a ,int_acc>;
5558 STMT: int_acc = widen_sum <short_a, int_acc>
5561 1. The tree-code that is used to create the vector operation in the
5562 epilog code (that reduces the partial results) is not the
5563 tree-code of STMT, but is rather the tree-code of the original
5564 stmt from the pattern that STMT is replacing. I.e, in the example
5565 above we want to use 'widen_sum' in the loop, but 'plus' in the
5567 2. The type (mode) we use to check available target support
5568 for the vector operation to be created in the *epilog*, is
5569 determined by the type of the reduction variable (in the example
5570 above we'd check this: optab_handler (plus_optab, vect_int_mode])).
5571 However the type (mode) we use to check available target support
5572 for the vector operation to be created *inside the loop*, is
5573 determined by the type of the other arguments to STMT (in the
5574 example we'd check this: optab_handler (widen_sum_optab,
5577 This is contrary to "regular" reductions, in which the types of all
5578 the arguments are the same as the type of the reduction variable.
5579 For "regular" reductions we can therefore use the same vector type
5580 (and also the same tree-code) when generating the epilog code and
5581 when generating the code inside the loop. */
5585 /* This is a reduction pattern: get the vectype from the type of the
5586 reduction variable, and get the tree-code from orig_stmt. */
5587 gcc_assert (STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
)
5588 == TREE_CODE_REDUCTION
);
5589 orig_code
= gimple_assign_rhs_code (orig_stmt
);
5590 gcc_assert (vectype_out
);
5591 vec_mode
= TYPE_MODE (vectype_out
);
5595 /* Regular reduction: use the same vectype and tree-code as used for
5596 the vector code inside the loop can be used for the epilog code. */
5599 if (code
== MINUS_EXPR
)
5600 orig_code
= PLUS_EXPR
;
5602 /* For simple condition reductions, replace with the actual expression
5603 we want to base our reduction around. */
5604 if (STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
)
5605 == INTEGER_INDUC_COND_REDUCTION
)
5606 orig_code
= MAX_EXPR
;
5611 def_bb
= gimple_bb (reduc_def_stmt
);
5612 def_stmt_loop
= def_bb
->loop_father
;
5613 def_arg
= PHI_ARG_DEF_FROM_EDGE (reduc_def_stmt
,
5614 loop_preheader_edge (def_stmt_loop
));
5615 if (TREE_CODE (def_arg
) == SSA_NAME
5616 && (def_arg_stmt
= SSA_NAME_DEF_STMT (def_arg
))
5617 && gimple_code (def_arg_stmt
) == GIMPLE_PHI
5618 && flow_bb_inside_loop_p (outer_loop
, gimple_bb (def_arg_stmt
))
5619 && vinfo_for_stmt (def_arg_stmt
)
5620 && STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def_arg_stmt
))
5621 == vect_double_reduction_def
)
5622 double_reduc
= true;
5625 epilog_reduc_code
= ERROR_MARK
;
5627 if (STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
) == TREE_CODE_REDUCTION
5628 || STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
)
5629 == INTEGER_INDUC_COND_REDUCTION
)
5631 if (reduction_code_for_scalar_code (orig_code
, &epilog_reduc_code
))
5633 reduc_optab
= optab_for_tree_code (epilog_reduc_code
, vectype_out
,
5637 if (dump_enabled_p ())
5638 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
5639 "no optab for reduction.\n");
5641 epilog_reduc_code
= ERROR_MARK
;
5643 else if (optab_handler (reduc_optab
, vec_mode
) == CODE_FOR_nothing
)
5645 optab
= scalar_reduc_to_vector (reduc_optab
, vectype_out
);
5646 if (optab_handler (optab
, vec_mode
) == CODE_FOR_nothing
)
5648 if (dump_enabled_p ())
5649 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
5650 "reduc op not supported by target.\n");
5652 epilog_reduc_code
= ERROR_MARK
;
5656 /* When epilog_reduc_code is ERROR_MARK then a reduction will be
5657 generated in the epilog using multiple expressions. This does not
5658 work for condition reductions. */
5659 if (epilog_reduc_code
== ERROR_MARK
5660 && STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
)
5661 == INTEGER_INDUC_COND_REDUCTION
)
5663 if (dump_enabled_p ())
5664 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
5665 "no reduc code for scalar code.\n");
5671 if (!nested_cycle
|| double_reduc
)
5673 if (dump_enabled_p ())
5674 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
5675 "no reduc code for scalar code.\n");
5683 int scalar_precision
= GET_MODE_PRECISION (TYPE_MODE (scalar_type
));
5684 cr_index_scalar_type
= make_unsigned_type (scalar_precision
);
5685 cr_index_vector_type
= build_vector_type
5686 (cr_index_scalar_type
, TYPE_VECTOR_SUBPARTS (vectype_out
));
5688 epilog_reduc_code
= REDUC_MAX_EXPR
;
5689 optab
= optab_for_tree_code (REDUC_MAX_EXPR
, cr_index_vector_type
,
5691 if (optab_handler (optab
, TYPE_MODE (cr_index_vector_type
))
5692 == CODE_FOR_nothing
)
5694 if (dump_enabled_p ())
5695 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
5696 "reduc max op not supported by target.\n");
5702 || STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
) == COND_REDUCTION
5703 || STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
)
5704 == INTEGER_INDUC_COND_REDUCTION
)
5707 if (dump_enabled_p ())
5708 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
5709 "multiple types in double reduction or condition "
5714 /* In case of widenning multiplication by a constant, we update the type
5715 of the constant to be the type of the other operand. We check that the
5716 constant fits the type in the pattern recognition pass. */
5717 if (code
== DOT_PROD_EXPR
5718 && !types_compatible_p (TREE_TYPE (ops
[0]), TREE_TYPE (ops
[1])))
5720 if (TREE_CODE (ops
[0]) == INTEGER_CST
)
5721 ops
[0] = fold_convert (TREE_TYPE (ops
[1]), ops
[0]);
5722 else if (TREE_CODE (ops
[1]) == INTEGER_CST
)
5723 ops
[1] = fold_convert (TREE_TYPE (ops
[0]), ops
[1]);
5726 if (dump_enabled_p ())
5727 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
5728 "invalid types in dot-prod\n");
5734 if (STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
) == COND_REDUCTION
)
5738 if (! max_loop_iterations (loop
, &ni
))
5740 if (dump_enabled_p ())
5741 dump_printf_loc (MSG_NOTE
, vect_location
,
5742 "loop count not known, cannot create cond "
5746 /* Convert backedges to iterations. */
5749 /* The additional index will be the same type as the condition. Check
5750 that the loop can fit into this less one (because we'll use up the
5751 zero slot for when there are no matches). */
5752 tree max_index
= TYPE_MAX_VALUE (cr_index_scalar_type
);
5753 if (wi::geu_p (ni
, wi::to_widest (max_index
)))
5755 if (dump_enabled_p ())
5756 dump_printf_loc (MSG_NOTE
, vect_location
,
5757 "loop size is greater than data size.\n");
5762 if (!vec_stmt
) /* transformation not required. */
5765 && !vect_model_reduction_cost (stmt_info
, epilog_reduc_code
, ncopies
,
5768 STMT_VINFO_TYPE (stmt_info
) = reduc_vec_info_type
;
5774 if (dump_enabled_p ())
5775 dump_printf_loc (MSG_NOTE
, vect_location
, "transform reduction.\n");
5777 /* FORNOW: Multiple types are not supported for condition. */
5778 if (code
== COND_EXPR
)
5779 gcc_assert (ncopies
== 1);
5781 /* Create the destination vector */
5782 vec_dest
= vect_create_destination_var (scalar_dest
, vectype_out
);
5784 /* In case the vectorization factor (VF) is bigger than the number
5785 of elements that we can fit in a vectype (nunits), we have to generate
5786 more than one vector stmt - i.e - we need to "unroll" the
5787 vector stmt by a factor VF/nunits. For more details see documentation
5788 in vectorizable_operation. */
5790 /* If the reduction is used in an outer loop we need to generate
5791 VF intermediate results, like so (e.g. for ncopies=2):
5796 (i.e. we generate VF results in 2 registers).
5797 In this case we have a separate def-use cycle for each copy, and therefore
5798 for each copy we get the vector def for the reduction variable from the
5799 respective phi node created for this copy.
5801 Otherwise (the reduction is unused in the loop nest), we can combine
5802 together intermediate results, like so (e.g. for ncopies=2):
5806 (i.e. we generate VF/2 results in a single register).
5807 In this case for each copy we get the vector def for the reduction variable
5808 from the vectorized reduction operation generated in the previous iteration.
5811 if (STMT_VINFO_RELEVANT (stmt_info
) == vect_unused_in_scope
)
5813 single_defuse_cycle
= true;
5817 epilog_copies
= ncopies
;
5819 prev_stmt_info
= NULL
;
5820 prev_phi_info
= NULL
;
5822 vec_num
= SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node
);
5826 vec_oprnds0
.create (1);
5827 if (op_type
== ternary_op
)
5828 vec_oprnds1
.create (1);
5831 phis
.create (vec_num
);
5832 vect_defs
.create (vec_num
);
5834 vect_defs
.quick_push (NULL_TREE
);
5836 for (j
= 0; j
< ncopies
; j
++)
5838 if (j
== 0 || !single_defuse_cycle
)
5840 for (i
= 0; i
< vec_num
; i
++)
5842 /* Create the reduction-phi that defines the reduction
5844 new_phi
= create_phi_node (vec_dest
, loop
->header
);
5845 set_vinfo_for_stmt (new_phi
,
5846 new_stmt_vec_info (new_phi
, loop_vinfo
));
5847 if (j
== 0 || slp_node
)
5848 phis
.quick_push (new_phi
);
5852 if (code
== COND_EXPR
)
5854 gcc_assert (!slp_node
);
5855 vectorizable_condition (stmt
, gsi
, vec_stmt
,
5856 PHI_RESULT (phis
[0]),
5858 /* Multiple types are not supported for condition. */
5865 op0
= ops
[!reduc_index
];
5866 if (op_type
== ternary_op
)
5868 if (reduc_index
== 0)
5875 vect_get_vec_defs (op0
, op1
, stmt
, &vec_oprnds0
, &vec_oprnds1
,
5879 loop_vec_def0
= vect_get_vec_def_for_operand (ops
[!reduc_index
],
5881 vec_oprnds0
.quick_push (loop_vec_def0
);
5882 if (op_type
== ternary_op
)
5884 loop_vec_def1
= vect_get_vec_def_for_operand (op1
, stmt
);
5885 vec_oprnds1
.quick_push (loop_vec_def1
);
5893 enum vect_def_type dt
;
5896 vect_is_simple_use (ops
[!reduc_index
], loop_vinfo
,
5898 loop_vec_def0
= vect_get_vec_def_for_stmt_copy (dt
,
5900 vec_oprnds0
[0] = loop_vec_def0
;
5901 if (op_type
== ternary_op
)
5903 vect_is_simple_use (op1
, loop_vinfo
, &dummy_stmt
, &dt
);
5904 loop_vec_def1
= vect_get_vec_def_for_stmt_copy (dt
,
5906 vec_oprnds1
[0] = loop_vec_def1
;
5910 if (single_defuse_cycle
)
5911 reduc_def
= gimple_assign_lhs (new_stmt
);
5913 STMT_VINFO_RELATED_STMT (prev_phi_info
) = new_phi
;
5916 FOR_EACH_VEC_ELT (vec_oprnds0
, i
, def0
)
5919 reduc_def
= PHI_RESULT (phis
[i
]);
5922 if (!single_defuse_cycle
|| j
== 0)
5923 reduc_def
= PHI_RESULT (new_phi
);
5926 def1
= ((op_type
== ternary_op
)
5927 ? vec_oprnds1
[i
] : NULL
);
5928 if (op_type
== binary_op
)
5930 if (reduc_index
== 0)
5931 expr
= build2 (code
, vectype_out
, reduc_def
, def0
);
5933 expr
= build2 (code
, vectype_out
, def0
, reduc_def
);
5937 if (reduc_index
== 0)
5938 expr
= build3 (code
, vectype_out
, reduc_def
, def0
, def1
);
5941 if (reduc_index
== 1)
5942 expr
= build3 (code
, vectype_out
, def0
, reduc_def
, def1
);
5944 expr
= build3 (code
, vectype_out
, def0
, def1
, reduc_def
);
5948 new_stmt
= gimple_build_assign (vec_dest
, expr
);
5949 new_temp
= make_ssa_name (vec_dest
, new_stmt
);
5950 gimple_assign_set_lhs (new_stmt
, new_temp
);
5951 vect_finish_stmt_generation (stmt
, new_stmt
, gsi
);
5955 SLP_TREE_VEC_STMTS (slp_node
).quick_push (new_stmt
);
5956 vect_defs
.quick_push (new_temp
);
5959 vect_defs
[0] = new_temp
;
5966 STMT_VINFO_VEC_STMT (stmt_info
) = *vec_stmt
= new_stmt
;
5968 STMT_VINFO_RELATED_STMT (prev_stmt_info
) = new_stmt
;
5970 prev_stmt_info
= vinfo_for_stmt (new_stmt
);
5971 prev_phi_info
= vinfo_for_stmt (new_phi
);
5974 tree indx_before_incr
, indx_after_incr
, cond_name
= NULL
;
5976 /* Finalize the reduction-phi (set its arguments) and create the
5977 epilog reduction code. */
5978 if ((!single_defuse_cycle
|| code
== COND_EXPR
) && !slp_node
)
5980 new_temp
= gimple_assign_lhs (*vec_stmt
);
5981 vect_defs
[0] = new_temp
;
5983 /* For cond reductions we want to create a new vector (INDEX_COND_EXPR)
5984 which is updated with the current index of the loop for every match of
5985 the original loop's cond_expr (VEC_STMT). This results in a vector
5986 containing the last time the condition passed for that vector lane.
5987 The first match will be a 1 to allow 0 to be used for non-matching
5988 indexes. If there are no matches at all then the vector will be all
5990 if (STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
) == COND_REDUCTION
)
5992 int nunits_out
= TYPE_VECTOR_SUBPARTS (vectype_out
);
5995 gcc_assert (gimple_assign_rhs_code (*vec_stmt
) == VEC_COND_EXPR
);
5997 /* First we create a simple vector induction variable which starts
5998 with the values {1,2,3,...} (SERIES_VECT) and increments by the
5999 vector size (STEP). */
6001 /* Create a {1,2,3,...} vector. */
6002 tree
*vtemp
= XALLOCAVEC (tree
, nunits_out
);
6003 for (k
= 0; k
< nunits_out
; ++k
)
6004 vtemp
[k
] = build_int_cst (cr_index_scalar_type
, k
+ 1);
6005 tree series_vect
= build_vector (cr_index_vector_type
, vtemp
);
6007 /* Create a vector of the step value. */
6008 tree step
= build_int_cst (cr_index_scalar_type
, nunits_out
);
6009 tree vec_step
= build_vector_from_val (cr_index_vector_type
, step
);
6011 /* Create an induction variable. */
6012 gimple_stmt_iterator incr_gsi
;
6014 standard_iv_increment_position (loop
, &incr_gsi
, &insert_after
);
6015 create_iv (series_vect
, vec_step
, NULL_TREE
, loop
, &incr_gsi
,
6016 insert_after
, &indx_before_incr
, &indx_after_incr
);
6018 /* Next create a new phi node vector (NEW_PHI_TREE) which starts
6019 filled with zeros (VEC_ZERO). */
6021 /* Create a vector of 0s. */
6022 tree zero
= build_zero_cst (cr_index_scalar_type
);
6023 tree vec_zero
= build_vector_from_val (cr_index_vector_type
, zero
);
6025 /* Create a vector phi node. */
6026 tree new_phi_tree
= make_ssa_name (cr_index_vector_type
);
6027 new_phi
= create_phi_node (new_phi_tree
, loop
->header
);
6028 set_vinfo_for_stmt (new_phi
,
6029 new_stmt_vec_info (new_phi
, loop_vinfo
));
6030 add_phi_arg (new_phi
, vec_zero
, loop_preheader_edge (loop
),
6033 /* Now take the condition from the loops original cond_expr
6034 (VEC_STMT) and produce a new cond_expr (INDEX_COND_EXPR) which for
6035 every match uses values from the induction variable
6036 (INDEX_BEFORE_INCR) otherwise uses values from the phi node
6038 Finally, we update the phi (NEW_PHI_TREE) to take the value of
6039 the new cond_expr (INDEX_COND_EXPR). */
6041 /* Turn the condition from vec_stmt into an ssa name. */
6042 gimple_stmt_iterator vec_stmt_gsi
= gsi_for_stmt (*vec_stmt
);
6043 tree ccompare
= gimple_assign_rhs1 (*vec_stmt
);
6044 tree ccompare_name
= make_ssa_name (TREE_TYPE (ccompare
));
6045 gimple
*ccompare_stmt
= gimple_build_assign (ccompare_name
,
6047 gsi_insert_before (&vec_stmt_gsi
, ccompare_stmt
, GSI_SAME_STMT
);
6048 gimple_assign_set_rhs1 (*vec_stmt
, ccompare_name
);
6049 update_stmt (*vec_stmt
);
6051 /* Create a conditional, where the condition is taken from vec_stmt
6052 (CCOMPARE_NAME), then is the induction index (INDEX_BEFORE_INCR)
6053 and else is the phi (NEW_PHI_TREE). */
6054 tree index_cond_expr
= build3 (VEC_COND_EXPR
, cr_index_vector_type
,
6055 ccompare_name
, indx_before_incr
,
6057 cond_name
= make_ssa_name (cr_index_vector_type
);
6058 gimple
*index_condition
= gimple_build_assign (cond_name
,
6060 gsi_insert_before (&incr_gsi
, index_condition
, GSI_SAME_STMT
);
6061 stmt_vec_info index_vec_info
= new_stmt_vec_info (index_condition
,
6063 STMT_VINFO_VECTYPE (index_vec_info
) = cr_index_vector_type
;
6064 set_vinfo_for_stmt (index_condition
, index_vec_info
);
6066 /* Update the phi with the vec cond. */
6067 add_phi_arg (new_phi
, cond_name
, loop_latch_edge (loop
),
6072 vect_create_epilog_for_reduction (vect_defs
, stmt
, epilog_copies
,
6073 epilog_reduc_code
, phis
, reduc_index
,
6074 double_reduc
, slp_node
, cond_name
);
6079 /* Function vect_min_worthwhile_factor.
6081 For a loop where we could vectorize the operation indicated by CODE,
6082 return the minimum vectorization factor that makes it worthwhile
6083 to use generic vectors. */
6085 vect_min_worthwhile_factor (enum tree_code code
)
6106 /* Function vectorizable_induction
6108 Check if PHI performs an induction computation that can be vectorized.
6109 If VEC_STMT is also passed, vectorize the induction PHI: create a vectorized
6110 phi to replace it, put it in VEC_STMT, and add it to the same basic block.
6111 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
6114 vectorizable_induction (gimple
*phi
,
6115 gimple_stmt_iterator
*gsi ATTRIBUTE_UNUSED
,
6118 stmt_vec_info stmt_info
= vinfo_for_stmt (phi
);
6119 tree vectype
= STMT_VINFO_VECTYPE (stmt_info
);
6120 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_info
);
6121 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
6122 int nunits
= TYPE_VECTOR_SUBPARTS (vectype
);
6123 int ncopies
= LOOP_VINFO_VECT_FACTOR (loop_vinfo
) / nunits
;
6126 gcc_assert (ncopies
>= 1);
6127 /* FORNOW. These restrictions should be relaxed. */
6128 if (nested_in_vect_loop_p (loop
, phi
))
6130 imm_use_iterator imm_iter
;
6131 use_operand_p use_p
;
6138 if (dump_enabled_p ())
6139 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
6140 "multiple types in nested loop.\n");
6145 latch_e
= loop_latch_edge (loop
->inner
);
6146 loop_arg
= PHI_ARG_DEF_FROM_EDGE (phi
, latch_e
);
6147 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, loop_arg
)
6149 gimple
*use_stmt
= USE_STMT (use_p
);
6150 if (is_gimple_debug (use_stmt
))
6153 if (!flow_bb_inside_loop_p (loop
->inner
, gimple_bb (use_stmt
)))
6155 exit_phi
= use_stmt
;
6161 stmt_vec_info exit_phi_vinfo
= vinfo_for_stmt (exit_phi
);
6162 if (!(STMT_VINFO_RELEVANT_P (exit_phi_vinfo
)
6163 && !STMT_VINFO_LIVE_P (exit_phi_vinfo
)))
6165 if (dump_enabled_p ())
6166 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
6167 "inner-loop induction only used outside "
6168 "of the outer vectorized loop.\n");
6174 if (!STMT_VINFO_RELEVANT_P (stmt_info
))
6177 /* FORNOW: SLP not supported. */
6178 if (STMT_SLP_TYPE (stmt_info
))
6181 gcc_assert (STMT_VINFO_DEF_TYPE (stmt_info
) == vect_induction_def
);
6183 if (gimple_code (phi
) != GIMPLE_PHI
)
6186 if (!vec_stmt
) /* transformation not required. */
6188 STMT_VINFO_TYPE (stmt_info
) = induc_vec_info_type
;
6189 if (dump_enabled_p ())
6190 dump_printf_loc (MSG_NOTE
, vect_location
,
6191 "=== vectorizable_induction ===\n");
6192 vect_model_induction_cost (stmt_info
, ncopies
);
6198 if (dump_enabled_p ())
6199 dump_printf_loc (MSG_NOTE
, vect_location
, "transform induction phi.\n");
6201 vec_def
= get_initial_def_for_induction (phi
);
6202 *vec_stmt
= SSA_NAME_DEF_STMT (vec_def
);
6206 /* Function vectorizable_live_operation.
6208 STMT computes a value that is used outside the loop. Check if
6209 it can be supported. */
6212 vectorizable_live_operation (gimple
*stmt
,
6213 gimple_stmt_iterator
*gsi ATTRIBUTE_UNUSED
,
6216 stmt_vec_info stmt_info
= vinfo_for_stmt (stmt
);
6217 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_info
);
6218 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
6223 gcc_assert (STMT_VINFO_LIVE_P (stmt_info
));
6225 if (STMT_VINFO_DEF_TYPE (stmt_info
) == vect_reduction_def
)
6228 if (!is_gimple_assign (stmt
))
6230 if (gimple_call_internal_p (stmt
)
6231 && gimple_call_internal_fn (stmt
) == IFN_GOMP_SIMD_LANE
6232 && gimple_call_lhs (stmt
)
6234 && TREE_CODE (gimple_call_arg (stmt
, 0)) == SSA_NAME
6236 == SSA_NAME_VAR (gimple_call_arg (stmt
, 0)))
6238 edge e
= single_exit (loop
);
6239 basic_block merge_bb
= e
->dest
;
6240 imm_use_iterator imm_iter
;
6241 use_operand_p use_p
;
6242 tree lhs
= gimple_call_lhs (stmt
);
6244 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, lhs
)
6246 gimple
*use_stmt
= USE_STMT (use_p
);
6247 if (gimple_code (use_stmt
) == GIMPLE_PHI
6248 && gimple_bb (use_stmt
) == merge_bb
)
6253 = build_int_cst (unsigned_type_node
,
6254 loop_vinfo
->vectorization_factor
- 1);
6255 SET_PHI_ARG_DEF (use_stmt
, e
->dest_idx
, vfm1
);
6265 if (TREE_CODE (gimple_assign_lhs (stmt
)) != SSA_NAME
)
6268 /* FORNOW. CHECKME. */
6269 if (nested_in_vect_loop_p (loop
, stmt
))
6272 /* FORNOW: support only if all uses are invariant. This means
6273 that the scalar operations can remain in place, unvectorized.
6274 The original last scalar value that they compute will be used. */
6275 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, iter
, SSA_OP_USE
)
6277 enum vect_def_type dt
= vect_uninitialized_def
;
6279 if (!vect_is_simple_use (op
, loop_vinfo
, &def_stmt
, &dt
))
6281 if (dump_enabled_p ())
6282 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
6283 "use not simple.\n");
6287 if (dt
!= vect_external_def
&& dt
!= vect_constant_def
)
6291 /* No transformation is required for the cases we currently support. */
6295 /* Kill any debug uses outside LOOP of SSA names defined in STMT. */
6298 vect_loop_kill_debug_uses (struct loop
*loop
, gimple
*stmt
)
6300 ssa_op_iter op_iter
;
6301 imm_use_iterator imm_iter
;
6302 def_operand_p def_p
;
6305 FOR_EACH_PHI_OR_STMT_DEF (def_p
, stmt
, op_iter
, SSA_OP_DEF
)
6307 FOR_EACH_IMM_USE_STMT (ustmt
, imm_iter
, DEF_FROM_PTR (def_p
))
6311 if (!is_gimple_debug (ustmt
))
6314 bb
= gimple_bb (ustmt
);
6316 if (!flow_bb_inside_loop_p (loop
, bb
))
6318 if (gimple_debug_bind_p (ustmt
))
6320 if (dump_enabled_p ())
6321 dump_printf_loc (MSG_NOTE
, vect_location
,
6322 "killing debug use\n");
6324 gimple_debug_bind_reset_value (ustmt
);
6325 update_stmt (ustmt
);
6335 /* This function builds ni_name = number of iterations. Statements
6336 are emitted on the loop preheader edge. */
6339 vect_build_loop_niters (loop_vec_info loop_vinfo
)
6341 tree ni
= unshare_expr (LOOP_VINFO_NITERS (loop_vinfo
));
6342 if (TREE_CODE (ni
) == INTEGER_CST
)
6347 gimple_seq stmts
= NULL
;
6348 edge pe
= loop_preheader_edge (LOOP_VINFO_LOOP (loop_vinfo
));
6350 var
= create_tmp_var (TREE_TYPE (ni
), "niters");
6351 ni_name
= force_gimple_operand (ni
, &stmts
, false, var
);
6353 gsi_insert_seq_on_edge_immediate (pe
, stmts
);
6360 /* This function generates the following statements:
6362 ni_name = number of iterations loop executes
6363 ratio = ni_name / vf
6364 ratio_mult_vf_name = ratio * vf
6366 and places them on the loop preheader edge. */
6369 vect_generate_tmps_on_preheader (loop_vec_info loop_vinfo
,
6371 tree
*ratio_mult_vf_name_ptr
,
6372 tree
*ratio_name_ptr
)
6374 tree ni_minus_gap_name
;
6377 tree ratio_mult_vf_name
;
6378 int vf
= LOOP_VINFO_VECT_FACTOR (loop_vinfo
);
6379 edge pe
= loop_preheader_edge (LOOP_VINFO_LOOP (loop_vinfo
));
6382 log_vf
= build_int_cst (TREE_TYPE (ni_name
), exact_log2 (vf
));
6384 /* If epilogue loop is required because of data accesses with gaps, we
6385 subtract one iteration from the total number of iterations here for
6386 correct calculation of RATIO. */
6387 if (LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo
))
6389 ni_minus_gap_name
= fold_build2 (MINUS_EXPR
, TREE_TYPE (ni_name
),
6391 build_one_cst (TREE_TYPE (ni_name
)));
6392 if (!is_gimple_val (ni_minus_gap_name
))
6394 var
= create_tmp_var (TREE_TYPE (ni_name
), "ni_gap");
6395 gimple
*stmts
= NULL
;
6396 ni_minus_gap_name
= force_gimple_operand (ni_minus_gap_name
, &stmts
,
6398 gsi_insert_seq_on_edge_immediate (pe
, stmts
);
6402 ni_minus_gap_name
= ni_name
;
6404 /* Create: ratio = ni >> log2(vf) */
6405 /* ??? As we have ni == number of latch executions + 1, ni could
6406 have overflown to zero. So avoid computing ratio based on ni
6407 but compute it using the fact that we know ratio will be at least
6408 one, thus via (ni - vf) >> log2(vf) + 1. */
6410 = fold_build2 (PLUS_EXPR
, TREE_TYPE (ni_name
),
6411 fold_build2 (RSHIFT_EXPR
, TREE_TYPE (ni_name
),
6412 fold_build2 (MINUS_EXPR
, TREE_TYPE (ni_name
),
6415 (TREE_TYPE (ni_name
), vf
)),
6417 build_int_cst (TREE_TYPE (ni_name
), 1));
6418 if (!is_gimple_val (ratio_name
))
6420 var
= create_tmp_var (TREE_TYPE (ni_name
), "bnd");
6421 gimple
*stmts
= NULL
;
6422 ratio_name
= force_gimple_operand (ratio_name
, &stmts
, true, var
);
6423 gsi_insert_seq_on_edge_immediate (pe
, stmts
);
6425 *ratio_name_ptr
= ratio_name
;
6427 /* Create: ratio_mult_vf = ratio << log2 (vf). */
6429 if (ratio_mult_vf_name_ptr
)
6431 ratio_mult_vf_name
= fold_build2 (LSHIFT_EXPR
, TREE_TYPE (ratio_name
),
6432 ratio_name
, log_vf
);
6433 if (!is_gimple_val (ratio_mult_vf_name
))
6435 var
= create_tmp_var (TREE_TYPE (ni_name
), "ratio_mult_vf");
6436 gimple
*stmts
= NULL
;
6437 ratio_mult_vf_name
= force_gimple_operand (ratio_mult_vf_name
, &stmts
,
6439 gsi_insert_seq_on_edge_immediate (pe
, stmts
);
6441 *ratio_mult_vf_name_ptr
= ratio_mult_vf_name
;
6448 /* Function vect_transform_loop.
6450 The analysis phase has determined that the loop is vectorizable.
6451 Vectorize the loop - created vectorized stmts to replace the scalar
6452 stmts in the loop, and update the loop exit condition. */
6455 vect_transform_loop (loop_vec_info loop_vinfo
)
6457 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
6458 basic_block
*bbs
= LOOP_VINFO_BBS (loop_vinfo
);
6459 int nbbs
= loop
->num_nodes
;
6462 int vectorization_factor
= LOOP_VINFO_VECT_FACTOR (loop_vinfo
);
6464 bool slp_scheduled
= false;
6465 gimple
*stmt
, *pattern_stmt
;
6466 gimple_seq pattern_def_seq
= NULL
;
6467 gimple_stmt_iterator pattern_def_si
= gsi_none ();
6468 bool transform_pattern_stmt
= false;
6469 bool check_profitability
= false;
6471 /* Record number of iterations before we started tampering with the profile. */
6472 gcov_type expected_iterations
= expected_loop_iterations_unbounded (loop
);
6474 if (dump_enabled_p ())
6475 dump_printf_loc (MSG_NOTE
, vect_location
, "=== vec_transform_loop ===\n");
6477 /* If profile is inprecise, we have chance to fix it up. */
6478 if (LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo
))
6479 expected_iterations
= LOOP_VINFO_INT_NITERS (loop_vinfo
);
6481 /* Use the more conservative vectorization threshold. If the number
6482 of iterations is constant assume the cost check has been performed
6483 by our caller. If the threshold makes all loops profitable that
6484 run at least the vectorization factor number of times checking
6485 is pointless, too. */
6486 th
= LOOP_VINFO_COST_MODEL_THRESHOLD (loop_vinfo
);
6487 if (th
>= LOOP_VINFO_VECT_FACTOR (loop_vinfo
) - 1
6488 && !LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo
))
6490 if (dump_enabled_p ())
6491 dump_printf_loc (MSG_NOTE
, vect_location
,
6492 "Profitability threshold is %d loop iterations.\n",
6494 check_profitability
= true;
6497 /* Version the loop first, if required, so the profitability check
6500 if (LOOP_REQUIRES_VERSIONING_FOR_ALIGNMENT (loop_vinfo
)
6501 || LOOP_REQUIRES_VERSIONING_FOR_ALIAS (loop_vinfo
))
6503 vect_loop_versioning (loop_vinfo
, th
, check_profitability
);
6504 check_profitability
= false;
6507 tree ni_name
= vect_build_loop_niters (loop_vinfo
);
6508 LOOP_VINFO_NITERS_UNCHANGED (loop_vinfo
) = ni_name
;
6510 /* Peel the loop if there are data refs with unknown alignment.
6511 Only one data ref with unknown store is allowed. */
6513 if (LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo
))
6515 vect_do_peeling_for_alignment (loop_vinfo
, ni_name
,
6516 th
, check_profitability
);
6517 check_profitability
= false;
6518 /* The above adjusts LOOP_VINFO_NITERS, so cause ni_name to
6520 ni_name
= NULL_TREE
;
6523 /* If the loop has a symbolic number of iterations 'n' (i.e. it's not a
6524 compile time constant), or it is a constant that doesn't divide by the
6525 vectorization factor, then an epilog loop needs to be created.
6526 We therefore duplicate the loop: the original loop will be vectorized,
6527 and will compute the first (n/VF) iterations. The second copy of the loop
6528 will remain scalar and will compute the remaining (n%VF) iterations.
6529 (VF is the vectorization factor). */
6531 if (LOOP_VINFO_PEELING_FOR_NITER (loop_vinfo
)
6532 || LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo
))
6536 ni_name
= vect_build_loop_niters (loop_vinfo
);
6537 vect_generate_tmps_on_preheader (loop_vinfo
, ni_name
, &ratio_mult_vf
,
6539 vect_do_peeling_for_loop_bound (loop_vinfo
, ni_name
, ratio_mult_vf
,
6540 th
, check_profitability
);
6542 else if (LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo
))
6543 ratio
= build_int_cst (TREE_TYPE (LOOP_VINFO_NITERS (loop_vinfo
)),
6544 LOOP_VINFO_INT_NITERS (loop_vinfo
) / vectorization_factor
);
6548 ni_name
= vect_build_loop_niters (loop_vinfo
);
6549 vect_generate_tmps_on_preheader (loop_vinfo
, ni_name
, NULL
, &ratio
);
6552 /* 1) Make sure the loop header has exactly two entries
6553 2) Make sure we have a preheader basic block. */
6555 gcc_assert (EDGE_COUNT (loop
->header
->preds
) == 2);
6557 split_edge (loop_preheader_edge (loop
));
6559 /* FORNOW: the vectorizer supports only loops which body consist
6560 of one basic block (header + empty latch). When the vectorizer will
6561 support more involved loop forms, the order by which the BBs are
6562 traversed need to be reconsidered. */
6564 for (i
= 0; i
< nbbs
; i
++)
6566 basic_block bb
= bbs
[i
];
6567 stmt_vec_info stmt_info
;
6569 for (gphi_iterator si
= gsi_start_phis (bb
); !gsi_end_p (si
);
6572 gphi
*phi
= si
.phi ();
6573 if (dump_enabled_p ())
6575 dump_printf_loc (MSG_NOTE
, vect_location
,
6576 "------>vectorizing phi: ");
6577 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
, phi
, 0);
6578 dump_printf (MSG_NOTE
, "\n");
6580 stmt_info
= vinfo_for_stmt (phi
);
6584 if (MAY_HAVE_DEBUG_STMTS
&& !STMT_VINFO_LIVE_P (stmt_info
))
6585 vect_loop_kill_debug_uses (loop
, phi
);
6587 if (!STMT_VINFO_RELEVANT_P (stmt_info
)
6588 && !STMT_VINFO_LIVE_P (stmt_info
))
6591 if (STMT_VINFO_VECTYPE (stmt_info
)
6592 && (TYPE_VECTOR_SUBPARTS (STMT_VINFO_VECTYPE (stmt_info
))
6593 != (unsigned HOST_WIDE_INT
) vectorization_factor
)
6594 && dump_enabled_p ())
6595 dump_printf_loc (MSG_NOTE
, vect_location
, "multiple-types.\n");
6597 if (STMT_VINFO_DEF_TYPE (stmt_info
) == vect_induction_def
)
6599 if (dump_enabled_p ())
6600 dump_printf_loc (MSG_NOTE
, vect_location
, "transform phi.\n");
6601 vect_transform_stmt (phi
, NULL
, NULL
, NULL
, NULL
);
6605 pattern_stmt
= NULL
;
6606 for (gimple_stmt_iterator si
= gsi_start_bb (bb
);
6607 !gsi_end_p (si
) || transform_pattern_stmt
;)
6611 if (transform_pattern_stmt
)
6612 stmt
= pattern_stmt
;
6615 stmt
= gsi_stmt (si
);
6616 /* During vectorization remove existing clobber stmts. */
6617 if (gimple_clobber_p (stmt
))
6619 unlink_stmt_vdef (stmt
);
6620 gsi_remove (&si
, true);
6621 release_defs (stmt
);
6626 if (dump_enabled_p ())
6628 dump_printf_loc (MSG_NOTE
, vect_location
,
6629 "------>vectorizing statement: ");
6630 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
, stmt
, 0);
6631 dump_printf (MSG_NOTE
, "\n");
6634 stmt_info
= vinfo_for_stmt (stmt
);
6636 /* vector stmts created in the outer-loop during vectorization of
6637 stmts in an inner-loop may not have a stmt_info, and do not
6638 need to be vectorized. */
6645 if (MAY_HAVE_DEBUG_STMTS
&& !STMT_VINFO_LIVE_P (stmt_info
))
6646 vect_loop_kill_debug_uses (loop
, stmt
);
6648 if (!STMT_VINFO_RELEVANT_P (stmt_info
)
6649 && !STMT_VINFO_LIVE_P (stmt_info
))
6651 if (STMT_VINFO_IN_PATTERN_P (stmt_info
)
6652 && (pattern_stmt
= STMT_VINFO_RELATED_STMT (stmt_info
))
6653 && (STMT_VINFO_RELEVANT_P (vinfo_for_stmt (pattern_stmt
))
6654 || STMT_VINFO_LIVE_P (vinfo_for_stmt (pattern_stmt
))))
6656 stmt
= pattern_stmt
;
6657 stmt_info
= vinfo_for_stmt (stmt
);
6665 else if (STMT_VINFO_IN_PATTERN_P (stmt_info
)
6666 && (pattern_stmt
= STMT_VINFO_RELATED_STMT (stmt_info
))
6667 && (STMT_VINFO_RELEVANT_P (vinfo_for_stmt (pattern_stmt
))
6668 || STMT_VINFO_LIVE_P (vinfo_for_stmt (pattern_stmt
))))
6669 transform_pattern_stmt
= true;
6671 /* If pattern statement has def stmts, vectorize them too. */
6672 if (is_pattern_stmt_p (stmt_info
))
6674 if (pattern_def_seq
== NULL
)
6676 pattern_def_seq
= STMT_VINFO_PATTERN_DEF_SEQ (stmt_info
);
6677 pattern_def_si
= gsi_start (pattern_def_seq
);
6679 else if (!gsi_end_p (pattern_def_si
))
6680 gsi_next (&pattern_def_si
);
6681 if (pattern_def_seq
!= NULL
)
6683 gimple
*pattern_def_stmt
= NULL
;
6684 stmt_vec_info pattern_def_stmt_info
= NULL
;
6686 while (!gsi_end_p (pattern_def_si
))
6688 pattern_def_stmt
= gsi_stmt (pattern_def_si
);
6689 pattern_def_stmt_info
6690 = vinfo_for_stmt (pattern_def_stmt
);
6691 if (STMT_VINFO_RELEVANT_P (pattern_def_stmt_info
)
6692 || STMT_VINFO_LIVE_P (pattern_def_stmt_info
))
6694 gsi_next (&pattern_def_si
);
6697 if (!gsi_end_p (pattern_def_si
))
6699 if (dump_enabled_p ())
6701 dump_printf_loc (MSG_NOTE
, vect_location
,
6702 "==> vectorizing pattern def "
6704 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
,
6705 pattern_def_stmt
, 0);
6706 dump_printf (MSG_NOTE
, "\n");
6709 stmt
= pattern_def_stmt
;
6710 stmt_info
= pattern_def_stmt_info
;
6714 pattern_def_si
= gsi_none ();
6715 transform_pattern_stmt
= false;
6719 transform_pattern_stmt
= false;
6722 if (STMT_VINFO_VECTYPE (stmt_info
))
6726 TYPE_VECTOR_SUBPARTS (STMT_VINFO_VECTYPE (stmt_info
));
6727 if (!STMT_SLP_TYPE (stmt_info
)
6728 && nunits
!= (unsigned int) vectorization_factor
6729 && dump_enabled_p ())
6730 /* For SLP VF is set according to unrolling factor, and not
6731 to vector size, hence for SLP this print is not valid. */
6732 dump_printf_loc (MSG_NOTE
, vect_location
, "multiple-types.\n");
6735 /* SLP. Schedule all the SLP instances when the first SLP stmt is
6737 if (STMT_SLP_TYPE (stmt_info
))
6741 slp_scheduled
= true;
6743 if (dump_enabled_p ())
6744 dump_printf_loc (MSG_NOTE
, vect_location
,
6745 "=== scheduling SLP instances ===\n");
6747 vect_schedule_slp (loop_vinfo
);
6750 /* Hybrid SLP stmts must be vectorized in addition to SLP. */
6751 if (!vinfo_for_stmt (stmt
) || PURE_SLP_STMT (stmt_info
))
6753 if (!transform_pattern_stmt
&& gsi_end_p (pattern_def_si
))
6755 pattern_def_seq
= NULL
;
6762 /* -------- vectorize statement ------------ */
6763 if (dump_enabled_p ())
6764 dump_printf_loc (MSG_NOTE
, vect_location
, "transform statement.\n");
6766 grouped_store
= false;
6767 is_store
= vect_transform_stmt (stmt
, &si
, &grouped_store
, NULL
, NULL
);
6770 if (STMT_VINFO_GROUPED_ACCESS (stmt_info
))
6772 /* Interleaving. If IS_STORE is TRUE, the vectorization of the
6773 interleaving chain was completed - free all the stores in
6776 vect_remove_stores (GROUP_FIRST_ELEMENT (stmt_info
));
6780 /* Free the attached stmt_vec_info and remove the stmt. */
6781 gimple
*store
= gsi_stmt (si
);
6782 free_stmt_vec_info (store
);
6783 unlink_stmt_vdef (store
);
6784 gsi_remove (&si
, true);
6785 release_defs (store
);
6788 /* Stores can only appear at the end of pattern statements. */
6789 gcc_assert (!transform_pattern_stmt
);
6790 pattern_def_seq
= NULL
;
6792 else if (!transform_pattern_stmt
&& gsi_end_p (pattern_def_si
))
6794 pattern_def_seq
= NULL
;
6800 slpeel_make_loop_iterate_ntimes (loop
, ratio
);
6802 /* Reduce loop iterations by the vectorization factor. */
6803 scale_loop_profile (loop
, GCOV_COMPUTE_SCALE (1, vectorization_factor
),
6804 expected_iterations
/ vectorization_factor
);
6805 loop
->nb_iterations_upper_bound
6806 = wi::udiv_floor (loop
->nb_iterations_upper_bound
, vectorization_factor
);
6807 if (LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo
)
6808 && loop
->nb_iterations_upper_bound
!= 0)
6809 loop
->nb_iterations_upper_bound
= loop
->nb_iterations_upper_bound
- 1;
6810 if (loop
->any_estimate
)
6812 loop
->nb_iterations_estimate
6813 = wi::udiv_floor (loop
->nb_iterations_estimate
, vectorization_factor
);
6814 if (LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo
)
6815 && loop
->nb_iterations_estimate
!= 0)
6816 loop
->nb_iterations_estimate
= loop
->nb_iterations_estimate
- 1;
6819 if (dump_enabled_p ())
6821 dump_printf_loc (MSG_NOTE
, vect_location
,
6822 "LOOP VECTORIZED\n");
6824 dump_printf_loc (MSG_NOTE
, vect_location
,
6825 "OUTER LOOP VECTORIZED\n");
6826 dump_printf (MSG_NOTE
, "\n");