]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/tree-data-ref.h
switch from gimple to gimple*
[thirdparty/gcc.git] / gcc / tree-data-ref.h
CommitLineData
b8698a0f 1/* Data references and dependences detectors.
5624e564 2 Copyright (C) 2003-2015 Free Software Foundation, Inc.
0ff4040e 3 Contributed by Sebastian Pop <pop@cri.ensmp.fr>
56cf8686
SP
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9dcd6f09 9Software Foundation; either version 3, or (at your option) any later
56cf8686
SP
10version.
11
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
16
17You should have received a copy of the GNU General Public License
9dcd6f09
NC
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
56cf8686
SP
20
21#ifndef GCC_TREE_DATA_REF_H
22#define GCC_TREE_DATA_REF_H
23
3a796c6f 24#include "graphds.h"
dea61d92 25#include "tree-chrec.h"
36d59cf7 26
98b44b0e 27/*
3cb960c7
ZD
28 innermost_loop_behavior describes the evolution of the address of the memory
29 reference in the innermost enclosing loop. The address is expressed as
30 BASE + STEP * # of iteration, and base is further decomposed as the base
31 pointer (BASE_ADDRESS), loop invariant offset (OFFSET) and
b8698a0f
L
32 constant offset (INIT). Examples, in loop nest
33
3cb960c7
ZD
34 for (i = 0; i < 100; i++)
35 for (j = 3; j < 100; j++)
98b44b0e 36
86a07404 37 Example 1 Example 2
3cb960c7 38 data-ref a[j].b[i][j] *(p + x + 16B + 4B * j)
b8698a0f 39
dea61d92 40
3cb960c7
ZD
41 innermost_loop_behavior
42 base_address &a p
43 offset i * D_i x
44 init 3 * D_j + offsetof (b) 28
86a07404 45 step D_j 4
86a07404 46
98b44b0e 47 */
3cb960c7 48struct innermost_loop_behavior
86a07404
IR
49{
50 tree base_address;
51 tree offset;
52 tree init;
53 tree step;
3cb960c7
ZD
54
55 /* Alignment information. ALIGNED_TO is set to the largest power of two
56 that divides OFFSET. */
57 tree aligned_to;
86a07404
IR
58};
59
3cb960c7 60/* Describes the evolutions of indices of the memory reference. The indices
c4ddde1b
RG
61 are indices of the ARRAY_REFs, indexes in artificial dimensions
62 added for member selection of records and the operands of MEM_REFs.
63 BASE_OBJECT is the part of the reference that is loop-invariant
64 (note that this reference does not have to cover the whole object
65 being accessed, in which case UNCONSTRAINED_BASE is set; hence it is
66 not recommended to use BASE_OBJECT in any code generation).
67 For the examples above,
68
69 base_object: a *(p + x + 4B * j_0)
3cb960c7 70 indices: {j_0, +, 1}_2 {16, +, 4}_2
c4ddde1b 71 4
3cb960c7
ZD
72 {i_0, +, 1}_1
73 {j_0, +, 1}_2
74*/
75
76struct indices
86a07404
IR
77{
78 /* The object. */
79 tree base_object;
b8698a0f 80
3cb960c7 81 /* A list of chrecs. Access functions of the indices. */
9771b263 82 vec<tree> access_fns;
f3ae4add
RB
83
84 /* Whether BASE_OBJECT is an access representing the whole object
85 or whether the access could not be constrained. */
86 bool unconstrained_base;
86a07404
IR
87};
88
3cb960c7
ZD
89struct dr_alias
90{
91 /* The alias information that should be used for new pointers to this
c4ddde1b 92 location. */
3cb960c7 93 struct ptr_info_def *ptr_info;
86a07404
IR
94};
95
b305e3da
SP
96/* An integer vector. A vector formally consists of an element of a vector
97 space. A vector space is a set that is closed under vector addition
98 and scalar multiplication. In this vector space, an element is a list of
99 integers. */
100typedef int *lambda_vector;
b305e3da
SP
101
102/* An integer matrix. A matrix consists of m vectors of length n (IE
103 all vectors are the same length). */
104typedef lambda_vector *lambda_matrix;
105
9f275479 106
9f275479 107
36d59cf7 108struct data_reference
56cf8686 109{
56cf8686 110 /* A pointer to the statement that contains this DR. */
355fe088 111 gimple *stmt;
b8698a0f 112
3cb960c7 113 /* A pointer to the memory reference. */
56cf8686
SP
114 tree ref;
115
56cf8686 116 /* Auxiliary info specific to a pass. */
5417e022 117 void *aux;
56cf8686
SP
118
119 /* True when the data reference is in RHS of a stmt. */
120 bool is_read;
121
3cb960c7
ZD
122 /* Behavior of the memory reference in the innermost loop. */
123 struct innermost_loop_behavior innermost;
86a07404 124
f8bf9252 125 /* Subscripts of this data reference. */
3cb960c7 126 struct indices indices;
86a07404 127
3cb960c7
ZD
128 /* Alias information for the data reference. */
129 struct dr_alias alias;
9f275479 130};
ebf78a47 131
86a07404
IR
132#define DR_STMT(DR) (DR)->stmt
133#define DR_REF(DR) (DR)->ref
3cb960c7 134#define DR_BASE_OBJECT(DR) (DR)->indices.base_object
f3ae4add 135#define DR_UNCONSTRAINED_BASE(DR) (DR)->indices.unconstrained_base
3cb960c7 136#define DR_ACCESS_FNS(DR) (DR)->indices.access_fns
9771b263
DN
137#define DR_ACCESS_FN(DR, I) DR_ACCESS_FNS (DR)[I]
138#define DR_NUM_DIMENSIONS(DR) DR_ACCESS_FNS (DR).length ()
86a07404 139#define DR_IS_READ(DR) (DR)->is_read
b0af49c4 140#define DR_IS_WRITE(DR) (!DR_IS_READ (DR))
3cb960c7
ZD
141#define DR_BASE_ADDRESS(DR) (DR)->innermost.base_address
142#define DR_OFFSET(DR) (DR)->innermost.offset
143#define DR_INIT(DR) (DR)->innermost.init
144#define DR_STEP(DR) (DR)->innermost.step
3cb960c7 145#define DR_PTR_INFO(DR) (DR)->alias.ptr_info
3cb960c7 146#define DR_ALIGNED_TO(DR) (DR)->innermost.aligned_to
9f275479
JS
147
148typedef struct data_reference *data_reference_p;
56cf8686
SP
149
150enum data_dependence_direction {
b8698a0f
L
151 dir_positive,
152 dir_negative,
153 dir_equal,
56cf8686
SP
154 dir_positive_or_negative,
155 dir_positive_or_equal,
156 dir_negative_or_equal,
157 dir_star,
158 dir_independent
159};
160
d93817c4
ZD
161/* The description of the grid of iterations that overlap. At most
162 two loops are considered at the same time just now, hence at most
163 two functions are needed. For each of the functions, we store
164 the vector of coefficients, f[0] + x * f[1] + y * f[2] + ...,
165 where x, y, ... are variables. */
166
167#define MAX_DIM 2
168
169/* Special values of N. */
170#define NO_DEPENDENCE 0
171#define NOT_KNOWN (MAX_DIM + 1)
172#define CF_NONTRIVIAL_P(CF) ((CF)->n != NO_DEPENDENCE && (CF)->n != NOT_KNOWN)
173#define CF_NOT_KNOWN_P(CF) ((CF)->n == NOT_KNOWN)
174#define CF_NO_DEPENDENCE_P(CF) ((CF)->n == NO_DEPENDENCE)
175
9771b263 176typedef vec<tree> affine_fn;
d93817c4 177
84562394 178struct conflict_function
d93817c4
ZD
179{
180 unsigned n;
181 affine_fn fns[MAX_DIM];
84562394 182};
d93817c4 183
56cf8686
SP
184/* What is a subscript? Given two array accesses a subscript is the
185 tuple composed of the access functions for a given dimension.
186 Example: Given A[f1][f2][f3] and B[g1][g2][g3], there are three
187 subscripts: (f1, g1), (f2, g2), (f3, g3). These three subscripts
188 are stored in the data_dependence_relation structure under the form
189 of an array of subscripts. */
190
36d59cf7 191struct subscript
56cf8686
SP
192{
193 /* A description of the iterations for which the elements are
194 accessed twice. */
d93817c4
ZD
195 conflict_function *conflicting_iterations_in_a;
196 conflict_function *conflicting_iterations_in_b;
b8698a0f 197
86df10e3 198 /* This field stores the information about the iteration domain
56cf8686 199 validity of the dependence relation. */
86df10e3 200 tree last_conflict;
b8698a0f 201
56cf8686
SP
202 /* Distance from the iteration that access a conflicting element in
203 A to the iteration that access this same conflicting element in
89dbed81 204 B. The distance is a tree scalar expression, i.e. a constant or a
56cf8686
SP
205 symbolic expression, but certainly not a chrec function. */
206 tree distance;
56cf8686
SP
207};
208
ebf78a47 209typedef struct subscript *subscript_p;
ebf78a47 210
56cf8686
SP
211#define SUB_CONFLICTS_IN_A(SUB) SUB->conflicting_iterations_in_a
212#define SUB_CONFLICTS_IN_B(SUB) SUB->conflicting_iterations_in_b
86df10e3 213#define SUB_LAST_CONFLICT(SUB) SUB->last_conflict
56cf8686 214#define SUB_DISTANCE(SUB) SUB->distance
56cf8686
SP
215
216/* A data_dependence_relation represents a relation between two
217 data_references A and B. */
218
36d59cf7 219struct data_dependence_relation
56cf8686 220{
b8698a0f 221
56cf8686
SP
222 struct data_reference *a;
223 struct data_reference *b;
224
225 /* A "yes/no/maybe" field for the dependence relation:
b8698a0f 226
56cf8686
SP
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,
b8698a0f 230
56cf8686
SP
231 - when "ARE_DEPENDENT == chrec_known", there is no dependence and
232 SUBSCRIPTS is empty,
b8698a0f 233
56cf8686
SP
234 - when "ARE_DEPENDENT == chrec_dont_know", there may be a dependence,
235 but the analyzer cannot be more specific. */
236 tree are_dependent;
b8698a0f 237
56cf8686
SP
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. */
9771b263 241 vec<subscript_p> subscripts;
36d59cf7 242
ba42e045 243 /* The analyzed loop nest. */
9771b263 244 vec<loop_p> loop_nest;
86df10e3 245
36d59cf7 246 /* The classic direction vector. */
9771b263 247 vec<lambda_vector> dir_vects;
36d59cf7
DB
248
249 /* The classic distance vector. */
9771b263 250 vec<lambda_vector> dist_vects;
71d5b5e1 251
8f5929e1
JJ
252 /* An index in loop_nest for the innermost loop that varies for
253 this data dependence relation. */
254 unsigned inner_loop;
255
71d5b5e1
SP
256 /* Is the dependence reversed with respect to the lexicographic order? */
257 bool reversed_p;
8f5929e1
JJ
258
259 /* When the dependence relation is affine, it can be represented by
260 a distance vector. */
261 bool affine_p;
262
263 /* Set to true when the dependence relation is on the same data
264 access. */
265 bool self_reference_p;
56cf8686
SP
266};
267
0ff4040e 268typedef struct data_dependence_relation *ddr_p;
0ff4040e 269
56cf8686
SP
270#define DDR_A(DDR) DDR->a
271#define DDR_B(DDR) DDR->b
86df10e3 272#define DDR_AFFINE_P(DDR) DDR->affine_p
56cf8686
SP
273#define DDR_ARE_DEPENDENT(DDR) DDR->are_dependent
274#define DDR_SUBSCRIPTS(DDR) DDR->subscripts
9771b263
DN
275#define DDR_SUBSCRIPT(DDR, I) DDR_SUBSCRIPTS (DDR)[I]
276#define DDR_NUM_SUBSCRIPTS(DDR) DDR_SUBSCRIPTS (DDR).length ()
ba42e045
SP
277
278#define DDR_LOOP_NEST(DDR) DDR->loop_nest
279/* The size of the direction/distance vectors: the number of loops in
280 the loop nest. */
9771b263 281#define DDR_NB_LOOPS(DDR) (DDR_LOOP_NEST (DDR).length ())
3d8864c0 282#define DDR_INNER_LOOP(DDR) DDR->inner_loop
b3924be9 283#define DDR_SELF_REFERENCE(DDR) DDR->self_reference_p
304afda6
SP
284
285#define DDR_DIST_VECTS(DDR) ((DDR)->dist_vects)
286#define DDR_DIR_VECTS(DDR) ((DDR)->dir_vects)
287#define DDR_NUM_DIST_VECTS(DDR) \
9771b263 288 (DDR_DIST_VECTS (DDR).length ())
304afda6 289#define DDR_NUM_DIR_VECTS(DDR) \
9771b263 290 (DDR_DIR_VECTS (DDR).length ())
304afda6 291#define DDR_DIR_VECT(DDR, I) \
9771b263 292 DDR_DIR_VECTS (DDR)[I]
304afda6 293#define DDR_DIST_VECT(DDR, I) \
9771b263 294 DDR_DIST_VECTS (DDR)[I]
71d5b5e1 295#define DDR_REVERSED_P(DDR) DDR->reversed_p
56cf8686
SP
296
297\f
4e4452b6 298bool dr_analyze_innermost (struct data_reference *, struct loop *);
9f275479 299extern bool compute_data_dependences_for_loop (struct loop *, bool,
9771b263
DN
300 vec<loop_p> *,
301 vec<data_reference_p> *,
302 vec<ddr_p> *);
9771b263 303extern void debug_ddrs (vec<ddr_p> );
56cf8686 304extern void dump_data_reference (FILE *, struct data_reference *);
7b3b6ae4
LC
305extern void debug (data_reference &ref);
306extern void debug (data_reference *ptr);
a37d995a 307extern void debug_data_reference (struct data_reference *);
9771b263 308extern void debug_data_references (vec<data_reference_p> );
7b3b6ae4
LC
309extern void debug (vec<data_reference_p> &ref);
310extern void debug (vec<data_reference_p> *ptr);
ba42e045 311extern void debug_data_dependence_relation (struct data_dependence_relation *);
9771b263 312extern void dump_data_dependence_relations (FILE *, vec<ddr_p> );
7b3b6ae4
LC
313extern void debug (vec<ddr_p> &ref);
314extern void debug (vec<ddr_p> *ptr);
9771b263 315extern void debug_data_dependence_relations (vec<ddr_p> );
36d59cf7 316extern void free_dependence_relation (struct data_dependence_relation *);
9771b263 317extern void free_dependence_relations (vec<ddr_p> );
dea61d92 318extern void free_data_ref (data_reference_p);
9771b263 319extern void free_data_refs (vec<data_reference_p> );
355fe088 320extern bool find_data_references_in_stmt (struct loop *, gimple *,
9771b263 321 vec<data_reference_p> *);
355fe088 322extern bool graphite_find_data_references_in_stmt (loop_p, loop_p, gimple *,
9771b263 323 vec<data_reference_p> *);
fcac74a1 324tree find_data_references_in_loop (struct loop *, vec<data_reference_p> *);
74032f47 325bool loop_nest_has_data_refs (loop_p loop);
355fe088 326struct data_reference *create_data_ref (loop_p, loop_p, tree, gimple *, bool);
9771b263 327extern bool find_loop_nest (struct loop *, vec<loop_p> *);
aec7ae7d 328extern struct data_dependence_relation *initialize_data_dependence_relation
9771b263 329 (struct data_reference *, struct data_reference *, vec<loop_p>);
f20132e7
RG
330extern void compute_affine_dependence (struct data_dependence_relation *,
331 loop_p);
aec7ae7d 332extern void compute_self_dependence (struct data_dependence_relation *);
9771b263
DN
333extern bool compute_all_dependences (vec<data_reference_p> ,
334 vec<ddr_p> *,
335 vec<loop_p>, bool);
bfe068c3 336extern tree find_data_references_in_bb (struct loop *, basic_block,
9771b263 337 vec<data_reference_p> *);
f8bf9252 338
f8bf9252 339extern bool dr_may_alias_p (const struct data_reference *,
02f5d6c5 340 const struct data_reference *, bool);
bfe068c3
IR
341extern bool dr_equal_offsets_p (struct data_reference *,
342 struct data_reference *);
e1fd038a
SP
343
344/* Return true when the base objects of data references A and B are
345 the same memory object. */
346
347static inline bool
348same_data_refs_base_objects (data_reference_p a, data_reference_p b)
349{
350 return DR_NUM_DIMENSIONS (a) == DR_NUM_DIMENSIONS (b)
351 && operand_equal_p (DR_BASE_OBJECT (a), DR_BASE_OBJECT (b), 0);
352}
353
354/* Return true when the data references A and B are accessing the same
355 memory object with the same access functions. */
356
357static inline bool
358same_data_refs (data_reference_p a, data_reference_p b)
359{
360 unsigned int i;
361
362 /* The references are exactly the same. */
363 if (operand_equal_p (DR_REF (a), DR_REF (b), 0))
364 return true;
365
366 if (!same_data_refs_base_objects (a, b))
367 return false;
368
369 for (i = 0; i < DR_NUM_DIMENSIONS (a); i++)
370 if (!eq_evolutions_p (DR_ACCESS_FN (a, i), DR_ACCESS_FN (b, i)))
371 return false;
372
373 return true;
374}
375
dea61d92
SP
376/* Return true when the DDR contains two data references that have the
377 same access functions. */
378
379static inline bool
380same_access_functions (const struct data_dependence_relation *ddr)
381{
382 unsigned i;
383
384 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
385 if (!eq_evolutions_p (DR_ACCESS_FN (DDR_A (ddr), i),
386 DR_ACCESS_FN (DDR_B (ddr), i)))
387 return false;
388
389 return true;
390}
391
2fd5894f
RB
392/* Returns true when all the dependences are computable. */
393
394inline bool
395known_dependences_p (vec<ddr_p> dependence_relations)
396{
397 ddr_p ddr;
398 unsigned int i;
399
400 FOR_EACH_VEC_ELT (dependence_relations, i, ddr)
401 if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
402 return false;
403
404 return true;
405}
406
b305e3da
SP
407/* Returns the dependence level for a vector DIST of size LENGTH.
408 LEVEL = 0 means a lexicographic dependence, i.e. a dependence due
409 to the sequence of statements, not carried by any loop. */
410
411static inline unsigned
412dependence_level (lambda_vector dist_vect, int length)
413{
414 int i;
415
416 for (i = 0; i < length; i++)
417 if (dist_vect[i] != 0)
418 return i + 1;
419
420 return 0;
421}
422
dea61d92
SP
423/* Return the dependence level for the DDR relation. */
424
425static inline unsigned
426ddr_dependence_level (ddr_p ddr)
427{
428 unsigned vector;
429 unsigned level = 0;
430
9771b263 431 if (DDR_DIST_VECTS (ddr).exists ())
dea61d92
SP
432 level = dependence_level (DDR_DIST_VECT (ddr, 0), DDR_NB_LOOPS (ddr));
433
434 for (vector = 1; vector < DDR_NUM_DIST_VECTS (ddr); vector++)
435 level = MIN (level, dependence_level (DDR_DIST_VECT (ddr, vector),
436 DDR_NB_LOOPS (ddr)));
437 return level;
438}
439
ba42e045
SP
440/* Return the index of the variable VAR in the LOOP_NEST array. */
441
442static inline int
9771b263 443index_in_loop_nest (int var, vec<loop_p> loop_nest)
ba42e045
SP
444{
445 struct loop *loopi;
446 int var_index;
447
9771b263 448 for (var_index = 0; loop_nest.iterate (var_index, &loopi);
ba42e045
SP
449 var_index++)
450 if (loopi->num == var)
451 break;
452
453 return var_index;
454}
455
be6b029b
RG
456/* Returns true when the data reference DR the form "A[i] = ..."
457 with a stride equal to its unit type size. */
5e37ea0e
SP
458
459static inline bool
d0582dc1 460adjacent_dr_p (struct data_reference *dr)
5e37ea0e 461{
be6b029b
RG
462 /* If this is a bitfield store bail out. */
463 if (TREE_CODE (DR_REF (dr)) == COMPONENT_REF
464 && DECL_BIT_FIELD (TREE_OPERAND (DR_REF (dr), 1)))
465 return false;
466
467 if (!DR_STEP (dr)
468 || TREE_CODE (DR_STEP (dr)) != INTEGER_CST)
469 return false;
470
471 return tree_int_cst_equal (fold_unary (ABS_EXPR, TREE_TYPE (DR_STEP (dr)),
472 DR_STEP (dr)),
473 TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dr))));
5e37ea0e
SP
474}
475
468c2ac0
DN
476void split_constant_offset (tree , tree *, tree *);
477
b305e3da
SP
478/* Compute the greatest common divisor of a VECTOR of SIZE numbers. */
479
480static inline int
481lambda_vector_gcd (lambda_vector vector, int size)
482{
483 int i;
484 int gcd1 = 0;
485
486 if (size > 0)
487 {
488 gcd1 = vector[0];
489 for (i = 1; i < size; i++)
490 gcd1 = gcd (gcd1, vector[i]);
491 }
492 return gcd1;
493}
494
495/* Allocate a new vector of given SIZE. */
496
497static inline lambda_vector
498lambda_vector_new (int size)
499{
6f4f1a50 500 /* ??? We shouldn't abuse the GC allocator here. */
766090c2 501 return ggc_cleared_vec_alloc<int> (size);
b305e3da
SP
502}
503
504/* Clear out vector VEC1 of length SIZE. */
505
506static inline void
507lambda_vector_clear (lambda_vector vec1, int size)
508{
509 memset (vec1, 0, size * sizeof (*vec1));
510}
511
512/* Returns true when the vector V is lexicographically positive, in
513 other words, when the first nonzero element is positive. */
514
515static inline bool
516lambda_vector_lexico_pos (lambda_vector v,
517 unsigned n)
518{
519 unsigned i;
520 for (i = 0; i < n; i++)
521 {
522 if (v[i] == 0)
523 continue;
524 if (v[i] < 0)
525 return false;
526 if (v[i] > 0)
527 return true;
528 }
529 return true;
530}
531
532/* Return true if vector VEC1 of length SIZE is the zero vector. */
533
534static inline bool
535lambda_vector_zerop (lambda_vector vec1, int size)
536{
537 int i;
538 for (i = 0; i < size; i++)
539 if (vec1[i] != 0)
540 return false;
541 return true;
542}
543
544/* Allocate a matrix of M rows x N cols. */
545
546static inline lambda_matrix
547lambda_matrix_new (int m, int n, struct obstack *lambda_obstack)
548{
549 lambda_matrix mat;
550 int i;
551
6f4f1a50 552 mat = XOBNEWVEC (lambda_obstack, lambda_vector, m);
b305e3da
SP
553
554 for (i = 0; i < m; i++)
6f4f1a50 555 mat[i] = XOBNEWVEC (lambda_obstack, int, n);
b305e3da
SP
556
557 return mat;
558}
559
56cf8686 560#endif /* GCC_TREE_DATA_REF_H */