2004-07-28 Eric Christopher <echristo@redhat.com>
[official-gcc.git] / gcc / tree-data-ref.c
blob1f14bcb011d32685fbdca7361bb8b4b5204d3776
1 /* Data references and dependences detectors.
2 Copyright (C) 2003, 2004 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 dependeces
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"
97 #include "lambda.h"
99 static unsigned int data_ref_id = 0;
103 /* Returns true iff A divides B. */
105 static inline bool
106 tree_fold_divides_p (tree type,
107 tree a,
108 tree b)
110 if (integer_onep (a))
111 return true;
113 /* Determines whether (A == gcd (A, B)). */
114 return integer_zerop
115 (fold (build (MINUS_EXPR, type, a, tree_fold_gcd (a, b))));
118 /* Bezout: Let A1 and A2 be two integers; there exist two integers U11
119 and U12 such that,
121 | U11 * A1 + U12 * A2 = gcd (A1, A2).
123 This function computes the greatest common divisor using the
124 Blankinship algorithm. The gcd is returned, and the coefficients
125 of the unimodular matrix U are (U11, U12, U21, U22) such that,
127 | U.A = S
129 | (U11 U12) (A1) = (gcd)
130 | (U21 U22) (A2) (0)
132 FIXME: Use lambda_..._hermite for implementing this function.
135 static tree
136 tree_fold_bezout (tree a1,
137 tree a2,
138 tree *u11, tree *u12,
139 tree *u21, tree *u22)
141 tree s1, s2;
143 /* Initialize S with the coefficients of A. */
144 s1 = a1;
145 s2 = a2;
147 /* Initialize the U matrix */
148 *u11 = integer_one_node;
149 *u12 = integer_zero_node;
150 *u21 = integer_zero_node;
151 *u22 = integer_one_node;
153 if (integer_zerop (a1)
154 || integer_zerop (a2))
155 return integer_zero_node;
157 while (!integer_zerop (s2))
159 int sign;
160 tree z, zu21, zu22, zs2;
162 sign = tree_int_cst_sgn (s1) * tree_int_cst_sgn (s2);
163 z = fold (build (FLOOR_DIV_EXPR, integer_type_node,
164 fold (build1 (ABS_EXPR, integer_type_node, s1)),
165 fold (build1 (ABS_EXPR, integer_type_node, s2))));
166 zu21 = fold (build (MULT_EXPR, integer_type_node, z, *u21));
167 zu22 = fold (build (MULT_EXPR, integer_type_node, z, *u22));
168 zs2 = fold (build (MULT_EXPR, integer_type_node, z, s2));
170 /* row1 -= z * row2. */
171 if (sign < 0)
173 *u11 = fold (build (PLUS_EXPR, integer_type_node, *u11, zu21));
174 *u12 = fold (build (PLUS_EXPR, integer_type_node, *u12, zu22));
175 s1 = fold (build (PLUS_EXPR, integer_type_node, s1, zs2));
177 else if (sign > 0)
179 *u11 = fold (build (MINUS_EXPR, integer_type_node, *u11, zu21));
180 *u12 = fold (build (MINUS_EXPR, integer_type_node, *u12, zu22));
181 s1 = fold (build (MINUS_EXPR, integer_type_node, s1, zs2));
183 else
184 /* Should not happen. */
185 abort ();
187 /* Interchange row1 and row2. */
189 tree flip;
191 flip = *u11;
192 *u11 = *u21;
193 *u21 = flip;
195 flip = *u12;
196 *u12 = *u22;
197 *u22 = flip;
199 flip = s1;
200 s1 = s2;
201 s2 = flip;
205 if (tree_int_cst_sgn (s1) < 0)
207 *u11 = fold (build (MULT_EXPR, integer_type_node, *u11,
208 integer_minus_one_node));
209 *u12 = fold (build (MULT_EXPR, integer_type_node, *u12,
210 integer_minus_one_node));
211 s1 = fold (build (MULT_EXPR, integer_type_node, s1, integer_minus_one_node));
214 return s1;
219 /* Dump into FILE all the data references from DATAREFS. */
221 void
222 dump_data_references (FILE *file,
223 varray_type datarefs)
225 unsigned int i;
227 for (i = 0; i < VARRAY_ACTIVE_SIZE (datarefs); i++)
228 dump_data_reference (file, VARRAY_GENERIC_PTR (datarefs, i));
231 /* Dump into FILE all the dependence relations from DDR. */
233 void
234 dump_data_dependence_relations (FILE *file,
235 varray_type ddr)
237 unsigned int i;
239 for (i = 0; i < VARRAY_ACTIVE_SIZE (ddr); i++)
240 dump_data_dependence_relation (file, VARRAY_GENERIC_PTR (ddr, i));
243 /* Dump function for a DATA_REFERENCE structure. */
245 void
246 dump_data_reference (FILE *outf,
247 struct data_reference *dr)
249 unsigned int i;
251 fprintf (outf, "(Data Ref %d: \n stmt: ", DR_ID (dr));
252 print_generic_stmt (outf, DR_STMT (dr), 0);
253 fprintf (outf, " ref: ");
254 print_generic_stmt (outf, DR_REF (dr), 0);
255 fprintf (outf, " base_name: ");
256 print_generic_stmt (outf, DR_BASE_NAME (dr), 0);
258 for (i = 0; i < DR_NUM_DIMENSIONS (dr); i++)
260 fprintf (outf, " Access function %d: ", i);
261 print_generic_stmt (outf, DR_ACCESS_FN (dr, i), 0);
263 fprintf (outf, ")\n");
266 /* Dump function for a DATA_DEPENDENCE_RELATION structure. */
268 void
269 dump_data_dependence_relation (FILE *outf,
270 struct data_dependence_relation *ddr)
272 unsigned int i;
273 struct data_reference *dra, *drb;
275 dra = DDR_A (ddr);
276 drb = DDR_B (ddr);
278 if (dra && drb)
279 fprintf (outf, "(Data Dep (A = %d, B = %d):", DR_ID (dra), DR_ID (drb));
280 else
281 fprintf (outf, "(Data Dep:");
283 if (chrec_contains_undetermined (DDR_ARE_DEPENDENT (ddr)))
284 fprintf (outf, " (don't know)\n");
286 else if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
287 fprintf (outf, " (no dependence)\n");
289 else
291 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
293 tree chrec;
294 struct subscript *subscript = DDR_SUBSCRIPT (ddr, i);
296 fprintf (outf, "\n (subscript %d:\n", i);
297 fprintf (outf, " access_fn_A: ");
298 print_generic_stmt (outf, DR_ACCESS_FN (dra, i), 0);
299 fprintf (outf, " access_fn_B: ");
300 print_generic_stmt (outf, DR_ACCESS_FN (drb, i), 0);
302 chrec = SUB_CONFLICTS_IN_A (subscript);
303 fprintf (outf, " iterations_that_access_an_element_twice_in_A: ");
304 print_generic_stmt (outf, chrec, 0);
305 if (chrec == chrec_known)
306 fprintf (outf, " (no dependence)\n");
307 else if (chrec_contains_undetermined (chrec))
308 fprintf (outf, " (don't know)\n");
309 else
311 tree last_iteration = SUB_LAST_CONFLICT_IN_A (subscript);
312 fprintf (outf, " last_iteration_that_access_an_element_twice_in_A: ");
313 print_generic_stmt (outf, last_iteration, 0);
316 chrec = SUB_CONFLICTS_IN_B (subscript);
317 fprintf (outf, " iterations_that_access_an_element_twice_in_B: ");
318 print_generic_stmt (outf, chrec, 0);
319 if (chrec == chrec_known)
320 fprintf (outf, " (no dependence)\n");
321 else if (chrec_contains_undetermined (chrec))
322 fprintf (outf, " (don't know)\n");
323 else
325 tree last_iteration = SUB_LAST_CONFLICT_IN_B (subscript);
326 fprintf (outf, " last_iteration_that_access_an_element_twice_in_B: ");
327 print_generic_stmt (outf, last_iteration, 0);
330 fprintf (outf, " )\n");
333 fprintf (outf, " (Distance Vector: \n");
334 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
336 struct subscript *subscript = DDR_SUBSCRIPT (ddr, i);
338 fprintf (outf, "(");
339 print_generic_stmt (outf, SUB_DISTANCE (subscript), 0);
340 fprintf (outf, ")\n");
342 fprintf (outf, " )\n");
345 fprintf (outf, ")\n");
350 /* Dump function for a DATA_DEPENDENCE_DIRECTION structure. */
352 void
353 dump_data_dependence_direction (FILE *file,
354 enum data_dependence_direction dir)
356 switch (dir)
358 case dir_positive:
359 fprintf (file, "+");
360 break;
362 case dir_negative:
363 fprintf (file, "-");
364 break;
366 case dir_equal:
367 fprintf (file, "=");
368 break;
370 case dir_positive_or_negative:
371 fprintf (file, "+-");
372 break;
374 case dir_positive_or_equal:
375 fprintf (file, "+=");
376 break;
378 case dir_negative_or_equal:
379 fprintf (file, "-=");
380 break;
382 case dir_star:
383 fprintf (file, "*");
384 break;
386 default:
387 break;
393 /* Given an ARRAY_REF node REF, records its access functions.
394 Example: given A[i][3], record in ACCESS_FNS the opnd1 function,
395 ie. the constant "3", then recursively call the function on opnd0,
396 ie. the ARRAY_REF "A[i]". The function returns the base name:
397 "A". */
399 static tree
400 analyze_array_indexes (struct loop *loop,
401 varray_type access_fns,
402 tree ref)
404 tree opnd0, opnd1;
405 tree access_fn;
407 opnd0 = TREE_OPERAND (ref, 0);
408 opnd1 = TREE_OPERAND (ref, 1);
410 /* The detection of the evolution function for this data access is
411 postponed until the dependence test. This lazy strategy avoids
412 the computation of access functions that are of no interest for
413 the optimizers. */
414 access_fn = instantiate_parameters
415 (loop, analyze_scalar_evolution (loop, opnd1));
417 VARRAY_PUSH_TREE (access_fns, access_fn);
419 /* Recursively record other array access functions. */
420 if (TREE_CODE (opnd0) == ARRAY_REF)
421 return analyze_array_indexes (loop, access_fns, opnd0);
423 /* Return the base name of the data access. */
424 else
425 return opnd0;
428 /* For a data reference REF contained in the statemet STMT, initialize
429 a DATA_REFERENCE structure, and return it. IS_READ flag has to be
430 set to true when REF is in the right hand side of an
431 assignment. */
433 struct data_reference *
434 analyze_array (tree stmt, tree ref, bool is_read)
436 struct data_reference *res;
438 if (dump_file && (dump_flags & TDF_DETAILS))
440 fprintf (dump_file, "(analyze_array \n");
441 fprintf (dump_file, " (ref = ");
442 print_generic_stmt (dump_file, ref, 0);
443 fprintf (dump_file, ")\n");
446 res = ggc_alloc (sizeof (struct data_reference));
448 DR_ID (res) = data_ref_id++;
449 DR_STMT (res) = stmt;
450 DR_REF (res) = ref;
451 VARRAY_TREE_INIT (DR_ACCESS_FNS (res), 3, "access_fns");
452 DR_BASE_NAME (res) = analyze_array_indexes
453 (loop_containing_stmt (stmt), DR_ACCESS_FNS (res), ref);
454 DR_IS_READ (res) = is_read;
456 if (dump_file && (dump_flags & TDF_DETAILS))
457 fprintf (dump_file, ")\n");
459 return res;
462 /* For a data reference REF contained in the statemet STMT, initialize
463 a DATA_REFERENCE structure, and return it. */
465 struct data_reference *
466 init_data_ref (tree stmt,
467 tree ref,
468 tree base,
469 tree access_fn)
471 struct data_reference *res;
473 if (dump_file && (dump_flags & TDF_DETAILS))
475 fprintf (dump_file, "(init_data_ref \n");
476 fprintf (dump_file, " (ref = ");
477 print_generic_stmt (dump_file, ref, 0);
478 fprintf (dump_file, ")\n");
481 res = ggc_alloc (sizeof (struct data_reference));
483 DR_ID (res) = data_ref_id++;
484 DR_STMT (res) = stmt;
485 DR_REF (res) = ref;
486 VARRAY_TREE_INIT (DR_ACCESS_FNS (res), 5, "access_fns");
487 DR_BASE_NAME (res) = base;
488 VARRAY_PUSH_TREE (DR_ACCESS_FNS (res), access_fn);
490 if (dump_file && (dump_flags & TDF_DETAILS))
491 fprintf (dump_file, ")\n");
493 return res;
498 /* When there exists a dependence relation, determine its distance
499 vector. */
501 static void
502 compute_distance_vector (struct data_dependence_relation *ddr)
504 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
506 unsigned int i;
508 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
510 tree conflicts_a, conflicts_b, difference;
511 struct subscript *subscript;
513 subscript = DDR_SUBSCRIPT (ddr, i);
514 conflicts_a = SUB_CONFLICTS_IN_A (subscript);
515 conflicts_b = SUB_CONFLICTS_IN_B (subscript);
516 difference = chrec_fold_minus
517 (integer_type_node, conflicts_b, conflicts_a);
519 if (evolution_function_is_constant_p (difference))
520 SUB_DISTANCE (subscript) = difference;
522 else
523 SUB_DISTANCE (subscript) = chrec_dont_know;
528 /* Initialize a ddr. */
530 struct data_dependence_relation *
531 initialize_data_dependence_relation (struct data_reference *a,
532 struct data_reference *b)
534 struct data_dependence_relation *res;
536 res = ggc_alloc (sizeof (struct data_dependence_relation));
537 DDR_A (res) = a;
538 DDR_B (res) = b;
540 if (a == NULL || b == NULL
541 || DR_BASE_NAME (a) == NULL_TREE
542 || DR_BASE_NAME (b) == NULL_TREE)
543 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
545 /* When the dimensions of A and B differ, we directly initialize
546 the relation to "there is no dependence": chrec_known. */
547 else if (DR_NUM_DIMENSIONS (a) != DR_NUM_DIMENSIONS (b)
548 || array_base_name_differ_p (a, b))
549 DDR_ARE_DEPENDENT (res) = chrec_known;
551 else
553 unsigned int i;
554 DDR_ARE_DEPENDENT (res) = NULL_TREE;
555 DDR_SUBSCRIPTS_VECTOR_INIT (res, DR_NUM_DIMENSIONS (a));
557 for (i = 0; i < DR_NUM_DIMENSIONS (a); i++)
559 struct subscript *subscript;
561 subscript = ggc_alloc (sizeof (struct subscript));
562 SUB_CONFLICTS_IN_A (subscript) = chrec_dont_know;
563 SUB_CONFLICTS_IN_B (subscript) = chrec_dont_know;
564 SUB_LAST_CONFLICT_IN_A (subscript) = chrec_dont_know;
565 SUB_LAST_CONFLICT_IN_B (subscript) = chrec_dont_know;
566 SUB_DISTANCE (subscript) = chrec_dont_know;
567 SUB_DIRECTION (subscript) = dir_star;
568 VARRAY_PUSH_GENERIC_PTR (DDR_SUBSCRIPTS (res), subscript);
572 return res;
575 /* Set DDR_ARE_DEPENDENT to CHREC and finalize the subscript overlap
576 description. */
578 static inline void
579 finalize_ddr_dependent (struct data_dependence_relation *ddr,
580 tree chrec)
582 DDR_ARE_DEPENDENT (ddr) = chrec;
583 varray_clear (DDR_SUBSCRIPTS (ddr));
588 /* This section contains the classic Banerjee tests. */
590 /* Returns true iff CHREC_A and CHREC_B are not dependent on any index
591 variables, i.e., if the ZIV (Zero Index Variable) test is true. */
593 static inline bool
594 ziv_subscript_p (tree chrec_a,
595 tree chrec_b)
597 return (evolution_function_is_constant_p (chrec_a)
598 && evolution_function_is_constant_p (chrec_b));
601 /* Returns true iff CHREC_A and CHREC_B are dependent on an index
602 variable, i.e., if the SIV (Single Index Variable) test is true. */
604 static bool
605 siv_subscript_p (tree chrec_a,
606 tree chrec_b)
608 if ((evolution_function_is_constant_p (chrec_a)
609 && evolution_function_is_univariate_p (chrec_b))
610 || (evolution_function_is_constant_p (chrec_b)
611 && evolution_function_is_univariate_p (chrec_a)))
612 return true;
614 if (evolution_function_is_univariate_p (chrec_a)
615 && evolution_function_is_univariate_p (chrec_b))
617 switch (TREE_CODE (chrec_a))
619 case POLYNOMIAL_CHREC:
620 switch (TREE_CODE (chrec_b))
622 case POLYNOMIAL_CHREC:
623 if (CHREC_VARIABLE (chrec_a) != CHREC_VARIABLE (chrec_b))
624 return false;
626 default:
627 return true;
630 default:
631 return true;
635 return false;
638 /* Analyze a ZIV (Zero Index Variable) subscript. *OVERLAPS_A and
639 *OVERLAPS_B are initialized to the functions that describe the
640 relation between the elements accessed twice by CHREC_A and
641 CHREC_B. For k >= 0, the following property is verified:
643 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
645 static void
646 analyze_ziv_subscript (tree chrec_a,
647 tree chrec_b,
648 tree *overlaps_a,
649 tree *overlaps_b)
651 tree difference;
653 if (dump_file && (dump_flags & TDF_DETAILS))
654 fprintf (dump_file, "(analyze_ziv_subscript \n");
656 difference = chrec_fold_minus (integer_type_node, chrec_a, chrec_b);
658 switch (TREE_CODE (difference))
660 case INTEGER_CST:
661 if (integer_zerop (difference))
663 /* The difference is equal to zero: the accessed index
664 overlaps for each iteration in the loop. */
665 *overlaps_a = integer_zero_node;
666 *overlaps_b = integer_zero_node;
668 else
670 /* The accesses do not overlap. */
671 *overlaps_a = chrec_known;
672 *overlaps_b = chrec_known;
674 break;
676 default:
677 /* We're not sure whether the indexes overlap. For the moment,
678 conservatively answer "don't know". */
679 *overlaps_a = chrec_dont_know;
680 *overlaps_b = chrec_dont_know;
681 break;
684 if (dump_file && (dump_flags & TDF_DETAILS))
685 fprintf (dump_file, ")\n");
688 /* Analyze a SIV (Single Index Variable) subscript where CHREC_A is a
689 constant, and CHREC_B is an affine function. *OVERLAPS_A and
690 *OVERLAPS_B are initialized to the functions that describe the
691 relation between the elements accessed twice by CHREC_A and
692 CHREC_B. For k >= 0, the following property is verified:
694 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
696 static void
697 analyze_siv_subscript_cst_affine (tree chrec_a,
698 tree chrec_b,
699 tree *overlaps_a,
700 tree *overlaps_b)
702 bool value0, value1, value2;
703 tree difference = chrec_fold_minus
704 (integer_type_node, CHREC_LEFT (chrec_b), chrec_a);
706 if (!chrec_is_positive (initial_condition (difference), &value0))
708 *overlaps_a = chrec_dont_know;
709 *overlaps_b = chrec_dont_know;
710 return;
712 else
714 if (value0 == false)
716 if (!chrec_is_positive (CHREC_RIGHT (chrec_b), &value1))
718 *overlaps_a = chrec_dont_know;
719 *overlaps_b = chrec_dont_know;
720 return;
722 else
724 if (value1 == true)
726 /* Example:
727 chrec_a = 12
728 chrec_b = {10, +, 1}
731 if (tree_fold_divides_p
732 (integer_type_node, CHREC_RIGHT (chrec_b), difference))
734 *overlaps_a = integer_zero_node;
735 *overlaps_b = fold
736 (build (EXACT_DIV_EXPR, integer_type_node,
737 fold (build1 (ABS_EXPR, integer_type_node, difference)),
738 CHREC_RIGHT (chrec_b)));
739 return;
742 /* When the step does not divides the difference, there are
743 no overlaps. */
744 else
746 *overlaps_a = chrec_known;
747 *overlaps_b = chrec_known;
748 return;
752 else
754 /* Example:
755 chrec_a = 12
756 chrec_b = {10, +, -1}
758 In this case, chrec_a will not overlap with chrec_b. */
759 *overlaps_a = chrec_known;
760 *overlaps_b = chrec_known;
761 return;
765 else
767 if (!chrec_is_positive (CHREC_RIGHT (chrec_b), &value2))
769 *overlaps_a = chrec_dont_know;
770 *overlaps_b = chrec_dont_know;
771 return;
773 else
775 if (value2 == false)
777 /* Example:
778 chrec_a = 3
779 chrec_b = {10, +, -1}
781 if (tree_fold_divides_p
782 (integer_type_node, CHREC_RIGHT (chrec_b), difference))
784 *overlaps_a = integer_zero_node;
785 *overlaps_b = fold
786 (build (EXACT_DIV_EXPR, integer_type_node, difference,
787 CHREC_RIGHT (chrec_b)));
788 return;
791 /* When the step does not divides the difference, there
792 are no overlaps. */
793 else
795 *overlaps_a = chrec_known;
796 *overlaps_b = chrec_known;
797 return;
800 else
802 /* Example:
803 chrec_a = 3
804 chrec_b = {4, +, 1}
806 In this case, chrec_a will not overlap with chrec_b. */
807 *overlaps_a = chrec_known;
808 *overlaps_b = chrec_known;
809 return;
816 /* Analyze a SIV (Single Index Variable) subscript where CHREC_A is an
817 affine function, and CHREC_B is a constant. *OVERLAPS_A and
818 *OVERLAPS_B are initialized to the functions that describe the
819 relation between the elements accessed twice by CHREC_A and
820 CHREC_B. For k >= 0, the following property is verified:
822 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
824 static void
825 analyze_siv_subscript_affine_cst (tree chrec_a,
826 tree chrec_b,
827 tree *overlaps_a,
828 tree *overlaps_b)
830 analyze_siv_subscript_cst_affine (chrec_b, chrec_a, overlaps_b, overlaps_a);
833 /* Determines the overlapping elements due to accesses CHREC_A and
834 CHREC_B, that are affine functions. This is a part of the
835 subscript analyzer. */
837 static void
838 analyze_subscript_affine_affine (tree chrec_a,
839 tree chrec_b,
840 tree *overlaps_a,
841 tree *overlaps_b)
843 tree left_a, left_b, right_a, right_b;
845 if (dump_file && (dump_flags & TDF_DETAILS))
846 fprintf (dump_file, "(analyze_subscript_affine_affine \n");
848 /* For determining the initial intersection, we have to solve a
849 Diophantine equation. This is the most time consuming part.
851 For answering to the question: "Is there a dependence?" we have
852 to prove that there exists a solution to the Diophantine
853 equation, and that the solution is in the iteration domain,
854 ie. the solution is positive or zero, and that the solution
855 happens before the upper bound loop.nb_iterations. Otherwise
856 there is no dependence. This function outputs a description of
857 the iterations that hold the intersections. */
859 left_a = CHREC_LEFT (chrec_a);
860 left_b = CHREC_LEFT (chrec_b);
861 right_a = CHREC_RIGHT (chrec_a);
862 right_b = CHREC_RIGHT (chrec_b);
864 if (chrec_zerop (chrec_fold_minus (integer_type_node, left_a, left_b)))
866 /* The first element accessed twice is on the first
867 iteration. */
868 *overlaps_a = build_polynomial_chrec
869 (CHREC_VARIABLE (chrec_b), integer_zero_node, integer_one_node);
870 *overlaps_b = build_polynomial_chrec
871 (CHREC_VARIABLE (chrec_a), integer_zero_node, integer_one_node);
874 else if (TREE_CODE (left_a) == INTEGER_CST
875 && TREE_CODE (left_b) == INTEGER_CST
876 && TREE_CODE (right_a) == INTEGER_CST
877 && TREE_CODE (right_b) == INTEGER_CST
879 /* Both functions should have the same evolution sign. */
880 && ((tree_int_cst_sgn (right_a) > 0
881 && tree_int_cst_sgn (right_b) > 0)
882 || (tree_int_cst_sgn (right_a) < 0
883 && tree_int_cst_sgn (right_b) < 0)))
885 /* Here we have to solve the Diophantine equation. Reference
886 book: "Loop Transformations for Restructuring Compilers - The
887 Foundations" by Utpal Banerjee, pages 59-80.
889 ALPHA * X0 = BETA * Y0 + GAMMA.
891 with:
892 ALPHA = RIGHT_A
893 BETA = RIGHT_B
894 GAMMA = LEFT_B - LEFT_A
895 CHREC_A = {LEFT_A, +, RIGHT_A}
896 CHREC_B = {LEFT_B, +, RIGHT_B}
898 The Diophantine equation has a solution if and only if gcd
899 (ALPHA, BETA) divides GAMMA. This is commonly known under
900 the name of the "gcd-test".
902 tree alpha, beta, gamma;
903 tree la, lb;
904 tree gcd_alpha_beta;
905 tree u11, u12, u21, u22;
907 /* Both alpha and beta have to be integer_type_node. The gcd
908 function does not work correctly otherwise. */
909 alpha = copy_node (right_a);
910 beta = copy_node (right_b);
911 la = copy_node (left_a);
912 lb = copy_node (left_b);
913 TREE_TYPE (alpha) = integer_type_node;
914 TREE_TYPE (beta) = integer_type_node;
915 TREE_TYPE (la) = integer_type_node;
916 TREE_TYPE (lb) = integer_type_node;
918 gamma = fold (build (MINUS_EXPR, integer_type_node, lb, la));
920 /* FIXME: Use lambda_*_Hermite for implementing Bezout. */
921 gcd_alpha_beta = tree_fold_bezout
922 (alpha,
923 fold (build (MULT_EXPR, integer_type_node, beta,
924 integer_minus_one_node)),
925 &u11, &u12,
926 &u21, &u22);
928 if (dump_file && (dump_flags & TDF_DETAILS))
930 fprintf (dump_file, " (alpha = ");
931 print_generic_expr (dump_file, alpha, 0);
932 fprintf (dump_file, ")\n (beta = ");
933 print_generic_expr (dump_file, beta, 0);
934 fprintf (dump_file, ")\n (gamma = ");
935 print_generic_expr (dump_file, gamma, 0);
936 fprintf (dump_file, ")\n (gcd_alpha_beta = ");
937 print_generic_expr (dump_file, gcd_alpha_beta, 0);
938 fprintf (dump_file, ")\n");
941 /* The classic "gcd-test". */
942 if (!tree_fold_divides_p (integer_type_node, gcd_alpha_beta, gamma))
944 /* The "gcd-test" has determined that there is no integer
945 solution, ie. there is no dependence. */
946 *overlaps_a = chrec_known;
947 *overlaps_b = chrec_known;
950 else
952 /* The solutions are given by:
954 | [GAMMA/GCD_ALPHA_BETA t].[u11 u12] = [X]
955 | [u21 u22] [Y]
957 For a given integer t. Using the following variables,
959 | i0 = u11 * gamma / gcd_alpha_beta
960 | j0 = u12 * gamma / gcd_alpha_beta
961 | i1 = u21
962 | j1 = u22
964 the solutions are:
966 | x = i0 + i1 * t,
967 | y = j0 + j1 * t. */
969 tree i0, j0, i1, j1, t;
970 tree gamma_gcd;
972 /* X0 and Y0 are the first iterations for which there is a
973 dependence. X0, Y0 are two solutions of the Diophantine
974 equation: chrec_a (X0) = chrec_b (Y0). */
975 tree x0, y0;
977 /* Exact div because in this case gcd_alpha_beta divides
978 gamma. */
979 gamma_gcd = fold (build (EXACT_DIV_EXPR, integer_type_node, gamma,
980 gcd_alpha_beta));
981 i0 = fold (build (MULT_EXPR, integer_type_node, u11, gamma_gcd));
982 j0 = fold (build (MULT_EXPR, integer_type_node, u12, gamma_gcd));
983 i1 = u21;
984 j1 = u22;
986 if ((tree_int_cst_sgn (i1) == 0
987 && tree_int_cst_sgn (i0) < 0)
988 || (tree_int_cst_sgn (j1) == 0
989 && tree_int_cst_sgn (j0) < 0))
991 /* There is no solution.
992 FIXME: The case "i0 > nb_iterations, j0 > nb_iterations"
993 falls in here, but for the moment we don't look at the
994 upper bound of the iteration domain. */
995 *overlaps_a = chrec_known;
996 *overlaps_b = chrec_known;
999 else
1001 if (tree_int_cst_sgn (i1) > 0)
1003 t = fold
1004 (build (CEIL_DIV_EXPR, integer_type_node,
1005 fold (build (MULT_EXPR, integer_type_node, i0,
1006 integer_minus_one_node)),
1007 i1));
1009 if (tree_int_cst_sgn (j1) > 0)
1011 t = fold
1012 (build (MAX_EXPR, integer_type_node, t,
1013 fold (build (CEIL_DIV_EXPR, integer_type_node,
1014 fold (build
1015 (MULT_EXPR,
1016 integer_type_node, j0,
1017 integer_minus_one_node)),
1018 j1))));
1020 x0 = fold
1021 (build (PLUS_EXPR, integer_type_node, i0,
1022 fold (build
1023 (MULT_EXPR, integer_type_node, i1, t))));
1024 y0 = fold
1025 (build (PLUS_EXPR, integer_type_node, j0,
1026 fold (build
1027 (MULT_EXPR, integer_type_node, j1, t))));
1029 *overlaps_a = build_polynomial_chrec
1030 (CHREC_VARIABLE (chrec_b), x0, u21);
1031 *overlaps_b = build_polynomial_chrec
1032 (CHREC_VARIABLE (chrec_a), y0, u22);
1034 else
1036 /* FIXME: For the moment, the upper bound of the
1037 iteration domain for j is not checked. */
1038 *overlaps_a = chrec_dont_know;
1039 *overlaps_b = chrec_dont_know;
1043 else
1045 /* FIXME: For the moment, the upper bound of the
1046 iteration domain for i is not checked. */
1047 *overlaps_a = chrec_dont_know;
1048 *overlaps_b = chrec_dont_know;
1054 else
1056 /* For the moment, "don't know". */
1057 *overlaps_a = chrec_dont_know;
1058 *overlaps_b = chrec_dont_know;
1061 if (dump_file && (dump_flags & TDF_DETAILS))
1063 fprintf (dump_file, " (overlaps_a = ");
1064 print_generic_expr (dump_file, *overlaps_a, 0);
1065 fprintf (dump_file, ")\n (overlaps_b = ");
1066 print_generic_expr (dump_file, *overlaps_b, 0);
1067 fprintf (dump_file, ")\n");
1070 if (dump_file && (dump_flags & TDF_DETAILS))
1071 fprintf (dump_file, ")\n");
1074 /* Analyze a SIV (Single Index Variable) subscript. *OVERLAPS_A and
1075 *OVERLAPS_B are initialized to the functions that describe the
1076 relation between the elements accessed twice by CHREC_A and
1077 CHREC_B. For k >= 0, the following property is verified:
1079 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
1081 static void
1082 analyze_siv_subscript (tree chrec_a,
1083 tree chrec_b,
1084 tree *overlaps_a,
1085 tree *overlaps_b)
1087 if (dump_file && (dump_flags & TDF_DETAILS))
1088 fprintf (dump_file, "(analyze_siv_subscript \n");
1090 if (evolution_function_is_constant_p (chrec_a)
1091 && evolution_function_is_affine_p (chrec_b))
1092 analyze_siv_subscript_cst_affine (chrec_a, chrec_b,
1093 overlaps_a, overlaps_b);
1095 else if (evolution_function_is_affine_p (chrec_a)
1096 && evolution_function_is_constant_p (chrec_b))
1097 analyze_siv_subscript_affine_cst (chrec_a, chrec_b,
1098 overlaps_a, overlaps_b);
1100 else if (evolution_function_is_affine_p (chrec_a)
1101 && evolution_function_is_affine_p (chrec_b)
1102 && (CHREC_VARIABLE (chrec_a) == CHREC_VARIABLE (chrec_b)))
1103 analyze_subscript_affine_affine (chrec_a, chrec_b,
1104 overlaps_a, overlaps_b);
1105 else
1107 *overlaps_a = chrec_dont_know;
1108 *overlaps_b = chrec_dont_know;
1111 if (dump_file && (dump_flags & TDF_DETAILS))
1112 fprintf (dump_file, ")\n");
1115 /* Return true when the evolution steps of an affine CHREC divide the
1116 constant CST. */
1118 static bool
1119 chrec_steps_divide_constant_p (tree chrec,
1120 tree cst)
1122 switch (TREE_CODE (chrec))
1124 case POLYNOMIAL_CHREC:
1125 return (tree_fold_divides_p (integer_type_node, CHREC_RIGHT (chrec), cst)
1126 && chrec_steps_divide_constant_p (CHREC_LEFT (chrec), cst));
1128 default:
1129 /* On the initial condition, return true. */
1130 return true;
1134 /* Analyze a MIV (Multiple Index Variable) subscript. *OVERLAPS_A and
1135 *OVERLAPS_B are initialized to the functions that describe the
1136 relation between the elements accessed twice by CHREC_A and
1137 CHREC_B. For k >= 0, the following property is verified:
1139 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
1141 static void
1142 analyze_miv_subscript (tree chrec_a,
1143 tree chrec_b,
1144 tree *overlaps_a,
1145 tree *overlaps_b)
1147 /* FIXME: This is a MIV subscript, not yet handled.
1148 Example: (A[{1, +, 1}_1] vs. A[{1, +, 1}_2]) that comes from
1149 (A[i] vs. A[j]).
1151 In the SIV test we had to solve a Diophantine equation with two
1152 variables. In the MIV case we have to solve a Diophantine
1153 equation with 2*n variables (if the subscript uses n IVs).
1155 tree difference;
1157 if (dump_file && (dump_flags & TDF_DETAILS))
1158 fprintf (dump_file, "(analyze_miv_subscript \n");
1160 difference = chrec_fold_minus (integer_type_node, chrec_a, chrec_b);
1162 if (chrec_zerop (difference))
1164 /* Access functions are the same: all the elements are accessed
1165 in the same order. */
1166 *overlaps_a = integer_zero_node;
1167 *overlaps_b = integer_zero_node;
1170 else if (evolution_function_is_constant_p (difference)
1171 /* For the moment, the following is verified:
1172 evolution_function_is_affine_multivariate_p (chrec_a) */
1173 && !chrec_steps_divide_constant_p (chrec_a, difference))
1175 /* testsuite/.../ssa-chrec-33.c
1176 {{21, +, 2}_1, +, -2}_2 vs. {{20, +, 2}_1, +, -2}_2
1178 The difference is 1, and the evolution steps are equal to 2,
1179 consequently there are no overlapping elements. */
1180 *overlaps_a = chrec_known;
1181 *overlaps_b = chrec_known;
1184 else if (evolution_function_is_univariate_p (chrec_a)
1185 && evolution_function_is_univariate_p (chrec_b))
1187 /* testsuite/.../ssa-chrec-35.c
1188 {0, +, 1}_2 vs. {0, +, 1}_3
1189 the overlapping elements are respectively located at iterations:
1190 {0, +, 1}_3 and {0, +, 1}_2.
1192 if (evolution_function_is_affine_p (chrec_a)
1193 && evolution_function_is_affine_p (chrec_b))
1194 analyze_subscript_affine_affine (chrec_a, chrec_b,
1195 overlaps_a, overlaps_b);
1196 else
1198 *overlaps_a = chrec_dont_know;
1199 *overlaps_b = chrec_dont_know;
1203 else
1205 /* When the analysis is too difficult, answer "don't know". */
1206 *overlaps_a = chrec_dont_know;
1207 *overlaps_b = chrec_dont_know;
1210 if (dump_file && (dump_flags & TDF_DETAILS))
1211 fprintf (dump_file, ")\n");
1214 /* Determines the iterations for which CHREC_A is equal to CHREC_B.
1215 OVERLAP_ITERATIONS_A and OVERLAP_ITERATIONS_B are initialized with
1216 two functions that describe the iterations that contain conflicting
1217 elements.
1219 Remark: For an integer k >= 0, the following equality is true:
1221 CHREC_A (OVERLAP_ITERATIONS_A (k)) == CHREC_B (OVERLAP_ITERATIONS_B (k)).
1224 static void
1225 analyze_overlapping_iterations (tree chrec_a,
1226 tree chrec_b,
1227 tree *overlap_iterations_a,
1228 tree *overlap_iterations_b)
1230 if (dump_file && (dump_flags & TDF_DETAILS))
1232 fprintf (dump_file, "(analyze_overlapping_iterations \n");
1233 fprintf (dump_file, " (chrec_a = ");
1234 print_generic_expr (dump_file, chrec_a, 0);
1235 fprintf (dump_file, ")\n chrec_b = ");
1236 print_generic_expr (dump_file, chrec_b, 0);
1237 fprintf (dump_file, ")\n");
1240 if (chrec_a == NULL_TREE
1241 || chrec_b == NULL_TREE
1242 || chrec_contains_undetermined (chrec_a)
1243 || chrec_contains_undetermined (chrec_b)
1244 || chrec_contains_symbols (chrec_a)
1245 || chrec_contains_symbols (chrec_b))
1247 *overlap_iterations_a = chrec_dont_know;
1248 *overlap_iterations_b = chrec_dont_know;
1251 else if (ziv_subscript_p (chrec_a, chrec_b))
1252 analyze_ziv_subscript (chrec_a, chrec_b,
1253 overlap_iterations_a, overlap_iterations_b);
1255 else if (siv_subscript_p (chrec_a, chrec_b))
1256 analyze_siv_subscript (chrec_a, chrec_b,
1257 overlap_iterations_a, overlap_iterations_b);
1259 else
1260 analyze_miv_subscript (chrec_a, chrec_b,
1261 overlap_iterations_a, overlap_iterations_b);
1263 if (dump_file && (dump_flags & TDF_DETAILS))
1265 fprintf (dump_file, " (overlap_iterations_a = ");
1266 print_generic_expr (dump_file, *overlap_iterations_a, 0);
1267 fprintf (dump_file, ")\n (overlap_iterations_b = ");
1268 print_generic_expr (dump_file, *overlap_iterations_b, 0);
1269 fprintf (dump_file, ")\n");
1275 /* This section contains the affine functions dependences detector. */
1277 /* Computes the conflicting iterations, and initialize DDR. */
1279 static void
1280 subscript_dependence_tester (struct data_dependence_relation *ddr)
1282 unsigned int i;
1283 struct data_reference *dra = DDR_A (ddr);
1284 struct data_reference *drb = DDR_B (ddr);
1286 if (dump_file && (dump_flags & TDF_DETAILS))
1287 fprintf (dump_file, "(subscript_dependence_tester \n");
1289 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
1291 tree overlaps_a, overlaps_b;
1292 struct subscript *subscript = DDR_SUBSCRIPT (ddr, i);
1294 analyze_overlapping_iterations (DR_ACCESS_FN (dra, i),
1295 DR_ACCESS_FN (drb, i),
1296 &overlaps_a, &overlaps_b);
1298 if (chrec_contains_undetermined (overlaps_a)
1299 || chrec_contains_undetermined (overlaps_b))
1301 finalize_ddr_dependent (ddr, chrec_dont_know);
1302 break;
1305 else if (overlaps_a == chrec_known
1306 || overlaps_b == chrec_known)
1308 finalize_ddr_dependent (ddr, chrec_known);
1309 break;
1312 else
1314 SUB_CONFLICTS_IN_A (subscript) = overlaps_a;
1315 SUB_CONFLICTS_IN_B (subscript) = overlaps_b;
1319 if (dump_file && (dump_flags & TDF_DETAILS))
1320 fprintf (dump_file, ")\n");
1323 /* Compute the classic per loop distance vector.
1325 RES is the data dependence relation to build a vector from.
1326 CLASSIC_DIST is the varray to place the vector in.
1327 NB_LOOPS is the total number of loops we are considering.
1328 FIRST_LOOP is the loop->num of the first loop. */
1330 static void
1331 build_classic_dist_vector (struct data_dependence_relation *res,
1332 varray_type *classic_dist,
1333 int nb_loops, unsigned int first_loop)
1335 unsigned i;
1336 lambda_vector dist_v, init_v;
1338 dist_v = lambda_vector_new (nb_loops);
1339 init_v = lambda_vector_new (nb_loops);
1340 lambda_vector_clear (dist_v, nb_loops);
1341 lambda_vector_clear (init_v, nb_loops);
1343 if (DDR_ARE_DEPENDENT (res) != NULL_TREE)
1344 return;
1346 for (i = 0; i < DDR_NUM_SUBSCRIPTS (res); i++)
1348 struct subscript *subscript = DDR_SUBSCRIPT (res, i);
1350 if (chrec_contains_undetermined (SUB_DISTANCE (subscript)))
1351 return;
1353 if (TREE_CODE (SUB_CONFLICTS_IN_A (subscript)) == POLYNOMIAL_CHREC)
1355 int dist;
1356 int loop_nb;
1357 loop_nb = CHREC_VARIABLE (SUB_CONFLICTS_IN_A (subscript));
1358 loop_nb -= first_loop;
1359 /* If the loop number is still greater than the number of
1360 loops we've been asked to analyze, or negative,
1361 something is borked. */
1362 if (loop_nb < 0 || loop_nb >= nb_loops)
1363 abort ();
1364 dist = int_cst_value (SUB_DISTANCE (subscript));
1366 /* This is the subscript coupling test.
1367 | loop i = 0, N, 1
1368 | T[i+1][i] = ...
1369 | ... = T[i][i]
1370 | endloop
1371 There is no dependence. */
1372 if (init_v[loop_nb] != 0
1373 && dist_v[loop_nb] != dist)
1375 finalize_ddr_dependent (res, chrec_known);
1376 return;
1379 dist_v[loop_nb] = dist;
1380 init_v[loop_nb] = 1;
1384 /* There is a distance of 1 on all the outer loops:
1386 Example: there is a dependence of distance 1 on loop_1 for the array A.
1387 | loop_1
1388 | A[5] = ...
1389 | endloop
1392 struct loop *lca, *loop_a, *loop_b;
1393 struct data_reference *a = DDR_A (res);
1394 struct data_reference *b = DDR_B (res);
1395 int lca_nb;
1396 loop_a = loop_containing_stmt (DR_STMT (a));
1397 loop_b = loop_containing_stmt (DR_STMT (b));
1399 /* Get the common ancestor loop. */
1400 lca = find_common_loop (loop_a, loop_b);
1402 lca_nb = lca->num;
1403 lca_nb -= first_loop;
1404 if (lca_nb < 0 || lca_nb >= nb_loops)
1405 abort ();
1406 /* For each outer loop where init_v is not set, the accesses are
1407 in dependence of distance 1 in the loop. */
1408 if (lca != loop_a
1409 && lca != loop_b
1410 && init_v[lca_nb] == 0)
1411 dist_v[lca_nb] = 1;
1413 lca = lca->outer;
1415 if (lca)
1417 lca_nb = lca->num - first_loop;
1418 while (lca->depth != 0)
1420 if (lca_nb < 0 || lca_nb >= nb_loops)
1421 abort ();
1422 if (init_v[lca_nb] == 0)
1423 dist_v[lca_nb] = 1;
1424 lca = lca->outer;
1425 lca_nb = lca->num - first_loop;
1431 VARRAY_PUSH_GENERIC_PTR (*classic_dist, dist_v);
1434 /* Compute the classic per loop direction vector.
1436 RES is the data dependence relation to build a vector from.
1437 CLASSIC_DIR is the varray to place the vector in.
1438 NB_LOOPS is the total number of loops we are considering.
1439 FIRST_LOOP is the loop->num of the first loop. */
1441 static void
1442 build_classic_dir_vector (struct data_dependence_relation *res,
1443 varray_type *classic_dir,
1444 int nb_loops, unsigned int first_loop)
1446 unsigned i;
1447 lambda_vector dir_v, init_v;
1449 dir_v = lambda_vector_new (nb_loops);
1450 init_v = lambda_vector_new (nb_loops);
1451 lambda_vector_clear (dir_v, nb_loops);
1452 lambda_vector_clear (init_v, nb_loops);
1454 if (DDR_ARE_DEPENDENT (res) != NULL_TREE)
1455 return;
1457 for (i = 0; i < DDR_NUM_SUBSCRIPTS (res); i++)
1459 struct subscript *subscript = DDR_SUBSCRIPT (res, i);
1461 if (TREE_CODE (SUB_CONFLICTS_IN_A (subscript)) == POLYNOMIAL_CHREC
1462 && TREE_CODE (SUB_CONFLICTS_IN_B (subscript)) == POLYNOMIAL_CHREC)
1464 int loop_nb;
1466 enum data_dependence_direction dir = dir_star;
1467 loop_nb = CHREC_VARIABLE (SUB_CONFLICTS_IN_A (subscript));
1468 loop_nb -= first_loop;
1470 /* If the loop number is still greater than the number of
1471 loops we've been asked to analyze, or negative,
1472 something is borked. */
1473 if (loop_nb < 0 || loop_nb >= nb_loops)
1474 abort ();
1475 if (chrec_contains_undetermined (SUB_DISTANCE (subscript)))
1479 else
1481 int dist = int_cst_value (SUB_DISTANCE (subscript));
1483 if (dist == 0)
1484 dir = dir_equal;
1485 else if (dist > 0)
1486 dir = dir_positive;
1487 else if (dist < 0)
1488 dir = dir_negative;
1491 /* This is the subscript coupling test.
1492 | loop i = 0, N, 1
1493 | T[i+1][i] = ...
1494 | ... = T[i][i]
1495 | endloop
1496 There is no dependence. */
1497 if (init_v[loop_nb] != 0
1498 && dir != dir_star
1499 && (enum data_dependence_direction) dir_v[loop_nb] != dir
1500 && (enum data_dependence_direction) dir_v[loop_nb] != dir_star)
1502 finalize_ddr_dependent (res, chrec_known);
1503 return;
1506 dir_v[loop_nb] = dir;
1507 init_v[loop_nb] = 1;
1511 /* There is a distance of 1 on all the outer loops:
1513 Example: there is a dependence of distance 1 on loop_1 for the array A.
1514 | loop_1
1515 | A[5] = ...
1516 | endloop
1519 struct loop *lca, *loop_a, *loop_b;
1520 struct data_reference *a = DDR_A (res);
1521 struct data_reference *b = DDR_B (res);
1522 int lca_nb;
1523 loop_a = loop_containing_stmt (DR_STMT (a));
1524 loop_b = loop_containing_stmt (DR_STMT (b));
1526 /* Get the common ancestor loop. */
1527 lca = find_common_loop (loop_a, loop_b);
1528 lca_nb = lca->num - first_loop;
1530 if (lca_nb < 0 || lca_nb >= nb_loops)
1531 abort ();
1532 /* For each outer loop where init_v is not set, the accesses are
1533 in dependence of distance 1 in the loop. */
1534 if (lca != loop_a
1535 && lca != loop_b
1536 && init_v[lca_nb] == 0)
1537 dir_v[lca_nb] = dir_positive;
1539 lca = lca->outer;
1540 if (lca)
1542 lca_nb = lca->num - first_loop;
1543 while (lca->depth != 0)
1545 if (lca_nb < 0 || lca_nb >= nb_loops)
1546 abort ();
1547 if (init_v[lca_nb] == 0)
1548 dir_v[lca_nb] = dir_positive;
1549 lca = lca->outer;
1550 lca_nb = lca->num - first_loop;
1556 VARRAY_PUSH_GENERIC_PTR (*classic_dir, dir_v);
1559 /* Returns true when all the access functions of A are affine or
1560 constant. */
1562 static bool
1563 access_functions_are_affine_or_constant_p (struct data_reference *a)
1565 unsigned int i;
1566 varray_type fns = DR_ACCESS_FNS (a);
1568 for (i = 0; i < VARRAY_ACTIVE_SIZE (fns); i++)
1569 if (!evolution_function_is_constant_p (VARRAY_TREE (fns, i))
1570 && !evolution_function_is_affine_multivariate_p (VARRAY_TREE (fns, i)))
1571 return false;
1573 return true;
1576 /* This computes the affine dependence relation between A and B.
1577 CHREC_KNOWN is used for representing the independence between two
1578 accesses, while CHREC_DONT_KNOW is used for representing the unknown
1579 relation.
1581 Note that it is possible to stop the computation of the dependence
1582 relation the first time we detect a CHREC_KNOWN element for a given
1583 subscript. */
1585 void
1586 compute_affine_dependence (struct data_dependence_relation *ddr)
1588 struct data_reference *dra = DDR_A (ddr);
1589 struct data_reference *drb = DDR_B (ddr);
1591 if (dump_file && (dump_flags & TDF_DETAILS))
1593 fprintf (dump_file, "(compute_affine_dependence (%d, %d)\n",
1594 DR_ID (dra), DR_ID (drb));
1595 fprintf (dump_file, " (stmt_a = \n");
1596 print_generic_expr (dump_file, DR_STMT (dra), 0);
1597 fprintf (dump_file, ")\n (stmt_b = \n");
1598 print_generic_expr (dump_file, DR_STMT (drb), 0);
1599 fprintf (dump_file, ")\n");
1602 /* Analyze only when the dependence relation is not yet known. */
1603 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
1605 if (access_functions_are_affine_or_constant_p (dra)
1606 && access_functions_are_affine_or_constant_p (drb))
1607 subscript_dependence_tester (ddr);
1609 /* As a last case, if the dependence cannot be determined, or if
1610 the dependence is considered too difficult to determine, answer
1611 "don't know". */
1612 else
1613 finalize_ddr_dependent (ddr, chrec_dont_know);
1616 if (dump_file && (dump_flags & TDF_DETAILS))
1617 fprintf (dump_file, ")\n");
1620 /* Compute a subset of the data dependence relation graph. Don't
1621 compute read-read relations, and avoid the computation of the
1622 opposite relation, ie. when AB has been computed, don't compute BA.
1623 DATAREFS contains a list of data references, and the result is set
1624 in DEPENDENCE_RELATIONS. */
1626 static void
1627 compute_rw_wr_ww_dependences (varray_type datarefs,
1628 varray_type *dependence_relations)
1630 unsigned int i, j, N;
1632 N = VARRAY_ACTIVE_SIZE (datarefs);
1634 for (i = 0; i < N; i++)
1635 for (j = i; j < N; j++)
1637 struct data_reference *a, *b;
1638 struct data_dependence_relation *ddr;
1640 a = VARRAY_GENERIC_PTR (datarefs, i);
1641 b = VARRAY_GENERIC_PTR (datarefs, j);
1643 /* Don't compute the "read-read" relations. */
1644 if (DR_IS_READ (a) && DR_IS_READ (b))
1645 continue;
1647 ddr = initialize_data_dependence_relation (a, b);
1649 VARRAY_PUSH_GENERIC_PTR (*dependence_relations, ddr);
1650 compute_affine_dependence (ddr);
1651 compute_distance_vector (ddr);
1655 /* Search the data references in LOOP, and record the information into
1656 DATAREFS. Returns chrec_dont_know when failing to analyze a
1657 difficult case, returns NULL_TREE otherwise.
1659 FIXME: This is a "dumb" walker over all the trees in the loop body.
1660 Find another technique that avoids this costly walk. This is
1661 acceptable for the moment, since this function is used only for
1662 debugging purposes. */
1664 static tree
1665 find_data_references_in_loop (struct loop *loop, varray_type *datarefs)
1667 basic_block bb;
1668 block_stmt_iterator bsi;
1670 FOR_EACH_BB (bb)
1672 if (!flow_bb_inside_loop_p (loop, bb))
1673 continue;
1675 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
1677 tree stmt = bsi_stmt (bsi);
1678 stmt_ann_t ann = stmt_ann (stmt);
1680 if (TREE_CODE (stmt) != MODIFY_EXPR)
1681 continue;
1683 if (!VUSE_OPS (ann)
1684 && !V_MUST_DEF_OPS (ann)
1685 && !V_MAY_DEF_OPS (ann))
1686 continue;
1688 /* In the GIMPLE representation, a modify expression
1689 contains a single load or store to memory. */
1690 if (TREE_CODE (TREE_OPERAND (stmt, 0)) == ARRAY_REF)
1691 VARRAY_PUSH_GENERIC_PTR
1692 (*datarefs, analyze_array (stmt, TREE_OPERAND (stmt, 0),
1693 false));
1695 else if (TREE_CODE (TREE_OPERAND (stmt, 1)) == ARRAY_REF)
1696 VARRAY_PUSH_GENERIC_PTR
1697 (*datarefs, analyze_array (stmt, TREE_OPERAND (stmt, 1),
1698 true));
1700 else
1701 return chrec_dont_know;
1705 return NULL_TREE;
1710 /* This section contains all the entry points. */
1712 /* Given a loop nest LOOP, the following vectors are returned:
1713 *DATAREFS is initialized to all the array elements contained in this loop,
1714 *DEPENDENCE_RELATIONS contains the relations between the data references,
1715 *CLASSIC_DIST contains the set of distance vectors,
1716 *CLASSIC_DIR contains the set of direction vectors. */
1718 void
1719 compute_data_dependences_for_loop (unsigned nb_loops,
1720 struct loop *loop,
1721 varray_type *datarefs,
1722 varray_type *dependence_relations,
1723 varray_type *classic_dist,
1724 varray_type *classic_dir)
1726 unsigned int i;
1728 /* If one of the data references is not computable, give up without
1729 spending time to compute other dependences. */
1730 if (find_data_references_in_loop (loop, datarefs) == chrec_dont_know)
1732 struct data_dependence_relation *ddr;
1734 /* Insert a single relation into dependence_relations:
1735 chrec_dont_know. */
1736 ddr = initialize_data_dependence_relation (NULL, NULL);
1737 VARRAY_PUSH_GENERIC_PTR (*dependence_relations, ddr);
1738 build_classic_dist_vector (ddr, classic_dist, nb_loops, loop->num);
1739 build_classic_dir_vector (ddr, classic_dir, nb_loops, loop->num);
1740 return;
1743 compute_rw_wr_ww_dependences (*datarefs, dependence_relations);
1745 for (i = 0; i < VARRAY_ACTIVE_SIZE (*dependence_relations); i++)
1747 struct data_dependence_relation *ddr;
1748 ddr = VARRAY_GENERIC_PTR (*dependence_relations, i);
1749 build_classic_dist_vector (ddr, classic_dist, nb_loops, loop->num);
1750 build_classic_dir_vector (ddr, classic_dir, nb_loops, loop->num);
1754 /* Entry point (for testing only). Analyze all the data references
1755 and the dependence relations.
1757 The data references are computed first.
1759 A relation on these nodes is represented by a complete graph. Some
1760 of the relations could be of no interest, thus the relations can be
1761 computed on demand.
1763 In the following function we compute all the relations. This is
1764 just a first implementation that is here for:
1765 - for showing how to ask for the dependence relations,
1766 - for the debugging the whole dependence graph,
1767 - for the dejagnu testcases and maintenance.
1769 It is possible to ask only for a part of the graph, avoiding to
1770 compute the whole dependence graph. The computed dependences are
1771 stored in a knowledge base (KB) such that later queries don't
1772 recompute the same information. The implementation of this KB is
1773 transparent to the optimizer, and thus the KB can be changed with a
1774 more efficient implementation, or the KB could be disabled. */
1776 void
1777 analyze_all_data_dependences (struct loops *loops)
1779 unsigned int i;
1780 varray_type datarefs;
1781 varray_type dependence_relations;
1782 varray_type classic_dist, classic_dir;
1783 int nb_data_refs = 10;
1785 VARRAY_GENERIC_PTR_INIT (classic_dist, 10, "classic_dist");
1786 VARRAY_GENERIC_PTR_INIT (classic_dir, 10, "classic_dir");
1787 VARRAY_GENERIC_PTR_INIT (datarefs, nb_data_refs, "datarefs");
1788 VARRAY_GENERIC_PTR_INIT (dependence_relations,
1789 nb_data_refs * nb_data_refs,
1790 "dependence_relations");
1792 /* Compute DDs on the whole function. */
1793 compute_data_dependences_for_loop (loops->num, loops->parray[0],
1794 &datarefs, &dependence_relations,
1795 &classic_dist, &classic_dir);
1797 if (dump_file)
1799 dump_data_dependence_relations (dump_file, dependence_relations);
1800 fprintf (dump_file, "\n\n");
1803 /* Don't dump distances in order to avoid to update the
1804 testsuite. */
1805 if (dump_file && (dump_flags & TDF_DETAILS))
1807 for (i = 0; i < VARRAY_ACTIVE_SIZE (classic_dist); i++)
1809 fprintf (dump_file, "DISTANCE_V (");
1810 print_lambda_vector (dump_file,
1811 VARRAY_GENERIC_PTR (classic_dist, i),
1812 loops->num);
1813 fprintf (dump_file, ")\n");
1815 for (i = 0; i < VARRAY_ACTIVE_SIZE (classic_dir); i++)
1817 fprintf (dump_file, "DIRECTION_V (");
1818 print_lambda_vector (dump_file,
1819 VARRAY_GENERIC_PTR (classic_dir, i),
1820 loops->num);
1821 fprintf (dump_file, ")\n");
1823 fprintf (dump_file, "\n\n");
1826 if (dump_file && (dump_flags & TDF_STATS))
1828 unsigned nb_top_relations = 0;
1829 unsigned nb_bot_relations = 0;
1830 unsigned nb_basename_differ = 0;
1831 unsigned nb_chrec_relations = 0;
1833 for (i = 0; i < VARRAY_ACTIVE_SIZE (dependence_relations); i++)
1835 struct data_dependence_relation *ddr;
1836 ddr = VARRAY_GENERIC_PTR (dependence_relations, i);
1838 if (chrec_contains_undetermined (DDR_ARE_DEPENDENT (ddr)))
1839 nb_top_relations++;
1841 else if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
1843 struct data_reference *a = DDR_A (ddr);
1844 struct data_reference *b = DDR_B (ddr);
1846 if (DR_NUM_DIMENSIONS (a) != DR_NUM_DIMENSIONS (b)
1847 || array_base_name_differ_p (a, b))
1848 nb_basename_differ++;
1849 else
1850 nb_bot_relations++;
1853 else
1854 nb_chrec_relations++;
1857 fprintf (dump_file, "\n(\n");
1858 fprintf (dump_file, "%d\tnb_top_relations\n", nb_top_relations);
1859 fprintf (dump_file, "%d\tnb_bot_relations\n", nb_bot_relations);
1860 fprintf (dump_file, "%d\tnb_basename_differ\n", nb_basename_differ);
1861 fprintf (dump_file, "%d\tnb_distance_relations\n", (int) VARRAY_ACTIVE_SIZE (classic_dist));
1862 fprintf (dump_file, "%d\tnb_chrec_relations\n", nb_chrec_relations);
1863 fprintf (dump_file, "\n)\n");
1865 gather_stats_on_scev_database ();
1868 varray_clear (dependence_relations);
1869 varray_clear (datarefs);
1870 varray_clear (classic_dist);
1871 varray_clear (classic_dir);