hide isl_map_add_basic_map
[isl.git] / isl_flow.c
blob374a50754092d5902258194cf01b342da1da593a
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 isl_stat isl_flow_foreach(__isl_keep isl_flow *deps,
440 isl_stat (*fn)(__isl_take isl_map *dep, int must, void *dep_user,
441 void *user),
442 void *user)
444 int i;
446 if (!deps)
447 return isl_stat_error;
449 for (i = 0; i < deps->n_source; ++i) {
450 if (isl_map_plain_is_empty(deps->dep[i].map))
451 continue;
452 if (fn(isl_map_copy(deps->dep[i].map), deps->dep[i].must,
453 deps->dep[i].data, user) < 0)
454 return isl_stat_error;
457 return isl_stat_ok;
460 /* Return a copy of the subset of the sink for which no source could be found.
462 __isl_give isl_map *isl_flow_get_no_source(__isl_keep isl_flow *deps, int must)
464 if (!deps)
465 return NULL;
467 if (must)
468 return isl_set_unwrap(isl_set_copy(deps->must_no_source));
469 else
470 return isl_set_unwrap(isl_set_copy(deps->may_no_source));
473 void isl_flow_free(__isl_take isl_flow *deps)
475 int i;
477 if (!deps)
478 return;
479 isl_set_free(deps->must_no_source);
480 isl_set_free(deps->may_no_source);
481 if (deps->dep) {
482 for (i = 0; i < deps->n_source; ++i)
483 isl_map_free(deps->dep[i].map);
484 free(deps->dep);
486 free(deps);
489 isl_ctx *isl_flow_get_ctx(__isl_keep isl_flow *deps)
491 return deps ? isl_set_get_ctx(deps->must_no_source) : NULL;
494 /* Return a map that enforces that the domain iteration occurs after
495 * the range iteration at the given level.
496 * If level is odd, then the domain iteration should occur after
497 * the target iteration in their shared level/2 outermost loops.
498 * In this case we simply need to enforce that these outermost
499 * loop iterations are the same.
500 * If level is even, then the loop iterator of the domain should
501 * be greater than the loop iterator of the range at the last
502 * of the level/2 shared loops, i.e., loop level/2 - 1.
504 static __isl_give isl_map *after_at_level(__isl_take isl_space *dim, int level)
506 struct isl_basic_map *bmap;
508 if (level % 2)
509 bmap = isl_basic_map_equal(dim, level/2);
510 else
511 bmap = isl_basic_map_more_at(dim, level/2 - 1);
513 return isl_map_from_basic_map(bmap);
516 /* Compute the partial lexicographic maximum of "dep" on domain "sink",
517 * but first check if the user has set acc->restrict_fn and if so
518 * update either the input or the output of the maximization problem
519 * with respect to the resulting restriction.
521 * Since the user expects a mapping from sink iterations to source iterations,
522 * whereas the domain of "dep" is a wrapped map, mapping sink iterations
523 * to accessed array elements, we first need to project out the accessed
524 * sink array elements by applying acc->domain_map.
525 * Similarly, the sink restriction specified by the user needs to be
526 * converted back to the wrapped map.
528 static __isl_give isl_map *restricted_partial_lexmax(
529 __isl_keep isl_access_info *acc, __isl_take isl_map *dep,
530 int source, __isl_take isl_set *sink, __isl_give isl_set **empty)
532 isl_map *source_map;
533 isl_restriction *restr;
534 isl_set *sink_domain;
535 isl_set *sink_restr;
536 isl_map *res;
538 if (!acc->restrict_fn)
539 return isl_map_partial_lexmax(dep, sink, empty);
541 source_map = isl_map_copy(dep);
542 source_map = isl_map_apply_domain(source_map,
543 isl_map_copy(acc->domain_map));
544 sink_domain = isl_set_copy(sink);
545 sink_domain = isl_set_apply(sink_domain, isl_map_copy(acc->domain_map));
546 restr = acc->restrict_fn(source_map, sink_domain,
547 acc->source[source].data, acc->restrict_user);
548 isl_set_free(sink_domain);
549 isl_map_free(source_map);
551 if (!restr)
552 goto error;
553 if (restr->type == isl_restriction_type_input) {
554 dep = isl_map_intersect_range(dep, isl_set_copy(restr->source));
555 sink_restr = isl_set_copy(restr->sink);
556 sink_restr = isl_set_apply(sink_restr,
557 isl_map_reverse(isl_map_copy(acc->domain_map)));
558 sink = isl_set_intersect(sink, sink_restr);
559 } else if (restr->type == isl_restriction_type_empty) {
560 isl_space *space = isl_map_get_space(dep);
561 isl_map_free(dep);
562 dep = isl_map_empty(space);
565 res = isl_map_partial_lexmax(dep, sink, empty);
567 if (restr->type == isl_restriction_type_output)
568 res = isl_map_intersect_range(res, isl_set_copy(restr->source));
570 isl_restriction_free(restr);
571 return res;
572 error:
573 isl_map_free(dep);
574 isl_set_free(sink);
575 *empty = NULL;
576 return NULL;
579 /* Compute the last iteration of must source j that precedes the sink
580 * at the given level for sink iterations in set_C.
581 * The subset of set_C for which no such iteration can be found is returned
582 * in *empty.
584 static struct isl_map *last_source(struct isl_access_info *acc,
585 struct isl_set *set_C,
586 int j, int level, struct isl_set **empty)
588 struct isl_map *read_map;
589 struct isl_map *write_map;
590 struct isl_map *dep_map;
591 struct isl_map *after;
592 struct isl_map *result;
594 read_map = isl_map_copy(acc->sink.map);
595 write_map = isl_map_copy(acc->source[j].map);
596 write_map = isl_map_reverse(write_map);
597 dep_map = isl_map_apply_range(read_map, write_map);
598 after = after_at_level(isl_map_get_space(dep_map), level);
599 dep_map = isl_map_intersect(dep_map, after);
600 result = restricted_partial_lexmax(acc, dep_map, j, set_C, empty);
601 result = isl_map_reverse(result);
603 return result;
606 /* For a given mapping between iterations of must source j and iterations
607 * of the sink, compute the last iteration of must source k preceding
608 * the sink at level before_level for any of the sink iterations,
609 * but following the corresponding iteration of must source j at level
610 * after_level.
612 static struct isl_map *last_later_source(struct isl_access_info *acc,
613 struct isl_map *old_map,
614 int j, int before_level,
615 int k, int after_level,
616 struct isl_set **empty)
618 isl_space *dim;
619 struct isl_set *set_C;
620 struct isl_map *read_map;
621 struct isl_map *write_map;
622 struct isl_map *dep_map;
623 struct isl_map *after_write;
624 struct isl_map *before_read;
625 struct isl_map *result;
627 set_C = isl_map_range(isl_map_copy(old_map));
628 read_map = isl_map_copy(acc->sink.map);
629 write_map = isl_map_copy(acc->source[k].map);
631 write_map = isl_map_reverse(write_map);
632 dep_map = isl_map_apply_range(read_map, write_map);
633 dim = space_align_and_join(isl_map_get_space(acc->source[k].map),
634 isl_space_reverse(isl_map_get_space(acc->source[j].map)));
635 after_write = after_at_level(dim, after_level);
636 after_write = isl_map_apply_range(after_write, old_map);
637 after_write = isl_map_reverse(after_write);
638 dep_map = isl_map_intersect(dep_map, after_write);
639 before_read = after_at_level(isl_map_get_space(dep_map), before_level);
640 dep_map = isl_map_intersect(dep_map, before_read);
641 result = restricted_partial_lexmax(acc, dep_map, k, set_C, empty);
642 result = isl_map_reverse(result);
644 return result;
647 /* Given a shared_level between two accesses, return 1 if the
648 * the first can precede the second at the requested target_level.
649 * If the target level is odd, i.e., refers to a statement level
650 * dimension, then first needs to precede second at the requested
651 * level, i.e., shared_level must be equal to target_level.
652 * If the target level is odd, then the two loops should share
653 * at least the requested number of outer loops.
655 static int can_precede_at_level(int shared_level, int target_level)
657 if (shared_level < target_level)
658 return 0;
659 if ((target_level % 2) && shared_level > target_level)
660 return 0;
661 return 1;
664 /* Given a possible flow dependence temp_rel[j] between source j and the sink
665 * at level sink_level, remove those elements for which
666 * there is an iteration of another source k < j that is closer to the sink.
667 * The flow dependences temp_rel[k] are updated with the improved sources.
668 * Any improved source needs to precede the sink at the same level
669 * and needs to follow source j at the same or a deeper level.
670 * The lower this level, the later the execution date of source k.
671 * We therefore consider lower levels first.
673 * If temp_rel[j] is empty, then there can be no improvement and
674 * we return immediately.
676 static int intermediate_sources(__isl_keep isl_access_info *acc,
677 struct isl_map **temp_rel, int j, int sink_level)
679 int k, level;
680 int depth = 2 * isl_map_dim(acc->source[j].map, isl_dim_in) + 1;
682 if (isl_map_plain_is_empty(temp_rel[j]))
683 return 0;
685 for (k = j - 1; k >= 0; --k) {
686 int plevel, plevel2;
687 plevel = acc->level_before(acc->source[k].data, acc->sink.data);
688 if (!can_precede_at_level(plevel, sink_level))
689 continue;
691 plevel2 = acc->level_before(acc->source[j].data,
692 acc->source[k].data);
694 for (level = sink_level; level <= depth; ++level) {
695 struct isl_map *T;
696 struct isl_set *trest;
697 struct isl_map *copy;
699 if (!can_precede_at_level(plevel2, level))
700 continue;
702 copy = isl_map_copy(temp_rel[j]);
703 T = last_later_source(acc, copy, j, sink_level, k,
704 level, &trest);
705 if (isl_map_plain_is_empty(T)) {
706 isl_set_free(trest);
707 isl_map_free(T);
708 continue;
710 temp_rel[j] = isl_map_intersect_range(temp_rel[j], trest);
711 temp_rel[k] = isl_map_union_disjoint(temp_rel[k], T);
715 return 0;
718 /* Compute all iterations of may source j that precedes the sink at the given
719 * level for sink iterations in set_C.
721 static __isl_give isl_map *all_sources(__isl_keep isl_access_info *acc,
722 __isl_take isl_set *set_C, int j, int level)
724 isl_map *read_map;
725 isl_map *write_map;
726 isl_map *dep_map;
727 isl_map *after;
729 read_map = isl_map_copy(acc->sink.map);
730 read_map = isl_map_intersect_domain(read_map, set_C);
731 write_map = isl_map_copy(acc->source[acc->n_must + j].map);
732 write_map = isl_map_reverse(write_map);
733 dep_map = isl_map_apply_range(read_map, write_map);
734 after = after_at_level(isl_map_get_space(dep_map), level);
735 dep_map = isl_map_intersect(dep_map, after);
737 return isl_map_reverse(dep_map);
740 /* For a given mapping between iterations of must source k and iterations
741 * of the sink, compute the all iteration of may source j preceding
742 * the sink at level before_level for any of the sink iterations,
743 * but following the corresponding iteration of must source k at level
744 * after_level.
746 static __isl_give isl_map *all_later_sources(__isl_keep isl_access_info *acc,
747 __isl_take isl_map *old_map,
748 int j, int before_level, int k, int after_level)
750 isl_space *dim;
751 isl_set *set_C;
752 isl_map *read_map;
753 isl_map *write_map;
754 isl_map *dep_map;
755 isl_map *after_write;
756 isl_map *before_read;
758 set_C = isl_map_range(isl_map_copy(old_map));
759 read_map = isl_map_copy(acc->sink.map);
760 read_map = isl_map_intersect_domain(read_map, set_C);
761 write_map = isl_map_copy(acc->source[acc->n_must + j].map);
763 write_map = isl_map_reverse(write_map);
764 dep_map = isl_map_apply_range(read_map, write_map);
765 dim = isl_space_join(isl_map_get_space(acc->source[acc->n_must + j].map),
766 isl_space_reverse(isl_map_get_space(acc->source[k].map)));
767 after_write = after_at_level(dim, after_level);
768 after_write = isl_map_apply_range(after_write, old_map);
769 after_write = isl_map_reverse(after_write);
770 dep_map = isl_map_intersect(dep_map, after_write);
771 before_read = after_at_level(isl_map_get_space(dep_map), before_level);
772 dep_map = isl_map_intersect(dep_map, before_read);
773 return isl_map_reverse(dep_map);
776 /* Given the must and may dependence relations for the must accesses
777 * for level sink_level, check if there are any accesses of may access j
778 * that occur in between and return their union.
779 * If some of these accesses are intermediate with respect to
780 * (previously thought to be) must dependences, then these
781 * must dependences are turned into may dependences.
783 static __isl_give isl_map *all_intermediate_sources(
784 __isl_keep isl_access_info *acc, __isl_take isl_map *map,
785 struct isl_map **must_rel, struct isl_map **may_rel,
786 int j, int sink_level)
788 int k, level;
789 int depth = 2 * isl_map_dim(acc->source[acc->n_must + j].map,
790 isl_dim_in) + 1;
792 for (k = 0; k < acc->n_must; ++k) {
793 int plevel;
795 if (isl_map_plain_is_empty(may_rel[k]) &&
796 isl_map_plain_is_empty(must_rel[k]))
797 continue;
799 plevel = acc->level_before(acc->source[k].data,
800 acc->source[acc->n_must + j].data);
802 for (level = sink_level; level <= depth; ++level) {
803 isl_map *T;
804 isl_map *copy;
805 isl_set *ran;
807 if (!can_precede_at_level(plevel, level))
808 continue;
810 copy = isl_map_copy(may_rel[k]);
811 T = all_later_sources(acc, copy, j, sink_level, k, level);
812 map = isl_map_union(map, T);
814 copy = isl_map_copy(must_rel[k]);
815 T = all_later_sources(acc, copy, j, sink_level, k, level);
816 ran = isl_map_range(isl_map_copy(T));
817 map = isl_map_union(map, T);
818 may_rel[k] = isl_map_union_disjoint(may_rel[k],
819 isl_map_intersect_range(isl_map_copy(must_rel[k]),
820 isl_set_copy(ran)));
821 T = isl_map_from_domain_and_range(
822 isl_set_universe(
823 isl_space_domain(isl_map_get_space(must_rel[k]))),
824 ran);
825 must_rel[k] = isl_map_subtract(must_rel[k], T);
829 return map;
832 /* Compute dependences for the case where all accesses are "may"
833 * accesses, which boils down to computing memory based dependences.
834 * The generic algorithm would also work in this case, but it would
835 * be overkill to use it.
837 static __isl_give isl_flow *compute_mem_based_dependences(
838 __isl_keep isl_access_info *acc)
840 int i;
841 isl_set *mustdo;
842 isl_set *maydo;
843 isl_flow *res;
845 res = isl_flow_alloc(acc);
846 if (!res)
847 return NULL;
849 mustdo = isl_map_domain(isl_map_copy(acc->sink.map));
850 maydo = isl_set_copy(mustdo);
852 for (i = 0; i < acc->n_may; ++i) {
853 int plevel;
854 int is_before;
855 isl_space *dim;
856 isl_map *before;
857 isl_map *dep;
859 plevel = acc->level_before(acc->source[i].data, acc->sink.data);
860 is_before = plevel & 1;
861 plevel >>= 1;
863 dim = isl_map_get_space(res->dep[i].map);
864 if (is_before)
865 before = isl_map_lex_le_first(dim, plevel);
866 else
867 before = isl_map_lex_lt_first(dim, plevel);
868 dep = isl_map_apply_range(isl_map_copy(acc->source[i].map),
869 isl_map_reverse(isl_map_copy(acc->sink.map)));
870 dep = isl_map_intersect(dep, before);
871 mustdo = isl_set_subtract(mustdo,
872 isl_map_range(isl_map_copy(dep)));
873 res->dep[i].map = isl_map_union(res->dep[i].map, dep);
876 res->may_no_source = isl_set_subtract(maydo, isl_set_copy(mustdo));
877 res->must_no_source = mustdo;
879 return res;
882 /* Compute dependences for the case where there is at least one
883 * "must" access.
885 * The core algorithm considers all levels in which a source may precede
886 * the sink, where a level may either be a statement level or a loop level.
887 * The outermost statement level is 1, the first loop level is 2, etc...
888 * The algorithm basically does the following:
889 * for all levels l of the read access from innermost to outermost
890 * for all sources w that may precede the sink access at that level
891 * compute the last iteration of the source that precedes the sink access
892 * at that level
893 * add result to possible last accesses at level l of source w
894 * for all sources w2 that we haven't considered yet at this level that may
895 * also precede the sink access
896 * for all levels l2 of w from l to innermost
897 * for all possible last accesses dep of w at l
898 * compute last iteration of w2 between the source and sink
899 * of dep
900 * add result to possible last accesses at level l of write w2
901 * and replace possible last accesses dep by the remainder
904 * The above algorithm is applied to the must access. During the course
905 * of the algorithm, we keep track of sink iterations that still
906 * need to be considered. These iterations are split into those that
907 * haven't been matched to any source access (mustdo) and those that have only
908 * been matched to may accesses (maydo).
909 * At the end of each level, we also consider the may accesses.
910 * In particular, we consider may accesses that precede the remaining
911 * sink iterations, moving elements from mustdo to maydo when appropriate,
912 * and may accesses that occur between a must source and a sink of any
913 * dependences found at the current level, turning must dependences into
914 * may dependences when appropriate.
917 static __isl_give isl_flow *compute_val_based_dependences(
918 __isl_keep isl_access_info *acc)
920 isl_ctx *ctx;
921 isl_flow *res;
922 isl_set *mustdo = NULL;
923 isl_set *maydo = NULL;
924 int level, j;
925 int depth;
926 isl_map **must_rel = NULL;
927 isl_map **may_rel = NULL;
929 if (!acc)
930 return NULL;
932 res = isl_flow_alloc(acc);
933 if (!res)
934 goto error;
935 ctx = isl_map_get_ctx(acc->sink.map);
937 depth = 2 * isl_map_dim(acc->sink.map, isl_dim_in) + 1;
938 mustdo = isl_map_domain(isl_map_copy(acc->sink.map));
939 maydo = isl_set_empty(isl_set_get_space(mustdo));
940 if (!mustdo || !maydo)
941 goto error;
942 if (isl_set_plain_is_empty(mustdo))
943 goto done;
945 must_rel = isl_alloc_array(ctx, struct isl_map *, acc->n_must);
946 may_rel = isl_alloc_array(ctx, struct isl_map *, acc->n_must);
947 if (!must_rel || !may_rel)
948 goto error;
950 for (level = depth; level >= 1; --level) {
951 for (j = acc->n_must-1; j >=0; --j) {
952 isl_space *space;
953 space = isl_map_get_space(res->dep[2 * j].map);
954 must_rel[j] = isl_map_empty(space);
955 may_rel[j] = isl_map_copy(must_rel[j]);
958 for (j = acc->n_must - 1; j >= 0; --j) {
959 struct isl_map *T;
960 struct isl_set *rest;
961 int plevel;
963 plevel = acc->level_before(acc->source[j].data,
964 acc->sink.data);
965 if (!can_precede_at_level(plevel, level))
966 continue;
968 T = last_source(acc, mustdo, j, level, &rest);
969 must_rel[j] = isl_map_union_disjoint(must_rel[j], T);
970 mustdo = rest;
972 intermediate_sources(acc, must_rel, j, level);
974 T = last_source(acc, maydo, j, level, &rest);
975 may_rel[j] = isl_map_union_disjoint(may_rel[j], T);
976 maydo = rest;
978 intermediate_sources(acc, may_rel, j, level);
980 if (isl_set_plain_is_empty(mustdo) &&
981 isl_set_plain_is_empty(maydo))
982 break;
984 for (j = j - 1; j >= 0; --j) {
985 int plevel;
987 plevel = acc->level_before(acc->source[j].data,
988 acc->sink.data);
989 if (!can_precede_at_level(plevel, level))
990 continue;
992 intermediate_sources(acc, must_rel, j, level);
993 intermediate_sources(acc, may_rel, j, level);
996 for (j = 0; j < acc->n_may; ++j) {
997 int plevel;
998 isl_map *T;
999 isl_set *ran;
1001 plevel = acc->level_before(acc->source[acc->n_must + j].data,
1002 acc->sink.data);
1003 if (!can_precede_at_level(plevel, level))
1004 continue;
1006 T = all_sources(acc, isl_set_copy(maydo), j, level);
1007 res->dep[2 * acc->n_must + j].map =
1008 isl_map_union(res->dep[2 * acc->n_must + j].map, T);
1009 T = all_sources(acc, isl_set_copy(mustdo), j, level);
1010 ran = isl_map_range(isl_map_copy(T));
1011 res->dep[2 * acc->n_must + j].map =
1012 isl_map_union(res->dep[2 * acc->n_must + j].map, T);
1013 mustdo = isl_set_subtract(mustdo, isl_set_copy(ran));
1014 maydo = isl_set_union_disjoint(maydo, ran);
1016 T = res->dep[2 * acc->n_must + j].map;
1017 T = all_intermediate_sources(acc, T, must_rel, may_rel,
1018 j, level);
1019 res->dep[2 * acc->n_must + j].map = T;
1022 for (j = acc->n_must - 1; j >= 0; --j) {
1023 res->dep[2 * j].map =
1024 isl_map_union_disjoint(res->dep[2 * j].map,
1025 must_rel[j]);
1026 res->dep[2 * j + 1].map =
1027 isl_map_union_disjoint(res->dep[2 * j + 1].map,
1028 may_rel[j]);
1031 if (isl_set_plain_is_empty(mustdo) &&
1032 isl_set_plain_is_empty(maydo))
1033 break;
1036 free(must_rel);
1037 free(may_rel);
1038 done:
1039 res->must_no_source = mustdo;
1040 res->may_no_source = maydo;
1041 return res;
1042 error:
1043 isl_flow_free(res);
1044 isl_set_free(mustdo);
1045 isl_set_free(maydo);
1046 free(must_rel);
1047 free(may_rel);
1048 return NULL;
1051 /* Given a "sink" access, a list of n "source" accesses,
1052 * compute for each iteration of the sink access
1053 * and for each element accessed by that iteration,
1054 * the source access in the list that last accessed the
1055 * element accessed by the sink access before this sink access.
1056 * Each access is given as a map from the loop iterators
1057 * to the array indices.
1058 * The result is a list of n relations between source and sink
1059 * iterations and a subset of the domain of the sink access,
1060 * corresponding to those iterations that access an element
1061 * not previously accessed.
1063 * To deal with multi-valued sink access relations, the sink iteration
1064 * domain is first extended with dimensions that correspond to the data
1065 * space. After the computation is finished, these extra dimensions are
1066 * projected out again.
1068 __isl_give isl_flow *isl_access_info_compute_flow(__isl_take isl_access_info *acc)
1070 int j;
1071 struct isl_flow *res = NULL;
1073 if (!acc)
1074 return NULL;
1076 acc->domain_map = isl_map_domain_map(isl_map_copy(acc->sink.map));
1077 acc->sink.map = isl_map_range_map(acc->sink.map);
1078 if (!acc->sink.map)
1079 goto error;
1081 if (acc->n_must == 0)
1082 res = compute_mem_based_dependences(acc);
1083 else {
1084 acc = isl_access_info_sort_sources(acc);
1085 res = compute_val_based_dependences(acc);
1087 if (!res)
1088 goto error;
1090 for (j = 0; j < res->n_source; ++j) {
1091 res->dep[j].map = isl_map_apply_range(res->dep[j].map,
1092 isl_map_copy(acc->domain_map));
1093 if (!res->dep[j].map)
1094 goto error;
1096 if (!res->must_no_source || !res->may_no_source)
1097 goto error;
1099 isl_access_info_free(acc);
1100 return res;
1101 error:
1102 isl_access_info_free(acc);
1103 isl_flow_free(res);
1104 return NULL;
1108 /* Keep track of some information about a schedule for a given
1109 * access. In particular, keep track of which dimensions
1110 * have a constant value and of the actual constant values.
1112 struct isl_sched_info {
1113 int *is_cst;
1114 isl_vec *cst;
1117 static void sched_info_free(__isl_take struct isl_sched_info *info)
1119 if (!info)
1120 return;
1121 isl_vec_free(info->cst);
1122 free(info->is_cst);
1123 free(info);
1126 /* Extract information on the constant dimensions of the schedule
1127 * for a given access. The "map" is of the form
1129 * [S -> D] -> A
1131 * with S the schedule domain, D the iteration domain and A the data domain.
1133 static __isl_give struct isl_sched_info *sched_info_alloc(
1134 __isl_keep isl_map *map)
1136 isl_ctx *ctx;
1137 isl_space *dim;
1138 struct isl_sched_info *info;
1139 int i, n;
1141 if (!map)
1142 return NULL;
1144 dim = isl_space_unwrap(isl_space_domain(isl_map_get_space(map)));
1145 if (!dim)
1146 return NULL;
1147 n = isl_space_dim(dim, isl_dim_in);
1148 isl_space_free(dim);
1150 ctx = isl_map_get_ctx(map);
1151 info = isl_alloc_type(ctx, struct isl_sched_info);
1152 if (!info)
1153 return NULL;
1154 info->is_cst = isl_alloc_array(ctx, int, n);
1155 info->cst = isl_vec_alloc(ctx, n);
1156 if (n && (!info->is_cst || !info->cst))
1157 goto error;
1159 for (i = 0; i < n; ++i) {
1160 isl_val *v;
1162 v = isl_map_plain_get_val_if_fixed(map, isl_dim_in, i);
1163 if (!v)
1164 goto error;
1165 info->is_cst[i] = !isl_val_is_nan(v);
1166 if (info->is_cst[i])
1167 info->cst = isl_vec_set_element_val(info->cst, i, v);
1168 else
1169 isl_val_free(v);
1172 return info;
1173 error:
1174 sched_info_free(info);
1175 return NULL;
1178 /* This structure represents the input for a dependence analysis computation.
1180 * "sink" represents the sink accesses.
1181 * "must_source" represents the definite source accesses.
1182 * "may_source" represents the possible source accesses.
1184 * "schedule" or "schedule_map" represents the execution order.
1185 * Exactly one of these fields should be NULL. The other field
1186 * determines the execution order.
1188 * The domains of these four maps refer to the same iteration spaces(s).
1189 * The ranges of the first three maps also refer to the same data space(s).
1191 * After a call to isl_union_access_info_introduce_schedule,
1192 * the "schedule_map" field no longer contains useful information.
1194 struct isl_union_access_info {
1195 isl_union_map *sink;
1196 isl_union_map *must_source;
1197 isl_union_map *may_source;
1199 isl_schedule *schedule;
1200 isl_union_map *schedule_map;
1203 /* Free "access" and return NULL.
1205 __isl_null isl_union_access_info *isl_union_access_info_free(
1206 __isl_take isl_union_access_info *access)
1208 if (!access)
1209 return NULL;
1211 isl_union_map_free(access->sink);
1212 isl_union_map_free(access->must_source);
1213 isl_union_map_free(access->may_source);
1214 isl_schedule_free(access->schedule);
1215 isl_union_map_free(access->schedule_map);
1216 free(access);
1218 return NULL;
1221 /* Return the isl_ctx to which "access" belongs.
1223 isl_ctx *isl_union_access_info_get_ctx(__isl_keep isl_union_access_info *access)
1225 return access ? isl_union_map_get_ctx(access->sink) : NULL;
1228 /* Create a new isl_union_access_info with the given sink accesses and
1229 * and no source accesses or schedule information.
1231 * By default, we use the schedule field of the isl_union_access_info,
1232 * but this may be overridden by a call
1233 * to isl_union_access_info_set_schedule_map.
1235 __isl_give isl_union_access_info *isl_union_access_info_from_sink(
1236 __isl_take isl_union_map *sink)
1238 isl_ctx *ctx;
1239 isl_space *space;
1240 isl_union_map *empty;
1241 isl_union_access_info *access;
1243 if (!sink)
1244 return NULL;
1245 ctx = isl_union_map_get_ctx(sink);
1246 access = isl_alloc_type(ctx, isl_union_access_info);
1247 if (!access)
1248 goto error;
1250 space = isl_union_map_get_space(sink);
1251 empty = isl_union_map_empty(isl_space_copy(space));
1252 access->sink = sink;
1253 access->must_source = isl_union_map_copy(empty);
1254 access->may_source = empty;
1255 access->schedule = isl_schedule_empty(space);
1256 access->schedule_map = NULL;
1258 if (!access->sink || !access->must_source ||
1259 !access->may_source || !access->schedule)
1260 return isl_union_access_info_free(access);
1262 return access;
1263 error:
1264 isl_union_map_free(sink);
1265 return NULL;
1268 /* Replace the definite source accesses of "access" by "must_source".
1270 __isl_give isl_union_access_info *isl_union_access_info_set_must_source(
1271 __isl_take isl_union_access_info *access,
1272 __isl_take isl_union_map *must_source)
1274 if (!access || !must_source)
1275 goto error;
1277 isl_union_map_free(access->must_source);
1278 access->must_source = must_source;
1280 return access;
1281 error:
1282 isl_union_access_info_free(access);
1283 isl_union_map_free(must_source);
1284 return NULL;
1287 /* Replace the possible source accesses of "access" by "may_source".
1289 __isl_give isl_union_access_info *isl_union_access_info_set_may_source(
1290 __isl_take isl_union_access_info *access,
1291 __isl_take isl_union_map *may_source)
1293 if (!access || !may_source)
1294 goto error;
1296 isl_union_map_free(access->may_source);
1297 access->may_source = may_source;
1299 return access;
1300 error:
1301 isl_union_access_info_free(access);
1302 isl_union_map_free(may_source);
1303 return NULL;
1306 /* Replace the schedule of "access" by "schedule".
1307 * Also free the schedule_map in case it was set last.
1309 __isl_give isl_union_access_info *isl_union_access_info_set_schedule(
1310 __isl_take isl_union_access_info *access,
1311 __isl_take isl_schedule *schedule)
1313 if (!access || !schedule)
1314 goto error;
1316 access->schedule_map = isl_union_map_free(access->schedule_map);
1317 isl_schedule_free(access->schedule);
1318 access->schedule = schedule;
1320 return access;
1321 error:
1322 isl_union_access_info_free(access);
1323 isl_schedule_free(schedule);
1324 return NULL;
1327 /* Replace the schedule map of "access" by "schedule_map".
1328 * Also free the schedule in case it was set last.
1330 __isl_give isl_union_access_info *isl_union_access_info_set_schedule_map(
1331 __isl_take isl_union_access_info *access,
1332 __isl_take isl_union_map *schedule_map)
1334 if (!access || !schedule_map)
1335 goto error;
1337 isl_union_map_free(access->schedule_map);
1338 access->schedule = isl_schedule_free(access->schedule);
1339 access->schedule_map = schedule_map;
1341 return access;
1342 error:
1343 isl_union_access_info_free(access);
1344 isl_union_map_free(schedule_map);
1345 return NULL;
1348 /* Update the fields of "access" such that they all have the same parameters,
1349 * keeping in mind that the schedule_map field may be NULL and ignoring
1350 * the schedule field.
1352 static __isl_give isl_union_access_info *isl_union_access_info_align_params(
1353 __isl_take isl_union_access_info *access)
1355 isl_space *space;
1357 if (!access)
1358 return NULL;
1360 space = isl_union_map_get_space(access->sink);
1361 space = isl_space_align_params(space,
1362 isl_union_map_get_space(access->must_source));
1363 space = isl_space_align_params(space,
1364 isl_union_map_get_space(access->may_source));
1365 if (access->schedule_map)
1366 space = isl_space_align_params(space,
1367 isl_union_map_get_space(access->schedule_map));
1368 access->sink = isl_union_map_align_params(access->sink,
1369 isl_space_copy(space));
1370 access->must_source = isl_union_map_align_params(access->must_source,
1371 isl_space_copy(space));
1372 access->may_source = isl_union_map_align_params(access->may_source,
1373 isl_space_copy(space));
1374 if (!access->schedule_map) {
1375 isl_space_free(space);
1376 } else {
1377 access->schedule_map =
1378 isl_union_map_align_params(access->schedule_map, space);
1379 if (!access->schedule_map)
1380 return isl_union_access_info_free(access);
1383 if (!access->sink || !access->must_source || !access->may_source)
1384 return isl_union_access_info_free(access);
1386 return access;
1389 /* Prepend the schedule dimensions to the iteration domains.
1391 * That is, if the schedule is of the form
1393 * D -> S
1395 * while the access relations are of the form
1397 * D -> A
1399 * then the updated access relations are of the form
1401 * [S -> D] -> A
1403 * The schedule map is also replaced by the map
1405 * [S -> D] -> D
1407 * that is used during the internal computation.
1408 * Neither the original schedule map nor this updated schedule map
1409 * are used after the call to this function.
1411 static __isl_give isl_union_access_info *
1412 isl_union_access_info_introduce_schedule(
1413 __isl_take isl_union_access_info *access)
1415 isl_union_map *sm;
1417 if (!access)
1418 return NULL;
1420 sm = isl_union_map_reverse(access->schedule_map);
1421 sm = isl_union_map_range_map(sm);
1422 access->sink = isl_union_map_apply_range(isl_union_map_copy(sm),
1423 access->sink);
1424 access->may_source = isl_union_map_apply_range(isl_union_map_copy(sm),
1425 access->may_source);
1426 access->must_source = isl_union_map_apply_range(isl_union_map_copy(sm),
1427 access->must_source);
1428 access->schedule_map = sm;
1430 if (!access->sink || !access->must_source ||
1431 !access->may_source || !access->schedule_map)
1432 return isl_union_access_info_free(access);
1434 return access;
1437 /* This structure epresents the result of a dependence analysis computation.
1439 * "must_dep" represents the definite dependences.
1440 * "may_dep" represents the non-definite dependences.
1441 * "must_no_source" represents the subset of the sink accesses for which
1442 * definitely no source was found.
1443 * "may_no_source" represents the subset of the sink accesses for which
1444 * possibly, but not definitely, no source was found.
1446 struct isl_union_flow {
1447 isl_union_map *must_dep;
1448 isl_union_map *may_dep;
1449 isl_union_map *must_no_source;
1450 isl_union_map *may_no_source;
1453 /* Free "flow" and return NULL.
1455 __isl_null isl_union_flow *isl_union_flow_free(__isl_take isl_union_flow *flow)
1457 if (!flow)
1458 return NULL;
1459 isl_union_map_free(flow->must_dep);
1460 isl_union_map_free(flow->may_dep);
1461 isl_union_map_free(flow->must_no_source);
1462 isl_union_map_free(flow->may_no_source);
1463 free(flow);
1464 return NULL;
1467 void isl_union_flow_dump(__isl_keep isl_union_flow *flow)
1469 if (!flow)
1470 return;
1472 fprintf(stderr, "must dependences: ");
1473 isl_union_map_dump(flow->must_dep);
1474 fprintf(stderr, "may dependences: ");
1475 isl_union_map_dump(flow->may_dep);
1476 fprintf(stderr, "must no source: ");
1477 isl_union_map_dump(flow->must_no_source);
1478 fprintf(stderr, "may no source: ");
1479 isl_union_map_dump(flow->may_no_source);
1482 /* Return the definite dependences in "flow".
1484 __isl_give isl_union_map *isl_union_flow_get_must_dependence(
1485 __isl_keep isl_union_flow *flow)
1487 if (!flow)
1488 return NULL;
1489 return isl_union_map_copy(flow->must_dep);
1492 /* Return the possible dependences in "flow", including the definite
1493 * dependences.
1495 __isl_give isl_union_map *isl_union_flow_get_may_dependence(
1496 __isl_keep isl_union_flow *flow)
1498 if (!flow)
1499 return NULL;
1500 return isl_union_map_union(isl_union_map_copy(flow->must_dep),
1501 isl_union_map_copy(flow->may_dep));
1504 /* Return the non-definite dependences in "flow".
1506 static __isl_give isl_union_map *isl_union_flow_get_non_must_dependence(
1507 __isl_keep isl_union_flow *flow)
1509 if (!flow)
1510 return NULL;
1511 return isl_union_map_copy(flow->may_dep);
1514 /* Return the subset of the sink accesses for which definitely
1515 * no source was found.
1517 __isl_give isl_union_map *isl_union_flow_get_must_no_source(
1518 __isl_keep isl_union_flow *flow)
1520 if (!flow)
1521 return NULL;
1522 return isl_union_map_copy(flow->must_no_source);
1525 /* Return the subset of the sink accesses for which possibly
1526 * no source was found, including those for which definitely
1527 * no source was found.
1529 __isl_give isl_union_map *isl_union_flow_get_may_no_source(
1530 __isl_keep isl_union_flow *flow)
1532 if (!flow)
1533 return NULL;
1534 return isl_union_map_union(isl_union_map_copy(flow->must_no_source),
1535 isl_union_map_copy(flow->may_no_source));
1538 /* Return the subset of the sink accesses for which possibly, but not
1539 * definitely, no source was found.
1541 static __isl_give isl_union_map *isl_union_flow_get_non_must_no_source(
1542 __isl_keep isl_union_flow *flow)
1544 if (!flow)
1545 return NULL;
1546 return isl_union_map_copy(flow->may_no_source);
1549 /* Create a new isl_union_flow object, initialized with empty
1550 * dependence relations and sink subsets.
1552 static __isl_give isl_union_flow *isl_union_flow_alloc(
1553 __isl_take isl_space *space)
1555 isl_ctx *ctx;
1556 isl_union_map *empty;
1557 isl_union_flow *flow;
1559 if (!space)
1560 return NULL;
1561 ctx = isl_space_get_ctx(space);
1562 flow = isl_alloc_type(ctx, isl_union_flow);
1563 if (!flow)
1564 goto error;
1566 empty = isl_union_map_empty(space);
1567 flow->must_dep = isl_union_map_copy(empty);
1568 flow->may_dep = isl_union_map_copy(empty);
1569 flow->must_no_source = isl_union_map_copy(empty);
1570 flow->may_no_source = empty;
1572 if (!flow->must_dep || !flow->may_dep ||
1573 !flow->must_no_source || !flow->may_no_source)
1574 return isl_union_flow_free(flow);
1576 return flow;
1577 error:
1578 isl_space_free(space);
1579 return NULL;
1582 /* Drop the schedule dimensions from the iteration domains in "flow".
1583 * In particular, the schedule dimensions have been prepended
1584 * to the iteration domains prior to the dependence analysis by
1585 * replacing the iteration domain D, by the wrapped map [S -> D].
1586 * Replace these wrapped maps by the original D.
1588 static __isl_give isl_union_flow *isl_union_flow_drop_schedule(
1589 __isl_take isl_union_flow *flow)
1591 if (!flow)
1592 return NULL;
1594 flow->must_dep = isl_union_map_factor_range(flow->must_dep);
1595 flow->may_dep = isl_union_map_factor_range(flow->may_dep);
1596 flow->must_no_source =
1597 isl_union_map_domain_factor_range(flow->must_no_source);
1598 flow->may_no_source =
1599 isl_union_map_domain_factor_range(flow->may_no_source);
1601 if (!flow->must_dep || !flow->may_dep ||
1602 !flow->must_no_source || !flow->may_no_source)
1603 return isl_union_flow_free(flow);
1605 return flow;
1608 struct isl_compute_flow_data {
1609 isl_union_map *must_source;
1610 isl_union_map *may_source;
1611 isl_union_flow *flow;
1613 int count;
1614 int must;
1615 isl_space *dim;
1616 struct isl_sched_info *sink_info;
1617 struct isl_sched_info **source_info;
1618 isl_access_info *accesses;
1621 static isl_stat count_matching_array(__isl_take isl_map *map, void *user)
1623 int eq;
1624 isl_space *dim;
1625 struct isl_compute_flow_data *data;
1627 data = (struct isl_compute_flow_data *)user;
1629 dim = isl_space_range(isl_map_get_space(map));
1631 eq = isl_space_is_equal(dim, data->dim);
1633 isl_space_free(dim);
1634 isl_map_free(map);
1636 if (eq < 0)
1637 return isl_stat_error;
1638 if (eq)
1639 data->count++;
1641 return isl_stat_ok;
1644 static isl_stat collect_matching_array(__isl_take isl_map *map, void *user)
1646 int eq;
1647 isl_space *dim;
1648 struct isl_sched_info *info;
1649 struct isl_compute_flow_data *data;
1651 data = (struct isl_compute_flow_data *)user;
1653 dim = isl_space_range(isl_map_get_space(map));
1655 eq = isl_space_is_equal(dim, data->dim);
1657 isl_space_free(dim);
1659 if (eq < 0)
1660 goto error;
1661 if (!eq) {
1662 isl_map_free(map);
1663 return isl_stat_ok;
1666 info = sched_info_alloc(map);
1667 data->source_info[data->count] = info;
1669 data->accesses = isl_access_info_add_source(data->accesses,
1670 map, data->must, info);
1672 data->count++;
1674 return isl_stat_ok;
1675 error:
1676 isl_map_free(map);
1677 return isl_stat_error;
1680 /* Determine the shared nesting level and the "textual order" of
1681 * the given accesses.
1683 * We first determine the minimal schedule dimension for both accesses.
1685 * If among those dimensions, we can find one where both have a fixed
1686 * value and if moreover those values are different, then the previous
1687 * dimension is the last shared nesting level and the textual order
1688 * is determined based on the order of the fixed values.
1689 * If no such fixed values can be found, then we set the shared
1690 * nesting level to the minimal schedule dimension, with no textual ordering.
1692 static int before(void *first, void *second)
1694 struct isl_sched_info *info1 = first;
1695 struct isl_sched_info *info2 = second;
1696 int n1, n2;
1697 int i;
1699 n1 = isl_vec_size(info1->cst);
1700 n2 = isl_vec_size(info2->cst);
1702 if (n2 < n1)
1703 n1 = n2;
1705 for (i = 0; i < n1; ++i) {
1706 int r;
1707 int cmp;
1709 if (!info1->is_cst[i])
1710 continue;
1711 if (!info2->is_cst[i])
1712 continue;
1713 cmp = isl_vec_cmp_element(info1->cst, info2->cst, i);
1714 if (cmp == 0)
1715 continue;
1717 r = 2 * i + (cmp < 0);
1719 return r;
1722 return 2 * n1;
1725 /* Given a sink access, look for all the source accesses that access
1726 * the same array and perform dataflow analysis on them using
1727 * isl_access_info_compute_flow.
1729 static isl_stat compute_flow(__isl_take isl_map *map, void *user)
1731 int i;
1732 isl_ctx *ctx;
1733 struct isl_compute_flow_data *data;
1734 isl_flow *flow;
1735 isl_union_flow *df;
1737 data = (struct isl_compute_flow_data *)user;
1738 df = data->flow;
1740 ctx = isl_map_get_ctx(map);
1742 data->accesses = NULL;
1743 data->sink_info = NULL;
1744 data->source_info = NULL;
1745 data->count = 0;
1746 data->dim = isl_space_range(isl_map_get_space(map));
1748 if (isl_union_map_foreach_map(data->must_source,
1749 &count_matching_array, data) < 0)
1750 goto error;
1751 if (isl_union_map_foreach_map(data->may_source,
1752 &count_matching_array, data) < 0)
1753 goto error;
1755 data->sink_info = sched_info_alloc(map);
1756 data->source_info = isl_calloc_array(ctx, struct isl_sched_info *,
1757 data->count);
1759 data->accesses = isl_access_info_alloc(isl_map_copy(map),
1760 data->sink_info, &before, data->count);
1761 if (!data->sink_info || (data->count && !data->source_info) ||
1762 !data->accesses)
1763 goto error;
1764 data->count = 0;
1765 data->must = 1;
1766 if (isl_union_map_foreach_map(data->must_source,
1767 &collect_matching_array, data) < 0)
1768 goto error;
1769 data->must = 0;
1770 if (isl_union_map_foreach_map(data->may_source,
1771 &collect_matching_array, data) < 0)
1772 goto error;
1774 flow = isl_access_info_compute_flow(data->accesses);
1775 data->accesses = NULL;
1777 if (!flow)
1778 goto error;
1780 df->must_no_source = isl_union_map_union(df->must_no_source,
1781 isl_union_map_from_map(isl_flow_get_no_source(flow, 1)));
1782 df->may_no_source = isl_union_map_union(df->may_no_source,
1783 isl_union_map_from_map(isl_flow_get_no_source(flow, 0)));
1785 for (i = 0; i < flow->n_source; ++i) {
1786 isl_union_map *dep;
1787 dep = isl_union_map_from_map(isl_map_copy(flow->dep[i].map));
1788 if (flow->dep[i].must)
1789 df->must_dep = isl_union_map_union(df->must_dep, dep);
1790 else
1791 df->may_dep = isl_union_map_union(df->may_dep, dep);
1794 isl_flow_free(flow);
1796 sched_info_free(data->sink_info);
1797 if (data->source_info) {
1798 for (i = 0; i < data->count; ++i)
1799 sched_info_free(data->source_info[i]);
1800 free(data->source_info);
1802 isl_space_free(data->dim);
1803 isl_map_free(map);
1805 return isl_stat_ok;
1806 error:
1807 isl_access_info_free(data->accesses);
1808 sched_info_free(data->sink_info);
1809 if (data->source_info) {
1810 for (i = 0; i < data->count; ++i)
1811 sched_info_free(data->source_info[i]);
1812 free(data->source_info);
1814 isl_space_free(data->dim);
1815 isl_map_free(map);
1817 return isl_stat_error;
1820 /* Remove the must accesses from the may accesses.
1822 * A must access always trumps a may access, so there is no need
1823 * for a must access to also be considered as a may access. Doing so
1824 * would only cost extra computations only to find out that
1825 * the duplicated may access does not make any difference.
1827 static __isl_give isl_union_access_info *isl_union_access_info_normalize(
1828 __isl_take isl_union_access_info *access)
1830 if (!access)
1831 return NULL;
1832 access->may_source = isl_union_map_subtract(access->may_source,
1833 isl_union_map_copy(access->must_source));
1834 if (!access->may_source)
1835 return isl_union_access_info_free(access);
1837 return access;
1840 /* Given a description of the "sink" accesses, the "source" accesses and
1841 * a schedule, compute for each instance of a sink access
1842 * and for each element accessed by that instance,
1843 * the possible or definite source accesses that last accessed the
1844 * element accessed by the sink access before this sink access
1845 * in the sense that there is no intermediate definite source access.
1847 * The must_no_source and may_no_source elements of the result
1848 * are subsets of access->sink. The elements must_dep and may_dep
1849 * map domain elements of access->{may,must)_source to
1850 * domain elements of access->sink.
1852 * This function is used when only the schedule map representation
1853 * is available.
1855 * We first prepend the schedule dimensions to the domain
1856 * of the accesses so that we can easily compare their relative order.
1857 * Then we consider each sink access individually in compute_flow.
1859 static __isl_give isl_union_flow *compute_flow_union_map(
1860 __isl_take isl_union_access_info *access)
1862 struct isl_compute_flow_data data;
1864 access = isl_union_access_info_align_params(access);
1865 access = isl_union_access_info_introduce_schedule(access);
1866 if (!access)
1867 return NULL;
1869 data.must_source = access->must_source;
1870 data.may_source = access->may_source;
1872 data.flow = isl_union_flow_alloc(isl_union_map_get_space(access->sink));
1874 if (isl_union_map_foreach_map(access->sink, &compute_flow, &data) < 0)
1875 goto error;
1877 data.flow = isl_union_flow_drop_schedule(data.flow);
1879 isl_union_access_info_free(access);
1880 return data.flow;
1881 error:
1882 isl_union_access_info_free(access);
1883 isl_union_flow_free(data.flow);
1884 return NULL;
1887 /* A schedule access relation.
1889 * The access relation "access" is of the form [S -> D] -> A,
1890 * where S corresponds to the prefix schedule at "node".
1891 * "must" is only relevant for source accesses and indicates
1892 * whether the access is a must source or a may source.
1894 struct isl_scheduled_access {
1895 isl_map *access;
1896 int must;
1897 isl_schedule_node *node;
1900 /* Data structure for keeping track of individual scheduled sink and source
1901 * accesses when computing dependence analysis based on a schedule tree.
1903 * "n_sink" is the number of used entries in "sink"
1904 * "n_source" is the number of used entries in "source"
1906 * "set_sink", "must" and "node" are only used inside collect_sink_source,
1907 * to keep track of the current node and
1908 * of what extract_sink_source needs to do.
1910 struct isl_compute_flow_schedule_data {
1911 isl_union_access_info *access;
1913 int n_sink;
1914 int n_source;
1916 struct isl_scheduled_access *sink;
1917 struct isl_scheduled_access *source;
1919 int set_sink;
1920 int must;
1921 isl_schedule_node *node;
1924 /* Align the parameters of all sinks with all sources.
1926 * If there are no sinks or no sources, then no alignment is needed.
1928 static void isl_compute_flow_schedule_data_align_params(
1929 struct isl_compute_flow_schedule_data *data)
1931 int i;
1932 isl_space *space;
1934 if (data->n_sink == 0 || data->n_source == 0)
1935 return;
1937 space = isl_map_get_space(data->sink[0].access);
1939 for (i = 1; i < data->n_sink; ++i)
1940 space = isl_space_align_params(space,
1941 isl_map_get_space(data->sink[i].access));
1942 for (i = 0; i < data->n_source; ++i)
1943 space = isl_space_align_params(space,
1944 isl_map_get_space(data->source[i].access));
1946 for (i = 0; i < data->n_sink; ++i)
1947 data->sink[i].access =
1948 isl_map_align_params(data->sink[i].access,
1949 isl_space_copy(space));
1950 for (i = 0; i < data->n_source; ++i)
1951 data->source[i].access =
1952 isl_map_align_params(data->source[i].access,
1953 isl_space_copy(space));
1955 isl_space_free(space);
1958 /* Free all the memory referenced from "data".
1959 * Do not free "data" itself as it may be allocated on the stack.
1961 static void isl_compute_flow_schedule_data_clear(
1962 struct isl_compute_flow_schedule_data *data)
1964 int i;
1966 if (!data->sink)
1967 return;
1969 for (i = 0; i < data->n_sink; ++i) {
1970 isl_map_free(data->sink[i].access);
1971 isl_schedule_node_free(data->sink[i].node);
1974 for (i = 0; i < data->n_source; ++i) {
1975 isl_map_free(data->source[i].access);
1976 isl_schedule_node_free(data->source[i].node);
1979 free(data->sink);
1982 /* isl_schedule_foreach_schedule_node_top_down callback for counting
1983 * (an upper bound on) the number of sinks and sources.
1985 * Sinks and sources are only extracted at leaves of the tree,
1986 * so we skip the node if it is not a leaf.
1987 * Otherwise we increment data->n_sink and data->n_source with
1988 * the number of spaces in the sink and source access domains
1989 * that reach this node.
1991 static isl_bool count_sink_source(__isl_keep isl_schedule_node *node,
1992 void *user)
1994 struct isl_compute_flow_schedule_data *data = user;
1995 isl_union_set *domain;
1996 isl_union_map *umap;
1997 isl_bool r = isl_bool_false;
1999 if (isl_schedule_node_get_type(node) != isl_schedule_node_leaf)
2000 return isl_bool_true;
2002 domain = isl_schedule_node_get_universe_domain(node);
2004 umap = isl_union_map_copy(data->access->sink);
2005 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
2006 data->n_sink += isl_union_map_n_map(umap);
2007 isl_union_map_free(umap);
2008 if (!umap)
2009 r = isl_bool_error;
2011 umap = isl_union_map_copy(data->access->must_source);
2012 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
2013 data->n_source += isl_union_map_n_map(umap);
2014 isl_union_map_free(umap);
2015 if (!umap)
2016 r = isl_bool_error;
2018 umap = isl_union_map_copy(data->access->may_source);
2019 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
2020 data->n_source += isl_union_map_n_map(umap);
2021 isl_union_map_free(umap);
2022 if (!umap)
2023 r = isl_bool_error;
2025 isl_union_set_free(domain);
2027 return r;
2030 /* Add a single scheduled sink or source (depending on data->set_sink)
2031 * with scheduled access relation "map", must property data->must and
2032 * schedule node data->node to the list of sinks or sources.
2034 static isl_stat extract_sink_source(__isl_take isl_map *map, void *user)
2036 struct isl_compute_flow_schedule_data *data = user;
2037 struct isl_scheduled_access *access;
2039 if (data->set_sink)
2040 access = data->sink + data->n_sink++;
2041 else
2042 access = data->source + data->n_source++;
2044 access->access = map;
2045 access->must = data->must;
2046 access->node = isl_schedule_node_copy(data->node);
2048 return isl_stat_ok;
2051 /* isl_schedule_foreach_schedule_node_top_down callback for collecting
2052 * individual scheduled source and sink accesses.
2054 * We only collect accesses at the leaves of the schedule tree.
2055 * We prepend the schedule dimensions at the leaf to the iteration
2056 * domains of the source and sink accesses and then extract
2057 * the individual accesses (per space).
2059 * In particular, if the prefix schedule at the node is of the form
2061 * D -> S
2063 * while the access relations are of the form
2065 * D -> A
2067 * then the updated access relations are of the form
2069 * [S -> D] -> A
2071 * Note that S consists of a single space such that introducing S
2072 * in the access relations does not increase the number of spaces.
2074 static isl_bool collect_sink_source(__isl_keep isl_schedule_node *node,
2075 void *user)
2077 struct isl_compute_flow_schedule_data *data = user;
2078 isl_union_map *prefix;
2079 isl_union_map *umap;
2080 isl_bool r = isl_bool_false;
2082 if (isl_schedule_node_get_type(node) != isl_schedule_node_leaf)
2083 return isl_bool_true;
2085 data->node = node;
2087 prefix = isl_schedule_node_get_prefix_schedule_union_map(node);
2088 prefix = isl_union_map_reverse(prefix);
2089 prefix = isl_union_map_range_map(prefix);
2091 data->set_sink = 1;
2092 umap = isl_union_map_copy(data->access->sink);
2093 umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
2094 if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
2095 r = isl_bool_error;
2096 isl_union_map_free(umap);
2098 data->set_sink = 0;
2099 data->must = 1;
2100 umap = isl_union_map_copy(data->access->must_source);
2101 umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
2102 if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
2103 r = isl_bool_error;
2104 isl_union_map_free(umap);
2106 data->set_sink = 0;
2107 data->must = 0;
2108 umap = isl_union_map_copy(data->access->may_source);
2109 umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
2110 if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
2111 r = isl_bool_error;
2112 isl_union_map_free(umap);
2114 isl_union_map_free(prefix);
2116 return r;
2119 /* isl_access_info_compute_flow callback for determining whether
2120 * the shared nesting level and the ordering within that level
2121 * for two scheduled accesses for use in compute_single_flow.
2123 * The tokens passed to this function refer to the leaves
2124 * in the schedule tree where the accesses take place.
2126 * If n is the shared number of loops, then we need to return
2127 * "2 * n + 1" if "first" precedes "second" inside the innermost
2128 * shared loop and "2 * n" otherwise.
2130 * The innermost shared ancestor may be the leaves themselves
2131 * if the accesses take place in the same leaf. Otherwise,
2132 * it is either a set node or a sequence node. Only in the case
2133 * of a sequence node do we consider one access to precede the other.
2135 static int before_node(void *first, void *second)
2137 isl_schedule_node *node1 = first;
2138 isl_schedule_node *node2 = second;
2139 isl_schedule_node *shared;
2140 int depth;
2141 int before = 0;
2143 shared = isl_schedule_node_get_shared_ancestor(node1, node2);
2144 if (!shared)
2145 return -1;
2147 depth = isl_schedule_node_get_schedule_depth(shared);
2148 if (isl_schedule_node_get_type(shared) == isl_schedule_node_sequence) {
2149 int pos1, pos2;
2151 pos1 = isl_schedule_node_get_ancestor_child_position(node1,
2152 shared);
2153 pos2 = isl_schedule_node_get_ancestor_child_position(node2,
2154 shared);
2155 before = pos1 < pos2;
2158 isl_schedule_node_free(shared);
2160 return 2 * depth + before;
2163 /* Add the scheduled sources from "data" that access
2164 * the same data space as "sink" to "access".
2166 static __isl_give isl_access_info *add_matching_sources(
2167 __isl_take isl_access_info *access, struct isl_scheduled_access *sink,
2168 struct isl_compute_flow_schedule_data *data)
2170 int i;
2171 isl_space *space;
2173 space = isl_space_range(isl_map_get_space(sink->access));
2174 for (i = 0; i < data->n_source; ++i) {
2175 struct isl_scheduled_access *source;
2176 isl_space *source_space;
2177 int eq;
2179 source = &data->source[i];
2180 source_space = isl_map_get_space(source->access);
2181 source_space = isl_space_range(source_space);
2182 eq = isl_space_is_equal(space, source_space);
2183 isl_space_free(source_space);
2185 if (!eq)
2186 continue;
2187 if (eq < 0)
2188 goto error;
2190 access = isl_access_info_add_source(access,
2191 isl_map_copy(source->access), source->must, source->node);
2194 isl_space_free(space);
2195 return access;
2196 error:
2197 isl_space_free(space);
2198 isl_access_info_free(access);
2199 return NULL;
2202 /* Given a scheduled sink access relation "sink", compute the corresponding
2203 * dependences on the sources in "data" and add the computed dependences
2204 * to "uf".
2206 static __isl_give isl_union_flow *compute_single_flow(
2207 __isl_take isl_union_flow *uf, struct isl_scheduled_access *sink,
2208 struct isl_compute_flow_schedule_data *data)
2210 int i;
2211 isl_access_info *access;
2212 isl_flow *flow;
2213 isl_map *map;
2215 if (!uf)
2216 return NULL;
2218 access = isl_access_info_alloc(isl_map_copy(sink->access), sink->node,
2219 &before_node, data->n_source);
2220 access = add_matching_sources(access, sink, data);
2222 flow = isl_access_info_compute_flow(access);
2223 if (!flow)
2224 return isl_union_flow_free(uf);
2226 map = isl_map_domain_factor_range(isl_flow_get_no_source(flow, 1));
2227 uf->must_no_source = isl_union_map_union(uf->must_no_source,
2228 isl_union_map_from_map(map));
2229 map = isl_map_domain_factor_range(isl_flow_get_no_source(flow, 0));
2230 uf->may_no_source = isl_union_map_union(uf->may_no_source,
2231 isl_union_map_from_map(map));
2233 for (i = 0; i < flow->n_source; ++i) {
2234 isl_union_map *dep;
2236 map = isl_map_factor_range(isl_map_copy(flow->dep[i].map));
2237 dep = isl_union_map_from_map(map);
2238 if (flow->dep[i].must)
2239 uf->must_dep = isl_union_map_union(uf->must_dep, dep);
2240 else
2241 uf->may_dep = isl_union_map_union(uf->may_dep, dep);
2244 isl_flow_free(flow);
2246 return uf;
2249 /* Given a description of the "sink" accesses, the "source" accesses and
2250 * a schedule, compute for each instance of a sink access
2251 * and for each element accessed by that instance,
2252 * the possible or definite source accesses that last accessed the
2253 * element accessed by the sink access before this sink access
2254 * in the sense that there is no intermediate definite source access.
2256 * The must_no_source and may_no_source elements of the result
2257 * are subsets of access->sink. The elements must_dep and may_dep
2258 * map domain elements of access->{may,must)_source to
2259 * domain elements of access->sink.
2261 * This function is used when a schedule tree representation
2262 * is available.
2264 * We extract the individual scheduled source and sink access relations and
2265 * then compute dependences for each scheduled sink individually.
2267 static __isl_give isl_union_flow *compute_flow_schedule(
2268 __isl_take isl_union_access_info *access)
2270 struct isl_compute_flow_schedule_data data = { access };
2271 int i, n;
2272 isl_ctx *ctx;
2273 isl_union_flow *flow;
2275 ctx = isl_union_access_info_get_ctx(access);
2277 data.n_sink = 0;
2278 data.n_source = 0;
2279 if (isl_schedule_foreach_schedule_node_top_down(access->schedule,
2280 &count_sink_source, &data) < 0)
2281 goto error;
2283 n = data.n_sink + data.n_source;
2284 data.sink = isl_calloc_array(ctx, struct isl_scheduled_access, n);
2285 if (n && !data.sink)
2286 goto error;
2287 data.source = data.sink + data.n_sink;
2289 data.n_sink = 0;
2290 data.n_source = 0;
2291 if (isl_schedule_foreach_schedule_node_top_down(access->schedule,
2292 &collect_sink_source, &data) < 0)
2293 goto error;
2295 flow = isl_union_flow_alloc(isl_union_map_get_space(access->sink));
2297 isl_compute_flow_schedule_data_align_params(&data);
2299 for (i = 0; i < data.n_sink; ++i)
2300 flow = compute_single_flow(flow, &data.sink[i], &data);
2302 isl_compute_flow_schedule_data_clear(&data);
2304 isl_union_access_info_free(access);
2305 return flow;
2306 error:
2307 isl_union_access_info_free(access);
2308 isl_compute_flow_schedule_data_clear(&data);
2309 return NULL;
2312 /* Given a description of the "sink" accesses, the "source" accesses and
2313 * a schedule, compute for each instance of a sink access
2314 * and for each element accessed by that instance,
2315 * the possible or definite source accesses that last accessed the
2316 * element accessed by the sink access before this sink access
2317 * in the sense that there is no intermediate definite source access.
2319 * The must_no_source and may_no_source elements of the result
2320 * are subsets of access->sink. The elements must_dep and may_dep
2321 * map domain elements of access->{may,must)_source to
2322 * domain elements of access->sink.
2324 * We check whether the schedule is available as a schedule tree
2325 * or a schedule map and call the correpsonding function to perform
2326 * the analysis.
2328 __isl_give isl_union_flow *isl_union_access_info_compute_flow(
2329 __isl_take isl_union_access_info *access)
2331 access = isl_union_access_info_normalize(access);
2332 if (!access)
2333 return NULL;
2334 if (access->schedule)
2335 return compute_flow_schedule(access);
2336 else
2337 return compute_flow_union_map(access);
2340 /* Given a collection of "sink" and "source" accesses,
2341 * compute for each iteration of a sink access
2342 * and for each element accessed by that iteration,
2343 * the source access in the list that last accessed the
2344 * element accessed by the sink access before this sink access.
2345 * Each access is given as a map from the loop iterators
2346 * to the array indices.
2347 * The result is a relations between source and sink
2348 * iterations and a subset of the domain of the sink accesses,
2349 * corresponding to those iterations that access an element
2350 * not previously accessed.
2352 * We collect the inputs in an isl_union_access_info object,
2353 * call isl_union_access_info_compute_flow and extract
2354 * the outputs from the result.
2356 int isl_union_map_compute_flow(__isl_take isl_union_map *sink,
2357 __isl_take isl_union_map *must_source,
2358 __isl_take isl_union_map *may_source,
2359 __isl_take isl_union_map *schedule,
2360 __isl_give isl_union_map **must_dep, __isl_give isl_union_map **may_dep,
2361 __isl_give isl_union_map **must_no_source,
2362 __isl_give isl_union_map **may_no_source)
2364 isl_union_access_info *access;
2365 isl_union_flow *flow;
2367 access = isl_union_access_info_from_sink(sink);
2368 access = isl_union_access_info_set_must_source(access, must_source);
2369 access = isl_union_access_info_set_may_source(access, may_source);
2370 access = isl_union_access_info_set_schedule_map(access, schedule);
2371 flow = isl_union_access_info_compute_flow(access);
2373 if (must_dep)
2374 *must_dep = isl_union_flow_get_must_dependence(flow);
2375 if (may_dep)
2376 *may_dep = isl_union_flow_get_non_must_dependence(flow);
2377 if (must_no_source)
2378 *must_no_source = isl_union_flow_get_must_no_source(flow);
2379 if (may_no_source)
2380 *may_no_source = isl_union_flow_get_non_must_no_source(flow);
2382 isl_union_flow_free(flow);
2384 if ((must_dep && !*must_dep) || (may_dep && !*may_dep) ||
2385 (must_no_source && !*must_no_source) ||
2386 (may_no_source && !*may_no_source))
2387 goto error;
2389 return 0;
2390 error:
2391 if (must_dep)
2392 *must_dep = isl_union_map_free(*must_dep);
2393 if (may_dep)
2394 *may_dep = isl_union_map_free(*may_dep);
2395 if (must_no_source)
2396 *must_no_source = isl_union_map_free(*must_no_source);
2397 if (may_no_source)
2398 *may_no_source = isl_union_map_free(*may_no_source);
2399 return -1;