[gcc]
[official-gcc.git] / gcc / tree-vect-loop.c
bloba0558360a96dc7c3554c4554c9a1e33c6037285d
1 /* Loop Vectorization
2 Copyright (C) 2003-2017 Free Software Foundation, Inc.
3 Contributed by Dorit Naishlos <dorit@il.ibm.com> and
4 Ira Rosen <irar@il.ibm.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
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
16 for more details.
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/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "backend.h"
26 #include "target.h"
27 #include "rtl.h"
28 #include "tree.h"
29 #include "gimple.h"
30 #include "cfghooks.h"
31 #include "tree-pass.h"
32 #include "ssa.h"
33 #include "optabs-tree.h"
34 #include "diagnostic-core.h"
35 #include "fold-const.h"
36 #include "stor-layout.h"
37 #include "cfganal.h"
38 #include "gimplify.h"
39 #include "gimple-iterator.h"
40 #include "gimplify-me.h"
41 #include "tree-ssa-loop-ivopts.h"
42 #include "tree-ssa-loop-manip.h"
43 #include "tree-ssa-loop-niter.h"
44 #include "tree-ssa-loop.h"
45 #include "cfgloop.h"
46 #include "params.h"
47 #include "tree-scalar-evolution.h"
48 #include "tree-vectorizer.h"
49 #include "gimple-fold.h"
50 #include "cgraph.h"
51 #include "tree-cfg.h"
52 #include "tree-if-conv.h"
54 /* Loop Vectorization Pass.
56 This pass tries to vectorize loops.
58 For example, the vectorizer transforms the following simple loop:
60 short a[N]; short b[N]; short c[N]; int i;
62 for (i=0; i<N; i++){
63 a[i] = b[i] + c[i];
66 as if it was manually vectorized by rewriting the source code into:
68 typedef int __attribute__((mode(V8HI))) v8hi;
69 short a[N]; short b[N]; short c[N]; int i;
70 v8hi *pa = (v8hi*)a, *pb = (v8hi*)b, *pc = (v8hi*)c;
71 v8hi va, vb, vc;
73 for (i=0; i<N/8; i++){
74 vb = pb[i];
75 vc = pc[i];
76 va = vb + vc;
77 pa[i] = va;
80 The main entry to this pass is vectorize_loops(), in which
81 the vectorizer applies a set of analyses on a given set of loops,
82 followed by the actual vectorization transformation for the loops that
83 had successfully passed the analysis phase.
84 Throughout this pass we make a distinction between two types of
85 data: scalars (which are represented by SSA_NAMES), and memory references
86 ("data-refs"). These two types of data require different handling both
87 during analysis and transformation. The types of data-refs that the
88 vectorizer currently supports are ARRAY_REFS which base is an array DECL
89 (not a pointer), and INDIRECT_REFS through pointers; both array and pointer
90 accesses are required to have a simple (consecutive) access pattern.
92 Analysis phase:
93 ===============
94 The driver for the analysis phase is vect_analyze_loop().
95 It applies a set of analyses, some of which rely on the scalar evolution
96 analyzer (scev) developed by Sebastian Pop.
98 During the analysis phase the vectorizer records some information
99 per stmt in a "stmt_vec_info" struct which is attached to each stmt in the
100 loop, as well as general information about the loop as a whole, which is
101 recorded in a "loop_vec_info" struct attached to each loop.
103 Transformation phase:
104 =====================
105 The loop transformation phase scans all the stmts in the loop, and
106 creates a vector stmt (or a sequence of stmts) for each scalar stmt S in
107 the loop that needs to be vectorized. It inserts the vector code sequence
108 just before the scalar stmt S, and records a pointer to the vector code
109 in STMT_VINFO_VEC_STMT (stmt_info) (stmt_info is the stmt_vec_info struct
110 attached to S). This pointer will be used for the vectorization of following
111 stmts which use the def of stmt S. Stmt S is removed if it writes to memory;
112 otherwise, we rely on dead code elimination for removing it.
114 For example, say stmt S1 was vectorized into stmt VS1:
116 VS1: vb = px[i];
117 S1: b = x[i]; STMT_VINFO_VEC_STMT (stmt_info (S1)) = VS1
118 S2: a = b;
120 To vectorize stmt S2, the vectorizer first finds the stmt that defines
121 the operand 'b' (S1), and gets the relevant vector def 'vb' from the
122 vector stmt VS1 pointed to by STMT_VINFO_VEC_STMT (stmt_info (S1)). The
123 resulting sequence would be:
125 VS1: vb = px[i];
126 S1: b = x[i]; STMT_VINFO_VEC_STMT (stmt_info (S1)) = VS1
127 VS2: va = vb;
128 S2: a = b; STMT_VINFO_VEC_STMT (stmt_info (S2)) = VS2
130 Operands that are not SSA_NAMEs, are data-refs that appear in
131 load/store operations (like 'x[i]' in S1), and are handled differently.
133 Target modeling:
134 =================
135 Currently the only target specific information that is used is the
136 size of the vector (in bytes) - "TARGET_VECTORIZE_UNITS_PER_SIMD_WORD".
137 Targets that can support different sizes of vectors, for now will need
138 to specify one value for "TARGET_VECTORIZE_UNITS_PER_SIMD_WORD". More
139 flexibility will be added in the future.
141 Since we only vectorize operations which vector form can be
142 expressed using existing tree codes, to verify that an operation is
143 supported, the vectorizer checks the relevant optab at the relevant
144 machine_mode (e.g, optab_handler (add_optab, V8HImode)). If
145 the value found is CODE_FOR_nothing, then there's no target support, and
146 we can't vectorize the stmt.
148 For additional information on this project see:
149 http://gcc.gnu.org/projects/tree-ssa/vectorization.html
152 static void vect_estimate_min_profitable_iters (loop_vec_info, int *, int *);
154 /* Function vect_determine_vectorization_factor
156 Determine the vectorization factor (VF). VF is the number of data elements
157 that are operated upon in parallel in a single iteration of the vectorized
158 loop. For example, when vectorizing a loop that operates on 4byte elements,
159 on a target with vector size (VS) 16byte, the VF is set to 4, since 4
160 elements can fit in a single vector register.
162 We currently support vectorization of loops in which all types operated upon
163 are of the same size. Therefore this function currently sets VF according to
164 the size of the types operated upon, and fails if there are multiple sizes
165 in the loop.
167 VF is also the factor by which the loop iterations are strip-mined, e.g.:
168 original loop:
169 for (i=0; i<N; i++){
170 a[i] = b[i] + c[i];
173 vectorized loop:
174 for (i=0; i<N; i+=VF){
175 a[i:VF] = b[i:VF] + c[i:VF];
179 static bool
180 vect_determine_vectorization_factor (loop_vec_info loop_vinfo)
182 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
183 basic_block *bbs = LOOP_VINFO_BBS (loop_vinfo);
184 unsigned nbbs = loop->num_nodes;
185 unsigned int vectorization_factor = 0;
186 tree scalar_type = NULL_TREE;
187 gphi *phi;
188 tree vectype;
189 unsigned int nunits;
190 stmt_vec_info stmt_info;
191 unsigned i;
192 HOST_WIDE_INT dummy;
193 gimple *stmt, *pattern_stmt = NULL;
194 gimple_seq pattern_def_seq = NULL;
195 gimple_stmt_iterator pattern_def_si = gsi_none ();
196 bool analyze_pattern_stmt = false;
197 bool bool_result;
198 auto_vec<stmt_vec_info> mask_producers;
200 if (dump_enabled_p ())
201 dump_printf_loc (MSG_NOTE, vect_location,
202 "=== vect_determine_vectorization_factor ===\n");
204 for (i = 0; i < nbbs; i++)
206 basic_block bb = bbs[i];
208 for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
209 gsi_next (&si))
211 phi = si.phi ();
212 stmt_info = vinfo_for_stmt (phi);
213 if (dump_enabled_p ())
215 dump_printf_loc (MSG_NOTE, vect_location, "==> examining phi: ");
216 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, phi, 0);
219 gcc_assert (stmt_info);
221 if (STMT_VINFO_RELEVANT_P (stmt_info)
222 || STMT_VINFO_LIVE_P (stmt_info))
224 gcc_assert (!STMT_VINFO_VECTYPE (stmt_info));
225 scalar_type = TREE_TYPE (PHI_RESULT (phi));
227 if (dump_enabled_p ())
229 dump_printf_loc (MSG_NOTE, vect_location,
230 "get vectype for scalar type: ");
231 dump_generic_expr (MSG_NOTE, TDF_SLIM, scalar_type);
232 dump_printf (MSG_NOTE, "\n");
235 vectype = get_vectype_for_scalar_type (scalar_type);
236 if (!vectype)
238 if (dump_enabled_p ())
240 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
241 "not vectorized: unsupported "
242 "data-type ");
243 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
244 scalar_type);
245 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
247 return false;
249 STMT_VINFO_VECTYPE (stmt_info) = vectype;
251 if (dump_enabled_p ())
253 dump_printf_loc (MSG_NOTE, vect_location, "vectype: ");
254 dump_generic_expr (MSG_NOTE, TDF_SLIM, vectype);
255 dump_printf (MSG_NOTE, "\n");
258 nunits = TYPE_VECTOR_SUBPARTS (vectype);
259 if (dump_enabled_p ())
260 dump_printf_loc (MSG_NOTE, vect_location, "nunits = %d\n",
261 nunits);
263 if (!vectorization_factor
264 || (nunits > vectorization_factor))
265 vectorization_factor = nunits;
269 for (gimple_stmt_iterator si = gsi_start_bb (bb);
270 !gsi_end_p (si) || analyze_pattern_stmt;)
272 tree vf_vectype;
274 if (analyze_pattern_stmt)
275 stmt = pattern_stmt;
276 else
277 stmt = gsi_stmt (si);
279 stmt_info = vinfo_for_stmt (stmt);
281 if (dump_enabled_p ())
283 dump_printf_loc (MSG_NOTE, vect_location,
284 "==> examining statement: ");
285 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
288 gcc_assert (stmt_info);
290 /* Skip stmts which do not need to be vectorized. */
291 if ((!STMT_VINFO_RELEVANT_P (stmt_info)
292 && !STMT_VINFO_LIVE_P (stmt_info))
293 || gimple_clobber_p (stmt))
295 if (STMT_VINFO_IN_PATTERN_P (stmt_info)
296 && (pattern_stmt = STMT_VINFO_RELATED_STMT (stmt_info))
297 && (STMT_VINFO_RELEVANT_P (vinfo_for_stmt (pattern_stmt))
298 || STMT_VINFO_LIVE_P (vinfo_for_stmt (pattern_stmt))))
300 stmt = pattern_stmt;
301 stmt_info = vinfo_for_stmt (pattern_stmt);
302 if (dump_enabled_p ())
304 dump_printf_loc (MSG_NOTE, vect_location,
305 "==> examining pattern statement: ");
306 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
309 else
311 if (dump_enabled_p ())
312 dump_printf_loc (MSG_NOTE, vect_location, "skip.\n");
313 gsi_next (&si);
314 continue;
317 else if (STMT_VINFO_IN_PATTERN_P (stmt_info)
318 && (pattern_stmt = STMT_VINFO_RELATED_STMT (stmt_info))
319 && (STMT_VINFO_RELEVANT_P (vinfo_for_stmt (pattern_stmt))
320 || STMT_VINFO_LIVE_P (vinfo_for_stmt (pattern_stmt))))
321 analyze_pattern_stmt = true;
323 /* If a pattern statement has def stmts, analyze them too. */
324 if (is_pattern_stmt_p (stmt_info))
326 if (pattern_def_seq == NULL)
328 pattern_def_seq = STMT_VINFO_PATTERN_DEF_SEQ (stmt_info);
329 pattern_def_si = gsi_start (pattern_def_seq);
331 else if (!gsi_end_p (pattern_def_si))
332 gsi_next (&pattern_def_si);
333 if (pattern_def_seq != NULL)
335 gimple *pattern_def_stmt = NULL;
336 stmt_vec_info pattern_def_stmt_info = NULL;
338 while (!gsi_end_p (pattern_def_si))
340 pattern_def_stmt = gsi_stmt (pattern_def_si);
341 pattern_def_stmt_info
342 = vinfo_for_stmt (pattern_def_stmt);
343 if (STMT_VINFO_RELEVANT_P (pattern_def_stmt_info)
344 || STMT_VINFO_LIVE_P (pattern_def_stmt_info))
345 break;
346 gsi_next (&pattern_def_si);
349 if (!gsi_end_p (pattern_def_si))
351 if (dump_enabled_p ())
353 dump_printf_loc (MSG_NOTE, vect_location,
354 "==> examining pattern def stmt: ");
355 dump_gimple_stmt (MSG_NOTE, TDF_SLIM,
356 pattern_def_stmt, 0);
359 stmt = pattern_def_stmt;
360 stmt_info = pattern_def_stmt_info;
362 else
364 pattern_def_si = gsi_none ();
365 analyze_pattern_stmt = false;
368 else
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;
387 gsi_next (&si);
389 continue;
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,
398 return false;
401 if (VECTOR_MODE_P (TYPE_MODE (gimple_expr_type (stmt))))
403 if (dump_enabled_p ())
405 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
406 "not vectorized: vector stmt in loop:");
407 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
409 return false;
412 bool_result = false;
414 if (STMT_VINFO_VECTYPE (stmt_info))
416 /* The only case when a vectype had been already set is for stmts
417 that contain a dataref, or for "pattern-stmts" (stmts
418 generated by the vectorizer to represent/replace a certain
419 idiom). */
420 gcc_assert (STMT_VINFO_DATA_REF (stmt_info)
421 || is_pattern_stmt_p (stmt_info)
422 || !gsi_end_p (pattern_def_si));
423 vectype = STMT_VINFO_VECTYPE (stmt_info);
425 else
427 gcc_assert (!STMT_VINFO_DATA_REF (stmt_info));
428 if (gimple_call_internal_p (stmt, IFN_MASK_STORE))
429 scalar_type = TREE_TYPE (gimple_call_arg (stmt, 3));
430 else
431 scalar_type = TREE_TYPE (gimple_get_lhs (stmt));
433 /* Bool ops don't participate in vectorization factor
434 computation. For comparison use compared types to
435 compute a factor. */
436 if (VECT_SCALAR_BOOLEAN_TYPE_P (scalar_type)
437 && is_gimple_assign (stmt)
438 && gimple_assign_rhs_code (stmt) != COND_EXPR)
440 if (STMT_VINFO_RELEVANT_P (stmt_info)
441 || STMT_VINFO_LIVE_P (stmt_info))
442 mask_producers.safe_push (stmt_info);
443 bool_result = true;
445 if (TREE_CODE_CLASS (gimple_assign_rhs_code (stmt))
446 == tcc_comparison
447 && !VECT_SCALAR_BOOLEAN_TYPE_P
448 (TREE_TYPE (gimple_assign_rhs1 (stmt))))
449 scalar_type = TREE_TYPE (gimple_assign_rhs1 (stmt));
450 else
452 if (!analyze_pattern_stmt && gsi_end_p (pattern_def_si))
454 pattern_def_seq = NULL;
455 gsi_next (&si);
457 continue;
461 if (dump_enabled_p ())
463 dump_printf_loc (MSG_NOTE, vect_location,
464 "get vectype for scalar type: ");
465 dump_generic_expr (MSG_NOTE, TDF_SLIM, scalar_type);
466 dump_printf (MSG_NOTE, "\n");
468 vectype = get_vectype_for_scalar_type (scalar_type);
469 if (!vectype)
471 if (dump_enabled_p ())
473 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
474 "not vectorized: unsupported "
475 "data-type ");
476 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
477 scalar_type);
478 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
480 return false;
483 if (!bool_result)
484 STMT_VINFO_VECTYPE (stmt_info) = vectype;
486 if (dump_enabled_p ())
488 dump_printf_loc (MSG_NOTE, vect_location, "vectype: ");
489 dump_generic_expr (MSG_NOTE, TDF_SLIM, vectype);
490 dump_printf (MSG_NOTE, "\n");
494 /* Don't try to compute VF out scalar types if we stmt
495 produces boolean vector. Use result vectype instead. */
496 if (VECTOR_BOOLEAN_TYPE_P (vectype))
497 vf_vectype = vectype;
498 else
500 /* The vectorization factor is according to the smallest
501 scalar type (or the largest vector size, but we only
502 support one vector size per loop). */
503 if (!bool_result)
504 scalar_type = vect_get_smallest_scalar_type (stmt, &dummy,
505 &dummy);
506 if (dump_enabled_p ())
508 dump_printf_loc (MSG_NOTE, vect_location,
509 "get vectype for scalar type: ");
510 dump_generic_expr (MSG_NOTE, TDF_SLIM, scalar_type);
511 dump_printf (MSG_NOTE, "\n");
513 vf_vectype = get_vectype_for_scalar_type (scalar_type);
515 if (!vf_vectype)
517 if (dump_enabled_p ())
519 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
520 "not vectorized: unsupported data-type ");
521 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
522 scalar_type);
523 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
525 return false;
528 if ((GET_MODE_SIZE (TYPE_MODE (vectype))
529 != GET_MODE_SIZE (TYPE_MODE (vf_vectype))))
531 if (dump_enabled_p ())
533 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
534 "not vectorized: different sized vector "
535 "types in statement, ");
536 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
537 vectype);
538 dump_printf (MSG_MISSED_OPTIMIZATION, " and ");
539 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
540 vf_vectype);
541 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
543 return false;
546 if (dump_enabled_p ())
548 dump_printf_loc (MSG_NOTE, vect_location, "vectype: ");
549 dump_generic_expr (MSG_NOTE, TDF_SLIM, vf_vectype);
550 dump_printf (MSG_NOTE, "\n");
553 nunits = TYPE_VECTOR_SUBPARTS (vf_vectype);
554 if (dump_enabled_p ())
555 dump_printf_loc (MSG_NOTE, vect_location, "nunits = %d\n", nunits);
556 if (!vectorization_factor
557 || (nunits > vectorization_factor))
558 vectorization_factor = nunits;
560 if (!analyze_pattern_stmt && gsi_end_p (pattern_def_si))
562 pattern_def_seq = NULL;
563 gsi_next (&si);
568 /* TODO: Analyze cost. Decide if worth while to vectorize. */
569 if (dump_enabled_p ())
570 dump_printf_loc (MSG_NOTE, vect_location, "vectorization factor = %d\n",
571 vectorization_factor);
572 if (vectorization_factor <= 1)
574 if (dump_enabled_p ())
575 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
576 "not vectorized: unsupported data-type\n");
577 return false;
579 LOOP_VINFO_VECT_FACTOR (loop_vinfo) = vectorization_factor;
581 for (i = 0; i < mask_producers.length (); i++)
583 tree mask_type = NULL;
585 stmt = STMT_VINFO_STMT (mask_producers[i]);
587 if (is_gimple_assign (stmt)
588 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)) == tcc_comparison
589 && !VECT_SCALAR_BOOLEAN_TYPE_P
590 (TREE_TYPE (gimple_assign_rhs1 (stmt))))
592 scalar_type = TREE_TYPE (gimple_assign_rhs1 (stmt));
593 mask_type = get_mask_type_for_scalar_type (scalar_type);
595 if (!mask_type)
597 if (dump_enabled_p ())
598 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
599 "not vectorized: unsupported mask\n");
600 return false;
603 else
605 tree rhs;
606 ssa_op_iter iter;
607 gimple *def_stmt;
608 enum vect_def_type dt;
610 FOR_EACH_SSA_TREE_OPERAND (rhs, stmt, iter, SSA_OP_USE)
612 if (!vect_is_simple_use (rhs, mask_producers[i]->vinfo,
613 &def_stmt, &dt, &vectype))
615 if (dump_enabled_p ())
617 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
618 "not vectorized: can't compute mask type "
619 "for statement, ");
620 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt,
623 return false;
626 /* No vectype probably means external definition.
627 Allow it in case there is another operand which
628 allows to determine mask type. */
629 if (!vectype)
630 continue;
632 if (!mask_type)
633 mask_type = vectype;
634 else if (TYPE_VECTOR_SUBPARTS (mask_type)
635 != TYPE_VECTOR_SUBPARTS (vectype))
637 if (dump_enabled_p ())
639 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
640 "not vectorized: different sized masks "
641 "types in statement, ");
642 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
643 mask_type);
644 dump_printf (MSG_MISSED_OPTIMIZATION, " and ");
645 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
646 vectype);
647 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
649 return false;
651 else if (VECTOR_BOOLEAN_TYPE_P (mask_type)
652 != VECTOR_BOOLEAN_TYPE_P (vectype))
654 if (dump_enabled_p ())
656 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
657 "not vectorized: mixed mask and "
658 "nonmask vector types in statement, ");
659 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
660 mask_type);
661 dump_printf (MSG_MISSED_OPTIMIZATION, " and ");
662 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
663 vectype);
664 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
666 return false;
670 /* We may compare boolean value loaded as vector of integers.
671 Fix mask_type in such case. */
672 if (mask_type
673 && !VECTOR_BOOLEAN_TYPE_P (mask_type)
674 && gimple_code (stmt) == GIMPLE_ASSIGN
675 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)) == tcc_comparison)
676 mask_type = build_same_sized_truth_vector_type (mask_type);
679 /* No mask_type should mean loop invariant predicate.
680 This is probably a subject for optimization in
681 if-conversion. */
682 if (!mask_type)
684 if (dump_enabled_p ())
686 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
687 "not vectorized: can't compute mask type "
688 "for statement, ");
689 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt,
692 return false;
695 STMT_VINFO_VECTYPE (mask_producers[i]) = mask_type;
698 return true;
702 /* Function vect_is_simple_iv_evolution.
704 FORNOW: A simple evolution of an induction variables in the loop is
705 considered a polynomial evolution. */
707 static bool
708 vect_is_simple_iv_evolution (unsigned loop_nb, tree access_fn, tree * init,
709 tree * step)
711 tree init_expr;
712 tree step_expr;
713 tree evolution_part = evolution_part_in_loop_num (access_fn, loop_nb);
714 basic_block bb;
716 /* When there is no evolution in this loop, the evolution function
717 is not "simple". */
718 if (evolution_part == NULL_TREE)
719 return false;
721 /* When the evolution is a polynomial of degree >= 2
722 the evolution function is not "simple". */
723 if (tree_is_chrec (evolution_part))
724 return false;
726 step_expr = evolution_part;
727 init_expr = unshare_expr (initial_condition_in_loop_num (access_fn, loop_nb));
729 if (dump_enabled_p ())
731 dump_printf_loc (MSG_NOTE, vect_location, "step: ");
732 dump_generic_expr (MSG_NOTE, TDF_SLIM, step_expr);
733 dump_printf (MSG_NOTE, ", init: ");
734 dump_generic_expr (MSG_NOTE, TDF_SLIM, init_expr);
735 dump_printf (MSG_NOTE, "\n");
738 *init = init_expr;
739 *step = step_expr;
741 if (TREE_CODE (step_expr) != INTEGER_CST
742 && (TREE_CODE (step_expr) != SSA_NAME
743 || ((bb = gimple_bb (SSA_NAME_DEF_STMT (step_expr)))
744 && flow_bb_inside_loop_p (get_loop (cfun, loop_nb), bb))
745 || (!INTEGRAL_TYPE_P (TREE_TYPE (step_expr))
746 && (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (step_expr))
747 || !flag_associative_math)))
748 && (TREE_CODE (step_expr) != REAL_CST
749 || !flag_associative_math))
751 if (dump_enabled_p ())
752 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
753 "step unknown.\n");
754 return false;
757 return true;
760 /* Function vect_analyze_scalar_cycles_1.
762 Examine the cross iteration def-use cycles of scalar variables
763 in LOOP. LOOP_VINFO represents the loop that is now being
764 considered for vectorization (can be LOOP, or an outer-loop
765 enclosing LOOP). */
767 static void
768 vect_analyze_scalar_cycles_1 (loop_vec_info loop_vinfo, struct loop *loop)
770 basic_block bb = loop->header;
771 tree init, step;
772 auto_vec<gimple *, 64> worklist;
773 gphi_iterator gsi;
774 bool double_reduc;
776 if (dump_enabled_p ())
777 dump_printf_loc (MSG_NOTE, vect_location,
778 "=== vect_analyze_scalar_cycles ===\n");
780 /* First - identify all inductions. Reduction detection assumes that all the
781 inductions have been identified, therefore, this order must not be
782 changed. */
783 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
785 gphi *phi = gsi.phi ();
786 tree access_fn = NULL;
787 tree def = PHI_RESULT (phi);
788 stmt_vec_info stmt_vinfo = vinfo_for_stmt (phi);
790 if (dump_enabled_p ())
792 dump_printf_loc (MSG_NOTE, vect_location, "Analyze phi: ");
793 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, phi, 0);
796 /* Skip virtual phi's. The data dependences that are associated with
797 virtual defs/uses (i.e., memory accesses) are analyzed elsewhere. */
798 if (virtual_operand_p (def))
799 continue;
801 STMT_VINFO_DEF_TYPE (stmt_vinfo) = vect_unknown_def_type;
803 /* Analyze the evolution function. */
804 access_fn = analyze_scalar_evolution (loop, def);
805 if (access_fn)
807 STRIP_NOPS (access_fn);
808 if (dump_enabled_p ())
810 dump_printf_loc (MSG_NOTE, vect_location,
811 "Access function of PHI: ");
812 dump_generic_expr (MSG_NOTE, TDF_SLIM, access_fn);
813 dump_printf (MSG_NOTE, "\n");
815 STMT_VINFO_LOOP_PHI_EVOLUTION_BASE_UNCHANGED (stmt_vinfo)
816 = initial_condition_in_loop_num (access_fn, loop->num);
817 STMT_VINFO_LOOP_PHI_EVOLUTION_PART (stmt_vinfo)
818 = evolution_part_in_loop_num (access_fn, loop->num);
821 if (!access_fn
822 || !vect_is_simple_iv_evolution (loop->num, access_fn, &init, &step)
823 || (LOOP_VINFO_LOOP (loop_vinfo) != loop
824 && TREE_CODE (step) != INTEGER_CST))
826 worklist.safe_push (phi);
827 continue;
830 gcc_assert (STMT_VINFO_LOOP_PHI_EVOLUTION_BASE_UNCHANGED (stmt_vinfo)
831 != NULL_TREE);
832 gcc_assert (STMT_VINFO_LOOP_PHI_EVOLUTION_PART (stmt_vinfo) != NULL_TREE);
834 if (dump_enabled_p ())
835 dump_printf_loc (MSG_NOTE, vect_location, "Detected induction.\n");
836 STMT_VINFO_DEF_TYPE (stmt_vinfo) = vect_induction_def;
840 /* Second - identify all reductions and nested cycles. */
841 while (worklist.length () > 0)
843 gimple *phi = worklist.pop ();
844 tree def = PHI_RESULT (phi);
845 stmt_vec_info stmt_vinfo = vinfo_for_stmt (phi);
846 gimple *reduc_stmt;
848 if (dump_enabled_p ())
850 dump_printf_loc (MSG_NOTE, vect_location, "Analyze phi: ");
851 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, phi, 0);
854 gcc_assert (!virtual_operand_p (def)
855 && STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_unknown_def_type);
857 reduc_stmt = vect_force_simple_reduction (loop_vinfo, phi,
858 &double_reduc, false);
859 if (reduc_stmt)
861 if (double_reduc)
863 if (dump_enabled_p ())
864 dump_printf_loc (MSG_NOTE, vect_location,
865 "Detected double reduction.\n");
867 STMT_VINFO_DEF_TYPE (stmt_vinfo) = vect_double_reduction_def;
868 STMT_VINFO_DEF_TYPE (vinfo_for_stmt (reduc_stmt)) =
869 vect_double_reduction_def;
871 else
873 if (loop != LOOP_VINFO_LOOP (loop_vinfo))
875 if (dump_enabled_p ())
876 dump_printf_loc (MSG_NOTE, vect_location,
877 "Detected vectorizable nested cycle.\n");
879 STMT_VINFO_DEF_TYPE (stmt_vinfo) = vect_nested_cycle;
880 STMT_VINFO_DEF_TYPE (vinfo_for_stmt (reduc_stmt)) =
881 vect_nested_cycle;
883 else
885 if (dump_enabled_p ())
886 dump_printf_loc (MSG_NOTE, vect_location,
887 "Detected reduction.\n");
889 STMT_VINFO_DEF_TYPE (stmt_vinfo) = vect_reduction_def;
890 STMT_VINFO_DEF_TYPE (vinfo_for_stmt (reduc_stmt)) =
891 vect_reduction_def;
892 /* Store the reduction cycles for possible vectorization in
893 loop-aware SLP. */
894 LOOP_VINFO_REDUCTIONS (loop_vinfo).safe_push (reduc_stmt);
898 else
899 if (dump_enabled_p ())
900 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
901 "Unknown def-use cycle pattern.\n");
906 /* Function vect_analyze_scalar_cycles.
908 Examine the cross iteration def-use cycles of scalar variables, by
909 analyzing the loop-header PHIs of scalar variables. Classify each
910 cycle as one of the following: invariant, induction, reduction, unknown.
911 We do that for the loop represented by LOOP_VINFO, and also to its
912 inner-loop, if exists.
913 Examples for scalar cycles:
915 Example1: reduction:
917 loop1:
918 for (i=0; i<N; i++)
919 sum += a[i];
921 Example2: induction:
923 loop2:
924 for (i=0; i<N; i++)
925 a[i] = i; */
927 static void
928 vect_analyze_scalar_cycles (loop_vec_info loop_vinfo)
930 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
932 vect_analyze_scalar_cycles_1 (loop_vinfo, loop);
934 /* When vectorizing an outer-loop, the inner-loop is executed sequentially.
935 Reductions in such inner-loop therefore have different properties than
936 the reductions in the nest that gets vectorized:
937 1. When vectorized, they are executed in the same order as in the original
938 scalar loop, so we can't change the order of computation when
939 vectorizing them.
940 2. FIXME: Inner-loop reductions can be used in the inner-loop, so the
941 current checks are too strict. */
943 if (loop->inner)
944 vect_analyze_scalar_cycles_1 (loop_vinfo, loop->inner);
947 /* Transfer group and reduction information from STMT to its pattern stmt. */
949 static void
950 vect_fixup_reduc_chain (gimple *stmt)
952 gimple *firstp = STMT_VINFO_RELATED_STMT (vinfo_for_stmt (stmt));
953 gimple *stmtp;
954 gcc_assert (!GROUP_FIRST_ELEMENT (vinfo_for_stmt (firstp))
955 && GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)));
956 GROUP_SIZE (vinfo_for_stmt (firstp)) = GROUP_SIZE (vinfo_for_stmt (stmt));
959 stmtp = STMT_VINFO_RELATED_STMT (vinfo_for_stmt (stmt));
960 GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmtp)) = firstp;
961 stmt = GROUP_NEXT_ELEMENT (vinfo_for_stmt (stmt));
962 if (stmt)
963 GROUP_NEXT_ELEMENT (vinfo_for_stmt (stmtp))
964 = STMT_VINFO_RELATED_STMT (vinfo_for_stmt (stmt));
966 while (stmt);
967 STMT_VINFO_DEF_TYPE (vinfo_for_stmt (stmtp)) = vect_reduction_def;
970 /* Fixup scalar cycles that now have their stmts detected as patterns. */
972 static void
973 vect_fixup_scalar_cycles_with_patterns (loop_vec_info loop_vinfo)
975 gimple *first;
976 unsigned i;
978 FOR_EACH_VEC_ELT (LOOP_VINFO_REDUCTION_CHAINS (loop_vinfo), i, first)
979 if (STMT_VINFO_IN_PATTERN_P (vinfo_for_stmt (first)))
981 gimple *next = GROUP_NEXT_ELEMENT (vinfo_for_stmt (first));
982 while (next)
984 if (! STMT_VINFO_IN_PATTERN_P (vinfo_for_stmt (next)))
985 break;
986 next = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next));
988 /* If not all stmt in the chain are patterns try to handle
989 the chain without patterns. */
990 if (! next)
992 vect_fixup_reduc_chain (first);
993 LOOP_VINFO_REDUCTION_CHAINS (loop_vinfo)[i]
994 = STMT_VINFO_RELATED_STMT (vinfo_for_stmt (first));
999 /* Function vect_get_loop_niters.
1001 Determine how many iterations the loop is executed and place it
1002 in NUMBER_OF_ITERATIONS. Place the number of latch iterations
1003 in NUMBER_OF_ITERATIONSM1. Place the condition under which the
1004 niter information holds in ASSUMPTIONS.
1006 Return the loop exit condition. */
1009 static gcond *
1010 vect_get_loop_niters (struct loop *loop, tree *assumptions,
1011 tree *number_of_iterations, tree *number_of_iterationsm1)
1013 edge exit = single_exit (loop);
1014 struct tree_niter_desc niter_desc;
1015 tree niter_assumptions, niter, may_be_zero;
1016 gcond *cond = get_loop_exit_condition (loop);
1018 *assumptions = boolean_true_node;
1019 *number_of_iterationsm1 = chrec_dont_know;
1020 *number_of_iterations = chrec_dont_know;
1021 if (dump_enabled_p ())
1022 dump_printf_loc (MSG_NOTE, vect_location,
1023 "=== get_loop_niters ===\n");
1025 if (!exit)
1026 return cond;
1028 niter = chrec_dont_know;
1029 may_be_zero = NULL_TREE;
1030 niter_assumptions = boolean_true_node;
1031 if (!number_of_iterations_exit_assumptions (loop, exit, &niter_desc, NULL)
1032 || chrec_contains_undetermined (niter_desc.niter))
1033 return cond;
1035 niter_assumptions = niter_desc.assumptions;
1036 may_be_zero = niter_desc.may_be_zero;
1037 niter = niter_desc.niter;
1039 if (may_be_zero && integer_zerop (may_be_zero))
1040 may_be_zero = NULL_TREE;
1042 if (may_be_zero)
1044 if (COMPARISON_CLASS_P (may_be_zero))
1046 /* Try to combine may_be_zero with assumptions, this can simplify
1047 computation of niter expression. */
1048 if (niter_assumptions && !integer_nonzerop (niter_assumptions))
1049 niter_assumptions = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
1050 niter_assumptions,
1051 fold_build1 (TRUTH_NOT_EXPR,
1052 boolean_type_node,
1053 may_be_zero));
1054 else
1055 niter = fold_build3 (COND_EXPR, TREE_TYPE (niter), may_be_zero,
1056 build_int_cst (TREE_TYPE (niter), 0), niter);
1058 may_be_zero = NULL_TREE;
1060 else if (integer_nonzerop (may_be_zero))
1062 *number_of_iterationsm1 = build_int_cst (TREE_TYPE (niter), 0);
1063 *number_of_iterations = build_int_cst (TREE_TYPE (niter), 1);
1064 return cond;
1066 else
1067 return cond;
1070 *assumptions = niter_assumptions;
1071 *number_of_iterationsm1 = niter;
1073 /* We want the number of loop header executions which is the number
1074 of latch executions plus one.
1075 ??? For UINT_MAX latch executions this number overflows to zero
1076 for loops like do { n++; } while (n != 0); */
1077 if (niter && !chrec_contains_undetermined (niter))
1078 niter = fold_build2 (PLUS_EXPR, TREE_TYPE (niter), unshare_expr (niter),
1079 build_int_cst (TREE_TYPE (niter), 1));
1080 *number_of_iterations = niter;
1082 return cond;
1085 /* Function bb_in_loop_p
1087 Used as predicate for dfs order traversal of the loop bbs. */
1089 static bool
1090 bb_in_loop_p (const_basic_block bb, const void *data)
1092 const struct loop *const loop = (const struct loop *)data;
1093 if (flow_bb_inside_loop_p (loop, bb))
1094 return true;
1095 return false;
1099 /* Function new_loop_vec_info.
1101 Create and initialize a new loop_vec_info struct for LOOP, as well as
1102 stmt_vec_info structs for all the stmts in LOOP. */
1104 static loop_vec_info
1105 new_loop_vec_info (struct loop *loop)
1107 loop_vec_info res;
1108 basic_block *bbs;
1109 gimple_stmt_iterator si;
1110 unsigned int i, nbbs;
1112 res = (loop_vec_info) xcalloc (1, sizeof (struct _loop_vec_info));
1113 res->kind = vec_info::loop;
1114 LOOP_VINFO_LOOP (res) = loop;
1116 bbs = get_loop_body (loop);
1118 /* Create/Update stmt_info for all stmts in the loop. */
1119 for (i = 0; i < loop->num_nodes; i++)
1121 basic_block bb = bbs[i];
1123 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
1125 gimple *phi = gsi_stmt (si);
1126 gimple_set_uid (phi, 0);
1127 set_vinfo_for_stmt (phi, new_stmt_vec_info (phi, res));
1130 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
1132 gimple *stmt = gsi_stmt (si);
1133 gimple_set_uid (stmt, 0);
1134 set_vinfo_for_stmt (stmt, new_stmt_vec_info (stmt, res));
1138 /* CHECKME: We want to visit all BBs before their successors (except for
1139 latch blocks, for which this assertion wouldn't hold). In the simple
1140 case of the loop forms we allow, a dfs order of the BBs would the same
1141 as reversed postorder traversal, so we are safe. */
1143 free (bbs);
1144 bbs = XCNEWVEC (basic_block, loop->num_nodes);
1145 nbbs = dfs_enumerate_from (loop->header, 0, bb_in_loop_p,
1146 bbs, loop->num_nodes, loop);
1147 gcc_assert (nbbs == loop->num_nodes);
1149 LOOP_VINFO_BBS (res) = bbs;
1150 LOOP_VINFO_NITERSM1 (res) = NULL;
1151 LOOP_VINFO_NITERS (res) = NULL;
1152 LOOP_VINFO_NITERS_UNCHANGED (res) = NULL;
1153 LOOP_VINFO_NITERS_ASSUMPTIONS (res) = NULL;
1154 LOOP_VINFO_COST_MODEL_THRESHOLD (res) = 0;
1155 LOOP_VINFO_VECTORIZABLE_P (res) = 0;
1156 LOOP_VINFO_PEELING_FOR_ALIGNMENT (res) = 0;
1157 LOOP_VINFO_VECT_FACTOR (res) = 0;
1158 LOOP_VINFO_LOOP_NEST (res) = vNULL;
1159 LOOP_VINFO_DATAREFS (res) = vNULL;
1160 LOOP_VINFO_DDRS (res) = vNULL;
1161 LOOP_VINFO_UNALIGNED_DR (res) = NULL;
1162 LOOP_VINFO_MAY_MISALIGN_STMTS (res) = vNULL;
1163 LOOP_VINFO_MAY_ALIAS_DDRS (res) = vNULL;
1164 LOOP_VINFO_GROUPED_STORES (res) = vNULL;
1165 LOOP_VINFO_REDUCTIONS (res) = vNULL;
1166 LOOP_VINFO_REDUCTION_CHAINS (res) = vNULL;
1167 LOOP_VINFO_SLP_INSTANCES (res) = vNULL;
1168 LOOP_VINFO_SLP_UNROLLING_FACTOR (res) = 1;
1169 LOOP_VINFO_TARGET_COST_DATA (res) = init_cost (loop);
1170 LOOP_VINFO_PEELING_FOR_GAPS (res) = false;
1171 LOOP_VINFO_PEELING_FOR_NITER (res) = false;
1172 LOOP_VINFO_OPERANDS_SWAPPED (res) = false;
1173 LOOP_VINFO_ORIG_LOOP_INFO (res) = NULL;
1175 return res;
1179 /* Function destroy_loop_vec_info.
1181 Free LOOP_VINFO struct, as well as all the stmt_vec_info structs of all the
1182 stmts in the loop. */
1184 void
1185 destroy_loop_vec_info (loop_vec_info loop_vinfo, bool clean_stmts)
1187 struct loop *loop;
1188 basic_block *bbs;
1189 int nbbs;
1190 gimple_stmt_iterator si;
1191 int j;
1192 vec<slp_instance> slp_instances;
1193 slp_instance instance;
1194 bool swapped;
1196 if (!loop_vinfo)
1197 return;
1199 loop = LOOP_VINFO_LOOP (loop_vinfo);
1201 bbs = LOOP_VINFO_BBS (loop_vinfo);
1202 nbbs = clean_stmts ? loop->num_nodes : 0;
1203 swapped = LOOP_VINFO_OPERANDS_SWAPPED (loop_vinfo);
1205 for (j = 0; j < nbbs; j++)
1207 basic_block bb = bbs[j];
1208 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
1209 free_stmt_vec_info (gsi_stmt (si));
1211 for (si = gsi_start_bb (bb); !gsi_end_p (si); )
1213 gimple *stmt = gsi_stmt (si);
1215 /* We may have broken canonical form by moving a constant
1216 into RHS1 of a commutative op. Fix such occurrences. */
1217 if (swapped && is_gimple_assign (stmt))
1219 enum tree_code code = gimple_assign_rhs_code (stmt);
1221 if ((code == PLUS_EXPR
1222 || code == POINTER_PLUS_EXPR
1223 || code == MULT_EXPR)
1224 && CONSTANT_CLASS_P (gimple_assign_rhs1 (stmt)))
1225 swap_ssa_operands (stmt,
1226 gimple_assign_rhs1_ptr (stmt),
1227 gimple_assign_rhs2_ptr (stmt));
1228 else if (code == COND_EXPR
1229 && CONSTANT_CLASS_P (gimple_assign_rhs2 (stmt)))
1231 tree cond_expr = gimple_assign_rhs1 (stmt);
1232 enum tree_code cond_code = TREE_CODE (cond_expr);
1234 if (TREE_CODE_CLASS (cond_code) == tcc_comparison)
1236 bool honor_nans = HONOR_NANS (TREE_OPERAND (cond_expr,
1237 0));
1238 cond_code = invert_tree_comparison (cond_code,
1239 honor_nans);
1240 if (cond_code != ERROR_MARK)
1242 TREE_SET_CODE (cond_expr, cond_code);
1243 swap_ssa_operands (stmt,
1244 gimple_assign_rhs2_ptr (stmt),
1245 gimple_assign_rhs3_ptr (stmt));
1251 /* Free stmt_vec_info. */
1252 free_stmt_vec_info (stmt);
1253 gsi_next (&si);
1257 free (LOOP_VINFO_BBS (loop_vinfo));
1258 vect_destroy_datarefs (loop_vinfo);
1259 free_dependence_relations (LOOP_VINFO_DDRS (loop_vinfo));
1260 LOOP_VINFO_LOOP_NEST (loop_vinfo).release ();
1261 LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo).release ();
1262 LOOP_VINFO_COMP_ALIAS_DDRS (loop_vinfo).release ();
1263 LOOP_VINFO_MAY_ALIAS_DDRS (loop_vinfo).release ();
1264 slp_instances = LOOP_VINFO_SLP_INSTANCES (loop_vinfo);
1265 FOR_EACH_VEC_ELT (slp_instances, j, instance)
1266 vect_free_slp_instance (instance);
1268 LOOP_VINFO_SLP_INSTANCES (loop_vinfo).release ();
1269 LOOP_VINFO_GROUPED_STORES (loop_vinfo).release ();
1270 LOOP_VINFO_REDUCTIONS (loop_vinfo).release ();
1271 LOOP_VINFO_REDUCTION_CHAINS (loop_vinfo).release ();
1273 destroy_cost_data (LOOP_VINFO_TARGET_COST_DATA (loop_vinfo));
1274 loop_vinfo->scalar_cost_vec.release ();
1276 free (loop_vinfo);
1277 loop->aux = NULL;
1281 /* Calculate the cost of one scalar iteration of the loop. */
1282 static void
1283 vect_compute_single_scalar_iteration_cost (loop_vec_info loop_vinfo)
1285 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
1286 basic_block *bbs = LOOP_VINFO_BBS (loop_vinfo);
1287 int nbbs = loop->num_nodes, factor, scalar_single_iter_cost = 0;
1288 int innerloop_iters, i;
1290 /* Count statements in scalar loop. Using this as scalar cost for a single
1291 iteration for now.
1293 TODO: Add outer loop support.
1295 TODO: Consider assigning different costs to different scalar
1296 statements. */
1298 /* FORNOW. */
1299 innerloop_iters = 1;
1300 if (loop->inner)
1301 innerloop_iters = 50; /* FIXME */
1303 for (i = 0; i < nbbs; i++)
1305 gimple_stmt_iterator si;
1306 basic_block bb = bbs[i];
1308 if (bb->loop_father == loop->inner)
1309 factor = innerloop_iters;
1310 else
1311 factor = 1;
1313 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
1315 gimple *stmt = gsi_stmt (si);
1316 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1318 if (!is_gimple_assign (stmt) && !is_gimple_call (stmt))
1319 continue;
1321 /* Skip stmts that are not vectorized inside the loop. */
1322 if (stmt_info
1323 && !STMT_VINFO_RELEVANT_P (stmt_info)
1324 && (!STMT_VINFO_LIVE_P (stmt_info)
1325 || !VECTORIZABLE_CYCLE_DEF (STMT_VINFO_DEF_TYPE (stmt_info)))
1326 && !STMT_VINFO_IN_PATTERN_P (stmt_info))
1327 continue;
1329 vect_cost_for_stmt kind;
1330 if (STMT_VINFO_DATA_REF (stmt_info))
1332 if (DR_IS_READ (STMT_VINFO_DATA_REF (stmt_info)))
1333 kind = scalar_load;
1334 else
1335 kind = scalar_store;
1337 else
1338 kind = scalar_stmt;
1340 scalar_single_iter_cost
1341 += record_stmt_cost (&LOOP_VINFO_SCALAR_ITERATION_COST (loop_vinfo),
1342 factor, kind, stmt_info, 0, vect_prologue);
1345 LOOP_VINFO_SINGLE_SCALAR_ITERATION_COST (loop_vinfo)
1346 = scalar_single_iter_cost;
1350 /* Function vect_analyze_loop_form_1.
1352 Verify that certain CFG restrictions hold, including:
1353 - the loop has a pre-header
1354 - the loop has a single entry and exit
1355 - the loop exit condition is simple enough
1356 - the number of iterations can be analyzed, i.e, a countable loop. The
1357 niter could be analyzed under some assumptions. */
1359 bool
1360 vect_analyze_loop_form_1 (struct loop *loop, gcond **loop_cond,
1361 tree *assumptions, tree *number_of_iterationsm1,
1362 tree *number_of_iterations, gcond **inner_loop_cond)
1364 if (dump_enabled_p ())
1365 dump_printf_loc (MSG_NOTE, vect_location,
1366 "=== vect_analyze_loop_form ===\n");
1368 /* Different restrictions apply when we are considering an inner-most loop,
1369 vs. an outer (nested) loop.
1370 (FORNOW. May want to relax some of these restrictions in the future). */
1372 if (!loop->inner)
1374 /* Inner-most loop. We currently require that the number of BBs is
1375 exactly 2 (the header and latch). Vectorizable inner-most loops
1376 look like this:
1378 (pre-header)
1380 header <--------+
1381 | | |
1382 | +--> latch --+
1384 (exit-bb) */
1386 if (loop->num_nodes != 2)
1388 if (dump_enabled_p ())
1389 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1390 "not vectorized: control flow in loop.\n");
1391 return false;
1394 if (empty_block_p (loop->header))
1396 if (dump_enabled_p ())
1397 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1398 "not vectorized: empty loop.\n");
1399 return false;
1402 else
1404 struct loop *innerloop = loop->inner;
1405 edge entryedge;
1407 /* Nested loop. We currently require that the loop is doubly-nested,
1408 contains a single inner loop, and the number of BBs is exactly 5.
1409 Vectorizable outer-loops look like this:
1411 (pre-header)
1413 header <---+
1415 inner-loop |
1417 tail ------+
1419 (exit-bb)
1421 The inner-loop has the properties expected of inner-most loops
1422 as described above. */
1424 if ((loop->inner)->inner || (loop->inner)->next)
1426 if (dump_enabled_p ())
1427 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1428 "not vectorized: multiple nested loops.\n");
1429 return false;
1432 if (loop->num_nodes != 5)
1434 if (dump_enabled_p ())
1435 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1436 "not vectorized: control flow in loop.\n");
1437 return false;
1440 entryedge = loop_preheader_edge (innerloop);
1441 if (entryedge->src != loop->header
1442 || !single_exit (innerloop)
1443 || single_exit (innerloop)->dest != EDGE_PRED (loop->latch, 0)->src)
1445 if (dump_enabled_p ())
1446 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1447 "not vectorized: unsupported outerloop form.\n");
1448 return false;
1451 /* Analyze the inner-loop. */
1452 tree inner_niterm1, inner_niter, inner_assumptions;
1453 if (! vect_analyze_loop_form_1 (loop->inner, inner_loop_cond,
1454 &inner_assumptions, &inner_niterm1,
1455 &inner_niter, NULL)
1456 /* Don't support analyzing niter under assumptions for inner
1457 loop. */
1458 || !integer_onep (inner_assumptions))
1460 if (dump_enabled_p ())
1461 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1462 "not vectorized: Bad inner loop.\n");
1463 return false;
1466 if (!expr_invariant_in_loop_p (loop, inner_niter))
1468 if (dump_enabled_p ())
1469 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1470 "not vectorized: inner-loop count not"
1471 " invariant.\n");
1472 return false;
1475 if (dump_enabled_p ())
1476 dump_printf_loc (MSG_NOTE, vect_location,
1477 "Considering outer-loop vectorization.\n");
1480 if (!single_exit (loop)
1481 || EDGE_COUNT (loop->header->preds) != 2)
1483 if (dump_enabled_p ())
1485 if (!single_exit (loop))
1486 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1487 "not vectorized: multiple exits.\n");
1488 else if (EDGE_COUNT (loop->header->preds) != 2)
1489 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1490 "not vectorized: too many incoming edges.\n");
1492 return false;
1495 /* We assume that the loop exit condition is at the end of the loop. i.e,
1496 that the loop is represented as a do-while (with a proper if-guard
1497 before the loop if needed), where the loop header contains all the
1498 executable statements, and the latch is empty. */
1499 if (!empty_block_p (loop->latch)
1500 || !gimple_seq_empty_p (phi_nodes (loop->latch)))
1502 if (dump_enabled_p ())
1503 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1504 "not vectorized: latch block not empty.\n");
1505 return false;
1508 /* Make sure the exit is not abnormal. */
1509 edge e = single_exit (loop);
1510 if (e->flags & EDGE_ABNORMAL)
1512 if (dump_enabled_p ())
1513 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1514 "not vectorized: abnormal loop exit edge.\n");
1515 return false;
1518 *loop_cond = vect_get_loop_niters (loop, assumptions, number_of_iterations,
1519 number_of_iterationsm1);
1520 if (!*loop_cond)
1522 if (dump_enabled_p ())
1523 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1524 "not vectorized: complicated exit condition.\n");
1525 return false;
1528 if (integer_zerop (*assumptions)
1529 || !*number_of_iterations
1530 || chrec_contains_undetermined (*number_of_iterations))
1532 if (dump_enabled_p ())
1533 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1534 "not vectorized: number of iterations cannot be "
1535 "computed.\n");
1536 return false;
1539 if (integer_zerop (*number_of_iterations))
1541 if (dump_enabled_p ())
1542 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1543 "not vectorized: number of iterations = 0.\n");
1544 return false;
1547 return true;
1550 /* Analyze LOOP form and return a loop_vec_info if it is of suitable form. */
1552 loop_vec_info
1553 vect_analyze_loop_form (struct loop *loop)
1555 tree assumptions, number_of_iterations, number_of_iterationsm1;
1556 gcond *loop_cond, *inner_loop_cond = NULL;
1558 if (! vect_analyze_loop_form_1 (loop, &loop_cond,
1559 &assumptions, &number_of_iterationsm1,
1560 &number_of_iterations, &inner_loop_cond))
1561 return NULL;
1563 loop_vec_info loop_vinfo = new_loop_vec_info (loop);
1564 LOOP_VINFO_NITERSM1 (loop_vinfo) = number_of_iterationsm1;
1565 LOOP_VINFO_NITERS (loop_vinfo) = number_of_iterations;
1566 LOOP_VINFO_NITERS_UNCHANGED (loop_vinfo) = number_of_iterations;
1567 if (!integer_onep (assumptions))
1569 /* We consider to vectorize this loop by versioning it under
1570 some assumptions. In order to do this, we need to clear
1571 existing information computed by scev and niter analyzer. */
1572 scev_reset_htab ();
1573 free_numbers_of_iterations_estimates (loop);
1574 /* Also set flag for this loop so that following scev and niter
1575 analysis are done under the assumptions. */
1576 loop_constraint_set (loop, LOOP_C_FINITE);
1577 /* Also record the assumptions for versioning. */
1578 LOOP_VINFO_NITERS_ASSUMPTIONS (loop_vinfo) = assumptions;
1581 if (!LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo))
1583 if (dump_enabled_p ())
1585 dump_printf_loc (MSG_NOTE, vect_location,
1586 "Symbolic number of iterations is ");
1587 dump_generic_expr (MSG_NOTE, TDF_DETAILS, number_of_iterations);
1588 dump_printf (MSG_NOTE, "\n");
1592 STMT_VINFO_TYPE (vinfo_for_stmt (loop_cond)) = loop_exit_ctrl_vec_info_type;
1593 if (inner_loop_cond)
1594 STMT_VINFO_TYPE (vinfo_for_stmt (inner_loop_cond))
1595 = loop_exit_ctrl_vec_info_type;
1597 gcc_assert (!loop->aux);
1598 loop->aux = loop_vinfo;
1599 return loop_vinfo;
1604 /* Scan the loop stmts and dependent on whether there are any (non-)SLP
1605 statements update the vectorization factor. */
1607 static void
1608 vect_update_vf_for_slp (loop_vec_info loop_vinfo)
1610 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
1611 basic_block *bbs = LOOP_VINFO_BBS (loop_vinfo);
1612 int nbbs = loop->num_nodes;
1613 unsigned int vectorization_factor;
1614 int i;
1616 if (dump_enabled_p ())
1617 dump_printf_loc (MSG_NOTE, vect_location,
1618 "=== vect_update_vf_for_slp ===\n");
1620 vectorization_factor = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
1621 gcc_assert (vectorization_factor != 0);
1623 /* If all the stmts in the loop can be SLPed, we perform only SLP, and
1624 vectorization factor of the loop is the unrolling factor required by
1625 the SLP instances. If that unrolling factor is 1, we say, that we
1626 perform pure SLP on loop - cross iteration parallelism is not
1627 exploited. */
1628 bool only_slp_in_loop = true;
1629 for (i = 0; i < nbbs; i++)
1631 basic_block bb = bbs[i];
1632 for (gimple_stmt_iterator si = gsi_start_bb (bb); !gsi_end_p (si);
1633 gsi_next (&si))
1635 gimple *stmt = gsi_stmt (si);
1636 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1637 if (STMT_VINFO_IN_PATTERN_P (stmt_info)
1638 && STMT_VINFO_RELATED_STMT (stmt_info))
1640 stmt = STMT_VINFO_RELATED_STMT (stmt_info);
1641 stmt_info = vinfo_for_stmt (stmt);
1643 if ((STMT_VINFO_RELEVANT_P (stmt_info)
1644 || VECTORIZABLE_CYCLE_DEF (STMT_VINFO_DEF_TYPE (stmt_info)))
1645 && !PURE_SLP_STMT (stmt_info))
1646 /* STMT needs both SLP and loop-based vectorization. */
1647 only_slp_in_loop = false;
1651 if (only_slp_in_loop)
1653 dump_printf_loc (MSG_NOTE, vect_location,
1654 "Loop contains only SLP stmts\n");
1655 vectorization_factor = LOOP_VINFO_SLP_UNROLLING_FACTOR (loop_vinfo);
1657 else
1659 dump_printf_loc (MSG_NOTE, vect_location,
1660 "Loop contains SLP and non-SLP stmts\n");
1661 vectorization_factor
1662 = least_common_multiple (vectorization_factor,
1663 LOOP_VINFO_SLP_UNROLLING_FACTOR (loop_vinfo));
1666 LOOP_VINFO_VECT_FACTOR (loop_vinfo) = vectorization_factor;
1667 if (dump_enabled_p ())
1668 dump_printf_loc (MSG_NOTE, vect_location,
1669 "Updating vectorization factor to %d\n",
1670 vectorization_factor);
1673 /* Function vect_analyze_loop_operations.
1675 Scan the loop stmts and make sure they are all vectorizable. */
1677 static bool
1678 vect_analyze_loop_operations (loop_vec_info loop_vinfo)
1680 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
1681 basic_block *bbs = LOOP_VINFO_BBS (loop_vinfo);
1682 int nbbs = loop->num_nodes;
1683 int i;
1684 stmt_vec_info stmt_info;
1685 bool need_to_vectorize = false;
1686 bool ok;
1688 if (dump_enabled_p ())
1689 dump_printf_loc (MSG_NOTE, vect_location,
1690 "=== vect_analyze_loop_operations ===\n");
1692 for (i = 0; i < nbbs; i++)
1694 basic_block bb = bbs[i];
1696 for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
1697 gsi_next (&si))
1699 gphi *phi = si.phi ();
1700 ok = true;
1702 stmt_info = vinfo_for_stmt (phi);
1703 if (dump_enabled_p ())
1705 dump_printf_loc (MSG_NOTE, vect_location, "examining phi: ");
1706 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, phi, 0);
1708 if (virtual_operand_p (gimple_phi_result (phi)))
1709 continue;
1711 /* Inner-loop loop-closed exit phi in outer-loop vectorization
1712 (i.e., a phi in the tail of the outer-loop). */
1713 if (! is_loop_header_bb_p (bb))
1715 /* FORNOW: we currently don't support the case that these phis
1716 are not used in the outerloop (unless it is double reduction,
1717 i.e., this phi is vect_reduction_def), cause this case
1718 requires to actually do something here. */
1719 if (STMT_VINFO_LIVE_P (stmt_info)
1720 && STMT_VINFO_DEF_TYPE (stmt_info)
1721 != vect_double_reduction_def)
1723 if (dump_enabled_p ())
1724 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1725 "Unsupported loop-closed phi in "
1726 "outer-loop.\n");
1727 return false;
1730 /* If PHI is used in the outer loop, we check that its operand
1731 is defined in the inner loop. */
1732 if (STMT_VINFO_RELEVANT_P (stmt_info))
1734 tree phi_op;
1735 gimple *op_def_stmt;
1737 if (gimple_phi_num_args (phi) != 1)
1738 return false;
1740 phi_op = PHI_ARG_DEF (phi, 0);
1741 if (TREE_CODE (phi_op) != SSA_NAME)
1742 return false;
1744 op_def_stmt = SSA_NAME_DEF_STMT (phi_op);
1745 if (gimple_nop_p (op_def_stmt)
1746 || !flow_bb_inside_loop_p (loop, gimple_bb (op_def_stmt))
1747 || !vinfo_for_stmt (op_def_stmt))
1748 return false;
1750 if (STMT_VINFO_RELEVANT (vinfo_for_stmt (op_def_stmt))
1751 != vect_used_in_outer
1752 && STMT_VINFO_RELEVANT (vinfo_for_stmt (op_def_stmt))
1753 != vect_used_in_outer_by_reduction)
1754 return false;
1757 continue;
1760 gcc_assert (stmt_info);
1762 if ((STMT_VINFO_RELEVANT (stmt_info) == vect_used_in_scope
1763 || STMT_VINFO_LIVE_P (stmt_info))
1764 && STMT_VINFO_DEF_TYPE (stmt_info) != vect_induction_def)
1766 /* A scalar-dependence cycle that we don't support. */
1767 if (dump_enabled_p ())
1768 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1769 "not vectorized: scalar dependence cycle.\n");
1770 return false;
1773 if (STMT_VINFO_RELEVANT_P (stmt_info))
1775 need_to_vectorize = true;
1776 if (STMT_VINFO_DEF_TYPE (stmt_info) == vect_induction_def
1777 && ! PURE_SLP_STMT (stmt_info))
1778 ok = vectorizable_induction (phi, NULL, NULL, NULL);
1781 if (ok && STMT_VINFO_LIVE_P (stmt_info))
1782 ok = vectorizable_live_operation (phi, NULL, NULL, -1, NULL);
1784 if (!ok)
1786 if (dump_enabled_p ())
1788 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1789 "not vectorized: relevant phi not "
1790 "supported: ");
1791 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, phi, 0);
1793 return false;
1797 for (gimple_stmt_iterator si = gsi_start_bb (bb); !gsi_end_p (si);
1798 gsi_next (&si))
1800 gimple *stmt = gsi_stmt (si);
1801 if (!gimple_clobber_p (stmt)
1802 && !vect_analyze_stmt (stmt, &need_to_vectorize, NULL))
1803 return false;
1805 } /* bbs */
1807 /* All operations in the loop are either irrelevant (deal with loop
1808 control, or dead), or only used outside the loop and can be moved
1809 out of the loop (e.g. invariants, inductions). The loop can be
1810 optimized away by scalar optimizations. We're better off not
1811 touching this loop. */
1812 if (!need_to_vectorize)
1814 if (dump_enabled_p ())
1815 dump_printf_loc (MSG_NOTE, vect_location,
1816 "All the computation can be taken out of the loop.\n");
1817 if (dump_enabled_p ())
1818 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1819 "not vectorized: redundant loop. no profit to "
1820 "vectorize.\n");
1821 return false;
1824 return true;
1828 /* Function vect_analyze_loop_2.
1830 Apply a set of analyses on LOOP, and create a loop_vec_info struct
1831 for it. The different analyses will record information in the
1832 loop_vec_info struct. */
1833 static bool
1834 vect_analyze_loop_2 (loop_vec_info loop_vinfo, bool &fatal)
1836 bool ok;
1837 int max_vf = MAX_VECTORIZATION_FACTOR;
1838 int min_vf = 2;
1839 unsigned int n_stmts = 0;
1841 /* The first group of checks is independent of the vector size. */
1842 fatal = true;
1844 /* Find all data references in the loop (which correspond to vdefs/vuses)
1845 and analyze their evolution in the loop. */
1847 basic_block *bbs = LOOP_VINFO_BBS (loop_vinfo);
1849 loop_p loop = LOOP_VINFO_LOOP (loop_vinfo);
1850 if (!find_loop_nest (loop, &LOOP_VINFO_LOOP_NEST (loop_vinfo)))
1852 if (dump_enabled_p ())
1853 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1854 "not vectorized: loop nest containing two "
1855 "or more consecutive inner loops cannot be "
1856 "vectorized\n");
1857 return false;
1860 for (unsigned i = 0; i < loop->num_nodes; i++)
1861 for (gimple_stmt_iterator gsi = gsi_start_bb (bbs[i]);
1862 !gsi_end_p (gsi); gsi_next (&gsi))
1864 gimple *stmt = gsi_stmt (gsi);
1865 if (is_gimple_debug (stmt))
1866 continue;
1867 ++n_stmts;
1868 if (!find_data_references_in_stmt (loop, stmt,
1869 &LOOP_VINFO_DATAREFS (loop_vinfo)))
1871 if (is_gimple_call (stmt) && loop->safelen)
1873 tree fndecl = gimple_call_fndecl (stmt), op;
1874 if (fndecl != NULL_TREE)
1876 cgraph_node *node = cgraph_node::get (fndecl);
1877 if (node != NULL && node->simd_clones != NULL)
1879 unsigned int j, n = gimple_call_num_args (stmt);
1880 for (j = 0; j < n; j++)
1882 op = gimple_call_arg (stmt, j);
1883 if (DECL_P (op)
1884 || (REFERENCE_CLASS_P (op)
1885 && get_base_address (op)))
1886 break;
1888 op = gimple_call_lhs (stmt);
1889 /* Ignore #pragma omp declare simd functions
1890 if they don't have data references in the
1891 call stmt itself. */
1892 if (j == n
1893 && !(op
1894 && (DECL_P (op)
1895 || (REFERENCE_CLASS_P (op)
1896 && get_base_address (op)))))
1897 continue;
1901 if (dump_enabled_p ())
1902 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1903 "not vectorized: loop contains function "
1904 "calls or data references that cannot "
1905 "be analyzed\n");
1906 return false;
1910 /* Analyze the data references and also adjust the minimal
1911 vectorization factor according to the loads and stores. */
1913 ok = vect_analyze_data_refs (loop_vinfo, &min_vf);
1914 if (!ok)
1916 if (dump_enabled_p ())
1917 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1918 "bad data references.\n");
1919 return false;
1922 /* Classify all cross-iteration scalar data-flow cycles.
1923 Cross-iteration cycles caused by virtual phis are analyzed separately. */
1924 vect_analyze_scalar_cycles (loop_vinfo);
1926 vect_pattern_recog (loop_vinfo);
1928 vect_fixup_scalar_cycles_with_patterns (loop_vinfo);
1930 /* Analyze the access patterns of the data-refs in the loop (consecutive,
1931 complex, etc.). FORNOW: Only handle consecutive access pattern. */
1933 ok = vect_analyze_data_ref_accesses (loop_vinfo);
1934 if (!ok)
1936 if (dump_enabled_p ())
1937 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1938 "bad data access.\n");
1939 return false;
1942 /* Data-flow analysis to detect stmts that do not need to be vectorized. */
1944 ok = vect_mark_stmts_to_be_vectorized (loop_vinfo);
1945 if (!ok)
1947 if (dump_enabled_p ())
1948 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1949 "unexpected pattern.\n");
1950 return false;
1953 /* While the rest of the analysis below depends on it in some way. */
1954 fatal = false;
1956 /* Analyze data dependences between the data-refs in the loop
1957 and adjust the maximum vectorization factor according to
1958 the dependences.
1959 FORNOW: fail at the first data dependence that we encounter. */
1961 ok = vect_analyze_data_ref_dependences (loop_vinfo, &max_vf);
1962 if (!ok
1963 || max_vf < min_vf)
1965 if (dump_enabled_p ())
1966 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1967 "bad data dependence.\n");
1968 return false;
1971 ok = vect_determine_vectorization_factor (loop_vinfo);
1972 if (!ok)
1974 if (dump_enabled_p ())
1975 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1976 "can't determine vectorization factor.\n");
1977 return false;
1979 if (max_vf < LOOP_VINFO_VECT_FACTOR (loop_vinfo))
1981 if (dump_enabled_p ())
1982 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1983 "bad data dependence.\n");
1984 return false;
1987 /* Compute the scalar iteration cost. */
1988 vect_compute_single_scalar_iteration_cost (loop_vinfo);
1990 int saved_vectorization_factor = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
1991 HOST_WIDE_INT estimated_niter;
1992 unsigned th;
1993 int min_scalar_loop_bound;
1995 /* Check the SLP opportunities in the loop, analyze and build SLP trees. */
1996 ok = vect_analyze_slp (loop_vinfo, n_stmts);
1997 if (!ok)
1998 return false;
2000 /* If there are any SLP instances mark them as pure_slp. */
2001 bool slp = vect_make_slp_decision (loop_vinfo);
2002 if (slp)
2004 /* Find stmts that need to be both vectorized and SLPed. */
2005 vect_detect_hybrid_slp (loop_vinfo);
2007 /* Update the vectorization factor based on the SLP decision. */
2008 vect_update_vf_for_slp (loop_vinfo);
2011 /* This is the point where we can re-start analysis with SLP forced off. */
2012 start_over:
2014 /* Now the vectorization factor is final. */
2015 unsigned vectorization_factor = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
2016 gcc_assert (vectorization_factor != 0);
2018 if (LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo) && dump_enabled_p ())
2019 dump_printf_loc (MSG_NOTE, vect_location,
2020 "vectorization_factor = %d, niters = "
2021 HOST_WIDE_INT_PRINT_DEC "\n", vectorization_factor,
2022 LOOP_VINFO_INT_NITERS (loop_vinfo));
2024 HOST_WIDE_INT max_niter
2025 = likely_max_stmt_executions_int (LOOP_VINFO_LOOP (loop_vinfo));
2026 if ((LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo)
2027 && (LOOP_VINFO_INT_NITERS (loop_vinfo) < vectorization_factor))
2028 || (max_niter != -1
2029 && (unsigned HOST_WIDE_INT) max_niter < vectorization_factor))
2031 if (dump_enabled_p ())
2032 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2033 "not vectorized: iteration count smaller than "
2034 "vectorization factor.\n");
2035 return false;
2038 /* Analyze the alignment of the data-refs in the loop.
2039 Fail if a data reference is found that cannot be vectorized. */
2041 ok = vect_analyze_data_refs_alignment (loop_vinfo);
2042 if (!ok)
2044 if (dump_enabled_p ())
2045 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2046 "bad data alignment.\n");
2047 return false;
2050 /* Prune the list of ddrs to be tested at run-time by versioning for alias.
2051 It is important to call pruning after vect_analyze_data_ref_accesses,
2052 since we use grouping information gathered by interleaving analysis. */
2053 ok = vect_prune_runtime_alias_test_list (loop_vinfo);
2054 if (!ok)
2055 return false;
2057 /* Do not invoke vect_enhance_data_refs_alignment for eplilogue
2058 vectorization. */
2059 if (!LOOP_VINFO_EPILOGUE_P (loop_vinfo))
2061 /* This pass will decide on using loop versioning and/or loop peeling in
2062 order to enhance the alignment of data references in the loop. */
2063 ok = vect_enhance_data_refs_alignment (loop_vinfo);
2064 if (!ok)
2066 if (dump_enabled_p ())
2067 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2068 "bad data alignment.\n");
2069 return false;
2073 if (slp)
2075 /* Analyze operations in the SLP instances. Note this may
2076 remove unsupported SLP instances which makes the above
2077 SLP kind detection invalid. */
2078 unsigned old_size = LOOP_VINFO_SLP_INSTANCES (loop_vinfo).length ();
2079 vect_slp_analyze_operations (LOOP_VINFO_SLP_INSTANCES (loop_vinfo),
2080 LOOP_VINFO_TARGET_COST_DATA (loop_vinfo));
2081 if (LOOP_VINFO_SLP_INSTANCES (loop_vinfo).length () != old_size)
2082 goto again;
2085 /* Scan all the remaining operations in the loop that are not subject
2086 to SLP and make sure they are vectorizable. */
2087 ok = vect_analyze_loop_operations (loop_vinfo);
2088 if (!ok)
2090 if (dump_enabled_p ())
2091 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2092 "bad operation or unsupported loop bound.\n");
2093 return false;
2096 /* If epilog loop is required because of data accesses with gaps,
2097 one additional iteration needs to be peeled. Check if there is
2098 enough iterations for vectorization. */
2099 if (LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo)
2100 && LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo))
2102 int vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
2103 tree scalar_niters = LOOP_VINFO_NITERSM1 (loop_vinfo);
2105 if (wi::to_widest (scalar_niters) < vf)
2107 if (dump_enabled_p ())
2108 dump_printf_loc (MSG_NOTE, vect_location,
2109 "loop has no enough iterations to support"
2110 " peeling for gaps.\n");
2111 return false;
2115 /* Analyze cost. Decide if worth while to vectorize. */
2116 int min_profitable_estimate, min_profitable_iters;
2117 vect_estimate_min_profitable_iters (loop_vinfo, &min_profitable_iters,
2118 &min_profitable_estimate);
2120 if (min_profitable_iters < 0)
2122 if (dump_enabled_p ())
2123 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2124 "not vectorized: vectorization not profitable.\n");
2125 if (dump_enabled_p ())
2126 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2127 "not vectorized: vector version will never be "
2128 "profitable.\n");
2129 goto again;
2132 min_scalar_loop_bound = ((PARAM_VALUE (PARAM_MIN_VECT_LOOP_BOUND)
2133 * vectorization_factor) - 1);
2135 /* Use the cost model only if it is more conservative than user specified
2136 threshold. */
2137 th = (unsigned) min_scalar_loop_bound;
2138 if (min_profitable_iters
2139 && (!min_scalar_loop_bound
2140 || min_profitable_iters > min_scalar_loop_bound))
2141 th = (unsigned) min_profitable_iters;
2143 LOOP_VINFO_COST_MODEL_THRESHOLD (loop_vinfo) = th;
2145 if (LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo)
2146 && LOOP_VINFO_INT_NITERS (loop_vinfo) <= th)
2148 if (dump_enabled_p ())
2149 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2150 "not vectorized: vectorization not profitable.\n");
2151 if (dump_enabled_p ())
2152 dump_printf_loc (MSG_NOTE, vect_location,
2153 "not vectorized: iteration count smaller than user "
2154 "specified loop bound parameter or minimum profitable "
2155 "iterations (whichever is more conservative).\n");
2156 goto again;
2159 estimated_niter
2160 = estimated_stmt_executions_int (LOOP_VINFO_LOOP (loop_vinfo));
2161 if (estimated_niter == -1)
2162 estimated_niter = max_niter;
2163 if (estimated_niter != -1
2164 && ((unsigned HOST_WIDE_INT) estimated_niter
2165 <= MAX (th, (unsigned)min_profitable_estimate)))
2167 if (dump_enabled_p ())
2168 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2169 "not vectorized: estimated iteration count too "
2170 "small.\n");
2171 if (dump_enabled_p ())
2172 dump_printf_loc (MSG_NOTE, vect_location,
2173 "not vectorized: estimated iteration count smaller "
2174 "than specified loop bound parameter or minimum "
2175 "profitable iterations (whichever is more "
2176 "conservative).\n");
2177 goto again;
2180 /* Decide whether we need to create an epilogue loop to handle
2181 remaining scalar iterations. */
2182 th = ((LOOP_VINFO_COST_MODEL_THRESHOLD (loop_vinfo) + 1)
2183 / LOOP_VINFO_VECT_FACTOR (loop_vinfo))
2184 * LOOP_VINFO_VECT_FACTOR (loop_vinfo);
2186 if (LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo)
2187 && LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo) > 0)
2189 if (ctz_hwi (LOOP_VINFO_INT_NITERS (loop_vinfo)
2190 - LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo))
2191 < exact_log2 (LOOP_VINFO_VECT_FACTOR (loop_vinfo)))
2192 LOOP_VINFO_PEELING_FOR_NITER (loop_vinfo) = true;
2194 else if (LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo)
2195 || (tree_ctz (LOOP_VINFO_NITERS (loop_vinfo))
2196 < (unsigned)exact_log2 (LOOP_VINFO_VECT_FACTOR (loop_vinfo))
2197 /* In case of versioning, check if the maximum number of
2198 iterations is greater than th. If they are identical,
2199 the epilogue is unnecessary. */
2200 && (!LOOP_REQUIRES_VERSIONING (loop_vinfo)
2201 || (unsigned HOST_WIDE_INT) max_niter > th)))
2202 LOOP_VINFO_PEELING_FOR_NITER (loop_vinfo) = true;
2204 /* If an epilogue loop is required make sure we can create one. */
2205 if (LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo)
2206 || LOOP_VINFO_PEELING_FOR_NITER (loop_vinfo))
2208 if (dump_enabled_p ())
2209 dump_printf_loc (MSG_NOTE, vect_location, "epilog loop required\n");
2210 if (!vect_can_advance_ivs_p (loop_vinfo)
2211 || !slpeel_can_duplicate_loop_p (LOOP_VINFO_LOOP (loop_vinfo),
2212 single_exit (LOOP_VINFO_LOOP
2213 (loop_vinfo))))
2215 if (dump_enabled_p ())
2216 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2217 "not vectorized: can't create required "
2218 "epilog loop\n");
2219 goto again;
2223 /* During peeling, we need to check if number of loop iterations is
2224 enough for both peeled prolog loop and vector loop. This check
2225 can be merged along with threshold check of loop versioning, so
2226 increase threshold for this case if necessary. */
2227 if (LOOP_REQUIRES_VERSIONING (loop_vinfo)
2228 && (LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo)
2229 || LOOP_VINFO_PEELING_FOR_NITER (loop_vinfo)))
2231 unsigned niters_th;
2233 /* Niters for peeled prolog loop. */
2234 if (LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo) < 0)
2236 struct data_reference *dr = LOOP_VINFO_UNALIGNED_DR (loop_vinfo);
2237 tree vectype = STMT_VINFO_VECTYPE (vinfo_for_stmt (DR_STMT (dr)));
2239 niters_th = TYPE_VECTOR_SUBPARTS (vectype) - 1;
2241 else
2242 niters_th = LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo);
2244 /* Niters for at least one iteration of vectorized loop. */
2245 niters_th += LOOP_VINFO_VECT_FACTOR (loop_vinfo);
2246 /* One additional iteration because of peeling for gap. */
2247 if (!LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo))
2248 niters_th++;
2249 if (LOOP_VINFO_COST_MODEL_THRESHOLD (loop_vinfo) < niters_th)
2250 LOOP_VINFO_COST_MODEL_THRESHOLD (loop_vinfo) = niters_th;
2253 gcc_assert (vectorization_factor
2254 == (unsigned)LOOP_VINFO_VECT_FACTOR (loop_vinfo));
2256 /* Ok to vectorize! */
2257 return true;
2259 again:
2260 /* Try again with SLP forced off but if we didn't do any SLP there is
2261 no point in re-trying. */
2262 if (!slp)
2263 return false;
2265 /* If there are reduction chains re-trying will fail anyway. */
2266 if (! LOOP_VINFO_REDUCTION_CHAINS (loop_vinfo).is_empty ())
2267 return false;
2269 /* Likewise if the grouped loads or stores in the SLP cannot be handled
2270 via interleaving or lane instructions. */
2271 slp_instance instance;
2272 slp_tree node;
2273 unsigned i, j;
2274 FOR_EACH_VEC_ELT (LOOP_VINFO_SLP_INSTANCES (loop_vinfo), i, instance)
2276 stmt_vec_info vinfo;
2277 vinfo = vinfo_for_stmt
2278 (SLP_TREE_SCALAR_STMTS (SLP_INSTANCE_TREE (instance))[0]);
2279 if (! STMT_VINFO_GROUPED_ACCESS (vinfo))
2280 continue;
2281 vinfo = vinfo_for_stmt (STMT_VINFO_GROUP_FIRST_ELEMENT (vinfo));
2282 unsigned int size = STMT_VINFO_GROUP_SIZE (vinfo);
2283 tree vectype = STMT_VINFO_VECTYPE (vinfo);
2284 if (! vect_store_lanes_supported (vectype, size)
2285 && ! vect_grouped_store_supported (vectype, size))
2286 return false;
2287 FOR_EACH_VEC_ELT (SLP_INSTANCE_LOADS (instance), j, node)
2289 vinfo = vinfo_for_stmt (SLP_TREE_SCALAR_STMTS (node)[0]);
2290 vinfo = vinfo_for_stmt (STMT_VINFO_GROUP_FIRST_ELEMENT (vinfo));
2291 bool single_element_p = !STMT_VINFO_GROUP_NEXT_ELEMENT (vinfo);
2292 size = STMT_VINFO_GROUP_SIZE (vinfo);
2293 vectype = STMT_VINFO_VECTYPE (vinfo);
2294 if (! vect_load_lanes_supported (vectype, size)
2295 && ! vect_grouped_load_supported (vectype, single_element_p,
2296 size))
2297 return false;
2301 if (dump_enabled_p ())
2302 dump_printf_loc (MSG_NOTE, vect_location,
2303 "re-trying with SLP disabled\n");
2305 /* Roll back state appropriately. No SLP this time. */
2306 slp = false;
2307 /* Restore vectorization factor as it were without SLP. */
2308 LOOP_VINFO_VECT_FACTOR (loop_vinfo) = saved_vectorization_factor;
2309 /* Free the SLP instances. */
2310 FOR_EACH_VEC_ELT (LOOP_VINFO_SLP_INSTANCES (loop_vinfo), j, instance)
2311 vect_free_slp_instance (instance);
2312 LOOP_VINFO_SLP_INSTANCES (loop_vinfo).release ();
2313 /* Reset SLP type to loop_vect on all stmts. */
2314 for (i = 0; i < LOOP_VINFO_LOOP (loop_vinfo)->num_nodes; ++i)
2316 basic_block bb = LOOP_VINFO_BBS (loop_vinfo)[i];
2317 for (gimple_stmt_iterator si = gsi_start_phis (bb);
2318 !gsi_end_p (si); gsi_next (&si))
2320 stmt_vec_info stmt_info = vinfo_for_stmt (gsi_stmt (si));
2321 STMT_SLP_TYPE (stmt_info) = loop_vect;
2323 for (gimple_stmt_iterator si = gsi_start_bb (bb);
2324 !gsi_end_p (si); gsi_next (&si))
2326 stmt_vec_info stmt_info = vinfo_for_stmt (gsi_stmt (si));
2327 STMT_SLP_TYPE (stmt_info) = loop_vect;
2328 if (STMT_VINFO_IN_PATTERN_P (stmt_info))
2330 stmt_info = vinfo_for_stmt (STMT_VINFO_RELATED_STMT (stmt_info));
2331 STMT_SLP_TYPE (stmt_info) = loop_vect;
2332 for (gimple_stmt_iterator pi
2333 = gsi_start (STMT_VINFO_PATTERN_DEF_SEQ (stmt_info));
2334 !gsi_end_p (pi); gsi_next (&pi))
2336 gimple *pstmt = gsi_stmt (pi);
2337 STMT_SLP_TYPE (vinfo_for_stmt (pstmt)) = loop_vect;
2342 /* Free optimized alias test DDRS. */
2343 LOOP_VINFO_COMP_ALIAS_DDRS (loop_vinfo).release ();
2344 /* Reset target cost data. */
2345 destroy_cost_data (LOOP_VINFO_TARGET_COST_DATA (loop_vinfo));
2346 LOOP_VINFO_TARGET_COST_DATA (loop_vinfo)
2347 = init_cost (LOOP_VINFO_LOOP (loop_vinfo));
2348 /* Reset assorted flags. */
2349 LOOP_VINFO_PEELING_FOR_NITER (loop_vinfo) = false;
2350 LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo) = false;
2351 LOOP_VINFO_COST_MODEL_THRESHOLD (loop_vinfo) = 0;
2353 goto start_over;
2356 /* Function vect_analyze_loop.
2358 Apply a set of analyses on LOOP, and create a loop_vec_info struct
2359 for it. The different analyses will record information in the
2360 loop_vec_info struct. If ORIG_LOOP_VINFO is not NULL epilogue must
2361 be vectorized. */
2362 loop_vec_info
2363 vect_analyze_loop (struct loop *loop, loop_vec_info orig_loop_vinfo)
2365 loop_vec_info loop_vinfo;
2366 unsigned int vector_sizes;
2368 /* Autodetect first vector size we try. */
2369 current_vector_size = 0;
2370 vector_sizes = targetm.vectorize.autovectorize_vector_sizes ();
2372 if (dump_enabled_p ())
2373 dump_printf_loc (MSG_NOTE, vect_location,
2374 "===== analyze_loop_nest =====\n");
2376 if (loop_outer (loop)
2377 && loop_vec_info_for_loop (loop_outer (loop))
2378 && LOOP_VINFO_VECTORIZABLE_P (loop_vec_info_for_loop (loop_outer (loop))))
2380 if (dump_enabled_p ())
2381 dump_printf_loc (MSG_NOTE, vect_location,
2382 "outer-loop already vectorized.\n");
2383 return NULL;
2386 while (1)
2388 /* Check the CFG characteristics of the loop (nesting, entry/exit). */
2389 loop_vinfo = vect_analyze_loop_form (loop);
2390 if (!loop_vinfo)
2392 if (dump_enabled_p ())
2393 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2394 "bad loop form.\n");
2395 return NULL;
2398 bool fatal = false;
2400 if (orig_loop_vinfo)
2401 LOOP_VINFO_ORIG_LOOP_INFO (loop_vinfo) = orig_loop_vinfo;
2403 if (vect_analyze_loop_2 (loop_vinfo, fatal))
2405 LOOP_VINFO_VECTORIZABLE_P (loop_vinfo) = 1;
2407 return loop_vinfo;
2410 destroy_loop_vec_info (loop_vinfo, true);
2412 vector_sizes &= ~current_vector_size;
2413 if (fatal
2414 || vector_sizes == 0
2415 || current_vector_size == 0)
2416 return NULL;
2418 /* Try the next biggest vector size. */
2419 current_vector_size = 1 << floor_log2 (vector_sizes);
2420 if (dump_enabled_p ())
2421 dump_printf_loc (MSG_NOTE, vect_location,
2422 "***** Re-trying analysis with "
2423 "vector size %d\n", current_vector_size);
2428 /* Function reduction_code_for_scalar_code
2430 Input:
2431 CODE - tree_code of a reduction operations.
2433 Output:
2434 REDUC_CODE - the corresponding tree-code to be used to reduce the
2435 vector of partial results into a single scalar result, or ERROR_MARK
2436 if the operation is a supported reduction operation, but does not have
2437 such a tree-code.
2439 Return FALSE if CODE currently cannot be vectorized as reduction. */
2441 static bool
2442 reduction_code_for_scalar_code (enum tree_code code,
2443 enum tree_code *reduc_code)
2445 switch (code)
2447 case MAX_EXPR:
2448 *reduc_code = REDUC_MAX_EXPR;
2449 return true;
2451 case MIN_EXPR:
2452 *reduc_code = REDUC_MIN_EXPR;
2453 return true;
2455 case PLUS_EXPR:
2456 *reduc_code = REDUC_PLUS_EXPR;
2457 return true;
2459 case MULT_EXPR:
2460 case MINUS_EXPR:
2461 case BIT_IOR_EXPR:
2462 case BIT_XOR_EXPR:
2463 case BIT_AND_EXPR:
2464 *reduc_code = ERROR_MARK;
2465 return true;
2467 default:
2468 return false;
2473 /* Error reporting helper for vect_is_simple_reduction below. GIMPLE statement
2474 STMT is printed with a message MSG. */
2476 static void
2477 report_vect_op (dump_flags_t msg_type, gimple *stmt, const char *msg)
2479 dump_printf_loc (msg_type, vect_location, "%s", msg);
2480 dump_gimple_stmt (msg_type, TDF_SLIM, stmt, 0);
2484 /* Detect SLP reduction of the form:
2486 #a1 = phi <a5, a0>
2487 a2 = operation (a1)
2488 a3 = operation (a2)
2489 a4 = operation (a3)
2490 a5 = operation (a4)
2492 #a = phi <a5>
2494 PHI is the reduction phi node (#a1 = phi <a5, a0> above)
2495 FIRST_STMT is the first reduction stmt in the chain
2496 (a2 = operation (a1)).
2498 Return TRUE if a reduction chain was detected. */
2500 static bool
2501 vect_is_slp_reduction (loop_vec_info loop_info, gimple *phi,
2502 gimple *first_stmt)
2504 struct loop *loop = (gimple_bb (phi))->loop_father;
2505 struct loop *vect_loop = LOOP_VINFO_LOOP (loop_info);
2506 enum tree_code code;
2507 gimple *current_stmt = NULL, *loop_use_stmt = NULL, *first, *next_stmt;
2508 stmt_vec_info use_stmt_info, current_stmt_info;
2509 tree lhs;
2510 imm_use_iterator imm_iter;
2511 use_operand_p use_p;
2512 int nloop_uses, size = 0, n_out_of_loop_uses;
2513 bool found = false;
2515 if (loop != vect_loop)
2516 return false;
2518 lhs = PHI_RESULT (phi);
2519 code = gimple_assign_rhs_code (first_stmt);
2520 while (1)
2522 nloop_uses = 0;
2523 n_out_of_loop_uses = 0;
2524 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, lhs)
2526 gimple *use_stmt = USE_STMT (use_p);
2527 if (is_gimple_debug (use_stmt))
2528 continue;
2530 /* Check if we got back to the reduction phi. */
2531 if (use_stmt == phi)
2533 loop_use_stmt = use_stmt;
2534 found = true;
2535 break;
2538 if (flow_bb_inside_loop_p (loop, gimple_bb (use_stmt)))
2540 loop_use_stmt = use_stmt;
2541 nloop_uses++;
2543 else
2544 n_out_of_loop_uses++;
2546 /* There are can be either a single use in the loop or two uses in
2547 phi nodes. */
2548 if (nloop_uses > 1 || (n_out_of_loop_uses && nloop_uses))
2549 return false;
2552 if (found)
2553 break;
2555 /* We reached a statement with no loop uses. */
2556 if (nloop_uses == 0)
2557 return false;
2559 /* This is a loop exit phi, and we haven't reached the reduction phi. */
2560 if (gimple_code (loop_use_stmt) == GIMPLE_PHI)
2561 return false;
2563 if (!is_gimple_assign (loop_use_stmt)
2564 || code != gimple_assign_rhs_code (loop_use_stmt)
2565 || !flow_bb_inside_loop_p (loop, gimple_bb (loop_use_stmt)))
2566 return false;
2568 /* Insert USE_STMT into reduction chain. */
2569 use_stmt_info = vinfo_for_stmt (loop_use_stmt);
2570 if (current_stmt)
2572 current_stmt_info = vinfo_for_stmt (current_stmt);
2573 GROUP_NEXT_ELEMENT (current_stmt_info) = loop_use_stmt;
2574 GROUP_FIRST_ELEMENT (use_stmt_info)
2575 = GROUP_FIRST_ELEMENT (current_stmt_info);
2577 else
2578 GROUP_FIRST_ELEMENT (use_stmt_info) = loop_use_stmt;
2580 lhs = gimple_assign_lhs (loop_use_stmt);
2581 current_stmt = loop_use_stmt;
2582 size++;
2585 if (!found || loop_use_stmt != phi || size < 2)
2586 return false;
2588 /* Swap the operands, if needed, to make the reduction operand be the second
2589 operand. */
2590 lhs = PHI_RESULT (phi);
2591 next_stmt = GROUP_FIRST_ELEMENT (vinfo_for_stmt (current_stmt));
2592 while (next_stmt)
2594 if (gimple_assign_rhs2 (next_stmt) == lhs)
2596 tree op = gimple_assign_rhs1 (next_stmt);
2597 gimple *def_stmt = NULL;
2599 if (TREE_CODE (op) == SSA_NAME)
2600 def_stmt = SSA_NAME_DEF_STMT (op);
2602 /* Check that the other def is either defined in the loop
2603 ("vect_internal_def"), or it's an induction (defined by a
2604 loop-header phi-node). */
2605 if (def_stmt
2606 && gimple_bb (def_stmt)
2607 && flow_bb_inside_loop_p (loop, gimple_bb (def_stmt))
2608 && (is_gimple_assign (def_stmt)
2609 || is_gimple_call (def_stmt)
2610 || STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def_stmt))
2611 == vect_induction_def
2612 || (gimple_code (def_stmt) == GIMPLE_PHI
2613 && STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def_stmt))
2614 == vect_internal_def
2615 && !is_loop_header_bb_p (gimple_bb (def_stmt)))))
2617 lhs = gimple_assign_lhs (next_stmt);
2618 next_stmt = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next_stmt));
2619 continue;
2622 return false;
2624 else
2626 tree op = gimple_assign_rhs2 (next_stmt);
2627 gimple *def_stmt = NULL;
2629 if (TREE_CODE (op) == SSA_NAME)
2630 def_stmt = SSA_NAME_DEF_STMT (op);
2632 /* Check that the other def is either defined in the loop
2633 ("vect_internal_def"), or it's an induction (defined by a
2634 loop-header phi-node). */
2635 if (def_stmt
2636 && gimple_bb (def_stmt)
2637 && flow_bb_inside_loop_p (loop, gimple_bb (def_stmt))
2638 && (is_gimple_assign (def_stmt)
2639 || is_gimple_call (def_stmt)
2640 || STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def_stmt))
2641 == vect_induction_def
2642 || (gimple_code (def_stmt) == GIMPLE_PHI
2643 && STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def_stmt))
2644 == vect_internal_def
2645 && !is_loop_header_bb_p (gimple_bb (def_stmt)))))
2647 if (dump_enabled_p ())
2649 dump_printf_loc (MSG_NOTE, vect_location, "swapping oprnds: ");
2650 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, next_stmt, 0);
2653 swap_ssa_operands (next_stmt,
2654 gimple_assign_rhs1_ptr (next_stmt),
2655 gimple_assign_rhs2_ptr (next_stmt));
2656 update_stmt (next_stmt);
2658 if (CONSTANT_CLASS_P (gimple_assign_rhs1 (next_stmt)))
2659 LOOP_VINFO_OPERANDS_SWAPPED (loop_info) = true;
2661 else
2662 return false;
2665 lhs = gimple_assign_lhs (next_stmt);
2666 next_stmt = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next_stmt));
2669 /* Save the chain for further analysis in SLP detection. */
2670 first = GROUP_FIRST_ELEMENT (vinfo_for_stmt (current_stmt));
2671 LOOP_VINFO_REDUCTION_CHAINS (loop_info).safe_push (first);
2672 GROUP_SIZE (vinfo_for_stmt (first)) = size;
2674 return true;
2678 /* Function vect_is_simple_reduction
2680 (1) Detect a cross-iteration def-use cycle that represents a simple
2681 reduction computation. We look for the following pattern:
2683 loop_header:
2684 a1 = phi < a0, a2 >
2685 a3 = ...
2686 a2 = operation (a3, a1)
2690 a3 = ...
2691 loop_header:
2692 a1 = phi < a0, a2 >
2693 a2 = operation (a3, a1)
2695 such that:
2696 1. operation is commutative and associative and it is safe to
2697 change the order of the computation
2698 2. no uses for a2 in the loop (a2 is used out of the loop)
2699 3. no uses of a1 in the loop besides the reduction operation
2700 4. no uses of a1 outside the loop.
2702 Conditions 1,4 are tested here.
2703 Conditions 2,3 are tested in vect_mark_stmts_to_be_vectorized.
2705 (2) Detect a cross-iteration def-use cycle in nested loops, i.e.,
2706 nested cycles.
2708 (3) Detect cycles of phi nodes in outer-loop vectorization, i.e., double
2709 reductions:
2711 a1 = phi < a0, a2 >
2712 inner loop (def of a3)
2713 a2 = phi < a3 >
2715 (4) Detect condition expressions, ie:
2716 for (int i = 0; i < N; i++)
2717 if (a[i] < val)
2718 ret_val = a[i];
2722 static gimple *
2723 vect_is_simple_reduction (loop_vec_info loop_info, gimple *phi,
2724 bool *double_reduc,
2725 bool need_wrapping_integral_overflow,
2726 enum vect_reduction_type *v_reduc_type)
2728 struct loop *loop = (gimple_bb (phi))->loop_father;
2729 struct loop *vect_loop = LOOP_VINFO_LOOP (loop_info);
2730 gimple *def_stmt, *def1 = NULL, *def2 = NULL, *phi_use_stmt = NULL;
2731 enum tree_code orig_code, code;
2732 tree op1, op2, op3 = NULL_TREE, op4 = NULL_TREE;
2733 tree type;
2734 int nloop_uses;
2735 tree name;
2736 imm_use_iterator imm_iter;
2737 use_operand_p use_p;
2738 bool phi_def;
2740 *double_reduc = false;
2741 *v_reduc_type = TREE_CODE_REDUCTION;
2743 name = PHI_RESULT (phi);
2744 /* ??? If there are no uses of the PHI result the inner loop reduction
2745 won't be detected as possibly double-reduction by vectorizable_reduction
2746 because that tries to walk the PHI arg from the preheader edge which
2747 can be constant. See PR60382. */
2748 if (has_zero_uses (name))
2749 return NULL;
2750 nloop_uses = 0;
2751 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, name)
2753 gimple *use_stmt = USE_STMT (use_p);
2754 if (is_gimple_debug (use_stmt))
2755 continue;
2757 if (!flow_bb_inside_loop_p (loop, gimple_bb (use_stmt)))
2759 if (dump_enabled_p ())
2760 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2761 "intermediate value used outside loop.\n");
2763 return NULL;
2766 nloop_uses++;
2767 if (nloop_uses > 1)
2769 if (dump_enabled_p ())
2770 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2771 "reduction value used in loop.\n");
2772 return NULL;
2775 phi_use_stmt = use_stmt;
2778 edge latch_e = loop_latch_edge (loop);
2779 tree loop_arg = PHI_ARG_DEF_FROM_EDGE (phi, latch_e);
2780 if (TREE_CODE (loop_arg) != SSA_NAME)
2782 if (dump_enabled_p ())
2784 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2785 "reduction: not ssa_name: ");
2786 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, loop_arg);
2787 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
2789 return NULL;
2792 def_stmt = SSA_NAME_DEF_STMT (loop_arg);
2793 if (is_gimple_assign (def_stmt))
2795 name = gimple_assign_lhs (def_stmt);
2796 phi_def = false;
2798 else if (gimple_code (def_stmt) == GIMPLE_PHI)
2800 name = PHI_RESULT (def_stmt);
2801 phi_def = true;
2803 else
2805 if (dump_enabled_p ())
2807 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2808 "reduction: unhandled reduction operation: ");
2809 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, def_stmt, 0);
2811 return NULL;
2814 nloop_uses = 0;
2815 auto_vec<gphi *, 3> lcphis;
2816 if (flow_bb_inside_loop_p (loop, gimple_bb (def_stmt)))
2817 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, name)
2819 gimple *use_stmt = USE_STMT (use_p);
2820 if (is_gimple_debug (use_stmt))
2821 continue;
2822 if (flow_bb_inside_loop_p (loop, gimple_bb (use_stmt)))
2823 nloop_uses++;
2824 else
2825 /* We can have more than one loop-closed PHI. */
2826 lcphis.safe_push (as_a <gphi *> (use_stmt));
2827 if (nloop_uses > 1)
2829 if (dump_enabled_p ())
2830 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2831 "reduction used in loop.\n");
2832 return NULL;
2836 /* If DEF_STMT is a phi node itself, we expect it to have a single argument
2837 defined in the inner loop. */
2838 if (phi_def)
2840 op1 = PHI_ARG_DEF (def_stmt, 0);
2842 if (gimple_phi_num_args (def_stmt) != 1
2843 || TREE_CODE (op1) != SSA_NAME)
2845 if (dump_enabled_p ())
2846 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2847 "unsupported phi node definition.\n");
2849 return NULL;
2852 def1 = SSA_NAME_DEF_STMT (op1);
2853 if (gimple_bb (def1)
2854 && flow_bb_inside_loop_p (loop, gimple_bb (def_stmt))
2855 && loop->inner
2856 && flow_bb_inside_loop_p (loop->inner, gimple_bb (def1))
2857 && is_gimple_assign (def1)
2858 && flow_bb_inside_loop_p (loop->inner, gimple_bb (phi_use_stmt)))
2860 if (dump_enabled_p ())
2861 report_vect_op (MSG_NOTE, def_stmt,
2862 "detected double reduction: ");
2864 *double_reduc = true;
2865 return def_stmt;
2868 return NULL;
2871 /* If we are vectorizing an inner reduction we are executing that
2872 in the original order only in case we are not dealing with a
2873 double reduction. */
2874 bool check_reduction = true;
2875 if (flow_loop_nested_p (vect_loop, loop))
2877 gphi *lcphi;
2878 unsigned i;
2879 check_reduction = false;
2880 FOR_EACH_VEC_ELT (lcphis, i, lcphi)
2881 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, gimple_phi_result (lcphi))
2883 gimple *use_stmt = USE_STMT (use_p);
2884 if (is_gimple_debug (use_stmt))
2885 continue;
2886 if (! flow_bb_inside_loop_p (vect_loop, gimple_bb (use_stmt)))
2887 check_reduction = true;
2891 bool nested_in_vect_loop = flow_loop_nested_p (vect_loop, loop);
2892 code = orig_code = gimple_assign_rhs_code (def_stmt);
2894 /* We can handle "res -= x[i]", which is non-associative by
2895 simply rewriting this into "res += -x[i]". Avoid changing
2896 gimple instruction for the first simple tests and only do this
2897 if we're allowed to change code at all. */
2898 if (code == MINUS_EXPR
2899 && (op1 = gimple_assign_rhs1 (def_stmt))
2900 && TREE_CODE (op1) == SSA_NAME
2901 && SSA_NAME_DEF_STMT (op1) == phi)
2902 code = PLUS_EXPR;
2904 if (code == COND_EXPR)
2906 if (! nested_in_vect_loop)
2907 *v_reduc_type = COND_REDUCTION;
2909 op3 = gimple_assign_rhs1 (def_stmt);
2910 if (COMPARISON_CLASS_P (op3))
2912 op4 = TREE_OPERAND (op3, 1);
2913 op3 = TREE_OPERAND (op3, 0);
2916 op1 = gimple_assign_rhs2 (def_stmt);
2917 op2 = gimple_assign_rhs3 (def_stmt);
2919 else if (!commutative_tree_code (code) || !associative_tree_code (code))
2921 if (dump_enabled_p ())
2922 report_vect_op (MSG_MISSED_OPTIMIZATION, def_stmt,
2923 "reduction: not commutative/associative: ");
2924 return NULL;
2926 else if (get_gimple_rhs_class (code) == GIMPLE_BINARY_RHS)
2928 op1 = gimple_assign_rhs1 (def_stmt);
2929 op2 = gimple_assign_rhs2 (def_stmt);
2931 else
2933 if (dump_enabled_p ())
2934 report_vect_op (MSG_MISSED_OPTIMIZATION, def_stmt,
2935 "reduction: not handled operation: ");
2936 return NULL;
2939 if (TREE_CODE (op1) != SSA_NAME && TREE_CODE (op2) != SSA_NAME)
2941 if (dump_enabled_p ())
2942 report_vect_op (MSG_MISSED_OPTIMIZATION, def_stmt,
2943 "reduction: both uses not ssa_names: ");
2945 return NULL;
2948 type = TREE_TYPE (gimple_assign_lhs (def_stmt));
2949 if ((TREE_CODE (op1) == SSA_NAME
2950 && !types_compatible_p (type,TREE_TYPE (op1)))
2951 || (TREE_CODE (op2) == SSA_NAME
2952 && !types_compatible_p (type, TREE_TYPE (op2)))
2953 || (op3 && TREE_CODE (op3) == SSA_NAME
2954 && !types_compatible_p (type, TREE_TYPE (op3)))
2955 || (op4 && TREE_CODE (op4) == SSA_NAME
2956 && !types_compatible_p (type, TREE_TYPE (op4))))
2958 if (dump_enabled_p ())
2960 dump_printf_loc (MSG_NOTE, vect_location,
2961 "reduction: multiple types: operation type: ");
2962 dump_generic_expr (MSG_NOTE, TDF_SLIM, type);
2963 dump_printf (MSG_NOTE, ", operands types: ");
2964 dump_generic_expr (MSG_NOTE, TDF_SLIM,
2965 TREE_TYPE (op1));
2966 dump_printf (MSG_NOTE, ",");
2967 dump_generic_expr (MSG_NOTE, TDF_SLIM,
2968 TREE_TYPE (op2));
2969 if (op3)
2971 dump_printf (MSG_NOTE, ",");
2972 dump_generic_expr (MSG_NOTE, TDF_SLIM,
2973 TREE_TYPE (op3));
2976 if (op4)
2978 dump_printf (MSG_NOTE, ",");
2979 dump_generic_expr (MSG_NOTE, TDF_SLIM,
2980 TREE_TYPE (op4));
2982 dump_printf (MSG_NOTE, "\n");
2985 return NULL;
2988 /* Check that it's ok to change the order of the computation.
2989 Generally, when vectorizing a reduction we change the order of the
2990 computation. This may change the behavior of the program in some
2991 cases, so we need to check that this is ok. One exception is when
2992 vectorizing an outer-loop: the inner-loop is executed sequentially,
2993 and therefore vectorizing reductions in the inner-loop during
2994 outer-loop vectorization is safe. */
2996 if (*v_reduc_type != COND_REDUCTION
2997 && check_reduction)
2999 /* CHECKME: check for !flag_finite_math_only too? */
3000 if (SCALAR_FLOAT_TYPE_P (type) && !flag_associative_math)
3002 /* Changing the order of operations changes the semantics. */
3003 if (dump_enabled_p ())
3004 report_vect_op (MSG_MISSED_OPTIMIZATION, def_stmt,
3005 "reduction: unsafe fp math optimization: ");
3006 return NULL;
3008 else if (INTEGRAL_TYPE_P (type))
3010 if (!operation_no_trapping_overflow (type, code))
3012 /* Changing the order of operations changes the semantics. */
3013 if (dump_enabled_p ())
3014 report_vect_op (MSG_MISSED_OPTIMIZATION, def_stmt,
3015 "reduction: unsafe int math optimization"
3016 " (overflow traps): ");
3017 return NULL;
3019 if (need_wrapping_integral_overflow
3020 && !TYPE_OVERFLOW_WRAPS (type)
3021 && operation_can_overflow (code))
3023 /* Changing the order of operations changes the semantics. */
3024 if (dump_enabled_p ())
3025 report_vect_op (MSG_MISSED_OPTIMIZATION, def_stmt,
3026 "reduction: unsafe int math optimization"
3027 " (overflow doesn't wrap): ");
3028 return NULL;
3031 else if (SAT_FIXED_POINT_TYPE_P (type))
3033 /* Changing the order of operations changes the semantics. */
3034 if (dump_enabled_p ())
3035 report_vect_op (MSG_MISSED_OPTIMIZATION, def_stmt,
3036 "reduction: unsafe fixed-point math optimization: ");
3037 return NULL;
3041 /* Reduction is safe. We're dealing with one of the following:
3042 1) integer arithmetic and no trapv
3043 2) floating point arithmetic, and special flags permit this optimization
3044 3) nested cycle (i.e., outer loop vectorization). */
3045 if (TREE_CODE (op1) == SSA_NAME)
3046 def1 = SSA_NAME_DEF_STMT (op1);
3048 if (TREE_CODE (op2) == SSA_NAME)
3049 def2 = SSA_NAME_DEF_STMT (op2);
3051 if (code != COND_EXPR
3052 && ((!def1 || gimple_nop_p (def1)) && (!def2 || gimple_nop_p (def2))))
3054 if (dump_enabled_p ())
3055 report_vect_op (MSG_NOTE, def_stmt, "reduction: no defs for operands: ");
3056 return NULL;
3059 /* Check that one def is the reduction def, defined by PHI,
3060 the other def is either defined in the loop ("vect_internal_def"),
3061 or it's an induction (defined by a loop-header phi-node). */
3063 if (def2 && def2 == phi
3064 && (code == COND_EXPR
3065 || !def1 || gimple_nop_p (def1)
3066 || !flow_bb_inside_loop_p (loop, gimple_bb (def1))
3067 || (def1 && flow_bb_inside_loop_p (loop, gimple_bb (def1))
3068 && (is_gimple_assign (def1)
3069 || is_gimple_call (def1)
3070 || STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def1))
3071 == vect_induction_def
3072 || (gimple_code (def1) == GIMPLE_PHI
3073 && STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def1))
3074 == vect_internal_def
3075 && !is_loop_header_bb_p (gimple_bb (def1)))))))
3077 if (dump_enabled_p ())
3078 report_vect_op (MSG_NOTE, def_stmt, "detected reduction: ");
3079 return def_stmt;
3082 if (def1 && def1 == phi
3083 && (code == COND_EXPR
3084 || !def2 || gimple_nop_p (def2)
3085 || !flow_bb_inside_loop_p (loop, gimple_bb (def2))
3086 || (def2 && flow_bb_inside_loop_p (loop, gimple_bb (def2))
3087 && (is_gimple_assign (def2)
3088 || is_gimple_call (def2)
3089 || STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def2))
3090 == vect_induction_def
3091 || (gimple_code (def2) == GIMPLE_PHI
3092 && STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def2))
3093 == vect_internal_def
3094 && !is_loop_header_bb_p (gimple_bb (def2)))))))
3096 if (! nested_in_vect_loop && orig_code != MINUS_EXPR)
3098 /* Check if we can swap operands (just for simplicity - so that
3099 the rest of the code can assume that the reduction variable
3100 is always the last (second) argument). */
3101 if (code == COND_EXPR)
3103 /* Swap cond_expr by inverting the condition. */
3104 tree cond_expr = gimple_assign_rhs1 (def_stmt);
3105 enum tree_code invert_code = ERROR_MARK;
3106 enum tree_code cond_code = TREE_CODE (cond_expr);
3108 if (TREE_CODE_CLASS (cond_code) == tcc_comparison)
3110 bool honor_nans = HONOR_NANS (TREE_OPERAND (cond_expr, 0));
3111 invert_code = invert_tree_comparison (cond_code, honor_nans);
3113 if (invert_code != ERROR_MARK)
3115 TREE_SET_CODE (cond_expr, invert_code);
3116 swap_ssa_operands (def_stmt,
3117 gimple_assign_rhs2_ptr (def_stmt),
3118 gimple_assign_rhs3_ptr (def_stmt));
3120 else
3122 if (dump_enabled_p ())
3123 report_vect_op (MSG_NOTE, def_stmt,
3124 "detected reduction: cannot swap operands "
3125 "for cond_expr");
3126 return NULL;
3129 else
3130 swap_ssa_operands (def_stmt, gimple_assign_rhs1_ptr (def_stmt),
3131 gimple_assign_rhs2_ptr (def_stmt));
3133 if (dump_enabled_p ())
3134 report_vect_op (MSG_NOTE, def_stmt,
3135 "detected reduction: need to swap operands: ");
3137 if (CONSTANT_CLASS_P (gimple_assign_rhs1 (def_stmt)))
3138 LOOP_VINFO_OPERANDS_SWAPPED (loop_info) = true;
3140 else
3142 if (dump_enabled_p ())
3143 report_vect_op (MSG_NOTE, def_stmt, "detected reduction: ");
3146 return def_stmt;
3149 /* Try to find SLP reduction chain. */
3150 if (! nested_in_vect_loop
3151 && code != COND_EXPR
3152 && vect_is_slp_reduction (loop_info, phi, def_stmt))
3154 if (dump_enabled_p ())
3155 report_vect_op (MSG_NOTE, def_stmt,
3156 "reduction: detected reduction chain: ");
3158 return def_stmt;
3161 if (dump_enabled_p ())
3162 report_vect_op (MSG_MISSED_OPTIMIZATION, def_stmt,
3163 "reduction: unknown pattern: ");
3165 return NULL;
3168 /* Wrapper around vect_is_simple_reduction, which will modify code
3169 in-place if it enables detection of more reductions. Arguments
3170 as there. */
3172 gimple *
3173 vect_force_simple_reduction (loop_vec_info loop_info, gimple *phi,
3174 bool *double_reduc,
3175 bool need_wrapping_integral_overflow)
3177 enum vect_reduction_type v_reduc_type;
3178 gimple *def = vect_is_simple_reduction (loop_info, phi, double_reduc,
3179 need_wrapping_integral_overflow,
3180 &v_reduc_type);
3181 if (def)
3183 stmt_vec_info reduc_def_info = vinfo_for_stmt (phi);
3184 STMT_VINFO_REDUC_TYPE (reduc_def_info) = v_reduc_type;
3185 STMT_VINFO_REDUC_DEF (reduc_def_info) = def;
3187 return def;
3190 /* Calculate cost of peeling the loop PEEL_ITERS_PROLOGUE times. */
3192 vect_get_known_peeling_cost (loop_vec_info loop_vinfo, int peel_iters_prologue,
3193 int *peel_iters_epilogue,
3194 stmt_vector_for_cost *scalar_cost_vec,
3195 stmt_vector_for_cost *prologue_cost_vec,
3196 stmt_vector_for_cost *epilogue_cost_vec)
3198 int retval = 0;
3199 int vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
3201 if (!LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo))
3203 *peel_iters_epilogue = vf/2;
3204 if (dump_enabled_p ())
3205 dump_printf_loc (MSG_NOTE, vect_location,
3206 "cost model: epilogue peel iters set to vf/2 "
3207 "because loop iterations are unknown .\n");
3209 /* If peeled iterations are known but number of scalar loop
3210 iterations are unknown, count a taken branch per peeled loop. */
3211 retval = record_stmt_cost (prologue_cost_vec, 1, cond_branch_taken,
3212 NULL, 0, vect_prologue);
3213 retval = record_stmt_cost (prologue_cost_vec, 1, cond_branch_taken,
3214 NULL, 0, vect_epilogue);
3216 else
3218 int niters = LOOP_VINFO_INT_NITERS (loop_vinfo);
3219 peel_iters_prologue = niters < peel_iters_prologue ?
3220 niters : peel_iters_prologue;
3221 *peel_iters_epilogue = (niters - peel_iters_prologue) % vf;
3222 /* If we need to peel for gaps, but no peeling is required, we have to
3223 peel VF iterations. */
3224 if (LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo) && !*peel_iters_epilogue)
3225 *peel_iters_epilogue = vf;
3228 stmt_info_for_cost *si;
3229 int j;
3230 if (peel_iters_prologue)
3231 FOR_EACH_VEC_ELT (*scalar_cost_vec, j, si)
3233 stmt_vec_info stmt_info
3234 = si->stmt ? vinfo_for_stmt (si->stmt) : NULL;
3235 retval += record_stmt_cost (prologue_cost_vec,
3236 si->count * peel_iters_prologue,
3237 si->kind, stmt_info, si->misalign,
3238 vect_prologue);
3240 if (*peel_iters_epilogue)
3241 FOR_EACH_VEC_ELT (*scalar_cost_vec, j, si)
3243 stmt_vec_info stmt_info
3244 = si->stmt ? vinfo_for_stmt (si->stmt) : NULL;
3245 retval += record_stmt_cost (epilogue_cost_vec,
3246 si->count * *peel_iters_epilogue,
3247 si->kind, stmt_info, si->misalign,
3248 vect_epilogue);
3251 return retval;
3254 /* Function vect_estimate_min_profitable_iters
3256 Return the number of iterations required for the vector version of the
3257 loop to be profitable relative to the cost of the scalar version of the
3258 loop.
3260 *RET_MIN_PROFITABLE_NITERS is a cost model profitability threshold
3261 of iterations for vectorization. -1 value means loop vectorization
3262 is not profitable. This returned value may be used for dynamic
3263 profitability check.
3265 *RET_MIN_PROFITABLE_ESTIMATE is a profitability threshold to be used
3266 for static check against estimated number of iterations. */
3268 static void
3269 vect_estimate_min_profitable_iters (loop_vec_info loop_vinfo,
3270 int *ret_min_profitable_niters,
3271 int *ret_min_profitable_estimate)
3273 int min_profitable_iters;
3274 int min_profitable_estimate;
3275 int peel_iters_prologue;
3276 int peel_iters_epilogue;
3277 unsigned vec_inside_cost = 0;
3278 int vec_outside_cost = 0;
3279 unsigned vec_prologue_cost = 0;
3280 unsigned vec_epilogue_cost = 0;
3281 int scalar_single_iter_cost = 0;
3282 int scalar_outside_cost = 0;
3283 int vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
3284 int npeel = LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo);
3285 void *target_cost_data = LOOP_VINFO_TARGET_COST_DATA (loop_vinfo);
3287 /* Cost model disabled. */
3288 if (unlimited_cost_model (LOOP_VINFO_LOOP (loop_vinfo)))
3290 dump_printf_loc (MSG_NOTE, vect_location, "cost model disabled.\n");
3291 *ret_min_profitable_niters = 0;
3292 *ret_min_profitable_estimate = 0;
3293 return;
3296 /* Requires loop versioning tests to handle misalignment. */
3297 if (LOOP_REQUIRES_VERSIONING_FOR_ALIGNMENT (loop_vinfo))
3299 /* FIXME: Make cost depend on complexity of individual check. */
3300 unsigned len = LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo).length ();
3301 (void) add_stmt_cost (target_cost_data, len, vector_stmt, NULL, 0,
3302 vect_prologue);
3303 dump_printf (MSG_NOTE,
3304 "cost model: Adding cost of checks for loop "
3305 "versioning to treat misalignment.\n");
3308 /* Requires loop versioning with alias checks. */
3309 if (LOOP_REQUIRES_VERSIONING_FOR_ALIAS (loop_vinfo))
3311 /* FIXME: Make cost depend on complexity of individual check. */
3312 unsigned len = LOOP_VINFO_COMP_ALIAS_DDRS (loop_vinfo).length ();
3313 (void) add_stmt_cost (target_cost_data, len, vector_stmt, NULL, 0,
3314 vect_prologue);
3315 dump_printf (MSG_NOTE,
3316 "cost model: Adding cost of checks for loop "
3317 "versioning aliasing.\n");
3320 /* Requires loop versioning with niter checks. */
3321 if (LOOP_REQUIRES_VERSIONING_FOR_NITERS (loop_vinfo))
3323 /* FIXME: Make cost depend on complexity of individual check. */
3324 (void) add_stmt_cost (target_cost_data, 1, vector_stmt, NULL, 0,
3325 vect_prologue);
3326 dump_printf (MSG_NOTE,
3327 "cost model: Adding cost of checks for loop "
3328 "versioning niters.\n");
3331 if (LOOP_REQUIRES_VERSIONING (loop_vinfo))
3332 (void) add_stmt_cost (target_cost_data, 1, cond_branch_taken, NULL, 0,
3333 vect_prologue);
3335 /* Count statements in scalar loop. Using this as scalar cost for a single
3336 iteration for now.
3338 TODO: Add outer loop support.
3340 TODO: Consider assigning different costs to different scalar
3341 statements. */
3343 scalar_single_iter_cost
3344 = LOOP_VINFO_SINGLE_SCALAR_ITERATION_COST (loop_vinfo);
3346 /* Add additional cost for the peeled instructions in prologue and epilogue
3347 loop.
3349 FORNOW: If we don't know the value of peel_iters for prologue or epilogue
3350 at compile-time - we assume it's vf/2 (the worst would be vf-1).
3352 TODO: Build an expression that represents peel_iters for prologue and
3353 epilogue to be used in a run-time test. */
3355 if (npeel < 0)
3357 peel_iters_prologue = vf/2;
3358 dump_printf (MSG_NOTE, "cost model: "
3359 "prologue peel iters set to vf/2.\n");
3361 /* If peeling for alignment is unknown, loop bound of main loop becomes
3362 unknown. */
3363 peel_iters_epilogue = vf/2;
3364 dump_printf (MSG_NOTE, "cost model: "
3365 "epilogue peel iters set to vf/2 because "
3366 "peeling for alignment is unknown.\n");
3368 /* If peeled iterations are unknown, count a taken branch and a not taken
3369 branch per peeled loop. Even if scalar loop iterations are known,
3370 vector iterations are not known since peeled prologue iterations are
3371 not known. Hence guards remain the same. */
3372 (void) add_stmt_cost (target_cost_data, 1, cond_branch_taken,
3373 NULL, 0, vect_prologue);
3374 (void) add_stmt_cost (target_cost_data, 1, cond_branch_not_taken,
3375 NULL, 0, vect_prologue);
3376 (void) add_stmt_cost (target_cost_data, 1, cond_branch_taken,
3377 NULL, 0, vect_epilogue);
3378 (void) add_stmt_cost (target_cost_data, 1, cond_branch_not_taken,
3379 NULL, 0, vect_epilogue);
3380 stmt_info_for_cost *si;
3381 int j;
3382 FOR_EACH_VEC_ELT (LOOP_VINFO_SCALAR_ITERATION_COST (loop_vinfo), j, si)
3384 struct _stmt_vec_info *stmt_info
3385 = si->stmt ? vinfo_for_stmt (si->stmt) : NULL;
3386 (void) add_stmt_cost (target_cost_data,
3387 si->count * peel_iters_prologue,
3388 si->kind, stmt_info, si->misalign,
3389 vect_prologue);
3390 (void) add_stmt_cost (target_cost_data,
3391 si->count * peel_iters_epilogue,
3392 si->kind, stmt_info, si->misalign,
3393 vect_epilogue);
3396 else
3398 stmt_vector_for_cost prologue_cost_vec, epilogue_cost_vec;
3399 stmt_info_for_cost *si;
3400 int j;
3401 void *data = LOOP_VINFO_TARGET_COST_DATA (loop_vinfo);
3403 prologue_cost_vec.create (2);
3404 epilogue_cost_vec.create (2);
3405 peel_iters_prologue = npeel;
3407 (void) vect_get_known_peeling_cost (loop_vinfo, peel_iters_prologue,
3408 &peel_iters_epilogue,
3409 &LOOP_VINFO_SCALAR_ITERATION_COST
3410 (loop_vinfo),
3411 &prologue_cost_vec,
3412 &epilogue_cost_vec);
3414 FOR_EACH_VEC_ELT (prologue_cost_vec, j, si)
3416 struct _stmt_vec_info *stmt_info
3417 = si->stmt ? vinfo_for_stmt (si->stmt) : NULL;
3418 (void) add_stmt_cost (data, si->count, si->kind, stmt_info,
3419 si->misalign, vect_prologue);
3422 FOR_EACH_VEC_ELT (epilogue_cost_vec, j, si)
3424 struct _stmt_vec_info *stmt_info
3425 = si->stmt ? vinfo_for_stmt (si->stmt) : NULL;
3426 (void) add_stmt_cost (data, si->count, si->kind, stmt_info,
3427 si->misalign, vect_epilogue);
3430 prologue_cost_vec.release ();
3431 epilogue_cost_vec.release ();
3434 /* FORNOW: The scalar outside cost is incremented in one of the
3435 following ways:
3437 1. The vectorizer checks for alignment and aliasing and generates
3438 a condition that allows dynamic vectorization. A cost model
3439 check is ANDED with the versioning condition. Hence scalar code
3440 path now has the added cost of the versioning check.
3442 if (cost > th & versioning_check)
3443 jmp to vector code
3445 Hence run-time scalar is incremented by not-taken branch cost.
3447 2. The vectorizer then checks if a prologue is required. If the
3448 cost model check was not done before during versioning, it has to
3449 be done before the prologue check.
3451 if (cost <= th)
3452 prologue = scalar_iters
3453 if (prologue == 0)
3454 jmp to vector code
3455 else
3456 execute prologue
3457 if (prologue == num_iters)
3458 go to exit
3460 Hence the run-time scalar cost is incremented by a taken branch,
3461 plus a not-taken branch, plus a taken branch cost.
3463 3. The vectorizer then checks if an epilogue is required. If the
3464 cost model check was not done before during prologue check, it
3465 has to be done with the epilogue check.
3467 if (prologue == 0)
3468 jmp to vector code
3469 else
3470 execute prologue
3471 if (prologue == num_iters)
3472 go to exit
3473 vector code:
3474 if ((cost <= th) | (scalar_iters-prologue-epilogue == 0))
3475 jmp to epilogue
3477 Hence the run-time scalar cost should be incremented by 2 taken
3478 branches.
3480 TODO: The back end may reorder the BBS's differently and reverse
3481 conditions/branch directions. Change the estimates below to
3482 something more reasonable. */
3484 /* If the number of iterations is known and we do not do versioning, we can
3485 decide whether to vectorize at compile time. Hence the scalar version
3486 do not carry cost model guard costs. */
3487 if (!LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo)
3488 || LOOP_REQUIRES_VERSIONING (loop_vinfo))
3490 /* Cost model check occurs at versioning. */
3491 if (LOOP_REQUIRES_VERSIONING (loop_vinfo))
3492 scalar_outside_cost += vect_get_stmt_cost (cond_branch_not_taken);
3493 else
3495 /* Cost model check occurs at prologue generation. */
3496 if (LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo) < 0)
3497 scalar_outside_cost += 2 * vect_get_stmt_cost (cond_branch_taken)
3498 + vect_get_stmt_cost (cond_branch_not_taken);
3499 /* Cost model check occurs at epilogue generation. */
3500 else
3501 scalar_outside_cost += 2 * vect_get_stmt_cost (cond_branch_taken);
3505 /* Complete the target-specific cost calculations. */
3506 finish_cost (LOOP_VINFO_TARGET_COST_DATA (loop_vinfo), &vec_prologue_cost,
3507 &vec_inside_cost, &vec_epilogue_cost);
3509 vec_outside_cost = (int)(vec_prologue_cost + vec_epilogue_cost);
3511 if (dump_enabled_p ())
3513 dump_printf_loc (MSG_NOTE, vect_location, "Cost model analysis: \n");
3514 dump_printf (MSG_NOTE, " Vector inside of loop cost: %d\n",
3515 vec_inside_cost);
3516 dump_printf (MSG_NOTE, " Vector prologue cost: %d\n",
3517 vec_prologue_cost);
3518 dump_printf (MSG_NOTE, " Vector epilogue cost: %d\n",
3519 vec_epilogue_cost);
3520 dump_printf (MSG_NOTE, " Scalar iteration cost: %d\n",
3521 scalar_single_iter_cost);
3522 dump_printf (MSG_NOTE, " Scalar outside cost: %d\n",
3523 scalar_outside_cost);
3524 dump_printf (MSG_NOTE, " Vector outside cost: %d\n",
3525 vec_outside_cost);
3526 dump_printf (MSG_NOTE, " prologue iterations: %d\n",
3527 peel_iters_prologue);
3528 dump_printf (MSG_NOTE, " epilogue iterations: %d\n",
3529 peel_iters_epilogue);
3532 /* Calculate number of iterations required to make the vector version
3533 profitable, relative to the loop bodies only. The following condition
3534 must hold true:
3535 SIC * niters + SOC > VIC * ((niters-PL_ITERS-EP_ITERS)/VF) + VOC
3536 where
3537 SIC = scalar iteration cost, VIC = vector iteration cost,
3538 VOC = vector outside cost, VF = vectorization factor,
3539 PL_ITERS = prologue iterations, EP_ITERS= epilogue iterations
3540 SOC = scalar outside cost for run time cost model check. */
3542 if ((scalar_single_iter_cost * vf) > (int) vec_inside_cost)
3544 if (vec_outside_cost <= 0)
3545 min_profitable_iters = 1;
3546 else
3548 min_profitable_iters = ((vec_outside_cost - scalar_outside_cost) * vf
3549 - vec_inside_cost * peel_iters_prologue
3550 - vec_inside_cost * peel_iters_epilogue)
3551 / ((scalar_single_iter_cost * vf)
3552 - vec_inside_cost);
3554 if ((scalar_single_iter_cost * vf * min_profitable_iters)
3555 <= (((int) vec_inside_cost * min_profitable_iters)
3556 + (((int) vec_outside_cost - scalar_outside_cost) * vf)))
3557 min_profitable_iters++;
3560 /* vector version will never be profitable. */
3561 else
3563 if (LOOP_VINFO_LOOP (loop_vinfo)->force_vectorize)
3564 warning_at (vect_location, OPT_Wopenmp_simd, "vectorization "
3565 "did not happen for a simd loop");
3567 if (dump_enabled_p ())
3568 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3569 "cost model: the vector iteration cost = %d "
3570 "divided by the scalar iteration cost = %d "
3571 "is greater or equal to the vectorization factor = %d"
3572 ".\n",
3573 vec_inside_cost, scalar_single_iter_cost, vf);
3574 *ret_min_profitable_niters = -1;
3575 *ret_min_profitable_estimate = -1;
3576 return;
3579 dump_printf (MSG_NOTE,
3580 " Calculated minimum iters for profitability: %d\n",
3581 min_profitable_iters);
3583 min_profitable_iters =
3584 min_profitable_iters < vf ? vf : min_profitable_iters;
3586 /* Because the condition we create is:
3587 if (niters <= min_profitable_iters)
3588 then skip the vectorized loop. */
3589 min_profitable_iters--;
3591 if (dump_enabled_p ())
3592 dump_printf_loc (MSG_NOTE, vect_location,
3593 " Runtime profitability threshold = %d\n",
3594 min_profitable_iters);
3596 *ret_min_profitable_niters = min_profitable_iters;
3598 /* Calculate number of iterations required to make the vector version
3599 profitable, relative to the loop bodies only.
3601 Non-vectorized variant is SIC * niters and it must win over vector
3602 variant on the expected loop trip count. The following condition must hold true:
3603 SIC * niters > VIC * ((niters-PL_ITERS-EP_ITERS)/VF) + VOC + SOC */
3605 if (vec_outside_cost <= 0)
3606 min_profitable_estimate = 1;
3607 else
3609 min_profitable_estimate = ((vec_outside_cost + scalar_outside_cost) * vf
3610 - vec_inside_cost * peel_iters_prologue
3611 - vec_inside_cost * peel_iters_epilogue)
3612 / ((scalar_single_iter_cost * vf)
3613 - vec_inside_cost);
3615 min_profitable_estimate --;
3616 min_profitable_estimate = MAX (min_profitable_estimate, min_profitable_iters);
3617 if (dump_enabled_p ())
3618 dump_printf_loc (MSG_NOTE, vect_location,
3619 " Static estimate profitability threshold = %d\n",
3620 min_profitable_estimate);
3622 *ret_min_profitable_estimate = min_profitable_estimate;
3625 /* Writes into SEL a mask for a vec_perm, equivalent to a vec_shr by OFFSET
3626 vector elements (not bits) for a vector of mode MODE. */
3627 static void
3628 calc_vec_perm_mask_for_shift (enum machine_mode mode, unsigned int offset,
3629 unsigned char *sel)
3631 unsigned int i, nelt = GET_MODE_NUNITS (mode);
3633 for (i = 0; i < nelt; i++)
3634 sel[i] = (i + offset) & (2*nelt - 1);
3637 /* Checks whether the target supports whole-vector shifts for vectors of mode
3638 MODE. This is the case if _either_ the platform handles vec_shr_optab, _or_
3639 it supports vec_perm_const with masks for all necessary shift amounts. */
3640 static bool
3641 have_whole_vector_shift (enum machine_mode mode)
3643 if (optab_handler (vec_shr_optab, mode) != CODE_FOR_nothing)
3644 return true;
3646 if (direct_optab_handler (vec_perm_const_optab, mode) == CODE_FOR_nothing)
3647 return false;
3649 unsigned int i, nelt = GET_MODE_NUNITS (mode);
3650 unsigned char *sel = XALLOCAVEC (unsigned char, nelt);
3652 for (i = nelt/2; i >= 1; i/=2)
3654 calc_vec_perm_mask_for_shift (mode, i, sel);
3655 if (!can_vec_perm_p (mode, false, sel))
3656 return false;
3658 return true;
3661 /* Return the reduction operand (with index REDUC_INDEX) of STMT. */
3663 static tree
3664 get_reduction_op (gimple *stmt, int reduc_index)
3666 switch (get_gimple_rhs_class (gimple_assign_rhs_code (stmt)))
3668 case GIMPLE_SINGLE_RHS:
3669 gcc_assert (TREE_OPERAND_LENGTH (gimple_assign_rhs1 (stmt))
3670 == ternary_op);
3671 return TREE_OPERAND (gimple_assign_rhs1 (stmt), reduc_index);
3672 case GIMPLE_UNARY_RHS:
3673 return gimple_assign_rhs1 (stmt);
3674 case GIMPLE_BINARY_RHS:
3675 return (reduc_index
3676 ? gimple_assign_rhs2 (stmt) : gimple_assign_rhs1 (stmt));
3677 case GIMPLE_TERNARY_RHS:
3678 return gimple_op (stmt, reduc_index + 1);
3679 default:
3680 gcc_unreachable ();
3684 /* TODO: Close dependency between vect_model_*_cost and vectorizable_*
3685 functions. Design better to avoid maintenance issues. */
3687 /* Function vect_model_reduction_cost.
3689 Models cost for a reduction operation, including the vector ops
3690 generated within the strip-mine loop, the initial definition before
3691 the loop, and the epilogue code that must be generated. */
3693 static void
3694 vect_model_reduction_cost (stmt_vec_info stmt_info, enum tree_code reduc_code,
3695 int ncopies)
3697 int prologue_cost = 0, epilogue_cost = 0;
3698 enum tree_code code;
3699 optab optab;
3700 tree vectype;
3701 gimple *orig_stmt;
3702 machine_mode mode;
3703 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
3704 struct loop *loop = NULL;
3705 void *target_cost_data;
3707 if (loop_vinfo)
3709 loop = LOOP_VINFO_LOOP (loop_vinfo);
3710 target_cost_data = LOOP_VINFO_TARGET_COST_DATA (loop_vinfo);
3712 else
3713 target_cost_data = BB_VINFO_TARGET_COST_DATA (STMT_VINFO_BB_VINFO (stmt_info));
3715 /* Condition reductions generate two reductions in the loop. */
3716 if (STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info) == COND_REDUCTION)
3717 ncopies *= 2;
3719 /* Cost of reduction op inside loop. */
3720 unsigned inside_cost = add_stmt_cost (target_cost_data, ncopies, vector_stmt,
3721 stmt_info, 0, vect_body);
3723 vectype = STMT_VINFO_VECTYPE (stmt_info);
3724 mode = TYPE_MODE (vectype);
3725 orig_stmt = STMT_VINFO_RELATED_STMT (stmt_info);
3727 if (!orig_stmt)
3728 orig_stmt = STMT_VINFO_STMT (stmt_info);
3730 code = gimple_assign_rhs_code (orig_stmt);
3732 /* Add in cost for initial definition.
3733 For cond reduction we have four vectors: initial index, step, initial
3734 result of the data reduction, initial value of the index reduction. */
3735 int prologue_stmts = STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info)
3736 == COND_REDUCTION ? 4 : 1;
3737 prologue_cost += add_stmt_cost (target_cost_data, prologue_stmts,
3738 scalar_to_vec, stmt_info, 0,
3739 vect_prologue);
3741 /* Determine cost of epilogue code.
3743 We have a reduction operator that will reduce the vector in one statement.
3744 Also requires scalar extract. */
3746 if (!loop || !nested_in_vect_loop_p (loop, orig_stmt))
3748 if (reduc_code != ERROR_MARK)
3750 if (STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info) == COND_REDUCTION)
3752 /* An EQ stmt and an COND_EXPR stmt. */
3753 epilogue_cost += add_stmt_cost (target_cost_data, 2,
3754 vector_stmt, stmt_info, 0,
3755 vect_epilogue);
3756 /* Reduction of the max index and a reduction of the found
3757 values. */
3758 epilogue_cost += add_stmt_cost (target_cost_data, 2,
3759 vec_to_scalar, stmt_info, 0,
3760 vect_epilogue);
3761 /* A broadcast of the max value. */
3762 epilogue_cost += add_stmt_cost (target_cost_data, 1,
3763 scalar_to_vec, stmt_info, 0,
3764 vect_epilogue);
3766 else
3768 epilogue_cost += add_stmt_cost (target_cost_data, 1, vector_stmt,
3769 stmt_info, 0, vect_epilogue);
3770 epilogue_cost += add_stmt_cost (target_cost_data, 1,
3771 vec_to_scalar, stmt_info, 0,
3772 vect_epilogue);
3775 else if (STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info) == COND_REDUCTION)
3777 unsigned nunits = TYPE_VECTOR_SUBPARTS (vectype);
3778 /* Extraction of scalar elements. */
3779 epilogue_cost += add_stmt_cost (target_cost_data, 2 * nunits,
3780 vec_to_scalar, stmt_info, 0,
3781 vect_epilogue);
3782 /* Scalar max reductions via COND_EXPR / MAX_EXPR. */
3783 epilogue_cost += add_stmt_cost (target_cost_data, 2 * nunits - 3,
3784 scalar_stmt, stmt_info, 0,
3785 vect_epilogue);
3787 else
3789 int vec_size_in_bits = tree_to_uhwi (TYPE_SIZE (vectype));
3790 tree bitsize =
3791 TYPE_SIZE (TREE_TYPE (gimple_assign_lhs (orig_stmt)));
3792 int element_bitsize = tree_to_uhwi (bitsize);
3793 int nelements = vec_size_in_bits / element_bitsize;
3795 if (code == COND_EXPR)
3796 code = MAX_EXPR;
3798 optab = optab_for_tree_code (code, vectype, optab_default);
3800 /* We have a whole vector shift available. */
3801 if (optab != unknown_optab
3802 && VECTOR_MODE_P (mode)
3803 && optab_handler (optab, mode) != CODE_FOR_nothing
3804 && have_whole_vector_shift (mode))
3806 /* Final reduction via vector shifts and the reduction operator.
3807 Also requires scalar extract. */
3808 epilogue_cost += add_stmt_cost (target_cost_data,
3809 exact_log2 (nelements) * 2,
3810 vector_stmt, stmt_info, 0,
3811 vect_epilogue);
3812 epilogue_cost += add_stmt_cost (target_cost_data, 1,
3813 vec_to_scalar, stmt_info, 0,
3814 vect_epilogue);
3816 else
3817 /* Use extracts and reduction op for final reduction. For N
3818 elements, we have N extracts and N-1 reduction ops. */
3819 epilogue_cost += add_stmt_cost (target_cost_data,
3820 nelements + nelements - 1,
3821 vector_stmt, stmt_info, 0,
3822 vect_epilogue);
3826 if (dump_enabled_p ())
3827 dump_printf (MSG_NOTE,
3828 "vect_model_reduction_cost: inside_cost = %d, "
3829 "prologue_cost = %d, epilogue_cost = %d .\n", inside_cost,
3830 prologue_cost, epilogue_cost);
3834 /* Function vect_model_induction_cost.
3836 Models cost for induction operations. */
3838 static void
3839 vect_model_induction_cost (stmt_vec_info stmt_info, int ncopies)
3841 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
3842 void *target_cost_data = LOOP_VINFO_TARGET_COST_DATA (loop_vinfo);
3843 unsigned inside_cost, prologue_cost;
3845 if (PURE_SLP_STMT (stmt_info))
3846 return;
3848 /* loop cost for vec_loop. */
3849 inside_cost = add_stmt_cost (target_cost_data, ncopies, vector_stmt,
3850 stmt_info, 0, vect_body);
3852 /* prologue cost for vec_init and vec_step. */
3853 prologue_cost = add_stmt_cost (target_cost_data, 2, scalar_to_vec,
3854 stmt_info, 0, vect_prologue);
3856 if (dump_enabled_p ())
3857 dump_printf_loc (MSG_NOTE, vect_location,
3858 "vect_model_induction_cost: inside_cost = %d, "
3859 "prologue_cost = %d .\n", inside_cost, prologue_cost);
3864 /* Function get_initial_def_for_reduction
3866 Input:
3867 STMT - a stmt that performs a reduction operation in the loop.
3868 INIT_VAL - the initial value of the reduction variable
3870 Output:
3871 ADJUSTMENT_DEF - a tree that holds a value to be added to the final result
3872 of the reduction (used for adjusting the epilog - see below).
3873 Return a vector variable, initialized according to the operation that STMT
3874 performs. This vector will be used as the initial value of the
3875 vector of partial results.
3877 Option1 (adjust in epilog): Initialize the vector as follows:
3878 add/bit or/xor: [0,0,...,0,0]
3879 mult/bit and: [1,1,...,1,1]
3880 min/max/cond_expr: [init_val,init_val,..,init_val,init_val]
3881 and when necessary (e.g. add/mult case) let the caller know
3882 that it needs to adjust the result by init_val.
3884 Option2: Initialize the vector as follows:
3885 add/bit or/xor: [init_val,0,0,...,0]
3886 mult/bit and: [init_val,1,1,...,1]
3887 min/max/cond_expr: [init_val,init_val,...,init_val]
3888 and no adjustments are needed.
3890 For example, for the following code:
3892 s = init_val;
3893 for (i=0;i<n;i++)
3894 s = s + a[i];
3896 STMT is 's = s + a[i]', and the reduction variable is 's'.
3897 For a vector of 4 units, we want to return either [0,0,0,init_val],
3898 or [0,0,0,0] and let the caller know that it needs to adjust
3899 the result at the end by 'init_val'.
3901 FORNOW, we are using the 'adjust in epilog' scheme, because this way the
3902 initialization vector is simpler (same element in all entries), if
3903 ADJUSTMENT_DEF is not NULL, and Option2 otherwise.
3905 A cost model should help decide between these two schemes. */
3907 tree
3908 get_initial_def_for_reduction (gimple *stmt, tree init_val,
3909 tree *adjustment_def)
3911 stmt_vec_info stmt_vinfo = vinfo_for_stmt (stmt);
3912 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_vinfo);
3913 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
3914 tree scalar_type = TREE_TYPE (init_val);
3915 tree vectype = get_vectype_for_scalar_type (scalar_type);
3916 int nunits;
3917 enum tree_code code = gimple_assign_rhs_code (stmt);
3918 tree def_for_init;
3919 tree init_def;
3920 tree *elts;
3921 int i;
3922 bool nested_in_vect_loop = false;
3923 REAL_VALUE_TYPE real_init_val = dconst0;
3924 int int_init_val = 0;
3925 gimple *def_stmt = NULL;
3926 gimple_seq stmts = NULL;
3928 gcc_assert (vectype);
3929 nunits = TYPE_VECTOR_SUBPARTS (vectype);
3931 gcc_assert (POINTER_TYPE_P (scalar_type) || INTEGRAL_TYPE_P (scalar_type)
3932 || SCALAR_FLOAT_TYPE_P (scalar_type));
3934 if (nested_in_vect_loop_p (loop, stmt))
3935 nested_in_vect_loop = true;
3936 else
3937 gcc_assert (loop == (gimple_bb (stmt))->loop_father);
3939 /* In case of double reduction we only create a vector variable to be put
3940 in the reduction phi node. The actual statement creation is done in
3941 vect_create_epilog_for_reduction. */
3942 if (adjustment_def && nested_in_vect_loop
3943 && TREE_CODE (init_val) == SSA_NAME
3944 && (def_stmt = SSA_NAME_DEF_STMT (init_val))
3945 && gimple_code (def_stmt) == GIMPLE_PHI
3946 && flow_bb_inside_loop_p (loop, gimple_bb (def_stmt))
3947 && vinfo_for_stmt (def_stmt)
3948 && STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def_stmt))
3949 == vect_double_reduction_def)
3951 *adjustment_def = NULL;
3952 return vect_create_destination_var (init_val, vectype);
3955 /* In case of a nested reduction do not use an adjustment def as
3956 that case is not supported by the epilogue generation correctly
3957 if ncopies is not one. */
3958 if (adjustment_def && nested_in_vect_loop)
3960 *adjustment_def = NULL;
3961 return vect_get_vec_def_for_operand (init_val, stmt);
3964 switch (code)
3966 case WIDEN_SUM_EXPR:
3967 case DOT_PROD_EXPR:
3968 case SAD_EXPR:
3969 case PLUS_EXPR:
3970 case MINUS_EXPR:
3971 case BIT_IOR_EXPR:
3972 case BIT_XOR_EXPR:
3973 case MULT_EXPR:
3974 case BIT_AND_EXPR:
3975 /* ADJUSMENT_DEF is NULL when called from
3976 vect_create_epilog_for_reduction to vectorize double reduction. */
3977 if (adjustment_def)
3978 *adjustment_def = init_val;
3980 if (code == MULT_EXPR)
3982 real_init_val = dconst1;
3983 int_init_val = 1;
3986 if (code == BIT_AND_EXPR)
3987 int_init_val = -1;
3989 if (SCALAR_FLOAT_TYPE_P (scalar_type))
3990 def_for_init = build_real (scalar_type, real_init_val);
3991 else
3992 def_for_init = build_int_cst (scalar_type, int_init_val);
3994 /* Create a vector of '0' or '1' except the first element. */
3995 elts = XALLOCAVEC (tree, nunits);
3996 for (i = nunits - 2; i >= 0; --i)
3997 elts[i + 1] = def_for_init;
3999 /* Option1: the first element is '0' or '1' as well. */
4000 if (adjustment_def)
4002 elts[0] = def_for_init;
4003 init_def = build_vector (vectype, elts);
4004 break;
4007 /* Option2: the first element is INIT_VAL. */
4008 elts[0] = init_val;
4009 if (TREE_CONSTANT (init_val))
4010 init_def = build_vector (vectype, elts);
4011 else
4013 vec<constructor_elt, va_gc> *v;
4014 vec_alloc (v, nunits);
4015 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init_val);
4016 for (i = 1; i < nunits; ++i)
4017 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, elts[i]);
4018 init_def = build_constructor (vectype, v);
4021 break;
4023 case MIN_EXPR:
4024 case MAX_EXPR:
4025 case COND_EXPR:
4026 if (adjustment_def)
4028 *adjustment_def = NULL_TREE;
4029 if (STMT_VINFO_VEC_REDUCTION_TYPE (stmt_vinfo) != COND_REDUCTION)
4031 init_def = vect_get_vec_def_for_operand (init_val, stmt);
4032 break;
4035 init_val = gimple_convert (&stmts, TREE_TYPE (vectype), init_val);
4036 if (! gimple_seq_empty_p (stmts))
4037 gsi_insert_seq_on_edge_immediate (loop_preheader_edge (loop), stmts);
4038 init_def = build_vector_from_val (vectype, init_val);
4039 break;
4041 default:
4042 gcc_unreachable ();
4045 return init_def;
4048 /* Get at the initial defs for OP in the reduction SLP_NODE.
4049 NUMBER_OF_VECTORS is the number of vector defs to create.
4050 REDUC_INDEX is the index of the reduction operand in the statements. */
4052 static void
4053 get_initial_defs_for_reduction (slp_tree slp_node,
4054 vec<tree> *vec_oprnds,
4055 unsigned int number_of_vectors,
4056 int reduc_index, enum tree_code code)
4058 vec<gimple *> stmts = SLP_TREE_SCALAR_STMTS (slp_node);
4059 gimple *stmt = stmts[0];
4060 stmt_vec_info stmt_vinfo = vinfo_for_stmt (stmt);
4061 unsigned nunits;
4062 tree vec_cst;
4063 tree *elts;
4064 unsigned j, number_of_places_left_in_vector;
4065 tree vector_type, scalar_type;
4066 tree vop;
4067 int group_size = stmts.length ();
4068 unsigned int vec_num, i;
4069 unsigned number_of_copies = 1;
4070 vec<tree> voprnds;
4071 voprnds.create (number_of_vectors);
4072 bool constant_p;
4073 tree neutral_op = NULL;
4074 gimple *def_stmt;
4075 struct loop *loop;
4076 gimple_seq ctor_seq = NULL;
4078 vector_type = STMT_VINFO_VECTYPE (stmt_vinfo);
4079 scalar_type = TREE_TYPE (vector_type);
4080 nunits = TYPE_VECTOR_SUBPARTS (vector_type);
4082 gcc_assert (STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_reduction_def
4083 && reduc_index != -1);
4085 /* op is the reduction operand of the first stmt already. */
4086 /* For additional copies (see the explanation of NUMBER_OF_COPIES below)
4087 we need either neutral operands or the original operands. See
4088 get_initial_def_for_reduction() for details. */
4089 switch (code)
4091 case WIDEN_SUM_EXPR:
4092 case DOT_PROD_EXPR:
4093 case SAD_EXPR:
4094 case PLUS_EXPR:
4095 case MINUS_EXPR:
4096 case BIT_IOR_EXPR:
4097 case BIT_XOR_EXPR:
4098 neutral_op = build_zero_cst (scalar_type);
4099 break;
4101 case MULT_EXPR:
4102 neutral_op = build_one_cst (scalar_type);
4103 break;
4105 case BIT_AND_EXPR:
4106 neutral_op = build_all_ones_cst (scalar_type);
4107 break;
4109 /* For MIN/MAX we don't have an easy neutral operand but
4110 the initial values can be used fine here. Only for
4111 a reduction chain we have to force a neutral element. */
4112 case MAX_EXPR:
4113 case MIN_EXPR:
4114 if (!GROUP_FIRST_ELEMENT (stmt_vinfo))
4115 neutral_op = NULL;
4116 else
4118 tree op = get_reduction_op (stmts[0], reduc_index);
4119 def_stmt = SSA_NAME_DEF_STMT (op);
4120 loop = (gimple_bb (stmt))->loop_father;
4121 neutral_op = PHI_ARG_DEF_FROM_EDGE (def_stmt,
4122 loop_preheader_edge (loop));
4124 break;
4126 default:
4127 gcc_assert (!GROUP_FIRST_ELEMENT (stmt_vinfo));
4128 neutral_op = NULL;
4131 /* NUMBER_OF_COPIES is the number of times we need to use the same values in
4132 created vectors. It is greater than 1 if unrolling is performed.
4134 For example, we have two scalar operands, s1 and s2 (e.g., group of
4135 strided accesses of size two), while NUNITS is four (i.e., four scalars
4136 of this type can be packed in a vector). The output vector will contain
4137 two copies of each scalar operand: {s1, s2, s1, s2}. (NUMBER_OF_COPIES
4138 will be 2).
4140 If GROUP_SIZE > NUNITS, the scalars will be split into several vectors
4141 containing the operands.
4143 For example, NUNITS is four as before, and the group size is 8
4144 (s1, s2, ..., s8). We will create two vectors {s1, s2, s3, s4} and
4145 {s5, s6, s7, s8}. */
4147 number_of_copies = nunits * number_of_vectors / group_size;
4149 number_of_places_left_in_vector = nunits;
4150 constant_p = true;
4151 elts = XALLOCAVEC (tree, nunits);
4152 for (j = 0; j < number_of_copies; j++)
4154 for (i = group_size - 1; stmts.iterate (i, &stmt); i--)
4156 tree op = get_reduction_op (stmt, reduc_index);
4157 loop = (gimple_bb (stmt))->loop_father;
4158 def_stmt = SSA_NAME_DEF_STMT (op);
4160 gcc_assert (loop);
4162 /* Get the def before the loop. In reduction chain we have only
4163 one initial value. */
4164 if ((j != (number_of_copies - 1)
4165 || (GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt))
4166 && i != 0))
4167 && neutral_op)
4168 op = neutral_op;
4169 else
4170 op = PHI_ARG_DEF_FROM_EDGE (def_stmt,
4171 loop_preheader_edge (loop));
4173 /* Create 'vect_ = {op0,op1,...,opn}'. */
4174 number_of_places_left_in_vector--;
4175 elts[number_of_places_left_in_vector] = op;
4176 if (!CONSTANT_CLASS_P (op))
4177 constant_p = false;
4179 if (number_of_places_left_in_vector == 0)
4181 if (constant_p)
4182 vec_cst = build_vector (vector_type, elts);
4183 else
4185 vec<constructor_elt, va_gc> *v;
4186 unsigned k;
4187 vec_alloc (v, nunits);
4188 for (k = 0; k < nunits; ++k)
4189 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, elts[k]);
4190 vec_cst = build_constructor (vector_type, v);
4192 tree init;
4193 gimple_stmt_iterator gsi;
4194 init = vect_init_vector (stmt, vec_cst, vector_type, NULL);
4195 if (ctor_seq != NULL)
4197 gsi = gsi_for_stmt (SSA_NAME_DEF_STMT (init));
4198 gsi_insert_seq_before_without_update (&gsi, ctor_seq,
4199 GSI_SAME_STMT);
4200 ctor_seq = NULL;
4202 voprnds.quick_push (init);
4204 number_of_places_left_in_vector = nunits;
4205 constant_p = true;
4210 /* Since the vectors are created in the reverse order, we should invert
4211 them. */
4212 vec_num = voprnds.length ();
4213 for (j = vec_num; j != 0; j--)
4215 vop = voprnds[j - 1];
4216 vec_oprnds->quick_push (vop);
4219 voprnds.release ();
4221 /* In case that VF is greater than the unrolling factor needed for the SLP
4222 group of stmts, NUMBER_OF_VECTORS to be created is greater than
4223 NUMBER_OF_SCALARS/NUNITS or NUNITS/NUMBER_OF_SCALARS, and hence we have
4224 to replicate the vectors. */
4225 while (number_of_vectors > vec_oprnds->length ())
4227 tree neutral_vec = NULL;
4229 if (neutral_op)
4231 if (!neutral_vec)
4232 neutral_vec = build_vector_from_val (vector_type, neutral_op);
4234 vec_oprnds->quick_push (neutral_vec);
4236 else
4238 for (i = 0; vec_oprnds->iterate (i, &vop) && i < vec_num; i++)
4239 vec_oprnds->quick_push (vop);
4245 /* Function vect_create_epilog_for_reduction
4247 Create code at the loop-epilog to finalize the result of a reduction
4248 computation.
4250 VECT_DEFS is list of vector of partial results, i.e., the lhs's of vector
4251 reduction statements.
4252 STMT is the scalar reduction stmt that is being vectorized.
4253 NCOPIES is > 1 in case the vectorization factor (VF) is bigger than the
4254 number of elements that we can fit in a vectype (nunits). In this case
4255 we have to generate more than one vector stmt - i.e - we need to "unroll"
4256 the vector stmt by a factor VF/nunits. For more details see documentation
4257 in vectorizable_operation.
4258 REDUC_CODE is the tree-code for the epilog reduction.
4259 REDUCTION_PHIS is a list of the phi-nodes that carry the reduction
4260 computation.
4261 REDUC_INDEX is the index of the operand in the right hand side of the
4262 statement that is defined by REDUCTION_PHI.
4263 DOUBLE_REDUC is TRUE if double reduction phi nodes should be handled.
4264 SLP_NODE is an SLP node containing a group of reduction statements. The
4265 first one in this group is STMT.
4267 This function:
4268 1. Creates the reduction def-use cycles: sets the arguments for
4269 REDUCTION_PHIS:
4270 The loop-entry argument is the vectorized initial-value of the reduction.
4271 The loop-latch argument is taken from VECT_DEFS - the vector of partial
4272 sums.
4273 2. "Reduces" each vector of partial results VECT_DEFS into a single result,
4274 by applying the operation specified by REDUC_CODE if available, or by
4275 other means (whole-vector shifts or a scalar loop).
4276 The function also creates a new phi node at the loop exit to preserve
4277 loop-closed form, as illustrated below.
4279 The flow at the entry to this function:
4281 loop:
4282 vec_def = phi <null, null> # REDUCTION_PHI
4283 VECT_DEF = vector_stmt # vectorized form of STMT
4284 s_loop = scalar_stmt # (scalar) STMT
4285 loop_exit:
4286 s_out0 = phi <s_loop> # (scalar) EXIT_PHI
4287 use <s_out0>
4288 use <s_out0>
4290 The above is transformed by this function into:
4292 loop:
4293 vec_def = phi <vec_init, VECT_DEF> # REDUCTION_PHI
4294 VECT_DEF = vector_stmt # vectorized form of STMT
4295 s_loop = scalar_stmt # (scalar) STMT
4296 loop_exit:
4297 s_out0 = phi <s_loop> # (scalar) EXIT_PHI
4298 v_out1 = phi <VECT_DEF> # NEW_EXIT_PHI
4299 v_out2 = reduce <v_out1>
4300 s_out3 = extract_field <v_out2, 0>
4301 s_out4 = adjust_result <s_out3>
4302 use <s_out4>
4303 use <s_out4>
4306 static void
4307 vect_create_epilog_for_reduction (vec<tree> vect_defs, gimple *stmt,
4308 int ncopies, enum tree_code reduc_code,
4309 vec<gimple *> reduction_phis,
4310 int reduc_index, bool double_reduc,
4311 slp_tree slp_node)
4313 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
4314 stmt_vec_info prev_phi_info;
4315 tree vectype;
4316 machine_mode mode;
4317 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
4318 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo), *outer_loop = NULL;
4319 basic_block exit_bb;
4320 tree scalar_dest;
4321 tree scalar_type;
4322 gimple *new_phi = NULL, *phi;
4323 gimple_stmt_iterator exit_gsi;
4324 tree vec_dest;
4325 tree new_temp = NULL_TREE, new_dest, new_name, new_scalar_dest;
4326 gimple *epilog_stmt = NULL;
4327 enum tree_code code = gimple_assign_rhs_code (stmt);
4328 gimple *exit_phi;
4329 tree bitsize;
4330 tree adjustment_def = NULL;
4331 tree vec_initial_def = NULL;
4332 tree expr, def, initial_def = NULL;
4333 tree orig_name, scalar_result;
4334 imm_use_iterator imm_iter, phi_imm_iter;
4335 use_operand_p use_p, phi_use_p;
4336 gimple *use_stmt, *orig_stmt, *reduction_phi = NULL;
4337 bool nested_in_vect_loop = false;
4338 auto_vec<gimple *> new_phis;
4339 auto_vec<gimple *> inner_phis;
4340 enum vect_def_type dt = vect_unknown_def_type;
4341 int j, i;
4342 auto_vec<tree> scalar_results;
4343 unsigned int group_size = 1, k, ratio;
4344 auto_vec<tree> vec_initial_defs;
4345 auto_vec<gimple *> phis;
4346 bool slp_reduc = false;
4347 tree new_phi_result;
4348 gimple *inner_phi = NULL;
4349 tree induction_index = NULL_TREE;
4351 if (slp_node)
4352 group_size = SLP_TREE_SCALAR_STMTS (slp_node).length ();
4354 if (nested_in_vect_loop_p (loop, stmt))
4356 outer_loop = loop;
4357 loop = loop->inner;
4358 nested_in_vect_loop = true;
4359 gcc_assert (!slp_node);
4362 vectype = STMT_VINFO_VECTYPE (stmt_info);
4363 gcc_assert (vectype);
4364 mode = TYPE_MODE (vectype);
4366 /* 1. Create the reduction def-use cycle:
4367 Set the arguments of REDUCTION_PHIS, i.e., transform
4369 loop:
4370 vec_def = phi <null, null> # REDUCTION_PHI
4371 VECT_DEF = vector_stmt # vectorized form of STMT
4374 into:
4376 loop:
4377 vec_def = phi <vec_init, VECT_DEF> # REDUCTION_PHI
4378 VECT_DEF = vector_stmt # vectorized form of STMT
4381 (in case of SLP, do it for all the phis). */
4383 /* Get the loop-entry arguments. */
4384 enum vect_def_type initial_def_dt = vect_unknown_def_type;
4385 if (slp_node)
4387 unsigned vec_num = SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node);
4388 vec_initial_defs.reserve (vec_num);
4389 get_initial_defs_for_reduction (slp_node, &vec_initial_defs,
4390 vec_num, reduc_index, code);
4392 else
4394 /* Get at the scalar def before the loop, that defines the initial value
4395 of the reduction variable. */
4396 tree reduction_op = get_reduction_op (stmt, reduc_index);
4397 gimple *def_stmt = SSA_NAME_DEF_STMT (reduction_op);
4398 initial_def = PHI_ARG_DEF_FROM_EDGE (def_stmt,
4399 loop_preheader_edge (loop));
4400 vect_is_simple_use (initial_def, loop_vinfo, &def_stmt, &initial_def_dt);
4401 vec_initial_def = get_initial_def_for_reduction (stmt, initial_def,
4402 &adjustment_def);
4403 vec_initial_defs.create (1);
4404 vec_initial_defs.quick_push (vec_initial_def);
4407 /* Set phi nodes arguments. */
4408 FOR_EACH_VEC_ELT (reduction_phis, i, phi)
4410 tree vec_init_def, def;
4411 gimple_seq stmts;
4412 vec_init_def = force_gimple_operand (vec_initial_defs[i], &stmts,
4413 true, NULL_TREE);
4414 if (stmts)
4415 gsi_insert_seq_on_edge_immediate (loop_preheader_edge (loop), stmts);
4417 def = vect_defs[i];
4418 for (j = 0; j < ncopies; j++)
4420 if (j != 0)
4422 phi = STMT_VINFO_RELATED_STMT (vinfo_for_stmt (phi));
4423 if (nested_in_vect_loop)
4424 vec_init_def
4425 = vect_get_vec_def_for_stmt_copy (initial_def_dt,
4426 vec_init_def);
4429 /* Set the loop-entry arg of the reduction-phi. */
4431 if (STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info)
4432 == INTEGER_INDUC_COND_REDUCTION)
4434 /* Initialise the reduction phi to zero. This prevents initial
4435 values of non-zero interferring with the reduction op. */
4436 gcc_assert (ncopies == 1);
4437 gcc_assert (i == 0);
4439 tree vec_init_def_type = TREE_TYPE (vec_init_def);
4440 tree zero_vec = build_zero_cst (vec_init_def_type);
4442 add_phi_arg (as_a <gphi *> (phi), zero_vec,
4443 loop_preheader_edge (loop), UNKNOWN_LOCATION);
4445 else
4446 add_phi_arg (as_a <gphi *> (phi), vec_init_def,
4447 loop_preheader_edge (loop), UNKNOWN_LOCATION);
4449 /* Set the loop-latch arg for the reduction-phi. */
4450 if (j > 0)
4451 def = vect_get_vec_def_for_stmt_copy (vect_unknown_def_type, def);
4453 add_phi_arg (as_a <gphi *> (phi), def, loop_latch_edge (loop),
4454 UNKNOWN_LOCATION);
4456 if (dump_enabled_p ())
4458 dump_printf_loc (MSG_NOTE, vect_location,
4459 "transform reduction: created def-use cycle: ");
4460 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, phi, 0);
4461 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, SSA_NAME_DEF_STMT (def), 0);
4466 /* For cond reductions we want to create a new vector (INDEX_COND_EXPR)
4467 which is updated with the current index of the loop for every match of
4468 the original loop's cond_expr (VEC_STMT). This results in a vector
4469 containing the last time the condition passed for that vector lane.
4470 The first match will be a 1 to allow 0 to be used for non-matching
4471 indexes. If there are no matches at all then the vector will be all
4472 zeroes. */
4473 if (STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info) == COND_REDUCTION)
4475 tree indx_before_incr, indx_after_incr;
4476 int nunits_out = TYPE_VECTOR_SUBPARTS (vectype);
4477 int k;
4479 gimple *vec_stmt = STMT_VINFO_VEC_STMT (stmt_info);
4480 gcc_assert (gimple_assign_rhs_code (vec_stmt) == VEC_COND_EXPR);
4482 int scalar_precision
4483 = GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (vectype)));
4484 tree cr_index_scalar_type = make_unsigned_type (scalar_precision);
4485 tree cr_index_vector_type = build_vector_type
4486 (cr_index_scalar_type, TYPE_VECTOR_SUBPARTS (vectype));
4488 /* First we create a simple vector induction variable which starts
4489 with the values {1,2,3,...} (SERIES_VECT) and increments by the
4490 vector size (STEP). */
4492 /* Create a {1,2,3,...} vector. */
4493 tree *vtemp = XALLOCAVEC (tree, nunits_out);
4494 for (k = 0; k < nunits_out; ++k)
4495 vtemp[k] = build_int_cst (cr_index_scalar_type, k + 1);
4496 tree series_vect = build_vector (cr_index_vector_type, vtemp);
4498 /* Create a vector of the step value. */
4499 tree step = build_int_cst (cr_index_scalar_type, nunits_out);
4500 tree vec_step = build_vector_from_val (cr_index_vector_type, step);
4502 /* Create an induction variable. */
4503 gimple_stmt_iterator incr_gsi;
4504 bool insert_after;
4505 standard_iv_increment_position (loop, &incr_gsi, &insert_after);
4506 create_iv (series_vect, vec_step, NULL_TREE, loop, &incr_gsi,
4507 insert_after, &indx_before_incr, &indx_after_incr);
4509 /* Next create a new phi node vector (NEW_PHI_TREE) which starts
4510 filled with zeros (VEC_ZERO). */
4512 /* Create a vector of 0s. */
4513 tree zero = build_zero_cst (cr_index_scalar_type);
4514 tree vec_zero = build_vector_from_val (cr_index_vector_type, zero);
4516 /* Create a vector phi node. */
4517 tree new_phi_tree = make_ssa_name (cr_index_vector_type);
4518 new_phi = create_phi_node (new_phi_tree, loop->header);
4519 set_vinfo_for_stmt (new_phi,
4520 new_stmt_vec_info (new_phi, loop_vinfo));
4521 add_phi_arg (as_a <gphi *> (new_phi), vec_zero,
4522 loop_preheader_edge (loop), UNKNOWN_LOCATION);
4524 /* Now take the condition from the loops original cond_expr
4525 (VEC_STMT) and produce a new cond_expr (INDEX_COND_EXPR) which for
4526 every match uses values from the induction variable
4527 (INDEX_BEFORE_INCR) otherwise uses values from the phi node
4528 (NEW_PHI_TREE).
4529 Finally, we update the phi (NEW_PHI_TREE) to take the value of
4530 the new cond_expr (INDEX_COND_EXPR). */
4532 /* Duplicate the condition from vec_stmt. */
4533 tree ccompare = unshare_expr (gimple_assign_rhs1 (vec_stmt));
4535 /* Create a conditional, where the condition is taken from vec_stmt
4536 (CCOMPARE), then is the induction index (INDEX_BEFORE_INCR) and
4537 else is the phi (NEW_PHI_TREE). */
4538 tree index_cond_expr = build3 (VEC_COND_EXPR, cr_index_vector_type,
4539 ccompare, indx_before_incr,
4540 new_phi_tree);
4541 induction_index = make_ssa_name (cr_index_vector_type);
4542 gimple *index_condition = gimple_build_assign (induction_index,
4543 index_cond_expr);
4544 gsi_insert_before (&incr_gsi, index_condition, GSI_SAME_STMT);
4545 stmt_vec_info index_vec_info = new_stmt_vec_info (index_condition,
4546 loop_vinfo);
4547 STMT_VINFO_VECTYPE (index_vec_info) = cr_index_vector_type;
4548 set_vinfo_for_stmt (index_condition, index_vec_info);
4550 /* Update the phi with the vec cond. */
4551 add_phi_arg (as_a <gphi *> (new_phi), induction_index,
4552 loop_latch_edge (loop), UNKNOWN_LOCATION);
4555 /* 2. Create epilog code.
4556 The reduction epilog code operates across the elements of the vector
4557 of partial results computed by the vectorized loop.
4558 The reduction epilog code consists of:
4560 step 1: compute the scalar result in a vector (v_out2)
4561 step 2: extract the scalar result (s_out3) from the vector (v_out2)
4562 step 3: adjust the scalar result (s_out3) if needed.
4564 Step 1 can be accomplished using one the following three schemes:
4565 (scheme 1) using reduc_code, if available.
4566 (scheme 2) using whole-vector shifts, if available.
4567 (scheme 3) using a scalar loop. In this case steps 1+2 above are
4568 combined.
4570 The overall epilog code looks like this:
4572 s_out0 = phi <s_loop> # original EXIT_PHI
4573 v_out1 = phi <VECT_DEF> # NEW_EXIT_PHI
4574 v_out2 = reduce <v_out1> # step 1
4575 s_out3 = extract_field <v_out2, 0> # step 2
4576 s_out4 = adjust_result <s_out3> # step 3
4578 (step 3 is optional, and steps 1 and 2 may be combined).
4579 Lastly, the uses of s_out0 are replaced by s_out4. */
4582 /* 2.1 Create new loop-exit-phis to preserve loop-closed form:
4583 v_out1 = phi <VECT_DEF>
4584 Store them in NEW_PHIS. */
4586 exit_bb = single_exit (loop)->dest;
4587 prev_phi_info = NULL;
4588 new_phis.create (vect_defs.length ());
4589 FOR_EACH_VEC_ELT (vect_defs, i, def)
4591 for (j = 0; j < ncopies; j++)
4593 tree new_def = copy_ssa_name (def);
4594 phi = create_phi_node (new_def, exit_bb);
4595 set_vinfo_for_stmt (phi, new_stmt_vec_info (phi, loop_vinfo));
4596 if (j == 0)
4597 new_phis.quick_push (phi);
4598 else
4600 def = vect_get_vec_def_for_stmt_copy (dt, def);
4601 STMT_VINFO_RELATED_STMT (prev_phi_info) = phi;
4604 SET_PHI_ARG_DEF (phi, single_exit (loop)->dest_idx, def);
4605 prev_phi_info = vinfo_for_stmt (phi);
4609 /* The epilogue is created for the outer-loop, i.e., for the loop being
4610 vectorized. Create exit phis for the outer loop. */
4611 if (double_reduc)
4613 loop = outer_loop;
4614 exit_bb = single_exit (loop)->dest;
4615 inner_phis.create (vect_defs.length ());
4616 FOR_EACH_VEC_ELT (new_phis, i, phi)
4618 tree new_result = copy_ssa_name (PHI_RESULT (phi));
4619 gphi *outer_phi = create_phi_node (new_result, exit_bb);
4620 SET_PHI_ARG_DEF (outer_phi, single_exit (loop)->dest_idx,
4621 PHI_RESULT (phi));
4622 set_vinfo_for_stmt (outer_phi, new_stmt_vec_info (outer_phi,
4623 loop_vinfo));
4624 inner_phis.quick_push (phi);
4625 new_phis[i] = outer_phi;
4626 prev_phi_info = vinfo_for_stmt (outer_phi);
4627 while (STMT_VINFO_RELATED_STMT (vinfo_for_stmt (phi)))
4629 phi = STMT_VINFO_RELATED_STMT (vinfo_for_stmt (phi));
4630 new_result = copy_ssa_name (PHI_RESULT (phi));
4631 outer_phi = create_phi_node (new_result, exit_bb);
4632 SET_PHI_ARG_DEF (outer_phi, single_exit (loop)->dest_idx,
4633 PHI_RESULT (phi));
4634 set_vinfo_for_stmt (outer_phi, new_stmt_vec_info (outer_phi,
4635 loop_vinfo));
4636 STMT_VINFO_RELATED_STMT (prev_phi_info) = outer_phi;
4637 prev_phi_info = vinfo_for_stmt (outer_phi);
4642 exit_gsi = gsi_after_labels (exit_bb);
4644 /* 2.2 Get the relevant tree-code to use in the epilog for schemes 2,3
4645 (i.e. when reduc_code is not available) and in the final adjustment
4646 code (if needed). Also get the original scalar reduction variable as
4647 defined in the loop. In case STMT is a "pattern-stmt" (i.e. - it
4648 represents a reduction pattern), the tree-code and scalar-def are
4649 taken from the original stmt that the pattern-stmt (STMT) replaces.
4650 Otherwise (it is a regular reduction) - the tree-code and scalar-def
4651 are taken from STMT. */
4653 orig_stmt = STMT_VINFO_RELATED_STMT (stmt_info);
4654 if (!orig_stmt)
4656 /* Regular reduction */
4657 orig_stmt = stmt;
4659 else
4661 /* Reduction pattern */
4662 stmt_vec_info stmt_vinfo = vinfo_for_stmt (orig_stmt);
4663 gcc_assert (STMT_VINFO_IN_PATTERN_P (stmt_vinfo));
4664 gcc_assert (STMT_VINFO_RELATED_STMT (stmt_vinfo) == stmt);
4667 code = gimple_assign_rhs_code (orig_stmt);
4668 /* For MINUS_EXPR the initial vector is [init_val,0,...,0], therefore,
4669 partial results are added and not subtracted. */
4670 if (code == MINUS_EXPR)
4671 code = PLUS_EXPR;
4673 scalar_dest = gimple_assign_lhs (orig_stmt);
4674 scalar_type = TREE_TYPE (scalar_dest);
4675 scalar_results.create (group_size);
4676 new_scalar_dest = vect_create_destination_var (scalar_dest, NULL);
4677 bitsize = TYPE_SIZE (scalar_type);
4679 /* In case this is a reduction in an inner-loop while vectorizing an outer
4680 loop - we don't need to extract a single scalar result at the end of the
4681 inner-loop (unless it is double reduction, i.e., the use of reduction is
4682 outside the outer-loop). The final vector of partial results will be used
4683 in the vectorized outer-loop, or reduced to a scalar result at the end of
4684 the outer-loop. */
4685 if (nested_in_vect_loop && !double_reduc)
4686 goto vect_finalize_reduction;
4688 /* SLP reduction without reduction chain, e.g.,
4689 # a1 = phi <a2, a0>
4690 # b1 = phi <b2, b0>
4691 a2 = operation (a1)
4692 b2 = operation (b1) */
4693 slp_reduc = (slp_node && !GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)));
4695 /* In case of reduction chain, e.g.,
4696 # a1 = phi <a3, a0>
4697 a2 = operation (a1)
4698 a3 = operation (a2),
4700 we may end up with more than one vector result. Here we reduce them to
4701 one vector. */
4702 if (GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)))
4704 tree first_vect = PHI_RESULT (new_phis[0]);
4705 tree tmp;
4706 gassign *new_vec_stmt = NULL;
4708 vec_dest = vect_create_destination_var (scalar_dest, vectype);
4709 for (k = 1; k < new_phis.length (); k++)
4711 gimple *next_phi = new_phis[k];
4712 tree second_vect = PHI_RESULT (next_phi);
4714 tmp = build2 (code, vectype, first_vect, second_vect);
4715 new_vec_stmt = gimple_build_assign (vec_dest, tmp);
4716 first_vect = make_ssa_name (vec_dest, new_vec_stmt);
4717 gimple_assign_set_lhs (new_vec_stmt, first_vect);
4718 gsi_insert_before (&exit_gsi, new_vec_stmt, GSI_SAME_STMT);
4721 new_phi_result = first_vect;
4722 if (new_vec_stmt)
4724 new_phis.truncate (0);
4725 new_phis.safe_push (new_vec_stmt);
4728 else
4729 new_phi_result = PHI_RESULT (new_phis[0]);
4731 if (STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info) == COND_REDUCTION
4732 && reduc_code != ERROR_MARK)
4734 /* For condition reductions, we have a vector (NEW_PHI_RESULT) containing
4735 various data values where the condition matched and another vector
4736 (INDUCTION_INDEX) containing all the indexes of those matches. We
4737 need to extract the last matching index (which will be the index with
4738 highest value) and use this to index into the data vector.
4739 For the case where there were no matches, the data vector will contain
4740 all default values and the index vector will be all zeros. */
4742 /* Get various versions of the type of the vector of indexes. */
4743 tree index_vec_type = TREE_TYPE (induction_index);
4744 gcc_checking_assert (TYPE_UNSIGNED (index_vec_type));
4745 tree index_scalar_type = TREE_TYPE (index_vec_type);
4746 tree index_vec_cmp_type = build_same_sized_truth_vector_type
4747 (index_vec_type);
4749 /* Get an unsigned integer version of the type of the data vector. */
4750 int scalar_precision = GET_MODE_PRECISION (TYPE_MODE (scalar_type));
4751 tree scalar_type_unsigned = make_unsigned_type (scalar_precision);
4752 tree vectype_unsigned = build_vector_type
4753 (scalar_type_unsigned, TYPE_VECTOR_SUBPARTS (vectype));
4755 /* First we need to create a vector (ZERO_VEC) of zeros and another
4756 vector (MAX_INDEX_VEC) filled with the last matching index, which we
4757 can create using a MAX reduction and then expanding.
4758 In the case where the loop never made any matches, the max index will
4759 be zero. */
4761 /* Vector of {0, 0, 0,...}. */
4762 tree zero_vec = make_ssa_name (vectype);
4763 tree zero_vec_rhs = build_zero_cst (vectype);
4764 gimple *zero_vec_stmt = gimple_build_assign (zero_vec, zero_vec_rhs);
4765 gsi_insert_before (&exit_gsi, zero_vec_stmt, GSI_SAME_STMT);
4767 /* Find maximum value from the vector of found indexes. */
4768 tree max_index = make_ssa_name (index_scalar_type);
4769 gimple *max_index_stmt = gimple_build_assign (max_index, REDUC_MAX_EXPR,
4770 induction_index);
4771 gsi_insert_before (&exit_gsi, max_index_stmt, GSI_SAME_STMT);
4773 /* Vector of {max_index, max_index, max_index,...}. */
4774 tree max_index_vec = make_ssa_name (index_vec_type);
4775 tree max_index_vec_rhs = build_vector_from_val (index_vec_type,
4776 max_index);
4777 gimple *max_index_vec_stmt = gimple_build_assign (max_index_vec,
4778 max_index_vec_rhs);
4779 gsi_insert_before (&exit_gsi, max_index_vec_stmt, GSI_SAME_STMT);
4781 /* Next we compare the new vector (MAX_INDEX_VEC) full of max indexes
4782 with the vector (INDUCTION_INDEX) of found indexes, choosing values
4783 from the data vector (NEW_PHI_RESULT) for matches, 0 (ZERO_VEC)
4784 otherwise. Only one value should match, resulting in a vector
4785 (VEC_COND) with one data value and the rest zeros.
4786 In the case where the loop never made any matches, every index will
4787 match, resulting in a vector with all data values (which will all be
4788 the default value). */
4790 /* Compare the max index vector to the vector of found indexes to find
4791 the position of the max value. */
4792 tree vec_compare = make_ssa_name (index_vec_cmp_type);
4793 gimple *vec_compare_stmt = gimple_build_assign (vec_compare, EQ_EXPR,
4794 induction_index,
4795 max_index_vec);
4796 gsi_insert_before (&exit_gsi, vec_compare_stmt, GSI_SAME_STMT);
4798 /* Use the compare to choose either values from the data vector or
4799 zero. */
4800 tree vec_cond = make_ssa_name (vectype);
4801 gimple *vec_cond_stmt = gimple_build_assign (vec_cond, VEC_COND_EXPR,
4802 vec_compare, new_phi_result,
4803 zero_vec);
4804 gsi_insert_before (&exit_gsi, vec_cond_stmt, GSI_SAME_STMT);
4806 /* Finally we need to extract the data value from the vector (VEC_COND)
4807 into a scalar (MATCHED_DATA_REDUC). Logically we want to do a OR
4808 reduction, but because this doesn't exist, we can use a MAX reduction
4809 instead. The data value might be signed or a float so we need to cast
4810 it first.
4811 In the case where the loop never made any matches, the data values are
4812 all identical, and so will reduce down correctly. */
4814 /* Make the matched data values unsigned. */
4815 tree vec_cond_cast = make_ssa_name (vectype_unsigned);
4816 tree vec_cond_cast_rhs = build1 (VIEW_CONVERT_EXPR, vectype_unsigned,
4817 vec_cond);
4818 gimple *vec_cond_cast_stmt = gimple_build_assign (vec_cond_cast,
4819 VIEW_CONVERT_EXPR,
4820 vec_cond_cast_rhs);
4821 gsi_insert_before (&exit_gsi, vec_cond_cast_stmt, GSI_SAME_STMT);
4823 /* Reduce down to a scalar value. */
4824 tree data_reduc = make_ssa_name (scalar_type_unsigned);
4825 optab ot = optab_for_tree_code (REDUC_MAX_EXPR, vectype_unsigned,
4826 optab_default);
4827 gcc_assert (optab_handler (ot, TYPE_MODE (vectype_unsigned))
4828 != CODE_FOR_nothing);
4829 gimple *data_reduc_stmt = gimple_build_assign (data_reduc,
4830 REDUC_MAX_EXPR,
4831 vec_cond_cast);
4832 gsi_insert_before (&exit_gsi, data_reduc_stmt, GSI_SAME_STMT);
4834 /* Convert the reduced value back to the result type and set as the
4835 result. */
4836 tree data_reduc_cast = build1 (VIEW_CONVERT_EXPR, scalar_type,
4837 data_reduc);
4838 epilog_stmt = gimple_build_assign (new_scalar_dest, data_reduc_cast);
4839 new_temp = make_ssa_name (new_scalar_dest, epilog_stmt);
4840 gimple_assign_set_lhs (epilog_stmt, new_temp);
4841 gsi_insert_before (&exit_gsi, epilog_stmt, GSI_SAME_STMT);
4842 scalar_results.safe_push (new_temp);
4844 else if (STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info) == COND_REDUCTION
4845 && reduc_code == ERROR_MARK)
4847 /* Condition redution without supported REDUC_MAX_EXPR. Generate
4848 idx = 0;
4849 idx_val = induction_index[0];
4850 val = data_reduc[0];
4851 for (idx = 0, val = init, i = 0; i < nelts; ++i)
4852 if (induction_index[i] > idx_val)
4853 val = data_reduc[i], idx_val = induction_index[i];
4854 return val; */
4856 tree data_eltype = TREE_TYPE (TREE_TYPE (new_phi_result));
4857 tree idx_eltype = TREE_TYPE (TREE_TYPE (induction_index));
4858 unsigned HOST_WIDE_INT el_size = tree_to_uhwi (TYPE_SIZE (idx_eltype));
4859 unsigned HOST_WIDE_INT v_size
4860 = el_size * TYPE_VECTOR_SUBPARTS (TREE_TYPE (induction_index));
4861 tree idx_val = NULL_TREE, val = NULL_TREE;
4862 for (unsigned HOST_WIDE_INT off = 0; off < v_size; off += el_size)
4864 tree old_idx_val = idx_val;
4865 tree old_val = val;
4866 idx_val = make_ssa_name (idx_eltype);
4867 epilog_stmt = gimple_build_assign (idx_val, BIT_FIELD_REF,
4868 build3 (BIT_FIELD_REF, idx_eltype,
4869 induction_index,
4870 bitsize_int (el_size),
4871 bitsize_int (off)));
4872 gsi_insert_before (&exit_gsi, epilog_stmt, GSI_SAME_STMT);
4873 val = make_ssa_name (data_eltype);
4874 epilog_stmt = gimple_build_assign (val, BIT_FIELD_REF,
4875 build3 (BIT_FIELD_REF,
4876 data_eltype,
4877 new_phi_result,
4878 bitsize_int (el_size),
4879 bitsize_int (off)));
4880 gsi_insert_before (&exit_gsi, epilog_stmt, GSI_SAME_STMT);
4881 if (off != 0)
4883 tree new_idx_val = idx_val;
4884 tree new_val = val;
4885 if (off != v_size - el_size)
4887 new_idx_val = make_ssa_name (idx_eltype);
4888 epilog_stmt = gimple_build_assign (new_idx_val,
4889 MAX_EXPR, idx_val,
4890 old_idx_val);
4891 gsi_insert_before (&exit_gsi, epilog_stmt, GSI_SAME_STMT);
4893 new_val = make_ssa_name (data_eltype);
4894 epilog_stmt = gimple_build_assign (new_val,
4895 COND_EXPR,
4896 build2 (GT_EXPR,
4897 boolean_type_node,
4898 idx_val,
4899 old_idx_val),
4900 val, old_val);
4901 gsi_insert_before (&exit_gsi, epilog_stmt, GSI_SAME_STMT);
4902 idx_val = new_idx_val;
4903 val = new_val;
4906 scalar_results.safe_push (val);
4909 /* 2.3 Create the reduction code, using one of the three schemes described
4910 above. In SLP we simply need to extract all the elements from the
4911 vector (without reducing them), so we use scalar shifts. */
4912 else if (reduc_code != ERROR_MARK && !slp_reduc)
4914 tree tmp;
4915 tree vec_elem_type;
4917 /* Case 1: Create:
4918 v_out2 = reduc_expr <v_out1> */
4920 if (dump_enabled_p ())
4921 dump_printf_loc (MSG_NOTE, vect_location,
4922 "Reduce using direct vector reduction.\n");
4924 vec_elem_type = TREE_TYPE (TREE_TYPE (new_phi_result));
4925 if (!useless_type_conversion_p (scalar_type, vec_elem_type))
4927 tree tmp_dest =
4928 vect_create_destination_var (scalar_dest, vec_elem_type);
4929 tmp = build1 (reduc_code, vec_elem_type, new_phi_result);
4930 epilog_stmt = gimple_build_assign (tmp_dest, tmp);
4931 new_temp = make_ssa_name (tmp_dest, epilog_stmt);
4932 gimple_assign_set_lhs (epilog_stmt, new_temp);
4933 gsi_insert_before (&exit_gsi, epilog_stmt, GSI_SAME_STMT);
4935 tmp = build1 (NOP_EXPR, scalar_type, new_temp);
4937 else
4938 tmp = build1 (reduc_code, scalar_type, new_phi_result);
4940 epilog_stmt = gimple_build_assign (new_scalar_dest, tmp);
4941 new_temp = make_ssa_name (new_scalar_dest, epilog_stmt);
4942 gimple_assign_set_lhs (epilog_stmt, new_temp);
4943 gsi_insert_before (&exit_gsi, epilog_stmt, GSI_SAME_STMT);
4945 if (STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info)
4946 == INTEGER_INDUC_COND_REDUCTION)
4948 /* Earlier we set the initial value to be zero. Check the result
4949 and if it is zero then replace with the original initial
4950 value. */
4951 tree zero = build_zero_cst (scalar_type);
4952 tree zcompare = build2 (EQ_EXPR, boolean_type_node, new_temp, zero);
4954 tmp = make_ssa_name (new_scalar_dest);
4955 epilog_stmt = gimple_build_assign (tmp, COND_EXPR, zcompare,
4956 initial_def, new_temp);
4957 gsi_insert_before (&exit_gsi, epilog_stmt, GSI_SAME_STMT);
4958 new_temp = tmp;
4961 scalar_results.safe_push (new_temp);
4963 else
4965 bool reduce_with_shift = have_whole_vector_shift (mode);
4966 int element_bitsize = tree_to_uhwi (bitsize);
4967 int vec_size_in_bits = tree_to_uhwi (TYPE_SIZE (vectype));
4968 tree vec_temp;
4970 /* COND reductions all do the final reduction with MAX_EXPR. */
4971 if (code == COND_EXPR)
4972 code = MAX_EXPR;
4974 /* Regardless of whether we have a whole vector shift, if we're
4975 emulating the operation via tree-vect-generic, we don't want
4976 to use it. Only the first round of the reduction is likely
4977 to still be profitable via emulation. */
4978 /* ??? It might be better to emit a reduction tree code here, so that
4979 tree-vect-generic can expand the first round via bit tricks. */
4980 if (!VECTOR_MODE_P (mode))
4981 reduce_with_shift = false;
4982 else
4984 optab optab = optab_for_tree_code (code, vectype, optab_default);
4985 if (optab_handler (optab, mode) == CODE_FOR_nothing)
4986 reduce_with_shift = false;
4989 if (reduce_with_shift && !slp_reduc)
4991 int nelements = vec_size_in_bits / element_bitsize;
4992 unsigned char *sel = XALLOCAVEC (unsigned char, nelements);
4994 int elt_offset;
4996 tree zero_vec = build_zero_cst (vectype);
4997 /* Case 2: Create:
4998 for (offset = nelements/2; offset >= 1; offset/=2)
5000 Create: va' = vec_shift <va, offset>
5001 Create: va = vop <va, va'>
5002 } */
5004 tree rhs;
5006 if (dump_enabled_p ())
5007 dump_printf_loc (MSG_NOTE, vect_location,
5008 "Reduce using vector shifts\n");
5010 vec_dest = vect_create_destination_var (scalar_dest, vectype);
5011 new_temp = new_phi_result;
5012 for (elt_offset = nelements / 2;
5013 elt_offset >= 1;
5014 elt_offset /= 2)
5016 calc_vec_perm_mask_for_shift (mode, elt_offset, sel);
5017 tree mask = vect_gen_perm_mask_any (vectype, sel);
5018 epilog_stmt = gimple_build_assign (vec_dest, VEC_PERM_EXPR,
5019 new_temp, zero_vec, mask);
5020 new_name = make_ssa_name (vec_dest, epilog_stmt);
5021 gimple_assign_set_lhs (epilog_stmt, new_name);
5022 gsi_insert_before (&exit_gsi, epilog_stmt, GSI_SAME_STMT);
5024 epilog_stmt = gimple_build_assign (vec_dest, code, new_name,
5025 new_temp);
5026 new_temp = make_ssa_name (vec_dest, epilog_stmt);
5027 gimple_assign_set_lhs (epilog_stmt, new_temp);
5028 gsi_insert_before (&exit_gsi, epilog_stmt, GSI_SAME_STMT);
5031 /* 2.4 Extract the final scalar result. Create:
5032 s_out3 = extract_field <v_out2, bitpos> */
5034 if (dump_enabled_p ())
5035 dump_printf_loc (MSG_NOTE, vect_location,
5036 "extract scalar result\n");
5038 rhs = build3 (BIT_FIELD_REF, scalar_type, new_temp,
5039 bitsize, bitsize_zero_node);
5040 epilog_stmt = gimple_build_assign (new_scalar_dest, rhs);
5041 new_temp = make_ssa_name (new_scalar_dest, epilog_stmt);
5042 gimple_assign_set_lhs (epilog_stmt, new_temp);
5043 gsi_insert_before (&exit_gsi, epilog_stmt, GSI_SAME_STMT);
5044 scalar_results.safe_push (new_temp);
5046 else
5048 /* Case 3: Create:
5049 s = extract_field <v_out2, 0>
5050 for (offset = element_size;
5051 offset < vector_size;
5052 offset += element_size;)
5054 Create: s' = extract_field <v_out2, offset>
5055 Create: s = op <s, s'> // For non SLP cases
5056 } */
5058 if (dump_enabled_p ())
5059 dump_printf_loc (MSG_NOTE, vect_location,
5060 "Reduce using scalar code.\n");
5062 vec_size_in_bits = tree_to_uhwi (TYPE_SIZE (vectype));
5063 FOR_EACH_VEC_ELT (new_phis, i, new_phi)
5065 int bit_offset;
5066 if (gimple_code (new_phi) == GIMPLE_PHI)
5067 vec_temp = PHI_RESULT (new_phi);
5068 else
5069 vec_temp = gimple_assign_lhs (new_phi);
5070 tree rhs = build3 (BIT_FIELD_REF, scalar_type, vec_temp, bitsize,
5071 bitsize_zero_node);
5072 epilog_stmt = gimple_build_assign (new_scalar_dest, rhs);
5073 new_temp = make_ssa_name (new_scalar_dest, epilog_stmt);
5074 gimple_assign_set_lhs (epilog_stmt, new_temp);
5075 gsi_insert_before (&exit_gsi, epilog_stmt, GSI_SAME_STMT);
5077 /* In SLP we don't need to apply reduction operation, so we just
5078 collect s' values in SCALAR_RESULTS. */
5079 if (slp_reduc)
5080 scalar_results.safe_push (new_temp);
5082 for (bit_offset = element_bitsize;
5083 bit_offset < vec_size_in_bits;
5084 bit_offset += element_bitsize)
5086 tree bitpos = bitsize_int (bit_offset);
5087 tree rhs = build3 (BIT_FIELD_REF, scalar_type, vec_temp,
5088 bitsize, bitpos);
5090 epilog_stmt = gimple_build_assign (new_scalar_dest, rhs);
5091 new_name = make_ssa_name (new_scalar_dest, epilog_stmt);
5092 gimple_assign_set_lhs (epilog_stmt, new_name);
5093 gsi_insert_before (&exit_gsi, epilog_stmt, GSI_SAME_STMT);
5095 if (slp_reduc)
5097 /* In SLP we don't need to apply reduction operation, so
5098 we just collect s' values in SCALAR_RESULTS. */
5099 new_temp = new_name;
5100 scalar_results.safe_push (new_name);
5102 else
5104 epilog_stmt = gimple_build_assign (new_scalar_dest, code,
5105 new_name, new_temp);
5106 new_temp = make_ssa_name (new_scalar_dest, epilog_stmt);
5107 gimple_assign_set_lhs (epilog_stmt, new_temp);
5108 gsi_insert_before (&exit_gsi, epilog_stmt, GSI_SAME_STMT);
5113 /* The only case where we need to reduce scalar results in SLP, is
5114 unrolling. If the size of SCALAR_RESULTS is greater than
5115 GROUP_SIZE, we reduce them combining elements modulo
5116 GROUP_SIZE. */
5117 if (slp_reduc)
5119 tree res, first_res, new_res;
5120 gimple *new_stmt;
5122 /* Reduce multiple scalar results in case of SLP unrolling. */
5123 for (j = group_size; scalar_results.iterate (j, &res);
5124 j++)
5126 first_res = scalar_results[j % group_size];
5127 new_stmt = gimple_build_assign (new_scalar_dest, code,
5128 first_res, res);
5129 new_res = make_ssa_name (new_scalar_dest, new_stmt);
5130 gimple_assign_set_lhs (new_stmt, new_res);
5131 gsi_insert_before (&exit_gsi, new_stmt, GSI_SAME_STMT);
5132 scalar_results[j % group_size] = new_res;
5135 else
5136 /* Not SLP - we have one scalar to keep in SCALAR_RESULTS. */
5137 scalar_results.safe_push (new_temp);
5140 if (STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info)
5141 == INTEGER_INDUC_COND_REDUCTION)
5143 /* Earlier we set the initial value to be zero. Check the result
5144 and if it is zero then replace with the original initial
5145 value. */
5146 tree zero = build_zero_cst (scalar_type);
5147 tree zcompare = build2 (EQ_EXPR, boolean_type_node, new_temp, zero);
5149 tree tmp = make_ssa_name (new_scalar_dest);
5150 epilog_stmt = gimple_build_assign (tmp, COND_EXPR, zcompare,
5151 initial_def, new_temp);
5152 gsi_insert_before (&exit_gsi, epilog_stmt, GSI_SAME_STMT);
5153 scalar_results[0] = tmp;
5157 vect_finalize_reduction:
5159 if (double_reduc)
5160 loop = loop->inner;
5162 /* 2.5 Adjust the final result by the initial value of the reduction
5163 variable. (When such adjustment is not needed, then
5164 'adjustment_def' is zero). For example, if code is PLUS we create:
5165 new_temp = loop_exit_def + adjustment_def */
5167 if (adjustment_def)
5169 gcc_assert (!slp_reduc);
5170 if (nested_in_vect_loop)
5172 new_phi = new_phis[0];
5173 gcc_assert (TREE_CODE (TREE_TYPE (adjustment_def)) == VECTOR_TYPE);
5174 expr = build2 (code, vectype, PHI_RESULT (new_phi), adjustment_def);
5175 new_dest = vect_create_destination_var (scalar_dest, vectype);
5177 else
5179 new_temp = scalar_results[0];
5180 gcc_assert (TREE_CODE (TREE_TYPE (adjustment_def)) != VECTOR_TYPE);
5181 expr = build2 (code, scalar_type, new_temp, adjustment_def);
5182 new_dest = vect_create_destination_var (scalar_dest, scalar_type);
5185 epilog_stmt = gimple_build_assign (new_dest, expr);
5186 new_temp = make_ssa_name (new_dest, epilog_stmt);
5187 gimple_assign_set_lhs (epilog_stmt, new_temp);
5188 gsi_insert_before (&exit_gsi, epilog_stmt, GSI_SAME_STMT);
5189 if (nested_in_vect_loop)
5191 set_vinfo_for_stmt (epilog_stmt,
5192 new_stmt_vec_info (epilog_stmt, loop_vinfo));
5193 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (epilog_stmt)) =
5194 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (new_phi));
5196 if (!double_reduc)
5197 scalar_results.quick_push (new_temp);
5198 else
5199 scalar_results[0] = new_temp;
5201 else
5202 scalar_results[0] = new_temp;
5204 new_phis[0] = epilog_stmt;
5207 /* 2.6 Handle the loop-exit phis. Replace the uses of scalar loop-exit
5208 phis with new adjusted scalar results, i.e., replace use <s_out0>
5209 with use <s_out4>.
5211 Transform:
5212 loop_exit:
5213 s_out0 = phi <s_loop> # (scalar) EXIT_PHI
5214 v_out1 = phi <VECT_DEF> # NEW_EXIT_PHI
5215 v_out2 = reduce <v_out1>
5216 s_out3 = extract_field <v_out2, 0>
5217 s_out4 = adjust_result <s_out3>
5218 use <s_out0>
5219 use <s_out0>
5221 into:
5223 loop_exit:
5224 s_out0 = phi <s_loop> # (scalar) EXIT_PHI
5225 v_out1 = phi <VECT_DEF> # NEW_EXIT_PHI
5226 v_out2 = reduce <v_out1>
5227 s_out3 = extract_field <v_out2, 0>
5228 s_out4 = adjust_result <s_out3>
5229 use <s_out4>
5230 use <s_out4> */
5233 /* In SLP reduction chain we reduce vector results into one vector if
5234 necessary, hence we set here GROUP_SIZE to 1. SCALAR_DEST is the LHS of
5235 the last stmt in the reduction chain, since we are looking for the loop
5236 exit phi node. */
5237 if (GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)))
5239 gimple *dest_stmt = SLP_TREE_SCALAR_STMTS (slp_node)[group_size - 1];
5240 /* Handle reduction patterns. */
5241 if (STMT_VINFO_RELATED_STMT (vinfo_for_stmt (dest_stmt)))
5242 dest_stmt = STMT_VINFO_RELATED_STMT (vinfo_for_stmt (dest_stmt));
5244 scalar_dest = gimple_assign_lhs (dest_stmt);
5245 group_size = 1;
5248 /* In SLP we may have several statements in NEW_PHIS and REDUCTION_PHIS (in
5249 case that GROUP_SIZE is greater than vectorization factor). Therefore, we
5250 need to match SCALAR_RESULTS with corresponding statements. The first
5251 (GROUP_SIZE / number of new vector stmts) scalar results correspond to
5252 the first vector stmt, etc.
5253 (RATIO is equal to (GROUP_SIZE / number of new vector stmts)). */
5254 if (group_size > new_phis.length ())
5256 ratio = group_size / new_phis.length ();
5257 gcc_assert (!(group_size % new_phis.length ()));
5259 else
5260 ratio = 1;
5262 for (k = 0; k < group_size; k++)
5264 if (k % ratio == 0)
5266 epilog_stmt = new_phis[k / ratio];
5267 reduction_phi = reduction_phis[k / ratio];
5268 if (double_reduc)
5269 inner_phi = inner_phis[k / ratio];
5272 if (slp_reduc)
5274 gimple *current_stmt = SLP_TREE_SCALAR_STMTS (slp_node)[k];
5276 orig_stmt = STMT_VINFO_RELATED_STMT (vinfo_for_stmt (current_stmt));
5277 /* SLP statements can't participate in patterns. */
5278 gcc_assert (!orig_stmt);
5279 scalar_dest = gimple_assign_lhs (current_stmt);
5282 phis.create (3);
5283 /* Find the loop-closed-use at the loop exit of the original scalar
5284 result. (The reduction result is expected to have two immediate uses -
5285 one at the latch block, and one at the loop exit). */
5286 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, scalar_dest)
5287 if (!flow_bb_inside_loop_p (loop, gimple_bb (USE_STMT (use_p)))
5288 && !is_gimple_debug (USE_STMT (use_p)))
5289 phis.safe_push (USE_STMT (use_p));
5291 /* While we expect to have found an exit_phi because of loop-closed-ssa
5292 form we can end up without one if the scalar cycle is dead. */
5294 FOR_EACH_VEC_ELT (phis, i, exit_phi)
5296 if (outer_loop)
5298 stmt_vec_info exit_phi_vinfo = vinfo_for_stmt (exit_phi);
5299 gphi *vect_phi;
5301 /* FORNOW. Currently not supporting the case that an inner-loop
5302 reduction is not used in the outer-loop (but only outside the
5303 outer-loop), unless it is double reduction. */
5304 gcc_assert ((STMT_VINFO_RELEVANT_P (exit_phi_vinfo)
5305 && !STMT_VINFO_LIVE_P (exit_phi_vinfo))
5306 || double_reduc);
5308 if (double_reduc)
5309 STMT_VINFO_VEC_STMT (exit_phi_vinfo) = inner_phi;
5310 else
5311 STMT_VINFO_VEC_STMT (exit_phi_vinfo) = epilog_stmt;
5312 if (!double_reduc
5313 || STMT_VINFO_DEF_TYPE (exit_phi_vinfo)
5314 != vect_double_reduction_def)
5315 continue;
5317 /* Handle double reduction:
5319 stmt1: s1 = phi <s0, s2> - double reduction phi (outer loop)
5320 stmt2: s3 = phi <s1, s4> - (regular) reduc phi (inner loop)
5321 stmt3: s4 = use (s3) - (regular) reduc stmt (inner loop)
5322 stmt4: s2 = phi <s4> - double reduction stmt (outer loop)
5324 At that point the regular reduction (stmt2 and stmt3) is
5325 already vectorized, as well as the exit phi node, stmt4.
5326 Here we vectorize the phi node of double reduction, stmt1, and
5327 update all relevant statements. */
5329 /* Go through all the uses of s2 to find double reduction phi
5330 node, i.e., stmt1 above. */
5331 orig_name = PHI_RESULT (exit_phi);
5332 FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, orig_name)
5334 stmt_vec_info use_stmt_vinfo;
5335 stmt_vec_info new_phi_vinfo;
5336 tree vect_phi_init, preheader_arg, vect_phi_res, init_def;
5337 basic_block bb = gimple_bb (use_stmt);
5338 gimple *use;
5340 /* Check that USE_STMT is really double reduction phi
5341 node. */
5342 if (gimple_code (use_stmt) != GIMPLE_PHI
5343 || gimple_phi_num_args (use_stmt) != 2
5344 || bb->loop_father != outer_loop)
5345 continue;
5346 use_stmt_vinfo = vinfo_for_stmt (use_stmt);
5347 if (!use_stmt_vinfo
5348 || STMT_VINFO_DEF_TYPE (use_stmt_vinfo)
5349 != vect_double_reduction_def)
5350 continue;
5352 /* Create vector phi node for double reduction:
5353 vs1 = phi <vs0, vs2>
5354 vs1 was created previously in this function by a call to
5355 vect_get_vec_def_for_operand and is stored in
5356 vec_initial_def;
5357 vs2 is defined by INNER_PHI, the vectorized EXIT_PHI;
5358 vs0 is created here. */
5360 /* Create vector phi node. */
5361 vect_phi = create_phi_node (vec_initial_def, bb);
5362 new_phi_vinfo = new_stmt_vec_info (vect_phi,
5363 loop_vec_info_for_loop (outer_loop));
5364 set_vinfo_for_stmt (vect_phi, new_phi_vinfo);
5366 /* Create vs0 - initial def of the double reduction phi. */
5367 preheader_arg = PHI_ARG_DEF_FROM_EDGE (use_stmt,
5368 loop_preheader_edge (outer_loop));
5369 init_def = get_initial_def_for_reduction (stmt,
5370 preheader_arg, NULL);
5371 vect_phi_init = vect_init_vector (use_stmt, init_def,
5372 vectype, NULL);
5374 /* Update phi node arguments with vs0 and vs2. */
5375 add_phi_arg (vect_phi, vect_phi_init,
5376 loop_preheader_edge (outer_loop),
5377 UNKNOWN_LOCATION);
5378 add_phi_arg (vect_phi, PHI_RESULT (inner_phi),
5379 loop_latch_edge (outer_loop), UNKNOWN_LOCATION);
5380 if (dump_enabled_p ())
5382 dump_printf_loc (MSG_NOTE, vect_location,
5383 "created double reduction phi node: ");
5384 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, vect_phi, 0);
5387 vect_phi_res = PHI_RESULT (vect_phi);
5389 /* Replace the use, i.e., set the correct vs1 in the regular
5390 reduction phi node. FORNOW, NCOPIES is always 1, so the
5391 loop is redundant. */
5392 use = reduction_phi;
5393 for (j = 0; j < ncopies; j++)
5395 edge pr_edge = loop_preheader_edge (loop);
5396 SET_PHI_ARG_DEF (use, pr_edge->dest_idx, vect_phi_res);
5397 use = STMT_VINFO_RELATED_STMT (vinfo_for_stmt (use));
5403 phis.release ();
5404 if (nested_in_vect_loop)
5406 if (double_reduc)
5407 loop = outer_loop;
5408 else
5409 continue;
5412 phis.create (3);
5413 /* Find the loop-closed-use at the loop exit of the original scalar
5414 result. (The reduction result is expected to have two immediate uses,
5415 one at the latch block, and one at the loop exit). For double
5416 reductions we are looking for exit phis of the outer loop. */
5417 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, scalar_dest)
5419 if (!flow_bb_inside_loop_p (loop, gimple_bb (USE_STMT (use_p))))
5421 if (!is_gimple_debug (USE_STMT (use_p)))
5422 phis.safe_push (USE_STMT (use_p));
5424 else
5426 if (double_reduc && gimple_code (USE_STMT (use_p)) == GIMPLE_PHI)
5428 tree phi_res = PHI_RESULT (USE_STMT (use_p));
5430 FOR_EACH_IMM_USE_FAST (phi_use_p, phi_imm_iter, phi_res)
5432 if (!flow_bb_inside_loop_p (loop,
5433 gimple_bb (USE_STMT (phi_use_p)))
5434 && !is_gimple_debug (USE_STMT (phi_use_p)))
5435 phis.safe_push (USE_STMT (phi_use_p));
5441 FOR_EACH_VEC_ELT (phis, i, exit_phi)
5443 /* Replace the uses: */
5444 orig_name = PHI_RESULT (exit_phi);
5445 scalar_result = scalar_results[k];
5446 FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, orig_name)
5447 FOR_EACH_IMM_USE_ON_STMT (use_p, imm_iter)
5448 SET_USE (use_p, scalar_result);
5451 phis.release ();
5456 /* Function is_nonwrapping_integer_induction.
5458 Check if STMT (which is part of loop LOOP) both increments and
5459 does not cause overflow. */
5461 static bool
5462 is_nonwrapping_integer_induction (gimple *stmt, struct loop *loop)
5464 stmt_vec_info stmt_vinfo = vinfo_for_stmt (stmt);
5465 tree base = STMT_VINFO_LOOP_PHI_EVOLUTION_BASE_UNCHANGED (stmt_vinfo);
5466 tree step = STMT_VINFO_LOOP_PHI_EVOLUTION_PART (stmt_vinfo);
5467 tree lhs_type = TREE_TYPE (gimple_phi_result (stmt));
5468 widest_int ni, max_loop_value, lhs_max;
5469 bool overflow = false;
5471 /* Make sure the loop is integer based. */
5472 if (TREE_CODE (base) != INTEGER_CST
5473 || TREE_CODE (step) != INTEGER_CST)
5474 return false;
5476 /* Check that the induction increments. */
5477 if (tree_int_cst_sgn (step) == -1)
5478 return false;
5480 /* Check that the max size of the loop will not wrap. */
5482 if (TYPE_OVERFLOW_UNDEFINED (lhs_type))
5483 return true;
5485 if (! max_stmt_executions (loop, &ni))
5486 return false;
5488 max_loop_value = wi::mul (wi::to_widest (step), ni, TYPE_SIGN (lhs_type),
5489 &overflow);
5490 if (overflow)
5491 return false;
5493 max_loop_value = wi::add (wi::to_widest (base), max_loop_value,
5494 TYPE_SIGN (lhs_type), &overflow);
5495 if (overflow)
5496 return false;
5498 return (wi::min_precision (max_loop_value, TYPE_SIGN (lhs_type))
5499 <= TYPE_PRECISION (lhs_type));
5502 /* Function vectorizable_reduction.
5504 Check if STMT performs a reduction operation that can be vectorized.
5505 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
5506 stmt to replace it, put it in VEC_STMT, and insert it at GSI.
5507 Return FALSE if not a vectorizable STMT, TRUE otherwise.
5509 This function also handles reduction idioms (patterns) that have been
5510 recognized in advance during vect_pattern_recog. In this case, STMT may be
5511 of this form:
5512 X = pattern_expr (arg0, arg1, ..., X)
5513 and it's STMT_VINFO_RELATED_STMT points to the last stmt in the original
5514 sequence that had been detected and replaced by the pattern-stmt (STMT).
5516 This function also handles reduction of condition expressions, for example:
5517 for (int i = 0; i < N; i++)
5518 if (a[i] < value)
5519 last = a[i];
5520 This is handled by vectorising the loop and creating an additional vector
5521 containing the loop indexes for which "a[i] < value" was true. In the
5522 function epilogue this is reduced to a single max value and then used to
5523 index into the vector of results.
5525 In some cases of reduction patterns, the type of the reduction variable X is
5526 different than the type of the other arguments of STMT.
5527 In such cases, the vectype that is used when transforming STMT into a vector
5528 stmt is different than the vectype that is used to determine the
5529 vectorization factor, because it consists of a different number of elements
5530 than the actual number of elements that are being operated upon in parallel.
5532 For example, consider an accumulation of shorts into an int accumulator.
5533 On some targets it's possible to vectorize this pattern operating on 8
5534 shorts at a time (hence, the vectype for purposes of determining the
5535 vectorization factor should be V8HI); on the other hand, the vectype that
5536 is used to create the vector form is actually V4SI (the type of the result).
5538 Upon entry to this function, STMT_VINFO_VECTYPE records the vectype that
5539 indicates what is the actual level of parallelism (V8HI in the example), so
5540 that the right vectorization factor would be derived. This vectype
5541 corresponds to the type of arguments to the reduction stmt, and should *NOT*
5542 be used to create the vectorized stmt. The right vectype for the vectorized
5543 stmt is obtained from the type of the result X:
5544 get_vectype_for_scalar_type (TREE_TYPE (X))
5546 This means that, contrary to "regular" reductions (or "regular" stmts in
5547 general), the following equation:
5548 STMT_VINFO_VECTYPE == get_vectype_for_scalar_type (TREE_TYPE (X))
5549 does *NOT* necessarily hold for reduction patterns. */
5551 bool
5552 vectorizable_reduction (gimple *stmt, gimple_stmt_iterator *gsi,
5553 gimple **vec_stmt, slp_tree slp_node)
5555 tree vec_dest;
5556 tree scalar_dest;
5557 tree loop_vec_def0 = NULL_TREE, loop_vec_def1 = NULL_TREE;
5558 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
5559 tree vectype_out = STMT_VINFO_VECTYPE (stmt_info);
5560 tree vectype_in = NULL_TREE;
5561 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
5562 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
5563 enum tree_code code, orig_code, epilog_reduc_code;
5564 machine_mode vec_mode;
5565 int op_type;
5566 optab optab, reduc_optab;
5567 tree new_temp = NULL_TREE;
5568 gimple *def_stmt;
5569 enum vect_def_type dt, cond_reduc_dt = vect_unknown_def_type;
5570 gphi *new_phi = NULL;
5571 tree scalar_type;
5572 bool is_simple_use;
5573 gimple *orig_stmt;
5574 stmt_vec_info orig_stmt_info;
5575 tree expr = NULL_TREE;
5576 int i;
5577 int ncopies;
5578 int epilog_copies;
5579 stmt_vec_info prev_stmt_info, prev_phi_info;
5580 bool single_defuse_cycle = false;
5581 tree reduc_def = NULL_TREE;
5582 gimple *new_stmt = NULL;
5583 int j;
5584 tree ops[3];
5585 bool nested_cycle = false, found_nested_cycle_def = false;
5586 gimple *reduc_def_stmt = NULL;
5587 bool double_reduc = false;
5588 basic_block def_bb;
5589 struct loop * def_stmt_loop, *outer_loop = NULL;
5590 tree def_arg;
5591 gimple *def_arg_stmt;
5592 auto_vec<tree> vec_oprnds0;
5593 auto_vec<tree> vec_oprnds1;
5594 auto_vec<tree> vect_defs;
5595 auto_vec<gimple *> phis;
5596 int vec_num;
5597 tree def0, def1, tem, op1 = NULL_TREE;
5598 bool first_p = true;
5599 tree cr_index_scalar_type = NULL_TREE, cr_index_vector_type = NULL_TREE;
5600 tree cond_reduc_val = NULL_TREE;
5602 /* In case of reduction chain we switch to the first stmt in the chain, but
5603 we don't update STMT_INFO, since only the last stmt is marked as reduction
5604 and has reduction properties. */
5605 if (GROUP_FIRST_ELEMENT (stmt_info)
5606 && GROUP_FIRST_ELEMENT (stmt_info) != stmt)
5608 stmt = GROUP_FIRST_ELEMENT (stmt_info);
5609 first_p = false;
5612 if (nested_in_vect_loop_p (loop, stmt))
5614 outer_loop = loop;
5615 loop = loop->inner;
5616 nested_cycle = true;
5619 /* 1. Is vectorizable reduction? */
5620 /* Not supportable if the reduction variable is used in the loop, unless
5621 it's a reduction chain. */
5622 if (STMT_VINFO_RELEVANT (stmt_info) > vect_used_in_outer
5623 && !GROUP_FIRST_ELEMENT (stmt_info))
5624 return false;
5626 /* Reductions that are not used even in an enclosing outer-loop,
5627 are expected to be "live" (used out of the loop). */
5628 if (STMT_VINFO_RELEVANT (stmt_info) == vect_unused_in_scope
5629 && !STMT_VINFO_LIVE_P (stmt_info))
5630 return false;
5632 /* Make sure it was already recognized as a reduction computation. */
5633 if (STMT_VINFO_DEF_TYPE (vinfo_for_stmt (stmt)) != vect_reduction_def
5634 && STMT_VINFO_DEF_TYPE (vinfo_for_stmt (stmt)) != vect_nested_cycle)
5635 return false;
5637 /* 2. Has this been recognized as a reduction pattern?
5639 Check if STMT represents a pattern that has been recognized
5640 in earlier analysis stages. For stmts that represent a pattern,
5641 the STMT_VINFO_RELATED_STMT field records the last stmt in
5642 the original sequence that constitutes the pattern. */
5644 orig_stmt = STMT_VINFO_RELATED_STMT (vinfo_for_stmt (stmt));
5645 if (orig_stmt)
5647 orig_stmt_info = vinfo_for_stmt (orig_stmt);
5648 gcc_assert (STMT_VINFO_IN_PATTERN_P (orig_stmt_info));
5649 gcc_assert (!STMT_VINFO_IN_PATTERN_P (stmt_info));
5652 /* 3. Check the operands of the operation. The first operands are defined
5653 inside the loop body. The last operand is the reduction variable,
5654 which is defined by the loop-header-phi. */
5656 gcc_assert (is_gimple_assign (stmt));
5658 /* Flatten RHS. */
5659 switch (get_gimple_rhs_class (gimple_assign_rhs_code (stmt)))
5661 case GIMPLE_BINARY_RHS:
5662 code = gimple_assign_rhs_code (stmt);
5663 op_type = TREE_CODE_LENGTH (code);
5664 gcc_assert (op_type == binary_op);
5665 ops[0] = gimple_assign_rhs1 (stmt);
5666 ops[1] = gimple_assign_rhs2 (stmt);
5667 break;
5669 case GIMPLE_TERNARY_RHS:
5670 code = gimple_assign_rhs_code (stmt);
5671 op_type = TREE_CODE_LENGTH (code);
5672 gcc_assert (op_type == ternary_op);
5673 ops[0] = gimple_assign_rhs1 (stmt);
5674 ops[1] = gimple_assign_rhs2 (stmt);
5675 ops[2] = gimple_assign_rhs3 (stmt);
5676 break;
5678 case GIMPLE_UNARY_RHS:
5679 return false;
5681 default:
5682 gcc_unreachable ();
5684 /* The default is that the reduction variable is the last in statement. */
5685 int reduc_index = op_type - 1;
5686 if (code == MINUS_EXPR)
5687 reduc_index = 0;
5689 if (code == COND_EXPR && slp_node)
5690 return false;
5692 scalar_dest = gimple_assign_lhs (stmt);
5693 scalar_type = TREE_TYPE (scalar_dest);
5694 if (!POINTER_TYPE_P (scalar_type) && !INTEGRAL_TYPE_P (scalar_type)
5695 && !SCALAR_FLOAT_TYPE_P (scalar_type))
5696 return false;
5698 /* Do not try to vectorize bit-precision reductions. */
5699 if ((TYPE_PRECISION (scalar_type)
5700 != GET_MODE_PRECISION (TYPE_MODE (scalar_type))))
5701 return false;
5703 /* All uses but the last are expected to be defined in the loop.
5704 The last use is the reduction variable. In case of nested cycle this
5705 assumption is not true: we use reduc_index to record the index of the
5706 reduction variable. */
5707 for (i = 0; i < op_type; i++)
5709 if (i == reduc_index)
5710 continue;
5712 /* The condition of COND_EXPR is checked in vectorizable_condition(). */
5713 if (i == 0 && code == COND_EXPR)
5714 continue;
5716 is_simple_use = vect_is_simple_use (ops[i], loop_vinfo,
5717 &def_stmt, &dt, &tem);
5718 if (!vectype_in)
5719 vectype_in = tem;
5720 gcc_assert (is_simple_use);
5722 if (dt != vect_internal_def
5723 && dt != vect_external_def
5724 && dt != vect_constant_def
5725 && dt != vect_induction_def
5726 && !(dt == vect_nested_cycle && nested_cycle))
5727 return false;
5729 if (dt == vect_nested_cycle)
5731 found_nested_cycle_def = true;
5732 reduc_def_stmt = def_stmt;
5733 reduc_index = i;
5736 if (i == 1 && code == COND_EXPR)
5738 /* Record how value of COND_EXPR is defined. */
5739 if (dt == vect_constant_def)
5741 cond_reduc_dt = dt;
5742 cond_reduc_val = ops[i];
5744 if (dt == vect_induction_def && def_stmt != NULL
5745 && is_nonwrapping_integer_induction (def_stmt, loop))
5746 cond_reduc_dt = dt;
5750 is_simple_use = vect_is_simple_use (ops[reduc_index], loop_vinfo,
5751 &def_stmt, &dt, &tem);
5752 if (!vectype_in)
5753 vectype_in = tem;
5754 gcc_assert (is_simple_use);
5755 if (!found_nested_cycle_def)
5756 reduc_def_stmt = def_stmt;
5758 if (reduc_def_stmt && gimple_code (reduc_def_stmt) != GIMPLE_PHI)
5759 return false;
5761 if (!(dt == vect_reduction_def
5762 || dt == vect_nested_cycle
5763 || ((dt == vect_internal_def || dt == vect_external_def
5764 || dt == vect_constant_def || dt == vect_induction_def)
5765 && nested_cycle && found_nested_cycle_def)))
5767 /* For pattern recognized stmts, orig_stmt might be a reduction,
5768 but some helper statements for the pattern might not, or
5769 might be COND_EXPRs with reduction uses in the condition. */
5770 gcc_assert (orig_stmt);
5771 return false;
5774 stmt_vec_info reduc_def_info = vinfo_for_stmt (reduc_def_stmt);
5775 enum vect_reduction_type v_reduc_type
5776 = STMT_VINFO_REDUC_TYPE (reduc_def_info);
5777 gimple *tmp = STMT_VINFO_REDUC_DEF (reduc_def_info);
5779 STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info) = v_reduc_type;
5780 /* If we have a condition reduction, see if we can simplify it further. */
5781 if (v_reduc_type == COND_REDUCTION)
5783 if (cond_reduc_dt == vect_induction_def)
5785 if (dump_enabled_p ())
5786 dump_printf_loc (MSG_NOTE, vect_location,
5787 "condition expression based on "
5788 "integer induction.\n");
5789 STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info)
5790 = INTEGER_INDUC_COND_REDUCTION;
5793 /* Loop peeling modifies initial value of reduction PHI, which
5794 makes the reduction stmt to be transformed different to the
5795 original stmt analyzed. We need to record reduction code for
5796 CONST_COND_REDUCTION type reduction at analyzing stage, thus
5797 it can be used directly at transform stage. */
5798 if (STMT_VINFO_VEC_CONST_COND_REDUC_CODE (stmt_info) == MAX_EXPR
5799 || STMT_VINFO_VEC_CONST_COND_REDUC_CODE (stmt_info) == MIN_EXPR)
5801 /* Also set the reduction type to CONST_COND_REDUCTION. */
5802 gcc_assert (cond_reduc_dt == vect_constant_def);
5803 STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info) = CONST_COND_REDUCTION;
5805 else if (cond_reduc_dt == vect_constant_def)
5807 enum vect_def_type cond_initial_dt;
5808 gimple *def_stmt = SSA_NAME_DEF_STMT (ops[reduc_index]);
5809 tree cond_initial_val
5810 = PHI_ARG_DEF_FROM_EDGE (def_stmt, loop_preheader_edge (loop));
5812 gcc_assert (cond_reduc_val != NULL_TREE);
5813 vect_is_simple_use (cond_initial_val, loop_vinfo,
5814 &def_stmt, &cond_initial_dt);
5815 if (cond_initial_dt == vect_constant_def
5816 && types_compatible_p (TREE_TYPE (cond_initial_val),
5817 TREE_TYPE (cond_reduc_val)))
5819 tree e = fold_build2 (LE_EXPR, boolean_type_node,
5820 cond_initial_val, cond_reduc_val);
5821 if (e && (integer_onep (e) || integer_zerop (e)))
5823 if (dump_enabled_p ())
5824 dump_printf_loc (MSG_NOTE, vect_location,
5825 "condition expression based on "
5826 "compile time constant.\n");
5827 /* Record reduction code at analysis stage. */
5828 STMT_VINFO_VEC_CONST_COND_REDUC_CODE (stmt_info)
5829 = integer_onep (e) ? MAX_EXPR : MIN_EXPR;
5830 STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info)
5831 = CONST_COND_REDUCTION;
5837 if (orig_stmt)
5838 gcc_assert (tmp == orig_stmt
5839 || GROUP_FIRST_ELEMENT (vinfo_for_stmt (tmp)) == orig_stmt);
5840 else
5841 /* We changed STMT to be the first stmt in reduction chain, hence we
5842 check that in this case the first element in the chain is STMT. */
5843 gcc_assert (stmt == tmp
5844 || GROUP_FIRST_ELEMENT (vinfo_for_stmt (tmp)) == stmt);
5846 if (STMT_VINFO_LIVE_P (vinfo_for_stmt (reduc_def_stmt)))
5847 return false;
5849 if (slp_node)
5850 ncopies = 1;
5851 else
5852 ncopies = (LOOP_VINFO_VECT_FACTOR (loop_vinfo)
5853 / TYPE_VECTOR_SUBPARTS (vectype_in));
5855 gcc_assert (ncopies >= 1);
5857 vec_mode = TYPE_MODE (vectype_in);
5859 if (code == COND_EXPR)
5861 /* Only call during the analysis stage, otherwise we'll lose
5862 STMT_VINFO_TYPE. */
5863 if (!vec_stmt && !vectorizable_condition (stmt, gsi, NULL,
5864 ops[reduc_index], 0, NULL))
5866 if (dump_enabled_p ())
5867 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5868 "unsupported condition in reduction\n");
5869 return false;
5872 else
5874 /* 4. Supportable by target? */
5876 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR
5877 || code == LROTATE_EXPR || code == RROTATE_EXPR)
5879 /* Shifts and rotates are only supported by vectorizable_shifts,
5880 not vectorizable_reduction. */
5881 if (dump_enabled_p ())
5882 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5883 "unsupported shift or rotation.\n");
5884 return false;
5887 /* 4.1. check support for the operation in the loop */
5888 optab = optab_for_tree_code (code, vectype_in, optab_default);
5889 if (!optab)
5891 if (dump_enabled_p ())
5892 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5893 "no optab.\n");
5895 return false;
5898 if (optab_handler (optab, vec_mode) == CODE_FOR_nothing)
5900 if (dump_enabled_p ())
5901 dump_printf (MSG_NOTE, "op not supported by target.\n");
5903 if (GET_MODE_SIZE (vec_mode) != UNITS_PER_WORD
5904 || LOOP_VINFO_VECT_FACTOR (loop_vinfo)
5905 < vect_min_worthwhile_factor (code))
5906 return false;
5908 if (dump_enabled_p ())
5909 dump_printf (MSG_NOTE, "proceeding using word mode.\n");
5912 /* Worthwhile without SIMD support? */
5913 if (!VECTOR_MODE_P (TYPE_MODE (vectype_in))
5914 && LOOP_VINFO_VECT_FACTOR (loop_vinfo)
5915 < vect_min_worthwhile_factor (code))
5917 if (dump_enabled_p ())
5918 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5919 "not worthwhile without SIMD support.\n");
5921 return false;
5925 /* 4.2. Check support for the epilog operation.
5927 If STMT represents a reduction pattern, then the type of the
5928 reduction variable may be different than the type of the rest
5929 of the arguments. For example, consider the case of accumulation
5930 of shorts into an int accumulator; The original code:
5931 S1: int_a = (int) short_a;
5932 orig_stmt-> S2: int_acc = plus <int_a ,int_acc>;
5934 was replaced with:
5935 STMT: int_acc = widen_sum <short_a, int_acc>
5937 This means that:
5938 1. The tree-code that is used to create the vector operation in the
5939 epilog code (that reduces the partial results) is not the
5940 tree-code of STMT, but is rather the tree-code of the original
5941 stmt from the pattern that STMT is replacing. I.e, in the example
5942 above we want to use 'widen_sum' in the loop, but 'plus' in the
5943 epilog.
5944 2. The type (mode) we use to check available target support
5945 for the vector operation to be created in the *epilog*, is
5946 determined by the type of the reduction variable (in the example
5947 above we'd check this: optab_handler (plus_optab, vect_int_mode])).
5948 However the type (mode) we use to check available target support
5949 for the vector operation to be created *inside the loop*, is
5950 determined by the type of the other arguments to STMT (in the
5951 example we'd check this: optab_handler (widen_sum_optab,
5952 vect_short_mode)).
5954 This is contrary to "regular" reductions, in which the types of all
5955 the arguments are the same as the type of the reduction variable.
5956 For "regular" reductions we can therefore use the same vector type
5957 (and also the same tree-code) when generating the epilog code and
5958 when generating the code inside the loop. */
5960 if (orig_stmt)
5962 /* This is a reduction pattern: get the vectype from the type of the
5963 reduction variable, and get the tree-code from orig_stmt. */
5964 gcc_assert (STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info)
5965 == TREE_CODE_REDUCTION);
5966 orig_code = gimple_assign_rhs_code (orig_stmt);
5967 gcc_assert (vectype_out);
5968 vec_mode = TYPE_MODE (vectype_out);
5970 else
5972 /* Regular reduction: use the same vectype and tree-code as used for
5973 the vector code inside the loop can be used for the epilog code. */
5974 orig_code = code;
5976 if (code == MINUS_EXPR)
5977 orig_code = PLUS_EXPR;
5979 /* For simple condition reductions, replace with the actual expression
5980 we want to base our reduction around. */
5981 if (STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info) == CONST_COND_REDUCTION)
5983 orig_code = STMT_VINFO_VEC_CONST_COND_REDUC_CODE (stmt_info);
5984 gcc_assert (orig_code == MAX_EXPR || orig_code == MIN_EXPR);
5986 else if (STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info)
5987 == INTEGER_INDUC_COND_REDUCTION)
5988 orig_code = MAX_EXPR;
5991 if (nested_cycle)
5993 def_bb = gimple_bb (reduc_def_stmt);
5994 def_stmt_loop = def_bb->loop_father;
5995 def_arg = PHI_ARG_DEF_FROM_EDGE (reduc_def_stmt,
5996 loop_preheader_edge (def_stmt_loop));
5997 if (TREE_CODE (def_arg) == SSA_NAME
5998 && (def_arg_stmt = SSA_NAME_DEF_STMT (def_arg))
5999 && gimple_code (def_arg_stmt) == GIMPLE_PHI
6000 && flow_bb_inside_loop_p (outer_loop, gimple_bb (def_arg_stmt))
6001 && vinfo_for_stmt (def_arg_stmt)
6002 && STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def_arg_stmt))
6003 == vect_double_reduction_def)
6004 double_reduc = true;
6007 epilog_reduc_code = ERROR_MARK;
6009 if (STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info) != COND_REDUCTION)
6011 if (reduction_code_for_scalar_code (orig_code, &epilog_reduc_code))
6013 reduc_optab = optab_for_tree_code (epilog_reduc_code, vectype_out,
6014 optab_default);
6015 if (!reduc_optab)
6017 if (dump_enabled_p ())
6018 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6019 "no optab for reduction.\n");
6021 epilog_reduc_code = ERROR_MARK;
6023 else if (optab_handler (reduc_optab, vec_mode) == CODE_FOR_nothing)
6025 if (dump_enabled_p ())
6026 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6027 "reduc op not supported by target.\n");
6029 epilog_reduc_code = ERROR_MARK;
6032 else
6034 if (!nested_cycle || double_reduc)
6036 if (dump_enabled_p ())
6037 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6038 "no reduc code for scalar code.\n");
6040 return false;
6044 else
6046 int scalar_precision = GET_MODE_PRECISION (TYPE_MODE (scalar_type));
6047 cr_index_scalar_type = make_unsigned_type (scalar_precision);
6048 cr_index_vector_type = build_vector_type
6049 (cr_index_scalar_type, TYPE_VECTOR_SUBPARTS (vectype_out));
6051 optab = optab_for_tree_code (REDUC_MAX_EXPR, cr_index_vector_type,
6052 optab_default);
6053 if (optab_handler (optab, TYPE_MODE (cr_index_vector_type))
6054 != CODE_FOR_nothing)
6055 epilog_reduc_code = REDUC_MAX_EXPR;
6058 if ((double_reduc
6059 || STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info) != TREE_CODE_REDUCTION)
6060 && ncopies > 1)
6062 if (dump_enabled_p ())
6063 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6064 "multiple types in double reduction or condition "
6065 "reduction.\n");
6066 return false;
6069 /* In case of widenning multiplication by a constant, we update the type
6070 of the constant to be the type of the other operand. We check that the
6071 constant fits the type in the pattern recognition pass. */
6072 if (code == DOT_PROD_EXPR
6073 && !types_compatible_p (TREE_TYPE (ops[0]), TREE_TYPE (ops[1])))
6075 if (TREE_CODE (ops[0]) == INTEGER_CST)
6076 ops[0] = fold_convert (TREE_TYPE (ops[1]), ops[0]);
6077 else if (TREE_CODE (ops[1]) == INTEGER_CST)
6078 ops[1] = fold_convert (TREE_TYPE (ops[0]), ops[1]);
6079 else
6081 if (dump_enabled_p ())
6082 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6083 "invalid types in dot-prod\n");
6085 return false;
6089 if (STMT_VINFO_VEC_REDUCTION_TYPE (stmt_info) == COND_REDUCTION)
6091 widest_int ni;
6093 if (! max_loop_iterations (loop, &ni))
6095 if (dump_enabled_p ())
6096 dump_printf_loc (MSG_NOTE, vect_location,
6097 "loop count not known, cannot create cond "
6098 "reduction.\n");
6099 return false;
6101 /* Convert backedges to iterations. */
6102 ni += 1;
6104 /* The additional index will be the same type as the condition. Check
6105 that the loop can fit into this less one (because we'll use up the
6106 zero slot for when there are no matches). */
6107 tree max_index = TYPE_MAX_VALUE (cr_index_scalar_type);
6108 if (wi::geu_p (ni, wi::to_widest (max_index)))
6110 if (dump_enabled_p ())
6111 dump_printf_loc (MSG_NOTE, vect_location,
6112 "loop size is greater than data size.\n");
6113 return false;
6117 if (!vec_stmt) /* transformation not required. */
6119 if (first_p)
6120 vect_model_reduction_cost (stmt_info, epilog_reduc_code, ncopies);
6121 STMT_VINFO_TYPE (stmt_info) = reduc_vec_info_type;
6122 return true;
6125 /* Transform. */
6127 if (dump_enabled_p ())
6128 dump_printf_loc (MSG_NOTE, vect_location, "transform reduction.\n");
6130 /* FORNOW: Multiple types are not supported for condition. */
6131 if (code == COND_EXPR)
6132 gcc_assert (ncopies == 1);
6134 /* Create the destination vector */
6135 vec_dest = vect_create_destination_var (scalar_dest, vectype_out);
6137 /* In case the vectorization factor (VF) is bigger than the number
6138 of elements that we can fit in a vectype (nunits), we have to generate
6139 more than one vector stmt - i.e - we need to "unroll" the
6140 vector stmt by a factor VF/nunits. For more details see documentation
6141 in vectorizable_operation. */
6143 /* If the reduction is used in an outer loop we need to generate
6144 VF intermediate results, like so (e.g. for ncopies=2):
6145 r0 = phi (init, r0)
6146 r1 = phi (init, r1)
6147 r0 = x0 + r0;
6148 r1 = x1 + r1;
6149 (i.e. we generate VF results in 2 registers).
6150 In this case we have a separate def-use cycle for each copy, and therefore
6151 for each copy we get the vector def for the reduction variable from the
6152 respective phi node created for this copy.
6154 Otherwise (the reduction is unused in the loop nest), we can combine
6155 together intermediate results, like so (e.g. for ncopies=2):
6156 r = phi (init, r)
6157 r = x0 + r;
6158 r = x1 + r;
6159 (i.e. we generate VF/2 results in a single register).
6160 In this case for each copy we get the vector def for the reduction variable
6161 from the vectorized reduction operation generated in the previous iteration.
6164 if (STMT_VINFO_RELEVANT (stmt_info) <= vect_used_only_live)
6166 single_defuse_cycle = true;
6167 epilog_copies = 1;
6169 else
6170 epilog_copies = ncopies;
6172 prev_stmt_info = NULL;
6173 prev_phi_info = NULL;
6174 if (slp_node)
6175 vec_num = SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node);
6176 else
6178 vec_num = 1;
6179 vec_oprnds0.create (1);
6180 if (op_type == ternary_op)
6181 vec_oprnds1.create (1);
6184 phis.create (vec_num);
6185 vect_defs.create (vec_num);
6186 if (!slp_node)
6187 vect_defs.quick_push (NULL_TREE);
6189 for (j = 0; j < ncopies; j++)
6191 if (j == 0 || !single_defuse_cycle)
6193 for (i = 0; i < vec_num; i++)
6195 /* Create the reduction-phi that defines the reduction
6196 operand. */
6197 new_phi = create_phi_node (vec_dest, loop->header);
6198 set_vinfo_for_stmt (new_phi,
6199 new_stmt_vec_info (new_phi, loop_vinfo));
6200 if (j == 0 || slp_node)
6201 phis.quick_push (new_phi);
6205 if (code == COND_EXPR)
6207 gcc_assert (!slp_node);
6208 vectorizable_condition (stmt, gsi, vec_stmt,
6209 PHI_RESULT (phis[0]),
6210 reduc_index, NULL);
6211 /* Multiple types are not supported for condition. */
6212 break;
6215 /* Handle uses. */
6216 if (j == 0)
6218 if (slp_node)
6220 /* Get vec defs for all the operands except the reduction index,
6221 ensuring the ordering of the ops in the vector is kept. */
6222 auto_vec<tree, 3> slp_ops;
6223 auto_vec<vec<tree>, 3> vec_defs;
6225 slp_ops.quick_push (reduc_index == 0 ? NULL : ops[0]);
6226 slp_ops.quick_push (reduc_index == 1 ? NULL : ops[1]);
6227 if (op_type == ternary_op)
6228 slp_ops.quick_push (reduc_index == 2 ? NULL : ops[2]);
6230 vect_get_slp_defs (slp_ops, slp_node, &vec_defs);
6232 vec_oprnds0.safe_splice (vec_defs[reduc_index == 0 ? 1 : 0]);
6233 vec_defs[reduc_index == 0 ? 1 : 0].release ();
6234 if (op_type == ternary_op)
6236 vec_oprnds1.safe_splice (vec_defs[reduc_index == 2 ? 1 : 2]);
6237 vec_defs[reduc_index == 2 ? 1 : 2].release ();
6240 else
6242 loop_vec_def0 = vect_get_vec_def_for_operand (ops[!reduc_index],
6243 stmt);
6244 vec_oprnds0.quick_push (loop_vec_def0);
6245 if (op_type == ternary_op)
6247 op1 = reduc_index == 0 ? ops[2] : ops[1];
6248 loop_vec_def1 = vect_get_vec_def_for_operand (op1, stmt);
6249 vec_oprnds1.quick_push (loop_vec_def1);
6253 else
6255 if (!slp_node)
6257 enum vect_def_type dt;
6258 gimple *dummy_stmt;
6260 vect_is_simple_use (ops[!reduc_index], loop_vinfo,
6261 &dummy_stmt, &dt);
6262 loop_vec_def0 = vect_get_vec_def_for_stmt_copy (dt,
6263 loop_vec_def0);
6264 vec_oprnds0[0] = loop_vec_def0;
6265 if (op_type == ternary_op)
6267 vect_is_simple_use (op1, loop_vinfo, &dummy_stmt, &dt);
6268 loop_vec_def1 = vect_get_vec_def_for_stmt_copy (dt,
6269 loop_vec_def1);
6270 vec_oprnds1[0] = loop_vec_def1;
6274 if (single_defuse_cycle)
6275 reduc_def = gimple_assign_lhs (new_stmt);
6277 STMT_VINFO_RELATED_STMT (prev_phi_info) = new_phi;
6280 FOR_EACH_VEC_ELT (vec_oprnds0, i, def0)
6282 if (slp_node)
6283 reduc_def = PHI_RESULT (phis[i]);
6284 else
6286 if (!single_defuse_cycle || j == 0)
6287 reduc_def = PHI_RESULT (new_phi);
6290 def1 = ((op_type == ternary_op)
6291 ? vec_oprnds1[i] : NULL);
6292 if (op_type == binary_op)
6294 if (reduc_index == 0)
6295 expr = build2 (code, vectype_out, reduc_def, def0);
6296 else
6297 expr = build2 (code, vectype_out, def0, reduc_def);
6299 else
6301 if (reduc_index == 0)
6302 expr = build3 (code, vectype_out, reduc_def, def0, def1);
6303 else
6305 if (reduc_index == 1)
6306 expr = build3 (code, vectype_out, def0, reduc_def, def1);
6307 else
6308 expr = build3 (code, vectype_out, def0, def1, reduc_def);
6312 new_stmt = gimple_build_assign (vec_dest, expr);
6313 new_temp = make_ssa_name (vec_dest, new_stmt);
6314 gimple_assign_set_lhs (new_stmt, new_temp);
6315 vect_finish_stmt_generation (stmt, new_stmt, gsi);
6317 if (slp_node)
6319 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
6320 vect_defs.quick_push (new_temp);
6322 else
6323 vect_defs[0] = new_temp;
6326 if (slp_node)
6327 continue;
6329 if (j == 0)
6330 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
6331 else
6332 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
6334 prev_stmt_info = vinfo_for_stmt (new_stmt);
6335 prev_phi_info = vinfo_for_stmt (new_phi);
6338 /* Finalize the reduction-phi (set its arguments) and create the
6339 epilog reduction code. */
6340 if ((!single_defuse_cycle || code == COND_EXPR) && !slp_node)
6341 vect_defs[0] = gimple_assign_lhs (*vec_stmt);
6343 vect_create_epilog_for_reduction (vect_defs, stmt, epilog_copies,
6344 epilog_reduc_code, phis, reduc_index,
6345 double_reduc, slp_node);
6347 return true;
6350 /* Function vect_min_worthwhile_factor.
6352 For a loop where we could vectorize the operation indicated by CODE,
6353 return the minimum vectorization factor that makes it worthwhile
6354 to use generic vectors. */
6356 vect_min_worthwhile_factor (enum tree_code code)
6358 switch (code)
6360 case PLUS_EXPR:
6361 case MINUS_EXPR:
6362 case NEGATE_EXPR:
6363 return 4;
6365 case BIT_AND_EXPR:
6366 case BIT_IOR_EXPR:
6367 case BIT_XOR_EXPR:
6368 case BIT_NOT_EXPR:
6369 return 2;
6371 default:
6372 return INT_MAX;
6377 /* Function vectorizable_induction
6379 Check if PHI performs an induction computation that can be vectorized.
6380 If VEC_STMT is also passed, vectorize the induction PHI: create a vectorized
6381 phi to replace it, put it in VEC_STMT, and add it to the same basic block.
6382 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
6384 bool
6385 vectorizable_induction (gimple *phi,
6386 gimple_stmt_iterator *gsi ATTRIBUTE_UNUSED,
6387 gimple **vec_stmt, slp_tree slp_node)
6389 stmt_vec_info stmt_info = vinfo_for_stmt (phi);
6390 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
6391 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
6392 unsigned ncopies;
6393 bool nested_in_vect_loop = false;
6394 struct loop *iv_loop;
6395 tree vec_def;
6396 edge pe = loop_preheader_edge (loop);
6397 basic_block new_bb;
6398 tree new_vec, vec_init, vec_step, t;
6399 tree new_name;
6400 gimple *new_stmt;
6401 gphi *induction_phi;
6402 tree induc_def, vec_dest;
6403 tree init_expr, step_expr;
6404 int vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
6405 unsigned i;
6406 tree expr;
6407 gimple_seq stmts;
6408 imm_use_iterator imm_iter;
6409 use_operand_p use_p;
6410 gimple *exit_phi;
6411 edge latch_e;
6412 tree loop_arg;
6413 gimple_stmt_iterator si;
6414 basic_block bb = gimple_bb (phi);
6416 if (gimple_code (phi) != GIMPLE_PHI)
6417 return false;
6419 if (!STMT_VINFO_RELEVANT_P (stmt_info))
6420 return false;
6422 /* Make sure it was recognized as induction computation. */
6423 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_induction_def)
6424 return false;
6426 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
6427 unsigned nunits = TYPE_VECTOR_SUBPARTS (vectype);
6429 if (slp_node)
6430 ncopies = 1;
6431 else
6432 ncopies = vf / nunits;
6433 gcc_assert (ncopies >= 1);
6435 /* FORNOW. These restrictions should be relaxed. */
6436 if (nested_in_vect_loop_p (loop, phi))
6438 imm_use_iterator imm_iter;
6439 use_operand_p use_p;
6440 gimple *exit_phi;
6441 edge latch_e;
6442 tree loop_arg;
6444 if (ncopies > 1)
6446 if (dump_enabled_p ())
6447 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6448 "multiple types in nested loop.\n");
6449 return false;
6452 /* FORNOW: outer loop induction with SLP not supported. */
6453 if (STMT_SLP_TYPE (stmt_info))
6454 return false;
6456 exit_phi = NULL;
6457 latch_e = loop_latch_edge (loop->inner);
6458 loop_arg = PHI_ARG_DEF_FROM_EDGE (phi, latch_e);
6459 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, loop_arg)
6461 gimple *use_stmt = USE_STMT (use_p);
6462 if (is_gimple_debug (use_stmt))
6463 continue;
6465 if (!flow_bb_inside_loop_p (loop->inner, gimple_bb (use_stmt)))
6467 exit_phi = use_stmt;
6468 break;
6471 if (exit_phi)
6473 stmt_vec_info exit_phi_vinfo = vinfo_for_stmt (exit_phi);
6474 if (!(STMT_VINFO_RELEVANT_P (exit_phi_vinfo)
6475 && !STMT_VINFO_LIVE_P (exit_phi_vinfo)))
6477 if (dump_enabled_p ())
6478 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
6479 "inner-loop induction only used outside "
6480 "of the outer vectorized loop.\n");
6481 return false;
6485 nested_in_vect_loop = true;
6486 iv_loop = loop->inner;
6488 else
6489 iv_loop = loop;
6490 gcc_assert (iv_loop == (gimple_bb (phi))->loop_father);
6492 if (!vec_stmt) /* transformation not required. */
6494 STMT_VINFO_TYPE (stmt_info) = induc_vec_info_type;
6495 if (dump_enabled_p ())
6496 dump_printf_loc (MSG_NOTE, vect_location,
6497 "=== vectorizable_induction ===\n");
6498 vect_model_induction_cost (stmt_info, ncopies);
6499 return true;
6502 /* Transform. */
6504 /* Compute a vector variable, initialized with the first VF values of
6505 the induction variable. E.g., for an iv with IV_PHI='X' and
6506 evolution S, for a vector of 4 units, we want to compute:
6507 [X, X + S, X + 2*S, X + 3*S]. */
6509 if (dump_enabled_p ())
6510 dump_printf_loc (MSG_NOTE, vect_location, "transform induction phi.\n");
6512 latch_e = loop_latch_edge (iv_loop);
6513 loop_arg = PHI_ARG_DEF_FROM_EDGE (phi, latch_e);
6515 step_expr = STMT_VINFO_LOOP_PHI_EVOLUTION_PART (stmt_info);
6516 gcc_assert (step_expr != NULL_TREE);
6518 pe = loop_preheader_edge (iv_loop);
6519 init_expr = PHI_ARG_DEF_FROM_EDGE (phi,
6520 loop_preheader_edge (iv_loop));
6522 /* Convert the step to the desired type. */
6523 stmts = NULL;
6524 step_expr = gimple_convert (&stmts, TREE_TYPE (vectype), step_expr);
6525 if (stmts)
6527 new_bb = gsi_insert_seq_on_edge_immediate (pe, stmts);
6528 gcc_assert (!new_bb);
6531 /* Find the first insertion point in the BB. */
6532 si = gsi_after_labels (bb);
6534 /* For SLP induction we have to generate several IVs as for example
6535 with group size 3 we need [i, i, i, i + S] [i + S, i + S, i + 2*S, i + 2*S]
6536 [i + 2*S, i + 3*S, i + 3*S, i + 3*S]. The step is the same uniform
6537 [VF*S, VF*S, VF*S, VF*S] for all. */
6538 if (slp_node)
6540 /* Convert the init to the desired type. */
6541 stmts = NULL;
6542 init_expr = gimple_convert (&stmts, TREE_TYPE (vectype), init_expr);
6543 if (stmts)
6545 new_bb = gsi_insert_seq_on_edge_immediate (pe, stmts);
6546 gcc_assert (!new_bb);
6549 /* Generate [VF*S, VF*S, ... ]. */
6550 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (step_expr)))
6552 expr = build_int_cst (integer_type_node, vf);
6553 expr = fold_convert (TREE_TYPE (step_expr), expr);
6555 else
6556 expr = build_int_cst (TREE_TYPE (step_expr), vf);
6557 new_name = fold_build2 (MULT_EXPR, TREE_TYPE (step_expr),
6558 expr, step_expr);
6559 if (! CONSTANT_CLASS_P (new_name))
6560 new_name = vect_init_vector (phi, new_name,
6561 TREE_TYPE (step_expr), NULL);
6562 new_vec = build_vector_from_val (vectype, new_name);
6563 vec_step = vect_init_vector (phi, new_vec, vectype, NULL);
6565 /* Now generate the IVs. */
6566 unsigned group_size = SLP_TREE_SCALAR_STMTS (slp_node).length ();
6567 unsigned nvects = SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node);
6568 unsigned elts = nunits * nvects;
6569 unsigned nivs = least_common_multiple (group_size, nunits) / nunits;
6570 gcc_assert (elts % group_size == 0);
6571 tree elt = init_expr;
6572 unsigned ivn;
6573 for (ivn = 0; ivn < nivs; ++ivn)
6575 tree *elts = XALLOCAVEC (tree, nunits);
6576 bool constant_p = true;
6577 for (unsigned eltn = 0; eltn < nunits; ++eltn)
6579 if (ivn*nunits + eltn >= group_size
6580 && (ivn*nunits + eltn) % group_size == 0)
6582 stmts = NULL;
6583 elt = gimple_build (&stmts, PLUS_EXPR, TREE_TYPE (elt),
6584 elt, step_expr);
6585 if (stmts)
6587 new_bb = gsi_insert_seq_on_edge_immediate (pe, stmts);
6588 gcc_assert (!new_bb);
6591 if (! CONSTANT_CLASS_P (elt))
6592 constant_p = false;
6593 elts[eltn] = elt;
6595 if (constant_p)
6596 new_vec = build_vector (vectype, elts);
6597 else
6599 vec<constructor_elt, va_gc> *v;
6600 vec_alloc (v, nunits);
6601 for (i = 0; i < nunits; ++i)
6602 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, elts[i]);
6603 new_vec = build_constructor (vectype, v);
6605 vec_init = vect_init_vector (phi, new_vec, vectype, NULL);
6607 /* Create the induction-phi that defines the induction-operand. */
6608 vec_dest = vect_get_new_vect_var (vectype, vect_simple_var, "vec_iv_");
6609 induction_phi = create_phi_node (vec_dest, iv_loop->header);
6610 set_vinfo_for_stmt (induction_phi,
6611 new_stmt_vec_info (induction_phi, loop_vinfo));
6612 induc_def = PHI_RESULT (induction_phi);
6614 /* Create the iv update inside the loop */
6615 vec_def = make_ssa_name (vec_dest);
6616 new_stmt = gimple_build_assign (vec_def, PLUS_EXPR, induc_def, vec_step);
6617 gsi_insert_before (&si, new_stmt, GSI_SAME_STMT);
6618 set_vinfo_for_stmt (new_stmt, new_stmt_vec_info (new_stmt, loop_vinfo));
6620 /* Set the arguments of the phi node: */
6621 add_phi_arg (induction_phi, vec_init, pe, UNKNOWN_LOCATION);
6622 add_phi_arg (induction_phi, vec_def, loop_latch_edge (iv_loop),
6623 UNKNOWN_LOCATION);
6625 SLP_TREE_VEC_STMTS (slp_node).quick_push (induction_phi);
6628 /* Re-use IVs when we can. */
6629 if (ivn < nvects)
6631 unsigned vfp
6632 = least_common_multiple (group_size, nunits) / group_size;
6633 /* Generate [VF'*S, VF'*S, ... ]. */
6634 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (step_expr)))
6636 expr = build_int_cst (integer_type_node, vfp);
6637 expr = fold_convert (TREE_TYPE (step_expr), expr);
6639 else
6640 expr = build_int_cst (TREE_TYPE (step_expr), vfp);
6641 new_name = fold_build2 (MULT_EXPR, TREE_TYPE (step_expr),
6642 expr, step_expr);
6643 if (! CONSTANT_CLASS_P (new_name))
6644 new_name = vect_init_vector (phi, new_name,
6645 TREE_TYPE (step_expr), NULL);
6646 new_vec = build_vector_from_val (vectype, new_name);
6647 vec_step = vect_init_vector (phi, new_vec, vectype, NULL);
6648 for (; ivn < nvects; ++ivn)
6650 gimple *iv = SLP_TREE_VEC_STMTS (slp_node)[ivn - nivs];
6651 tree def;
6652 if (gimple_code (iv) == GIMPLE_PHI)
6653 def = gimple_phi_result (iv);
6654 else
6655 def = gimple_assign_lhs (iv);
6656 new_stmt = gimple_build_assign (make_ssa_name (vectype),
6657 PLUS_EXPR,
6658 def, vec_step);
6659 if (gimple_code (iv) == GIMPLE_PHI)
6660 gsi_insert_before (&si, new_stmt, GSI_SAME_STMT);
6661 else
6663 gimple_stmt_iterator tgsi = gsi_for_stmt (iv);
6664 gsi_insert_after (&tgsi, new_stmt, GSI_CONTINUE_LINKING);
6666 set_vinfo_for_stmt (new_stmt,
6667 new_stmt_vec_info (new_stmt, loop_vinfo));
6668 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
6672 return true;
6675 /* Create the vector that holds the initial_value of the induction. */
6676 if (nested_in_vect_loop)
6678 /* iv_loop is nested in the loop to be vectorized. init_expr had already
6679 been created during vectorization of previous stmts. We obtain it
6680 from the STMT_VINFO_VEC_STMT of the defining stmt. */
6681 vec_init = vect_get_vec_def_for_operand (init_expr, phi);
6682 /* If the initial value is not of proper type, convert it. */
6683 if (!useless_type_conversion_p (vectype, TREE_TYPE (vec_init)))
6685 new_stmt
6686 = gimple_build_assign (vect_get_new_ssa_name (vectype,
6687 vect_simple_var,
6688 "vec_iv_"),
6689 VIEW_CONVERT_EXPR,
6690 build1 (VIEW_CONVERT_EXPR, vectype,
6691 vec_init));
6692 vec_init = gimple_assign_lhs (new_stmt);
6693 new_bb = gsi_insert_on_edge_immediate (loop_preheader_edge (iv_loop),
6694 new_stmt);
6695 gcc_assert (!new_bb);
6696 set_vinfo_for_stmt (new_stmt,
6697 new_stmt_vec_info (new_stmt, loop_vinfo));
6700 else
6702 vec<constructor_elt, va_gc> *v;
6704 /* iv_loop is the loop to be vectorized. Create:
6705 vec_init = [X, X+S, X+2*S, X+3*S] (S = step_expr, X = init_expr) */
6706 stmts = NULL;
6707 new_name = gimple_convert (&stmts, TREE_TYPE (vectype), init_expr);
6709 vec_alloc (v, nunits);
6710 bool constant_p = is_gimple_min_invariant (new_name);
6711 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, new_name);
6712 for (i = 1; i < nunits; i++)
6714 /* Create: new_name_i = new_name + step_expr */
6715 new_name = gimple_build (&stmts, PLUS_EXPR, TREE_TYPE (new_name),
6716 new_name, step_expr);
6717 if (!is_gimple_min_invariant (new_name))
6718 constant_p = false;
6719 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, new_name);
6721 if (stmts)
6723 new_bb = gsi_insert_seq_on_edge_immediate (pe, stmts);
6724 gcc_assert (!new_bb);
6727 /* Create a vector from [new_name_0, new_name_1, ..., new_name_nunits-1] */
6728 if (constant_p)
6729 new_vec = build_vector_from_ctor (vectype, v);
6730 else
6731 new_vec = build_constructor (vectype, v);
6732 vec_init = vect_init_vector (phi, new_vec, vectype, NULL);
6736 /* Create the vector that holds the step of the induction. */
6737 if (nested_in_vect_loop)
6738 /* iv_loop is nested in the loop to be vectorized. Generate:
6739 vec_step = [S, S, S, S] */
6740 new_name = step_expr;
6741 else
6743 /* iv_loop is the loop to be vectorized. Generate:
6744 vec_step = [VF*S, VF*S, VF*S, VF*S] */
6745 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (step_expr)))
6747 expr = build_int_cst (integer_type_node, vf);
6748 expr = fold_convert (TREE_TYPE (step_expr), expr);
6750 else
6751 expr = build_int_cst (TREE_TYPE (step_expr), vf);
6752 new_name = fold_build2 (MULT_EXPR, TREE_TYPE (step_expr),
6753 expr, step_expr);
6754 if (TREE_CODE (step_expr) == SSA_NAME)
6755 new_name = vect_init_vector (phi, new_name,
6756 TREE_TYPE (step_expr), NULL);
6759 t = unshare_expr (new_name);
6760 gcc_assert (CONSTANT_CLASS_P (new_name)
6761 || TREE_CODE (new_name) == SSA_NAME);
6762 new_vec = build_vector_from_val (vectype, t);
6763 vec_step = vect_init_vector (phi, new_vec, vectype, NULL);
6766 /* Create the following def-use cycle:
6767 loop prolog:
6768 vec_init = ...
6769 vec_step = ...
6770 loop:
6771 vec_iv = PHI <vec_init, vec_loop>
6773 STMT
6775 vec_loop = vec_iv + vec_step; */
6777 /* Create the induction-phi that defines the induction-operand. */
6778 vec_dest = vect_get_new_vect_var (vectype, vect_simple_var, "vec_iv_");
6779 induction_phi = create_phi_node (vec_dest, iv_loop->header);
6780 set_vinfo_for_stmt (induction_phi,
6781 new_stmt_vec_info (induction_phi, loop_vinfo));
6782 induc_def = PHI_RESULT (induction_phi);
6784 /* Create the iv update inside the loop */
6785 vec_def = make_ssa_name (vec_dest);
6786 new_stmt = gimple_build_assign (vec_def, PLUS_EXPR, induc_def, vec_step);
6787 gsi_insert_before (&si, new_stmt, GSI_SAME_STMT);
6788 set_vinfo_for_stmt (new_stmt, new_stmt_vec_info (new_stmt, loop_vinfo));
6790 /* Set the arguments of the phi node: */
6791 add_phi_arg (induction_phi, vec_init, pe, UNKNOWN_LOCATION);
6792 add_phi_arg (induction_phi, vec_def, loop_latch_edge (iv_loop),
6793 UNKNOWN_LOCATION);
6795 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = induction_phi;
6797 /* In case that vectorization factor (VF) is bigger than the number
6798 of elements that we can fit in a vectype (nunits), we have to generate
6799 more than one vector stmt - i.e - we need to "unroll" the
6800 vector stmt by a factor VF/nunits. For more details see documentation
6801 in vectorizable_operation. */
6803 if (ncopies > 1)
6805 stmt_vec_info prev_stmt_vinfo;
6806 /* FORNOW. This restriction should be relaxed. */
6807 gcc_assert (!nested_in_vect_loop);
6809 /* Create the vector that holds the step of the induction. */
6810 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (step_expr)))
6812 expr = build_int_cst (integer_type_node, nunits);
6813 expr = fold_convert (TREE_TYPE (step_expr), expr);
6815 else
6816 expr = build_int_cst (TREE_TYPE (step_expr), nunits);
6817 new_name = fold_build2 (MULT_EXPR, TREE_TYPE (step_expr),
6818 expr, step_expr);
6819 if (TREE_CODE (step_expr) == SSA_NAME)
6820 new_name = vect_init_vector (phi, new_name,
6821 TREE_TYPE (step_expr), NULL);
6822 t = unshare_expr (new_name);
6823 gcc_assert (CONSTANT_CLASS_P (new_name)
6824 || TREE_CODE (new_name) == SSA_NAME);
6825 new_vec = build_vector_from_val (vectype, t);
6826 vec_step = vect_init_vector (phi, new_vec, vectype, NULL);
6828 vec_def = induc_def;
6829 prev_stmt_vinfo = vinfo_for_stmt (induction_phi);
6830 for (i = 1; i < ncopies; i++)
6832 /* vec_i = vec_prev + vec_step */
6833 new_stmt = gimple_build_assign (vec_dest, PLUS_EXPR,
6834 vec_def, vec_step);
6835 vec_def = make_ssa_name (vec_dest, new_stmt);
6836 gimple_assign_set_lhs (new_stmt, vec_def);
6838 gsi_insert_before (&si, new_stmt, GSI_SAME_STMT);
6839 set_vinfo_for_stmt (new_stmt,
6840 new_stmt_vec_info (new_stmt, loop_vinfo));
6841 STMT_VINFO_RELATED_STMT (prev_stmt_vinfo) = new_stmt;
6842 prev_stmt_vinfo = vinfo_for_stmt (new_stmt);
6846 if (nested_in_vect_loop)
6848 /* Find the loop-closed exit-phi of the induction, and record
6849 the final vector of induction results: */
6850 exit_phi = NULL;
6851 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, loop_arg)
6853 gimple *use_stmt = USE_STMT (use_p);
6854 if (is_gimple_debug (use_stmt))
6855 continue;
6857 if (!flow_bb_inside_loop_p (iv_loop, gimple_bb (use_stmt)))
6859 exit_phi = use_stmt;
6860 break;
6863 if (exit_phi)
6865 stmt_vec_info stmt_vinfo = vinfo_for_stmt (exit_phi);
6866 /* FORNOW. Currently not supporting the case that an inner-loop induction
6867 is not used in the outer-loop (i.e. only outside the outer-loop). */
6868 gcc_assert (STMT_VINFO_RELEVANT_P (stmt_vinfo)
6869 && !STMT_VINFO_LIVE_P (stmt_vinfo));
6871 STMT_VINFO_VEC_STMT (stmt_vinfo) = new_stmt;
6872 if (dump_enabled_p ())
6874 dump_printf_loc (MSG_NOTE, vect_location,
6875 "vector of inductions after inner-loop:");
6876 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, new_stmt, 0);
6882 if (dump_enabled_p ())
6884 dump_printf_loc (MSG_NOTE, vect_location,
6885 "transform induction: created def-use cycle: ");
6886 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, induction_phi, 0);
6887 dump_gimple_stmt (MSG_NOTE, TDF_SLIM,
6888 SSA_NAME_DEF_STMT (vec_def), 0);
6891 return true;
6894 /* Function vectorizable_live_operation.
6896 STMT computes a value that is used outside the loop. Check if
6897 it can be supported. */
6899 bool
6900 vectorizable_live_operation (gimple *stmt,
6901 gimple_stmt_iterator *gsi ATTRIBUTE_UNUSED,
6902 slp_tree slp_node, int slp_index,
6903 gimple **vec_stmt)
6905 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
6906 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
6907 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
6908 imm_use_iterator imm_iter;
6909 tree lhs, lhs_type, bitsize, vec_bitsize;
6910 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
6911 int nunits = TYPE_VECTOR_SUBPARTS (vectype);
6912 int ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits;
6913 gimple *use_stmt;
6914 auto_vec<tree> vec_oprnds;
6916 gcc_assert (STMT_VINFO_LIVE_P (stmt_info));
6918 if (STMT_VINFO_DEF_TYPE (stmt_info) == vect_reduction_def)
6919 return false;
6921 /* FORNOW. CHECKME. */
6922 if (nested_in_vect_loop_p (loop, stmt))
6923 return false;
6925 /* If STMT is not relevant and it is a simple assignment and its inputs are
6926 invariant then it can remain in place, unvectorized. The original last
6927 scalar value that it computes will be used. */
6928 if (!STMT_VINFO_RELEVANT_P (stmt_info))
6930 gcc_assert (is_simple_and_all_uses_invariant (stmt, loop_vinfo));
6931 if (dump_enabled_p ())
6932 dump_printf_loc (MSG_NOTE, vect_location,
6933 "statement is simple and uses invariant. Leaving in "
6934 "place.\n");
6935 return true;
6938 if (!vec_stmt)
6939 /* No transformation required. */
6940 return true;
6942 /* If stmt has a related stmt, then use that for getting the lhs. */
6943 if (is_pattern_stmt_p (stmt_info))
6944 stmt = STMT_VINFO_RELATED_STMT (stmt_info);
6946 lhs = (is_a <gphi *> (stmt)) ? gimple_phi_result (stmt)
6947 : gimple_get_lhs (stmt);
6948 lhs_type = TREE_TYPE (lhs);
6950 bitsize = TYPE_SIZE (TREE_TYPE (vectype));
6951 vec_bitsize = TYPE_SIZE (vectype);
6953 /* Get the vectorized lhs of STMT and the lane to use (counted in bits). */
6954 tree vec_lhs, bitstart;
6955 if (slp_node)
6957 gcc_assert (slp_index >= 0);
6959 int num_scalar = SLP_TREE_SCALAR_STMTS (slp_node).length ();
6960 int num_vec = SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node);
6962 /* Get the last occurrence of the scalar index from the concatenation of
6963 all the slp vectors. Calculate which slp vector it is and the index
6964 within. */
6965 int pos = (num_vec * nunits) - num_scalar + slp_index;
6966 int vec_entry = pos / nunits;
6967 int vec_index = pos % nunits;
6969 /* Get the correct slp vectorized stmt. */
6970 vec_lhs = gimple_get_lhs (SLP_TREE_VEC_STMTS (slp_node)[vec_entry]);
6972 /* Get entry to use. */
6973 bitstart = build_int_cst (unsigned_type_node, vec_index);
6974 bitstart = int_const_binop (MULT_EXPR, bitsize, bitstart);
6976 else
6978 enum vect_def_type dt = STMT_VINFO_DEF_TYPE (stmt_info);
6979 vec_lhs = vect_get_vec_def_for_operand_1 (stmt, dt);
6981 /* For multiple copies, get the last copy. */
6982 for (int i = 1; i < ncopies; ++i)
6983 vec_lhs = vect_get_vec_def_for_stmt_copy (vect_unknown_def_type,
6984 vec_lhs);
6986 /* Get the last lane in the vector. */
6987 bitstart = int_const_binop (MINUS_EXPR, vec_bitsize, bitsize);
6990 /* Create a new vectorized stmt for the uses of STMT and insert outside the
6991 loop. */
6992 gimple_seq stmts = NULL;
6993 tree bftype = TREE_TYPE (vectype);
6994 if (VECTOR_BOOLEAN_TYPE_P (vectype))
6995 bftype = build_nonstandard_integer_type (tree_to_uhwi (bitsize), 1);
6996 tree new_tree = build3 (BIT_FIELD_REF, bftype, vec_lhs, bitsize, bitstart);
6997 new_tree = force_gimple_operand (fold_convert (lhs_type, new_tree), &stmts,
6998 true, NULL_TREE);
6999 if (stmts)
7000 gsi_insert_seq_on_edge_immediate (single_exit (loop), stmts);
7002 /* Replace use of lhs with newly computed result. If the use stmt is a
7003 single arg PHI, just replace all uses of PHI result. It's necessary
7004 because lcssa PHI defining lhs may be before newly inserted stmt. */
7005 use_operand_p use_p;
7006 FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, lhs)
7007 if (!flow_bb_inside_loop_p (loop, gimple_bb (use_stmt))
7008 && !is_gimple_debug (use_stmt))
7010 if (gimple_code (use_stmt) == GIMPLE_PHI
7011 && gimple_phi_num_args (use_stmt) == 1)
7013 replace_uses_by (gimple_phi_result (use_stmt), new_tree);
7015 else
7017 FOR_EACH_IMM_USE_ON_STMT (use_p, imm_iter)
7018 SET_USE (use_p, new_tree);
7020 update_stmt (use_stmt);
7023 return true;
7026 /* Kill any debug uses outside LOOP of SSA names defined in STMT. */
7028 static void
7029 vect_loop_kill_debug_uses (struct loop *loop, gimple *stmt)
7031 ssa_op_iter op_iter;
7032 imm_use_iterator imm_iter;
7033 def_operand_p def_p;
7034 gimple *ustmt;
7036 FOR_EACH_PHI_OR_STMT_DEF (def_p, stmt, op_iter, SSA_OP_DEF)
7038 FOR_EACH_IMM_USE_STMT (ustmt, imm_iter, DEF_FROM_PTR (def_p))
7040 basic_block bb;
7042 if (!is_gimple_debug (ustmt))
7043 continue;
7045 bb = gimple_bb (ustmt);
7047 if (!flow_bb_inside_loop_p (loop, bb))
7049 if (gimple_debug_bind_p (ustmt))
7051 if (dump_enabled_p ())
7052 dump_printf_loc (MSG_NOTE, vect_location,
7053 "killing debug use\n");
7055 gimple_debug_bind_reset_value (ustmt);
7056 update_stmt (ustmt);
7058 else
7059 gcc_unreachable ();
7065 /* Given loop represented by LOOP_VINFO, return true if computation of
7066 LOOP_VINFO_NITERS (= LOOP_VINFO_NITERSM1 + 1) doesn't overflow, false
7067 otherwise. */
7069 static bool
7070 loop_niters_no_overflow (loop_vec_info loop_vinfo)
7072 /* Constant case. */
7073 if (LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo))
7075 tree cst_niters = LOOP_VINFO_NITERS (loop_vinfo);
7076 tree cst_nitersm1 = LOOP_VINFO_NITERSM1 (loop_vinfo);
7078 gcc_assert (TREE_CODE (cst_niters) == INTEGER_CST);
7079 gcc_assert (TREE_CODE (cst_nitersm1) == INTEGER_CST);
7080 if (wi::to_widest (cst_nitersm1) < wi::to_widest (cst_niters))
7081 return true;
7084 widest_int max;
7085 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
7086 /* Check the upper bound of loop niters. */
7087 if (get_max_loop_iterations (loop, &max))
7089 tree type = TREE_TYPE (LOOP_VINFO_NITERS (loop_vinfo));
7090 signop sgn = TYPE_SIGN (type);
7091 widest_int type_max = widest_int::from (wi::max_value (type), sgn);
7092 if (max < type_max)
7093 return true;
7095 return false;
7098 /* Scale profiling counters by estimation for LOOP which is vectorized
7099 by factor VF. */
7101 static void
7102 scale_profile_for_vect_loop (struct loop *loop, unsigned vf)
7104 edge preheader = loop_preheader_edge (loop);
7105 /* Reduce loop iterations by the vectorization factor. */
7106 gcov_type new_est_niter = niter_for_unrolled_loop (loop, vf);
7107 profile_count freq_h = loop->header->count, freq_e = preheader->count;
7109 /* Use frequency only if counts are zero. */
7110 if (!(freq_h > 0) && !(freq_e > 0))
7112 freq_h = profile_count::from_gcov_type (loop->header->frequency);
7113 freq_e = profile_count::from_gcov_type (EDGE_FREQUENCY (preheader));
7115 if (freq_h > 0)
7117 gcov_type scale;
7119 /* Avoid dropping loop body profile counter to 0 because of zero count
7120 in loop's preheader. */
7121 if (!(freq_e > profile_count::from_gcov_type (1)))
7122 freq_e = profile_count::from_gcov_type (1);
7123 /* This should not overflow. */
7124 scale = freq_e.apply_scale (new_est_niter + 1, 1).probability_in (freq_h);
7125 scale_loop_frequencies (loop, scale, REG_BR_PROB_BASE);
7128 basic_block exit_bb = single_pred (loop->latch);
7129 edge exit_e = single_exit (loop);
7130 exit_e->count = loop_preheader_edge (loop)->count;
7131 exit_e->probability = REG_BR_PROB_BASE / (new_est_niter + 1);
7133 edge exit_l = single_pred_edge (loop->latch);
7134 int prob = exit_l->probability;
7135 exit_l->probability = REG_BR_PROB_BASE - exit_e->probability;
7136 exit_l->count = exit_bb->count - exit_e->count;
7137 if (prob > 0)
7138 scale_bbs_frequencies_int (&loop->latch, 1, exit_l->probability, prob);
7141 /* Function vect_transform_loop.
7143 The analysis phase has determined that the loop is vectorizable.
7144 Vectorize the loop - created vectorized stmts to replace the scalar
7145 stmts in the loop, and update the loop exit condition.
7146 Returns scalar epilogue loop if any. */
7148 struct loop *
7149 vect_transform_loop (loop_vec_info loop_vinfo)
7151 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
7152 struct loop *epilogue = NULL;
7153 basic_block *bbs = LOOP_VINFO_BBS (loop_vinfo);
7154 int nbbs = loop->num_nodes;
7155 int i;
7156 tree niters_vector = NULL;
7157 int vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
7158 bool grouped_store;
7159 bool slp_scheduled = false;
7160 gimple *stmt, *pattern_stmt;
7161 gimple_seq pattern_def_seq = NULL;
7162 gimple_stmt_iterator pattern_def_si = gsi_none ();
7163 bool transform_pattern_stmt = false;
7164 bool check_profitability = false;
7165 int th;
7167 if (dump_enabled_p ())
7168 dump_printf_loc (MSG_NOTE, vect_location, "=== vec_transform_loop ===\n");
7170 /* Use the more conservative vectorization threshold. If the number
7171 of iterations is constant assume the cost check has been performed
7172 by our caller. If the threshold makes all loops profitable that
7173 run at least the vectorization factor number of times checking
7174 is pointless, too. */
7175 th = LOOP_VINFO_COST_MODEL_THRESHOLD (loop_vinfo);
7176 if (th >= LOOP_VINFO_VECT_FACTOR (loop_vinfo) - 1
7177 && !LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo))
7179 if (dump_enabled_p ())
7180 dump_printf_loc (MSG_NOTE, vect_location,
7181 "Profitability threshold is %d loop iterations.\n",
7182 th);
7183 check_profitability = true;
7186 /* Make sure there exists a single-predecessor exit bb. Do this before
7187 versioning. */
7188 edge e = single_exit (loop);
7189 if (! single_pred_p (e->dest))
7191 split_loop_exit_edge (e);
7192 if (dump_enabled_p ())
7193 dump_printf (MSG_NOTE, "split exit edge\n");
7196 /* Version the loop first, if required, so the profitability check
7197 comes first. */
7199 if (LOOP_REQUIRES_VERSIONING (loop_vinfo))
7201 vect_loop_versioning (loop_vinfo, th, check_profitability);
7202 check_profitability = false;
7205 /* Make sure there exists a single-predecessor exit bb also on the
7206 scalar loop copy. Do this after versioning but before peeling
7207 so CFG structure is fine for both scalar and if-converted loop
7208 to make slpeel_duplicate_current_defs_from_edges face matched
7209 loop closed PHI nodes on the exit. */
7210 if (LOOP_VINFO_SCALAR_LOOP (loop_vinfo))
7212 e = single_exit (LOOP_VINFO_SCALAR_LOOP (loop_vinfo));
7213 if (! single_pred_p (e->dest))
7215 split_loop_exit_edge (e);
7216 if (dump_enabled_p ())
7217 dump_printf (MSG_NOTE, "split exit edge of scalar loop\n");
7221 tree niters = vect_build_loop_niters (loop_vinfo);
7222 LOOP_VINFO_NITERS_UNCHANGED (loop_vinfo) = niters;
7223 tree nitersm1 = unshare_expr (LOOP_VINFO_NITERSM1 (loop_vinfo));
7224 bool niters_no_overflow = loop_niters_no_overflow (loop_vinfo);
7225 epilogue = vect_do_peeling (loop_vinfo, niters, nitersm1, &niters_vector, th,
7226 check_profitability, niters_no_overflow);
7227 if (niters_vector == NULL_TREE)
7229 if (LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo))
7230 niters_vector
7231 = build_int_cst (TREE_TYPE (LOOP_VINFO_NITERS (loop_vinfo)),
7232 LOOP_VINFO_INT_NITERS (loop_vinfo) / vf);
7233 else
7234 vect_gen_vector_loop_niters (loop_vinfo, niters, &niters_vector,
7235 niters_no_overflow);
7238 /* 1) Make sure the loop header has exactly two entries
7239 2) Make sure we have a preheader basic block. */
7241 gcc_assert (EDGE_COUNT (loop->header->preds) == 2);
7243 split_edge (loop_preheader_edge (loop));
7245 /* FORNOW: the vectorizer supports only loops which body consist
7246 of one basic block (header + empty latch). When the vectorizer will
7247 support more involved loop forms, the order by which the BBs are
7248 traversed need to be reconsidered. */
7250 for (i = 0; i < nbbs; i++)
7252 basic_block bb = bbs[i];
7253 stmt_vec_info stmt_info;
7255 for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
7256 gsi_next (&si))
7258 gphi *phi = si.phi ();
7259 if (dump_enabled_p ())
7261 dump_printf_loc (MSG_NOTE, vect_location,
7262 "------>vectorizing phi: ");
7263 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, phi, 0);
7265 stmt_info = vinfo_for_stmt (phi);
7266 if (!stmt_info)
7267 continue;
7269 if (MAY_HAVE_DEBUG_STMTS && !STMT_VINFO_LIVE_P (stmt_info))
7270 vect_loop_kill_debug_uses (loop, phi);
7272 if (!STMT_VINFO_RELEVANT_P (stmt_info)
7273 && !STMT_VINFO_LIVE_P (stmt_info))
7274 continue;
7276 if (STMT_VINFO_VECTYPE (stmt_info)
7277 && (TYPE_VECTOR_SUBPARTS (STMT_VINFO_VECTYPE (stmt_info))
7278 != (unsigned HOST_WIDE_INT) vf)
7279 && dump_enabled_p ())
7280 dump_printf_loc (MSG_NOTE, vect_location, "multiple-types.\n");
7282 if (STMT_VINFO_DEF_TYPE (stmt_info) == vect_induction_def
7283 && ! PURE_SLP_STMT (stmt_info))
7285 if (dump_enabled_p ())
7286 dump_printf_loc (MSG_NOTE, vect_location, "transform phi.\n");
7287 vect_transform_stmt (phi, NULL, NULL, NULL, NULL);
7291 pattern_stmt = NULL;
7292 for (gimple_stmt_iterator si = gsi_start_bb (bb);
7293 !gsi_end_p (si) || transform_pattern_stmt;)
7295 bool is_store;
7297 if (transform_pattern_stmt)
7298 stmt = pattern_stmt;
7299 else
7301 stmt = gsi_stmt (si);
7302 /* During vectorization remove existing clobber stmts. */
7303 if (gimple_clobber_p (stmt))
7305 unlink_stmt_vdef (stmt);
7306 gsi_remove (&si, true);
7307 release_defs (stmt);
7308 continue;
7312 if (dump_enabled_p ())
7314 dump_printf_loc (MSG_NOTE, vect_location,
7315 "------>vectorizing statement: ");
7316 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
7319 stmt_info = vinfo_for_stmt (stmt);
7321 /* vector stmts created in the outer-loop during vectorization of
7322 stmts in an inner-loop may not have a stmt_info, and do not
7323 need to be vectorized. */
7324 if (!stmt_info)
7326 gsi_next (&si);
7327 continue;
7330 if (MAY_HAVE_DEBUG_STMTS && !STMT_VINFO_LIVE_P (stmt_info))
7331 vect_loop_kill_debug_uses (loop, stmt);
7333 if (!STMT_VINFO_RELEVANT_P (stmt_info)
7334 && !STMT_VINFO_LIVE_P (stmt_info))
7336 if (STMT_VINFO_IN_PATTERN_P (stmt_info)
7337 && (pattern_stmt = STMT_VINFO_RELATED_STMT (stmt_info))
7338 && (STMT_VINFO_RELEVANT_P (vinfo_for_stmt (pattern_stmt))
7339 || STMT_VINFO_LIVE_P (vinfo_for_stmt (pattern_stmt))))
7341 stmt = pattern_stmt;
7342 stmt_info = vinfo_for_stmt (stmt);
7344 else
7346 gsi_next (&si);
7347 continue;
7350 else if (STMT_VINFO_IN_PATTERN_P (stmt_info)
7351 && (pattern_stmt = STMT_VINFO_RELATED_STMT (stmt_info))
7352 && (STMT_VINFO_RELEVANT_P (vinfo_for_stmt (pattern_stmt))
7353 || STMT_VINFO_LIVE_P (vinfo_for_stmt (pattern_stmt))))
7354 transform_pattern_stmt = true;
7356 /* If pattern statement has def stmts, vectorize them too. */
7357 if (is_pattern_stmt_p (stmt_info))
7359 if (pattern_def_seq == NULL)
7361 pattern_def_seq = STMT_VINFO_PATTERN_DEF_SEQ (stmt_info);
7362 pattern_def_si = gsi_start (pattern_def_seq);
7364 else if (!gsi_end_p (pattern_def_si))
7365 gsi_next (&pattern_def_si);
7366 if (pattern_def_seq != NULL)
7368 gimple *pattern_def_stmt = NULL;
7369 stmt_vec_info pattern_def_stmt_info = NULL;
7371 while (!gsi_end_p (pattern_def_si))
7373 pattern_def_stmt = gsi_stmt (pattern_def_si);
7374 pattern_def_stmt_info
7375 = vinfo_for_stmt (pattern_def_stmt);
7376 if (STMT_VINFO_RELEVANT_P (pattern_def_stmt_info)
7377 || STMT_VINFO_LIVE_P (pattern_def_stmt_info))
7378 break;
7379 gsi_next (&pattern_def_si);
7382 if (!gsi_end_p (pattern_def_si))
7384 if (dump_enabled_p ())
7386 dump_printf_loc (MSG_NOTE, vect_location,
7387 "==> vectorizing pattern def "
7388 "stmt: ");
7389 dump_gimple_stmt (MSG_NOTE, TDF_SLIM,
7390 pattern_def_stmt, 0);
7393 stmt = pattern_def_stmt;
7394 stmt_info = pattern_def_stmt_info;
7396 else
7398 pattern_def_si = gsi_none ();
7399 transform_pattern_stmt = false;
7402 else
7403 transform_pattern_stmt = false;
7406 if (STMT_VINFO_VECTYPE (stmt_info))
7408 unsigned int nunits
7409 = (unsigned int)
7410 TYPE_VECTOR_SUBPARTS (STMT_VINFO_VECTYPE (stmt_info));
7411 if (!STMT_SLP_TYPE (stmt_info)
7412 && nunits != (unsigned int) vf
7413 && dump_enabled_p ())
7414 /* For SLP VF is set according to unrolling factor, and not
7415 to vector size, hence for SLP this print is not valid. */
7416 dump_printf_loc (MSG_NOTE, vect_location, "multiple-types.\n");
7419 /* SLP. Schedule all the SLP instances when the first SLP stmt is
7420 reached. */
7421 if (STMT_SLP_TYPE (stmt_info))
7423 if (!slp_scheduled)
7425 slp_scheduled = true;
7427 if (dump_enabled_p ())
7428 dump_printf_loc (MSG_NOTE, vect_location,
7429 "=== scheduling SLP instances ===\n");
7431 vect_schedule_slp (loop_vinfo);
7434 /* Hybrid SLP stmts must be vectorized in addition to SLP. */
7435 if (!vinfo_for_stmt (stmt) || PURE_SLP_STMT (stmt_info))
7437 if (!transform_pattern_stmt && gsi_end_p (pattern_def_si))
7439 pattern_def_seq = NULL;
7440 gsi_next (&si);
7442 continue;
7446 /* -------- vectorize statement ------------ */
7447 if (dump_enabled_p ())
7448 dump_printf_loc (MSG_NOTE, vect_location, "transform statement.\n");
7450 grouped_store = false;
7451 is_store = vect_transform_stmt (stmt, &si, &grouped_store, NULL, NULL);
7452 if (is_store)
7454 if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
7456 /* Interleaving. If IS_STORE is TRUE, the vectorization of the
7457 interleaving chain was completed - free all the stores in
7458 the chain. */
7459 gsi_next (&si);
7460 vect_remove_stores (GROUP_FIRST_ELEMENT (stmt_info));
7462 else
7464 /* Free the attached stmt_vec_info and remove the stmt. */
7465 gimple *store = gsi_stmt (si);
7466 free_stmt_vec_info (store);
7467 unlink_stmt_vdef (store);
7468 gsi_remove (&si, true);
7469 release_defs (store);
7472 /* Stores can only appear at the end of pattern statements. */
7473 gcc_assert (!transform_pattern_stmt);
7474 pattern_def_seq = NULL;
7476 else if (!transform_pattern_stmt && gsi_end_p (pattern_def_si))
7478 pattern_def_seq = NULL;
7479 gsi_next (&si);
7481 } /* stmts in BB */
7482 } /* BBs in loop */
7484 slpeel_make_loop_iterate_ntimes (loop, niters_vector);
7486 scale_profile_for_vect_loop (loop, vf);
7488 /* The minimum number of iterations performed by the epilogue. This
7489 is 1 when peeling for gaps because we always need a final scalar
7490 iteration. */
7491 int min_epilogue_iters = LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo) ? 1 : 0;
7492 /* +1 to convert latch counts to loop iteration counts,
7493 -min_epilogue_iters to remove iterations that cannot be performed
7494 by the vector code. */
7495 int bias = 1 - min_epilogue_iters;
7496 /* In these calculations the "- 1" converts loop iteration counts
7497 back to latch counts. */
7498 if (loop->any_upper_bound)
7499 loop->nb_iterations_upper_bound
7500 = wi::udiv_floor (loop->nb_iterations_upper_bound + bias, vf) - 1;
7501 if (loop->any_likely_upper_bound)
7502 loop->nb_iterations_likely_upper_bound
7503 = wi::udiv_floor (loop->nb_iterations_likely_upper_bound + bias, vf) - 1;
7504 if (loop->any_estimate)
7505 loop->nb_iterations_estimate
7506 = wi::udiv_floor (loop->nb_iterations_estimate + bias, vf) - 1;
7508 if (dump_enabled_p ())
7510 if (!LOOP_VINFO_EPILOGUE_P (loop_vinfo))
7512 dump_printf_loc (MSG_NOTE, vect_location,
7513 "LOOP VECTORIZED\n");
7514 if (loop->inner)
7515 dump_printf_loc (MSG_NOTE, vect_location,
7516 "OUTER LOOP VECTORIZED\n");
7517 dump_printf (MSG_NOTE, "\n");
7519 else
7520 dump_printf_loc (MSG_NOTE, vect_location,
7521 "LOOP EPILOGUE VECTORIZED (VS=%d)\n",
7522 current_vector_size);
7525 /* Free SLP instances here because otherwise stmt reference counting
7526 won't work. */
7527 slp_instance instance;
7528 FOR_EACH_VEC_ELT (LOOP_VINFO_SLP_INSTANCES (loop_vinfo), i, instance)
7529 vect_free_slp_instance (instance);
7530 LOOP_VINFO_SLP_INSTANCES (loop_vinfo).release ();
7531 /* Clear-up safelen field since its value is invalid after vectorization
7532 since vectorized loop can have loop-carried dependencies. */
7533 loop->safelen = 0;
7535 /* Don't vectorize epilogue for epilogue. */
7536 if (LOOP_VINFO_EPILOGUE_P (loop_vinfo))
7537 epilogue = NULL;
7539 if (epilogue)
7541 unsigned int vector_sizes
7542 = targetm.vectorize.autovectorize_vector_sizes ();
7543 vector_sizes &= current_vector_size - 1;
7545 if (!PARAM_VALUE (PARAM_VECT_EPILOGUES_NOMASK))
7546 epilogue = NULL;
7547 else if (!vector_sizes)
7548 epilogue = NULL;
7549 else if (LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo)
7550 && LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo) >= 0)
7552 int smallest_vec_size = 1 << ctz_hwi (vector_sizes);
7553 int ratio = current_vector_size / smallest_vec_size;
7554 int eiters = LOOP_VINFO_INT_NITERS (loop_vinfo)
7555 - LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo);
7556 eiters = eiters % vf;
7558 epilogue->nb_iterations_upper_bound = eiters - 1;
7560 if (eiters < vf / ratio)
7561 epilogue = NULL;
7565 if (epilogue)
7567 epilogue->force_vectorize = loop->force_vectorize;
7568 epilogue->safelen = loop->safelen;
7569 epilogue->dont_vectorize = false;
7571 /* We may need to if-convert epilogue to vectorize it. */
7572 if (LOOP_VINFO_SCALAR_LOOP (loop_vinfo))
7573 tree_if_conversion (epilogue);
7576 return epilogue;
7579 /* The code below is trying to perform simple optimization - revert
7580 if-conversion for masked stores, i.e. if the mask of a store is zero
7581 do not perform it and all stored value producers also if possible.
7582 For example,
7583 for (i=0; i<n; i++)
7584 if (c[i])
7586 p1[i] += 1;
7587 p2[i] = p3[i] +2;
7589 this transformation will produce the following semi-hammock:
7591 if (!mask__ifc__42.18_165 == { 0, 0, 0, 0, 0, 0, 0, 0 })
7593 vect__11.19_170 = MASK_LOAD (vectp_p1.20_168, 0B, mask__ifc__42.18_165);
7594 vect__12.22_172 = vect__11.19_170 + vect_cst__171;
7595 MASK_STORE (vectp_p1.23_175, 0B, mask__ifc__42.18_165, vect__12.22_172);
7596 vect__18.25_182 = MASK_LOAD (vectp_p3.26_180, 0B, mask__ifc__42.18_165);
7597 vect__19.28_184 = vect__18.25_182 + vect_cst__183;
7598 MASK_STORE (vectp_p2.29_187, 0B, mask__ifc__42.18_165, vect__19.28_184);
7602 void
7603 optimize_mask_stores (struct loop *loop)
7605 basic_block *bbs = get_loop_body (loop);
7606 unsigned nbbs = loop->num_nodes;
7607 unsigned i;
7608 basic_block bb;
7609 struct loop *bb_loop;
7610 gimple_stmt_iterator gsi;
7611 gimple *stmt;
7612 auto_vec<gimple *> worklist;
7614 vect_location = find_loop_location (loop);
7615 /* Pick up all masked stores in loop if any. */
7616 for (i = 0; i < nbbs; i++)
7618 bb = bbs[i];
7619 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
7620 gsi_next (&gsi))
7622 stmt = gsi_stmt (gsi);
7623 if (gimple_call_internal_p (stmt, IFN_MASK_STORE))
7624 worklist.safe_push (stmt);
7628 free (bbs);
7629 if (worklist.is_empty ())
7630 return;
7632 /* Loop has masked stores. */
7633 while (!worklist.is_empty ())
7635 gimple *last, *last_store;
7636 edge e, efalse;
7637 tree mask;
7638 basic_block store_bb, join_bb;
7639 gimple_stmt_iterator gsi_to;
7640 tree vdef, new_vdef;
7641 gphi *phi;
7642 tree vectype;
7643 tree zero;
7645 last = worklist.pop ();
7646 mask = gimple_call_arg (last, 2);
7647 bb = gimple_bb (last);
7648 /* Create then_bb and if-then structure in CFG, then_bb belongs to
7649 the same loop as if_bb. It could be different to LOOP when two
7650 level loop-nest is vectorized and mask_store belongs to the inner
7651 one. */
7652 e = split_block (bb, last);
7653 bb_loop = bb->loop_father;
7654 gcc_assert (loop == bb_loop || flow_loop_nested_p (loop, bb_loop));
7655 join_bb = e->dest;
7656 store_bb = create_empty_bb (bb);
7657 add_bb_to_loop (store_bb, bb_loop);
7658 e->flags = EDGE_TRUE_VALUE;
7659 efalse = make_edge (bb, store_bb, EDGE_FALSE_VALUE);
7660 /* Put STORE_BB to likely part. */
7661 efalse->probability = PROB_UNLIKELY;
7662 store_bb->frequency = PROB_ALWAYS - EDGE_FREQUENCY (efalse);
7663 make_edge (store_bb, join_bb, EDGE_FALLTHRU);
7664 if (dom_info_available_p (CDI_DOMINATORS))
7665 set_immediate_dominator (CDI_DOMINATORS, store_bb, bb);
7666 if (dump_enabled_p ())
7667 dump_printf_loc (MSG_NOTE, vect_location,
7668 "Create new block %d to sink mask stores.",
7669 store_bb->index);
7670 /* Create vector comparison with boolean result. */
7671 vectype = TREE_TYPE (mask);
7672 zero = build_zero_cst (vectype);
7673 stmt = gimple_build_cond (EQ_EXPR, mask, zero, NULL_TREE, NULL_TREE);
7674 gsi = gsi_last_bb (bb);
7675 gsi_insert_after (&gsi, stmt, GSI_SAME_STMT);
7676 /* Create new PHI node for vdef of the last masked store:
7677 .MEM_2 = VDEF <.MEM_1>
7678 will be converted to
7679 .MEM.3 = VDEF <.MEM_1>
7680 and new PHI node will be created in join bb
7681 .MEM_2 = PHI <.MEM_1, .MEM_3>
7683 vdef = gimple_vdef (last);
7684 new_vdef = make_ssa_name (gimple_vop (cfun), last);
7685 gimple_set_vdef (last, new_vdef);
7686 phi = create_phi_node (vdef, join_bb);
7687 add_phi_arg (phi, new_vdef, EDGE_SUCC (store_bb, 0), UNKNOWN_LOCATION);
7689 /* Put all masked stores with the same mask to STORE_BB if possible. */
7690 while (true)
7692 gimple_stmt_iterator gsi_from;
7693 gimple *stmt1 = NULL;
7695 /* Move masked store to STORE_BB. */
7696 last_store = last;
7697 gsi = gsi_for_stmt (last);
7698 gsi_from = gsi;
7699 /* Shift GSI to the previous stmt for further traversal. */
7700 gsi_prev (&gsi);
7701 gsi_to = gsi_start_bb (store_bb);
7702 gsi_move_before (&gsi_from, &gsi_to);
7703 /* Setup GSI_TO to the non-empty block start. */
7704 gsi_to = gsi_start_bb (store_bb);
7705 if (dump_enabled_p ())
7707 dump_printf_loc (MSG_NOTE, vect_location,
7708 "Move stmt to created bb\n");
7709 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, last, 0);
7711 /* Move all stored value producers if possible. */
7712 while (!gsi_end_p (gsi))
7714 tree lhs;
7715 imm_use_iterator imm_iter;
7716 use_operand_p use_p;
7717 bool res;
7719 /* Skip debug statements. */
7720 if (is_gimple_debug (gsi_stmt (gsi)))
7722 gsi_prev (&gsi);
7723 continue;
7725 stmt1 = gsi_stmt (gsi);
7726 /* Do not consider statements writing to memory or having
7727 volatile operand. */
7728 if (gimple_vdef (stmt1)
7729 || gimple_has_volatile_ops (stmt1))
7730 break;
7731 gsi_from = gsi;
7732 gsi_prev (&gsi);
7733 lhs = gimple_get_lhs (stmt1);
7734 if (!lhs)
7735 break;
7737 /* LHS of vectorized stmt must be SSA_NAME. */
7738 if (TREE_CODE (lhs) != SSA_NAME)
7739 break;
7741 if (!VECTOR_TYPE_P (TREE_TYPE (lhs)))
7743 /* Remove dead scalar statement. */
7744 if (has_zero_uses (lhs))
7746 gsi_remove (&gsi_from, true);
7747 continue;
7751 /* Check that LHS does not have uses outside of STORE_BB. */
7752 res = true;
7753 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, lhs)
7755 gimple *use_stmt;
7756 use_stmt = USE_STMT (use_p);
7757 if (is_gimple_debug (use_stmt))
7758 continue;
7759 if (gimple_bb (use_stmt) != store_bb)
7761 res = false;
7762 break;
7765 if (!res)
7766 break;
7768 if (gimple_vuse (stmt1)
7769 && gimple_vuse (stmt1) != gimple_vuse (last_store))
7770 break;
7772 /* Can move STMT1 to STORE_BB. */
7773 if (dump_enabled_p ())
7775 dump_printf_loc (MSG_NOTE, vect_location,
7776 "Move stmt to created bb\n");
7777 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt1, 0);
7779 gsi_move_before (&gsi_from, &gsi_to);
7780 /* Shift GSI_TO for further insertion. */
7781 gsi_prev (&gsi_to);
7783 /* Put other masked stores with the same mask to STORE_BB. */
7784 if (worklist.is_empty ()
7785 || gimple_call_arg (worklist.last (), 2) != mask
7786 || worklist.last () != stmt1)
7787 break;
7788 last = worklist.pop ();
7790 add_phi_arg (phi, gimple_vuse (last_store), e, UNKNOWN_LOCATION);