PR c++/65051
[official-gcc.git] / gcc / tree-data-ref.h
blob3c4569072b74a1fc644bf99d456db909fd1028d7
1 /* Data references and dependences detectors.
2 Copyright (C) 2003-2015 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 "omega.h"
26 #include "tree-chrec.h"
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
35 for (i = 0; i < 100; i++)
36 for (j = 3; j < 100; j++)
38 Example 1 Example 2
39 data-ref a[j].b[i][j] *(p + x + 16B + 4B * j)
42 innermost_loop_behavior
43 base_address &a p
44 offset i * D_i x
45 init 3 * D_j + offsetof (b) 28
46 step D_j 4
49 struct innermost_loop_behavior
51 tree base_address;
52 tree offset;
53 tree init;
54 tree step;
56 /* Alignment information. ALIGNED_TO is set to the largest power of two
57 that divides OFFSET. */
58 tree aligned_to;
61 /* Describes the evolutions of indices of the memory reference. The indices
62 are indices of the ARRAY_REFs, indexes in artificial dimensions
63 added for member selection of records and the operands of MEM_REFs.
64 BASE_OBJECT is the part of the reference that is loop-invariant
65 (note that this reference does not have to cover the whole object
66 being accessed, in which case UNCONSTRAINED_BASE is set; hence it is
67 not recommended to use BASE_OBJECT in any code generation).
68 For the examples above,
70 base_object: a *(p + x + 4B * j_0)
71 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> access_fns;
86 struct dr_alias
88 /* The alias information that should be used for new pointers to this
89 location. */
90 struct ptr_info_def *ptr_info;
93 /* An integer vector. A vector formally consists of an element of a vector
94 space. A vector space is a set that is closed under vector addition
95 and scalar multiplication. In this vector space, an element is a list of
96 integers. */
97 typedef int *lambda_vector;
99 /* An integer matrix. A matrix consists of m vectors of length n (IE
100 all vectors are the same length). */
101 typedef lambda_vector *lambda_matrix;
103 /* Each vector of the access matrix represents a linear access
104 function for a subscript. First elements correspond to the
105 leftmost indices, ie. for a[i][j] the first vector corresponds to
106 the subscript in "i". The elements of a vector are relative to
107 the loop nests in which the data reference is considered,
108 i.e. the vector is relative to the SCoP that provides the context
109 in which this data reference occurs.
111 For example, in
113 | loop_1
114 | loop_2
115 | a[i+3][2*j+n-1]
117 if "i" varies in loop_1 and "j" varies in loop_2, the access
118 matrix with respect to the loop nest {loop_1, loop_2} is:
120 | loop_1 loop_2 param_n cst
121 | 1 0 0 3
122 | 0 2 1 -1
124 whereas the access matrix with respect to loop_2 considers "i" as
125 a parameter:
127 | loop_2 param_i param_n cst
128 | 0 1 0 3
129 | 2 0 1 -1
131 struct access_matrix
133 vec<loop_p> loop_nest;
134 int nb_induction_vars;
135 vec<tree> parameters;
136 vec<lambda_vector, va_gc> *matrix;
139 #define AM_LOOP_NEST(M) (M)->loop_nest
140 #define AM_NB_INDUCTION_VARS(M) (M)->nb_induction_vars
141 #define AM_PARAMETERS(M) (M)->parameters
142 #define AM_MATRIX(M) (M)->matrix
143 #define AM_NB_PARAMETERS(M) (AM_PARAMETERS (M)).length ()
144 #define AM_CONST_COLUMN_INDEX(M) (AM_NB_INDUCTION_VARS (M) + AM_NB_PARAMETERS (M))
145 #define AM_NB_COLUMNS(M) (AM_NB_INDUCTION_VARS (M) + AM_NB_PARAMETERS (M) + 1)
146 #define AM_GET_SUBSCRIPT_ACCESS_VECTOR(M, I) AM_MATRIX (M)[I]
147 #define AM_GET_ACCESS_MATRIX_ELEMENT(M, I, J) AM_GET_SUBSCRIPT_ACCESS_VECTOR (M, I)[J]
149 /* Return the column in the access matrix of LOOP_NUM. */
151 static inline int
152 am_vector_index_for_loop (struct access_matrix *access_matrix, int loop_num)
154 int i;
155 loop_p l;
157 for (i = 0; AM_LOOP_NEST (access_matrix).iterate (i, &l); i++)
158 if (l->num == loop_num)
159 return i;
161 gcc_unreachable ();
164 struct data_reference
166 /* A pointer to the statement that contains this DR. */
167 gimple stmt;
169 /* A pointer to the memory reference. */
170 tree ref;
172 /* Auxiliary info specific to a pass. */
173 void *aux;
175 /* True when the data reference is in RHS of a stmt. */
176 bool is_read;
178 /* Behavior of the memory reference in the innermost loop. */
179 struct innermost_loop_behavior innermost;
181 /* Subscripts of this data reference. */
182 struct indices indices;
184 /* Alias information for the data reference. */
185 struct dr_alias alias;
187 /* Matrix representation for the data access functions. */
188 struct access_matrix *access_matrix;
191 #define DR_STMT(DR) (DR)->stmt
192 #define DR_REF(DR) (DR)->ref
193 #define DR_BASE_OBJECT(DR) (DR)->indices.base_object
194 #define DR_ACCESS_FNS(DR) (DR)->indices.access_fns
195 #define DR_ACCESS_FN(DR, I) DR_ACCESS_FNS (DR)[I]
196 #define DR_NUM_DIMENSIONS(DR) DR_ACCESS_FNS (DR).length ()
197 #define DR_IS_READ(DR) (DR)->is_read
198 #define DR_IS_WRITE(DR) (!DR_IS_READ (DR))
199 #define DR_BASE_ADDRESS(DR) (DR)->innermost.base_address
200 #define DR_OFFSET(DR) (DR)->innermost.offset
201 #define DR_INIT(DR) (DR)->innermost.init
202 #define DR_STEP(DR) (DR)->innermost.step
203 #define DR_PTR_INFO(DR) (DR)->alias.ptr_info
204 #define DR_ALIGNED_TO(DR) (DR)->innermost.aligned_to
205 #define DR_ACCESS_MATRIX(DR) (DR)->access_matrix
207 typedef struct data_reference *data_reference_p;
209 enum data_dependence_direction {
210 dir_positive,
211 dir_negative,
212 dir_equal,
213 dir_positive_or_negative,
214 dir_positive_or_equal,
215 dir_negative_or_equal,
216 dir_star,
217 dir_independent
220 /* The description of the grid of iterations that overlap. At most
221 two loops are considered at the same time just now, hence at most
222 two functions are needed. For each of the functions, we store
223 the vector of coefficients, f[0] + x * f[1] + y * f[2] + ...,
224 where x, y, ... are variables. */
226 #define MAX_DIM 2
228 /* Special values of N. */
229 #define NO_DEPENDENCE 0
230 #define NOT_KNOWN (MAX_DIM + 1)
231 #define CF_NONTRIVIAL_P(CF) ((CF)->n != NO_DEPENDENCE && (CF)->n != NOT_KNOWN)
232 #define CF_NOT_KNOWN_P(CF) ((CF)->n == NOT_KNOWN)
233 #define CF_NO_DEPENDENCE_P(CF) ((CF)->n == NO_DEPENDENCE)
235 typedef vec<tree> affine_fn;
237 struct conflict_function
239 unsigned n;
240 affine_fn fns[MAX_DIM];
243 /* What is a subscript? Given two array accesses a subscript is the
244 tuple composed of the access functions for a given dimension.
245 Example: Given A[f1][f2][f3] and B[g1][g2][g3], there are three
246 subscripts: (f1, g1), (f2, g2), (f3, g3). These three subscripts
247 are stored in the data_dependence_relation structure under the form
248 of an array of subscripts. */
250 struct subscript
252 /* A description of the iterations for which the elements are
253 accessed twice. */
254 conflict_function *conflicting_iterations_in_a;
255 conflict_function *conflicting_iterations_in_b;
257 /* This field stores the information about the iteration domain
258 validity of the dependence relation. */
259 tree last_conflict;
261 /* Distance from the iteration that access a conflicting element in
262 A to the iteration that access this same conflicting element in
263 B. The distance is a tree scalar expression, i.e. a constant or a
264 symbolic expression, but certainly not a chrec function. */
265 tree distance;
268 typedef struct subscript *subscript_p;
270 #define SUB_CONFLICTS_IN_A(SUB) SUB->conflicting_iterations_in_a
271 #define SUB_CONFLICTS_IN_B(SUB) SUB->conflicting_iterations_in_b
272 #define SUB_LAST_CONFLICT(SUB) SUB->last_conflict
273 #define SUB_DISTANCE(SUB) SUB->distance
275 /* A data_dependence_relation represents a relation between two
276 data_references A and B. */
278 struct data_dependence_relation
281 struct data_reference *a;
282 struct data_reference *b;
284 /* A "yes/no/maybe" field for the dependence relation:
286 - when "ARE_DEPENDENT == NULL_TREE", there exist a dependence
287 relation between A and B, and the description of this relation
288 is given in the SUBSCRIPTS array,
290 - when "ARE_DEPENDENT == chrec_known", there is no dependence and
291 SUBSCRIPTS is empty,
293 - when "ARE_DEPENDENT == chrec_dont_know", there may be a dependence,
294 but the analyzer cannot be more specific. */
295 tree are_dependent;
297 /* For each subscript in the dependence test, there is an element in
298 this array. This is the attribute that labels the edge A->B of
299 the data_dependence_relation. */
300 vec<subscript_p> subscripts;
302 /* The analyzed loop nest. */
303 vec<loop_p> loop_nest;
305 /* The classic direction vector. */
306 vec<lambda_vector> dir_vects;
308 /* The classic distance vector. */
309 vec<lambda_vector> dist_vects;
311 /* An index in loop_nest for the innermost loop that varies for
312 this data dependence relation. */
313 unsigned inner_loop;
315 /* Is the dependence reversed with respect to the lexicographic order? */
316 bool reversed_p;
318 /* When the dependence relation is affine, it can be represented by
319 a distance vector. */
320 bool affine_p;
322 /* Set to true when the dependence relation is on the same data
323 access. */
324 bool self_reference_p;
327 typedef struct data_dependence_relation *ddr_p;
329 #define DDR_A(DDR) DDR->a
330 #define DDR_B(DDR) DDR->b
331 #define DDR_AFFINE_P(DDR) DDR->affine_p
332 #define DDR_ARE_DEPENDENT(DDR) DDR->are_dependent
333 #define DDR_SUBSCRIPTS(DDR) DDR->subscripts
334 #define DDR_SUBSCRIPT(DDR, I) DDR_SUBSCRIPTS (DDR)[I]
335 #define DDR_NUM_SUBSCRIPTS(DDR) DDR_SUBSCRIPTS (DDR).length ()
337 #define DDR_LOOP_NEST(DDR) DDR->loop_nest
338 /* The size of the direction/distance vectors: the number of loops in
339 the loop nest. */
340 #define DDR_NB_LOOPS(DDR) (DDR_LOOP_NEST (DDR).length ())
341 #define DDR_INNER_LOOP(DDR) DDR->inner_loop
342 #define DDR_SELF_REFERENCE(DDR) DDR->self_reference_p
344 #define DDR_DIST_VECTS(DDR) ((DDR)->dist_vects)
345 #define DDR_DIR_VECTS(DDR) ((DDR)->dir_vects)
346 #define DDR_NUM_DIST_VECTS(DDR) \
347 (DDR_DIST_VECTS (DDR).length ())
348 #define DDR_NUM_DIR_VECTS(DDR) \
349 (DDR_DIR_VECTS (DDR).length ())
350 #define DDR_DIR_VECT(DDR, I) \
351 DDR_DIR_VECTS (DDR)[I]
352 #define DDR_DIST_VECT(DDR, I) \
353 DDR_DIST_VECTS (DDR)[I]
354 #define DDR_REVERSED_P(DDR) DDR->reversed_p
357 bool dr_analyze_innermost (struct data_reference *, struct loop *);
358 extern bool compute_data_dependences_for_loop (struct loop *, bool,
359 vec<loop_p> *,
360 vec<data_reference_p> *,
361 vec<ddr_p> *);
362 extern bool compute_data_dependences_for_bb (basic_block, bool,
363 vec<data_reference_p> *,
364 vec<ddr_p> *);
365 extern void debug_ddrs (vec<ddr_p> );
366 extern void dump_data_reference (FILE *, struct data_reference *);
367 extern void debug (data_reference &ref);
368 extern void debug (data_reference *ptr);
369 extern void debug_data_reference (struct data_reference *);
370 extern void debug_data_references (vec<data_reference_p> );
371 extern void debug (vec<data_reference_p> &ref);
372 extern void debug (vec<data_reference_p> *ptr);
373 extern void debug_data_dependence_relation (struct data_dependence_relation *);
374 extern void dump_data_dependence_relations (FILE *, vec<ddr_p> );
375 extern void debug (vec<ddr_p> &ref);
376 extern void debug (vec<ddr_p> *ptr);
377 extern void debug_data_dependence_relations (vec<ddr_p> );
378 extern void free_dependence_relation (struct data_dependence_relation *);
379 extern void free_dependence_relations (vec<ddr_p> );
380 extern void free_data_ref (data_reference_p);
381 extern void free_data_refs (vec<data_reference_p> );
382 extern bool find_data_references_in_stmt (struct loop *, gimple,
383 vec<data_reference_p> *);
384 extern bool graphite_find_data_references_in_stmt (loop_p, loop_p, gimple,
385 vec<data_reference_p> *);
386 tree find_data_references_in_loop (struct loop *, vec<data_reference_p> *);
387 struct data_reference *create_data_ref (loop_p, loop_p, tree, gimple, bool);
388 extern bool find_loop_nest (struct loop *, vec<loop_p> *);
389 extern struct data_dependence_relation *initialize_data_dependence_relation
390 (struct data_reference *, struct data_reference *, vec<loop_p>);
391 extern void compute_affine_dependence (struct data_dependence_relation *,
392 loop_p);
393 extern void compute_self_dependence (struct data_dependence_relation *);
394 extern bool compute_all_dependences (vec<data_reference_p> ,
395 vec<ddr_p> *,
396 vec<loop_p>, bool);
397 extern tree find_data_references_in_bb (struct loop *, basic_block,
398 vec<data_reference_p> *);
400 extern bool dr_may_alias_p (const struct data_reference *,
401 const struct data_reference *, bool);
402 extern bool dr_equal_offsets_p (struct data_reference *,
403 struct data_reference *);
404 extern void tree_check_data_deps (void);
407 /* Return true when the base objects of data references A and B are
408 the same memory object. */
410 static inline bool
411 same_data_refs_base_objects (data_reference_p a, data_reference_p b)
413 return DR_NUM_DIMENSIONS (a) == DR_NUM_DIMENSIONS (b)
414 && operand_equal_p (DR_BASE_OBJECT (a), DR_BASE_OBJECT (b), 0);
417 /* Return true when the data references A and B are accessing the same
418 memory object with the same access functions. */
420 static inline bool
421 same_data_refs (data_reference_p a, data_reference_p b)
423 unsigned int i;
425 /* The references are exactly the same. */
426 if (operand_equal_p (DR_REF (a), DR_REF (b), 0))
427 return true;
429 if (!same_data_refs_base_objects (a, b))
430 return false;
432 for (i = 0; i < DR_NUM_DIMENSIONS (a); i++)
433 if (!eq_evolutions_p (DR_ACCESS_FN (a, i), DR_ACCESS_FN (b, i)))
434 return false;
436 return true;
439 /* Return true when the DDR contains two data references that have the
440 same access functions. */
442 static inline bool
443 same_access_functions (const struct data_dependence_relation *ddr)
445 unsigned i;
447 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
448 if (!eq_evolutions_p (DR_ACCESS_FN (DDR_A (ddr), i),
449 DR_ACCESS_FN (DDR_B (ddr), i)))
450 return false;
452 return true;
455 /* Returns true when all the dependences are computable. */
457 inline bool
458 known_dependences_p (vec<ddr_p> dependence_relations)
460 ddr_p ddr;
461 unsigned int i;
463 FOR_EACH_VEC_ELT (dependence_relations, i, ddr)
464 if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
465 return false;
467 return true;
470 /* Returns the dependence level for a vector DIST of size LENGTH.
471 LEVEL = 0 means a lexicographic dependence, i.e. a dependence due
472 to the sequence of statements, not carried by any loop. */
474 static inline unsigned
475 dependence_level (lambda_vector dist_vect, int length)
477 int i;
479 for (i = 0; i < length; i++)
480 if (dist_vect[i] != 0)
481 return i + 1;
483 return 0;
486 /* Return the dependence level for the DDR relation. */
488 static inline unsigned
489 ddr_dependence_level (ddr_p ddr)
491 unsigned vector;
492 unsigned level = 0;
494 if (DDR_DIST_VECTS (ddr).exists ())
495 level = dependence_level (DDR_DIST_VECT (ddr, 0), DDR_NB_LOOPS (ddr));
497 for (vector = 1; vector < DDR_NUM_DIST_VECTS (ddr); vector++)
498 level = MIN (level, dependence_level (DDR_DIST_VECT (ddr, vector),
499 DDR_NB_LOOPS (ddr)));
500 return level;
503 /* Return the index of the variable VAR in the LOOP_NEST array. */
505 static inline int
506 index_in_loop_nest (int var, vec<loop_p> loop_nest)
508 struct loop *loopi;
509 int var_index;
511 for (var_index = 0; loop_nest.iterate (var_index, &loopi);
512 var_index++)
513 if (loopi->num == var)
514 break;
516 return var_index;
519 /* Returns true when the data reference DR the form "A[i] = ..."
520 with a stride equal to its unit type size. */
522 static inline bool
523 adjacent_dr_p (struct data_reference *dr)
525 /* If this is a bitfield store bail out. */
526 if (TREE_CODE (DR_REF (dr)) == COMPONENT_REF
527 && DECL_BIT_FIELD (TREE_OPERAND (DR_REF (dr), 1)))
528 return false;
530 if (!DR_STEP (dr)
531 || TREE_CODE (DR_STEP (dr)) != INTEGER_CST)
532 return false;
534 return tree_int_cst_equal (fold_unary (ABS_EXPR, TREE_TYPE (DR_STEP (dr)),
535 DR_STEP (dr)),
536 TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dr))));
539 void split_constant_offset (tree , tree *, tree *);
541 /* Compute the greatest common divisor of a VECTOR of SIZE numbers. */
543 static inline int
544 lambda_vector_gcd (lambda_vector vector, int size)
546 int i;
547 int gcd1 = 0;
549 if (size > 0)
551 gcd1 = vector[0];
552 for (i = 1; i < size; i++)
553 gcd1 = gcd (gcd1, vector[i]);
555 return gcd1;
558 /* Allocate a new vector of given SIZE. */
560 static inline lambda_vector
561 lambda_vector_new (int size)
563 return ggc_cleared_vec_alloc<int> (size);
566 /* Clear out vector VEC1 of length SIZE. */
568 static inline void
569 lambda_vector_clear (lambda_vector vec1, int size)
571 memset (vec1, 0, size * sizeof (*vec1));
574 /* Returns true when the vector V is lexicographically positive, in
575 other words, when the first nonzero element is positive. */
577 static inline bool
578 lambda_vector_lexico_pos (lambda_vector v,
579 unsigned n)
581 unsigned i;
582 for (i = 0; i < n; i++)
584 if (v[i] == 0)
585 continue;
586 if (v[i] < 0)
587 return false;
588 if (v[i] > 0)
589 return true;
591 return true;
594 /* Return true if vector VEC1 of length SIZE is the zero vector. */
596 static inline bool
597 lambda_vector_zerop (lambda_vector vec1, int size)
599 int i;
600 for (i = 0; i < size; i++)
601 if (vec1[i] != 0)
602 return false;
603 return true;
606 /* Allocate a matrix of M rows x N cols. */
608 static inline lambda_matrix
609 lambda_matrix_new (int m, int n, struct obstack *lambda_obstack)
611 lambda_matrix mat;
612 int i;
614 mat = (lambda_matrix) obstack_alloc (lambda_obstack,
615 sizeof (lambda_vector *) * m);
617 for (i = 0; i < m; i++)
618 mat[i] = lambda_vector_new (n);
620 return mat;
623 #endif /* GCC_TREE_DATA_REF_H */