drop isl_basic_set_dim_is_unique
[isl.git] / isl_flow.c
blob6e2a9a3a51a7eec3660917d0e8cc8250a4735eb1
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.
170 * This field is only needed when restrict_fn is set and
171 * the field itself is set by isl_access_info_compute_flow.
173 * restrict_fn is a callback that (if not NULL) will be called
174 * right before any lexicographical maximization.
176 struct isl_access_info {
177 isl_map *domain_map;
178 struct isl_labeled_map sink;
179 isl_access_level_before level_before;
181 isl_access_restrict restrict_fn;
182 void *restrict_user;
184 int max_source;
185 int n_must;
186 int n_may;
187 struct isl_labeled_map source[1];
190 /* A structure containing the output of dependence analysis:
191 * - n_source dependences
192 * - a wrapped subset of the sink for which definitely no source could be found
193 * - a wrapped subset of the sink for which possibly no source could be found
195 struct isl_flow {
196 isl_set *must_no_source;
197 isl_set *may_no_source;
198 int n_source;
199 struct isl_labeled_map *dep;
202 /* Construct an isl_access_info structure and fill it up with
203 * the given data. The number of sources is set to 0.
205 __isl_give isl_access_info *isl_access_info_alloc(__isl_take isl_map *sink,
206 void *sink_user, isl_access_level_before fn, int max_source)
208 isl_ctx *ctx;
209 struct isl_access_info *acc;
211 if (!sink)
212 return NULL;
214 ctx = isl_map_get_ctx(sink);
215 isl_assert(ctx, max_source >= 0, goto error);
217 acc = isl_calloc(ctx, struct isl_access_info,
218 sizeof(struct isl_access_info) +
219 (max_source - 1) * sizeof(struct isl_labeled_map));
220 if (!acc)
221 goto error;
223 acc->sink.map = sink;
224 acc->sink.data = sink_user;
225 acc->level_before = fn;
226 acc->max_source = max_source;
227 acc->n_must = 0;
228 acc->n_may = 0;
230 return acc;
231 error:
232 isl_map_free(sink);
233 return NULL;
236 /* Free the given isl_access_info structure.
238 __isl_null isl_access_info *isl_access_info_free(
239 __isl_take isl_access_info *acc)
241 int i;
243 if (!acc)
244 return NULL;
245 isl_map_free(acc->domain_map);
246 isl_map_free(acc->sink.map);
247 for (i = 0; i < acc->n_must + acc->n_may; ++i)
248 isl_map_free(acc->source[i].map);
249 free(acc);
250 return NULL;
253 isl_ctx *isl_access_info_get_ctx(__isl_keep isl_access_info *acc)
255 return acc ? isl_map_get_ctx(acc->sink.map) : NULL;
258 __isl_give isl_access_info *isl_access_info_set_restrict(
259 __isl_take isl_access_info *acc, isl_access_restrict fn, void *user)
261 if (!acc)
262 return NULL;
263 acc->restrict_fn = fn;
264 acc->restrict_user = user;
265 return acc;
268 /* Add another source to an isl_access_info structure, making
269 * sure the "must" sources are placed before the "may" sources.
270 * This function may be called at most max_source times on a
271 * given isl_access_info structure, with max_source as specified
272 * in the call to isl_access_info_alloc that constructed the structure.
274 __isl_give isl_access_info *isl_access_info_add_source(
275 __isl_take isl_access_info *acc, __isl_take isl_map *source,
276 int must, void *source_user)
278 isl_ctx *ctx;
280 if (!acc)
281 goto error;
282 ctx = isl_map_get_ctx(acc->sink.map);
283 isl_assert(ctx, acc->n_must + acc->n_may < acc->max_source, goto error);
285 if (must) {
286 if (acc->n_may)
287 acc->source[acc->n_must + acc->n_may] =
288 acc->source[acc->n_must];
289 acc->source[acc->n_must].map = source;
290 acc->source[acc->n_must].data = source_user;
291 acc->source[acc->n_must].must = 1;
292 acc->n_must++;
293 } else {
294 acc->source[acc->n_must + acc->n_may].map = source;
295 acc->source[acc->n_must + acc->n_may].data = source_user;
296 acc->source[acc->n_must + acc->n_may].must = 0;
297 acc->n_may++;
300 return acc;
301 error:
302 isl_map_free(source);
303 isl_access_info_free(acc);
304 return NULL;
307 /* Return -n, 0 or n (with n a positive value), depending on whether
308 * the source access identified by p1 should be sorted before, together
309 * or after that identified by p2.
311 * If p1 appears before p2, then it should be sorted first.
312 * For more generic initial schedules, it is possible that neither
313 * p1 nor p2 appears before the other, or at least not in any obvious way.
314 * We therefore also check if p2 appears before p1, in which case p2
315 * should be sorted first.
316 * If not, we try to order the two statements based on the description
317 * of the iteration domains. This results in an arbitrary, but fairly
318 * stable ordering.
320 static int access_sort_cmp(const void *p1, const void *p2, void *user)
322 isl_access_info *acc = user;
323 const struct isl_labeled_map *i1, *i2;
324 int level1, level2;
325 uint32_t h1, h2;
326 i1 = (const struct isl_labeled_map *) p1;
327 i2 = (const struct isl_labeled_map *) p2;
329 level1 = acc->level_before(i1->data, i2->data);
330 if (level1 % 2)
331 return -1;
333 level2 = acc->level_before(i2->data, i1->data);
334 if (level2 % 2)
335 return 1;
337 h1 = isl_map_get_hash(i1->map);
338 h2 = isl_map_get_hash(i2->map);
339 return h1 > h2 ? 1 : h1 < h2 ? -1 : 0;
342 /* Sort the must source accesses in their textual order.
344 static __isl_give isl_access_info *isl_access_info_sort_sources(
345 __isl_take isl_access_info *acc)
347 if (!acc)
348 return NULL;
349 if (acc->n_must <= 1)
350 return acc;
352 if (isl_sort(acc->source, acc->n_must, sizeof(struct isl_labeled_map),
353 access_sort_cmp, acc) < 0)
354 return isl_access_info_free(acc);
356 return acc;
359 /* Align the parameters of the two spaces if needed and then call
360 * isl_space_join.
362 static __isl_give isl_space *space_align_and_join(__isl_take isl_space *left,
363 __isl_take isl_space *right)
365 if (isl_space_match(left, isl_dim_param, right, isl_dim_param))
366 return isl_space_join(left, right);
368 left = isl_space_align_params(left, isl_space_copy(right));
369 right = isl_space_align_params(right, isl_space_copy(left));
370 return isl_space_join(left, right);
373 /* Initialize an empty isl_flow structure corresponding to a given
374 * isl_access_info structure.
375 * For each must access, two dependences are created (initialized
376 * to the empty relation), one for the resulting must dependences
377 * and one for the resulting may dependences. May accesses can
378 * only lead to may dependences, so only one dependence is created
379 * for each of them.
380 * This function is private as isl_flow structures are only supposed
381 * to be created by isl_access_info_compute_flow.
383 static __isl_give isl_flow *isl_flow_alloc(__isl_keep isl_access_info *acc)
385 int i, n;
386 struct isl_ctx *ctx;
387 struct isl_flow *dep;
389 if (!acc)
390 return NULL;
392 ctx = isl_map_get_ctx(acc->sink.map);
393 dep = isl_calloc_type(ctx, struct isl_flow);
394 if (!dep)
395 return NULL;
397 n = 2 * acc->n_must + acc->n_may;
398 dep->dep = isl_calloc_array(ctx, struct isl_labeled_map, n);
399 if (n && !dep->dep)
400 goto error;
402 dep->n_source = n;
403 for (i = 0; i < acc->n_must; ++i) {
404 isl_space *dim;
405 dim = space_align_and_join(
406 isl_map_get_space(acc->source[i].map),
407 isl_space_reverse(isl_map_get_space(acc->sink.map)));
408 dep->dep[2 * i].map = isl_map_empty(dim);
409 dep->dep[2 * i + 1].map = isl_map_copy(dep->dep[2 * i].map);
410 dep->dep[2 * i].data = acc->source[i].data;
411 dep->dep[2 * i + 1].data = acc->source[i].data;
412 dep->dep[2 * i].must = 1;
413 dep->dep[2 * i + 1].must = 0;
414 if (!dep->dep[2 * i].map || !dep->dep[2 * i + 1].map)
415 goto error;
417 for (i = acc->n_must; i < acc->n_must + acc->n_may; ++i) {
418 isl_space *dim;
419 dim = space_align_and_join(
420 isl_map_get_space(acc->source[i].map),
421 isl_space_reverse(isl_map_get_space(acc->sink.map)));
422 dep->dep[acc->n_must + i].map = isl_map_empty(dim);
423 dep->dep[acc->n_must + i].data = acc->source[i].data;
424 dep->dep[acc->n_must + i].must = 0;
425 if (!dep->dep[acc->n_must + i].map)
426 goto error;
429 return dep;
430 error:
431 isl_flow_free(dep);
432 return NULL;
435 /* Iterate over all sources and for each resulting flow dependence
436 * that is not empty, call the user specfied function.
437 * The second argument in this function call identifies the source,
438 * while the third argument correspond to the final argument of
439 * the isl_flow_foreach call.
441 isl_stat isl_flow_foreach(__isl_keep isl_flow *deps,
442 isl_stat (*fn)(__isl_take isl_map *dep, int must, void *dep_user,
443 void *user),
444 void *user)
446 int i;
448 if (!deps)
449 return isl_stat_error;
451 for (i = 0; i < deps->n_source; ++i) {
452 if (isl_map_plain_is_empty(deps->dep[i].map))
453 continue;
454 if (fn(isl_map_copy(deps->dep[i].map), deps->dep[i].must,
455 deps->dep[i].data, user) < 0)
456 return isl_stat_error;
459 return isl_stat_ok;
462 /* Return a copy of the subset of the sink for which no source could be found.
464 __isl_give isl_map *isl_flow_get_no_source(__isl_keep isl_flow *deps, int must)
466 if (!deps)
467 return NULL;
469 if (must)
470 return isl_set_unwrap(isl_set_copy(deps->must_no_source));
471 else
472 return isl_set_unwrap(isl_set_copy(deps->may_no_source));
475 void isl_flow_free(__isl_take isl_flow *deps)
477 int i;
479 if (!deps)
480 return;
481 isl_set_free(deps->must_no_source);
482 isl_set_free(deps->may_no_source);
483 if (deps->dep) {
484 for (i = 0; i < deps->n_source; ++i)
485 isl_map_free(deps->dep[i].map);
486 free(deps->dep);
488 free(deps);
491 isl_ctx *isl_flow_get_ctx(__isl_keep isl_flow *deps)
493 return deps ? isl_set_get_ctx(deps->must_no_source) : NULL;
496 /* Return a map that enforces that the domain iteration occurs after
497 * the range iteration at the given level.
498 * If level is odd, then the domain iteration should occur after
499 * the target iteration in their shared level/2 outermost loops.
500 * In this case we simply need to enforce that these outermost
501 * loop iterations are the same.
502 * If level is even, then the loop iterator of the domain should
503 * be greater than the loop iterator of the range at the last
504 * of the level/2 shared loops, i.e., loop level/2 - 1.
506 static __isl_give isl_map *after_at_level(__isl_take isl_space *dim, int level)
508 struct isl_basic_map *bmap;
510 if (level % 2)
511 bmap = isl_basic_map_equal(dim, level/2);
512 else
513 bmap = isl_basic_map_more_at(dim, level/2 - 1);
515 return isl_map_from_basic_map(bmap);
518 /* Compute the partial lexicographic maximum of "dep" on domain "sink",
519 * but first check if the user has set acc->restrict_fn and if so
520 * update either the input or the output of the maximization problem
521 * with respect to the resulting restriction.
523 * Since the user expects a mapping from sink iterations to source iterations,
524 * whereas the domain of "dep" is a wrapped map, mapping sink iterations
525 * to accessed array elements, we first need to project out the accessed
526 * sink array elements by applying acc->domain_map.
527 * Similarly, the sink restriction specified by the user needs to be
528 * converted back to the wrapped map.
530 static __isl_give isl_map *restricted_partial_lexmax(
531 __isl_keep isl_access_info *acc, __isl_take isl_map *dep,
532 int source, __isl_take isl_set *sink, __isl_give isl_set **empty)
534 isl_map *source_map;
535 isl_restriction *restr;
536 isl_set *sink_domain;
537 isl_set *sink_restr;
538 isl_map *res;
540 if (!acc->restrict_fn)
541 return isl_map_partial_lexmax(dep, sink, empty);
543 source_map = isl_map_copy(dep);
544 source_map = isl_map_apply_domain(source_map,
545 isl_map_copy(acc->domain_map));
546 sink_domain = isl_set_copy(sink);
547 sink_domain = isl_set_apply(sink_domain, isl_map_copy(acc->domain_map));
548 restr = acc->restrict_fn(source_map, sink_domain,
549 acc->source[source].data, acc->restrict_user);
550 isl_set_free(sink_domain);
551 isl_map_free(source_map);
553 if (!restr)
554 goto error;
555 if (restr->type == isl_restriction_type_input) {
556 dep = isl_map_intersect_range(dep, isl_set_copy(restr->source));
557 sink_restr = isl_set_copy(restr->sink);
558 sink_restr = isl_set_apply(sink_restr,
559 isl_map_reverse(isl_map_copy(acc->domain_map)));
560 sink = isl_set_intersect(sink, sink_restr);
561 } else if (restr->type == isl_restriction_type_empty) {
562 isl_space *space = isl_map_get_space(dep);
563 isl_map_free(dep);
564 dep = isl_map_empty(space);
567 res = isl_map_partial_lexmax(dep, sink, empty);
569 if (restr->type == isl_restriction_type_output)
570 res = isl_map_intersect_range(res, isl_set_copy(restr->source));
572 isl_restriction_free(restr);
573 return res;
574 error:
575 isl_map_free(dep);
576 isl_set_free(sink);
577 *empty = NULL;
578 return NULL;
581 /* Compute the last iteration of must source j that precedes the sink
582 * at the given level for sink iterations in set_C.
583 * The subset of set_C for which no such iteration can be found is returned
584 * in *empty.
586 static struct isl_map *last_source(struct isl_access_info *acc,
587 struct isl_set *set_C,
588 int j, int level, struct isl_set **empty)
590 struct isl_map *read_map;
591 struct isl_map *write_map;
592 struct isl_map *dep_map;
593 struct isl_map *after;
594 struct isl_map *result;
596 read_map = isl_map_copy(acc->sink.map);
597 write_map = isl_map_copy(acc->source[j].map);
598 write_map = isl_map_reverse(write_map);
599 dep_map = isl_map_apply_range(read_map, write_map);
600 after = after_at_level(isl_map_get_space(dep_map), level);
601 dep_map = isl_map_intersect(dep_map, after);
602 result = restricted_partial_lexmax(acc, dep_map, j, set_C, empty);
603 result = isl_map_reverse(result);
605 return result;
608 /* For a given mapping between iterations of must source j and iterations
609 * of the sink, compute the last iteration of must source k preceding
610 * the sink at level before_level for any of the sink iterations,
611 * but following the corresponding iteration of must source j at level
612 * after_level.
614 static struct isl_map *last_later_source(struct isl_access_info *acc,
615 struct isl_map *old_map,
616 int j, int before_level,
617 int k, int after_level,
618 struct isl_set **empty)
620 isl_space *dim;
621 struct isl_set *set_C;
622 struct isl_map *read_map;
623 struct isl_map *write_map;
624 struct isl_map *dep_map;
625 struct isl_map *after_write;
626 struct isl_map *before_read;
627 struct isl_map *result;
629 set_C = isl_map_range(isl_map_copy(old_map));
630 read_map = isl_map_copy(acc->sink.map);
631 write_map = isl_map_copy(acc->source[k].map);
633 write_map = isl_map_reverse(write_map);
634 dep_map = isl_map_apply_range(read_map, write_map);
635 dim = space_align_and_join(isl_map_get_space(acc->source[k].map),
636 isl_space_reverse(isl_map_get_space(acc->source[j].map)));
637 after_write = after_at_level(dim, after_level);
638 after_write = isl_map_apply_range(after_write, old_map);
639 after_write = isl_map_reverse(after_write);
640 dep_map = isl_map_intersect(dep_map, after_write);
641 before_read = after_at_level(isl_map_get_space(dep_map), before_level);
642 dep_map = isl_map_intersect(dep_map, before_read);
643 result = restricted_partial_lexmax(acc, dep_map, k, set_C, empty);
644 result = isl_map_reverse(result);
646 return result;
649 /* Given a shared_level between two accesses, return 1 if the
650 * the first can precede the second at the requested target_level.
651 * If the target level is odd, i.e., refers to a statement level
652 * dimension, then first needs to precede second at the requested
653 * level, i.e., shared_level must be equal to target_level.
654 * If the target level is odd, then the two loops should share
655 * at least the requested number of outer loops.
657 static int can_precede_at_level(int shared_level, int target_level)
659 if (shared_level < target_level)
660 return 0;
661 if ((target_level % 2) && shared_level > target_level)
662 return 0;
663 return 1;
666 /* Given a possible flow dependence temp_rel[j] between source j and the sink
667 * at level sink_level, remove those elements for which
668 * there is an iteration of another source k < j that is closer to the sink.
669 * The flow dependences temp_rel[k] are updated with the improved sources.
670 * Any improved source needs to precede the sink at the same level
671 * and needs to follow source j at the same or a deeper level.
672 * The lower this level, the later the execution date of source k.
673 * We therefore consider lower levels first.
675 * If temp_rel[j] is empty, then there can be no improvement and
676 * we return immediately.
678 static int intermediate_sources(__isl_keep isl_access_info *acc,
679 struct isl_map **temp_rel, int j, int sink_level)
681 int k, level;
682 int depth = 2 * isl_map_dim(acc->source[j].map, isl_dim_in) + 1;
684 if (isl_map_plain_is_empty(temp_rel[j]))
685 return 0;
687 for (k = j - 1; k >= 0; --k) {
688 int plevel, plevel2;
689 plevel = acc->level_before(acc->source[k].data, acc->sink.data);
690 if (!can_precede_at_level(plevel, sink_level))
691 continue;
693 plevel2 = acc->level_before(acc->source[j].data,
694 acc->source[k].data);
696 for (level = sink_level; level <= depth; ++level) {
697 struct isl_map *T;
698 struct isl_set *trest;
699 struct isl_map *copy;
701 if (!can_precede_at_level(plevel2, level))
702 continue;
704 copy = isl_map_copy(temp_rel[j]);
705 T = last_later_source(acc, copy, j, sink_level, k,
706 level, &trest);
707 if (isl_map_plain_is_empty(T)) {
708 isl_set_free(trest);
709 isl_map_free(T);
710 continue;
712 temp_rel[j] = isl_map_intersect_range(temp_rel[j], trest);
713 temp_rel[k] = isl_map_union_disjoint(temp_rel[k], T);
717 return 0;
720 /* Compute all iterations of may source j that precedes the sink at the given
721 * level for sink iterations in set_C.
723 static __isl_give isl_map *all_sources(__isl_keep isl_access_info *acc,
724 __isl_take isl_set *set_C, int j, int level)
726 isl_map *read_map;
727 isl_map *write_map;
728 isl_map *dep_map;
729 isl_map *after;
731 read_map = isl_map_copy(acc->sink.map);
732 read_map = isl_map_intersect_domain(read_map, set_C);
733 write_map = isl_map_copy(acc->source[acc->n_must + j].map);
734 write_map = isl_map_reverse(write_map);
735 dep_map = isl_map_apply_range(read_map, write_map);
736 after = after_at_level(isl_map_get_space(dep_map), level);
737 dep_map = isl_map_intersect(dep_map, after);
739 return isl_map_reverse(dep_map);
742 /* For a given mapping between iterations of must source k and iterations
743 * of the sink, compute the all iteration of may source j preceding
744 * the sink at level before_level for any of the sink iterations,
745 * but following the corresponding iteration of must source k at level
746 * after_level.
748 static __isl_give isl_map *all_later_sources(__isl_keep isl_access_info *acc,
749 __isl_take isl_map *old_map,
750 int j, int before_level, int k, int after_level)
752 isl_space *dim;
753 isl_set *set_C;
754 isl_map *read_map;
755 isl_map *write_map;
756 isl_map *dep_map;
757 isl_map *after_write;
758 isl_map *before_read;
760 set_C = isl_map_range(isl_map_copy(old_map));
761 read_map = isl_map_copy(acc->sink.map);
762 read_map = isl_map_intersect_domain(read_map, set_C);
763 write_map = isl_map_copy(acc->source[acc->n_must + j].map);
765 write_map = isl_map_reverse(write_map);
766 dep_map = isl_map_apply_range(read_map, write_map);
767 dim = isl_space_join(isl_map_get_space(acc->source[acc->n_must + j].map),
768 isl_space_reverse(isl_map_get_space(acc->source[k].map)));
769 after_write = after_at_level(dim, after_level);
770 after_write = isl_map_apply_range(after_write, old_map);
771 after_write = isl_map_reverse(after_write);
772 dep_map = isl_map_intersect(dep_map, after_write);
773 before_read = after_at_level(isl_map_get_space(dep_map), before_level);
774 dep_map = isl_map_intersect(dep_map, before_read);
775 return isl_map_reverse(dep_map);
778 /* Given the must and may dependence relations for the must accesses
779 * for level sink_level, check if there are any accesses of may access j
780 * that occur in between and return their union.
781 * If some of these accesses are intermediate with respect to
782 * (previously thought to be) must dependences, then these
783 * must dependences are turned into may dependences.
785 static __isl_give isl_map *all_intermediate_sources(
786 __isl_keep isl_access_info *acc, __isl_take isl_map *map,
787 struct isl_map **must_rel, struct isl_map **may_rel,
788 int j, int sink_level)
790 int k, level;
791 int depth = 2 * isl_map_dim(acc->source[acc->n_must + j].map,
792 isl_dim_in) + 1;
794 for (k = 0; k < acc->n_must; ++k) {
795 int plevel;
797 if (isl_map_plain_is_empty(may_rel[k]) &&
798 isl_map_plain_is_empty(must_rel[k]))
799 continue;
801 plevel = acc->level_before(acc->source[k].data,
802 acc->source[acc->n_must + j].data);
804 for (level = sink_level; level <= depth; ++level) {
805 isl_map *T;
806 isl_map *copy;
807 isl_set *ran;
809 if (!can_precede_at_level(plevel, level))
810 continue;
812 copy = isl_map_copy(may_rel[k]);
813 T = all_later_sources(acc, copy, j, sink_level, k, level);
814 map = isl_map_union(map, T);
816 copy = isl_map_copy(must_rel[k]);
817 T = all_later_sources(acc, copy, j, sink_level, k, level);
818 ran = isl_map_range(isl_map_copy(T));
819 map = isl_map_union(map, T);
820 may_rel[k] = isl_map_union_disjoint(may_rel[k],
821 isl_map_intersect_range(isl_map_copy(must_rel[k]),
822 isl_set_copy(ran)));
823 T = isl_map_from_domain_and_range(
824 isl_set_universe(
825 isl_space_domain(isl_map_get_space(must_rel[k]))),
826 ran);
827 must_rel[k] = isl_map_subtract(must_rel[k], T);
831 return map;
834 /* Compute dependences for the case where all accesses are "may"
835 * accesses, which boils down to computing memory based dependences.
836 * The generic algorithm would also work in this case, but it would
837 * be overkill to use it.
839 static __isl_give isl_flow *compute_mem_based_dependences(
840 __isl_keep isl_access_info *acc)
842 int i;
843 isl_set *mustdo;
844 isl_set *maydo;
845 isl_flow *res;
847 res = isl_flow_alloc(acc);
848 if (!res)
849 return NULL;
851 mustdo = isl_map_domain(isl_map_copy(acc->sink.map));
852 maydo = isl_set_copy(mustdo);
854 for (i = 0; i < acc->n_may; ++i) {
855 int plevel;
856 int is_before;
857 isl_space *dim;
858 isl_map *before;
859 isl_map *dep;
861 plevel = acc->level_before(acc->source[i].data, acc->sink.data);
862 is_before = plevel & 1;
863 plevel >>= 1;
865 dim = isl_map_get_space(res->dep[i].map);
866 if (is_before)
867 before = isl_map_lex_le_first(dim, plevel);
868 else
869 before = isl_map_lex_lt_first(dim, plevel);
870 dep = isl_map_apply_range(isl_map_copy(acc->source[i].map),
871 isl_map_reverse(isl_map_copy(acc->sink.map)));
872 dep = isl_map_intersect(dep, before);
873 mustdo = isl_set_subtract(mustdo,
874 isl_map_range(isl_map_copy(dep)));
875 res->dep[i].map = isl_map_union(res->dep[i].map, dep);
878 res->may_no_source = isl_set_subtract(maydo, isl_set_copy(mustdo));
879 res->must_no_source = mustdo;
881 return res;
884 /* Compute dependences for the case where there is at least one
885 * "must" access.
887 * The core algorithm considers all levels in which a source may precede
888 * the sink, where a level may either be a statement level or a loop level.
889 * The outermost statement level is 1, the first loop level is 2, etc...
890 * The algorithm basically does the following:
891 * for all levels l of the read access from innermost to outermost
892 * for all sources w that may precede the sink access at that level
893 * compute the last iteration of the source that precedes the sink access
894 * at that level
895 * add result to possible last accesses at level l of source w
896 * for all sources w2 that we haven't considered yet at this level that may
897 * also precede the sink access
898 * for all levels l2 of w from l to innermost
899 * for all possible last accesses dep of w at l
900 * compute last iteration of w2 between the source and sink
901 * of dep
902 * add result to possible last accesses at level l of write w2
903 * and replace possible last accesses dep by the remainder
906 * The above algorithm is applied to the must access. During the course
907 * of the algorithm, we keep track of sink iterations that still
908 * need to be considered. These iterations are split into those that
909 * haven't been matched to any source access (mustdo) and those that have only
910 * been matched to may accesses (maydo).
911 * At the end of each level, we also consider the may accesses.
912 * In particular, we consider may accesses that precede the remaining
913 * sink iterations, moving elements from mustdo to maydo when appropriate,
914 * and may accesses that occur between a must source and a sink of any
915 * dependences found at the current level, turning must dependences into
916 * may dependences when appropriate.
919 static __isl_give isl_flow *compute_val_based_dependences(
920 __isl_keep isl_access_info *acc)
922 isl_ctx *ctx;
923 isl_flow *res;
924 isl_set *mustdo = NULL;
925 isl_set *maydo = NULL;
926 int level, j;
927 int depth;
928 isl_map **must_rel = NULL;
929 isl_map **may_rel = NULL;
931 if (!acc)
932 return NULL;
934 res = isl_flow_alloc(acc);
935 if (!res)
936 goto error;
937 ctx = isl_map_get_ctx(acc->sink.map);
939 depth = 2 * isl_map_dim(acc->sink.map, isl_dim_in) + 1;
940 mustdo = isl_map_domain(isl_map_copy(acc->sink.map));
941 maydo = isl_set_empty(isl_set_get_space(mustdo));
942 if (!mustdo || !maydo)
943 goto error;
944 if (isl_set_plain_is_empty(mustdo))
945 goto done;
947 must_rel = isl_alloc_array(ctx, struct isl_map *, acc->n_must);
948 may_rel = isl_alloc_array(ctx, struct isl_map *, acc->n_must);
949 if (!must_rel || !may_rel)
950 goto error;
952 for (level = depth; level >= 1; --level) {
953 for (j = acc->n_must-1; j >=0; --j) {
954 isl_space *space;
955 space = isl_map_get_space(res->dep[2 * j].map);
956 must_rel[j] = isl_map_empty(space);
957 may_rel[j] = isl_map_copy(must_rel[j]);
960 for (j = acc->n_must - 1; j >= 0; --j) {
961 struct isl_map *T;
962 struct isl_set *rest;
963 int plevel;
965 plevel = acc->level_before(acc->source[j].data,
966 acc->sink.data);
967 if (!can_precede_at_level(plevel, level))
968 continue;
970 T = last_source(acc, mustdo, j, level, &rest);
971 must_rel[j] = isl_map_union_disjoint(must_rel[j], T);
972 mustdo = rest;
974 intermediate_sources(acc, must_rel, j, level);
976 T = last_source(acc, maydo, j, level, &rest);
977 may_rel[j] = isl_map_union_disjoint(may_rel[j], T);
978 maydo = rest;
980 intermediate_sources(acc, may_rel, j, level);
982 if (isl_set_plain_is_empty(mustdo) &&
983 isl_set_plain_is_empty(maydo))
984 break;
986 for (j = j - 1; j >= 0; --j) {
987 int plevel;
989 plevel = acc->level_before(acc->source[j].data,
990 acc->sink.data);
991 if (!can_precede_at_level(plevel, level))
992 continue;
994 intermediate_sources(acc, must_rel, j, level);
995 intermediate_sources(acc, may_rel, j, level);
998 for (j = 0; j < acc->n_may; ++j) {
999 int plevel;
1000 isl_map *T;
1001 isl_set *ran;
1003 plevel = acc->level_before(acc->source[acc->n_must + j].data,
1004 acc->sink.data);
1005 if (!can_precede_at_level(plevel, level))
1006 continue;
1008 T = all_sources(acc, isl_set_copy(maydo), j, level);
1009 res->dep[2 * acc->n_must + j].map =
1010 isl_map_union(res->dep[2 * acc->n_must + j].map, T);
1011 T = all_sources(acc, isl_set_copy(mustdo), j, level);
1012 ran = isl_map_range(isl_map_copy(T));
1013 res->dep[2 * acc->n_must + j].map =
1014 isl_map_union(res->dep[2 * acc->n_must + j].map, T);
1015 mustdo = isl_set_subtract(mustdo, isl_set_copy(ran));
1016 maydo = isl_set_union_disjoint(maydo, ran);
1018 T = res->dep[2 * acc->n_must + j].map;
1019 T = all_intermediate_sources(acc, T, must_rel, may_rel,
1020 j, level);
1021 res->dep[2 * acc->n_must + j].map = T;
1024 for (j = acc->n_must - 1; j >= 0; --j) {
1025 res->dep[2 * j].map =
1026 isl_map_union_disjoint(res->dep[2 * j].map,
1027 must_rel[j]);
1028 res->dep[2 * j + 1].map =
1029 isl_map_union_disjoint(res->dep[2 * j + 1].map,
1030 may_rel[j]);
1033 if (isl_set_plain_is_empty(mustdo) &&
1034 isl_set_plain_is_empty(maydo))
1035 break;
1038 free(must_rel);
1039 free(may_rel);
1040 done:
1041 res->must_no_source = mustdo;
1042 res->may_no_source = maydo;
1043 return res;
1044 error:
1045 isl_flow_free(res);
1046 isl_set_free(mustdo);
1047 isl_set_free(maydo);
1048 free(must_rel);
1049 free(may_rel);
1050 return NULL;
1053 /* Given a "sink" access, a list of n "source" accesses,
1054 * compute for each iteration of the sink access
1055 * and for each element accessed by that iteration,
1056 * the source access in the list that last accessed the
1057 * element accessed by the sink access before this sink access.
1058 * Each access is given as a map from the loop iterators
1059 * to the array indices.
1060 * The result is a list of n relations between source and sink
1061 * iterations and a subset of the domain of the sink access,
1062 * corresponding to those iterations that access an element
1063 * not previously accessed.
1065 * To deal with multi-valued sink access relations, the sink iteration
1066 * domain is first extended with dimensions that correspond to the data
1067 * space. However, these extra dimensions are not projected out again.
1068 * It is up to the caller to decide whether these dimensions should be kept.
1070 static __isl_give isl_flow *access_info_compute_flow_core(
1071 __isl_take isl_access_info *acc)
1073 struct isl_flow *res = NULL;
1075 if (!acc)
1076 return NULL;
1078 acc->sink.map = isl_map_range_map(acc->sink.map);
1079 if (!acc->sink.map)
1080 goto error;
1082 if (acc->n_must == 0)
1083 res = compute_mem_based_dependences(acc);
1084 else {
1085 acc = isl_access_info_sort_sources(acc);
1086 res = compute_val_based_dependences(acc);
1088 acc = isl_access_info_free(acc);
1089 if (!res)
1090 return NULL;
1091 if (!res->must_no_source || !res->may_no_source)
1092 goto error;
1093 return res;
1094 error:
1095 isl_access_info_free(acc);
1096 isl_flow_free(res);
1097 return NULL;
1100 /* Given a "sink" access, a list of n "source" accesses,
1101 * compute for each iteration of the sink access
1102 * and for each element accessed by that iteration,
1103 * the source access in the list that last accessed the
1104 * element accessed by the sink access before this sink access.
1105 * Each access is given as a map from the loop iterators
1106 * to the array indices.
1107 * The result is a list of n relations between source and sink
1108 * iterations and a subset of the domain of the sink access,
1109 * corresponding to those iterations that access an element
1110 * not previously accessed.
1112 * To deal with multi-valued sink access relations,
1113 * access_info_compute_flow_core extends the sink iteration domain
1114 * with dimensions that correspond to the data space. These extra dimensions
1115 * are projected out from the result of access_info_compute_flow_core.
1117 __isl_give isl_flow *isl_access_info_compute_flow(__isl_take isl_access_info *acc)
1119 int j;
1120 struct isl_flow *res;
1122 if (!acc)
1123 return NULL;
1125 acc->domain_map = isl_map_domain_map(isl_map_copy(acc->sink.map));
1126 res = access_info_compute_flow_core(acc);
1127 if (!res)
1128 return NULL;
1130 for (j = 0; j < res->n_source; ++j) {
1131 res->dep[j].map = isl_map_range_factor_domain(res->dep[j].map);
1132 if (!res->dep[j].map)
1133 goto error;
1136 return res;
1137 error:
1138 isl_flow_free(res);
1139 return NULL;
1143 /* Keep track of some information about a schedule for a given
1144 * access. In particular, keep track of which dimensions
1145 * have a constant value and of the actual constant values.
1147 struct isl_sched_info {
1148 int *is_cst;
1149 isl_vec *cst;
1152 static void sched_info_free(__isl_take struct isl_sched_info *info)
1154 if (!info)
1155 return;
1156 isl_vec_free(info->cst);
1157 free(info->is_cst);
1158 free(info);
1161 /* Extract information on the constant dimensions of the schedule
1162 * for a given access. The "map" is of the form
1164 * [S -> D] -> A
1166 * with S the schedule domain, D the iteration domain and A the data domain.
1168 static __isl_give struct isl_sched_info *sched_info_alloc(
1169 __isl_keep isl_map *map)
1171 isl_ctx *ctx;
1172 isl_space *dim;
1173 struct isl_sched_info *info;
1174 int i, n;
1176 if (!map)
1177 return NULL;
1179 dim = isl_space_unwrap(isl_space_domain(isl_map_get_space(map)));
1180 if (!dim)
1181 return NULL;
1182 n = isl_space_dim(dim, isl_dim_in);
1183 isl_space_free(dim);
1185 ctx = isl_map_get_ctx(map);
1186 info = isl_alloc_type(ctx, struct isl_sched_info);
1187 if (!info)
1188 return NULL;
1189 info->is_cst = isl_alloc_array(ctx, int, n);
1190 info->cst = isl_vec_alloc(ctx, n);
1191 if (n && (!info->is_cst || !info->cst))
1192 goto error;
1194 for (i = 0; i < n; ++i) {
1195 isl_val *v;
1197 v = isl_map_plain_get_val_if_fixed(map, isl_dim_in, i);
1198 if (!v)
1199 goto error;
1200 info->is_cst[i] = !isl_val_is_nan(v);
1201 if (info->is_cst[i])
1202 info->cst = isl_vec_set_element_val(info->cst, i, v);
1203 else
1204 isl_val_free(v);
1207 return info;
1208 error:
1209 sched_info_free(info);
1210 return NULL;
1213 /* This structure represents the input for a dependence analysis computation.
1215 * "sink" represents the sink accesses.
1216 * "must_source" represents the definite source accesses.
1217 * "may_source" represents the possible source accesses.
1219 * "schedule" or "schedule_map" represents the execution order.
1220 * Exactly one of these fields should be NULL. The other field
1221 * determines the execution order.
1223 * The domains of these four maps refer to the same iteration spaces(s).
1224 * The ranges of the first three maps also refer to the same data space(s).
1226 * After a call to isl_union_access_info_introduce_schedule,
1227 * the "schedule_map" field no longer contains useful information.
1229 struct isl_union_access_info {
1230 isl_union_map *sink;
1231 isl_union_map *must_source;
1232 isl_union_map *may_source;
1234 isl_schedule *schedule;
1235 isl_union_map *schedule_map;
1238 /* Free "access" and return NULL.
1240 __isl_null isl_union_access_info *isl_union_access_info_free(
1241 __isl_take isl_union_access_info *access)
1243 if (!access)
1244 return NULL;
1246 isl_union_map_free(access->sink);
1247 isl_union_map_free(access->must_source);
1248 isl_union_map_free(access->may_source);
1249 isl_schedule_free(access->schedule);
1250 isl_union_map_free(access->schedule_map);
1251 free(access);
1253 return NULL;
1256 /* Return the isl_ctx to which "access" belongs.
1258 isl_ctx *isl_union_access_info_get_ctx(__isl_keep isl_union_access_info *access)
1260 return access ? isl_union_map_get_ctx(access->sink) : NULL;
1263 /* Create a new isl_union_access_info with the given sink accesses and
1264 * and no source accesses or schedule information.
1266 * By default, we use the schedule field of the isl_union_access_info,
1267 * but this may be overridden by a call
1268 * to isl_union_access_info_set_schedule_map.
1270 __isl_give isl_union_access_info *isl_union_access_info_from_sink(
1271 __isl_take isl_union_map *sink)
1273 isl_ctx *ctx;
1274 isl_space *space;
1275 isl_union_map *empty;
1276 isl_union_access_info *access;
1278 if (!sink)
1279 return NULL;
1280 ctx = isl_union_map_get_ctx(sink);
1281 access = isl_alloc_type(ctx, isl_union_access_info);
1282 if (!access)
1283 goto error;
1285 space = isl_union_map_get_space(sink);
1286 empty = isl_union_map_empty(isl_space_copy(space));
1287 access->sink = sink;
1288 access->must_source = isl_union_map_copy(empty);
1289 access->may_source = empty;
1290 access->schedule = isl_schedule_empty(space);
1291 access->schedule_map = NULL;
1293 if (!access->sink || !access->must_source ||
1294 !access->may_source || !access->schedule)
1295 return isl_union_access_info_free(access);
1297 return access;
1298 error:
1299 isl_union_map_free(sink);
1300 return NULL;
1303 /* Replace the definite source accesses of "access" by "must_source".
1305 __isl_give isl_union_access_info *isl_union_access_info_set_must_source(
1306 __isl_take isl_union_access_info *access,
1307 __isl_take isl_union_map *must_source)
1309 if (!access || !must_source)
1310 goto error;
1312 isl_union_map_free(access->must_source);
1313 access->must_source = must_source;
1315 return access;
1316 error:
1317 isl_union_access_info_free(access);
1318 isl_union_map_free(must_source);
1319 return NULL;
1322 /* Replace the possible source accesses of "access" by "may_source".
1324 __isl_give isl_union_access_info *isl_union_access_info_set_may_source(
1325 __isl_take isl_union_access_info *access,
1326 __isl_take isl_union_map *may_source)
1328 if (!access || !may_source)
1329 goto error;
1331 isl_union_map_free(access->may_source);
1332 access->may_source = may_source;
1334 return access;
1335 error:
1336 isl_union_access_info_free(access);
1337 isl_union_map_free(may_source);
1338 return NULL;
1341 /* Replace the schedule of "access" by "schedule".
1342 * Also free the schedule_map in case it was set last.
1344 __isl_give isl_union_access_info *isl_union_access_info_set_schedule(
1345 __isl_take isl_union_access_info *access,
1346 __isl_take isl_schedule *schedule)
1348 if (!access || !schedule)
1349 goto error;
1351 access->schedule_map = isl_union_map_free(access->schedule_map);
1352 isl_schedule_free(access->schedule);
1353 access->schedule = schedule;
1355 return access;
1356 error:
1357 isl_union_access_info_free(access);
1358 isl_schedule_free(schedule);
1359 return NULL;
1362 /* Replace the schedule map of "access" by "schedule_map".
1363 * Also free the schedule in case it was set last.
1365 __isl_give isl_union_access_info *isl_union_access_info_set_schedule_map(
1366 __isl_take isl_union_access_info *access,
1367 __isl_take isl_union_map *schedule_map)
1369 if (!access || !schedule_map)
1370 goto error;
1372 isl_union_map_free(access->schedule_map);
1373 access->schedule = isl_schedule_free(access->schedule);
1374 access->schedule_map = schedule_map;
1376 return access;
1377 error:
1378 isl_union_access_info_free(access);
1379 isl_union_map_free(schedule_map);
1380 return NULL;
1383 __isl_give isl_union_access_info *isl_union_access_info_copy(
1384 __isl_keep isl_union_access_info *access)
1386 isl_union_access_info *copy;
1388 if (!access)
1389 return NULL;
1390 copy = isl_union_access_info_from_sink(
1391 isl_union_map_copy(access->sink));
1392 copy = isl_union_access_info_set_must_source(copy,
1393 isl_union_map_copy(access->must_source));
1394 copy = isl_union_access_info_set_may_source(copy,
1395 isl_union_map_copy(access->may_source));
1396 if (access->schedule)
1397 copy = isl_union_access_info_set_schedule(copy,
1398 isl_schedule_copy(access->schedule));
1399 else
1400 copy = isl_union_access_info_set_schedule_map(copy,
1401 isl_union_map_copy(access->schedule_map));
1403 return copy;
1406 /* Print a key-value pair of a YAML mapping to "p",
1407 * with key "name" and value "umap".
1409 static __isl_give isl_printer *print_union_map_field(__isl_take isl_printer *p,
1410 const char *name, __isl_keep isl_union_map *umap)
1412 p = isl_printer_print_str(p, name);
1413 p = isl_printer_yaml_next(p);
1414 p = isl_printer_print_str(p, "\"");
1415 p = isl_printer_print_union_map(p, umap);
1416 p = isl_printer_print_str(p, "\"");
1417 p = isl_printer_yaml_next(p);
1419 return p;
1422 /* Print the information contained in "access" to "p".
1423 * The information is printed as a YAML document.
1425 __isl_give isl_printer *isl_printer_print_union_access_info(
1426 __isl_take isl_printer *p, __isl_keep isl_union_access_info *access)
1428 if (!access)
1429 return isl_printer_free(p);
1431 p = isl_printer_yaml_start_mapping(p);
1432 p = print_union_map_field(p, "sink", access->sink);
1433 p = print_union_map_field(p, "must_source", access->must_source);
1434 p = print_union_map_field(p, "may_source", access->may_source);
1435 if (access->schedule) {
1436 p = isl_printer_print_str(p, "schedule");
1437 p = isl_printer_yaml_next(p);
1438 p = isl_printer_print_schedule(p, access->schedule);
1439 p = isl_printer_yaml_next(p);
1440 } else {
1441 p = print_union_map_field(p, "schedule_map",
1442 access->schedule_map);
1444 p = isl_printer_yaml_end_mapping(p);
1446 return p;
1449 /* Return a string representation of the information in "access".
1450 * The information is printed in flow format.
1452 __isl_give char *isl_union_access_info_to_str(
1453 __isl_keep isl_union_access_info *access)
1455 isl_printer *p;
1456 char *s;
1458 if (!access)
1459 return NULL;
1461 p = isl_printer_to_str(isl_union_access_info_get_ctx(access));
1462 p = isl_printer_set_yaml_style(p, ISL_YAML_STYLE_FLOW);
1463 p = isl_printer_print_union_access_info(p, access);
1464 s = isl_printer_get_str(p);
1465 isl_printer_free(p);
1467 return s;
1470 /* Update the fields of "access" such that they all have the same parameters,
1471 * keeping in mind that the schedule_map field may be NULL and ignoring
1472 * the schedule field.
1474 static __isl_give isl_union_access_info *isl_union_access_info_align_params(
1475 __isl_take isl_union_access_info *access)
1477 isl_space *space;
1479 if (!access)
1480 return NULL;
1482 space = isl_union_map_get_space(access->sink);
1483 space = isl_space_align_params(space,
1484 isl_union_map_get_space(access->must_source));
1485 space = isl_space_align_params(space,
1486 isl_union_map_get_space(access->may_source));
1487 if (access->schedule_map)
1488 space = isl_space_align_params(space,
1489 isl_union_map_get_space(access->schedule_map));
1490 access->sink = isl_union_map_align_params(access->sink,
1491 isl_space_copy(space));
1492 access->must_source = isl_union_map_align_params(access->must_source,
1493 isl_space_copy(space));
1494 access->may_source = isl_union_map_align_params(access->may_source,
1495 isl_space_copy(space));
1496 if (!access->schedule_map) {
1497 isl_space_free(space);
1498 } else {
1499 access->schedule_map =
1500 isl_union_map_align_params(access->schedule_map, space);
1501 if (!access->schedule_map)
1502 return isl_union_access_info_free(access);
1505 if (!access->sink || !access->must_source || !access->may_source)
1506 return isl_union_access_info_free(access);
1508 return access;
1511 /* Prepend the schedule dimensions to the iteration domains.
1513 * That is, if the schedule is of the form
1515 * D -> S
1517 * while the access relations are of the form
1519 * D -> A
1521 * then the updated access relations are of the form
1523 * [S -> D] -> A
1525 * The schedule map is also replaced by the map
1527 * [S -> D] -> D
1529 * that is used during the internal computation.
1530 * Neither the original schedule map nor this updated schedule map
1531 * are used after the call to this function.
1533 static __isl_give isl_union_access_info *
1534 isl_union_access_info_introduce_schedule(
1535 __isl_take isl_union_access_info *access)
1537 isl_union_map *sm;
1539 if (!access)
1540 return NULL;
1542 sm = isl_union_map_reverse(access->schedule_map);
1543 sm = isl_union_map_range_map(sm);
1544 access->sink = isl_union_map_apply_range(isl_union_map_copy(sm),
1545 access->sink);
1546 access->may_source = isl_union_map_apply_range(isl_union_map_copy(sm),
1547 access->may_source);
1548 access->must_source = isl_union_map_apply_range(isl_union_map_copy(sm),
1549 access->must_source);
1550 access->schedule_map = sm;
1552 if (!access->sink || !access->must_source ||
1553 !access->may_source || !access->schedule_map)
1554 return isl_union_access_info_free(access);
1556 return access;
1559 /* This structure represents the result of a dependence analysis computation.
1561 * "must_dep" represents the full definite dependences
1562 * "may_dep" represents the full non-definite dependences.
1563 * Both are of the form
1565 * [Source] -> [[Sink -> Data]]
1567 * (after the schedule dimensions have been projected out).
1568 * "must_no_source" represents the subset of the sink accesses for which
1569 * definitely no source was found.
1570 * "may_no_source" represents the subset of the sink accesses for which
1571 * possibly, but not definitely, no source was found.
1573 struct isl_union_flow {
1574 isl_union_map *must_dep;
1575 isl_union_map *may_dep;
1576 isl_union_map *must_no_source;
1577 isl_union_map *may_no_source;
1580 /* Return the isl_ctx to which "flow" belongs.
1582 isl_ctx *isl_union_flow_get_ctx(__isl_keep isl_union_flow *flow)
1584 return flow ? isl_union_map_get_ctx(flow->must_dep) : NULL;
1587 /* Free "flow" and return NULL.
1589 __isl_null isl_union_flow *isl_union_flow_free(__isl_take isl_union_flow *flow)
1591 if (!flow)
1592 return NULL;
1593 isl_union_map_free(flow->must_dep);
1594 isl_union_map_free(flow->may_dep);
1595 isl_union_map_free(flow->must_no_source);
1596 isl_union_map_free(flow->may_no_source);
1597 free(flow);
1598 return NULL;
1601 void isl_union_flow_dump(__isl_keep isl_union_flow *flow)
1603 if (!flow)
1604 return;
1606 fprintf(stderr, "must dependences: ");
1607 isl_union_map_dump(flow->must_dep);
1608 fprintf(stderr, "may dependences: ");
1609 isl_union_map_dump(flow->may_dep);
1610 fprintf(stderr, "must no source: ");
1611 isl_union_map_dump(flow->must_no_source);
1612 fprintf(stderr, "may no source: ");
1613 isl_union_map_dump(flow->may_no_source);
1616 /* Return the full definite dependences in "flow", with accessed elements.
1618 __isl_give isl_union_map *isl_union_flow_get_full_must_dependence(
1619 __isl_keep isl_union_flow *flow)
1621 if (!flow)
1622 return NULL;
1623 return isl_union_map_copy(flow->must_dep);
1626 /* Return the full possible dependences in "flow", including the definite
1627 * dependences, with accessed elements.
1629 __isl_give isl_union_map *isl_union_flow_get_full_may_dependence(
1630 __isl_keep isl_union_flow *flow)
1632 if (!flow)
1633 return NULL;
1634 return isl_union_map_union(isl_union_map_copy(flow->must_dep),
1635 isl_union_map_copy(flow->may_dep));
1638 /* Return the definite dependences in "flow", without the accessed elements.
1640 __isl_give isl_union_map *isl_union_flow_get_must_dependence(
1641 __isl_keep isl_union_flow *flow)
1643 isl_union_map *dep;
1645 if (!flow)
1646 return NULL;
1647 dep = isl_union_map_copy(flow->must_dep);
1648 return isl_union_map_range_factor_domain(dep);
1651 /* Return the possible dependences in "flow", including the definite
1652 * dependences, without the accessed elements.
1654 __isl_give isl_union_map *isl_union_flow_get_may_dependence(
1655 __isl_keep isl_union_flow *flow)
1657 isl_union_map *dep;
1659 if (!flow)
1660 return NULL;
1661 dep = isl_union_map_union(isl_union_map_copy(flow->must_dep),
1662 isl_union_map_copy(flow->may_dep));
1663 return isl_union_map_range_factor_domain(dep);
1666 /* Return the non-definite dependences in "flow".
1668 static __isl_give isl_union_map *isl_union_flow_get_non_must_dependence(
1669 __isl_keep isl_union_flow *flow)
1671 if (!flow)
1672 return NULL;
1673 return isl_union_map_copy(flow->may_dep);
1676 /* Return the subset of the sink accesses for which definitely
1677 * no source was found.
1679 __isl_give isl_union_map *isl_union_flow_get_must_no_source(
1680 __isl_keep isl_union_flow *flow)
1682 if (!flow)
1683 return NULL;
1684 return isl_union_map_copy(flow->must_no_source);
1687 /* Return the subset of the sink accesses for which possibly
1688 * no source was found, including those for which definitely
1689 * no source was found.
1691 __isl_give isl_union_map *isl_union_flow_get_may_no_source(
1692 __isl_keep isl_union_flow *flow)
1694 if (!flow)
1695 return NULL;
1696 return isl_union_map_union(isl_union_map_copy(flow->must_no_source),
1697 isl_union_map_copy(flow->may_no_source));
1700 /* Return the subset of the sink accesses for which possibly, but not
1701 * definitely, no source was found.
1703 static __isl_give isl_union_map *isl_union_flow_get_non_must_no_source(
1704 __isl_keep isl_union_flow *flow)
1706 if (!flow)
1707 return NULL;
1708 return isl_union_map_copy(flow->may_no_source);
1711 /* Create a new isl_union_flow object, initialized with empty
1712 * dependence relations and sink subsets.
1714 static __isl_give isl_union_flow *isl_union_flow_alloc(
1715 __isl_take isl_space *space)
1717 isl_ctx *ctx;
1718 isl_union_map *empty;
1719 isl_union_flow *flow;
1721 if (!space)
1722 return NULL;
1723 ctx = isl_space_get_ctx(space);
1724 flow = isl_alloc_type(ctx, isl_union_flow);
1725 if (!flow)
1726 goto error;
1728 empty = isl_union_map_empty(space);
1729 flow->must_dep = isl_union_map_copy(empty);
1730 flow->may_dep = isl_union_map_copy(empty);
1731 flow->must_no_source = isl_union_map_copy(empty);
1732 flow->may_no_source = empty;
1734 if (!flow->must_dep || !flow->may_dep ||
1735 !flow->must_no_source || !flow->may_no_source)
1736 return isl_union_flow_free(flow);
1738 return flow;
1739 error:
1740 isl_space_free(space);
1741 return NULL;
1744 /* Copy this isl_union_flow object.
1746 __isl_give isl_union_flow *isl_union_flow_copy(__isl_keep isl_union_flow *flow)
1748 isl_union_flow *copy;
1750 if (!flow)
1751 return NULL;
1753 copy = isl_union_flow_alloc(isl_union_map_get_space(flow->must_dep));
1755 if (!copy)
1756 return NULL;
1758 copy->must_dep = isl_union_map_union(copy->must_dep,
1759 isl_union_map_copy(flow->must_dep));
1760 copy->may_dep = isl_union_map_union(copy->may_dep,
1761 isl_union_map_copy(flow->may_dep));
1762 copy->must_no_source = isl_union_map_union(copy->must_no_source,
1763 isl_union_map_copy(flow->must_no_source));
1764 copy->may_no_source = isl_union_map_union(copy->may_no_source,
1765 isl_union_map_copy(flow->may_no_source));
1767 if (!copy->must_dep || !copy->may_dep ||
1768 !copy->must_no_source || !copy->may_no_source)
1769 return isl_union_flow_free(copy);
1771 return copy;
1774 /* Drop the schedule dimensions from the iteration domains in "flow".
1775 * In particular, the schedule dimensions have been prepended
1776 * to the iteration domains prior to the dependence analysis by
1777 * replacing the iteration domain D, by the wrapped map [S -> D].
1778 * Replace these wrapped maps by the original D.
1780 * In particular, the dependences computed by access_info_compute_flow_core
1781 * are of the form
1783 * [S -> D] -> [[S' -> D'] -> A]
1785 * The schedule dimensions are projected out by first currying the range,
1786 * resulting in
1788 * [S -> D] -> [S' -> [D' -> A]]
1790 * and then computing the factor range
1792 * D -> [D' -> A]
1794 static __isl_give isl_union_flow *isl_union_flow_drop_schedule(
1795 __isl_take isl_union_flow *flow)
1797 if (!flow)
1798 return NULL;
1800 flow->must_dep = isl_union_map_range_curry(flow->must_dep);
1801 flow->must_dep = isl_union_map_factor_range(flow->must_dep);
1802 flow->may_dep = isl_union_map_range_curry(flow->may_dep);
1803 flow->may_dep = isl_union_map_factor_range(flow->may_dep);
1804 flow->must_no_source =
1805 isl_union_map_domain_factor_range(flow->must_no_source);
1806 flow->may_no_source =
1807 isl_union_map_domain_factor_range(flow->may_no_source);
1809 if (!flow->must_dep || !flow->may_dep ||
1810 !flow->must_no_source || !flow->may_no_source)
1811 return isl_union_flow_free(flow);
1813 return flow;
1816 struct isl_compute_flow_data {
1817 isl_union_map *must_source;
1818 isl_union_map *may_source;
1819 isl_union_flow *flow;
1821 int count;
1822 int must;
1823 isl_space *dim;
1824 struct isl_sched_info *sink_info;
1825 struct isl_sched_info **source_info;
1826 isl_access_info *accesses;
1829 static isl_stat count_matching_array(__isl_take isl_map *map, void *user)
1831 int eq;
1832 isl_space *dim;
1833 struct isl_compute_flow_data *data;
1835 data = (struct isl_compute_flow_data *)user;
1837 dim = isl_space_range(isl_map_get_space(map));
1839 eq = isl_space_is_equal(dim, data->dim);
1841 isl_space_free(dim);
1842 isl_map_free(map);
1844 if (eq < 0)
1845 return isl_stat_error;
1846 if (eq)
1847 data->count++;
1849 return isl_stat_ok;
1852 static isl_stat collect_matching_array(__isl_take isl_map *map, void *user)
1854 int eq;
1855 isl_space *dim;
1856 struct isl_sched_info *info;
1857 struct isl_compute_flow_data *data;
1859 data = (struct isl_compute_flow_data *)user;
1861 dim = isl_space_range(isl_map_get_space(map));
1863 eq = isl_space_is_equal(dim, data->dim);
1865 isl_space_free(dim);
1867 if (eq < 0)
1868 goto error;
1869 if (!eq) {
1870 isl_map_free(map);
1871 return isl_stat_ok;
1874 info = sched_info_alloc(map);
1875 data->source_info[data->count] = info;
1877 data->accesses = isl_access_info_add_source(data->accesses,
1878 map, data->must, info);
1880 data->count++;
1882 return isl_stat_ok;
1883 error:
1884 isl_map_free(map);
1885 return isl_stat_error;
1888 /* Determine the shared nesting level and the "textual order" of
1889 * the given accesses.
1891 * We first determine the minimal schedule dimension for both accesses.
1893 * If among those dimensions, we can find one where both have a fixed
1894 * value and if moreover those values are different, then the previous
1895 * dimension is the last shared nesting level and the textual order
1896 * is determined based on the order of the fixed values.
1897 * If no such fixed values can be found, then we set the shared
1898 * nesting level to the minimal schedule dimension, with no textual ordering.
1900 static int before(void *first, void *second)
1902 struct isl_sched_info *info1 = first;
1903 struct isl_sched_info *info2 = second;
1904 int n1, n2;
1905 int i;
1907 n1 = isl_vec_size(info1->cst);
1908 n2 = isl_vec_size(info2->cst);
1910 if (n2 < n1)
1911 n1 = n2;
1913 for (i = 0; i < n1; ++i) {
1914 int r;
1915 int cmp;
1917 if (!info1->is_cst[i])
1918 continue;
1919 if (!info2->is_cst[i])
1920 continue;
1921 cmp = isl_vec_cmp_element(info1->cst, info2->cst, i);
1922 if (cmp == 0)
1923 continue;
1925 r = 2 * i + (cmp < 0);
1927 return r;
1930 return 2 * n1;
1933 /* Given a sink access, look for all the source accesses that access
1934 * the same array and perform dataflow analysis on them using
1935 * isl_access_info_compute_flow_core.
1937 static isl_stat compute_flow(__isl_take isl_map *map, void *user)
1939 int i;
1940 isl_ctx *ctx;
1941 struct isl_compute_flow_data *data;
1942 isl_flow *flow;
1943 isl_union_flow *df;
1945 data = (struct isl_compute_flow_data *)user;
1946 df = data->flow;
1948 ctx = isl_map_get_ctx(map);
1950 data->accesses = NULL;
1951 data->sink_info = NULL;
1952 data->source_info = NULL;
1953 data->count = 0;
1954 data->dim = isl_space_range(isl_map_get_space(map));
1956 if (isl_union_map_foreach_map(data->must_source,
1957 &count_matching_array, data) < 0)
1958 goto error;
1959 if (isl_union_map_foreach_map(data->may_source,
1960 &count_matching_array, data) < 0)
1961 goto error;
1963 data->sink_info = sched_info_alloc(map);
1964 data->source_info = isl_calloc_array(ctx, struct isl_sched_info *,
1965 data->count);
1967 data->accesses = isl_access_info_alloc(isl_map_copy(map),
1968 data->sink_info, &before, data->count);
1969 if (!data->sink_info || (data->count && !data->source_info) ||
1970 !data->accesses)
1971 goto error;
1972 data->count = 0;
1973 data->must = 1;
1974 if (isl_union_map_foreach_map(data->must_source,
1975 &collect_matching_array, data) < 0)
1976 goto error;
1977 data->must = 0;
1978 if (isl_union_map_foreach_map(data->may_source,
1979 &collect_matching_array, data) < 0)
1980 goto error;
1982 flow = access_info_compute_flow_core(data->accesses);
1983 data->accesses = NULL;
1985 if (!flow)
1986 goto error;
1988 df->must_no_source = isl_union_map_union(df->must_no_source,
1989 isl_union_map_from_map(isl_flow_get_no_source(flow, 1)));
1990 df->may_no_source = isl_union_map_union(df->may_no_source,
1991 isl_union_map_from_map(isl_flow_get_no_source(flow, 0)));
1993 for (i = 0; i < flow->n_source; ++i) {
1994 isl_union_map *dep;
1995 dep = isl_union_map_from_map(isl_map_copy(flow->dep[i].map));
1996 if (flow->dep[i].must)
1997 df->must_dep = isl_union_map_union(df->must_dep, dep);
1998 else
1999 df->may_dep = isl_union_map_union(df->may_dep, dep);
2002 isl_flow_free(flow);
2004 sched_info_free(data->sink_info);
2005 if (data->source_info) {
2006 for (i = 0; i < data->count; ++i)
2007 sched_info_free(data->source_info[i]);
2008 free(data->source_info);
2010 isl_space_free(data->dim);
2011 isl_map_free(map);
2013 return isl_stat_ok;
2014 error:
2015 isl_access_info_free(data->accesses);
2016 sched_info_free(data->sink_info);
2017 if (data->source_info) {
2018 for (i = 0; i < data->count; ++i)
2019 sched_info_free(data->source_info[i]);
2020 free(data->source_info);
2022 isl_space_free(data->dim);
2023 isl_map_free(map);
2025 return isl_stat_error;
2028 /* Remove the must accesses from the may accesses.
2030 * A must access always trumps a may access, so there is no need
2031 * for a must access to also be considered as a may access. Doing so
2032 * would only cost extra computations only to find out that
2033 * the duplicated may access does not make any difference.
2035 static __isl_give isl_union_access_info *isl_union_access_info_normalize(
2036 __isl_take isl_union_access_info *access)
2038 if (!access)
2039 return NULL;
2040 access->may_source = isl_union_map_subtract(access->may_source,
2041 isl_union_map_copy(access->must_source));
2042 if (!access->may_source)
2043 return isl_union_access_info_free(access);
2045 return access;
2048 /* Given a description of the "sink" accesses, the "source" accesses and
2049 * a schedule, compute for each instance of a sink access
2050 * and for each element accessed by that instance,
2051 * the possible or definite source accesses that last accessed the
2052 * element accessed by the sink access before this sink access
2053 * in the sense that there is no intermediate definite source access.
2055 * The must_no_source and may_no_source elements of the result
2056 * are subsets of access->sink. The elements must_dep and may_dep
2057 * map domain elements of access->{may,must)_source to
2058 * domain elements of access->sink.
2060 * This function is used when only the schedule map representation
2061 * is available.
2063 * We first prepend the schedule dimensions to the domain
2064 * of the accesses so that we can easily compare their relative order.
2065 * Then we consider each sink access individually in compute_flow.
2067 static __isl_give isl_union_flow *compute_flow_union_map(
2068 __isl_take isl_union_access_info *access)
2070 struct isl_compute_flow_data data;
2072 access = isl_union_access_info_align_params(access);
2073 access = isl_union_access_info_introduce_schedule(access);
2074 if (!access)
2075 return NULL;
2077 data.must_source = access->must_source;
2078 data.may_source = access->may_source;
2080 data.flow = isl_union_flow_alloc(isl_union_map_get_space(access->sink));
2082 if (isl_union_map_foreach_map(access->sink, &compute_flow, &data) < 0)
2083 goto error;
2085 data.flow = isl_union_flow_drop_schedule(data.flow);
2087 isl_union_access_info_free(access);
2088 return data.flow;
2089 error:
2090 isl_union_access_info_free(access);
2091 isl_union_flow_free(data.flow);
2092 return NULL;
2095 /* A schedule access relation.
2097 * The access relation "access" is of the form [S -> D] -> A,
2098 * where S corresponds to the prefix schedule at "node".
2099 * "must" is only relevant for source accesses and indicates
2100 * whether the access is a must source or a may source.
2102 struct isl_scheduled_access {
2103 isl_map *access;
2104 int must;
2105 isl_schedule_node *node;
2108 /* Data structure for keeping track of individual scheduled sink and source
2109 * accesses when computing dependence analysis based on a schedule tree.
2111 * "n_sink" is the number of used entries in "sink"
2112 * "n_source" is the number of used entries in "source"
2114 * "set_sink", "must" and "node" are only used inside collect_sink_source,
2115 * to keep track of the current node and
2116 * of what extract_sink_source needs to do.
2118 struct isl_compute_flow_schedule_data {
2119 isl_union_access_info *access;
2121 int n_sink;
2122 int n_source;
2124 struct isl_scheduled_access *sink;
2125 struct isl_scheduled_access *source;
2127 int set_sink;
2128 int must;
2129 isl_schedule_node *node;
2132 /* Align the parameters of all sinks with all sources.
2134 * If there are no sinks or no sources, then no alignment is needed.
2136 static void isl_compute_flow_schedule_data_align_params(
2137 struct isl_compute_flow_schedule_data *data)
2139 int i;
2140 isl_space *space;
2142 if (data->n_sink == 0 || data->n_source == 0)
2143 return;
2145 space = isl_map_get_space(data->sink[0].access);
2147 for (i = 1; i < data->n_sink; ++i)
2148 space = isl_space_align_params(space,
2149 isl_map_get_space(data->sink[i].access));
2150 for (i = 0; i < data->n_source; ++i)
2151 space = isl_space_align_params(space,
2152 isl_map_get_space(data->source[i].access));
2154 for (i = 0; i < data->n_sink; ++i)
2155 data->sink[i].access =
2156 isl_map_align_params(data->sink[i].access,
2157 isl_space_copy(space));
2158 for (i = 0; i < data->n_source; ++i)
2159 data->source[i].access =
2160 isl_map_align_params(data->source[i].access,
2161 isl_space_copy(space));
2163 isl_space_free(space);
2166 /* Free all the memory referenced from "data".
2167 * Do not free "data" itself as it may be allocated on the stack.
2169 static void isl_compute_flow_schedule_data_clear(
2170 struct isl_compute_flow_schedule_data *data)
2172 int i;
2174 if (!data->sink)
2175 return;
2177 for (i = 0; i < data->n_sink; ++i) {
2178 isl_map_free(data->sink[i].access);
2179 isl_schedule_node_free(data->sink[i].node);
2182 for (i = 0; i < data->n_source; ++i) {
2183 isl_map_free(data->source[i].access);
2184 isl_schedule_node_free(data->source[i].node);
2187 free(data->sink);
2190 /* isl_schedule_foreach_schedule_node_top_down callback for counting
2191 * (an upper bound on) the number of sinks and sources.
2193 * Sinks and sources are only extracted at leaves of the tree,
2194 * so we skip the node if it is not a leaf.
2195 * Otherwise we increment data->n_sink and data->n_source with
2196 * the number of spaces in the sink and source access domains
2197 * that reach this node.
2199 static isl_bool count_sink_source(__isl_keep isl_schedule_node *node,
2200 void *user)
2202 struct isl_compute_flow_schedule_data *data = user;
2203 isl_union_set *domain;
2204 isl_union_map *umap;
2205 isl_bool r = isl_bool_false;
2207 if (isl_schedule_node_get_type(node) != isl_schedule_node_leaf)
2208 return isl_bool_true;
2210 domain = isl_schedule_node_get_universe_domain(node);
2212 umap = isl_union_map_copy(data->access->sink);
2213 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
2214 data->n_sink += isl_union_map_n_map(umap);
2215 isl_union_map_free(umap);
2216 if (!umap)
2217 r = isl_bool_error;
2219 umap = isl_union_map_copy(data->access->must_source);
2220 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
2221 data->n_source += isl_union_map_n_map(umap);
2222 isl_union_map_free(umap);
2223 if (!umap)
2224 r = isl_bool_error;
2226 umap = isl_union_map_copy(data->access->may_source);
2227 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
2228 data->n_source += isl_union_map_n_map(umap);
2229 isl_union_map_free(umap);
2230 if (!umap)
2231 r = isl_bool_error;
2233 isl_union_set_free(domain);
2235 return r;
2238 /* Add a single scheduled sink or source (depending on data->set_sink)
2239 * with scheduled access relation "map", must property data->must and
2240 * schedule node data->node to the list of sinks or sources.
2242 static isl_stat extract_sink_source(__isl_take isl_map *map, void *user)
2244 struct isl_compute_flow_schedule_data *data = user;
2245 struct isl_scheduled_access *access;
2247 if (data->set_sink)
2248 access = data->sink + data->n_sink++;
2249 else
2250 access = data->source + data->n_source++;
2252 access->access = map;
2253 access->must = data->must;
2254 access->node = isl_schedule_node_copy(data->node);
2256 return isl_stat_ok;
2259 /* isl_schedule_foreach_schedule_node_top_down callback for collecting
2260 * individual scheduled source and sink accesses (taking into account
2261 * the domain of the schedule).
2263 * We only collect accesses at the leaves of the schedule tree.
2264 * We prepend the schedule dimensions at the leaf to the iteration
2265 * domains of the source and sink accesses and then extract
2266 * the individual accesses (per space).
2268 * In particular, if the prefix schedule at the node is of the form
2270 * D -> S
2272 * while the access relations are of the form
2274 * D -> A
2276 * then the updated access relations are of the form
2278 * [S -> D] -> A
2280 * Note that S consists of a single space such that introducing S
2281 * in the access relations does not increase the number of spaces.
2283 static isl_bool collect_sink_source(__isl_keep isl_schedule_node *node,
2284 void *user)
2286 struct isl_compute_flow_schedule_data *data = user;
2287 isl_union_map *prefix;
2288 isl_union_map *umap;
2289 isl_bool r = isl_bool_false;
2291 if (isl_schedule_node_get_type(node) != isl_schedule_node_leaf)
2292 return isl_bool_true;
2294 data->node = node;
2296 prefix = isl_schedule_node_get_prefix_schedule_relation(node);
2297 prefix = isl_union_map_reverse(prefix);
2298 prefix = isl_union_map_range_map(prefix);
2300 data->set_sink = 1;
2301 umap = isl_union_map_copy(data->access->sink);
2302 umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
2303 if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
2304 r = isl_bool_error;
2305 isl_union_map_free(umap);
2307 data->set_sink = 0;
2308 data->must = 1;
2309 umap = isl_union_map_copy(data->access->must_source);
2310 umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
2311 if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
2312 r = isl_bool_error;
2313 isl_union_map_free(umap);
2315 data->set_sink = 0;
2316 data->must = 0;
2317 umap = isl_union_map_copy(data->access->may_source);
2318 umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
2319 if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
2320 r = isl_bool_error;
2321 isl_union_map_free(umap);
2323 isl_union_map_free(prefix);
2325 return r;
2328 /* isl_access_info_compute_flow callback for determining whether
2329 * the shared nesting level and the ordering within that level
2330 * for two scheduled accesses for use in compute_single_flow.
2332 * The tokens passed to this function refer to the leaves
2333 * in the schedule tree where the accesses take place.
2335 * If n is the shared number of loops, then we need to return
2336 * "2 * n + 1" if "first" precedes "second" inside the innermost
2337 * shared loop and "2 * n" otherwise.
2339 * The innermost shared ancestor may be the leaves themselves
2340 * if the accesses take place in the same leaf. Otherwise,
2341 * it is either a set node or a sequence node. Only in the case
2342 * of a sequence node do we consider one access to precede the other.
2344 static int before_node(void *first, void *second)
2346 isl_schedule_node *node1 = first;
2347 isl_schedule_node *node2 = second;
2348 isl_schedule_node *shared;
2349 int depth;
2350 int before = 0;
2352 shared = isl_schedule_node_get_shared_ancestor(node1, node2);
2353 if (!shared)
2354 return -1;
2356 depth = isl_schedule_node_get_schedule_depth(shared);
2357 if (isl_schedule_node_get_type(shared) == isl_schedule_node_sequence) {
2358 int pos1, pos2;
2360 pos1 = isl_schedule_node_get_ancestor_child_position(node1,
2361 shared);
2362 pos2 = isl_schedule_node_get_ancestor_child_position(node2,
2363 shared);
2364 before = pos1 < pos2;
2367 isl_schedule_node_free(shared);
2369 return 2 * depth + before;
2372 /* Add the scheduled sources from "data" that access
2373 * the same data space as "sink" to "access".
2375 static __isl_give isl_access_info *add_matching_sources(
2376 __isl_take isl_access_info *access, struct isl_scheduled_access *sink,
2377 struct isl_compute_flow_schedule_data *data)
2379 int i;
2380 isl_space *space;
2382 space = isl_space_range(isl_map_get_space(sink->access));
2383 for (i = 0; i < data->n_source; ++i) {
2384 struct isl_scheduled_access *source;
2385 isl_space *source_space;
2386 int eq;
2388 source = &data->source[i];
2389 source_space = isl_map_get_space(source->access);
2390 source_space = isl_space_range(source_space);
2391 eq = isl_space_is_equal(space, source_space);
2392 isl_space_free(source_space);
2394 if (!eq)
2395 continue;
2396 if (eq < 0)
2397 goto error;
2399 access = isl_access_info_add_source(access,
2400 isl_map_copy(source->access), source->must, source->node);
2403 isl_space_free(space);
2404 return access;
2405 error:
2406 isl_space_free(space);
2407 isl_access_info_free(access);
2408 return NULL;
2411 /* Given a scheduled sink access relation "sink", compute the corresponding
2412 * dependences on the sources in "data" and add the computed dependences
2413 * to "uf".
2415 * The dependences computed by access_info_compute_flow_core are of the form
2417 * [S -> I] -> [[S' -> I'] -> A]
2419 * The schedule dimensions are projected out by first currying the range,
2420 * resulting in
2422 * [S -> I] -> [S' -> [I' -> A]]
2424 * and then computing the factor range
2426 * I -> [I' -> A]
2428 static __isl_give isl_union_flow *compute_single_flow(
2429 __isl_take isl_union_flow *uf, struct isl_scheduled_access *sink,
2430 struct isl_compute_flow_schedule_data *data)
2432 int i;
2433 isl_access_info *access;
2434 isl_flow *flow;
2435 isl_map *map;
2437 if (!uf)
2438 return NULL;
2440 access = isl_access_info_alloc(isl_map_copy(sink->access), sink->node,
2441 &before_node, data->n_source);
2442 access = add_matching_sources(access, sink, data);
2444 flow = access_info_compute_flow_core(access);
2445 if (!flow)
2446 return isl_union_flow_free(uf);
2448 map = isl_map_domain_factor_range(isl_flow_get_no_source(flow, 1));
2449 uf->must_no_source = isl_union_map_union(uf->must_no_source,
2450 isl_union_map_from_map(map));
2451 map = isl_map_domain_factor_range(isl_flow_get_no_source(flow, 0));
2452 uf->may_no_source = isl_union_map_union(uf->may_no_source,
2453 isl_union_map_from_map(map));
2455 for (i = 0; i < flow->n_source; ++i) {
2456 isl_union_map *dep;
2458 map = isl_map_range_curry(isl_map_copy(flow->dep[i].map));
2459 map = isl_map_factor_range(map);
2460 dep = isl_union_map_from_map(map);
2461 if (flow->dep[i].must)
2462 uf->must_dep = isl_union_map_union(uf->must_dep, dep);
2463 else
2464 uf->may_dep = isl_union_map_union(uf->may_dep, dep);
2467 isl_flow_free(flow);
2469 return uf;
2472 /* Given a description of the "sink" accesses, the "source" accesses and
2473 * a schedule, compute for each instance of a sink access
2474 * and for each element accessed by that instance,
2475 * the possible or definite source accesses that last accessed the
2476 * element accessed by the sink access before this sink access
2477 * in the sense that there is no intermediate definite source access.
2478 * Only consider dependences between statement instances that belong
2479 * to the domain of the schedule.
2481 * The must_no_source and may_no_source elements of the result
2482 * are subsets of access->sink. The elements must_dep and may_dep
2483 * map domain elements of access->{may,must)_source to
2484 * domain elements of access->sink.
2486 * This function is used when a schedule tree representation
2487 * is available.
2489 * We extract the individual scheduled source and sink access relations
2490 * (taking into account the domain of the schedule) and
2491 * then compute dependences for each scheduled sink individually.
2493 static __isl_give isl_union_flow *compute_flow_schedule(
2494 __isl_take isl_union_access_info *access)
2496 struct isl_compute_flow_schedule_data data = { access };
2497 int i, n;
2498 isl_ctx *ctx;
2499 isl_union_flow *flow;
2501 ctx = isl_union_access_info_get_ctx(access);
2503 data.n_sink = 0;
2504 data.n_source = 0;
2505 if (isl_schedule_foreach_schedule_node_top_down(access->schedule,
2506 &count_sink_source, &data) < 0)
2507 goto error;
2509 n = data.n_sink + data.n_source;
2510 data.sink = isl_calloc_array(ctx, struct isl_scheduled_access, n);
2511 if (n && !data.sink)
2512 goto error;
2513 data.source = data.sink + data.n_sink;
2515 data.n_sink = 0;
2516 data.n_source = 0;
2517 if (isl_schedule_foreach_schedule_node_top_down(access->schedule,
2518 &collect_sink_source, &data) < 0)
2519 goto error;
2521 flow = isl_union_flow_alloc(isl_union_map_get_space(access->sink));
2523 isl_compute_flow_schedule_data_align_params(&data);
2525 for (i = 0; i < data.n_sink; ++i)
2526 flow = compute_single_flow(flow, &data.sink[i], &data);
2528 isl_compute_flow_schedule_data_clear(&data);
2530 isl_union_access_info_free(access);
2531 return flow;
2532 error:
2533 isl_union_access_info_free(access);
2534 isl_compute_flow_schedule_data_clear(&data);
2535 return NULL;
2538 /* Given a description of the "sink" accesses, the "source" accesses and
2539 * a schedule, compute for each instance of a sink access
2540 * and for each element accessed by that instance,
2541 * the possible or definite source accesses that last accessed the
2542 * element accessed by the sink access before this sink access
2543 * in the sense that there is no intermediate definite source access.
2545 * The must_no_source and may_no_source elements of the result
2546 * are subsets of access->sink. The elements must_dep and may_dep
2547 * map domain elements of access->{may,must)_source to
2548 * domain elements of access->sink.
2550 * We check whether the schedule is available as a schedule tree
2551 * or a schedule map and call the correpsonding function to perform
2552 * the analysis.
2554 __isl_give isl_union_flow *isl_union_access_info_compute_flow(
2555 __isl_take isl_union_access_info *access)
2557 access = isl_union_access_info_normalize(access);
2558 if (!access)
2559 return NULL;
2560 if (access->schedule)
2561 return compute_flow_schedule(access);
2562 else
2563 return compute_flow_union_map(access);
2566 /* Print the information contained in "flow" to "p".
2567 * The information is printed as a YAML document.
2569 __isl_give isl_printer *isl_printer_print_union_flow(
2570 __isl_take isl_printer *p, __isl_keep isl_union_flow *flow)
2572 isl_union_map *umap;
2574 if (!flow)
2575 return isl_printer_free(p);
2577 p = isl_printer_yaml_start_mapping(p);
2578 p = print_union_map_field(p, "must_dependence", flow->must_dep);
2579 umap = isl_union_flow_get_may_dependence(flow);
2580 p = print_union_map_field(p, "may_dependence", umap);
2581 isl_union_map_free(umap);
2582 p = print_union_map_field(p, "must_no_source", flow->must_no_source);
2583 umap = isl_union_flow_get_may_no_source(flow);
2584 p = print_union_map_field(p, "may_no_source", umap);
2585 isl_union_map_free(umap);
2586 p = isl_printer_yaml_end_mapping(p);
2588 return p;
2591 /* Return a string representation of the information in "flow".
2592 * The information is printed in flow format.
2594 __isl_give char *isl_union_flow_to_str(__isl_keep isl_union_flow *flow)
2596 isl_printer *p;
2597 char *s;
2599 if (!flow)
2600 return NULL;
2602 p = isl_printer_to_str(isl_union_flow_get_ctx(flow));
2603 p = isl_printer_set_yaml_style(p, ISL_YAML_STYLE_FLOW);
2604 p = isl_printer_print_union_flow(p, flow);
2605 s = isl_printer_get_str(p);
2606 isl_printer_free(p);
2608 return s;
2611 /* Given a collection of "sink" and "source" accesses,
2612 * compute for each iteration of a sink access
2613 * and for each element accessed by that iteration,
2614 * the source access in the list that last accessed the
2615 * element accessed by the sink access before this sink access.
2616 * Each access is given as a map from the loop iterators
2617 * to the array indices.
2618 * The result is a relations between source and sink
2619 * iterations and a subset of the domain of the sink accesses,
2620 * corresponding to those iterations that access an element
2621 * not previously accessed.
2623 * We collect the inputs in an isl_union_access_info object,
2624 * call isl_union_access_info_compute_flow and extract
2625 * the outputs from the result.
2627 int isl_union_map_compute_flow(__isl_take isl_union_map *sink,
2628 __isl_take isl_union_map *must_source,
2629 __isl_take isl_union_map *may_source,
2630 __isl_take isl_union_map *schedule,
2631 __isl_give isl_union_map **must_dep, __isl_give isl_union_map **may_dep,
2632 __isl_give isl_union_map **must_no_source,
2633 __isl_give isl_union_map **may_no_source)
2635 isl_union_access_info *access;
2636 isl_union_flow *flow;
2638 access = isl_union_access_info_from_sink(sink);
2639 access = isl_union_access_info_set_must_source(access, must_source);
2640 access = isl_union_access_info_set_may_source(access, may_source);
2641 access = isl_union_access_info_set_schedule_map(access, schedule);
2642 flow = isl_union_access_info_compute_flow(access);
2644 if (must_dep)
2645 *must_dep = isl_union_flow_get_must_dependence(flow);
2646 if (may_dep)
2647 *may_dep = isl_union_flow_get_non_must_dependence(flow);
2648 if (must_no_source)
2649 *must_no_source = isl_union_flow_get_must_no_source(flow);
2650 if (may_no_source)
2651 *may_no_source = isl_union_flow_get_non_must_no_source(flow);
2653 isl_union_flow_free(flow);
2655 if ((must_dep && !*must_dep) || (may_dep && !*may_dep) ||
2656 (must_no_source && !*must_no_source) ||
2657 (may_no_source && !*may_no_source))
2658 goto error;
2660 return 0;
2661 error:
2662 if (must_dep)
2663 *must_dep = isl_union_map_free(*must_dep);
2664 if (may_dep)
2665 *may_dep = isl_union_map_free(*may_dep);
2666 if (must_no_source)
2667 *must_no_source = isl_union_map_free(*must_no_source);
2668 if (may_no_source)
2669 *may_no_source = isl_union_map_free(*may_no_source);
2670 return -1;