isl_map_simplify.c: move up is_opposite
[isl.git] / isl_flow.c
blobb4ac76b59ac946d39a9c19fda5286a64060cd585
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 isl_bool (*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 __isl_null isl_flow *isl_flow_free(__isl_take isl_flow *deps)
524 int i;
526 if (!deps)
527 return NULL;
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);
537 return NULL;
540 isl_ctx *isl_flow_get_ctx(__isl_keep isl_flow *deps)
542 return deps ? isl_set_get_ctx(deps->must_no_source) : NULL;
545 /* Return a map that enforces that the domain iteration occurs after
546 * the range iteration at the given level.
547 * If level is odd, then the domain iteration should occur after
548 * the target iteration in their shared level/2 outermost loops.
549 * In this case we simply need to enforce that these outermost
550 * loop iterations are the same.
551 * If level is even, then the loop iterator of the domain should
552 * be greater than the loop iterator of the range at the last
553 * of the level/2 shared loops, i.e., loop level/2 - 1.
555 static __isl_give isl_map *after_at_level(__isl_take isl_space *dim, int level)
557 struct isl_basic_map *bmap;
559 if (level % 2)
560 bmap = isl_basic_map_equal(dim, level/2);
561 else
562 bmap = isl_basic_map_more_at(dim, level/2 - 1);
564 return isl_map_from_basic_map(bmap);
567 /* Compute the partial lexicographic maximum of "dep" on domain "sink",
568 * but first check if the user has set acc->restrict_fn and if so
569 * update either the input or the output of the maximization problem
570 * with respect to the resulting restriction.
572 * Since the user expects a mapping from sink iterations to source iterations,
573 * whereas the domain of "dep" is a wrapped map, mapping sink iterations
574 * to accessed array elements, we first need to project out the accessed
575 * sink array elements by applying acc->domain_map.
576 * Similarly, the sink restriction specified by the user needs to be
577 * converted back to the wrapped map.
579 static __isl_give isl_map *restricted_partial_lexmax(
580 __isl_keep isl_access_info *acc, __isl_take isl_map *dep,
581 int source, __isl_take isl_set *sink, __isl_give isl_set **empty)
583 isl_map *source_map;
584 isl_restriction *restr;
585 isl_set *sink_domain;
586 isl_set *sink_restr;
587 isl_map *res;
589 if (!acc->restrict_fn)
590 return isl_map_partial_lexmax(dep, sink, empty);
592 source_map = isl_map_copy(dep);
593 source_map = isl_map_apply_domain(source_map,
594 isl_map_copy(acc->domain_map));
595 sink_domain = isl_set_copy(sink);
596 sink_domain = isl_set_apply(sink_domain, isl_map_copy(acc->domain_map));
597 restr = acc->restrict_fn(source_map, sink_domain,
598 acc->source[source].data, acc->restrict_user);
599 isl_set_free(sink_domain);
600 isl_map_free(source_map);
602 if (!restr)
603 goto error;
604 if (restr->type == isl_restriction_type_input) {
605 dep = isl_map_intersect_range(dep, isl_set_copy(restr->source));
606 sink_restr = isl_set_copy(restr->sink);
607 sink_restr = isl_set_apply(sink_restr,
608 isl_map_reverse(isl_map_copy(acc->domain_map)));
609 sink = isl_set_intersect(sink, sink_restr);
610 } else if (restr->type == isl_restriction_type_empty) {
611 isl_space *space = isl_map_get_space(dep);
612 isl_map_free(dep);
613 dep = isl_map_empty(space);
616 res = isl_map_partial_lexmax(dep, sink, empty);
618 if (restr->type == isl_restriction_type_output)
619 res = isl_map_intersect_range(res, isl_set_copy(restr->source));
621 isl_restriction_free(restr);
622 return res;
623 error:
624 isl_map_free(dep);
625 isl_set_free(sink);
626 *empty = NULL;
627 return NULL;
630 /* Compute the last iteration of must source j that precedes the sink
631 * at the given level for sink iterations in set_C.
632 * The subset of set_C for which no such iteration can be found is returned
633 * in *empty.
635 static struct isl_map *last_source(struct isl_access_info *acc,
636 struct isl_set *set_C,
637 int j, int level, struct isl_set **empty)
639 struct isl_map *read_map;
640 struct isl_map *write_map;
641 struct isl_map *dep_map;
642 struct isl_map *after;
643 struct isl_map *result;
645 read_map = isl_map_copy(acc->sink.map);
646 write_map = isl_map_copy(acc->source[j].map);
647 write_map = isl_map_reverse(write_map);
648 dep_map = isl_map_apply_range(read_map, write_map);
649 after = after_at_level(isl_map_get_space(dep_map), level);
650 dep_map = isl_map_intersect(dep_map, after);
651 result = restricted_partial_lexmax(acc, dep_map, j, set_C, empty);
652 result = isl_map_reverse(result);
654 return result;
657 /* For a given mapping between iterations of must source j and iterations
658 * of the sink, compute the last iteration of must source k preceding
659 * the sink at level before_level for any of the sink iterations,
660 * but following the corresponding iteration of must source j at level
661 * after_level.
663 static struct isl_map *last_later_source(struct isl_access_info *acc,
664 struct isl_map *old_map,
665 int j, int before_level,
666 int k, int after_level,
667 struct isl_set **empty)
669 isl_space *dim;
670 struct isl_set *set_C;
671 struct isl_map *read_map;
672 struct isl_map *write_map;
673 struct isl_map *dep_map;
674 struct isl_map *after_write;
675 struct isl_map *before_read;
676 struct isl_map *result;
678 set_C = isl_map_range(isl_map_copy(old_map));
679 read_map = isl_map_copy(acc->sink.map);
680 write_map = isl_map_copy(acc->source[k].map);
682 write_map = isl_map_reverse(write_map);
683 dep_map = isl_map_apply_range(read_map, write_map);
684 dim = space_align_and_join(isl_map_get_space(acc->source[k].map),
685 isl_space_reverse(isl_map_get_space(acc->source[j].map)));
686 after_write = after_at_level(dim, after_level);
687 after_write = isl_map_apply_range(after_write, old_map);
688 after_write = isl_map_reverse(after_write);
689 dep_map = isl_map_intersect(dep_map, after_write);
690 before_read = after_at_level(isl_map_get_space(dep_map), before_level);
691 dep_map = isl_map_intersect(dep_map, before_read);
692 result = restricted_partial_lexmax(acc, dep_map, k, set_C, empty);
693 result = isl_map_reverse(result);
695 return result;
698 /* Given a shared_level between two accesses, return 1 if the
699 * the first can precede the second at the requested target_level.
700 * If the target level is odd, i.e., refers to a statement level
701 * dimension, then first needs to precede second at the requested
702 * level, i.e., shared_level must be equal to target_level.
703 * If the target level is odd, then the two loops should share
704 * at least the requested number of outer loops.
706 static int can_precede_at_level(int shared_level, int target_level)
708 if (shared_level < target_level)
709 return 0;
710 if ((target_level % 2) && shared_level > target_level)
711 return 0;
712 return 1;
715 /* Given a possible flow dependence temp_rel[j] between source j and the sink
716 * at level sink_level, remove those elements for which
717 * there is an iteration of another source k < j that is closer to the sink.
718 * The flow dependences temp_rel[k] are updated with the improved sources.
719 * Any improved source needs to precede the sink at the same level
720 * and needs to follow source j at the same or a deeper level.
721 * The lower this level, the later the execution date of source k.
722 * We therefore consider lower levels first.
724 * If temp_rel[j] is empty, then there can be no improvement and
725 * we return immediately.
727 * This function returns isl_stat_ok in case it was executed successfully and
728 * isl_stat_error in case of errors during the execution of this function.
730 static isl_stat intermediate_sources(__isl_keep isl_access_info *acc,
731 struct isl_map **temp_rel, int j, int sink_level)
733 int k, level;
734 int depth = 2 * isl_map_dim(acc->source[j].map, isl_dim_in) + 1;
736 if (isl_map_plain_is_empty(temp_rel[j]))
737 return isl_stat_ok;
739 for (k = j - 1; k >= 0; --k) {
740 int plevel, plevel2;
741 plevel = acc->level_before(acc->source[k].data, acc->sink.data);
742 if (plevel < 0)
743 return isl_stat_error;
744 if (!can_precede_at_level(plevel, sink_level))
745 continue;
747 plevel2 = acc->level_before(acc->source[j].data,
748 acc->source[k].data);
749 if (plevel2 < 0)
750 return isl_stat_error;
752 for (level = sink_level; level <= depth; ++level) {
753 struct isl_map *T;
754 struct isl_set *trest;
755 struct isl_map *copy;
757 if (!can_precede_at_level(plevel2, level))
758 continue;
760 copy = isl_map_copy(temp_rel[j]);
761 T = last_later_source(acc, copy, j, sink_level, k,
762 level, &trest);
763 if (isl_map_plain_is_empty(T)) {
764 isl_set_free(trest);
765 isl_map_free(T);
766 continue;
768 temp_rel[j] = isl_map_intersect_range(temp_rel[j], trest);
769 temp_rel[k] = isl_map_union_disjoint(temp_rel[k], T);
773 return isl_stat_ok;
776 /* Compute all iterations of may source j that precedes the sink at the given
777 * level for sink iterations in set_C.
779 static __isl_give isl_map *all_sources(__isl_keep isl_access_info *acc,
780 __isl_take isl_set *set_C, int j, int level)
782 isl_map *read_map;
783 isl_map *write_map;
784 isl_map *dep_map;
785 isl_map *after;
787 read_map = isl_map_copy(acc->sink.map);
788 read_map = isl_map_intersect_domain(read_map, set_C);
789 write_map = isl_map_copy(acc->source[acc->n_must + j].map);
790 write_map = isl_map_reverse(write_map);
791 dep_map = isl_map_apply_range(read_map, write_map);
792 after = after_at_level(isl_map_get_space(dep_map), level);
793 dep_map = isl_map_intersect(dep_map, after);
795 return isl_map_reverse(dep_map);
798 /* For a given mapping between iterations of must source k and iterations
799 * of the sink, compute all iterations of may source j preceding
800 * the sink at level before_level for any of the sink iterations,
801 * but following the corresponding iteration of must source k at level
802 * after_level.
804 static __isl_give isl_map *all_later_sources(__isl_keep isl_access_info *acc,
805 __isl_take isl_map *old_map,
806 int j, int before_level, int k, int after_level)
808 isl_space *dim;
809 isl_set *set_C;
810 isl_map *read_map;
811 isl_map *write_map;
812 isl_map *dep_map;
813 isl_map *after_write;
814 isl_map *before_read;
816 set_C = isl_map_range(isl_map_copy(old_map));
817 read_map = isl_map_copy(acc->sink.map);
818 read_map = isl_map_intersect_domain(read_map, set_C);
819 write_map = isl_map_copy(acc->source[acc->n_must + j].map);
821 write_map = isl_map_reverse(write_map);
822 dep_map = isl_map_apply_range(read_map, write_map);
823 dim = isl_space_join(isl_map_get_space(acc->source[acc->n_must + j].map),
824 isl_space_reverse(isl_map_get_space(acc->source[k].map)));
825 after_write = after_at_level(dim, after_level);
826 after_write = isl_map_apply_range(after_write, old_map);
827 after_write = isl_map_reverse(after_write);
828 dep_map = isl_map_intersect(dep_map, after_write);
829 before_read = after_at_level(isl_map_get_space(dep_map), before_level);
830 dep_map = isl_map_intersect(dep_map, before_read);
831 return isl_map_reverse(dep_map);
834 /* Given the must and may dependence relations for the must accesses
835 * for level sink_level, check if there are any accesses of may access j
836 * that occur in between and return their union.
837 * If some of these accesses are intermediate with respect to
838 * (previously thought to be) must dependences, then these
839 * must dependences are turned into may dependences.
841 static __isl_give isl_map *all_intermediate_sources(
842 __isl_keep isl_access_info *acc, __isl_take isl_map *map,
843 struct isl_map **must_rel, struct isl_map **may_rel,
844 int j, int sink_level)
846 int k, level;
847 int depth = 2 * isl_map_dim(acc->source[acc->n_must + j].map,
848 isl_dim_in) + 1;
850 for (k = 0; k < acc->n_must; ++k) {
851 int plevel;
853 if (isl_map_plain_is_empty(may_rel[k]) &&
854 isl_map_plain_is_empty(must_rel[k]))
855 continue;
857 plevel = acc->level_before(acc->source[k].data,
858 acc->source[acc->n_must + j].data);
859 if (plevel < 0)
860 return isl_map_free(map);
862 for (level = sink_level; level <= depth; ++level) {
863 isl_map *T;
864 isl_map *copy;
865 isl_set *ran;
867 if (!can_precede_at_level(plevel, level))
868 continue;
870 copy = isl_map_copy(may_rel[k]);
871 T = all_later_sources(acc, copy, j, sink_level, k, level);
872 map = isl_map_union(map, T);
874 copy = isl_map_copy(must_rel[k]);
875 T = all_later_sources(acc, copy, j, sink_level, k, level);
876 ran = isl_map_range(isl_map_copy(T));
877 map = isl_map_union(map, T);
878 may_rel[k] = isl_map_union_disjoint(may_rel[k],
879 isl_map_intersect_range(isl_map_copy(must_rel[k]),
880 isl_set_copy(ran)));
881 T = isl_map_from_domain_and_range(
882 isl_set_universe(
883 isl_space_domain(isl_map_get_space(must_rel[k]))),
884 ran);
885 must_rel[k] = isl_map_subtract(must_rel[k], T);
889 return map;
892 /* Given a dependence relation "old_map" between a must-source and the sink,
893 * return a subset of the dependences, augmented with instances
894 * of the source at position "pos" in "acc" that are coscheduled
895 * with the must-source and that access the same element.
896 * That is, if the input lives in a space T -> K, then the output
897 * lives in the space [T -> S] -> K, with S the space of source "pos", and
898 * the domain factor of the domain product is a subset of the input.
899 * The sources are considered to be coscheduled if they have the same values
900 * for the initial "depth" coordinates.
902 * First construct a dependence relation S -> K and a mapping
903 * between coscheduled sources T -> S.
904 * The second is combined with the original dependence relation T -> K
905 * to form a relation in T -> [S -> K], which is subsequently
906 * uncurried to [T -> S] -> K.
907 * This result is then intersected with the dependence relation S -> K
908 * to form the output.
910 * In case a negative depth is given, NULL is returned to indicate an error.
912 static __isl_give isl_map *coscheduled_source(__isl_keep isl_access_info *acc,
913 __isl_keep isl_map *old_map, int pos, int depth)
915 isl_space *space;
916 isl_set *set_C;
917 isl_map *read_map;
918 isl_map *write_map;
919 isl_map *dep_map;
920 isl_map *equal;
921 isl_map *map;
923 if (depth < 0)
924 return NULL;
926 set_C = isl_map_range(isl_map_copy(old_map));
927 read_map = isl_map_copy(acc->sink.map);
928 read_map = isl_map_intersect_domain(read_map, set_C);
929 write_map = isl_map_copy(acc->source[pos].map);
930 dep_map = isl_map_domain_product(write_map, read_map);
931 dep_map = isl_set_unwrap(isl_map_domain(dep_map));
932 space = isl_space_join(isl_map_get_space(old_map),
933 isl_space_reverse(isl_map_get_space(dep_map)));
934 equal = isl_map_from_basic_map(isl_basic_map_equal(space, depth));
935 map = isl_map_range_product(equal, isl_map_copy(old_map));
936 map = isl_map_uncurry(map);
937 map = isl_map_intersect_domain_factor_range(map, dep_map);
939 return map;
942 /* After the dependences derived from a must-source have been computed
943 * at a certain level, check if any of the sources of the must-dependences
944 * may be coscheduled with other sources.
945 * If they are any such sources, then there is no way of determining
946 * which of the sources actually comes last and the must-dependences
947 * need to be turned into may-dependences, while dependences from
948 * the other sources need to be added to the may-dependences as well.
949 * "acc" describes the sources and a callback for checking whether
950 * two sources may be coscheduled. If acc->coscheduled is NULL then
951 * the sources are assumed not to be coscheduled.
952 * "must_rel" and "may_rel" describe the must and may-dependence relations
953 * computed at the current level for the must-sources. Some of the dependences
954 * may be moved from "must_rel" to "may_rel".
955 * "flow" contains all dependences computed so far (apart from those
956 * in "must_rel" and "may_rel") and may be updated with additional
957 * dependences derived from may-sources.
959 * In particular, consider all the must-sources with a non-empty
960 * dependence relation in "must_rel". They are considered in reverse
961 * order because that is the order in which they are considered in the caller.
962 * If any of the must-sources are coscheduled, then the last one
963 * is the one that will have a corresponding dependence relation.
964 * For each must-source i, consider both all the previous must-sources
965 * and all the may-sources. If any of those may be coscheduled with
966 * must-source i, then compute the coscheduled instances that access
967 * the same memory elements. The result is a relation [T -> S] -> K.
968 * The projection onto T -> K is a subset of the must-dependence relation
969 * that needs to be turned into may-dependences.
970 * The projection onto S -> K needs to be added to the may-dependences
971 * of source S.
972 * Since a given must-source instance may be coscheduled with several
973 * other source instances, the dependences that need to be turned
974 * into may-dependences are first collected and only actually removed
975 * from the must-dependences after all other sources have been considered.
977 static __isl_give isl_flow *handle_coscheduled(__isl_keep isl_access_info *acc,
978 __isl_keep isl_map **must_rel, __isl_keep isl_map **may_rel,
979 __isl_take isl_flow *flow)
981 int i, j;
983 if (!acc->coscheduled)
984 return flow;
985 for (i = acc->n_must - 1; i >= 0; --i) {
986 isl_map *move;
988 if (isl_map_plain_is_empty(must_rel[i]))
989 continue;
990 move = isl_map_empty(isl_map_get_space(must_rel[i]));
991 for (j = i - 1; j >= 0; --j) {
992 int depth;
993 isl_bool coscheduled;
994 isl_map *map, *factor;
996 coscheduled = acc->coscheduled(acc->source[i].data,
997 acc->source[j].data);
998 if (coscheduled < 0) {
999 isl_map_free(move);
1000 return isl_flow_free(flow);
1002 if (!coscheduled)
1003 continue;
1004 depth = acc->level_before(acc->source[i].data,
1005 acc->source[j].data) / 2;
1006 map = coscheduled_source(acc, must_rel[i], j, depth);
1007 factor = isl_map_domain_factor_range(isl_map_copy(map));
1008 may_rel[j] = isl_map_union(may_rel[j], factor);
1009 map = isl_map_domain_factor_domain(map);
1010 move = isl_map_union(move, map);
1012 for (j = 0; j < acc->n_may; ++j) {
1013 int depth, pos;
1014 isl_bool coscheduled;
1015 isl_map *map, *factor;
1017 pos = acc->n_must + j;
1018 coscheduled = acc->coscheduled(acc->source[i].data,
1019 acc->source[pos].data);
1020 if (coscheduled < 0) {
1021 isl_map_free(move);
1022 return isl_flow_free(flow);
1024 if (!coscheduled)
1025 continue;
1026 depth = acc->level_before(acc->source[i].data,
1027 acc->source[pos].data) / 2;
1028 map = coscheduled_source(acc, must_rel[i], pos, depth);
1029 factor = isl_map_domain_factor_range(isl_map_copy(map));
1030 pos = 2 * acc->n_must + j;
1031 flow->dep[pos].map = isl_map_union(flow->dep[pos].map,
1032 factor);
1033 map = isl_map_domain_factor_domain(map);
1034 move = isl_map_union(move, map);
1036 must_rel[i] = isl_map_subtract(must_rel[i], isl_map_copy(move));
1037 may_rel[i] = isl_map_union(may_rel[i], move);
1040 return flow;
1043 /* Compute dependences for the case where all accesses are "may"
1044 * accesses, which boils down to computing memory based dependences.
1045 * The generic algorithm would also work in this case, but it would
1046 * be overkill to use it.
1048 static __isl_give isl_flow *compute_mem_based_dependences(
1049 __isl_keep isl_access_info *acc)
1051 int i;
1052 isl_set *mustdo;
1053 isl_set *maydo;
1054 isl_flow *res;
1056 res = isl_flow_alloc(acc);
1057 if (!res)
1058 return NULL;
1060 mustdo = isl_map_domain(isl_map_copy(acc->sink.map));
1061 maydo = isl_set_copy(mustdo);
1063 for (i = 0; i < acc->n_may; ++i) {
1064 int plevel;
1065 int is_before;
1066 isl_space *dim;
1067 isl_map *before;
1068 isl_map *dep;
1070 plevel = acc->level_before(acc->source[i].data, acc->sink.data);
1071 if (plevel < 0)
1072 goto error;
1074 is_before = plevel & 1;
1075 plevel >>= 1;
1077 dim = isl_map_get_space(res->dep[i].map);
1078 if (is_before)
1079 before = isl_map_lex_le_first(dim, plevel);
1080 else
1081 before = isl_map_lex_lt_first(dim, plevel);
1082 dep = isl_map_apply_range(isl_map_copy(acc->source[i].map),
1083 isl_map_reverse(isl_map_copy(acc->sink.map)));
1084 dep = isl_map_intersect(dep, before);
1085 mustdo = isl_set_subtract(mustdo,
1086 isl_map_range(isl_map_copy(dep)));
1087 res->dep[i].map = isl_map_union(res->dep[i].map, dep);
1090 res->may_no_source = isl_set_subtract(maydo, isl_set_copy(mustdo));
1091 res->must_no_source = mustdo;
1093 return res;
1094 error:
1095 isl_set_free(mustdo);
1096 isl_set_free(maydo);
1097 isl_flow_free(res);
1098 return NULL;
1101 /* Compute dependences for the case where there is at least one
1102 * "must" access.
1104 * The core algorithm considers all levels in which a source may precede
1105 * the sink, where a level may either be a statement level or a loop level.
1106 * The outermost statement level is 1, the first loop level is 2, etc...
1107 * The algorithm basically does the following:
1108 * for all levels l of the read access from innermost to outermost
1109 * for all sources w that may precede the sink access at that level
1110 * compute the last iteration of the source that precedes the sink access
1111 * at that level
1112 * add result to possible last accesses at level l of source w
1113 * for all sources w2 that we haven't considered yet at this level that may
1114 * also precede the sink access
1115 * for all levels l2 of w from l to innermost
1116 * for all possible last accesses dep of w at l
1117 * compute last iteration of w2 between the source and sink
1118 * of dep
1119 * add result to possible last accesses at level l of write w2
1120 * and replace possible last accesses dep by the remainder
1123 * The above algorithm is applied to the must access. During the course
1124 * of the algorithm, we keep track of sink iterations that still
1125 * need to be considered. These iterations are split into those that
1126 * haven't been matched to any source access (mustdo) and those that have only
1127 * been matched to may accesses (maydo).
1128 * At the end of each level, must-sources and may-sources that are coscheduled
1129 * with the sources of the must-dependences at that level are considered.
1130 * If any coscheduled instances are found, then corresponding may-dependences
1131 * are added and the original must-dependences are turned into may-dependences.
1132 * Afterwards, the may accesses that occur after must-dependence sources
1133 * are considered.
1134 * In particular, we consider may accesses that precede the remaining
1135 * sink iterations, moving elements from mustdo to maydo when appropriate,
1136 * and may accesses that occur between a must source and a sink of any
1137 * dependences found at the current level, turning must dependences into
1138 * may dependences when appropriate.
1141 static __isl_give isl_flow *compute_val_based_dependences(
1142 __isl_keep isl_access_info *acc)
1144 isl_ctx *ctx;
1145 isl_flow *res;
1146 isl_set *mustdo = NULL;
1147 isl_set *maydo = NULL;
1148 int level, j;
1149 int depth;
1150 isl_map **must_rel = NULL;
1151 isl_map **may_rel = NULL;
1153 if (!acc)
1154 return NULL;
1156 res = isl_flow_alloc(acc);
1157 if (!res)
1158 goto error;
1159 ctx = isl_map_get_ctx(acc->sink.map);
1161 depth = 2 * isl_map_dim(acc->sink.map, isl_dim_in) + 1;
1162 mustdo = isl_map_domain(isl_map_copy(acc->sink.map));
1163 maydo = isl_set_empty(isl_set_get_space(mustdo));
1164 if (!mustdo || !maydo)
1165 goto error;
1166 if (isl_set_plain_is_empty(mustdo))
1167 goto done;
1169 must_rel = isl_calloc_array(ctx, struct isl_map *, acc->n_must);
1170 may_rel = isl_calloc_array(ctx, struct isl_map *, acc->n_must);
1171 if (!must_rel || !may_rel)
1172 goto error;
1174 for (level = depth; level >= 1; --level) {
1175 for (j = acc->n_must-1; j >=0; --j) {
1176 isl_space *space;
1177 space = isl_map_get_space(res->dep[2 * j].map);
1178 must_rel[j] = isl_map_empty(space);
1179 may_rel[j] = isl_map_copy(must_rel[j]);
1182 for (j = acc->n_must - 1; j >= 0; --j) {
1183 struct isl_map *T;
1184 struct isl_set *rest;
1185 int plevel;
1187 plevel = acc->level_before(acc->source[j].data,
1188 acc->sink.data);
1189 if (plevel < 0)
1190 goto error;
1191 if (!can_precede_at_level(plevel, level))
1192 continue;
1194 T = last_source(acc, mustdo, j, level, &rest);
1195 must_rel[j] = isl_map_union_disjoint(must_rel[j], T);
1196 mustdo = rest;
1198 if (intermediate_sources(acc, must_rel, j, level) < 0)
1199 goto error;
1201 T = last_source(acc, maydo, j, level, &rest);
1202 may_rel[j] = isl_map_union_disjoint(may_rel[j], T);
1203 maydo = rest;
1205 if (intermediate_sources(acc, may_rel, j, level) < 0)
1206 goto error;
1208 if (isl_set_plain_is_empty(mustdo) &&
1209 isl_set_plain_is_empty(maydo))
1210 break;
1212 for (j = j - 1; j >= 0; --j) {
1213 int plevel;
1215 plevel = acc->level_before(acc->source[j].data,
1216 acc->sink.data);
1217 if (plevel < 0)
1218 goto error;
1219 if (!can_precede_at_level(plevel, level))
1220 continue;
1222 if (intermediate_sources(acc, must_rel, j, level) < 0)
1223 goto error;
1224 if (intermediate_sources(acc, may_rel, j, level) < 0)
1225 goto error;
1228 res = handle_coscheduled(acc, must_rel, may_rel, res);
1229 if (!res)
1230 goto error;
1232 for (j = 0; j < acc->n_may; ++j) {
1233 int plevel;
1234 isl_map *T;
1235 isl_set *ran;
1237 plevel = acc->level_before(acc->source[acc->n_must + j].data,
1238 acc->sink.data);
1239 if (plevel < 0)
1240 goto error;
1241 if (!can_precede_at_level(plevel, level))
1242 continue;
1244 T = all_sources(acc, isl_set_copy(maydo), j, level);
1245 res->dep[2 * acc->n_must + j].map =
1246 isl_map_union(res->dep[2 * acc->n_must + j].map, T);
1247 T = all_sources(acc, isl_set_copy(mustdo), j, level);
1248 ran = isl_map_range(isl_map_copy(T));
1249 res->dep[2 * acc->n_must + j].map =
1250 isl_map_union(res->dep[2 * acc->n_must + j].map, T);
1251 mustdo = isl_set_subtract(mustdo, isl_set_copy(ran));
1252 maydo = isl_set_union_disjoint(maydo, ran);
1254 T = res->dep[2 * acc->n_must + j].map;
1255 T = all_intermediate_sources(acc, T, must_rel, may_rel,
1256 j, level);
1257 res->dep[2 * acc->n_must + j].map = T;
1260 for (j = acc->n_must - 1; j >= 0; --j) {
1261 res->dep[2 * j].map =
1262 isl_map_union_disjoint(res->dep[2 * j].map,
1263 must_rel[j]);
1264 res->dep[2 * j + 1].map =
1265 isl_map_union_disjoint(res->dep[2 * j + 1].map,
1266 may_rel[j]);
1269 if (isl_set_plain_is_empty(mustdo) &&
1270 isl_set_plain_is_empty(maydo))
1271 break;
1274 free(must_rel);
1275 free(may_rel);
1276 done:
1277 res->must_no_source = mustdo;
1278 res->may_no_source = maydo;
1279 return res;
1280 error:
1281 if (must_rel)
1282 for (j = 0; j < acc->n_must; ++j)
1283 isl_map_free(must_rel[j]);
1284 if (may_rel)
1285 for (j = 0; j < acc->n_must; ++j)
1286 isl_map_free(may_rel[j]);
1287 isl_flow_free(res);
1288 isl_set_free(mustdo);
1289 isl_set_free(maydo);
1290 free(must_rel);
1291 free(may_rel);
1292 return NULL;
1295 /* Given a "sink" access, a list of n "source" accesses,
1296 * compute for each iteration of the sink access
1297 * and for each element accessed by that iteration,
1298 * the source access in the list that last accessed the
1299 * element accessed by the sink access before this sink access.
1300 * Each access is given as a map from the loop iterators
1301 * to the array indices.
1302 * The result is a list of n relations between source and sink
1303 * iterations and a subset of the domain of the sink access,
1304 * corresponding to those iterations that access an element
1305 * not previously accessed.
1307 * To deal with multi-valued sink access relations, the sink iteration
1308 * domain is first extended with dimensions that correspond to the data
1309 * space. However, these extra dimensions are not projected out again.
1310 * It is up to the caller to decide whether these dimensions should be kept.
1312 static __isl_give isl_flow *access_info_compute_flow_core(
1313 __isl_take isl_access_info *acc)
1315 struct isl_flow *res = NULL;
1317 if (!acc)
1318 return NULL;
1320 acc->sink.map = isl_map_range_map(acc->sink.map);
1321 if (!acc->sink.map)
1322 goto error;
1324 if (acc->n_must == 0)
1325 res = compute_mem_based_dependences(acc);
1326 else {
1327 acc = isl_access_info_sort_sources(acc);
1328 res = compute_val_based_dependences(acc);
1330 acc = isl_access_info_free(acc);
1331 if (!res)
1332 return NULL;
1333 if (!res->must_no_source || !res->may_no_source)
1334 goto error;
1335 return res;
1336 error:
1337 isl_access_info_free(acc);
1338 isl_flow_free(res);
1339 return NULL;
1342 /* Given a "sink" access, a list of n "source" accesses,
1343 * compute for each iteration of the sink access
1344 * and for each element accessed by that iteration,
1345 * the source access in the list that last accessed the
1346 * element accessed by the sink access before this sink access.
1347 * Each access is given as a map from the loop iterators
1348 * to the array indices.
1349 * The result is a list of n relations between source and sink
1350 * iterations and a subset of the domain of the sink access,
1351 * corresponding to those iterations that access an element
1352 * not previously accessed.
1354 * To deal with multi-valued sink access relations,
1355 * access_info_compute_flow_core extends the sink iteration domain
1356 * with dimensions that correspond to the data space. These extra dimensions
1357 * are projected out from the result of access_info_compute_flow_core.
1359 __isl_give isl_flow *isl_access_info_compute_flow(__isl_take isl_access_info *acc)
1361 int j;
1362 struct isl_flow *res;
1364 if (!acc)
1365 return NULL;
1367 acc->domain_map = isl_map_domain_map(isl_map_copy(acc->sink.map));
1368 res = access_info_compute_flow_core(acc);
1369 if (!res)
1370 return NULL;
1372 for (j = 0; j < res->n_source; ++j) {
1373 res->dep[j].map = isl_map_range_factor_domain(res->dep[j].map);
1374 if (!res->dep[j].map)
1375 goto error;
1378 return res;
1379 error:
1380 isl_flow_free(res);
1381 return NULL;
1385 /* Keep track of some information about a schedule for a given
1386 * access. In particular, keep track of which dimensions
1387 * have a constant value and of the actual constant values.
1389 struct isl_sched_info {
1390 int *is_cst;
1391 isl_vec *cst;
1394 static void sched_info_free(__isl_take struct isl_sched_info *info)
1396 if (!info)
1397 return;
1398 isl_vec_free(info->cst);
1399 free(info->is_cst);
1400 free(info);
1403 /* Extract information on the constant dimensions of the schedule
1404 * for a given access. The "map" is of the form
1406 * [S -> D] -> A
1408 * with S the schedule domain, D the iteration domain and A the data domain.
1410 static __isl_give struct isl_sched_info *sched_info_alloc(
1411 __isl_keep isl_map *map)
1413 isl_ctx *ctx;
1414 isl_space *space;
1415 struct isl_sched_info *info;
1416 int i, n;
1418 if (!map)
1419 return NULL;
1421 space = isl_space_unwrap(isl_space_domain(isl_map_get_space(map)));
1422 if (!space)
1423 return NULL;
1424 n = isl_space_dim(space, isl_dim_in);
1425 isl_space_free(space);
1427 ctx = isl_map_get_ctx(map);
1428 info = isl_alloc_type(ctx, struct isl_sched_info);
1429 if (!info)
1430 return NULL;
1431 info->is_cst = isl_alloc_array(ctx, int, n);
1432 info->cst = isl_vec_alloc(ctx, n);
1433 if (n && (!info->is_cst || !info->cst))
1434 goto error;
1436 for (i = 0; i < n; ++i) {
1437 isl_val *v;
1439 v = isl_map_plain_get_val_if_fixed(map, isl_dim_in, i);
1440 if (!v)
1441 goto error;
1442 info->is_cst[i] = !isl_val_is_nan(v);
1443 if (info->is_cst[i])
1444 info->cst = isl_vec_set_element_val(info->cst, i, v);
1445 else
1446 isl_val_free(v);
1449 return info;
1450 error:
1451 sched_info_free(info);
1452 return NULL;
1455 /* The different types of access relations that isl_union_access_info
1456 * keeps track of.
1458 * "isl_access_sink" represents the sink accesses.
1459 * "isl_access_must_source" represents the definite source accesses.
1460 * "isl_access_may_source" represents the possible source accesses.
1461 * "isl_access_kill" represents the kills.
1463 * isl_access_sink is sometimes treated differently and
1464 * should therefore appear first.
1466 enum isl_access_type {
1467 isl_access_sink,
1468 isl_access_must_source,
1469 isl_access_may_source,
1470 isl_access_kill,
1471 isl_access_end
1474 /* This structure represents the input for a dependence analysis computation.
1476 * "access" contains the access relations.
1478 * "schedule" or "schedule_map" represents the execution order.
1479 * Exactly one of these fields should be NULL. The other field
1480 * determines the execution order.
1482 * The domains of these four maps refer to the same iteration spaces(s).
1483 * The ranges of the first three maps also refer to the same data space(s).
1485 * After a call to isl_union_access_info_introduce_schedule,
1486 * the "schedule_map" field no longer contains useful information.
1488 struct isl_union_access_info {
1489 isl_union_map *access[isl_access_end];
1491 isl_schedule *schedule;
1492 isl_union_map *schedule_map;
1495 /* Free "access" and return NULL.
1497 __isl_null isl_union_access_info *isl_union_access_info_free(
1498 __isl_take isl_union_access_info *access)
1500 enum isl_access_type i;
1502 if (!access)
1503 return NULL;
1505 for (i = isl_access_sink; i < isl_access_end; ++i)
1506 isl_union_map_free(access->access[i]);
1507 isl_schedule_free(access->schedule);
1508 isl_union_map_free(access->schedule_map);
1509 free(access);
1511 return NULL;
1514 /* Return the isl_ctx to which "access" belongs.
1516 isl_ctx *isl_union_access_info_get_ctx(__isl_keep isl_union_access_info *access)
1518 if (!access)
1519 return NULL;
1520 return isl_union_map_get_ctx(access->access[isl_access_sink]);
1523 /* Construct an empty (invalid) isl_union_access_info object.
1524 * The caller is responsible for setting the sink access relation and
1525 * initializing all the other fields, e.g., by calling
1526 * isl_union_access_info_init.
1528 static __isl_give isl_union_access_info *isl_union_access_info_alloc(
1529 isl_ctx *ctx)
1531 return isl_calloc_type(ctx, isl_union_access_info);
1534 /* Initialize all the fields of "info", except the sink access relation,
1535 * which is assumed to have been set by the caller.
1537 * By default, we use the schedule field of the isl_union_access_info,
1538 * but this may be overridden by a call
1539 * to isl_union_access_info_set_schedule_map.
1541 static __isl_give isl_union_access_info *isl_union_access_info_init(
1542 __isl_take isl_union_access_info *info)
1544 isl_space *space;
1545 isl_union_map *empty;
1546 enum isl_access_type i;
1548 if (!info)
1549 return NULL;
1550 if (!info->access[isl_access_sink])
1551 return isl_union_access_info_free(info);
1553 space = isl_union_map_get_space(info->access[isl_access_sink]);
1554 empty = isl_union_map_empty(isl_space_copy(space));
1555 for (i = isl_access_sink + 1; i < isl_access_end; ++i)
1556 if (!info->access[i])
1557 info->access[i] = isl_union_map_copy(empty);
1558 isl_union_map_free(empty);
1559 if (!info->schedule && !info->schedule_map)
1560 info->schedule = isl_schedule_empty(isl_space_copy(space));
1561 isl_space_free(space);
1563 for (i = isl_access_sink + 1; i < isl_access_end; ++i)
1564 if (!info->access[i])
1565 return isl_union_access_info_free(info);
1566 if (!info->schedule && !info->schedule_map)
1567 return isl_union_access_info_free(info);
1569 return info;
1572 /* Create a new isl_union_access_info with the given sink accesses and
1573 * and no other accesses or schedule information.
1575 __isl_give isl_union_access_info *isl_union_access_info_from_sink(
1576 __isl_take isl_union_map *sink)
1578 isl_ctx *ctx;
1579 isl_union_access_info *access;
1581 if (!sink)
1582 return NULL;
1583 ctx = isl_union_map_get_ctx(sink);
1584 access = isl_union_access_info_alloc(ctx);
1585 if (!access)
1586 goto error;
1587 access->access[isl_access_sink] = sink;
1588 return isl_union_access_info_init(access);
1589 error:
1590 isl_union_map_free(sink);
1591 return NULL;
1594 /* Replace the access relation of type "type" of "info" by "access".
1596 static __isl_give isl_union_access_info *isl_union_access_info_set(
1597 __isl_take isl_union_access_info *info,
1598 enum isl_access_type type, __isl_take isl_union_map *access)
1600 if (!info || !access)
1601 goto error;
1603 isl_union_map_free(info->access[type]);
1604 info->access[type] = access;
1606 return info;
1607 error:
1608 isl_union_access_info_free(info);
1609 isl_union_map_free(access);
1610 return NULL;
1613 /* Replace the definite source accesses of "access" by "must_source".
1615 __isl_give isl_union_access_info *isl_union_access_info_set_must_source(
1616 __isl_take isl_union_access_info *access,
1617 __isl_take isl_union_map *must_source)
1619 return isl_union_access_info_set(access, isl_access_must_source,
1620 must_source);
1623 /* Replace the possible source accesses of "access" by "may_source".
1625 __isl_give isl_union_access_info *isl_union_access_info_set_may_source(
1626 __isl_take isl_union_access_info *access,
1627 __isl_take isl_union_map *may_source)
1629 return isl_union_access_info_set(access, isl_access_may_source,
1630 may_source);
1633 /* Replace the kills of "info" by "kill".
1635 __isl_give isl_union_access_info *isl_union_access_info_set_kill(
1636 __isl_take isl_union_access_info *info, __isl_take isl_union_map *kill)
1638 return isl_union_access_info_set(info, isl_access_kill, kill);
1641 /* Return the access relation of type "type" of "info".
1643 static __isl_give isl_union_map *isl_union_access_info_get(
1644 __isl_keep isl_union_access_info *info, enum isl_access_type type)
1646 if (!info)
1647 return NULL;
1648 return isl_union_map_copy(info->access[type]);
1651 /* Return the definite source accesses of "info".
1653 __isl_give isl_union_map *isl_union_access_info_get_must_source(
1654 __isl_keep isl_union_access_info *info)
1656 return isl_union_access_info_get(info, isl_access_must_source);
1659 /* Return the possible source accesses of "info".
1661 __isl_give isl_union_map *isl_union_access_info_get_may_source(
1662 __isl_keep isl_union_access_info *info)
1664 return isl_union_access_info_get(info, isl_access_may_source);
1667 /* Return the kills of "info".
1669 __isl_give isl_union_map *isl_union_access_info_get_kill(
1670 __isl_keep isl_union_access_info *info)
1672 return isl_union_access_info_get(info, isl_access_kill);
1675 /* Does "info" specify any kills?
1677 static isl_bool isl_union_access_has_kill(
1678 __isl_keep isl_union_access_info *info)
1680 isl_bool empty;
1682 if (!info)
1683 return isl_bool_error;
1684 empty = isl_union_map_is_empty(info->access[isl_access_kill]);
1685 return isl_bool_not(empty);
1688 /* Replace the schedule of "access" by "schedule".
1689 * Also free the schedule_map in case it was set last.
1691 __isl_give isl_union_access_info *isl_union_access_info_set_schedule(
1692 __isl_take isl_union_access_info *access,
1693 __isl_take isl_schedule *schedule)
1695 if (!access || !schedule)
1696 goto error;
1698 access->schedule_map = isl_union_map_free(access->schedule_map);
1699 isl_schedule_free(access->schedule);
1700 access->schedule = schedule;
1702 return access;
1703 error:
1704 isl_union_access_info_free(access);
1705 isl_schedule_free(schedule);
1706 return NULL;
1709 /* Replace the schedule map of "access" by "schedule_map".
1710 * Also free the schedule in case it was set last.
1712 __isl_give isl_union_access_info *isl_union_access_info_set_schedule_map(
1713 __isl_take isl_union_access_info *access,
1714 __isl_take isl_union_map *schedule_map)
1716 if (!access || !schedule_map)
1717 goto error;
1719 isl_union_map_free(access->schedule_map);
1720 access->schedule = isl_schedule_free(access->schedule);
1721 access->schedule_map = schedule_map;
1723 return access;
1724 error:
1725 isl_union_access_info_free(access);
1726 isl_union_map_free(schedule_map);
1727 return NULL;
1730 __isl_give isl_union_access_info *isl_union_access_info_copy(
1731 __isl_keep isl_union_access_info *access)
1733 isl_union_access_info *copy;
1734 enum isl_access_type i;
1736 if (!access)
1737 return NULL;
1738 copy = isl_union_access_info_from_sink(
1739 isl_union_map_copy(access->access[isl_access_sink]));
1740 for (i = isl_access_sink + 1; i < isl_access_end; ++i)
1741 copy = isl_union_access_info_set(copy, i,
1742 isl_union_map_copy(access->access[i]));
1743 if (access->schedule)
1744 copy = isl_union_access_info_set_schedule(copy,
1745 isl_schedule_copy(access->schedule));
1746 else
1747 copy = isl_union_access_info_set_schedule_map(copy,
1748 isl_union_map_copy(access->schedule_map));
1750 return copy;
1753 /* Print a key-value pair of a YAML mapping to "p",
1754 * with key "name" and value "umap".
1756 static __isl_give isl_printer *print_union_map_field(__isl_take isl_printer *p,
1757 const char *name, __isl_keep isl_union_map *umap)
1759 p = isl_printer_print_str(p, name);
1760 p = isl_printer_yaml_next(p);
1761 p = isl_printer_print_str(p, "\"");
1762 p = isl_printer_print_union_map(p, umap);
1763 p = isl_printer_print_str(p, "\"");
1764 p = isl_printer_yaml_next(p);
1766 return p;
1769 /* An enumeration of the various keys that may appear in a YAML mapping
1770 * of an isl_union_access_info object.
1771 * The keys for the access relation types are assumed to have the same values
1772 * as the access relation types in isl_access_type.
1774 enum isl_ai_key {
1775 isl_ai_key_error = -1,
1776 isl_ai_key_sink = isl_access_sink,
1777 isl_ai_key_must_source = isl_access_must_source,
1778 isl_ai_key_may_source = isl_access_may_source,
1779 isl_ai_key_kill = isl_access_kill,
1780 isl_ai_key_schedule_map,
1781 isl_ai_key_schedule,
1782 isl_ai_key_end
1785 /* Textual representations of the YAML keys for an isl_union_access_info
1786 * object.
1788 static char *key_str[] = {
1789 [isl_ai_key_sink] = "sink",
1790 [isl_ai_key_must_source] = "must_source",
1791 [isl_ai_key_may_source] = "may_source",
1792 [isl_ai_key_kill] = "kill",
1793 [isl_ai_key_schedule_map] = "schedule_map",
1794 [isl_ai_key_schedule] = "schedule",
1797 /* Print a key-value pair corresponding to the access relation of type "type"
1798 * of a YAML mapping of "info" to "p".
1800 * The sink access relation is always printed, but any other access relation
1801 * is only printed if it is non-empty.
1803 static __isl_give isl_printer *print_access_field(__isl_take isl_printer *p,
1804 __isl_keep isl_union_access_info *info, enum isl_access_type type)
1806 if (type != isl_access_sink) {
1807 isl_bool empty;
1809 empty = isl_union_map_is_empty(info->access[type]);
1810 if (empty < 0)
1811 return isl_printer_free(p);
1812 if (empty)
1813 return p;
1815 return print_union_map_field(p, key_str[type], info->access[type]);
1818 /* Print the information contained in "access" to "p".
1819 * The information is printed as a YAML document.
1821 __isl_give isl_printer *isl_printer_print_union_access_info(
1822 __isl_take isl_printer *p, __isl_keep isl_union_access_info *access)
1824 enum isl_access_type i;
1826 if (!access)
1827 return isl_printer_free(p);
1829 p = isl_printer_yaml_start_mapping(p);
1830 for (i = isl_access_sink; i < isl_access_end; ++i)
1831 p = print_access_field(p, access, i);
1832 if (access->schedule) {
1833 p = isl_printer_print_str(p, key_str[isl_ai_key_schedule]);
1834 p = isl_printer_yaml_next(p);
1835 p = isl_printer_print_schedule(p, access->schedule);
1836 p = isl_printer_yaml_next(p);
1837 } else {
1838 p = print_union_map_field(p, key_str[isl_ai_key_schedule_map],
1839 access->schedule_map);
1841 p = isl_printer_yaml_end_mapping(p);
1843 return p;
1846 /* Return a string representation of the information in "access".
1847 * The information is printed in flow format.
1849 __isl_give char *isl_union_access_info_to_str(
1850 __isl_keep isl_union_access_info *access)
1852 isl_printer *p;
1853 char *s;
1855 if (!access)
1856 return NULL;
1858 p = isl_printer_to_str(isl_union_access_info_get_ctx(access));
1859 p = isl_printer_set_yaml_style(p, ISL_YAML_STYLE_FLOW);
1860 p = isl_printer_print_union_access_info(p, access);
1861 s = isl_printer_get_str(p);
1862 isl_printer_free(p);
1864 return s;
1867 #undef KEY
1868 #define KEY enum isl_ai_key
1869 #undef KEY_ERROR
1870 #define KEY_ERROR isl_ai_key_error
1871 #undef KEY_END
1872 #define KEY_END isl_ai_key_end
1873 #include "extract_key.c"
1875 #undef BASE
1876 #define BASE union_map
1877 #include "read_in_string_templ.c"
1879 /* Read an isl_union_access_info object from "s".
1881 * Start off with an empty (invalid) isl_union_access_info object and
1882 * then fill up the fields based on the input.
1883 * The input needs to contain at least a description of the sink
1884 * access relation as well as some form of schedule.
1885 * The other access relations are set to empty relations
1886 * by isl_union_access_info_init if they are not specified in the input.
1888 __isl_give isl_union_access_info *isl_stream_read_union_access_info(
1889 isl_stream *s)
1891 isl_ctx *ctx;
1892 isl_union_access_info *info;
1893 int more;
1894 int sink_set = 0;
1895 int schedule_set = 0;
1897 if (isl_stream_yaml_read_start_mapping(s))
1898 return NULL;
1900 ctx = isl_stream_get_ctx(s);
1901 info = isl_union_access_info_alloc(ctx);
1902 while ((more = isl_stream_yaml_next(s)) > 0) {
1903 enum isl_ai_key key;
1904 isl_union_map *access, *schedule_map;
1905 isl_schedule *schedule;
1907 key = get_key(s);
1908 if (isl_stream_yaml_next(s) < 0)
1909 return isl_union_access_info_free(info);
1910 switch (key) {
1911 case isl_ai_key_end:
1912 case isl_ai_key_error:
1913 return isl_union_access_info_free(info);
1914 case isl_ai_key_sink:
1915 sink_set = 1;
1916 case isl_ai_key_must_source:
1917 case isl_ai_key_may_source:
1918 case isl_ai_key_kill:
1919 access = read_union_map(s);
1920 info = isl_union_access_info_set(info, key, access);
1921 if (!info)
1922 return NULL;
1923 break;
1924 case isl_ai_key_schedule_map:
1925 schedule_set = 1;
1926 schedule_map = read_union_map(s);
1927 info = isl_union_access_info_set_schedule_map(info,
1928 schedule_map);
1929 if (!info)
1930 return NULL;
1931 break;
1932 case isl_ai_key_schedule:
1933 schedule_set = 1;
1934 schedule = isl_stream_read_schedule(s);
1935 info = isl_union_access_info_set_schedule(info,
1936 schedule);
1937 if (!info)
1938 return NULL;
1939 break;
1942 if (more < 0)
1943 return isl_union_access_info_free(info);
1945 if (isl_stream_yaml_read_end_mapping(s) < 0) {
1946 isl_stream_error(s, NULL, "unexpected extra elements");
1947 return isl_union_access_info_free(info);
1950 if (!sink_set) {
1951 isl_stream_error(s, NULL, "no sink specified");
1952 return isl_union_access_info_free(info);
1955 if (!schedule_set) {
1956 isl_stream_error(s, NULL, "no schedule specified");
1957 return isl_union_access_info_free(info);
1960 return isl_union_access_info_init(info);
1963 /* Read an isl_union_access_info object from the file "input".
1965 __isl_give isl_union_access_info *isl_union_access_info_read_from_file(
1966 isl_ctx *ctx, FILE *input)
1968 isl_stream *s;
1969 isl_union_access_info *access;
1971 s = isl_stream_new_file(ctx, input);
1972 if (!s)
1973 return NULL;
1974 access = isl_stream_read_union_access_info(s);
1975 isl_stream_free(s);
1977 return access;
1980 /* Update the fields of "access" such that they all have the same parameters,
1981 * keeping in mind that the schedule_map field may be NULL and ignoring
1982 * the schedule field.
1984 static __isl_give isl_union_access_info *isl_union_access_info_align_params(
1985 __isl_take isl_union_access_info *access)
1987 isl_space *space;
1988 enum isl_access_type i;
1990 if (!access)
1991 return NULL;
1993 space = isl_union_map_get_space(access->access[isl_access_sink]);
1994 for (i = isl_access_sink + 1; i < isl_access_end; ++i)
1995 space = isl_space_align_params(space,
1996 isl_union_map_get_space(access->access[i]));
1997 if (access->schedule_map)
1998 space = isl_space_align_params(space,
1999 isl_union_map_get_space(access->schedule_map));
2000 for (i = isl_access_sink; i < isl_access_end; ++i)
2001 access->access[i] =
2002 isl_union_map_align_params(access->access[i],
2003 isl_space_copy(space));
2004 if (!access->schedule_map) {
2005 isl_space_free(space);
2006 } else {
2007 access->schedule_map =
2008 isl_union_map_align_params(access->schedule_map, space);
2009 if (!access->schedule_map)
2010 return isl_union_access_info_free(access);
2013 for (i = isl_access_sink; i < isl_access_end; ++i)
2014 if (!access->access[i])
2015 return isl_union_access_info_free(access);
2017 return access;
2020 /* Prepend the schedule dimensions to the iteration domains.
2022 * That is, if the schedule is of the form
2024 * D -> S
2026 * while the access relations are of the form
2028 * D -> A
2030 * then the updated access relations are of the form
2032 * [S -> D] -> A
2034 * The schedule map is also replaced by the map
2036 * [S -> D] -> D
2038 * that is used during the internal computation.
2039 * Neither the original schedule map nor this updated schedule map
2040 * are used after the call to this function.
2042 static __isl_give isl_union_access_info *
2043 isl_union_access_info_introduce_schedule(
2044 __isl_take isl_union_access_info *access)
2046 isl_union_map *sm;
2047 enum isl_access_type i;
2049 if (!access)
2050 return NULL;
2052 sm = isl_union_map_reverse(access->schedule_map);
2053 sm = isl_union_map_range_map(sm);
2054 for (i = isl_access_sink; i < isl_access_end; ++i)
2055 access->access[i] =
2056 isl_union_map_apply_range(isl_union_map_copy(sm),
2057 access->access[i]);
2058 access->schedule_map = sm;
2060 for (i = isl_access_sink; i < isl_access_end; ++i)
2061 if (!access->access[i])
2062 return isl_union_access_info_free(access);
2063 if (!access->schedule_map)
2064 return isl_union_access_info_free(access);
2066 return access;
2069 /* This structure represents the result of a dependence analysis computation.
2071 * "must_dep" represents the full definite dependences
2072 * "may_dep" represents the full non-definite dependences.
2073 * Both are of the form
2075 * [Source] -> [[Sink -> Data]]
2077 * (after the schedule dimensions have been projected out).
2078 * "must_no_source" represents the subset of the sink accesses for which
2079 * definitely no source was found.
2080 * "may_no_source" represents the subset of the sink accesses for which
2081 * possibly, but not definitely, no source was found.
2083 struct isl_union_flow {
2084 isl_union_map *must_dep;
2085 isl_union_map *may_dep;
2086 isl_union_map *must_no_source;
2087 isl_union_map *may_no_source;
2090 /* Return the isl_ctx to which "flow" belongs.
2092 isl_ctx *isl_union_flow_get_ctx(__isl_keep isl_union_flow *flow)
2094 return flow ? isl_union_map_get_ctx(flow->must_dep) : NULL;
2097 /* Free "flow" and return NULL.
2099 __isl_null isl_union_flow *isl_union_flow_free(__isl_take isl_union_flow *flow)
2101 if (!flow)
2102 return NULL;
2103 isl_union_map_free(flow->must_dep);
2104 isl_union_map_free(flow->may_dep);
2105 isl_union_map_free(flow->must_no_source);
2106 isl_union_map_free(flow->may_no_source);
2107 free(flow);
2108 return NULL;
2111 void isl_union_flow_dump(__isl_keep isl_union_flow *flow)
2113 if (!flow)
2114 return;
2116 fprintf(stderr, "must dependences: ");
2117 isl_union_map_dump(flow->must_dep);
2118 fprintf(stderr, "may dependences: ");
2119 isl_union_map_dump(flow->may_dep);
2120 fprintf(stderr, "must no source: ");
2121 isl_union_map_dump(flow->must_no_source);
2122 fprintf(stderr, "may no source: ");
2123 isl_union_map_dump(flow->may_no_source);
2126 /* Return the full definite dependences in "flow", with accessed elements.
2128 __isl_give isl_union_map *isl_union_flow_get_full_must_dependence(
2129 __isl_keep isl_union_flow *flow)
2131 if (!flow)
2132 return NULL;
2133 return isl_union_map_copy(flow->must_dep);
2136 /* Return the full possible dependences in "flow", including the definite
2137 * dependences, with accessed elements.
2139 __isl_give isl_union_map *isl_union_flow_get_full_may_dependence(
2140 __isl_keep isl_union_flow *flow)
2142 if (!flow)
2143 return NULL;
2144 return isl_union_map_union(isl_union_map_copy(flow->must_dep),
2145 isl_union_map_copy(flow->may_dep));
2148 /* Return the definite dependences in "flow", without the accessed elements.
2150 __isl_give isl_union_map *isl_union_flow_get_must_dependence(
2151 __isl_keep isl_union_flow *flow)
2153 isl_union_map *dep;
2155 if (!flow)
2156 return NULL;
2157 dep = isl_union_map_copy(flow->must_dep);
2158 return isl_union_map_range_factor_domain(dep);
2161 /* Return the possible dependences in "flow", including the definite
2162 * dependences, without the accessed elements.
2164 __isl_give isl_union_map *isl_union_flow_get_may_dependence(
2165 __isl_keep isl_union_flow *flow)
2167 isl_union_map *dep;
2169 if (!flow)
2170 return NULL;
2171 dep = isl_union_map_union(isl_union_map_copy(flow->must_dep),
2172 isl_union_map_copy(flow->may_dep));
2173 return isl_union_map_range_factor_domain(dep);
2176 /* Return the non-definite dependences in "flow".
2178 static __isl_give isl_union_map *isl_union_flow_get_non_must_dependence(
2179 __isl_keep isl_union_flow *flow)
2181 if (!flow)
2182 return NULL;
2183 return isl_union_map_copy(flow->may_dep);
2186 /* Return the subset of the sink accesses for which definitely
2187 * no source was found.
2189 __isl_give isl_union_map *isl_union_flow_get_must_no_source(
2190 __isl_keep isl_union_flow *flow)
2192 if (!flow)
2193 return NULL;
2194 return isl_union_map_copy(flow->must_no_source);
2197 /* Return the subset of the sink accesses for which possibly
2198 * no source was found, including those for which definitely
2199 * no source was found.
2201 __isl_give isl_union_map *isl_union_flow_get_may_no_source(
2202 __isl_keep isl_union_flow *flow)
2204 if (!flow)
2205 return NULL;
2206 return isl_union_map_union(isl_union_map_copy(flow->must_no_source),
2207 isl_union_map_copy(flow->may_no_source));
2210 /* Return the subset of the sink accesses for which possibly, but not
2211 * definitely, no source was found.
2213 static __isl_give isl_union_map *isl_union_flow_get_non_must_no_source(
2214 __isl_keep isl_union_flow *flow)
2216 if (!flow)
2217 return NULL;
2218 return isl_union_map_copy(flow->may_no_source);
2221 /* Create a new isl_union_flow object, initialized with empty
2222 * dependence relations and sink subsets.
2224 static __isl_give isl_union_flow *isl_union_flow_alloc(
2225 __isl_take isl_space *space)
2227 isl_ctx *ctx;
2228 isl_union_map *empty;
2229 isl_union_flow *flow;
2231 if (!space)
2232 return NULL;
2233 ctx = isl_space_get_ctx(space);
2234 flow = isl_alloc_type(ctx, isl_union_flow);
2235 if (!flow)
2236 goto error;
2238 empty = isl_union_map_empty(space);
2239 flow->must_dep = isl_union_map_copy(empty);
2240 flow->may_dep = isl_union_map_copy(empty);
2241 flow->must_no_source = isl_union_map_copy(empty);
2242 flow->may_no_source = empty;
2244 if (!flow->must_dep || !flow->may_dep ||
2245 !flow->must_no_source || !flow->may_no_source)
2246 return isl_union_flow_free(flow);
2248 return flow;
2249 error:
2250 isl_space_free(space);
2251 return NULL;
2254 /* Copy this isl_union_flow object.
2256 __isl_give isl_union_flow *isl_union_flow_copy(__isl_keep isl_union_flow *flow)
2258 isl_union_flow *copy;
2260 if (!flow)
2261 return NULL;
2263 copy = isl_union_flow_alloc(isl_union_map_get_space(flow->must_dep));
2265 if (!copy)
2266 return NULL;
2268 copy->must_dep = isl_union_map_union(copy->must_dep,
2269 isl_union_map_copy(flow->must_dep));
2270 copy->may_dep = isl_union_map_union(copy->may_dep,
2271 isl_union_map_copy(flow->may_dep));
2272 copy->must_no_source = isl_union_map_union(copy->must_no_source,
2273 isl_union_map_copy(flow->must_no_source));
2274 copy->may_no_source = isl_union_map_union(copy->may_no_source,
2275 isl_union_map_copy(flow->may_no_source));
2277 if (!copy->must_dep || !copy->may_dep ||
2278 !copy->must_no_source || !copy->may_no_source)
2279 return isl_union_flow_free(copy);
2281 return copy;
2284 /* Drop the schedule dimensions from the iteration domains in "flow".
2285 * In particular, the schedule dimensions have been prepended
2286 * to the iteration domains prior to the dependence analysis by
2287 * replacing the iteration domain D, by the wrapped map [S -> D].
2288 * Replace these wrapped maps by the original D.
2290 * In particular, the dependences computed by access_info_compute_flow_core
2291 * are of the form
2293 * [S -> D] -> [[S' -> D'] -> A]
2295 * The schedule dimensions are projected out by first currying the range,
2296 * resulting in
2298 * [S -> D] -> [S' -> [D' -> A]]
2300 * and then computing the factor range
2302 * D -> [D' -> A]
2304 static __isl_give isl_union_flow *isl_union_flow_drop_schedule(
2305 __isl_take isl_union_flow *flow)
2307 if (!flow)
2308 return NULL;
2310 flow->must_dep = isl_union_map_range_curry(flow->must_dep);
2311 flow->must_dep = isl_union_map_factor_range(flow->must_dep);
2312 flow->may_dep = isl_union_map_range_curry(flow->may_dep);
2313 flow->may_dep = isl_union_map_factor_range(flow->may_dep);
2314 flow->must_no_source =
2315 isl_union_map_domain_factor_range(flow->must_no_source);
2316 flow->may_no_source =
2317 isl_union_map_domain_factor_range(flow->may_no_source);
2319 if (!flow->must_dep || !flow->may_dep ||
2320 !flow->must_no_source || !flow->may_no_source)
2321 return isl_union_flow_free(flow);
2323 return flow;
2326 struct isl_compute_flow_data {
2327 isl_union_map *must_source;
2328 isl_union_map *may_source;
2329 isl_union_flow *flow;
2331 int count;
2332 int must;
2333 isl_space *dim;
2334 struct isl_sched_info *sink_info;
2335 struct isl_sched_info **source_info;
2336 isl_access_info *accesses;
2339 static isl_stat count_matching_array(__isl_take isl_map *map, void *user)
2341 int eq;
2342 isl_space *dim;
2343 struct isl_compute_flow_data *data;
2345 data = (struct isl_compute_flow_data *)user;
2347 dim = isl_space_range(isl_map_get_space(map));
2349 eq = isl_space_is_equal(dim, data->dim);
2351 isl_space_free(dim);
2352 isl_map_free(map);
2354 if (eq < 0)
2355 return isl_stat_error;
2356 if (eq)
2357 data->count++;
2359 return isl_stat_ok;
2362 static isl_stat collect_matching_array(__isl_take isl_map *map, void *user)
2364 int eq;
2365 isl_space *dim;
2366 struct isl_sched_info *info;
2367 struct isl_compute_flow_data *data;
2369 data = (struct isl_compute_flow_data *)user;
2371 dim = isl_space_range(isl_map_get_space(map));
2373 eq = isl_space_is_equal(dim, data->dim);
2375 isl_space_free(dim);
2377 if (eq < 0)
2378 goto error;
2379 if (!eq) {
2380 isl_map_free(map);
2381 return isl_stat_ok;
2384 info = sched_info_alloc(map);
2385 data->source_info[data->count] = info;
2387 data->accesses = isl_access_info_add_source(data->accesses,
2388 map, data->must, info);
2390 data->count++;
2392 return isl_stat_ok;
2393 error:
2394 isl_map_free(map);
2395 return isl_stat_error;
2398 /* Determine the shared nesting level and the "textual order" of
2399 * the given accesses.
2401 * We first determine the minimal schedule dimension for both accesses.
2403 * If among those dimensions, we can find one where both have a fixed
2404 * value and if moreover those values are different, then the previous
2405 * dimension is the last shared nesting level and the textual order
2406 * is determined based on the order of the fixed values.
2407 * If no such fixed values can be found, then we set the shared
2408 * nesting level to the minimal schedule dimension, with no textual ordering.
2410 static int before(void *first, void *second)
2412 struct isl_sched_info *info1 = first;
2413 struct isl_sched_info *info2 = second;
2414 int n1, n2;
2415 int i;
2417 n1 = isl_vec_size(info1->cst);
2418 n2 = isl_vec_size(info2->cst);
2420 if (n2 < n1)
2421 n1 = n2;
2423 for (i = 0; i < n1; ++i) {
2424 int r;
2425 int cmp;
2427 if (!info1->is_cst[i])
2428 continue;
2429 if (!info2->is_cst[i])
2430 continue;
2431 cmp = isl_vec_cmp_element(info1->cst, info2->cst, i);
2432 if (cmp == 0)
2433 continue;
2435 r = 2 * i + (cmp < 0);
2437 return r;
2440 return 2 * n1;
2443 /* Check if the given two accesses may be coscheduled.
2444 * If so, return isl_bool_true. Otherwise return isl_bool_false.
2446 * Two accesses may only be coscheduled if the fixed schedule
2447 * coordinates have the same values.
2449 static isl_bool coscheduled(void *first, void *second)
2451 struct isl_sched_info *info1 = first;
2452 struct isl_sched_info *info2 = second;
2453 int n1, n2;
2454 int i;
2456 n1 = isl_vec_size(info1->cst);
2457 n2 = isl_vec_size(info2->cst);
2459 if (n2 < n1)
2460 n1 = n2;
2462 for (i = 0; i < n1; ++i) {
2463 int cmp;
2465 if (!info1->is_cst[i])
2466 continue;
2467 if (!info2->is_cst[i])
2468 continue;
2469 cmp = isl_vec_cmp_element(info1->cst, info2->cst, i);
2470 if (cmp != 0)
2471 return isl_bool_false;
2474 return isl_bool_true;
2477 /* Given a sink access, look for all the source accesses that access
2478 * the same array and perform dataflow analysis on them using
2479 * isl_access_info_compute_flow_core.
2481 static isl_stat compute_flow(__isl_take isl_map *map, void *user)
2483 int i;
2484 isl_ctx *ctx;
2485 struct isl_compute_flow_data *data;
2486 isl_flow *flow;
2487 isl_union_flow *df;
2489 data = (struct isl_compute_flow_data *)user;
2490 df = data->flow;
2492 ctx = isl_map_get_ctx(map);
2494 data->accesses = NULL;
2495 data->sink_info = NULL;
2496 data->source_info = NULL;
2497 data->count = 0;
2498 data->dim = isl_space_range(isl_map_get_space(map));
2500 if (isl_union_map_foreach_map(data->must_source,
2501 &count_matching_array, data) < 0)
2502 goto error;
2503 if (isl_union_map_foreach_map(data->may_source,
2504 &count_matching_array, data) < 0)
2505 goto error;
2507 data->sink_info = sched_info_alloc(map);
2508 data->source_info = isl_calloc_array(ctx, struct isl_sched_info *,
2509 data->count);
2511 data->accesses = isl_access_info_alloc(isl_map_copy(map),
2512 data->sink_info, &before, data->count);
2513 if (!data->sink_info || (data->count && !data->source_info) ||
2514 !data->accesses)
2515 goto error;
2516 data->accesses->coscheduled = &coscheduled;
2517 data->count = 0;
2518 data->must = 1;
2519 if (isl_union_map_foreach_map(data->must_source,
2520 &collect_matching_array, data) < 0)
2521 goto error;
2522 data->must = 0;
2523 if (isl_union_map_foreach_map(data->may_source,
2524 &collect_matching_array, data) < 0)
2525 goto error;
2527 flow = access_info_compute_flow_core(data->accesses);
2528 data->accesses = NULL;
2530 if (!flow)
2531 goto error;
2533 df->must_no_source = isl_union_map_union(df->must_no_source,
2534 isl_union_map_from_map(isl_flow_get_no_source(flow, 1)));
2535 df->may_no_source = isl_union_map_union(df->may_no_source,
2536 isl_union_map_from_map(isl_flow_get_no_source(flow, 0)));
2538 for (i = 0; i < flow->n_source; ++i) {
2539 isl_union_map *dep;
2540 dep = isl_union_map_from_map(isl_map_copy(flow->dep[i].map));
2541 if (flow->dep[i].must)
2542 df->must_dep = isl_union_map_union(df->must_dep, dep);
2543 else
2544 df->may_dep = isl_union_map_union(df->may_dep, dep);
2547 isl_flow_free(flow);
2549 sched_info_free(data->sink_info);
2550 if (data->source_info) {
2551 for (i = 0; i < data->count; ++i)
2552 sched_info_free(data->source_info[i]);
2553 free(data->source_info);
2555 isl_space_free(data->dim);
2556 isl_map_free(map);
2558 return isl_stat_ok;
2559 error:
2560 isl_access_info_free(data->accesses);
2561 sched_info_free(data->sink_info);
2562 if (data->source_info) {
2563 for (i = 0; i < data->count; ++i)
2564 sched_info_free(data->source_info[i]);
2565 free(data->source_info);
2567 isl_space_free(data->dim);
2568 isl_map_free(map);
2570 return isl_stat_error;
2573 /* Add the kills of "info" to the must-sources.
2575 static __isl_give isl_union_access_info *
2576 isl_union_access_info_add_kill_to_must_source(
2577 __isl_take isl_union_access_info *info)
2579 isl_union_map *must, *kill;
2581 must = isl_union_access_info_get_must_source(info);
2582 kill = isl_union_access_info_get_kill(info);
2583 must = isl_union_map_union(must, kill);
2584 return isl_union_access_info_set_must_source(info, must);
2587 /* Drop dependences from "flow" that purely originate from kills.
2588 * That is, only keep those dependences that originate from
2589 * the original must-sources "must" and/or the original may-sources "may".
2590 * In particular, "must" contains the must-sources from before
2591 * the kills were added and "may" contains the may-source from before
2592 * the kills were removed.
2594 * The dependences are of the form
2596 * Source -> [Sink -> Data]
2598 * Only those dependences are kept where the Source -> Data part
2599 * is a subset of the original may-sources or must-sources.
2600 * Of those, only the must-dependences that intersect with the must-sources
2601 * remain must-dependences.
2602 * If there is some overlap between the may-sources and the must-sources,
2603 * then the may-dependences and must-dependences may also overlap.
2604 * This should be fine since the may-dependences are only kept
2605 * disjoint from the must-dependences for the isl_union_map_compute_flow
2606 * interface. This interface does not support kills, so it will
2607 * not end up calling this function.
2609 static __isl_give isl_union_flow *isl_union_flow_drop_kill_source(
2610 __isl_take isl_union_flow *flow, __isl_take isl_union_map *must,
2611 __isl_take isl_union_map *may)
2613 isl_union_map *move;
2615 if (!flow)
2616 goto error;
2617 move = isl_union_map_copy(flow->must_dep);
2618 move = isl_union_map_intersect_range_factor_range(move,
2619 isl_union_map_copy(may));
2620 may = isl_union_map_union(may, isl_union_map_copy(must));
2621 flow->may_dep = isl_union_map_intersect_range_factor_range(
2622 flow->may_dep, may);
2623 flow->must_dep = isl_union_map_intersect_range_factor_range(
2624 flow->must_dep, must);
2625 flow->may_dep = isl_union_map_union(flow->may_dep, move);
2626 if (!flow->must_dep || !flow->may_dep)
2627 return isl_union_flow_free(flow);
2629 return flow;
2630 error:
2631 isl_union_map_free(must);
2632 isl_union_map_free(may);
2633 return NULL;
2636 /* Remove the must accesses from the may accesses.
2638 * A must access always trumps a may access, so there is no need
2639 * for a must access to also be considered as a may access. Doing so
2640 * would only cost extra computations only to find out that
2641 * the duplicated may access does not make any difference.
2643 static __isl_give isl_union_access_info *isl_union_access_info_normalize(
2644 __isl_take isl_union_access_info *access)
2646 if (!access)
2647 return NULL;
2648 access->access[isl_access_may_source] =
2649 isl_union_map_subtract(access->access[isl_access_may_source],
2650 isl_union_map_copy(access->access[isl_access_must_source]));
2651 if (!access->access[isl_access_may_source])
2652 return isl_union_access_info_free(access);
2654 return access;
2657 /* Given a description of the "sink" accesses, the "source" accesses and
2658 * a schedule, compute for each instance of a sink access
2659 * and for each element accessed by that instance,
2660 * the possible or definite source accesses that last accessed the
2661 * element accessed by the sink access before this sink access
2662 * in the sense that there is no intermediate definite source access.
2664 * The must_no_source and may_no_source elements of the result
2665 * are subsets of access->sink. The elements must_dep and may_dep
2666 * map domain elements of access->{may,must)_source to
2667 * domain elements of access->sink.
2669 * This function is used when only the schedule map representation
2670 * is available.
2672 * We first prepend the schedule dimensions to the domain
2673 * of the accesses so that we can easily compare their relative order.
2674 * Then we consider each sink access individually in compute_flow.
2676 static __isl_give isl_union_flow *compute_flow_union_map(
2677 __isl_take isl_union_access_info *access)
2679 struct isl_compute_flow_data data;
2680 isl_union_map *sink;
2682 access = isl_union_access_info_align_params(access);
2683 access = isl_union_access_info_introduce_schedule(access);
2684 if (!access)
2685 return NULL;
2687 data.must_source = access->access[isl_access_must_source];
2688 data.may_source = access->access[isl_access_may_source];
2690 sink = access->access[isl_access_sink];
2691 data.flow = isl_union_flow_alloc(isl_union_map_get_space(sink));
2693 if (isl_union_map_foreach_map(sink, &compute_flow, &data) < 0)
2694 goto error;
2696 data.flow = isl_union_flow_drop_schedule(data.flow);
2698 isl_union_access_info_free(access);
2699 return data.flow;
2700 error:
2701 isl_union_access_info_free(access);
2702 isl_union_flow_free(data.flow);
2703 return NULL;
2706 /* A schedule access relation.
2708 * The access relation "access" is of the form [S -> D] -> A,
2709 * where S corresponds to the prefix schedule at "node".
2710 * "must" is only relevant for source accesses and indicates
2711 * whether the access is a must source or a may source.
2713 struct isl_scheduled_access {
2714 isl_map *access;
2715 int must;
2716 isl_schedule_node *node;
2719 /* Data structure for keeping track of individual scheduled sink and source
2720 * accesses when computing dependence analysis based on a schedule tree.
2722 * "n_sink" is the number of used entries in "sink"
2723 * "n_source" is the number of used entries in "source"
2725 * "set_sink", "must" and "node" are only used inside collect_sink_source,
2726 * to keep track of the current node and
2727 * of what extract_sink_source needs to do.
2729 struct isl_compute_flow_schedule_data {
2730 isl_union_access_info *access;
2732 int n_sink;
2733 int n_source;
2735 struct isl_scheduled_access *sink;
2736 struct isl_scheduled_access *source;
2738 int set_sink;
2739 int must;
2740 isl_schedule_node *node;
2743 /* Align the parameters of all sinks with all sources.
2745 * If there are no sinks or no sources, then no alignment is needed.
2747 static void isl_compute_flow_schedule_data_align_params(
2748 struct isl_compute_flow_schedule_data *data)
2750 int i;
2751 isl_space *space;
2753 if (data->n_sink == 0 || data->n_source == 0)
2754 return;
2756 space = isl_map_get_space(data->sink[0].access);
2758 for (i = 1; i < data->n_sink; ++i)
2759 space = isl_space_align_params(space,
2760 isl_map_get_space(data->sink[i].access));
2761 for (i = 0; i < data->n_source; ++i)
2762 space = isl_space_align_params(space,
2763 isl_map_get_space(data->source[i].access));
2765 for (i = 0; i < data->n_sink; ++i)
2766 data->sink[i].access =
2767 isl_map_align_params(data->sink[i].access,
2768 isl_space_copy(space));
2769 for (i = 0; i < data->n_source; ++i)
2770 data->source[i].access =
2771 isl_map_align_params(data->source[i].access,
2772 isl_space_copy(space));
2774 isl_space_free(space);
2777 /* Free all the memory referenced from "data".
2778 * Do not free "data" itself as it may be allocated on the stack.
2780 static void isl_compute_flow_schedule_data_clear(
2781 struct isl_compute_flow_schedule_data *data)
2783 int i;
2785 if (!data->sink)
2786 return;
2788 for (i = 0; i < data->n_sink; ++i) {
2789 isl_map_free(data->sink[i].access);
2790 isl_schedule_node_free(data->sink[i].node);
2793 for (i = 0; i < data->n_source; ++i) {
2794 isl_map_free(data->source[i].access);
2795 isl_schedule_node_free(data->source[i].node);
2798 free(data->sink);
2801 /* isl_schedule_foreach_schedule_node_top_down callback for counting
2802 * (an upper bound on) the number of sinks and sources.
2804 * Sinks and sources are only extracted at leaves of the tree,
2805 * so we skip the node if it is not a leaf.
2806 * Otherwise we increment data->n_sink and data->n_source with
2807 * the number of spaces in the sink and source access domains
2808 * that reach this node.
2810 static isl_bool count_sink_source(__isl_keep isl_schedule_node *node,
2811 void *user)
2813 struct isl_compute_flow_schedule_data *data = user;
2814 isl_union_set *domain;
2815 isl_union_map *umap;
2816 isl_bool r = isl_bool_false;
2818 if (isl_schedule_node_get_type(node) != isl_schedule_node_leaf)
2819 return isl_bool_true;
2821 domain = isl_schedule_node_get_universe_domain(node);
2823 umap = isl_union_map_copy(data->access->access[isl_access_sink]);
2824 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
2825 data->n_sink += isl_union_map_n_map(umap);
2826 isl_union_map_free(umap);
2827 if (!umap)
2828 r = isl_bool_error;
2830 umap = isl_union_map_copy(data->access->access[isl_access_must_source]);
2831 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
2832 data->n_source += isl_union_map_n_map(umap);
2833 isl_union_map_free(umap);
2834 if (!umap)
2835 r = isl_bool_error;
2837 umap = isl_union_map_copy(data->access->access[isl_access_may_source]);
2838 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
2839 data->n_source += isl_union_map_n_map(umap);
2840 isl_union_map_free(umap);
2841 if (!umap)
2842 r = isl_bool_error;
2844 isl_union_set_free(domain);
2846 return r;
2849 /* Add a single scheduled sink or source (depending on data->set_sink)
2850 * with scheduled access relation "map", must property data->must and
2851 * schedule node data->node to the list of sinks or sources.
2853 static isl_stat extract_sink_source(__isl_take isl_map *map, void *user)
2855 struct isl_compute_flow_schedule_data *data = user;
2856 struct isl_scheduled_access *access;
2858 if (data->set_sink)
2859 access = data->sink + data->n_sink++;
2860 else
2861 access = data->source + data->n_source++;
2863 access->access = map;
2864 access->must = data->must;
2865 access->node = isl_schedule_node_copy(data->node);
2867 return isl_stat_ok;
2870 /* isl_schedule_foreach_schedule_node_top_down callback for collecting
2871 * individual scheduled source and sink accesses (taking into account
2872 * the domain of the schedule).
2874 * We only collect accesses at the leaves of the schedule tree.
2875 * We prepend the schedule dimensions at the leaf to the iteration
2876 * domains of the source and sink accesses and then extract
2877 * the individual accesses (per space).
2879 * In particular, if the prefix schedule at the node is of the form
2881 * D -> S
2883 * while the access relations are of the form
2885 * D -> A
2887 * then the updated access relations are of the form
2889 * [S -> D] -> A
2891 * Note that S consists of a single space such that introducing S
2892 * in the access relations does not increase the number of spaces.
2894 static isl_bool collect_sink_source(__isl_keep isl_schedule_node *node,
2895 void *user)
2897 struct isl_compute_flow_schedule_data *data = user;
2898 isl_union_map *prefix;
2899 isl_union_map *umap;
2900 isl_bool r = isl_bool_false;
2902 if (isl_schedule_node_get_type(node) != isl_schedule_node_leaf)
2903 return isl_bool_true;
2905 data->node = node;
2907 prefix = isl_schedule_node_get_prefix_schedule_relation(node);
2908 prefix = isl_union_map_reverse(prefix);
2909 prefix = isl_union_map_range_map(prefix);
2911 data->set_sink = 1;
2912 umap = isl_union_map_copy(data->access->access[isl_access_sink]);
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 data->set_sink = 0;
2919 data->must = 1;
2920 umap = isl_union_map_copy(data->access->access[isl_access_must_source]);
2921 umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
2922 if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
2923 r = isl_bool_error;
2924 isl_union_map_free(umap);
2926 data->set_sink = 0;
2927 data->must = 0;
2928 umap = isl_union_map_copy(data->access->access[isl_access_may_source]);
2929 umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
2930 if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
2931 r = isl_bool_error;
2932 isl_union_map_free(umap);
2934 isl_union_map_free(prefix);
2936 return r;
2939 /* isl_access_info_compute_flow callback for determining whether
2940 * the shared nesting level and the ordering within that level
2941 * for two scheduled accesses for use in compute_single_flow.
2943 * The tokens passed to this function refer to the leaves
2944 * in the schedule tree where the accesses take place.
2946 * If n is the shared number of loops, then we need to return
2947 * "2 * n + 1" if "first" precedes "second" inside the innermost
2948 * shared loop and "2 * n" otherwise.
2950 * The innermost shared ancestor may be the leaves themselves
2951 * if the accesses take place in the same leaf. Otherwise,
2952 * it is either a set node or a sequence node. Only in the case
2953 * of a sequence node do we consider one access to precede the other.
2955 static int before_node(void *first, void *second)
2957 isl_schedule_node *node1 = first;
2958 isl_schedule_node *node2 = second;
2959 isl_schedule_node *shared;
2960 int depth;
2961 int before = 0;
2963 shared = isl_schedule_node_get_shared_ancestor(node1, node2);
2964 if (!shared)
2965 return -1;
2967 depth = isl_schedule_node_get_schedule_depth(shared);
2968 if (isl_schedule_node_get_type(shared) == isl_schedule_node_sequence) {
2969 int pos1, pos2;
2971 pos1 = isl_schedule_node_get_ancestor_child_position(node1,
2972 shared);
2973 pos2 = isl_schedule_node_get_ancestor_child_position(node2,
2974 shared);
2975 before = pos1 < pos2;
2978 isl_schedule_node_free(shared);
2980 return 2 * depth + before;
2983 /* Check if the given two accesses may be coscheduled.
2984 * If so, return isl_bool_true. Otherwise return isl_bool_false.
2986 * Two accesses may only be coscheduled if they appear in the same leaf.
2988 static isl_bool coscheduled_node(void *first, void *second)
2990 isl_schedule_node *node1 = first;
2991 isl_schedule_node *node2 = second;
2993 return node1 == node2;
2996 /* Add the scheduled sources from "data" that access
2997 * the same data space as "sink" to "access".
2999 static __isl_give isl_access_info *add_matching_sources(
3000 __isl_take isl_access_info *access, struct isl_scheduled_access *sink,
3001 struct isl_compute_flow_schedule_data *data)
3003 int i;
3004 isl_space *space;
3006 space = isl_space_range(isl_map_get_space(sink->access));
3007 for (i = 0; i < data->n_source; ++i) {
3008 struct isl_scheduled_access *source;
3009 isl_space *source_space;
3010 int eq;
3012 source = &data->source[i];
3013 source_space = isl_map_get_space(source->access);
3014 source_space = isl_space_range(source_space);
3015 eq = isl_space_is_equal(space, source_space);
3016 isl_space_free(source_space);
3018 if (!eq)
3019 continue;
3020 if (eq < 0)
3021 goto error;
3023 access = isl_access_info_add_source(access,
3024 isl_map_copy(source->access), source->must, source->node);
3027 isl_space_free(space);
3028 return access;
3029 error:
3030 isl_space_free(space);
3031 isl_access_info_free(access);
3032 return NULL;
3035 /* Given a scheduled sink access relation "sink", compute the corresponding
3036 * dependences on the sources in "data" and add the computed dependences
3037 * to "uf".
3039 * The dependences computed by access_info_compute_flow_core are of the form
3041 * [S -> I] -> [[S' -> I'] -> A]
3043 * The schedule dimensions are projected out by first currying the range,
3044 * resulting in
3046 * [S -> I] -> [S' -> [I' -> A]]
3048 * and then computing the factor range
3050 * I -> [I' -> A]
3052 static __isl_give isl_union_flow *compute_single_flow(
3053 __isl_take isl_union_flow *uf, struct isl_scheduled_access *sink,
3054 struct isl_compute_flow_schedule_data *data)
3056 int i;
3057 isl_access_info *access;
3058 isl_flow *flow;
3059 isl_map *map;
3061 if (!uf)
3062 return NULL;
3064 access = isl_access_info_alloc(isl_map_copy(sink->access), sink->node,
3065 &before_node, data->n_source);
3066 if (access)
3067 access->coscheduled = &coscheduled_node;
3068 access = add_matching_sources(access, sink, data);
3070 flow = access_info_compute_flow_core(access);
3071 if (!flow)
3072 return isl_union_flow_free(uf);
3074 map = isl_map_domain_factor_range(isl_flow_get_no_source(flow, 1));
3075 uf->must_no_source = isl_union_map_union(uf->must_no_source,
3076 isl_union_map_from_map(map));
3077 map = isl_map_domain_factor_range(isl_flow_get_no_source(flow, 0));
3078 uf->may_no_source = isl_union_map_union(uf->may_no_source,
3079 isl_union_map_from_map(map));
3081 for (i = 0; i < flow->n_source; ++i) {
3082 isl_union_map *dep;
3084 map = isl_map_range_curry(isl_map_copy(flow->dep[i].map));
3085 map = isl_map_factor_range(map);
3086 dep = isl_union_map_from_map(map);
3087 if (flow->dep[i].must)
3088 uf->must_dep = isl_union_map_union(uf->must_dep, dep);
3089 else
3090 uf->may_dep = isl_union_map_union(uf->may_dep, dep);
3093 isl_flow_free(flow);
3095 return uf;
3098 /* Given a description of the "sink" accesses, the "source" accesses and
3099 * a schedule, compute for each instance of a sink access
3100 * and for each element accessed by that instance,
3101 * the possible or definite source accesses that last accessed the
3102 * element accessed by the sink access before this sink access
3103 * in the sense that there is no intermediate definite source access.
3104 * Only consider dependences between statement instances that belong
3105 * to the domain of the schedule.
3107 * The must_no_source and may_no_source elements of the result
3108 * are subsets of access->sink. The elements must_dep and may_dep
3109 * map domain elements of access->{may,must)_source to
3110 * domain elements of access->sink.
3112 * This function is used when a schedule tree representation
3113 * is available.
3115 * We extract the individual scheduled source and sink access relations
3116 * (taking into account the domain of the schedule) and
3117 * then compute dependences for each scheduled sink individually.
3119 static __isl_give isl_union_flow *compute_flow_schedule(
3120 __isl_take isl_union_access_info *access)
3122 struct isl_compute_flow_schedule_data data = { access };
3123 int i, n;
3124 isl_ctx *ctx;
3125 isl_space *space;
3126 isl_union_flow *flow;
3128 ctx = isl_union_access_info_get_ctx(access);
3130 data.n_sink = 0;
3131 data.n_source = 0;
3132 if (isl_schedule_foreach_schedule_node_top_down(access->schedule,
3133 &count_sink_source, &data) < 0)
3134 goto error;
3136 n = data.n_sink + data.n_source;
3137 data.sink = isl_calloc_array(ctx, struct isl_scheduled_access, n);
3138 if (n && !data.sink)
3139 goto error;
3140 data.source = data.sink + data.n_sink;
3142 data.n_sink = 0;
3143 data.n_source = 0;
3144 if (isl_schedule_foreach_schedule_node_top_down(access->schedule,
3145 &collect_sink_source, &data) < 0)
3146 goto error;
3148 space = isl_union_map_get_space(access->access[isl_access_sink]);
3149 flow = isl_union_flow_alloc(space);
3151 isl_compute_flow_schedule_data_align_params(&data);
3153 for (i = 0; i < data.n_sink; ++i)
3154 flow = compute_single_flow(flow, &data.sink[i], &data);
3156 isl_compute_flow_schedule_data_clear(&data);
3158 isl_union_access_info_free(access);
3159 return flow;
3160 error:
3161 isl_union_access_info_free(access);
3162 isl_compute_flow_schedule_data_clear(&data);
3163 return NULL;
3166 /* Given a description of the "sink" accesses, the "source" accesses and
3167 * a schedule, compute for each instance of a sink access
3168 * and for each element accessed by that instance,
3169 * the possible or definite source accesses that last accessed the
3170 * element accessed by the sink access before this sink access
3171 * in the sense that there is no intermediate definite source access.
3173 * The must_no_source and may_no_source elements of the result
3174 * are subsets of access->sink. The elements must_dep and may_dep
3175 * map domain elements of access->{may,must)_source to
3176 * domain elements of access->sink.
3178 * If any kills have been specified, then they are treated as
3179 * must-sources internally. Any dependence that purely derives
3180 * from an original kill is removed from the output.
3182 * We check whether the schedule is available as a schedule tree
3183 * or a schedule map and call the corresponding function to perform
3184 * the analysis.
3186 __isl_give isl_union_flow *isl_union_access_info_compute_flow(
3187 __isl_take isl_union_access_info *access)
3189 isl_bool has_kill;
3190 isl_union_map *must = NULL, *may = NULL;
3191 isl_union_flow *flow;
3193 has_kill = isl_union_access_has_kill(access);
3194 if (has_kill < 0)
3195 goto error;
3196 if (has_kill) {
3197 must = isl_union_access_info_get_must_source(access);
3198 may = isl_union_access_info_get_may_source(access);
3200 access = isl_union_access_info_add_kill_to_must_source(access);
3201 access = isl_union_access_info_normalize(access);
3202 if (!access)
3203 goto error;
3204 if (access->schedule)
3205 flow = compute_flow_schedule(access);
3206 else
3207 flow = compute_flow_union_map(access);
3208 if (has_kill)
3209 flow = isl_union_flow_drop_kill_source(flow, must, may);
3210 return flow;
3211 error:
3212 isl_union_access_info_free(access);
3213 isl_union_map_free(must);
3214 isl_union_map_free(may);
3215 return NULL;
3218 /* Print the information contained in "flow" to "p".
3219 * The information is printed as a YAML document.
3221 __isl_give isl_printer *isl_printer_print_union_flow(
3222 __isl_take isl_printer *p, __isl_keep isl_union_flow *flow)
3224 isl_union_map *umap;
3226 if (!flow)
3227 return isl_printer_free(p);
3229 p = isl_printer_yaml_start_mapping(p);
3230 umap = isl_union_flow_get_full_must_dependence(flow);
3231 p = print_union_map_field(p, "must_dependence", umap);
3232 isl_union_map_free(umap);
3233 umap = isl_union_flow_get_full_may_dependence(flow);
3234 p = print_union_map_field(p, "may_dependence", umap);
3235 isl_union_map_free(umap);
3236 p = print_union_map_field(p, "must_no_source", flow->must_no_source);
3237 umap = isl_union_flow_get_may_no_source(flow);
3238 p = print_union_map_field(p, "may_no_source", umap);
3239 isl_union_map_free(umap);
3240 p = isl_printer_yaml_end_mapping(p);
3242 return p;
3245 /* Return a string representation of the information in "flow".
3246 * The information is printed in flow format.
3248 __isl_give char *isl_union_flow_to_str(__isl_keep isl_union_flow *flow)
3250 isl_printer *p;
3251 char *s;
3253 if (!flow)
3254 return NULL;
3256 p = isl_printer_to_str(isl_union_flow_get_ctx(flow));
3257 p = isl_printer_set_yaml_style(p, ISL_YAML_STYLE_FLOW);
3258 p = isl_printer_print_union_flow(p, flow);
3259 s = isl_printer_get_str(p);
3260 isl_printer_free(p);
3262 return s;
3265 /* Given a collection of "sink" and "source" accesses,
3266 * compute for each iteration of a sink access
3267 * and for each element accessed by that iteration,
3268 * the source access in the list that last accessed the
3269 * element accessed by the sink access before this sink access.
3270 * Each access is given as a map from the loop iterators
3271 * to the array indices.
3272 * The result is a relations between source and sink
3273 * iterations and a subset of the domain of the sink accesses,
3274 * corresponding to those iterations that access an element
3275 * not previously accessed.
3277 * We collect the inputs in an isl_union_access_info object,
3278 * call isl_union_access_info_compute_flow and extract
3279 * the outputs from the result.
3281 int isl_union_map_compute_flow(__isl_take isl_union_map *sink,
3282 __isl_take isl_union_map *must_source,
3283 __isl_take isl_union_map *may_source,
3284 __isl_take isl_union_map *schedule,
3285 __isl_give isl_union_map **must_dep, __isl_give isl_union_map **may_dep,
3286 __isl_give isl_union_map **must_no_source,
3287 __isl_give isl_union_map **may_no_source)
3289 isl_union_access_info *access;
3290 isl_union_flow *flow;
3292 access = isl_union_access_info_from_sink(sink);
3293 access = isl_union_access_info_set_must_source(access, must_source);
3294 access = isl_union_access_info_set_may_source(access, may_source);
3295 access = isl_union_access_info_set_schedule_map(access, schedule);
3296 flow = isl_union_access_info_compute_flow(access);
3298 if (must_dep)
3299 *must_dep = isl_union_flow_get_must_dependence(flow);
3300 if (may_dep)
3301 *may_dep = isl_union_flow_get_non_must_dependence(flow);
3302 if (must_no_source)
3303 *must_no_source = isl_union_flow_get_must_no_source(flow);
3304 if (may_no_source)
3305 *may_no_source = isl_union_flow_get_non_must_no_source(flow);
3307 isl_union_flow_free(flow);
3309 if ((must_dep && !*must_dep) || (may_dep && !*may_dep) ||
3310 (must_no_source && !*must_no_source) ||
3311 (may_no_source && !*may_no_source))
3312 goto error;
3314 return 0;
3315 error:
3316 if (must_dep)
3317 *must_dep = isl_union_map_free(*must_dep);
3318 if (may_dep)
3319 *may_dep = isl_union_map_free(*may_dep);
3320 if (must_no_source)
3321 *must_no_source = isl_union_map_free(*must_no_source);
3322 if (may_no_source)
3323 *may_no_source = isl_union_map_free(*may_no_source);
3324 return -1;