* gcc.target/powerpc: New directory.
[official-gcc.git] / gcc / tree-data-ref.c
blobcf624038e2b41658651818e59e9f85ea07a98d78
1 /* Data references and dependences detectors.
2 Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
3 Contributed by Sebastian Pop <s.pop@laposte.net>
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
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 COPYING. If not, write to the Free
19 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, USA. */
22 /* This pass walks a given loop structure searching for array
23 references. The information about the array accesses is recorded
24 in DATA_REFERENCE structures.
26 The basic test for determining the dependences is:
27 given two access functions chrec1 and chrec2 to a same array, and
28 x and y two vectors from the iteration domain, the same element of
29 the array is accessed twice at iterations x and y if and only if:
30 | chrec1 (x) == chrec2 (y).
32 The goals of this analysis are:
34 - to determine the independence: the relation between two
35 independent accesses is qualified with the chrec_known (this
36 information allows a loop parallelization),
38 - when two data references access the same data, to qualify the
39 dependence relation with classic dependence representations:
41 - distance vectors
42 - direction vectors
43 - loop carried level dependence
44 - polyhedron dependence
45 or with the chains of recurrences based representation,
47 - to define a knowledge base for storing the data dependence
48 information,
50 - to define an interface to access this data.
53 Definitions:
55 - subscript: given two array accesses a subscript is the tuple
56 composed of the access functions for a given dimension. Example:
57 Given A[f1][f2][f3] and B[g1][g2][g3], there are three subscripts:
58 (f1, g1), (f2, g2), (f3, g3).
60 - Diophantine equation: an equation whose coefficients and
61 solutions are integer constants, for example the equation
62 | 3*x + 2*y = 1
63 has an integer solution x = 1 and y = -1.
65 References:
67 - "Advanced Compilation for High Performance Computing" by Randy
68 Allen and Ken Kennedy.
69 http://citeseer.ist.psu.edu/goff91practical.html
71 - "Loop Transformations for Restructuring Compilers - The Foundations"
72 by Utpal Banerjee.
77 #include "config.h"
78 #include "system.h"
79 #include "coretypes.h"
80 #include "tm.h"
81 #include "ggc.h"
82 #include "tree.h"
84 /* These RTL headers are needed for basic-block.h. */
85 #include "rtl.h"
86 #include "basic-block.h"
87 #include "diagnostic.h"
88 #include "tree-flow.h"
89 #include "tree-dump.h"
90 #include "timevar.h"
91 #include "cfgloop.h"
92 #include "tree-chrec.h"
93 #include "tree-data-ref.h"
94 #include "tree-scalar-evolution.h"
95 #include "tree-pass.h"
97 static tree object_analysis (tree, tree, bool, struct data_reference **,
98 tree *, tree *, tree *, tree *, tree *,
99 struct ptr_info_def **, subvar_t *);
100 static struct data_reference * init_data_ref (tree, tree, tree, tree, bool,
101 tree, tree, tree, tree, tree,
102 struct ptr_info_def *,
103 enum data_ref_type);
105 /* Determine if PTR and DECL may alias, the result is put in ALIASED.
106 Return FALSE if there is no type memory tag for PTR.
108 static bool
109 ptr_decl_may_alias_p (tree ptr, tree decl,
110 struct data_reference *ptr_dr,
111 bool *aliased)
113 tree tag;
115 gcc_assert (TREE_CODE (ptr) == SSA_NAME && DECL_P (decl));
117 tag = get_var_ann (SSA_NAME_VAR (ptr))->type_mem_tag;
118 if (!tag)
119 tag = DR_MEMTAG (ptr_dr);
120 if (!tag)
121 return false;
123 *aliased = is_aliased_with (tag, decl);
124 return true;
128 /* Determine if two pointers may alias, the result is put in ALIASED.
129 Return FALSE if there is no type memory tag for one of the pointers.
131 static bool
132 ptr_ptr_may_alias_p (tree ptr_a, tree ptr_b,
133 struct data_reference *dra,
134 struct data_reference *drb,
135 bool *aliased)
137 tree tag_a, tag_b;
139 tag_a = get_var_ann (SSA_NAME_VAR (ptr_a))->type_mem_tag;
140 if (!tag_a)
141 tag_a = DR_MEMTAG (dra);
142 if (!tag_a)
143 return false;
144 tag_b = get_var_ann (SSA_NAME_VAR (ptr_b))->type_mem_tag;
145 if (!tag_b)
146 tag_b = DR_MEMTAG (drb);
147 if (!tag_b)
148 return false;
149 *aliased = (tag_a == tag_b);
150 return true;
154 /* Determine if BASE_A and BASE_B may alias, the result is put in ALIASED.
155 Return FALSE if there is no type memory tag for one of the symbols.
157 static bool
158 may_alias_p (tree base_a, tree base_b,
159 struct data_reference *dra,
160 struct data_reference *drb,
161 bool *aliased)
163 if (TREE_CODE (base_a) == ADDR_EXPR || TREE_CODE (base_b) == ADDR_EXPR)
165 if (TREE_CODE (base_a) == ADDR_EXPR && TREE_CODE (base_b) == ADDR_EXPR)
167 *aliased = (TREE_OPERAND (base_a, 0) == TREE_OPERAND (base_b, 0));
168 return true;
170 if (TREE_CODE (base_a) == ADDR_EXPR)
171 return ptr_decl_may_alias_p (base_b, TREE_OPERAND (base_a, 0), drb,
172 aliased);
173 else
174 return ptr_decl_may_alias_p (base_a, TREE_OPERAND (base_b, 0), dra,
175 aliased);
178 return ptr_ptr_may_alias_p (base_a, base_b, dra, drb, aliased);
182 /* Determine if a pointer (BASE_A) and a record/union access (BASE_B)
183 are not aliased. Return TRUE if they differ. */
184 static bool
185 record_ptr_differ_p (struct data_reference *dra,
186 struct data_reference *drb)
188 bool aliased;
189 tree base_a = DR_BASE_OBJECT (dra);
190 tree base_b = DR_BASE_OBJECT (drb);
192 if (TREE_CODE (base_b) != COMPONENT_REF)
193 return false;
195 /* Peel COMPONENT_REFs to get to the base. Do not peel INDIRECT_REFs.
196 For a.b.c.d[i] we will get a, and for a.b->c.d[i] we will get a.b.
197 Probably will be unnecessary with struct alias analysis. */
198 while (TREE_CODE (base_b) == COMPONENT_REF)
199 base_b = TREE_OPERAND (base_b, 0);
200 /* Compare a record/union access (b.c[i] or p->c[i]) and a pointer
201 ((*q)[i]). */
202 if (TREE_CODE (base_a) == INDIRECT_REF
203 && ((TREE_CODE (base_b) == VAR_DECL
204 && (ptr_decl_may_alias_p (TREE_OPERAND (base_a, 0), base_b, dra,
205 &aliased)
206 && !aliased))
207 || (TREE_CODE (base_b) == INDIRECT_REF
208 && (ptr_ptr_may_alias_p (TREE_OPERAND (base_a, 0),
209 TREE_OPERAND (base_b, 0), dra, drb,
210 &aliased)
211 && !aliased))))
212 return true;
213 else
214 return false;
218 /* Determine if an array access (BASE_A) and a record/union access (BASE_B)
219 are not aliased. Return TRUE if they differ. */
220 static bool
221 record_array_differ_p (struct data_reference *dra,
222 struct data_reference *drb)
224 bool aliased;
225 tree base_a = DR_BASE_OBJECT (dra);
226 tree base_b = DR_BASE_OBJECT (drb);
228 if (TREE_CODE (base_b) != COMPONENT_REF)
229 return false;
231 /* Peel COMPONENT_REFs to get to the base. Do not peel INDIRECT_REFs.
232 For a.b.c.d[i] we will get a, and for a.b->c.d[i] we will get a.b.
233 Probably will be unnecessary with struct alias analysis. */
234 while (TREE_CODE (base_b) == COMPONENT_REF)
235 base_b = TREE_OPERAND (base_b, 0);
237 /* Compare a record/union access (b.c[i] or p->c[i]) and an array access
238 (a[i]). In case of p->c[i] use alias analysis to verify that p is not
239 pointing to a. */
240 if (TREE_CODE (base_a) == VAR_DECL
241 && (TREE_CODE (base_b) == VAR_DECL
242 || (TREE_CODE (base_b) == INDIRECT_REF
243 && (ptr_decl_may_alias_p (TREE_OPERAND (base_b, 0), base_a, drb,
244 &aliased)
245 && !aliased))))
246 return true;
247 else
248 return false;
252 /* Determine if an array access (BASE_A) and a pointer (BASE_B)
253 are not aliased. Return TRUE if they differ. */
254 static bool
255 array_ptr_differ_p (tree base_a, tree base_b,
256 struct data_reference *drb)
258 bool aliased;
260 /* In case one of the bases is a pointer (a[i] and (*p)[i]), we check with the
261 help of alias analysis that p is not pointing to a. */
262 if (TREE_CODE (base_a) == VAR_DECL && TREE_CODE (base_b) == INDIRECT_REF
263 && (ptr_decl_may_alias_p (TREE_OPERAND (base_b, 0), base_a, drb, &aliased)
264 && !aliased))
265 return true;
266 else
267 return false;
271 /* This is the simplest data dependence test: determines whether the
272 data references A and B access the same array/region. Returns
273 false when the property is not computable at compile time.
274 Otherwise return true, and DIFFER_P will record the result. This
275 utility will not be necessary when alias_sets_conflict_p will be
276 less conservative. */
278 static bool
279 base_object_differ_p (struct data_reference *a,
280 struct data_reference *b,
281 bool *differ_p)
283 tree base_a = DR_BASE_OBJECT (a);
284 tree base_b = DR_BASE_OBJECT (b);
285 bool aliased;
287 if (!base_a || !base_b)
288 return false;
290 /* Determine if same base. Example: for the array accesses
291 a[i], b[i] or pointer accesses *a, *b, bases are a, b. */
292 if (base_a == base_b)
294 *differ_p = false;
295 return true;
298 /* For pointer based accesses, (*p)[i], (*q)[j], the bases are (*p)
299 and (*q) */
300 if (TREE_CODE (base_a) == INDIRECT_REF && TREE_CODE (base_b) == INDIRECT_REF
301 && TREE_OPERAND (base_a, 0) == TREE_OPERAND (base_b, 0))
303 *differ_p = false;
304 return true;
307 /* Record/union based accesses - s.a[i], t.b[j]. bases are s.a,t.b. */
308 if (TREE_CODE (base_a) == COMPONENT_REF && TREE_CODE (base_b) == COMPONENT_REF
309 && TREE_OPERAND (base_a, 0) == TREE_OPERAND (base_b, 0)
310 && TREE_OPERAND (base_a, 1) == TREE_OPERAND (base_b, 1))
312 *differ_p = false;
313 return true;
317 /* Determine if different bases. */
319 /* At this point we know that base_a != base_b. However, pointer
320 accesses of the form x=(*p) and y=(*q), whose bases are p and q,
321 may still be pointing to the same base. In SSAed GIMPLE p and q will
322 be SSA_NAMES in this case. Therefore, here we check if they are
323 really two different declarations. */
324 if (TREE_CODE (base_a) == VAR_DECL && TREE_CODE (base_b) == VAR_DECL)
326 *differ_p = true;
327 return true;
330 /* In case one of the bases is a pointer (a[i] and (*p)[i]), we check with the
331 help of alias analysis that p is not pointing to a. */
332 if (array_ptr_differ_p (base_a, base_b, b)
333 || array_ptr_differ_p (base_b, base_a, a))
335 *differ_p = true;
336 return true;
339 /* If the bases are pointers ((*q)[i] and (*p)[i]), we check with the
340 help of alias analysis they don't point to the same bases. */
341 if (TREE_CODE (base_a) == INDIRECT_REF && TREE_CODE (base_b) == INDIRECT_REF
342 && (may_alias_p (TREE_OPERAND (base_a, 0), TREE_OPERAND (base_b, 0), a, b,
343 &aliased)
344 && !aliased))
346 *differ_p = true;
347 return true;
350 /* Compare two record/union bases s.a and t.b: s != t or (a != b and
351 s and t are not unions). */
352 if (TREE_CODE (base_a) == COMPONENT_REF && TREE_CODE (base_b) == COMPONENT_REF
353 && ((TREE_CODE (TREE_OPERAND (base_a, 0)) == VAR_DECL
354 && TREE_CODE (TREE_OPERAND (base_b, 0)) == VAR_DECL
355 && TREE_OPERAND (base_a, 0) != TREE_OPERAND (base_b, 0))
356 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (base_a, 0))) == RECORD_TYPE
357 && TREE_CODE (TREE_TYPE (TREE_OPERAND (base_b, 0))) == RECORD_TYPE
358 && TREE_OPERAND (base_a, 1) != TREE_OPERAND (base_b, 1))))
360 *differ_p = true;
361 return true;
364 /* Compare a record/union access (b.c[i] or p->c[i]) and a pointer
365 ((*q)[i]). */
366 if (record_ptr_differ_p (a, b) || record_ptr_differ_p (b, a))
368 *differ_p = true;
369 return true;
372 /* Compare a record/union access (b.c[i] or p->c[i]) and an array access
373 (a[i]). In case of p->c[i] use alias analysis to verify that p is not
374 pointing to a. */
375 if (record_array_differ_p (a, b) || record_array_differ_p (b, a))
377 *differ_p = true;
378 return true;
381 return false;
384 /* Function base_addr_differ_p.
386 This is the simplest data dependence test: determines whether the
387 data references DRA and DRB access the same array/region. Returns
388 false when the property is not computable at compile time.
389 Otherwise return true, and DIFFER_P will record the result.
391 The algorithm:
392 1. if (both DRA and DRB are represented as arrays)
393 compare DRA.BASE_OBJECT and DRB.BASE_OBJECT
394 2. else if (both DRA and DRB are represented as pointers)
395 try to prove that DRA.FIRST_LOCATION == DRB.FIRST_LOCATION
396 3. else if (DRA and DRB are represented differently or 2. fails)
397 only try to prove that the bases are surely different
401 static bool
402 base_addr_differ_p (struct data_reference *dra,
403 struct data_reference *drb,
404 bool *differ_p)
406 tree addr_a = DR_BASE_ADDRESS (dra);
407 tree addr_b = DR_BASE_ADDRESS (drb);
408 tree type_a, type_b;
409 bool aliased;
411 if (!addr_a || !addr_b)
412 return false;
414 type_a = TREE_TYPE (addr_a);
415 type_b = TREE_TYPE (addr_b);
417 gcc_assert (POINTER_TYPE_P (type_a) && POINTER_TYPE_P (type_b));
419 /* 1. if (both DRA and DRB are represented as arrays)
420 compare DRA.BASE_OBJECT and DRB.BASE_OBJECT. */
421 if (DR_TYPE (dra) == ARRAY_REF_TYPE && DR_TYPE (drb) == ARRAY_REF_TYPE)
422 return base_object_differ_p (dra, drb, differ_p);
425 /* 2. else if (both DRA and DRB are represented as pointers)
426 try to prove that DRA.FIRST_LOCATION == DRB.FIRST_LOCATION. */
427 /* If base addresses are the same, we check the offsets, since the access of
428 the data-ref is described by {base addr + offset} and its access function,
429 i.e., in order to decide whether the bases of data-refs are the same we
430 compare both base addresses and offsets. */
431 if (DR_TYPE (dra) == POINTER_REF_TYPE && DR_TYPE (drb) == POINTER_REF_TYPE
432 && (addr_a == addr_b
433 || (TREE_CODE (addr_a) == ADDR_EXPR && TREE_CODE (addr_b) == ADDR_EXPR
434 && TREE_OPERAND (addr_a, 0) == TREE_OPERAND (addr_b, 0))))
436 /* Compare offsets. */
437 tree offset_a = DR_OFFSET (dra);
438 tree offset_b = DR_OFFSET (drb);
440 STRIP_NOPS (offset_a);
441 STRIP_NOPS (offset_b);
443 /* FORNOW: we only compare offsets that are MULT_EXPR, i.e., we don't handle
444 PLUS_EXPR. */
445 if ((offset_a == offset_b)
446 || (TREE_CODE (offset_a) == MULT_EXPR
447 && TREE_CODE (offset_b) == MULT_EXPR
448 && TREE_OPERAND (offset_a, 0) == TREE_OPERAND (offset_b, 0)
449 && TREE_OPERAND (offset_a, 1) == TREE_OPERAND (offset_b, 1)))
451 *differ_p = false;
452 return true;
456 /* 3. else if (DRA and DRB are represented differently or 2. fails)
457 only try to prove that the bases are surely different. */
459 /* Apply alias analysis. */
460 if (may_alias_p (addr_a, addr_b, dra, drb, &aliased) && !aliased)
462 *differ_p = true;
463 return true;
466 /* An instruction writing through a restricted pointer is "independent" of any
467 instruction reading or writing through a different pointer, in the same
468 block/scope. */
469 else if ((TYPE_RESTRICT (type_a) && !DR_IS_READ (dra))
470 || (TYPE_RESTRICT (type_b) && !DR_IS_READ (drb)))
472 *differ_p = true;
473 return true;
475 return false;
479 /* Returns true iff A divides B. */
481 static inline bool
482 tree_fold_divides_p (tree a,
483 tree b)
485 /* Determines whether (A == gcd (A, B)). */
486 return tree_int_cst_equal (a, tree_fold_gcd (a, b));
489 /* Compute the greatest common denominator of two numbers using
490 Euclid's algorithm. */
492 static int
493 gcd (int a, int b)
496 int x, y, z;
498 x = abs (a);
499 y = abs (b);
501 while (x>0)
503 z = y % x;
504 y = x;
505 x = z;
508 return (y);
511 /* Returns true iff A divides B. */
513 static inline bool
514 int_divides_p (int a, int b)
516 return ((b % a) == 0);
521 /* Dump into FILE all the data references from DATAREFS. */
523 void
524 dump_data_references (FILE *file,
525 varray_type datarefs)
527 unsigned int i;
529 for (i = 0; i < VARRAY_ACTIVE_SIZE (datarefs); i++)
530 dump_data_reference (file, VARRAY_GENERIC_PTR (datarefs, i));
533 /* Dump into FILE all the dependence relations from DDR. */
535 void
536 dump_data_dependence_relations (FILE *file,
537 varray_type ddr)
539 unsigned int i;
541 for (i = 0; i < VARRAY_ACTIVE_SIZE (ddr); i++)
542 dump_data_dependence_relation (file, VARRAY_GENERIC_PTR (ddr, i));
545 /* Dump function for a DATA_REFERENCE structure. */
547 void
548 dump_data_reference (FILE *outf,
549 struct data_reference *dr)
551 unsigned int i;
553 fprintf (outf, "(Data Ref: \n stmt: ");
554 print_generic_stmt (outf, DR_STMT (dr), 0);
555 fprintf (outf, " ref: ");
556 print_generic_stmt (outf, DR_REF (dr), 0);
557 fprintf (outf, " base_name: ");
558 print_generic_stmt (outf, DR_BASE_OBJECT (dr), 0);
560 for (i = 0; i < DR_NUM_DIMENSIONS (dr); i++)
562 fprintf (outf, " Access function %d: ", i);
563 print_generic_stmt (outf, DR_ACCESS_FN (dr, i), 0);
565 fprintf (outf, ")\n");
568 /* Dump function for a SUBSCRIPT structure. */
570 void
571 dump_subscript (FILE *outf, struct subscript *subscript)
573 tree chrec = SUB_CONFLICTS_IN_A (subscript);
575 fprintf (outf, "\n (subscript \n");
576 fprintf (outf, " iterations_that_access_an_element_twice_in_A: ");
577 print_generic_stmt (outf, chrec, 0);
578 if (chrec == chrec_known)
579 fprintf (outf, " (no dependence)\n");
580 else if (chrec_contains_undetermined (chrec))
581 fprintf (outf, " (don't know)\n");
582 else
584 tree last_iteration = SUB_LAST_CONFLICT (subscript);
585 fprintf (outf, " last_conflict: ");
586 print_generic_stmt (outf, last_iteration, 0);
589 chrec = SUB_CONFLICTS_IN_B (subscript);
590 fprintf (outf, " iterations_that_access_an_element_twice_in_B: ");
591 print_generic_stmt (outf, chrec, 0);
592 if (chrec == chrec_known)
593 fprintf (outf, " (no dependence)\n");
594 else if (chrec_contains_undetermined (chrec))
595 fprintf (outf, " (don't know)\n");
596 else
598 tree last_iteration = SUB_LAST_CONFLICT (subscript);
599 fprintf (outf, " last_conflict: ");
600 print_generic_stmt (outf, last_iteration, 0);
603 fprintf (outf, " (Subscript distance: ");
604 print_generic_stmt (outf, SUB_DISTANCE (subscript), 0);
605 fprintf (outf, " )\n");
606 fprintf (outf, " )\n");
609 /* Dump function for a DATA_DEPENDENCE_RELATION structure. */
611 void
612 dump_data_dependence_relation (FILE *outf,
613 struct data_dependence_relation *ddr)
615 struct data_reference *dra, *drb;
617 dra = DDR_A (ddr);
618 drb = DDR_B (ddr);
619 fprintf (outf, "(Data Dep: \n");
620 if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
621 fprintf (outf, " (don't know)\n");
623 else if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
624 fprintf (outf, " (no dependence)\n");
626 else if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
628 unsigned int i;
629 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
631 fprintf (outf, " access_fn_A: ");
632 print_generic_stmt (outf, DR_ACCESS_FN (dra, i), 0);
633 fprintf (outf, " access_fn_B: ");
634 print_generic_stmt (outf, DR_ACCESS_FN (drb, i), 0);
635 dump_subscript (outf, DDR_SUBSCRIPT (ddr, i));
637 if (DDR_DIST_VECT (ddr))
639 fprintf (outf, " distance_vect: ");
640 print_lambda_vector (outf, DDR_DIST_VECT (ddr), DDR_SIZE_VECT (ddr));
642 if (DDR_DIR_VECT (ddr))
644 fprintf (outf, " direction_vect: ");
645 print_lambda_vector (outf, DDR_DIR_VECT (ddr), DDR_SIZE_VECT (ddr));
649 fprintf (outf, ")\n");
654 /* Dump function for a DATA_DEPENDENCE_DIRECTION structure. */
656 void
657 dump_data_dependence_direction (FILE *file,
658 enum data_dependence_direction dir)
660 switch (dir)
662 case dir_positive:
663 fprintf (file, "+");
664 break;
666 case dir_negative:
667 fprintf (file, "-");
668 break;
670 case dir_equal:
671 fprintf (file, "=");
672 break;
674 case dir_positive_or_negative:
675 fprintf (file, "+-");
676 break;
678 case dir_positive_or_equal:
679 fprintf (file, "+=");
680 break;
682 case dir_negative_or_equal:
683 fprintf (file, "-=");
684 break;
686 case dir_star:
687 fprintf (file, "*");
688 break;
690 default:
691 break;
695 /* Dumps the distance and direction vectors in FILE. DDRS contains
696 the dependence relations, and VECT_SIZE is the size of the
697 dependence vectors, or in other words the number of loops in the
698 considered nest. */
700 void
701 dump_dist_dir_vectors (FILE *file, varray_type ddrs)
703 unsigned int i;
705 for (i = 0; i < VARRAY_ACTIVE_SIZE (ddrs); i++)
707 struct data_dependence_relation *ddr =
708 (struct data_dependence_relation *)
709 VARRAY_GENERIC_PTR (ddrs, i);
710 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE
711 && DDR_AFFINE_P (ddr))
713 fprintf (file, "DISTANCE_V (");
714 print_lambda_vector (file, DDR_DIST_VECT (ddr), DDR_SIZE_VECT (ddr));
715 fprintf (file, ")\n");
716 fprintf (file, "DIRECTION_V (");
717 print_lambda_vector (file, DDR_DIR_VECT (ddr), DDR_SIZE_VECT (ddr));
718 fprintf (file, ")\n");
721 fprintf (file, "\n\n");
724 /* Dumps the data dependence relations DDRS in FILE. */
726 void
727 dump_ddrs (FILE *file, varray_type ddrs)
729 unsigned int i;
731 for (i = 0; i < VARRAY_ACTIVE_SIZE (ddrs); i++)
733 struct data_dependence_relation *ddr =
734 (struct data_dependence_relation *)
735 VARRAY_GENERIC_PTR (ddrs, i);
736 dump_data_dependence_relation (file, ddr);
738 fprintf (file, "\n\n");
743 /* Estimate the number of iterations from the size of the data and the
744 access functions. */
746 static void
747 estimate_niter_from_size_of_data (struct loop *loop,
748 tree opnd0,
749 tree access_fn,
750 tree stmt)
752 tree estimation = NULL_TREE;
753 tree array_size, data_size, element_size;
754 tree init, step;
756 init = initial_condition (access_fn);
757 step = evolution_part_in_loop_num (access_fn, loop->num);
759 array_size = TYPE_SIZE (TREE_TYPE (opnd0));
760 element_size = TYPE_SIZE (TREE_TYPE (TREE_TYPE (opnd0)));
761 if (array_size == NULL_TREE
762 || TREE_CODE (array_size) != INTEGER_CST
763 || TREE_CODE (element_size) != INTEGER_CST)
764 return;
766 data_size = fold_build2 (EXACT_DIV_EXPR, integer_type_node,
767 array_size, element_size);
769 if (init != NULL_TREE
770 && step != NULL_TREE
771 && TREE_CODE (init) == INTEGER_CST
772 && TREE_CODE (step) == INTEGER_CST)
774 tree i_plus_s = fold_build2 (PLUS_EXPR, integer_type_node, init, step);
775 tree sign = fold_build2 (GT_EXPR, boolean_type_node, i_plus_s, init);
777 if (sign == boolean_true_node)
778 estimation = fold_build2 (CEIL_DIV_EXPR, integer_type_node,
779 fold_build2 (MINUS_EXPR, integer_type_node,
780 data_size, init), step);
782 /* When the step is negative, as in PR23386: (init = 3, step =
783 0ffffffff, data_size = 100), we have to compute the
784 estimation as ceil_div (init, 0 - step) + 1. */
785 else if (sign == boolean_false_node)
786 estimation =
787 fold_build2 (PLUS_EXPR, integer_type_node,
788 fold_build2 (CEIL_DIV_EXPR, integer_type_node,
789 init,
790 fold_build2 (MINUS_EXPR, unsigned_type_node,
791 integer_zero_node, step)),
792 integer_one_node);
794 if (estimation)
795 record_estimate (loop, estimation, boolean_true_node, stmt);
799 /* Given an ARRAY_REF node REF, records its access functions.
800 Example: given A[i][3], record in ACCESS_FNS the opnd1 function,
801 i.e. the constant "3", then recursively call the function on opnd0,
802 i.e. the ARRAY_REF "A[i]".
803 If ESTIMATE_ONLY is true, we just set the estimated number of loop
804 iterations, we don't store the access function.
805 The function returns the base name: "A". */
807 static tree
808 analyze_array_indexes (struct loop *loop,
809 VEC(tree,heap) **access_fns,
810 tree ref, tree stmt,
811 bool estimate_only)
813 tree opnd0, opnd1;
814 tree access_fn;
816 opnd0 = TREE_OPERAND (ref, 0);
817 opnd1 = TREE_OPERAND (ref, 1);
819 /* The detection of the evolution function for this data access is
820 postponed until the dependence test. This lazy strategy avoids
821 the computation of access functions that are of no interest for
822 the optimizers. */
823 access_fn = instantiate_parameters
824 (loop, analyze_scalar_evolution (loop, opnd1));
826 if (estimate_only
827 && chrec_contains_undetermined (loop->estimated_nb_iterations))
828 estimate_niter_from_size_of_data (loop, opnd0, access_fn, stmt);
830 if (!estimate_only)
831 VEC_safe_push (tree, heap, *access_fns, access_fn);
833 /* Recursively record other array access functions. */
834 if (TREE_CODE (opnd0) == ARRAY_REF)
835 return analyze_array_indexes (loop, access_fns, opnd0, stmt, estimate_only);
837 /* Return the base name of the data access. */
838 else
839 return opnd0;
842 /* For an array reference REF contained in STMT, attempt to bound the
843 number of iterations in the loop containing STMT */
845 void
846 estimate_iters_using_array (tree stmt, tree ref)
848 analyze_array_indexes (loop_containing_stmt (stmt), NULL, ref, stmt,
849 true);
852 /* For a data reference REF contained in the statement STMT, initialize
853 a DATA_REFERENCE structure, and return it. IS_READ flag has to be
854 set to true when REF is in the right hand side of an
855 assignment. */
857 struct data_reference *
858 analyze_array (tree stmt, tree ref, bool is_read)
860 struct data_reference *res;
861 VEC(tree,heap) *acc_fns;
863 if (dump_file && (dump_flags & TDF_DETAILS))
865 fprintf (dump_file, "(analyze_array \n");
866 fprintf (dump_file, " (ref = ");
867 print_generic_stmt (dump_file, ref, 0);
868 fprintf (dump_file, ")\n");
871 res = xmalloc (sizeof (struct data_reference));
873 DR_STMT (res) = stmt;
874 DR_REF (res) = ref;
875 acc_fns = VEC_alloc (tree, heap, 3);
876 DR_BASE_OBJECT (res) = analyze_array_indexes
877 (loop_containing_stmt (stmt), &acc_fns, ref, stmt, false);
878 DR_TYPE (res) = ARRAY_REF_TYPE;
879 DR_SET_ACCESS_FNS (res, acc_fns);
880 DR_IS_READ (res) = is_read;
881 DR_BASE_ADDRESS (res) = NULL_TREE;
882 DR_OFFSET (res) = NULL_TREE;
883 DR_INIT (res) = NULL_TREE;
884 DR_STEP (res) = NULL_TREE;
885 DR_OFFSET_MISALIGNMENT (res) = NULL_TREE;
886 DR_MEMTAG (res) = NULL_TREE;
887 DR_PTR_INFO (res) = NULL;
889 if (dump_file && (dump_flags & TDF_DETAILS))
890 fprintf (dump_file, ")\n");
892 return res;
896 /* Analyze an indirect memory reference, REF, that comes from STMT.
897 IS_READ is true if this is an indirect load, and false if it is
898 an indirect store.
899 Return a new data reference structure representing the indirect_ref, or
900 NULL if we cannot describe the access function. */
902 static struct data_reference *
903 analyze_indirect_ref (tree stmt, tree ref, bool is_read)
905 struct loop *loop = loop_containing_stmt (stmt);
906 tree ptr_ref = TREE_OPERAND (ref, 0);
907 tree access_fn = analyze_scalar_evolution (loop, ptr_ref);
908 tree init = initial_condition_in_loop_num (access_fn, loop->num);
909 tree base_address = NULL_TREE, evolution, step = NULL_TREE;
910 struct ptr_info_def *ptr_info = NULL;
912 if (TREE_CODE (ptr_ref) == SSA_NAME)
913 ptr_info = SSA_NAME_PTR_INFO (ptr_ref);
915 STRIP_NOPS (init);
916 if (access_fn == chrec_dont_know || !init || init == chrec_dont_know)
918 if (dump_file && (dump_flags & TDF_DETAILS))
920 fprintf (dump_file, "\nBad access function of ptr: ");
921 print_generic_expr (dump_file, ref, TDF_SLIM);
922 fprintf (dump_file, "\n");
924 return NULL;
927 if (dump_file && (dump_flags & TDF_DETAILS))
929 fprintf (dump_file, "\nAccess function of ptr: ");
930 print_generic_expr (dump_file, access_fn, TDF_SLIM);
931 fprintf (dump_file, "\n");
934 if (!expr_invariant_in_loop_p (loop, init))
936 if (dump_file && (dump_flags & TDF_DETAILS))
937 fprintf (dump_file, "\ninitial condition is not loop invariant.\n");
939 else
941 base_address = init;
942 evolution = evolution_part_in_loop_num (access_fn, loop->num);
943 if (evolution != chrec_dont_know)
945 if (!evolution)
946 step = ssize_int (0);
947 else
949 if (TREE_CODE (evolution) == INTEGER_CST)
950 step = fold_convert (ssizetype, evolution);
951 else
952 if (dump_file && (dump_flags & TDF_DETAILS))
953 fprintf (dump_file, "\nnon constant step for ptr access.\n");
956 else
957 if (dump_file && (dump_flags & TDF_DETAILS))
958 fprintf (dump_file, "\nunknown evolution of ptr.\n");
960 return init_data_ref (stmt, ref, NULL_TREE, access_fn, is_read, base_address,
961 NULL_TREE, step, NULL_TREE, NULL_TREE,
962 ptr_info, POINTER_REF_TYPE);
965 /* For a data reference REF contained in the statement STMT, initialize
966 a DATA_REFERENCE structure, and return it. */
968 struct data_reference *
969 init_data_ref (tree stmt,
970 tree ref,
971 tree base,
972 tree access_fn,
973 bool is_read,
974 tree base_address,
975 tree init_offset,
976 tree step,
977 tree misalign,
978 tree memtag,
979 struct ptr_info_def *ptr_info,
980 enum data_ref_type type)
982 struct data_reference *res;
983 VEC(tree,heap) *acc_fns;
985 if (dump_file && (dump_flags & TDF_DETAILS))
987 fprintf (dump_file, "(init_data_ref \n");
988 fprintf (dump_file, " (ref = ");
989 print_generic_stmt (dump_file, ref, 0);
990 fprintf (dump_file, ")\n");
993 res = xmalloc (sizeof (struct data_reference));
995 DR_STMT (res) = stmt;
996 DR_REF (res) = ref;
997 DR_BASE_OBJECT (res) = base;
998 DR_TYPE (res) = type;
999 acc_fns = VEC_alloc (tree, heap, 3);
1000 DR_SET_ACCESS_FNS (res, acc_fns);
1001 VEC_quick_push (tree, DR_ACCESS_FNS (res), access_fn);
1002 DR_IS_READ (res) = is_read;
1003 DR_BASE_ADDRESS (res) = base_address;
1004 DR_OFFSET (res) = init_offset;
1005 DR_INIT (res) = NULL_TREE;
1006 DR_STEP (res) = step;
1007 DR_OFFSET_MISALIGNMENT (res) = misalign;
1008 DR_MEMTAG (res) = memtag;
1009 DR_PTR_INFO (res) = ptr_info;
1011 if (dump_file && (dump_flags & TDF_DETAILS))
1012 fprintf (dump_file, ")\n");
1014 return res;
1019 /* Function strip_conversions
1021 Strip conversions that don't narrow the mode. */
1023 static tree
1024 strip_conversion (tree expr)
1026 tree to, ti, oprnd0;
1028 while (TREE_CODE (expr) == NOP_EXPR || TREE_CODE (expr) == CONVERT_EXPR)
1030 to = TREE_TYPE (expr);
1031 oprnd0 = TREE_OPERAND (expr, 0);
1032 ti = TREE_TYPE (oprnd0);
1034 if (!INTEGRAL_TYPE_P (to) || !INTEGRAL_TYPE_P (ti))
1035 return NULL_TREE;
1036 if (GET_MODE_SIZE (TYPE_MODE (to)) < GET_MODE_SIZE (TYPE_MODE (ti)))
1037 return NULL_TREE;
1039 expr = oprnd0;
1041 return expr;
1045 /* Function analyze_offset_expr
1047 Given an offset expression EXPR received from get_inner_reference, analyze
1048 it and create an expression for INITIAL_OFFSET by substituting the variables
1049 of EXPR with initial_condition of the corresponding access_fn in the loop.
1050 E.g.,
1051 for i
1052 for (j = 3; j < N; j++)
1053 a[j].b[i][j] = 0;
1055 For a[j].b[i][j], EXPR will be 'i * C_i + j * C_j + C'. 'i' cannot be
1056 substituted, since its access_fn in the inner loop is i. 'j' will be
1057 substituted with 3. An INITIAL_OFFSET will be 'i * C_i + C`', where
1058 C` = 3 * C_j + C.
1060 Compute MISALIGN (the misalignment of the data reference initial access from
1061 its base). Misalignment can be calculated only if all the variables can be
1062 substituted with constants, otherwise, we record maximum possible alignment
1063 in ALIGNED_TO. In the above example, since 'i' cannot be substituted, MISALIGN
1064 will be NULL_TREE, and the biggest divider of C_i (a power of 2) will be
1065 recorded in ALIGNED_TO.
1067 STEP is an evolution of the data reference in this loop in bytes.
1068 In the above example, STEP is C_j.
1070 Return FALSE, if the analysis fails, e.g., there is no access_fn for a
1071 variable. In this case, all the outputs (INITIAL_OFFSET, MISALIGN, ALIGNED_TO
1072 and STEP) are NULL_TREEs. Otherwise, return TRUE.
1076 static bool
1077 analyze_offset_expr (tree expr,
1078 struct loop *loop,
1079 tree *initial_offset,
1080 tree *misalign,
1081 tree *aligned_to,
1082 tree *step)
1084 tree oprnd0;
1085 tree oprnd1;
1086 tree left_offset = ssize_int (0);
1087 tree right_offset = ssize_int (0);
1088 tree left_misalign = ssize_int (0);
1089 tree right_misalign = ssize_int (0);
1090 tree left_step = ssize_int (0);
1091 tree right_step = ssize_int (0);
1092 enum tree_code code;
1093 tree init, evolution;
1094 tree left_aligned_to = NULL_TREE, right_aligned_to = NULL_TREE;
1096 *step = NULL_TREE;
1097 *misalign = NULL_TREE;
1098 *aligned_to = NULL_TREE;
1099 *initial_offset = NULL_TREE;
1101 /* Strip conversions that don't narrow the mode. */
1102 expr = strip_conversion (expr);
1103 if (!expr)
1104 return false;
1106 /* Stop conditions:
1107 1. Constant. */
1108 if (TREE_CODE (expr) == INTEGER_CST)
1110 *initial_offset = fold_convert (ssizetype, expr);
1111 *misalign = fold_convert (ssizetype, expr);
1112 *step = ssize_int (0);
1113 return true;
1116 /* 2. Variable. Try to substitute with initial_condition of the corresponding
1117 access_fn in the current loop. */
1118 if (SSA_VAR_P (expr))
1120 tree access_fn = analyze_scalar_evolution (loop, expr);
1122 if (access_fn == chrec_dont_know)
1123 /* No access_fn. */
1124 return false;
1126 init = initial_condition_in_loop_num (access_fn, loop->num);
1127 if (!expr_invariant_in_loop_p (loop, init))
1128 /* Not enough information: may be not loop invariant.
1129 E.g., for a[b[i]], we get a[D], where D=b[i]. EXPR is D, its
1130 initial_condition is D, but it depends on i - loop's induction
1131 variable. */
1132 return false;
1134 evolution = evolution_part_in_loop_num (access_fn, loop->num);
1135 if (evolution && TREE_CODE (evolution) != INTEGER_CST)
1136 /* Evolution is not constant. */
1137 return false;
1139 if (TREE_CODE (init) == INTEGER_CST)
1140 *misalign = fold_convert (ssizetype, init);
1141 else
1142 /* Not constant, misalignment cannot be calculated. */
1143 *misalign = NULL_TREE;
1145 *initial_offset = fold_convert (ssizetype, init);
1147 *step = evolution ? fold_convert (ssizetype, evolution) : ssize_int (0);
1148 return true;
1151 /* Recursive computation. */
1152 if (!BINARY_CLASS_P (expr))
1154 /* We expect to get binary expressions (PLUS/MINUS and MULT). */
1155 if (dump_file && (dump_flags & TDF_DETAILS))
1157 fprintf (dump_file, "\nNot binary expression ");
1158 print_generic_expr (dump_file, expr, TDF_SLIM);
1159 fprintf (dump_file, "\n");
1161 return false;
1163 oprnd0 = TREE_OPERAND (expr, 0);
1164 oprnd1 = TREE_OPERAND (expr, 1);
1166 if (!analyze_offset_expr (oprnd0, loop, &left_offset, &left_misalign,
1167 &left_aligned_to, &left_step)
1168 || !analyze_offset_expr (oprnd1, loop, &right_offset, &right_misalign,
1169 &right_aligned_to, &right_step))
1170 return false;
1172 /* The type of the operation: plus, minus or mult. */
1173 code = TREE_CODE (expr);
1174 switch (code)
1176 case MULT_EXPR:
1177 if (TREE_CODE (right_offset) != INTEGER_CST)
1178 /* RIGHT_OFFSET can be not constant. For example, for arrays of variable
1179 sized types.
1180 FORNOW: We don't support such cases. */
1181 return false;
1183 /* Strip conversions that don't narrow the mode. */
1184 left_offset = strip_conversion (left_offset);
1185 if (!left_offset)
1186 return false;
1187 /* Misalignment computation. */
1188 if (SSA_VAR_P (left_offset))
1190 /* If the left side contains variables that can't be substituted with
1191 constants, the misalignment is unknown. However, if the right side
1192 is a multiple of some alignment, we know that the expression is
1193 aligned to it. Therefore, we record such maximum possible value.
1195 *misalign = NULL_TREE;
1196 *aligned_to = ssize_int (highest_pow2_factor (right_offset));
1198 else
1200 /* The left operand was successfully substituted with constant. */
1201 if (left_misalign)
1203 /* In case of EXPR '(i * C1 + j) * C2', LEFT_MISALIGN is
1204 NULL_TREE. */
1205 *misalign = size_binop (code, left_misalign, right_misalign);
1206 if (left_aligned_to && right_aligned_to)
1207 *aligned_to = size_binop (MIN_EXPR, left_aligned_to,
1208 right_aligned_to);
1209 else
1210 *aligned_to = left_aligned_to ?
1211 left_aligned_to : right_aligned_to;
1213 else
1214 *misalign = NULL_TREE;
1217 /* Step calculation. */
1218 /* Multiply the step by the right operand. */
1219 *step = size_binop (MULT_EXPR, left_step, right_offset);
1220 break;
1222 case PLUS_EXPR:
1223 case MINUS_EXPR:
1224 /* Combine the recursive calculations for step and misalignment. */
1225 *step = size_binop (code, left_step, right_step);
1227 /* Unknown alignment. */
1228 if ((!left_misalign && !left_aligned_to)
1229 || (!right_misalign && !right_aligned_to))
1231 *misalign = NULL_TREE;
1232 *aligned_to = NULL_TREE;
1233 break;
1236 if (left_misalign && right_misalign)
1237 *misalign = size_binop (code, left_misalign, right_misalign);
1238 else
1239 *misalign = left_misalign ? left_misalign : right_misalign;
1241 if (left_aligned_to && right_aligned_to)
1242 *aligned_to = size_binop (MIN_EXPR, left_aligned_to, right_aligned_to);
1243 else
1244 *aligned_to = left_aligned_to ? left_aligned_to : right_aligned_to;
1246 break;
1248 default:
1249 gcc_unreachable ();
1252 /* Compute offset. */
1253 *initial_offset = fold_convert (ssizetype,
1254 fold_build2 (code, TREE_TYPE (left_offset),
1255 left_offset,
1256 right_offset));
1257 return true;
1260 /* Function address_analysis
1262 Return the BASE of the address expression EXPR.
1263 Also compute the OFFSET from BASE, MISALIGN and STEP.
1265 Input:
1266 EXPR - the address expression that is being analyzed
1267 STMT - the statement that contains EXPR or its original memory reference
1268 IS_READ - TRUE if STMT reads from EXPR, FALSE if writes to EXPR
1269 DR - data_reference struct for the original memory reference
1271 Output:
1272 BASE (returned value) - the base of the data reference EXPR.
1273 INITIAL_OFFSET - initial offset of EXPR from BASE (an expression)
1274 MISALIGN - offset of EXPR from BASE in bytes (a constant) or NULL_TREE if the
1275 computation is impossible
1276 ALIGNED_TO - maximum alignment of EXPR or NULL_TREE if MISALIGN can be
1277 calculated (doesn't depend on variables)
1278 STEP - evolution of EXPR in the loop
1280 If something unexpected is encountered (an unsupported form of data-ref),
1281 then NULL_TREE is returned.
1284 static tree
1285 address_analysis (tree expr, tree stmt, bool is_read, struct data_reference *dr,
1286 tree *offset, tree *misalign, tree *aligned_to, tree *step)
1288 tree oprnd0, oprnd1, base_address, offset_expr, base_addr0, base_addr1;
1289 tree address_offset = ssize_int (0), address_misalign = ssize_int (0);
1290 tree dummy, address_aligned_to = NULL_TREE;
1291 struct ptr_info_def *dummy1;
1292 subvar_t dummy2;
1294 switch (TREE_CODE (expr))
1296 case PLUS_EXPR:
1297 case MINUS_EXPR:
1298 /* EXPR is of form {base +/- offset} (or {offset +/- base}). */
1299 oprnd0 = TREE_OPERAND (expr, 0);
1300 oprnd1 = TREE_OPERAND (expr, 1);
1302 STRIP_NOPS (oprnd0);
1303 STRIP_NOPS (oprnd1);
1305 /* Recursively try to find the base of the address contained in EXPR.
1306 For offset, the returned base will be NULL. */
1307 base_addr0 = address_analysis (oprnd0, stmt, is_read, dr, &address_offset,
1308 &address_misalign, &address_aligned_to,
1309 step);
1311 base_addr1 = address_analysis (oprnd1, stmt, is_read, dr, &address_offset,
1312 &address_misalign, &address_aligned_to,
1313 step);
1315 /* We support cases where only one of the operands contains an
1316 address. */
1317 if ((base_addr0 && base_addr1) || (!base_addr0 && !base_addr1))
1319 if (dump_file && (dump_flags & TDF_DETAILS))
1321 fprintf (dump_file,
1322 "\neither more than one address or no addresses in expr ");
1323 print_generic_expr (dump_file, expr, TDF_SLIM);
1324 fprintf (dump_file, "\n");
1326 return NULL_TREE;
1329 /* To revert STRIP_NOPS. */
1330 oprnd0 = TREE_OPERAND (expr, 0);
1331 oprnd1 = TREE_OPERAND (expr, 1);
1333 offset_expr = base_addr0 ?
1334 fold_convert (ssizetype, oprnd1) : fold_convert (ssizetype, oprnd0);
1336 /* EXPR is of form {base +/- offset} (or {offset +/- base}). If offset is
1337 a number, we can add it to the misalignment value calculated for base,
1338 otherwise, misalignment is NULL. */
1339 if (TREE_CODE (offset_expr) == INTEGER_CST && address_misalign)
1341 *misalign = size_binop (TREE_CODE (expr), address_misalign,
1342 offset_expr);
1343 *aligned_to = address_aligned_to;
1345 else
1347 *misalign = NULL_TREE;
1348 *aligned_to = NULL_TREE;
1351 /* Combine offset (from EXPR {base + offset}) with the offset calculated
1352 for base. */
1353 *offset = size_binop (TREE_CODE (expr), address_offset, offset_expr);
1354 return base_addr0 ? base_addr0 : base_addr1;
1356 case ADDR_EXPR:
1357 base_address = object_analysis (TREE_OPERAND (expr, 0), stmt, is_read,
1358 &dr, offset, misalign, aligned_to, step,
1359 &dummy, &dummy1, &dummy2);
1360 return base_address;
1362 case SSA_NAME:
1363 if (!POINTER_TYPE_P (TREE_TYPE (expr)))
1365 if (dump_file && (dump_flags & TDF_DETAILS))
1367 fprintf (dump_file, "\nnot pointer SSA_NAME ");
1368 print_generic_expr (dump_file, expr, TDF_SLIM);
1369 fprintf (dump_file, "\n");
1371 return NULL_TREE;
1373 *aligned_to = ssize_int (TYPE_ALIGN_UNIT (TREE_TYPE (TREE_TYPE (expr))));
1374 *misalign = ssize_int (0);
1375 *offset = ssize_int (0);
1376 *step = ssize_int (0);
1377 return expr;
1379 default:
1380 return NULL_TREE;
1385 /* Function object_analysis
1387 Create a data-reference structure DR for MEMREF.
1388 Return the BASE of the data reference MEMREF if the analysis is possible.
1389 Also compute the INITIAL_OFFSET from BASE, MISALIGN and STEP.
1390 E.g., for EXPR a.b[i] + 4B, BASE is a, and OFFSET is the overall offset
1391 'a.b[i] + 4B' from a (can be an expression), MISALIGN is an OFFSET
1392 instantiated with initial_conditions of access_functions of variables,
1393 and STEP is the evolution of the DR_REF in this loop.
1395 Function get_inner_reference is used for the above in case of ARRAY_REF and
1396 COMPONENT_REF.
1398 The structure of the function is as follows:
1399 Part 1:
1400 Case 1. For handled_component_p refs
1401 1.1 build data-reference structure for MEMREF
1402 1.2 call get_inner_reference
1403 1.2.1 analyze offset expr received from get_inner_reference
1404 (fall through with BASE)
1405 Case 2. For declarations
1406 2.1 set MEMTAG
1407 Case 3. For INDIRECT_REFs
1408 3.1 build data-reference structure for MEMREF
1409 3.2 analyze evolution and initial condition of MEMREF
1410 3.3 set data-reference structure for MEMREF
1411 3.4 call address_analysis to analyze INIT of the access function
1412 3.5 extract memory tag
1414 Part 2:
1415 Combine the results of object and address analysis to calculate
1416 INITIAL_OFFSET, STEP and misalignment info.
1418 Input:
1419 MEMREF - the memory reference that is being analyzed
1420 STMT - the statement that contains MEMREF
1421 IS_READ - TRUE if STMT reads from MEMREF, FALSE if writes to MEMREF
1423 Output:
1424 BASE_ADDRESS (returned value) - the base address of the data reference MEMREF
1425 E.g, if MEMREF is a.b[k].c[i][j] the returned
1426 base is &a.
1427 DR - data_reference struct for MEMREF
1428 INITIAL_OFFSET - initial offset of MEMREF from BASE (an expression)
1429 MISALIGN - offset of MEMREF from BASE in bytes (a constant) modulo alignment of
1430 ALIGNMENT or NULL_TREE if the computation is impossible
1431 ALIGNED_TO - maximum alignment of EXPR or NULL_TREE if MISALIGN can be
1432 calculated (doesn't depend on variables)
1433 STEP - evolution of the DR_REF in the loop
1434 MEMTAG - memory tag for aliasing purposes
1435 PTR_INFO - NULL or points-to aliasing info from a pointer SSA_NAME
1436 SUBVARS - Sub-variables of the variable
1438 If the analysis of MEMREF evolution in the loop fails, NULL_TREE is returned,
1439 but DR can be created anyway.
1443 static tree
1444 object_analysis (tree memref, tree stmt, bool is_read,
1445 struct data_reference **dr, tree *offset, tree *misalign,
1446 tree *aligned_to, tree *step, tree *memtag,
1447 struct ptr_info_def **ptr_info, subvar_t *subvars)
1449 tree base = NULL_TREE, base_address = NULL_TREE;
1450 tree object_offset = ssize_int (0), object_misalign = ssize_int (0);
1451 tree object_step = ssize_int (0), address_step = ssize_int (0);
1452 tree address_offset = ssize_int (0), address_misalign = ssize_int (0);
1453 HOST_WIDE_INT pbitsize, pbitpos;
1454 tree poffset, bit_pos_in_bytes;
1455 enum machine_mode pmode;
1456 int punsignedp, pvolatilep;
1457 tree ptr_step = ssize_int (0), ptr_init = NULL_TREE;
1458 struct loop *loop = loop_containing_stmt (stmt);
1459 struct data_reference *ptr_dr = NULL;
1460 tree object_aligned_to = NULL_TREE, address_aligned_to = NULL_TREE;
1462 *ptr_info = NULL;
1464 /* Part 1: */
1465 /* Case 1. handled_component_p refs. */
1466 if (handled_component_p (memref))
1468 /* 1.1 build data-reference structure for MEMREF. */
1469 /* TODO: handle COMPONENT_REFs. */
1470 if (!(*dr))
1472 if (TREE_CODE (memref) == ARRAY_REF)
1473 *dr = analyze_array (stmt, memref, is_read);
1474 else
1476 /* FORNOW. */
1477 if (dump_file && (dump_flags & TDF_DETAILS))
1479 fprintf (dump_file, "\ndata-ref of unsupported type ");
1480 print_generic_expr (dump_file, memref, TDF_SLIM);
1481 fprintf (dump_file, "\n");
1483 return NULL_TREE;
1487 /* 1.2 call get_inner_reference. */
1488 /* Find the base and the offset from it. */
1489 base = get_inner_reference (memref, &pbitsize, &pbitpos, &poffset,
1490 &pmode, &punsignedp, &pvolatilep, false);
1491 if (!base)
1493 if (dump_file && (dump_flags & TDF_DETAILS))
1495 fprintf (dump_file, "\nfailed to get inner ref for ");
1496 print_generic_expr (dump_file, memref, TDF_SLIM);
1497 fprintf (dump_file, "\n");
1499 return NULL_TREE;
1502 /* 1.2.1 analyze offset expr received from get_inner_reference. */
1503 if (poffset
1504 && !analyze_offset_expr (poffset, loop, &object_offset,
1505 &object_misalign, &object_aligned_to,
1506 &object_step))
1508 if (dump_file && (dump_flags & TDF_DETAILS))
1510 fprintf (dump_file, "\nfailed to compute offset or step for ");
1511 print_generic_expr (dump_file, memref, TDF_SLIM);
1512 fprintf (dump_file, "\n");
1514 return NULL_TREE;
1517 /* Add bit position to OFFSET and MISALIGN. */
1519 bit_pos_in_bytes = ssize_int (pbitpos/BITS_PER_UNIT);
1520 /* Check that there is no remainder in bits. */
1521 if (pbitpos%BITS_PER_UNIT)
1523 if (dump_file && (dump_flags & TDF_DETAILS))
1524 fprintf (dump_file, "\nbit offset alignment.\n");
1525 return NULL_TREE;
1527 object_offset = size_binop (PLUS_EXPR, bit_pos_in_bytes, object_offset);
1528 if (object_misalign)
1529 object_misalign = size_binop (PLUS_EXPR, object_misalign,
1530 bit_pos_in_bytes);
1532 memref = base; /* To continue analysis of BASE. */
1533 /* fall through */
1536 /* Part 1: Case 2. Declarations. */
1537 if (DECL_P (memref))
1539 /* We expect to get a decl only if we already have a DR. */
1540 if (!(*dr))
1542 if (dump_file && (dump_flags & TDF_DETAILS))
1544 fprintf (dump_file, "\nunhandled decl ");
1545 print_generic_expr (dump_file, memref, TDF_SLIM);
1546 fprintf (dump_file, "\n");
1548 return NULL_TREE;
1551 /* TODO: if during the analysis of INDIRECT_REF we get to an object, put
1552 the object in BASE_OBJECT field if we can prove that this is O.K.,
1553 i.e., the data-ref access is bounded by the bounds of the BASE_OBJECT.
1554 (e.g., if the object is an array base 'a', where 'a[N]', we must prove
1555 that every access with 'p' (the original INDIRECT_REF based on '&a')
1556 in the loop is within the array boundaries - from a[0] to a[N-1]).
1557 Otherwise, our alias analysis can be incorrect.
1558 Even if an access function based on BASE_OBJECT can't be build, update
1559 BASE_OBJECT field to enable us to prove that two data-refs are
1560 different (without access function, distance analysis is impossible).
1562 if (SSA_VAR_P (memref) && var_can_have_subvars (memref))
1563 *subvars = get_subvars_for_var (memref);
1564 base_address = build_fold_addr_expr (memref);
1565 /* 2.1 set MEMTAG. */
1566 *memtag = memref;
1569 /* Part 1: Case 3. INDIRECT_REFs. */
1570 else if (TREE_CODE (memref) == INDIRECT_REF)
1572 tree ptr_ref = TREE_OPERAND (memref, 0);
1573 if (TREE_CODE (ptr_ref) == SSA_NAME)
1574 *ptr_info = SSA_NAME_PTR_INFO (ptr_ref);
1576 /* 3.1 build data-reference structure for MEMREF. */
1577 ptr_dr = analyze_indirect_ref (stmt, memref, is_read);
1578 if (!ptr_dr)
1580 if (dump_file && (dump_flags & TDF_DETAILS))
1582 fprintf (dump_file, "\nfailed to create dr for ");
1583 print_generic_expr (dump_file, memref, TDF_SLIM);
1584 fprintf (dump_file, "\n");
1586 return NULL_TREE;
1589 /* 3.2 analyze evolution and initial condition of MEMREF. */
1590 ptr_step = DR_STEP (ptr_dr);
1591 ptr_init = DR_BASE_ADDRESS (ptr_dr);
1592 if (!ptr_init || !ptr_step || !POINTER_TYPE_P (TREE_TYPE (ptr_init)))
1594 *dr = (*dr) ? *dr : ptr_dr;
1595 if (dump_file && (dump_flags & TDF_DETAILS))
1597 fprintf (dump_file, "\nbad pointer access ");
1598 print_generic_expr (dump_file, memref, TDF_SLIM);
1599 fprintf (dump_file, "\n");
1601 return NULL_TREE;
1604 if (integer_zerop (ptr_step) && !(*dr))
1606 if (dump_file && (dump_flags & TDF_DETAILS))
1607 fprintf (dump_file, "\nptr is loop invariant.\n");
1608 *dr = ptr_dr;
1609 return NULL_TREE;
1611 /* If there exists DR for MEMREF, we are analyzing the base of
1612 handled component (PTR_INIT), which not necessary has evolution in
1613 the loop. */
1615 object_step = size_binop (PLUS_EXPR, object_step, ptr_step);
1617 /* 3.3 set data-reference structure for MEMREF. */
1618 if (!*dr)
1619 *dr = ptr_dr;
1621 /* 3.4 call address_analysis to analyze INIT of the access
1622 function. */
1623 base_address = address_analysis (ptr_init, stmt, is_read, *dr,
1624 &address_offset, &address_misalign,
1625 &address_aligned_to, &address_step);
1626 if (!base_address)
1628 if (dump_file && (dump_flags & TDF_DETAILS))
1630 fprintf (dump_file, "\nfailed to analyze address ");
1631 print_generic_expr (dump_file, ptr_init, TDF_SLIM);
1632 fprintf (dump_file, "\n");
1634 return NULL_TREE;
1637 /* 3.5 extract memory tag. */
1638 switch (TREE_CODE (base_address))
1640 case SSA_NAME:
1641 *memtag = get_var_ann (SSA_NAME_VAR (base_address))->type_mem_tag;
1642 if (!(*memtag) && TREE_CODE (TREE_OPERAND (memref, 0)) == SSA_NAME)
1643 *memtag = get_var_ann (
1644 SSA_NAME_VAR (TREE_OPERAND (memref, 0)))->type_mem_tag;
1645 break;
1646 case ADDR_EXPR:
1647 *memtag = TREE_OPERAND (base_address, 0);
1648 break;
1649 default:
1650 if (dump_file && (dump_flags & TDF_DETAILS))
1652 fprintf (dump_file, "\nno memtag for ");
1653 print_generic_expr (dump_file, memref, TDF_SLIM);
1654 fprintf (dump_file, "\n");
1656 *memtag = NULL_TREE;
1657 break;
1661 if (!base_address)
1663 /* MEMREF cannot be analyzed. */
1664 if (dump_file && (dump_flags & TDF_DETAILS))
1666 fprintf (dump_file, "\ndata-ref of unsupported type ");
1667 print_generic_expr (dump_file, memref, TDF_SLIM);
1668 fprintf (dump_file, "\n");
1670 return NULL_TREE;
1673 if (SSA_VAR_P (*memtag) && var_can_have_subvars (*memtag))
1674 *subvars = get_subvars_for_var (*memtag);
1676 /* Part 2: Combine the results of object and address analysis to calculate
1677 INITIAL_OFFSET, STEP and misalignment info. */
1678 *offset = size_binop (PLUS_EXPR, object_offset, address_offset);
1680 if ((!object_misalign && !object_aligned_to)
1681 || (!address_misalign && !address_aligned_to))
1683 *misalign = NULL_TREE;
1684 *aligned_to = NULL_TREE;
1686 else
1688 if (object_misalign && address_misalign)
1689 *misalign = size_binop (PLUS_EXPR, object_misalign, address_misalign);
1690 else
1691 *misalign = object_misalign ? object_misalign : address_misalign;
1692 if (object_aligned_to && address_aligned_to)
1693 *aligned_to = size_binop (MIN_EXPR, object_aligned_to,
1694 address_aligned_to);
1695 else
1696 *aligned_to = object_aligned_to ?
1697 object_aligned_to : address_aligned_to;
1699 *step = size_binop (PLUS_EXPR, object_step, address_step);
1701 return base_address;
1704 /* Function analyze_offset.
1706 Extract INVARIANT and CONSTANT parts from OFFSET.
1709 static void
1710 analyze_offset (tree offset, tree *invariant, tree *constant)
1712 tree op0, op1, constant_0, constant_1, invariant_0, invariant_1;
1713 enum tree_code code = TREE_CODE (offset);
1715 *invariant = NULL_TREE;
1716 *constant = NULL_TREE;
1718 /* Not PLUS/MINUS expression - recursion stop condition. */
1719 if (code != PLUS_EXPR && code != MINUS_EXPR)
1721 if (TREE_CODE (offset) == INTEGER_CST)
1722 *constant = offset;
1723 else
1724 *invariant = offset;
1725 return;
1728 op0 = TREE_OPERAND (offset, 0);
1729 op1 = TREE_OPERAND (offset, 1);
1731 /* Recursive call with the operands. */
1732 analyze_offset (op0, &invariant_0, &constant_0);
1733 analyze_offset (op1, &invariant_1, &constant_1);
1735 /* Combine the results. */
1736 *constant = constant_0 ? constant_0 : constant_1;
1737 if (invariant_0 && invariant_1)
1738 *invariant =
1739 fold_build2 (code, TREE_TYPE (invariant_0), invariant_0, invariant_1);
1740 else
1741 *invariant = invariant_0 ? invariant_0 : invariant_1;
1745 /* Function create_data_ref.
1747 Create a data-reference structure for MEMREF. Set its DR_BASE_ADDRESS,
1748 DR_OFFSET, DR_INIT, DR_STEP, DR_OFFSET_MISALIGNMENT, DR_ALIGNED_TO,
1749 DR_MEMTAG, and DR_POINTSTO_INFO fields.
1751 Input:
1752 MEMREF - the memory reference that is being analyzed
1753 STMT - the statement that contains MEMREF
1754 IS_READ - TRUE if STMT reads from MEMREF, FALSE if writes to MEMREF
1756 Output:
1757 DR (returned value) - data_reference struct for MEMREF
1760 static struct data_reference *
1761 create_data_ref (tree memref, tree stmt, bool is_read)
1763 struct data_reference *dr = NULL;
1764 tree base_address, offset, step, misalign, memtag;
1765 struct loop *loop = loop_containing_stmt (stmt);
1766 tree invariant = NULL_TREE, constant = NULL_TREE;
1767 tree type_size, init_cond;
1768 struct ptr_info_def *ptr_info;
1769 subvar_t subvars = NULL;
1770 tree aligned_to;
1772 if (!memref)
1773 return NULL;
1775 base_address = object_analysis (memref, stmt, is_read, &dr, &offset,
1776 &misalign, &aligned_to, &step, &memtag,
1777 &ptr_info, &subvars);
1778 if (!dr || !base_address)
1780 if (dump_file && (dump_flags & TDF_DETAILS))
1782 fprintf (dump_file, "\ncreate_data_ref: failed to create a dr for ");
1783 print_generic_expr (dump_file, memref, TDF_SLIM);
1784 fprintf (dump_file, "\n");
1786 return NULL;
1789 DR_BASE_ADDRESS (dr) = base_address;
1790 DR_OFFSET (dr) = offset;
1791 DR_INIT (dr) = ssize_int (0);
1792 DR_STEP (dr) = step;
1793 DR_OFFSET_MISALIGNMENT (dr) = misalign;
1794 DR_ALIGNED_TO (dr) = aligned_to;
1795 DR_MEMTAG (dr) = memtag;
1796 DR_PTR_INFO (dr) = ptr_info;
1797 DR_SUBVARS (dr) = subvars;
1799 type_size = fold_convert (ssizetype, TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dr))));
1801 /* Change the access function for INIDIRECT_REFs, according to
1802 DR_BASE_ADDRESS. Analyze OFFSET calculated in object_analysis. OFFSET is
1803 an expression that can contain loop invariant expressions and constants.
1804 We put the constant part in the initial condition of the access function
1805 (for data dependence tests), and in DR_INIT of the data-ref. The loop
1806 invariant part is put in DR_OFFSET.
1807 The evolution part of the access function is STEP calculated in
1808 object_analysis divided by the size of data type.
1810 if (!DR_BASE_OBJECT (dr))
1812 tree access_fn;
1813 tree new_step;
1815 /* Extract CONSTANT and INVARIANT from OFFSET, and put them in DR_INIT and
1816 DR_OFFSET fields of DR. */
1817 analyze_offset (offset, &invariant, &constant);
1818 if (constant)
1820 DR_INIT (dr) = fold_convert (ssizetype, constant);
1821 init_cond = fold_build2 (TRUNC_DIV_EXPR, TREE_TYPE (constant),
1822 constant, type_size);
1824 else
1825 DR_INIT (dr) = init_cond = ssize_int (0);;
1827 if (invariant)
1828 DR_OFFSET (dr) = invariant;
1829 else
1830 DR_OFFSET (dr) = ssize_int (0);
1832 /* Update access function. */
1833 access_fn = DR_ACCESS_FN (dr, 0);
1834 new_step = size_binop (TRUNC_DIV_EXPR,
1835 fold_convert (ssizetype, step), type_size);
1837 access_fn = chrec_replace_initial_condition (access_fn, init_cond);
1838 access_fn = reset_evolution_in_loop (loop->num, access_fn, new_step);
1840 VEC_replace (tree, DR_ACCESS_FNS (dr), 0, access_fn);
1843 if (dump_file && (dump_flags & TDF_DETAILS))
1845 struct ptr_info_def *pi = DR_PTR_INFO (dr);
1847 fprintf (dump_file, "\nCreated dr for ");
1848 print_generic_expr (dump_file, memref, TDF_SLIM);
1849 fprintf (dump_file, "\n\tbase_address: ");
1850 print_generic_expr (dump_file, DR_BASE_ADDRESS (dr), TDF_SLIM);
1851 fprintf (dump_file, "\n\toffset from base address: ");
1852 print_generic_expr (dump_file, DR_OFFSET (dr), TDF_SLIM);
1853 fprintf (dump_file, "\n\tconstant offset from base address: ");
1854 print_generic_expr (dump_file, DR_INIT (dr), TDF_SLIM);
1855 fprintf (dump_file, "\n\tbase_object: ");
1856 print_generic_expr (dump_file, DR_BASE_OBJECT (dr), TDF_SLIM);
1857 fprintf (dump_file, "\n\tstep: ");
1858 print_generic_expr (dump_file, DR_STEP (dr), TDF_SLIM);
1859 fprintf (dump_file, "B\n\tmisalignment from base: ");
1860 print_generic_expr (dump_file, DR_OFFSET_MISALIGNMENT (dr), TDF_SLIM);
1861 if (DR_OFFSET_MISALIGNMENT (dr))
1862 fprintf (dump_file, "B");
1863 if (DR_ALIGNED_TO (dr))
1865 fprintf (dump_file, "\n\taligned to: ");
1866 print_generic_expr (dump_file, DR_ALIGNED_TO (dr), TDF_SLIM);
1868 fprintf (dump_file, "\n\tmemtag: ");
1869 print_generic_expr (dump_file, DR_MEMTAG (dr), TDF_SLIM);
1870 fprintf (dump_file, "\n");
1871 if (pi && pi->name_mem_tag)
1873 fprintf (dump_file, "\n\tnametag: ");
1874 print_generic_expr (dump_file, pi->name_mem_tag, TDF_SLIM);
1875 fprintf (dump_file, "\n");
1878 return dr;
1882 /* Returns true when all the functions of a tree_vec CHREC are the
1883 same. */
1885 static bool
1886 all_chrecs_equal_p (tree chrec)
1888 int j;
1890 for (j = 0; j < TREE_VEC_LENGTH (chrec) - 1; j++)
1892 tree chrec_j = TREE_VEC_ELT (chrec, j);
1893 tree chrec_j_1 = TREE_VEC_ELT (chrec, j + 1);
1894 if (!integer_zerop
1895 (chrec_fold_minus
1896 (integer_type_node, chrec_j, chrec_j_1)))
1897 return false;
1899 return true;
1902 /* Determine for each subscript in the data dependence relation DDR
1903 the distance. */
1905 void
1906 compute_subscript_distance (struct data_dependence_relation *ddr)
1908 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
1910 unsigned int i;
1912 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
1914 tree conflicts_a, conflicts_b, difference;
1915 struct subscript *subscript;
1917 subscript = DDR_SUBSCRIPT (ddr, i);
1918 conflicts_a = SUB_CONFLICTS_IN_A (subscript);
1919 conflicts_b = SUB_CONFLICTS_IN_B (subscript);
1921 if (TREE_CODE (conflicts_a) == TREE_VEC)
1923 if (!all_chrecs_equal_p (conflicts_a))
1925 SUB_DISTANCE (subscript) = chrec_dont_know;
1926 return;
1928 else
1929 conflicts_a = TREE_VEC_ELT (conflicts_a, 0);
1932 if (TREE_CODE (conflicts_b) == TREE_VEC)
1934 if (!all_chrecs_equal_p (conflicts_b))
1936 SUB_DISTANCE (subscript) = chrec_dont_know;
1937 return;
1939 else
1940 conflicts_b = TREE_VEC_ELT (conflicts_b, 0);
1943 difference = chrec_fold_minus
1944 (integer_type_node, conflicts_b, conflicts_a);
1946 if (evolution_function_is_constant_p (difference))
1947 SUB_DISTANCE (subscript) = difference;
1949 else
1950 SUB_DISTANCE (subscript) = chrec_dont_know;
1955 /* Initialize a ddr. */
1957 struct data_dependence_relation *
1958 initialize_data_dependence_relation (struct data_reference *a,
1959 struct data_reference *b)
1961 struct data_dependence_relation *res;
1962 bool differ_p;
1963 unsigned int i;
1965 res = xmalloc (sizeof (struct data_dependence_relation));
1966 DDR_A (res) = a;
1967 DDR_B (res) = b;
1969 if (a == NULL || b == NULL)
1971 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
1972 return res;
1975 /* When A and B are arrays and their dimensions differ, we directly
1976 initialize the relation to "there is no dependence": chrec_known. */
1977 if (DR_BASE_OBJECT (a) && DR_BASE_OBJECT (b)
1978 && DR_NUM_DIMENSIONS (a) != DR_NUM_DIMENSIONS (b))
1980 DDR_ARE_DEPENDENT (res) = chrec_known;
1981 return res;
1984 /* Compare the bases of the data-refs. */
1985 if (!base_addr_differ_p (a, b, &differ_p))
1987 /* Can't determine whether the data-refs access the same memory
1988 region. */
1989 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
1990 return res;
1992 if (differ_p)
1994 DDR_ARE_DEPENDENT (res) = chrec_known;
1995 return res;
1998 DDR_AFFINE_P (res) = true;
1999 DDR_ARE_DEPENDENT (res) = NULL_TREE;
2000 DDR_SUBSCRIPTS_VECTOR_INIT (res, DR_NUM_DIMENSIONS (a));
2001 DDR_SIZE_VECT (res) = 0;
2002 DDR_DIST_VECT (res) = NULL;
2003 DDR_DIR_VECT (res) = NULL;
2005 for (i = 0; i < DR_NUM_DIMENSIONS (a); i++)
2007 struct subscript *subscript;
2009 subscript = xmalloc (sizeof (struct subscript));
2010 SUB_CONFLICTS_IN_A (subscript) = chrec_dont_know;
2011 SUB_CONFLICTS_IN_B (subscript) = chrec_dont_know;
2012 SUB_LAST_CONFLICT (subscript) = chrec_dont_know;
2013 SUB_DISTANCE (subscript) = chrec_dont_know;
2014 VARRAY_PUSH_GENERIC_PTR (DDR_SUBSCRIPTS (res), subscript);
2017 return res;
2020 /* Set DDR_ARE_DEPENDENT to CHREC and finalize the subscript overlap
2021 description. */
2023 static inline void
2024 finalize_ddr_dependent (struct data_dependence_relation *ddr,
2025 tree chrec)
2027 if (dump_file && (dump_flags & TDF_DETAILS))
2029 fprintf (dump_file, "(dependence classified: ");
2030 print_generic_expr (dump_file, chrec, 0);
2031 fprintf (dump_file, ")\n");
2034 DDR_ARE_DEPENDENT (ddr) = chrec;
2035 varray_clear (DDR_SUBSCRIPTS (ddr));
2038 /* The dependence relation DDR cannot be represented by a distance
2039 vector. */
2041 static inline void
2042 non_affine_dependence_relation (struct data_dependence_relation *ddr)
2044 if (dump_file && (dump_flags & TDF_DETAILS))
2045 fprintf (dump_file, "(Dependence relation cannot be represented by distance vector.) \n");
2047 DDR_AFFINE_P (ddr) = false;
2052 /* This section contains the classic Banerjee tests. */
2054 /* Returns true iff CHREC_A and CHREC_B are not dependent on any index
2055 variables, i.e., if the ZIV (Zero Index Variable) test is true. */
2057 static inline bool
2058 ziv_subscript_p (tree chrec_a,
2059 tree chrec_b)
2061 return (evolution_function_is_constant_p (chrec_a)
2062 && evolution_function_is_constant_p (chrec_b));
2065 /* Returns true iff CHREC_A and CHREC_B are dependent on an index
2066 variable, i.e., if the SIV (Single Index Variable) test is true. */
2068 static bool
2069 siv_subscript_p (tree chrec_a,
2070 tree chrec_b)
2072 if ((evolution_function_is_constant_p (chrec_a)
2073 && evolution_function_is_univariate_p (chrec_b))
2074 || (evolution_function_is_constant_p (chrec_b)
2075 && evolution_function_is_univariate_p (chrec_a)))
2076 return true;
2078 if (evolution_function_is_univariate_p (chrec_a)
2079 && evolution_function_is_univariate_p (chrec_b))
2081 switch (TREE_CODE (chrec_a))
2083 case POLYNOMIAL_CHREC:
2084 switch (TREE_CODE (chrec_b))
2086 case POLYNOMIAL_CHREC:
2087 if (CHREC_VARIABLE (chrec_a) != CHREC_VARIABLE (chrec_b))
2088 return false;
2090 default:
2091 return true;
2094 default:
2095 return true;
2099 return false;
2102 /* Analyze a ZIV (Zero Index Variable) subscript. *OVERLAPS_A and
2103 *OVERLAPS_B are initialized to the functions that describe the
2104 relation between the elements accessed twice by CHREC_A and
2105 CHREC_B. For k >= 0, the following property is verified:
2107 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
2109 static void
2110 analyze_ziv_subscript (tree chrec_a,
2111 tree chrec_b,
2112 tree *overlaps_a,
2113 tree *overlaps_b,
2114 tree *last_conflicts)
2116 tree difference;
2118 if (dump_file && (dump_flags & TDF_DETAILS))
2119 fprintf (dump_file, "(analyze_ziv_subscript \n");
2121 difference = chrec_fold_minus (integer_type_node, chrec_a, chrec_b);
2123 switch (TREE_CODE (difference))
2125 case INTEGER_CST:
2126 if (integer_zerop (difference))
2128 /* The difference is equal to zero: the accessed index
2129 overlaps for each iteration in the loop. */
2130 *overlaps_a = integer_zero_node;
2131 *overlaps_b = integer_zero_node;
2132 *last_conflicts = chrec_dont_know;
2134 else
2136 /* The accesses do not overlap. */
2137 *overlaps_a = chrec_known;
2138 *overlaps_b = chrec_known;
2139 *last_conflicts = integer_zero_node;
2141 break;
2143 default:
2144 /* We're not sure whether the indexes overlap. For the moment,
2145 conservatively answer "don't know". */
2146 *overlaps_a = chrec_dont_know;
2147 *overlaps_b = chrec_dont_know;
2148 *last_conflicts = chrec_dont_know;
2149 break;
2152 if (dump_file && (dump_flags & TDF_DETAILS))
2153 fprintf (dump_file, ")\n");
2156 /* Get the real or estimated number of iterations for LOOPNUM, whichever is
2157 available. Return the number of iterations as a tree, or NULL_TREE if
2158 we don't know. */
2160 static tree
2161 get_number_of_iters_for_loop (int loopnum)
2163 tree numiter = number_of_iterations_in_loop (current_loops->parray[loopnum]);
2165 if (TREE_CODE (numiter) != INTEGER_CST)
2166 numiter = current_loops->parray[loopnum]->estimated_nb_iterations;
2167 if (chrec_contains_undetermined (numiter))
2168 return NULL_TREE;
2169 return numiter;
2172 /* Analyze a SIV (Single Index Variable) subscript where CHREC_A is a
2173 constant, and CHREC_B is an affine function. *OVERLAPS_A and
2174 *OVERLAPS_B are initialized to the functions that describe the
2175 relation between the elements accessed twice by CHREC_A and
2176 CHREC_B. For k >= 0, the following property is verified:
2178 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
2180 static void
2181 analyze_siv_subscript_cst_affine (tree chrec_a,
2182 tree chrec_b,
2183 tree *overlaps_a,
2184 tree *overlaps_b,
2185 tree *last_conflicts)
2187 bool value0, value1, value2;
2188 tree difference = chrec_fold_minus
2189 (integer_type_node, CHREC_LEFT (chrec_b), chrec_a);
2191 if (!chrec_is_positive (initial_condition (difference), &value0))
2193 *overlaps_a = chrec_dont_know;
2194 *overlaps_b = chrec_dont_know;
2195 *last_conflicts = chrec_dont_know;
2196 return;
2198 else
2200 if (value0 == false)
2202 if (!chrec_is_positive (CHREC_RIGHT (chrec_b), &value1))
2204 *overlaps_a = chrec_dont_know;
2205 *overlaps_b = chrec_dont_know;
2206 *last_conflicts = chrec_dont_know;
2207 return;
2209 else
2211 if (value1 == true)
2213 /* Example:
2214 chrec_a = 12
2215 chrec_b = {10, +, 1}
2218 if (tree_fold_divides_p (CHREC_RIGHT (chrec_b), difference))
2220 tree numiter;
2221 int loopnum = CHREC_VARIABLE (chrec_b);
2223 *overlaps_a = integer_zero_node;
2224 *overlaps_b = fold_build2 (EXACT_DIV_EXPR, integer_type_node,
2225 fold_build1 (ABS_EXPR,
2226 integer_type_node,
2227 difference),
2228 CHREC_RIGHT (chrec_b));
2229 *last_conflicts = integer_one_node;
2232 /* Perform weak-zero siv test to see if overlap is
2233 outside the loop bounds. */
2234 numiter = get_number_of_iters_for_loop (loopnum);
2236 if (numiter != NULL_TREE
2237 && TREE_CODE (*overlaps_b) == INTEGER_CST
2238 && tree_int_cst_lt (numiter, *overlaps_b))
2240 *overlaps_a = chrec_known;
2241 *overlaps_b = chrec_known;
2242 *last_conflicts = integer_zero_node;
2243 return;
2245 return;
2248 /* When the step does not divide the difference, there are
2249 no overlaps. */
2250 else
2252 *overlaps_a = chrec_known;
2253 *overlaps_b = chrec_known;
2254 *last_conflicts = integer_zero_node;
2255 return;
2259 else
2261 /* Example:
2262 chrec_a = 12
2263 chrec_b = {10, +, -1}
2265 In this case, chrec_a will not overlap with chrec_b. */
2266 *overlaps_a = chrec_known;
2267 *overlaps_b = chrec_known;
2268 *last_conflicts = integer_zero_node;
2269 return;
2273 else
2275 if (!chrec_is_positive (CHREC_RIGHT (chrec_b), &value2))
2277 *overlaps_a = chrec_dont_know;
2278 *overlaps_b = chrec_dont_know;
2279 *last_conflicts = chrec_dont_know;
2280 return;
2282 else
2284 if (value2 == false)
2286 /* Example:
2287 chrec_a = 3
2288 chrec_b = {10, +, -1}
2290 if (tree_fold_divides_p (CHREC_RIGHT (chrec_b), difference))
2292 tree numiter;
2293 int loopnum = CHREC_VARIABLE (chrec_b);
2295 *overlaps_a = integer_zero_node;
2296 *overlaps_b = fold_build2 (EXACT_DIV_EXPR,
2297 integer_type_node, difference,
2298 CHREC_RIGHT (chrec_b));
2299 *last_conflicts = integer_one_node;
2301 /* Perform weak-zero siv test to see if overlap is
2302 outside the loop bounds. */
2303 numiter = get_number_of_iters_for_loop (loopnum);
2305 if (numiter != NULL_TREE
2306 && TREE_CODE (*overlaps_b) == INTEGER_CST
2307 && tree_int_cst_lt (numiter, *overlaps_b))
2309 *overlaps_a = chrec_known;
2310 *overlaps_b = chrec_known;
2311 *last_conflicts = integer_zero_node;
2312 return;
2314 return;
2317 /* When the step does not divide the difference, there
2318 are no overlaps. */
2319 else
2321 *overlaps_a = chrec_known;
2322 *overlaps_b = chrec_known;
2323 *last_conflicts = integer_zero_node;
2324 return;
2327 else
2329 /* Example:
2330 chrec_a = 3
2331 chrec_b = {4, +, 1}
2333 In this case, chrec_a will not overlap with chrec_b. */
2334 *overlaps_a = chrec_known;
2335 *overlaps_b = chrec_known;
2336 *last_conflicts = integer_zero_node;
2337 return;
2344 /* Helper recursive function for initializing the matrix A. Returns
2345 the initial value of CHREC. */
2347 static int
2348 initialize_matrix_A (lambda_matrix A, tree chrec, unsigned index, int mult)
2350 gcc_assert (chrec);
2352 if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
2353 return int_cst_value (chrec);
2355 A[index][0] = mult * int_cst_value (CHREC_RIGHT (chrec));
2356 return initialize_matrix_A (A, CHREC_LEFT (chrec), index + 1, mult);
2359 #define FLOOR_DIV(x,y) ((x) / (y))
2361 /* Solves the special case of the Diophantine equation:
2362 | {0, +, STEP_A}_x (OVERLAPS_A) = {0, +, STEP_B}_y (OVERLAPS_B)
2364 Computes the descriptions OVERLAPS_A and OVERLAPS_B. NITER is the
2365 number of iterations that loops X and Y run. The overlaps will be
2366 constructed as evolutions in dimension DIM. */
2368 static void
2369 compute_overlap_steps_for_affine_univar (int niter, int step_a, int step_b,
2370 tree *overlaps_a, tree *overlaps_b,
2371 tree *last_conflicts, int dim)
2373 if (((step_a > 0 && step_b > 0)
2374 || (step_a < 0 && step_b < 0)))
2376 int step_overlaps_a, step_overlaps_b;
2377 int gcd_steps_a_b, last_conflict, tau2;
2379 gcd_steps_a_b = gcd (step_a, step_b);
2380 step_overlaps_a = step_b / gcd_steps_a_b;
2381 step_overlaps_b = step_a / gcd_steps_a_b;
2383 tau2 = FLOOR_DIV (niter, step_overlaps_a);
2384 tau2 = MIN (tau2, FLOOR_DIV (niter, step_overlaps_b));
2385 last_conflict = tau2;
2387 *overlaps_a = build_polynomial_chrec
2388 (dim, integer_zero_node,
2389 build_int_cst (NULL_TREE, step_overlaps_a));
2390 *overlaps_b = build_polynomial_chrec
2391 (dim, integer_zero_node,
2392 build_int_cst (NULL_TREE, step_overlaps_b));
2393 *last_conflicts = build_int_cst (NULL_TREE, last_conflict);
2396 else
2398 *overlaps_a = integer_zero_node;
2399 *overlaps_b = integer_zero_node;
2400 *last_conflicts = integer_zero_node;
2405 /* Solves the special case of a Diophantine equation where CHREC_A is
2406 an affine bivariate function, and CHREC_B is an affine univariate
2407 function. For example,
2409 | {{0, +, 1}_x, +, 1335}_y = {0, +, 1336}_z
2411 has the following overlapping functions:
2413 | x (t, u, v) = {{0, +, 1336}_t, +, 1}_v
2414 | y (t, u, v) = {{0, +, 1336}_u, +, 1}_v
2415 | z (t, u, v) = {{{0, +, 1}_t, +, 1335}_u, +, 1}_v
2417 FORNOW: This is a specialized implementation for a case occurring in
2418 a common benchmark. Implement the general algorithm. */
2420 static void
2421 compute_overlap_steps_for_affine_1_2 (tree chrec_a, tree chrec_b,
2422 tree *overlaps_a, tree *overlaps_b,
2423 tree *last_conflicts)
2425 bool xz_p, yz_p, xyz_p;
2426 int step_x, step_y, step_z;
2427 int niter_x, niter_y, niter_z, niter;
2428 tree numiter_x, numiter_y, numiter_z;
2429 tree overlaps_a_xz, overlaps_b_xz, last_conflicts_xz;
2430 tree overlaps_a_yz, overlaps_b_yz, last_conflicts_yz;
2431 tree overlaps_a_xyz, overlaps_b_xyz, last_conflicts_xyz;
2433 step_x = int_cst_value (CHREC_RIGHT (CHREC_LEFT (chrec_a)));
2434 step_y = int_cst_value (CHREC_RIGHT (chrec_a));
2435 step_z = int_cst_value (CHREC_RIGHT (chrec_b));
2437 numiter_x = get_number_of_iters_for_loop (CHREC_VARIABLE (CHREC_LEFT (chrec_a)));
2438 numiter_y = get_number_of_iters_for_loop (CHREC_VARIABLE (chrec_a));
2439 numiter_z = get_number_of_iters_for_loop (CHREC_VARIABLE (chrec_b));
2441 if (numiter_x == NULL_TREE || numiter_y == NULL_TREE
2442 || numiter_z == NULL_TREE)
2444 *overlaps_a = chrec_dont_know;
2445 *overlaps_b = chrec_dont_know;
2446 *last_conflicts = chrec_dont_know;
2447 return;
2450 niter_x = int_cst_value (numiter_x);
2451 niter_y = int_cst_value (numiter_y);
2452 niter_z = int_cst_value (numiter_z);
2454 niter = MIN (niter_x, niter_z);
2455 compute_overlap_steps_for_affine_univar (niter, step_x, step_z,
2456 &overlaps_a_xz,
2457 &overlaps_b_xz,
2458 &last_conflicts_xz, 1);
2459 niter = MIN (niter_y, niter_z);
2460 compute_overlap_steps_for_affine_univar (niter, step_y, step_z,
2461 &overlaps_a_yz,
2462 &overlaps_b_yz,
2463 &last_conflicts_yz, 2);
2464 niter = MIN (niter_x, niter_z);
2465 niter = MIN (niter_y, niter);
2466 compute_overlap_steps_for_affine_univar (niter, step_x + step_y, step_z,
2467 &overlaps_a_xyz,
2468 &overlaps_b_xyz,
2469 &last_conflicts_xyz, 3);
2471 xz_p = !integer_zerop (last_conflicts_xz);
2472 yz_p = !integer_zerop (last_conflicts_yz);
2473 xyz_p = !integer_zerop (last_conflicts_xyz);
2475 if (xz_p || yz_p || xyz_p)
2477 *overlaps_a = make_tree_vec (2);
2478 TREE_VEC_ELT (*overlaps_a, 0) = integer_zero_node;
2479 TREE_VEC_ELT (*overlaps_a, 1) = integer_zero_node;
2480 *overlaps_b = integer_zero_node;
2481 if (xz_p)
2483 TREE_VEC_ELT (*overlaps_a, 0) =
2484 chrec_fold_plus (integer_type_node, TREE_VEC_ELT (*overlaps_a, 0),
2485 overlaps_a_xz);
2486 *overlaps_b =
2487 chrec_fold_plus (integer_type_node, *overlaps_b, overlaps_b_xz);
2488 *last_conflicts = last_conflicts_xz;
2490 if (yz_p)
2492 TREE_VEC_ELT (*overlaps_a, 1) =
2493 chrec_fold_plus (integer_type_node, TREE_VEC_ELT (*overlaps_a, 1),
2494 overlaps_a_yz);
2495 *overlaps_b =
2496 chrec_fold_plus (integer_type_node, *overlaps_b, overlaps_b_yz);
2497 *last_conflicts = last_conflicts_yz;
2499 if (xyz_p)
2501 TREE_VEC_ELT (*overlaps_a, 0) =
2502 chrec_fold_plus (integer_type_node, TREE_VEC_ELT (*overlaps_a, 0),
2503 overlaps_a_xyz);
2504 TREE_VEC_ELT (*overlaps_a, 1) =
2505 chrec_fold_plus (integer_type_node, TREE_VEC_ELT (*overlaps_a, 1),
2506 overlaps_a_xyz);
2507 *overlaps_b =
2508 chrec_fold_plus (integer_type_node, *overlaps_b, overlaps_b_xyz);
2509 *last_conflicts = last_conflicts_xyz;
2512 else
2514 *overlaps_a = integer_zero_node;
2515 *overlaps_b = integer_zero_node;
2516 *last_conflicts = integer_zero_node;
2520 /* Determines the overlapping elements due to accesses CHREC_A and
2521 CHREC_B, that are affine functions. This is a part of the
2522 subscript analyzer. */
2524 static void
2525 analyze_subscript_affine_affine (tree chrec_a,
2526 tree chrec_b,
2527 tree *overlaps_a,
2528 tree *overlaps_b,
2529 tree *last_conflicts)
2531 unsigned nb_vars_a, nb_vars_b, dim;
2532 int init_a, init_b, gamma, gcd_alpha_beta;
2533 int tau1, tau2;
2534 lambda_matrix A, U, S;
2535 tree difference = chrec_fold_minus (integer_type_node, chrec_a, chrec_b);
2537 if (integer_zerop (difference))
2539 /* The difference is equal to zero: the accessed index
2540 overlaps for each iteration in the loop. */
2541 *overlaps_a = integer_zero_node;
2542 *overlaps_b = integer_zero_node;
2543 *last_conflicts = chrec_dont_know;
2544 return;
2546 if (dump_file && (dump_flags & TDF_DETAILS))
2547 fprintf (dump_file, "(analyze_subscript_affine_affine \n");
2549 /* For determining the initial intersection, we have to solve a
2550 Diophantine equation. This is the most time consuming part.
2552 For answering to the question: "Is there a dependence?" we have
2553 to prove that there exists a solution to the Diophantine
2554 equation, and that the solution is in the iteration domain,
2555 i.e. the solution is positive or zero, and that the solution
2556 happens before the upper bound loop.nb_iterations. Otherwise
2557 there is no dependence. This function outputs a description of
2558 the iterations that hold the intersections. */
2561 nb_vars_a = nb_vars_in_chrec (chrec_a);
2562 nb_vars_b = nb_vars_in_chrec (chrec_b);
2564 dim = nb_vars_a + nb_vars_b;
2565 U = lambda_matrix_new (dim, dim);
2566 A = lambda_matrix_new (dim, 1);
2567 S = lambda_matrix_new (dim, 1);
2569 init_a = initialize_matrix_A (A, chrec_a, 0, 1);
2570 init_b = initialize_matrix_A (A, chrec_b, nb_vars_a, -1);
2571 gamma = init_b - init_a;
2573 /* Don't do all the hard work of solving the Diophantine equation
2574 when we already know the solution: for example,
2575 | {3, +, 1}_1
2576 | {3, +, 4}_2
2577 | gamma = 3 - 3 = 0.
2578 Then the first overlap occurs during the first iterations:
2579 | {3, +, 1}_1 ({0, +, 4}_x) = {3, +, 4}_2 ({0, +, 1}_x)
2581 if (gamma == 0)
2583 if (nb_vars_a == 1 && nb_vars_b == 1)
2585 int step_a, step_b;
2586 int niter, niter_a, niter_b;
2587 tree numiter_a, numiter_b;
2589 numiter_a = get_number_of_iters_for_loop (CHREC_VARIABLE (chrec_a));
2590 numiter_b = get_number_of_iters_for_loop (CHREC_VARIABLE (chrec_b));
2591 if (numiter_a == NULL_TREE || numiter_b == NULL_TREE)
2593 *overlaps_a = chrec_dont_know;
2594 *overlaps_b = chrec_dont_know;
2595 *last_conflicts = chrec_dont_know;
2596 return;
2599 niter_a = int_cst_value (numiter_a);
2600 niter_b = int_cst_value (numiter_b);
2601 niter = MIN (niter_a, niter_b);
2603 step_a = int_cst_value (CHREC_RIGHT (chrec_a));
2604 step_b = int_cst_value (CHREC_RIGHT (chrec_b));
2606 compute_overlap_steps_for_affine_univar (niter, step_a, step_b,
2607 overlaps_a, overlaps_b,
2608 last_conflicts, 1);
2611 else if (nb_vars_a == 2 && nb_vars_b == 1)
2612 compute_overlap_steps_for_affine_1_2
2613 (chrec_a, chrec_b, overlaps_a, overlaps_b, last_conflicts);
2615 else if (nb_vars_a == 1 && nb_vars_b == 2)
2616 compute_overlap_steps_for_affine_1_2
2617 (chrec_b, chrec_a, overlaps_b, overlaps_a, last_conflicts);
2619 else
2621 *overlaps_a = chrec_dont_know;
2622 *overlaps_b = chrec_dont_know;
2623 *last_conflicts = chrec_dont_know;
2625 return;
2628 /* U.A = S */
2629 lambda_matrix_right_hermite (A, dim, 1, S, U);
2631 if (S[0][0] < 0)
2633 S[0][0] *= -1;
2634 lambda_matrix_row_negate (U, dim, 0);
2636 gcd_alpha_beta = S[0][0];
2638 /* The classic "gcd-test". */
2639 if (!int_divides_p (gcd_alpha_beta, gamma))
2641 /* The "gcd-test" has determined that there is no integer
2642 solution, i.e. there is no dependence. */
2643 *overlaps_a = chrec_known;
2644 *overlaps_b = chrec_known;
2645 *last_conflicts = integer_zero_node;
2648 /* Both access functions are univariate. This includes SIV and MIV cases. */
2649 else if (nb_vars_a == 1 && nb_vars_b == 1)
2651 /* Both functions should have the same evolution sign. */
2652 if (((A[0][0] > 0 && -A[1][0] > 0)
2653 || (A[0][0] < 0 && -A[1][0] < 0)))
2655 /* The solutions are given by:
2657 | [GAMMA/GCD_ALPHA_BETA t].[u11 u12] = [x0]
2658 | [u21 u22] [y0]
2660 For a given integer t. Using the following variables,
2662 | i0 = u11 * gamma / gcd_alpha_beta
2663 | j0 = u12 * gamma / gcd_alpha_beta
2664 | i1 = u21
2665 | j1 = u22
2667 the solutions are:
2669 | x0 = i0 + i1 * t,
2670 | y0 = j0 + j1 * t. */
2672 int i0, j0, i1, j1;
2674 /* X0 and Y0 are the first iterations for which there is a
2675 dependence. X0, Y0 are two solutions of the Diophantine
2676 equation: chrec_a (X0) = chrec_b (Y0). */
2677 int x0, y0;
2678 int niter, niter_a, niter_b;
2679 tree numiter_a, numiter_b;
2681 numiter_a = get_number_of_iters_for_loop (CHREC_VARIABLE (chrec_a));
2682 numiter_b = get_number_of_iters_for_loop (CHREC_VARIABLE (chrec_b));
2684 if (numiter_a == NULL_TREE || numiter_b == NULL_TREE)
2686 *overlaps_a = chrec_dont_know;
2687 *overlaps_b = chrec_dont_know;
2688 *last_conflicts = chrec_dont_know;
2689 return;
2692 niter_a = int_cst_value (numiter_a);
2693 niter_b = int_cst_value (numiter_b);
2694 niter = MIN (niter_a, niter_b);
2696 i0 = U[0][0] * gamma / gcd_alpha_beta;
2697 j0 = U[0][1] * gamma / gcd_alpha_beta;
2698 i1 = U[1][0];
2699 j1 = U[1][1];
2701 if ((i1 == 0 && i0 < 0)
2702 || (j1 == 0 && j0 < 0))
2704 /* There is no solution.
2705 FIXME: The case "i0 > nb_iterations, j0 > nb_iterations"
2706 falls in here, but for the moment we don't look at the
2707 upper bound of the iteration domain. */
2708 *overlaps_a = chrec_known;
2709 *overlaps_b = chrec_known;
2710 *last_conflicts = integer_zero_node;
2713 else
2715 if (i1 > 0)
2717 tau1 = CEIL (-i0, i1);
2718 tau2 = FLOOR_DIV (niter - i0, i1);
2720 if (j1 > 0)
2722 int last_conflict, min_multiple;
2723 tau1 = MAX (tau1, CEIL (-j0, j1));
2724 tau2 = MIN (tau2, FLOOR_DIV (niter - j0, j1));
2726 x0 = i1 * tau1 + i0;
2727 y0 = j1 * tau1 + j0;
2729 /* At this point (x0, y0) is one of the
2730 solutions to the Diophantine equation. The
2731 next step has to compute the smallest
2732 positive solution: the first conflicts. */
2733 min_multiple = MIN (x0 / i1, y0 / j1);
2734 x0 -= i1 * min_multiple;
2735 y0 -= j1 * min_multiple;
2737 tau1 = (x0 - i0)/i1;
2738 last_conflict = tau2 - tau1;
2740 /* If the overlap occurs outside of the bounds of the
2741 loop, there is no dependence. */
2742 if (x0 > niter || y0 > niter)
2745 *overlaps_a = chrec_known;
2746 *overlaps_b = chrec_known;
2747 *last_conflicts = integer_zero_node;
2749 else
2751 *overlaps_a = build_polynomial_chrec
2753 build_int_cst (NULL_TREE, x0),
2754 build_int_cst (NULL_TREE, i1));
2755 *overlaps_b = build_polynomial_chrec
2757 build_int_cst (NULL_TREE, y0),
2758 build_int_cst (NULL_TREE, j1));
2759 *last_conflicts = build_int_cst (NULL_TREE, last_conflict);
2762 else
2764 /* FIXME: For the moment, the upper bound of the
2765 iteration domain for j is not checked. */
2766 *overlaps_a = chrec_dont_know;
2767 *overlaps_b = chrec_dont_know;
2768 *last_conflicts = chrec_dont_know;
2772 else
2774 /* FIXME: For the moment, the upper bound of the
2775 iteration domain for i is not checked. */
2776 *overlaps_a = chrec_dont_know;
2777 *overlaps_b = chrec_dont_know;
2778 *last_conflicts = chrec_dont_know;
2782 else
2784 *overlaps_a = chrec_dont_know;
2785 *overlaps_b = chrec_dont_know;
2786 *last_conflicts = chrec_dont_know;
2790 else
2792 *overlaps_a = chrec_dont_know;
2793 *overlaps_b = chrec_dont_know;
2794 *last_conflicts = chrec_dont_know;
2798 if (dump_file && (dump_flags & TDF_DETAILS))
2800 fprintf (dump_file, " (overlaps_a = ");
2801 print_generic_expr (dump_file, *overlaps_a, 0);
2802 fprintf (dump_file, ")\n (overlaps_b = ");
2803 print_generic_expr (dump_file, *overlaps_b, 0);
2804 fprintf (dump_file, ")\n");
2807 if (dump_file && (dump_flags & TDF_DETAILS))
2808 fprintf (dump_file, ")\n");
2811 /* Analyze a SIV (Single Index Variable) subscript. *OVERLAPS_A and
2812 *OVERLAPS_B are initialized to the functions that describe the
2813 relation between the elements accessed twice by CHREC_A and
2814 CHREC_B. For k >= 0, the following property is verified:
2816 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
2818 static void
2819 analyze_siv_subscript (tree chrec_a,
2820 tree chrec_b,
2821 tree *overlaps_a,
2822 tree *overlaps_b,
2823 tree *last_conflicts)
2825 if (dump_file && (dump_flags & TDF_DETAILS))
2826 fprintf (dump_file, "(analyze_siv_subscript \n");
2828 if (evolution_function_is_constant_p (chrec_a)
2829 && evolution_function_is_affine_p (chrec_b))
2830 analyze_siv_subscript_cst_affine (chrec_a, chrec_b,
2831 overlaps_a, overlaps_b, last_conflicts);
2833 else if (evolution_function_is_affine_p (chrec_a)
2834 && evolution_function_is_constant_p (chrec_b))
2835 analyze_siv_subscript_cst_affine (chrec_b, chrec_a,
2836 overlaps_b, overlaps_a, last_conflicts);
2838 else if (evolution_function_is_affine_p (chrec_a)
2839 && evolution_function_is_affine_p (chrec_b))
2840 analyze_subscript_affine_affine (chrec_a, chrec_b,
2841 overlaps_a, overlaps_b, last_conflicts);
2842 else
2844 *overlaps_a = chrec_dont_know;
2845 *overlaps_b = chrec_dont_know;
2846 *last_conflicts = chrec_dont_know;
2849 if (dump_file && (dump_flags & TDF_DETAILS))
2850 fprintf (dump_file, ")\n");
2853 /* Return true when the evolution steps of an affine CHREC divide the
2854 constant CST. */
2856 static bool
2857 chrec_steps_divide_constant_p (tree chrec,
2858 tree cst)
2860 switch (TREE_CODE (chrec))
2862 case POLYNOMIAL_CHREC:
2863 return (tree_fold_divides_p (CHREC_RIGHT (chrec), cst)
2864 && chrec_steps_divide_constant_p (CHREC_LEFT (chrec), cst));
2866 default:
2867 /* On the initial condition, return true. */
2868 return true;
2872 /* Analyze a MIV (Multiple Index Variable) subscript. *OVERLAPS_A and
2873 *OVERLAPS_B are initialized to the functions that describe the
2874 relation between the elements accessed twice by CHREC_A and
2875 CHREC_B. For k >= 0, the following property is verified:
2877 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
2879 static void
2880 analyze_miv_subscript (tree chrec_a,
2881 tree chrec_b,
2882 tree *overlaps_a,
2883 tree *overlaps_b,
2884 tree *last_conflicts)
2886 /* FIXME: This is a MIV subscript, not yet handled.
2887 Example: (A[{1, +, 1}_1] vs. A[{1, +, 1}_2]) that comes from
2888 (A[i] vs. A[j]).
2890 In the SIV test we had to solve a Diophantine equation with two
2891 variables. In the MIV case we have to solve a Diophantine
2892 equation with 2*n variables (if the subscript uses n IVs).
2894 tree difference;
2896 if (dump_file && (dump_flags & TDF_DETAILS))
2897 fprintf (dump_file, "(analyze_miv_subscript \n");
2899 difference = chrec_fold_minus (integer_type_node, chrec_a, chrec_b);
2901 if (chrec_zerop (difference))
2903 /* Access functions are the same: all the elements are accessed
2904 in the same order. */
2905 *overlaps_a = integer_zero_node;
2906 *overlaps_b = integer_zero_node;
2907 *last_conflicts = get_number_of_iters_for_loop (CHREC_VARIABLE (chrec_a));
2911 else if (evolution_function_is_constant_p (difference)
2912 /* For the moment, the following is verified:
2913 evolution_function_is_affine_multivariate_p (chrec_a) */
2914 && !chrec_steps_divide_constant_p (chrec_a, difference))
2916 /* testsuite/.../ssa-chrec-33.c
2917 {{21, +, 2}_1, +, -2}_2 vs. {{20, +, 2}_1, +, -2}_2
2919 The difference is 1, and the evolution steps are equal to 2,
2920 consequently there are no overlapping elements. */
2921 *overlaps_a = chrec_known;
2922 *overlaps_b = chrec_known;
2923 *last_conflicts = integer_zero_node;
2926 else if (evolution_function_is_affine_multivariate_p (chrec_a)
2927 && evolution_function_is_affine_multivariate_p (chrec_b))
2929 /* testsuite/.../ssa-chrec-35.c
2930 {0, +, 1}_2 vs. {0, +, 1}_3
2931 the overlapping elements are respectively located at iterations:
2932 {0, +, 1}_x and {0, +, 1}_x,
2933 in other words, we have the equality:
2934 {0, +, 1}_2 ({0, +, 1}_x) = {0, +, 1}_3 ({0, +, 1}_x)
2936 Other examples:
2937 {{0, +, 1}_1, +, 2}_2 ({0, +, 1}_x, {0, +, 1}_y) =
2938 {0, +, 1}_1 ({{0, +, 1}_x, +, 2}_y)
2940 {{0, +, 2}_1, +, 3}_2 ({0, +, 1}_y, {0, +, 1}_x) =
2941 {{0, +, 3}_1, +, 2}_2 ({0, +, 1}_x, {0, +, 1}_y)
2943 analyze_subscript_affine_affine (chrec_a, chrec_b,
2944 overlaps_a, overlaps_b, last_conflicts);
2947 else
2949 /* When the analysis is too difficult, answer "don't know". */
2950 *overlaps_a = chrec_dont_know;
2951 *overlaps_b = chrec_dont_know;
2952 *last_conflicts = chrec_dont_know;
2955 if (dump_file && (dump_flags & TDF_DETAILS))
2956 fprintf (dump_file, ")\n");
2959 /* Determines the iterations for which CHREC_A is equal to CHREC_B.
2960 OVERLAP_ITERATIONS_A and OVERLAP_ITERATIONS_B are initialized with
2961 two functions that describe the iterations that contain conflicting
2962 elements.
2964 Remark: For an integer k >= 0, the following equality is true:
2966 CHREC_A (OVERLAP_ITERATIONS_A (k)) == CHREC_B (OVERLAP_ITERATIONS_B (k)).
2969 static void
2970 analyze_overlapping_iterations (tree chrec_a,
2971 tree chrec_b,
2972 tree *overlap_iterations_a,
2973 tree *overlap_iterations_b,
2974 tree *last_conflicts)
2976 if (dump_file && (dump_flags & TDF_DETAILS))
2978 fprintf (dump_file, "(analyze_overlapping_iterations \n");
2979 fprintf (dump_file, " (chrec_a = ");
2980 print_generic_expr (dump_file, chrec_a, 0);
2981 fprintf (dump_file, ")\n chrec_b = ");
2982 print_generic_expr (dump_file, chrec_b, 0);
2983 fprintf (dump_file, ")\n");
2986 if (chrec_a == NULL_TREE
2987 || chrec_b == NULL_TREE
2988 || chrec_contains_undetermined (chrec_a)
2989 || chrec_contains_undetermined (chrec_b)
2990 || chrec_contains_symbols (chrec_a)
2991 || chrec_contains_symbols (chrec_b))
2993 *overlap_iterations_a = chrec_dont_know;
2994 *overlap_iterations_b = chrec_dont_know;
2997 else if (ziv_subscript_p (chrec_a, chrec_b))
2998 analyze_ziv_subscript (chrec_a, chrec_b,
2999 overlap_iterations_a, overlap_iterations_b,
3000 last_conflicts);
3002 else if (siv_subscript_p (chrec_a, chrec_b))
3003 analyze_siv_subscript (chrec_a, chrec_b,
3004 overlap_iterations_a, overlap_iterations_b,
3005 last_conflicts);
3007 else
3008 analyze_miv_subscript (chrec_a, chrec_b,
3009 overlap_iterations_a, overlap_iterations_b,
3010 last_conflicts);
3012 if (dump_file && (dump_flags & TDF_DETAILS))
3014 fprintf (dump_file, " (overlap_iterations_a = ");
3015 print_generic_expr (dump_file, *overlap_iterations_a, 0);
3016 fprintf (dump_file, ")\n (overlap_iterations_b = ");
3017 print_generic_expr (dump_file, *overlap_iterations_b, 0);
3018 fprintf (dump_file, ")\n");
3024 /* This section contains the affine functions dependences detector. */
3026 /* Computes the conflicting iterations, and initialize DDR. */
3028 static void
3029 subscript_dependence_tester (struct data_dependence_relation *ddr)
3031 unsigned int i;
3032 struct data_reference *dra = DDR_A (ddr);
3033 struct data_reference *drb = DDR_B (ddr);
3034 tree last_conflicts;
3036 if (dump_file && (dump_flags & TDF_DETAILS))
3037 fprintf (dump_file, "(subscript_dependence_tester \n");
3039 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3041 tree overlaps_a, overlaps_b;
3042 struct subscript *subscript = DDR_SUBSCRIPT (ddr, i);
3044 analyze_overlapping_iterations (DR_ACCESS_FN (dra, i),
3045 DR_ACCESS_FN (drb, i),
3046 &overlaps_a, &overlaps_b,
3047 &last_conflicts);
3049 if (chrec_contains_undetermined (overlaps_a)
3050 || chrec_contains_undetermined (overlaps_b))
3052 finalize_ddr_dependent (ddr, chrec_dont_know);
3053 break;
3056 else if (overlaps_a == chrec_known
3057 || overlaps_b == chrec_known)
3059 finalize_ddr_dependent (ddr, chrec_known);
3060 break;
3063 else
3065 SUB_CONFLICTS_IN_A (subscript) = overlaps_a;
3066 SUB_CONFLICTS_IN_B (subscript) = overlaps_b;
3067 SUB_LAST_CONFLICT (subscript) = last_conflicts;
3071 if (dump_file && (dump_flags & TDF_DETAILS))
3072 fprintf (dump_file, ")\n");
3075 /* Compute the classic per loop distance vector.
3077 DDR is the data dependence relation to build a vector from.
3078 NB_LOOPS is the total number of loops we are considering.
3079 FIRST_LOOP_DEPTH is the loop->depth of the first loop in the analyzed
3080 loop nest.
3081 Return FALSE when fail to represent the data dependence as a distance
3082 vector.
3083 Return TRUE otherwise. */
3085 static bool
3086 build_classic_dist_vector (struct data_dependence_relation *ddr,
3087 int nb_loops, int first_loop_depth)
3089 unsigned i;
3090 lambda_vector dist_v, init_v;
3092 dist_v = lambda_vector_new (nb_loops);
3093 init_v = lambda_vector_new (nb_loops);
3094 lambda_vector_clear (dist_v, nb_loops);
3095 lambda_vector_clear (init_v, nb_loops);
3097 if (DDR_ARE_DEPENDENT (ddr) != NULL_TREE)
3098 return true;
3100 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3102 tree access_fn_a, access_fn_b;
3103 struct subscript *subscript = DDR_SUBSCRIPT (ddr, i);
3105 if (chrec_contains_undetermined (SUB_DISTANCE (subscript)))
3107 non_affine_dependence_relation (ddr);
3108 return true;
3111 access_fn_a = DR_ACCESS_FN (DDR_A (ddr), i);
3112 access_fn_b = DR_ACCESS_FN (DDR_B (ddr), i);
3114 if (TREE_CODE (access_fn_a) == POLYNOMIAL_CHREC
3115 && TREE_CODE (access_fn_b) == POLYNOMIAL_CHREC)
3117 int dist, loop_nb, loop_depth;
3118 int loop_nb_a = CHREC_VARIABLE (access_fn_a);
3119 int loop_nb_b = CHREC_VARIABLE (access_fn_b);
3120 struct loop *loop_a = current_loops->parray[loop_nb_a];
3121 struct loop *loop_b = current_loops->parray[loop_nb_b];
3123 /* If the loop for either variable is at a lower depth than
3124 the first_loop's depth, then we can't possibly have a
3125 dependency at this level of the loop. */
3127 if (loop_a->depth < first_loop_depth
3128 || loop_b->depth < first_loop_depth)
3129 return false;
3131 if (loop_nb_a != loop_nb_b
3132 && !flow_loop_nested_p (loop_a, loop_b)
3133 && !flow_loop_nested_p (loop_b, loop_a))
3135 /* Example: when there are two consecutive loops,
3137 | loop_1
3138 | A[{0, +, 1}_1]
3139 | endloop_1
3140 | loop_2
3141 | A[{0, +, 1}_2]
3142 | endloop_2
3144 the dependence relation cannot be captured by the
3145 distance abstraction. */
3146 non_affine_dependence_relation (ddr);
3147 return true;
3150 /* The dependence is carried by the outermost loop. Example:
3151 | loop_1
3152 | A[{4, +, 1}_1]
3153 | loop_2
3154 | A[{5, +, 1}_2]
3155 | endloop_2
3156 | endloop_1
3157 In this case, the dependence is carried by loop_1. */
3158 loop_nb = loop_nb_a < loop_nb_b ? loop_nb_a : loop_nb_b;
3159 loop_depth = current_loops->parray[loop_nb]->depth - first_loop_depth;
3161 /* If the loop number is still greater than the number of
3162 loops we've been asked to analyze, or negative,
3163 something is borked. */
3164 gcc_assert (loop_depth >= 0);
3165 gcc_assert (loop_depth < nb_loops);
3166 if (chrec_contains_undetermined (SUB_DISTANCE (subscript)))
3168 non_affine_dependence_relation (ddr);
3169 return true;
3172 dist = int_cst_value (SUB_DISTANCE (subscript));
3174 /* This is the subscript coupling test.
3175 | loop i = 0, N, 1
3176 | T[i+1][i] = ...
3177 | ... = T[i][i]
3178 | endloop
3179 There is no dependence. */
3180 if (init_v[loop_depth] != 0
3181 && dist_v[loop_depth] != dist)
3183 finalize_ddr_dependent (ddr, chrec_known);
3184 return true;
3187 dist_v[loop_depth] = dist;
3188 init_v[loop_depth] = 1;
3192 /* There is a distance of 1 on all the outer loops:
3194 Example: there is a dependence of distance 1 on loop_1 for the array A.
3195 | loop_1
3196 | A[5] = ...
3197 | endloop
3200 struct loop *lca, *loop_a, *loop_b;
3201 struct data_reference *a = DDR_A (ddr);
3202 struct data_reference *b = DDR_B (ddr);
3203 int lca_depth;
3204 loop_a = loop_containing_stmt (DR_STMT (a));
3205 loop_b = loop_containing_stmt (DR_STMT (b));
3207 /* Get the common ancestor loop. */
3208 lca = find_common_loop (loop_a, loop_b);
3210 lca_depth = lca->depth;
3211 lca_depth -= first_loop_depth;
3212 gcc_assert (lca_depth >= 0);
3213 gcc_assert (lca_depth < nb_loops);
3215 /* For each outer loop where init_v is not set, the accesses are
3216 in dependence of distance 1 in the loop. */
3217 if (lca != loop_a
3218 && lca != loop_b
3219 && init_v[lca_depth] == 0)
3220 dist_v[lca_depth] = 1;
3222 lca = lca->outer;
3224 if (lca)
3226 lca_depth = lca->depth - first_loop_depth;
3227 while (lca->depth != 0)
3229 /* If we're considering just a sub-nest, then don't record
3230 any information on the outer loops. */
3231 if (lca_depth < 0)
3232 break;
3234 gcc_assert (lca_depth < nb_loops);
3236 if (init_v[lca_depth] == 0)
3237 dist_v[lca_depth] = 1;
3238 lca = lca->outer;
3239 lca_depth = lca->depth - first_loop_depth;
3245 DDR_DIST_VECT (ddr) = dist_v;
3246 DDR_SIZE_VECT (ddr) = nb_loops;
3248 /* Verify a basic constraint: classic distance vectors should always
3249 be lexicographically positive. */
3250 if (!lambda_vector_lexico_pos (DDR_DIST_VECT (ddr),
3251 DDR_SIZE_VECT (ddr)))
3253 if (DDR_SIZE_VECT (ddr) == 1)
3254 /* This one is simple to fix, and can be fixed.
3255 Multidimensional arrays cannot be fixed that simply. */
3256 lambda_vector_negate (DDR_DIST_VECT (ddr), DDR_DIST_VECT (ddr),
3257 DDR_SIZE_VECT (ddr));
3258 else
3259 /* This is not valid: we need the delta test for properly
3260 fixing all this. */
3261 return false;
3264 return true;
3267 /* Compute the classic per loop direction vector.
3269 DDR is the data dependence relation to build a vector from.
3270 NB_LOOPS is the total number of loops we are considering.
3271 FIRST_LOOP_DEPTH is the loop->depth of the first loop in the analyzed
3272 loop nest.
3273 Return FALSE if the dependence relation is outside of the loop nest
3274 at FIRST_LOOP_DEPTH.
3275 Return TRUE otherwise. */
3277 static bool
3278 build_classic_dir_vector (struct data_dependence_relation *ddr,
3279 int nb_loops, int first_loop_depth)
3281 unsigned i;
3282 lambda_vector dir_v, init_v;
3284 dir_v = lambda_vector_new (nb_loops);
3285 init_v = lambda_vector_new (nb_loops);
3286 lambda_vector_clear (dir_v, nb_loops);
3287 lambda_vector_clear (init_v, nb_loops);
3289 if (DDR_ARE_DEPENDENT (ddr) != NULL_TREE)
3290 return true;
3292 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3294 tree access_fn_a, access_fn_b;
3295 struct subscript *subscript = DDR_SUBSCRIPT (ddr, i);
3297 if (chrec_contains_undetermined (SUB_DISTANCE (subscript)))
3299 non_affine_dependence_relation (ddr);
3300 return true;
3303 access_fn_a = DR_ACCESS_FN (DDR_A (ddr), i);
3304 access_fn_b = DR_ACCESS_FN (DDR_B (ddr), i);
3305 if (TREE_CODE (access_fn_a) == POLYNOMIAL_CHREC
3306 && TREE_CODE (access_fn_b) == POLYNOMIAL_CHREC)
3308 int dist, loop_nb, loop_depth;
3309 enum data_dependence_direction dir = dir_star;
3310 int loop_nb_a = CHREC_VARIABLE (access_fn_a);
3311 int loop_nb_b = CHREC_VARIABLE (access_fn_b);
3312 struct loop *loop_a = current_loops->parray[loop_nb_a];
3313 struct loop *loop_b = current_loops->parray[loop_nb_b];
3315 /* If the loop for either variable is at a lower depth than
3316 the first_loop's depth, then we can't possibly have a
3317 dependency at this level of the loop. */
3319 if (loop_a->depth < first_loop_depth
3320 || loop_b->depth < first_loop_depth)
3321 return false;
3323 if (loop_nb_a != loop_nb_b
3324 && !flow_loop_nested_p (loop_a, loop_b)
3325 && !flow_loop_nested_p (loop_b, loop_a))
3327 /* Example: when there are two consecutive loops,
3329 | loop_1
3330 | A[{0, +, 1}_1]
3331 | endloop_1
3332 | loop_2
3333 | A[{0, +, 1}_2]
3334 | endloop_2
3336 the dependence relation cannot be captured by the
3337 distance abstraction. */
3338 non_affine_dependence_relation (ddr);
3339 return true;
3342 /* The dependence is carried by the outermost loop. Example:
3343 | loop_1
3344 | A[{4, +, 1}_1]
3345 | loop_2
3346 | A[{5, +, 1}_2]
3347 | endloop_2
3348 | endloop_1
3349 In this case, the dependence is carried by loop_1. */
3350 loop_nb = loop_nb_a < loop_nb_b ? loop_nb_a : loop_nb_b;
3351 loop_depth = current_loops->parray[loop_nb]->depth - first_loop_depth;
3353 /* If the loop number is still greater than the number of
3354 loops we've been asked to analyze, or negative,
3355 something is borked. */
3356 gcc_assert (loop_depth >= 0);
3357 gcc_assert (loop_depth < nb_loops);
3359 if (chrec_contains_undetermined (SUB_DISTANCE (subscript)))
3361 non_affine_dependence_relation (ddr);
3362 return true;
3365 dist = int_cst_value (SUB_DISTANCE (subscript));
3367 if (dist == 0)
3368 dir = dir_equal;
3369 else if (dist > 0)
3370 dir = dir_positive;
3371 else if (dist < 0)
3372 dir = dir_negative;
3374 /* This is the subscript coupling test.
3375 | loop i = 0, N, 1
3376 | T[i+1][i] = ...
3377 | ... = T[i][i]
3378 | endloop
3379 There is no dependence. */
3380 if (init_v[loop_depth] != 0
3381 && dir != dir_star
3382 && (enum data_dependence_direction) dir_v[loop_depth] != dir
3383 && (enum data_dependence_direction) dir_v[loop_depth] != dir_star)
3385 finalize_ddr_dependent (ddr, chrec_known);
3386 return true;
3389 dir_v[loop_depth] = dir;
3390 init_v[loop_depth] = 1;
3394 /* There is a distance of 1 on all the outer loops:
3396 Example: there is a dependence of distance 1 on loop_1 for the array A.
3397 | loop_1
3398 | A[5] = ...
3399 | endloop
3402 struct loop *lca, *loop_a, *loop_b;
3403 struct data_reference *a = DDR_A (ddr);
3404 struct data_reference *b = DDR_B (ddr);
3405 int lca_depth;
3406 loop_a = loop_containing_stmt (DR_STMT (a));
3407 loop_b = loop_containing_stmt (DR_STMT (b));
3409 /* Get the common ancestor loop. */
3410 lca = find_common_loop (loop_a, loop_b);
3411 lca_depth = lca->depth - first_loop_depth;
3413 gcc_assert (lca_depth >= 0);
3414 gcc_assert (lca_depth < nb_loops);
3416 /* For each outer loop where init_v is not set, the accesses are
3417 in dependence of distance 1 in the loop. */
3418 if (lca != loop_a
3419 && lca != loop_b
3420 && init_v[lca_depth] == 0)
3421 dir_v[lca_depth] = dir_positive;
3423 lca = lca->outer;
3424 if (lca)
3426 lca_depth = lca->depth - first_loop_depth;
3427 while (lca->depth != 0)
3429 /* If we're considering just a sub-nest, then don't record
3430 any information on the outer loops. */
3431 if (lca_depth < 0)
3432 break;
3434 gcc_assert (lca_depth < nb_loops);
3436 if (init_v[lca_depth] == 0)
3437 dir_v[lca_depth] = dir_positive;
3438 lca = lca->outer;
3439 lca_depth = lca->depth - first_loop_depth;
3445 DDR_DIR_VECT (ddr) = dir_v;
3446 DDR_SIZE_VECT (ddr) = nb_loops;
3447 return true;
3450 /* Returns true when all the access functions of A are affine or
3451 constant. */
3453 static bool
3454 access_functions_are_affine_or_constant_p (struct data_reference *a)
3456 unsigned int i;
3457 VEC(tree,heap) **fns = DR_ACCESS_FNS_ADDR (a);
3458 tree t;
3460 for (i = 0; VEC_iterate (tree, *fns, i, t); i++)
3461 if (!evolution_function_is_constant_p (t)
3462 && !evolution_function_is_affine_multivariate_p (t))
3463 return false;
3465 return true;
3468 /* This computes the affine dependence relation between A and B.
3469 CHREC_KNOWN is used for representing the independence between two
3470 accesses, while CHREC_DONT_KNOW is used for representing the unknown
3471 relation.
3473 Note that it is possible to stop the computation of the dependence
3474 relation the first time we detect a CHREC_KNOWN element for a given
3475 subscript. */
3477 void
3478 compute_affine_dependence (struct data_dependence_relation *ddr)
3480 struct data_reference *dra = DDR_A (ddr);
3481 struct data_reference *drb = DDR_B (ddr);
3483 if (dump_file && (dump_flags & TDF_DETAILS))
3485 fprintf (dump_file, "(compute_affine_dependence\n");
3486 fprintf (dump_file, " (stmt_a = \n");
3487 print_generic_expr (dump_file, DR_STMT (dra), 0);
3488 fprintf (dump_file, ")\n (stmt_b = \n");
3489 print_generic_expr (dump_file, DR_STMT (drb), 0);
3490 fprintf (dump_file, ")\n");
3493 /* Analyze only when the dependence relation is not yet known. */
3494 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
3496 if (access_functions_are_affine_or_constant_p (dra)
3497 && access_functions_are_affine_or_constant_p (drb))
3498 subscript_dependence_tester (ddr);
3500 /* As a last case, if the dependence cannot be determined, or if
3501 the dependence is considered too difficult to determine, answer
3502 "don't know". */
3503 else
3504 finalize_ddr_dependent (ddr, chrec_dont_know);
3507 if (dump_file && (dump_flags & TDF_DETAILS))
3508 fprintf (dump_file, ")\n");
3511 /* This computes the dependence relation for the same data
3512 reference into DDR. */
3514 static void
3515 compute_self_dependence (struct data_dependence_relation *ddr)
3517 unsigned int i;
3519 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3521 struct subscript *subscript = DDR_SUBSCRIPT (ddr, i);
3523 /* The accessed index overlaps for each iteration. */
3524 SUB_CONFLICTS_IN_A (subscript) = integer_zero_node;
3525 SUB_CONFLICTS_IN_B (subscript) = integer_zero_node;
3526 SUB_LAST_CONFLICT (subscript) = chrec_dont_know;
3531 typedef struct data_dependence_relation *ddr_p;
3532 DEF_VEC_P(ddr_p);
3533 DEF_VEC_ALLOC_P(ddr_p,heap);
3535 /* Compute a subset of the data dependence relation graph. Don't
3536 compute read-read and self relations if
3537 COMPUTE_SELF_AND_READ_READ_DEPENDENCES is FALSE, and avoid the computation
3538 of the opposite relation, i.e. when AB has been computed, don't compute BA.
3539 DATAREFS contains a list of data references, and the result is set
3540 in DEPENDENCE_RELATIONS. */
3542 static void
3543 compute_all_dependences (varray_type datarefs,
3544 bool compute_self_and_read_read_dependences,
3545 VEC(ddr_p,heap) **dependence_relations)
3547 unsigned int i, j, N;
3549 N = VARRAY_ACTIVE_SIZE (datarefs);
3551 /* Note that we specifically skip i == j because it's a self dependence, and
3552 use compute_self_dependence below. */
3554 for (i = 0; i < N; i++)
3555 for (j = i + 1; j < N; j++)
3557 struct data_reference *a, *b;
3558 struct data_dependence_relation *ddr;
3560 a = VARRAY_GENERIC_PTR (datarefs, i);
3561 b = VARRAY_GENERIC_PTR (datarefs, j);
3562 if (DR_IS_READ (a) && DR_IS_READ (b)
3563 && !compute_self_and_read_read_dependences)
3564 continue;
3565 ddr = initialize_data_dependence_relation (a, b);
3567 VEC_safe_push (ddr_p, heap, *dependence_relations, ddr);
3568 compute_affine_dependence (ddr);
3569 compute_subscript_distance (ddr);
3571 if (!compute_self_and_read_read_dependences)
3572 return;
3574 /* Compute self dependence relation of each dataref to itself. */
3576 for (i = 0; i < N; i++)
3578 struct data_reference *a, *b;
3579 struct data_dependence_relation *ddr;
3581 a = VARRAY_GENERIC_PTR (datarefs, i);
3582 b = VARRAY_GENERIC_PTR (datarefs, i);
3583 ddr = initialize_data_dependence_relation (a, b);
3585 VEC_safe_push (ddr_p, heap, *dependence_relations, ddr);
3586 compute_self_dependence (ddr);
3587 compute_subscript_distance (ddr);
3591 /* Search the data references in LOOP, and record the information into
3592 DATAREFS. Returns chrec_dont_know when failing to analyze a
3593 difficult case, returns NULL_TREE otherwise.
3595 TODO: This function should be made smarter so that it can handle address
3596 arithmetic as if they were array accesses, etc. */
3598 tree
3599 find_data_references_in_loop (struct loop *loop, varray_type *datarefs)
3601 basic_block bb, *bbs;
3602 unsigned int i;
3603 block_stmt_iterator bsi;
3604 struct data_reference *dr;
3606 bbs = get_loop_body (loop);
3608 for (i = 0; i < loop->num_nodes; i++)
3610 bb = bbs[i];
3612 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
3614 tree stmt = bsi_stmt (bsi);
3616 /* ASM_EXPR and CALL_EXPR may embed arbitrary side effects.
3617 Calls have side-effects, except those to const or pure
3618 functions. */
3619 if ((TREE_CODE (stmt) == CALL_EXPR
3620 && !(call_expr_flags (stmt) & (ECF_CONST | ECF_PURE)))
3621 || (TREE_CODE (stmt) == ASM_EXPR
3622 && ASM_VOLATILE_P (stmt)))
3623 goto insert_dont_know_node;
3625 if (ZERO_SSA_OPERANDS (stmt, SSA_OP_ALL_VIRTUALS))
3626 continue;
3628 switch (TREE_CODE (stmt))
3630 case MODIFY_EXPR:
3632 bool one_inserted = false;
3633 tree opnd0 = TREE_OPERAND (stmt, 0);
3634 tree opnd1 = TREE_OPERAND (stmt, 1);
3636 if (TREE_CODE (opnd0) == ARRAY_REF
3637 || TREE_CODE (opnd0) == INDIRECT_REF)
3639 dr = create_data_ref (opnd0, stmt, false);
3640 if (dr)
3642 VARRAY_PUSH_GENERIC_PTR (*datarefs, dr);
3643 one_inserted = true;
3647 if (TREE_CODE (opnd1) == ARRAY_REF
3648 || TREE_CODE (opnd1) == INDIRECT_REF)
3650 dr = create_data_ref (opnd1, stmt, true);
3651 if (dr)
3653 VARRAY_PUSH_GENERIC_PTR (*datarefs, dr);
3654 one_inserted = true;
3658 if (!one_inserted)
3659 goto insert_dont_know_node;
3661 break;
3664 case CALL_EXPR:
3666 tree args;
3667 bool one_inserted = false;
3669 for (args = TREE_OPERAND (stmt, 1); args;
3670 args = TREE_CHAIN (args))
3671 if (TREE_CODE (TREE_VALUE (args)) == ARRAY_REF
3672 || TREE_CODE (TREE_VALUE (args)) == INDIRECT_REF)
3674 dr = create_data_ref (TREE_VALUE (args), stmt, true);
3675 if (dr)
3677 VARRAY_PUSH_GENERIC_PTR (*datarefs, dr);
3678 one_inserted = true;
3682 if (!one_inserted)
3683 goto insert_dont_know_node;
3685 break;
3688 default:
3690 struct data_reference *res;
3692 insert_dont_know_node:;
3693 res = xmalloc (sizeof (struct data_reference));
3694 DR_STMT (res) = NULL_TREE;
3695 DR_REF (res) = NULL_TREE;
3696 DR_BASE_OBJECT (res) = NULL;
3697 DR_TYPE (res) = ARRAY_REF_TYPE;
3698 DR_SET_ACCESS_FNS (res, NULL);
3699 DR_BASE_OBJECT (res) = NULL;
3700 DR_IS_READ (res) = false;
3701 DR_BASE_ADDRESS (res) = NULL_TREE;
3702 DR_OFFSET (res) = NULL_TREE;
3703 DR_INIT (res) = NULL_TREE;
3704 DR_STEP (res) = NULL_TREE;
3705 DR_OFFSET_MISALIGNMENT (res) = NULL_TREE;
3706 DR_MEMTAG (res) = NULL_TREE;
3707 DR_PTR_INFO (res) = NULL;
3708 VARRAY_PUSH_GENERIC_PTR (*datarefs, res);
3710 free (bbs);
3711 return chrec_dont_know;
3715 /* When there are no defs in the loop, the loop is parallel. */
3716 if (!ZERO_SSA_OPERANDS (stmt, SSA_OP_VIRTUAL_DEFS))
3717 loop->parallel_p = false;
3721 free (bbs);
3723 return NULL_TREE;
3728 /* This section contains all the entry points. */
3730 /* Given a loop nest LOOP, the following vectors are returned:
3731 *DATAREFS is initialized to all the array elements contained in this loop,
3732 *DEPENDENCE_RELATIONS contains the relations between the data references.
3733 Compute read-read and self relations if
3734 COMPUTE_SELF_AND_READ_READ_DEPENDENCES is TRUE. */
3736 void
3737 compute_data_dependences_for_loop (struct loop *loop,
3738 bool compute_self_and_read_read_dependences,
3739 varray_type *datarefs,
3740 varray_type *dependence_relations)
3742 unsigned int i, nb_loops;
3743 VEC(ddr_p,heap) *allrelations;
3744 struct data_dependence_relation *ddr;
3745 struct loop *loop_nest = loop;
3747 while (loop_nest && loop_nest->outer && loop_nest->outer->outer)
3748 loop_nest = loop_nest->outer;
3750 nb_loops = loop_nest->level;
3752 /* If one of the data references is not computable, give up without
3753 spending time to compute other dependences. */
3754 if (find_data_references_in_loop (loop, datarefs) == chrec_dont_know)
3756 struct data_dependence_relation *ddr;
3758 /* Insert a single relation into dependence_relations:
3759 chrec_dont_know. */
3760 ddr = initialize_data_dependence_relation (NULL, NULL);
3761 VARRAY_PUSH_GENERIC_PTR (*dependence_relations, ddr);
3762 build_classic_dist_vector (ddr, nb_loops, loop->depth);
3763 build_classic_dir_vector (ddr, nb_loops, loop->depth);
3764 return;
3767 allrelations = NULL;
3768 compute_all_dependences (*datarefs, compute_self_and_read_read_dependences,
3769 &allrelations);
3771 for (i = 0; VEC_iterate (ddr_p, allrelations, i, ddr); i++)
3773 if (build_classic_dist_vector (ddr, nb_loops, loop_nest->depth))
3775 VARRAY_PUSH_GENERIC_PTR (*dependence_relations, ddr);
3776 build_classic_dir_vector (ddr, nb_loops, loop_nest->depth);
3781 /* Entry point (for testing only). Analyze all the data references
3782 and the dependence relations.
3784 The data references are computed first.
3786 A relation on these nodes is represented by a complete graph. Some
3787 of the relations could be of no interest, thus the relations can be
3788 computed on demand.
3790 In the following function we compute all the relations. This is
3791 just a first implementation that is here for:
3792 - for showing how to ask for the dependence relations,
3793 - for the debugging the whole dependence graph,
3794 - for the dejagnu testcases and maintenance.
3796 It is possible to ask only for a part of the graph, avoiding to
3797 compute the whole dependence graph. The computed dependences are
3798 stored in a knowledge base (KB) such that later queries don't
3799 recompute the same information. The implementation of this KB is
3800 transparent to the optimizer, and thus the KB can be changed with a
3801 more efficient implementation, or the KB could be disabled. */
3803 void
3804 analyze_all_data_dependences (struct loops *loops)
3806 unsigned int i;
3807 varray_type datarefs;
3808 varray_type dependence_relations;
3809 int nb_data_refs = 10;
3811 VARRAY_GENERIC_PTR_INIT (datarefs, nb_data_refs, "datarefs");
3812 VARRAY_GENERIC_PTR_INIT (dependence_relations,
3813 nb_data_refs * nb_data_refs,
3814 "dependence_relations");
3816 /* Compute DDs on the whole function. */
3817 compute_data_dependences_for_loop (loops->parray[0], false,
3818 &datarefs, &dependence_relations);
3820 if (dump_file)
3822 dump_data_dependence_relations (dump_file, dependence_relations);
3823 fprintf (dump_file, "\n\n");
3825 if (dump_flags & TDF_DETAILS)
3826 dump_dist_dir_vectors (dump_file, dependence_relations);
3828 if (dump_flags & TDF_STATS)
3830 unsigned nb_top_relations = 0;
3831 unsigned nb_bot_relations = 0;
3832 unsigned nb_basename_differ = 0;
3833 unsigned nb_chrec_relations = 0;
3835 for (i = 0; i < VARRAY_ACTIVE_SIZE (dependence_relations); i++)
3837 struct data_dependence_relation *ddr;
3838 ddr = VARRAY_GENERIC_PTR (dependence_relations, i);
3840 if (chrec_contains_undetermined (DDR_ARE_DEPENDENT (ddr)))
3841 nb_top_relations++;
3843 else if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
3845 struct data_reference *a = DDR_A (ddr);
3846 struct data_reference *b = DDR_B (ddr);
3847 bool differ_p;
3849 if ((DR_BASE_OBJECT (a) && DR_BASE_OBJECT (b)
3850 && DR_NUM_DIMENSIONS (a) != DR_NUM_DIMENSIONS (b))
3851 || (base_object_differ_p (a, b, &differ_p)
3852 && differ_p))
3853 nb_basename_differ++;
3854 else
3855 nb_bot_relations++;
3858 else
3859 nb_chrec_relations++;
3862 gather_stats_on_scev_database ();
3866 free_dependence_relations (dependence_relations);
3867 free_data_refs (datarefs);
3870 /* Free the memory used by a data dependence relation DDR. */
3872 void
3873 free_dependence_relation (struct data_dependence_relation *ddr)
3875 if (ddr == NULL)
3876 return;
3878 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE && DDR_SUBSCRIPTS (ddr))
3879 varray_clear (DDR_SUBSCRIPTS (ddr));
3880 free (ddr);
3883 /* Free the memory used by the data dependence relations from
3884 DEPENDENCE_RELATIONS. */
3886 void
3887 free_dependence_relations (varray_type dependence_relations)
3889 unsigned int i;
3890 if (dependence_relations == NULL)
3891 return;
3893 for (i = 0; i < VARRAY_ACTIVE_SIZE (dependence_relations); i++)
3894 free_dependence_relation (VARRAY_GENERIC_PTR (dependence_relations, i));
3895 varray_clear (dependence_relations);
3898 /* Free the memory used by the data references from DATAREFS. */
3900 void
3901 free_data_refs (varray_type datarefs)
3903 unsigned int i;
3905 if (datarefs == NULL)
3906 return;
3908 for (i = 0; i < VARRAY_ACTIVE_SIZE (datarefs); i++)
3910 struct data_reference *dr = (struct data_reference *)
3911 VARRAY_GENERIC_PTR (datarefs, i);
3912 if (dr)
3914 DR_FREE_ACCESS_FNS (dr);
3915 free (dr);
3918 varray_clear (datarefs);