add isl_aff_from_range
[isl.git] / isl_flow.c
blob946be84e4b0b930c145040fbfbefb52aad31cd22
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/val.h>
20 #include <isl/space.h>
21 #include <isl/set.h>
22 #include <isl/map.h>
23 #include <isl/union_set.h>
24 #include <isl/union_map.h>
25 #include <isl/flow.h>
26 #include <isl/schedule_node.h>
27 #include <isl_sort.h>
28 #include <isl/stream.h>
30 enum isl_restriction_type {
31 isl_restriction_type_empty,
32 isl_restriction_type_none,
33 isl_restriction_type_input,
34 isl_restriction_type_output
37 struct isl_restriction {
38 enum isl_restriction_type type;
40 isl_set *source;
41 isl_set *sink;
44 /* Create a restriction of the given type.
46 static __isl_give isl_restriction *isl_restriction_alloc(
47 __isl_take isl_map *source_map, enum isl_restriction_type type)
49 isl_ctx *ctx;
50 isl_restriction *restr;
52 if (!source_map)
53 return NULL;
55 ctx = isl_map_get_ctx(source_map);
56 restr = isl_calloc_type(ctx, struct isl_restriction);
57 if (!restr)
58 goto error;
60 restr->type = type;
62 isl_map_free(source_map);
63 return restr;
64 error:
65 isl_map_free(source_map);
66 return NULL;
69 /* Create a restriction that doesn't restrict anything.
71 __isl_give isl_restriction *isl_restriction_none(__isl_take isl_map *source_map)
73 return isl_restriction_alloc(source_map, isl_restriction_type_none);
76 /* Create a restriction that removes everything.
78 __isl_give isl_restriction *isl_restriction_empty(
79 __isl_take isl_map *source_map)
81 return isl_restriction_alloc(source_map, isl_restriction_type_empty);
84 /* Create a restriction on the input of the maximization problem
85 * based on the given source and sink restrictions.
87 __isl_give isl_restriction *isl_restriction_input(
88 __isl_take isl_set *source_restr, __isl_take isl_set *sink_restr)
90 isl_ctx *ctx;
91 isl_restriction *restr;
93 if (!source_restr || !sink_restr)
94 goto error;
96 ctx = isl_set_get_ctx(source_restr);
97 restr = isl_calloc_type(ctx, struct isl_restriction);
98 if (!restr)
99 goto error;
101 restr->type = isl_restriction_type_input;
102 restr->source = source_restr;
103 restr->sink = sink_restr;
105 return restr;
106 error:
107 isl_set_free(source_restr);
108 isl_set_free(sink_restr);
109 return NULL;
112 /* Create a restriction on the output of the maximization problem
113 * based on the given source restriction.
115 __isl_give isl_restriction *isl_restriction_output(
116 __isl_take isl_set *source_restr)
118 isl_ctx *ctx;
119 isl_restriction *restr;
121 if (!source_restr)
122 return NULL;
124 ctx = isl_set_get_ctx(source_restr);
125 restr = isl_calloc_type(ctx, struct isl_restriction);
126 if (!restr)
127 goto error;
129 restr->type = isl_restriction_type_output;
130 restr->source = source_restr;
132 return restr;
133 error:
134 isl_set_free(source_restr);
135 return NULL;
138 __isl_null isl_restriction *isl_restriction_free(
139 __isl_take isl_restriction *restr)
141 if (!restr)
142 return NULL;
144 isl_set_free(restr->source);
145 isl_set_free(restr->sink);
146 free(restr);
147 return NULL;
150 isl_ctx *isl_restriction_get_ctx(__isl_keep isl_restriction *restr)
152 return restr ? isl_set_get_ctx(restr->source) : NULL;
155 /* A private structure to keep track of a mapping together with
156 * a user-specified identifier and a boolean indicating whether
157 * the map represents a must or may access/dependence.
159 struct isl_labeled_map {
160 struct isl_map *map;
161 void *data;
162 int must;
165 typedef int (*isl_access_coscheduled)(void *first, void *second);
167 /* A structure containing the input for dependence analysis:
168 * - a sink
169 * - n_must + n_may (<= max_source) sources
170 * - a function for determining the relative order of sources and sink
171 * - an optional function "coscheduled" for determining whether sources
172 * may be coscheduled. If "coscheduled" is NULL, then the sources
173 * are assumed not to be coscheduled.
174 * The must sources are placed before the may sources.
176 * domain_map is an auxiliary map that maps the sink access relation
177 * to the domain of this access relation.
178 * This field is only needed when restrict_fn is set and
179 * the field itself is set by isl_access_info_compute_flow.
181 * restrict_fn is a callback that (if not NULL) will be called
182 * right before any lexicographical maximization.
184 struct isl_access_info {
185 isl_map *domain_map;
186 struct isl_labeled_map sink;
187 isl_access_level_before level_before;
188 isl_access_coscheduled coscheduled;
190 isl_access_restrict restrict_fn;
191 void *restrict_user;
193 int max_source;
194 int n_must;
195 int n_may;
196 struct isl_labeled_map source[1];
199 /* A structure containing the output of dependence analysis:
200 * - n_source dependences
201 * - a wrapped subset of the sink for which definitely no source could be found
202 * - a wrapped subset of the sink for which possibly no source could be found
204 struct isl_flow {
205 isl_set *must_no_source;
206 isl_set *may_no_source;
207 int n_source;
208 struct isl_labeled_map *dep;
211 /* Construct an isl_access_info structure and fill it up with
212 * the given data. The number of sources is set to 0.
214 __isl_give isl_access_info *isl_access_info_alloc(__isl_take isl_map *sink,
215 void *sink_user, isl_access_level_before fn, int max_source)
217 isl_ctx *ctx;
218 struct isl_access_info *acc;
220 if (!sink)
221 return NULL;
223 ctx = isl_map_get_ctx(sink);
224 isl_assert(ctx, max_source >= 0, goto error);
226 acc = isl_calloc(ctx, struct isl_access_info,
227 sizeof(struct isl_access_info) +
228 (max_source - 1) * sizeof(struct isl_labeled_map));
229 if (!acc)
230 goto error;
232 acc->sink.map = sink;
233 acc->sink.data = sink_user;
234 acc->level_before = fn;
235 acc->max_source = max_source;
236 acc->n_must = 0;
237 acc->n_may = 0;
239 return acc;
240 error:
241 isl_map_free(sink);
242 return NULL;
245 /* Free the given isl_access_info structure.
247 __isl_null isl_access_info *isl_access_info_free(
248 __isl_take isl_access_info *acc)
250 int i;
252 if (!acc)
253 return NULL;
254 isl_map_free(acc->domain_map);
255 isl_map_free(acc->sink.map);
256 for (i = 0; i < acc->n_must + acc->n_may; ++i)
257 isl_map_free(acc->source[i].map);
258 free(acc);
259 return NULL;
262 isl_ctx *isl_access_info_get_ctx(__isl_keep isl_access_info *acc)
264 return acc ? isl_map_get_ctx(acc->sink.map) : NULL;
267 __isl_give isl_access_info *isl_access_info_set_restrict(
268 __isl_take isl_access_info *acc, isl_access_restrict fn, void *user)
270 if (!acc)
271 return NULL;
272 acc->restrict_fn = fn;
273 acc->restrict_user = user;
274 return acc;
277 /* Add another source to an isl_access_info structure, making
278 * sure the "must" sources are placed before the "may" sources.
279 * This function may be called at most max_source times on a
280 * given isl_access_info structure, with max_source as specified
281 * in the call to isl_access_info_alloc that constructed the structure.
283 __isl_give isl_access_info *isl_access_info_add_source(
284 __isl_take isl_access_info *acc, __isl_take isl_map *source,
285 int must, void *source_user)
287 isl_ctx *ctx;
289 if (!acc)
290 goto error;
291 ctx = isl_map_get_ctx(acc->sink.map);
292 isl_assert(ctx, acc->n_must + acc->n_may < acc->max_source, goto error);
294 if (must) {
295 if (acc->n_may)
296 acc->source[acc->n_must + acc->n_may] =
297 acc->source[acc->n_must];
298 acc->source[acc->n_must].map = source;
299 acc->source[acc->n_must].data = source_user;
300 acc->source[acc->n_must].must = 1;
301 acc->n_must++;
302 } else {
303 acc->source[acc->n_must + acc->n_may].map = source;
304 acc->source[acc->n_must + acc->n_may].data = source_user;
305 acc->source[acc->n_must + acc->n_may].must = 0;
306 acc->n_may++;
309 return acc;
310 error:
311 isl_map_free(source);
312 isl_access_info_free(acc);
313 return NULL;
316 /* A helper struct carrying the isl_access_info and an error condition.
318 struct access_sort_info {
319 isl_access_info *access_info;
320 int error;
323 /* Return -n, 0 or n (with n a positive value), depending on whether
324 * the source access identified by p1 should be sorted before, together
325 * or after that identified by p2.
327 * If p1 appears before p2, then it should be sorted first.
328 * For more generic initial schedules, it is possible that neither
329 * p1 nor p2 appears before the other, or at least not in any obvious way.
330 * We therefore also check if p2 appears before p1, in which case p2
331 * should be sorted first.
332 * If not, we try to order the two statements based on the description
333 * of the iteration domains. This results in an arbitrary, but fairly
334 * stable ordering.
336 * In case of an error, sort_info.error is set to true and all elements are
337 * reported to be equal.
339 static int access_sort_cmp(const void *p1, const void *p2, void *user)
341 struct access_sort_info *sort_info = user;
342 isl_access_info *acc = sort_info->access_info;
344 if (sort_info->error)
345 return 0;
347 const struct isl_labeled_map *i1, *i2;
348 int level1, level2;
349 uint32_t h1, h2;
350 i1 = (const struct isl_labeled_map *) p1;
351 i2 = (const struct isl_labeled_map *) p2;
353 level1 = acc->level_before(i1->data, i2->data);
354 if (level1 < 0)
355 goto error;
356 if (level1 % 2)
357 return -1;
359 level2 = acc->level_before(i2->data, i1->data);
360 if (level2 < 0)
361 goto error;
362 if (level2 % 2)
363 return 1;
365 h1 = isl_map_get_hash(i1->map);
366 h2 = isl_map_get_hash(i2->map);
367 return h1 > h2 ? 1 : h1 < h2 ? -1 : 0;
368 error:
369 sort_info->error = 1;
370 return 0;
373 /* Sort the must source accesses in their textual order.
375 static __isl_give isl_access_info *isl_access_info_sort_sources(
376 __isl_take isl_access_info *acc)
378 struct access_sort_info sort_info;
380 sort_info.access_info = acc;
381 sort_info.error = 0;
383 if (!acc)
384 return NULL;
385 if (acc->n_must <= 1)
386 return acc;
388 if (isl_sort(acc->source, acc->n_must, sizeof(struct isl_labeled_map),
389 access_sort_cmp, &sort_info) < 0)
390 return isl_access_info_free(acc);
391 if (sort_info.error)
392 return isl_access_info_free(acc);
394 return acc;
397 /* Align the parameters of the two spaces if needed and then call
398 * isl_space_join.
400 static __isl_give isl_space *space_align_and_join(__isl_take isl_space *left,
401 __isl_take isl_space *right)
403 isl_bool equal_params;
405 equal_params = isl_space_has_equal_params(left, right);
406 if (equal_params < 0)
407 goto error;
408 if (equal_params)
409 return isl_space_join(left, right);
411 left = isl_space_align_params(left, isl_space_copy(right));
412 right = isl_space_align_params(right, isl_space_copy(left));
413 return isl_space_join(left, right);
414 error:
415 isl_space_free(left);
416 isl_space_free(right);
417 return NULL;
420 /* Initialize an empty isl_flow structure corresponding to a given
421 * isl_access_info structure.
422 * For each must access, two dependences are created (initialized
423 * to the empty relation), one for the resulting must dependences
424 * and one for the resulting may dependences. May accesses can
425 * only lead to may dependences, so only one dependence is created
426 * for each of them.
427 * This function is private as isl_flow structures are only supposed
428 * to be created by isl_access_info_compute_flow.
430 static __isl_give isl_flow *isl_flow_alloc(__isl_keep isl_access_info *acc)
432 int i, n;
433 struct isl_ctx *ctx;
434 struct isl_flow *dep;
436 if (!acc)
437 return NULL;
439 ctx = isl_map_get_ctx(acc->sink.map);
440 dep = isl_calloc_type(ctx, struct isl_flow);
441 if (!dep)
442 return NULL;
444 n = 2 * acc->n_must + acc->n_may;
445 dep->dep = isl_calloc_array(ctx, struct isl_labeled_map, n);
446 if (n && !dep->dep)
447 goto error;
449 dep->n_source = n;
450 for (i = 0; i < acc->n_must; ++i) {
451 isl_space *dim;
452 dim = space_align_and_join(
453 isl_map_get_space(acc->source[i].map),
454 isl_space_reverse(isl_map_get_space(acc->sink.map)));
455 dep->dep[2 * i].map = isl_map_empty(dim);
456 dep->dep[2 * i + 1].map = isl_map_copy(dep->dep[2 * i].map);
457 dep->dep[2 * i].data = acc->source[i].data;
458 dep->dep[2 * i + 1].data = acc->source[i].data;
459 dep->dep[2 * i].must = 1;
460 dep->dep[2 * i + 1].must = 0;
461 if (!dep->dep[2 * i].map || !dep->dep[2 * i + 1].map)
462 goto error;
464 for (i = acc->n_must; i < acc->n_must + acc->n_may; ++i) {
465 isl_space *dim;
466 dim = space_align_and_join(
467 isl_map_get_space(acc->source[i].map),
468 isl_space_reverse(isl_map_get_space(acc->sink.map)));
469 dep->dep[acc->n_must + i].map = isl_map_empty(dim);
470 dep->dep[acc->n_must + i].data = acc->source[i].data;
471 dep->dep[acc->n_must + i].must = 0;
472 if (!dep->dep[acc->n_must + i].map)
473 goto error;
476 return dep;
477 error:
478 isl_flow_free(dep);
479 return NULL;
482 /* Iterate over all sources and for each resulting flow dependence
483 * that is not empty, call the user specfied function.
484 * The second argument in this function call identifies the source,
485 * while the third argument correspond to the final argument of
486 * the isl_flow_foreach call.
488 isl_stat isl_flow_foreach(__isl_keep isl_flow *deps,
489 isl_stat (*fn)(__isl_take isl_map *dep, int must, void *dep_user,
490 void *user),
491 void *user)
493 int i;
495 if (!deps)
496 return isl_stat_error;
498 for (i = 0; i < deps->n_source; ++i) {
499 if (isl_map_plain_is_empty(deps->dep[i].map))
500 continue;
501 if (fn(isl_map_copy(deps->dep[i].map), deps->dep[i].must,
502 deps->dep[i].data, user) < 0)
503 return isl_stat_error;
506 return isl_stat_ok;
509 /* Return a copy of the subset of the sink for which no source could be found.
511 __isl_give isl_map *isl_flow_get_no_source(__isl_keep isl_flow *deps, int must)
513 if (!deps)
514 return NULL;
516 if (must)
517 return isl_set_unwrap(isl_set_copy(deps->must_no_source));
518 else
519 return isl_set_unwrap(isl_set_copy(deps->may_no_source));
522 void isl_flow_free(__isl_take isl_flow *deps)
524 int i;
526 if (!deps)
527 return;
528 isl_set_free(deps->must_no_source);
529 isl_set_free(deps->may_no_source);
530 if (deps->dep) {
531 for (i = 0; i < deps->n_source; ++i)
532 isl_map_free(deps->dep[i].map);
533 free(deps->dep);
535 free(deps);
538 isl_ctx *isl_flow_get_ctx(__isl_keep isl_flow *deps)
540 return deps ? isl_set_get_ctx(deps->must_no_source) : NULL;
543 /* Return a map that enforces that the domain iteration occurs after
544 * the range iteration at the given level.
545 * If level is odd, then the domain iteration should occur after
546 * the target iteration in their shared level/2 outermost loops.
547 * In this case we simply need to enforce that these outermost
548 * loop iterations are the same.
549 * If level is even, then the loop iterator of the domain should
550 * be greater than the loop iterator of the range at the last
551 * of the level/2 shared loops, i.e., loop level/2 - 1.
553 static __isl_give isl_map *after_at_level(__isl_take isl_space *dim, int level)
555 struct isl_basic_map *bmap;
557 if (level % 2)
558 bmap = isl_basic_map_equal(dim, level/2);
559 else
560 bmap = isl_basic_map_more_at(dim, level/2 - 1);
562 return isl_map_from_basic_map(bmap);
565 /* Compute the partial lexicographic maximum of "dep" on domain "sink",
566 * but first check if the user has set acc->restrict_fn and if so
567 * update either the input or the output of the maximization problem
568 * with respect to the resulting restriction.
570 * Since the user expects a mapping from sink iterations to source iterations,
571 * whereas the domain of "dep" is a wrapped map, mapping sink iterations
572 * to accessed array elements, we first need to project out the accessed
573 * sink array elements by applying acc->domain_map.
574 * Similarly, the sink restriction specified by the user needs to be
575 * converted back to the wrapped map.
577 static __isl_give isl_map *restricted_partial_lexmax(
578 __isl_keep isl_access_info *acc, __isl_take isl_map *dep,
579 int source, __isl_take isl_set *sink, __isl_give isl_set **empty)
581 isl_map *source_map;
582 isl_restriction *restr;
583 isl_set *sink_domain;
584 isl_set *sink_restr;
585 isl_map *res;
587 if (!acc->restrict_fn)
588 return isl_map_partial_lexmax(dep, sink, empty);
590 source_map = isl_map_copy(dep);
591 source_map = isl_map_apply_domain(source_map,
592 isl_map_copy(acc->domain_map));
593 sink_domain = isl_set_copy(sink);
594 sink_domain = isl_set_apply(sink_domain, isl_map_copy(acc->domain_map));
595 restr = acc->restrict_fn(source_map, sink_domain,
596 acc->source[source].data, acc->restrict_user);
597 isl_set_free(sink_domain);
598 isl_map_free(source_map);
600 if (!restr)
601 goto error;
602 if (restr->type == isl_restriction_type_input) {
603 dep = isl_map_intersect_range(dep, isl_set_copy(restr->source));
604 sink_restr = isl_set_copy(restr->sink);
605 sink_restr = isl_set_apply(sink_restr,
606 isl_map_reverse(isl_map_copy(acc->domain_map)));
607 sink = isl_set_intersect(sink, sink_restr);
608 } else if (restr->type == isl_restriction_type_empty) {
609 isl_space *space = isl_map_get_space(dep);
610 isl_map_free(dep);
611 dep = isl_map_empty(space);
614 res = isl_map_partial_lexmax(dep, sink, empty);
616 if (restr->type == isl_restriction_type_output)
617 res = isl_map_intersect_range(res, isl_set_copy(restr->source));
619 isl_restriction_free(restr);
620 return res;
621 error:
622 isl_map_free(dep);
623 isl_set_free(sink);
624 *empty = NULL;
625 return NULL;
628 /* Compute the last iteration of must source j that precedes the sink
629 * at the given level for sink iterations in set_C.
630 * The subset of set_C for which no such iteration can be found is returned
631 * in *empty.
633 static struct isl_map *last_source(struct isl_access_info *acc,
634 struct isl_set *set_C,
635 int j, int level, struct isl_set **empty)
637 struct isl_map *read_map;
638 struct isl_map *write_map;
639 struct isl_map *dep_map;
640 struct isl_map *after;
641 struct isl_map *result;
643 read_map = isl_map_copy(acc->sink.map);
644 write_map = isl_map_copy(acc->source[j].map);
645 write_map = isl_map_reverse(write_map);
646 dep_map = isl_map_apply_range(read_map, write_map);
647 after = after_at_level(isl_map_get_space(dep_map), level);
648 dep_map = isl_map_intersect(dep_map, after);
649 result = restricted_partial_lexmax(acc, dep_map, j, set_C, empty);
650 result = isl_map_reverse(result);
652 return result;
655 /* For a given mapping between iterations of must source j and iterations
656 * of the sink, compute the last iteration of must source k preceding
657 * the sink at level before_level for any of the sink iterations,
658 * but following the corresponding iteration of must source j at level
659 * after_level.
661 static struct isl_map *last_later_source(struct isl_access_info *acc,
662 struct isl_map *old_map,
663 int j, int before_level,
664 int k, int after_level,
665 struct isl_set **empty)
667 isl_space *dim;
668 struct isl_set *set_C;
669 struct isl_map *read_map;
670 struct isl_map *write_map;
671 struct isl_map *dep_map;
672 struct isl_map *after_write;
673 struct isl_map *before_read;
674 struct isl_map *result;
676 set_C = isl_map_range(isl_map_copy(old_map));
677 read_map = isl_map_copy(acc->sink.map);
678 write_map = isl_map_copy(acc->source[k].map);
680 write_map = isl_map_reverse(write_map);
681 dep_map = isl_map_apply_range(read_map, write_map);
682 dim = space_align_and_join(isl_map_get_space(acc->source[k].map),
683 isl_space_reverse(isl_map_get_space(acc->source[j].map)));
684 after_write = after_at_level(dim, after_level);
685 after_write = isl_map_apply_range(after_write, old_map);
686 after_write = isl_map_reverse(after_write);
687 dep_map = isl_map_intersect(dep_map, after_write);
688 before_read = after_at_level(isl_map_get_space(dep_map), before_level);
689 dep_map = isl_map_intersect(dep_map, before_read);
690 result = restricted_partial_lexmax(acc, dep_map, k, set_C, empty);
691 result = isl_map_reverse(result);
693 return result;
696 /* Given a shared_level between two accesses, return 1 if the
697 * the first can precede the second at the requested target_level.
698 * If the target level is odd, i.e., refers to a statement level
699 * dimension, then first needs to precede second at the requested
700 * level, i.e., shared_level must be equal to target_level.
701 * If the target level is odd, then the two loops should share
702 * at least the requested number of outer loops.
704 static int can_precede_at_level(int shared_level, int target_level)
706 if (shared_level < target_level)
707 return 0;
708 if ((target_level % 2) && shared_level > target_level)
709 return 0;
710 return 1;
713 /* Given a possible flow dependence temp_rel[j] between source j and the sink
714 * at level sink_level, remove those elements for which
715 * there is an iteration of another source k < j that is closer to the sink.
716 * The flow dependences temp_rel[k] are updated with the improved sources.
717 * Any improved source needs to precede the sink at the same level
718 * and needs to follow source j at the same or a deeper level.
719 * The lower this level, the later the execution date of source k.
720 * We therefore consider lower levels first.
722 * If temp_rel[j] is empty, then there can be no improvement and
723 * we return immediately.
725 * This function returns isl_stat_ok in case it was executed successfully and
726 * isl_stat_error in case of errors during the execution of this function.
728 static isl_stat intermediate_sources(__isl_keep isl_access_info *acc,
729 struct isl_map **temp_rel, int j, int sink_level)
731 int k, level;
732 int depth = 2 * isl_map_dim(acc->source[j].map, isl_dim_in) + 1;
734 if (isl_map_plain_is_empty(temp_rel[j]))
735 return isl_stat_ok;
737 for (k = j - 1; k >= 0; --k) {
738 int plevel, plevel2;
739 plevel = acc->level_before(acc->source[k].data, acc->sink.data);
740 if (plevel < 0)
741 return isl_stat_error;
742 if (!can_precede_at_level(plevel, sink_level))
743 continue;
745 plevel2 = acc->level_before(acc->source[j].data,
746 acc->source[k].data);
747 if (plevel2 < 0)
748 return isl_stat_error;
750 for (level = sink_level; level <= depth; ++level) {
751 struct isl_map *T;
752 struct isl_set *trest;
753 struct isl_map *copy;
755 if (!can_precede_at_level(plevel2, level))
756 continue;
758 copy = isl_map_copy(temp_rel[j]);
759 T = last_later_source(acc, copy, j, sink_level, k,
760 level, &trest);
761 if (isl_map_plain_is_empty(T)) {
762 isl_set_free(trest);
763 isl_map_free(T);
764 continue;
766 temp_rel[j] = isl_map_intersect_range(temp_rel[j], trest);
767 temp_rel[k] = isl_map_union_disjoint(temp_rel[k], T);
771 return isl_stat_ok;
774 /* Compute all iterations of may source j that precedes the sink at the given
775 * level for sink iterations in set_C.
777 static __isl_give isl_map *all_sources(__isl_keep isl_access_info *acc,
778 __isl_take isl_set *set_C, int j, int level)
780 isl_map *read_map;
781 isl_map *write_map;
782 isl_map *dep_map;
783 isl_map *after;
785 read_map = isl_map_copy(acc->sink.map);
786 read_map = isl_map_intersect_domain(read_map, set_C);
787 write_map = isl_map_copy(acc->source[acc->n_must + j].map);
788 write_map = isl_map_reverse(write_map);
789 dep_map = isl_map_apply_range(read_map, write_map);
790 after = after_at_level(isl_map_get_space(dep_map), level);
791 dep_map = isl_map_intersect(dep_map, after);
793 return isl_map_reverse(dep_map);
796 /* For a given mapping between iterations of must source k and iterations
797 * of the sink, compute all iterations of may source j preceding
798 * the sink at level before_level for any of the sink iterations,
799 * but following the corresponding iteration of must source k at level
800 * after_level.
802 static __isl_give isl_map *all_later_sources(__isl_keep isl_access_info *acc,
803 __isl_take isl_map *old_map,
804 int j, int before_level, int k, int after_level)
806 isl_space *dim;
807 isl_set *set_C;
808 isl_map *read_map;
809 isl_map *write_map;
810 isl_map *dep_map;
811 isl_map *after_write;
812 isl_map *before_read;
814 set_C = isl_map_range(isl_map_copy(old_map));
815 read_map = isl_map_copy(acc->sink.map);
816 read_map = isl_map_intersect_domain(read_map, set_C);
817 write_map = isl_map_copy(acc->source[acc->n_must + j].map);
819 write_map = isl_map_reverse(write_map);
820 dep_map = isl_map_apply_range(read_map, write_map);
821 dim = isl_space_join(isl_map_get_space(acc->source[acc->n_must + j].map),
822 isl_space_reverse(isl_map_get_space(acc->source[k].map)));
823 after_write = after_at_level(dim, after_level);
824 after_write = isl_map_apply_range(after_write, old_map);
825 after_write = isl_map_reverse(after_write);
826 dep_map = isl_map_intersect(dep_map, after_write);
827 before_read = after_at_level(isl_map_get_space(dep_map), before_level);
828 dep_map = isl_map_intersect(dep_map, before_read);
829 return isl_map_reverse(dep_map);
832 /* Given the must and may dependence relations for the must accesses
833 * for level sink_level, check if there are any accesses of may access j
834 * that occur in between and return their union.
835 * If some of these accesses are intermediate with respect to
836 * (previously thought to be) must dependences, then these
837 * must dependences are turned into may dependences.
839 static __isl_give isl_map *all_intermediate_sources(
840 __isl_keep isl_access_info *acc, __isl_take isl_map *map,
841 struct isl_map **must_rel, struct isl_map **may_rel,
842 int j, int sink_level)
844 int k, level;
845 int depth = 2 * isl_map_dim(acc->source[acc->n_must + j].map,
846 isl_dim_in) + 1;
848 for (k = 0; k < acc->n_must; ++k) {
849 int plevel;
851 if (isl_map_plain_is_empty(may_rel[k]) &&
852 isl_map_plain_is_empty(must_rel[k]))
853 continue;
855 plevel = acc->level_before(acc->source[k].data,
856 acc->source[acc->n_must + j].data);
857 if (plevel < 0)
858 return isl_map_free(map);
860 for (level = sink_level; level <= depth; ++level) {
861 isl_map *T;
862 isl_map *copy;
863 isl_set *ran;
865 if (!can_precede_at_level(plevel, level))
866 continue;
868 copy = isl_map_copy(may_rel[k]);
869 T = all_later_sources(acc, copy, j, sink_level, k, level);
870 map = isl_map_union(map, T);
872 copy = isl_map_copy(must_rel[k]);
873 T = all_later_sources(acc, copy, j, sink_level, k, level);
874 ran = isl_map_range(isl_map_copy(T));
875 map = isl_map_union(map, T);
876 may_rel[k] = isl_map_union_disjoint(may_rel[k],
877 isl_map_intersect_range(isl_map_copy(must_rel[k]),
878 isl_set_copy(ran)));
879 T = isl_map_from_domain_and_range(
880 isl_set_universe(
881 isl_space_domain(isl_map_get_space(must_rel[k]))),
882 ran);
883 must_rel[k] = isl_map_subtract(must_rel[k], T);
887 return map;
890 /* Given a dependence relation "old_map" between a must-source and the sink,
891 * return a subset of the dependences, augmented with instances
892 * of the source at position "pos" in "acc" that are coscheduled
893 * with the must-source and that access the same element.
894 * That is, if the input lives in a space T -> K, then the output
895 * lives in the space [T -> S] -> K, with S the space of source "pos", and
896 * the domain factor of the domain product is a subset of the input.
897 * The sources are considered to be coscheduled if they have the same values
898 * for the initial "depth" coordinates.
900 * First construct a dependence relation S -> K and a mapping
901 * between coscheduled sources T -> S.
902 * The second is combined with the original dependence relation T -> K
903 * to form a relation in T -> [S -> K], which is subsequently
904 * uncurried to [T -> S] -> K.
905 * This result is then intersected with the dependence relation S -> K
906 * to form the output.
908 * In case a negative depth is given, NULL is returned to indicate an error.
910 static __isl_give isl_map *coscheduled_source(__isl_keep isl_access_info *acc,
911 __isl_keep isl_map *old_map, int pos, int depth)
913 isl_space *space;
914 isl_set *set_C;
915 isl_map *read_map;
916 isl_map *write_map;
917 isl_map *dep_map;
918 isl_map *equal;
919 isl_map *map;
921 if (depth < 0)
922 return NULL;
924 set_C = isl_map_range(isl_map_copy(old_map));
925 read_map = isl_map_copy(acc->sink.map);
926 read_map = isl_map_intersect_domain(read_map, set_C);
927 write_map = isl_map_copy(acc->source[pos].map);
928 dep_map = isl_map_domain_product(write_map, read_map);
929 dep_map = isl_set_unwrap(isl_map_domain(dep_map));
930 space = isl_space_join(isl_map_get_space(old_map),
931 isl_space_reverse(isl_map_get_space(dep_map)));
932 equal = isl_map_from_basic_map(isl_basic_map_equal(space, depth));
933 map = isl_map_range_product(equal, isl_map_copy(old_map));
934 map = isl_map_uncurry(map);
935 map = isl_map_intersect_domain_factor_range(map, dep_map);
937 return map;
940 /* After the dependences derived from a must-source have been computed
941 * at a certain level, check if any of the sources of the must-dependences
942 * may be coscheduled with other sources.
943 * If they are any such sources, then there is no way of determining
944 * which of the sources actually comes last and the must-dependences
945 * need to be turned into may-dependences, while dependences from
946 * the other sources need to be added to the may-dependences as well.
947 * "acc" describes the sources and a callback for checking whether
948 * two sources may be coscheduled. If acc->coscheduled is NULL then
949 * the sources are assumed not to be coscheduled.
950 * "must_rel" and "may_rel" describe the must and may-dependence relations
951 * computed at the current level for the must-sources. Some of the dependences
952 * may be moved from "must_rel" to "may_rel".
953 * "flow" contains all dependences computed so far (apart from those
954 * in "must_rel" and "may_rel") and may be updated with additional
955 * dependences derived from may-sources.
957 * In particular, consider all the must-sources with a non-empty
958 * dependence relation in "must_rel". They are considered in reverse
959 * order because that is the order in which they are considered in the caller.
960 * If any of the must-sources are coscheduled, then the last one
961 * is the one that will have a corresponding dependence relation.
962 * For each must-source i, consider both all the previous must-sources
963 * and all the may-sources. If any of those may be coscheduled with
964 * must-source i, then compute the coscheduled instances that access
965 * the same memory elements. The result is a relation [T -> S] -> K.
966 * The projection onto T -> K is a subset of the must-dependence relation
967 * that needs to be turned into may-dependences.
968 * The projection onto S -> K needs to be added to the may-dependences
969 * of source S.
970 * Since a given must-source instance may be coscheduled with several
971 * other source instances, the dependences that need to be turned
972 * into may-dependences are first collected and only actually removed
973 * from the must-dependences after all other sources have been considered.
975 static __isl_give isl_flow *handle_coscheduled(__isl_keep isl_access_info *acc,
976 __isl_keep isl_map **must_rel, __isl_keep isl_map **may_rel,
977 __isl_take isl_flow *flow)
979 int i, j;
981 if (!acc->coscheduled)
982 return flow;
983 for (i = acc->n_must - 1; i >= 0; --i) {
984 isl_map *move;
986 if (isl_map_plain_is_empty(must_rel[i]))
987 continue;
988 move = isl_map_empty(isl_map_get_space(must_rel[i]));
989 for (j = i - 1; j >= 0; --j) {
990 int depth;
991 isl_map *map, *factor;
993 if (!acc->coscheduled(acc->source[i].data,
994 acc->source[j].data))
995 continue;
996 depth = acc->level_before(acc->source[i].data,
997 acc->source[j].data) / 2;
998 map = coscheduled_source(acc, must_rel[i], j, depth);
999 factor = isl_map_domain_factor_range(isl_map_copy(map));
1000 may_rel[j] = isl_map_union(may_rel[j], factor);
1001 map = isl_map_domain_factor_domain(map);
1002 move = isl_map_union(move, map);
1004 for (j = 0; j < acc->n_may; ++j) {
1005 int depth, pos;
1006 isl_map *map, *factor;
1008 pos = acc->n_must + j;
1009 if (!acc->coscheduled(acc->source[i].data,
1010 acc->source[pos].data))
1011 continue;
1012 depth = acc->level_before(acc->source[i].data,
1013 acc->source[pos].data) / 2;
1014 map = coscheduled_source(acc, must_rel[i], pos, depth);
1015 factor = isl_map_domain_factor_range(isl_map_copy(map));
1016 pos = 2 * acc->n_must + j;
1017 flow->dep[pos].map = isl_map_union(flow->dep[pos].map,
1018 factor);
1019 map = isl_map_domain_factor_domain(map);
1020 move = isl_map_union(move, map);
1022 must_rel[i] = isl_map_subtract(must_rel[i], isl_map_copy(move));
1023 may_rel[i] = isl_map_union(may_rel[i], move);
1026 return flow;
1029 /* Compute dependences for the case where all accesses are "may"
1030 * accesses, which boils down to computing memory based dependences.
1031 * The generic algorithm would also work in this case, but it would
1032 * be overkill to use it.
1034 static __isl_give isl_flow *compute_mem_based_dependences(
1035 __isl_keep isl_access_info *acc)
1037 int i;
1038 isl_set *mustdo;
1039 isl_set *maydo;
1040 isl_flow *res;
1042 res = isl_flow_alloc(acc);
1043 if (!res)
1044 return NULL;
1046 mustdo = isl_map_domain(isl_map_copy(acc->sink.map));
1047 maydo = isl_set_copy(mustdo);
1049 for (i = 0; i < acc->n_may; ++i) {
1050 int plevel;
1051 int is_before;
1052 isl_space *dim;
1053 isl_map *before;
1054 isl_map *dep;
1056 plevel = acc->level_before(acc->source[i].data, acc->sink.data);
1057 if (plevel < 0)
1058 goto error;
1060 is_before = plevel & 1;
1061 plevel >>= 1;
1063 dim = isl_map_get_space(res->dep[i].map);
1064 if (is_before)
1065 before = isl_map_lex_le_first(dim, plevel);
1066 else
1067 before = isl_map_lex_lt_first(dim, plevel);
1068 dep = isl_map_apply_range(isl_map_copy(acc->source[i].map),
1069 isl_map_reverse(isl_map_copy(acc->sink.map)));
1070 dep = isl_map_intersect(dep, before);
1071 mustdo = isl_set_subtract(mustdo,
1072 isl_map_range(isl_map_copy(dep)));
1073 res->dep[i].map = isl_map_union(res->dep[i].map, dep);
1076 res->may_no_source = isl_set_subtract(maydo, isl_set_copy(mustdo));
1077 res->must_no_source = mustdo;
1079 return res;
1080 error:
1081 isl_set_free(mustdo);
1082 isl_set_free(maydo);
1083 isl_flow_free(res);
1084 return NULL;
1087 /* Compute dependences for the case where there is at least one
1088 * "must" access.
1090 * The core algorithm considers all levels in which a source may precede
1091 * the sink, where a level may either be a statement level or a loop level.
1092 * The outermost statement level is 1, the first loop level is 2, etc...
1093 * The algorithm basically does the following:
1094 * for all levels l of the read access from innermost to outermost
1095 * for all sources w that may precede the sink access at that level
1096 * compute the last iteration of the source that precedes the sink access
1097 * at that level
1098 * add result to possible last accesses at level l of source w
1099 * for all sources w2 that we haven't considered yet at this level that may
1100 * also precede the sink access
1101 * for all levels l2 of w from l to innermost
1102 * for all possible last accesses dep of w at l
1103 * compute last iteration of w2 between the source and sink
1104 * of dep
1105 * add result to possible last accesses at level l of write w2
1106 * and replace possible last accesses dep by the remainder
1109 * The above algorithm is applied to the must access. During the course
1110 * of the algorithm, we keep track of sink iterations that still
1111 * need to be considered. These iterations are split into those that
1112 * haven't been matched to any source access (mustdo) and those that have only
1113 * been matched to may accesses (maydo).
1114 * At the end of each level, must-sources and may-sources that are coscheduled
1115 * with the sources of the must-dependences at that level are considered.
1116 * If any coscheduled instances are found, then corresponding may-dependences
1117 * are added and the original must-dependences are turned into may-dependences.
1118 * Afterwards, the may accesses that occur after must-dependence sources
1119 * are considered.
1120 * In particular, we consider may accesses that precede the remaining
1121 * sink iterations, moving elements from mustdo to maydo when appropriate,
1122 * and may accesses that occur between a must source and a sink of any
1123 * dependences found at the current level, turning must dependences into
1124 * may dependences when appropriate.
1127 static __isl_give isl_flow *compute_val_based_dependences(
1128 __isl_keep isl_access_info *acc)
1130 isl_ctx *ctx;
1131 isl_flow *res;
1132 isl_set *mustdo = NULL;
1133 isl_set *maydo = NULL;
1134 int level, j;
1135 int depth;
1136 isl_map **must_rel = NULL;
1137 isl_map **may_rel = NULL;
1139 if (!acc)
1140 return NULL;
1142 res = isl_flow_alloc(acc);
1143 if (!res)
1144 goto error;
1145 ctx = isl_map_get_ctx(acc->sink.map);
1147 depth = 2 * isl_map_dim(acc->sink.map, isl_dim_in) + 1;
1148 mustdo = isl_map_domain(isl_map_copy(acc->sink.map));
1149 maydo = isl_set_empty(isl_set_get_space(mustdo));
1150 if (!mustdo || !maydo)
1151 goto error;
1152 if (isl_set_plain_is_empty(mustdo))
1153 goto done;
1155 must_rel = isl_calloc_array(ctx, struct isl_map *, acc->n_must);
1156 may_rel = isl_calloc_array(ctx, struct isl_map *, acc->n_must);
1157 if (!must_rel || !may_rel)
1158 goto error;
1160 for (level = depth; level >= 1; --level) {
1161 for (j = acc->n_must-1; j >=0; --j) {
1162 isl_space *space;
1163 space = isl_map_get_space(res->dep[2 * j].map);
1164 must_rel[j] = isl_map_empty(space);
1165 may_rel[j] = isl_map_copy(must_rel[j]);
1168 for (j = acc->n_must - 1; j >= 0; --j) {
1169 struct isl_map *T;
1170 struct isl_set *rest;
1171 int plevel;
1173 plevel = acc->level_before(acc->source[j].data,
1174 acc->sink.data);
1175 if (plevel < 0)
1176 goto error;
1177 if (!can_precede_at_level(plevel, level))
1178 continue;
1180 T = last_source(acc, mustdo, j, level, &rest);
1181 must_rel[j] = isl_map_union_disjoint(must_rel[j], T);
1182 mustdo = rest;
1184 if (intermediate_sources(acc, must_rel, j, level) < 0)
1185 goto error;
1187 T = last_source(acc, maydo, j, level, &rest);
1188 may_rel[j] = isl_map_union_disjoint(may_rel[j], T);
1189 maydo = rest;
1191 if (intermediate_sources(acc, may_rel, j, level) < 0)
1192 goto error;
1194 if (isl_set_plain_is_empty(mustdo) &&
1195 isl_set_plain_is_empty(maydo))
1196 break;
1198 for (j = j - 1; j >= 0; --j) {
1199 int plevel;
1201 plevel = acc->level_before(acc->source[j].data,
1202 acc->sink.data);
1203 if (plevel < 0)
1204 goto error;
1205 if (!can_precede_at_level(plevel, level))
1206 continue;
1208 if (intermediate_sources(acc, must_rel, j, level) < 0)
1209 goto error;
1210 if (intermediate_sources(acc, may_rel, j, level) < 0)
1211 goto error;
1214 handle_coscheduled(acc, must_rel, may_rel, res);
1216 for (j = 0; j < acc->n_may; ++j) {
1217 int plevel;
1218 isl_map *T;
1219 isl_set *ran;
1221 plevel = acc->level_before(acc->source[acc->n_must + j].data,
1222 acc->sink.data);
1223 if (plevel < 0)
1224 goto error;
1225 if (!can_precede_at_level(plevel, level))
1226 continue;
1228 T = all_sources(acc, isl_set_copy(maydo), j, level);
1229 res->dep[2 * acc->n_must + j].map =
1230 isl_map_union(res->dep[2 * acc->n_must + j].map, T);
1231 T = all_sources(acc, isl_set_copy(mustdo), j, level);
1232 ran = isl_map_range(isl_map_copy(T));
1233 res->dep[2 * acc->n_must + j].map =
1234 isl_map_union(res->dep[2 * acc->n_must + j].map, T);
1235 mustdo = isl_set_subtract(mustdo, isl_set_copy(ran));
1236 maydo = isl_set_union_disjoint(maydo, ran);
1238 T = res->dep[2 * acc->n_must + j].map;
1239 T = all_intermediate_sources(acc, T, must_rel, may_rel,
1240 j, level);
1241 res->dep[2 * acc->n_must + j].map = T;
1244 for (j = acc->n_must - 1; j >= 0; --j) {
1245 res->dep[2 * j].map =
1246 isl_map_union_disjoint(res->dep[2 * j].map,
1247 must_rel[j]);
1248 res->dep[2 * j + 1].map =
1249 isl_map_union_disjoint(res->dep[2 * j + 1].map,
1250 may_rel[j]);
1253 if (isl_set_plain_is_empty(mustdo) &&
1254 isl_set_plain_is_empty(maydo))
1255 break;
1258 free(must_rel);
1259 free(may_rel);
1260 done:
1261 res->must_no_source = mustdo;
1262 res->may_no_source = maydo;
1263 return res;
1264 error:
1265 if (must_rel)
1266 for (j = 0; j < acc->n_must; ++j)
1267 isl_map_free(must_rel[j]);
1268 if (may_rel)
1269 for (j = 0; j < acc->n_must; ++j)
1270 isl_map_free(may_rel[j]);
1271 isl_flow_free(res);
1272 isl_set_free(mustdo);
1273 isl_set_free(maydo);
1274 free(must_rel);
1275 free(may_rel);
1276 return NULL;
1279 /* Given a "sink" access, a list of n "source" accesses,
1280 * compute for each iteration of the sink access
1281 * and for each element accessed by that iteration,
1282 * the source access in the list that last accessed the
1283 * element accessed by the sink access before this sink access.
1284 * Each access is given as a map from the loop iterators
1285 * to the array indices.
1286 * The result is a list of n relations between source and sink
1287 * iterations and a subset of the domain of the sink access,
1288 * corresponding to those iterations that access an element
1289 * not previously accessed.
1291 * To deal with multi-valued sink access relations, the sink iteration
1292 * domain is first extended with dimensions that correspond to the data
1293 * space. However, these extra dimensions are not projected out again.
1294 * It is up to the caller to decide whether these dimensions should be kept.
1296 static __isl_give isl_flow *access_info_compute_flow_core(
1297 __isl_take isl_access_info *acc)
1299 struct isl_flow *res = NULL;
1301 if (!acc)
1302 return NULL;
1304 acc->sink.map = isl_map_range_map(acc->sink.map);
1305 if (!acc->sink.map)
1306 goto error;
1308 if (acc->n_must == 0)
1309 res = compute_mem_based_dependences(acc);
1310 else {
1311 acc = isl_access_info_sort_sources(acc);
1312 res = compute_val_based_dependences(acc);
1314 acc = isl_access_info_free(acc);
1315 if (!res)
1316 return NULL;
1317 if (!res->must_no_source || !res->may_no_source)
1318 goto error;
1319 return res;
1320 error:
1321 isl_access_info_free(acc);
1322 isl_flow_free(res);
1323 return NULL;
1326 /* Given a "sink" access, a list of n "source" accesses,
1327 * compute for each iteration of the sink access
1328 * and for each element accessed by that iteration,
1329 * the source access in the list that last accessed the
1330 * element accessed by the sink access before this sink access.
1331 * Each access is given as a map from the loop iterators
1332 * to the array indices.
1333 * The result is a list of n relations between source and sink
1334 * iterations and a subset of the domain of the sink access,
1335 * corresponding to those iterations that access an element
1336 * not previously accessed.
1338 * To deal with multi-valued sink access relations,
1339 * access_info_compute_flow_core extends the sink iteration domain
1340 * with dimensions that correspond to the data space. These extra dimensions
1341 * are projected out from the result of access_info_compute_flow_core.
1343 __isl_give isl_flow *isl_access_info_compute_flow(__isl_take isl_access_info *acc)
1345 int j;
1346 struct isl_flow *res;
1348 if (!acc)
1349 return NULL;
1351 acc->domain_map = isl_map_domain_map(isl_map_copy(acc->sink.map));
1352 res = access_info_compute_flow_core(acc);
1353 if (!res)
1354 return NULL;
1356 for (j = 0; j < res->n_source; ++j) {
1357 res->dep[j].map = isl_map_range_factor_domain(res->dep[j].map);
1358 if (!res->dep[j].map)
1359 goto error;
1362 return res;
1363 error:
1364 isl_flow_free(res);
1365 return NULL;
1369 /* Keep track of some information about a schedule for a given
1370 * access. In particular, keep track of which dimensions
1371 * have a constant value and of the actual constant values.
1373 struct isl_sched_info {
1374 int *is_cst;
1375 isl_vec *cst;
1378 static void sched_info_free(__isl_take struct isl_sched_info *info)
1380 if (!info)
1381 return;
1382 isl_vec_free(info->cst);
1383 free(info->is_cst);
1384 free(info);
1387 /* Extract information on the constant dimensions of the schedule
1388 * for a given access. The "map" is of the form
1390 * [S -> D] -> A
1392 * with S the schedule domain, D the iteration domain and A the data domain.
1394 static __isl_give struct isl_sched_info *sched_info_alloc(
1395 __isl_keep isl_map *map)
1397 isl_ctx *ctx;
1398 isl_space *dim;
1399 struct isl_sched_info *info;
1400 int i, n;
1402 if (!map)
1403 return NULL;
1405 dim = isl_space_unwrap(isl_space_domain(isl_map_get_space(map)));
1406 if (!dim)
1407 return NULL;
1408 n = isl_space_dim(dim, isl_dim_in);
1409 isl_space_free(dim);
1411 ctx = isl_map_get_ctx(map);
1412 info = isl_alloc_type(ctx, struct isl_sched_info);
1413 if (!info)
1414 return NULL;
1415 info->is_cst = isl_alloc_array(ctx, int, n);
1416 info->cst = isl_vec_alloc(ctx, n);
1417 if (n && (!info->is_cst || !info->cst))
1418 goto error;
1420 for (i = 0; i < n; ++i) {
1421 isl_val *v;
1423 v = isl_map_plain_get_val_if_fixed(map, isl_dim_in, i);
1424 if (!v)
1425 goto error;
1426 info->is_cst[i] = !isl_val_is_nan(v);
1427 if (info->is_cst[i])
1428 info->cst = isl_vec_set_element_val(info->cst, i, v);
1429 else
1430 isl_val_free(v);
1433 return info;
1434 error:
1435 sched_info_free(info);
1436 return NULL;
1439 /* The different types of access relations that isl_union_access_info
1440 * keeps track of.
1442 * "isl_access_sink" represents the sink accesses.
1443 * "isl_access_must_source" represents the definite source accesses.
1444 * "isl_access_may_source" represents the possible source accesses.
1445 * "isl_access_kill" represents the kills.
1447 * isl_access_sink is sometimes treated differently and
1448 * should therefore appear first.
1450 enum isl_access_type {
1451 isl_access_sink,
1452 isl_access_must_source,
1453 isl_access_may_source,
1454 isl_access_kill,
1455 isl_access_end
1458 /* This structure represents the input for a dependence analysis computation.
1460 * "access" contains the access relations.
1462 * "schedule" or "schedule_map" represents the execution order.
1463 * Exactly one of these fields should be NULL. The other field
1464 * determines the execution order.
1466 * The domains of these four maps refer to the same iteration spaces(s).
1467 * The ranges of the first three maps also refer to the same data space(s).
1469 * After a call to isl_union_access_info_introduce_schedule,
1470 * the "schedule_map" field no longer contains useful information.
1472 struct isl_union_access_info {
1473 isl_union_map *access[isl_access_end];
1475 isl_schedule *schedule;
1476 isl_union_map *schedule_map;
1479 /* Free "access" and return NULL.
1481 __isl_null isl_union_access_info *isl_union_access_info_free(
1482 __isl_take isl_union_access_info *access)
1484 enum isl_access_type i;
1486 if (!access)
1487 return NULL;
1489 for (i = isl_access_sink; i < isl_access_end; ++i)
1490 isl_union_map_free(access->access[i]);
1491 isl_schedule_free(access->schedule);
1492 isl_union_map_free(access->schedule_map);
1493 free(access);
1495 return NULL;
1498 /* Return the isl_ctx to which "access" belongs.
1500 isl_ctx *isl_union_access_info_get_ctx(__isl_keep isl_union_access_info *access)
1502 if (!access)
1503 return NULL;
1504 return isl_union_map_get_ctx(access->access[isl_access_sink]);
1507 /* Construct an empty (invalid) isl_union_access_info object.
1508 * The caller is responsible for setting the sink access relation and
1509 * initializing all the other fields, e.g., by calling
1510 * isl_union_access_info_init.
1512 static __isl_give isl_union_access_info *isl_union_access_info_alloc(
1513 isl_ctx *ctx)
1515 return isl_calloc_type(ctx, isl_union_access_info);
1518 /* Initialize all the fields of "info", except the sink access relation,
1519 * which is assumed to have been set by the caller.
1521 * By default, we use the schedule field of the isl_union_access_info,
1522 * but this may be overridden by a call
1523 * to isl_union_access_info_set_schedule_map.
1525 static __isl_give isl_union_access_info *isl_union_access_info_init(
1526 __isl_take isl_union_access_info *info)
1528 isl_space *space;
1529 isl_union_map *empty;
1530 enum isl_access_type i;
1532 if (!info)
1533 return NULL;
1534 if (!info->access[isl_access_sink])
1535 return isl_union_access_info_free(info);
1537 space = isl_union_map_get_space(info->access[isl_access_sink]);
1538 empty = isl_union_map_empty(isl_space_copy(space));
1539 for (i = isl_access_sink + 1; i < isl_access_end; ++i)
1540 if (!info->access[i])
1541 info->access[i] = isl_union_map_copy(empty);
1542 isl_union_map_free(empty);
1543 if (!info->schedule && !info->schedule_map)
1544 info->schedule = isl_schedule_empty(isl_space_copy(space));
1545 isl_space_free(space);
1547 for (i = isl_access_sink + 1; i < isl_access_end; ++i)
1548 if (!info->access[i])
1549 return isl_union_access_info_free(info);
1550 if (!info->schedule && !info->schedule_map)
1551 return isl_union_access_info_free(info);
1553 return info;
1556 /* Create a new isl_union_access_info with the given sink accesses and
1557 * and no other accesses or schedule information.
1559 __isl_give isl_union_access_info *isl_union_access_info_from_sink(
1560 __isl_take isl_union_map *sink)
1562 isl_ctx *ctx;
1563 isl_union_access_info *access;
1565 if (!sink)
1566 return NULL;
1567 ctx = isl_union_map_get_ctx(sink);
1568 access = isl_union_access_info_alloc(ctx);
1569 if (!access)
1570 goto error;
1571 access->access[isl_access_sink] = sink;
1572 return isl_union_access_info_init(access);
1573 error:
1574 isl_union_map_free(sink);
1575 return NULL;
1578 /* Replace the access relation of type "type" of "info" by "access".
1580 static __isl_give isl_union_access_info *isl_union_access_info_set(
1581 __isl_take isl_union_access_info *info,
1582 enum isl_access_type type, __isl_take isl_union_map *access)
1584 if (!info || !access)
1585 goto error;
1587 isl_union_map_free(info->access[type]);
1588 info->access[type] = access;
1590 return info;
1591 error:
1592 isl_union_access_info_free(info);
1593 isl_union_map_free(access);
1594 return NULL;
1597 /* Replace the definite source accesses of "access" by "must_source".
1599 __isl_give isl_union_access_info *isl_union_access_info_set_must_source(
1600 __isl_take isl_union_access_info *access,
1601 __isl_take isl_union_map *must_source)
1603 return isl_union_access_info_set(access, isl_access_must_source,
1604 must_source);
1607 /* Replace the possible source accesses of "access" by "may_source".
1609 __isl_give isl_union_access_info *isl_union_access_info_set_may_source(
1610 __isl_take isl_union_access_info *access,
1611 __isl_take isl_union_map *may_source)
1613 return isl_union_access_info_set(access, isl_access_may_source,
1614 may_source);
1617 /* Replace the kills of "info" by "kill".
1619 __isl_give isl_union_access_info *isl_union_access_info_set_kill(
1620 __isl_take isl_union_access_info *info, __isl_take isl_union_map *kill)
1622 return isl_union_access_info_set(info, isl_access_kill, kill);
1625 /* Return the access relation of type "type" of "info".
1627 static __isl_give isl_union_map *isl_union_access_info_get(
1628 __isl_keep isl_union_access_info *info, enum isl_access_type type)
1630 if (!info)
1631 return NULL;
1632 return isl_union_map_copy(info->access[type]);
1635 /* Return the definite source accesses of "info".
1637 __isl_give isl_union_map *isl_union_access_info_get_must_source(
1638 __isl_keep isl_union_access_info *info)
1640 return isl_union_access_info_get(info, isl_access_must_source);
1643 /* Return the possible source accesses of "info".
1645 __isl_give isl_union_map *isl_union_access_info_get_may_source(
1646 __isl_keep isl_union_access_info *info)
1648 return isl_union_access_info_get(info, isl_access_may_source);
1651 /* Return the kills of "info".
1653 __isl_give isl_union_map *isl_union_access_info_get_kill(
1654 __isl_keep isl_union_access_info *info)
1656 return isl_union_access_info_get(info, isl_access_kill);
1659 /* Does "info" specify any kills?
1661 static isl_bool isl_union_access_has_kill(
1662 __isl_keep isl_union_access_info *info)
1664 isl_bool empty;
1666 if (!info)
1667 return isl_bool_error;
1668 empty = isl_union_map_is_empty(info->access[isl_access_kill]);
1669 return isl_bool_not(empty);
1672 /* Replace the schedule of "access" by "schedule".
1673 * Also free the schedule_map in case it was set last.
1675 __isl_give isl_union_access_info *isl_union_access_info_set_schedule(
1676 __isl_take isl_union_access_info *access,
1677 __isl_take isl_schedule *schedule)
1679 if (!access || !schedule)
1680 goto error;
1682 access->schedule_map = isl_union_map_free(access->schedule_map);
1683 isl_schedule_free(access->schedule);
1684 access->schedule = schedule;
1686 return access;
1687 error:
1688 isl_union_access_info_free(access);
1689 isl_schedule_free(schedule);
1690 return NULL;
1693 /* Replace the schedule map of "access" by "schedule_map".
1694 * Also free the schedule in case it was set last.
1696 __isl_give isl_union_access_info *isl_union_access_info_set_schedule_map(
1697 __isl_take isl_union_access_info *access,
1698 __isl_take isl_union_map *schedule_map)
1700 if (!access || !schedule_map)
1701 goto error;
1703 isl_union_map_free(access->schedule_map);
1704 access->schedule = isl_schedule_free(access->schedule);
1705 access->schedule_map = schedule_map;
1707 return access;
1708 error:
1709 isl_union_access_info_free(access);
1710 isl_union_map_free(schedule_map);
1711 return NULL;
1714 __isl_give isl_union_access_info *isl_union_access_info_copy(
1715 __isl_keep isl_union_access_info *access)
1717 isl_union_access_info *copy;
1718 enum isl_access_type i;
1720 if (!access)
1721 return NULL;
1722 copy = isl_union_access_info_from_sink(
1723 isl_union_map_copy(access->access[isl_access_sink]));
1724 for (i = isl_access_sink + 1; i < isl_access_end; ++i)
1725 copy = isl_union_access_info_set(copy, i,
1726 isl_union_map_copy(access->access[i]));
1727 if (access->schedule)
1728 copy = isl_union_access_info_set_schedule(copy,
1729 isl_schedule_copy(access->schedule));
1730 else
1731 copy = isl_union_access_info_set_schedule_map(copy,
1732 isl_union_map_copy(access->schedule_map));
1734 return copy;
1737 /* Print a key-value pair of a YAML mapping to "p",
1738 * with key "name" and value "umap".
1740 static __isl_give isl_printer *print_union_map_field(__isl_take isl_printer *p,
1741 const char *name, __isl_keep isl_union_map *umap)
1743 p = isl_printer_print_str(p, name);
1744 p = isl_printer_yaml_next(p);
1745 p = isl_printer_print_str(p, "\"");
1746 p = isl_printer_print_union_map(p, umap);
1747 p = isl_printer_print_str(p, "\"");
1748 p = isl_printer_yaml_next(p);
1750 return p;
1753 /* An enumeration of the various keys that may appear in a YAML mapping
1754 * of an isl_union_access_info object.
1755 * The keys for the access relation types are assumed to have the same values
1756 * as the access relation types in isl_access_type.
1758 enum isl_ai_key {
1759 isl_ai_key_error = -1,
1760 isl_ai_key_sink = isl_access_sink,
1761 isl_ai_key_must_source = isl_access_must_source,
1762 isl_ai_key_may_source = isl_access_may_source,
1763 isl_ai_key_kill = isl_access_kill,
1764 isl_ai_key_schedule_map,
1765 isl_ai_key_schedule,
1766 isl_ai_key_end
1769 /* Textual representations of the YAML keys for an isl_union_access_info
1770 * object.
1772 static char *key_str[] = {
1773 [isl_ai_key_sink] = "sink",
1774 [isl_ai_key_must_source] = "must_source",
1775 [isl_ai_key_may_source] = "may_source",
1776 [isl_ai_key_kill] = "kill",
1777 [isl_ai_key_schedule_map] = "schedule_map",
1778 [isl_ai_key_schedule] = "schedule",
1781 /* Print a key-value pair corresponding to the access relation of type "type"
1782 * of a YAML mapping of "info" to "p".
1784 * The sink access relation is always printed, but any other access relation
1785 * is only printed if it is non-empty.
1787 static __isl_give isl_printer *print_access_field(__isl_take isl_printer *p,
1788 __isl_keep isl_union_access_info *info, enum isl_access_type type)
1790 if (type != isl_access_sink) {
1791 isl_bool empty;
1793 empty = isl_union_map_is_empty(info->access[type]);
1794 if (empty < 0)
1795 return isl_printer_free(p);
1796 if (empty)
1797 return p;
1799 return print_union_map_field(p, key_str[type], info->access[type]);
1802 /* Print the information contained in "access" to "p".
1803 * The information is printed as a YAML document.
1805 __isl_give isl_printer *isl_printer_print_union_access_info(
1806 __isl_take isl_printer *p, __isl_keep isl_union_access_info *access)
1808 enum isl_access_type i;
1810 if (!access)
1811 return isl_printer_free(p);
1813 p = isl_printer_yaml_start_mapping(p);
1814 for (i = isl_access_sink; i < isl_access_end; ++i)
1815 p = print_access_field(p, access, i);
1816 if (access->schedule) {
1817 p = isl_printer_print_str(p, key_str[isl_ai_key_schedule]);
1818 p = isl_printer_yaml_next(p);
1819 p = isl_printer_print_schedule(p, access->schedule);
1820 p = isl_printer_yaml_next(p);
1821 } else {
1822 p = print_union_map_field(p, key_str[isl_ai_key_schedule_map],
1823 access->schedule_map);
1825 p = isl_printer_yaml_end_mapping(p);
1827 return p;
1830 /* Return a string representation of the information in "access".
1831 * The information is printed in flow format.
1833 __isl_give char *isl_union_access_info_to_str(
1834 __isl_keep isl_union_access_info *access)
1836 isl_printer *p;
1837 char *s;
1839 if (!access)
1840 return NULL;
1842 p = isl_printer_to_str(isl_union_access_info_get_ctx(access));
1843 p = isl_printer_set_yaml_style(p, ISL_YAML_STYLE_FLOW);
1844 p = isl_printer_print_union_access_info(p, access);
1845 s = isl_printer_get_str(p);
1846 isl_printer_free(p);
1848 return s;
1851 #undef KEY
1852 #define KEY enum isl_ai_key
1853 #undef KEY_ERROR
1854 #define KEY_ERROR isl_ai_key_error
1855 #undef KEY_END
1856 #define KEY_END isl_ai_key_end
1857 #include "extract_key.c"
1859 #undef BASE
1860 #define BASE union_map
1861 #include "read_in_string_templ.c"
1863 /* Read an isl_union_access_info object from "s".
1865 * Start off with an empty (invalid) isl_union_access_info object and
1866 * then fill up the fields based on the input.
1867 * The input needs to contain at least a description of the sink
1868 * access relation as well as some form of schedule.
1869 * The other access relations are set to empty relations
1870 * by isl_union_access_info_init if they are not specified in the input.
1872 __isl_give isl_union_access_info *isl_stream_read_union_access_info(
1873 isl_stream *s)
1875 isl_ctx *ctx;
1876 isl_union_access_info *info;
1877 int more;
1878 int sink_set = 0;
1879 int schedule_set = 0;
1881 if (isl_stream_yaml_read_start_mapping(s))
1882 return NULL;
1884 ctx = isl_stream_get_ctx(s);
1885 info = isl_union_access_info_alloc(ctx);
1886 while ((more = isl_stream_yaml_next(s)) > 0) {
1887 enum isl_ai_key key;
1888 isl_union_map *access, *schedule_map;
1889 isl_schedule *schedule;
1891 key = get_key(s);
1892 if (isl_stream_yaml_next(s) < 0)
1893 return isl_union_access_info_free(info);
1894 switch (key) {
1895 case isl_ai_key_end:
1896 case isl_ai_key_error:
1897 return isl_union_access_info_free(info);
1898 case isl_ai_key_sink:
1899 sink_set = 1;
1900 case isl_ai_key_must_source:
1901 case isl_ai_key_may_source:
1902 case isl_ai_key_kill:
1903 access = read_union_map(s);
1904 info = isl_union_access_info_set(info, key, access);
1905 if (!info)
1906 return NULL;
1907 break;
1908 case isl_ai_key_schedule_map:
1909 schedule_set = 1;
1910 schedule_map = read_union_map(s);
1911 info = isl_union_access_info_set_schedule_map(info,
1912 schedule_map);
1913 if (!info)
1914 return NULL;
1915 break;
1916 case isl_ai_key_schedule:
1917 schedule_set = 1;
1918 schedule = isl_stream_read_schedule(s);
1919 info = isl_union_access_info_set_schedule(info,
1920 schedule);
1921 if (!info)
1922 return NULL;
1923 break;
1926 if (more < 0)
1927 return isl_union_access_info_free(info);
1929 if (isl_stream_yaml_read_end_mapping(s) < 0) {
1930 isl_stream_error(s, NULL, "unexpected extra elements");
1931 return isl_union_access_info_free(info);
1934 if (!sink_set) {
1935 isl_stream_error(s, NULL, "no sink specified");
1936 return isl_union_access_info_free(info);
1939 if (!schedule_set) {
1940 isl_stream_error(s, NULL, "no schedule specified");
1941 return isl_union_access_info_free(info);
1944 return isl_union_access_info_init(info);
1947 /* Read an isl_union_access_info object from the file "input".
1949 __isl_give isl_union_access_info *isl_union_access_info_read_from_file(
1950 isl_ctx *ctx, FILE *input)
1952 isl_stream *s;
1953 isl_union_access_info *access;
1955 s = isl_stream_new_file(ctx, input);
1956 if (!s)
1957 return NULL;
1958 access = isl_stream_read_union_access_info(s);
1959 isl_stream_free(s);
1961 return access;
1964 /* Update the fields of "access" such that they all have the same parameters,
1965 * keeping in mind that the schedule_map field may be NULL and ignoring
1966 * the schedule field.
1968 static __isl_give isl_union_access_info *isl_union_access_info_align_params(
1969 __isl_take isl_union_access_info *access)
1971 isl_space *space;
1972 enum isl_access_type i;
1974 if (!access)
1975 return NULL;
1977 space = isl_union_map_get_space(access->access[isl_access_sink]);
1978 for (i = isl_access_sink + 1; i < isl_access_end; ++i)
1979 space = isl_space_align_params(space,
1980 isl_union_map_get_space(access->access[i]));
1981 if (access->schedule_map)
1982 space = isl_space_align_params(space,
1983 isl_union_map_get_space(access->schedule_map));
1984 for (i = isl_access_sink; i < isl_access_end; ++i)
1985 access->access[i] =
1986 isl_union_map_align_params(access->access[i],
1987 isl_space_copy(space));
1988 if (!access->schedule_map) {
1989 isl_space_free(space);
1990 } else {
1991 access->schedule_map =
1992 isl_union_map_align_params(access->schedule_map, space);
1993 if (!access->schedule_map)
1994 return isl_union_access_info_free(access);
1997 for (i = isl_access_sink; i < isl_access_end; ++i)
1998 if (!access->access[i])
1999 return isl_union_access_info_free(access);
2001 return access;
2004 /* Prepend the schedule dimensions to the iteration domains.
2006 * That is, if the schedule is of the form
2008 * D -> S
2010 * while the access relations are of the form
2012 * D -> A
2014 * then the updated access relations are of the form
2016 * [S -> D] -> A
2018 * The schedule map is also replaced by the map
2020 * [S -> D] -> D
2022 * that is used during the internal computation.
2023 * Neither the original schedule map nor this updated schedule map
2024 * are used after the call to this function.
2026 static __isl_give isl_union_access_info *
2027 isl_union_access_info_introduce_schedule(
2028 __isl_take isl_union_access_info *access)
2030 isl_union_map *sm;
2031 enum isl_access_type i;
2033 if (!access)
2034 return NULL;
2036 sm = isl_union_map_reverse(access->schedule_map);
2037 sm = isl_union_map_range_map(sm);
2038 for (i = isl_access_sink; i < isl_access_end; ++i)
2039 access->access[i] =
2040 isl_union_map_apply_range(isl_union_map_copy(sm),
2041 access->access[i]);
2042 access->schedule_map = sm;
2044 for (i = isl_access_sink; i < isl_access_end; ++i)
2045 if (!access->access[i])
2046 return isl_union_access_info_free(access);
2047 if (!access->schedule_map)
2048 return isl_union_access_info_free(access);
2050 return access;
2053 /* This structure represents the result of a dependence analysis computation.
2055 * "must_dep" represents the full definite dependences
2056 * "may_dep" represents the full non-definite dependences.
2057 * Both are of the form
2059 * [Source] -> [[Sink -> Data]]
2061 * (after the schedule dimensions have been projected out).
2062 * "must_no_source" represents the subset of the sink accesses for which
2063 * definitely no source was found.
2064 * "may_no_source" represents the subset of the sink accesses for which
2065 * possibly, but not definitely, no source was found.
2067 struct isl_union_flow {
2068 isl_union_map *must_dep;
2069 isl_union_map *may_dep;
2070 isl_union_map *must_no_source;
2071 isl_union_map *may_no_source;
2074 /* Return the isl_ctx to which "flow" belongs.
2076 isl_ctx *isl_union_flow_get_ctx(__isl_keep isl_union_flow *flow)
2078 return flow ? isl_union_map_get_ctx(flow->must_dep) : NULL;
2081 /* Free "flow" and return NULL.
2083 __isl_null isl_union_flow *isl_union_flow_free(__isl_take isl_union_flow *flow)
2085 if (!flow)
2086 return NULL;
2087 isl_union_map_free(flow->must_dep);
2088 isl_union_map_free(flow->may_dep);
2089 isl_union_map_free(flow->must_no_source);
2090 isl_union_map_free(flow->may_no_source);
2091 free(flow);
2092 return NULL;
2095 void isl_union_flow_dump(__isl_keep isl_union_flow *flow)
2097 if (!flow)
2098 return;
2100 fprintf(stderr, "must dependences: ");
2101 isl_union_map_dump(flow->must_dep);
2102 fprintf(stderr, "may dependences: ");
2103 isl_union_map_dump(flow->may_dep);
2104 fprintf(stderr, "must no source: ");
2105 isl_union_map_dump(flow->must_no_source);
2106 fprintf(stderr, "may no source: ");
2107 isl_union_map_dump(flow->may_no_source);
2110 /* Return the full definite dependences in "flow", with accessed elements.
2112 __isl_give isl_union_map *isl_union_flow_get_full_must_dependence(
2113 __isl_keep isl_union_flow *flow)
2115 if (!flow)
2116 return NULL;
2117 return isl_union_map_copy(flow->must_dep);
2120 /* Return the full possible dependences in "flow", including the definite
2121 * dependences, with accessed elements.
2123 __isl_give isl_union_map *isl_union_flow_get_full_may_dependence(
2124 __isl_keep isl_union_flow *flow)
2126 if (!flow)
2127 return NULL;
2128 return isl_union_map_union(isl_union_map_copy(flow->must_dep),
2129 isl_union_map_copy(flow->may_dep));
2132 /* Return the definite dependences in "flow", without the accessed elements.
2134 __isl_give isl_union_map *isl_union_flow_get_must_dependence(
2135 __isl_keep isl_union_flow *flow)
2137 isl_union_map *dep;
2139 if (!flow)
2140 return NULL;
2141 dep = isl_union_map_copy(flow->must_dep);
2142 return isl_union_map_range_factor_domain(dep);
2145 /* Return the possible dependences in "flow", including the definite
2146 * dependences, without the accessed elements.
2148 __isl_give isl_union_map *isl_union_flow_get_may_dependence(
2149 __isl_keep isl_union_flow *flow)
2151 isl_union_map *dep;
2153 if (!flow)
2154 return NULL;
2155 dep = isl_union_map_union(isl_union_map_copy(flow->must_dep),
2156 isl_union_map_copy(flow->may_dep));
2157 return isl_union_map_range_factor_domain(dep);
2160 /* Return the non-definite dependences in "flow".
2162 static __isl_give isl_union_map *isl_union_flow_get_non_must_dependence(
2163 __isl_keep isl_union_flow *flow)
2165 if (!flow)
2166 return NULL;
2167 return isl_union_map_copy(flow->may_dep);
2170 /* Return the subset of the sink accesses for which definitely
2171 * no source was found.
2173 __isl_give isl_union_map *isl_union_flow_get_must_no_source(
2174 __isl_keep isl_union_flow *flow)
2176 if (!flow)
2177 return NULL;
2178 return isl_union_map_copy(flow->must_no_source);
2181 /* Return the subset of the sink accesses for which possibly
2182 * no source was found, including those for which definitely
2183 * no source was found.
2185 __isl_give isl_union_map *isl_union_flow_get_may_no_source(
2186 __isl_keep isl_union_flow *flow)
2188 if (!flow)
2189 return NULL;
2190 return isl_union_map_union(isl_union_map_copy(flow->must_no_source),
2191 isl_union_map_copy(flow->may_no_source));
2194 /* Return the subset of the sink accesses for which possibly, but not
2195 * definitely, no source was found.
2197 static __isl_give isl_union_map *isl_union_flow_get_non_must_no_source(
2198 __isl_keep isl_union_flow *flow)
2200 if (!flow)
2201 return NULL;
2202 return isl_union_map_copy(flow->may_no_source);
2205 /* Create a new isl_union_flow object, initialized with empty
2206 * dependence relations and sink subsets.
2208 static __isl_give isl_union_flow *isl_union_flow_alloc(
2209 __isl_take isl_space *space)
2211 isl_ctx *ctx;
2212 isl_union_map *empty;
2213 isl_union_flow *flow;
2215 if (!space)
2216 return NULL;
2217 ctx = isl_space_get_ctx(space);
2218 flow = isl_alloc_type(ctx, isl_union_flow);
2219 if (!flow)
2220 goto error;
2222 empty = isl_union_map_empty(space);
2223 flow->must_dep = isl_union_map_copy(empty);
2224 flow->may_dep = isl_union_map_copy(empty);
2225 flow->must_no_source = isl_union_map_copy(empty);
2226 flow->may_no_source = empty;
2228 if (!flow->must_dep || !flow->may_dep ||
2229 !flow->must_no_source || !flow->may_no_source)
2230 return isl_union_flow_free(flow);
2232 return flow;
2233 error:
2234 isl_space_free(space);
2235 return NULL;
2238 /* Copy this isl_union_flow object.
2240 __isl_give isl_union_flow *isl_union_flow_copy(__isl_keep isl_union_flow *flow)
2242 isl_union_flow *copy;
2244 if (!flow)
2245 return NULL;
2247 copy = isl_union_flow_alloc(isl_union_map_get_space(flow->must_dep));
2249 if (!copy)
2250 return NULL;
2252 copy->must_dep = isl_union_map_union(copy->must_dep,
2253 isl_union_map_copy(flow->must_dep));
2254 copy->may_dep = isl_union_map_union(copy->may_dep,
2255 isl_union_map_copy(flow->may_dep));
2256 copy->must_no_source = isl_union_map_union(copy->must_no_source,
2257 isl_union_map_copy(flow->must_no_source));
2258 copy->may_no_source = isl_union_map_union(copy->may_no_source,
2259 isl_union_map_copy(flow->may_no_source));
2261 if (!copy->must_dep || !copy->may_dep ||
2262 !copy->must_no_source || !copy->may_no_source)
2263 return isl_union_flow_free(copy);
2265 return copy;
2268 /* Drop the schedule dimensions from the iteration domains in "flow".
2269 * In particular, the schedule dimensions have been prepended
2270 * to the iteration domains prior to the dependence analysis by
2271 * replacing the iteration domain D, by the wrapped map [S -> D].
2272 * Replace these wrapped maps by the original D.
2274 * In particular, the dependences computed by access_info_compute_flow_core
2275 * are of the form
2277 * [S -> D] -> [[S' -> D'] -> A]
2279 * The schedule dimensions are projected out by first currying the range,
2280 * resulting in
2282 * [S -> D] -> [S' -> [D' -> A]]
2284 * and then computing the factor range
2286 * D -> [D' -> A]
2288 static __isl_give isl_union_flow *isl_union_flow_drop_schedule(
2289 __isl_take isl_union_flow *flow)
2291 if (!flow)
2292 return NULL;
2294 flow->must_dep = isl_union_map_range_curry(flow->must_dep);
2295 flow->must_dep = isl_union_map_factor_range(flow->must_dep);
2296 flow->may_dep = isl_union_map_range_curry(flow->may_dep);
2297 flow->may_dep = isl_union_map_factor_range(flow->may_dep);
2298 flow->must_no_source =
2299 isl_union_map_domain_factor_range(flow->must_no_source);
2300 flow->may_no_source =
2301 isl_union_map_domain_factor_range(flow->may_no_source);
2303 if (!flow->must_dep || !flow->may_dep ||
2304 !flow->must_no_source || !flow->may_no_source)
2305 return isl_union_flow_free(flow);
2307 return flow;
2310 struct isl_compute_flow_data {
2311 isl_union_map *must_source;
2312 isl_union_map *may_source;
2313 isl_union_flow *flow;
2315 int count;
2316 int must;
2317 isl_space *dim;
2318 struct isl_sched_info *sink_info;
2319 struct isl_sched_info **source_info;
2320 isl_access_info *accesses;
2323 static isl_stat count_matching_array(__isl_take isl_map *map, void *user)
2325 int eq;
2326 isl_space *dim;
2327 struct isl_compute_flow_data *data;
2329 data = (struct isl_compute_flow_data *)user;
2331 dim = isl_space_range(isl_map_get_space(map));
2333 eq = isl_space_is_equal(dim, data->dim);
2335 isl_space_free(dim);
2336 isl_map_free(map);
2338 if (eq < 0)
2339 return isl_stat_error;
2340 if (eq)
2341 data->count++;
2343 return isl_stat_ok;
2346 static isl_stat collect_matching_array(__isl_take isl_map *map, void *user)
2348 int eq;
2349 isl_space *dim;
2350 struct isl_sched_info *info;
2351 struct isl_compute_flow_data *data;
2353 data = (struct isl_compute_flow_data *)user;
2355 dim = isl_space_range(isl_map_get_space(map));
2357 eq = isl_space_is_equal(dim, data->dim);
2359 isl_space_free(dim);
2361 if (eq < 0)
2362 goto error;
2363 if (!eq) {
2364 isl_map_free(map);
2365 return isl_stat_ok;
2368 info = sched_info_alloc(map);
2369 data->source_info[data->count] = info;
2371 data->accesses = isl_access_info_add_source(data->accesses,
2372 map, data->must, info);
2374 data->count++;
2376 return isl_stat_ok;
2377 error:
2378 isl_map_free(map);
2379 return isl_stat_error;
2382 /* Determine the shared nesting level and the "textual order" of
2383 * the given accesses.
2385 * We first determine the minimal schedule dimension for both accesses.
2387 * If among those dimensions, we can find one where both have a fixed
2388 * value and if moreover those values are different, then the previous
2389 * dimension is the last shared nesting level and the textual order
2390 * is determined based on the order of the fixed values.
2391 * If no such fixed values can be found, then we set the shared
2392 * nesting level to the minimal schedule dimension, with no textual ordering.
2394 static int before(void *first, void *second)
2396 struct isl_sched_info *info1 = first;
2397 struct isl_sched_info *info2 = second;
2398 int n1, n2;
2399 int i;
2401 n1 = isl_vec_size(info1->cst);
2402 n2 = isl_vec_size(info2->cst);
2404 if (n2 < n1)
2405 n1 = n2;
2407 for (i = 0; i < n1; ++i) {
2408 int r;
2409 int cmp;
2411 if (!info1->is_cst[i])
2412 continue;
2413 if (!info2->is_cst[i])
2414 continue;
2415 cmp = isl_vec_cmp_element(info1->cst, info2->cst, i);
2416 if (cmp == 0)
2417 continue;
2419 r = 2 * i + (cmp < 0);
2421 return r;
2424 return 2 * n1;
2427 /* Check if the given two accesses may be coscheduled.
2428 * If so, return 1. Otherwise return 0.
2430 * Two accesses may only be coscheduled if the fixed schedule
2431 * coordinates have the same values.
2433 static int coscheduled(void *first, void *second)
2435 struct isl_sched_info *info1 = first;
2436 struct isl_sched_info *info2 = second;
2437 int n1, n2;
2438 int i;
2440 n1 = isl_vec_size(info1->cst);
2441 n2 = isl_vec_size(info2->cst);
2443 if (n2 < n1)
2444 n1 = n2;
2446 for (i = 0; i < n1; ++i) {
2447 int cmp;
2449 if (!info1->is_cst[i])
2450 continue;
2451 if (!info2->is_cst[i])
2452 continue;
2453 cmp = isl_vec_cmp_element(info1->cst, info2->cst, i);
2454 if (cmp != 0)
2455 return 0;
2458 return 1;
2461 /* Given a sink access, look for all the source accesses that access
2462 * the same array and perform dataflow analysis on them using
2463 * isl_access_info_compute_flow_core.
2465 static isl_stat compute_flow(__isl_take isl_map *map, void *user)
2467 int i;
2468 isl_ctx *ctx;
2469 struct isl_compute_flow_data *data;
2470 isl_flow *flow;
2471 isl_union_flow *df;
2473 data = (struct isl_compute_flow_data *)user;
2474 df = data->flow;
2476 ctx = isl_map_get_ctx(map);
2478 data->accesses = NULL;
2479 data->sink_info = NULL;
2480 data->source_info = NULL;
2481 data->count = 0;
2482 data->dim = isl_space_range(isl_map_get_space(map));
2484 if (isl_union_map_foreach_map(data->must_source,
2485 &count_matching_array, data) < 0)
2486 goto error;
2487 if (isl_union_map_foreach_map(data->may_source,
2488 &count_matching_array, data) < 0)
2489 goto error;
2491 data->sink_info = sched_info_alloc(map);
2492 data->source_info = isl_calloc_array(ctx, struct isl_sched_info *,
2493 data->count);
2495 data->accesses = isl_access_info_alloc(isl_map_copy(map),
2496 data->sink_info, &before, data->count);
2497 if (!data->sink_info || (data->count && !data->source_info) ||
2498 !data->accesses)
2499 goto error;
2500 data->accesses->coscheduled = &coscheduled;
2501 data->count = 0;
2502 data->must = 1;
2503 if (isl_union_map_foreach_map(data->must_source,
2504 &collect_matching_array, data) < 0)
2505 goto error;
2506 data->must = 0;
2507 if (isl_union_map_foreach_map(data->may_source,
2508 &collect_matching_array, data) < 0)
2509 goto error;
2511 flow = access_info_compute_flow_core(data->accesses);
2512 data->accesses = NULL;
2514 if (!flow)
2515 goto error;
2517 df->must_no_source = isl_union_map_union(df->must_no_source,
2518 isl_union_map_from_map(isl_flow_get_no_source(flow, 1)));
2519 df->may_no_source = isl_union_map_union(df->may_no_source,
2520 isl_union_map_from_map(isl_flow_get_no_source(flow, 0)));
2522 for (i = 0; i < flow->n_source; ++i) {
2523 isl_union_map *dep;
2524 dep = isl_union_map_from_map(isl_map_copy(flow->dep[i].map));
2525 if (flow->dep[i].must)
2526 df->must_dep = isl_union_map_union(df->must_dep, dep);
2527 else
2528 df->may_dep = isl_union_map_union(df->may_dep, dep);
2531 isl_flow_free(flow);
2533 sched_info_free(data->sink_info);
2534 if (data->source_info) {
2535 for (i = 0; i < data->count; ++i)
2536 sched_info_free(data->source_info[i]);
2537 free(data->source_info);
2539 isl_space_free(data->dim);
2540 isl_map_free(map);
2542 return isl_stat_ok;
2543 error:
2544 isl_access_info_free(data->accesses);
2545 sched_info_free(data->sink_info);
2546 if (data->source_info) {
2547 for (i = 0; i < data->count; ++i)
2548 sched_info_free(data->source_info[i]);
2549 free(data->source_info);
2551 isl_space_free(data->dim);
2552 isl_map_free(map);
2554 return isl_stat_error;
2557 /* Add the kills of "info" to the must-sources.
2559 static __isl_give isl_union_access_info *
2560 isl_union_access_info_add_kill_to_must_source(
2561 __isl_take isl_union_access_info *info)
2563 isl_union_map *must, *kill;
2565 must = isl_union_access_info_get_must_source(info);
2566 kill = isl_union_access_info_get_kill(info);
2567 must = isl_union_map_union(must, kill);
2568 return isl_union_access_info_set_must_source(info, must);
2571 /* Drop dependences from "flow" that purely originate from kills.
2572 * That is, only keep those dependences that originate from
2573 * the original must-sources "must" and/or the original may-sources "may".
2574 * In particular, "must" contains the must-sources from before
2575 * the kills were added and "may" contains the may-source from before
2576 * the kills were removed.
2578 * The dependences are of the form
2580 * Source -> [Sink -> Data]
2582 * Only those dependences are kept where the Source -> Data part
2583 * is a subset of the original may-sources or must-sources.
2584 * Of those, only the must-dependences that intersect with the must-sources
2585 * remain must-dependences.
2586 * If there is some overlap between the may-sources and the must-sources,
2587 * then the may-dependences and must-dependences may also overlap.
2588 * This should be fine since the may-dependences are only kept
2589 * disjoint from the must-dependences for the isl_union_map_compute_flow
2590 * interface. This interface does not support kills, so it will
2591 * not end up calling this function.
2593 static __isl_give isl_union_flow *isl_union_flow_drop_kill_source(
2594 __isl_take isl_union_flow *flow, __isl_take isl_union_map *must,
2595 __isl_take isl_union_map *may)
2597 isl_union_map *move;
2599 if (!flow)
2600 goto error;
2601 move = isl_union_map_copy(flow->must_dep);
2602 move = isl_union_map_intersect_range_factor_range(move,
2603 isl_union_map_copy(may));
2604 may = isl_union_map_union(may, isl_union_map_copy(must));
2605 flow->may_dep = isl_union_map_intersect_range_factor_range(
2606 flow->may_dep, may);
2607 flow->must_dep = isl_union_map_intersect_range_factor_range(
2608 flow->must_dep, must);
2609 flow->may_dep = isl_union_map_union(flow->may_dep, move);
2610 if (!flow->must_dep || !flow->may_dep)
2611 return isl_union_flow_free(flow);
2613 return flow;
2614 error:
2615 isl_union_map_free(must);
2616 isl_union_map_free(may);
2617 return NULL;
2620 /* Remove the must accesses from the may accesses.
2622 * A must access always trumps a may access, so there is no need
2623 * for a must access to also be considered as a may access. Doing so
2624 * would only cost extra computations only to find out that
2625 * the duplicated may access does not make any difference.
2627 static __isl_give isl_union_access_info *isl_union_access_info_normalize(
2628 __isl_take isl_union_access_info *access)
2630 if (!access)
2631 return NULL;
2632 access->access[isl_access_may_source] =
2633 isl_union_map_subtract(access->access[isl_access_may_source],
2634 isl_union_map_copy(access->access[isl_access_must_source]));
2635 if (!access->access[isl_access_may_source])
2636 return isl_union_access_info_free(access);
2638 return access;
2641 /* Given a description of the "sink" accesses, the "source" accesses and
2642 * a schedule, compute for each instance of a sink access
2643 * and for each element accessed by that instance,
2644 * the possible or definite source accesses that last accessed the
2645 * element accessed by the sink access before this sink access
2646 * in the sense that there is no intermediate definite source access.
2648 * The must_no_source and may_no_source elements of the result
2649 * are subsets of access->sink. The elements must_dep and may_dep
2650 * map domain elements of access->{may,must)_source to
2651 * domain elements of access->sink.
2653 * This function is used when only the schedule map representation
2654 * is available.
2656 * We first prepend the schedule dimensions to the domain
2657 * of the accesses so that we can easily compare their relative order.
2658 * Then we consider each sink access individually in compute_flow.
2660 static __isl_give isl_union_flow *compute_flow_union_map(
2661 __isl_take isl_union_access_info *access)
2663 struct isl_compute_flow_data data;
2664 isl_union_map *sink;
2666 access = isl_union_access_info_align_params(access);
2667 access = isl_union_access_info_introduce_schedule(access);
2668 if (!access)
2669 return NULL;
2671 data.must_source = access->access[isl_access_must_source];
2672 data.may_source = access->access[isl_access_may_source];
2674 sink = access->access[isl_access_sink];
2675 data.flow = isl_union_flow_alloc(isl_union_map_get_space(sink));
2677 if (isl_union_map_foreach_map(sink, &compute_flow, &data) < 0)
2678 goto error;
2680 data.flow = isl_union_flow_drop_schedule(data.flow);
2682 isl_union_access_info_free(access);
2683 return data.flow;
2684 error:
2685 isl_union_access_info_free(access);
2686 isl_union_flow_free(data.flow);
2687 return NULL;
2690 /* A schedule access relation.
2692 * The access relation "access" is of the form [S -> D] -> A,
2693 * where S corresponds to the prefix schedule at "node".
2694 * "must" is only relevant for source accesses and indicates
2695 * whether the access is a must source or a may source.
2697 struct isl_scheduled_access {
2698 isl_map *access;
2699 int must;
2700 isl_schedule_node *node;
2703 /* Data structure for keeping track of individual scheduled sink and source
2704 * accesses when computing dependence analysis based on a schedule tree.
2706 * "n_sink" is the number of used entries in "sink"
2707 * "n_source" is the number of used entries in "source"
2709 * "set_sink", "must" and "node" are only used inside collect_sink_source,
2710 * to keep track of the current node and
2711 * of what extract_sink_source needs to do.
2713 struct isl_compute_flow_schedule_data {
2714 isl_union_access_info *access;
2716 int n_sink;
2717 int n_source;
2719 struct isl_scheduled_access *sink;
2720 struct isl_scheduled_access *source;
2722 int set_sink;
2723 int must;
2724 isl_schedule_node *node;
2727 /* Align the parameters of all sinks with all sources.
2729 * If there are no sinks or no sources, then no alignment is needed.
2731 static void isl_compute_flow_schedule_data_align_params(
2732 struct isl_compute_flow_schedule_data *data)
2734 int i;
2735 isl_space *space;
2737 if (data->n_sink == 0 || data->n_source == 0)
2738 return;
2740 space = isl_map_get_space(data->sink[0].access);
2742 for (i = 1; i < data->n_sink; ++i)
2743 space = isl_space_align_params(space,
2744 isl_map_get_space(data->sink[i].access));
2745 for (i = 0; i < data->n_source; ++i)
2746 space = isl_space_align_params(space,
2747 isl_map_get_space(data->source[i].access));
2749 for (i = 0; i < data->n_sink; ++i)
2750 data->sink[i].access =
2751 isl_map_align_params(data->sink[i].access,
2752 isl_space_copy(space));
2753 for (i = 0; i < data->n_source; ++i)
2754 data->source[i].access =
2755 isl_map_align_params(data->source[i].access,
2756 isl_space_copy(space));
2758 isl_space_free(space);
2761 /* Free all the memory referenced from "data".
2762 * Do not free "data" itself as it may be allocated on the stack.
2764 static void isl_compute_flow_schedule_data_clear(
2765 struct isl_compute_flow_schedule_data *data)
2767 int i;
2769 if (!data->sink)
2770 return;
2772 for (i = 0; i < data->n_sink; ++i) {
2773 isl_map_free(data->sink[i].access);
2774 isl_schedule_node_free(data->sink[i].node);
2777 for (i = 0; i < data->n_source; ++i) {
2778 isl_map_free(data->source[i].access);
2779 isl_schedule_node_free(data->source[i].node);
2782 free(data->sink);
2785 /* isl_schedule_foreach_schedule_node_top_down callback for counting
2786 * (an upper bound on) the number of sinks and sources.
2788 * Sinks and sources are only extracted at leaves of the tree,
2789 * so we skip the node if it is not a leaf.
2790 * Otherwise we increment data->n_sink and data->n_source with
2791 * the number of spaces in the sink and source access domains
2792 * that reach this node.
2794 static isl_bool count_sink_source(__isl_keep isl_schedule_node *node,
2795 void *user)
2797 struct isl_compute_flow_schedule_data *data = user;
2798 isl_union_set *domain;
2799 isl_union_map *umap;
2800 isl_bool r = isl_bool_false;
2802 if (isl_schedule_node_get_type(node) != isl_schedule_node_leaf)
2803 return isl_bool_true;
2805 domain = isl_schedule_node_get_universe_domain(node);
2807 umap = isl_union_map_copy(data->access->access[isl_access_sink]);
2808 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
2809 data->n_sink += isl_union_map_n_map(umap);
2810 isl_union_map_free(umap);
2811 if (!umap)
2812 r = isl_bool_error;
2814 umap = isl_union_map_copy(data->access->access[isl_access_must_source]);
2815 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
2816 data->n_source += isl_union_map_n_map(umap);
2817 isl_union_map_free(umap);
2818 if (!umap)
2819 r = isl_bool_error;
2821 umap = isl_union_map_copy(data->access->access[isl_access_may_source]);
2822 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
2823 data->n_source += isl_union_map_n_map(umap);
2824 isl_union_map_free(umap);
2825 if (!umap)
2826 r = isl_bool_error;
2828 isl_union_set_free(domain);
2830 return r;
2833 /* Add a single scheduled sink or source (depending on data->set_sink)
2834 * with scheduled access relation "map", must property data->must and
2835 * schedule node data->node to the list of sinks or sources.
2837 static isl_stat extract_sink_source(__isl_take isl_map *map, void *user)
2839 struct isl_compute_flow_schedule_data *data = user;
2840 struct isl_scheduled_access *access;
2842 if (data->set_sink)
2843 access = data->sink + data->n_sink++;
2844 else
2845 access = data->source + data->n_source++;
2847 access->access = map;
2848 access->must = data->must;
2849 access->node = isl_schedule_node_copy(data->node);
2851 return isl_stat_ok;
2854 /* isl_schedule_foreach_schedule_node_top_down callback for collecting
2855 * individual scheduled source and sink accesses (taking into account
2856 * the domain of the schedule).
2858 * We only collect accesses at the leaves of the schedule tree.
2859 * We prepend the schedule dimensions at the leaf to the iteration
2860 * domains of the source and sink accesses and then extract
2861 * the individual accesses (per space).
2863 * In particular, if the prefix schedule at the node is of the form
2865 * D -> S
2867 * while the access relations are of the form
2869 * D -> A
2871 * then the updated access relations are of the form
2873 * [S -> D] -> A
2875 * Note that S consists of a single space such that introducing S
2876 * in the access relations does not increase the number of spaces.
2878 static isl_bool collect_sink_source(__isl_keep isl_schedule_node *node,
2879 void *user)
2881 struct isl_compute_flow_schedule_data *data = user;
2882 isl_union_map *prefix;
2883 isl_union_map *umap;
2884 isl_bool r = isl_bool_false;
2886 if (isl_schedule_node_get_type(node) != isl_schedule_node_leaf)
2887 return isl_bool_true;
2889 data->node = node;
2891 prefix = isl_schedule_node_get_prefix_schedule_relation(node);
2892 prefix = isl_union_map_reverse(prefix);
2893 prefix = isl_union_map_range_map(prefix);
2895 data->set_sink = 1;
2896 umap = isl_union_map_copy(data->access->access[isl_access_sink]);
2897 umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
2898 if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
2899 r = isl_bool_error;
2900 isl_union_map_free(umap);
2902 data->set_sink = 0;
2903 data->must = 1;
2904 umap = isl_union_map_copy(data->access->access[isl_access_must_source]);
2905 umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
2906 if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
2907 r = isl_bool_error;
2908 isl_union_map_free(umap);
2910 data->set_sink = 0;
2911 data->must = 0;
2912 umap = isl_union_map_copy(data->access->access[isl_access_may_source]);
2913 umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
2914 if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
2915 r = isl_bool_error;
2916 isl_union_map_free(umap);
2918 isl_union_map_free(prefix);
2920 return r;
2923 /* isl_access_info_compute_flow callback for determining whether
2924 * the shared nesting level and the ordering within that level
2925 * for two scheduled accesses for use in compute_single_flow.
2927 * The tokens passed to this function refer to the leaves
2928 * in the schedule tree where the accesses take place.
2930 * If n is the shared number of loops, then we need to return
2931 * "2 * n + 1" if "first" precedes "second" inside the innermost
2932 * shared loop and "2 * n" otherwise.
2934 * The innermost shared ancestor may be the leaves themselves
2935 * if the accesses take place in the same leaf. Otherwise,
2936 * it is either a set node or a sequence node. Only in the case
2937 * of a sequence node do we consider one access to precede the other.
2939 static int before_node(void *first, void *second)
2941 isl_schedule_node *node1 = first;
2942 isl_schedule_node *node2 = second;
2943 isl_schedule_node *shared;
2944 int depth;
2945 int before = 0;
2947 shared = isl_schedule_node_get_shared_ancestor(node1, node2);
2948 if (!shared)
2949 return -1;
2951 depth = isl_schedule_node_get_schedule_depth(shared);
2952 if (isl_schedule_node_get_type(shared) == isl_schedule_node_sequence) {
2953 int pos1, pos2;
2955 pos1 = isl_schedule_node_get_ancestor_child_position(node1,
2956 shared);
2957 pos2 = isl_schedule_node_get_ancestor_child_position(node2,
2958 shared);
2959 before = pos1 < pos2;
2962 isl_schedule_node_free(shared);
2964 return 2 * depth + before;
2967 /* Check if the given two accesses may be coscheduled.
2968 * If so, return 1. Otherwise return 0.
2970 * Two accesses may only be coscheduled if they appear in the same leaf.
2972 static int coscheduled_node(void *first, void *second)
2974 isl_schedule_node *node1 = first;
2975 isl_schedule_node *node2 = second;
2977 return node1 == node2;
2980 /* Add the scheduled sources from "data" that access
2981 * the same data space as "sink" to "access".
2983 static __isl_give isl_access_info *add_matching_sources(
2984 __isl_take isl_access_info *access, struct isl_scheduled_access *sink,
2985 struct isl_compute_flow_schedule_data *data)
2987 int i;
2988 isl_space *space;
2990 space = isl_space_range(isl_map_get_space(sink->access));
2991 for (i = 0; i < data->n_source; ++i) {
2992 struct isl_scheduled_access *source;
2993 isl_space *source_space;
2994 int eq;
2996 source = &data->source[i];
2997 source_space = isl_map_get_space(source->access);
2998 source_space = isl_space_range(source_space);
2999 eq = isl_space_is_equal(space, source_space);
3000 isl_space_free(source_space);
3002 if (!eq)
3003 continue;
3004 if (eq < 0)
3005 goto error;
3007 access = isl_access_info_add_source(access,
3008 isl_map_copy(source->access), source->must, source->node);
3011 isl_space_free(space);
3012 return access;
3013 error:
3014 isl_space_free(space);
3015 isl_access_info_free(access);
3016 return NULL;
3019 /* Given a scheduled sink access relation "sink", compute the corresponding
3020 * dependences on the sources in "data" and add the computed dependences
3021 * to "uf".
3023 * The dependences computed by access_info_compute_flow_core are of the form
3025 * [S -> I] -> [[S' -> I'] -> A]
3027 * The schedule dimensions are projected out by first currying the range,
3028 * resulting in
3030 * [S -> I] -> [S' -> [I' -> A]]
3032 * and then computing the factor range
3034 * I -> [I' -> A]
3036 static __isl_give isl_union_flow *compute_single_flow(
3037 __isl_take isl_union_flow *uf, struct isl_scheduled_access *sink,
3038 struct isl_compute_flow_schedule_data *data)
3040 int i;
3041 isl_access_info *access;
3042 isl_flow *flow;
3043 isl_map *map;
3045 if (!uf)
3046 return NULL;
3048 access = isl_access_info_alloc(isl_map_copy(sink->access), sink->node,
3049 &before_node, data->n_source);
3050 if (access)
3051 access->coscheduled = &coscheduled_node;
3052 access = add_matching_sources(access, sink, data);
3054 flow = access_info_compute_flow_core(access);
3055 if (!flow)
3056 return isl_union_flow_free(uf);
3058 map = isl_map_domain_factor_range(isl_flow_get_no_source(flow, 1));
3059 uf->must_no_source = isl_union_map_union(uf->must_no_source,
3060 isl_union_map_from_map(map));
3061 map = isl_map_domain_factor_range(isl_flow_get_no_source(flow, 0));
3062 uf->may_no_source = isl_union_map_union(uf->may_no_source,
3063 isl_union_map_from_map(map));
3065 for (i = 0; i < flow->n_source; ++i) {
3066 isl_union_map *dep;
3068 map = isl_map_range_curry(isl_map_copy(flow->dep[i].map));
3069 map = isl_map_factor_range(map);
3070 dep = isl_union_map_from_map(map);
3071 if (flow->dep[i].must)
3072 uf->must_dep = isl_union_map_union(uf->must_dep, dep);
3073 else
3074 uf->may_dep = isl_union_map_union(uf->may_dep, dep);
3077 isl_flow_free(flow);
3079 return uf;
3082 /* Given a description of the "sink" accesses, the "source" accesses and
3083 * a schedule, compute for each instance of a sink access
3084 * and for each element accessed by that instance,
3085 * the possible or definite source accesses that last accessed the
3086 * element accessed by the sink access before this sink access
3087 * in the sense that there is no intermediate definite source access.
3088 * Only consider dependences between statement instances that belong
3089 * to the domain of the schedule.
3091 * The must_no_source and may_no_source elements of the result
3092 * are subsets of access->sink. The elements must_dep and may_dep
3093 * map domain elements of access->{may,must)_source to
3094 * domain elements of access->sink.
3096 * This function is used when a schedule tree representation
3097 * is available.
3099 * We extract the individual scheduled source and sink access relations
3100 * (taking into account the domain of the schedule) and
3101 * then compute dependences for each scheduled sink individually.
3103 static __isl_give isl_union_flow *compute_flow_schedule(
3104 __isl_take isl_union_access_info *access)
3106 struct isl_compute_flow_schedule_data data = { access };
3107 int i, n;
3108 isl_ctx *ctx;
3109 isl_space *space;
3110 isl_union_flow *flow;
3112 ctx = isl_union_access_info_get_ctx(access);
3114 data.n_sink = 0;
3115 data.n_source = 0;
3116 if (isl_schedule_foreach_schedule_node_top_down(access->schedule,
3117 &count_sink_source, &data) < 0)
3118 goto error;
3120 n = data.n_sink + data.n_source;
3121 data.sink = isl_calloc_array(ctx, struct isl_scheduled_access, n);
3122 if (n && !data.sink)
3123 goto error;
3124 data.source = data.sink + data.n_sink;
3126 data.n_sink = 0;
3127 data.n_source = 0;
3128 if (isl_schedule_foreach_schedule_node_top_down(access->schedule,
3129 &collect_sink_source, &data) < 0)
3130 goto error;
3132 space = isl_union_map_get_space(access->access[isl_access_sink]);
3133 flow = isl_union_flow_alloc(space);
3135 isl_compute_flow_schedule_data_align_params(&data);
3137 for (i = 0; i < data.n_sink; ++i)
3138 flow = compute_single_flow(flow, &data.sink[i], &data);
3140 isl_compute_flow_schedule_data_clear(&data);
3142 isl_union_access_info_free(access);
3143 return flow;
3144 error:
3145 isl_union_access_info_free(access);
3146 isl_compute_flow_schedule_data_clear(&data);
3147 return NULL;
3150 /* Given a description of the "sink" accesses, the "source" accesses and
3151 * a schedule, compute for each instance of a sink access
3152 * and for each element accessed by that instance,
3153 * the possible or definite source accesses that last accessed the
3154 * element accessed by the sink access before this sink access
3155 * in the sense that there is no intermediate definite source access.
3157 * The must_no_source and may_no_source elements of the result
3158 * are subsets of access->sink. The elements must_dep and may_dep
3159 * map domain elements of access->{may,must)_source to
3160 * domain elements of access->sink.
3162 * If any kills have been specified, then they are treated as
3163 * must-sources internally. Any dependence that purely derives
3164 * from an original kill is removed from the output.
3166 * We check whether the schedule is available as a schedule tree
3167 * or a schedule map and call the corresponding function to perform
3168 * the analysis.
3170 __isl_give isl_union_flow *isl_union_access_info_compute_flow(
3171 __isl_take isl_union_access_info *access)
3173 isl_bool has_kill;
3174 isl_union_map *must = NULL, *may = NULL;
3175 isl_union_flow *flow;
3177 has_kill = isl_union_access_has_kill(access);
3178 if (has_kill < 0)
3179 goto error;
3180 if (has_kill) {
3181 must = isl_union_access_info_get_must_source(access);
3182 may = isl_union_access_info_get_may_source(access);
3184 access = isl_union_access_info_add_kill_to_must_source(access);
3185 access = isl_union_access_info_normalize(access);
3186 if (!access)
3187 goto error;
3188 if (access->schedule)
3189 flow = compute_flow_schedule(access);
3190 else
3191 flow = compute_flow_union_map(access);
3192 if (has_kill)
3193 flow = isl_union_flow_drop_kill_source(flow, must, may);
3194 return flow;
3195 error:
3196 isl_union_access_info_free(access);
3197 isl_union_map_free(must);
3198 isl_union_map_free(may);
3199 return NULL;
3202 /* Print the information contained in "flow" to "p".
3203 * The information is printed as a YAML document.
3205 __isl_give isl_printer *isl_printer_print_union_flow(
3206 __isl_take isl_printer *p, __isl_keep isl_union_flow *flow)
3208 isl_union_map *umap;
3210 if (!flow)
3211 return isl_printer_free(p);
3213 p = isl_printer_yaml_start_mapping(p);
3214 umap = isl_union_flow_get_full_must_dependence(flow);
3215 p = print_union_map_field(p, "must_dependence", umap);
3216 isl_union_map_free(umap);
3217 umap = isl_union_flow_get_full_may_dependence(flow);
3218 p = print_union_map_field(p, "may_dependence", umap);
3219 isl_union_map_free(umap);
3220 p = print_union_map_field(p, "must_no_source", flow->must_no_source);
3221 umap = isl_union_flow_get_may_no_source(flow);
3222 p = print_union_map_field(p, "may_no_source", umap);
3223 isl_union_map_free(umap);
3224 p = isl_printer_yaml_end_mapping(p);
3226 return p;
3229 /* Return a string representation of the information in "flow".
3230 * The information is printed in flow format.
3232 __isl_give char *isl_union_flow_to_str(__isl_keep isl_union_flow *flow)
3234 isl_printer *p;
3235 char *s;
3237 if (!flow)
3238 return NULL;
3240 p = isl_printer_to_str(isl_union_flow_get_ctx(flow));
3241 p = isl_printer_set_yaml_style(p, ISL_YAML_STYLE_FLOW);
3242 p = isl_printer_print_union_flow(p, flow);
3243 s = isl_printer_get_str(p);
3244 isl_printer_free(p);
3246 return s;
3249 /* Given a collection of "sink" and "source" accesses,
3250 * compute for each iteration of a sink access
3251 * and for each element accessed by that iteration,
3252 * the source access in the list that last accessed the
3253 * element accessed by the sink access before this sink access.
3254 * Each access is given as a map from the loop iterators
3255 * to the array indices.
3256 * The result is a relations between source and sink
3257 * iterations and a subset of the domain of the sink accesses,
3258 * corresponding to those iterations that access an element
3259 * not previously accessed.
3261 * We collect the inputs in an isl_union_access_info object,
3262 * call isl_union_access_info_compute_flow and extract
3263 * the outputs from the result.
3265 int isl_union_map_compute_flow(__isl_take isl_union_map *sink,
3266 __isl_take isl_union_map *must_source,
3267 __isl_take isl_union_map *may_source,
3268 __isl_take isl_union_map *schedule,
3269 __isl_give isl_union_map **must_dep, __isl_give isl_union_map **may_dep,
3270 __isl_give isl_union_map **must_no_source,
3271 __isl_give isl_union_map **may_no_source)
3273 isl_union_access_info *access;
3274 isl_union_flow *flow;
3276 access = isl_union_access_info_from_sink(sink);
3277 access = isl_union_access_info_set_must_source(access, must_source);
3278 access = isl_union_access_info_set_may_source(access, may_source);
3279 access = isl_union_access_info_set_schedule_map(access, schedule);
3280 flow = isl_union_access_info_compute_flow(access);
3282 if (must_dep)
3283 *must_dep = isl_union_flow_get_must_dependence(flow);
3284 if (may_dep)
3285 *may_dep = isl_union_flow_get_non_must_dependence(flow);
3286 if (must_no_source)
3287 *must_no_source = isl_union_flow_get_must_no_source(flow);
3288 if (may_no_source)
3289 *may_no_source = isl_union_flow_get_non_must_no_source(flow);
3291 isl_union_flow_free(flow);
3293 if ((must_dep && !*must_dep) || (may_dep && !*may_dep) ||
3294 (must_no_source && !*must_no_source) ||
3295 (may_no_source && !*may_no_source))
3296 goto error;
3298 return 0;
3299 error:
3300 if (must_dep)
3301 *must_dep = isl_union_map_free(*must_dep);
3302 if (may_dep)
3303 *may_dep = isl_union_map_free(*may_dep);
3304 if (must_no_source)
3305 *must_no_source = isl_union_map_free(*must_no_source);
3306 if (may_no_source)
3307 *may_no_source = isl_union_map_free(*may_no_source);
3308 return -1;