2015-05-22 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / tree-vect-loop.c
blob123958b31ca352a39b8cc2a63c1510421fbb3cc3
1 /* Loop Vectorization
2 Copyright (C) 2003-2015 Free Software Foundation, Inc.
3 Contributed by Dorit Naishlos <dorit@il.ibm.com> and
4 Ira Rosen <irar@il.ibm.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "dumpfile.h"
26 #include "tm.h"
27 #include "hash-set.h"
28 #include "machmode.h"
29 #include "vec.h"
30 #include "double-int.h"
31 #include "input.h"
32 #include "alias.h"
33 #include "symtab.h"
34 #include "wide-int.h"
35 #include "inchash.h"
36 #include "tree.h"
37 #include "fold-const.h"
38 #include "stor-layout.h"
39 #include "predict.h"
40 #include "hard-reg-set.h"
41 #include "function.h"
42 #include "dominance.h"
43 #include "cfg.h"
44 #include "cfganal.h"
45 #include "basic-block.h"
46 #include "gimple-pretty-print.h"
47 #include "tree-ssa-alias.h"
48 #include "internal-fn.h"
49 #include "gimple-expr.h"
50 #include "is-a.h"
51 #include "gimple.h"
52 #include "gimplify.h"
53 #include "gimple-iterator.h"
54 #include "gimplify-me.h"
55 #include "gimple-ssa.h"
56 #include "tree-phinodes.h"
57 #include "ssa-iterators.h"
58 #include "stringpool.h"
59 #include "tree-ssanames.h"
60 #include "tree-ssa-loop-ivopts.h"
61 #include "tree-ssa-loop-manip.h"
62 #include "tree-ssa-loop-niter.h"
63 #include "tree-pass.h"
64 #include "cfgloop.h"
65 #include "hashtab.h"
66 #include "rtl.h"
67 #include "flags.h"
68 #include "statistics.h"
69 #include "real.h"
70 #include "fixed-value.h"
71 #include "insn-config.h"
72 #include "expmed.h"
73 #include "dojump.h"
74 #include "explow.h"
75 #include "calls.h"
76 #include "emit-rtl.h"
77 #include "varasm.h"
78 #include "stmt.h"
79 #include "expr.h"
80 #include "recog.h"
81 #include "insn-codes.h"
82 #include "optabs.h"
83 #include "params.h"
84 #include "diagnostic-core.h"
85 #include "tree-chrec.h"
86 #include "tree-scalar-evolution.h"
87 #include "tree-vectorizer.h"
88 #include "target.h"
90 /* Loop Vectorization Pass.
92 This pass tries to vectorize loops.
94 For example, the vectorizer transforms the following simple loop:
96 short a[N]; short b[N]; short c[N]; int i;
98 for (i=0; i<N; i++){
99 a[i] = b[i] + c[i];
102 as if it was manually vectorized by rewriting the source code into:
104 typedef int __attribute__((mode(V8HI))) v8hi;
105 short a[N]; short b[N]; short c[N]; int i;
106 v8hi *pa = (v8hi*)a, *pb = (v8hi*)b, *pc = (v8hi*)c;
107 v8hi va, vb, vc;
109 for (i=0; i<N/8; i++){
110 vb = pb[i];
111 vc = pc[i];
112 va = vb + vc;
113 pa[i] = va;
116 The main entry to this pass is vectorize_loops(), in which
117 the vectorizer applies a set of analyses on a given set of loops,
118 followed by the actual vectorization transformation for the loops that
119 had successfully passed the analysis phase.
120 Throughout this pass we make a distinction between two types of
121 data: scalars (which are represented by SSA_NAMES), and memory references
122 ("data-refs"). These two types of data require different handling both
123 during analysis and transformation. The types of data-refs that the
124 vectorizer currently supports are ARRAY_REFS which base is an array DECL
125 (not a pointer), and INDIRECT_REFS through pointers; both array and pointer
126 accesses are required to have a simple (consecutive) access pattern.
128 Analysis phase:
129 ===============
130 The driver for the analysis phase is vect_analyze_loop().
131 It applies a set of analyses, some of which rely on the scalar evolution
132 analyzer (scev) developed by Sebastian Pop.
134 During the analysis phase the vectorizer records some information
135 per stmt in a "stmt_vec_info" struct which is attached to each stmt in the
136 loop, as well as general information about the loop as a whole, which is
137 recorded in a "loop_vec_info" struct attached to each loop.
139 Transformation phase:
140 =====================
141 The loop transformation phase scans all the stmts in the loop, and
142 creates a vector stmt (or a sequence of stmts) for each scalar stmt S in
143 the loop that needs to be vectorized. It inserts the vector code sequence
144 just before the scalar stmt S, and records a pointer to the vector code
145 in STMT_VINFO_VEC_STMT (stmt_info) (stmt_info is the stmt_vec_info struct
146 attached to S). This pointer will be used for the vectorization of following
147 stmts which use the def of stmt S. Stmt S is removed if it writes to memory;
148 otherwise, we rely on dead code elimination for removing it.
150 For example, say stmt S1 was vectorized into stmt VS1:
152 VS1: vb = px[i];
153 S1: b = x[i]; STMT_VINFO_VEC_STMT (stmt_info (S1)) = VS1
154 S2: a = b;
156 To vectorize stmt S2, the vectorizer first finds the stmt that defines
157 the operand 'b' (S1), and gets the relevant vector def 'vb' from the
158 vector stmt VS1 pointed to by STMT_VINFO_VEC_STMT (stmt_info (S1)). The
159 resulting sequence would be:
161 VS1: vb = px[i];
162 S1: b = x[i]; STMT_VINFO_VEC_STMT (stmt_info (S1)) = VS1
163 VS2: va = vb;
164 S2: a = b; STMT_VINFO_VEC_STMT (stmt_info (S2)) = VS2
166 Operands that are not SSA_NAMEs, are data-refs that appear in
167 load/store operations (like 'x[i]' in S1), and are handled differently.
169 Target modeling:
170 =================
171 Currently the only target specific information that is used is the
172 size of the vector (in bytes) - "TARGET_VECTORIZE_UNITS_PER_SIMD_WORD".
173 Targets that can support different sizes of vectors, for now will need
174 to specify one value for "TARGET_VECTORIZE_UNITS_PER_SIMD_WORD". More
175 flexibility will be added in the future.
177 Since we only vectorize operations which vector form can be
178 expressed using existing tree codes, to verify that an operation is
179 supported, the vectorizer checks the relevant optab at the relevant
180 machine_mode (e.g, optab_handler (add_optab, V8HImode)). If
181 the value found is CODE_FOR_nothing, then there's no target support, and
182 we can't vectorize the stmt.
184 For additional information on this project see:
185 http://gcc.gnu.org/projects/tree-ssa/vectorization.html
188 static void vect_estimate_min_profitable_iters (loop_vec_info, int *, int *);
190 /* Function vect_determine_vectorization_factor
192 Determine the vectorization factor (VF). VF is the number of data elements
193 that are operated upon in parallel in a single iteration of the vectorized
194 loop. For example, when vectorizing a loop that operates on 4byte elements,
195 on a target with vector size (VS) 16byte, the VF is set to 4, since 4
196 elements can fit in a single vector register.
198 We currently support vectorization of loops in which all types operated upon
199 are of the same size. Therefore this function currently sets VF according to
200 the size of the types operated upon, and fails if there are multiple sizes
201 in the loop.
203 VF is also the factor by which the loop iterations are strip-mined, e.g.:
204 original loop:
205 for (i=0; i<N; i++){
206 a[i] = b[i] + c[i];
209 vectorized loop:
210 for (i=0; i<N; i+=VF){
211 a[i:VF] = b[i:VF] + c[i:VF];
215 static bool
216 vect_determine_vectorization_factor (loop_vec_info loop_vinfo)
218 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
219 basic_block *bbs = LOOP_VINFO_BBS (loop_vinfo);
220 int nbbs = loop->num_nodes;
221 unsigned int vectorization_factor = 0;
222 tree scalar_type;
223 gphi *phi;
224 tree vectype;
225 unsigned int nunits;
226 stmt_vec_info stmt_info;
227 int i;
228 HOST_WIDE_INT dummy;
229 gimple stmt, pattern_stmt = NULL;
230 gimple_seq pattern_def_seq = NULL;
231 gimple_stmt_iterator pattern_def_si = gsi_none ();
232 bool analyze_pattern_stmt = false;
234 if (dump_enabled_p ())
235 dump_printf_loc (MSG_NOTE, vect_location,
236 "=== vect_determine_vectorization_factor ===\n");
238 for (i = 0; i < nbbs; i++)
240 basic_block bb = bbs[i];
242 for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
243 gsi_next (&si))
245 phi = si.phi ();
246 stmt_info = vinfo_for_stmt (phi);
247 if (dump_enabled_p ())
249 dump_printf_loc (MSG_NOTE, vect_location, "==> examining phi: ");
250 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, phi, 0);
251 dump_printf (MSG_NOTE, "\n");
254 gcc_assert (stmt_info);
256 if (STMT_VINFO_RELEVANT_P (stmt_info))
258 gcc_assert (!STMT_VINFO_VECTYPE (stmt_info));
259 scalar_type = TREE_TYPE (PHI_RESULT (phi));
261 if (dump_enabled_p ())
263 dump_printf_loc (MSG_NOTE, vect_location,
264 "get vectype for scalar type: ");
265 dump_generic_expr (MSG_NOTE, TDF_SLIM, scalar_type);
266 dump_printf (MSG_NOTE, "\n");
269 vectype = get_vectype_for_scalar_type (scalar_type);
270 if (!vectype)
272 if (dump_enabled_p ())
274 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
275 "not vectorized: unsupported "
276 "data-type ");
277 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
278 scalar_type);
279 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
281 return false;
283 STMT_VINFO_VECTYPE (stmt_info) = vectype;
285 if (dump_enabled_p ())
287 dump_printf_loc (MSG_NOTE, vect_location, "vectype: ");
288 dump_generic_expr (MSG_NOTE, TDF_SLIM, vectype);
289 dump_printf (MSG_NOTE, "\n");
292 nunits = TYPE_VECTOR_SUBPARTS (vectype);
293 if (dump_enabled_p ())
294 dump_printf_loc (MSG_NOTE, vect_location, "nunits = %d\n",
295 nunits);
297 if (!vectorization_factor
298 || (nunits > vectorization_factor))
299 vectorization_factor = nunits;
303 for (gimple_stmt_iterator si = gsi_start_bb (bb);
304 !gsi_end_p (si) || analyze_pattern_stmt;)
306 tree vf_vectype;
308 if (analyze_pattern_stmt)
309 stmt = pattern_stmt;
310 else
311 stmt = gsi_stmt (si);
313 stmt_info = vinfo_for_stmt (stmt);
315 if (dump_enabled_p ())
317 dump_printf_loc (MSG_NOTE, vect_location,
318 "==> examining statement: ");
319 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
320 dump_printf (MSG_NOTE, "\n");
323 gcc_assert (stmt_info);
325 /* Skip stmts which do not need to be vectorized. */
326 if ((!STMT_VINFO_RELEVANT_P (stmt_info)
327 && !STMT_VINFO_LIVE_P (stmt_info))
328 || gimple_clobber_p (stmt))
330 if (STMT_VINFO_IN_PATTERN_P (stmt_info)
331 && (pattern_stmt = STMT_VINFO_RELATED_STMT (stmt_info))
332 && (STMT_VINFO_RELEVANT_P (vinfo_for_stmt (pattern_stmt))
333 || STMT_VINFO_LIVE_P (vinfo_for_stmt (pattern_stmt))))
335 stmt = pattern_stmt;
336 stmt_info = vinfo_for_stmt (pattern_stmt);
337 if (dump_enabled_p ())
339 dump_printf_loc (MSG_NOTE, vect_location,
340 "==> examining pattern statement: ");
341 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
342 dump_printf (MSG_NOTE, "\n");
345 else
347 if (dump_enabled_p ())
348 dump_printf_loc (MSG_NOTE, vect_location, "skip.\n");
349 gsi_next (&si);
350 continue;
353 else if (STMT_VINFO_IN_PATTERN_P (stmt_info)
354 && (pattern_stmt = STMT_VINFO_RELATED_STMT (stmt_info))
355 && (STMT_VINFO_RELEVANT_P (vinfo_for_stmt (pattern_stmt))
356 || STMT_VINFO_LIVE_P (vinfo_for_stmt (pattern_stmt))))
357 analyze_pattern_stmt = true;
359 /* If a pattern statement has def stmts, analyze them too. */
360 if (is_pattern_stmt_p (stmt_info))
362 if (pattern_def_seq == NULL)
364 pattern_def_seq = STMT_VINFO_PATTERN_DEF_SEQ (stmt_info);
365 pattern_def_si = gsi_start (pattern_def_seq);
367 else if (!gsi_end_p (pattern_def_si))
368 gsi_next (&pattern_def_si);
369 if (pattern_def_seq != NULL)
371 gimple pattern_def_stmt = NULL;
372 stmt_vec_info pattern_def_stmt_info = NULL;
374 while (!gsi_end_p (pattern_def_si))
376 pattern_def_stmt = gsi_stmt (pattern_def_si);
377 pattern_def_stmt_info
378 = vinfo_for_stmt (pattern_def_stmt);
379 if (STMT_VINFO_RELEVANT_P (pattern_def_stmt_info)
380 || STMT_VINFO_LIVE_P (pattern_def_stmt_info))
381 break;
382 gsi_next (&pattern_def_si);
385 if (!gsi_end_p (pattern_def_si))
387 if (dump_enabled_p ())
389 dump_printf_loc (MSG_NOTE, vect_location,
390 "==> examining pattern def stmt: ");
391 dump_gimple_stmt (MSG_NOTE, TDF_SLIM,
392 pattern_def_stmt, 0);
393 dump_printf (MSG_NOTE, "\n");
396 stmt = pattern_def_stmt;
397 stmt_info = pattern_def_stmt_info;
399 else
401 pattern_def_si = gsi_none ();
402 analyze_pattern_stmt = false;
405 else
406 analyze_pattern_stmt = false;
409 if (gimple_get_lhs (stmt) == NULL_TREE
410 /* MASK_STORE has no lhs, but is ok. */
411 && (!is_gimple_call (stmt)
412 || !gimple_call_internal_p (stmt)
413 || gimple_call_internal_fn (stmt) != IFN_MASK_STORE))
415 if (is_gimple_call (stmt))
417 /* Ignore calls with no lhs. These must be calls to
418 #pragma omp simd functions, and what vectorization factor
419 it really needs can't be determined until
420 vectorizable_simd_clone_call. */
421 if (!analyze_pattern_stmt && gsi_end_p (pattern_def_si))
423 pattern_def_seq = NULL;
424 gsi_next (&si);
426 continue;
428 if (dump_enabled_p ())
430 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
431 "not vectorized: irregular stmt.");
432 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt,
434 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
436 return false;
439 if (VECTOR_MODE_P (TYPE_MODE (gimple_expr_type (stmt))))
441 if (dump_enabled_p ())
443 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
444 "not vectorized: vector stmt in loop:");
445 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, stmt, 0);
446 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
448 return false;
451 if (STMT_VINFO_VECTYPE (stmt_info))
453 /* The only case when a vectype had been already set is for stmts
454 that contain a dataref, or for "pattern-stmts" (stmts
455 generated by the vectorizer to represent/replace a certain
456 idiom). */
457 gcc_assert (STMT_VINFO_DATA_REF (stmt_info)
458 || is_pattern_stmt_p (stmt_info)
459 || !gsi_end_p (pattern_def_si));
460 vectype = STMT_VINFO_VECTYPE (stmt_info);
462 else
464 gcc_assert (!STMT_VINFO_DATA_REF (stmt_info));
465 if (is_gimple_call (stmt)
466 && gimple_call_internal_p (stmt)
467 && gimple_call_internal_fn (stmt) == IFN_MASK_STORE)
468 scalar_type = TREE_TYPE (gimple_call_arg (stmt, 3));
469 else
470 scalar_type = TREE_TYPE (gimple_get_lhs (stmt));
471 if (dump_enabled_p ())
473 dump_printf_loc (MSG_NOTE, vect_location,
474 "get vectype for scalar type: ");
475 dump_generic_expr (MSG_NOTE, TDF_SLIM, scalar_type);
476 dump_printf (MSG_NOTE, "\n");
478 vectype = get_vectype_for_scalar_type (scalar_type);
479 if (!vectype)
481 if (dump_enabled_p ())
483 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
484 "not vectorized: unsupported "
485 "data-type ");
486 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
487 scalar_type);
488 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
490 return false;
493 STMT_VINFO_VECTYPE (stmt_info) = vectype;
495 if (dump_enabled_p ())
497 dump_printf_loc (MSG_NOTE, vect_location, "vectype: ");
498 dump_generic_expr (MSG_NOTE, TDF_SLIM, vectype);
499 dump_printf (MSG_NOTE, "\n");
503 /* The vectorization factor is according to the smallest
504 scalar type (or the largest vector size, but we only
505 support one vector size per loop). */
506 scalar_type = vect_get_smallest_scalar_type (stmt, &dummy,
507 &dummy);
508 if (dump_enabled_p ())
510 dump_printf_loc (MSG_NOTE, vect_location,
511 "get vectype for scalar type: ");
512 dump_generic_expr (MSG_NOTE, TDF_SLIM, scalar_type);
513 dump_printf (MSG_NOTE, "\n");
515 vf_vectype = get_vectype_for_scalar_type (scalar_type);
516 if (!vf_vectype)
518 if (dump_enabled_p ())
520 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
521 "not vectorized: unsupported data-type ");
522 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
523 scalar_type);
524 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
526 return false;
529 if ((GET_MODE_SIZE (TYPE_MODE (vectype))
530 != GET_MODE_SIZE (TYPE_MODE (vf_vectype))))
532 if (dump_enabled_p ())
534 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
535 "not vectorized: different sized vector "
536 "types in statement, ");
537 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
538 vectype);
539 dump_printf (MSG_MISSED_OPTIMIZATION, " and ");
540 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
541 vf_vectype);
542 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
544 return false;
547 if (dump_enabled_p ())
549 dump_printf_loc (MSG_NOTE, vect_location, "vectype: ");
550 dump_generic_expr (MSG_NOTE, TDF_SLIM, vf_vectype);
551 dump_printf (MSG_NOTE, "\n");
554 nunits = TYPE_VECTOR_SUBPARTS (vf_vectype);
555 if (dump_enabled_p ())
556 dump_printf_loc (MSG_NOTE, vect_location, "nunits = %d\n", nunits);
557 if (!vectorization_factor
558 || (nunits > vectorization_factor))
559 vectorization_factor = nunits;
561 if (!analyze_pattern_stmt && gsi_end_p (pattern_def_si))
563 pattern_def_seq = NULL;
564 gsi_next (&si);
569 /* TODO: Analyze cost. Decide if worth while to vectorize. */
570 if (dump_enabled_p ())
571 dump_printf_loc (MSG_NOTE, vect_location, "vectorization factor = %d\n",
572 vectorization_factor);
573 if (vectorization_factor <= 1)
575 if (dump_enabled_p ())
576 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
577 "not vectorized: unsupported data-type\n");
578 return false;
580 LOOP_VINFO_VECT_FACTOR (loop_vinfo) = vectorization_factor;
582 return true;
586 /* Function vect_is_simple_iv_evolution.
588 FORNOW: A simple evolution of an induction variables in the loop is
589 considered a polynomial evolution. */
591 static bool
592 vect_is_simple_iv_evolution (unsigned loop_nb, tree access_fn, tree * init,
593 tree * step)
595 tree init_expr;
596 tree step_expr;
597 tree evolution_part = evolution_part_in_loop_num (access_fn, loop_nb);
598 basic_block bb;
600 /* When there is no evolution in this loop, the evolution function
601 is not "simple". */
602 if (evolution_part == NULL_TREE)
603 return false;
605 /* When the evolution is a polynomial of degree >= 2
606 the evolution function is not "simple". */
607 if (tree_is_chrec (evolution_part))
608 return false;
610 step_expr = evolution_part;
611 init_expr = unshare_expr (initial_condition_in_loop_num (access_fn, loop_nb));
613 if (dump_enabled_p ())
615 dump_printf_loc (MSG_NOTE, vect_location, "step: ");
616 dump_generic_expr (MSG_NOTE, TDF_SLIM, step_expr);
617 dump_printf (MSG_NOTE, ", init: ");
618 dump_generic_expr (MSG_NOTE, TDF_SLIM, init_expr);
619 dump_printf (MSG_NOTE, "\n");
622 *init = init_expr;
623 *step = step_expr;
625 if (TREE_CODE (step_expr) != INTEGER_CST
626 && (TREE_CODE (step_expr) != SSA_NAME
627 || ((bb = gimple_bb (SSA_NAME_DEF_STMT (step_expr)))
628 && flow_bb_inside_loop_p (get_loop (cfun, loop_nb), bb))
629 || (!INTEGRAL_TYPE_P (TREE_TYPE (step_expr))
630 && (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (step_expr))
631 || !flag_associative_math)))
632 && (TREE_CODE (step_expr) != REAL_CST
633 || !flag_associative_math))
635 if (dump_enabled_p ())
636 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
637 "step unknown.\n");
638 return false;
641 return true;
644 /* Function vect_analyze_scalar_cycles_1.
646 Examine the cross iteration def-use cycles of scalar variables
647 in LOOP. LOOP_VINFO represents the loop that is now being
648 considered for vectorization (can be LOOP, or an outer-loop
649 enclosing LOOP). */
651 static void
652 vect_analyze_scalar_cycles_1 (loop_vec_info loop_vinfo, struct loop *loop)
654 basic_block bb = loop->header;
655 tree init, step;
656 auto_vec<gimple, 64> worklist;
657 gphi_iterator gsi;
658 bool double_reduc;
660 if (dump_enabled_p ())
661 dump_printf_loc (MSG_NOTE, vect_location,
662 "=== vect_analyze_scalar_cycles ===\n");
664 /* First - identify all inductions. Reduction detection assumes that all the
665 inductions have been identified, therefore, this order must not be
666 changed. */
667 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
669 gphi *phi = gsi.phi ();
670 tree access_fn = NULL;
671 tree def = PHI_RESULT (phi);
672 stmt_vec_info stmt_vinfo = vinfo_for_stmt (phi);
674 if (dump_enabled_p ())
676 dump_printf_loc (MSG_NOTE, vect_location, "Analyze phi: ");
677 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, phi, 0);
678 dump_printf (MSG_NOTE, "\n");
681 /* Skip virtual phi's. The data dependences that are associated with
682 virtual defs/uses (i.e., memory accesses) are analyzed elsewhere. */
683 if (virtual_operand_p (def))
684 continue;
686 STMT_VINFO_DEF_TYPE (stmt_vinfo) = vect_unknown_def_type;
688 /* Analyze the evolution function. */
689 access_fn = analyze_scalar_evolution (loop, def);
690 if (access_fn)
692 STRIP_NOPS (access_fn);
693 if (dump_enabled_p ())
695 dump_printf_loc (MSG_NOTE, vect_location,
696 "Access function of PHI: ");
697 dump_generic_expr (MSG_NOTE, TDF_SLIM, access_fn);
698 dump_printf (MSG_NOTE, "\n");
700 STMT_VINFO_LOOP_PHI_EVOLUTION_PART (stmt_vinfo)
701 = evolution_part_in_loop_num (access_fn, loop->num);
704 if (!access_fn
705 || !vect_is_simple_iv_evolution (loop->num, access_fn, &init, &step)
706 || (LOOP_VINFO_LOOP (loop_vinfo) != loop
707 && TREE_CODE (step) != INTEGER_CST))
709 worklist.safe_push (phi);
710 continue;
713 gcc_assert (STMT_VINFO_LOOP_PHI_EVOLUTION_PART (stmt_vinfo) != NULL_TREE);
715 if (dump_enabled_p ())
716 dump_printf_loc (MSG_NOTE, vect_location, "Detected induction.\n");
717 STMT_VINFO_DEF_TYPE (stmt_vinfo) = vect_induction_def;
721 /* Second - identify all reductions and nested cycles. */
722 while (worklist.length () > 0)
724 gimple phi = worklist.pop ();
725 tree def = PHI_RESULT (phi);
726 stmt_vec_info stmt_vinfo = vinfo_for_stmt (phi);
727 gimple reduc_stmt;
728 bool nested_cycle;
730 if (dump_enabled_p ())
732 dump_printf_loc (MSG_NOTE, vect_location, "Analyze phi: ");
733 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, phi, 0);
734 dump_printf (MSG_NOTE, "\n");
737 gcc_assert (!virtual_operand_p (def)
738 && STMT_VINFO_DEF_TYPE (stmt_vinfo) == vect_unknown_def_type);
740 nested_cycle = (loop != LOOP_VINFO_LOOP (loop_vinfo));
741 reduc_stmt = vect_force_simple_reduction (loop_vinfo, phi, !nested_cycle,
742 &double_reduc);
743 if (reduc_stmt)
745 if (double_reduc)
747 if (dump_enabled_p ())
748 dump_printf_loc (MSG_NOTE, vect_location,
749 "Detected double reduction.\n");
751 STMT_VINFO_DEF_TYPE (stmt_vinfo) = vect_double_reduction_def;
752 STMT_VINFO_DEF_TYPE (vinfo_for_stmt (reduc_stmt)) =
753 vect_double_reduction_def;
755 else
757 if (nested_cycle)
759 if (dump_enabled_p ())
760 dump_printf_loc (MSG_NOTE, vect_location,
761 "Detected vectorizable nested cycle.\n");
763 STMT_VINFO_DEF_TYPE (stmt_vinfo) = vect_nested_cycle;
764 STMT_VINFO_DEF_TYPE (vinfo_for_stmt (reduc_stmt)) =
765 vect_nested_cycle;
767 else
769 if (dump_enabled_p ())
770 dump_printf_loc (MSG_NOTE, vect_location,
771 "Detected reduction.\n");
773 STMT_VINFO_DEF_TYPE (stmt_vinfo) = vect_reduction_def;
774 STMT_VINFO_DEF_TYPE (vinfo_for_stmt (reduc_stmt)) =
775 vect_reduction_def;
776 /* Store the reduction cycles for possible vectorization in
777 loop-aware SLP. */
778 LOOP_VINFO_REDUCTIONS (loop_vinfo).safe_push (reduc_stmt);
782 else
783 if (dump_enabled_p ())
784 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
785 "Unknown def-use cycle pattern.\n");
790 /* Function vect_analyze_scalar_cycles.
792 Examine the cross iteration def-use cycles of scalar variables, by
793 analyzing the loop-header PHIs of scalar variables. Classify each
794 cycle as one of the following: invariant, induction, reduction, unknown.
795 We do that for the loop represented by LOOP_VINFO, and also to its
796 inner-loop, if exists.
797 Examples for scalar cycles:
799 Example1: reduction:
801 loop1:
802 for (i=0; i<N; i++)
803 sum += a[i];
805 Example2: induction:
807 loop2:
808 for (i=0; i<N; i++)
809 a[i] = i; */
811 static void
812 vect_analyze_scalar_cycles (loop_vec_info loop_vinfo)
814 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
816 vect_analyze_scalar_cycles_1 (loop_vinfo, loop);
818 /* When vectorizing an outer-loop, the inner-loop is executed sequentially.
819 Reductions in such inner-loop therefore have different properties than
820 the reductions in the nest that gets vectorized:
821 1. When vectorized, they are executed in the same order as in the original
822 scalar loop, so we can't change the order of computation when
823 vectorizing them.
824 2. FIXME: Inner-loop reductions can be used in the inner-loop, so the
825 current checks are too strict. */
827 if (loop->inner)
828 vect_analyze_scalar_cycles_1 (loop_vinfo, loop->inner);
832 /* Function vect_get_loop_niters.
834 Determine how many iterations the loop is executed and place it
835 in NUMBER_OF_ITERATIONS. Place the number of latch iterations
836 in NUMBER_OF_ITERATIONSM1.
838 Return the loop exit condition. */
841 static gcond *
842 vect_get_loop_niters (struct loop *loop, tree *number_of_iterations,
843 tree *number_of_iterationsm1)
845 tree niters;
847 if (dump_enabled_p ())
848 dump_printf_loc (MSG_NOTE, vect_location,
849 "=== get_loop_niters ===\n");
851 niters = number_of_latch_executions (loop);
852 *number_of_iterationsm1 = niters;
854 /* We want the number of loop header executions which is the number
855 of latch executions plus one.
856 ??? For UINT_MAX latch executions this number overflows to zero
857 for loops like do { n++; } while (n != 0); */
858 if (niters && !chrec_contains_undetermined (niters))
859 niters = fold_build2 (PLUS_EXPR, TREE_TYPE (niters), unshare_expr (niters),
860 build_int_cst (TREE_TYPE (niters), 1));
861 *number_of_iterations = niters;
863 return get_loop_exit_condition (loop);
867 /* Function bb_in_loop_p
869 Used as predicate for dfs order traversal of the loop bbs. */
871 static bool
872 bb_in_loop_p (const_basic_block bb, const void *data)
874 const struct loop *const loop = (const struct loop *)data;
875 if (flow_bb_inside_loop_p (loop, bb))
876 return true;
877 return false;
881 /* Function new_loop_vec_info.
883 Create and initialize a new loop_vec_info struct for LOOP, as well as
884 stmt_vec_info structs for all the stmts in LOOP. */
886 static loop_vec_info
887 new_loop_vec_info (struct loop *loop)
889 loop_vec_info res;
890 basic_block *bbs;
891 gimple_stmt_iterator si;
892 unsigned int i, nbbs;
894 res = (loop_vec_info) xcalloc (1, sizeof (struct _loop_vec_info));
895 LOOP_VINFO_LOOP (res) = loop;
897 bbs = get_loop_body (loop);
899 /* Create/Update stmt_info for all stmts in the loop. */
900 for (i = 0; i < loop->num_nodes; i++)
902 basic_block bb = bbs[i];
904 /* BBs in a nested inner-loop will have been already processed (because
905 we will have called vect_analyze_loop_form for any nested inner-loop).
906 Therefore, for stmts in an inner-loop we just want to update the
907 STMT_VINFO_LOOP_VINFO field of their stmt_info to point to the new
908 loop_info of the outer-loop we are currently considering to vectorize
909 (instead of the loop_info of the inner-loop).
910 For stmts in other BBs we need to create a stmt_info from scratch. */
911 if (bb->loop_father != loop)
913 /* Inner-loop bb. */
914 gcc_assert (loop->inner && bb->loop_father == loop->inner);
915 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
917 gimple phi = gsi_stmt (si);
918 stmt_vec_info stmt_info = vinfo_for_stmt (phi);
919 loop_vec_info inner_loop_vinfo =
920 STMT_VINFO_LOOP_VINFO (stmt_info);
921 gcc_assert (loop->inner == LOOP_VINFO_LOOP (inner_loop_vinfo));
922 STMT_VINFO_LOOP_VINFO (stmt_info) = res;
924 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
926 gimple stmt = gsi_stmt (si);
927 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
928 loop_vec_info inner_loop_vinfo =
929 STMT_VINFO_LOOP_VINFO (stmt_info);
930 gcc_assert (loop->inner == LOOP_VINFO_LOOP (inner_loop_vinfo));
931 STMT_VINFO_LOOP_VINFO (stmt_info) = res;
934 else
936 /* bb in current nest. */
937 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
939 gimple phi = gsi_stmt (si);
940 gimple_set_uid (phi, 0);
941 set_vinfo_for_stmt (phi, new_stmt_vec_info (phi, res, NULL));
944 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
946 gimple stmt = gsi_stmt (si);
947 gimple_set_uid (stmt, 0);
948 set_vinfo_for_stmt (stmt, new_stmt_vec_info (stmt, res, NULL));
953 /* CHECKME: We want to visit all BBs before their successors (except for
954 latch blocks, for which this assertion wouldn't hold). In the simple
955 case of the loop forms we allow, a dfs order of the BBs would the same
956 as reversed postorder traversal, so we are safe. */
958 free (bbs);
959 bbs = XCNEWVEC (basic_block, loop->num_nodes);
960 nbbs = dfs_enumerate_from (loop->header, 0, bb_in_loop_p,
961 bbs, loop->num_nodes, loop);
962 gcc_assert (nbbs == loop->num_nodes);
964 LOOP_VINFO_BBS (res) = bbs;
965 LOOP_VINFO_NITERSM1 (res) = NULL;
966 LOOP_VINFO_NITERS (res) = NULL;
967 LOOP_VINFO_NITERS_UNCHANGED (res) = NULL;
968 LOOP_VINFO_COST_MODEL_MIN_ITERS (res) = 0;
969 LOOP_VINFO_COST_MODEL_THRESHOLD (res) = 0;
970 LOOP_VINFO_VECTORIZABLE_P (res) = 0;
971 LOOP_VINFO_PEELING_FOR_ALIGNMENT (res) = 0;
972 LOOP_VINFO_VECT_FACTOR (res) = 0;
973 LOOP_VINFO_LOOP_NEST (res).create (3);
974 LOOP_VINFO_DATAREFS (res).create (10);
975 LOOP_VINFO_DDRS (res).create (10 * 10);
976 LOOP_VINFO_UNALIGNED_DR (res) = NULL;
977 LOOP_VINFO_MAY_MISALIGN_STMTS (res).create (
978 PARAM_VALUE (PARAM_VECT_MAX_VERSION_FOR_ALIGNMENT_CHECKS));
979 LOOP_VINFO_MAY_ALIAS_DDRS (res).create (
980 PARAM_VALUE (PARAM_VECT_MAX_VERSION_FOR_ALIAS_CHECKS));
981 LOOP_VINFO_GROUPED_STORES (res).create (10);
982 LOOP_VINFO_REDUCTIONS (res).create (10);
983 LOOP_VINFO_REDUCTION_CHAINS (res).create (10);
984 LOOP_VINFO_SLP_INSTANCES (res).create (10);
985 LOOP_VINFO_SLP_UNROLLING_FACTOR (res) = 1;
986 LOOP_VINFO_TARGET_COST_DATA (res) = init_cost (loop);
987 LOOP_VINFO_PEELING_FOR_GAPS (res) = false;
988 LOOP_VINFO_PEELING_FOR_NITER (res) = false;
989 LOOP_VINFO_OPERANDS_SWAPPED (res) = false;
991 return res;
995 /* Function destroy_loop_vec_info.
997 Free LOOP_VINFO struct, as well as all the stmt_vec_info structs of all the
998 stmts in the loop. */
1000 void
1001 destroy_loop_vec_info (loop_vec_info loop_vinfo, bool clean_stmts)
1003 struct loop *loop;
1004 basic_block *bbs;
1005 int nbbs;
1006 gimple_stmt_iterator si;
1007 int j;
1008 vec<slp_instance> slp_instances;
1009 slp_instance instance;
1010 bool swapped;
1012 if (!loop_vinfo)
1013 return;
1015 loop = LOOP_VINFO_LOOP (loop_vinfo);
1017 bbs = LOOP_VINFO_BBS (loop_vinfo);
1018 nbbs = clean_stmts ? loop->num_nodes : 0;
1019 swapped = LOOP_VINFO_OPERANDS_SWAPPED (loop_vinfo);
1021 for (j = 0; j < nbbs; j++)
1023 basic_block bb = bbs[j];
1024 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
1025 free_stmt_vec_info (gsi_stmt (si));
1027 for (si = gsi_start_bb (bb); !gsi_end_p (si); )
1029 gimple stmt = gsi_stmt (si);
1031 /* We may have broken canonical form by moving a constant
1032 into RHS1 of a commutative op. Fix such occurrences. */
1033 if (swapped && is_gimple_assign (stmt))
1035 enum tree_code code = gimple_assign_rhs_code (stmt);
1037 if ((code == PLUS_EXPR
1038 || code == POINTER_PLUS_EXPR
1039 || code == MULT_EXPR)
1040 && CONSTANT_CLASS_P (gimple_assign_rhs1 (stmt)))
1041 swap_ssa_operands (stmt,
1042 gimple_assign_rhs1_ptr (stmt),
1043 gimple_assign_rhs2_ptr (stmt));
1046 /* Free stmt_vec_info. */
1047 free_stmt_vec_info (stmt);
1048 gsi_next (&si);
1052 free (LOOP_VINFO_BBS (loop_vinfo));
1053 vect_destroy_datarefs (loop_vinfo, NULL);
1054 free_dependence_relations (LOOP_VINFO_DDRS (loop_vinfo));
1055 LOOP_VINFO_LOOP_NEST (loop_vinfo).release ();
1056 LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo).release ();
1057 LOOP_VINFO_MAY_ALIAS_DDRS (loop_vinfo).release ();
1058 slp_instances = LOOP_VINFO_SLP_INSTANCES (loop_vinfo);
1059 FOR_EACH_VEC_ELT (slp_instances, j, instance)
1060 vect_free_slp_instance (instance);
1062 LOOP_VINFO_SLP_INSTANCES (loop_vinfo).release ();
1063 LOOP_VINFO_GROUPED_STORES (loop_vinfo).release ();
1064 LOOP_VINFO_REDUCTIONS (loop_vinfo).release ();
1065 LOOP_VINFO_REDUCTION_CHAINS (loop_vinfo).release ();
1067 delete LOOP_VINFO_PEELING_HTAB (loop_vinfo);
1068 LOOP_VINFO_PEELING_HTAB (loop_vinfo) = NULL;
1070 destroy_cost_data (LOOP_VINFO_TARGET_COST_DATA (loop_vinfo));
1072 free (loop_vinfo);
1073 loop->aux = NULL;
1077 /* Function vect_analyze_loop_1.
1079 Apply a set of analyses on LOOP, and create a loop_vec_info struct
1080 for it. The different analyses will record information in the
1081 loop_vec_info struct. This is a subset of the analyses applied in
1082 vect_analyze_loop, to be applied on an inner-loop nested in the loop
1083 that is now considered for (outer-loop) vectorization. */
1085 static loop_vec_info
1086 vect_analyze_loop_1 (struct loop *loop)
1088 loop_vec_info loop_vinfo;
1090 if (dump_enabled_p ())
1091 dump_printf_loc (MSG_NOTE, vect_location,
1092 "===== analyze_loop_nest_1 =====\n");
1094 /* Check the CFG characteristics of the loop (nesting, entry/exit, etc. */
1096 loop_vinfo = vect_analyze_loop_form (loop);
1097 if (!loop_vinfo)
1099 if (dump_enabled_p ())
1100 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1101 "bad inner-loop form.\n");
1102 return NULL;
1105 return loop_vinfo;
1109 /* Function vect_analyze_loop_form.
1111 Verify that certain CFG restrictions hold, including:
1112 - the loop has a pre-header
1113 - the loop has a single entry and exit
1114 - the loop exit condition is simple enough, and the number of iterations
1115 can be analyzed (a countable loop). */
1117 loop_vec_info
1118 vect_analyze_loop_form (struct loop *loop)
1120 loop_vec_info loop_vinfo;
1121 gcond *loop_cond;
1122 tree number_of_iterations = NULL, number_of_iterationsm1 = NULL;
1123 loop_vec_info inner_loop_vinfo = NULL;
1125 if (dump_enabled_p ())
1126 dump_printf_loc (MSG_NOTE, vect_location,
1127 "=== vect_analyze_loop_form ===\n");
1129 /* Different restrictions apply when we are considering an inner-most loop,
1130 vs. an outer (nested) loop.
1131 (FORNOW. May want to relax some of these restrictions in the future). */
1133 if (!loop->inner)
1135 /* Inner-most loop. We currently require that the number of BBs is
1136 exactly 2 (the header and latch). Vectorizable inner-most loops
1137 look like this:
1139 (pre-header)
1141 header <--------+
1142 | | |
1143 | +--> latch --+
1145 (exit-bb) */
1147 if (loop->num_nodes != 2)
1149 if (dump_enabled_p ())
1150 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1151 "not vectorized: control flow in loop.\n");
1152 return NULL;
1155 if (empty_block_p (loop->header))
1157 if (dump_enabled_p ())
1158 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1159 "not vectorized: empty loop.\n");
1160 return NULL;
1163 else
1165 struct loop *innerloop = loop->inner;
1166 edge entryedge;
1168 /* Nested loop. We currently require that the loop is doubly-nested,
1169 contains a single inner loop, and the number of BBs is exactly 5.
1170 Vectorizable outer-loops look like this:
1172 (pre-header)
1174 header <---+
1176 inner-loop |
1178 tail ------+
1180 (exit-bb)
1182 The inner-loop has the properties expected of inner-most loops
1183 as described above. */
1185 if ((loop->inner)->inner || (loop->inner)->next)
1187 if (dump_enabled_p ())
1188 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1189 "not vectorized: multiple nested loops.\n");
1190 return NULL;
1193 /* Analyze the inner-loop. */
1194 inner_loop_vinfo = vect_analyze_loop_1 (loop->inner);
1195 if (!inner_loop_vinfo)
1197 if (dump_enabled_p ())
1198 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1199 "not vectorized: Bad inner loop.\n");
1200 return NULL;
1203 if (!expr_invariant_in_loop_p (loop,
1204 LOOP_VINFO_NITERS (inner_loop_vinfo)))
1206 if (dump_enabled_p ())
1207 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1208 "not vectorized: inner-loop count not"
1209 " invariant.\n");
1210 destroy_loop_vec_info (inner_loop_vinfo, true);
1211 return NULL;
1214 if (loop->num_nodes != 5)
1216 if (dump_enabled_p ())
1217 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1218 "not vectorized: control flow in loop.\n");
1219 destroy_loop_vec_info (inner_loop_vinfo, true);
1220 return NULL;
1223 gcc_assert (EDGE_COUNT (innerloop->header->preds) == 2);
1224 entryedge = EDGE_PRED (innerloop->header, 0);
1225 if (EDGE_PRED (innerloop->header, 0)->src == innerloop->latch)
1226 entryedge = EDGE_PRED (innerloop->header, 1);
1228 if (entryedge->src != loop->header
1229 || !single_exit (innerloop)
1230 || single_exit (innerloop)->dest != EDGE_PRED (loop->latch, 0)->src)
1232 if (dump_enabled_p ())
1233 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1234 "not vectorized: unsupported outerloop form.\n");
1235 destroy_loop_vec_info (inner_loop_vinfo, true);
1236 return NULL;
1239 if (dump_enabled_p ())
1240 dump_printf_loc (MSG_NOTE, vect_location,
1241 "Considering outer-loop vectorization.\n");
1244 if (!single_exit (loop)
1245 || EDGE_COUNT (loop->header->preds) != 2)
1247 if (dump_enabled_p ())
1249 if (!single_exit (loop))
1250 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1251 "not vectorized: multiple exits.\n");
1252 else if (EDGE_COUNT (loop->header->preds) != 2)
1253 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1254 "not vectorized: too many incoming edges.\n");
1256 if (inner_loop_vinfo)
1257 destroy_loop_vec_info (inner_loop_vinfo, true);
1258 return NULL;
1261 /* We assume that the loop exit condition is at the end of the loop. i.e,
1262 that the loop is represented as a do-while (with a proper if-guard
1263 before the loop if needed), where the loop header contains all the
1264 executable statements, and the latch is empty. */
1265 if (!empty_block_p (loop->latch)
1266 || !gimple_seq_empty_p (phi_nodes (loop->latch)))
1268 if (dump_enabled_p ())
1269 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1270 "not vectorized: latch block not empty.\n");
1271 if (inner_loop_vinfo)
1272 destroy_loop_vec_info (inner_loop_vinfo, true);
1273 return NULL;
1276 /* Make sure there exists a single-predecessor exit bb: */
1277 if (!single_pred_p (single_exit (loop)->dest))
1279 edge e = single_exit (loop);
1280 if (!(e->flags & EDGE_ABNORMAL))
1282 split_loop_exit_edge (e);
1283 if (dump_enabled_p ())
1284 dump_printf (MSG_NOTE, "split exit edge.\n");
1286 else
1288 if (dump_enabled_p ())
1289 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1290 "not vectorized: abnormal loop exit edge.\n");
1291 if (inner_loop_vinfo)
1292 destroy_loop_vec_info (inner_loop_vinfo, true);
1293 return NULL;
1297 loop_cond = vect_get_loop_niters (loop, &number_of_iterations,
1298 &number_of_iterationsm1);
1299 if (!loop_cond)
1301 if (dump_enabled_p ())
1302 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1303 "not vectorized: complicated exit condition.\n");
1304 if (inner_loop_vinfo)
1305 destroy_loop_vec_info (inner_loop_vinfo, true);
1306 return NULL;
1309 if (!number_of_iterations
1310 || chrec_contains_undetermined (number_of_iterations))
1312 if (dump_enabled_p ())
1313 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1314 "not vectorized: number of iterations cannot be "
1315 "computed.\n");
1316 if (inner_loop_vinfo)
1317 destroy_loop_vec_info (inner_loop_vinfo, true);
1318 return NULL;
1321 if (integer_zerop (number_of_iterations))
1323 if (dump_enabled_p ())
1324 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1325 "not vectorized: number of iterations = 0.\n");
1326 if (inner_loop_vinfo)
1327 destroy_loop_vec_info (inner_loop_vinfo, true);
1328 return NULL;
1331 loop_vinfo = new_loop_vec_info (loop);
1332 LOOP_VINFO_NITERSM1 (loop_vinfo) = number_of_iterationsm1;
1333 LOOP_VINFO_NITERS (loop_vinfo) = number_of_iterations;
1334 LOOP_VINFO_NITERS_UNCHANGED (loop_vinfo) = number_of_iterations;
1336 if (!LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo))
1338 if (dump_enabled_p ())
1340 dump_printf_loc (MSG_NOTE, vect_location,
1341 "Symbolic number of iterations is ");
1342 dump_generic_expr (MSG_NOTE, TDF_DETAILS, number_of_iterations);
1343 dump_printf (MSG_NOTE, "\n");
1347 STMT_VINFO_TYPE (vinfo_for_stmt (loop_cond)) = loop_exit_ctrl_vec_info_type;
1349 /* CHECKME: May want to keep it around it in the future. */
1350 if (inner_loop_vinfo)
1351 destroy_loop_vec_info (inner_loop_vinfo, false);
1353 gcc_assert (!loop->aux);
1354 loop->aux = loop_vinfo;
1355 return loop_vinfo;
1359 /* Function vect_analyze_loop_operations.
1361 Scan the loop stmts and make sure they are all vectorizable. */
1363 static bool
1364 vect_analyze_loop_operations (loop_vec_info loop_vinfo, bool slp)
1366 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
1367 basic_block *bbs = LOOP_VINFO_BBS (loop_vinfo);
1368 int nbbs = loop->num_nodes;
1369 unsigned int vectorization_factor = 0;
1370 int i;
1371 stmt_vec_info stmt_info;
1372 bool need_to_vectorize = false;
1373 int min_profitable_iters;
1374 int min_scalar_loop_bound;
1375 unsigned int th;
1376 bool only_slp_in_loop = true, ok;
1377 HOST_WIDE_INT max_niter;
1378 HOST_WIDE_INT estimated_niter;
1379 int min_profitable_estimate;
1381 if (dump_enabled_p ())
1382 dump_printf_loc (MSG_NOTE, vect_location,
1383 "=== vect_analyze_loop_operations ===\n");
1385 gcc_assert (LOOP_VINFO_VECT_FACTOR (loop_vinfo));
1386 vectorization_factor = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
1387 if (slp)
1389 /* If all the stmts in the loop can be SLPed, we perform only SLP, and
1390 vectorization factor of the loop is the unrolling factor required by
1391 the SLP instances. If that unrolling factor is 1, we say, that we
1392 perform pure SLP on loop - cross iteration parallelism is not
1393 exploited. */
1394 for (i = 0; i < nbbs; i++)
1396 basic_block bb = bbs[i];
1397 for (gimple_stmt_iterator si = gsi_start_bb (bb); !gsi_end_p (si);
1398 gsi_next (&si))
1400 gimple stmt = gsi_stmt (si);
1401 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1402 gcc_assert (stmt_info);
1403 if ((STMT_VINFO_RELEVANT_P (stmt_info)
1404 || VECTORIZABLE_CYCLE_DEF (STMT_VINFO_DEF_TYPE (stmt_info)))
1405 && !PURE_SLP_STMT (stmt_info))
1406 /* STMT needs both SLP and loop-based vectorization. */
1407 only_slp_in_loop = false;
1411 if (only_slp_in_loop)
1412 vectorization_factor = LOOP_VINFO_SLP_UNROLLING_FACTOR (loop_vinfo);
1413 else
1414 vectorization_factor = least_common_multiple (vectorization_factor,
1415 LOOP_VINFO_SLP_UNROLLING_FACTOR (loop_vinfo));
1417 LOOP_VINFO_VECT_FACTOR (loop_vinfo) = vectorization_factor;
1418 if (dump_enabled_p ())
1419 dump_printf_loc (MSG_NOTE, vect_location,
1420 "Updating vectorization factor to %d\n",
1421 vectorization_factor);
1424 for (i = 0; i < nbbs; i++)
1426 basic_block bb = bbs[i];
1428 for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
1429 gsi_next (&si))
1431 gphi *phi = si.phi ();
1432 ok = true;
1434 stmt_info = vinfo_for_stmt (phi);
1435 if (dump_enabled_p ())
1437 dump_printf_loc (MSG_NOTE, vect_location, "examining phi: ");
1438 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, phi, 0);
1439 dump_printf (MSG_NOTE, "\n");
1442 /* Inner-loop loop-closed exit phi in outer-loop vectorization
1443 (i.e., a phi in the tail of the outer-loop). */
1444 if (! is_loop_header_bb_p (bb))
1446 /* FORNOW: we currently don't support the case that these phis
1447 are not used in the outerloop (unless it is double reduction,
1448 i.e., this phi is vect_reduction_def), cause this case
1449 requires to actually do something here. */
1450 if ((!STMT_VINFO_RELEVANT_P (stmt_info)
1451 || STMT_VINFO_LIVE_P (stmt_info))
1452 && STMT_VINFO_DEF_TYPE (stmt_info)
1453 != vect_double_reduction_def)
1455 if (dump_enabled_p ())
1456 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1457 "Unsupported loop-closed phi in "
1458 "outer-loop.\n");
1459 return false;
1462 /* If PHI is used in the outer loop, we check that its operand
1463 is defined in the inner loop. */
1464 if (STMT_VINFO_RELEVANT_P (stmt_info))
1466 tree phi_op;
1467 gimple op_def_stmt;
1469 if (gimple_phi_num_args (phi) != 1)
1470 return false;
1472 phi_op = PHI_ARG_DEF (phi, 0);
1473 if (TREE_CODE (phi_op) != SSA_NAME)
1474 return false;
1476 op_def_stmt = SSA_NAME_DEF_STMT (phi_op);
1477 if (gimple_nop_p (op_def_stmt)
1478 || !flow_bb_inside_loop_p (loop, gimple_bb (op_def_stmt))
1479 || !vinfo_for_stmt (op_def_stmt))
1480 return false;
1482 if (STMT_VINFO_RELEVANT (vinfo_for_stmt (op_def_stmt))
1483 != vect_used_in_outer
1484 && STMT_VINFO_RELEVANT (vinfo_for_stmt (op_def_stmt))
1485 != vect_used_in_outer_by_reduction)
1486 return false;
1489 continue;
1492 gcc_assert (stmt_info);
1494 if (STMT_VINFO_LIVE_P (stmt_info))
1496 /* FORNOW: not yet supported. */
1497 if (dump_enabled_p ())
1498 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1499 "not vectorized: value used after loop.\n");
1500 return false;
1503 if (STMT_VINFO_RELEVANT (stmt_info) == vect_used_in_scope
1504 && STMT_VINFO_DEF_TYPE (stmt_info) != vect_induction_def)
1506 /* A scalar-dependence cycle that we don't support. */
1507 if (dump_enabled_p ())
1508 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1509 "not vectorized: scalar dependence cycle.\n");
1510 return false;
1513 if (STMT_VINFO_RELEVANT_P (stmt_info))
1515 need_to_vectorize = true;
1516 if (STMT_VINFO_DEF_TYPE (stmt_info) == vect_induction_def)
1517 ok = vectorizable_induction (phi, NULL, NULL);
1520 if (!ok)
1522 if (dump_enabled_p ())
1524 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1525 "not vectorized: relevant phi not "
1526 "supported: ");
1527 dump_gimple_stmt (MSG_MISSED_OPTIMIZATION, TDF_SLIM, phi, 0);
1528 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
1530 return false;
1534 for (gimple_stmt_iterator si = gsi_start_bb (bb); !gsi_end_p (si);
1535 gsi_next (&si))
1537 gimple stmt = gsi_stmt (si);
1538 if (!gimple_clobber_p (stmt)
1539 && !vect_analyze_stmt (stmt, &need_to_vectorize, NULL))
1540 return false;
1542 } /* bbs */
1544 /* All operations in the loop are either irrelevant (deal with loop
1545 control, or dead), or only used outside the loop and can be moved
1546 out of the loop (e.g. invariants, inductions). The loop can be
1547 optimized away by scalar optimizations. We're better off not
1548 touching this loop. */
1549 if (!need_to_vectorize)
1551 if (dump_enabled_p ())
1552 dump_printf_loc (MSG_NOTE, vect_location,
1553 "All the computation can be taken out of the loop.\n");
1554 if (dump_enabled_p ())
1555 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1556 "not vectorized: redundant loop. no profit to "
1557 "vectorize.\n");
1558 return false;
1561 if (LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo) && dump_enabled_p ())
1562 dump_printf_loc (MSG_NOTE, vect_location,
1563 "vectorization_factor = %d, niters = "
1564 HOST_WIDE_INT_PRINT_DEC "\n", vectorization_factor,
1565 LOOP_VINFO_INT_NITERS (loop_vinfo));
1567 if ((LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo)
1568 && (LOOP_VINFO_INT_NITERS (loop_vinfo) < vectorization_factor))
1569 || ((max_niter = max_stmt_executions_int (loop)) != -1
1570 && (unsigned HOST_WIDE_INT) max_niter < vectorization_factor))
1572 if (dump_enabled_p ())
1573 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1574 "not vectorized: iteration count too small.\n");
1575 if (dump_enabled_p ())
1576 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1577 "not vectorized: iteration count smaller than "
1578 "vectorization factor.\n");
1579 return false;
1582 /* Analyze cost. Decide if worth while to vectorize. */
1584 /* Once VF is set, SLP costs should be updated since the number of created
1585 vector stmts depends on VF. */
1586 vect_update_slp_costs_according_to_vf (loop_vinfo);
1588 vect_estimate_min_profitable_iters (loop_vinfo, &min_profitable_iters,
1589 &min_profitable_estimate);
1590 LOOP_VINFO_COST_MODEL_MIN_ITERS (loop_vinfo) = min_profitable_iters;
1592 if (min_profitable_iters < 0)
1594 if (dump_enabled_p ())
1595 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1596 "not vectorized: vectorization not profitable.\n");
1597 if (dump_enabled_p ())
1598 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1599 "not vectorized: vector version will never be "
1600 "profitable.\n");
1601 return false;
1604 min_scalar_loop_bound = ((PARAM_VALUE (PARAM_MIN_VECT_LOOP_BOUND)
1605 * vectorization_factor) - 1);
1608 /* Use the cost model only if it is more conservative than user specified
1609 threshold. */
1611 th = (unsigned) min_scalar_loop_bound;
1612 if (min_profitable_iters
1613 && (!min_scalar_loop_bound
1614 || min_profitable_iters > min_scalar_loop_bound))
1615 th = (unsigned) min_profitable_iters;
1617 LOOP_VINFO_COST_MODEL_THRESHOLD (loop_vinfo) = th;
1619 if (LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo)
1620 && LOOP_VINFO_INT_NITERS (loop_vinfo) <= th)
1622 if (dump_enabled_p ())
1623 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1624 "not vectorized: vectorization not profitable.\n");
1625 if (dump_enabled_p ())
1626 dump_printf_loc (MSG_NOTE, vect_location,
1627 "not vectorized: iteration count smaller than user "
1628 "specified loop bound parameter or minimum profitable "
1629 "iterations (whichever is more conservative).\n");
1630 return false;
1633 if ((estimated_niter = estimated_stmt_executions_int (loop)) != -1
1634 && ((unsigned HOST_WIDE_INT) estimated_niter
1635 <= MAX (th, (unsigned)min_profitable_estimate)))
1637 if (dump_enabled_p ())
1638 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1639 "not vectorized: estimated iteration count too "
1640 "small.\n");
1641 if (dump_enabled_p ())
1642 dump_printf_loc (MSG_NOTE, vect_location,
1643 "not vectorized: estimated iteration count smaller "
1644 "than specified loop bound parameter or minimum "
1645 "profitable iterations (whichever is more "
1646 "conservative).\n");
1647 return false;
1650 return true;
1654 /* Function vect_analyze_loop_2.
1656 Apply a set of analyses on LOOP, and create a loop_vec_info struct
1657 for it. The different analyses will record information in the
1658 loop_vec_info struct. */
1659 static bool
1660 vect_analyze_loop_2 (loop_vec_info loop_vinfo)
1662 bool ok, slp = false;
1663 int max_vf = MAX_VECTORIZATION_FACTOR;
1664 int min_vf = 2;
1665 unsigned int th;
1666 unsigned int n_stmts = 0;
1668 /* Find all data references in the loop (which correspond to vdefs/vuses)
1669 and analyze their evolution in the loop. Also adjust the minimal
1670 vectorization factor according to the loads and stores.
1672 FORNOW: Handle only simple, array references, which
1673 alignment can be forced, and aligned pointer-references. */
1675 ok = vect_analyze_data_refs (loop_vinfo, NULL, &min_vf, &n_stmts);
1676 if (!ok)
1678 if (dump_enabled_p ())
1679 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1680 "bad data references.\n");
1681 return false;
1684 /* Classify all cross-iteration scalar data-flow cycles.
1685 Cross-iteration cycles caused by virtual phis are analyzed separately. */
1687 vect_analyze_scalar_cycles (loop_vinfo);
1689 vect_pattern_recog (loop_vinfo, NULL);
1691 /* Analyze the access patterns of the data-refs in the loop (consecutive,
1692 complex, etc.). FORNOW: Only handle consecutive access pattern. */
1694 ok = vect_analyze_data_ref_accesses (loop_vinfo, NULL);
1695 if (!ok)
1697 if (dump_enabled_p ())
1698 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1699 "bad data access.\n");
1700 return false;
1703 /* Data-flow analysis to detect stmts that do not need to be vectorized. */
1705 ok = vect_mark_stmts_to_be_vectorized (loop_vinfo);
1706 if (!ok)
1708 if (dump_enabled_p ())
1709 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1710 "unexpected pattern.\n");
1711 return false;
1714 /* Analyze data dependences between the data-refs in the loop
1715 and adjust the maximum vectorization factor according to
1716 the dependences.
1717 FORNOW: fail at the first data dependence that we encounter. */
1719 ok = vect_analyze_data_ref_dependences (loop_vinfo, &max_vf);
1720 if (!ok
1721 || max_vf < min_vf)
1723 if (dump_enabled_p ())
1724 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1725 "bad data dependence.\n");
1726 return false;
1729 ok = vect_determine_vectorization_factor (loop_vinfo);
1730 if (!ok)
1732 if (dump_enabled_p ())
1733 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1734 "can't determine vectorization factor.\n");
1735 return false;
1737 if (max_vf < LOOP_VINFO_VECT_FACTOR (loop_vinfo))
1739 if (dump_enabled_p ())
1740 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1741 "bad data dependence.\n");
1742 return false;
1745 /* Analyze the alignment of the data-refs in the loop.
1746 Fail if a data reference is found that cannot be vectorized. */
1748 ok = vect_analyze_data_refs_alignment (loop_vinfo, NULL);
1749 if (!ok)
1751 if (dump_enabled_p ())
1752 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1753 "bad data alignment.\n");
1754 return false;
1757 /* Prune the list of ddrs to be tested at run-time by versioning for alias.
1758 It is important to call pruning after vect_analyze_data_ref_accesses,
1759 since we use grouping information gathered by interleaving analysis. */
1760 ok = vect_prune_runtime_alias_test_list (loop_vinfo);
1761 if (!ok)
1763 if (dump_enabled_p ())
1764 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1765 "number of versioning for alias "
1766 "run-time tests exceeds %d "
1767 "(--param vect-max-version-for-alias-checks)\n",
1768 PARAM_VALUE (PARAM_VECT_MAX_VERSION_FOR_ALIAS_CHECKS));
1769 return false;
1772 /* This pass will decide on using loop versioning and/or loop peeling in
1773 order to enhance the alignment of data references in the loop. */
1775 ok = vect_enhance_data_refs_alignment (loop_vinfo);
1776 if (!ok)
1778 if (dump_enabled_p ())
1779 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1780 "bad data alignment.\n");
1781 return false;
1784 /* Check the SLP opportunities in the loop, analyze and build SLP trees. */
1785 ok = vect_analyze_slp (loop_vinfo, NULL, n_stmts);
1786 if (ok)
1788 /* Decide which possible SLP instances to SLP. */
1789 slp = vect_make_slp_decision (loop_vinfo);
1791 /* Find stmts that need to be both vectorized and SLPed. */
1792 vect_detect_hybrid_slp (loop_vinfo);
1794 else
1795 return false;
1797 /* Scan all the operations in the loop and make sure they are
1798 vectorizable. */
1800 ok = vect_analyze_loop_operations (loop_vinfo, slp);
1801 if (!ok)
1803 if (dump_enabled_p ())
1804 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1805 "bad operation or unsupported loop bound.\n");
1806 return false;
1809 /* Decide whether we need to create an epilogue loop to handle
1810 remaining scalar iterations. */
1811 th = ((LOOP_VINFO_COST_MODEL_THRESHOLD (loop_vinfo) + 1)
1812 / LOOP_VINFO_VECT_FACTOR (loop_vinfo))
1813 * LOOP_VINFO_VECT_FACTOR (loop_vinfo);
1815 if (LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo)
1816 && LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo) > 0)
1818 if (ctz_hwi (LOOP_VINFO_INT_NITERS (loop_vinfo)
1819 - LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo))
1820 < exact_log2 (LOOP_VINFO_VECT_FACTOR (loop_vinfo)))
1821 LOOP_VINFO_PEELING_FOR_NITER (loop_vinfo) = true;
1823 else if (LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo)
1824 || (tree_ctz (LOOP_VINFO_NITERS (loop_vinfo))
1825 < (unsigned)exact_log2 (LOOP_VINFO_VECT_FACTOR (loop_vinfo))
1826 /* In case of versioning, check if the maximum number of
1827 iterations is greater than th. If they are identical,
1828 the epilogue is unnecessary. */
1829 && ((!LOOP_REQUIRES_VERSIONING_FOR_ALIAS (loop_vinfo)
1830 && !LOOP_REQUIRES_VERSIONING_FOR_ALIGNMENT (loop_vinfo))
1831 || (unsigned HOST_WIDE_INT)max_stmt_executions_int
1832 (LOOP_VINFO_LOOP (loop_vinfo)) > th)))
1833 LOOP_VINFO_PEELING_FOR_NITER (loop_vinfo) = true;
1835 /* If an epilogue loop is required make sure we can create one. */
1836 if (LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo)
1837 || LOOP_VINFO_PEELING_FOR_NITER (loop_vinfo))
1839 if (dump_enabled_p ())
1840 dump_printf_loc (MSG_NOTE, vect_location, "epilog loop required\n");
1841 if (!vect_can_advance_ivs_p (loop_vinfo)
1842 || !slpeel_can_duplicate_loop_p (LOOP_VINFO_LOOP (loop_vinfo),
1843 single_exit (LOOP_VINFO_LOOP
1844 (loop_vinfo))))
1846 if (dump_enabled_p ())
1847 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1848 "not vectorized: can't create required "
1849 "epilog loop\n");
1850 return false;
1854 return true;
1857 /* Function vect_analyze_loop.
1859 Apply a set of analyses on LOOP, and create a loop_vec_info struct
1860 for it. The different analyses will record information in the
1861 loop_vec_info struct. */
1862 loop_vec_info
1863 vect_analyze_loop (struct loop *loop)
1865 loop_vec_info loop_vinfo;
1866 unsigned int vector_sizes;
1868 /* Autodetect first vector size we try. */
1869 current_vector_size = 0;
1870 vector_sizes = targetm.vectorize.autovectorize_vector_sizes ();
1872 if (dump_enabled_p ())
1873 dump_printf_loc (MSG_NOTE, vect_location,
1874 "===== analyze_loop_nest =====\n");
1876 if (loop_outer (loop)
1877 && loop_vec_info_for_loop (loop_outer (loop))
1878 && LOOP_VINFO_VECTORIZABLE_P (loop_vec_info_for_loop (loop_outer (loop))))
1880 if (dump_enabled_p ())
1881 dump_printf_loc (MSG_NOTE, vect_location,
1882 "outer-loop already vectorized.\n");
1883 return NULL;
1886 while (1)
1888 /* Check the CFG characteristics of the loop (nesting, entry/exit). */
1889 loop_vinfo = vect_analyze_loop_form (loop);
1890 if (!loop_vinfo)
1892 if (dump_enabled_p ())
1893 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
1894 "bad loop form.\n");
1895 return NULL;
1898 if (vect_analyze_loop_2 (loop_vinfo))
1900 LOOP_VINFO_VECTORIZABLE_P (loop_vinfo) = 1;
1902 return loop_vinfo;
1905 destroy_loop_vec_info (loop_vinfo, true);
1907 vector_sizes &= ~current_vector_size;
1908 if (vector_sizes == 0
1909 || current_vector_size == 0)
1910 return NULL;
1912 /* Try the next biggest vector size. */
1913 current_vector_size = 1 << floor_log2 (vector_sizes);
1914 if (dump_enabled_p ())
1915 dump_printf_loc (MSG_NOTE, vect_location,
1916 "***** Re-trying analysis with "
1917 "vector size %d\n", current_vector_size);
1922 /* Function reduction_code_for_scalar_code
1924 Input:
1925 CODE - tree_code of a reduction operations.
1927 Output:
1928 REDUC_CODE - the corresponding tree-code to be used to reduce the
1929 vector of partial results into a single scalar result, or ERROR_MARK
1930 if the operation is a supported reduction operation, but does not have
1931 such a tree-code.
1933 Return FALSE if CODE currently cannot be vectorized as reduction. */
1935 static bool
1936 reduction_code_for_scalar_code (enum tree_code code,
1937 enum tree_code *reduc_code)
1939 switch (code)
1941 case MAX_EXPR:
1942 *reduc_code = REDUC_MAX_EXPR;
1943 return true;
1945 case MIN_EXPR:
1946 *reduc_code = REDUC_MIN_EXPR;
1947 return true;
1949 case PLUS_EXPR:
1950 *reduc_code = REDUC_PLUS_EXPR;
1951 return true;
1953 case MULT_EXPR:
1954 case MINUS_EXPR:
1955 case BIT_IOR_EXPR:
1956 case BIT_XOR_EXPR:
1957 case BIT_AND_EXPR:
1958 *reduc_code = ERROR_MARK;
1959 return true;
1961 default:
1962 return false;
1967 /* Error reporting helper for vect_is_simple_reduction below. GIMPLE statement
1968 STMT is printed with a message MSG. */
1970 static void
1971 report_vect_op (int msg_type, gimple stmt, const char *msg)
1973 dump_printf_loc (msg_type, vect_location, "%s", msg);
1974 dump_gimple_stmt (msg_type, TDF_SLIM, stmt, 0);
1975 dump_printf (msg_type, "\n");
1979 /* Detect SLP reduction of the form:
1981 #a1 = phi <a5, a0>
1982 a2 = operation (a1)
1983 a3 = operation (a2)
1984 a4 = operation (a3)
1985 a5 = operation (a4)
1987 #a = phi <a5>
1989 PHI is the reduction phi node (#a1 = phi <a5, a0> above)
1990 FIRST_STMT is the first reduction stmt in the chain
1991 (a2 = operation (a1)).
1993 Return TRUE if a reduction chain was detected. */
1995 static bool
1996 vect_is_slp_reduction (loop_vec_info loop_info, gimple phi, gimple first_stmt)
1998 struct loop *loop = (gimple_bb (phi))->loop_father;
1999 struct loop *vect_loop = LOOP_VINFO_LOOP (loop_info);
2000 enum tree_code code;
2001 gimple current_stmt = NULL, loop_use_stmt = NULL, first, next_stmt;
2002 stmt_vec_info use_stmt_info, current_stmt_info;
2003 tree lhs;
2004 imm_use_iterator imm_iter;
2005 use_operand_p use_p;
2006 int nloop_uses, size = 0, n_out_of_loop_uses;
2007 bool found = false;
2009 if (loop != vect_loop)
2010 return false;
2012 lhs = PHI_RESULT (phi);
2013 code = gimple_assign_rhs_code (first_stmt);
2014 while (1)
2016 nloop_uses = 0;
2017 n_out_of_loop_uses = 0;
2018 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, lhs)
2020 gimple use_stmt = USE_STMT (use_p);
2021 if (is_gimple_debug (use_stmt))
2022 continue;
2024 /* Check if we got back to the reduction phi. */
2025 if (use_stmt == phi)
2027 loop_use_stmt = use_stmt;
2028 found = true;
2029 break;
2032 if (flow_bb_inside_loop_p (loop, gimple_bb (use_stmt)))
2034 if (vinfo_for_stmt (use_stmt)
2035 && !STMT_VINFO_IN_PATTERN_P (vinfo_for_stmt (use_stmt)))
2037 loop_use_stmt = use_stmt;
2038 nloop_uses++;
2041 else
2042 n_out_of_loop_uses++;
2044 /* There are can be either a single use in the loop or two uses in
2045 phi nodes. */
2046 if (nloop_uses > 1 || (n_out_of_loop_uses && nloop_uses))
2047 return false;
2050 if (found)
2051 break;
2053 /* We reached a statement with no loop uses. */
2054 if (nloop_uses == 0)
2055 return false;
2057 /* This is a loop exit phi, and we haven't reached the reduction phi. */
2058 if (gimple_code (loop_use_stmt) == GIMPLE_PHI)
2059 return false;
2061 if (!is_gimple_assign (loop_use_stmt)
2062 || code != gimple_assign_rhs_code (loop_use_stmt)
2063 || !flow_bb_inside_loop_p (loop, gimple_bb (loop_use_stmt)))
2064 return false;
2066 /* Insert USE_STMT into reduction chain. */
2067 use_stmt_info = vinfo_for_stmt (loop_use_stmt);
2068 if (current_stmt)
2070 current_stmt_info = vinfo_for_stmt (current_stmt);
2071 GROUP_NEXT_ELEMENT (current_stmt_info) = loop_use_stmt;
2072 GROUP_FIRST_ELEMENT (use_stmt_info)
2073 = GROUP_FIRST_ELEMENT (current_stmt_info);
2075 else
2076 GROUP_FIRST_ELEMENT (use_stmt_info) = loop_use_stmt;
2078 lhs = gimple_assign_lhs (loop_use_stmt);
2079 current_stmt = loop_use_stmt;
2080 size++;
2083 if (!found || loop_use_stmt != phi || size < 2)
2084 return false;
2086 /* Swap the operands, if needed, to make the reduction operand be the second
2087 operand. */
2088 lhs = PHI_RESULT (phi);
2089 next_stmt = GROUP_FIRST_ELEMENT (vinfo_for_stmt (current_stmt));
2090 while (next_stmt)
2092 if (gimple_assign_rhs2 (next_stmt) == lhs)
2094 tree op = gimple_assign_rhs1 (next_stmt);
2095 gimple def_stmt = NULL;
2097 if (TREE_CODE (op) == SSA_NAME)
2098 def_stmt = SSA_NAME_DEF_STMT (op);
2100 /* Check that the other def is either defined in the loop
2101 ("vect_internal_def"), or it's an induction (defined by a
2102 loop-header phi-node). */
2103 if (def_stmt
2104 && gimple_bb (def_stmt)
2105 && flow_bb_inside_loop_p (loop, gimple_bb (def_stmt))
2106 && (is_gimple_assign (def_stmt)
2107 || is_gimple_call (def_stmt)
2108 || STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def_stmt))
2109 == vect_induction_def
2110 || (gimple_code (def_stmt) == GIMPLE_PHI
2111 && STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def_stmt))
2112 == vect_internal_def
2113 && !is_loop_header_bb_p (gimple_bb (def_stmt)))))
2115 lhs = gimple_assign_lhs (next_stmt);
2116 next_stmt = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next_stmt));
2117 continue;
2120 return false;
2122 else
2124 tree op = gimple_assign_rhs2 (next_stmt);
2125 gimple def_stmt = NULL;
2127 if (TREE_CODE (op) == SSA_NAME)
2128 def_stmt = SSA_NAME_DEF_STMT (op);
2130 /* Check that the other def is either defined in the loop
2131 ("vect_internal_def"), or it's an induction (defined by a
2132 loop-header phi-node). */
2133 if (def_stmt
2134 && gimple_bb (def_stmt)
2135 && flow_bb_inside_loop_p (loop, gimple_bb (def_stmt))
2136 && (is_gimple_assign (def_stmt)
2137 || is_gimple_call (def_stmt)
2138 || STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def_stmt))
2139 == vect_induction_def
2140 || (gimple_code (def_stmt) == GIMPLE_PHI
2141 && STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def_stmt))
2142 == vect_internal_def
2143 && !is_loop_header_bb_p (gimple_bb (def_stmt)))))
2145 if (dump_enabled_p ())
2147 dump_printf_loc (MSG_NOTE, vect_location, "swapping oprnds: ");
2148 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, next_stmt, 0);
2149 dump_printf (MSG_NOTE, "\n");
2152 swap_ssa_operands (next_stmt,
2153 gimple_assign_rhs1_ptr (next_stmt),
2154 gimple_assign_rhs2_ptr (next_stmt));
2155 update_stmt (next_stmt);
2157 if (CONSTANT_CLASS_P (gimple_assign_rhs1 (next_stmt)))
2158 LOOP_VINFO_OPERANDS_SWAPPED (loop_info) = true;
2160 else
2161 return false;
2164 lhs = gimple_assign_lhs (next_stmt);
2165 next_stmt = GROUP_NEXT_ELEMENT (vinfo_for_stmt (next_stmt));
2168 /* Save the chain for further analysis in SLP detection. */
2169 first = GROUP_FIRST_ELEMENT (vinfo_for_stmt (current_stmt));
2170 LOOP_VINFO_REDUCTION_CHAINS (loop_info).safe_push (first);
2171 GROUP_SIZE (vinfo_for_stmt (first)) = size;
2173 return true;
2177 /* Function vect_is_simple_reduction_1
2179 (1) Detect a cross-iteration def-use cycle that represents a simple
2180 reduction computation. We look for the following pattern:
2182 loop_header:
2183 a1 = phi < a0, a2 >
2184 a3 = ...
2185 a2 = operation (a3, a1)
2189 a3 = ...
2190 loop_header:
2191 a1 = phi < a0, a2 >
2192 a2 = operation (a3, a1)
2194 such that:
2195 1. operation is commutative and associative and it is safe to
2196 change the order of the computation (if CHECK_REDUCTION is true)
2197 2. no uses for a2 in the loop (a2 is used out of the loop)
2198 3. no uses of a1 in the loop besides the reduction operation
2199 4. no uses of a1 outside the loop.
2201 Conditions 1,4 are tested here.
2202 Conditions 2,3 are tested in vect_mark_stmts_to_be_vectorized.
2204 (2) Detect a cross-iteration def-use cycle in nested loops, i.e.,
2205 nested cycles, if CHECK_REDUCTION is false.
2207 (3) Detect cycles of phi nodes in outer-loop vectorization, i.e., double
2208 reductions:
2210 a1 = phi < a0, a2 >
2211 inner loop (def of a3)
2212 a2 = phi < a3 >
2214 If MODIFY is true it tries also to rework the code in-place to enable
2215 detection of more reduction patterns. For the time being we rewrite
2216 "res -= RHS" into "rhs += -RHS" when it seems worthwhile.
2219 static gimple
2220 vect_is_simple_reduction_1 (loop_vec_info loop_info, gimple phi,
2221 bool check_reduction, bool *double_reduc,
2222 bool modify)
2224 struct loop *loop = (gimple_bb (phi))->loop_father;
2225 struct loop *vect_loop = LOOP_VINFO_LOOP (loop_info);
2226 edge latch_e = loop_latch_edge (loop);
2227 tree loop_arg = PHI_ARG_DEF_FROM_EDGE (phi, latch_e);
2228 gimple def_stmt, def1 = NULL, def2 = NULL;
2229 enum tree_code orig_code, code;
2230 tree op1, op2, op3 = NULL_TREE, op4 = NULL_TREE;
2231 tree type;
2232 int nloop_uses;
2233 tree name;
2234 imm_use_iterator imm_iter;
2235 use_operand_p use_p;
2236 bool phi_def;
2238 *double_reduc = false;
2240 /* If CHECK_REDUCTION is true, we assume inner-most loop vectorization,
2241 otherwise, we assume outer loop vectorization. */
2242 gcc_assert ((check_reduction && loop == vect_loop)
2243 || (!check_reduction && flow_loop_nested_p (vect_loop, loop)));
2245 name = PHI_RESULT (phi);
2246 /* ??? If there are no uses of the PHI result the inner loop reduction
2247 won't be detected as possibly double-reduction by vectorizable_reduction
2248 because that tries to walk the PHI arg from the preheader edge which
2249 can be constant. See PR60382. */
2250 if (has_zero_uses (name))
2251 return NULL;
2252 nloop_uses = 0;
2253 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, name)
2255 gimple use_stmt = USE_STMT (use_p);
2256 if (is_gimple_debug (use_stmt))
2257 continue;
2259 if (!flow_bb_inside_loop_p (loop, gimple_bb (use_stmt)))
2261 if (dump_enabled_p ())
2262 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2263 "intermediate value used outside loop.\n");
2265 return NULL;
2268 if (vinfo_for_stmt (use_stmt)
2269 && !is_pattern_stmt_p (vinfo_for_stmt (use_stmt)))
2270 nloop_uses++;
2271 if (nloop_uses > 1)
2273 if (dump_enabled_p ())
2274 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2275 "reduction used in loop.\n");
2276 return NULL;
2280 if (TREE_CODE (loop_arg) != SSA_NAME)
2282 if (dump_enabled_p ())
2284 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2285 "reduction: not ssa_name: ");
2286 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM, loop_arg);
2287 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
2289 return NULL;
2292 def_stmt = SSA_NAME_DEF_STMT (loop_arg);
2293 if (!def_stmt)
2295 if (dump_enabled_p ())
2296 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2297 "reduction: no def_stmt.\n");
2298 return NULL;
2301 if (!is_gimple_assign (def_stmt) && gimple_code (def_stmt) != GIMPLE_PHI)
2303 if (dump_enabled_p ())
2305 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, def_stmt, 0);
2306 dump_printf (MSG_NOTE, "\n");
2308 return NULL;
2311 if (is_gimple_assign (def_stmt))
2313 name = gimple_assign_lhs (def_stmt);
2314 phi_def = false;
2316 else
2318 name = PHI_RESULT (def_stmt);
2319 phi_def = true;
2322 nloop_uses = 0;
2323 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, name)
2325 gimple use_stmt = USE_STMT (use_p);
2326 if (is_gimple_debug (use_stmt))
2327 continue;
2328 if (flow_bb_inside_loop_p (loop, gimple_bb (use_stmt))
2329 && vinfo_for_stmt (use_stmt)
2330 && !is_pattern_stmt_p (vinfo_for_stmt (use_stmt)))
2331 nloop_uses++;
2332 if (nloop_uses > 1)
2334 if (dump_enabled_p ())
2335 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2336 "reduction used in loop.\n");
2337 return NULL;
2341 /* If DEF_STMT is a phi node itself, we expect it to have a single argument
2342 defined in the inner loop. */
2343 if (phi_def)
2345 op1 = PHI_ARG_DEF (def_stmt, 0);
2347 if (gimple_phi_num_args (def_stmt) != 1
2348 || TREE_CODE (op1) != SSA_NAME)
2350 if (dump_enabled_p ())
2351 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
2352 "unsupported phi node definition.\n");
2354 return NULL;
2357 def1 = SSA_NAME_DEF_STMT (op1);
2358 if (gimple_bb (def1)
2359 && flow_bb_inside_loop_p (loop, gimple_bb (def_stmt))
2360 && loop->inner
2361 && flow_bb_inside_loop_p (loop->inner, gimple_bb (def1))
2362 && is_gimple_assign (def1))
2364 if (dump_enabled_p ())
2365 report_vect_op (MSG_NOTE, def_stmt,
2366 "detected double reduction: ");
2368 *double_reduc = true;
2369 return def_stmt;
2372 return NULL;
2375 code = orig_code = gimple_assign_rhs_code (def_stmt);
2377 /* We can handle "res -= x[i]", which is non-associative by
2378 simply rewriting this into "res += -x[i]". Avoid changing
2379 gimple instruction for the first simple tests and only do this
2380 if we're allowed to change code at all. */
2381 if (code == MINUS_EXPR
2382 && modify
2383 && (op1 = gimple_assign_rhs1 (def_stmt))
2384 && TREE_CODE (op1) == SSA_NAME
2385 && SSA_NAME_DEF_STMT (op1) == phi)
2386 code = PLUS_EXPR;
2388 if (check_reduction
2389 && (!commutative_tree_code (code) || !associative_tree_code (code)))
2391 if (dump_enabled_p ())
2392 report_vect_op (MSG_MISSED_OPTIMIZATION, def_stmt,
2393 "reduction: not commutative/associative: ");
2394 return NULL;
2397 if (get_gimple_rhs_class (code) != GIMPLE_BINARY_RHS)
2399 if (code != COND_EXPR)
2401 if (dump_enabled_p ())
2402 report_vect_op (MSG_MISSED_OPTIMIZATION, def_stmt,
2403 "reduction: not binary operation: ");
2405 return NULL;
2408 op3 = gimple_assign_rhs1 (def_stmt);
2409 if (COMPARISON_CLASS_P (op3))
2411 op4 = TREE_OPERAND (op3, 1);
2412 op3 = TREE_OPERAND (op3, 0);
2415 op1 = gimple_assign_rhs2 (def_stmt);
2416 op2 = gimple_assign_rhs3 (def_stmt);
2418 if (TREE_CODE (op1) != SSA_NAME && TREE_CODE (op2) != SSA_NAME)
2420 if (dump_enabled_p ())
2421 report_vect_op (MSG_MISSED_OPTIMIZATION, def_stmt,
2422 "reduction: uses not ssa_names: ");
2424 return NULL;
2427 else
2429 op1 = gimple_assign_rhs1 (def_stmt);
2430 op2 = gimple_assign_rhs2 (def_stmt);
2432 if (TREE_CODE (op1) != SSA_NAME && TREE_CODE (op2) != SSA_NAME)
2434 if (dump_enabled_p ())
2435 report_vect_op (MSG_MISSED_OPTIMIZATION, def_stmt,
2436 "reduction: uses not ssa_names: ");
2438 return NULL;
2442 type = TREE_TYPE (gimple_assign_lhs (def_stmt));
2443 if ((TREE_CODE (op1) == SSA_NAME
2444 && !types_compatible_p (type,TREE_TYPE (op1)))
2445 || (TREE_CODE (op2) == SSA_NAME
2446 && !types_compatible_p (type, TREE_TYPE (op2)))
2447 || (op3 && TREE_CODE (op3) == SSA_NAME
2448 && !types_compatible_p (type, TREE_TYPE (op3)))
2449 || (op4 && TREE_CODE (op4) == SSA_NAME
2450 && !types_compatible_p (type, TREE_TYPE (op4))))
2452 if (dump_enabled_p ())
2454 dump_printf_loc (MSG_NOTE, vect_location,
2455 "reduction: multiple types: operation type: ");
2456 dump_generic_expr (MSG_NOTE, TDF_SLIM, type);
2457 dump_printf (MSG_NOTE, ", operands types: ");
2458 dump_generic_expr (MSG_NOTE, TDF_SLIM,
2459 TREE_TYPE (op1));
2460 dump_printf (MSG_NOTE, ",");
2461 dump_generic_expr (MSG_NOTE, TDF_SLIM,
2462 TREE_TYPE (op2));
2463 if (op3)
2465 dump_printf (MSG_NOTE, ",");
2466 dump_generic_expr (MSG_NOTE, TDF_SLIM,
2467 TREE_TYPE (op3));
2470 if (op4)
2472 dump_printf (MSG_NOTE, ",");
2473 dump_generic_expr (MSG_NOTE, TDF_SLIM,
2474 TREE_TYPE (op4));
2476 dump_printf (MSG_NOTE, "\n");
2479 return NULL;
2482 /* Check that it's ok to change the order of the computation.
2483 Generally, when vectorizing a reduction we change the order of the
2484 computation. This may change the behavior of the program in some
2485 cases, so we need to check that this is ok. One exception is when
2486 vectorizing an outer-loop: the inner-loop is executed sequentially,
2487 and therefore vectorizing reductions in the inner-loop during
2488 outer-loop vectorization is safe. */
2490 /* CHECKME: check for !flag_finite_math_only too? */
2491 if (SCALAR_FLOAT_TYPE_P (type) && !flag_associative_math
2492 && check_reduction)
2494 /* Changing the order of operations changes the semantics. */
2495 if (dump_enabled_p ())
2496 report_vect_op (MSG_MISSED_OPTIMIZATION, def_stmt,
2497 "reduction: unsafe fp math optimization: ");
2498 return NULL;
2500 else if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_TRAPS (type)
2501 && check_reduction)
2503 /* Changing the order of operations changes the semantics. */
2504 if (dump_enabled_p ())
2505 report_vect_op (MSG_MISSED_OPTIMIZATION, def_stmt,
2506 "reduction: unsafe int math optimization: ");
2507 return NULL;
2509 else if (SAT_FIXED_POINT_TYPE_P (type) && check_reduction)
2511 /* Changing the order of operations changes the semantics. */
2512 if (dump_enabled_p ())
2513 report_vect_op (MSG_MISSED_OPTIMIZATION, def_stmt,
2514 "reduction: unsafe fixed-point math optimization: ");
2515 return NULL;
2518 /* If we detected "res -= x[i]" earlier, rewrite it into
2519 "res += -x[i]" now. If this turns out to be useless reassoc
2520 will clean it up again. */
2521 if (orig_code == MINUS_EXPR)
2523 tree rhs = gimple_assign_rhs2 (def_stmt);
2524 tree negrhs = make_ssa_name (TREE_TYPE (rhs));
2525 gimple negate_stmt = gimple_build_assign (negrhs, NEGATE_EXPR, rhs);
2526 gimple_stmt_iterator gsi = gsi_for_stmt (def_stmt);
2527 set_vinfo_for_stmt (negate_stmt, new_stmt_vec_info (negate_stmt,
2528 loop_info, NULL));
2529 gsi_insert_before (&gsi, negate_stmt, GSI_NEW_STMT);
2530 gimple_assign_set_rhs2 (def_stmt, negrhs);
2531 gimple_assign_set_rhs_code (def_stmt, PLUS_EXPR);
2532 update_stmt (def_stmt);
2535 /* Reduction is safe. We're dealing with one of the following:
2536 1) integer arithmetic and no trapv
2537 2) floating point arithmetic, and special flags permit this optimization
2538 3) nested cycle (i.e., outer loop vectorization). */
2539 if (TREE_CODE (op1) == SSA_NAME)
2540 def1 = SSA_NAME_DEF_STMT (op1);
2542 if (TREE_CODE (op2) == SSA_NAME)
2543 def2 = SSA_NAME_DEF_STMT (op2);
2545 if (code != COND_EXPR
2546 && ((!def1 || gimple_nop_p (def1)) && (!def2 || gimple_nop_p (def2))))
2548 if (dump_enabled_p ())
2549 report_vect_op (MSG_NOTE, def_stmt, "reduction: no defs for operands: ");
2550 return NULL;
2553 /* Check that one def is the reduction def, defined by PHI,
2554 the other def is either defined in the loop ("vect_internal_def"),
2555 or it's an induction (defined by a loop-header phi-node). */
2557 if (def2 && def2 == phi
2558 && (code == COND_EXPR
2559 || !def1 || gimple_nop_p (def1)
2560 || !flow_bb_inside_loop_p (loop, gimple_bb (def1))
2561 || (def1 && flow_bb_inside_loop_p (loop, gimple_bb (def1))
2562 && (is_gimple_assign (def1)
2563 || is_gimple_call (def1)
2564 || STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def1))
2565 == vect_induction_def
2566 || (gimple_code (def1) == GIMPLE_PHI
2567 && STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def1))
2568 == vect_internal_def
2569 && !is_loop_header_bb_p (gimple_bb (def1)))))))
2571 if (dump_enabled_p ())
2572 report_vect_op (MSG_NOTE, def_stmt, "detected reduction: ");
2573 return def_stmt;
2576 if (def1 && def1 == phi
2577 && (code == COND_EXPR
2578 || !def2 || gimple_nop_p (def2)
2579 || !flow_bb_inside_loop_p (loop, gimple_bb (def2))
2580 || (def2 && flow_bb_inside_loop_p (loop, gimple_bb (def2))
2581 && (is_gimple_assign (def2)
2582 || is_gimple_call (def2)
2583 || STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def2))
2584 == vect_induction_def
2585 || (gimple_code (def2) == GIMPLE_PHI
2586 && STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def2))
2587 == vect_internal_def
2588 && !is_loop_header_bb_p (gimple_bb (def2)))))))
2590 if (check_reduction)
2592 /* Swap operands (just for simplicity - so that the rest of the code
2593 can assume that the reduction variable is always the last (second)
2594 argument). */
2595 if (dump_enabled_p ())
2596 report_vect_op (MSG_NOTE, def_stmt,
2597 "detected reduction: need to swap operands: ");
2599 swap_ssa_operands (def_stmt, gimple_assign_rhs1_ptr (def_stmt),
2600 gimple_assign_rhs2_ptr (def_stmt));
2602 if (CONSTANT_CLASS_P (gimple_assign_rhs1 (def_stmt)))
2603 LOOP_VINFO_OPERANDS_SWAPPED (loop_info) = true;
2605 else
2607 if (dump_enabled_p ())
2608 report_vect_op (MSG_NOTE, def_stmt, "detected reduction: ");
2611 return def_stmt;
2614 /* Try to find SLP reduction chain. */
2615 if (check_reduction && vect_is_slp_reduction (loop_info, phi, def_stmt))
2617 if (dump_enabled_p ())
2618 report_vect_op (MSG_NOTE, def_stmt,
2619 "reduction: detected reduction chain: ");
2621 return def_stmt;
2624 if (dump_enabled_p ())
2625 report_vect_op (MSG_MISSED_OPTIMIZATION, def_stmt,
2626 "reduction: unknown pattern: ");
2628 return NULL;
2631 /* Wrapper around vect_is_simple_reduction_1, that won't modify code
2632 in-place. Arguments as there. */
2634 static gimple
2635 vect_is_simple_reduction (loop_vec_info loop_info, gimple phi,
2636 bool check_reduction, bool *double_reduc)
2638 return vect_is_simple_reduction_1 (loop_info, phi, check_reduction,
2639 double_reduc, false);
2642 /* Wrapper around vect_is_simple_reduction_1, which will modify code
2643 in-place if it enables detection of more reductions. Arguments
2644 as there. */
2646 gimple
2647 vect_force_simple_reduction (loop_vec_info loop_info, gimple phi,
2648 bool check_reduction, bool *double_reduc)
2650 return vect_is_simple_reduction_1 (loop_info, phi, check_reduction,
2651 double_reduc, true);
2654 /* Calculate the cost of one scalar iteration of the loop. */
2656 vect_get_single_scalar_iteration_cost (loop_vec_info loop_vinfo,
2657 stmt_vector_for_cost *scalar_cost_vec)
2659 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
2660 basic_block *bbs = LOOP_VINFO_BBS (loop_vinfo);
2661 int nbbs = loop->num_nodes, factor, scalar_single_iter_cost = 0;
2662 int innerloop_iters, i;
2664 /* Count statements in scalar loop. Using this as scalar cost for a single
2665 iteration for now.
2667 TODO: Add outer loop support.
2669 TODO: Consider assigning different costs to different scalar
2670 statements. */
2672 /* FORNOW. */
2673 innerloop_iters = 1;
2674 if (loop->inner)
2675 innerloop_iters = 50; /* FIXME */
2677 for (i = 0; i < nbbs; i++)
2679 gimple_stmt_iterator si;
2680 basic_block bb = bbs[i];
2682 if (bb->loop_father == loop->inner)
2683 factor = innerloop_iters;
2684 else
2685 factor = 1;
2687 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
2689 gimple stmt = gsi_stmt (si);
2690 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
2692 if (!is_gimple_assign (stmt) && !is_gimple_call (stmt))
2693 continue;
2695 /* Skip stmts that are not vectorized inside the loop. */
2696 if (stmt_info
2697 && !STMT_VINFO_RELEVANT_P (stmt_info)
2698 && (!STMT_VINFO_LIVE_P (stmt_info)
2699 || !VECTORIZABLE_CYCLE_DEF (STMT_VINFO_DEF_TYPE (stmt_info)))
2700 && !STMT_VINFO_IN_PATTERN_P (stmt_info))
2701 continue;
2703 vect_cost_for_stmt kind;
2704 if (STMT_VINFO_DATA_REF (vinfo_for_stmt (stmt)))
2706 if (DR_IS_READ (STMT_VINFO_DATA_REF (vinfo_for_stmt (stmt))))
2707 kind = scalar_load;
2708 else
2709 kind = scalar_store;
2711 else
2712 kind = scalar_stmt;
2714 scalar_single_iter_cost
2715 += record_stmt_cost (scalar_cost_vec, factor, kind,
2716 NULL, 0, vect_prologue);
2719 return scalar_single_iter_cost;
2722 /* Calculate cost of peeling the loop PEEL_ITERS_PROLOGUE times. */
2724 vect_get_known_peeling_cost (loop_vec_info loop_vinfo, int peel_iters_prologue,
2725 int *peel_iters_epilogue,
2726 stmt_vector_for_cost *scalar_cost_vec,
2727 stmt_vector_for_cost *prologue_cost_vec,
2728 stmt_vector_for_cost *epilogue_cost_vec)
2730 int retval = 0;
2731 int vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
2733 if (!LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo))
2735 *peel_iters_epilogue = vf/2;
2736 if (dump_enabled_p ())
2737 dump_printf_loc (MSG_NOTE, vect_location,
2738 "cost model: epilogue peel iters set to vf/2 "
2739 "because loop iterations are unknown .\n");
2741 /* If peeled iterations are known but number of scalar loop
2742 iterations are unknown, count a taken branch per peeled loop. */
2743 retval = record_stmt_cost (prologue_cost_vec, 1, cond_branch_taken,
2744 NULL, 0, vect_prologue);
2745 retval = record_stmt_cost (prologue_cost_vec, 1, cond_branch_taken,
2746 NULL, 0, vect_epilogue);
2748 else
2750 int niters = LOOP_VINFO_INT_NITERS (loop_vinfo);
2751 peel_iters_prologue = niters < peel_iters_prologue ?
2752 niters : peel_iters_prologue;
2753 *peel_iters_epilogue = (niters - peel_iters_prologue) % vf;
2754 /* If we need to peel for gaps, but no peeling is required, we have to
2755 peel VF iterations. */
2756 if (LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo) && !*peel_iters_epilogue)
2757 *peel_iters_epilogue = vf;
2760 stmt_info_for_cost *si;
2761 int j;
2762 if (peel_iters_prologue)
2763 FOR_EACH_VEC_ELT (*scalar_cost_vec, j, si)
2764 retval += record_stmt_cost (prologue_cost_vec,
2765 si->count * peel_iters_prologue,
2766 si->kind, NULL, si->misalign,
2767 vect_prologue);
2768 if (*peel_iters_epilogue)
2769 FOR_EACH_VEC_ELT (*scalar_cost_vec, j, si)
2770 retval += record_stmt_cost (epilogue_cost_vec,
2771 si->count * *peel_iters_epilogue,
2772 si->kind, NULL, si->misalign,
2773 vect_epilogue);
2775 return retval;
2778 /* Function vect_estimate_min_profitable_iters
2780 Return the number of iterations required for the vector version of the
2781 loop to be profitable relative to the cost of the scalar version of the
2782 loop. */
2784 static void
2785 vect_estimate_min_profitable_iters (loop_vec_info loop_vinfo,
2786 int *ret_min_profitable_niters,
2787 int *ret_min_profitable_estimate)
2789 int min_profitable_iters;
2790 int min_profitable_estimate;
2791 int peel_iters_prologue;
2792 int peel_iters_epilogue;
2793 unsigned vec_inside_cost = 0;
2794 int vec_outside_cost = 0;
2795 unsigned vec_prologue_cost = 0;
2796 unsigned vec_epilogue_cost = 0;
2797 int scalar_single_iter_cost = 0;
2798 int scalar_outside_cost = 0;
2799 int vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
2800 int npeel = LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo);
2801 void *target_cost_data = LOOP_VINFO_TARGET_COST_DATA (loop_vinfo);
2803 /* Cost model disabled. */
2804 if (unlimited_cost_model (LOOP_VINFO_LOOP (loop_vinfo)))
2806 dump_printf_loc (MSG_NOTE, vect_location, "cost model disabled.\n");
2807 *ret_min_profitable_niters = 0;
2808 *ret_min_profitable_estimate = 0;
2809 return;
2812 /* Requires loop versioning tests to handle misalignment. */
2813 if (LOOP_REQUIRES_VERSIONING_FOR_ALIGNMENT (loop_vinfo))
2815 /* FIXME: Make cost depend on complexity of individual check. */
2816 unsigned len = LOOP_VINFO_MAY_MISALIGN_STMTS (loop_vinfo).length ();
2817 (void) add_stmt_cost (target_cost_data, len, vector_stmt, NULL, 0,
2818 vect_prologue);
2819 dump_printf (MSG_NOTE,
2820 "cost model: Adding cost of checks for loop "
2821 "versioning to treat misalignment.\n");
2824 /* Requires loop versioning with alias checks. */
2825 if (LOOP_REQUIRES_VERSIONING_FOR_ALIAS (loop_vinfo))
2827 /* FIXME: Make cost depend on complexity of individual check. */
2828 unsigned len = LOOP_VINFO_MAY_ALIAS_DDRS (loop_vinfo).length ();
2829 (void) add_stmt_cost (target_cost_data, len, vector_stmt, NULL, 0,
2830 vect_prologue);
2831 dump_printf (MSG_NOTE,
2832 "cost model: Adding cost of checks for loop "
2833 "versioning aliasing.\n");
2836 if (LOOP_REQUIRES_VERSIONING_FOR_ALIGNMENT (loop_vinfo)
2837 || LOOP_REQUIRES_VERSIONING_FOR_ALIAS (loop_vinfo))
2838 (void) add_stmt_cost (target_cost_data, 1, cond_branch_taken, NULL, 0,
2839 vect_prologue);
2841 /* Count statements in scalar loop. Using this as scalar cost for a single
2842 iteration for now.
2844 TODO: Add outer loop support.
2846 TODO: Consider assigning different costs to different scalar
2847 statements. */
2849 auto_vec<stmt_info_for_cost> scalar_cost_vec;
2850 scalar_single_iter_cost
2851 = vect_get_single_scalar_iteration_cost (loop_vinfo, &scalar_cost_vec);
2853 /* Add additional cost for the peeled instructions in prologue and epilogue
2854 loop.
2856 FORNOW: If we don't know the value of peel_iters for prologue or epilogue
2857 at compile-time - we assume it's vf/2 (the worst would be vf-1).
2859 TODO: Build an expression that represents peel_iters for prologue and
2860 epilogue to be used in a run-time test. */
2862 if (npeel < 0)
2864 peel_iters_prologue = vf/2;
2865 dump_printf (MSG_NOTE, "cost model: "
2866 "prologue peel iters set to vf/2.\n");
2868 /* If peeling for alignment is unknown, loop bound of main loop becomes
2869 unknown. */
2870 peel_iters_epilogue = vf/2;
2871 dump_printf (MSG_NOTE, "cost model: "
2872 "epilogue peel iters set to vf/2 because "
2873 "peeling for alignment is unknown.\n");
2875 /* If peeled iterations are unknown, count a taken branch and a not taken
2876 branch per peeled loop. Even if scalar loop iterations are known,
2877 vector iterations are not known since peeled prologue iterations are
2878 not known. Hence guards remain the same. */
2879 (void) add_stmt_cost (target_cost_data, 1, cond_branch_taken,
2880 NULL, 0, vect_prologue);
2881 (void) add_stmt_cost (target_cost_data, 1, cond_branch_not_taken,
2882 NULL, 0, vect_prologue);
2883 (void) add_stmt_cost (target_cost_data, 1, cond_branch_taken,
2884 NULL, 0, vect_epilogue);
2885 (void) add_stmt_cost (target_cost_data, 1, cond_branch_not_taken,
2886 NULL, 0, vect_epilogue);
2887 stmt_info_for_cost *si;
2888 int j;
2889 FOR_EACH_VEC_ELT (scalar_cost_vec, j, si)
2891 struct _stmt_vec_info *stmt_info
2892 = si->stmt ? vinfo_for_stmt (si->stmt) : NULL;
2893 (void) add_stmt_cost (target_cost_data,
2894 si->count * peel_iters_prologue,
2895 si->kind, stmt_info, si->misalign,
2896 vect_prologue);
2897 (void) add_stmt_cost (target_cost_data,
2898 si->count * peel_iters_epilogue,
2899 si->kind, stmt_info, si->misalign,
2900 vect_epilogue);
2903 else
2905 stmt_vector_for_cost prologue_cost_vec, epilogue_cost_vec;
2906 stmt_info_for_cost *si;
2907 int j;
2908 void *data = LOOP_VINFO_TARGET_COST_DATA (loop_vinfo);
2910 prologue_cost_vec.create (2);
2911 epilogue_cost_vec.create (2);
2912 peel_iters_prologue = npeel;
2914 (void) vect_get_known_peeling_cost (loop_vinfo, peel_iters_prologue,
2915 &peel_iters_epilogue,
2916 &scalar_cost_vec,
2917 &prologue_cost_vec,
2918 &epilogue_cost_vec);
2920 FOR_EACH_VEC_ELT (prologue_cost_vec, j, si)
2922 struct _stmt_vec_info *stmt_info
2923 = si->stmt ? vinfo_for_stmt (si->stmt) : NULL;
2924 (void) add_stmt_cost (data, si->count, si->kind, stmt_info,
2925 si->misalign, vect_prologue);
2928 FOR_EACH_VEC_ELT (epilogue_cost_vec, j, si)
2930 struct _stmt_vec_info *stmt_info
2931 = si->stmt ? vinfo_for_stmt (si->stmt) : NULL;
2932 (void) add_stmt_cost (data, si->count, si->kind, stmt_info,
2933 si->misalign, vect_epilogue);
2936 prologue_cost_vec.release ();
2937 epilogue_cost_vec.release ();
2940 /* FORNOW: The scalar outside cost is incremented in one of the
2941 following ways:
2943 1. The vectorizer checks for alignment and aliasing and generates
2944 a condition that allows dynamic vectorization. A cost model
2945 check is ANDED with the versioning condition. Hence scalar code
2946 path now has the added cost of the versioning check.
2948 if (cost > th & versioning_check)
2949 jmp to vector code
2951 Hence run-time scalar is incremented by not-taken branch cost.
2953 2. The vectorizer then checks if a prologue is required. If the
2954 cost model check was not done before during versioning, it has to
2955 be done before the prologue check.
2957 if (cost <= th)
2958 prologue = scalar_iters
2959 if (prologue == 0)
2960 jmp to vector code
2961 else
2962 execute prologue
2963 if (prologue == num_iters)
2964 go to exit
2966 Hence the run-time scalar cost is incremented by a taken branch,
2967 plus a not-taken branch, plus a taken branch cost.
2969 3. The vectorizer then checks if an epilogue is required. If the
2970 cost model check was not done before during prologue check, it
2971 has to be done with the epilogue check.
2973 if (prologue == 0)
2974 jmp to vector code
2975 else
2976 execute prologue
2977 if (prologue == num_iters)
2978 go to exit
2979 vector code:
2980 if ((cost <= th) | (scalar_iters-prologue-epilogue == 0))
2981 jmp to epilogue
2983 Hence the run-time scalar cost should be incremented by 2 taken
2984 branches.
2986 TODO: The back end may reorder the BBS's differently and reverse
2987 conditions/branch directions. Change the estimates below to
2988 something more reasonable. */
2990 /* If the number of iterations is known and we do not do versioning, we can
2991 decide whether to vectorize at compile time. Hence the scalar version
2992 do not carry cost model guard costs. */
2993 if (!LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo)
2994 || LOOP_REQUIRES_VERSIONING_FOR_ALIGNMENT (loop_vinfo)
2995 || LOOP_REQUIRES_VERSIONING_FOR_ALIAS (loop_vinfo))
2997 /* Cost model check occurs at versioning. */
2998 if (LOOP_REQUIRES_VERSIONING_FOR_ALIGNMENT (loop_vinfo)
2999 || LOOP_REQUIRES_VERSIONING_FOR_ALIAS (loop_vinfo))
3000 scalar_outside_cost += vect_get_stmt_cost (cond_branch_not_taken);
3001 else
3003 /* Cost model check occurs at prologue generation. */
3004 if (LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo) < 0)
3005 scalar_outside_cost += 2 * vect_get_stmt_cost (cond_branch_taken)
3006 + vect_get_stmt_cost (cond_branch_not_taken);
3007 /* Cost model check occurs at epilogue generation. */
3008 else
3009 scalar_outside_cost += 2 * vect_get_stmt_cost (cond_branch_taken);
3013 /* Complete the target-specific cost calculations. */
3014 finish_cost (LOOP_VINFO_TARGET_COST_DATA (loop_vinfo), &vec_prologue_cost,
3015 &vec_inside_cost, &vec_epilogue_cost);
3017 vec_outside_cost = (int)(vec_prologue_cost + vec_epilogue_cost);
3019 if (dump_enabled_p ())
3021 dump_printf_loc (MSG_NOTE, vect_location, "Cost model analysis: \n");
3022 dump_printf (MSG_NOTE, " Vector inside of loop cost: %d\n",
3023 vec_inside_cost);
3024 dump_printf (MSG_NOTE, " Vector prologue cost: %d\n",
3025 vec_prologue_cost);
3026 dump_printf (MSG_NOTE, " Vector epilogue cost: %d\n",
3027 vec_epilogue_cost);
3028 dump_printf (MSG_NOTE, " Scalar iteration cost: %d\n",
3029 scalar_single_iter_cost);
3030 dump_printf (MSG_NOTE, " Scalar outside cost: %d\n",
3031 scalar_outside_cost);
3032 dump_printf (MSG_NOTE, " Vector outside cost: %d\n",
3033 vec_outside_cost);
3034 dump_printf (MSG_NOTE, " prologue iterations: %d\n",
3035 peel_iters_prologue);
3036 dump_printf (MSG_NOTE, " epilogue iterations: %d\n",
3037 peel_iters_epilogue);
3040 /* Calculate number of iterations required to make the vector version
3041 profitable, relative to the loop bodies only. The following condition
3042 must hold true:
3043 SIC * niters + SOC > VIC * ((niters-PL_ITERS-EP_ITERS)/VF) + VOC
3044 where
3045 SIC = scalar iteration cost, VIC = vector iteration cost,
3046 VOC = vector outside cost, VF = vectorization factor,
3047 PL_ITERS = prologue iterations, EP_ITERS= epilogue iterations
3048 SOC = scalar outside cost for run time cost model check. */
3050 if ((scalar_single_iter_cost * vf) > (int) vec_inside_cost)
3052 if (vec_outside_cost <= 0)
3053 min_profitable_iters = 1;
3054 else
3056 min_profitable_iters = ((vec_outside_cost - scalar_outside_cost) * vf
3057 - vec_inside_cost * peel_iters_prologue
3058 - vec_inside_cost * peel_iters_epilogue)
3059 / ((scalar_single_iter_cost * vf)
3060 - vec_inside_cost);
3062 if ((scalar_single_iter_cost * vf * min_profitable_iters)
3063 <= (((int) vec_inside_cost * min_profitable_iters)
3064 + (((int) vec_outside_cost - scalar_outside_cost) * vf)))
3065 min_profitable_iters++;
3068 /* vector version will never be profitable. */
3069 else
3071 if (LOOP_VINFO_LOOP (loop_vinfo)->force_vectorize)
3072 warning_at (vect_location, OPT_Wopenmp_simd, "vectorization "
3073 "did not happen for a simd loop");
3075 if (dump_enabled_p ())
3076 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3077 "cost model: the vector iteration cost = %d "
3078 "divided by the scalar iteration cost = %d "
3079 "is greater or equal to the vectorization factor = %d"
3080 ".\n",
3081 vec_inside_cost, scalar_single_iter_cost, vf);
3082 *ret_min_profitable_niters = -1;
3083 *ret_min_profitable_estimate = -1;
3084 return;
3087 dump_printf (MSG_NOTE,
3088 " Calculated minimum iters for profitability: %d\n",
3089 min_profitable_iters);
3091 min_profitable_iters =
3092 min_profitable_iters < vf ? vf : min_profitable_iters;
3094 /* Because the condition we create is:
3095 if (niters <= min_profitable_iters)
3096 then skip the vectorized loop. */
3097 min_profitable_iters--;
3099 if (dump_enabled_p ())
3100 dump_printf_loc (MSG_NOTE, vect_location,
3101 " Runtime profitability threshold = %d\n",
3102 min_profitable_iters);
3104 *ret_min_profitable_niters = min_profitable_iters;
3106 /* Calculate number of iterations required to make the vector version
3107 profitable, relative to the loop bodies only.
3109 Non-vectorized variant is SIC * niters and it must win over vector
3110 variant on the expected loop trip count. The following condition must hold true:
3111 SIC * niters > VIC * ((niters-PL_ITERS-EP_ITERS)/VF) + VOC + SOC */
3113 if (vec_outside_cost <= 0)
3114 min_profitable_estimate = 1;
3115 else
3117 min_profitable_estimate = ((vec_outside_cost + scalar_outside_cost) * vf
3118 - vec_inside_cost * peel_iters_prologue
3119 - vec_inside_cost * peel_iters_epilogue)
3120 / ((scalar_single_iter_cost * vf)
3121 - vec_inside_cost);
3123 min_profitable_estimate --;
3124 min_profitable_estimate = MAX (min_profitable_estimate, min_profitable_iters);
3125 if (dump_enabled_p ())
3126 dump_printf_loc (MSG_NOTE, vect_location,
3127 " Static estimate profitability threshold = %d\n",
3128 min_profitable_iters);
3130 *ret_min_profitable_estimate = min_profitable_estimate;
3133 /* Writes into SEL a mask for a vec_perm, equivalent to a vec_shr by OFFSET
3134 vector elements (not bits) for a vector of mode MODE. */
3135 static void
3136 calc_vec_perm_mask_for_shift (enum machine_mode mode, unsigned int offset,
3137 unsigned char *sel)
3139 unsigned int i, nelt = GET_MODE_NUNITS (mode);
3141 for (i = 0; i < nelt; i++)
3142 sel[i] = (i + offset) & (2*nelt - 1);
3145 /* Checks whether the target supports whole-vector shifts for vectors of mode
3146 MODE. This is the case if _either_ the platform handles vec_shr_optab, _or_
3147 it supports vec_perm_const with masks for all necessary shift amounts. */
3148 static bool
3149 have_whole_vector_shift (enum machine_mode mode)
3151 if (optab_handler (vec_shr_optab, mode) != CODE_FOR_nothing)
3152 return true;
3154 if (direct_optab_handler (vec_perm_const_optab, mode) == CODE_FOR_nothing)
3155 return false;
3157 unsigned int i, nelt = GET_MODE_NUNITS (mode);
3158 unsigned char *sel = XALLOCAVEC (unsigned char, nelt);
3160 for (i = nelt/2; i >= 1; i/=2)
3162 calc_vec_perm_mask_for_shift (mode, i, sel);
3163 if (!can_vec_perm_p (mode, false, sel))
3164 return false;
3166 return true;
3169 /* Return the reduction operand (with index REDUC_INDEX) of STMT. */
3171 static tree
3172 get_reduction_op (gimple stmt, int reduc_index)
3174 switch (get_gimple_rhs_class (gimple_assign_rhs_code (stmt)))
3176 case GIMPLE_SINGLE_RHS:
3177 gcc_assert (TREE_OPERAND_LENGTH (gimple_assign_rhs1 (stmt))
3178 == ternary_op);
3179 return TREE_OPERAND (gimple_assign_rhs1 (stmt), reduc_index);
3180 case GIMPLE_UNARY_RHS:
3181 return gimple_assign_rhs1 (stmt);
3182 case GIMPLE_BINARY_RHS:
3183 return (reduc_index
3184 ? gimple_assign_rhs2 (stmt) : gimple_assign_rhs1 (stmt));
3185 case GIMPLE_TERNARY_RHS:
3186 return gimple_op (stmt, reduc_index + 1);
3187 default:
3188 gcc_unreachable ();
3192 /* TODO: Close dependency between vect_model_*_cost and vectorizable_*
3193 functions. Design better to avoid maintenance issues. */
3195 /* Function vect_model_reduction_cost.
3197 Models cost for a reduction operation, including the vector ops
3198 generated within the strip-mine loop, the initial definition before
3199 the loop, and the epilogue code that must be generated. */
3201 static bool
3202 vect_model_reduction_cost (stmt_vec_info stmt_info, enum tree_code reduc_code,
3203 int ncopies, int reduc_index)
3205 int prologue_cost = 0, epilogue_cost = 0;
3206 enum tree_code code;
3207 optab optab;
3208 tree vectype;
3209 gimple stmt, orig_stmt;
3210 tree reduction_op;
3211 machine_mode mode;
3212 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
3213 struct loop *loop = NULL;
3214 void *target_cost_data;
3216 if (loop_vinfo)
3218 loop = LOOP_VINFO_LOOP (loop_vinfo);
3219 target_cost_data = LOOP_VINFO_TARGET_COST_DATA (loop_vinfo);
3221 else
3222 target_cost_data = BB_VINFO_TARGET_COST_DATA (STMT_VINFO_BB_VINFO (stmt_info));
3224 /* Cost of reduction op inside loop. */
3225 unsigned inside_cost = add_stmt_cost (target_cost_data, ncopies, vector_stmt,
3226 stmt_info, 0, vect_body);
3227 stmt = STMT_VINFO_STMT (stmt_info);
3229 reduction_op = get_reduction_op (stmt, reduc_index);
3231 vectype = get_vectype_for_scalar_type (TREE_TYPE (reduction_op));
3232 if (!vectype)
3234 if (dump_enabled_p ())
3236 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
3237 "unsupported data-type ");
3238 dump_generic_expr (MSG_MISSED_OPTIMIZATION, TDF_SLIM,
3239 TREE_TYPE (reduction_op));
3240 dump_printf (MSG_MISSED_OPTIMIZATION, "\n");
3242 return false;
3245 mode = TYPE_MODE (vectype);
3246 orig_stmt = STMT_VINFO_RELATED_STMT (stmt_info);
3248 if (!orig_stmt)
3249 orig_stmt = STMT_VINFO_STMT (stmt_info);
3251 code = gimple_assign_rhs_code (orig_stmt);
3253 /* Add in cost for initial definition. */
3254 prologue_cost += add_stmt_cost (target_cost_data, 1, scalar_to_vec,
3255 stmt_info, 0, vect_prologue);
3257 /* Determine cost of epilogue code.
3259 We have a reduction operator that will reduce the vector in one statement.
3260 Also requires scalar extract. */
3262 if (!loop || !nested_in_vect_loop_p (loop, orig_stmt))
3264 if (reduc_code != ERROR_MARK)
3266 epilogue_cost += add_stmt_cost (target_cost_data, 1, vector_stmt,
3267 stmt_info, 0, vect_epilogue);
3268 epilogue_cost += add_stmt_cost (target_cost_data, 1, vec_to_scalar,
3269 stmt_info, 0, vect_epilogue);
3271 else
3273 int vec_size_in_bits = tree_to_uhwi (TYPE_SIZE (vectype));
3274 tree bitsize =
3275 TYPE_SIZE (TREE_TYPE (gimple_assign_lhs (orig_stmt)));
3276 int element_bitsize = tree_to_uhwi (bitsize);
3277 int nelements = vec_size_in_bits / element_bitsize;
3279 optab = optab_for_tree_code (code, vectype, optab_default);
3281 /* We have a whole vector shift available. */
3282 if (VECTOR_MODE_P (mode)
3283 && optab_handler (optab, mode) != CODE_FOR_nothing
3284 && have_whole_vector_shift (mode))
3286 /* Final reduction via vector shifts and the reduction operator.
3287 Also requires scalar extract. */
3288 epilogue_cost += add_stmt_cost (target_cost_data,
3289 exact_log2 (nelements) * 2,
3290 vector_stmt, stmt_info, 0,
3291 vect_epilogue);
3292 epilogue_cost += add_stmt_cost (target_cost_data, 1,
3293 vec_to_scalar, stmt_info, 0,
3294 vect_epilogue);
3296 else
3297 /* Use extracts and reduction op for final reduction. For N
3298 elements, we have N extracts and N-1 reduction ops. */
3299 epilogue_cost += add_stmt_cost (target_cost_data,
3300 nelements + nelements - 1,
3301 vector_stmt, stmt_info, 0,
3302 vect_epilogue);
3306 if (dump_enabled_p ())
3307 dump_printf (MSG_NOTE,
3308 "vect_model_reduction_cost: inside_cost = %d, "
3309 "prologue_cost = %d, epilogue_cost = %d .\n", inside_cost,
3310 prologue_cost, epilogue_cost);
3312 return true;
3316 /* Function vect_model_induction_cost.
3318 Models cost for induction operations. */
3320 static void
3321 vect_model_induction_cost (stmt_vec_info stmt_info, int ncopies)
3323 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
3324 void *target_cost_data = LOOP_VINFO_TARGET_COST_DATA (loop_vinfo);
3325 unsigned inside_cost, prologue_cost;
3327 /* loop cost for vec_loop. */
3328 inside_cost = add_stmt_cost (target_cost_data, ncopies, vector_stmt,
3329 stmt_info, 0, vect_body);
3331 /* prologue cost for vec_init and vec_step. */
3332 prologue_cost = add_stmt_cost (target_cost_data, 2, scalar_to_vec,
3333 stmt_info, 0, vect_prologue);
3335 if (dump_enabled_p ())
3336 dump_printf_loc (MSG_NOTE, vect_location,
3337 "vect_model_induction_cost: inside_cost = %d, "
3338 "prologue_cost = %d .\n", inside_cost, prologue_cost);
3342 /* Function get_initial_def_for_induction
3344 Input:
3345 STMT - a stmt that performs an induction operation in the loop.
3346 IV_PHI - the initial value of the induction variable
3348 Output:
3349 Return a vector variable, initialized with the first VF values of
3350 the induction variable. E.g., for an iv with IV_PHI='X' and
3351 evolution S, for a vector of 4 units, we want to return:
3352 [X, X + S, X + 2*S, X + 3*S]. */
3354 static tree
3355 get_initial_def_for_induction (gimple iv_phi)
3357 stmt_vec_info stmt_vinfo = vinfo_for_stmt (iv_phi);
3358 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_vinfo);
3359 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
3360 tree vectype;
3361 int nunits;
3362 edge pe = loop_preheader_edge (loop);
3363 struct loop *iv_loop;
3364 basic_block new_bb;
3365 tree new_vec, vec_init, vec_step, t;
3366 tree new_var;
3367 tree new_name;
3368 gimple init_stmt, new_stmt;
3369 gphi *induction_phi;
3370 tree induc_def, vec_def, vec_dest;
3371 tree init_expr, step_expr;
3372 int vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
3373 int i;
3374 int ncopies;
3375 tree expr;
3376 stmt_vec_info phi_info = vinfo_for_stmt (iv_phi);
3377 bool nested_in_vect_loop = false;
3378 gimple_seq stmts = NULL;
3379 imm_use_iterator imm_iter;
3380 use_operand_p use_p;
3381 gimple exit_phi;
3382 edge latch_e;
3383 tree loop_arg;
3384 gimple_stmt_iterator si;
3385 basic_block bb = gimple_bb (iv_phi);
3386 tree stepvectype;
3387 tree resvectype;
3389 /* Is phi in an inner-loop, while vectorizing an enclosing outer-loop? */
3390 if (nested_in_vect_loop_p (loop, iv_phi))
3392 nested_in_vect_loop = true;
3393 iv_loop = loop->inner;
3395 else
3396 iv_loop = loop;
3397 gcc_assert (iv_loop == (gimple_bb (iv_phi))->loop_father);
3399 latch_e = loop_latch_edge (iv_loop);
3400 loop_arg = PHI_ARG_DEF_FROM_EDGE (iv_phi, latch_e);
3402 step_expr = STMT_VINFO_LOOP_PHI_EVOLUTION_PART (phi_info);
3403 gcc_assert (step_expr != NULL_TREE);
3405 pe = loop_preheader_edge (iv_loop);
3406 init_expr = PHI_ARG_DEF_FROM_EDGE (iv_phi,
3407 loop_preheader_edge (iv_loop));
3409 vectype = get_vectype_for_scalar_type (TREE_TYPE (init_expr));
3410 resvectype = get_vectype_for_scalar_type (TREE_TYPE (PHI_RESULT (iv_phi)));
3411 gcc_assert (vectype);
3412 nunits = TYPE_VECTOR_SUBPARTS (vectype);
3413 ncopies = vf / nunits;
3415 gcc_assert (phi_info);
3416 gcc_assert (ncopies >= 1);
3418 /* Convert the step to the desired type. */
3419 step_expr = force_gimple_operand (fold_convert (TREE_TYPE (vectype),
3420 step_expr),
3421 &stmts, true, NULL_TREE);
3422 if (stmts)
3424 new_bb = gsi_insert_seq_on_edge_immediate (pe, stmts);
3425 gcc_assert (!new_bb);
3428 /* Find the first insertion point in the BB. */
3429 si = gsi_after_labels (bb);
3431 /* Create the vector that holds the initial_value of the induction. */
3432 if (nested_in_vect_loop)
3434 /* iv_loop is nested in the loop to be vectorized. init_expr had already
3435 been created during vectorization of previous stmts. We obtain it
3436 from the STMT_VINFO_VEC_STMT of the defining stmt. */
3437 vec_init = vect_get_vec_def_for_operand (init_expr, iv_phi, NULL);
3438 /* If the initial value is not of proper type, convert it. */
3439 if (!useless_type_conversion_p (vectype, TREE_TYPE (vec_init)))
3441 new_stmt
3442 = gimple_build_assign (vect_get_new_vect_var (vectype,
3443 vect_simple_var,
3444 "vec_iv_"),
3445 VIEW_CONVERT_EXPR,
3446 build1 (VIEW_CONVERT_EXPR, vectype,
3447 vec_init));
3448 vec_init = make_ssa_name (gimple_assign_lhs (new_stmt), new_stmt);
3449 gimple_assign_set_lhs (new_stmt, vec_init);
3450 new_bb = gsi_insert_on_edge_immediate (loop_preheader_edge (iv_loop),
3451 new_stmt);
3452 gcc_assert (!new_bb);
3453 set_vinfo_for_stmt (new_stmt,
3454 new_stmt_vec_info (new_stmt, loop_vinfo, NULL));
3457 else
3459 vec<constructor_elt, va_gc> *v;
3461 /* iv_loop is the loop to be vectorized. Create:
3462 vec_init = [X, X+S, X+2*S, X+3*S] (S = step_expr, X = init_expr) */
3463 new_var = vect_get_new_vect_var (TREE_TYPE (vectype),
3464 vect_scalar_var, "var_");
3465 new_name = force_gimple_operand (fold_convert (TREE_TYPE (vectype),
3466 init_expr),
3467 &stmts, false, new_var);
3468 if (stmts)
3470 new_bb = gsi_insert_seq_on_edge_immediate (pe, stmts);
3471 gcc_assert (!new_bb);
3474 vec_alloc (v, nunits);
3475 bool constant_p = is_gimple_min_invariant (new_name);
3476 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, new_name);
3477 for (i = 1; i < nunits; i++)
3479 /* Create: new_name_i = new_name + step_expr */
3480 new_name = fold_build2 (PLUS_EXPR, TREE_TYPE (new_name),
3481 new_name, step_expr);
3482 if (!is_gimple_min_invariant (new_name))
3484 init_stmt = gimple_build_assign (new_var, new_name);
3485 new_name = make_ssa_name (new_var, init_stmt);
3486 gimple_assign_set_lhs (init_stmt, new_name);
3487 new_bb = gsi_insert_on_edge_immediate (pe, init_stmt);
3488 gcc_assert (!new_bb);
3489 if (dump_enabled_p ())
3491 dump_printf_loc (MSG_NOTE, vect_location,
3492 "created new init_stmt: ");
3493 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, init_stmt, 0);
3494 dump_printf (MSG_NOTE, "\n");
3496 constant_p = false;
3498 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, new_name);
3500 /* Create a vector from [new_name_0, new_name_1, ..., new_name_nunits-1] */
3501 if (constant_p)
3502 new_vec = build_vector_from_ctor (vectype, v);
3503 else
3504 new_vec = build_constructor (vectype, v);
3505 vec_init = vect_init_vector (iv_phi, new_vec, vectype, NULL);
3509 /* Create the vector that holds the step of the induction. */
3510 if (nested_in_vect_loop)
3511 /* iv_loop is nested in the loop to be vectorized. Generate:
3512 vec_step = [S, S, S, S] */
3513 new_name = step_expr;
3514 else
3516 /* iv_loop is the loop to be vectorized. Generate:
3517 vec_step = [VF*S, VF*S, VF*S, VF*S] */
3518 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (step_expr)))
3520 expr = build_int_cst (integer_type_node, vf);
3521 expr = fold_convert (TREE_TYPE (step_expr), expr);
3523 else
3524 expr = build_int_cst (TREE_TYPE (step_expr), vf);
3525 new_name = fold_build2 (MULT_EXPR, TREE_TYPE (step_expr),
3526 expr, step_expr);
3527 if (TREE_CODE (step_expr) == SSA_NAME)
3528 new_name = vect_init_vector (iv_phi, new_name,
3529 TREE_TYPE (step_expr), NULL);
3532 t = unshare_expr (new_name);
3533 gcc_assert (CONSTANT_CLASS_P (new_name)
3534 || TREE_CODE (new_name) == SSA_NAME);
3535 stepvectype = get_vectype_for_scalar_type (TREE_TYPE (new_name));
3536 gcc_assert (stepvectype);
3537 new_vec = build_vector_from_val (stepvectype, t);
3538 vec_step = vect_init_vector (iv_phi, new_vec, stepvectype, NULL);
3541 /* Create the following def-use cycle:
3542 loop prolog:
3543 vec_init = ...
3544 vec_step = ...
3545 loop:
3546 vec_iv = PHI <vec_init, vec_loop>
3548 STMT
3550 vec_loop = vec_iv + vec_step; */
3552 /* Create the induction-phi that defines the induction-operand. */
3553 vec_dest = vect_get_new_vect_var (vectype, vect_simple_var, "vec_iv_");
3554 induction_phi = create_phi_node (vec_dest, iv_loop->header);
3555 set_vinfo_for_stmt (induction_phi,
3556 new_stmt_vec_info (induction_phi, loop_vinfo, NULL));
3557 induc_def = PHI_RESULT (induction_phi);
3559 /* Create the iv update inside the loop */
3560 new_stmt = gimple_build_assign (vec_dest, PLUS_EXPR, induc_def, vec_step);
3561 vec_def = make_ssa_name (vec_dest, new_stmt);
3562 gimple_assign_set_lhs (new_stmt, vec_def);
3563 gsi_insert_before (&si, new_stmt, GSI_SAME_STMT);
3564 set_vinfo_for_stmt (new_stmt, new_stmt_vec_info (new_stmt, loop_vinfo,
3565 NULL));
3567 /* Set the arguments of the phi node: */
3568 add_phi_arg (induction_phi, vec_init, pe, UNKNOWN_LOCATION);
3569 add_phi_arg (induction_phi, vec_def, loop_latch_edge (iv_loop),
3570 UNKNOWN_LOCATION);
3573 /* In case that vectorization factor (VF) is bigger than the number
3574 of elements that we can fit in a vectype (nunits), we have to generate
3575 more than one vector stmt - i.e - we need to "unroll" the
3576 vector stmt by a factor VF/nunits. For more details see documentation
3577 in vectorizable_operation. */
3579 if (ncopies > 1)
3581 stmt_vec_info prev_stmt_vinfo;
3582 /* FORNOW. This restriction should be relaxed. */
3583 gcc_assert (!nested_in_vect_loop);
3585 /* Create the vector that holds the step of the induction. */
3586 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (step_expr)))
3588 expr = build_int_cst (integer_type_node, nunits);
3589 expr = fold_convert (TREE_TYPE (step_expr), expr);
3591 else
3592 expr = build_int_cst (TREE_TYPE (step_expr), nunits);
3593 new_name = fold_build2 (MULT_EXPR, TREE_TYPE (step_expr),
3594 expr, step_expr);
3595 if (TREE_CODE (step_expr) == SSA_NAME)
3596 new_name = vect_init_vector (iv_phi, new_name,
3597 TREE_TYPE (step_expr), NULL);
3598 t = unshare_expr (new_name);
3599 gcc_assert (CONSTANT_CLASS_P (new_name)
3600 || TREE_CODE (new_name) == SSA_NAME);
3601 new_vec = build_vector_from_val (stepvectype, t);
3602 vec_step = vect_init_vector (iv_phi, new_vec, stepvectype, NULL);
3604 vec_def = induc_def;
3605 prev_stmt_vinfo = vinfo_for_stmt (induction_phi);
3606 for (i = 1; i < ncopies; i++)
3608 /* vec_i = vec_prev + vec_step */
3609 new_stmt = gimple_build_assign (vec_dest, PLUS_EXPR,
3610 vec_def, vec_step);
3611 vec_def = make_ssa_name (vec_dest, new_stmt);
3612 gimple_assign_set_lhs (new_stmt, vec_def);
3614 gsi_insert_before (&si, new_stmt, GSI_SAME_STMT);
3615 if (!useless_type_conversion_p (resvectype, vectype))
3617 new_stmt
3618 = gimple_build_assign
3619 (vect_get_new_vect_var (resvectype, vect_simple_var,
3620 "vec_iv_"),
3621 VIEW_CONVERT_EXPR,
3622 build1 (VIEW_CONVERT_EXPR, resvectype,
3623 gimple_assign_lhs (new_stmt)));
3624 gimple_assign_set_lhs (new_stmt,
3625 make_ssa_name
3626 (gimple_assign_lhs (new_stmt), new_stmt));
3627 gsi_insert_before (&si, new_stmt, GSI_SAME_STMT);
3629 set_vinfo_for_stmt (new_stmt,
3630 new_stmt_vec_info (new_stmt, loop_vinfo, NULL));
3631 STMT_VINFO_RELATED_STMT (prev_stmt_vinfo) = new_stmt;
3632 prev_stmt_vinfo = vinfo_for_stmt (new_stmt);
3636 if (nested_in_vect_loop)
3638 /* Find the loop-closed exit-phi of the induction, and record
3639 the final vector of induction results: */
3640 exit_phi = NULL;
3641 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, loop_arg)
3643 gimple use_stmt = USE_STMT (use_p);
3644 if (is_gimple_debug (use_stmt))
3645 continue;
3647 if (!flow_bb_inside_loop_p (iv_loop, gimple_bb (use_stmt)))
3649 exit_phi = use_stmt;
3650 break;
3653 if (exit_phi)
3655 stmt_vec_info stmt_vinfo = vinfo_for_stmt (exit_phi);
3656 /* FORNOW. Currently not supporting the case that an inner-loop induction
3657 is not used in the outer-loop (i.e. only outside the outer-loop). */
3658 gcc_assert (STMT_VINFO_RELEVANT_P (stmt_vinfo)
3659 && !STMT_VINFO_LIVE_P (stmt_vinfo));
3661 STMT_VINFO_VEC_STMT (stmt_vinfo) = new_stmt;
3662 if (dump_enabled_p ())
3664 dump_printf_loc (MSG_NOTE, vect_location,
3665 "vector of inductions after inner-loop:");
3666 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, new_stmt, 0);
3667 dump_printf (MSG_NOTE, "\n");
3673 if (dump_enabled_p ())
3675 dump_printf_loc (MSG_NOTE, vect_location,
3676 "transform induction: created def-use cycle: ");
3677 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, induction_phi, 0);
3678 dump_printf (MSG_NOTE, "\n");
3679 dump_gimple_stmt (MSG_NOTE, TDF_SLIM,
3680 SSA_NAME_DEF_STMT (vec_def), 0);
3681 dump_printf (MSG_NOTE, "\n");
3684 STMT_VINFO_VEC_STMT (phi_info) = induction_phi;
3685 if (!useless_type_conversion_p (resvectype, vectype))
3687 new_stmt = gimple_build_assign (vect_get_new_vect_var (resvectype,
3688 vect_simple_var,
3689 "vec_iv_"),
3690 VIEW_CONVERT_EXPR,
3691 build1 (VIEW_CONVERT_EXPR, resvectype,
3692 induc_def));
3693 induc_def = make_ssa_name (gimple_assign_lhs (new_stmt), new_stmt);
3694 gimple_assign_set_lhs (new_stmt, induc_def);
3695 si = gsi_after_labels (bb);
3696 gsi_insert_before (&si, new_stmt, GSI_SAME_STMT);
3697 set_vinfo_for_stmt (new_stmt,
3698 new_stmt_vec_info (new_stmt, loop_vinfo, NULL));
3699 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (new_stmt))
3700 = STMT_VINFO_RELATED_STMT (vinfo_for_stmt (induction_phi));
3703 return induc_def;
3707 /* Function get_initial_def_for_reduction
3709 Input:
3710 STMT - a stmt that performs a reduction operation in the loop.
3711 INIT_VAL - the initial value of the reduction variable
3713 Output:
3714 ADJUSTMENT_DEF - a tree that holds a value to be added to the final result
3715 of the reduction (used for adjusting the epilog - see below).
3716 Return a vector variable, initialized according to the operation that STMT
3717 performs. This vector will be used as the initial value of the
3718 vector of partial results.
3720 Option1 (adjust in epilog): Initialize the vector as follows:
3721 add/bit or/xor: [0,0,...,0,0]
3722 mult/bit and: [1,1,...,1,1]
3723 min/max/cond_expr: [init_val,init_val,..,init_val,init_val]
3724 and when necessary (e.g. add/mult case) let the caller know
3725 that it needs to adjust the result by init_val.
3727 Option2: Initialize the vector as follows:
3728 add/bit or/xor: [init_val,0,0,...,0]
3729 mult/bit and: [init_val,1,1,...,1]
3730 min/max/cond_expr: [init_val,init_val,...,init_val]
3731 and no adjustments are needed.
3733 For example, for the following code:
3735 s = init_val;
3736 for (i=0;i<n;i++)
3737 s = s + a[i];
3739 STMT is 's = s + a[i]', and the reduction variable is 's'.
3740 For a vector of 4 units, we want to return either [0,0,0,init_val],
3741 or [0,0,0,0] and let the caller know that it needs to adjust
3742 the result at the end by 'init_val'.
3744 FORNOW, we are using the 'adjust in epilog' scheme, because this way the
3745 initialization vector is simpler (same element in all entries), if
3746 ADJUSTMENT_DEF is not NULL, and Option2 otherwise.
3748 A cost model should help decide between these two schemes. */
3750 tree
3751 get_initial_def_for_reduction (gimple stmt, tree init_val,
3752 tree *adjustment_def)
3754 stmt_vec_info stmt_vinfo = vinfo_for_stmt (stmt);
3755 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_vinfo);
3756 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
3757 tree scalar_type = TREE_TYPE (init_val);
3758 tree vectype = get_vectype_for_scalar_type (scalar_type);
3759 int nunits;
3760 enum tree_code code = gimple_assign_rhs_code (stmt);
3761 tree def_for_init;
3762 tree init_def;
3763 tree *elts;
3764 int i;
3765 bool nested_in_vect_loop = false;
3766 tree init_value;
3767 REAL_VALUE_TYPE real_init_val = dconst0;
3768 int int_init_val = 0;
3769 gimple def_stmt = NULL;
3771 gcc_assert (vectype);
3772 nunits = TYPE_VECTOR_SUBPARTS (vectype);
3774 gcc_assert (POINTER_TYPE_P (scalar_type) || INTEGRAL_TYPE_P (scalar_type)
3775 || SCALAR_FLOAT_TYPE_P (scalar_type));
3777 if (nested_in_vect_loop_p (loop, stmt))
3778 nested_in_vect_loop = true;
3779 else
3780 gcc_assert (loop == (gimple_bb (stmt))->loop_father);
3782 /* In case of double reduction we only create a vector variable to be put
3783 in the reduction phi node. The actual statement creation is done in
3784 vect_create_epilog_for_reduction. */
3785 if (adjustment_def && nested_in_vect_loop
3786 && TREE_CODE (init_val) == SSA_NAME
3787 && (def_stmt = SSA_NAME_DEF_STMT (init_val))
3788 && gimple_code (def_stmt) == GIMPLE_PHI
3789 && flow_bb_inside_loop_p (loop, gimple_bb (def_stmt))
3790 && vinfo_for_stmt (def_stmt)
3791 && STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def_stmt))
3792 == vect_double_reduction_def)
3794 *adjustment_def = NULL;
3795 return vect_create_destination_var (init_val, vectype);
3798 if (TREE_CONSTANT (init_val))
3800 if (SCALAR_FLOAT_TYPE_P (scalar_type))
3801 init_value = build_real (scalar_type, TREE_REAL_CST (init_val));
3802 else
3803 init_value = build_int_cst (scalar_type, TREE_INT_CST_LOW (init_val));
3805 else
3806 init_value = init_val;
3808 switch (code)
3810 case WIDEN_SUM_EXPR:
3811 case DOT_PROD_EXPR:
3812 case SAD_EXPR:
3813 case PLUS_EXPR:
3814 case MINUS_EXPR:
3815 case BIT_IOR_EXPR:
3816 case BIT_XOR_EXPR:
3817 case MULT_EXPR:
3818 case BIT_AND_EXPR:
3819 /* ADJUSMENT_DEF is NULL when called from
3820 vect_create_epilog_for_reduction to vectorize double reduction. */
3821 if (adjustment_def)
3823 if (nested_in_vect_loop)
3824 *adjustment_def = vect_get_vec_def_for_operand (init_val, stmt,
3825 NULL);
3826 else
3827 *adjustment_def = init_val;
3830 if (code == MULT_EXPR)
3832 real_init_val = dconst1;
3833 int_init_val = 1;
3836 if (code == BIT_AND_EXPR)
3837 int_init_val = -1;
3839 if (SCALAR_FLOAT_TYPE_P (scalar_type))
3840 def_for_init = build_real (scalar_type, real_init_val);
3841 else
3842 def_for_init = build_int_cst (scalar_type, int_init_val);
3844 /* Create a vector of '0' or '1' except the first element. */
3845 elts = XALLOCAVEC (tree, nunits);
3846 for (i = nunits - 2; i >= 0; --i)
3847 elts[i + 1] = def_for_init;
3849 /* Option1: the first element is '0' or '1' as well. */
3850 if (adjustment_def)
3852 elts[0] = def_for_init;
3853 init_def = build_vector (vectype, elts);
3854 break;
3857 /* Option2: the first element is INIT_VAL. */
3858 elts[0] = init_val;
3859 if (TREE_CONSTANT (init_val))
3860 init_def = build_vector (vectype, elts);
3861 else
3863 vec<constructor_elt, va_gc> *v;
3864 vec_alloc (v, nunits);
3865 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init_val);
3866 for (i = 1; i < nunits; ++i)
3867 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, elts[i]);
3868 init_def = build_constructor (vectype, v);
3871 break;
3873 case MIN_EXPR:
3874 case MAX_EXPR:
3875 case COND_EXPR:
3876 if (adjustment_def)
3878 *adjustment_def = NULL_TREE;
3879 init_def = vect_get_vec_def_for_operand (init_val, stmt, NULL);
3880 break;
3883 init_def = build_vector_from_val (vectype, init_value);
3884 break;
3886 default:
3887 gcc_unreachable ();
3890 return init_def;
3893 /* Function vect_create_epilog_for_reduction
3895 Create code at the loop-epilog to finalize the result of a reduction
3896 computation.
3898 VECT_DEFS is list of vector of partial results, i.e., the lhs's of vector
3899 reduction statements.
3900 STMT is the scalar reduction stmt that is being vectorized.
3901 NCOPIES is > 1 in case the vectorization factor (VF) is bigger than the
3902 number of elements that we can fit in a vectype (nunits). In this case
3903 we have to generate more than one vector stmt - i.e - we need to "unroll"
3904 the vector stmt by a factor VF/nunits. For more details see documentation
3905 in vectorizable_operation.
3906 REDUC_CODE is the tree-code for the epilog reduction.
3907 REDUCTION_PHIS is a list of the phi-nodes that carry the reduction
3908 computation.
3909 REDUC_INDEX is the index of the operand in the right hand side of the
3910 statement that is defined by REDUCTION_PHI.
3911 DOUBLE_REDUC is TRUE if double reduction phi nodes should be handled.
3912 SLP_NODE is an SLP node containing a group of reduction statements. The
3913 first one in this group is STMT.
3915 This function:
3916 1. Creates the reduction def-use cycles: sets the arguments for
3917 REDUCTION_PHIS:
3918 The loop-entry argument is the vectorized initial-value of the reduction.
3919 The loop-latch argument is taken from VECT_DEFS - the vector of partial
3920 sums.
3921 2. "Reduces" each vector of partial results VECT_DEFS into a single result,
3922 by applying the operation specified by REDUC_CODE if available, or by
3923 other means (whole-vector shifts or a scalar loop).
3924 The function also creates a new phi node at the loop exit to preserve
3925 loop-closed form, as illustrated below.
3927 The flow at the entry to this function:
3929 loop:
3930 vec_def = phi <null, null> # REDUCTION_PHI
3931 VECT_DEF = vector_stmt # vectorized form of STMT
3932 s_loop = scalar_stmt # (scalar) STMT
3933 loop_exit:
3934 s_out0 = phi <s_loop> # (scalar) EXIT_PHI
3935 use <s_out0>
3936 use <s_out0>
3938 The above is transformed by this function into:
3940 loop:
3941 vec_def = phi <vec_init, VECT_DEF> # REDUCTION_PHI
3942 VECT_DEF = vector_stmt # vectorized form of STMT
3943 s_loop = scalar_stmt # (scalar) STMT
3944 loop_exit:
3945 s_out0 = phi <s_loop> # (scalar) EXIT_PHI
3946 v_out1 = phi <VECT_DEF> # NEW_EXIT_PHI
3947 v_out2 = reduce <v_out1>
3948 s_out3 = extract_field <v_out2, 0>
3949 s_out4 = adjust_result <s_out3>
3950 use <s_out4>
3951 use <s_out4>
3954 static void
3955 vect_create_epilog_for_reduction (vec<tree> vect_defs, gimple stmt,
3956 int ncopies, enum tree_code reduc_code,
3957 vec<gimple> reduction_phis,
3958 int reduc_index, bool double_reduc,
3959 slp_tree slp_node)
3961 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
3962 stmt_vec_info prev_phi_info;
3963 tree vectype;
3964 machine_mode mode;
3965 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
3966 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo), *outer_loop = NULL;
3967 basic_block exit_bb;
3968 tree scalar_dest;
3969 tree scalar_type;
3970 gimple new_phi = NULL, phi;
3971 gimple_stmt_iterator exit_gsi;
3972 tree vec_dest;
3973 tree new_temp = NULL_TREE, new_dest, new_name, new_scalar_dest;
3974 gimple epilog_stmt = NULL;
3975 enum tree_code code = gimple_assign_rhs_code (stmt);
3976 gimple exit_phi;
3977 tree bitsize;
3978 tree adjustment_def = NULL;
3979 tree vec_initial_def = NULL;
3980 tree reduction_op, expr, def;
3981 tree orig_name, scalar_result;
3982 imm_use_iterator imm_iter, phi_imm_iter;
3983 use_operand_p use_p, phi_use_p;
3984 gimple use_stmt, orig_stmt, reduction_phi = NULL;
3985 bool nested_in_vect_loop = false;
3986 auto_vec<gimple> new_phis;
3987 auto_vec<gimple> inner_phis;
3988 enum vect_def_type dt = vect_unknown_def_type;
3989 int j, i;
3990 auto_vec<tree> scalar_results;
3991 unsigned int group_size = 1, k, ratio;
3992 auto_vec<tree> vec_initial_defs;
3993 auto_vec<gimple> phis;
3994 bool slp_reduc = false;
3995 tree new_phi_result;
3996 gimple inner_phi = NULL;
3998 if (slp_node)
3999 group_size = SLP_TREE_SCALAR_STMTS (slp_node).length ();
4001 if (nested_in_vect_loop_p (loop, stmt))
4003 outer_loop = loop;
4004 loop = loop->inner;
4005 nested_in_vect_loop = true;
4006 gcc_assert (!slp_node);
4009 reduction_op = get_reduction_op (stmt, reduc_index);
4011 vectype = get_vectype_for_scalar_type (TREE_TYPE (reduction_op));
4012 gcc_assert (vectype);
4013 mode = TYPE_MODE (vectype);
4015 /* 1. Create the reduction def-use cycle:
4016 Set the arguments of REDUCTION_PHIS, i.e., transform
4018 loop:
4019 vec_def = phi <null, null> # REDUCTION_PHI
4020 VECT_DEF = vector_stmt # vectorized form of STMT
4023 into:
4025 loop:
4026 vec_def = phi <vec_init, VECT_DEF> # REDUCTION_PHI
4027 VECT_DEF = vector_stmt # vectorized form of STMT
4030 (in case of SLP, do it for all the phis). */
4032 /* Get the loop-entry arguments. */
4033 if (slp_node)
4034 vect_get_vec_defs (reduction_op, NULL_TREE, stmt, &vec_initial_defs,
4035 NULL, slp_node, reduc_index);
4036 else
4038 vec_initial_defs.create (1);
4039 /* For the case of reduction, vect_get_vec_def_for_operand returns
4040 the scalar def before the loop, that defines the initial value
4041 of the reduction variable. */
4042 vec_initial_def = vect_get_vec_def_for_operand (reduction_op, stmt,
4043 &adjustment_def);
4044 vec_initial_defs.quick_push (vec_initial_def);
4047 /* Set phi nodes arguments. */
4048 FOR_EACH_VEC_ELT (reduction_phis, i, phi)
4050 tree vec_init_def, def;
4051 gimple_seq stmts;
4052 vec_init_def = force_gimple_operand (vec_initial_defs[i], &stmts,
4053 true, NULL_TREE);
4054 gsi_insert_seq_on_edge_immediate (loop_preheader_edge (loop), stmts);
4055 def = vect_defs[i];
4056 for (j = 0; j < ncopies; j++)
4058 /* Set the loop-entry arg of the reduction-phi. */
4059 add_phi_arg (as_a <gphi *> (phi), vec_init_def,
4060 loop_preheader_edge (loop), UNKNOWN_LOCATION);
4062 /* Set the loop-latch arg for the reduction-phi. */
4063 if (j > 0)
4064 def = vect_get_vec_def_for_stmt_copy (vect_unknown_def_type, def);
4066 add_phi_arg (as_a <gphi *> (phi), def, loop_latch_edge (loop),
4067 UNKNOWN_LOCATION);
4069 if (dump_enabled_p ())
4071 dump_printf_loc (MSG_NOTE, vect_location,
4072 "transform reduction: created def-use cycle: ");
4073 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, phi, 0);
4074 dump_printf (MSG_NOTE, "\n");
4075 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, SSA_NAME_DEF_STMT (def), 0);
4076 dump_printf (MSG_NOTE, "\n");
4079 phi = STMT_VINFO_RELATED_STMT (vinfo_for_stmt (phi));
4083 /* 2. Create epilog code.
4084 The reduction epilog code operates across the elements of the vector
4085 of partial results computed by the vectorized loop.
4086 The reduction epilog code consists of:
4088 step 1: compute the scalar result in a vector (v_out2)
4089 step 2: extract the scalar result (s_out3) from the vector (v_out2)
4090 step 3: adjust the scalar result (s_out3) if needed.
4092 Step 1 can be accomplished using one the following three schemes:
4093 (scheme 1) using reduc_code, if available.
4094 (scheme 2) using whole-vector shifts, if available.
4095 (scheme 3) using a scalar loop. In this case steps 1+2 above are
4096 combined.
4098 The overall epilog code looks like this:
4100 s_out0 = phi <s_loop> # original EXIT_PHI
4101 v_out1 = phi <VECT_DEF> # NEW_EXIT_PHI
4102 v_out2 = reduce <v_out1> # step 1
4103 s_out3 = extract_field <v_out2, 0> # step 2
4104 s_out4 = adjust_result <s_out3> # step 3
4106 (step 3 is optional, and steps 1 and 2 may be combined).
4107 Lastly, the uses of s_out0 are replaced by s_out4. */
4110 /* 2.1 Create new loop-exit-phis to preserve loop-closed form:
4111 v_out1 = phi <VECT_DEF>
4112 Store them in NEW_PHIS. */
4114 exit_bb = single_exit (loop)->dest;
4115 prev_phi_info = NULL;
4116 new_phis.create (vect_defs.length ());
4117 FOR_EACH_VEC_ELT (vect_defs, i, def)
4119 for (j = 0; j < ncopies; j++)
4121 tree new_def = copy_ssa_name (def);
4122 phi = create_phi_node (new_def, exit_bb);
4123 set_vinfo_for_stmt (phi, new_stmt_vec_info (phi, loop_vinfo, NULL));
4124 if (j == 0)
4125 new_phis.quick_push (phi);
4126 else
4128 def = vect_get_vec_def_for_stmt_copy (dt, def);
4129 STMT_VINFO_RELATED_STMT (prev_phi_info) = phi;
4132 SET_PHI_ARG_DEF (phi, single_exit (loop)->dest_idx, def);
4133 prev_phi_info = vinfo_for_stmt (phi);
4137 /* The epilogue is created for the outer-loop, i.e., for the loop being
4138 vectorized. Create exit phis for the outer loop. */
4139 if (double_reduc)
4141 loop = outer_loop;
4142 exit_bb = single_exit (loop)->dest;
4143 inner_phis.create (vect_defs.length ());
4144 FOR_EACH_VEC_ELT (new_phis, i, phi)
4146 tree new_result = copy_ssa_name (PHI_RESULT (phi));
4147 gphi *outer_phi = create_phi_node (new_result, exit_bb);
4148 SET_PHI_ARG_DEF (outer_phi, single_exit (loop)->dest_idx,
4149 PHI_RESULT (phi));
4150 set_vinfo_for_stmt (outer_phi, new_stmt_vec_info (outer_phi,
4151 loop_vinfo, NULL));
4152 inner_phis.quick_push (phi);
4153 new_phis[i] = outer_phi;
4154 prev_phi_info = vinfo_for_stmt (outer_phi);
4155 while (STMT_VINFO_RELATED_STMT (vinfo_for_stmt (phi)))
4157 phi = STMT_VINFO_RELATED_STMT (vinfo_for_stmt (phi));
4158 new_result = copy_ssa_name (PHI_RESULT (phi));
4159 outer_phi = create_phi_node (new_result, exit_bb);
4160 SET_PHI_ARG_DEF (outer_phi, single_exit (loop)->dest_idx,
4161 PHI_RESULT (phi));
4162 set_vinfo_for_stmt (outer_phi, new_stmt_vec_info (outer_phi,
4163 loop_vinfo, NULL));
4164 STMT_VINFO_RELATED_STMT (prev_phi_info) = outer_phi;
4165 prev_phi_info = vinfo_for_stmt (outer_phi);
4170 exit_gsi = gsi_after_labels (exit_bb);
4172 /* 2.2 Get the relevant tree-code to use in the epilog for schemes 2,3
4173 (i.e. when reduc_code is not available) and in the final adjustment
4174 code (if needed). Also get the original scalar reduction variable as
4175 defined in the loop. In case STMT is a "pattern-stmt" (i.e. - it
4176 represents a reduction pattern), the tree-code and scalar-def are
4177 taken from the original stmt that the pattern-stmt (STMT) replaces.
4178 Otherwise (it is a regular reduction) - the tree-code and scalar-def
4179 are taken from STMT. */
4181 orig_stmt = STMT_VINFO_RELATED_STMT (stmt_info);
4182 if (!orig_stmt)
4184 /* Regular reduction */
4185 orig_stmt = stmt;
4187 else
4189 /* Reduction pattern */
4190 stmt_vec_info stmt_vinfo = vinfo_for_stmt (orig_stmt);
4191 gcc_assert (STMT_VINFO_IN_PATTERN_P (stmt_vinfo));
4192 gcc_assert (STMT_VINFO_RELATED_STMT (stmt_vinfo) == stmt);
4195 code = gimple_assign_rhs_code (orig_stmt);
4196 /* For MINUS_EXPR the initial vector is [init_val,0,...,0], therefore,
4197 partial results are added and not subtracted. */
4198 if (code == MINUS_EXPR)
4199 code = PLUS_EXPR;
4201 scalar_dest = gimple_assign_lhs (orig_stmt);
4202 scalar_type = TREE_TYPE (scalar_dest);
4203 scalar_results.create (group_size);
4204 new_scalar_dest = vect_create_destination_var (scalar_dest, NULL);
4205 bitsize = TYPE_SIZE (scalar_type);
4207 /* In case this is a reduction in an inner-loop while vectorizing an outer
4208 loop - we don't need to extract a single scalar result at the end of the
4209 inner-loop (unless it is double reduction, i.e., the use of reduction is
4210 outside the outer-loop). The final vector of partial results will be used
4211 in the vectorized outer-loop, or reduced to a scalar result at the end of
4212 the outer-loop. */
4213 if (nested_in_vect_loop && !double_reduc)
4214 goto vect_finalize_reduction;
4216 /* SLP reduction without reduction chain, e.g.,
4217 # a1 = phi <a2, a0>
4218 # b1 = phi <b2, b0>
4219 a2 = operation (a1)
4220 b2 = operation (b1) */
4221 slp_reduc = (slp_node && !GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)));
4223 /* In case of reduction chain, e.g.,
4224 # a1 = phi <a3, a0>
4225 a2 = operation (a1)
4226 a3 = operation (a2),
4228 we may end up with more than one vector result. Here we reduce them to
4229 one vector. */
4230 if (GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)))
4232 tree first_vect = PHI_RESULT (new_phis[0]);
4233 tree tmp;
4234 gassign *new_vec_stmt = NULL;
4236 vec_dest = vect_create_destination_var (scalar_dest, vectype);
4237 for (k = 1; k < new_phis.length (); k++)
4239 gimple next_phi = new_phis[k];
4240 tree second_vect = PHI_RESULT (next_phi);
4242 tmp = build2 (code, vectype, first_vect, second_vect);
4243 new_vec_stmt = gimple_build_assign (vec_dest, tmp);
4244 first_vect = make_ssa_name (vec_dest, new_vec_stmt);
4245 gimple_assign_set_lhs (new_vec_stmt, first_vect);
4246 gsi_insert_before (&exit_gsi, new_vec_stmt, GSI_SAME_STMT);
4249 new_phi_result = first_vect;
4250 if (new_vec_stmt)
4252 new_phis.truncate (0);
4253 new_phis.safe_push (new_vec_stmt);
4256 else
4257 new_phi_result = PHI_RESULT (new_phis[0]);
4259 /* 2.3 Create the reduction code, using one of the three schemes described
4260 above. In SLP we simply need to extract all the elements from the
4261 vector (without reducing them), so we use scalar shifts. */
4262 if (reduc_code != ERROR_MARK && !slp_reduc)
4264 tree tmp;
4265 tree vec_elem_type;
4267 /*** Case 1: Create:
4268 v_out2 = reduc_expr <v_out1> */
4270 if (dump_enabled_p ())
4271 dump_printf_loc (MSG_NOTE, vect_location,
4272 "Reduce using direct vector reduction.\n");
4274 vec_elem_type = TREE_TYPE (TREE_TYPE (new_phi_result));
4275 if (!useless_type_conversion_p (scalar_type, vec_elem_type))
4277 tree tmp_dest =
4278 vect_create_destination_var (scalar_dest, vec_elem_type);
4279 tmp = build1 (reduc_code, vec_elem_type, new_phi_result);
4280 epilog_stmt = gimple_build_assign (tmp_dest, tmp);
4281 new_temp = make_ssa_name (tmp_dest, epilog_stmt);
4282 gimple_assign_set_lhs (epilog_stmt, new_temp);
4283 gsi_insert_before (&exit_gsi, epilog_stmt, GSI_SAME_STMT);
4285 tmp = build1 (NOP_EXPR, scalar_type, new_temp);
4287 else
4288 tmp = build1 (reduc_code, scalar_type, new_phi_result);
4289 epilog_stmt = gimple_build_assign (new_scalar_dest, tmp);
4290 new_temp = make_ssa_name (new_scalar_dest, epilog_stmt);
4291 gimple_assign_set_lhs (epilog_stmt, new_temp);
4292 gsi_insert_before (&exit_gsi, epilog_stmt, GSI_SAME_STMT);
4293 scalar_results.safe_push (new_temp);
4295 else
4297 bool reduce_with_shift = have_whole_vector_shift (mode);
4298 int element_bitsize = tree_to_uhwi (bitsize);
4299 int vec_size_in_bits = tree_to_uhwi (TYPE_SIZE (vectype));
4300 tree vec_temp;
4302 /* Regardless of whether we have a whole vector shift, if we're
4303 emulating the operation via tree-vect-generic, we don't want
4304 to use it. Only the first round of the reduction is likely
4305 to still be profitable via emulation. */
4306 /* ??? It might be better to emit a reduction tree code here, so that
4307 tree-vect-generic can expand the first round via bit tricks. */
4308 if (!VECTOR_MODE_P (mode))
4309 reduce_with_shift = false;
4310 else
4312 optab optab = optab_for_tree_code (code, vectype, optab_default);
4313 if (optab_handler (optab, mode) == CODE_FOR_nothing)
4314 reduce_with_shift = false;
4317 if (reduce_with_shift && !slp_reduc)
4319 int nelements = vec_size_in_bits / element_bitsize;
4320 unsigned char *sel = XALLOCAVEC (unsigned char, nelements);
4322 int elt_offset;
4324 tree zero_vec = build_zero_cst (vectype);
4325 /*** Case 2: Create:
4326 for (offset = nelements/2; offset >= 1; offset/=2)
4328 Create: va' = vec_shift <va, offset>
4329 Create: va = vop <va, va'>
4330 } */
4332 tree rhs;
4334 if (dump_enabled_p ())
4335 dump_printf_loc (MSG_NOTE, vect_location,
4336 "Reduce using vector shifts\n");
4338 vec_dest = vect_create_destination_var (scalar_dest, vectype);
4339 new_temp = new_phi_result;
4340 for (elt_offset = nelements / 2;
4341 elt_offset >= 1;
4342 elt_offset /= 2)
4344 calc_vec_perm_mask_for_shift (mode, elt_offset, sel);
4345 tree mask = vect_gen_perm_mask_any (vectype, sel);
4346 epilog_stmt = gimple_build_assign (vec_dest, VEC_PERM_EXPR,
4347 new_temp, zero_vec, mask);
4348 new_name = make_ssa_name (vec_dest, epilog_stmt);
4349 gimple_assign_set_lhs (epilog_stmt, new_name);
4350 gsi_insert_before (&exit_gsi, epilog_stmt, GSI_SAME_STMT);
4352 epilog_stmt = gimple_build_assign (vec_dest, code, new_name,
4353 new_temp);
4354 new_temp = make_ssa_name (vec_dest, epilog_stmt);
4355 gimple_assign_set_lhs (epilog_stmt, new_temp);
4356 gsi_insert_before (&exit_gsi, epilog_stmt, GSI_SAME_STMT);
4359 /* 2.4 Extract the final scalar result. Create:
4360 s_out3 = extract_field <v_out2, bitpos> */
4362 if (dump_enabled_p ())
4363 dump_printf_loc (MSG_NOTE, vect_location,
4364 "extract scalar result\n");
4366 rhs = build3 (BIT_FIELD_REF, scalar_type, new_temp,
4367 bitsize, bitsize_zero_node);
4368 epilog_stmt = gimple_build_assign (new_scalar_dest, rhs);
4369 new_temp = make_ssa_name (new_scalar_dest, epilog_stmt);
4370 gimple_assign_set_lhs (epilog_stmt, new_temp);
4371 gsi_insert_before (&exit_gsi, epilog_stmt, GSI_SAME_STMT);
4372 scalar_results.safe_push (new_temp);
4374 else
4376 /*** Case 3: Create:
4377 s = extract_field <v_out2, 0>
4378 for (offset = element_size;
4379 offset < vector_size;
4380 offset += element_size;)
4382 Create: s' = extract_field <v_out2, offset>
4383 Create: s = op <s, s'> // For non SLP cases
4384 } */
4386 if (dump_enabled_p ())
4387 dump_printf_loc (MSG_NOTE, vect_location,
4388 "Reduce using scalar code.\n");
4390 vec_size_in_bits = tree_to_uhwi (TYPE_SIZE (vectype));
4391 FOR_EACH_VEC_ELT (new_phis, i, new_phi)
4393 int bit_offset;
4394 if (gimple_code (new_phi) == GIMPLE_PHI)
4395 vec_temp = PHI_RESULT (new_phi);
4396 else
4397 vec_temp = gimple_assign_lhs (new_phi);
4398 tree rhs = build3 (BIT_FIELD_REF, scalar_type, vec_temp, bitsize,
4399 bitsize_zero_node);
4400 epilog_stmt = gimple_build_assign (new_scalar_dest, rhs);
4401 new_temp = make_ssa_name (new_scalar_dest, epilog_stmt);
4402 gimple_assign_set_lhs (epilog_stmt, new_temp);
4403 gsi_insert_before (&exit_gsi, epilog_stmt, GSI_SAME_STMT);
4405 /* In SLP we don't need to apply reduction operation, so we just
4406 collect s' values in SCALAR_RESULTS. */
4407 if (slp_reduc)
4408 scalar_results.safe_push (new_temp);
4410 for (bit_offset = element_bitsize;
4411 bit_offset < vec_size_in_bits;
4412 bit_offset += element_bitsize)
4414 tree bitpos = bitsize_int (bit_offset);
4415 tree rhs = build3 (BIT_FIELD_REF, scalar_type, vec_temp,
4416 bitsize, bitpos);
4418 epilog_stmt = gimple_build_assign (new_scalar_dest, rhs);
4419 new_name = make_ssa_name (new_scalar_dest, epilog_stmt);
4420 gimple_assign_set_lhs (epilog_stmt, new_name);
4421 gsi_insert_before (&exit_gsi, epilog_stmt, GSI_SAME_STMT);
4423 if (slp_reduc)
4425 /* In SLP we don't need to apply reduction operation, so
4426 we just collect s' values in SCALAR_RESULTS. */
4427 new_temp = new_name;
4428 scalar_results.safe_push (new_name);
4430 else
4432 epilog_stmt = gimple_build_assign (new_scalar_dest, code,
4433 new_name, new_temp);
4434 new_temp = make_ssa_name (new_scalar_dest, epilog_stmt);
4435 gimple_assign_set_lhs (epilog_stmt, new_temp);
4436 gsi_insert_before (&exit_gsi, epilog_stmt, GSI_SAME_STMT);
4441 /* The only case where we need to reduce scalar results in SLP, is
4442 unrolling. If the size of SCALAR_RESULTS is greater than
4443 GROUP_SIZE, we reduce them combining elements modulo
4444 GROUP_SIZE. */
4445 if (slp_reduc)
4447 tree res, first_res, new_res;
4448 gimple new_stmt;
4450 /* Reduce multiple scalar results in case of SLP unrolling. */
4451 for (j = group_size; scalar_results.iterate (j, &res);
4452 j++)
4454 first_res = scalar_results[j % group_size];
4455 new_stmt = gimple_build_assign (new_scalar_dest, code,
4456 first_res, res);
4457 new_res = make_ssa_name (new_scalar_dest, new_stmt);
4458 gimple_assign_set_lhs (new_stmt, new_res);
4459 gsi_insert_before (&exit_gsi, new_stmt, GSI_SAME_STMT);
4460 scalar_results[j % group_size] = new_res;
4463 else
4464 /* Not SLP - we have one scalar to keep in SCALAR_RESULTS. */
4465 scalar_results.safe_push (new_temp);
4469 vect_finalize_reduction:
4471 if (double_reduc)
4472 loop = loop->inner;
4474 /* 2.5 Adjust the final result by the initial value of the reduction
4475 variable. (When such adjustment is not needed, then
4476 'adjustment_def' is zero). For example, if code is PLUS we create:
4477 new_temp = loop_exit_def + adjustment_def */
4479 if (adjustment_def)
4481 gcc_assert (!slp_reduc);
4482 if (nested_in_vect_loop)
4484 new_phi = new_phis[0];
4485 gcc_assert (TREE_CODE (TREE_TYPE (adjustment_def)) == VECTOR_TYPE);
4486 expr = build2 (code, vectype, PHI_RESULT (new_phi), adjustment_def);
4487 new_dest = vect_create_destination_var (scalar_dest, vectype);
4489 else
4491 new_temp = scalar_results[0];
4492 gcc_assert (TREE_CODE (TREE_TYPE (adjustment_def)) != VECTOR_TYPE);
4493 expr = build2 (code, scalar_type, new_temp, adjustment_def);
4494 new_dest = vect_create_destination_var (scalar_dest, scalar_type);
4497 epilog_stmt = gimple_build_assign (new_dest, expr);
4498 new_temp = make_ssa_name (new_dest, epilog_stmt);
4499 gimple_assign_set_lhs (epilog_stmt, new_temp);
4500 gsi_insert_before (&exit_gsi, epilog_stmt, GSI_SAME_STMT);
4501 if (nested_in_vect_loop)
4503 set_vinfo_for_stmt (epilog_stmt,
4504 new_stmt_vec_info (epilog_stmt, loop_vinfo,
4505 NULL));
4506 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (epilog_stmt)) =
4507 STMT_VINFO_RELATED_STMT (vinfo_for_stmt (new_phi));
4509 if (!double_reduc)
4510 scalar_results.quick_push (new_temp);
4511 else
4512 scalar_results[0] = new_temp;
4514 else
4515 scalar_results[0] = new_temp;
4517 new_phis[0] = epilog_stmt;
4520 /* 2.6 Handle the loop-exit phis. Replace the uses of scalar loop-exit
4521 phis with new adjusted scalar results, i.e., replace use <s_out0>
4522 with use <s_out4>.
4524 Transform:
4525 loop_exit:
4526 s_out0 = phi <s_loop> # (scalar) EXIT_PHI
4527 v_out1 = phi <VECT_DEF> # NEW_EXIT_PHI
4528 v_out2 = reduce <v_out1>
4529 s_out3 = extract_field <v_out2, 0>
4530 s_out4 = adjust_result <s_out3>
4531 use <s_out0>
4532 use <s_out0>
4534 into:
4536 loop_exit:
4537 s_out0 = phi <s_loop> # (scalar) EXIT_PHI
4538 v_out1 = phi <VECT_DEF> # NEW_EXIT_PHI
4539 v_out2 = reduce <v_out1>
4540 s_out3 = extract_field <v_out2, 0>
4541 s_out4 = adjust_result <s_out3>
4542 use <s_out4>
4543 use <s_out4> */
4546 /* In SLP reduction chain we reduce vector results into one vector if
4547 necessary, hence we set here GROUP_SIZE to 1. SCALAR_DEST is the LHS of
4548 the last stmt in the reduction chain, since we are looking for the loop
4549 exit phi node. */
4550 if (GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)))
4552 scalar_dest = gimple_assign_lhs (
4553 SLP_TREE_SCALAR_STMTS (slp_node)[group_size - 1]);
4554 group_size = 1;
4557 /* In SLP we may have several statements in NEW_PHIS and REDUCTION_PHIS (in
4558 case that GROUP_SIZE is greater than vectorization factor). Therefore, we
4559 need to match SCALAR_RESULTS with corresponding statements. The first
4560 (GROUP_SIZE / number of new vector stmts) scalar results correspond to
4561 the first vector stmt, etc.
4562 (RATIO is equal to (GROUP_SIZE / number of new vector stmts)). */
4563 if (group_size > new_phis.length ())
4565 ratio = group_size / new_phis.length ();
4566 gcc_assert (!(group_size % new_phis.length ()));
4568 else
4569 ratio = 1;
4571 for (k = 0; k < group_size; k++)
4573 if (k % ratio == 0)
4575 epilog_stmt = new_phis[k / ratio];
4576 reduction_phi = reduction_phis[k / ratio];
4577 if (double_reduc)
4578 inner_phi = inner_phis[k / ratio];
4581 if (slp_reduc)
4583 gimple current_stmt = SLP_TREE_SCALAR_STMTS (slp_node)[k];
4585 orig_stmt = STMT_VINFO_RELATED_STMT (vinfo_for_stmt (current_stmt));
4586 /* SLP statements can't participate in patterns. */
4587 gcc_assert (!orig_stmt);
4588 scalar_dest = gimple_assign_lhs (current_stmt);
4591 phis.create (3);
4592 /* Find the loop-closed-use at the loop exit of the original scalar
4593 result. (The reduction result is expected to have two immediate uses -
4594 one at the latch block, and one at the loop exit). */
4595 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, scalar_dest)
4596 if (!flow_bb_inside_loop_p (loop, gimple_bb (USE_STMT (use_p)))
4597 && !is_gimple_debug (USE_STMT (use_p)))
4598 phis.safe_push (USE_STMT (use_p));
4600 /* While we expect to have found an exit_phi because of loop-closed-ssa
4601 form we can end up without one if the scalar cycle is dead. */
4603 FOR_EACH_VEC_ELT (phis, i, exit_phi)
4605 if (outer_loop)
4607 stmt_vec_info exit_phi_vinfo = vinfo_for_stmt (exit_phi);
4608 gphi *vect_phi;
4610 /* FORNOW. Currently not supporting the case that an inner-loop
4611 reduction is not used in the outer-loop (but only outside the
4612 outer-loop), unless it is double reduction. */
4613 gcc_assert ((STMT_VINFO_RELEVANT_P (exit_phi_vinfo)
4614 && !STMT_VINFO_LIVE_P (exit_phi_vinfo))
4615 || double_reduc);
4617 if (double_reduc)
4618 STMT_VINFO_VEC_STMT (exit_phi_vinfo) = inner_phi;
4619 else
4620 STMT_VINFO_VEC_STMT (exit_phi_vinfo) = epilog_stmt;
4621 if (!double_reduc
4622 || STMT_VINFO_DEF_TYPE (exit_phi_vinfo)
4623 != vect_double_reduction_def)
4624 continue;
4626 /* Handle double reduction:
4628 stmt1: s1 = phi <s0, s2> - double reduction phi (outer loop)
4629 stmt2: s3 = phi <s1, s4> - (regular) reduc phi (inner loop)
4630 stmt3: s4 = use (s3) - (regular) reduc stmt (inner loop)
4631 stmt4: s2 = phi <s4> - double reduction stmt (outer loop)
4633 At that point the regular reduction (stmt2 and stmt3) is
4634 already vectorized, as well as the exit phi node, stmt4.
4635 Here we vectorize the phi node of double reduction, stmt1, and
4636 update all relevant statements. */
4638 /* Go through all the uses of s2 to find double reduction phi
4639 node, i.e., stmt1 above. */
4640 orig_name = PHI_RESULT (exit_phi);
4641 FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, orig_name)
4643 stmt_vec_info use_stmt_vinfo;
4644 stmt_vec_info new_phi_vinfo;
4645 tree vect_phi_init, preheader_arg, vect_phi_res, init_def;
4646 basic_block bb = gimple_bb (use_stmt);
4647 gimple use;
4649 /* Check that USE_STMT is really double reduction phi
4650 node. */
4651 if (gimple_code (use_stmt) != GIMPLE_PHI
4652 || gimple_phi_num_args (use_stmt) != 2
4653 || bb->loop_father != outer_loop)
4654 continue;
4655 use_stmt_vinfo = vinfo_for_stmt (use_stmt);
4656 if (!use_stmt_vinfo
4657 || STMT_VINFO_DEF_TYPE (use_stmt_vinfo)
4658 != vect_double_reduction_def)
4659 continue;
4661 /* Create vector phi node for double reduction:
4662 vs1 = phi <vs0, vs2>
4663 vs1 was created previously in this function by a call to
4664 vect_get_vec_def_for_operand and is stored in
4665 vec_initial_def;
4666 vs2 is defined by INNER_PHI, the vectorized EXIT_PHI;
4667 vs0 is created here. */
4669 /* Create vector phi node. */
4670 vect_phi = create_phi_node (vec_initial_def, bb);
4671 new_phi_vinfo = new_stmt_vec_info (vect_phi,
4672 loop_vec_info_for_loop (outer_loop), NULL);
4673 set_vinfo_for_stmt (vect_phi, new_phi_vinfo);
4675 /* Create vs0 - initial def of the double reduction phi. */
4676 preheader_arg = PHI_ARG_DEF_FROM_EDGE (use_stmt,
4677 loop_preheader_edge (outer_loop));
4678 init_def = get_initial_def_for_reduction (stmt,
4679 preheader_arg, NULL);
4680 vect_phi_init = vect_init_vector (use_stmt, init_def,
4681 vectype, NULL);
4683 /* Update phi node arguments with vs0 and vs2. */
4684 add_phi_arg (vect_phi, vect_phi_init,
4685 loop_preheader_edge (outer_loop),
4686 UNKNOWN_LOCATION);
4687 add_phi_arg (vect_phi, PHI_RESULT (inner_phi),
4688 loop_latch_edge (outer_loop), UNKNOWN_LOCATION);
4689 if (dump_enabled_p ())
4691 dump_printf_loc (MSG_NOTE, vect_location,
4692 "created double reduction phi node: ");
4693 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, vect_phi, 0);
4694 dump_printf (MSG_NOTE, "\n");
4697 vect_phi_res = PHI_RESULT (vect_phi);
4699 /* Replace the use, i.e., set the correct vs1 in the regular
4700 reduction phi node. FORNOW, NCOPIES is always 1, so the
4701 loop is redundant. */
4702 use = reduction_phi;
4703 for (j = 0; j < ncopies; j++)
4705 edge pr_edge = loop_preheader_edge (loop);
4706 SET_PHI_ARG_DEF (use, pr_edge->dest_idx, vect_phi_res);
4707 use = STMT_VINFO_RELATED_STMT (vinfo_for_stmt (use));
4713 phis.release ();
4714 if (nested_in_vect_loop)
4716 if (double_reduc)
4717 loop = outer_loop;
4718 else
4719 continue;
4722 phis.create (3);
4723 /* Find the loop-closed-use at the loop exit of the original scalar
4724 result. (The reduction result is expected to have two immediate uses,
4725 one at the latch block, and one at the loop exit). For double
4726 reductions we are looking for exit phis of the outer loop. */
4727 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, scalar_dest)
4729 if (!flow_bb_inside_loop_p (loop, gimple_bb (USE_STMT (use_p))))
4731 if (!is_gimple_debug (USE_STMT (use_p)))
4732 phis.safe_push (USE_STMT (use_p));
4734 else
4736 if (double_reduc && gimple_code (USE_STMT (use_p)) == GIMPLE_PHI)
4738 tree phi_res = PHI_RESULT (USE_STMT (use_p));
4740 FOR_EACH_IMM_USE_FAST (phi_use_p, phi_imm_iter, phi_res)
4742 if (!flow_bb_inside_loop_p (loop,
4743 gimple_bb (USE_STMT (phi_use_p)))
4744 && !is_gimple_debug (USE_STMT (phi_use_p)))
4745 phis.safe_push (USE_STMT (phi_use_p));
4751 FOR_EACH_VEC_ELT (phis, i, exit_phi)
4753 /* Replace the uses: */
4754 orig_name = PHI_RESULT (exit_phi);
4755 scalar_result = scalar_results[k];
4756 FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, orig_name)
4757 FOR_EACH_IMM_USE_ON_STMT (use_p, imm_iter)
4758 SET_USE (use_p, scalar_result);
4761 phis.release ();
4766 /* Function vectorizable_reduction.
4768 Check if STMT performs a reduction operation that can be vectorized.
4769 If VEC_STMT is also passed, vectorize the STMT: create a vectorized
4770 stmt to replace it, put it in VEC_STMT, and insert it at GSI.
4771 Return FALSE if not a vectorizable STMT, TRUE otherwise.
4773 This function also handles reduction idioms (patterns) that have been
4774 recognized in advance during vect_pattern_recog. In this case, STMT may be
4775 of this form:
4776 X = pattern_expr (arg0, arg1, ..., X)
4777 and it's STMT_VINFO_RELATED_STMT points to the last stmt in the original
4778 sequence that had been detected and replaced by the pattern-stmt (STMT).
4780 In some cases of reduction patterns, the type of the reduction variable X is
4781 different than the type of the other arguments of STMT.
4782 In such cases, the vectype that is used when transforming STMT into a vector
4783 stmt is different than the vectype that is used to determine the
4784 vectorization factor, because it consists of a different number of elements
4785 than the actual number of elements that are being operated upon in parallel.
4787 For example, consider an accumulation of shorts into an int accumulator.
4788 On some targets it's possible to vectorize this pattern operating on 8
4789 shorts at a time (hence, the vectype for purposes of determining the
4790 vectorization factor should be V8HI); on the other hand, the vectype that
4791 is used to create the vector form is actually V4SI (the type of the result).
4793 Upon entry to this function, STMT_VINFO_VECTYPE records the vectype that
4794 indicates what is the actual level of parallelism (V8HI in the example), so
4795 that the right vectorization factor would be derived. This vectype
4796 corresponds to the type of arguments to the reduction stmt, and should *NOT*
4797 be used to create the vectorized stmt. The right vectype for the vectorized
4798 stmt is obtained from the type of the result X:
4799 get_vectype_for_scalar_type (TREE_TYPE (X))
4801 This means that, contrary to "regular" reductions (or "regular" stmts in
4802 general), the following equation:
4803 STMT_VINFO_VECTYPE == get_vectype_for_scalar_type (TREE_TYPE (X))
4804 does *NOT* necessarily hold for reduction patterns. */
4806 bool
4807 vectorizable_reduction (gimple stmt, gimple_stmt_iterator *gsi,
4808 gimple *vec_stmt, slp_tree slp_node)
4810 tree vec_dest;
4811 tree scalar_dest;
4812 tree loop_vec_def0 = NULL_TREE, loop_vec_def1 = NULL_TREE;
4813 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
4814 tree vectype_out = STMT_VINFO_VECTYPE (stmt_info);
4815 tree vectype_in = NULL_TREE;
4816 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
4817 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
4818 enum tree_code code, orig_code, epilog_reduc_code;
4819 machine_mode vec_mode;
4820 int op_type;
4821 optab optab, reduc_optab;
4822 tree new_temp = NULL_TREE;
4823 tree def;
4824 gimple def_stmt;
4825 enum vect_def_type dt;
4826 gphi *new_phi = NULL;
4827 tree scalar_type;
4828 bool is_simple_use;
4829 gimple orig_stmt;
4830 stmt_vec_info orig_stmt_info;
4831 tree expr = NULL_TREE;
4832 int i;
4833 int ncopies;
4834 int epilog_copies;
4835 stmt_vec_info prev_stmt_info, prev_phi_info;
4836 bool single_defuse_cycle = false;
4837 tree reduc_def = NULL_TREE;
4838 gimple new_stmt = NULL;
4839 int j;
4840 tree ops[3];
4841 bool nested_cycle = false, found_nested_cycle_def = false;
4842 gimple reduc_def_stmt = NULL;
4843 bool double_reduc = false, dummy;
4844 basic_block def_bb;
4845 struct loop * def_stmt_loop, *outer_loop = NULL;
4846 tree def_arg;
4847 gimple def_arg_stmt;
4848 auto_vec<tree> vec_oprnds0;
4849 auto_vec<tree> vec_oprnds1;
4850 auto_vec<tree> vect_defs;
4851 auto_vec<gimple> phis;
4852 int vec_num;
4853 tree def0, def1, tem, op0, op1 = NULL_TREE;
4855 /* In case of reduction chain we switch to the first stmt in the chain, but
4856 we don't update STMT_INFO, since only the last stmt is marked as reduction
4857 and has reduction properties. */
4858 if (GROUP_FIRST_ELEMENT (vinfo_for_stmt (stmt)))
4859 stmt = GROUP_FIRST_ELEMENT (stmt_info);
4861 if (nested_in_vect_loop_p (loop, stmt))
4863 outer_loop = loop;
4864 loop = loop->inner;
4865 nested_cycle = true;
4868 /* 1. Is vectorizable reduction? */
4869 /* Not supportable if the reduction variable is used in the loop, unless
4870 it's a reduction chain. */
4871 if (STMT_VINFO_RELEVANT (stmt_info) > vect_used_in_outer
4872 && !GROUP_FIRST_ELEMENT (stmt_info))
4873 return false;
4875 /* Reductions that are not used even in an enclosing outer-loop,
4876 are expected to be "live" (used out of the loop). */
4877 if (STMT_VINFO_RELEVANT (stmt_info) == vect_unused_in_scope
4878 && !STMT_VINFO_LIVE_P (stmt_info))
4879 return false;
4881 /* Make sure it was already recognized as a reduction computation. */
4882 if (STMT_VINFO_DEF_TYPE (stmt_info) != vect_reduction_def
4883 && STMT_VINFO_DEF_TYPE (stmt_info) != vect_nested_cycle)
4884 return false;
4886 /* 2. Has this been recognized as a reduction pattern?
4888 Check if STMT represents a pattern that has been recognized
4889 in earlier analysis stages. For stmts that represent a pattern,
4890 the STMT_VINFO_RELATED_STMT field records the last stmt in
4891 the original sequence that constitutes the pattern. */
4893 orig_stmt = STMT_VINFO_RELATED_STMT (stmt_info);
4894 if (orig_stmt)
4896 orig_stmt_info = vinfo_for_stmt (orig_stmt);
4897 gcc_assert (STMT_VINFO_IN_PATTERN_P (orig_stmt_info));
4898 gcc_assert (!STMT_VINFO_IN_PATTERN_P (stmt_info));
4901 /* 3. Check the operands of the operation. The first operands are defined
4902 inside the loop body. The last operand is the reduction variable,
4903 which is defined by the loop-header-phi. */
4905 gcc_assert (is_gimple_assign (stmt));
4907 /* Flatten RHS. */
4908 switch (get_gimple_rhs_class (gimple_assign_rhs_code (stmt)))
4910 case GIMPLE_SINGLE_RHS:
4911 op_type = TREE_OPERAND_LENGTH (gimple_assign_rhs1 (stmt));
4912 if (op_type == ternary_op)
4914 tree rhs = gimple_assign_rhs1 (stmt);
4915 ops[0] = TREE_OPERAND (rhs, 0);
4916 ops[1] = TREE_OPERAND (rhs, 1);
4917 ops[2] = TREE_OPERAND (rhs, 2);
4918 code = TREE_CODE (rhs);
4920 else
4921 return false;
4922 break;
4924 case GIMPLE_BINARY_RHS:
4925 code = gimple_assign_rhs_code (stmt);
4926 op_type = TREE_CODE_LENGTH (code);
4927 gcc_assert (op_type == binary_op);
4928 ops[0] = gimple_assign_rhs1 (stmt);
4929 ops[1] = gimple_assign_rhs2 (stmt);
4930 break;
4932 case GIMPLE_TERNARY_RHS:
4933 code = gimple_assign_rhs_code (stmt);
4934 op_type = TREE_CODE_LENGTH (code);
4935 gcc_assert (op_type == ternary_op);
4936 ops[0] = gimple_assign_rhs1 (stmt);
4937 ops[1] = gimple_assign_rhs2 (stmt);
4938 ops[2] = gimple_assign_rhs3 (stmt);
4939 break;
4941 case GIMPLE_UNARY_RHS:
4942 return false;
4944 default:
4945 gcc_unreachable ();
4947 /* The default is that the reduction variable is the last in statement. */
4948 int reduc_index = op_type - 1;
4950 if (code == COND_EXPR && slp_node)
4951 return false;
4953 scalar_dest = gimple_assign_lhs (stmt);
4954 scalar_type = TREE_TYPE (scalar_dest);
4955 if (!POINTER_TYPE_P (scalar_type) && !INTEGRAL_TYPE_P (scalar_type)
4956 && !SCALAR_FLOAT_TYPE_P (scalar_type))
4957 return false;
4959 /* Do not try to vectorize bit-precision reductions. */
4960 if ((TYPE_PRECISION (scalar_type)
4961 != GET_MODE_PRECISION (TYPE_MODE (scalar_type))))
4962 return false;
4964 /* All uses but the last are expected to be defined in the loop.
4965 The last use is the reduction variable. In case of nested cycle this
4966 assumption is not true: we use reduc_index to record the index of the
4967 reduction variable. */
4968 for (i = 0; i < op_type - 1; i++)
4970 /* The condition of COND_EXPR is checked in vectorizable_condition(). */
4971 if (i == 0 && code == COND_EXPR)
4972 continue;
4974 is_simple_use = vect_is_simple_use_1 (ops[i], stmt, loop_vinfo, NULL,
4975 &def_stmt, &def, &dt, &tem);
4976 if (!vectype_in)
4977 vectype_in = tem;
4978 gcc_assert (is_simple_use);
4980 if (dt != vect_internal_def
4981 && dt != vect_external_def
4982 && dt != vect_constant_def
4983 && dt != vect_induction_def
4984 && !(dt == vect_nested_cycle && nested_cycle))
4985 return false;
4987 if (dt == vect_nested_cycle)
4989 found_nested_cycle_def = true;
4990 reduc_def_stmt = def_stmt;
4991 reduc_index = i;
4995 is_simple_use = vect_is_simple_use_1 (ops[i], stmt, loop_vinfo, NULL,
4996 &def_stmt, &def, &dt, &tem);
4997 if (!vectype_in)
4998 vectype_in = tem;
4999 gcc_assert (is_simple_use);
5000 if (!found_nested_cycle_def)
5001 reduc_def_stmt = def_stmt;
5003 if (reduc_def_stmt && gimple_code (reduc_def_stmt) != GIMPLE_PHI)
5004 return false;
5006 if (!(dt == vect_reduction_def
5007 || dt == vect_nested_cycle
5008 || ((dt == vect_internal_def || dt == vect_external_def
5009 || dt == vect_constant_def || dt == vect_induction_def)
5010 && nested_cycle && found_nested_cycle_def)))
5012 /* For pattern recognized stmts, orig_stmt might be a reduction,
5013 but some helper statements for the pattern might not, or
5014 might be COND_EXPRs with reduction uses in the condition. */
5015 gcc_assert (orig_stmt);
5016 return false;
5019 if (orig_stmt)
5020 gcc_assert (orig_stmt == vect_is_simple_reduction (loop_vinfo,
5021 reduc_def_stmt,
5022 !nested_cycle,
5023 &dummy));
5024 else
5026 gimple tmp = vect_is_simple_reduction (loop_vinfo, reduc_def_stmt,
5027 !nested_cycle, &dummy);
5028 /* We changed STMT to be the first stmt in reduction chain, hence we
5029 check that in this case the first element in the chain is STMT. */
5030 gcc_assert (stmt == tmp
5031 || GROUP_FIRST_ELEMENT (vinfo_for_stmt (tmp)) == stmt);
5034 if (STMT_VINFO_LIVE_P (vinfo_for_stmt (reduc_def_stmt)))
5035 return false;
5037 if (slp_node || PURE_SLP_STMT (stmt_info))
5038 ncopies = 1;
5039 else
5040 ncopies = (LOOP_VINFO_VECT_FACTOR (loop_vinfo)
5041 / TYPE_VECTOR_SUBPARTS (vectype_in));
5043 gcc_assert (ncopies >= 1);
5045 vec_mode = TYPE_MODE (vectype_in);
5047 if (code == COND_EXPR)
5049 if (!vectorizable_condition (stmt, gsi, NULL, ops[reduc_index], 0, NULL))
5051 if (dump_enabled_p ())
5052 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5053 "unsupported condition in reduction\n");
5055 return false;
5058 else
5060 /* 4. Supportable by target? */
5062 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR
5063 || code == LROTATE_EXPR || code == RROTATE_EXPR)
5065 /* Shifts and rotates are only supported by vectorizable_shifts,
5066 not vectorizable_reduction. */
5067 if (dump_enabled_p ())
5068 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5069 "unsupported shift or rotation.\n");
5070 return false;
5073 /* 4.1. check support for the operation in the loop */
5074 optab = optab_for_tree_code (code, vectype_in, optab_default);
5075 if (!optab)
5077 if (dump_enabled_p ())
5078 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5079 "no optab.\n");
5081 return false;
5084 if (optab_handler (optab, vec_mode) == CODE_FOR_nothing)
5086 if (dump_enabled_p ())
5087 dump_printf (MSG_NOTE, "op not supported by target.\n");
5089 if (GET_MODE_SIZE (vec_mode) != UNITS_PER_WORD
5090 || LOOP_VINFO_VECT_FACTOR (loop_vinfo)
5091 < vect_min_worthwhile_factor (code))
5092 return false;
5094 if (dump_enabled_p ())
5095 dump_printf (MSG_NOTE, "proceeding using word mode.\n");
5098 /* Worthwhile without SIMD support? */
5099 if (!VECTOR_MODE_P (TYPE_MODE (vectype_in))
5100 && LOOP_VINFO_VECT_FACTOR (loop_vinfo)
5101 < vect_min_worthwhile_factor (code))
5103 if (dump_enabled_p ())
5104 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5105 "not worthwhile without SIMD support.\n");
5107 return false;
5111 /* 4.2. Check support for the epilog operation.
5113 If STMT represents a reduction pattern, then the type of the
5114 reduction variable may be different than the type of the rest
5115 of the arguments. For example, consider the case of accumulation
5116 of shorts into an int accumulator; The original code:
5117 S1: int_a = (int) short_a;
5118 orig_stmt-> S2: int_acc = plus <int_a ,int_acc>;
5120 was replaced with:
5121 STMT: int_acc = widen_sum <short_a, int_acc>
5123 This means that:
5124 1. The tree-code that is used to create the vector operation in the
5125 epilog code (that reduces the partial results) is not the
5126 tree-code of STMT, but is rather the tree-code of the original
5127 stmt from the pattern that STMT is replacing. I.e, in the example
5128 above we want to use 'widen_sum' in the loop, but 'plus' in the
5129 epilog.
5130 2. The type (mode) we use to check available target support
5131 for the vector operation to be created in the *epilog*, is
5132 determined by the type of the reduction variable (in the example
5133 above we'd check this: optab_handler (plus_optab, vect_int_mode])).
5134 However the type (mode) we use to check available target support
5135 for the vector operation to be created *inside the loop*, is
5136 determined by the type of the other arguments to STMT (in the
5137 example we'd check this: optab_handler (widen_sum_optab,
5138 vect_short_mode)).
5140 This is contrary to "regular" reductions, in which the types of all
5141 the arguments are the same as the type of the reduction variable.
5142 For "regular" reductions we can therefore use the same vector type
5143 (and also the same tree-code) when generating the epilog code and
5144 when generating the code inside the loop. */
5146 if (orig_stmt)
5148 /* This is a reduction pattern: get the vectype from the type of the
5149 reduction variable, and get the tree-code from orig_stmt. */
5150 orig_code = gimple_assign_rhs_code (orig_stmt);
5151 gcc_assert (vectype_out);
5152 vec_mode = TYPE_MODE (vectype_out);
5154 else
5156 /* Regular reduction: use the same vectype and tree-code as used for
5157 the vector code inside the loop can be used for the epilog code. */
5158 orig_code = code;
5161 if (nested_cycle)
5163 def_bb = gimple_bb (reduc_def_stmt);
5164 def_stmt_loop = def_bb->loop_father;
5165 def_arg = PHI_ARG_DEF_FROM_EDGE (reduc_def_stmt,
5166 loop_preheader_edge (def_stmt_loop));
5167 if (TREE_CODE (def_arg) == SSA_NAME
5168 && (def_arg_stmt = SSA_NAME_DEF_STMT (def_arg))
5169 && gimple_code (def_arg_stmt) == GIMPLE_PHI
5170 && flow_bb_inside_loop_p (outer_loop, gimple_bb (def_arg_stmt))
5171 && vinfo_for_stmt (def_arg_stmt)
5172 && STMT_VINFO_DEF_TYPE (vinfo_for_stmt (def_arg_stmt))
5173 == vect_double_reduction_def)
5174 double_reduc = true;
5177 epilog_reduc_code = ERROR_MARK;
5178 if (reduction_code_for_scalar_code (orig_code, &epilog_reduc_code))
5180 reduc_optab = optab_for_tree_code (epilog_reduc_code, vectype_out,
5181 optab_default);
5182 if (!reduc_optab)
5184 if (dump_enabled_p ())
5185 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5186 "no optab for reduction.\n");
5188 epilog_reduc_code = ERROR_MARK;
5190 else if (optab_handler (reduc_optab, vec_mode) == CODE_FOR_nothing)
5192 optab = scalar_reduc_to_vector (reduc_optab, vectype_out);
5193 if (optab_handler (optab, vec_mode) == CODE_FOR_nothing)
5195 if (dump_enabled_p ())
5196 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5197 "reduc op not supported by target.\n");
5199 epilog_reduc_code = ERROR_MARK;
5203 else
5205 if (!nested_cycle || double_reduc)
5207 if (dump_enabled_p ())
5208 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5209 "no reduc code for scalar code.\n");
5211 return false;
5215 if (double_reduc && ncopies > 1)
5217 if (dump_enabled_p ())
5218 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5219 "multiple types in double reduction\n");
5221 return false;
5224 /* In case of widenning multiplication by a constant, we update the type
5225 of the constant to be the type of the other operand. We check that the
5226 constant fits the type in the pattern recognition pass. */
5227 if (code == DOT_PROD_EXPR
5228 && !types_compatible_p (TREE_TYPE (ops[0]), TREE_TYPE (ops[1])))
5230 if (TREE_CODE (ops[0]) == INTEGER_CST)
5231 ops[0] = fold_convert (TREE_TYPE (ops[1]), ops[0]);
5232 else if (TREE_CODE (ops[1]) == INTEGER_CST)
5233 ops[1] = fold_convert (TREE_TYPE (ops[0]), ops[1]);
5234 else
5236 if (dump_enabled_p ())
5237 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5238 "invalid types in dot-prod\n");
5240 return false;
5244 if (!vec_stmt) /* transformation not required. */
5246 if (!vect_model_reduction_cost (stmt_info, epilog_reduc_code, ncopies,
5247 reduc_index))
5248 return false;
5249 STMT_VINFO_TYPE (stmt_info) = reduc_vec_info_type;
5250 return true;
5253 /** Transform. **/
5255 if (dump_enabled_p ())
5256 dump_printf_loc (MSG_NOTE, vect_location, "transform reduction.\n");
5258 /* FORNOW: Multiple types are not supported for condition. */
5259 if (code == COND_EXPR)
5260 gcc_assert (ncopies == 1);
5262 /* Create the destination vector */
5263 vec_dest = vect_create_destination_var (scalar_dest, vectype_out);
5265 /* In case the vectorization factor (VF) is bigger than the number
5266 of elements that we can fit in a vectype (nunits), we have to generate
5267 more than one vector stmt - i.e - we need to "unroll" the
5268 vector stmt by a factor VF/nunits. For more details see documentation
5269 in vectorizable_operation. */
5271 /* If the reduction is used in an outer loop we need to generate
5272 VF intermediate results, like so (e.g. for ncopies=2):
5273 r0 = phi (init, r0)
5274 r1 = phi (init, r1)
5275 r0 = x0 + r0;
5276 r1 = x1 + r1;
5277 (i.e. we generate VF results in 2 registers).
5278 In this case we have a separate def-use cycle for each copy, and therefore
5279 for each copy we get the vector def for the reduction variable from the
5280 respective phi node created for this copy.
5282 Otherwise (the reduction is unused in the loop nest), we can combine
5283 together intermediate results, like so (e.g. for ncopies=2):
5284 r = phi (init, r)
5285 r = x0 + r;
5286 r = x1 + r;
5287 (i.e. we generate VF/2 results in a single register).
5288 In this case for each copy we get the vector def for the reduction variable
5289 from the vectorized reduction operation generated in the previous iteration.
5292 if (STMT_VINFO_RELEVANT (stmt_info) == vect_unused_in_scope)
5294 single_defuse_cycle = true;
5295 epilog_copies = 1;
5297 else
5298 epilog_copies = ncopies;
5300 prev_stmt_info = NULL;
5301 prev_phi_info = NULL;
5302 if (slp_node)
5304 vec_num = SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node);
5305 gcc_assert (TYPE_VECTOR_SUBPARTS (vectype_out)
5306 == TYPE_VECTOR_SUBPARTS (vectype_in));
5308 else
5310 vec_num = 1;
5311 vec_oprnds0.create (1);
5312 if (op_type == ternary_op)
5313 vec_oprnds1.create (1);
5316 phis.create (vec_num);
5317 vect_defs.create (vec_num);
5318 if (!slp_node)
5319 vect_defs.quick_push (NULL_TREE);
5321 for (j = 0; j < ncopies; j++)
5323 if (j == 0 || !single_defuse_cycle)
5325 for (i = 0; i < vec_num; i++)
5327 /* Create the reduction-phi that defines the reduction
5328 operand. */
5329 new_phi = create_phi_node (vec_dest, loop->header);
5330 set_vinfo_for_stmt (new_phi,
5331 new_stmt_vec_info (new_phi, loop_vinfo,
5332 NULL));
5333 if (j == 0 || slp_node)
5334 phis.quick_push (new_phi);
5338 if (code == COND_EXPR)
5340 gcc_assert (!slp_node);
5341 vectorizable_condition (stmt, gsi, vec_stmt,
5342 PHI_RESULT (phis[0]),
5343 reduc_index, NULL);
5344 /* Multiple types are not supported for condition. */
5345 break;
5348 /* Handle uses. */
5349 if (j == 0)
5351 op0 = ops[!reduc_index];
5352 if (op_type == ternary_op)
5354 if (reduc_index == 0)
5355 op1 = ops[2];
5356 else
5357 op1 = ops[1];
5360 if (slp_node)
5361 vect_get_vec_defs (op0, op1, stmt, &vec_oprnds0, &vec_oprnds1,
5362 slp_node, -1);
5363 else
5365 loop_vec_def0 = vect_get_vec_def_for_operand (ops[!reduc_index],
5366 stmt, NULL);
5367 vec_oprnds0.quick_push (loop_vec_def0);
5368 if (op_type == ternary_op)
5370 loop_vec_def1 = vect_get_vec_def_for_operand (op1, stmt,
5371 NULL);
5372 vec_oprnds1.quick_push (loop_vec_def1);
5376 else
5378 if (!slp_node)
5380 enum vect_def_type dt;
5381 gimple dummy_stmt;
5382 tree dummy;
5384 vect_is_simple_use (ops[!reduc_index], stmt, loop_vinfo, NULL,
5385 &dummy_stmt, &dummy, &dt);
5386 loop_vec_def0 = vect_get_vec_def_for_stmt_copy (dt,
5387 loop_vec_def0);
5388 vec_oprnds0[0] = loop_vec_def0;
5389 if (op_type == ternary_op)
5391 vect_is_simple_use (op1, stmt, loop_vinfo, NULL, &dummy_stmt,
5392 &dummy, &dt);
5393 loop_vec_def1 = vect_get_vec_def_for_stmt_copy (dt,
5394 loop_vec_def1);
5395 vec_oprnds1[0] = loop_vec_def1;
5399 if (single_defuse_cycle)
5400 reduc_def = gimple_assign_lhs (new_stmt);
5402 STMT_VINFO_RELATED_STMT (prev_phi_info) = new_phi;
5405 FOR_EACH_VEC_ELT (vec_oprnds0, i, def0)
5407 if (slp_node)
5408 reduc_def = PHI_RESULT (phis[i]);
5409 else
5411 if (!single_defuse_cycle || j == 0)
5412 reduc_def = PHI_RESULT (new_phi);
5415 def1 = ((op_type == ternary_op)
5416 ? vec_oprnds1[i] : NULL);
5417 if (op_type == binary_op)
5419 if (reduc_index == 0)
5420 expr = build2 (code, vectype_out, reduc_def, def0);
5421 else
5422 expr = build2 (code, vectype_out, def0, reduc_def);
5424 else
5426 if (reduc_index == 0)
5427 expr = build3 (code, vectype_out, reduc_def, def0, def1);
5428 else
5430 if (reduc_index == 1)
5431 expr = build3 (code, vectype_out, def0, reduc_def, def1);
5432 else
5433 expr = build3 (code, vectype_out, def0, def1, reduc_def);
5437 new_stmt = gimple_build_assign (vec_dest, expr);
5438 new_temp = make_ssa_name (vec_dest, new_stmt);
5439 gimple_assign_set_lhs (new_stmt, new_temp);
5440 vect_finish_stmt_generation (stmt, new_stmt, gsi);
5442 if (slp_node)
5444 SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt);
5445 vect_defs.quick_push (new_temp);
5447 else
5448 vect_defs[0] = new_temp;
5451 if (slp_node)
5452 continue;
5454 if (j == 0)
5455 STMT_VINFO_VEC_STMT (stmt_info) = *vec_stmt = new_stmt;
5456 else
5457 STMT_VINFO_RELATED_STMT (prev_stmt_info) = new_stmt;
5459 prev_stmt_info = vinfo_for_stmt (new_stmt);
5460 prev_phi_info = vinfo_for_stmt (new_phi);
5463 /* Finalize the reduction-phi (set its arguments) and create the
5464 epilog reduction code. */
5465 if ((!single_defuse_cycle || code == COND_EXPR) && !slp_node)
5467 new_temp = gimple_assign_lhs (*vec_stmt);
5468 vect_defs[0] = new_temp;
5471 vect_create_epilog_for_reduction (vect_defs, stmt, epilog_copies,
5472 epilog_reduc_code, phis, reduc_index,
5473 double_reduc, slp_node);
5475 return true;
5478 /* Function vect_min_worthwhile_factor.
5480 For a loop where we could vectorize the operation indicated by CODE,
5481 return the minimum vectorization factor that makes it worthwhile
5482 to use generic vectors. */
5484 vect_min_worthwhile_factor (enum tree_code code)
5486 switch (code)
5488 case PLUS_EXPR:
5489 case MINUS_EXPR:
5490 case NEGATE_EXPR:
5491 return 4;
5493 case BIT_AND_EXPR:
5494 case BIT_IOR_EXPR:
5495 case BIT_XOR_EXPR:
5496 case BIT_NOT_EXPR:
5497 return 2;
5499 default:
5500 return INT_MAX;
5505 /* Function vectorizable_induction
5507 Check if PHI performs an induction computation that can be vectorized.
5508 If VEC_STMT is also passed, vectorize the induction PHI: create a vectorized
5509 phi to replace it, put it in VEC_STMT, and add it to the same basic block.
5510 Return FALSE if not a vectorizable STMT, TRUE otherwise. */
5512 bool
5513 vectorizable_induction (gimple phi, gimple_stmt_iterator *gsi ATTRIBUTE_UNUSED,
5514 gimple *vec_stmt)
5516 stmt_vec_info stmt_info = vinfo_for_stmt (phi);
5517 tree vectype = STMT_VINFO_VECTYPE (stmt_info);
5518 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
5519 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
5520 int nunits = TYPE_VECTOR_SUBPARTS (vectype);
5521 int ncopies = LOOP_VINFO_VECT_FACTOR (loop_vinfo) / nunits;
5522 tree vec_def;
5524 gcc_assert (ncopies >= 1);
5525 /* FORNOW. These restrictions should be relaxed. */
5526 if (nested_in_vect_loop_p (loop, phi))
5528 imm_use_iterator imm_iter;
5529 use_operand_p use_p;
5530 gimple exit_phi;
5531 edge latch_e;
5532 tree loop_arg;
5534 if (ncopies > 1)
5536 if (dump_enabled_p ())
5537 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5538 "multiple types in nested loop.\n");
5539 return false;
5542 exit_phi = NULL;
5543 latch_e = loop_latch_edge (loop->inner);
5544 loop_arg = PHI_ARG_DEF_FROM_EDGE (phi, latch_e);
5545 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, loop_arg)
5547 gimple use_stmt = USE_STMT (use_p);
5548 if (is_gimple_debug (use_stmt))
5549 continue;
5551 if (!flow_bb_inside_loop_p (loop->inner, gimple_bb (use_stmt)))
5553 exit_phi = use_stmt;
5554 break;
5557 if (exit_phi)
5559 stmt_vec_info exit_phi_vinfo = vinfo_for_stmt (exit_phi);
5560 if (!(STMT_VINFO_RELEVANT_P (exit_phi_vinfo)
5561 && !STMT_VINFO_LIVE_P (exit_phi_vinfo)))
5563 if (dump_enabled_p ())
5564 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5565 "inner-loop induction only used outside "
5566 "of the outer vectorized loop.\n");
5567 return false;
5572 if (!STMT_VINFO_RELEVANT_P (stmt_info))
5573 return false;
5575 /* FORNOW: SLP not supported. */
5576 if (STMT_SLP_TYPE (stmt_info))
5577 return false;
5579 gcc_assert (STMT_VINFO_DEF_TYPE (stmt_info) == vect_induction_def);
5581 if (gimple_code (phi) != GIMPLE_PHI)
5582 return false;
5584 if (!vec_stmt) /* transformation not required. */
5586 STMT_VINFO_TYPE (stmt_info) = induc_vec_info_type;
5587 if (dump_enabled_p ())
5588 dump_printf_loc (MSG_NOTE, vect_location,
5589 "=== vectorizable_induction ===\n");
5590 vect_model_induction_cost (stmt_info, ncopies);
5591 return true;
5594 /** Transform. **/
5596 if (dump_enabled_p ())
5597 dump_printf_loc (MSG_NOTE, vect_location, "transform induction phi.\n");
5599 vec_def = get_initial_def_for_induction (phi);
5600 *vec_stmt = SSA_NAME_DEF_STMT (vec_def);
5601 return true;
5604 /* Function vectorizable_live_operation.
5606 STMT computes a value that is used outside the loop. Check if
5607 it can be supported. */
5609 bool
5610 vectorizable_live_operation (gimple stmt,
5611 gimple_stmt_iterator *gsi ATTRIBUTE_UNUSED,
5612 gimple *vec_stmt)
5614 stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
5615 loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
5616 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
5617 int i;
5618 int op_type;
5619 tree op;
5620 tree def;
5621 gimple def_stmt;
5622 enum vect_def_type dt;
5623 enum tree_code code;
5624 enum gimple_rhs_class rhs_class;
5626 gcc_assert (STMT_VINFO_LIVE_P (stmt_info));
5628 if (STMT_VINFO_DEF_TYPE (stmt_info) == vect_reduction_def)
5629 return false;
5631 if (!is_gimple_assign (stmt))
5633 if (gimple_call_internal_p (stmt)
5634 && gimple_call_internal_fn (stmt) == IFN_GOMP_SIMD_LANE
5635 && gimple_call_lhs (stmt)
5636 && loop->simduid
5637 && TREE_CODE (gimple_call_arg (stmt, 0)) == SSA_NAME
5638 && loop->simduid
5639 == SSA_NAME_VAR (gimple_call_arg (stmt, 0)))
5641 edge e = single_exit (loop);
5642 basic_block merge_bb = e->dest;
5643 imm_use_iterator imm_iter;
5644 use_operand_p use_p;
5645 tree lhs = gimple_call_lhs (stmt);
5647 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, lhs)
5649 gimple use_stmt = USE_STMT (use_p);
5650 if (gimple_code (use_stmt) == GIMPLE_PHI
5651 && gimple_bb (use_stmt) == merge_bb)
5653 if (vec_stmt)
5655 tree vfm1
5656 = build_int_cst (unsigned_type_node,
5657 loop_vinfo->vectorization_factor - 1);
5658 SET_PHI_ARG_DEF (use_stmt, e->dest_idx, vfm1);
5660 return true;
5665 return false;
5668 if (TREE_CODE (gimple_assign_lhs (stmt)) != SSA_NAME)
5669 return false;
5671 /* FORNOW. CHECKME. */
5672 if (nested_in_vect_loop_p (loop, stmt))
5673 return false;
5675 code = gimple_assign_rhs_code (stmt);
5676 op_type = TREE_CODE_LENGTH (code);
5677 rhs_class = get_gimple_rhs_class (code);
5678 gcc_assert (rhs_class != GIMPLE_UNARY_RHS || op_type == unary_op);
5679 gcc_assert (rhs_class != GIMPLE_BINARY_RHS || op_type == binary_op);
5681 /* FORNOW: support only if all uses are invariant. This means
5682 that the scalar operations can remain in place, unvectorized.
5683 The original last scalar value that they compute will be used. */
5685 for (i = 0; i < op_type; i++)
5687 if (rhs_class == GIMPLE_SINGLE_RHS)
5688 op = TREE_OPERAND (gimple_op (stmt, 1), i);
5689 else
5690 op = gimple_op (stmt, i + 1);
5691 if (op
5692 && !vect_is_simple_use (op, stmt, loop_vinfo, NULL, &def_stmt, &def,
5693 &dt))
5695 if (dump_enabled_p ())
5696 dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
5697 "use not simple.\n");
5698 return false;
5701 if (dt != vect_external_def && dt != vect_constant_def)
5702 return false;
5705 /* No transformation is required for the cases we currently support. */
5706 return true;
5709 /* Kill any debug uses outside LOOP of SSA names defined in STMT. */
5711 static void
5712 vect_loop_kill_debug_uses (struct loop *loop, gimple stmt)
5714 ssa_op_iter op_iter;
5715 imm_use_iterator imm_iter;
5716 def_operand_p def_p;
5717 gimple ustmt;
5719 FOR_EACH_PHI_OR_STMT_DEF (def_p, stmt, op_iter, SSA_OP_DEF)
5721 FOR_EACH_IMM_USE_STMT (ustmt, imm_iter, DEF_FROM_PTR (def_p))
5723 basic_block bb;
5725 if (!is_gimple_debug (ustmt))
5726 continue;
5728 bb = gimple_bb (ustmt);
5730 if (!flow_bb_inside_loop_p (loop, bb))
5732 if (gimple_debug_bind_p (ustmt))
5734 if (dump_enabled_p ())
5735 dump_printf_loc (MSG_NOTE, vect_location,
5736 "killing debug use\n");
5738 gimple_debug_bind_reset_value (ustmt);
5739 update_stmt (ustmt);
5741 else
5742 gcc_unreachable ();
5749 /* This function builds ni_name = number of iterations. Statements
5750 are emitted on the loop preheader edge. */
5752 static tree
5753 vect_build_loop_niters (loop_vec_info loop_vinfo)
5755 tree ni = unshare_expr (LOOP_VINFO_NITERS (loop_vinfo));
5756 if (TREE_CODE (ni) == INTEGER_CST)
5757 return ni;
5758 else
5760 tree ni_name, var;
5761 gimple_seq stmts = NULL;
5762 edge pe = loop_preheader_edge (LOOP_VINFO_LOOP (loop_vinfo));
5764 var = create_tmp_var (TREE_TYPE (ni), "niters");
5765 ni_name = force_gimple_operand (ni, &stmts, false, var);
5766 if (stmts)
5767 gsi_insert_seq_on_edge_immediate (pe, stmts);
5769 return ni_name;
5774 /* This function generates the following statements:
5776 ni_name = number of iterations loop executes
5777 ratio = ni_name / vf
5778 ratio_mult_vf_name = ratio * vf
5780 and places them on the loop preheader edge. */
5782 static void
5783 vect_generate_tmps_on_preheader (loop_vec_info loop_vinfo,
5784 tree ni_name,
5785 tree *ratio_mult_vf_name_ptr,
5786 tree *ratio_name_ptr)
5788 tree ni_minus_gap_name;
5789 tree var;
5790 tree ratio_name;
5791 tree ratio_mult_vf_name;
5792 int vf = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
5793 edge pe = loop_preheader_edge (LOOP_VINFO_LOOP (loop_vinfo));
5794 tree log_vf;
5796 log_vf = build_int_cst (TREE_TYPE (ni_name), exact_log2 (vf));
5798 /* If epilogue loop is required because of data accesses with gaps, we
5799 subtract one iteration from the total number of iterations here for
5800 correct calculation of RATIO. */
5801 if (LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo))
5803 ni_minus_gap_name = fold_build2 (MINUS_EXPR, TREE_TYPE (ni_name),
5804 ni_name,
5805 build_one_cst (TREE_TYPE (ni_name)));
5806 if (!is_gimple_val (ni_minus_gap_name))
5808 var = create_tmp_var (TREE_TYPE (ni_name), "ni_gap");
5809 gimple stmts = NULL;
5810 ni_minus_gap_name = force_gimple_operand (ni_minus_gap_name, &stmts,
5811 true, var);
5812 gsi_insert_seq_on_edge_immediate (pe, stmts);
5815 else
5816 ni_minus_gap_name = ni_name;
5818 /* Create: ratio = ni >> log2(vf) */
5819 /* ??? As we have ni == number of latch executions + 1, ni could
5820 have overflown to zero. So avoid computing ratio based on ni
5821 but compute it using the fact that we know ratio will be at least
5822 one, thus via (ni - vf) >> log2(vf) + 1. */
5823 ratio_name
5824 = fold_build2 (PLUS_EXPR, TREE_TYPE (ni_name),
5825 fold_build2 (RSHIFT_EXPR, TREE_TYPE (ni_name),
5826 fold_build2 (MINUS_EXPR, TREE_TYPE (ni_name),
5827 ni_minus_gap_name,
5828 build_int_cst
5829 (TREE_TYPE (ni_name), vf)),
5830 log_vf),
5831 build_int_cst (TREE_TYPE (ni_name), 1));
5832 if (!is_gimple_val (ratio_name))
5834 var = create_tmp_var (TREE_TYPE (ni_name), "bnd");
5835 gimple stmts = NULL;
5836 ratio_name = force_gimple_operand (ratio_name, &stmts, true, var);
5837 gsi_insert_seq_on_edge_immediate (pe, stmts);
5839 *ratio_name_ptr = ratio_name;
5841 /* Create: ratio_mult_vf = ratio << log2 (vf). */
5843 if (ratio_mult_vf_name_ptr)
5845 ratio_mult_vf_name = fold_build2 (LSHIFT_EXPR, TREE_TYPE (ratio_name),
5846 ratio_name, log_vf);
5847 if (!is_gimple_val (ratio_mult_vf_name))
5849 var = create_tmp_var (TREE_TYPE (ni_name), "ratio_mult_vf");
5850 gimple stmts = NULL;
5851 ratio_mult_vf_name = force_gimple_operand (ratio_mult_vf_name, &stmts,
5852 true, var);
5853 gsi_insert_seq_on_edge_immediate (pe, stmts);
5855 *ratio_mult_vf_name_ptr = ratio_mult_vf_name;
5858 return;
5862 /* Function vect_transform_loop.
5864 The analysis phase has determined that the loop is vectorizable.
5865 Vectorize the loop - created vectorized stmts to replace the scalar
5866 stmts in the loop, and update the loop exit condition. */
5868 void
5869 vect_transform_loop (loop_vec_info loop_vinfo)
5871 struct loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
5872 basic_block *bbs = LOOP_VINFO_BBS (loop_vinfo);
5873 int nbbs = loop->num_nodes;
5874 int i;
5875 tree ratio = NULL;
5876 int vectorization_factor = LOOP_VINFO_VECT_FACTOR (loop_vinfo);
5877 bool grouped_store;
5878 bool slp_scheduled = false;
5879 gimple stmt, pattern_stmt;
5880 gimple_seq pattern_def_seq = NULL;
5881 gimple_stmt_iterator pattern_def_si = gsi_none ();
5882 bool transform_pattern_stmt = false;
5883 bool check_profitability = false;
5884 int th;
5885 /* Record number of iterations before we started tampering with the profile. */
5886 gcov_type expected_iterations = expected_loop_iterations_unbounded (loop);
5888 if (dump_enabled_p ())
5889 dump_printf_loc (MSG_NOTE, vect_location, "=== vec_transform_loop ===\n");
5891 /* If profile is inprecise, we have chance to fix it up. */
5892 if (LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo))
5893 expected_iterations = LOOP_VINFO_INT_NITERS (loop_vinfo);
5895 /* Use the more conservative vectorization threshold. If the number
5896 of iterations is constant assume the cost check has been performed
5897 by our caller. If the threshold makes all loops profitable that
5898 run at least the vectorization factor number of times checking
5899 is pointless, too. */
5900 th = LOOP_VINFO_COST_MODEL_THRESHOLD (loop_vinfo);
5901 if (th >= LOOP_VINFO_VECT_FACTOR (loop_vinfo) - 1
5902 && !LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo))
5904 if (dump_enabled_p ())
5905 dump_printf_loc (MSG_NOTE, vect_location,
5906 "Profitability threshold is %d loop iterations.\n",
5907 th);
5908 check_profitability = true;
5911 /* Version the loop first, if required, so the profitability check
5912 comes first. */
5914 if (LOOP_REQUIRES_VERSIONING_FOR_ALIGNMENT (loop_vinfo)
5915 || LOOP_REQUIRES_VERSIONING_FOR_ALIAS (loop_vinfo))
5917 vect_loop_versioning (loop_vinfo, th, check_profitability);
5918 check_profitability = false;
5921 tree ni_name = vect_build_loop_niters (loop_vinfo);
5922 LOOP_VINFO_NITERS_UNCHANGED (loop_vinfo) = ni_name;
5924 /* Peel the loop if there are data refs with unknown alignment.
5925 Only one data ref with unknown store is allowed. */
5927 if (LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo))
5929 vect_do_peeling_for_alignment (loop_vinfo, ni_name,
5930 th, check_profitability);
5931 check_profitability = false;
5932 /* The above adjusts LOOP_VINFO_NITERS, so cause ni_name to
5933 be re-computed. */
5934 ni_name = NULL_TREE;
5937 /* If the loop has a symbolic number of iterations 'n' (i.e. it's not a
5938 compile time constant), or it is a constant that doesn't divide by the
5939 vectorization factor, then an epilog loop needs to be created.
5940 We therefore duplicate the loop: the original loop will be vectorized,
5941 and will compute the first (n/VF) iterations. The second copy of the loop
5942 will remain scalar and will compute the remaining (n%VF) iterations.
5943 (VF is the vectorization factor). */
5945 if (LOOP_VINFO_PEELING_FOR_NITER (loop_vinfo)
5946 || LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo))
5948 tree ratio_mult_vf;
5949 if (!ni_name)
5950 ni_name = vect_build_loop_niters (loop_vinfo);
5951 vect_generate_tmps_on_preheader (loop_vinfo, ni_name, &ratio_mult_vf,
5952 &ratio);
5953 vect_do_peeling_for_loop_bound (loop_vinfo, ni_name, ratio_mult_vf,
5954 th, check_profitability);
5956 else if (LOOP_VINFO_NITERS_KNOWN_P (loop_vinfo))
5957 ratio = build_int_cst (TREE_TYPE (LOOP_VINFO_NITERS (loop_vinfo)),
5958 LOOP_VINFO_INT_NITERS (loop_vinfo) / vectorization_factor);
5959 else
5961 if (!ni_name)
5962 ni_name = vect_build_loop_niters (loop_vinfo);
5963 vect_generate_tmps_on_preheader (loop_vinfo, ni_name, NULL, &ratio);
5966 /* 1) Make sure the loop header has exactly two entries
5967 2) Make sure we have a preheader basic block. */
5969 gcc_assert (EDGE_COUNT (loop->header->preds) == 2);
5971 split_edge (loop_preheader_edge (loop));
5973 /* FORNOW: the vectorizer supports only loops which body consist
5974 of one basic block (header + empty latch). When the vectorizer will
5975 support more involved loop forms, the order by which the BBs are
5976 traversed need to be reconsidered. */
5978 for (i = 0; i < nbbs; i++)
5980 basic_block bb = bbs[i];
5981 stmt_vec_info stmt_info;
5983 for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
5984 gsi_next (&si))
5986 gphi *phi = si.phi ();
5987 if (dump_enabled_p ())
5989 dump_printf_loc (MSG_NOTE, vect_location,
5990 "------>vectorizing phi: ");
5991 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, phi, 0);
5992 dump_printf (MSG_NOTE, "\n");
5994 stmt_info = vinfo_for_stmt (phi);
5995 if (!stmt_info)
5996 continue;
5998 if (MAY_HAVE_DEBUG_STMTS && !STMT_VINFO_LIVE_P (stmt_info))
5999 vect_loop_kill_debug_uses (loop, phi);
6001 if (!STMT_VINFO_RELEVANT_P (stmt_info)
6002 && !STMT_VINFO_LIVE_P (stmt_info))
6003 continue;
6005 if (STMT_VINFO_VECTYPE (stmt_info)
6006 && (TYPE_VECTOR_SUBPARTS (STMT_VINFO_VECTYPE (stmt_info))
6007 != (unsigned HOST_WIDE_INT) vectorization_factor)
6008 && dump_enabled_p ())
6009 dump_printf_loc (MSG_NOTE, vect_location, "multiple-types.\n");
6011 if (STMT_VINFO_DEF_TYPE (stmt_info) == vect_induction_def)
6013 if (dump_enabled_p ())
6014 dump_printf_loc (MSG_NOTE, vect_location, "transform phi.\n");
6015 vect_transform_stmt (phi, NULL, NULL, NULL, NULL);
6019 pattern_stmt = NULL;
6020 for (gimple_stmt_iterator si = gsi_start_bb (bb);
6021 !gsi_end_p (si) || transform_pattern_stmt;)
6023 bool is_store;
6025 if (transform_pattern_stmt)
6026 stmt = pattern_stmt;
6027 else
6029 stmt = gsi_stmt (si);
6030 /* During vectorization remove existing clobber stmts. */
6031 if (gimple_clobber_p (stmt))
6033 unlink_stmt_vdef (stmt);
6034 gsi_remove (&si, true);
6035 release_defs (stmt);
6036 continue;
6040 if (dump_enabled_p ())
6042 dump_printf_loc (MSG_NOTE, vect_location,
6043 "------>vectorizing statement: ");
6044 dump_gimple_stmt (MSG_NOTE, TDF_SLIM, stmt, 0);
6045 dump_printf (MSG_NOTE, "\n");
6048 stmt_info = vinfo_for_stmt (stmt);
6050 /* vector stmts created in the outer-loop during vectorization of
6051 stmts in an inner-loop may not have a stmt_info, and do not
6052 need to be vectorized. */
6053 if (!stmt_info)
6055 gsi_next (&si);
6056 continue;
6059 if (MAY_HAVE_DEBUG_STMTS && !STMT_VINFO_LIVE_P (stmt_info))
6060 vect_loop_kill_debug_uses (loop, stmt);
6062 if (!STMT_VINFO_RELEVANT_P (stmt_info)
6063 && !STMT_VINFO_LIVE_P (stmt_info))
6065 if (STMT_VINFO_IN_PATTERN_P (stmt_info)
6066 && (pattern_stmt = STMT_VINFO_RELATED_STMT (stmt_info))
6067 && (STMT_VINFO_RELEVANT_P (vinfo_for_stmt (pattern_stmt))
6068 || STMT_VINFO_LIVE_P (vinfo_for_stmt (pattern_stmt))))
6070 stmt = pattern_stmt;
6071 stmt_info = vinfo_for_stmt (stmt);
6073 else
6075 gsi_next (&si);
6076 continue;
6079 else if (STMT_VINFO_IN_PATTERN_P (stmt_info)
6080 && (pattern_stmt = STMT_VINFO_RELATED_STMT (stmt_info))
6081 && (STMT_VINFO_RELEVANT_P (vinfo_for_stmt (pattern_stmt))
6082 || STMT_VINFO_LIVE_P (vinfo_for_stmt (pattern_stmt))))
6083 transform_pattern_stmt = true;
6085 /* If pattern statement has def stmts, vectorize them too. */
6086 if (is_pattern_stmt_p (stmt_info))
6088 if (pattern_def_seq == NULL)
6090 pattern_def_seq = STMT_VINFO_PATTERN_DEF_SEQ (stmt_info);
6091 pattern_def_si = gsi_start (pattern_def_seq);
6093 else if (!gsi_end_p (pattern_def_si))
6094 gsi_next (&pattern_def_si);
6095 if (pattern_def_seq != NULL)
6097 gimple pattern_def_stmt = NULL;
6098 stmt_vec_info pattern_def_stmt_info = NULL;
6100 while (!gsi_end_p (pattern_def_si))
6102 pattern_def_stmt = gsi_stmt (pattern_def_si);
6103 pattern_def_stmt_info
6104 = vinfo_for_stmt (pattern_def_stmt);
6105 if (STMT_VINFO_RELEVANT_P (pattern_def_stmt_info)
6106 || STMT_VINFO_LIVE_P (pattern_def_stmt_info))
6107 break;
6108 gsi_next (&pattern_def_si);
6111 if (!gsi_end_p (pattern_def_si))
6113 if (dump_enabled_p ())
6115 dump_printf_loc (MSG_NOTE, vect_location,
6116 "==> vectorizing pattern def "
6117 "stmt: ");
6118 dump_gimple_stmt (MSG_NOTE, TDF_SLIM,
6119 pattern_def_stmt, 0);
6120 dump_printf (MSG_NOTE, "\n");
6123 stmt = pattern_def_stmt;
6124 stmt_info = pattern_def_stmt_info;
6126 else
6128 pattern_def_si = gsi_none ();
6129 transform_pattern_stmt = false;
6132 else
6133 transform_pattern_stmt = false;
6136 if (STMT_VINFO_VECTYPE (stmt_info))
6138 unsigned int nunits
6139 = (unsigned int)
6140 TYPE_VECTOR_SUBPARTS (STMT_VINFO_VECTYPE (stmt_info));
6141 if (!STMT_SLP_TYPE (stmt_info)
6142 && nunits != (unsigned int) vectorization_factor
6143 && dump_enabled_p ())
6144 /* For SLP VF is set according to unrolling factor, and not
6145 to vector size, hence for SLP this print is not valid. */
6146 dump_printf_loc (MSG_NOTE, vect_location, "multiple-types.\n");
6149 /* SLP. Schedule all the SLP instances when the first SLP stmt is
6150 reached. */
6151 if (STMT_SLP_TYPE (stmt_info))
6153 if (!slp_scheduled)
6155 slp_scheduled = true;
6157 if (dump_enabled_p ())
6158 dump_printf_loc (MSG_NOTE, vect_location,
6159 "=== scheduling SLP instances ===\n");
6161 vect_schedule_slp (loop_vinfo, NULL);
6164 /* Hybrid SLP stmts must be vectorized in addition to SLP. */
6165 if (!vinfo_for_stmt (stmt) || PURE_SLP_STMT (stmt_info))
6167 if (!transform_pattern_stmt && gsi_end_p (pattern_def_si))
6169 pattern_def_seq = NULL;
6170 gsi_next (&si);
6172 continue;
6176 /* -------- vectorize statement ------------ */
6177 if (dump_enabled_p ())
6178 dump_printf_loc (MSG_NOTE, vect_location, "transform statement.\n");
6180 grouped_store = false;
6181 is_store = vect_transform_stmt (stmt, &si, &grouped_store, NULL, NULL);
6182 if (is_store)
6184 if (STMT_VINFO_GROUPED_ACCESS (stmt_info))
6186 /* Interleaving. If IS_STORE is TRUE, the vectorization of the
6187 interleaving chain was completed - free all the stores in
6188 the chain. */
6189 gsi_next (&si);
6190 vect_remove_stores (GROUP_FIRST_ELEMENT (stmt_info));
6192 else
6194 /* Free the attached stmt_vec_info and remove the stmt. */
6195 gimple store = gsi_stmt (si);
6196 free_stmt_vec_info (store);
6197 unlink_stmt_vdef (store);
6198 gsi_remove (&si, true);
6199 release_defs (store);
6202 /* Stores can only appear at the end of pattern statements. */
6203 gcc_assert (!transform_pattern_stmt);
6204 pattern_def_seq = NULL;
6206 else if (!transform_pattern_stmt && gsi_end_p (pattern_def_si))
6208 pattern_def_seq = NULL;
6209 gsi_next (&si);
6211 } /* stmts in BB */
6212 } /* BBs in loop */
6214 slpeel_make_loop_iterate_ntimes (loop, ratio);
6216 /* Reduce loop iterations by the vectorization factor. */
6217 scale_loop_profile (loop, GCOV_COMPUTE_SCALE (1, vectorization_factor),
6218 expected_iterations / vectorization_factor);
6219 loop->nb_iterations_upper_bound
6220 = wi::udiv_floor (loop->nb_iterations_upper_bound, vectorization_factor);
6221 if (LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo)
6222 && loop->nb_iterations_upper_bound != 0)
6223 loop->nb_iterations_upper_bound = loop->nb_iterations_upper_bound - 1;
6224 if (loop->any_estimate)
6226 loop->nb_iterations_estimate
6227 = wi::udiv_floor (loop->nb_iterations_estimate, vectorization_factor);
6228 if (LOOP_VINFO_PEELING_FOR_GAPS (loop_vinfo)
6229 && loop->nb_iterations_estimate != 0)
6230 loop->nb_iterations_estimate = loop->nb_iterations_estimate - 1;
6233 if (dump_enabled_p ())
6235 dump_printf_loc (MSG_NOTE, vect_location,
6236 "LOOP VECTORIZED\n");
6237 if (loop->inner)
6238 dump_printf_loc (MSG_NOTE, vect_location,
6239 "OUTER LOOP VECTORIZED\n");
6240 dump_printf (MSG_NOTE, "\n");