OSDN Git Service

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