1465070a0066f8d8b1809a69d736dfa1e41ce73f
[gcc.git] / gcc / tree-vectorizer.h
1 /* Vectorizer
2 Copyright (C) 2003-2016 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 "target.h"
26
27 /* Used for naming of new temporaries. */
28 enum vect_var_kind {
29 vect_simple_var,
30 vect_pointer_var,
31 vect_scalar_var,
32 vect_mask_var
33 };
34
35 /* Defines type of operation. */
36 enum operation_type {
37 unary_op = 1,
38 binary_op,
39 ternary_op
40 };
41
42 /* Define type of available alignment support. */
43 enum dr_alignment_support {
44 dr_unaligned_unsupported,
45 dr_unaligned_supported,
46 dr_explicit_realign,
47 dr_explicit_realign_optimized,
48 dr_aligned
49 };
50
51 /* Define type of def-use cross-iteration cycle. */
52 enum vect_def_type {
53 vect_uninitialized_def = 0,
54 vect_constant_def = 1,
55 vect_external_def,
56 vect_internal_def,
57 vect_induction_def,
58 vect_reduction_def,
59 vect_double_reduction_def,
60 vect_nested_cycle,
61 vect_unknown_def_type
62 };
63
64 /* Define type of reduction. */
65 enum vect_reduction_type {
66 TREE_CODE_REDUCTION,
67 COND_REDUCTION,
68 INTEGER_INDUC_COND_REDUCTION
69 };
70
71 #define VECTORIZABLE_CYCLE_DEF(D) (((D) == vect_reduction_def) \
72 || ((D) == vect_double_reduction_def) \
73 || ((D) == vect_nested_cycle))
74
75 /* Structure to encapsulate information about a group of like
76 instructions to be presented to the target cost model. */
77 struct stmt_info_for_cost {
78 int count;
79 enum vect_cost_for_stmt kind;
80 gimple *stmt;
81 int misalign;
82 };
83
84 typedef vec<stmt_info_for_cost> stmt_vector_for_cost;
85
86 /************************************************************************
87 SLP
88 ************************************************************************/
89 typedef struct _slp_tree *slp_tree;
90
91 /* A computation tree of an SLP instance. Each node corresponds to a group of
92 stmts to be packed in a SIMD stmt. */
93 struct _slp_tree {
94 /* Nodes that contain def-stmts of this node statements operands. */
95 vec<slp_tree> children;
96 /* A group of scalar stmts to be vectorized together. */
97 vec<gimple *> stmts;
98 /* Load permutation relative to the stores, NULL if there is no
99 permutation. */
100 vec<unsigned> load_permutation;
101 /* Vectorized stmt/s. */
102 vec<gimple *> vec_stmts;
103 /* Number of vector stmts that are created to replace the group of scalar
104 stmts. It is calculated during the transformation phase as the number of
105 scalar elements in one scalar iteration (GROUP_SIZE) multiplied by VF
106 divided by vector size. */
107 unsigned int vec_stmts_size;
108 /* Whether the scalar computations use two different operators. */
109 bool two_operators;
110 /* The DEF type of this node. */
111 enum vect_def_type def_type;
112 };
113
114
115 /* SLP instance is a sequence of stmts in a loop that can be packed into
116 SIMD stmts. */
117 typedef struct _slp_instance {
118 /* The root of SLP tree. */
119 slp_tree root;
120
121 /* Size of groups of scalar stmts that will be replaced by SIMD stmt/s. */
122 unsigned int group_size;
123
124 /* The unrolling factor required to vectorized this SLP instance. */
125 unsigned int unrolling_factor;
126
127 /* The group of nodes that contain loads of this SLP instance. */
128 vec<slp_tree> loads;
129 } *slp_instance;
130
131
132 /* Access Functions. */
133 #define SLP_INSTANCE_TREE(S) (S)->root
134 #define SLP_INSTANCE_GROUP_SIZE(S) (S)->group_size
135 #define SLP_INSTANCE_UNROLLING_FACTOR(S) (S)->unrolling_factor
136 #define SLP_INSTANCE_LOADS(S) (S)->loads
137
138 #define SLP_TREE_CHILDREN(S) (S)->children
139 #define SLP_TREE_SCALAR_STMTS(S) (S)->stmts
140 #define SLP_TREE_VEC_STMTS(S) (S)->vec_stmts
141 #define SLP_TREE_NUMBER_OF_VEC_STMTS(S) (S)->vec_stmts_size
142 #define SLP_TREE_LOAD_PERMUTATION(S) (S)->load_permutation
143 #define SLP_TREE_TWO_OPERATORS(S) (S)->two_operators
144 #define SLP_TREE_DEF_TYPE(S) (S)->def_type
145
146
147
148 /* This struct is used to store the information of a data reference,
149 including the data ref itself, the access offset (calculated by summing its
150 offset and init) and the segment length for aliasing checks.
151 This is used to merge alias checks. */
152
153 struct dr_with_seg_len
154 {
155 dr_with_seg_len (data_reference_p d, tree len)
156 : dr (d),
157 offset (size_binop (PLUS_EXPR, DR_OFFSET (d), DR_INIT (d))),
158 seg_len (len) {}
159
160 data_reference_p dr;
161 tree offset;
162 tree seg_len;
163 };
164
165 /* This struct contains two dr_with_seg_len objects with aliasing data
166 refs. Two comparisons are generated from them. */
167
168 struct dr_with_seg_len_pair_t
169 {
170 dr_with_seg_len_pair_t (const dr_with_seg_len& d1,
171 const dr_with_seg_len& d2)
172 : first (d1), second (d2) {}
173
174 dr_with_seg_len first;
175 dr_with_seg_len second;
176 };
177
178
179
180 /* Vectorizer state common between loop and basic-block vectorization. */
181 struct vec_info {
182 enum { bb, loop } kind;
183
184 /* All SLP instances. */
185 vec<slp_instance> slp_instances;
186
187 /* All data references. */
188 vec<data_reference_p> datarefs;
189
190 /* All data dependences. */
191 vec<ddr_p> ddrs;
192
193 /* All interleaving chains of stores, represented by the first
194 stmt in the chain. */
195 vec<gimple *> grouped_stores;
196
197 /* Cost data used by the target cost model. */
198 void *target_cost_data;
199 };
200
201 struct _loop_vec_info;
202 struct _bb_vec_info;
203
204 template<>
205 template<>
206 inline bool
207 is_a_helper <_loop_vec_info *>::test (vec_info *i)
208 {
209 return i->kind == vec_info::loop;
210 }
211
212 template<>
213 template<>
214 inline bool
215 is_a_helper <_bb_vec_info *>::test (vec_info *i)
216 {
217 return i->kind == vec_info::bb;
218 }
219
220
221 /*-----------------------------------------------------------------*/
222 /* Info on vectorized loops. */
223 /*-----------------------------------------------------------------*/
224 typedef struct _loop_vec_info : public vec_info {
225
226 /* The loop to which this info struct refers to. */
227 struct loop *loop;
228
229 /* The loop basic blocks. */
230 basic_block *bbs;
231
232 /* Number of latch executions. */
233 tree num_itersm1;
234 /* Number of iterations. */
235 tree num_iters;
236 /* Number of iterations of the original loop. */
237 tree num_iters_unchanged;
238
239 /* Threshold of number of iterations below which vectorzation will not be
240 performed. It is calculated from MIN_PROFITABLE_ITERS and
241 PARAM_MIN_VECT_LOOP_BOUND. */
242 unsigned int th;
243
244 /* Is the loop vectorizable? */
245 bool vectorizable;
246
247 /* Unrolling factor */
248 int vectorization_factor;
249
250 /* Unknown DRs according to which loop was peeled. */
251 struct data_reference *unaligned_dr;
252
253 /* peeling_for_alignment indicates whether peeling for alignment will take
254 place, and what the peeling factor should be:
255 peeling_for_alignment = X means:
256 If X=0: Peeling for alignment will not be applied.
257 If X>0: Peel first X iterations.
258 If X=-1: Generate a runtime test to calculate the number of iterations
259 to be peeled, using the dataref recorded in the field
260 unaligned_dr. */
261 int peeling_for_alignment;
262
263 /* The mask used to check the alignment of pointers or arrays. */
264 int ptr_mask;
265
266 /* The loop nest in which the data dependences are computed. */
267 vec<loop_p> loop_nest;
268
269 /* Data Dependence Relations defining address ranges that are candidates
270 for a run-time aliasing check. */
271 vec<ddr_p> may_alias_ddrs;
272
273 /* Data Dependence Relations defining address ranges together with segment
274 lengths from which the run-time aliasing check is built. */
275 vec<dr_with_seg_len_pair_t> comp_alias_ddrs;
276
277 /* Statements in the loop that have data references that are candidates for a
278 runtime (loop versioning) misalignment check. */
279 vec<gimple *> may_misalign_stmts;
280
281 /* The unrolling factor needed to SLP the loop. In case of that pure SLP is
282 applied to the loop, i.e., no unrolling is needed, this is 1. */
283 unsigned slp_unrolling_factor;
284
285 /* Reduction cycles detected in the loop. Used in loop-aware SLP. */
286 vec<gimple *> reductions;
287
288 /* All reduction chains in the loop, represented by the first
289 stmt in the chain. */
290 vec<gimple *> reduction_chains;
291
292 /* Cost vector for a single scalar iteration. */
293 vec<stmt_info_for_cost> scalar_cost_vec;
294
295 /* Cost of a single scalar iteration. */
296 int single_scalar_iteration_cost;
297
298 /* When we have grouped data accesses with gaps, we may introduce invalid
299 memory accesses. We peel the last iteration of the loop to prevent
300 this. */
301 bool peeling_for_gaps;
302
303 /* When the number of iterations is not a multiple of the vector size
304 we need to peel off iterations at the end to form an epilogue loop. */
305 bool peeling_for_niter;
306
307 /* Reductions are canonicalized so that the last operand is the reduction
308 operand. If this places a constant into RHS1, this decanonicalizes
309 GIMPLE for other phases, so we must track when this has occurred and
310 fix it up. */
311 bool operands_swapped;
312
313 /* True if there are no loop carried data dependencies in the loop.
314 If loop->safelen <= 1, then this is always true, either the loop
315 didn't have any loop carried data dependencies, or the loop is being
316 vectorized guarded with some runtime alias checks, or couldn't
317 be vectorized at all, but then this field shouldn't be used.
318 For loop->safelen >= 2, the user has asserted that there are no
319 backward dependencies, but there still could be loop carried forward
320 dependencies in such loops. This flag will be false if normal
321 vectorizer data dependency analysis would fail or require versioning
322 for alias, but because of loop->safelen >= 2 it has been vectorized
323 even without versioning for alias. E.g. in:
324 #pragma omp simd
325 for (int i = 0; i < m; i++)
326 a[i] = a[i + k] * c;
327 (or #pragma simd or #pragma ivdep) we can vectorize this and it will
328 DTRT even for k > 0 && k < m, but without safelen we would not
329 vectorize this, so this field would be false. */
330 bool no_data_dependencies;
331
332 /* If if-conversion versioned this loop before conversion, this is the
333 loop version without if-conversion. */
334 struct loop *scalar_loop;
335
336 } *loop_vec_info;
337
338 /* Access Functions. */
339 #define LOOP_VINFO_LOOP(L) (L)->loop
340 #define LOOP_VINFO_BBS(L) (L)->bbs
341 #define LOOP_VINFO_NITERSM1(L) (L)->num_itersm1
342 #define LOOP_VINFO_NITERS(L) (L)->num_iters
343 /* Since LOOP_VINFO_NITERS and LOOP_VINFO_NITERSM1 can change after
344 prologue peeling retain total unchanged scalar loop iterations for
345 cost model. */
346 #define LOOP_VINFO_NITERS_UNCHANGED(L) (L)->num_iters_unchanged
347 #define LOOP_VINFO_COST_MODEL_THRESHOLD(L) (L)->th
348 #define LOOP_VINFO_VECTORIZABLE_P(L) (L)->vectorizable
349 #define LOOP_VINFO_VECT_FACTOR(L) (L)->vectorization_factor
350 #define LOOP_VINFO_PTR_MASK(L) (L)->ptr_mask
351 #define LOOP_VINFO_LOOP_NEST(L) (L)->loop_nest
352 #define LOOP_VINFO_DATAREFS(L) (L)->datarefs
353 #define LOOP_VINFO_DDRS(L) (L)->ddrs
354 #define LOOP_VINFO_INT_NITERS(L) (TREE_INT_CST_LOW ((L)->num_iters))
355 #define LOOP_VINFO_PEELING_FOR_ALIGNMENT(L) (L)->peeling_for_alignment
356 #define LOOP_VINFO_UNALIGNED_DR(L) (L)->unaligned_dr
357 #define LOOP_VINFO_MAY_MISALIGN_STMTS(L) (L)->may_misalign_stmts
358 #define LOOP_VINFO_MAY_ALIAS_DDRS(L) (L)->may_alias_ddrs
359 #define LOOP_VINFO_COMP_ALIAS_DDRS(L) (L)->comp_alias_ddrs
360 #define LOOP_VINFO_GROUPED_STORES(L) (L)->grouped_stores
361 #define LOOP_VINFO_SLP_INSTANCES(L) (L)->slp_instances
362 #define LOOP_VINFO_SLP_UNROLLING_FACTOR(L) (L)->slp_unrolling_factor
363 #define LOOP_VINFO_REDUCTIONS(L) (L)->reductions
364 #define LOOP_VINFO_REDUCTION_CHAINS(L) (L)->reduction_chains
365 #define LOOP_VINFO_TARGET_COST_DATA(L) (L)->target_cost_data
366 #define LOOP_VINFO_PEELING_FOR_GAPS(L) (L)->peeling_for_gaps
367 #define LOOP_VINFO_OPERANDS_SWAPPED(L) (L)->operands_swapped
368 #define LOOP_VINFO_PEELING_FOR_NITER(L) (L)->peeling_for_niter
369 #define LOOP_VINFO_NO_DATA_DEPENDENCIES(L) (L)->no_data_dependencies
370 #define LOOP_VINFO_SCALAR_LOOP(L) (L)->scalar_loop
371 #define LOOP_VINFO_SCALAR_ITERATION_COST(L) (L)->scalar_cost_vec
372 #define LOOP_VINFO_SINGLE_SCALAR_ITERATION_COST(L) (L)->single_scalar_iteration_cost
373
374 #define LOOP_REQUIRES_VERSIONING_FOR_ALIGNMENT(L) \
375 ((L)->may_misalign_stmts.length () > 0)
376 #define LOOP_REQUIRES_VERSIONING_FOR_ALIAS(L) \
377 ((L)->may_alias_ddrs.length () > 0)
378
379 #define LOOP_VINFO_NITERS_KNOWN_P(L) \
380 (tree_fits_shwi_p ((L)->num_iters) && tree_to_shwi ((L)->num_iters) > 0)
381
382 static inline loop_vec_info
383 loop_vec_info_for_loop (struct loop *loop)
384 {
385 return (loop_vec_info) loop->aux;
386 }
387
388 static inline bool
389 nested_in_vect_loop_p (struct loop *loop, gimple *stmt)
390 {
391 return (loop->inner
392 && (loop->inner == (gimple_bb (stmt))->loop_father));
393 }
394
395 typedef struct _bb_vec_info : public vec_info
396 {
397 basic_block bb;
398 gimple_stmt_iterator region_begin;
399 gimple_stmt_iterator region_end;
400 } *bb_vec_info;
401
402 #define BB_VINFO_BB(B) (B)->bb
403 #define BB_VINFO_GROUPED_STORES(B) (B)->grouped_stores
404 #define BB_VINFO_SLP_INSTANCES(B) (B)->slp_instances
405 #define BB_VINFO_DATAREFS(B) (B)->datarefs
406 #define BB_VINFO_DDRS(B) (B)->ddrs
407 #define BB_VINFO_TARGET_COST_DATA(B) (B)->target_cost_data
408
409 static inline bb_vec_info
410 vec_info_for_bb (basic_block bb)
411 {
412 return (bb_vec_info) bb->aux;
413 }
414
415 /*-----------------------------------------------------------------*/
416 /* Info on vectorized defs. */
417 /*-----------------------------------------------------------------*/
418 enum stmt_vec_info_type {
419 undef_vec_info_type = 0,
420 load_vec_info_type,
421 store_vec_info_type,
422 shift_vec_info_type,
423 op_vec_info_type,
424 call_vec_info_type,
425 call_simd_clone_vec_info_type,
426 assignment_vec_info_type,
427 condition_vec_info_type,
428 comparison_vec_info_type,
429 reduc_vec_info_type,
430 induc_vec_info_type,
431 type_promotion_vec_info_type,
432 type_demotion_vec_info_type,
433 type_conversion_vec_info_type,
434 loop_exit_ctrl_vec_info_type
435 };
436
437 /* Indicates whether/how a variable is used in the scope of loop/basic
438 block. */
439 enum vect_relevant {
440 vect_unused_in_scope = 0,
441 /* The def is in the inner loop, and the use is in the outer loop, and the
442 use is a reduction stmt. */
443 vect_used_in_outer_by_reduction,
444 /* The def is in the inner loop, and the use is in the outer loop (and is
445 not part of reduction). */
446 vect_used_in_outer,
447
448 /* defs that feed computations that end up (only) in a reduction. These
449 defs may be used by non-reduction stmts, but eventually, any
450 computations/values that are affected by these defs are used to compute
451 a reduction (i.e. don't get stored to memory, for example). We use this
452 to identify computations that we can change the order in which they are
453 computed. */
454 vect_used_by_reduction,
455
456 vect_used_in_scope
457 };
458
459 /* The type of vectorization that can be applied to the stmt: regular loop-based
460 vectorization; pure SLP - the stmt is a part of SLP instances and does not
461 have uses outside SLP instances; or hybrid SLP and loop-based - the stmt is
462 a part of SLP instance and also must be loop-based vectorized, since it has
463 uses outside SLP sequences.
464
465 In the loop context the meanings of pure and hybrid SLP are slightly
466 different. By saying that pure SLP is applied to the loop, we mean that we
467 exploit only intra-iteration parallelism in the loop; i.e., the loop can be
468 vectorized without doing any conceptual unrolling, cause we don't pack
469 together stmts from different iterations, only within a single iteration.
470 Loop hybrid SLP means that we exploit both intra-iteration and
471 inter-iteration parallelism (e.g., number of elements in the vector is 4
472 and the slp-group-size is 2, in which case we don't have enough parallelism
473 within an iteration, so we obtain the rest of the parallelism from subsequent
474 iterations by unrolling the loop by 2). */
475 enum slp_vect_type {
476 loop_vect = 0,
477 pure_slp,
478 hybrid
479 };
480
481
482 typedef struct data_reference *dr_p;
483
484 typedef struct _stmt_vec_info {
485
486 enum stmt_vec_info_type type;
487
488 /* Indicates whether this stmts is part of a computation whose result is
489 used outside the loop. */
490 bool live;
491
492 /* Stmt is part of some pattern (computation idiom) */
493 bool in_pattern_p;
494
495 /* The stmt to which this info struct refers to. */
496 gimple *stmt;
497
498 /* The vec_info with respect to which STMT is vectorized. */
499 vec_info *vinfo;
500
501 /* The vector type to be used for the LHS of this statement. */
502 tree vectype;
503
504 /* The vectorized version of the stmt. */
505 gimple *vectorized_stmt;
506
507
508 /** The following is relevant only for stmts that contain a non-scalar
509 data-ref (array/pointer/struct access). A GIMPLE stmt is expected to have
510 at most one such data-ref. **/
511
512 /* Information about the data-ref (access function, etc),
513 relative to the inner-most containing loop. */
514 struct data_reference *data_ref_info;
515
516 /* Information about the data-ref relative to this loop
517 nest (the loop that is being considered for vectorization). */
518 tree dr_base_address;
519 tree dr_init;
520 tree dr_offset;
521 tree dr_step;
522 tree dr_aligned_to;
523
524 /* For loop PHI nodes, the base and evolution part of it. This makes sure
525 this information is still available in vect_update_ivs_after_vectorizer
526 where we may not be able to re-analyze the PHI nodes evolution as
527 peeling for the prologue loop can make it unanalyzable. The evolution
528 part is still correct after peeling, but the base may have changed from
529 the version here. */
530 tree loop_phi_evolution_base_unchanged;
531 tree loop_phi_evolution_part;
532
533 /* Used for various bookkeeping purposes, generally holding a pointer to
534 some other stmt S that is in some way "related" to this stmt.
535 Current use of this field is:
536 If this stmt is part of a pattern (i.e. the field 'in_pattern_p' is
537 true): S is the "pattern stmt" that represents (and replaces) the
538 sequence of stmts that constitutes the pattern. Similarly, the
539 related_stmt of the "pattern stmt" points back to this stmt (which is
540 the last stmt in the original sequence of stmts that constitutes the
541 pattern). */
542 gimple *related_stmt;
543
544 /* Used to keep a sequence of def stmts of a pattern stmt if such exists. */
545 gimple_seq pattern_def_seq;
546
547 /* List of datarefs that are known to have the same alignment as the dataref
548 of this stmt. */
549 vec<dr_p> same_align_refs;
550
551 /* Selected SIMD clone's function info. First vector element
552 is SIMD clone's function decl, followed by a pair of trees (base + step)
553 for linear arguments (pair of NULLs for other arguments). */
554 vec<tree> simd_clone_info;
555
556 /* Classify the def of this stmt. */
557 enum vect_def_type def_type;
558
559 /* Whether the stmt is SLPed, loop-based vectorized, or both. */
560 enum slp_vect_type slp_type;
561
562 /* Interleaving and reduction chains info. */
563 /* First element in the group. */
564 gimple *first_element;
565 /* Pointer to the next element in the group. */
566 gimple *next_element;
567 /* For data-refs, in case that two or more stmts share data-ref, this is the
568 pointer to the previously detected stmt with the same dr. */
569 gimple *same_dr_stmt;
570 /* The size of the group. */
571 unsigned int size;
572 /* For stores, number of stores from this group seen. We vectorize the last
573 one. */
574 unsigned int store_count;
575 /* For loads only, the gap from the previous load. For consecutive loads, GAP
576 is 1. */
577 unsigned int gap;
578
579 /* The minimum negative dependence distance this stmt participates in
580 or zero if none. */
581 unsigned int min_neg_dist;
582
583 /* Not all stmts in the loop need to be vectorized. e.g, the increment
584 of the loop induction variable and computation of array indexes. relevant
585 indicates whether the stmt needs to be vectorized. */
586 enum vect_relevant relevant;
587
588 /* Is this statement vectorizable or should it be skipped in (partial)
589 vectorization. */
590 bool vectorizable;
591
592 /* For loads if this is a gather, for stores if this is a scatter. */
593 bool gather_scatter_p;
594
595 /* True if this is an access with loop-invariant stride. */
596 bool strided_p;
597
598 /* For both loads and stores. */
599 bool simd_lane_access_p;
600
601 /* For reduction loops, this is the type of reduction. */
602 enum vect_reduction_type v_reduc_type;
603
604 } *stmt_vec_info;
605
606 /* Access Functions. */
607 #define STMT_VINFO_TYPE(S) (S)->type
608 #define STMT_VINFO_STMT(S) (S)->stmt
609 inline loop_vec_info
610 STMT_VINFO_LOOP_VINFO (stmt_vec_info stmt_vinfo)
611 {
612 if (loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (stmt_vinfo->vinfo))
613 return loop_vinfo;
614 return NULL;
615 }
616 inline bb_vec_info
617 STMT_VINFO_BB_VINFO (stmt_vec_info stmt_vinfo)
618 {
619 if (bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (stmt_vinfo->vinfo))
620 return bb_vinfo;
621 return NULL;
622 }
623 #define STMT_VINFO_RELEVANT(S) (S)->relevant
624 #define STMT_VINFO_LIVE_P(S) (S)->live
625 #define STMT_VINFO_VECTYPE(S) (S)->vectype
626 #define STMT_VINFO_VEC_STMT(S) (S)->vectorized_stmt
627 #define STMT_VINFO_VECTORIZABLE(S) (S)->vectorizable
628 #define STMT_VINFO_DATA_REF(S) (S)->data_ref_info
629 #define STMT_VINFO_GATHER_SCATTER_P(S) (S)->gather_scatter_p
630 #define STMT_VINFO_STRIDED_P(S) (S)->strided_p
631 #define STMT_VINFO_SIMD_LANE_ACCESS_P(S) (S)->simd_lane_access_p
632 #define STMT_VINFO_VEC_REDUCTION_TYPE(S) (S)->v_reduc_type
633
634 #define STMT_VINFO_DR_BASE_ADDRESS(S) (S)->dr_base_address
635 #define STMT_VINFO_DR_INIT(S) (S)->dr_init
636 #define STMT_VINFO_DR_OFFSET(S) (S)->dr_offset
637 #define STMT_VINFO_DR_STEP(S) (S)->dr_step
638 #define STMT_VINFO_DR_ALIGNED_TO(S) (S)->dr_aligned_to
639
640 #define STMT_VINFO_IN_PATTERN_P(S) (S)->in_pattern_p
641 #define STMT_VINFO_RELATED_STMT(S) (S)->related_stmt
642 #define STMT_VINFO_PATTERN_DEF_SEQ(S) (S)->pattern_def_seq
643 #define STMT_VINFO_SAME_ALIGN_REFS(S) (S)->same_align_refs
644 #define STMT_VINFO_SIMD_CLONE_INFO(S) (S)->simd_clone_info
645 #define STMT_VINFO_DEF_TYPE(S) (S)->def_type
646 #define STMT_VINFO_GROUP_FIRST_ELEMENT(S) (S)->first_element
647 #define STMT_VINFO_GROUP_NEXT_ELEMENT(S) (S)->next_element
648 #define STMT_VINFO_GROUP_SIZE(S) (S)->size
649 #define STMT_VINFO_GROUP_STORE_COUNT(S) (S)->store_count
650 #define STMT_VINFO_GROUP_GAP(S) (S)->gap
651 #define STMT_VINFO_GROUP_SAME_DR_STMT(S) (S)->same_dr_stmt
652 #define STMT_VINFO_GROUPED_ACCESS(S) ((S)->first_element != NULL && (S)->data_ref_info)
653 #define STMT_VINFO_LOOP_PHI_EVOLUTION_BASE_UNCHANGED(S) (S)->loop_phi_evolution_base_unchanged
654 #define STMT_VINFO_LOOP_PHI_EVOLUTION_PART(S) (S)->loop_phi_evolution_part
655 #define STMT_VINFO_MIN_NEG_DIST(S) (S)->min_neg_dist
656
657 #define GROUP_FIRST_ELEMENT(S) (S)->first_element
658 #define GROUP_NEXT_ELEMENT(S) (S)->next_element
659 #define GROUP_SIZE(S) (S)->size
660 #define GROUP_STORE_COUNT(S) (S)->store_count
661 #define GROUP_GAP(S) (S)->gap
662 #define GROUP_SAME_DR_STMT(S) (S)->same_dr_stmt
663
664 #define STMT_VINFO_RELEVANT_P(S) ((S)->relevant != vect_unused_in_scope)
665
666 #define HYBRID_SLP_STMT(S) ((S)->slp_type == hybrid)
667 #define PURE_SLP_STMT(S) ((S)->slp_type == pure_slp)
668 #define STMT_SLP_TYPE(S) (S)->slp_type
669
670 struct dataref_aux {
671 int misalignment;
672 /* If true the alignment of base_decl needs to be increased. */
673 bool base_misaligned;
674 /* If true we know the base is at least vector element alignment aligned. */
675 bool base_element_aligned;
676 tree base_decl;
677 };
678
679 #define DR_VECT_AUX(dr) ((dataref_aux *)(dr)->aux)
680
681 #define VECT_MAX_COST 1000
682
683 /* The maximum number of intermediate steps required in multi-step type
684 conversion. */
685 #define MAX_INTERM_CVT_STEPS 3
686
687 /* The maximum vectorization factor supported by any target (V64QI). */
688 #define MAX_VECTORIZATION_FACTOR 64
689
690 extern vec<stmt_vec_info> stmt_vec_info_vec;
691
692 void init_stmt_vec_info_vec (void);
693 void free_stmt_vec_info_vec (void);
694
695 /* Return a stmt_vec_info corresponding to STMT. */
696
697 static inline stmt_vec_info
698 vinfo_for_stmt (gimple *stmt)
699 {
700 unsigned int uid = gimple_uid (stmt);
701 if (uid == 0)
702 return NULL;
703
704 return stmt_vec_info_vec[uid - 1];
705 }
706
707 /* Set vectorizer information INFO for STMT. */
708
709 static inline void
710 set_vinfo_for_stmt (gimple *stmt, stmt_vec_info info)
711 {
712 unsigned int uid = gimple_uid (stmt);
713 if (uid == 0)
714 {
715 gcc_checking_assert (info);
716 uid = stmt_vec_info_vec.length () + 1;
717 gimple_set_uid (stmt, uid);
718 stmt_vec_info_vec.safe_push (info);
719 }
720 else
721 {
722 gcc_checking_assert (info == NULL);
723 stmt_vec_info_vec[uid - 1] = info;
724 }
725 }
726
727 /* Return the earlier statement between STMT1 and STMT2. */
728
729 static inline gimple *
730 get_earlier_stmt (gimple *stmt1, gimple *stmt2)
731 {
732 unsigned int uid1, uid2;
733
734 if (stmt1 == NULL)
735 return stmt2;
736
737 if (stmt2 == NULL)
738 return stmt1;
739
740 uid1 = gimple_uid (stmt1);
741 uid2 = gimple_uid (stmt2);
742
743 if (uid1 == 0 || uid2 == 0)
744 return NULL;
745
746 gcc_checking_assert (uid1 <= stmt_vec_info_vec.length ()
747 && uid2 <= stmt_vec_info_vec.length ());
748
749 if (uid1 < uid2)
750 return stmt1;
751 else
752 return stmt2;
753 }
754
755 /* Return the later statement between STMT1 and STMT2. */
756
757 static inline gimple *
758 get_later_stmt (gimple *stmt1, gimple *stmt2)
759 {
760 unsigned int uid1, uid2;
761
762 if (stmt1 == NULL)
763 return stmt2;
764
765 if (stmt2 == NULL)
766 return stmt1;
767
768 uid1 = gimple_uid (stmt1);
769 uid2 = gimple_uid (stmt2);
770
771 if (uid1 == 0 || uid2 == 0)
772 return NULL;
773
774 gcc_assert (uid1 <= stmt_vec_info_vec.length ());
775 gcc_assert (uid2 <= stmt_vec_info_vec.length ());
776
777 if (uid1 > uid2)
778 return stmt1;
779 else
780 return stmt2;
781 }
782
783 /* Return TRUE if a statement represented by STMT_INFO is a part of a
784 pattern. */
785
786 static inline bool
787 is_pattern_stmt_p (stmt_vec_info stmt_info)
788 {
789 gimple *related_stmt;
790 stmt_vec_info related_stmt_info;
791
792 related_stmt = STMT_VINFO_RELATED_STMT (stmt_info);
793 if (related_stmt
794 && (related_stmt_info = vinfo_for_stmt (related_stmt))
795 && STMT_VINFO_IN_PATTERN_P (related_stmt_info))
796 return true;
797
798 return false;
799 }
800
801 /* Return true if BB is a loop header. */
802
803 static inline bool
804 is_loop_header_bb_p (basic_block bb)
805 {
806 if (bb == (bb->loop_father)->header)
807 return true;
808 gcc_checking_assert (EDGE_COUNT (bb->preds) == 1);
809 return false;
810 }
811
812 /* Return pow2 (X). */
813
814 static inline int
815 vect_pow2 (int x)
816 {
817 int i, res = 1;
818
819 for (i = 0; i < x; i++)
820 res *= 2;
821
822 return res;
823 }
824
825 /* Alias targetm.vectorize.builtin_vectorization_cost. */
826
827 static inline int
828 builtin_vectorization_cost (enum vect_cost_for_stmt type_of_cost,
829 tree vectype, int misalign)
830 {
831 return targetm.vectorize.builtin_vectorization_cost (type_of_cost,
832 vectype, misalign);
833 }
834
835 /* Get cost by calling cost target builtin. */
836
837 static inline
838 int vect_get_stmt_cost (enum vect_cost_for_stmt type_of_cost)
839 {
840 return builtin_vectorization_cost (type_of_cost, NULL, 0);
841 }
842
843 /* Alias targetm.vectorize.init_cost. */
844
845 static inline void *
846 init_cost (struct loop *loop_info)
847 {
848 return targetm.vectorize.init_cost (loop_info);
849 }
850
851 /* Alias targetm.vectorize.add_stmt_cost. */
852
853 static inline unsigned
854 add_stmt_cost (void *data, int count, enum vect_cost_for_stmt kind,
855 stmt_vec_info stmt_info, int misalign,
856 enum vect_cost_model_location where)
857 {
858 return targetm.vectorize.add_stmt_cost (data, count, kind,
859 stmt_info, misalign, where);
860 }
861
862 /* Alias targetm.vectorize.finish_cost. */
863
864 static inline void
865 finish_cost (void *data, unsigned *prologue_cost,
866 unsigned *body_cost, unsigned *epilogue_cost)
867 {
868 targetm.vectorize.finish_cost (data, prologue_cost, body_cost, epilogue_cost);
869 }
870
871 /* Alias targetm.vectorize.destroy_cost_data. */
872
873 static inline void
874 destroy_cost_data (void *data)
875 {
876 targetm.vectorize.destroy_cost_data (data);
877 }
878
879 /*-----------------------------------------------------------------*/
880 /* Info on data references alignment. */
881 /*-----------------------------------------------------------------*/
882 inline void
883 set_dr_misalignment (struct data_reference *dr, int val)
884 {
885 dataref_aux *data_aux = DR_VECT_AUX (dr);
886
887 if (!data_aux)
888 {
889 data_aux = XCNEW (dataref_aux);
890 dr->aux = data_aux;
891 }
892
893 data_aux->misalignment = val;
894 }
895
896 inline int
897 dr_misalignment (struct data_reference *dr)
898 {
899 return DR_VECT_AUX (dr)->misalignment;
900 }
901
902 /* Reflects actual alignment of first access in the vectorized loop,
903 taking into account peeling/versioning if applied. */
904 #define DR_MISALIGNMENT(DR) dr_misalignment (DR)
905 #define SET_DR_MISALIGNMENT(DR, VAL) set_dr_misalignment (DR, VAL)
906
907 /* Return TRUE if the data access is aligned, and FALSE otherwise. */
908
909 static inline bool
910 aligned_access_p (struct data_reference *data_ref_info)
911 {
912 return (DR_MISALIGNMENT (data_ref_info) == 0);
913 }
914
915 /* Return TRUE if the alignment of the data access is known, and FALSE
916 otherwise. */
917
918 static inline bool
919 known_alignment_for_access_p (struct data_reference *data_ref_info)
920 {
921 return (DR_MISALIGNMENT (data_ref_info) != -1);
922 }
923
924
925 /* Return true if the vect cost model is unlimited. */
926 static inline bool
927 unlimited_cost_model (loop_p loop)
928 {
929 if (loop != NULL && loop->force_vectorize
930 && flag_simd_cost_model != VECT_COST_MODEL_DEFAULT)
931 return flag_simd_cost_model == VECT_COST_MODEL_UNLIMITED;
932 return (flag_vect_cost_model == VECT_COST_MODEL_UNLIMITED);
933 }
934
935 /* Source location */
936 extern source_location vect_location;
937
938 /*-----------------------------------------------------------------*/
939 /* Function prototypes. */
940 /*-----------------------------------------------------------------*/
941
942 /* Simple loop peeling and versioning utilities for vectorizer's purposes -
943 in tree-vect-loop-manip.c. */
944 extern void slpeel_make_loop_iterate_ntimes (struct loop *, tree);
945 extern bool slpeel_can_duplicate_loop_p (const struct loop *, const_edge);
946 struct loop *slpeel_tree_duplicate_loop_to_edge_cfg (struct loop *,
947 struct loop *, edge);
948 extern void vect_loop_versioning (loop_vec_info, unsigned int, bool);
949 extern void vect_do_peeling_for_loop_bound (loop_vec_info, tree, tree,
950 unsigned int, bool);
951 extern void vect_do_peeling_for_alignment (loop_vec_info, tree,
952 unsigned int, bool);
953 extern source_location find_loop_location (struct loop *);
954 extern bool vect_can_advance_ivs_p (loop_vec_info);
955
956 /* In tree-vect-stmts.c. */
957 extern unsigned int current_vector_size;
958 extern tree get_vectype_for_scalar_type (tree);
959 extern tree get_mask_type_for_scalar_type (tree);
960 extern tree get_same_sized_vectype (tree, tree);
961 extern bool vect_is_simple_use (tree, vec_info *, gimple **,
962 enum vect_def_type *);
963 extern bool vect_is_simple_use (tree, vec_info *, gimple **,
964 enum vect_def_type *, tree *);
965 extern bool supportable_widening_operation (enum tree_code, gimple *, tree,
966 tree, enum tree_code *,
967 enum tree_code *, int *,
968 vec<tree> *);
969 extern bool supportable_narrowing_operation (enum tree_code, tree, tree,
970 enum tree_code *,
971 int *, vec<tree> *);
972 extern stmt_vec_info new_stmt_vec_info (gimple *stmt, vec_info *);
973 extern void free_stmt_vec_info (gimple *stmt);
974 extern void vect_model_simple_cost (stmt_vec_info, int, enum vect_def_type *,
975 stmt_vector_for_cost *,
976 stmt_vector_for_cost *);
977 extern void vect_model_store_cost (stmt_vec_info, int, bool,
978 enum vect_def_type, slp_tree,
979 stmt_vector_for_cost *,
980 stmt_vector_for_cost *);
981 extern void vect_model_load_cost (stmt_vec_info, int, bool, slp_tree,
982 stmt_vector_for_cost *,
983 stmt_vector_for_cost *);
984 extern unsigned record_stmt_cost (stmt_vector_for_cost *, int,
985 enum vect_cost_for_stmt, stmt_vec_info,
986 int, enum vect_cost_model_location);
987 extern void vect_finish_stmt_generation (gimple *, gimple *,
988 gimple_stmt_iterator *);
989 extern bool vect_mark_stmts_to_be_vectorized (loop_vec_info);
990 extern tree vect_get_vec_def_for_operand (tree, gimple *, tree = NULL);
991 extern tree vect_init_vector (gimple *, tree, tree,
992 gimple_stmt_iterator *);
993 extern tree vect_get_vec_def_for_stmt_copy (enum vect_def_type, tree);
994 extern bool vect_transform_stmt (gimple *, gimple_stmt_iterator *,
995 bool *, slp_tree, slp_instance);
996 extern void vect_remove_stores (gimple *);
997 extern bool vect_analyze_stmt (gimple *, bool *, slp_tree);
998 extern bool vectorizable_condition (gimple *, gimple_stmt_iterator *,
999 gimple **, tree, int, slp_tree);
1000 extern bool vectorizable_comparison (gimple *, gimple_stmt_iterator *,
1001 gimple **, tree, int, slp_tree);
1002 extern void vect_get_load_cost (struct data_reference *, int, bool,
1003 unsigned int *, unsigned int *,
1004 stmt_vector_for_cost *,
1005 stmt_vector_for_cost *, bool);
1006 extern void vect_get_store_cost (struct data_reference *, int,
1007 unsigned int *, stmt_vector_for_cost *);
1008 extern bool vect_supportable_shift (enum tree_code, tree);
1009 extern void vect_get_vec_defs (tree, tree, gimple *, vec<tree> *,
1010 vec<tree> *, slp_tree, int);
1011 extern tree vect_gen_perm_mask_any (tree, const unsigned char *);
1012 extern tree vect_gen_perm_mask_checked (tree, const unsigned char *);
1013
1014 /* In tree-vect-data-refs.c. */
1015 extern bool vect_can_force_dr_alignment_p (const_tree, unsigned int);
1016 extern enum dr_alignment_support vect_supportable_dr_alignment
1017 (struct data_reference *, bool);
1018 extern tree vect_get_smallest_scalar_type (gimple *, HOST_WIDE_INT *,
1019 HOST_WIDE_INT *);
1020 extern bool vect_analyze_data_ref_dependences (loop_vec_info, int *);
1021 extern bool vect_slp_analyze_instance_dependence (slp_instance);
1022 extern bool vect_enhance_data_refs_alignment (loop_vec_info);
1023 extern bool vect_analyze_data_refs_alignment (loop_vec_info);
1024 extern bool vect_verify_datarefs_alignment (loop_vec_info);
1025 extern bool vect_slp_analyze_and_verify_instance_alignment (slp_instance);
1026 extern bool vect_analyze_data_ref_accesses (vec_info *);
1027 extern bool vect_prune_runtime_alias_test_list (loop_vec_info);
1028 extern tree vect_check_gather_scatter (gimple *, loop_vec_info, tree *, tree *,
1029 int *);
1030 extern bool vect_analyze_data_refs (vec_info *, int *);
1031 extern tree vect_create_data_ref_ptr (gimple *, tree, struct loop *, tree,
1032 tree *, gimple_stmt_iterator *,
1033 gimple **, bool, bool *,
1034 tree = NULL_TREE);
1035 extern tree bump_vector_ptr (tree, gimple *, gimple_stmt_iterator *, gimple *,
1036 tree);
1037 extern tree vect_create_destination_var (tree, tree);
1038 extern bool vect_grouped_store_supported (tree, unsigned HOST_WIDE_INT);
1039 extern bool vect_store_lanes_supported (tree, unsigned HOST_WIDE_INT);
1040 extern bool vect_grouped_load_supported (tree, unsigned HOST_WIDE_INT);
1041 extern bool vect_load_lanes_supported (tree, unsigned HOST_WIDE_INT);
1042 extern void vect_permute_store_chain (vec<tree> ,unsigned int, gimple *,
1043 gimple_stmt_iterator *, vec<tree> *);
1044 extern tree vect_setup_realignment (gimple *, gimple_stmt_iterator *, tree *,
1045 enum dr_alignment_support, tree,
1046 struct loop **);
1047 extern void vect_transform_grouped_load (gimple *, vec<tree> , int,
1048 gimple_stmt_iterator *);
1049 extern void vect_record_grouped_load_vectors (gimple *, vec<tree> );
1050 extern tree vect_get_new_vect_var (tree, enum vect_var_kind, const char *);
1051 extern tree vect_get_new_ssa_name (tree, enum vect_var_kind,
1052 const char * = NULL);
1053 extern tree vect_create_addr_base_for_vector_ref (gimple *, gimple_seq *,
1054 tree, struct loop *,
1055 tree = NULL_TREE);
1056
1057 /* In tree-vect-loop.c. */
1058 /* FORNOW: Used in tree-parloops.c. */
1059 extern void destroy_loop_vec_info (loop_vec_info, bool);
1060 extern gimple *vect_force_simple_reduction (loop_vec_info, gimple *, bool,
1061 bool *, bool);
1062 /* Drive for loop analysis stage. */
1063 extern loop_vec_info vect_analyze_loop (struct loop *);
1064 /* Drive for loop transformation stage. */
1065 extern void vect_transform_loop (loop_vec_info);
1066 extern loop_vec_info vect_analyze_loop_form (struct loop *);
1067 extern bool vectorizable_live_operation (gimple *, gimple_stmt_iterator *,
1068 gimple **);
1069 extern bool vectorizable_reduction (gimple *, gimple_stmt_iterator *,
1070 gimple **, slp_tree);
1071 extern bool vectorizable_induction (gimple *, gimple_stmt_iterator *, gimple **);
1072 extern tree get_initial_def_for_reduction (gimple *, tree, tree *);
1073 extern int vect_min_worthwhile_factor (enum tree_code);
1074 extern int vect_get_known_peeling_cost (loop_vec_info, int, int *,
1075 stmt_vector_for_cost *,
1076 stmt_vector_for_cost *,
1077 stmt_vector_for_cost *);
1078
1079 /* In tree-vect-slp.c. */
1080 extern void vect_free_slp_instance (slp_instance);
1081 extern bool vect_transform_slp_perm_load (slp_tree, vec<tree> ,
1082 gimple_stmt_iterator *, int,
1083 slp_instance, bool);
1084 extern bool vect_slp_analyze_operations (vec<slp_instance> slp_instances,
1085 void *);
1086 extern bool vect_schedule_slp (vec_info *);
1087 extern bool vect_analyze_slp (vec_info *, unsigned);
1088 extern bool vect_make_slp_decision (loop_vec_info);
1089 extern void vect_detect_hybrid_slp (loop_vec_info);
1090 extern void vect_get_slp_defs (vec<tree> , slp_tree,
1091 vec<vec<tree> > *, int);
1092 extern bool vect_slp_bb (basic_block);
1093 extern gimple *vect_find_last_scalar_stmt_in_slp (slp_tree);
1094
1095 /* In tree-vect-patterns.c. */
1096 /* Pattern recognition functions.
1097 Additional pattern recognition functions can (and will) be added
1098 in the future. */
1099 typedef gimple *(* vect_recog_func_ptr) (vec<gimple *> *, tree *, tree *);
1100 #define NUM_PATTERNS 14
1101 void vect_pattern_recog (vec_info *);
1102
1103 /* In tree-vectorizer.c. */
1104 unsigned vectorize_loops (void);
1105 void vect_destroy_datarefs (vec_info *);
1106 bool vect_stmt_in_region_p (vec_info *, gimple *);
1107
1108 #endif /* GCC_TREE_VECTORIZER_H */