Update concepts branch to revision 131834
[official-gcc.git] / gcc / tree-data-ref.h
blobc1672eb3d53f53d655a1d9f24d629a5f76092546
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>
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_DATA_REF_H
22 #define GCC_TREE_DATA_REF_H
24 #include "graphds.h"
25 #include "lambda.h"
26 #include "omega.h"
27 #include "tree-chrec.h"
30 innermost_loop_behavior describes the evolution of the address of the memory
31 reference in the innermost enclosing loop. The address is expressed as
32 BASE + STEP * # of iteration, and base is further decomposed as the base
33 pointer (BASE_ADDRESS), loop invariant offset (OFFSET) and
34 constant offset (INIT). Examples, in loop nest
36 for (i = 0; i < 100; i++)
37 for (j = 3; j < 100; j++)
39 Example 1 Example 2
40 data-ref a[j].b[i][j] *(p + x + 16B + 4B * j)
43 innermost_loop_behavior
44 base_address &a p
45 offset i * D_i x
46 init 3 * D_j + offsetof (b) 28
47 step D_j 4
50 struct innermost_loop_behavior
52 tree base_address;
53 tree offset;
54 tree init;
55 tree step;
57 /* Alignment information. ALIGNED_TO is set to the largest power of two
58 that divides OFFSET. */
59 tree aligned_to;
62 /* Describes the evolutions of indices of the memory reference. The indices
63 are indices of the ARRAY_REFs and the operands of INDIRECT_REFs.
64 For ARRAY_REFs, BASE_OBJECT is the reference with zeroed indices
65 (note that this reference does not have to be valid, if zero does not
66 belong to the range of the array; hence it is not recommended to use
67 BASE_OBJECT in any code generation). For INDIRECT_REFs, the address is
68 set to the loop-invariant part of the address of the object, except for
69 the constant offset. For the examples above,
71 base_object: a[0].b[0][0] *(p + x + 4B * j_0)
72 indices: {j_0, +, 1}_2 {16, +, 4}_2
73 {i_0, +, 1}_1
74 {j_0, +, 1}_2
77 struct indices
79 /* The object. */
80 tree base_object;
82 /* A list of chrecs. Access functions of the indices. */
83 VEC(tree,heap) *access_fns;
86 struct dr_alias
88 /* The alias information that should be used for new pointers to this
89 location. SYMBOL_TAG is either a DECL or a SYMBOL_MEMORY_TAG. */
90 tree symbol_tag;
91 struct ptr_info_def *ptr_info;
93 /* The set of virtual operands corresponding to this memory reference,
94 serving as a description of the alias information for the memory
95 reference. This could be eliminated if we had alias oracle. */
96 bitmap vops;
99 /* Each vector of the access matrix represents a linear access
100 function for a subscript. First elements correspond to the
101 leftmost indices, ie. for a[i][j] the first vector corresponds to
102 the subscript in "i". The elements of a vector are relative to
103 the loop nests in which the data reference is considered,
104 i.e. the vector is relative to the SCoP that provides the context
105 in which this data reference occurs.
107 For example, in
109 | loop_1
110 | loop_2
111 | a[i+3][2*j+n-1]
113 if "i" varies in loop_1 and "j" varies in loop_2, the access
114 matrix with respect to the loop nest {loop_1, loop_2} is:
116 | loop_1 loop_2 param_n cst
117 | 1 0 0 3
118 | 0 2 1 -1
120 whereas the access matrix with respect to loop_2 considers "i" as
121 a parameter:
123 | loop_2 param_i param_n cst
124 | 0 1 0 3
125 | 2 0 1 -1
127 struct access_matrix
129 int loop_nest_num;
130 int nb_induction_vars;
131 VEC (tree, heap) *parameters;
132 VEC (lambda_vector, heap) *matrix;
135 #define AM_LOOP_NEST_NUM(M) (M)->loop_nest_num
136 #define AM_NB_INDUCTION_VARS(M) (M)->nb_induction_vars
137 #define AM_PARAMETERS(M) (M)->parameters
138 #define AM_MATRIX(M) (M)->matrix
139 #define AM_NB_PARAMETERS(M) (VEC_length (tree, AM_PARAMETERS(M)))
140 #define AM_CONST_COLUMN_INDEX(M) (AM_NB_INDUCTION_VARS (M) + AM_NB_PARAMETERS (M))
141 #define AM_NB_COLUMNS(M) (AM_NB_INDUCTION_VARS (M) + AM_NB_PARAMETERS (M) + 1)
142 #define AM_GET_SUBSCRIPT_ACCESS_VECTOR(M, I) VEC_index (lambda_vector, AM_MATRIX (M), I)
143 #define AM_GET_ACCESS_MATRIX_ELEMENT(M, I, J) AM_GET_SUBSCRIPT_ACCESS_VECTOR (M, I)[J]
145 /* Return the column in the access matrix of LOOP_NUM. */
147 static inline int
148 am_vector_index_for_loop (struct access_matrix *access_matrix, int loop_num)
150 gcc_assert (loop_num >= AM_LOOP_NEST_NUM (access_matrix));
151 return loop_num - AM_LOOP_NEST_NUM (access_matrix);
154 int access_matrix_get_index_for_parameter (tree, struct access_matrix *);
156 struct data_reference
158 /* A pointer to the statement that contains this DR. */
159 tree stmt;
161 /* A pointer to the memory reference. */
162 tree ref;
164 /* Auxiliary info specific to a pass. */
165 void *aux;
167 /* True when the data reference is in RHS of a stmt. */
168 bool is_read;
170 /* Behavior of the memory reference in the innermost loop. */
171 struct innermost_loop_behavior innermost;
173 /* Decomposition to indices for alias analysis. */
174 struct indices indices;
176 /* Alias information for the data reference. */
177 struct dr_alias alias;
179 /* Matrix representation for the data access functions. */
180 struct access_matrix *access_matrix;
183 #define DR_STMT(DR) (DR)->stmt
184 #define DR_REF(DR) (DR)->ref
185 #define DR_BASE_OBJECT(DR) (DR)->indices.base_object
186 #define DR_ACCESS_FNS(DR) (DR)->indices.access_fns
187 #define DR_ACCESS_FN(DR, I) VEC_index (tree, DR_ACCESS_FNS (DR), I)
188 #define DR_NUM_DIMENSIONS(DR) VEC_length (tree, DR_ACCESS_FNS (DR))
189 #define DR_IS_READ(DR) (DR)->is_read
190 #define DR_BASE_ADDRESS(DR) (DR)->innermost.base_address
191 #define DR_OFFSET(DR) (DR)->innermost.offset
192 #define DR_INIT(DR) (DR)->innermost.init
193 #define DR_STEP(DR) (DR)->innermost.step
194 #define DR_SYMBOL_TAG(DR) (DR)->alias.symbol_tag
195 #define DR_PTR_INFO(DR) (DR)->alias.ptr_info
196 #define DR_VOPS(DR) (DR)->alias.vops
197 #define DR_ALIGNED_TO(DR) (DR)->innermost.aligned_to
198 #define DR_ACCESS_MATRIX(DR) (DR)->access_matrix
200 typedef struct data_reference *data_reference_p;
201 DEF_VEC_P(data_reference_p);
202 DEF_VEC_ALLOC_P (data_reference_p, heap);
204 enum data_dependence_direction {
205 dir_positive,
206 dir_negative,
207 dir_equal,
208 dir_positive_or_negative,
209 dir_positive_or_equal,
210 dir_negative_or_equal,
211 dir_star,
212 dir_independent
215 /* The description of the grid of iterations that overlap. At most
216 two loops are considered at the same time just now, hence at most
217 two functions are needed. For each of the functions, we store
218 the vector of coefficients, f[0] + x * f[1] + y * f[2] + ...,
219 where x, y, ... are variables. */
221 #define MAX_DIM 2
223 /* Special values of N. */
224 #define NO_DEPENDENCE 0
225 #define NOT_KNOWN (MAX_DIM + 1)
226 #define CF_NONTRIVIAL_P(CF) ((CF)->n != NO_DEPENDENCE && (CF)->n != NOT_KNOWN)
227 #define CF_NOT_KNOWN_P(CF) ((CF)->n == NOT_KNOWN)
228 #define CF_NO_DEPENDENCE_P(CF) ((CF)->n == NO_DEPENDENCE)
230 typedef VEC (tree, heap) *affine_fn;
232 typedef struct
234 unsigned n;
235 affine_fn fns[MAX_DIM];
236 } conflict_function;
238 /* What is a subscript? Given two array accesses a subscript is the
239 tuple composed of the access functions for a given dimension.
240 Example: Given A[f1][f2][f3] and B[g1][g2][g3], there are three
241 subscripts: (f1, g1), (f2, g2), (f3, g3). These three subscripts
242 are stored in the data_dependence_relation structure under the form
243 of an array of subscripts. */
245 struct subscript
247 /* A description of the iterations for which the elements are
248 accessed twice. */
249 conflict_function *conflicting_iterations_in_a;
250 conflict_function *conflicting_iterations_in_b;
252 /* This field stores the information about the iteration domain
253 validity of the dependence relation. */
254 tree last_conflict;
256 /* Distance from the iteration that access a conflicting element in
257 A to the iteration that access this same conflicting element in
258 B. The distance is a tree scalar expression, i.e. a constant or a
259 symbolic expression, but certainly not a chrec function. */
260 tree distance;
263 typedef struct subscript *subscript_p;
264 DEF_VEC_P(subscript_p);
265 DEF_VEC_ALLOC_P (subscript_p, heap);
267 #define SUB_CONFLICTS_IN_A(SUB) SUB->conflicting_iterations_in_a
268 #define SUB_CONFLICTS_IN_B(SUB) SUB->conflicting_iterations_in_b
269 #define SUB_LAST_CONFLICT(SUB) SUB->last_conflict
270 #define SUB_DISTANCE(SUB) SUB->distance
272 /* A data_dependence_relation represents a relation between two
273 data_references A and B. */
275 struct data_dependence_relation
278 struct data_reference *a;
279 struct data_reference *b;
281 /* When the dependence relation is affine, it can be represented by
282 a distance vector. */
283 bool affine_p;
285 /* Set to true when the dependence relation is on the same data
286 access. */
287 bool self_reference_p;
289 /* A "yes/no/maybe" field for the dependence relation:
291 - when "ARE_DEPENDENT == NULL_TREE", there exist a dependence
292 relation between A and B, and the description of this relation
293 is given in the SUBSCRIPTS array,
295 - when "ARE_DEPENDENT == chrec_known", there is no dependence and
296 SUBSCRIPTS is empty,
298 - when "ARE_DEPENDENT == chrec_dont_know", there may be a dependence,
299 but the analyzer cannot be more specific. */
300 tree are_dependent;
302 /* For each subscript in the dependence test, there is an element in
303 this array. This is the attribute that labels the edge A->B of
304 the data_dependence_relation. */
305 VEC (subscript_p, heap) *subscripts;
307 /* The analyzed loop nest. */
308 VEC (loop_p, heap) *loop_nest;
310 /* An index in loop_nest for the innermost loop that varies for
311 this data dependence relation. */
312 unsigned inner_loop;
314 /* The classic direction vector. */
315 VEC (lambda_vector, heap) *dir_vects;
317 /* The classic distance vector. */
318 VEC (lambda_vector, heap) *dist_vects;
320 /* Is the dependence reversed with respect to the lexicographic order? */
321 bool reversed_p;
324 typedef struct data_dependence_relation *ddr_p;
325 DEF_VEC_P(ddr_p);
326 DEF_VEC_ALLOC_P(ddr_p,heap);
328 #define DDR_A(DDR) DDR->a
329 #define DDR_B(DDR) DDR->b
330 #define DDR_AFFINE_P(DDR) DDR->affine_p
331 #define DDR_ARE_DEPENDENT(DDR) DDR->are_dependent
332 #define DDR_SUBSCRIPTS(DDR) DDR->subscripts
333 #define DDR_SUBSCRIPT(DDR, I) VEC_index (subscript_p, DDR_SUBSCRIPTS (DDR), I)
334 #define DDR_NUM_SUBSCRIPTS(DDR) VEC_length (subscript_p, DDR_SUBSCRIPTS (DDR))
336 #define DDR_LOOP_NEST(DDR) DDR->loop_nest
337 /* The size of the direction/distance vectors: the number of loops in
338 the loop nest. */
339 #define DDR_NB_LOOPS(DDR) (VEC_length (loop_p, DDR_LOOP_NEST (DDR)))
340 #define DDR_INNER_LOOP(DDR) DDR->inner_loop
341 #define DDR_SELF_REFERENCE(DDR) DDR->self_reference_p
343 #define DDR_DIST_VECTS(DDR) ((DDR)->dist_vects)
344 #define DDR_DIR_VECTS(DDR) ((DDR)->dir_vects)
345 #define DDR_NUM_DIST_VECTS(DDR) \
346 (VEC_length (lambda_vector, DDR_DIST_VECTS (DDR)))
347 #define DDR_NUM_DIR_VECTS(DDR) \
348 (VEC_length (lambda_vector, DDR_DIR_VECTS (DDR)))
349 #define DDR_DIR_VECT(DDR, I) \
350 VEC_index (lambda_vector, DDR_DIR_VECTS (DDR), I)
351 #define DDR_DIST_VECT(DDR, I) \
352 VEC_index (lambda_vector, DDR_DIST_VECTS (DDR), I)
353 #define DDR_REVERSED_P(DDR) DDR->reversed_p
357 /* Describes a location of a memory reference. */
359 typedef struct data_ref_loc_d
361 /* Position of the memory reference. */
362 tree *pos;
364 /* True if the memory reference is read. */
365 bool is_read;
366 } data_ref_loc;
368 DEF_VEC_O (data_ref_loc);
369 DEF_VEC_ALLOC_O (data_ref_loc, heap);
371 bool get_references_in_stmt (tree, VEC (data_ref_loc, heap) **);
372 void dr_analyze_innermost (struct data_reference *);
373 extern bool compute_data_dependences_for_loop (struct loop *, bool,
374 VEC (data_reference_p, heap) **,
375 VEC (ddr_p, heap) **);
376 extern void print_direction_vector (FILE *, lambda_vector, int);
377 extern void print_dir_vectors (FILE *, VEC (lambda_vector, heap) *, int);
378 extern void print_dist_vectors (FILE *, VEC (lambda_vector, heap) *, int);
379 extern void dump_subscript (FILE *, struct subscript *);
380 extern void dump_ddrs (FILE *, VEC (ddr_p, heap) *);
381 extern void dump_dist_dir_vectors (FILE *, VEC (ddr_p, heap) *);
382 extern void dump_data_reference (FILE *, struct data_reference *);
383 extern void dump_data_references (FILE *, VEC (data_reference_p, heap) *);
384 extern void debug_data_dependence_relation (struct data_dependence_relation *);
385 extern void dump_data_dependence_relation (FILE *,
386 struct data_dependence_relation *);
387 extern void dump_data_dependence_relations (FILE *, VEC (ddr_p, heap) *);
388 extern void debug_data_dependence_relations (VEC (ddr_p, heap) *);
389 extern void dump_data_dependence_direction (FILE *,
390 enum data_dependence_direction);
391 extern void free_dependence_relation (struct data_dependence_relation *);
392 extern void free_dependence_relations (VEC (ddr_p, heap) *);
393 extern void free_data_ref (data_reference_p);
394 extern void free_data_refs (VEC (data_reference_p, heap) *);
395 struct data_reference *create_data_ref (struct loop *, tree, tree, bool);
396 bool find_loop_nest (struct loop *, VEC (loop_p, heap) **);
397 void compute_all_dependences (VEC (data_reference_p, heap) *,
398 VEC (ddr_p, heap) **, VEC (loop_p, heap) *, bool);
400 /* Return true when the DDR contains two data references that have the
401 same access functions. */
403 static inline bool
404 same_access_functions (const struct data_dependence_relation *ddr)
406 unsigned i;
408 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
409 if (!eq_evolutions_p (DR_ACCESS_FN (DDR_A (ddr), i),
410 DR_ACCESS_FN (DDR_B (ddr), i)))
411 return false;
413 return true;
416 /* Return true when DDR is an anti-dependence relation. */
418 static inline bool
419 ddr_is_anti_dependent (ddr_p ddr)
421 return (DDR_ARE_DEPENDENT (ddr) == NULL_TREE
422 && DR_IS_READ (DDR_A (ddr))
423 && !DR_IS_READ (DDR_B (ddr))
424 && !same_access_functions (ddr));
427 /* Return true when DEPENDENCE_RELATIONS contains an anti-dependence. */
429 static inline bool
430 ddrs_have_anti_deps (VEC (ddr_p, heap) *dependence_relations)
432 unsigned i;
433 ddr_p ddr;
435 for (i = 0; VEC_iterate (ddr_p, dependence_relations, i, ddr); i++)
436 if (ddr_is_anti_dependent (ddr))
437 return true;
439 return false;
442 /* Return the dependence level for the DDR relation. */
444 static inline unsigned
445 ddr_dependence_level (ddr_p ddr)
447 unsigned vector;
448 unsigned level = 0;
450 if (DDR_DIST_VECTS (ddr))
451 level = dependence_level (DDR_DIST_VECT (ddr, 0), DDR_NB_LOOPS (ddr));
453 for (vector = 1; vector < DDR_NUM_DIST_VECTS (ddr); vector++)
454 level = MIN (level, dependence_level (DDR_DIST_VECT (ddr, vector),
455 DDR_NB_LOOPS (ddr)));
456 return level;
461 /* A Reduced Dependence Graph (RDG) vertex representing a statement. */
462 typedef struct rdg_vertex
464 /* The statement represented by this vertex. */
465 tree stmt;
467 /* True when the statement contains a write to memory. */
468 bool has_mem_write;
470 /* True when the statement contains a read from memory. */
471 bool has_mem_reads;
472 } *rdg_vertex_p;
474 #define RDGV_STMT(V) ((struct rdg_vertex *) ((V)->data))->stmt
475 #define RDGV_HAS_MEM_WRITE(V) ((struct rdg_vertex *) ((V)->data))->has_mem_write
476 #define RDGV_HAS_MEM_READS(V) ((struct rdg_vertex *) ((V)->data))->has_mem_reads
477 #define RDG_STMT(RDG, I) RDGV_STMT (&(RDG->vertices[I]))
478 #define RDG_MEM_WRITE_STMT(RDG, I) RDGV_HAS_MEM_WRITE (&(RDG->vertices[I]))
479 #define RDG_MEM_READS_STMT(RDG, I) RDGV_HAS_MEM_READS (&(RDG->vertices[I]))
481 void dump_rdg_vertex (FILE *, struct graph *, int);
482 void debug_rdg_vertex (struct graph *, int);
483 void dump_rdg_component (FILE *, struct graph *, int, bitmap);
484 void debug_rdg_component (struct graph *, int);
485 void dump_rdg (FILE *, struct graph *);
486 void debug_rdg (struct graph *);
487 void dot_rdg (struct graph *);
488 int rdg_vertex_for_stmt (struct graph *, tree);
490 /* Data dependence type. */
492 enum rdg_dep_type
494 /* Read After Write (RAW). */
495 flow_dd = 'f',
497 /* Write After Read (WAR). */
498 anti_dd = 'a',
500 /* Write After Write (WAW). */
501 output_dd = 'o',
503 /* Read After Read (RAR). */
504 input_dd = 'i'
507 /* Dependence information attached to an edge of the RDG. */
509 typedef struct rdg_edge
511 /* Type of the dependence. */
512 enum rdg_dep_type type;
514 /* Levels of the dependence: the depth of the loops that
515 carry the dependence. */
516 unsigned level;
517 } *rdg_edge_p;
519 #define RDGE_TYPE(E) ((struct rdg_edge *) ((E)->data))->type
520 #define RDGE_LEVEL(E) ((struct rdg_edge *) ((E)->data))->level
522 struct graph *build_rdg (struct loop *);
523 void free_rdg (struct graph *);
525 /* Return the index of the variable VAR in the LOOP_NEST array. */
527 static inline int
528 index_in_loop_nest (int var, VEC (loop_p, heap) *loop_nest)
530 struct loop *loopi;
531 int var_index;
533 for (var_index = 0; VEC_iterate (loop_p, loop_nest, var_index, loopi);
534 var_index++)
535 if (loopi->num == var)
536 break;
538 return var_index;
541 void stores_from_loop (struct loop *, VEC (tree, heap) **);
542 void remove_similar_memory_refs (VEC (tree, heap) **);
543 bool rdg_defs_used_in_other_loops_p (struct graph *, int);
544 bool have_similar_memory_accesses (tree, tree);
546 /* Determines whether RDG vertices V1 and V2 access to similar memory
547 locations, in which case they have to be in the same partition. */
549 static inline bool
550 rdg_has_similar_memory_accesses (struct graph *rdg, int v1, int v2)
552 return have_similar_memory_accesses (RDG_STMT (rdg, v1),
553 RDG_STMT (rdg, v2));
556 /* In lambda-code.c */
557 bool lambda_transform_legal_p (lambda_trans_matrix, int,
558 VEC (ddr_p, heap) *);
559 void lambda_collect_parameters (VEC (data_reference_p, heap) *,
560 VEC (tree, heap) **);
561 bool lambda_compute_access_matrices (VEC (data_reference_p, heap) *,
562 VEC (tree, heap) *, int);
564 /* In tree-data-refs.c */
565 void split_constant_offset (tree , tree *, tree *);
567 #endif /* GCC_TREE_DATA_REF_H */