configure.ac (multiarch): Use $enableval instead of $withval.
[official-gcc.git] / gcc / tree-vect-loop.c
blob04c3760598d9a337ed014bf692ec9a25cab581ad
1 /* Loop Vectorization
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
3 Free Software Foundation, Inc.
4 Contributed by Dorit Naishlos <dorit@il.ibm.com> and
5 Ira Rosen <irar@il.ibm.com>
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "dumpfile.h"
27 #include "tm.h"
28 #include "ggc.h"
29 #include "tree.h"
30 #include "basic-block.h"
31 #include "gimple-pretty-print.h"
32 #include "tree-flow.h"
33 #include "tree-pass.h"
34 #include "cfgloop.h"
35 #include "expr.h"
36 #include "recog.h"
37 #include "optabs.h"
38 #include "params.h"
39 #include "diagnostic-core.h"
40 #include "tree-chrec.h"
41 #include "tree-scalar-evolution.h"
42 #include "tree-vectorizer.h"
43 #include "target.h"
45 /* Loop Vectorization Pass.
47 This pass tries to vectorize loops.
49 For example, the vectorizer transforms the following simple loop:
51 short a[N]; short b[N]; short c[N]; int i;
53 for (i=0; i<N; i++){
54 a[i] = b[i] + c[i];
57 as if it was manually vectorized by rewriting the source code into:
59 typedef int __attribute__((mode(V8HI))) v8hi;
60 short a[N]; short b[N]; short c[N]; int i;
61 v8hi *pa = (v8hi*)a, *pb = (v8hi*)b, *pc = (v8hi*)c;
62 v8hi va, vb, vc;
64 for (i=0; i<N/8; i++){
65 vb = pb[i];
66 vc = pc[i];
67 va = vb + vc;
68 pa[i] = va;
71 The main entry to this pass is vectorize_loops(), in which
72 the vectorizer applies a set of analyses on a given set of loops,
73 followed by the actual vectorization transformation for the loops that
74 had successfully passed the analysis phase.
75 Throughout this pass we make a distinction between two types of
76 data: scalars (which are represented by SSA_NAMES), and memory references
77 ("data-refs"). These two types of data require different handling both
78 during analysis and transformation. The types of data-refs that the
79 vectorizer currently supports are ARRAY_REFS which base is an array DECL
80 (not a pointer), and INDIRECT_REFS through pointers; both array and pointer
81 accesses are required to have a simple (consecutive) access pattern.
83 Analysis phase:
84 ===============
85 The driver for the analysis phase is vect_analyze_loop().
86 It applies a set of analyses, some of which rely on the scalar evolution
87 analyzer (scev) developed by Sebastian Pop.
89 During the analysis phase the vectorizer records some information
90 per stmt in a "stmt_vec_info" struct which is attached to each stmt in the
91 loop, as well as general information about the loop as a whole, which is
92 recorded in a "loop_vec_info" struct attached to each loop.
94 Transformation phase:
95 =====================
96 The loop transformation phase scans all the stmts in the loop, and
97 creates a vector stmt (or a sequence of stmts) for each scalar stmt S in
98 the loop that needs to be vectorized. It inserts the vector code sequence
99 just before the scalar stmt S, and records a pointer to the vector code
100 in STMT_VINFO_VEC_STMT (stmt_info) (stmt_info is the stmt_vec_info struct
101 attached to S). This pointer will be used for the vectorization of following
102 stmts which use the def of stmt S. Stmt S is removed if it writes to memory;
103 otherwise, we rely on dead code elimination for removing it.
105 For example, say stmt S1 was vectorized into stmt VS1:
107 VS1: vb = px[i];
108 S1: b = x[i]; STMT_VINFO_VEC_STMT (stmt_info (S1)) = VS1
109 S2: a = b;
111 To vectorize stmt S2, the vectorizer first finds the stmt that defines
112 the operand 'b' (S1), and gets the relevant vector def 'vb' from the
113 vector stmt VS1 pointed to by STMT_VINFO_VEC_STMT (stmt_info (S1)). The
114 resulting sequence would be:
116 VS1: vb = px[i];
117 S1: b = x[i]; STMT_VINFO_VEC_STMT (stmt_info (S1)) = VS1
118 VS2: va = vb;
119 S2: a = b; STMT_VINFO_VEC_STMT (stmt_info (S2)) = VS2
121 Operands that are not SSA_NAMEs, are data-refs that appear in
122 load/store operations (like 'x[i]' in S1), and are handled differently.
124 Target modeling:
125 =================
126 Currently the only target specific information that is used is the
127 size of the vector (in bytes) - "TARGET_VECTORIZE_UNITS_PER_SIMD_WORD".
128 Targets that can support different sizes of vectors, for now will need
129 to specify one value for "TARGET_VECTORIZE_UNITS_PER_SIMD_WORD". More
130 flexibility will be added in the future.
132 Since we only vectorize operations which vector form can be
133 expressed using existing tree codes, to verify that an operation is
134 supported, the vectorizer checks the relevant optab at the relevant
135 machine_mode (e.g, optab_handler (add_optab, V8HImode)). If
136 the value found is CODE_FOR_nothing, then there's no target support, and
137 we can't vectorize the stmt.
139 For additional information on this project see:
140 http://gcc.gnu.org/projects/tree-ssa/vectorization.html
143 static void vect_estimate_min_profitable_iters (loop_vec_info, int *, int *);
145 /* Function vect_determine_vectorization_factor
147 Determine the vectorization factor (VF). VF is the number of data elements
148 that are operated upon in parallel in a single iteration of the vectorized
149 loop. For example, when vectorizing a loop that operates on 4byte elements,
150 on a target with vector size (VS) 16byte, the VF is set to 4, since 4
151 elements can fit in a single vector register.
153 We currently support vectorization of loops in which all types operated upon
154 are of the same size. Therefore this function currently sets VF according to
155 the size of the types operated upon, and fails if there are multiple sizes
156 in the loop.
158 VF is also the factor by which the loop iterations are strip-mined, e.g.:
159 original loop:
160 for (i=0; i<N; i++){
161 a[i] = b[i] + c[i];
164 vectorized loop:
165 for (i=0; i<N; i+=VF){
166 a[i:VF] = b[i:VF] + c[i:VF];
170 static bool
171 vect_determine_vectorization_factor (loop_vec_info loop_vinfo)
173 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
174 basic_block *bbs = LOOP_VINFO_BBS (loop_vinfo);
175 int nbbs = loop->num_nodes;
176 gimple_stmt_iterator si;
177 unsigned int vectorization_factor = 0;
178 tree scalar_type;
179 gimple phi;
180 tree vectype;
181 unsigned int nunits;
182 stmt_vec_info stmt_info;
183 int i;
184 HOST_WIDE_INT dummy;
185 gimple stmt, pattern_stmt = NULL;
186 gimple_seq pattern_def_seq = NULL;
187 gimple_stmt_iterator pattern_def_si = gsi_none ();
188 bool analyze_pattern_stmt = false;
190 if (dump_enabled_p ())
191 dump_printf_loc (MSG_NOTE, vect_location,
192 "=== vect_determine_vectorization_factor ===");
194 for (i = 0; i < nbbs; i++)
196 basic_block bb = bbs[i];
198 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
200 phi = gsi_stmt (si);
201 stmt_info = vinfo_for_stmt (phi);
202 if (dump_enabled_p ())
204 dump_printf_loc (MSG_NOTE, vect_location, "==> examining phi: ");
205 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, phi, 0);
208 gcc_assert (stmt_info);
210 if (STMT_VINFO_RELEVANT_P (stmt_info))
212 gcc_assert (!STMT_VINFO_VECTYPE (stmt_info));
213 scalar_type = TREE_TYPE (PHI_RESULT (phi));
215 if (dump_enabled_p ())
217 dump_printf_loc (MSG_NOTE, vect_location,
218 "get vectype for scalar type: ");
219 dump_generic_expr (MSG_NOTE, TDF_SLIM, scalar_type);
222 vectype = get_vectype_for_scalar_type (scalar_type);
223 if (!vectype)
225 if (dump_enabled_p ())
227 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
228 "not vectorized: unsupported "
229 "data-type ");
230 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
231 scalar_type);
233 return false;
235 STMT_VINFO_VECTYPE (stmt_info) = vectype;
237 if (dump_enabled_p ())
239 dump_printf_loc (MSG_NOTE, vect_location, "vectype: ");
240 dump_generic_expr (MSG_NOTE, TDF_SLIM, vectype);
243 nunits = TYPE_VECTOR_SUBPARTS (vectype);
244 if (dump_enabled_p ())
245 dump_printf_loc (MSG_NOTE, vect_location, "nunits = %d", nunits);
247 if (!vectorization_factor
248 || (nunits > vectorization_factor))
249 vectorization_factor = nunits;
253 for (si = gsi_start_bb (bb); !gsi_end_p (si) || analyze_pattern_stmt;)
255 tree vf_vectype;
257 if (analyze_pattern_stmt)
258 stmt = pattern_stmt;
259 else
260 stmt = gsi_stmt (si);
262 stmt_info = vinfo_for_stmt (stmt);
264 if (dump_enabled_p ())
266 dump_printf_loc (MSG_NOTE, vect_location,
267 "==> examining statement: ");
268 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
271 gcc_assert (stmt_info);
273 /* Skip stmts which do not need to be vectorized. */
274 if (!STMT_VINFO_RELEVANT_P (stmt_info)
275 && !STMT_VINFO_LIVE_P (stmt_info))
277 if (STMT_VINFO_IN_PATTERN_P (stmt_info)
278 && (pattern_stmt = STMT_VINFO_RELATED_STMT (stmt_info))
279 && (STMT_VINFO_RELEVANT_P (vinfo_for_stmt (pattern_stmt))
280 || STMT_VINFO_LIVE_P (vinfo_for_stmt (pattern_stmt))))
282 stmt = pattern_stmt;
283 stmt_info = vinfo_for_stmt (pattern_stmt);
284 if (dump_enabled_p ())
286 dump_printf_loc (MSG_NOTE, vect_location,
287 "==> examining pattern statement: ");
288 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
291 else
293 if (dump_enabled_p ())
294 dump_printf_loc (MSG_NOTE, vect_location, "skip.");
295 gsi_next (&si);
296 continue;
299 else if (STMT_VINFO_IN_PATTERN_P (stmt_info)
300 && (pattern_stmt = STMT_VINFO_RELATED_STMT (stmt_info))
301 && (STMT_VINFO_RELEVANT_P (vinfo_for_stmt (pattern_stmt))
302 || STMT_VINFO_LIVE_P (vinfo_for_stmt (pattern_stmt))))
303 analyze_pattern_stmt = true;
305 /* If a pattern statement has def stmts, analyze them too. */
306 if (is_pattern_stmt_p (stmt_info))
308 if (pattern_def_seq == NULL)
310 pattern_def_seq = STMT_VINFO_PATTERN_DEF_SEQ (stmt_info);
311 pattern_def_si = gsi_start (pattern_def_seq);
313 else if (!gsi_end_p (pattern_def_si))
314 gsi_next (&pattern_def_si);
315 if (pattern_def_seq != NULL)
317 gimple pattern_def_stmt = NULL;
318 stmt_vec_info pattern_def_stmt_info = NULL;
320 while (!gsi_end_p (pattern_def_si))
322 pattern_def_stmt = gsi_stmt (pattern_def_si);
323 pattern_def_stmt_info
324 = vinfo_for_stmt (pattern_def_stmt);
325 if (STMT_VINFO_RELEVANT_P (pattern_def_stmt_info)
326 || STMT_VINFO_LIVE_P (pattern_def_stmt_info))
327 break;
328 gsi_next (&pattern_def_si);
331 if (!gsi_end_p (pattern_def_si))
333 if (dump_enabled_p ())
335 dump_printf_loc (MSG_NOTE, vect_location,
336 "==> examining pattern def stmt: ");
337 dump_gimple_stmt (MSG_NOTE, TDF_SLIM,
338 pattern_def_stmt, 0);
341 stmt = pattern_def_stmt;
342 stmt_info = pattern_def_stmt_info;
344 else
346 pattern_def_si = gsi_none ();
347 analyze_pattern_stmt = false;
350 else
351 analyze_pattern_stmt = false;
354 if (gimple_get_lhs (stmt) == NULL_TREE)
356 if (dump_enabled_p ())
358 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
359 "not vectorized: irregular stmt.");
360 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt,
363 return false;
366 if (VECTOR_MODE_P (TYPE_MODE (gimple_expr_type (stmt))))
368 if (dump_enabled_p ())
370 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
371 "not vectorized: vector stmt in loop:");
372 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
374 return false;
377 if (STMT_VINFO_VECTYPE (stmt_info))
379 /* The only case when a vectype had been already set is for stmts
380 that contain a dataref, or for "pattern-stmts" (stmts
381 generated by the vectorizer to represent/replace a certain
382 idiom). */
383 gcc_assert (STMT_VINFO_DATA_REF (stmt_info)
384 || is_pattern_stmt_p (stmt_info)
385 || !gsi_end_p (pattern_def_si));
386 vectype = STMT_VINFO_VECTYPE (stmt_info);
388 else
390 gcc_assert (!STMT_VINFO_DATA_REF (stmt_info));
391 scalar_type = TREE_TYPE (gimple_get_lhs (stmt));
392 if (dump_enabled_p ())
394 dump_printf_loc (MSG_NOTE, vect_location,
395 "get vectype for scalar type: ");
396 dump_generic_expr (MSG_NOTE, TDF_SLIM, scalar_type);
398 vectype = get_vectype_for_scalar_type (scalar_type);
399 if (!vectype)
401 if (dump_enabled_p ())
403 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
404 "not vectorized: unsupported "
405 "data-type ");
406 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
407 scalar_type);
409 return false;
412 STMT_VINFO_VECTYPE (stmt_info) = vectype;
415 /* The vectorization factor is according to the smallest
416 scalar type (or the largest vector size, but we only
417 support one vector size per loop). */
418 scalar_type = vect_get_smallest_scalar_type (stmt, &dummy,
419 &dummy);
420 if (dump_enabled_p ())
422 dump_printf_loc (MSG_NOTE, vect_location,
423 "get vectype for scalar type: ");
424 dump_generic_expr (MSG_NOTE, TDF_SLIM, scalar_type);
426 vf_vectype = get_vectype_for_scalar_type (scalar_type);
427 if (!vf_vectype)
429 if (dump_enabled_p ())
431 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
432 "not vectorized: unsupported data-type ");
433 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
434 scalar_type);
436 return false;
439 if ((GET_MODE_SIZE (TYPE_MODE (vectype))
440 != GET_MODE_SIZE (TYPE_MODE (vf_vectype))))
442 if (dump_enabled_p ())
444 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
445 "not vectorized: different sized vector "
446 "types in statement, ");
447 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
448 vectype);
449 dump_printf (MSG_MISSED_OPTIMIZATION, " and ");
450 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
451 vf_vectype);
453 return false;
456 if (dump_enabled_p ())
458 dump_printf_loc (MSG_NOTE, vect_location, "vectype: ");
459 dump_generic_expr (MSG_NOTE, TDF_SLIM, vf_vectype);
462 nunits = TYPE_VECTOR_SUBPARTS (vf_vectype);
463 if (dump_enabled_p ())
464 dump_printf_loc (MSG_NOTE, vect_location, "nunits = %d", nunits);
465 if (!vectorization_factor
466 || (nunits > vectorization_factor))
467 vectorization_factor = nunits;
469 if (!analyze_pattern_stmt && gsi_end_p (pattern_def_si))
471 pattern_def_seq = NULL;
472 gsi_next (&si);
477 /* TODO: Analyze cost. Decide if worth while to vectorize. */
478 if (dump_enabled_p ())
479 dump_printf_loc (MSG_NOTE, vect_location, "vectorization factor = %d",
480 vectorization_factor);
481 if (vectorization_factor <= 1)
483 if (dump_enabled_p ())
484 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
485 "not vectorized: unsupported data-type");
486 return false;
488 LOOP_VINFO_VECT_FACTOR (loop_vinfo) = vectorization_factor;
490 return true;
494 /* Function vect_is_simple_iv_evolution.
496 FORNOW: A simple evolution of an induction variables in the loop is
497 considered a polynomial evolution with constant step. */
499 static bool
500 vect_is_simple_iv_evolution (unsigned loop_nb, tree access_fn, tree * init,
501 tree * step)
503 tree init_expr;
504 tree step_expr;
505 tree evolution_part = evolution_part_in_loop_num (access_fn, loop_nb);
507 /* When there is no evolution in this loop, the evolution function
508 is not "simple". */
509 if (evolution_part == NULL_TREE)
510 return false;
512 /* When the evolution is a polynomial of degree >= 2
513 the evolution function is not "simple". */
514 if (tree_is_chrec (evolution_part))
515 return false;
517 step_expr = evolution_part;
518 init_expr = unshare_expr (initial_condition_in_loop_num (access_fn, loop_nb));
520 if (dump_enabled_p ())
522 dump_printf_loc (MSG_NOTE, vect_location, "step: ");
523 dump_generic_expr (MSG_NOTE, TDF_SLIM, step_expr);
524 dump_printf (MSG_NOTE, ", init: ");
525 dump_generic_expr (MSG_NOTE, TDF_SLIM, init_expr);
528 *init = init_expr;
529 *step = step_expr;
531 if (TREE_CODE (step_expr) != INTEGER_CST)
533 if (dump_enabled_p ())
534 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
535 "step unknown.");
536 return false;
539 return true;
542 /* Function vect_analyze_scalar_cycles_1.
544 Examine the cross iteration def-use cycles of scalar variables
545 in LOOP. LOOP_VINFO represents the loop that is now being
546 considered for vectorization (can be LOOP, or an outer-loop
547 enclosing LOOP). */
549 static void
550 vect_analyze_scalar_cycles_1 (loop_vec_info loop_vinfo, struct loop *loop)
552 basic_block bb = loop->header;
553 tree dumy;
554 vec<gimple> worklist;
555 worklist.create (64);
556 gimple_stmt_iterator gsi;
557 bool double_reduc;
559 if (dump_enabled_p ())
560 dump_printf_loc (MSG_NOTE, vect_location,
561 "=== vect_analyze_scalar_cycles ===");
563 /* First - identify all inductions. Reduction detection assumes that all the
564 inductions have been identified, therefore, this order must not be
565 changed. */
566 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
568 gimple phi = gsi_stmt (gsi);
569 tree access_fn = NULL;
570 tree def = PHI_RESULT (phi);
571 stmt_vec_info stmt_vinfo = vinfo_for_stmt (phi);
573 if (dump_enabled_p ())
575 dump_printf_loc (MSG_NOTE, vect_location, "Analyze phi: ");
576 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, phi, 0);
579 /* Skip virtual phi's. The data dependences that are associated with
580 virtual defs/uses (i.e., memory accesses) are analyzed elsewhere. */
581 if (virtual_operand_p (def))
582 continue;
584 STMT_VINFO_DEF_TYPE (stmt_vinfo) = vect_unknown_def_type;
586 /* Analyze the evolution function. */
587 access_fn = analyze_scalar_evolution (loop, def);
588 if (access_fn)
590 STRIP_NOPS (access_fn);
591 if (dump_enabled_p ())
593 dump_printf_loc (MSG_NOTE, vect_location,
594 "Access function of PHI: ");
595 dump_generic_expr (MSG_NOTE, TDF_SLIM, access_fn);
597 STMT_VINFO_LOOP_PHI_EVOLUTION_PART (stmt_vinfo)
598 = evolution_part_in_loop_num (access_fn, loop->num);
601 if (!access_fn
602 || !vect_is_simple_iv_evolution (loop->num, access_fn, &dumy, &dumy))
604 worklist.safe_push (phi);
605 continue;
608 gcc_assert (STMT_VINFO_LOOP_PHI_EVOLUTION_PART (stmt_vinfo) != NULL_TREE);
610 if (dump_enabled_p ())
611 dump_printf_loc (MSG_NOTE, vect_location, "Detected induction.");
612 STMT_VINFO_DEF_TYPE (stmt_vinfo) = vect_induction_def;
616 /* Second - identify all reductions and nested cycles. */
617 while (worklist.length () > 0)
619 gimple phi = worklist.pop ();
620 tree def = PHI_RESULT (phi);
621 stmt_vec_info stmt_vinfo = vinfo_for_stmt (phi);
622 gimple reduc_stmt;
623 bool nested_cycle;
625 if (dump_enabled_p ())
627 dump_printf_loc (MSG_NOTE, vect_location, "Analyze phi: ");
628 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, phi, 0);
631 gcc_assert (!virtual_operand_p (def)
632 && STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_unknown_def_type);
634 nested_cycle = (loop != LOOP_VINFO_LOOP (loop_vinfo));
635 reduc_stmt = vect_force_simple_reduction (loop_vinfo, phi, !nested_cycle,
636 &double_reduc);
637 if (reduc_stmt)
639 if (double_reduc)
641 if (dump_enabled_p ())
642 dump_printf_loc (MSG_NOTE, vect_location,
643 "Detected double reduction.");
645 STMT_VINFO_DEF_TYPE (stmt_vinfo) = vect_double_reduction_def;
646 STMT_VINFO_DEF_TYPE (vinfo_for_stmt (reduc_stmt)) =
647 vect_double_reduction_def;
649 else
651 if (nested_cycle)
653 if (dump_enabled_p ())
654 dump_printf_loc (MSG_NOTE, vect_location,
655 "Detected vectorizable nested cycle.");
657 STMT_VINFO_DEF_TYPE (stmt_vinfo) = vect_nested_cycle;
658 STMT_VINFO_DEF_TYPE (vinfo_for_stmt (reduc_stmt)) =
659 vect_nested_cycle;
661 else
663 if (dump_enabled_p ())
664 dump_printf_loc (MSG_NOTE, vect_location,
665 "Detected reduction.");
667 STMT_VINFO_DEF_TYPE (stmt_vinfo) = vect_reduction_def;
668 STMT_VINFO_DEF_TYPE (vinfo_for_stmt (reduc_stmt)) =
669 vect_reduction_def;
670 /* Store the reduction cycles for possible vectorization in
671 loop-aware SLP. */
672 LOOP_VINFO_REDUCTIONS (loop_vinfo).safe_push (reduc_stmt);
676 else
677 if (dump_enabled_p ())
678 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
679 "Unknown def-use cycle pattern.");
682 worklist.release ();
686 /* Function vect_analyze_scalar_cycles.
688 Examine the cross iteration def-use cycles of scalar variables, by
689 analyzing the loop-header PHIs of scalar variables. Classify each
690 cycle as one of the following: invariant, induction, reduction, unknown.
691 We do that for the loop represented by LOOP_VINFO, and also to its
692 inner-loop, if exists.
693 Examples for scalar cycles:
695 Example1: reduction:
697 loop1:
698 for (i=0; i<N; i++)
699 sum += a[i];
701 Example2: induction:
703 loop2:
704 for (i=0; i<N; i++)
705 a[i] = i; */
707 static void
708 vect_analyze_scalar_cycles (loop_vec_info loop_vinfo)
710 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
712 vect_analyze_scalar_cycles_1 (loop_vinfo, loop);
714 /* When vectorizing an outer-loop, the inner-loop is executed sequentially.
715 Reductions in such inner-loop therefore have different properties than
716 the reductions in the nest that gets vectorized:
717 1. When vectorized, they are executed in the same order as in the original
718 scalar loop, so we can't change the order of computation when
719 vectorizing them.
720 2. FIXME: Inner-loop reductions can be used in the inner-loop, so the
721 current checks are too strict. */
723 if (loop->inner)
724 vect_analyze_scalar_cycles_1 (loop_vinfo, loop->inner);
727 /* Function vect_get_loop_niters.
729 Determine how many iterations the loop is executed.
730 If an expression that represents the number of iterations
731 can be constructed, place it in NUMBER_OF_ITERATIONS.
732 Return the loop exit condition. */
734 static gimple
735 vect_get_loop_niters (struct loop *loop, tree *number_of_iterations)
737 tree niters;
739 if (dump_enabled_p ())
740 dump_printf_loc (MSG_NOTE, vect_location,
741 "=== get_loop_niters ===");
742 niters = number_of_exit_cond_executions (loop);
744 if (niters != NULL_TREE
745 && niters != chrec_dont_know)
747 *number_of_iterations = niters;
749 if (dump_enabled_p ())
751 dump_printf_loc (MSG_NOTE, vect_location, "==> get_loop_niters:");
752 dump_generic_expr (MSG_NOTE, TDF_SLIM, *number_of_iterations);
756 return get_loop_exit_condition (loop);
760 /* Function bb_in_loop_p
762 Used as predicate for dfs order traversal of the loop bbs. */
764 static bool
765 bb_in_loop_p (const_basic_block bb, const void *data)
767 const struct loop *const loop = (const struct loop *)data;
768 if (flow_bb_inside_loop_p (loop, bb))
769 return true;
770 return false;
774 /* Function new_loop_vec_info.
776 Create and initialize a new loop_vec_info struct for LOOP, as well as
777 stmt_vec_info structs for all the stmts in LOOP. */
779 static loop_vec_info
780 new_loop_vec_info (struct loop *loop)
782 loop_vec_info res;
783 basic_block *bbs;
784 gimple_stmt_iterator si;
785 unsigned int i, nbbs;
787 res = (loop_vec_info) xcalloc (1, sizeof (struct _loop_vec_info));
788 LOOP_VINFO_LOOP (res) = loop;
790 bbs = get_loop_body (loop);
792 /* Create/Update stmt_info for all stmts in the loop. */
793 for (i = 0; i < loop->num_nodes; i++)
795 basic_block bb = bbs[i];
797 /* BBs in a nested inner-loop will have been already processed (because
798 we will have called vect_analyze_loop_form for any nested inner-loop).
799 Therefore, for stmts in an inner-loop we just want to update the
800 STMT_VINFO_LOOP_VINFO field of their stmt_info to point to the new
801 loop_info of the outer-loop we are currently considering to vectorize
802 (instead of the loop_info of the inner-loop).
803 For stmts in other BBs we need to create a stmt_info from scratch. */
804 if (bb->loop_father != loop)
806 /* Inner-loop bb. */
807 gcc_assert (loop->inner && bb->loop_father == loop->inner);
808 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
810 gimple phi = gsi_stmt (si);
811 stmt_vec_info stmt_info = vinfo_for_stmt (phi);
812 loop_vec_info inner_loop_vinfo =
813 STMT_VINFO_LOOP_VINFO (stmt_info);
814 gcc_assert (loop->inner == LOOP_VINFO_LOOP (inner_loop_vinfo));
815 STMT_VINFO_LOOP_VINFO (stmt_info) = res;
817 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
819 gimple stmt = gsi_stmt (si);
820 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
821 loop_vec_info inner_loop_vinfo =
822 STMT_VINFO_LOOP_VINFO (stmt_info);
823 gcc_assert (loop->inner == LOOP_VINFO_LOOP (inner_loop_vinfo));
824 STMT_VINFO_LOOP_VINFO (stmt_info) = res;
827 else
829 /* bb in current nest. */
830 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
832 gimple phi = gsi_stmt (si);
833 gimple_set_uid (phi, 0);
834 set_vinfo_for_stmt (phi, new_stmt_vec_info (phi, res, NULL));
837 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
839 gimple stmt = gsi_stmt (si);
840 gimple_set_uid (stmt, 0);
841 set_vinfo_for_stmt (stmt, new_stmt_vec_info (stmt, res, NULL));
846 /* CHECKME: We want to visit all BBs before their successors (except for
847 latch blocks, for which this assertion wouldn't hold). In the simple
848 case of the loop forms we allow, a dfs order of the BBs would the same
849 as reversed postorder traversal, so we are safe. */
851 free (bbs);
852 bbs = XCNEWVEC (basic_block, loop->num_nodes);
853 nbbs = dfs_enumerate_from (loop->header, 0, bb_in_loop_p,
854 bbs, loop->num_nodes, loop);
855 gcc_assert (nbbs == loop->num_nodes);
857 LOOP_VINFO_BBS (res) = bbs;
858 LOOP_VINFO_NITERS (res) = NULL;
859 LOOP_VINFO_NITERS_UNCHANGED (res) = NULL;
860 LOOP_VINFO_COST_MODEL_MIN_ITERS (res) = 0;
861 LOOP_VINFO_VECTORIZABLE_P (res) = 0;
862 LOOP_PEELING_FOR_ALIGNMENT (res) = 0;
863 LOOP_VINFO_VECT_FACTOR (res) = 0;
864 LOOP_VINFO_LOOP_NEST (res).create (3);
865 LOOP_VINFO_DATAREFS (res).create (10);
866 LOOP_VINFO_DDRS (res).create (10 * 10);
867 LOOP_VINFO_UNALIGNED_DR (res) = NULL;
868 LOOP_VINFO_MAY_MISALIGN_STMTS (res).create (
869 PARAM_VALUE (PARAM_VECT_MAX_VERSION_FOR_ALIGNMENT_CHECKS));
870 LOOP_VINFO_MAY_ALIAS_DDRS (res).create (
871 PARAM_VALUE (PARAM_VECT_MAX_VERSION_FOR_ALIAS_CHECKS));
872 LOOP_VINFO_GROUPED_STORES (res).create (10);
873 LOOP_VINFO_REDUCTIONS (res).create (10);
874 LOOP_VINFO_REDUCTION_CHAINS (res).create (10);
875 LOOP_VINFO_SLP_INSTANCES (res).create (10);
876 LOOP_VINFO_SLP_UNROLLING_FACTOR (res) = 1;
877 LOOP_VINFO_PEELING_HTAB (res) = NULL;
878 LOOP_VINFO_TARGET_COST_DATA (res) = init_cost (loop);
879 LOOP_VINFO_PEELING_FOR_GAPS (res) = false;
880 LOOP_VINFO_OPERANDS_SWAPPED (res) = false;
882 return res;
886 /* Function destroy_loop_vec_info.
888 Free LOOP_VINFO struct, as well as all the stmt_vec_info structs of all the
889 stmts in the loop. */
891 void
892 destroy_loop_vec_info (loop_vec_info loop_vinfo, bool clean_stmts)
894 struct loop *loop;
895 basic_block *bbs;
896 int nbbs;
897 gimple_stmt_iterator si;
898 int j;
899 vec<slp_instance> slp_instances;
900 slp_instance instance;
901 bool swapped;
903 if (!loop_vinfo)
904 return;
906 loop = LOOP_VINFO_LOOP (loop_vinfo);
908 bbs = LOOP_VINFO_BBS (loop_vinfo);
909 nbbs = loop->num_nodes;
910 swapped = LOOP_VINFO_OPERANDS_SWAPPED (loop_vinfo);
912 if (!clean_stmts)
914 free (LOOP_VINFO_BBS (loop_vinfo));
915 free_data_refs (LOOP_VINFO_DATAREFS (loop_vinfo));
916 free_dependence_relations (LOOP_VINFO_DDRS (loop_vinfo));
917 LOOP_VINFO_LOOP_NEST (loop_vinfo).release ();
918 LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo).release ();
919 LOOP_VINFO_MAY_ALIAS_DDRS (loop_vinfo).release ();
921 free (loop_vinfo);
922 loop->aux = NULL;
923 return;
926 for (j = 0; j < nbbs; j++)
928 basic_block bb = bbs[j];
929 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
930 free_stmt_vec_info (gsi_stmt (si));
932 for (si = gsi_start_bb (bb); !gsi_end_p (si); )
934 gimple stmt = gsi_stmt (si);
936 /* We may have broken canonical form by moving a constant
937 into RHS1 of a commutative op. Fix such occurrences. */
938 if (swapped && is_gimple_assign (stmt))
940 enum tree_code code = gimple_assign_rhs_code (stmt);
942 if ((code == PLUS_EXPR
943 || code == POINTER_PLUS_EXPR
944 || code == MULT_EXPR)
945 && CONSTANT_CLASS_P (gimple_assign_rhs1 (stmt)))
946 swap_tree_operands (stmt,
947 gimple_assign_rhs1_ptr (stmt),
948 gimple_assign_rhs2_ptr (stmt));
951 /* Free stmt_vec_info. */
952 free_stmt_vec_info (stmt);
953 gsi_next (&si);
957 free (LOOP_VINFO_BBS (loop_vinfo));
958 free_data_refs (LOOP_VINFO_DATAREFS (loop_vinfo));
959 free_dependence_relations (LOOP_VINFO_DDRS (loop_vinfo));
960 LOOP_VINFO_LOOP_NEST (loop_vinfo).release ();
961 LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo).release ();
962 LOOP_VINFO_MAY_ALIAS_DDRS (loop_vinfo).release ();
963 slp_instances = LOOP_VINFO_SLP_INSTANCES (loop_vinfo);
964 FOR_EACH_VEC_ELT (slp_instances, j, instance)
965 vect_free_slp_instance (instance);
967 LOOP_VINFO_SLP_INSTANCES (loop_vinfo).release ();
968 LOOP_VINFO_GROUPED_STORES (loop_vinfo).release ();
969 LOOP_VINFO_REDUCTIONS (loop_vinfo).release ();
970 LOOP_VINFO_REDUCTION_CHAINS (loop_vinfo).release ();
972 if (LOOP_VINFO_PEELING_HTAB (loop_vinfo))
973 htab_delete (LOOP_VINFO_PEELING_HTAB (loop_vinfo));
975 destroy_cost_data (LOOP_VINFO_TARGET_COST_DATA (loop_vinfo));
977 free (loop_vinfo);
978 loop->aux = NULL;
982 /* Function vect_analyze_loop_1.
984 Apply a set of analyses on LOOP, and create a loop_vec_info struct
985 for it. The different analyses will record information in the
986 loop_vec_info struct. This is a subset of the analyses applied in
987 vect_analyze_loop, to be applied on an inner-loop nested in the loop
988 that is now considered for (outer-loop) vectorization. */
990 static loop_vec_info
991 vect_analyze_loop_1 (struct loop *loop)
993 loop_vec_info loop_vinfo;
995 if (dump_enabled_p ())
996 dump_printf_loc (MSG_NOTE, vect_location,
997 "===== analyze_loop_nest_1 =====");
999 /* Check the CFG characteristics of the loop (nesting, entry/exit, etc. */
1001 loop_vinfo = vect_analyze_loop_form (loop);
1002 if (!loop_vinfo)
1004 if (dump_enabled_p ())
1005 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1006 "bad inner-loop form.");
1007 return NULL;
1010 return loop_vinfo;
1014 /* Function vect_analyze_loop_form.
1016 Verify that certain CFG restrictions hold, including:
1017 - the loop has a pre-header
1018 - the loop has a single entry and exit
1019 - the loop exit condition is simple enough, and the number of iterations
1020 can be analyzed (a countable loop). */
1022 loop_vec_info
1023 vect_analyze_loop_form (struct loop *loop)
1025 loop_vec_info loop_vinfo;
1026 gimple loop_cond;
1027 tree number_of_iterations = NULL;
1028 loop_vec_info inner_loop_vinfo = NULL;
1030 if (dump_enabled_p ())
1031 dump_printf_loc (MSG_NOTE, vect_location,
1032 "=== vect_analyze_loop_form ===");
1034 /* Different restrictions apply when we are considering an inner-most loop,
1035 vs. an outer (nested) loop.
1036 (FORNOW. May want to relax some of these restrictions in the future). */
1038 if (!loop->inner)
1040 /* Inner-most loop. We currently require that the number of BBs is
1041 exactly 2 (the header and latch). Vectorizable inner-most loops
1042 look like this:
1044 (pre-header)
1046 header <--------+
1047 | | |
1048 | +--> latch --+
1050 (exit-bb) */
1052 if (loop->num_nodes != 2)
1054 if (dump_enabled_p ())
1055 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1056 "not vectorized: control flow in loop.");
1057 return NULL;
1060 if (empty_block_p (loop->header))
1062 if (dump_enabled_p ())
1063 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1064 "not vectorized: empty loop.");
1065 return NULL;
1068 else
1070 struct loop *innerloop = loop->inner;
1071 edge entryedge;
1073 /* Nested loop. We currently require that the loop is doubly-nested,
1074 contains a single inner loop, and the number of BBs is exactly 5.
1075 Vectorizable outer-loops look like this:
1077 (pre-header)
1079 header <---+
1081 inner-loop |
1083 tail ------+
1085 (exit-bb)
1087 The inner-loop has the properties expected of inner-most loops
1088 as described above. */
1090 if ((loop->inner)->inner || (loop->inner)->next)
1092 if (dump_enabled_p ())
1093 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1094 "not vectorized: multiple nested loops.");
1095 return NULL;
1098 /* Analyze the inner-loop. */
1099 inner_loop_vinfo = vect_analyze_loop_1 (loop->inner);
1100 if (!inner_loop_vinfo)
1102 if (dump_enabled_p ())
1103 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1104 "not vectorized: Bad inner loop.");
1105 return NULL;
1108 if (!expr_invariant_in_loop_p (loop,
1109 LOOP_VINFO_NITERS (inner_loop_vinfo)))
1111 if (dump_enabled_p ())
1112 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1113 "not vectorized: inner-loop count not invariant.");
1114 destroy_loop_vec_info (inner_loop_vinfo, true);
1115 return NULL;
1118 if (loop->num_nodes != 5)
1120 if (dump_enabled_p ())
1121 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1122 "not vectorized: control flow in loop.");
1123 destroy_loop_vec_info (inner_loop_vinfo, true);
1124 return NULL;
1127 gcc_assert (EDGE_COUNT (innerloop->header->preds) == 2);
1128 entryedge = EDGE_PRED (innerloop->header, 0);
1129 if (EDGE_PRED (innerloop->header, 0)->src == innerloop->latch)
1130 entryedge = EDGE_PRED (innerloop->header, 1);
1132 if (entryedge->src != loop->header
1133 || !single_exit (innerloop)
1134 || single_exit (innerloop)->dest != EDGE_PRED (loop->latch, 0)->src)
1136 if (dump_enabled_p ())
1137 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1138 "not vectorized: unsupported outerloop form.");
1139 destroy_loop_vec_info (inner_loop_vinfo, true);
1140 return NULL;
1143 if (dump_enabled_p ())
1144 dump_printf_loc (MSG_NOTE, vect_location,
1145 "Considering outer-loop vectorization.");
1148 if (!single_exit (loop)
1149 || EDGE_COUNT (loop->header->preds) != 2)
1151 if (dump_enabled_p ())
1153 if (!single_exit (loop))
1154 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1155 "not vectorized: multiple exits.");
1156 else if (EDGE_COUNT (loop->header->preds) != 2)
1157 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1158 "not vectorized: too many incoming edges.");
1160 if (inner_loop_vinfo)
1161 destroy_loop_vec_info (inner_loop_vinfo, true);
1162 return NULL;
1165 /* We assume that the loop exit condition is at the end of the loop. i.e,
1166 that the loop is represented as a do-while (with a proper if-guard
1167 before the loop if needed), where the loop header contains all the
1168 executable statements, and the latch is empty. */
1169 if (!empty_block_p (loop->latch)
1170 || !gimple_seq_empty_p (phi_nodes (loop->latch)))
1172 if (dump_enabled_p ())
1173 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1174 "not vectorized: unexpected loop form.");
1175 if (inner_loop_vinfo)
1176 destroy_loop_vec_info (inner_loop_vinfo, true);
1177 return NULL;
1180 /* Make sure there exists a single-predecessor exit bb: */
1181 if (!single_pred_p (single_exit (loop)->dest))
1183 edge e = single_exit (loop);
1184 if (!(e->flags & EDGE_ABNORMAL))
1186 split_loop_exit_edge (e);
1187 if (dump_enabled_p ())
1188 dump_printf (MSG_NOTE, "split exit edge.");
1190 else
1192 if (dump_enabled_p ())
1193 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1194 "not vectorized: abnormal loop exit edge.");
1195 if (inner_loop_vinfo)
1196 destroy_loop_vec_info (inner_loop_vinfo, true);
1197 return NULL;
1201 loop_cond = vect_get_loop_niters (loop, &number_of_iterations);
1202 if (!loop_cond)
1204 if (dump_enabled_p ())
1205 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1206 "not vectorized: complicated exit condition.");
1207 if (inner_loop_vinfo)
1208 destroy_loop_vec_info (inner_loop_vinfo, true);
1209 return NULL;
1212 if (!number_of_iterations)
1214 if (dump_enabled_p ())
1215 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1216 "not vectorized: number of iterations cannot be "
1217 "computed.");
1218 if (inner_loop_vinfo)
1219 destroy_loop_vec_info (inner_loop_vinfo, true);
1220 return NULL;
1223 if (chrec_contains_undetermined (number_of_iterations))
1225 if (dump_enabled_p ())
1226 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1227 "Infinite number of iterations.");
1228 if (inner_loop_vinfo)
1229 destroy_loop_vec_info (inner_loop_vinfo, true);
1230 return NULL;
1233 if (!NITERS_KNOWN_P (number_of_iterations))
1235 if (dump_enabled_p ())
1237 dump_printf_loc (MSG_NOTE, vect_location,
1238 "Symbolic number of iterations is ");
1239 dump_generic_expr (MSG_NOTE, TDF_DETAILS, number_of_iterations);
1242 else if (TREE_INT_CST_LOW (number_of_iterations) == 0)
1244 if (dump_enabled_p ())
1245 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1246 "not vectorized: number of iterations = 0.");
1247 if (inner_loop_vinfo)
1248 destroy_loop_vec_info (inner_loop_vinfo, false);
1249 return NULL;
1252 loop_vinfo = new_loop_vec_info (loop);
1253 LOOP_VINFO_NITERS (loop_vinfo) = number_of_iterations;
1254 LOOP_VINFO_NITERS_UNCHANGED (loop_vinfo) = number_of_iterations;
1256 STMT_VINFO_TYPE (vinfo_for_stmt (loop_cond)) = loop_exit_ctrl_vec_info_type;
1258 /* CHECKME: May want to keep it around it in the future. */
1259 if (inner_loop_vinfo)
1260 destroy_loop_vec_info (inner_loop_vinfo, false);
1262 gcc_assert (!loop->aux);
1263 loop->aux = loop_vinfo;
1264 return loop_vinfo;
1268 /* Function vect_analyze_loop_operations.
1270 Scan the loop stmts and make sure they are all vectorizable. */
1272 static bool
1273 vect_analyze_loop_operations (loop_vec_info loop_vinfo, bool slp)
1275 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
1276 basic_block *bbs = LOOP_VINFO_BBS (loop_vinfo);
1277 int nbbs = loop->num_nodes;
1278 gimple_stmt_iterator si;
1279 unsigned int vectorization_factor = 0;
1280 int i;
1281 gimple phi;
1282 stmt_vec_info stmt_info;
1283 bool need_to_vectorize = false;
1284 int min_profitable_iters;
1285 int min_scalar_loop_bound;
1286 unsigned int th;
1287 bool only_slp_in_loop = true, ok;
1288 HOST_WIDE_INT max_niter;
1289 HOST_WIDE_INT estimated_niter;
1290 int min_profitable_estimate;
1292 if (dump_enabled_p ())
1293 dump_printf_loc (MSG_NOTE, vect_location,
1294 "=== vect_analyze_loop_operations ===");
1296 gcc_assert (LOOP_VINFO_VECT_FACTOR (loop_vinfo));
1297 vectorization_factor = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
1298 if (slp)
1300 /* If all the stmts in the loop can be SLPed, we perform only SLP, and
1301 vectorization factor of the loop is the unrolling factor required by
1302 the SLP instances. If that unrolling factor is 1, we say, that we
1303 perform pure SLP on loop - cross iteration parallelism is not
1304 exploited. */
1305 for (i = 0; i < nbbs; i++)
1307 basic_block bb = bbs[i];
1308 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
1310 gimple stmt = gsi_stmt (si);
1311 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1312 gcc_assert (stmt_info);
1313 if ((STMT_VINFO_RELEVANT_P (stmt_info)
1314 || VECTORIZABLE_CYCLE_DEF (STMT_VINFO_DEF_TYPE (stmt_info)))
1315 && !PURE_SLP_STMT (stmt_info))
1316 /* STMT needs both SLP and loop-based vectorization. */
1317 only_slp_in_loop = false;
1321 if (only_slp_in_loop)
1322 vectorization_factor = LOOP_VINFO_SLP_UNROLLING_FACTOR (loop_vinfo);
1323 else
1324 vectorization_factor = least_common_multiple (vectorization_factor,
1325 LOOP_VINFO_SLP_UNROLLING_FACTOR (loop_vinfo));
1327 LOOP_VINFO_VECT_FACTOR (loop_vinfo) = vectorization_factor;
1328 if (dump_enabled_p ())
1329 dump_printf_loc (MSG_NOTE, vect_location,
1330 "Updating vectorization factor to %d ",
1331 vectorization_factor);
1334 for (i = 0; i < nbbs; i++)
1336 basic_block bb = bbs[i];
1338 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
1340 phi = gsi_stmt (si);
1341 ok = true;
1343 stmt_info = vinfo_for_stmt (phi);
1344 if (dump_enabled_p ())
1346 dump_printf_loc (MSG_NOTE, vect_location, "examining phi: ");
1347 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, phi, 0);
1350 /* Inner-loop loop-closed exit phi in outer-loop vectorization
1351 (i.e., a phi in the tail of the outer-loop). */
1352 if (! is_loop_header_bb_p (bb))
1354 /* FORNOW: we currently don't support the case that these phis
1355 are not used in the outerloop (unless it is double reduction,
1356 i.e., this phi is vect_reduction_def), cause this case
1357 requires to actually do something here. */
1358 if ((!STMT_VINFO_RELEVANT_P (stmt_info)
1359 || STMT_VINFO_LIVE_P (stmt_info))
1360 && STMT_VINFO_DEF_TYPE (stmt_info)
1361 != vect_double_reduction_def)
1363 if (dump_enabled_p ())
1364 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1365 "Unsupported loop-closed phi in "
1366 "outer-loop.");
1367 return false;
1370 /* If PHI is used in the outer loop, we check that its operand
1371 is defined in the inner loop. */
1372 if (STMT_VINFO_RELEVANT_P (stmt_info))
1374 tree phi_op;
1375 gimple op_def_stmt;
1377 if (gimple_phi_num_args (phi) != 1)
1378 return false;
1380 phi_op = PHI_ARG_DEF (phi, 0);
1381 if (TREE_CODE (phi_op) != SSA_NAME)
1382 return false;
1384 op_def_stmt = SSA_NAME_DEF_STMT (phi_op);
1385 if (!op_def_stmt
1386 || !flow_bb_inside_loop_p (loop, gimple_bb (op_def_stmt))
1387 || !vinfo_for_stmt (op_def_stmt))
1388 return false;
1390 if (STMT_VINFO_RELEVANT (vinfo_for_stmt (op_def_stmt))
1391 != vect_used_in_outer
1392 && STMT_VINFO_RELEVANT (vinfo_for_stmt (op_def_stmt))
1393 != vect_used_in_outer_by_reduction)
1394 return false;
1397 continue;
1400 gcc_assert (stmt_info);
1402 if (STMT_VINFO_LIVE_P (stmt_info))
1404 /* FORNOW: not yet supported. */
1405 if (dump_enabled_p ())
1406 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1407 "not vectorized: value used after loop.");
1408 return false;
1411 if (STMT_VINFO_RELEVANT (stmt_info) == vect_used_in_scope
1412 && STMT_VINFO_DEF_TYPE (stmt_info) != vect_induction_def)
1414 /* A scalar-dependence cycle that we don't support. */
1415 if (dump_enabled_p ())
1416 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1417 "not vectorized: scalar dependence cycle.");
1418 return false;
1421 if (STMT_VINFO_RELEVANT_P (stmt_info))
1423 need_to_vectorize = true;
1424 if (STMT_VINFO_DEF_TYPE (stmt_info) == vect_induction_def)
1425 ok = vectorizable_induction (phi, NULL, NULL);
1428 if (!ok)
1430 if (dump_enabled_p ())
1432 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1433 "not vectorized: relevant phi not "
1434 "supported: ");
1435 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, phi, 0);
1437 return false;
1441 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
1443 gimple stmt = gsi_stmt (si);
1444 if (!vect_analyze_stmt (stmt, &need_to_vectorize, NULL))
1445 return false;
1447 } /* bbs */
1449 /* All operations in the loop are either irrelevant (deal with loop
1450 control, or dead), or only used outside the loop and can be moved
1451 out of the loop (e.g. invariants, inductions). The loop can be
1452 optimized away by scalar optimizations. We're better off not
1453 touching this loop. */
1454 if (!need_to_vectorize)
1456 if (dump_enabled_p ())
1457 dump_printf_loc (MSG_NOTE, vect_location,
1458 "All the computation can be taken out of the loop.");
1459 if (dump_enabled_p ())
1460 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1461 "not vectorized: redundant loop. no profit to "
1462 "vectorize.");
1463 return false;
1466 if (LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo) && dump_enabled_p ())
1467 dump_printf_loc (MSG_NOTE, vect_location,
1468 "vectorization_factor = %d, niters = "
1469 HOST_WIDE_INT_PRINT_DEC, vectorization_factor,
1470 LOOP_VINFO_INT_NITERS (loop_vinfo));
1472 if ((LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo)
1473 && (LOOP_VINFO_INT_NITERS (loop_vinfo) < vectorization_factor))
1474 || ((max_niter = max_stmt_executions_int (loop)) != -1
1475 && (unsigned HOST_WIDE_INT) max_niter < vectorization_factor))
1477 if (dump_enabled_p ())
1478 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1479 "not vectorized: iteration count too small.");
1480 if (dump_enabled_p ())
1481 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1482 "not vectorized: iteration count smaller than "
1483 "vectorization factor.");
1484 return false;
1487 /* Analyze cost. Decide if worth while to vectorize. */
1489 /* Once VF is set, SLP costs should be updated since the number of created
1490 vector stmts depends on VF. */
1491 vect_update_slp_costs_according_to_vf (loop_vinfo);
1493 vect_estimate_min_profitable_iters (loop_vinfo, &min_profitable_iters,
1494 &min_profitable_estimate);
1495 LOOP_VINFO_COST_MODEL_MIN_ITERS (loop_vinfo) = min_profitable_iters;
1497 if (min_profitable_iters < 0)
1499 if (dump_enabled_p ())
1500 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1501 "not vectorized: vectorization not profitable.");
1502 if (dump_enabled_p ())
1503 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1504 "not vectorized: vector version will never be "
1505 "profitable.");
1506 return false;
1509 min_scalar_loop_bound = ((PARAM_VALUE (PARAM_MIN_VECT_LOOP_BOUND)
1510 * vectorization_factor) - 1);
1513 /* Use the cost model only if it is more conservative than user specified
1514 threshold. */
1516 th = (unsigned) min_scalar_loop_bound;
1517 if (min_profitable_iters
1518 && (!min_scalar_loop_bound
1519 || min_profitable_iters > min_scalar_loop_bound))
1520 th = (unsigned) min_profitable_iters;
1522 if (LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo)
1523 && LOOP_VINFO_INT_NITERS (loop_vinfo) <= th)
1525 if (dump_enabled_p ())
1526 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1527 "not vectorized: vectorization not profitable.");
1528 if (dump_enabled_p ())
1529 dump_printf_loc (MSG_NOTE, vect_location,
1530 "not vectorized: iteration count smaller than user "
1531 "specified loop bound parameter or minimum profitable "
1532 "iterations (whichever is more conservative).");
1533 return false;
1536 if ((estimated_niter = estimated_stmt_executions_int (loop)) != -1
1537 && ((unsigned HOST_WIDE_INT) estimated_niter
1538 <= MAX (th, (unsigned)min_profitable_estimate)))
1540 if (dump_enabled_p ())
1541 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1542 "not vectorized: estimated iteration count too "
1543 "small.");
1544 if (dump_enabled_p ())
1545 dump_printf_loc (MSG_NOTE, vect_location,
1546 "not vectorized: estimated iteration count smaller "
1547 "than specified loop bound parameter or minimum "
1548 "profitable iterations (whichever is more "
1549 "conservative).");
1550 return false;
1553 if (!LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo)
1554 || LOOP_VINFO_INT_NITERS (loop_vinfo) % vectorization_factor != 0
1555 || LOOP_PEELING_FOR_ALIGNMENT (loop_vinfo))
1557 if (dump_enabled_p ())
1558 dump_printf_loc (MSG_NOTE, vect_location, "epilog loop required.");
1559 if (!vect_can_advance_ivs_p (loop_vinfo))
1561 if (dump_enabled_p ())
1562 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1563 "not vectorized: can't create epilog loop 1.");
1564 return false;
1566 if (!slpeel_can_duplicate_loop_p (loop, single_exit (loop)))
1568 if (dump_enabled_p ())
1569 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1570 "not vectorized: can't create epilog loop 2.");
1571 return false;
1575 return true;
1579 /* Function vect_analyze_loop_2.
1581 Apply a set of analyses on LOOP, and create a loop_vec_info struct
1582 for it. The different analyses will record information in the
1583 loop_vec_info struct. */
1584 static bool
1585 vect_analyze_loop_2 (loop_vec_info loop_vinfo)
1587 bool ok, slp = false;
1588 int max_vf = MAX_VECTORIZATION_FACTOR;
1589 int min_vf = 2;
1591 /* Find all data references in the loop (which correspond to vdefs/vuses)
1592 and analyze their evolution in the loop. Also adjust the minimal
1593 vectorization factor according to the loads and stores.
1595 FORNOW: Handle only simple, array references, which
1596 alignment can be forced, and aligned pointer-references. */
1598 ok = vect_analyze_data_refs (loop_vinfo, NULL, &min_vf);
1599 if (!ok)
1601 if (dump_enabled_p ())
1602 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1603 "bad data references.");
1604 return false;
1607 /* Classify all cross-iteration scalar data-flow cycles.
1608 Cross-iteration cycles caused by virtual phis are analyzed separately. */
1610 vect_analyze_scalar_cycles (loop_vinfo);
1612 vect_pattern_recog (loop_vinfo, NULL);
1614 /* Data-flow analysis to detect stmts that do not need to be vectorized. */
1616 ok = vect_mark_stmts_to_be_vectorized (loop_vinfo);
1617 if (!ok)
1619 if (dump_enabled_p ())
1620 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1621 "unexpected pattern.");
1622 return false;
1625 /* Analyze data dependences between the data-refs in the loop
1626 and adjust the maximum vectorization factor according to
1627 the dependences.
1628 FORNOW: fail at the first data dependence that we encounter. */
1630 ok = vect_analyze_data_ref_dependences (loop_vinfo, NULL, &max_vf);
1631 if (!ok
1632 || max_vf < min_vf)
1634 if (dump_enabled_p ())
1635 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1636 "bad data dependence.");
1637 return false;
1640 ok = vect_determine_vectorization_factor (loop_vinfo);
1641 if (!ok)
1643 if (dump_enabled_p ())
1644 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1645 "can't determine vectorization factor.");
1646 return false;
1648 if (max_vf < LOOP_VINFO_VECT_FACTOR (loop_vinfo))
1650 if (dump_enabled_p ())
1651 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1652 "bad data dependence.");
1653 return false;
1656 /* Analyze the alignment of the data-refs in the loop.
1657 Fail if a data reference is found that cannot be vectorized. */
1659 ok = vect_analyze_data_refs_alignment (loop_vinfo, NULL);
1660 if (!ok)
1662 if (dump_enabled_p ())
1663 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1664 "bad data alignment.");
1665 return false;
1668 /* Analyze the access patterns of the data-refs in the loop (consecutive,
1669 complex, etc.). FORNOW: Only handle consecutive access pattern. */
1671 ok = vect_analyze_data_ref_accesses (loop_vinfo, NULL);
1672 if (!ok)
1674 if (dump_enabled_p ())
1675 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1676 "bad data access.");
1677 return false;
1680 /* Prune the list of ddrs to be tested at run-time by versioning for alias.
1681 It is important to call pruning after vect_analyze_data_ref_accesses,
1682 since we use grouping information gathered by interleaving analysis. */
1683 ok = vect_prune_runtime_alias_test_list (loop_vinfo);
1684 if (!ok)
1686 if (dump_enabled_p ())
1687 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1688 "too long list of versioning for alias "
1689 "run-time tests.");
1690 return false;
1693 /* This pass will decide on using loop versioning and/or loop peeling in
1694 order to enhance the alignment of data references in the loop. */
1696 ok = vect_enhance_data_refs_alignment (loop_vinfo);
1697 if (!ok)
1699 if (dump_enabled_p ())
1700 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1701 "bad data alignment.");
1702 return false;
1705 /* Check the SLP opportunities in the loop, analyze and build SLP trees. */
1706 ok = vect_analyze_slp (loop_vinfo, NULL);
1707 if (ok)
1709 /* Decide which possible SLP instances to SLP. */
1710 slp = vect_make_slp_decision (loop_vinfo);
1712 /* Find stmts that need to be both vectorized and SLPed. */
1713 vect_detect_hybrid_slp (loop_vinfo);
1715 else
1716 return false;
1718 /* Scan all the operations in the loop and make sure they are
1719 vectorizable. */
1721 ok = vect_analyze_loop_operations (loop_vinfo, slp);
1722 if (!ok)
1724 if (dump_enabled_p ())
1725 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1726 "bad operation or unsupported loop bound.");
1727 return false;
1730 return true;
1733 /* Function vect_analyze_loop.
1735 Apply a set of analyses on LOOP, and create a loop_vec_info struct
1736 for it. The different analyses will record information in the
1737 loop_vec_info struct. */
1738 loop_vec_info
1739 vect_analyze_loop (struct loop *loop)
1741 loop_vec_info loop_vinfo;
1742 unsigned int vector_sizes;
1744 /* Autodetect first vector size we try. */
1745 current_vector_size = 0;
1746 vector_sizes = targetm.vectorize.autovectorize_vector_sizes ();
1748 if (dump_enabled_p ())
1749 dump_printf_loc (MSG_NOTE, vect_location,
1750 "===== analyze_loop_nest =====");
1752 if (loop_outer (loop)
1753 && loop_vec_info_for_loop (loop_outer (loop))
1754 && LOOP_VINFO_VECTORIZABLE_P (loop_vec_info_for_loop (loop_outer (loop))))
1756 if (dump_enabled_p ())
1757 dump_printf_loc (MSG_NOTE, vect_location,
1758 "outer-loop already vectorized.");
1759 return NULL;
1762 while (1)
1764 /* Check the CFG characteristics of the loop (nesting, entry/exit). */
1765 loop_vinfo = vect_analyze_loop_form (loop);
1766 if (!loop_vinfo)
1768 if (dump_enabled_p ())
1769 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1770 "bad loop form.");
1771 return NULL;
1774 if (vect_analyze_loop_2 (loop_vinfo))
1776 LOOP_VINFO_VECTORIZABLE_P (loop_vinfo) = 1;
1778 return loop_vinfo;
1781 destroy_loop_vec_info (loop_vinfo, true);
1783 vector_sizes &= ~current_vector_size;
1784 if (vector_sizes == 0
1785 || current_vector_size == 0)
1786 return NULL;
1788 /* Try the next biggest vector size. */
1789 current_vector_size = 1 << floor_log2 (vector_sizes);
1790 if (dump_enabled_p ())
1791 dump_printf_loc (MSG_NOTE, vect_location,
1792 "***** Re-trying analysis with "
1793 "vector size %d\n", current_vector_size);
1798 /* Function reduction_code_for_scalar_code
1800 Input:
1801 CODE - tree_code of a reduction operations.
1803 Output:
1804 REDUC_CODE - the corresponding tree-code to be used to reduce the
1805 vector of partial results into a single scalar result (which
1806 will also reside in a vector) or ERROR_MARK if the operation is
1807 a supported reduction operation, but does not have such tree-code.
1809 Return FALSE if CODE currently cannot be vectorized as reduction. */
1811 static bool
1812 reduction_code_for_scalar_code (enum tree_code code,
1813 enum tree_code *reduc_code)
1815 switch (code)
1817 case MAX_EXPR:
1818 *reduc_code = REDUC_MAX_EXPR;
1819 return true;
1821 case MIN_EXPR:
1822 *reduc_code = REDUC_MIN_EXPR;
1823 return true;
1825 case PLUS_EXPR:
1826 *reduc_code = REDUC_PLUS_EXPR;
1827 return true;
1829 case MULT_EXPR:
1830 case MINUS_EXPR:
1831 case BIT_IOR_EXPR:
1832 case BIT_XOR_EXPR:
1833 case BIT_AND_EXPR:
1834 *reduc_code = ERROR_MARK;
1835 return true;
1837 default:
1838 return false;
1843 /* Error reporting helper for vect_is_simple_reduction below. GIMPLE statement
1844 STMT is printed with a message MSG. */
1846 static void
1847 report_vect_op (int msg_type, gimple stmt, const char *msg)
1849 dump_printf_loc (msg_type, vect_location, "%s", msg);
1850 dump_gimple_stmt (msg_type, TDF_SLIM, stmt, 0);
1854 /* Detect SLP reduction of the form:
1856 #a1 = phi <a5, a0>
1857 a2 = operation (a1)
1858 a3 = operation (a2)
1859 a4 = operation (a3)
1860 a5 = operation (a4)
1862 #a = phi <a5>
1864 PHI is the reduction phi node (#a1 = phi <a5, a0> above)
1865 FIRST_STMT is the first reduction stmt in the chain
1866 (a2 = operation (a1)).
1868 Return TRUE if a reduction chain was detected. */
1870 static bool
1871 vect_is_slp_reduction (loop_vec_info loop_info, gimple phi, gimple first_stmt)
1873 struct loop *loop = (gimple_bb (phi))->loop_father;
1874 struct loop *vect_loop = LOOP_VINFO_LOOP (loop_info);
1875 enum tree_code code;
1876 gimple current_stmt = NULL, loop_use_stmt = NULL, first, next_stmt;
1877 stmt_vec_info use_stmt_info, current_stmt_info;
1878 tree lhs;
1879 imm_use_iterator imm_iter;
1880 use_operand_p use_p;
1881 int nloop_uses, size = 0, n_out_of_loop_uses;
1882 bool found = false;
1884 if (loop != vect_loop)
1885 return false;
1887 lhs = PHI_RESULT (phi);
1888 code = gimple_assign_rhs_code (first_stmt);
1889 while (1)
1891 nloop_uses = 0;
1892 n_out_of_loop_uses = 0;
1893 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, lhs)
1895 gimple use_stmt = USE_STMT (use_p);
1896 if (is_gimple_debug (use_stmt))
1897 continue;
1899 use_stmt = USE_STMT (use_p);
1901 /* Check if we got back to the reduction phi. */
1902 if (use_stmt == phi)
1904 loop_use_stmt = use_stmt;
1905 found = true;
1906 break;
1909 if (flow_bb_inside_loop_p (loop, gimple_bb (use_stmt)))
1911 if (vinfo_for_stmt (use_stmt)
1912 && !STMT_VINFO_IN_PATTERN_P (vinfo_for_stmt (use_stmt)))
1914 loop_use_stmt = use_stmt;
1915 nloop_uses++;
1918 else
1919 n_out_of_loop_uses++;
1921 /* There are can be either a single use in the loop or two uses in
1922 phi nodes. */
1923 if (nloop_uses > 1 || (n_out_of_loop_uses && nloop_uses))
1924 return false;
1927 if (found)
1928 break;
1930 /* We reached a statement with no loop uses. */
1931 if (nloop_uses == 0)
1932 return false;
1934 /* This is a loop exit phi, and we haven't reached the reduction phi. */
1935 if (gimple_code (loop_use_stmt) == GIMPLE_PHI)
1936 return false;
1938 if (!is_gimple_assign (loop_use_stmt)
1939 || code != gimple_assign_rhs_code (loop_use_stmt)
1940 || !flow_bb_inside_loop_p (loop, gimple_bb (loop_use_stmt)))
1941 return false;
1943 /* Insert USE_STMT into reduction chain. */
1944 use_stmt_info = vinfo_for_stmt (loop_use_stmt);
1945 if (current_stmt)
1947 current_stmt_info = vinfo_for_stmt (current_stmt);
1948 GROUP_NEXT_ELEMENT (current_stmt_info) = loop_use_stmt;
1949 GROUP_FIRST_ELEMENT (use_stmt_info)
1950 = GROUP_FIRST_ELEMENT (current_stmt_info);
1952 else
1953 GROUP_FIRST_ELEMENT (use_stmt_info) = loop_use_stmt;
1955 lhs = gimple_assign_lhs (loop_use_stmt);
1956 current_stmt = loop_use_stmt;
1957 size++;
1960 if (!found || loop_use_stmt != phi || size < 2)
1961 return false;
1963 /* Swap the operands, if needed, to make the reduction operand be the second
1964 operand. */
1965 lhs = PHI_RESULT (phi);
1966 next_stmt = GROUP_FIRST_ELEMENT (vinfo_for_stmt (current_stmt));
1967 while (next_stmt)
1969 if (gimple_assign_rhs2 (next_stmt) == lhs)
1971 tree op = gimple_assign_rhs1 (next_stmt);
1972 gimple def_stmt = NULL;
1974 if (TREE_CODE (op) == SSA_NAME)
1975 def_stmt = SSA_NAME_DEF_STMT (op);
1977 /* Check that the other def is either defined in the loop
1978 ("vect_internal_def"), or it's an induction (defined by a
1979 loop-header phi-node). */
1980 if (def_stmt
1981 && gimple_bb (def_stmt)
1982 && flow_bb_inside_loop_p (loop, gimple_bb (def_stmt))
1983 && (is_gimple_assign (def_stmt)
1984 || is_gimple_call (def_stmt)
1985 || STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def_stmt))
1986 == vect_induction_def
1987 || (gimple_code (def_stmt) == GIMPLE_PHI
1988 && STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def_stmt))
1989 == vect_internal_def
1990 && !is_loop_header_bb_p (gimple_bb (def_stmt)))))
1992 lhs = gimple_assign_lhs (next_stmt);
1993 next_stmt = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next_stmt));
1994 continue;
1997 return false;
1999 else
2001 tree op = gimple_assign_rhs2 (next_stmt);
2002 gimple def_stmt = NULL;
2004 if (TREE_CODE (op) == SSA_NAME)
2005 def_stmt = SSA_NAME_DEF_STMT (op);
2007 /* Check that the other def is either defined in the loop
2008 ("vect_internal_def"), or it's an induction (defined by a
2009 loop-header phi-node). */
2010 if (def_stmt
2011 && gimple_bb (def_stmt)
2012 && flow_bb_inside_loop_p (loop, gimple_bb (def_stmt))
2013 && (is_gimple_assign (def_stmt)
2014 || is_gimple_call (def_stmt)
2015 || STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def_stmt))
2016 == vect_induction_def
2017 || (gimple_code (def_stmt) == GIMPLE_PHI
2018 && STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def_stmt))
2019 == vect_internal_def
2020 && !is_loop_header_bb_p (gimple_bb (def_stmt)))))
2022 if (dump_enabled_p ())
2024 dump_printf_loc (MSG_NOTE, vect_location, "swapping oprnds: ");
2025 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, next_stmt, 0);
2028 swap_tree_operands (next_stmt,
2029 gimple_assign_rhs1_ptr (next_stmt),
2030 gimple_assign_rhs2_ptr (next_stmt));
2031 update_stmt (next_stmt);
2033 if (CONSTANT_CLASS_P (gimple_assign_rhs1 (next_stmt)))
2034 LOOP_VINFO_OPERANDS_SWAPPED (loop_info) = true;
2036 else
2037 return false;
2040 lhs = gimple_assign_lhs (next_stmt);
2041 next_stmt = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next_stmt));
2044 /* Save the chain for further analysis in SLP detection. */
2045 first = GROUP_FIRST_ELEMENT (vinfo_for_stmt (current_stmt));
2046 LOOP_VINFO_REDUCTION_CHAINS (loop_info).safe_push (first);
2047 GROUP_SIZE (vinfo_for_stmt (first)) = size;
2049 return true;
2053 /* Function vect_is_simple_reduction_1
2055 (1) Detect a cross-iteration def-use cycle that represents a simple
2056 reduction computation. We look for the following pattern:
2058 loop_header:
2059 a1 = phi < a0, a2 >
2060 a3 = ...
2061 a2 = operation (a3, a1)
2063 such that:
2064 1. operation is commutative and associative and it is safe to
2065 change the order of the computation (if CHECK_REDUCTION is true)
2066 2. no uses for a2 in the loop (a2 is used out of the loop)
2067 3. no uses of a1 in the loop besides the reduction operation
2068 4. no uses of a1 outside the loop.
2070 Conditions 1,4 are tested here.
2071 Conditions 2,3 are tested in vect_mark_stmts_to_be_vectorized.
2073 (2) Detect a cross-iteration def-use cycle in nested loops, i.e.,
2074 nested cycles, if CHECK_REDUCTION is false.
2076 (3) Detect cycles of phi nodes in outer-loop vectorization, i.e., double
2077 reductions:
2079 a1 = phi < a0, a2 >
2080 inner loop (def of a3)
2081 a2 = phi < a3 >
2083 If MODIFY is true it tries also to rework the code in-place to enable
2084 detection of more reduction patterns. For the time being we rewrite
2085 "res -= RHS" into "rhs += -RHS" when it seems worthwhile.
2088 static gimple
2089 vect_is_simple_reduction_1 (loop_vec_info loop_info, gimple phi,
2090 bool check_reduction, bool *double_reduc,
2091 bool modify)
2093 struct loop *loop = (gimple_bb (phi))->loop_father;
2094 struct loop *vect_loop = LOOP_VINFO_LOOP (loop_info);
2095 edge latch_e = loop_latch_edge (loop);
2096 tree loop_arg = PHI_ARG_DEF_FROM_EDGE (phi, latch_e);
2097 gimple def_stmt, def1 = NULL, def2 = NULL;
2098 enum tree_code orig_code, code;
2099 tree op1, op2, op3 = NULL_TREE, op4 = NULL_TREE;
2100 tree type;
2101 int nloop_uses;
2102 tree name;
2103 imm_use_iterator imm_iter;
2104 use_operand_p use_p;
2105 bool phi_def;
2107 *double_reduc = false;
2109 /* If CHECK_REDUCTION is true, we assume inner-most loop vectorization,
2110 otherwise, we assume outer loop vectorization. */
2111 gcc_assert ((check_reduction && loop == vect_loop)
2112 || (!check_reduction && flow_loop_nested_p (vect_loop, loop)));
2114 name = PHI_RESULT (phi);
2115 nloop_uses = 0;
2116 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, name)
2118 gimple use_stmt = USE_STMT (use_p);
2119 if (is_gimple_debug (use_stmt))
2120 continue;
2122 if (!flow_bb_inside_loop_p (loop, gimple_bb (use_stmt)))
2124 if (dump_enabled_p ())
2125 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2126 "intermediate value used outside loop.");
2128 return NULL;
2131 if (vinfo_for_stmt (use_stmt)
2132 && !is_pattern_stmt_p (vinfo_for_stmt (use_stmt)))
2133 nloop_uses++;
2134 if (nloop_uses > 1)
2136 if (dump_enabled_p ())
2137 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2138 "reduction used in loop.");
2139 return NULL;
2143 if (TREE_CODE (loop_arg) != SSA_NAME)
2145 if (dump_enabled_p ())
2147 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2148 "reduction: not ssa_name: ");
2149 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, loop_arg);
2151 return NULL;
2154 def_stmt = SSA_NAME_DEF_STMT (loop_arg);
2155 if (!def_stmt)
2157 if (dump_enabled_p ())
2158 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2159 "reduction: no def_stmt.");
2160 return NULL;
2163 if (!is_gimple_assign (def_stmt) && gimple_code (def_stmt) != GIMPLE_PHI)
2165 if (dump_enabled_p ())
2166 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, def_stmt, 0);
2167 return NULL;
2170 if (is_gimple_assign (def_stmt))
2172 name = gimple_assign_lhs (def_stmt);
2173 phi_def = false;
2175 else
2177 name = PHI_RESULT (def_stmt);
2178 phi_def = true;
2181 nloop_uses = 0;
2182 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, name)
2184 gimple use_stmt = USE_STMT (use_p);
2185 if (is_gimple_debug (use_stmt))
2186 continue;
2187 if (flow_bb_inside_loop_p (loop, gimple_bb (use_stmt))
2188 && vinfo_for_stmt (use_stmt)
2189 && !is_pattern_stmt_p (vinfo_for_stmt (use_stmt)))
2190 nloop_uses++;
2191 if (nloop_uses > 1)
2193 if (dump_enabled_p ())
2194 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2195 "reduction used in loop.");
2196 return NULL;
2200 /* If DEF_STMT is a phi node itself, we expect it to have a single argument
2201 defined in the inner loop. */
2202 if (phi_def)
2204 op1 = PHI_ARG_DEF (def_stmt, 0);
2206 if (gimple_phi_num_args (def_stmt) != 1
2207 || TREE_CODE (op1) != SSA_NAME)
2209 if (dump_enabled_p ())
2210 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2211 "unsupported phi node definition.");
2213 return NULL;
2216 def1 = SSA_NAME_DEF_STMT (op1);
2217 if (flow_bb_inside_loop_p (loop, gimple_bb (def_stmt))
2218 && loop->inner
2219 && flow_bb_inside_loop_p (loop->inner, gimple_bb (def1))
2220 && is_gimple_assign (def1))
2222 if (dump_enabled_p ())
2223 report_vect_op (MSG_NOTE, def_stmt,
2224 "detected double reduction: ");
2226 *double_reduc = true;
2227 return def_stmt;
2230 return NULL;
2233 code = orig_code = gimple_assign_rhs_code (def_stmt);
2235 /* We can handle "res -= x[i]", which is non-associative by
2236 simply rewriting this into "res += -x[i]". Avoid changing
2237 gimple instruction for the first simple tests and only do this
2238 if we're allowed to change code at all. */
2239 if (code == MINUS_EXPR
2240 && modify
2241 && (op1 = gimple_assign_rhs1 (def_stmt))
2242 && TREE_CODE (op1) == SSA_NAME
2243 && SSA_NAME_DEF_STMT (op1) == phi)
2244 code = PLUS_EXPR;
2246 if (check_reduction
2247 && (!commutative_tree_code (code) || !associative_tree_code (code)))
2249 if (dump_enabled_p ())
2250 report_vect_op (MSG_MISSED_OPTIMIZATION, def_stmt,
2251 "reduction: not commutative/associative: ");
2252 return NULL;
2255 if (get_gimple_rhs_class (code) != GIMPLE_BINARY_RHS)
2257 if (code != COND_EXPR)
2259 if (dump_enabled_p ())
2260 report_vect_op (MSG_MISSED_OPTIMIZATION, def_stmt,
2261 "reduction: not binary operation: ");
2263 return NULL;
2266 op3 = gimple_assign_rhs1 (def_stmt);
2267 if (COMPARISON_CLASS_P (op3))
2269 op4 = TREE_OPERAND (op3, 1);
2270 op3 = TREE_OPERAND (op3, 0);
2273 op1 = gimple_assign_rhs2 (def_stmt);
2274 op2 = gimple_assign_rhs3 (def_stmt);
2276 if (TREE_CODE (op1) != SSA_NAME && TREE_CODE (op2) != SSA_NAME)
2278 if (dump_enabled_p ())
2279 report_vect_op (MSG_MISSED_OPTIMIZATION, def_stmt,
2280 "reduction: uses not ssa_names: ");
2282 return NULL;
2285 else
2287 op1 = gimple_assign_rhs1 (def_stmt);
2288 op2 = gimple_assign_rhs2 (def_stmt);
2290 if (TREE_CODE (op1) != SSA_NAME && TREE_CODE (op2) != SSA_NAME)
2292 if (dump_enabled_p ())
2293 report_vect_op (MSG_MISSED_OPTIMIZATION, def_stmt,
2294 "reduction: uses not ssa_names: ");
2296 return NULL;
2300 type = TREE_TYPE (gimple_assign_lhs (def_stmt));
2301 if ((TREE_CODE (op1) == SSA_NAME
2302 && !types_compatible_p (type,TREE_TYPE (op1)))
2303 || (TREE_CODE (op2) == SSA_NAME
2304 && !types_compatible_p (type, TREE_TYPE (op2)))
2305 || (op3 && TREE_CODE (op3) == SSA_NAME
2306 && !types_compatible_p (type, TREE_TYPE (op3)))
2307 || (op4 && TREE_CODE (op4) == SSA_NAME
2308 && !types_compatible_p (type, TREE_TYPE (op4))))
2310 if (dump_enabled_p ())
2312 dump_printf_loc (MSG_NOTE, vect_location,
2313 "reduction: multiple types: operation type: ");
2314 dump_generic_expr (MSG_NOTE, TDF_SLIM, type);
2315 dump_printf (MSG_NOTE, ", operands types: ");
2316 dump_generic_expr (MSG_NOTE, TDF_SLIM,
2317 TREE_TYPE (op1));
2318 dump_printf (MSG_NOTE, ",");
2319 dump_generic_expr (MSG_NOTE, TDF_SLIM,
2320 TREE_TYPE (op2));
2321 if (op3)
2323 dump_printf (MSG_NOTE, ",");
2324 dump_generic_expr (MSG_NOTE, TDF_SLIM,
2325 TREE_TYPE (op3));
2328 if (op4)
2330 dump_printf (MSG_NOTE, ",");
2331 dump_generic_expr (MSG_NOTE, TDF_SLIM,
2332 TREE_TYPE (op4));
2336 return NULL;
2339 /* Check that it's ok to change the order of the computation.
2340 Generally, when vectorizing a reduction we change the order of the
2341 computation. This may change the behavior of the program in some
2342 cases, so we need to check that this is ok. One exception is when
2343 vectorizing an outer-loop: the inner-loop is executed sequentially,
2344 and therefore vectorizing reductions in the inner-loop during
2345 outer-loop vectorization is safe. */
2347 /* CHECKME: check for !flag_finite_math_only too? */
2348 if (SCALAR_FLOAT_TYPE_P (type) && !flag_associative_math
2349 && check_reduction)
2351 /* Changing the order of operations changes the semantics. */
2352 if (dump_enabled_p ())
2353 report_vect_op (MSG_MISSED_OPTIMIZATION, def_stmt,
2354 "reduction: unsafe fp math optimization: ");
2355 return NULL;
2357 else if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_TRAPS (type)
2358 && check_reduction)
2360 /* Changing the order of operations changes the semantics. */
2361 if (dump_enabled_p ())
2362 report_vect_op (MSG_MISSED_OPTIMIZATION, def_stmt,
2363 "reduction: unsafe int math optimization: ");
2364 return NULL;
2366 else if (SAT_FIXED_POINT_TYPE_P (type) && check_reduction)
2368 /* Changing the order of operations changes the semantics. */
2369 if (dump_enabled_p ())
2370 report_vect_op (MSG_MISSED_OPTIMIZATION, def_stmt,
2371 "reduction: unsafe fixed-point math optimization: ");
2372 return NULL;
2375 /* If we detected "res -= x[i]" earlier, rewrite it into
2376 "res += -x[i]" now. If this turns out to be useless reassoc
2377 will clean it up again. */
2378 if (orig_code == MINUS_EXPR)
2380 tree rhs = gimple_assign_rhs2 (def_stmt);
2381 tree negrhs = make_ssa_name (TREE_TYPE (rhs), NULL);
2382 gimple negate_stmt = gimple_build_assign_with_ops (NEGATE_EXPR, negrhs,
2383 rhs, NULL);
2384 gimple_stmt_iterator gsi = gsi_for_stmt (def_stmt);
2385 set_vinfo_for_stmt (negate_stmt, new_stmt_vec_info (negate_stmt,
2386 loop_info, NULL));
2387 gsi_insert_before (&gsi, negate_stmt, GSI_NEW_STMT);
2388 gimple_assign_set_rhs2 (def_stmt, negrhs);
2389 gimple_assign_set_rhs_code (def_stmt, PLUS_EXPR);
2390 update_stmt (def_stmt);
2393 /* Reduction is safe. We're dealing with one of the following:
2394 1) integer arithmetic and no trapv
2395 2) floating point arithmetic, and special flags permit this optimization
2396 3) nested cycle (i.e., outer loop vectorization). */
2397 if (TREE_CODE (op1) == SSA_NAME)
2398 def1 = SSA_NAME_DEF_STMT (op1);
2400 if (TREE_CODE (op2) == SSA_NAME)
2401 def2 = SSA_NAME_DEF_STMT (op2);
2403 if (code != COND_EXPR
2404 && ((!def1 || gimple_nop_p (def1)) && (!def2 || gimple_nop_p (def2))))
2406 if (dump_enabled_p ())
2407 report_vect_op (MSG_NOTE, def_stmt, "reduction: no defs for operands: ");
2408 return NULL;
2411 /* Check that one def is the reduction def, defined by PHI,
2412 the other def is either defined in the loop ("vect_internal_def"),
2413 or it's an induction (defined by a loop-header phi-node). */
2415 if (def2 && def2 == phi
2416 && (code == COND_EXPR
2417 || !def1 || gimple_nop_p (def1)
2418 || (def1 && flow_bb_inside_loop_p (loop, gimple_bb (def1))
2419 && (is_gimple_assign (def1)
2420 || is_gimple_call (def1)
2421 || STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def1))
2422 == vect_induction_def
2423 || (gimple_code (def1) == GIMPLE_PHI
2424 && STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def1))
2425 == vect_internal_def
2426 && !is_loop_header_bb_p (gimple_bb (def1)))))))
2428 if (dump_enabled_p ())
2429 report_vect_op (MSG_NOTE, def_stmt, "detected reduction: ");
2430 return def_stmt;
2433 if (def1 && def1 == phi
2434 && (code == COND_EXPR
2435 || !def2 || gimple_nop_p (def2)
2436 || (def2 && flow_bb_inside_loop_p (loop, gimple_bb (def2))
2437 && (is_gimple_assign (def2)
2438 || is_gimple_call (def2)
2439 || STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def2))
2440 == vect_induction_def
2441 || (gimple_code (def2) == GIMPLE_PHI
2442 && STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def2))
2443 == vect_internal_def
2444 && !is_loop_header_bb_p (gimple_bb (def2)))))))
2446 if (check_reduction)
2448 /* Swap operands (just for simplicity - so that the rest of the code
2449 can assume that the reduction variable is always the last (second)
2450 argument). */
2451 if (dump_enabled_p ())
2452 report_vect_op (MSG_NOTE, def_stmt,
2453 "detected reduction: need to swap operands: ");
2455 swap_tree_operands (def_stmt, gimple_assign_rhs1_ptr (def_stmt),
2456 gimple_assign_rhs2_ptr (def_stmt));
2458 if (CONSTANT_CLASS_P (gimple_assign_rhs1 (def_stmt)))
2459 LOOP_VINFO_OPERANDS_SWAPPED (loop_info) = true;
2461 else
2463 if (dump_enabled_p ())
2464 report_vect_op (MSG_NOTE, def_stmt, "detected reduction: ");
2467 return def_stmt;
2470 /* Try to find SLP reduction chain. */
2471 if (check_reduction && vect_is_slp_reduction (loop_info, phi, def_stmt))
2473 if (dump_enabled_p ())
2474 report_vect_op (MSG_NOTE, def_stmt,
2475 "reduction: detected reduction chain: ");
2477 return def_stmt;
2480 if (dump_enabled_p ())
2481 report_vect_op (MSG_MISSED_OPTIMIZATION, def_stmt,
2482 "reduction: unknown pattern: ");
2484 return NULL;
2487 /* Wrapper around vect_is_simple_reduction_1, that won't modify code
2488 in-place. Arguments as there. */
2490 static gimple
2491 vect_is_simple_reduction (loop_vec_info loop_info, gimple phi,
2492 bool check_reduction, bool *double_reduc)
2494 return vect_is_simple_reduction_1 (loop_info, phi, check_reduction,
2495 double_reduc, false);
2498 /* Wrapper around vect_is_simple_reduction_1, which will modify code
2499 in-place if it enables detection of more reductions. Arguments
2500 as there. */
2502 gimple
2503 vect_force_simple_reduction (loop_vec_info loop_info, gimple phi,
2504 bool check_reduction, bool *double_reduc)
2506 return vect_is_simple_reduction_1 (loop_info, phi, check_reduction,
2507 double_reduc, true);
2510 /* Calculate the cost of one scalar iteration of the loop. */
2512 vect_get_single_scalar_iteration_cost (loop_vec_info loop_vinfo)
2514 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
2515 basic_block *bbs = LOOP_VINFO_BBS (loop_vinfo);
2516 int nbbs = loop->num_nodes, factor, scalar_single_iter_cost = 0;
2517 int innerloop_iters, i, stmt_cost;
2519 /* Count statements in scalar loop. Using this as scalar cost for a single
2520 iteration for now.
2522 TODO: Add outer loop support.
2524 TODO: Consider assigning different costs to different scalar
2525 statements. */
2527 /* FORNOW. */
2528 innerloop_iters = 1;
2529 if (loop->inner)
2530 innerloop_iters = 50; /* FIXME */
2532 for (i = 0; i < nbbs; i++)
2534 gimple_stmt_iterator si;
2535 basic_block bb = bbs[i];
2537 if (bb->loop_father == loop->inner)
2538 factor = innerloop_iters;
2539 else
2540 factor = 1;
2542 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
2544 gimple stmt = gsi_stmt (si);
2545 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
2547 if (!is_gimple_assign (stmt) && !is_gimple_call (stmt))
2548 continue;
2550 /* Skip stmts that are not vectorized inside the loop. */
2551 if (stmt_info
2552 && !STMT_VINFO_RELEVANT_P (stmt_info)
2553 && (!STMT_VINFO_LIVE_P (stmt_info)
2554 || !VECTORIZABLE_CYCLE_DEF (STMT_VINFO_DEF_TYPE (stmt_info)))
2555 && !STMT_VINFO_IN_PATTERN_P (stmt_info))
2556 continue;
2558 if (STMT_VINFO_DATA_REF (vinfo_for_stmt (stmt)))
2560 if (DR_IS_READ (STMT_VINFO_DATA_REF (vinfo_for_stmt (stmt))))
2561 stmt_cost = vect_get_stmt_cost (scalar_load);
2562 else
2563 stmt_cost = vect_get_stmt_cost (scalar_store);
2565 else
2566 stmt_cost = vect_get_stmt_cost (scalar_stmt);
2568 scalar_single_iter_cost += stmt_cost * factor;
2571 return scalar_single_iter_cost;
2574 /* Calculate cost of peeling the loop PEEL_ITERS_PROLOGUE times. */
2576 vect_get_known_peeling_cost (loop_vec_info loop_vinfo, int peel_iters_prologue,
2577 int *peel_iters_epilogue,
2578 int scalar_single_iter_cost,
2579 stmt_vector_for_cost *prologue_cost_vec,
2580 stmt_vector_for_cost *epilogue_cost_vec)
2582 int retval = 0;
2583 int vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
2585 if (!LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo))
2587 *peel_iters_epilogue = vf/2;
2588 if (dump_enabled_p ())
2589 dump_printf_loc (MSG_NOTE, vect_location,
2590 "cost model: epilogue peel iters set to vf/2 "
2591 "because loop iterations are unknown .");
2593 /* If peeled iterations are known but number of scalar loop
2594 iterations are unknown, count a taken branch per peeled loop. */
2595 retval = record_stmt_cost (prologue_cost_vec, 2, cond_branch_taken,
2596 NULL, 0, vect_prologue);
2598 else
2600 int niters = LOOP_VINFO_INT_NITERS (loop_vinfo);
2601 peel_iters_prologue = niters < peel_iters_prologue ?
2602 niters : peel_iters_prologue;
2603 *peel_iters_epilogue = (niters - peel_iters_prologue) % vf;
2604 /* If we need to peel for gaps, but no peeling is required, we have to
2605 peel VF iterations. */
2606 if (LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo) && !*peel_iters_epilogue)
2607 *peel_iters_epilogue = vf;
2610 if (peel_iters_prologue)
2611 retval += record_stmt_cost (prologue_cost_vec,
2612 peel_iters_prologue * scalar_single_iter_cost,
2613 scalar_stmt, NULL, 0, vect_prologue);
2614 if (*peel_iters_epilogue)
2615 retval += record_stmt_cost (epilogue_cost_vec,
2616 *peel_iters_epilogue * scalar_single_iter_cost,
2617 scalar_stmt, NULL, 0, vect_epilogue);
2618 return retval;
2621 /* Function vect_estimate_min_profitable_iters
2623 Return the number of iterations required for the vector version of the
2624 loop to be profitable relative to the cost of the scalar version of the
2625 loop. */
2627 static void
2628 vect_estimate_min_profitable_iters (loop_vec_info loop_vinfo,
2629 int *ret_min_profitable_niters,
2630 int *ret_min_profitable_estimate)
2632 int min_profitable_iters;
2633 int min_profitable_estimate;
2634 int peel_iters_prologue;
2635 int peel_iters_epilogue;
2636 unsigned vec_inside_cost = 0;
2637 int vec_outside_cost = 0;
2638 unsigned vec_prologue_cost = 0;
2639 unsigned vec_epilogue_cost = 0;
2640 int scalar_single_iter_cost = 0;
2641 int scalar_outside_cost = 0;
2642 int vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
2643 int npeel = LOOP_PEELING_FOR_ALIGNMENT (loop_vinfo);
2644 void *target_cost_data = LOOP_VINFO_TARGET_COST_DATA (loop_vinfo);
2646 /* Cost model disabled. */
2647 if (!flag_vect_cost_model)
2649 dump_printf_loc (MSG_NOTE, vect_location, "cost model disabled.");
2650 *ret_min_profitable_niters = 0;
2651 *ret_min_profitable_estimate = 0;
2652 return;
2655 /* Requires loop versioning tests to handle misalignment. */
2656 if (LOOP_REQUIRES_VERSIONING_FOR_ALIGNMENT (loop_vinfo))
2658 /* FIXME: Make cost depend on complexity of individual check. */
2659 unsigned len = LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo).length ();
2660 (void) add_stmt_cost (target_cost_data, len, vector_stmt, NULL, 0,
2661 vect_prologue);
2662 dump_printf (MSG_NOTE,
2663 "cost model: Adding cost of checks for loop "
2664 "versioning to treat misalignment.\n");
2667 /* Requires loop versioning with alias checks. */
2668 if (LOOP_REQUIRES_VERSIONING_FOR_ALIAS (loop_vinfo))
2670 /* FIXME: Make cost depend on complexity of individual check. */
2671 unsigned len = LOOP_VINFO_MAY_ALIAS_DDRS (loop_vinfo).length ();
2672 (void) add_stmt_cost (target_cost_data, len, vector_stmt, NULL, 0,
2673 vect_prologue);
2674 dump_printf (MSG_NOTE,
2675 "cost model: Adding cost of checks for loop "
2676 "versioning aliasing.\n");
2679 if (LOOP_REQUIRES_VERSIONING_FOR_ALIGNMENT (loop_vinfo)
2680 || LOOP_REQUIRES_VERSIONING_FOR_ALIAS (loop_vinfo))
2681 (void) add_stmt_cost (target_cost_data, 1, cond_branch_taken, NULL, 0,
2682 vect_prologue);
2684 /* Count statements in scalar loop. Using this as scalar cost for a single
2685 iteration for now.
2687 TODO: Add outer loop support.
2689 TODO: Consider assigning different costs to different scalar
2690 statements. */
2692 scalar_single_iter_cost = vect_get_single_scalar_iteration_cost (loop_vinfo);
2694 /* Add additional cost for the peeled instructions in prologue and epilogue
2695 loop.
2697 FORNOW: If we don't know the value of peel_iters for prologue or epilogue
2698 at compile-time - we assume it's vf/2 (the worst would be vf-1).
2700 TODO: Build an expression that represents peel_iters for prologue and
2701 epilogue to be used in a run-time test. */
2703 if (npeel < 0)
2705 peel_iters_prologue = vf/2;
2706 dump_printf (MSG_NOTE, "cost model: "
2707 "prologue peel iters set to vf/2.");
2709 /* If peeling for alignment is unknown, loop bound of main loop becomes
2710 unknown. */
2711 peel_iters_epilogue = vf/2;
2712 dump_printf (MSG_NOTE, "cost model: "
2713 "epilogue peel iters set to vf/2 because "
2714 "peeling for alignment is unknown.");
2716 /* If peeled iterations are unknown, count a taken branch and a not taken
2717 branch per peeled loop. Even if scalar loop iterations are known,
2718 vector iterations are not known since peeled prologue iterations are
2719 not known. Hence guards remain the same. */
2720 (void) add_stmt_cost (target_cost_data, 2, cond_branch_taken,
2721 NULL, 0, vect_prologue);
2722 (void) add_stmt_cost (target_cost_data, 2, cond_branch_not_taken,
2723 NULL, 0, vect_prologue);
2724 /* FORNOW: Don't attempt to pass individual scalar instructions to
2725 the model; just assume linear cost for scalar iterations. */
2726 (void) add_stmt_cost (target_cost_data,
2727 peel_iters_prologue * scalar_single_iter_cost,
2728 scalar_stmt, NULL, 0, vect_prologue);
2729 (void) add_stmt_cost (target_cost_data,
2730 peel_iters_epilogue * scalar_single_iter_cost,
2731 scalar_stmt, NULL, 0, vect_epilogue);
2733 else
2735 stmt_vector_for_cost prologue_cost_vec, epilogue_cost_vec;
2736 stmt_info_for_cost *si;
2737 int j;
2738 void *data = LOOP_VINFO_TARGET_COST_DATA (loop_vinfo);
2740 prologue_cost_vec.create (2);
2741 epilogue_cost_vec.create (2);
2742 peel_iters_prologue = npeel;
2744 (void) vect_get_known_peeling_cost (loop_vinfo, peel_iters_prologue,
2745 &peel_iters_epilogue,
2746 scalar_single_iter_cost,
2747 &prologue_cost_vec,
2748 &epilogue_cost_vec);
2750 FOR_EACH_VEC_ELT (prologue_cost_vec, j, si)
2752 struct _stmt_vec_info *stmt_info
2753 = si->stmt ? vinfo_for_stmt (si->stmt) : NULL;
2754 (void) add_stmt_cost (data, si->count, si->kind, stmt_info,
2755 si->misalign, vect_prologue);
2758 FOR_EACH_VEC_ELT (epilogue_cost_vec, j, si)
2760 struct _stmt_vec_info *stmt_info
2761 = si->stmt ? vinfo_for_stmt (si->stmt) : NULL;
2762 (void) add_stmt_cost (data, si->count, si->kind, stmt_info,
2763 si->misalign, vect_epilogue);
2766 prologue_cost_vec.release ();
2767 epilogue_cost_vec.release ();
2770 /* FORNOW: The scalar outside cost is incremented in one of the
2771 following ways:
2773 1. The vectorizer checks for alignment and aliasing and generates
2774 a condition that allows dynamic vectorization. A cost model
2775 check is ANDED with the versioning condition. Hence scalar code
2776 path now has the added cost of the versioning check.
2778 if (cost > th & versioning_check)
2779 jmp to vector code
2781 Hence run-time scalar is incremented by not-taken branch cost.
2783 2. The vectorizer then checks if a prologue is required. If the
2784 cost model check was not done before during versioning, it has to
2785 be done before the prologue check.
2787 if (cost <= th)
2788 prologue = scalar_iters
2789 if (prologue == 0)
2790 jmp to vector code
2791 else
2792 execute prologue
2793 if (prologue == num_iters)
2794 go to exit
2796 Hence the run-time scalar cost is incremented by a taken branch,
2797 plus a not-taken branch, plus a taken branch cost.
2799 3. The vectorizer then checks if an epilogue is required. If the
2800 cost model check was not done before during prologue check, it
2801 has to be done with the epilogue check.
2803 if (prologue == 0)
2804 jmp to vector code
2805 else
2806 execute prologue
2807 if (prologue == num_iters)
2808 go to exit
2809 vector code:
2810 if ((cost <= th) | (scalar_iters-prologue-epilogue == 0))
2811 jmp to epilogue
2813 Hence the run-time scalar cost should be incremented by 2 taken
2814 branches.
2816 TODO: The back end may reorder the BBS's differently and reverse
2817 conditions/branch directions. Change the estimates below to
2818 something more reasonable. */
2820 /* If the number of iterations is known and we do not do versioning, we can
2821 decide whether to vectorize at compile time. Hence the scalar version
2822 do not carry cost model guard costs. */
2823 if (!LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo)
2824 || LOOP_REQUIRES_VERSIONING_FOR_ALIGNMENT (loop_vinfo)
2825 || LOOP_REQUIRES_VERSIONING_FOR_ALIAS (loop_vinfo))
2827 /* Cost model check occurs at versioning. */
2828 if (LOOP_REQUIRES_VERSIONING_FOR_ALIGNMENT (loop_vinfo)
2829 || LOOP_REQUIRES_VERSIONING_FOR_ALIAS (loop_vinfo))
2830 scalar_outside_cost += vect_get_stmt_cost (cond_branch_not_taken);
2831 else
2833 /* Cost model check occurs at prologue generation. */
2834 if (LOOP_PEELING_FOR_ALIGNMENT (loop_vinfo) < 0)
2835 scalar_outside_cost += 2 * vect_get_stmt_cost (cond_branch_taken)
2836 + vect_get_stmt_cost (cond_branch_not_taken);
2837 /* Cost model check occurs at epilogue generation. */
2838 else
2839 scalar_outside_cost += 2 * vect_get_stmt_cost (cond_branch_taken);
2843 /* Complete the target-specific cost calculations. */
2844 finish_cost (LOOP_VINFO_TARGET_COST_DATA (loop_vinfo), &vec_prologue_cost,
2845 &vec_inside_cost, &vec_epilogue_cost);
2847 vec_outside_cost = (int)(vec_prologue_cost + vec_epilogue_cost);
2849 /* Calculate number of iterations required to make the vector version
2850 profitable, relative to the loop bodies only. The following condition
2851 must hold true:
2852 SIC * niters + SOC > VIC * ((niters-PL_ITERS-EP_ITERS)/VF) + VOC
2853 where
2854 SIC = scalar iteration cost, VIC = vector iteration cost,
2855 VOC = vector outside cost, VF = vectorization factor,
2856 PL_ITERS = prologue iterations, EP_ITERS= epilogue iterations
2857 SOC = scalar outside cost for run time cost model check. */
2859 if ((scalar_single_iter_cost * vf) > (int) vec_inside_cost)
2861 if (vec_outside_cost <= 0)
2862 min_profitable_iters = 1;
2863 else
2865 min_profitable_iters = ((vec_outside_cost - scalar_outside_cost) * vf
2866 - vec_inside_cost * peel_iters_prologue
2867 - vec_inside_cost * peel_iters_epilogue)
2868 / ((scalar_single_iter_cost * vf)
2869 - vec_inside_cost);
2871 if ((scalar_single_iter_cost * vf * min_profitable_iters)
2872 <= (((int) vec_inside_cost * min_profitable_iters)
2873 + (((int) vec_outside_cost - scalar_outside_cost) * vf)))
2874 min_profitable_iters++;
2877 /* vector version will never be profitable. */
2878 else
2880 if (dump_enabled_p ())
2881 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2882 "cost model: the vector iteration cost = %d "
2883 "divided by the scalar iteration cost = %d "
2884 "is greater or equal to the vectorization factor = %d.",
2885 vec_inside_cost, scalar_single_iter_cost, vf);
2886 *ret_min_profitable_niters = -1;
2887 *ret_min_profitable_estimate = -1;
2888 return;
2891 if (dump_enabled_p ())
2893 dump_printf_loc (MSG_NOTE, vect_location, "Cost model analysis: \n");
2894 dump_printf (MSG_NOTE, " Vector inside of loop cost: %d\n",
2895 vec_inside_cost);
2896 dump_printf (MSG_NOTE, " Vector prologue cost: %d\n",
2897 vec_prologue_cost);
2898 dump_printf (MSG_NOTE, " Vector epilogue cost: %d\n",
2899 vec_epilogue_cost);
2900 dump_printf (MSG_NOTE, " Scalar iteration cost: %d\n",
2901 scalar_single_iter_cost);
2902 dump_printf (MSG_NOTE, " Scalar outside cost: %d\n",
2903 scalar_outside_cost);
2904 dump_printf (MSG_NOTE, " Vector outside cost: %d\n",
2905 vec_outside_cost);
2906 dump_printf (MSG_NOTE, " prologue iterations: %d\n",
2907 peel_iters_prologue);
2908 dump_printf (MSG_NOTE, " epilogue iterations: %d\n",
2909 peel_iters_epilogue);
2910 dump_printf (MSG_NOTE,
2911 " Calculated minimum iters for profitability: %d\n",
2912 min_profitable_iters);
2915 min_profitable_iters =
2916 min_profitable_iters < vf ? vf : min_profitable_iters;
2918 /* Because the condition we create is:
2919 if (niters <= min_profitable_iters)
2920 then skip the vectorized loop. */
2921 min_profitable_iters--;
2923 if (dump_enabled_p ())
2924 dump_printf_loc (MSG_NOTE, vect_location,
2925 " Runtime profitability threshold = %d\n", min_profitable_iters);
2927 *ret_min_profitable_niters = min_profitable_iters;
2929 /* Calculate number of iterations required to make the vector version
2930 profitable, relative to the loop bodies only.
2932 Non-vectorized variant is SIC * niters and it must win over vector
2933 variant on the expected loop trip count. The following condition must hold true:
2934 SIC * niters > VIC * ((niters-PL_ITERS-EP_ITERS)/VF) + VOC + SOC */
2936 if (vec_outside_cost <= 0)
2937 min_profitable_estimate = 1;
2938 else
2940 min_profitable_estimate = ((vec_outside_cost + scalar_outside_cost) * vf
2941 - vec_inside_cost * peel_iters_prologue
2942 - vec_inside_cost * peel_iters_epilogue)
2943 / ((scalar_single_iter_cost * vf)
2944 - vec_inside_cost);
2946 min_profitable_estimate --;
2947 min_profitable_estimate = MAX (min_profitable_estimate, min_profitable_iters);
2948 if (dump_enabled_p ())
2949 dump_printf_loc (MSG_NOTE, vect_location,
2950 " Static estimate profitability threshold = %d\n",
2951 min_profitable_iters);
2953 *ret_min_profitable_estimate = min_profitable_estimate;
2957 /* TODO: Close dependency between vect_model_*_cost and vectorizable_*
2958 functions. Design better to avoid maintenance issues. */
2960 /* Function vect_model_reduction_cost.
2962 Models cost for a reduction operation, including the vector ops
2963 generated within the strip-mine loop, the initial definition before
2964 the loop, and the epilogue code that must be generated. */
2966 static bool
2967 vect_model_reduction_cost (stmt_vec_info stmt_info, enum tree_code reduc_code,
2968 int ncopies)
2970 int prologue_cost = 0, epilogue_cost = 0;
2971 enum tree_code code;
2972 optab optab;
2973 tree vectype;
2974 gimple stmt, orig_stmt;
2975 tree reduction_op;
2976 enum machine_mode mode;
2977 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
2978 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
2979 void *target_cost_data = LOOP_VINFO_TARGET_COST_DATA (loop_vinfo);
2981 /* Cost of reduction op inside loop. */
2982 unsigned inside_cost = add_stmt_cost (target_cost_data, ncopies, vector_stmt,
2983 stmt_info, 0, vect_body);
2984 stmt = STMT_VINFO_STMT (stmt_info);
2986 switch (get_gimple_rhs_class (gimple_assign_rhs_code (stmt)))
2988 case GIMPLE_SINGLE_RHS:
2989 gcc_assert (TREE_OPERAND_LENGTH (gimple_assign_rhs1 (stmt)) == ternary_op);
2990 reduction_op = TREE_OPERAND (gimple_assign_rhs1 (stmt), 2);
2991 break;
2992 case GIMPLE_UNARY_RHS:
2993 reduction_op = gimple_assign_rhs1 (stmt);
2994 break;
2995 case GIMPLE_BINARY_RHS:
2996 reduction_op = gimple_assign_rhs2 (stmt);
2997 break;
2998 case GIMPLE_TERNARY_RHS:
2999 reduction_op = gimple_assign_rhs3 (stmt);
3000 break;
3001 default:
3002 gcc_unreachable ();
3005 vectype = get_vectype_for_scalar_type (TREE_TYPE (reduction_op));
3006 if (!vectype)
3008 if (dump_enabled_p ())
3010 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3011 "unsupported data-type ");
3012 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
3013 TREE_TYPE (reduction_op));
3015 return false;
3018 mode = TYPE_MODE (vectype);
3019 orig_stmt = STMT_VINFO_RELATED_STMT (stmt_info);
3021 if (!orig_stmt)
3022 orig_stmt = STMT_VINFO_STMT (stmt_info);
3024 code = gimple_assign_rhs_code (orig_stmt);
3026 /* Add in cost for initial definition. */
3027 prologue_cost += add_stmt_cost (target_cost_data, 1, scalar_to_vec,
3028 stmt_info, 0, vect_prologue);
3030 /* Determine cost of epilogue code.
3032 We have a reduction operator that will reduce the vector in one statement.
3033 Also requires scalar extract. */
3035 if (!nested_in_vect_loop_p (loop, orig_stmt))
3037 if (reduc_code != ERROR_MARK)
3039 epilogue_cost += add_stmt_cost (target_cost_data, 1, vector_stmt,
3040 stmt_info, 0, vect_epilogue);
3041 epilogue_cost += add_stmt_cost (target_cost_data, 1, vec_to_scalar,
3042 stmt_info, 0, vect_epilogue);
3044 else
3046 int vec_size_in_bits = tree_low_cst (TYPE_SIZE (vectype), 1);
3047 tree bitsize =
3048 TYPE_SIZE (TREE_TYPE (gimple_assign_lhs (orig_stmt)));
3049 int element_bitsize = tree_low_cst (bitsize, 1);
3050 int nelements = vec_size_in_bits / element_bitsize;
3052 optab = optab_for_tree_code (code, vectype, optab_default);
3054 /* We have a whole vector shift available. */
3055 if (VECTOR_MODE_P (mode)
3056 && optab_handler (optab, mode) != CODE_FOR_nothing
3057 && optab_handler (vec_shr_optab, mode) != CODE_FOR_nothing)
3059 /* Final reduction via vector shifts and the reduction operator.
3060 Also requires scalar extract. */
3061 epilogue_cost += add_stmt_cost (target_cost_data,
3062 exact_log2 (nelements) * 2,
3063 vector_stmt, stmt_info, 0,
3064 vect_epilogue);
3065 epilogue_cost += add_stmt_cost (target_cost_data, 1,
3066 vec_to_scalar, stmt_info, 0,
3067 vect_epilogue);
3069 else
3070 /* Use extracts and reduction op for final reduction. For N
3071 elements, we have N extracts and N-1 reduction ops. */
3072 epilogue_cost += add_stmt_cost (target_cost_data,
3073 nelements + nelements - 1,
3074 vector_stmt, stmt_info, 0,
3075 vect_epilogue);
3079 if (dump_enabled_p ())
3080 dump_printf (MSG_NOTE,
3081 "vect_model_reduction_cost: inside_cost = %d, "
3082 "prologue_cost = %d, epilogue_cost = %d .", inside_cost,
3083 prologue_cost, epilogue_cost);
3085 return true;
3089 /* Function vect_model_induction_cost.
3091 Models cost for induction operations. */
3093 static void
3094 vect_model_induction_cost (stmt_vec_info stmt_info, int ncopies)
3096 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
3097 void *target_cost_data = LOOP_VINFO_TARGET_COST_DATA (loop_vinfo);
3098 unsigned inside_cost, prologue_cost;
3100 /* loop cost for vec_loop. */
3101 inside_cost = add_stmt_cost (target_cost_data, ncopies, vector_stmt,
3102 stmt_info, 0, vect_body);
3104 /* prologue cost for vec_init and vec_step. */
3105 prologue_cost = add_stmt_cost (target_cost_data, 2, scalar_to_vec,
3106 stmt_info, 0, vect_prologue);
3108 if (dump_enabled_p ())
3109 dump_printf_loc (MSG_NOTE, vect_location,
3110 "vect_model_induction_cost: inside_cost = %d, "
3111 "prologue_cost = %d .", inside_cost, prologue_cost);
3115 /* Function get_initial_def_for_induction
3117 Input:
3118 STMT - a stmt that performs an induction operation in the loop.
3119 IV_PHI - the initial value of the induction variable
3121 Output:
3122 Return a vector variable, initialized with the first VF values of
3123 the induction variable. E.g., for an iv with IV_PHI='X' and
3124 evolution S, for a vector of 4 units, we want to return:
3125 [X, X + S, X + 2*S, X + 3*S]. */
3127 static tree
3128 get_initial_def_for_induction (gimple iv_phi)
3130 stmt_vec_info stmt_vinfo = vinfo_for_stmt (iv_phi);
3131 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_vinfo);
3132 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
3133 tree scalar_type;
3134 tree vectype;
3135 int nunits;
3136 edge pe = loop_preheader_edge (loop);
3137 struct loop *iv_loop;
3138 basic_block new_bb;
3139 tree new_vec, vec_init, vec_step, t;
3140 tree access_fn;
3141 tree new_var;
3142 tree new_name;
3143 gimple init_stmt, induction_phi, new_stmt;
3144 tree induc_def, vec_def, vec_dest;
3145 tree init_expr, step_expr;
3146 int vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
3147 int i;
3148 bool ok;
3149 int ncopies;
3150 tree expr;
3151 stmt_vec_info phi_info = vinfo_for_stmt (iv_phi);
3152 bool nested_in_vect_loop = false;
3153 gimple_seq stmts = NULL;
3154 imm_use_iterator imm_iter;
3155 use_operand_p use_p;
3156 gimple exit_phi;
3157 edge latch_e;
3158 tree loop_arg;
3159 gimple_stmt_iterator si;
3160 basic_block bb = gimple_bb (iv_phi);
3161 tree stepvectype;
3162 tree resvectype;
3164 /* Is phi in an inner-loop, while vectorizing an enclosing outer-loop? */
3165 if (nested_in_vect_loop_p (loop, iv_phi))
3167 nested_in_vect_loop = true;
3168 iv_loop = loop->inner;
3170 else
3171 iv_loop = loop;
3172 gcc_assert (iv_loop == (gimple_bb (iv_phi))->loop_father);
3174 latch_e = loop_latch_edge (iv_loop);
3175 loop_arg = PHI_ARG_DEF_FROM_EDGE (iv_phi, latch_e);
3177 access_fn = analyze_scalar_evolution (iv_loop, PHI_RESULT (iv_phi));
3178 gcc_assert (access_fn);
3179 STRIP_NOPS (access_fn);
3180 ok = vect_is_simple_iv_evolution (iv_loop->num, access_fn,
3181 &init_expr, &step_expr);
3182 gcc_assert (ok);
3183 pe = loop_preheader_edge (iv_loop);
3185 scalar_type = TREE_TYPE (init_expr);
3186 vectype = get_vectype_for_scalar_type (scalar_type);
3187 resvectype = get_vectype_for_scalar_type (TREE_TYPE (PHI_RESULT (iv_phi)));
3188 gcc_assert (vectype);
3189 nunits = TYPE_VECTOR_SUBPARTS (vectype);
3190 ncopies = vf / nunits;
3192 gcc_assert (phi_info);
3193 gcc_assert (ncopies >= 1);
3195 /* Find the first insertion point in the BB. */
3196 si = gsi_after_labels (bb);
3198 /* Create the vector that holds the initial_value of the induction. */
3199 if (nested_in_vect_loop)
3201 /* iv_loop is nested in the loop to be vectorized. init_expr had already
3202 been created during vectorization of previous stmts. We obtain it
3203 from the STMT_VINFO_VEC_STMT of the defining stmt. */
3204 tree iv_def = PHI_ARG_DEF_FROM_EDGE (iv_phi,
3205 loop_preheader_edge (iv_loop));
3206 vec_init = vect_get_vec_def_for_operand (iv_def, iv_phi, NULL);
3208 else
3210 vec<constructor_elt, va_gc> *v;
3212 /* iv_loop is the loop to be vectorized. Create:
3213 vec_init = [X, X+S, X+2*S, X+3*S] (S = step_expr, X = init_expr) */
3214 new_var = vect_get_new_vect_var (scalar_type, vect_scalar_var, "var_");
3215 new_name = force_gimple_operand (init_expr, &stmts, false, new_var);
3216 if (stmts)
3218 new_bb = gsi_insert_seq_on_edge_immediate (pe, stmts);
3219 gcc_assert (!new_bb);
3222 vec_alloc (v, nunits);
3223 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, new_name);
3224 for (i = 1; i < nunits; i++)
3226 /* Create: new_name_i = new_name + step_expr */
3227 enum tree_code code = POINTER_TYPE_P (scalar_type)
3228 ? POINTER_PLUS_EXPR : PLUS_EXPR;
3229 init_stmt = gimple_build_assign_with_ops (code, new_var,
3230 new_name, step_expr);
3231 new_name = make_ssa_name (new_var, init_stmt);
3232 gimple_assign_set_lhs (init_stmt, new_name);
3234 new_bb = gsi_insert_on_edge_immediate (pe, init_stmt);
3235 gcc_assert (!new_bb);
3237 if (dump_enabled_p ())
3239 dump_printf_loc (MSG_NOTE, vect_location,
3240 "created new init_stmt: ");
3241 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, init_stmt, 0);
3243 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, new_name);
3245 /* Create a vector from [new_name_0, new_name_1, ..., new_name_nunits-1] */
3246 new_vec = build_constructor (vectype, v);
3247 vec_init = vect_init_vector (iv_phi, new_vec, vectype, NULL);
3251 /* Create the vector that holds the step of the induction. */
3252 if (nested_in_vect_loop)
3253 /* iv_loop is nested in the loop to be vectorized. Generate:
3254 vec_step = [S, S, S, S] */
3255 new_name = step_expr;
3256 else
3258 /* iv_loop is the loop to be vectorized. Generate:
3259 vec_step = [VF*S, VF*S, VF*S, VF*S] */
3260 expr = build_int_cst (TREE_TYPE (step_expr), vf);
3261 new_name = fold_build2 (MULT_EXPR, TREE_TYPE (step_expr),
3262 expr, step_expr);
3265 t = unshare_expr (new_name);
3266 gcc_assert (CONSTANT_CLASS_P (new_name));
3267 stepvectype = get_vectype_for_scalar_type (TREE_TYPE (new_name));
3268 gcc_assert (stepvectype);
3269 new_vec = build_vector_from_val (stepvectype, t);
3270 vec_step = vect_init_vector (iv_phi, new_vec, stepvectype, NULL);
3273 /* Create the following def-use cycle:
3274 loop prolog:
3275 vec_init = ...
3276 vec_step = ...
3277 loop:
3278 vec_iv = PHI <vec_init, vec_loop>
3280 STMT
3282 vec_loop = vec_iv + vec_step; */
3284 /* Create the induction-phi that defines the induction-operand. */
3285 vec_dest = vect_get_new_vect_var (vectype, vect_simple_var, "vec_iv_");
3286 induction_phi = create_phi_node (vec_dest, iv_loop->header);
3287 set_vinfo_for_stmt (induction_phi,
3288 new_stmt_vec_info (induction_phi, loop_vinfo, NULL));
3289 induc_def = PHI_RESULT (induction_phi);
3291 /* Create the iv update inside the loop */
3292 new_stmt = gimple_build_assign_with_ops (PLUS_EXPR, vec_dest,
3293 induc_def, vec_step);
3294 vec_def = make_ssa_name (vec_dest, new_stmt);
3295 gimple_assign_set_lhs (new_stmt, vec_def);
3296 gsi_insert_before (&si, new_stmt, GSI_SAME_STMT);
3297 set_vinfo_for_stmt (new_stmt, new_stmt_vec_info (new_stmt, loop_vinfo,
3298 NULL));
3300 /* Set the arguments of the phi node: */
3301 add_phi_arg (induction_phi, vec_init, pe, UNKNOWN_LOCATION);
3302 add_phi_arg (induction_phi, vec_def, loop_latch_edge (iv_loop),
3303 UNKNOWN_LOCATION);
3306 /* In case that vectorization factor (VF) is bigger than the number
3307 of elements that we can fit in a vectype (nunits), we have to generate
3308 more than one vector stmt - i.e - we need to "unroll" the
3309 vector stmt by a factor VF/nunits. For more details see documentation
3310 in vectorizable_operation. */
3312 if (ncopies > 1)
3314 stmt_vec_info prev_stmt_vinfo;
3315 /* FORNOW. This restriction should be relaxed. */
3316 gcc_assert (!nested_in_vect_loop);
3318 /* Create the vector that holds the step of the induction. */
3319 expr = build_int_cst (TREE_TYPE (step_expr), nunits);
3320 new_name = fold_build2 (MULT_EXPR, TREE_TYPE (step_expr),
3321 expr, step_expr);
3322 t = unshare_expr (new_name);
3323 gcc_assert (CONSTANT_CLASS_P (new_name));
3324 new_vec = build_vector_from_val (stepvectype, t);
3325 vec_step = vect_init_vector (iv_phi, new_vec, stepvectype, NULL);
3327 vec_def = induc_def;
3328 prev_stmt_vinfo = vinfo_for_stmt (induction_phi);
3329 for (i = 1; i < ncopies; i++)
3331 /* vec_i = vec_prev + vec_step */
3332 new_stmt = gimple_build_assign_with_ops (PLUS_EXPR, vec_dest,
3333 vec_def, vec_step);
3334 vec_def = make_ssa_name (vec_dest, new_stmt);
3335 gimple_assign_set_lhs (new_stmt, vec_def);
3337 gsi_insert_before (&si, new_stmt, GSI_SAME_STMT);
3338 if (!useless_type_conversion_p (resvectype, vectype))
3340 new_stmt = gimple_build_assign_with_ops
3341 (VIEW_CONVERT_EXPR,
3342 vect_get_new_vect_var (resvectype, vect_simple_var,
3343 "vec_iv_"),
3344 build1 (VIEW_CONVERT_EXPR, resvectype,
3345 gimple_assign_lhs (new_stmt)), NULL_TREE);
3346 gimple_assign_set_lhs (new_stmt,
3347 make_ssa_name
3348 (gimple_assign_lhs (new_stmt), new_stmt));
3349 gsi_insert_before (&si, new_stmt, GSI_SAME_STMT);
3351 set_vinfo_for_stmt (new_stmt,
3352 new_stmt_vec_info (new_stmt, loop_vinfo, NULL));
3353 STMT_VINFO_RELATED_STMT (prev_stmt_vinfo) = new_stmt;
3354 prev_stmt_vinfo = vinfo_for_stmt (new_stmt);
3358 if (nested_in_vect_loop)
3360 /* Find the loop-closed exit-phi of the induction, and record
3361 the final vector of induction results: */
3362 exit_phi = NULL;
3363 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, loop_arg)
3365 if (!flow_bb_inside_loop_p (iv_loop, gimple_bb (USE_STMT (use_p))))
3367 exit_phi = USE_STMT (use_p);
3368 break;
3371 if (exit_phi)
3373 stmt_vec_info stmt_vinfo = vinfo_for_stmt (exit_phi);
3374 /* FORNOW. Currently not supporting the case that an inner-loop induction
3375 is not used in the outer-loop (i.e. only outside the outer-loop). */
3376 gcc_assert (STMT_VINFO_RELEVANT_P (stmt_vinfo)
3377 && !STMT_VINFO_LIVE_P (stmt_vinfo));
3379 STMT_VINFO_VEC_STMT (stmt_vinfo) = new_stmt;
3380 if (dump_enabled_p ())
3382 dump_printf_loc (MSG_NOTE, vect_location,
3383 "vector of inductions after inner-loop:");
3384 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, new_stmt, 0);
3390 if (dump_enabled_p ())
3392 dump_printf_loc (MSG_NOTE, vect_location,
3393 "transform induction: created def-use cycle: ");
3394 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, induction_phi, 0);
3395 dump_printf (MSG_NOTE, "\n");
3396 dump_gimple_stmt (MSG_NOTE, TDF_SLIM,
3397 SSA_NAME_DEF_STMT (vec_def), 0);
3400 STMT_VINFO_VEC_STMT (phi_info) = induction_phi;
3401 if (!useless_type_conversion_p (resvectype, vectype))
3403 new_stmt = gimple_build_assign_with_ops
3404 (VIEW_CONVERT_EXPR,
3405 vect_get_new_vect_var (resvectype, vect_simple_var, "vec_iv_"),
3406 build1 (VIEW_CONVERT_EXPR, resvectype, induc_def), NULL_TREE);
3407 induc_def = make_ssa_name (gimple_assign_lhs (new_stmt), new_stmt);
3408 gimple_assign_set_lhs (new_stmt, induc_def);
3409 si = gsi_start_bb (bb);
3410 gsi_insert_before (&si, new_stmt, GSI_SAME_STMT);
3411 set_vinfo_for_stmt (new_stmt,
3412 new_stmt_vec_info (new_stmt, loop_vinfo, NULL));
3413 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (new_stmt))
3414 = STMT_VINFO_RELATED_STMT (vinfo_for_stmt (induction_phi));
3417 return induc_def;
3421 /* Function get_initial_def_for_reduction
3423 Input:
3424 STMT - a stmt that performs a reduction operation in the loop.
3425 INIT_VAL - the initial value of the reduction variable
3427 Output:
3428 ADJUSTMENT_DEF - a tree that holds a value to be added to the final result
3429 of the reduction (used for adjusting the epilog - see below).
3430 Return a vector variable, initialized according to the operation that STMT
3431 performs. This vector will be used as the initial value of the
3432 vector of partial results.
3434 Option1 (adjust in epilog): Initialize the vector as follows:
3435 add/bit or/xor: [0,0,...,0,0]
3436 mult/bit and: [1,1,...,1,1]
3437 min/max/cond_expr: [init_val,init_val,..,init_val,init_val]
3438 and when necessary (e.g. add/mult case) let the caller know
3439 that it needs to adjust the result by init_val.
3441 Option2: Initialize the vector as follows:
3442 add/bit or/xor: [init_val,0,0,...,0]
3443 mult/bit and: [init_val,1,1,...,1]
3444 min/max/cond_expr: [init_val,init_val,...,init_val]
3445 and no adjustments are needed.
3447 For example, for the following code:
3449 s = init_val;
3450 for (i=0;i<n;i++)
3451 s = s + a[i];
3453 STMT is 's = s + a[i]', and the reduction variable is 's'.
3454 For a vector of 4 units, we want to return either [0,0,0,init_val],
3455 or [0,0,0,0] and let the caller know that it needs to adjust
3456 the result at the end by 'init_val'.
3458 FORNOW, we are using the 'adjust in epilog' scheme, because this way the
3459 initialization vector is simpler (same element in all entries), if
3460 ADJUSTMENT_DEF is not NULL, and Option2 otherwise.
3462 A cost model should help decide between these two schemes. */
3464 tree
3465 get_initial_def_for_reduction (gimple stmt, tree init_val,
3466 tree *adjustment_def)
3468 stmt_vec_info stmt_vinfo = vinfo_for_stmt (stmt);
3469 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_vinfo);
3470 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
3471 tree scalar_type = TREE_TYPE (init_val);
3472 tree vectype = get_vectype_for_scalar_type (scalar_type);
3473 int nunits;
3474 enum tree_code code = gimple_assign_rhs_code (stmt);
3475 tree def_for_init;
3476 tree init_def;
3477 tree *elts;
3478 int i;
3479 bool nested_in_vect_loop = false;
3480 tree init_value;
3481 REAL_VALUE_TYPE real_init_val = dconst0;
3482 int int_init_val = 0;
3483 gimple def_stmt = NULL;
3485 gcc_assert (vectype);
3486 nunits = TYPE_VECTOR_SUBPARTS (vectype);
3488 gcc_assert (POINTER_TYPE_P (scalar_type) || INTEGRAL_TYPE_P (scalar_type)
3489 || SCALAR_FLOAT_TYPE_P (scalar_type));
3491 if (nested_in_vect_loop_p (loop, stmt))
3492 nested_in_vect_loop = true;
3493 else
3494 gcc_assert (loop == (gimple_bb (stmt))->loop_father);
3496 /* In case of double reduction we only create a vector variable to be put
3497 in the reduction phi node. The actual statement creation is done in
3498 vect_create_epilog_for_reduction. */
3499 if (adjustment_def && nested_in_vect_loop
3500 && TREE_CODE (init_val) == SSA_NAME
3501 && (def_stmt = SSA_NAME_DEF_STMT (init_val))
3502 && gimple_code (def_stmt) == GIMPLE_PHI
3503 && flow_bb_inside_loop_p (loop, gimple_bb (def_stmt))
3504 && vinfo_for_stmt (def_stmt)
3505 && STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def_stmt))
3506 == vect_double_reduction_def)
3508 *adjustment_def = NULL;
3509 return vect_create_destination_var (init_val, vectype);
3512 if (TREE_CONSTANT (init_val))
3514 if (SCALAR_FLOAT_TYPE_P (scalar_type))
3515 init_value = build_real (scalar_type, TREE_REAL_CST (init_val));
3516 else
3517 init_value = build_int_cst (scalar_type, TREE_INT_CST_LOW (init_val));
3519 else
3520 init_value = init_val;
3522 switch (code)
3524 case WIDEN_SUM_EXPR:
3525 case DOT_PROD_EXPR:
3526 case PLUS_EXPR:
3527 case MINUS_EXPR:
3528 case BIT_IOR_EXPR:
3529 case BIT_XOR_EXPR:
3530 case MULT_EXPR:
3531 case BIT_AND_EXPR:
3532 /* ADJUSMENT_DEF is NULL when called from
3533 vect_create_epilog_for_reduction to vectorize double reduction. */
3534 if (adjustment_def)
3536 if (nested_in_vect_loop)
3537 *adjustment_def = vect_get_vec_def_for_operand (init_val, stmt,
3538 NULL);
3539 else
3540 *adjustment_def = init_val;
3543 if (code == MULT_EXPR)
3545 real_init_val = dconst1;
3546 int_init_val = 1;
3549 if (code == BIT_AND_EXPR)
3550 int_init_val = -1;
3552 if (SCALAR_FLOAT_TYPE_P (scalar_type))
3553 def_for_init = build_real (scalar_type, real_init_val);
3554 else
3555 def_for_init = build_int_cst (scalar_type, int_init_val);
3557 /* Create a vector of '0' or '1' except the first element. */
3558 elts = XALLOCAVEC (tree, nunits);
3559 for (i = nunits - 2; i >= 0; --i)
3560 elts[i + 1] = def_for_init;
3562 /* Option1: the first element is '0' or '1' as well. */
3563 if (adjustment_def)
3565 elts[0] = def_for_init;
3566 init_def = build_vector (vectype, elts);
3567 break;
3570 /* Option2: the first element is INIT_VAL. */
3571 elts[0] = init_val;
3572 if (TREE_CONSTANT (init_val))
3573 init_def = build_vector (vectype, elts);
3574 else
3576 vec<constructor_elt, va_gc> *v;
3577 vec_alloc (v, nunits);
3578 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init_val);
3579 for (i = 1; i < nunits; ++i)
3580 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, elts[i]);
3581 init_def = build_constructor (vectype, v);
3584 break;
3586 case MIN_EXPR:
3587 case MAX_EXPR:
3588 case COND_EXPR:
3589 if (adjustment_def)
3591 *adjustment_def = NULL_TREE;
3592 init_def = vect_get_vec_def_for_operand (init_val, stmt, NULL);
3593 break;
3596 init_def = build_vector_from_val (vectype, init_value);
3597 break;
3599 default:
3600 gcc_unreachable ();
3603 return init_def;
3607 /* Function vect_create_epilog_for_reduction
3609 Create code at the loop-epilog to finalize the result of a reduction
3610 computation.
3612 VECT_DEFS is list of vector of partial results, i.e., the lhs's of vector
3613 reduction statements.
3614 STMT is the scalar reduction stmt that is being vectorized.
3615 NCOPIES is > 1 in case the vectorization factor (VF) is bigger than the
3616 number of elements that we can fit in a vectype (nunits). In this case
3617 we have to generate more than one vector stmt - i.e - we need to "unroll"
3618 the vector stmt by a factor VF/nunits. For more details see documentation
3619 in vectorizable_operation.
3620 REDUC_CODE is the tree-code for the epilog reduction.
3621 REDUCTION_PHIS is a list of the phi-nodes that carry the reduction
3622 computation.
3623 REDUC_INDEX is the index of the operand in the right hand side of the
3624 statement that is defined by REDUCTION_PHI.
3625 DOUBLE_REDUC is TRUE if double reduction phi nodes should be handled.
3626 SLP_NODE is an SLP node containing a group of reduction statements. The
3627 first one in this group is STMT.
3629 This function:
3630 1. Creates the reduction def-use cycles: sets the arguments for
3631 REDUCTION_PHIS:
3632 The loop-entry argument is the vectorized initial-value of the reduction.
3633 The loop-latch argument is taken from VECT_DEFS - the vector of partial
3634 sums.
3635 2. "Reduces" each vector of partial results VECT_DEFS into a single result,
3636 by applying the operation specified by REDUC_CODE if available, or by
3637 other means (whole-vector shifts or a scalar loop).
3638 The function also creates a new phi node at the loop exit to preserve
3639 loop-closed form, as illustrated below.
3641 The flow at the entry to this function:
3643 loop:
3644 vec_def = phi <null, null> # REDUCTION_PHI
3645 VECT_DEF = vector_stmt # vectorized form of STMT
3646 s_loop = scalar_stmt # (scalar) STMT
3647 loop_exit:
3648 s_out0 = phi <s_loop> # (scalar) EXIT_PHI
3649 use <s_out0>
3650 use <s_out0>
3652 The above is transformed by this function into:
3654 loop:
3655 vec_def = phi <vec_init, VECT_DEF> # REDUCTION_PHI
3656 VECT_DEF = vector_stmt # vectorized form of STMT
3657 s_loop = scalar_stmt # (scalar) STMT
3658 loop_exit:
3659 s_out0 = phi <s_loop> # (scalar) EXIT_PHI
3660 v_out1 = phi <VECT_DEF> # NEW_EXIT_PHI
3661 v_out2 = reduce <v_out1>
3662 s_out3 = extract_field <v_out2, 0>
3663 s_out4 = adjust_result <s_out3>
3664 use <s_out4>
3665 use <s_out4>
3668 static void
3669 vect_create_epilog_for_reduction (vec<tree> vect_defs, gimple stmt,
3670 int ncopies, enum tree_code reduc_code,
3671 vec<gimple> reduction_phis,
3672 int reduc_index, bool double_reduc,
3673 slp_tree slp_node)
3675 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
3676 stmt_vec_info prev_phi_info;
3677 tree vectype;
3678 enum machine_mode mode;
3679 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
3680 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo), *outer_loop = NULL;
3681 basic_block exit_bb;
3682 tree scalar_dest;
3683 tree scalar_type;
3684 gimple new_phi = NULL, phi;
3685 gimple_stmt_iterator exit_gsi;
3686 tree vec_dest;
3687 tree new_temp = NULL_TREE, new_dest, new_name, new_scalar_dest;
3688 gimple epilog_stmt = NULL;
3689 enum tree_code code = gimple_assign_rhs_code (stmt);
3690 gimple exit_phi;
3691 tree bitsize, bitpos;
3692 tree adjustment_def = NULL;
3693 tree vec_initial_def = NULL;
3694 tree reduction_op, expr, def;
3695 tree orig_name, scalar_result;
3696 imm_use_iterator imm_iter, phi_imm_iter;
3697 use_operand_p use_p, phi_use_p;
3698 bool extract_scalar_result = false;
3699 gimple use_stmt, orig_stmt, reduction_phi = NULL;
3700 bool nested_in_vect_loop = false;
3701 vec<gimple> new_phis = vNULL;
3702 vec<gimple> inner_phis = vNULL;
3703 enum vect_def_type dt = vect_unknown_def_type;
3704 int j, i;
3705 vec<tree> scalar_results = vNULL;
3706 unsigned int group_size = 1, k, ratio;
3707 vec<tree> vec_initial_defs = vNULL;
3708 vec<gimple> phis;
3709 bool slp_reduc = false;
3710 tree new_phi_result;
3711 gimple inner_phi = NULL;
3713 if (slp_node)
3714 group_size = SLP_TREE_SCALAR_STMTS (slp_node).length ();
3716 if (nested_in_vect_loop_p (loop, stmt))
3718 outer_loop = loop;
3719 loop = loop->inner;
3720 nested_in_vect_loop = true;
3721 gcc_assert (!slp_node);
3724 switch (get_gimple_rhs_class (gimple_assign_rhs_code (stmt)))
3726 case GIMPLE_SINGLE_RHS:
3727 gcc_assert (TREE_OPERAND_LENGTH (gimple_assign_rhs1 (stmt))
3728 == ternary_op);
3729 reduction_op = TREE_OPERAND (gimple_assign_rhs1 (stmt), reduc_index);
3730 break;
3731 case GIMPLE_UNARY_RHS:
3732 reduction_op = gimple_assign_rhs1 (stmt);
3733 break;
3734 case GIMPLE_BINARY_RHS:
3735 reduction_op = reduc_index ?
3736 gimple_assign_rhs2 (stmt) : gimple_assign_rhs1 (stmt);
3737 break;
3738 case GIMPLE_TERNARY_RHS:
3739 reduction_op = gimple_op (stmt, reduc_index + 1);
3740 break;
3741 default:
3742 gcc_unreachable ();
3745 vectype = get_vectype_for_scalar_type (TREE_TYPE (reduction_op));
3746 gcc_assert (vectype);
3747 mode = TYPE_MODE (vectype);
3749 /* 1. Create the reduction def-use cycle:
3750 Set the arguments of REDUCTION_PHIS, i.e., transform
3752 loop:
3753 vec_def = phi <null, null> # REDUCTION_PHI
3754 VECT_DEF = vector_stmt # vectorized form of STMT
3757 into:
3759 loop:
3760 vec_def = phi <vec_init, VECT_DEF> # REDUCTION_PHI
3761 VECT_DEF = vector_stmt # vectorized form of STMT
3764 (in case of SLP, do it for all the phis). */
3766 /* Get the loop-entry arguments. */
3767 if (slp_node)
3768 vect_get_vec_defs (reduction_op, NULL_TREE, stmt, &vec_initial_defs,
3769 NULL, slp_node, reduc_index);
3770 else
3772 vec_initial_defs.create (1);
3773 /* For the case of reduction, vect_get_vec_def_for_operand returns
3774 the scalar def before the loop, that defines the initial value
3775 of the reduction variable. */
3776 vec_initial_def = vect_get_vec_def_for_operand (reduction_op, stmt,
3777 &adjustment_def);
3778 vec_initial_defs.quick_push (vec_initial_def);
3781 /* Set phi nodes arguments. */
3782 FOR_EACH_VEC_ELT (reduction_phis, i, phi)
3784 tree vec_init_def = vec_initial_defs[i];
3785 tree def = vect_defs[i];
3786 for (j = 0; j < ncopies; j++)
3788 /* Set the loop-entry arg of the reduction-phi. */
3789 add_phi_arg (phi, vec_init_def, loop_preheader_edge (loop),
3790 UNKNOWN_LOCATION);
3792 /* Set the loop-latch arg for the reduction-phi. */
3793 if (j > 0)
3794 def = vect_get_vec_def_for_stmt_copy (vect_unknown_def_type, def);
3796 add_phi_arg (phi, def, loop_latch_edge (loop), UNKNOWN_LOCATION);
3798 if (dump_enabled_p ())
3800 dump_printf_loc (MSG_NOTE, vect_location,
3801 "transform reduction: created def-use cycle: ");
3802 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, phi, 0);
3803 dump_printf (MSG_NOTE, "\n");
3804 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, SSA_NAME_DEF_STMT (def), 0);
3807 phi = STMT_VINFO_RELATED_STMT (vinfo_for_stmt (phi));
3811 vec_initial_defs.release ();
3813 /* 2. Create epilog code.
3814 The reduction epilog code operates across the elements of the vector
3815 of partial results computed by the vectorized loop.
3816 The reduction epilog code consists of:
3818 step 1: compute the scalar result in a vector (v_out2)
3819 step 2: extract the scalar result (s_out3) from the vector (v_out2)
3820 step 3: adjust the scalar result (s_out3) if needed.
3822 Step 1 can be accomplished using one the following three schemes:
3823 (scheme 1) using reduc_code, if available.
3824 (scheme 2) using whole-vector shifts, if available.
3825 (scheme 3) using a scalar loop. In this case steps 1+2 above are
3826 combined.
3828 The overall epilog code looks like this:
3830 s_out0 = phi <s_loop> # original EXIT_PHI
3831 v_out1 = phi <VECT_DEF> # NEW_EXIT_PHI
3832 v_out2 = reduce <v_out1> # step 1
3833 s_out3 = extract_field <v_out2, 0> # step 2
3834 s_out4 = adjust_result <s_out3> # step 3
3836 (step 3 is optional, and steps 1 and 2 may be combined).
3837 Lastly, the uses of s_out0 are replaced by s_out4. */
3840 /* 2.1 Create new loop-exit-phis to preserve loop-closed form:
3841 v_out1 = phi <VECT_DEF>
3842 Store them in NEW_PHIS. */
3844 exit_bb = single_exit (loop)->dest;
3845 prev_phi_info = NULL;
3846 new_phis.create (vect_defs.length ());
3847 FOR_EACH_VEC_ELT (vect_defs, i, def)
3849 for (j = 0; j < ncopies; j++)
3851 tree new_def = copy_ssa_name (def, NULL);
3852 phi = create_phi_node (new_def, exit_bb);
3853 set_vinfo_for_stmt (phi, new_stmt_vec_info (phi, loop_vinfo, NULL));
3854 if (j == 0)
3855 new_phis.quick_push (phi);
3856 else
3858 def = vect_get_vec_def_for_stmt_copy (dt, def);
3859 STMT_VINFO_RELATED_STMT (prev_phi_info) = phi;
3862 SET_PHI_ARG_DEF (phi, single_exit (loop)->dest_idx, def);
3863 prev_phi_info = vinfo_for_stmt (phi);
3867 /* The epilogue is created for the outer-loop, i.e., for the loop being
3868 vectorized. Create exit phis for the outer loop. */
3869 if (double_reduc)
3871 loop = outer_loop;
3872 exit_bb = single_exit (loop)->dest;
3873 inner_phis.create (vect_defs.length ());
3874 FOR_EACH_VEC_ELT (new_phis, i, phi)
3876 tree new_result = copy_ssa_name (PHI_RESULT (phi), NULL);
3877 gimple outer_phi = create_phi_node (new_result, exit_bb);
3878 SET_PHI_ARG_DEF (outer_phi, single_exit (loop)->dest_idx,
3879 PHI_RESULT (phi));
3880 set_vinfo_for_stmt (outer_phi, new_stmt_vec_info (outer_phi,
3881 loop_vinfo, NULL));
3882 inner_phis.quick_push (phi);
3883 new_phis[i] = outer_phi;
3884 prev_phi_info = vinfo_for_stmt (outer_phi);
3885 while (STMT_VINFO_RELATED_STMT (vinfo_for_stmt (phi)))
3887 phi = STMT_VINFO_RELATED_STMT (vinfo_for_stmt (phi));
3888 new_result = copy_ssa_name (PHI_RESULT (phi), NULL);
3889 outer_phi = create_phi_node (new_result, exit_bb);
3890 SET_PHI_ARG_DEF (outer_phi, single_exit (loop)->dest_idx,
3891 PHI_RESULT (phi));
3892 set_vinfo_for_stmt (outer_phi, new_stmt_vec_info (outer_phi,
3893 loop_vinfo, NULL));
3894 STMT_VINFO_RELATED_STMT (prev_phi_info) = outer_phi;
3895 prev_phi_info = vinfo_for_stmt (outer_phi);
3900 exit_gsi = gsi_after_labels (exit_bb);
3902 /* 2.2 Get the relevant tree-code to use in the epilog for schemes 2,3
3903 (i.e. when reduc_code is not available) and in the final adjustment
3904 code (if needed). Also get the original scalar reduction variable as
3905 defined in the loop. In case STMT is a "pattern-stmt" (i.e. - it
3906 represents a reduction pattern), the tree-code and scalar-def are
3907 taken from the original stmt that the pattern-stmt (STMT) replaces.
3908 Otherwise (it is a regular reduction) - the tree-code and scalar-def
3909 are taken from STMT. */
3911 orig_stmt = STMT_VINFO_RELATED_STMT (stmt_info);
3912 if (!orig_stmt)
3914 /* Regular reduction */
3915 orig_stmt = stmt;
3917 else
3919 /* Reduction pattern */
3920 stmt_vec_info stmt_vinfo = vinfo_for_stmt (orig_stmt);
3921 gcc_assert (STMT_VINFO_IN_PATTERN_P (stmt_vinfo));
3922 gcc_assert (STMT_VINFO_RELATED_STMT (stmt_vinfo) == stmt);
3925 code = gimple_assign_rhs_code (orig_stmt);
3926 /* For MINUS_EXPR the initial vector is [init_val,0,...,0], therefore,
3927 partial results are added and not subtracted. */
3928 if (code == MINUS_EXPR)
3929 code = PLUS_EXPR;
3931 scalar_dest = gimple_assign_lhs (orig_stmt);
3932 scalar_type = TREE_TYPE (scalar_dest);
3933 scalar_results.create (group_size);
3934 new_scalar_dest = vect_create_destination_var (scalar_dest, NULL);
3935 bitsize = TYPE_SIZE (scalar_type);
3937 /* In case this is a reduction in an inner-loop while vectorizing an outer
3938 loop - we don't need to extract a single scalar result at the end of the
3939 inner-loop (unless it is double reduction, i.e., the use of reduction is
3940 outside the outer-loop). The final vector of partial results will be used
3941 in the vectorized outer-loop, or reduced to a scalar result at the end of
3942 the outer-loop. */
3943 if (nested_in_vect_loop && !double_reduc)
3944 goto vect_finalize_reduction;
3946 /* SLP reduction without reduction chain, e.g.,
3947 # a1 = phi <a2, a0>
3948 # b1 = phi <b2, b0>
3949 a2 = operation (a1)
3950 b2 = operation (b1) */
3951 slp_reduc = (slp_node && !GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)));
3953 /* In case of reduction chain, e.g.,
3954 # a1 = phi <a3, a0>
3955 a2 = operation (a1)
3956 a3 = operation (a2),
3958 we may end up with more than one vector result. Here we reduce them to
3959 one vector. */
3960 if (GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)))
3962 tree first_vect = PHI_RESULT (new_phis[0]);
3963 tree tmp;
3964 gimple new_vec_stmt = NULL;
3966 vec_dest = vect_create_destination_var (scalar_dest, vectype);
3967 for (k = 1; k < new_phis.length (); k++)
3969 gimple next_phi = new_phis[k];
3970 tree second_vect = PHI_RESULT (next_phi);
3972 tmp = build2 (code, vectype, first_vect, second_vect);
3973 new_vec_stmt = gimple_build_assign (vec_dest, tmp);
3974 first_vect = make_ssa_name (vec_dest, new_vec_stmt);
3975 gimple_assign_set_lhs (new_vec_stmt, first_vect);
3976 gsi_insert_before (&exit_gsi, new_vec_stmt, GSI_SAME_STMT);
3979 new_phi_result = first_vect;
3980 if (new_vec_stmt)
3982 new_phis.truncate (0);
3983 new_phis.safe_push (new_vec_stmt);
3986 else
3987 new_phi_result = PHI_RESULT (new_phis[0]);
3989 /* 2.3 Create the reduction code, using one of the three schemes described
3990 above. In SLP we simply need to extract all the elements from the
3991 vector (without reducing them), so we use scalar shifts. */
3992 if (reduc_code != ERROR_MARK && !slp_reduc)
3994 tree tmp;
3996 /*** Case 1: Create:
3997 v_out2 = reduc_expr <v_out1> */
3999 if (dump_enabled_p ())
4000 dump_printf_loc (MSG_NOTE, vect_location,
4001 "Reduce using direct vector reduction.");
4003 vec_dest = vect_create_destination_var (scalar_dest, vectype);
4004 tmp = build1 (reduc_code, vectype, new_phi_result);
4005 epilog_stmt = gimple_build_assign (vec_dest, tmp);
4006 new_temp = make_ssa_name (vec_dest, epilog_stmt);
4007 gimple_assign_set_lhs (epilog_stmt, new_temp);
4008 gsi_insert_before (&exit_gsi, epilog_stmt, GSI_SAME_STMT);
4010 extract_scalar_result = true;
4012 else
4014 enum tree_code shift_code = ERROR_MARK;
4015 bool have_whole_vector_shift = true;
4016 int bit_offset;
4017 int element_bitsize = tree_low_cst (bitsize, 1);
4018 int vec_size_in_bits = tree_low_cst (TYPE_SIZE (vectype), 1);
4019 tree vec_temp;
4021 if (optab_handler (vec_shr_optab, mode) != CODE_FOR_nothing)
4022 shift_code = VEC_RSHIFT_EXPR;
4023 else
4024 have_whole_vector_shift = false;
4026 /* Regardless of whether we have a whole vector shift, if we're
4027 emulating the operation via tree-vect-generic, we don't want
4028 to use it. Only the first round of the reduction is likely
4029 to still be profitable via emulation. */
4030 /* ??? It might be better to emit a reduction tree code here, so that
4031 tree-vect-generic can expand the first round via bit tricks. */
4032 if (!VECTOR_MODE_P (mode))
4033 have_whole_vector_shift = false;
4034 else
4036 optab optab = optab_for_tree_code (code, vectype, optab_default);
4037 if (optab_handler (optab, mode) == CODE_FOR_nothing)
4038 have_whole_vector_shift = false;
4041 if (have_whole_vector_shift && !slp_reduc)
4043 /*** Case 2: Create:
4044 for (offset = VS/2; offset >= element_size; offset/=2)
4046 Create: va' = vec_shift <va, offset>
4047 Create: va = vop <va, va'>
4048 } */
4050 if (dump_enabled_p ())
4051 dump_printf_loc (MSG_NOTE, vect_location,
4052 "Reduce using vector shifts");
4054 vec_dest = vect_create_destination_var (scalar_dest, vectype);
4055 new_temp = new_phi_result;
4056 for (bit_offset = vec_size_in_bits/2;
4057 bit_offset >= element_bitsize;
4058 bit_offset /= 2)
4060 tree bitpos = size_int (bit_offset);
4062 epilog_stmt = gimple_build_assign_with_ops (shift_code,
4063 vec_dest, new_temp, bitpos);
4064 new_name = make_ssa_name (vec_dest, epilog_stmt);
4065 gimple_assign_set_lhs (epilog_stmt, new_name);
4066 gsi_insert_before (&exit_gsi, epilog_stmt, GSI_SAME_STMT);
4068 epilog_stmt = gimple_build_assign_with_ops (code, vec_dest,
4069 new_name, new_temp);
4070 new_temp = make_ssa_name (vec_dest, epilog_stmt);
4071 gimple_assign_set_lhs (epilog_stmt, new_temp);
4072 gsi_insert_before (&exit_gsi, epilog_stmt, GSI_SAME_STMT);
4075 extract_scalar_result = true;
4077 else
4079 tree rhs;
4081 /*** Case 3: Create:
4082 s = extract_field <v_out2, 0>
4083 for (offset = element_size;
4084 offset < vector_size;
4085 offset += element_size;)
4087 Create: s' = extract_field <v_out2, offset>
4088 Create: s = op <s, s'> // For non SLP cases
4089 } */
4091 if (dump_enabled_p ())
4092 dump_printf_loc (MSG_NOTE, vect_location,
4093 "Reduce using scalar code. ");
4095 vec_size_in_bits = tree_low_cst (TYPE_SIZE (vectype), 1);
4096 FOR_EACH_VEC_ELT (new_phis, i, new_phi)
4098 if (gimple_code (new_phi) == GIMPLE_PHI)
4099 vec_temp = PHI_RESULT (new_phi);
4100 else
4101 vec_temp = gimple_assign_lhs (new_phi);
4102 rhs = build3 (BIT_FIELD_REF, scalar_type, vec_temp, bitsize,
4103 bitsize_zero_node);
4104 epilog_stmt = gimple_build_assign (new_scalar_dest, rhs);
4105 new_temp = make_ssa_name (new_scalar_dest, epilog_stmt);
4106 gimple_assign_set_lhs (epilog_stmt, new_temp);
4107 gsi_insert_before (&exit_gsi, epilog_stmt, GSI_SAME_STMT);
4109 /* In SLP we don't need to apply reduction operation, so we just
4110 collect s' values in SCALAR_RESULTS. */
4111 if (slp_reduc)
4112 scalar_results.safe_push (new_temp);
4114 for (bit_offset = element_bitsize;
4115 bit_offset < vec_size_in_bits;
4116 bit_offset += element_bitsize)
4118 tree bitpos = bitsize_int (bit_offset);
4119 tree rhs = build3 (BIT_FIELD_REF, scalar_type, vec_temp,
4120 bitsize, bitpos);
4122 epilog_stmt = gimple_build_assign (new_scalar_dest, rhs);
4123 new_name = make_ssa_name (new_scalar_dest, epilog_stmt);
4124 gimple_assign_set_lhs (epilog_stmt, new_name);
4125 gsi_insert_before (&exit_gsi, epilog_stmt, GSI_SAME_STMT);
4127 if (slp_reduc)
4129 /* In SLP we don't need to apply reduction operation, so
4130 we just collect s' values in SCALAR_RESULTS. */
4131 new_temp = new_name;
4132 scalar_results.safe_push (new_name);
4134 else
4136 epilog_stmt = gimple_build_assign_with_ops (code,
4137 new_scalar_dest, new_name, new_temp);
4138 new_temp = make_ssa_name (new_scalar_dest, epilog_stmt);
4139 gimple_assign_set_lhs (epilog_stmt, new_temp);
4140 gsi_insert_before (&exit_gsi, epilog_stmt, GSI_SAME_STMT);
4145 /* The only case where we need to reduce scalar results in SLP, is
4146 unrolling. If the size of SCALAR_RESULTS is greater than
4147 GROUP_SIZE, we reduce them combining elements modulo
4148 GROUP_SIZE. */
4149 if (slp_reduc)
4151 tree res, first_res, new_res;
4152 gimple new_stmt;
4154 /* Reduce multiple scalar results in case of SLP unrolling. */
4155 for (j = group_size; scalar_results.iterate (j, &res);
4156 j++)
4158 first_res = scalar_results[j % group_size];
4159 new_stmt = gimple_build_assign_with_ops (code,
4160 new_scalar_dest, first_res, res);
4161 new_res = make_ssa_name (new_scalar_dest, new_stmt);
4162 gimple_assign_set_lhs (new_stmt, new_res);
4163 gsi_insert_before (&exit_gsi, new_stmt, GSI_SAME_STMT);
4164 scalar_results[j % group_size] = new_res;
4167 else
4168 /* Not SLP - we have one scalar to keep in SCALAR_RESULTS. */
4169 scalar_results.safe_push (new_temp);
4171 extract_scalar_result = false;
4175 /* 2.4 Extract the final scalar result. Create:
4176 s_out3 = extract_field <v_out2, bitpos> */
4178 if (extract_scalar_result)
4180 tree rhs;
4182 if (dump_enabled_p ())
4183 dump_printf_loc (MSG_NOTE, vect_location,
4184 "extract scalar result");
4186 if (BYTES_BIG_ENDIAN)
4187 bitpos = size_binop (MULT_EXPR,
4188 bitsize_int (TYPE_VECTOR_SUBPARTS (vectype) - 1),
4189 TYPE_SIZE (scalar_type));
4190 else
4191 bitpos = bitsize_zero_node;
4193 rhs = build3 (BIT_FIELD_REF, scalar_type, new_temp, bitsize, bitpos);
4194 epilog_stmt = gimple_build_assign (new_scalar_dest, rhs);
4195 new_temp = make_ssa_name (new_scalar_dest, epilog_stmt);
4196 gimple_assign_set_lhs (epilog_stmt, new_temp);
4197 gsi_insert_before (&exit_gsi, epilog_stmt, GSI_SAME_STMT);
4198 scalar_results.safe_push (new_temp);
4201 vect_finalize_reduction:
4203 if (double_reduc)
4204 loop = loop->inner;
4206 /* 2.5 Adjust the final result by the initial value of the reduction
4207 variable. (When such adjustment is not needed, then
4208 'adjustment_def' is zero). For example, if code is PLUS we create:
4209 new_temp = loop_exit_def + adjustment_def */
4211 if (adjustment_def)
4213 gcc_assert (!slp_reduc);
4214 if (nested_in_vect_loop)
4216 new_phi = new_phis[0];
4217 gcc_assert (TREE_CODE (TREE_TYPE (adjustment_def)) == VECTOR_TYPE);
4218 expr = build2 (code, vectype, PHI_RESULT (new_phi), adjustment_def);
4219 new_dest = vect_create_destination_var (scalar_dest, vectype);
4221 else
4223 new_temp = scalar_results[0];
4224 gcc_assert (TREE_CODE (TREE_TYPE (adjustment_def)) != VECTOR_TYPE);
4225 expr = build2 (code, scalar_type, new_temp, adjustment_def);
4226 new_dest = vect_create_destination_var (scalar_dest, scalar_type);
4229 epilog_stmt = gimple_build_assign (new_dest, expr);
4230 new_temp = make_ssa_name (new_dest, epilog_stmt);
4231 gimple_assign_set_lhs (epilog_stmt, new_temp);
4232 SSA_NAME_DEF_STMT (new_temp) = epilog_stmt;
4233 gsi_insert_before (&exit_gsi, epilog_stmt, GSI_SAME_STMT);
4234 if (nested_in_vect_loop)
4236 set_vinfo_for_stmt (epilog_stmt,
4237 new_stmt_vec_info (epilog_stmt, loop_vinfo,
4238 NULL));
4239 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (epilog_stmt)) =
4240 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (new_phi));
4242 if (!double_reduc)
4243 scalar_results.quick_push (new_temp);
4244 else
4245 scalar_results[0] = new_temp;
4247 else
4248 scalar_results[0] = new_temp;
4250 new_phis[0] = epilog_stmt;
4253 /* 2.6 Handle the loop-exit phis. Replace the uses of scalar loop-exit
4254 phis with new adjusted scalar results, i.e., replace use <s_out0>
4255 with use <s_out4>.
4257 Transform:
4258 loop_exit:
4259 s_out0 = phi <s_loop> # (scalar) EXIT_PHI
4260 v_out1 = phi <VECT_DEF> # NEW_EXIT_PHI
4261 v_out2 = reduce <v_out1>
4262 s_out3 = extract_field <v_out2, 0>
4263 s_out4 = adjust_result <s_out3>
4264 use <s_out0>
4265 use <s_out0>
4267 into:
4269 loop_exit:
4270 s_out0 = phi <s_loop> # (scalar) EXIT_PHI
4271 v_out1 = phi <VECT_DEF> # NEW_EXIT_PHI
4272 v_out2 = reduce <v_out1>
4273 s_out3 = extract_field <v_out2, 0>
4274 s_out4 = adjust_result <s_out3>
4275 use <s_out4>
4276 use <s_out4> */
4279 /* In SLP reduction chain we reduce vector results into one vector if
4280 necessary, hence we set here GROUP_SIZE to 1. SCALAR_DEST is the LHS of
4281 the last stmt in the reduction chain, since we are looking for the loop
4282 exit phi node. */
4283 if (GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)))
4285 scalar_dest = gimple_assign_lhs (
4286 SLP_TREE_SCALAR_STMTS (slp_node)[group_size - 1]);
4287 group_size = 1;
4290 /* In SLP we may have several statements in NEW_PHIS and REDUCTION_PHIS (in
4291 case that GROUP_SIZE is greater than vectorization factor). Therefore, we
4292 need to match SCALAR_RESULTS with corresponding statements. The first
4293 (GROUP_SIZE / number of new vector stmts) scalar results correspond to
4294 the first vector stmt, etc.
4295 (RATIO is equal to (GROUP_SIZE / number of new vector stmts)). */
4296 if (group_size > new_phis.length ())
4298 ratio = group_size / new_phis.length ();
4299 gcc_assert (!(group_size % new_phis.length ()));
4301 else
4302 ratio = 1;
4304 for (k = 0; k < group_size; k++)
4306 if (k % ratio == 0)
4308 epilog_stmt = new_phis[k / ratio];
4309 reduction_phi = reduction_phis[k / ratio];
4310 if (double_reduc)
4311 inner_phi = inner_phis[k / ratio];
4314 if (slp_reduc)
4316 gimple current_stmt = SLP_TREE_SCALAR_STMTS (slp_node)[k];
4318 orig_stmt = STMT_VINFO_RELATED_STMT (vinfo_for_stmt (current_stmt));
4319 /* SLP statements can't participate in patterns. */
4320 gcc_assert (!orig_stmt);
4321 scalar_dest = gimple_assign_lhs (current_stmt);
4324 phis.create (3);
4325 /* Find the loop-closed-use at the loop exit of the original scalar
4326 result. (The reduction result is expected to have two immediate uses -
4327 one at the latch block, and one at the loop exit). */
4328 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, scalar_dest)
4329 if (!flow_bb_inside_loop_p (loop, gimple_bb (USE_STMT (use_p))))
4330 phis.safe_push (USE_STMT (use_p));
4332 /* We expect to have found an exit_phi because of loop-closed-ssa
4333 form. */
4334 gcc_assert (!phis.is_empty ());
4336 FOR_EACH_VEC_ELT (phis, i, exit_phi)
4338 if (outer_loop)
4340 stmt_vec_info exit_phi_vinfo = vinfo_for_stmt (exit_phi);
4341 gimple vect_phi;
4343 /* FORNOW. Currently not supporting the case that an inner-loop
4344 reduction is not used in the outer-loop (but only outside the
4345 outer-loop), unless it is double reduction. */
4346 gcc_assert ((STMT_VINFO_RELEVANT_P (exit_phi_vinfo)
4347 && !STMT_VINFO_LIVE_P (exit_phi_vinfo))
4348 || double_reduc);
4350 STMT_VINFO_VEC_STMT (exit_phi_vinfo) = epilog_stmt;
4351 if (!double_reduc
4352 || STMT_VINFO_DEF_TYPE (exit_phi_vinfo)
4353 != vect_double_reduction_def)
4354 continue;
4356 /* Handle double reduction:
4358 stmt1: s1 = phi <s0, s2> - double reduction phi (outer loop)
4359 stmt2: s3 = phi <s1, s4> - (regular) reduc phi (inner loop)
4360 stmt3: s4 = use (s3) - (regular) reduc stmt (inner loop)
4361 stmt4: s2 = phi <s4> - double reduction stmt (outer loop)
4363 At that point the regular reduction (stmt2 and stmt3) is
4364 already vectorized, as well as the exit phi node, stmt4.
4365 Here we vectorize the phi node of double reduction, stmt1, and
4366 update all relevant statements. */
4368 /* Go through all the uses of s2 to find double reduction phi
4369 node, i.e., stmt1 above. */
4370 orig_name = PHI_RESULT (exit_phi);
4371 FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, orig_name)
4373 stmt_vec_info use_stmt_vinfo;
4374 stmt_vec_info new_phi_vinfo;
4375 tree vect_phi_init, preheader_arg, vect_phi_res, init_def;
4376 basic_block bb = gimple_bb (use_stmt);
4377 gimple use;
4379 /* Check that USE_STMT is really double reduction phi
4380 node. */
4381 if (gimple_code (use_stmt) != GIMPLE_PHI
4382 || gimple_phi_num_args (use_stmt) != 2
4383 || bb->loop_father != outer_loop)
4384 continue;
4385 use_stmt_vinfo = vinfo_for_stmt (use_stmt);
4386 if (!use_stmt_vinfo
4387 || STMT_VINFO_DEF_TYPE (use_stmt_vinfo)
4388 != vect_double_reduction_def)
4389 continue;
4391 /* Create vector phi node for double reduction:
4392 vs1 = phi <vs0, vs2>
4393 vs1 was created previously in this function by a call to
4394 vect_get_vec_def_for_operand and is stored in
4395 vec_initial_def;
4396 vs2 is defined by INNER_PHI, the vectorized EXIT_PHI;
4397 vs0 is created here. */
4399 /* Create vector phi node. */
4400 vect_phi = create_phi_node (vec_initial_def, bb);
4401 new_phi_vinfo = new_stmt_vec_info (vect_phi,
4402 loop_vec_info_for_loop (outer_loop), NULL);
4403 set_vinfo_for_stmt (vect_phi, new_phi_vinfo);
4405 /* Create vs0 - initial def of the double reduction phi. */
4406 preheader_arg = PHI_ARG_DEF_FROM_EDGE (use_stmt,
4407 loop_preheader_edge (outer_loop));
4408 init_def = get_initial_def_for_reduction (stmt,
4409 preheader_arg, NULL);
4410 vect_phi_init = vect_init_vector (use_stmt, init_def,
4411 vectype, NULL);
4413 /* Update phi node arguments with vs0 and vs2. */
4414 add_phi_arg (vect_phi, vect_phi_init,
4415 loop_preheader_edge (outer_loop),
4416 UNKNOWN_LOCATION);
4417 add_phi_arg (vect_phi, PHI_RESULT (inner_phi),
4418 loop_latch_edge (outer_loop), UNKNOWN_LOCATION);
4419 if (dump_enabled_p ())
4421 dump_printf_loc (MSG_NOTE, vect_location,
4422 "created double reduction phi node: ");
4423 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, vect_phi, 0);
4426 vect_phi_res = PHI_RESULT (vect_phi);
4428 /* Replace the use, i.e., set the correct vs1 in the regular
4429 reduction phi node. FORNOW, NCOPIES is always 1, so the
4430 loop is redundant. */
4431 use = reduction_phi;
4432 for (j = 0; j < ncopies; j++)
4434 edge pr_edge = loop_preheader_edge (loop);
4435 SET_PHI_ARG_DEF (use, pr_edge->dest_idx, vect_phi_res);
4436 use = STMT_VINFO_RELATED_STMT (vinfo_for_stmt (use));
4442 phis.release ();
4443 if (nested_in_vect_loop)
4445 if (double_reduc)
4446 loop = outer_loop;
4447 else
4448 continue;
4451 phis.create (3);
4452 /* Find the loop-closed-use at the loop exit of the original scalar
4453 result. (The reduction result is expected to have two immediate uses,
4454 one at the latch block, and one at the loop exit). For double
4455 reductions we are looking for exit phis of the outer loop. */
4456 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, scalar_dest)
4458 if (!flow_bb_inside_loop_p (loop, gimple_bb (USE_STMT (use_p))))
4459 phis.safe_push (USE_STMT (use_p));
4460 else
4462 if (double_reduc && gimple_code (USE_STMT (use_p)) == GIMPLE_PHI)
4464 tree phi_res = PHI_RESULT (USE_STMT (use_p));
4466 FOR_EACH_IMM_USE_FAST (phi_use_p, phi_imm_iter, phi_res)
4468 if (!flow_bb_inside_loop_p (loop,
4469 gimple_bb (USE_STMT (phi_use_p))))
4470 phis.safe_push (USE_STMT (phi_use_p));
4476 FOR_EACH_VEC_ELT (phis, i, exit_phi)
4478 /* Replace the uses: */
4479 orig_name = PHI_RESULT (exit_phi);
4480 scalar_result = scalar_results[k];
4481 FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, orig_name)
4482 FOR_EACH_IMM_USE_ON_STMT (use_p, imm_iter)
4483 SET_USE (use_p, scalar_result);
4486 phis.release ();
4489 scalar_results.release ();
4490 new_phis.release ();
4494 /* Function vectorizable_reduction.
4496 Check if STMT performs a reduction operation that can be vectorized.
4497 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
4498 stmt to replace it, put it in VEC_STMT, and insert it at GSI.
4499 Return FALSE if not a vectorizable STMT, TRUE otherwise.
4501 This function also handles reduction idioms (patterns) that have been
4502 recognized in advance during vect_pattern_recog. In this case, STMT may be
4503 of this form:
4504 X = pattern_expr (arg0, arg1, ..., X)
4505 and it's STMT_VINFO_RELATED_STMT points to the last stmt in the original
4506 sequence that had been detected and replaced by the pattern-stmt (STMT).
4508 In some cases of reduction patterns, the type of the reduction variable X is
4509 different than the type of the other arguments of STMT.
4510 In such cases, the vectype that is used when transforming STMT into a vector
4511 stmt is different than the vectype that is used to determine the
4512 vectorization factor, because it consists of a different number of elements
4513 than the actual number of elements that are being operated upon in parallel.
4515 For example, consider an accumulation of shorts into an int accumulator.
4516 On some targets it's possible to vectorize this pattern operating on 8
4517 shorts at a time (hence, the vectype for purposes of determining the
4518 vectorization factor should be V8HI); on the other hand, the vectype that
4519 is used to create the vector form is actually V4SI (the type of the result).
4521 Upon entry to this function, STMT_VINFO_VECTYPE records the vectype that
4522 indicates what is the actual level of parallelism (V8HI in the example), so
4523 that the right vectorization factor would be derived. This vectype
4524 corresponds to the type of arguments to the reduction stmt, and should *NOT*
4525 be used to create the vectorized stmt. The right vectype for the vectorized
4526 stmt is obtained from the type of the result X:
4527 get_vectype_for_scalar_type (TREE_TYPE (X))
4529 This means that, contrary to "regular" reductions (or "regular" stmts in
4530 general), the following equation:
4531 STMT_VINFO_VECTYPE == get_vectype_for_scalar_type (TREE_TYPE (X))
4532 does *NOT* necessarily hold for reduction patterns. */
4534 bool
4535 vectorizable_reduction (gimple stmt, gimple_stmt_iterator *gsi,
4536 gimple *vec_stmt, slp_tree slp_node)
4538 tree vec_dest;
4539 tree scalar_dest;
4540 tree loop_vec_def0 = NULL_TREE, loop_vec_def1 = NULL_TREE;
4541 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
4542 tree vectype_out = STMT_VINFO_VECTYPE (stmt_info);
4543 tree vectype_in = NULL_TREE;
4544 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
4545 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
4546 enum tree_code code, orig_code, epilog_reduc_code;
4547 enum machine_mode vec_mode;
4548 int op_type;
4549 optab optab, reduc_optab;
4550 tree new_temp = NULL_TREE;
4551 tree def;
4552 gimple def_stmt;
4553 enum vect_def_type dt;
4554 gimple new_phi = NULL;
4555 tree scalar_type;
4556 bool is_simple_use;
4557 gimple orig_stmt;
4558 stmt_vec_info orig_stmt_info;
4559 tree expr = NULL_TREE;
4560 int i;
4561 int ncopies;
4562 int epilog_copies;
4563 stmt_vec_info prev_stmt_info, prev_phi_info;
4564 bool single_defuse_cycle = false;
4565 tree reduc_def = NULL_TREE;
4566 gimple new_stmt = NULL;
4567 int j;
4568 tree ops[3];
4569 bool nested_cycle = false, found_nested_cycle_def = false;
4570 gimple reduc_def_stmt = NULL;
4571 /* The default is that the reduction variable is the last in statement. */
4572 int reduc_index = 2;
4573 bool double_reduc = false, dummy;
4574 basic_block def_bb;
4575 struct loop * def_stmt_loop, *outer_loop = NULL;
4576 tree def_arg;
4577 gimple def_arg_stmt;
4578 vec<tree> vec_oprnds0 = vNULL;
4579 vec<tree> vec_oprnds1 = vNULL;
4580 vec<tree> vect_defs = vNULL;
4581 vec<gimple> phis = vNULL;
4582 int vec_num;
4583 tree def0, def1, tem, op0, op1 = NULL_TREE;
4585 /* In case of reduction chain we switch to the first stmt in the chain, but
4586 we don't update STMT_INFO, since only the last stmt is marked as reduction
4587 and has reduction properties. */
4588 if (GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)))
4589 stmt = GROUP_FIRST_ELEMENT (stmt_info);
4591 if (nested_in_vect_loop_p (loop, stmt))
4593 outer_loop = loop;
4594 loop = loop->inner;
4595 nested_cycle = true;
4598 /* 1. Is vectorizable reduction? */
4599 /* Not supportable if the reduction variable is used in the loop, unless
4600 it's a reduction chain. */
4601 if (STMT_VINFO_RELEVANT (stmt_info) > vect_used_in_outer
4602 && !GROUP_FIRST_ELEMENT (stmt_info))
4603 return false;
4605 /* Reductions that are not used even in an enclosing outer-loop,
4606 are expected to be "live" (used out of the loop). */
4607 if (STMT_VINFO_RELEVANT (stmt_info) == vect_unused_in_scope
4608 && !STMT_VINFO_LIVE_P (stmt_info))
4609 return false;
4611 /* Make sure it was already recognized as a reduction computation. */
4612 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_reduction_def
4613 && STMT_VINFO_DEF_TYPE (stmt_info) != vect_nested_cycle)
4614 return false;
4616 /* 2. Has this been recognized as a reduction pattern?
4618 Check if STMT represents a pattern that has been recognized
4619 in earlier analysis stages. For stmts that represent a pattern,
4620 the STMT_VINFO_RELATED_STMT field records the last stmt in
4621 the original sequence that constitutes the pattern. */
4623 orig_stmt = STMT_VINFO_RELATED_STMT (stmt_info);
4624 if (orig_stmt)
4626 orig_stmt_info = vinfo_for_stmt (orig_stmt);
4627 gcc_assert (STMT_VINFO_RELATED_STMT (orig_stmt_info) == stmt);
4628 gcc_assert (STMT_VINFO_IN_PATTERN_P (orig_stmt_info));
4629 gcc_assert (!STMT_VINFO_IN_PATTERN_P (stmt_info));
4632 /* 3. Check the operands of the operation. The first operands are defined
4633 inside the loop body. The last operand is the reduction variable,
4634 which is defined by the loop-header-phi. */
4636 gcc_assert (is_gimple_assign (stmt));
4638 /* Flatten RHS. */
4639 switch (get_gimple_rhs_class (gimple_assign_rhs_code (stmt)))
4641 case GIMPLE_SINGLE_RHS:
4642 op_type = TREE_OPERAND_LENGTH (gimple_assign_rhs1 (stmt));
4643 if (op_type == ternary_op)
4645 tree rhs = gimple_assign_rhs1 (stmt);
4646 ops[0] = TREE_OPERAND (rhs, 0);
4647 ops[1] = TREE_OPERAND (rhs, 1);
4648 ops[2] = TREE_OPERAND (rhs, 2);
4649 code = TREE_CODE (rhs);
4651 else
4652 return false;
4653 break;
4655 case GIMPLE_BINARY_RHS:
4656 code = gimple_assign_rhs_code (stmt);
4657 op_type = TREE_CODE_LENGTH (code);
4658 gcc_assert (op_type == binary_op);
4659 ops[0] = gimple_assign_rhs1 (stmt);
4660 ops[1] = gimple_assign_rhs2 (stmt);
4661 break;
4663 case GIMPLE_TERNARY_RHS:
4664 code = gimple_assign_rhs_code (stmt);
4665 op_type = TREE_CODE_LENGTH (code);
4666 gcc_assert (op_type == ternary_op);
4667 ops[0] = gimple_assign_rhs1 (stmt);
4668 ops[1] = gimple_assign_rhs2 (stmt);
4669 ops[2] = gimple_assign_rhs3 (stmt);
4670 break;
4672 case GIMPLE_UNARY_RHS:
4673 return false;
4675 default:
4676 gcc_unreachable ();
4679 if (code == COND_EXPR && slp_node)
4680 return false;
4682 scalar_dest = gimple_assign_lhs (stmt);
4683 scalar_type = TREE_TYPE (scalar_dest);
4684 if (!POINTER_TYPE_P (scalar_type) && !INTEGRAL_TYPE_P (scalar_type)
4685 && !SCALAR_FLOAT_TYPE_P (scalar_type))
4686 return false;
4688 /* Do not try to vectorize bit-precision reductions. */
4689 if ((TYPE_PRECISION (scalar_type)
4690 != GET_MODE_PRECISION (TYPE_MODE (scalar_type))))
4691 return false;
4693 /* All uses but the last are expected to be defined in the loop.
4694 The last use is the reduction variable. In case of nested cycle this
4695 assumption is not true: we use reduc_index to record the index of the
4696 reduction variable. */
4697 for (i = 0; i < op_type-1; i++)
4699 /* The condition of COND_EXPR is checked in vectorizable_condition(). */
4700 if (i == 0 && code == COND_EXPR)
4701 continue;
4703 is_simple_use = vect_is_simple_use_1 (ops[i], stmt, loop_vinfo, NULL,
4704 &def_stmt, &def, &dt, &tem);
4705 if (!vectype_in)
4706 vectype_in = tem;
4707 gcc_assert (is_simple_use);
4709 if (dt != vect_internal_def
4710 && dt != vect_external_def
4711 && dt != vect_constant_def
4712 && dt != vect_induction_def
4713 && !(dt == vect_nested_cycle && nested_cycle))
4714 return false;
4716 if (dt == vect_nested_cycle)
4718 found_nested_cycle_def = true;
4719 reduc_def_stmt = def_stmt;
4720 reduc_index = i;
4724 is_simple_use = vect_is_simple_use_1 (ops[i], stmt, loop_vinfo, NULL,
4725 &def_stmt, &def, &dt, &tem);
4726 if (!vectype_in)
4727 vectype_in = tem;
4728 gcc_assert (is_simple_use);
4729 gcc_assert (dt == vect_reduction_def
4730 || dt == vect_nested_cycle
4731 || ((dt == vect_internal_def || dt == vect_external_def
4732 || dt == vect_constant_def || dt == vect_induction_def)
4733 && nested_cycle && found_nested_cycle_def));
4734 if (!found_nested_cycle_def)
4735 reduc_def_stmt = def_stmt;
4737 gcc_assert (gimple_code (reduc_def_stmt) == GIMPLE_PHI);
4738 if (orig_stmt)
4739 gcc_assert (orig_stmt == vect_is_simple_reduction (loop_vinfo,
4740 reduc_def_stmt,
4741 !nested_cycle,
4742 &dummy));
4743 else
4745 gimple tmp = vect_is_simple_reduction (loop_vinfo, reduc_def_stmt,
4746 !nested_cycle, &dummy);
4747 /* We changed STMT to be the first stmt in reduction chain, hence we
4748 check that in this case the first element in the chain is STMT. */
4749 gcc_assert (stmt == tmp
4750 || GROUP_FIRST_ELEMENT (vinfo_for_stmt (tmp)) == stmt);
4753 if (STMT_VINFO_LIVE_P (vinfo_for_stmt (reduc_def_stmt)))
4754 return false;
4756 if (slp_node || PURE_SLP_STMT (stmt_info))
4757 ncopies = 1;
4758 else
4759 ncopies = (LOOP_VINFO_VECT_FACTOR (loop_vinfo)
4760 / TYPE_VECTOR_SUBPARTS (vectype_in));
4762 gcc_assert (ncopies >= 1);
4764 vec_mode = TYPE_MODE (vectype_in);
4766 if (code == COND_EXPR)
4768 if (!vectorizable_condition (stmt, gsi, NULL, ops[reduc_index], 0, NULL))
4770 if (dump_enabled_p ())
4771 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4772 "unsupported condition in reduction");
4774 return false;
4777 else
4779 /* 4. Supportable by target? */
4781 /* 4.1. check support for the operation in the loop */
4782 optab = optab_for_tree_code (code, vectype_in, optab_default);
4783 if (!optab)
4785 if (dump_enabled_p ())
4786 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4787 "no optab.");
4789 return false;
4792 if (optab_handler (optab, vec_mode) == CODE_FOR_nothing)
4794 if (dump_enabled_p ())
4795 dump_printf (MSG_NOTE, "op not supported by target.");
4797 if (GET_MODE_SIZE (vec_mode) != UNITS_PER_WORD
4798 || LOOP_VINFO_VECT_FACTOR (loop_vinfo)
4799 < vect_min_worthwhile_factor (code))
4800 return false;
4802 if (dump_enabled_p ())
4803 dump_printf (MSG_NOTE, "proceeding using word mode.");
4806 /* Worthwhile without SIMD support? */
4807 if (!VECTOR_MODE_P (TYPE_MODE (vectype_in))
4808 && LOOP_VINFO_VECT_FACTOR (loop_vinfo)
4809 < vect_min_worthwhile_factor (code))
4811 if (dump_enabled_p ())
4812 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4813 "not worthwhile without SIMD support.");
4815 return false;
4819 /* 4.2. Check support for the epilog operation.
4821 If STMT represents a reduction pattern, then the type of the
4822 reduction variable may be different than the type of the rest
4823 of the arguments. For example, consider the case of accumulation
4824 of shorts into an int accumulator; The original code:
4825 S1: int_a = (int) short_a;
4826 orig_stmt-> S2: int_acc = plus <int_a ,int_acc>;
4828 was replaced with:
4829 STMT: int_acc = widen_sum <short_a, int_acc>
4831 This means that:
4832 1. The tree-code that is used to create the vector operation in the
4833 epilog code (that reduces the partial results) is not the
4834 tree-code of STMT, but is rather the tree-code of the original
4835 stmt from the pattern that STMT is replacing. I.e, in the example
4836 above we want to use 'widen_sum' in the loop, but 'plus' in the
4837 epilog.
4838 2. The type (mode) we use to check available target support
4839 for the vector operation to be created in the *epilog*, is
4840 determined by the type of the reduction variable (in the example
4841 above we'd check this: optab_handler (plus_optab, vect_int_mode])).
4842 However the type (mode) we use to check available target support
4843 for the vector operation to be created *inside the loop*, is
4844 determined by the type of the other arguments to STMT (in the
4845 example we'd check this: optab_handler (widen_sum_optab,
4846 vect_short_mode)).
4848 This is contrary to "regular" reductions, in which the types of all
4849 the arguments are the same as the type of the reduction variable.
4850 For "regular" reductions we can therefore use the same vector type
4851 (and also the same tree-code) when generating the epilog code and
4852 when generating the code inside the loop. */
4854 if (orig_stmt)
4856 /* This is a reduction pattern: get the vectype from the type of the
4857 reduction variable, and get the tree-code from orig_stmt. */
4858 orig_code = gimple_assign_rhs_code (orig_stmt);
4859 gcc_assert (vectype_out);
4860 vec_mode = TYPE_MODE (vectype_out);
4862 else
4864 /* Regular reduction: use the same vectype and tree-code as used for
4865 the vector code inside the loop can be used for the epilog code. */
4866 orig_code = code;
4869 if (nested_cycle)
4871 def_bb = gimple_bb (reduc_def_stmt);
4872 def_stmt_loop = def_bb->loop_father;
4873 def_arg = PHI_ARG_DEF_FROM_EDGE (reduc_def_stmt,
4874 loop_preheader_edge (def_stmt_loop));
4875 if (TREE_CODE (def_arg) == SSA_NAME
4876 && (def_arg_stmt = SSA_NAME_DEF_STMT (def_arg))
4877 && gimple_code (def_arg_stmt) == GIMPLE_PHI
4878 && flow_bb_inside_loop_p (outer_loop, gimple_bb (def_arg_stmt))
4879 && vinfo_for_stmt (def_arg_stmt)
4880 && STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def_arg_stmt))
4881 == vect_double_reduction_def)
4882 double_reduc = true;
4885 epilog_reduc_code = ERROR_MARK;
4886 if (reduction_code_for_scalar_code (orig_code, &epilog_reduc_code))
4888 reduc_optab = optab_for_tree_code (epilog_reduc_code, vectype_out,
4889 optab_default);
4890 if (!reduc_optab)
4892 if (dump_enabled_p ())
4893 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4894 "no optab for reduction.");
4896 epilog_reduc_code = ERROR_MARK;
4899 if (reduc_optab
4900 && optab_handler (reduc_optab, vec_mode) == CODE_FOR_nothing)
4902 if (dump_enabled_p ())
4903 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4904 "reduc op not supported by target.");
4906 epilog_reduc_code = ERROR_MARK;
4909 else
4911 if (!nested_cycle || double_reduc)
4913 if (dump_enabled_p ())
4914 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4915 "no reduc code for scalar code.");
4917 return false;
4921 if (double_reduc && ncopies > 1)
4923 if (dump_enabled_p ())
4924 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4925 "multiple types in double reduction");
4927 return false;
4930 /* In case of widenning multiplication by a constant, we update the type
4931 of the constant to be the type of the other operand. We check that the
4932 constant fits the type in the pattern recognition pass. */
4933 if (code == DOT_PROD_EXPR
4934 && !types_compatible_p (TREE_TYPE (ops[0]), TREE_TYPE (ops[1])))
4936 if (TREE_CODE (ops[0]) == INTEGER_CST)
4937 ops[0] = fold_convert (TREE_TYPE (ops[1]), ops[0]);
4938 else if (TREE_CODE (ops[1]) == INTEGER_CST)
4939 ops[1] = fold_convert (TREE_TYPE (ops[0]), ops[1]);
4940 else
4942 if (dump_enabled_p ())
4943 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
4944 "invalid types in dot-prod");
4946 return false;
4950 if (!vec_stmt) /* transformation not required. */
4952 if (!vect_model_reduction_cost (stmt_info, epilog_reduc_code, ncopies))
4953 return false;
4954 STMT_VINFO_TYPE (stmt_info) = reduc_vec_info_type;
4955 return true;
4958 /** Transform. **/
4960 if (dump_enabled_p ())
4961 dump_printf_loc (MSG_NOTE, vect_location, "transform reduction.");
4963 /* FORNOW: Multiple types are not supported for condition. */
4964 if (code == COND_EXPR)
4965 gcc_assert (ncopies == 1);
4967 /* Create the destination vector */
4968 vec_dest = vect_create_destination_var (scalar_dest, vectype_out);
4970 /* In case the vectorization factor (VF) is bigger than the number
4971 of elements that we can fit in a vectype (nunits), we have to generate
4972 more than one vector stmt - i.e - we need to "unroll" the
4973 vector stmt by a factor VF/nunits. For more details see documentation
4974 in vectorizable_operation. */
4976 /* If the reduction is used in an outer loop we need to generate
4977 VF intermediate results, like so (e.g. for ncopies=2):
4978 r0 = phi (init, r0)
4979 r1 = phi (init, r1)
4980 r0 = x0 + r0;
4981 r1 = x1 + r1;
4982 (i.e. we generate VF results in 2 registers).
4983 In this case we have a separate def-use cycle for each copy, and therefore
4984 for each copy we get the vector def for the reduction variable from the
4985 respective phi node created for this copy.
4987 Otherwise (the reduction is unused in the loop nest), we can combine
4988 together intermediate results, like so (e.g. for ncopies=2):
4989 r = phi (init, r)
4990 r = x0 + r;
4991 r = x1 + r;
4992 (i.e. we generate VF/2 results in a single register).
4993 In this case for each copy we get the vector def for the reduction variable
4994 from the vectorized reduction operation generated in the previous iteration.
4997 if (STMT_VINFO_RELEVANT (stmt_info) == vect_unused_in_scope)
4999 single_defuse_cycle = true;
5000 epilog_copies = 1;
5002 else
5003 epilog_copies = ncopies;
5005 prev_stmt_info = NULL;
5006 prev_phi_info = NULL;
5007 if (slp_node)
5009 vec_num = SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node);
5010 gcc_assert (TYPE_VECTOR_SUBPARTS (vectype_out)
5011 == TYPE_VECTOR_SUBPARTS (vectype_in));
5013 else
5015 vec_num = 1;
5016 vec_oprnds0.create (1);
5017 if (op_type == ternary_op)
5018 vec_oprnds1.create (1);
5021 phis.create (vec_num);
5022 vect_defs.create (vec_num);
5023 if (!slp_node)
5024 vect_defs.quick_push (NULL_TREE);
5026 for (j = 0; j < ncopies; j++)
5028 if (j == 0 || !single_defuse_cycle)
5030 for (i = 0; i < vec_num; i++)
5032 /* Create the reduction-phi that defines the reduction
5033 operand. */
5034 new_phi = create_phi_node (vec_dest, loop->header);
5035 set_vinfo_for_stmt (new_phi,
5036 new_stmt_vec_info (new_phi, loop_vinfo,
5037 NULL));
5038 if (j == 0 || slp_node)
5039 phis.quick_push (new_phi);
5043 if (code == COND_EXPR)
5045 gcc_assert (!slp_node);
5046 vectorizable_condition (stmt, gsi, vec_stmt,
5047 PHI_RESULT (phis[0]),
5048 reduc_index, NULL);
5049 /* Multiple types are not supported for condition. */
5050 break;
5053 /* Handle uses. */
5054 if (j == 0)
5056 op0 = ops[!reduc_index];
5057 if (op_type == ternary_op)
5059 if (reduc_index == 0)
5060 op1 = ops[2];
5061 else
5062 op1 = ops[1];
5065 if (slp_node)
5066 vect_get_vec_defs (op0, op1, stmt, &vec_oprnds0, &vec_oprnds1,
5067 slp_node, -1);
5068 else
5070 loop_vec_def0 = vect_get_vec_def_for_operand (ops[!reduc_index],
5071 stmt, NULL);
5072 vec_oprnds0.quick_push (loop_vec_def0);
5073 if (op_type == ternary_op)
5075 loop_vec_def1 = vect_get_vec_def_for_operand (op1, stmt,
5076 NULL);
5077 vec_oprnds1.quick_push (loop_vec_def1);
5081 else
5083 if (!slp_node)
5085 enum vect_def_type dt;
5086 gimple dummy_stmt;
5087 tree dummy;
5089 vect_is_simple_use (ops[!reduc_index], stmt, loop_vinfo, NULL,
5090 &dummy_stmt, &dummy, &dt);
5091 loop_vec_def0 = vect_get_vec_def_for_stmt_copy (dt,
5092 loop_vec_def0);
5093 vec_oprnds0[0] = loop_vec_def0;
5094 if (op_type == ternary_op)
5096 vect_is_simple_use (op1, stmt, loop_vinfo, NULL, &dummy_stmt,
5097 &dummy, &dt);
5098 loop_vec_def1 = vect_get_vec_def_for_stmt_copy (dt,
5099 loop_vec_def1);
5100 vec_oprnds1[0] = loop_vec_def1;
5104 if (single_defuse_cycle)
5105 reduc_def = gimple_assign_lhs (new_stmt);
5107 STMT_VINFO_RELATED_STMT (prev_phi_info) = new_phi;
5110 FOR_EACH_VEC_ELT (vec_oprnds0, i, def0)
5112 if (slp_node)
5113 reduc_def = PHI_RESULT (phis[i]);
5114 else
5116 if (!single_defuse_cycle || j == 0)
5117 reduc_def = PHI_RESULT (new_phi);
5120 def1 = ((op_type == ternary_op)
5121 ? vec_oprnds1[i] : NULL);
5122 if (op_type == binary_op)
5124 if (reduc_index == 0)
5125 expr = build2 (code, vectype_out, reduc_def, def0);
5126 else
5127 expr = build2 (code, vectype_out, def0, reduc_def);
5129 else
5131 if (reduc_index == 0)
5132 expr = build3 (code, vectype_out, reduc_def, def0, def1);
5133 else
5135 if (reduc_index == 1)
5136 expr = build3 (code, vectype_out, def0, reduc_def, def1);
5137 else
5138 expr = build3 (code, vectype_out, def0, def1, reduc_def);
5142 new_stmt = gimple_build_assign (vec_dest, expr);
5143 new_temp = make_ssa_name (vec_dest, new_stmt);
5144 gimple_assign_set_lhs (new_stmt, new_temp);
5145 vect_finish_stmt_generation (stmt, new_stmt, gsi);
5147 if (slp_node)
5149 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
5150 vect_defs.quick_push (new_temp);
5152 else
5153 vect_defs[0] = new_temp;
5156 if (slp_node)
5157 continue;
5159 if (j == 0)
5160 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
5161 else
5162 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
5164 prev_stmt_info = vinfo_for_stmt (new_stmt);
5165 prev_phi_info = vinfo_for_stmt (new_phi);
5168 /* Finalize the reduction-phi (set its arguments) and create the
5169 epilog reduction code. */
5170 if ((!single_defuse_cycle || code == COND_EXPR) && !slp_node)
5172 new_temp = gimple_assign_lhs (*vec_stmt);
5173 vect_defs[0] = new_temp;
5176 vect_create_epilog_for_reduction (vect_defs, stmt, epilog_copies,
5177 epilog_reduc_code, phis, reduc_index,
5178 double_reduc, slp_node);
5180 phis.release ();
5181 vec_oprnds0.release ();
5182 vec_oprnds1.release ();
5184 return true;
5187 /* Function vect_min_worthwhile_factor.
5189 For a loop where we could vectorize the operation indicated by CODE,
5190 return the minimum vectorization factor that makes it worthwhile
5191 to use generic vectors. */
5193 vect_min_worthwhile_factor (enum tree_code code)
5195 switch (code)
5197 case PLUS_EXPR:
5198 case MINUS_EXPR:
5199 case NEGATE_EXPR:
5200 return 4;
5202 case BIT_AND_EXPR:
5203 case BIT_IOR_EXPR:
5204 case BIT_XOR_EXPR:
5205 case BIT_NOT_EXPR:
5206 return 2;
5208 default:
5209 return INT_MAX;
5214 /* Function vectorizable_induction
5216 Check if PHI performs an induction computation that can be vectorized.
5217 If VEC_STMT is also passed, vectorize the induction PHI: create a vectorized
5218 phi to replace it, put it in VEC_STMT, and add it to the same basic block.
5219 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
5221 bool
5222 vectorizable_induction (gimple phi, gimple_stmt_iterator *gsi ATTRIBUTE_UNUSED,
5223 gimple *vec_stmt)
5225 stmt_vec_info stmt_info = vinfo_for_stmt (phi);
5226 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
5227 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
5228 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
5229 int nunits = TYPE_VECTOR_SUBPARTS (vectype);
5230 int ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits;
5231 tree vec_def;
5233 gcc_assert (ncopies >= 1);
5234 /* FORNOW. These restrictions should be relaxed. */
5235 if (nested_in_vect_loop_p (loop, phi))
5237 imm_use_iterator imm_iter;
5238 use_operand_p use_p;
5239 gimple exit_phi;
5240 edge latch_e;
5241 tree loop_arg;
5243 if (ncopies > 1)
5245 if (dump_enabled_p ())
5246 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5247 "multiple types in nested loop.");
5248 return false;
5251 exit_phi = NULL;
5252 latch_e = loop_latch_edge (loop->inner);
5253 loop_arg = PHI_ARG_DEF_FROM_EDGE (phi, latch_e);
5254 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, loop_arg)
5256 if (!flow_bb_inside_loop_p (loop->inner,
5257 gimple_bb (USE_STMT (use_p))))
5259 exit_phi = USE_STMT (use_p);
5260 break;
5263 if (exit_phi)
5265 stmt_vec_info exit_phi_vinfo = vinfo_for_stmt (exit_phi);
5266 if (!(STMT_VINFO_RELEVANT_P (exit_phi_vinfo)
5267 && !STMT_VINFO_LIVE_P (exit_phi_vinfo)))
5269 if (dump_enabled_p ())
5270 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5271 "inner-loop induction only used outside "
5272 "of the outer vectorized loop.");
5273 return false;
5278 if (!STMT_VINFO_RELEVANT_P (stmt_info))
5279 return false;
5281 /* FORNOW: SLP not supported. */
5282 if (STMT_SLP_TYPE (stmt_info))
5283 return false;
5285 gcc_assert (STMT_VINFO_DEF_TYPE (stmt_info) == vect_induction_def);
5287 if (gimple_code (phi) != GIMPLE_PHI)
5288 return false;
5290 if (!vec_stmt) /* transformation not required. */
5292 STMT_VINFO_TYPE (stmt_info) = induc_vec_info_type;
5293 if (dump_enabled_p ())
5294 dump_printf_loc (MSG_NOTE, vect_location,
5295 "=== vectorizable_induction ===");
5296 vect_model_induction_cost (stmt_info, ncopies);
5297 return true;
5300 /** Transform. **/
5302 if (dump_enabled_p ())
5303 dump_printf_loc (MSG_NOTE, vect_location, "transform induction phi.");
5305 vec_def = get_initial_def_for_induction (phi);
5306 *vec_stmt = SSA_NAME_DEF_STMT (vec_def);
5307 return true;
5310 /* Function vectorizable_live_operation.
5312 STMT computes a value that is used outside the loop. Check if
5313 it can be supported. */
5315 bool
5316 vectorizable_live_operation (gimple stmt,
5317 gimple_stmt_iterator *gsi ATTRIBUTE_UNUSED,
5318 gimple *vec_stmt ATTRIBUTE_UNUSED)
5320 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
5321 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
5322 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
5323 int i;
5324 int op_type;
5325 tree op;
5326 tree def;
5327 gimple def_stmt;
5328 enum vect_def_type dt;
5329 enum tree_code code;
5330 enum gimple_rhs_class rhs_class;
5332 gcc_assert (STMT_VINFO_LIVE_P (stmt_info));
5334 if (STMT_VINFO_DEF_TYPE (stmt_info) == vect_reduction_def)
5335 return false;
5337 if (!is_gimple_assign (stmt))
5338 return false;
5340 if (TREE_CODE (gimple_assign_lhs (stmt)) != SSA_NAME)
5341 return false;
5343 /* FORNOW. CHECKME. */
5344 if (nested_in_vect_loop_p (loop, stmt))
5345 return false;
5347 code = gimple_assign_rhs_code (stmt);
5348 op_type = TREE_CODE_LENGTH (code);
5349 rhs_class = get_gimple_rhs_class (code);
5350 gcc_assert (rhs_class != GIMPLE_UNARY_RHS || op_type == unary_op);
5351 gcc_assert (rhs_class != GIMPLE_BINARY_RHS || op_type == binary_op);
5353 /* FORNOW: support only if all uses are invariant. This means
5354 that the scalar operations can remain in place, unvectorized.
5355 The original last scalar value that they compute will be used. */
5357 for (i = 0; i < op_type; i++)
5359 if (rhs_class == GIMPLE_SINGLE_RHS)
5360 op = TREE_OPERAND (gimple_op (stmt, 1), i);
5361 else
5362 op = gimple_op (stmt, i + 1);
5363 if (op
5364 && !vect_is_simple_use (op, stmt, loop_vinfo, NULL, &def_stmt, &def,
5365 &dt))
5367 if (dump_enabled_p ())
5368 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5369 "use not simple.");
5370 return false;
5373 if (dt != vect_external_def && dt != vect_constant_def)
5374 return false;
5377 /* No transformation is required for the cases we currently support. */
5378 return true;
5381 /* Kill any debug uses outside LOOP of SSA names defined in STMT. */
5383 static void
5384 vect_loop_kill_debug_uses (struct loop *loop, gimple stmt)
5386 ssa_op_iter op_iter;
5387 imm_use_iterator imm_iter;
5388 def_operand_p def_p;
5389 gimple ustmt;
5391 FOR_EACH_PHI_OR_STMT_DEF (def_p, stmt, op_iter, SSA_OP_DEF)
5393 FOR_EACH_IMM_USE_STMT (ustmt, imm_iter, DEF_FROM_PTR (def_p))
5395 basic_block bb;
5397 if (!is_gimple_debug (ustmt))
5398 continue;
5400 bb = gimple_bb (ustmt);
5402 if (!flow_bb_inside_loop_p (loop, bb))
5404 if (gimple_debug_bind_p (ustmt))
5406 if (dump_enabled_p ())
5407 dump_printf_loc (MSG_NOTE, vect_location,
5408 "killing debug use");
5410 gimple_debug_bind_reset_value (ustmt);
5411 update_stmt (ustmt);
5413 else
5414 gcc_unreachable ();
5420 /* Function vect_transform_loop.
5422 The analysis phase has determined that the loop is vectorizable.
5423 Vectorize the loop - created vectorized stmts to replace the scalar
5424 stmts in the loop, and update the loop exit condition. */
5426 void
5427 vect_transform_loop (loop_vec_info loop_vinfo)
5429 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
5430 basic_block *bbs = LOOP_VINFO_BBS (loop_vinfo);
5431 int nbbs = loop->num_nodes;
5432 gimple_stmt_iterator si;
5433 int i;
5434 tree ratio = NULL;
5435 int vectorization_factor = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
5436 bool grouped_store;
5437 bool slp_scheduled = false;
5438 unsigned int nunits;
5439 gimple stmt, pattern_stmt;
5440 gimple_seq pattern_def_seq = NULL;
5441 gimple_stmt_iterator pattern_def_si = gsi_none ();
5442 bool transform_pattern_stmt = false;
5443 bool check_profitability = false;
5444 int th;
5445 /* Record number of iterations before we started tampering with the profile. */
5446 gcov_type expected_iterations = expected_loop_iterations_unbounded (loop);
5448 if (dump_enabled_p ())
5449 dump_printf_loc (MSG_NOTE, vect_location, "=== vec_transform_loop ===");
5451 /* If profile is inprecise, we have chance to fix it up. */
5452 if (LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo))
5453 expected_iterations = LOOP_VINFO_INT_NITERS (loop_vinfo);
5455 /* Use the more conservative vectorization threshold. If the number
5456 of iterations is constant assume the cost check has been performed
5457 by our caller. If the threshold makes all loops profitable that
5458 run at least the vectorization factor number of times checking
5459 is pointless, too. */
5460 th = ((PARAM_VALUE (PARAM_MIN_VECT_LOOP_BOUND)
5461 * LOOP_VINFO_VECT_FACTOR (loop_vinfo)) - 1);
5462 th = MAX (th, LOOP_VINFO_COST_MODEL_MIN_ITERS (loop_vinfo));
5463 if (th >= LOOP_VINFO_VECT_FACTOR (loop_vinfo) - 1
5464 && !LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo))
5466 if (dump_enabled_p ())
5467 dump_printf_loc (MSG_NOTE, vect_location,
5468 "Profitability threshold is %d loop iterations.", th);
5469 check_profitability = true;
5472 /* Peel the loop if there are data refs with unknown alignment.
5473 Only one data ref with unknown store is allowed. */
5475 if (LOOP_PEELING_FOR_ALIGNMENT (loop_vinfo))
5477 vect_do_peeling_for_alignment (loop_vinfo, th, check_profitability);
5478 check_profitability = false;
5481 if (LOOP_REQUIRES_VERSIONING_FOR_ALIGNMENT (loop_vinfo)
5482 || LOOP_REQUIRES_VERSIONING_FOR_ALIAS (loop_vinfo))
5484 vect_loop_versioning (loop_vinfo, th, check_profitability);
5485 check_profitability = false;
5488 /* If the loop has a symbolic number of iterations 'n' (i.e. it's not a
5489 compile time constant), or it is a constant that doesn't divide by the
5490 vectorization factor, then an epilog loop needs to be created.
5491 We therefore duplicate the loop: the original loop will be vectorized,
5492 and will compute the first (n/VF) iterations. The second copy of the loop
5493 will remain scalar and will compute the remaining (n%VF) iterations.
5494 (VF is the vectorization factor). */
5496 if (!LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo)
5497 || (LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo)
5498 && LOOP_VINFO_INT_NITERS (loop_vinfo) % vectorization_factor != 0)
5499 || LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo))
5500 vect_do_peeling_for_loop_bound (loop_vinfo, &ratio,
5501 th, check_profitability);
5502 else
5503 ratio = build_int_cst (TREE_TYPE (LOOP_VINFO_NITERS (loop_vinfo)),
5504 LOOP_VINFO_INT_NITERS (loop_vinfo) / vectorization_factor);
5506 /* 1) Make sure the loop header has exactly two entries
5507 2) Make sure we have a preheader basic block. */
5509 gcc_assert (EDGE_COUNT (loop->header->preds) == 2);
5511 split_edge (loop_preheader_edge (loop));
5513 /* FORNOW: the vectorizer supports only loops which body consist
5514 of one basic block (header + empty latch). When the vectorizer will
5515 support more involved loop forms, the order by which the BBs are
5516 traversed need to be reconsidered. */
5518 for (i = 0; i < nbbs; i++)
5520 basic_block bb = bbs[i];
5521 stmt_vec_info stmt_info;
5522 gimple phi;
5524 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
5526 phi = gsi_stmt (si);
5527 if (dump_enabled_p ())
5529 dump_printf_loc (MSG_NOTE, vect_location,
5530 "------>vectorizing phi: ");
5531 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, phi, 0);
5533 stmt_info = vinfo_for_stmt (phi);
5534 if (!stmt_info)
5535 continue;
5537 if (MAY_HAVE_DEBUG_STMTS && !STMT_VINFO_LIVE_P (stmt_info))
5538 vect_loop_kill_debug_uses (loop, phi);
5540 if (!STMT_VINFO_RELEVANT_P (stmt_info)
5541 && !STMT_VINFO_LIVE_P (stmt_info))
5542 continue;
5544 if ((TYPE_VECTOR_SUBPARTS (STMT_VINFO_VECTYPE (stmt_info))
5545 != (unsigned HOST_WIDE_INT) vectorization_factor)
5546 && dump_enabled_p ())
5547 dump_printf_loc (MSG_NOTE, vect_location, "multiple-types.");
5549 if (STMT_VINFO_DEF_TYPE (stmt_info) == vect_induction_def)
5551 if (dump_enabled_p ())
5552 dump_printf_loc (MSG_NOTE, vect_location, "transform phi.");
5553 vect_transform_stmt (phi, NULL, NULL, NULL, NULL);
5557 pattern_stmt = NULL;
5558 for (si = gsi_start_bb (bb); !gsi_end_p (si) || transform_pattern_stmt;)
5560 bool is_store;
5562 if (transform_pattern_stmt)
5563 stmt = pattern_stmt;
5564 else
5565 stmt = gsi_stmt (si);
5567 if (dump_enabled_p ())
5569 dump_printf_loc (MSG_NOTE, vect_location,
5570 "------>vectorizing statement: ");
5571 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
5574 stmt_info = vinfo_for_stmt (stmt);
5576 /* vector stmts created in the outer-loop during vectorization of
5577 stmts in an inner-loop may not have a stmt_info, and do not
5578 need to be vectorized. */
5579 if (!stmt_info)
5581 gsi_next (&si);
5582 continue;
5585 if (MAY_HAVE_DEBUG_STMTS && !STMT_VINFO_LIVE_P (stmt_info))
5586 vect_loop_kill_debug_uses (loop, stmt);
5588 if (!STMT_VINFO_RELEVANT_P (stmt_info)
5589 && !STMT_VINFO_LIVE_P (stmt_info))
5591 if (STMT_VINFO_IN_PATTERN_P (stmt_info)
5592 && (pattern_stmt = STMT_VINFO_RELATED_STMT (stmt_info))
5593 && (STMT_VINFO_RELEVANT_P (vinfo_for_stmt (pattern_stmt))
5594 || STMT_VINFO_LIVE_P (vinfo_for_stmt (pattern_stmt))))
5596 stmt = pattern_stmt;
5597 stmt_info = vinfo_for_stmt (stmt);
5599 else
5601 gsi_next (&si);
5602 continue;
5605 else if (STMT_VINFO_IN_PATTERN_P (stmt_info)
5606 && (pattern_stmt = STMT_VINFO_RELATED_STMT (stmt_info))
5607 && (STMT_VINFO_RELEVANT_P (vinfo_for_stmt (pattern_stmt))
5608 || STMT_VINFO_LIVE_P (vinfo_for_stmt (pattern_stmt))))
5609 transform_pattern_stmt = true;
5611 /* If pattern statement has def stmts, vectorize them too. */
5612 if (is_pattern_stmt_p (stmt_info))
5614 if (pattern_def_seq == NULL)
5616 pattern_def_seq = STMT_VINFO_PATTERN_DEF_SEQ (stmt_info);
5617 pattern_def_si = gsi_start (pattern_def_seq);
5619 else if (!gsi_end_p (pattern_def_si))
5620 gsi_next (&pattern_def_si);
5621 if (pattern_def_seq != NULL)
5623 gimple pattern_def_stmt = NULL;
5624 stmt_vec_info pattern_def_stmt_info = NULL;
5626 while (!gsi_end_p (pattern_def_si))
5628 pattern_def_stmt = gsi_stmt (pattern_def_si);
5629 pattern_def_stmt_info
5630 = vinfo_for_stmt (pattern_def_stmt);
5631 if (STMT_VINFO_RELEVANT_P (pattern_def_stmt_info)
5632 || STMT_VINFO_LIVE_P (pattern_def_stmt_info))
5633 break;
5634 gsi_next (&pattern_def_si);
5637 if (!gsi_end_p (pattern_def_si))
5639 if (dump_enabled_p ())
5641 dump_printf_loc (MSG_NOTE, vect_location,
5642 "==> vectorizing pattern def "
5643 "stmt: ");
5644 dump_gimple_stmt (MSG_NOTE, TDF_SLIM,
5645 pattern_def_stmt, 0);
5648 stmt = pattern_def_stmt;
5649 stmt_info = pattern_def_stmt_info;
5651 else
5653 pattern_def_si = gsi_none ();
5654 transform_pattern_stmt = false;
5657 else
5658 transform_pattern_stmt = false;
5661 gcc_assert (STMT_VINFO_VECTYPE (stmt_info));
5662 nunits = (unsigned int) TYPE_VECTOR_SUBPARTS (
5663 STMT_VINFO_VECTYPE (stmt_info));
5664 if (!STMT_SLP_TYPE (stmt_info)
5665 && nunits != (unsigned int) vectorization_factor
5666 && dump_enabled_p ())
5667 /* For SLP VF is set according to unrolling factor, and not to
5668 vector size, hence for SLP this print is not valid. */
5669 dump_printf_loc (MSG_NOTE, vect_location,
5670 "multiple-types.");
5672 /* SLP. Schedule all the SLP instances when the first SLP stmt is
5673 reached. */
5674 if (STMT_SLP_TYPE (stmt_info))
5676 if (!slp_scheduled)
5678 slp_scheduled = true;
5680 if (dump_enabled_p ())
5681 dump_printf_loc (MSG_NOTE, vect_location,
5682 "=== scheduling SLP instances ===");
5684 vect_schedule_slp (loop_vinfo, NULL);
5687 /* Hybrid SLP stmts must be vectorized in addition to SLP. */
5688 if (!vinfo_for_stmt (stmt) || PURE_SLP_STMT (stmt_info))
5690 if (!transform_pattern_stmt && gsi_end_p (pattern_def_si))
5692 pattern_def_seq = NULL;
5693 gsi_next (&si);
5695 continue;
5699 /* -------- vectorize statement ------------ */
5700 if (dump_enabled_p ())
5701 dump_printf_loc (MSG_NOTE, vect_location, "transform statement.");
5703 grouped_store = false;
5704 is_store = vect_transform_stmt (stmt, &si, &grouped_store, NULL, NULL);
5705 if (is_store)
5707 if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
5709 /* Interleaving. If IS_STORE is TRUE, the vectorization of the
5710 interleaving chain was completed - free all the stores in
5711 the chain. */
5712 gsi_next (&si);
5713 vect_remove_stores (GROUP_FIRST_ELEMENT (stmt_info));
5714 continue;
5716 else
5718 /* Free the attached stmt_vec_info and remove the stmt. */
5719 gimple store = gsi_stmt (si);
5720 free_stmt_vec_info (store);
5721 unlink_stmt_vdef (store);
5722 gsi_remove (&si, true);
5723 release_defs (store);
5724 continue;
5728 if (!transform_pattern_stmt && gsi_end_p (pattern_def_si))
5730 pattern_def_seq = NULL;
5731 gsi_next (&si);
5733 } /* stmts in BB */
5734 } /* BBs in loop */
5736 slpeel_make_loop_iterate_ntimes (loop, ratio);
5738 /* Reduce loop iterations by the vectorization factor. */
5739 scale_loop_profile (loop, RDIV (REG_BR_PROB_BASE , vectorization_factor),
5740 expected_iterations / vectorization_factor);
5741 loop->nb_iterations_upper_bound
5742 = loop->nb_iterations_upper_bound.udiv (double_int::from_uhwi (vectorization_factor),
5743 FLOOR_DIV_EXPR);
5744 if (LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo)
5745 && loop->nb_iterations_upper_bound != double_int_zero)
5746 loop->nb_iterations_upper_bound = loop->nb_iterations_upper_bound - double_int_one;
5747 if (loop->any_estimate)
5749 loop->nb_iterations_estimate
5750 = loop->nb_iterations_estimate.udiv (double_int::from_uhwi (vectorization_factor),
5751 FLOOR_DIV_EXPR);
5752 if (LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo)
5753 && loop->nb_iterations_estimate != double_int_zero)
5754 loop->nb_iterations_estimate = loop->nb_iterations_estimate - double_int_one;
5757 /* The memory tags and pointers in vectorized statements need to
5758 have their SSA forms updated. FIXME, why can't this be delayed
5759 until all the loops have been transformed? */
5760 update_ssa (TODO_update_ssa);
5762 if (dump_enabled_p ())
5763 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, vect_location, "LOOP VECTORIZED.");
5764 if (loop->inner && dump_enabled_p ())
5765 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, vect_location,
5766 "OUTER LOOP VECTORIZED.");