2007-02-20 Manuel Lopez-Ibanez <manu@gcc.gnu.org>
[official-gcc.git] / gcc / tree-data-ref.c
blobb84f36be660aad6141fd8787e30e379649b13953
1 /* Data references and dependences detectors.
2 Copyright (C) 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
3 Contributed by Sebastian Pop <pop@cri.ensmp.fr>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, USA. */
22 /* This pass walks a given loop structure searching for array
23 references. The information about the array accesses is recorded
24 in DATA_REFERENCE structures.
26 The basic test for determining the dependences is:
27 given two access functions chrec1 and chrec2 to a same array, and
28 x and y two vectors from the iteration domain, the same element of
29 the array is accessed twice at iterations x and y if and only if:
30 | chrec1 (x) == chrec2 (y).
32 The goals of this analysis are:
34 - to determine the independence: the relation between two
35 independent accesses is qualified with the chrec_known (this
36 information allows a loop parallelization),
38 - when two data references access the same data, to qualify the
39 dependence relation with classic dependence representations:
41 - distance vectors
42 - direction vectors
43 - loop carried level dependence
44 - polyhedron dependence
45 or with the chains of recurrences based representation,
47 - to define a knowledge base for storing the data dependence
48 information,
50 - to define an interface to access this data.
53 Definitions:
55 - subscript: given two array accesses a subscript is the tuple
56 composed of the access functions for a given dimension. Example:
57 Given A[f1][f2][f3] and B[g1][g2][g3], there are three subscripts:
58 (f1, g1), (f2, g2), (f3, g3).
60 - Diophantine equation: an equation whose coefficients and
61 solutions are integer constants, for example the equation
62 | 3*x + 2*y = 1
63 has an integer solution x = 1 and y = -1.
65 References:
67 - "Advanced Compilation for High Performance Computing" by Randy
68 Allen and Ken Kennedy.
69 http://citeseer.ist.psu.edu/goff91practical.html
71 - "Loop Transformations for Restructuring Compilers - The Foundations"
72 by Utpal Banerjee.
77 #include "config.h"
78 #include "system.h"
79 #include "coretypes.h"
80 #include "tm.h"
81 #include "ggc.h"
82 #include "tree.h"
84 /* These RTL headers are needed for basic-block.h. */
85 #include "rtl.h"
86 #include "basic-block.h"
87 #include "diagnostic.h"
88 #include "tree-flow.h"
89 #include "tree-dump.h"
90 #include "timevar.h"
91 #include "cfgloop.h"
92 #include "tree-chrec.h"
93 #include "tree-data-ref.h"
94 #include "tree-scalar-evolution.h"
95 #include "tree-pass.h"
96 #include "langhooks.h"
98 static struct datadep_stats
100 int num_dependence_tests;
101 int num_dependence_dependent;
102 int num_dependence_independent;
103 int num_dependence_undetermined;
105 int num_subscript_tests;
106 int num_subscript_undetermined;
107 int num_same_subscript_function;
109 int num_ziv;
110 int num_ziv_independent;
111 int num_ziv_dependent;
112 int num_ziv_unimplemented;
114 int num_siv;
115 int num_siv_independent;
116 int num_siv_dependent;
117 int num_siv_unimplemented;
119 int num_miv;
120 int num_miv_independent;
121 int num_miv_dependent;
122 int num_miv_unimplemented;
123 } dependence_stats;
125 static tree object_analysis (tree, tree, bool, struct data_reference **,
126 tree *, tree *, tree *, tree *, tree *,
127 struct ptr_info_def **, subvar_t *);
128 static struct data_reference * init_data_ref (tree, tree, tree, tree, bool,
129 tree, tree, tree, tree, tree,
130 struct ptr_info_def *,
131 enum data_ref_type);
132 static bool subscript_dependence_tester_1 (struct data_dependence_relation *,
133 struct data_reference *,
134 struct data_reference *);
136 /* Determine if PTR and DECL may alias, the result is put in ALIASED.
137 Return FALSE if there is no symbol memory tag for PTR. */
139 static bool
140 ptr_decl_may_alias_p (tree ptr, tree decl,
141 struct data_reference *ptr_dr,
142 bool *aliased)
144 tree tag = NULL_TREE;
145 struct ptr_info_def *pi = DR_PTR_INFO (ptr_dr);
147 gcc_assert (TREE_CODE (ptr) == SSA_NAME && DECL_P (decl));
149 if (pi)
150 tag = pi->name_mem_tag;
151 if (!tag)
152 tag = symbol_mem_tag (SSA_NAME_VAR (ptr));
153 if (!tag)
154 tag = DR_MEMTAG (ptr_dr);
155 if (!tag)
156 return false;
158 *aliased = is_aliased_with (tag, decl);
159 return true;
163 /* Determine if two pointers may alias, the result is put in ALIASED.
164 Return FALSE if there is no symbol memory tag for one of the pointers. */
166 static bool
167 ptr_ptr_may_alias_p (tree ptr_a, tree ptr_b,
168 struct data_reference *dra,
169 struct data_reference *drb,
170 bool *aliased)
172 tree tag_a = NULL_TREE, tag_b = NULL_TREE;
173 struct ptr_info_def *pi_a = DR_PTR_INFO (dra);
174 struct ptr_info_def *pi_b = DR_PTR_INFO (drb);
176 if (pi_a && pi_a->name_mem_tag && pi_b && pi_b->name_mem_tag)
178 tag_a = pi_a->name_mem_tag;
179 tag_b = pi_b->name_mem_tag;
181 else
183 tag_a = symbol_mem_tag (SSA_NAME_VAR (ptr_a));
184 if (!tag_a)
185 tag_a = DR_MEMTAG (dra);
186 if (!tag_a)
187 return false;
189 tag_b = symbol_mem_tag (SSA_NAME_VAR (ptr_b));
190 if (!tag_b)
191 tag_b = DR_MEMTAG (drb);
192 if (!tag_b)
193 return false;
195 *aliased = (tag_a == tag_b);
196 return true;
200 /* Determine if BASE_A and BASE_B may alias, the result is put in ALIASED.
201 Return FALSE if there is no symbol memory tag for one of the symbols. */
203 static bool
204 may_alias_p (tree base_a, tree base_b,
205 struct data_reference *dra,
206 struct data_reference *drb,
207 bool *aliased)
209 if (TREE_CODE (base_a) == ADDR_EXPR || TREE_CODE (base_b) == ADDR_EXPR)
211 if (TREE_CODE (base_a) == ADDR_EXPR && TREE_CODE (base_b) == ADDR_EXPR)
213 *aliased = (TREE_OPERAND (base_a, 0) == TREE_OPERAND (base_b, 0));
214 return true;
216 if (TREE_CODE (base_a) == ADDR_EXPR)
217 return ptr_decl_may_alias_p (base_b, TREE_OPERAND (base_a, 0), drb,
218 aliased);
219 else
220 return ptr_decl_may_alias_p (base_a, TREE_OPERAND (base_b, 0), dra,
221 aliased);
224 return ptr_ptr_may_alias_p (base_a, base_b, dra, drb, aliased);
228 /* Determine if a pointer (BASE_A) and a record/union access (BASE_B)
229 are not aliased. Return TRUE if they differ. */
230 static bool
231 record_ptr_differ_p (struct data_reference *dra,
232 struct data_reference *drb)
234 bool aliased;
235 tree base_a = DR_BASE_OBJECT (dra);
236 tree base_b = DR_BASE_OBJECT (drb);
238 if (TREE_CODE (base_b) != COMPONENT_REF)
239 return false;
241 /* Peel COMPONENT_REFs to get to the base. Do not peel INDIRECT_REFs.
242 For a.b.c.d[i] we will get a, and for a.b->c.d[i] we will get a.b.
243 Probably will be unnecessary with struct alias analysis. */
244 while (TREE_CODE (base_b) == COMPONENT_REF)
245 base_b = TREE_OPERAND (base_b, 0);
246 /* Compare a record/union access (b.c[i] or p->c[i]) and a pointer
247 ((*q)[i]). */
248 if (TREE_CODE (base_a) == INDIRECT_REF
249 && ((TREE_CODE (base_b) == VAR_DECL
250 && (ptr_decl_may_alias_p (TREE_OPERAND (base_a, 0), base_b, dra,
251 &aliased)
252 && !aliased))
253 || (TREE_CODE (base_b) == INDIRECT_REF
254 && (ptr_ptr_may_alias_p (TREE_OPERAND (base_a, 0),
255 TREE_OPERAND (base_b, 0), dra, drb,
256 &aliased)
257 && !aliased))))
258 return true;
259 else
260 return false;
263 /* Determine if two record/union accesses are aliased. Return TRUE if they
264 differ. */
265 static bool
266 record_record_differ_p (struct data_reference *dra,
267 struct data_reference *drb)
269 bool aliased;
270 tree base_a = DR_BASE_OBJECT (dra);
271 tree base_b = DR_BASE_OBJECT (drb);
273 if (TREE_CODE (base_b) != COMPONENT_REF
274 || TREE_CODE (base_a) != COMPONENT_REF)
275 return false;
277 /* Peel COMPONENT_REFs to get to the base. Do not peel INDIRECT_REFs.
278 For a.b.c.d[i] we will get a, and for a.b->c.d[i] we will get a.b.
279 Probably will be unnecessary with struct alias analysis. */
280 while (TREE_CODE (base_b) == COMPONENT_REF)
281 base_b = TREE_OPERAND (base_b, 0);
282 while (TREE_CODE (base_a) == COMPONENT_REF)
283 base_a = TREE_OPERAND (base_a, 0);
285 if (TREE_CODE (base_a) == INDIRECT_REF
286 && TREE_CODE (base_b) == INDIRECT_REF
287 && ptr_ptr_may_alias_p (TREE_OPERAND (base_a, 0),
288 TREE_OPERAND (base_b, 0),
289 dra, drb, &aliased)
290 && !aliased)
291 return true;
292 else
293 return false;
296 /* Determine if an array access (BASE_A) and a record/union access (BASE_B)
297 are not aliased. Return TRUE if they differ. */
298 static bool
299 record_array_differ_p (struct data_reference *dra,
300 struct data_reference *drb)
302 bool aliased;
303 tree base_a = DR_BASE_OBJECT (dra);
304 tree base_b = DR_BASE_OBJECT (drb);
306 if (TREE_CODE (base_b) != COMPONENT_REF)
307 return false;
309 /* Peel COMPONENT_REFs to get to the base. Do not peel INDIRECT_REFs.
310 For a.b.c.d[i] we will get a, and for a.b->c.d[i] we will get a.b.
311 Probably will be unnecessary with struct alias analysis. */
312 while (TREE_CODE (base_b) == COMPONENT_REF)
313 base_b = TREE_OPERAND (base_b, 0);
315 /* Compare a record/union access (b.c[i] or p->c[i]) and an array access
316 (a[i]). In case of p->c[i] use alias analysis to verify that p is not
317 pointing to a. */
318 if (TREE_CODE (base_a) == VAR_DECL
319 && (TREE_CODE (base_b) == VAR_DECL
320 || (TREE_CODE (base_b) == INDIRECT_REF
321 && (ptr_decl_may_alias_p (TREE_OPERAND (base_b, 0), base_a, drb,
322 &aliased)
323 && !aliased))))
324 return true;
325 else
326 return false;
330 /* Determine if an array access (BASE_A) and a pointer (BASE_B)
331 are not aliased. Return TRUE if they differ. */
332 static bool
333 array_ptr_differ_p (tree base_a, tree base_b,
334 struct data_reference *drb)
336 bool aliased;
338 /* In case one of the bases is a pointer (a[i] and (*p)[i]), we check with the
339 help of alias analysis that p is not pointing to a. */
340 if (TREE_CODE (base_a) == VAR_DECL && TREE_CODE (base_b) == INDIRECT_REF
341 && (ptr_decl_may_alias_p (TREE_OPERAND (base_b, 0), base_a, drb, &aliased)
342 && !aliased))
343 return true;
344 else
345 return false;
349 /* This is the simplest data dependence test: determines whether the
350 data references A and B access the same array/region. Returns
351 false when the property is not computable at compile time.
352 Otherwise return true, and DIFFER_P will record the result. This
353 utility will not be necessary when alias_sets_conflict_p will be
354 less conservative. */
356 static bool
357 base_object_differ_p (struct data_reference *a,
358 struct data_reference *b,
359 bool *differ_p)
361 tree base_a = DR_BASE_OBJECT (a);
362 tree base_b = DR_BASE_OBJECT (b);
363 bool aliased;
365 if (!base_a || !base_b)
366 return false;
368 /* Determine if same base. Example: for the array accesses
369 a[i], b[i] or pointer accesses *a, *b, bases are a, b. */
370 if (base_a == base_b)
372 *differ_p = false;
373 return true;
376 /* For pointer based accesses, (*p)[i], (*q)[j], the bases are (*p)
377 and (*q) */
378 if (TREE_CODE (base_a) == INDIRECT_REF && TREE_CODE (base_b) == INDIRECT_REF
379 && TREE_OPERAND (base_a, 0) == TREE_OPERAND (base_b, 0))
381 *differ_p = false;
382 return true;
385 /* Record/union based accesses - s.a[i], t.b[j]. bases are s.a,t.b. */
386 if (TREE_CODE (base_a) == COMPONENT_REF && TREE_CODE (base_b) == COMPONENT_REF
387 && TREE_OPERAND (base_a, 0) == TREE_OPERAND (base_b, 0)
388 && TREE_OPERAND (base_a, 1) == TREE_OPERAND (base_b, 1))
390 *differ_p = false;
391 return true;
395 /* Determine if different bases. */
397 /* At this point we know that base_a != base_b. However, pointer
398 accesses of the form x=(*p) and y=(*q), whose bases are p and q,
399 may still be pointing to the same base. In SSAed GIMPLE p and q will
400 be SSA_NAMES in this case. Therefore, here we check if they are
401 really two different declarations. */
402 if (TREE_CODE (base_a) == VAR_DECL && TREE_CODE (base_b) == VAR_DECL)
404 *differ_p = true;
405 return true;
408 /* In case one of the bases is a pointer (a[i] and (*p)[i]), we check with the
409 help of alias analysis that p is not pointing to a. */
410 if (array_ptr_differ_p (base_a, base_b, b)
411 || array_ptr_differ_p (base_b, base_a, a))
413 *differ_p = true;
414 return true;
417 /* If the bases are pointers ((*q)[i] and (*p)[i]), we check with the
418 help of alias analysis they don't point to the same bases. */
419 if (TREE_CODE (base_a) == INDIRECT_REF && TREE_CODE (base_b) == INDIRECT_REF
420 && (may_alias_p (TREE_OPERAND (base_a, 0), TREE_OPERAND (base_b, 0), a, b,
421 &aliased)
422 && !aliased))
424 *differ_p = true;
425 return true;
428 /* Compare two record/union bases s.a and t.b: s != t or (a != b and
429 s and t are not unions). */
430 if (TREE_CODE (base_a) == COMPONENT_REF && TREE_CODE (base_b) == COMPONENT_REF
431 && ((TREE_CODE (TREE_OPERAND (base_a, 0)) == VAR_DECL
432 && TREE_CODE (TREE_OPERAND (base_b, 0)) == VAR_DECL
433 && TREE_OPERAND (base_a, 0) != TREE_OPERAND (base_b, 0))
434 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (base_a, 0))) == RECORD_TYPE
435 && TREE_CODE (TREE_TYPE (TREE_OPERAND (base_b, 0))) == RECORD_TYPE
436 && TREE_OPERAND (base_a, 1) != TREE_OPERAND (base_b, 1))))
438 *differ_p = true;
439 return true;
442 /* Compare a record/union access (b.c[i] or p->c[i]) and a pointer
443 ((*q)[i]). */
444 if (record_ptr_differ_p (a, b) || record_ptr_differ_p (b, a))
446 *differ_p = true;
447 return true;
450 /* Compare a record/union access (b.c[i] or p->c[i]) and an array access
451 (a[i]). In case of p->c[i] use alias analysis to verify that p is not
452 pointing to a. */
453 if (record_array_differ_p (a, b) || record_array_differ_p (b, a))
455 *differ_p = true;
456 return true;
459 /* Compare two record/union accesses (b.c[i] or p->c[i]). */
460 if (record_record_differ_p (a, b))
462 *differ_p = true;
463 return true;
466 return false;
469 /* Function base_addr_differ_p.
471 This is the simplest data dependence test: determines whether the
472 data references DRA and DRB access the same array/region. Returns
473 false when the property is not computable at compile time.
474 Otherwise return true, and DIFFER_P will record the result.
476 The algorithm:
477 1. if (both DRA and DRB are represented as arrays)
478 compare DRA.BASE_OBJECT and DRB.BASE_OBJECT
479 2. else if (both DRA and DRB are represented as pointers)
480 try to prove that DRA.FIRST_LOCATION == DRB.FIRST_LOCATION
481 3. else if (DRA and DRB are represented differently or 2. fails)
482 only try to prove that the bases are surely different
485 static bool
486 base_addr_differ_p (struct data_reference *dra,
487 struct data_reference *drb,
488 bool *differ_p)
490 tree addr_a = DR_BASE_ADDRESS (dra);
491 tree addr_b = DR_BASE_ADDRESS (drb);
492 tree type_a, type_b;
493 tree decl_a, decl_b;
494 bool aliased;
496 if (!addr_a || !addr_b)
497 return false;
499 type_a = TREE_TYPE (addr_a);
500 type_b = TREE_TYPE (addr_b);
502 gcc_assert (POINTER_TYPE_P (type_a) && POINTER_TYPE_P (type_b));
504 /* 1. if (both DRA and DRB are represented as arrays)
505 compare DRA.BASE_OBJECT and DRB.BASE_OBJECT. */
506 if (DR_TYPE (dra) == ARRAY_REF_TYPE && DR_TYPE (drb) == ARRAY_REF_TYPE)
507 return base_object_differ_p (dra, drb, differ_p);
509 /* 2. else if (both DRA and DRB are represented as pointers)
510 try to prove that DRA.FIRST_LOCATION == DRB.FIRST_LOCATION. */
511 /* If base addresses are the same, we check the offsets, since the access of
512 the data-ref is described by {base addr + offset} and its access function,
513 i.e., in order to decide whether the bases of data-refs are the same we
514 compare both base addresses and offsets. */
515 if (DR_TYPE (dra) == POINTER_REF_TYPE && DR_TYPE (drb) == POINTER_REF_TYPE
516 && (addr_a == addr_b
517 || (TREE_CODE (addr_a) == ADDR_EXPR && TREE_CODE (addr_b) == ADDR_EXPR
518 && TREE_OPERAND (addr_a, 0) == TREE_OPERAND (addr_b, 0))))
520 /* Compare offsets. */
521 tree offset_a = DR_OFFSET (dra);
522 tree offset_b = DR_OFFSET (drb);
524 STRIP_NOPS (offset_a);
525 STRIP_NOPS (offset_b);
527 /* FORNOW: we only compare offsets that are MULT_EXPR, i.e., we don't handle
528 PLUS_EXPR. */
529 if (offset_a == offset_b
530 || (TREE_CODE (offset_a) == MULT_EXPR
531 && TREE_CODE (offset_b) == MULT_EXPR
532 && TREE_OPERAND (offset_a, 0) == TREE_OPERAND (offset_b, 0)
533 && TREE_OPERAND (offset_a, 1) == TREE_OPERAND (offset_b, 1)))
535 *differ_p = false;
536 return true;
540 /* 3. else if (DRA and DRB are represented differently or 2. fails)
541 only try to prove that the bases are surely different. */
543 /* Apply alias analysis. */
544 if (may_alias_p (addr_a, addr_b, dra, drb, &aliased) && !aliased)
546 *differ_p = true;
547 return true;
550 /* An instruction writing through a restricted pointer is "independent" of any
551 instruction reading or writing through a different restricted pointer,
552 in the same block/scope. */
553 else if (TYPE_RESTRICT (type_a)
554 && TYPE_RESTRICT (type_b)
555 && (!DR_IS_READ (drb) || !DR_IS_READ (dra))
556 && TREE_CODE (DR_BASE_ADDRESS (dra)) == SSA_NAME
557 && (decl_a = SSA_NAME_VAR (DR_BASE_ADDRESS (dra)))
558 && TREE_CODE (decl_a) == PARM_DECL
559 && TREE_CODE (DECL_CONTEXT (decl_a)) == FUNCTION_DECL
560 && TREE_CODE (DR_BASE_ADDRESS (drb)) == SSA_NAME
561 && (decl_b = SSA_NAME_VAR (DR_BASE_ADDRESS (drb)))
562 && TREE_CODE (decl_b) == PARM_DECL
563 && TREE_CODE (DECL_CONTEXT (decl_b)) == FUNCTION_DECL
564 && DECL_CONTEXT (decl_a) == DECL_CONTEXT (decl_b))
566 *differ_p = true;
567 return true;
570 return false;
573 /* Returns true iff A divides B. */
575 static inline bool
576 tree_fold_divides_p (tree a, tree b)
578 gcc_assert (TREE_CODE (a) == INTEGER_CST);
579 gcc_assert (TREE_CODE (b) == INTEGER_CST);
580 return integer_zerop (int_const_binop (TRUNC_MOD_EXPR, b, a, 0));
583 /* Returns true iff A divides B. */
585 static inline bool
586 int_divides_p (int a, int b)
588 return ((b % a) == 0);
593 /* Dump into FILE all the data references from DATAREFS. */
595 void
596 dump_data_references (FILE *file, VEC (data_reference_p, heap) *datarefs)
598 unsigned int i;
599 struct data_reference *dr;
601 for (i = 0; VEC_iterate (data_reference_p, datarefs, i, dr); i++)
602 dump_data_reference (file, dr);
605 /* Dump into FILE all the dependence relations from DDRS. */
607 void
608 dump_data_dependence_relations (FILE *file,
609 VEC (ddr_p, heap) *ddrs)
611 unsigned int i;
612 struct data_dependence_relation *ddr;
614 for (i = 0; VEC_iterate (ddr_p, ddrs, i, ddr); i++)
615 dump_data_dependence_relation (file, ddr);
618 /* Dump function for a DATA_REFERENCE structure. */
620 void
621 dump_data_reference (FILE *outf,
622 struct data_reference *dr)
624 unsigned int i;
626 fprintf (outf, "(Data Ref: \n stmt: ");
627 print_generic_stmt (outf, DR_STMT (dr), 0);
628 fprintf (outf, " ref: ");
629 print_generic_stmt (outf, DR_REF (dr), 0);
630 fprintf (outf, " base_object: ");
631 print_generic_stmt (outf, DR_BASE_OBJECT (dr), 0);
633 for (i = 0; i < DR_NUM_DIMENSIONS (dr); i++)
635 fprintf (outf, " Access function %d: ", i);
636 print_generic_stmt (outf, DR_ACCESS_FN (dr, i), 0);
638 fprintf (outf, ")\n");
641 /* Dumps the affine function described by FN to the file OUTF. */
643 static void
644 dump_affine_function (FILE *outf, affine_fn fn)
646 unsigned i;
647 tree coef;
649 print_generic_expr (outf, VEC_index (tree, fn, 0), TDF_SLIM);
650 for (i = 1; VEC_iterate (tree, fn, i, coef); i++)
652 fprintf (outf, " + ");
653 print_generic_expr (outf, coef, TDF_SLIM);
654 fprintf (outf, " * x_%u", i);
658 /* Dumps the conflict function CF to the file OUTF. */
660 static void
661 dump_conflict_function (FILE *outf, conflict_function *cf)
663 unsigned i;
665 if (cf->n == NO_DEPENDENCE)
666 fprintf (outf, "no dependence\n");
667 else if (cf->n == NOT_KNOWN)
668 fprintf (outf, "not known\n");
669 else
671 for (i = 0; i < cf->n; i++)
673 fprintf (outf, "[");
674 dump_affine_function (outf, cf->fns[i]);
675 fprintf (outf, "]\n");
680 /* Dump function for a SUBSCRIPT structure. */
682 void
683 dump_subscript (FILE *outf, struct subscript *subscript)
685 conflict_function *cf = SUB_CONFLICTS_IN_A (subscript);
687 fprintf (outf, "\n (subscript \n");
688 fprintf (outf, " iterations_that_access_an_element_twice_in_A: ");
689 dump_conflict_function (outf, cf);
690 if (CF_NONTRIVIAL_P (cf))
692 tree last_iteration = SUB_LAST_CONFLICT (subscript);
693 fprintf (outf, " last_conflict: ");
694 print_generic_stmt (outf, last_iteration, 0);
697 cf = SUB_CONFLICTS_IN_B (subscript);
698 fprintf (outf, " iterations_that_access_an_element_twice_in_B: ");
699 dump_conflict_function (outf, cf);
700 if (CF_NONTRIVIAL_P (cf))
702 tree last_iteration = SUB_LAST_CONFLICT (subscript);
703 fprintf (outf, " last_conflict: ");
704 print_generic_stmt (outf, last_iteration, 0);
707 fprintf (outf, " (Subscript distance: ");
708 print_generic_stmt (outf, SUB_DISTANCE (subscript), 0);
709 fprintf (outf, " )\n");
710 fprintf (outf, " )\n");
713 /* Print the classic direction vector DIRV to OUTF. */
715 void
716 print_direction_vector (FILE *outf,
717 lambda_vector dirv,
718 int length)
720 int eq;
722 for (eq = 0; eq < length; eq++)
724 enum data_dependence_direction dir = dirv[eq];
726 switch (dir)
728 case dir_positive:
729 fprintf (outf, " +");
730 break;
731 case dir_negative:
732 fprintf (outf, " -");
733 break;
734 case dir_equal:
735 fprintf (outf, " =");
736 break;
737 case dir_positive_or_equal:
738 fprintf (outf, " +=");
739 break;
740 case dir_positive_or_negative:
741 fprintf (outf, " +-");
742 break;
743 case dir_negative_or_equal:
744 fprintf (outf, " -=");
745 break;
746 case dir_star:
747 fprintf (outf, " *");
748 break;
749 default:
750 fprintf (outf, "indep");
751 break;
754 fprintf (outf, "\n");
757 /* Print a vector of direction vectors. */
759 void
760 print_dir_vectors (FILE *outf, VEC (lambda_vector, heap) *dir_vects,
761 int length)
763 unsigned j;
764 lambda_vector v;
766 for (j = 0; VEC_iterate (lambda_vector, dir_vects, j, v); j++)
767 print_direction_vector (outf, v, length);
770 /* Print a vector of distance vectors. */
772 void
773 print_dist_vectors (FILE *outf, VEC (lambda_vector, heap) *dist_vects,
774 int length)
776 unsigned j;
777 lambda_vector v;
779 for (j = 0; VEC_iterate (lambda_vector, dist_vects, j, v); j++)
780 print_lambda_vector (outf, v, length);
783 /* Debug version. */
785 void
786 debug_data_dependence_relation (struct data_dependence_relation *ddr)
788 dump_data_dependence_relation (stderr, ddr);
791 /* Dump function for a DATA_DEPENDENCE_RELATION structure. */
793 void
794 dump_data_dependence_relation (FILE *outf,
795 struct data_dependence_relation *ddr)
797 struct data_reference *dra, *drb;
799 dra = DDR_A (ddr);
800 drb = DDR_B (ddr);
801 fprintf (outf, "(Data Dep: \n");
802 if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
803 fprintf (outf, " (don't know)\n");
805 else if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
806 fprintf (outf, " (no dependence)\n");
808 else if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
810 unsigned int i;
811 struct loop *loopi;
813 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
815 fprintf (outf, " access_fn_A: ");
816 print_generic_stmt (outf, DR_ACCESS_FN (dra, i), 0);
817 fprintf (outf, " access_fn_B: ");
818 print_generic_stmt (outf, DR_ACCESS_FN (drb, i), 0);
819 dump_subscript (outf, DDR_SUBSCRIPT (ddr, i));
822 fprintf (outf, " loop nest: (");
823 for (i = 0; VEC_iterate (loop_p, DDR_LOOP_NEST (ddr), i, loopi); i++)
824 fprintf (outf, "%d ", loopi->num);
825 fprintf (outf, ")\n");
827 for (i = 0; i < DDR_NUM_DIST_VECTS (ddr); i++)
829 fprintf (outf, " distance_vector: ");
830 print_lambda_vector (outf, DDR_DIST_VECT (ddr, i),
831 DDR_NB_LOOPS (ddr));
834 for (i = 0; i < DDR_NUM_DIR_VECTS (ddr); i++)
836 fprintf (outf, " direction_vector: ");
837 print_direction_vector (outf, DDR_DIR_VECT (ddr, i),
838 DDR_NB_LOOPS (ddr));
842 fprintf (outf, ")\n");
845 /* Dump function for a DATA_DEPENDENCE_DIRECTION structure. */
847 void
848 dump_data_dependence_direction (FILE *file,
849 enum data_dependence_direction dir)
851 switch (dir)
853 case dir_positive:
854 fprintf (file, "+");
855 break;
857 case dir_negative:
858 fprintf (file, "-");
859 break;
861 case dir_equal:
862 fprintf (file, "=");
863 break;
865 case dir_positive_or_negative:
866 fprintf (file, "+-");
867 break;
869 case dir_positive_or_equal:
870 fprintf (file, "+=");
871 break;
873 case dir_negative_or_equal:
874 fprintf (file, "-=");
875 break;
877 case dir_star:
878 fprintf (file, "*");
879 break;
881 default:
882 break;
886 /* Dumps the distance and direction vectors in FILE. DDRS contains
887 the dependence relations, and VECT_SIZE is the size of the
888 dependence vectors, or in other words the number of loops in the
889 considered nest. */
891 void
892 dump_dist_dir_vectors (FILE *file, VEC (ddr_p, heap) *ddrs)
894 unsigned int i, j;
895 struct data_dependence_relation *ddr;
896 lambda_vector v;
898 for (i = 0; VEC_iterate (ddr_p, ddrs, i, ddr); i++)
899 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE && DDR_AFFINE_P (ddr))
901 for (j = 0; VEC_iterate (lambda_vector, DDR_DIST_VECTS (ddr), j, v); j++)
903 fprintf (file, "DISTANCE_V (");
904 print_lambda_vector (file, v, DDR_NB_LOOPS (ddr));
905 fprintf (file, ")\n");
908 for (j = 0; VEC_iterate (lambda_vector, DDR_DIR_VECTS (ddr), j, v); j++)
910 fprintf (file, "DIRECTION_V (");
911 print_direction_vector (file, v, DDR_NB_LOOPS (ddr));
912 fprintf (file, ")\n");
916 fprintf (file, "\n\n");
919 /* Dumps the data dependence relations DDRS in FILE. */
921 void
922 dump_ddrs (FILE *file, VEC (ddr_p, heap) *ddrs)
924 unsigned int i;
925 struct data_dependence_relation *ddr;
927 for (i = 0; VEC_iterate (ddr_p, ddrs, i, ddr); i++)
928 dump_data_dependence_relation (file, ddr);
930 fprintf (file, "\n\n");
935 /* Given an ARRAY_REF node REF, records its access functions.
936 Example: given A[i][3], record in ACCESS_FNS the opnd1 function,
937 i.e. the constant "3", then recursively call the function on opnd0,
938 i.e. the ARRAY_REF "A[i]".
939 The function returns the base name: "A". */
941 static tree
942 analyze_array_indexes (struct loop *loop,
943 VEC(tree,heap) **access_fns,
944 tree ref, tree stmt)
946 tree opnd0, opnd1;
947 tree access_fn;
949 opnd0 = TREE_OPERAND (ref, 0);
950 opnd1 = TREE_OPERAND (ref, 1);
952 /* The detection of the evolution function for this data access is
953 postponed until the dependence test. This lazy strategy avoids
954 the computation of access functions that are of no interest for
955 the optimizers. */
956 access_fn = instantiate_parameters
957 (loop, analyze_scalar_evolution (loop, opnd1));
959 VEC_safe_push (tree, heap, *access_fns, access_fn);
961 /* Recursively record other array access functions. */
962 if (TREE_CODE (opnd0) == ARRAY_REF)
963 return analyze_array_indexes (loop, access_fns, opnd0, stmt);
965 /* Return the base name of the data access. */
966 else
967 return opnd0;
970 /* For a data reference REF contained in the statement STMT, initialize
971 a DATA_REFERENCE structure, and return it. IS_READ flag has to be
972 set to true when REF is in the right hand side of an
973 assignment. */
975 struct data_reference *
976 analyze_array (tree stmt, tree ref, bool is_read)
978 struct data_reference *res;
979 VEC(tree,heap) *acc_fns;
981 if (dump_file && (dump_flags & TDF_DETAILS))
983 fprintf (dump_file, "(analyze_array \n");
984 fprintf (dump_file, " (ref = ");
985 print_generic_stmt (dump_file, ref, 0);
986 fprintf (dump_file, ")\n");
989 res = XNEW (struct data_reference);
991 DR_STMT (res) = stmt;
992 DR_REF (res) = ref;
993 acc_fns = VEC_alloc (tree, heap, 3);
994 DR_BASE_OBJECT (res) = analyze_array_indexes
995 (loop_containing_stmt (stmt), &acc_fns, ref, stmt);
996 DR_TYPE (res) = ARRAY_REF_TYPE;
997 DR_SET_ACCESS_FNS (res, acc_fns);
998 DR_IS_READ (res) = is_read;
999 DR_BASE_ADDRESS (res) = NULL_TREE;
1000 DR_OFFSET (res) = NULL_TREE;
1001 DR_INIT (res) = NULL_TREE;
1002 DR_STEP (res) = NULL_TREE;
1003 DR_OFFSET_MISALIGNMENT (res) = NULL_TREE;
1004 DR_MEMTAG (res) = NULL_TREE;
1005 DR_PTR_INFO (res) = NULL;
1007 if (dump_file && (dump_flags & TDF_DETAILS))
1008 fprintf (dump_file, ")\n");
1010 return res;
1013 /* Analyze an indirect memory reference, REF, that comes from STMT.
1014 IS_READ is true if this is an indirect load, and false if it is
1015 an indirect store.
1016 Return a new data reference structure representing the indirect_ref, or
1017 NULL if we cannot describe the access function. */
1019 static struct data_reference *
1020 analyze_indirect_ref (tree stmt, tree ref, bool is_read)
1022 struct loop *loop = loop_containing_stmt (stmt);
1023 tree ptr_ref = TREE_OPERAND (ref, 0);
1024 tree access_fn = analyze_scalar_evolution (loop, ptr_ref);
1025 tree init = initial_condition_in_loop_num (access_fn, loop->num);
1026 tree base_address = NULL_TREE, evolution, step = NULL_TREE;
1027 struct ptr_info_def *ptr_info = NULL;
1029 if (TREE_CODE (ptr_ref) == SSA_NAME)
1030 ptr_info = SSA_NAME_PTR_INFO (ptr_ref);
1032 STRIP_NOPS (init);
1033 if (access_fn == chrec_dont_know || !init || init == chrec_dont_know)
1035 if (dump_file && (dump_flags & TDF_DETAILS))
1037 fprintf (dump_file, "\nBad access function of ptr: ");
1038 print_generic_expr (dump_file, ref, TDF_SLIM);
1039 fprintf (dump_file, "\n");
1041 return NULL;
1044 if (dump_file && (dump_flags & TDF_DETAILS))
1046 fprintf (dump_file, "\nAccess function of ptr: ");
1047 print_generic_expr (dump_file, access_fn, TDF_SLIM);
1048 fprintf (dump_file, "\n");
1051 if (!expr_invariant_in_loop_p (loop, init))
1053 if (dump_file && (dump_flags & TDF_DETAILS))
1054 fprintf (dump_file, "\ninitial condition is not loop invariant.\n");
1056 else
1058 base_address = init;
1059 evolution = evolution_part_in_loop_num (access_fn, loop->num);
1060 if (evolution != chrec_dont_know)
1062 if (!evolution)
1063 step = ssize_int (0);
1064 else
1066 if (TREE_CODE (evolution) == INTEGER_CST)
1067 step = fold_convert (ssizetype, evolution);
1068 else
1069 if (dump_file && (dump_flags & TDF_DETAILS))
1070 fprintf (dump_file, "\nnon constant step for ptr access.\n");
1073 else
1074 if (dump_file && (dump_flags & TDF_DETAILS))
1075 fprintf (dump_file, "\nunknown evolution of ptr.\n");
1077 return init_data_ref (stmt, ref, NULL_TREE, access_fn, is_read, base_address,
1078 NULL_TREE, step, NULL_TREE, NULL_TREE,
1079 ptr_info, POINTER_REF_TYPE);
1082 /* For a data reference REF contained in the statement STMT, initialize
1083 a DATA_REFERENCE structure, and return it. */
1085 struct data_reference *
1086 init_data_ref (tree stmt,
1087 tree ref,
1088 tree base,
1089 tree access_fn,
1090 bool is_read,
1091 tree base_address,
1092 tree init_offset,
1093 tree step,
1094 tree misalign,
1095 tree memtag,
1096 struct ptr_info_def *ptr_info,
1097 enum data_ref_type type)
1099 struct data_reference *res;
1100 VEC(tree,heap) *acc_fns;
1102 if (dump_file && (dump_flags & TDF_DETAILS))
1104 fprintf (dump_file, "(init_data_ref \n");
1105 fprintf (dump_file, " (ref = ");
1106 print_generic_stmt (dump_file, ref, 0);
1107 fprintf (dump_file, ")\n");
1110 res = XNEW (struct data_reference);
1112 DR_STMT (res) = stmt;
1113 DR_REF (res) = ref;
1114 DR_BASE_OBJECT (res) = base;
1115 DR_TYPE (res) = type;
1116 acc_fns = VEC_alloc (tree, heap, 3);
1117 DR_SET_ACCESS_FNS (res, acc_fns);
1118 VEC_quick_push (tree, DR_ACCESS_FNS (res), access_fn);
1119 DR_IS_READ (res) = is_read;
1120 DR_BASE_ADDRESS (res) = base_address;
1121 DR_OFFSET (res) = init_offset;
1122 DR_INIT (res) = NULL_TREE;
1123 DR_STEP (res) = step;
1124 DR_OFFSET_MISALIGNMENT (res) = misalign;
1125 DR_MEMTAG (res) = memtag;
1126 DR_PTR_INFO (res) = ptr_info;
1128 if (dump_file && (dump_flags & TDF_DETAILS))
1129 fprintf (dump_file, ")\n");
1131 return res;
1134 /* Function strip_conversions
1136 Strip conversions that don't narrow the mode. */
1138 static tree
1139 strip_conversion (tree expr)
1141 tree to, ti, oprnd0;
1143 while (TREE_CODE (expr) == NOP_EXPR || TREE_CODE (expr) == CONVERT_EXPR)
1145 to = TREE_TYPE (expr);
1146 oprnd0 = TREE_OPERAND (expr, 0);
1147 ti = TREE_TYPE (oprnd0);
1149 if (!INTEGRAL_TYPE_P (to) || !INTEGRAL_TYPE_P (ti))
1150 return NULL_TREE;
1151 if (GET_MODE_SIZE (TYPE_MODE (to)) < GET_MODE_SIZE (TYPE_MODE (ti)))
1152 return NULL_TREE;
1154 expr = oprnd0;
1156 return expr;
1160 /* Function analyze_offset_expr
1162 Given an offset expression EXPR received from get_inner_reference, analyze
1163 it and create an expression for INITIAL_OFFSET by substituting the variables
1164 of EXPR with initial_condition of the corresponding access_fn in the loop.
1165 E.g.,
1166 for i
1167 for (j = 3; j < N; j++)
1168 a[j].b[i][j] = 0;
1170 For a[j].b[i][j], EXPR will be 'i * C_i + j * C_j + C'. 'i' cannot be
1171 substituted, since its access_fn in the inner loop is i. 'j' will be
1172 substituted with 3. An INITIAL_OFFSET will be 'i * C_i + C`', where
1173 C` = 3 * C_j + C.
1175 Compute MISALIGN (the misalignment of the data reference initial access from
1176 its base). Misalignment can be calculated only if all the variables can be
1177 substituted with constants, otherwise, we record maximum possible alignment
1178 in ALIGNED_TO. In the above example, since 'i' cannot be substituted, MISALIGN
1179 will be NULL_TREE, and the biggest divider of C_i (a power of 2) will be
1180 recorded in ALIGNED_TO.
1182 STEP is an evolution of the data reference in this loop in bytes.
1183 In the above example, STEP is C_j.
1185 Return FALSE, if the analysis fails, e.g., there is no access_fn for a
1186 variable. In this case, all the outputs (INITIAL_OFFSET, MISALIGN, ALIGNED_TO
1187 and STEP) are NULL_TREEs. Otherwise, return TRUE.
1191 static bool
1192 analyze_offset_expr (tree expr,
1193 struct loop *loop,
1194 tree *initial_offset,
1195 tree *misalign,
1196 tree *aligned_to,
1197 tree *step)
1199 tree oprnd0;
1200 tree oprnd1;
1201 tree left_offset = ssize_int (0);
1202 tree right_offset = ssize_int (0);
1203 tree left_misalign = ssize_int (0);
1204 tree right_misalign = ssize_int (0);
1205 tree left_step = ssize_int (0);
1206 tree right_step = ssize_int (0);
1207 enum tree_code code;
1208 tree init, evolution;
1209 tree left_aligned_to = NULL_TREE, right_aligned_to = NULL_TREE;
1211 *step = NULL_TREE;
1212 *misalign = NULL_TREE;
1213 *aligned_to = NULL_TREE;
1214 *initial_offset = NULL_TREE;
1216 /* Strip conversions that don't narrow the mode. */
1217 expr = strip_conversion (expr);
1218 if (!expr)
1219 return false;
1221 /* Stop conditions:
1222 1. Constant. */
1223 if (TREE_CODE (expr) == INTEGER_CST)
1225 *initial_offset = fold_convert (ssizetype, expr);
1226 *misalign = fold_convert (ssizetype, expr);
1227 *step = ssize_int (0);
1228 return true;
1231 /* 2. Variable. Try to substitute with initial_condition of the corresponding
1232 access_fn in the current loop. */
1233 if (SSA_VAR_P (expr))
1235 tree access_fn = analyze_scalar_evolution (loop, expr);
1237 if (access_fn == chrec_dont_know)
1238 /* No access_fn. */
1239 return false;
1241 init = initial_condition_in_loop_num (access_fn, loop->num);
1242 if (!expr_invariant_in_loop_p (loop, init))
1243 /* Not enough information: may be not loop invariant.
1244 E.g., for a[b[i]], we get a[D], where D=b[i]. EXPR is D, its
1245 initial_condition is D, but it depends on i - loop's induction
1246 variable. */
1247 return false;
1249 evolution = evolution_part_in_loop_num (access_fn, loop->num);
1250 if (evolution && TREE_CODE (evolution) != INTEGER_CST)
1251 /* Evolution is not constant. */
1252 return false;
1254 if (TREE_CODE (init) == INTEGER_CST)
1255 *misalign = fold_convert (ssizetype, init);
1256 else
1257 /* Not constant, misalignment cannot be calculated. */
1258 *misalign = NULL_TREE;
1260 *initial_offset = fold_convert (ssizetype, init);
1262 *step = evolution ? fold_convert (ssizetype, evolution) : ssize_int (0);
1263 return true;
1266 /* Recursive computation. */
1267 if (!BINARY_CLASS_P (expr))
1269 /* We expect to get binary expressions (PLUS/MINUS and MULT). */
1270 if (dump_file && (dump_flags & TDF_DETAILS))
1272 fprintf (dump_file, "\nNot binary expression ");
1273 print_generic_expr (dump_file, expr, TDF_SLIM);
1274 fprintf (dump_file, "\n");
1276 return false;
1278 oprnd0 = TREE_OPERAND (expr, 0);
1279 oprnd1 = TREE_OPERAND (expr, 1);
1281 if (!analyze_offset_expr (oprnd0, loop, &left_offset, &left_misalign,
1282 &left_aligned_to, &left_step)
1283 || !analyze_offset_expr (oprnd1, loop, &right_offset, &right_misalign,
1284 &right_aligned_to, &right_step))
1285 return false;
1287 /* The type of the operation: plus, minus or mult. */
1288 code = TREE_CODE (expr);
1289 switch (code)
1291 case MULT_EXPR:
1292 if (TREE_CODE (right_offset) != INTEGER_CST)
1293 /* RIGHT_OFFSET can be not constant. For example, for arrays of variable
1294 sized types.
1295 FORNOW: We don't support such cases. */
1296 return false;
1298 /* Strip conversions that don't narrow the mode. */
1299 left_offset = strip_conversion (left_offset);
1300 if (!left_offset)
1301 return false;
1302 /* Misalignment computation. */
1303 if (SSA_VAR_P (left_offset))
1305 /* If the left side contains variables that can't be substituted with
1306 constants, the misalignment is unknown. However, if the right side
1307 is a multiple of some alignment, we know that the expression is
1308 aligned to it. Therefore, we record such maximum possible value.
1310 *misalign = NULL_TREE;
1311 *aligned_to = ssize_int (highest_pow2_factor (right_offset));
1313 else
1315 /* The left operand was successfully substituted with constant. */
1316 if (left_misalign)
1318 /* In case of EXPR '(i * C1 + j) * C2', LEFT_MISALIGN is
1319 NULL_TREE. */
1320 *misalign = size_binop (code, left_misalign, right_misalign);
1321 if (left_aligned_to && right_aligned_to)
1322 *aligned_to = size_binop (MIN_EXPR, left_aligned_to,
1323 right_aligned_to);
1324 else
1325 *aligned_to = left_aligned_to ?
1326 left_aligned_to : right_aligned_to;
1328 else
1329 *misalign = NULL_TREE;
1332 /* Step calculation. */
1333 /* Multiply the step by the right operand. */
1334 *step = size_binop (MULT_EXPR, left_step, right_offset);
1335 break;
1337 case PLUS_EXPR:
1338 case MINUS_EXPR:
1339 /* Combine the recursive calculations for step and misalignment. */
1340 *step = size_binop (code, left_step, right_step);
1342 /* Unknown alignment. */
1343 if ((!left_misalign && !left_aligned_to)
1344 || (!right_misalign && !right_aligned_to))
1346 *misalign = NULL_TREE;
1347 *aligned_to = NULL_TREE;
1348 break;
1351 if (left_misalign && right_misalign)
1352 *misalign = size_binop (code, left_misalign, right_misalign);
1353 else
1354 *misalign = left_misalign ? left_misalign : right_misalign;
1356 if (left_aligned_to && right_aligned_to)
1357 *aligned_to = size_binop (MIN_EXPR, left_aligned_to, right_aligned_to);
1358 else
1359 *aligned_to = left_aligned_to ? left_aligned_to : right_aligned_to;
1361 break;
1363 default:
1364 gcc_unreachable ();
1367 /* Compute offset. */
1368 *initial_offset = fold_convert (ssizetype,
1369 fold_build2 (code, TREE_TYPE (left_offset),
1370 left_offset,
1371 right_offset));
1372 return true;
1375 /* Function address_analysis
1377 Return the BASE of the address expression EXPR.
1378 Also compute the OFFSET from BASE, MISALIGN and STEP.
1380 Input:
1381 EXPR - the address expression that is being analyzed
1382 STMT - the statement that contains EXPR or its original memory reference
1383 IS_READ - TRUE if STMT reads from EXPR, FALSE if writes to EXPR
1384 DR - data_reference struct for the original memory reference
1386 Output:
1387 BASE (returned value) - the base of the data reference EXPR.
1388 INITIAL_OFFSET - initial offset of EXPR from BASE (an expression)
1389 MISALIGN - offset of EXPR from BASE in bytes (a constant) or NULL_TREE if the
1390 computation is impossible
1391 ALIGNED_TO - maximum alignment of EXPR or NULL_TREE if MISALIGN can be
1392 calculated (doesn't depend on variables)
1393 STEP - evolution of EXPR in the loop
1395 If something unexpected is encountered (an unsupported form of data-ref),
1396 then NULL_TREE is returned.
1399 static tree
1400 address_analysis (tree expr, tree stmt, bool is_read, struct data_reference *dr,
1401 tree *offset, tree *misalign, tree *aligned_to, tree *step)
1403 tree oprnd0, oprnd1, base_address, offset_expr, base_addr0, base_addr1;
1404 tree address_offset = ssize_int (0), address_misalign = ssize_int (0);
1405 tree dummy, address_aligned_to = NULL_TREE;
1406 struct ptr_info_def *dummy1;
1407 subvar_t dummy2;
1409 switch (TREE_CODE (expr))
1411 case PLUS_EXPR:
1412 case MINUS_EXPR:
1413 /* EXPR is of form {base +/- offset} (or {offset +/- base}). */
1414 oprnd0 = TREE_OPERAND (expr, 0);
1415 oprnd1 = TREE_OPERAND (expr, 1);
1417 STRIP_NOPS (oprnd0);
1418 STRIP_NOPS (oprnd1);
1420 /* Recursively try to find the base of the address contained in EXPR.
1421 For offset, the returned base will be NULL. */
1422 base_addr0 = address_analysis (oprnd0, stmt, is_read, dr, &address_offset,
1423 &address_misalign, &address_aligned_to,
1424 step);
1426 base_addr1 = address_analysis (oprnd1, stmt, is_read, dr, &address_offset,
1427 &address_misalign, &address_aligned_to,
1428 step);
1430 /* We support cases where only one of the operands contains an
1431 address. */
1432 if ((base_addr0 && base_addr1) || (!base_addr0 && !base_addr1))
1434 if (dump_file && (dump_flags & TDF_DETAILS))
1436 fprintf (dump_file,
1437 "\neither more than one address or no addresses in expr ");
1438 print_generic_expr (dump_file, expr, TDF_SLIM);
1439 fprintf (dump_file, "\n");
1441 return NULL_TREE;
1444 /* To revert STRIP_NOPS. */
1445 oprnd0 = TREE_OPERAND (expr, 0);
1446 oprnd1 = TREE_OPERAND (expr, 1);
1448 offset_expr = base_addr0 ?
1449 fold_convert (ssizetype, oprnd1) : fold_convert (ssizetype, oprnd0);
1451 /* EXPR is of form {base +/- offset} (or {offset +/- base}). If offset is
1452 a number, we can add it to the misalignment value calculated for base,
1453 otherwise, misalignment is NULL. */
1454 if (TREE_CODE (offset_expr) == INTEGER_CST && address_misalign)
1456 *misalign = size_binop (TREE_CODE (expr), address_misalign,
1457 offset_expr);
1458 *aligned_to = address_aligned_to;
1460 else
1462 *misalign = NULL_TREE;
1463 *aligned_to = NULL_TREE;
1466 /* Combine offset (from EXPR {base + offset}) with the offset calculated
1467 for base. */
1468 *offset = size_binop (TREE_CODE (expr), address_offset, offset_expr);
1469 return base_addr0 ? base_addr0 : base_addr1;
1471 case ADDR_EXPR:
1472 base_address = object_analysis (TREE_OPERAND (expr, 0), stmt, is_read,
1473 &dr, offset, misalign, aligned_to, step,
1474 &dummy, &dummy1, &dummy2);
1475 return base_address;
1477 case SSA_NAME:
1478 if (!POINTER_TYPE_P (TREE_TYPE (expr)))
1480 if (dump_file && (dump_flags & TDF_DETAILS))
1482 fprintf (dump_file, "\nnot pointer SSA_NAME ");
1483 print_generic_expr (dump_file, expr, TDF_SLIM);
1484 fprintf (dump_file, "\n");
1486 return NULL_TREE;
1488 *aligned_to = ssize_int (TYPE_ALIGN_UNIT (TREE_TYPE (TREE_TYPE (expr))));
1489 *misalign = ssize_int (0);
1490 *offset = ssize_int (0);
1491 *step = ssize_int (0);
1492 return expr;
1494 default:
1495 return NULL_TREE;
1500 /* Function object_analysis
1502 Create a data-reference structure DR for MEMREF.
1503 Return the BASE of the data reference MEMREF if the analysis is possible.
1504 Also compute the INITIAL_OFFSET from BASE, MISALIGN and STEP.
1505 E.g., for EXPR a.b[i] + 4B, BASE is a, and OFFSET is the overall offset
1506 'a.b[i] + 4B' from a (can be an expression), MISALIGN is an OFFSET
1507 instantiated with initial_conditions of access_functions of variables,
1508 and STEP is the evolution of the DR_REF in this loop.
1510 Function get_inner_reference is used for the above in case of ARRAY_REF and
1511 COMPONENT_REF.
1513 The structure of the function is as follows:
1514 Part 1:
1515 Case 1. For handled_component_p refs
1516 1.1 build data-reference structure for MEMREF
1517 1.2 call get_inner_reference
1518 1.2.1 analyze offset expr received from get_inner_reference
1519 (fall through with BASE)
1520 Case 2. For declarations
1521 2.1 set MEMTAG
1522 Case 3. For INDIRECT_REFs
1523 3.1 build data-reference structure for MEMREF
1524 3.2 analyze evolution and initial condition of MEMREF
1525 3.3 set data-reference structure for MEMREF
1526 3.4 call address_analysis to analyze INIT of the access function
1527 3.5 extract memory tag
1529 Part 2:
1530 Combine the results of object and address analysis to calculate
1531 INITIAL_OFFSET, STEP and misalignment info.
1533 Input:
1534 MEMREF - the memory reference that is being analyzed
1535 STMT - the statement that contains MEMREF
1536 IS_READ - TRUE if STMT reads from MEMREF, FALSE if writes to MEMREF
1538 Output:
1539 BASE_ADDRESS (returned value) - the base address of the data reference MEMREF
1540 E.g, if MEMREF is a.b[k].c[i][j] the returned
1541 base is &a.
1542 DR - data_reference struct for MEMREF
1543 INITIAL_OFFSET - initial offset of MEMREF from BASE (an expression)
1544 MISALIGN - offset of MEMREF from BASE in bytes (a constant) modulo alignment of
1545 ALIGNMENT or NULL_TREE if the computation is impossible
1546 ALIGNED_TO - maximum alignment of EXPR or NULL_TREE if MISALIGN can be
1547 calculated (doesn't depend on variables)
1548 STEP - evolution of the DR_REF in the loop
1549 MEMTAG - memory tag for aliasing purposes
1550 PTR_INFO - NULL or points-to aliasing info from a pointer SSA_NAME
1551 SUBVARS - Sub-variables of the variable
1553 If the analysis of MEMREF evolution in the loop fails, NULL_TREE is returned,
1554 but DR can be created anyway.
1558 static tree
1559 object_analysis (tree memref, tree stmt, bool is_read,
1560 struct data_reference **dr, tree *offset, tree *misalign,
1561 tree *aligned_to, tree *step, tree *memtag,
1562 struct ptr_info_def **ptr_info, subvar_t *subvars)
1564 tree base = NULL_TREE, base_address = NULL_TREE;
1565 tree object_offset = ssize_int (0), object_misalign = ssize_int (0);
1566 tree object_step = ssize_int (0), address_step = ssize_int (0);
1567 tree address_offset = ssize_int (0), address_misalign = ssize_int (0);
1568 HOST_WIDE_INT pbitsize, pbitpos;
1569 tree poffset, bit_pos_in_bytes;
1570 enum machine_mode pmode;
1571 int punsignedp, pvolatilep;
1572 tree ptr_step = ssize_int (0), ptr_init = NULL_TREE;
1573 struct loop *loop = loop_containing_stmt (stmt);
1574 struct data_reference *ptr_dr = NULL;
1575 tree object_aligned_to = NULL_TREE, address_aligned_to = NULL_TREE;
1576 tree comp_ref = NULL_TREE;
1578 *ptr_info = NULL;
1580 /* Part 1: */
1581 /* Case 1. handled_component_p refs. */
1582 if (handled_component_p (memref))
1584 /* 1.1 build data-reference structure for MEMREF. */
1585 if (!(*dr))
1587 if (TREE_CODE (memref) == ARRAY_REF)
1588 *dr = analyze_array (stmt, memref, is_read);
1589 else if (TREE_CODE (memref) == COMPONENT_REF)
1590 comp_ref = memref;
1591 else
1593 if (dump_file && (dump_flags & TDF_DETAILS))
1595 fprintf (dump_file, "\ndata-ref of unsupported type ");
1596 print_generic_expr (dump_file, memref, TDF_SLIM);
1597 fprintf (dump_file, "\n");
1599 return NULL_TREE;
1603 /* 1.2 call get_inner_reference. */
1604 /* Find the base and the offset from it. */
1605 base = get_inner_reference (memref, &pbitsize, &pbitpos, &poffset,
1606 &pmode, &punsignedp, &pvolatilep, false);
1607 if (!base)
1609 if (dump_file && (dump_flags & TDF_DETAILS))
1611 fprintf (dump_file, "\nfailed to get inner ref for ");
1612 print_generic_expr (dump_file, memref, TDF_SLIM);
1613 fprintf (dump_file, "\n");
1615 return NULL_TREE;
1618 /* 1.2.1 analyze offset expr received from get_inner_reference. */
1619 if (poffset
1620 && !analyze_offset_expr (poffset, loop, &object_offset,
1621 &object_misalign, &object_aligned_to,
1622 &object_step))
1624 if (dump_file && (dump_flags & TDF_DETAILS))
1626 fprintf (dump_file, "\nfailed to compute offset or step for ");
1627 print_generic_expr (dump_file, memref, TDF_SLIM);
1628 fprintf (dump_file, "\n");
1630 return NULL_TREE;
1633 /* Add bit position to OFFSET and MISALIGN. */
1635 bit_pos_in_bytes = ssize_int (pbitpos/BITS_PER_UNIT);
1636 /* Check that there is no remainder in bits. */
1637 if (pbitpos%BITS_PER_UNIT)
1639 if (dump_file && (dump_flags & TDF_DETAILS))
1640 fprintf (dump_file, "\nbit offset alignment.\n");
1641 return NULL_TREE;
1643 object_offset = size_binop (PLUS_EXPR, bit_pos_in_bytes, object_offset);
1644 if (object_misalign)
1645 object_misalign = size_binop (PLUS_EXPR, object_misalign,
1646 bit_pos_in_bytes);
1648 memref = base; /* To continue analysis of BASE. */
1649 /* fall through */
1652 /* Part 1: Case 2. Declarations. */
1653 if (DECL_P (memref))
1655 /* We expect to get a decl only if we already have a DR, or with
1656 COMPONENT_REFs of type 'a[i].b'. */
1657 if (!(*dr))
1659 if (comp_ref && TREE_CODE (TREE_OPERAND (comp_ref, 0)) == ARRAY_REF)
1661 *dr = analyze_array (stmt, TREE_OPERAND (comp_ref, 0), is_read);
1662 if (DR_NUM_DIMENSIONS (*dr) != 1)
1664 if (dump_file && (dump_flags & TDF_DETAILS))
1666 fprintf (dump_file, "\n multidimensional component ref ");
1667 print_generic_expr (dump_file, comp_ref, TDF_SLIM);
1668 fprintf (dump_file, "\n");
1670 return NULL_TREE;
1673 else
1675 if (dump_file && (dump_flags & TDF_DETAILS))
1677 fprintf (dump_file, "\nunhandled decl ");
1678 print_generic_expr (dump_file, memref, TDF_SLIM);
1679 fprintf (dump_file, "\n");
1681 return NULL_TREE;
1685 /* TODO: if during the analysis of INDIRECT_REF we get to an object, put
1686 the object in BASE_OBJECT field if we can prove that this is O.K.,
1687 i.e., the data-ref access is bounded by the bounds of the BASE_OBJECT.
1688 (e.g., if the object is an array base 'a', where 'a[N]', we must prove
1689 that every access with 'p' (the original INDIRECT_REF based on '&a')
1690 in the loop is within the array boundaries - from a[0] to a[N-1]).
1691 Otherwise, our alias analysis can be incorrect.
1692 Even if an access function based on BASE_OBJECT can't be build, update
1693 BASE_OBJECT field to enable us to prove that two data-refs are
1694 different (without access function, distance analysis is impossible).
1696 if (SSA_VAR_P (memref) && var_can_have_subvars (memref))
1697 *subvars = get_subvars_for_var (memref);
1698 base_address = build_fold_addr_expr (memref);
1699 /* 2.1 set MEMTAG. */
1700 *memtag = memref;
1703 /* Part 1: Case 3. INDIRECT_REFs. */
1704 else if (TREE_CODE (memref) == INDIRECT_REF)
1706 tree ptr_ref = TREE_OPERAND (memref, 0);
1707 if (TREE_CODE (ptr_ref) == SSA_NAME)
1708 *ptr_info = SSA_NAME_PTR_INFO (ptr_ref);
1710 /* 3.1 build data-reference structure for MEMREF. */
1711 ptr_dr = analyze_indirect_ref (stmt, memref, is_read);
1712 if (!ptr_dr)
1714 if (dump_file && (dump_flags & TDF_DETAILS))
1716 fprintf (dump_file, "\nfailed to create dr for ");
1717 print_generic_expr (dump_file, memref, TDF_SLIM);
1718 fprintf (dump_file, "\n");
1720 return NULL_TREE;
1723 /* 3.2 analyze evolution and initial condition of MEMREF. */
1724 ptr_step = DR_STEP (ptr_dr);
1725 ptr_init = DR_BASE_ADDRESS (ptr_dr);
1726 if (!ptr_init || !ptr_step || !POINTER_TYPE_P (TREE_TYPE (ptr_init)))
1728 *dr = (*dr) ? *dr : ptr_dr;
1729 if (dump_file && (dump_flags & TDF_DETAILS))
1731 fprintf (dump_file, "\nbad pointer access ");
1732 print_generic_expr (dump_file, memref, TDF_SLIM);
1733 fprintf (dump_file, "\n");
1735 return NULL_TREE;
1738 if (integer_zerop (ptr_step) && !(*dr))
1740 if (dump_file && (dump_flags & TDF_DETAILS))
1741 fprintf (dump_file, "\nptr is loop invariant.\n");
1742 *dr = ptr_dr;
1743 return NULL_TREE;
1745 /* If there exists DR for MEMREF, we are analyzing the base of
1746 handled component (PTR_INIT), which not necessary has evolution in
1747 the loop. */
1749 object_step = size_binop (PLUS_EXPR, object_step, ptr_step);
1751 /* 3.3 set data-reference structure for MEMREF. */
1752 if (!*dr)
1753 *dr = ptr_dr;
1755 /* 3.4 call address_analysis to analyze INIT of the access
1756 function. */
1757 base_address = address_analysis (ptr_init, stmt, is_read, *dr,
1758 &address_offset, &address_misalign,
1759 &address_aligned_to, &address_step);
1760 if (!base_address)
1762 if (dump_file && (dump_flags & TDF_DETAILS))
1764 fprintf (dump_file, "\nfailed to analyze address ");
1765 print_generic_expr (dump_file, ptr_init, TDF_SLIM);
1766 fprintf (dump_file, "\n");
1768 return NULL_TREE;
1771 /* 3.5 extract memory tag. */
1772 switch (TREE_CODE (base_address))
1774 case SSA_NAME:
1775 *memtag = symbol_mem_tag (SSA_NAME_VAR (base_address));
1776 if (!(*memtag) && TREE_CODE (TREE_OPERAND (memref, 0)) == SSA_NAME)
1777 *memtag = symbol_mem_tag (SSA_NAME_VAR (TREE_OPERAND (memref, 0)));
1778 break;
1779 case ADDR_EXPR:
1780 *memtag = TREE_OPERAND (base_address, 0);
1781 break;
1782 default:
1783 if (dump_file && (dump_flags & TDF_DETAILS))
1785 fprintf (dump_file, "\nno memtag for ");
1786 print_generic_expr (dump_file, memref, TDF_SLIM);
1787 fprintf (dump_file, "\n");
1789 *memtag = NULL_TREE;
1790 break;
1794 if (!base_address)
1796 /* MEMREF cannot be analyzed. */
1797 if (dump_file && (dump_flags & TDF_DETAILS))
1799 fprintf (dump_file, "\ndata-ref of unsupported type ");
1800 print_generic_expr (dump_file, memref, TDF_SLIM);
1801 fprintf (dump_file, "\n");
1803 return NULL_TREE;
1806 if (comp_ref)
1807 DR_REF (*dr) = comp_ref;
1809 if (SSA_VAR_P (*memtag) && var_can_have_subvars (*memtag))
1810 *subvars = get_subvars_for_var (*memtag);
1812 /* Part 2: Combine the results of object and address analysis to calculate
1813 INITIAL_OFFSET, STEP and misalignment info. */
1814 *offset = size_binop (PLUS_EXPR, object_offset, address_offset);
1816 if ((!object_misalign && !object_aligned_to)
1817 || (!address_misalign && !address_aligned_to))
1819 *misalign = NULL_TREE;
1820 *aligned_to = NULL_TREE;
1822 else
1824 if (object_misalign && address_misalign)
1825 *misalign = size_binop (PLUS_EXPR, object_misalign, address_misalign);
1826 else
1827 *misalign = object_misalign ? object_misalign : address_misalign;
1828 if (object_aligned_to && address_aligned_to)
1829 *aligned_to = size_binop (MIN_EXPR, object_aligned_to,
1830 address_aligned_to);
1831 else
1832 *aligned_to = object_aligned_to ?
1833 object_aligned_to : address_aligned_to;
1835 *step = size_binop (PLUS_EXPR, object_step, address_step);
1837 return base_address;
1840 /* Function analyze_offset.
1842 Extract INVARIANT and CONSTANT parts from OFFSET.
1845 static void
1846 analyze_offset (tree offset, tree *invariant, tree *constant)
1848 tree op0, op1, constant_0, constant_1, invariant_0, invariant_1;
1849 enum tree_code code = TREE_CODE (offset);
1851 *invariant = NULL_TREE;
1852 *constant = NULL_TREE;
1854 /* Not PLUS/MINUS expression - recursion stop condition. */
1855 if (code != PLUS_EXPR && code != MINUS_EXPR)
1857 if (TREE_CODE (offset) == INTEGER_CST)
1858 *constant = offset;
1859 else
1860 *invariant = offset;
1861 return;
1864 op0 = TREE_OPERAND (offset, 0);
1865 op1 = TREE_OPERAND (offset, 1);
1867 /* Recursive call with the operands. */
1868 analyze_offset (op0, &invariant_0, &constant_0);
1869 analyze_offset (op1, &invariant_1, &constant_1);
1871 /* Combine the results. */
1872 *constant = constant_0 ? constant_0 : constant_1;
1873 if (invariant_0 && invariant_1)
1874 *invariant =
1875 fold_build2 (code, TREE_TYPE (invariant_0), invariant_0, invariant_1);
1876 else
1877 *invariant = invariant_0 ? invariant_0 : invariant_1;
1880 /* Free the memory used by the data reference DR. */
1882 static void
1883 free_data_ref (data_reference_p dr)
1885 DR_FREE_ACCESS_FNS (dr);
1886 free (dr);
1889 /* Function create_data_ref.
1891 Create a data-reference structure for MEMREF. Set its DR_BASE_ADDRESS,
1892 DR_OFFSET, DR_INIT, DR_STEP, DR_OFFSET_MISALIGNMENT, DR_ALIGNED_TO,
1893 DR_MEMTAG, and DR_POINTSTO_INFO fields.
1895 Input:
1896 MEMREF - the memory reference that is being analyzed
1897 STMT - the statement that contains MEMREF
1898 IS_READ - TRUE if STMT reads from MEMREF, FALSE if writes to MEMREF
1900 Output:
1901 DR (returned value) - data_reference struct for MEMREF
1904 static struct data_reference *
1905 create_data_ref (tree memref, tree stmt, bool is_read)
1907 struct data_reference *dr = NULL;
1908 tree base_address, offset, step, misalign, memtag;
1909 struct loop *loop = loop_containing_stmt (stmt);
1910 tree invariant = NULL_TREE, constant = NULL_TREE;
1911 tree type_size, init_cond;
1912 struct ptr_info_def *ptr_info;
1913 subvar_t subvars = NULL;
1914 tree aligned_to, type = NULL_TREE, orig_offset;
1916 if (!memref)
1917 return NULL;
1919 base_address = object_analysis (memref, stmt, is_read, &dr, &offset,
1920 &misalign, &aligned_to, &step, &memtag,
1921 &ptr_info, &subvars);
1922 if (!dr || !base_address)
1924 if (dump_file && (dump_flags & TDF_DETAILS))
1926 fprintf (dump_file, "\ncreate_data_ref: failed to create a dr for ");
1927 print_generic_expr (dump_file, memref, TDF_SLIM);
1928 fprintf (dump_file, "\n");
1930 return NULL;
1933 DR_BASE_ADDRESS (dr) = base_address;
1934 DR_OFFSET (dr) = offset;
1935 DR_INIT (dr) = ssize_int (0);
1936 DR_STEP (dr) = step;
1937 DR_OFFSET_MISALIGNMENT (dr) = misalign;
1938 DR_ALIGNED_TO (dr) = aligned_to;
1939 DR_MEMTAG (dr) = memtag;
1940 DR_PTR_INFO (dr) = ptr_info;
1941 DR_SUBVARS (dr) = subvars;
1943 type_size = fold_convert (ssizetype, TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dr))));
1945 /* Extract CONSTANT and INVARIANT from OFFSET. */
1946 /* Remove cast from OFFSET and restore it for INVARIANT part. */
1947 orig_offset = offset;
1948 STRIP_NOPS (offset);
1949 if (offset != orig_offset)
1950 type = TREE_TYPE (orig_offset);
1951 analyze_offset (offset, &invariant, &constant);
1952 if (type && invariant)
1953 invariant = fold_convert (type, invariant);
1955 /* Put CONSTANT part of OFFSET in DR_INIT and INVARIANT in DR_OFFSET field
1956 of DR. */
1957 if (constant)
1959 DR_INIT (dr) = fold_convert (ssizetype, constant);
1960 init_cond = fold_build2 (TRUNC_DIV_EXPR, TREE_TYPE (constant),
1961 constant, type_size);
1963 else
1964 DR_INIT (dr) = init_cond = ssize_int (0);
1966 if (invariant)
1967 DR_OFFSET (dr) = invariant;
1968 else
1969 DR_OFFSET (dr) = ssize_int (0);
1971 /* Change the access function for INIDIRECT_REFs, according to
1972 DR_BASE_ADDRESS. Analyze OFFSET calculated in object_analysis. OFFSET is
1973 an expression that can contain loop invariant expressions and constants.
1974 We put the constant part in the initial condition of the access function
1975 (for data dependence tests), and in DR_INIT of the data-ref. The loop
1976 invariant part is put in DR_OFFSET.
1977 The evolution part of the access function is STEP calculated in
1978 object_analysis divided by the size of data type.
1980 if (!DR_BASE_OBJECT (dr)
1981 || (TREE_CODE (memref) == COMPONENT_REF && DR_NUM_DIMENSIONS (dr) == 1))
1983 tree access_fn;
1984 tree new_step;
1986 /* Update access function. */
1987 access_fn = DR_ACCESS_FN (dr, 0);
1988 if (automatically_generated_chrec_p (access_fn))
1990 free_data_ref (dr);
1991 return NULL;
1994 new_step = size_binop (TRUNC_DIV_EXPR,
1995 fold_convert (ssizetype, step), type_size);
1997 init_cond = chrec_convert (chrec_type (access_fn), init_cond, stmt);
1998 new_step = chrec_convert (chrec_type (access_fn), new_step, stmt);
1999 if (automatically_generated_chrec_p (init_cond)
2000 || automatically_generated_chrec_p (new_step))
2002 free_data_ref (dr);
2003 return NULL;
2005 access_fn = chrec_replace_initial_condition (access_fn, init_cond);
2006 access_fn = reset_evolution_in_loop (loop->num, access_fn, new_step);
2008 VEC_replace (tree, DR_ACCESS_FNS (dr), 0, access_fn);
2011 if (dump_file && (dump_flags & TDF_DETAILS))
2013 struct ptr_info_def *pi = DR_PTR_INFO (dr);
2015 fprintf (dump_file, "\nCreated dr for ");
2016 print_generic_expr (dump_file, memref, TDF_SLIM);
2017 fprintf (dump_file, "\n\tbase_address: ");
2018 print_generic_expr (dump_file, DR_BASE_ADDRESS (dr), TDF_SLIM);
2019 fprintf (dump_file, "\n\toffset from base address: ");
2020 print_generic_expr (dump_file, DR_OFFSET (dr), TDF_SLIM);
2021 fprintf (dump_file, "\n\tconstant offset from base address: ");
2022 print_generic_expr (dump_file, DR_INIT (dr), TDF_SLIM);
2023 fprintf (dump_file, "\n\tbase_object: ");
2024 print_generic_expr (dump_file, DR_BASE_OBJECT (dr), TDF_SLIM);
2025 fprintf (dump_file, "\n\tstep: ");
2026 print_generic_expr (dump_file, DR_STEP (dr), TDF_SLIM);
2027 fprintf (dump_file, "B\n\tmisalignment from base: ");
2028 print_generic_expr (dump_file, DR_OFFSET_MISALIGNMENT (dr), TDF_SLIM);
2029 if (DR_OFFSET_MISALIGNMENT (dr))
2030 fprintf (dump_file, "B");
2031 if (DR_ALIGNED_TO (dr))
2033 fprintf (dump_file, "\n\taligned to: ");
2034 print_generic_expr (dump_file, DR_ALIGNED_TO (dr), TDF_SLIM);
2036 fprintf (dump_file, "\n\tmemtag: ");
2037 print_generic_expr (dump_file, DR_MEMTAG (dr), TDF_SLIM);
2038 fprintf (dump_file, "\n");
2039 if (pi && pi->name_mem_tag)
2041 fprintf (dump_file, "\n\tnametag: ");
2042 print_generic_expr (dump_file, pi->name_mem_tag, TDF_SLIM);
2043 fprintf (dump_file, "\n");
2046 return dr;
2049 /* Returns true if FNA == FNB. */
2051 static bool
2052 affine_function_equal_p (affine_fn fna, affine_fn fnb)
2054 unsigned i, n = VEC_length (tree, fna);
2056 gcc_assert (n == VEC_length (tree, fnb));
2058 for (i = 0; i < n; i++)
2059 if (!operand_equal_p (VEC_index (tree, fna, i),
2060 VEC_index (tree, fnb, i), 0))
2061 return false;
2063 return true;
2066 /* If all the functions in CF are the same, returns one of them,
2067 otherwise returns NULL. */
2069 static affine_fn
2070 common_affine_function (conflict_function *cf)
2072 unsigned i;
2073 affine_fn comm;
2075 if (!CF_NONTRIVIAL_P (cf))
2076 return NULL;
2078 comm = cf->fns[0];
2080 for (i = 1; i < cf->n; i++)
2081 if (!affine_function_equal_p (comm, cf->fns[i]))
2082 return NULL;
2084 return comm;
2087 /* Returns the base of the affine function FN. */
2089 static tree
2090 affine_function_base (affine_fn fn)
2092 return VEC_index (tree, fn, 0);
2095 /* Returns true if FN is a constant. */
2097 static bool
2098 affine_function_constant_p (affine_fn fn)
2100 unsigned i;
2101 tree coef;
2103 for (i = 1; VEC_iterate (tree, fn, i, coef); i++)
2104 if (!integer_zerop (coef))
2105 return false;
2107 return true;
2110 /* Applies operation OP on affine functions FNA and FNB, and returns the
2111 result. */
2113 static affine_fn
2114 affine_fn_op (enum tree_code op, affine_fn fna, affine_fn fnb)
2116 unsigned i, n, m;
2117 affine_fn ret;
2118 tree coef;
2120 if (VEC_length (tree, fnb) > VEC_length (tree, fna))
2122 n = VEC_length (tree, fna);
2123 m = VEC_length (tree, fnb);
2125 else
2127 n = VEC_length (tree, fnb);
2128 m = VEC_length (tree, fna);
2131 ret = VEC_alloc (tree, heap, m);
2132 for (i = 0; i < n; i++)
2133 VEC_quick_push (tree, ret,
2134 fold_build2 (op, integer_type_node,
2135 VEC_index (tree, fna, i),
2136 VEC_index (tree, fnb, i)));
2138 for (; VEC_iterate (tree, fna, i, coef); i++)
2139 VEC_quick_push (tree, ret,
2140 fold_build2 (op, integer_type_node,
2141 coef, integer_zero_node));
2142 for (; VEC_iterate (tree, fnb, i, coef); i++)
2143 VEC_quick_push (tree, ret,
2144 fold_build2 (op, integer_type_node,
2145 integer_zero_node, coef));
2147 return ret;
2150 /* Returns the sum of affine functions FNA and FNB. */
2152 static affine_fn
2153 affine_fn_plus (affine_fn fna, affine_fn fnb)
2155 return affine_fn_op (PLUS_EXPR, fna, fnb);
2158 /* Returns the difference of affine functions FNA and FNB. */
2160 static affine_fn
2161 affine_fn_minus (affine_fn fna, affine_fn fnb)
2163 return affine_fn_op (MINUS_EXPR, fna, fnb);
2166 /* Frees affine function FN. */
2168 static void
2169 affine_fn_free (affine_fn fn)
2171 VEC_free (tree, heap, fn);
2174 /* Determine for each subscript in the data dependence relation DDR
2175 the distance. */
2177 static void
2178 compute_subscript_distance (struct data_dependence_relation *ddr)
2180 conflict_function *cf_a, *cf_b;
2181 affine_fn fn_a, fn_b, diff;
2183 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
2185 unsigned int i;
2187 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
2189 struct subscript *subscript;
2191 subscript = DDR_SUBSCRIPT (ddr, i);
2192 cf_a = SUB_CONFLICTS_IN_A (subscript);
2193 cf_b = SUB_CONFLICTS_IN_B (subscript);
2195 fn_a = common_affine_function (cf_a);
2196 fn_b = common_affine_function (cf_b);
2197 if (!fn_a || !fn_b)
2199 SUB_DISTANCE (subscript) = chrec_dont_know;
2200 return;
2202 diff = affine_fn_minus (fn_a, fn_b);
2204 if (affine_function_constant_p (diff))
2205 SUB_DISTANCE (subscript) = affine_function_base (diff);
2206 else
2207 SUB_DISTANCE (subscript) = chrec_dont_know;
2209 affine_fn_free (diff);
2214 /* Returns the conflict function for "unknown". */
2216 static conflict_function *
2217 conflict_fn_not_known (void)
2219 conflict_function *fn = XCNEW (conflict_function);
2220 fn->n = NOT_KNOWN;
2222 return fn;
2225 /* Returns the conflict function for "independent". */
2227 static conflict_function *
2228 conflict_fn_no_dependence (void)
2230 conflict_function *fn = XCNEW (conflict_function);
2231 fn->n = NO_DEPENDENCE;
2233 return fn;
2236 /* Initialize a data dependence relation between data accesses A and
2237 B. NB_LOOPS is the number of loops surrounding the references: the
2238 size of the classic distance/direction vectors. */
2240 static struct data_dependence_relation *
2241 initialize_data_dependence_relation (struct data_reference *a,
2242 struct data_reference *b,
2243 VEC (loop_p, heap) *loop_nest)
2245 struct data_dependence_relation *res;
2246 bool differ_p, known_dependence;
2247 unsigned int i;
2249 res = XNEW (struct data_dependence_relation);
2250 DDR_A (res) = a;
2251 DDR_B (res) = b;
2252 DDR_LOOP_NEST (res) = NULL;
2254 if (a == NULL || b == NULL)
2256 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
2257 return res;
2260 /* When A and B are arrays and their dimensions differ, we directly
2261 initialize the relation to "there is no dependence": chrec_known. */
2262 if (DR_BASE_OBJECT (a) && DR_BASE_OBJECT (b)
2263 && DR_NUM_DIMENSIONS (a) != DR_NUM_DIMENSIONS (b))
2265 DDR_ARE_DEPENDENT (res) = chrec_known;
2266 return res;
2269 if (DR_BASE_ADDRESS (a) && DR_BASE_ADDRESS (b))
2270 known_dependence = base_addr_differ_p (a, b, &differ_p);
2271 else
2272 known_dependence = base_object_differ_p (a, b, &differ_p);
2274 if (!known_dependence)
2276 /* Can't determine whether the data-refs access the same memory
2277 region. */
2278 DDR_ARE_DEPENDENT (res) = chrec_dont_know;
2279 return res;
2282 if (differ_p)
2284 DDR_ARE_DEPENDENT (res) = chrec_known;
2285 return res;
2288 DDR_AFFINE_P (res) = true;
2289 DDR_ARE_DEPENDENT (res) = NULL_TREE;
2290 DDR_SUBSCRIPTS (res) = VEC_alloc (subscript_p, heap, DR_NUM_DIMENSIONS (a));
2291 DDR_LOOP_NEST (res) = loop_nest;
2292 DDR_DIR_VECTS (res) = NULL;
2293 DDR_DIST_VECTS (res) = NULL;
2295 for (i = 0; i < DR_NUM_DIMENSIONS (a); i++)
2297 struct subscript *subscript;
2299 subscript = XNEW (struct subscript);
2300 SUB_CONFLICTS_IN_A (subscript) = conflict_fn_not_known ();
2301 SUB_CONFLICTS_IN_B (subscript) = conflict_fn_not_known ();
2302 SUB_LAST_CONFLICT (subscript) = chrec_dont_know;
2303 SUB_DISTANCE (subscript) = chrec_dont_know;
2304 VEC_safe_push (subscript_p, heap, DDR_SUBSCRIPTS (res), subscript);
2307 return res;
2310 /* Frees memory used by the conflict function F. */
2312 static void
2313 free_conflict_function (conflict_function *f)
2315 unsigned i;
2317 if (CF_NONTRIVIAL_P (f))
2319 for (i = 0; i < f->n; i++)
2320 affine_fn_free (f->fns[i]);
2322 free (f);
2325 /* Frees memory used by SUBSCRIPTS. */
2327 static void
2328 free_subscripts (VEC (subscript_p, heap) *subscripts)
2330 unsigned i;
2331 subscript_p s;
2333 for (i = 0; VEC_iterate (subscript_p, subscripts, i, s); i++)
2335 free_conflict_function (s->conflicting_iterations_in_a);
2336 free_conflict_function (s->conflicting_iterations_in_b);
2338 VEC_free (subscript_p, heap, subscripts);
2341 /* Set DDR_ARE_DEPENDENT to CHREC and finalize the subscript overlap
2342 description. */
2344 static inline void
2345 finalize_ddr_dependent (struct data_dependence_relation *ddr,
2346 tree chrec)
2348 if (dump_file && (dump_flags & TDF_DETAILS))
2350 fprintf (dump_file, "(dependence classified: ");
2351 print_generic_expr (dump_file, chrec, 0);
2352 fprintf (dump_file, ")\n");
2355 DDR_ARE_DEPENDENT (ddr) = chrec;
2356 free_subscripts (DDR_SUBSCRIPTS (ddr));
2359 /* The dependence relation DDR cannot be represented by a distance
2360 vector. */
2362 static inline void
2363 non_affine_dependence_relation (struct data_dependence_relation *ddr)
2365 if (dump_file && (dump_flags & TDF_DETAILS))
2366 fprintf (dump_file, "(Dependence relation cannot be represented by distance vector.) \n");
2368 DDR_AFFINE_P (ddr) = false;
2373 /* This section contains the classic Banerjee tests. */
2375 /* Returns true iff CHREC_A and CHREC_B are not dependent on any index
2376 variables, i.e., if the ZIV (Zero Index Variable) test is true. */
2378 static inline bool
2379 ziv_subscript_p (tree chrec_a,
2380 tree chrec_b)
2382 return (evolution_function_is_constant_p (chrec_a)
2383 && evolution_function_is_constant_p (chrec_b));
2386 /* Returns true iff CHREC_A and CHREC_B are dependent on an index
2387 variable, i.e., if the SIV (Single Index Variable) test is true. */
2389 static bool
2390 siv_subscript_p (tree chrec_a,
2391 tree chrec_b)
2393 if ((evolution_function_is_constant_p (chrec_a)
2394 && evolution_function_is_univariate_p (chrec_b))
2395 || (evolution_function_is_constant_p (chrec_b)
2396 && evolution_function_is_univariate_p (chrec_a)))
2397 return true;
2399 if (evolution_function_is_univariate_p (chrec_a)
2400 && evolution_function_is_univariate_p (chrec_b))
2402 switch (TREE_CODE (chrec_a))
2404 case POLYNOMIAL_CHREC:
2405 switch (TREE_CODE (chrec_b))
2407 case POLYNOMIAL_CHREC:
2408 if (CHREC_VARIABLE (chrec_a) != CHREC_VARIABLE (chrec_b))
2409 return false;
2411 default:
2412 return true;
2415 default:
2416 return true;
2420 return false;
2423 /* Creates a conflict function with N dimensions. The affine functions
2424 in each dimension follow. */
2426 static conflict_function *
2427 conflict_fn (unsigned n, ...)
2429 unsigned i;
2430 conflict_function *ret = XCNEW (conflict_function);
2431 va_list ap;
2433 gcc_assert (0 < n && n <= MAX_DIM);
2434 va_start(ap, n);
2436 ret->n = n;
2437 for (i = 0; i < n; i++)
2438 ret->fns[i] = va_arg (ap, affine_fn);
2439 va_end(ap);
2441 return ret;
2444 /* Returns constant affine function with value CST. */
2446 static affine_fn
2447 affine_fn_cst (tree cst)
2449 affine_fn fn = VEC_alloc (tree, heap, 1);
2450 VEC_quick_push (tree, fn, cst);
2451 return fn;
2454 /* Returns affine function with single variable, CST + COEF * x_DIM. */
2456 static affine_fn
2457 affine_fn_univar (tree cst, unsigned dim, tree coef)
2459 affine_fn fn = VEC_alloc (tree, heap, dim + 1);
2460 unsigned i;
2462 gcc_assert (dim > 0);
2463 VEC_quick_push (tree, fn, cst);
2464 for (i = 1; i < dim; i++)
2465 VEC_quick_push (tree, fn, integer_zero_node);
2466 VEC_quick_push (tree, fn, coef);
2467 return fn;
2470 /* Analyze a ZIV (Zero Index Variable) subscript. *OVERLAPS_A and
2471 *OVERLAPS_B are initialized to the functions that describe the
2472 relation between the elements accessed twice by CHREC_A and
2473 CHREC_B. For k >= 0, the following property is verified:
2475 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
2477 static void
2478 analyze_ziv_subscript (tree chrec_a,
2479 tree chrec_b,
2480 conflict_function **overlaps_a,
2481 conflict_function **overlaps_b,
2482 tree *last_conflicts)
2484 tree difference;
2485 dependence_stats.num_ziv++;
2487 if (dump_file && (dump_flags & TDF_DETAILS))
2488 fprintf (dump_file, "(analyze_ziv_subscript \n");
2490 chrec_a = chrec_convert (integer_type_node, chrec_a, NULL_TREE);
2491 chrec_b = chrec_convert (integer_type_node, chrec_b, NULL_TREE);
2492 difference = chrec_fold_minus (integer_type_node, chrec_a, chrec_b);
2494 switch (TREE_CODE (difference))
2496 case INTEGER_CST:
2497 if (integer_zerop (difference))
2499 /* The difference is equal to zero: the accessed index
2500 overlaps for each iteration in the loop. */
2501 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
2502 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
2503 *last_conflicts = chrec_dont_know;
2504 dependence_stats.num_ziv_dependent++;
2506 else
2508 /* The accesses do not overlap. */
2509 *overlaps_a = conflict_fn_no_dependence ();
2510 *overlaps_b = conflict_fn_no_dependence ();
2511 *last_conflicts = integer_zero_node;
2512 dependence_stats.num_ziv_independent++;
2514 break;
2516 default:
2517 /* We're not sure whether the indexes overlap. For the moment,
2518 conservatively answer "don't know". */
2519 if (dump_file && (dump_flags & TDF_DETAILS))
2520 fprintf (dump_file, "ziv test failed: difference is non-integer.\n");
2522 *overlaps_a = conflict_fn_not_known ();
2523 *overlaps_b = conflict_fn_not_known ();
2524 *last_conflicts = chrec_dont_know;
2525 dependence_stats.num_ziv_unimplemented++;
2526 break;
2529 if (dump_file && (dump_flags & TDF_DETAILS))
2530 fprintf (dump_file, ")\n");
2533 /* Get the real or estimated number of iterations for LOOPNUM, whichever is
2534 available. Return the number of iterations as a tree, or NULL_TREE if
2535 we don't know. */
2537 static tree
2538 get_number_of_iters_for_loop (int loopnum)
2540 struct loop *loop = get_loop (loopnum);
2541 tree numiter = number_of_exit_cond_executions (loop);
2543 if (TREE_CODE (numiter) == INTEGER_CST)
2544 return numiter;
2546 if (loop->estimate_state == EST_AVAILABLE)
2548 tree type = lang_hooks.types.type_for_size (INT_TYPE_SIZE, true);
2549 if (double_int_fits_to_tree_p (type, loop->estimated_nb_iterations))
2550 return double_int_to_tree (type, loop->estimated_nb_iterations);
2553 return NULL_TREE;
2556 /* Analyze a SIV (Single Index Variable) subscript where CHREC_A is a
2557 constant, and CHREC_B is an affine function. *OVERLAPS_A and
2558 *OVERLAPS_B are initialized to the functions that describe the
2559 relation between the elements accessed twice by CHREC_A and
2560 CHREC_B. For k >= 0, the following property is verified:
2562 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
2564 static void
2565 analyze_siv_subscript_cst_affine (tree chrec_a,
2566 tree chrec_b,
2567 conflict_function **overlaps_a,
2568 conflict_function **overlaps_b,
2569 tree *last_conflicts)
2571 bool value0, value1, value2;
2572 tree difference, tmp;
2574 chrec_a = chrec_convert (integer_type_node, chrec_a, NULL_TREE);
2575 chrec_b = chrec_convert (integer_type_node, chrec_b, NULL_TREE);
2576 difference = chrec_fold_minus
2577 (integer_type_node, initial_condition (chrec_b), chrec_a);
2579 if (!chrec_is_positive (initial_condition (difference), &value0))
2581 if (dump_file && (dump_flags & TDF_DETAILS))
2582 fprintf (dump_file, "siv test failed: chrec is not positive.\n");
2584 dependence_stats.num_siv_unimplemented++;
2585 *overlaps_a = conflict_fn_not_known ();
2586 *overlaps_b = conflict_fn_not_known ();
2587 *last_conflicts = chrec_dont_know;
2588 return;
2590 else
2592 if (value0 == false)
2594 if (!chrec_is_positive (CHREC_RIGHT (chrec_b), &value1))
2596 if (dump_file && (dump_flags & TDF_DETAILS))
2597 fprintf (dump_file, "siv test failed: chrec not positive.\n");
2599 *overlaps_a = conflict_fn_not_known ();
2600 *overlaps_b = conflict_fn_not_known ();
2601 *last_conflicts = chrec_dont_know;
2602 dependence_stats.num_siv_unimplemented++;
2603 return;
2605 else
2607 if (value1 == true)
2609 /* Example:
2610 chrec_a = 12
2611 chrec_b = {10, +, 1}
2614 if (tree_fold_divides_p (CHREC_RIGHT (chrec_b), difference))
2616 tree numiter;
2617 int loopnum = CHREC_VARIABLE (chrec_b);
2619 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
2620 tmp = fold_build2 (EXACT_DIV_EXPR, integer_type_node,
2621 fold_build1 (ABS_EXPR,
2622 integer_type_node,
2623 difference),
2624 CHREC_RIGHT (chrec_b));
2625 *overlaps_b = conflict_fn (1, affine_fn_cst (tmp));
2626 *last_conflicts = integer_one_node;
2629 /* Perform weak-zero siv test to see if overlap is
2630 outside the loop bounds. */
2631 numiter = get_number_of_iters_for_loop (loopnum);
2633 if (numiter != NULL_TREE
2634 && TREE_CODE (tmp) == INTEGER_CST
2635 && tree_int_cst_lt (numiter, tmp))
2637 free_conflict_function (*overlaps_a);
2638 free_conflict_function (*overlaps_b);
2639 *overlaps_a = conflict_fn_no_dependence ();
2640 *overlaps_b = conflict_fn_no_dependence ();
2641 *last_conflicts = integer_zero_node;
2642 dependence_stats.num_siv_independent++;
2643 return;
2645 dependence_stats.num_siv_dependent++;
2646 return;
2649 /* When the step does not divide the difference, there are
2650 no overlaps. */
2651 else
2653 *overlaps_a = conflict_fn_no_dependence ();
2654 *overlaps_b = conflict_fn_no_dependence ();
2655 *last_conflicts = integer_zero_node;
2656 dependence_stats.num_siv_independent++;
2657 return;
2661 else
2663 /* Example:
2664 chrec_a = 12
2665 chrec_b = {10, +, -1}
2667 In this case, chrec_a will not overlap with chrec_b. */
2668 *overlaps_a = conflict_fn_no_dependence ();
2669 *overlaps_b = conflict_fn_no_dependence ();
2670 *last_conflicts = integer_zero_node;
2671 dependence_stats.num_siv_independent++;
2672 return;
2676 else
2678 if (!chrec_is_positive (CHREC_RIGHT (chrec_b), &value2))
2680 if (dump_file && (dump_flags & TDF_DETAILS))
2681 fprintf (dump_file, "siv test failed: chrec not positive.\n");
2683 *overlaps_a = conflict_fn_not_known ();
2684 *overlaps_b = conflict_fn_not_known ();
2685 *last_conflicts = chrec_dont_know;
2686 dependence_stats.num_siv_unimplemented++;
2687 return;
2689 else
2691 if (value2 == false)
2693 /* Example:
2694 chrec_a = 3
2695 chrec_b = {10, +, -1}
2697 if (tree_fold_divides_p (CHREC_RIGHT (chrec_b), difference))
2699 tree numiter;
2700 int loopnum = CHREC_VARIABLE (chrec_b);
2702 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
2703 tmp = fold_build2 (EXACT_DIV_EXPR,
2704 integer_type_node, difference,
2705 CHREC_RIGHT (chrec_b));
2706 *overlaps_b = conflict_fn (1, affine_fn_cst (tmp));
2707 *last_conflicts = integer_one_node;
2709 /* Perform weak-zero siv test to see if overlap is
2710 outside the loop bounds. */
2711 numiter = get_number_of_iters_for_loop (loopnum);
2713 if (numiter != NULL_TREE
2714 && TREE_CODE (tmp) == INTEGER_CST
2715 && tree_int_cst_lt (numiter, tmp))
2717 free_conflict_function (*overlaps_a);
2718 free_conflict_function (*overlaps_b);
2719 *overlaps_a = conflict_fn_no_dependence ();
2720 *overlaps_b = conflict_fn_no_dependence ();
2721 *last_conflicts = integer_zero_node;
2722 dependence_stats.num_siv_independent++;
2723 return;
2725 dependence_stats.num_siv_dependent++;
2726 return;
2729 /* When the step does not divide the difference, there
2730 are no overlaps. */
2731 else
2733 *overlaps_a = conflict_fn_no_dependence ();
2734 *overlaps_b = conflict_fn_no_dependence ();
2735 *last_conflicts = integer_zero_node;
2736 dependence_stats.num_siv_independent++;
2737 return;
2740 else
2742 /* Example:
2743 chrec_a = 3
2744 chrec_b = {4, +, 1}
2746 In this case, chrec_a will not overlap with chrec_b. */
2747 *overlaps_a = conflict_fn_no_dependence ();
2748 *overlaps_b = conflict_fn_no_dependence ();
2749 *last_conflicts = integer_zero_node;
2750 dependence_stats.num_siv_independent++;
2751 return;
2758 /* Helper recursive function for initializing the matrix A. Returns
2759 the initial value of CHREC. */
2761 static int
2762 initialize_matrix_A (lambda_matrix A, tree chrec, unsigned index, int mult)
2764 gcc_assert (chrec);
2766 if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
2767 return int_cst_value (chrec);
2769 A[index][0] = mult * int_cst_value (CHREC_RIGHT (chrec));
2770 return initialize_matrix_A (A, CHREC_LEFT (chrec), index + 1, mult);
2773 #define FLOOR_DIV(x,y) ((x) / (y))
2775 /* Solves the special case of the Diophantine equation:
2776 | {0, +, STEP_A}_x (OVERLAPS_A) = {0, +, STEP_B}_y (OVERLAPS_B)
2778 Computes the descriptions OVERLAPS_A and OVERLAPS_B. NITER is the
2779 number of iterations that loops X and Y run. The overlaps will be
2780 constructed as evolutions in dimension DIM. */
2782 static void
2783 compute_overlap_steps_for_affine_univar (int niter, int step_a, int step_b,
2784 affine_fn *overlaps_a,
2785 affine_fn *overlaps_b,
2786 tree *last_conflicts, int dim)
2788 if (((step_a > 0 && step_b > 0)
2789 || (step_a < 0 && step_b < 0)))
2791 int step_overlaps_a, step_overlaps_b;
2792 int gcd_steps_a_b, last_conflict, tau2;
2794 gcd_steps_a_b = gcd (step_a, step_b);
2795 step_overlaps_a = step_b / gcd_steps_a_b;
2796 step_overlaps_b = step_a / gcd_steps_a_b;
2798 tau2 = FLOOR_DIV (niter, step_overlaps_a);
2799 tau2 = MIN (tau2, FLOOR_DIV (niter, step_overlaps_b));
2800 last_conflict = tau2;
2802 *overlaps_a = affine_fn_univar (integer_zero_node, dim,
2803 build_int_cst (NULL_TREE,
2804 step_overlaps_a));
2805 *overlaps_b = affine_fn_univar (integer_zero_node, dim,
2806 build_int_cst (NULL_TREE,
2807 step_overlaps_b));
2808 *last_conflicts = build_int_cst (NULL_TREE, last_conflict);
2811 else
2813 *overlaps_a = affine_fn_cst (integer_zero_node);
2814 *overlaps_b = affine_fn_cst (integer_zero_node);
2815 *last_conflicts = integer_zero_node;
2819 /* Solves the special case of a Diophantine equation where CHREC_A is
2820 an affine bivariate function, and CHREC_B is an affine univariate
2821 function. For example,
2823 | {{0, +, 1}_x, +, 1335}_y = {0, +, 1336}_z
2825 has the following overlapping functions:
2827 | x (t, u, v) = {{0, +, 1336}_t, +, 1}_v
2828 | y (t, u, v) = {{0, +, 1336}_u, +, 1}_v
2829 | z (t, u, v) = {{{0, +, 1}_t, +, 1335}_u, +, 1}_v
2831 FORNOW: This is a specialized implementation for a case occurring in
2832 a common benchmark. Implement the general algorithm. */
2834 static void
2835 compute_overlap_steps_for_affine_1_2 (tree chrec_a, tree chrec_b,
2836 conflict_function **overlaps_a,
2837 conflict_function **overlaps_b,
2838 tree *last_conflicts)
2840 bool xz_p, yz_p, xyz_p;
2841 int step_x, step_y, step_z;
2842 int niter_x, niter_y, niter_z, niter;
2843 tree numiter_x, numiter_y, numiter_z;
2844 affine_fn overlaps_a_xz, overlaps_b_xz;
2845 affine_fn overlaps_a_yz, overlaps_b_yz;
2846 affine_fn overlaps_a_xyz, overlaps_b_xyz;
2847 affine_fn ova1, ova2, ovb;
2848 tree last_conflicts_xz, last_conflicts_yz, last_conflicts_xyz;
2850 step_x = int_cst_value (CHREC_RIGHT (CHREC_LEFT (chrec_a)));
2851 step_y = int_cst_value (CHREC_RIGHT (chrec_a));
2852 step_z = int_cst_value (CHREC_RIGHT (chrec_b));
2854 numiter_x = get_number_of_iters_for_loop (CHREC_VARIABLE (CHREC_LEFT (chrec_a)));
2855 numiter_y = get_number_of_iters_for_loop (CHREC_VARIABLE (chrec_a));
2856 numiter_z = get_number_of_iters_for_loop (CHREC_VARIABLE (chrec_b));
2858 if (numiter_x == NULL_TREE || numiter_y == NULL_TREE
2859 || numiter_z == NULL_TREE)
2861 if (dump_file && (dump_flags & TDF_DETAILS))
2862 fprintf (dump_file, "overlap steps test failed: no iteration counts.\n");
2864 *overlaps_a = conflict_fn_not_known ();
2865 *overlaps_b = conflict_fn_not_known ();
2866 *last_conflicts = chrec_dont_know;
2867 return;
2870 niter_x = int_cst_value (numiter_x);
2871 niter_y = int_cst_value (numiter_y);
2872 niter_z = int_cst_value (numiter_z);
2874 niter = MIN (niter_x, niter_z);
2875 compute_overlap_steps_for_affine_univar (niter, step_x, step_z,
2876 &overlaps_a_xz,
2877 &overlaps_b_xz,
2878 &last_conflicts_xz, 1);
2879 niter = MIN (niter_y, niter_z);
2880 compute_overlap_steps_for_affine_univar (niter, step_y, step_z,
2881 &overlaps_a_yz,
2882 &overlaps_b_yz,
2883 &last_conflicts_yz, 2);
2884 niter = MIN (niter_x, niter_z);
2885 niter = MIN (niter_y, niter);
2886 compute_overlap_steps_for_affine_univar (niter, step_x + step_y, step_z,
2887 &overlaps_a_xyz,
2888 &overlaps_b_xyz,
2889 &last_conflicts_xyz, 3);
2891 xz_p = !integer_zerop (last_conflicts_xz);
2892 yz_p = !integer_zerop (last_conflicts_yz);
2893 xyz_p = !integer_zerop (last_conflicts_xyz);
2895 if (xz_p || yz_p || xyz_p)
2897 ova1 = affine_fn_cst (integer_zero_node);
2898 ova2 = affine_fn_cst (integer_zero_node);
2899 ovb = affine_fn_cst (integer_zero_node);
2900 if (xz_p)
2902 affine_fn t0 = ova1;
2903 affine_fn t2 = ovb;
2905 ova1 = affine_fn_plus (ova1, overlaps_a_xz);
2906 ovb = affine_fn_plus (ovb, overlaps_b_xz);
2907 affine_fn_free (t0);
2908 affine_fn_free (t2);
2909 *last_conflicts = last_conflicts_xz;
2911 if (yz_p)
2913 affine_fn t0 = ova2;
2914 affine_fn t2 = ovb;
2916 ova2 = affine_fn_plus (ova2, overlaps_a_yz);
2917 ovb = affine_fn_plus (ovb, overlaps_b_yz);
2918 affine_fn_free (t0);
2919 affine_fn_free (t2);
2920 *last_conflicts = last_conflicts_yz;
2922 if (xyz_p)
2924 affine_fn t0 = ova1;
2925 affine_fn t2 = ova2;
2926 affine_fn t4 = ovb;
2928 ova1 = affine_fn_plus (ova1, overlaps_a_xyz);
2929 ova2 = affine_fn_plus (ova2, overlaps_a_xyz);
2930 ovb = affine_fn_plus (ovb, overlaps_b_xyz);
2931 affine_fn_free (t0);
2932 affine_fn_free (t2);
2933 affine_fn_free (t4);
2934 *last_conflicts = last_conflicts_xyz;
2936 *overlaps_a = conflict_fn (2, ova1, ova2);
2937 *overlaps_b = conflict_fn (1, ovb);
2939 else
2941 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
2942 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
2943 *last_conflicts = integer_zero_node;
2946 affine_fn_free (overlaps_a_xz);
2947 affine_fn_free (overlaps_b_xz);
2948 affine_fn_free (overlaps_a_yz);
2949 affine_fn_free (overlaps_b_yz);
2950 affine_fn_free (overlaps_a_xyz);
2951 affine_fn_free (overlaps_b_xyz);
2954 /* Determines the overlapping elements due to accesses CHREC_A and
2955 CHREC_B, that are affine functions. This function cannot handle
2956 symbolic evolution functions, ie. when initial conditions are
2957 parameters, because it uses lambda matrices of integers. */
2959 static void
2960 analyze_subscript_affine_affine (tree chrec_a,
2961 tree chrec_b,
2962 conflict_function **overlaps_a,
2963 conflict_function **overlaps_b,
2964 tree *last_conflicts)
2966 unsigned nb_vars_a, nb_vars_b, dim;
2967 int init_a, init_b, gamma, gcd_alpha_beta;
2968 int tau1, tau2;
2969 lambda_matrix A, U, S;
2971 if (eq_evolutions_p (chrec_a, chrec_b))
2973 /* The accessed index overlaps for each iteration in the
2974 loop. */
2975 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
2976 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
2977 *last_conflicts = chrec_dont_know;
2978 return;
2980 if (dump_file && (dump_flags & TDF_DETAILS))
2981 fprintf (dump_file, "(analyze_subscript_affine_affine \n");
2983 /* For determining the initial intersection, we have to solve a
2984 Diophantine equation. This is the most time consuming part.
2986 For answering to the question: "Is there a dependence?" we have
2987 to prove that there exists a solution to the Diophantine
2988 equation, and that the solution is in the iteration domain,
2989 i.e. the solution is positive or zero, and that the solution
2990 happens before the upper bound loop.nb_iterations. Otherwise
2991 there is no dependence. This function outputs a description of
2992 the iterations that hold the intersections. */
2994 nb_vars_a = nb_vars_in_chrec (chrec_a);
2995 nb_vars_b = nb_vars_in_chrec (chrec_b);
2997 dim = nb_vars_a + nb_vars_b;
2998 U = lambda_matrix_new (dim, dim);
2999 A = lambda_matrix_new (dim, 1);
3000 S = lambda_matrix_new (dim, 1);
3002 init_a = initialize_matrix_A (A, chrec_a, 0, 1);
3003 init_b = initialize_matrix_A (A, chrec_b, nb_vars_a, -1);
3004 gamma = init_b - init_a;
3006 /* Don't do all the hard work of solving the Diophantine equation
3007 when we already know the solution: for example,
3008 | {3, +, 1}_1
3009 | {3, +, 4}_2
3010 | gamma = 3 - 3 = 0.
3011 Then the first overlap occurs during the first iterations:
3012 | {3, +, 1}_1 ({0, +, 4}_x) = {3, +, 4}_2 ({0, +, 1}_x)
3014 if (gamma == 0)
3016 if (nb_vars_a == 1 && nb_vars_b == 1)
3018 int step_a, step_b;
3019 int niter, niter_a, niter_b;
3020 tree numiter_a, numiter_b;
3021 affine_fn ova, ovb;
3023 numiter_a = get_number_of_iters_for_loop (CHREC_VARIABLE (chrec_a));
3024 numiter_b = get_number_of_iters_for_loop (CHREC_VARIABLE (chrec_b));
3025 if (numiter_a == NULL_TREE || numiter_b == NULL_TREE)
3027 if (dump_file && (dump_flags & TDF_DETAILS))
3028 fprintf (dump_file, "affine-affine test failed: missing iteration counts.\n");
3029 *overlaps_a = conflict_fn_not_known ();
3030 *overlaps_b = conflict_fn_not_known ();
3031 *last_conflicts = chrec_dont_know;
3032 goto end_analyze_subs_aa;
3035 niter_a = int_cst_value (numiter_a);
3036 niter_b = int_cst_value (numiter_b);
3037 niter = MIN (niter_a, niter_b);
3039 step_a = int_cst_value (CHREC_RIGHT (chrec_a));
3040 step_b = int_cst_value (CHREC_RIGHT (chrec_b));
3042 compute_overlap_steps_for_affine_univar (niter, step_a, step_b,
3043 &ova, &ovb,
3044 last_conflicts, 1);
3045 *overlaps_a = conflict_fn (1, ova);
3046 *overlaps_b = conflict_fn (1, ovb);
3049 else if (nb_vars_a == 2 && nb_vars_b == 1)
3050 compute_overlap_steps_for_affine_1_2
3051 (chrec_a, chrec_b, overlaps_a, overlaps_b, last_conflicts);
3053 else if (nb_vars_a == 1 && nb_vars_b == 2)
3054 compute_overlap_steps_for_affine_1_2
3055 (chrec_b, chrec_a, overlaps_b, overlaps_a, last_conflicts);
3057 else
3059 if (dump_file && (dump_flags & TDF_DETAILS))
3060 fprintf (dump_file, "affine-affine test failed: too many variables.\n");
3061 *overlaps_a = conflict_fn_not_known ();
3062 *overlaps_b = conflict_fn_not_known ();
3063 *last_conflicts = chrec_dont_know;
3065 goto end_analyze_subs_aa;
3068 /* U.A = S */
3069 lambda_matrix_right_hermite (A, dim, 1, S, U);
3071 if (S[0][0] < 0)
3073 S[0][0] *= -1;
3074 lambda_matrix_row_negate (U, dim, 0);
3076 gcd_alpha_beta = S[0][0];
3078 /* Something went wrong: for example in {1, +, 0}_5 vs. {0, +, 0}_5,
3079 but that is a quite strange case. Instead of ICEing, answer
3080 don't know. */
3081 if (gcd_alpha_beta == 0)
3083 *overlaps_a = conflict_fn_not_known ();
3084 *overlaps_b = conflict_fn_not_known ();
3085 *last_conflicts = chrec_dont_know;
3086 goto end_analyze_subs_aa;
3089 /* The classic "gcd-test". */
3090 if (!int_divides_p (gcd_alpha_beta, gamma))
3092 /* The "gcd-test" has determined that there is no integer
3093 solution, i.e. there is no dependence. */
3094 *overlaps_a = conflict_fn_no_dependence ();
3095 *overlaps_b = conflict_fn_no_dependence ();
3096 *last_conflicts = integer_zero_node;
3099 /* Both access functions are univariate. This includes SIV and MIV cases. */
3100 else if (nb_vars_a == 1 && nb_vars_b == 1)
3102 /* Both functions should have the same evolution sign. */
3103 if (((A[0][0] > 0 && -A[1][0] > 0)
3104 || (A[0][0] < 0 && -A[1][0] < 0)))
3106 /* The solutions are given by:
3108 | [GAMMA/GCD_ALPHA_BETA t].[u11 u12] = [x0]
3109 | [u21 u22] [y0]
3111 For a given integer t. Using the following variables,
3113 | i0 = u11 * gamma / gcd_alpha_beta
3114 | j0 = u12 * gamma / gcd_alpha_beta
3115 | i1 = u21
3116 | j1 = u22
3118 the solutions are:
3120 | x0 = i0 + i1 * t,
3121 | y0 = j0 + j1 * t. */
3123 int i0, j0, i1, j1;
3125 /* X0 and Y0 are the first iterations for which there is a
3126 dependence. X0, Y0 are two solutions of the Diophantine
3127 equation: chrec_a (X0) = chrec_b (Y0). */
3128 int x0, y0;
3129 int niter, niter_a, niter_b;
3130 tree numiter_a, numiter_b;
3132 numiter_a = get_number_of_iters_for_loop (CHREC_VARIABLE (chrec_a));
3133 numiter_b = get_number_of_iters_for_loop (CHREC_VARIABLE (chrec_b));
3135 if (numiter_a == NULL_TREE || numiter_b == NULL_TREE)
3137 if (dump_file && (dump_flags & TDF_DETAILS))
3138 fprintf (dump_file, "affine-affine test failed: missing iteration counts.\n");
3139 *overlaps_a = conflict_fn_not_known ();
3140 *overlaps_b = conflict_fn_not_known ();
3141 *last_conflicts = chrec_dont_know;
3142 goto end_analyze_subs_aa;
3145 niter_a = int_cst_value (numiter_a);
3146 niter_b = int_cst_value (numiter_b);
3147 niter = MIN (niter_a, niter_b);
3149 i0 = U[0][0] * gamma / gcd_alpha_beta;
3150 j0 = U[0][1] * gamma / gcd_alpha_beta;
3151 i1 = U[1][0];
3152 j1 = U[1][1];
3154 if ((i1 == 0 && i0 < 0)
3155 || (j1 == 0 && j0 < 0))
3157 /* There is no solution.
3158 FIXME: The case "i0 > nb_iterations, j0 > nb_iterations"
3159 falls in here, but for the moment we don't look at the
3160 upper bound of the iteration domain. */
3161 *overlaps_a = conflict_fn_no_dependence ();
3162 *overlaps_b = conflict_fn_no_dependence ();
3163 *last_conflicts = integer_zero_node;
3166 else
3168 if (i1 > 0)
3170 tau1 = CEIL (-i0, i1);
3171 tau2 = FLOOR_DIV (niter - i0, i1);
3173 if (j1 > 0)
3175 int last_conflict, min_multiple;
3176 tau1 = MAX (tau1, CEIL (-j0, j1));
3177 tau2 = MIN (tau2, FLOOR_DIV (niter - j0, j1));
3179 x0 = i1 * tau1 + i0;
3180 y0 = j1 * tau1 + j0;
3182 /* At this point (x0, y0) is one of the
3183 solutions to the Diophantine equation. The
3184 next step has to compute the smallest
3185 positive solution: the first conflicts. */
3186 min_multiple = MIN (x0 / i1, y0 / j1);
3187 x0 -= i1 * min_multiple;
3188 y0 -= j1 * min_multiple;
3190 tau1 = (x0 - i0)/i1;
3191 last_conflict = tau2 - tau1;
3193 /* If the overlap occurs outside of the bounds of the
3194 loop, there is no dependence. */
3195 if (x0 > niter || y0 > niter)
3197 *overlaps_a = conflict_fn_no_dependence ();
3198 *overlaps_b = conflict_fn_no_dependence ();
3199 *last_conflicts = integer_zero_node;
3201 else
3203 *overlaps_a
3204 = conflict_fn (1,
3205 affine_fn_univar (build_int_cst (NULL_TREE, x0),
3207 build_int_cst (NULL_TREE, i1)));
3208 *overlaps_b
3209 = conflict_fn (1,
3210 affine_fn_univar (build_int_cst (NULL_TREE, y0),
3212 build_int_cst (NULL_TREE, j1)));
3213 *last_conflicts = build_int_cst (NULL_TREE, last_conflict);
3216 else
3218 /* FIXME: For the moment, the upper bound of the
3219 iteration domain for j is not checked. */
3220 if (dump_file && (dump_flags & TDF_DETAILS))
3221 fprintf (dump_file, "affine-affine test failed: unimplemented.\n");
3222 *overlaps_a = conflict_fn_not_known ();
3223 *overlaps_b = conflict_fn_not_known ();
3224 *last_conflicts = chrec_dont_know;
3228 else
3230 /* FIXME: For the moment, the upper bound of the
3231 iteration domain for i is not checked. */
3232 if (dump_file && (dump_flags & TDF_DETAILS))
3233 fprintf (dump_file, "affine-affine test failed: unimplemented.\n");
3234 *overlaps_a = conflict_fn_not_known ();
3235 *overlaps_b = conflict_fn_not_known ();
3236 *last_conflicts = chrec_dont_know;
3240 else
3242 if (dump_file && (dump_flags & TDF_DETAILS))
3243 fprintf (dump_file, "affine-affine test failed: unimplemented.\n");
3244 *overlaps_a = conflict_fn_not_known ();
3245 *overlaps_b = conflict_fn_not_known ();
3246 *last_conflicts = chrec_dont_know;
3250 else
3252 if (dump_file && (dump_flags & TDF_DETAILS))
3253 fprintf (dump_file, "affine-affine test failed: unimplemented.\n");
3254 *overlaps_a = conflict_fn_not_known ();
3255 *overlaps_b = conflict_fn_not_known ();
3256 *last_conflicts = chrec_dont_know;
3259 end_analyze_subs_aa:
3260 if (dump_file && (dump_flags & TDF_DETAILS))
3262 fprintf (dump_file, " (overlaps_a = ");
3263 dump_conflict_function (dump_file, *overlaps_a);
3264 fprintf (dump_file, ")\n (overlaps_b = ");
3265 dump_conflict_function (dump_file, *overlaps_b);
3266 fprintf (dump_file, ")\n");
3267 fprintf (dump_file, ")\n");
3271 /* Returns true when analyze_subscript_affine_affine can be used for
3272 determining the dependence relation between chrec_a and chrec_b,
3273 that contain symbols. This function modifies chrec_a and chrec_b
3274 such that the analysis result is the same, and such that they don't
3275 contain symbols, and then can safely be passed to the analyzer.
3277 Example: The analysis of the following tuples of evolutions produce
3278 the same results: {x+1, +, 1}_1 vs. {x+3, +, 1}_1, and {-2, +, 1}_1
3279 vs. {0, +, 1}_1
3281 {x+1, +, 1}_1 ({2, +, 1}_1) = {x+3, +, 1}_1 ({0, +, 1}_1)
3282 {-2, +, 1}_1 ({2, +, 1}_1) = {0, +, 1}_1 ({0, +, 1}_1)
3285 static bool
3286 can_use_analyze_subscript_affine_affine (tree *chrec_a, tree *chrec_b)
3288 tree diff, type, left_a, left_b, right_b;
3290 if (chrec_contains_symbols (CHREC_RIGHT (*chrec_a))
3291 || chrec_contains_symbols (CHREC_RIGHT (*chrec_b)))
3292 /* FIXME: For the moment not handled. Might be refined later. */
3293 return false;
3295 type = chrec_type (*chrec_a);
3296 left_a = CHREC_LEFT (*chrec_a);
3297 left_b = chrec_convert (type, CHREC_LEFT (*chrec_b), NULL_TREE);
3298 diff = chrec_fold_minus (type, left_a, left_b);
3300 if (!evolution_function_is_constant_p (diff))
3301 return false;
3303 if (dump_file && (dump_flags & TDF_DETAILS))
3304 fprintf (dump_file, "can_use_subscript_aff_aff_for_symbolic \n");
3306 *chrec_a = build_polynomial_chrec (CHREC_VARIABLE (*chrec_a),
3307 diff, CHREC_RIGHT (*chrec_a));
3308 right_b = chrec_convert (type, CHREC_RIGHT (*chrec_b), NULL_TREE);
3309 *chrec_b = build_polynomial_chrec (CHREC_VARIABLE (*chrec_b),
3310 build_int_cst (type, 0),
3311 right_b);
3312 return true;
3315 /* Analyze a SIV (Single Index Variable) subscript. *OVERLAPS_A and
3316 *OVERLAPS_B are initialized to the functions that describe the
3317 relation between the elements accessed twice by CHREC_A and
3318 CHREC_B. For k >= 0, the following property is verified:
3320 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
3322 static void
3323 analyze_siv_subscript (tree chrec_a,
3324 tree chrec_b,
3325 conflict_function **overlaps_a,
3326 conflict_function **overlaps_b,
3327 tree *last_conflicts)
3329 dependence_stats.num_siv++;
3331 if (dump_file && (dump_flags & TDF_DETAILS))
3332 fprintf (dump_file, "(analyze_siv_subscript \n");
3334 if (evolution_function_is_constant_p (chrec_a)
3335 && evolution_function_is_affine_p (chrec_b))
3336 analyze_siv_subscript_cst_affine (chrec_a, chrec_b,
3337 overlaps_a, overlaps_b, last_conflicts);
3339 else if (evolution_function_is_affine_p (chrec_a)
3340 && evolution_function_is_constant_p (chrec_b))
3341 analyze_siv_subscript_cst_affine (chrec_b, chrec_a,
3342 overlaps_b, overlaps_a, last_conflicts);
3344 else if (evolution_function_is_affine_p (chrec_a)
3345 && evolution_function_is_affine_p (chrec_b))
3347 if (!chrec_contains_symbols (chrec_a)
3348 && !chrec_contains_symbols (chrec_b))
3350 analyze_subscript_affine_affine (chrec_a, chrec_b,
3351 overlaps_a, overlaps_b,
3352 last_conflicts);
3354 if (CF_NOT_KNOWN_P (*overlaps_a)
3355 || CF_NOT_KNOWN_P (*overlaps_b))
3356 dependence_stats.num_siv_unimplemented++;
3357 else if (CF_NO_DEPENDENCE_P (*overlaps_a)
3358 || CF_NO_DEPENDENCE_P (*overlaps_b))
3359 dependence_stats.num_siv_independent++;
3360 else
3361 dependence_stats.num_siv_dependent++;
3363 else if (can_use_analyze_subscript_affine_affine (&chrec_a,
3364 &chrec_b))
3366 analyze_subscript_affine_affine (chrec_a, chrec_b,
3367 overlaps_a, overlaps_b,
3368 last_conflicts);
3369 /* FIXME: The number of iterations is a symbolic expression.
3370 Compute it properly. */
3371 *last_conflicts = chrec_dont_know;
3373 if (CF_NOT_KNOWN_P (*overlaps_a)
3374 || CF_NOT_KNOWN_P (*overlaps_b))
3375 dependence_stats.num_siv_unimplemented++;
3376 else if (CF_NO_DEPENDENCE_P (*overlaps_a)
3377 || CF_NO_DEPENDENCE_P (*overlaps_b))
3378 dependence_stats.num_siv_independent++;
3379 else
3380 dependence_stats.num_siv_dependent++;
3382 else
3383 goto siv_subscript_dontknow;
3386 else
3388 siv_subscript_dontknow:;
3389 if (dump_file && (dump_flags & TDF_DETAILS))
3390 fprintf (dump_file, "siv test failed: unimplemented.\n");
3391 *overlaps_a = conflict_fn_not_known ();
3392 *overlaps_b = conflict_fn_not_known ();
3393 *last_conflicts = chrec_dont_know;
3394 dependence_stats.num_siv_unimplemented++;
3397 if (dump_file && (dump_flags & TDF_DETAILS))
3398 fprintf (dump_file, ")\n");
3401 /* Return true when the property can be computed. RES should contain
3402 true when calling the first time this function, then it is set to
3403 false when one of the evolution steps of an affine CHREC does not
3404 divide the constant CST. */
3406 static bool
3407 chrec_steps_divide_constant_p (tree chrec,
3408 tree cst,
3409 bool *res)
3411 switch (TREE_CODE (chrec))
3413 case POLYNOMIAL_CHREC:
3414 if (evolution_function_is_constant_p (CHREC_RIGHT (chrec)))
3416 if (tree_fold_divides_p (CHREC_RIGHT (chrec), cst))
3417 /* Keep RES to true, and iterate on other dimensions. */
3418 return chrec_steps_divide_constant_p (CHREC_LEFT (chrec), cst, res);
3420 *res = false;
3421 return true;
3423 else
3424 /* When the step is a parameter the result is undetermined. */
3425 return false;
3427 default:
3428 /* On the initial condition, return true. */
3429 return true;
3433 /* Analyze a MIV (Multiple Index Variable) subscript. *OVERLAPS_A and
3434 *OVERLAPS_B are initialized to the functions that describe the
3435 relation between the elements accessed twice by CHREC_A and
3436 CHREC_B. For k >= 0, the following property is verified:
3438 CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)). */
3440 static void
3441 analyze_miv_subscript (tree chrec_a,
3442 tree chrec_b,
3443 conflict_function **overlaps_a,
3444 conflict_function **overlaps_b,
3445 tree *last_conflicts)
3447 /* FIXME: This is a MIV subscript, not yet handled.
3448 Example: (A[{1, +, 1}_1] vs. A[{1, +, 1}_2]) that comes from
3449 (A[i] vs. A[j]).
3451 In the SIV test we had to solve a Diophantine equation with two
3452 variables. In the MIV case we have to solve a Diophantine
3453 equation with 2*n variables (if the subscript uses n IVs).
3455 bool divide_p = true;
3456 tree difference;
3457 dependence_stats.num_miv++;
3458 if (dump_file && (dump_flags & TDF_DETAILS))
3459 fprintf (dump_file, "(analyze_miv_subscript \n");
3461 chrec_a = chrec_convert (integer_type_node, chrec_a, NULL_TREE);
3462 chrec_b = chrec_convert (integer_type_node, chrec_b, NULL_TREE);
3463 difference = chrec_fold_minus (integer_type_node, chrec_a, chrec_b);
3465 if (eq_evolutions_p (chrec_a, chrec_b))
3467 /* Access functions are the same: all the elements are accessed
3468 in the same order. */
3469 *overlaps_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
3470 *overlaps_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
3471 *last_conflicts = get_number_of_iters_for_loop (CHREC_VARIABLE (chrec_a));
3472 dependence_stats.num_miv_dependent++;
3475 else if (evolution_function_is_constant_p (difference)
3476 /* For the moment, the following is verified:
3477 evolution_function_is_affine_multivariate_p (chrec_a) */
3478 && chrec_steps_divide_constant_p (chrec_a, difference, &divide_p)
3479 && !divide_p)
3481 /* testsuite/.../ssa-chrec-33.c
3482 {{21, +, 2}_1, +, -2}_2 vs. {{20, +, 2}_1, +, -2}_2
3484 The difference is 1, and the evolution steps are equal to 2,
3485 consequently there are no overlapping elements. */
3486 *overlaps_a = conflict_fn_no_dependence ();
3487 *overlaps_b = conflict_fn_no_dependence ();
3488 *last_conflicts = integer_zero_node;
3489 dependence_stats.num_miv_independent++;
3492 else if (evolution_function_is_affine_multivariate_p (chrec_a)
3493 && !chrec_contains_symbols (chrec_a)
3494 && evolution_function_is_affine_multivariate_p (chrec_b)
3495 && !chrec_contains_symbols (chrec_b))
3497 /* testsuite/.../ssa-chrec-35.c
3498 {0, +, 1}_2 vs. {0, +, 1}_3
3499 the overlapping elements are respectively located at iterations:
3500 {0, +, 1}_x and {0, +, 1}_x,
3501 in other words, we have the equality:
3502 {0, +, 1}_2 ({0, +, 1}_x) = {0, +, 1}_3 ({0, +, 1}_x)
3504 Other examples:
3505 {{0, +, 1}_1, +, 2}_2 ({0, +, 1}_x, {0, +, 1}_y) =
3506 {0, +, 1}_1 ({{0, +, 1}_x, +, 2}_y)
3508 {{0, +, 2}_1, +, 3}_2 ({0, +, 1}_y, {0, +, 1}_x) =
3509 {{0, +, 3}_1, +, 2}_2 ({0, +, 1}_x, {0, +, 1}_y)
3511 analyze_subscript_affine_affine (chrec_a, chrec_b,
3512 overlaps_a, overlaps_b, last_conflicts);
3514 if (CF_NOT_KNOWN_P (*overlaps_a)
3515 || CF_NOT_KNOWN_P (*overlaps_b))
3516 dependence_stats.num_miv_unimplemented++;
3517 else if (CF_NO_DEPENDENCE_P (*overlaps_a)
3518 || CF_NO_DEPENDENCE_P (*overlaps_b))
3519 dependence_stats.num_miv_independent++;
3520 else
3521 dependence_stats.num_miv_dependent++;
3524 else
3526 /* When the analysis is too difficult, answer "don't know". */
3527 if (dump_file && (dump_flags & TDF_DETAILS))
3528 fprintf (dump_file, "analyze_miv_subscript test failed: unimplemented.\n");
3530 *overlaps_a = conflict_fn_not_known ();
3531 *overlaps_b = conflict_fn_not_known ();
3532 *last_conflicts = chrec_dont_know;
3533 dependence_stats.num_miv_unimplemented++;
3536 if (dump_file && (dump_flags & TDF_DETAILS))
3537 fprintf (dump_file, ")\n");
3540 /* Determines the iterations for which CHREC_A is equal to CHREC_B.
3541 OVERLAP_ITERATIONS_A and OVERLAP_ITERATIONS_B are initialized with
3542 two functions that describe the iterations that contain conflicting
3543 elements.
3545 Remark: For an integer k >= 0, the following equality is true:
3547 CHREC_A (OVERLAP_ITERATIONS_A (k)) == CHREC_B (OVERLAP_ITERATIONS_B (k)).
3550 static void
3551 analyze_overlapping_iterations (tree chrec_a,
3552 tree chrec_b,
3553 conflict_function **overlap_iterations_a,
3554 conflict_function **overlap_iterations_b,
3555 tree *last_conflicts)
3557 dependence_stats.num_subscript_tests++;
3559 if (dump_file && (dump_flags & TDF_DETAILS))
3561 fprintf (dump_file, "(analyze_overlapping_iterations \n");
3562 fprintf (dump_file, " (chrec_a = ");
3563 print_generic_expr (dump_file, chrec_a, 0);
3564 fprintf (dump_file, ")\n (chrec_b = ");
3565 print_generic_expr (dump_file, chrec_b, 0);
3566 fprintf (dump_file, ")\n");
3569 if (chrec_a == NULL_TREE
3570 || chrec_b == NULL_TREE
3571 || chrec_contains_undetermined (chrec_a)
3572 || chrec_contains_undetermined (chrec_b))
3574 dependence_stats.num_subscript_undetermined++;
3576 *overlap_iterations_a = conflict_fn_not_known ();
3577 *overlap_iterations_b = conflict_fn_not_known ();
3580 /* If they are the same chrec, and are affine, they overlap
3581 on every iteration. */
3582 else if (eq_evolutions_p (chrec_a, chrec_b)
3583 && evolution_function_is_affine_multivariate_p (chrec_a))
3585 dependence_stats.num_same_subscript_function++;
3586 *overlap_iterations_a = conflict_fn (1, affine_fn_cst (integer_zero_node));
3587 *overlap_iterations_b = conflict_fn (1, affine_fn_cst (integer_zero_node));
3588 *last_conflicts = chrec_dont_know;
3591 /* If they aren't the same, and aren't affine, we can't do anything
3592 yet. */
3593 else if ((chrec_contains_symbols (chrec_a)
3594 || chrec_contains_symbols (chrec_b))
3595 && (!evolution_function_is_affine_multivariate_p (chrec_a)
3596 || !evolution_function_is_affine_multivariate_p (chrec_b)))
3598 dependence_stats.num_subscript_undetermined++;
3599 *overlap_iterations_a = conflict_fn_not_known ();
3600 *overlap_iterations_b = conflict_fn_not_known ();
3603 else if (ziv_subscript_p (chrec_a, chrec_b))
3604 analyze_ziv_subscript (chrec_a, chrec_b,
3605 overlap_iterations_a, overlap_iterations_b,
3606 last_conflicts);
3608 else if (siv_subscript_p (chrec_a, chrec_b))
3609 analyze_siv_subscript (chrec_a, chrec_b,
3610 overlap_iterations_a, overlap_iterations_b,
3611 last_conflicts);
3613 else
3614 analyze_miv_subscript (chrec_a, chrec_b,
3615 overlap_iterations_a, overlap_iterations_b,
3616 last_conflicts);
3618 if (dump_file && (dump_flags & TDF_DETAILS))
3620 fprintf (dump_file, " (overlap_iterations_a = ");
3621 dump_conflict_function (dump_file, *overlap_iterations_a);
3622 fprintf (dump_file, ")\n (overlap_iterations_b = ");
3623 dump_conflict_function (dump_file, *overlap_iterations_b);
3624 fprintf (dump_file, ")\n");
3625 fprintf (dump_file, ")\n");
3629 /* Helper function for uniquely inserting distance vectors. */
3631 static void
3632 save_dist_v (struct data_dependence_relation *ddr, lambda_vector dist_v)
3634 unsigned i;
3635 lambda_vector v;
3637 for (i = 0; VEC_iterate (lambda_vector, DDR_DIST_VECTS (ddr), i, v); i++)
3638 if (lambda_vector_equal (v, dist_v, DDR_NB_LOOPS (ddr)))
3639 return;
3641 VEC_safe_push (lambda_vector, heap, DDR_DIST_VECTS (ddr), dist_v);
3644 /* Helper function for uniquely inserting direction vectors. */
3646 static void
3647 save_dir_v (struct data_dependence_relation *ddr, lambda_vector dir_v)
3649 unsigned i;
3650 lambda_vector v;
3652 for (i = 0; VEC_iterate (lambda_vector, DDR_DIR_VECTS (ddr), i, v); i++)
3653 if (lambda_vector_equal (v, dir_v, DDR_NB_LOOPS (ddr)))
3654 return;
3656 VEC_safe_push (lambda_vector, heap, DDR_DIR_VECTS (ddr), dir_v);
3659 /* Add a distance of 1 on all the loops outer than INDEX. If we
3660 haven't yet determined a distance for this outer loop, push a new
3661 distance vector composed of the previous distance, and a distance
3662 of 1 for this outer loop. Example:
3664 | loop_1
3665 | loop_2
3666 | A[10]
3667 | endloop_2
3668 | endloop_1
3670 Saved vectors are of the form (dist_in_1, dist_in_2). First, we
3671 save (0, 1), then we have to save (1, 0). */
3673 static void
3674 add_outer_distances (struct data_dependence_relation *ddr,
3675 lambda_vector dist_v, int index)
3677 /* For each outer loop where init_v is not set, the accesses are
3678 in dependence of distance 1 in the loop. */
3679 while (--index >= 0)
3681 lambda_vector save_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3682 lambda_vector_copy (dist_v, save_v, DDR_NB_LOOPS (ddr));
3683 save_v[index] = 1;
3684 save_dist_v (ddr, save_v);
3688 /* Return false when fail to represent the data dependence as a
3689 distance vector. INIT_B is set to true when a component has been
3690 added to the distance vector DIST_V. INDEX_CARRY is then set to
3691 the index in DIST_V that carries the dependence. */
3693 static bool
3694 build_classic_dist_vector_1 (struct data_dependence_relation *ddr,
3695 struct data_reference *ddr_a,
3696 struct data_reference *ddr_b,
3697 lambda_vector dist_v, bool *init_b,
3698 int *index_carry)
3700 unsigned i;
3701 lambda_vector init_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3703 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3705 tree access_fn_a, access_fn_b;
3706 struct subscript *subscript = DDR_SUBSCRIPT (ddr, i);
3708 if (chrec_contains_undetermined (SUB_DISTANCE (subscript)))
3710 non_affine_dependence_relation (ddr);
3711 return false;
3714 access_fn_a = DR_ACCESS_FN (ddr_a, i);
3715 access_fn_b = DR_ACCESS_FN (ddr_b, i);
3717 if (TREE_CODE (access_fn_a) == POLYNOMIAL_CHREC
3718 && TREE_CODE (access_fn_b) == POLYNOMIAL_CHREC)
3720 int dist, index;
3721 int index_a = index_in_loop_nest (CHREC_VARIABLE (access_fn_a),
3722 DDR_LOOP_NEST (ddr));
3723 int index_b = index_in_loop_nest (CHREC_VARIABLE (access_fn_b),
3724 DDR_LOOP_NEST (ddr));
3726 /* The dependence is carried by the outermost loop. Example:
3727 | loop_1
3728 | A[{4, +, 1}_1]
3729 | loop_2
3730 | A[{5, +, 1}_2]
3731 | endloop_2
3732 | endloop_1
3733 In this case, the dependence is carried by loop_1. */
3734 index = index_a < index_b ? index_a : index_b;
3735 *index_carry = MIN (index, *index_carry);
3737 if (chrec_contains_undetermined (SUB_DISTANCE (subscript)))
3739 non_affine_dependence_relation (ddr);
3740 return false;
3743 dist = int_cst_value (SUB_DISTANCE (subscript));
3745 /* This is the subscript coupling test. If we have already
3746 recorded a distance for this loop (a distance coming from
3747 another subscript), it should be the same. For example,
3748 in the following code, there is no dependence:
3750 | loop i = 0, N, 1
3751 | T[i+1][i] = ...
3752 | ... = T[i][i]
3753 | endloop
3755 if (init_v[index] != 0 && dist_v[index] != dist)
3757 finalize_ddr_dependent (ddr, chrec_known);
3758 return false;
3761 dist_v[index] = dist;
3762 init_v[index] = 1;
3763 *init_b = true;
3765 else
3767 /* This can be for example an affine vs. constant dependence
3768 (T[i] vs. T[3]) that is not an affine dependence and is
3769 not representable as a distance vector. */
3770 non_affine_dependence_relation (ddr);
3771 return false;
3775 return true;
3778 /* Return true when the DDR contains two data references that have the
3779 same access functions. */
3781 static bool
3782 same_access_functions (struct data_dependence_relation *ddr)
3784 unsigned i;
3786 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3787 if (!eq_evolutions_p (DR_ACCESS_FN (DDR_A (ddr), i),
3788 DR_ACCESS_FN (DDR_B (ddr), i)))
3789 return false;
3791 return true;
3794 /* Helper function for the case where DDR_A and DDR_B are the same
3795 multivariate access function. */
3797 static void
3798 add_multivariate_self_dist (struct data_dependence_relation *ddr, tree c_2)
3800 int x_1, x_2;
3801 tree c_1 = CHREC_LEFT (c_2);
3802 tree c_0 = CHREC_LEFT (c_1);
3803 lambda_vector dist_v;
3805 /* Polynomials with more than 2 variables are not handled yet. */
3806 if (TREE_CODE (c_0) != INTEGER_CST)
3808 DDR_ARE_DEPENDENT (ddr) = chrec_dont_know;
3809 return;
3812 x_2 = index_in_loop_nest (CHREC_VARIABLE (c_2), DDR_LOOP_NEST (ddr));
3813 x_1 = index_in_loop_nest (CHREC_VARIABLE (c_1), DDR_LOOP_NEST (ddr));
3815 /* For "{{0, +, 2}_1, +, 3}_2" the distance vector is (3, -2). */
3816 dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3817 dist_v[x_1] = int_cst_value (CHREC_RIGHT (c_2));
3818 dist_v[x_2] = -int_cst_value (CHREC_RIGHT (c_1));
3819 save_dist_v (ddr, dist_v);
3821 add_outer_distances (ddr, dist_v, x_1);
3824 /* Helper function for the case where DDR_A and DDR_B are the same
3825 access functions. */
3827 static void
3828 add_other_self_distances (struct data_dependence_relation *ddr)
3830 lambda_vector dist_v;
3831 unsigned i;
3832 int index_carry = DDR_NB_LOOPS (ddr);
3834 for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3836 tree access_fun = DR_ACCESS_FN (DDR_A (ddr), i);
3838 if (TREE_CODE (access_fun) == POLYNOMIAL_CHREC)
3840 if (!evolution_function_is_univariate_p (access_fun))
3842 if (DDR_NUM_SUBSCRIPTS (ddr) != 1)
3844 DDR_ARE_DEPENDENT (ddr) = chrec_dont_know;
3845 return;
3848 add_multivariate_self_dist (ddr, DR_ACCESS_FN (DDR_A (ddr), 0));
3849 return;
3852 index_carry = MIN (index_carry,
3853 index_in_loop_nest (CHREC_VARIABLE (access_fun),
3854 DDR_LOOP_NEST (ddr)));
3858 dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3859 add_outer_distances (ddr, dist_v, index_carry);
3862 /* Compute the classic per loop distance vector. DDR is the data
3863 dependence relation to build a vector from. Return false when fail
3864 to represent the data dependence as a distance vector. */
3866 static bool
3867 build_classic_dist_vector (struct data_dependence_relation *ddr)
3869 bool init_b = false;
3870 int index_carry = DDR_NB_LOOPS (ddr);
3871 lambda_vector dist_v;
3873 if (DDR_ARE_DEPENDENT (ddr) != NULL_TREE)
3874 return true;
3876 if (same_access_functions (ddr))
3878 /* Save the 0 vector. */
3879 dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3880 save_dist_v (ddr, dist_v);
3882 if (DDR_NB_LOOPS (ddr) > 1)
3883 add_other_self_distances (ddr);
3885 return true;
3888 dist_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3889 if (!build_classic_dist_vector_1 (ddr, DDR_A (ddr), DDR_B (ddr),
3890 dist_v, &init_b, &index_carry))
3891 return false;
3893 /* Save the distance vector if we initialized one. */
3894 if (init_b)
3896 /* Verify a basic constraint: classic distance vectors should
3897 always be lexicographically positive.
3899 Data references are collected in the order of execution of
3900 the program, thus for the following loop
3902 | for (i = 1; i < 100; i++)
3903 | for (j = 1; j < 100; j++)
3905 | t = T[j+1][i-1]; // A
3906 | T[j][i] = t + 2; // B
3909 references are collected following the direction of the wind:
3910 A then B. The data dependence tests are performed also
3911 following this order, such that we're looking at the distance
3912 separating the elements accessed by A from the elements later
3913 accessed by B. But in this example, the distance returned by
3914 test_dep (A, B) is lexicographically negative (-1, 1), that
3915 means that the access A occurs later than B with respect to
3916 the outer loop, ie. we're actually looking upwind. In this
3917 case we solve test_dep (B, A) looking downwind to the
3918 lexicographically positive solution, that returns the
3919 distance vector (1, -1). */
3920 if (!lambda_vector_lexico_pos (dist_v, DDR_NB_LOOPS (ddr)))
3922 lambda_vector save_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3923 subscript_dependence_tester_1 (ddr, DDR_B (ddr), DDR_A (ddr));
3924 compute_subscript_distance (ddr);
3925 build_classic_dist_vector_1 (ddr, DDR_B (ddr), DDR_A (ddr),
3926 save_v, &init_b, &index_carry);
3927 save_dist_v (ddr, save_v);
3929 /* In this case there is a dependence forward for all the
3930 outer loops:
3932 | for (k = 1; k < 100; k++)
3933 | for (i = 1; i < 100; i++)
3934 | for (j = 1; j < 100; j++)
3936 | t = T[j+1][i-1]; // A
3937 | T[j][i] = t + 2; // B
3940 the vectors are:
3941 (0, 1, -1)
3942 (1, 1, -1)
3943 (1, -1, 1)
3945 if (DDR_NB_LOOPS (ddr) > 1)
3947 add_outer_distances (ddr, save_v, index_carry);
3948 add_outer_distances (ddr, dist_v, index_carry);
3951 else
3953 lambda_vector save_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3954 lambda_vector_copy (dist_v, save_v, DDR_NB_LOOPS (ddr));
3955 save_dist_v (ddr, save_v);
3957 if (DDR_NB_LOOPS (ddr) > 1)
3959 lambda_vector opposite_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
3961 subscript_dependence_tester_1 (ddr, DDR_B (ddr), DDR_A (ddr));
3962 compute_subscript_distance (ddr);
3963 build_classic_dist_vector_1 (ddr, DDR_B (ddr), DDR_A (ddr),
3964 opposite_v, &init_b, &index_carry);
3966 add_outer_distances (ddr, dist_v, index_carry);
3967 add_outer_distances (ddr, opposite_v, index_carry);
3971 else
3973 /* There is a distance of 1 on all the outer loops: Example:
3974 there is a dependence of distance 1 on loop_1 for the array A.
3976 | loop_1
3977 | A[5] = ...
3978 | endloop
3980 add_outer_distances (ddr, dist_v,
3981 lambda_vector_first_nz (dist_v,
3982 DDR_NB_LOOPS (ddr), 0));
3985 if (dump_file && (dump_flags & TDF_DETAILS))
3987 unsigned i;
3989 fprintf (dump_file, "(build_classic_dist_vector\n");
3990 for (i = 0; i < DDR_NUM_DIST_VECTS (ddr); i++)
3992 fprintf (dump_file, " dist_vector = (");
3993 print_lambda_vector (dump_file, DDR_DIST_VECT (ddr, i),
3994 DDR_NB_LOOPS (ddr));
3995 fprintf (dump_file, " )\n");
3997 fprintf (dump_file, ")\n");
4000 return true;
4003 /* Return the direction for a given distance.
4004 FIXME: Computing dir this way is suboptimal, since dir can catch
4005 cases that dist is unable to represent. */
4007 static inline enum data_dependence_direction
4008 dir_from_dist (int dist)
4010 if (dist > 0)
4011 return dir_positive;
4012 else if (dist < 0)
4013 return dir_negative;
4014 else
4015 return dir_equal;
4018 /* Compute the classic per loop direction vector. DDR is the data
4019 dependence relation to build a vector from. */
4021 static void
4022 build_classic_dir_vector (struct data_dependence_relation *ddr)
4024 unsigned i, j;
4025 lambda_vector dist_v;
4027 for (i = 0; VEC_iterate (lambda_vector, DDR_DIST_VECTS (ddr), i, dist_v); i++)
4029 lambda_vector dir_v = lambda_vector_new (DDR_NB_LOOPS (ddr));
4031 for (j = 0; j < DDR_NB_LOOPS (ddr); j++)
4032 dir_v[j] = dir_from_dist (dist_v[j]);
4034 save_dir_v (ddr, dir_v);
4038 /* Helper function. Returns true when there is a dependence between
4039 data references DRA and DRB. */
4041 static bool
4042 subscript_dependence_tester_1 (struct data_dependence_relation *ddr,
4043 struct data_reference *dra,
4044 struct data_reference *drb)
4046 unsigned int i;
4047 tree last_conflicts;
4048 struct subscript *subscript;
4050 for (i = 0; VEC_iterate (subscript_p, DDR_SUBSCRIPTS (ddr), i, subscript);
4051 i++)
4053 conflict_function *overlaps_a, *overlaps_b;
4055 analyze_overlapping_iterations (DR_ACCESS_FN (dra, i),
4056 DR_ACCESS_FN (drb, i),
4057 &overlaps_a, &overlaps_b,
4058 &last_conflicts);
4060 if (CF_NOT_KNOWN_P (overlaps_a)
4061 || CF_NOT_KNOWN_P (overlaps_b))
4063 finalize_ddr_dependent (ddr, chrec_dont_know);
4064 dependence_stats.num_dependence_undetermined++;
4065 free_conflict_function (overlaps_a);
4066 free_conflict_function (overlaps_b);
4067 return false;
4070 else if (CF_NO_DEPENDENCE_P (overlaps_a)
4071 || CF_NO_DEPENDENCE_P (overlaps_b))
4073 finalize_ddr_dependent (ddr, chrec_known);
4074 dependence_stats.num_dependence_independent++;
4075 free_conflict_function (overlaps_a);
4076 free_conflict_function (overlaps_b);
4077 return false;
4080 else
4082 SUB_CONFLICTS_IN_A (subscript) = overlaps_a;
4083 SUB_CONFLICTS_IN_B (subscript) = overlaps_b;
4084 SUB_LAST_CONFLICT (subscript) = last_conflicts;
4088 return true;
4091 /* Computes the conflicting iterations, and initialize DDR. */
4093 static void
4094 subscript_dependence_tester (struct data_dependence_relation *ddr)
4097 if (dump_file && (dump_flags & TDF_DETAILS))
4098 fprintf (dump_file, "(subscript_dependence_tester \n");
4100 if (subscript_dependence_tester_1 (ddr, DDR_A (ddr), DDR_B (ddr)))
4101 dependence_stats.num_dependence_dependent++;
4103 compute_subscript_distance (ddr);
4104 if (build_classic_dist_vector (ddr))
4105 build_classic_dir_vector (ddr);
4107 if (dump_file && (dump_flags & TDF_DETAILS))
4108 fprintf (dump_file, ")\n");
4111 /* Returns true when all the access functions of A are affine or
4112 constant. */
4114 static bool
4115 access_functions_are_affine_or_constant_p (struct data_reference *a)
4117 unsigned int i;
4118 VEC(tree,heap) **fns = DR_ACCESS_FNS_ADDR (a);
4119 tree t;
4121 for (i = 0; VEC_iterate (tree, *fns, i, t); i++)
4122 if (!evolution_function_is_constant_p (t)
4123 && !evolution_function_is_affine_multivariate_p (t))
4124 return false;
4126 return true;
4129 /* This computes the affine dependence relation between A and B.
4130 CHREC_KNOWN is used for representing the independence between two
4131 accesses, while CHREC_DONT_KNOW is used for representing the unknown
4132 relation.
4134 Note that it is possible to stop the computation of the dependence
4135 relation the first time we detect a CHREC_KNOWN element for a given
4136 subscript. */
4138 static void
4139 compute_affine_dependence (struct data_dependence_relation *ddr)
4141 struct data_reference *dra = DDR_A (ddr);
4142 struct data_reference *drb = DDR_B (ddr);
4144 if (dump_file && (dump_flags & TDF_DETAILS))
4146 fprintf (dump_file, "(compute_affine_dependence\n");
4147 fprintf (dump_file, " (stmt_a = \n");
4148 print_generic_expr (dump_file, DR_STMT (dra), 0);
4149 fprintf (dump_file, ")\n (stmt_b = \n");
4150 print_generic_expr (dump_file, DR_STMT (drb), 0);
4151 fprintf (dump_file, ")\n");
4154 /* Analyze only when the dependence relation is not yet known. */
4155 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
4157 dependence_stats.num_dependence_tests++;
4159 if (access_functions_are_affine_or_constant_p (dra)
4160 && access_functions_are_affine_or_constant_p (drb))
4161 subscript_dependence_tester (ddr);
4163 /* As a last case, if the dependence cannot be determined, or if
4164 the dependence is considered too difficult to determine, answer
4165 "don't know". */
4166 else
4168 dependence_stats.num_dependence_undetermined++;
4170 if (dump_file && (dump_flags & TDF_DETAILS))
4172 fprintf (dump_file, "Data ref a:\n");
4173 dump_data_reference (dump_file, dra);
4174 fprintf (dump_file, "Data ref b:\n");
4175 dump_data_reference (dump_file, drb);
4176 fprintf (dump_file, "affine dependence test not usable: access function not affine or constant.\n");
4178 finalize_ddr_dependent (ddr, chrec_dont_know);
4182 if (dump_file && (dump_flags & TDF_DETAILS))
4183 fprintf (dump_file, ")\n");
4186 /* This computes the dependence relation for the same data
4187 reference into DDR. */
4189 static void
4190 compute_self_dependence (struct data_dependence_relation *ddr)
4192 unsigned int i;
4193 struct subscript *subscript;
4195 for (i = 0; VEC_iterate (subscript_p, DDR_SUBSCRIPTS (ddr), i, subscript);
4196 i++)
4198 /* The accessed index overlaps for each iteration. */
4199 SUB_CONFLICTS_IN_A (subscript)
4200 = conflict_fn (1, affine_fn_cst (integer_zero_node));
4201 SUB_CONFLICTS_IN_B (subscript)
4202 = conflict_fn (1, affine_fn_cst (integer_zero_node));
4203 SUB_LAST_CONFLICT (subscript) = chrec_dont_know;
4206 /* The distance vector is the zero vector. */
4207 save_dist_v (ddr, lambda_vector_new (DDR_NB_LOOPS (ddr)));
4208 save_dir_v (ddr, lambda_vector_new (DDR_NB_LOOPS (ddr)));
4211 /* Compute in DEPENDENCE_RELATIONS the data dependence graph for all
4212 the data references in DATAREFS, in the LOOP_NEST. When
4213 COMPUTE_SELF_AND_RR is FALSE, don't compute read-read and self
4214 relations. */
4216 static void
4217 compute_all_dependences (VEC (data_reference_p, heap) *datarefs,
4218 VEC (ddr_p, heap) **dependence_relations,
4219 VEC (loop_p, heap) *loop_nest,
4220 bool compute_self_and_rr)
4222 struct data_dependence_relation *ddr;
4223 struct data_reference *a, *b;
4224 unsigned int i, j;
4226 for (i = 0; VEC_iterate (data_reference_p, datarefs, i, a); i++)
4227 for (j = i + 1; VEC_iterate (data_reference_p, datarefs, j, b); j++)
4228 if (!DR_IS_READ (a) || !DR_IS_READ (b) || compute_self_and_rr)
4230 ddr = initialize_data_dependence_relation (a, b, loop_nest);
4231 VEC_safe_push (ddr_p, heap, *dependence_relations, ddr);
4232 compute_affine_dependence (ddr);
4235 if (compute_self_and_rr)
4236 for (i = 0; VEC_iterate (data_reference_p, datarefs, i, a); i++)
4238 ddr = initialize_data_dependence_relation (a, a, loop_nest);
4239 VEC_safe_push (ddr_p, heap, *dependence_relations, ddr);
4240 compute_self_dependence (ddr);
4244 /* Stores the locations of memory references in STMT to REFERENCES. Returns
4245 true if STMT clobbers memory, false otherwise. */
4247 bool
4248 get_references_in_stmt (tree stmt, VEC (data_ref_loc, heap) **references)
4250 bool clobbers_memory = false;
4251 data_ref_loc *ref;
4252 tree *op0, *op1, arg, call;
4253 call_expr_arg_iterator iter;
4255 *references = NULL;
4257 /* ASM_EXPR and CALL_EXPR may embed arbitrary side effects.
4258 Calls have side-effects, except those to const or pure
4259 functions. */
4260 call = get_call_expr_in (stmt);
4261 if ((call
4262 && !(call_expr_flags (call) & (ECF_CONST | ECF_PURE)))
4263 || (TREE_CODE (stmt) == ASM_EXPR
4264 && ASM_VOLATILE_P (stmt)))
4265 clobbers_memory = true;
4267 if (ZERO_SSA_OPERANDS (stmt, SSA_OP_ALL_VIRTUALS))
4268 return clobbers_memory;
4270 if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT)
4272 op0 = &GIMPLE_STMT_OPERAND (stmt, 0);
4273 op1 = &GIMPLE_STMT_OPERAND (stmt, 1);
4275 if (DECL_P (*op1)
4276 || REFERENCE_CLASS_P (*op1))
4278 ref = VEC_safe_push (data_ref_loc, heap, *references, NULL);
4279 ref->pos = op1;
4280 ref->is_read = true;
4283 if (DECL_P (*op0)
4284 || REFERENCE_CLASS_P (*op0))
4286 ref = VEC_safe_push (data_ref_loc, heap, *references, NULL);
4287 ref->pos = op0;
4288 ref->is_read = false;
4292 if (call)
4294 FOR_EACH_CALL_EXPR_ARG (arg, iter, call)
4296 op0 = &arg;
4297 if (DECL_P (*op0)
4298 || REFERENCE_CLASS_P (*op0))
4300 ref = VEC_safe_push (data_ref_loc, heap, *references, NULL);
4301 ref->pos = op0;
4302 ref->is_read = true;
4307 return clobbers_memory;
4310 /* Stores the data references in STMT to DATAREFS. If there is an unanalyzable
4311 reference, returns false, otherwise returns true. */
4313 static bool
4314 find_data_references_in_stmt (tree stmt,
4315 VEC (data_reference_p, heap) **datarefs)
4317 unsigned i;
4318 VEC (data_ref_loc, heap) *references;
4319 data_ref_loc *ref;
4320 bool ret = true;
4321 data_reference_p dr;
4323 if (get_references_in_stmt (stmt, &references))
4325 VEC_free (data_ref_loc, heap, references);
4326 return false;
4329 for (i = 0; VEC_iterate (data_ref_loc, references, i, ref); i++)
4331 dr = create_data_ref (*ref->pos, stmt, ref->is_read);
4332 if (dr)
4333 VEC_safe_push (data_reference_p, heap, *datarefs, dr);
4334 else
4336 ret = false;
4337 break;
4340 VEC_free (data_ref_loc, heap, references);
4341 return ret;
4344 /* Search the data references in LOOP, and record the information into
4345 DATAREFS. Returns chrec_dont_know when failing to analyze a
4346 difficult case, returns NULL_TREE otherwise.
4348 TODO: This function should be made smarter so that it can handle address
4349 arithmetic as if they were array accesses, etc. */
4351 tree
4352 find_data_references_in_loop (struct loop *loop,
4353 VEC (data_reference_p, heap) **datarefs)
4355 basic_block bb, *bbs;
4356 unsigned int i;
4357 block_stmt_iterator bsi;
4359 bbs = get_loop_body (loop);
4361 for (i = 0; i < loop->num_nodes; i++)
4363 bb = bbs[i];
4365 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
4367 tree stmt = bsi_stmt (bsi);
4369 if (!find_data_references_in_stmt (stmt, datarefs))
4371 struct data_reference *res;
4372 res = XNEW (struct data_reference);
4373 DR_STMT (res) = NULL_TREE;
4374 DR_REF (res) = NULL_TREE;
4375 DR_BASE_OBJECT (res) = NULL;
4376 DR_TYPE (res) = ARRAY_REF_TYPE;
4377 DR_SET_ACCESS_FNS (res, NULL);
4378 DR_BASE_OBJECT (res) = NULL;
4379 DR_IS_READ (res) = false;
4380 DR_BASE_ADDRESS (res) = NULL_TREE;
4381 DR_OFFSET (res) = NULL_TREE;
4382 DR_INIT (res) = NULL_TREE;
4383 DR_STEP (res) = NULL_TREE;
4384 DR_OFFSET_MISALIGNMENT (res) = NULL_TREE;
4385 DR_MEMTAG (res) = NULL_TREE;
4386 DR_PTR_INFO (res) = NULL;
4387 VEC_safe_push (data_reference_p, heap, *datarefs, res);
4389 free (bbs);
4390 return chrec_dont_know;
4394 free (bbs);
4396 return NULL_TREE;
4399 /* Recursive helper function. */
4401 static bool
4402 find_loop_nest_1 (struct loop *loop, VEC (loop_p, heap) **loop_nest)
4404 /* Inner loops of the nest should not contain siblings. Example:
4405 when there are two consecutive loops,
4407 | loop_0
4408 | loop_1
4409 | A[{0, +, 1}_1]
4410 | endloop_1
4411 | loop_2
4412 | A[{0, +, 1}_2]
4413 | endloop_2
4414 | endloop_0
4416 the dependence relation cannot be captured by the distance
4417 abstraction. */
4418 if (loop->next)
4419 return false;
4421 VEC_safe_push (loop_p, heap, *loop_nest, loop);
4422 if (loop->inner)
4423 return find_loop_nest_1 (loop->inner, loop_nest);
4424 return true;
4427 /* Return false when the LOOP is not well nested. Otherwise return
4428 true and insert in LOOP_NEST the loops of the nest. LOOP_NEST will
4429 contain the loops from the outermost to the innermost, as they will
4430 appear in the classic distance vector. */
4432 static bool
4433 find_loop_nest (struct loop *loop, VEC (loop_p, heap) **loop_nest)
4435 VEC_safe_push (loop_p, heap, *loop_nest, loop);
4436 if (loop->inner)
4437 return find_loop_nest_1 (loop->inner, loop_nest);
4438 return true;
4441 /* Given a loop nest LOOP, the following vectors are returned:
4442 DATAREFS is initialized to all the array elements contained in this loop,
4443 DEPENDENCE_RELATIONS contains the relations between the data references.
4444 Compute read-read and self relations if
4445 COMPUTE_SELF_AND_READ_READ_DEPENDENCES is TRUE. */
4447 void
4448 compute_data_dependences_for_loop (struct loop *loop,
4449 bool compute_self_and_read_read_dependences,
4450 VEC (data_reference_p, heap) **datarefs,
4451 VEC (ddr_p, heap) **dependence_relations)
4453 struct loop *loop_nest = loop;
4454 VEC (loop_p, heap) *vloops = VEC_alloc (loop_p, heap, 3);
4456 memset (&dependence_stats, 0, sizeof (dependence_stats));
4458 /* If the loop nest is not well formed, or one of the data references
4459 is not computable, give up without spending time to compute other
4460 dependences. */
4461 if (!loop_nest
4462 || !find_loop_nest (loop_nest, &vloops)
4463 || find_data_references_in_loop (loop, datarefs) == chrec_dont_know)
4465 struct data_dependence_relation *ddr;
4467 /* Insert a single relation into dependence_relations:
4468 chrec_dont_know. */
4469 ddr = initialize_data_dependence_relation (NULL, NULL, vloops);
4470 VEC_safe_push (ddr_p, heap, *dependence_relations, ddr);
4472 else
4473 compute_all_dependences (*datarefs, dependence_relations, vloops,
4474 compute_self_and_read_read_dependences);
4476 if (dump_file && (dump_flags & TDF_STATS))
4478 fprintf (dump_file, "Dependence tester statistics:\n");
4480 fprintf (dump_file, "Number of dependence tests: %d\n",
4481 dependence_stats.num_dependence_tests);
4482 fprintf (dump_file, "Number of dependence tests classified dependent: %d\n",
4483 dependence_stats.num_dependence_dependent);
4484 fprintf (dump_file, "Number of dependence tests classified independent: %d\n",
4485 dependence_stats.num_dependence_independent);
4486 fprintf (dump_file, "Number of undetermined dependence tests: %d\n",
4487 dependence_stats.num_dependence_undetermined);
4489 fprintf (dump_file, "Number of subscript tests: %d\n",
4490 dependence_stats.num_subscript_tests);
4491 fprintf (dump_file, "Number of undetermined subscript tests: %d\n",
4492 dependence_stats.num_subscript_undetermined);
4493 fprintf (dump_file, "Number of same subscript function: %d\n",
4494 dependence_stats.num_same_subscript_function);
4496 fprintf (dump_file, "Number of ziv tests: %d\n",
4497 dependence_stats.num_ziv);
4498 fprintf (dump_file, "Number of ziv tests returning dependent: %d\n",
4499 dependence_stats.num_ziv_dependent);
4500 fprintf (dump_file, "Number of ziv tests returning independent: %d\n",
4501 dependence_stats.num_ziv_independent);
4502 fprintf (dump_file, "Number of ziv tests unimplemented: %d\n",
4503 dependence_stats.num_ziv_unimplemented);
4505 fprintf (dump_file, "Number of siv tests: %d\n",
4506 dependence_stats.num_siv);
4507 fprintf (dump_file, "Number of siv tests returning dependent: %d\n",
4508 dependence_stats.num_siv_dependent);
4509 fprintf (dump_file, "Number of siv tests returning independent: %d\n",
4510 dependence_stats.num_siv_independent);
4511 fprintf (dump_file, "Number of siv tests unimplemented: %d\n",
4512 dependence_stats.num_siv_unimplemented);
4514 fprintf (dump_file, "Number of miv tests: %d\n",
4515 dependence_stats.num_miv);
4516 fprintf (dump_file, "Number of miv tests returning dependent: %d\n",
4517 dependence_stats.num_miv_dependent);
4518 fprintf (dump_file, "Number of miv tests returning independent: %d\n",
4519 dependence_stats.num_miv_independent);
4520 fprintf (dump_file, "Number of miv tests unimplemented: %d\n",
4521 dependence_stats.num_miv_unimplemented);
4525 /* Entry point (for testing only). Analyze all the data references
4526 and the dependence relations.
4528 The data references are computed first.
4530 A relation on these nodes is represented by a complete graph. Some
4531 of the relations could be of no interest, thus the relations can be
4532 computed on demand.
4534 In the following function we compute all the relations. This is
4535 just a first implementation that is here for:
4536 - for showing how to ask for the dependence relations,
4537 - for the debugging the whole dependence graph,
4538 - for the dejagnu testcases and maintenance.
4540 It is possible to ask only for a part of the graph, avoiding to
4541 compute the whole dependence graph. The computed dependences are
4542 stored in a knowledge base (KB) such that later queries don't
4543 recompute the same information. The implementation of this KB is
4544 transparent to the optimizer, and thus the KB can be changed with a
4545 more efficient implementation, or the KB could be disabled. */
4546 #if 0
4547 static void
4548 analyze_all_data_dependences (struct loops *loops)
4550 unsigned int i;
4551 int nb_data_refs = 10;
4552 VEC (data_reference_p, heap) *datarefs =
4553 VEC_alloc (data_reference_p, heap, nb_data_refs);
4554 VEC (ddr_p, heap) *dependence_relations =
4555 VEC_alloc (ddr_p, heap, nb_data_refs * nb_data_refs);
4557 /* Compute DDs on the whole function. */
4558 compute_data_dependences_for_loop (loops->parray[0], false,
4559 &datarefs, &dependence_relations);
4561 if (dump_file)
4563 dump_data_dependence_relations (dump_file, dependence_relations);
4564 fprintf (dump_file, "\n\n");
4566 if (dump_flags & TDF_DETAILS)
4567 dump_dist_dir_vectors (dump_file, dependence_relations);
4569 if (dump_flags & TDF_STATS)
4571 unsigned nb_top_relations = 0;
4572 unsigned nb_bot_relations = 0;
4573 unsigned nb_basename_differ = 0;
4574 unsigned nb_chrec_relations = 0;
4575 struct data_dependence_relation *ddr;
4577 for (i = 0; VEC_iterate (ddr_p, dependence_relations, i, ddr); i++)
4579 if (chrec_contains_undetermined (DDR_ARE_DEPENDENT (ddr)))
4580 nb_top_relations++;
4582 else if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
4584 struct data_reference *a = DDR_A (ddr);
4585 struct data_reference *b = DDR_B (ddr);
4586 bool differ_p;
4588 if ((DR_BASE_OBJECT (a) && DR_BASE_OBJECT (b)
4589 && DR_NUM_DIMENSIONS (a) != DR_NUM_DIMENSIONS (b))
4590 || (base_object_differ_p (a, b, &differ_p)
4591 && differ_p))
4592 nb_basename_differ++;
4593 else
4594 nb_bot_relations++;
4597 else
4598 nb_chrec_relations++;
4601 gather_stats_on_scev_database ();
4605 free_dependence_relations (dependence_relations);
4606 free_data_refs (datarefs);
4608 #endif
4610 /* Free the memory used by a data dependence relation DDR. */
4612 void
4613 free_dependence_relation (struct data_dependence_relation *ddr)
4615 if (ddr == NULL)
4616 return;
4618 if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE && DDR_SUBSCRIPTS (ddr))
4619 free_subscripts (DDR_SUBSCRIPTS (ddr));
4621 free (ddr);
4624 /* Free the memory used by the data dependence relations from
4625 DEPENDENCE_RELATIONS. */
4627 void
4628 free_dependence_relations (VEC (ddr_p, heap) *dependence_relations)
4630 unsigned int i;
4631 struct data_dependence_relation *ddr;
4632 VEC (loop_p, heap) *loop_nest = NULL;
4634 for (i = 0; VEC_iterate (ddr_p, dependence_relations, i, ddr); i++)
4636 if (ddr == NULL)
4637 continue;
4638 if (loop_nest == NULL)
4639 loop_nest = DDR_LOOP_NEST (ddr);
4640 else
4641 gcc_assert (DDR_LOOP_NEST (ddr) == NULL
4642 || DDR_LOOP_NEST (ddr) == loop_nest);
4643 free_dependence_relation (ddr);
4646 if (loop_nest)
4647 VEC_free (loop_p, heap, loop_nest);
4648 VEC_free (ddr_p, heap, dependence_relations);
4651 /* Free the memory used by the data references from DATAREFS. */
4653 void
4654 free_data_refs (VEC (data_reference_p, heap) *datarefs)
4656 unsigned int i;
4657 struct data_reference *dr;
4659 for (i = 0; VEC_iterate (data_reference_p, datarefs, i, dr); i++)
4660 free_data_ref (dr);
4661 VEC_free (data_reference_p, heap, datarefs);