OSDN Git Service

Daily bump.
[pf3gnuchains/gcc-fork.git] / gcc / tree-data-ref.h
1 /* Data references and dependences detectors. 
2    Copyright (C) 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
3    Contributed by Sebastian Pop <pop@cri.ensmp.fr>
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_DATA_REF_H
22 #define GCC_TREE_DATA_REF_H
23
24 #include "graphds.h"
25 #include "lambda.h"
26 #include "omega.h"
27
28 /*
29   innermost_loop_behavior describes the evolution of the address of the memory
30   reference in the innermost enclosing loop.  The address is expressed as
31   BASE + STEP * # of iteration, and base is further decomposed as the base
32   pointer (BASE_ADDRESS),  loop invariant offset (OFFSET) and
33   constant offset (INIT).  Examples, in loop nest 
34   
35   for (i = 0; i < 100; i++)
36     for (j = 3; j < 100; j++)
37
38                        Example 1                      Example 2
39       data-ref         a[j].b[i][j]                   *(p + x + 16B + 4B * j)
40       
41   innermost_loop_behavior
42       base_address     &a                             p
43       offset           i * D_i                        x
44       init             3 * D_j + offsetof (b)         28
45       step             D_j                            4
46
47   */
48 struct innermost_loop_behavior
49 {
50   tree base_address;
51   tree offset;
52   tree init;
53   tree step;
54
55   /* Alignment information.  ALIGNED_TO is set to the largest power of two
56      that divides OFFSET.  */
57   tree aligned_to;
58 };
59
60 /* Describes the evolutions of indices of the memory reference.  The indices
61    are indices of the ARRAY_REFs and the operands of INDIRECT_REFs.
62    For ARRAY_REFs, BASE_OBJECT is the reference with zeroed indices
63    (note that this reference does not have to be valid, if zero does not
64    belong to the range of the array; hence it is not recommended to use
65    BASE_OBJECT in any code generation).  For INDIRECT_REFs, the address is
66    set to the loop-invariant part of the address of the object, except for
67    the constant offset.  For the examples above,
68
69    base_object:        a[0].b[0][0]                   *(p + x + 4B * j_0)
70    indices:            {j_0, +, 1}_2                  {16, +, 4}_2
71                        {i_0, +, 1}_1
72                        {j_0, +, 1}_2
73 */
74
75 struct indices
76 {
77   /* The object.  */
78   tree base_object;
79   
80   /* A list of chrecs.  Access functions of the indices.  */
81   VEC(tree,heap) *access_fns;
82 };
83
84 struct dr_alias
85 {
86   /* The alias information that should be used for new pointers to this
87      location.  SYMBOL_TAG is either a DECL or a SYMBOL_MEMORY_TAG.  */
88   tree symbol_tag;
89   subvar_t subvars;
90   struct ptr_info_def *ptr_info;
91
92   /* The set of virtual operands corresponding to this memory reference,
93      serving as a description of the alias information for the memory
94      reference.  This could be eliminated if we had alias oracle.  */
95   bitmap vops;
96 };
97
98 struct data_reference
99 {
100   /* A pointer to the statement that contains this DR.  */
101   tree stmt;
102   
103   /* A pointer to the memory reference.  */
104   tree ref;
105
106   /* Auxiliary info specific to a pass.  */
107   void *aux;
108
109   /* True when the data reference is in RHS of a stmt.  */
110   bool is_read;
111
112   /* Behavior of the memory reference in the innermost loop.  */
113   struct innermost_loop_behavior innermost;
114
115   /* Decomposition to indices for alias analysis.  */
116   struct indices indices;
117
118   /* Alias information for the data reference.  */
119   struct dr_alias alias;
120 };
121
122 typedef struct data_reference *data_reference_p;
123 DEF_VEC_P(data_reference_p);
124 DEF_VEC_ALLOC_P (data_reference_p, heap);
125
126 #define DR_STMT(DR)                (DR)->stmt
127 #define DR_REF(DR)                 (DR)->ref
128 #define DR_BASE_OBJECT(DR)         (DR)->indices.base_object
129 #define DR_ACCESS_FNS(DR)          (DR)->indices.access_fns
130 #define DR_ACCESS_FN(DR, I)        VEC_index (tree, DR_ACCESS_FNS (DR), I)
131 #define DR_NUM_DIMENSIONS(DR)      VEC_length (tree, DR_ACCESS_FNS (DR))  
132 #define DR_IS_READ(DR)             (DR)->is_read
133 #define DR_BASE_ADDRESS(DR)        (DR)->innermost.base_address
134 #define DR_OFFSET(DR)              (DR)->innermost.offset
135 #define DR_INIT(DR)                (DR)->innermost.init
136 #define DR_STEP(DR)                (DR)->innermost.step
137 #define DR_SYMBOL_TAG(DR)          (DR)->alias.symbol_tag
138 #define DR_PTR_INFO(DR)            (DR)->alias.ptr_info
139 #define DR_SUBVARS(DR)             (DR)->alias.subvars
140 #define DR_VOPS(DR)                (DR)->alias.vops
141 #define DR_ALIGNED_TO(DR)          (DR)->innermost.aligned_to
142
143 enum data_dependence_direction {
144   dir_positive, 
145   dir_negative, 
146   dir_equal, 
147   dir_positive_or_negative,
148   dir_positive_or_equal,
149   dir_negative_or_equal,
150   dir_star,
151   dir_independent
152 };
153
154 /* The description of the grid of iterations that overlap.  At most
155    two loops are considered at the same time just now, hence at most
156    two functions are needed.  For each of the functions, we store
157    the vector of coefficients, f[0] + x * f[1] + y * f[2] + ...,
158    where x, y, ... are variables.  */
159
160 #define MAX_DIM 2
161
162 /* Special values of N.  */
163 #define NO_DEPENDENCE 0
164 #define NOT_KNOWN (MAX_DIM + 1)
165 #define CF_NONTRIVIAL_P(CF) ((CF)->n != NO_DEPENDENCE && (CF)->n != NOT_KNOWN)
166 #define CF_NOT_KNOWN_P(CF) ((CF)->n == NOT_KNOWN)
167 #define CF_NO_DEPENDENCE_P(CF) ((CF)->n == NO_DEPENDENCE)
168
169 typedef VEC (tree, heap) *affine_fn;
170
171 typedef struct
172 {
173   unsigned n;
174   affine_fn fns[MAX_DIM];
175 } conflict_function;
176
177 /* What is a subscript?  Given two array accesses a subscript is the
178    tuple composed of the access functions for a given dimension.
179    Example: Given A[f1][f2][f3] and B[g1][g2][g3], there are three
180    subscripts: (f1, g1), (f2, g2), (f3, g3).  These three subscripts
181    are stored in the data_dependence_relation structure under the form
182    of an array of subscripts.  */
183
184 struct subscript
185 {
186   /* A description of the iterations for which the elements are
187      accessed twice.  */
188   conflict_function *conflicting_iterations_in_a;
189   conflict_function *conflicting_iterations_in_b;
190   
191   /* This field stores the information about the iteration domain
192      validity of the dependence relation.  */
193   tree last_conflict;
194   
195   /* Distance from the iteration that access a conflicting element in
196      A to the iteration that access this same conflicting element in
197      B.  The distance is a tree scalar expression, i.e. a constant or a
198      symbolic expression, but certainly not a chrec function.  */
199   tree distance;
200 };
201
202 typedef struct subscript *subscript_p;
203 DEF_VEC_P(subscript_p);
204 DEF_VEC_ALLOC_P (subscript_p, heap);
205
206 #define SUB_CONFLICTS_IN_A(SUB) SUB->conflicting_iterations_in_a
207 #define SUB_CONFLICTS_IN_B(SUB) SUB->conflicting_iterations_in_b
208 #define SUB_LAST_CONFLICT(SUB) SUB->last_conflict
209 #define SUB_DISTANCE(SUB) SUB->distance
210
211 /* A data_dependence_relation represents a relation between two
212    data_references A and B.  */
213
214 struct data_dependence_relation
215 {
216   
217   struct data_reference *a;
218   struct data_reference *b;
219
220   /* When the dependence relation is affine, it can be represented by
221      a distance vector.  */
222   bool affine_p;
223
224   /* A "yes/no/maybe" field for the dependence relation:
225      
226      - when "ARE_DEPENDENT == NULL_TREE", there exist a dependence
227        relation between A and B, and the description of this relation
228        is given in the SUBSCRIPTS array,
229      
230      - when "ARE_DEPENDENT == chrec_known", there is no dependence and
231        SUBSCRIPTS is empty,
232      
233      - when "ARE_DEPENDENT == chrec_dont_know", there may be a dependence,
234        but the analyzer cannot be more specific.  */
235   tree are_dependent;
236   
237   /* For each subscript in the dependence test, there is an element in
238      this array.  This is the attribute that labels the edge A->B of
239      the data_dependence_relation.  */
240   VEC (subscript_p, heap) *subscripts;
241
242   /* The analyzed loop nest.  */
243   VEC (loop_p, heap) *loop_nest;
244
245   /* An index in loop_nest for the innermost loop that varies for
246      this data dependence relation.  */
247   unsigned inner_loop;
248
249   /* The classic direction vector.  */
250   VEC (lambda_vector, heap) *dir_vects;
251
252   /* The classic distance vector.  */
253   VEC (lambda_vector, heap) *dist_vects;
254
255   /* Is the dependence reversed with respect to the lexicographic order?  */
256   bool reversed_p;
257 };
258
259 typedef struct data_dependence_relation *ddr_p;
260 DEF_VEC_P(ddr_p);
261 DEF_VEC_ALLOC_P(ddr_p,heap);
262
263 #define DDR_A(DDR) DDR->a
264 #define DDR_B(DDR) DDR->b
265 #define DDR_AFFINE_P(DDR) DDR->affine_p
266 #define DDR_ARE_DEPENDENT(DDR) DDR->are_dependent
267 #define DDR_SUBSCRIPTS(DDR) DDR->subscripts
268 #define DDR_SUBSCRIPT(DDR, I) VEC_index (subscript_p, DDR_SUBSCRIPTS (DDR), I)
269 #define DDR_NUM_SUBSCRIPTS(DDR) VEC_length (subscript_p, DDR_SUBSCRIPTS (DDR))
270
271 #define DDR_LOOP_NEST(DDR) DDR->loop_nest
272 /* The size of the direction/distance vectors: the number of loops in
273    the loop nest.  */
274 #define DDR_NB_LOOPS(DDR) (VEC_length (loop_p, DDR_LOOP_NEST (DDR)))
275 #define DDR_INNER_LOOP(DDR) DDR->inner_loop
276
277 #define DDR_DIST_VECTS(DDR) ((DDR)->dist_vects)
278 #define DDR_DIR_VECTS(DDR) ((DDR)->dir_vects)
279 #define DDR_NUM_DIST_VECTS(DDR) \
280   (VEC_length (lambda_vector, DDR_DIST_VECTS (DDR)))
281 #define DDR_NUM_DIR_VECTS(DDR) \
282   (VEC_length (lambda_vector, DDR_DIR_VECTS (DDR)))
283 #define DDR_DIR_VECT(DDR, I) \
284   VEC_index (lambda_vector, DDR_DIR_VECTS (DDR), I)
285 #define DDR_DIST_VECT(DDR, I) \
286   VEC_index (lambda_vector, DDR_DIST_VECTS (DDR), I)
287 #define DDR_REVERSED_P(DDR) DDR->reversed_p
288
289 \f
290
291 /* Describes a location of a memory reference.  */
292
293 typedef struct data_ref_loc_d
294 {
295   /* Position of the memory reference.  */
296   tree *pos;
297
298   /* True if the memory reference is read.  */
299   bool is_read;
300 } data_ref_loc;
301
302 DEF_VEC_O (data_ref_loc);
303 DEF_VEC_ALLOC_O (data_ref_loc, heap);
304
305 bool get_references_in_stmt (tree, VEC (data_ref_loc, heap) **);
306 void dr_analyze_innermost (struct data_reference *);
307 extern void compute_data_dependences_for_loop (struct loop *, bool,
308                                                VEC (data_reference_p, heap) **,
309                                                VEC (ddr_p, heap) **);
310 extern void print_direction_vector (FILE *, lambda_vector, int);
311 extern void print_dir_vectors (FILE *, VEC (lambda_vector, heap) *, int);
312 extern void print_dist_vectors (FILE *, VEC (lambda_vector, heap) *, int);
313 extern void dump_subscript (FILE *, struct subscript *);
314 extern void dump_ddrs (FILE *, VEC (ddr_p, heap) *);
315 extern void dump_dist_dir_vectors (FILE *, VEC (ddr_p, heap) *);
316 extern void dump_data_reference (FILE *, struct data_reference *);
317 extern void dump_data_references (FILE *, VEC (data_reference_p, heap) *);
318 extern void debug_data_dependence_relation (struct data_dependence_relation *);
319 extern void dump_data_dependence_relation (FILE *, 
320                                            struct data_dependence_relation *);
321 extern void dump_data_dependence_relations (FILE *, VEC (ddr_p, heap) *);
322 extern void dump_data_dependence_direction (FILE *, 
323                                             enum data_dependence_direction);
324 extern void free_dependence_relation (struct data_dependence_relation *);
325 extern void free_dependence_relations (VEC (ddr_p, heap) *);
326 extern void free_data_refs (VEC (data_reference_p, heap) *);
327 struct data_reference *create_data_ref (struct loop *, tree, tree, bool);
328 bool find_loop_nest (struct loop *, VEC (loop_p, heap) **);
329 void compute_all_dependences (VEC (data_reference_p, heap) *,
330                               VEC (ddr_p, heap) **, VEC (loop_p, heap) *, bool);
331
332 \f
333
334 /* A RDG vertex representing a statement.  */
335 typedef struct rdg_vertex
336 {
337   /* The statement represented by this vertex.  */
338   tree stmt;
339 } *rdg_vertex_p;
340
341 #define RDGV_STMT(V)       ((struct rdg_vertex *) ((V)->data))->stmt
342
343 /* Data dependence type.  */
344
345 enum rdg_dep_type 
346 {
347   /* Read After Write (RAW).  */
348   flow_dd = 'f',
349   
350   /* Write After Read (WAR).  */
351   anti_dd = 'a',
352   
353   /* Write After Write (WAW).  */
354   output_dd = 'o', 
355   
356   /* Read After Read (RAR).  */
357   input_dd = 'i' 
358 };
359
360 /* Dependence information attached to an edge of the RDG.  */
361
362 typedef struct rdg_edge 
363 {
364   /* Type of the dependence.  */
365   enum rdg_dep_type type;
366 } *rdg_edge_p;
367
368 #define RDGE_TYPE(E)        ((struct rdg_edge *) ((E)->data))->type
369
370 struct graph *build_rdg (struct loop *);
371
372 /* Return the index of the variable VAR in the LOOP_NEST array.  */
373
374 static inline int
375 index_in_loop_nest (int var, VEC (loop_p, heap) *loop_nest)
376 {
377   struct loop *loopi;
378   int var_index;
379
380   for (var_index = 0; VEC_iterate (loop_p, loop_nest, var_index, loopi);
381        var_index++)
382     if (loopi->num == var)
383       break;
384
385   return var_index;
386 }
387
388 /* In lambda-code.c  */
389 bool lambda_transform_legal_p (lambda_trans_matrix, int, VEC (ddr_p, heap) *);
390
391 /* In tree-data-refs.c  */
392 void split_constant_offset (tree , tree *, tree *);
393
394 #endif  /* GCC_TREE_DATA_REF_H  */