1 /* Vectorizer
2    Copyright (C) 2003-2018 Free Software Foundation, Inc.
3    Contributed by Dorit Naishlos <dorit@il.ibm.com>
4 
5 This file is part of GCC.
6 
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
11 
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3.  If not see
19 <http://www.gnu.org/licenses/>.  */
20 
21 #ifndef GCC_TREE_VECTORIZER_H
22 #define GCC_TREE_VECTORIZER_H
23 
24 #include "tree-data-ref.h"
25 #include "tree-hash-traits.h"
26 #include "target.h"
27 
28 /* Used for naming of new temporaries.  */
29 enum vect_var_kind {
30   vect_simple_var,
31   vect_pointer_var,
32   vect_scalar_var,
33   vect_mask_var
34 };
35 
36 /* Defines type of operation.  */
37 enum operation_type {
38   unary_op = 1,
39   binary_op,
40   ternary_op
41 };
42 
43 /* Define type of available alignment support.  */
44 enum dr_alignment_support {
45   dr_unaligned_unsupported,
46   dr_unaligned_supported,
47   dr_explicit_realign,
48   dr_explicit_realign_optimized,
49   dr_aligned
50 };
51 
52 /* Define type of def-use cross-iteration cycle.  */
53 enum vect_def_type {
54   vect_uninitialized_def = 0,
55   vect_constant_def = 1,
56   vect_external_def,
57   vect_internal_def,
58   vect_induction_def,
59   vect_reduction_def,
60   vect_double_reduction_def,
61   vect_nested_cycle,
62   vect_unknown_def_type
63 };
64 
65 /* Define type of reduction.  */
66 enum vect_reduction_type {
67   TREE_CODE_REDUCTION,
68   COND_REDUCTION,
69   INTEGER_INDUC_COND_REDUCTION,
70   CONST_COND_REDUCTION,
71 
72   /* Retain a scalar phi and use a FOLD_EXTRACT_LAST within the loop
73      to implement:
74 
75        for (int i = 0; i < VF; ++i)
76          res = cond[i] ? val[i] : res;  */
77   EXTRACT_LAST_REDUCTION,
78 
79   /* Use a folding reduction within the loop to implement:
80 
81        for (int i = 0; i < VF; ++i)
82 	 res = res OP val[i];
83 
84      (with no reassocation).  */
85   FOLD_LEFT_REDUCTION
86 };
87 
88 #define VECTORIZABLE_CYCLE_DEF(D) (((D) == vect_reduction_def)           \
89                                    || ((D) == vect_double_reduction_def) \
90                                    || ((D) == vect_nested_cycle))
91 
92 /* Structure to encapsulate information about a group of like
93    instructions to be presented to the target cost model.  */
94 struct stmt_info_for_cost {
95   int count;
96   enum vect_cost_for_stmt kind;
97   gimple *stmt;
98   int misalign;
99 };
100 
101 typedef vec<stmt_info_for_cost> stmt_vector_for_cost;
102 
103 /* Maps base addresses to an innermost_loop_behavior that gives the maximum
104    known alignment for that base.  */
105 typedef hash_map<tree_operand_hash,
106 		 innermost_loop_behavior *> vec_base_alignments;
107 
108 /************************************************************************
109   SLP
110  ************************************************************************/
111 typedef struct _slp_tree *slp_tree;
112 
113 /* A computation tree of an SLP instance.  Each node corresponds to a group of
114    stmts to be packed in a SIMD stmt.  */
115 struct _slp_tree {
116   /* Nodes that contain def-stmts of this node statements operands.  */
117   vec<slp_tree> children;
118   /* A group of scalar stmts to be vectorized together.  */
119   vec<gimple *> stmts;
120   /* Load permutation relative to the stores, NULL if there is no
121      permutation.  */
122   vec<unsigned> load_permutation;
123   /* Vectorized stmt/s.  */
124   vec<gimple *> vec_stmts;
125   /* Number of vector stmts that are created to replace the group of scalar
126      stmts. It is calculated during the transformation phase as the number of
127      scalar elements in one scalar iteration (GROUP_SIZE) multiplied by VF
128      divided by vector size.  */
129   unsigned int vec_stmts_size;
130   /* Whether the scalar computations use two different operators.  */
131   bool two_operators;
132   /* The DEF type of this node.  */
133   enum vect_def_type def_type;
134 };
135 
136 
137 /* SLP instance is a sequence of stmts in a loop that can be packed into
138    SIMD stmts.  */
139 typedef struct _slp_instance {
140   /* The root of SLP tree.  */
141   slp_tree root;
142 
143   /* Size of groups of scalar stmts that will be replaced by SIMD stmt/s.  */
144   unsigned int group_size;
145 
146   /* The unrolling factor required to vectorized this SLP instance.  */
147   poly_uint64 unrolling_factor;
148 
149   /* The group of nodes that contain loads of this SLP instance.  */
150   vec<slp_tree> loads;
151 
152   /* The SLP node containing the reduction PHIs.  */
153   slp_tree reduc_phis;
154 } *slp_instance;
155 
156 
157 /* Access Functions.  */
158 #define SLP_INSTANCE_TREE(S)                     (S)->root
159 #define SLP_INSTANCE_GROUP_SIZE(S)               (S)->group_size
160 #define SLP_INSTANCE_UNROLLING_FACTOR(S)         (S)->unrolling_factor
161 #define SLP_INSTANCE_LOADS(S)                    (S)->loads
162 
163 #define SLP_TREE_CHILDREN(S)                     (S)->children
164 #define SLP_TREE_SCALAR_STMTS(S)                 (S)->stmts
165 #define SLP_TREE_VEC_STMTS(S)                    (S)->vec_stmts
166 #define SLP_TREE_NUMBER_OF_VEC_STMTS(S)          (S)->vec_stmts_size
167 #define SLP_TREE_LOAD_PERMUTATION(S)             (S)->load_permutation
168 #define SLP_TREE_TWO_OPERATORS(S)		 (S)->two_operators
169 #define SLP_TREE_DEF_TYPE(S)			 (S)->def_type
170 
171 
172 
173 /* Describes two objects whose addresses must be unequal for the vectorized
174    loop to be valid.  */
175 typedef std::pair<tree, tree> vec_object_pair;
176 
177 /* Records that vectorization is only possible if abs (EXPR) >= MIN_VALUE.
178    UNSIGNED_P is true if we can assume that abs (EXPR) == EXPR.  */
179 struct vec_lower_bound {
180   vec_lower_bound () {}
181   vec_lower_bound (tree e, bool u, poly_uint64 m)
182     : expr (e), unsigned_p (u), min_value (m) {}
183 
184   tree expr;
185   bool unsigned_p;
186   poly_uint64 min_value;
187 };
188 
189 /* Vectorizer state common between loop and basic-block vectorization.  */
190 struct vec_info {
191   enum vec_kind { bb, loop };
192 
193   vec_info (vec_kind, void *);
194   ~vec_info ();
195 
196   /* The type of vectorization.  */
197   vec_kind kind;
198 
199   /* All SLP instances.  */
200   auto_vec<slp_instance> slp_instances;
201 
202   /* All data references.  Freed by free_data_refs, so not an auto_vec.  */
203   vec<data_reference_p> datarefs;
204 
205   /* Maps base addresses to an innermost_loop_behavior that gives the maximum
206      known alignment for that base.  */
207   vec_base_alignments base_alignments;
208 
209   /* All data dependences.  Freed by free_dependence_relations, so not
210      an auto_vec.  */
211   vec<ddr_p> ddrs;
212 
213   /* All interleaving chains of stores, represented by the first
214      stmt in the chain.  */
215   auto_vec<gimple *> grouped_stores;
216 
217   /* Cost data used by the target cost model.  */
218   void *target_cost_data;
219 };
220 
221 struct _loop_vec_info;
222 struct _bb_vec_info;
223 
224 template<>
225 template<>
226 inline bool
227 is_a_helper <_loop_vec_info *>::test (vec_info *i)
228 {
229   return i->kind == vec_info::loop;
230 }
231 
232 template<>
233 template<>
234 inline bool
235 is_a_helper <_bb_vec_info *>::test (vec_info *i)
236 {
237   return i->kind == vec_info::bb;
238 }
239 
240 
241 /* In general, we can divide the vector statements in a vectorized loop
242    into related groups ("rgroups") and say that for each rgroup there is
243    some nS such that the rgroup operates on nS values from one scalar
244    iteration followed by nS values from the next.  That is, if VF is the
245    vectorization factor of the loop, the rgroup operates on a sequence:
246 
247      (1,1) (1,2) ... (1,nS) (2,1) ... (2,nS) ... (VF,1) ... (VF,nS)
248 
249    where (i,j) represents a scalar value with index j in a scalar
250    iteration with index i.
251 
252    [ We use the term "rgroup" to emphasise that this grouping isn't
253      necessarily the same as the grouping of statements used elsewhere.
254      For example, if we implement a group of scalar loads using gather
255      loads, we'll use a separate gather load for each scalar load, and
256      thus each gather load will belong to its own rgroup. ]
257 
258    In general this sequence will occupy nV vectors concatenated
259    together.  If these vectors have nL lanes each, the total number
260    of scalar values N is given by:
261 
262        N = nS * VF = nV * nL
263 
264    None of nS, VF, nV and nL are required to be a power of 2.  nS and nV
265    are compile-time constants but VF and nL can be variable (if the target
266    supports variable-length vectors).
267 
268    In classical vectorization, each iteration of the vector loop would
269    handle exactly VF iterations of the original scalar loop.  However,
270    in a fully-masked loop, a particular iteration of the vector loop
271    might handle fewer than VF iterations of the scalar loop.  The vector
272    lanes that correspond to iterations of the scalar loop are said to be
273    "active" and the other lanes are said to be "inactive".
274 
275    In a fully-masked loop, many rgroups need to be masked to ensure that
276    they have no effect for the inactive lanes.  Each such rgroup needs a
277    sequence of booleans in the same order as above, but with each (i,j)
278    replaced by a boolean that indicates whether iteration i is active.
279    This sequence occupies nV vector masks that again have nL lanes each.
280    Thus the mask sequence as a whole consists of VF independent booleans
281    that are each repeated nS times.
282 
283    We make the simplifying assumption that if a sequence of nV masks is
284    suitable for one (nS,nL) pair, we can reuse it for (nS/2,nL/2) by
285    VIEW_CONVERTing it.  This holds for all current targets that support
286    fully-masked loops.  For example, suppose the scalar loop is:
287 
288      float *f;
289      double *d;
290      for (int i = 0; i < n; ++i)
291        {
292 	 f[i * 2 + 0] += 1.0f;
293 	 f[i * 2 + 1] += 2.0f;
294 	 d[i] += 3.0;
295        }
296 
297    and suppose that vectors have 256 bits.  The vectorized f accesses
298    will belong to one rgroup and the vectorized d access to another:
299 
300      f rgroup: nS = 2, nV = 1, nL = 8
301      d rgroup: nS = 1, nV = 1, nL = 4
302 	       VF = 4
303 
304      [ In this simple example the rgroups do correspond to the normal
305        SLP grouping scheme. ]
306 
307    If only the first three lanes are active, the masks we need are:
308 
309      f rgroup: 1 1 | 1 1 | 1 1 | 0 0
310      d rgroup:  1  |  1  |  1  |  0
311 
312    Here we can use a mask calculated for f's rgroup for d's, but not
313    vice versa.
314 
315    Thus for each value of nV, it is enough to provide nV masks, with the
316    mask being calculated based on the highest nL (or, equivalently, based
317    on the highest nS) required by any rgroup with that nV.  We therefore
318    represent the entire collection of masks as a two-level table, with the
319    first level being indexed by nV - 1 (since nV == 0 doesn't exist) and
320    the second being indexed by the mask index 0 <= i < nV.  */
321 
322 /* The masks needed by rgroups with nV vectors, according to the
323    description above.  */
324 struct rgroup_masks {
325   /* The largest nS for all rgroups that use these masks.  */
326   unsigned int max_nscalars_per_iter;
327 
328   /* The type of mask to use, based on the highest nS recorded above.  */
329   tree mask_type;
330 
331   /* A vector of nV masks, in iteration order.  */
332   vec<tree> masks;
333 };
334 
335 typedef auto_vec<rgroup_masks> vec_loop_masks;
336 
337 /*-----------------------------------------------------------------*/
338 /* Info on vectorized loops.                                       */
339 /*-----------------------------------------------------------------*/
340 typedef struct _loop_vec_info : public vec_info {
341   _loop_vec_info (struct loop *);
342   ~_loop_vec_info ();
343 
344   /* The loop to which this info struct refers to.  */
345   struct loop *loop;
346 
347   /* The loop basic blocks.  */
348   basic_block *bbs;
349 
350   /* Number of latch executions.  */
351   tree num_itersm1;
352   /* Number of iterations.  */
353   tree num_iters;
354   /* Number of iterations of the original loop.  */
355   tree num_iters_unchanged;
356   /* Condition under which this loop is analyzed and versioned.  */
357   tree num_iters_assumptions;
358 
359   /* Threshold of number of iterations below which vectorzation will not be
360      performed. It is calculated from MIN_PROFITABLE_ITERS and
361      PARAM_MIN_VECT_LOOP_BOUND.  */
362   unsigned int th;
363 
364   /* When applying loop versioning, the vector form should only be used
365      if the number of scalar iterations is >= this value, on top of all
366      the other requirements.  Ignored when loop versioning is not being
367      used.  */
368   poly_uint64 versioning_threshold;
369 
370   /* Unrolling factor  */
371   poly_uint64 vectorization_factor;
372 
373   /* Maximum runtime vectorization factor, or MAX_VECTORIZATION_FACTOR
374      if there is no particular limit.  */
375   unsigned HOST_WIDE_INT max_vectorization_factor;
376 
377   /* The masks that a fully-masked loop should use to avoid operating
378      on inactive scalars.  */
379   vec_loop_masks masks;
380 
381   /* If we are using a loop mask to align memory addresses, this variable
382      contains the number of vector elements that we should skip in the
383      first iteration of the vector loop (i.e. the number of leading
384      elements that should be false in the first mask).  */
385   tree mask_skip_niters;
386 
387   /* Type of the variables to use in the WHILE_ULT call for fully-masked
388      loops.  */
389   tree mask_compare_type;
390 
391   /* Unknown DRs according to which loop was peeled.  */
392   struct data_reference *unaligned_dr;
393 
394   /* peeling_for_alignment indicates whether peeling for alignment will take
395      place, and what the peeling factor should be:
396      peeling_for_alignment = X means:
397         If X=0: Peeling for alignment will not be applied.
398         If X>0: Peel first X iterations.
399         If X=-1: Generate a runtime test to calculate the number of iterations
400                  to be peeled, using the dataref recorded in the field
401                  unaligned_dr.  */
402   int peeling_for_alignment;
403 
404   /* The mask used to check the alignment of pointers or arrays.  */
405   int ptr_mask;
406 
407   /* The loop nest in which the data dependences are computed.  */
408   auto_vec<loop_p> loop_nest;
409 
410   /* Data Dependence Relations defining address ranges that are candidates
411      for a run-time aliasing check.  */
412   auto_vec<ddr_p> may_alias_ddrs;
413 
414   /* Data Dependence Relations defining address ranges together with segment
415      lengths from which the run-time aliasing check is built.  */
416   auto_vec<dr_with_seg_len_pair_t> comp_alias_ddrs;
417 
418   /* Check that the addresses of each pair of objects is unequal.  */
419   auto_vec<vec_object_pair> check_unequal_addrs;
420 
421   /* List of values that are required to be nonzero.  This is used to check
422      whether things like "x[i * n] += 1;" are safe and eventually gets added
423      to the checks for lower bounds below.  */
424   auto_vec<tree> check_nonzero;
425 
426   /* List of values that need to be checked for a minimum value.  */
427   auto_vec<vec_lower_bound> lower_bounds;
428 
429   /* Statements in the loop that have data references that are candidates for a
430      runtime (loop versioning) misalignment check.  */
431   auto_vec<gimple *> may_misalign_stmts;
432 
433   /* Reduction cycles detected in the loop. Used in loop-aware SLP.  */
434   auto_vec<gimple *> reductions;
435 
436   /* All reduction chains in the loop, represented by the first
437      stmt in the chain.  */
438   auto_vec<gimple *> reduction_chains;
439 
440   /* Cost vector for a single scalar iteration.  */
441   auto_vec<stmt_info_for_cost> scalar_cost_vec;
442 
443   /* Map of IV base/step expressions to inserted name in the preheader.  */
444   hash_map<tree_operand_hash, tree> *ivexpr_map;
445 
446   /* The unrolling factor needed to SLP the loop. In case of that pure SLP is
447      applied to the loop, i.e., no unrolling is needed, this is 1.  */
448   poly_uint64 slp_unrolling_factor;
449 
450   /* Cost of a single scalar iteration.  */
451   int single_scalar_iteration_cost;
452 
453   /* Is the loop vectorizable? */
454   bool vectorizable;
455 
456   /* Records whether we still have the option of using a fully-masked loop.  */
457   bool can_fully_mask_p;
458 
459   /* True if have decided to use a fully-masked loop.  */
460   bool fully_masked_p;
461 
462   /* When we have grouped data accesses with gaps, we may introduce invalid
463      memory accesses.  We peel the last iteration of the loop to prevent
464      this.  */
465   bool peeling_for_gaps;
466 
467   /* When the number of iterations is not a multiple of the vector size
468      we need to peel off iterations at the end to form an epilogue loop.  */
469   bool peeling_for_niter;
470 
471   /* Reductions are canonicalized so that the last operand is the reduction
472      operand.  If this places a constant into RHS1, this decanonicalizes
473      GIMPLE for other phases, so we must track when this has occurred and
474      fix it up.  */
475   bool operands_swapped;
476 
477   /* True if there are no loop carried data dependencies in the loop.
478      If loop->safelen <= 1, then this is always true, either the loop
479      didn't have any loop carried data dependencies, or the loop is being
480      vectorized guarded with some runtime alias checks, or couldn't
481      be vectorized at all, but then this field shouldn't be used.
482      For loop->safelen >= 2, the user has asserted that there are no
483      backward dependencies, but there still could be loop carried forward
484      dependencies in such loops.  This flag will be false if normal
485      vectorizer data dependency analysis would fail or require versioning
486      for alias, but because of loop->safelen >= 2 it has been vectorized
487      even without versioning for alias.  E.g. in:
488      #pragma omp simd
489      for (int i = 0; i < m; i++)
490        a[i] = a[i + k] * c;
491      (or #pragma simd or #pragma ivdep) we can vectorize this and it will
492      DTRT even for k > 0 && k < m, but without safelen we would not
493      vectorize this, so this field would be false.  */
494   bool no_data_dependencies;
495 
496   /* Mark loops having masked stores.  */
497   bool has_mask_store;
498 
499   /* If if-conversion versioned this loop before conversion, this is the
500      loop version without if-conversion.  */
501   struct loop *scalar_loop;
502 
503   /* For loops being epilogues of already vectorized loops
504      this points to the original vectorized loop.  Otherwise NULL.  */
505   _loop_vec_info *orig_loop_info;
506 
507 } *loop_vec_info;
508 
509 /* Access Functions.  */
510 #define LOOP_VINFO_LOOP(L)                 (L)->loop
511 #define LOOP_VINFO_BBS(L)                  (L)->bbs
512 #define LOOP_VINFO_NITERSM1(L)             (L)->num_itersm1
513 #define LOOP_VINFO_NITERS(L)               (L)->num_iters
514 /* Since LOOP_VINFO_NITERS and LOOP_VINFO_NITERSM1 can change after
515    prologue peeling retain total unchanged scalar loop iterations for
516    cost model.  */
517 #define LOOP_VINFO_NITERS_UNCHANGED(L)     (L)->num_iters_unchanged
518 #define LOOP_VINFO_NITERS_ASSUMPTIONS(L)   (L)->num_iters_assumptions
519 #define LOOP_VINFO_COST_MODEL_THRESHOLD(L) (L)->th
520 #define LOOP_VINFO_VERSIONING_THRESHOLD(L) (L)->versioning_threshold
521 #define LOOP_VINFO_VECTORIZABLE_P(L)       (L)->vectorizable
522 #define LOOP_VINFO_CAN_FULLY_MASK_P(L)     (L)->can_fully_mask_p
523 #define LOOP_VINFO_FULLY_MASKED_P(L)       (L)->fully_masked_p
524 #define LOOP_VINFO_VECT_FACTOR(L)          (L)->vectorization_factor
525 #define LOOP_VINFO_MAX_VECT_FACTOR(L)      (L)->max_vectorization_factor
526 #define LOOP_VINFO_MASKS(L)                (L)->masks
527 #define LOOP_VINFO_MASK_SKIP_NITERS(L)     (L)->mask_skip_niters
528 #define LOOP_VINFO_MASK_COMPARE_TYPE(L)    (L)->mask_compare_type
529 #define LOOP_VINFO_PTR_MASK(L)             (L)->ptr_mask
530 #define LOOP_VINFO_LOOP_NEST(L)            (L)->loop_nest
531 #define LOOP_VINFO_DATAREFS(L)             (L)->datarefs
532 #define LOOP_VINFO_DDRS(L)                 (L)->ddrs
533 #define LOOP_VINFO_INT_NITERS(L)           (TREE_INT_CST_LOW ((L)->num_iters))
534 #define LOOP_VINFO_PEELING_FOR_ALIGNMENT(L) (L)->peeling_for_alignment
535 #define LOOP_VINFO_UNALIGNED_DR(L)         (L)->unaligned_dr
536 #define LOOP_VINFO_MAY_MISALIGN_STMTS(L)   (L)->may_misalign_stmts
537 #define LOOP_VINFO_MAY_ALIAS_DDRS(L)       (L)->may_alias_ddrs
538 #define LOOP_VINFO_COMP_ALIAS_DDRS(L)      (L)->comp_alias_ddrs
539 #define LOOP_VINFO_CHECK_UNEQUAL_ADDRS(L)  (L)->check_unequal_addrs
540 #define LOOP_VINFO_CHECK_NONZERO(L)        (L)->check_nonzero
541 #define LOOP_VINFO_LOWER_BOUNDS(L)         (L)->lower_bounds
542 #define LOOP_VINFO_GROUPED_STORES(L)       (L)->grouped_stores
543 #define LOOP_VINFO_SLP_INSTANCES(L)        (L)->slp_instances
544 #define LOOP_VINFO_SLP_UNROLLING_FACTOR(L) (L)->slp_unrolling_factor
545 #define LOOP_VINFO_REDUCTIONS(L)           (L)->reductions
546 #define LOOP_VINFO_REDUCTION_CHAINS(L)     (L)->reduction_chains
547 #define LOOP_VINFO_TARGET_COST_DATA(L)     (L)->target_cost_data
548 #define LOOP_VINFO_PEELING_FOR_GAPS(L)     (L)->peeling_for_gaps
549 #define LOOP_VINFO_OPERANDS_SWAPPED(L)     (L)->operands_swapped
550 #define LOOP_VINFO_PEELING_FOR_NITER(L)    (L)->peeling_for_niter
551 #define LOOP_VINFO_NO_DATA_DEPENDENCIES(L) (L)->no_data_dependencies
552 #define LOOP_VINFO_SCALAR_LOOP(L)	   (L)->scalar_loop
553 #define LOOP_VINFO_HAS_MASK_STORE(L)       (L)->has_mask_store
554 #define LOOP_VINFO_SCALAR_ITERATION_COST(L) (L)->scalar_cost_vec
555 #define LOOP_VINFO_SINGLE_SCALAR_ITERATION_COST(L) (L)->single_scalar_iteration_cost
556 #define LOOP_VINFO_ORIG_LOOP_INFO(L)       (L)->orig_loop_info
557 
558 #define LOOP_REQUIRES_VERSIONING_FOR_ALIGNMENT(L)	\
559   ((L)->may_misalign_stmts.length () > 0)
560 #define LOOP_REQUIRES_VERSIONING_FOR_ALIAS(L)		\
561   ((L)->comp_alias_ddrs.length () > 0 \
562    || (L)->check_unequal_addrs.length () > 0 \
563    || (L)->lower_bounds.length () > 0)
564 #define LOOP_REQUIRES_VERSIONING_FOR_NITERS(L)		\
565   (LOOP_VINFO_NITERS_ASSUMPTIONS (L))
566 #define LOOP_REQUIRES_VERSIONING(L)			\
567   (LOOP_REQUIRES_VERSIONING_FOR_ALIGNMENT (L)		\
568    || LOOP_REQUIRES_VERSIONING_FOR_ALIAS (L)		\
569    || LOOP_REQUIRES_VERSIONING_FOR_NITERS (L))
570 
571 #define LOOP_VINFO_NITERS_KNOWN_P(L)          \
572   (tree_fits_shwi_p ((L)->num_iters) && tree_to_shwi ((L)->num_iters) > 0)
573 
574 #define LOOP_VINFO_EPILOGUE_P(L) \
575   (LOOP_VINFO_ORIG_LOOP_INFO (L) != NULL)
576 
577 #define LOOP_VINFO_ORIG_MAX_VECT_FACTOR(L) \
578   (LOOP_VINFO_MAX_VECT_FACTOR (LOOP_VINFO_ORIG_LOOP_INFO (L)))
579 
580 static inline loop_vec_info
581 loop_vec_info_for_loop (struct loop *loop)
582 {
583   return (loop_vec_info) loop->aux;
584 }
585 
586 static inline bool
587 nested_in_vect_loop_p (struct loop *loop, gimple *stmt)
588 {
589   return (loop->inner
590           && (loop->inner == (gimple_bb (stmt))->loop_father));
591 }
592 
593 typedef struct _bb_vec_info : public vec_info
594 {
595   _bb_vec_info (gimple_stmt_iterator, gimple_stmt_iterator);
596   ~_bb_vec_info ();
597 
598   basic_block bb;
599   gimple_stmt_iterator region_begin;
600   gimple_stmt_iterator region_end;
601 } *bb_vec_info;
602 
603 #define BB_VINFO_BB(B)               (B)->bb
604 #define BB_VINFO_GROUPED_STORES(B)   (B)->grouped_stores
605 #define BB_VINFO_SLP_INSTANCES(B)    (B)->slp_instances
606 #define BB_VINFO_DATAREFS(B)         (B)->datarefs
607 #define BB_VINFO_DDRS(B)             (B)->ddrs
608 #define BB_VINFO_TARGET_COST_DATA(B) (B)->target_cost_data
609 
610 static inline bb_vec_info
611 vec_info_for_bb (basic_block bb)
612 {
613   return (bb_vec_info) bb->aux;
614 }
615 
616 /*-----------------------------------------------------------------*/
617 /* Info on vectorized defs.                                        */
618 /*-----------------------------------------------------------------*/
619 enum stmt_vec_info_type {
620   undef_vec_info_type = 0,
621   load_vec_info_type,
622   store_vec_info_type,
623   shift_vec_info_type,
624   op_vec_info_type,
625   call_vec_info_type,
626   call_simd_clone_vec_info_type,
627   assignment_vec_info_type,
628   condition_vec_info_type,
629   comparison_vec_info_type,
630   reduc_vec_info_type,
631   induc_vec_info_type,
632   type_promotion_vec_info_type,
633   type_demotion_vec_info_type,
634   type_conversion_vec_info_type,
635   loop_exit_ctrl_vec_info_type
636 };
637 
638 /* Indicates whether/how a variable is used in the scope of loop/basic
639    block.  */
640 enum vect_relevant {
641   vect_unused_in_scope = 0,
642 
643   /* The def is only used outside the loop.  */
644   vect_used_only_live,
645   /* The def is in the inner loop, and the use is in the outer loop, and the
646      use is a reduction stmt.  */
647   vect_used_in_outer_by_reduction,
648   /* The def is in the inner loop, and the use is in the outer loop (and is
649      not part of reduction).  */
650   vect_used_in_outer,
651 
652   /* defs that feed computations that end up (only) in a reduction. These
653      defs may be used by non-reduction stmts, but eventually, any
654      computations/values that are affected by these defs are used to compute
655      a reduction (i.e. don't get stored to memory, for example). We use this
656      to identify computations that we can change the order in which they are
657      computed.  */
658   vect_used_by_reduction,
659 
660   vect_used_in_scope
661 };
662 
663 /* The type of vectorization that can be applied to the stmt: regular loop-based
664    vectorization; pure SLP - the stmt is a part of SLP instances and does not
665    have uses outside SLP instances; or hybrid SLP and loop-based - the stmt is
666    a part of SLP instance and also must be loop-based vectorized, since it has
667    uses outside SLP sequences.
668 
669    In the loop context the meanings of pure and hybrid SLP are slightly
670    different. By saying that pure SLP is applied to the loop, we mean that we
671    exploit only intra-iteration parallelism in the loop; i.e., the loop can be
672    vectorized without doing any conceptual unrolling, cause we don't pack
673    together stmts from different iterations, only within a single iteration.
674    Loop hybrid SLP means that we exploit both intra-iteration and
675    inter-iteration parallelism (e.g., number of elements in the vector is 4
676    and the slp-group-size is 2, in which case we don't have enough parallelism
677    within an iteration, so we obtain the rest of the parallelism from subsequent
678    iterations by unrolling the loop by 2).  */
679 enum slp_vect_type {
680   loop_vect = 0,
681   pure_slp,
682   hybrid
683 };
684 
685 /* Says whether a statement is a load, a store of a vectorized statement
686    result, or a store of an invariant value.  */
687 enum vec_load_store_type {
688   VLS_LOAD,
689   VLS_STORE,
690   VLS_STORE_INVARIANT
691 };
692 
693 /* Describes how we're going to vectorize an individual load or store,
694    or a group of loads or stores.  */
695 enum vect_memory_access_type {
696   /* An access to an invariant address.  This is used only for loads.  */
697   VMAT_INVARIANT,
698 
699   /* A simple contiguous access.  */
700   VMAT_CONTIGUOUS,
701 
702   /* A contiguous access that goes down in memory rather than up,
703      with no additional permutation.  This is used only for stores
704      of invariants.  */
705   VMAT_CONTIGUOUS_DOWN,
706 
707   /* A simple contiguous access in which the elements need to be permuted
708      after loading or before storing.  Only used for loop vectorization;
709      SLP uses separate permutes.  */
710   VMAT_CONTIGUOUS_PERMUTE,
711 
712   /* A simple contiguous access in which the elements need to be reversed
713      after loading or before storing.  */
714   VMAT_CONTIGUOUS_REVERSE,
715 
716   /* An access that uses IFN_LOAD_LANES or IFN_STORE_LANES.  */
717   VMAT_LOAD_STORE_LANES,
718 
719   /* An access in which each scalar element is loaded or stored
720      individually.  */
721   VMAT_ELEMENTWISE,
722 
723   /* A hybrid of VMAT_CONTIGUOUS and VMAT_ELEMENTWISE, used for grouped
724      SLP accesses.  Each unrolled iteration uses a contiguous load
725      or store for the whole group, but the groups from separate iterations
726      are combined in the same way as for VMAT_ELEMENTWISE.  */
727   VMAT_STRIDED_SLP,
728 
729   /* The access uses gather loads or scatter stores.  */
730   VMAT_GATHER_SCATTER
731 };
732 
733 typedef struct data_reference *dr_p;
734 
735 typedef struct _stmt_vec_info {
736 
737   enum stmt_vec_info_type type;
738 
739   /* Indicates whether this stmts is part of a computation whose result is
740      used outside the loop.  */
741   bool live;
742 
743   /* Stmt is part of some pattern (computation idiom)  */
744   bool in_pattern_p;
745 
746   /* Is this statement vectorizable or should it be skipped in (partial)
747      vectorization.  */
748   bool vectorizable;
749 
750   /* The stmt to which this info struct refers to.  */
751   gimple *stmt;
752 
753   /* The vec_info with respect to which STMT is vectorized.  */
754   vec_info *vinfo;
755 
756   /* The vector type to be used for the LHS of this statement.  */
757   tree vectype;
758 
759   /* The vectorized version of the stmt.  */
760   gimple *vectorized_stmt;
761 
762 
763   /* The following is relevant only for stmts that contain a non-scalar
764      data-ref (array/pointer/struct access). A GIMPLE stmt is expected to have
765      at most one such data-ref.  */
766 
767   /* Information about the data-ref (access function, etc),
768      relative to the inner-most containing loop.  */
769   struct data_reference *data_ref_info;
770 
771   /* Information about the data-ref relative to this loop
772      nest (the loop that is being considered for vectorization).  */
773   innermost_loop_behavior dr_wrt_vec_loop;
774 
775   /* For loop PHI nodes, the base and evolution part of it.  This makes sure
776      this information is still available in vect_update_ivs_after_vectorizer
777      where we may not be able to re-analyze the PHI nodes evolution as
778      peeling for the prologue loop can make it unanalyzable.  The evolution
779      part is still correct after peeling, but the base may have changed from
780      the version here.  */
781   tree loop_phi_evolution_base_unchanged;
782   tree loop_phi_evolution_part;
783 
784   /* Used for various bookkeeping purposes, generally holding a pointer to
785      some other stmt S that is in some way "related" to this stmt.
786      Current use of this field is:
787         If this stmt is part of a pattern (i.e. the field 'in_pattern_p' is
788         true): S is the "pattern stmt" that represents (and replaces) the
789         sequence of stmts that constitutes the pattern.  Similarly, the
790         related_stmt of the "pattern stmt" points back to this stmt (which is
791         the last stmt in the original sequence of stmts that constitutes the
792         pattern).  */
793   gimple *related_stmt;
794 
795   /* Used to keep a sequence of def stmts of a pattern stmt if such exists.  */
796   gimple_seq pattern_def_seq;
797 
798   /* List of datarefs that are known to have the same alignment as the dataref
799      of this stmt.  */
800   vec<dr_p> same_align_refs;
801 
802   /* Selected SIMD clone's function info.  First vector element
803      is SIMD clone's function decl, followed by a pair of trees (base + step)
804      for linear arguments (pair of NULLs for other arguments).  */
805   vec<tree> simd_clone_info;
806 
807   /* Classify the def of this stmt.  */
808   enum vect_def_type def_type;
809 
810   /*  Whether the stmt is SLPed, loop-based vectorized, or both.  */
811   enum slp_vect_type slp_type;
812 
813   /* Interleaving and reduction chains info.  */
814   /* First element in the group.  */
815   gimple *first_element;
816   /* Pointer to the next element in the group.  */
817   gimple *next_element;
818   /* For data-refs, in case that two or more stmts share data-ref, this is the
819      pointer to the previously detected stmt with the same dr.  */
820   gimple *same_dr_stmt;
821   /* The size of the group.  */
822   unsigned int size;
823   /* For stores, number of stores from this group seen. We vectorize the last
824      one.  */
825   unsigned int store_count;
826   /* For loads only, the gap from the previous load. For consecutive loads, GAP
827      is 1.  */
828   unsigned int gap;
829 
830   /* The minimum negative dependence distance this stmt participates in
831      or zero if none.  */
832   unsigned int min_neg_dist;
833 
834   /* Not all stmts in the loop need to be vectorized. e.g, the increment
835      of the loop induction variable and computation of array indexes. relevant
836      indicates whether the stmt needs to be vectorized.  */
837   enum vect_relevant relevant;
838 
839   /* For loads if this is a gather, for stores if this is a scatter.  */
840   bool gather_scatter_p;
841 
842   /* True if this is an access with loop-invariant stride.  */
843   bool strided_p;
844 
845   /* For both loads and stores.  */
846   bool simd_lane_access_p;
847 
848   /* Classifies how the load or store is going to be implemented
849      for loop vectorization.  */
850   vect_memory_access_type memory_access_type;
851 
852   /* For reduction loops, this is the type of reduction.  */
853   enum vect_reduction_type v_reduc_type;
854 
855   /* For CONST_COND_REDUCTION, record the reduc code.  */
856   enum tree_code const_cond_reduc_code;
857 
858   /* On a reduction PHI the reduction type as detected by
859      vect_force_simple_reduction.  */
860   enum vect_reduction_type reduc_type;
861 
862   /* On a reduction PHI the def returned by vect_force_simple_reduction.
863      On the def returned by vect_force_simple_reduction the
864      corresponding PHI.  */
865   gimple *reduc_def;
866 
867   /* The number of scalar stmt references from active SLP instances.  */
868   unsigned int num_slp_uses;
869 } *stmt_vec_info;
870 
871 /* Information about a gather/scatter call.  */
872 struct gather_scatter_info {
873   /* The internal function to use for the gather/scatter operation,
874      or IFN_LAST if a built-in function should be used instead.  */
875   internal_fn ifn;
876 
877   /* The FUNCTION_DECL for the built-in gather/scatter function,
878      or null if an internal function should be used instead.  */
879   tree decl;
880 
881   /* The loop-invariant base value.  */
882   tree base;
883 
884   /* The original scalar offset, which is a non-loop-invariant SSA_NAME.  */
885   tree offset;
886 
887   /* Each offset element should be multiplied by this amount before
888      being added to the base.  */
889   int scale;
890 
891   /* The definition type for the vectorized offset.  */
892   enum vect_def_type offset_dt;
893 
894   /* The type of the vectorized offset.  */
895   tree offset_vectype;
896 
897   /* The type of the scalar elements after loading or before storing.  */
898   tree element_type;
899 
900   /* The type of the scalar elements being loaded or stored.  */
901   tree memory_type;
902 };
903 
904 /* Access Functions.  */
905 #define STMT_VINFO_TYPE(S)                 (S)->type
906 #define STMT_VINFO_STMT(S)                 (S)->stmt
907 inline loop_vec_info
908 STMT_VINFO_LOOP_VINFO (stmt_vec_info stmt_vinfo)
909 {
910   if (loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (stmt_vinfo->vinfo))
911     return loop_vinfo;
912   return NULL;
913 }
914 inline bb_vec_info
915 STMT_VINFO_BB_VINFO (stmt_vec_info stmt_vinfo)
916 {
917   if (bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (stmt_vinfo->vinfo))
918     return bb_vinfo;
919   return NULL;
920 }
921 #define STMT_VINFO_RELEVANT(S)             (S)->relevant
922 #define STMT_VINFO_LIVE_P(S)               (S)->live
923 #define STMT_VINFO_VECTYPE(S)              (S)->vectype
924 #define STMT_VINFO_VEC_STMT(S)             (S)->vectorized_stmt
925 #define STMT_VINFO_VECTORIZABLE(S)         (S)->vectorizable
926 #define STMT_VINFO_DATA_REF(S)             (S)->data_ref_info
927 #define STMT_VINFO_GATHER_SCATTER_P(S)	   (S)->gather_scatter_p
928 #define STMT_VINFO_STRIDED_P(S)	   	   (S)->strided_p
929 #define STMT_VINFO_MEMORY_ACCESS_TYPE(S)   (S)->memory_access_type
930 #define STMT_VINFO_SIMD_LANE_ACCESS_P(S)   (S)->simd_lane_access_p
931 #define STMT_VINFO_VEC_REDUCTION_TYPE(S)   (S)->v_reduc_type
932 #define STMT_VINFO_VEC_CONST_COND_REDUC_CODE(S) (S)->const_cond_reduc_code
933 
934 #define STMT_VINFO_DR_WRT_VEC_LOOP(S)      (S)->dr_wrt_vec_loop
935 #define STMT_VINFO_DR_BASE_ADDRESS(S)      (S)->dr_wrt_vec_loop.base_address
936 #define STMT_VINFO_DR_INIT(S)              (S)->dr_wrt_vec_loop.init
937 #define STMT_VINFO_DR_OFFSET(S)            (S)->dr_wrt_vec_loop.offset
938 #define STMT_VINFO_DR_STEP(S)              (S)->dr_wrt_vec_loop.step
939 #define STMT_VINFO_DR_BASE_ALIGNMENT(S)    (S)->dr_wrt_vec_loop.base_alignment
940 #define STMT_VINFO_DR_BASE_MISALIGNMENT(S) \
941   (S)->dr_wrt_vec_loop.base_misalignment
942 #define STMT_VINFO_DR_OFFSET_ALIGNMENT(S) \
943   (S)->dr_wrt_vec_loop.offset_alignment
944 #define STMT_VINFO_DR_STEP_ALIGNMENT(S) \
945   (S)->dr_wrt_vec_loop.step_alignment
946 
947 #define STMT_VINFO_IN_PATTERN_P(S)         (S)->in_pattern_p
948 #define STMT_VINFO_RELATED_STMT(S)         (S)->related_stmt
949 #define STMT_VINFO_PATTERN_DEF_SEQ(S)      (S)->pattern_def_seq
950 #define STMT_VINFO_SAME_ALIGN_REFS(S)      (S)->same_align_refs
951 #define STMT_VINFO_SIMD_CLONE_INFO(S)	   (S)->simd_clone_info
952 #define STMT_VINFO_DEF_TYPE(S)             (S)->def_type
953 #define STMT_VINFO_GROUP_FIRST_ELEMENT(S)  (S)->first_element
954 #define STMT_VINFO_GROUP_NEXT_ELEMENT(S)   (S)->next_element
955 #define STMT_VINFO_GROUP_SIZE(S)           (S)->size
956 #define STMT_VINFO_GROUP_STORE_COUNT(S)    (S)->store_count
957 #define STMT_VINFO_GROUP_GAP(S)            (S)->gap
958 #define STMT_VINFO_GROUP_SAME_DR_STMT(S)   (S)->same_dr_stmt
959 #define STMT_VINFO_GROUPED_ACCESS(S)      ((S)->first_element != NULL && (S)->data_ref_info)
960 #define STMT_VINFO_LOOP_PHI_EVOLUTION_BASE_UNCHANGED(S) (S)->loop_phi_evolution_base_unchanged
961 #define STMT_VINFO_LOOP_PHI_EVOLUTION_PART(S) (S)->loop_phi_evolution_part
962 #define STMT_VINFO_MIN_NEG_DIST(S)	(S)->min_neg_dist
963 #define STMT_VINFO_NUM_SLP_USES(S)	(S)->num_slp_uses
964 #define STMT_VINFO_REDUC_TYPE(S)	(S)->reduc_type
965 #define STMT_VINFO_REDUC_DEF(S)		(S)->reduc_def
966 
967 #define GROUP_FIRST_ELEMENT(S)          (S)->first_element
968 #define GROUP_NEXT_ELEMENT(S)           (S)->next_element
969 #define GROUP_SIZE(S)                   (S)->size
970 #define GROUP_STORE_COUNT(S)            (S)->store_count
971 #define GROUP_GAP(S)                    (S)->gap
972 #define GROUP_SAME_DR_STMT(S)           (S)->same_dr_stmt
973 
974 #define STMT_VINFO_RELEVANT_P(S)          ((S)->relevant != vect_unused_in_scope)
975 
976 #define HYBRID_SLP_STMT(S)                ((S)->slp_type == hybrid)
977 #define PURE_SLP_STMT(S)                  ((S)->slp_type == pure_slp)
978 #define STMT_SLP_TYPE(S)                   (S)->slp_type
979 
980 struct dataref_aux {
981   /* The misalignment in bytes of the reference, or -1 if not known.  */
982   int misalignment;
983   /* The byte alignment that we'd ideally like the reference to have,
984      and the value that misalignment is measured against.  */
985   int target_alignment;
986   /* If true the alignment of base_decl needs to be increased.  */
987   bool base_misaligned;
988   tree base_decl;
989 };
990 
991 #define DR_VECT_AUX(dr) ((dataref_aux *)(dr)->aux)
992 
993 #define VECT_MAX_COST 1000
994 
995 /* The maximum number of intermediate steps required in multi-step type
996    conversion.  */
997 #define MAX_INTERM_CVT_STEPS         3
998 
999 #define MAX_VECTORIZATION_FACTOR INT_MAX
1000 
1001 /* Nonzero if TYPE represents a (scalar) boolean type or type
1002    in the middle-end compatible with it (unsigned precision 1 integral
1003    types).  Used to determine which types should be vectorized as
1004    VECTOR_BOOLEAN_TYPE_P.  */
1005 
1006 #define VECT_SCALAR_BOOLEAN_TYPE_P(TYPE) \
1007   (TREE_CODE (TYPE) == BOOLEAN_TYPE		\
1008    || ((TREE_CODE (TYPE) == INTEGER_TYPE	\
1009 	|| TREE_CODE (TYPE) == ENUMERAL_TYPE)	\
1010        && TYPE_PRECISION (TYPE) == 1		\
1011        && TYPE_UNSIGNED (TYPE)))
1012 
1013 extern vec<stmt_vec_info> stmt_vec_info_vec;
1014 
1015 void init_stmt_vec_info_vec (void);
1016 void free_stmt_vec_info_vec (void);
1017 
1018 /* Return a stmt_vec_info corresponding to STMT.  */
1019 
1020 static inline stmt_vec_info
1021 vinfo_for_stmt (gimple *stmt)
1022 {
1023   int uid = gimple_uid (stmt);
1024   if (uid <= 0)
1025     return NULL;
1026 
1027   return stmt_vec_info_vec[uid - 1];
1028 }
1029 
1030 /* Set vectorizer information INFO for STMT.  */
1031 
1032 static inline void
1033 set_vinfo_for_stmt (gimple *stmt, stmt_vec_info info)
1034 {
1035   unsigned int uid = gimple_uid (stmt);
1036   if (uid == 0)
1037     {
1038       gcc_checking_assert (info);
1039       uid = stmt_vec_info_vec.length () + 1;
1040       gimple_set_uid (stmt, uid);
1041       stmt_vec_info_vec.safe_push (info);
1042     }
1043   else
1044     {
1045       gcc_checking_assert (info == NULL);
1046       stmt_vec_info_vec[uid - 1] = info;
1047     }
1048 }
1049 
1050 /* Return TRUE if a statement represented by STMT_INFO is a part of a
1051    pattern.  */
1052 
1053 static inline bool
1054 is_pattern_stmt_p (stmt_vec_info stmt_info)
1055 {
1056   gimple *related_stmt;
1057   stmt_vec_info related_stmt_info;
1058 
1059   related_stmt = STMT_VINFO_RELATED_STMT (stmt_info);
1060   if (related_stmt
1061       && (related_stmt_info = vinfo_for_stmt (related_stmt))
1062       && STMT_VINFO_IN_PATTERN_P (related_stmt_info))
1063     return true;
1064 
1065   return false;
1066 }
1067 
1068 /* Return the later statement between STMT1 and STMT2.  */
1069 
1070 static inline gimple *
1071 get_later_stmt (gimple *stmt1, gimple *stmt2)
1072 {
1073   unsigned int uid1, uid2;
1074 
1075   if (stmt1 == NULL)
1076     return stmt2;
1077 
1078   if (stmt2 == NULL)
1079     return stmt1;
1080 
1081   stmt_vec_info stmt_info1 = vinfo_for_stmt (stmt1);
1082   stmt_vec_info stmt_info2 = vinfo_for_stmt (stmt2);
1083   uid1 = gimple_uid (is_pattern_stmt_p (stmt_info1)
1084 		     ? STMT_VINFO_RELATED_STMT (stmt_info1) : stmt1);
1085   uid2 = gimple_uid (is_pattern_stmt_p (stmt_info2)
1086 		     ? STMT_VINFO_RELATED_STMT (stmt_info2) : stmt2);
1087 
1088   if (uid1 == 0 || uid2 == 0)
1089     return NULL;
1090 
1091   gcc_assert (uid1 <= stmt_vec_info_vec.length ());
1092   gcc_assert (uid2 <= stmt_vec_info_vec.length ());
1093 
1094   if (uid1 > uid2)
1095     return stmt1;
1096   else
1097     return stmt2;
1098 }
1099 
1100 /* Return true if BB is a loop header.  */
1101 
1102 static inline bool
1103 is_loop_header_bb_p (basic_block bb)
1104 {
1105   if (bb == (bb->loop_father)->header)
1106     return true;
1107   gcc_checking_assert (EDGE_COUNT (bb->preds) == 1);
1108   return false;
1109 }
1110 
1111 /* Return pow2 (X).  */
1112 
1113 static inline int
1114 vect_pow2 (int x)
1115 {
1116   int i, res = 1;
1117 
1118   for (i = 0; i < x; i++)
1119     res *= 2;
1120 
1121   return res;
1122 }
1123 
1124 /* Alias targetm.vectorize.builtin_vectorization_cost.  */
1125 
1126 static inline int
1127 builtin_vectorization_cost (enum vect_cost_for_stmt type_of_cost,
1128 			    tree vectype, int misalign)
1129 {
1130   return targetm.vectorize.builtin_vectorization_cost (type_of_cost,
1131 						       vectype, misalign);
1132 }
1133 
1134 /* Get cost by calling cost target builtin.  */
1135 
1136 static inline
1137 int vect_get_stmt_cost (enum vect_cost_for_stmt type_of_cost)
1138 {
1139   return builtin_vectorization_cost (type_of_cost, NULL, 0);
1140 }
1141 
1142 /* Alias targetm.vectorize.init_cost.  */
1143 
1144 static inline void *
1145 init_cost (struct loop *loop_info)
1146 {
1147   return targetm.vectorize.init_cost (loop_info);
1148 }
1149 
1150 /* Alias targetm.vectorize.add_stmt_cost.  */
1151 
1152 static inline unsigned
1153 add_stmt_cost (void *data, int count, enum vect_cost_for_stmt kind,
1154 	       stmt_vec_info stmt_info, int misalign,
1155 	       enum vect_cost_model_location where)
1156 {
1157   return targetm.vectorize.add_stmt_cost (data, count, kind,
1158 					  stmt_info, misalign, where);
1159 }
1160 
1161 /* Alias targetm.vectorize.finish_cost.  */
1162 
1163 static inline void
1164 finish_cost (void *data, unsigned *prologue_cost,
1165 	     unsigned *body_cost, unsigned *epilogue_cost)
1166 {
1167   targetm.vectorize.finish_cost (data, prologue_cost, body_cost, epilogue_cost);
1168 }
1169 
1170 /* Alias targetm.vectorize.destroy_cost_data.  */
1171 
1172 static inline void
1173 destroy_cost_data (void *data)
1174 {
1175   targetm.vectorize.destroy_cost_data (data);
1176 }
1177 
1178 /*-----------------------------------------------------------------*/
1179 /* Info on data references alignment.                              */
1180 /*-----------------------------------------------------------------*/
1181 inline void
1182 set_dr_misalignment (struct data_reference *dr, int val)
1183 {
1184   dataref_aux *data_aux = DR_VECT_AUX (dr);
1185 
1186   if (!data_aux)
1187     {
1188       data_aux = XCNEW (dataref_aux);
1189       dr->aux = data_aux;
1190     }
1191 
1192   data_aux->misalignment = val;
1193 }
1194 
1195 inline int
1196 dr_misalignment (struct data_reference *dr)
1197 {
1198   return DR_VECT_AUX (dr)->misalignment;
1199 }
1200 
1201 /* Reflects actual alignment of first access in the vectorized loop,
1202    taking into account peeling/versioning if applied.  */
1203 #define DR_MISALIGNMENT(DR) dr_misalignment (DR)
1204 #define SET_DR_MISALIGNMENT(DR, VAL) set_dr_misalignment (DR, VAL)
1205 #define DR_MISALIGNMENT_UNKNOWN (-1)
1206 
1207 /* Only defined once DR_MISALIGNMENT is defined.  */
1208 #define DR_TARGET_ALIGNMENT(DR) DR_VECT_AUX (DR)->target_alignment
1209 
1210 /* Return true if data access DR is aligned to its target alignment
1211    (which may be less than a full vector).  */
1212 
1213 static inline bool
1214 aligned_access_p (struct data_reference *data_ref_info)
1215 {
1216   return (DR_MISALIGNMENT (data_ref_info) == 0);
1217 }
1218 
1219 /* Return TRUE if the alignment of the data access is known, and FALSE
1220    otherwise.  */
1221 
1222 static inline bool
1223 known_alignment_for_access_p (struct data_reference *data_ref_info)
1224 {
1225   return (DR_MISALIGNMENT (data_ref_info) != DR_MISALIGNMENT_UNKNOWN);
1226 }
1227 
1228 /* Return the minimum alignment in bytes that the vectorized version
1229    of DR is guaranteed to have.  */
1230 
1231 static inline unsigned int
1232 vect_known_alignment_in_bytes (struct data_reference *dr)
1233 {
1234   if (DR_MISALIGNMENT (dr) == DR_MISALIGNMENT_UNKNOWN)
1235     return TYPE_ALIGN_UNIT (TREE_TYPE (DR_REF (dr)));
1236   if (DR_MISALIGNMENT (dr) == 0)
1237     return DR_TARGET_ALIGNMENT (dr);
1238   return DR_MISALIGNMENT (dr) & -DR_MISALIGNMENT (dr);
1239 }
1240 
1241 /* Return the behavior of DR with respect to the vectorization context
1242    (which for outer loop vectorization might not be the behavior recorded
1243    in DR itself).  */
1244 
1245 static inline innermost_loop_behavior *
1246 vect_dr_behavior (data_reference *dr)
1247 {
1248   gimple *stmt = DR_STMT (dr);
1249   stmt_vec_info stmt_info = vinfo_for_stmt (stmt);
1250   loop_vec_info loop_vinfo = STMT_VINFO_LOOP_VINFO (stmt_info);
1251   if (loop_vinfo == NULL
1252       || !nested_in_vect_loop_p (LOOP_VINFO_LOOP (loop_vinfo), stmt))
1253     return &DR_INNERMOST (dr);
1254   else
1255     return &STMT_VINFO_DR_WRT_VEC_LOOP (stmt_info);
1256 }
1257 
1258 /* Return true if the vect cost model is unlimited.  */
1259 static inline bool
1260 unlimited_cost_model (loop_p loop)
1261 {
1262   if (loop != NULL && loop->force_vectorize
1263       && flag_simd_cost_model != VECT_COST_MODEL_DEFAULT)
1264     return flag_simd_cost_model == VECT_COST_MODEL_UNLIMITED;
1265   return (flag_vect_cost_model == VECT_COST_MODEL_UNLIMITED);
1266 }
1267 
1268 /* Return true if the loop described by LOOP_VINFO is fully-masked and
1269    if the first iteration should use a partial mask in order to achieve
1270    alignment.  */
1271 
1272 static inline bool
1273 vect_use_loop_mask_for_alignment_p (loop_vec_info loop_vinfo)
1274 {
1275   return (LOOP_VINFO_FULLY_MASKED_P (loop_vinfo)
1276 	  && LOOP_VINFO_PEELING_FOR_ALIGNMENT (loop_vinfo));
1277 }
1278 
1279 /* Return the number of vectors of type VECTYPE that are needed to get
1280    NUNITS elements.  NUNITS should be based on the vectorization factor,
1281    so it is always a known multiple of the number of elements in VECTYPE.  */
1282 
1283 static inline unsigned int
1284 vect_get_num_vectors (poly_uint64 nunits, tree vectype)
1285 {
1286   return exact_div (nunits, TYPE_VECTOR_SUBPARTS (vectype)).to_constant ();
1287 }
1288 
1289 /* Return the number of copies needed for loop vectorization when
1290    a statement operates on vectors of type VECTYPE.  This is the
1291    vectorization factor divided by the number of elements in
1292    VECTYPE and is always known at compile time.  */
1293 
1294 static inline unsigned int
1295 vect_get_num_copies (loop_vec_info loop_vinfo, tree vectype)
1296 {
1297   return vect_get_num_vectors (LOOP_VINFO_VECT_FACTOR (loop_vinfo), vectype);
1298 }
1299 
1300 /* Update maximum unit count *MAX_NUNITS so that it accounts for
1301    the number of units in vector type VECTYPE.  *MAX_NUNITS can be 1
1302    if we haven't yet recorded any vector types.  */
1303 
1304 static inline void
1305 vect_update_max_nunits (poly_uint64 *max_nunits, tree vectype)
1306 {
1307   /* All unit counts have the form current_vector_size * X for some
1308      rational X, so two unit sizes must have a common multiple.
1309      Everything is a multiple of the initial value of 1.  */
1310   poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
1311   *max_nunits = force_common_multiple (*max_nunits, nunits);
1312 }
1313 
1314 /* Return the vectorization factor that should be used for costing
1315    purposes while vectorizing the loop described by LOOP_VINFO.
1316    Pick a reasonable estimate if the vectorization factor isn't
1317    known at compile time.  */
1318 
1319 static inline unsigned int
1320 vect_vf_for_cost (loop_vec_info loop_vinfo)
1321 {
1322   return estimated_poly_value (LOOP_VINFO_VECT_FACTOR (loop_vinfo));
1323 }
1324 
1325 /* Estimate the number of elements in VEC_TYPE for costing purposes.
1326    Pick a reasonable estimate if the exact number isn't known at
1327    compile time.  */
1328 
1329 static inline unsigned int
1330 vect_nunits_for_cost (tree vec_type)
1331 {
1332   return estimated_poly_value (TYPE_VECTOR_SUBPARTS (vec_type));
1333 }
1334 
1335 /* Return the maximum possible vectorization factor for LOOP_VINFO.  */
1336 
1337 static inline unsigned HOST_WIDE_INT
1338 vect_max_vf (loop_vec_info loop_vinfo)
1339 {
1340   unsigned HOST_WIDE_INT vf;
1341   if (LOOP_VINFO_VECT_FACTOR (loop_vinfo).is_constant (&vf))
1342     return vf;
1343   return MAX_VECTORIZATION_FACTOR;
1344 }
1345 
1346 /* Return the size of the value accessed by unvectorized data reference DR.
1347    This is only valid once STMT_VINFO_VECTYPE has been calculated for the
1348    associated gimple statement, since that guarantees that DR accesses
1349    either a scalar or a scalar equivalent.  ("Scalar equivalent" here
1350    includes things like V1SI, which can be vectorized in the same way
1351    as a plain SI.)  */
1352 
1353 inline unsigned int
1354 vect_get_scalar_dr_size (struct data_reference *dr)
1355 {
1356   return tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dr))));
1357 }
1358 
1359 /* Source location */
1360 extern source_location vect_location;
1361 
1362 /*-----------------------------------------------------------------*/
1363 /* Function prototypes.                                            */
1364 /*-----------------------------------------------------------------*/
1365 
1366 /* Simple loop peeling and versioning utilities for vectorizer's purposes -
1367    in tree-vect-loop-manip.c.  */
1368 extern void vect_set_loop_condition (struct loop *, loop_vec_info,
1369 				     tree, tree, tree, bool);
1370 extern bool slpeel_can_duplicate_loop_p (const struct loop *, const_edge);
1371 struct loop *slpeel_tree_duplicate_loop_to_edge_cfg (struct loop *,
1372 						     struct loop *, edge);
1373 extern void vect_loop_versioning (loop_vec_info, unsigned int, bool,
1374 				  poly_uint64);
1375 extern struct loop *vect_do_peeling (loop_vec_info, tree, tree,
1376 				     tree *, tree *, tree *, int, bool, bool);
1377 extern void vect_prepare_for_masked_peels (loop_vec_info);
1378 extern source_location find_loop_location (struct loop *);
1379 extern bool vect_can_advance_ivs_p (loop_vec_info);
1380 
1381 /* In tree-vect-stmts.c.  */
1382 extern poly_uint64 current_vector_size;
1383 extern tree get_vectype_for_scalar_type (tree);
1384 extern tree get_vectype_for_scalar_type_and_size (tree, poly_uint64);
1385 extern tree get_mask_type_for_scalar_type (tree);
1386 extern tree get_same_sized_vectype (tree, tree);
1387 extern bool vect_get_loop_mask_type (loop_vec_info);
1388 extern bool vect_is_simple_use (tree, vec_info *, gimple **,
1389                                 enum vect_def_type *);
1390 extern bool vect_is_simple_use (tree, vec_info *, gimple **,
1391 				enum vect_def_type *, tree *);
1392 extern bool supportable_widening_operation (enum tree_code, gimple *, tree,
1393 					    tree, enum tree_code *,
1394 					    enum tree_code *, int *,
1395 					    vec<tree> *);
1396 extern bool supportable_narrowing_operation (enum tree_code, tree, tree,
1397 					     enum tree_code *,
1398 					     int *, vec<tree> *);
1399 extern stmt_vec_info new_stmt_vec_info (gimple *stmt, vec_info *);
1400 extern void free_stmt_vec_info (gimple *stmt);
1401 extern void vect_model_simple_cost (stmt_vec_info, int, enum vect_def_type *,
1402 				    int, stmt_vector_for_cost *,
1403 				    stmt_vector_for_cost *);
1404 extern void vect_model_store_cost (stmt_vec_info, int, vect_memory_access_type,
1405 				   vec_load_store_type, slp_tree,
1406 				   stmt_vector_for_cost *,
1407 				   stmt_vector_for_cost *);
1408 extern void vect_model_load_cost (stmt_vec_info, int, vect_memory_access_type,
1409 				  slp_tree, stmt_vector_for_cost *,
1410 				  stmt_vector_for_cost *);
1411 extern unsigned record_stmt_cost (stmt_vector_for_cost *, int,
1412 				  enum vect_cost_for_stmt, stmt_vec_info,
1413 				  int, enum vect_cost_model_location);
1414 extern void vect_finish_replace_stmt (gimple *, gimple *);
1415 extern void vect_finish_stmt_generation (gimple *, gimple *,
1416                                          gimple_stmt_iterator *);
1417 extern bool vect_mark_stmts_to_be_vectorized (loop_vec_info);
1418 extern tree vect_get_store_rhs (gimple *);
1419 extern tree vect_get_vec_def_for_operand_1 (gimple *, enum vect_def_type);
1420 extern tree vect_get_vec_def_for_operand (tree, gimple *, tree = NULL);
1421 extern void vect_get_vec_defs (tree, tree, gimple *, vec<tree> *,
1422 			       vec<tree> *, slp_tree);
1423 extern void vect_get_vec_defs_for_stmt_copy (enum vect_def_type *,
1424 					     vec<tree> *, vec<tree> *);
1425 extern tree vect_init_vector (gimple *, tree, tree,
1426                               gimple_stmt_iterator *);
1427 extern tree vect_get_vec_def_for_stmt_copy (enum vect_def_type, tree);
1428 extern bool vect_transform_stmt (gimple *, gimple_stmt_iterator *,
1429                                  bool *, slp_tree, slp_instance);
1430 extern void vect_remove_stores (gimple *);
1431 extern bool vect_analyze_stmt (gimple *, bool *, slp_tree, slp_instance);
1432 extern bool vectorizable_condition (gimple *, gimple_stmt_iterator *,
1433 				    gimple **, tree, int, slp_tree);
1434 extern void vect_get_load_cost (struct data_reference *, int, bool,
1435 				unsigned int *, unsigned int *,
1436 				stmt_vector_for_cost *,
1437 				stmt_vector_for_cost *, bool);
1438 extern void vect_get_store_cost (struct data_reference *, int,
1439 				 unsigned int *, stmt_vector_for_cost *);
1440 extern bool vect_supportable_shift (enum tree_code, tree);
1441 extern tree vect_gen_perm_mask_any (tree, const vec_perm_indices &);
1442 extern tree vect_gen_perm_mask_checked (tree, const vec_perm_indices &);
1443 extern void optimize_mask_stores (struct loop*);
1444 extern gcall *vect_gen_while (tree, tree, tree);
1445 extern tree vect_gen_while_not (gimple_seq *, tree, tree, tree);
1446 
1447 /* In tree-vect-data-refs.c.  */
1448 extern bool vect_can_force_dr_alignment_p (const_tree, unsigned int);
1449 extern enum dr_alignment_support vect_supportable_dr_alignment
1450                                            (struct data_reference *, bool);
1451 extern tree vect_get_smallest_scalar_type (gimple *, HOST_WIDE_INT *,
1452                                            HOST_WIDE_INT *);
1453 extern bool vect_analyze_data_ref_dependences (loop_vec_info, unsigned int *);
1454 extern bool vect_slp_analyze_instance_dependence (slp_instance);
1455 extern bool vect_enhance_data_refs_alignment (loop_vec_info);
1456 extern bool vect_analyze_data_refs_alignment (loop_vec_info);
1457 extern bool vect_verify_datarefs_alignment (loop_vec_info);
1458 extern bool vect_slp_analyze_and_verify_instance_alignment (slp_instance);
1459 extern bool vect_analyze_data_ref_accesses (vec_info *);
1460 extern bool vect_prune_runtime_alias_test_list (loop_vec_info);
1461 extern bool vect_gather_scatter_fn_p (bool, bool, tree, tree, unsigned int,
1462 				      signop, int, internal_fn *, tree *);
1463 extern bool vect_check_gather_scatter (gimple *, loop_vec_info,
1464 				       gather_scatter_info *);
1465 extern bool vect_analyze_data_refs (vec_info *, poly_uint64 *);
1466 extern void vect_record_base_alignments (vec_info *);
1467 extern tree vect_create_data_ref_ptr (gimple *, tree, struct loop *, tree,
1468 				      tree *, gimple_stmt_iterator *,
1469 				      gimple **, bool, bool *,
1470 				      tree = NULL_TREE, tree = NULL_TREE);
1471 extern tree bump_vector_ptr (tree, gimple *, gimple_stmt_iterator *, gimple *,
1472 			     tree);
1473 extern void vect_copy_ref_info (tree, tree);
1474 extern tree vect_create_destination_var (tree, tree);
1475 extern bool vect_grouped_store_supported (tree, unsigned HOST_WIDE_INT);
1476 extern bool vect_store_lanes_supported (tree, unsigned HOST_WIDE_INT, bool);
1477 extern bool vect_grouped_load_supported (tree, bool, unsigned HOST_WIDE_INT);
1478 extern bool vect_load_lanes_supported (tree, unsigned HOST_WIDE_INT, bool);
1479 extern void vect_permute_store_chain (vec<tree> ,unsigned int, gimple *,
1480                                     gimple_stmt_iterator *, vec<tree> *);
1481 extern tree vect_setup_realignment (gimple *, gimple_stmt_iterator *, tree *,
1482                                     enum dr_alignment_support, tree,
1483                                     struct loop **);
1484 extern void vect_transform_grouped_load (gimple *, vec<tree> , int,
1485                                          gimple_stmt_iterator *);
1486 extern void vect_record_grouped_load_vectors (gimple *, vec<tree> );
1487 extern tree vect_get_new_vect_var (tree, enum vect_var_kind, const char *);
1488 extern tree vect_get_new_ssa_name (tree, enum vect_var_kind,
1489 				   const char * = NULL);
1490 extern tree vect_create_addr_base_for_vector_ref (gimple *, gimple_seq *,
1491 						  tree, tree = NULL_TREE);
1492 
1493 /* In tree-vect-loop.c.  */
1494 /* FORNOW: Used in tree-parloops.c.  */
1495 extern gimple *vect_force_simple_reduction (loop_vec_info, gimple *,
1496 					    bool *, bool);
1497 /* Used in gimple-loop-interchange.c.  */
1498 extern bool check_reduction_path (location_t, loop_p, gphi *, tree,
1499 				  enum tree_code);
1500 /* Drive for loop analysis stage.  */
1501 extern loop_vec_info vect_analyze_loop (struct loop *, loop_vec_info);
1502 extern tree vect_build_loop_niters (loop_vec_info, bool * = NULL);
1503 extern void vect_gen_vector_loop_niters (loop_vec_info, tree, tree *,
1504 					 tree *, bool);
1505 extern tree vect_halve_mask_nunits (tree);
1506 extern tree vect_double_mask_nunits (tree);
1507 extern void vect_record_loop_mask (loop_vec_info, vec_loop_masks *,
1508 				   unsigned int, tree);
1509 extern tree vect_get_loop_mask (gimple_stmt_iterator *, vec_loop_masks *,
1510 				unsigned int, tree, unsigned int);
1511 
1512 /* Drive for loop transformation stage.  */
1513 extern struct loop *vect_transform_loop (loop_vec_info);
1514 extern loop_vec_info vect_analyze_loop_form (struct loop *);
1515 extern bool vectorizable_live_operation (gimple *, gimple_stmt_iterator *,
1516 					 slp_tree, int, gimple **);
1517 extern bool vectorizable_reduction (gimple *, gimple_stmt_iterator *,
1518 				    gimple **, slp_tree, slp_instance);
1519 extern bool vectorizable_induction (gimple *, gimple_stmt_iterator *,
1520 				    gimple **, slp_tree);
1521 extern tree get_initial_def_for_reduction (gimple *, tree, tree *);
1522 extern bool vect_worthwhile_without_simd_p (vec_info *, tree_code);
1523 extern int vect_get_known_peeling_cost (loop_vec_info, int, int *,
1524 					stmt_vector_for_cost *,
1525 					stmt_vector_for_cost *,
1526 					stmt_vector_for_cost *);
1527 extern tree cse_and_gimplify_to_preheader (loop_vec_info, tree);
1528 
1529 /* In tree-vect-slp.c.  */
1530 extern void vect_free_slp_instance (slp_instance);
1531 extern bool vect_transform_slp_perm_load (slp_tree, vec<tree> ,
1532 					  gimple_stmt_iterator *, poly_uint64,
1533 					  slp_instance, bool, unsigned *);
1534 extern bool vect_slp_analyze_operations (vec_info *);
1535 extern bool vect_schedule_slp (vec_info *);
1536 extern bool vect_analyze_slp (vec_info *, unsigned);
1537 extern bool vect_make_slp_decision (loop_vec_info);
1538 extern void vect_detect_hybrid_slp (loop_vec_info);
1539 extern void vect_get_slp_defs (vec<tree> , slp_tree, vec<vec<tree> > *);
1540 extern bool vect_slp_bb (basic_block);
1541 extern gimple *vect_find_last_scalar_stmt_in_slp (slp_tree);
1542 extern bool is_simple_and_all_uses_invariant (gimple *, loop_vec_info);
1543 extern bool can_duplicate_and_interleave_p (unsigned int, machine_mode,
1544 					    unsigned int * = NULL,
1545 					    tree * = NULL, tree * = NULL);
1546 extern void duplicate_and_interleave (gimple_seq *, tree, vec<tree>,
1547 				      unsigned int, vec<tree> &);
1548 extern int vect_get_place_in_interleaving_chain (gimple *, gimple *);
1549 
1550 /* In tree-vect-patterns.c.  */
1551 /* Pattern recognition functions.
1552    Additional pattern recognition functions can (and will) be added
1553    in the future.  */
1554 typedef gimple *(* vect_recog_func_ptr) (vec<gimple *> *, tree *, tree *);
1555 #define NUM_PATTERNS 15
1556 void vect_pattern_recog (vec_info *);
1557 
1558 /* In tree-vectorizer.c.  */
1559 unsigned vectorize_loops (void);
1560 bool vect_stmt_in_region_p (vec_info *, gimple *);
1561 void vect_free_loop_info_assumptions (struct loop *);
1562 
1563 #endif  /* GCC_TREE_VECTORIZER_H  */
1564