PR c++/14032
[official-gcc.git] / gcc / tree-vectorizer.h
blob1dd472c3e2d1566024764454492bbdbc248e1317
1 /* Loop Vectorization
2 Copyright (C) 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
3 Contributed by Dorit Naishlos <dorit@il.ibm.com>
5 This file is part of GCC.
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.
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.
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/>. */
21 #ifndef GCC_TREE_VECTORIZER_H
22 #define GCC_TREE_VECTORIZER_H
24 #ifdef USE_MAPPED_LOCATION
25 typedef source_location LOC;
26 #define UNKNOWN_LOC UNKNOWN_LOCATION
27 #define EXPR_LOC(e) EXPR_LOCATION(e)
28 #define LOC_FILE(l) LOCATION_FILE (l)
29 #define LOC_LINE(l) LOCATION_LINE (l)
30 #else
31 typedef source_locus LOC;
32 #define UNKNOWN_LOC NULL
33 #define EXPR_LOC(e) EXPR_LOCUS(e)
34 #define LOC_FILE(l) (l)->file
35 #define LOC_LINE(l) (l)->line
36 #endif
38 /* Used for naming of new temporaries. */
39 enum vect_var_kind {
40 vect_simple_var,
41 vect_pointer_var,
42 vect_scalar_var
45 /* Defines type of operation. */
46 enum operation_type {
47 unary_op = 1,
48 binary_op,
49 ternary_op
52 /* Define type of available alignment support. */
53 enum dr_alignment_support {
54 dr_unaligned_unsupported,
55 dr_unaligned_supported,
56 dr_explicit_realign,
57 dr_explicit_realign_optimized,
58 dr_aligned
61 /* Define type of def-use cross-iteration cycle. */
62 enum vect_def_type {
63 vect_constant_def,
64 vect_invariant_def,
65 vect_loop_def,
66 vect_induction_def,
67 vect_reduction_def,
68 vect_unknown_def_type
71 /* Define verbosity levels. */
72 enum verbosity_levels {
73 REPORT_NONE,
74 REPORT_VECTORIZED_LOOPS,
75 REPORT_UNVECTORIZED_LOOPS,
76 REPORT_ALIGNMENT,
77 REPORT_DR_DETAILS,
78 REPORT_BAD_FORM_LOOPS,
79 REPORT_OUTER_LOOPS,
80 REPORT_DETAILS,
81 /* New verbosity levels should be added before this one. */
82 MAX_VERBOSITY_LEVEL
85 /*-----------------------------------------------------------------*/
86 /* Info on vectorized loops. */
87 /*-----------------------------------------------------------------*/
88 typedef struct _loop_vec_info {
90 /* The loop to which this info struct refers to. */
91 struct loop *loop;
93 /* The loop basic blocks. */
94 basic_block *bbs;
96 /* Number of iterations. */
97 tree num_iters;
99 /* Minimum number of iterations below which vectorization is expected to
100 not be profitable (as estimated by the cost model).
101 -1 indicates that vectorization will not be profitable.
102 FORNOW: This field is an int. Will be a tree in the future, to represent
103 values unknown at compile time. */
104 int min_profitable_iters;
106 /* Is the loop vectorizable? */
107 bool vectorizable;
109 /* Unrolling factor */
110 int vectorization_factor;
112 /* Unknown DRs according to which loop was peeled. */
113 struct data_reference *unaligned_dr;
115 /* peeling_for_alignment indicates whether peeling for alignment will take
116 place, and what the peeling factor should be:
117 peeling_for_alignment = X means:
118 If X=0: Peeling for alignment will not be applied.
119 If X>0: Peel first X iterations.
120 If X=-1: Generate a runtime test to calculate the number of iterations
121 to be peeled, using the dataref recorded in the field
122 unaligned_dr. */
123 int peeling_for_alignment;
125 /* The mask used to check the alignment of pointers or arrays. */
126 int ptr_mask;
128 /* All data references in the loop. */
129 VEC (data_reference_p, heap) *datarefs;
131 /* All data dependences in the loop. */
132 VEC (ddr_p, heap) *ddrs;
134 /* Data Dependence Relations defining address ranges that are candidates
135 for a run-time aliasing check. */
136 VEC (ddr_p, heap) *may_alias_ddrs;
138 /* Statements in the loop that have data references that are candidates for a
139 runtime (loop versioning) misalignment check. */
140 VEC(tree,heap) *may_misalign_stmts;
142 /* The loop location in the source. */
143 LOC loop_line_number;
144 } *loop_vec_info;
146 /* Access Functions. */
147 #define LOOP_VINFO_LOOP(L) (L)->loop
148 #define LOOP_VINFO_BBS(L) (L)->bbs
149 #define LOOP_VINFO_NITERS(L) (L)->num_iters
150 #define LOOP_VINFO_COST_MODEL_MIN_ITERS(L) (L)->min_profitable_iters
151 #define LOOP_VINFO_VECTORIZABLE_P(L) (L)->vectorizable
152 #define LOOP_VINFO_VECT_FACTOR(L) (L)->vectorization_factor
153 #define LOOP_VINFO_PTR_MASK(L) (L)->ptr_mask
154 #define LOOP_VINFO_DATAREFS(L) (L)->datarefs
155 #define LOOP_VINFO_DDRS(L) (L)->ddrs
156 #define LOOP_VINFO_INT_NITERS(L) (TREE_INT_CST_LOW ((L)->num_iters))
157 #define LOOP_PEELING_FOR_ALIGNMENT(L) (L)->peeling_for_alignment
158 #define LOOP_VINFO_UNALIGNED_DR(L) (L)->unaligned_dr
159 #define LOOP_VINFO_MAY_MISALIGN_STMTS(L) (L)->may_misalign_stmts
160 #define LOOP_VINFO_LOC(L) (L)->loop_line_number
161 #define LOOP_VINFO_MAY_ALIAS_DDRS(L) (L)->may_alias_ddrs
163 #define NITERS_KNOWN_P(n) \
164 (host_integerp ((n),0) \
165 && TREE_INT_CST_LOW ((n)) > 0)
167 #define LOOP_VINFO_NITERS_KNOWN_P(L) \
168 NITERS_KNOWN_P((L)->num_iters)
170 static inline loop_vec_info
171 loop_vec_info_for_loop (struct loop *loop)
173 return (loop_vec_info) loop->aux;
176 static inline bool
177 nested_in_vect_loop_p (struct loop *loop, tree stmt)
179 return (loop->inner
180 && (loop->inner == (bb_for_stmt (stmt))->loop_father));
183 /*-----------------------------------------------------------------*/
184 /* Info on vectorized defs. */
185 /*-----------------------------------------------------------------*/
186 enum stmt_vec_info_type {
187 undef_vec_info_type = 0,
188 load_vec_info_type,
189 store_vec_info_type,
190 op_vec_info_type,
191 call_vec_info_type,
192 assignment_vec_info_type,
193 condition_vec_info_type,
194 reduc_vec_info_type,
195 induc_vec_info_type,
196 type_promotion_vec_info_type,
197 type_demotion_vec_info_type,
198 type_conversion_vec_info_type,
199 loop_exit_ctrl_vec_info_type
202 /* Indicates whether/how a variable is used in the loop. */
203 enum vect_relevant {
204 vect_unused_in_loop = 0,
205 vect_used_in_outer_by_reduction,
206 vect_used_in_outer,
208 /* defs that feed computations that end up (only) in a reduction. These
209 defs may be used by non-reduction stmts, but eventually, any
210 computations/values that are affected by these defs are used to compute
211 a reduction (i.e. don't get stored to memory, for example). We use this
212 to identify computations that we can change the order in which they are
213 computed. */
214 vect_used_by_reduction,
216 vect_used_in_loop
219 typedef struct data_reference *dr_p;
220 DEF_VEC_P(dr_p);
221 DEF_VEC_ALLOC_P(dr_p,heap);
223 typedef struct _stmt_vec_info {
225 enum stmt_vec_info_type type;
227 /* The stmt to which this info struct refers to. */
228 tree stmt;
230 /* The loop_vec_info with respect to which STMT is vectorized. */
231 loop_vec_info loop_vinfo;
233 /* Not all stmts in the loop need to be vectorized. e.g, the increment
234 of the loop induction variable and computation of array indexes. relevant
235 indicates whether the stmt needs to be vectorized. */
236 enum vect_relevant relevant;
238 /* Indicates whether this stmts is part of a computation whose result is
239 used outside the loop. */
240 bool live;
242 /* The vector type to be used. */
243 tree vectype;
245 /* The vectorized version of the stmt. */
246 tree vectorized_stmt;
249 /** The following is relevant only for stmts that contain a non-scalar
250 data-ref (array/pointer/struct access). A GIMPLE stmt is expected to have
251 at most one such data-ref. **/
253 /* Information about the data-ref (access function, etc),
254 relative to the inner-most containing loop. */
255 struct data_reference *data_ref_info;
257 /* Information about the data-ref relative to this loop
258 nest (the loop that is being considered for vectorization). */
259 tree dr_base_address;
260 tree dr_init;
261 tree dr_offset;
262 tree dr_step;
263 tree dr_aligned_to;
265 /* Stmt is part of some pattern (computation idiom) */
266 bool in_pattern_p;
268 /* Used for various bookkeeping purposes, generally holding a pointer to
269 some other stmt S that is in some way "related" to this stmt.
270 Current use of this field is:
271 If this stmt is part of a pattern (i.e. the field 'in_pattern_p' is
272 true): S is the "pattern stmt" that represents (and replaces) the
273 sequence of stmts that constitutes the pattern. Similarly, the
274 related_stmt of the "pattern stmt" points back to this stmt (which is
275 the last stmt in the original sequence of stmts that constitutes the
276 pattern). */
277 tree related_stmt;
279 /* List of datarefs that are known to have the same alignment as the dataref
280 of this stmt. */
281 VEC(dr_p,heap) *same_align_refs;
283 /* Classify the def of this stmt. */
284 enum vect_def_type def_type;
286 /* Interleaving info. */
287 /* First data-ref in the interleaving group. */
288 tree first_dr;
289 /* Pointer to the next data-ref in the group. */
290 tree next_dr;
291 /* The size of the interleaving group. */
292 unsigned int size;
293 /* For stores, number of stores from this group seen. We vectorize the last
294 one. */
295 unsigned int store_count;
296 /* For loads only, the gap from the previous load. For consecutive loads, GAP
297 is 1. */
298 unsigned int gap;
299 /* In case that two or more stmts share data-ref, this is the pointer to the
300 previously detected stmt with the same dr. */
301 tree same_dr_stmt;
302 /* For loads only, if there is a store with the same location, this field is
303 TRUE. */
304 bool read_write_dep;
306 /* Vectorization costs associated with statement. */
307 struct
309 int outside_of_loop; /* Statements generated outside loop. */
310 int inside_of_loop; /* Statements generated inside loop. */
311 } cost;
312 } *stmt_vec_info;
314 /* Access Functions. */
315 #define STMT_VINFO_TYPE(S) (S)->type
316 #define STMT_VINFO_STMT(S) (S)->stmt
317 #define STMT_VINFO_LOOP_VINFO(S) (S)->loop_vinfo
318 #define STMT_VINFO_RELEVANT(S) (S)->relevant
319 #define STMT_VINFO_LIVE_P(S) (S)->live
320 #define STMT_VINFO_VECTYPE(S) (S)->vectype
321 #define STMT_VINFO_VEC_STMT(S) (S)->vectorized_stmt
322 #define STMT_VINFO_DATA_REF(S) (S)->data_ref_info
324 #define STMT_VINFO_DR_BASE_ADDRESS(S) (S)->dr_base_address
325 #define STMT_VINFO_DR_INIT(S) (S)->dr_init
326 #define STMT_VINFO_DR_OFFSET(S) (S)->dr_offset
327 #define STMT_VINFO_DR_STEP(S) (S)->dr_step
328 #define STMT_VINFO_DR_ALIGNED_TO(S) (S)->dr_aligned_to
330 #define STMT_VINFO_IN_PATTERN_P(S) (S)->in_pattern_p
331 #define STMT_VINFO_RELATED_STMT(S) (S)->related_stmt
332 #define STMT_VINFO_SAME_ALIGN_REFS(S) (S)->same_align_refs
333 #define STMT_VINFO_DEF_TYPE(S) (S)->def_type
334 #define STMT_VINFO_DR_GROUP_FIRST_DR(S) (S)->first_dr
335 #define STMT_VINFO_DR_GROUP_NEXT_DR(S) (S)->next_dr
336 #define STMT_VINFO_DR_GROUP_SIZE(S) (S)->size
337 #define STMT_VINFO_DR_GROUP_STORE_COUNT(S) (S)->store_count
338 #define STMT_VINFO_DR_GROUP_GAP(S) (S)->gap
339 #define STMT_VINFO_DR_GROUP_SAME_DR_STMT(S)(S)->same_dr_stmt
340 #define STMT_VINFO_DR_GROUP_READ_WRITE_DEPENDENCE(S) (S)->read_write_dep
342 #define DR_GROUP_FIRST_DR(S) (S)->first_dr
343 #define DR_GROUP_NEXT_DR(S) (S)->next_dr
344 #define DR_GROUP_SIZE(S) (S)->size
345 #define DR_GROUP_STORE_COUNT(S) (S)->store_count
346 #define DR_GROUP_GAP(S) (S)->gap
347 #define DR_GROUP_SAME_DR_STMT(S) (S)->same_dr_stmt
348 #define DR_GROUP_READ_WRITE_DEPENDENCE(S) (S)->read_write_dep
350 #define STMT_VINFO_RELEVANT_P(S) ((S)->relevant != vect_unused_in_loop)
351 #define STMT_VINFO_OUTSIDE_OF_LOOP_COST(S) (S)->cost.outside_of_loop
352 #define STMT_VINFO_INSIDE_OF_LOOP_COST(S) (S)->cost.inside_of_loop
354 /* These are some defines for the initial implementation of the vectorizer's
355 cost model. These will later be target specific hooks. */
357 /* Cost of conditional branch. */
358 #ifndef TARG_COND_BRANCH_COST
359 #define TARG_COND_BRANCH_COST 3
360 #endif
362 /* Cost of any scalar operation, excluding load and store. */
363 #ifndef TARG_SCALAR_STMT_COST
364 #define TARG_SCALAR_STMT_COST 1
365 #endif
367 /* Cost of scalar load. */
368 #ifndef TARG_SCALAR_LOAD_COST
369 #define TARG_SCALAR_LOAD_COST 1
370 #endif
372 /* Cost of scalar store. */
373 #ifndef TARG_SCALAR_STORE_COST
374 #define TARG_SCALAR_STORE_COST 1
375 #endif
377 /* Cost of any vector operation, excluding load, store or vector to scalar
378 operation. */
379 #ifndef TARG_VEC_STMT_COST
380 #define TARG_VEC_STMT_COST 1
381 #endif
383 /* Cost of vector to scalar operation. */
384 #ifndef TARG_VEC_TO_SCALAR_COST
385 #define TARG_VEC_TO_SCALAR_COST 1
386 #endif
388 /* Cost of scalar to vector operation. */
389 #ifndef TARG_SCALAR_TO_VEC_COST
390 #define TARG_SCALAR_TO_VEC_COST 1
391 #endif
393 /* Cost of aligned vector load. */
394 #ifndef TARG_VEC_LOAD_COST
395 #define TARG_VEC_LOAD_COST 1
396 #endif
398 /* Cost of misaligned vector load. */
399 #ifndef TARG_VEC_UNALIGNED_LOAD_COST
400 #define TARG_VEC_UNALIGNED_LOAD_COST 2
401 #endif
403 /* Cost of vector store. */
404 #ifndef TARG_VEC_STORE_COST
405 #define TARG_VEC_STORE_COST 1
406 #endif
408 static inline void set_stmt_info (stmt_ann_t ann, stmt_vec_info stmt_info);
409 static inline stmt_vec_info vinfo_for_stmt (tree stmt);
411 static inline void
412 set_stmt_info (stmt_ann_t ann, stmt_vec_info stmt_info)
414 if (ann)
415 ann->common.aux = (char *) stmt_info;
418 static inline stmt_vec_info
419 vinfo_for_stmt (tree stmt)
421 stmt_ann_t ann = stmt_ann (stmt);
422 return ann ? (stmt_vec_info) ann->common.aux : NULL;
425 static inline bool
426 is_pattern_stmt_p (stmt_vec_info stmt_info)
428 tree related_stmt;
429 stmt_vec_info related_stmt_info;
431 related_stmt = STMT_VINFO_RELATED_STMT (stmt_info);
432 if (related_stmt
433 && (related_stmt_info = vinfo_for_stmt (related_stmt))
434 && STMT_VINFO_IN_PATTERN_P (related_stmt_info))
435 return true;
437 return false;
440 static inline bool
441 is_loop_header_bb_p (basic_block bb)
443 if (bb == (bb->loop_father)->header)
444 return true;
445 gcc_assert (EDGE_COUNT (bb->preds) == 1);
446 return false;
449 /*-----------------------------------------------------------------*/
450 /* Info on data references alignment. */
451 /*-----------------------------------------------------------------*/
453 /* Reflects actual alignment of first access in the vectorized loop,
454 taking into account peeling/versioning if applied. */
455 #define DR_MISALIGNMENT(DR) ((int) (size_t) (DR)->aux)
456 #define SET_DR_MISALIGNMENT(DR, VAL) ((DR)->aux = (void *) (size_t) (VAL))
458 static inline bool
459 aligned_access_p (struct data_reference *data_ref_info)
461 return (DR_MISALIGNMENT (data_ref_info) == 0);
464 static inline bool
465 known_alignment_for_access_p (struct data_reference *data_ref_info)
467 return (DR_MISALIGNMENT (data_ref_info) != -1);
470 /* vect_dump will be set to stderr or dump_file if exist. */
471 extern FILE *vect_dump;
472 extern enum verbosity_levels vect_verbosity_level;
474 /* Bitmap of virtual variables to be renamed. */
475 extern bitmap vect_memsyms_to_rename;
477 /*-----------------------------------------------------------------*/
478 /* Function prototypes. */
479 /*-----------------------------------------------------------------*/
481 /*************************************************************************
482 Simple Loop Peeling Utilities - in tree-vectorizer.c
483 *************************************************************************/
484 /* Entry point for peeling of simple loops.
485 Peel the first/last iterations of a loop.
486 It can be used outside of the vectorizer for loops that are simple enough
487 (see function documentation). In the vectorizer it is used to peel the
488 last few iterations when the loop bound is unknown or does not evenly
489 divide by the vectorization factor, and to peel the first few iterations
490 to force the alignment of data references in the loop. */
491 extern struct loop *slpeel_tree_peel_loop_to_edge
492 (struct loop *, edge, tree, tree, bool, unsigned int);
493 extern void slpeel_make_loop_iterate_ntimes (struct loop *, tree);
494 extern bool slpeel_can_duplicate_loop_p (const struct loop *, const_edge);
495 #ifdef ENABLE_CHECKING
496 extern void slpeel_verify_cfg_after_peeling (struct loop *, struct loop *);
497 #endif
500 /*************************************************************************
501 General Vectorization Utilities
502 *************************************************************************/
503 /** In tree-vectorizer.c **/
504 extern tree get_vectype_for_scalar_type (tree);
505 extern bool vect_is_simple_use (tree, loop_vec_info, tree *, tree *,
506 enum vect_def_type *);
507 extern bool vect_is_simple_iv_evolution (unsigned, tree, tree *, tree *);
508 extern tree vect_is_simple_reduction (loop_vec_info, tree);
509 extern bool vect_can_force_dr_alignment_p (const_tree, unsigned int);
510 extern enum dr_alignment_support vect_supportable_dr_alignment
511 (struct data_reference *);
512 extern bool reduction_code_for_scalar_code (enum tree_code, enum tree_code *);
513 extern bool supportable_widening_operation (enum tree_code, tree, tree,
514 tree *, tree *, enum tree_code *, enum tree_code *);
515 extern bool supportable_narrowing_operation (enum tree_code, const_tree,
516 const_tree, enum tree_code *);
518 /* Creation and deletion of loop and stmt info structs. */
519 extern loop_vec_info new_loop_vec_info (struct loop *loop);
520 extern void destroy_loop_vec_info (loop_vec_info, bool);
521 extern stmt_vec_info new_stmt_vec_info (tree stmt, loop_vec_info);
524 /** In tree-vect-analyze.c **/
525 /* Driver for analysis stage. */
526 extern loop_vec_info vect_analyze_loop (struct loop *);
529 /** In tree-vect-patterns.c **/
530 /* Pattern recognition functions.
531 Additional pattern recognition functions can (and will) be added
532 in the future. */
533 typedef tree (* vect_recog_func_ptr) (tree, tree *, tree *);
534 #define NUM_PATTERNS 4
535 void vect_pattern_recog (loop_vec_info);
538 /** In tree-vect-transform.c **/
539 extern bool vectorizable_load (tree, block_stmt_iterator *, tree *);
540 extern bool vectorizable_store (tree, block_stmt_iterator *, tree *);
541 extern bool vectorizable_operation (tree, block_stmt_iterator *, tree *);
542 extern bool vectorizable_type_promotion (tree, block_stmt_iterator *, tree *);
543 extern bool vectorizable_type_demotion (tree, block_stmt_iterator *, tree *);
544 extern bool vectorizable_conversion (tree, block_stmt_iterator *,
545 tree *);
546 extern bool vectorizable_assignment (tree, block_stmt_iterator *, tree *);
547 extern tree vectorizable_function (tree, tree, tree);
548 extern bool vectorizable_call (tree, block_stmt_iterator *, tree *);
549 extern bool vectorizable_condition (tree, block_stmt_iterator *, tree *);
550 extern bool vectorizable_live_operation (tree, block_stmt_iterator *, tree *);
551 extern bool vectorizable_reduction (tree, block_stmt_iterator *, tree *);
552 extern bool vectorizable_induction (tree, block_stmt_iterator *, tree *);
553 extern int vect_estimate_min_profitable_iters (loop_vec_info);
554 /* Driver for transformation stage. */
555 extern void vect_transform_loop (loop_vec_info);
557 /*************************************************************************
558 Vectorization Debug Information - in tree-vectorizer.c
559 *************************************************************************/
560 extern bool vect_print_dump_info (enum verbosity_levels);
561 extern void vect_set_verbosity_level (const char *);
562 extern LOC find_loop_location (struct loop *);
564 #endif /* GCC_TREE_VECTORIZER_H */