Merge branch 'maint'
[isl.git] / isl_flow.c
blobccdbe72d56bc6b91f486a33112c26ffc2a462f2f
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;
372 /* Sort the must source accesses in their textual order.
374 static __isl_give isl_access_info *isl_access_info_sort_sources(
375 __isl_take isl_access_info *acc)
377 struct access_sort_info sort_info;
379 sort_info.access_info = acc;
380 sort_info.error = 0;
382 if (!acc)
383 return NULL;
384 if (acc->n_must <= 1)
385 return acc;
387 if (isl_sort(acc->source, acc->n_must, sizeof(struct isl_labeled_map),
388 access_sort_cmp, &sort_info) < 0)
389 return isl_access_info_free(acc);
390 if (sort_info.error)
391 return isl_access_info_free(acc);
393 return acc;
396 /* Align the parameters of the two spaces if needed and then call
397 * isl_space_join.
399 static __isl_give isl_space *space_align_and_join(__isl_take isl_space *left,
400 __isl_take isl_space *right)
402 isl_bool equal_params;
404 equal_params = isl_space_has_equal_params(left, right);
405 if (equal_params < 0)
406 goto error;
407 if (equal_params)
408 return isl_space_join(left, right);
410 left = isl_space_align_params(left, isl_space_copy(right));
411 right = isl_space_align_params(right, isl_space_copy(left));
412 return isl_space_join(left, right);
413 error:
414 isl_space_free(left);
415 isl_space_free(right);
416 return NULL;
419 /* Initialize an empty isl_flow structure corresponding to a given
420 * isl_access_info structure.
421 * For each must access, two dependences are created (initialized
422 * to the empty relation), one for the resulting must dependences
423 * and one for the resulting may dependences. May accesses can
424 * only lead to may dependences, so only one dependence is created
425 * for each of them.
426 * This function is private as isl_flow structures are only supposed
427 * to be created by isl_access_info_compute_flow.
429 static __isl_give isl_flow *isl_flow_alloc(__isl_keep isl_access_info *acc)
431 int i, n;
432 struct isl_ctx *ctx;
433 struct isl_flow *dep;
435 if (!acc)
436 return NULL;
438 ctx = isl_map_get_ctx(acc->sink.map);
439 dep = isl_calloc_type(ctx, struct isl_flow);
440 if (!dep)
441 return NULL;
443 n = 2 * acc->n_must + acc->n_may;
444 dep->dep = isl_calloc_array(ctx, struct isl_labeled_map, n);
445 if (n && !dep->dep)
446 goto error;
448 dep->n_source = n;
449 for (i = 0; i < acc->n_must; ++i) {
450 isl_space *dim;
451 dim = space_align_and_join(
452 isl_map_get_space(acc->source[i].map),
453 isl_space_reverse(isl_map_get_space(acc->sink.map)));
454 dep->dep[2 * i].map = isl_map_empty(dim);
455 dep->dep[2 * i + 1].map = isl_map_copy(dep->dep[2 * i].map);
456 dep->dep[2 * i].data = acc->source[i].data;
457 dep->dep[2 * i + 1].data = acc->source[i].data;
458 dep->dep[2 * i].must = 1;
459 dep->dep[2 * i + 1].must = 0;
460 if (!dep->dep[2 * i].map || !dep->dep[2 * i + 1].map)
461 goto error;
463 for (i = acc->n_must; i < acc->n_must + acc->n_may; ++i) {
464 isl_space *dim;
465 dim = space_align_and_join(
466 isl_map_get_space(acc->source[i].map),
467 isl_space_reverse(isl_map_get_space(acc->sink.map)));
468 dep->dep[acc->n_must + i].map = isl_map_empty(dim);
469 dep->dep[acc->n_must + i].data = acc->source[i].data;
470 dep->dep[acc->n_must + i].must = 0;
471 if (!dep->dep[acc->n_must + i].map)
472 goto error;
475 return dep;
476 error:
477 isl_flow_free(dep);
478 return NULL;
481 /* Iterate over all sources and for each resulting flow dependence
482 * that is not empty, call the user specfied function.
483 * The second argument in this function call identifies the source,
484 * while the third argument correspond to the final argument of
485 * the isl_flow_foreach call.
487 isl_stat isl_flow_foreach(__isl_keep isl_flow *deps,
488 isl_stat (*fn)(__isl_take isl_map *dep, int must, void *dep_user,
489 void *user),
490 void *user)
492 int i;
494 if (!deps)
495 return isl_stat_error;
497 for (i = 0; i < deps->n_source; ++i) {
498 if (isl_map_plain_is_empty(deps->dep[i].map))
499 continue;
500 if (fn(isl_map_copy(deps->dep[i].map), deps->dep[i].must,
501 deps->dep[i].data, user) < 0)
502 return isl_stat_error;
505 return isl_stat_ok;
508 /* Return a copy of the subset of the sink for which no source could be found.
510 __isl_give isl_map *isl_flow_get_no_source(__isl_keep isl_flow *deps, int must)
512 if (!deps)
513 return NULL;
515 if (must)
516 return isl_set_unwrap(isl_set_copy(deps->must_no_source));
517 else
518 return isl_set_unwrap(isl_set_copy(deps->may_no_source));
521 void isl_flow_free(__isl_take isl_flow *deps)
523 int i;
525 if (!deps)
526 return;
527 isl_set_free(deps->must_no_source);
528 isl_set_free(deps->may_no_source);
529 if (deps->dep) {
530 for (i = 0; i < deps->n_source; ++i)
531 isl_map_free(deps->dep[i].map);
532 free(deps->dep);
534 free(deps);
537 isl_ctx *isl_flow_get_ctx(__isl_keep isl_flow *deps)
539 return deps ? isl_set_get_ctx(deps->must_no_source) : NULL;
542 /* Return a map that enforces that the domain iteration occurs after
543 * the range iteration at the given level.
544 * If level is odd, then the domain iteration should occur after
545 * the target iteration in their shared level/2 outermost loops.
546 * In this case we simply need to enforce that these outermost
547 * loop iterations are the same.
548 * If level is even, then the loop iterator of the domain should
549 * be greater than the loop iterator of the range at the last
550 * of the level/2 shared loops, i.e., loop level/2 - 1.
552 static __isl_give isl_map *after_at_level(__isl_take isl_space *dim, int level)
554 struct isl_basic_map *bmap;
556 if (level % 2)
557 bmap = isl_basic_map_equal(dim, level/2);
558 else
559 bmap = isl_basic_map_more_at(dim, level/2 - 1);
561 return isl_map_from_basic_map(bmap);
564 /* Compute the partial lexicographic maximum of "dep" on domain "sink",
565 * but first check if the user has set acc->restrict_fn and if so
566 * update either the input or the output of the maximization problem
567 * with respect to the resulting restriction.
569 * Since the user expects a mapping from sink iterations to source iterations,
570 * whereas the domain of "dep" is a wrapped map, mapping sink iterations
571 * to accessed array elements, we first need to project out the accessed
572 * sink array elements by applying acc->domain_map.
573 * Similarly, the sink restriction specified by the user needs to be
574 * converted back to the wrapped map.
576 static __isl_give isl_map *restricted_partial_lexmax(
577 __isl_keep isl_access_info *acc, __isl_take isl_map *dep,
578 int source, __isl_take isl_set *sink, __isl_give isl_set **empty)
580 isl_map *source_map;
581 isl_restriction *restr;
582 isl_set *sink_domain;
583 isl_set *sink_restr;
584 isl_map *res;
586 if (!acc->restrict_fn)
587 return isl_map_partial_lexmax(dep, sink, empty);
589 source_map = isl_map_copy(dep);
590 source_map = isl_map_apply_domain(source_map,
591 isl_map_copy(acc->domain_map));
592 sink_domain = isl_set_copy(sink);
593 sink_domain = isl_set_apply(sink_domain, isl_map_copy(acc->domain_map));
594 restr = acc->restrict_fn(source_map, sink_domain,
595 acc->source[source].data, acc->restrict_user);
596 isl_set_free(sink_domain);
597 isl_map_free(source_map);
599 if (!restr)
600 goto error;
601 if (restr->type == isl_restriction_type_input) {
602 dep = isl_map_intersect_range(dep, isl_set_copy(restr->source));
603 sink_restr = isl_set_copy(restr->sink);
604 sink_restr = isl_set_apply(sink_restr,
605 isl_map_reverse(isl_map_copy(acc->domain_map)));
606 sink = isl_set_intersect(sink, sink_restr);
607 } else if (restr->type == isl_restriction_type_empty) {
608 isl_space *space = isl_map_get_space(dep);
609 isl_map_free(dep);
610 dep = isl_map_empty(space);
613 res = isl_map_partial_lexmax(dep, sink, empty);
615 if (restr->type == isl_restriction_type_output)
616 res = isl_map_intersect_range(res, isl_set_copy(restr->source));
618 isl_restriction_free(restr);
619 return res;
620 error:
621 isl_map_free(dep);
622 isl_set_free(sink);
623 *empty = NULL;
624 return NULL;
627 /* Compute the last iteration of must source j that precedes the sink
628 * at the given level for sink iterations in set_C.
629 * The subset of set_C for which no such iteration can be found is returned
630 * in *empty.
632 static struct isl_map *last_source(struct isl_access_info *acc,
633 struct isl_set *set_C,
634 int j, int level, struct isl_set **empty)
636 struct isl_map *read_map;
637 struct isl_map *write_map;
638 struct isl_map *dep_map;
639 struct isl_map *after;
640 struct isl_map *result;
642 read_map = isl_map_copy(acc->sink.map);
643 write_map = isl_map_copy(acc->source[j].map);
644 write_map = isl_map_reverse(write_map);
645 dep_map = isl_map_apply_range(read_map, write_map);
646 after = after_at_level(isl_map_get_space(dep_map), level);
647 dep_map = isl_map_intersect(dep_map, after);
648 result = restricted_partial_lexmax(acc, dep_map, j, set_C, empty);
649 result = isl_map_reverse(result);
651 return result;
654 /* For a given mapping between iterations of must source j and iterations
655 * of the sink, compute the last iteration of must source k preceding
656 * the sink at level before_level for any of the sink iterations,
657 * but following the corresponding iteration of must source j at level
658 * after_level.
660 static struct isl_map *last_later_source(struct isl_access_info *acc,
661 struct isl_map *old_map,
662 int j, int before_level,
663 int k, int after_level,
664 struct isl_set **empty)
666 isl_space *dim;
667 struct isl_set *set_C;
668 struct isl_map *read_map;
669 struct isl_map *write_map;
670 struct isl_map *dep_map;
671 struct isl_map *after_write;
672 struct isl_map *before_read;
673 struct isl_map *result;
675 set_C = isl_map_range(isl_map_copy(old_map));
676 read_map = isl_map_copy(acc->sink.map);
677 write_map = isl_map_copy(acc->source[k].map);
679 write_map = isl_map_reverse(write_map);
680 dep_map = isl_map_apply_range(read_map, write_map);
681 dim = space_align_and_join(isl_map_get_space(acc->source[k].map),
682 isl_space_reverse(isl_map_get_space(acc->source[j].map)));
683 after_write = after_at_level(dim, after_level);
684 after_write = isl_map_apply_range(after_write, old_map);
685 after_write = isl_map_reverse(after_write);
686 dep_map = isl_map_intersect(dep_map, after_write);
687 before_read = after_at_level(isl_map_get_space(dep_map), before_level);
688 dep_map = isl_map_intersect(dep_map, before_read);
689 result = restricted_partial_lexmax(acc, dep_map, k, set_C, empty);
690 result = isl_map_reverse(result);
692 return result;
695 /* Given a shared_level between two accesses, return 1 if the
696 * the first can precede the second at the requested target_level.
697 * If the target level is odd, i.e., refers to a statement level
698 * dimension, then first needs to precede second at the requested
699 * level, i.e., shared_level must be equal to target_level.
700 * If the target level is odd, then the two loops should share
701 * at least the requested number of outer loops.
703 static int can_precede_at_level(int shared_level, int target_level)
705 if (shared_level < target_level)
706 return 0;
707 if ((target_level % 2) && shared_level > target_level)
708 return 0;
709 return 1;
712 /* Given a possible flow dependence temp_rel[j] between source j and the sink
713 * at level sink_level, remove those elements for which
714 * there is an iteration of another source k < j that is closer to the sink.
715 * The flow dependences temp_rel[k] are updated with the improved sources.
716 * Any improved source needs to precede the sink at the same level
717 * and needs to follow source j at the same or a deeper level.
718 * The lower this level, the later the execution date of source k.
719 * We therefore consider lower levels first.
721 * If temp_rel[j] is empty, then there can be no improvement and
722 * we return immediately.
724 * This function returns 0 in case it was executed successfully and
725 * -1 in case of errors during the execution of this function.
727 static int intermediate_sources(__isl_keep isl_access_info *acc,
728 struct isl_map **temp_rel, int j, int sink_level)
730 int k, level;
731 int depth = 2 * isl_map_dim(acc->source[j].map, isl_dim_in) + 1;
733 if (isl_map_plain_is_empty(temp_rel[j]))
734 return 0;
736 for (k = j - 1; k >= 0; --k) {
737 int plevel, plevel2;
738 plevel = acc->level_before(acc->source[k].data, acc->sink.data);
739 if (plevel < 0)
740 return -1;
741 if (!can_precede_at_level(plevel, sink_level))
742 continue;
744 plevel2 = acc->level_before(acc->source[j].data,
745 acc->source[k].data);
746 if (plevel2 < 0)
747 return -1;
749 for (level = sink_level; level <= depth; ++level) {
750 struct isl_map *T;
751 struct isl_set *trest;
752 struct isl_map *copy;
754 if (!can_precede_at_level(plevel2, level))
755 continue;
757 copy = isl_map_copy(temp_rel[j]);
758 T = last_later_source(acc, copy, j, sink_level, k,
759 level, &trest);
760 if (isl_map_plain_is_empty(T)) {
761 isl_set_free(trest);
762 isl_map_free(T);
763 continue;
765 temp_rel[j] = isl_map_intersect_range(temp_rel[j], trest);
766 temp_rel[k] = isl_map_union_disjoint(temp_rel[k], T);
770 return 0;
773 /* Compute all iterations of may source j that precedes the sink at the given
774 * level for sink iterations in set_C.
776 static __isl_give isl_map *all_sources(__isl_keep isl_access_info *acc,
777 __isl_take isl_set *set_C, int j, int level)
779 isl_map *read_map;
780 isl_map *write_map;
781 isl_map *dep_map;
782 isl_map *after;
784 read_map = isl_map_copy(acc->sink.map);
785 read_map = isl_map_intersect_domain(read_map, set_C);
786 write_map = isl_map_copy(acc->source[acc->n_must + j].map);
787 write_map = isl_map_reverse(write_map);
788 dep_map = isl_map_apply_range(read_map, write_map);
789 after = after_at_level(isl_map_get_space(dep_map), level);
790 dep_map = isl_map_intersect(dep_map, after);
792 return isl_map_reverse(dep_map);
795 /* For a given mapping between iterations of must source k and iterations
796 * of the sink, compute all iterations of may source j preceding
797 * the sink at level before_level for any of the sink iterations,
798 * but following the corresponding iteration of must source k at level
799 * after_level.
801 static __isl_give isl_map *all_later_sources(__isl_keep isl_access_info *acc,
802 __isl_take isl_map *old_map,
803 int j, int before_level, int k, int after_level)
805 isl_space *dim;
806 isl_set *set_C;
807 isl_map *read_map;
808 isl_map *write_map;
809 isl_map *dep_map;
810 isl_map *after_write;
811 isl_map *before_read;
813 set_C = isl_map_range(isl_map_copy(old_map));
814 read_map = isl_map_copy(acc->sink.map);
815 read_map = isl_map_intersect_domain(read_map, set_C);
816 write_map = isl_map_copy(acc->source[acc->n_must + j].map);
818 write_map = isl_map_reverse(write_map);
819 dep_map = isl_map_apply_range(read_map, write_map);
820 dim = isl_space_join(isl_map_get_space(acc->source[acc->n_must + j].map),
821 isl_space_reverse(isl_map_get_space(acc->source[k].map)));
822 after_write = after_at_level(dim, after_level);
823 after_write = isl_map_apply_range(after_write, old_map);
824 after_write = isl_map_reverse(after_write);
825 dep_map = isl_map_intersect(dep_map, after_write);
826 before_read = after_at_level(isl_map_get_space(dep_map), before_level);
827 dep_map = isl_map_intersect(dep_map, before_read);
828 return isl_map_reverse(dep_map);
831 /* Given the must and may dependence relations for the must accesses
832 * for level sink_level, check if there are any accesses of may access j
833 * that occur in between and return their union.
834 * If some of these accesses are intermediate with respect to
835 * (previously thought to be) must dependences, then these
836 * must dependences are turned into may dependences.
838 static __isl_give isl_map *all_intermediate_sources(
839 __isl_keep isl_access_info *acc, __isl_take isl_map *map,
840 struct isl_map **must_rel, struct isl_map **may_rel,
841 int j, int sink_level)
843 int k, level;
844 int depth = 2 * isl_map_dim(acc->source[acc->n_must + j].map,
845 isl_dim_in) + 1;
847 for (k = 0; k < acc->n_must; ++k) {
848 int plevel;
850 if (isl_map_plain_is_empty(may_rel[k]) &&
851 isl_map_plain_is_empty(must_rel[k]))
852 continue;
854 plevel = acc->level_before(acc->source[k].data,
855 acc->source[acc->n_must + j].data);
856 if (plevel < 0)
857 return isl_map_free(map);
859 for (level = sink_level; level <= depth; ++level) {
860 isl_map *T;
861 isl_map *copy;
862 isl_set *ran;
864 if (!can_precede_at_level(plevel, level))
865 continue;
867 copy = isl_map_copy(may_rel[k]);
868 T = all_later_sources(acc, copy, j, sink_level, k, level);
869 map = isl_map_union(map, T);
871 copy = isl_map_copy(must_rel[k]);
872 T = all_later_sources(acc, copy, j, sink_level, k, level);
873 ran = isl_map_range(isl_map_copy(T));
874 map = isl_map_union(map, T);
875 may_rel[k] = isl_map_union_disjoint(may_rel[k],
876 isl_map_intersect_range(isl_map_copy(must_rel[k]),
877 isl_set_copy(ran)));
878 T = isl_map_from_domain_and_range(
879 isl_set_universe(
880 isl_space_domain(isl_map_get_space(must_rel[k]))),
881 ran);
882 must_rel[k] = isl_map_subtract(must_rel[k], T);
886 return map;
889 /* Given a dependence relation "old_map" between a must-source and the sink,
890 * return a subset of the dependences, augmented with instances
891 * of the source at position "pos" in "acc" that are coscheduled
892 * with the must-source and that access the same element.
893 * That is, if the input lives in a space T -> K, then the output
894 * lives in the space [T -> S] -> K, with S the space of source "pos", and
895 * the domain factor of the domain product is a subset of the input.
896 * The sources are considered to be coscheduled if they have the same values
897 * for the initial "depth" coordinates.
899 * First construct a dependence relation S -> K and a mapping
900 * between coscheduled sources T -> S.
901 * The second is combined with the original dependence relation T -> K
902 * to form a relation in T -> [S -> K], which is subsequently
903 * uncurried to [T -> S] -> K.
904 * This result is then intersected with the dependence relation S -> K
905 * to form the output.
907 static __isl_give isl_map *coscheduled_source(__isl_keep isl_access_info *acc,
908 __isl_keep isl_map *old_map, int pos, int depth)
910 isl_space *space;
911 isl_set *set_C;
912 isl_map *read_map;
913 isl_map *write_map;
914 isl_map *dep_map;
915 isl_map *equal;
916 isl_map *map;
918 set_C = isl_map_range(isl_map_copy(old_map));
919 read_map = isl_map_copy(acc->sink.map);
920 read_map = isl_map_intersect_domain(read_map, set_C);
921 write_map = isl_map_copy(acc->source[pos].map);
922 dep_map = isl_map_domain_product(write_map, read_map);
923 dep_map = isl_set_unwrap(isl_map_domain(dep_map));
924 space = isl_space_join(isl_map_get_space(old_map),
925 isl_space_reverse(isl_map_get_space(dep_map)));
926 equal = isl_map_from_basic_map(isl_basic_map_equal(space, depth));
927 map = isl_map_range_product(equal, isl_map_copy(old_map));
928 map = isl_map_uncurry(map);
929 map = isl_map_intersect_domain_factor_range(map, dep_map);
931 return map;
934 /* After the dependences derived from a must-source have been computed
935 * at a certain level, check if any of the sources of the must-dependences
936 * may be coscheduled with other sources.
937 * If they are any such sources, then there is no way of determining
938 * which of the sources actually comes last and the must-dependences
939 * need to be turned into may-dependences, while dependences from
940 * the other sources need to be added to the may-dependences as well.
941 * "acc" describes the sources and a callback for checking whether
942 * two sources may be coscheduled. If acc->coscheduled is NULL then
943 * the sources are assumed not to be coscheduled.
944 * "must_rel" and "may_rel" describe the must and may-dependence relations
945 * computed at the current level for the must-sources. Some of the dependences
946 * may be moved from "must_rel" to "may_rel".
947 * "flow" contains all dependences computed so far (apart from those
948 * in "must_rel" and "may_rel") and may be updated with additional
949 * dependences derived from may-sources.
951 * In particular, consider all the must-sources with a non-empty
952 * dependence relation in "must_rel". They are considered in reverse
953 * order because that is the order in which they are considered in the caller.
954 * If any of the must-sources are coscheduled, then the last one
955 * is the one that will have a corresponding dependence relation.
956 * For each must-source i, consider both all the previous must-sources
957 * and all the may-sources. If any of those may be coscheduled with
958 * must-source i, then compute the coscheduled instances that access
959 * the same memory elements. The result is a relation [T -> S] -> K.
960 * The projection onto T -> K is a subset of the must-dependence relation
961 * that needs to be turned into may-dependences.
962 * The projection onto S -> K needs to be added to the may-dependences
963 * of source S.
964 * Since a given must-source instance may be coscheduled with several
965 * other source instances, the dependences that need to be turned
966 * into may-dependences are first collected and only actually removed
967 * from the must-dependences after all other sources have been considered.
969 static __isl_give isl_flow *handle_coscheduled(__isl_keep isl_access_info *acc,
970 __isl_keep isl_map **must_rel, __isl_keep isl_map **may_rel,
971 __isl_take isl_flow *flow)
973 int i, j;
975 if (!acc->coscheduled)
976 return flow;
977 for (i = acc->n_must - 1; i >= 0; --i) {
978 isl_map *move;
980 if (isl_map_plain_is_empty(must_rel[i]))
981 continue;
982 move = isl_map_empty(isl_map_get_space(must_rel[i]));
983 for (j = i - 1; j >= 0; --j) {
984 int depth;
985 isl_map *map, *factor;
987 if (!acc->coscheduled(acc->source[i].data,
988 acc->source[j].data))
989 continue;
990 depth = acc->level_before(acc->source[i].data,
991 acc->source[j].data) / 2;
992 map = coscheduled_source(acc, must_rel[i], j, depth);
993 factor = isl_map_domain_factor_range(isl_map_copy(map));
994 may_rel[j] = isl_map_union(may_rel[j], factor);
995 map = isl_map_domain_factor_domain(map);
996 move = isl_map_union(move, map);
998 for (j = 0; j < acc->n_may; ++j) {
999 int depth, pos;
1000 isl_map *map, *factor;
1002 pos = acc->n_must + j;
1003 if (!acc->coscheduled(acc->source[i].data,
1004 acc->source[pos].data))
1005 continue;
1006 depth = acc->level_before(acc->source[i].data,
1007 acc->source[pos].data) / 2;
1008 map = coscheduled_source(acc, must_rel[i], pos, depth);
1009 factor = isl_map_domain_factor_range(isl_map_copy(map));
1010 pos = 2 * acc->n_must + j;
1011 flow->dep[pos].map = isl_map_union(flow->dep[pos].map,
1012 factor);
1013 map = isl_map_domain_factor_domain(map);
1014 move = isl_map_union(move, map);
1016 must_rel[i] = isl_map_subtract(must_rel[i], isl_map_copy(move));
1017 may_rel[i] = isl_map_union(may_rel[i], move);
1020 return flow;
1023 /* Compute dependences for the case where all accesses are "may"
1024 * accesses, which boils down to computing memory based dependences.
1025 * The generic algorithm would also work in this case, but it would
1026 * be overkill to use it.
1028 static __isl_give isl_flow *compute_mem_based_dependences(
1029 __isl_keep isl_access_info *acc)
1031 int i;
1032 isl_set *mustdo;
1033 isl_set *maydo;
1034 isl_flow *res;
1036 res = isl_flow_alloc(acc);
1037 if (!res)
1038 return NULL;
1040 mustdo = isl_map_domain(isl_map_copy(acc->sink.map));
1041 maydo = isl_set_copy(mustdo);
1043 for (i = 0; i < acc->n_may; ++i) {
1044 int plevel;
1045 int is_before;
1046 isl_space *dim;
1047 isl_map *before;
1048 isl_map *dep;
1050 plevel = acc->level_before(acc->source[i].data, acc->sink.data);
1051 if (plevel < 0)
1052 goto error;
1054 is_before = plevel & 1;
1055 plevel >>= 1;
1057 dim = isl_map_get_space(res->dep[i].map);
1058 if (is_before)
1059 before = isl_map_lex_le_first(dim, plevel);
1060 else
1061 before = isl_map_lex_lt_first(dim, plevel);
1062 dep = isl_map_apply_range(isl_map_copy(acc->source[i].map),
1063 isl_map_reverse(isl_map_copy(acc->sink.map)));
1064 dep = isl_map_intersect(dep, before);
1065 mustdo = isl_set_subtract(mustdo,
1066 isl_map_range(isl_map_copy(dep)));
1067 res->dep[i].map = isl_map_union(res->dep[i].map, dep);
1070 res->may_no_source = isl_set_subtract(maydo, isl_set_copy(mustdo));
1071 res->must_no_source = mustdo;
1073 return res;
1074 error:
1075 isl_set_free(mustdo);
1076 isl_set_free(maydo);
1077 isl_flow_free(res);
1078 return NULL;
1081 /* Compute dependences for the case where there is at least one
1082 * "must" access.
1084 * The core algorithm considers all levels in which a source may precede
1085 * the sink, where a level may either be a statement level or a loop level.
1086 * The outermost statement level is 1, the first loop level is 2, etc...
1087 * The algorithm basically does the following:
1088 * for all levels l of the read access from innermost to outermost
1089 * for all sources w that may precede the sink access at that level
1090 * compute the last iteration of the source that precedes the sink access
1091 * at that level
1092 * add result to possible last accesses at level l of source w
1093 * for all sources w2 that we haven't considered yet at this level that may
1094 * also precede the sink access
1095 * for all levels l2 of w from l to innermost
1096 * for all possible last accesses dep of w at l
1097 * compute last iteration of w2 between the source and sink
1098 * of dep
1099 * add result to possible last accesses at level l of write w2
1100 * and replace possible last accesses dep by the remainder
1103 * The above algorithm is applied to the must access. During the course
1104 * of the algorithm, we keep track of sink iterations that still
1105 * need to be considered. These iterations are split into those that
1106 * haven't been matched to any source access (mustdo) and those that have only
1107 * been matched to may accesses (maydo).
1108 * At the end of each level, must-sources and may-sources that are coscheduled
1109 * with the sources of the must-dependences at that level are considered.
1110 * If any coscheduled instances are found, then corresponding may-dependences
1111 * are added and the original must-dependences are turned into may-dependences.
1112 * Afterwards, the may accesses that occur after must-dependence sources
1113 * are considered.
1114 * In particular, we consider may accesses that precede the remaining
1115 * sink iterations, moving elements from mustdo to maydo when appropriate,
1116 * and may accesses that occur between a must source and a sink of any
1117 * dependences found at the current level, turning must dependences into
1118 * may dependences when appropriate.
1121 static __isl_give isl_flow *compute_val_based_dependences(
1122 __isl_keep isl_access_info *acc)
1124 isl_ctx *ctx;
1125 isl_flow *res;
1126 isl_set *mustdo = NULL;
1127 isl_set *maydo = NULL;
1128 int level, j;
1129 int depth;
1130 isl_map **must_rel = NULL;
1131 isl_map **may_rel = NULL;
1133 if (!acc)
1134 return NULL;
1136 res = isl_flow_alloc(acc);
1137 if (!res)
1138 goto error;
1139 ctx = isl_map_get_ctx(acc->sink.map);
1141 depth = 2 * isl_map_dim(acc->sink.map, isl_dim_in) + 1;
1142 mustdo = isl_map_domain(isl_map_copy(acc->sink.map));
1143 maydo = isl_set_empty(isl_set_get_space(mustdo));
1144 if (!mustdo || !maydo)
1145 goto error;
1146 if (isl_set_plain_is_empty(mustdo))
1147 goto done;
1149 must_rel = isl_calloc_array(ctx, struct isl_map *, acc->n_must);
1150 may_rel = isl_calloc_array(ctx, struct isl_map *, acc->n_must);
1151 if (!must_rel || !may_rel)
1152 goto error;
1154 for (level = depth; level >= 1; --level) {
1155 for (j = acc->n_must-1; j >=0; --j) {
1156 isl_space *space;
1157 space = isl_map_get_space(res->dep[2 * j].map);
1158 must_rel[j] = isl_map_empty(space);
1159 may_rel[j] = isl_map_copy(must_rel[j]);
1162 for (j = acc->n_must - 1; j >= 0; --j) {
1163 struct isl_map *T;
1164 struct isl_set *rest;
1165 int plevel;
1167 plevel = acc->level_before(acc->source[j].data,
1168 acc->sink.data);
1169 if (plevel < 0)
1170 goto error;
1171 if (!can_precede_at_level(plevel, level))
1172 continue;
1174 T = last_source(acc, mustdo, j, level, &rest);
1175 must_rel[j] = isl_map_union_disjoint(must_rel[j], T);
1176 mustdo = rest;
1178 if (intermediate_sources(acc, must_rel, j, level))
1179 goto error;
1181 T = last_source(acc, maydo, j, level, &rest);
1182 may_rel[j] = isl_map_union_disjoint(may_rel[j], T);
1183 maydo = rest;
1185 if (intermediate_sources(acc, may_rel, j, level))
1186 goto error;
1188 if (isl_set_plain_is_empty(mustdo) &&
1189 isl_set_plain_is_empty(maydo))
1190 break;
1192 for (j = j - 1; j >= 0; --j) {
1193 int plevel;
1195 plevel = acc->level_before(acc->source[j].data,
1196 acc->sink.data);
1197 if (plevel < 0)
1198 goto error;
1199 if (!can_precede_at_level(plevel, level))
1200 continue;
1202 if (intermediate_sources(acc, must_rel, j, level))
1203 goto error;
1204 if (intermediate_sources(acc, may_rel, j, level))
1205 goto error;
1208 handle_coscheduled(acc, must_rel, may_rel, res);
1210 for (j = 0; j < acc->n_may; ++j) {
1211 int plevel;
1212 isl_map *T;
1213 isl_set *ran;
1215 plevel = acc->level_before(acc->source[acc->n_must + j].data,
1216 acc->sink.data);
1217 if (plevel < 0)
1218 goto error;
1219 if (!can_precede_at_level(plevel, level))
1220 continue;
1222 T = all_sources(acc, isl_set_copy(maydo), j, level);
1223 res->dep[2 * acc->n_must + j].map =
1224 isl_map_union(res->dep[2 * acc->n_must + j].map, T);
1225 T = all_sources(acc, isl_set_copy(mustdo), j, level);
1226 ran = isl_map_range(isl_map_copy(T));
1227 res->dep[2 * acc->n_must + j].map =
1228 isl_map_union(res->dep[2 * acc->n_must + j].map, T);
1229 mustdo = isl_set_subtract(mustdo, isl_set_copy(ran));
1230 maydo = isl_set_union_disjoint(maydo, ran);
1232 T = res->dep[2 * acc->n_must + j].map;
1233 T = all_intermediate_sources(acc, T, must_rel, may_rel,
1234 j, level);
1235 res->dep[2 * acc->n_must + j].map = T;
1238 for (j = acc->n_must - 1; j >= 0; --j) {
1239 res->dep[2 * j].map =
1240 isl_map_union_disjoint(res->dep[2 * j].map,
1241 must_rel[j]);
1242 res->dep[2 * j + 1].map =
1243 isl_map_union_disjoint(res->dep[2 * j + 1].map,
1244 may_rel[j]);
1247 if (isl_set_plain_is_empty(mustdo) &&
1248 isl_set_plain_is_empty(maydo))
1249 break;
1252 free(must_rel);
1253 free(may_rel);
1254 done:
1255 res->must_no_source = mustdo;
1256 res->may_no_source = maydo;
1257 return res;
1258 error:
1259 if (must_rel)
1260 for (j = 0; j < acc->n_must; ++j)
1261 isl_map_free(must_rel[j]);
1262 if (may_rel)
1263 for (j = 0; j < acc->n_must; ++j)
1264 isl_map_free(may_rel[j]);
1265 isl_flow_free(res);
1266 isl_set_free(mustdo);
1267 isl_set_free(maydo);
1268 free(must_rel);
1269 free(may_rel);
1270 return NULL;
1273 /* Given a "sink" access, a list of n "source" accesses,
1274 * compute for each iteration of the sink access
1275 * and for each element accessed by that iteration,
1276 * the source access in the list that last accessed the
1277 * element accessed by the sink access before this sink access.
1278 * Each access is given as a map from the loop iterators
1279 * to the array indices.
1280 * The result is a list of n relations between source and sink
1281 * iterations and a subset of the domain of the sink access,
1282 * corresponding to those iterations that access an element
1283 * not previously accessed.
1285 * To deal with multi-valued sink access relations, the sink iteration
1286 * domain is first extended with dimensions that correspond to the data
1287 * space. However, these extra dimensions are not projected out again.
1288 * It is up to the caller to decide whether these dimensions should be kept.
1290 static __isl_give isl_flow *access_info_compute_flow_core(
1291 __isl_take isl_access_info *acc)
1293 struct isl_flow *res = NULL;
1295 if (!acc)
1296 return NULL;
1298 acc->sink.map = isl_map_range_map(acc->sink.map);
1299 if (!acc->sink.map)
1300 goto error;
1302 if (acc->n_must == 0)
1303 res = compute_mem_based_dependences(acc);
1304 else {
1305 acc = isl_access_info_sort_sources(acc);
1306 res = compute_val_based_dependences(acc);
1308 acc = isl_access_info_free(acc);
1309 if (!res)
1310 return NULL;
1311 if (!res->must_no_source || !res->may_no_source)
1312 goto error;
1313 return res;
1314 error:
1315 isl_access_info_free(acc);
1316 isl_flow_free(res);
1317 return NULL;
1320 /* Given a "sink" access, a list of n "source" accesses,
1321 * compute for each iteration of the sink access
1322 * and for each element accessed by that iteration,
1323 * the source access in the list that last accessed the
1324 * element accessed by the sink access before this sink access.
1325 * Each access is given as a map from the loop iterators
1326 * to the array indices.
1327 * The result is a list of n relations between source and sink
1328 * iterations and a subset of the domain of the sink access,
1329 * corresponding to those iterations that access an element
1330 * not previously accessed.
1332 * To deal with multi-valued sink access relations,
1333 * access_info_compute_flow_core extends the sink iteration domain
1334 * with dimensions that correspond to the data space. These extra dimensions
1335 * are projected out from the result of access_info_compute_flow_core.
1337 __isl_give isl_flow *isl_access_info_compute_flow(__isl_take isl_access_info *acc)
1339 int j;
1340 struct isl_flow *res;
1342 if (!acc)
1343 return NULL;
1345 acc->domain_map = isl_map_domain_map(isl_map_copy(acc->sink.map));
1346 res = access_info_compute_flow_core(acc);
1347 if (!res)
1348 return NULL;
1350 for (j = 0; j < res->n_source; ++j) {
1351 res->dep[j].map = isl_map_range_factor_domain(res->dep[j].map);
1352 if (!res->dep[j].map)
1353 goto error;
1356 return res;
1357 error:
1358 isl_flow_free(res);
1359 return NULL;
1363 /* Keep track of some information about a schedule for a given
1364 * access. In particular, keep track of which dimensions
1365 * have a constant value and of the actual constant values.
1367 struct isl_sched_info {
1368 int *is_cst;
1369 isl_vec *cst;
1372 static void sched_info_free(__isl_take struct isl_sched_info *info)
1374 if (!info)
1375 return;
1376 isl_vec_free(info->cst);
1377 free(info->is_cst);
1378 free(info);
1381 /* Extract information on the constant dimensions of the schedule
1382 * for a given access. The "map" is of the form
1384 * [S -> D] -> A
1386 * with S the schedule domain, D the iteration domain and A the data domain.
1388 static __isl_give struct isl_sched_info *sched_info_alloc(
1389 __isl_keep isl_map *map)
1391 isl_ctx *ctx;
1392 isl_space *dim;
1393 struct isl_sched_info *info;
1394 int i, n;
1396 if (!map)
1397 return NULL;
1399 dim = isl_space_unwrap(isl_space_domain(isl_map_get_space(map)));
1400 if (!dim)
1401 return NULL;
1402 n = isl_space_dim(dim, isl_dim_in);
1403 isl_space_free(dim);
1405 ctx = isl_map_get_ctx(map);
1406 info = isl_alloc_type(ctx, struct isl_sched_info);
1407 if (!info)
1408 return NULL;
1409 info->is_cst = isl_alloc_array(ctx, int, n);
1410 info->cst = isl_vec_alloc(ctx, n);
1411 if (n && (!info->is_cst || !info->cst))
1412 goto error;
1414 for (i = 0; i < n; ++i) {
1415 isl_val *v;
1417 v = isl_map_plain_get_val_if_fixed(map, isl_dim_in, i);
1418 if (!v)
1419 goto error;
1420 info->is_cst[i] = !isl_val_is_nan(v);
1421 if (info->is_cst[i])
1422 info->cst = isl_vec_set_element_val(info->cst, i, v);
1423 else
1424 isl_val_free(v);
1427 return info;
1428 error:
1429 sched_info_free(info);
1430 return NULL;
1433 /* The different types of access relations that isl_union_access_info
1434 * keeps track of.
1436 * "isl_access_sink" represents the sink accesses.
1437 * "isl_access_must_source" represents the definite source accesses.
1438 * "isl_access_may_source" represents the possible source accesses.
1439 * "isl_access_kill" represents the kills.
1441 * isl_access_sink is sometimes treated differently and
1442 * should therefore appear first.
1444 enum isl_access_type {
1445 isl_access_sink,
1446 isl_access_must_source,
1447 isl_access_may_source,
1448 isl_access_kill,
1449 isl_access_end
1452 /* This structure represents the input for a dependence analysis computation.
1454 * "access" contains the access relations.
1456 * "schedule" or "schedule_map" represents the execution order.
1457 * Exactly one of these fields should be NULL. The other field
1458 * determines the execution order.
1460 * The domains of these four maps refer to the same iteration spaces(s).
1461 * The ranges of the first three maps also refer to the same data space(s).
1463 * After a call to isl_union_access_info_introduce_schedule,
1464 * the "schedule_map" field no longer contains useful information.
1466 struct isl_union_access_info {
1467 isl_union_map *access[isl_access_end];
1469 isl_schedule *schedule;
1470 isl_union_map *schedule_map;
1473 /* Free "access" and return NULL.
1475 __isl_null isl_union_access_info *isl_union_access_info_free(
1476 __isl_take isl_union_access_info *access)
1478 enum isl_access_type i;
1480 if (!access)
1481 return NULL;
1483 for (i = isl_access_sink; i < isl_access_end; ++i)
1484 isl_union_map_free(access->access[i]);
1485 isl_schedule_free(access->schedule);
1486 isl_union_map_free(access->schedule_map);
1487 free(access);
1489 return NULL;
1492 /* Return the isl_ctx to which "access" belongs.
1494 isl_ctx *isl_union_access_info_get_ctx(__isl_keep isl_union_access_info *access)
1496 if (!access)
1497 return NULL;
1498 return isl_union_map_get_ctx(access->access[isl_access_sink]);
1501 /* Construct an empty (invalid) isl_union_access_info object.
1502 * The caller is responsible for setting the sink access relation and
1503 * initializing all the other fields, e.g., by calling
1504 * isl_union_access_info_init.
1506 static __isl_give isl_union_access_info *isl_union_access_info_alloc(
1507 isl_ctx *ctx)
1509 return isl_calloc_type(ctx, isl_union_access_info);
1512 /* Initialize all the fields of "info", except the sink access relation,
1513 * which is assumed to have been set by the caller.
1515 * By default, we use the schedule field of the isl_union_access_info,
1516 * but this may be overridden by a call
1517 * to isl_union_access_info_set_schedule_map.
1519 static __isl_give isl_union_access_info *isl_union_access_info_init(
1520 __isl_take isl_union_access_info *info)
1522 isl_space *space;
1523 isl_union_map *empty;
1524 enum isl_access_type i;
1526 if (!info)
1527 return NULL;
1528 if (!info->access[isl_access_sink])
1529 return isl_union_access_info_free(info);
1531 space = isl_union_map_get_space(info->access[isl_access_sink]);
1532 empty = isl_union_map_empty(isl_space_copy(space));
1533 for (i = isl_access_sink + 1; i < isl_access_end; ++i)
1534 if (!info->access[i])
1535 info->access[i] = isl_union_map_copy(empty);
1536 isl_union_map_free(empty);
1537 if (!info->schedule && !info->schedule_map)
1538 info->schedule = isl_schedule_empty(isl_space_copy(space));
1539 isl_space_free(space);
1541 for (i = isl_access_sink + 1; i < isl_access_end; ++i)
1542 if (!info->access[i])
1543 return isl_union_access_info_free(info);
1544 if (!info->schedule && !info->schedule_map)
1545 return isl_union_access_info_free(info);
1547 return info;
1550 /* Create a new isl_union_access_info with the given sink accesses and
1551 * and no other accesses or schedule information.
1553 __isl_give isl_union_access_info *isl_union_access_info_from_sink(
1554 __isl_take isl_union_map *sink)
1556 isl_ctx *ctx;
1557 isl_union_access_info *access;
1559 if (!sink)
1560 return NULL;
1561 ctx = isl_union_map_get_ctx(sink);
1562 access = isl_union_access_info_alloc(ctx);
1563 if (!access)
1564 goto error;
1565 access->access[isl_access_sink] = sink;
1566 return isl_union_access_info_init(access);
1567 error:
1568 isl_union_map_free(sink);
1569 return NULL;
1572 /* Replace the access relation of type "type" of "info" by "access".
1574 static __isl_give isl_union_access_info *isl_union_access_info_set(
1575 __isl_take isl_union_access_info *info,
1576 enum isl_access_type type, __isl_take isl_union_map *access)
1578 if (!info || !access)
1579 goto error;
1581 isl_union_map_free(info->access[type]);
1582 info->access[type] = access;
1584 return info;
1585 error:
1586 isl_union_access_info_free(info);
1587 isl_union_map_free(access);
1588 return NULL;
1591 /* Replace the definite source accesses of "access" by "must_source".
1593 __isl_give isl_union_access_info *isl_union_access_info_set_must_source(
1594 __isl_take isl_union_access_info *access,
1595 __isl_take isl_union_map *must_source)
1597 return isl_union_access_info_set(access, isl_access_must_source,
1598 must_source);
1601 /* Replace the possible source accesses of "access" by "may_source".
1603 __isl_give isl_union_access_info *isl_union_access_info_set_may_source(
1604 __isl_take isl_union_access_info *access,
1605 __isl_take isl_union_map *may_source)
1607 return isl_union_access_info_set(access, isl_access_may_source,
1608 may_source);
1611 /* Replace the kills of "info" by "kill".
1613 __isl_give isl_union_access_info *isl_union_access_info_set_kill(
1614 __isl_take isl_union_access_info *info, __isl_take isl_union_map *kill)
1616 return isl_union_access_info_set(info, isl_access_kill, kill);
1619 /* Return the access relation of type "type" of "info".
1621 static __isl_give isl_union_map *isl_union_access_info_get(
1622 __isl_keep isl_union_access_info *info, enum isl_access_type type)
1624 if (!info)
1625 return NULL;
1626 return isl_union_map_copy(info->access[type]);
1629 /* Return the definite source accesses of "info".
1631 __isl_give isl_union_map *isl_union_access_info_get_must_source(
1632 __isl_keep isl_union_access_info *info)
1634 return isl_union_access_info_get(info, isl_access_must_source);
1637 /* Return the possible source accesses of "info".
1639 __isl_give isl_union_map *isl_union_access_info_get_may_source(
1640 __isl_keep isl_union_access_info *info)
1642 return isl_union_access_info_get(info, isl_access_may_source);
1645 /* Return the kills of "info".
1647 __isl_give isl_union_map *isl_union_access_info_get_kill(
1648 __isl_keep isl_union_access_info *info)
1650 return isl_union_access_info_get(info, isl_access_kill);
1653 /* Does "info" specify any kills?
1655 static isl_bool isl_union_access_has_kill(
1656 __isl_keep isl_union_access_info *info)
1658 isl_bool empty;
1660 if (!info)
1661 return isl_bool_error;
1662 empty = isl_union_map_is_empty(info->access[isl_access_kill]);
1663 return isl_bool_not(empty);
1666 /* Replace the schedule of "access" by "schedule".
1667 * Also free the schedule_map in case it was set last.
1669 __isl_give isl_union_access_info *isl_union_access_info_set_schedule(
1670 __isl_take isl_union_access_info *access,
1671 __isl_take isl_schedule *schedule)
1673 if (!access || !schedule)
1674 goto error;
1676 access->schedule_map = isl_union_map_free(access->schedule_map);
1677 isl_schedule_free(access->schedule);
1678 access->schedule = schedule;
1680 return access;
1681 error:
1682 isl_union_access_info_free(access);
1683 isl_schedule_free(schedule);
1684 return NULL;
1687 /* Replace the schedule map of "access" by "schedule_map".
1688 * Also free the schedule in case it was set last.
1690 __isl_give isl_union_access_info *isl_union_access_info_set_schedule_map(
1691 __isl_take isl_union_access_info *access,
1692 __isl_take isl_union_map *schedule_map)
1694 if (!access || !schedule_map)
1695 goto error;
1697 isl_union_map_free(access->schedule_map);
1698 access->schedule = isl_schedule_free(access->schedule);
1699 access->schedule_map = schedule_map;
1701 return access;
1702 error:
1703 isl_union_access_info_free(access);
1704 isl_union_map_free(schedule_map);
1705 return NULL;
1708 __isl_give isl_union_access_info *isl_union_access_info_copy(
1709 __isl_keep isl_union_access_info *access)
1711 isl_union_access_info *copy;
1712 enum isl_access_type i;
1714 if (!access)
1715 return NULL;
1716 copy = isl_union_access_info_from_sink(
1717 isl_union_map_copy(access->access[isl_access_sink]));
1718 for (i = isl_access_sink + 1; i < isl_access_end; ++i)
1719 copy = isl_union_access_info_set(copy, i,
1720 isl_union_map_copy(access->access[i]));
1721 if (access->schedule)
1722 copy = isl_union_access_info_set_schedule(copy,
1723 isl_schedule_copy(access->schedule));
1724 else
1725 copy = isl_union_access_info_set_schedule_map(copy,
1726 isl_union_map_copy(access->schedule_map));
1728 return copy;
1731 /* Print a key-value pair of a YAML mapping to "p",
1732 * with key "name" and value "umap".
1734 static __isl_give isl_printer *print_union_map_field(__isl_take isl_printer *p,
1735 const char *name, __isl_keep isl_union_map *umap)
1737 p = isl_printer_print_str(p, name);
1738 p = isl_printer_yaml_next(p);
1739 p = isl_printer_print_str(p, "\"");
1740 p = isl_printer_print_union_map(p, umap);
1741 p = isl_printer_print_str(p, "\"");
1742 p = isl_printer_yaml_next(p);
1744 return p;
1747 /* An enumeration of the various keys that may appear in a YAML mapping
1748 * of an isl_union_access_info object.
1749 * The keys for the access relation types are assumed to have the same values
1750 * as the access relation types in isl_access_type.
1752 enum isl_ai_key {
1753 isl_ai_key_error = -1,
1754 isl_ai_key_sink = isl_access_sink,
1755 isl_ai_key_must_source = isl_access_must_source,
1756 isl_ai_key_may_source = isl_access_may_source,
1757 isl_ai_key_kill = isl_access_kill,
1758 isl_ai_key_schedule_map,
1759 isl_ai_key_schedule,
1760 isl_ai_key_end
1763 /* Textual representations of the YAML keys for an isl_union_access_info
1764 * object.
1766 static char *key_str[] = {
1767 [isl_ai_key_sink] = "sink",
1768 [isl_ai_key_must_source] = "must_source",
1769 [isl_ai_key_may_source] = "may_source",
1770 [isl_ai_key_kill] = "kill",
1771 [isl_ai_key_schedule_map] = "schedule_map",
1772 [isl_ai_key_schedule] = "schedule",
1775 /* Print a key-value pair corresponding to the access relation of type "type"
1776 * of a YAML mapping of "info" to "p".
1778 * The sink access relation is always printed, but any other access relation
1779 * is only printed if it is non-empty.
1781 static __isl_give isl_printer *print_access_field(__isl_take isl_printer *p,
1782 __isl_keep isl_union_access_info *info, enum isl_access_type type)
1784 if (type != isl_access_sink) {
1785 isl_bool empty;
1787 empty = isl_union_map_is_empty(info->access[type]);
1788 if (empty < 0)
1789 return isl_printer_free(p);
1790 if (empty)
1791 return p;
1793 return print_union_map_field(p, key_str[type], info->access[type]);
1796 /* Print the information contained in "access" to "p".
1797 * The information is printed as a YAML document.
1799 __isl_give isl_printer *isl_printer_print_union_access_info(
1800 __isl_take isl_printer *p, __isl_keep isl_union_access_info *access)
1802 enum isl_access_type i;
1804 if (!access)
1805 return isl_printer_free(p);
1807 p = isl_printer_yaml_start_mapping(p);
1808 for (i = isl_access_sink; i < isl_access_end; ++i)
1809 p = print_access_field(p, access, i);
1810 if (access->schedule) {
1811 p = isl_printer_print_str(p, key_str[isl_ai_key_schedule]);
1812 p = isl_printer_yaml_next(p);
1813 p = isl_printer_print_schedule(p, access->schedule);
1814 p = isl_printer_yaml_next(p);
1815 } else {
1816 p = print_union_map_field(p, key_str[isl_ai_key_schedule_map],
1817 access->schedule_map);
1819 p = isl_printer_yaml_end_mapping(p);
1821 return p;
1824 /* Return a string representation of the information in "access".
1825 * The information is printed in flow format.
1827 __isl_give char *isl_union_access_info_to_str(
1828 __isl_keep isl_union_access_info *access)
1830 isl_printer *p;
1831 char *s;
1833 if (!access)
1834 return NULL;
1836 p = isl_printer_to_str(isl_union_access_info_get_ctx(access));
1837 p = isl_printer_set_yaml_style(p, ISL_YAML_STYLE_FLOW);
1838 p = isl_printer_print_union_access_info(p, access);
1839 s = isl_printer_get_str(p);
1840 isl_printer_free(p);
1842 return s;
1845 #undef KEY
1846 #define KEY enum isl_ai_key
1847 #undef KEY_ERROR
1848 #define KEY_ERROR isl_ai_key_error
1849 #undef KEY_END
1850 #define KEY_END isl_ai_key_end
1851 #include "extract_key.c"
1853 #undef BASE
1854 #define BASE union_map
1855 #include "read_in_string_templ.c"
1857 /* Read an isl_union_access_info object from "s".
1859 * Start off with an empty (invalid) isl_union_access_info object and
1860 * then fill up the fields based on the input.
1861 * The input needs to contain at least a description of the sink
1862 * access relation as well as some form of schedule.
1863 * The other access relations are set to empty relations
1864 * by isl_union_access_info_init if they are not specified in the input.
1866 __isl_give isl_union_access_info *isl_stream_read_union_access_info(
1867 isl_stream *s)
1869 isl_ctx *ctx;
1870 isl_union_access_info *info;
1871 int more;
1872 int sink_set = 0;
1873 int schedule_set = 0;
1875 if (isl_stream_yaml_read_start_mapping(s))
1876 return NULL;
1878 ctx = isl_stream_get_ctx(s);
1879 info = isl_union_access_info_alloc(ctx);
1880 while ((more = isl_stream_yaml_next(s)) > 0) {
1881 enum isl_ai_key key;
1882 isl_union_map *access, *schedule_map;
1883 isl_schedule *schedule;
1885 key = get_key(s);
1886 if (isl_stream_yaml_next(s) < 0)
1887 return isl_union_access_info_free(info);
1888 switch (key) {
1889 case isl_ai_key_end:
1890 case isl_ai_key_error:
1891 return isl_union_access_info_free(info);
1892 case isl_ai_key_sink:
1893 sink_set = 1;
1894 case isl_ai_key_must_source:
1895 case isl_ai_key_may_source:
1896 case isl_ai_key_kill:
1897 access = read_union_map(s);
1898 info = isl_union_access_info_set(info, key, access);
1899 if (!info)
1900 return NULL;
1901 break;
1902 case isl_ai_key_schedule_map:
1903 schedule_set = 1;
1904 schedule_map = read_union_map(s);
1905 info = isl_union_access_info_set_schedule_map(info,
1906 schedule_map);
1907 if (!info)
1908 return NULL;
1909 break;
1910 case isl_ai_key_schedule:
1911 schedule_set = 1;
1912 schedule = isl_stream_read_schedule(s);
1913 info = isl_union_access_info_set_schedule(info,
1914 schedule);
1915 if (!info)
1916 return NULL;
1917 break;
1920 if (more < 0)
1921 return isl_union_access_info_free(info);
1923 if (isl_stream_yaml_read_end_mapping(s) < 0) {
1924 isl_stream_error(s, NULL, "unexpected extra elements");
1925 return isl_union_access_info_free(info);
1928 if (!sink_set) {
1929 isl_stream_error(s, NULL, "no sink specified");
1930 return isl_union_access_info_free(info);
1933 if (!schedule_set) {
1934 isl_stream_error(s, NULL, "no schedule specified");
1935 return isl_union_access_info_free(info);
1938 return isl_union_access_info_init(info);
1941 /* Read an isl_union_access_info object from the file "input".
1943 __isl_give isl_union_access_info *isl_union_access_info_read_from_file(
1944 isl_ctx *ctx, FILE *input)
1946 isl_stream *s;
1947 isl_union_access_info *access;
1949 s = isl_stream_new_file(ctx, input);
1950 if (!s)
1951 return NULL;
1952 access = isl_stream_read_union_access_info(s);
1953 isl_stream_free(s);
1955 return access;
1958 /* Update the fields of "access" such that they all have the same parameters,
1959 * keeping in mind that the schedule_map field may be NULL and ignoring
1960 * the schedule field.
1962 static __isl_give isl_union_access_info *isl_union_access_info_align_params(
1963 __isl_take isl_union_access_info *access)
1965 isl_space *space;
1966 enum isl_access_type i;
1968 if (!access)
1969 return NULL;
1971 space = isl_union_map_get_space(access->access[isl_access_sink]);
1972 for (i = isl_access_sink + 1; i < isl_access_end; ++i)
1973 space = isl_space_align_params(space,
1974 isl_union_map_get_space(access->access[i]));
1975 if (access->schedule_map)
1976 space = isl_space_align_params(space,
1977 isl_union_map_get_space(access->schedule_map));
1978 for (i = isl_access_sink; i < isl_access_end; ++i)
1979 access->access[i] =
1980 isl_union_map_align_params(access->access[i],
1981 isl_space_copy(space));
1982 if (!access->schedule_map) {
1983 isl_space_free(space);
1984 } else {
1985 access->schedule_map =
1986 isl_union_map_align_params(access->schedule_map, space);
1987 if (!access->schedule_map)
1988 return isl_union_access_info_free(access);
1991 for (i = isl_access_sink; i < isl_access_end; ++i)
1992 if (!access->access[i])
1993 return isl_union_access_info_free(access);
1995 return access;
1998 /* Prepend the schedule dimensions to the iteration domains.
2000 * That is, if the schedule is of the form
2002 * D -> S
2004 * while the access relations are of the form
2006 * D -> A
2008 * then the updated access relations are of the form
2010 * [S -> D] -> A
2012 * The schedule map is also replaced by the map
2014 * [S -> D] -> D
2016 * that is used during the internal computation.
2017 * Neither the original schedule map nor this updated schedule map
2018 * are used after the call to this function.
2020 static __isl_give isl_union_access_info *
2021 isl_union_access_info_introduce_schedule(
2022 __isl_take isl_union_access_info *access)
2024 isl_union_map *sm;
2025 enum isl_access_type i;
2027 if (!access)
2028 return NULL;
2030 sm = isl_union_map_reverse(access->schedule_map);
2031 sm = isl_union_map_range_map(sm);
2032 for (i = isl_access_sink; i < isl_access_end; ++i)
2033 access->access[i] =
2034 isl_union_map_apply_range(isl_union_map_copy(sm),
2035 access->access[i]);
2036 access->schedule_map = sm;
2038 for (i = isl_access_sink; i < isl_access_end; ++i)
2039 if (!access->access[i])
2040 return isl_union_access_info_free(access);
2041 if (!access->schedule_map)
2042 return isl_union_access_info_free(access);
2044 return access;
2047 /* This structure represents the result of a dependence analysis computation.
2049 * "must_dep" represents the full definite dependences
2050 * "may_dep" represents the full non-definite dependences.
2051 * Both are of the form
2053 * [Source] -> [[Sink -> Data]]
2055 * (after the schedule dimensions have been projected out).
2056 * "must_no_source" represents the subset of the sink accesses for which
2057 * definitely no source was found.
2058 * "may_no_source" represents the subset of the sink accesses for which
2059 * possibly, but not definitely, no source was found.
2061 struct isl_union_flow {
2062 isl_union_map *must_dep;
2063 isl_union_map *may_dep;
2064 isl_union_map *must_no_source;
2065 isl_union_map *may_no_source;
2068 /* Return the isl_ctx to which "flow" belongs.
2070 isl_ctx *isl_union_flow_get_ctx(__isl_keep isl_union_flow *flow)
2072 return flow ? isl_union_map_get_ctx(flow->must_dep) : NULL;
2075 /* Free "flow" and return NULL.
2077 __isl_null isl_union_flow *isl_union_flow_free(__isl_take isl_union_flow *flow)
2079 if (!flow)
2080 return NULL;
2081 isl_union_map_free(flow->must_dep);
2082 isl_union_map_free(flow->may_dep);
2083 isl_union_map_free(flow->must_no_source);
2084 isl_union_map_free(flow->may_no_source);
2085 free(flow);
2086 return NULL;
2089 void isl_union_flow_dump(__isl_keep isl_union_flow *flow)
2091 if (!flow)
2092 return;
2094 fprintf(stderr, "must dependences: ");
2095 isl_union_map_dump(flow->must_dep);
2096 fprintf(stderr, "may dependences: ");
2097 isl_union_map_dump(flow->may_dep);
2098 fprintf(stderr, "must no source: ");
2099 isl_union_map_dump(flow->must_no_source);
2100 fprintf(stderr, "may no source: ");
2101 isl_union_map_dump(flow->may_no_source);
2104 /* Return the full definite dependences in "flow", with accessed elements.
2106 __isl_give isl_union_map *isl_union_flow_get_full_must_dependence(
2107 __isl_keep isl_union_flow *flow)
2109 if (!flow)
2110 return NULL;
2111 return isl_union_map_copy(flow->must_dep);
2114 /* Return the full possible dependences in "flow", including the definite
2115 * dependences, with accessed elements.
2117 __isl_give isl_union_map *isl_union_flow_get_full_may_dependence(
2118 __isl_keep isl_union_flow *flow)
2120 if (!flow)
2121 return NULL;
2122 return isl_union_map_union(isl_union_map_copy(flow->must_dep),
2123 isl_union_map_copy(flow->may_dep));
2126 /* Return the definite dependences in "flow", without the accessed elements.
2128 __isl_give isl_union_map *isl_union_flow_get_must_dependence(
2129 __isl_keep isl_union_flow *flow)
2131 isl_union_map *dep;
2133 if (!flow)
2134 return NULL;
2135 dep = isl_union_map_copy(flow->must_dep);
2136 return isl_union_map_range_factor_domain(dep);
2139 /* Return the possible dependences in "flow", including the definite
2140 * dependences, without the accessed elements.
2142 __isl_give isl_union_map *isl_union_flow_get_may_dependence(
2143 __isl_keep isl_union_flow *flow)
2145 isl_union_map *dep;
2147 if (!flow)
2148 return NULL;
2149 dep = isl_union_map_union(isl_union_map_copy(flow->must_dep),
2150 isl_union_map_copy(flow->may_dep));
2151 return isl_union_map_range_factor_domain(dep);
2154 /* Return the non-definite dependences in "flow".
2156 static __isl_give isl_union_map *isl_union_flow_get_non_must_dependence(
2157 __isl_keep isl_union_flow *flow)
2159 if (!flow)
2160 return NULL;
2161 return isl_union_map_copy(flow->may_dep);
2164 /* Return the subset of the sink accesses for which definitely
2165 * no source was found.
2167 __isl_give isl_union_map *isl_union_flow_get_must_no_source(
2168 __isl_keep isl_union_flow *flow)
2170 if (!flow)
2171 return NULL;
2172 return isl_union_map_copy(flow->must_no_source);
2175 /* Return the subset of the sink accesses for which possibly
2176 * no source was found, including those for which definitely
2177 * no source was found.
2179 __isl_give isl_union_map *isl_union_flow_get_may_no_source(
2180 __isl_keep isl_union_flow *flow)
2182 if (!flow)
2183 return NULL;
2184 return isl_union_map_union(isl_union_map_copy(flow->must_no_source),
2185 isl_union_map_copy(flow->may_no_source));
2188 /* Return the subset of the sink accesses for which possibly, but not
2189 * definitely, no source was found.
2191 static __isl_give isl_union_map *isl_union_flow_get_non_must_no_source(
2192 __isl_keep isl_union_flow *flow)
2194 if (!flow)
2195 return NULL;
2196 return isl_union_map_copy(flow->may_no_source);
2199 /* Create a new isl_union_flow object, initialized with empty
2200 * dependence relations and sink subsets.
2202 static __isl_give isl_union_flow *isl_union_flow_alloc(
2203 __isl_take isl_space *space)
2205 isl_ctx *ctx;
2206 isl_union_map *empty;
2207 isl_union_flow *flow;
2209 if (!space)
2210 return NULL;
2211 ctx = isl_space_get_ctx(space);
2212 flow = isl_alloc_type(ctx, isl_union_flow);
2213 if (!flow)
2214 goto error;
2216 empty = isl_union_map_empty(space);
2217 flow->must_dep = isl_union_map_copy(empty);
2218 flow->may_dep = isl_union_map_copy(empty);
2219 flow->must_no_source = isl_union_map_copy(empty);
2220 flow->may_no_source = empty;
2222 if (!flow->must_dep || !flow->may_dep ||
2223 !flow->must_no_source || !flow->may_no_source)
2224 return isl_union_flow_free(flow);
2226 return flow;
2227 error:
2228 isl_space_free(space);
2229 return NULL;
2232 /* Copy this isl_union_flow object.
2234 __isl_give isl_union_flow *isl_union_flow_copy(__isl_keep isl_union_flow *flow)
2236 isl_union_flow *copy;
2238 if (!flow)
2239 return NULL;
2241 copy = isl_union_flow_alloc(isl_union_map_get_space(flow->must_dep));
2243 if (!copy)
2244 return NULL;
2246 copy->must_dep = isl_union_map_union(copy->must_dep,
2247 isl_union_map_copy(flow->must_dep));
2248 copy->may_dep = isl_union_map_union(copy->may_dep,
2249 isl_union_map_copy(flow->may_dep));
2250 copy->must_no_source = isl_union_map_union(copy->must_no_source,
2251 isl_union_map_copy(flow->must_no_source));
2252 copy->may_no_source = isl_union_map_union(copy->may_no_source,
2253 isl_union_map_copy(flow->may_no_source));
2255 if (!copy->must_dep || !copy->may_dep ||
2256 !copy->must_no_source || !copy->may_no_source)
2257 return isl_union_flow_free(copy);
2259 return copy;
2262 /* Drop the schedule dimensions from the iteration domains in "flow".
2263 * In particular, the schedule dimensions have been prepended
2264 * to the iteration domains prior to the dependence analysis by
2265 * replacing the iteration domain D, by the wrapped map [S -> D].
2266 * Replace these wrapped maps by the original D.
2268 * In particular, the dependences computed by access_info_compute_flow_core
2269 * are of the form
2271 * [S -> D] -> [[S' -> D'] -> A]
2273 * The schedule dimensions are projected out by first currying the range,
2274 * resulting in
2276 * [S -> D] -> [S' -> [D' -> A]]
2278 * and then computing the factor range
2280 * D -> [D' -> A]
2282 static __isl_give isl_union_flow *isl_union_flow_drop_schedule(
2283 __isl_take isl_union_flow *flow)
2285 if (!flow)
2286 return NULL;
2288 flow->must_dep = isl_union_map_range_curry(flow->must_dep);
2289 flow->must_dep = isl_union_map_factor_range(flow->must_dep);
2290 flow->may_dep = isl_union_map_range_curry(flow->may_dep);
2291 flow->may_dep = isl_union_map_factor_range(flow->may_dep);
2292 flow->must_no_source =
2293 isl_union_map_domain_factor_range(flow->must_no_source);
2294 flow->may_no_source =
2295 isl_union_map_domain_factor_range(flow->may_no_source);
2297 if (!flow->must_dep || !flow->may_dep ||
2298 !flow->must_no_source || !flow->may_no_source)
2299 return isl_union_flow_free(flow);
2301 return flow;
2304 struct isl_compute_flow_data {
2305 isl_union_map *must_source;
2306 isl_union_map *may_source;
2307 isl_union_flow *flow;
2309 int count;
2310 int must;
2311 isl_space *dim;
2312 struct isl_sched_info *sink_info;
2313 struct isl_sched_info **source_info;
2314 isl_access_info *accesses;
2317 static isl_stat count_matching_array(__isl_take isl_map *map, void *user)
2319 int eq;
2320 isl_space *dim;
2321 struct isl_compute_flow_data *data;
2323 data = (struct isl_compute_flow_data *)user;
2325 dim = isl_space_range(isl_map_get_space(map));
2327 eq = isl_space_is_equal(dim, data->dim);
2329 isl_space_free(dim);
2330 isl_map_free(map);
2332 if (eq < 0)
2333 return isl_stat_error;
2334 if (eq)
2335 data->count++;
2337 return isl_stat_ok;
2340 static isl_stat collect_matching_array(__isl_take isl_map *map, void *user)
2342 int eq;
2343 isl_space *dim;
2344 struct isl_sched_info *info;
2345 struct isl_compute_flow_data *data;
2347 data = (struct isl_compute_flow_data *)user;
2349 dim = isl_space_range(isl_map_get_space(map));
2351 eq = isl_space_is_equal(dim, data->dim);
2353 isl_space_free(dim);
2355 if (eq < 0)
2356 goto error;
2357 if (!eq) {
2358 isl_map_free(map);
2359 return isl_stat_ok;
2362 info = sched_info_alloc(map);
2363 data->source_info[data->count] = info;
2365 data->accesses = isl_access_info_add_source(data->accesses,
2366 map, data->must, info);
2368 data->count++;
2370 return isl_stat_ok;
2371 error:
2372 isl_map_free(map);
2373 return isl_stat_error;
2376 /* Determine the shared nesting level and the "textual order" of
2377 * the given accesses.
2379 * We first determine the minimal schedule dimension for both accesses.
2381 * If among those dimensions, we can find one where both have a fixed
2382 * value and if moreover those values are different, then the previous
2383 * dimension is the last shared nesting level and the textual order
2384 * is determined based on the order of the fixed values.
2385 * If no such fixed values can be found, then we set the shared
2386 * nesting level to the minimal schedule dimension, with no textual ordering.
2388 static int before(void *first, void *second)
2390 struct isl_sched_info *info1 = first;
2391 struct isl_sched_info *info2 = second;
2392 int n1, n2;
2393 int i;
2395 n1 = isl_vec_size(info1->cst);
2396 n2 = isl_vec_size(info2->cst);
2398 if (n2 < n1)
2399 n1 = n2;
2401 for (i = 0; i < n1; ++i) {
2402 int r;
2403 int cmp;
2405 if (!info1->is_cst[i])
2406 continue;
2407 if (!info2->is_cst[i])
2408 continue;
2409 cmp = isl_vec_cmp_element(info1->cst, info2->cst, i);
2410 if (cmp == 0)
2411 continue;
2413 r = 2 * i + (cmp < 0);
2415 return r;
2418 return 2 * n1;
2421 /* Check if the given two accesses may be coscheduled.
2422 * If so, return 1. Otherwise return 0.
2424 * Two accesses may only be coscheduled if the fixed schedule
2425 * coordinates have the same values.
2427 static int coscheduled(void *first, void *second)
2429 struct isl_sched_info *info1 = first;
2430 struct isl_sched_info *info2 = second;
2431 int n1, n2;
2432 int i;
2434 n1 = isl_vec_size(info1->cst);
2435 n2 = isl_vec_size(info2->cst);
2437 if (n2 < n1)
2438 n1 = n2;
2440 for (i = 0; i < n1; ++i) {
2441 int cmp;
2443 if (!info1->is_cst[i])
2444 continue;
2445 if (!info2->is_cst[i])
2446 continue;
2447 cmp = isl_vec_cmp_element(info1->cst, info2->cst, i);
2448 if (cmp != 0)
2449 return 0;
2452 return 1;
2455 /* Given a sink access, look for all the source accesses that access
2456 * the same array and perform dataflow analysis on them using
2457 * isl_access_info_compute_flow_core.
2459 static isl_stat compute_flow(__isl_take isl_map *map, void *user)
2461 int i;
2462 isl_ctx *ctx;
2463 struct isl_compute_flow_data *data;
2464 isl_flow *flow;
2465 isl_union_flow *df;
2467 data = (struct isl_compute_flow_data *)user;
2468 df = data->flow;
2470 ctx = isl_map_get_ctx(map);
2472 data->accesses = NULL;
2473 data->sink_info = NULL;
2474 data->source_info = NULL;
2475 data->count = 0;
2476 data->dim = isl_space_range(isl_map_get_space(map));
2478 if (isl_union_map_foreach_map(data->must_source,
2479 &count_matching_array, data) < 0)
2480 goto error;
2481 if (isl_union_map_foreach_map(data->may_source,
2482 &count_matching_array, data) < 0)
2483 goto error;
2485 data->sink_info = sched_info_alloc(map);
2486 data->source_info = isl_calloc_array(ctx, struct isl_sched_info *,
2487 data->count);
2489 data->accesses = isl_access_info_alloc(isl_map_copy(map),
2490 data->sink_info, &before, data->count);
2491 if (!data->sink_info || (data->count && !data->source_info) ||
2492 !data->accesses)
2493 goto error;
2494 data->accesses->coscheduled = &coscheduled;
2495 data->count = 0;
2496 data->must = 1;
2497 if (isl_union_map_foreach_map(data->must_source,
2498 &collect_matching_array, data) < 0)
2499 goto error;
2500 data->must = 0;
2501 if (isl_union_map_foreach_map(data->may_source,
2502 &collect_matching_array, data) < 0)
2503 goto error;
2505 flow = access_info_compute_flow_core(data->accesses);
2506 data->accesses = NULL;
2508 if (!flow)
2509 goto error;
2511 df->must_no_source = isl_union_map_union(df->must_no_source,
2512 isl_union_map_from_map(isl_flow_get_no_source(flow, 1)));
2513 df->may_no_source = isl_union_map_union(df->may_no_source,
2514 isl_union_map_from_map(isl_flow_get_no_source(flow, 0)));
2516 for (i = 0; i < flow->n_source; ++i) {
2517 isl_union_map *dep;
2518 dep = isl_union_map_from_map(isl_map_copy(flow->dep[i].map));
2519 if (flow->dep[i].must)
2520 df->must_dep = isl_union_map_union(df->must_dep, dep);
2521 else
2522 df->may_dep = isl_union_map_union(df->may_dep, dep);
2525 isl_flow_free(flow);
2527 sched_info_free(data->sink_info);
2528 if (data->source_info) {
2529 for (i = 0; i < data->count; ++i)
2530 sched_info_free(data->source_info[i]);
2531 free(data->source_info);
2533 isl_space_free(data->dim);
2534 isl_map_free(map);
2536 return isl_stat_ok;
2537 error:
2538 isl_access_info_free(data->accesses);
2539 sched_info_free(data->sink_info);
2540 if (data->source_info) {
2541 for (i = 0; i < data->count; ++i)
2542 sched_info_free(data->source_info[i]);
2543 free(data->source_info);
2545 isl_space_free(data->dim);
2546 isl_map_free(map);
2548 return isl_stat_error;
2551 /* Add the kills of "info" to the must-sources.
2553 static __isl_give isl_union_access_info *
2554 isl_union_access_info_add_kill_to_must_source(
2555 __isl_take isl_union_access_info *info)
2557 isl_union_map *must, *kill;
2559 must = isl_union_access_info_get_must_source(info);
2560 kill = isl_union_access_info_get_kill(info);
2561 must = isl_union_map_union(must, kill);
2562 return isl_union_access_info_set_must_source(info, must);
2565 /* Drop dependences from "flow" that purely originate from kills.
2566 * That is, only keep those dependences that originate from
2567 * the original must-sources "must" and/or the original may-sources "may".
2568 * In particular, "must" contains the must-sources from before
2569 * the kills were added and "may" contains the may-source from before
2570 * the kills were removed.
2572 * The dependences are of the form
2574 * Source -> [Sink -> Data]
2576 * Only those dependences are kept where the Source -> Data part
2577 * is a subset of the original may-sources or must-sources.
2578 * Of those, only the must-dependences that intersect with the must-sources
2579 * remain must-dependences.
2580 * If there is some overlap between the may-sources and the must-sources,
2581 * then the may-dependences and must-dependences may also overlap.
2582 * This should be fine since the may-dependences are only kept
2583 * disjoint from the must-dependences for the isl_union_map_compute_flow
2584 * interface. This interface does not support kills, so it will
2585 * not end up calling this function.
2587 static __isl_give isl_union_flow *isl_union_flow_drop_kill_source(
2588 __isl_take isl_union_flow *flow, __isl_take isl_union_map *must,
2589 __isl_take isl_union_map *may)
2591 isl_union_map *move;
2593 if (!flow)
2594 goto error;
2595 move = isl_union_map_copy(flow->must_dep);
2596 move = isl_union_map_intersect_range_factor_range(move,
2597 isl_union_map_copy(may));
2598 may = isl_union_map_union(may, isl_union_map_copy(must));
2599 flow->may_dep = isl_union_map_intersect_range_factor_range(
2600 flow->may_dep, may);
2601 flow->must_dep = isl_union_map_intersect_range_factor_range(
2602 flow->must_dep, must);
2603 flow->may_dep = isl_union_map_union(flow->may_dep, move);
2604 if (!flow->must_dep || !flow->may_dep)
2605 return isl_union_flow_free(flow);
2607 return flow;
2608 error:
2609 isl_union_map_free(must);
2610 isl_union_map_free(may);
2611 return NULL;
2614 /* Remove the must accesses from the may accesses.
2616 * A must access always trumps a may access, so there is no need
2617 * for a must access to also be considered as a may access. Doing so
2618 * would only cost extra computations only to find out that
2619 * the duplicated may access does not make any difference.
2621 static __isl_give isl_union_access_info *isl_union_access_info_normalize(
2622 __isl_take isl_union_access_info *access)
2624 if (!access)
2625 return NULL;
2626 access->access[isl_access_may_source] =
2627 isl_union_map_subtract(access->access[isl_access_may_source],
2628 isl_union_map_copy(access->access[isl_access_must_source]));
2629 if (!access->access[isl_access_may_source])
2630 return isl_union_access_info_free(access);
2632 return access;
2635 /* Given a description of the "sink" accesses, the "source" accesses and
2636 * a schedule, compute for each instance of a sink access
2637 * and for each element accessed by that instance,
2638 * the possible or definite source accesses that last accessed the
2639 * element accessed by the sink access before this sink access
2640 * in the sense that there is no intermediate definite source access.
2642 * The must_no_source and may_no_source elements of the result
2643 * are subsets of access->sink. The elements must_dep and may_dep
2644 * map domain elements of access->{may,must)_source to
2645 * domain elements of access->sink.
2647 * This function is used when only the schedule map representation
2648 * is available.
2650 * We first prepend the schedule dimensions to the domain
2651 * of the accesses so that we can easily compare their relative order.
2652 * Then we consider each sink access individually in compute_flow.
2654 static __isl_give isl_union_flow *compute_flow_union_map(
2655 __isl_take isl_union_access_info *access)
2657 struct isl_compute_flow_data data;
2658 isl_union_map *sink;
2660 access = isl_union_access_info_align_params(access);
2661 access = isl_union_access_info_introduce_schedule(access);
2662 if (!access)
2663 return NULL;
2665 data.must_source = access->access[isl_access_must_source];
2666 data.may_source = access->access[isl_access_may_source];
2668 sink = access->access[isl_access_sink];
2669 data.flow = isl_union_flow_alloc(isl_union_map_get_space(sink));
2671 if (isl_union_map_foreach_map(sink, &compute_flow, &data) < 0)
2672 goto error;
2674 data.flow = isl_union_flow_drop_schedule(data.flow);
2676 isl_union_access_info_free(access);
2677 return data.flow;
2678 error:
2679 isl_union_access_info_free(access);
2680 isl_union_flow_free(data.flow);
2681 return NULL;
2684 /* A schedule access relation.
2686 * The access relation "access" is of the form [S -> D] -> A,
2687 * where S corresponds to the prefix schedule at "node".
2688 * "must" is only relevant for source accesses and indicates
2689 * whether the access is a must source or a may source.
2691 struct isl_scheduled_access {
2692 isl_map *access;
2693 int must;
2694 isl_schedule_node *node;
2697 /* Data structure for keeping track of individual scheduled sink and source
2698 * accesses when computing dependence analysis based on a schedule tree.
2700 * "n_sink" is the number of used entries in "sink"
2701 * "n_source" is the number of used entries in "source"
2703 * "set_sink", "must" and "node" are only used inside collect_sink_source,
2704 * to keep track of the current node and
2705 * of what extract_sink_source needs to do.
2707 struct isl_compute_flow_schedule_data {
2708 isl_union_access_info *access;
2710 int n_sink;
2711 int n_source;
2713 struct isl_scheduled_access *sink;
2714 struct isl_scheduled_access *source;
2716 int set_sink;
2717 int must;
2718 isl_schedule_node *node;
2721 /* Align the parameters of all sinks with all sources.
2723 * If there are no sinks or no sources, then no alignment is needed.
2725 static void isl_compute_flow_schedule_data_align_params(
2726 struct isl_compute_flow_schedule_data *data)
2728 int i;
2729 isl_space *space;
2731 if (data->n_sink == 0 || data->n_source == 0)
2732 return;
2734 space = isl_map_get_space(data->sink[0].access);
2736 for (i = 1; i < data->n_sink; ++i)
2737 space = isl_space_align_params(space,
2738 isl_map_get_space(data->sink[i].access));
2739 for (i = 0; i < data->n_source; ++i)
2740 space = isl_space_align_params(space,
2741 isl_map_get_space(data->source[i].access));
2743 for (i = 0; i < data->n_sink; ++i)
2744 data->sink[i].access =
2745 isl_map_align_params(data->sink[i].access,
2746 isl_space_copy(space));
2747 for (i = 0; i < data->n_source; ++i)
2748 data->source[i].access =
2749 isl_map_align_params(data->source[i].access,
2750 isl_space_copy(space));
2752 isl_space_free(space);
2755 /* Free all the memory referenced from "data".
2756 * Do not free "data" itself as it may be allocated on the stack.
2758 static void isl_compute_flow_schedule_data_clear(
2759 struct isl_compute_flow_schedule_data *data)
2761 int i;
2763 if (!data->sink)
2764 return;
2766 for (i = 0; i < data->n_sink; ++i) {
2767 isl_map_free(data->sink[i].access);
2768 isl_schedule_node_free(data->sink[i].node);
2771 for (i = 0; i < data->n_source; ++i) {
2772 isl_map_free(data->source[i].access);
2773 isl_schedule_node_free(data->source[i].node);
2776 free(data->sink);
2779 /* isl_schedule_foreach_schedule_node_top_down callback for counting
2780 * (an upper bound on) the number of sinks and sources.
2782 * Sinks and sources are only extracted at leaves of the tree,
2783 * so we skip the node if it is not a leaf.
2784 * Otherwise we increment data->n_sink and data->n_source with
2785 * the number of spaces in the sink and source access domains
2786 * that reach this node.
2788 static isl_bool count_sink_source(__isl_keep isl_schedule_node *node,
2789 void *user)
2791 struct isl_compute_flow_schedule_data *data = user;
2792 isl_union_set *domain;
2793 isl_union_map *umap;
2794 isl_bool r = isl_bool_false;
2796 if (isl_schedule_node_get_type(node) != isl_schedule_node_leaf)
2797 return isl_bool_true;
2799 domain = isl_schedule_node_get_universe_domain(node);
2801 umap = isl_union_map_copy(data->access->access[isl_access_sink]);
2802 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
2803 data->n_sink += isl_union_map_n_map(umap);
2804 isl_union_map_free(umap);
2805 if (!umap)
2806 r = isl_bool_error;
2808 umap = isl_union_map_copy(data->access->access[isl_access_must_source]);
2809 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
2810 data->n_source += isl_union_map_n_map(umap);
2811 isl_union_map_free(umap);
2812 if (!umap)
2813 r = isl_bool_error;
2815 umap = isl_union_map_copy(data->access->access[isl_access_may_source]);
2816 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
2817 data->n_source += isl_union_map_n_map(umap);
2818 isl_union_map_free(umap);
2819 if (!umap)
2820 r = isl_bool_error;
2822 isl_union_set_free(domain);
2824 return r;
2827 /* Add a single scheduled sink or source (depending on data->set_sink)
2828 * with scheduled access relation "map", must property data->must and
2829 * schedule node data->node to the list of sinks or sources.
2831 static isl_stat extract_sink_source(__isl_take isl_map *map, void *user)
2833 struct isl_compute_flow_schedule_data *data = user;
2834 struct isl_scheduled_access *access;
2836 if (data->set_sink)
2837 access = data->sink + data->n_sink++;
2838 else
2839 access = data->source + data->n_source++;
2841 access->access = map;
2842 access->must = data->must;
2843 access->node = isl_schedule_node_copy(data->node);
2845 return isl_stat_ok;
2848 /* isl_schedule_foreach_schedule_node_top_down callback for collecting
2849 * individual scheduled source and sink accesses (taking into account
2850 * the domain of the schedule).
2852 * We only collect accesses at the leaves of the schedule tree.
2853 * We prepend the schedule dimensions at the leaf to the iteration
2854 * domains of the source and sink accesses and then extract
2855 * the individual accesses (per space).
2857 * In particular, if the prefix schedule at the node is of the form
2859 * D -> S
2861 * while the access relations are of the form
2863 * D -> A
2865 * then the updated access relations are of the form
2867 * [S -> D] -> A
2869 * Note that S consists of a single space such that introducing S
2870 * in the access relations does not increase the number of spaces.
2872 static isl_bool collect_sink_source(__isl_keep isl_schedule_node *node,
2873 void *user)
2875 struct isl_compute_flow_schedule_data *data = user;
2876 isl_union_map *prefix;
2877 isl_union_map *umap;
2878 isl_bool r = isl_bool_false;
2880 if (isl_schedule_node_get_type(node) != isl_schedule_node_leaf)
2881 return isl_bool_true;
2883 data->node = node;
2885 prefix = isl_schedule_node_get_prefix_schedule_relation(node);
2886 prefix = isl_union_map_reverse(prefix);
2887 prefix = isl_union_map_range_map(prefix);
2889 data->set_sink = 1;
2890 umap = isl_union_map_copy(data->access->access[isl_access_sink]);
2891 umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
2892 if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
2893 r = isl_bool_error;
2894 isl_union_map_free(umap);
2896 data->set_sink = 0;
2897 data->must = 1;
2898 umap = isl_union_map_copy(data->access->access[isl_access_must_source]);
2899 umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
2900 if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
2901 r = isl_bool_error;
2902 isl_union_map_free(umap);
2904 data->set_sink = 0;
2905 data->must = 0;
2906 umap = isl_union_map_copy(data->access->access[isl_access_may_source]);
2907 umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
2908 if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
2909 r = isl_bool_error;
2910 isl_union_map_free(umap);
2912 isl_union_map_free(prefix);
2914 return r;
2917 /* isl_access_info_compute_flow callback for determining whether
2918 * the shared nesting level and the ordering within that level
2919 * for two scheduled accesses for use in compute_single_flow.
2921 * The tokens passed to this function refer to the leaves
2922 * in the schedule tree where the accesses take place.
2924 * If n is the shared number of loops, then we need to return
2925 * "2 * n + 1" if "first" precedes "second" inside the innermost
2926 * shared loop and "2 * n" otherwise.
2928 * The innermost shared ancestor may be the leaves themselves
2929 * if the accesses take place in the same leaf. Otherwise,
2930 * it is either a set node or a sequence node. Only in the case
2931 * of a sequence node do we consider one access to precede the other.
2933 static int before_node(void *first, void *second)
2935 isl_schedule_node *node1 = first;
2936 isl_schedule_node *node2 = second;
2937 isl_schedule_node *shared;
2938 int depth;
2939 int before = 0;
2941 shared = isl_schedule_node_get_shared_ancestor(node1, node2);
2942 if (!shared)
2943 return -1;
2945 depth = isl_schedule_node_get_schedule_depth(shared);
2946 if (isl_schedule_node_get_type(shared) == isl_schedule_node_sequence) {
2947 int pos1, pos2;
2949 pos1 = isl_schedule_node_get_ancestor_child_position(node1,
2950 shared);
2951 pos2 = isl_schedule_node_get_ancestor_child_position(node2,
2952 shared);
2953 before = pos1 < pos2;
2956 isl_schedule_node_free(shared);
2958 return 2 * depth + before;
2961 /* Check if the given two accesses may be coscheduled.
2962 * If so, return 1. Otherwise return 0.
2964 * Two accesses may only be coscheduled if they appear in the same leaf.
2966 static int coscheduled_node(void *first, void *second)
2968 isl_schedule_node *node1 = first;
2969 isl_schedule_node *node2 = second;
2971 return node1 == node2;
2974 /* Add the scheduled sources from "data" that access
2975 * the same data space as "sink" to "access".
2977 static __isl_give isl_access_info *add_matching_sources(
2978 __isl_take isl_access_info *access, struct isl_scheduled_access *sink,
2979 struct isl_compute_flow_schedule_data *data)
2981 int i;
2982 isl_space *space;
2984 space = isl_space_range(isl_map_get_space(sink->access));
2985 for (i = 0; i < data->n_source; ++i) {
2986 struct isl_scheduled_access *source;
2987 isl_space *source_space;
2988 int eq;
2990 source = &data->source[i];
2991 source_space = isl_map_get_space(source->access);
2992 source_space = isl_space_range(source_space);
2993 eq = isl_space_is_equal(space, source_space);
2994 isl_space_free(source_space);
2996 if (!eq)
2997 continue;
2998 if (eq < 0)
2999 goto error;
3001 access = isl_access_info_add_source(access,
3002 isl_map_copy(source->access), source->must, source->node);
3005 isl_space_free(space);
3006 return access;
3007 error:
3008 isl_space_free(space);
3009 isl_access_info_free(access);
3010 return NULL;
3013 /* Given a scheduled sink access relation "sink", compute the corresponding
3014 * dependences on the sources in "data" and add the computed dependences
3015 * to "uf".
3017 * The dependences computed by access_info_compute_flow_core are of the form
3019 * [S -> I] -> [[S' -> I'] -> A]
3021 * The schedule dimensions are projected out by first currying the range,
3022 * resulting in
3024 * [S -> I] -> [S' -> [I' -> A]]
3026 * and then computing the factor range
3028 * I -> [I' -> A]
3030 static __isl_give isl_union_flow *compute_single_flow(
3031 __isl_take isl_union_flow *uf, struct isl_scheduled_access *sink,
3032 struct isl_compute_flow_schedule_data *data)
3034 int i;
3035 isl_access_info *access;
3036 isl_flow *flow;
3037 isl_map *map;
3039 if (!uf)
3040 return NULL;
3042 access = isl_access_info_alloc(isl_map_copy(sink->access), sink->node,
3043 &before_node, data->n_source);
3044 if (access)
3045 access->coscheduled = &coscheduled_node;
3046 access = add_matching_sources(access, sink, data);
3048 flow = access_info_compute_flow_core(access);
3049 if (!flow)
3050 return isl_union_flow_free(uf);
3052 map = isl_map_domain_factor_range(isl_flow_get_no_source(flow, 1));
3053 uf->must_no_source = isl_union_map_union(uf->must_no_source,
3054 isl_union_map_from_map(map));
3055 map = isl_map_domain_factor_range(isl_flow_get_no_source(flow, 0));
3056 uf->may_no_source = isl_union_map_union(uf->may_no_source,
3057 isl_union_map_from_map(map));
3059 for (i = 0; i < flow->n_source; ++i) {
3060 isl_union_map *dep;
3062 map = isl_map_range_curry(isl_map_copy(flow->dep[i].map));
3063 map = isl_map_factor_range(map);
3064 dep = isl_union_map_from_map(map);
3065 if (flow->dep[i].must)
3066 uf->must_dep = isl_union_map_union(uf->must_dep, dep);
3067 else
3068 uf->may_dep = isl_union_map_union(uf->may_dep, dep);
3071 isl_flow_free(flow);
3073 return uf;
3076 /* Given a description of the "sink" accesses, the "source" accesses and
3077 * a schedule, compute for each instance of a sink access
3078 * and for each element accessed by that instance,
3079 * the possible or definite source accesses that last accessed the
3080 * element accessed by the sink access before this sink access
3081 * in the sense that there is no intermediate definite source access.
3082 * Only consider dependences between statement instances that belong
3083 * to the domain of the schedule.
3085 * The must_no_source and may_no_source elements of the result
3086 * are subsets of access->sink. The elements must_dep and may_dep
3087 * map domain elements of access->{may,must)_source to
3088 * domain elements of access->sink.
3090 * This function is used when a schedule tree representation
3091 * is available.
3093 * We extract the individual scheduled source and sink access relations
3094 * (taking into account the domain of the schedule) and
3095 * then compute dependences for each scheduled sink individually.
3097 static __isl_give isl_union_flow *compute_flow_schedule(
3098 __isl_take isl_union_access_info *access)
3100 struct isl_compute_flow_schedule_data data = { access };
3101 int i, n;
3102 isl_ctx *ctx;
3103 isl_space *space;
3104 isl_union_flow *flow;
3106 ctx = isl_union_access_info_get_ctx(access);
3108 data.n_sink = 0;
3109 data.n_source = 0;
3110 if (isl_schedule_foreach_schedule_node_top_down(access->schedule,
3111 &count_sink_source, &data) < 0)
3112 goto error;
3114 n = data.n_sink + data.n_source;
3115 data.sink = isl_calloc_array(ctx, struct isl_scheduled_access, n);
3116 if (n && !data.sink)
3117 goto error;
3118 data.source = data.sink + data.n_sink;
3120 data.n_sink = 0;
3121 data.n_source = 0;
3122 if (isl_schedule_foreach_schedule_node_top_down(access->schedule,
3123 &collect_sink_source, &data) < 0)
3124 goto error;
3126 space = isl_union_map_get_space(access->access[isl_access_sink]);
3127 flow = isl_union_flow_alloc(space);
3129 isl_compute_flow_schedule_data_align_params(&data);
3131 for (i = 0; i < data.n_sink; ++i)
3132 flow = compute_single_flow(flow, &data.sink[i], &data);
3134 isl_compute_flow_schedule_data_clear(&data);
3136 isl_union_access_info_free(access);
3137 return flow;
3138 error:
3139 isl_union_access_info_free(access);
3140 isl_compute_flow_schedule_data_clear(&data);
3141 return NULL;
3144 /* Given a description of the "sink" accesses, the "source" accesses and
3145 * a schedule, compute for each instance of a sink access
3146 * and for each element accessed by that instance,
3147 * the possible or definite source accesses that last accessed the
3148 * element accessed by the sink access before this sink access
3149 * in the sense that there is no intermediate definite source access.
3151 * The must_no_source and may_no_source elements of the result
3152 * are subsets of access->sink. The elements must_dep and may_dep
3153 * map domain elements of access->{may,must)_source to
3154 * domain elements of access->sink.
3156 * If any kills have been specified, then they are treated as
3157 * must-sources internally. Any dependence that purely derives
3158 * from an original kill is removed from the output.
3160 * We check whether the schedule is available as a schedule tree
3161 * or a schedule map and call the corresponding function to perform
3162 * the analysis.
3164 __isl_give isl_union_flow *isl_union_access_info_compute_flow(
3165 __isl_take isl_union_access_info *access)
3167 isl_bool has_kill;
3168 isl_union_map *must = NULL, *may = NULL;
3169 isl_union_flow *flow;
3171 has_kill = isl_union_access_has_kill(access);
3172 if (has_kill < 0)
3173 goto error;
3174 if (has_kill) {
3175 must = isl_union_access_info_get_must_source(access);
3176 may = isl_union_access_info_get_may_source(access);
3178 access = isl_union_access_info_add_kill_to_must_source(access);
3179 access = isl_union_access_info_normalize(access);
3180 if (!access)
3181 goto error;
3182 if (access->schedule)
3183 flow = compute_flow_schedule(access);
3184 else
3185 flow = compute_flow_union_map(access);
3186 if (has_kill)
3187 flow = isl_union_flow_drop_kill_source(flow, must, may);
3188 return flow;
3189 error:
3190 isl_union_access_info_free(access);
3191 isl_union_map_free(must);
3192 isl_union_map_free(may);
3193 return NULL;
3196 /* Print the information contained in "flow" to "p".
3197 * The information is printed as a YAML document.
3199 __isl_give isl_printer *isl_printer_print_union_flow(
3200 __isl_take isl_printer *p, __isl_keep isl_union_flow *flow)
3202 isl_union_map *umap;
3204 if (!flow)
3205 return isl_printer_free(p);
3207 p = isl_printer_yaml_start_mapping(p);
3208 umap = isl_union_flow_get_full_must_dependence(flow);
3209 p = print_union_map_field(p, "must_dependence", umap);
3210 isl_union_map_free(umap);
3211 umap = isl_union_flow_get_full_may_dependence(flow);
3212 p = print_union_map_field(p, "may_dependence", umap);
3213 isl_union_map_free(umap);
3214 p = print_union_map_field(p, "must_no_source", flow->must_no_source);
3215 umap = isl_union_flow_get_may_no_source(flow);
3216 p = print_union_map_field(p, "may_no_source", umap);
3217 isl_union_map_free(umap);
3218 p = isl_printer_yaml_end_mapping(p);
3220 return p;
3223 /* Return a string representation of the information in "flow".
3224 * The information is printed in flow format.
3226 __isl_give char *isl_union_flow_to_str(__isl_keep isl_union_flow *flow)
3228 isl_printer *p;
3229 char *s;
3231 if (!flow)
3232 return NULL;
3234 p = isl_printer_to_str(isl_union_flow_get_ctx(flow));
3235 p = isl_printer_set_yaml_style(p, ISL_YAML_STYLE_FLOW);
3236 p = isl_printer_print_union_flow(p, flow);
3237 s = isl_printer_get_str(p);
3238 isl_printer_free(p);
3240 return s;
3243 /* Given a collection of "sink" and "source" accesses,
3244 * compute for each iteration of a sink access
3245 * and for each element accessed by that iteration,
3246 * the source access in the list that last accessed the
3247 * element accessed by the sink access before this sink access.
3248 * Each access is given as a map from the loop iterators
3249 * to the array indices.
3250 * The result is a relations between source and sink
3251 * iterations and a subset of the domain of the sink accesses,
3252 * corresponding to those iterations that access an element
3253 * not previously accessed.
3255 * We collect the inputs in an isl_union_access_info object,
3256 * call isl_union_access_info_compute_flow and extract
3257 * the outputs from the result.
3259 int isl_union_map_compute_flow(__isl_take isl_union_map *sink,
3260 __isl_take isl_union_map *must_source,
3261 __isl_take isl_union_map *may_source,
3262 __isl_take isl_union_map *schedule,
3263 __isl_give isl_union_map **must_dep, __isl_give isl_union_map **may_dep,
3264 __isl_give isl_union_map **must_no_source,
3265 __isl_give isl_union_map **may_no_source)
3267 isl_union_access_info *access;
3268 isl_union_flow *flow;
3270 access = isl_union_access_info_from_sink(sink);
3271 access = isl_union_access_info_set_must_source(access, must_source);
3272 access = isl_union_access_info_set_may_source(access, may_source);
3273 access = isl_union_access_info_set_schedule_map(access, schedule);
3274 flow = isl_union_access_info_compute_flow(access);
3276 if (must_dep)
3277 *must_dep = isl_union_flow_get_must_dependence(flow);
3278 if (may_dep)
3279 *may_dep = isl_union_flow_get_non_must_dependence(flow);
3280 if (must_no_source)
3281 *must_no_source = isl_union_flow_get_must_no_source(flow);
3282 if (may_no_source)
3283 *may_no_source = isl_union_flow_get_non_must_no_source(flow);
3285 isl_union_flow_free(flow);
3287 if ((must_dep && !*must_dep) || (may_dep && !*may_dep) ||
3288 (must_no_source && !*must_no_source) ||
3289 (may_no_source && !*may_no_source))
3290 goto error;
3292 return 0;
3293 error:
3294 if (must_dep)
3295 *must_dep = isl_union_map_free(*must_dep);
3296 if (may_dep)
3297 *may_dep = isl_union_map_free(*may_dep);
3298 if (must_no_source)
3299 *must_no_source = isl_union_map_free(*must_no_source);
3300 if (may_no_source)
3301 *may_no_source = isl_union_map_free(*may_no_source);
3302 return -1;