isl_scheduler.c: extract_edge: finish conversion to isl_stat return type
[isl.git] / isl_flow.c
blob37a29b0d9c6f9937efefbb08e657a79bdcf977fe
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>
26 #include <isl/stream.h>
28 enum isl_restriction_type {
29 isl_restriction_type_empty,
30 isl_restriction_type_none,
31 isl_restriction_type_input,
32 isl_restriction_type_output
35 struct isl_restriction {
36 enum isl_restriction_type type;
38 isl_set *source;
39 isl_set *sink;
42 /* Create a restriction of the given type.
44 static __isl_give isl_restriction *isl_restriction_alloc(
45 __isl_take isl_map *source_map, enum isl_restriction_type type)
47 isl_ctx *ctx;
48 isl_restriction *restr;
50 if (!source_map)
51 return NULL;
53 ctx = isl_map_get_ctx(source_map);
54 restr = isl_calloc_type(ctx, struct isl_restriction);
55 if (!restr)
56 goto error;
58 restr->type = type;
60 isl_map_free(source_map);
61 return restr;
62 error:
63 isl_map_free(source_map);
64 return NULL;
67 /* Create a restriction that doesn't restrict anything.
69 __isl_give isl_restriction *isl_restriction_none(__isl_take isl_map *source_map)
71 return isl_restriction_alloc(source_map, isl_restriction_type_none);
74 /* Create a restriction that removes everything.
76 __isl_give isl_restriction *isl_restriction_empty(
77 __isl_take isl_map *source_map)
79 return isl_restriction_alloc(source_map, isl_restriction_type_empty);
82 /* Create a restriction on the input of the maximization problem
83 * based on the given source and sink restrictions.
85 __isl_give isl_restriction *isl_restriction_input(
86 __isl_take isl_set *source_restr, __isl_take isl_set *sink_restr)
88 isl_ctx *ctx;
89 isl_restriction *restr;
91 if (!source_restr || !sink_restr)
92 goto error;
94 ctx = isl_set_get_ctx(source_restr);
95 restr = isl_calloc_type(ctx, struct isl_restriction);
96 if (!restr)
97 goto error;
99 restr->type = isl_restriction_type_input;
100 restr->source = source_restr;
101 restr->sink = sink_restr;
103 return restr;
104 error:
105 isl_set_free(source_restr);
106 isl_set_free(sink_restr);
107 return NULL;
110 /* Create a restriction on the output of the maximization problem
111 * based on the given source restriction.
113 __isl_give isl_restriction *isl_restriction_output(
114 __isl_take isl_set *source_restr)
116 isl_ctx *ctx;
117 isl_restriction *restr;
119 if (!source_restr)
120 return NULL;
122 ctx = isl_set_get_ctx(source_restr);
123 restr = isl_calloc_type(ctx, struct isl_restriction);
124 if (!restr)
125 goto error;
127 restr->type = isl_restriction_type_output;
128 restr->source = source_restr;
130 return restr;
131 error:
132 isl_set_free(source_restr);
133 return NULL;
136 __isl_null isl_restriction *isl_restriction_free(
137 __isl_take isl_restriction *restr)
139 if (!restr)
140 return NULL;
142 isl_set_free(restr->source);
143 isl_set_free(restr->sink);
144 free(restr);
145 return NULL;
148 isl_ctx *isl_restriction_get_ctx(__isl_keep isl_restriction *restr)
150 return restr ? isl_set_get_ctx(restr->source) : NULL;
153 /* A private structure to keep track of a mapping together with
154 * a user-specified identifier and a boolean indicating whether
155 * the map represents a must or may access/dependence.
157 struct isl_labeled_map {
158 struct isl_map *map;
159 void *data;
160 int must;
163 typedef int (*isl_access_coscheduled)(void *first, void *second);
165 /* A structure containing the input for dependence analysis:
166 * - a sink
167 * - n_must + n_may (<= max_source) sources
168 * - a function for determining the relative order of sources and sink
169 * - an optional function "coscheduled" for determining whether sources
170 * may be coscheduled. If "coscheduled" is NULL, then the sources
171 * are assumed not to be coscheduled.
172 * The must sources are placed before the may sources.
174 * domain_map is an auxiliary map that maps the sink access relation
175 * to the domain of this access relation.
176 * This field is only needed when restrict_fn is set and
177 * the field itself is set by isl_access_info_compute_flow.
179 * restrict_fn is a callback that (if not NULL) will be called
180 * right before any lexicographical maximization.
182 struct isl_access_info {
183 isl_map *domain_map;
184 struct isl_labeled_map sink;
185 isl_access_level_before level_before;
186 isl_access_coscheduled coscheduled;
188 isl_access_restrict restrict_fn;
189 void *restrict_user;
191 int max_source;
192 int n_must;
193 int n_may;
194 struct isl_labeled_map source[1];
197 /* A structure containing the output of dependence analysis:
198 * - n_source dependences
199 * - a wrapped subset of the sink for which definitely no source could be found
200 * - a wrapped subset of the sink for which possibly no source could be found
202 struct isl_flow {
203 isl_set *must_no_source;
204 isl_set *may_no_source;
205 int n_source;
206 struct isl_labeled_map *dep;
209 /* Construct an isl_access_info structure and fill it up with
210 * the given data. The number of sources is set to 0.
212 __isl_give isl_access_info *isl_access_info_alloc(__isl_take isl_map *sink,
213 void *sink_user, isl_access_level_before fn, int max_source)
215 isl_ctx *ctx;
216 struct isl_access_info *acc;
218 if (!sink)
219 return NULL;
221 ctx = isl_map_get_ctx(sink);
222 isl_assert(ctx, max_source >= 0, goto error);
224 acc = isl_calloc(ctx, struct isl_access_info,
225 sizeof(struct isl_access_info) +
226 (max_source - 1) * sizeof(struct isl_labeled_map));
227 if (!acc)
228 goto error;
230 acc->sink.map = sink;
231 acc->sink.data = sink_user;
232 acc->level_before = fn;
233 acc->max_source = max_source;
234 acc->n_must = 0;
235 acc->n_may = 0;
237 return acc;
238 error:
239 isl_map_free(sink);
240 return NULL;
243 /* Free the given isl_access_info structure.
245 __isl_null isl_access_info *isl_access_info_free(
246 __isl_take isl_access_info *acc)
248 int i;
250 if (!acc)
251 return NULL;
252 isl_map_free(acc->domain_map);
253 isl_map_free(acc->sink.map);
254 for (i = 0; i < acc->n_must + acc->n_may; ++i)
255 isl_map_free(acc->source[i].map);
256 free(acc);
257 return NULL;
260 isl_ctx *isl_access_info_get_ctx(__isl_keep isl_access_info *acc)
262 return acc ? isl_map_get_ctx(acc->sink.map) : NULL;
265 __isl_give isl_access_info *isl_access_info_set_restrict(
266 __isl_take isl_access_info *acc, isl_access_restrict fn, void *user)
268 if (!acc)
269 return NULL;
270 acc->restrict_fn = fn;
271 acc->restrict_user = user;
272 return acc;
275 /* Add another source to an isl_access_info structure, making
276 * sure the "must" sources are placed before the "may" sources.
277 * This function may be called at most max_source times on a
278 * given isl_access_info structure, with max_source as specified
279 * in the call to isl_access_info_alloc that constructed the structure.
281 __isl_give isl_access_info *isl_access_info_add_source(
282 __isl_take isl_access_info *acc, __isl_take isl_map *source,
283 int must, void *source_user)
285 isl_ctx *ctx;
287 if (!acc)
288 goto error;
289 ctx = isl_map_get_ctx(acc->sink.map);
290 isl_assert(ctx, acc->n_must + acc->n_may < acc->max_source, goto error);
292 if (must) {
293 if (acc->n_may)
294 acc->source[acc->n_must + acc->n_may] =
295 acc->source[acc->n_must];
296 acc->source[acc->n_must].map = source;
297 acc->source[acc->n_must].data = source_user;
298 acc->source[acc->n_must].must = 1;
299 acc->n_must++;
300 } else {
301 acc->source[acc->n_must + acc->n_may].map = source;
302 acc->source[acc->n_must + acc->n_may].data = source_user;
303 acc->source[acc->n_must + acc->n_may].must = 0;
304 acc->n_may++;
307 return acc;
308 error:
309 isl_map_free(source);
310 isl_access_info_free(acc);
311 return NULL;
314 /* A helper struct carrying the isl_access_info and an error condition.
316 struct access_sort_info {
317 isl_access_info *access_info;
318 int error;
321 /* Return -n, 0 or n (with n a positive value), depending on whether
322 * the source access identified by p1 should be sorted before, together
323 * or after that identified by p2.
325 * If p1 appears before p2, then it should be sorted first.
326 * For more generic initial schedules, it is possible that neither
327 * p1 nor p2 appears before the other, or at least not in any obvious way.
328 * We therefore also check if p2 appears before p1, in which case p2
329 * should be sorted first.
330 * If not, we try to order the two statements based on the description
331 * of the iteration domains. This results in an arbitrary, but fairly
332 * stable ordering.
334 * In case of an error, sort_info.error is set to true and all elements are
335 * reported to be equal.
337 static int access_sort_cmp(const void *p1, const void *p2, void *user)
339 struct access_sort_info *sort_info = user;
340 isl_access_info *acc = sort_info->access_info;
342 if (sort_info->error)
343 return 0;
345 const struct isl_labeled_map *i1, *i2;
346 int level1, level2;
347 uint32_t h1, h2;
348 i1 = (const struct isl_labeled_map *) p1;
349 i2 = (const struct isl_labeled_map *) p2;
351 level1 = acc->level_before(i1->data, i2->data);
352 if (level1 < 0)
353 goto error;
354 if (level1 % 2)
355 return -1;
357 level2 = acc->level_before(i2->data, i1->data);
358 if (level2 < 0)
359 goto error;
360 if (level2 % 2)
361 return 1;
363 h1 = isl_map_get_hash(i1->map);
364 h2 = isl_map_get_hash(i2->map);
365 return h1 > h2 ? 1 : h1 < h2 ? -1 : 0;
366 error:
367 sort_info->error = 1;
368 return 0;
371 /* Sort the must source accesses in their textual order.
373 static __isl_give isl_access_info *isl_access_info_sort_sources(
374 __isl_take isl_access_info *acc)
376 struct access_sort_info sort_info;
378 sort_info.access_info = acc;
379 sort_info.error = 0;
381 if (!acc)
382 return NULL;
383 if (acc->n_must <= 1)
384 return acc;
386 if (isl_sort(acc->source, acc->n_must, sizeof(struct isl_labeled_map),
387 access_sort_cmp, &sort_info) < 0)
388 return isl_access_info_free(acc);
389 if (sort_info.error)
390 return isl_access_info_free(acc);
392 return acc;
395 /* Align the parameters of the two spaces if needed and then call
396 * isl_space_join.
398 static __isl_give isl_space *space_align_and_join(__isl_take isl_space *left,
399 __isl_take isl_space *right)
401 isl_bool equal_params;
403 equal_params = isl_space_has_equal_params(left, right);
404 if (equal_params < 0)
405 goto error;
406 if (equal_params)
407 return isl_space_join(left, right);
409 left = isl_space_align_params(left, isl_space_copy(right));
410 right = isl_space_align_params(right, isl_space_copy(left));
411 return isl_space_join(left, right);
412 error:
413 isl_space_free(left);
414 isl_space_free(right);
415 return NULL;
418 /* Initialize an empty isl_flow structure corresponding to a given
419 * isl_access_info structure.
420 * For each must access, two dependences are created (initialized
421 * to the empty relation), one for the resulting must dependences
422 * and one for the resulting may dependences. May accesses can
423 * only lead to may dependences, so only one dependence is created
424 * for each of them.
425 * This function is private as isl_flow structures are only supposed
426 * to be created by isl_access_info_compute_flow.
428 static __isl_give isl_flow *isl_flow_alloc(__isl_keep isl_access_info *acc)
430 int i, n;
431 struct isl_ctx *ctx;
432 struct isl_flow *dep;
434 if (!acc)
435 return NULL;
437 ctx = isl_map_get_ctx(acc->sink.map);
438 dep = isl_calloc_type(ctx, struct isl_flow);
439 if (!dep)
440 return NULL;
442 n = 2 * acc->n_must + acc->n_may;
443 dep->dep = isl_calloc_array(ctx, struct isl_labeled_map, n);
444 if (n && !dep->dep)
445 goto error;
447 dep->n_source = n;
448 for (i = 0; i < acc->n_must; ++i) {
449 isl_space *dim;
450 dim = space_align_and_join(
451 isl_map_get_space(acc->source[i].map),
452 isl_space_reverse(isl_map_get_space(acc->sink.map)));
453 dep->dep[2 * i].map = isl_map_empty(dim);
454 dep->dep[2 * i + 1].map = isl_map_copy(dep->dep[2 * i].map);
455 dep->dep[2 * i].data = acc->source[i].data;
456 dep->dep[2 * i + 1].data = acc->source[i].data;
457 dep->dep[2 * i].must = 1;
458 dep->dep[2 * i + 1].must = 0;
459 if (!dep->dep[2 * i].map || !dep->dep[2 * i + 1].map)
460 goto error;
462 for (i = acc->n_must; i < acc->n_must + acc->n_may; ++i) {
463 isl_space *dim;
464 dim = space_align_and_join(
465 isl_map_get_space(acc->source[i].map),
466 isl_space_reverse(isl_map_get_space(acc->sink.map)));
467 dep->dep[acc->n_must + i].map = isl_map_empty(dim);
468 dep->dep[acc->n_must + i].data = acc->source[i].data;
469 dep->dep[acc->n_must + i].must = 0;
470 if (!dep->dep[acc->n_must + i].map)
471 goto error;
474 return dep;
475 error:
476 isl_flow_free(dep);
477 return NULL;
480 /* Iterate over all sources and for each resulting flow dependence
481 * that is not empty, call the user specfied function.
482 * The second argument in this function call identifies the source,
483 * while the third argument correspond to the final argument of
484 * the isl_flow_foreach call.
486 isl_stat isl_flow_foreach(__isl_keep isl_flow *deps,
487 isl_stat (*fn)(__isl_take isl_map *dep, int must, void *dep_user,
488 void *user),
489 void *user)
491 int i;
493 if (!deps)
494 return isl_stat_error;
496 for (i = 0; i < deps->n_source; ++i) {
497 if (isl_map_plain_is_empty(deps->dep[i].map))
498 continue;
499 if (fn(isl_map_copy(deps->dep[i].map), deps->dep[i].must,
500 deps->dep[i].data, user) < 0)
501 return isl_stat_error;
504 return isl_stat_ok;
507 /* Return a copy of the subset of the sink for which no source could be found.
509 __isl_give isl_map *isl_flow_get_no_source(__isl_keep isl_flow *deps, int must)
511 if (!deps)
512 return NULL;
514 if (must)
515 return isl_set_unwrap(isl_set_copy(deps->must_no_source));
516 else
517 return isl_set_unwrap(isl_set_copy(deps->may_no_source));
520 void isl_flow_free(__isl_take isl_flow *deps)
522 int i;
524 if (!deps)
525 return;
526 isl_set_free(deps->must_no_source);
527 isl_set_free(deps->may_no_source);
528 if (deps->dep) {
529 for (i = 0; i < deps->n_source; ++i)
530 isl_map_free(deps->dep[i].map);
531 free(deps->dep);
533 free(deps);
536 isl_ctx *isl_flow_get_ctx(__isl_keep isl_flow *deps)
538 return deps ? isl_set_get_ctx(deps->must_no_source) : NULL;
541 /* Return a map that enforces that the domain iteration occurs after
542 * the range iteration at the given level.
543 * If level is odd, then the domain iteration should occur after
544 * the target iteration in their shared level/2 outermost loops.
545 * In this case we simply need to enforce that these outermost
546 * loop iterations are the same.
547 * If level is even, then the loop iterator of the domain should
548 * be greater than the loop iterator of the range at the last
549 * of the level/2 shared loops, i.e., loop level/2 - 1.
551 static __isl_give isl_map *after_at_level(__isl_take isl_space *dim, int level)
553 struct isl_basic_map *bmap;
555 if (level % 2)
556 bmap = isl_basic_map_equal(dim, level/2);
557 else
558 bmap = isl_basic_map_more_at(dim, level/2 - 1);
560 return isl_map_from_basic_map(bmap);
563 /* Compute the partial lexicographic maximum of "dep" on domain "sink",
564 * but first check if the user has set acc->restrict_fn and if so
565 * update either the input or the output of the maximization problem
566 * with respect to the resulting restriction.
568 * Since the user expects a mapping from sink iterations to source iterations,
569 * whereas the domain of "dep" is a wrapped map, mapping sink iterations
570 * to accessed array elements, we first need to project out the accessed
571 * sink array elements by applying acc->domain_map.
572 * Similarly, the sink restriction specified by the user needs to be
573 * converted back to the wrapped map.
575 static __isl_give isl_map *restricted_partial_lexmax(
576 __isl_keep isl_access_info *acc, __isl_take isl_map *dep,
577 int source, __isl_take isl_set *sink, __isl_give isl_set **empty)
579 isl_map *source_map;
580 isl_restriction *restr;
581 isl_set *sink_domain;
582 isl_set *sink_restr;
583 isl_map *res;
585 if (!acc->restrict_fn)
586 return isl_map_partial_lexmax(dep, sink, empty);
588 source_map = isl_map_copy(dep);
589 source_map = isl_map_apply_domain(source_map,
590 isl_map_copy(acc->domain_map));
591 sink_domain = isl_set_copy(sink);
592 sink_domain = isl_set_apply(sink_domain, isl_map_copy(acc->domain_map));
593 restr = acc->restrict_fn(source_map, sink_domain,
594 acc->source[source].data, acc->restrict_user);
595 isl_set_free(sink_domain);
596 isl_map_free(source_map);
598 if (!restr)
599 goto error;
600 if (restr->type == isl_restriction_type_input) {
601 dep = isl_map_intersect_range(dep, isl_set_copy(restr->source));
602 sink_restr = isl_set_copy(restr->sink);
603 sink_restr = isl_set_apply(sink_restr,
604 isl_map_reverse(isl_map_copy(acc->domain_map)));
605 sink = isl_set_intersect(sink, sink_restr);
606 } else if (restr->type == isl_restriction_type_empty) {
607 isl_space *space = isl_map_get_space(dep);
608 isl_map_free(dep);
609 dep = isl_map_empty(space);
612 res = isl_map_partial_lexmax(dep, sink, empty);
614 if (restr->type == isl_restriction_type_output)
615 res = isl_map_intersect_range(res, isl_set_copy(restr->source));
617 isl_restriction_free(restr);
618 return res;
619 error:
620 isl_map_free(dep);
621 isl_set_free(sink);
622 *empty = NULL;
623 return NULL;
626 /* Compute the last iteration of must source j that precedes the sink
627 * at the given level for sink iterations in set_C.
628 * The subset of set_C for which no such iteration can be found is returned
629 * in *empty.
631 static struct isl_map *last_source(struct isl_access_info *acc,
632 struct isl_set *set_C,
633 int j, int level, struct isl_set **empty)
635 struct isl_map *read_map;
636 struct isl_map *write_map;
637 struct isl_map *dep_map;
638 struct isl_map *after;
639 struct isl_map *result;
641 read_map = isl_map_copy(acc->sink.map);
642 write_map = isl_map_copy(acc->source[j].map);
643 write_map = isl_map_reverse(write_map);
644 dep_map = isl_map_apply_range(read_map, write_map);
645 after = after_at_level(isl_map_get_space(dep_map), level);
646 dep_map = isl_map_intersect(dep_map, after);
647 result = restricted_partial_lexmax(acc, dep_map, j, set_C, empty);
648 result = isl_map_reverse(result);
650 return result;
653 /* For a given mapping between iterations of must source j and iterations
654 * of the sink, compute the last iteration of must source k preceding
655 * the sink at level before_level for any of the sink iterations,
656 * but following the corresponding iteration of must source j at level
657 * after_level.
659 static struct isl_map *last_later_source(struct isl_access_info *acc,
660 struct isl_map *old_map,
661 int j, int before_level,
662 int k, int after_level,
663 struct isl_set **empty)
665 isl_space *dim;
666 struct isl_set *set_C;
667 struct isl_map *read_map;
668 struct isl_map *write_map;
669 struct isl_map *dep_map;
670 struct isl_map *after_write;
671 struct isl_map *before_read;
672 struct isl_map *result;
674 set_C = isl_map_range(isl_map_copy(old_map));
675 read_map = isl_map_copy(acc->sink.map);
676 write_map = isl_map_copy(acc->source[k].map);
678 write_map = isl_map_reverse(write_map);
679 dep_map = isl_map_apply_range(read_map, write_map);
680 dim = space_align_and_join(isl_map_get_space(acc->source[k].map),
681 isl_space_reverse(isl_map_get_space(acc->source[j].map)));
682 after_write = after_at_level(dim, after_level);
683 after_write = isl_map_apply_range(after_write, old_map);
684 after_write = isl_map_reverse(after_write);
685 dep_map = isl_map_intersect(dep_map, after_write);
686 before_read = after_at_level(isl_map_get_space(dep_map), before_level);
687 dep_map = isl_map_intersect(dep_map, before_read);
688 result = restricted_partial_lexmax(acc, dep_map, k, set_C, empty);
689 result = isl_map_reverse(result);
691 return result;
694 /* Given a shared_level between two accesses, return 1 if the
695 * the first can precede the second at the requested target_level.
696 * If the target level is odd, i.e., refers to a statement level
697 * dimension, then first needs to precede second at the requested
698 * level, i.e., shared_level must be equal to target_level.
699 * If the target level is odd, then the two loops should share
700 * at least the requested number of outer loops.
702 static int can_precede_at_level(int shared_level, int target_level)
704 if (shared_level < target_level)
705 return 0;
706 if ((target_level % 2) && shared_level > target_level)
707 return 0;
708 return 1;
711 /* Given a possible flow dependence temp_rel[j] between source j and the sink
712 * at level sink_level, remove those elements for which
713 * there is an iteration of another source k < j that is closer to the sink.
714 * The flow dependences temp_rel[k] are updated with the improved sources.
715 * Any improved source needs to precede the sink at the same level
716 * and needs to follow source j at the same or a deeper level.
717 * The lower this level, the later the execution date of source k.
718 * We therefore consider lower levels first.
720 * If temp_rel[j] is empty, then there can be no improvement and
721 * we return immediately.
723 * This function returns isl_stat_ok in case it was executed successfully and
724 * isl_stat_error in case of errors during the execution of this function.
726 static isl_stat intermediate_sources(__isl_keep isl_access_info *acc,
727 struct isl_map **temp_rel, int j, int sink_level)
729 int k, level;
730 int depth = 2 * isl_map_dim(acc->source[j].map, isl_dim_in) + 1;
732 if (isl_map_plain_is_empty(temp_rel[j]))
733 return isl_stat_ok;
735 for (k = j - 1; k >= 0; --k) {
736 int plevel, plevel2;
737 plevel = acc->level_before(acc->source[k].data, acc->sink.data);
738 if (plevel < 0)
739 return isl_stat_error;
740 if (!can_precede_at_level(plevel, sink_level))
741 continue;
743 plevel2 = acc->level_before(acc->source[j].data,
744 acc->source[k].data);
745 if (plevel2 < 0)
746 return isl_stat_error;
748 for (level = sink_level; level <= depth; ++level) {
749 struct isl_map *T;
750 struct isl_set *trest;
751 struct isl_map *copy;
753 if (!can_precede_at_level(plevel2, level))
754 continue;
756 copy = isl_map_copy(temp_rel[j]);
757 T = last_later_source(acc, copy, j, sink_level, k,
758 level, &trest);
759 if (isl_map_plain_is_empty(T)) {
760 isl_set_free(trest);
761 isl_map_free(T);
762 continue;
764 temp_rel[j] = isl_map_intersect_range(temp_rel[j], trest);
765 temp_rel[k] = isl_map_union_disjoint(temp_rel[k], T);
769 return isl_stat_ok;
772 /* Compute all iterations of may source j that precedes the sink at the given
773 * level for sink iterations in set_C.
775 static __isl_give isl_map *all_sources(__isl_keep isl_access_info *acc,
776 __isl_take isl_set *set_C, int j, int level)
778 isl_map *read_map;
779 isl_map *write_map;
780 isl_map *dep_map;
781 isl_map *after;
783 read_map = isl_map_copy(acc->sink.map);
784 read_map = isl_map_intersect_domain(read_map, set_C);
785 write_map = isl_map_copy(acc->source[acc->n_must + j].map);
786 write_map = isl_map_reverse(write_map);
787 dep_map = isl_map_apply_range(read_map, write_map);
788 after = after_at_level(isl_map_get_space(dep_map), level);
789 dep_map = isl_map_intersect(dep_map, after);
791 return isl_map_reverse(dep_map);
794 /* For a given mapping between iterations of must source k and iterations
795 * of the sink, compute all iterations of may source j preceding
796 * the sink at level before_level for any of the sink iterations,
797 * but following the corresponding iteration of must source k at level
798 * after_level.
800 static __isl_give isl_map *all_later_sources(__isl_keep isl_access_info *acc,
801 __isl_take isl_map *old_map,
802 int j, int before_level, int k, int after_level)
804 isl_space *dim;
805 isl_set *set_C;
806 isl_map *read_map;
807 isl_map *write_map;
808 isl_map *dep_map;
809 isl_map *after_write;
810 isl_map *before_read;
812 set_C = isl_map_range(isl_map_copy(old_map));
813 read_map = isl_map_copy(acc->sink.map);
814 read_map = isl_map_intersect_domain(read_map, set_C);
815 write_map = isl_map_copy(acc->source[acc->n_must + j].map);
817 write_map = isl_map_reverse(write_map);
818 dep_map = isl_map_apply_range(read_map, write_map);
819 dim = isl_space_join(isl_map_get_space(acc->source[acc->n_must + j].map),
820 isl_space_reverse(isl_map_get_space(acc->source[k].map)));
821 after_write = after_at_level(dim, after_level);
822 after_write = isl_map_apply_range(after_write, old_map);
823 after_write = isl_map_reverse(after_write);
824 dep_map = isl_map_intersect(dep_map, after_write);
825 before_read = after_at_level(isl_map_get_space(dep_map), before_level);
826 dep_map = isl_map_intersect(dep_map, before_read);
827 return isl_map_reverse(dep_map);
830 /* Given the must and may dependence relations for the must accesses
831 * for level sink_level, check if there are any accesses of may access j
832 * that occur in between and return their union.
833 * If some of these accesses are intermediate with respect to
834 * (previously thought to be) must dependences, then these
835 * must dependences are turned into may dependences.
837 static __isl_give isl_map *all_intermediate_sources(
838 __isl_keep isl_access_info *acc, __isl_take isl_map *map,
839 struct isl_map **must_rel, struct isl_map **may_rel,
840 int j, int sink_level)
842 int k, level;
843 int depth = 2 * isl_map_dim(acc->source[acc->n_must + j].map,
844 isl_dim_in) + 1;
846 for (k = 0; k < acc->n_must; ++k) {
847 int plevel;
849 if (isl_map_plain_is_empty(may_rel[k]) &&
850 isl_map_plain_is_empty(must_rel[k]))
851 continue;
853 plevel = acc->level_before(acc->source[k].data,
854 acc->source[acc->n_must + j].data);
855 if (plevel < 0)
856 return isl_map_free(map);
858 for (level = sink_level; level <= depth; ++level) {
859 isl_map *T;
860 isl_map *copy;
861 isl_set *ran;
863 if (!can_precede_at_level(plevel, level))
864 continue;
866 copy = isl_map_copy(may_rel[k]);
867 T = all_later_sources(acc, copy, j, sink_level, k, level);
868 map = isl_map_union(map, T);
870 copy = isl_map_copy(must_rel[k]);
871 T = all_later_sources(acc, copy, j, sink_level, k, level);
872 ran = isl_map_range(isl_map_copy(T));
873 map = isl_map_union(map, T);
874 may_rel[k] = isl_map_union_disjoint(may_rel[k],
875 isl_map_intersect_range(isl_map_copy(must_rel[k]),
876 isl_set_copy(ran)));
877 T = isl_map_from_domain_and_range(
878 isl_set_universe(
879 isl_space_domain(isl_map_get_space(must_rel[k]))),
880 ran);
881 must_rel[k] = isl_map_subtract(must_rel[k], T);
885 return map;
888 /* Given a dependence relation "old_map" between a must-source and the sink,
889 * return a subset of the dependences, augmented with instances
890 * of the source at position "pos" in "acc" that are coscheduled
891 * with the must-source and that access the same element.
892 * That is, if the input lives in a space T -> K, then the output
893 * lives in the space [T -> S] -> K, with S the space of source "pos", and
894 * the domain factor of the domain product is a subset of the input.
895 * The sources are considered to be coscheduled if they have the same values
896 * for the initial "depth" coordinates.
898 * First construct a dependence relation S -> K and a mapping
899 * between coscheduled sources T -> S.
900 * The second is combined with the original dependence relation T -> K
901 * to form a relation in T -> [S -> K], which is subsequently
902 * uncurried to [T -> S] -> K.
903 * This result is then intersected with the dependence relation S -> K
904 * to form the output.
906 * In case a negative depth is given, NULL is returned to indicate an error.
908 static __isl_give isl_map *coscheduled_source(__isl_keep isl_access_info *acc,
909 __isl_keep isl_map *old_map, int pos, int depth)
911 isl_space *space;
912 isl_set *set_C;
913 isl_map *read_map;
914 isl_map *write_map;
915 isl_map *dep_map;
916 isl_map *equal;
917 isl_map *map;
919 if (depth < 0)
920 return NULL;
922 set_C = isl_map_range(isl_map_copy(old_map));
923 read_map = isl_map_copy(acc->sink.map);
924 read_map = isl_map_intersect_domain(read_map, set_C);
925 write_map = isl_map_copy(acc->source[pos].map);
926 dep_map = isl_map_domain_product(write_map, read_map);
927 dep_map = isl_set_unwrap(isl_map_domain(dep_map));
928 space = isl_space_join(isl_map_get_space(old_map),
929 isl_space_reverse(isl_map_get_space(dep_map)));
930 equal = isl_map_from_basic_map(isl_basic_map_equal(space, depth));
931 map = isl_map_range_product(equal, isl_map_copy(old_map));
932 map = isl_map_uncurry(map);
933 map = isl_map_intersect_domain_factor_range(map, dep_map);
935 return map;
938 /* After the dependences derived from a must-source have been computed
939 * at a certain level, check if any of the sources of the must-dependences
940 * may be coscheduled with other sources.
941 * If they are any such sources, then there is no way of determining
942 * which of the sources actually comes last and the must-dependences
943 * need to be turned into may-dependences, while dependences from
944 * the other sources need to be added to the may-dependences as well.
945 * "acc" describes the sources and a callback for checking whether
946 * two sources may be coscheduled. If acc->coscheduled is NULL then
947 * the sources are assumed not to be coscheduled.
948 * "must_rel" and "may_rel" describe the must and may-dependence relations
949 * computed at the current level for the must-sources. Some of the dependences
950 * may be moved from "must_rel" to "may_rel".
951 * "flow" contains all dependences computed so far (apart from those
952 * in "must_rel" and "may_rel") and may be updated with additional
953 * dependences derived from may-sources.
955 * In particular, consider all the must-sources with a non-empty
956 * dependence relation in "must_rel". They are considered in reverse
957 * order because that is the order in which they are considered in the caller.
958 * If any of the must-sources are coscheduled, then the last one
959 * is the one that will have a corresponding dependence relation.
960 * For each must-source i, consider both all the previous must-sources
961 * and all the may-sources. If any of those may be coscheduled with
962 * must-source i, then compute the coscheduled instances that access
963 * the same memory elements. The result is a relation [T -> S] -> K.
964 * The projection onto T -> K is a subset of the must-dependence relation
965 * that needs to be turned into may-dependences.
966 * The projection onto S -> K needs to be added to the may-dependences
967 * of source S.
968 * Since a given must-source instance may be coscheduled with several
969 * other source instances, the dependences that need to be turned
970 * into may-dependences are first collected and only actually removed
971 * from the must-dependences after all other sources have been considered.
973 static __isl_give isl_flow *handle_coscheduled(__isl_keep isl_access_info *acc,
974 __isl_keep isl_map **must_rel, __isl_keep isl_map **may_rel,
975 __isl_take isl_flow *flow)
977 int i, j;
979 if (!acc->coscheduled)
980 return flow;
981 for (i = acc->n_must - 1; i >= 0; --i) {
982 isl_map *move;
984 if (isl_map_plain_is_empty(must_rel[i]))
985 continue;
986 move = isl_map_empty(isl_map_get_space(must_rel[i]));
987 for (j = i - 1; j >= 0; --j) {
988 int depth;
989 isl_map *map, *factor;
991 if (!acc->coscheduled(acc->source[i].data,
992 acc->source[j].data))
993 continue;
994 depth = acc->level_before(acc->source[i].data,
995 acc->source[j].data) / 2;
996 map = coscheduled_source(acc, must_rel[i], j, depth);
997 factor = isl_map_domain_factor_range(isl_map_copy(map));
998 may_rel[j] = isl_map_union(may_rel[j], factor);
999 map = isl_map_domain_factor_domain(map);
1000 move = isl_map_union(move, map);
1002 for (j = 0; j < acc->n_may; ++j) {
1003 int depth, pos;
1004 isl_map *map, *factor;
1006 pos = acc->n_must + j;
1007 if (!acc->coscheduled(acc->source[i].data,
1008 acc->source[pos].data))
1009 continue;
1010 depth = acc->level_before(acc->source[i].data,
1011 acc->source[pos].data) / 2;
1012 map = coscheduled_source(acc, must_rel[i], pos, depth);
1013 factor = isl_map_domain_factor_range(isl_map_copy(map));
1014 pos = 2 * acc->n_must + j;
1015 flow->dep[pos].map = isl_map_union(flow->dep[pos].map,
1016 factor);
1017 map = isl_map_domain_factor_domain(map);
1018 move = isl_map_union(move, map);
1020 must_rel[i] = isl_map_subtract(must_rel[i], isl_map_copy(move));
1021 may_rel[i] = isl_map_union(may_rel[i], move);
1024 return flow;
1027 /* Compute dependences for the case where all accesses are "may"
1028 * accesses, which boils down to computing memory based dependences.
1029 * The generic algorithm would also work in this case, but it would
1030 * be overkill to use it.
1032 static __isl_give isl_flow *compute_mem_based_dependences(
1033 __isl_keep isl_access_info *acc)
1035 int i;
1036 isl_set *mustdo;
1037 isl_set *maydo;
1038 isl_flow *res;
1040 res = isl_flow_alloc(acc);
1041 if (!res)
1042 return NULL;
1044 mustdo = isl_map_domain(isl_map_copy(acc->sink.map));
1045 maydo = isl_set_copy(mustdo);
1047 for (i = 0; i < acc->n_may; ++i) {
1048 int plevel;
1049 int is_before;
1050 isl_space *dim;
1051 isl_map *before;
1052 isl_map *dep;
1054 plevel = acc->level_before(acc->source[i].data, acc->sink.data);
1055 if (plevel < 0)
1056 goto error;
1058 is_before = plevel & 1;
1059 plevel >>= 1;
1061 dim = isl_map_get_space(res->dep[i].map);
1062 if (is_before)
1063 before = isl_map_lex_le_first(dim, plevel);
1064 else
1065 before = isl_map_lex_lt_first(dim, plevel);
1066 dep = isl_map_apply_range(isl_map_copy(acc->source[i].map),
1067 isl_map_reverse(isl_map_copy(acc->sink.map)));
1068 dep = isl_map_intersect(dep, before);
1069 mustdo = isl_set_subtract(mustdo,
1070 isl_map_range(isl_map_copy(dep)));
1071 res->dep[i].map = isl_map_union(res->dep[i].map, dep);
1074 res->may_no_source = isl_set_subtract(maydo, isl_set_copy(mustdo));
1075 res->must_no_source = mustdo;
1077 return res;
1078 error:
1079 isl_set_free(mustdo);
1080 isl_set_free(maydo);
1081 isl_flow_free(res);
1082 return NULL;
1085 /* Compute dependences for the case where there is at least one
1086 * "must" access.
1088 * The core algorithm considers all levels in which a source may precede
1089 * the sink, where a level may either be a statement level or a loop level.
1090 * The outermost statement level is 1, the first loop level is 2, etc...
1091 * The algorithm basically does the following:
1092 * for all levels l of the read access from innermost to outermost
1093 * for all sources w that may precede the sink access at that level
1094 * compute the last iteration of the source that precedes the sink access
1095 * at that level
1096 * add result to possible last accesses at level l of source w
1097 * for all sources w2 that we haven't considered yet at this level that may
1098 * also precede the sink access
1099 * for all levels l2 of w from l to innermost
1100 * for all possible last accesses dep of w at l
1101 * compute last iteration of w2 between the source and sink
1102 * of dep
1103 * add result to possible last accesses at level l of write w2
1104 * and replace possible last accesses dep by the remainder
1107 * The above algorithm is applied to the must access. During the course
1108 * of the algorithm, we keep track of sink iterations that still
1109 * need to be considered. These iterations are split into those that
1110 * haven't been matched to any source access (mustdo) and those that have only
1111 * been matched to may accesses (maydo).
1112 * At the end of each level, must-sources and may-sources that are coscheduled
1113 * with the sources of the must-dependences at that level are considered.
1114 * If any coscheduled instances are found, then corresponding may-dependences
1115 * are added and the original must-dependences are turned into may-dependences.
1116 * Afterwards, the may accesses that occur after must-dependence sources
1117 * are considered.
1118 * In particular, we consider may accesses that precede the remaining
1119 * sink iterations, moving elements from mustdo to maydo when appropriate,
1120 * and may accesses that occur between a must source and a sink of any
1121 * dependences found at the current level, turning must dependences into
1122 * may dependences when appropriate.
1125 static __isl_give isl_flow *compute_val_based_dependences(
1126 __isl_keep isl_access_info *acc)
1128 isl_ctx *ctx;
1129 isl_flow *res;
1130 isl_set *mustdo = NULL;
1131 isl_set *maydo = NULL;
1132 int level, j;
1133 int depth;
1134 isl_map **must_rel = NULL;
1135 isl_map **may_rel = NULL;
1137 if (!acc)
1138 return NULL;
1140 res = isl_flow_alloc(acc);
1141 if (!res)
1142 goto error;
1143 ctx = isl_map_get_ctx(acc->sink.map);
1145 depth = 2 * isl_map_dim(acc->sink.map, isl_dim_in) + 1;
1146 mustdo = isl_map_domain(isl_map_copy(acc->sink.map));
1147 maydo = isl_set_empty(isl_set_get_space(mustdo));
1148 if (!mustdo || !maydo)
1149 goto error;
1150 if (isl_set_plain_is_empty(mustdo))
1151 goto done;
1153 must_rel = isl_calloc_array(ctx, struct isl_map *, acc->n_must);
1154 may_rel = isl_calloc_array(ctx, struct isl_map *, acc->n_must);
1155 if (!must_rel || !may_rel)
1156 goto error;
1158 for (level = depth; level >= 1; --level) {
1159 for (j = acc->n_must-1; j >=0; --j) {
1160 isl_space *space;
1161 space = isl_map_get_space(res->dep[2 * j].map);
1162 must_rel[j] = isl_map_empty(space);
1163 may_rel[j] = isl_map_copy(must_rel[j]);
1166 for (j = acc->n_must - 1; j >= 0; --j) {
1167 struct isl_map *T;
1168 struct isl_set *rest;
1169 int plevel;
1171 plevel = acc->level_before(acc->source[j].data,
1172 acc->sink.data);
1173 if (plevel < 0)
1174 goto error;
1175 if (!can_precede_at_level(plevel, level))
1176 continue;
1178 T = last_source(acc, mustdo, j, level, &rest);
1179 must_rel[j] = isl_map_union_disjoint(must_rel[j], T);
1180 mustdo = rest;
1182 if (intermediate_sources(acc, must_rel, j, level) < 0)
1183 goto error;
1185 T = last_source(acc, maydo, j, level, &rest);
1186 may_rel[j] = isl_map_union_disjoint(may_rel[j], T);
1187 maydo = rest;
1189 if (intermediate_sources(acc, may_rel, j, level) < 0)
1190 goto error;
1192 if (isl_set_plain_is_empty(mustdo) &&
1193 isl_set_plain_is_empty(maydo))
1194 break;
1196 for (j = j - 1; j >= 0; --j) {
1197 int plevel;
1199 plevel = acc->level_before(acc->source[j].data,
1200 acc->sink.data);
1201 if (plevel < 0)
1202 goto error;
1203 if (!can_precede_at_level(plevel, level))
1204 continue;
1206 if (intermediate_sources(acc, must_rel, j, level) < 0)
1207 goto error;
1208 if (intermediate_sources(acc, may_rel, j, level) < 0)
1209 goto error;
1212 handle_coscheduled(acc, must_rel, may_rel, res);
1214 for (j = 0; j < acc->n_may; ++j) {
1215 int plevel;
1216 isl_map *T;
1217 isl_set *ran;
1219 plevel = acc->level_before(acc->source[acc->n_must + j].data,
1220 acc->sink.data);
1221 if (plevel < 0)
1222 goto error;
1223 if (!can_precede_at_level(plevel, level))
1224 continue;
1226 T = all_sources(acc, isl_set_copy(maydo), j, level);
1227 res->dep[2 * acc->n_must + j].map =
1228 isl_map_union(res->dep[2 * acc->n_must + j].map, T);
1229 T = all_sources(acc, isl_set_copy(mustdo), j, level);
1230 ran = isl_map_range(isl_map_copy(T));
1231 res->dep[2 * acc->n_must + j].map =
1232 isl_map_union(res->dep[2 * acc->n_must + j].map, T);
1233 mustdo = isl_set_subtract(mustdo, isl_set_copy(ran));
1234 maydo = isl_set_union_disjoint(maydo, ran);
1236 T = res->dep[2 * acc->n_must + j].map;
1237 T = all_intermediate_sources(acc, T, must_rel, may_rel,
1238 j, level);
1239 res->dep[2 * acc->n_must + j].map = T;
1242 for (j = acc->n_must - 1; j >= 0; --j) {
1243 res->dep[2 * j].map =
1244 isl_map_union_disjoint(res->dep[2 * j].map,
1245 must_rel[j]);
1246 res->dep[2 * j + 1].map =
1247 isl_map_union_disjoint(res->dep[2 * j + 1].map,
1248 may_rel[j]);
1251 if (isl_set_plain_is_empty(mustdo) &&
1252 isl_set_plain_is_empty(maydo))
1253 break;
1256 free(must_rel);
1257 free(may_rel);
1258 done:
1259 res->must_no_source = mustdo;
1260 res->may_no_source = maydo;
1261 return res;
1262 error:
1263 if (must_rel)
1264 for (j = 0; j < acc->n_must; ++j)
1265 isl_map_free(must_rel[j]);
1266 if (may_rel)
1267 for (j = 0; j < acc->n_must; ++j)
1268 isl_map_free(may_rel[j]);
1269 isl_flow_free(res);
1270 isl_set_free(mustdo);
1271 isl_set_free(maydo);
1272 free(must_rel);
1273 free(may_rel);
1274 return NULL;
1277 /* Given a "sink" access, a list of n "source" accesses,
1278 * compute for each iteration of the sink access
1279 * and for each element accessed by that iteration,
1280 * the source access in the list that last accessed the
1281 * element accessed by the sink access before this sink access.
1282 * Each access is given as a map from the loop iterators
1283 * to the array indices.
1284 * The result is a list of n relations between source and sink
1285 * iterations and a subset of the domain of the sink access,
1286 * corresponding to those iterations that access an element
1287 * not previously accessed.
1289 * To deal with multi-valued sink access relations, the sink iteration
1290 * domain is first extended with dimensions that correspond to the data
1291 * space. However, these extra dimensions are not projected out again.
1292 * It is up to the caller to decide whether these dimensions should be kept.
1294 static __isl_give isl_flow *access_info_compute_flow_core(
1295 __isl_take isl_access_info *acc)
1297 struct isl_flow *res = NULL;
1299 if (!acc)
1300 return NULL;
1302 acc->sink.map = isl_map_range_map(acc->sink.map);
1303 if (!acc->sink.map)
1304 goto error;
1306 if (acc->n_must == 0)
1307 res = compute_mem_based_dependences(acc);
1308 else {
1309 acc = isl_access_info_sort_sources(acc);
1310 res = compute_val_based_dependences(acc);
1312 acc = isl_access_info_free(acc);
1313 if (!res)
1314 return NULL;
1315 if (!res->must_no_source || !res->may_no_source)
1316 goto error;
1317 return res;
1318 error:
1319 isl_access_info_free(acc);
1320 isl_flow_free(res);
1321 return NULL;
1324 /* Given a "sink" access, a list of n "source" accesses,
1325 * compute for each iteration of the sink access
1326 * and for each element accessed by that iteration,
1327 * the source access in the list that last accessed the
1328 * element accessed by the sink access before this sink access.
1329 * Each access is given as a map from the loop iterators
1330 * to the array indices.
1331 * The result is a list of n relations between source and sink
1332 * iterations and a subset of the domain of the sink access,
1333 * corresponding to those iterations that access an element
1334 * not previously accessed.
1336 * To deal with multi-valued sink access relations,
1337 * access_info_compute_flow_core extends the sink iteration domain
1338 * with dimensions that correspond to the data space. These extra dimensions
1339 * are projected out from the result of access_info_compute_flow_core.
1341 __isl_give isl_flow *isl_access_info_compute_flow(__isl_take isl_access_info *acc)
1343 int j;
1344 struct isl_flow *res;
1346 if (!acc)
1347 return NULL;
1349 acc->domain_map = isl_map_domain_map(isl_map_copy(acc->sink.map));
1350 res = access_info_compute_flow_core(acc);
1351 if (!res)
1352 return NULL;
1354 for (j = 0; j < res->n_source; ++j) {
1355 res->dep[j].map = isl_map_range_factor_domain(res->dep[j].map);
1356 if (!res->dep[j].map)
1357 goto error;
1360 return res;
1361 error:
1362 isl_flow_free(res);
1363 return NULL;
1367 /* Keep track of some information about a schedule for a given
1368 * access. In particular, keep track of which dimensions
1369 * have a constant value and of the actual constant values.
1371 struct isl_sched_info {
1372 int *is_cst;
1373 isl_vec *cst;
1376 static void sched_info_free(__isl_take struct isl_sched_info *info)
1378 if (!info)
1379 return;
1380 isl_vec_free(info->cst);
1381 free(info->is_cst);
1382 free(info);
1385 /* Extract information on the constant dimensions of the schedule
1386 * for a given access. The "map" is of the form
1388 * [S -> D] -> A
1390 * with S the schedule domain, D the iteration domain and A the data domain.
1392 static __isl_give struct isl_sched_info *sched_info_alloc(
1393 __isl_keep isl_map *map)
1395 isl_ctx *ctx;
1396 isl_space *dim;
1397 struct isl_sched_info *info;
1398 int i, n;
1400 if (!map)
1401 return NULL;
1403 dim = isl_space_unwrap(isl_space_domain(isl_map_get_space(map)));
1404 if (!dim)
1405 return NULL;
1406 n = isl_space_dim(dim, isl_dim_in);
1407 isl_space_free(dim);
1409 ctx = isl_map_get_ctx(map);
1410 info = isl_alloc_type(ctx, struct isl_sched_info);
1411 if (!info)
1412 return NULL;
1413 info->is_cst = isl_alloc_array(ctx, int, n);
1414 info->cst = isl_vec_alloc(ctx, n);
1415 if (n && (!info->is_cst || !info->cst))
1416 goto error;
1418 for (i = 0; i < n; ++i) {
1419 isl_val *v;
1421 v = isl_map_plain_get_val_if_fixed(map, isl_dim_in, i);
1422 if (!v)
1423 goto error;
1424 info->is_cst[i] = !isl_val_is_nan(v);
1425 if (info->is_cst[i])
1426 info->cst = isl_vec_set_element_val(info->cst, i, v);
1427 else
1428 isl_val_free(v);
1431 return info;
1432 error:
1433 sched_info_free(info);
1434 return NULL;
1437 /* The different types of access relations that isl_union_access_info
1438 * keeps track of.
1440 * "isl_access_sink" represents the sink accesses.
1441 * "isl_access_must_source" represents the definite source accesses.
1442 * "isl_access_may_source" represents the possible source accesses.
1443 * "isl_access_kill" represents the kills.
1445 * isl_access_sink is sometimes treated differently and
1446 * should therefore appear first.
1448 enum isl_access_type {
1449 isl_access_sink,
1450 isl_access_must_source,
1451 isl_access_may_source,
1452 isl_access_kill,
1453 isl_access_end
1456 /* This structure represents the input for a dependence analysis computation.
1458 * "access" contains the access relations.
1460 * "schedule" or "schedule_map" represents the execution order.
1461 * Exactly one of these fields should be NULL. The other field
1462 * determines the execution order.
1464 * The domains of these four maps refer to the same iteration spaces(s).
1465 * The ranges of the first three maps also refer to the same data space(s).
1467 * After a call to isl_union_access_info_introduce_schedule,
1468 * the "schedule_map" field no longer contains useful information.
1470 struct isl_union_access_info {
1471 isl_union_map *access[isl_access_end];
1473 isl_schedule *schedule;
1474 isl_union_map *schedule_map;
1477 /* Free "access" and return NULL.
1479 __isl_null isl_union_access_info *isl_union_access_info_free(
1480 __isl_take isl_union_access_info *access)
1482 enum isl_access_type i;
1484 if (!access)
1485 return NULL;
1487 for (i = isl_access_sink; i < isl_access_end; ++i)
1488 isl_union_map_free(access->access[i]);
1489 isl_schedule_free(access->schedule);
1490 isl_union_map_free(access->schedule_map);
1491 free(access);
1493 return NULL;
1496 /* Return the isl_ctx to which "access" belongs.
1498 isl_ctx *isl_union_access_info_get_ctx(__isl_keep isl_union_access_info *access)
1500 if (!access)
1501 return NULL;
1502 return isl_union_map_get_ctx(access->access[isl_access_sink]);
1505 /* Construct an empty (invalid) isl_union_access_info object.
1506 * The caller is responsible for setting the sink access relation and
1507 * initializing all the other fields, e.g., by calling
1508 * isl_union_access_info_init.
1510 static __isl_give isl_union_access_info *isl_union_access_info_alloc(
1511 isl_ctx *ctx)
1513 return isl_calloc_type(ctx, isl_union_access_info);
1516 /* Initialize all the fields of "info", except the sink access relation,
1517 * which is assumed to have been set by the caller.
1519 * By default, we use the schedule field of the isl_union_access_info,
1520 * but this may be overridden by a call
1521 * to isl_union_access_info_set_schedule_map.
1523 static __isl_give isl_union_access_info *isl_union_access_info_init(
1524 __isl_take isl_union_access_info *info)
1526 isl_space *space;
1527 isl_union_map *empty;
1528 enum isl_access_type i;
1530 if (!info)
1531 return NULL;
1532 if (!info->access[isl_access_sink])
1533 return isl_union_access_info_free(info);
1535 space = isl_union_map_get_space(info->access[isl_access_sink]);
1536 empty = isl_union_map_empty(isl_space_copy(space));
1537 for (i = isl_access_sink + 1; i < isl_access_end; ++i)
1538 if (!info->access[i])
1539 info->access[i] = isl_union_map_copy(empty);
1540 isl_union_map_free(empty);
1541 if (!info->schedule && !info->schedule_map)
1542 info->schedule = isl_schedule_empty(isl_space_copy(space));
1543 isl_space_free(space);
1545 for (i = isl_access_sink + 1; i < isl_access_end; ++i)
1546 if (!info->access[i])
1547 return isl_union_access_info_free(info);
1548 if (!info->schedule && !info->schedule_map)
1549 return isl_union_access_info_free(info);
1551 return info;
1554 /* Create a new isl_union_access_info with the given sink accesses and
1555 * and no other accesses or schedule information.
1557 __isl_give isl_union_access_info *isl_union_access_info_from_sink(
1558 __isl_take isl_union_map *sink)
1560 isl_ctx *ctx;
1561 isl_union_access_info *access;
1563 if (!sink)
1564 return NULL;
1565 ctx = isl_union_map_get_ctx(sink);
1566 access = isl_union_access_info_alloc(ctx);
1567 if (!access)
1568 goto error;
1569 access->access[isl_access_sink] = sink;
1570 return isl_union_access_info_init(access);
1571 error:
1572 isl_union_map_free(sink);
1573 return NULL;
1576 /* Replace the access relation of type "type" of "info" by "access".
1578 static __isl_give isl_union_access_info *isl_union_access_info_set(
1579 __isl_take isl_union_access_info *info,
1580 enum isl_access_type type, __isl_take isl_union_map *access)
1582 if (!info || !access)
1583 goto error;
1585 isl_union_map_free(info->access[type]);
1586 info->access[type] = access;
1588 return info;
1589 error:
1590 isl_union_access_info_free(info);
1591 isl_union_map_free(access);
1592 return NULL;
1595 /* Replace the definite source accesses of "access" by "must_source".
1597 __isl_give isl_union_access_info *isl_union_access_info_set_must_source(
1598 __isl_take isl_union_access_info *access,
1599 __isl_take isl_union_map *must_source)
1601 return isl_union_access_info_set(access, isl_access_must_source,
1602 must_source);
1605 /* Replace the possible source accesses of "access" by "may_source".
1607 __isl_give isl_union_access_info *isl_union_access_info_set_may_source(
1608 __isl_take isl_union_access_info *access,
1609 __isl_take isl_union_map *may_source)
1611 return isl_union_access_info_set(access, isl_access_may_source,
1612 may_source);
1615 /* Replace the kills of "info" by "kill".
1617 __isl_give isl_union_access_info *isl_union_access_info_set_kill(
1618 __isl_take isl_union_access_info *info, __isl_take isl_union_map *kill)
1620 return isl_union_access_info_set(info, isl_access_kill, kill);
1623 /* Return the access relation of type "type" of "info".
1625 static __isl_give isl_union_map *isl_union_access_info_get(
1626 __isl_keep isl_union_access_info *info, enum isl_access_type type)
1628 if (!info)
1629 return NULL;
1630 return isl_union_map_copy(info->access[type]);
1633 /* Return the definite source accesses of "info".
1635 __isl_give isl_union_map *isl_union_access_info_get_must_source(
1636 __isl_keep isl_union_access_info *info)
1638 return isl_union_access_info_get(info, isl_access_must_source);
1641 /* Return the possible source accesses of "info".
1643 __isl_give isl_union_map *isl_union_access_info_get_may_source(
1644 __isl_keep isl_union_access_info *info)
1646 return isl_union_access_info_get(info, isl_access_may_source);
1649 /* Return the kills of "info".
1651 __isl_give isl_union_map *isl_union_access_info_get_kill(
1652 __isl_keep isl_union_access_info *info)
1654 return isl_union_access_info_get(info, isl_access_kill);
1657 /* Does "info" specify any kills?
1659 static isl_bool isl_union_access_has_kill(
1660 __isl_keep isl_union_access_info *info)
1662 isl_bool empty;
1664 if (!info)
1665 return isl_bool_error;
1666 empty = isl_union_map_is_empty(info->access[isl_access_kill]);
1667 return isl_bool_not(empty);
1670 /* Replace the schedule of "access" by "schedule".
1671 * Also free the schedule_map in case it was set last.
1673 __isl_give isl_union_access_info *isl_union_access_info_set_schedule(
1674 __isl_take isl_union_access_info *access,
1675 __isl_take isl_schedule *schedule)
1677 if (!access || !schedule)
1678 goto error;
1680 access->schedule_map = isl_union_map_free(access->schedule_map);
1681 isl_schedule_free(access->schedule);
1682 access->schedule = schedule;
1684 return access;
1685 error:
1686 isl_union_access_info_free(access);
1687 isl_schedule_free(schedule);
1688 return NULL;
1691 /* Replace the schedule map of "access" by "schedule_map".
1692 * Also free the schedule in case it was set last.
1694 __isl_give isl_union_access_info *isl_union_access_info_set_schedule_map(
1695 __isl_take isl_union_access_info *access,
1696 __isl_take isl_union_map *schedule_map)
1698 if (!access || !schedule_map)
1699 goto error;
1701 isl_union_map_free(access->schedule_map);
1702 access->schedule = isl_schedule_free(access->schedule);
1703 access->schedule_map = schedule_map;
1705 return access;
1706 error:
1707 isl_union_access_info_free(access);
1708 isl_union_map_free(schedule_map);
1709 return NULL;
1712 __isl_give isl_union_access_info *isl_union_access_info_copy(
1713 __isl_keep isl_union_access_info *access)
1715 isl_union_access_info *copy;
1716 enum isl_access_type i;
1718 if (!access)
1719 return NULL;
1720 copy = isl_union_access_info_from_sink(
1721 isl_union_map_copy(access->access[isl_access_sink]));
1722 for (i = isl_access_sink + 1; i < isl_access_end; ++i)
1723 copy = isl_union_access_info_set(copy, i,
1724 isl_union_map_copy(access->access[i]));
1725 if (access->schedule)
1726 copy = isl_union_access_info_set_schedule(copy,
1727 isl_schedule_copy(access->schedule));
1728 else
1729 copy = isl_union_access_info_set_schedule_map(copy,
1730 isl_union_map_copy(access->schedule_map));
1732 return copy;
1735 /* Print a key-value pair of a YAML mapping to "p",
1736 * with key "name" and value "umap".
1738 static __isl_give isl_printer *print_union_map_field(__isl_take isl_printer *p,
1739 const char *name, __isl_keep isl_union_map *umap)
1741 p = isl_printer_print_str(p, name);
1742 p = isl_printer_yaml_next(p);
1743 p = isl_printer_print_str(p, "\"");
1744 p = isl_printer_print_union_map(p, umap);
1745 p = isl_printer_print_str(p, "\"");
1746 p = isl_printer_yaml_next(p);
1748 return p;
1751 /* An enumeration of the various keys that may appear in a YAML mapping
1752 * of an isl_union_access_info object.
1753 * The keys for the access relation types are assumed to have the same values
1754 * as the access relation types in isl_access_type.
1756 enum isl_ai_key {
1757 isl_ai_key_error = -1,
1758 isl_ai_key_sink = isl_access_sink,
1759 isl_ai_key_must_source = isl_access_must_source,
1760 isl_ai_key_may_source = isl_access_may_source,
1761 isl_ai_key_kill = isl_access_kill,
1762 isl_ai_key_schedule_map,
1763 isl_ai_key_schedule,
1764 isl_ai_key_end
1767 /* Textual representations of the YAML keys for an isl_union_access_info
1768 * object.
1770 static char *key_str[] = {
1771 [isl_ai_key_sink] = "sink",
1772 [isl_ai_key_must_source] = "must_source",
1773 [isl_ai_key_may_source] = "may_source",
1774 [isl_ai_key_kill] = "kill",
1775 [isl_ai_key_schedule_map] = "schedule_map",
1776 [isl_ai_key_schedule] = "schedule",
1779 /* Print a key-value pair corresponding to the access relation of type "type"
1780 * of a YAML mapping of "info" to "p".
1782 * The sink access relation is always printed, but any other access relation
1783 * is only printed if it is non-empty.
1785 static __isl_give isl_printer *print_access_field(__isl_take isl_printer *p,
1786 __isl_keep isl_union_access_info *info, enum isl_access_type type)
1788 if (type != isl_access_sink) {
1789 isl_bool empty;
1791 empty = isl_union_map_is_empty(info->access[type]);
1792 if (empty < 0)
1793 return isl_printer_free(p);
1794 if (empty)
1795 return p;
1797 return print_union_map_field(p, key_str[type], info->access[type]);
1800 /* Print the information contained in "access" to "p".
1801 * The information is printed as a YAML document.
1803 __isl_give isl_printer *isl_printer_print_union_access_info(
1804 __isl_take isl_printer *p, __isl_keep isl_union_access_info *access)
1806 enum isl_access_type i;
1808 if (!access)
1809 return isl_printer_free(p);
1811 p = isl_printer_yaml_start_mapping(p);
1812 for (i = isl_access_sink; i < isl_access_end; ++i)
1813 p = print_access_field(p, access, i);
1814 if (access->schedule) {
1815 p = isl_printer_print_str(p, key_str[isl_ai_key_schedule]);
1816 p = isl_printer_yaml_next(p);
1817 p = isl_printer_print_schedule(p, access->schedule);
1818 p = isl_printer_yaml_next(p);
1819 } else {
1820 p = print_union_map_field(p, key_str[isl_ai_key_schedule_map],
1821 access->schedule_map);
1823 p = isl_printer_yaml_end_mapping(p);
1825 return p;
1828 /* Return a string representation of the information in "access".
1829 * The information is printed in flow format.
1831 __isl_give char *isl_union_access_info_to_str(
1832 __isl_keep isl_union_access_info *access)
1834 isl_printer *p;
1835 char *s;
1837 if (!access)
1838 return NULL;
1840 p = isl_printer_to_str(isl_union_access_info_get_ctx(access));
1841 p = isl_printer_set_yaml_style(p, ISL_YAML_STYLE_FLOW);
1842 p = isl_printer_print_union_access_info(p, access);
1843 s = isl_printer_get_str(p);
1844 isl_printer_free(p);
1846 return s;
1849 #undef KEY
1850 #define KEY enum isl_ai_key
1851 #undef KEY_ERROR
1852 #define KEY_ERROR isl_ai_key_error
1853 #undef KEY_END
1854 #define KEY_END isl_ai_key_end
1855 #include "extract_key.c"
1857 #undef BASE
1858 #define BASE union_map
1859 #include "read_in_string_templ.c"
1861 /* Read an isl_union_access_info object from "s".
1863 * Start off with an empty (invalid) isl_union_access_info object and
1864 * then fill up the fields based on the input.
1865 * The input needs to contain at least a description of the sink
1866 * access relation as well as some form of schedule.
1867 * The other access relations are set to empty relations
1868 * by isl_union_access_info_init if they are not specified in the input.
1870 __isl_give isl_union_access_info *isl_stream_read_union_access_info(
1871 isl_stream *s)
1873 isl_ctx *ctx;
1874 isl_union_access_info *info;
1875 int more;
1876 int sink_set = 0;
1877 int schedule_set = 0;
1879 if (isl_stream_yaml_read_start_mapping(s))
1880 return NULL;
1882 ctx = isl_stream_get_ctx(s);
1883 info = isl_union_access_info_alloc(ctx);
1884 while ((more = isl_stream_yaml_next(s)) > 0) {
1885 enum isl_ai_key key;
1886 isl_union_map *access, *schedule_map;
1887 isl_schedule *schedule;
1889 key = get_key(s);
1890 if (isl_stream_yaml_next(s) < 0)
1891 return isl_union_access_info_free(info);
1892 switch (key) {
1893 case isl_ai_key_end:
1894 case isl_ai_key_error:
1895 return isl_union_access_info_free(info);
1896 case isl_ai_key_sink:
1897 sink_set = 1;
1898 case isl_ai_key_must_source:
1899 case isl_ai_key_may_source:
1900 case isl_ai_key_kill:
1901 access = read_union_map(s);
1902 info = isl_union_access_info_set(info, key, access);
1903 if (!info)
1904 return NULL;
1905 break;
1906 case isl_ai_key_schedule_map:
1907 schedule_set = 1;
1908 schedule_map = read_union_map(s);
1909 info = isl_union_access_info_set_schedule_map(info,
1910 schedule_map);
1911 if (!info)
1912 return NULL;
1913 break;
1914 case isl_ai_key_schedule:
1915 schedule_set = 1;
1916 schedule = isl_stream_read_schedule(s);
1917 info = isl_union_access_info_set_schedule(info,
1918 schedule);
1919 if (!info)
1920 return NULL;
1921 break;
1924 if (more < 0)
1925 return isl_union_access_info_free(info);
1927 if (isl_stream_yaml_read_end_mapping(s) < 0) {
1928 isl_stream_error(s, NULL, "unexpected extra elements");
1929 return isl_union_access_info_free(info);
1932 if (!sink_set) {
1933 isl_stream_error(s, NULL, "no sink specified");
1934 return isl_union_access_info_free(info);
1937 if (!schedule_set) {
1938 isl_stream_error(s, NULL, "no schedule specified");
1939 return isl_union_access_info_free(info);
1942 return isl_union_access_info_init(info);
1945 /* Read an isl_union_access_info object from the file "input".
1947 __isl_give isl_union_access_info *isl_union_access_info_read_from_file(
1948 isl_ctx *ctx, FILE *input)
1950 isl_stream *s;
1951 isl_union_access_info *access;
1953 s = isl_stream_new_file(ctx, input);
1954 if (!s)
1955 return NULL;
1956 access = isl_stream_read_union_access_info(s);
1957 isl_stream_free(s);
1959 return access;
1962 /* Update the fields of "access" such that they all have the same parameters,
1963 * keeping in mind that the schedule_map field may be NULL and ignoring
1964 * the schedule field.
1966 static __isl_give isl_union_access_info *isl_union_access_info_align_params(
1967 __isl_take isl_union_access_info *access)
1969 isl_space *space;
1970 enum isl_access_type i;
1972 if (!access)
1973 return NULL;
1975 space = isl_union_map_get_space(access->access[isl_access_sink]);
1976 for (i = isl_access_sink + 1; i < isl_access_end; ++i)
1977 space = isl_space_align_params(space,
1978 isl_union_map_get_space(access->access[i]));
1979 if (access->schedule_map)
1980 space = isl_space_align_params(space,
1981 isl_union_map_get_space(access->schedule_map));
1982 for (i = isl_access_sink; i < isl_access_end; ++i)
1983 access->access[i] =
1984 isl_union_map_align_params(access->access[i],
1985 isl_space_copy(space));
1986 if (!access->schedule_map) {
1987 isl_space_free(space);
1988 } else {
1989 access->schedule_map =
1990 isl_union_map_align_params(access->schedule_map, space);
1991 if (!access->schedule_map)
1992 return isl_union_access_info_free(access);
1995 for (i = isl_access_sink; i < isl_access_end; ++i)
1996 if (!access->access[i])
1997 return isl_union_access_info_free(access);
1999 return access;
2002 /* Prepend the schedule dimensions to the iteration domains.
2004 * That is, if the schedule is of the form
2006 * D -> S
2008 * while the access relations are of the form
2010 * D -> A
2012 * then the updated access relations are of the form
2014 * [S -> D] -> A
2016 * The schedule map is also replaced by the map
2018 * [S -> D] -> D
2020 * that is used during the internal computation.
2021 * Neither the original schedule map nor this updated schedule map
2022 * are used after the call to this function.
2024 static __isl_give isl_union_access_info *
2025 isl_union_access_info_introduce_schedule(
2026 __isl_take isl_union_access_info *access)
2028 isl_union_map *sm;
2029 enum isl_access_type i;
2031 if (!access)
2032 return NULL;
2034 sm = isl_union_map_reverse(access->schedule_map);
2035 sm = isl_union_map_range_map(sm);
2036 for (i = isl_access_sink; i < isl_access_end; ++i)
2037 access->access[i] =
2038 isl_union_map_apply_range(isl_union_map_copy(sm),
2039 access->access[i]);
2040 access->schedule_map = sm;
2042 for (i = isl_access_sink; i < isl_access_end; ++i)
2043 if (!access->access[i])
2044 return isl_union_access_info_free(access);
2045 if (!access->schedule_map)
2046 return isl_union_access_info_free(access);
2048 return access;
2051 /* This structure represents the result of a dependence analysis computation.
2053 * "must_dep" represents the full definite dependences
2054 * "may_dep" represents the full non-definite dependences.
2055 * Both are of the form
2057 * [Source] -> [[Sink -> Data]]
2059 * (after the schedule dimensions have been projected out).
2060 * "must_no_source" represents the subset of the sink accesses for which
2061 * definitely no source was found.
2062 * "may_no_source" represents the subset of the sink accesses for which
2063 * possibly, but not definitely, no source was found.
2065 struct isl_union_flow {
2066 isl_union_map *must_dep;
2067 isl_union_map *may_dep;
2068 isl_union_map *must_no_source;
2069 isl_union_map *may_no_source;
2072 /* Return the isl_ctx to which "flow" belongs.
2074 isl_ctx *isl_union_flow_get_ctx(__isl_keep isl_union_flow *flow)
2076 return flow ? isl_union_map_get_ctx(flow->must_dep) : NULL;
2079 /* Free "flow" and return NULL.
2081 __isl_null isl_union_flow *isl_union_flow_free(__isl_take isl_union_flow *flow)
2083 if (!flow)
2084 return NULL;
2085 isl_union_map_free(flow->must_dep);
2086 isl_union_map_free(flow->may_dep);
2087 isl_union_map_free(flow->must_no_source);
2088 isl_union_map_free(flow->may_no_source);
2089 free(flow);
2090 return NULL;
2093 void isl_union_flow_dump(__isl_keep isl_union_flow *flow)
2095 if (!flow)
2096 return;
2098 fprintf(stderr, "must dependences: ");
2099 isl_union_map_dump(flow->must_dep);
2100 fprintf(stderr, "may dependences: ");
2101 isl_union_map_dump(flow->may_dep);
2102 fprintf(stderr, "must no source: ");
2103 isl_union_map_dump(flow->must_no_source);
2104 fprintf(stderr, "may no source: ");
2105 isl_union_map_dump(flow->may_no_source);
2108 /* Return the full definite dependences in "flow", with accessed elements.
2110 __isl_give isl_union_map *isl_union_flow_get_full_must_dependence(
2111 __isl_keep isl_union_flow *flow)
2113 if (!flow)
2114 return NULL;
2115 return isl_union_map_copy(flow->must_dep);
2118 /* Return the full possible dependences in "flow", including the definite
2119 * dependences, with accessed elements.
2121 __isl_give isl_union_map *isl_union_flow_get_full_may_dependence(
2122 __isl_keep isl_union_flow *flow)
2124 if (!flow)
2125 return NULL;
2126 return isl_union_map_union(isl_union_map_copy(flow->must_dep),
2127 isl_union_map_copy(flow->may_dep));
2130 /* Return the definite dependences in "flow", without the accessed elements.
2132 __isl_give isl_union_map *isl_union_flow_get_must_dependence(
2133 __isl_keep isl_union_flow *flow)
2135 isl_union_map *dep;
2137 if (!flow)
2138 return NULL;
2139 dep = isl_union_map_copy(flow->must_dep);
2140 return isl_union_map_range_factor_domain(dep);
2143 /* Return the possible dependences in "flow", including the definite
2144 * dependences, without the accessed elements.
2146 __isl_give isl_union_map *isl_union_flow_get_may_dependence(
2147 __isl_keep isl_union_flow *flow)
2149 isl_union_map *dep;
2151 if (!flow)
2152 return NULL;
2153 dep = isl_union_map_union(isl_union_map_copy(flow->must_dep),
2154 isl_union_map_copy(flow->may_dep));
2155 return isl_union_map_range_factor_domain(dep);
2158 /* Return the non-definite dependences in "flow".
2160 static __isl_give isl_union_map *isl_union_flow_get_non_must_dependence(
2161 __isl_keep isl_union_flow *flow)
2163 if (!flow)
2164 return NULL;
2165 return isl_union_map_copy(flow->may_dep);
2168 /* Return the subset of the sink accesses for which definitely
2169 * no source was found.
2171 __isl_give isl_union_map *isl_union_flow_get_must_no_source(
2172 __isl_keep isl_union_flow *flow)
2174 if (!flow)
2175 return NULL;
2176 return isl_union_map_copy(flow->must_no_source);
2179 /* Return the subset of the sink accesses for which possibly
2180 * no source was found, including those for which definitely
2181 * no source was found.
2183 __isl_give isl_union_map *isl_union_flow_get_may_no_source(
2184 __isl_keep isl_union_flow *flow)
2186 if (!flow)
2187 return NULL;
2188 return isl_union_map_union(isl_union_map_copy(flow->must_no_source),
2189 isl_union_map_copy(flow->may_no_source));
2192 /* Return the subset of the sink accesses for which possibly, but not
2193 * definitely, no source was found.
2195 static __isl_give isl_union_map *isl_union_flow_get_non_must_no_source(
2196 __isl_keep isl_union_flow *flow)
2198 if (!flow)
2199 return NULL;
2200 return isl_union_map_copy(flow->may_no_source);
2203 /* Create a new isl_union_flow object, initialized with empty
2204 * dependence relations and sink subsets.
2206 static __isl_give isl_union_flow *isl_union_flow_alloc(
2207 __isl_take isl_space *space)
2209 isl_ctx *ctx;
2210 isl_union_map *empty;
2211 isl_union_flow *flow;
2213 if (!space)
2214 return NULL;
2215 ctx = isl_space_get_ctx(space);
2216 flow = isl_alloc_type(ctx, isl_union_flow);
2217 if (!flow)
2218 goto error;
2220 empty = isl_union_map_empty(space);
2221 flow->must_dep = isl_union_map_copy(empty);
2222 flow->may_dep = isl_union_map_copy(empty);
2223 flow->must_no_source = isl_union_map_copy(empty);
2224 flow->may_no_source = empty;
2226 if (!flow->must_dep || !flow->may_dep ||
2227 !flow->must_no_source || !flow->may_no_source)
2228 return isl_union_flow_free(flow);
2230 return flow;
2231 error:
2232 isl_space_free(space);
2233 return NULL;
2236 /* Copy this isl_union_flow object.
2238 __isl_give isl_union_flow *isl_union_flow_copy(__isl_keep isl_union_flow *flow)
2240 isl_union_flow *copy;
2242 if (!flow)
2243 return NULL;
2245 copy = isl_union_flow_alloc(isl_union_map_get_space(flow->must_dep));
2247 if (!copy)
2248 return NULL;
2250 copy->must_dep = isl_union_map_union(copy->must_dep,
2251 isl_union_map_copy(flow->must_dep));
2252 copy->may_dep = isl_union_map_union(copy->may_dep,
2253 isl_union_map_copy(flow->may_dep));
2254 copy->must_no_source = isl_union_map_union(copy->must_no_source,
2255 isl_union_map_copy(flow->must_no_source));
2256 copy->may_no_source = isl_union_map_union(copy->may_no_source,
2257 isl_union_map_copy(flow->may_no_source));
2259 if (!copy->must_dep || !copy->may_dep ||
2260 !copy->must_no_source || !copy->may_no_source)
2261 return isl_union_flow_free(copy);
2263 return copy;
2266 /* Drop the schedule dimensions from the iteration domains in "flow".
2267 * In particular, the schedule dimensions have been prepended
2268 * to the iteration domains prior to the dependence analysis by
2269 * replacing the iteration domain D, by the wrapped map [S -> D].
2270 * Replace these wrapped maps by the original D.
2272 * In particular, the dependences computed by access_info_compute_flow_core
2273 * are of the form
2275 * [S -> D] -> [[S' -> D'] -> A]
2277 * The schedule dimensions are projected out by first currying the range,
2278 * resulting in
2280 * [S -> D] -> [S' -> [D' -> A]]
2282 * and then computing the factor range
2284 * D -> [D' -> A]
2286 static __isl_give isl_union_flow *isl_union_flow_drop_schedule(
2287 __isl_take isl_union_flow *flow)
2289 if (!flow)
2290 return NULL;
2292 flow->must_dep = isl_union_map_range_curry(flow->must_dep);
2293 flow->must_dep = isl_union_map_factor_range(flow->must_dep);
2294 flow->may_dep = isl_union_map_range_curry(flow->may_dep);
2295 flow->may_dep = isl_union_map_factor_range(flow->may_dep);
2296 flow->must_no_source =
2297 isl_union_map_domain_factor_range(flow->must_no_source);
2298 flow->may_no_source =
2299 isl_union_map_domain_factor_range(flow->may_no_source);
2301 if (!flow->must_dep || !flow->may_dep ||
2302 !flow->must_no_source || !flow->may_no_source)
2303 return isl_union_flow_free(flow);
2305 return flow;
2308 struct isl_compute_flow_data {
2309 isl_union_map *must_source;
2310 isl_union_map *may_source;
2311 isl_union_flow *flow;
2313 int count;
2314 int must;
2315 isl_space *dim;
2316 struct isl_sched_info *sink_info;
2317 struct isl_sched_info **source_info;
2318 isl_access_info *accesses;
2321 static isl_stat count_matching_array(__isl_take isl_map *map, void *user)
2323 int eq;
2324 isl_space *dim;
2325 struct isl_compute_flow_data *data;
2327 data = (struct isl_compute_flow_data *)user;
2329 dim = isl_space_range(isl_map_get_space(map));
2331 eq = isl_space_is_equal(dim, data->dim);
2333 isl_space_free(dim);
2334 isl_map_free(map);
2336 if (eq < 0)
2337 return isl_stat_error;
2338 if (eq)
2339 data->count++;
2341 return isl_stat_ok;
2344 static isl_stat collect_matching_array(__isl_take isl_map *map, void *user)
2346 int eq;
2347 isl_space *dim;
2348 struct isl_sched_info *info;
2349 struct isl_compute_flow_data *data;
2351 data = (struct isl_compute_flow_data *)user;
2353 dim = isl_space_range(isl_map_get_space(map));
2355 eq = isl_space_is_equal(dim, data->dim);
2357 isl_space_free(dim);
2359 if (eq < 0)
2360 goto error;
2361 if (!eq) {
2362 isl_map_free(map);
2363 return isl_stat_ok;
2366 info = sched_info_alloc(map);
2367 data->source_info[data->count] = info;
2369 data->accesses = isl_access_info_add_source(data->accesses,
2370 map, data->must, info);
2372 data->count++;
2374 return isl_stat_ok;
2375 error:
2376 isl_map_free(map);
2377 return isl_stat_error;
2380 /* Determine the shared nesting level and the "textual order" of
2381 * the given accesses.
2383 * We first determine the minimal schedule dimension for both accesses.
2385 * If among those dimensions, we can find one where both have a fixed
2386 * value and if moreover those values are different, then the previous
2387 * dimension is the last shared nesting level and the textual order
2388 * is determined based on the order of the fixed values.
2389 * If no such fixed values can be found, then we set the shared
2390 * nesting level to the minimal schedule dimension, with no textual ordering.
2392 static int before(void *first, void *second)
2394 struct isl_sched_info *info1 = first;
2395 struct isl_sched_info *info2 = second;
2396 int n1, n2;
2397 int i;
2399 n1 = isl_vec_size(info1->cst);
2400 n2 = isl_vec_size(info2->cst);
2402 if (n2 < n1)
2403 n1 = n2;
2405 for (i = 0; i < n1; ++i) {
2406 int r;
2407 int cmp;
2409 if (!info1->is_cst[i])
2410 continue;
2411 if (!info2->is_cst[i])
2412 continue;
2413 cmp = isl_vec_cmp_element(info1->cst, info2->cst, i);
2414 if (cmp == 0)
2415 continue;
2417 r = 2 * i + (cmp < 0);
2419 return r;
2422 return 2 * n1;
2425 /* Check if the given two accesses may be coscheduled.
2426 * If so, return 1. Otherwise return 0.
2428 * Two accesses may only be coscheduled if the fixed schedule
2429 * coordinates have the same values.
2431 static int coscheduled(void *first, void *second)
2433 struct isl_sched_info *info1 = first;
2434 struct isl_sched_info *info2 = second;
2435 int n1, n2;
2436 int i;
2438 n1 = isl_vec_size(info1->cst);
2439 n2 = isl_vec_size(info2->cst);
2441 if (n2 < n1)
2442 n1 = n2;
2444 for (i = 0; i < n1; ++i) {
2445 int cmp;
2447 if (!info1->is_cst[i])
2448 continue;
2449 if (!info2->is_cst[i])
2450 continue;
2451 cmp = isl_vec_cmp_element(info1->cst, info2->cst, i);
2452 if (cmp != 0)
2453 return 0;
2456 return 1;
2459 /* Given a sink access, look for all the source accesses that access
2460 * the same array and perform dataflow analysis on them using
2461 * isl_access_info_compute_flow_core.
2463 static isl_stat compute_flow(__isl_take isl_map *map, void *user)
2465 int i;
2466 isl_ctx *ctx;
2467 struct isl_compute_flow_data *data;
2468 isl_flow *flow;
2469 isl_union_flow *df;
2471 data = (struct isl_compute_flow_data *)user;
2472 df = data->flow;
2474 ctx = isl_map_get_ctx(map);
2476 data->accesses = NULL;
2477 data->sink_info = NULL;
2478 data->source_info = NULL;
2479 data->count = 0;
2480 data->dim = isl_space_range(isl_map_get_space(map));
2482 if (isl_union_map_foreach_map(data->must_source,
2483 &count_matching_array, data) < 0)
2484 goto error;
2485 if (isl_union_map_foreach_map(data->may_source,
2486 &count_matching_array, data) < 0)
2487 goto error;
2489 data->sink_info = sched_info_alloc(map);
2490 data->source_info = isl_calloc_array(ctx, struct isl_sched_info *,
2491 data->count);
2493 data->accesses = isl_access_info_alloc(isl_map_copy(map),
2494 data->sink_info, &before, data->count);
2495 if (!data->sink_info || (data->count && !data->source_info) ||
2496 !data->accesses)
2497 goto error;
2498 data->accesses->coscheduled = &coscheduled;
2499 data->count = 0;
2500 data->must = 1;
2501 if (isl_union_map_foreach_map(data->must_source,
2502 &collect_matching_array, data) < 0)
2503 goto error;
2504 data->must = 0;
2505 if (isl_union_map_foreach_map(data->may_source,
2506 &collect_matching_array, data) < 0)
2507 goto error;
2509 flow = access_info_compute_flow_core(data->accesses);
2510 data->accesses = NULL;
2512 if (!flow)
2513 goto error;
2515 df->must_no_source = isl_union_map_union(df->must_no_source,
2516 isl_union_map_from_map(isl_flow_get_no_source(flow, 1)));
2517 df->may_no_source = isl_union_map_union(df->may_no_source,
2518 isl_union_map_from_map(isl_flow_get_no_source(flow, 0)));
2520 for (i = 0; i < flow->n_source; ++i) {
2521 isl_union_map *dep;
2522 dep = isl_union_map_from_map(isl_map_copy(flow->dep[i].map));
2523 if (flow->dep[i].must)
2524 df->must_dep = isl_union_map_union(df->must_dep, dep);
2525 else
2526 df->may_dep = isl_union_map_union(df->may_dep, dep);
2529 isl_flow_free(flow);
2531 sched_info_free(data->sink_info);
2532 if (data->source_info) {
2533 for (i = 0; i < data->count; ++i)
2534 sched_info_free(data->source_info[i]);
2535 free(data->source_info);
2537 isl_space_free(data->dim);
2538 isl_map_free(map);
2540 return isl_stat_ok;
2541 error:
2542 isl_access_info_free(data->accesses);
2543 sched_info_free(data->sink_info);
2544 if (data->source_info) {
2545 for (i = 0; i < data->count; ++i)
2546 sched_info_free(data->source_info[i]);
2547 free(data->source_info);
2549 isl_space_free(data->dim);
2550 isl_map_free(map);
2552 return isl_stat_error;
2555 /* Add the kills of "info" to the must-sources.
2557 static __isl_give isl_union_access_info *
2558 isl_union_access_info_add_kill_to_must_source(
2559 __isl_take isl_union_access_info *info)
2561 isl_union_map *must, *kill;
2563 must = isl_union_access_info_get_must_source(info);
2564 kill = isl_union_access_info_get_kill(info);
2565 must = isl_union_map_union(must, kill);
2566 return isl_union_access_info_set_must_source(info, must);
2569 /* Drop dependences from "flow" that purely originate from kills.
2570 * That is, only keep those dependences that originate from
2571 * the original must-sources "must" and/or the original may-sources "may".
2572 * In particular, "must" contains the must-sources from before
2573 * the kills were added and "may" contains the may-source from before
2574 * the kills were removed.
2576 * The dependences are of the form
2578 * Source -> [Sink -> Data]
2580 * Only those dependences are kept where the Source -> Data part
2581 * is a subset of the original may-sources or must-sources.
2582 * Of those, only the must-dependences that intersect with the must-sources
2583 * remain must-dependences.
2584 * If there is some overlap between the may-sources and the must-sources,
2585 * then the may-dependences and must-dependences may also overlap.
2586 * This should be fine since the may-dependences are only kept
2587 * disjoint from the must-dependences for the isl_union_map_compute_flow
2588 * interface. This interface does not support kills, so it will
2589 * not end up calling this function.
2591 static __isl_give isl_union_flow *isl_union_flow_drop_kill_source(
2592 __isl_take isl_union_flow *flow, __isl_take isl_union_map *must,
2593 __isl_take isl_union_map *may)
2595 isl_union_map *move;
2597 if (!flow)
2598 goto error;
2599 move = isl_union_map_copy(flow->must_dep);
2600 move = isl_union_map_intersect_range_factor_range(move,
2601 isl_union_map_copy(may));
2602 may = isl_union_map_union(may, isl_union_map_copy(must));
2603 flow->may_dep = isl_union_map_intersect_range_factor_range(
2604 flow->may_dep, may);
2605 flow->must_dep = isl_union_map_intersect_range_factor_range(
2606 flow->must_dep, must);
2607 flow->may_dep = isl_union_map_union(flow->may_dep, move);
2608 if (!flow->must_dep || !flow->may_dep)
2609 return isl_union_flow_free(flow);
2611 return flow;
2612 error:
2613 isl_union_map_free(must);
2614 isl_union_map_free(may);
2615 return NULL;
2618 /* Remove the must accesses from the may accesses.
2620 * A must access always trumps a may access, so there is no need
2621 * for a must access to also be considered as a may access. Doing so
2622 * would only cost extra computations only to find out that
2623 * the duplicated may access does not make any difference.
2625 static __isl_give isl_union_access_info *isl_union_access_info_normalize(
2626 __isl_take isl_union_access_info *access)
2628 if (!access)
2629 return NULL;
2630 access->access[isl_access_may_source] =
2631 isl_union_map_subtract(access->access[isl_access_may_source],
2632 isl_union_map_copy(access->access[isl_access_must_source]));
2633 if (!access->access[isl_access_may_source])
2634 return isl_union_access_info_free(access);
2636 return access;
2639 /* Given a description of the "sink" accesses, the "source" accesses and
2640 * a schedule, compute for each instance of a sink access
2641 * and for each element accessed by that instance,
2642 * the possible or definite source accesses that last accessed the
2643 * element accessed by the sink access before this sink access
2644 * in the sense that there is no intermediate definite source access.
2646 * The must_no_source and may_no_source elements of the result
2647 * are subsets of access->sink. The elements must_dep and may_dep
2648 * map domain elements of access->{may,must)_source to
2649 * domain elements of access->sink.
2651 * This function is used when only the schedule map representation
2652 * is available.
2654 * We first prepend the schedule dimensions to the domain
2655 * of the accesses so that we can easily compare their relative order.
2656 * Then we consider each sink access individually in compute_flow.
2658 static __isl_give isl_union_flow *compute_flow_union_map(
2659 __isl_take isl_union_access_info *access)
2661 struct isl_compute_flow_data data;
2662 isl_union_map *sink;
2664 access = isl_union_access_info_align_params(access);
2665 access = isl_union_access_info_introduce_schedule(access);
2666 if (!access)
2667 return NULL;
2669 data.must_source = access->access[isl_access_must_source];
2670 data.may_source = access->access[isl_access_may_source];
2672 sink = access->access[isl_access_sink];
2673 data.flow = isl_union_flow_alloc(isl_union_map_get_space(sink));
2675 if (isl_union_map_foreach_map(sink, &compute_flow, &data) < 0)
2676 goto error;
2678 data.flow = isl_union_flow_drop_schedule(data.flow);
2680 isl_union_access_info_free(access);
2681 return data.flow;
2682 error:
2683 isl_union_access_info_free(access);
2684 isl_union_flow_free(data.flow);
2685 return NULL;
2688 /* A schedule access relation.
2690 * The access relation "access" is of the form [S -> D] -> A,
2691 * where S corresponds to the prefix schedule at "node".
2692 * "must" is only relevant for source accesses and indicates
2693 * whether the access is a must source or a may source.
2695 struct isl_scheduled_access {
2696 isl_map *access;
2697 int must;
2698 isl_schedule_node *node;
2701 /* Data structure for keeping track of individual scheduled sink and source
2702 * accesses when computing dependence analysis based on a schedule tree.
2704 * "n_sink" is the number of used entries in "sink"
2705 * "n_source" is the number of used entries in "source"
2707 * "set_sink", "must" and "node" are only used inside collect_sink_source,
2708 * to keep track of the current node and
2709 * of what extract_sink_source needs to do.
2711 struct isl_compute_flow_schedule_data {
2712 isl_union_access_info *access;
2714 int n_sink;
2715 int n_source;
2717 struct isl_scheduled_access *sink;
2718 struct isl_scheduled_access *source;
2720 int set_sink;
2721 int must;
2722 isl_schedule_node *node;
2725 /* Align the parameters of all sinks with all sources.
2727 * If there are no sinks or no sources, then no alignment is needed.
2729 static void isl_compute_flow_schedule_data_align_params(
2730 struct isl_compute_flow_schedule_data *data)
2732 int i;
2733 isl_space *space;
2735 if (data->n_sink == 0 || data->n_source == 0)
2736 return;
2738 space = isl_map_get_space(data->sink[0].access);
2740 for (i = 1; i < data->n_sink; ++i)
2741 space = isl_space_align_params(space,
2742 isl_map_get_space(data->sink[i].access));
2743 for (i = 0; i < data->n_source; ++i)
2744 space = isl_space_align_params(space,
2745 isl_map_get_space(data->source[i].access));
2747 for (i = 0; i < data->n_sink; ++i)
2748 data->sink[i].access =
2749 isl_map_align_params(data->sink[i].access,
2750 isl_space_copy(space));
2751 for (i = 0; i < data->n_source; ++i)
2752 data->source[i].access =
2753 isl_map_align_params(data->source[i].access,
2754 isl_space_copy(space));
2756 isl_space_free(space);
2759 /* Free all the memory referenced from "data".
2760 * Do not free "data" itself as it may be allocated on the stack.
2762 static void isl_compute_flow_schedule_data_clear(
2763 struct isl_compute_flow_schedule_data *data)
2765 int i;
2767 if (!data->sink)
2768 return;
2770 for (i = 0; i < data->n_sink; ++i) {
2771 isl_map_free(data->sink[i].access);
2772 isl_schedule_node_free(data->sink[i].node);
2775 for (i = 0; i < data->n_source; ++i) {
2776 isl_map_free(data->source[i].access);
2777 isl_schedule_node_free(data->source[i].node);
2780 free(data->sink);
2783 /* isl_schedule_foreach_schedule_node_top_down callback for counting
2784 * (an upper bound on) the number of sinks and sources.
2786 * Sinks and sources are only extracted at leaves of the tree,
2787 * so we skip the node if it is not a leaf.
2788 * Otherwise we increment data->n_sink and data->n_source with
2789 * the number of spaces in the sink and source access domains
2790 * that reach this node.
2792 static isl_bool count_sink_source(__isl_keep isl_schedule_node *node,
2793 void *user)
2795 struct isl_compute_flow_schedule_data *data = user;
2796 isl_union_set *domain;
2797 isl_union_map *umap;
2798 isl_bool r = isl_bool_false;
2800 if (isl_schedule_node_get_type(node) != isl_schedule_node_leaf)
2801 return isl_bool_true;
2803 domain = isl_schedule_node_get_universe_domain(node);
2805 umap = isl_union_map_copy(data->access->access[isl_access_sink]);
2806 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
2807 data->n_sink += isl_union_map_n_map(umap);
2808 isl_union_map_free(umap);
2809 if (!umap)
2810 r = isl_bool_error;
2812 umap = isl_union_map_copy(data->access->access[isl_access_must_source]);
2813 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
2814 data->n_source += isl_union_map_n_map(umap);
2815 isl_union_map_free(umap);
2816 if (!umap)
2817 r = isl_bool_error;
2819 umap = isl_union_map_copy(data->access->access[isl_access_may_source]);
2820 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
2821 data->n_source += isl_union_map_n_map(umap);
2822 isl_union_map_free(umap);
2823 if (!umap)
2824 r = isl_bool_error;
2826 isl_union_set_free(domain);
2828 return r;
2831 /* Add a single scheduled sink or source (depending on data->set_sink)
2832 * with scheduled access relation "map", must property data->must and
2833 * schedule node data->node to the list of sinks or sources.
2835 static isl_stat extract_sink_source(__isl_take isl_map *map, void *user)
2837 struct isl_compute_flow_schedule_data *data = user;
2838 struct isl_scheduled_access *access;
2840 if (data->set_sink)
2841 access = data->sink + data->n_sink++;
2842 else
2843 access = data->source + data->n_source++;
2845 access->access = map;
2846 access->must = data->must;
2847 access->node = isl_schedule_node_copy(data->node);
2849 return isl_stat_ok;
2852 /* isl_schedule_foreach_schedule_node_top_down callback for collecting
2853 * individual scheduled source and sink accesses (taking into account
2854 * the domain of the schedule).
2856 * We only collect accesses at the leaves of the schedule tree.
2857 * We prepend the schedule dimensions at the leaf to the iteration
2858 * domains of the source and sink accesses and then extract
2859 * the individual accesses (per space).
2861 * In particular, if the prefix schedule at the node is of the form
2863 * D -> S
2865 * while the access relations are of the form
2867 * D -> A
2869 * then the updated access relations are of the form
2871 * [S -> D] -> A
2873 * Note that S consists of a single space such that introducing S
2874 * in the access relations does not increase the number of spaces.
2876 static isl_bool collect_sink_source(__isl_keep isl_schedule_node *node,
2877 void *user)
2879 struct isl_compute_flow_schedule_data *data = user;
2880 isl_union_map *prefix;
2881 isl_union_map *umap;
2882 isl_bool r = isl_bool_false;
2884 if (isl_schedule_node_get_type(node) != isl_schedule_node_leaf)
2885 return isl_bool_true;
2887 data->node = node;
2889 prefix = isl_schedule_node_get_prefix_schedule_relation(node);
2890 prefix = isl_union_map_reverse(prefix);
2891 prefix = isl_union_map_range_map(prefix);
2893 data->set_sink = 1;
2894 umap = isl_union_map_copy(data->access->access[isl_access_sink]);
2895 umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
2896 if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
2897 r = isl_bool_error;
2898 isl_union_map_free(umap);
2900 data->set_sink = 0;
2901 data->must = 1;
2902 umap = isl_union_map_copy(data->access->access[isl_access_must_source]);
2903 umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
2904 if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
2905 r = isl_bool_error;
2906 isl_union_map_free(umap);
2908 data->set_sink = 0;
2909 data->must = 0;
2910 umap = isl_union_map_copy(data->access->access[isl_access_may_source]);
2911 umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
2912 if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
2913 r = isl_bool_error;
2914 isl_union_map_free(umap);
2916 isl_union_map_free(prefix);
2918 return r;
2921 /* isl_access_info_compute_flow callback for determining whether
2922 * the shared nesting level and the ordering within that level
2923 * for two scheduled accesses for use in compute_single_flow.
2925 * The tokens passed to this function refer to the leaves
2926 * in the schedule tree where the accesses take place.
2928 * If n is the shared number of loops, then we need to return
2929 * "2 * n + 1" if "first" precedes "second" inside the innermost
2930 * shared loop and "2 * n" otherwise.
2932 * The innermost shared ancestor may be the leaves themselves
2933 * if the accesses take place in the same leaf. Otherwise,
2934 * it is either a set node or a sequence node. Only in the case
2935 * of a sequence node do we consider one access to precede the other.
2937 static int before_node(void *first, void *second)
2939 isl_schedule_node *node1 = first;
2940 isl_schedule_node *node2 = second;
2941 isl_schedule_node *shared;
2942 int depth;
2943 int before = 0;
2945 shared = isl_schedule_node_get_shared_ancestor(node1, node2);
2946 if (!shared)
2947 return -1;
2949 depth = isl_schedule_node_get_schedule_depth(shared);
2950 if (isl_schedule_node_get_type(shared) == isl_schedule_node_sequence) {
2951 int pos1, pos2;
2953 pos1 = isl_schedule_node_get_ancestor_child_position(node1,
2954 shared);
2955 pos2 = isl_schedule_node_get_ancestor_child_position(node2,
2956 shared);
2957 before = pos1 < pos2;
2960 isl_schedule_node_free(shared);
2962 return 2 * depth + before;
2965 /* Check if the given two accesses may be coscheduled.
2966 * If so, return 1. Otherwise return 0.
2968 * Two accesses may only be coscheduled if they appear in the same leaf.
2970 static int coscheduled_node(void *first, void *second)
2972 isl_schedule_node *node1 = first;
2973 isl_schedule_node *node2 = second;
2975 return node1 == node2;
2978 /* Add the scheduled sources from "data" that access
2979 * the same data space as "sink" to "access".
2981 static __isl_give isl_access_info *add_matching_sources(
2982 __isl_take isl_access_info *access, struct isl_scheduled_access *sink,
2983 struct isl_compute_flow_schedule_data *data)
2985 int i;
2986 isl_space *space;
2988 space = isl_space_range(isl_map_get_space(sink->access));
2989 for (i = 0; i < data->n_source; ++i) {
2990 struct isl_scheduled_access *source;
2991 isl_space *source_space;
2992 int eq;
2994 source = &data->source[i];
2995 source_space = isl_map_get_space(source->access);
2996 source_space = isl_space_range(source_space);
2997 eq = isl_space_is_equal(space, source_space);
2998 isl_space_free(source_space);
3000 if (!eq)
3001 continue;
3002 if (eq < 0)
3003 goto error;
3005 access = isl_access_info_add_source(access,
3006 isl_map_copy(source->access), source->must, source->node);
3009 isl_space_free(space);
3010 return access;
3011 error:
3012 isl_space_free(space);
3013 isl_access_info_free(access);
3014 return NULL;
3017 /* Given a scheduled sink access relation "sink", compute the corresponding
3018 * dependences on the sources in "data" and add the computed dependences
3019 * to "uf".
3021 * The dependences computed by access_info_compute_flow_core are of the form
3023 * [S -> I] -> [[S' -> I'] -> A]
3025 * The schedule dimensions are projected out by first currying the range,
3026 * resulting in
3028 * [S -> I] -> [S' -> [I' -> A]]
3030 * and then computing the factor range
3032 * I -> [I' -> A]
3034 static __isl_give isl_union_flow *compute_single_flow(
3035 __isl_take isl_union_flow *uf, struct isl_scheduled_access *sink,
3036 struct isl_compute_flow_schedule_data *data)
3038 int i;
3039 isl_access_info *access;
3040 isl_flow *flow;
3041 isl_map *map;
3043 if (!uf)
3044 return NULL;
3046 access = isl_access_info_alloc(isl_map_copy(sink->access), sink->node,
3047 &before_node, data->n_source);
3048 if (access)
3049 access->coscheduled = &coscheduled_node;
3050 access = add_matching_sources(access, sink, data);
3052 flow = access_info_compute_flow_core(access);
3053 if (!flow)
3054 return isl_union_flow_free(uf);
3056 map = isl_map_domain_factor_range(isl_flow_get_no_source(flow, 1));
3057 uf->must_no_source = isl_union_map_union(uf->must_no_source,
3058 isl_union_map_from_map(map));
3059 map = isl_map_domain_factor_range(isl_flow_get_no_source(flow, 0));
3060 uf->may_no_source = isl_union_map_union(uf->may_no_source,
3061 isl_union_map_from_map(map));
3063 for (i = 0; i < flow->n_source; ++i) {
3064 isl_union_map *dep;
3066 map = isl_map_range_curry(isl_map_copy(flow->dep[i].map));
3067 map = isl_map_factor_range(map);
3068 dep = isl_union_map_from_map(map);
3069 if (flow->dep[i].must)
3070 uf->must_dep = isl_union_map_union(uf->must_dep, dep);
3071 else
3072 uf->may_dep = isl_union_map_union(uf->may_dep, dep);
3075 isl_flow_free(flow);
3077 return uf;
3080 /* Given a description of the "sink" accesses, the "source" accesses and
3081 * a schedule, compute for each instance of a sink access
3082 * and for each element accessed by that instance,
3083 * the possible or definite source accesses that last accessed the
3084 * element accessed by the sink access before this sink access
3085 * in the sense that there is no intermediate definite source access.
3086 * Only consider dependences between statement instances that belong
3087 * to the domain of the schedule.
3089 * The must_no_source and may_no_source elements of the result
3090 * are subsets of access->sink. The elements must_dep and may_dep
3091 * map domain elements of access->{may,must)_source to
3092 * domain elements of access->sink.
3094 * This function is used when a schedule tree representation
3095 * is available.
3097 * We extract the individual scheduled source and sink access relations
3098 * (taking into account the domain of the schedule) and
3099 * then compute dependences for each scheduled sink individually.
3101 static __isl_give isl_union_flow *compute_flow_schedule(
3102 __isl_take isl_union_access_info *access)
3104 struct isl_compute_flow_schedule_data data = { access };
3105 int i, n;
3106 isl_ctx *ctx;
3107 isl_space *space;
3108 isl_union_flow *flow;
3110 ctx = isl_union_access_info_get_ctx(access);
3112 data.n_sink = 0;
3113 data.n_source = 0;
3114 if (isl_schedule_foreach_schedule_node_top_down(access->schedule,
3115 &count_sink_source, &data) < 0)
3116 goto error;
3118 n = data.n_sink + data.n_source;
3119 data.sink = isl_calloc_array(ctx, struct isl_scheduled_access, n);
3120 if (n && !data.sink)
3121 goto error;
3122 data.source = data.sink + data.n_sink;
3124 data.n_sink = 0;
3125 data.n_source = 0;
3126 if (isl_schedule_foreach_schedule_node_top_down(access->schedule,
3127 &collect_sink_source, &data) < 0)
3128 goto error;
3130 space = isl_union_map_get_space(access->access[isl_access_sink]);
3131 flow = isl_union_flow_alloc(space);
3133 isl_compute_flow_schedule_data_align_params(&data);
3135 for (i = 0; i < data.n_sink; ++i)
3136 flow = compute_single_flow(flow, &data.sink[i], &data);
3138 isl_compute_flow_schedule_data_clear(&data);
3140 isl_union_access_info_free(access);
3141 return flow;
3142 error:
3143 isl_union_access_info_free(access);
3144 isl_compute_flow_schedule_data_clear(&data);
3145 return NULL;
3148 /* Given a description of the "sink" accesses, the "source" accesses and
3149 * a schedule, compute for each instance of a sink access
3150 * and for each element accessed by that instance,
3151 * the possible or definite source accesses that last accessed the
3152 * element accessed by the sink access before this sink access
3153 * in the sense that there is no intermediate definite source access.
3155 * The must_no_source and may_no_source elements of the result
3156 * are subsets of access->sink. The elements must_dep and may_dep
3157 * map domain elements of access->{may,must)_source to
3158 * domain elements of access->sink.
3160 * If any kills have been specified, then they are treated as
3161 * must-sources internally. Any dependence that purely derives
3162 * from an original kill is removed from the output.
3164 * We check whether the schedule is available as a schedule tree
3165 * or a schedule map and call the corresponding function to perform
3166 * the analysis.
3168 __isl_give isl_union_flow *isl_union_access_info_compute_flow(
3169 __isl_take isl_union_access_info *access)
3171 isl_bool has_kill;
3172 isl_union_map *must = NULL, *may = NULL;
3173 isl_union_flow *flow;
3175 has_kill = isl_union_access_has_kill(access);
3176 if (has_kill < 0)
3177 goto error;
3178 if (has_kill) {
3179 must = isl_union_access_info_get_must_source(access);
3180 may = isl_union_access_info_get_may_source(access);
3182 access = isl_union_access_info_add_kill_to_must_source(access);
3183 access = isl_union_access_info_normalize(access);
3184 if (!access)
3185 goto error;
3186 if (access->schedule)
3187 flow = compute_flow_schedule(access);
3188 else
3189 flow = compute_flow_union_map(access);
3190 if (has_kill)
3191 flow = isl_union_flow_drop_kill_source(flow, must, may);
3192 return flow;
3193 error:
3194 isl_union_access_info_free(access);
3195 isl_union_map_free(must);
3196 isl_union_map_free(may);
3197 return NULL;
3200 /* Print the information contained in "flow" to "p".
3201 * The information is printed as a YAML document.
3203 __isl_give isl_printer *isl_printer_print_union_flow(
3204 __isl_take isl_printer *p, __isl_keep isl_union_flow *flow)
3206 isl_union_map *umap;
3208 if (!flow)
3209 return isl_printer_free(p);
3211 p = isl_printer_yaml_start_mapping(p);
3212 umap = isl_union_flow_get_full_must_dependence(flow);
3213 p = print_union_map_field(p, "must_dependence", umap);
3214 isl_union_map_free(umap);
3215 umap = isl_union_flow_get_full_may_dependence(flow);
3216 p = print_union_map_field(p, "may_dependence", umap);
3217 isl_union_map_free(umap);
3218 p = print_union_map_field(p, "must_no_source", flow->must_no_source);
3219 umap = isl_union_flow_get_may_no_source(flow);
3220 p = print_union_map_field(p, "may_no_source", umap);
3221 isl_union_map_free(umap);
3222 p = isl_printer_yaml_end_mapping(p);
3224 return p;
3227 /* Return a string representation of the information in "flow".
3228 * The information is printed in flow format.
3230 __isl_give char *isl_union_flow_to_str(__isl_keep isl_union_flow *flow)
3232 isl_printer *p;
3233 char *s;
3235 if (!flow)
3236 return NULL;
3238 p = isl_printer_to_str(isl_union_flow_get_ctx(flow));
3239 p = isl_printer_set_yaml_style(p, ISL_YAML_STYLE_FLOW);
3240 p = isl_printer_print_union_flow(p, flow);
3241 s = isl_printer_get_str(p);
3242 isl_printer_free(p);
3244 return s;
3247 /* Given a collection of "sink" and "source" accesses,
3248 * compute for each iteration of a sink access
3249 * and for each element accessed by that iteration,
3250 * the source access in the list that last accessed the
3251 * element accessed by the sink access before this sink access.
3252 * Each access is given as a map from the loop iterators
3253 * to the array indices.
3254 * The result is a relations between source and sink
3255 * iterations and a subset of the domain of the sink accesses,
3256 * corresponding to those iterations that access an element
3257 * not previously accessed.
3259 * We collect the inputs in an isl_union_access_info object,
3260 * call isl_union_access_info_compute_flow and extract
3261 * the outputs from the result.
3263 int isl_union_map_compute_flow(__isl_take isl_union_map *sink,
3264 __isl_take isl_union_map *must_source,
3265 __isl_take isl_union_map *may_source,
3266 __isl_take isl_union_map *schedule,
3267 __isl_give isl_union_map **must_dep, __isl_give isl_union_map **may_dep,
3268 __isl_give isl_union_map **must_no_source,
3269 __isl_give isl_union_map **may_no_source)
3271 isl_union_access_info *access;
3272 isl_union_flow *flow;
3274 access = isl_union_access_info_from_sink(sink);
3275 access = isl_union_access_info_set_must_source(access, must_source);
3276 access = isl_union_access_info_set_may_source(access, may_source);
3277 access = isl_union_access_info_set_schedule_map(access, schedule);
3278 flow = isl_union_access_info_compute_flow(access);
3280 if (must_dep)
3281 *must_dep = isl_union_flow_get_must_dependence(flow);
3282 if (may_dep)
3283 *may_dep = isl_union_flow_get_non_must_dependence(flow);
3284 if (must_no_source)
3285 *must_no_source = isl_union_flow_get_must_no_source(flow);
3286 if (may_no_source)
3287 *may_no_source = isl_union_flow_get_non_must_no_source(flow);
3289 isl_union_flow_free(flow);
3291 if ((must_dep && !*must_dep) || (may_dep && !*may_dep) ||
3292 (must_no_source && !*must_no_source) ||
3293 (may_no_source && !*may_no_source))
3294 goto error;
3296 return 0;
3297 error:
3298 if (must_dep)
3299 *must_dep = isl_union_map_free(*must_dep);
3300 if (may_dep)
3301 *may_dep = isl_union_map_free(*may_dep);
3302 if (must_no_source)
3303 *must_no_source = isl_union_map_free(*must_no_source);
3304 if (may_no_source)
3305 *may_no_source = isl_union_map_free(*may_no_source);
3306 return -1;