2 Copyright (C) 2003-2016 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 if (STMT_VINFO_RELEVANT_P (stmt_info
))
443 mask_producers
.safe_push (stmt_info
);
446 if (gimple_code (stmt
) == GIMPLE_ASSIGN
447 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt
))
449 && TREE_CODE (TREE_TYPE (gimple_assign_rhs1 (stmt
)))
451 scalar_type
= TREE_TYPE (gimple_assign_rhs1 (stmt
));
454 if (!analyze_pattern_stmt
&& gsi_end_p (pattern_def_si
))
456 pattern_def_seq
= NULL
;
463 if (dump_enabled_p ())
465 dump_printf_loc (MSG_NOTE
, vect_location
,
466 "get vectype for scalar type: ");
467 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, scalar_type
);
468 dump_printf (MSG_NOTE
, "\n");
470 vectype
= get_vectype_for_scalar_type (scalar_type
);
473 if (dump_enabled_p ())
475 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
476 "not vectorized: unsupported "
478 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
,
480 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
486 STMT_VINFO_VECTYPE (stmt_info
) = vectype
;
488 if (dump_enabled_p ())
490 dump_printf_loc (MSG_NOTE
, vect_location
, "vectype: ");
491 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, vectype
);
492 dump_printf (MSG_NOTE
, "\n");
496 /* Don't try to compute VF out scalar types if we stmt
497 produces boolean vector. Use result vectype instead. */
498 if (VECTOR_BOOLEAN_TYPE_P (vectype
))
499 vf_vectype
= vectype
;
502 /* The vectorization factor is according to the smallest
503 scalar type (or the largest vector size, but we only
504 support one vector size per loop). */
506 scalar_type
= vect_get_smallest_scalar_type (stmt
, &dummy
,
508 if (dump_enabled_p ())
510 dump_printf_loc (MSG_NOTE
, vect_location
,
511 "get vectype for scalar type: ");
512 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, scalar_type
);
513 dump_printf (MSG_NOTE
, "\n");
515 vf_vectype
= get_vectype_for_scalar_type (scalar_type
);
519 if (dump_enabled_p ())
521 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
522 "not vectorized: unsupported data-type ");
523 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
,
525 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
530 if ((GET_MODE_SIZE (TYPE_MODE (vectype
))
531 != GET_MODE_SIZE (TYPE_MODE (vf_vectype
))))
533 if (dump_enabled_p ())
535 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
536 "not vectorized: different sized vector "
537 "types in statement, ");
538 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
,
540 dump_printf (MSG_MISSED_OPTIMIZATION
, " and ");
541 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
,
543 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
548 if (dump_enabled_p ())
550 dump_printf_loc (MSG_NOTE
, vect_location
, "vectype: ");
551 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, vf_vectype
);
552 dump_printf (MSG_NOTE
, "\n");
555 nunits
= TYPE_VECTOR_SUBPARTS (vf_vectype
);
556 if (dump_enabled_p ())
557 dump_printf_loc (MSG_NOTE
, vect_location
, "nunits = %d\n", nunits
);
558 if (!vectorization_factor
559 || (nunits
> vectorization_factor
))
560 vectorization_factor
= nunits
;
562 if (!analyze_pattern_stmt
&& gsi_end_p (pattern_def_si
))
564 pattern_def_seq
= NULL
;
570 /* TODO: Analyze cost. Decide if worth while to vectorize. */
571 if (dump_enabled_p ())
572 dump_printf_loc (MSG_NOTE
, vect_location
, "vectorization factor = %d\n",
573 vectorization_factor
);
574 if (vectorization_factor
<= 1)
576 if (dump_enabled_p ())
577 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
578 "not vectorized: unsupported data-type\n");
581 LOOP_VINFO_VECT_FACTOR (loop_vinfo
) = vectorization_factor
;
583 for (i
= 0; i
< mask_producers
.length (); i
++)
585 tree mask_type
= NULL
;
587 stmt
= STMT_VINFO_STMT (mask_producers
[i
]);
589 if (gimple_code (stmt
) == GIMPLE_ASSIGN
590 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt
)) == tcc_comparison
591 && TREE_CODE (TREE_TYPE (gimple_assign_rhs1 (stmt
))) != BOOLEAN_TYPE
)
593 scalar_type
= TREE_TYPE (gimple_assign_rhs1 (stmt
));
594 mask_type
= get_mask_type_for_scalar_type (scalar_type
);
598 if (dump_enabled_p ())
599 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
600 "not vectorized: unsupported mask\n");
609 enum vect_def_type dt
;
611 FOR_EACH_SSA_TREE_OPERAND (rhs
, stmt
, iter
, SSA_OP_USE
)
613 if (!vect_is_simple_use (rhs
, mask_producers
[i
]->vinfo
,
614 &def_stmt
, &dt
, &vectype
))
616 if (dump_enabled_p ())
618 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
619 "not vectorized: can't compute mask type "
621 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
, stmt
,
623 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
628 /* No vectype probably means external definition.
629 Allow it in case there is another operand which
630 allows to determine mask type. */
636 else if (TYPE_VECTOR_SUBPARTS (mask_type
)
637 != TYPE_VECTOR_SUBPARTS (vectype
))
639 if (dump_enabled_p ())
641 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
642 "not vectorized: different sized masks "
643 "types in statement, ");
644 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
,
646 dump_printf (MSG_MISSED_OPTIMIZATION
, " and ");
647 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
,
649 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
653 else if (VECTOR_BOOLEAN_TYPE_P (mask_type
)
654 != VECTOR_BOOLEAN_TYPE_P (vectype
))
656 if (dump_enabled_p ())
658 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
659 "not vectorized: mixed mask and "
660 "nonmask vector types in statement, ");
661 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
,
663 dump_printf (MSG_MISSED_OPTIMIZATION
, " and ");
664 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
,
666 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
672 /* We may compare boolean value loaded as vector of integers.
673 Fix mask_type in such case. */
675 && !VECTOR_BOOLEAN_TYPE_P (mask_type
)
676 && gimple_code (stmt
) == GIMPLE_ASSIGN
677 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt
)) == tcc_comparison
)
678 mask_type
= build_same_sized_truth_vector_type (mask_type
);
681 /* No mask_type should mean loop invariant predicate.
682 This is probably a subject for optimization in
686 if (dump_enabled_p ())
688 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
689 "not vectorized: can't compute mask type "
691 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
, stmt
,
693 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
698 STMT_VINFO_VECTYPE (mask_producers
[i
]) = mask_type
;
705 /* Function vect_is_simple_iv_evolution.
707 FORNOW: A simple evolution of an induction variables in the loop is
708 considered a polynomial evolution. */
711 vect_is_simple_iv_evolution (unsigned loop_nb
, tree access_fn
, tree
* init
,
716 tree evolution_part
= evolution_part_in_loop_num (access_fn
, loop_nb
);
719 /* When there is no evolution in this loop, the evolution function
721 if (evolution_part
== NULL_TREE
)
724 /* When the evolution is a polynomial of degree >= 2
725 the evolution function is not "simple". */
726 if (tree_is_chrec (evolution_part
))
729 step_expr
= evolution_part
;
730 init_expr
= unshare_expr (initial_condition_in_loop_num (access_fn
, loop_nb
));
732 if (dump_enabled_p ())
734 dump_printf_loc (MSG_NOTE
, vect_location
, "step: ");
735 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, step_expr
);
736 dump_printf (MSG_NOTE
, ", init: ");
737 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, init_expr
);
738 dump_printf (MSG_NOTE
, "\n");
744 if (TREE_CODE (step_expr
) != INTEGER_CST
745 && (TREE_CODE (step_expr
) != SSA_NAME
746 || ((bb
= gimple_bb (SSA_NAME_DEF_STMT (step_expr
)))
747 && flow_bb_inside_loop_p (get_loop (cfun
, loop_nb
), bb
))
748 || (!INTEGRAL_TYPE_P (TREE_TYPE (step_expr
))
749 && (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (step_expr
))
750 || !flag_associative_math
)))
751 && (TREE_CODE (step_expr
) != REAL_CST
752 || !flag_associative_math
))
754 if (dump_enabled_p ())
755 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
763 /* Function vect_analyze_scalar_cycles_1.
765 Examine the cross iteration def-use cycles of scalar variables
766 in LOOP. LOOP_VINFO represents the loop that is now being
767 considered for vectorization (can be LOOP, or an outer-loop
771 vect_analyze_scalar_cycles_1 (loop_vec_info loop_vinfo
, struct loop
*loop
)
773 basic_block bb
= loop
->header
;
775 auto_vec
<gimple
*, 64> worklist
;
779 if (dump_enabled_p ())
780 dump_printf_loc (MSG_NOTE
, vect_location
,
781 "=== vect_analyze_scalar_cycles ===\n");
783 /* First - identify all inductions. Reduction detection assumes that all the
784 inductions have been identified, therefore, this order must not be
786 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
788 gphi
*phi
= gsi
.phi ();
789 tree access_fn
= NULL
;
790 tree def
= PHI_RESULT (phi
);
791 stmt_vec_info stmt_vinfo
= vinfo_for_stmt (phi
);
793 if (dump_enabled_p ())
795 dump_printf_loc (MSG_NOTE
, vect_location
, "Analyze phi: ");
796 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
, phi
, 0);
797 dump_printf (MSG_NOTE
, "\n");
800 /* Skip virtual phi's. The data dependences that are associated with
801 virtual defs/uses (i.e., memory accesses) are analyzed elsewhere. */
802 if (virtual_operand_p (def
))
805 STMT_VINFO_DEF_TYPE (stmt_vinfo
) = vect_unknown_def_type
;
807 /* Analyze the evolution function. */
808 access_fn
= analyze_scalar_evolution (loop
, def
);
811 STRIP_NOPS (access_fn
);
812 if (dump_enabled_p ())
814 dump_printf_loc (MSG_NOTE
, vect_location
,
815 "Access function of PHI: ");
816 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, access_fn
);
817 dump_printf (MSG_NOTE
, "\n");
819 STMT_VINFO_LOOP_PHI_EVOLUTION_BASE_UNCHANGED (stmt_vinfo
)
820 = initial_condition_in_loop_num (access_fn
, loop
->num
);
821 STMT_VINFO_LOOP_PHI_EVOLUTION_PART (stmt_vinfo
)
822 = evolution_part_in_loop_num (access_fn
, loop
->num
);
826 || !vect_is_simple_iv_evolution (loop
->num
, access_fn
, &init
, &step
)
827 || (LOOP_VINFO_LOOP (loop_vinfo
) != loop
828 && TREE_CODE (step
) != INTEGER_CST
))
830 worklist
.safe_push (phi
);
834 gcc_assert (STMT_VINFO_LOOP_PHI_EVOLUTION_BASE_UNCHANGED (stmt_vinfo
)
836 gcc_assert (STMT_VINFO_LOOP_PHI_EVOLUTION_PART (stmt_vinfo
) != NULL_TREE
);
838 if (dump_enabled_p ())
839 dump_printf_loc (MSG_NOTE
, vect_location
, "Detected induction.\n");
840 STMT_VINFO_DEF_TYPE (stmt_vinfo
) = vect_induction_def
;
844 /* Second - identify all reductions and nested cycles. */
845 while (worklist
.length () > 0)
847 gimple
*phi
= worklist
.pop ();
848 tree def
= PHI_RESULT (phi
);
849 stmt_vec_info stmt_vinfo
= vinfo_for_stmt (phi
);
853 if (dump_enabled_p ())
855 dump_printf_loc (MSG_NOTE
, vect_location
, "Analyze phi: ");
856 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
, phi
, 0);
857 dump_printf (MSG_NOTE
, "\n");
860 gcc_assert (!virtual_operand_p (def
)
861 && STMT_VINFO_DEF_TYPE (stmt_vinfo
) == vect_unknown_def_type
);
863 nested_cycle
= (loop
!= LOOP_VINFO_LOOP (loop_vinfo
));
864 reduc_stmt
= vect_force_simple_reduction (loop_vinfo
, phi
, !nested_cycle
,
865 &double_reduc
, false);
870 if (dump_enabled_p ())
871 dump_printf_loc (MSG_NOTE
, vect_location
,
872 "Detected double reduction.\n");
874 STMT_VINFO_DEF_TYPE (stmt_vinfo
) = vect_double_reduction_def
;
875 STMT_VINFO_DEF_TYPE (vinfo_for_stmt (reduc_stmt
)) =
876 vect_double_reduction_def
;
882 if (dump_enabled_p ())
883 dump_printf_loc (MSG_NOTE
, vect_location
,
884 "Detected vectorizable nested cycle.\n");
886 STMT_VINFO_DEF_TYPE (stmt_vinfo
) = vect_nested_cycle
;
887 STMT_VINFO_DEF_TYPE (vinfo_for_stmt (reduc_stmt
)) =
892 if (dump_enabled_p ())
893 dump_printf_loc (MSG_NOTE
, vect_location
,
894 "Detected reduction.\n");
896 STMT_VINFO_DEF_TYPE (stmt_vinfo
) = vect_reduction_def
;
897 STMT_VINFO_DEF_TYPE (vinfo_for_stmt (reduc_stmt
)) =
899 /* Store the reduction cycles for possible vectorization in
901 LOOP_VINFO_REDUCTIONS (loop_vinfo
).safe_push (reduc_stmt
);
906 if (dump_enabled_p ())
907 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
908 "Unknown def-use cycle pattern.\n");
913 /* Function vect_analyze_scalar_cycles.
915 Examine the cross iteration def-use cycles of scalar variables, by
916 analyzing the loop-header PHIs of scalar variables. Classify each
917 cycle as one of the following: invariant, induction, reduction, unknown.
918 We do that for the loop represented by LOOP_VINFO, and also to its
919 inner-loop, if exists.
920 Examples for scalar cycles:
935 vect_analyze_scalar_cycles (loop_vec_info loop_vinfo
)
937 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
939 vect_analyze_scalar_cycles_1 (loop_vinfo
, loop
);
941 /* When vectorizing an outer-loop, the inner-loop is executed sequentially.
942 Reductions in such inner-loop therefore have different properties than
943 the reductions in the nest that gets vectorized:
944 1. When vectorized, they are executed in the same order as in the original
945 scalar loop, so we can't change the order of computation when
947 2. FIXME: Inner-loop reductions can be used in the inner-loop, so the
948 current checks are too strict. */
951 vect_analyze_scalar_cycles_1 (loop_vinfo
, loop
->inner
);
954 /* Transfer group and reduction information from STMT to its pattern stmt. */
957 vect_fixup_reduc_chain (gimple
*stmt
)
959 gimple
*firstp
= STMT_VINFO_RELATED_STMT (vinfo_for_stmt (stmt
));
961 gcc_assert (!GROUP_FIRST_ELEMENT (vinfo_for_stmt (firstp
))
962 && GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt
)));
963 GROUP_SIZE (vinfo_for_stmt (firstp
)) = GROUP_SIZE (vinfo_for_stmt (stmt
));
966 stmtp
= STMT_VINFO_RELATED_STMT (vinfo_for_stmt (stmt
));
967 GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmtp
)) = firstp
;
968 stmt
= GROUP_NEXT_ELEMENT (vinfo_for_stmt (stmt
));
970 GROUP_NEXT_ELEMENT (vinfo_for_stmt (stmtp
))
971 = STMT_VINFO_RELATED_STMT (vinfo_for_stmt (stmt
));
974 STMT_VINFO_DEF_TYPE (vinfo_for_stmt (stmtp
)) = vect_reduction_def
;
977 /* Fixup scalar cycles that now have their stmts detected as patterns. */
980 vect_fixup_scalar_cycles_with_patterns (loop_vec_info loop_vinfo
)
985 FOR_EACH_VEC_ELT (LOOP_VINFO_REDUCTION_CHAINS (loop_vinfo
), i
, first
)
986 if (STMT_VINFO_IN_PATTERN_P (vinfo_for_stmt (first
)))
988 vect_fixup_reduc_chain (first
);
989 LOOP_VINFO_REDUCTION_CHAINS (loop_vinfo
)[i
]
990 = STMT_VINFO_RELATED_STMT (vinfo_for_stmt (first
));
994 /* Function vect_get_loop_niters.
996 Determine how many iterations the loop is executed and place it
997 in NUMBER_OF_ITERATIONS. Place the number of latch iterations
998 in NUMBER_OF_ITERATIONSM1.
1000 Return the loop exit condition. */
1004 vect_get_loop_niters (struct loop
*loop
, tree
*number_of_iterations
,
1005 tree
*number_of_iterationsm1
)
1009 if (dump_enabled_p ())
1010 dump_printf_loc (MSG_NOTE
, vect_location
,
1011 "=== get_loop_niters ===\n");
1013 niters
= number_of_latch_executions (loop
);
1014 *number_of_iterationsm1
= niters
;
1016 /* We want the number of loop header executions which is the number
1017 of latch executions plus one.
1018 ??? For UINT_MAX latch executions this number overflows to zero
1019 for loops like do { n++; } while (n != 0); */
1020 if (niters
&& !chrec_contains_undetermined (niters
))
1021 niters
= fold_build2 (PLUS_EXPR
, TREE_TYPE (niters
), unshare_expr (niters
),
1022 build_int_cst (TREE_TYPE (niters
), 1));
1023 *number_of_iterations
= niters
;
1025 return get_loop_exit_condition (loop
);
1029 /* Function bb_in_loop_p
1031 Used as predicate for dfs order traversal of the loop bbs. */
1034 bb_in_loop_p (const_basic_block bb
, const void *data
)
1036 const struct loop
*const loop
= (const struct loop
*)data
;
1037 if (flow_bb_inside_loop_p (loop
, bb
))
1043 /* Function new_loop_vec_info.
1045 Create and initialize a new loop_vec_info struct for LOOP, as well as
1046 stmt_vec_info structs for all the stmts in LOOP. */
1048 static loop_vec_info
1049 new_loop_vec_info (struct loop
*loop
)
1053 gimple_stmt_iterator si
;
1054 unsigned int i
, nbbs
;
1056 res
= (loop_vec_info
) xcalloc (1, sizeof (struct _loop_vec_info
));
1057 res
->kind
= vec_info::loop
;
1058 LOOP_VINFO_LOOP (res
) = loop
;
1060 bbs
= get_loop_body (loop
);
1062 /* Create/Update stmt_info for all stmts in the loop. */
1063 for (i
= 0; i
< loop
->num_nodes
; i
++)
1065 basic_block bb
= bbs
[i
];
1067 for (si
= gsi_start_phis (bb
); !gsi_end_p (si
); gsi_next (&si
))
1069 gimple
*phi
= gsi_stmt (si
);
1070 gimple_set_uid (phi
, 0);
1071 set_vinfo_for_stmt (phi
, new_stmt_vec_info (phi
, res
));
1074 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); gsi_next (&si
))
1076 gimple
*stmt
= gsi_stmt (si
);
1077 gimple_set_uid (stmt
, 0);
1078 set_vinfo_for_stmt (stmt
, new_stmt_vec_info (stmt
, res
));
1082 /* CHECKME: We want to visit all BBs before their successors (except for
1083 latch blocks, for which this assertion wouldn't hold). In the simple
1084 case of the loop forms we allow, a dfs order of the BBs would the same
1085 as reversed postorder traversal, so we are safe. */
1088 bbs
= XCNEWVEC (basic_block
, loop
->num_nodes
);
1089 nbbs
= dfs_enumerate_from (loop
->header
, 0, bb_in_loop_p
,
1090 bbs
, loop
->num_nodes
, loop
);
1091 gcc_assert (nbbs
== loop
->num_nodes
);
1093 LOOP_VINFO_BBS (res
) = bbs
;
1094 LOOP_VINFO_NITERSM1 (res
) = NULL
;
1095 LOOP_VINFO_NITERS (res
) = NULL
;
1096 LOOP_VINFO_NITERS_UNCHANGED (res
) = NULL
;
1097 LOOP_VINFO_COST_MODEL_THRESHOLD (res
) = 0;
1098 LOOP_VINFO_VECTORIZABLE_P (res
) = 0;
1099 LOOP_VINFO_PEELING_FOR_ALIGNMENT (res
) = 0;
1100 LOOP_VINFO_VECT_FACTOR (res
) = 0;
1101 LOOP_VINFO_LOOP_NEST (res
) = vNULL
;
1102 LOOP_VINFO_DATAREFS (res
) = vNULL
;
1103 LOOP_VINFO_DDRS (res
) = vNULL
;
1104 LOOP_VINFO_UNALIGNED_DR (res
) = NULL
;
1105 LOOP_VINFO_MAY_MISALIGN_STMTS (res
) = vNULL
;
1106 LOOP_VINFO_MAY_ALIAS_DDRS (res
) = vNULL
;
1107 LOOP_VINFO_GROUPED_STORES (res
) = vNULL
;
1108 LOOP_VINFO_REDUCTIONS (res
) = vNULL
;
1109 LOOP_VINFO_REDUCTION_CHAINS (res
) = vNULL
;
1110 LOOP_VINFO_SLP_INSTANCES (res
) = vNULL
;
1111 LOOP_VINFO_SLP_UNROLLING_FACTOR (res
) = 1;
1112 LOOP_VINFO_TARGET_COST_DATA (res
) = init_cost (loop
);
1113 LOOP_VINFO_PEELING_FOR_GAPS (res
) = false;
1114 LOOP_VINFO_PEELING_FOR_NITER (res
) = false;
1115 LOOP_VINFO_OPERANDS_SWAPPED (res
) = false;
1121 /* Function destroy_loop_vec_info.
1123 Free LOOP_VINFO struct, as well as all the stmt_vec_info structs of all the
1124 stmts in the loop. */
1127 destroy_loop_vec_info (loop_vec_info loop_vinfo
, bool clean_stmts
)
1132 gimple_stmt_iterator si
;
1134 vec
<slp_instance
> slp_instances
;
1135 slp_instance instance
;
1141 loop
= LOOP_VINFO_LOOP (loop_vinfo
);
1143 bbs
= LOOP_VINFO_BBS (loop_vinfo
);
1144 nbbs
= clean_stmts
? loop
->num_nodes
: 0;
1145 swapped
= LOOP_VINFO_OPERANDS_SWAPPED (loop_vinfo
);
1147 for (j
= 0; j
< nbbs
; j
++)
1149 basic_block bb
= bbs
[j
];
1150 for (si
= gsi_start_phis (bb
); !gsi_end_p (si
); gsi_next (&si
))
1151 free_stmt_vec_info (gsi_stmt (si
));
1153 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); )
1155 gimple
*stmt
= gsi_stmt (si
);
1157 /* We may have broken canonical form by moving a constant
1158 into RHS1 of a commutative op. Fix such occurrences. */
1159 if (swapped
&& is_gimple_assign (stmt
))
1161 enum tree_code code
= gimple_assign_rhs_code (stmt
);
1163 if ((code
== PLUS_EXPR
1164 || code
== POINTER_PLUS_EXPR
1165 || code
== MULT_EXPR
)
1166 && CONSTANT_CLASS_P (gimple_assign_rhs1 (stmt
)))
1167 swap_ssa_operands (stmt
,
1168 gimple_assign_rhs1_ptr (stmt
),
1169 gimple_assign_rhs2_ptr (stmt
));
1172 /* Free stmt_vec_info. */
1173 free_stmt_vec_info (stmt
);
1178 free (LOOP_VINFO_BBS (loop_vinfo
));
1179 vect_destroy_datarefs (loop_vinfo
);
1180 free_dependence_relations (LOOP_VINFO_DDRS (loop_vinfo
));
1181 LOOP_VINFO_LOOP_NEST (loop_vinfo
).release ();
1182 LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo
).release ();
1183 LOOP_VINFO_COMP_ALIAS_DDRS (loop_vinfo
).release ();
1184 LOOP_VINFO_MAY_ALIAS_DDRS (loop_vinfo
).release ();
1185 slp_instances
= LOOP_VINFO_SLP_INSTANCES (loop_vinfo
);
1186 FOR_EACH_VEC_ELT (slp_instances
, j
, instance
)
1187 vect_free_slp_instance (instance
);
1189 LOOP_VINFO_SLP_INSTANCES (loop_vinfo
).release ();
1190 LOOP_VINFO_GROUPED_STORES (loop_vinfo
).release ();
1191 LOOP_VINFO_REDUCTIONS (loop_vinfo
).release ();
1192 LOOP_VINFO_REDUCTION_CHAINS (loop_vinfo
).release ();
1194 destroy_cost_data (LOOP_VINFO_TARGET_COST_DATA (loop_vinfo
));
1195 loop_vinfo
->scalar_cost_vec
.release ();
1202 /* Calculate the cost of one scalar iteration of the loop. */
1204 vect_compute_single_scalar_iteration_cost (loop_vec_info loop_vinfo
)
1206 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
1207 basic_block
*bbs
= LOOP_VINFO_BBS (loop_vinfo
);
1208 int nbbs
= loop
->num_nodes
, factor
, scalar_single_iter_cost
= 0;
1209 int innerloop_iters
, i
;
1211 /* Count statements in scalar loop. Using this as scalar cost for a single
1214 TODO: Add outer loop support.
1216 TODO: Consider assigning different costs to different scalar
1220 innerloop_iters
= 1;
1222 innerloop_iters
= 50; /* FIXME */
1224 for (i
= 0; i
< nbbs
; i
++)
1226 gimple_stmt_iterator si
;
1227 basic_block bb
= bbs
[i
];
1229 if (bb
->loop_father
== loop
->inner
)
1230 factor
= innerloop_iters
;
1234 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); gsi_next (&si
))
1236 gimple
*stmt
= gsi_stmt (si
);
1237 stmt_vec_info stmt_info
= vinfo_for_stmt (stmt
);
1239 if (!is_gimple_assign (stmt
) && !is_gimple_call (stmt
))
1242 /* Skip stmts that are not vectorized inside the loop. */
1244 && !STMT_VINFO_RELEVANT_P (stmt_info
)
1245 && (!STMT_VINFO_LIVE_P (stmt_info
)
1246 || !VECTORIZABLE_CYCLE_DEF (STMT_VINFO_DEF_TYPE (stmt_info
)))
1247 && !STMT_VINFO_IN_PATTERN_P (stmt_info
))
1250 vect_cost_for_stmt kind
;
1251 if (STMT_VINFO_DATA_REF (vinfo_for_stmt (stmt
)))
1253 if (DR_IS_READ (STMT_VINFO_DATA_REF (vinfo_for_stmt (stmt
))))
1256 kind
= scalar_store
;
1261 scalar_single_iter_cost
1262 += record_stmt_cost (&LOOP_VINFO_SCALAR_ITERATION_COST (loop_vinfo
),
1263 factor
, kind
, NULL
, 0, vect_prologue
);
1266 LOOP_VINFO_SINGLE_SCALAR_ITERATION_COST (loop_vinfo
)
1267 = scalar_single_iter_cost
;
1271 /* Function vect_analyze_loop_form_1.
1273 Verify that certain CFG restrictions hold, including:
1274 - the loop has a pre-header
1275 - the loop has a single entry and exit
1276 - the loop exit condition is simple enough, and the number of iterations
1277 can be analyzed (a countable loop). */
1280 vect_analyze_loop_form_1 (struct loop
*loop
, gcond
**loop_cond
,
1281 tree
*number_of_iterationsm1
,
1282 tree
*number_of_iterations
, gcond
**inner_loop_cond
)
1284 if (dump_enabled_p ())
1285 dump_printf_loc (MSG_NOTE
, vect_location
,
1286 "=== vect_analyze_loop_form ===\n");
1288 /* Different restrictions apply when we are considering an inner-most loop,
1289 vs. an outer (nested) loop.
1290 (FORNOW. May want to relax some of these restrictions in the future). */
1294 /* Inner-most loop. We currently require that the number of BBs is
1295 exactly 2 (the header and latch). Vectorizable inner-most loops
1306 if (loop
->num_nodes
!= 2)
1308 if (dump_enabled_p ())
1309 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1310 "not vectorized: control flow in loop.\n");
1314 if (empty_block_p (loop
->header
))
1316 if (dump_enabled_p ())
1317 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1318 "not vectorized: empty loop.\n");
1324 struct loop
*innerloop
= loop
->inner
;
1327 /* Nested loop. We currently require that the loop is doubly-nested,
1328 contains a single inner loop, and the number of BBs is exactly 5.
1329 Vectorizable outer-loops look like this:
1341 The inner-loop has the properties expected of inner-most loops
1342 as described above. */
1344 if ((loop
->inner
)->inner
|| (loop
->inner
)->next
)
1346 if (dump_enabled_p ())
1347 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1348 "not vectorized: multiple nested loops.\n");
1352 if (loop
->num_nodes
!= 5)
1354 if (dump_enabled_p ())
1355 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1356 "not vectorized: control flow in loop.\n");
1360 entryedge
= loop_preheader_edge (innerloop
);
1361 if (entryedge
->src
!= loop
->header
1362 || !single_exit (innerloop
)
1363 || single_exit (innerloop
)->dest
!= EDGE_PRED (loop
->latch
, 0)->src
)
1365 if (dump_enabled_p ())
1366 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1367 "not vectorized: unsupported outerloop form.\n");
1371 /* Analyze the inner-loop. */
1372 tree inner_niterm1
, inner_niter
;
1373 if (! vect_analyze_loop_form_1 (loop
->inner
, inner_loop_cond
,
1374 &inner_niterm1
, &inner_niter
, NULL
))
1376 if (dump_enabled_p ())
1377 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1378 "not vectorized: Bad inner loop.\n");
1382 if (!expr_invariant_in_loop_p (loop
, inner_niter
))
1384 if (dump_enabled_p ())
1385 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1386 "not vectorized: inner-loop count not"
1391 if (dump_enabled_p ())
1392 dump_printf_loc (MSG_NOTE
, vect_location
,
1393 "Considering outer-loop vectorization.\n");
1396 if (!single_exit (loop
)
1397 || EDGE_COUNT (loop
->header
->preds
) != 2)
1399 if (dump_enabled_p ())
1401 if (!single_exit (loop
))
1402 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1403 "not vectorized: multiple exits.\n");
1404 else if (EDGE_COUNT (loop
->header
->preds
) != 2)
1405 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1406 "not vectorized: too many incoming edges.\n");
1411 /* We assume that the loop exit condition is at the end of the loop. i.e,
1412 that the loop is represented as a do-while (with a proper if-guard
1413 before the loop if needed), where the loop header contains all the
1414 executable statements, and the latch is empty. */
1415 if (!empty_block_p (loop
->latch
)
1416 || !gimple_seq_empty_p (phi_nodes (loop
->latch
)))
1418 if (dump_enabled_p ())
1419 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1420 "not vectorized: latch block not empty.\n");
1424 /* Make sure there exists a single-predecessor exit bb: */
1425 if (!single_pred_p (single_exit (loop
)->dest
))
1427 edge e
= single_exit (loop
);
1428 if (!(e
->flags
& EDGE_ABNORMAL
))
1430 split_loop_exit_edge (e
);
1431 if (dump_enabled_p ())
1432 dump_printf (MSG_NOTE
, "split exit edge.\n");
1436 if (dump_enabled_p ())
1437 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1438 "not vectorized: abnormal loop exit edge.\n");
1443 *loop_cond
= vect_get_loop_niters (loop
, number_of_iterations
,
1444 number_of_iterationsm1
);
1447 if (dump_enabled_p ())
1448 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1449 "not vectorized: complicated exit condition.\n");
1453 if (!*number_of_iterations
1454 || chrec_contains_undetermined (*number_of_iterations
))
1456 if (dump_enabled_p ())
1457 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1458 "not vectorized: number of iterations cannot be "
1463 if (integer_zerop (*number_of_iterations
))
1465 if (dump_enabled_p ())
1466 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1467 "not vectorized: number of iterations = 0.\n");
1474 /* Analyze LOOP form and return a loop_vec_info if it is of suitable form. */
1477 vect_analyze_loop_form (struct loop
*loop
)
1479 tree number_of_iterations
, number_of_iterationsm1
;
1480 gcond
*loop_cond
, *inner_loop_cond
= NULL
;
1482 if (! vect_analyze_loop_form_1 (loop
, &loop_cond
, &number_of_iterationsm1
,
1483 &number_of_iterations
, &inner_loop_cond
))
1486 loop_vec_info loop_vinfo
= new_loop_vec_info (loop
);
1487 LOOP_VINFO_NITERSM1 (loop_vinfo
) = number_of_iterationsm1
;
1488 LOOP_VINFO_NITERS (loop_vinfo
) = number_of_iterations
;
1489 LOOP_VINFO_NITERS_UNCHANGED (loop_vinfo
) = number_of_iterations
;
1491 if (!LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo
))
1493 if (dump_enabled_p ())
1495 dump_printf_loc (MSG_NOTE
, vect_location
,
1496 "Symbolic number of iterations is ");
1497 dump_generic_expr (MSG_NOTE
, TDF_DETAILS
, number_of_iterations
);
1498 dump_printf (MSG_NOTE
, "\n");
1502 STMT_VINFO_TYPE (vinfo_for_stmt (loop_cond
)) = loop_exit_ctrl_vec_info_type
;
1503 if (inner_loop_cond
)
1504 STMT_VINFO_TYPE (vinfo_for_stmt (inner_loop_cond
))
1505 = loop_exit_ctrl_vec_info_type
;
1507 gcc_assert (!loop
->aux
);
1508 loop
->aux
= loop_vinfo
;
1514 /* Scan the loop stmts and dependent on whether there are any (non-)SLP
1515 statements update the vectorization factor. */
1518 vect_update_vf_for_slp (loop_vec_info loop_vinfo
)
1520 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
1521 basic_block
*bbs
= LOOP_VINFO_BBS (loop_vinfo
);
1522 int nbbs
= loop
->num_nodes
;
1523 unsigned int vectorization_factor
;
1526 if (dump_enabled_p ())
1527 dump_printf_loc (MSG_NOTE
, vect_location
,
1528 "=== vect_update_vf_for_slp ===\n");
1530 vectorization_factor
= LOOP_VINFO_VECT_FACTOR (loop_vinfo
);
1531 gcc_assert (vectorization_factor
!= 0);
1533 /* If all the stmts in the loop can be SLPed, we perform only SLP, and
1534 vectorization factor of the loop is the unrolling factor required by
1535 the SLP instances. If that unrolling factor is 1, we say, that we
1536 perform pure SLP on loop - cross iteration parallelism is not
1538 bool only_slp_in_loop
= true;
1539 for (i
= 0; i
< nbbs
; i
++)
1541 basic_block bb
= bbs
[i
];
1542 for (gimple_stmt_iterator si
= gsi_start_bb (bb
); !gsi_end_p (si
);
1545 gimple
*stmt
= gsi_stmt (si
);
1546 stmt_vec_info stmt_info
= vinfo_for_stmt (stmt
);
1547 if (STMT_VINFO_IN_PATTERN_P (stmt_info
)
1548 && STMT_VINFO_RELATED_STMT (stmt_info
))
1550 stmt
= STMT_VINFO_RELATED_STMT (stmt_info
);
1551 stmt_info
= vinfo_for_stmt (stmt
);
1553 if ((STMT_VINFO_RELEVANT_P (stmt_info
)
1554 || VECTORIZABLE_CYCLE_DEF (STMT_VINFO_DEF_TYPE (stmt_info
)))
1555 && !PURE_SLP_STMT (stmt_info
))
1556 /* STMT needs both SLP and loop-based vectorization. */
1557 only_slp_in_loop
= false;
1561 if (only_slp_in_loop
)
1562 vectorization_factor
= LOOP_VINFO_SLP_UNROLLING_FACTOR (loop_vinfo
);
1564 vectorization_factor
1565 = least_common_multiple (vectorization_factor
,
1566 LOOP_VINFO_SLP_UNROLLING_FACTOR (loop_vinfo
));
1568 LOOP_VINFO_VECT_FACTOR (loop_vinfo
) = vectorization_factor
;
1569 if (dump_enabled_p ())
1570 dump_printf_loc (MSG_NOTE
, vect_location
,
1571 "Updating vectorization factor to %d\n",
1572 vectorization_factor
);
1575 /* Function vect_analyze_loop_operations.
1577 Scan the loop stmts and make sure they are all vectorizable. */
1580 vect_analyze_loop_operations (loop_vec_info loop_vinfo
)
1582 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
1583 basic_block
*bbs
= LOOP_VINFO_BBS (loop_vinfo
);
1584 int nbbs
= loop
->num_nodes
;
1586 stmt_vec_info stmt_info
;
1587 bool need_to_vectorize
= false;
1590 if (dump_enabled_p ())
1591 dump_printf_loc (MSG_NOTE
, vect_location
,
1592 "=== vect_analyze_loop_operations ===\n");
1594 for (i
= 0; i
< nbbs
; i
++)
1596 basic_block bb
= bbs
[i
];
1598 for (gphi_iterator si
= gsi_start_phis (bb
); !gsi_end_p (si
);
1601 gphi
*phi
= si
.phi ();
1604 stmt_info
= vinfo_for_stmt (phi
);
1605 if (dump_enabled_p ())
1607 dump_printf_loc (MSG_NOTE
, vect_location
, "examining phi: ");
1608 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
, phi
, 0);
1609 dump_printf (MSG_NOTE
, "\n");
1611 if (virtual_operand_p (gimple_phi_result (phi
)))
1614 /* Inner-loop loop-closed exit phi in outer-loop vectorization
1615 (i.e., a phi in the tail of the outer-loop). */
1616 if (! is_loop_header_bb_p (bb
))
1618 /* FORNOW: we currently don't support the case that these phis
1619 are not used in the outerloop (unless it is double reduction,
1620 i.e., this phi is vect_reduction_def), cause this case
1621 requires to actually do something here. */
1622 if ((!STMT_VINFO_RELEVANT_P (stmt_info
)
1623 || STMT_VINFO_LIVE_P (stmt_info
))
1624 && STMT_VINFO_DEF_TYPE (stmt_info
)
1625 != vect_double_reduction_def
)
1627 if (dump_enabled_p ())
1628 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1629 "Unsupported loop-closed phi in "
1634 /* If PHI is used in the outer loop, we check that its operand
1635 is defined in the inner loop. */
1636 if (STMT_VINFO_RELEVANT_P (stmt_info
))
1639 gimple
*op_def_stmt
;
1641 if (gimple_phi_num_args (phi
) != 1)
1644 phi_op
= PHI_ARG_DEF (phi
, 0);
1645 if (TREE_CODE (phi_op
) != SSA_NAME
)
1648 op_def_stmt
= SSA_NAME_DEF_STMT (phi_op
);
1649 if (gimple_nop_p (op_def_stmt
)
1650 || !flow_bb_inside_loop_p (loop
, gimple_bb (op_def_stmt
))
1651 || !vinfo_for_stmt (op_def_stmt
))
1654 if (STMT_VINFO_RELEVANT (vinfo_for_stmt (op_def_stmt
))
1655 != vect_used_in_outer
1656 && STMT_VINFO_RELEVANT (vinfo_for_stmt (op_def_stmt
))
1657 != vect_used_in_outer_by_reduction
)
1664 gcc_assert (stmt_info
);
1666 if (STMT_VINFO_LIVE_P (stmt_info
))
1668 /* FORNOW: not yet supported. */
1669 if (dump_enabled_p ())
1670 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1671 "not vectorized: value used after loop.\n");
1675 if (STMT_VINFO_RELEVANT (stmt_info
) == vect_used_in_scope
1676 && STMT_VINFO_DEF_TYPE (stmt_info
) != vect_induction_def
)
1678 /* A scalar-dependence cycle that we don't support. */
1679 if (dump_enabled_p ())
1680 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1681 "not vectorized: scalar dependence cycle.\n");
1685 if (STMT_VINFO_RELEVANT_P (stmt_info
))
1687 need_to_vectorize
= true;
1688 if (STMT_VINFO_DEF_TYPE (stmt_info
) == vect_induction_def
)
1689 ok
= vectorizable_induction (phi
, NULL
, NULL
);
1694 if (dump_enabled_p ())
1696 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1697 "not vectorized: relevant phi not "
1699 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
, phi
, 0);
1700 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
1706 for (gimple_stmt_iterator si
= gsi_start_bb (bb
); !gsi_end_p (si
);
1709 gimple
*stmt
= gsi_stmt (si
);
1710 if (!gimple_clobber_p (stmt
)
1711 && !vect_analyze_stmt (stmt
, &need_to_vectorize
, NULL
))
1716 /* All operations in the loop are either irrelevant (deal with loop
1717 control, or dead), or only used outside the loop and can be moved
1718 out of the loop (e.g. invariants, inductions). The loop can be
1719 optimized away by scalar optimizations. We're better off not
1720 touching this loop. */
1721 if (!need_to_vectorize
)
1723 if (dump_enabled_p ())
1724 dump_printf_loc (MSG_NOTE
, vect_location
,
1725 "All the computation can be taken out of the loop.\n");
1726 if (dump_enabled_p ())
1727 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1728 "not vectorized: redundant loop. no profit to "
1737 /* Function vect_analyze_loop_2.
1739 Apply a set of analyses on LOOP, and create a loop_vec_info struct
1740 for it. The different analyses will record information in the
1741 loop_vec_info struct. */
1743 vect_analyze_loop_2 (loop_vec_info loop_vinfo
, bool &fatal
)
1746 int max_vf
= MAX_VECTORIZATION_FACTOR
;
1748 unsigned int n_stmts
= 0;
1750 /* The first group of checks is independent of the vector size. */
1753 /* Find all data references in the loop (which correspond to vdefs/vuses)
1754 and analyze their evolution in the loop. */
1756 basic_block
*bbs
= LOOP_VINFO_BBS (loop_vinfo
);
1758 loop_p loop
= LOOP_VINFO_LOOP (loop_vinfo
);
1759 if (!find_loop_nest (loop
, &LOOP_VINFO_LOOP_NEST (loop_vinfo
)))
1761 if (dump_enabled_p ())
1762 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1763 "not vectorized: loop contains function calls"
1764 " or data references that cannot be analyzed\n");
1768 for (unsigned i
= 0; i
< loop
->num_nodes
; i
++)
1769 for (gimple_stmt_iterator gsi
= gsi_start_bb (bbs
[i
]);
1770 !gsi_end_p (gsi
); gsi_next (&gsi
))
1772 gimple
*stmt
= gsi_stmt (gsi
);
1773 if (is_gimple_debug (stmt
))
1776 if (!find_data_references_in_stmt (loop
, stmt
,
1777 &LOOP_VINFO_DATAREFS (loop_vinfo
)))
1779 if (is_gimple_call (stmt
) && loop
->safelen
)
1781 tree fndecl
= gimple_call_fndecl (stmt
), op
;
1782 if (fndecl
!= NULL_TREE
)
1784 cgraph_node
*node
= cgraph_node::get (fndecl
);
1785 if (node
!= NULL
&& node
->simd_clones
!= NULL
)
1787 unsigned int j
, n
= gimple_call_num_args (stmt
);
1788 for (j
= 0; j
< n
; j
++)
1790 op
= gimple_call_arg (stmt
, j
);
1792 || (REFERENCE_CLASS_P (op
)
1793 && get_base_address (op
)))
1796 op
= gimple_call_lhs (stmt
);
1797 /* Ignore #pragma omp declare simd functions
1798 if they don't have data references in the
1799 call stmt itself. */
1803 || (REFERENCE_CLASS_P (op
)
1804 && get_base_address (op
)))))
1809 if (dump_enabled_p ())
1810 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1811 "not vectorized: loop contains function "
1812 "calls or data references that cannot "
1818 /* Analyze the data references and also adjust the minimal
1819 vectorization factor according to the loads and stores. */
1821 ok
= vect_analyze_data_refs (loop_vinfo
, &min_vf
);
1824 if (dump_enabled_p ())
1825 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1826 "bad data references.\n");
1830 /* Classify all cross-iteration scalar data-flow cycles.
1831 Cross-iteration cycles caused by virtual phis are analyzed separately. */
1832 vect_analyze_scalar_cycles (loop_vinfo
);
1834 vect_pattern_recog (loop_vinfo
);
1836 vect_fixup_scalar_cycles_with_patterns (loop_vinfo
);
1838 /* Analyze the access patterns of the data-refs in the loop (consecutive,
1839 complex, etc.). FORNOW: Only handle consecutive access pattern. */
1841 ok
= vect_analyze_data_ref_accesses (loop_vinfo
);
1844 if (dump_enabled_p ())
1845 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1846 "bad data access.\n");
1850 /* Data-flow analysis to detect stmts that do not need to be vectorized. */
1852 ok
= vect_mark_stmts_to_be_vectorized (loop_vinfo
);
1855 if (dump_enabled_p ())
1856 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1857 "unexpected pattern.\n");
1861 /* While the rest of the analysis below depends on it in some way. */
1864 /* Analyze data dependences between the data-refs in the loop
1865 and adjust the maximum vectorization factor according to
1867 FORNOW: fail at the first data dependence that we encounter. */
1869 ok
= vect_analyze_data_ref_dependences (loop_vinfo
, &max_vf
);
1873 if (dump_enabled_p ())
1874 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1875 "bad data dependence.\n");
1879 ok
= vect_determine_vectorization_factor (loop_vinfo
);
1882 if (dump_enabled_p ())
1883 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1884 "can't determine vectorization factor.\n");
1887 if (max_vf
< LOOP_VINFO_VECT_FACTOR (loop_vinfo
))
1889 if (dump_enabled_p ())
1890 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1891 "bad data dependence.\n");
1895 /* Compute the scalar iteration cost. */
1896 vect_compute_single_scalar_iteration_cost (loop_vinfo
);
1898 int saved_vectorization_factor
= LOOP_VINFO_VECT_FACTOR (loop_vinfo
);
1899 HOST_WIDE_INT estimated_niter
;
1901 int min_scalar_loop_bound
;
1903 /* Check the SLP opportunities in the loop, analyze and build SLP trees. */
1904 ok
= vect_analyze_slp (loop_vinfo
, n_stmts
);
1908 /* If there are any SLP instances mark them as pure_slp. */
1909 bool slp
= vect_make_slp_decision (loop_vinfo
);
1912 /* Find stmts that need to be both vectorized and SLPed. */
1913 vect_detect_hybrid_slp (loop_vinfo
);
1915 /* Update the vectorization factor based on the SLP decision. */
1916 vect_update_vf_for_slp (loop_vinfo
);
1919 /* This is the point where we can re-start analysis with SLP forced off. */
1922 /* Now the vectorization factor is final. */
1923 unsigned vectorization_factor
= LOOP_VINFO_VECT_FACTOR (loop_vinfo
);
1924 gcc_assert (vectorization_factor
!= 0);
1926 if (LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo
) && dump_enabled_p ())
1927 dump_printf_loc (MSG_NOTE
, vect_location
,
1928 "vectorization_factor = %d, niters = "
1929 HOST_WIDE_INT_PRINT_DEC
"\n", vectorization_factor
,
1930 LOOP_VINFO_INT_NITERS (loop_vinfo
));
1932 HOST_WIDE_INT max_niter
1933 = max_stmt_executions_int (LOOP_VINFO_LOOP (loop_vinfo
));
1934 if ((LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo
)
1935 && (LOOP_VINFO_INT_NITERS (loop_vinfo
) < vectorization_factor
))
1937 && (unsigned HOST_WIDE_INT
) max_niter
< vectorization_factor
))
1939 if (dump_enabled_p ())
1940 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1941 "not vectorized: iteration count smaller than "
1942 "vectorization factor.\n");
1946 /* Analyze the alignment of the data-refs in the loop.
1947 Fail if a data reference is found that cannot be vectorized. */
1949 ok
= vect_analyze_data_refs_alignment (loop_vinfo
);
1952 if (dump_enabled_p ())
1953 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1954 "bad data alignment.\n");
1958 /* Prune the list of ddrs to be tested at run-time by versioning for alias.
1959 It is important to call pruning after vect_analyze_data_ref_accesses,
1960 since we use grouping information gathered by interleaving analysis. */
1961 ok
= vect_prune_runtime_alias_test_list (loop_vinfo
);
1964 if (dump_enabled_p ())
1965 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1966 "number of versioning for alias "
1967 "run-time tests exceeds %d "
1968 "(--param vect-max-version-for-alias-checks)\n",
1969 PARAM_VALUE (PARAM_VECT_MAX_VERSION_FOR_ALIAS_CHECKS
));
1973 /* This pass will decide on using loop versioning and/or loop peeling in
1974 order to enhance the alignment of data references in the loop. */
1975 ok
= vect_enhance_data_refs_alignment (loop_vinfo
);
1978 if (dump_enabled_p ())
1979 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
1980 "bad data alignment.\n");
1986 /* Analyze operations in the SLP instances. Note this may
1987 remove unsupported SLP instances which makes the above
1988 SLP kind detection invalid. */
1989 unsigned old_size
= LOOP_VINFO_SLP_INSTANCES (loop_vinfo
).length ();
1990 vect_slp_analyze_operations (LOOP_VINFO_SLP_INSTANCES (loop_vinfo
),
1991 LOOP_VINFO_TARGET_COST_DATA (loop_vinfo
));
1992 if (LOOP_VINFO_SLP_INSTANCES (loop_vinfo
).length () != old_size
)
1996 /* Scan all the remaining operations in the loop that are not subject
1997 to SLP and make sure they are vectorizable. */
1998 ok
= vect_analyze_loop_operations (loop_vinfo
);
2001 if (dump_enabled_p ())
2002 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2003 "bad operation or unsupported loop bound.\n");
2007 /* Analyze cost. Decide if worth while to vectorize. */
2008 int min_profitable_estimate
, min_profitable_iters
;
2009 vect_estimate_min_profitable_iters (loop_vinfo
, &min_profitable_iters
,
2010 &min_profitable_estimate
);
2012 if (min_profitable_iters
< 0)
2014 if (dump_enabled_p ())
2015 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2016 "not vectorized: vectorization not profitable.\n");
2017 if (dump_enabled_p ())
2018 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2019 "not vectorized: vector version will never be "
2024 min_scalar_loop_bound
= ((PARAM_VALUE (PARAM_MIN_VECT_LOOP_BOUND
)
2025 * vectorization_factor
) - 1);
2027 /* Use the cost model only if it is more conservative than user specified
2029 th
= (unsigned) min_scalar_loop_bound
;
2030 if (min_profitable_iters
2031 && (!min_scalar_loop_bound
2032 || min_profitable_iters
> min_scalar_loop_bound
))
2033 th
= (unsigned) min_profitable_iters
;
2035 LOOP_VINFO_COST_MODEL_THRESHOLD (loop_vinfo
) = th
;
2037 if (LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo
)
2038 && LOOP_VINFO_INT_NITERS (loop_vinfo
) <= th
)
2040 if (dump_enabled_p ())
2041 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2042 "not vectorized: vectorization not profitable.\n");
2043 if (dump_enabled_p ())
2044 dump_printf_loc (MSG_NOTE
, vect_location
,
2045 "not vectorized: iteration count smaller than user "
2046 "specified loop bound parameter or minimum profitable "
2047 "iterations (whichever is more conservative).\n");
2052 = estimated_stmt_executions_int (LOOP_VINFO_LOOP (loop_vinfo
));
2053 if (estimated_niter
!= -1
2054 && ((unsigned HOST_WIDE_INT
) estimated_niter
2055 <= MAX (th
, (unsigned)min_profitable_estimate
)))
2057 if (dump_enabled_p ())
2058 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2059 "not vectorized: estimated iteration count too "
2061 if (dump_enabled_p ())
2062 dump_printf_loc (MSG_NOTE
, vect_location
,
2063 "not vectorized: estimated iteration count smaller "
2064 "than specified loop bound parameter or minimum "
2065 "profitable iterations (whichever is more "
2066 "conservative).\n");
2070 /* Decide whether we need to create an epilogue loop to handle
2071 remaining scalar iterations. */
2072 th
= ((LOOP_VINFO_COST_MODEL_THRESHOLD (loop_vinfo
) + 1)
2073 / LOOP_VINFO_VECT_FACTOR (loop_vinfo
))
2074 * LOOP_VINFO_VECT_FACTOR (loop_vinfo
);
2076 if (LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo
)
2077 && LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo
) > 0)
2079 if (ctz_hwi (LOOP_VINFO_INT_NITERS (loop_vinfo
)
2080 - LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo
))
2081 < exact_log2 (LOOP_VINFO_VECT_FACTOR (loop_vinfo
)))
2082 LOOP_VINFO_PEELING_FOR_NITER (loop_vinfo
) = true;
2084 else if (LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo
)
2085 || (tree_ctz (LOOP_VINFO_NITERS (loop_vinfo
))
2086 < (unsigned)exact_log2 (LOOP_VINFO_VECT_FACTOR (loop_vinfo
))
2087 /* In case of versioning, check if the maximum number of
2088 iterations is greater than th. If they are identical,
2089 the epilogue is unnecessary. */
2090 && ((!LOOP_REQUIRES_VERSIONING_FOR_ALIAS (loop_vinfo
)
2091 && !LOOP_REQUIRES_VERSIONING_FOR_ALIGNMENT (loop_vinfo
))
2092 || (unsigned HOST_WIDE_INT
) max_niter
> th
)))
2093 LOOP_VINFO_PEELING_FOR_NITER (loop_vinfo
) = true;
2095 /* If an epilogue loop is required make sure we can create one. */
2096 if (LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo
)
2097 || LOOP_VINFO_PEELING_FOR_NITER (loop_vinfo
))
2099 if (dump_enabled_p ())
2100 dump_printf_loc (MSG_NOTE
, vect_location
, "epilog loop required\n");
2101 if (!vect_can_advance_ivs_p (loop_vinfo
)
2102 || !slpeel_can_duplicate_loop_p (LOOP_VINFO_LOOP (loop_vinfo
),
2103 single_exit (LOOP_VINFO_LOOP
2106 if (dump_enabled_p ())
2107 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2108 "not vectorized: can't create required "
2114 gcc_assert (vectorization_factor
2115 == (unsigned)LOOP_VINFO_VECT_FACTOR (loop_vinfo
));
2117 /* Ok to vectorize! */
2121 /* Try again with SLP forced off but if we didn't do any SLP there is
2122 no point in re-trying. */
2126 /* If there are reduction chains re-trying will fail anyway. */
2127 if (! LOOP_VINFO_REDUCTION_CHAINS (loop_vinfo
).is_empty ())
2130 /* Likewise if the grouped loads or stores in the SLP cannot be handled
2131 via interleaving or lane instructions. */
2132 slp_instance instance
;
2135 FOR_EACH_VEC_ELT (LOOP_VINFO_SLP_INSTANCES (loop_vinfo
), i
, instance
)
2137 stmt_vec_info vinfo
;
2138 vinfo
= vinfo_for_stmt
2139 (SLP_TREE_SCALAR_STMTS (SLP_INSTANCE_TREE (instance
))[0]);
2140 if (! STMT_VINFO_GROUPED_ACCESS (vinfo
))
2142 vinfo
= vinfo_for_stmt (STMT_VINFO_GROUP_FIRST_ELEMENT (vinfo
));
2143 unsigned int size
= STMT_VINFO_GROUP_SIZE (vinfo
);
2144 tree vectype
= STMT_VINFO_VECTYPE (vinfo
);
2145 if (! vect_store_lanes_supported (vectype
, size
)
2146 && ! vect_grouped_store_supported (vectype
, size
))
2148 FOR_EACH_VEC_ELT (SLP_INSTANCE_LOADS (instance
), j
, node
)
2150 vinfo
= vinfo_for_stmt (SLP_TREE_SCALAR_STMTS (node
)[0]);
2151 vinfo
= vinfo_for_stmt (STMT_VINFO_GROUP_FIRST_ELEMENT (vinfo
));
2152 size
= STMT_VINFO_GROUP_SIZE (vinfo
);
2153 vectype
= STMT_VINFO_VECTYPE (vinfo
);
2154 if (! vect_load_lanes_supported (vectype
, size
)
2155 && ! vect_grouped_load_supported (vectype
, size
))
2160 if (dump_enabled_p ())
2161 dump_printf_loc (MSG_NOTE
, vect_location
,
2162 "re-trying with SLP disabled\n");
2164 /* Roll back state appropriately. No SLP this time. */
2166 /* Restore vectorization factor as it were without SLP. */
2167 LOOP_VINFO_VECT_FACTOR (loop_vinfo
) = saved_vectorization_factor
;
2168 /* Free the SLP instances. */
2169 FOR_EACH_VEC_ELT (LOOP_VINFO_SLP_INSTANCES (loop_vinfo
), j
, instance
)
2170 vect_free_slp_instance (instance
);
2171 LOOP_VINFO_SLP_INSTANCES (loop_vinfo
).release ();
2172 /* Reset SLP type to loop_vect on all stmts. */
2173 for (i
= 0; i
< LOOP_VINFO_LOOP (loop_vinfo
)->num_nodes
; ++i
)
2175 basic_block bb
= LOOP_VINFO_BBS (loop_vinfo
)[i
];
2176 for (gimple_stmt_iterator si
= gsi_start_bb (bb
);
2177 !gsi_end_p (si
); gsi_next (&si
))
2179 stmt_vec_info stmt_info
= vinfo_for_stmt (gsi_stmt (si
));
2180 if (STMT_VINFO_IN_PATTERN_P (stmt_info
))
2182 gcc_assert (STMT_SLP_TYPE (stmt_info
) == loop_vect
);
2183 stmt_info
= vinfo_for_stmt (STMT_VINFO_RELATED_STMT (stmt_info
));
2184 for (gimple_stmt_iterator pi
2185 = gsi_start (STMT_VINFO_PATTERN_DEF_SEQ (stmt_info
));
2186 !gsi_end_p (pi
); gsi_next (&pi
))
2188 gimple
*pstmt
= gsi_stmt (pi
);
2189 STMT_SLP_TYPE (vinfo_for_stmt (pstmt
)) = loop_vect
;
2192 STMT_SLP_TYPE (stmt_info
) = loop_vect
;
2195 /* Free optimized alias test DDRS. */
2196 LOOP_VINFO_COMP_ALIAS_DDRS (loop_vinfo
).release ();
2197 /* Reset target cost data. */
2198 destroy_cost_data (LOOP_VINFO_TARGET_COST_DATA (loop_vinfo
));
2199 LOOP_VINFO_TARGET_COST_DATA (loop_vinfo
)
2200 = init_cost (LOOP_VINFO_LOOP (loop_vinfo
));
2201 /* Reset assorted flags. */
2202 LOOP_VINFO_PEELING_FOR_NITER (loop_vinfo
) = false;
2203 LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo
) = false;
2204 LOOP_VINFO_COST_MODEL_THRESHOLD (loop_vinfo
) = 0;
2209 /* Function vect_analyze_loop.
2211 Apply a set of analyses on LOOP, and create a loop_vec_info struct
2212 for it. The different analyses will record information in the
2213 loop_vec_info struct. */
2215 vect_analyze_loop (struct loop
*loop
)
2217 loop_vec_info loop_vinfo
;
2218 unsigned int vector_sizes
;
2220 /* Autodetect first vector size we try. */
2221 current_vector_size
= 0;
2222 vector_sizes
= targetm
.vectorize
.autovectorize_vector_sizes ();
2224 if (dump_enabled_p ())
2225 dump_printf_loc (MSG_NOTE
, vect_location
,
2226 "===== analyze_loop_nest =====\n");
2228 if (loop_outer (loop
)
2229 && loop_vec_info_for_loop (loop_outer (loop
))
2230 && LOOP_VINFO_VECTORIZABLE_P (loop_vec_info_for_loop (loop_outer (loop
))))
2232 if (dump_enabled_p ())
2233 dump_printf_loc (MSG_NOTE
, vect_location
,
2234 "outer-loop already vectorized.\n");
2240 /* Check the CFG characteristics of the loop (nesting, entry/exit). */
2241 loop_vinfo
= vect_analyze_loop_form (loop
);
2244 if (dump_enabled_p ())
2245 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2246 "bad loop form.\n");
2251 if (vect_analyze_loop_2 (loop_vinfo
, fatal
))
2253 LOOP_VINFO_VECTORIZABLE_P (loop_vinfo
) = 1;
2258 destroy_loop_vec_info (loop_vinfo
, true);
2260 vector_sizes
&= ~current_vector_size
;
2262 || vector_sizes
== 0
2263 || current_vector_size
== 0)
2266 /* Try the next biggest vector size. */
2267 current_vector_size
= 1 << floor_log2 (vector_sizes
);
2268 if (dump_enabled_p ())
2269 dump_printf_loc (MSG_NOTE
, vect_location
,
2270 "***** Re-trying analysis with "
2271 "vector size %d\n", current_vector_size
);
2276 /* Function reduction_code_for_scalar_code
2279 CODE - tree_code of a reduction operations.
2282 REDUC_CODE - the corresponding tree-code to be used to reduce the
2283 vector of partial results into a single scalar result, or ERROR_MARK
2284 if the operation is a supported reduction operation, but does not have
2287 Return FALSE if CODE currently cannot be vectorized as reduction. */
2290 reduction_code_for_scalar_code (enum tree_code code
,
2291 enum tree_code
*reduc_code
)
2296 *reduc_code
= REDUC_MAX_EXPR
;
2300 *reduc_code
= REDUC_MIN_EXPR
;
2304 *reduc_code
= REDUC_PLUS_EXPR
;
2312 *reduc_code
= ERROR_MARK
;
2321 /* Error reporting helper for vect_is_simple_reduction below. GIMPLE statement
2322 STMT is printed with a message MSG. */
2325 report_vect_op (int msg_type
, gimple
*stmt
, const char *msg
)
2327 dump_printf_loc (msg_type
, vect_location
, "%s", msg
);
2328 dump_gimple_stmt (msg_type
, TDF_SLIM
, stmt
, 0);
2329 dump_printf (msg_type
, "\n");
2333 /* Detect SLP reduction of the form:
2343 PHI is the reduction phi node (#a1 = phi <a5, a0> above)
2344 FIRST_STMT is the first reduction stmt in the chain
2345 (a2 = operation (a1)).
2347 Return TRUE if a reduction chain was detected. */
2350 vect_is_slp_reduction (loop_vec_info loop_info
, gimple
*phi
,
2353 struct loop
*loop
= (gimple_bb (phi
))->loop_father
;
2354 struct loop
*vect_loop
= LOOP_VINFO_LOOP (loop_info
);
2355 enum tree_code code
;
2356 gimple
*current_stmt
= NULL
, *loop_use_stmt
= NULL
, *first
, *next_stmt
;
2357 stmt_vec_info use_stmt_info
, current_stmt_info
;
2359 imm_use_iterator imm_iter
;
2360 use_operand_p use_p
;
2361 int nloop_uses
, size
= 0, n_out_of_loop_uses
;
2364 if (loop
!= vect_loop
)
2367 lhs
= PHI_RESULT (phi
);
2368 code
= gimple_assign_rhs_code (first_stmt
);
2372 n_out_of_loop_uses
= 0;
2373 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, lhs
)
2375 gimple
*use_stmt
= USE_STMT (use_p
);
2376 if (is_gimple_debug (use_stmt
))
2379 /* Check if we got back to the reduction phi. */
2380 if (use_stmt
== phi
)
2382 loop_use_stmt
= use_stmt
;
2387 if (flow_bb_inside_loop_p (loop
, gimple_bb (use_stmt
)))
2389 loop_use_stmt
= use_stmt
;
2393 n_out_of_loop_uses
++;
2395 /* There are can be either a single use in the loop or two uses in
2397 if (nloop_uses
> 1 || (n_out_of_loop_uses
&& nloop_uses
))
2404 /* We reached a statement with no loop uses. */
2405 if (nloop_uses
== 0)
2408 /* This is a loop exit phi, and we haven't reached the reduction phi. */
2409 if (gimple_code (loop_use_stmt
) == GIMPLE_PHI
)
2412 if (!is_gimple_assign (loop_use_stmt
)
2413 || code
!= gimple_assign_rhs_code (loop_use_stmt
)
2414 || !flow_bb_inside_loop_p (loop
, gimple_bb (loop_use_stmt
)))
2417 /* Insert USE_STMT into reduction chain. */
2418 use_stmt_info
= vinfo_for_stmt (loop_use_stmt
);
2421 current_stmt_info
= vinfo_for_stmt (current_stmt
);
2422 GROUP_NEXT_ELEMENT (current_stmt_info
) = loop_use_stmt
;
2423 GROUP_FIRST_ELEMENT (use_stmt_info
)
2424 = GROUP_FIRST_ELEMENT (current_stmt_info
);
2427 GROUP_FIRST_ELEMENT (use_stmt_info
) = loop_use_stmt
;
2429 lhs
= gimple_assign_lhs (loop_use_stmt
);
2430 current_stmt
= loop_use_stmt
;
2434 if (!found
|| loop_use_stmt
!= phi
|| size
< 2)
2437 /* Swap the operands, if needed, to make the reduction operand be the second
2439 lhs
= PHI_RESULT (phi
);
2440 next_stmt
= GROUP_FIRST_ELEMENT (vinfo_for_stmt (current_stmt
));
2443 if (gimple_assign_rhs2 (next_stmt
) == lhs
)
2445 tree op
= gimple_assign_rhs1 (next_stmt
);
2446 gimple
*def_stmt
= NULL
;
2448 if (TREE_CODE (op
) == SSA_NAME
)
2449 def_stmt
= SSA_NAME_DEF_STMT (op
);
2451 /* Check that the other def is either defined in the loop
2452 ("vect_internal_def"), or it's an induction (defined by a
2453 loop-header phi-node). */
2455 && gimple_bb (def_stmt
)
2456 && flow_bb_inside_loop_p (loop
, gimple_bb (def_stmt
))
2457 && (is_gimple_assign (def_stmt
)
2458 || is_gimple_call (def_stmt
)
2459 || STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def_stmt
))
2460 == vect_induction_def
2461 || (gimple_code (def_stmt
) == GIMPLE_PHI
2462 && STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def_stmt
))
2463 == vect_internal_def
2464 && !is_loop_header_bb_p (gimple_bb (def_stmt
)))))
2466 lhs
= gimple_assign_lhs (next_stmt
);
2467 next_stmt
= GROUP_NEXT_ELEMENT (vinfo_for_stmt (next_stmt
));
2475 tree op
= gimple_assign_rhs2 (next_stmt
);
2476 gimple
*def_stmt
= NULL
;
2478 if (TREE_CODE (op
) == SSA_NAME
)
2479 def_stmt
= SSA_NAME_DEF_STMT (op
);
2481 /* Check that the other def is either defined in the loop
2482 ("vect_internal_def"), or it's an induction (defined by a
2483 loop-header phi-node). */
2485 && gimple_bb (def_stmt
)
2486 && flow_bb_inside_loop_p (loop
, gimple_bb (def_stmt
))
2487 && (is_gimple_assign (def_stmt
)
2488 || is_gimple_call (def_stmt
)
2489 || STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def_stmt
))
2490 == vect_induction_def
2491 || (gimple_code (def_stmt
) == GIMPLE_PHI
2492 && STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def_stmt
))
2493 == vect_internal_def
2494 && !is_loop_header_bb_p (gimple_bb (def_stmt
)))))
2496 if (dump_enabled_p ())
2498 dump_printf_loc (MSG_NOTE
, vect_location
, "swapping oprnds: ");
2499 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
, next_stmt
, 0);
2500 dump_printf (MSG_NOTE
, "\n");
2503 swap_ssa_operands (next_stmt
,
2504 gimple_assign_rhs1_ptr (next_stmt
),
2505 gimple_assign_rhs2_ptr (next_stmt
));
2506 update_stmt (next_stmt
);
2508 if (CONSTANT_CLASS_P (gimple_assign_rhs1 (next_stmt
)))
2509 LOOP_VINFO_OPERANDS_SWAPPED (loop_info
) = true;
2515 lhs
= gimple_assign_lhs (next_stmt
);
2516 next_stmt
= GROUP_NEXT_ELEMENT (vinfo_for_stmt (next_stmt
));
2519 /* Save the chain for further analysis in SLP detection. */
2520 first
= GROUP_FIRST_ELEMENT (vinfo_for_stmt (current_stmt
));
2521 LOOP_VINFO_REDUCTION_CHAINS (loop_info
).safe_push (first
);
2522 GROUP_SIZE (vinfo_for_stmt (first
)) = size
;
2528 /* Function vect_is_simple_reduction_1
2530 (1) Detect a cross-iteration def-use cycle that represents a simple
2531 reduction computation. We look for the following pattern:
2536 a2 = operation (a3, a1)
2543 a2 = operation (a3, a1)
2546 1. operation is commutative and associative and it is safe to
2547 change the order of the computation (if CHECK_REDUCTION is true)
2548 2. no uses for a2 in the loop (a2 is used out of the loop)
2549 3. no uses of a1 in the loop besides the reduction operation
2550 4. no uses of a1 outside the loop.
2552 Conditions 1,4 are tested here.
2553 Conditions 2,3 are tested in vect_mark_stmts_to_be_vectorized.
2555 (2) Detect a cross-iteration def-use cycle in nested loops, i.e.,
2556 nested cycles, if CHECK_REDUCTION is false.
2558 (3) Detect cycles of phi nodes in outer-loop vectorization, i.e., double
2562 inner loop (def of a3)
2565 (4) Detect condition expressions, ie:
2566 for (int i = 0; i < N; i++)
2573 vect_is_simple_reduction (loop_vec_info loop_info
, gimple
*phi
,
2574 bool check_reduction
, bool *double_reduc
,
2575 bool need_wrapping_integral_overflow
,
2576 enum vect_reduction_type
*v_reduc_type
)
2578 struct loop
*loop
= (gimple_bb (phi
))->loop_father
;
2579 struct loop
*vect_loop
= LOOP_VINFO_LOOP (loop_info
);
2580 edge latch_e
= loop_latch_edge (loop
);
2581 tree loop_arg
= PHI_ARG_DEF_FROM_EDGE (phi
, latch_e
);
2582 gimple
*def_stmt
, *def1
= NULL
, *def2
= NULL
;
2583 enum tree_code orig_code
, code
;
2584 tree op1
, op2
, op3
= NULL_TREE
, op4
= NULL_TREE
;
2588 imm_use_iterator imm_iter
;
2589 use_operand_p use_p
;
2592 *double_reduc
= false;
2593 *v_reduc_type
= TREE_CODE_REDUCTION
;
2595 /* If CHECK_REDUCTION is true, we assume inner-most loop vectorization,
2596 otherwise, we assume outer loop vectorization. */
2597 gcc_assert ((check_reduction
&& loop
== vect_loop
)
2598 || (!check_reduction
&& flow_loop_nested_p (vect_loop
, loop
)));
2600 name
= PHI_RESULT (phi
);
2601 /* ??? If there are no uses of the PHI result the inner loop reduction
2602 won't be detected as possibly double-reduction by vectorizable_reduction
2603 because that tries to walk the PHI arg from the preheader edge which
2604 can be constant. See PR60382. */
2605 if (has_zero_uses (name
))
2608 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, name
)
2610 gimple
*use_stmt
= USE_STMT (use_p
);
2611 if (is_gimple_debug (use_stmt
))
2614 if (!flow_bb_inside_loop_p (loop
, gimple_bb (use_stmt
)))
2616 if (dump_enabled_p ())
2617 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2618 "intermediate value used outside loop.\n");
2626 if (dump_enabled_p ())
2627 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2628 "reduction used in loop.\n");
2633 if (TREE_CODE (loop_arg
) != SSA_NAME
)
2635 if (dump_enabled_p ())
2637 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2638 "reduction: not ssa_name: ");
2639 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
, loop_arg
);
2640 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
2645 def_stmt
= SSA_NAME_DEF_STMT (loop_arg
);
2648 if (dump_enabled_p ())
2649 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2650 "reduction: no def_stmt.\n");
2654 if (!is_gimple_assign (def_stmt
) && gimple_code (def_stmt
) != GIMPLE_PHI
)
2656 if (dump_enabled_p ())
2658 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
, def_stmt
, 0);
2659 dump_printf (MSG_NOTE
, "\n");
2664 if (is_gimple_assign (def_stmt
))
2666 name
= gimple_assign_lhs (def_stmt
);
2671 name
= PHI_RESULT (def_stmt
);
2676 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, name
)
2678 gimple
*use_stmt
= USE_STMT (use_p
);
2679 if (is_gimple_debug (use_stmt
))
2681 if (flow_bb_inside_loop_p (loop
, gimple_bb (use_stmt
)))
2685 if (dump_enabled_p ())
2686 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2687 "reduction used in loop.\n");
2692 /* If DEF_STMT is a phi node itself, we expect it to have a single argument
2693 defined in the inner loop. */
2696 op1
= PHI_ARG_DEF (def_stmt
, 0);
2698 if (gimple_phi_num_args (def_stmt
) != 1
2699 || TREE_CODE (op1
) != SSA_NAME
)
2701 if (dump_enabled_p ())
2702 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
2703 "unsupported phi node definition.\n");
2708 def1
= SSA_NAME_DEF_STMT (op1
);
2709 if (gimple_bb (def1
)
2710 && flow_bb_inside_loop_p (loop
, gimple_bb (def_stmt
))
2712 && flow_bb_inside_loop_p (loop
->inner
, gimple_bb (def1
))
2713 && is_gimple_assign (def1
))
2715 if (dump_enabled_p ())
2716 report_vect_op (MSG_NOTE
, def_stmt
,
2717 "detected double reduction: ");
2719 *double_reduc
= true;
2726 code
= orig_code
= gimple_assign_rhs_code (def_stmt
);
2728 /* We can handle "res -= x[i]", which is non-associative by
2729 simply rewriting this into "res += -x[i]". Avoid changing
2730 gimple instruction for the first simple tests and only do this
2731 if we're allowed to change code at all. */
2732 if (code
== MINUS_EXPR
2733 && (op1
= gimple_assign_rhs1 (def_stmt
))
2734 && TREE_CODE (op1
) == SSA_NAME
2735 && SSA_NAME_DEF_STMT (op1
) == phi
)
2738 if (check_reduction
)
2740 if (code
== COND_EXPR
)
2741 *v_reduc_type
= COND_REDUCTION
;
2742 else if (!commutative_tree_code (code
) || !associative_tree_code (code
))
2744 if (dump_enabled_p ())
2745 report_vect_op (MSG_MISSED_OPTIMIZATION
, def_stmt
,
2746 "reduction: not commutative/associative: ");
2751 if (get_gimple_rhs_class (code
) != GIMPLE_BINARY_RHS
)
2753 if (code
!= COND_EXPR
)
2755 if (dump_enabled_p ())
2756 report_vect_op (MSG_MISSED_OPTIMIZATION
, def_stmt
,
2757 "reduction: not binary operation: ");
2762 op3
= gimple_assign_rhs1 (def_stmt
);
2763 if (COMPARISON_CLASS_P (op3
))
2765 op4
= TREE_OPERAND (op3
, 1);
2766 op3
= TREE_OPERAND (op3
, 0);
2769 op1
= gimple_assign_rhs2 (def_stmt
);
2770 op2
= gimple_assign_rhs3 (def_stmt
);
2772 if (TREE_CODE (op1
) != SSA_NAME
&& TREE_CODE (op2
) != SSA_NAME
)
2774 if (dump_enabled_p ())
2775 report_vect_op (MSG_MISSED_OPTIMIZATION
, def_stmt
,
2776 "reduction: uses not ssa_names: ");
2783 op1
= gimple_assign_rhs1 (def_stmt
);
2784 op2
= gimple_assign_rhs2 (def_stmt
);
2786 if (TREE_CODE (op1
) != SSA_NAME
&& TREE_CODE (op2
) != SSA_NAME
)
2788 if (dump_enabled_p ())
2789 report_vect_op (MSG_MISSED_OPTIMIZATION
, def_stmt
,
2790 "reduction: uses not ssa_names: ");
2796 type
= TREE_TYPE (gimple_assign_lhs (def_stmt
));
2797 if ((TREE_CODE (op1
) == SSA_NAME
2798 && !types_compatible_p (type
,TREE_TYPE (op1
)))
2799 || (TREE_CODE (op2
) == SSA_NAME
2800 && !types_compatible_p (type
, TREE_TYPE (op2
)))
2801 || (op3
&& TREE_CODE (op3
) == SSA_NAME
2802 && !types_compatible_p (type
, TREE_TYPE (op3
)))
2803 || (op4
&& TREE_CODE (op4
) == SSA_NAME
2804 && !types_compatible_p (type
, TREE_TYPE (op4
))))
2806 if (dump_enabled_p ())
2808 dump_printf_loc (MSG_NOTE
, vect_location
,
2809 "reduction: multiple types: operation type: ");
2810 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, type
);
2811 dump_printf (MSG_NOTE
, ", operands types: ");
2812 dump_generic_expr (MSG_NOTE
, TDF_SLIM
,
2814 dump_printf (MSG_NOTE
, ",");
2815 dump_generic_expr (MSG_NOTE
, TDF_SLIM
,
2819 dump_printf (MSG_NOTE
, ",");
2820 dump_generic_expr (MSG_NOTE
, TDF_SLIM
,
2826 dump_printf (MSG_NOTE
, ",");
2827 dump_generic_expr (MSG_NOTE
, TDF_SLIM
,
2830 dump_printf (MSG_NOTE
, "\n");
2836 /* Check that it's ok to change the order of the computation.
2837 Generally, when vectorizing a reduction we change the order of the
2838 computation. This may change the behavior of the program in some
2839 cases, so we need to check that this is ok. One exception is when
2840 vectorizing an outer-loop: the inner-loop is executed sequentially,
2841 and therefore vectorizing reductions in the inner-loop during
2842 outer-loop vectorization is safe. */
2844 if (*v_reduc_type
!= COND_REDUCTION
)
2846 /* CHECKME: check for !flag_finite_math_only too? */
2847 if (SCALAR_FLOAT_TYPE_P (type
) && !flag_associative_math
2850 /* Changing the order of operations changes the semantics. */
2851 if (dump_enabled_p ())
2852 report_vect_op (MSG_MISSED_OPTIMIZATION
, def_stmt
,
2853 "reduction: unsafe fp math optimization: ");
2856 else if (INTEGRAL_TYPE_P (type
) && check_reduction
)
2858 if (!operation_no_trapping_overflow (type
, code
))
2860 /* Changing the order of operations changes the semantics. */
2861 if (dump_enabled_p ())
2862 report_vect_op (MSG_MISSED_OPTIMIZATION
, def_stmt
,
2863 "reduction: unsafe int math optimization"
2864 " (overflow traps): ");
2867 if (need_wrapping_integral_overflow
2868 && !TYPE_OVERFLOW_WRAPS (type
)
2869 && operation_can_overflow (code
))
2871 /* Changing the order of operations changes the semantics. */
2872 if (dump_enabled_p ())
2873 report_vect_op (MSG_MISSED_OPTIMIZATION
, def_stmt
,
2874 "reduction: unsafe int math optimization"
2875 " (overflow doesn't wrap): ");
2879 else if (SAT_FIXED_POINT_TYPE_P (type
) && check_reduction
)
2881 /* Changing the order of operations changes the semantics. */
2882 if (dump_enabled_p ())
2883 report_vect_op (MSG_MISSED_OPTIMIZATION
, def_stmt
,
2884 "reduction: unsafe fixed-point math optimization: ");
2889 /* Reduction is safe. We're dealing with one of the following:
2890 1) integer arithmetic and no trapv
2891 2) floating point arithmetic, and special flags permit this optimization
2892 3) nested cycle (i.e., outer loop vectorization). */
2893 if (TREE_CODE (op1
) == SSA_NAME
)
2894 def1
= SSA_NAME_DEF_STMT (op1
);
2896 if (TREE_CODE (op2
) == SSA_NAME
)
2897 def2
= SSA_NAME_DEF_STMT (op2
);
2899 if (code
!= COND_EXPR
2900 && ((!def1
|| gimple_nop_p (def1
)) && (!def2
|| gimple_nop_p (def2
))))
2902 if (dump_enabled_p ())
2903 report_vect_op (MSG_NOTE
, def_stmt
, "reduction: no defs for operands: ");
2907 /* Check that one def is the reduction def, defined by PHI,
2908 the other def is either defined in the loop ("vect_internal_def"),
2909 or it's an induction (defined by a loop-header phi-node). */
2911 if (def2
&& def2
== phi
2912 && (code
== COND_EXPR
2913 || !def1
|| gimple_nop_p (def1
)
2914 || !flow_bb_inside_loop_p (loop
, gimple_bb (def1
))
2915 || (def1
&& flow_bb_inside_loop_p (loop
, gimple_bb (def1
))
2916 && (is_gimple_assign (def1
)
2917 || is_gimple_call (def1
)
2918 || STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def1
))
2919 == vect_induction_def
2920 || (gimple_code (def1
) == GIMPLE_PHI
2921 && STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def1
))
2922 == vect_internal_def
2923 && !is_loop_header_bb_p (gimple_bb (def1
)))))))
2925 if (dump_enabled_p ())
2926 report_vect_op (MSG_NOTE
, def_stmt
, "detected reduction: ");
2930 if (def1
&& def1
== phi
2931 && (code
== COND_EXPR
2932 || !def2
|| gimple_nop_p (def2
)
2933 || !flow_bb_inside_loop_p (loop
, gimple_bb (def2
))
2934 || (def2
&& flow_bb_inside_loop_p (loop
, gimple_bb (def2
))
2935 && (is_gimple_assign (def2
)
2936 || is_gimple_call (def2
)
2937 || STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def2
))
2938 == vect_induction_def
2939 || (gimple_code (def2
) == GIMPLE_PHI
2940 && STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def2
))
2941 == vect_internal_def
2942 && !is_loop_header_bb_p (gimple_bb (def2
)))))))
2945 && orig_code
!= MINUS_EXPR
)
2947 if (code
== COND_EXPR
)
2949 /* No current known use where this case would be useful. */
2950 if (dump_enabled_p ())
2951 report_vect_op (MSG_NOTE
, def_stmt
,
2952 "detected reduction: cannot currently swap "
2953 "operands for cond_expr");
2957 /* Swap operands (just for simplicity - so that the rest of the code
2958 can assume that the reduction variable is always the last (second)
2960 if (dump_enabled_p ())
2961 report_vect_op (MSG_NOTE
, def_stmt
,
2962 "detected reduction: need to swap operands: ");
2964 swap_ssa_operands (def_stmt
, gimple_assign_rhs1_ptr (def_stmt
),
2965 gimple_assign_rhs2_ptr (def_stmt
));
2967 if (CONSTANT_CLASS_P (gimple_assign_rhs1 (def_stmt
)))
2968 LOOP_VINFO_OPERANDS_SWAPPED (loop_info
) = true;
2972 if (dump_enabled_p ())
2973 report_vect_op (MSG_NOTE
, def_stmt
, "detected reduction: ");
2979 /* Try to find SLP reduction chain. */
2980 if (check_reduction
&& code
!= COND_EXPR
2981 && vect_is_slp_reduction (loop_info
, phi
, def_stmt
))
2983 if (dump_enabled_p ())
2984 report_vect_op (MSG_NOTE
, def_stmt
,
2985 "reduction: detected reduction chain: ");
2990 if (dump_enabled_p ())
2991 report_vect_op (MSG_MISSED_OPTIMIZATION
, def_stmt
,
2992 "reduction: unknown pattern: ");
2997 /* Wrapper around vect_is_simple_reduction_1, which will modify code
2998 in-place if it enables detection of more reductions. Arguments
3002 vect_force_simple_reduction (loop_vec_info loop_info
, gimple
*phi
,
3003 bool check_reduction
, bool *double_reduc
,
3004 bool need_wrapping_integral_overflow
)
3006 enum vect_reduction_type v_reduc_type
;
3007 return vect_is_simple_reduction (loop_info
, phi
, check_reduction
,
3009 need_wrapping_integral_overflow
,
3013 /* Calculate cost of peeling the loop PEEL_ITERS_PROLOGUE times. */
3015 vect_get_known_peeling_cost (loop_vec_info loop_vinfo
, int peel_iters_prologue
,
3016 int *peel_iters_epilogue
,
3017 stmt_vector_for_cost
*scalar_cost_vec
,
3018 stmt_vector_for_cost
*prologue_cost_vec
,
3019 stmt_vector_for_cost
*epilogue_cost_vec
)
3022 int vf
= LOOP_VINFO_VECT_FACTOR (loop_vinfo
);
3024 if (!LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo
))
3026 *peel_iters_epilogue
= vf
/2;
3027 if (dump_enabled_p ())
3028 dump_printf_loc (MSG_NOTE
, vect_location
,
3029 "cost model: epilogue peel iters set to vf/2 "
3030 "because loop iterations are unknown .\n");
3032 /* If peeled iterations are known but number of scalar loop
3033 iterations are unknown, count a taken branch per peeled loop. */
3034 retval
= record_stmt_cost (prologue_cost_vec
, 1, cond_branch_taken
,
3035 NULL
, 0, vect_prologue
);
3036 retval
= record_stmt_cost (prologue_cost_vec
, 1, cond_branch_taken
,
3037 NULL
, 0, vect_epilogue
);
3041 int niters
= LOOP_VINFO_INT_NITERS (loop_vinfo
);
3042 peel_iters_prologue
= niters
< peel_iters_prologue
?
3043 niters
: peel_iters_prologue
;
3044 *peel_iters_epilogue
= (niters
- peel_iters_prologue
) % vf
;
3045 /* If we need to peel for gaps, but no peeling is required, we have to
3046 peel VF iterations. */
3047 if (LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo
) && !*peel_iters_epilogue
)
3048 *peel_iters_epilogue
= vf
;
3051 stmt_info_for_cost
*si
;
3053 if (peel_iters_prologue
)
3054 FOR_EACH_VEC_ELT (*scalar_cost_vec
, j
, si
)
3055 retval
+= record_stmt_cost (prologue_cost_vec
,
3056 si
->count
* peel_iters_prologue
,
3057 si
->kind
, NULL
, si
->misalign
,
3059 if (*peel_iters_epilogue
)
3060 FOR_EACH_VEC_ELT (*scalar_cost_vec
, j
, si
)
3061 retval
+= record_stmt_cost (epilogue_cost_vec
,
3062 si
->count
* *peel_iters_epilogue
,
3063 si
->kind
, NULL
, si
->misalign
,
3069 /* Function vect_estimate_min_profitable_iters
3071 Return the number of iterations required for the vector version of the
3072 loop to be profitable relative to the cost of the scalar version of the
3076 vect_estimate_min_profitable_iters (loop_vec_info loop_vinfo
,
3077 int *ret_min_profitable_niters
,
3078 int *ret_min_profitable_estimate
)
3080 int min_profitable_iters
;
3081 int min_profitable_estimate
;
3082 int peel_iters_prologue
;
3083 int peel_iters_epilogue
;
3084 unsigned vec_inside_cost
= 0;
3085 int vec_outside_cost
= 0;
3086 unsigned vec_prologue_cost
= 0;
3087 unsigned vec_epilogue_cost
= 0;
3088 int scalar_single_iter_cost
= 0;
3089 int scalar_outside_cost
= 0;
3090 int vf
= LOOP_VINFO_VECT_FACTOR (loop_vinfo
);
3091 int npeel
= LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo
);
3092 void *target_cost_data
= LOOP_VINFO_TARGET_COST_DATA (loop_vinfo
);
3094 /* Cost model disabled. */
3095 if (unlimited_cost_model (LOOP_VINFO_LOOP (loop_vinfo
)))
3097 dump_printf_loc (MSG_NOTE
, vect_location
, "cost model disabled.\n");
3098 *ret_min_profitable_niters
= 0;
3099 *ret_min_profitable_estimate
= 0;
3103 /* Requires loop versioning tests to handle misalignment. */
3104 if (LOOP_REQUIRES_VERSIONING_FOR_ALIGNMENT (loop_vinfo
))
3106 /* FIXME: Make cost depend on complexity of individual check. */
3107 unsigned len
= LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo
).length ();
3108 (void) add_stmt_cost (target_cost_data
, len
, vector_stmt
, NULL
, 0,
3110 dump_printf (MSG_NOTE
,
3111 "cost model: Adding cost of checks for loop "
3112 "versioning to treat misalignment.\n");
3115 /* Requires loop versioning with alias checks. */
3116 if (LOOP_REQUIRES_VERSIONING_FOR_ALIAS (loop_vinfo
))
3118 /* FIXME: Make cost depend on complexity of individual check. */
3119 unsigned len
= LOOP_VINFO_COMP_ALIAS_DDRS (loop_vinfo
).length ();
3120 (void) add_stmt_cost (target_cost_data
, len
, vector_stmt
, NULL
, 0,
3122 dump_printf (MSG_NOTE
,
3123 "cost model: Adding cost of checks for loop "
3124 "versioning aliasing.\n");
3127 if (LOOP_REQUIRES_VERSIONING_FOR_ALIGNMENT (loop_vinfo
)
3128 || LOOP_REQUIRES_VERSIONING_FOR_ALIAS (loop_vinfo
))
3129 (void) add_stmt_cost (target_cost_data
, 1, cond_branch_taken
, NULL
, 0,
3132 /* Count statements in scalar loop. Using this as scalar cost for a single
3135 TODO: Add outer loop support.
3137 TODO: Consider assigning different costs to different scalar
3140 scalar_single_iter_cost
3141 = LOOP_VINFO_SINGLE_SCALAR_ITERATION_COST (loop_vinfo
);
3143 /* Add additional cost for the peeled instructions in prologue and epilogue
3146 FORNOW: If we don't know the value of peel_iters for prologue or epilogue
3147 at compile-time - we assume it's vf/2 (the worst would be vf-1).
3149 TODO: Build an expression that represents peel_iters for prologue and
3150 epilogue to be used in a run-time test. */
3154 peel_iters_prologue
= vf
/2;
3155 dump_printf (MSG_NOTE
, "cost model: "
3156 "prologue peel iters set to vf/2.\n");
3158 /* If peeling for alignment is unknown, loop bound of main loop becomes
3160 peel_iters_epilogue
= vf
/2;
3161 dump_printf (MSG_NOTE
, "cost model: "
3162 "epilogue peel iters set to vf/2 because "
3163 "peeling for alignment is unknown.\n");
3165 /* If peeled iterations are unknown, count a taken branch and a not taken
3166 branch per peeled loop. Even if scalar loop iterations are known,
3167 vector iterations are not known since peeled prologue iterations are
3168 not known. Hence guards remain the same. */
3169 (void) add_stmt_cost (target_cost_data
, 1, cond_branch_taken
,
3170 NULL
, 0, vect_prologue
);
3171 (void) add_stmt_cost (target_cost_data
, 1, cond_branch_not_taken
,
3172 NULL
, 0, vect_prologue
);
3173 (void) add_stmt_cost (target_cost_data
, 1, cond_branch_taken
,
3174 NULL
, 0, vect_epilogue
);
3175 (void) add_stmt_cost (target_cost_data
, 1, cond_branch_not_taken
,
3176 NULL
, 0, vect_epilogue
);
3177 stmt_info_for_cost
*si
;
3179 FOR_EACH_VEC_ELT (LOOP_VINFO_SCALAR_ITERATION_COST (loop_vinfo
), j
, si
)
3181 struct _stmt_vec_info
*stmt_info
3182 = si
->stmt
? vinfo_for_stmt (si
->stmt
) : NULL
;
3183 (void) add_stmt_cost (target_cost_data
,
3184 si
->count
* peel_iters_prologue
,
3185 si
->kind
, stmt_info
, si
->misalign
,
3187 (void) add_stmt_cost (target_cost_data
,
3188 si
->count
* peel_iters_epilogue
,
3189 si
->kind
, stmt_info
, si
->misalign
,
3195 stmt_vector_for_cost prologue_cost_vec
, epilogue_cost_vec
;
3196 stmt_info_for_cost
*si
;
3198 void *data
= LOOP_VINFO_TARGET_COST_DATA (loop_vinfo
);
3200 prologue_cost_vec
.create (2);
3201 epilogue_cost_vec
.create (2);
3202 peel_iters_prologue
= npeel
;
3204 (void) vect_get_known_peeling_cost (loop_vinfo
, peel_iters_prologue
,
3205 &peel_iters_epilogue
,
3206 &LOOP_VINFO_SCALAR_ITERATION_COST
3209 &epilogue_cost_vec
);
3211 FOR_EACH_VEC_ELT (prologue_cost_vec
, j
, si
)
3213 struct _stmt_vec_info
*stmt_info
3214 = si
->stmt
? vinfo_for_stmt (si
->stmt
) : NULL
;
3215 (void) add_stmt_cost (data
, si
->count
, si
->kind
, stmt_info
,
3216 si
->misalign
, vect_prologue
);
3219 FOR_EACH_VEC_ELT (epilogue_cost_vec
, j
, si
)
3221 struct _stmt_vec_info
*stmt_info
3222 = si
->stmt
? vinfo_for_stmt (si
->stmt
) : NULL
;
3223 (void) add_stmt_cost (data
, si
->count
, si
->kind
, stmt_info
,
3224 si
->misalign
, vect_epilogue
);
3227 prologue_cost_vec
.release ();
3228 epilogue_cost_vec
.release ();
3231 /* FORNOW: The scalar outside cost is incremented in one of the
3234 1. The vectorizer checks for alignment and aliasing and generates
3235 a condition that allows dynamic vectorization. A cost model
3236 check is ANDED with the versioning condition. Hence scalar code
3237 path now has the added cost of the versioning check.
3239 if (cost > th & versioning_check)
3242 Hence run-time scalar is incremented by not-taken branch cost.
3244 2. The vectorizer then checks if a prologue is required. If the
3245 cost model check was not done before during versioning, it has to
3246 be done before the prologue check.
3249 prologue = scalar_iters
3254 if (prologue == num_iters)
3257 Hence the run-time scalar cost is incremented by a taken branch,
3258 plus a not-taken branch, plus a taken branch cost.
3260 3. The vectorizer then checks if an epilogue is required. If the
3261 cost model check was not done before during prologue check, it
3262 has to be done with the epilogue check.
3268 if (prologue == num_iters)
3271 if ((cost <= th) | (scalar_iters-prologue-epilogue == 0))
3274 Hence the run-time scalar cost should be incremented by 2 taken
3277 TODO: The back end may reorder the BBS's differently and reverse
3278 conditions/branch directions. Change the estimates below to
3279 something more reasonable. */
3281 /* If the number of iterations is known and we do not do versioning, we can
3282 decide whether to vectorize at compile time. Hence the scalar version
3283 do not carry cost model guard costs. */
3284 if (!LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo
)
3285 || LOOP_REQUIRES_VERSIONING_FOR_ALIGNMENT (loop_vinfo
)
3286 || LOOP_REQUIRES_VERSIONING_FOR_ALIAS (loop_vinfo
))
3288 /* Cost model check occurs at versioning. */
3289 if (LOOP_REQUIRES_VERSIONING_FOR_ALIGNMENT (loop_vinfo
)
3290 || LOOP_REQUIRES_VERSIONING_FOR_ALIAS (loop_vinfo
))
3291 scalar_outside_cost
+= vect_get_stmt_cost (cond_branch_not_taken
);
3294 /* Cost model check occurs at prologue generation. */
3295 if (LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo
) < 0)
3296 scalar_outside_cost
+= 2 * vect_get_stmt_cost (cond_branch_taken
)
3297 + vect_get_stmt_cost (cond_branch_not_taken
);
3298 /* Cost model check occurs at epilogue generation. */
3300 scalar_outside_cost
+= 2 * vect_get_stmt_cost (cond_branch_taken
);
3304 /* Complete the target-specific cost calculations. */
3305 finish_cost (LOOP_VINFO_TARGET_COST_DATA (loop_vinfo
), &vec_prologue_cost
,
3306 &vec_inside_cost
, &vec_epilogue_cost
);
3308 vec_outside_cost
= (int)(vec_prologue_cost
+ vec_epilogue_cost
);
3310 if (dump_enabled_p ())
3312 dump_printf_loc (MSG_NOTE
, vect_location
, "Cost model analysis: \n");
3313 dump_printf (MSG_NOTE
, " Vector inside of loop cost: %d\n",
3315 dump_printf (MSG_NOTE
, " Vector prologue cost: %d\n",
3317 dump_printf (MSG_NOTE
, " Vector epilogue cost: %d\n",
3319 dump_printf (MSG_NOTE
, " Scalar iteration cost: %d\n",
3320 scalar_single_iter_cost
);
3321 dump_printf (MSG_NOTE
, " Scalar outside cost: %d\n",
3322 scalar_outside_cost
);
3323 dump_printf (MSG_NOTE
, " Vector outside cost: %d\n",
3325 dump_printf (MSG_NOTE
, " prologue iterations: %d\n",
3326 peel_iters_prologue
);
3327 dump_printf (MSG_NOTE
, " epilogue iterations: %d\n",
3328 peel_iters_epilogue
);
3331 /* Calculate number of iterations required to make the vector version
3332 profitable, relative to the loop bodies only. The following condition
3334 SIC * niters + SOC > VIC * ((niters-PL_ITERS-EP_ITERS)/VF) + VOC
3336 SIC = scalar iteration cost, VIC = vector iteration cost,
3337 VOC = vector outside cost, VF = vectorization factor,
3338 PL_ITERS = prologue iterations, EP_ITERS= epilogue iterations
3339 SOC = scalar outside cost for run time cost model check. */
3341 if ((scalar_single_iter_cost
* vf
) > (int) vec_inside_cost
)
3343 if (vec_outside_cost
<= 0)
3344 min_profitable_iters
= 1;
3347 min_profitable_iters
= ((vec_outside_cost
- scalar_outside_cost
) * vf
3348 - vec_inside_cost
* peel_iters_prologue
3349 - vec_inside_cost
* peel_iters_epilogue
)
3350 / ((scalar_single_iter_cost
* vf
)
3353 if ((scalar_single_iter_cost
* vf
* min_profitable_iters
)
3354 <= (((int) vec_inside_cost
* min_profitable_iters
)
3355 + (((int) vec_outside_cost
- scalar_outside_cost
) * vf
)))
3356 min_profitable_iters
++;
3359 /* vector version will never be profitable. */
3362 if (LOOP_VINFO_LOOP (loop_vinfo
)->force_vectorize
)
3363 warning_at (vect_location
, OPT_Wopenmp_simd
, "vectorization "
3364 "did not happen for a simd loop");
3366 if (dump_enabled_p ())
3367 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
3368 "cost model: the vector iteration cost = %d "
3369 "divided by the scalar iteration cost = %d "
3370 "is greater or equal to the vectorization factor = %d"
3372 vec_inside_cost
, scalar_single_iter_cost
, vf
);
3373 *ret_min_profitable_niters
= -1;
3374 *ret_min_profitable_estimate
= -1;
3378 dump_printf (MSG_NOTE
,
3379 " Calculated minimum iters for profitability: %d\n",
3380 min_profitable_iters
);
3382 min_profitable_iters
=
3383 min_profitable_iters
< vf
? vf
: min_profitable_iters
;
3385 /* Because the condition we create is:
3386 if (niters <= min_profitable_iters)
3387 then skip the vectorized loop. */
3388 min_profitable_iters
--;
3390 if (dump_enabled_p ())
3391 dump_printf_loc (MSG_NOTE
, vect_location
,
3392 " Runtime profitability threshold = %d\n",
3393 min_profitable_iters
);
3395 *ret_min_profitable_niters
= min_profitable_iters
;
3397 /* Calculate number of iterations required to make the vector version
3398 profitable, relative to the loop bodies only.
3400 Non-vectorized variant is SIC * niters and it must win over vector
3401 variant on the expected loop trip count. The following condition must hold true:
3402 SIC * niters > VIC * ((niters-PL_ITERS-EP_ITERS)/VF) + VOC + SOC */
3404 if (vec_outside_cost
<= 0)
3405 min_profitable_estimate
= 1;
3408 min_profitable_estimate
= ((vec_outside_cost
+ scalar_outside_cost
) * vf
3409 - vec_inside_cost
* peel_iters_prologue
3410 - vec_inside_cost
* peel_iters_epilogue
)
3411 / ((scalar_single_iter_cost
* vf
)
3414 min_profitable_estimate
--;
3415 min_profitable_estimate
= MAX (min_profitable_estimate
, min_profitable_iters
);
3416 if (dump_enabled_p ())
3417 dump_printf_loc (MSG_NOTE
, vect_location
,
3418 " Static estimate profitability threshold = %d\n",
3419 min_profitable_iters
);
3421 *ret_min_profitable_estimate
= min_profitable_estimate
;
3424 /* Writes into SEL a mask for a vec_perm, equivalent to a vec_shr by OFFSET
3425 vector elements (not bits) for a vector of mode MODE. */
3427 calc_vec_perm_mask_for_shift (enum machine_mode mode
, unsigned int offset
,
3430 unsigned int i
, nelt
= GET_MODE_NUNITS (mode
);
3432 for (i
= 0; i
< nelt
; i
++)
3433 sel
[i
] = (i
+ offset
) & (2*nelt
- 1);
3436 /* Checks whether the target supports whole-vector shifts for vectors of mode
3437 MODE. This is the case if _either_ the platform handles vec_shr_optab, _or_
3438 it supports vec_perm_const with masks for all necessary shift amounts. */
3440 have_whole_vector_shift (enum machine_mode mode
)
3442 if (optab_handler (vec_shr_optab
, mode
) != CODE_FOR_nothing
)
3445 if (direct_optab_handler (vec_perm_const_optab
, mode
) == CODE_FOR_nothing
)
3448 unsigned int i
, nelt
= GET_MODE_NUNITS (mode
);
3449 unsigned char *sel
= XALLOCAVEC (unsigned char, nelt
);
3451 for (i
= nelt
/2; i
>= 1; i
/=2)
3453 calc_vec_perm_mask_for_shift (mode
, i
, sel
);
3454 if (!can_vec_perm_p (mode
, false, sel
))
3460 /* Return the reduction operand (with index REDUC_INDEX) of STMT. */
3463 get_reduction_op (gimple
*stmt
, int reduc_index
)
3465 switch (get_gimple_rhs_class (gimple_assign_rhs_code (stmt
)))
3467 case GIMPLE_SINGLE_RHS
:
3468 gcc_assert (TREE_OPERAND_LENGTH (gimple_assign_rhs1 (stmt
))
3470 return TREE_OPERAND (gimple_assign_rhs1 (stmt
), reduc_index
);
3471 case GIMPLE_UNARY_RHS
:
3472 return gimple_assign_rhs1 (stmt
);
3473 case GIMPLE_BINARY_RHS
:
3475 ? gimple_assign_rhs2 (stmt
) : gimple_assign_rhs1 (stmt
));
3476 case GIMPLE_TERNARY_RHS
:
3477 return gimple_op (stmt
, reduc_index
+ 1);
3483 /* TODO: Close dependency between vect_model_*_cost and vectorizable_*
3484 functions. Design better to avoid maintenance issues. */
3486 /* Function vect_model_reduction_cost.
3488 Models cost for a reduction operation, including the vector ops
3489 generated within the strip-mine loop, the initial definition before
3490 the loop, and the epilogue code that must be generated. */
3493 vect_model_reduction_cost (stmt_vec_info stmt_info
, enum tree_code reduc_code
,
3494 int ncopies
, int reduc_index
)
3496 int prologue_cost
= 0, epilogue_cost
= 0;
3497 enum tree_code code
;
3500 gimple
*stmt
, *orig_stmt
;
3503 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_info
);
3504 struct loop
*loop
= NULL
;
3505 void *target_cost_data
;
3509 loop
= LOOP_VINFO_LOOP (loop_vinfo
);
3510 target_cost_data
= LOOP_VINFO_TARGET_COST_DATA (loop_vinfo
);
3513 target_cost_data
= BB_VINFO_TARGET_COST_DATA (STMT_VINFO_BB_VINFO (stmt_info
));
3515 /* Condition reductions generate two reductions in the loop. */
3516 if (STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
) == COND_REDUCTION
)
3519 /* Cost of reduction op inside loop. */
3520 unsigned inside_cost
= add_stmt_cost (target_cost_data
, ncopies
, vector_stmt
,
3521 stmt_info
, 0, vect_body
);
3522 stmt
= STMT_VINFO_STMT (stmt_info
);
3524 reduction_op
= get_reduction_op (stmt
, reduc_index
);
3526 vectype
= get_vectype_for_scalar_type (TREE_TYPE (reduction_op
));
3529 if (dump_enabled_p ())
3531 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
3532 "unsupported data-type ");
3533 dump_generic_expr (MSG_MISSED_OPTIMIZATION
, TDF_SLIM
,
3534 TREE_TYPE (reduction_op
));
3535 dump_printf (MSG_MISSED_OPTIMIZATION
, "\n");
3540 mode
= TYPE_MODE (vectype
);
3541 orig_stmt
= STMT_VINFO_RELATED_STMT (stmt_info
);
3544 orig_stmt
= STMT_VINFO_STMT (stmt_info
);
3546 code
= gimple_assign_rhs_code (orig_stmt
);
3548 /* Add in cost for initial definition.
3549 For cond reduction we have four vectors: initial index, step, initial
3550 result of the data reduction, initial value of the index reduction. */
3551 int prologue_stmts
= STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
)
3552 == COND_REDUCTION
? 4 : 1;
3553 prologue_cost
+= add_stmt_cost (target_cost_data
, prologue_stmts
,
3554 scalar_to_vec
, stmt_info
, 0,
3557 /* Determine cost of epilogue code.
3559 We have a reduction operator that will reduce the vector in one statement.
3560 Also requires scalar extract. */
3562 if (!loop
|| !nested_in_vect_loop_p (loop
, orig_stmt
))
3564 if (reduc_code
!= ERROR_MARK
)
3566 if (STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
) == COND_REDUCTION
)
3568 /* An EQ stmt and an COND_EXPR stmt. */
3569 epilogue_cost
+= add_stmt_cost (target_cost_data
, 2,
3570 vector_stmt
, stmt_info
, 0,
3572 /* Reduction of the max index and a reduction of the found
3574 epilogue_cost
+= add_stmt_cost (target_cost_data
, 2,
3575 vec_to_scalar
, stmt_info
, 0,
3577 /* A broadcast of the max value. */
3578 epilogue_cost
+= add_stmt_cost (target_cost_data
, 1,
3579 scalar_to_vec
, stmt_info
, 0,
3584 epilogue_cost
+= add_stmt_cost (target_cost_data
, 1, vector_stmt
,
3585 stmt_info
, 0, vect_epilogue
);
3586 epilogue_cost
+= add_stmt_cost (target_cost_data
, 1,
3587 vec_to_scalar
, stmt_info
, 0,
3593 int vec_size_in_bits
= tree_to_uhwi (TYPE_SIZE (vectype
));
3595 TYPE_SIZE (TREE_TYPE (gimple_assign_lhs (orig_stmt
)));
3596 int element_bitsize
= tree_to_uhwi (bitsize
);
3597 int nelements
= vec_size_in_bits
/ element_bitsize
;
3599 optab
= optab_for_tree_code (code
, vectype
, optab_default
);
3601 /* We have a whole vector shift available. */
3602 if (VECTOR_MODE_P (mode
)
3603 && optab_handler (optab
, mode
) != CODE_FOR_nothing
3604 && have_whole_vector_shift (mode
))
3606 /* Final reduction via vector shifts and the reduction operator.
3607 Also requires scalar extract. */
3608 epilogue_cost
+= add_stmt_cost (target_cost_data
,
3609 exact_log2 (nelements
) * 2,
3610 vector_stmt
, stmt_info
, 0,
3612 epilogue_cost
+= add_stmt_cost (target_cost_data
, 1,
3613 vec_to_scalar
, stmt_info
, 0,
3617 /* Use extracts and reduction op for final reduction. For N
3618 elements, we have N extracts and N-1 reduction ops. */
3619 epilogue_cost
+= add_stmt_cost (target_cost_data
,
3620 nelements
+ nelements
- 1,
3621 vector_stmt
, stmt_info
, 0,
3626 if (dump_enabled_p ())
3627 dump_printf (MSG_NOTE
,
3628 "vect_model_reduction_cost: inside_cost = %d, "
3629 "prologue_cost = %d, epilogue_cost = %d .\n", inside_cost
,
3630 prologue_cost
, epilogue_cost
);
3636 /* Function vect_model_induction_cost.
3638 Models cost for induction operations. */
3641 vect_model_induction_cost (stmt_vec_info stmt_info
, int ncopies
)
3643 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_info
);
3644 void *target_cost_data
= LOOP_VINFO_TARGET_COST_DATA (loop_vinfo
);
3645 unsigned inside_cost
, prologue_cost
;
3647 /* loop cost for vec_loop. */
3648 inside_cost
= add_stmt_cost (target_cost_data
, ncopies
, vector_stmt
,
3649 stmt_info
, 0, vect_body
);
3651 /* prologue cost for vec_init and vec_step. */
3652 prologue_cost
= add_stmt_cost (target_cost_data
, 2, scalar_to_vec
,
3653 stmt_info
, 0, vect_prologue
);
3655 if (dump_enabled_p ())
3656 dump_printf_loc (MSG_NOTE
, vect_location
,
3657 "vect_model_induction_cost: inside_cost = %d, "
3658 "prologue_cost = %d .\n", inside_cost
, prologue_cost
);
3662 /* Function get_initial_def_for_induction
3665 STMT - a stmt that performs an induction operation in the loop.
3666 IV_PHI - the initial value of the induction variable
3669 Return a vector variable, initialized with the first VF values of
3670 the induction variable. E.g., for an iv with IV_PHI='X' and
3671 evolution S, for a vector of 4 units, we want to return:
3672 [X, X + S, X + 2*S, X + 3*S]. */
3675 get_initial_def_for_induction (gimple
*iv_phi
)
3677 stmt_vec_info stmt_vinfo
= vinfo_for_stmt (iv_phi
);
3678 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_vinfo
);
3679 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
3682 edge pe
= loop_preheader_edge (loop
);
3683 struct loop
*iv_loop
;
3685 tree new_vec
, vec_init
, vec_step
, t
;
3688 gphi
*induction_phi
;
3689 tree induc_def
, vec_def
, vec_dest
;
3690 tree init_expr
, step_expr
;
3691 int vf
= LOOP_VINFO_VECT_FACTOR (loop_vinfo
);
3695 stmt_vec_info phi_info
= vinfo_for_stmt (iv_phi
);
3696 bool nested_in_vect_loop
= false;
3698 imm_use_iterator imm_iter
;
3699 use_operand_p use_p
;
3703 gimple_stmt_iterator si
;
3704 basic_block bb
= gimple_bb (iv_phi
);
3708 /* Is phi in an inner-loop, while vectorizing an enclosing outer-loop? */
3709 if (nested_in_vect_loop_p (loop
, iv_phi
))
3711 nested_in_vect_loop
= true;
3712 iv_loop
= loop
->inner
;
3716 gcc_assert (iv_loop
== (gimple_bb (iv_phi
))->loop_father
);
3718 latch_e
= loop_latch_edge (iv_loop
);
3719 loop_arg
= PHI_ARG_DEF_FROM_EDGE (iv_phi
, latch_e
);
3721 step_expr
= STMT_VINFO_LOOP_PHI_EVOLUTION_PART (phi_info
);
3722 gcc_assert (step_expr
!= NULL_TREE
);
3724 pe
= loop_preheader_edge (iv_loop
);
3725 init_expr
= PHI_ARG_DEF_FROM_EDGE (iv_phi
,
3726 loop_preheader_edge (iv_loop
));
3728 vectype
= get_vectype_for_scalar_type (TREE_TYPE (init_expr
));
3729 resvectype
= get_vectype_for_scalar_type (TREE_TYPE (PHI_RESULT (iv_phi
)));
3730 gcc_assert (vectype
);
3731 nunits
= TYPE_VECTOR_SUBPARTS (vectype
);
3732 ncopies
= vf
/ nunits
;
3734 gcc_assert (phi_info
);
3735 gcc_assert (ncopies
>= 1);
3737 /* Convert the step to the desired type. */
3739 step_expr
= gimple_convert (&stmts
, TREE_TYPE (vectype
), step_expr
);
3742 new_bb
= gsi_insert_seq_on_edge_immediate (pe
, stmts
);
3743 gcc_assert (!new_bb
);
3746 /* Find the first insertion point in the BB. */
3747 si
= gsi_after_labels (bb
);
3749 /* Create the vector that holds the initial_value of the induction. */
3750 if (nested_in_vect_loop
)
3752 /* iv_loop is nested in the loop to be vectorized. init_expr had already
3753 been created during vectorization of previous stmts. We obtain it
3754 from the STMT_VINFO_VEC_STMT of the defining stmt. */
3755 vec_init
= vect_get_vec_def_for_operand (init_expr
, iv_phi
);
3756 /* If the initial value is not of proper type, convert it. */
3757 if (!useless_type_conversion_p (vectype
, TREE_TYPE (vec_init
)))
3760 = gimple_build_assign (vect_get_new_ssa_name (vectype
,
3764 build1 (VIEW_CONVERT_EXPR
, vectype
,
3766 vec_init
= gimple_assign_lhs (new_stmt
);
3767 new_bb
= gsi_insert_on_edge_immediate (loop_preheader_edge (iv_loop
),
3769 gcc_assert (!new_bb
);
3770 set_vinfo_for_stmt (new_stmt
,
3771 new_stmt_vec_info (new_stmt
, loop_vinfo
));
3776 vec
<constructor_elt
, va_gc
> *v
;
3778 /* iv_loop is the loop to be vectorized. Create:
3779 vec_init = [X, X+S, X+2*S, X+3*S] (S = step_expr, X = init_expr) */
3781 new_name
= gimple_convert (&stmts
, TREE_TYPE (vectype
), init_expr
);
3783 vec_alloc (v
, nunits
);
3784 bool constant_p
= is_gimple_min_invariant (new_name
);
3785 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, new_name
);
3786 for (i
= 1; i
< nunits
; i
++)
3788 /* Create: new_name_i = new_name + step_expr */
3789 new_name
= gimple_build (&stmts
, PLUS_EXPR
, TREE_TYPE (new_name
),
3790 new_name
, step_expr
);
3791 if (!is_gimple_min_invariant (new_name
))
3793 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, new_name
);
3797 new_bb
= gsi_insert_seq_on_edge_immediate (pe
, stmts
);
3798 gcc_assert (!new_bb
);
3801 /* Create a vector from [new_name_0, new_name_1, ..., new_name_nunits-1] */
3803 new_vec
= build_vector_from_ctor (vectype
, v
);
3805 new_vec
= build_constructor (vectype
, v
);
3806 vec_init
= vect_init_vector (iv_phi
, new_vec
, vectype
, NULL
);
3810 /* Create the vector that holds the step of the induction. */
3811 if (nested_in_vect_loop
)
3812 /* iv_loop is nested in the loop to be vectorized. Generate:
3813 vec_step = [S, S, S, S] */
3814 new_name
= step_expr
;
3817 /* iv_loop is the loop to be vectorized. Generate:
3818 vec_step = [VF*S, VF*S, VF*S, VF*S] */
3819 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (step_expr
)))
3821 expr
= build_int_cst (integer_type_node
, vf
);
3822 expr
= fold_convert (TREE_TYPE (step_expr
), expr
);
3825 expr
= build_int_cst (TREE_TYPE (step_expr
), vf
);
3826 new_name
= fold_build2 (MULT_EXPR
, TREE_TYPE (step_expr
),
3828 if (TREE_CODE (step_expr
) == SSA_NAME
)
3829 new_name
= vect_init_vector (iv_phi
, new_name
,
3830 TREE_TYPE (step_expr
), NULL
);
3833 t
= unshare_expr (new_name
);
3834 gcc_assert (CONSTANT_CLASS_P (new_name
)
3835 || TREE_CODE (new_name
) == SSA_NAME
);
3836 stepvectype
= get_vectype_for_scalar_type (TREE_TYPE (new_name
));
3837 gcc_assert (stepvectype
);
3838 new_vec
= build_vector_from_val (stepvectype
, t
);
3839 vec_step
= vect_init_vector (iv_phi
, new_vec
, stepvectype
, NULL
);
3842 /* Create the following def-use cycle:
3847 vec_iv = PHI <vec_init, vec_loop>
3851 vec_loop = vec_iv + vec_step; */
3853 /* Create the induction-phi that defines the induction-operand. */
3854 vec_dest
= vect_get_new_vect_var (vectype
, vect_simple_var
, "vec_iv_");
3855 induction_phi
= create_phi_node (vec_dest
, iv_loop
->header
);
3856 set_vinfo_for_stmt (induction_phi
,
3857 new_stmt_vec_info (induction_phi
, loop_vinfo
));
3858 induc_def
= PHI_RESULT (induction_phi
);
3860 /* Create the iv update inside the loop */
3861 new_stmt
= gimple_build_assign (vec_dest
, PLUS_EXPR
, induc_def
, vec_step
);
3862 vec_def
= make_ssa_name (vec_dest
, new_stmt
);
3863 gimple_assign_set_lhs (new_stmt
, vec_def
);
3864 gsi_insert_before (&si
, new_stmt
, GSI_SAME_STMT
);
3865 set_vinfo_for_stmt (new_stmt
, new_stmt_vec_info (new_stmt
, loop_vinfo
));
3867 /* Set the arguments of the phi node: */
3868 add_phi_arg (induction_phi
, vec_init
, pe
, UNKNOWN_LOCATION
);
3869 add_phi_arg (induction_phi
, vec_def
, loop_latch_edge (iv_loop
),
3873 /* In case that vectorization factor (VF) is bigger than the number
3874 of elements that we can fit in a vectype (nunits), we have to generate
3875 more than one vector stmt - i.e - we need to "unroll" the
3876 vector stmt by a factor VF/nunits. For more details see documentation
3877 in vectorizable_operation. */
3881 stmt_vec_info prev_stmt_vinfo
;
3882 /* FORNOW. This restriction should be relaxed. */
3883 gcc_assert (!nested_in_vect_loop
);
3885 /* Create the vector that holds the step of the induction. */
3886 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (step_expr
)))
3888 expr
= build_int_cst (integer_type_node
, nunits
);
3889 expr
= fold_convert (TREE_TYPE (step_expr
), expr
);
3892 expr
= build_int_cst (TREE_TYPE (step_expr
), nunits
);
3893 new_name
= fold_build2 (MULT_EXPR
, TREE_TYPE (step_expr
),
3895 if (TREE_CODE (step_expr
) == SSA_NAME
)
3896 new_name
= vect_init_vector (iv_phi
, new_name
,
3897 TREE_TYPE (step_expr
), NULL
);
3898 t
= unshare_expr (new_name
);
3899 gcc_assert (CONSTANT_CLASS_P (new_name
)
3900 || TREE_CODE (new_name
) == SSA_NAME
);
3901 new_vec
= build_vector_from_val (stepvectype
, t
);
3902 vec_step
= vect_init_vector (iv_phi
, new_vec
, stepvectype
, NULL
);
3904 vec_def
= induc_def
;
3905 prev_stmt_vinfo
= vinfo_for_stmt (induction_phi
);
3906 for (i
= 1; i
< ncopies
; i
++)
3908 /* vec_i = vec_prev + vec_step */
3909 new_stmt
= gimple_build_assign (vec_dest
, PLUS_EXPR
,
3911 vec_def
= make_ssa_name (vec_dest
, new_stmt
);
3912 gimple_assign_set_lhs (new_stmt
, vec_def
);
3914 gsi_insert_before (&si
, new_stmt
, GSI_SAME_STMT
);
3915 if (!useless_type_conversion_p (resvectype
, vectype
))
3918 = gimple_build_assign
3919 (vect_get_new_vect_var (resvectype
, vect_simple_var
,
3922 build1 (VIEW_CONVERT_EXPR
, resvectype
,
3923 gimple_assign_lhs (new_stmt
)));
3924 gimple_assign_set_lhs (new_stmt
,
3926 (gimple_assign_lhs (new_stmt
), new_stmt
));
3927 gsi_insert_before (&si
, new_stmt
, GSI_SAME_STMT
);
3929 set_vinfo_for_stmt (new_stmt
,
3930 new_stmt_vec_info (new_stmt
, loop_vinfo
));
3931 STMT_VINFO_RELATED_STMT (prev_stmt_vinfo
) = new_stmt
;
3932 prev_stmt_vinfo
= vinfo_for_stmt (new_stmt
);
3936 if (nested_in_vect_loop
)
3938 /* Find the loop-closed exit-phi of the induction, and record
3939 the final vector of induction results: */
3941 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, loop_arg
)
3943 gimple
*use_stmt
= USE_STMT (use_p
);
3944 if (is_gimple_debug (use_stmt
))
3947 if (!flow_bb_inside_loop_p (iv_loop
, gimple_bb (use_stmt
)))
3949 exit_phi
= use_stmt
;
3955 stmt_vec_info stmt_vinfo
= vinfo_for_stmt (exit_phi
);
3956 /* FORNOW. Currently not supporting the case that an inner-loop induction
3957 is not used in the outer-loop (i.e. only outside the outer-loop). */
3958 gcc_assert (STMT_VINFO_RELEVANT_P (stmt_vinfo
)
3959 && !STMT_VINFO_LIVE_P (stmt_vinfo
));
3961 STMT_VINFO_VEC_STMT (stmt_vinfo
) = new_stmt
;
3962 if (dump_enabled_p ())
3964 dump_printf_loc (MSG_NOTE
, vect_location
,
3965 "vector of inductions after inner-loop:");
3966 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
, new_stmt
, 0);
3967 dump_printf (MSG_NOTE
, "\n");
3973 if (dump_enabled_p ())
3975 dump_printf_loc (MSG_NOTE
, vect_location
,
3976 "transform induction: created def-use cycle: ");
3977 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
, induction_phi
, 0);
3978 dump_printf (MSG_NOTE
, "\n");
3979 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
,
3980 SSA_NAME_DEF_STMT (vec_def
), 0);
3981 dump_printf (MSG_NOTE
, "\n");
3984 STMT_VINFO_VEC_STMT (phi_info
) = induction_phi
;
3985 if (!useless_type_conversion_p (resvectype
, vectype
))
3987 new_stmt
= gimple_build_assign (vect_get_new_vect_var (resvectype
,
3991 build1 (VIEW_CONVERT_EXPR
, resvectype
,
3993 induc_def
= make_ssa_name (gimple_assign_lhs (new_stmt
), new_stmt
);
3994 gimple_assign_set_lhs (new_stmt
, induc_def
);
3995 si
= gsi_after_labels (bb
);
3996 gsi_insert_before (&si
, new_stmt
, GSI_SAME_STMT
);
3997 set_vinfo_for_stmt (new_stmt
,
3998 new_stmt_vec_info (new_stmt
, loop_vinfo
));
3999 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (new_stmt
))
4000 = STMT_VINFO_RELATED_STMT (vinfo_for_stmt (induction_phi
));
4007 /* Function get_initial_def_for_reduction
4010 STMT - a stmt that performs a reduction operation in the loop.
4011 INIT_VAL - the initial value of the reduction variable
4014 ADJUSTMENT_DEF - a tree that holds a value to be added to the final result
4015 of the reduction (used for adjusting the epilog - see below).
4016 Return a vector variable, initialized according to the operation that STMT
4017 performs. This vector will be used as the initial value of the
4018 vector of partial results.
4020 Option1 (adjust in epilog): Initialize the vector as follows:
4021 add/bit or/xor: [0,0,...,0,0]
4022 mult/bit and: [1,1,...,1,1]
4023 min/max/cond_expr: [init_val,init_val,..,init_val,init_val]
4024 and when necessary (e.g. add/mult case) let the caller know
4025 that it needs to adjust the result by init_val.
4027 Option2: Initialize the vector as follows:
4028 add/bit or/xor: [init_val,0,0,...,0]
4029 mult/bit and: [init_val,1,1,...,1]
4030 min/max/cond_expr: [init_val,init_val,...,init_val]
4031 and no adjustments are needed.
4033 For example, for the following code:
4039 STMT is 's = s + a[i]', and the reduction variable is 's'.
4040 For a vector of 4 units, we want to return either [0,0,0,init_val],
4041 or [0,0,0,0] and let the caller know that it needs to adjust
4042 the result at the end by 'init_val'.
4044 FORNOW, we are using the 'adjust in epilog' scheme, because this way the
4045 initialization vector is simpler (same element in all entries), if
4046 ADJUSTMENT_DEF is not NULL, and Option2 otherwise.
4048 A cost model should help decide between these two schemes. */
4051 get_initial_def_for_reduction (gimple
*stmt
, tree init_val
,
4052 tree
*adjustment_def
)
4054 stmt_vec_info stmt_vinfo
= vinfo_for_stmt (stmt
);
4055 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_vinfo
);
4056 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
4057 tree scalar_type
= TREE_TYPE (init_val
);
4058 tree vectype
= get_vectype_for_scalar_type (scalar_type
);
4060 enum tree_code code
= gimple_assign_rhs_code (stmt
);
4065 bool nested_in_vect_loop
= false;
4067 REAL_VALUE_TYPE real_init_val
= dconst0
;
4068 int int_init_val
= 0;
4069 gimple
*def_stmt
= NULL
;
4071 gcc_assert (vectype
);
4072 nunits
= TYPE_VECTOR_SUBPARTS (vectype
);
4074 gcc_assert (POINTER_TYPE_P (scalar_type
) || INTEGRAL_TYPE_P (scalar_type
)
4075 || SCALAR_FLOAT_TYPE_P (scalar_type
));
4077 if (nested_in_vect_loop_p (loop
, stmt
))
4078 nested_in_vect_loop
= true;
4080 gcc_assert (loop
== (gimple_bb (stmt
))->loop_father
);
4082 /* In case of double reduction we only create a vector variable to be put
4083 in the reduction phi node. The actual statement creation is done in
4084 vect_create_epilog_for_reduction. */
4085 if (adjustment_def
&& nested_in_vect_loop
4086 && TREE_CODE (init_val
) == SSA_NAME
4087 && (def_stmt
= SSA_NAME_DEF_STMT (init_val
))
4088 && gimple_code (def_stmt
) == GIMPLE_PHI
4089 && flow_bb_inside_loop_p (loop
, gimple_bb (def_stmt
))
4090 && vinfo_for_stmt (def_stmt
)
4091 && STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def_stmt
))
4092 == vect_double_reduction_def
)
4094 *adjustment_def
= NULL
;
4095 return vect_create_destination_var (init_val
, vectype
);
4098 if (TREE_CONSTANT (init_val
))
4100 if (SCALAR_FLOAT_TYPE_P (scalar_type
))
4101 init_value
= build_real (scalar_type
, TREE_REAL_CST (init_val
));
4103 init_value
= build_int_cst (scalar_type
, TREE_INT_CST_LOW (init_val
));
4106 init_value
= init_val
;
4110 case WIDEN_SUM_EXPR
:
4119 /* ADJUSMENT_DEF is NULL when called from
4120 vect_create_epilog_for_reduction to vectorize double reduction. */
4123 if (nested_in_vect_loop
)
4124 *adjustment_def
= vect_get_vec_def_for_operand (init_val
, stmt
);
4126 *adjustment_def
= init_val
;
4129 if (code
== MULT_EXPR
)
4131 real_init_val
= dconst1
;
4135 if (code
== BIT_AND_EXPR
)
4138 if (SCALAR_FLOAT_TYPE_P (scalar_type
))
4139 def_for_init
= build_real (scalar_type
, real_init_val
);
4141 def_for_init
= build_int_cst (scalar_type
, int_init_val
);
4143 /* Create a vector of '0' or '1' except the first element. */
4144 elts
= XALLOCAVEC (tree
, nunits
);
4145 for (i
= nunits
- 2; i
>= 0; --i
)
4146 elts
[i
+ 1] = def_for_init
;
4148 /* Option1: the first element is '0' or '1' as well. */
4151 elts
[0] = def_for_init
;
4152 init_def
= build_vector (vectype
, elts
);
4156 /* Option2: the first element is INIT_VAL. */
4158 if (TREE_CONSTANT (init_val
))
4159 init_def
= build_vector (vectype
, elts
);
4162 vec
<constructor_elt
, va_gc
> *v
;
4163 vec_alloc (v
, nunits
);
4164 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, init_val
);
4165 for (i
= 1; i
< nunits
; ++i
)
4166 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, elts
[i
]);
4167 init_def
= build_constructor (vectype
, v
);
4177 *adjustment_def
= NULL_TREE
;
4178 if (STMT_VINFO_VEC_REDUCTION_TYPE (stmt_vinfo
) != COND_REDUCTION
)
4180 init_def
= vect_get_vec_def_for_operand (init_val
, stmt
);
4184 init_def
= build_vector_from_val (vectype
, init_value
);
4194 /* Function vect_create_epilog_for_reduction
4196 Create code at the loop-epilog to finalize the result of a reduction
4199 VECT_DEFS is list of vector of partial results, i.e., the lhs's of vector
4200 reduction statements.
4201 STMT is the scalar reduction stmt that is being vectorized.
4202 NCOPIES is > 1 in case the vectorization factor (VF) is bigger than the
4203 number of elements that we can fit in a vectype (nunits). In this case
4204 we have to generate more than one vector stmt - i.e - we need to "unroll"
4205 the vector stmt by a factor VF/nunits. For more details see documentation
4206 in vectorizable_operation.
4207 REDUC_CODE is the tree-code for the epilog reduction.
4208 REDUCTION_PHIS is a list of the phi-nodes that carry the reduction
4210 REDUC_INDEX is the index of the operand in the right hand side of the
4211 statement that is defined by REDUCTION_PHI.
4212 DOUBLE_REDUC is TRUE if double reduction phi nodes should be handled.
4213 SLP_NODE is an SLP node containing a group of reduction statements. The
4214 first one in this group is STMT.
4215 INDUCTION_INDEX is the index of the loop for condition reductions.
4216 Otherwise it is undefined.
4219 1. Creates the reduction def-use cycles: sets the arguments for
4221 The loop-entry argument is the vectorized initial-value of the reduction.
4222 The loop-latch argument is taken from VECT_DEFS - the vector of partial
4224 2. "Reduces" each vector of partial results VECT_DEFS into a single result,
4225 by applying the operation specified by REDUC_CODE if available, or by
4226 other means (whole-vector shifts or a scalar loop).
4227 The function also creates a new phi node at the loop exit to preserve
4228 loop-closed form, as illustrated below.
4230 The flow at the entry to this function:
4233 vec_def = phi <null, null> # REDUCTION_PHI
4234 VECT_DEF = vector_stmt # vectorized form of STMT
4235 s_loop = scalar_stmt # (scalar) STMT
4237 s_out0 = phi <s_loop> # (scalar) EXIT_PHI
4241 The above is transformed by this function into:
4244 vec_def = phi <vec_init, VECT_DEF> # REDUCTION_PHI
4245 VECT_DEF = vector_stmt # vectorized form of STMT
4246 s_loop = scalar_stmt # (scalar) STMT
4248 s_out0 = phi <s_loop> # (scalar) EXIT_PHI
4249 v_out1 = phi <VECT_DEF> # NEW_EXIT_PHI
4250 v_out2 = reduce <v_out1>
4251 s_out3 = extract_field <v_out2, 0>
4252 s_out4 = adjust_result <s_out3>
4258 vect_create_epilog_for_reduction (vec
<tree
> vect_defs
, gimple
*stmt
,
4259 int ncopies
, enum tree_code reduc_code
,
4260 vec
<gimple
*> reduction_phis
,
4261 int reduc_index
, bool double_reduc
,
4262 slp_tree slp_node
, tree induction_index
)
4264 stmt_vec_info stmt_info
= vinfo_for_stmt (stmt
);
4265 stmt_vec_info prev_phi_info
;
4268 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_info
);
4269 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
), *outer_loop
= NULL
;
4270 basic_block exit_bb
;
4273 gimple
*new_phi
= NULL
, *phi
;
4274 gimple_stmt_iterator exit_gsi
;
4276 tree new_temp
= NULL_TREE
, new_dest
, new_name
, new_scalar_dest
;
4277 gimple
*epilog_stmt
= NULL
;
4278 enum tree_code code
= gimple_assign_rhs_code (stmt
);
4281 tree adjustment_def
= NULL
;
4282 tree vec_initial_def
= NULL
;
4283 tree reduction_op
, expr
, def
, initial_def
= NULL
;
4284 tree orig_name
, scalar_result
;
4285 imm_use_iterator imm_iter
, phi_imm_iter
;
4286 use_operand_p use_p
, phi_use_p
;
4287 gimple
*use_stmt
, *orig_stmt
, *reduction_phi
= NULL
;
4288 bool nested_in_vect_loop
= false;
4289 auto_vec
<gimple
*> new_phis
;
4290 auto_vec
<gimple
*> inner_phis
;
4291 enum vect_def_type dt
= vect_unknown_def_type
;
4293 auto_vec
<tree
> scalar_results
;
4294 unsigned int group_size
= 1, k
, ratio
;
4295 auto_vec
<tree
> vec_initial_defs
;
4296 auto_vec
<gimple
*> phis
;
4297 bool slp_reduc
= false;
4298 tree new_phi_result
;
4299 gimple
*inner_phi
= NULL
;
4302 group_size
= SLP_TREE_SCALAR_STMTS (slp_node
).length ();
4304 if (nested_in_vect_loop_p (loop
, stmt
))
4308 nested_in_vect_loop
= true;
4309 gcc_assert (!slp_node
);
4312 reduction_op
= get_reduction_op (stmt
, reduc_index
);
4314 vectype
= get_vectype_for_scalar_type (TREE_TYPE (reduction_op
));
4315 gcc_assert (vectype
);
4316 mode
= TYPE_MODE (vectype
);
4318 /* 1. Create the reduction def-use cycle:
4319 Set the arguments of REDUCTION_PHIS, i.e., transform
4322 vec_def = phi <null, null> # REDUCTION_PHI
4323 VECT_DEF = vector_stmt # vectorized form of STMT
4329 vec_def = phi <vec_init, VECT_DEF> # REDUCTION_PHI
4330 VECT_DEF = vector_stmt # vectorized form of STMT
4333 (in case of SLP, do it for all the phis). */
4335 /* Get the loop-entry arguments. */
4337 vect_get_vec_defs (reduction_op
, NULL_TREE
, stmt
, &vec_initial_defs
,
4338 NULL
, slp_node
, reduc_index
);
4341 /* Get at the scalar def before the loop, that defines the initial value
4342 of the reduction variable. */
4343 gimple
*def_stmt
= SSA_NAME_DEF_STMT (reduction_op
);
4344 initial_def
= PHI_ARG_DEF_FROM_EDGE (def_stmt
,
4345 loop_preheader_edge (loop
));
4346 vec_initial_defs
.create (1);
4347 vec_initial_def
= get_initial_def_for_reduction (stmt
, initial_def
,
4349 vec_initial_defs
.quick_push (vec_initial_def
);
4352 /* Set phi nodes arguments. */
4353 FOR_EACH_VEC_ELT (reduction_phis
, i
, phi
)
4355 tree vec_init_def
, def
;
4357 vec_init_def
= force_gimple_operand (vec_initial_defs
[i
], &stmts
,
4359 gsi_insert_seq_on_edge_immediate (loop_preheader_edge (loop
), stmts
);
4361 for (j
= 0; j
< ncopies
; j
++)
4363 /* Set the loop-entry arg of the reduction-phi. */
4365 if (STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
)
4366 == INTEGER_INDUC_COND_REDUCTION
)
4368 /* Initialise the reduction phi to zero. This prevents initial
4369 values of non-zero interferring with the reduction op. */
4370 gcc_assert (ncopies
== 1);
4371 gcc_assert (i
== 0);
4373 tree vec_init_def_type
= TREE_TYPE (vec_init_def
);
4374 tree zero_vec
= build_zero_cst (vec_init_def_type
);
4376 add_phi_arg (as_a
<gphi
*> (phi
), zero_vec
,
4377 loop_preheader_edge (loop
), UNKNOWN_LOCATION
);
4380 add_phi_arg (as_a
<gphi
*> (phi
), vec_init_def
,
4381 loop_preheader_edge (loop
), UNKNOWN_LOCATION
);
4383 /* Set the loop-latch arg for the reduction-phi. */
4385 def
= vect_get_vec_def_for_stmt_copy (vect_unknown_def_type
, def
);
4387 add_phi_arg (as_a
<gphi
*> (phi
), def
, loop_latch_edge (loop
),
4390 if (dump_enabled_p ())
4392 dump_printf_loc (MSG_NOTE
, vect_location
,
4393 "transform reduction: created def-use cycle: ");
4394 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
, phi
, 0);
4395 dump_printf (MSG_NOTE
, "\n");
4396 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
, SSA_NAME_DEF_STMT (def
), 0);
4397 dump_printf (MSG_NOTE
, "\n");
4400 phi
= STMT_VINFO_RELATED_STMT (vinfo_for_stmt (phi
));
4404 /* 2. Create epilog code.
4405 The reduction epilog code operates across the elements of the vector
4406 of partial results computed by the vectorized loop.
4407 The reduction epilog code consists of:
4409 step 1: compute the scalar result in a vector (v_out2)
4410 step 2: extract the scalar result (s_out3) from the vector (v_out2)
4411 step 3: adjust the scalar result (s_out3) if needed.
4413 Step 1 can be accomplished using one the following three schemes:
4414 (scheme 1) using reduc_code, if available.
4415 (scheme 2) using whole-vector shifts, if available.
4416 (scheme 3) using a scalar loop. In this case steps 1+2 above are
4419 The overall epilog code looks like this:
4421 s_out0 = phi <s_loop> # original EXIT_PHI
4422 v_out1 = phi <VECT_DEF> # NEW_EXIT_PHI
4423 v_out2 = reduce <v_out1> # step 1
4424 s_out3 = extract_field <v_out2, 0> # step 2
4425 s_out4 = adjust_result <s_out3> # step 3
4427 (step 3 is optional, and steps 1 and 2 may be combined).
4428 Lastly, the uses of s_out0 are replaced by s_out4. */
4431 /* 2.1 Create new loop-exit-phis to preserve loop-closed form:
4432 v_out1 = phi <VECT_DEF>
4433 Store them in NEW_PHIS. */
4435 exit_bb
= single_exit (loop
)->dest
;
4436 prev_phi_info
= NULL
;
4437 new_phis
.create (vect_defs
.length ());
4438 FOR_EACH_VEC_ELT (vect_defs
, i
, def
)
4440 for (j
= 0; j
< ncopies
; j
++)
4442 tree new_def
= copy_ssa_name (def
);
4443 phi
= create_phi_node (new_def
, exit_bb
);
4444 set_vinfo_for_stmt (phi
, new_stmt_vec_info (phi
, loop_vinfo
));
4446 new_phis
.quick_push (phi
);
4449 def
= vect_get_vec_def_for_stmt_copy (dt
, def
);
4450 STMT_VINFO_RELATED_STMT (prev_phi_info
) = phi
;
4453 SET_PHI_ARG_DEF (phi
, single_exit (loop
)->dest_idx
, def
);
4454 prev_phi_info
= vinfo_for_stmt (phi
);
4458 /* The epilogue is created for the outer-loop, i.e., for the loop being
4459 vectorized. Create exit phis for the outer loop. */
4463 exit_bb
= single_exit (loop
)->dest
;
4464 inner_phis
.create (vect_defs
.length ());
4465 FOR_EACH_VEC_ELT (new_phis
, i
, phi
)
4467 tree new_result
= copy_ssa_name (PHI_RESULT (phi
));
4468 gphi
*outer_phi
= create_phi_node (new_result
, exit_bb
);
4469 SET_PHI_ARG_DEF (outer_phi
, single_exit (loop
)->dest_idx
,
4471 set_vinfo_for_stmt (outer_phi
, new_stmt_vec_info (outer_phi
,
4473 inner_phis
.quick_push (phi
);
4474 new_phis
[i
] = outer_phi
;
4475 prev_phi_info
= vinfo_for_stmt (outer_phi
);
4476 while (STMT_VINFO_RELATED_STMT (vinfo_for_stmt (phi
)))
4478 phi
= STMT_VINFO_RELATED_STMT (vinfo_for_stmt (phi
));
4479 new_result
= copy_ssa_name (PHI_RESULT (phi
));
4480 outer_phi
= create_phi_node (new_result
, exit_bb
);
4481 SET_PHI_ARG_DEF (outer_phi
, single_exit (loop
)->dest_idx
,
4483 set_vinfo_for_stmt (outer_phi
, new_stmt_vec_info (outer_phi
,
4485 STMT_VINFO_RELATED_STMT (prev_phi_info
) = outer_phi
;
4486 prev_phi_info
= vinfo_for_stmt (outer_phi
);
4491 exit_gsi
= gsi_after_labels (exit_bb
);
4493 /* 2.2 Get the relevant tree-code to use in the epilog for schemes 2,3
4494 (i.e. when reduc_code is not available) and in the final adjustment
4495 code (if needed). Also get the original scalar reduction variable as
4496 defined in the loop. In case STMT is a "pattern-stmt" (i.e. - it
4497 represents a reduction pattern), the tree-code and scalar-def are
4498 taken from the original stmt that the pattern-stmt (STMT) replaces.
4499 Otherwise (it is a regular reduction) - the tree-code and scalar-def
4500 are taken from STMT. */
4502 orig_stmt
= STMT_VINFO_RELATED_STMT (stmt_info
);
4505 /* Regular reduction */
4510 /* Reduction pattern */
4511 stmt_vec_info stmt_vinfo
= vinfo_for_stmt (orig_stmt
);
4512 gcc_assert (STMT_VINFO_IN_PATTERN_P (stmt_vinfo
));
4513 gcc_assert (STMT_VINFO_RELATED_STMT (stmt_vinfo
) == stmt
);
4516 code
= gimple_assign_rhs_code (orig_stmt
);
4517 /* For MINUS_EXPR the initial vector is [init_val,0,...,0], therefore,
4518 partial results are added and not subtracted. */
4519 if (code
== MINUS_EXPR
)
4522 scalar_dest
= gimple_assign_lhs (orig_stmt
);
4523 scalar_type
= TREE_TYPE (scalar_dest
);
4524 scalar_results
.create (group_size
);
4525 new_scalar_dest
= vect_create_destination_var (scalar_dest
, NULL
);
4526 bitsize
= TYPE_SIZE (scalar_type
);
4528 /* In case this is a reduction in an inner-loop while vectorizing an outer
4529 loop - we don't need to extract a single scalar result at the end of the
4530 inner-loop (unless it is double reduction, i.e., the use of reduction is
4531 outside the outer-loop). The final vector of partial results will be used
4532 in the vectorized outer-loop, or reduced to a scalar result at the end of
4534 if (nested_in_vect_loop
&& !double_reduc
)
4535 goto vect_finalize_reduction
;
4537 /* SLP reduction without reduction chain, e.g.,
4541 b2 = operation (b1) */
4542 slp_reduc
= (slp_node
&& !GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt
)));
4544 /* In case of reduction chain, e.g.,
4547 a3 = operation (a2),
4549 we may end up with more than one vector result. Here we reduce them to
4551 if (GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt
)))
4553 tree first_vect
= PHI_RESULT (new_phis
[0]);
4555 gassign
*new_vec_stmt
= NULL
;
4557 vec_dest
= vect_create_destination_var (scalar_dest
, vectype
);
4558 for (k
= 1; k
< new_phis
.length (); k
++)
4560 gimple
*next_phi
= new_phis
[k
];
4561 tree second_vect
= PHI_RESULT (next_phi
);
4563 tmp
= build2 (code
, vectype
, first_vect
, second_vect
);
4564 new_vec_stmt
= gimple_build_assign (vec_dest
, tmp
);
4565 first_vect
= make_ssa_name (vec_dest
, new_vec_stmt
);
4566 gimple_assign_set_lhs (new_vec_stmt
, first_vect
);
4567 gsi_insert_before (&exit_gsi
, new_vec_stmt
, GSI_SAME_STMT
);
4570 new_phi_result
= first_vect
;
4573 new_phis
.truncate (0);
4574 new_phis
.safe_push (new_vec_stmt
);
4578 new_phi_result
= PHI_RESULT (new_phis
[0]);
4580 if (STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
) == COND_REDUCTION
)
4582 /* For condition reductions, we have a vector (NEW_PHI_RESULT) containing
4583 various data values where the condition matched and another vector
4584 (INDUCTION_INDEX) containing all the indexes of those matches. We
4585 need to extract the last matching index (which will be the index with
4586 highest value) and use this to index into the data vector.
4587 For the case where there were no matches, the data vector will contain
4588 all default values and the index vector will be all zeros. */
4590 /* Get various versions of the type of the vector of indexes. */
4591 tree index_vec_type
= TREE_TYPE (induction_index
);
4592 gcc_checking_assert (TYPE_UNSIGNED (index_vec_type
));
4593 tree index_scalar_type
= TREE_TYPE (index_vec_type
);
4594 tree index_vec_cmp_type
= build_same_sized_truth_vector_type
4597 /* Get an unsigned integer version of the type of the data vector. */
4598 int scalar_precision
= GET_MODE_PRECISION (TYPE_MODE (scalar_type
));
4599 tree scalar_type_unsigned
= make_unsigned_type (scalar_precision
);
4600 tree vectype_unsigned
= build_vector_type
4601 (scalar_type_unsigned
, TYPE_VECTOR_SUBPARTS (vectype
));
4603 /* First we need to create a vector (ZERO_VEC) of zeros and another
4604 vector (MAX_INDEX_VEC) filled with the last matching index, which we
4605 can create using a MAX reduction and then expanding.
4606 In the case where the loop never made any matches, the max index will
4609 /* Vector of {0, 0, 0,...}. */
4610 tree zero_vec
= make_ssa_name (vectype
);
4611 tree zero_vec_rhs
= build_zero_cst (vectype
);
4612 gimple
*zero_vec_stmt
= gimple_build_assign (zero_vec
, zero_vec_rhs
);
4613 gsi_insert_before (&exit_gsi
, zero_vec_stmt
, GSI_SAME_STMT
);
4615 /* Find maximum value from the vector of found indexes. */
4616 tree max_index
= make_ssa_name (index_scalar_type
);
4617 gimple
*max_index_stmt
= gimple_build_assign (max_index
, REDUC_MAX_EXPR
,
4619 gsi_insert_before (&exit_gsi
, max_index_stmt
, GSI_SAME_STMT
);
4621 /* Vector of {max_index, max_index, max_index,...}. */
4622 tree max_index_vec
= make_ssa_name (index_vec_type
);
4623 tree max_index_vec_rhs
= build_vector_from_val (index_vec_type
,
4625 gimple
*max_index_vec_stmt
= gimple_build_assign (max_index_vec
,
4627 gsi_insert_before (&exit_gsi
, max_index_vec_stmt
, GSI_SAME_STMT
);
4629 /* Next we compare the new vector (MAX_INDEX_VEC) full of max indexes
4630 with the vector (INDUCTION_INDEX) of found indexes, choosing values
4631 from the data vector (NEW_PHI_RESULT) for matches, 0 (ZERO_VEC)
4632 otherwise. Only one value should match, resulting in a vector
4633 (VEC_COND) with one data value and the rest zeros.
4634 In the case where the loop never made any matches, every index will
4635 match, resulting in a vector with all data values (which will all be
4636 the default value). */
4638 /* Compare the max index vector to the vector of found indexes to find
4639 the position of the max value. */
4640 tree vec_compare
= make_ssa_name (index_vec_cmp_type
);
4641 gimple
*vec_compare_stmt
= gimple_build_assign (vec_compare
, EQ_EXPR
,
4644 gsi_insert_before (&exit_gsi
, vec_compare_stmt
, GSI_SAME_STMT
);
4646 /* Use the compare to choose either values from the data vector or
4648 tree vec_cond
= make_ssa_name (vectype
);
4649 gimple
*vec_cond_stmt
= gimple_build_assign (vec_cond
, VEC_COND_EXPR
,
4650 vec_compare
, new_phi_result
,
4652 gsi_insert_before (&exit_gsi
, vec_cond_stmt
, GSI_SAME_STMT
);
4654 /* Finally we need to extract the data value from the vector (VEC_COND)
4655 into a scalar (MATCHED_DATA_REDUC). Logically we want to do a OR
4656 reduction, but because this doesn't exist, we can use a MAX reduction
4657 instead. The data value might be signed or a float so we need to cast
4659 In the case where the loop never made any matches, the data values are
4660 all identical, and so will reduce down correctly. */
4662 /* Make the matched data values unsigned. */
4663 tree vec_cond_cast
= make_ssa_name (vectype_unsigned
);
4664 tree vec_cond_cast_rhs
= build1 (VIEW_CONVERT_EXPR
, vectype_unsigned
,
4666 gimple
*vec_cond_cast_stmt
= gimple_build_assign (vec_cond_cast
,
4669 gsi_insert_before (&exit_gsi
, vec_cond_cast_stmt
, GSI_SAME_STMT
);
4671 /* Reduce down to a scalar value. */
4672 tree data_reduc
= make_ssa_name (scalar_type_unsigned
);
4673 optab ot
= optab_for_tree_code (REDUC_MAX_EXPR
, vectype_unsigned
,
4675 gcc_assert (optab_handler (ot
, TYPE_MODE (vectype_unsigned
))
4676 != CODE_FOR_nothing
);
4677 gimple
*data_reduc_stmt
= gimple_build_assign (data_reduc
,
4680 gsi_insert_before (&exit_gsi
, data_reduc_stmt
, GSI_SAME_STMT
);
4682 /* Convert the reduced value back to the result type and set as the
4684 tree data_reduc_cast
= build1 (VIEW_CONVERT_EXPR
, scalar_type
,
4686 epilog_stmt
= gimple_build_assign (new_scalar_dest
, data_reduc_cast
);
4687 new_temp
= make_ssa_name (new_scalar_dest
, epilog_stmt
);
4688 gimple_assign_set_lhs (epilog_stmt
, new_temp
);
4689 gsi_insert_before (&exit_gsi
, epilog_stmt
, GSI_SAME_STMT
);
4690 scalar_results
.safe_push (new_temp
);
4693 /* 2.3 Create the reduction code, using one of the three schemes described
4694 above. In SLP we simply need to extract all the elements from the
4695 vector (without reducing them), so we use scalar shifts. */
4696 else if (reduc_code
!= ERROR_MARK
&& !slp_reduc
)
4701 /*** Case 1: Create:
4702 v_out2 = reduc_expr <v_out1> */
4704 if (dump_enabled_p ())
4705 dump_printf_loc (MSG_NOTE
, vect_location
,
4706 "Reduce using direct vector reduction.\n");
4708 vec_elem_type
= TREE_TYPE (TREE_TYPE (new_phi_result
));
4709 if (!useless_type_conversion_p (scalar_type
, vec_elem_type
))
4712 vect_create_destination_var (scalar_dest
, vec_elem_type
);
4713 tmp
= build1 (reduc_code
, vec_elem_type
, new_phi_result
);
4714 epilog_stmt
= gimple_build_assign (tmp_dest
, tmp
);
4715 new_temp
= make_ssa_name (tmp_dest
, epilog_stmt
);
4716 gimple_assign_set_lhs (epilog_stmt
, new_temp
);
4717 gsi_insert_before (&exit_gsi
, epilog_stmt
, GSI_SAME_STMT
);
4719 tmp
= build1 (NOP_EXPR
, scalar_type
, new_temp
);
4722 tmp
= build1 (reduc_code
, scalar_type
, new_phi_result
);
4724 epilog_stmt
= gimple_build_assign (new_scalar_dest
, tmp
);
4725 new_temp
= make_ssa_name (new_scalar_dest
, epilog_stmt
);
4726 gimple_assign_set_lhs (epilog_stmt
, new_temp
);
4727 gsi_insert_before (&exit_gsi
, epilog_stmt
, GSI_SAME_STMT
);
4729 if (STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
)
4730 == INTEGER_INDUC_COND_REDUCTION
)
4732 /* Earlier we set the initial value to be zero. Check the result
4733 and if it is zero then replace with the original initial
4735 tree zero
= build_zero_cst (scalar_type
);
4736 tree zcompare
= build2 (EQ_EXPR
, boolean_type_node
, new_temp
, zero
);
4738 tmp
= make_ssa_name (new_scalar_dest
);
4739 epilog_stmt
= gimple_build_assign (tmp
, COND_EXPR
, zcompare
,
4740 initial_def
, new_temp
);
4741 gsi_insert_before (&exit_gsi
, epilog_stmt
, GSI_SAME_STMT
);
4745 scalar_results
.safe_push (new_temp
);
4749 bool reduce_with_shift
= have_whole_vector_shift (mode
);
4750 int element_bitsize
= tree_to_uhwi (bitsize
);
4751 int vec_size_in_bits
= tree_to_uhwi (TYPE_SIZE (vectype
));
4754 /* Regardless of whether we have a whole vector shift, if we're
4755 emulating the operation via tree-vect-generic, we don't want
4756 to use it. Only the first round of the reduction is likely
4757 to still be profitable via emulation. */
4758 /* ??? It might be better to emit a reduction tree code here, so that
4759 tree-vect-generic can expand the first round via bit tricks. */
4760 if (!VECTOR_MODE_P (mode
))
4761 reduce_with_shift
= false;
4764 optab optab
= optab_for_tree_code (code
, vectype
, optab_default
);
4765 if (optab_handler (optab
, mode
) == CODE_FOR_nothing
)
4766 reduce_with_shift
= false;
4769 if (reduce_with_shift
&& !slp_reduc
)
4771 int nelements
= vec_size_in_bits
/ element_bitsize
;
4772 unsigned char *sel
= XALLOCAVEC (unsigned char, nelements
);
4776 tree zero_vec
= build_zero_cst (vectype
);
4777 /*** Case 2: Create:
4778 for (offset = nelements/2; offset >= 1; offset/=2)
4780 Create: va' = vec_shift <va, offset>
4781 Create: va = vop <va, va'>
4786 if (dump_enabled_p ())
4787 dump_printf_loc (MSG_NOTE
, vect_location
,
4788 "Reduce using vector shifts\n");
4790 vec_dest
= vect_create_destination_var (scalar_dest
, vectype
);
4791 new_temp
= new_phi_result
;
4792 for (elt_offset
= nelements
/ 2;
4796 calc_vec_perm_mask_for_shift (mode
, elt_offset
, sel
);
4797 tree mask
= vect_gen_perm_mask_any (vectype
, sel
);
4798 epilog_stmt
= gimple_build_assign (vec_dest
, VEC_PERM_EXPR
,
4799 new_temp
, zero_vec
, mask
);
4800 new_name
= make_ssa_name (vec_dest
, epilog_stmt
);
4801 gimple_assign_set_lhs (epilog_stmt
, new_name
);
4802 gsi_insert_before (&exit_gsi
, epilog_stmt
, GSI_SAME_STMT
);
4804 epilog_stmt
= gimple_build_assign (vec_dest
, code
, new_name
,
4806 new_temp
= make_ssa_name (vec_dest
, epilog_stmt
);
4807 gimple_assign_set_lhs (epilog_stmt
, new_temp
);
4808 gsi_insert_before (&exit_gsi
, epilog_stmt
, GSI_SAME_STMT
);
4811 /* 2.4 Extract the final scalar result. Create:
4812 s_out3 = extract_field <v_out2, bitpos> */
4814 if (dump_enabled_p ())
4815 dump_printf_loc (MSG_NOTE
, vect_location
,
4816 "extract scalar result\n");
4818 rhs
= build3 (BIT_FIELD_REF
, scalar_type
, new_temp
,
4819 bitsize
, bitsize_zero_node
);
4820 epilog_stmt
= gimple_build_assign (new_scalar_dest
, rhs
);
4821 new_temp
= make_ssa_name (new_scalar_dest
, epilog_stmt
);
4822 gimple_assign_set_lhs (epilog_stmt
, new_temp
);
4823 gsi_insert_before (&exit_gsi
, epilog_stmt
, GSI_SAME_STMT
);
4824 scalar_results
.safe_push (new_temp
);
4828 /*** Case 3: Create:
4829 s = extract_field <v_out2, 0>
4830 for (offset = element_size;
4831 offset < vector_size;
4832 offset += element_size;)
4834 Create: s' = extract_field <v_out2, offset>
4835 Create: s = op <s, s'> // For non SLP cases
4838 if (dump_enabled_p ())
4839 dump_printf_loc (MSG_NOTE
, vect_location
,
4840 "Reduce using scalar code.\n");
4842 vec_size_in_bits
= tree_to_uhwi (TYPE_SIZE (vectype
));
4843 FOR_EACH_VEC_ELT (new_phis
, i
, new_phi
)
4846 if (gimple_code (new_phi
) == GIMPLE_PHI
)
4847 vec_temp
= PHI_RESULT (new_phi
);
4849 vec_temp
= gimple_assign_lhs (new_phi
);
4850 tree rhs
= build3 (BIT_FIELD_REF
, scalar_type
, vec_temp
, bitsize
,
4852 epilog_stmt
= gimple_build_assign (new_scalar_dest
, rhs
);
4853 new_temp
= make_ssa_name (new_scalar_dest
, epilog_stmt
);
4854 gimple_assign_set_lhs (epilog_stmt
, new_temp
);
4855 gsi_insert_before (&exit_gsi
, epilog_stmt
, GSI_SAME_STMT
);
4857 /* In SLP we don't need to apply reduction operation, so we just
4858 collect s' values in SCALAR_RESULTS. */
4860 scalar_results
.safe_push (new_temp
);
4862 for (bit_offset
= element_bitsize
;
4863 bit_offset
< vec_size_in_bits
;
4864 bit_offset
+= element_bitsize
)
4866 tree bitpos
= bitsize_int (bit_offset
);
4867 tree rhs
= build3 (BIT_FIELD_REF
, scalar_type
, vec_temp
,
4870 epilog_stmt
= gimple_build_assign (new_scalar_dest
, rhs
);
4871 new_name
= make_ssa_name (new_scalar_dest
, epilog_stmt
);
4872 gimple_assign_set_lhs (epilog_stmt
, new_name
);
4873 gsi_insert_before (&exit_gsi
, epilog_stmt
, GSI_SAME_STMT
);
4877 /* In SLP we don't need to apply reduction operation, so
4878 we just collect s' values in SCALAR_RESULTS. */
4879 new_temp
= new_name
;
4880 scalar_results
.safe_push (new_name
);
4884 epilog_stmt
= gimple_build_assign (new_scalar_dest
, code
,
4885 new_name
, new_temp
);
4886 new_temp
= make_ssa_name (new_scalar_dest
, epilog_stmt
);
4887 gimple_assign_set_lhs (epilog_stmt
, new_temp
);
4888 gsi_insert_before (&exit_gsi
, epilog_stmt
, GSI_SAME_STMT
);
4893 /* The only case where we need to reduce scalar results in SLP, is
4894 unrolling. If the size of SCALAR_RESULTS is greater than
4895 GROUP_SIZE, we reduce them combining elements modulo
4899 tree res
, first_res
, new_res
;
4902 /* Reduce multiple scalar results in case of SLP unrolling. */
4903 for (j
= group_size
; scalar_results
.iterate (j
, &res
);
4906 first_res
= scalar_results
[j
% group_size
];
4907 new_stmt
= gimple_build_assign (new_scalar_dest
, code
,
4909 new_res
= make_ssa_name (new_scalar_dest
, new_stmt
);
4910 gimple_assign_set_lhs (new_stmt
, new_res
);
4911 gsi_insert_before (&exit_gsi
, new_stmt
, GSI_SAME_STMT
);
4912 scalar_results
[j
% group_size
] = new_res
;
4916 /* Not SLP - we have one scalar to keep in SCALAR_RESULTS. */
4917 scalar_results
.safe_push (new_temp
);
4921 vect_finalize_reduction
:
4926 /* 2.5 Adjust the final result by the initial value of the reduction
4927 variable. (When such adjustment is not needed, then
4928 'adjustment_def' is zero). For example, if code is PLUS we create:
4929 new_temp = loop_exit_def + adjustment_def */
4933 gcc_assert (!slp_reduc
);
4934 if (nested_in_vect_loop
)
4936 new_phi
= new_phis
[0];
4937 gcc_assert (TREE_CODE (TREE_TYPE (adjustment_def
)) == VECTOR_TYPE
);
4938 expr
= build2 (code
, vectype
, PHI_RESULT (new_phi
), adjustment_def
);
4939 new_dest
= vect_create_destination_var (scalar_dest
, vectype
);
4943 new_temp
= scalar_results
[0];
4944 gcc_assert (TREE_CODE (TREE_TYPE (adjustment_def
)) != VECTOR_TYPE
);
4945 expr
= build2 (code
, scalar_type
, new_temp
, adjustment_def
);
4946 new_dest
= vect_create_destination_var (scalar_dest
, scalar_type
);
4949 epilog_stmt
= gimple_build_assign (new_dest
, expr
);
4950 new_temp
= make_ssa_name (new_dest
, epilog_stmt
);
4951 gimple_assign_set_lhs (epilog_stmt
, new_temp
);
4952 gsi_insert_before (&exit_gsi
, epilog_stmt
, GSI_SAME_STMT
);
4953 if (nested_in_vect_loop
)
4955 set_vinfo_for_stmt (epilog_stmt
,
4956 new_stmt_vec_info (epilog_stmt
, loop_vinfo
));
4957 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (epilog_stmt
)) =
4958 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (new_phi
));
4961 scalar_results
.quick_push (new_temp
);
4963 scalar_results
[0] = new_temp
;
4966 scalar_results
[0] = new_temp
;
4968 new_phis
[0] = epilog_stmt
;
4971 /* 2.6 Handle the loop-exit phis. Replace the uses of scalar loop-exit
4972 phis with new adjusted scalar results, i.e., replace use <s_out0>
4977 s_out0 = phi <s_loop> # (scalar) EXIT_PHI
4978 v_out1 = phi <VECT_DEF> # NEW_EXIT_PHI
4979 v_out2 = reduce <v_out1>
4980 s_out3 = extract_field <v_out2, 0>
4981 s_out4 = adjust_result <s_out3>
4988 s_out0 = phi <s_loop> # (scalar) EXIT_PHI
4989 v_out1 = phi <VECT_DEF> # NEW_EXIT_PHI
4990 v_out2 = reduce <v_out1>
4991 s_out3 = extract_field <v_out2, 0>
4992 s_out4 = adjust_result <s_out3>
4997 /* In SLP reduction chain we reduce vector results into one vector if
4998 necessary, hence we set here GROUP_SIZE to 1. SCALAR_DEST is the LHS of
4999 the last stmt in the reduction chain, since we are looking for the loop
5001 if (GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt
)))
5003 gimple
*dest_stmt
= SLP_TREE_SCALAR_STMTS (slp_node
)[group_size
- 1];
5004 /* Handle reduction patterns. */
5005 if (STMT_VINFO_RELATED_STMT (vinfo_for_stmt (dest_stmt
)))
5006 dest_stmt
= STMT_VINFO_RELATED_STMT (vinfo_for_stmt (dest_stmt
));
5008 scalar_dest
= gimple_assign_lhs (dest_stmt
);
5012 /* In SLP we may have several statements in NEW_PHIS and REDUCTION_PHIS (in
5013 case that GROUP_SIZE is greater than vectorization factor). Therefore, we
5014 need to match SCALAR_RESULTS with corresponding statements. The first
5015 (GROUP_SIZE / number of new vector stmts) scalar results correspond to
5016 the first vector stmt, etc.
5017 (RATIO is equal to (GROUP_SIZE / number of new vector stmts)). */
5018 if (group_size
> new_phis
.length ())
5020 ratio
= group_size
/ new_phis
.length ();
5021 gcc_assert (!(group_size
% new_phis
.length ()));
5026 for (k
= 0; k
< group_size
; k
++)
5030 epilog_stmt
= new_phis
[k
/ ratio
];
5031 reduction_phi
= reduction_phis
[k
/ ratio
];
5033 inner_phi
= inner_phis
[k
/ ratio
];
5038 gimple
*current_stmt
= SLP_TREE_SCALAR_STMTS (slp_node
)[k
];
5040 orig_stmt
= STMT_VINFO_RELATED_STMT (vinfo_for_stmt (current_stmt
));
5041 /* SLP statements can't participate in patterns. */
5042 gcc_assert (!orig_stmt
);
5043 scalar_dest
= gimple_assign_lhs (current_stmt
);
5047 /* Find the loop-closed-use at the loop exit of the original scalar
5048 result. (The reduction result is expected to have two immediate uses -
5049 one at the latch block, and one at the loop exit). */
5050 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, scalar_dest
)
5051 if (!flow_bb_inside_loop_p (loop
, gimple_bb (USE_STMT (use_p
)))
5052 && !is_gimple_debug (USE_STMT (use_p
)))
5053 phis
.safe_push (USE_STMT (use_p
));
5055 /* While we expect to have found an exit_phi because of loop-closed-ssa
5056 form we can end up without one if the scalar cycle is dead. */
5058 FOR_EACH_VEC_ELT (phis
, i
, exit_phi
)
5062 stmt_vec_info exit_phi_vinfo
= vinfo_for_stmt (exit_phi
);
5065 /* FORNOW. Currently not supporting the case that an inner-loop
5066 reduction is not used in the outer-loop (but only outside the
5067 outer-loop), unless it is double reduction. */
5068 gcc_assert ((STMT_VINFO_RELEVANT_P (exit_phi_vinfo
)
5069 && !STMT_VINFO_LIVE_P (exit_phi_vinfo
))
5073 STMT_VINFO_VEC_STMT (exit_phi_vinfo
) = inner_phi
;
5075 STMT_VINFO_VEC_STMT (exit_phi_vinfo
) = epilog_stmt
;
5077 || STMT_VINFO_DEF_TYPE (exit_phi_vinfo
)
5078 != vect_double_reduction_def
)
5081 /* Handle double reduction:
5083 stmt1: s1 = phi <s0, s2> - double reduction phi (outer loop)
5084 stmt2: s3 = phi <s1, s4> - (regular) reduc phi (inner loop)
5085 stmt3: s4 = use (s3) - (regular) reduc stmt (inner loop)
5086 stmt4: s2 = phi <s4> - double reduction stmt (outer loop)
5088 At that point the regular reduction (stmt2 and stmt3) is
5089 already vectorized, as well as the exit phi node, stmt4.
5090 Here we vectorize the phi node of double reduction, stmt1, and
5091 update all relevant statements. */
5093 /* Go through all the uses of s2 to find double reduction phi
5094 node, i.e., stmt1 above. */
5095 orig_name
= PHI_RESULT (exit_phi
);
5096 FOR_EACH_IMM_USE_STMT (use_stmt
, imm_iter
, orig_name
)
5098 stmt_vec_info use_stmt_vinfo
;
5099 stmt_vec_info new_phi_vinfo
;
5100 tree vect_phi_init
, preheader_arg
, vect_phi_res
, init_def
;
5101 basic_block bb
= gimple_bb (use_stmt
);
5104 /* Check that USE_STMT is really double reduction phi
5106 if (gimple_code (use_stmt
) != GIMPLE_PHI
5107 || gimple_phi_num_args (use_stmt
) != 2
5108 || bb
->loop_father
!= outer_loop
)
5110 use_stmt_vinfo
= vinfo_for_stmt (use_stmt
);
5112 || STMT_VINFO_DEF_TYPE (use_stmt_vinfo
)
5113 != vect_double_reduction_def
)
5116 /* Create vector phi node for double reduction:
5117 vs1 = phi <vs0, vs2>
5118 vs1 was created previously in this function by a call to
5119 vect_get_vec_def_for_operand and is stored in
5121 vs2 is defined by INNER_PHI, the vectorized EXIT_PHI;
5122 vs0 is created here. */
5124 /* Create vector phi node. */
5125 vect_phi
= create_phi_node (vec_initial_def
, bb
);
5126 new_phi_vinfo
= new_stmt_vec_info (vect_phi
,
5127 loop_vec_info_for_loop (outer_loop
));
5128 set_vinfo_for_stmt (vect_phi
, new_phi_vinfo
);
5130 /* Create vs0 - initial def of the double reduction phi. */
5131 preheader_arg
= PHI_ARG_DEF_FROM_EDGE (use_stmt
,
5132 loop_preheader_edge (outer_loop
));
5133 init_def
= get_initial_def_for_reduction (stmt
,
5134 preheader_arg
, NULL
);
5135 vect_phi_init
= vect_init_vector (use_stmt
, init_def
,
5138 /* Update phi node arguments with vs0 and vs2. */
5139 add_phi_arg (vect_phi
, vect_phi_init
,
5140 loop_preheader_edge (outer_loop
),
5142 add_phi_arg (vect_phi
, PHI_RESULT (inner_phi
),
5143 loop_latch_edge (outer_loop
), UNKNOWN_LOCATION
);
5144 if (dump_enabled_p ())
5146 dump_printf_loc (MSG_NOTE
, vect_location
,
5147 "created double reduction phi node: ");
5148 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
, vect_phi
, 0);
5149 dump_printf (MSG_NOTE
, "\n");
5152 vect_phi_res
= PHI_RESULT (vect_phi
);
5154 /* Replace the use, i.e., set the correct vs1 in the regular
5155 reduction phi node. FORNOW, NCOPIES is always 1, so the
5156 loop is redundant. */
5157 use
= reduction_phi
;
5158 for (j
= 0; j
< ncopies
; j
++)
5160 edge pr_edge
= loop_preheader_edge (loop
);
5161 SET_PHI_ARG_DEF (use
, pr_edge
->dest_idx
, vect_phi_res
);
5162 use
= STMT_VINFO_RELATED_STMT (vinfo_for_stmt (use
));
5169 if (nested_in_vect_loop
)
5178 /* Find the loop-closed-use at the loop exit of the original scalar
5179 result. (The reduction result is expected to have two immediate uses,
5180 one at the latch block, and one at the loop exit). For double
5181 reductions we are looking for exit phis of the outer loop. */
5182 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, scalar_dest
)
5184 if (!flow_bb_inside_loop_p (loop
, gimple_bb (USE_STMT (use_p
))))
5186 if (!is_gimple_debug (USE_STMT (use_p
)))
5187 phis
.safe_push (USE_STMT (use_p
));
5191 if (double_reduc
&& gimple_code (USE_STMT (use_p
)) == GIMPLE_PHI
)
5193 tree phi_res
= PHI_RESULT (USE_STMT (use_p
));
5195 FOR_EACH_IMM_USE_FAST (phi_use_p
, phi_imm_iter
, phi_res
)
5197 if (!flow_bb_inside_loop_p (loop
,
5198 gimple_bb (USE_STMT (phi_use_p
)))
5199 && !is_gimple_debug (USE_STMT (phi_use_p
)))
5200 phis
.safe_push (USE_STMT (phi_use_p
));
5206 FOR_EACH_VEC_ELT (phis
, i
, exit_phi
)
5208 /* Replace the uses: */
5209 orig_name
= PHI_RESULT (exit_phi
);
5210 scalar_result
= scalar_results
[k
];
5211 FOR_EACH_IMM_USE_STMT (use_stmt
, imm_iter
, orig_name
)
5212 FOR_EACH_IMM_USE_ON_STMT (use_p
, imm_iter
)
5213 SET_USE (use_p
, scalar_result
);
5221 /* Function is_nonwrapping_integer_induction.
5223 Check if STMT (which is part of loop LOOP) both increments and
5224 does not cause overflow. */
5227 is_nonwrapping_integer_induction (gimple
*stmt
, struct loop
*loop
)
5229 stmt_vec_info stmt_vinfo
= vinfo_for_stmt (stmt
);
5230 tree base
= STMT_VINFO_LOOP_PHI_EVOLUTION_BASE_UNCHANGED (stmt_vinfo
);
5231 tree step
= STMT_VINFO_LOOP_PHI_EVOLUTION_PART (stmt_vinfo
);
5232 tree lhs_type
= TREE_TYPE (gimple_phi_result (stmt
));
5233 widest_int ni
, max_loop_value
, lhs_max
;
5234 bool overflow
= false;
5236 /* Make sure the loop is integer based. */
5237 if (TREE_CODE (base
) != INTEGER_CST
5238 || TREE_CODE (step
) != INTEGER_CST
)
5241 /* Check that the induction increments. */
5242 if (tree_int_cst_sgn (step
) == -1)
5245 /* Check that the max size of the loop will not wrap. */
5247 if (TYPE_OVERFLOW_UNDEFINED (lhs_type
))
5250 if (! max_stmt_executions (loop
, &ni
))
5253 max_loop_value
= wi::mul (wi::to_widest (step
), ni
, TYPE_SIGN (lhs_type
),
5258 max_loop_value
= wi::add (wi::to_widest (base
), max_loop_value
,
5259 TYPE_SIGN (lhs_type
), &overflow
);
5263 return (wi::min_precision (max_loop_value
, TYPE_SIGN (lhs_type
))
5264 <= TYPE_PRECISION (lhs_type
));
5267 /* Function vectorizable_reduction.
5269 Check if STMT performs a reduction operation that can be vectorized.
5270 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
5271 stmt to replace it, put it in VEC_STMT, and insert it at GSI.
5272 Return FALSE if not a vectorizable STMT, TRUE otherwise.
5274 This function also handles reduction idioms (patterns) that have been
5275 recognized in advance during vect_pattern_recog. In this case, STMT may be
5277 X = pattern_expr (arg0, arg1, ..., X)
5278 and it's STMT_VINFO_RELATED_STMT points to the last stmt in the original
5279 sequence that had been detected and replaced by the pattern-stmt (STMT).
5281 This function also handles reduction of condition expressions, for example:
5282 for (int i = 0; i < N; i++)
5285 This is handled by vectorising the loop and creating an additional vector
5286 containing the loop indexes for which "a[i] < value" was true. In the
5287 function epilogue this is reduced to a single max value and then used to
5288 index into the vector of results.
5290 In some cases of reduction patterns, the type of the reduction variable X is
5291 different than the type of the other arguments of STMT.
5292 In such cases, the vectype that is used when transforming STMT into a vector
5293 stmt is different than the vectype that is used to determine the
5294 vectorization factor, because it consists of a different number of elements
5295 than the actual number of elements that are being operated upon in parallel.
5297 For example, consider an accumulation of shorts into an int accumulator.
5298 On some targets it's possible to vectorize this pattern operating on 8
5299 shorts at a time (hence, the vectype for purposes of determining the
5300 vectorization factor should be V8HI); on the other hand, the vectype that
5301 is used to create the vector form is actually V4SI (the type of the result).
5303 Upon entry to this function, STMT_VINFO_VECTYPE records the vectype that
5304 indicates what is the actual level of parallelism (V8HI in the example), so
5305 that the right vectorization factor would be derived. This vectype
5306 corresponds to the type of arguments to the reduction stmt, and should *NOT*
5307 be used to create the vectorized stmt. The right vectype for the vectorized
5308 stmt is obtained from the type of the result X:
5309 get_vectype_for_scalar_type (TREE_TYPE (X))
5311 This means that, contrary to "regular" reductions (or "regular" stmts in
5312 general), the following equation:
5313 STMT_VINFO_VECTYPE == get_vectype_for_scalar_type (TREE_TYPE (X))
5314 does *NOT* necessarily hold for reduction patterns. */
5317 vectorizable_reduction (gimple
*stmt
, gimple_stmt_iterator
*gsi
,
5318 gimple
**vec_stmt
, slp_tree slp_node
)
5322 tree loop_vec_def0
= NULL_TREE
, loop_vec_def1
= NULL_TREE
;
5323 stmt_vec_info stmt_info
= vinfo_for_stmt (stmt
);
5324 tree vectype_out
= STMT_VINFO_VECTYPE (stmt_info
);
5325 tree vectype_in
= NULL_TREE
;
5326 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_info
);
5327 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
5328 enum tree_code code
, orig_code
, epilog_reduc_code
;
5329 machine_mode vec_mode
;
5331 optab optab
, reduc_optab
;
5332 tree new_temp
= NULL_TREE
;
5334 enum vect_def_type dt
;
5335 gphi
*new_phi
= NULL
;
5339 stmt_vec_info orig_stmt_info
;
5340 tree expr
= NULL_TREE
;
5344 stmt_vec_info prev_stmt_info
, prev_phi_info
;
5345 bool single_defuse_cycle
= false;
5346 tree reduc_def
= NULL_TREE
;
5347 gimple
*new_stmt
= NULL
;
5350 bool nested_cycle
= false, found_nested_cycle_def
= false;
5351 gimple
*reduc_def_stmt
= NULL
;
5352 bool double_reduc
= false, dummy
;
5354 struct loop
* def_stmt_loop
, *outer_loop
= NULL
;
5356 gimple
*def_arg_stmt
;
5357 auto_vec
<tree
> vec_oprnds0
;
5358 auto_vec
<tree
> vec_oprnds1
;
5359 auto_vec
<tree
> vect_defs
;
5360 auto_vec
<gimple
*> phis
;
5362 tree def0
, def1
, tem
, op0
, op1
= NULL_TREE
;
5363 bool first_p
= true;
5364 tree cr_index_scalar_type
= NULL_TREE
, cr_index_vector_type
= NULL_TREE
;
5365 gimple
*cond_expr_induction_def_stmt
= NULL
;
5367 /* In case of reduction chain we switch to the first stmt in the chain, but
5368 we don't update STMT_INFO, since only the last stmt is marked as reduction
5369 and has reduction properties. */
5370 if (GROUP_FIRST_ELEMENT (stmt_info
)
5371 && GROUP_FIRST_ELEMENT (stmt_info
) != stmt
)
5373 stmt
= GROUP_FIRST_ELEMENT (stmt_info
);
5377 if (nested_in_vect_loop_p (loop
, stmt
))
5381 nested_cycle
= true;
5384 /* 1. Is vectorizable reduction? */
5385 /* Not supportable if the reduction variable is used in the loop, unless
5386 it's a reduction chain. */
5387 if (STMT_VINFO_RELEVANT (stmt_info
) > vect_used_in_outer
5388 && !GROUP_FIRST_ELEMENT (stmt_info
))
5391 /* Reductions that are not used even in an enclosing outer-loop,
5392 are expected to be "live" (used out of the loop). */
5393 if (STMT_VINFO_RELEVANT (stmt_info
) == vect_unused_in_scope
5394 && !STMT_VINFO_LIVE_P (stmt_info
))
5397 /* Make sure it was already recognized as a reduction computation. */
5398 if (STMT_VINFO_DEF_TYPE (vinfo_for_stmt (stmt
)) != vect_reduction_def
5399 && STMT_VINFO_DEF_TYPE (vinfo_for_stmt (stmt
)) != vect_nested_cycle
)
5402 /* 2. Has this been recognized as a reduction pattern?
5404 Check if STMT represents a pattern that has been recognized
5405 in earlier analysis stages. For stmts that represent a pattern,
5406 the STMT_VINFO_RELATED_STMT field records the last stmt in
5407 the original sequence that constitutes the pattern. */
5409 orig_stmt
= STMT_VINFO_RELATED_STMT (vinfo_for_stmt (stmt
));
5412 orig_stmt_info
= vinfo_for_stmt (orig_stmt
);
5413 gcc_assert (STMT_VINFO_IN_PATTERN_P (orig_stmt_info
));
5414 gcc_assert (!STMT_VINFO_IN_PATTERN_P (stmt_info
));
5417 /* 3. Check the operands of the operation. The first operands are defined
5418 inside the loop body. The last operand is the reduction variable,
5419 which is defined by the loop-header-phi. */
5421 gcc_assert (is_gimple_assign (stmt
));
5424 switch (get_gimple_rhs_class (gimple_assign_rhs_code (stmt
)))
5426 case GIMPLE_SINGLE_RHS
:
5427 op_type
= TREE_OPERAND_LENGTH (gimple_assign_rhs1 (stmt
));
5428 if (op_type
== ternary_op
)
5430 tree rhs
= gimple_assign_rhs1 (stmt
);
5431 ops
[0] = TREE_OPERAND (rhs
, 0);
5432 ops
[1] = TREE_OPERAND (rhs
, 1);
5433 ops
[2] = TREE_OPERAND (rhs
, 2);
5434 code
= TREE_CODE (rhs
);
5440 case GIMPLE_BINARY_RHS
:
5441 code
= gimple_assign_rhs_code (stmt
);
5442 op_type
= TREE_CODE_LENGTH (code
);
5443 gcc_assert (op_type
== binary_op
);
5444 ops
[0] = gimple_assign_rhs1 (stmt
);
5445 ops
[1] = gimple_assign_rhs2 (stmt
);
5448 case GIMPLE_TERNARY_RHS
:
5449 code
= gimple_assign_rhs_code (stmt
);
5450 op_type
= TREE_CODE_LENGTH (code
);
5451 gcc_assert (op_type
== ternary_op
);
5452 ops
[0] = gimple_assign_rhs1 (stmt
);
5453 ops
[1] = gimple_assign_rhs2 (stmt
);
5454 ops
[2] = gimple_assign_rhs3 (stmt
);
5457 case GIMPLE_UNARY_RHS
:
5463 /* The default is that the reduction variable is the last in statement. */
5464 int reduc_index
= op_type
- 1;
5465 if (code
== MINUS_EXPR
)
5468 if (code
== COND_EXPR
&& slp_node
)
5471 scalar_dest
= gimple_assign_lhs (stmt
);
5472 scalar_type
= TREE_TYPE (scalar_dest
);
5473 if (!POINTER_TYPE_P (scalar_type
) && !INTEGRAL_TYPE_P (scalar_type
)
5474 && !SCALAR_FLOAT_TYPE_P (scalar_type
))
5477 /* Do not try to vectorize bit-precision reductions. */
5478 if ((TYPE_PRECISION (scalar_type
)
5479 != GET_MODE_PRECISION (TYPE_MODE (scalar_type
))))
5482 /* All uses but the last are expected to be defined in the loop.
5483 The last use is the reduction variable. In case of nested cycle this
5484 assumption is not true: we use reduc_index to record the index of the
5485 reduction variable. */
5486 for (i
= 0; i
< op_type
; i
++)
5488 if (i
== reduc_index
)
5491 /* The condition of COND_EXPR is checked in vectorizable_condition(). */
5492 if (i
== 0 && code
== COND_EXPR
)
5495 is_simple_use
= vect_is_simple_use (ops
[i
], loop_vinfo
,
5496 &def_stmt
, &dt
, &tem
);
5499 gcc_assert (is_simple_use
);
5501 if (dt
!= vect_internal_def
5502 && dt
!= vect_external_def
5503 && dt
!= vect_constant_def
5504 && dt
!= vect_induction_def
5505 && !(dt
== vect_nested_cycle
&& nested_cycle
))
5508 if (dt
== vect_nested_cycle
)
5510 found_nested_cycle_def
= true;
5511 reduc_def_stmt
= def_stmt
;
5515 if (i
== 1 && code
== COND_EXPR
&& dt
== vect_induction_def
)
5516 cond_expr_induction_def_stmt
= def_stmt
;
5519 is_simple_use
= vect_is_simple_use (ops
[reduc_index
], loop_vinfo
,
5520 &def_stmt
, &dt
, &tem
);
5523 gcc_assert (is_simple_use
);
5524 if (!found_nested_cycle_def
)
5525 reduc_def_stmt
= def_stmt
;
5527 if (reduc_def_stmt
&& gimple_code (reduc_def_stmt
) != GIMPLE_PHI
)
5530 if (!(dt
== vect_reduction_def
5531 || dt
== vect_nested_cycle
5532 || ((dt
== vect_internal_def
|| dt
== vect_external_def
5533 || dt
== vect_constant_def
|| dt
== vect_induction_def
)
5534 && nested_cycle
&& found_nested_cycle_def
)))
5536 /* For pattern recognized stmts, orig_stmt might be a reduction,
5537 but some helper statements for the pattern might not, or
5538 might be COND_EXPRs with reduction uses in the condition. */
5539 gcc_assert (orig_stmt
);
5543 enum vect_reduction_type v_reduc_type
;
5544 gimple
*tmp
= vect_is_simple_reduction (loop_vinfo
, reduc_def_stmt
,
5545 !nested_cycle
, &dummy
, false,
5548 /* If we have a condition reduction, see if we can simplify it further. */
5549 if (v_reduc_type
== COND_REDUCTION
5550 && cond_expr_induction_def_stmt
!= NULL
5551 && is_nonwrapping_integer_induction (cond_expr_induction_def_stmt
, loop
))
5553 if (dump_enabled_p ())
5554 dump_printf_loc (MSG_NOTE
, vect_location
,
5555 "condition expression based on integer induction.\n");
5556 STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
) = INTEGER_INDUC_COND_REDUCTION
;
5559 STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
) = v_reduc_type
;
5562 gcc_assert (tmp
== orig_stmt
5563 || GROUP_FIRST_ELEMENT (vinfo_for_stmt (tmp
)) == orig_stmt
);
5565 /* We changed STMT to be the first stmt in reduction chain, hence we
5566 check that in this case the first element in the chain is STMT. */
5567 gcc_assert (stmt
== tmp
5568 || GROUP_FIRST_ELEMENT (vinfo_for_stmt (tmp
)) == stmt
);
5570 if (STMT_VINFO_LIVE_P (vinfo_for_stmt (reduc_def_stmt
)))
5573 if (slp_node
|| PURE_SLP_STMT (stmt_info
))
5576 ncopies
= (LOOP_VINFO_VECT_FACTOR (loop_vinfo
)
5577 / TYPE_VECTOR_SUBPARTS (vectype_in
));
5579 gcc_assert (ncopies
>= 1);
5581 vec_mode
= TYPE_MODE (vectype_in
);
5583 if (code
== COND_EXPR
)
5585 /* Only call during the analysis stage, otherwise we'll lose
5587 if (!vec_stmt
&& !vectorizable_condition (stmt
, gsi
, NULL
,
5588 ops
[reduc_index
], 0, NULL
))
5590 if (dump_enabled_p ())
5591 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
5592 "unsupported condition in reduction\n");
5598 /* 4. Supportable by target? */
5600 if (code
== LSHIFT_EXPR
|| code
== RSHIFT_EXPR
5601 || code
== LROTATE_EXPR
|| code
== RROTATE_EXPR
)
5603 /* Shifts and rotates are only supported by vectorizable_shifts,
5604 not vectorizable_reduction. */
5605 if (dump_enabled_p ())
5606 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
5607 "unsupported shift or rotation.\n");
5611 /* 4.1. check support for the operation in the loop */
5612 optab
= optab_for_tree_code (code
, vectype_in
, optab_default
);
5615 if (dump_enabled_p ())
5616 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
5622 if (optab_handler (optab
, vec_mode
) == CODE_FOR_nothing
)
5624 if (dump_enabled_p ())
5625 dump_printf (MSG_NOTE
, "op not supported by target.\n");
5627 if (GET_MODE_SIZE (vec_mode
) != UNITS_PER_WORD
5628 || LOOP_VINFO_VECT_FACTOR (loop_vinfo
)
5629 < vect_min_worthwhile_factor (code
))
5632 if (dump_enabled_p ())
5633 dump_printf (MSG_NOTE
, "proceeding using word mode.\n");
5636 /* Worthwhile without SIMD support? */
5637 if (!VECTOR_MODE_P (TYPE_MODE (vectype_in
))
5638 && LOOP_VINFO_VECT_FACTOR (loop_vinfo
)
5639 < vect_min_worthwhile_factor (code
))
5641 if (dump_enabled_p ())
5642 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
5643 "not worthwhile without SIMD support.\n");
5649 /* 4.2. Check support for the epilog operation.
5651 If STMT represents a reduction pattern, then the type of the
5652 reduction variable may be different than the type of the rest
5653 of the arguments. For example, consider the case of accumulation
5654 of shorts into an int accumulator; The original code:
5655 S1: int_a = (int) short_a;
5656 orig_stmt-> S2: int_acc = plus <int_a ,int_acc>;
5659 STMT: int_acc = widen_sum <short_a, int_acc>
5662 1. The tree-code that is used to create the vector operation in the
5663 epilog code (that reduces the partial results) is not the
5664 tree-code of STMT, but is rather the tree-code of the original
5665 stmt from the pattern that STMT is replacing. I.e, in the example
5666 above we want to use 'widen_sum' in the loop, but 'plus' in the
5668 2. The type (mode) we use to check available target support
5669 for the vector operation to be created in the *epilog*, is
5670 determined by the type of the reduction variable (in the example
5671 above we'd check this: optab_handler (plus_optab, vect_int_mode])).
5672 However the type (mode) we use to check available target support
5673 for the vector operation to be created *inside the loop*, is
5674 determined by the type of the other arguments to STMT (in the
5675 example we'd check this: optab_handler (widen_sum_optab,
5678 This is contrary to "regular" reductions, in which the types of all
5679 the arguments are the same as the type of the reduction variable.
5680 For "regular" reductions we can therefore use the same vector type
5681 (and also the same tree-code) when generating the epilog code and
5682 when generating the code inside the loop. */
5686 /* This is a reduction pattern: get the vectype from the type of the
5687 reduction variable, and get the tree-code from orig_stmt. */
5688 gcc_assert (STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
)
5689 == TREE_CODE_REDUCTION
);
5690 orig_code
= gimple_assign_rhs_code (orig_stmt
);
5691 gcc_assert (vectype_out
);
5692 vec_mode
= TYPE_MODE (vectype_out
);
5696 /* Regular reduction: use the same vectype and tree-code as used for
5697 the vector code inside the loop can be used for the epilog code. */
5700 if (code
== MINUS_EXPR
)
5701 orig_code
= PLUS_EXPR
;
5703 /* For simple condition reductions, replace with the actual expression
5704 we want to base our reduction around. */
5705 if (STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
)
5706 == INTEGER_INDUC_COND_REDUCTION
)
5707 orig_code
= MAX_EXPR
;
5712 def_bb
= gimple_bb (reduc_def_stmt
);
5713 def_stmt_loop
= def_bb
->loop_father
;
5714 def_arg
= PHI_ARG_DEF_FROM_EDGE (reduc_def_stmt
,
5715 loop_preheader_edge (def_stmt_loop
));
5716 if (TREE_CODE (def_arg
) == SSA_NAME
5717 && (def_arg_stmt
= SSA_NAME_DEF_STMT (def_arg
))
5718 && gimple_code (def_arg_stmt
) == GIMPLE_PHI
5719 && flow_bb_inside_loop_p (outer_loop
, gimple_bb (def_arg_stmt
))
5720 && vinfo_for_stmt (def_arg_stmt
)
5721 && STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def_arg_stmt
))
5722 == vect_double_reduction_def
)
5723 double_reduc
= true;
5726 epilog_reduc_code
= ERROR_MARK
;
5728 if (STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
) == TREE_CODE_REDUCTION
5729 || STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
)
5730 == INTEGER_INDUC_COND_REDUCTION
)
5732 if (reduction_code_for_scalar_code (orig_code
, &epilog_reduc_code
))
5734 reduc_optab
= optab_for_tree_code (epilog_reduc_code
, vectype_out
,
5738 if (dump_enabled_p ())
5739 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
5740 "no optab for reduction.\n");
5742 epilog_reduc_code
= ERROR_MARK
;
5744 else if (optab_handler (reduc_optab
, vec_mode
) == CODE_FOR_nothing
)
5746 optab
= scalar_reduc_to_vector (reduc_optab
, vectype_out
);
5747 if (optab_handler (optab
, vec_mode
) == CODE_FOR_nothing
)
5749 if (dump_enabled_p ())
5750 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
5751 "reduc op not supported by target.\n");
5753 epilog_reduc_code
= ERROR_MARK
;
5757 /* When epilog_reduc_code is ERROR_MARK then a reduction will be
5758 generated in the epilog using multiple expressions. This does not
5759 work for condition reductions. */
5760 if (epilog_reduc_code
== ERROR_MARK
5761 && STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
)
5762 == INTEGER_INDUC_COND_REDUCTION
)
5764 if (dump_enabled_p ())
5765 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
5766 "no reduc code for scalar code.\n");
5772 if (!nested_cycle
|| double_reduc
)
5774 if (dump_enabled_p ())
5775 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
5776 "no reduc code for scalar code.\n");
5784 int scalar_precision
= GET_MODE_PRECISION (TYPE_MODE (scalar_type
));
5785 cr_index_scalar_type
= make_unsigned_type (scalar_precision
);
5786 cr_index_vector_type
= build_vector_type
5787 (cr_index_scalar_type
, TYPE_VECTOR_SUBPARTS (vectype_out
));
5789 epilog_reduc_code
= REDUC_MAX_EXPR
;
5790 optab
= optab_for_tree_code (REDUC_MAX_EXPR
, cr_index_vector_type
,
5792 if (optab_handler (optab
, TYPE_MODE (cr_index_vector_type
))
5793 == CODE_FOR_nothing
)
5795 if (dump_enabled_p ())
5796 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
5797 "reduc max op not supported by target.\n");
5803 || STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
) == COND_REDUCTION
5804 || STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
)
5805 == INTEGER_INDUC_COND_REDUCTION
)
5808 if (dump_enabled_p ())
5809 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
5810 "multiple types in double reduction or condition "
5815 /* In case of widenning multiplication by a constant, we update the type
5816 of the constant to be the type of the other operand. We check that the
5817 constant fits the type in the pattern recognition pass. */
5818 if (code
== DOT_PROD_EXPR
5819 && !types_compatible_p (TREE_TYPE (ops
[0]), TREE_TYPE (ops
[1])))
5821 if (TREE_CODE (ops
[0]) == INTEGER_CST
)
5822 ops
[0] = fold_convert (TREE_TYPE (ops
[1]), ops
[0]);
5823 else if (TREE_CODE (ops
[1]) == INTEGER_CST
)
5824 ops
[1] = fold_convert (TREE_TYPE (ops
[0]), ops
[1]);
5827 if (dump_enabled_p ())
5828 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
5829 "invalid types in dot-prod\n");
5835 if (STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
) == COND_REDUCTION
)
5839 if (! max_loop_iterations (loop
, &ni
))
5841 if (dump_enabled_p ())
5842 dump_printf_loc (MSG_NOTE
, vect_location
,
5843 "loop count not known, cannot create cond "
5847 /* Convert backedges to iterations. */
5850 /* The additional index will be the same type as the condition. Check
5851 that the loop can fit into this less one (because we'll use up the
5852 zero slot for when there are no matches). */
5853 tree max_index
= TYPE_MAX_VALUE (cr_index_scalar_type
);
5854 if (wi::geu_p (ni
, wi::to_widest (max_index
)))
5856 if (dump_enabled_p ())
5857 dump_printf_loc (MSG_NOTE
, vect_location
,
5858 "loop size is greater than data size.\n");
5863 if (!vec_stmt
) /* transformation not required. */
5866 && !vect_model_reduction_cost (stmt_info
, epilog_reduc_code
, ncopies
,
5869 STMT_VINFO_TYPE (stmt_info
) = reduc_vec_info_type
;
5875 if (dump_enabled_p ())
5876 dump_printf_loc (MSG_NOTE
, vect_location
, "transform reduction.\n");
5878 /* FORNOW: Multiple types are not supported for condition. */
5879 if (code
== COND_EXPR
)
5880 gcc_assert (ncopies
== 1);
5882 /* Create the destination vector */
5883 vec_dest
= vect_create_destination_var (scalar_dest
, vectype_out
);
5885 /* In case the vectorization factor (VF) is bigger than the number
5886 of elements that we can fit in a vectype (nunits), we have to generate
5887 more than one vector stmt - i.e - we need to "unroll" the
5888 vector stmt by a factor VF/nunits. For more details see documentation
5889 in vectorizable_operation. */
5891 /* If the reduction is used in an outer loop we need to generate
5892 VF intermediate results, like so (e.g. for ncopies=2):
5897 (i.e. we generate VF results in 2 registers).
5898 In this case we have a separate def-use cycle for each copy, and therefore
5899 for each copy we get the vector def for the reduction variable from the
5900 respective phi node created for this copy.
5902 Otherwise (the reduction is unused in the loop nest), we can combine
5903 together intermediate results, like so (e.g. for ncopies=2):
5907 (i.e. we generate VF/2 results in a single register).
5908 In this case for each copy we get the vector def for the reduction variable
5909 from the vectorized reduction operation generated in the previous iteration.
5912 if (STMT_VINFO_RELEVANT (stmt_info
) == vect_unused_in_scope
)
5914 single_defuse_cycle
= true;
5918 epilog_copies
= ncopies
;
5920 prev_stmt_info
= NULL
;
5921 prev_phi_info
= NULL
;
5923 vec_num
= SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node
);
5927 vec_oprnds0
.create (1);
5928 if (op_type
== ternary_op
)
5929 vec_oprnds1
.create (1);
5932 phis
.create (vec_num
);
5933 vect_defs
.create (vec_num
);
5935 vect_defs
.quick_push (NULL_TREE
);
5937 for (j
= 0; j
< ncopies
; j
++)
5939 if (j
== 0 || !single_defuse_cycle
)
5941 for (i
= 0; i
< vec_num
; i
++)
5943 /* Create the reduction-phi that defines the reduction
5945 new_phi
= create_phi_node (vec_dest
, loop
->header
);
5946 set_vinfo_for_stmt (new_phi
,
5947 new_stmt_vec_info (new_phi
, loop_vinfo
));
5948 if (j
== 0 || slp_node
)
5949 phis
.quick_push (new_phi
);
5953 if (code
== COND_EXPR
)
5955 gcc_assert (!slp_node
);
5956 vectorizable_condition (stmt
, gsi
, vec_stmt
,
5957 PHI_RESULT (phis
[0]),
5959 /* Multiple types are not supported for condition. */
5966 op0
= ops
[!reduc_index
];
5967 if (op_type
== ternary_op
)
5969 if (reduc_index
== 0)
5976 vect_get_vec_defs (op0
, op1
, stmt
, &vec_oprnds0
, &vec_oprnds1
,
5980 loop_vec_def0
= vect_get_vec_def_for_operand (ops
[!reduc_index
],
5982 vec_oprnds0
.quick_push (loop_vec_def0
);
5983 if (op_type
== ternary_op
)
5985 loop_vec_def1
= vect_get_vec_def_for_operand (op1
, stmt
);
5986 vec_oprnds1
.quick_push (loop_vec_def1
);
5994 enum vect_def_type dt
;
5997 vect_is_simple_use (ops
[!reduc_index
], loop_vinfo
,
5999 loop_vec_def0
= vect_get_vec_def_for_stmt_copy (dt
,
6001 vec_oprnds0
[0] = loop_vec_def0
;
6002 if (op_type
== ternary_op
)
6004 vect_is_simple_use (op1
, loop_vinfo
, &dummy_stmt
, &dt
);
6005 loop_vec_def1
= vect_get_vec_def_for_stmt_copy (dt
,
6007 vec_oprnds1
[0] = loop_vec_def1
;
6011 if (single_defuse_cycle
)
6012 reduc_def
= gimple_assign_lhs (new_stmt
);
6014 STMT_VINFO_RELATED_STMT (prev_phi_info
) = new_phi
;
6017 FOR_EACH_VEC_ELT (vec_oprnds0
, i
, def0
)
6020 reduc_def
= PHI_RESULT (phis
[i
]);
6023 if (!single_defuse_cycle
|| j
== 0)
6024 reduc_def
= PHI_RESULT (new_phi
);
6027 def1
= ((op_type
== ternary_op
)
6028 ? vec_oprnds1
[i
] : NULL
);
6029 if (op_type
== binary_op
)
6031 if (reduc_index
== 0)
6032 expr
= build2 (code
, vectype_out
, reduc_def
, def0
);
6034 expr
= build2 (code
, vectype_out
, def0
, reduc_def
);
6038 if (reduc_index
== 0)
6039 expr
= build3 (code
, vectype_out
, reduc_def
, def0
, def1
);
6042 if (reduc_index
== 1)
6043 expr
= build3 (code
, vectype_out
, def0
, reduc_def
, def1
);
6045 expr
= build3 (code
, vectype_out
, def0
, def1
, reduc_def
);
6049 new_stmt
= gimple_build_assign (vec_dest
, expr
);
6050 new_temp
= make_ssa_name (vec_dest
, new_stmt
);
6051 gimple_assign_set_lhs (new_stmt
, new_temp
);
6052 vect_finish_stmt_generation (stmt
, new_stmt
, gsi
);
6056 SLP_TREE_VEC_STMTS (slp_node
).quick_push (new_stmt
);
6057 vect_defs
.quick_push (new_temp
);
6060 vect_defs
[0] = new_temp
;
6067 STMT_VINFO_VEC_STMT (stmt_info
) = *vec_stmt
= new_stmt
;
6069 STMT_VINFO_RELATED_STMT (prev_stmt_info
) = new_stmt
;
6071 prev_stmt_info
= vinfo_for_stmt (new_stmt
);
6072 prev_phi_info
= vinfo_for_stmt (new_phi
);
6075 tree indx_before_incr
, indx_after_incr
, cond_name
= NULL
;
6077 /* Finalize the reduction-phi (set its arguments) and create the
6078 epilog reduction code. */
6079 if ((!single_defuse_cycle
|| code
== COND_EXPR
) && !slp_node
)
6081 new_temp
= gimple_assign_lhs (*vec_stmt
);
6082 vect_defs
[0] = new_temp
;
6084 /* For cond reductions we want to create a new vector (INDEX_COND_EXPR)
6085 which is updated with the current index of the loop for every match of
6086 the original loop's cond_expr (VEC_STMT). This results in a vector
6087 containing the last time the condition passed for that vector lane.
6088 The first match will be a 1 to allow 0 to be used for non-matching
6089 indexes. If there are no matches at all then the vector will be all
6091 if (STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info
) == COND_REDUCTION
)
6093 int nunits_out
= TYPE_VECTOR_SUBPARTS (vectype_out
);
6096 gcc_assert (gimple_assign_rhs_code (*vec_stmt
) == VEC_COND_EXPR
);
6098 /* First we create a simple vector induction variable which starts
6099 with the values {1,2,3,...} (SERIES_VECT) and increments by the
6100 vector size (STEP). */
6102 /* Create a {1,2,3,...} vector. */
6103 tree
*vtemp
= XALLOCAVEC (tree
, nunits_out
);
6104 for (k
= 0; k
< nunits_out
; ++k
)
6105 vtemp
[k
] = build_int_cst (cr_index_scalar_type
, k
+ 1);
6106 tree series_vect
= build_vector (cr_index_vector_type
, vtemp
);
6108 /* Create a vector of the step value. */
6109 tree step
= build_int_cst (cr_index_scalar_type
, nunits_out
);
6110 tree vec_step
= build_vector_from_val (cr_index_vector_type
, step
);
6112 /* Create an induction variable. */
6113 gimple_stmt_iterator incr_gsi
;
6115 standard_iv_increment_position (loop
, &incr_gsi
, &insert_after
);
6116 create_iv (series_vect
, vec_step
, NULL_TREE
, loop
, &incr_gsi
,
6117 insert_after
, &indx_before_incr
, &indx_after_incr
);
6119 /* Next create a new phi node vector (NEW_PHI_TREE) which starts
6120 filled with zeros (VEC_ZERO). */
6122 /* Create a vector of 0s. */
6123 tree zero
= build_zero_cst (cr_index_scalar_type
);
6124 tree vec_zero
= build_vector_from_val (cr_index_vector_type
, zero
);
6126 /* Create a vector phi node. */
6127 tree new_phi_tree
= make_ssa_name (cr_index_vector_type
);
6128 new_phi
= create_phi_node (new_phi_tree
, loop
->header
);
6129 set_vinfo_for_stmt (new_phi
,
6130 new_stmt_vec_info (new_phi
, loop_vinfo
));
6131 add_phi_arg (new_phi
, vec_zero
, loop_preheader_edge (loop
),
6134 /* Now take the condition from the loops original cond_expr
6135 (VEC_STMT) and produce a new cond_expr (INDEX_COND_EXPR) which for
6136 every match uses values from the induction variable
6137 (INDEX_BEFORE_INCR) otherwise uses values from the phi node
6139 Finally, we update the phi (NEW_PHI_TREE) to take the value of
6140 the new cond_expr (INDEX_COND_EXPR). */
6142 /* Turn the condition from vec_stmt into an ssa name. */
6143 gimple_stmt_iterator vec_stmt_gsi
= gsi_for_stmt (*vec_stmt
);
6144 tree ccompare
= gimple_assign_rhs1 (*vec_stmt
);
6145 tree ccompare_name
= make_ssa_name (TREE_TYPE (ccompare
));
6146 gimple
*ccompare_stmt
= gimple_build_assign (ccompare_name
,
6148 gsi_insert_before (&vec_stmt_gsi
, ccompare_stmt
, GSI_SAME_STMT
);
6149 gimple_assign_set_rhs1 (*vec_stmt
, ccompare_name
);
6150 update_stmt (*vec_stmt
);
6152 /* Create a conditional, where the condition is taken from vec_stmt
6153 (CCOMPARE_NAME), then is the induction index (INDEX_BEFORE_INCR)
6154 and else is the phi (NEW_PHI_TREE). */
6155 tree index_cond_expr
= build3 (VEC_COND_EXPR
, cr_index_vector_type
,
6156 ccompare_name
, indx_before_incr
,
6158 cond_name
= make_ssa_name (cr_index_vector_type
);
6159 gimple
*index_condition
= gimple_build_assign (cond_name
,
6161 gsi_insert_before (&incr_gsi
, index_condition
, GSI_SAME_STMT
);
6162 stmt_vec_info index_vec_info
= new_stmt_vec_info (index_condition
,
6164 STMT_VINFO_VECTYPE (index_vec_info
) = cr_index_vector_type
;
6165 set_vinfo_for_stmt (index_condition
, index_vec_info
);
6167 /* Update the phi with the vec cond. */
6168 add_phi_arg (new_phi
, cond_name
, loop_latch_edge (loop
),
6173 vect_create_epilog_for_reduction (vect_defs
, stmt
, epilog_copies
,
6174 epilog_reduc_code
, phis
, reduc_index
,
6175 double_reduc
, slp_node
, cond_name
);
6180 /* Function vect_min_worthwhile_factor.
6182 For a loop where we could vectorize the operation indicated by CODE,
6183 return the minimum vectorization factor that makes it worthwhile
6184 to use generic vectors. */
6186 vect_min_worthwhile_factor (enum tree_code code
)
6207 /* Function vectorizable_induction
6209 Check if PHI performs an induction computation that can be vectorized.
6210 If VEC_STMT is also passed, vectorize the induction PHI: create a vectorized
6211 phi to replace it, put it in VEC_STMT, and add it to the same basic block.
6212 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
6215 vectorizable_induction (gimple
*phi
,
6216 gimple_stmt_iterator
*gsi ATTRIBUTE_UNUSED
,
6219 stmt_vec_info stmt_info
= vinfo_for_stmt (phi
);
6220 tree vectype
= STMT_VINFO_VECTYPE (stmt_info
);
6221 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_info
);
6222 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
6223 int nunits
= TYPE_VECTOR_SUBPARTS (vectype
);
6224 int ncopies
= LOOP_VINFO_VECT_FACTOR (loop_vinfo
) / nunits
;
6227 gcc_assert (ncopies
>= 1);
6228 /* FORNOW. These restrictions should be relaxed. */
6229 if (nested_in_vect_loop_p (loop
, phi
))
6231 imm_use_iterator imm_iter
;
6232 use_operand_p use_p
;
6239 if (dump_enabled_p ())
6240 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
6241 "multiple types in nested loop.\n");
6246 latch_e
= loop_latch_edge (loop
->inner
);
6247 loop_arg
= PHI_ARG_DEF_FROM_EDGE (phi
, latch_e
);
6248 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, loop_arg
)
6250 gimple
*use_stmt
= USE_STMT (use_p
);
6251 if (is_gimple_debug (use_stmt
))
6254 if (!flow_bb_inside_loop_p (loop
->inner
, gimple_bb (use_stmt
)))
6256 exit_phi
= use_stmt
;
6262 stmt_vec_info exit_phi_vinfo
= vinfo_for_stmt (exit_phi
);
6263 if (!(STMT_VINFO_RELEVANT_P (exit_phi_vinfo
)
6264 && !STMT_VINFO_LIVE_P (exit_phi_vinfo
)))
6266 if (dump_enabled_p ())
6267 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
6268 "inner-loop induction only used outside "
6269 "of the outer vectorized loop.\n");
6275 if (!STMT_VINFO_RELEVANT_P (stmt_info
))
6278 /* FORNOW: SLP not supported. */
6279 if (STMT_SLP_TYPE (stmt_info
))
6282 gcc_assert (STMT_VINFO_DEF_TYPE (stmt_info
) == vect_induction_def
);
6284 if (gimple_code (phi
) != GIMPLE_PHI
)
6287 if (!vec_stmt
) /* transformation not required. */
6289 STMT_VINFO_TYPE (stmt_info
) = induc_vec_info_type
;
6290 if (dump_enabled_p ())
6291 dump_printf_loc (MSG_NOTE
, vect_location
,
6292 "=== vectorizable_induction ===\n");
6293 vect_model_induction_cost (stmt_info
, ncopies
);
6299 if (dump_enabled_p ())
6300 dump_printf_loc (MSG_NOTE
, vect_location
, "transform induction phi.\n");
6302 vec_def
= get_initial_def_for_induction (phi
);
6303 *vec_stmt
= SSA_NAME_DEF_STMT (vec_def
);
6307 /* Function vectorizable_live_operation.
6309 STMT computes a value that is used outside the loop. Check if
6310 it can be supported. */
6313 vectorizable_live_operation (gimple
*stmt
,
6314 gimple_stmt_iterator
*gsi ATTRIBUTE_UNUSED
,
6317 stmt_vec_info stmt_info
= vinfo_for_stmt (stmt
);
6318 loop_vec_info loop_vinfo
= STMT_VINFO_LOOP_VINFO (stmt_info
);
6319 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
6324 gcc_assert (STMT_VINFO_LIVE_P (stmt_info
));
6326 if (STMT_VINFO_DEF_TYPE (stmt_info
) == vect_reduction_def
)
6329 if (!is_gimple_assign (stmt
))
6331 if (gimple_call_internal_p (stmt
)
6332 && gimple_call_internal_fn (stmt
) == IFN_GOMP_SIMD_LANE
6333 && gimple_call_lhs (stmt
)
6335 && TREE_CODE (gimple_call_arg (stmt
, 0)) == SSA_NAME
6337 == SSA_NAME_VAR (gimple_call_arg (stmt
, 0)))
6339 edge e
= single_exit (loop
);
6340 basic_block merge_bb
= e
->dest
;
6341 imm_use_iterator imm_iter
;
6342 use_operand_p use_p
;
6343 tree lhs
= gimple_call_lhs (stmt
);
6345 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, lhs
)
6347 gimple
*use_stmt
= USE_STMT (use_p
);
6348 if (gimple_code (use_stmt
) == GIMPLE_PHI
6349 && gimple_bb (use_stmt
) == merge_bb
)
6354 = build_int_cst (unsigned_type_node
,
6355 loop_vinfo
->vectorization_factor
- 1);
6356 SET_PHI_ARG_DEF (use_stmt
, e
->dest_idx
, vfm1
);
6366 if (TREE_CODE (gimple_assign_lhs (stmt
)) != SSA_NAME
)
6369 /* FORNOW. CHECKME. */
6370 if (nested_in_vect_loop_p (loop
, stmt
))
6373 /* FORNOW: support only if all uses are invariant. This means
6374 that the scalar operations can remain in place, unvectorized.
6375 The original last scalar value that they compute will be used. */
6376 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, iter
, SSA_OP_USE
)
6378 enum vect_def_type dt
= vect_uninitialized_def
;
6380 if (!vect_is_simple_use (op
, loop_vinfo
, &def_stmt
, &dt
))
6382 if (dump_enabled_p ())
6383 dump_printf_loc (MSG_MISSED_OPTIMIZATION
, vect_location
,
6384 "use not simple.\n");
6388 if (dt
!= vect_external_def
&& dt
!= vect_constant_def
)
6392 /* No transformation is required for the cases we currently support. */
6396 /* Kill any debug uses outside LOOP of SSA names defined in STMT. */
6399 vect_loop_kill_debug_uses (struct loop
*loop
, gimple
*stmt
)
6401 ssa_op_iter op_iter
;
6402 imm_use_iterator imm_iter
;
6403 def_operand_p def_p
;
6406 FOR_EACH_PHI_OR_STMT_DEF (def_p
, stmt
, op_iter
, SSA_OP_DEF
)
6408 FOR_EACH_IMM_USE_STMT (ustmt
, imm_iter
, DEF_FROM_PTR (def_p
))
6412 if (!is_gimple_debug (ustmt
))
6415 bb
= gimple_bb (ustmt
);
6417 if (!flow_bb_inside_loop_p (loop
, bb
))
6419 if (gimple_debug_bind_p (ustmt
))
6421 if (dump_enabled_p ())
6422 dump_printf_loc (MSG_NOTE
, vect_location
,
6423 "killing debug use\n");
6425 gimple_debug_bind_reset_value (ustmt
);
6426 update_stmt (ustmt
);
6436 /* This function builds ni_name = number of iterations. Statements
6437 are emitted on the loop preheader edge. */
6440 vect_build_loop_niters (loop_vec_info loop_vinfo
)
6442 tree ni
= unshare_expr (LOOP_VINFO_NITERS (loop_vinfo
));
6443 if (TREE_CODE (ni
) == INTEGER_CST
)
6448 gimple_seq stmts
= NULL
;
6449 edge pe
= loop_preheader_edge (LOOP_VINFO_LOOP (loop_vinfo
));
6451 var
= create_tmp_var (TREE_TYPE (ni
), "niters");
6452 ni_name
= force_gimple_operand (ni
, &stmts
, false, var
);
6454 gsi_insert_seq_on_edge_immediate (pe
, stmts
);
6461 /* This function generates the following statements:
6463 ni_name = number of iterations loop executes
6464 ratio = ni_name / vf
6465 ratio_mult_vf_name = ratio * vf
6467 and places them on the loop preheader edge. */
6470 vect_generate_tmps_on_preheader (loop_vec_info loop_vinfo
,
6472 tree
*ratio_mult_vf_name_ptr
,
6473 tree
*ratio_name_ptr
)
6475 tree ni_minus_gap_name
;
6478 tree ratio_mult_vf_name
;
6479 int vf
= LOOP_VINFO_VECT_FACTOR (loop_vinfo
);
6480 edge pe
= loop_preheader_edge (LOOP_VINFO_LOOP (loop_vinfo
));
6483 log_vf
= build_int_cst (TREE_TYPE (ni_name
), exact_log2 (vf
));
6485 /* If epilogue loop is required because of data accesses with gaps, we
6486 subtract one iteration from the total number of iterations here for
6487 correct calculation of RATIO. */
6488 if (LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo
))
6490 ni_minus_gap_name
= fold_build2 (MINUS_EXPR
, TREE_TYPE (ni_name
),
6492 build_one_cst (TREE_TYPE (ni_name
)));
6493 if (!is_gimple_val (ni_minus_gap_name
))
6495 var
= create_tmp_var (TREE_TYPE (ni_name
), "ni_gap");
6496 gimple
*stmts
= NULL
;
6497 ni_minus_gap_name
= force_gimple_operand (ni_minus_gap_name
, &stmts
,
6499 gsi_insert_seq_on_edge_immediate (pe
, stmts
);
6503 ni_minus_gap_name
= ni_name
;
6505 /* Create: ratio = ni >> log2(vf) */
6506 /* ??? As we have ni == number of latch executions + 1, ni could
6507 have overflown to zero. So avoid computing ratio based on ni
6508 but compute it using the fact that we know ratio will be at least
6509 one, thus via (ni - vf) >> log2(vf) + 1. */
6511 = fold_build2 (PLUS_EXPR
, TREE_TYPE (ni_name
),
6512 fold_build2 (RSHIFT_EXPR
, TREE_TYPE (ni_name
),
6513 fold_build2 (MINUS_EXPR
, TREE_TYPE (ni_name
),
6516 (TREE_TYPE (ni_name
), vf
)),
6518 build_int_cst (TREE_TYPE (ni_name
), 1));
6519 if (!is_gimple_val (ratio_name
))
6521 var
= create_tmp_var (TREE_TYPE (ni_name
), "bnd");
6522 gimple
*stmts
= NULL
;
6523 ratio_name
= force_gimple_operand (ratio_name
, &stmts
, true, var
);
6524 gsi_insert_seq_on_edge_immediate (pe
, stmts
);
6526 *ratio_name_ptr
= ratio_name
;
6528 /* Create: ratio_mult_vf = ratio << log2 (vf). */
6530 if (ratio_mult_vf_name_ptr
)
6532 ratio_mult_vf_name
= fold_build2 (LSHIFT_EXPR
, TREE_TYPE (ratio_name
),
6533 ratio_name
, log_vf
);
6534 if (!is_gimple_val (ratio_mult_vf_name
))
6536 var
= create_tmp_var (TREE_TYPE (ni_name
), "ratio_mult_vf");
6537 gimple
*stmts
= NULL
;
6538 ratio_mult_vf_name
= force_gimple_operand (ratio_mult_vf_name
, &stmts
,
6540 gsi_insert_seq_on_edge_immediate (pe
, stmts
);
6542 *ratio_mult_vf_name_ptr
= ratio_mult_vf_name
;
6549 /* Function vect_transform_loop.
6551 The analysis phase has determined that the loop is vectorizable.
6552 Vectorize the loop - created vectorized stmts to replace the scalar
6553 stmts in the loop, and update the loop exit condition. */
6556 vect_transform_loop (loop_vec_info loop_vinfo
)
6558 struct loop
*loop
= LOOP_VINFO_LOOP (loop_vinfo
);
6559 basic_block
*bbs
= LOOP_VINFO_BBS (loop_vinfo
);
6560 int nbbs
= loop
->num_nodes
;
6563 int vectorization_factor
= LOOP_VINFO_VECT_FACTOR (loop_vinfo
);
6565 bool slp_scheduled
= false;
6566 gimple
*stmt
, *pattern_stmt
;
6567 gimple_seq pattern_def_seq
= NULL
;
6568 gimple_stmt_iterator pattern_def_si
= gsi_none ();
6569 bool transform_pattern_stmt
= false;
6570 bool check_profitability
= false;
6572 /* Record number of iterations before we started tampering with the profile. */
6573 gcov_type expected_iterations
= expected_loop_iterations_unbounded (loop
);
6575 if (dump_enabled_p ())
6576 dump_printf_loc (MSG_NOTE
, vect_location
, "=== vec_transform_loop ===\n");
6578 /* If profile is inprecise, we have chance to fix it up. */
6579 if (LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo
))
6580 expected_iterations
= LOOP_VINFO_INT_NITERS (loop_vinfo
);
6582 /* Use the more conservative vectorization threshold. If the number
6583 of iterations is constant assume the cost check has been performed
6584 by our caller. If the threshold makes all loops profitable that
6585 run at least the vectorization factor number of times checking
6586 is pointless, too. */
6587 th
= LOOP_VINFO_COST_MODEL_THRESHOLD (loop_vinfo
);
6588 if (th
>= LOOP_VINFO_VECT_FACTOR (loop_vinfo
) - 1
6589 && !LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo
))
6591 if (dump_enabled_p ())
6592 dump_printf_loc (MSG_NOTE
, vect_location
,
6593 "Profitability threshold is %d loop iterations.\n",
6595 check_profitability
= true;
6598 /* Version the loop first, if required, so the profitability check
6601 if (LOOP_REQUIRES_VERSIONING_FOR_ALIGNMENT (loop_vinfo
)
6602 || LOOP_REQUIRES_VERSIONING_FOR_ALIAS (loop_vinfo
))
6604 vect_loop_versioning (loop_vinfo
, th
, check_profitability
);
6605 check_profitability
= false;
6608 tree ni_name
= vect_build_loop_niters (loop_vinfo
);
6609 LOOP_VINFO_NITERS_UNCHANGED (loop_vinfo
) = ni_name
;
6611 /* Peel the loop if there are data refs with unknown alignment.
6612 Only one data ref with unknown store is allowed. */
6614 if (LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo
))
6616 vect_do_peeling_for_alignment (loop_vinfo
, ni_name
,
6617 th
, check_profitability
);
6618 check_profitability
= false;
6619 /* The above adjusts LOOP_VINFO_NITERS, so cause ni_name to
6621 ni_name
= NULL_TREE
;
6624 /* If the loop has a symbolic number of iterations 'n' (i.e. it's not a
6625 compile time constant), or it is a constant that doesn't divide by the
6626 vectorization factor, then an epilog loop needs to be created.
6627 We therefore duplicate the loop: the original loop will be vectorized,
6628 and will compute the first (n/VF) iterations. The second copy of the loop
6629 will remain scalar and will compute the remaining (n%VF) iterations.
6630 (VF is the vectorization factor). */
6632 if (LOOP_VINFO_PEELING_FOR_NITER (loop_vinfo
)
6633 || LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo
))
6637 ni_name
= vect_build_loop_niters (loop_vinfo
);
6638 vect_generate_tmps_on_preheader (loop_vinfo
, ni_name
, &ratio_mult_vf
,
6640 vect_do_peeling_for_loop_bound (loop_vinfo
, ni_name
, ratio_mult_vf
,
6641 th
, check_profitability
);
6643 else if (LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo
))
6644 ratio
= build_int_cst (TREE_TYPE (LOOP_VINFO_NITERS (loop_vinfo
)),
6645 LOOP_VINFO_INT_NITERS (loop_vinfo
) / vectorization_factor
);
6649 ni_name
= vect_build_loop_niters (loop_vinfo
);
6650 vect_generate_tmps_on_preheader (loop_vinfo
, ni_name
, NULL
, &ratio
);
6653 /* 1) Make sure the loop header has exactly two entries
6654 2) Make sure we have a preheader basic block. */
6656 gcc_assert (EDGE_COUNT (loop
->header
->preds
) == 2);
6658 split_edge (loop_preheader_edge (loop
));
6660 /* FORNOW: the vectorizer supports only loops which body consist
6661 of one basic block (header + empty latch). When the vectorizer will
6662 support more involved loop forms, the order by which the BBs are
6663 traversed need to be reconsidered. */
6665 for (i
= 0; i
< nbbs
; i
++)
6667 basic_block bb
= bbs
[i
];
6668 stmt_vec_info stmt_info
;
6670 for (gphi_iterator si
= gsi_start_phis (bb
); !gsi_end_p (si
);
6673 gphi
*phi
= si
.phi ();
6674 if (dump_enabled_p ())
6676 dump_printf_loc (MSG_NOTE
, vect_location
,
6677 "------>vectorizing phi: ");
6678 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
, phi
, 0);
6679 dump_printf (MSG_NOTE
, "\n");
6681 stmt_info
= vinfo_for_stmt (phi
);
6685 if (MAY_HAVE_DEBUG_STMTS
&& !STMT_VINFO_LIVE_P (stmt_info
))
6686 vect_loop_kill_debug_uses (loop
, phi
);
6688 if (!STMT_VINFO_RELEVANT_P (stmt_info
)
6689 && !STMT_VINFO_LIVE_P (stmt_info
))
6692 if (STMT_VINFO_VECTYPE (stmt_info
)
6693 && (TYPE_VECTOR_SUBPARTS (STMT_VINFO_VECTYPE (stmt_info
))
6694 != (unsigned HOST_WIDE_INT
) vectorization_factor
)
6695 && dump_enabled_p ())
6696 dump_printf_loc (MSG_NOTE
, vect_location
, "multiple-types.\n");
6698 if (STMT_VINFO_DEF_TYPE (stmt_info
) == vect_induction_def
)
6700 if (dump_enabled_p ())
6701 dump_printf_loc (MSG_NOTE
, vect_location
, "transform phi.\n");
6702 vect_transform_stmt (phi
, NULL
, NULL
, NULL
, NULL
);
6706 pattern_stmt
= NULL
;
6707 for (gimple_stmt_iterator si
= gsi_start_bb (bb
);
6708 !gsi_end_p (si
) || transform_pattern_stmt
;)
6712 if (transform_pattern_stmt
)
6713 stmt
= pattern_stmt
;
6716 stmt
= gsi_stmt (si
);
6717 /* During vectorization remove existing clobber stmts. */
6718 if (gimple_clobber_p (stmt
))
6720 unlink_stmt_vdef (stmt
);
6721 gsi_remove (&si
, true);
6722 release_defs (stmt
);
6727 if (dump_enabled_p ())
6729 dump_printf_loc (MSG_NOTE
, vect_location
,
6730 "------>vectorizing statement: ");
6731 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
, stmt
, 0);
6732 dump_printf (MSG_NOTE
, "\n");
6735 stmt_info
= vinfo_for_stmt (stmt
);
6737 /* vector stmts created in the outer-loop during vectorization of
6738 stmts in an inner-loop may not have a stmt_info, and do not
6739 need to be vectorized. */
6746 if (MAY_HAVE_DEBUG_STMTS
&& !STMT_VINFO_LIVE_P (stmt_info
))
6747 vect_loop_kill_debug_uses (loop
, stmt
);
6749 if (!STMT_VINFO_RELEVANT_P (stmt_info
)
6750 && !STMT_VINFO_LIVE_P (stmt_info
))
6752 if (STMT_VINFO_IN_PATTERN_P (stmt_info
)
6753 && (pattern_stmt
= STMT_VINFO_RELATED_STMT (stmt_info
))
6754 && (STMT_VINFO_RELEVANT_P (vinfo_for_stmt (pattern_stmt
))
6755 || STMT_VINFO_LIVE_P (vinfo_for_stmt (pattern_stmt
))))
6757 stmt
= pattern_stmt
;
6758 stmt_info
= vinfo_for_stmt (stmt
);
6766 else if (STMT_VINFO_IN_PATTERN_P (stmt_info
)
6767 && (pattern_stmt
= STMT_VINFO_RELATED_STMT (stmt_info
))
6768 && (STMT_VINFO_RELEVANT_P (vinfo_for_stmt (pattern_stmt
))
6769 || STMT_VINFO_LIVE_P (vinfo_for_stmt (pattern_stmt
))))
6770 transform_pattern_stmt
= true;
6772 /* If pattern statement has def stmts, vectorize them too. */
6773 if (is_pattern_stmt_p (stmt_info
))
6775 if (pattern_def_seq
== NULL
)
6777 pattern_def_seq
= STMT_VINFO_PATTERN_DEF_SEQ (stmt_info
);
6778 pattern_def_si
= gsi_start (pattern_def_seq
);
6780 else if (!gsi_end_p (pattern_def_si
))
6781 gsi_next (&pattern_def_si
);
6782 if (pattern_def_seq
!= NULL
)
6784 gimple
*pattern_def_stmt
= NULL
;
6785 stmt_vec_info pattern_def_stmt_info
= NULL
;
6787 while (!gsi_end_p (pattern_def_si
))
6789 pattern_def_stmt
= gsi_stmt (pattern_def_si
);
6790 pattern_def_stmt_info
6791 = vinfo_for_stmt (pattern_def_stmt
);
6792 if (STMT_VINFO_RELEVANT_P (pattern_def_stmt_info
)
6793 || STMT_VINFO_LIVE_P (pattern_def_stmt_info
))
6795 gsi_next (&pattern_def_si
);
6798 if (!gsi_end_p (pattern_def_si
))
6800 if (dump_enabled_p ())
6802 dump_printf_loc (MSG_NOTE
, vect_location
,
6803 "==> vectorizing pattern def "
6805 dump_gimple_stmt (MSG_NOTE
, TDF_SLIM
,
6806 pattern_def_stmt
, 0);
6807 dump_printf (MSG_NOTE
, "\n");
6810 stmt
= pattern_def_stmt
;
6811 stmt_info
= pattern_def_stmt_info
;
6815 pattern_def_si
= gsi_none ();
6816 transform_pattern_stmt
= false;
6820 transform_pattern_stmt
= false;
6823 if (STMT_VINFO_VECTYPE (stmt_info
))
6827 TYPE_VECTOR_SUBPARTS (STMT_VINFO_VECTYPE (stmt_info
));
6828 if (!STMT_SLP_TYPE (stmt_info
)
6829 && nunits
!= (unsigned int) vectorization_factor
6830 && dump_enabled_p ())
6831 /* For SLP VF is set according to unrolling factor, and not
6832 to vector size, hence for SLP this print is not valid. */
6833 dump_printf_loc (MSG_NOTE
, vect_location
, "multiple-types.\n");
6836 /* SLP. Schedule all the SLP instances when the first SLP stmt is
6838 if (STMT_SLP_TYPE (stmt_info
))
6842 slp_scheduled
= true;
6844 if (dump_enabled_p ())
6845 dump_printf_loc (MSG_NOTE
, vect_location
,
6846 "=== scheduling SLP instances ===\n");
6848 vect_schedule_slp (loop_vinfo
);
6851 /* Hybrid SLP stmts must be vectorized in addition to SLP. */
6852 if (!vinfo_for_stmt (stmt
) || PURE_SLP_STMT (stmt_info
))
6854 if (!transform_pattern_stmt
&& gsi_end_p (pattern_def_si
))
6856 pattern_def_seq
= NULL
;
6863 /* -------- vectorize statement ------------ */
6864 if (dump_enabled_p ())
6865 dump_printf_loc (MSG_NOTE
, vect_location
, "transform statement.\n");
6867 grouped_store
= false;
6868 is_store
= vect_transform_stmt (stmt
, &si
, &grouped_store
, NULL
, NULL
);
6871 if (STMT_VINFO_GROUPED_ACCESS (stmt_info
))
6873 /* Interleaving. If IS_STORE is TRUE, the vectorization of the
6874 interleaving chain was completed - free all the stores in
6877 vect_remove_stores (GROUP_FIRST_ELEMENT (stmt_info
));
6881 /* Free the attached stmt_vec_info and remove the stmt. */
6882 gimple
*store
= gsi_stmt (si
);
6883 free_stmt_vec_info (store
);
6884 unlink_stmt_vdef (store
);
6885 gsi_remove (&si
, true);
6886 release_defs (store
);
6889 /* Stores can only appear at the end of pattern statements. */
6890 gcc_assert (!transform_pattern_stmt
);
6891 pattern_def_seq
= NULL
;
6893 else if (!transform_pattern_stmt
&& gsi_end_p (pattern_def_si
))
6895 pattern_def_seq
= NULL
;
6901 slpeel_make_loop_iterate_ntimes (loop
, ratio
);
6903 /* Reduce loop iterations by the vectorization factor. */
6904 scale_loop_profile (loop
, GCOV_COMPUTE_SCALE (1, vectorization_factor
),
6905 expected_iterations
/ vectorization_factor
);
6906 loop
->nb_iterations_upper_bound
6907 = wi::udiv_floor (loop
->nb_iterations_upper_bound
, vectorization_factor
);
6908 if (LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo
)
6909 && loop
->nb_iterations_upper_bound
!= 0)
6910 loop
->nb_iterations_upper_bound
= loop
->nb_iterations_upper_bound
- 1;
6911 if (loop
->any_estimate
)
6913 loop
->nb_iterations_estimate
6914 = wi::udiv_floor (loop
->nb_iterations_estimate
, vectorization_factor
);
6915 if (LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo
)
6916 && loop
->nb_iterations_estimate
!= 0)
6917 loop
->nb_iterations_estimate
= loop
->nb_iterations_estimate
- 1;
6920 if (dump_enabled_p ())
6922 dump_printf_loc (MSG_NOTE
, vect_location
,
6923 "LOOP VECTORIZED\n");
6925 dump_printf_loc (MSG_NOTE
, vect_location
,
6926 "OUTER LOOP VECTORIZED\n");
6927 dump_printf (MSG_NOTE
, "\n");