1 /* Data references and dependences detectors.
2 Copyright (C) 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
3 Contributed by Sebastian Pop <pop@cri.ensmp.fr>
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, 51 Franklin Street, Fifth Floor, 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"
84 /* These RTL headers are needed for basic-block.h. */
86 #include "basic-block.h"
87 #include "diagnostic.h"
88 #include "tree-flow.h"
89 #include "tree-dump.h"
92 #include "tree-chrec.h"
93 #include "tree-data-ref.h"
94 #include "tree-scalar-evolution.h"
95 #include "tree-pass.h"
97 static struct datadep_stats
99 int num_dependence_tests;
100 int num_dependence_dependent;
101 int num_dependence_independent;
102 int num_dependence_undetermined;
104 int num_subscript_tests;
105 int num_subscript_undetermined;
106 int num_same_subscript_function;
109 int num_ziv_independent;
110 int num_ziv_dependent;
111 int num_ziv_unimplemented;
114 int num_siv_independent;
115 int num_siv_dependent;
116 int num_siv_unimplemented;
119 int num_miv_independent;
120 int num_miv_dependent;
121 int num_miv_unimplemented;
124 static tree object_analysis (tree, tree, bool, struct data_reference **,
125 tree *, tree *, tree *, tree *, tree *,
126 struct ptr_info_def **, subvar_t *);
127 static struct data_reference * init_data_ref (tree, tree, tree, tree, bool,
128 tree, tree, tree, tree, tree,
129 struct ptr_info_def *,
131 static bool subscript_dependence_tester_1 (struct data_dependence_relation *,
132 struct data_reference *,
133 struct data_reference *);
135 /* Determine if PTR and DECL may alias, the result is put in ALIASED.
136 Return FALSE if there is no symbol memory tag for PTR. */
139 ptr_decl_may_alias_p (tree ptr, tree decl,
140 struct data_reference *ptr_dr,
145 gcc_assert (TREE_CODE (ptr) == SSA_NAME && DECL_P (decl));
147 tag = get_var_ann (SSA_NAME_VAR (ptr))->symbol_mem_tag;
149 tag = DR_MEMTAG (ptr_dr);
153 *aliased = is_aliased_with (tag, decl);
158 /* Determine if two pointers may alias, the result is put in ALIASED.
159 Return FALSE if there is no symbol memory tag for one of the pointers. */
162 ptr_ptr_may_alias_p (tree ptr_a, tree ptr_b,
163 struct data_reference *dra,
164 struct data_reference *drb,
169 tag_a = get_var_ann (SSA_NAME_VAR (ptr_a))->symbol_mem_tag;
171 tag_a = DR_MEMTAG (dra);
174 tag_b = get_var_ann (SSA_NAME_VAR (ptr_b))->symbol_mem_tag;
176 tag_b = DR_MEMTAG (drb);
179 *aliased = (tag_a == tag_b);
184 /* Determine if BASE_A and BASE_B may alias, the result is put in ALIASED.
185 Return FALSE if there is no symbol memory tag for one of the symbols. */
188 may_alias_p (tree base_a, tree base_b,
189 struct data_reference *dra,
190 struct data_reference *drb,
193 if (TREE_CODE (base_a) == ADDR_EXPR || TREE_CODE (base_b) == ADDR_EXPR)
195 if (TREE_CODE (base_a) == ADDR_EXPR && TREE_CODE (base_b) == ADDR_EXPR)
197 *aliased = (TREE_OPERAND (base_a, 0) == TREE_OPERAND (base_b, 0));
200 if (TREE_CODE (base_a) == ADDR_EXPR)
201 return ptr_decl_may_alias_p (base_b, TREE_OPERAND (base_a, 0), drb,
204 return ptr_decl_may_alias_p (base_a, TREE_OPERAND (base_b, 0), dra,
208 return ptr_ptr_may_alias_p (base_a, base_b, dra, drb, aliased);
212 /* Determine if a pointer (BASE_A) and a record/union access (BASE_B)
213 are not aliased. Return TRUE if they differ. */
215 record_ptr_differ_p (struct data_reference *dra,
216 struct data_reference *drb)
219 tree base_a = DR_BASE_OBJECT (dra);
220 tree base_b = DR_BASE_OBJECT (drb);
222 if (TREE_CODE (base_b) != COMPONENT_REF)
225 /* Peel COMPONENT_REFs to get to the base. Do not peel INDIRECT_REFs.
226 For a.b.c.d[i] we will get a, and for a.b->c.d[i] we will get a.b.
227 Probably will be unnecessary with struct alias analysis. */
228 while (TREE_CODE (base_b) == COMPONENT_REF)
229 base_b = TREE_OPERAND (base_b, 0);
230 /* Compare a record/union access (b.c[i] or p->c[i]) and a pointer
232 if (TREE_CODE (base_a) == INDIRECT_REF
233 && ((TREE_CODE (base_b) == VAR_DECL
234 && (ptr_decl_may_alias_p (TREE_OPERAND (base_a, 0), base_b, dra,
237 || (TREE_CODE (base_b) == INDIRECT_REF
238 && (ptr_ptr_may_alias_p (TREE_OPERAND (base_a, 0),
239 TREE_OPERAND (base_b, 0), dra, drb,
248 /* Determine if an array access (BASE_A) and a record/union access (BASE_B)
249 are not aliased. Return TRUE if they differ. */
251 record_array_differ_p (struct data_reference *dra,
252 struct data_reference *drb)
255 tree base_a = DR_BASE_OBJECT (dra);
256 tree base_b = DR_BASE_OBJECT (drb);
258 if (TREE_CODE (base_b) != COMPONENT_REF)
261 /* Peel COMPONENT_REFs to get to the base. Do not peel INDIRECT_REFs.
262 For a.b.c.d[i] we will get a, and for a.b->c.d[i] we will get a.b.
263 Probably will be unnecessary with struct alias analysis. */
264 while (TREE_CODE (base_b) == COMPONENT_REF)
265 base_b = TREE_OPERAND (base_b, 0);
267 /* Compare a record/union access (b.c[i] or p->c[i]) and an array access
268 (a[i]). In case of p->c[i] use alias analysis to verify that p is not
270 if (TREE_CODE (base_a) == VAR_DECL
271 && (TREE_CODE (base_b) == VAR_DECL
272 || (TREE_CODE (base_b) == INDIRECT_REF
273 && (ptr_decl_may_alias_p (TREE_OPERAND (base_b, 0), base_a, drb,
282 /* Determine if an array access (BASE_A) and a pointer (BASE_B)
283 are not aliased. Return TRUE if they differ. */
285 array_ptr_differ_p (tree base_a, tree base_b,
286 struct data_reference *drb)
290 /* In case one of the bases is a pointer (a[i] and (*p)[i]), we check with the
291 help of alias analysis that p is not pointing to a. */
292 if (TREE_CODE (base_a) == VAR_DECL && TREE_CODE (base_b) == INDIRECT_REF
293 && (ptr_decl_may_alias_p (TREE_OPERAND (base_b, 0), base_a, drb, &aliased)
301 /* This is the simplest data dependence test: determines whether the
302 data references A and B access the same array/region. Returns
303 false when the property is not computable at compile time.
304 Otherwise return true, and DIFFER_P will record the result. This
305 utility will not be necessary when alias_sets_conflict_p will be
306 less conservative. */
309 base_object_differ_p (struct data_reference *a,
310 struct data_reference *b,
313 tree base_a = DR_BASE_OBJECT (a);
314 tree base_b = DR_BASE_OBJECT (b);
317 if (!base_a || !base_b)
320 /* Determine if same base. Example: for the array accesses
321 a[i], b[i] or pointer accesses *a, *b, bases are a, b. */
322 if (base_a == base_b)
328 /* For pointer based accesses, (*p)[i], (*q)[j], the bases are (*p)
330 if (TREE_CODE (base_a) == INDIRECT_REF && TREE_CODE (base_b) == INDIRECT_REF
331 && TREE_OPERAND (base_a, 0) == TREE_OPERAND (base_b, 0))
337 /* Record/union based accesses - s.a[i], t.b[j]. bases are s.a,t.b. */
338 if (TREE_CODE (base_a) == COMPONENT_REF && TREE_CODE (base_b) == COMPONENT_REF
339 && TREE_OPERAND (base_a, 0) == TREE_OPERAND (base_b, 0)
340 && TREE_OPERAND (base_a, 1) == TREE_OPERAND (base_b, 1))
347 /* Determine if different bases. */
349 /* At this point we know that base_a != base_b. However, pointer
350 accesses of the form x=(*p) and y=(*q), whose bases are p and q,
351 may still be pointing to the same base. In SSAed GIMPLE p and q will
352 be SSA_NAMES in this case. Therefore, here we check if they are
353 really two different declarations. */
354 if (TREE_CODE (base_a) == VAR_DECL && TREE_CODE (base_b) == VAR_DECL)
360 /* In case one of the bases is a pointer (a[i] and (*p)[i]), we check with the
361 help of alias analysis that p is not pointing to a. */
362 if (array_ptr_differ_p (base_a, base_b, b)
363 || array_ptr_differ_p (base_b, base_a, a))
369 /* If the bases are pointers ((*q)[i] and (*p)[i]), we check with the
370 help of alias analysis they don't point to the same bases. */
371 if (TREE_CODE (base_a) == INDIRECT_REF && TREE_CODE (base_b) == INDIRECT_REF
372 && (may_alias_p (TREE_OPERAND (base_a, 0), TREE_OPERAND (base_b, 0), a, b,
380 /* Compare two record/union bases s.a and t.b: s != t or (a != b and
381 s and t are not unions). */
382 if (TREE_CODE (base_a) == COMPONENT_REF && TREE_CODE (base_b) == COMPONENT_REF
383 && ((TREE_CODE (TREE_OPERAND (base_a, 0)) == VAR_DECL
384 && TREE_CODE (TREE_OPERAND (base_b, 0)) == VAR_DECL
385 && TREE_OPERAND (base_a, 0) != TREE_OPERAND (base_b, 0))
386 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (base_a, 0))) == RECORD_TYPE
387 && TREE_CODE (TREE_TYPE (TREE_OPERAND (base_b, 0))) == RECORD_TYPE
388 && TREE_OPERAND (base_a, 1) != TREE_OPERAND (base_b, 1))))
394 /* Compare a record/union access (b.c[i] or p->c[i]) and a pointer
396 if (record_ptr_differ_p (a, b) || record_ptr_differ_p (b, a))
402 /* Compare a record/union access (b.c[i] or p->c[i]) and an array access
403 (a[i]). In case of p->c[i] use alias analysis to verify that p is not
405 if (record_array_differ_p (a, b) || record_array_differ_p (b, a))
414 /* Function base_addr_differ_p.
416 This is the simplest data dependence test: determines whether the
417 data references DRA and DRB access the same array/region. Returns
418 false when the property is not computable at compile time.
419 Otherwise return true, and DIFFER_P will record the result.
422 1. if (both DRA and DRB are represented as arrays)
423 compare DRA.BASE_OBJECT and DRB.BASE_OBJECT
424 2. else if (both DRA and DRB are represented as pointers)
425 try to prove that DRA.FIRST_LOCATION == DRB.FIRST_LOCATION
426 3. else if (DRA and DRB are represented differently or 2. fails)
427 only try to prove that the bases are surely different
431 base_addr_differ_p (struct data_reference *dra,
432 struct data_reference *drb,
435 tree addr_a = DR_BASE_ADDRESS (dra);
436 tree addr_b = DR_BASE_ADDRESS (drb);
440 if (!addr_a || !addr_b)
443 type_a = TREE_TYPE (addr_a);
444 type_b = TREE_TYPE (addr_b);
446 gcc_assert (POINTER_TYPE_P (type_a) && POINTER_TYPE_P (type_b));
448 /* 1. if (both DRA and DRB are represented as arrays)
449 compare DRA.BASE_OBJECT and DRB.BASE_OBJECT. */
450 if (DR_TYPE (dra) == ARRAY_REF_TYPE && DR_TYPE (drb) == ARRAY_REF_TYPE)
451 return base_object_differ_p (dra, drb, differ_p);
453 /* 2. else if (both DRA and DRB are represented as pointers)
454 try to prove that DRA.FIRST_LOCATION == DRB.FIRST_LOCATION. */
455 /* If base addresses are the same, we check the offsets, since the access of
456 the data-ref is described by {base addr + offset} and its access function,
457 i.e., in order to decide whether the bases of data-refs are the same we
458 compare both base addresses and offsets. */
459 if (DR_TYPE (dra) == POINTER_REF_TYPE && DR_TYPE (drb) == POINTER_REF_TYPE
461 || (TREE_CODE (addr_a) == ADDR_EXPR && TREE_CODE (addr_b) == ADDR_EXPR
462 && TREE_OPERAND (addr_a, 0) == TREE_OPERAND (addr_b, 0))))
464 /* Compare offsets. */
465 tree offset_a = DR_OFFSET (dra);
466 tree offset_b = DR_OFFSET (drb);
468 STRIP_NOPS (offset_a);
469 STRIP_NOPS (offset_b);
471 /* FORNOW: we only compare offsets that are MULT_EXPR, i.e., we don't handle
473 if (offset_a == offset_b
474 || (TREE_CODE (offset_a) == MULT_EXPR
475 && TREE_CODE (offset_b) == MULT_EXPR
476 && TREE_OPERAND (offset_a, 0) == TREE_OPERAND (offset_b, 0)
477 && TREE_OPERAND (offset_a, 1) == TREE_OPERAND (offset_b, 1)))
484 /* 3. else if (DRA and DRB are represented differently or 2. fails)
485 only try to prove that the bases are surely different. */
487 /* Apply alias analysis. */
488 if (may_alias_p (addr_a, addr_b, dra, drb, &aliased) && !aliased)
494 /* An instruction writing through a restricted pointer is "independent" of any
495 instruction reading or writing through a different pointer, in the same
497 else if ((TYPE_RESTRICT (type_a) && !DR_IS_READ (dra))
498 || (TYPE_RESTRICT (type_b) && !DR_IS_READ (drb)))
506 /* Returns true iff A divides B. */
509 tree_fold_divides_p (tree a,
512 /* Determines whether (A == gcd (A, B)). */
513 return tree_int_cst_equal (a, tree_fold_gcd (a, b));
516 /* Returns true iff A divides B. */
519 int_divides_p (int a, int b)
521 return ((b % a) == 0);
526 /* Dump into FILE all the data references from DATAREFS. */
529 dump_data_references (FILE *file,
530 varray_type datarefs)
534 for (i = 0; i < VARRAY_ACTIVE_SIZE (datarefs); i++)
535 dump_data_reference (file, VARRAY_GENERIC_PTR (datarefs, i));
538 /* Dump into FILE all the dependence relations from DDR. */
541 dump_data_dependence_relations (FILE *file,
546 for (i = 0; i < VARRAY_ACTIVE_SIZE (ddr); i++)
547 dump_data_dependence_relation (file, VARRAY_GENERIC_PTR (ddr, i));
550 /* Dump function for a DATA_REFERENCE structure. */
553 dump_data_reference (FILE *outf,
554 struct data_reference *dr)
558 fprintf (outf, "(Data Ref: \n stmt: ");
559 print_generic_stmt (outf, DR_STMT (dr), 0);
560 fprintf (outf, " ref: ");
561 print_generic_stmt (outf, DR_REF (dr), 0);
562 fprintf (outf, " base_object: ");
563 print_generic_stmt (outf, DR_BASE_OBJECT (dr), 0);
565 for (i = 0; i < DR_NUM_DIMENSIONS (dr); i++)
567 fprintf (outf, " Access function %d: ", i);
568 print_generic_stmt (outf, DR_ACCESS_FN (dr, i), 0);
570 fprintf (outf, ")\n");
573 /* Dump function for a SUBSCRIPT structure. */
576 dump_subscript (FILE *outf, struct subscript *subscript)
578 tree chrec = SUB_CONFLICTS_IN_A (subscript);
580 fprintf (outf, "\n (subscript \n");
581 fprintf (outf, " iterations_that_access_an_element_twice_in_A: ");
582 print_generic_stmt (outf, chrec, 0);
583 if (chrec == chrec_known)
584 fprintf (outf, " (no dependence)\n");
585 else if (chrec_contains_undetermined (chrec))
586 fprintf (outf, " (don't know)\n");
589 tree last_iteration = SUB_LAST_CONFLICT (subscript);
590 fprintf (outf, " last_conflict: ");
591 print_generic_stmt (outf, last_iteration, 0);
594 chrec = SUB_CONFLICTS_IN_B (subscript);
595 fprintf (outf, " iterations_that_access_an_element_twice_in_B: ");
596 print_generic_stmt (outf, chrec, 0);
597 if (chrec == chrec_known)
598 fprintf (outf, " (no dependence)\n");
599 else if (chrec_contains_undetermined (chrec))
600 fprintf (outf, " (don't know)\n");
603 tree last_iteration = SUB_LAST_CONFLICT (subscript);
604 fprintf (outf, " last_conflict: ");
605 print_generic_stmt (outf, last_iteration, 0);
608 fprintf (outf, " (Subscript distance: ");
609 print_generic_stmt (outf, SUB_DISTANCE (subscript), 0);
610 fprintf (outf, " )\n");
611 fprintf (outf, " )\n");
614 /* Print the classic direction vector DIRV to OUTF. */
617 print_direction_vector (FILE *outf,
623 for (eq = 0; eq < length; eq++)
625 enum data_dependence_direction dir = dirv[eq];
630 fprintf (outf, " +");
633 fprintf (outf, " -");
636 fprintf (outf, " =");
638 case dir_positive_or_equal:
639 fprintf (outf, " +=");
641 case dir_positive_or_negative:
642 fprintf (outf, " +-");
644 case dir_negative_or_equal:
645 fprintf (outf, " -=");
648 fprintf (outf, " *");
651 fprintf (outf, "indep");
655 fprintf (outf, "\n");
658 /* Print a vector of direction vectors. */
661 print_dir_vectors (FILE *outf, VEC (lambda_vector, heap) *dir_vects,
667 for (j = 0; VEC_iterate (lambda_vector, dir_vects, j, v); j++)
668 print_direction_vector (outf, v, length);
671 /* Print a vector of distance vectors. */
674 print_dist_vectors (FILE *outf, VEC (lambda_vector, heap) *dist_vects,
680 for (j = 0; VEC_iterate (lambda_vector, dist_vects, j, v); j++)
681 print_lambda_vector (outf, v, length);
687 debug_data_dependence_relation (struct data_dependence_relation *ddr)
689 dump_data_dependence_relation (stderr, ddr);
692 /* Dump function for a DATA_DEPENDENCE_RELATION structure. */
695 dump_data_dependence_relation (FILE *outf,
696 struct data_dependence_relation *ddr)
698 struct data_reference *dra, *drb;
702 fprintf (outf, "(Data Dep: \n");
703 if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
704 fprintf (outf, " (don't know)\n");
706 else if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
707 fprintf (outf, " (no dependence)\n");
709 else if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
714 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
716 fprintf (outf, " access_fn_A: ");
717 print_generic_stmt (outf, DR_ACCESS_FN (dra, i), 0);
718 fprintf (outf, " access_fn_B: ");
719 print_generic_stmt (outf, DR_ACCESS_FN (drb, i), 0);
720 dump_subscript (outf, DDR_SUBSCRIPT (ddr, i));
723 fprintf (outf, " loop nest: (");
724 for (i = 0; VEC_iterate (loop_p, DDR_LOOP_NEST (ddr), i, loopi); i++)
725 fprintf (outf, "%d ", loopi->num);
726 fprintf (outf, ")\n");
728 for (i = 0; i < DDR_NUM_DIST_VECTS (ddr); i++)
730 fprintf (outf, " distance_vector: ");
731 print_lambda_vector (outf, DDR_DIST_VECT (ddr, i),
735 for (i = 0; i < DDR_NUM_DIR_VECTS (ddr); i++)
737 fprintf (outf, " direction_vector: ");
738 print_direction_vector (outf, DDR_DIR_VECT (ddr, i),
743 fprintf (outf, ")\n");
746 /* Dump function for a DATA_DEPENDENCE_DIRECTION structure. */
749 dump_data_dependence_direction (FILE *file,
750 enum data_dependence_direction dir)
766 case dir_positive_or_negative:
767 fprintf (file, "+-");
770 case dir_positive_or_equal:
771 fprintf (file, "+=");
774 case dir_negative_or_equal:
775 fprintf (file, "-=");
787 /* Dumps the distance and direction vectors in FILE. DDRS contains
788 the dependence relations, and VECT_SIZE is the size of the
789 dependence vectors, or in other words the number of loops in the
793 dump_dist_dir_vectors (FILE *file, varray_type ddrs)
797 for (i = 0; i < VARRAY_ACTIVE_SIZE (ddrs); i++)
799 struct data_dependence_relation *ddr =
800 (struct data_dependence_relation *)
801 VARRAY_GENERIC_PTR (ddrs, i);
802 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE
803 && DDR_AFFINE_P (ddr))
805 for (j = 0; j < DDR_NUM_DIST_VECTS (ddr); j++)
807 fprintf (file, "DISTANCE_V (");
808 print_lambda_vector (file, DDR_DIST_VECT (ddr, j),
810 fprintf (file, ")\n");
813 for (j = 0; j < DDR_NUM_DIR_VECTS (ddr); j++)
815 fprintf (file, "DIRECTION_V (");
816 print_direction_vector (file, DDR_DIR_VECT (ddr, j),
818 fprintf (file, ")\n");
822 fprintf (file, "\n\n");
825 /* Dumps the data dependence relations DDRS in FILE. */
828 dump_ddrs (FILE *file, varray_type ddrs)
832 for (i = 0; i < VARRAY_ACTIVE_SIZE (ddrs); i++)
834 struct data_dependence_relation *ddr =
835 (struct data_dependence_relation *)
836 VARRAY_GENERIC_PTR (ddrs, i);
837 dump_data_dependence_relation (file, ddr);
839 fprintf (file, "\n\n");
844 /* Estimate the number of iterations from the size of the data and the
848 estimate_niter_from_size_of_data (struct loop *loop,
853 tree estimation = NULL_TREE;
854 tree array_size, data_size, element_size;
857 init = initial_condition (access_fn);
858 step = evolution_part_in_loop_num (access_fn, loop->num);
860 array_size = TYPE_SIZE (TREE_TYPE (opnd0));
861 element_size = TYPE_SIZE (TREE_TYPE (TREE_TYPE (opnd0)));
862 if (array_size == NULL_TREE
863 || TREE_CODE (array_size) != INTEGER_CST
864 || TREE_CODE (element_size) != INTEGER_CST)
867 data_size = fold_build2 (EXACT_DIV_EXPR, integer_type_node,
868 array_size, element_size);
870 if (init != NULL_TREE
872 && TREE_CODE (init) == INTEGER_CST
873 && TREE_CODE (step) == INTEGER_CST)
875 tree i_plus_s = fold_build2 (PLUS_EXPR, integer_type_node, init, step);
876 tree sign = fold_binary (GT_EXPR, boolean_type_node, i_plus_s, init);
878 if (sign == boolean_true_node)
879 estimation = fold_build2 (CEIL_DIV_EXPR, integer_type_node,
880 fold_build2 (MINUS_EXPR, integer_type_node,
881 data_size, init), step);
883 /* When the step is negative, as in PR23386: (init = 3, step =
884 0ffffffff, data_size = 100), we have to compute the
885 estimation as ceil_div (init, 0 - step) + 1. */
886 else if (sign == boolean_false_node)
888 fold_build2 (PLUS_EXPR, integer_type_node,
889 fold_build2 (CEIL_DIV_EXPR, integer_type_node,
891 fold_build2 (MINUS_EXPR, unsigned_type_node,
892 integer_zero_node, step)),
896 record_estimate (loop, estimation, boolean_true_node, stmt);
900 /* Given an ARRAY_REF node REF, records its access functions.
901 Example: given A[i][3], record in ACCESS_FNS the opnd1 function,
902 i.e. the constant "3", then recursively call the function on opnd0,
903 i.e. the ARRAY_REF "A[i]".
904 If ESTIMATE_ONLY is true, we just set the estimated number of loop
905 iterations, we don't store the access function.
906 The function returns the base name: "A". */
909 analyze_array_indexes (struct loop *loop,
910 VEC(tree,heap) **access_fns,
917 opnd0 = TREE_OPERAND (ref, 0);
918 opnd1 = TREE_OPERAND (ref, 1);
920 /* The detection of the evolution function for this data access is
921 postponed until the dependence test. This lazy strategy avoids
922 the computation of access functions that are of no interest for
924 access_fn = instantiate_parameters
925 (loop, analyze_scalar_evolution (loop, opnd1));
928 && chrec_contains_undetermined (loop->estimated_nb_iterations))
929 estimate_niter_from_size_of_data (loop, opnd0, access_fn, stmt);
932 VEC_safe_push (tree, heap, *access_fns, access_fn);
934 /* Recursively record other array access functions. */
935 if (TREE_CODE (opnd0) == ARRAY_REF)
936 return analyze_array_indexes (loop, access_fns, opnd0, stmt, estimate_only);
938 /* Return the base name of the data access. */
943 /* For an array reference REF contained in STMT, attempt to bound the
944 number of iterations in the loop containing STMT */
947 estimate_iters_using_array (tree stmt, tree ref)
949 analyze_array_indexes (loop_containing_stmt (stmt), NULL, ref, stmt,
953 /* For a data reference REF contained in the statement STMT, initialize
954 a DATA_REFERENCE structure, and return it. IS_READ flag has to be
955 set to true when REF is in the right hand side of an
958 struct data_reference *
959 analyze_array (tree stmt, tree ref, bool is_read)
961 struct data_reference *res;
962 VEC(tree,heap) *acc_fns;
964 if (dump_file && (dump_flags & TDF_DETAILS))
966 fprintf (dump_file, "(analyze_array \n");
967 fprintf (dump_file, " (ref = ");
968 print_generic_stmt (dump_file, ref, 0);
969 fprintf (dump_file, ")\n");
972 res = XNEW (struct data_reference);
974 DR_STMT (res) = stmt;
976 acc_fns = VEC_alloc (tree, heap, 3);
977 DR_BASE_OBJECT (res) = analyze_array_indexes
978 (loop_containing_stmt (stmt), &acc_fns, ref, stmt, false);
979 DR_TYPE (res) = ARRAY_REF_TYPE;
980 DR_SET_ACCESS_FNS (res, acc_fns);
981 DR_IS_READ (res) = is_read;
982 DR_BASE_ADDRESS (res) = NULL_TREE;
983 DR_OFFSET (res) = NULL_TREE;
984 DR_INIT (res) = NULL_TREE;
985 DR_STEP (res) = NULL_TREE;
986 DR_OFFSET_MISALIGNMENT (res) = NULL_TREE;
987 DR_MEMTAG (res) = NULL_TREE;
988 DR_PTR_INFO (res) = NULL;
990 if (dump_file && (dump_flags & TDF_DETAILS))
991 fprintf (dump_file, ")\n");
996 /* Analyze an indirect memory reference, REF, that comes from STMT.
997 IS_READ is true if this is an indirect load, and false if it is
999 Return a new data reference structure representing the indirect_ref, or
1000 NULL if we cannot describe the access function. */
1002 static struct data_reference *
1003 analyze_indirect_ref (tree stmt, tree ref, bool is_read)
1005 struct loop *loop = loop_containing_stmt (stmt);
1006 tree ptr_ref = TREE_OPERAND (ref, 0);
1007 tree access_fn = analyze_scalar_evolution (loop, ptr_ref);
1008 tree init = initial_condition_in_loop_num (access_fn, loop->num);
1009 tree base_address = NULL_TREE, evolution, step = NULL_TREE;
1010 struct ptr_info_def *ptr_info = NULL;
1012 if (TREE_CODE (ptr_ref) == SSA_NAME)
1013 ptr_info = SSA_NAME_PTR_INFO (ptr_ref);
1016 if (access_fn == chrec_dont_know || !init || init == chrec_dont_know)
1018 if (dump_file && (dump_flags & TDF_DETAILS))
1020 fprintf (dump_file, "\nBad access function of ptr: ");
1021 print_generic_expr (dump_file, ref, TDF_SLIM);
1022 fprintf (dump_file, "\n");
1027 if (dump_file && (dump_flags & TDF_DETAILS))
1029 fprintf (dump_file, "\nAccess function of ptr: ");
1030 print_generic_expr (dump_file, access_fn, TDF_SLIM);
1031 fprintf (dump_file, "\n");
1034 if (!expr_invariant_in_loop_p (loop, init))
1036 if (dump_file && (dump_flags & TDF_DETAILS))
1037 fprintf (dump_file, "\ninitial condition is not loop invariant.\n");
1041 base_address = init;
1042 evolution = evolution_part_in_loop_num (access_fn, loop->num);
1043 if (evolution != chrec_dont_know)
1046 step = ssize_int (0);
1049 if (TREE_CODE (evolution) == INTEGER_CST)
1050 step = fold_convert (ssizetype, evolution);
1052 if (dump_file && (dump_flags & TDF_DETAILS))
1053 fprintf (dump_file, "\nnon constant step for ptr access.\n");
1057 if (dump_file && (dump_flags & TDF_DETAILS))
1058 fprintf (dump_file, "\nunknown evolution of ptr.\n");
1060 return init_data_ref (stmt, ref, NULL_TREE, access_fn, is_read, base_address,
1061 NULL_TREE, step, NULL_TREE, NULL_TREE,
1062 ptr_info, POINTER_REF_TYPE);
1065 /* For a data reference REF contained in the statement STMT, initialize
1066 a DATA_REFERENCE structure, and return it. */
1068 struct data_reference *
1069 init_data_ref (tree stmt,
1079 struct ptr_info_def *ptr_info,
1080 enum data_ref_type type)
1082 struct data_reference *res;
1083 VEC(tree,heap) *acc_fns;
1085 if (dump_file && (dump_flags & TDF_DETAILS))
1087 fprintf (dump_file, "(init_data_ref \n");
1088 fprintf (dump_file, " (ref = ");
1089 print_generic_stmt (dump_file, ref, 0);
1090 fprintf (dump_file, ")\n");
1093 res = XNEW (struct data_reference);
1095 DR_STMT (res) = stmt;
1097 DR_BASE_OBJECT (res) = base;
1098 DR_TYPE (res) = type;
1099 acc_fns = VEC_alloc (tree, heap, 3);
1100 DR_SET_ACCESS_FNS (res, acc_fns);
1101 VEC_quick_push (tree, DR_ACCESS_FNS (res), access_fn);
1102 DR_IS_READ (res) = is_read;
1103 DR_BASE_ADDRESS (res) = base_address;
1104 DR_OFFSET (res) = init_offset;
1105 DR_INIT (res) = NULL_TREE;
1106 DR_STEP (res) = step;
1107 DR_OFFSET_MISALIGNMENT (res) = misalign;
1108 DR_MEMTAG (res) = memtag;
1109 DR_PTR_INFO (res) = ptr_info;
1111 if (dump_file && (dump_flags & TDF_DETAILS))
1112 fprintf (dump_file, ")\n");
1117 /* Function strip_conversions
1119 Strip conversions that don't narrow the mode. */
1122 strip_conversion (tree expr)
1124 tree to, ti, oprnd0;
1126 while (TREE_CODE (expr) == NOP_EXPR || TREE_CODE (expr) == CONVERT_EXPR)
1128 to = TREE_TYPE (expr);
1129 oprnd0 = TREE_OPERAND (expr, 0);
1130 ti = TREE_TYPE (oprnd0);
1132 if (!INTEGRAL_TYPE_P (to) || !INTEGRAL_TYPE_P (ti))
1134 if (GET_MODE_SIZE (TYPE_MODE (to)) < GET_MODE_SIZE (TYPE_MODE (ti)))
1143 /* Function analyze_offset_expr
1145 Given an offset expression EXPR received from get_inner_reference, analyze
1146 it and create an expression for INITIAL_OFFSET by substituting the variables
1147 of EXPR with initial_condition of the corresponding access_fn in the loop.
1150 for (j = 3; j < N; j++)
1153 For a[j].b[i][j], EXPR will be 'i * C_i + j * C_j + C'. 'i' cannot be
1154 substituted, since its access_fn in the inner loop is i. 'j' will be
1155 substituted with 3. An INITIAL_OFFSET will be 'i * C_i + C`', where
1158 Compute MISALIGN (the misalignment of the data reference initial access from
1159 its base). Misalignment can be calculated only if all the variables can be
1160 substituted with constants, otherwise, we record maximum possible alignment
1161 in ALIGNED_TO. In the above example, since 'i' cannot be substituted, MISALIGN
1162 will be NULL_TREE, and the biggest divider of C_i (a power of 2) will be
1163 recorded in ALIGNED_TO.
1165 STEP is an evolution of the data reference in this loop in bytes.
1166 In the above example, STEP is C_j.
1168 Return FALSE, if the analysis fails, e.g., there is no access_fn for a
1169 variable. In this case, all the outputs (INITIAL_OFFSET, MISALIGN, ALIGNED_TO
1170 and STEP) are NULL_TREEs. Otherwise, return TRUE.
1175 analyze_offset_expr (tree expr,
1177 tree *initial_offset,
1184 tree left_offset = ssize_int (0);
1185 tree right_offset = ssize_int (0);
1186 tree left_misalign = ssize_int (0);
1187 tree right_misalign = ssize_int (0);
1188 tree left_step = ssize_int (0);
1189 tree right_step = ssize_int (0);
1190 enum tree_code code;
1191 tree init, evolution;
1192 tree left_aligned_to = NULL_TREE, right_aligned_to = NULL_TREE;
1195 *misalign = NULL_TREE;
1196 *aligned_to = NULL_TREE;
1197 *initial_offset = NULL_TREE;
1199 /* Strip conversions that don't narrow the mode. */
1200 expr = strip_conversion (expr);
1206 if (TREE_CODE (expr) == INTEGER_CST)
1208 *initial_offset = fold_convert (ssizetype, expr);
1209 *misalign = fold_convert (ssizetype, expr);
1210 *step = ssize_int (0);
1214 /* 2. Variable. Try to substitute with initial_condition of the corresponding
1215 access_fn in the current loop. */
1216 if (SSA_VAR_P (expr))
1218 tree access_fn = analyze_scalar_evolution (loop, expr);
1220 if (access_fn == chrec_dont_know)
1224 init = initial_condition_in_loop_num (access_fn, loop->num);
1225 if (!expr_invariant_in_loop_p (loop, init))
1226 /* Not enough information: may be not loop invariant.
1227 E.g., for a[b[i]], we get a[D], where D=b[i]. EXPR is D, its
1228 initial_condition is D, but it depends on i - loop's induction
1232 evolution = evolution_part_in_loop_num (access_fn, loop->num);
1233 if (evolution && TREE_CODE (evolution) != INTEGER_CST)
1234 /* Evolution is not constant. */
1237 if (TREE_CODE (init) == INTEGER_CST)
1238 *misalign = fold_convert (ssizetype, init);
1240 /* Not constant, misalignment cannot be calculated. */
1241 *misalign = NULL_TREE;
1243 *initial_offset = fold_convert (ssizetype, init);
1245 *step = evolution ? fold_convert (ssizetype, evolution) : ssize_int (0);
1249 /* Recursive computation. */
1250 if (!BINARY_CLASS_P (expr))
1252 /* We expect to get binary expressions (PLUS/MINUS and MULT). */
1253 if (dump_file && (dump_flags & TDF_DETAILS))
1255 fprintf (dump_file, "\nNot binary expression ");
1256 print_generic_expr (dump_file, expr, TDF_SLIM);
1257 fprintf (dump_file, "\n");
1261 oprnd0 = TREE_OPERAND (expr, 0);
1262 oprnd1 = TREE_OPERAND (expr, 1);
1264 if (!analyze_offset_expr (oprnd0, loop, &left_offset, &left_misalign,
1265 &left_aligned_to, &left_step)
1266 || !analyze_offset_expr (oprnd1, loop, &right_offset, &right_misalign,
1267 &right_aligned_to, &right_step))
1270 /* The type of the operation: plus, minus or mult. */
1271 code = TREE_CODE (expr);
1275 if (TREE_CODE (right_offset) != INTEGER_CST)
1276 /* RIGHT_OFFSET can be not constant. For example, for arrays of variable
1278 FORNOW: We don't support such cases. */
1281 /* Strip conversions that don't narrow the mode. */
1282 left_offset = strip_conversion (left_offset);
1285 /* Misalignment computation. */
1286 if (SSA_VAR_P (left_offset))
1288 /* If the left side contains variables that can't be substituted with
1289 constants, the misalignment is unknown. However, if the right side
1290 is a multiple of some alignment, we know that the expression is
1291 aligned to it. Therefore, we record such maximum possible value.
1293 *misalign = NULL_TREE;
1294 *aligned_to = ssize_int (highest_pow2_factor (right_offset));
1298 /* The left operand was successfully substituted with constant. */
1301 /* In case of EXPR '(i * C1 + j) * C2', LEFT_MISALIGN is
1303 *misalign = size_binop (code, left_misalign, right_misalign);
1304 if (left_aligned_to && right_aligned_to)
1305 *aligned_to = size_binop (MIN_EXPR, left_aligned_to,
1308 *aligned_to = left_aligned_to ?
1309 left_aligned_to : right_aligned_to;
1312 *misalign = NULL_TREE;
1315 /* Step calculation. */
1316 /* Multiply the step by the right operand. */
1317 *step = size_binop (MULT_EXPR, left_step, right_offset);
1322 /* Combine the recursive calculations for step and misalignment. */
1323 *step = size_binop (code, left_step, right_step);
1325 /* Unknown alignment. */
1326 if ((!left_misalign && !left_aligned_to)
1327 || (!right_misalign && !right_aligned_to))
1329 *misalign = NULL_TREE;
1330 *aligned_to = NULL_TREE;
1334 if (left_misalign && right_misalign)
1335 *misalign = size_binop (code, left_misalign, right_misalign);
1337 *misalign = left_misalign ? left_misalign : right_misalign;
1339 if (left_aligned_to && right_aligned_to)
1340 *aligned_to = size_binop (MIN_EXPR, left_aligned_to, right_aligned_to);
1342 *aligned_to = left_aligned_to ? left_aligned_to : right_aligned_to;
1350 /* Compute offset. */
1351 *initial_offset = fold_convert (ssizetype,
1352 fold_build2 (code, TREE_TYPE (left_offset),
1358 /* Function address_analysis
1360 Return the BASE of the address expression EXPR.
1361 Also compute the OFFSET from BASE, MISALIGN and STEP.
1364 EXPR - the address expression that is being analyzed
1365 STMT - the statement that contains EXPR or its original memory reference
1366 IS_READ - TRUE if STMT reads from EXPR, FALSE if writes to EXPR
1367 DR - data_reference struct for the original memory reference
1370 BASE (returned value) - the base of the data reference EXPR.
1371 INITIAL_OFFSET - initial offset of EXPR from BASE (an expression)
1372 MISALIGN - offset of EXPR from BASE in bytes (a constant) or NULL_TREE if the
1373 computation is impossible
1374 ALIGNED_TO - maximum alignment of EXPR or NULL_TREE if MISALIGN can be
1375 calculated (doesn't depend on variables)
1376 STEP - evolution of EXPR in the loop
1378 If something unexpected is encountered (an unsupported form of data-ref),
1379 then NULL_TREE is returned.
1383 address_analysis (tree expr, tree stmt, bool is_read, struct data_reference *dr,
1384 tree *offset, tree *misalign, tree *aligned_to, tree *step)
1386 tree oprnd0, oprnd1, base_address, offset_expr, base_addr0, base_addr1;
1387 tree address_offset = ssize_int (0), address_misalign = ssize_int (0);
1388 tree dummy, address_aligned_to = NULL_TREE;
1389 struct ptr_info_def *dummy1;
1392 switch (TREE_CODE (expr))
1396 /* EXPR is of form {base +/- offset} (or {offset +/- base}). */
1397 oprnd0 = TREE_OPERAND (expr, 0);
1398 oprnd1 = TREE_OPERAND (expr, 1);
1400 STRIP_NOPS (oprnd0);
1401 STRIP_NOPS (oprnd1);
1403 /* Recursively try to find the base of the address contained in EXPR.
1404 For offset, the returned base will be NULL. */
1405 base_addr0 = address_analysis (oprnd0, stmt, is_read, dr, &address_offset,
1406 &address_misalign, &address_aligned_to,
1409 base_addr1 = address_analysis (oprnd1, stmt, is_read, dr, &address_offset,
1410 &address_misalign, &address_aligned_to,
1413 /* We support cases where only one of the operands contains an
1415 if ((base_addr0 && base_addr1) || (!base_addr0 && !base_addr1))
1417 if (dump_file && (dump_flags & TDF_DETAILS))
1420 "\neither more than one address or no addresses in expr ");
1421 print_generic_expr (dump_file, expr, TDF_SLIM);
1422 fprintf (dump_file, "\n");
1427 /* To revert STRIP_NOPS. */
1428 oprnd0 = TREE_OPERAND (expr, 0);
1429 oprnd1 = TREE_OPERAND (expr, 1);
1431 offset_expr = base_addr0 ?
1432 fold_convert (ssizetype, oprnd1) : fold_convert (ssizetype, oprnd0);
1434 /* EXPR is of form {base +/- offset} (or {offset +/- base}). If offset is
1435 a number, we can add it to the misalignment value calculated for base,
1436 otherwise, misalignment is NULL. */
1437 if (TREE_CODE (offset_expr) == INTEGER_CST && address_misalign)
1439 *misalign = size_binop (TREE_CODE (expr), address_misalign,
1441 *aligned_to = address_aligned_to;
1445 *misalign = NULL_TREE;
1446 *aligned_to = NULL_TREE;
1449 /* Combine offset (from EXPR {base + offset}) with the offset calculated
1451 *offset = size_binop (TREE_CODE (expr), address_offset, offset_expr);
1452 return base_addr0 ? base_addr0 : base_addr1;
1455 base_address = object_analysis (TREE_OPERAND (expr, 0), stmt, is_read,
1456 &dr, offset, misalign, aligned_to, step,
1457 &dummy, &dummy1, &dummy2);
1458 return base_address;
1461 if (!POINTER_TYPE_P (TREE_TYPE (expr)))
1463 if (dump_file && (dump_flags & TDF_DETAILS))
1465 fprintf (dump_file, "\nnot pointer SSA_NAME ");
1466 print_generic_expr (dump_file, expr, TDF_SLIM);
1467 fprintf (dump_file, "\n");
1471 *aligned_to = ssize_int (TYPE_ALIGN_UNIT (TREE_TYPE (TREE_TYPE (expr))));
1472 *misalign = ssize_int (0);
1473 *offset = ssize_int (0);
1474 *step = ssize_int (0);
1483 /* Function object_analysis
1485 Create a data-reference structure DR for MEMREF.
1486 Return the BASE of the data reference MEMREF if the analysis is possible.
1487 Also compute the INITIAL_OFFSET from BASE, MISALIGN and STEP.
1488 E.g., for EXPR a.b[i] + 4B, BASE is a, and OFFSET is the overall offset
1489 'a.b[i] + 4B' from a (can be an expression), MISALIGN is an OFFSET
1490 instantiated with initial_conditions of access_functions of variables,
1491 and STEP is the evolution of the DR_REF in this loop.
1493 Function get_inner_reference is used for the above in case of ARRAY_REF and
1496 The structure of the function is as follows:
1498 Case 1. For handled_component_p refs
1499 1.1 build data-reference structure for MEMREF
1500 1.2 call get_inner_reference
1501 1.2.1 analyze offset expr received from get_inner_reference
1502 (fall through with BASE)
1503 Case 2. For declarations
1505 Case 3. For INDIRECT_REFs
1506 3.1 build data-reference structure for MEMREF
1507 3.2 analyze evolution and initial condition of MEMREF
1508 3.3 set data-reference structure for MEMREF
1509 3.4 call address_analysis to analyze INIT of the access function
1510 3.5 extract memory tag
1513 Combine the results of object and address analysis to calculate
1514 INITIAL_OFFSET, STEP and misalignment info.
1517 MEMREF - the memory reference that is being analyzed
1518 STMT - the statement that contains MEMREF
1519 IS_READ - TRUE if STMT reads from MEMREF, FALSE if writes to MEMREF
1522 BASE_ADDRESS (returned value) - the base address of the data reference MEMREF
1523 E.g, if MEMREF is a.b[k].c[i][j] the returned
1525 DR - data_reference struct for MEMREF
1526 INITIAL_OFFSET - initial offset of MEMREF from BASE (an expression)
1527 MISALIGN - offset of MEMREF from BASE in bytes (a constant) modulo alignment of
1528 ALIGNMENT or NULL_TREE if the computation is impossible
1529 ALIGNED_TO - maximum alignment of EXPR or NULL_TREE if MISALIGN can be
1530 calculated (doesn't depend on variables)
1531 STEP - evolution of the DR_REF in the loop
1532 MEMTAG - memory tag for aliasing purposes
1533 PTR_INFO - NULL or points-to aliasing info from a pointer SSA_NAME
1534 SUBVARS - Sub-variables of the variable
1536 If the analysis of MEMREF evolution in the loop fails, NULL_TREE is returned,
1537 but DR can be created anyway.
1542 object_analysis (tree memref, tree stmt, bool is_read,
1543 struct data_reference **dr, tree *offset, tree *misalign,
1544 tree *aligned_to, tree *step, tree *memtag,
1545 struct ptr_info_def **ptr_info, subvar_t *subvars)
1547 tree base = NULL_TREE, base_address = NULL_TREE;
1548 tree object_offset = ssize_int (0), object_misalign = ssize_int (0);
1549 tree object_step = ssize_int (0), address_step = ssize_int (0);
1550 tree address_offset = ssize_int (0), address_misalign = ssize_int (0);
1551 HOST_WIDE_INT pbitsize, pbitpos;
1552 tree poffset, bit_pos_in_bytes;
1553 enum machine_mode pmode;
1554 int punsignedp, pvolatilep;
1555 tree ptr_step = ssize_int (0), ptr_init = NULL_TREE;
1556 struct loop *loop = loop_containing_stmt (stmt);
1557 struct data_reference *ptr_dr = NULL;
1558 tree object_aligned_to = NULL_TREE, address_aligned_to = NULL_TREE;
1559 tree comp_ref = NULL_TREE;
1564 /* Case 1. handled_component_p refs. */
1565 if (handled_component_p (memref))
1567 /* 1.1 build data-reference structure for MEMREF. */
1570 if (TREE_CODE (memref) == ARRAY_REF)
1571 *dr = analyze_array (stmt, memref, is_read);
1572 else if (TREE_CODE (memref) == COMPONENT_REF)
1576 if (dump_file && (dump_flags & TDF_DETAILS))
1578 fprintf (dump_file, "\ndata-ref of unsupported type ");
1579 print_generic_expr (dump_file, memref, TDF_SLIM);
1580 fprintf (dump_file, "\n");
1586 /* 1.2 call get_inner_reference. */
1587 /* Find the base and the offset from it. */
1588 base = get_inner_reference (memref, &pbitsize, &pbitpos, &poffset,
1589 &pmode, &punsignedp, &pvolatilep, false);
1592 if (dump_file && (dump_flags & TDF_DETAILS))
1594 fprintf (dump_file, "\nfailed to get inner ref for ");
1595 print_generic_expr (dump_file, memref, TDF_SLIM);
1596 fprintf (dump_file, "\n");
1601 /* 1.2.1 analyze offset expr received from get_inner_reference. */
1603 && !analyze_offset_expr (poffset, loop, &object_offset,
1604 &object_misalign, &object_aligned_to,
1607 if (dump_file && (dump_flags & TDF_DETAILS))
1609 fprintf (dump_file, "\nfailed to compute offset or step for ");
1610 print_generic_expr (dump_file, memref, TDF_SLIM);
1611 fprintf (dump_file, "\n");
1616 /* Add bit position to OFFSET and MISALIGN. */
1618 bit_pos_in_bytes = ssize_int (pbitpos/BITS_PER_UNIT);
1619 /* Check that there is no remainder in bits. */
1620 if (pbitpos%BITS_PER_UNIT)
1622 if (dump_file && (dump_flags & TDF_DETAILS))
1623 fprintf (dump_file, "\nbit offset alignment.\n");
1626 object_offset = size_binop (PLUS_EXPR, bit_pos_in_bytes, object_offset);
1627 if (object_misalign)
1628 object_misalign = size_binop (PLUS_EXPR, object_misalign,
1631 memref = base; /* To continue analysis of BASE. */
1635 /* Part 1: Case 2. Declarations. */
1636 if (DECL_P (memref))
1638 /* We expect to get a decl only if we already have a DR, or with
1639 COMPONENT_REFs of type 'a[i].b'. */
1642 if (comp_ref && TREE_CODE (TREE_OPERAND (comp_ref, 0)) == ARRAY_REF)
1644 *dr = analyze_array (stmt, TREE_OPERAND (comp_ref, 0), is_read);
1645 if (DR_NUM_DIMENSIONS (*dr) != 1)
1647 if (dump_file && (dump_flags & TDF_DETAILS))
1649 fprintf (dump_file, "\n multidimensional component ref ");
1650 print_generic_expr (dump_file, comp_ref, TDF_SLIM);
1651 fprintf (dump_file, "\n");
1658 if (dump_file && (dump_flags & TDF_DETAILS))
1660 fprintf (dump_file, "\nunhandled decl ");
1661 print_generic_expr (dump_file, memref, TDF_SLIM);
1662 fprintf (dump_file, "\n");
1668 /* TODO: if during the analysis of INDIRECT_REF we get to an object, put
1669 the object in BASE_OBJECT field if we can prove that this is O.K.,
1670 i.e., the data-ref access is bounded by the bounds of the BASE_OBJECT.
1671 (e.g., if the object is an array base 'a', where 'a[N]', we must prove
1672 that every access with 'p' (the original INDIRECT_REF based on '&a')
1673 in the loop is within the array boundaries - from a[0] to a[N-1]).
1674 Otherwise, our alias analysis can be incorrect.
1675 Even if an access function based on BASE_OBJECT can't be build, update
1676 BASE_OBJECT field to enable us to prove that two data-refs are
1677 different (without access function, distance analysis is impossible).
1679 if (SSA_VAR_P (memref) && var_can_have_subvars (memref))
1680 *subvars = get_subvars_for_var (memref);
1681 base_address = build_fold_addr_expr (memref);
1682 /* 2.1 set MEMTAG. */
1686 /* Part 1: Case 3. INDIRECT_REFs. */
1687 else if (TREE_CODE (memref) == INDIRECT_REF)
1689 tree ptr_ref = TREE_OPERAND (memref, 0);
1690 if (TREE_CODE (ptr_ref) == SSA_NAME)
1691 *ptr_info = SSA_NAME_PTR_INFO (ptr_ref);
1693 /* 3.1 build data-reference structure for MEMREF. */
1694 ptr_dr = analyze_indirect_ref (stmt, memref, is_read);
1697 if (dump_file && (dump_flags & TDF_DETAILS))
1699 fprintf (dump_file, "\nfailed to create dr for ");
1700 print_generic_expr (dump_file, memref, TDF_SLIM);
1701 fprintf (dump_file, "\n");
1706 /* 3.2 analyze evolution and initial condition of MEMREF. */
1707 ptr_step = DR_STEP (ptr_dr);
1708 ptr_init = DR_BASE_ADDRESS (ptr_dr);
1709 if (!ptr_init || !ptr_step || !POINTER_TYPE_P (TREE_TYPE (ptr_init)))
1711 *dr = (*dr) ? *dr : ptr_dr;
1712 if (dump_file && (dump_flags & TDF_DETAILS))
1714 fprintf (dump_file, "\nbad pointer access ");
1715 print_generic_expr (dump_file, memref, TDF_SLIM);
1716 fprintf (dump_file, "\n");
1721 if (integer_zerop (ptr_step) && !(*dr))
1723 if (dump_file && (dump_flags & TDF_DETAILS))
1724 fprintf (dump_file, "\nptr is loop invariant.\n");
1728 /* If there exists DR for MEMREF, we are analyzing the base of
1729 handled component (PTR_INIT), which not necessary has evolution in
1732 object_step = size_binop (PLUS_EXPR, object_step, ptr_step);
1734 /* 3.3 set data-reference structure for MEMREF. */
1738 /* 3.4 call address_analysis to analyze INIT of the access
1740 base_address = address_analysis (ptr_init, stmt, is_read, *dr,
1741 &address_offset, &address_misalign,
1742 &address_aligned_to, &address_step);
1745 if (dump_file && (dump_flags & TDF_DETAILS))
1747 fprintf (dump_file, "\nfailed to analyze address ");
1748 print_generic_expr (dump_file, ptr_init, TDF_SLIM);
1749 fprintf (dump_file, "\n");
1754 /* 3.5 extract memory tag. */
1755 switch (TREE_CODE (base_address))
1758 *memtag = get_var_ann (SSA_NAME_VAR (base_address))->symbol_mem_tag;
1759 if (!(*memtag) && TREE_CODE (TREE_OPERAND (memref, 0)) == SSA_NAME)
1760 *memtag = get_var_ann (
1761 SSA_NAME_VAR (TREE_OPERAND (memref, 0)))->symbol_mem_tag;
1764 *memtag = TREE_OPERAND (base_address, 0);
1767 if (dump_file && (dump_flags & TDF_DETAILS))
1769 fprintf (dump_file, "\nno memtag for ");
1770 print_generic_expr (dump_file, memref, TDF_SLIM);
1771 fprintf (dump_file, "\n");
1773 *memtag = NULL_TREE;
1780 /* MEMREF cannot be analyzed. */
1781 if (dump_file && (dump_flags & TDF_DETAILS))
1783 fprintf (dump_file, "\ndata-ref of unsupported type ");
1784 print_generic_expr (dump_file, memref, TDF_SLIM);
1785 fprintf (dump_file, "\n");
1791 DR_REF (*dr) = comp_ref;
1793 if (SSA_VAR_P (*memtag) && var_can_have_subvars (*memtag))
1794 *subvars = get_subvars_for_var (*memtag);
1796 /* Part 2: Combine the results of object and address analysis to calculate
1797 INITIAL_OFFSET, STEP and misalignment info. */
1798 *offset = size_binop (PLUS_EXPR, object_offset, address_offset);
1800 if ((!object_misalign && !object_aligned_to)
1801 || (!address_misalign && !address_aligned_to))
1803 *misalign = NULL_TREE;
1804 *aligned_to = NULL_TREE;
1808 if (object_misalign && address_misalign)
1809 *misalign = size_binop (PLUS_EXPR, object_misalign, address_misalign);
1811 *misalign = object_misalign ? object_misalign : address_misalign;
1812 if (object_aligned_to && address_aligned_to)
1813 *aligned_to = size_binop (MIN_EXPR, object_aligned_to,
1814 address_aligned_to);
1816 *aligned_to = object_aligned_to ?
1817 object_aligned_to : address_aligned_to;
1819 *step = size_binop (PLUS_EXPR, object_step, address_step);
1821 return base_address;
1824 /* Function analyze_offset.
1826 Extract INVARIANT and CONSTANT parts from OFFSET.
1830 analyze_offset (tree offset, tree *invariant, tree *constant)
1832 tree op0, op1, constant_0, constant_1, invariant_0, invariant_1;
1833 enum tree_code code = TREE_CODE (offset);
1835 *invariant = NULL_TREE;
1836 *constant = NULL_TREE;
1838 /* Not PLUS/MINUS expression - recursion stop condition. */
1839 if (code != PLUS_EXPR && code != MINUS_EXPR)
1841 if (TREE_CODE (offset) == INTEGER_CST)
1844 *invariant = offset;
1848 op0 = TREE_OPERAND (offset, 0);
1849 op1 = TREE_OPERAND (offset, 1);
1851 /* Recursive call with the operands. */
1852 analyze_offset (op0, &invariant_0, &constant_0);
1853 analyze_offset (op1, &invariant_1, &constant_1);
1855 /* Combine the results. */
1856 *constant = constant_0 ? constant_0 : constant_1;
1857 if (invariant_0 && invariant_1)
1859 fold_build2 (code, TREE_TYPE (invariant_0), invariant_0, invariant_1);
1861 *invariant = invariant_0 ? invariant_0 : invariant_1;
1865 /* Function create_data_ref.
1867 Create a data-reference structure for MEMREF. Set its DR_BASE_ADDRESS,
1868 DR_OFFSET, DR_INIT, DR_STEP, DR_OFFSET_MISALIGNMENT, DR_ALIGNED_TO,
1869 DR_MEMTAG, and DR_POINTSTO_INFO fields.
1872 MEMREF - the memory reference that is being analyzed
1873 STMT - the statement that contains MEMREF
1874 IS_READ - TRUE if STMT reads from MEMREF, FALSE if writes to MEMREF
1877 DR (returned value) - data_reference struct for MEMREF
1880 static struct data_reference *
1881 create_data_ref (tree memref, tree stmt, bool is_read)
1883 struct data_reference *dr = NULL;
1884 tree base_address, offset, step, misalign, memtag;
1885 struct loop *loop = loop_containing_stmt (stmt);
1886 tree invariant = NULL_TREE, constant = NULL_TREE;
1887 tree type_size, init_cond;
1888 struct ptr_info_def *ptr_info;
1889 subvar_t subvars = NULL;
1890 tree aligned_to, type = NULL_TREE, orig_offset;
1895 base_address = object_analysis (memref, stmt, is_read, &dr, &offset,
1896 &misalign, &aligned_to, &step, &memtag,
1897 &ptr_info, &subvars);
1898 if (!dr || !base_address)
1900 if (dump_file && (dump_flags & TDF_DETAILS))
1902 fprintf (dump_file, "\ncreate_data_ref: failed to create a dr for ");
1903 print_generic_expr (dump_file, memref, TDF_SLIM);
1904 fprintf (dump_file, "\n");
1909 DR_BASE_ADDRESS (dr) = base_address;
1910 DR_OFFSET (dr) = offset;
1911 DR_INIT (dr) = ssize_int (0);
1912 DR_STEP (dr) = step;
1913 DR_OFFSET_MISALIGNMENT (dr) = misalign;
1914 DR_ALIGNED_TO (dr) = aligned_to;
1915 DR_MEMTAG (dr) = memtag;
1916 DR_PTR_INFO (dr) = ptr_info;
1917 DR_SUBVARS (dr) = subvars;
1919 type_size = fold_convert (ssizetype, TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dr))));
1921 /* Extract CONSTANT and INVARIANT from OFFSET. */
1922 /* Remove cast from OFFSET and restore it for INVARIANT part. */
1923 orig_offset = offset;
1924 STRIP_NOPS (offset);
1925 if (offset != orig_offset)
1926 type = TREE_TYPE (orig_offset);
1927 analyze_offset (offset, &invariant, &constant);
1928 if (type && invariant)
1929 invariant = fold_convert (type, invariant);
1931 /* Put CONSTANT part of OFFSET in DR_INIT and INVARIANT in DR_OFFSET field
1935 DR_INIT (dr) = fold_convert (ssizetype, constant);
1936 init_cond = fold_build2 (TRUNC_DIV_EXPR, TREE_TYPE (constant),
1937 constant, type_size);
1940 DR_INIT (dr) = init_cond = ssize_int (0);;
1943 DR_OFFSET (dr) = invariant;
1945 DR_OFFSET (dr) = ssize_int (0);
1947 /* Change the access function for INIDIRECT_REFs, according to
1948 DR_BASE_ADDRESS. Analyze OFFSET calculated in object_analysis. OFFSET is
1949 an expression that can contain loop invariant expressions and constants.
1950 We put the constant part in the initial condition of the access function
1951 (for data dependence tests), and in DR_INIT of the data-ref. The loop
1952 invariant part is put in DR_OFFSET.
1953 The evolution part of the access function is STEP calculated in
1954 object_analysis divided by the size of data type.
1956 if (!DR_BASE_OBJECT (dr)
1957 || (TREE_CODE (memref) == COMPONENT_REF && DR_NUM_DIMENSIONS (dr) == 1))
1962 /* Update access function. */
1963 access_fn = DR_ACCESS_FN (dr, 0);
1964 new_step = size_binop (TRUNC_DIV_EXPR,
1965 fold_convert (ssizetype, step), type_size);
1967 access_fn = chrec_replace_initial_condition (access_fn, init_cond);
1968 access_fn = reset_evolution_in_loop (loop->num, access_fn, new_step);
1970 VEC_replace (tree, DR_ACCESS_FNS (dr), 0, access_fn);
1973 if (dump_file && (dump_flags & TDF_DETAILS))
1975 struct ptr_info_def *pi = DR_PTR_INFO (dr);
1977 fprintf (dump_file, "\nCreated dr for ");
1978 print_generic_expr (dump_file, memref, TDF_SLIM);
1979 fprintf (dump_file, "\n\tbase_address: ");
1980 print_generic_expr (dump_file, DR_BASE_ADDRESS (dr), TDF_SLIM);
1981 fprintf (dump_file, "\n\toffset from base address: ");
1982 print_generic_expr (dump_file, DR_OFFSET (dr), TDF_SLIM);
1983 fprintf (dump_file, "\n\tconstant offset from base address: ");
1984 print_generic_expr (dump_file, DR_INIT (dr), TDF_SLIM);
1985 fprintf (dump_file, "\n\tbase_object: ");
1986 print_generic_expr (dump_file, DR_BASE_OBJECT (dr), TDF_SLIM);
1987 fprintf (dump_file, "\n\tstep: ");
1988 print_generic_expr (dump_file, DR_STEP (dr), TDF_SLIM);
1989 fprintf (dump_file, "B\n\tmisalignment from base: ");
1990 print_generic_expr (dump_file, DR_OFFSET_MISALIGNMENT (dr), TDF_SLIM);
1991 if (DR_OFFSET_MISALIGNMENT (dr))
1992 fprintf (dump_file, "B");
1993 if (DR_ALIGNED_TO (dr))
1995 fprintf (dump_file, "\n\taligned to: ");
1996 print_generic_expr (dump_file, DR_ALIGNED_TO (dr), TDF_SLIM);
1998 fprintf (dump_file, "\n\tmemtag: ");
1999 print_generic_expr (dump_file, DR_MEMTAG (dr), TDF_SLIM);
2000 fprintf (dump_file, "\n");
2001 if (pi && pi->name_mem_tag)
2003 fprintf (dump_file, "\n\tnametag: ");
2004 print_generic_expr (dump_file, pi->name_mem_tag, TDF_SLIM);
2005 fprintf (dump_file, "\n");
2012 /* Returns true when all the functions of a tree_vec CHREC are the
2016 all_chrecs_equal_p (tree chrec)
2020 for (j = 0; j < TREE_VEC_LENGTH (chrec) - 1; j++)
2022 tree chrec_j = TREE_VEC_ELT (chrec, j);
2023 tree chrec_j_1 = TREE_VEC_ELT (chrec, j + 1);
2026 (integer_type_node, chrec_j, chrec_j_1)))
2032 /* Determine for each subscript in the data dependence relation DDR
2036 compute_subscript_distance (struct data_dependence_relation *ddr)
2038 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
2042 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
2044 tree conflicts_a, conflicts_b, difference;
2045 struct subscript *subscript;
2047 subscript = DDR_SUBSCRIPT (ddr, i);
2048 conflicts_a = SUB_CONFLICTS_IN_A (subscript);
2049 conflicts_b = SUB_CONFLICTS_IN_B (subscript);
2051 if (TREE_CODE (conflicts_a) == TREE_VEC)
2053 if (!all_chrecs_equal_p (conflicts_a))
2055 SUB_DISTANCE (subscript) = chrec_dont_know;
2059 conflicts_a = TREE_VEC_ELT (conflicts_a, 0);
2062 if (TREE_CODE (conflicts_b) == TREE_VEC)
2064 if (!all_chrecs_equal_p (conflicts_b))
2066 SUB_DISTANCE (subscript) = chrec_dont_know;
2070 conflicts_b = TREE_VEC_ELT (conflicts_b, 0);
2073 difference = chrec_fold_minus
2074 (integer_type_node, conflicts_b, conflicts_a);
2076 if (evolution_function_is_constant_p (difference))
2077 SUB_DISTANCE (subscript) = difference;
2080 SUB_DISTANCE (subscript) = chrec_dont_know;
2085 /* Initialize a data dependence relation between data accesses A and
2086 B. NB_LOOPS is the number of loops surrounding the references: the
2087 size of the classic distance/direction vectors. */
2089 static struct data_dependence_relation *
2090 initialize_data_dependence_relation (struct data_reference *a,
2091 struct data_reference *b,
2092 VEC (loop_p, heap) *loop_nest)
2094 struct data_dependence_relation *res;
2095 bool differ_p, known_dependence;
2098 res = XNEW (struct data_dependence_relation);
2102 if (a == NULL || b == NULL)
2104 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
2108 /* When A and B are arrays and their dimensions differ, we directly
2109 initialize the relation to "there is no dependence": chrec_known. */
2110 if (DR_BASE_OBJECT (a) && DR_BASE_OBJECT (b)
2111 && DR_NUM_DIMENSIONS (a) != DR_NUM_DIMENSIONS (b))
2113 DDR_ARE_DEPENDENT (res) = chrec_known;
2117 if (DR_BASE_ADDRESS (a) && DR_BASE_ADDRESS (b))
2118 known_dependence = base_addr_differ_p (a, b, &differ_p);
2120 known_dependence = base_object_differ_p (a, b, &differ_p);
2122 if (!known_dependence)
2124 /* Can't determine whether the data-refs access the same memory
2126 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
2132 DDR_ARE_DEPENDENT (res) = chrec_known;
2136 DDR_AFFINE_P (res) = true;
2137 DDR_ARE_DEPENDENT (res) = NULL_TREE;
2138 DDR_SUBSCRIPTS_VECTOR_INIT (res, DR_NUM_DIMENSIONS (a));
2139 DDR_LOOP_NEST (res) = loop_nest;
2140 DDR_DIR_VECTS (res) = NULL;
2141 DDR_DIST_VECTS (res) = NULL;
2143 for (i = 0; i < DR_NUM_DIMENSIONS (a); i++)
2145 struct subscript *subscript;
2147 subscript = XNEW (struct subscript);
2148 SUB_CONFLICTS_IN_A (subscript) = chrec_dont_know;
2149 SUB_CONFLICTS_IN_B (subscript) = chrec_dont_know;
2150 SUB_LAST_CONFLICT (subscript) = chrec_dont_know;
2151 SUB_DISTANCE (subscript) = chrec_dont_know;
2152 VARRAY_PUSH_GENERIC_PTR (DDR_SUBSCRIPTS (res), subscript);
2158 /* Set DDR_ARE_DEPENDENT to CHREC and finalize the subscript overlap
2162 finalize_ddr_dependent (struct data_dependence_relation *ddr,
2165 if (dump_file && (dump_flags & TDF_DETAILS))
2167 fprintf (dump_file, "(dependence classified: ");
2168 print_generic_expr (dump_file, chrec, 0);
2169 fprintf (dump_file, ")\n");
2172 DDR_ARE_DEPENDENT (ddr) = chrec;
2173 varray_clear (DDR_SUBSCRIPTS (ddr));
2176 /* The dependence relation DDR cannot be represented by a distance
2180 non_affine_dependence_relation (struct data_dependence_relation *ddr)
2182 if (dump_file && (dump_flags & TDF_DETAILS))
2183 fprintf (dump_file, "(Dependence relation cannot be represented by distance vector.) \n");
2185 DDR_AFFINE_P (ddr) = false;
2190 /* This section contains the classic Banerjee tests. */
2192 /* Returns true iff CHREC_A and CHREC_B are not dependent on any index
2193 variables, i.e., if the ZIV (Zero Index Variable) test is true. */
2196 ziv_subscript_p (tree chrec_a,
2199 return (evolution_function_is_constant_p (chrec_a)
2200 && evolution_function_is_constant_p (chrec_b));
2203 /* Returns true iff CHREC_A and CHREC_B are dependent on an index
2204 variable, i.e., if the SIV (Single Index Variable) test is true. */
2207 siv_subscript_p (tree chrec_a,
2210 if ((evolution_function_is_constant_p (chrec_a)
2211 && evolution_function_is_univariate_p (chrec_b))
2212 || (evolution_function_is_constant_p (chrec_b)
2213 && evolution_function_is_univariate_p (chrec_a)))
2216 if (evolution_function_is_univariate_p (chrec_a)
2217 && evolution_function_is_univariate_p (chrec_b))
2219 switch (TREE_CODE (chrec_a))
2221 case POLYNOMIAL_CHREC:
2222 switch (TREE_CODE (chrec_b))
2224 case POLYNOMIAL_CHREC:
2225 if (CHREC_VARIABLE (chrec_a) != CHREC_VARIABLE (chrec_b))
2240 /* Analyze a ZIV (Zero Index Variable) subscript. *OVERLAPS_A and
2241 *OVERLAPS_B are initialized to the functions that describe the
2242 relation between the elements accessed twice by CHREC_A and
2243 CHREC_B. For k >= 0, the following property is verified:
2245 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
2248 analyze_ziv_subscript (tree chrec_a,
2252 tree *last_conflicts)
2255 dependence_stats.num_ziv++;
2257 if (dump_file && (dump_flags & TDF_DETAILS))
2258 fprintf (dump_file, "(analyze_ziv_subscript \n");
2260 difference = chrec_fold_minus (integer_type_node, chrec_a, chrec_b);
2262 switch (TREE_CODE (difference))
2265 if (integer_zerop (difference))
2267 /* The difference is equal to zero: the accessed index
2268 overlaps for each iteration in the loop. */
2269 *overlaps_a = integer_zero_node;
2270 *overlaps_b = integer_zero_node;
2271 *last_conflicts = chrec_dont_know;
2272 dependence_stats.num_ziv_dependent++;
2276 /* The accesses do not overlap. */
2277 *overlaps_a = chrec_known;
2278 *overlaps_b = chrec_known;
2279 *last_conflicts = integer_zero_node;
2280 dependence_stats.num_ziv_independent++;
2285 /* We're not sure whether the indexes overlap. For the moment,
2286 conservatively answer "don't know". */
2287 if (dump_file && (dump_flags & TDF_DETAILS))
2288 fprintf (dump_file, "ziv test failed: difference is non-integer.\n");
2290 *overlaps_a = chrec_dont_know;
2291 *overlaps_b = chrec_dont_know;
2292 *last_conflicts = chrec_dont_know;
2293 dependence_stats.num_ziv_unimplemented++;
2297 if (dump_file && (dump_flags & TDF_DETAILS))
2298 fprintf (dump_file, ")\n");
2301 /* Get the real or estimated number of iterations for LOOPNUM, whichever is
2302 available. Return the number of iterations as a tree, or NULL_TREE if
2306 get_number_of_iters_for_loop (int loopnum)
2308 tree numiter = number_of_iterations_in_loop (current_loops->parray[loopnum]);
2310 if (TREE_CODE (numiter) != INTEGER_CST)
2311 numiter = current_loops->parray[loopnum]->estimated_nb_iterations;
2312 if (chrec_contains_undetermined (numiter))
2317 /* Analyze a SIV (Single Index Variable) subscript where CHREC_A is a
2318 constant, and CHREC_B is an affine function. *OVERLAPS_A and
2319 *OVERLAPS_B are initialized to the functions that describe the
2320 relation between the elements accessed twice by CHREC_A and
2321 CHREC_B. For k >= 0, the following property is verified:
2323 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
2326 analyze_siv_subscript_cst_affine (tree chrec_a,
2330 tree *last_conflicts)
2332 bool value0, value1, value2;
2333 tree difference = chrec_fold_minus
2334 (integer_type_node, CHREC_LEFT (chrec_b), chrec_a);
2336 if (!chrec_is_positive (initial_condition (difference), &value0))
2338 if (dump_file && (dump_flags & TDF_DETAILS))
2339 fprintf (dump_file, "siv test failed: chrec is not positive.\n");
2341 dependence_stats.num_siv_unimplemented++;
2342 *overlaps_a = chrec_dont_know;
2343 *overlaps_b = chrec_dont_know;
2344 *last_conflicts = chrec_dont_know;
2349 if (value0 == false)
2351 if (!chrec_is_positive (CHREC_RIGHT (chrec_b), &value1))
2353 if (dump_file && (dump_flags & TDF_DETAILS))
2354 fprintf (dump_file, "siv test failed: chrec not positive.\n");
2356 *overlaps_a = chrec_dont_know;
2357 *overlaps_b = chrec_dont_know;
2358 *last_conflicts = chrec_dont_know;
2359 dependence_stats.num_siv_unimplemented++;
2368 chrec_b = {10, +, 1}
2371 if (tree_fold_divides_p (CHREC_RIGHT (chrec_b), difference))
2374 int loopnum = CHREC_VARIABLE (chrec_b);
2376 *overlaps_a = integer_zero_node;
2377 *overlaps_b = fold_build2 (EXACT_DIV_EXPR, integer_type_node,
2378 fold_build1 (ABS_EXPR,
2381 CHREC_RIGHT (chrec_b));
2382 *last_conflicts = integer_one_node;
2385 /* Perform weak-zero siv test to see if overlap is
2386 outside the loop bounds. */
2387 numiter = get_number_of_iters_for_loop (loopnum);
2389 if (numiter != NULL_TREE
2390 && TREE_CODE (*overlaps_b) == INTEGER_CST
2391 && tree_int_cst_lt (numiter, *overlaps_b))
2393 *overlaps_a = chrec_known;
2394 *overlaps_b = chrec_known;
2395 *last_conflicts = integer_zero_node;
2396 dependence_stats.num_siv_independent++;
2399 dependence_stats.num_siv_dependent++;
2403 /* When the step does not divide the difference, there are
2407 *overlaps_a = chrec_known;
2408 *overlaps_b = chrec_known;
2409 *last_conflicts = integer_zero_node;
2410 dependence_stats.num_siv_independent++;
2419 chrec_b = {10, +, -1}
2421 In this case, chrec_a will not overlap with chrec_b. */
2422 *overlaps_a = chrec_known;
2423 *overlaps_b = chrec_known;
2424 *last_conflicts = integer_zero_node;
2425 dependence_stats.num_siv_independent++;
2432 if (!chrec_is_positive (CHREC_RIGHT (chrec_b), &value2))
2434 if (dump_file && (dump_flags & TDF_DETAILS))
2435 fprintf (dump_file, "siv test failed: chrec not positive.\n");
2437 *overlaps_a = chrec_dont_know;
2438 *overlaps_b = chrec_dont_know;
2439 *last_conflicts = chrec_dont_know;
2440 dependence_stats.num_siv_unimplemented++;
2445 if (value2 == false)
2449 chrec_b = {10, +, -1}
2451 if (tree_fold_divides_p (CHREC_RIGHT (chrec_b), difference))
2454 int loopnum = CHREC_VARIABLE (chrec_b);
2456 *overlaps_a = integer_zero_node;
2457 *overlaps_b = fold_build2 (EXACT_DIV_EXPR,
2458 integer_type_node, difference,
2459 CHREC_RIGHT (chrec_b));
2460 *last_conflicts = integer_one_node;
2462 /* Perform weak-zero siv test to see if overlap is
2463 outside the loop bounds. */
2464 numiter = get_number_of_iters_for_loop (loopnum);
2466 if (numiter != NULL_TREE
2467 && TREE_CODE (*overlaps_b) == INTEGER_CST
2468 && tree_int_cst_lt (numiter, *overlaps_b))
2470 *overlaps_a = chrec_known;
2471 *overlaps_b = chrec_known;
2472 *last_conflicts = integer_zero_node;
2473 dependence_stats.num_siv_independent++;
2476 dependence_stats.num_siv_dependent++;
2480 /* When the step does not divide the difference, there
2484 *overlaps_a = chrec_known;
2485 *overlaps_b = chrec_known;
2486 *last_conflicts = integer_zero_node;
2487 dependence_stats.num_siv_independent++;
2497 In this case, chrec_a will not overlap with chrec_b. */
2498 *overlaps_a = chrec_known;
2499 *overlaps_b = chrec_known;
2500 *last_conflicts = integer_zero_node;
2501 dependence_stats.num_siv_independent++;
2509 /* Helper recursive function for initializing the matrix A. Returns
2510 the initial value of CHREC. */
2513 initialize_matrix_A (lambda_matrix A, tree chrec, unsigned index, int mult)
2517 if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
2518 return int_cst_value (chrec);
2520 A[index][0] = mult * int_cst_value (CHREC_RIGHT (chrec));
2521 return initialize_matrix_A (A, CHREC_LEFT (chrec), index + 1, mult);
2524 #define FLOOR_DIV(x,y) ((x) / (y))
2526 /* Solves the special case of the Diophantine equation:
2527 | {0, +, STEP_A}_x (OVERLAPS_A) = {0, +, STEP_B}_y (OVERLAPS_B)
2529 Computes the descriptions OVERLAPS_A and OVERLAPS_B. NITER is the
2530 number of iterations that loops X and Y run. The overlaps will be
2531 constructed as evolutions in dimension DIM. */
2534 compute_overlap_steps_for_affine_univar (int niter, int step_a, int step_b,
2535 tree *overlaps_a, tree *overlaps_b,
2536 tree *last_conflicts, int dim)
2538 if (((step_a > 0 && step_b > 0)
2539 || (step_a < 0 && step_b < 0)))
2541 int step_overlaps_a, step_overlaps_b;
2542 int gcd_steps_a_b, last_conflict, tau2;
2544 gcd_steps_a_b = gcd (step_a, step_b);
2545 step_overlaps_a = step_b / gcd_steps_a_b;
2546 step_overlaps_b = step_a / gcd_steps_a_b;
2548 tau2 = FLOOR_DIV (niter, step_overlaps_a);
2549 tau2 = MIN (tau2, FLOOR_DIV (niter, step_overlaps_b));
2550 last_conflict = tau2;
2552 *overlaps_a = build_polynomial_chrec
2553 (dim, integer_zero_node,
2554 build_int_cst (NULL_TREE, step_overlaps_a));
2555 *overlaps_b = build_polynomial_chrec
2556 (dim, integer_zero_node,
2557 build_int_cst (NULL_TREE, step_overlaps_b));
2558 *last_conflicts = build_int_cst (NULL_TREE, last_conflict);
2563 *overlaps_a = integer_zero_node;
2564 *overlaps_b = integer_zero_node;
2565 *last_conflicts = integer_zero_node;
2570 /* Solves the special case of a Diophantine equation where CHREC_A is
2571 an affine bivariate function, and CHREC_B is an affine univariate
2572 function. For example,
2574 | {{0, +, 1}_x, +, 1335}_y = {0, +, 1336}_z
2576 has the following overlapping functions:
2578 | x (t, u, v) = {{0, +, 1336}_t, +, 1}_v
2579 | y (t, u, v) = {{0, +, 1336}_u, +, 1}_v
2580 | z (t, u, v) = {{{0, +, 1}_t, +, 1335}_u, +, 1}_v
2582 FORNOW: This is a specialized implementation for a case occurring in
2583 a common benchmark. Implement the general algorithm. */
2586 compute_overlap_steps_for_affine_1_2 (tree chrec_a, tree chrec_b,
2587 tree *overlaps_a, tree *overlaps_b,
2588 tree *last_conflicts)
2590 bool xz_p, yz_p, xyz_p;
2591 int step_x, step_y, step_z;
2592 int niter_x, niter_y, niter_z, niter;
2593 tree numiter_x, numiter_y, numiter_z;
2594 tree overlaps_a_xz, overlaps_b_xz, last_conflicts_xz;
2595 tree overlaps_a_yz, overlaps_b_yz, last_conflicts_yz;
2596 tree overlaps_a_xyz, overlaps_b_xyz, last_conflicts_xyz;
2598 step_x = int_cst_value (CHREC_RIGHT (CHREC_LEFT (chrec_a)));
2599 step_y = int_cst_value (CHREC_RIGHT (chrec_a));
2600 step_z = int_cst_value (CHREC_RIGHT (chrec_b));
2602 numiter_x = get_number_of_iters_for_loop (CHREC_VARIABLE (CHREC_LEFT (chrec_a)));
2603 numiter_y = get_number_of_iters_for_loop (CHREC_VARIABLE (chrec_a));
2604 numiter_z = get_number_of_iters_for_loop (CHREC_VARIABLE (chrec_b));
2606 if (numiter_x == NULL_TREE || numiter_y == NULL_TREE
2607 || numiter_z == NULL_TREE)
2609 if (dump_file && (dump_flags & TDF_DETAILS))
2610 fprintf (dump_file, "overlap steps test failed: no iteration counts.\n");
2612 *overlaps_a = chrec_dont_know;
2613 *overlaps_b = chrec_dont_know;
2614 *last_conflicts = chrec_dont_know;
2618 niter_x = int_cst_value (numiter_x);
2619 niter_y = int_cst_value (numiter_y);
2620 niter_z = int_cst_value (numiter_z);
2622 niter = MIN (niter_x, niter_z);
2623 compute_overlap_steps_for_affine_univar (niter, step_x, step_z,
2626 &last_conflicts_xz, 1);
2627 niter = MIN (niter_y, niter_z);
2628 compute_overlap_steps_for_affine_univar (niter, step_y, step_z,
2631 &last_conflicts_yz, 2);
2632 niter = MIN (niter_x, niter_z);
2633 niter = MIN (niter_y, niter);
2634 compute_overlap_steps_for_affine_univar (niter, step_x + step_y, step_z,
2637 &last_conflicts_xyz, 3);
2639 xz_p = !integer_zerop (last_conflicts_xz);
2640 yz_p = !integer_zerop (last_conflicts_yz);
2641 xyz_p = !integer_zerop (last_conflicts_xyz);
2643 if (xz_p || yz_p || xyz_p)
2645 *overlaps_a = make_tree_vec (2);
2646 TREE_VEC_ELT (*overlaps_a, 0) = integer_zero_node;
2647 TREE_VEC_ELT (*overlaps_a, 1) = integer_zero_node;
2648 *overlaps_b = integer_zero_node;
2651 TREE_VEC_ELT (*overlaps_a, 0) =
2652 chrec_fold_plus (integer_type_node, TREE_VEC_ELT (*overlaps_a, 0),
2655 chrec_fold_plus (integer_type_node, *overlaps_b, overlaps_b_xz);
2656 *last_conflicts = last_conflicts_xz;
2660 TREE_VEC_ELT (*overlaps_a, 1) =
2661 chrec_fold_plus (integer_type_node, TREE_VEC_ELT (*overlaps_a, 1),
2664 chrec_fold_plus (integer_type_node, *overlaps_b, overlaps_b_yz);
2665 *last_conflicts = last_conflicts_yz;
2669 TREE_VEC_ELT (*overlaps_a, 0) =
2670 chrec_fold_plus (integer_type_node, TREE_VEC_ELT (*overlaps_a, 0),
2672 TREE_VEC_ELT (*overlaps_a, 1) =
2673 chrec_fold_plus (integer_type_node, TREE_VEC_ELT (*overlaps_a, 1),
2676 chrec_fold_plus (integer_type_node, *overlaps_b, overlaps_b_xyz);
2677 *last_conflicts = last_conflicts_xyz;
2682 *overlaps_a = integer_zero_node;
2683 *overlaps_b = integer_zero_node;
2684 *last_conflicts = integer_zero_node;
2688 /* Determines the overlapping elements due to accesses CHREC_A and
2689 CHREC_B, that are affine functions. This function cannot handle
2690 symbolic evolution functions, ie. when initial conditions are
2691 parameters, because it uses lambda matrices of integers. */
2694 analyze_subscript_affine_affine (tree chrec_a,
2698 tree *last_conflicts)
2700 unsigned nb_vars_a, nb_vars_b, dim;
2701 int init_a, init_b, gamma, gcd_alpha_beta;
2703 lambda_matrix A, U, S;
2704 tree difference = chrec_fold_minus (integer_type_node, chrec_a, chrec_b);
2706 if (integer_zerop (difference))
2708 /* The difference is equal to zero: the accessed index
2709 overlaps for each iteration in the loop. */
2710 *overlaps_a = integer_zero_node;
2711 *overlaps_b = integer_zero_node;
2712 *last_conflicts = chrec_dont_know;
2715 if (dump_file && (dump_flags & TDF_DETAILS))
2716 fprintf (dump_file, "(analyze_subscript_affine_affine \n");
2718 /* For determining the initial intersection, we have to solve a
2719 Diophantine equation. This is the most time consuming part.
2721 For answering to the question: "Is there a dependence?" we have
2722 to prove that there exists a solution to the Diophantine
2723 equation, and that the solution is in the iteration domain,
2724 i.e. the solution is positive or zero, and that the solution
2725 happens before the upper bound loop.nb_iterations. Otherwise
2726 there is no dependence. This function outputs a description of
2727 the iterations that hold the intersections. */
2730 nb_vars_a = nb_vars_in_chrec (chrec_a);
2731 nb_vars_b = nb_vars_in_chrec (chrec_b);
2733 dim = nb_vars_a + nb_vars_b;
2734 U = lambda_matrix_new (dim, dim);
2735 A = lambda_matrix_new (dim, 1);
2736 S = lambda_matrix_new (dim, 1);
2738 init_a = initialize_matrix_A (A, chrec_a, 0, 1);
2739 init_b = initialize_matrix_A (A, chrec_b, nb_vars_a, -1);
2740 gamma = init_b - init_a;
2742 /* Don't do all the hard work of solving the Diophantine equation
2743 when we already know the solution: for example,
2746 | gamma = 3 - 3 = 0.
2747 Then the first overlap occurs during the first iterations:
2748 | {3, +, 1}_1 ({0, +, 4}_x) = {3, +, 4}_2 ({0, +, 1}_x)
2752 if (nb_vars_a == 1 && nb_vars_b == 1)
2755 int niter, niter_a, niter_b;
2756 tree numiter_a, numiter_b;
2758 numiter_a = get_number_of_iters_for_loop (CHREC_VARIABLE (chrec_a));
2759 numiter_b = get_number_of_iters_for_loop (CHREC_VARIABLE (chrec_b));
2760 if (numiter_a == NULL_TREE || numiter_b == NULL_TREE)
2762 if (dump_file && (dump_flags & TDF_DETAILS))
2763 fprintf (dump_file, "affine-affine test failed: missing iteration counts.\n");
2764 *overlaps_a = chrec_dont_know;
2765 *overlaps_b = chrec_dont_know;
2766 *last_conflicts = chrec_dont_know;
2767 goto end_analyze_subs_aa;
2770 niter_a = int_cst_value (numiter_a);
2771 niter_b = int_cst_value (numiter_b);
2772 niter = MIN (niter_a, niter_b);
2774 step_a = int_cst_value (CHREC_RIGHT (chrec_a));
2775 step_b = int_cst_value (CHREC_RIGHT (chrec_b));
2777 compute_overlap_steps_for_affine_univar (niter, step_a, step_b,
2778 overlaps_a, overlaps_b,
2782 else if (nb_vars_a == 2 && nb_vars_b == 1)
2783 compute_overlap_steps_for_affine_1_2
2784 (chrec_a, chrec_b, overlaps_a, overlaps_b, last_conflicts);
2786 else if (nb_vars_a == 1 && nb_vars_b == 2)
2787 compute_overlap_steps_for_affine_1_2
2788 (chrec_b, chrec_a, overlaps_b, overlaps_a, last_conflicts);
2792 if (dump_file && (dump_flags & TDF_DETAILS))
2793 fprintf (dump_file, "affine-affine test failed: too many variables.\n");
2794 *overlaps_a = chrec_dont_know;
2795 *overlaps_b = chrec_dont_know;
2796 *last_conflicts = chrec_dont_know;
2798 goto end_analyze_subs_aa;
2802 lambda_matrix_right_hermite (A, dim, 1, S, U);
2807 lambda_matrix_row_negate (U, dim, 0);
2809 gcd_alpha_beta = S[0][0];
2811 /* Something went wrong: for example in {1, +, 0}_5 vs. {0, +, 0}_5,
2812 but that is a quite strange case. Instead of ICEing, answer
2814 if (gcd_alpha_beta == 0)
2816 *overlaps_a = chrec_dont_know;
2817 *overlaps_b = chrec_dont_know;
2818 *last_conflicts = chrec_dont_know;
2819 goto end_analyze_subs_aa;
2822 /* The classic "gcd-test". */
2823 if (!int_divides_p (gcd_alpha_beta, gamma))
2825 /* The "gcd-test" has determined that there is no integer
2826 solution, i.e. there is no dependence. */
2827 *overlaps_a = chrec_known;
2828 *overlaps_b = chrec_known;
2829 *last_conflicts = integer_zero_node;
2832 /* Both access functions are univariate. This includes SIV and MIV cases. */
2833 else if (nb_vars_a == 1 && nb_vars_b == 1)
2835 /* Both functions should have the same evolution sign. */
2836 if (((A[0][0] > 0 && -A[1][0] > 0)
2837 || (A[0][0] < 0 && -A[1][0] < 0)))
2839 /* The solutions are given by:
2841 | [GAMMA/GCD_ALPHA_BETA t].[u11 u12] = [x0]
2844 For a given integer t. Using the following variables,
2846 | i0 = u11 * gamma / gcd_alpha_beta
2847 | j0 = u12 * gamma / gcd_alpha_beta
2854 | y0 = j0 + j1 * t. */
2858 /* X0 and Y0 are the first iterations for which there is a
2859 dependence. X0, Y0 are two solutions of the Diophantine
2860 equation: chrec_a (X0) = chrec_b (Y0). */
2862 int niter, niter_a, niter_b;
2863 tree numiter_a, numiter_b;
2865 numiter_a = get_number_of_iters_for_loop (CHREC_VARIABLE (chrec_a));
2866 numiter_b = get_number_of_iters_for_loop (CHREC_VARIABLE (chrec_b));
2868 if (numiter_a == NULL_TREE || numiter_b == NULL_TREE)
2870 if (dump_file && (dump_flags & TDF_DETAILS))
2871 fprintf (dump_file, "affine-affine test failed: missing iteration counts.\n");
2872 *overlaps_a = chrec_dont_know;
2873 *overlaps_b = chrec_dont_know;
2874 *last_conflicts = chrec_dont_know;
2875 goto end_analyze_subs_aa;
2878 niter_a = int_cst_value (numiter_a);
2879 niter_b = int_cst_value (numiter_b);
2880 niter = MIN (niter_a, niter_b);
2882 i0 = U[0][0] * gamma / gcd_alpha_beta;
2883 j0 = U[0][1] * gamma / gcd_alpha_beta;
2887 if ((i1 == 0 && i0 < 0)
2888 || (j1 == 0 && j0 < 0))
2890 /* There is no solution.
2891 FIXME: The case "i0 > nb_iterations, j0 > nb_iterations"
2892 falls in here, but for the moment we don't look at the
2893 upper bound of the iteration domain. */
2894 *overlaps_a = chrec_known;
2895 *overlaps_b = chrec_known;
2896 *last_conflicts = integer_zero_node;
2903 tau1 = CEIL (-i0, i1);
2904 tau2 = FLOOR_DIV (niter - i0, i1);
2908 int last_conflict, min_multiple;
2909 tau1 = MAX (tau1, CEIL (-j0, j1));
2910 tau2 = MIN (tau2, FLOOR_DIV (niter - j0, j1));
2912 x0 = i1 * tau1 + i0;
2913 y0 = j1 * tau1 + j0;
2915 /* At this point (x0, y0) is one of the
2916 solutions to the Diophantine equation. The
2917 next step has to compute the smallest
2918 positive solution: the first conflicts. */
2919 min_multiple = MIN (x0 / i1, y0 / j1);
2920 x0 -= i1 * min_multiple;
2921 y0 -= j1 * min_multiple;
2923 tau1 = (x0 - i0)/i1;
2924 last_conflict = tau2 - tau1;
2926 /* If the overlap occurs outside of the bounds of the
2927 loop, there is no dependence. */
2928 if (x0 > niter || y0 > niter)
2930 *overlaps_a = chrec_known;
2931 *overlaps_b = chrec_known;
2932 *last_conflicts = integer_zero_node;
2936 *overlaps_a = build_polynomial_chrec
2938 build_int_cst (NULL_TREE, x0),
2939 build_int_cst (NULL_TREE, i1));
2940 *overlaps_b = build_polynomial_chrec
2942 build_int_cst (NULL_TREE, y0),
2943 build_int_cst (NULL_TREE, j1));
2944 *last_conflicts = build_int_cst (NULL_TREE, last_conflict);
2949 /* FIXME: For the moment, the upper bound of the
2950 iteration domain for j is not checked. */
2951 if (dump_file && (dump_flags & TDF_DETAILS))
2952 fprintf (dump_file, "affine-affine test failed: unimplemented.\n");
2953 *overlaps_a = chrec_dont_know;
2954 *overlaps_b = chrec_dont_know;
2955 *last_conflicts = chrec_dont_know;
2961 /* FIXME: For the moment, the upper bound of the
2962 iteration domain for i is not checked. */
2963 if (dump_file && (dump_flags & TDF_DETAILS))
2964 fprintf (dump_file, "affine-affine test failed: unimplemented.\n");
2965 *overlaps_a = chrec_dont_know;
2966 *overlaps_b = chrec_dont_know;
2967 *last_conflicts = chrec_dont_know;
2973 if (dump_file && (dump_flags & TDF_DETAILS))
2974 fprintf (dump_file, "affine-affine test failed: unimplemented.\n");
2975 *overlaps_a = chrec_dont_know;
2976 *overlaps_b = chrec_dont_know;
2977 *last_conflicts = chrec_dont_know;
2983 if (dump_file && (dump_flags & TDF_DETAILS))
2984 fprintf (dump_file, "affine-affine test failed: unimplemented.\n");
2985 *overlaps_a = chrec_dont_know;
2986 *overlaps_b = chrec_dont_know;
2987 *last_conflicts = chrec_dont_know;
2990 end_analyze_subs_aa:
2991 if (dump_file && (dump_flags & TDF_DETAILS))
2993 fprintf (dump_file, " (overlaps_a = ");
2994 print_generic_expr (dump_file, *overlaps_a, 0);
2995 fprintf (dump_file, ")\n (overlaps_b = ");
2996 print_generic_expr (dump_file, *overlaps_b, 0);
2997 fprintf (dump_file, ")\n");
2998 fprintf (dump_file, ")\n");
3002 /* Returns true when analyze_subscript_affine_affine can be used for
3003 determining the dependence relation between chrec_a and chrec_b,
3004 that contain symbols. This function modifies chrec_a and chrec_b
3005 such that the analysis result is the same, and such that they don't
3006 contain symbols, and then can safely be passed to the analyzer.
3008 Example: The analysis of the following tuples of evolutions produce
3009 the same results: {x+1, +, 1}_1 vs. {x+3, +, 1}_1, and {-2, +, 1}_1
3012 {x+1, +, 1}_1 ({2, +, 1}_1) = {x+3, +, 1}_1 ({0, +, 1}_1)
3013 {-2, +, 1}_1 ({2, +, 1}_1) = {0, +, 1}_1 ({0, +, 1}_1)
3017 can_use_analyze_subscript_affine_affine (tree *chrec_a, tree *chrec_b)
3021 if (chrec_contains_symbols (CHREC_RIGHT (*chrec_a))
3022 || chrec_contains_symbols (CHREC_RIGHT (*chrec_b)))
3023 /* FIXME: For the moment not handled. Might be refined later. */
3026 diff = chrec_fold_minus (chrec_type (*chrec_a), CHREC_LEFT (*chrec_a),
3027 CHREC_LEFT (*chrec_b));
3028 if (!evolution_function_is_constant_p (diff))
3031 if (dump_file && (dump_flags & TDF_DETAILS))
3032 fprintf (dump_file, "can_use_subscript_aff_aff_for_symbolic \n");
3034 *chrec_a = build_polynomial_chrec (CHREC_VARIABLE (*chrec_a),
3035 diff, CHREC_RIGHT (*chrec_a));
3036 *chrec_b = build_polynomial_chrec (CHREC_VARIABLE (*chrec_b),
3038 CHREC_RIGHT (*chrec_b));
3042 /* Analyze a SIV (Single Index Variable) subscript. *OVERLAPS_A and
3043 *OVERLAPS_B are initialized to the functions that describe the
3044 relation between the elements accessed twice by CHREC_A and
3045 CHREC_B. For k >= 0, the following property is verified:
3047 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
3050 analyze_siv_subscript (tree chrec_a,
3054 tree *last_conflicts)
3056 dependence_stats.num_siv++;
3058 if (dump_file && (dump_flags & TDF_DETAILS))
3059 fprintf (dump_file, "(analyze_siv_subscript \n");
3061 if (evolution_function_is_constant_p (chrec_a)
3062 && evolution_function_is_affine_p (chrec_b))
3063 analyze_siv_subscript_cst_affine (chrec_a, chrec_b,
3064 overlaps_a, overlaps_b, last_conflicts);
3066 else if (evolution_function_is_affine_p (chrec_a)
3067 && evolution_function_is_constant_p (chrec_b))
3068 analyze_siv_subscript_cst_affine (chrec_b, chrec_a,
3069 overlaps_b, overlaps_a, last_conflicts);
3071 else if (evolution_function_is_affine_p (chrec_a)
3072 && evolution_function_is_affine_p (chrec_b))
3074 if (!chrec_contains_symbols (chrec_a)
3075 && !chrec_contains_symbols (chrec_b))
3077 analyze_subscript_affine_affine (chrec_a, chrec_b,
3078 overlaps_a, overlaps_b,
3081 if (*overlaps_a == chrec_dont_know
3082 || *overlaps_b == chrec_dont_know)
3083 dependence_stats.num_siv_unimplemented++;
3084 else if (*overlaps_a == chrec_known
3085 || *overlaps_b == chrec_known)
3086 dependence_stats.num_siv_independent++;
3088 dependence_stats.num_siv_dependent++;
3090 else if (can_use_analyze_subscript_affine_affine (&chrec_a,
3093 analyze_subscript_affine_affine (chrec_a, chrec_b,
3094 overlaps_a, overlaps_b,
3096 /* FIXME: The number of iterations is a symbolic expression.
3097 Compute it properly. */
3098 *last_conflicts = chrec_dont_know;
3100 if (*overlaps_a == chrec_dont_know
3101 || *overlaps_b == chrec_dont_know)
3102 dependence_stats.num_siv_unimplemented++;
3103 else if (*overlaps_a == chrec_known
3104 || *overlaps_b == chrec_known)
3105 dependence_stats.num_siv_independent++;
3107 dependence_stats.num_siv_dependent++;
3110 goto siv_subscript_dontknow;
3115 siv_subscript_dontknow:;
3116 if (dump_file && (dump_flags & TDF_DETAILS))
3117 fprintf (dump_file, "siv test failed: unimplemented.\n");
3118 *overlaps_a = chrec_dont_know;
3119 *overlaps_b = chrec_dont_know;
3120 *last_conflicts = chrec_dont_know;
3121 dependence_stats.num_siv_unimplemented++;
3124 if (dump_file && (dump_flags & TDF_DETAILS))
3125 fprintf (dump_file, ")\n");
3128 /* Return true when the property can be computed. RES should contain
3129 true when calling the first time this function, then it is set to
3130 false when one of the evolution steps of an affine CHREC does not
3131 divide the constant CST. */
3134 chrec_steps_divide_constant_p (tree chrec,
3138 switch (TREE_CODE (chrec))
3140 case POLYNOMIAL_CHREC:
3141 if (evolution_function_is_constant_p (CHREC_RIGHT (chrec)))
3143 if (tree_fold_divides_p (CHREC_RIGHT (chrec), cst))
3144 /* Keep RES to true, and iterate on other dimensions. */
3145 return chrec_steps_divide_constant_p (CHREC_LEFT (chrec), cst, res);
3151 /* When the step is a parameter the result is undetermined. */
3155 /* On the initial condition, return true. */
3160 /* Analyze a MIV (Multiple Index Variable) subscript. *OVERLAPS_A and
3161 *OVERLAPS_B are initialized to the functions that describe the
3162 relation between the elements accessed twice by CHREC_A and
3163 CHREC_B. For k >= 0, the following property is verified:
3165 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
3168 analyze_miv_subscript (tree chrec_a,
3172 tree *last_conflicts)
3174 /* FIXME: This is a MIV subscript, not yet handled.
3175 Example: (A[{1, +, 1}_1] vs. A[{1, +, 1}_2]) that comes from
3178 In the SIV test we had to solve a Diophantine equation with two
3179 variables. In the MIV case we have to solve a Diophantine
3180 equation with 2*n variables (if the subscript uses n IVs).
3182 bool divide_p = true;
3184 dependence_stats.num_miv++;
3185 if (dump_file && (dump_flags & TDF_DETAILS))
3186 fprintf (dump_file, "(analyze_miv_subscript \n");
3188 difference = chrec_fold_minus (integer_type_node, chrec_a, chrec_b);
3190 if (chrec_zerop (difference))
3192 /* Access functions are the same: all the elements are accessed
3193 in the same order. */
3194 *overlaps_a = integer_zero_node;
3195 *overlaps_b = integer_zero_node;
3196 *last_conflicts = get_number_of_iters_for_loop (CHREC_VARIABLE (chrec_a));
3197 dependence_stats.num_miv_dependent++;
3200 else if (evolution_function_is_constant_p (difference)
3201 /* For the moment, the following is verified:
3202 evolution_function_is_affine_multivariate_p (chrec_a) */
3203 && chrec_steps_divide_constant_p (chrec_a, difference, ÷_p)
3206 /* testsuite/.../ssa-chrec-33.c
3207 {{21, +, 2}_1, +, -2}_2 vs. {{20, +, 2}_1, +, -2}_2
3209 The difference is 1, and the evolution steps are equal to 2,
3210 consequently there are no overlapping elements. */
3211 *overlaps_a = chrec_known;
3212 *overlaps_b = chrec_known;
3213 *last_conflicts = integer_zero_node;
3214 dependence_stats.num_miv_independent++;
3217 else if (evolution_function_is_affine_multivariate_p (chrec_a)
3218 && !chrec_contains_symbols (chrec_a)
3219 && evolution_function_is_affine_multivariate_p (chrec_b)
3220 && !chrec_contains_symbols (chrec_b))
3222 /* testsuite/.../ssa-chrec-35.c
3223 {0, +, 1}_2 vs. {0, +, 1}_3
3224 the overlapping elements are respectively located at iterations:
3225 {0, +, 1}_x and {0, +, 1}_x,
3226 in other words, we have the equality:
3227 {0, +, 1}_2 ({0, +, 1}_x) = {0, +, 1}_3 ({0, +, 1}_x)
3230 {{0, +, 1}_1, +, 2}_2 ({0, +, 1}_x, {0, +, 1}_y) =
3231 {0, +, 1}_1 ({{0, +, 1}_x, +, 2}_y)
3233 {{0, +, 2}_1, +, 3}_2 ({0, +, 1}_y, {0, +, 1}_x) =
3234 {{0, +, 3}_1, +, 2}_2 ({0, +, 1}_x, {0, +, 1}_y)
3236 analyze_subscript_affine_affine (chrec_a, chrec_b,
3237 overlaps_a, overlaps_b, last_conflicts);
3239 if (*overlaps_a == chrec_dont_know
3240 || *overlaps_b == chrec_dont_know)
3241 dependence_stats.num_miv_unimplemented++;
3242 else if (*overlaps_a == chrec_known
3243 || *overlaps_b == chrec_known)
3244 dependence_stats.num_miv_independent++;
3246 dependence_stats.num_miv_dependent++;
3251 /* When the analysis is too difficult, answer "don't know". */
3252 if (dump_file && (dump_flags & TDF_DETAILS))
3253 fprintf (dump_file, "analyze_miv_subscript test failed: unimplemented.\n");
3255 *overlaps_a = chrec_dont_know;
3256 *overlaps_b = chrec_dont_know;
3257 *last_conflicts = chrec_dont_know;
3258 dependence_stats.num_miv_unimplemented++;
3261 if (dump_file && (dump_flags & TDF_DETAILS))
3262 fprintf (dump_file, ")\n");
3265 /* Determines the iterations for which CHREC_A is equal to CHREC_B.
3266 OVERLAP_ITERATIONS_A and OVERLAP_ITERATIONS_B are initialized with
3267 two functions that describe the iterations that contain conflicting
3270 Remark: For an integer k >= 0, the following equality is true:
3272 CHREC_A (OVERLAP_ITERATIONS_A (k)) == CHREC_B (OVERLAP_ITERATIONS_B (k)).
3276 analyze_overlapping_iterations (tree chrec_a,
3278 tree *overlap_iterations_a,
3279 tree *overlap_iterations_b,
3280 tree *last_conflicts)
3282 dependence_stats.num_subscript_tests++;
3284 if (dump_file && (dump_flags & TDF_DETAILS))
3286 fprintf (dump_file, "(analyze_overlapping_iterations \n");
3287 fprintf (dump_file, " (chrec_a = ");
3288 print_generic_expr (dump_file, chrec_a, 0);
3289 fprintf (dump_file, ")\n (chrec_b = ");
3290 print_generic_expr (dump_file, chrec_b, 0);
3291 fprintf (dump_file, ")\n");
3294 if (chrec_a == NULL_TREE
3295 || chrec_b == NULL_TREE
3296 || chrec_contains_undetermined (chrec_a)
3297 || chrec_contains_undetermined (chrec_b))
3299 dependence_stats.num_subscript_undetermined++;
3301 *overlap_iterations_a = chrec_dont_know;
3302 *overlap_iterations_b = chrec_dont_know;
3305 /* If they are the same chrec, and are affine, they overlap
3306 on every iteration. */
3307 else if (eq_evolutions_p (chrec_a, chrec_b)
3308 && evolution_function_is_affine_multivariate_p (chrec_a))
3310 dependence_stats.num_same_subscript_function++;
3311 *overlap_iterations_a = integer_zero_node;
3312 *overlap_iterations_b = integer_zero_node;
3313 *last_conflicts = chrec_dont_know;
3316 /* If they aren't the same, and aren't affine, we can't do anything
3318 else if ((chrec_contains_symbols (chrec_a)
3319 || chrec_contains_symbols (chrec_b))
3320 && (!evolution_function_is_affine_multivariate_p (chrec_a)
3321 || !evolution_function_is_affine_multivariate_p (chrec_b)))
3323 dependence_stats.num_subscript_undetermined++;
3324 *overlap_iterations_a = chrec_dont_know;
3325 *overlap_iterations_b = chrec_dont_know;
3328 else if (ziv_subscript_p (chrec_a, chrec_b))
3329 analyze_ziv_subscript (chrec_a, chrec_b,
3330 overlap_iterations_a, overlap_iterations_b,
3333 else if (siv_subscript_p (chrec_a, chrec_b))
3334 analyze_siv_subscript (chrec_a, chrec_b,
3335 overlap_iterations_a, overlap_iterations_b,
3339 analyze_miv_subscript (chrec_a, chrec_b,
3340 overlap_iterations_a, overlap_iterations_b,
3343 if (dump_file && (dump_flags & TDF_DETAILS))
3345 fprintf (dump_file, " (overlap_iterations_a = ");
3346 print_generic_expr (dump_file, *overlap_iterations_a, 0);
3347 fprintf (dump_file, ")\n (overlap_iterations_b = ");
3348 print_generic_expr (dump_file, *overlap_iterations_b, 0);
3349 fprintf (dump_file, ")\n");
3350 fprintf (dump_file, ")\n");
3354 /* Helper function for uniquely inserting distance vectors. */
3357 save_dist_v (struct data_dependence_relation *ddr, lambda_vector dist_v)
3362 for (i = 0; VEC_iterate (lambda_vector, DDR_DIST_VECTS (ddr), i, v); i++)
3363 if (lambda_vector_equal (v, dist_v, DDR_NB_LOOPS (ddr)))
3366 VEC_safe_push (lambda_vector, heap, DDR_DIST_VECTS (ddr), dist_v);
3369 /* Helper function for uniquely inserting direction vectors. */
3372 save_dir_v (struct data_dependence_relation *ddr, lambda_vector dir_v)
3377 for (i = 0; VEC_iterate (lambda_vector, DDR_DIR_VECTS (ddr), i, v); i++)
3378 if (lambda_vector_equal (v, dir_v, DDR_NB_LOOPS (ddr)))
3381 VEC_safe_push (lambda_vector, heap, DDR_DIR_VECTS (ddr), dir_v);
3384 /* Add a distance of 1 on all the loops outer than INDEX. If we
3385 haven't yet determined a distance for this outer loop, push a new
3386 distance vector composed of the previous distance, and a distance
3387 of 1 for this outer loop. Example:
3395 Saved vectors are of the form (dist_in_1, dist_in_2). First, we
3396 save (0, 1), then we have to save (1, 0). */
3399 add_outer_distances (struct data_dependence_relation *ddr,
3400 lambda_vector dist_v, int index)
3402 /* For each outer loop where init_v is not set, the accesses are
3403 in dependence of distance 1 in the loop. */
3404 while (--index >= 0)
3406 lambda_vector save_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3407 lambda_vector_copy (dist_v, save_v, DDR_NB_LOOPS (ddr));
3409 save_dist_v (ddr, save_v);
3413 /* Return false when fail to represent the data dependence as a
3414 distance vector. INIT_B is set to true when a component has been
3415 added to the distance vector DIST_V. INDEX_CARRY is then set to
3416 the index in DIST_V that carries the dependence. */
3419 build_classic_dist_vector_1 (struct data_dependence_relation *ddr,
3420 struct data_reference *ddr_a,
3421 struct data_reference *ddr_b,
3422 lambda_vector dist_v, bool *init_b,
3426 lambda_vector init_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3428 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3430 tree access_fn_a, access_fn_b;
3431 struct subscript *subscript = DDR_SUBSCRIPT (ddr, i);
3433 if (chrec_contains_undetermined (SUB_DISTANCE (subscript)))
3435 non_affine_dependence_relation (ddr);
3439 access_fn_a = DR_ACCESS_FN (ddr_a, i);
3440 access_fn_b = DR_ACCESS_FN (ddr_b, i);
3442 if (TREE_CODE (access_fn_a) == POLYNOMIAL_CHREC
3443 && TREE_CODE (access_fn_b) == POLYNOMIAL_CHREC)
3446 int index_a = index_in_loop_nest (CHREC_VARIABLE (access_fn_a),
3447 DDR_LOOP_NEST (ddr));
3448 int index_b = index_in_loop_nest (CHREC_VARIABLE (access_fn_b),
3449 DDR_LOOP_NEST (ddr));
3451 /* The dependence is carried by the outermost loop. Example:
3458 In this case, the dependence is carried by loop_1. */
3459 index = index_a < index_b ? index_a : index_b;
3460 *index_carry = MIN (index, *index_carry);
3462 if (chrec_contains_undetermined (SUB_DISTANCE (subscript)))
3464 non_affine_dependence_relation (ddr);
3468 dist = int_cst_value (SUB_DISTANCE (subscript));
3470 /* This is the subscript coupling test. If we have already
3471 recorded a distance for this loop (a distance coming from
3472 another subscript), it should be the same. For example,
3473 in the following code, there is no dependence:
3480 if (init_v[index] != 0 && dist_v[index] != dist)
3482 finalize_ddr_dependent (ddr, chrec_known);
3486 dist_v[index] = dist;
3492 /* This can be for example an affine vs. constant dependence
3493 (T[i] vs. T[3]) that is not an affine dependence and is
3494 not representable as a distance vector. */
3495 non_affine_dependence_relation (ddr);
3503 /* Return true when the DDR contains two data references that have the
3504 same access functions. */
3507 same_access_functions (struct data_dependence_relation *ddr)
3511 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3513 tree access_fun_a = DR_ACCESS_FN (DDR_A (ddr), i);
3514 tree access_fun_b = DR_ACCESS_FN (DDR_B (ddr), i);
3515 tree difference = chrec_fold_minus (integer_type_node, access_fun_a,
3517 if (TREE_CODE (difference) != INTEGER_CST
3518 || !integer_zerop (difference))
3525 /* Helper function for the case where DDR_A and DDR_B are the same
3526 multivariate access function. */
3529 add_multivariate_self_dist (struct data_dependence_relation *ddr, tree c_2)
3532 tree c_1 = CHREC_LEFT (c_2);
3533 tree c_0 = CHREC_LEFT (c_1);
3534 lambda_vector dist_v;
3536 /* Polynomials with more than 2 variables are not handled yet. */
3537 if (TREE_CODE (c_0) != INTEGER_CST)
3539 DDR_ARE_DEPENDENT (ddr) = chrec_dont_know;
3543 x_2 = index_in_loop_nest (CHREC_VARIABLE (c_2), DDR_LOOP_NEST (ddr));
3544 x_1 = index_in_loop_nest (CHREC_VARIABLE (c_1), DDR_LOOP_NEST (ddr));
3546 /* For "{{0, +, 2}_1, +, 3}_2" the distance vector is (3, -2). */
3547 dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3548 dist_v[x_1] = int_cst_value (CHREC_RIGHT (c_2));
3549 dist_v[x_2] = -int_cst_value (CHREC_RIGHT (c_1));
3550 save_dist_v (ddr, dist_v);
3552 add_outer_distances (ddr, dist_v, x_1);
3555 /* Helper function for the case where DDR_A and DDR_B are the same
3556 access functions. */
3559 add_other_self_distances (struct data_dependence_relation *ddr)
3561 lambda_vector dist_v;
3563 int index_carry = DDR_NB_LOOPS (ddr);
3565 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3567 tree access_fun = DR_ACCESS_FN (DDR_A (ddr), i);
3569 if (TREE_CODE (access_fun) == POLYNOMIAL_CHREC)
3571 if (!evolution_function_is_univariate_p (access_fun))
3573 if (DDR_NUM_SUBSCRIPTS (ddr) != 1)
3575 DDR_ARE_DEPENDENT (ddr) = chrec_dont_know;
3579 add_multivariate_self_dist (ddr, DR_ACCESS_FN (DDR_A (ddr), 0));
3583 index_carry = MIN (index_carry,
3584 index_in_loop_nest (CHREC_VARIABLE (access_fun),
3585 DDR_LOOP_NEST (ddr)));
3589 dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3590 add_outer_distances (ddr, dist_v, index_carry);
3593 /* Compute the classic per loop distance vector. DDR is the data
3594 dependence relation to build a vector from. Return false when fail
3595 to represent the data dependence as a distance vector. */
3598 build_classic_dist_vector (struct data_dependence_relation *ddr)
3600 bool init_b = false;
3601 int index_carry = DDR_NB_LOOPS (ddr);
3602 lambda_vector dist_v;
3604 if (DDR_ARE_DEPENDENT (ddr) != NULL_TREE)
3607 if (same_access_functions (ddr))
3609 /* Save the 0 vector. */
3610 dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3611 save_dist_v (ddr, dist_v);
3613 if (DDR_NB_LOOPS (ddr) > 1)
3614 add_other_self_distances (ddr);
3619 dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3620 if (!build_classic_dist_vector_1 (ddr, DDR_A (ddr), DDR_B (ddr),
3621 dist_v, &init_b, &index_carry))
3624 /* Save the distance vector if we initialized one. */
3627 /* Verify a basic constraint: classic distance vectors should
3628 always be lexicographically positive.
3630 Data references are collected in the order of execution of
3631 the program, thus for the following loop
3633 | for (i = 1; i < 100; i++)
3634 | for (j = 1; j < 100; j++)
3636 | t = T[j+1][i-1]; // A
3637 | T[j][i] = t + 2; // B
3640 references are collected following the direction of the wind:
3641 A then B. The data dependence tests are performed also
3642 following this order, such that we're looking at the distance
3643 separating the elements accessed by A from the elements later
3644 accessed by B. But in this example, the distance returned by
3645 test_dep (A, B) is lexicographically negative (-1, 1), that
3646 means that the access A occurs later than B with respect to
3647 the outer loop, ie. we're actually looking upwind. In this
3648 case we solve test_dep (B, A) looking downwind to the
3649 lexicographically positive solution, that returns the
3650 distance vector (1, -1). */
3651 if (!lambda_vector_lexico_pos (dist_v, DDR_NB_LOOPS (ddr)))
3653 lambda_vector save_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3654 subscript_dependence_tester_1 (ddr, DDR_B (ddr), DDR_A (ddr));
3655 compute_subscript_distance (ddr);
3656 build_classic_dist_vector_1 (ddr, DDR_B (ddr), DDR_A (ddr),
3657 save_v, &init_b, &index_carry);
3658 save_dist_v (ddr, save_v);
3660 /* In this case there is a dependence forward for all the
3663 | for (k = 1; k < 100; k++)
3664 | for (i = 1; i < 100; i++)
3665 | for (j = 1; j < 100; j++)
3667 | t = T[j+1][i-1]; // A
3668 | T[j][i] = t + 2; // B
3676 if (DDR_NB_LOOPS (ddr) > 1)
3678 add_outer_distances (ddr, save_v, index_carry);
3679 add_outer_distances (ddr, dist_v, index_carry);
3684 lambda_vector save_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3685 lambda_vector_copy (dist_v, save_v, DDR_NB_LOOPS (ddr));
3686 save_dist_v (ddr, save_v);
3688 if (DDR_NB_LOOPS (ddr) > 1)
3690 lambda_vector opposite_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3692 subscript_dependence_tester_1 (ddr, DDR_B (ddr), DDR_A (ddr));
3693 compute_subscript_distance (ddr);
3694 build_classic_dist_vector_1 (ddr, DDR_B (ddr), DDR_A (ddr),
3695 opposite_v, &init_b, &index_carry);
3697 add_outer_distances (ddr, dist_v, index_carry);
3698 add_outer_distances (ddr, opposite_v, index_carry);
3704 /* There is a distance of 1 on all the outer loops: Example:
3705 there is a dependence of distance 1 on loop_1 for the array A.
3711 add_outer_distances (ddr, dist_v,
3712 lambda_vector_first_nz (dist_v,
3713 DDR_NB_LOOPS (ddr), 0));
3716 if (dump_file && (dump_flags & TDF_DETAILS))
3720 fprintf (dump_file, "(build_classic_dist_vector\n");
3721 for (i = 0; i < DDR_NUM_DIST_VECTS (ddr); i++)
3723 fprintf (dump_file, " dist_vector = (");
3724 print_lambda_vector (dump_file, DDR_DIST_VECT (ddr, i),
3725 DDR_NB_LOOPS (ddr));
3726 fprintf (dump_file, " )\n");
3728 fprintf (dump_file, ")\n");
3734 /* Return the direction for a given distance.
3735 FIXME: Computing dir this way is suboptimal, since dir can catch
3736 cases that dist is unable to represent. */
3738 static inline enum data_dependence_direction
3739 dir_from_dist (int dist)
3742 return dir_positive;
3744 return dir_negative;
3749 /* Compute the classic per loop direction vector. DDR is the data
3750 dependence relation to build a vector from. */
3753 build_classic_dir_vector (struct data_dependence_relation *ddr)
3756 lambda_vector dist_v;
3758 for (i = 0; VEC_iterate (lambda_vector, DDR_DIST_VECTS (ddr), i, dist_v); i++)
3760 lambda_vector dir_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3762 for (j = 0; j < DDR_NB_LOOPS (ddr); j++)
3763 dir_v[j] = dir_from_dist (dist_v[j]);
3765 save_dir_v (ddr, dir_v);
3769 /* Helper function. Returns true when there is a dependence between
3770 data references DRA and DRB. */
3773 subscript_dependence_tester_1 (struct data_dependence_relation *ddr,
3774 struct data_reference *dra,
3775 struct data_reference *drb)
3778 tree last_conflicts;
3780 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3782 tree overlaps_a, overlaps_b;
3783 struct subscript *subscript = DDR_SUBSCRIPT (ddr, i);
3785 analyze_overlapping_iterations (DR_ACCESS_FN (dra, i),
3786 DR_ACCESS_FN (drb, i),
3787 &overlaps_a, &overlaps_b,
3790 if (chrec_contains_undetermined (overlaps_a)
3791 || chrec_contains_undetermined (overlaps_b))
3793 finalize_ddr_dependent (ddr, chrec_dont_know);
3794 dependence_stats.num_dependence_undetermined++;
3798 else if (overlaps_a == chrec_known
3799 || overlaps_b == chrec_known)
3801 finalize_ddr_dependent (ddr, chrec_known);
3802 dependence_stats.num_dependence_independent++;
3808 SUB_CONFLICTS_IN_A (subscript) = overlaps_a;
3809 SUB_CONFLICTS_IN_B (subscript) = overlaps_b;
3810 SUB_LAST_CONFLICT (subscript) = last_conflicts;
3817 /* Computes the conflicting iterations, and initialize DDR. */
3820 subscript_dependence_tester (struct data_dependence_relation *ddr)
3823 if (dump_file && (dump_flags & TDF_DETAILS))
3824 fprintf (dump_file, "(subscript_dependence_tester \n");
3826 if (subscript_dependence_tester_1 (ddr, DDR_A (ddr), DDR_B (ddr)))
3827 dependence_stats.num_dependence_dependent++;
3829 compute_subscript_distance (ddr);
3830 if (build_classic_dist_vector (ddr))
3831 build_classic_dir_vector (ddr);
3833 if (dump_file && (dump_flags & TDF_DETAILS))
3834 fprintf (dump_file, ")\n");
3837 /* Returns true when all the access functions of A are affine or
3841 access_functions_are_affine_or_constant_p (struct data_reference *a)
3844 VEC(tree,heap) **fns = DR_ACCESS_FNS_ADDR (a);
3847 for (i = 0; VEC_iterate (tree, *fns, i, t); i++)
3848 if (!evolution_function_is_constant_p (t)
3849 && !evolution_function_is_affine_multivariate_p (t))
3855 /* This computes the affine dependence relation between A and B.
3856 CHREC_KNOWN is used for representing the independence between two
3857 accesses, while CHREC_DONT_KNOW is used for representing the unknown
3860 Note that it is possible to stop the computation of the dependence
3861 relation the first time we detect a CHREC_KNOWN element for a given
3865 compute_affine_dependence (struct data_dependence_relation *ddr)
3867 struct data_reference *dra = DDR_A (ddr);
3868 struct data_reference *drb = DDR_B (ddr);
3870 if (dump_file && (dump_flags & TDF_DETAILS))
3872 fprintf (dump_file, "(compute_affine_dependence\n");
3873 fprintf (dump_file, " (stmt_a = \n");
3874 print_generic_expr (dump_file, DR_STMT (dra), 0);
3875 fprintf (dump_file, ")\n (stmt_b = \n");
3876 print_generic_expr (dump_file, DR_STMT (drb), 0);
3877 fprintf (dump_file, ")\n");
3880 /* Analyze only when the dependence relation is not yet known. */
3881 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
3883 dependence_stats.num_dependence_tests++;
3885 if (access_functions_are_affine_or_constant_p (dra)
3886 && access_functions_are_affine_or_constant_p (drb))
3887 subscript_dependence_tester (ddr);
3889 /* As a last case, if the dependence cannot be determined, or if
3890 the dependence is considered too difficult to determine, answer
3894 dependence_stats.num_dependence_undetermined++;
3896 if (dump_file && (dump_flags & TDF_DETAILS))
3898 fprintf (dump_file, "Data ref a:\n");
3899 dump_data_reference (dump_file, dra);
3900 fprintf (dump_file, "Data ref b:\n");
3901 dump_data_reference (dump_file, drb);
3902 fprintf (dump_file, "affine dependence test not usable: access function not affine or constant.\n");
3904 finalize_ddr_dependent (ddr, chrec_dont_know);
3908 if (dump_file && (dump_flags & TDF_DETAILS))
3909 fprintf (dump_file, ")\n");
3912 /* This computes the dependence relation for the same data
3913 reference into DDR. */
3916 compute_self_dependence (struct data_dependence_relation *ddr)
3920 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3922 struct subscript *subscript = DDR_SUBSCRIPT (ddr, i);
3924 /* The accessed index overlaps for each iteration. */
3925 SUB_CONFLICTS_IN_A (subscript) = integer_zero_node;
3926 SUB_CONFLICTS_IN_B (subscript) = integer_zero_node;
3927 SUB_LAST_CONFLICT (subscript) = chrec_dont_know;
3930 /* The distance vector is the zero vector. */
3931 save_dist_v (ddr, lambda_vector_new (DDR_NB_LOOPS (ddr)));
3932 save_dir_v (ddr, lambda_vector_new (DDR_NB_LOOPS (ddr)));
3935 /* Compute in DEPENDENCE_RELATIONS the data dependence graph for all
3936 the data references in DATAREFS, in the LOOP_NEST. When
3937 COMPUTE_SELF_AND_READ_READ_DEPENDENCES is FALSE, don't compute
3938 read-read and self relations. */
3941 compute_all_dependences (varray_type datarefs,
3942 VEC(ddr_p,heap) **dependence_relations,
3943 VEC (loop_p, heap) *loop_nest,
3944 bool compute_self_and_read_read_dependences)
3946 unsigned int i, j, N = VARRAY_ACTIVE_SIZE (datarefs);
3948 /* Note that we specifically skip i == j because it's a self dependence, and
3949 use compute_self_dependence below. */
3951 for (i = 0; i < N; i++)
3952 for (j = i + 1; j < N; j++)
3954 struct data_reference *a, *b;
3955 struct data_dependence_relation *ddr;
3957 a = VARRAY_GENERIC_PTR (datarefs, i);
3958 b = VARRAY_GENERIC_PTR (datarefs, j);
3960 if (DR_IS_READ (a) && DR_IS_READ (b)
3961 && !compute_self_and_read_read_dependences)
3964 ddr = initialize_data_dependence_relation (a, b, loop_nest);
3965 VEC_safe_push (ddr_p, heap, *dependence_relations, ddr);
3966 compute_affine_dependence (ddr);
3969 if (!compute_self_and_read_read_dependences)
3972 /* Compute self dependence relation of each dataref to itself. */
3973 for (i = 0; i < N; i++)
3975 struct data_reference *a, *b;
3976 struct data_dependence_relation *ddr;
3978 a = VARRAY_GENERIC_PTR (datarefs, i);
3979 b = VARRAY_GENERIC_PTR (datarefs, i);
3980 ddr = initialize_data_dependence_relation (a, b, loop_nest);
3981 VEC_safe_push (ddr_p, heap, *dependence_relations, ddr);
3982 compute_self_dependence (ddr);
3986 /* Search the data references in LOOP, and record the information into
3987 DATAREFS. Returns chrec_dont_know when failing to analyze a
3988 difficult case, returns NULL_TREE otherwise.
3990 TODO: This function should be made smarter so that it can handle address
3991 arithmetic as if they were array accesses, etc. */
3994 find_data_references_in_loop (struct loop *loop, varray_type *datarefs)
3996 basic_block bb, *bbs;
3998 block_stmt_iterator bsi;
3999 struct data_reference *dr;
4001 bbs = get_loop_body (loop);
4003 for (i = 0; i < loop->num_nodes; i++)
4007 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
4009 tree stmt = bsi_stmt (bsi);
4011 /* ASM_EXPR and CALL_EXPR may embed arbitrary side effects.
4012 Calls have side-effects, except those to const or pure
4014 if ((TREE_CODE (stmt) == CALL_EXPR
4015 && !(call_expr_flags (stmt) & (ECF_CONST | ECF_PURE)))
4016 || (TREE_CODE (stmt) == ASM_EXPR
4017 && ASM_VOLATILE_P (stmt)))
4018 goto insert_dont_know_node;
4020 if (ZERO_SSA_OPERANDS (stmt, SSA_OP_ALL_VIRTUALS))
4023 switch (TREE_CODE (stmt))
4027 bool one_inserted = false;
4028 tree opnd0 = TREE_OPERAND (stmt, 0);
4029 tree opnd1 = TREE_OPERAND (stmt, 1);
4031 if (TREE_CODE (opnd0) == ARRAY_REF
4032 || TREE_CODE (opnd0) == INDIRECT_REF
4033 || TREE_CODE (opnd0) == COMPONENT_REF)
4035 dr = create_data_ref (opnd0, stmt, false);
4038 VARRAY_PUSH_GENERIC_PTR (*datarefs, dr);
4039 one_inserted = true;
4043 if (TREE_CODE (opnd1) == ARRAY_REF
4044 || TREE_CODE (opnd1) == INDIRECT_REF
4045 || TREE_CODE (opnd1) == COMPONENT_REF)
4047 dr = create_data_ref (opnd1, stmt, true);
4050 VARRAY_PUSH_GENERIC_PTR (*datarefs, dr);
4051 one_inserted = true;
4056 goto insert_dont_know_node;
4064 bool one_inserted = false;
4066 for (args = TREE_OPERAND (stmt, 1); args;
4067 args = TREE_CHAIN (args))
4068 if (TREE_CODE (TREE_VALUE (args)) == ARRAY_REF
4069 || TREE_CODE (TREE_VALUE (args)) == INDIRECT_REF
4070 || TREE_CODE (TREE_VALUE (args)) == COMPONENT_REF)
4072 dr = create_data_ref (TREE_VALUE (args), stmt, true);
4075 VARRAY_PUSH_GENERIC_PTR (*datarefs, dr);
4076 one_inserted = true;
4081 goto insert_dont_know_node;
4088 struct data_reference *res;
4090 insert_dont_know_node:;
4091 res = XNEW (struct data_reference);
4092 DR_STMT (res) = NULL_TREE;
4093 DR_REF (res) = NULL_TREE;
4094 DR_BASE_OBJECT (res) = NULL;
4095 DR_TYPE (res) = ARRAY_REF_TYPE;
4096 DR_SET_ACCESS_FNS (res, NULL);
4097 DR_BASE_OBJECT (res) = NULL;
4098 DR_IS_READ (res) = false;
4099 DR_BASE_ADDRESS (res) = NULL_TREE;
4100 DR_OFFSET (res) = NULL_TREE;
4101 DR_INIT (res) = NULL_TREE;
4102 DR_STEP (res) = NULL_TREE;
4103 DR_OFFSET_MISALIGNMENT (res) = NULL_TREE;
4104 DR_MEMTAG (res) = NULL_TREE;
4105 DR_PTR_INFO (res) = NULL;
4106 VARRAY_PUSH_GENERIC_PTR (*datarefs, res);
4109 return chrec_dont_know;
4113 /* When there are no defs in the loop, the loop is parallel. */
4114 if (!ZERO_SSA_OPERANDS (stmt, SSA_OP_VIRTUAL_DEFS))
4115 loop->parallel_p = false;
4124 /* Recursive helper function. */
4127 find_loop_nest_1 (struct loop *loop, VEC (loop_p, heap) *loop_nest)
4129 /* Inner loops of the nest should not contain siblings. Example:
4130 when there are two consecutive loops,
4141 the dependence relation cannot be captured by the distance
4146 VEC_safe_push (loop_p, heap, loop_nest, loop);
4148 return find_loop_nest_1 (loop->inner, loop_nest);
4152 /* Return false when the LOOP is not well nested. Otherwise return
4153 true and insert in LOOP_NEST the loops of the nest. LOOP_NEST will
4154 contain the loops from the outermost to the innermost, as they will
4155 appear in the classic distance vector. */
4158 find_loop_nest (struct loop *loop, VEC (loop_p, heap) *loop_nest)
4160 VEC_safe_push (loop_p, heap, loop_nest, loop);
4162 return find_loop_nest_1 (loop->inner, loop_nest);
4166 /* Given a loop nest LOOP, the following vectors are returned:
4167 *DATAREFS is initialized to all the array elements contained in this loop,
4168 *DEPENDENCE_RELATIONS contains the relations between the data references.
4169 Compute read-read and self relations if
4170 COMPUTE_SELF_AND_READ_READ_DEPENDENCES is TRUE. */
4173 compute_data_dependences_for_loop (struct loop *loop,
4174 bool compute_self_and_read_read_dependences,
4175 varray_type *datarefs,
4176 varray_type *dependence_relations)
4179 VEC(ddr_p,heap) *allrelations;
4180 struct data_dependence_relation *ddr;
4181 struct loop *loop_nest = loop;
4182 VEC (loop_p, heap) *vloops = VEC_alloc (loop_p, heap, 3);
4184 memset (&dependence_stats, 0, sizeof (dependence_stats));
4186 /* If the loop nest is not well formed, or one of the data references
4187 is not computable, give up without spending time to compute other
4190 || !find_loop_nest (loop_nest, vloops)
4191 || find_data_references_in_loop (loop, datarefs) == chrec_dont_know)
4193 struct data_dependence_relation *ddr;
4195 /* Insert a single relation into dependence_relations:
4197 ddr = initialize_data_dependence_relation (NULL, NULL, vloops);
4198 VARRAY_PUSH_GENERIC_PTR (*dependence_relations, ddr);
4202 allrelations = NULL;
4203 compute_all_dependences (*datarefs, &allrelations, vloops,
4204 compute_self_and_read_read_dependences);
4207 /* FIXME: We copy the contents of allrelations back to a VARRAY
4208 because the vectorizer has not yet been converted to use VECs. */
4209 for (i = 0; VEC_iterate (ddr_p, allrelations, i, ddr); i++)
4210 VARRAY_PUSH_GENERIC_PTR (*dependence_relations, ddr);
4213 if (dump_file && (dump_flags & TDF_STATS))
4215 fprintf (dump_file, "Dependence tester statistics:\n");
4217 fprintf (dump_file, "Number of dependence tests: %d\n",
4218 dependence_stats.num_dependence_tests);
4219 fprintf (dump_file, "Number of dependence tests classified dependent: %d\n",
4220 dependence_stats.num_dependence_dependent);
4221 fprintf (dump_file, "Number of dependence tests classified independent: %d\n",
4222 dependence_stats.num_dependence_independent);
4223 fprintf (dump_file, "Number of undetermined dependence tests: %d\n",
4224 dependence_stats.num_dependence_undetermined);
4226 fprintf (dump_file, "Number of subscript tests: %d\n",
4227 dependence_stats.num_subscript_tests);
4228 fprintf (dump_file, "Number of undetermined subscript tests: %d\n",
4229 dependence_stats.num_subscript_undetermined);
4230 fprintf (dump_file, "Number of same subscript function: %d\n",
4231 dependence_stats.num_same_subscript_function);
4233 fprintf (dump_file, "Number of ziv tests: %d\n",
4234 dependence_stats.num_ziv);
4235 fprintf (dump_file, "Number of ziv tests returning dependent: %d\n",
4236 dependence_stats.num_ziv_dependent);
4237 fprintf (dump_file, "Number of ziv tests returning independent: %d\n",
4238 dependence_stats.num_ziv_independent);
4239 fprintf (dump_file, "Number of ziv tests unimplemented: %d\n",
4240 dependence_stats.num_ziv_unimplemented);
4242 fprintf (dump_file, "Number of siv tests: %d\n",
4243 dependence_stats.num_siv);
4244 fprintf (dump_file, "Number of siv tests returning dependent: %d\n",
4245 dependence_stats.num_siv_dependent);
4246 fprintf (dump_file, "Number of siv tests returning independent: %d\n",
4247 dependence_stats.num_siv_independent);
4248 fprintf (dump_file, "Number of siv tests unimplemented: %d\n",
4249 dependence_stats.num_siv_unimplemented);
4251 fprintf (dump_file, "Number of miv tests: %d\n",
4252 dependence_stats.num_miv);
4253 fprintf (dump_file, "Number of miv tests returning dependent: %d\n",
4254 dependence_stats.num_miv_dependent);
4255 fprintf (dump_file, "Number of miv tests returning independent: %d\n",
4256 dependence_stats.num_miv_independent);
4257 fprintf (dump_file, "Number of miv tests unimplemented: %d\n",
4258 dependence_stats.num_miv_unimplemented);
4262 /* Entry point (for testing only). Analyze all the data references
4263 and the dependence relations.
4265 The data references are computed first.
4267 A relation on these nodes is represented by a complete graph. Some
4268 of the relations could be of no interest, thus the relations can be
4271 In the following function we compute all the relations. This is
4272 just a first implementation that is here for:
4273 - for showing how to ask for the dependence relations,
4274 - for the debugging the whole dependence graph,
4275 - for the dejagnu testcases and maintenance.
4277 It is possible to ask only for a part of the graph, avoiding to
4278 compute the whole dependence graph. The computed dependences are
4279 stored in a knowledge base (KB) such that later queries don't
4280 recompute the same information. The implementation of this KB is
4281 transparent to the optimizer, and thus the KB can be changed with a
4282 more efficient implementation, or the KB could be disabled. */
4285 analyze_all_data_dependences (struct loops *loops)
4288 varray_type datarefs;
4289 varray_type dependence_relations;
4290 int nb_data_refs = 10;
4292 VARRAY_GENERIC_PTR_INIT (datarefs, nb_data_refs, "datarefs");
4293 VARRAY_GENERIC_PTR_INIT (dependence_relations,
4294 nb_data_refs * nb_data_refs,
4295 "dependence_relations");
4297 /* Compute DDs on the whole function. */
4298 compute_data_dependences_for_loop (loops->parray[0], false,
4299 &datarefs, &dependence_relations);
4303 dump_data_dependence_relations (dump_file, dependence_relations);
4304 fprintf (dump_file, "\n\n");
4306 if (dump_flags & TDF_DETAILS)
4307 dump_dist_dir_vectors (dump_file, dependence_relations);
4309 if (dump_flags & TDF_STATS)
4311 unsigned nb_top_relations = 0;
4312 unsigned nb_bot_relations = 0;
4313 unsigned nb_basename_differ = 0;
4314 unsigned nb_chrec_relations = 0;
4316 for (i = 0; i < VARRAY_ACTIVE_SIZE (dependence_relations); i++)
4318 struct data_dependence_relation *ddr;
4319 ddr = VARRAY_GENERIC_PTR (dependence_relations, i);
4321 if (chrec_contains_undetermined (DDR_ARE_DEPENDENT (ddr)))
4324 else if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
4326 struct data_reference *a = DDR_A (ddr);
4327 struct data_reference *b = DDR_B (ddr);
4330 if ((DR_BASE_OBJECT (a) && DR_BASE_OBJECT (b)
4331 && DR_NUM_DIMENSIONS (a) != DR_NUM_DIMENSIONS (b))
4332 || (base_object_differ_p (a, b, &differ_p)
4334 nb_basename_differ++;
4340 nb_chrec_relations++;
4343 gather_stats_on_scev_database ();
4347 free_dependence_relations (dependence_relations);
4348 free_data_refs (datarefs);
4352 /* Free the memory used by a data dependence relation DDR. */
4355 free_dependence_relation (struct data_dependence_relation *ddr)
4360 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE && DDR_SUBSCRIPTS (ddr))
4361 varray_clear (DDR_SUBSCRIPTS (ddr));
4365 /* Free the memory used by the data dependence relations from
4366 DEPENDENCE_RELATIONS. */
4369 free_dependence_relations (varray_type dependence_relations)
4372 if (dependence_relations == NULL)
4375 for (i = 0; i < VARRAY_ACTIVE_SIZE (dependence_relations); i++)
4376 free_dependence_relation (VARRAY_GENERIC_PTR (dependence_relations, i));
4377 varray_clear (dependence_relations);
4380 /* Free the memory used by the data references from DATAREFS. */
4383 free_data_refs (varray_type datarefs)
4387 if (datarefs == NULL)
4390 for (i = 0; i < VARRAY_ACTIVE_SIZE (datarefs); i++)
4392 struct data_reference *dr = (struct data_reference *)
4393 VARRAY_GENERIC_PTR (datarefs, i);
4396 DR_FREE_ACCESS_FNS (dr);
4400 varray_clear (datarefs);