Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / gcc / tree-data-ref.c
blob7987f66390f12110299bdf73e7020f9c2441006a
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, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, 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 "errors.h"
82 #include "ggc.h"
83 #include "tree.h"
85 /* These RTL headers are needed for basic-block.h. */
86 #include "rtl.h"
87 #include "basic-block.h"
88 #include "diagnostic.h"
89 #include "tree-flow.h"
90 #include "tree-dump.h"
91 #include "timevar.h"
92 #include "cfgloop.h"
93 #include "tree-chrec.h"
94 #include "tree-data-ref.h"
95 #include "tree-scalar-evolution.h"
96 #include "tree-pass.h"
98 /* This is the simplest data dependence test: determines whether the
99 data references A and B access the same array/region. Returns
100 false when the property is not computable at compile time.
101 Otherwise return true, and DIFFER_P will record the result. This
102 utility will not be necessary when alias_sets_conflict_p will be
103 less conservative. */
105 bool
106 array_base_name_differ_p (struct data_reference *a,
107 struct data_reference *b,
108 bool *differ_p)
110 tree base_a = DR_BASE_NAME (a);
111 tree base_b = DR_BASE_NAME (b);
112 tree ta, tb;
114 if (!base_a || !base_b)
115 return false;
117 ta = TREE_TYPE (base_a);
118 tb = TREE_TYPE (base_b);
120 /* Determine if same base. Example: for the array accesses
121 a[i], b[i] or pointer accesses *a, *b, bases are a, b. */
122 if (base_a == base_b)
124 *differ_p = false;
125 return true;
128 /* For pointer based accesses, (*p)[i], (*q)[j], the bases are (*p)
129 and (*q) */
130 if (TREE_CODE (base_a) == INDIRECT_REF && TREE_CODE (base_b) == INDIRECT_REF
131 && TREE_OPERAND (base_a, 0) == TREE_OPERAND (base_b, 0))
133 *differ_p = false;
134 return true;
137 /* Record/union based accesses - s.a[i], t.b[j]. bases are s.a,t.b. */
138 if (TREE_CODE (base_a) == COMPONENT_REF && TREE_CODE (base_b) == COMPONENT_REF
139 && TREE_OPERAND (base_a, 0) == TREE_OPERAND (base_b, 0)
140 && TREE_OPERAND (base_a, 1) == TREE_OPERAND (base_b, 1))
142 *differ_p = false;
143 return true;
147 /* Determine if different bases. */
149 /* At this point we know that base_a != base_b. However, pointer
150 accesses of the form x=(*p) and y=(*q), whose bases are p and q,
151 may still be pointing to the same base. In SSAed GIMPLE p and q will
152 be SSA_NAMES in this case. Therefore, here we check if they are
153 really two different declarations. */
154 if (TREE_CODE (base_a) == VAR_DECL && TREE_CODE (base_b) == VAR_DECL)
156 *differ_p = true;
157 return true;
160 /* Compare two record/union bases s.a and t.b: s != t or (a != b and
161 s and t are not unions). */
162 if (TREE_CODE (base_a) == COMPONENT_REF && TREE_CODE (base_b) == COMPONENT_REF
163 && ((TREE_CODE (TREE_OPERAND (base_a, 0)) == VAR_DECL
164 && TREE_CODE (TREE_OPERAND (base_b, 0)) == VAR_DECL
165 && TREE_OPERAND (base_a, 0) != TREE_OPERAND (base_b, 0))
166 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (base_a, 0))) == RECORD_TYPE
167 && TREE_CODE (TREE_TYPE (TREE_OPERAND (base_b, 0))) == RECORD_TYPE
168 && TREE_OPERAND (base_a, 1) != TREE_OPERAND (base_b, 1))))
170 *differ_p = true;
171 return true;
174 /* Compare a record/union access and an array access. */
175 if ((TREE_CODE (base_a) == VAR_DECL
176 && (TREE_CODE (base_b) == COMPONENT_REF
177 && TREE_CODE (TREE_OPERAND (base_b, 0)) == VAR_DECL))
178 || (TREE_CODE (base_b) == VAR_DECL
179 && (TREE_CODE (base_a) == COMPONENT_REF
180 && TREE_CODE (TREE_OPERAND (base_a, 0)) == VAR_DECL)))
182 *differ_p = true;
183 return true;
186 return false;
189 /* Returns true iff A divides B. */
191 static inline bool
192 tree_fold_divides_p (tree type,
193 tree a,
194 tree b)
196 /* Determines whether (A == gcd (A, B)). */
197 return integer_zerop
198 (fold (build (MINUS_EXPR, type, a, tree_fold_gcd (a, b))));
201 /* Compute the greatest common denominator of two numbers using
202 Euclid's algorithm. */
204 static int
205 gcd (int a, int b)
208 int x, y, z;
210 x = abs (a);
211 y = abs (b);
213 while (x>0)
215 z = y % x;
216 y = x;
217 x = z;
220 return (y);
223 /* Returns true iff A divides B. */
225 static inline bool
226 int_divides_p (int a, int b)
228 return ((b % a) == 0);
233 /* Dump into FILE all the data references from DATAREFS. */
235 void
236 dump_data_references (FILE *file,
237 varray_type datarefs)
239 unsigned int i;
241 for (i = 0; i < VARRAY_ACTIVE_SIZE (datarefs); i++)
242 dump_data_reference (file, VARRAY_GENERIC_PTR (datarefs, i));
245 /* Dump into FILE all the dependence relations from DDR. */
247 void
248 dump_data_dependence_relations (FILE *file,
249 varray_type ddr)
251 unsigned int i;
253 for (i = 0; i < VARRAY_ACTIVE_SIZE (ddr); i++)
254 dump_data_dependence_relation (file, VARRAY_GENERIC_PTR (ddr, i));
257 /* Dump function for a DATA_REFERENCE structure. */
259 void
260 dump_data_reference (FILE *outf,
261 struct data_reference *dr)
263 unsigned int i;
265 fprintf (outf, "(Data Ref: \n stmt: ");
266 print_generic_stmt (outf, DR_STMT (dr), 0);
267 fprintf (outf, " ref: ");
268 print_generic_stmt (outf, DR_REF (dr), 0);
269 fprintf (outf, " base_name: ");
270 print_generic_stmt (outf, DR_BASE_NAME (dr), 0);
272 for (i = 0; i < DR_NUM_DIMENSIONS (dr); i++)
274 fprintf (outf, " Access function %d: ", i);
275 print_generic_stmt (outf, DR_ACCESS_FN (dr, i), 0);
277 fprintf (outf, ")\n");
280 /* Dump function for a SUBSCRIPT structure. */
282 void
283 dump_subscript (FILE *outf, struct subscript *subscript)
285 tree chrec = SUB_CONFLICTS_IN_A (subscript);
287 fprintf (outf, "\n (subscript \n");
288 fprintf (outf, " iterations_that_access_an_element_twice_in_A: ");
289 print_generic_stmt (outf, chrec, 0);
290 if (chrec == chrec_known)
291 fprintf (outf, " (no dependence)\n");
292 else if (chrec_contains_undetermined (chrec))
293 fprintf (outf, " (don't know)\n");
294 else
296 tree last_iteration = SUB_LAST_CONFLICT (subscript);
297 fprintf (outf, " last_conflict: ");
298 print_generic_stmt (outf, last_iteration, 0);
301 chrec = SUB_CONFLICTS_IN_B (subscript);
302 fprintf (outf, " iterations_that_access_an_element_twice_in_B: ");
303 print_generic_stmt (outf, chrec, 0);
304 if (chrec == chrec_known)
305 fprintf (outf, " (no dependence)\n");
306 else if (chrec_contains_undetermined (chrec))
307 fprintf (outf, " (don't know)\n");
308 else
310 tree last_iteration = SUB_LAST_CONFLICT (subscript);
311 fprintf (outf, " last_conflict: ");
312 print_generic_stmt (outf, last_iteration, 0);
315 fprintf (outf, " (Subscript distance: ");
316 print_generic_stmt (outf, SUB_DISTANCE (subscript), 0);
317 fprintf (outf, " )\n");
318 fprintf (outf, " )\n");
321 /* Dump function for a DATA_DEPENDENCE_RELATION structure. */
323 void
324 dump_data_dependence_relation (FILE *outf,
325 struct data_dependence_relation *ddr)
327 struct data_reference *dra, *drb;
329 dra = DDR_A (ddr);
330 drb = DDR_B (ddr);
331 fprintf (outf, "(Data Dep: \n");
332 if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
333 fprintf (outf, " (don't know)\n");
335 else if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
336 fprintf (outf, " (no dependence)\n");
338 else if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
340 unsigned int i;
341 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
343 fprintf (outf, " access_fn_A: ");
344 print_generic_stmt (outf, DR_ACCESS_FN (dra, i), 0);
345 fprintf (outf, " access_fn_B: ");
346 print_generic_stmt (outf, DR_ACCESS_FN (drb, i), 0);
347 dump_subscript (outf, DDR_SUBSCRIPT (ddr, i));
349 if (DDR_DIST_VECT (ddr))
351 fprintf (outf, " distance_vect: ");
352 print_lambda_vector (outf, DDR_DIST_VECT (ddr), DDR_SIZE_VECT (ddr));
354 if (DDR_DIR_VECT (ddr))
356 fprintf (outf, " direction_vect: ");
357 print_lambda_vector (outf, DDR_DIR_VECT (ddr), DDR_SIZE_VECT (ddr));
361 fprintf (outf, ")\n");
366 /* Dump function for a DATA_DEPENDENCE_DIRECTION structure. */
368 void
369 dump_data_dependence_direction (FILE *file,
370 enum data_dependence_direction dir)
372 switch (dir)
374 case dir_positive:
375 fprintf (file, "+");
376 break;
378 case dir_negative:
379 fprintf (file, "-");
380 break;
382 case dir_equal:
383 fprintf (file, "=");
384 break;
386 case dir_positive_or_negative:
387 fprintf (file, "+-");
388 break;
390 case dir_positive_or_equal:
391 fprintf (file, "+=");
392 break;
394 case dir_negative_or_equal:
395 fprintf (file, "-=");
396 break;
398 case dir_star:
399 fprintf (file, "*");
400 break;
402 default:
403 break;
407 /* Dumps the distance and direction vectors in FILE. DDRS contains
408 the dependence relations, and VECT_SIZE is the size of the
409 dependence vectors, or in other words the number of loops in the
410 considered nest. */
412 void
413 dump_dist_dir_vectors (FILE *file, varray_type ddrs)
415 unsigned int i;
417 for (i = 0; i < VARRAY_ACTIVE_SIZE (ddrs); i++)
419 struct data_dependence_relation *ddr =
420 (struct data_dependence_relation *)
421 VARRAY_GENERIC_PTR (ddrs, i);
422 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE
423 && DDR_AFFINE_P (ddr))
425 fprintf (file, "DISTANCE_V (");
426 print_lambda_vector (file, DDR_DIST_VECT (ddr), DDR_SIZE_VECT (ddr));
427 fprintf (file, ")\n");
428 fprintf (file, "DIRECTION_V (");
429 print_lambda_vector (file, DDR_DIR_VECT (ddr), DDR_SIZE_VECT (ddr));
430 fprintf (file, ")\n");
433 fprintf (file, "\n\n");
436 /* Dumps the data dependence relations DDRS in FILE. */
438 void
439 dump_ddrs (FILE *file, varray_type ddrs)
441 unsigned int i;
443 for (i = 0; i < VARRAY_ACTIVE_SIZE (ddrs); i++)
445 struct data_dependence_relation *ddr =
446 (struct data_dependence_relation *)
447 VARRAY_GENERIC_PTR (ddrs, i);
448 dump_data_dependence_relation (file, ddr);
450 fprintf (file, "\n\n");
455 /* Compute the lowest iteration bound for LOOP. It is an
456 INTEGER_CST. */
458 static void
459 compute_estimated_nb_iterations (struct loop *loop)
461 tree estimation;
462 struct nb_iter_bound *bound, *next;
464 for (bound = loop->bounds; bound; bound = next)
466 next = bound->next;
467 estimation = bound->bound;
469 if (TREE_CODE (estimation) != INTEGER_CST)
470 continue;
472 if (loop->estimated_nb_iterations)
474 /* Update only if estimation is smaller. */
475 if (tree_int_cst_lt (estimation, loop->estimated_nb_iterations))
476 loop->estimated_nb_iterations = estimation;
478 else
479 loop->estimated_nb_iterations = estimation;
483 /* Estimate the number of iterations from the size of the data and the
484 access functions. */
486 static void
487 estimate_niter_from_size_of_data (struct loop *loop,
488 tree opnd0,
489 tree access_fn,
490 tree stmt)
492 tree estimation;
493 tree array_size, data_size, element_size;
494 tree init, step;
496 init = initial_condition (access_fn);
497 step = evolution_part_in_loop_num (access_fn, loop->num);
499 array_size = TYPE_SIZE (TREE_TYPE (opnd0));
500 element_size = TYPE_SIZE (TREE_TYPE (TREE_TYPE (opnd0)));
501 if (array_size == NULL_TREE
502 || TREE_CODE (array_size) != INTEGER_CST
503 || TREE_CODE (element_size) != INTEGER_CST)
504 return;
506 data_size = fold (build2 (EXACT_DIV_EXPR, integer_type_node,
507 array_size, element_size));
509 if (init != NULL_TREE
510 && step != NULL_TREE
511 && TREE_CODE (init) == INTEGER_CST
512 && TREE_CODE (step) == INTEGER_CST)
514 estimation = fold (build2 (CEIL_DIV_EXPR, integer_type_node,
515 fold (build2 (MINUS_EXPR, integer_type_node,
516 data_size, init)), step));
518 record_estimate (loop, estimation, boolean_true_node, stmt);
522 /* Given an ARRAY_REF node REF, records its access functions.
523 Example: given A[i][3], record in ACCESS_FNS the opnd1 function,
524 i.e. the constant "3", then recursively call the function on opnd0,
525 i.e. the ARRAY_REF "A[i]". The function returns the base name:
526 "A". */
528 static tree
529 analyze_array_indexes (struct loop *loop,
530 varray_type *access_fns,
531 tree ref, tree stmt)
533 tree opnd0, opnd1;
534 tree access_fn;
536 opnd0 = TREE_OPERAND (ref, 0);
537 opnd1 = TREE_OPERAND (ref, 1);
539 /* The detection of the evolution function for this data access is
540 postponed until the dependence test. This lazy strategy avoids
541 the computation of access functions that are of no interest for
542 the optimizers. */
543 access_fn = instantiate_parameters
544 (loop, analyze_scalar_evolution (loop, opnd1));
546 if (loop->estimated_nb_iterations == NULL_TREE)
547 estimate_niter_from_size_of_data (loop, opnd0, access_fn, stmt);
549 VARRAY_PUSH_TREE (*access_fns, access_fn);
551 /* Recursively record other array access functions. */
552 if (TREE_CODE (opnd0) == ARRAY_REF)
553 return analyze_array_indexes (loop, access_fns, opnd0, stmt);
555 /* Return the base name of the data access. */
556 else
557 return opnd0;
560 /* For a data reference REF contained in the statement STMT, initialize
561 a DATA_REFERENCE structure, and return it. IS_READ flag has to be
562 set to true when REF is in the right hand side of an
563 assignment. */
565 struct data_reference *
566 analyze_array (tree stmt, tree ref, bool is_read)
568 struct data_reference *res;
570 if (dump_file && (dump_flags & TDF_DETAILS))
572 fprintf (dump_file, "(analyze_array \n");
573 fprintf (dump_file, " (ref = ");
574 print_generic_stmt (dump_file, ref, 0);
575 fprintf (dump_file, ")\n");
578 res = xmalloc (sizeof (struct data_reference));
580 DR_STMT (res) = stmt;
581 DR_REF (res) = ref;
582 VARRAY_TREE_INIT (DR_ACCESS_FNS (res), 3, "access_fns");
583 DR_BASE_NAME (res) = analyze_array_indexes
584 (loop_containing_stmt (stmt), &(DR_ACCESS_FNS (res)), ref, stmt);
585 DR_IS_READ (res) = is_read;
587 if (dump_file && (dump_flags & TDF_DETAILS))
588 fprintf (dump_file, ")\n");
590 return res;
593 /* For a data reference REF contained in the statement STMT, initialize
594 a DATA_REFERENCE structure, and return it. */
596 struct data_reference *
597 init_data_ref (tree stmt,
598 tree ref,
599 tree base,
600 tree access_fn,
601 bool is_read)
603 struct data_reference *res;
605 if (dump_file && (dump_flags & TDF_DETAILS))
607 fprintf (dump_file, "(init_data_ref \n");
608 fprintf (dump_file, " (ref = ");
609 print_generic_stmt (dump_file, ref, 0);
610 fprintf (dump_file, ")\n");
613 res = xmalloc (sizeof (struct data_reference));
615 DR_STMT (res) = stmt;
616 DR_REF (res) = ref;
617 VARRAY_TREE_INIT (DR_ACCESS_FNS (res), 5, "access_fns");
618 DR_BASE_NAME (res) = base;
619 VARRAY_PUSH_TREE (DR_ACCESS_FNS (res), access_fn);
620 DR_IS_READ (res) = is_read;
622 if (dump_file && (dump_flags & TDF_DETAILS))
623 fprintf (dump_file, ")\n");
625 return res;
630 /* Returns true when all the functions of a tree_vec CHREC are the
631 same. */
633 static bool
634 all_chrecs_equal_p (tree chrec)
636 int j;
638 for (j = 0; j < TREE_VEC_LENGTH (chrec) - 1; j++)
640 tree chrec_j = TREE_VEC_ELT (chrec, j);
641 tree chrec_j_1 = TREE_VEC_ELT (chrec, j + 1);
642 if (!integer_zerop
643 (chrec_fold_minus
644 (integer_type_node, chrec_j, chrec_j_1)))
645 return false;
647 return true;
650 /* Determine for each subscript in the data dependence relation DDR
651 the distance. */
653 static void
654 compute_subscript_distance (struct data_dependence_relation *ddr)
656 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
658 unsigned int i;
660 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
662 tree conflicts_a, conflicts_b, difference;
663 struct subscript *subscript;
665 subscript = DDR_SUBSCRIPT (ddr, i);
666 conflicts_a = SUB_CONFLICTS_IN_A (subscript);
667 conflicts_b = SUB_CONFLICTS_IN_B (subscript);
669 if (TREE_CODE (conflicts_a) == TREE_VEC)
671 if (!all_chrecs_equal_p (conflicts_a))
673 SUB_DISTANCE (subscript) = chrec_dont_know;
674 return;
676 else
677 conflicts_a = TREE_VEC_ELT (conflicts_a, 0);
680 if (TREE_CODE (conflicts_b) == TREE_VEC)
682 if (!all_chrecs_equal_p (conflicts_b))
684 SUB_DISTANCE (subscript) = chrec_dont_know;
685 return;
687 else
688 conflicts_b = TREE_VEC_ELT (conflicts_b, 0);
691 difference = chrec_fold_minus
692 (integer_type_node, conflicts_b, conflicts_a);
694 if (evolution_function_is_constant_p (difference))
695 SUB_DISTANCE (subscript) = difference;
697 else
698 SUB_DISTANCE (subscript) = chrec_dont_know;
703 /* Initialize a ddr. */
705 struct data_dependence_relation *
706 initialize_data_dependence_relation (struct data_reference *a,
707 struct data_reference *b)
709 struct data_dependence_relation *res;
710 bool differ_p;
712 res = xmalloc (sizeof (struct data_dependence_relation));
713 DDR_A (res) = a;
714 DDR_B (res) = b;
716 if (a == NULL || b == NULL
717 || DR_BASE_NAME (a) == NULL_TREE
718 || DR_BASE_NAME (b) == NULL_TREE)
719 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
721 /* When the dimensions of A and B differ, we directly initialize
722 the relation to "there is no dependence": chrec_known. */
723 else if (DR_NUM_DIMENSIONS (a) != DR_NUM_DIMENSIONS (b)
724 || (array_base_name_differ_p (a, b, &differ_p) && differ_p))
725 DDR_ARE_DEPENDENT (res) = chrec_known;
727 else
729 unsigned int i;
730 DDR_AFFINE_P (res) = true;
731 DDR_ARE_DEPENDENT (res) = NULL_TREE;
732 DDR_SUBSCRIPTS_VECTOR_INIT (res, DR_NUM_DIMENSIONS (a));
733 DDR_SIZE_VECT (res) = 0;
734 DDR_DIST_VECT (res) = NULL;
735 DDR_DIR_VECT (res) = NULL;
737 for (i = 0; i < DR_NUM_DIMENSIONS (a); i++)
739 struct subscript *subscript;
741 subscript = xmalloc (sizeof (struct subscript));
742 SUB_CONFLICTS_IN_A (subscript) = chrec_dont_know;
743 SUB_CONFLICTS_IN_B (subscript) = chrec_dont_know;
744 SUB_LAST_CONFLICT (subscript) = chrec_dont_know;
745 SUB_DISTANCE (subscript) = chrec_dont_know;
746 VARRAY_PUSH_GENERIC_PTR (DDR_SUBSCRIPTS (res), subscript);
750 return res;
753 /* Set DDR_ARE_DEPENDENT to CHREC and finalize the subscript overlap
754 description. */
756 static inline void
757 finalize_ddr_dependent (struct data_dependence_relation *ddr,
758 tree chrec)
760 if (dump_file && (dump_flags & TDF_DETAILS))
762 fprintf (dump_file, "(dependence classified: ");
763 print_generic_expr (dump_file, chrec, 0);
764 fprintf (dump_file, ")\n");
767 DDR_ARE_DEPENDENT (ddr) = chrec;
768 varray_clear (DDR_SUBSCRIPTS (ddr));
771 /* The dependence relation DDR cannot be represented by a distance
772 vector. */
774 static inline void
775 non_affine_dependence_relation (struct data_dependence_relation *ddr)
777 if (dump_file && (dump_flags & TDF_DETAILS))
778 fprintf (dump_file, "(Dependence relation cannot be represented by distance vector.) \n");
780 DDR_AFFINE_P (ddr) = false;
785 /* This section contains the classic Banerjee tests. */
787 /* Returns true iff CHREC_A and CHREC_B are not dependent on any index
788 variables, i.e., if the ZIV (Zero Index Variable) test is true. */
790 static inline bool
791 ziv_subscript_p (tree chrec_a,
792 tree chrec_b)
794 return (evolution_function_is_constant_p (chrec_a)
795 && evolution_function_is_constant_p (chrec_b));
798 /* Returns true iff CHREC_A and CHREC_B are dependent on an index
799 variable, i.e., if the SIV (Single Index Variable) test is true. */
801 static bool
802 siv_subscript_p (tree chrec_a,
803 tree chrec_b)
805 if ((evolution_function_is_constant_p (chrec_a)
806 && evolution_function_is_univariate_p (chrec_b))
807 || (evolution_function_is_constant_p (chrec_b)
808 && evolution_function_is_univariate_p (chrec_a)))
809 return true;
811 if (evolution_function_is_univariate_p (chrec_a)
812 && evolution_function_is_univariate_p (chrec_b))
814 switch (TREE_CODE (chrec_a))
816 case POLYNOMIAL_CHREC:
817 switch (TREE_CODE (chrec_b))
819 case POLYNOMIAL_CHREC:
820 if (CHREC_VARIABLE (chrec_a) != CHREC_VARIABLE (chrec_b))
821 return false;
823 default:
824 return true;
827 default:
828 return true;
832 return false;
835 /* Analyze a ZIV (Zero Index Variable) subscript. *OVERLAPS_A and
836 *OVERLAPS_B are initialized to the functions that describe the
837 relation between the elements accessed twice by CHREC_A and
838 CHREC_B. For k >= 0, the following property is verified:
840 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
842 static void
843 analyze_ziv_subscript (tree chrec_a,
844 tree chrec_b,
845 tree *overlaps_a,
846 tree *overlaps_b,
847 tree *last_conflicts)
849 tree difference;
851 if (dump_file && (dump_flags & TDF_DETAILS))
852 fprintf (dump_file, "(analyze_ziv_subscript \n");
854 difference = chrec_fold_minus (integer_type_node, chrec_a, chrec_b);
856 switch (TREE_CODE (difference))
858 case INTEGER_CST:
859 if (integer_zerop (difference))
861 /* The difference is equal to zero: the accessed index
862 overlaps for each iteration in the loop. */
863 *overlaps_a = integer_zero_node;
864 *overlaps_b = integer_zero_node;
865 *last_conflicts = chrec_dont_know;
867 else
869 /* The accesses do not overlap. */
870 *overlaps_a = chrec_known;
871 *overlaps_b = chrec_known;
872 *last_conflicts = integer_zero_node;
874 break;
876 default:
877 /* We're not sure whether the indexes overlap. For the moment,
878 conservatively answer "don't know". */
879 *overlaps_a = chrec_dont_know;
880 *overlaps_b = chrec_dont_know;
881 *last_conflicts = chrec_dont_know;
882 break;
885 if (dump_file && (dump_flags & TDF_DETAILS))
886 fprintf (dump_file, ")\n");
889 /* Analyze a SIV (Single Index Variable) subscript where CHREC_A is a
890 constant, and CHREC_B is an affine function. *OVERLAPS_A and
891 *OVERLAPS_B are initialized to the functions that describe the
892 relation between the elements accessed twice by CHREC_A and
893 CHREC_B. For k >= 0, the following property is verified:
895 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
897 static void
898 analyze_siv_subscript_cst_affine (tree chrec_a,
899 tree chrec_b,
900 tree *overlaps_a,
901 tree *overlaps_b,
902 tree *last_conflicts)
904 bool value0, value1, value2;
905 tree difference = chrec_fold_minus
906 (integer_type_node, CHREC_LEFT (chrec_b), chrec_a);
908 if (!chrec_is_positive (initial_condition (difference), &value0))
910 *overlaps_a = chrec_dont_know;
911 *overlaps_b = chrec_dont_know;
912 *last_conflicts = chrec_dont_know;
913 return;
915 else
917 if (value0 == false)
919 if (!chrec_is_positive (CHREC_RIGHT (chrec_b), &value1))
921 *overlaps_a = chrec_dont_know;
922 *overlaps_b = chrec_dont_know;
923 *last_conflicts = chrec_dont_know;
924 return;
926 else
928 if (value1 == true)
930 /* Example:
931 chrec_a = 12
932 chrec_b = {10, +, 1}
935 if (tree_fold_divides_p
936 (integer_type_node, CHREC_RIGHT (chrec_b), difference))
938 *overlaps_a = integer_zero_node;
939 *overlaps_b = fold
940 (build (EXACT_DIV_EXPR, integer_type_node,
941 fold (build1 (ABS_EXPR, integer_type_node, difference)),
942 CHREC_RIGHT (chrec_b)));
943 *last_conflicts = integer_one_node;
944 return;
947 /* When the step does not divides the difference, there are
948 no overlaps. */
949 else
951 *overlaps_a = chrec_known;
952 *overlaps_b = chrec_known;
953 *last_conflicts = integer_zero_node;
954 return;
958 else
960 /* Example:
961 chrec_a = 12
962 chrec_b = {10, +, -1}
964 In this case, chrec_a will not overlap with chrec_b. */
965 *overlaps_a = chrec_known;
966 *overlaps_b = chrec_known;
967 *last_conflicts = integer_zero_node;
968 return;
972 else
974 if (!chrec_is_positive (CHREC_RIGHT (chrec_b), &value2))
976 *overlaps_a = chrec_dont_know;
977 *overlaps_b = chrec_dont_know;
978 *last_conflicts = chrec_dont_know;
979 return;
981 else
983 if (value2 == false)
985 /* Example:
986 chrec_a = 3
987 chrec_b = {10, +, -1}
989 if (tree_fold_divides_p
990 (integer_type_node, CHREC_RIGHT (chrec_b), difference))
992 *overlaps_a = integer_zero_node;
993 *overlaps_b = fold
994 (build (EXACT_DIV_EXPR, integer_type_node, difference,
995 CHREC_RIGHT (chrec_b)));
996 *last_conflicts = integer_one_node;
997 return;
1000 /* When the step does not divides the difference, there
1001 are no overlaps. */
1002 else
1004 *overlaps_a = chrec_known;
1005 *overlaps_b = chrec_known;
1006 *last_conflicts = integer_zero_node;
1007 return;
1010 else
1012 /* Example:
1013 chrec_a = 3
1014 chrec_b = {4, +, 1}
1016 In this case, chrec_a will not overlap with chrec_b. */
1017 *overlaps_a = chrec_known;
1018 *overlaps_b = chrec_known;
1019 *last_conflicts = integer_zero_node;
1020 return;
1027 /* Helper recursive function for initializing the matrix A. Returns
1028 the initial value of CHREC. */
1030 static int
1031 initialize_matrix_A (lambda_matrix A, tree chrec, unsigned index, int mult)
1033 gcc_assert (chrec);
1035 if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
1036 return int_cst_value (chrec);
1038 A[index][0] = mult * int_cst_value (CHREC_RIGHT (chrec));
1039 return initialize_matrix_A (A, CHREC_LEFT (chrec), index + 1, mult);
1042 #define FLOOR_DIV(x,y) ((x) / (y))
1044 /* Solves the special case of the Diophantine equation:
1045 | {0, +, STEP_A}_x (OVERLAPS_A) = {0, +, STEP_B}_y (OVERLAPS_B)
1047 Computes the descriptions OVERLAPS_A and OVERLAPS_B. NITER is the
1048 number of iterations that loops X and Y run. The overlaps will be
1049 constructed as evolutions in dimension DIM. */
1051 static void
1052 compute_overlap_steps_for_affine_univar (int niter, int step_a, int step_b,
1053 tree *overlaps_a, tree *overlaps_b,
1054 tree *last_conflicts, int dim)
1056 if (((step_a > 0 && step_b > 0)
1057 || (step_a < 0 && step_b < 0)))
1059 int step_overlaps_a, step_overlaps_b;
1060 int gcd_steps_a_b, last_conflict, tau2;
1062 gcd_steps_a_b = gcd (step_a, step_b);
1063 step_overlaps_a = step_b / gcd_steps_a_b;
1064 step_overlaps_b = step_a / gcd_steps_a_b;
1066 tau2 = FLOOR_DIV (niter, step_overlaps_a);
1067 tau2 = MIN (tau2, FLOOR_DIV (niter, step_overlaps_b));
1068 last_conflict = tau2;
1070 *overlaps_a = build_polynomial_chrec
1071 (dim, integer_zero_node,
1072 build_int_cst (NULL_TREE, step_overlaps_a));
1073 *overlaps_b = build_polynomial_chrec
1074 (dim, integer_zero_node,
1075 build_int_cst (NULL_TREE, step_overlaps_b));
1076 *last_conflicts = build_int_cst (NULL_TREE, last_conflict);
1079 else
1081 *overlaps_a = integer_zero_node;
1082 *overlaps_b = integer_zero_node;
1083 *last_conflicts = integer_zero_node;
1088 /* Solves the special case of a Diophantine equation where CHREC_A is
1089 an affine bivariate function, and CHREC_B is an affine univariate
1090 function. For example,
1092 | {{0, +, 1}_x, +, 1335}_y = {0, +, 1336}_z
1094 has the following overlapping functions:
1096 | x (t, u, v) = {{0, +, 1336}_t, +, 1}_v
1097 | y (t, u, v) = {{0, +, 1336}_u, +, 1}_v
1098 | z (t, u, v) = {{{0, +, 1}_t, +, 1335}_u, +, 1}_v
1100 FORNOW: This is a specialized implementation for a case occurring in
1101 a common benchmark. Implement the general algorithm. */
1103 static void
1104 compute_overlap_steps_for_affine_1_2 (tree chrec_a, tree chrec_b,
1105 tree *overlaps_a, tree *overlaps_b,
1106 tree *last_conflicts)
1108 bool xz_p, yz_p, xyz_p;
1109 int step_x, step_y, step_z;
1110 int niter_x, niter_y, niter_z, niter;
1111 tree numiter_x, numiter_y, numiter_z;
1112 tree overlaps_a_xz, overlaps_b_xz, last_conflicts_xz;
1113 tree overlaps_a_yz, overlaps_b_yz, last_conflicts_yz;
1114 tree overlaps_a_xyz, overlaps_b_xyz, last_conflicts_xyz;
1116 step_x = int_cst_value (CHREC_RIGHT (CHREC_LEFT (chrec_a)));
1117 step_y = int_cst_value (CHREC_RIGHT (chrec_a));
1118 step_z = int_cst_value (CHREC_RIGHT (chrec_b));
1120 numiter_x = number_of_iterations_in_loop
1121 (current_loops->parray[CHREC_VARIABLE (CHREC_LEFT (chrec_a))]);
1122 numiter_y = number_of_iterations_in_loop
1123 (current_loops->parray[CHREC_VARIABLE (chrec_a)]);
1124 numiter_z = number_of_iterations_in_loop
1125 (current_loops->parray[CHREC_VARIABLE (chrec_b)]);
1127 if (TREE_CODE (numiter_x) != INTEGER_CST)
1128 numiter_x = current_loops->parray[CHREC_VARIABLE (CHREC_LEFT (chrec_a))]
1129 ->estimated_nb_iterations;
1130 if (TREE_CODE (numiter_y) != INTEGER_CST)
1131 numiter_y = current_loops->parray[CHREC_VARIABLE (chrec_a)]
1132 ->estimated_nb_iterations;
1133 if (TREE_CODE (numiter_z) != INTEGER_CST)
1134 numiter_z = current_loops->parray[CHREC_VARIABLE (chrec_b)]
1135 ->estimated_nb_iterations;
1137 if (numiter_x == NULL_TREE || numiter_y == NULL_TREE
1138 || numiter_z == NULL_TREE)
1140 *overlaps_a = chrec_dont_know;
1141 *overlaps_b = chrec_dont_know;
1142 *last_conflicts = chrec_dont_know;
1143 return;
1146 niter_x = int_cst_value (numiter_x);
1147 niter_y = int_cst_value (numiter_y);
1148 niter_z = int_cst_value (numiter_z);
1150 niter = MIN (niter_x, niter_z);
1151 compute_overlap_steps_for_affine_univar (niter, step_x, step_z,
1152 &overlaps_a_xz,
1153 &overlaps_b_xz,
1154 &last_conflicts_xz, 1);
1155 niter = MIN (niter_y, niter_z);
1156 compute_overlap_steps_for_affine_univar (niter, step_y, step_z,
1157 &overlaps_a_yz,
1158 &overlaps_b_yz,
1159 &last_conflicts_yz, 2);
1160 niter = MIN (niter_x, niter_z);
1161 niter = MIN (niter_y, niter);
1162 compute_overlap_steps_for_affine_univar (niter, step_x + step_y, step_z,
1163 &overlaps_a_xyz,
1164 &overlaps_b_xyz,
1165 &last_conflicts_xyz, 3);
1167 xz_p = !integer_zerop (last_conflicts_xz);
1168 yz_p = !integer_zerop (last_conflicts_yz);
1169 xyz_p = !integer_zerop (last_conflicts_xyz);
1171 if (xz_p || yz_p || xyz_p)
1173 *overlaps_a = make_tree_vec (2);
1174 TREE_VEC_ELT (*overlaps_a, 0) = integer_zero_node;
1175 TREE_VEC_ELT (*overlaps_a, 1) = integer_zero_node;
1176 *overlaps_b = integer_zero_node;
1177 if (xz_p)
1179 TREE_VEC_ELT (*overlaps_a, 0) =
1180 chrec_fold_plus (integer_type_node, TREE_VEC_ELT (*overlaps_a, 0),
1181 overlaps_a_xz);
1182 *overlaps_b =
1183 chrec_fold_plus (integer_type_node, *overlaps_b, overlaps_b_xz);
1184 *last_conflicts = last_conflicts_xz;
1186 if (yz_p)
1188 TREE_VEC_ELT (*overlaps_a, 1) =
1189 chrec_fold_plus (integer_type_node, TREE_VEC_ELT (*overlaps_a, 1),
1190 overlaps_a_yz);
1191 *overlaps_b =
1192 chrec_fold_plus (integer_type_node, *overlaps_b, overlaps_b_yz);
1193 *last_conflicts = last_conflicts_yz;
1195 if (xyz_p)
1197 TREE_VEC_ELT (*overlaps_a, 0) =
1198 chrec_fold_plus (integer_type_node, TREE_VEC_ELT (*overlaps_a, 0),
1199 overlaps_a_xyz);
1200 TREE_VEC_ELT (*overlaps_a, 1) =
1201 chrec_fold_plus (integer_type_node, TREE_VEC_ELT (*overlaps_a, 1),
1202 overlaps_a_xyz);
1203 *overlaps_b =
1204 chrec_fold_plus (integer_type_node, *overlaps_b, overlaps_b_xyz);
1205 *last_conflicts = last_conflicts_xyz;
1208 else
1210 *overlaps_a = integer_zero_node;
1211 *overlaps_b = integer_zero_node;
1212 *last_conflicts = integer_zero_node;
1216 /* Determines the overlapping elements due to accesses CHREC_A and
1217 CHREC_B, that are affine functions. This is a part of the
1218 subscript analyzer. */
1220 static void
1221 analyze_subscript_affine_affine (tree chrec_a,
1222 tree chrec_b,
1223 tree *overlaps_a,
1224 tree *overlaps_b,
1225 tree *last_conflicts)
1227 unsigned nb_vars_a, nb_vars_b, dim;
1228 int init_a, init_b, gamma, gcd_alpha_beta;
1229 int tau1, tau2;
1230 lambda_matrix A, U, S;
1232 if (dump_file && (dump_flags & TDF_DETAILS))
1233 fprintf (dump_file, "(analyze_subscript_affine_affine \n");
1235 /* For determining the initial intersection, we have to solve a
1236 Diophantine equation. This is the most time consuming part.
1238 For answering to the question: "Is there a dependence?" we have
1239 to prove that there exists a solution to the Diophantine
1240 equation, and that the solution is in the iteration domain,
1241 i.e. the solution is positive or zero, and that the solution
1242 happens before the upper bound loop.nb_iterations. Otherwise
1243 there is no dependence. This function outputs a description of
1244 the iterations that hold the intersections. */
1247 nb_vars_a = nb_vars_in_chrec (chrec_a);
1248 nb_vars_b = nb_vars_in_chrec (chrec_b);
1250 dim = nb_vars_a + nb_vars_b;
1251 U = lambda_matrix_new (dim, dim);
1252 A = lambda_matrix_new (dim, 1);
1253 S = lambda_matrix_new (dim, 1);
1255 init_a = initialize_matrix_A (A, chrec_a, 0, 1);
1256 init_b = initialize_matrix_A (A, chrec_b, nb_vars_a, -1);
1257 gamma = init_b - init_a;
1259 /* Don't do all the hard work of solving the Diophantine equation
1260 when we already know the solution: for example,
1261 | {3, +, 1}_1
1262 | {3, +, 4}_2
1263 | gamma = 3 - 3 = 0.
1264 Then the first overlap occurs during the first iterations:
1265 | {3, +, 1}_1 ({0, +, 4}_x) = {3, +, 4}_2 ({0, +, 1}_x)
1267 if (gamma == 0)
1269 if (nb_vars_a == 1 && nb_vars_b == 1)
1271 int step_a, step_b;
1272 int niter, niter_a, niter_b;
1273 tree numiter_a, numiter_b;
1275 numiter_a = number_of_iterations_in_loop
1276 (current_loops->parray[CHREC_VARIABLE (chrec_a)]);
1277 numiter_b = number_of_iterations_in_loop
1278 (current_loops->parray[CHREC_VARIABLE (chrec_b)]);
1280 if (TREE_CODE (numiter_a) != INTEGER_CST)
1281 numiter_a = current_loops->parray[CHREC_VARIABLE (chrec_a)]
1282 ->estimated_nb_iterations;
1283 if (TREE_CODE (numiter_b) != INTEGER_CST)
1284 numiter_b = current_loops->parray[CHREC_VARIABLE (chrec_b)]
1285 ->estimated_nb_iterations;
1286 if (numiter_a == NULL_TREE || numiter_b == NULL_TREE)
1288 *overlaps_a = chrec_dont_know;
1289 *overlaps_b = chrec_dont_know;
1290 *last_conflicts = chrec_dont_know;
1291 return;
1294 niter_a = int_cst_value (numiter_a);
1295 niter_b = int_cst_value (numiter_b);
1296 niter = MIN (niter_a, niter_b);
1298 step_a = int_cst_value (CHREC_RIGHT (chrec_a));
1299 step_b = int_cst_value (CHREC_RIGHT (chrec_b));
1301 compute_overlap_steps_for_affine_univar (niter, step_a, step_b,
1302 overlaps_a, overlaps_b,
1303 last_conflicts, 1);
1306 else if (nb_vars_a == 2 && nb_vars_b == 1)
1307 compute_overlap_steps_for_affine_1_2
1308 (chrec_a, chrec_b, overlaps_a, overlaps_b, last_conflicts);
1310 else if (nb_vars_a == 1 && nb_vars_b == 2)
1311 compute_overlap_steps_for_affine_1_2
1312 (chrec_b, chrec_a, overlaps_b, overlaps_a, last_conflicts);
1314 else
1316 *overlaps_a = chrec_dont_know;
1317 *overlaps_b = chrec_dont_know;
1318 *last_conflicts = chrec_dont_know;
1320 return;
1323 /* U.A = S */
1324 lambda_matrix_right_hermite (A, dim, 1, S, U);
1326 if (S[0][0] < 0)
1328 S[0][0] *= -1;
1329 lambda_matrix_row_negate (U, dim, 0);
1331 gcd_alpha_beta = S[0][0];
1333 /* The classic "gcd-test". */
1334 if (!int_divides_p (gcd_alpha_beta, gamma))
1336 /* The "gcd-test" has determined that there is no integer
1337 solution, i.e. there is no dependence. */
1338 *overlaps_a = chrec_known;
1339 *overlaps_b = chrec_known;
1340 *last_conflicts = integer_zero_node;
1343 /* Both access functions are univariate. This includes SIV and MIV cases. */
1344 else if (nb_vars_a == 1 && nb_vars_b == 1)
1346 /* Both functions should have the same evolution sign. */
1347 if (((A[0][0] > 0 && -A[1][0] > 0)
1348 || (A[0][0] < 0 && -A[1][0] < 0)))
1350 /* The solutions are given by:
1352 | [GAMMA/GCD_ALPHA_BETA t].[u11 u12] = [x0]
1353 | [u21 u22] [y0]
1355 For a given integer t. Using the following variables,
1357 | i0 = u11 * gamma / gcd_alpha_beta
1358 | j0 = u12 * gamma / gcd_alpha_beta
1359 | i1 = u21
1360 | j1 = u22
1362 the solutions are:
1364 | x0 = i0 + i1 * t,
1365 | y0 = j0 + j1 * t. */
1367 int i0, j0, i1, j1;
1369 /* X0 and Y0 are the first iterations for which there is a
1370 dependence. X0, Y0 are two solutions of the Diophantine
1371 equation: chrec_a (X0) = chrec_b (Y0). */
1372 int x0, y0;
1373 int niter, niter_a, niter_b;
1374 tree numiter_a, numiter_b;
1376 numiter_a = number_of_iterations_in_loop
1377 (current_loops->parray[CHREC_VARIABLE (chrec_a)]);
1378 numiter_b = number_of_iterations_in_loop
1379 (current_loops->parray[CHREC_VARIABLE (chrec_b)]);
1381 if (TREE_CODE (numiter_a) != INTEGER_CST)
1382 numiter_a = current_loops->parray[CHREC_VARIABLE (chrec_a)]
1383 ->estimated_nb_iterations;
1384 if (TREE_CODE (numiter_b) != INTEGER_CST)
1385 numiter_b = current_loops->parray[CHREC_VARIABLE (chrec_b)]
1386 ->estimated_nb_iterations;
1387 if (numiter_a == NULL_TREE || numiter_b == NULL_TREE)
1389 *overlaps_a = chrec_dont_know;
1390 *overlaps_b = chrec_dont_know;
1391 *last_conflicts = chrec_dont_know;
1392 return;
1395 niter_a = int_cst_value (numiter_a);
1396 niter_b = int_cst_value (numiter_b);
1397 niter = MIN (niter_a, niter_b);
1399 i0 = U[0][0] * gamma / gcd_alpha_beta;
1400 j0 = U[0][1] * gamma / gcd_alpha_beta;
1401 i1 = U[1][0];
1402 j1 = U[1][1];
1404 if ((i1 == 0 && i0 < 0)
1405 || (j1 == 0 && j0 < 0))
1407 /* There is no solution.
1408 FIXME: The case "i0 > nb_iterations, j0 > nb_iterations"
1409 falls in here, but for the moment we don't look at the
1410 upper bound of the iteration domain. */
1411 *overlaps_a = chrec_known;
1412 *overlaps_b = chrec_known;
1413 *last_conflicts = integer_zero_node;
1416 else
1418 if (i1 > 0)
1420 tau1 = CEIL (-i0, i1);
1421 tau2 = FLOOR_DIV (niter - i0, i1);
1423 if (j1 > 0)
1425 int last_conflict, min_multiple;
1426 tau1 = MAX (tau1, CEIL (-j0, j1));
1427 tau2 = MIN (tau2, FLOOR_DIV (niter - j0, j1));
1429 x0 = i1 * tau1 + i0;
1430 y0 = j1 * tau1 + j0;
1432 /* At this point (x0, y0) is one of the
1433 solutions to the Diophantine equation. The
1434 next step has to compute the smallest
1435 positive solution: the first conflicts. */
1436 min_multiple = MIN (x0 / i1, y0 / j1);
1437 x0 -= i1 * min_multiple;
1438 y0 -= j1 * min_multiple;
1440 tau1 = (x0 - i0)/i1;
1441 last_conflict = tau2 - tau1;
1443 *overlaps_a = build_polynomial_chrec
1445 build_int_cst (NULL_TREE, x0),
1446 build_int_cst (NULL_TREE, i1));
1447 *overlaps_b = build_polynomial_chrec
1449 build_int_cst (NULL_TREE, y0),
1450 build_int_cst (NULL_TREE, j1));
1451 *last_conflicts = build_int_cst (NULL_TREE, last_conflict);
1453 else
1455 /* FIXME: For the moment, the upper bound of the
1456 iteration domain for j is not checked. */
1457 *overlaps_a = chrec_dont_know;
1458 *overlaps_b = chrec_dont_know;
1459 *last_conflicts = chrec_dont_know;
1463 else
1465 /* FIXME: For the moment, the upper bound of the
1466 iteration domain for i is not checked. */
1467 *overlaps_a = chrec_dont_know;
1468 *overlaps_b = chrec_dont_know;
1469 *last_conflicts = chrec_dont_know;
1473 else
1475 *overlaps_a = chrec_dont_know;
1476 *overlaps_b = chrec_dont_know;
1477 *last_conflicts = chrec_dont_know;
1481 else
1483 *overlaps_a = chrec_dont_know;
1484 *overlaps_b = chrec_dont_know;
1485 *last_conflicts = chrec_dont_know;
1489 if (dump_file && (dump_flags & TDF_DETAILS))
1491 fprintf (dump_file, " (overlaps_a = ");
1492 print_generic_expr (dump_file, *overlaps_a, 0);
1493 fprintf (dump_file, ")\n (overlaps_b = ");
1494 print_generic_expr (dump_file, *overlaps_b, 0);
1495 fprintf (dump_file, ")\n");
1498 if (dump_file && (dump_flags & TDF_DETAILS))
1499 fprintf (dump_file, ")\n");
1502 /* Analyze a SIV (Single Index Variable) subscript. *OVERLAPS_A and
1503 *OVERLAPS_B are initialized to the functions that describe the
1504 relation between the elements accessed twice by CHREC_A and
1505 CHREC_B. For k >= 0, the following property is verified:
1507 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
1509 static void
1510 analyze_siv_subscript (tree chrec_a,
1511 tree chrec_b,
1512 tree *overlaps_a,
1513 tree *overlaps_b,
1514 tree *last_conflicts)
1516 if (dump_file && (dump_flags & TDF_DETAILS))
1517 fprintf (dump_file, "(analyze_siv_subscript \n");
1519 if (evolution_function_is_constant_p (chrec_a)
1520 && evolution_function_is_affine_p (chrec_b))
1521 analyze_siv_subscript_cst_affine (chrec_a, chrec_b,
1522 overlaps_a, overlaps_b, last_conflicts);
1524 else if (evolution_function_is_affine_p (chrec_a)
1525 && evolution_function_is_constant_p (chrec_b))
1526 analyze_siv_subscript_cst_affine (chrec_b, chrec_a,
1527 overlaps_b, overlaps_a, last_conflicts);
1529 else if (evolution_function_is_affine_p (chrec_a)
1530 && evolution_function_is_affine_p (chrec_b))
1531 analyze_subscript_affine_affine (chrec_a, chrec_b,
1532 overlaps_a, overlaps_b, last_conflicts);
1533 else
1535 *overlaps_a = chrec_dont_know;
1536 *overlaps_b = chrec_dont_know;
1537 *last_conflicts = chrec_dont_know;
1540 if (dump_file && (dump_flags & TDF_DETAILS))
1541 fprintf (dump_file, ")\n");
1544 /* Return true when the evolution steps of an affine CHREC divide the
1545 constant CST. */
1547 static bool
1548 chrec_steps_divide_constant_p (tree chrec,
1549 tree cst)
1551 switch (TREE_CODE (chrec))
1553 case POLYNOMIAL_CHREC:
1554 return (tree_fold_divides_p (integer_type_node, CHREC_RIGHT (chrec), cst)
1555 && chrec_steps_divide_constant_p (CHREC_LEFT (chrec), cst));
1557 default:
1558 /* On the initial condition, return true. */
1559 return true;
1563 /* Analyze a MIV (Multiple Index Variable) subscript. *OVERLAPS_A and
1564 *OVERLAPS_B are initialized to the functions that describe the
1565 relation between the elements accessed twice by CHREC_A and
1566 CHREC_B. For k >= 0, the following property is verified:
1568 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
1570 static void
1571 analyze_miv_subscript (tree chrec_a,
1572 tree chrec_b,
1573 tree *overlaps_a,
1574 tree *overlaps_b,
1575 tree *last_conflicts)
1577 /* FIXME: This is a MIV subscript, not yet handled.
1578 Example: (A[{1, +, 1}_1] vs. A[{1, +, 1}_2]) that comes from
1579 (A[i] vs. A[j]).
1581 In the SIV test we had to solve a Diophantine equation with two
1582 variables. In the MIV case we have to solve a Diophantine
1583 equation with 2*n variables (if the subscript uses n IVs).
1585 tree difference;
1587 if (dump_file && (dump_flags & TDF_DETAILS))
1588 fprintf (dump_file, "(analyze_miv_subscript \n");
1590 difference = chrec_fold_minus (integer_type_node, chrec_a, chrec_b);
1592 if (chrec_zerop (difference))
1594 /* Access functions are the same: all the elements are accessed
1595 in the same order. */
1596 *overlaps_a = integer_zero_node;
1597 *overlaps_b = integer_zero_node;
1598 *last_conflicts = number_of_iterations_in_loop
1599 (current_loops->parray[CHREC_VARIABLE (chrec_a)]);
1602 else if (evolution_function_is_constant_p (difference)
1603 /* For the moment, the following is verified:
1604 evolution_function_is_affine_multivariate_p (chrec_a) */
1605 && !chrec_steps_divide_constant_p (chrec_a, difference))
1607 /* testsuite/.../ssa-chrec-33.c
1608 {{21, +, 2}_1, +, -2}_2 vs. {{20, +, 2}_1, +, -2}_2
1610 The difference is 1, and the evolution steps are equal to 2,
1611 consequently there are no overlapping elements. */
1612 *overlaps_a = chrec_known;
1613 *overlaps_b = chrec_known;
1614 *last_conflicts = integer_zero_node;
1617 else if (evolution_function_is_affine_multivariate_p (chrec_a)
1618 && evolution_function_is_affine_multivariate_p (chrec_b))
1620 /* testsuite/.../ssa-chrec-35.c
1621 {0, +, 1}_2 vs. {0, +, 1}_3
1622 the overlapping elements are respectively located at iterations:
1623 {0, +, 1}_x and {0, +, 1}_x,
1624 in other words, we have the equality:
1625 {0, +, 1}_2 ({0, +, 1}_x) = {0, +, 1}_3 ({0, +, 1}_x)
1627 Other examples:
1628 {{0, +, 1}_1, +, 2}_2 ({0, +, 1}_x, {0, +, 1}_y) =
1629 {0, +, 1}_1 ({{0, +, 1}_x, +, 2}_y)
1631 {{0, +, 2}_1, +, 3}_2 ({0, +, 1}_y, {0, +, 1}_x) =
1632 {{0, +, 3}_1, +, 2}_2 ({0, +, 1}_x, {0, +, 1}_y)
1634 analyze_subscript_affine_affine (chrec_a, chrec_b,
1635 overlaps_a, overlaps_b, last_conflicts);
1638 else
1640 /* When the analysis is too difficult, answer "don't know". */
1641 *overlaps_a = chrec_dont_know;
1642 *overlaps_b = chrec_dont_know;
1643 *last_conflicts = chrec_dont_know;
1646 if (dump_file && (dump_flags & TDF_DETAILS))
1647 fprintf (dump_file, ")\n");
1650 /* Determines the iterations for which CHREC_A is equal to CHREC_B.
1651 OVERLAP_ITERATIONS_A and OVERLAP_ITERATIONS_B are initialized with
1652 two functions that describe the iterations that contain conflicting
1653 elements.
1655 Remark: For an integer k >= 0, the following equality is true:
1657 CHREC_A (OVERLAP_ITERATIONS_A (k)) == CHREC_B (OVERLAP_ITERATIONS_B (k)).
1660 static void
1661 analyze_overlapping_iterations (tree chrec_a,
1662 tree chrec_b,
1663 tree *overlap_iterations_a,
1664 tree *overlap_iterations_b,
1665 tree *last_conflicts)
1667 if (dump_file && (dump_flags & TDF_DETAILS))
1669 fprintf (dump_file, "(analyze_overlapping_iterations \n");
1670 fprintf (dump_file, " (chrec_a = ");
1671 print_generic_expr (dump_file, chrec_a, 0);
1672 fprintf (dump_file, ")\n chrec_b = ");
1673 print_generic_expr (dump_file, chrec_b, 0);
1674 fprintf (dump_file, ")\n");
1677 if (chrec_a == NULL_TREE
1678 || chrec_b == NULL_TREE
1679 || chrec_contains_undetermined (chrec_a)
1680 || chrec_contains_undetermined (chrec_b)
1681 || chrec_contains_symbols (chrec_a)
1682 || chrec_contains_symbols (chrec_b))
1684 *overlap_iterations_a = chrec_dont_know;
1685 *overlap_iterations_b = chrec_dont_know;
1688 else if (ziv_subscript_p (chrec_a, chrec_b))
1689 analyze_ziv_subscript (chrec_a, chrec_b,
1690 overlap_iterations_a, overlap_iterations_b,
1691 last_conflicts);
1693 else if (siv_subscript_p (chrec_a, chrec_b))
1694 analyze_siv_subscript (chrec_a, chrec_b,
1695 overlap_iterations_a, overlap_iterations_b,
1696 last_conflicts);
1698 else
1699 analyze_miv_subscript (chrec_a, chrec_b,
1700 overlap_iterations_a, overlap_iterations_b,
1701 last_conflicts);
1703 if (dump_file && (dump_flags & TDF_DETAILS))
1705 fprintf (dump_file, " (overlap_iterations_a = ");
1706 print_generic_expr (dump_file, *overlap_iterations_a, 0);
1707 fprintf (dump_file, ")\n (overlap_iterations_b = ");
1708 print_generic_expr (dump_file, *overlap_iterations_b, 0);
1709 fprintf (dump_file, ")\n");
1715 /* This section contains the affine functions dependences detector. */
1717 /* Computes the conflicting iterations, and initialize DDR. */
1719 static void
1720 subscript_dependence_tester (struct data_dependence_relation *ddr)
1722 unsigned int i;
1723 struct data_reference *dra = DDR_A (ddr);
1724 struct data_reference *drb = DDR_B (ddr);
1725 tree last_conflicts;
1727 if (dump_file && (dump_flags & TDF_DETAILS))
1728 fprintf (dump_file, "(subscript_dependence_tester \n");
1730 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
1732 tree overlaps_a, overlaps_b;
1733 struct subscript *subscript = DDR_SUBSCRIPT (ddr, i);
1735 analyze_overlapping_iterations (DR_ACCESS_FN (dra, i),
1736 DR_ACCESS_FN (drb, i),
1737 &overlaps_a, &overlaps_b,
1738 &last_conflicts);
1740 if (chrec_contains_undetermined (overlaps_a)
1741 || chrec_contains_undetermined (overlaps_b))
1743 finalize_ddr_dependent (ddr, chrec_dont_know);
1744 break;
1747 else if (overlaps_a == chrec_known
1748 || overlaps_b == chrec_known)
1750 finalize_ddr_dependent (ddr, chrec_known);
1751 break;
1754 else
1756 SUB_CONFLICTS_IN_A (subscript) = overlaps_a;
1757 SUB_CONFLICTS_IN_B (subscript) = overlaps_b;
1758 SUB_LAST_CONFLICT (subscript) = last_conflicts;
1762 if (dump_file && (dump_flags & TDF_DETAILS))
1763 fprintf (dump_file, ")\n");
1766 /* Compute the classic per loop distance vector.
1768 DDR is the data dependence relation to build a vector from.
1769 NB_LOOPS is the total number of loops we are considering.
1770 FIRST_LOOP_DEPTH is the loop->depth of the first loop in the analyzed
1771 loop nest.
1772 Return FALSE if the dependence relation is outside of the loop nest
1773 starting at FIRST_LOOP_DEPTH.
1774 Return TRUE otherwise. */
1776 static bool
1777 build_classic_dist_vector (struct data_dependence_relation *ddr,
1778 int nb_loops, int first_loop_depth)
1780 unsigned i;
1781 lambda_vector dist_v, init_v;
1783 dist_v = lambda_vector_new (nb_loops);
1784 init_v = lambda_vector_new (nb_loops);
1785 lambda_vector_clear (dist_v, nb_loops);
1786 lambda_vector_clear (init_v, nb_loops);
1788 if (DDR_ARE_DEPENDENT (ddr) != NULL_TREE)
1789 return true;
1791 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
1793 tree access_fn_a, access_fn_b;
1794 struct subscript *subscript = DDR_SUBSCRIPT (ddr, i);
1796 if (chrec_contains_undetermined (SUB_DISTANCE (subscript)))
1798 non_affine_dependence_relation (ddr);
1799 return true;
1802 access_fn_a = DR_ACCESS_FN (DDR_A (ddr), i);
1803 access_fn_b = DR_ACCESS_FN (DDR_B (ddr), i);
1805 if (TREE_CODE (access_fn_a) == POLYNOMIAL_CHREC
1806 && TREE_CODE (access_fn_b) == POLYNOMIAL_CHREC)
1808 int dist, loop_nb, loop_depth;
1809 int loop_nb_a = CHREC_VARIABLE (access_fn_a);
1810 int loop_nb_b = CHREC_VARIABLE (access_fn_b);
1811 struct loop *loop_a = current_loops->parray[loop_nb_a];
1812 struct loop *loop_b = current_loops->parray[loop_nb_b];
1814 /* If the loop for either variable is at a lower depth than
1815 the first_loop's depth, then we can't possibly have a
1816 dependency at this level of the loop. */
1818 if (loop_a->depth < first_loop_depth
1819 || loop_b->depth < first_loop_depth)
1820 return false;
1822 if (loop_nb_a != loop_nb_b
1823 && !flow_loop_nested_p (loop_a, loop_b)
1824 && !flow_loop_nested_p (loop_b, loop_a))
1826 /* Example: when there are two consecutive loops,
1828 | loop_1
1829 | A[{0, +, 1}_1]
1830 | endloop_1
1831 | loop_2
1832 | A[{0, +, 1}_2]
1833 | endloop_2
1835 the dependence relation cannot be captured by the
1836 distance abstraction. */
1837 non_affine_dependence_relation (ddr);
1838 return true;
1841 /* The dependence is carried by the outermost loop. Example:
1842 | loop_1
1843 | A[{4, +, 1}_1]
1844 | loop_2
1845 | A[{5, +, 1}_2]
1846 | endloop_2
1847 | endloop_1
1848 In this case, the dependence is carried by loop_1. */
1849 loop_nb = loop_nb_a < loop_nb_b ? loop_nb_a : loop_nb_b;
1850 loop_depth = current_loops->parray[loop_nb]->depth - first_loop_depth;
1852 /* If the loop number is still greater than the number of
1853 loops we've been asked to analyze, or negative,
1854 something is borked. */
1855 gcc_assert (loop_depth >= 0);
1856 gcc_assert (loop_depth < nb_loops);
1857 if (chrec_contains_undetermined (SUB_DISTANCE (subscript)))
1859 non_affine_dependence_relation (ddr);
1860 return true;
1863 dist = int_cst_value (SUB_DISTANCE (subscript));
1865 /* This is the subscript coupling test.
1866 | loop i = 0, N, 1
1867 | T[i+1][i] = ...
1868 | ... = T[i][i]
1869 | endloop
1870 There is no dependence. */
1871 if (init_v[loop_depth] != 0
1872 && dist_v[loop_depth] != dist)
1874 finalize_ddr_dependent (ddr, chrec_known);
1875 return true;
1878 dist_v[loop_depth] = dist;
1879 init_v[loop_depth] = 1;
1883 /* There is a distance of 1 on all the outer loops:
1885 Example: there is a dependence of distance 1 on loop_1 for the array A.
1886 | loop_1
1887 | A[5] = ...
1888 | endloop
1891 struct loop *lca, *loop_a, *loop_b;
1892 struct data_reference *a = DDR_A (ddr);
1893 struct data_reference *b = DDR_B (ddr);
1894 int lca_depth;
1895 loop_a = loop_containing_stmt (DR_STMT (a));
1896 loop_b = loop_containing_stmt (DR_STMT (b));
1898 /* Get the common ancestor loop. */
1899 lca = find_common_loop (loop_a, loop_b);
1901 lca_depth = lca->depth;
1902 lca_depth -= first_loop_depth;
1903 gcc_assert (lca_depth >= 0);
1904 gcc_assert (lca_depth < nb_loops);
1906 /* For each outer loop where init_v is not set, the accesses are
1907 in dependence of distance 1 in the loop. */
1908 if (lca != loop_a
1909 && lca != loop_b
1910 && init_v[lca_depth] == 0)
1911 dist_v[lca_depth] = 1;
1913 lca = lca->outer;
1915 if (lca)
1917 lca_depth = lca->depth - first_loop_depth;
1918 while (lca->depth != 0)
1920 /* If we're considering just a sub-nest, then don't record
1921 any information on the outer loops. */
1922 if (lca_depth < 0)
1923 break;
1925 gcc_assert (lca_depth < nb_loops);
1927 if (init_v[lca_depth] == 0)
1928 dist_v[lca_depth] = 1;
1929 lca = lca->outer;
1930 lca_depth = lca->depth - first_loop_depth;
1936 DDR_DIST_VECT (ddr) = dist_v;
1937 DDR_SIZE_VECT (ddr) = nb_loops;
1938 return true;
1941 /* Compute the classic per loop direction vector.
1943 DDR is the data dependence relation to build a vector from.
1944 NB_LOOPS is the total number of loops we are considering.
1945 FIRST_LOOP_DEPTH is the loop->depth of the first loop in the analyzed
1946 loop nest.
1947 Return FALSE if the dependence relation is outside of the loop nest
1948 at FIRST_LOOP_DEPTH.
1949 Return TRUE otherwise. */
1951 static bool
1952 build_classic_dir_vector (struct data_dependence_relation *ddr,
1953 int nb_loops, int first_loop_depth)
1955 unsigned i;
1956 lambda_vector dir_v, init_v;
1958 dir_v = lambda_vector_new (nb_loops);
1959 init_v = lambda_vector_new (nb_loops);
1960 lambda_vector_clear (dir_v, nb_loops);
1961 lambda_vector_clear (init_v, nb_loops);
1963 if (DDR_ARE_DEPENDENT (ddr) != NULL_TREE)
1964 return true;
1966 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
1968 tree access_fn_a, access_fn_b;
1969 struct subscript *subscript = DDR_SUBSCRIPT (ddr, i);
1971 if (chrec_contains_undetermined (SUB_DISTANCE (subscript)))
1973 non_affine_dependence_relation (ddr);
1974 return true;
1977 access_fn_a = DR_ACCESS_FN (DDR_A (ddr), i);
1978 access_fn_b = DR_ACCESS_FN (DDR_B (ddr), i);
1979 if (TREE_CODE (access_fn_a) == POLYNOMIAL_CHREC
1980 && TREE_CODE (access_fn_b) == POLYNOMIAL_CHREC)
1982 int dist, loop_nb, loop_depth;
1983 enum data_dependence_direction dir = dir_star;
1984 int loop_nb_a = CHREC_VARIABLE (access_fn_a);
1985 int loop_nb_b = CHREC_VARIABLE (access_fn_b);
1986 struct loop *loop_a = current_loops->parray[loop_nb_a];
1987 struct loop *loop_b = current_loops->parray[loop_nb_b];
1989 /* If the loop for either variable is at a lower depth than
1990 the first_loop's depth, then we can't possibly have a
1991 dependency at this level of the loop. */
1993 if (loop_a->depth < first_loop_depth
1994 || loop_b->depth < first_loop_depth)
1995 return false;
1997 if (loop_nb_a != loop_nb_b
1998 && !flow_loop_nested_p (loop_a, loop_b)
1999 && !flow_loop_nested_p (loop_b, loop_a))
2001 /* Example: when there are two consecutive loops,
2003 | loop_1
2004 | A[{0, +, 1}_1]
2005 | endloop_1
2006 | loop_2
2007 | A[{0, +, 1}_2]
2008 | endloop_2
2010 the dependence relation cannot be captured by the
2011 distance abstraction. */
2012 non_affine_dependence_relation (ddr);
2013 return true;
2016 /* The dependence is carried by the outermost loop. Example:
2017 | loop_1
2018 | A[{4, +, 1}_1]
2019 | loop_2
2020 | A[{5, +, 1}_2]
2021 | endloop_2
2022 | endloop_1
2023 In this case, the dependence is carried by loop_1. */
2024 loop_nb = loop_nb_a < loop_nb_b ? loop_nb_a : loop_nb_b;
2025 loop_depth = current_loops->parray[loop_nb]->depth - first_loop_depth;
2027 /* If the loop number is still greater than the number of
2028 loops we've been asked to analyze, or negative,
2029 something is borked. */
2030 gcc_assert (loop_depth >= 0);
2031 gcc_assert (loop_depth < nb_loops);
2033 if (chrec_contains_undetermined (SUB_DISTANCE (subscript)))
2035 non_affine_dependence_relation (ddr);
2036 return true;
2039 dist = int_cst_value (SUB_DISTANCE (subscript));
2041 if (dist == 0)
2042 dir = dir_equal;
2043 else if (dist > 0)
2044 dir = dir_positive;
2045 else if (dist < 0)
2046 dir = dir_negative;
2048 /* This is the subscript coupling test.
2049 | loop i = 0, N, 1
2050 | T[i+1][i] = ...
2051 | ... = T[i][i]
2052 | endloop
2053 There is no dependence. */
2054 if (init_v[loop_depth] != 0
2055 && dir != dir_star
2056 && (enum data_dependence_direction) dir_v[loop_depth] != dir
2057 && (enum data_dependence_direction) dir_v[loop_depth] != dir_star)
2059 finalize_ddr_dependent (ddr, chrec_known);
2060 return true;
2063 dir_v[loop_depth] = dir;
2064 init_v[loop_depth] = 1;
2068 /* There is a distance of 1 on all the outer loops:
2070 Example: there is a dependence of distance 1 on loop_1 for the array A.
2071 | loop_1
2072 | A[5] = ...
2073 | endloop
2076 struct loop *lca, *loop_a, *loop_b;
2077 struct data_reference *a = DDR_A (ddr);
2078 struct data_reference *b = DDR_B (ddr);
2079 int lca_depth;
2080 loop_a = loop_containing_stmt (DR_STMT (a));
2081 loop_b = loop_containing_stmt (DR_STMT (b));
2083 /* Get the common ancestor loop. */
2084 lca = find_common_loop (loop_a, loop_b);
2085 lca_depth = lca->depth - first_loop_depth;
2087 gcc_assert (lca_depth >= 0);
2088 gcc_assert (lca_depth < nb_loops);
2090 /* For each outer loop where init_v is not set, the accesses are
2091 in dependence of distance 1 in the loop. */
2092 if (lca != loop_a
2093 && lca != loop_b
2094 && init_v[lca_depth] == 0)
2095 dir_v[lca_depth] = dir_positive;
2097 lca = lca->outer;
2098 if (lca)
2100 lca_depth = lca->depth - first_loop_depth;
2101 while (lca->depth != 0)
2103 /* If we're considering just a sub-nest, then don't record
2104 any information on the outer loops. */
2105 if (lca_depth < 0)
2106 break;
2108 gcc_assert (lca_depth < nb_loops);
2110 if (init_v[lca_depth] == 0)
2111 dir_v[lca_depth] = dir_positive;
2112 lca = lca->outer;
2113 lca_depth = lca->depth - first_loop_depth;
2119 DDR_DIR_VECT (ddr) = dir_v;
2120 DDR_SIZE_VECT (ddr) = nb_loops;
2121 return true;
2124 /* Returns true when all the access functions of A are affine or
2125 constant. */
2127 static bool
2128 access_functions_are_affine_or_constant_p (struct data_reference *a)
2130 unsigned int i;
2131 varray_type fns = DR_ACCESS_FNS (a);
2133 for (i = 0; i < VARRAY_ACTIVE_SIZE (fns); i++)
2134 if (!evolution_function_is_constant_p (VARRAY_TREE (fns, i))
2135 && !evolution_function_is_affine_multivariate_p (VARRAY_TREE (fns, i)))
2136 return false;
2138 return true;
2141 /* This computes the affine dependence relation between A and B.
2142 CHREC_KNOWN is used for representing the independence between two
2143 accesses, while CHREC_DONT_KNOW is used for representing the unknown
2144 relation.
2146 Note that it is possible to stop the computation of the dependence
2147 relation the first time we detect a CHREC_KNOWN element for a given
2148 subscript. */
2150 void
2151 compute_affine_dependence (struct data_dependence_relation *ddr)
2153 struct data_reference *dra = DDR_A (ddr);
2154 struct data_reference *drb = DDR_B (ddr);
2156 if (dump_file && (dump_flags & TDF_DETAILS))
2158 fprintf (dump_file, "(compute_affine_dependence\n");
2159 fprintf (dump_file, " (stmt_a = \n");
2160 print_generic_expr (dump_file, DR_STMT (dra), 0);
2161 fprintf (dump_file, ")\n (stmt_b = \n");
2162 print_generic_expr (dump_file, DR_STMT (drb), 0);
2163 fprintf (dump_file, ")\n");
2166 /* Analyze only when the dependence relation is not yet known. */
2167 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
2169 if (access_functions_are_affine_or_constant_p (dra)
2170 && access_functions_are_affine_or_constant_p (drb))
2171 subscript_dependence_tester (ddr);
2173 /* As a last case, if the dependence cannot be determined, or if
2174 the dependence is considered too difficult to determine, answer
2175 "don't know". */
2176 else
2177 finalize_ddr_dependent (ddr, chrec_dont_know);
2180 if (dump_file && (dump_flags & TDF_DETAILS))
2181 fprintf (dump_file, ")\n");
2184 /* Compute a subset of the data dependence relation graph. Don't
2185 compute read-read relations, and avoid the computation of the
2186 opposite relation, i.e. when AB has been computed, don't compute BA.
2187 DATAREFS contains a list of data references, and the result is set
2188 in DEPENDENCE_RELATIONS. */
2190 static void
2191 compute_all_dependences (varray_type datarefs,
2192 varray_type *dependence_relations)
2194 unsigned int i, j, N;
2196 N = VARRAY_ACTIVE_SIZE (datarefs);
2198 for (i = 0; i < N; i++)
2199 for (j = i; j < N; j++)
2201 struct data_reference *a, *b;
2202 struct data_dependence_relation *ddr;
2204 a = VARRAY_GENERIC_PTR (datarefs, i);
2205 b = VARRAY_GENERIC_PTR (datarefs, j);
2206 ddr = initialize_data_dependence_relation (a, b);
2208 VARRAY_PUSH_GENERIC_PTR (*dependence_relations, ddr);
2209 compute_affine_dependence (ddr);
2210 compute_subscript_distance (ddr);
2214 /* Search the data references in LOOP, and record the information into
2215 DATAREFS. Returns chrec_dont_know when failing to analyze a
2216 difficult case, returns NULL_TREE otherwise.
2218 TODO: This function should be made smarter so that it can handle address
2219 arithmetic as if they were array accesses, etc. */
2221 tree
2222 find_data_references_in_loop (struct loop *loop, varray_type *datarefs)
2224 bool dont_know_node_not_inserted = true;
2225 basic_block bb, *bbs;
2226 unsigned int i;
2227 block_stmt_iterator bsi;
2229 bbs = get_loop_body (loop);
2231 for (i = 0; i < loop->num_nodes; i++)
2233 bb = bbs[i];
2235 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
2237 tree stmt = bsi_stmt (bsi);
2238 stmt_ann_t ann = stmt_ann (stmt);
2240 if (TREE_CODE (stmt) != MODIFY_EXPR)
2241 continue;
2243 if (!VUSE_OPS (ann)
2244 && !V_MUST_DEF_OPS (ann)
2245 && !V_MAY_DEF_OPS (ann))
2246 continue;
2248 /* In the GIMPLE representation, a modify expression
2249 contains a single load or store to memory. */
2250 if (TREE_CODE (TREE_OPERAND (stmt, 0)) == ARRAY_REF)
2251 VARRAY_PUSH_GENERIC_PTR
2252 (*datarefs, analyze_array (stmt, TREE_OPERAND (stmt, 0),
2253 false));
2255 else if (TREE_CODE (TREE_OPERAND (stmt, 1)) == ARRAY_REF)
2256 VARRAY_PUSH_GENERIC_PTR
2257 (*datarefs, analyze_array (stmt, TREE_OPERAND (stmt, 1),
2258 true));
2259 else
2261 if (dont_know_node_not_inserted)
2263 struct data_reference *res;
2264 res = xmalloc (sizeof (struct data_reference));
2265 DR_STMT (res) = NULL_TREE;
2266 DR_REF (res) = NULL_TREE;
2267 DR_ACCESS_FNS (res) = NULL;
2268 DR_BASE_NAME (res) = NULL;
2269 DR_IS_READ (res) = false;
2270 VARRAY_PUSH_GENERIC_PTR (*datarefs, res);
2271 dont_know_node_not_inserted = false;
2275 /* When there are no defs in the loop, the loop is parallel. */
2276 if (NUM_V_MAY_DEFS (STMT_V_MAY_DEF_OPS (stmt)) > 0
2277 || NUM_V_MUST_DEFS (STMT_V_MUST_DEF_OPS (stmt)) > 0)
2278 bb->loop_father->parallel_p = false;
2281 if (bb->loop_father->estimated_nb_iterations == NULL_TREE)
2282 compute_estimated_nb_iterations (bb->loop_father);
2285 free (bbs);
2287 return dont_know_node_not_inserted ? NULL_TREE : chrec_dont_know;
2292 /* This section contains all the entry points. */
2294 /* Given a loop nest LOOP, the following vectors are returned:
2295 *DATAREFS is initialized to all the array elements contained in this loop,
2296 *DEPENDENCE_RELATIONS contains the relations between the data references. */
2298 void
2299 compute_data_dependences_for_loop (unsigned nb_loops,
2300 struct loop *loop,
2301 varray_type *datarefs,
2302 varray_type *dependence_relations)
2304 unsigned int i;
2305 varray_type allrelations;
2307 /* If one of the data references is not computable, give up without
2308 spending time to compute other dependences. */
2309 if (find_data_references_in_loop (loop, datarefs) == chrec_dont_know)
2311 struct data_dependence_relation *ddr;
2313 /* Insert a single relation into dependence_relations:
2314 chrec_dont_know. */
2315 ddr = initialize_data_dependence_relation (NULL, NULL);
2316 VARRAY_PUSH_GENERIC_PTR (*dependence_relations, ddr);
2317 build_classic_dist_vector (ddr, nb_loops, loop->depth);
2318 build_classic_dir_vector (ddr, nb_loops, loop->depth);
2319 return;
2322 VARRAY_GENERIC_PTR_INIT (allrelations, 1, "Data dependence relations");
2323 compute_all_dependences (*datarefs, &allrelations);
2325 for (i = 0; i < VARRAY_ACTIVE_SIZE (allrelations); i++)
2327 struct data_dependence_relation *ddr;
2328 ddr = VARRAY_GENERIC_PTR (allrelations, i);
2329 if (build_classic_dist_vector (ddr, nb_loops, loop->depth))
2331 VARRAY_PUSH_GENERIC_PTR (*dependence_relations, ddr);
2332 build_classic_dir_vector (ddr, nb_loops, loop->depth);
2337 /* Entry point (for testing only). Analyze all the data references
2338 and the dependence relations.
2340 The data references are computed first.
2342 A relation on these nodes is represented by a complete graph. Some
2343 of the relations could be of no interest, thus the relations can be
2344 computed on demand.
2346 In the following function we compute all the relations. This is
2347 just a first implementation that is here for:
2348 - for showing how to ask for the dependence relations,
2349 - for the debugging the whole dependence graph,
2350 - for the dejagnu testcases and maintenance.
2352 It is possible to ask only for a part of the graph, avoiding to
2353 compute the whole dependence graph. The computed dependences are
2354 stored in a knowledge base (KB) such that later queries don't
2355 recompute the same information. The implementation of this KB is
2356 transparent to the optimizer, and thus the KB can be changed with a
2357 more efficient implementation, or the KB could be disabled. */
2359 void
2360 analyze_all_data_dependences (struct loops *loops)
2362 unsigned int i;
2363 varray_type datarefs;
2364 varray_type dependence_relations;
2365 int nb_data_refs = 10;
2367 VARRAY_GENERIC_PTR_INIT (datarefs, nb_data_refs, "datarefs");
2368 VARRAY_GENERIC_PTR_INIT (dependence_relations,
2369 nb_data_refs * nb_data_refs,
2370 "dependence_relations");
2372 /* Compute DDs on the whole function. */
2373 compute_data_dependences_for_loop (loops->num, loops->parray[0],
2374 &datarefs, &dependence_relations);
2376 if (dump_file)
2378 dump_data_dependence_relations (dump_file, dependence_relations);
2379 fprintf (dump_file, "\n\n");
2381 if (dump_flags & TDF_DETAILS)
2382 dump_dist_dir_vectors (dump_file, dependence_relations);
2384 if (dump_flags & TDF_STATS)
2386 unsigned nb_top_relations = 0;
2387 unsigned nb_bot_relations = 0;
2388 unsigned nb_basename_differ = 0;
2389 unsigned nb_chrec_relations = 0;
2391 for (i = 0; i < VARRAY_ACTIVE_SIZE (dependence_relations); i++)
2393 struct data_dependence_relation *ddr;
2394 ddr = VARRAY_GENERIC_PTR (dependence_relations, i);
2396 if (chrec_contains_undetermined (DDR_ARE_DEPENDENT (ddr)))
2397 nb_top_relations++;
2399 else if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
2401 struct data_reference *a = DDR_A (ddr);
2402 struct data_reference *b = DDR_B (ddr);
2403 bool differ_p;
2405 if (DR_NUM_DIMENSIONS (a) != DR_NUM_DIMENSIONS (b)
2406 || (array_base_name_differ_p (a, b, &differ_p) && differ_p))
2407 nb_basename_differ++;
2408 else
2409 nb_bot_relations++;
2412 else
2413 nb_chrec_relations++;
2416 gather_stats_on_scev_database ();
2420 free_dependence_relations (dependence_relations);
2421 free_data_refs (datarefs);
2424 /* Free the memory used by a data dependence relation DDR. */
2426 void
2427 free_dependence_relation (struct data_dependence_relation *ddr)
2429 if (ddr == NULL)
2430 return;
2432 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE && DDR_SUBSCRIPTS (ddr))
2433 varray_clear (DDR_SUBSCRIPTS (ddr));
2434 free (ddr);
2437 /* Free the memory used by the data dependence relations from
2438 DEPENDENCE_RELATIONS. */
2440 void
2441 free_dependence_relations (varray_type dependence_relations)
2443 unsigned int i;
2444 if (dependence_relations == NULL)
2445 return;
2447 for (i = 0; i < VARRAY_ACTIVE_SIZE (dependence_relations); i++)
2448 free_dependence_relation (VARRAY_GENERIC_PTR (dependence_relations, i));
2449 varray_clear (dependence_relations);
2452 /* Free the memory used by the data references from DATAREFS. */
2454 void
2455 free_data_refs (varray_type datarefs)
2457 unsigned int i;
2459 if (datarefs == NULL)
2460 return;
2462 for (i = 0; i < VARRAY_ACTIVE_SIZE (datarefs); i++)
2464 struct data_reference *dr = (struct data_reference *)
2465 VARRAY_GENERIC_PTR (datarefs, i);
2466 if (dr)
2468 if (DR_ACCESS_FNS (dr))
2469 varray_clear (DR_ACCESS_FNS (dr));
2470 free (dr);
2473 varray_clear (datarefs);