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
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
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
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:
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
50 - to define an interface to access this data.
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
63 has an integer solution x = 1 and y = -1.
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"
79 #include "coretypes.h"
85 /* These RTL headers are needed for basic-block.h. */
87 #include "basic-block.h"
88 #include "diagnostic.h"
89 #include "tree-flow.h"
90 #include "tree-dump.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. */
106 array_base_name_differ_p (struct data_reference
*a
,
107 struct data_reference
*b
,
110 tree base_a
= DR_BASE_NAME (a
);
111 tree base_b
= DR_BASE_NAME (b
);
112 tree ta
= TREE_TYPE (base_a
);
113 tree tb
= TREE_TYPE (base_b
);
116 /* Determine if same base. Example: for the array accesses
117 a[i], b[i] or pointer accesses *a, *b, bases are a, b. */
118 if (base_a
== base_b
)
124 /* For pointer based accesses, (*p)[i], (*q)[j], the bases are (*p)
126 if (TREE_CODE (base_a
) == INDIRECT_REF
&& TREE_CODE (base_b
) == INDIRECT_REF
127 && TREE_OPERAND (base_a
, 0) == TREE_OPERAND (base_b
, 0))
133 /* Record/union based accesses - s.a[i], t.b[j]. bases are s.a,t.b. */
134 if (TREE_CODE (base_a
) == COMPONENT_REF
&& TREE_CODE (base_b
) == COMPONENT_REF
135 && TREE_OPERAND (base_a
, 0) == TREE_OPERAND (base_b
, 0)
136 && TREE_OPERAND (base_a
, 1) == TREE_OPERAND (base_b
, 1))
143 /* Determine if different bases. */
145 /* At this point we know that base_a != base_b. However, pointer
146 accesses of the form x=(*p) and y=(*q), whose bases are p and q,
147 may still be pointing to the same base. In SSAed GIMPLE p and q will
148 be SSA_NAMES in this case. Therefore, here we check if they are
149 really two different declarations. */
150 if (TREE_CODE (base_a
) == VAR_DECL
&& TREE_CODE (base_b
) == VAR_DECL
)
156 /* Compare two record/union bases s.a and t.b: s != t or (a != b and
157 s and t are not unions). */
158 if (TREE_CODE (base_a
) == COMPONENT_REF
&& TREE_CODE (base_b
) == COMPONENT_REF
159 && ((TREE_CODE (TREE_OPERAND (base_a
, 0)) == VAR_DECL
160 && TREE_CODE (TREE_OPERAND (base_b
, 0)) == VAR_DECL
161 && TREE_OPERAND (base_a
, 0) != TREE_OPERAND (base_b
, 0))
162 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (base_a
, 0))) == RECORD_TYPE
163 && TREE_CODE (TREE_TYPE (TREE_OPERAND (base_b
, 0))) == RECORD_TYPE
164 && TREE_OPERAND (base_a
, 1) != TREE_OPERAND (base_b
, 1))))
170 /* Compare a record/union access and an array access. */
171 if ((TREE_CODE (base_a
) == VAR_DECL
172 && (TREE_CODE (base_b
) == COMPONENT_REF
173 && TREE_CODE (TREE_OPERAND (base_b
, 0)) == VAR_DECL
))
174 || (TREE_CODE (base_b
) == VAR_DECL
175 && (TREE_CODE (base_a
) == COMPONENT_REF
176 && TREE_CODE (TREE_OPERAND (base_a
, 0)) == VAR_DECL
)))
182 if (!alias_sets_conflict_p (get_alias_set (base_a
), get_alias_set (base_b
)))
188 /* An instruction writing through a restricted pointer is
189 "independent" of any instruction reading or writing through a
190 different pointer, in the same block/scope. */
191 if ((TREE_CODE (ta
) == POINTER_TYPE
&& TYPE_RESTRICT (ta
)
193 || (TREE_CODE (tb
) == POINTER_TYPE
&& TYPE_RESTRICT (tb
)
203 /* Returns true iff A divides B. */
206 tree_fold_divides_p (tree type
,
210 /* Determines whether (A == gcd (A, B)). */
212 (fold (build (MINUS_EXPR
, type
, a
, tree_fold_gcd (a
, b
))));
215 /* Compute the greatest common denominator of two numbers using
216 Euclid's algorithm. */
237 /* Returns true iff A divides B. */
240 int_divides_p (int a
, int b
)
242 return ((b
% a
) == 0);
247 /* Dump into FILE all the data references from DATAREFS. */
250 dump_data_references (FILE *file
,
251 varray_type datarefs
)
255 for (i
= 0; i
< VARRAY_ACTIVE_SIZE (datarefs
); i
++)
256 dump_data_reference (file
, VARRAY_GENERIC_PTR (datarefs
, i
));
259 /* Dump into FILE all the dependence relations from DDR. */
262 dump_data_dependence_relations (FILE *file
,
267 for (i
= 0; i
< VARRAY_ACTIVE_SIZE (ddr
); i
++)
268 dump_data_dependence_relation (file
, VARRAY_GENERIC_PTR (ddr
, i
));
271 /* Dump function for a DATA_REFERENCE structure. */
274 dump_data_reference (FILE *outf
,
275 struct data_reference
*dr
)
279 fprintf (outf
, "(Data Ref: \n stmt: ");
280 print_generic_stmt (outf
, DR_STMT (dr
), 0);
281 fprintf (outf
, " ref: ");
282 print_generic_stmt (outf
, DR_REF (dr
), 0);
283 fprintf (outf
, " base_name: ");
284 print_generic_stmt (outf
, DR_BASE_NAME (dr
), 0);
286 for (i
= 0; i
< DR_NUM_DIMENSIONS (dr
); i
++)
288 fprintf (outf
, " Access function %d: ", i
);
289 print_generic_stmt (outf
, DR_ACCESS_FN (dr
, i
), 0);
291 fprintf (outf
, ")\n");
294 /* Dump function for a SUBSCRIPT structure. */
297 dump_subscript (FILE *outf
, struct subscript
*subscript
)
299 tree chrec
= SUB_CONFLICTS_IN_A (subscript
);
301 fprintf (outf
, "\n (subscript \n");
302 fprintf (outf
, " iterations_that_access_an_element_twice_in_A: ");
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");
310 tree last_iteration
= SUB_LAST_CONFLICT (subscript
);
311 fprintf (outf
, " last_conflict: ");
312 print_generic_stmt (outf
, last_iteration
, 0);
315 chrec
= SUB_CONFLICTS_IN_B (subscript
);
316 fprintf (outf
, " iterations_that_access_an_element_twice_in_B: ");
317 print_generic_stmt (outf
, chrec
, 0);
318 if (chrec
== chrec_known
)
319 fprintf (outf
, " (no dependence)\n");
320 else if (chrec_contains_undetermined (chrec
))
321 fprintf (outf
, " (don't know)\n");
324 tree last_iteration
= SUB_LAST_CONFLICT (subscript
);
325 fprintf (outf
, " last_conflict: ");
326 print_generic_stmt (outf
, last_iteration
, 0);
329 fprintf (outf
, " (Subscript distance: ");
330 print_generic_stmt (outf
, SUB_DISTANCE (subscript
), 0);
331 fprintf (outf
, " )\n");
332 fprintf (outf
, " )\n");
335 /* Dump function for a DATA_DEPENDENCE_RELATION structure. */
338 dump_data_dependence_relation (FILE *outf
,
339 struct data_dependence_relation
*ddr
)
341 struct data_reference
*dra
, *drb
;
345 fprintf (outf
, "(Data Dep: \n");
346 if (DDR_ARE_DEPENDENT (ddr
) == chrec_dont_know
)
347 fprintf (outf
, " (don't know)\n");
349 else if (DDR_ARE_DEPENDENT (ddr
) == chrec_known
)
350 fprintf (outf
, " (no dependence)\n");
352 else if (DDR_ARE_DEPENDENT (ddr
) == NULL_TREE
)
355 for (i
= 0; i
< DDR_NUM_SUBSCRIPTS (ddr
); i
++)
357 fprintf (outf
, " access_fn_A: ");
358 print_generic_stmt (outf
, DR_ACCESS_FN (dra
, i
), 0);
359 fprintf (outf
, " access_fn_B: ");
360 print_generic_stmt (outf
, DR_ACCESS_FN (drb
, i
), 0);
361 dump_subscript (outf
, DDR_SUBSCRIPT (ddr
, i
));
363 if (DDR_DIST_VECT (ddr
))
365 fprintf (outf
, " distance_vect: ");
366 print_lambda_vector (outf
, DDR_DIST_VECT (ddr
), DDR_SIZE_VECT (ddr
));
368 if (DDR_DIR_VECT (ddr
))
370 fprintf (outf
, " direction_vect: ");
371 print_lambda_vector (outf
, DDR_DIR_VECT (ddr
), DDR_SIZE_VECT (ddr
));
375 fprintf (outf
, ")\n");
380 /* Dump function for a DATA_DEPENDENCE_DIRECTION structure. */
383 dump_data_dependence_direction (FILE *file
,
384 enum data_dependence_direction dir
)
400 case dir_positive_or_negative
:
401 fprintf (file
, "+-");
404 case dir_positive_or_equal
:
405 fprintf (file
, "+=");
408 case dir_negative_or_equal
:
409 fprintf (file
, "-=");
421 /* Dumps the distance and direction vectors in FILE. DDRS contains
422 the dependence relations, and VECT_SIZE is the size of the
423 dependence vectors, or in other words the number of loops in the
427 dump_dist_dir_vectors (FILE *file
, varray_type ddrs
)
431 for (i
= 0; i
< VARRAY_ACTIVE_SIZE (ddrs
); i
++)
433 struct data_dependence_relation
*ddr
=
434 (struct data_dependence_relation
*)
435 VARRAY_GENERIC_PTR (ddrs
, i
);
436 if (DDR_ARE_DEPENDENT (ddr
) == NULL_TREE
437 && DDR_AFFINE_P (ddr
))
439 fprintf (file
, "DISTANCE_V (");
440 print_lambda_vector (file
, DDR_DIST_VECT (ddr
), DDR_SIZE_VECT (ddr
));
441 fprintf (file
, ")\n");
442 fprintf (file
, "DIRECTION_V (");
443 print_lambda_vector (file
, DDR_DIR_VECT (ddr
), DDR_SIZE_VECT (ddr
));
444 fprintf (file
, ")\n");
447 fprintf (file
, "\n\n");
450 /* Dumps the data dependence relations DDRS in FILE. */
453 dump_ddrs (FILE *file
, varray_type ddrs
)
457 for (i
= 0; i
< VARRAY_ACTIVE_SIZE (ddrs
); i
++)
459 struct data_dependence_relation
*ddr
=
460 (struct data_dependence_relation
*)
461 VARRAY_GENERIC_PTR (ddrs
, i
);
462 dump_data_dependence_relation (file
, ddr
);
464 fprintf (file
, "\n\n");
469 /* Compute the lowest iteration bound for LOOP. It is an
473 compute_estimated_nb_iterations (struct loop
*loop
)
476 struct nb_iter_bound
*bound
, *next
;
478 for (bound
= loop
->bounds
; bound
; bound
= next
)
481 estimation
= bound
->bound
;
483 if (TREE_CODE (estimation
) != INTEGER_CST
)
486 if (loop
->estimated_nb_iterations
)
488 /* Update only if estimation is smaller. */
489 if (tree_int_cst_lt (estimation
, loop
->estimated_nb_iterations
))
490 loop
->estimated_nb_iterations
= estimation
;
493 loop
->estimated_nb_iterations
= estimation
;
497 /* Estimate the number of iterations from the size of the data and the
501 estimate_niter_from_size_of_data (struct loop
*loop
,
507 tree array_size
, data_size
, element_size
;
510 init
= initial_condition (access_fn
);
511 step
= evolution_part_in_loop_num (access_fn
, loop
->num
);
513 array_size
= TYPE_SIZE (TREE_TYPE (opnd0
));
514 element_size
= TYPE_SIZE (TREE_TYPE (TREE_TYPE (opnd0
)));
515 if (array_size
== NULL_TREE
516 || TREE_CODE (array_size
) != INTEGER_CST
517 || TREE_CODE (element_size
) != INTEGER_CST
)
520 data_size
= fold (build2 (EXACT_DIV_EXPR
, integer_type_node
,
521 array_size
, element_size
));
523 if (init
!= NULL_TREE
525 && TREE_CODE (init
) == INTEGER_CST
526 && TREE_CODE (step
) == INTEGER_CST
)
528 estimation
= fold (build2 (CEIL_DIV_EXPR
, integer_type_node
,
529 fold (build2 (MINUS_EXPR
, integer_type_node
,
530 data_size
, init
)), step
));
532 record_estimate (loop
, estimation
, boolean_true_node
, stmt
);
536 /* Given an ARRAY_REF node REF, records its access functions.
537 Example: given A[i][3], record in ACCESS_FNS the opnd1 function,
538 i.e. the constant "3", then recursively call the function on opnd0,
539 i.e. the ARRAY_REF "A[i]". The function returns the base name:
543 analyze_array_indexes (struct loop
*loop
,
544 varray_type
*access_fns
,
550 opnd0
= TREE_OPERAND (ref
, 0);
551 opnd1
= TREE_OPERAND (ref
, 1);
553 /* The detection of the evolution function for this data access is
554 postponed until the dependence test. This lazy strategy avoids
555 the computation of access functions that are of no interest for
557 access_fn
= instantiate_parameters
558 (loop
, analyze_scalar_evolution (loop
, opnd1
));
560 if (loop
->estimated_nb_iterations
== NULL_TREE
)
561 estimate_niter_from_size_of_data (loop
, opnd0
, access_fn
, stmt
);
563 VARRAY_PUSH_TREE (*access_fns
, access_fn
);
565 /* Recursively record other array access functions. */
566 if (TREE_CODE (opnd0
) == ARRAY_REF
)
567 return analyze_array_indexes (loop
, access_fns
, opnd0
, stmt
);
569 /* Return the base name of the data access. */
574 /* For a data reference REF contained in the statement STMT, initialize
575 a DATA_REFERENCE structure, and return it. IS_READ flag has to be
576 set to true when REF is in the right hand side of an
579 struct data_reference
*
580 analyze_array (tree stmt
, tree ref
, bool is_read
)
582 struct data_reference
*res
;
584 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
586 fprintf (dump_file
, "(analyze_array \n");
587 fprintf (dump_file
, " (ref = ");
588 print_generic_stmt (dump_file
, ref
, 0);
589 fprintf (dump_file
, ")\n");
592 res
= xmalloc (sizeof (struct data_reference
));
594 DR_STMT (res
) = stmt
;
596 VARRAY_TREE_INIT (DR_ACCESS_FNS (res
), 3, "access_fns");
597 DR_BASE_NAME (res
) = analyze_array_indexes
598 (loop_containing_stmt (stmt
), &(DR_ACCESS_FNS (res
)), ref
, stmt
);
599 DR_IS_READ (res
) = is_read
;
601 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
602 fprintf (dump_file
, ")\n");
607 /* For a data reference REF contained in the statement STMT, initialize
608 a DATA_REFERENCE structure, and return it. */
610 struct data_reference
*
611 init_data_ref (tree stmt
,
617 struct data_reference
*res
;
619 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
621 fprintf (dump_file
, "(init_data_ref \n");
622 fprintf (dump_file
, " (ref = ");
623 print_generic_stmt (dump_file
, ref
, 0);
624 fprintf (dump_file
, ")\n");
627 res
= xmalloc (sizeof (struct data_reference
));
629 DR_STMT (res
) = stmt
;
631 VARRAY_TREE_INIT (DR_ACCESS_FNS (res
), 5, "access_fns");
632 DR_BASE_NAME (res
) = base
;
633 VARRAY_PUSH_TREE (DR_ACCESS_FNS (res
), access_fn
);
634 DR_IS_READ (res
) = is_read
;
636 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
637 fprintf (dump_file
, ")\n");
644 /* Returns true when all the functions of a tree_vec CHREC are the
648 all_chrecs_equal_p (tree chrec
)
652 for (j
= 0; j
< TREE_VEC_LENGTH (chrec
) - 1; j
++)
654 tree chrec_j
= TREE_VEC_ELT (chrec
, j
);
655 tree chrec_j_1
= TREE_VEC_ELT (chrec
, j
+ 1);
658 (integer_type_node
, chrec_j
, chrec_j_1
)))
664 /* Determine for each subscript in the data dependence relation DDR
668 compute_subscript_distance (struct data_dependence_relation
*ddr
)
670 if (DDR_ARE_DEPENDENT (ddr
) == NULL_TREE
)
674 for (i
= 0; i
< DDR_NUM_SUBSCRIPTS (ddr
); i
++)
676 tree conflicts_a
, conflicts_b
, difference
;
677 struct subscript
*subscript
;
679 subscript
= DDR_SUBSCRIPT (ddr
, i
);
680 conflicts_a
= SUB_CONFLICTS_IN_A (subscript
);
681 conflicts_b
= SUB_CONFLICTS_IN_B (subscript
);
683 if (TREE_CODE (conflicts_a
) == TREE_VEC
)
685 if (!all_chrecs_equal_p (conflicts_a
))
687 SUB_DISTANCE (subscript
) = chrec_dont_know
;
691 conflicts_a
= TREE_VEC_ELT (conflicts_a
, 0);
694 if (TREE_CODE (conflicts_b
) == TREE_VEC
)
696 if (!all_chrecs_equal_p (conflicts_b
))
698 SUB_DISTANCE (subscript
) = chrec_dont_know
;
702 conflicts_b
= TREE_VEC_ELT (conflicts_b
, 0);
705 difference
= chrec_fold_minus
706 (integer_type_node
, conflicts_b
, conflicts_a
);
708 if (evolution_function_is_constant_p (difference
))
709 SUB_DISTANCE (subscript
) = difference
;
712 SUB_DISTANCE (subscript
) = chrec_dont_know
;
717 /* Initialize a ddr. */
719 struct data_dependence_relation
*
720 initialize_data_dependence_relation (struct data_reference
*a
,
721 struct data_reference
*b
)
723 struct data_dependence_relation
*res
;
726 res
= xmalloc (sizeof (struct data_dependence_relation
));
730 if (a
== NULL
|| b
== NULL
731 || DR_BASE_NAME (a
) == NULL_TREE
732 || DR_BASE_NAME (b
) == NULL_TREE
)
733 DDR_ARE_DEPENDENT (res
) = chrec_dont_know
;
735 /* When the dimensions of A and B differ, we directly initialize
736 the relation to "there is no dependence": chrec_known. */
737 else if (DR_NUM_DIMENSIONS (a
) != DR_NUM_DIMENSIONS (b
)
738 || (array_base_name_differ_p (a
, b
, &differ_p
) && differ_p
))
739 DDR_ARE_DEPENDENT (res
) = chrec_known
;
744 DDR_AFFINE_P (res
) = true;
745 DDR_ARE_DEPENDENT (res
) = NULL_TREE
;
746 DDR_SUBSCRIPTS_VECTOR_INIT (res
, DR_NUM_DIMENSIONS (a
));
747 DDR_SIZE_VECT (res
) = 0;
748 DDR_DIST_VECT (res
) = NULL
;
749 DDR_DIR_VECT (res
) = NULL
;
751 for (i
= 0; i
< DR_NUM_DIMENSIONS (a
); i
++)
753 struct subscript
*subscript
;
755 subscript
= xmalloc (sizeof (struct subscript
));
756 SUB_CONFLICTS_IN_A (subscript
) = chrec_dont_know
;
757 SUB_CONFLICTS_IN_B (subscript
) = chrec_dont_know
;
758 SUB_LAST_CONFLICT (subscript
) = chrec_dont_know
;
759 SUB_DISTANCE (subscript
) = chrec_dont_know
;
760 VARRAY_PUSH_GENERIC_PTR (DDR_SUBSCRIPTS (res
), subscript
);
767 /* Set DDR_ARE_DEPENDENT to CHREC and finalize the subscript overlap
771 finalize_ddr_dependent (struct data_dependence_relation
*ddr
,
774 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
776 fprintf (dump_file
, "(dependence classified: ");
777 print_generic_expr (dump_file
, chrec
, 0);
778 fprintf (dump_file
, ")\n");
781 DDR_ARE_DEPENDENT (ddr
) = chrec
;
782 varray_clear (DDR_SUBSCRIPTS (ddr
));
785 /* The dependence relation DDR cannot be represented by a distance
789 non_affine_dependence_relation (struct data_dependence_relation
*ddr
)
791 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
792 fprintf (dump_file
, "(Dependence relation cannot be represented by distance vector.) \n");
794 DDR_AFFINE_P (ddr
) = false;
799 /* This section contains the classic Banerjee tests. */
801 /* Returns true iff CHREC_A and CHREC_B are not dependent on any index
802 variables, i.e., if the ZIV (Zero Index Variable) test is true. */
805 ziv_subscript_p (tree chrec_a
,
808 return (evolution_function_is_constant_p (chrec_a
)
809 && evolution_function_is_constant_p (chrec_b
));
812 /* Returns true iff CHREC_A and CHREC_B are dependent on an index
813 variable, i.e., if the SIV (Single Index Variable) test is true. */
816 siv_subscript_p (tree chrec_a
,
819 if ((evolution_function_is_constant_p (chrec_a
)
820 && evolution_function_is_univariate_p (chrec_b
))
821 || (evolution_function_is_constant_p (chrec_b
)
822 && evolution_function_is_univariate_p (chrec_a
)))
825 if (evolution_function_is_univariate_p (chrec_a
)
826 && evolution_function_is_univariate_p (chrec_b
))
828 switch (TREE_CODE (chrec_a
))
830 case POLYNOMIAL_CHREC
:
831 switch (TREE_CODE (chrec_b
))
833 case POLYNOMIAL_CHREC
:
834 if (CHREC_VARIABLE (chrec_a
) != CHREC_VARIABLE (chrec_b
))
849 /* Analyze a ZIV (Zero Index Variable) subscript. *OVERLAPS_A and
850 *OVERLAPS_B are initialized to the functions that describe the
851 relation between the elements accessed twice by CHREC_A and
852 CHREC_B. For k >= 0, the following property is verified:
854 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
857 analyze_ziv_subscript (tree chrec_a
,
861 tree
*last_conflicts
)
865 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
866 fprintf (dump_file
, "(analyze_ziv_subscript \n");
868 difference
= chrec_fold_minus (integer_type_node
, chrec_a
, chrec_b
);
870 switch (TREE_CODE (difference
))
873 if (integer_zerop (difference
))
875 /* The difference is equal to zero: the accessed index
876 overlaps for each iteration in the loop. */
877 *overlaps_a
= integer_zero_node
;
878 *overlaps_b
= integer_zero_node
;
879 *last_conflicts
= chrec_dont_know
;
883 /* The accesses do not overlap. */
884 *overlaps_a
= chrec_known
;
885 *overlaps_b
= chrec_known
;
886 *last_conflicts
= integer_zero_node
;
891 /* We're not sure whether the indexes overlap. For the moment,
892 conservatively answer "don't know". */
893 *overlaps_a
= chrec_dont_know
;
894 *overlaps_b
= chrec_dont_know
;
895 *last_conflicts
= chrec_dont_know
;
899 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
900 fprintf (dump_file
, ")\n");
903 /* Analyze a SIV (Single Index Variable) subscript where CHREC_A is a
904 constant, and CHREC_B is an affine function. *OVERLAPS_A and
905 *OVERLAPS_B are initialized to the functions that describe the
906 relation between the elements accessed twice by CHREC_A and
907 CHREC_B. For k >= 0, the following property is verified:
909 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
912 analyze_siv_subscript_cst_affine (tree chrec_a
,
916 tree
*last_conflicts
)
918 bool value0
, value1
, value2
;
919 tree difference
= chrec_fold_minus
920 (integer_type_node
, CHREC_LEFT (chrec_b
), chrec_a
);
922 if (!chrec_is_positive (initial_condition (difference
), &value0
))
924 *overlaps_a
= chrec_dont_know
;
925 *overlaps_b
= chrec_dont_know
;
926 *last_conflicts
= chrec_dont_know
;
933 if (!chrec_is_positive (CHREC_RIGHT (chrec_b
), &value1
))
935 *overlaps_a
= chrec_dont_know
;
936 *overlaps_b
= chrec_dont_know
;
937 *last_conflicts
= chrec_dont_know
;
949 if (tree_fold_divides_p
950 (integer_type_node
, CHREC_RIGHT (chrec_b
), difference
))
952 *overlaps_a
= integer_zero_node
;
954 (build (EXACT_DIV_EXPR
, integer_type_node
,
955 fold (build1 (ABS_EXPR
, integer_type_node
, difference
)),
956 CHREC_RIGHT (chrec_b
)));
957 *last_conflicts
= integer_one_node
;
961 /* When the step does not divides the difference, there are
965 *overlaps_a
= chrec_known
;
966 *overlaps_b
= chrec_known
;
967 *last_conflicts
= integer_zero_node
;
976 chrec_b = {10, +, -1}
978 In this case, chrec_a will not overlap with chrec_b. */
979 *overlaps_a
= chrec_known
;
980 *overlaps_b
= chrec_known
;
981 *last_conflicts
= integer_zero_node
;
988 if (!chrec_is_positive (CHREC_RIGHT (chrec_b
), &value2
))
990 *overlaps_a
= chrec_dont_know
;
991 *overlaps_b
= chrec_dont_know
;
992 *last_conflicts
= chrec_dont_know
;
1001 chrec_b = {10, +, -1}
1003 if (tree_fold_divides_p
1004 (integer_type_node
, CHREC_RIGHT (chrec_b
), difference
))
1006 *overlaps_a
= integer_zero_node
;
1008 (build (EXACT_DIV_EXPR
, integer_type_node
, difference
,
1009 CHREC_RIGHT (chrec_b
)));
1010 *last_conflicts
= integer_one_node
;
1014 /* When the step does not divides the difference, there
1018 *overlaps_a
= chrec_known
;
1019 *overlaps_b
= chrec_known
;
1020 *last_conflicts
= integer_zero_node
;
1030 In this case, chrec_a will not overlap with chrec_b. */
1031 *overlaps_a
= chrec_known
;
1032 *overlaps_b
= chrec_known
;
1033 *last_conflicts
= integer_zero_node
;
1041 /* Helper recursive function for initializing the matrix A. Returns
1042 the initial value of CHREC. */
1045 initialize_matrix_A (lambda_matrix A
, tree chrec
, unsigned index
, int mult
)
1049 if (TREE_CODE (chrec
) != POLYNOMIAL_CHREC
)
1050 return int_cst_value (chrec
);
1052 A
[index
][0] = mult
* int_cst_value (CHREC_RIGHT (chrec
));
1053 return initialize_matrix_A (A
, CHREC_LEFT (chrec
), index
+ 1, mult
);
1056 #define FLOOR_DIV(x,y) ((x) / (y))
1058 /* Solves the special case of the Diophantine equation:
1059 | {0, +, STEP_A}_x (OVERLAPS_A) = {0, +, STEP_B}_y (OVERLAPS_B)
1061 Computes the descriptions OVERLAPS_A and OVERLAPS_B. NITER is the
1062 number of iterations that loops X and Y run. The overlaps will be
1063 constructed as evolutions in dimension DIM. */
1066 compute_overlap_steps_for_affine_univar (int niter
, int step_a
, int step_b
,
1067 tree
*overlaps_a
, tree
*overlaps_b
,
1068 tree
*last_conflicts
, int dim
)
1070 if (((step_a
> 0 && step_b
> 0)
1071 || (step_a
< 0 && step_b
< 0)))
1073 int step_overlaps_a
, step_overlaps_b
;
1074 int gcd_steps_a_b
, last_conflict
, tau2
;
1076 gcd_steps_a_b
= gcd (step_a
, step_b
);
1077 step_overlaps_a
= step_b
/ gcd_steps_a_b
;
1078 step_overlaps_b
= step_a
/ gcd_steps_a_b
;
1080 tau2
= FLOOR_DIV (niter
, step_overlaps_a
);
1081 tau2
= MIN (tau2
, FLOOR_DIV (niter
, step_overlaps_b
));
1082 last_conflict
= tau2
;
1084 *overlaps_a
= build_polynomial_chrec
1085 (dim
, integer_zero_node
,
1086 build_int_cst (NULL_TREE
, step_overlaps_a
));
1087 *overlaps_b
= build_polynomial_chrec
1088 (dim
, integer_zero_node
,
1089 build_int_cst (NULL_TREE
, step_overlaps_b
));
1090 *last_conflicts
= build_int_cst (NULL_TREE
, last_conflict
);
1095 *overlaps_a
= integer_zero_node
;
1096 *overlaps_b
= integer_zero_node
;
1097 *last_conflicts
= integer_zero_node
;
1102 /* Solves the special case of a Diophantine equation where CHREC_A is
1103 an affine bivariate function, and CHREC_B is an affine univariate
1104 function. For example,
1106 | {{0, +, 1}_x, +, 1335}_y = {0, +, 1336}_z
1108 has the following overlapping functions:
1110 | x (t, u, v) = {{0, +, 1336}_t, +, 1}_v
1111 | y (t, u, v) = {{0, +, 1336}_u, +, 1}_v
1112 | z (t, u, v) = {{{0, +, 1}_t, +, 1335}_u, +, 1}_v
1114 FORNOW: This is a specialized implementation for a case occuring in
1115 a common benchmark. Implement the general algorithm. */
1118 compute_overlap_steps_for_affine_1_2 (tree chrec_a
, tree chrec_b
,
1119 tree
*overlaps_a
, tree
*overlaps_b
,
1120 tree
*last_conflicts
)
1122 bool xz_p
, yz_p
, xyz_p
;
1123 int step_x
, step_y
, step_z
;
1124 int niter_x
, niter_y
, niter_z
, niter
;
1125 tree numiter_x
, numiter_y
, numiter_z
;
1126 tree overlaps_a_xz
, overlaps_b_xz
, last_conflicts_xz
;
1127 tree overlaps_a_yz
, overlaps_b_yz
, last_conflicts_yz
;
1128 tree overlaps_a_xyz
, overlaps_b_xyz
, last_conflicts_xyz
;
1130 step_x
= int_cst_value (CHREC_RIGHT (CHREC_LEFT (chrec_a
)));
1131 step_y
= int_cst_value (CHREC_RIGHT (chrec_a
));
1132 step_z
= int_cst_value (CHREC_RIGHT (chrec_b
));
1134 numiter_x
= number_of_iterations_in_loop
1135 (current_loops
->parray
[CHREC_VARIABLE (CHREC_LEFT (chrec_a
))]);
1136 numiter_y
= number_of_iterations_in_loop
1137 (current_loops
->parray
[CHREC_VARIABLE (chrec_a
)]);
1138 numiter_z
= number_of_iterations_in_loop
1139 (current_loops
->parray
[CHREC_VARIABLE (chrec_b
)]);
1141 if (TREE_CODE (numiter_x
) != INTEGER_CST
)
1142 numiter_x
= current_loops
->parray
[CHREC_VARIABLE (CHREC_LEFT (chrec_a
))]
1143 ->estimated_nb_iterations
;
1144 if (TREE_CODE (numiter_y
) != INTEGER_CST
)
1145 numiter_y
= current_loops
->parray
[CHREC_VARIABLE (chrec_a
)]
1146 ->estimated_nb_iterations
;
1147 if (TREE_CODE (numiter_z
) != INTEGER_CST
)
1148 numiter_z
= current_loops
->parray
[CHREC_VARIABLE (chrec_b
)]
1149 ->estimated_nb_iterations
;
1151 if (numiter_x
== NULL_TREE
|| numiter_y
== NULL_TREE
1152 || numiter_z
== NULL_TREE
)
1154 *overlaps_a
= chrec_dont_know
;
1155 *overlaps_b
= chrec_dont_know
;
1156 *last_conflicts
= chrec_dont_know
;
1160 niter_x
= int_cst_value (numiter_x
);
1161 niter_y
= int_cst_value (numiter_y
);
1162 niter_z
= int_cst_value (numiter_z
);
1164 niter
= MIN (niter_x
, niter_z
);
1165 compute_overlap_steps_for_affine_univar (niter
, step_x
, step_z
,
1168 &last_conflicts_xz
, 1);
1169 niter
= MIN (niter_y
, niter_z
);
1170 compute_overlap_steps_for_affine_univar (niter
, step_y
, step_z
,
1173 &last_conflicts_yz
, 2);
1174 niter
= MIN (niter_x
, niter_z
);
1175 niter
= MIN (niter_y
, niter
);
1176 compute_overlap_steps_for_affine_univar (niter
, step_x
+ step_y
, step_z
,
1179 &last_conflicts_xyz
, 3);
1181 xz_p
= !integer_zerop (last_conflicts_xz
);
1182 yz_p
= !integer_zerop (last_conflicts_yz
);
1183 xyz_p
= !integer_zerop (last_conflicts_xyz
);
1185 if (xz_p
|| yz_p
|| xyz_p
)
1187 *overlaps_a
= make_tree_vec (2);
1188 TREE_VEC_ELT (*overlaps_a
, 0) = integer_zero_node
;
1189 TREE_VEC_ELT (*overlaps_a
, 1) = integer_zero_node
;
1190 *overlaps_b
= integer_zero_node
;
1193 TREE_VEC_ELT (*overlaps_a
, 0) =
1194 chrec_fold_plus (integer_type_node
, TREE_VEC_ELT (*overlaps_a
, 0),
1197 chrec_fold_plus (integer_type_node
, *overlaps_b
, overlaps_b_xz
);
1198 *last_conflicts
= last_conflicts_xz
;
1202 TREE_VEC_ELT (*overlaps_a
, 1) =
1203 chrec_fold_plus (integer_type_node
, TREE_VEC_ELT (*overlaps_a
, 1),
1206 chrec_fold_plus (integer_type_node
, *overlaps_b
, overlaps_b_yz
);
1207 *last_conflicts
= last_conflicts_yz
;
1211 TREE_VEC_ELT (*overlaps_a
, 0) =
1212 chrec_fold_plus (integer_type_node
, TREE_VEC_ELT (*overlaps_a
, 0),
1214 TREE_VEC_ELT (*overlaps_a
, 1) =
1215 chrec_fold_plus (integer_type_node
, TREE_VEC_ELT (*overlaps_a
, 1),
1218 chrec_fold_plus (integer_type_node
, *overlaps_b
, overlaps_b_xyz
);
1219 *last_conflicts
= last_conflicts_xyz
;
1224 *overlaps_a
= integer_zero_node
;
1225 *overlaps_b
= integer_zero_node
;
1226 *last_conflicts
= integer_zero_node
;
1230 /* Determines the overlapping elements due to accesses CHREC_A and
1231 CHREC_B, that are affine functions. This is a part of the
1232 subscript analyzer. */
1235 analyze_subscript_affine_affine (tree chrec_a
,
1239 tree
*last_conflicts
)
1241 unsigned nb_vars_a
, nb_vars_b
, dim
;
1242 int init_a
, init_b
, gamma
, gcd_alpha_beta
;
1244 lambda_matrix A
, U
, S
;
1246 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1247 fprintf (dump_file
, "(analyze_subscript_affine_affine \n");
1249 /* For determining the initial intersection, we have to solve a
1250 Diophantine equation. This is the most time consuming part.
1252 For answering to the question: "Is there a dependence?" we have
1253 to prove that there exists a solution to the Diophantine
1254 equation, and that the solution is in the iteration domain,
1255 i.e. the solution is positive or zero, and that the solution
1256 happens before the upper bound loop.nb_iterations. Otherwise
1257 there is no dependence. This function outputs a description of
1258 the iterations that hold the intersections. */
1261 nb_vars_a
= nb_vars_in_chrec (chrec_a
);
1262 nb_vars_b
= nb_vars_in_chrec (chrec_b
);
1264 dim
= nb_vars_a
+ nb_vars_b
;
1265 U
= lambda_matrix_new (dim
, dim
);
1266 A
= lambda_matrix_new (dim
, 1);
1267 S
= lambda_matrix_new (dim
, 1);
1269 init_a
= initialize_matrix_A (A
, chrec_a
, 0, 1);
1270 init_b
= initialize_matrix_A (A
, chrec_b
, nb_vars_a
, -1);
1271 gamma
= init_b
- init_a
;
1273 /* Don't do all the hard work of solving the Diophantine equation
1274 when we already know the solution: for example,
1277 | gamma = 3 - 3 = 0.
1278 Then the first overlap occurs during the first iterations:
1279 | {3, +, 1}_1 ({0, +, 4}_x) = {3, +, 4}_2 ({0, +, 1}_x)
1283 if (nb_vars_a
== 1 && nb_vars_b
== 1)
1286 int niter
, niter_a
, niter_b
;
1287 tree numiter_a
, numiter_b
;
1289 numiter_a
= number_of_iterations_in_loop
1290 (current_loops
->parray
[CHREC_VARIABLE (chrec_a
)]);
1291 numiter_b
= number_of_iterations_in_loop
1292 (current_loops
->parray
[CHREC_VARIABLE (chrec_b
)]);
1294 if (TREE_CODE (numiter_a
) != INTEGER_CST
)
1295 numiter_a
= current_loops
->parray
[CHREC_VARIABLE (chrec_a
)]
1296 ->estimated_nb_iterations
;
1297 if (TREE_CODE (numiter_b
) != INTEGER_CST
)
1298 numiter_b
= current_loops
->parray
[CHREC_VARIABLE (chrec_b
)]
1299 ->estimated_nb_iterations
;
1300 if (numiter_a
== NULL_TREE
|| numiter_b
== NULL_TREE
)
1302 *overlaps_a
= chrec_dont_know
;
1303 *overlaps_b
= chrec_dont_know
;
1304 *last_conflicts
= chrec_dont_know
;
1308 niter_a
= int_cst_value (numiter_a
);
1309 niter_b
= int_cst_value (numiter_b
);
1310 niter
= MIN (niter_a
, niter_b
);
1312 step_a
= int_cst_value (CHREC_RIGHT (chrec_a
));
1313 step_b
= int_cst_value (CHREC_RIGHT (chrec_b
));
1315 compute_overlap_steps_for_affine_univar (niter
, step_a
, step_b
,
1316 overlaps_a
, overlaps_b
,
1320 else if (nb_vars_a
== 2 && nb_vars_b
== 1)
1321 compute_overlap_steps_for_affine_1_2
1322 (chrec_a
, chrec_b
, overlaps_a
, overlaps_b
, last_conflicts
);
1324 else if (nb_vars_a
== 1 && nb_vars_b
== 2)
1325 compute_overlap_steps_for_affine_1_2
1326 (chrec_b
, chrec_a
, overlaps_b
, overlaps_a
, last_conflicts
);
1330 *overlaps_a
= chrec_dont_know
;
1331 *overlaps_b
= chrec_dont_know
;
1332 *last_conflicts
= chrec_dont_know
;
1338 lambda_matrix_right_hermite (A
, dim
, 1, S
, U
);
1343 lambda_matrix_row_negate (U
, dim
, 0);
1345 gcd_alpha_beta
= S
[0][0];
1347 /* The classic "gcd-test". */
1348 if (!int_divides_p (gcd_alpha_beta
, gamma
))
1350 /* The "gcd-test" has determined that there is no integer
1351 solution, i.e. there is no dependence. */
1352 *overlaps_a
= chrec_known
;
1353 *overlaps_b
= chrec_known
;
1354 *last_conflicts
= integer_zero_node
;
1357 /* Both access functions are univariate. This includes SIV and MIV cases. */
1358 else if (nb_vars_a
== 1 && nb_vars_b
== 1)
1360 /* Both functions should have the same evolution sign. */
1361 if (((A
[0][0] > 0 && -A
[1][0] > 0)
1362 || (A
[0][0] < 0 && -A
[1][0] < 0)))
1364 /* The solutions are given by:
1366 | [GAMMA/GCD_ALPHA_BETA t].[u11 u12] = [x0]
1369 For a given integer t. Using the following variables,
1371 | i0 = u11 * gamma / gcd_alpha_beta
1372 | j0 = u12 * gamma / gcd_alpha_beta
1379 | y0 = j0 + j1 * t. */
1383 /* X0 and Y0 are the first iterations for which there is a
1384 dependence. X0, Y0 are two solutions of the Diophantine
1385 equation: chrec_a (X0) = chrec_b (Y0). */
1387 int niter
, niter_a
, niter_b
;
1388 tree numiter_a
, numiter_b
;
1390 numiter_a
= number_of_iterations_in_loop
1391 (current_loops
->parray
[CHREC_VARIABLE (chrec_a
)]);
1392 numiter_b
= number_of_iterations_in_loop
1393 (current_loops
->parray
[CHREC_VARIABLE (chrec_b
)]);
1395 if (TREE_CODE (numiter_a
) != INTEGER_CST
)
1396 numiter_a
= current_loops
->parray
[CHREC_VARIABLE (chrec_a
)]
1397 ->estimated_nb_iterations
;
1398 if (TREE_CODE (numiter_b
) != INTEGER_CST
)
1399 numiter_b
= current_loops
->parray
[CHREC_VARIABLE (chrec_b
)]
1400 ->estimated_nb_iterations
;
1401 if (numiter_a
== NULL_TREE
|| numiter_b
== NULL_TREE
)
1403 *overlaps_a
= chrec_dont_know
;
1404 *overlaps_b
= chrec_dont_know
;
1405 *last_conflicts
= chrec_dont_know
;
1409 niter_a
= int_cst_value (numiter_a
);
1410 niter_b
= int_cst_value (numiter_b
);
1411 niter
= MIN (niter_a
, niter_b
);
1413 i0
= U
[0][0] * gamma
/ gcd_alpha_beta
;
1414 j0
= U
[0][1] * gamma
/ gcd_alpha_beta
;
1418 if ((i1
== 0 && i0
< 0)
1419 || (j1
== 0 && j0
< 0))
1421 /* There is no solution.
1422 FIXME: The case "i0 > nb_iterations, j0 > nb_iterations"
1423 falls in here, but for the moment we don't look at the
1424 upper bound of the iteration domain. */
1425 *overlaps_a
= chrec_known
;
1426 *overlaps_b
= chrec_known
;
1427 *last_conflicts
= integer_zero_node
;
1434 tau1
= CEIL (-i0
, i1
);
1435 tau2
= FLOOR_DIV (niter
- i0
, i1
);
1439 int last_conflict
, min_multiple
;
1440 tau1
= MAX (tau1
, CEIL (-j0
, j1
));
1441 tau2
= MIN (tau2
, FLOOR_DIV (niter
- j0
, j1
));
1443 x0
= i1
* tau1
+ i0
;
1444 y0
= j1
* tau1
+ j0
;
1446 /* At this point (x0, y0) is one of the
1447 solutions to the Diophantine equation. The
1448 next step has to compute the smallest
1449 positive solution: the first conflicts. */
1450 min_multiple
= MIN (x0
/ i1
, y0
/ j1
);
1451 x0
-= i1
* min_multiple
;
1452 y0
-= j1
* min_multiple
;
1454 tau1
= (x0
- i0
)/i1
;
1455 last_conflict
= tau2
- tau1
;
1457 *overlaps_a
= build_polynomial_chrec
1459 build_int_cst (NULL_TREE
, x0
),
1460 build_int_cst (NULL_TREE
, i1
));
1461 *overlaps_b
= build_polynomial_chrec
1463 build_int_cst (NULL_TREE
, y0
),
1464 build_int_cst (NULL_TREE
, j1
));
1465 *last_conflicts
= build_int_cst (NULL_TREE
, last_conflict
);
1469 /* FIXME: For the moment, the upper bound of the
1470 iteration domain for j is not checked. */
1471 *overlaps_a
= chrec_dont_know
;
1472 *overlaps_b
= chrec_dont_know
;
1473 *last_conflicts
= chrec_dont_know
;
1479 /* FIXME: For the moment, the upper bound of the
1480 iteration domain for i is not checked. */
1481 *overlaps_a
= chrec_dont_know
;
1482 *overlaps_b
= chrec_dont_know
;
1483 *last_conflicts
= chrec_dont_know
;
1489 *overlaps_a
= chrec_dont_know
;
1490 *overlaps_b
= chrec_dont_know
;
1491 *last_conflicts
= chrec_dont_know
;
1497 *overlaps_a
= chrec_dont_know
;
1498 *overlaps_b
= chrec_dont_know
;
1499 *last_conflicts
= chrec_dont_know
;
1503 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1505 fprintf (dump_file
, " (overlaps_a = ");
1506 print_generic_expr (dump_file
, *overlaps_a
, 0);
1507 fprintf (dump_file
, ")\n (overlaps_b = ");
1508 print_generic_expr (dump_file
, *overlaps_b
, 0);
1509 fprintf (dump_file
, ")\n");
1512 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1513 fprintf (dump_file
, ")\n");
1516 /* Analyze a SIV (Single Index Variable) subscript. *OVERLAPS_A and
1517 *OVERLAPS_B are initialized to the functions that describe the
1518 relation between the elements accessed twice by CHREC_A and
1519 CHREC_B. For k >= 0, the following property is verified:
1521 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
1524 analyze_siv_subscript (tree chrec_a
,
1528 tree
*last_conflicts
)
1530 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1531 fprintf (dump_file
, "(analyze_siv_subscript \n");
1533 if (evolution_function_is_constant_p (chrec_a
)
1534 && evolution_function_is_affine_p (chrec_b
))
1535 analyze_siv_subscript_cst_affine (chrec_a
, chrec_b
,
1536 overlaps_a
, overlaps_b
, last_conflicts
);
1538 else if (evolution_function_is_affine_p (chrec_a
)
1539 && evolution_function_is_constant_p (chrec_b
))
1540 analyze_siv_subscript_cst_affine (chrec_b
, chrec_a
,
1541 overlaps_b
, overlaps_a
, last_conflicts
);
1543 else if (evolution_function_is_affine_p (chrec_a
)
1544 && evolution_function_is_affine_p (chrec_b
))
1545 analyze_subscript_affine_affine (chrec_a
, chrec_b
,
1546 overlaps_a
, overlaps_b
, last_conflicts
);
1549 *overlaps_a
= chrec_dont_know
;
1550 *overlaps_b
= chrec_dont_know
;
1551 *last_conflicts
= chrec_dont_know
;
1554 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1555 fprintf (dump_file
, ")\n");
1558 /* Return true when the evolution steps of an affine CHREC divide the
1562 chrec_steps_divide_constant_p (tree chrec
,
1565 switch (TREE_CODE (chrec
))
1567 case POLYNOMIAL_CHREC
:
1568 return (tree_fold_divides_p (integer_type_node
, CHREC_RIGHT (chrec
), cst
)
1569 && chrec_steps_divide_constant_p (CHREC_LEFT (chrec
), cst
));
1572 /* On the initial condition, return true. */
1577 /* Analyze a MIV (Multiple Index Variable) subscript. *OVERLAPS_A and
1578 *OVERLAPS_B are initialized to the functions that describe the
1579 relation between the elements accessed twice by CHREC_A and
1580 CHREC_B. For k >= 0, the following property is verified:
1582 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
1585 analyze_miv_subscript (tree chrec_a
,
1589 tree
*last_conflicts
)
1591 /* FIXME: This is a MIV subscript, not yet handled.
1592 Example: (A[{1, +, 1}_1] vs. A[{1, +, 1}_2]) that comes from
1595 In the SIV test we had to solve a Diophantine equation with two
1596 variables. In the MIV case we have to solve a Diophantine
1597 equation with 2*n variables (if the subscript uses n IVs).
1601 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1602 fprintf (dump_file
, "(analyze_miv_subscript \n");
1604 difference
= chrec_fold_minus (integer_type_node
, chrec_a
, chrec_b
);
1606 if (chrec_zerop (difference
))
1608 /* Access functions are the same: all the elements are accessed
1609 in the same order. */
1610 *overlaps_a
= integer_zero_node
;
1611 *overlaps_b
= integer_zero_node
;
1612 *last_conflicts
= number_of_iterations_in_loop
1613 (current_loops
->parray
[CHREC_VARIABLE (chrec_a
)]);
1616 else if (evolution_function_is_constant_p (difference
)
1617 /* For the moment, the following is verified:
1618 evolution_function_is_affine_multivariate_p (chrec_a) */
1619 && !chrec_steps_divide_constant_p (chrec_a
, difference
))
1621 /* testsuite/.../ssa-chrec-33.c
1622 {{21, +, 2}_1, +, -2}_2 vs. {{20, +, 2}_1, +, -2}_2
1624 The difference is 1, and the evolution steps are equal to 2,
1625 consequently there are no overlapping elements. */
1626 *overlaps_a
= chrec_known
;
1627 *overlaps_b
= chrec_known
;
1628 *last_conflicts
= integer_zero_node
;
1631 else if (evolution_function_is_affine_multivariate_p (chrec_a
)
1632 && evolution_function_is_affine_multivariate_p (chrec_b
))
1634 /* testsuite/.../ssa-chrec-35.c
1635 {0, +, 1}_2 vs. {0, +, 1}_3
1636 the overlapping elements are respectively located at iterations:
1637 {0, +, 1}_x and {0, +, 1}_x,
1638 in other words, we have the equality:
1639 {0, +, 1}_2 ({0, +, 1}_x) = {0, +, 1}_3 ({0, +, 1}_x)
1642 {{0, +, 1}_1, +, 2}_2 ({0, +, 1}_x, {0, +, 1}_y) =
1643 {0, +, 1}_1 ({{0, +, 1}_x, +, 2}_y)
1645 {{0, +, 2}_1, +, 3}_2 ({0, +, 1}_y, {0, +, 1}_x) =
1646 {{0, +, 3}_1, +, 2}_2 ({0, +, 1}_x, {0, +, 1}_y)
1648 analyze_subscript_affine_affine (chrec_a
, chrec_b
,
1649 overlaps_a
, overlaps_b
, last_conflicts
);
1654 /* When the analysis is too difficult, answer "don't know". */
1655 *overlaps_a
= chrec_dont_know
;
1656 *overlaps_b
= chrec_dont_know
;
1657 *last_conflicts
= chrec_dont_know
;
1660 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1661 fprintf (dump_file
, ")\n");
1664 /* Determines the iterations for which CHREC_A is equal to CHREC_B.
1665 OVERLAP_ITERATIONS_A and OVERLAP_ITERATIONS_B are initialized with
1666 two functions that describe the iterations that contain conflicting
1669 Remark: For an integer k >= 0, the following equality is true:
1671 CHREC_A (OVERLAP_ITERATIONS_A (k)) == CHREC_B (OVERLAP_ITERATIONS_B (k)).
1675 analyze_overlapping_iterations (tree chrec_a
,
1677 tree
*overlap_iterations_a
,
1678 tree
*overlap_iterations_b
,
1679 tree
*last_conflicts
)
1681 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1683 fprintf (dump_file
, "(analyze_overlapping_iterations \n");
1684 fprintf (dump_file
, " (chrec_a = ");
1685 print_generic_expr (dump_file
, chrec_a
, 0);
1686 fprintf (dump_file
, ")\n chrec_b = ");
1687 print_generic_expr (dump_file
, chrec_b
, 0);
1688 fprintf (dump_file
, ")\n");
1691 if (chrec_a
== NULL_TREE
1692 || chrec_b
== NULL_TREE
1693 || chrec_contains_undetermined (chrec_a
)
1694 || chrec_contains_undetermined (chrec_b
)
1695 || chrec_contains_symbols (chrec_a
)
1696 || chrec_contains_symbols (chrec_b
))
1698 *overlap_iterations_a
= chrec_dont_know
;
1699 *overlap_iterations_b
= chrec_dont_know
;
1702 else if (ziv_subscript_p (chrec_a
, chrec_b
))
1703 analyze_ziv_subscript (chrec_a
, chrec_b
,
1704 overlap_iterations_a
, overlap_iterations_b
,
1707 else if (siv_subscript_p (chrec_a
, chrec_b
))
1708 analyze_siv_subscript (chrec_a
, chrec_b
,
1709 overlap_iterations_a
, overlap_iterations_b
,
1713 analyze_miv_subscript (chrec_a
, chrec_b
,
1714 overlap_iterations_a
, overlap_iterations_b
,
1717 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1719 fprintf (dump_file
, " (overlap_iterations_a = ");
1720 print_generic_expr (dump_file
, *overlap_iterations_a
, 0);
1721 fprintf (dump_file
, ")\n (overlap_iterations_b = ");
1722 print_generic_expr (dump_file
, *overlap_iterations_b
, 0);
1723 fprintf (dump_file
, ")\n");
1729 /* This section contains the affine functions dependences detector. */
1731 /* Computes the conflicting iterations, and initialize DDR. */
1734 subscript_dependence_tester (struct data_dependence_relation
*ddr
)
1737 struct data_reference
*dra
= DDR_A (ddr
);
1738 struct data_reference
*drb
= DDR_B (ddr
);
1739 tree last_conflicts
;
1741 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1742 fprintf (dump_file
, "(subscript_dependence_tester \n");
1744 for (i
= 0; i
< DDR_NUM_SUBSCRIPTS (ddr
); i
++)
1746 tree overlaps_a
, overlaps_b
;
1747 struct subscript
*subscript
= DDR_SUBSCRIPT (ddr
, i
);
1749 analyze_overlapping_iterations (DR_ACCESS_FN (dra
, i
),
1750 DR_ACCESS_FN (drb
, i
),
1751 &overlaps_a
, &overlaps_b
,
1754 if (chrec_contains_undetermined (overlaps_a
)
1755 || chrec_contains_undetermined (overlaps_b
))
1757 finalize_ddr_dependent (ddr
, chrec_dont_know
);
1761 else if (overlaps_a
== chrec_known
1762 || overlaps_b
== chrec_known
)
1764 finalize_ddr_dependent (ddr
, chrec_known
);
1770 SUB_CONFLICTS_IN_A (subscript
) = overlaps_a
;
1771 SUB_CONFLICTS_IN_B (subscript
) = overlaps_b
;
1772 SUB_LAST_CONFLICT (subscript
) = last_conflicts
;
1776 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1777 fprintf (dump_file
, ")\n");
1780 /* Compute the classic per loop distance vector.
1782 DDR is the data dependence relation to build a vector from.
1783 NB_LOOPS is the total number of loops we are considering.
1784 FIRST_LOOP is the loop->num of the first loop in the analyzed
1786 Return FALSE if the dependence relation is outside of the loop nest
1787 starting with FIRST_LOOP.
1788 Return TRUE otherwise. */
1791 build_classic_dist_vector (struct data_dependence_relation
*ddr
,
1792 int nb_loops
, unsigned int first_loop
)
1795 lambda_vector dist_v
, init_v
;
1797 dist_v
= lambda_vector_new (nb_loops
);
1798 init_v
= lambda_vector_new (nb_loops
);
1799 lambda_vector_clear (dist_v
, nb_loops
);
1800 lambda_vector_clear (init_v
, nb_loops
);
1802 if (DDR_ARE_DEPENDENT (ddr
) != NULL_TREE
)
1805 for (i
= 0; i
< DDR_NUM_SUBSCRIPTS (ddr
); i
++)
1807 tree access_fn_a
, access_fn_b
;
1808 struct subscript
*subscript
= DDR_SUBSCRIPT (ddr
, i
);
1810 if (chrec_contains_undetermined (SUB_DISTANCE (subscript
)))
1812 non_affine_dependence_relation (ddr
);
1816 access_fn_a
= DR_ACCESS_FN (DDR_A (ddr
), i
);
1817 access_fn_b
= DR_ACCESS_FN (DDR_B (ddr
), i
);
1819 if (TREE_CODE (access_fn_a
) == POLYNOMIAL_CHREC
1820 && TREE_CODE (access_fn_b
) == POLYNOMIAL_CHREC
)
1823 int loop_nb_a
= CHREC_VARIABLE (access_fn_a
);
1824 int loop_nb_b
= CHREC_VARIABLE (access_fn_b
);
1825 struct loop
*loop_a
= current_loops
->parray
[loop_nb_a
];
1826 struct loop
*loop_b
= current_loops
->parray
[loop_nb_b
];
1827 struct loop
*loop_first
= current_loops
->parray
[first_loop
];
1829 /* If the loop for either variable is at a lower depth than
1830 the first_loop's depth, then we can't possibly have a
1831 dependency at this level of the loop. */
1833 if (loop_a
->depth
< loop_first
->depth
1834 || loop_b
->depth
< loop_first
->depth
)
1837 if (loop_nb_a
!= loop_nb_b
1838 && !flow_loop_nested_p (loop_a
, loop_b
)
1839 && !flow_loop_nested_p (loop_b
, loop_a
))
1841 /* Example: when there are two consecutive loops,
1850 the dependence relation cannot be captured by the
1851 distance abstraction. */
1852 non_affine_dependence_relation (ddr
);
1856 /* The dependence is carried by the outermost loop. Example:
1863 In this case, the dependence is carried by loop_1. */
1864 loop_nb
= loop_nb_a
< loop_nb_b
? loop_nb_a
: loop_nb_b
;
1865 loop_nb
-= first_loop
;
1867 /* If the loop number is still greater than the number of
1868 loops we've been asked to analyze, or negative,
1869 something is borked. */
1870 gcc_assert (loop_nb
>= 0);
1871 gcc_assert (loop_nb
< nb_loops
);
1872 if (chrec_contains_undetermined (SUB_DISTANCE (subscript
)))
1874 non_affine_dependence_relation (ddr
);
1878 dist
= int_cst_value (SUB_DISTANCE (subscript
));
1880 /* This is the subscript coupling test.
1885 There is no dependence. */
1886 if (init_v
[loop_nb
] != 0
1887 && dist_v
[loop_nb
] != dist
)
1889 finalize_ddr_dependent (ddr
, chrec_known
);
1893 dist_v
[loop_nb
] = dist
;
1894 init_v
[loop_nb
] = 1;
1898 /* There is a distance of 1 on all the outer loops:
1900 Example: there is a dependence of distance 1 on loop_1 for the array A.
1906 struct loop
*lca
, *loop_a
, *loop_b
;
1907 struct data_reference
*a
= DDR_A (ddr
);
1908 struct data_reference
*b
= DDR_B (ddr
);
1910 loop_a
= loop_containing_stmt (DR_STMT (a
));
1911 loop_b
= loop_containing_stmt (DR_STMT (b
));
1913 /* Get the common ancestor loop. */
1914 lca
= find_common_loop (loop_a
, loop_b
);
1917 lca_nb
-= first_loop
;
1918 gcc_assert (lca_nb
>= 0);
1919 gcc_assert (lca_nb
< nb_loops
);
1921 /* For each outer loop where init_v is not set, the accesses are
1922 in dependence of distance 1 in the loop. */
1925 && init_v
[lca_nb
] == 0)
1932 lca_nb
= lca
->num
- first_loop
;
1933 while (lca
->depth
!= 0)
1935 /* If we're considering just a sub-nest, then don't record
1936 any information on the outer loops. */
1940 gcc_assert (lca_nb
< nb_loops
);
1942 if (init_v
[lca_nb
] == 0)
1945 lca_nb
= lca
->num
- first_loop
;
1951 DDR_DIST_VECT (ddr
) = dist_v
;
1952 DDR_SIZE_VECT (ddr
) = nb_loops
;
1956 /* Compute the classic per loop direction vector.
1958 DDR is the data dependence relation to build a vector from.
1959 NB_LOOPS is the total number of loops we are considering.
1960 FIRST_LOOP is the loop->num of the first loop in the analyzed
1962 Return FALSE if the dependence relation is outside of the loop nest
1963 starting with FIRST_LOOP.
1964 Return TRUE otherwise. */
1967 build_classic_dir_vector (struct data_dependence_relation
*ddr
,
1968 int nb_loops
, unsigned int first_loop
)
1971 lambda_vector dir_v
, init_v
;
1973 dir_v
= lambda_vector_new (nb_loops
);
1974 init_v
= lambda_vector_new (nb_loops
);
1975 lambda_vector_clear (dir_v
, nb_loops
);
1976 lambda_vector_clear (init_v
, nb_loops
);
1978 if (DDR_ARE_DEPENDENT (ddr
) != NULL_TREE
)
1981 for (i
= 0; i
< DDR_NUM_SUBSCRIPTS (ddr
); i
++)
1983 tree access_fn_a
, access_fn_b
;
1984 struct subscript
*subscript
= DDR_SUBSCRIPT (ddr
, i
);
1986 if (chrec_contains_undetermined (SUB_DISTANCE (subscript
)))
1988 non_affine_dependence_relation (ddr
);
1992 access_fn_a
= DR_ACCESS_FN (DDR_A (ddr
), i
);
1993 access_fn_b
= DR_ACCESS_FN (DDR_B (ddr
), i
);
1994 if (TREE_CODE (access_fn_a
) == POLYNOMIAL_CHREC
1995 && TREE_CODE (access_fn_b
) == POLYNOMIAL_CHREC
)
1998 enum data_dependence_direction dir
= dir_star
;
1999 int loop_nb_a
= CHREC_VARIABLE (access_fn_a
);
2000 int loop_nb_b
= CHREC_VARIABLE (access_fn_b
);
2001 struct loop
*loop_a
= current_loops
->parray
[loop_nb_a
];
2002 struct loop
*loop_b
= current_loops
->parray
[loop_nb_b
];
2003 struct loop
*loop_first
= current_loops
->parray
[first_loop
];
2005 /* If the loop for either variable is at a lower depth than
2006 the first_loop's depth, then we can't possibly have a
2007 dependency at this level of the loop. */
2009 if (loop_a
->depth
< loop_first
->depth
2010 || loop_b
->depth
< loop_first
->depth
)
2013 if (loop_nb_a
!= loop_nb_b
2014 && !flow_loop_nested_p (loop_a
, loop_b
)
2015 && !flow_loop_nested_p (loop_b
, loop_a
))
2017 /* Example: when there are two consecutive loops,
2026 the dependence relation cannot be captured by the
2027 distance abstraction. */
2028 non_affine_dependence_relation (ddr
);
2032 /* The dependence is carried by the outermost loop. Example:
2039 In this case, the dependence is carried by loop_1. */
2040 loop_nb
= loop_nb_a
< loop_nb_b
? loop_nb_a
: loop_nb_b
;
2041 loop_nb
-= first_loop
;
2043 /* If the loop number is still greater than the number of
2044 loops we've been asked to analyze, or negative,
2045 something is borked. */
2046 gcc_assert (loop_nb
>= 0);
2047 gcc_assert (loop_nb
< nb_loops
);
2049 if (chrec_contains_undetermined (SUB_DISTANCE (subscript
)))
2051 non_affine_dependence_relation (ddr
);
2055 dist
= int_cst_value (SUB_DISTANCE (subscript
));
2064 /* This is the subscript coupling test.
2069 There is no dependence. */
2070 if (init_v
[loop_nb
] != 0
2072 && (enum data_dependence_direction
) dir_v
[loop_nb
] != dir
2073 && (enum data_dependence_direction
) dir_v
[loop_nb
] != dir_star
)
2075 finalize_ddr_dependent (ddr
, chrec_known
);
2079 dir_v
[loop_nb
] = dir
;
2080 init_v
[loop_nb
] = 1;
2084 /* There is a distance of 1 on all the outer loops:
2086 Example: there is a dependence of distance 1 on loop_1 for the array A.
2092 struct loop
*lca
, *loop_a
, *loop_b
;
2093 struct data_reference
*a
= DDR_A (ddr
);
2094 struct data_reference
*b
= DDR_B (ddr
);
2096 loop_a
= loop_containing_stmt (DR_STMT (a
));
2097 loop_b
= loop_containing_stmt (DR_STMT (b
));
2099 /* Get the common ancestor loop. */
2100 lca
= find_common_loop (loop_a
, loop_b
);
2101 lca_nb
= lca
->num
- first_loop
;
2103 gcc_assert (lca_nb
>= 0);
2104 gcc_assert (lca_nb
< nb_loops
);
2106 /* For each outer loop where init_v is not set, the accesses are
2107 in dependence of distance 1 in the loop. */
2110 && init_v
[lca_nb
] == 0)
2111 dir_v
[lca_nb
] = dir_positive
;
2116 lca_nb
= lca
->num
- first_loop
;
2117 while (lca
->depth
!= 0)
2119 /* If we're considering just a sub-nest, then don't record
2120 any information on the outer loops. */
2124 gcc_assert (lca_nb
< nb_loops
);
2126 if (init_v
[lca_nb
] == 0)
2127 dir_v
[lca_nb
] = dir_positive
;
2129 lca_nb
= lca
->num
- first_loop
;
2135 DDR_DIR_VECT (ddr
) = dir_v
;
2136 DDR_SIZE_VECT (ddr
) = nb_loops
;
2140 /* Returns true when all the access functions of A are affine or
2144 access_functions_are_affine_or_constant_p (struct data_reference
*a
)
2147 varray_type fns
= DR_ACCESS_FNS (a
);
2149 for (i
= 0; i
< VARRAY_ACTIVE_SIZE (fns
); i
++)
2150 if (!evolution_function_is_constant_p (VARRAY_TREE (fns
, i
))
2151 && !evolution_function_is_affine_multivariate_p (VARRAY_TREE (fns
, i
)))
2157 /* This computes the affine dependence relation between A and B.
2158 CHREC_KNOWN is used for representing the independence between two
2159 accesses, while CHREC_DONT_KNOW is used for representing the unknown
2162 Note that it is possible to stop the computation of the dependence
2163 relation the first time we detect a CHREC_KNOWN element for a given
2167 compute_affine_dependence (struct data_dependence_relation
*ddr
)
2169 struct data_reference
*dra
= DDR_A (ddr
);
2170 struct data_reference
*drb
= DDR_B (ddr
);
2172 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2174 fprintf (dump_file
, "(compute_affine_dependence\n");
2175 fprintf (dump_file
, " (stmt_a = \n");
2176 print_generic_expr (dump_file
, DR_STMT (dra
), 0);
2177 fprintf (dump_file
, ")\n (stmt_b = \n");
2178 print_generic_expr (dump_file
, DR_STMT (drb
), 0);
2179 fprintf (dump_file
, ")\n");
2182 /* Analyze only when the dependence relation is not yet known. */
2183 if (DDR_ARE_DEPENDENT (ddr
) == NULL_TREE
)
2185 if (access_functions_are_affine_or_constant_p (dra
)
2186 && access_functions_are_affine_or_constant_p (drb
))
2187 subscript_dependence_tester (ddr
);
2189 /* As a last case, if the dependence cannot be determined, or if
2190 the dependence is considered too difficult to determine, answer
2193 finalize_ddr_dependent (ddr
, chrec_dont_know
);
2196 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2197 fprintf (dump_file
, ")\n");
2200 /* Compute a subset of the data dependence relation graph. Don't
2201 compute read-read relations, and avoid the computation of the
2202 opposite relation, i.e. when AB has been computed, don't compute BA.
2203 DATAREFS contains a list of data references, and the result is set
2204 in DEPENDENCE_RELATIONS. */
2207 compute_all_dependences (varray_type datarefs
,
2208 varray_type
*dependence_relations
)
2210 unsigned int i
, j
, N
;
2212 N
= VARRAY_ACTIVE_SIZE (datarefs
);
2214 for (i
= 0; i
< N
; i
++)
2215 for (j
= i
; j
< N
; j
++)
2217 struct data_reference
*a
, *b
;
2218 struct data_dependence_relation
*ddr
;
2220 a
= VARRAY_GENERIC_PTR (datarefs
, i
);
2221 b
= VARRAY_GENERIC_PTR (datarefs
, j
);
2222 ddr
= initialize_data_dependence_relation (a
, b
);
2224 VARRAY_PUSH_GENERIC_PTR (*dependence_relations
, ddr
);
2225 compute_affine_dependence (ddr
);
2226 compute_subscript_distance (ddr
);
2230 /* Search the data references in LOOP, and record the information into
2231 DATAREFS. Returns chrec_dont_know when failing to analyze a
2232 difficult case, returns NULL_TREE otherwise.
2234 TODO: This function should be made smarter so that it can handle address
2235 arithmetic as if they were array accesses, etc. */
2238 find_data_references_in_loop (struct loop
*loop
, varray_type
*datarefs
)
2240 bool dont_know_node_not_inserted
= true;
2241 basic_block bb
, *bbs
;
2243 block_stmt_iterator bsi
;
2245 bbs
= get_loop_body (loop
);
2247 for (i
= 0; i
< loop
->num_nodes
; i
++)
2251 for (bsi
= bsi_start (bb
); !bsi_end_p (bsi
); bsi_next (&bsi
))
2253 tree stmt
= bsi_stmt (bsi
);
2254 stmt_ann_t ann
= stmt_ann (stmt
);
2256 if (TREE_CODE (stmt
) != MODIFY_EXPR
)
2260 && !V_MUST_DEF_OPS (ann
)
2261 && !V_MAY_DEF_OPS (ann
))
2264 /* In the GIMPLE representation, a modify expression
2265 contains a single load or store to memory. */
2266 if (TREE_CODE (TREE_OPERAND (stmt
, 0)) == ARRAY_REF
)
2267 VARRAY_PUSH_GENERIC_PTR
2268 (*datarefs
, analyze_array (stmt
, TREE_OPERAND (stmt
, 0),
2271 else if (TREE_CODE (TREE_OPERAND (stmt
, 1)) == ARRAY_REF
)
2272 VARRAY_PUSH_GENERIC_PTR
2273 (*datarefs
, analyze_array (stmt
, TREE_OPERAND (stmt
, 1),
2277 if (dont_know_node_not_inserted
)
2279 struct data_reference
*res
;
2280 res
= xmalloc (sizeof (struct data_reference
));
2281 DR_STMT (res
) = NULL_TREE
;
2282 DR_REF (res
) = NULL_TREE
;
2283 DR_ACCESS_FNS (res
) = NULL
;
2284 DR_BASE_NAME (res
) = NULL
;
2285 DR_IS_READ (res
) = false;
2286 VARRAY_PUSH_GENERIC_PTR (*datarefs
, res
);
2287 dont_know_node_not_inserted
= false;
2291 /* When there are no defs in the loop, the loop is parallel. */
2292 if (NUM_V_MAY_DEFS (STMT_V_MAY_DEF_OPS (stmt
)) > 0
2293 || NUM_V_MUST_DEFS (STMT_V_MUST_DEF_OPS (stmt
)) > 0)
2294 bb
->loop_father
->parallel_p
= false;
2297 if (bb
->loop_father
->estimated_nb_iterations
== NULL_TREE
)
2298 compute_estimated_nb_iterations (bb
->loop_father
);
2303 return dont_know_node_not_inserted
? NULL_TREE
: chrec_dont_know
;
2308 /* This section contains all the entry points. */
2310 /* Given a loop nest LOOP, the following vectors are returned:
2311 *DATAREFS is initialized to all the array elements contained in this loop,
2312 *DEPENDENCE_RELATIONS contains the relations between the data references. */
2315 compute_data_dependences_for_loop (unsigned nb_loops
,
2317 varray_type
*datarefs
,
2318 varray_type
*dependence_relations
)
2321 varray_type allrelations
;
2323 /* If one of the data references is not computable, give up without
2324 spending time to compute other dependences. */
2325 if (find_data_references_in_loop (loop
, datarefs
) == chrec_dont_know
)
2327 struct data_dependence_relation
*ddr
;
2329 /* Insert a single relation into dependence_relations:
2331 ddr
= initialize_data_dependence_relation (NULL
, NULL
);
2332 VARRAY_PUSH_GENERIC_PTR (*dependence_relations
, ddr
);
2333 build_classic_dist_vector (ddr
, nb_loops
, loop
->num
);
2334 build_classic_dir_vector (ddr
, nb_loops
, loop
->num
);
2338 VARRAY_GENERIC_PTR_INIT (allrelations
, 1, "Data dependence relations");
2339 compute_all_dependences (*datarefs
, &allrelations
);
2341 for (i
= 0; i
< VARRAY_ACTIVE_SIZE (allrelations
); i
++)
2343 struct data_dependence_relation
*ddr
;
2344 ddr
= VARRAY_GENERIC_PTR (allrelations
, i
);
2345 if (build_classic_dist_vector (ddr
, nb_loops
, loop
->num
))
2347 VARRAY_PUSH_GENERIC_PTR (*dependence_relations
, ddr
);
2348 build_classic_dir_vector (ddr
, nb_loops
, loop
->num
);
2353 /* Entry point (for testing only). Analyze all the data references
2354 and the dependence relations.
2356 The data references are computed first.
2358 A relation on these nodes is represented by a complete graph. Some
2359 of the relations could be of no interest, thus the relations can be
2362 In the following function we compute all the relations. This is
2363 just a first implementation that is here for:
2364 - for showing how to ask for the dependence relations,
2365 - for the debugging the whole dependence graph,
2366 - for the dejagnu testcases and maintenance.
2368 It is possible to ask only for a part of the graph, avoiding to
2369 compute the whole dependence graph. The computed dependences are
2370 stored in a knowledge base (KB) such that later queries don't
2371 recompute the same information. The implementation of this KB is
2372 transparent to the optimizer, and thus the KB can be changed with a
2373 more efficient implementation, or the KB could be disabled. */
2376 analyze_all_data_dependences (struct loops
*loops
)
2379 varray_type datarefs
;
2380 varray_type dependence_relations
;
2381 int nb_data_refs
= 10;
2383 VARRAY_GENERIC_PTR_INIT (datarefs
, nb_data_refs
, "datarefs");
2384 VARRAY_GENERIC_PTR_INIT (dependence_relations
,
2385 nb_data_refs
* nb_data_refs
,
2386 "dependence_relations");
2388 /* Compute DDs on the whole function. */
2389 compute_data_dependences_for_loop (loops
->num
, loops
->parray
[0],
2390 &datarefs
, &dependence_relations
);
2394 dump_data_dependence_relations (dump_file
, dependence_relations
);
2395 fprintf (dump_file
, "\n\n");
2397 if (dump_flags
& TDF_DETAILS
)
2398 dump_dist_dir_vectors (dump_file
, dependence_relations
);
2400 if (dump_flags
& TDF_STATS
)
2402 unsigned nb_top_relations
= 0;
2403 unsigned nb_bot_relations
= 0;
2404 unsigned nb_basename_differ
= 0;
2405 unsigned nb_chrec_relations
= 0;
2407 for (i
= 0; i
< VARRAY_ACTIVE_SIZE (dependence_relations
); i
++)
2409 struct data_dependence_relation
*ddr
;
2410 ddr
= VARRAY_GENERIC_PTR (dependence_relations
, i
);
2412 if (chrec_contains_undetermined (DDR_ARE_DEPENDENT (ddr
)))
2415 else if (DDR_ARE_DEPENDENT (ddr
) == chrec_known
)
2417 struct data_reference
*a
= DDR_A (ddr
);
2418 struct data_reference
*b
= DDR_B (ddr
);
2421 if (DR_NUM_DIMENSIONS (a
) != DR_NUM_DIMENSIONS (b
)
2422 || (array_base_name_differ_p (a
, b
, &differ_p
) && differ_p
))
2423 nb_basename_differ
++;
2429 nb_chrec_relations
++;
2432 gather_stats_on_scev_database ();
2436 free_dependence_relations (dependence_relations
);
2437 free_data_refs (datarefs
);
2440 /* Free the memory used by a data dependence relation DDR. */
2443 free_dependence_relation (struct data_dependence_relation
*ddr
)
2448 if (DDR_ARE_DEPENDENT (ddr
) == NULL_TREE
&& DDR_SUBSCRIPTS (ddr
))
2449 varray_clear (DDR_SUBSCRIPTS (ddr
));
2453 /* Free the memory used by the data dependence relations from
2454 DEPENDENCE_RELATIONS. */
2457 free_dependence_relations (varray_type dependence_relations
)
2460 if (dependence_relations
== NULL
)
2463 for (i
= 0; i
< VARRAY_ACTIVE_SIZE (dependence_relations
); i
++)
2464 free_dependence_relation (VARRAY_GENERIC_PTR (dependence_relations
, i
));
2465 varray_clear (dependence_relations
);
2468 /* Free the memory used by the data references from DATAREFS. */
2471 free_data_refs (varray_type datarefs
)
2475 if (datarefs
== NULL
)
2478 for (i
= 0; i
< VARRAY_ACTIVE_SIZE (datarefs
); i
++)
2480 struct data_reference
*dr
= (struct data_reference
*)
2481 VARRAY_GENERIC_PTR (datarefs
, i
);
2482 if (dr
&& DR_ACCESS_FNS (dr
))
2483 varray_clear (DR_ACCESS_FNS (dr
));
2485 varray_clear (datarefs
);