OSDN Git Service

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