isl_affine_hull.c: uset_affine_hull: avoid use of isl_basic_set_universe_like
[isl.git] / isl_flow.c
blob2f026d5084138df5e534d8f6bb76b74542874ad8
1 /*
2 * Copyright 2005-2007 Universiteit Leiden
3 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Copyright 2010 INRIA Saclay
5 * Copyright 2012 Universiteit Leiden
6 * Copyright 2014 Ecole Normale Superieure
8 * Use of this software is governed by the MIT license
10 * Written by Sven Verdoolaege, Leiden Institute of Advanced Computer Science,
11 * Universiteit Leiden, Niels Bohrweg 1, 2333 CA Leiden, The Netherlands
12 * and K.U.Leuven, Departement Computerwetenschappen, Celestijnenlaan 200A,
13 * B-3001 Leuven, Belgium
14 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
15 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
16 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
19 #include <isl/set.h>
20 #include <isl/map.h>
21 #include <isl/union_set.h>
22 #include <isl/union_map.h>
23 #include <isl/flow.h>
24 #include <isl/schedule_node.h>
25 #include <isl_sort.h>
27 enum isl_restriction_type {
28 isl_restriction_type_empty,
29 isl_restriction_type_none,
30 isl_restriction_type_input,
31 isl_restriction_type_output
34 struct isl_restriction {
35 enum isl_restriction_type type;
37 isl_set *source;
38 isl_set *sink;
41 /* Create a restriction of the given type.
43 static __isl_give isl_restriction *isl_restriction_alloc(
44 __isl_take isl_map *source_map, enum isl_restriction_type type)
46 isl_ctx *ctx;
47 isl_restriction *restr;
49 if (!source_map)
50 return NULL;
52 ctx = isl_map_get_ctx(source_map);
53 restr = isl_calloc_type(ctx, struct isl_restriction);
54 if (!restr)
55 goto error;
57 restr->type = type;
59 isl_map_free(source_map);
60 return restr;
61 error:
62 isl_map_free(source_map);
63 return NULL;
66 /* Create a restriction that doesn't restrict anything.
68 __isl_give isl_restriction *isl_restriction_none(__isl_take isl_map *source_map)
70 return isl_restriction_alloc(source_map, isl_restriction_type_none);
73 /* Create a restriction that removes everything.
75 __isl_give isl_restriction *isl_restriction_empty(
76 __isl_take isl_map *source_map)
78 return isl_restriction_alloc(source_map, isl_restriction_type_empty);
81 /* Create a restriction on the input of the maximization problem
82 * based on the given source and sink restrictions.
84 __isl_give isl_restriction *isl_restriction_input(
85 __isl_take isl_set *source_restr, __isl_take isl_set *sink_restr)
87 isl_ctx *ctx;
88 isl_restriction *restr;
90 if (!source_restr || !sink_restr)
91 goto error;
93 ctx = isl_set_get_ctx(source_restr);
94 restr = isl_calloc_type(ctx, struct isl_restriction);
95 if (!restr)
96 goto error;
98 restr->type = isl_restriction_type_input;
99 restr->source = source_restr;
100 restr->sink = sink_restr;
102 return restr;
103 error:
104 isl_set_free(source_restr);
105 isl_set_free(sink_restr);
106 return NULL;
109 /* Create a restriction on the output of the maximization problem
110 * based on the given source restriction.
112 __isl_give isl_restriction *isl_restriction_output(
113 __isl_take isl_set *source_restr)
115 isl_ctx *ctx;
116 isl_restriction *restr;
118 if (!source_restr)
119 return NULL;
121 ctx = isl_set_get_ctx(source_restr);
122 restr = isl_calloc_type(ctx, struct isl_restriction);
123 if (!restr)
124 goto error;
126 restr->type = isl_restriction_type_output;
127 restr->source = source_restr;
129 return restr;
130 error:
131 isl_set_free(source_restr);
132 return NULL;
135 __isl_null isl_restriction *isl_restriction_free(
136 __isl_take isl_restriction *restr)
138 if (!restr)
139 return NULL;
141 isl_set_free(restr->source);
142 isl_set_free(restr->sink);
143 free(restr);
144 return NULL;
147 isl_ctx *isl_restriction_get_ctx(__isl_keep isl_restriction *restr)
149 return restr ? isl_set_get_ctx(restr->source) : NULL;
152 /* A private structure to keep track of a mapping together with
153 * a user-specified identifier and a boolean indicating whether
154 * the map represents a must or may access/dependence.
156 struct isl_labeled_map {
157 struct isl_map *map;
158 void *data;
159 int must;
162 /* A structure containing the input for dependence analysis:
163 * - a sink
164 * - n_must + n_may (<= max_source) sources
165 * - a function for determining the relative order of sources and sink
166 * The must sources are placed before the may sources.
168 * domain_map is an auxiliary map that maps the sink access relation
169 * to the domain of this access relation.
171 * restrict_fn is a callback that (if not NULL) will be called
172 * right before any lexicographical maximization.
174 struct isl_access_info {
175 isl_map *domain_map;
176 struct isl_labeled_map sink;
177 isl_access_level_before level_before;
179 isl_access_restrict restrict_fn;
180 void *restrict_user;
182 int max_source;
183 int n_must;
184 int n_may;
185 struct isl_labeled_map source[1];
188 /* A structure containing the output of dependence analysis:
189 * - n_source dependences
190 * - a wrapped subset of the sink for which definitely no source could be found
191 * - a wrapped subset of the sink for which possibly no source could be found
193 struct isl_flow {
194 isl_set *must_no_source;
195 isl_set *may_no_source;
196 int n_source;
197 struct isl_labeled_map *dep;
200 /* Construct an isl_access_info structure and fill it up with
201 * the given data. The number of sources is set to 0.
203 __isl_give isl_access_info *isl_access_info_alloc(__isl_take isl_map *sink,
204 void *sink_user, isl_access_level_before fn, int max_source)
206 isl_ctx *ctx;
207 struct isl_access_info *acc;
209 if (!sink)
210 return NULL;
212 ctx = isl_map_get_ctx(sink);
213 isl_assert(ctx, max_source >= 0, goto error);
215 acc = isl_calloc(ctx, struct isl_access_info,
216 sizeof(struct isl_access_info) +
217 (max_source - 1) * sizeof(struct isl_labeled_map));
218 if (!acc)
219 goto error;
221 acc->sink.map = sink;
222 acc->sink.data = sink_user;
223 acc->level_before = fn;
224 acc->max_source = max_source;
225 acc->n_must = 0;
226 acc->n_may = 0;
228 return acc;
229 error:
230 isl_map_free(sink);
231 return NULL;
234 /* Free the given isl_access_info structure.
236 __isl_null isl_access_info *isl_access_info_free(
237 __isl_take isl_access_info *acc)
239 int i;
241 if (!acc)
242 return NULL;
243 isl_map_free(acc->domain_map);
244 isl_map_free(acc->sink.map);
245 for (i = 0; i < acc->n_must + acc->n_may; ++i)
246 isl_map_free(acc->source[i].map);
247 free(acc);
248 return NULL;
251 isl_ctx *isl_access_info_get_ctx(__isl_keep isl_access_info *acc)
253 return acc ? isl_map_get_ctx(acc->sink.map) : NULL;
256 __isl_give isl_access_info *isl_access_info_set_restrict(
257 __isl_take isl_access_info *acc, isl_access_restrict fn, void *user)
259 if (!acc)
260 return NULL;
261 acc->restrict_fn = fn;
262 acc->restrict_user = user;
263 return acc;
266 /* Add another source to an isl_access_info structure, making
267 * sure the "must" sources are placed before the "may" sources.
268 * This function may be called at most max_source times on a
269 * given isl_access_info structure, with max_source as specified
270 * in the call to isl_access_info_alloc that constructed the structure.
272 __isl_give isl_access_info *isl_access_info_add_source(
273 __isl_take isl_access_info *acc, __isl_take isl_map *source,
274 int must, void *source_user)
276 isl_ctx *ctx;
278 if (!acc)
279 goto error;
280 ctx = isl_map_get_ctx(acc->sink.map);
281 isl_assert(ctx, acc->n_must + acc->n_may < acc->max_source, goto error);
283 if (must) {
284 if (acc->n_may)
285 acc->source[acc->n_must + acc->n_may] =
286 acc->source[acc->n_must];
287 acc->source[acc->n_must].map = source;
288 acc->source[acc->n_must].data = source_user;
289 acc->source[acc->n_must].must = 1;
290 acc->n_must++;
291 } else {
292 acc->source[acc->n_must + acc->n_may].map = source;
293 acc->source[acc->n_must + acc->n_may].data = source_user;
294 acc->source[acc->n_must + acc->n_may].must = 0;
295 acc->n_may++;
298 return acc;
299 error:
300 isl_map_free(source);
301 isl_access_info_free(acc);
302 return NULL;
305 /* Return -n, 0 or n (with n a positive value), depending on whether
306 * the source access identified by p1 should be sorted before, together
307 * or after that identified by p2.
309 * If p1 appears before p2, then it should be sorted first.
310 * For more generic initial schedules, it is possible that neither
311 * p1 nor p2 appears before the other, or at least not in any obvious way.
312 * We therefore also check if p2 appears before p1, in which case p2
313 * should be sorted first.
314 * If not, we try to order the two statements based on the description
315 * of the iteration domains. This results in an arbitrary, but fairly
316 * stable ordering.
318 static int access_sort_cmp(const void *p1, const void *p2, void *user)
320 isl_access_info *acc = user;
321 const struct isl_labeled_map *i1, *i2;
322 int level1, level2;
323 uint32_t h1, h2;
324 i1 = (const struct isl_labeled_map *) p1;
325 i2 = (const struct isl_labeled_map *) p2;
327 level1 = acc->level_before(i1->data, i2->data);
328 if (level1 % 2)
329 return -1;
331 level2 = acc->level_before(i2->data, i1->data);
332 if (level2 % 2)
333 return 1;
335 h1 = isl_map_get_hash(i1->map);
336 h2 = isl_map_get_hash(i2->map);
337 return h1 > h2 ? 1 : h1 < h2 ? -1 : 0;
340 /* Sort the must source accesses in their textual order.
342 static __isl_give isl_access_info *isl_access_info_sort_sources(
343 __isl_take isl_access_info *acc)
345 if (!acc)
346 return NULL;
347 if (acc->n_must <= 1)
348 return acc;
350 if (isl_sort(acc->source, acc->n_must, sizeof(struct isl_labeled_map),
351 access_sort_cmp, acc) < 0)
352 return isl_access_info_free(acc);
354 return acc;
357 /* Align the parameters of the two spaces if needed and then call
358 * isl_space_join.
360 static __isl_give isl_space *space_align_and_join(__isl_take isl_space *left,
361 __isl_take isl_space *right)
363 if (isl_space_match(left, isl_dim_param, right, isl_dim_param))
364 return isl_space_join(left, right);
366 left = isl_space_align_params(left, isl_space_copy(right));
367 right = isl_space_align_params(right, isl_space_copy(left));
368 return isl_space_join(left, right);
371 /* Initialize an empty isl_flow structure corresponding to a given
372 * isl_access_info structure.
373 * For each must access, two dependences are created (initialized
374 * to the empty relation), one for the resulting must dependences
375 * and one for the resulting may dependences. May accesses can
376 * only lead to may dependences, so only one dependence is created
377 * for each of them.
378 * This function is private as isl_flow structures are only supposed
379 * to be created by isl_access_info_compute_flow.
381 static __isl_give isl_flow *isl_flow_alloc(__isl_keep isl_access_info *acc)
383 int i, n;
384 struct isl_ctx *ctx;
385 struct isl_flow *dep;
387 if (!acc)
388 return NULL;
390 ctx = isl_map_get_ctx(acc->sink.map);
391 dep = isl_calloc_type(ctx, struct isl_flow);
392 if (!dep)
393 return NULL;
395 n = 2 * acc->n_must + acc->n_may;
396 dep->dep = isl_calloc_array(ctx, struct isl_labeled_map, n);
397 if (n && !dep->dep)
398 goto error;
400 dep->n_source = n;
401 for (i = 0; i < acc->n_must; ++i) {
402 isl_space *dim;
403 dim = space_align_and_join(
404 isl_map_get_space(acc->source[i].map),
405 isl_space_reverse(isl_map_get_space(acc->sink.map)));
406 dep->dep[2 * i].map = isl_map_empty(dim);
407 dep->dep[2 * i + 1].map = isl_map_copy(dep->dep[2 * i].map);
408 dep->dep[2 * i].data = acc->source[i].data;
409 dep->dep[2 * i + 1].data = acc->source[i].data;
410 dep->dep[2 * i].must = 1;
411 dep->dep[2 * i + 1].must = 0;
412 if (!dep->dep[2 * i].map || !dep->dep[2 * i + 1].map)
413 goto error;
415 for (i = acc->n_must; i < acc->n_must + acc->n_may; ++i) {
416 isl_space *dim;
417 dim = space_align_and_join(
418 isl_map_get_space(acc->source[i].map),
419 isl_space_reverse(isl_map_get_space(acc->sink.map)));
420 dep->dep[acc->n_must + i].map = isl_map_empty(dim);
421 dep->dep[acc->n_must + i].data = acc->source[i].data;
422 dep->dep[acc->n_must + i].must = 0;
423 if (!dep->dep[acc->n_must + i].map)
424 goto error;
427 return dep;
428 error:
429 isl_flow_free(dep);
430 return NULL;
433 /* Iterate over all sources and for each resulting flow dependence
434 * that is not empty, call the user specfied function.
435 * The second argument in this function call identifies the source,
436 * while the third argument correspond to the final argument of
437 * the isl_flow_foreach call.
439 int isl_flow_foreach(__isl_keep isl_flow *deps,
440 int (*fn)(__isl_take isl_map *dep, int must, void *dep_user, void *user),
441 void *user)
443 int i;
445 if (!deps)
446 return -1;
448 for (i = 0; i < deps->n_source; ++i) {
449 if (isl_map_plain_is_empty(deps->dep[i].map))
450 continue;
451 if (fn(isl_map_copy(deps->dep[i].map), deps->dep[i].must,
452 deps->dep[i].data, user) < 0)
453 return -1;
456 return 0;
459 /* Return a copy of the subset of the sink for which no source could be found.
461 __isl_give isl_map *isl_flow_get_no_source(__isl_keep isl_flow *deps, int must)
463 if (!deps)
464 return NULL;
466 if (must)
467 return isl_set_unwrap(isl_set_copy(deps->must_no_source));
468 else
469 return isl_set_unwrap(isl_set_copy(deps->may_no_source));
472 void isl_flow_free(__isl_take isl_flow *deps)
474 int i;
476 if (!deps)
477 return;
478 isl_set_free(deps->must_no_source);
479 isl_set_free(deps->may_no_source);
480 if (deps->dep) {
481 for (i = 0; i < deps->n_source; ++i)
482 isl_map_free(deps->dep[i].map);
483 free(deps->dep);
485 free(deps);
488 isl_ctx *isl_flow_get_ctx(__isl_keep isl_flow *deps)
490 return deps ? isl_set_get_ctx(deps->must_no_source) : NULL;
493 /* Return a map that enforces that the domain iteration occurs after
494 * the range iteration at the given level.
495 * If level is odd, then the domain iteration should occur after
496 * the target iteration in their shared level/2 outermost loops.
497 * In this case we simply need to enforce that these outermost
498 * loop iterations are the same.
499 * If level is even, then the loop iterator of the domain should
500 * be greater than the loop iterator of the range at the last
501 * of the level/2 shared loops, i.e., loop level/2 - 1.
503 static __isl_give isl_map *after_at_level(__isl_take isl_space *dim, int level)
505 struct isl_basic_map *bmap;
507 if (level % 2)
508 bmap = isl_basic_map_equal(dim, level/2);
509 else
510 bmap = isl_basic_map_more_at(dim, level/2 - 1);
512 return isl_map_from_basic_map(bmap);
515 /* Compute the partial lexicographic maximum of "dep" on domain "sink",
516 * but first check if the user has set acc->restrict_fn and if so
517 * update either the input or the output of the maximization problem
518 * with respect to the resulting restriction.
520 * Since the user expects a mapping from sink iterations to source iterations,
521 * whereas the domain of "dep" is a wrapped map, mapping sink iterations
522 * to accessed array elements, we first need to project out the accessed
523 * sink array elements by applying acc->domain_map.
524 * Similarly, the sink restriction specified by the user needs to be
525 * converted back to the wrapped map.
527 static __isl_give isl_map *restricted_partial_lexmax(
528 __isl_keep isl_access_info *acc, __isl_take isl_map *dep,
529 int source, __isl_take isl_set *sink, __isl_give isl_set **empty)
531 isl_map *source_map;
532 isl_restriction *restr;
533 isl_set *sink_domain;
534 isl_set *sink_restr;
535 isl_map *res;
537 if (!acc->restrict_fn)
538 return isl_map_partial_lexmax(dep, sink, empty);
540 source_map = isl_map_copy(dep);
541 source_map = isl_map_apply_domain(source_map,
542 isl_map_copy(acc->domain_map));
543 sink_domain = isl_set_copy(sink);
544 sink_domain = isl_set_apply(sink_domain, isl_map_copy(acc->domain_map));
545 restr = acc->restrict_fn(source_map, sink_domain,
546 acc->source[source].data, acc->restrict_user);
547 isl_set_free(sink_domain);
548 isl_map_free(source_map);
550 if (!restr)
551 goto error;
552 if (restr->type == isl_restriction_type_input) {
553 dep = isl_map_intersect_range(dep, isl_set_copy(restr->source));
554 sink_restr = isl_set_copy(restr->sink);
555 sink_restr = isl_set_apply(sink_restr,
556 isl_map_reverse(isl_map_copy(acc->domain_map)));
557 sink = isl_set_intersect(sink, sink_restr);
558 } else if (restr->type == isl_restriction_type_empty) {
559 isl_space *space = isl_map_get_space(dep);
560 isl_map_free(dep);
561 dep = isl_map_empty(space);
564 res = isl_map_partial_lexmax(dep, sink, empty);
566 if (restr->type == isl_restriction_type_output)
567 res = isl_map_intersect_range(res, isl_set_copy(restr->source));
569 isl_restriction_free(restr);
570 return res;
571 error:
572 isl_map_free(dep);
573 isl_set_free(sink);
574 *empty = NULL;
575 return NULL;
578 /* Compute the last iteration of must source j that precedes the sink
579 * at the given level for sink iterations in set_C.
580 * The subset of set_C for which no such iteration can be found is returned
581 * in *empty.
583 static struct isl_map *last_source(struct isl_access_info *acc,
584 struct isl_set *set_C,
585 int j, int level, struct isl_set **empty)
587 struct isl_map *read_map;
588 struct isl_map *write_map;
589 struct isl_map *dep_map;
590 struct isl_map *after;
591 struct isl_map *result;
593 read_map = isl_map_copy(acc->sink.map);
594 write_map = isl_map_copy(acc->source[j].map);
595 write_map = isl_map_reverse(write_map);
596 dep_map = isl_map_apply_range(read_map, write_map);
597 after = after_at_level(isl_map_get_space(dep_map), level);
598 dep_map = isl_map_intersect(dep_map, after);
599 result = restricted_partial_lexmax(acc, dep_map, j, set_C, empty);
600 result = isl_map_reverse(result);
602 return result;
605 /* For a given mapping between iterations of must source j and iterations
606 * of the sink, compute the last iteration of must source k preceding
607 * the sink at level before_level for any of the sink iterations,
608 * but following the corresponding iteration of must source j at level
609 * after_level.
611 static struct isl_map *last_later_source(struct isl_access_info *acc,
612 struct isl_map *old_map,
613 int j, int before_level,
614 int k, int after_level,
615 struct isl_set **empty)
617 isl_space *dim;
618 struct isl_set *set_C;
619 struct isl_map *read_map;
620 struct isl_map *write_map;
621 struct isl_map *dep_map;
622 struct isl_map *after_write;
623 struct isl_map *before_read;
624 struct isl_map *result;
626 set_C = isl_map_range(isl_map_copy(old_map));
627 read_map = isl_map_copy(acc->sink.map);
628 write_map = isl_map_copy(acc->source[k].map);
630 write_map = isl_map_reverse(write_map);
631 dep_map = isl_map_apply_range(read_map, write_map);
632 dim = space_align_and_join(isl_map_get_space(acc->source[k].map),
633 isl_space_reverse(isl_map_get_space(acc->source[j].map)));
634 after_write = after_at_level(dim, after_level);
635 after_write = isl_map_apply_range(after_write, old_map);
636 after_write = isl_map_reverse(after_write);
637 dep_map = isl_map_intersect(dep_map, after_write);
638 before_read = after_at_level(isl_map_get_space(dep_map), before_level);
639 dep_map = isl_map_intersect(dep_map, before_read);
640 result = restricted_partial_lexmax(acc, dep_map, k, set_C, empty);
641 result = isl_map_reverse(result);
643 return result;
646 /* Given a shared_level between two accesses, return 1 if the
647 * the first can precede the second at the requested target_level.
648 * If the target level is odd, i.e., refers to a statement level
649 * dimension, then first needs to precede second at the requested
650 * level, i.e., shared_level must be equal to target_level.
651 * If the target level is odd, then the two loops should share
652 * at least the requested number of outer loops.
654 static int can_precede_at_level(int shared_level, int target_level)
656 if (shared_level < target_level)
657 return 0;
658 if ((target_level % 2) && shared_level > target_level)
659 return 0;
660 return 1;
663 /* Given a possible flow dependence temp_rel[j] between source j and the sink
664 * at level sink_level, remove those elements for which
665 * there is an iteration of another source k < j that is closer to the sink.
666 * The flow dependences temp_rel[k] are updated with the improved sources.
667 * Any improved source needs to precede the sink at the same level
668 * and needs to follow source j at the same or a deeper level.
669 * The lower this level, the later the execution date of source k.
670 * We therefore consider lower levels first.
672 * If temp_rel[j] is empty, then there can be no improvement and
673 * we return immediately.
675 static int intermediate_sources(__isl_keep isl_access_info *acc,
676 struct isl_map **temp_rel, int j, int sink_level)
678 int k, level;
679 int depth = 2 * isl_map_dim(acc->source[j].map, isl_dim_in) + 1;
681 if (isl_map_plain_is_empty(temp_rel[j]))
682 return 0;
684 for (k = j - 1; k >= 0; --k) {
685 int plevel, plevel2;
686 plevel = acc->level_before(acc->source[k].data, acc->sink.data);
687 if (!can_precede_at_level(plevel, sink_level))
688 continue;
690 plevel2 = acc->level_before(acc->source[j].data,
691 acc->source[k].data);
693 for (level = sink_level; level <= depth; ++level) {
694 struct isl_map *T;
695 struct isl_set *trest;
696 struct isl_map *copy;
698 if (!can_precede_at_level(plevel2, level))
699 continue;
701 copy = isl_map_copy(temp_rel[j]);
702 T = last_later_source(acc, copy, j, sink_level, k,
703 level, &trest);
704 if (isl_map_plain_is_empty(T)) {
705 isl_set_free(trest);
706 isl_map_free(T);
707 continue;
709 temp_rel[j] = isl_map_intersect_range(temp_rel[j], trest);
710 temp_rel[k] = isl_map_union_disjoint(temp_rel[k], T);
714 return 0;
717 /* Compute all iterations of may source j that precedes the sink at the given
718 * level for sink iterations in set_C.
720 static __isl_give isl_map *all_sources(__isl_keep isl_access_info *acc,
721 __isl_take isl_set *set_C, int j, int level)
723 isl_map *read_map;
724 isl_map *write_map;
725 isl_map *dep_map;
726 isl_map *after;
728 read_map = isl_map_copy(acc->sink.map);
729 read_map = isl_map_intersect_domain(read_map, set_C);
730 write_map = isl_map_copy(acc->source[acc->n_must + j].map);
731 write_map = isl_map_reverse(write_map);
732 dep_map = isl_map_apply_range(read_map, write_map);
733 after = after_at_level(isl_map_get_space(dep_map), level);
734 dep_map = isl_map_intersect(dep_map, after);
736 return isl_map_reverse(dep_map);
739 /* For a given mapping between iterations of must source k and iterations
740 * of the sink, compute the all iteration of may source j preceding
741 * the sink at level before_level for any of the sink iterations,
742 * but following the corresponding iteration of must source k at level
743 * after_level.
745 static __isl_give isl_map *all_later_sources(__isl_keep isl_access_info *acc,
746 __isl_take isl_map *old_map,
747 int j, int before_level, int k, int after_level)
749 isl_space *dim;
750 isl_set *set_C;
751 isl_map *read_map;
752 isl_map *write_map;
753 isl_map *dep_map;
754 isl_map *after_write;
755 isl_map *before_read;
757 set_C = isl_map_range(isl_map_copy(old_map));
758 read_map = isl_map_copy(acc->sink.map);
759 read_map = isl_map_intersect_domain(read_map, set_C);
760 write_map = isl_map_copy(acc->source[acc->n_must + j].map);
762 write_map = isl_map_reverse(write_map);
763 dep_map = isl_map_apply_range(read_map, write_map);
764 dim = isl_space_join(isl_map_get_space(acc->source[acc->n_must + j].map),
765 isl_space_reverse(isl_map_get_space(acc->source[k].map)));
766 after_write = after_at_level(dim, after_level);
767 after_write = isl_map_apply_range(after_write, old_map);
768 after_write = isl_map_reverse(after_write);
769 dep_map = isl_map_intersect(dep_map, after_write);
770 before_read = after_at_level(isl_map_get_space(dep_map), before_level);
771 dep_map = isl_map_intersect(dep_map, before_read);
772 return isl_map_reverse(dep_map);
775 /* Given the must and may dependence relations for the must accesses
776 * for level sink_level, check if there are any accesses of may access j
777 * that occur in between and return their union.
778 * If some of these accesses are intermediate with respect to
779 * (previously thought to be) must dependences, then these
780 * must dependences are turned into may dependences.
782 static __isl_give isl_map *all_intermediate_sources(
783 __isl_keep isl_access_info *acc, __isl_take isl_map *map,
784 struct isl_map **must_rel, struct isl_map **may_rel,
785 int j, int sink_level)
787 int k, level;
788 int depth = 2 * isl_map_dim(acc->source[acc->n_must + j].map,
789 isl_dim_in) + 1;
791 for (k = 0; k < acc->n_must; ++k) {
792 int plevel;
794 if (isl_map_plain_is_empty(may_rel[k]) &&
795 isl_map_plain_is_empty(must_rel[k]))
796 continue;
798 plevel = acc->level_before(acc->source[k].data,
799 acc->source[acc->n_must + j].data);
801 for (level = sink_level; level <= depth; ++level) {
802 isl_map *T;
803 isl_map *copy;
804 isl_set *ran;
806 if (!can_precede_at_level(plevel, level))
807 continue;
809 copy = isl_map_copy(may_rel[k]);
810 T = all_later_sources(acc, copy, j, sink_level, k, level);
811 map = isl_map_union(map, T);
813 copy = isl_map_copy(must_rel[k]);
814 T = all_later_sources(acc, copy, j, sink_level, k, level);
815 ran = isl_map_range(isl_map_copy(T));
816 map = isl_map_union(map, T);
817 may_rel[k] = isl_map_union_disjoint(may_rel[k],
818 isl_map_intersect_range(isl_map_copy(must_rel[k]),
819 isl_set_copy(ran)));
820 T = isl_map_from_domain_and_range(
821 isl_set_universe(
822 isl_space_domain(isl_map_get_space(must_rel[k]))),
823 ran);
824 must_rel[k] = isl_map_subtract(must_rel[k], T);
828 return map;
831 /* Compute dependences for the case where all accesses are "may"
832 * accesses, which boils down to computing memory based dependences.
833 * The generic algorithm would also work in this case, but it would
834 * be overkill to use it.
836 static __isl_give isl_flow *compute_mem_based_dependences(
837 __isl_keep isl_access_info *acc)
839 int i;
840 isl_set *mustdo;
841 isl_set *maydo;
842 isl_flow *res;
844 res = isl_flow_alloc(acc);
845 if (!res)
846 return NULL;
848 mustdo = isl_map_domain(isl_map_copy(acc->sink.map));
849 maydo = isl_set_copy(mustdo);
851 for (i = 0; i < acc->n_may; ++i) {
852 int plevel;
853 int is_before;
854 isl_space *dim;
855 isl_map *before;
856 isl_map *dep;
858 plevel = acc->level_before(acc->source[i].data, acc->sink.data);
859 is_before = plevel & 1;
860 plevel >>= 1;
862 dim = isl_map_get_space(res->dep[i].map);
863 if (is_before)
864 before = isl_map_lex_le_first(dim, plevel);
865 else
866 before = isl_map_lex_lt_first(dim, plevel);
867 dep = isl_map_apply_range(isl_map_copy(acc->source[i].map),
868 isl_map_reverse(isl_map_copy(acc->sink.map)));
869 dep = isl_map_intersect(dep, before);
870 mustdo = isl_set_subtract(mustdo,
871 isl_map_range(isl_map_copy(dep)));
872 res->dep[i].map = isl_map_union(res->dep[i].map, dep);
875 res->may_no_source = isl_set_subtract(maydo, isl_set_copy(mustdo));
876 res->must_no_source = mustdo;
878 return res;
881 /* Compute dependences for the case where there is at least one
882 * "must" access.
884 * The core algorithm considers all levels in which a source may precede
885 * the sink, where a level may either be a statement level or a loop level.
886 * The outermost statement level is 1, the first loop level is 2, etc...
887 * The algorithm basically does the following:
888 * for all levels l of the read access from innermost to outermost
889 * for all sources w that may precede the sink access at that level
890 * compute the last iteration of the source that precedes the sink access
891 * at that level
892 * add result to possible last accesses at level l of source w
893 * for all sources w2 that we haven't considered yet at this level that may
894 * also precede the sink access
895 * for all levels l2 of w from l to innermost
896 * for all possible last accesses dep of w at l
897 * compute last iteration of w2 between the source and sink
898 * of dep
899 * add result to possible last accesses at level l of write w2
900 * and replace possible last accesses dep by the remainder
903 * The above algorithm is applied to the must access. During the course
904 * of the algorithm, we keep track of sink iterations that still
905 * need to be considered. These iterations are split into those that
906 * haven't been matched to any source access (mustdo) and those that have only
907 * been matched to may accesses (maydo).
908 * At the end of each level, we also consider the may accesses.
909 * In particular, we consider may accesses that precede the remaining
910 * sink iterations, moving elements from mustdo to maydo when appropriate,
911 * and may accesses that occur between a must source and a sink of any
912 * dependences found at the current level, turning must dependences into
913 * may dependences when appropriate.
916 static __isl_give isl_flow *compute_val_based_dependences(
917 __isl_keep isl_access_info *acc)
919 isl_ctx *ctx;
920 isl_flow *res;
921 isl_set *mustdo = NULL;
922 isl_set *maydo = NULL;
923 int level, j;
924 int depth;
925 isl_map **must_rel = NULL;
926 isl_map **may_rel = NULL;
928 if (!acc)
929 return NULL;
931 res = isl_flow_alloc(acc);
932 if (!res)
933 goto error;
934 ctx = isl_map_get_ctx(acc->sink.map);
936 depth = 2 * isl_map_dim(acc->sink.map, isl_dim_in) + 1;
937 mustdo = isl_map_domain(isl_map_copy(acc->sink.map));
938 maydo = isl_set_empty(isl_set_get_space(mustdo));
939 if (!mustdo || !maydo)
940 goto error;
941 if (isl_set_plain_is_empty(mustdo))
942 goto done;
944 must_rel = isl_alloc_array(ctx, struct isl_map *, acc->n_must);
945 may_rel = isl_alloc_array(ctx, struct isl_map *, acc->n_must);
946 if (!must_rel || !may_rel)
947 goto error;
949 for (level = depth; level >= 1; --level) {
950 for (j = acc->n_must-1; j >=0; --j) {
951 isl_space *space;
952 space = isl_map_get_space(res->dep[2 * j].map);
953 must_rel[j] = isl_map_empty(space);
954 may_rel[j] = isl_map_copy(must_rel[j]);
957 for (j = acc->n_must - 1; j >= 0; --j) {
958 struct isl_map *T;
959 struct isl_set *rest;
960 int plevel;
962 plevel = acc->level_before(acc->source[j].data,
963 acc->sink.data);
964 if (!can_precede_at_level(plevel, level))
965 continue;
967 T = last_source(acc, mustdo, j, level, &rest);
968 must_rel[j] = isl_map_union_disjoint(must_rel[j], T);
969 mustdo = rest;
971 intermediate_sources(acc, must_rel, j, level);
973 T = last_source(acc, maydo, j, level, &rest);
974 may_rel[j] = isl_map_union_disjoint(may_rel[j], T);
975 maydo = rest;
977 intermediate_sources(acc, may_rel, j, level);
979 if (isl_set_plain_is_empty(mustdo) &&
980 isl_set_plain_is_empty(maydo))
981 break;
983 for (j = j - 1; j >= 0; --j) {
984 int plevel;
986 plevel = acc->level_before(acc->source[j].data,
987 acc->sink.data);
988 if (!can_precede_at_level(plevel, level))
989 continue;
991 intermediate_sources(acc, must_rel, j, level);
992 intermediate_sources(acc, may_rel, j, level);
995 for (j = 0; j < acc->n_may; ++j) {
996 int plevel;
997 isl_map *T;
998 isl_set *ran;
1000 plevel = acc->level_before(acc->source[acc->n_must + j].data,
1001 acc->sink.data);
1002 if (!can_precede_at_level(plevel, level))
1003 continue;
1005 T = all_sources(acc, isl_set_copy(maydo), j, level);
1006 res->dep[2 * acc->n_must + j].map =
1007 isl_map_union(res->dep[2 * acc->n_must + j].map, T);
1008 T = all_sources(acc, isl_set_copy(mustdo), j, level);
1009 ran = isl_map_range(isl_map_copy(T));
1010 res->dep[2 * acc->n_must + j].map =
1011 isl_map_union(res->dep[2 * acc->n_must + j].map, T);
1012 mustdo = isl_set_subtract(mustdo, isl_set_copy(ran));
1013 maydo = isl_set_union_disjoint(maydo, ran);
1015 T = res->dep[2 * acc->n_must + j].map;
1016 T = all_intermediate_sources(acc, T, must_rel, may_rel,
1017 j, level);
1018 res->dep[2 * acc->n_must + j].map = T;
1021 for (j = acc->n_must - 1; j >= 0; --j) {
1022 res->dep[2 * j].map =
1023 isl_map_union_disjoint(res->dep[2 * j].map,
1024 must_rel[j]);
1025 res->dep[2 * j + 1].map =
1026 isl_map_union_disjoint(res->dep[2 * j + 1].map,
1027 may_rel[j]);
1030 if (isl_set_plain_is_empty(mustdo) &&
1031 isl_set_plain_is_empty(maydo))
1032 break;
1035 free(must_rel);
1036 free(may_rel);
1037 done:
1038 res->must_no_source = mustdo;
1039 res->may_no_source = maydo;
1040 return res;
1041 error:
1042 isl_flow_free(res);
1043 isl_set_free(mustdo);
1044 isl_set_free(maydo);
1045 free(must_rel);
1046 free(may_rel);
1047 return NULL;
1050 /* Given a "sink" access, a list of n "source" accesses,
1051 * compute for each iteration of the sink access
1052 * and for each element accessed by that iteration,
1053 * the source access in the list that last accessed the
1054 * element accessed by the sink access before this sink access.
1055 * Each access is given as a map from the loop iterators
1056 * to the array indices.
1057 * The result is a list of n relations between source and sink
1058 * iterations and a subset of the domain of the sink access,
1059 * corresponding to those iterations that access an element
1060 * not previously accessed.
1062 * To deal with multi-valued sink access relations, the sink iteration
1063 * domain is first extended with dimensions that correspond to the data
1064 * space. After the computation is finished, these extra dimensions are
1065 * projected out again.
1067 __isl_give isl_flow *isl_access_info_compute_flow(__isl_take isl_access_info *acc)
1069 int j;
1070 struct isl_flow *res = NULL;
1072 if (!acc)
1073 return NULL;
1075 acc->domain_map = isl_map_domain_map(isl_map_copy(acc->sink.map));
1076 acc->sink.map = isl_map_range_map(acc->sink.map);
1077 if (!acc->sink.map)
1078 goto error;
1080 if (acc->n_must == 0)
1081 res = compute_mem_based_dependences(acc);
1082 else {
1083 acc = isl_access_info_sort_sources(acc);
1084 res = compute_val_based_dependences(acc);
1086 if (!res)
1087 goto error;
1089 for (j = 0; j < res->n_source; ++j) {
1090 res->dep[j].map = isl_map_apply_range(res->dep[j].map,
1091 isl_map_copy(acc->domain_map));
1092 if (!res->dep[j].map)
1093 goto error;
1095 if (!res->must_no_source || !res->may_no_source)
1096 goto error;
1098 isl_access_info_free(acc);
1099 return res;
1100 error:
1101 isl_access_info_free(acc);
1102 isl_flow_free(res);
1103 return NULL;
1107 /* Keep track of some information about a schedule for a given
1108 * access. In particular, keep track of which dimensions
1109 * have a constant value and of the actual constant values.
1111 struct isl_sched_info {
1112 int *is_cst;
1113 isl_vec *cst;
1116 static void sched_info_free(__isl_take struct isl_sched_info *info)
1118 if (!info)
1119 return;
1120 isl_vec_free(info->cst);
1121 free(info->is_cst);
1122 free(info);
1125 /* Extract information on the constant dimensions of the schedule
1126 * for a given access. The "map" is of the form
1128 * [S -> D] -> A
1130 * with S the schedule domain, D the iteration domain and A the data domain.
1132 static __isl_give struct isl_sched_info *sched_info_alloc(
1133 __isl_keep isl_map *map)
1135 isl_ctx *ctx;
1136 isl_space *dim;
1137 struct isl_sched_info *info;
1138 int i, n;
1140 if (!map)
1141 return NULL;
1143 dim = isl_space_unwrap(isl_space_domain(isl_map_get_space(map)));
1144 if (!dim)
1145 return NULL;
1146 n = isl_space_dim(dim, isl_dim_in);
1147 isl_space_free(dim);
1149 ctx = isl_map_get_ctx(map);
1150 info = isl_alloc_type(ctx, struct isl_sched_info);
1151 if (!info)
1152 return NULL;
1153 info->is_cst = isl_alloc_array(ctx, int, n);
1154 info->cst = isl_vec_alloc(ctx, n);
1155 if (n && (!info->is_cst || !info->cst))
1156 goto error;
1158 for (i = 0; i < n; ++i) {
1159 isl_val *v;
1161 v = isl_map_plain_get_val_if_fixed(map, isl_dim_in, i);
1162 if (!v)
1163 goto error;
1164 info->is_cst[i] = !isl_val_is_nan(v);
1165 if (info->is_cst[i])
1166 info->cst = isl_vec_set_element_val(info->cst, i, v);
1167 else
1168 isl_val_free(v);
1171 return info;
1172 error:
1173 sched_info_free(info);
1174 return NULL;
1177 /* This structure represents the input for a dependence analysis computation.
1179 * "sink" represents the sink accesses.
1180 * "must_source" represents the definite source accesses.
1181 * "may_source" represents the possible source accesses.
1183 * "schedule" or "schedule_map" represents the execution order.
1184 * Exactly one of these fields should be NULL. The other field
1185 * determines the execution order.
1187 * The domains of these four maps refer to the same iteration spaces(s).
1188 * The ranges of the first three maps also refer to the same data space(s).
1190 * After a call to isl_union_access_info_introduce_schedule,
1191 * the "schedule_map" field no longer contains useful information.
1193 struct isl_union_access_info {
1194 isl_union_map *sink;
1195 isl_union_map *must_source;
1196 isl_union_map *may_source;
1198 isl_schedule *schedule;
1199 isl_union_map *schedule_map;
1202 /* Free "access" and return NULL.
1204 __isl_null isl_union_access_info *isl_union_access_info_free(
1205 __isl_take isl_union_access_info *access)
1207 if (!access)
1208 return NULL;
1210 isl_union_map_free(access->sink);
1211 isl_union_map_free(access->must_source);
1212 isl_union_map_free(access->may_source);
1213 isl_schedule_free(access->schedule);
1214 isl_union_map_free(access->schedule_map);
1215 free(access);
1217 return NULL;
1220 /* Return the isl_ctx to which "access" belongs.
1222 isl_ctx *isl_union_access_info_get_ctx(__isl_keep isl_union_access_info *access)
1224 return access ? isl_union_map_get_ctx(access->sink) : NULL;
1227 /* Create a new isl_union_access_info with the given sink accesses and
1228 * and no source accesses or schedule information.
1230 * By default, we use the schedule field of the isl_union_access_info,
1231 * but this may be overridden by a call
1232 * to isl_union_access_info_set_schedule_map.
1234 __isl_give isl_union_access_info *isl_union_access_info_from_sink(
1235 __isl_take isl_union_map *sink)
1237 isl_ctx *ctx;
1238 isl_space *space;
1239 isl_union_map *empty;
1240 isl_union_access_info *access;
1242 if (!sink)
1243 return NULL;
1244 ctx = isl_union_map_get_ctx(sink);
1245 access = isl_alloc_type(ctx, isl_union_access_info);
1246 if (!access)
1247 goto error;
1249 space = isl_union_map_get_space(sink);
1250 empty = isl_union_map_empty(isl_space_copy(space));
1251 access->sink = sink;
1252 access->must_source = isl_union_map_copy(empty);
1253 access->may_source = empty;
1254 access->schedule = isl_schedule_empty(space);
1255 access->schedule_map = NULL;
1257 if (!access->sink || !access->must_source ||
1258 !access->may_source || !access->schedule)
1259 return isl_union_access_info_free(access);
1261 return access;
1262 error:
1263 isl_union_map_free(sink);
1264 return NULL;
1267 /* Replace the definite source accesses of "access" by "must_source".
1269 __isl_give isl_union_access_info *isl_union_access_info_set_must_source(
1270 __isl_take isl_union_access_info *access,
1271 __isl_take isl_union_map *must_source)
1273 if (!access || !must_source)
1274 goto error;
1276 isl_union_map_free(access->must_source);
1277 access->must_source = must_source;
1279 return access;
1280 error:
1281 isl_union_access_info_free(access);
1282 isl_union_map_free(must_source);
1283 return NULL;
1286 /* Replace the possible source accesses of "access" by "may_source".
1288 __isl_give isl_union_access_info *isl_union_access_info_set_may_source(
1289 __isl_take isl_union_access_info *access,
1290 __isl_take isl_union_map *may_source)
1292 if (!access || !may_source)
1293 goto error;
1295 isl_union_map_free(access->may_source);
1296 access->may_source = may_source;
1298 return access;
1299 error:
1300 isl_union_access_info_free(access);
1301 isl_union_map_free(may_source);
1302 return NULL;
1305 /* Replace the schedule of "access" by "schedule".
1306 * Also free the schedule_map in case it was set last.
1308 __isl_give isl_union_access_info *isl_union_access_info_set_schedule(
1309 __isl_take isl_union_access_info *access,
1310 __isl_take isl_schedule *schedule)
1312 if (!access || !schedule)
1313 goto error;
1315 access->schedule_map = isl_union_map_free(access->schedule_map);
1316 isl_schedule_free(access->schedule);
1317 access->schedule = schedule;
1319 return access;
1320 error:
1321 isl_union_access_info_free(access);
1322 isl_schedule_free(schedule);
1323 return NULL;
1326 /* Replace the schedule map of "access" by "schedule_map".
1327 * Also free the schedule in case it was set last.
1329 __isl_give isl_union_access_info *isl_union_access_info_set_schedule_map(
1330 __isl_take isl_union_access_info *access,
1331 __isl_take isl_union_map *schedule_map)
1333 if (!access || !schedule_map)
1334 goto error;
1336 isl_union_map_free(access->schedule_map);
1337 access->schedule = isl_schedule_free(access->schedule);
1338 access->schedule_map = schedule_map;
1340 return access;
1341 error:
1342 isl_union_access_info_free(access);
1343 isl_union_map_free(schedule_map);
1344 return NULL;
1347 /* Update the fields of "access" such that they all have the same parameters,
1348 * keeping in mind that the schedule_map field may be NULL and ignoring
1349 * the schedule field.
1351 static __isl_give isl_union_access_info *isl_union_access_info_align_params(
1352 __isl_take isl_union_access_info *access)
1354 isl_space *space;
1356 if (!access)
1357 return NULL;
1359 space = isl_union_map_get_space(access->sink);
1360 space = isl_space_align_params(space,
1361 isl_union_map_get_space(access->must_source));
1362 space = isl_space_align_params(space,
1363 isl_union_map_get_space(access->may_source));
1364 if (access->schedule_map)
1365 space = isl_space_align_params(space,
1366 isl_union_map_get_space(access->schedule_map));
1367 access->sink = isl_union_map_align_params(access->sink,
1368 isl_space_copy(space));
1369 access->must_source = isl_union_map_align_params(access->must_source,
1370 isl_space_copy(space));
1371 access->may_source = isl_union_map_align_params(access->may_source,
1372 isl_space_copy(space));
1373 if (!access->schedule_map) {
1374 isl_space_free(space);
1375 } else {
1376 access->schedule_map =
1377 isl_union_map_align_params(access->schedule_map, space);
1378 if (!access->schedule_map)
1379 return isl_union_access_info_free(access);
1382 if (!access->sink || !access->must_source || !access->may_source)
1383 return isl_union_access_info_free(access);
1385 return access;
1388 /* Prepend the schedule dimensions to the iteration domains.
1390 * That is, if the schedule is of the form
1392 * D -> S
1394 * while the access relations are of the form
1396 * D -> A
1398 * then the updated access relations are of the form
1400 * [S -> D] -> A
1402 * The schedule map is also replaced by the map
1404 * [S -> D] -> D
1406 * that is used during the internal computation.
1407 * Neither the original schedule map nor this updated schedule map
1408 * are used after the call to this function.
1410 static __isl_give isl_union_access_info *
1411 isl_union_access_info_introduce_schedule(
1412 __isl_take isl_union_access_info *access)
1414 isl_union_map *sm;
1416 if (!access)
1417 return NULL;
1419 sm = isl_union_map_reverse(access->schedule_map);
1420 sm = isl_union_map_range_map(sm);
1421 access->sink = isl_union_map_apply_range(isl_union_map_copy(sm),
1422 access->sink);
1423 access->may_source = isl_union_map_apply_range(isl_union_map_copy(sm),
1424 access->may_source);
1425 access->must_source = isl_union_map_apply_range(isl_union_map_copy(sm),
1426 access->must_source);
1427 access->schedule_map = sm;
1429 if (!access->sink || !access->must_source ||
1430 !access->may_source || !access->schedule_map)
1431 return isl_union_access_info_free(access);
1433 return access;
1436 /* This structure epresents the result of a dependence analysis computation.
1438 * "must_dep" represents the definite dependences.
1439 * "may_dep" represents the non-definite dependences.
1440 * "must_no_source" represents the subset of the sink accesses for which
1441 * definitely no source was found.
1442 * "may_no_source" represents the subset of the sink accesses for which
1443 * possibly, but not definitely, no source was found.
1445 struct isl_union_flow {
1446 isl_union_map *must_dep;
1447 isl_union_map *may_dep;
1448 isl_union_map *must_no_source;
1449 isl_union_map *may_no_source;
1452 /* Free "flow" and return NULL.
1454 __isl_null isl_union_flow *isl_union_flow_free(__isl_take isl_union_flow *flow)
1456 if (!flow)
1457 return NULL;
1458 isl_union_map_free(flow->must_dep);
1459 isl_union_map_free(flow->may_dep);
1460 isl_union_map_free(flow->must_no_source);
1461 isl_union_map_free(flow->may_no_source);
1462 free(flow);
1463 return NULL;
1466 void isl_union_flow_dump(__isl_keep isl_union_flow *flow)
1468 if (!flow)
1469 return;
1471 fprintf(stderr, "must dependences: ");
1472 isl_union_map_dump(flow->must_dep);
1473 fprintf(stderr, "may dependences: ");
1474 isl_union_map_dump(flow->may_dep);
1475 fprintf(stderr, "must no source: ");
1476 isl_union_map_dump(flow->must_no_source);
1477 fprintf(stderr, "may no source: ");
1478 isl_union_map_dump(flow->may_no_source);
1481 /* Return the definite dependences in "flow".
1483 __isl_give isl_union_map *isl_union_flow_get_must_dependence(
1484 __isl_keep isl_union_flow *flow)
1486 if (!flow)
1487 return NULL;
1488 return isl_union_map_copy(flow->must_dep);
1491 /* Return the possible dependences in "flow", including the definite
1492 * dependences.
1494 __isl_give isl_union_map *isl_union_flow_get_may_dependence(
1495 __isl_keep isl_union_flow *flow)
1497 if (!flow)
1498 return NULL;
1499 return isl_union_map_union(isl_union_map_copy(flow->must_dep),
1500 isl_union_map_copy(flow->may_dep));
1503 /* Return the non-definite dependences in "flow".
1505 static __isl_give isl_union_map *isl_union_flow_get_non_must_dependence(
1506 __isl_keep isl_union_flow *flow)
1508 if (!flow)
1509 return NULL;
1510 return isl_union_map_copy(flow->may_dep);
1513 /* Return the subset of the sink accesses for which definitely
1514 * no source was found.
1516 __isl_give isl_union_map *isl_union_flow_get_must_no_source(
1517 __isl_keep isl_union_flow *flow)
1519 if (!flow)
1520 return NULL;
1521 return isl_union_map_copy(flow->must_no_source);
1524 /* Return the subset of the sink accesses for which possibly
1525 * no source was found, including those for which definitely
1526 * no source was found.
1528 __isl_give isl_union_map *isl_union_flow_get_may_no_source(
1529 __isl_keep isl_union_flow *flow)
1531 if (!flow)
1532 return NULL;
1533 return isl_union_map_union(isl_union_map_copy(flow->must_no_source),
1534 isl_union_map_copy(flow->may_no_source));
1537 /* Return the subset of the sink accesses for which possibly, but not
1538 * definitely, no source was found.
1540 static __isl_give isl_union_map *isl_union_flow_get_non_must_no_source(
1541 __isl_keep isl_union_flow *flow)
1543 if (!flow)
1544 return NULL;
1545 return isl_union_map_copy(flow->may_no_source);
1548 /* Create a new isl_union_flow object, initialized with empty
1549 * dependence relations and sink subsets.
1551 static __isl_give isl_union_flow *isl_union_flow_alloc(
1552 __isl_take isl_space *space)
1554 isl_ctx *ctx;
1555 isl_union_map *empty;
1556 isl_union_flow *flow;
1558 if (!space)
1559 return NULL;
1560 ctx = isl_space_get_ctx(space);
1561 flow = isl_alloc_type(ctx, isl_union_flow);
1562 if (!flow)
1563 goto error;
1565 empty = isl_union_map_empty(space);
1566 flow->must_dep = isl_union_map_copy(empty);
1567 flow->may_dep = isl_union_map_copy(empty);
1568 flow->must_no_source = isl_union_map_copy(empty);
1569 flow->may_no_source = empty;
1571 if (!flow->must_dep || !flow->may_dep ||
1572 !flow->must_no_source || !flow->may_no_source)
1573 return isl_union_flow_free(flow);
1575 return flow;
1576 error:
1577 isl_space_free(space);
1578 return NULL;
1581 /* Drop the schedule dimensions from the iteration domains in "flow".
1582 * In particular, the schedule dimensions have been prepended
1583 * to the iteration domains prior to the dependence analysis by
1584 * replacing the iteration domain D, by the wrapped map [S -> D].
1585 * Replace these wrapped maps by the original D.
1587 static __isl_give isl_union_flow *isl_union_flow_drop_schedule(
1588 __isl_take isl_union_flow *flow)
1590 if (!flow)
1591 return NULL;
1593 flow->must_dep = isl_union_map_factor_range(flow->must_dep);
1594 flow->may_dep = isl_union_map_factor_range(flow->may_dep);
1595 flow->must_no_source =
1596 isl_union_map_domain_factor_range(flow->must_no_source);
1597 flow->may_no_source =
1598 isl_union_map_domain_factor_range(flow->may_no_source);
1600 if (!flow->must_dep || !flow->may_dep ||
1601 !flow->must_no_source || !flow->may_no_source)
1602 return isl_union_flow_free(flow);
1604 return flow;
1607 struct isl_compute_flow_data {
1608 isl_union_map *must_source;
1609 isl_union_map *may_source;
1610 isl_union_flow *flow;
1612 int count;
1613 int must;
1614 isl_space *dim;
1615 struct isl_sched_info *sink_info;
1616 struct isl_sched_info **source_info;
1617 isl_access_info *accesses;
1620 static int count_matching_array(__isl_take isl_map *map, void *user)
1622 int eq;
1623 isl_space *dim;
1624 struct isl_compute_flow_data *data;
1626 data = (struct isl_compute_flow_data *)user;
1628 dim = isl_space_range(isl_map_get_space(map));
1630 eq = isl_space_is_equal(dim, data->dim);
1632 isl_space_free(dim);
1633 isl_map_free(map);
1635 if (eq < 0)
1636 return -1;
1637 if (eq)
1638 data->count++;
1640 return 0;
1643 static int collect_matching_array(__isl_take isl_map *map, void *user)
1645 int eq;
1646 isl_space *dim;
1647 struct isl_sched_info *info;
1648 struct isl_compute_flow_data *data;
1650 data = (struct isl_compute_flow_data *)user;
1652 dim = isl_space_range(isl_map_get_space(map));
1654 eq = isl_space_is_equal(dim, data->dim);
1656 isl_space_free(dim);
1658 if (eq < 0)
1659 goto error;
1660 if (!eq) {
1661 isl_map_free(map);
1662 return 0;
1665 info = sched_info_alloc(map);
1666 data->source_info[data->count] = info;
1668 data->accesses = isl_access_info_add_source(data->accesses,
1669 map, data->must, info);
1671 data->count++;
1673 return 0;
1674 error:
1675 isl_map_free(map);
1676 return -1;
1679 /* Determine the shared nesting level and the "textual order" of
1680 * the given accesses.
1682 * We first determine the minimal schedule dimension for both accesses.
1684 * If among those dimensions, we can find one where both have a fixed
1685 * value and if moreover those values are different, then the previous
1686 * dimension is the last shared nesting level and the textual order
1687 * is determined based on the order of the fixed values.
1688 * If no such fixed values can be found, then we set the shared
1689 * nesting level to the minimal schedule dimension, with no textual ordering.
1691 static int before(void *first, void *second)
1693 struct isl_sched_info *info1 = first;
1694 struct isl_sched_info *info2 = second;
1695 int n1, n2;
1696 int i;
1698 n1 = isl_vec_size(info1->cst);
1699 n2 = isl_vec_size(info2->cst);
1701 if (n2 < n1)
1702 n1 = n2;
1704 for (i = 0; i < n1; ++i) {
1705 int r;
1706 int cmp;
1708 if (!info1->is_cst[i])
1709 continue;
1710 if (!info2->is_cst[i])
1711 continue;
1712 cmp = isl_vec_cmp_element(info1->cst, info2->cst, i);
1713 if (cmp == 0)
1714 continue;
1716 r = 2 * i + (cmp < 0);
1718 return r;
1721 return 2 * n1;
1724 /* Given a sink access, look for all the source accesses that access
1725 * the same array and perform dataflow analysis on them using
1726 * isl_access_info_compute_flow.
1728 static int compute_flow(__isl_take isl_map *map, void *user)
1730 int i;
1731 isl_ctx *ctx;
1732 struct isl_compute_flow_data *data;
1733 isl_flow *flow;
1734 isl_union_flow *df;
1736 data = (struct isl_compute_flow_data *)user;
1737 df = data->flow;
1739 ctx = isl_map_get_ctx(map);
1741 data->accesses = NULL;
1742 data->sink_info = NULL;
1743 data->source_info = NULL;
1744 data->count = 0;
1745 data->dim = isl_space_range(isl_map_get_space(map));
1747 if (isl_union_map_foreach_map(data->must_source,
1748 &count_matching_array, data) < 0)
1749 goto error;
1750 if (isl_union_map_foreach_map(data->may_source,
1751 &count_matching_array, data) < 0)
1752 goto error;
1754 data->sink_info = sched_info_alloc(map);
1755 data->source_info = isl_calloc_array(ctx, struct isl_sched_info *,
1756 data->count);
1758 data->accesses = isl_access_info_alloc(isl_map_copy(map),
1759 data->sink_info, &before, data->count);
1760 if (!data->sink_info || (data->count && !data->source_info) ||
1761 !data->accesses)
1762 goto error;
1763 data->count = 0;
1764 data->must = 1;
1765 if (isl_union_map_foreach_map(data->must_source,
1766 &collect_matching_array, data) < 0)
1767 goto error;
1768 data->must = 0;
1769 if (isl_union_map_foreach_map(data->may_source,
1770 &collect_matching_array, data) < 0)
1771 goto error;
1773 flow = isl_access_info_compute_flow(data->accesses);
1774 data->accesses = NULL;
1776 if (!flow)
1777 goto error;
1779 df->must_no_source = isl_union_map_union(df->must_no_source,
1780 isl_union_map_from_map(isl_flow_get_no_source(flow, 1)));
1781 df->may_no_source = isl_union_map_union(df->may_no_source,
1782 isl_union_map_from_map(isl_flow_get_no_source(flow, 0)));
1784 for (i = 0; i < flow->n_source; ++i) {
1785 isl_union_map *dep;
1786 dep = isl_union_map_from_map(isl_map_copy(flow->dep[i].map));
1787 if (flow->dep[i].must)
1788 df->must_dep = isl_union_map_union(df->must_dep, dep);
1789 else
1790 df->may_dep = isl_union_map_union(df->may_dep, dep);
1793 isl_flow_free(flow);
1795 sched_info_free(data->sink_info);
1796 if (data->source_info) {
1797 for (i = 0; i < data->count; ++i)
1798 sched_info_free(data->source_info[i]);
1799 free(data->source_info);
1801 isl_space_free(data->dim);
1802 isl_map_free(map);
1804 return 0;
1805 error:
1806 isl_access_info_free(data->accesses);
1807 sched_info_free(data->sink_info);
1808 if (data->source_info) {
1809 for (i = 0; i < data->count; ++i)
1810 sched_info_free(data->source_info[i]);
1811 free(data->source_info);
1813 isl_space_free(data->dim);
1814 isl_map_free(map);
1816 return -1;
1819 /* Remove the must accesses from the may accesses.
1821 * A must access always trumps a may access, so there is no need
1822 * for a must access to also be considered as a may access. Doing so
1823 * would only cost extra computations only to find out that
1824 * the duplicated may access does not make any difference.
1826 static __isl_give isl_union_access_info *isl_union_access_info_normalize(
1827 __isl_take isl_union_access_info *access)
1829 if (!access)
1830 return NULL;
1831 access->may_source = isl_union_map_subtract(access->may_source,
1832 isl_union_map_copy(access->must_source));
1833 if (!access->may_source)
1834 return isl_union_access_info_free(access);
1836 return access;
1839 /* Given a description of the "sink" accesses, the "source" accesses and
1840 * a schedule, compute for each instance of a sink access
1841 * and for each element accessed by that instance,
1842 * the possible or definite source accesses that last accessed the
1843 * element accessed by the sink access before this sink access
1844 * in the sense that there is no intermediate definite source access.
1846 * The must_no_source and may_no_source elements of the result
1847 * are subsets of access->sink. The elements must_dep and may_dep
1848 * map domain elements of access->{may,must)_source to
1849 * domain elements of access->sink.
1851 * This function is used when only the schedule map representation
1852 * is available.
1854 * We first prepend the schedule dimensions to the domain
1855 * of the accesses so that we can easily compare their relative order.
1856 * Then we consider each sink access individually in compute_flow.
1858 static __isl_give isl_union_flow *compute_flow_union_map(
1859 __isl_take isl_union_access_info *access)
1861 struct isl_compute_flow_data data;
1863 access = isl_union_access_info_align_params(access);
1864 access = isl_union_access_info_introduce_schedule(access);
1865 if (!access)
1866 return NULL;
1868 data.must_source = access->must_source;
1869 data.may_source = access->may_source;
1871 data.flow = isl_union_flow_alloc(isl_union_map_get_space(access->sink));
1873 if (isl_union_map_foreach_map(access->sink, &compute_flow, &data) < 0)
1874 goto error;
1876 data.flow = isl_union_flow_drop_schedule(data.flow);
1878 isl_union_access_info_free(access);
1879 return data.flow;
1880 error:
1881 isl_union_access_info_free(access);
1882 isl_union_flow_free(data.flow);
1883 return NULL;
1886 /* A schedule access relation.
1888 * The access relation "access" is of the form [S -> D] -> A,
1889 * where S corresponds to the prefix schedule at "node".
1890 * "must" is only relevant for source accesses and indicates
1891 * whether the access is a must source or a may source.
1893 struct isl_scheduled_access {
1894 isl_map *access;
1895 int must;
1896 isl_schedule_node *node;
1899 /* Data structure for keeping track of individual scheduled sink and source
1900 * accesses when computing dependence analysis based on a schedule tree.
1902 * "n_sink" is the number of used entries in "sink"
1903 * "n_source" is the number of used entries in "source"
1905 * "set_sink", "must" and "node" are only used inside collect_sink_source,
1906 * to keep track of the current node and
1907 * of what extract_sink_source needs to do.
1909 struct isl_compute_flow_schedule_data {
1910 isl_union_access_info *access;
1912 int n_sink;
1913 int n_source;
1915 struct isl_scheduled_access *sink;
1916 struct isl_scheduled_access *source;
1918 int set_sink;
1919 int must;
1920 isl_schedule_node *node;
1923 /* Align the parameters of all sinks with all sources.
1925 * If there are no sinks or no sources, then no alignment is needed.
1927 static void isl_compute_flow_schedule_data_align_params(
1928 struct isl_compute_flow_schedule_data *data)
1930 int i;
1931 isl_space *space;
1933 if (data->n_sink == 0 || data->n_source == 0)
1934 return;
1936 space = isl_map_get_space(data->sink[0].access);
1938 for (i = 1; i < data->n_sink; ++i)
1939 space = isl_space_align_params(space,
1940 isl_map_get_space(data->sink[i].access));
1941 for (i = 0; i < data->n_source; ++i)
1942 space = isl_space_align_params(space,
1943 isl_map_get_space(data->source[i].access));
1945 for (i = 0; i < data->n_sink; ++i)
1946 data->sink[i].access =
1947 isl_map_align_params(data->sink[i].access,
1948 isl_space_copy(space));
1949 for (i = 0; i < data->n_source; ++i)
1950 data->source[i].access =
1951 isl_map_align_params(data->source[i].access,
1952 isl_space_copy(space));
1954 isl_space_free(space);
1957 /* Free all the memory referenced from "data".
1958 * Do not free "data" itself as it may be allocated on the stack.
1960 static void isl_compute_flow_schedule_data_clear(
1961 struct isl_compute_flow_schedule_data *data)
1963 int i;
1965 if (!data->sink)
1966 return;
1968 for (i = 0; i < data->n_sink; ++i) {
1969 isl_map_free(data->sink[i].access);
1970 isl_schedule_node_free(data->sink[i].node);
1973 for (i = 0; i < data->n_source; ++i) {
1974 isl_map_free(data->source[i].access);
1975 isl_schedule_node_free(data->source[i].node);
1978 free(data->sink);
1981 /* isl_schedule_foreach_schedule_node callback for counting
1982 * (an upper bound on) the number of sinks and sources.
1984 * Sinks and sources are only extracted at leaves of the tree,
1985 * so we skip the node if it is not a leaf.
1986 * Otherwise we increment data->n_sink and data->n_source with
1987 * the number of spaces in the sink and source access domains
1988 * that reach this node.
1990 static int count_sink_source(__isl_keep isl_schedule_node *node, void *user)
1992 struct isl_compute_flow_schedule_data *data = user;
1993 isl_union_set *domain;
1994 isl_union_map *umap;
1995 int r = 0;
1997 if (isl_schedule_node_get_type(node) != isl_schedule_node_leaf)
1998 return 1;
2000 domain = isl_schedule_node_get_universe_domain(node);
2002 umap = isl_union_map_copy(data->access->sink);
2003 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
2004 data->n_sink += isl_union_map_n_map(umap);
2005 isl_union_map_free(umap);
2006 if (!umap)
2007 r = -1;
2009 umap = isl_union_map_copy(data->access->must_source);
2010 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
2011 data->n_source += isl_union_map_n_map(umap);
2012 isl_union_map_free(umap);
2013 if (!umap)
2014 r = -1;
2016 umap = isl_union_map_copy(data->access->may_source);
2017 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
2018 data->n_source += isl_union_map_n_map(umap);
2019 isl_union_map_free(umap);
2020 if (!umap)
2021 r = -1;
2023 isl_union_set_free(domain);
2025 return r;
2028 /* Add a single scheduled sink or source (depending on data->set_sink)
2029 * with scheduled access relation "map", must property data->must and
2030 * schedule node data->node to the list of sinks or sources.
2032 static int extract_sink_source(__isl_take isl_map *map, void *user)
2034 struct isl_compute_flow_schedule_data *data = user;
2035 struct isl_scheduled_access *access;
2037 if (data->set_sink)
2038 access = data->sink + data->n_sink++;
2039 else
2040 access = data->source + data->n_source++;
2042 access->access = map;
2043 access->must = data->must;
2044 access->node = isl_schedule_node_copy(data->node);
2046 return 0;
2049 /* isl_schedule_foreach_schedule_node callback for collecting
2050 * individual scheduled source and sink accesses.
2052 * We only collect accesses at the leaves of the schedule tree.
2053 * We prepend the schedule dimensions at the leaf to the iteration
2054 * domains of the source and sink accesses and then extract
2055 * the individual accesses (per space).
2057 * In particular, if the prefix schedule at the node is of the form
2059 * D -> S
2061 * while the access relations are of the form
2063 * D -> A
2065 * then the updated access relations are of the form
2067 * [S -> D] -> A
2069 * Note that S consists of a single space such that introducing S
2070 * in the access relations does not increase the number of spaces.
2072 static int collect_sink_source(__isl_keep isl_schedule_node *node, void *user)
2074 struct isl_compute_flow_schedule_data *data = user;
2075 isl_union_map *prefix;
2076 isl_union_map *umap;
2077 int r = 0;
2079 if (isl_schedule_node_get_type(node) != isl_schedule_node_leaf)
2080 return 1;
2082 data->node = node;
2084 prefix = isl_schedule_node_get_prefix_schedule_union_map(node);
2085 prefix = isl_union_map_reverse(prefix);
2086 prefix = isl_union_map_range_map(prefix);
2088 data->set_sink = 1;
2089 umap = isl_union_map_copy(data->access->sink);
2090 umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
2091 if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
2092 r = -1;
2093 isl_union_map_free(umap);
2095 data->set_sink = 0;
2096 data->must = 1;
2097 umap = isl_union_map_copy(data->access->must_source);
2098 umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
2099 if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
2100 r = -1;
2101 isl_union_map_free(umap);
2103 data->set_sink = 0;
2104 data->must = 0;
2105 umap = isl_union_map_copy(data->access->may_source);
2106 umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
2107 if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
2108 r = -1;
2109 isl_union_map_free(umap);
2111 isl_union_map_free(prefix);
2113 return r;
2116 /* isl_access_info_compute_flow callback for determining whether
2117 * the shared nesting level and the ordering within that level
2118 * for two scheduled accesses for use in compute_single_flow.
2120 * The tokens passed to this function refer to the leaves
2121 * in the schedule tree where the accesses take place.
2123 * If n is the shared number of loops, then we need to return
2124 * "2 * n + 1" if "first" precedes "second" inside the innermost
2125 * shared loop and "2 * n" otherwise.
2127 * The innermost shared ancestor may be the leaves themselves
2128 * if the accesses take place in the same leaf. Otherwise,
2129 * it is either a set node or a sequence node. Only in the case
2130 * of a sequence node do we consider one access to precede the other.
2132 static int before_node(void *first, void *second)
2134 isl_schedule_node *node1 = first;
2135 isl_schedule_node *node2 = second;
2136 isl_schedule_node *shared;
2137 int depth;
2138 int before = 0;
2140 shared = isl_schedule_node_get_shared_ancestor(node1, node2);
2141 if (!shared)
2142 return -1;
2144 depth = isl_schedule_node_get_schedule_depth(shared);
2145 if (isl_schedule_node_get_type(shared) == isl_schedule_node_sequence) {
2146 int pos1, pos2;
2148 pos1 = isl_schedule_node_get_ancestor_child_position(node1,
2149 shared);
2150 pos2 = isl_schedule_node_get_ancestor_child_position(node2,
2151 shared);
2152 before = pos1 < pos2;
2155 isl_schedule_node_free(shared);
2157 return 2 * depth + before;
2160 /* Add the scheduled sources from "data" that access
2161 * the same data space as "sink" to "access".
2163 static __isl_give isl_access_info *add_matching_sources(
2164 __isl_take isl_access_info *access, struct isl_scheduled_access *sink,
2165 struct isl_compute_flow_schedule_data *data)
2167 int i;
2168 isl_space *space;
2170 space = isl_space_range(isl_map_get_space(sink->access));
2171 for (i = 0; i < data->n_source; ++i) {
2172 struct isl_scheduled_access *source;
2173 isl_space *source_space;
2174 int eq;
2176 source = &data->source[i];
2177 source_space = isl_map_get_space(source->access);
2178 source_space = isl_space_range(source_space);
2179 eq = isl_space_is_equal(space, source_space);
2180 isl_space_free(source_space);
2182 if (!eq)
2183 continue;
2184 if (eq < 0)
2185 goto error;
2187 access = isl_access_info_add_source(access,
2188 isl_map_copy(source->access), source->must, source->node);
2191 isl_space_free(space);
2192 return access;
2193 error:
2194 isl_space_free(space);
2195 isl_access_info_free(access);
2196 return NULL;
2199 /* Given a scheduled sink access relation "sink", compute the corresponding
2200 * dependences on the sources in "data" and add the computed dependences
2201 * to "uf".
2203 static __isl_give isl_union_flow *compute_single_flow(
2204 __isl_take isl_union_flow *uf, struct isl_scheduled_access *sink,
2205 struct isl_compute_flow_schedule_data *data)
2207 int i;
2208 isl_access_info *access;
2209 isl_flow *flow;
2210 isl_map *map;
2212 if (!uf)
2213 return NULL;
2215 access = isl_access_info_alloc(isl_map_copy(sink->access), sink->node,
2216 &before_node, data->n_source);
2217 access = add_matching_sources(access, sink, data);
2219 flow = isl_access_info_compute_flow(access);
2220 if (!flow)
2221 return isl_union_flow_free(uf);
2223 map = isl_map_domain_factor_range(isl_flow_get_no_source(flow, 1));
2224 uf->must_no_source = isl_union_map_union(uf->must_no_source,
2225 isl_union_map_from_map(map));
2226 map = isl_map_domain_factor_range(isl_flow_get_no_source(flow, 0));
2227 uf->may_no_source = isl_union_map_union(uf->may_no_source,
2228 isl_union_map_from_map(map));
2230 for (i = 0; i < flow->n_source; ++i) {
2231 isl_union_map *dep;
2233 map = isl_map_factor_range(isl_map_copy(flow->dep[i].map));
2234 dep = isl_union_map_from_map(map);
2235 if (flow->dep[i].must)
2236 uf->must_dep = isl_union_map_union(uf->must_dep, dep);
2237 else
2238 uf->may_dep = isl_union_map_union(uf->may_dep, dep);
2241 isl_flow_free(flow);
2243 return uf;
2246 /* Given a description of the "sink" accesses, the "source" accesses and
2247 * a schedule, compute for each instance of a sink access
2248 * and for each element accessed by that instance,
2249 * the possible or definite source accesses that last accessed the
2250 * element accessed by the sink access before this sink access
2251 * in the sense that there is no intermediate definite source access.
2253 * The must_no_source and may_no_source elements of the result
2254 * are subsets of access->sink. The elements must_dep and may_dep
2255 * map domain elements of access->{may,must)_source to
2256 * domain elements of access->sink.
2258 * This function is used when a schedule tree representation
2259 * is available.
2261 * We extract the individual scheduled source and sink access relations and
2262 * then compute dependences for each scheduled sink individually.
2264 static __isl_give isl_union_flow *compute_flow_schedule(
2265 __isl_take isl_union_access_info *access)
2267 struct isl_compute_flow_schedule_data data = { access };
2268 int i, n;
2269 isl_ctx *ctx;
2270 isl_union_flow *flow;
2272 ctx = isl_union_access_info_get_ctx(access);
2274 data.n_sink = 0;
2275 data.n_source = 0;
2276 if (isl_schedule_foreach_schedule_node(access->schedule,
2277 &count_sink_source, &data) < 0)
2278 goto error;
2280 n = data.n_sink + data.n_source;
2281 data.sink = isl_calloc_array(ctx, struct isl_scheduled_access, n);
2282 if (n && !data.sink)
2283 goto error;
2284 data.source = data.sink + data.n_sink;
2286 data.n_sink = 0;
2287 data.n_source = 0;
2288 if (isl_schedule_foreach_schedule_node(access->schedule,
2289 &collect_sink_source, &data) < 0)
2290 goto error;
2292 flow = isl_union_flow_alloc(isl_union_map_get_space(access->sink));
2294 isl_compute_flow_schedule_data_align_params(&data);
2296 for (i = 0; i < data.n_sink; ++i)
2297 flow = compute_single_flow(flow, &data.sink[i], &data);
2299 isl_compute_flow_schedule_data_clear(&data);
2301 isl_union_access_info_free(access);
2302 return flow;
2303 error:
2304 isl_union_access_info_free(access);
2305 isl_compute_flow_schedule_data_clear(&data);
2306 return NULL;
2309 /* Given a description of the "sink" accesses, the "source" accesses and
2310 * a schedule, compute for each instance of a sink access
2311 * and for each element accessed by that instance,
2312 * the possible or definite source accesses that last accessed the
2313 * element accessed by the sink access before this sink access
2314 * in the sense that there is no intermediate definite source access.
2316 * The must_no_source and may_no_source elements of the result
2317 * are subsets of access->sink. The elements must_dep and may_dep
2318 * map domain elements of access->{may,must)_source to
2319 * domain elements of access->sink.
2321 * We check whether the schedule is available as a schedule tree
2322 * or a schedule map and call the correpsonding function to perform
2323 * the analysis.
2325 __isl_give isl_union_flow *isl_union_access_info_compute_flow(
2326 __isl_take isl_union_access_info *access)
2328 access = isl_union_access_info_normalize(access);
2329 if (!access)
2330 return NULL;
2331 if (access->schedule)
2332 return compute_flow_schedule(access);
2333 else
2334 return compute_flow_union_map(access);
2337 /* Given a collection of "sink" and "source" accesses,
2338 * compute for each iteration of a sink access
2339 * and for each element accessed by that iteration,
2340 * the source access in the list that last accessed the
2341 * element accessed by the sink access before this sink access.
2342 * Each access is given as a map from the loop iterators
2343 * to the array indices.
2344 * The result is a relations between source and sink
2345 * iterations and a subset of the domain of the sink accesses,
2346 * corresponding to those iterations that access an element
2347 * not previously accessed.
2349 * We collect the inputs in an isl_union_access_info object,
2350 * call isl_union_access_info_compute_flow and extract
2351 * the outputs from the result.
2353 int isl_union_map_compute_flow(__isl_take isl_union_map *sink,
2354 __isl_take isl_union_map *must_source,
2355 __isl_take isl_union_map *may_source,
2356 __isl_take isl_union_map *schedule,
2357 __isl_give isl_union_map **must_dep, __isl_give isl_union_map **may_dep,
2358 __isl_give isl_union_map **must_no_source,
2359 __isl_give isl_union_map **may_no_source)
2361 isl_union_access_info *access;
2362 isl_union_flow *flow;
2364 access = isl_union_access_info_from_sink(sink);
2365 access = isl_union_access_info_set_must_source(access, must_source);
2366 access = isl_union_access_info_set_may_source(access, may_source);
2367 access = isl_union_access_info_set_schedule_map(access, schedule);
2368 flow = isl_union_access_info_compute_flow(access);
2370 if (must_dep)
2371 *must_dep = isl_union_flow_get_must_dependence(flow);
2372 if (may_dep)
2373 *may_dep = isl_union_flow_get_non_must_dependence(flow);
2374 if (must_no_source)
2375 *must_no_source = isl_union_flow_get_must_no_source(flow);
2376 if (may_no_source)
2377 *may_no_source = isl_union_flow_get_non_must_no_source(flow);
2379 isl_union_flow_free(flow);
2381 if ((must_dep && !*must_dep) || (may_dep && !*may_dep) ||
2382 (must_no_source && !*must_no_source) ||
2383 (may_no_source && !*may_no_source))
2384 goto error;
2386 return 0;
2387 error:
2388 if (must_dep)
2389 *must_dep = isl_union_map_free(*must_dep);
2390 if (may_dep)
2391 *may_dep = isl_union_map_free(*may_dep);
2392 if (must_no_source)
2393 *must_no_source = isl_union_map_free(*must_no_source);
2394 if (may_no_source)
2395 *may_no_source = isl_union_map_free(*may_no_source);
2396 return -1;