isl_map_list.c: directly include required header
[isl.git] / isl_flow.c
blobb3497a6d2eb19b2512e2a9fb8a0f34c7fc004138
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_like(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 must_rel[j] = isl_map_empty_like(res->dep[2 * j].map);
952 may_rel[j] = isl_map_copy(must_rel[j]);
955 for (j = acc->n_must - 1; j >= 0; --j) {
956 struct isl_map *T;
957 struct isl_set *rest;
958 int plevel;
960 plevel = acc->level_before(acc->source[j].data,
961 acc->sink.data);
962 if (!can_precede_at_level(plevel, level))
963 continue;
965 T = last_source(acc, mustdo, j, level, &rest);
966 must_rel[j] = isl_map_union_disjoint(must_rel[j], T);
967 mustdo = rest;
969 intermediate_sources(acc, must_rel, j, level);
971 T = last_source(acc, maydo, j, level, &rest);
972 may_rel[j] = isl_map_union_disjoint(may_rel[j], T);
973 maydo = rest;
975 intermediate_sources(acc, may_rel, j, level);
977 if (isl_set_plain_is_empty(mustdo) &&
978 isl_set_plain_is_empty(maydo))
979 break;
981 for (j = j - 1; j >= 0; --j) {
982 int plevel;
984 plevel = acc->level_before(acc->source[j].data,
985 acc->sink.data);
986 if (!can_precede_at_level(plevel, level))
987 continue;
989 intermediate_sources(acc, must_rel, j, level);
990 intermediate_sources(acc, may_rel, j, level);
993 for (j = 0; j < acc->n_may; ++j) {
994 int plevel;
995 isl_map *T;
996 isl_set *ran;
998 plevel = acc->level_before(acc->source[acc->n_must + j].data,
999 acc->sink.data);
1000 if (!can_precede_at_level(plevel, level))
1001 continue;
1003 T = all_sources(acc, isl_set_copy(maydo), j, level);
1004 res->dep[2 * acc->n_must + j].map =
1005 isl_map_union(res->dep[2 * acc->n_must + j].map, T);
1006 T = all_sources(acc, isl_set_copy(mustdo), j, level);
1007 ran = isl_map_range(isl_map_copy(T));
1008 res->dep[2 * acc->n_must + j].map =
1009 isl_map_union(res->dep[2 * acc->n_must + j].map, T);
1010 mustdo = isl_set_subtract(mustdo, isl_set_copy(ran));
1011 maydo = isl_set_union_disjoint(maydo, ran);
1013 T = res->dep[2 * acc->n_must + j].map;
1014 T = all_intermediate_sources(acc, T, must_rel, may_rel,
1015 j, level);
1016 res->dep[2 * acc->n_must + j].map = T;
1019 for (j = acc->n_must - 1; j >= 0; --j) {
1020 res->dep[2 * j].map =
1021 isl_map_union_disjoint(res->dep[2 * j].map,
1022 must_rel[j]);
1023 res->dep[2 * j + 1].map =
1024 isl_map_union_disjoint(res->dep[2 * j + 1].map,
1025 may_rel[j]);
1028 if (isl_set_plain_is_empty(mustdo) &&
1029 isl_set_plain_is_empty(maydo))
1030 break;
1033 free(must_rel);
1034 free(may_rel);
1035 done:
1036 res->must_no_source = mustdo;
1037 res->may_no_source = maydo;
1038 return res;
1039 error:
1040 isl_flow_free(res);
1041 isl_set_free(mustdo);
1042 isl_set_free(maydo);
1043 free(must_rel);
1044 free(may_rel);
1045 return NULL;
1048 /* Given a "sink" access, a list of n "source" accesses,
1049 * compute for each iteration of the sink access
1050 * and for each element accessed by that iteration,
1051 * the source access in the list that last accessed the
1052 * element accessed by the sink access before this sink access.
1053 * Each access is given as a map from the loop iterators
1054 * to the array indices.
1055 * The result is a list of n relations between source and sink
1056 * iterations and a subset of the domain of the sink access,
1057 * corresponding to those iterations that access an element
1058 * not previously accessed.
1060 * To deal with multi-valued sink access relations, the sink iteration
1061 * domain is first extended with dimensions that correspond to the data
1062 * space. After the computation is finished, these extra dimensions are
1063 * projected out again.
1065 __isl_give isl_flow *isl_access_info_compute_flow(__isl_take isl_access_info *acc)
1067 int j;
1068 struct isl_flow *res = NULL;
1070 if (!acc)
1071 return NULL;
1073 acc->domain_map = isl_map_domain_map(isl_map_copy(acc->sink.map));
1074 acc->sink.map = isl_map_range_map(acc->sink.map);
1075 if (!acc->sink.map)
1076 goto error;
1078 if (acc->n_must == 0)
1079 res = compute_mem_based_dependences(acc);
1080 else {
1081 acc = isl_access_info_sort_sources(acc);
1082 res = compute_val_based_dependences(acc);
1084 if (!res)
1085 goto error;
1087 for (j = 0; j < res->n_source; ++j) {
1088 res->dep[j].map = isl_map_apply_range(res->dep[j].map,
1089 isl_map_copy(acc->domain_map));
1090 if (!res->dep[j].map)
1091 goto error;
1093 if (!res->must_no_source || !res->may_no_source)
1094 goto error;
1096 isl_access_info_free(acc);
1097 return res;
1098 error:
1099 isl_access_info_free(acc);
1100 isl_flow_free(res);
1101 return NULL;
1105 /* Keep track of some information about a schedule for a given
1106 * access. In particular, keep track of which dimensions
1107 * have a constant value and of the actual constant values.
1109 struct isl_sched_info {
1110 int *is_cst;
1111 isl_vec *cst;
1114 static void sched_info_free(__isl_take struct isl_sched_info *info)
1116 if (!info)
1117 return;
1118 isl_vec_free(info->cst);
1119 free(info->is_cst);
1120 free(info);
1123 /* Extract information on the constant dimensions of the schedule
1124 * for a given access. The "map" is of the form
1126 * [S -> D] -> A
1128 * with S the schedule domain, D the iteration domain and A the data domain.
1130 static __isl_give struct isl_sched_info *sched_info_alloc(
1131 __isl_keep isl_map *map)
1133 isl_ctx *ctx;
1134 isl_space *dim;
1135 struct isl_sched_info *info;
1136 int i, n;
1138 if (!map)
1139 return NULL;
1141 dim = isl_space_unwrap(isl_space_domain(isl_map_get_space(map)));
1142 if (!dim)
1143 return NULL;
1144 n = isl_space_dim(dim, isl_dim_in);
1145 isl_space_free(dim);
1147 ctx = isl_map_get_ctx(map);
1148 info = isl_alloc_type(ctx, struct isl_sched_info);
1149 if (!info)
1150 return NULL;
1151 info->is_cst = isl_alloc_array(ctx, int, n);
1152 info->cst = isl_vec_alloc(ctx, n);
1153 if (n && (!info->is_cst || !info->cst))
1154 goto error;
1156 for (i = 0; i < n; ++i) {
1157 isl_val *v;
1159 v = isl_map_plain_get_val_if_fixed(map, isl_dim_in, i);
1160 if (!v)
1161 goto error;
1162 info->is_cst[i] = !isl_val_is_nan(v);
1163 if (info->is_cst[i])
1164 info->cst = isl_vec_set_element_val(info->cst, i, v);
1165 else
1166 isl_val_free(v);
1169 return info;
1170 error:
1171 sched_info_free(info);
1172 return NULL;
1175 /* This structure represents the input for a dependence analysis computation.
1177 * "sink" represents the sink accesses.
1178 * "must_source" represents the definite source accesses.
1179 * "may_source" represents the possible source accesses.
1181 * "schedule" or "schedule_map" represents the execution order.
1182 * Exactly one of these fields should be NULL. The other field
1183 * determines the execution order.
1185 * The domains of these four maps refer to the same iteration spaces(s).
1186 * The ranges of the first three maps also refer to the same data space(s).
1188 * After a call to isl_union_access_info_introduce_schedule,
1189 * the "schedule_map" field no longer contains useful information.
1191 struct isl_union_access_info {
1192 isl_union_map *sink;
1193 isl_union_map *must_source;
1194 isl_union_map *may_source;
1196 isl_schedule *schedule;
1197 isl_union_map *schedule_map;
1200 /* Free "access" and return NULL.
1202 __isl_null isl_union_access_info *isl_union_access_info_free(
1203 __isl_take isl_union_access_info *access)
1205 if (!access)
1206 return NULL;
1208 isl_union_map_free(access->sink);
1209 isl_union_map_free(access->must_source);
1210 isl_union_map_free(access->may_source);
1211 isl_schedule_free(access->schedule);
1212 isl_union_map_free(access->schedule_map);
1213 free(access);
1215 return NULL;
1218 /* Return the isl_ctx to which "access" belongs.
1220 isl_ctx *isl_union_access_info_get_ctx(__isl_keep isl_union_access_info *access)
1222 return access ? isl_union_map_get_ctx(access->sink) : NULL;
1225 /* Create a new isl_union_access_info with the given sink accesses and
1226 * and no source accesses or schedule information.
1228 * By default, we use the schedule field of the isl_union_access_info,
1229 * but this may be overridden by a call
1230 * to isl_union_access_info_set_schedule_map.
1232 __isl_give isl_union_access_info *isl_union_access_info_from_sink(
1233 __isl_take isl_union_map *sink)
1235 isl_ctx *ctx;
1236 isl_space *space;
1237 isl_union_map *empty;
1238 isl_union_access_info *access;
1240 if (!sink)
1241 return NULL;
1242 ctx = isl_union_map_get_ctx(sink);
1243 access = isl_alloc_type(ctx, isl_union_access_info);
1244 if (!access)
1245 goto error;
1247 space = isl_union_map_get_space(sink);
1248 empty = isl_union_map_empty(isl_space_copy(space));
1249 access->sink = sink;
1250 access->must_source = isl_union_map_copy(empty);
1251 access->may_source = empty;
1252 access->schedule = isl_schedule_empty(space);
1253 access->schedule_map = NULL;
1255 if (!access->sink || !access->must_source ||
1256 !access->may_source || !access->schedule)
1257 return isl_union_access_info_free(access);
1259 return access;
1260 error:
1261 isl_union_map_free(sink);
1262 return NULL;
1265 /* Replace the definite source accesses of "access" by "must_source".
1267 __isl_give isl_union_access_info *isl_union_access_info_set_must_source(
1268 __isl_take isl_union_access_info *access,
1269 __isl_take isl_union_map *must_source)
1271 if (!access || !must_source)
1272 goto error;
1274 isl_union_map_free(access->must_source);
1275 access->must_source = must_source;
1277 return access;
1278 error:
1279 isl_union_access_info_free(access);
1280 isl_union_map_free(must_source);
1281 return NULL;
1284 /* Replace the possible source accesses of "access" by "may_source".
1286 __isl_give isl_union_access_info *isl_union_access_info_set_may_source(
1287 __isl_take isl_union_access_info *access,
1288 __isl_take isl_union_map *may_source)
1290 if (!access || !may_source)
1291 goto error;
1293 isl_union_map_free(access->may_source);
1294 access->may_source = may_source;
1296 return access;
1297 error:
1298 isl_union_access_info_free(access);
1299 isl_union_map_free(may_source);
1300 return NULL;
1303 /* Replace the schedule of "access" by "schedule".
1304 * Also free the schedule_map in case it was set last.
1306 __isl_give isl_union_access_info *isl_union_access_info_set_schedule(
1307 __isl_take isl_union_access_info *access,
1308 __isl_take isl_schedule *schedule)
1310 if (!access || !schedule)
1311 goto error;
1313 access->schedule_map = isl_union_map_free(access->schedule_map);
1314 isl_schedule_free(access->schedule);
1315 access->schedule = schedule;
1317 return access;
1318 error:
1319 isl_union_access_info_free(access);
1320 isl_schedule_free(schedule);
1321 return NULL;
1324 /* Replace the schedule map of "access" by "schedule_map".
1325 * Also free the schedule in case it was set last.
1327 __isl_give isl_union_access_info *isl_union_access_info_set_schedule_map(
1328 __isl_take isl_union_access_info *access,
1329 __isl_take isl_union_map *schedule_map)
1331 if (!access || !schedule_map)
1332 goto error;
1334 isl_union_map_free(access->schedule_map);
1335 access->schedule = isl_schedule_free(access->schedule);
1336 access->schedule_map = schedule_map;
1338 return access;
1339 error:
1340 isl_union_access_info_free(access);
1341 isl_union_map_free(schedule_map);
1342 return NULL;
1345 /* Update the fields of "access" such that they all have the same parameters,
1346 * keeping in mind that the schedule_map field may be NULL and ignoring
1347 * the schedule field.
1349 static __isl_give isl_union_access_info *isl_union_access_info_align_params(
1350 __isl_take isl_union_access_info *access)
1352 isl_space *space;
1354 if (!access)
1355 return NULL;
1357 space = isl_union_map_get_space(access->sink);
1358 space = isl_space_align_params(space,
1359 isl_union_map_get_space(access->must_source));
1360 space = isl_space_align_params(space,
1361 isl_union_map_get_space(access->may_source));
1362 if (access->schedule_map)
1363 space = isl_space_align_params(space,
1364 isl_union_map_get_space(access->schedule_map));
1365 access->sink = isl_union_map_align_params(access->sink,
1366 isl_space_copy(space));
1367 access->must_source = isl_union_map_align_params(access->must_source,
1368 isl_space_copy(space));
1369 access->may_source = isl_union_map_align_params(access->may_source,
1370 isl_space_copy(space));
1371 if (!access->schedule_map) {
1372 isl_space_free(space);
1373 } else {
1374 access->schedule_map =
1375 isl_union_map_align_params(access->schedule_map, space);
1376 if (!access->schedule_map)
1377 return isl_union_access_info_free(access);
1380 if (!access->sink || !access->must_source || !access->may_source)
1381 return isl_union_access_info_free(access);
1383 return access;
1386 /* Prepend the schedule dimensions to the iteration domains.
1388 * That is, if the schedule is of the form
1390 * D -> S
1392 * while the access relations are of the form
1394 * D -> A
1396 * then the updated access relations are of the form
1398 * [S -> D] -> A
1400 * The schedule map is also replaced by the map
1402 * [S -> D] -> D
1404 * that is used during the internal computation.
1405 * Neither the original schedule map nor this updated schedule map
1406 * are used after the call to this function.
1408 static __isl_give isl_union_access_info *
1409 isl_union_access_info_introduce_schedule(
1410 __isl_take isl_union_access_info *access)
1412 isl_union_map *sm;
1414 if (!access)
1415 return NULL;
1417 sm = isl_union_map_reverse(access->schedule_map);
1418 sm = isl_union_map_range_map(sm);
1419 access->sink = isl_union_map_apply_range(isl_union_map_copy(sm),
1420 access->sink);
1421 access->may_source = isl_union_map_apply_range(isl_union_map_copy(sm),
1422 access->may_source);
1423 access->must_source = isl_union_map_apply_range(isl_union_map_copy(sm),
1424 access->must_source);
1425 access->schedule_map = sm;
1427 if (!access->sink || !access->must_source ||
1428 !access->may_source || !access->schedule_map)
1429 return isl_union_access_info_free(access);
1431 return access;
1434 /* This structure epresents the result of a dependence analysis computation.
1436 * "must_dep" represents the definite dependences.
1437 * "may_dep" represents the non-definite dependences.
1438 * "must_no_source" represents the subset of the sink accesses for which
1439 * definitely no source was found.
1440 * "may_no_source" represents the subset of the sink accesses for which
1441 * possibly, but not definitely, no source was found.
1443 struct isl_union_flow {
1444 isl_union_map *must_dep;
1445 isl_union_map *may_dep;
1446 isl_union_map *must_no_source;
1447 isl_union_map *may_no_source;
1450 /* Free "flow" and return NULL.
1452 __isl_null isl_union_flow *isl_union_flow_free(__isl_take isl_union_flow *flow)
1454 if (!flow)
1455 return NULL;
1456 isl_union_map_free(flow->must_dep);
1457 isl_union_map_free(flow->may_dep);
1458 isl_union_map_free(flow->must_no_source);
1459 isl_union_map_free(flow->may_no_source);
1460 free(flow);
1461 return NULL;
1464 void isl_union_flow_dump(__isl_keep isl_union_flow *flow)
1466 if (!flow)
1467 return;
1469 fprintf(stderr, "must dependences: ");
1470 isl_union_map_dump(flow->must_dep);
1471 fprintf(stderr, "may dependences: ");
1472 isl_union_map_dump(flow->may_dep);
1473 fprintf(stderr, "must no source: ");
1474 isl_union_map_dump(flow->must_no_source);
1475 fprintf(stderr, "may no source: ");
1476 isl_union_map_dump(flow->may_no_source);
1479 /* Return the definite dependences in "flow".
1481 __isl_give isl_union_map *isl_union_flow_get_must_dependence(
1482 __isl_keep isl_union_flow *flow)
1484 if (!flow)
1485 return NULL;
1486 return isl_union_map_copy(flow->must_dep);
1489 /* Return the possible dependences in "flow", including the definite
1490 * dependences.
1492 __isl_give isl_union_map *isl_union_flow_get_may_dependence(
1493 __isl_keep isl_union_flow *flow)
1495 if (!flow)
1496 return NULL;
1497 return isl_union_map_union(isl_union_map_copy(flow->must_dep),
1498 isl_union_map_copy(flow->may_dep));
1501 /* Return the non-definite dependences in "flow".
1503 static __isl_give isl_union_map *isl_union_flow_get_non_must_dependence(
1504 __isl_keep isl_union_flow *flow)
1506 if (!flow)
1507 return NULL;
1508 return isl_union_map_copy(flow->may_dep);
1511 /* Return the subset of the sink accesses for which definitely
1512 * no source was found.
1514 __isl_give isl_union_map *isl_union_flow_get_must_no_source(
1515 __isl_keep isl_union_flow *flow)
1517 if (!flow)
1518 return NULL;
1519 return isl_union_map_copy(flow->must_no_source);
1522 /* Return the subset of the sink accesses for which possibly
1523 * no source was found, including those for which definitely
1524 * no source was found.
1526 __isl_give isl_union_map *isl_union_flow_get_may_no_source(
1527 __isl_keep isl_union_flow *flow)
1529 if (!flow)
1530 return NULL;
1531 return isl_union_map_union(isl_union_map_copy(flow->must_no_source),
1532 isl_union_map_copy(flow->may_no_source));
1535 /* Return the subset of the sink accesses for which possibly, but not
1536 * definitely, no source was found.
1538 static __isl_give isl_union_map *isl_union_flow_get_non_must_no_source(
1539 __isl_keep isl_union_flow *flow)
1541 if (!flow)
1542 return NULL;
1543 return isl_union_map_copy(flow->may_no_source);
1546 /* Create a new isl_union_flow object, initialized with empty
1547 * dependence relations and sink subsets.
1549 static __isl_give isl_union_flow *isl_union_flow_alloc(
1550 __isl_take isl_space *space)
1552 isl_ctx *ctx;
1553 isl_union_map *empty;
1554 isl_union_flow *flow;
1556 if (!space)
1557 return NULL;
1558 ctx = isl_space_get_ctx(space);
1559 flow = isl_alloc_type(ctx, isl_union_flow);
1560 if (!flow)
1561 goto error;
1563 empty = isl_union_map_empty(space);
1564 flow->must_dep = isl_union_map_copy(empty);
1565 flow->may_dep = isl_union_map_copy(empty);
1566 flow->must_no_source = isl_union_map_copy(empty);
1567 flow->may_no_source = empty;
1569 if (!flow->must_dep || !flow->may_dep ||
1570 !flow->must_no_source || !flow->may_no_source)
1571 return isl_union_flow_free(flow);
1573 return flow;
1574 error:
1575 isl_space_free(space);
1576 return NULL;
1579 /* Drop the schedule dimensions from the iteration domains in "flow".
1580 * In particular, the schedule dimensions have been prepended
1581 * to the iteration domains prior to the dependence analysis by
1582 * replacing the iteration domain D, by the wrapped map [S -> D].
1583 * Replace these wrapped maps by the original D.
1585 static __isl_give isl_union_flow *isl_union_flow_drop_schedule(
1586 __isl_take isl_union_flow *flow)
1588 if (!flow)
1589 return NULL;
1591 flow->must_dep = isl_union_map_factor_range(flow->must_dep);
1592 flow->may_dep = isl_union_map_factor_range(flow->may_dep);
1593 flow->must_no_source =
1594 isl_union_map_domain_factor_range(flow->must_no_source);
1595 flow->may_no_source =
1596 isl_union_map_domain_factor_range(flow->may_no_source);
1598 if (!flow->must_dep || !flow->may_dep ||
1599 !flow->must_no_source || !flow->may_no_source)
1600 return isl_union_flow_free(flow);
1602 return flow;
1605 struct isl_compute_flow_data {
1606 isl_union_map *must_source;
1607 isl_union_map *may_source;
1608 isl_union_flow *flow;
1610 int count;
1611 int must;
1612 isl_space *dim;
1613 struct isl_sched_info *sink_info;
1614 struct isl_sched_info **source_info;
1615 isl_access_info *accesses;
1618 static int count_matching_array(__isl_take isl_map *map, void *user)
1620 int eq;
1621 isl_space *dim;
1622 struct isl_compute_flow_data *data;
1624 data = (struct isl_compute_flow_data *)user;
1626 dim = isl_space_range(isl_map_get_space(map));
1628 eq = isl_space_is_equal(dim, data->dim);
1630 isl_space_free(dim);
1631 isl_map_free(map);
1633 if (eq < 0)
1634 return -1;
1635 if (eq)
1636 data->count++;
1638 return 0;
1641 static int collect_matching_array(__isl_take isl_map *map, void *user)
1643 int eq;
1644 isl_space *dim;
1645 struct isl_sched_info *info;
1646 struct isl_compute_flow_data *data;
1648 data = (struct isl_compute_flow_data *)user;
1650 dim = isl_space_range(isl_map_get_space(map));
1652 eq = isl_space_is_equal(dim, data->dim);
1654 isl_space_free(dim);
1656 if (eq < 0)
1657 goto error;
1658 if (!eq) {
1659 isl_map_free(map);
1660 return 0;
1663 info = sched_info_alloc(map);
1664 data->source_info[data->count] = info;
1666 data->accesses = isl_access_info_add_source(data->accesses,
1667 map, data->must, info);
1669 data->count++;
1671 return 0;
1672 error:
1673 isl_map_free(map);
1674 return -1;
1677 /* Determine the shared nesting level and the "textual order" of
1678 * the given accesses.
1680 * We first determine the minimal schedule dimension for both accesses.
1682 * If among those dimensions, we can find one where both have a fixed
1683 * value and if moreover those values are different, then the previous
1684 * dimension is the last shared nesting level and the textual order
1685 * is determined based on the order of the fixed values.
1686 * If no such fixed values can be found, then we set the shared
1687 * nesting level to the minimal schedule dimension, with no textual ordering.
1689 static int before(void *first, void *second)
1691 struct isl_sched_info *info1 = first;
1692 struct isl_sched_info *info2 = second;
1693 int n1, n2;
1694 int i;
1696 n1 = isl_vec_size(info1->cst);
1697 n2 = isl_vec_size(info2->cst);
1699 if (n2 < n1)
1700 n1 = n2;
1702 for (i = 0; i < n1; ++i) {
1703 int r;
1704 int cmp;
1706 if (!info1->is_cst[i])
1707 continue;
1708 if (!info2->is_cst[i])
1709 continue;
1710 cmp = isl_vec_cmp_element(info1->cst, info2->cst, i);
1711 if (cmp == 0)
1712 continue;
1714 r = 2 * i + (cmp < 0);
1716 return r;
1719 return 2 * n1;
1722 /* Given a sink access, look for all the source accesses that access
1723 * the same array and perform dataflow analysis on them using
1724 * isl_access_info_compute_flow.
1726 static int compute_flow(__isl_take isl_map *map, void *user)
1728 int i;
1729 isl_ctx *ctx;
1730 struct isl_compute_flow_data *data;
1731 isl_flow *flow;
1732 isl_union_flow *df;
1734 data = (struct isl_compute_flow_data *)user;
1735 df = data->flow;
1737 ctx = isl_map_get_ctx(map);
1739 data->accesses = NULL;
1740 data->sink_info = NULL;
1741 data->source_info = NULL;
1742 data->count = 0;
1743 data->dim = isl_space_range(isl_map_get_space(map));
1745 if (isl_union_map_foreach_map(data->must_source,
1746 &count_matching_array, data) < 0)
1747 goto error;
1748 if (isl_union_map_foreach_map(data->may_source,
1749 &count_matching_array, data) < 0)
1750 goto error;
1752 data->sink_info = sched_info_alloc(map);
1753 data->source_info = isl_calloc_array(ctx, struct isl_sched_info *,
1754 data->count);
1756 data->accesses = isl_access_info_alloc(isl_map_copy(map),
1757 data->sink_info, &before, data->count);
1758 if (!data->sink_info || (data->count && !data->source_info) ||
1759 !data->accesses)
1760 goto error;
1761 data->count = 0;
1762 data->must = 1;
1763 if (isl_union_map_foreach_map(data->must_source,
1764 &collect_matching_array, data) < 0)
1765 goto error;
1766 data->must = 0;
1767 if (isl_union_map_foreach_map(data->may_source,
1768 &collect_matching_array, data) < 0)
1769 goto error;
1771 flow = isl_access_info_compute_flow(data->accesses);
1772 data->accesses = NULL;
1774 if (!flow)
1775 goto error;
1777 df->must_no_source = isl_union_map_union(df->must_no_source,
1778 isl_union_map_from_map(isl_flow_get_no_source(flow, 1)));
1779 df->may_no_source = isl_union_map_union(df->may_no_source,
1780 isl_union_map_from_map(isl_flow_get_no_source(flow, 0)));
1782 for (i = 0; i < flow->n_source; ++i) {
1783 isl_union_map *dep;
1784 dep = isl_union_map_from_map(isl_map_copy(flow->dep[i].map));
1785 if (flow->dep[i].must)
1786 df->must_dep = isl_union_map_union(df->must_dep, dep);
1787 else
1788 df->may_dep = isl_union_map_union(df->may_dep, dep);
1791 isl_flow_free(flow);
1793 sched_info_free(data->sink_info);
1794 if (data->source_info) {
1795 for (i = 0; i < data->count; ++i)
1796 sched_info_free(data->source_info[i]);
1797 free(data->source_info);
1799 isl_space_free(data->dim);
1800 isl_map_free(map);
1802 return 0;
1803 error:
1804 isl_access_info_free(data->accesses);
1805 sched_info_free(data->sink_info);
1806 if (data->source_info) {
1807 for (i = 0; i < data->count; ++i)
1808 sched_info_free(data->source_info[i]);
1809 free(data->source_info);
1811 isl_space_free(data->dim);
1812 isl_map_free(map);
1814 return -1;
1817 /* Remove the must accesses from the may accesses.
1819 * A must access always trumps a may access, so there is no need
1820 * for a must access to also be considered as a may access. Doing so
1821 * would only cost extra computations only to find out that
1822 * the duplicated may access does not make any difference.
1824 static __isl_give isl_union_access_info *isl_union_access_info_normalize(
1825 __isl_take isl_union_access_info *access)
1827 if (!access)
1828 return NULL;
1829 access->may_source = isl_union_map_subtract(access->may_source,
1830 isl_union_map_copy(access->must_source));
1831 if (!access->may_source)
1832 return isl_union_access_info_free(access);
1834 return access;
1837 /* Given a description of the "sink" accesses, the "source" accesses and
1838 * a schedule, compute for each instance of a sink access
1839 * and for each element accessed by that instance,
1840 * the possible or definite source accesses that last accessed the
1841 * element accessed by the sink access before this sink access
1842 * in the sense that there is no intermediate definite source access.
1844 * The must_no_source and may_no_source elements of the result
1845 * are subsets of access->sink. The elements must_dep and may_dep
1846 * map domain elements of access->{may,must)_source to
1847 * domain elements of access->sink.
1849 * This function is used when only the schedule map representation
1850 * is available.
1852 * We first prepend the schedule dimensions to the domain
1853 * of the accesses so that we can easily compare their relative order.
1854 * Then we consider each sink access individually in compute_flow.
1856 static __isl_give isl_union_flow *compute_flow_union_map(
1857 __isl_take isl_union_access_info *access)
1859 struct isl_compute_flow_data data;
1861 access = isl_union_access_info_align_params(access);
1862 access = isl_union_access_info_introduce_schedule(access);
1863 if (!access)
1864 return NULL;
1866 data.must_source = access->must_source;
1867 data.may_source = access->may_source;
1869 data.flow = isl_union_flow_alloc(isl_union_map_get_space(access->sink));
1871 if (isl_union_map_foreach_map(access->sink, &compute_flow, &data) < 0)
1872 goto error;
1874 data.flow = isl_union_flow_drop_schedule(data.flow);
1876 isl_union_access_info_free(access);
1877 return data.flow;
1878 error:
1879 isl_union_access_info_free(access);
1880 isl_union_flow_free(data.flow);
1881 return NULL;
1884 /* A schedule access relation.
1886 * The access relation "access" is of the form [S -> D] -> A,
1887 * where S corresponds to the prefix schedule at "node".
1888 * "must" is only relevant for source accesses and indicates
1889 * whether the access is a must source or a may source.
1891 struct isl_scheduled_access {
1892 isl_map *access;
1893 int must;
1894 isl_schedule_node *node;
1897 /* Data structure for keeping track of individual scheduled sink and source
1898 * accesses when computing dependence analysis based on a schedule tree.
1900 * "n_sink" is the number of used entries in "sink"
1901 * "n_source" is the number of used entries in "source"
1903 * "set_sink", "must" and "node" are only used inside collect_sink_source,
1904 * to keep track of the current node and
1905 * of what extract_sink_source needs to do.
1907 struct isl_compute_flow_schedule_data {
1908 isl_union_access_info *access;
1910 int n_sink;
1911 int n_source;
1913 struct isl_scheduled_access *sink;
1914 struct isl_scheduled_access *source;
1916 int set_sink;
1917 int must;
1918 isl_schedule_node *node;
1921 /* Align the parameters of all sinks with all sources.
1923 * If there are no sinks or no sources, then no alignment is needed.
1925 static void isl_compute_flow_schedule_data_align_params(
1926 struct isl_compute_flow_schedule_data *data)
1928 int i;
1929 isl_space *space;
1931 if (data->n_sink == 0 || data->n_source == 0)
1932 return;
1934 space = isl_map_get_space(data->sink[0].access);
1936 for (i = 1; i < data->n_sink; ++i)
1937 space = isl_space_align_params(space,
1938 isl_map_get_space(data->sink[i].access));
1939 for (i = 0; i < data->n_source; ++i)
1940 space = isl_space_align_params(space,
1941 isl_map_get_space(data->source[i].access));
1943 for (i = 0; i < data->n_sink; ++i)
1944 data->sink[i].access =
1945 isl_map_align_params(data->sink[i].access,
1946 isl_space_copy(space));
1947 for (i = 0; i < data->n_source; ++i)
1948 data->source[i].access =
1949 isl_map_align_params(data->source[i].access,
1950 isl_space_copy(space));
1952 isl_space_free(space);
1955 /* Free all the memory referenced from "data".
1956 * Do not free "data" itself as it may be allocated on the stack.
1958 static void isl_compute_flow_schedule_data_clear(
1959 struct isl_compute_flow_schedule_data *data)
1961 int i;
1963 if (!data->sink)
1964 return;
1966 for (i = 0; i < data->n_sink; ++i) {
1967 isl_map_free(data->sink[i].access);
1968 isl_schedule_node_free(data->sink[i].node);
1971 for (i = 0; i < data->n_source; ++i) {
1972 isl_map_free(data->source[i].access);
1973 isl_schedule_node_free(data->source[i].node);
1976 free(data->sink);
1979 /* isl_schedule_foreach_schedule_node callback for counting
1980 * (an upper bound on) the number of sinks and sources.
1982 * Sinks and sources are only extracted at leaves of the tree,
1983 * so we skip the node if it is not a leaf.
1984 * Otherwise we increment data->n_sink and data->n_source with
1985 * the number of spaces in the sink and source access domains
1986 * that reach this node.
1988 static int count_sink_source(__isl_keep isl_schedule_node *node, void *user)
1990 struct isl_compute_flow_schedule_data *data = user;
1991 isl_union_set *domain;
1992 isl_union_map *umap;
1993 int r = 0;
1995 if (isl_schedule_node_get_type(node) != isl_schedule_node_leaf)
1996 return 1;
1998 domain = isl_schedule_node_get_universe_domain(node);
2000 umap = isl_union_map_copy(data->access->sink);
2001 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
2002 data->n_sink += isl_union_map_n_map(umap);
2003 isl_union_map_free(umap);
2004 if (!umap)
2005 r = -1;
2007 umap = isl_union_map_copy(data->access->must_source);
2008 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
2009 data->n_source += isl_union_map_n_map(umap);
2010 isl_union_map_free(umap);
2011 if (!umap)
2012 r = -1;
2014 umap = isl_union_map_copy(data->access->may_source);
2015 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
2016 data->n_source += isl_union_map_n_map(umap);
2017 isl_union_map_free(umap);
2018 if (!umap)
2019 r = -1;
2021 isl_union_set_free(domain);
2023 return r;
2026 /* Add a single scheduled sink or source (depending on data->set_sink)
2027 * with scheduled access relation "map", must property data->must and
2028 * schedule node data->node to the list of sinks or sources.
2030 static int extract_sink_source(__isl_take isl_map *map, void *user)
2032 struct isl_compute_flow_schedule_data *data = user;
2033 struct isl_scheduled_access *access;
2035 if (data->set_sink)
2036 access = data->sink + data->n_sink++;
2037 else
2038 access = data->source + data->n_source++;
2040 access->access = map;
2041 access->must = data->must;
2042 access->node = isl_schedule_node_copy(data->node);
2044 return 0;
2047 /* isl_schedule_foreach_schedule_node callback for collecting
2048 * individual scheduled source and sink accesses.
2050 * We only collect accesses at the leaves of the schedule tree.
2051 * We prepend the schedule dimensions at the leaf to the iteration
2052 * domains of the source and sink accesses and then extract
2053 * the individual accesses (per space).
2055 * In particular, if the prefix schedule at the node is of the form
2057 * D -> S
2059 * while the access relations are of the form
2061 * D -> A
2063 * then the updated access relations are of the form
2065 * [S -> D] -> A
2067 * Note that S consists of a single space such that introducing S
2068 * in the access relations does not increase the number of spaces.
2070 static int collect_sink_source(__isl_keep isl_schedule_node *node, void *user)
2072 struct isl_compute_flow_schedule_data *data = user;
2073 isl_union_map *prefix;
2074 isl_union_map *umap;
2075 int r = 0;
2077 if (isl_schedule_node_get_type(node) != isl_schedule_node_leaf)
2078 return 1;
2080 data->node = node;
2082 prefix = isl_schedule_node_get_prefix_schedule_union_map(node);
2083 prefix = isl_union_map_reverse(prefix);
2084 prefix = isl_union_map_range_map(prefix);
2086 data->set_sink = 1;
2087 umap = isl_union_map_copy(data->access->sink);
2088 umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
2089 if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
2090 r = -1;
2091 isl_union_map_free(umap);
2093 data->set_sink = 0;
2094 data->must = 1;
2095 umap = isl_union_map_copy(data->access->must_source);
2096 umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
2097 if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
2098 r = -1;
2099 isl_union_map_free(umap);
2101 data->set_sink = 0;
2102 data->must = 0;
2103 umap = isl_union_map_copy(data->access->may_source);
2104 umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
2105 if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
2106 r = -1;
2107 isl_union_map_free(umap);
2109 isl_union_map_free(prefix);
2111 return r;
2114 /* isl_access_info_compute_flow callback for determining whether
2115 * the shared nesting level and the ordering within that level
2116 * for two scheduled accesses for use in compute_single_flow.
2118 * The tokens passed to this function refer to the leaves
2119 * in the schedule tree where the accesses take place.
2121 * If n is the shared number of loops, then we need to return
2122 * "2 * n + 1" if "first" precedes "second" inside the innermost
2123 * shared loop and "2 * n" otherwise.
2125 * The innermost shared ancestor may be the leaves themselves
2126 * if the accesses take place in the same leaf. Otherwise,
2127 * it is either a set node or a sequence node. Only in the case
2128 * of a sequence node do we consider one access to precede the other.
2130 static int before_node(void *first, void *second)
2132 isl_schedule_node *node1 = first;
2133 isl_schedule_node *node2 = second;
2134 isl_schedule_node *shared;
2135 int depth;
2136 int before = 0;
2138 shared = isl_schedule_node_get_shared_ancestor(node1, node2);
2139 if (!shared)
2140 return -1;
2142 depth = isl_schedule_node_get_schedule_depth(shared);
2143 if (isl_schedule_node_get_type(shared) == isl_schedule_node_sequence) {
2144 int pos1, pos2;
2146 pos1 = isl_schedule_node_get_ancestor_child_position(node1,
2147 shared);
2148 pos2 = isl_schedule_node_get_ancestor_child_position(node2,
2149 shared);
2150 before = pos1 < pos2;
2153 isl_schedule_node_free(shared);
2155 return 2 * depth + before;
2158 /* Add the scheduled sources from "data" that access
2159 * the same data space as "sink" to "access".
2161 static __isl_give isl_access_info *add_matching_sources(
2162 __isl_take isl_access_info *access, struct isl_scheduled_access *sink,
2163 struct isl_compute_flow_schedule_data *data)
2165 int i;
2166 isl_space *space;
2168 space = isl_space_range(isl_map_get_space(sink->access));
2169 for (i = 0; i < data->n_source; ++i) {
2170 struct isl_scheduled_access *source;
2171 isl_space *source_space;
2172 int eq;
2174 source = &data->source[i];
2175 source_space = isl_map_get_space(source->access);
2176 source_space = isl_space_range(source_space);
2177 eq = isl_space_is_equal(space, source_space);
2178 isl_space_free(source_space);
2180 if (!eq)
2181 continue;
2182 if (eq < 0)
2183 goto error;
2185 access = isl_access_info_add_source(access,
2186 isl_map_copy(source->access), source->must, source->node);
2189 isl_space_free(space);
2190 return access;
2191 error:
2192 isl_space_free(space);
2193 isl_access_info_free(access);
2194 return NULL;
2197 /* Given a scheduled sink access relation "sink", compute the corresponding
2198 * dependences on the sources in "data" and add the computed dependences
2199 * to "uf".
2201 static __isl_give isl_union_flow *compute_single_flow(
2202 __isl_take isl_union_flow *uf, struct isl_scheduled_access *sink,
2203 struct isl_compute_flow_schedule_data *data)
2205 int i;
2206 isl_access_info *access;
2207 isl_flow *flow;
2208 isl_map *map;
2210 if (!uf)
2211 return NULL;
2213 access = isl_access_info_alloc(isl_map_copy(sink->access), sink->node,
2214 &before_node, data->n_source);
2215 access = add_matching_sources(access, sink, data);
2217 flow = isl_access_info_compute_flow(access);
2218 if (!flow)
2219 return isl_union_flow_free(uf);
2221 map = isl_map_domain_factor_range(isl_flow_get_no_source(flow, 1));
2222 uf->must_no_source = isl_union_map_union(uf->must_no_source,
2223 isl_union_map_from_map(map));
2224 map = isl_map_domain_factor_range(isl_flow_get_no_source(flow, 0));
2225 uf->may_no_source = isl_union_map_union(uf->may_no_source,
2226 isl_union_map_from_map(map));
2228 for (i = 0; i < flow->n_source; ++i) {
2229 isl_union_map *dep;
2231 map = isl_map_factor_range(isl_map_copy(flow->dep[i].map));
2232 dep = isl_union_map_from_map(map);
2233 if (flow->dep[i].must)
2234 uf->must_dep = isl_union_map_union(uf->must_dep, dep);
2235 else
2236 uf->may_dep = isl_union_map_union(uf->may_dep, dep);
2239 isl_flow_free(flow);
2241 return uf;
2244 /* Given a description of the "sink" accesses, the "source" accesses and
2245 * a schedule, compute for each instance of a sink access
2246 * and for each element accessed by that instance,
2247 * the possible or definite source accesses that last accessed the
2248 * element accessed by the sink access before this sink access
2249 * in the sense that there is no intermediate definite source access.
2251 * The must_no_source and may_no_source elements of the result
2252 * are subsets of access->sink. The elements must_dep and may_dep
2253 * map domain elements of access->{may,must)_source to
2254 * domain elements of access->sink.
2256 * This function is used when a schedule tree representation
2257 * is available.
2259 * We extract the individual scheduled source and sink access relations and
2260 * then compute dependences for each scheduled sink individually.
2262 static __isl_give isl_union_flow *compute_flow_schedule(
2263 __isl_take isl_union_access_info *access)
2265 struct isl_compute_flow_schedule_data data = { access };
2266 int i, n;
2267 isl_ctx *ctx;
2268 isl_union_flow *flow;
2270 ctx = isl_union_access_info_get_ctx(access);
2272 data.n_sink = 0;
2273 data.n_source = 0;
2274 if (isl_schedule_foreach_schedule_node(access->schedule,
2275 &count_sink_source, &data) < 0)
2276 goto error;
2278 n = data.n_sink + data.n_source;
2279 data.sink = isl_calloc_array(ctx, struct isl_scheduled_access, n);
2280 if (n && !data.sink)
2281 goto error;
2282 data.source = data.sink + data.n_sink;
2284 data.n_sink = 0;
2285 data.n_source = 0;
2286 if (isl_schedule_foreach_schedule_node(access->schedule,
2287 &collect_sink_source, &data) < 0)
2288 goto error;
2290 flow = isl_union_flow_alloc(isl_union_map_get_space(access->sink));
2292 isl_compute_flow_schedule_data_align_params(&data);
2294 for (i = 0; i < data.n_sink; ++i)
2295 flow = compute_single_flow(flow, &data.sink[i], &data);
2297 isl_compute_flow_schedule_data_clear(&data);
2299 isl_union_access_info_free(access);
2300 return flow;
2301 error:
2302 isl_union_access_info_free(access);
2303 isl_compute_flow_schedule_data_clear(&data);
2304 return NULL;
2307 /* Given a description of the "sink" accesses, the "source" accesses and
2308 * a schedule, compute for each instance of a sink access
2309 * and for each element accessed by that instance,
2310 * the possible or definite source accesses that last accessed the
2311 * element accessed by the sink access before this sink access
2312 * in the sense that there is no intermediate definite source access.
2314 * The must_no_source and may_no_source elements of the result
2315 * are subsets of access->sink. The elements must_dep and may_dep
2316 * map domain elements of access->{may,must)_source to
2317 * domain elements of access->sink.
2319 * We check whether the schedule is available as a schedule tree
2320 * or a schedule map and call the correpsonding function to perform
2321 * the analysis.
2323 __isl_give isl_union_flow *isl_union_access_info_compute_flow(
2324 __isl_take isl_union_access_info *access)
2326 access = isl_union_access_info_normalize(access);
2327 if (!access)
2328 return NULL;
2329 if (access->schedule)
2330 return compute_flow_schedule(access);
2331 else
2332 return compute_flow_union_map(access);
2335 /* Given a collection of "sink" and "source" accesses,
2336 * compute for each iteration of a sink access
2337 * and for each element accessed by that iteration,
2338 * the source access in the list that last accessed the
2339 * element accessed by the sink access before this sink access.
2340 * Each access is given as a map from the loop iterators
2341 * to the array indices.
2342 * The result is a relations between source and sink
2343 * iterations and a subset of the domain of the sink accesses,
2344 * corresponding to those iterations that access an element
2345 * not previously accessed.
2347 * We collect the inputs in an isl_union_access_info object,
2348 * call isl_union_access_info_compute_flow and extract
2349 * the outputs from the result.
2351 int isl_union_map_compute_flow(__isl_take isl_union_map *sink,
2352 __isl_take isl_union_map *must_source,
2353 __isl_take isl_union_map *may_source,
2354 __isl_take isl_union_map *schedule,
2355 __isl_give isl_union_map **must_dep, __isl_give isl_union_map **may_dep,
2356 __isl_give isl_union_map **must_no_source,
2357 __isl_give isl_union_map **may_no_source)
2359 isl_union_access_info *access;
2360 isl_union_flow *flow;
2362 access = isl_union_access_info_from_sink(sink);
2363 access = isl_union_access_info_set_must_source(access, must_source);
2364 access = isl_union_access_info_set_may_source(access, may_source);
2365 access = isl_union_access_info_set_schedule_map(access, schedule);
2366 flow = isl_union_access_info_compute_flow(access);
2368 if (must_dep)
2369 *must_dep = isl_union_flow_get_must_dependence(flow);
2370 if (may_dep)
2371 *may_dep = isl_union_flow_get_non_must_dependence(flow);
2372 if (must_no_source)
2373 *must_no_source = isl_union_flow_get_must_no_source(flow);
2374 if (may_no_source)
2375 *may_no_source = isl_union_flow_get_non_must_no_source(flow);
2377 isl_union_flow_free(flow);
2379 if ((must_dep && !*must_dep) || (may_dep && !*may_dep) ||
2380 (must_no_source && !*must_no_source) ||
2381 (may_no_source && !*may_no_source))
2382 goto error;
2384 return 0;
2385 error:
2386 if (must_dep)
2387 *must_dep = isl_union_map_free(*must_dep);
2388 if (may_dep)
2389 *may_dep = isl_union_map_free(*may_dep);
2390 if (must_no_source)
2391 *must_no_source = isl_union_map_free(*must_no_source);
2392 if (may_no_source)
2393 *may_no_source = isl_union_map_free(*may_no_source);
2394 return -1;