1 /* Data references and dependences detectors.
2 Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
3 Contributed by Sebastian Pop <s.pop@laposte.net>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
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 tree
object_analysis (tree
, tree
, bool, struct data_reference
**,
98 tree
*, tree
*, tree
*, tree
*, tree
*,
99 struct ptr_info_def
**, subvar_t
*);
100 static struct data_reference
* init_data_ref (tree
, tree
, tree
, tree
, bool,
101 tree
, tree
, tree
, tree
, tree
,
102 struct ptr_info_def
*,
105 /* Determine if PTR and DECL may alias, the result is put in ALIASED.
106 Return FALSE if there is no type memory tag for PTR.
109 ptr_decl_may_alias_p (tree ptr
, tree decl
,
110 struct data_reference
*ptr_dr
,
115 gcc_assert (TREE_CODE (ptr
) == SSA_NAME
&& DECL_P (decl
));
117 tag
= get_var_ann (SSA_NAME_VAR (ptr
))->type_mem_tag
;
119 tag
= DR_MEMTAG (ptr_dr
);
123 *aliased
= is_aliased_with (tag
, decl
);
128 /* Determine if two pointers may alias, the result is put in ALIASED.
129 Return FALSE if there is no type memory tag for one of the pointers.
132 ptr_ptr_may_alias_p (tree ptr_a
, tree ptr_b
,
133 struct data_reference
*dra
,
134 struct data_reference
*drb
,
139 tag_a
= get_var_ann (SSA_NAME_VAR (ptr_a
))->type_mem_tag
;
141 tag_a
= DR_MEMTAG (dra
);
144 tag_b
= get_var_ann (SSA_NAME_VAR (ptr_b
))->type_mem_tag
;
146 tag_b
= DR_MEMTAG (drb
);
153 *aliased
= may_aliases_intersect (tag_a
, tag_b
);
159 /* Determine if BASE_A and BASE_B may alias, the result is put in ALIASED.
160 Return FALSE if there is no type memory tag for one of the symbols.
163 may_alias_p (tree base_a
, tree base_b
,
164 struct data_reference
*dra
,
165 struct data_reference
*drb
,
168 if (TREE_CODE (base_a
) == ADDR_EXPR
|| TREE_CODE (base_b
) == ADDR_EXPR
)
170 if (TREE_CODE (base_a
) == ADDR_EXPR
&& TREE_CODE (base_b
) == ADDR_EXPR
)
172 *aliased
= (TREE_OPERAND (base_a
, 0) == TREE_OPERAND (base_b
, 0));
175 if (TREE_CODE (base_a
) == ADDR_EXPR
)
176 return ptr_decl_may_alias_p (base_b
, TREE_OPERAND (base_a
, 0), drb
,
179 return ptr_decl_may_alias_p (base_a
, TREE_OPERAND (base_b
, 0), dra
,
183 return ptr_ptr_may_alias_p (base_a
, base_b
, dra
, drb
, aliased
);
187 /* Determine if a pointer (BASE_A) and a record/union access (BASE_B)
188 are not aliased. Return TRUE if they differ. */
190 record_ptr_differ_p (struct data_reference
*dra
,
191 struct data_reference
*drb
)
194 tree base_a
= DR_BASE_OBJECT (dra
);
195 tree base_b
= DR_BASE_OBJECT (drb
);
197 if (TREE_CODE (base_b
) != COMPONENT_REF
)
200 /* Peel COMPONENT_REFs to get to the base. Do not peel INDIRECT_REFs.
201 For a.b.c.d[i] we will get a, and for a.b->c.d[i] we will get a.b.
202 Probably will be unnecessary with struct alias analysis. */
203 while (TREE_CODE (base_b
) == COMPONENT_REF
)
204 base_b
= TREE_OPERAND (base_b
, 0);
205 /* Compare a record/union access (b.c[i] or p->c[i]) and a pointer
207 if (TREE_CODE (base_a
) == INDIRECT_REF
208 && ((TREE_CODE (base_b
) == VAR_DECL
209 && (ptr_decl_may_alias_p (TREE_OPERAND (base_a
, 0), base_b
, dra
,
212 || (TREE_CODE (base_b
) == INDIRECT_REF
213 && (ptr_ptr_may_alias_p (TREE_OPERAND (base_a
, 0),
214 TREE_OPERAND (base_b
, 0), dra
, drb
,
223 /* Determine if an array access (BASE_A) and a record/union access (BASE_B)
224 are not aliased. Return TRUE if they differ. */
226 record_array_differ_p (struct data_reference
*dra
,
227 struct data_reference
*drb
)
230 tree base_a
= DR_BASE_OBJECT (dra
);
231 tree base_b
= DR_BASE_OBJECT (drb
);
233 if (TREE_CODE (base_b
) != COMPONENT_REF
)
236 /* Peel COMPONENT_REFs to get to the base. Do not peel INDIRECT_REFs.
237 For a.b.c.d[i] we will get a, and for a.b->c.d[i] we will get a.b.
238 Probably will be unnecessary with struct alias analysis. */
239 while (TREE_CODE (base_b
) == COMPONENT_REF
)
240 base_b
= TREE_OPERAND (base_b
, 0);
242 /* Compare a record/union access (b.c[i] or p->c[i]) and an array access
243 (a[i]). In case of p->c[i] use alias analysis to verify that p is not
245 if (TREE_CODE (base_a
) == VAR_DECL
246 && (TREE_CODE (base_b
) == VAR_DECL
247 || (TREE_CODE (base_b
) == INDIRECT_REF
248 && (ptr_decl_may_alias_p (TREE_OPERAND (base_b
, 0), base_a
, drb
,
257 /* Determine if an array access (BASE_A) and a pointer (BASE_B)
258 are not aliased. Return TRUE if they differ. */
260 array_ptr_differ_p (tree base_a
, tree base_b
,
261 struct data_reference
*drb
)
265 /* In case one of the bases is a pointer (a[i] and (*p)[i]), we check with the
266 help of alias analysis that p is not pointing to a. */
267 if (TREE_CODE (base_a
) == VAR_DECL
&& TREE_CODE (base_b
) == INDIRECT_REF
268 && (ptr_decl_may_alias_p (TREE_OPERAND (base_b
, 0), base_a
, drb
, &aliased
)
276 /* This is the simplest data dependence test: determines whether the
277 data references A and B access the same array/region. Returns
278 false when the property is not computable at compile time.
279 Otherwise return true, and DIFFER_P will record the result. This
280 utility will not be necessary when alias_sets_conflict_p will be
281 less conservative. */
284 base_object_differ_p (struct data_reference
*a
,
285 struct data_reference
*b
,
288 tree base_a
= DR_BASE_OBJECT (a
);
289 tree base_b
= DR_BASE_OBJECT (b
);
292 if (!base_a
|| !base_b
)
295 /* Determine if same base. Example: for the array accesses
296 a[i], b[i] or pointer accesses *a, *b, bases are a, b. */
297 if (base_a
== base_b
)
303 /* For pointer based accesses, (*p)[i], (*q)[j], the bases are (*p)
305 if (TREE_CODE (base_a
) == INDIRECT_REF
&& TREE_CODE (base_b
) == INDIRECT_REF
306 && TREE_OPERAND (base_a
, 0) == TREE_OPERAND (base_b
, 0))
312 /* Record/union based accesses - s.a[i], t.b[j]. bases are s.a,t.b. */
313 if (TREE_CODE (base_a
) == COMPONENT_REF
&& TREE_CODE (base_b
) == COMPONENT_REF
314 && TREE_OPERAND (base_a
, 0) == TREE_OPERAND (base_b
, 0)
315 && TREE_OPERAND (base_a
, 1) == TREE_OPERAND (base_b
, 1))
322 /* Determine if different bases. */
324 /* At this point we know that base_a != base_b. However, pointer
325 accesses of the form x=(*p) and y=(*q), whose bases are p and q,
326 may still be pointing to the same base. In SSAed GIMPLE p and q will
327 be SSA_NAMES in this case. Therefore, here we check if they are
328 really two different declarations. */
329 if (TREE_CODE (base_a
) == VAR_DECL
&& TREE_CODE (base_b
) == VAR_DECL
)
335 /* In case one of the bases is a pointer (a[i] and (*p)[i]), we check with the
336 help of alias analysis that p is not pointing to a. */
337 if (array_ptr_differ_p (base_a
, base_b
, b
)
338 || array_ptr_differ_p (base_b
, base_a
, a
))
344 /* If the bases are pointers ((*q)[i] and (*p)[i]), we check with the
345 help of alias analysis they don't point to the same bases. */
346 if (TREE_CODE (base_a
) == INDIRECT_REF
&& TREE_CODE (base_b
) == INDIRECT_REF
347 && (may_alias_p (TREE_OPERAND (base_a
, 0), TREE_OPERAND (base_b
, 0), a
, b
,
355 /* Compare two record/union bases s.a and t.b: s != t or (a != b and
356 s and t are not unions). */
357 if (TREE_CODE (base_a
) == COMPONENT_REF
&& TREE_CODE (base_b
) == COMPONENT_REF
358 && ((TREE_CODE (TREE_OPERAND (base_a
, 0)) == VAR_DECL
359 && TREE_CODE (TREE_OPERAND (base_b
, 0)) == VAR_DECL
360 && TREE_OPERAND (base_a
, 0) != TREE_OPERAND (base_b
, 0))
361 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (base_a
, 0))) == RECORD_TYPE
362 && TREE_CODE (TREE_TYPE (TREE_OPERAND (base_b
, 0))) == RECORD_TYPE
363 && TREE_OPERAND (base_a
, 1) != TREE_OPERAND (base_b
, 1))))
369 /* Compare a record/union access (b.c[i] or p->c[i]) and a pointer
371 if (record_ptr_differ_p (a
, b
) || record_ptr_differ_p (b
, a
))
377 /* Compare a record/union access (b.c[i] or p->c[i]) and an array access
378 (a[i]). In case of p->c[i] use alias analysis to verify that p is not
380 if (record_array_differ_p (a
, b
) || record_array_differ_p (b
, a
))
389 /* Function base_addr_differ_p.
391 This is the simplest data dependence test: determines whether the
392 data references DRA and DRB access the same array/region. Returns
393 false when the property is not computable at compile time.
394 Otherwise return true, and DIFFER_P will record the result.
397 1. if (both DRA and DRB are represented as arrays)
398 compare DRA.BASE_OBJECT and DRB.BASE_OBJECT
399 2. else if (both DRA and DRB are represented as pointers)
400 try to prove that DRA.FIRST_LOCATION == DRB.FIRST_LOCATION
401 3. else if (DRA and DRB are represented differently or 2. fails)
402 only try to prove that the bases are surely different
407 base_addr_differ_p (struct data_reference
*dra
,
408 struct data_reference
*drb
,
411 tree addr_a
= DR_BASE_ADDRESS (dra
);
412 tree addr_b
= DR_BASE_ADDRESS (drb
);
416 if (!addr_a
|| !addr_b
)
419 type_a
= TREE_TYPE (addr_a
);
420 type_b
= TREE_TYPE (addr_b
);
422 gcc_assert (POINTER_TYPE_P (type_a
) && POINTER_TYPE_P (type_b
));
424 /* 1. if (both DRA and DRB are represented as arrays)
425 compare DRA.BASE_OBJECT and DRB.BASE_OBJECT. */
426 if (DR_TYPE (dra
) == ARRAY_REF_TYPE
&& DR_TYPE (drb
) == ARRAY_REF_TYPE
)
427 return base_object_differ_p (dra
, drb
, differ_p
);
430 /* 2. else if (both DRA and DRB are represented as pointers)
431 try to prove that DRA.FIRST_LOCATION == DRB.FIRST_LOCATION. */
432 /* If base addresses are the same, we check the offsets, since the access of
433 the data-ref is described by {base addr + offset} and its access function,
434 i.e., in order to decide whether the bases of data-refs are the same we
435 compare both base addresses and offsets. */
436 if (DR_TYPE (dra
) == POINTER_REF_TYPE
&& DR_TYPE (drb
) == POINTER_REF_TYPE
438 || (TREE_CODE (addr_a
) == ADDR_EXPR
&& TREE_CODE (addr_b
) == ADDR_EXPR
439 && TREE_OPERAND (addr_a
, 0) == TREE_OPERAND (addr_b
, 0))))
441 /* Compare offsets. */
442 tree offset_a
= DR_OFFSET (dra
);
443 tree offset_b
= DR_OFFSET (drb
);
445 STRIP_NOPS (offset_a
);
446 STRIP_NOPS (offset_b
);
448 /* FORNOW: we only compare offsets that are MULT_EXPR, i.e., we don't handle
450 if ((offset_a
== offset_b
)
451 || (TREE_CODE (offset_a
) == MULT_EXPR
452 && TREE_CODE (offset_b
) == MULT_EXPR
453 && TREE_OPERAND (offset_a
, 0) == TREE_OPERAND (offset_b
, 0)
454 && TREE_OPERAND (offset_a
, 1) == TREE_OPERAND (offset_b
, 1)))
461 /* 3. else if (DRA and DRB are represented differently or 2. fails)
462 only try to prove that the bases are surely different. */
464 /* Apply alias analysis. */
465 if (may_alias_p (addr_a
, addr_b
, dra
, drb
, &aliased
) && !aliased
)
471 /* An instruction writing through a restricted pointer is "independent" of any
472 instruction reading or writing through a different pointer, in the same
474 else if ((TYPE_RESTRICT (type_a
) && !DR_IS_READ (dra
))
475 || (TYPE_RESTRICT (type_b
) && !DR_IS_READ (drb
)))
484 /* Returns true iff A divides B. */
487 tree_fold_divides_p (tree a
,
490 /* Determines whether (A == gcd (A, B)). */
491 return tree_int_cst_equal (a
, tree_fold_gcd (a
, b
));
494 /* Compute the greatest common denominator of two numbers using
495 Euclid's algorithm. */
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_name: ");
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 /* Dump function for a DATA_DEPENDENCE_RELATION structure. */
617 dump_data_dependence_relation (FILE *outf
,
618 struct data_dependence_relation
*ddr
)
620 struct data_reference
*dra
, *drb
;
624 fprintf (outf
, "(Data Dep: \n");
625 if (DDR_ARE_DEPENDENT (ddr
) == chrec_dont_know
)
626 fprintf (outf
, " (don't know)\n");
628 else if (DDR_ARE_DEPENDENT (ddr
) == chrec_known
)
629 fprintf (outf
, " (no dependence)\n");
631 else if (DDR_ARE_DEPENDENT (ddr
) == NULL_TREE
)
635 for (i
= 0; i
< DDR_NUM_SUBSCRIPTS (ddr
); i
++)
637 fprintf (outf
, " access_fn_A: ");
638 print_generic_stmt (outf
, DR_ACCESS_FN (dra
, i
), 0);
639 fprintf (outf
, " access_fn_B: ");
640 print_generic_stmt (outf
, DR_ACCESS_FN (drb
, i
), 0);
641 dump_subscript (outf
, DDR_SUBSCRIPT (ddr
, i
));
644 for (i
= 0; i
< DDR_NUM_DIST_VECTS (ddr
); i
++)
646 fprintf (outf
, " distance_vector: ");
647 print_lambda_vector (outf
, DDR_DIST_VECT (ddr
, i
),
648 DDR_SIZE_VECT (ddr
));
651 for (i
= 0; i
< DDR_NUM_DIR_VECTS (ddr
); i
++)
653 fprintf (outf
, " direction_vector: ");
654 print_lambda_vector (outf
, DDR_DIR_VECT (ddr
, i
),
655 DDR_SIZE_VECT (ddr
));
659 fprintf (outf
, ")\n");
664 /* Dump function for a DATA_DEPENDENCE_DIRECTION structure. */
667 dump_data_dependence_direction (FILE *file
,
668 enum data_dependence_direction dir
)
684 case dir_positive_or_negative
:
685 fprintf (file
, "+-");
688 case dir_positive_or_equal
:
689 fprintf (file
, "+=");
692 case dir_negative_or_equal
:
693 fprintf (file
, "-=");
705 /* Dumps the distance and direction vectors in FILE. DDRS contains
706 the dependence relations, and VECT_SIZE is the size of the
707 dependence vectors, or in other words the number of loops in the
711 dump_dist_dir_vectors (FILE *file
, varray_type ddrs
)
715 for (i
= 0; i
< VARRAY_ACTIVE_SIZE (ddrs
); i
++)
717 struct data_dependence_relation
*ddr
=
718 (struct data_dependence_relation
*)
719 VARRAY_GENERIC_PTR (ddrs
, i
);
720 if (DDR_ARE_DEPENDENT (ddr
) == NULL_TREE
721 && DDR_AFFINE_P (ddr
))
723 for (j
= 0; j
< DDR_NUM_DIST_VECTS (ddr
); j
++)
725 fprintf (file
, "DISTANCE_V (");
726 print_lambda_vector (file
, DDR_DIST_VECT (ddr
, j
),
727 DDR_SIZE_VECT (ddr
));
728 fprintf (file
, ")\n");
731 for (j
= 0; j
< DDR_NUM_DIR_VECTS (ddr
); j
++)
733 fprintf (file
, "DIRECTION_V (");
734 print_lambda_vector (file
, DDR_DIR_VECT (ddr
, j
),
735 DDR_SIZE_VECT (ddr
));
736 fprintf (file
, ")\n");
740 fprintf (file
, "\n\n");
743 /* Dumps the data dependence relations DDRS in FILE. */
746 dump_ddrs (FILE *file
, varray_type ddrs
)
750 for (i
= 0; i
< VARRAY_ACTIVE_SIZE (ddrs
); i
++)
752 struct data_dependence_relation
*ddr
=
753 (struct data_dependence_relation
*)
754 VARRAY_GENERIC_PTR (ddrs
, i
);
755 dump_data_dependence_relation (file
, ddr
);
757 fprintf (file
, "\n\n");
762 /* Estimate the number of iterations from the size of the data and the
766 estimate_niter_from_size_of_data (struct loop
*loop
,
771 tree estimation
= NULL_TREE
;
772 tree array_size
, data_size
, element_size
;
775 init
= initial_condition (access_fn
);
776 step
= evolution_part_in_loop_num (access_fn
, loop
->num
);
778 array_size
= TYPE_SIZE (TREE_TYPE (opnd0
));
779 element_size
= TYPE_SIZE (TREE_TYPE (TREE_TYPE (opnd0
)));
780 if (array_size
== NULL_TREE
781 || TREE_CODE (array_size
) != INTEGER_CST
782 || TREE_CODE (element_size
) != INTEGER_CST
)
785 data_size
= fold_build2 (EXACT_DIV_EXPR
, integer_type_node
,
786 array_size
, element_size
);
788 if (init
!= NULL_TREE
790 && TREE_CODE (init
) == INTEGER_CST
791 && TREE_CODE (step
) == INTEGER_CST
)
793 tree i_plus_s
= fold_build2 (PLUS_EXPR
, integer_type_node
, init
, step
);
794 tree sign
= fold_build2 (GT_EXPR
, boolean_type_node
, i_plus_s
, init
);
796 if (sign
== boolean_true_node
)
797 estimation
= fold_build2 (CEIL_DIV_EXPR
, integer_type_node
,
798 fold_build2 (MINUS_EXPR
, integer_type_node
,
799 data_size
, init
), step
);
801 /* When the step is negative, as in PR23386: (init = 3, step =
802 0ffffffff, data_size = 100), we have to compute the
803 estimation as ceil_div (init, 0 - step) + 1. */
804 else if (sign
== boolean_false_node
)
806 fold_build2 (PLUS_EXPR
, integer_type_node
,
807 fold_build2 (CEIL_DIV_EXPR
, integer_type_node
,
809 fold_build2 (MINUS_EXPR
, unsigned_type_node
,
810 integer_zero_node
, step
)),
814 record_estimate (loop
, estimation
, boolean_true_node
, stmt
);
818 /* Given an ARRAY_REF node REF, records its access functions.
819 Example: given A[i][3], record in ACCESS_FNS the opnd1 function,
820 i.e. the constant "3", then recursively call the function on opnd0,
821 i.e. the ARRAY_REF "A[i]".
822 If ESTIMATE_ONLY is true, we just set the estimated number of loop
823 iterations, we don't store the access function.
824 The function returns the base name: "A". */
827 analyze_array_indexes (struct loop
*loop
,
828 VEC(tree
,heap
) **access_fns
,
835 opnd0
= TREE_OPERAND (ref
, 0);
836 opnd1
= TREE_OPERAND (ref
, 1);
838 /* The detection of the evolution function for this data access is
839 postponed until the dependence test. This lazy strategy avoids
840 the computation of access functions that are of no interest for
842 access_fn
= instantiate_parameters
843 (loop
, analyze_scalar_evolution (loop
, opnd1
));
846 && chrec_contains_undetermined (loop
->estimated_nb_iterations
))
847 estimate_niter_from_size_of_data (loop
, opnd0
, access_fn
, stmt
);
850 VEC_safe_push (tree
, heap
, *access_fns
, access_fn
);
852 /* Recursively record other array access functions. */
853 if (TREE_CODE (opnd0
) == ARRAY_REF
)
854 return analyze_array_indexes (loop
, access_fns
, opnd0
, stmt
, estimate_only
);
856 /* Return the base name of the data access. */
861 /* For an array reference REF contained in STMT, attempt to bound the
862 number of iterations in the loop containing STMT */
865 estimate_iters_using_array (tree stmt
, tree ref
)
867 analyze_array_indexes (loop_containing_stmt (stmt
), NULL
, ref
, stmt
,
871 /* For a data reference REF contained in the statement STMT, initialize
872 a DATA_REFERENCE structure, and return it. IS_READ flag has to be
873 set to true when REF is in the right hand side of an
876 struct data_reference
*
877 analyze_array (tree stmt
, tree ref
, bool is_read
)
879 struct data_reference
*res
;
880 VEC(tree
,heap
) *acc_fns
;
882 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
884 fprintf (dump_file
, "(analyze_array \n");
885 fprintf (dump_file
, " (ref = ");
886 print_generic_stmt (dump_file
, ref
, 0);
887 fprintf (dump_file
, ")\n");
890 res
= xmalloc (sizeof (struct data_reference
));
892 DR_STMT (res
) = stmt
;
894 acc_fns
= VEC_alloc (tree
, heap
, 3);
895 DR_BASE_OBJECT (res
) = analyze_array_indexes
896 (loop_containing_stmt (stmt
), &acc_fns
, ref
, stmt
, false);
897 DR_TYPE (res
) = ARRAY_REF_TYPE
;
898 DR_SET_ACCESS_FNS (res
, acc_fns
);
899 DR_IS_READ (res
) = is_read
;
900 DR_BASE_ADDRESS (res
) = NULL_TREE
;
901 DR_OFFSET (res
) = NULL_TREE
;
902 DR_INIT (res
) = NULL_TREE
;
903 DR_STEP (res
) = NULL_TREE
;
904 DR_OFFSET_MISALIGNMENT (res
) = NULL_TREE
;
905 DR_MEMTAG (res
) = NULL_TREE
;
906 DR_PTR_INFO (res
) = NULL
;
908 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
909 fprintf (dump_file
, ")\n");
915 /* Analyze an indirect memory reference, REF, that comes from STMT.
916 IS_READ is true if this is an indirect load, and false if it is
918 Return a new data reference structure representing the indirect_ref, or
919 NULL if we cannot describe the access function. */
921 static struct data_reference
*
922 analyze_indirect_ref (tree stmt
, tree ref
, bool is_read
)
924 struct loop
*loop
= loop_containing_stmt (stmt
);
925 tree ptr_ref
= TREE_OPERAND (ref
, 0);
926 tree access_fn
= analyze_scalar_evolution (loop
, ptr_ref
);
927 tree init
= initial_condition_in_loop_num (access_fn
, loop
->num
);
928 tree base_address
= NULL_TREE
, evolution
, step
= NULL_TREE
;
929 struct ptr_info_def
*ptr_info
= NULL
;
931 if (TREE_CODE (ptr_ref
) == SSA_NAME
)
932 ptr_info
= SSA_NAME_PTR_INFO (ptr_ref
);
935 if (access_fn
== chrec_dont_know
|| !init
|| init
== chrec_dont_know
)
937 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
939 fprintf (dump_file
, "\nBad access function of ptr: ");
940 print_generic_expr (dump_file
, ref
, TDF_SLIM
);
941 fprintf (dump_file
, "\n");
946 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
948 fprintf (dump_file
, "\nAccess function of ptr: ");
949 print_generic_expr (dump_file
, access_fn
, TDF_SLIM
);
950 fprintf (dump_file
, "\n");
953 if (!expr_invariant_in_loop_p (loop
, init
))
955 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
956 fprintf (dump_file
, "\ninitial condition is not loop invariant.\n");
961 evolution
= evolution_part_in_loop_num (access_fn
, loop
->num
);
962 if (evolution
!= chrec_dont_know
)
965 step
= ssize_int (0);
968 if (TREE_CODE (evolution
) == INTEGER_CST
)
969 step
= fold_convert (ssizetype
, evolution
);
971 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
972 fprintf (dump_file
, "\nnon constant step for ptr access.\n");
976 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
977 fprintf (dump_file
, "\nunknown evolution of ptr.\n");
979 return init_data_ref (stmt
, ref
, NULL_TREE
, access_fn
, is_read
, base_address
,
980 NULL_TREE
, step
, NULL_TREE
, NULL_TREE
,
981 ptr_info
, POINTER_REF_TYPE
);
984 /* For a data reference REF contained in the statement STMT, initialize
985 a DATA_REFERENCE structure, and return it. */
987 struct data_reference
*
988 init_data_ref (tree stmt
,
998 struct ptr_info_def
*ptr_info
,
999 enum data_ref_type type
)
1001 struct data_reference
*res
;
1002 VEC(tree
,heap
) *acc_fns
;
1004 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1006 fprintf (dump_file
, "(init_data_ref \n");
1007 fprintf (dump_file
, " (ref = ");
1008 print_generic_stmt (dump_file
, ref
, 0);
1009 fprintf (dump_file
, ")\n");
1012 res
= xmalloc (sizeof (struct data_reference
));
1014 DR_STMT (res
) = stmt
;
1016 DR_BASE_OBJECT (res
) = base
;
1017 DR_TYPE (res
) = type
;
1018 acc_fns
= VEC_alloc (tree
, heap
, 3);
1019 DR_SET_ACCESS_FNS (res
, acc_fns
);
1020 VEC_quick_push (tree
, DR_ACCESS_FNS (res
), access_fn
);
1021 DR_IS_READ (res
) = is_read
;
1022 DR_BASE_ADDRESS (res
) = base_address
;
1023 DR_OFFSET (res
) = init_offset
;
1024 DR_INIT (res
) = NULL_TREE
;
1025 DR_STEP (res
) = step
;
1026 DR_OFFSET_MISALIGNMENT (res
) = misalign
;
1027 DR_MEMTAG (res
) = memtag
;
1028 DR_PTR_INFO (res
) = ptr_info
;
1030 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1031 fprintf (dump_file
, ")\n");
1038 /* Function strip_conversions
1040 Strip conversions that don't narrow the mode. */
1043 strip_conversion (tree expr
)
1045 tree to
, ti
, oprnd0
;
1047 while (TREE_CODE (expr
) == NOP_EXPR
|| TREE_CODE (expr
) == CONVERT_EXPR
)
1049 to
= TREE_TYPE (expr
);
1050 oprnd0
= TREE_OPERAND (expr
, 0);
1051 ti
= TREE_TYPE (oprnd0
);
1053 if (!INTEGRAL_TYPE_P (to
) || !INTEGRAL_TYPE_P (ti
))
1055 if (GET_MODE_SIZE (TYPE_MODE (to
)) < GET_MODE_SIZE (TYPE_MODE (ti
)))
1064 /* Function analyze_offset_expr
1066 Given an offset expression EXPR received from get_inner_reference, analyze
1067 it and create an expression for INITIAL_OFFSET by substituting the variables
1068 of EXPR with initial_condition of the corresponding access_fn in the loop.
1071 for (j = 3; j < N; j++)
1074 For a[j].b[i][j], EXPR will be 'i * C_i + j * C_j + C'. 'i' cannot be
1075 substituted, since its access_fn in the inner loop is i. 'j' will be
1076 substituted with 3. An INITIAL_OFFSET will be 'i * C_i + C`', where
1079 Compute MISALIGN (the misalignment of the data reference initial access from
1080 its base). Misalignment can be calculated only if all the variables can be
1081 substituted with constants, otherwise, we record maximum possible alignment
1082 in ALIGNED_TO. In the above example, since 'i' cannot be substituted, MISALIGN
1083 will be NULL_TREE, and the biggest divider of C_i (a power of 2) will be
1084 recorded in ALIGNED_TO.
1086 STEP is an evolution of the data reference in this loop in bytes.
1087 In the above example, STEP is C_j.
1089 Return FALSE, if the analysis fails, e.g., there is no access_fn for a
1090 variable. In this case, all the outputs (INITIAL_OFFSET, MISALIGN, ALIGNED_TO
1091 and STEP) are NULL_TREEs. Otherwise, return TRUE.
1096 analyze_offset_expr (tree expr
,
1098 tree
*initial_offset
,
1105 tree left_offset
= ssize_int (0);
1106 tree right_offset
= ssize_int (0);
1107 tree left_misalign
= ssize_int (0);
1108 tree right_misalign
= ssize_int (0);
1109 tree left_step
= ssize_int (0);
1110 tree right_step
= ssize_int (0);
1111 enum tree_code code
;
1112 tree init
, evolution
;
1113 tree left_aligned_to
= NULL_TREE
, right_aligned_to
= NULL_TREE
;
1116 *misalign
= NULL_TREE
;
1117 *aligned_to
= NULL_TREE
;
1118 *initial_offset
= NULL_TREE
;
1120 /* Strip conversions that don't narrow the mode. */
1121 expr
= strip_conversion (expr
);
1127 if (TREE_CODE (expr
) == INTEGER_CST
)
1129 *initial_offset
= fold_convert (ssizetype
, expr
);
1130 *misalign
= fold_convert (ssizetype
, expr
);
1131 *step
= ssize_int (0);
1135 /* 2. Variable. Try to substitute with initial_condition of the corresponding
1136 access_fn in the current loop. */
1137 if (SSA_VAR_P (expr
))
1139 tree access_fn
= analyze_scalar_evolution (loop
, expr
);
1141 if (access_fn
== chrec_dont_know
)
1145 init
= initial_condition_in_loop_num (access_fn
, loop
->num
);
1146 if (!expr_invariant_in_loop_p (loop
, init
))
1147 /* Not enough information: may be not loop invariant.
1148 E.g., for a[b[i]], we get a[D], where D=b[i]. EXPR is D, its
1149 initial_condition is D, but it depends on i - loop's induction
1153 evolution
= evolution_part_in_loop_num (access_fn
, loop
->num
);
1154 if (evolution
&& TREE_CODE (evolution
) != INTEGER_CST
)
1155 /* Evolution is not constant. */
1158 if (TREE_CODE (init
) == INTEGER_CST
)
1159 *misalign
= fold_convert (ssizetype
, init
);
1161 /* Not constant, misalignment cannot be calculated. */
1162 *misalign
= NULL_TREE
;
1164 *initial_offset
= fold_convert (ssizetype
, init
);
1166 *step
= evolution
? fold_convert (ssizetype
, evolution
) : ssize_int (0);
1170 /* Recursive computation. */
1171 if (!BINARY_CLASS_P (expr
))
1173 /* We expect to get binary expressions (PLUS/MINUS and MULT). */
1174 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1176 fprintf (dump_file
, "\nNot binary expression ");
1177 print_generic_expr (dump_file
, expr
, TDF_SLIM
);
1178 fprintf (dump_file
, "\n");
1182 oprnd0
= TREE_OPERAND (expr
, 0);
1183 oprnd1
= TREE_OPERAND (expr
, 1);
1185 if (!analyze_offset_expr (oprnd0
, loop
, &left_offset
, &left_misalign
,
1186 &left_aligned_to
, &left_step
)
1187 || !analyze_offset_expr (oprnd1
, loop
, &right_offset
, &right_misalign
,
1188 &right_aligned_to
, &right_step
))
1191 /* The type of the operation: plus, minus or mult. */
1192 code
= TREE_CODE (expr
);
1196 if (TREE_CODE (right_offset
) != INTEGER_CST
)
1197 /* RIGHT_OFFSET can be not constant. For example, for arrays of variable
1199 FORNOW: We don't support such cases. */
1202 /* Strip conversions that don't narrow the mode. */
1203 left_offset
= strip_conversion (left_offset
);
1206 /* Misalignment computation. */
1207 if (SSA_VAR_P (left_offset
))
1209 /* If the left side contains variables that can't be substituted with
1210 constants, the misalignment is unknown. However, if the right side
1211 is a multiple of some alignment, we know that the expression is
1212 aligned to it. Therefore, we record such maximum possible value.
1214 *misalign
= NULL_TREE
;
1215 *aligned_to
= ssize_int (highest_pow2_factor (right_offset
));
1219 /* The left operand was successfully substituted with constant. */
1222 /* In case of EXPR '(i * C1 + j) * C2', LEFT_MISALIGN is
1224 *misalign
= size_binop (code
, left_misalign
, right_misalign
);
1225 if (left_aligned_to
&& right_aligned_to
)
1226 *aligned_to
= size_binop (MIN_EXPR
, left_aligned_to
,
1229 *aligned_to
= left_aligned_to
?
1230 left_aligned_to
: right_aligned_to
;
1233 *misalign
= NULL_TREE
;
1236 /* Step calculation. */
1237 /* Multiply the step by the right operand. */
1238 *step
= size_binop (MULT_EXPR
, left_step
, right_offset
);
1243 /* Combine the recursive calculations for step and misalignment. */
1244 *step
= size_binop (code
, left_step
, right_step
);
1246 /* Unknown alignment. */
1247 if ((!left_misalign
&& !left_aligned_to
)
1248 || (!right_misalign
&& !right_aligned_to
))
1250 *misalign
= NULL_TREE
;
1251 *aligned_to
= NULL_TREE
;
1255 if (left_misalign
&& right_misalign
)
1256 *misalign
= size_binop (code
, left_misalign
, right_misalign
);
1258 *misalign
= left_misalign
? left_misalign
: right_misalign
;
1260 if (left_aligned_to
&& right_aligned_to
)
1261 *aligned_to
= size_binop (MIN_EXPR
, left_aligned_to
, right_aligned_to
);
1263 *aligned_to
= left_aligned_to
? left_aligned_to
: right_aligned_to
;
1271 /* Compute offset. */
1272 *initial_offset
= fold_convert (ssizetype
,
1273 fold_build2 (code
, TREE_TYPE (left_offset
),
1279 /* Function address_analysis
1281 Return the BASE of the address expression EXPR.
1282 Also compute the OFFSET from BASE, MISALIGN and STEP.
1285 EXPR - the address expression that is being analyzed
1286 STMT - the statement that contains EXPR or its original memory reference
1287 IS_READ - TRUE if STMT reads from EXPR, FALSE if writes to EXPR
1288 DR - data_reference struct for the original memory reference
1291 BASE (returned value) - the base of the data reference EXPR.
1292 INITIAL_OFFSET - initial offset of EXPR from BASE (an expression)
1293 MISALIGN - offset of EXPR from BASE in bytes (a constant) or NULL_TREE if the
1294 computation is impossible
1295 ALIGNED_TO - maximum alignment of EXPR or NULL_TREE if MISALIGN can be
1296 calculated (doesn't depend on variables)
1297 STEP - evolution of EXPR in the loop
1299 If something unexpected is encountered (an unsupported form of data-ref),
1300 then NULL_TREE is returned.
1304 address_analysis (tree expr
, tree stmt
, bool is_read
, struct data_reference
*dr
,
1305 tree
*offset
, tree
*misalign
, tree
*aligned_to
, tree
*step
)
1307 tree oprnd0
, oprnd1
, base_address
, offset_expr
, base_addr0
, base_addr1
;
1308 tree address_offset
= ssize_int (0), address_misalign
= ssize_int (0);
1309 tree dummy
, address_aligned_to
= NULL_TREE
;
1310 struct ptr_info_def
*dummy1
;
1313 switch (TREE_CODE (expr
))
1317 /* EXPR is of form {base +/- offset} (or {offset +/- base}). */
1318 oprnd0
= TREE_OPERAND (expr
, 0);
1319 oprnd1
= TREE_OPERAND (expr
, 1);
1321 STRIP_NOPS (oprnd0
);
1322 STRIP_NOPS (oprnd1
);
1324 /* Recursively try to find the base of the address contained in EXPR.
1325 For offset, the returned base will be NULL. */
1326 base_addr0
= address_analysis (oprnd0
, stmt
, is_read
, dr
, &address_offset
,
1327 &address_misalign
, &address_aligned_to
,
1330 base_addr1
= address_analysis (oprnd1
, stmt
, is_read
, dr
, &address_offset
,
1331 &address_misalign
, &address_aligned_to
,
1334 /* We support cases where only one of the operands contains an
1336 if ((base_addr0
&& base_addr1
) || (!base_addr0
&& !base_addr1
))
1338 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1341 "\neither more than one address or no addresses in expr ");
1342 print_generic_expr (dump_file
, expr
, TDF_SLIM
);
1343 fprintf (dump_file
, "\n");
1348 /* To revert STRIP_NOPS. */
1349 oprnd0
= TREE_OPERAND (expr
, 0);
1350 oprnd1
= TREE_OPERAND (expr
, 1);
1352 offset_expr
= base_addr0
?
1353 fold_convert (ssizetype
, oprnd1
) : fold_convert (ssizetype
, oprnd0
);
1355 /* EXPR is of form {base +/- offset} (or {offset +/- base}). If offset is
1356 a number, we can add it to the misalignment value calculated for base,
1357 otherwise, misalignment is NULL. */
1358 if (TREE_CODE (offset_expr
) == INTEGER_CST
&& address_misalign
)
1360 *misalign
= size_binop (TREE_CODE (expr
), address_misalign
,
1362 *aligned_to
= address_aligned_to
;
1366 *misalign
= NULL_TREE
;
1367 *aligned_to
= NULL_TREE
;
1370 /* Combine offset (from EXPR {base + offset}) with the offset calculated
1372 *offset
= size_binop (TREE_CODE (expr
), address_offset
, offset_expr
);
1373 return base_addr0
? base_addr0
: base_addr1
;
1376 base_address
= object_analysis (TREE_OPERAND (expr
, 0), stmt
, is_read
,
1377 &dr
, offset
, misalign
, aligned_to
, step
,
1378 &dummy
, &dummy1
, &dummy2
);
1379 return base_address
;
1382 if (!POINTER_TYPE_P (TREE_TYPE (expr
)))
1384 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1386 fprintf (dump_file
, "\nnot pointer SSA_NAME ");
1387 print_generic_expr (dump_file
, expr
, TDF_SLIM
);
1388 fprintf (dump_file
, "\n");
1392 *aligned_to
= ssize_int (TYPE_ALIGN_UNIT (TREE_TYPE (TREE_TYPE (expr
))));
1393 *misalign
= ssize_int (0);
1394 *offset
= ssize_int (0);
1395 *step
= ssize_int (0);
1404 /* Function object_analysis
1406 Create a data-reference structure DR for MEMREF.
1407 Return the BASE of the data reference MEMREF if the analysis is possible.
1408 Also compute the INITIAL_OFFSET from BASE, MISALIGN and STEP.
1409 E.g., for EXPR a.b[i] + 4B, BASE is a, and OFFSET is the overall offset
1410 'a.b[i] + 4B' from a (can be an expression), MISALIGN is an OFFSET
1411 instantiated with initial_conditions of access_functions of variables,
1412 and STEP is the evolution of the DR_REF in this loop.
1414 Function get_inner_reference is used for the above in case of ARRAY_REF and
1417 The structure of the function is as follows:
1419 Case 1. For handled_component_p refs
1420 1.1 build data-reference structure for MEMREF
1421 1.2 call get_inner_reference
1422 1.2.1 analyze offset expr received from get_inner_reference
1423 (fall through with BASE)
1424 Case 2. For declarations
1426 Case 3. For INDIRECT_REFs
1427 3.1 build data-reference structure for MEMREF
1428 3.2 analyze evolution and initial condition of MEMREF
1429 3.3 set data-reference structure for MEMREF
1430 3.4 call address_analysis to analyze INIT of the access function
1431 3.5 extract memory tag
1434 Combine the results of object and address analysis to calculate
1435 INITIAL_OFFSET, STEP and misalignment info.
1438 MEMREF - the memory reference that is being analyzed
1439 STMT - the statement that contains MEMREF
1440 IS_READ - TRUE if STMT reads from MEMREF, FALSE if writes to MEMREF
1443 BASE_ADDRESS (returned value) - the base address of the data reference MEMREF
1444 E.g, if MEMREF is a.b[k].c[i][j] the returned
1446 DR - data_reference struct for MEMREF
1447 INITIAL_OFFSET - initial offset of MEMREF from BASE (an expression)
1448 MISALIGN - offset of MEMREF from BASE in bytes (a constant) modulo alignment of
1449 ALIGNMENT or NULL_TREE if the computation is impossible
1450 ALIGNED_TO - maximum alignment of EXPR or NULL_TREE if MISALIGN can be
1451 calculated (doesn't depend on variables)
1452 STEP - evolution of the DR_REF in the loop
1453 MEMTAG - memory tag for aliasing purposes
1454 PTR_INFO - NULL or points-to aliasing info from a pointer SSA_NAME
1455 SUBVARS - Sub-variables of the variable
1457 If the analysis of MEMREF evolution in the loop fails, NULL_TREE is returned,
1458 but DR can be created anyway.
1463 object_analysis (tree memref
, tree stmt
, bool is_read
,
1464 struct data_reference
**dr
, tree
*offset
, tree
*misalign
,
1465 tree
*aligned_to
, tree
*step
, tree
*memtag
,
1466 struct ptr_info_def
**ptr_info
, subvar_t
*subvars
)
1468 tree base
= NULL_TREE
, base_address
= NULL_TREE
;
1469 tree object_offset
= ssize_int (0), object_misalign
= ssize_int (0);
1470 tree object_step
= ssize_int (0), address_step
= ssize_int (0);
1471 tree address_offset
= ssize_int (0), address_misalign
= ssize_int (0);
1472 HOST_WIDE_INT pbitsize
, pbitpos
;
1473 tree poffset
, bit_pos_in_bytes
;
1474 enum machine_mode pmode
;
1475 int punsignedp
, pvolatilep
;
1476 tree ptr_step
= ssize_int (0), ptr_init
= NULL_TREE
;
1477 struct loop
*loop
= loop_containing_stmt (stmt
);
1478 struct data_reference
*ptr_dr
= NULL
;
1479 tree object_aligned_to
= NULL_TREE
, address_aligned_to
= NULL_TREE
;
1484 /* Case 1. handled_component_p refs. */
1485 if (handled_component_p (memref
))
1487 /* 1.1 build data-reference structure for MEMREF. */
1488 /* TODO: handle COMPONENT_REFs. */
1491 if (TREE_CODE (memref
) == ARRAY_REF
)
1492 *dr
= analyze_array (stmt
, memref
, is_read
);
1496 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1498 fprintf (dump_file
, "\ndata-ref of unsupported type ");
1499 print_generic_expr (dump_file
, memref
, TDF_SLIM
);
1500 fprintf (dump_file
, "\n");
1506 /* 1.2 call get_inner_reference. */
1507 /* Find the base and the offset from it. */
1508 base
= get_inner_reference (memref
, &pbitsize
, &pbitpos
, &poffset
,
1509 &pmode
, &punsignedp
, &pvolatilep
, false);
1512 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1514 fprintf (dump_file
, "\nfailed to get inner ref for ");
1515 print_generic_expr (dump_file
, memref
, TDF_SLIM
);
1516 fprintf (dump_file
, "\n");
1521 /* 1.2.1 analyze offset expr received from get_inner_reference. */
1523 && !analyze_offset_expr (poffset
, loop
, &object_offset
,
1524 &object_misalign
, &object_aligned_to
,
1527 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1529 fprintf (dump_file
, "\nfailed to compute offset or step for ");
1530 print_generic_expr (dump_file
, memref
, TDF_SLIM
);
1531 fprintf (dump_file
, "\n");
1536 /* Add bit position to OFFSET and MISALIGN. */
1538 bit_pos_in_bytes
= ssize_int (pbitpos
/BITS_PER_UNIT
);
1539 /* Check that there is no remainder in bits. */
1540 if (pbitpos
%BITS_PER_UNIT
)
1542 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1543 fprintf (dump_file
, "\nbit offset alignment.\n");
1546 object_offset
= size_binop (PLUS_EXPR
, bit_pos_in_bytes
, object_offset
);
1547 if (object_misalign
)
1548 object_misalign
= size_binop (PLUS_EXPR
, object_misalign
,
1551 memref
= base
; /* To continue analysis of BASE. */
1555 /* Part 1: Case 2. Declarations. */
1556 if (DECL_P (memref
))
1558 /* We expect to get a decl only if we already have a DR. */
1561 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1563 fprintf (dump_file
, "\nunhandled decl ");
1564 print_generic_expr (dump_file
, memref
, TDF_SLIM
);
1565 fprintf (dump_file
, "\n");
1570 /* TODO: if during the analysis of INDIRECT_REF we get to an object, put
1571 the object in BASE_OBJECT field if we can prove that this is O.K.,
1572 i.e., the data-ref access is bounded by the bounds of the BASE_OBJECT.
1573 (e.g., if the object is an array base 'a', where 'a[N]', we must prove
1574 that every access with 'p' (the original INDIRECT_REF based on '&a')
1575 in the loop is within the array boundaries - from a[0] to a[N-1]).
1576 Otherwise, our alias analysis can be incorrect.
1577 Even if an access function based on BASE_OBJECT can't be build, update
1578 BASE_OBJECT field to enable us to prove that two data-refs are
1579 different (without access function, distance analysis is impossible).
1581 if (SSA_VAR_P (memref
) && var_can_have_subvars (memref
))
1582 *subvars
= get_subvars_for_var (memref
);
1583 base_address
= build_fold_addr_expr (memref
);
1584 /* 2.1 set MEMTAG. */
1588 /* Part 1: Case 3. INDIRECT_REFs. */
1589 else if (TREE_CODE (memref
) == INDIRECT_REF
)
1591 tree ptr_ref
= TREE_OPERAND (memref
, 0);
1592 if (TREE_CODE (ptr_ref
) == SSA_NAME
)
1593 *ptr_info
= SSA_NAME_PTR_INFO (ptr_ref
);
1595 /* 3.1 build data-reference structure for MEMREF. */
1596 ptr_dr
= analyze_indirect_ref (stmt
, memref
, is_read
);
1599 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1601 fprintf (dump_file
, "\nfailed to create dr for ");
1602 print_generic_expr (dump_file
, memref
, TDF_SLIM
);
1603 fprintf (dump_file
, "\n");
1608 /* 3.2 analyze evolution and initial condition of MEMREF. */
1609 ptr_step
= DR_STEP (ptr_dr
);
1610 ptr_init
= DR_BASE_ADDRESS (ptr_dr
);
1611 if (!ptr_init
|| !ptr_step
|| !POINTER_TYPE_P (TREE_TYPE (ptr_init
)))
1613 *dr
= (*dr
) ? *dr
: ptr_dr
;
1614 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1616 fprintf (dump_file
, "\nbad pointer access ");
1617 print_generic_expr (dump_file
, memref
, TDF_SLIM
);
1618 fprintf (dump_file
, "\n");
1623 if (integer_zerop (ptr_step
) && !(*dr
))
1625 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1626 fprintf (dump_file
, "\nptr is loop invariant.\n");
1630 /* If there exists DR for MEMREF, we are analyzing the base of
1631 handled component (PTR_INIT), which not necessary has evolution in
1634 object_step
= size_binop (PLUS_EXPR
, object_step
, ptr_step
);
1636 /* 3.3 set data-reference structure for MEMREF. */
1640 /* 3.4 call address_analysis to analyze INIT of the access
1642 base_address
= address_analysis (ptr_init
, stmt
, is_read
, *dr
,
1643 &address_offset
, &address_misalign
,
1644 &address_aligned_to
, &address_step
);
1647 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1649 fprintf (dump_file
, "\nfailed to analyze address ");
1650 print_generic_expr (dump_file
, ptr_init
, TDF_SLIM
);
1651 fprintf (dump_file
, "\n");
1656 /* 3.5 extract memory tag. */
1657 switch (TREE_CODE (base_address
))
1660 *memtag
= get_var_ann (SSA_NAME_VAR (base_address
))->type_mem_tag
;
1661 if (!(*memtag
) && TREE_CODE (TREE_OPERAND (memref
, 0)) == SSA_NAME
)
1662 *memtag
= get_var_ann (
1663 SSA_NAME_VAR (TREE_OPERAND (memref
, 0)))->type_mem_tag
;
1666 *memtag
= TREE_OPERAND (base_address
, 0);
1669 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1671 fprintf (dump_file
, "\nno memtag for ");
1672 print_generic_expr (dump_file
, memref
, TDF_SLIM
);
1673 fprintf (dump_file
, "\n");
1675 *memtag
= NULL_TREE
;
1682 /* MEMREF cannot be analyzed. */
1683 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1685 fprintf (dump_file
, "\ndata-ref of unsupported type ");
1686 print_generic_expr (dump_file
, memref
, TDF_SLIM
);
1687 fprintf (dump_file
, "\n");
1692 if (SSA_VAR_P (*memtag
) && var_can_have_subvars (*memtag
))
1693 *subvars
= get_subvars_for_var (*memtag
);
1695 /* Part 2: Combine the results of object and address analysis to calculate
1696 INITIAL_OFFSET, STEP and misalignment info. */
1697 *offset
= size_binop (PLUS_EXPR
, object_offset
, address_offset
);
1699 if ((!object_misalign
&& !object_aligned_to
)
1700 || (!address_misalign
&& !address_aligned_to
))
1702 *misalign
= NULL_TREE
;
1703 *aligned_to
= NULL_TREE
;
1707 if (object_misalign
&& address_misalign
)
1708 *misalign
= size_binop (PLUS_EXPR
, object_misalign
, address_misalign
);
1710 *misalign
= object_misalign
? object_misalign
: address_misalign
;
1711 if (object_aligned_to
&& address_aligned_to
)
1712 *aligned_to
= size_binop (MIN_EXPR
, object_aligned_to
,
1713 address_aligned_to
);
1715 *aligned_to
= object_aligned_to
?
1716 object_aligned_to
: address_aligned_to
;
1718 *step
= size_binop (PLUS_EXPR
, object_step
, address_step
);
1720 return base_address
;
1723 /* Function analyze_offset.
1725 Extract INVARIANT and CONSTANT parts from OFFSET.
1729 analyze_offset (tree offset
, tree
*invariant
, tree
*constant
)
1731 tree op0
, op1
, constant_0
, constant_1
, invariant_0
, invariant_1
;
1732 enum tree_code code
= TREE_CODE (offset
);
1734 *invariant
= NULL_TREE
;
1735 *constant
= NULL_TREE
;
1737 /* Not PLUS/MINUS expression - recursion stop condition. */
1738 if (code
!= PLUS_EXPR
&& code
!= MINUS_EXPR
)
1740 if (TREE_CODE (offset
) == INTEGER_CST
)
1743 *invariant
= offset
;
1747 op0
= TREE_OPERAND (offset
, 0);
1748 op1
= TREE_OPERAND (offset
, 1);
1750 /* Recursive call with the operands. */
1751 if (!analyze_offset (op0
, &invariant_0
, &constant_0
)
1752 || !analyze_offset (op1
, &invariant_1
, &constant_1
))
1755 /* Combine the results. Add negation to the subtrahend in case of
1757 if (constant_0
&& constant_1
)
1759 *constant
= constant_0
? constant_0
: constant_1
;
1760 if (code
== MINUS_EXPR
&& constant_1
)
1761 *constant
= fold_build1 (NEGATE_EXPR
, TREE_TYPE (*constant
), *constant
);
1763 if (invariant_0
&& invariant_1
)
1765 fold_build2 (code
, TREE_TYPE (invariant_0
), invariant_0
, invariant_1
);
1768 *invariant
= invariant_0
? invariant_0
: invariant_1
;
1769 if (code
== MINUS_EXPR
&& invariant_1
)
1771 fold_build1 (NEGATE_EXPR
, TREE_TYPE (*invariant
), *invariant
);
1777 /* Function create_data_ref.
1779 Create a data-reference structure for MEMREF. Set its DR_BASE_ADDRESS,
1780 DR_OFFSET, DR_INIT, DR_STEP, DR_OFFSET_MISALIGNMENT, DR_ALIGNED_TO,
1781 DR_MEMTAG, and DR_POINTSTO_INFO fields.
1784 MEMREF - the memory reference that is being analyzed
1785 STMT - the statement that contains MEMREF
1786 IS_READ - TRUE if STMT reads from MEMREF, FALSE if writes to MEMREF
1789 DR (returned value) - data_reference struct for MEMREF
1792 static struct data_reference
*
1793 create_data_ref (tree memref
, tree stmt
, bool is_read
)
1795 struct data_reference
*dr
= NULL
;
1796 tree base_address
, offset
, step
, misalign
, memtag
;
1797 struct loop
*loop
= loop_containing_stmt (stmt
);
1798 tree invariant
= NULL_TREE
, constant
= NULL_TREE
;
1799 tree type_size
, init_cond
;
1800 struct ptr_info_def
*ptr_info
;
1801 subvar_t subvars
= NULL
;
1807 base_address
= object_analysis (memref
, stmt
, is_read
, &dr
, &offset
,
1808 &misalign
, &aligned_to
, &step
, &memtag
,
1809 &ptr_info
, &subvars
);
1810 if (!dr
|| !base_address
)
1812 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1814 fprintf (dump_file
, "\ncreate_data_ref: failed to create a dr for ");
1815 print_generic_expr (dump_file
, memref
, TDF_SLIM
);
1816 fprintf (dump_file
, "\n");
1821 DR_BASE_ADDRESS (dr
) = base_address
;
1822 DR_OFFSET (dr
) = offset
;
1823 DR_INIT (dr
) = ssize_int (0);
1824 DR_STEP (dr
) = step
;
1825 DR_OFFSET_MISALIGNMENT (dr
) = misalign
;
1826 DR_ALIGNED_TO (dr
) = aligned_to
;
1827 DR_MEMTAG (dr
) = memtag
;
1828 DR_PTR_INFO (dr
) = ptr_info
;
1829 DR_SUBVARS (dr
) = subvars
;
1831 type_size
= fold_convert (ssizetype
, TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dr
))));
1833 /* Change the access function for INIDIRECT_REFs, according to
1834 DR_BASE_ADDRESS. Analyze OFFSET calculated in object_analysis. OFFSET is
1835 an expression that can contain loop invariant expressions and constants.
1836 We put the constant part in the initial condition of the access function
1837 (for data dependence tests), and in DR_INIT of the data-ref. The loop
1838 invariant part is put in DR_OFFSET.
1839 The evolution part of the access function is STEP calculated in
1840 object_analysis divided by the size of data type.
1842 if (!DR_BASE_OBJECT (dr
))
1847 /* Extract CONSTANT and INVARIANT from OFFSET, and put them in DR_INIT and
1848 DR_OFFSET fields of DR. */
1849 if (!analyze_offset (offset
, &invariant
, &constant
))
1851 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1853 fprintf (dump_file
, "\ncreate_data_ref: failed to analyze dr's");
1854 fprintf (dump_file
, " offset for ");
1855 print_generic_expr (dump_file
, memref
, TDF_SLIM
);
1856 fprintf (dump_file
, "\n");
1862 DR_INIT (dr
) = fold_convert (ssizetype
, constant
);
1863 init_cond
= fold_build2 (TRUNC_DIV_EXPR
, TREE_TYPE (constant
),
1864 constant
, type_size
);
1867 DR_INIT (dr
) = init_cond
= ssize_int (0);;
1870 DR_OFFSET (dr
) = invariant
;
1872 DR_OFFSET (dr
) = ssize_int (0);
1874 /* Update access function. */
1875 access_fn
= DR_ACCESS_FN (dr
, 0);
1876 new_step
= size_binop (TRUNC_DIV_EXPR
,
1877 fold_convert (ssizetype
, step
), type_size
);
1879 access_fn
= chrec_replace_initial_condition (access_fn
, init_cond
);
1880 access_fn
= reset_evolution_in_loop (loop
->num
, access_fn
, new_step
);
1882 VEC_replace (tree
, DR_ACCESS_FNS (dr
), 0, access_fn
);
1885 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1887 struct ptr_info_def
*pi
= DR_PTR_INFO (dr
);
1889 fprintf (dump_file
, "\nCreated dr for ");
1890 print_generic_expr (dump_file
, memref
, TDF_SLIM
);
1891 fprintf (dump_file
, "\n\tbase_address: ");
1892 print_generic_expr (dump_file
, DR_BASE_ADDRESS (dr
), TDF_SLIM
);
1893 fprintf (dump_file
, "\n\toffset from base address: ");
1894 print_generic_expr (dump_file
, DR_OFFSET (dr
), TDF_SLIM
);
1895 fprintf (dump_file
, "\n\tconstant offset from base address: ");
1896 print_generic_expr (dump_file
, DR_INIT (dr
), TDF_SLIM
);
1897 fprintf (dump_file
, "\n\tbase_object: ");
1898 print_generic_expr (dump_file
, DR_BASE_OBJECT (dr
), TDF_SLIM
);
1899 fprintf (dump_file
, "\n\tstep: ");
1900 print_generic_expr (dump_file
, DR_STEP (dr
), TDF_SLIM
);
1901 fprintf (dump_file
, "B\n\tmisalignment from base: ");
1902 print_generic_expr (dump_file
, DR_OFFSET_MISALIGNMENT (dr
), TDF_SLIM
);
1903 if (DR_OFFSET_MISALIGNMENT (dr
))
1904 fprintf (dump_file
, "B");
1905 if (DR_ALIGNED_TO (dr
))
1907 fprintf (dump_file
, "\n\taligned to: ");
1908 print_generic_expr (dump_file
, DR_ALIGNED_TO (dr
), TDF_SLIM
);
1910 fprintf (dump_file
, "\n\tmemtag: ");
1911 print_generic_expr (dump_file
, DR_MEMTAG (dr
), TDF_SLIM
);
1912 fprintf (dump_file
, "\n");
1913 if (pi
&& pi
->name_mem_tag
)
1915 fprintf (dump_file
, "\n\tnametag: ");
1916 print_generic_expr (dump_file
, pi
->name_mem_tag
, TDF_SLIM
);
1917 fprintf (dump_file
, "\n");
1924 /* Returns true when all the functions of a tree_vec CHREC are the
1928 all_chrecs_equal_p (tree chrec
)
1932 for (j
= 0; j
< TREE_VEC_LENGTH (chrec
) - 1; j
++)
1934 tree chrec_j
= TREE_VEC_ELT (chrec
, j
);
1935 tree chrec_j_1
= TREE_VEC_ELT (chrec
, j
+ 1);
1938 (integer_type_node
, chrec_j
, chrec_j_1
)))
1944 /* Determine for each subscript in the data dependence relation DDR
1948 compute_subscript_distance (struct data_dependence_relation
*ddr
)
1950 if (DDR_ARE_DEPENDENT (ddr
) == NULL_TREE
)
1954 for (i
= 0; i
< DDR_NUM_SUBSCRIPTS (ddr
); i
++)
1956 tree conflicts_a
, conflicts_b
, difference
;
1957 struct subscript
*subscript
;
1959 subscript
= DDR_SUBSCRIPT (ddr
, i
);
1960 conflicts_a
= SUB_CONFLICTS_IN_A (subscript
);
1961 conflicts_b
= SUB_CONFLICTS_IN_B (subscript
);
1963 if (TREE_CODE (conflicts_a
) == TREE_VEC
)
1965 if (!all_chrecs_equal_p (conflicts_a
))
1967 SUB_DISTANCE (subscript
) = chrec_dont_know
;
1971 conflicts_a
= TREE_VEC_ELT (conflicts_a
, 0);
1974 if (TREE_CODE (conflicts_b
) == TREE_VEC
)
1976 if (!all_chrecs_equal_p (conflicts_b
))
1978 SUB_DISTANCE (subscript
) = chrec_dont_know
;
1982 conflicts_b
= TREE_VEC_ELT (conflicts_b
, 0);
1985 difference
= chrec_fold_minus
1986 (integer_type_node
, conflicts_b
, conflicts_a
);
1988 if (evolution_function_is_constant_p (difference
))
1989 SUB_DISTANCE (subscript
) = difference
;
1992 SUB_DISTANCE (subscript
) = chrec_dont_know
;
1997 /* Initialize a ddr. */
1999 struct data_dependence_relation
*
2000 initialize_data_dependence_relation (struct data_reference
*a
,
2001 struct data_reference
*b
)
2003 struct data_dependence_relation
*res
;
2007 res
= xmalloc (sizeof (struct data_dependence_relation
));
2011 if (a
== NULL
|| b
== NULL
)
2013 DDR_ARE_DEPENDENT (res
) = chrec_dont_know
;
2017 /* When A and B are arrays and their dimensions differ, we directly
2018 initialize the relation to "there is no dependence": chrec_known. */
2019 if (DR_BASE_OBJECT (a
) && DR_BASE_OBJECT (b
)
2020 && DR_NUM_DIMENSIONS (a
) != DR_NUM_DIMENSIONS (b
))
2022 DDR_ARE_DEPENDENT (res
) = chrec_known
;
2026 /* Compare the bases of the data-refs. */
2027 if (!base_addr_differ_p (a
, b
, &differ_p
))
2029 /* Can't determine whether the data-refs access the same memory
2031 DDR_ARE_DEPENDENT (res
) = chrec_dont_know
;
2036 DDR_ARE_DEPENDENT (res
) = chrec_known
;
2040 DDR_AFFINE_P (res
) = true;
2041 DDR_ARE_DEPENDENT (res
) = NULL_TREE
;
2042 DDR_SUBSCRIPTS_VECTOR_INIT (res
, DR_NUM_DIMENSIONS (a
));
2043 DDR_SIZE_VECT (res
) = 0;
2044 DDR_DIR_VECTS (res
) = NULL
;
2045 DDR_DIST_VECTS (res
) = NULL
;
2047 for (i
= 0; i
< DR_NUM_DIMENSIONS (a
); i
++)
2049 struct subscript
*subscript
;
2051 subscript
= xmalloc (sizeof (struct subscript
));
2052 SUB_CONFLICTS_IN_A (subscript
) = chrec_dont_know
;
2053 SUB_CONFLICTS_IN_B (subscript
) = chrec_dont_know
;
2054 SUB_LAST_CONFLICT (subscript
) = chrec_dont_know
;
2055 SUB_DISTANCE (subscript
) = chrec_dont_know
;
2056 VARRAY_PUSH_GENERIC_PTR (DDR_SUBSCRIPTS (res
), subscript
);
2062 /* Set DDR_ARE_DEPENDENT to CHREC and finalize the subscript overlap
2066 finalize_ddr_dependent (struct data_dependence_relation
*ddr
,
2069 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2071 fprintf (dump_file
, "(dependence classified: ");
2072 print_generic_expr (dump_file
, chrec
, 0);
2073 fprintf (dump_file
, ")\n");
2076 DDR_ARE_DEPENDENT (ddr
) = chrec
;
2077 varray_clear (DDR_SUBSCRIPTS (ddr
));
2080 /* The dependence relation DDR cannot be represented by a distance
2084 non_affine_dependence_relation (struct data_dependence_relation
*ddr
)
2086 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2087 fprintf (dump_file
, "(Dependence relation cannot be represented by distance vector.) \n");
2089 DDR_AFFINE_P (ddr
) = false;
2094 /* This section contains the classic Banerjee tests. */
2096 /* Returns true iff CHREC_A and CHREC_B are not dependent on any index
2097 variables, i.e., if the ZIV (Zero Index Variable) test is true. */
2100 ziv_subscript_p (tree chrec_a
,
2103 return (evolution_function_is_constant_p (chrec_a
)
2104 && evolution_function_is_constant_p (chrec_b
));
2107 /* Returns true iff CHREC_A and CHREC_B are dependent on an index
2108 variable, i.e., if the SIV (Single Index Variable) test is true. */
2111 siv_subscript_p (tree chrec_a
,
2114 if ((evolution_function_is_constant_p (chrec_a
)
2115 && evolution_function_is_univariate_p (chrec_b
))
2116 || (evolution_function_is_constant_p (chrec_b
)
2117 && evolution_function_is_univariate_p (chrec_a
)))
2120 if (evolution_function_is_univariate_p (chrec_a
)
2121 && evolution_function_is_univariate_p (chrec_b
))
2123 switch (TREE_CODE (chrec_a
))
2125 case POLYNOMIAL_CHREC
:
2126 switch (TREE_CODE (chrec_b
))
2128 case POLYNOMIAL_CHREC
:
2129 if (CHREC_VARIABLE (chrec_a
) != CHREC_VARIABLE (chrec_b
))
2144 /* Analyze a ZIV (Zero Index Variable) subscript. *OVERLAPS_A and
2145 *OVERLAPS_B are initialized to the functions that describe the
2146 relation between the elements accessed twice by CHREC_A and
2147 CHREC_B. For k >= 0, the following property is verified:
2149 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
2152 analyze_ziv_subscript (tree chrec_a
,
2156 tree
*last_conflicts
)
2160 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2161 fprintf (dump_file
, "(analyze_ziv_subscript \n");
2163 difference
= chrec_fold_minus (integer_type_node
, chrec_a
, chrec_b
);
2165 switch (TREE_CODE (difference
))
2168 if (integer_zerop (difference
))
2170 /* The difference is equal to zero: the accessed index
2171 overlaps for each iteration in the loop. */
2172 *overlaps_a
= integer_zero_node
;
2173 *overlaps_b
= integer_zero_node
;
2174 *last_conflicts
= chrec_dont_know
;
2178 /* The accesses do not overlap. */
2179 *overlaps_a
= chrec_known
;
2180 *overlaps_b
= chrec_known
;
2181 *last_conflicts
= integer_zero_node
;
2186 /* We're not sure whether the indexes overlap. For the moment,
2187 conservatively answer "don't know". */
2188 *overlaps_a
= chrec_dont_know
;
2189 *overlaps_b
= chrec_dont_know
;
2190 *last_conflicts
= chrec_dont_know
;
2194 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2195 fprintf (dump_file
, ")\n");
2198 /* Get the real or estimated number of iterations for LOOPNUM, whichever is
2199 available. Return the number of iterations as a tree, or NULL_TREE if
2203 get_number_of_iters_for_loop (int loopnum
)
2205 tree numiter
= number_of_iterations_in_loop (current_loops
->parray
[loopnum
]);
2207 if (TREE_CODE (numiter
) != INTEGER_CST
)
2208 numiter
= current_loops
->parray
[loopnum
]->estimated_nb_iterations
;
2209 if (chrec_contains_undetermined (numiter
))
2214 /* Analyze a SIV (Single Index Variable) subscript where CHREC_A is a
2215 constant, and CHREC_B is an affine function. *OVERLAPS_A and
2216 *OVERLAPS_B are initialized to the functions that describe the
2217 relation between the elements accessed twice by CHREC_A and
2218 CHREC_B. For k >= 0, the following property is verified:
2220 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
2223 analyze_siv_subscript_cst_affine (tree chrec_a
,
2227 tree
*last_conflicts
)
2229 bool value0
, value1
, value2
;
2230 tree difference
= chrec_fold_minus
2231 (integer_type_node
, CHREC_LEFT (chrec_b
), chrec_a
);
2233 if (!chrec_is_positive (initial_condition (difference
), &value0
))
2235 *overlaps_a
= chrec_dont_know
;
2236 *overlaps_b
= chrec_dont_know
;
2237 *last_conflicts
= chrec_dont_know
;
2242 if (value0
== false)
2244 if (!chrec_is_positive (CHREC_RIGHT (chrec_b
), &value1
))
2246 *overlaps_a
= chrec_dont_know
;
2247 *overlaps_b
= chrec_dont_know
;
2248 *last_conflicts
= chrec_dont_know
;
2257 chrec_b = {10, +, 1}
2260 if (tree_fold_divides_p (CHREC_RIGHT (chrec_b
), difference
))
2263 int loopnum
= CHREC_VARIABLE (chrec_b
);
2265 *overlaps_a
= integer_zero_node
;
2266 *overlaps_b
= fold_build2 (EXACT_DIV_EXPR
, integer_type_node
,
2267 fold_build1 (ABS_EXPR
,
2270 CHREC_RIGHT (chrec_b
));
2271 *last_conflicts
= integer_one_node
;
2274 /* Perform weak-zero siv test to see if overlap is
2275 outside the loop bounds. */
2276 numiter
= get_number_of_iters_for_loop (loopnum
);
2278 if (numiter
!= NULL_TREE
2279 && TREE_CODE (*overlaps_b
) == INTEGER_CST
2280 && tree_int_cst_lt (numiter
, *overlaps_b
))
2282 *overlaps_a
= chrec_known
;
2283 *overlaps_b
= chrec_known
;
2284 *last_conflicts
= integer_zero_node
;
2290 /* When the step does not divide the difference, there are
2294 *overlaps_a
= chrec_known
;
2295 *overlaps_b
= chrec_known
;
2296 *last_conflicts
= integer_zero_node
;
2305 chrec_b = {10, +, -1}
2307 In this case, chrec_a will not overlap with chrec_b. */
2308 *overlaps_a
= chrec_known
;
2309 *overlaps_b
= chrec_known
;
2310 *last_conflicts
= integer_zero_node
;
2317 if (!chrec_is_positive (CHREC_RIGHT (chrec_b
), &value2
))
2319 *overlaps_a
= chrec_dont_know
;
2320 *overlaps_b
= chrec_dont_know
;
2321 *last_conflicts
= chrec_dont_know
;
2326 if (value2
== false)
2330 chrec_b = {10, +, -1}
2332 if (tree_fold_divides_p (CHREC_RIGHT (chrec_b
), difference
))
2335 int loopnum
= CHREC_VARIABLE (chrec_b
);
2337 *overlaps_a
= integer_zero_node
;
2338 *overlaps_b
= fold_build2 (EXACT_DIV_EXPR
,
2339 integer_type_node
, difference
,
2340 CHREC_RIGHT (chrec_b
));
2341 *last_conflicts
= integer_one_node
;
2343 /* Perform weak-zero siv test to see if overlap is
2344 outside the loop bounds. */
2345 numiter
= get_number_of_iters_for_loop (loopnum
);
2347 if (numiter
!= NULL_TREE
2348 && TREE_CODE (*overlaps_b
) == INTEGER_CST
2349 && tree_int_cst_lt (numiter
, *overlaps_b
))
2351 *overlaps_a
= chrec_known
;
2352 *overlaps_b
= chrec_known
;
2353 *last_conflicts
= integer_zero_node
;
2359 /* When the step does not divide the difference, there
2363 *overlaps_a
= chrec_known
;
2364 *overlaps_b
= chrec_known
;
2365 *last_conflicts
= integer_zero_node
;
2375 In this case, chrec_a will not overlap with chrec_b. */
2376 *overlaps_a
= chrec_known
;
2377 *overlaps_b
= chrec_known
;
2378 *last_conflicts
= integer_zero_node
;
2386 /* Helper recursive function for initializing the matrix A. Returns
2387 the initial value of CHREC. */
2390 initialize_matrix_A (lambda_matrix A
, tree chrec
, unsigned index
, int mult
)
2394 if (TREE_CODE (chrec
) != POLYNOMIAL_CHREC
)
2395 return int_cst_value (chrec
);
2397 A
[index
][0] = mult
* int_cst_value (CHREC_RIGHT (chrec
));
2398 return initialize_matrix_A (A
, CHREC_LEFT (chrec
), index
+ 1, mult
);
2401 #define FLOOR_DIV(x,y) ((x) / (y))
2403 /* Solves the special case of the Diophantine equation:
2404 | {0, +, STEP_A}_x (OVERLAPS_A) = {0, +, STEP_B}_y (OVERLAPS_B)
2406 Computes the descriptions OVERLAPS_A and OVERLAPS_B. NITER is the
2407 number of iterations that loops X and Y run. The overlaps will be
2408 constructed as evolutions in dimension DIM. */
2411 compute_overlap_steps_for_affine_univar (int niter
, int step_a
, int step_b
,
2412 tree
*overlaps_a
, tree
*overlaps_b
,
2413 tree
*last_conflicts
, int dim
)
2415 if (((step_a
> 0 && step_b
> 0)
2416 || (step_a
< 0 && step_b
< 0)))
2418 int step_overlaps_a
, step_overlaps_b
;
2419 int gcd_steps_a_b
, last_conflict
, tau2
;
2421 gcd_steps_a_b
= gcd (step_a
, step_b
);
2422 step_overlaps_a
= step_b
/ gcd_steps_a_b
;
2423 step_overlaps_b
= step_a
/ gcd_steps_a_b
;
2425 tau2
= FLOOR_DIV (niter
, step_overlaps_a
);
2426 tau2
= MIN (tau2
, FLOOR_DIV (niter
, step_overlaps_b
));
2427 last_conflict
= tau2
;
2429 *overlaps_a
= build_polynomial_chrec
2430 (dim
, integer_zero_node
,
2431 build_int_cst (NULL_TREE
, step_overlaps_a
));
2432 *overlaps_b
= build_polynomial_chrec
2433 (dim
, integer_zero_node
,
2434 build_int_cst (NULL_TREE
, step_overlaps_b
));
2435 *last_conflicts
= build_int_cst (NULL_TREE
, last_conflict
);
2440 *overlaps_a
= integer_zero_node
;
2441 *overlaps_b
= integer_zero_node
;
2442 *last_conflicts
= integer_zero_node
;
2447 /* Solves the special case of a Diophantine equation where CHREC_A is
2448 an affine bivariate function, and CHREC_B is an affine univariate
2449 function. For example,
2451 | {{0, +, 1}_x, +, 1335}_y = {0, +, 1336}_z
2453 has the following overlapping functions:
2455 | x (t, u, v) = {{0, +, 1336}_t, +, 1}_v
2456 | y (t, u, v) = {{0, +, 1336}_u, +, 1}_v
2457 | z (t, u, v) = {{{0, +, 1}_t, +, 1335}_u, +, 1}_v
2459 FORNOW: This is a specialized implementation for a case occurring in
2460 a common benchmark. Implement the general algorithm. */
2463 compute_overlap_steps_for_affine_1_2 (tree chrec_a
, tree chrec_b
,
2464 tree
*overlaps_a
, tree
*overlaps_b
,
2465 tree
*last_conflicts
)
2467 bool xz_p
, yz_p
, xyz_p
;
2468 int step_x
, step_y
, step_z
;
2469 int niter_x
, niter_y
, niter_z
, niter
;
2470 tree numiter_x
, numiter_y
, numiter_z
;
2471 tree overlaps_a_xz
, overlaps_b_xz
, last_conflicts_xz
;
2472 tree overlaps_a_yz
, overlaps_b_yz
, last_conflicts_yz
;
2473 tree overlaps_a_xyz
, overlaps_b_xyz
, last_conflicts_xyz
;
2475 step_x
= int_cst_value (CHREC_RIGHT (CHREC_LEFT (chrec_a
)));
2476 step_y
= int_cst_value (CHREC_RIGHT (chrec_a
));
2477 step_z
= int_cst_value (CHREC_RIGHT (chrec_b
));
2479 numiter_x
= get_number_of_iters_for_loop (CHREC_VARIABLE (CHREC_LEFT (chrec_a
)));
2480 numiter_y
= get_number_of_iters_for_loop (CHREC_VARIABLE (chrec_a
));
2481 numiter_z
= get_number_of_iters_for_loop (CHREC_VARIABLE (chrec_b
));
2483 if (numiter_x
== NULL_TREE
|| numiter_y
== NULL_TREE
2484 || numiter_z
== NULL_TREE
)
2486 *overlaps_a
= chrec_dont_know
;
2487 *overlaps_b
= chrec_dont_know
;
2488 *last_conflicts
= chrec_dont_know
;
2492 niter_x
= int_cst_value (numiter_x
);
2493 niter_y
= int_cst_value (numiter_y
);
2494 niter_z
= int_cst_value (numiter_z
);
2496 niter
= MIN (niter_x
, niter_z
);
2497 compute_overlap_steps_for_affine_univar (niter
, step_x
, step_z
,
2500 &last_conflicts_xz
, 1);
2501 niter
= MIN (niter_y
, niter_z
);
2502 compute_overlap_steps_for_affine_univar (niter
, step_y
, step_z
,
2505 &last_conflicts_yz
, 2);
2506 niter
= MIN (niter_x
, niter_z
);
2507 niter
= MIN (niter_y
, niter
);
2508 compute_overlap_steps_for_affine_univar (niter
, step_x
+ step_y
, step_z
,
2511 &last_conflicts_xyz
, 3);
2513 xz_p
= !integer_zerop (last_conflicts_xz
);
2514 yz_p
= !integer_zerop (last_conflicts_yz
);
2515 xyz_p
= !integer_zerop (last_conflicts_xyz
);
2517 if (xz_p
|| yz_p
|| xyz_p
)
2519 *overlaps_a
= make_tree_vec (2);
2520 TREE_VEC_ELT (*overlaps_a
, 0) = integer_zero_node
;
2521 TREE_VEC_ELT (*overlaps_a
, 1) = integer_zero_node
;
2522 *overlaps_b
= integer_zero_node
;
2525 TREE_VEC_ELT (*overlaps_a
, 0) =
2526 chrec_fold_plus (integer_type_node
, TREE_VEC_ELT (*overlaps_a
, 0),
2529 chrec_fold_plus (integer_type_node
, *overlaps_b
, overlaps_b_xz
);
2530 *last_conflicts
= last_conflicts_xz
;
2534 TREE_VEC_ELT (*overlaps_a
, 1) =
2535 chrec_fold_plus (integer_type_node
, TREE_VEC_ELT (*overlaps_a
, 1),
2538 chrec_fold_plus (integer_type_node
, *overlaps_b
, overlaps_b_yz
);
2539 *last_conflicts
= last_conflicts_yz
;
2543 TREE_VEC_ELT (*overlaps_a
, 0) =
2544 chrec_fold_plus (integer_type_node
, TREE_VEC_ELT (*overlaps_a
, 0),
2546 TREE_VEC_ELT (*overlaps_a
, 1) =
2547 chrec_fold_plus (integer_type_node
, TREE_VEC_ELT (*overlaps_a
, 1),
2550 chrec_fold_plus (integer_type_node
, *overlaps_b
, overlaps_b_xyz
);
2551 *last_conflicts
= last_conflicts_xyz
;
2556 *overlaps_a
= integer_zero_node
;
2557 *overlaps_b
= integer_zero_node
;
2558 *last_conflicts
= integer_zero_node
;
2562 /* Determines the overlapping elements due to accesses CHREC_A and
2563 CHREC_B, that are affine functions. This is a part of the
2564 subscript analyzer. */
2567 analyze_subscript_affine_affine (tree chrec_a
,
2571 tree
*last_conflicts
)
2573 unsigned nb_vars_a
, nb_vars_b
, dim
;
2574 int init_a
, init_b
, gamma
, gcd_alpha_beta
;
2576 lambda_matrix A
, U
, S
;
2577 tree difference
= chrec_fold_minus (integer_type_node
, chrec_a
, chrec_b
);
2579 if (integer_zerop (difference
))
2581 /* The difference is equal to zero: the accessed index
2582 overlaps for each iteration in the loop. */
2583 *overlaps_a
= integer_zero_node
;
2584 *overlaps_b
= integer_zero_node
;
2585 *last_conflicts
= chrec_dont_know
;
2588 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2589 fprintf (dump_file
, "(analyze_subscript_affine_affine \n");
2591 /* For determining the initial intersection, we have to solve a
2592 Diophantine equation. This is the most time consuming part.
2594 For answering to the question: "Is there a dependence?" we have
2595 to prove that there exists a solution to the Diophantine
2596 equation, and that the solution is in the iteration domain,
2597 i.e. the solution is positive or zero, and that the solution
2598 happens before the upper bound loop.nb_iterations. Otherwise
2599 there is no dependence. This function outputs a description of
2600 the iterations that hold the intersections. */
2603 nb_vars_a
= nb_vars_in_chrec (chrec_a
);
2604 nb_vars_b
= nb_vars_in_chrec (chrec_b
);
2606 dim
= nb_vars_a
+ nb_vars_b
;
2607 U
= lambda_matrix_new (dim
, dim
);
2608 A
= lambda_matrix_new (dim
, 1);
2609 S
= lambda_matrix_new (dim
, 1);
2611 init_a
= initialize_matrix_A (A
, chrec_a
, 0, 1);
2612 init_b
= initialize_matrix_A (A
, chrec_b
, nb_vars_a
, -1);
2613 gamma
= init_b
- init_a
;
2615 /* Don't do all the hard work of solving the Diophantine equation
2616 when we already know the solution: for example,
2619 | gamma = 3 - 3 = 0.
2620 Then the first overlap occurs during the first iterations:
2621 | {3, +, 1}_1 ({0, +, 4}_x) = {3, +, 4}_2 ({0, +, 1}_x)
2625 if (nb_vars_a
== 1 && nb_vars_b
== 1)
2628 int niter
, niter_a
, niter_b
;
2629 tree numiter_a
, numiter_b
;
2631 numiter_a
= get_number_of_iters_for_loop (CHREC_VARIABLE (chrec_a
));
2632 numiter_b
= get_number_of_iters_for_loop (CHREC_VARIABLE (chrec_b
));
2633 if (numiter_a
== NULL_TREE
|| numiter_b
== NULL_TREE
)
2635 *overlaps_a
= chrec_dont_know
;
2636 *overlaps_b
= chrec_dont_know
;
2637 *last_conflicts
= chrec_dont_know
;
2641 niter_a
= int_cst_value (numiter_a
);
2642 niter_b
= int_cst_value (numiter_b
);
2643 niter
= MIN (niter_a
, niter_b
);
2645 step_a
= int_cst_value (CHREC_RIGHT (chrec_a
));
2646 step_b
= int_cst_value (CHREC_RIGHT (chrec_b
));
2648 compute_overlap_steps_for_affine_univar (niter
, step_a
, step_b
,
2649 overlaps_a
, overlaps_b
,
2653 else if (nb_vars_a
== 2 && nb_vars_b
== 1)
2654 compute_overlap_steps_for_affine_1_2
2655 (chrec_a
, chrec_b
, overlaps_a
, overlaps_b
, last_conflicts
);
2657 else if (nb_vars_a
== 1 && nb_vars_b
== 2)
2658 compute_overlap_steps_for_affine_1_2
2659 (chrec_b
, chrec_a
, overlaps_b
, overlaps_a
, last_conflicts
);
2663 *overlaps_a
= chrec_dont_know
;
2664 *overlaps_b
= chrec_dont_know
;
2665 *last_conflicts
= chrec_dont_know
;
2671 lambda_matrix_right_hermite (A
, dim
, 1, S
, U
);
2676 lambda_matrix_row_negate (U
, dim
, 0);
2678 gcd_alpha_beta
= S
[0][0];
2680 /* The classic "gcd-test". */
2681 if (!int_divides_p (gcd_alpha_beta
, gamma
))
2683 /* The "gcd-test" has determined that there is no integer
2684 solution, i.e. there is no dependence. */
2685 *overlaps_a
= chrec_known
;
2686 *overlaps_b
= chrec_known
;
2687 *last_conflicts
= integer_zero_node
;
2690 /* Both access functions are univariate. This includes SIV and MIV cases. */
2691 else if (nb_vars_a
== 1 && nb_vars_b
== 1)
2693 /* Both functions should have the same evolution sign. */
2694 if (((A
[0][0] > 0 && -A
[1][0] > 0)
2695 || (A
[0][0] < 0 && -A
[1][0] < 0)))
2697 /* The solutions are given by:
2699 | [GAMMA/GCD_ALPHA_BETA t].[u11 u12] = [x0]
2702 For a given integer t. Using the following variables,
2704 | i0 = u11 * gamma / gcd_alpha_beta
2705 | j0 = u12 * gamma / gcd_alpha_beta
2712 | y0 = j0 + j1 * t. */
2716 /* X0 and Y0 are the first iterations for which there is a
2717 dependence. X0, Y0 are two solutions of the Diophantine
2718 equation: chrec_a (X0) = chrec_b (Y0). */
2720 int niter
, niter_a
, niter_b
;
2721 tree numiter_a
, numiter_b
;
2723 numiter_a
= get_number_of_iters_for_loop (CHREC_VARIABLE (chrec_a
));
2724 numiter_b
= get_number_of_iters_for_loop (CHREC_VARIABLE (chrec_b
));
2726 if (numiter_a
== NULL_TREE
|| numiter_b
== NULL_TREE
)
2728 *overlaps_a
= chrec_dont_know
;
2729 *overlaps_b
= chrec_dont_know
;
2730 *last_conflicts
= chrec_dont_know
;
2734 niter_a
= int_cst_value (numiter_a
);
2735 niter_b
= int_cst_value (numiter_b
);
2736 niter
= MIN (niter_a
, niter_b
);
2738 i0
= U
[0][0] * gamma
/ gcd_alpha_beta
;
2739 j0
= U
[0][1] * gamma
/ gcd_alpha_beta
;
2743 if ((i1
== 0 && i0
< 0)
2744 || (j1
== 0 && j0
< 0))
2746 /* There is no solution.
2747 FIXME: The case "i0 > nb_iterations, j0 > nb_iterations"
2748 falls in here, but for the moment we don't look at the
2749 upper bound of the iteration domain. */
2750 *overlaps_a
= chrec_known
;
2751 *overlaps_b
= chrec_known
;
2752 *last_conflicts
= integer_zero_node
;
2759 tau1
= CEIL (-i0
, i1
);
2760 tau2
= FLOOR_DIV (niter
- i0
, i1
);
2764 int last_conflict
, min_multiple
;
2765 tau1
= MAX (tau1
, CEIL (-j0
, j1
));
2766 tau2
= MIN (tau2
, FLOOR_DIV (niter
- j0
, j1
));
2768 x0
= i1
* tau1
+ i0
;
2769 y0
= j1
* tau1
+ j0
;
2771 /* At this point (x0, y0) is one of the
2772 solutions to the Diophantine equation. The
2773 next step has to compute the smallest
2774 positive solution: the first conflicts. */
2775 min_multiple
= MIN (x0
/ i1
, y0
/ j1
);
2776 x0
-= i1
* min_multiple
;
2777 y0
-= j1
* min_multiple
;
2779 tau1
= (x0
- i0
)/i1
;
2780 last_conflict
= tau2
- tau1
;
2782 /* If the overlap occurs outside of the bounds of the
2783 loop, there is no dependence. */
2784 if (x0
> niter
|| y0
> niter
)
2787 *overlaps_a
= chrec_known
;
2788 *overlaps_b
= chrec_known
;
2789 *last_conflicts
= integer_zero_node
;
2793 *overlaps_a
= build_polynomial_chrec
2795 build_int_cst (NULL_TREE
, x0
),
2796 build_int_cst (NULL_TREE
, i1
));
2797 *overlaps_b
= build_polynomial_chrec
2799 build_int_cst (NULL_TREE
, y0
),
2800 build_int_cst (NULL_TREE
, j1
));
2801 *last_conflicts
= build_int_cst (NULL_TREE
, last_conflict
);
2806 /* FIXME: For the moment, the upper bound of the
2807 iteration domain for j is not checked. */
2808 *overlaps_a
= chrec_dont_know
;
2809 *overlaps_b
= chrec_dont_know
;
2810 *last_conflicts
= chrec_dont_know
;
2816 /* FIXME: For the moment, the upper bound of the
2817 iteration domain for i is not checked. */
2818 *overlaps_a
= chrec_dont_know
;
2819 *overlaps_b
= chrec_dont_know
;
2820 *last_conflicts
= chrec_dont_know
;
2826 *overlaps_a
= chrec_dont_know
;
2827 *overlaps_b
= chrec_dont_know
;
2828 *last_conflicts
= chrec_dont_know
;
2834 *overlaps_a
= chrec_dont_know
;
2835 *overlaps_b
= chrec_dont_know
;
2836 *last_conflicts
= chrec_dont_know
;
2840 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2842 fprintf (dump_file
, " (overlaps_a = ");
2843 print_generic_expr (dump_file
, *overlaps_a
, 0);
2844 fprintf (dump_file
, ")\n (overlaps_b = ");
2845 print_generic_expr (dump_file
, *overlaps_b
, 0);
2846 fprintf (dump_file
, ")\n");
2849 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2850 fprintf (dump_file
, ")\n");
2853 /* Analyze a SIV (Single Index Variable) subscript. *OVERLAPS_A and
2854 *OVERLAPS_B are initialized to the functions that describe the
2855 relation between the elements accessed twice by CHREC_A and
2856 CHREC_B. For k >= 0, the following property is verified:
2858 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
2861 analyze_siv_subscript (tree chrec_a
,
2865 tree
*last_conflicts
)
2867 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2868 fprintf (dump_file
, "(analyze_siv_subscript \n");
2870 if (evolution_function_is_constant_p (chrec_a
)
2871 && evolution_function_is_affine_p (chrec_b
))
2872 analyze_siv_subscript_cst_affine (chrec_a
, chrec_b
,
2873 overlaps_a
, overlaps_b
, last_conflicts
);
2875 else if (evolution_function_is_affine_p (chrec_a
)
2876 && evolution_function_is_constant_p (chrec_b
))
2877 analyze_siv_subscript_cst_affine (chrec_b
, chrec_a
,
2878 overlaps_b
, overlaps_a
, last_conflicts
);
2880 else if (evolution_function_is_affine_p (chrec_a
)
2881 && evolution_function_is_affine_p (chrec_b
))
2882 analyze_subscript_affine_affine (chrec_a
, chrec_b
,
2883 overlaps_a
, overlaps_b
, last_conflicts
);
2886 *overlaps_a
= chrec_dont_know
;
2887 *overlaps_b
= chrec_dont_know
;
2888 *last_conflicts
= chrec_dont_know
;
2891 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2892 fprintf (dump_file
, ")\n");
2895 /* Return true when the evolution steps of an affine CHREC divide the
2899 chrec_steps_divide_constant_p (tree chrec
,
2902 switch (TREE_CODE (chrec
))
2904 case POLYNOMIAL_CHREC
:
2905 return (tree_fold_divides_p (CHREC_RIGHT (chrec
), cst
)
2906 && chrec_steps_divide_constant_p (CHREC_LEFT (chrec
), cst
));
2909 /* On the initial condition, return true. */
2914 /* Analyze a MIV (Multiple Index Variable) subscript. *OVERLAPS_A and
2915 *OVERLAPS_B are initialized to the functions that describe the
2916 relation between the elements accessed twice by CHREC_A and
2917 CHREC_B. For k >= 0, the following property is verified:
2919 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
2922 analyze_miv_subscript (tree chrec_a
,
2926 tree
*last_conflicts
)
2928 /* FIXME: This is a MIV subscript, not yet handled.
2929 Example: (A[{1, +, 1}_1] vs. A[{1, +, 1}_2]) that comes from
2932 In the SIV test we had to solve a Diophantine equation with two
2933 variables. In the MIV case we have to solve a Diophantine
2934 equation with 2*n variables (if the subscript uses n IVs).
2938 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2939 fprintf (dump_file
, "(analyze_miv_subscript \n");
2941 difference
= chrec_fold_minus (integer_type_node
, chrec_a
, chrec_b
);
2943 if (chrec_zerop (difference
))
2945 /* Access functions are the same: all the elements are accessed
2946 in the same order. */
2947 *overlaps_a
= integer_zero_node
;
2948 *overlaps_b
= integer_zero_node
;
2949 *last_conflicts
= get_number_of_iters_for_loop (CHREC_VARIABLE (chrec_a
));
2953 else if (evolution_function_is_constant_p (difference
)
2954 /* For the moment, the following is verified:
2955 evolution_function_is_affine_multivariate_p (chrec_a) */
2956 && !chrec_steps_divide_constant_p (chrec_a
, difference
))
2958 /* testsuite/.../ssa-chrec-33.c
2959 {{21, +, 2}_1, +, -2}_2 vs. {{20, +, 2}_1, +, -2}_2
2961 The difference is 1, and the evolution steps are equal to 2,
2962 consequently there are no overlapping elements. */
2963 *overlaps_a
= chrec_known
;
2964 *overlaps_b
= chrec_known
;
2965 *last_conflicts
= integer_zero_node
;
2968 else if (evolution_function_is_affine_multivariate_p (chrec_a
)
2969 && evolution_function_is_affine_multivariate_p (chrec_b
))
2971 /* testsuite/.../ssa-chrec-35.c
2972 {0, +, 1}_2 vs. {0, +, 1}_3
2973 the overlapping elements are respectively located at iterations:
2974 {0, +, 1}_x and {0, +, 1}_x,
2975 in other words, we have the equality:
2976 {0, +, 1}_2 ({0, +, 1}_x) = {0, +, 1}_3 ({0, +, 1}_x)
2979 {{0, +, 1}_1, +, 2}_2 ({0, +, 1}_x, {0, +, 1}_y) =
2980 {0, +, 1}_1 ({{0, +, 1}_x, +, 2}_y)
2982 {{0, +, 2}_1, +, 3}_2 ({0, +, 1}_y, {0, +, 1}_x) =
2983 {{0, +, 3}_1, +, 2}_2 ({0, +, 1}_x, {0, +, 1}_y)
2985 analyze_subscript_affine_affine (chrec_a
, chrec_b
,
2986 overlaps_a
, overlaps_b
, last_conflicts
);
2991 /* When the analysis is too difficult, answer "don't know". */
2992 *overlaps_a
= chrec_dont_know
;
2993 *overlaps_b
= chrec_dont_know
;
2994 *last_conflicts
= chrec_dont_know
;
2997 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2998 fprintf (dump_file
, ")\n");
3001 /* Determines the iterations for which CHREC_A is equal to CHREC_B.
3002 OVERLAP_ITERATIONS_A and OVERLAP_ITERATIONS_B are initialized with
3003 two functions that describe the iterations that contain conflicting
3006 Remark: For an integer k >= 0, the following equality is true:
3008 CHREC_A (OVERLAP_ITERATIONS_A (k)) == CHREC_B (OVERLAP_ITERATIONS_B (k)).
3012 analyze_overlapping_iterations (tree chrec_a
,
3014 tree
*overlap_iterations_a
,
3015 tree
*overlap_iterations_b
,
3016 tree
*last_conflicts
)
3018 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3020 fprintf (dump_file
, "(analyze_overlapping_iterations \n");
3021 fprintf (dump_file
, " (chrec_a = ");
3022 print_generic_expr (dump_file
, chrec_a
, 0);
3023 fprintf (dump_file
, ")\n chrec_b = ");
3024 print_generic_expr (dump_file
, chrec_b
, 0);
3025 fprintf (dump_file
, ")\n");
3028 if (chrec_a
== NULL_TREE
3029 || chrec_b
== NULL_TREE
3030 || chrec_contains_undetermined (chrec_a
)
3031 || chrec_contains_undetermined (chrec_b
)
3032 || chrec_contains_symbols (chrec_a
)
3033 || chrec_contains_symbols (chrec_b
))
3035 *overlap_iterations_a
= chrec_dont_know
;
3036 *overlap_iterations_b
= chrec_dont_know
;
3039 else if (ziv_subscript_p (chrec_a
, chrec_b
))
3040 analyze_ziv_subscript (chrec_a
, chrec_b
,
3041 overlap_iterations_a
, overlap_iterations_b
,
3044 else if (siv_subscript_p (chrec_a
, chrec_b
))
3045 analyze_siv_subscript (chrec_a
, chrec_b
,
3046 overlap_iterations_a
, overlap_iterations_b
,
3050 analyze_miv_subscript (chrec_a
, chrec_b
,
3051 overlap_iterations_a
, overlap_iterations_b
,
3054 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3056 fprintf (dump_file
, " (overlap_iterations_a = ");
3057 print_generic_expr (dump_file
, *overlap_iterations_a
, 0);
3058 fprintf (dump_file
, ")\n (overlap_iterations_b = ");
3059 print_generic_expr (dump_file
, *overlap_iterations_b
, 0);
3060 fprintf (dump_file
, ")\n");
3066 /* This section contains the affine functions dependences detector. */
3068 /* Computes the conflicting iterations, and initialize DDR. */
3071 subscript_dependence_tester (struct data_dependence_relation
*ddr
)
3074 struct data_reference
*dra
= DDR_A (ddr
);
3075 struct data_reference
*drb
= DDR_B (ddr
);
3076 tree last_conflicts
;
3078 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3079 fprintf (dump_file
, "(subscript_dependence_tester \n");
3081 for (i
= 0; i
< DDR_NUM_SUBSCRIPTS (ddr
); i
++)
3083 tree overlaps_a
, overlaps_b
;
3084 struct subscript
*subscript
= DDR_SUBSCRIPT (ddr
, i
);
3086 analyze_overlapping_iterations (DR_ACCESS_FN (dra
, i
),
3087 DR_ACCESS_FN (drb
, i
),
3088 &overlaps_a
, &overlaps_b
,
3091 if (chrec_contains_undetermined (overlaps_a
)
3092 || chrec_contains_undetermined (overlaps_b
))
3094 finalize_ddr_dependent (ddr
, chrec_dont_know
);
3098 else if (overlaps_a
== chrec_known
3099 || overlaps_b
== chrec_known
)
3101 finalize_ddr_dependent (ddr
, chrec_known
);
3107 SUB_CONFLICTS_IN_A (subscript
) = overlaps_a
;
3108 SUB_CONFLICTS_IN_B (subscript
) = overlaps_b
;
3109 SUB_LAST_CONFLICT (subscript
) = last_conflicts
;
3113 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3114 fprintf (dump_file
, ")\n");
3117 /* Compute the classic per loop distance vector.
3119 DDR is the data dependence relation to build a vector from.
3120 NB_LOOPS is the total number of loops we are considering.
3121 FIRST_LOOP_DEPTH is the loop->depth of the first loop in the analyzed
3123 Return FALSE when fail to represent the data dependence as a distance
3125 Return TRUE otherwise. */
3128 build_classic_dist_vector (struct data_dependence_relation
*ddr
,
3129 int nb_loops
, int first_loop_depth
)
3132 lambda_vector dist_v
, init_v
;
3133 bool init_b
= false;
3135 DDR_SIZE_VECT (ddr
) = nb_loops
;
3136 dist_v
= lambda_vector_new (nb_loops
);
3137 init_v
= lambda_vector_new (nb_loops
);
3138 lambda_vector_clear (dist_v
, nb_loops
);
3139 lambda_vector_clear (init_v
, nb_loops
);
3141 if (DDR_ARE_DEPENDENT (ddr
) != NULL_TREE
)
3144 for (i
= 0; i
< DDR_NUM_SUBSCRIPTS (ddr
); i
++)
3146 tree access_fn_a
, access_fn_b
;
3147 struct subscript
*subscript
= DDR_SUBSCRIPT (ddr
, i
);
3149 if (chrec_contains_undetermined (SUB_DISTANCE (subscript
)))
3151 non_affine_dependence_relation (ddr
);
3155 access_fn_a
= DR_ACCESS_FN (DDR_A (ddr
), i
);
3156 access_fn_b
= DR_ACCESS_FN (DDR_B (ddr
), i
);
3158 if (TREE_CODE (access_fn_a
) == POLYNOMIAL_CHREC
3159 && TREE_CODE (access_fn_b
) == POLYNOMIAL_CHREC
)
3161 int dist
, loop_nb
, loop_depth
;
3162 int loop_nb_a
= CHREC_VARIABLE (access_fn_a
);
3163 int loop_nb_b
= CHREC_VARIABLE (access_fn_b
);
3164 struct loop
*loop_a
= current_loops
->parray
[loop_nb_a
];
3165 struct loop
*loop_b
= current_loops
->parray
[loop_nb_b
];
3167 /* If the loop for either variable is at a lower depth than
3168 the first_loop's depth, then we can't possibly have a
3169 dependency at this level of the loop. */
3171 if (loop_a
->depth
< first_loop_depth
3172 || loop_b
->depth
< first_loop_depth
)
3175 if (loop_nb_a
!= loop_nb_b
3176 && !flow_loop_nested_p (loop_a
, loop_b
)
3177 && !flow_loop_nested_p (loop_b
, loop_a
))
3179 /* Example: when there are two consecutive loops,
3188 the dependence relation cannot be captured by the
3189 distance abstraction. */
3190 non_affine_dependence_relation (ddr
);
3194 /* The dependence is carried by the outermost loop. Example:
3201 In this case, the dependence is carried by loop_1. */
3202 loop_nb
= loop_nb_a
< loop_nb_b
? loop_nb_a
: loop_nb_b
;
3203 loop_depth
= current_loops
->parray
[loop_nb
]->depth
- first_loop_depth
;
3205 /* If the loop number is still greater than the number of
3206 loops we've been asked to analyze, or negative,
3207 something is borked. */
3208 gcc_assert (loop_depth
>= 0);
3209 gcc_assert (loop_depth
< nb_loops
);
3210 if (chrec_contains_undetermined (SUB_DISTANCE (subscript
)))
3212 non_affine_dependence_relation (ddr
);
3216 dist
= int_cst_value (SUB_DISTANCE (subscript
));
3218 /* This is the subscript coupling test.
3223 There is no dependence. */
3224 if (init_v
[loop_depth
] != 0
3225 && dist_v
[loop_depth
] != dist
)
3227 finalize_ddr_dependent (ddr
, chrec_known
);
3231 dist_v
[loop_depth
] = dist
;
3232 init_v
[loop_depth
] = 1;
3237 /* Save the distance vector if we initialized one. */
3240 lambda_vector save_v
;
3242 /* Verify a basic constraint: classic distance vectors should always
3243 be lexicographically positive. */
3244 if (!lambda_vector_lexico_pos (dist_v
, DDR_SIZE_VECT (ddr
)))
3246 if (DDR_SIZE_VECT (ddr
) == 1)
3247 /* This one is simple to fix, and can be fixed.
3248 Multidimensional arrays cannot be fixed that simply. */
3249 lambda_vector_negate (dist_v
, dist_v
, DDR_SIZE_VECT (ddr
));
3251 /* This is not valid: we need the delta test for properly
3256 save_v
= lambda_vector_new (DDR_SIZE_VECT (ddr
));
3257 lambda_vector_copy (dist_v
, save_v
, DDR_SIZE_VECT (ddr
));
3258 VEC_safe_push (lambda_vector
, heap
, DDR_DIST_VECTS (ddr
), save_v
);
3260 /* There is nothing more to do when there are no outer loops. */
3261 if (DDR_SIZE_VECT (ddr
) == 1)
3262 goto classic_dist_done
;
3265 /* There is a distance of 1 on all the outer loops:
3267 Example: there is a dependence of distance 1 on loop_1 for the array A.
3273 struct loop
*lca
, *loop_a
, *loop_b
;
3274 struct data_reference
*a
= DDR_A (ddr
);
3275 struct data_reference
*b
= DDR_B (ddr
);
3277 loop_a
= loop_containing_stmt (DR_STMT (a
));
3278 loop_b
= loop_containing_stmt (DR_STMT (b
));
3280 /* Get the common ancestor loop. */
3281 lca
= find_common_loop (loop_a
, loop_b
);
3282 lca_depth
= lca
->depth
- first_loop_depth
;
3284 gcc_assert (lca_depth
>= 0);
3285 gcc_assert (lca_depth
< nb_loops
);
3287 /* For each outer loop where init_v is not set, the accesses are
3288 in dependence of distance 1 in the loop. */
3289 while (lca
->depth
!= 0)
3291 /* If we're considering just a sub-nest, then don't record
3292 any information on the outer loops. */
3296 gcc_assert (lca_depth
< nb_loops
);
3298 /* If we haven't yet determined a distance for this outer
3299 loop, push a new distance vector composed of the previous
3300 distance, and a distance of 1 for this outer loop.
3309 Saved vectors are of the form (dist_in_1, dist_in_2).
3310 First, we save (0, 1), then we have to save (1, 0). */
3311 if (init_v
[lca_depth
] == 0)
3313 lambda_vector save_v
= lambda_vector_new (DDR_SIZE_VECT (ddr
));
3315 lambda_vector_copy (dist_v
, save_v
, DDR_SIZE_VECT (ddr
));
3316 save_v
[lca_depth
] = 1;
3317 VEC_safe_push (lambda_vector
, heap
, DDR_DIST_VECTS (ddr
), save_v
);
3321 lca_depth
= lca
->depth
- first_loop_depth
;
3327 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3329 fprintf (dump_file
, "(build_classic_dist_vector\n");
3331 for (i
= 0; i
< DDR_NUM_DIST_VECTS (ddr
); i
++)
3333 fprintf (dump_file
, " dist_vector = (");
3334 print_lambda_vector (dump_file
, DDR_DIST_VECT (ddr
, i
),
3335 DDR_SIZE_VECT (ddr
));
3336 fprintf (dump_file
, " )\n");
3338 fprintf (dump_file
, ")\n");
3344 /* Compute the classic per loop direction vector.
3346 DDR is the data dependence relation to build a vector from.
3347 NB_LOOPS is the total number of loops we are considering.
3348 FIRST_LOOP_DEPTH is the loop->depth of the first loop in the analyzed
3350 Return FALSE if the dependence relation is outside of the loop nest
3351 at FIRST_LOOP_DEPTH.
3352 Return TRUE otherwise. */
3355 build_classic_dir_vector (struct data_dependence_relation
*ddr
,
3356 int nb_loops
, int first_loop_depth
)
3359 lambda_vector dir_v
, init_v
;
3360 bool init_b
= false;
3362 dir_v
= lambda_vector_new (nb_loops
);
3363 init_v
= lambda_vector_new (nb_loops
);
3364 lambda_vector_clear (dir_v
, nb_loops
);
3365 lambda_vector_clear (init_v
, nb_loops
);
3367 DDR_SIZE_VECT (ddr
) = nb_loops
;
3369 if (DDR_ARE_DEPENDENT (ddr
) != NULL_TREE
)
3372 for (i
= 0; i
< DDR_NUM_SUBSCRIPTS (ddr
); i
++)
3374 tree access_fn_a
, access_fn_b
;
3375 struct subscript
*subscript
= DDR_SUBSCRIPT (ddr
, i
);
3377 if (chrec_contains_undetermined (SUB_DISTANCE (subscript
)))
3379 non_affine_dependence_relation (ddr
);
3383 access_fn_a
= DR_ACCESS_FN (DDR_A (ddr
), i
);
3384 access_fn_b
= DR_ACCESS_FN (DDR_B (ddr
), i
);
3385 if (TREE_CODE (access_fn_a
) == POLYNOMIAL_CHREC
3386 && TREE_CODE (access_fn_b
) == POLYNOMIAL_CHREC
)
3388 int dist
, loop_nb
, loop_depth
;
3389 enum data_dependence_direction dir
= dir_star
;
3390 int loop_nb_a
= CHREC_VARIABLE (access_fn_a
);
3391 int loop_nb_b
= CHREC_VARIABLE (access_fn_b
);
3392 struct loop
*loop_a
= current_loops
->parray
[loop_nb_a
];
3393 struct loop
*loop_b
= current_loops
->parray
[loop_nb_b
];
3395 /* If the loop for either variable is at a lower depth than
3396 the first_loop's depth, then we can't possibly have a
3397 dependency at this level of the loop. */
3399 if (loop_a
->depth
< first_loop_depth
3400 || loop_b
->depth
< first_loop_depth
)
3403 if (loop_nb_a
!= loop_nb_b
3404 && !flow_loop_nested_p (loop_a
, loop_b
)
3405 && !flow_loop_nested_p (loop_b
, loop_a
))
3407 /* Example: when there are two consecutive loops,
3416 the dependence relation cannot be captured by the
3417 distance abstraction. */
3418 non_affine_dependence_relation (ddr
);
3422 /* The dependence is carried by the outermost loop. Example:
3429 In this case, the dependence is carried by loop_1. */
3430 loop_nb
= loop_nb_a
< loop_nb_b
? loop_nb_a
: loop_nb_b
;
3431 loop_depth
= current_loops
->parray
[loop_nb
]->depth
- first_loop_depth
;
3433 /* If the loop number is still greater than the number of
3434 loops we've been asked to analyze, or negative,
3435 something is borked. */
3436 gcc_assert (loop_depth
>= 0);
3437 gcc_assert (loop_depth
< nb_loops
);
3439 if (chrec_contains_undetermined (SUB_DISTANCE (subscript
)))
3441 non_affine_dependence_relation (ddr
);
3445 dist
= int_cst_value (SUB_DISTANCE (subscript
));
3454 /* This is the subscript coupling test.
3459 There is no dependence. */
3460 if (init_v
[loop_depth
] != 0
3462 && (enum data_dependence_direction
) dir_v
[loop_depth
] != dir
3463 && (enum data_dependence_direction
) dir_v
[loop_depth
] != dir_star
)
3465 finalize_ddr_dependent (ddr
, chrec_known
);
3469 dir_v
[loop_depth
] = dir
;
3470 init_v
[loop_depth
] = 1;
3475 /* Save the direction vector if we initialized one. */
3478 lambda_vector save_v
= lambda_vector_new (DDR_SIZE_VECT (ddr
));
3480 lambda_vector_copy (dir_v
, save_v
, DDR_SIZE_VECT (ddr
));
3481 VEC_safe_push (lambda_vector
, heap
, DDR_DIR_VECTS (ddr
), save_v
);
3484 /* There is a distance of 1 on all the outer loops:
3486 Example: there is a dependence of distance 1 on loop_1 for the array A.
3492 struct loop
*lca
, *loop_a
, *loop_b
;
3493 struct data_reference
*a
= DDR_A (ddr
);
3494 struct data_reference
*b
= DDR_B (ddr
);
3496 loop_a
= loop_containing_stmt (DR_STMT (a
));
3497 loop_b
= loop_containing_stmt (DR_STMT (b
));
3499 /* Get the common ancestor loop. */
3500 lca
= find_common_loop (loop_a
, loop_b
);
3501 lca_depth
= lca
->depth
- first_loop_depth
;
3503 gcc_assert (lca_depth
>= 0);
3504 gcc_assert (lca_depth
< nb_loops
);
3506 while (lca
->depth
!= 0)
3508 /* If we're considering just a sub-nest, then don't record
3509 any information on the outer loops. */
3513 gcc_assert (lca_depth
< nb_loops
);
3515 if (init_v
[lca_depth
] == 0)
3517 lambda_vector save_v
= lambda_vector_new (DDR_SIZE_VECT (ddr
));
3519 lambda_vector_copy (dir_v
, save_v
, DDR_SIZE_VECT (ddr
));
3520 save_v
[lca_depth
] = dir_positive
;
3521 VEC_safe_push (lambda_vector
, heap
, DDR_DIR_VECTS (ddr
), save_v
);
3525 lca_depth
= lca
->depth
- first_loop_depth
;
3533 /* Returns true when all the access functions of A are affine or
3537 access_functions_are_affine_or_constant_p (struct data_reference
*a
)
3540 VEC(tree
,heap
) **fns
= DR_ACCESS_FNS_ADDR (a
);
3543 for (i
= 0; VEC_iterate (tree
, *fns
, i
, t
); i
++)
3544 if (!evolution_function_is_constant_p (t
)
3545 && !evolution_function_is_affine_multivariate_p (t
))
3551 /* This computes the affine dependence relation between A and B.
3552 CHREC_KNOWN is used for representing the independence between two
3553 accesses, while CHREC_DONT_KNOW is used for representing the unknown
3556 Note that it is possible to stop the computation of the dependence
3557 relation the first time we detect a CHREC_KNOWN element for a given
3561 compute_affine_dependence (struct data_dependence_relation
*ddr
)
3563 struct data_reference
*dra
= DDR_A (ddr
);
3564 struct data_reference
*drb
= DDR_B (ddr
);
3566 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3568 fprintf (dump_file
, "(compute_affine_dependence\n");
3569 fprintf (dump_file
, " (stmt_a = \n");
3570 print_generic_expr (dump_file
, DR_STMT (dra
), 0);
3571 fprintf (dump_file
, ")\n (stmt_b = \n");
3572 print_generic_expr (dump_file
, DR_STMT (drb
), 0);
3573 fprintf (dump_file
, ")\n");
3576 /* Analyze only when the dependence relation is not yet known. */
3577 if (DDR_ARE_DEPENDENT (ddr
) == NULL_TREE
)
3579 if (access_functions_are_affine_or_constant_p (dra
)
3580 && access_functions_are_affine_or_constant_p (drb
))
3581 subscript_dependence_tester (ddr
);
3583 /* As a last case, if the dependence cannot be determined, or if
3584 the dependence is considered too difficult to determine, answer
3587 finalize_ddr_dependent (ddr
, chrec_dont_know
);
3590 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3591 fprintf (dump_file
, ")\n");
3594 /* This computes the dependence relation for the same data
3595 reference into DDR. */
3598 compute_self_dependence (struct data_dependence_relation
*ddr
)
3602 for (i
= 0; i
< DDR_NUM_SUBSCRIPTS (ddr
); i
++)
3604 struct subscript
*subscript
= DDR_SUBSCRIPT (ddr
, i
);
3606 /* The accessed index overlaps for each iteration. */
3607 SUB_CONFLICTS_IN_A (subscript
) = integer_zero_node
;
3608 SUB_CONFLICTS_IN_B (subscript
) = integer_zero_node
;
3609 SUB_LAST_CONFLICT (subscript
) = chrec_dont_know
;
3614 typedef struct data_dependence_relation
*ddr_p
;
3616 DEF_VEC_ALLOC_P(ddr_p
,heap
);
3618 /* Compute a subset of the data dependence relation graph. Don't
3619 compute read-read and self relations if
3620 COMPUTE_SELF_AND_READ_READ_DEPENDENCES is FALSE, and avoid the computation
3621 of the opposite relation, i.e. when AB has been computed, don't compute BA.
3622 DATAREFS contains a list of data references, and the result is set
3623 in DEPENDENCE_RELATIONS. */
3626 compute_all_dependences (varray_type datarefs
,
3627 bool compute_self_and_read_read_dependences
,
3628 VEC(ddr_p
,heap
) **dependence_relations
)
3630 unsigned int i
, j
, N
;
3632 N
= VARRAY_ACTIVE_SIZE (datarefs
);
3634 /* Note that we specifically skip i == j because it's a self dependence, and
3635 use compute_self_dependence below. */
3637 for (i
= 0; i
< N
; i
++)
3638 for (j
= i
+ 1; j
< N
; j
++)
3640 struct data_reference
*a
, *b
;
3641 struct data_dependence_relation
*ddr
;
3643 a
= VARRAY_GENERIC_PTR (datarefs
, i
);
3644 b
= VARRAY_GENERIC_PTR (datarefs
, j
);
3645 if (DR_IS_READ (a
) && DR_IS_READ (b
)
3646 && !compute_self_and_read_read_dependences
)
3648 ddr
= initialize_data_dependence_relation (a
, b
);
3650 VEC_safe_push (ddr_p
, heap
, *dependence_relations
, ddr
);
3651 compute_affine_dependence (ddr
);
3652 compute_subscript_distance (ddr
);
3654 if (!compute_self_and_read_read_dependences
)
3657 /* Compute self dependence relation of each dataref to itself. */
3659 for (i
= 0; i
< N
; i
++)
3661 struct data_reference
*a
, *b
;
3662 struct data_dependence_relation
*ddr
;
3664 a
= VARRAY_GENERIC_PTR (datarefs
, i
);
3665 b
= VARRAY_GENERIC_PTR (datarefs
, i
);
3666 ddr
= initialize_data_dependence_relation (a
, b
);
3668 VEC_safe_push (ddr_p
, heap
, *dependence_relations
, ddr
);
3669 compute_self_dependence (ddr
);
3670 compute_subscript_distance (ddr
);
3674 /* Search the data references in LOOP, and record the information into
3675 DATAREFS. Returns chrec_dont_know when failing to analyze a
3676 difficult case, returns NULL_TREE otherwise.
3678 TODO: This function should be made smarter so that it can handle address
3679 arithmetic as if they were array accesses, etc. */
3682 find_data_references_in_loop (struct loop
*loop
, varray_type
*datarefs
)
3684 basic_block bb
, *bbs
;
3686 block_stmt_iterator bsi
;
3687 struct data_reference
*dr
;
3689 bbs
= get_loop_body (loop
);
3691 for (i
= 0; i
< loop
->num_nodes
; i
++)
3695 for (bsi
= bsi_start (bb
); !bsi_end_p (bsi
); bsi_next (&bsi
))
3697 tree stmt
= bsi_stmt (bsi
);
3699 /* ASM_EXPR and CALL_EXPR may embed arbitrary side effects.
3700 Calls have side-effects, except those to const or pure
3702 if ((TREE_CODE (stmt
) == CALL_EXPR
3703 && !(call_expr_flags (stmt
) & (ECF_CONST
| ECF_PURE
)))
3704 || (TREE_CODE (stmt
) == ASM_EXPR
3705 && ASM_VOLATILE_P (stmt
)))
3706 goto insert_dont_know_node
;
3708 if (ZERO_SSA_OPERANDS (stmt
, SSA_OP_ALL_VIRTUALS
))
3711 switch (TREE_CODE (stmt
))
3715 bool one_inserted
= false;
3716 tree opnd0
= TREE_OPERAND (stmt
, 0);
3717 tree opnd1
= TREE_OPERAND (stmt
, 1);
3719 if (TREE_CODE (opnd0
) == ARRAY_REF
3720 || TREE_CODE (opnd0
) == INDIRECT_REF
)
3722 dr
= create_data_ref (opnd0
, stmt
, false);
3725 VARRAY_PUSH_GENERIC_PTR (*datarefs
, dr
);
3726 one_inserted
= true;
3730 if (TREE_CODE (opnd1
) == ARRAY_REF
3731 || TREE_CODE (opnd1
) == INDIRECT_REF
)
3733 dr
= create_data_ref (opnd1
, stmt
, true);
3736 VARRAY_PUSH_GENERIC_PTR (*datarefs
, dr
);
3737 one_inserted
= true;
3742 goto insert_dont_know_node
;
3750 bool one_inserted
= false;
3752 for (args
= TREE_OPERAND (stmt
, 1); args
;
3753 args
= TREE_CHAIN (args
))
3754 if (TREE_CODE (TREE_VALUE (args
)) == ARRAY_REF
3755 || TREE_CODE (TREE_VALUE (args
)) == INDIRECT_REF
)
3757 dr
= create_data_ref (TREE_VALUE (args
), stmt
, true);
3760 VARRAY_PUSH_GENERIC_PTR (*datarefs
, dr
);
3761 one_inserted
= true;
3766 goto insert_dont_know_node
;
3773 struct data_reference
*res
;
3775 insert_dont_know_node
:;
3776 res
= xmalloc (sizeof (struct data_reference
));
3777 DR_STMT (res
) = NULL_TREE
;
3778 DR_REF (res
) = NULL_TREE
;
3779 DR_BASE_OBJECT (res
) = NULL
;
3780 DR_TYPE (res
) = ARRAY_REF_TYPE
;
3781 DR_SET_ACCESS_FNS (res
, NULL
);
3782 DR_BASE_OBJECT (res
) = NULL
;
3783 DR_IS_READ (res
) = false;
3784 DR_BASE_ADDRESS (res
) = NULL_TREE
;
3785 DR_OFFSET (res
) = NULL_TREE
;
3786 DR_INIT (res
) = NULL_TREE
;
3787 DR_STEP (res
) = NULL_TREE
;
3788 DR_OFFSET_MISALIGNMENT (res
) = NULL_TREE
;
3789 DR_MEMTAG (res
) = NULL_TREE
;
3790 DR_PTR_INFO (res
) = NULL
;
3791 VARRAY_PUSH_GENERIC_PTR (*datarefs
, res
);
3794 return chrec_dont_know
;
3798 /* When there are no defs in the loop, the loop is parallel. */
3799 if (!ZERO_SSA_OPERANDS (stmt
, SSA_OP_VIRTUAL_DEFS
))
3800 loop
->parallel_p
= false;
3811 /* This section contains all the entry points. */
3813 /* Given a loop nest LOOP, the following vectors are returned:
3814 *DATAREFS is initialized to all the array elements contained in this loop,
3815 *DEPENDENCE_RELATIONS contains the relations between the data references.
3816 Compute read-read and self relations if
3817 COMPUTE_SELF_AND_READ_READ_DEPENDENCES is TRUE. */
3820 compute_data_dependences_for_loop (struct loop
*loop
,
3821 bool compute_self_and_read_read_dependences
,
3822 varray_type
*datarefs
,
3823 varray_type
*dependence_relations
)
3825 unsigned int i
, nb_loops
;
3826 VEC(ddr_p
,heap
) *allrelations
;
3827 struct data_dependence_relation
*ddr
;
3828 struct loop
*loop_nest
= loop
;
3830 while (loop_nest
&& loop_nest
->outer
&& loop_nest
->outer
->outer
)
3831 loop_nest
= loop_nest
->outer
;
3833 nb_loops
= loop_nest
->level
;
3835 /* If one of the data references is not computable, give up without
3836 spending time to compute other dependences. */
3837 if (find_data_references_in_loop (loop
, datarefs
) == chrec_dont_know
)
3839 struct data_dependence_relation
*ddr
;
3841 /* Insert a single relation into dependence_relations:
3843 ddr
= initialize_data_dependence_relation (NULL
, NULL
);
3844 VARRAY_PUSH_GENERIC_PTR (*dependence_relations
, ddr
);
3845 build_classic_dist_vector (ddr
, nb_loops
, loop
->depth
);
3846 build_classic_dir_vector (ddr
, nb_loops
, loop
->depth
);
3850 allrelations
= NULL
;
3851 compute_all_dependences (*datarefs
, compute_self_and_read_read_dependences
,
3854 for (i
= 0; VEC_iterate (ddr_p
, allrelations
, i
, ddr
); i
++)
3856 if (build_classic_dist_vector (ddr
, nb_loops
, loop_nest
->depth
))
3858 VARRAY_PUSH_GENERIC_PTR (*dependence_relations
, ddr
);
3859 build_classic_dir_vector (ddr
, nb_loops
, loop_nest
->depth
);
3864 /* Entry point (for testing only). Analyze all the data references
3865 and the dependence relations.
3867 The data references are computed first.
3869 A relation on these nodes is represented by a complete graph. Some
3870 of the relations could be of no interest, thus the relations can be
3873 In the following function we compute all the relations. This is
3874 just a first implementation that is here for:
3875 - for showing how to ask for the dependence relations,
3876 - for the debugging the whole dependence graph,
3877 - for the dejagnu testcases and maintenance.
3879 It is possible to ask only for a part of the graph, avoiding to
3880 compute the whole dependence graph. The computed dependences are
3881 stored in a knowledge base (KB) such that later queries don't
3882 recompute the same information. The implementation of this KB is
3883 transparent to the optimizer, and thus the KB can be changed with a
3884 more efficient implementation, or the KB could be disabled. */
3887 analyze_all_data_dependences (struct loops
*loops
)
3890 varray_type datarefs
;
3891 varray_type dependence_relations
;
3892 int nb_data_refs
= 10;
3894 VARRAY_GENERIC_PTR_INIT (datarefs
, nb_data_refs
, "datarefs");
3895 VARRAY_GENERIC_PTR_INIT (dependence_relations
,
3896 nb_data_refs
* nb_data_refs
,
3897 "dependence_relations");
3899 /* Compute DDs on the whole function. */
3900 compute_data_dependences_for_loop (loops
->parray
[0], false,
3901 &datarefs
, &dependence_relations
);
3905 dump_data_dependence_relations (dump_file
, dependence_relations
);
3906 fprintf (dump_file
, "\n\n");
3908 if (dump_flags
& TDF_DETAILS
)
3909 dump_dist_dir_vectors (dump_file
, dependence_relations
);
3911 if (dump_flags
& TDF_STATS
)
3913 unsigned nb_top_relations
= 0;
3914 unsigned nb_bot_relations
= 0;
3915 unsigned nb_basename_differ
= 0;
3916 unsigned nb_chrec_relations
= 0;
3918 for (i
= 0; i
< VARRAY_ACTIVE_SIZE (dependence_relations
); i
++)
3920 struct data_dependence_relation
*ddr
;
3921 ddr
= VARRAY_GENERIC_PTR (dependence_relations
, i
);
3923 if (chrec_contains_undetermined (DDR_ARE_DEPENDENT (ddr
)))
3926 else if (DDR_ARE_DEPENDENT (ddr
) == chrec_known
)
3928 struct data_reference
*a
= DDR_A (ddr
);
3929 struct data_reference
*b
= DDR_B (ddr
);
3932 if ((DR_BASE_OBJECT (a
) && DR_BASE_OBJECT (b
)
3933 && DR_NUM_DIMENSIONS (a
) != DR_NUM_DIMENSIONS (b
))
3934 || (base_object_differ_p (a
, b
, &differ_p
)
3936 nb_basename_differ
++;
3942 nb_chrec_relations
++;
3945 gather_stats_on_scev_database ();
3949 free_dependence_relations (dependence_relations
);
3950 free_data_refs (datarefs
);
3953 /* Free the memory used by a data dependence relation DDR. */
3956 free_dependence_relation (struct data_dependence_relation
*ddr
)
3961 if (DDR_ARE_DEPENDENT (ddr
) == NULL_TREE
&& DDR_SUBSCRIPTS (ddr
))
3962 varray_clear (DDR_SUBSCRIPTS (ddr
));
3966 /* Free the memory used by the data dependence relations from
3967 DEPENDENCE_RELATIONS. */
3970 free_dependence_relations (varray_type dependence_relations
)
3973 if (dependence_relations
== NULL
)
3976 for (i
= 0; i
< VARRAY_ACTIVE_SIZE (dependence_relations
); i
++)
3977 free_dependence_relation (VARRAY_GENERIC_PTR (dependence_relations
, i
));
3978 varray_clear (dependence_relations
);
3981 /* Free the memory used by the data references from DATAREFS. */
3984 free_data_refs (varray_type datarefs
)
3988 if (datarefs
== NULL
)
3991 for (i
= 0; i
< VARRAY_ACTIVE_SIZE (datarefs
); i
++)
3993 struct data_reference
*dr
= (struct data_reference
*)
3994 VARRAY_GENERIC_PTR (datarefs
, i
);
3997 DR_FREE_ACCESS_FNS (dr
);
4001 varray_clear (datarefs
);