OSDN Git Service

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