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 2, or (at your option) any later
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
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
22 #ifndef GCC_TREE_DATA_REF_H
23 #define GCC_TREE_DATA_REF_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++)
40 data-ref a[j].b[i][j] *(p + x + 16B + 4B * j)
42 innermost_loop_behavior
45 init 3 * D_j + offsetof (b) 28
49 struct innermost_loop_behavior
56 /* Alignment information. ALIGNED_TO is set to the largest power of two
57 that divides OFFSET. */
61 /* Describes the evolutions of indices of the memory reference. The indices
62 are indices of the ARRAY_REFs and the operands of INDIRECT_REFs.
63 For ARRAY_REFs, BASE_OBJECT is the reference with zeroed indices
64 (note that this reference does not have to be valid, if zero does not
65 belong to the range of the array; hence it is not recommended to use
66 BASE_OBJECT in any code generation). For INDIRECT_REFs, the address is
67 set to the loop-invariant part of the address of the object, except for
68 the constant offset. For the examples above,
70 base_object: a[0].b[0][0] *(p + x + 4B * j_0)
71 indices: {j_0, +, 1}_2 {16, +, 4}_2
81 /* A list of chrecs. Access functions of the indices. */
82 VEC(tree,heap) *access_fns;
87 /* The alias information that should be used for new pointers to this
88 location. SYMBOL_TAG is either a DECL or a SYMBOL_MEMORY_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. */
101 /* A pointer to the statement that contains this DR. */
104 /* A pointer to the memory reference. */
107 /* Auxiliary info specific to a pass. */
110 /* True when the data reference is in RHS of a stmt. */
113 /* Behavior of the memory reference in the innermost loop. */
114 struct innermost_loop_behavior innermost;
116 /* Decomposition to indices for alias analysis. */
117 struct indices indices;
119 /* Alias information for the data reference. */
120 struct dr_alias alias;
123 typedef struct data_reference *data_reference_p;
124 DEF_VEC_P(data_reference_p);
125 DEF_VEC_ALLOC_P (data_reference_p, heap);
127 #define DR_STMT(DR) (DR)->stmt
128 #define DR_REF(DR) (DR)->ref
129 #define DR_BASE_OBJECT(DR) (DR)->indices.base_object
130 #define DR_ACCESS_FNS(DR) (DR)->indices.access_fns
131 #define DR_ACCESS_FN(DR, I) VEC_index (tree, DR_ACCESS_FNS (DR), I)
132 #define DR_NUM_DIMENSIONS(DR) VEC_length (tree, DR_ACCESS_FNS (DR))
133 #define DR_IS_READ(DR) (DR)->is_read
134 #define DR_BASE_ADDRESS(DR) (DR)->innermost.base_address
135 #define DR_OFFSET(DR) (DR)->innermost.offset
136 #define DR_INIT(DR) (DR)->innermost.init
137 #define DR_STEP(DR) (DR)->innermost.step
138 #define DR_SYMBOL_TAG(DR) (DR)->alias.symbol_tag
139 #define DR_PTR_INFO(DR) (DR)->alias.ptr_info
140 #define DR_SUBVARS(DR) (DR)->alias.subvars
141 #define DR_VOPS(DR) (DR)->alias.vops
142 #define DR_ALIGNED_TO(DR) (DR)->innermost.aligned_to
144 enum data_dependence_direction {
148 dir_positive_or_negative,
149 dir_positive_or_equal,
150 dir_negative_or_equal,
155 /* The description of the grid of iterations that overlap. At most
156 two loops are considered at the same time just now, hence at most
157 two functions are needed. For each of the functions, we store
158 the vector of coefficients, f[0] + x * f[1] + y * f[2] + ...,
159 where x, y, ... are variables. */
163 /* Special values of N. */
164 #define NO_DEPENDENCE 0
165 #define NOT_KNOWN (MAX_DIM + 1)
166 #define CF_NONTRIVIAL_P(CF) ((CF)->n != NO_DEPENDENCE && (CF)->n != NOT_KNOWN)
167 #define CF_NOT_KNOWN_P(CF) ((CF)->n == NOT_KNOWN)
168 #define CF_NO_DEPENDENCE_P(CF) ((CF)->n == NO_DEPENDENCE)
170 typedef VEC (tree, heap) *affine_fn;
175 affine_fn fns[MAX_DIM];
178 /* What is a subscript? Given two array accesses a subscript is the
179 tuple composed of the access functions for a given dimension.
180 Example: Given A[f1][f2][f3] and B[g1][g2][g3], there are three
181 subscripts: (f1, g1), (f2, g2), (f3, g3). These three subscripts
182 are stored in the data_dependence_relation structure under the form
183 of an array of subscripts. */
187 /* A description of the iterations for which the elements are
189 conflict_function *conflicting_iterations_in_a;
190 conflict_function *conflicting_iterations_in_b;
192 /* This field stores the information about the iteration domain
193 validity of the dependence relation. */
196 /* Distance from the iteration that access a conflicting element in
197 A to the iteration that access this same conflicting element in
198 B. The distance is a tree scalar expression, i.e. a constant or a
199 symbolic expression, but certainly not a chrec function. */
203 typedef struct subscript *subscript_p;
204 DEF_VEC_P(subscript_p);
205 DEF_VEC_ALLOC_P (subscript_p, heap);
207 #define SUB_CONFLICTS_IN_A(SUB) SUB->conflicting_iterations_in_a
208 #define SUB_CONFLICTS_IN_B(SUB) SUB->conflicting_iterations_in_b
209 #define SUB_LAST_CONFLICT(SUB) SUB->last_conflict
210 #define SUB_DISTANCE(SUB) SUB->distance
212 /* A data_dependence_relation represents a relation between two
213 data_references A and B. */
215 struct data_dependence_relation
218 struct data_reference *a;
219 struct data_reference *b;
221 /* When the dependence relation is affine, it can be represented by
222 a distance vector. */
225 /* A "yes/no/maybe" field for the dependence relation:
227 - when "ARE_DEPENDENT == NULL_TREE", there exist a dependence
228 relation between A and B, and the description of this relation
229 is given in the SUBSCRIPTS array,
231 - when "ARE_DEPENDENT == chrec_known", there is no dependence and
234 - when "ARE_DEPENDENT == chrec_dont_know", there may be a dependence,
235 but the analyzer cannot be more specific. */
238 /* For each subscript in the dependence test, there is an element in
239 this array. This is the attribute that labels the edge A->B of
240 the data_dependence_relation. */
241 VEC (subscript_p, heap) *subscripts;
243 /* The analyzed loop nest. */
244 VEC (loop_p, heap) *loop_nest;
246 /* An index in loop_nest for the innermost loop that varies for
247 this data dependence relation. */
250 /* The classic direction vector. */
251 VEC (lambda_vector, heap) *dir_vects;
253 /* The classic distance vector. */
254 VEC (lambda_vector, heap) *dist_vects;
256 /* Is the dependence reversed with respect to the lexicographic order? */
260 typedef struct data_dependence_relation *ddr_p;
262 DEF_VEC_ALLOC_P(ddr_p,heap);
264 #define DDR_A(DDR) DDR->a
265 #define DDR_B(DDR) DDR->b
266 #define DDR_AFFINE_P(DDR) DDR->affine_p
267 #define DDR_ARE_DEPENDENT(DDR) DDR->are_dependent
268 #define DDR_SUBSCRIPTS(DDR) DDR->subscripts
269 #define DDR_SUBSCRIPT(DDR, I) VEC_index (subscript_p, DDR_SUBSCRIPTS (DDR), I)
270 #define DDR_NUM_SUBSCRIPTS(DDR) VEC_length (subscript_p, DDR_SUBSCRIPTS (DDR))
272 #define DDR_LOOP_NEST(DDR) DDR->loop_nest
273 /* The size of the direction/distance vectors: the number of loops in
275 #define DDR_NB_LOOPS(DDR) (VEC_length (loop_p, DDR_LOOP_NEST (DDR)))
276 #define DDR_INNER_LOOP(DDR) DDR->inner_loop
278 #define DDR_DIST_VECTS(DDR) ((DDR)->dist_vects)
279 #define DDR_DIR_VECTS(DDR) ((DDR)->dir_vects)
280 #define DDR_NUM_DIST_VECTS(DDR) \
281 (VEC_length (lambda_vector, DDR_DIST_VECTS (DDR)))
282 #define DDR_NUM_DIR_VECTS(DDR) \
283 (VEC_length (lambda_vector, DDR_DIR_VECTS (DDR)))
284 #define DDR_DIR_VECT(DDR, I) \
285 VEC_index (lambda_vector, DDR_DIR_VECTS (DDR), I)
286 #define DDR_DIST_VECT(DDR, I) \
287 VEC_index (lambda_vector, DDR_DIST_VECTS (DDR), I)
288 #define DDR_REVERSED_P(DDR) DDR->reversed_p
292 /* Describes a location of a memory reference. */
294 typedef struct data_ref_loc_d
296 /* Position of the memory reference. */
299 /* True if the memory reference is read. */
303 DEF_VEC_O (data_ref_loc);
304 DEF_VEC_ALLOC_O (data_ref_loc, heap);
306 bool get_references_in_stmt (tree, VEC (data_ref_loc, heap) **);
307 void dr_analyze_innermost (struct data_reference *);
308 extern void compute_data_dependences_for_loop (struct loop *, bool,
309 VEC (data_reference_p, heap) **,
310 VEC (ddr_p, heap) **);
311 extern void print_direction_vector (FILE *, lambda_vector, int);
312 extern void print_dir_vectors (FILE *, VEC (lambda_vector, heap) *, int);
313 extern void print_dist_vectors (FILE *, VEC (lambda_vector, heap) *, int);
314 extern void dump_subscript (FILE *, struct subscript *);
315 extern void dump_ddrs (FILE *, VEC (ddr_p, heap) *);
316 extern void dump_dist_dir_vectors (FILE *, VEC (ddr_p, heap) *);
317 extern void dump_data_reference (FILE *, struct data_reference *);
318 extern void dump_data_references (FILE *, VEC (data_reference_p, heap) *);
319 extern void debug_data_dependence_relation (struct data_dependence_relation *);
320 extern void dump_data_dependence_relation (FILE *,
321 struct data_dependence_relation *);
322 extern void dump_data_dependence_relations (FILE *, VEC (ddr_p, heap) *);
323 extern void dump_data_dependence_direction (FILE *,
324 enum data_dependence_direction);
325 extern void free_dependence_relation (struct data_dependence_relation *);
326 extern void free_dependence_relations (VEC (ddr_p, heap) *);
327 extern void free_data_refs (VEC (data_reference_p, heap) *);
328 struct data_reference *create_data_ref (struct loop *, tree, tree, bool);
329 bool find_loop_nest (struct loop *, VEC (loop_p, heap) **);
330 void compute_all_dependences (VEC (data_reference_p, heap) *,
331 VEC (ddr_p, heap) **, VEC (loop_p, heap) *, bool);
335 /* A RDG vertex representing a statement. */
336 typedef struct rdg_vertex
338 /* The statement represented by this vertex. */
342 #define RDGV_STMT(V) ((struct rdg_vertex *) ((V)->data))->stmt
344 /* Data dependence type. */
348 /* Read After Write (RAW). */
351 /* Write After Read (WAR). */
354 /* Write After Write (WAW). */
357 /* Read After Read (RAR). */
361 /* Dependence information attached to an edge of the RDG. */
363 typedef struct rdg_edge
365 /* Type of the dependence. */
366 enum rdg_dep_type type;
369 #define RDGE_TYPE(E) ((struct rdg_edge *) ((E)->data))->type
371 struct graph *build_rdg (struct loop *);
373 /* Return the index of the variable VAR in the LOOP_NEST array. */
376 index_in_loop_nest (int var, VEC (loop_p, heap) *loop_nest)
381 for (var_index = 0; VEC_iterate (loop_p, loop_nest, var_index, loopi);
383 if (loopi->num == var)
389 /* In lambda-code.c */
390 bool lambda_transform_legal_p (lambda_trans_matrix, int, VEC (ddr_p, heap) *);
392 #endif /* GCC_TREE_DATA_REF_H */