isl_poly_is_infty: use isl_bool_ok
[isl.git] / isl_flow.c
blobe996f52d4c03b71ed1f0ba1144e8ec85fa4c8ec1
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 isl_size n_in = isl_map_dim(acc->source[j].map, isl_dim_in);
735 int depth = 2 * n_in + 1;
737 if (n_in < 0)
738 return isl_stat_error;
739 if (isl_map_plain_is_empty(temp_rel[j]))
740 return isl_stat_ok;
742 for (k = j - 1; k >= 0; --k) {
743 int plevel, plevel2;
744 plevel = acc->level_before(acc->source[k].data, acc->sink.data);
745 if (plevel < 0)
746 return isl_stat_error;
747 if (!can_precede_at_level(plevel, sink_level))
748 continue;
750 plevel2 = acc->level_before(acc->source[j].data,
751 acc->source[k].data);
752 if (plevel2 < 0)
753 return isl_stat_error;
755 for (level = sink_level; level <= depth; ++level) {
756 struct isl_map *T;
757 struct isl_set *trest;
758 struct isl_map *copy;
760 if (!can_precede_at_level(plevel2, level))
761 continue;
763 copy = isl_map_copy(temp_rel[j]);
764 T = last_later_source(acc, copy, j, sink_level, k,
765 level, &trest);
766 if (isl_map_plain_is_empty(T)) {
767 isl_set_free(trest);
768 isl_map_free(T);
769 continue;
771 temp_rel[j] = isl_map_intersect_range(temp_rel[j], trest);
772 temp_rel[k] = isl_map_union_disjoint(temp_rel[k], T);
776 return isl_stat_ok;
779 /* Compute all iterations of may source j that precedes the sink at the given
780 * level for sink iterations in set_C.
782 static __isl_give isl_map *all_sources(__isl_keep isl_access_info *acc,
783 __isl_take isl_set *set_C, int j, int level)
785 isl_map *read_map;
786 isl_map *write_map;
787 isl_map *dep_map;
788 isl_map *after;
790 read_map = isl_map_copy(acc->sink.map);
791 read_map = isl_map_intersect_domain(read_map, set_C);
792 write_map = isl_map_copy(acc->source[acc->n_must + j].map);
793 write_map = isl_map_reverse(write_map);
794 dep_map = isl_map_apply_range(read_map, write_map);
795 after = after_at_level(isl_map_get_space(dep_map), level);
796 dep_map = isl_map_intersect(dep_map, after);
798 return isl_map_reverse(dep_map);
801 /* For a given mapping between iterations of must source k and iterations
802 * of the sink, compute all iterations of may source j preceding
803 * the sink at level before_level for any of the sink iterations,
804 * but following the corresponding iteration of must source k at level
805 * after_level.
807 static __isl_give isl_map *all_later_sources(__isl_keep isl_access_info *acc,
808 __isl_take isl_map *old_map,
809 int j, int before_level, int k, int after_level)
811 isl_space *dim;
812 isl_set *set_C;
813 isl_map *read_map;
814 isl_map *write_map;
815 isl_map *dep_map;
816 isl_map *after_write;
817 isl_map *before_read;
819 set_C = isl_map_range(isl_map_copy(old_map));
820 read_map = isl_map_copy(acc->sink.map);
821 read_map = isl_map_intersect_domain(read_map, set_C);
822 write_map = isl_map_copy(acc->source[acc->n_must + j].map);
824 write_map = isl_map_reverse(write_map);
825 dep_map = isl_map_apply_range(read_map, write_map);
826 dim = isl_space_join(isl_map_get_space(acc->source[acc->n_must + j].map),
827 isl_space_reverse(isl_map_get_space(acc->source[k].map)));
828 after_write = after_at_level(dim, after_level);
829 after_write = isl_map_apply_range(after_write, old_map);
830 after_write = isl_map_reverse(after_write);
831 dep_map = isl_map_intersect(dep_map, after_write);
832 before_read = after_at_level(isl_map_get_space(dep_map), before_level);
833 dep_map = isl_map_intersect(dep_map, before_read);
834 return isl_map_reverse(dep_map);
837 /* Given the must and may dependence relations for the must accesses
838 * for level sink_level, check if there are any accesses of may access j
839 * that occur in between and return their union.
840 * If some of these accesses are intermediate with respect to
841 * (previously thought to be) must dependences, then these
842 * must dependences are turned into may dependences.
844 static __isl_give isl_map *all_intermediate_sources(
845 __isl_keep isl_access_info *acc, __isl_take isl_map *map,
846 struct isl_map **must_rel, struct isl_map **may_rel,
847 int j, int sink_level)
849 int k, level;
850 isl_size n_in = isl_map_dim(acc->source[acc->n_must + j].map,
851 isl_dim_in);
852 int depth = 2 * n_in + 1;
854 if (n_in < 0)
855 return isl_map_free(map);
856 for (k = 0; k < acc->n_must; ++k) {
857 int plevel;
859 if (isl_map_plain_is_empty(may_rel[k]) &&
860 isl_map_plain_is_empty(must_rel[k]))
861 continue;
863 plevel = acc->level_before(acc->source[k].data,
864 acc->source[acc->n_must + j].data);
865 if (plevel < 0)
866 return isl_map_free(map);
868 for (level = sink_level; level <= depth; ++level) {
869 isl_map *T;
870 isl_map *copy;
871 isl_set *ran;
873 if (!can_precede_at_level(plevel, level))
874 continue;
876 copy = isl_map_copy(may_rel[k]);
877 T = all_later_sources(acc, copy, j, sink_level, k, level);
878 map = isl_map_union(map, T);
880 copy = isl_map_copy(must_rel[k]);
881 T = all_later_sources(acc, copy, j, sink_level, k, level);
882 ran = isl_map_range(isl_map_copy(T));
883 map = isl_map_union(map, T);
884 may_rel[k] = isl_map_union_disjoint(may_rel[k],
885 isl_map_intersect_range(isl_map_copy(must_rel[k]),
886 isl_set_copy(ran)));
887 T = isl_map_from_domain_and_range(
888 isl_set_universe(
889 isl_space_domain(isl_map_get_space(must_rel[k]))),
890 ran);
891 must_rel[k] = isl_map_subtract(must_rel[k], T);
895 return map;
898 /* Given a dependence relation "old_map" between a must-source and the sink,
899 * return a subset of the dependences, augmented with instances
900 * of the source at position "pos" in "acc" that are coscheduled
901 * with the must-source and that access the same element.
902 * That is, if the input lives in a space T -> K, then the output
903 * lives in the space [T -> S] -> K, with S the space of source "pos", and
904 * the domain factor of the domain product is a subset of the input.
905 * The sources are considered to be coscheduled if they have the same values
906 * for the initial "depth" coordinates.
908 * First construct a dependence relation S -> K and a mapping
909 * between coscheduled sources T -> S.
910 * The second is combined with the original dependence relation T -> K
911 * to form a relation in T -> [S -> K], which is subsequently
912 * uncurried to [T -> S] -> K.
913 * This result is then intersected with the dependence relation S -> K
914 * to form the output.
916 * In case a negative depth is given, NULL is returned to indicate an error.
918 static __isl_give isl_map *coscheduled_source(__isl_keep isl_access_info *acc,
919 __isl_keep isl_map *old_map, int pos, int depth)
921 isl_space *space;
922 isl_set *set_C;
923 isl_map *read_map;
924 isl_map *write_map;
925 isl_map *dep_map;
926 isl_map *equal;
927 isl_map *map;
929 if (depth < 0)
930 return NULL;
932 set_C = isl_map_range(isl_map_copy(old_map));
933 read_map = isl_map_copy(acc->sink.map);
934 read_map = isl_map_intersect_domain(read_map, set_C);
935 write_map = isl_map_copy(acc->source[pos].map);
936 dep_map = isl_map_domain_product(write_map, read_map);
937 dep_map = isl_set_unwrap(isl_map_domain(dep_map));
938 space = isl_space_join(isl_map_get_space(old_map),
939 isl_space_reverse(isl_map_get_space(dep_map)));
940 equal = isl_map_from_basic_map(isl_basic_map_equal(space, depth));
941 map = isl_map_range_product(equal, isl_map_copy(old_map));
942 map = isl_map_uncurry(map);
943 map = isl_map_intersect_domain_factor_range(map, dep_map);
945 return map;
948 /* After the dependences derived from a must-source have been computed
949 * at a certain level, check if any of the sources of the must-dependences
950 * may be coscheduled with other sources.
951 * If they are any such sources, then there is no way of determining
952 * which of the sources actually comes last and the must-dependences
953 * need to be turned into may-dependences, while dependences from
954 * the other sources need to be added to the may-dependences as well.
955 * "acc" describes the sources and a callback for checking whether
956 * two sources may be coscheduled. If acc->coscheduled is NULL then
957 * the sources are assumed not to be coscheduled.
958 * "must_rel" and "may_rel" describe the must and may-dependence relations
959 * computed at the current level for the must-sources. Some of the dependences
960 * may be moved from "must_rel" to "may_rel".
961 * "flow" contains all dependences computed so far (apart from those
962 * in "must_rel" and "may_rel") and may be updated with additional
963 * dependences derived from may-sources.
965 * In particular, consider all the must-sources with a non-empty
966 * dependence relation in "must_rel". They are considered in reverse
967 * order because that is the order in which they are considered in the caller.
968 * If any of the must-sources are coscheduled, then the last one
969 * is the one that will have a corresponding dependence relation.
970 * For each must-source i, consider both all the previous must-sources
971 * and all the may-sources. If any of those may be coscheduled with
972 * must-source i, then compute the coscheduled instances that access
973 * the same memory elements. The result is a relation [T -> S] -> K.
974 * The projection onto T -> K is a subset of the must-dependence relation
975 * that needs to be turned into may-dependences.
976 * The projection onto S -> K needs to be added to the may-dependences
977 * of source S.
978 * Since a given must-source instance may be coscheduled with several
979 * other source instances, the dependences that need to be turned
980 * into may-dependences are first collected and only actually removed
981 * from the must-dependences after all other sources have been considered.
983 static __isl_give isl_flow *handle_coscheduled(__isl_keep isl_access_info *acc,
984 __isl_keep isl_map **must_rel, __isl_keep isl_map **may_rel,
985 __isl_take isl_flow *flow)
987 int i, j;
989 if (!acc->coscheduled)
990 return flow;
991 for (i = acc->n_must - 1; i >= 0; --i) {
992 isl_map *move;
994 if (isl_map_plain_is_empty(must_rel[i]))
995 continue;
996 move = isl_map_empty(isl_map_get_space(must_rel[i]));
997 for (j = i - 1; j >= 0; --j) {
998 int depth;
999 isl_bool coscheduled;
1000 isl_map *map, *factor;
1002 coscheduled = acc->coscheduled(acc->source[i].data,
1003 acc->source[j].data);
1004 if (coscheduled < 0) {
1005 isl_map_free(move);
1006 return isl_flow_free(flow);
1008 if (!coscheduled)
1009 continue;
1010 depth = acc->level_before(acc->source[i].data,
1011 acc->source[j].data) / 2;
1012 map = coscheduled_source(acc, must_rel[i], j, depth);
1013 factor = isl_map_domain_factor_range(isl_map_copy(map));
1014 may_rel[j] = isl_map_union(may_rel[j], factor);
1015 map = isl_map_domain_factor_domain(map);
1016 move = isl_map_union(move, map);
1018 for (j = 0; j < acc->n_may; ++j) {
1019 int depth, pos;
1020 isl_bool coscheduled;
1021 isl_map *map, *factor;
1023 pos = acc->n_must + j;
1024 coscheduled = acc->coscheduled(acc->source[i].data,
1025 acc->source[pos].data);
1026 if (coscheduled < 0) {
1027 isl_map_free(move);
1028 return isl_flow_free(flow);
1030 if (!coscheduled)
1031 continue;
1032 depth = acc->level_before(acc->source[i].data,
1033 acc->source[pos].data) / 2;
1034 map = coscheduled_source(acc, must_rel[i], pos, depth);
1035 factor = isl_map_domain_factor_range(isl_map_copy(map));
1036 pos = 2 * acc->n_must + j;
1037 flow->dep[pos].map = isl_map_union(flow->dep[pos].map,
1038 factor);
1039 map = isl_map_domain_factor_domain(map);
1040 move = isl_map_union(move, map);
1042 must_rel[i] = isl_map_subtract(must_rel[i], isl_map_copy(move));
1043 may_rel[i] = isl_map_union(may_rel[i], move);
1046 return flow;
1049 /* Compute dependences for the case where all accesses are "may"
1050 * accesses, which boils down to computing memory based dependences.
1051 * The generic algorithm would also work in this case, but it would
1052 * be overkill to use it.
1054 static __isl_give isl_flow *compute_mem_based_dependences(
1055 __isl_keep isl_access_info *acc)
1057 int i;
1058 isl_set *mustdo;
1059 isl_set *maydo;
1060 isl_flow *res;
1062 res = isl_flow_alloc(acc);
1063 if (!res)
1064 return NULL;
1066 mustdo = isl_map_domain(isl_map_copy(acc->sink.map));
1067 maydo = isl_set_copy(mustdo);
1069 for (i = 0; i < acc->n_may; ++i) {
1070 int plevel;
1071 int is_before;
1072 isl_space *dim;
1073 isl_map *before;
1074 isl_map *dep;
1076 plevel = acc->level_before(acc->source[i].data, acc->sink.data);
1077 if (plevel < 0)
1078 goto error;
1080 is_before = plevel & 1;
1081 plevel >>= 1;
1083 dim = isl_map_get_space(res->dep[i].map);
1084 if (is_before)
1085 before = isl_map_lex_le_first(dim, plevel);
1086 else
1087 before = isl_map_lex_lt_first(dim, plevel);
1088 dep = isl_map_apply_range(isl_map_copy(acc->source[i].map),
1089 isl_map_reverse(isl_map_copy(acc->sink.map)));
1090 dep = isl_map_intersect(dep, before);
1091 mustdo = isl_set_subtract(mustdo,
1092 isl_map_range(isl_map_copy(dep)));
1093 res->dep[i].map = isl_map_union(res->dep[i].map, dep);
1096 res->may_no_source = isl_set_subtract(maydo, isl_set_copy(mustdo));
1097 res->must_no_source = mustdo;
1099 return res;
1100 error:
1101 isl_set_free(mustdo);
1102 isl_set_free(maydo);
1103 isl_flow_free(res);
1104 return NULL;
1107 /* Compute dependences for the case where there is at least one
1108 * "must" access.
1110 * The core algorithm considers all levels in which a source may precede
1111 * the sink, where a level may either be a statement level or a loop level.
1112 * The outermost statement level is 1, the first loop level is 2, etc...
1113 * The algorithm basically does the following:
1114 * for all levels l of the read access from innermost to outermost
1115 * for all sources w that may precede the sink access at that level
1116 * compute the last iteration of the source that precedes the sink access
1117 * at that level
1118 * add result to possible last accesses at level l of source w
1119 * for all sources w2 that we haven't considered yet at this level that may
1120 * also precede the sink access
1121 * for all levels l2 of w from l to innermost
1122 * for all possible last accesses dep of w at l
1123 * compute last iteration of w2 between the source and sink
1124 * of dep
1125 * add result to possible last accesses at level l of write w2
1126 * and replace possible last accesses dep by the remainder
1129 * The above algorithm is applied to the must access. During the course
1130 * of the algorithm, we keep track of sink iterations that still
1131 * need to be considered. These iterations are split into those that
1132 * haven't been matched to any source access (mustdo) and those that have only
1133 * been matched to may accesses (maydo).
1134 * At the end of each level, must-sources and may-sources that are coscheduled
1135 * with the sources of the must-dependences at that level are considered.
1136 * If any coscheduled instances are found, then corresponding may-dependences
1137 * are added and the original must-dependences are turned into may-dependences.
1138 * Afterwards, the may accesses that occur after must-dependence sources
1139 * are considered.
1140 * In particular, we consider may accesses that precede the remaining
1141 * sink iterations, moving elements from mustdo to maydo when appropriate,
1142 * and may accesses that occur between a must source and a sink of any
1143 * dependences found at the current level, turning must dependences into
1144 * may dependences when appropriate.
1147 static __isl_give isl_flow *compute_val_based_dependences(
1148 __isl_keep isl_access_info *acc)
1150 isl_ctx *ctx;
1151 isl_flow *res;
1152 isl_set *mustdo = NULL;
1153 isl_set *maydo = NULL;
1154 int level, j;
1155 isl_size n_in;
1156 int depth;
1157 isl_map **must_rel = NULL;
1158 isl_map **may_rel = NULL;
1160 if (!acc)
1161 return NULL;
1163 res = isl_flow_alloc(acc);
1164 if (!res)
1165 goto error;
1166 ctx = isl_map_get_ctx(acc->sink.map);
1168 n_in = isl_map_dim(acc->sink.map, isl_dim_in);
1169 if (n_in < 0)
1170 goto error;
1171 depth = 2 * n_in + 1;
1172 mustdo = isl_map_domain(isl_map_copy(acc->sink.map));
1173 maydo = isl_set_empty(isl_set_get_space(mustdo));
1174 if (!mustdo || !maydo)
1175 goto error;
1176 if (isl_set_plain_is_empty(mustdo))
1177 goto done;
1179 must_rel = isl_calloc_array(ctx, struct isl_map *, acc->n_must);
1180 may_rel = isl_calloc_array(ctx, struct isl_map *, acc->n_must);
1181 if (!must_rel || !may_rel)
1182 goto error;
1184 for (level = depth; level >= 1; --level) {
1185 for (j = acc->n_must-1; j >=0; --j) {
1186 isl_space *space;
1187 space = isl_map_get_space(res->dep[2 * j].map);
1188 must_rel[j] = isl_map_empty(space);
1189 may_rel[j] = isl_map_copy(must_rel[j]);
1192 for (j = acc->n_must - 1; j >= 0; --j) {
1193 struct isl_map *T;
1194 struct isl_set *rest;
1195 int plevel;
1197 plevel = acc->level_before(acc->source[j].data,
1198 acc->sink.data);
1199 if (plevel < 0)
1200 goto error;
1201 if (!can_precede_at_level(plevel, level))
1202 continue;
1204 T = last_source(acc, mustdo, j, level, &rest);
1205 must_rel[j] = isl_map_union_disjoint(must_rel[j], T);
1206 mustdo = rest;
1208 if (intermediate_sources(acc, must_rel, j, level) < 0)
1209 goto error;
1211 T = last_source(acc, maydo, j, level, &rest);
1212 may_rel[j] = isl_map_union_disjoint(may_rel[j], T);
1213 maydo = rest;
1215 if (intermediate_sources(acc, may_rel, j, level) < 0)
1216 goto error;
1218 if (isl_set_plain_is_empty(mustdo) &&
1219 isl_set_plain_is_empty(maydo))
1220 break;
1222 for (j = j - 1; j >= 0; --j) {
1223 int plevel;
1225 plevel = acc->level_before(acc->source[j].data,
1226 acc->sink.data);
1227 if (plevel < 0)
1228 goto error;
1229 if (!can_precede_at_level(plevel, level))
1230 continue;
1232 if (intermediate_sources(acc, must_rel, j, level) < 0)
1233 goto error;
1234 if (intermediate_sources(acc, may_rel, j, level) < 0)
1235 goto error;
1238 res = handle_coscheduled(acc, must_rel, may_rel, res);
1239 if (!res)
1240 goto error;
1242 for (j = 0; j < acc->n_may; ++j) {
1243 int plevel;
1244 isl_map *T;
1245 isl_set *ran;
1247 plevel = acc->level_before(acc->source[acc->n_must + j].data,
1248 acc->sink.data);
1249 if (plevel < 0)
1250 goto error;
1251 if (!can_precede_at_level(plevel, level))
1252 continue;
1254 T = all_sources(acc, isl_set_copy(maydo), j, level);
1255 res->dep[2 * acc->n_must + j].map =
1256 isl_map_union(res->dep[2 * acc->n_must + j].map, T);
1257 T = all_sources(acc, isl_set_copy(mustdo), j, level);
1258 ran = isl_map_range(isl_map_copy(T));
1259 res->dep[2 * acc->n_must + j].map =
1260 isl_map_union(res->dep[2 * acc->n_must + j].map, T);
1261 mustdo = isl_set_subtract(mustdo, isl_set_copy(ran));
1262 maydo = isl_set_union_disjoint(maydo, ran);
1264 T = res->dep[2 * acc->n_must + j].map;
1265 T = all_intermediate_sources(acc, T, must_rel, may_rel,
1266 j, level);
1267 res->dep[2 * acc->n_must + j].map = T;
1270 for (j = acc->n_must - 1; j >= 0; --j) {
1271 res->dep[2 * j].map =
1272 isl_map_union_disjoint(res->dep[2 * j].map,
1273 must_rel[j]);
1274 res->dep[2 * j + 1].map =
1275 isl_map_union_disjoint(res->dep[2 * j + 1].map,
1276 may_rel[j]);
1279 if (isl_set_plain_is_empty(mustdo) &&
1280 isl_set_plain_is_empty(maydo))
1281 break;
1284 free(must_rel);
1285 free(may_rel);
1286 done:
1287 res->must_no_source = mustdo;
1288 res->may_no_source = maydo;
1289 return res;
1290 error:
1291 if (must_rel)
1292 for (j = 0; j < acc->n_must; ++j)
1293 isl_map_free(must_rel[j]);
1294 if (may_rel)
1295 for (j = 0; j < acc->n_must; ++j)
1296 isl_map_free(may_rel[j]);
1297 isl_flow_free(res);
1298 isl_set_free(mustdo);
1299 isl_set_free(maydo);
1300 free(must_rel);
1301 free(may_rel);
1302 return NULL;
1305 /* Given a "sink" access, a list of n "source" accesses,
1306 * compute for each iteration of the sink access
1307 * and for each element accessed by that iteration,
1308 * the source access in the list that last accessed the
1309 * element accessed by the sink access before this sink access.
1310 * Each access is given as a map from the loop iterators
1311 * to the array indices.
1312 * The result is a list of n relations between source and sink
1313 * iterations and a subset of the domain of the sink access,
1314 * corresponding to those iterations that access an element
1315 * not previously accessed.
1317 * To deal with multi-valued sink access relations, the sink iteration
1318 * domain is first extended with dimensions that correspond to the data
1319 * space. However, these extra dimensions are not projected out again.
1320 * It is up to the caller to decide whether these dimensions should be kept.
1322 static __isl_give isl_flow *access_info_compute_flow_core(
1323 __isl_take isl_access_info *acc)
1325 struct isl_flow *res = NULL;
1327 if (!acc)
1328 return NULL;
1330 acc->sink.map = isl_map_range_map(acc->sink.map);
1331 if (!acc->sink.map)
1332 goto error;
1334 if (acc->n_must == 0)
1335 res = compute_mem_based_dependences(acc);
1336 else {
1337 acc = isl_access_info_sort_sources(acc);
1338 res = compute_val_based_dependences(acc);
1340 acc = isl_access_info_free(acc);
1341 if (!res)
1342 return NULL;
1343 if (!res->must_no_source || !res->may_no_source)
1344 goto error;
1345 return res;
1346 error:
1347 isl_access_info_free(acc);
1348 isl_flow_free(res);
1349 return NULL;
1352 /* Given a "sink" access, a list of n "source" accesses,
1353 * compute for each iteration of the sink access
1354 * and for each element accessed by that iteration,
1355 * the source access in the list that last accessed the
1356 * element accessed by the sink access before this sink access.
1357 * Each access is given as a map from the loop iterators
1358 * to the array indices.
1359 * The result is a list of n relations between source and sink
1360 * iterations and a subset of the domain of the sink access,
1361 * corresponding to those iterations that access an element
1362 * not previously accessed.
1364 * To deal with multi-valued sink access relations,
1365 * access_info_compute_flow_core extends the sink iteration domain
1366 * with dimensions that correspond to the data space. These extra dimensions
1367 * are projected out from the result of access_info_compute_flow_core.
1369 __isl_give isl_flow *isl_access_info_compute_flow(__isl_take isl_access_info *acc)
1371 int j;
1372 struct isl_flow *res;
1374 if (!acc)
1375 return NULL;
1377 acc->domain_map = isl_map_domain_map(isl_map_copy(acc->sink.map));
1378 res = access_info_compute_flow_core(acc);
1379 if (!res)
1380 return NULL;
1382 for (j = 0; j < res->n_source; ++j) {
1383 res->dep[j].map = isl_map_range_factor_domain(res->dep[j].map);
1384 if (!res->dep[j].map)
1385 goto error;
1388 return res;
1389 error:
1390 isl_flow_free(res);
1391 return NULL;
1395 /* Keep track of some information about a schedule for a given
1396 * access. In particular, keep track of which dimensions
1397 * have a constant value and of the actual constant values.
1399 struct isl_sched_info {
1400 int *is_cst;
1401 isl_vec *cst;
1404 static void sched_info_free(__isl_take struct isl_sched_info *info)
1406 if (!info)
1407 return;
1408 isl_vec_free(info->cst);
1409 free(info->is_cst);
1410 free(info);
1413 /* Extract information on the constant dimensions of the schedule
1414 * for a given access. The "map" is of the form
1416 * [S -> D] -> A
1418 * with S the schedule domain, D the iteration domain and A the data domain.
1420 static __isl_give struct isl_sched_info *sched_info_alloc(
1421 __isl_keep isl_map *map)
1423 isl_ctx *ctx;
1424 isl_space *space;
1425 struct isl_sched_info *info;
1426 int i;
1427 isl_size n;
1429 if (!map)
1430 return NULL;
1432 space = isl_space_unwrap(isl_space_domain(isl_map_get_space(map)));
1433 if (!space)
1434 return NULL;
1435 n = isl_space_dim(space, isl_dim_in);
1436 isl_space_free(space);
1437 if (n < 0)
1438 return NULL;
1440 ctx = isl_map_get_ctx(map);
1441 info = isl_alloc_type(ctx, struct isl_sched_info);
1442 if (!info)
1443 return NULL;
1444 info->is_cst = isl_alloc_array(ctx, int, n);
1445 info->cst = isl_vec_alloc(ctx, n);
1446 if (n && (!info->is_cst || !info->cst))
1447 goto error;
1449 for (i = 0; i < n; ++i) {
1450 isl_val *v;
1452 v = isl_map_plain_get_val_if_fixed(map, isl_dim_in, i);
1453 if (!v)
1454 goto error;
1455 info->is_cst[i] = !isl_val_is_nan(v);
1456 if (info->is_cst[i])
1457 info->cst = isl_vec_set_element_val(info->cst, i, v);
1458 else
1459 isl_val_free(v);
1462 return info;
1463 error:
1464 sched_info_free(info);
1465 return NULL;
1468 /* The different types of access relations that isl_union_access_info
1469 * keeps track of.
1471 * "isl_access_sink" represents the sink accesses.
1472 * "isl_access_must_source" represents the definite source accesses.
1473 * "isl_access_may_source" represents the possible source accesses.
1474 * "isl_access_kill" represents the kills.
1476 * isl_access_sink is sometimes treated differently and
1477 * should therefore appear first.
1479 enum isl_access_type {
1480 isl_access_sink,
1481 isl_access_must_source,
1482 isl_access_may_source,
1483 isl_access_kill,
1484 isl_access_end
1487 /* This structure represents the input for a dependence analysis computation.
1489 * "access" contains the access relations.
1491 * "schedule" or "schedule_map" represents the execution order.
1492 * Exactly one of these fields should be NULL. The other field
1493 * determines the execution order.
1495 * The domains of these four maps refer to the same iteration spaces(s).
1496 * The ranges of the first three maps also refer to the same data space(s).
1498 * After a call to isl_union_access_info_introduce_schedule,
1499 * the "schedule_map" field no longer contains useful information.
1501 struct isl_union_access_info {
1502 isl_union_map *access[isl_access_end];
1504 isl_schedule *schedule;
1505 isl_union_map *schedule_map;
1508 /* Free "access" and return NULL.
1510 __isl_null isl_union_access_info *isl_union_access_info_free(
1511 __isl_take isl_union_access_info *access)
1513 enum isl_access_type i;
1515 if (!access)
1516 return NULL;
1518 for (i = isl_access_sink; i < isl_access_end; ++i)
1519 isl_union_map_free(access->access[i]);
1520 isl_schedule_free(access->schedule);
1521 isl_union_map_free(access->schedule_map);
1522 free(access);
1524 return NULL;
1527 /* Return the isl_ctx to which "access" belongs.
1529 isl_ctx *isl_union_access_info_get_ctx(__isl_keep isl_union_access_info *access)
1531 if (!access)
1532 return NULL;
1533 return isl_union_map_get_ctx(access->access[isl_access_sink]);
1536 /* Construct an empty (invalid) isl_union_access_info object.
1537 * The caller is responsible for setting the sink access relation and
1538 * initializing all the other fields, e.g., by calling
1539 * isl_union_access_info_init.
1541 static __isl_give isl_union_access_info *isl_union_access_info_alloc(
1542 isl_ctx *ctx)
1544 return isl_calloc_type(ctx, isl_union_access_info);
1547 /* Initialize all the fields of "info", except the sink access relation,
1548 * which is assumed to have been set by the caller.
1550 * By default, we use the schedule field of the isl_union_access_info,
1551 * but this may be overridden by a call
1552 * to isl_union_access_info_set_schedule_map.
1554 static __isl_give isl_union_access_info *isl_union_access_info_init(
1555 __isl_take isl_union_access_info *info)
1557 isl_space *space;
1558 isl_union_map *empty;
1559 enum isl_access_type i;
1561 if (!info)
1562 return NULL;
1563 if (!info->access[isl_access_sink])
1564 return isl_union_access_info_free(info);
1566 space = isl_union_map_get_space(info->access[isl_access_sink]);
1567 empty = isl_union_map_empty(isl_space_copy(space));
1568 for (i = isl_access_sink + 1; i < isl_access_end; ++i)
1569 if (!info->access[i])
1570 info->access[i] = isl_union_map_copy(empty);
1571 isl_union_map_free(empty);
1572 if (!info->schedule && !info->schedule_map)
1573 info->schedule = isl_schedule_empty(isl_space_copy(space));
1574 isl_space_free(space);
1576 for (i = isl_access_sink + 1; i < isl_access_end; ++i)
1577 if (!info->access[i])
1578 return isl_union_access_info_free(info);
1579 if (!info->schedule && !info->schedule_map)
1580 return isl_union_access_info_free(info);
1582 return info;
1585 /* Create a new isl_union_access_info with the given sink accesses and
1586 * and no other accesses or schedule information.
1588 __isl_give isl_union_access_info *isl_union_access_info_from_sink(
1589 __isl_take isl_union_map *sink)
1591 isl_ctx *ctx;
1592 isl_union_access_info *access;
1594 if (!sink)
1595 return NULL;
1596 ctx = isl_union_map_get_ctx(sink);
1597 access = isl_union_access_info_alloc(ctx);
1598 if (!access)
1599 goto error;
1600 access->access[isl_access_sink] = sink;
1601 return isl_union_access_info_init(access);
1602 error:
1603 isl_union_map_free(sink);
1604 return NULL;
1607 /* Replace the access relation of type "type" of "info" by "access".
1609 static __isl_give isl_union_access_info *isl_union_access_info_set(
1610 __isl_take isl_union_access_info *info,
1611 enum isl_access_type type, __isl_take isl_union_map *access)
1613 if (!info || !access)
1614 goto error;
1616 isl_union_map_free(info->access[type]);
1617 info->access[type] = access;
1619 return info;
1620 error:
1621 isl_union_access_info_free(info);
1622 isl_union_map_free(access);
1623 return NULL;
1626 /* Replace the definite source accesses of "access" by "must_source".
1628 __isl_give isl_union_access_info *isl_union_access_info_set_must_source(
1629 __isl_take isl_union_access_info *access,
1630 __isl_take isl_union_map *must_source)
1632 return isl_union_access_info_set(access, isl_access_must_source,
1633 must_source);
1636 /* Replace the possible source accesses of "access" by "may_source".
1638 __isl_give isl_union_access_info *isl_union_access_info_set_may_source(
1639 __isl_take isl_union_access_info *access,
1640 __isl_take isl_union_map *may_source)
1642 return isl_union_access_info_set(access, isl_access_may_source,
1643 may_source);
1646 /* Replace the kills of "info" by "kill".
1648 __isl_give isl_union_access_info *isl_union_access_info_set_kill(
1649 __isl_take isl_union_access_info *info, __isl_take isl_union_map *kill)
1651 return isl_union_access_info_set(info, isl_access_kill, kill);
1654 /* Return the access relation of type "type" of "info".
1656 static __isl_give isl_union_map *isl_union_access_info_get(
1657 __isl_keep isl_union_access_info *info, enum isl_access_type type)
1659 if (!info)
1660 return NULL;
1661 return isl_union_map_copy(info->access[type]);
1664 /* Return the definite source accesses of "info".
1666 __isl_give isl_union_map *isl_union_access_info_get_must_source(
1667 __isl_keep isl_union_access_info *info)
1669 return isl_union_access_info_get(info, isl_access_must_source);
1672 /* Return the possible source accesses of "info".
1674 __isl_give isl_union_map *isl_union_access_info_get_may_source(
1675 __isl_keep isl_union_access_info *info)
1677 return isl_union_access_info_get(info, isl_access_may_source);
1680 /* Return the kills of "info".
1682 __isl_give isl_union_map *isl_union_access_info_get_kill(
1683 __isl_keep isl_union_access_info *info)
1685 return isl_union_access_info_get(info, isl_access_kill);
1688 /* Does "info" specify any kills?
1690 static isl_bool isl_union_access_has_kill(
1691 __isl_keep isl_union_access_info *info)
1693 isl_bool empty;
1695 if (!info)
1696 return isl_bool_error;
1697 empty = isl_union_map_is_empty(info->access[isl_access_kill]);
1698 return isl_bool_not(empty);
1701 /* Replace the schedule of "access" by "schedule".
1702 * Also free the schedule_map in case it was set last.
1704 __isl_give isl_union_access_info *isl_union_access_info_set_schedule(
1705 __isl_take isl_union_access_info *access,
1706 __isl_take isl_schedule *schedule)
1708 if (!access || !schedule)
1709 goto error;
1711 access->schedule_map = isl_union_map_free(access->schedule_map);
1712 isl_schedule_free(access->schedule);
1713 access->schedule = schedule;
1715 return access;
1716 error:
1717 isl_union_access_info_free(access);
1718 isl_schedule_free(schedule);
1719 return NULL;
1722 /* Replace the schedule map of "access" by "schedule_map".
1723 * Also free the schedule in case it was set last.
1725 __isl_give isl_union_access_info *isl_union_access_info_set_schedule_map(
1726 __isl_take isl_union_access_info *access,
1727 __isl_take isl_union_map *schedule_map)
1729 if (!access || !schedule_map)
1730 goto error;
1732 isl_union_map_free(access->schedule_map);
1733 access->schedule = isl_schedule_free(access->schedule);
1734 access->schedule_map = schedule_map;
1736 return access;
1737 error:
1738 isl_union_access_info_free(access);
1739 isl_union_map_free(schedule_map);
1740 return NULL;
1743 __isl_give isl_union_access_info *isl_union_access_info_copy(
1744 __isl_keep isl_union_access_info *access)
1746 isl_union_access_info *copy;
1747 enum isl_access_type i;
1749 if (!access)
1750 return NULL;
1751 copy = isl_union_access_info_from_sink(
1752 isl_union_map_copy(access->access[isl_access_sink]));
1753 for (i = isl_access_sink + 1; i < isl_access_end; ++i)
1754 copy = isl_union_access_info_set(copy, i,
1755 isl_union_map_copy(access->access[i]));
1756 if (access->schedule)
1757 copy = isl_union_access_info_set_schedule(copy,
1758 isl_schedule_copy(access->schedule));
1759 else
1760 copy = isl_union_access_info_set_schedule_map(copy,
1761 isl_union_map_copy(access->schedule_map));
1763 return copy;
1766 /* Print a key-value pair of a YAML mapping to "p",
1767 * with key "name" and value "umap".
1769 static __isl_give isl_printer *print_union_map_field(__isl_take isl_printer *p,
1770 const char *name, __isl_keep isl_union_map *umap)
1772 p = isl_printer_print_str(p, name);
1773 p = isl_printer_yaml_next(p);
1774 p = isl_printer_print_str(p, "\"");
1775 p = isl_printer_print_union_map(p, umap);
1776 p = isl_printer_print_str(p, "\"");
1777 p = isl_printer_yaml_next(p);
1779 return p;
1782 /* An enumeration of the various keys that may appear in a YAML mapping
1783 * of an isl_union_access_info object.
1784 * The keys for the access relation types are assumed to have the same values
1785 * as the access relation types in isl_access_type.
1787 enum isl_ai_key {
1788 isl_ai_key_error = -1,
1789 isl_ai_key_sink = isl_access_sink,
1790 isl_ai_key_must_source = isl_access_must_source,
1791 isl_ai_key_may_source = isl_access_may_source,
1792 isl_ai_key_kill = isl_access_kill,
1793 isl_ai_key_schedule_map,
1794 isl_ai_key_schedule,
1795 isl_ai_key_end
1798 /* Textual representations of the YAML keys for an isl_union_access_info
1799 * object.
1801 static char *key_str[] = {
1802 [isl_ai_key_sink] = "sink",
1803 [isl_ai_key_must_source] = "must_source",
1804 [isl_ai_key_may_source] = "may_source",
1805 [isl_ai_key_kill] = "kill",
1806 [isl_ai_key_schedule_map] = "schedule_map",
1807 [isl_ai_key_schedule] = "schedule",
1810 /* Print a key-value pair corresponding to the access relation of type "type"
1811 * of a YAML mapping of "info" to "p".
1813 * The sink access relation is always printed, but any other access relation
1814 * is only printed if it is non-empty.
1816 static __isl_give isl_printer *print_access_field(__isl_take isl_printer *p,
1817 __isl_keep isl_union_access_info *info, enum isl_access_type type)
1819 if (type != isl_access_sink) {
1820 isl_bool empty;
1822 empty = isl_union_map_is_empty(info->access[type]);
1823 if (empty < 0)
1824 return isl_printer_free(p);
1825 if (empty)
1826 return p;
1828 return print_union_map_field(p, key_str[type], info->access[type]);
1831 /* Print the information contained in "access" to "p".
1832 * The information is printed as a YAML document.
1834 __isl_give isl_printer *isl_printer_print_union_access_info(
1835 __isl_take isl_printer *p, __isl_keep isl_union_access_info *access)
1837 enum isl_access_type i;
1839 if (!access)
1840 return isl_printer_free(p);
1842 p = isl_printer_yaml_start_mapping(p);
1843 for (i = isl_access_sink; i < isl_access_end; ++i)
1844 p = print_access_field(p, access, i);
1845 if (access->schedule) {
1846 p = isl_printer_print_str(p, key_str[isl_ai_key_schedule]);
1847 p = isl_printer_yaml_next(p);
1848 p = isl_printer_print_schedule(p, access->schedule);
1849 p = isl_printer_yaml_next(p);
1850 } else {
1851 p = print_union_map_field(p, key_str[isl_ai_key_schedule_map],
1852 access->schedule_map);
1854 p = isl_printer_yaml_end_mapping(p);
1856 return p;
1859 /* Return a string representation of the information in "access".
1860 * The information is printed in flow format.
1862 __isl_give char *isl_union_access_info_to_str(
1863 __isl_keep isl_union_access_info *access)
1865 isl_printer *p;
1866 char *s;
1868 if (!access)
1869 return NULL;
1871 p = isl_printer_to_str(isl_union_access_info_get_ctx(access));
1872 p = isl_printer_set_yaml_style(p, ISL_YAML_STYLE_FLOW);
1873 p = isl_printer_print_union_access_info(p, access);
1874 s = isl_printer_get_str(p);
1875 isl_printer_free(p);
1877 return s;
1880 #undef KEY
1881 #define KEY enum isl_ai_key
1882 #undef KEY_ERROR
1883 #define KEY_ERROR isl_ai_key_error
1884 #undef KEY_END
1885 #define KEY_END isl_ai_key_end
1886 #include "extract_key.c"
1888 #undef BASE
1889 #define BASE union_map
1890 #include "read_in_string_templ.c"
1892 /* Read an isl_union_access_info object from "s".
1894 * Start off with an empty (invalid) isl_union_access_info object and
1895 * then fill up the fields based on the input.
1896 * The input needs to contain at least a description of the sink
1897 * access relation as well as some form of schedule.
1898 * The other access relations are set to empty relations
1899 * by isl_union_access_info_init if they are not specified in the input.
1901 __isl_give isl_union_access_info *isl_stream_read_union_access_info(
1902 isl_stream *s)
1904 isl_ctx *ctx;
1905 isl_union_access_info *info;
1906 int more;
1907 int sink_set = 0;
1908 int schedule_set = 0;
1910 if (isl_stream_yaml_read_start_mapping(s))
1911 return NULL;
1913 ctx = isl_stream_get_ctx(s);
1914 info = isl_union_access_info_alloc(ctx);
1915 while ((more = isl_stream_yaml_next(s)) > 0) {
1916 enum isl_ai_key key;
1917 isl_union_map *access, *schedule_map;
1918 isl_schedule *schedule;
1920 key = get_key(s);
1921 if (isl_stream_yaml_next(s) < 0)
1922 return isl_union_access_info_free(info);
1923 switch (key) {
1924 case isl_ai_key_end:
1925 case isl_ai_key_error:
1926 return isl_union_access_info_free(info);
1927 case isl_ai_key_sink:
1928 sink_set = 1;
1929 case isl_ai_key_must_source:
1930 case isl_ai_key_may_source:
1931 case isl_ai_key_kill:
1932 access = read_union_map(s);
1933 info = isl_union_access_info_set(info, key, access);
1934 if (!info)
1935 return NULL;
1936 break;
1937 case isl_ai_key_schedule_map:
1938 schedule_set = 1;
1939 schedule_map = read_union_map(s);
1940 info = isl_union_access_info_set_schedule_map(info,
1941 schedule_map);
1942 if (!info)
1943 return NULL;
1944 break;
1945 case isl_ai_key_schedule:
1946 schedule_set = 1;
1947 schedule = isl_stream_read_schedule(s);
1948 info = isl_union_access_info_set_schedule(info,
1949 schedule);
1950 if (!info)
1951 return NULL;
1952 break;
1955 if (more < 0)
1956 return isl_union_access_info_free(info);
1958 if (isl_stream_yaml_read_end_mapping(s) < 0) {
1959 isl_stream_error(s, NULL, "unexpected extra elements");
1960 return isl_union_access_info_free(info);
1963 if (!sink_set) {
1964 isl_stream_error(s, NULL, "no sink specified");
1965 return isl_union_access_info_free(info);
1968 if (!schedule_set) {
1969 isl_stream_error(s, NULL, "no schedule specified");
1970 return isl_union_access_info_free(info);
1973 return isl_union_access_info_init(info);
1976 /* Read an isl_union_access_info object from the file "input".
1978 __isl_give isl_union_access_info *isl_union_access_info_read_from_file(
1979 isl_ctx *ctx, FILE *input)
1981 isl_stream *s;
1982 isl_union_access_info *access;
1984 s = isl_stream_new_file(ctx, input);
1985 if (!s)
1986 return NULL;
1987 access = isl_stream_read_union_access_info(s);
1988 isl_stream_free(s);
1990 return access;
1993 /* Update the fields of "access" such that they all have the same parameters,
1994 * keeping in mind that the schedule_map field may be NULL and ignoring
1995 * the schedule field.
1997 static __isl_give isl_union_access_info *isl_union_access_info_align_params(
1998 __isl_take isl_union_access_info *access)
2000 isl_space *space;
2001 enum isl_access_type i;
2003 if (!access)
2004 return NULL;
2006 space = isl_union_map_get_space(access->access[isl_access_sink]);
2007 for (i = isl_access_sink + 1; i < isl_access_end; ++i)
2008 space = isl_space_align_params(space,
2009 isl_union_map_get_space(access->access[i]));
2010 if (access->schedule_map)
2011 space = isl_space_align_params(space,
2012 isl_union_map_get_space(access->schedule_map));
2013 for (i = isl_access_sink; i < isl_access_end; ++i)
2014 access->access[i] =
2015 isl_union_map_align_params(access->access[i],
2016 isl_space_copy(space));
2017 if (!access->schedule_map) {
2018 isl_space_free(space);
2019 } else {
2020 access->schedule_map =
2021 isl_union_map_align_params(access->schedule_map, space);
2022 if (!access->schedule_map)
2023 return isl_union_access_info_free(access);
2026 for (i = isl_access_sink; i < isl_access_end; ++i)
2027 if (!access->access[i])
2028 return isl_union_access_info_free(access);
2030 return access;
2033 /* Prepend the schedule dimensions to the iteration domains.
2035 * That is, if the schedule is of the form
2037 * D -> S
2039 * while the access relations are of the form
2041 * D -> A
2043 * then the updated access relations are of the form
2045 * [S -> D] -> A
2047 * The schedule map is also replaced by the map
2049 * [S -> D] -> D
2051 * that is used during the internal computation.
2052 * Neither the original schedule map nor this updated schedule map
2053 * are used after the call to this function.
2055 static __isl_give isl_union_access_info *
2056 isl_union_access_info_introduce_schedule(
2057 __isl_take isl_union_access_info *access)
2059 isl_union_map *sm;
2060 enum isl_access_type i;
2062 if (!access)
2063 return NULL;
2065 sm = isl_union_map_reverse(access->schedule_map);
2066 sm = isl_union_map_range_map(sm);
2067 for (i = isl_access_sink; i < isl_access_end; ++i)
2068 access->access[i] =
2069 isl_union_map_apply_range(isl_union_map_copy(sm),
2070 access->access[i]);
2071 access->schedule_map = sm;
2073 for (i = isl_access_sink; i < isl_access_end; ++i)
2074 if (!access->access[i])
2075 return isl_union_access_info_free(access);
2076 if (!access->schedule_map)
2077 return isl_union_access_info_free(access);
2079 return access;
2082 /* This structure represents the result of a dependence analysis computation.
2084 * "must_dep" represents the full definite dependences
2085 * "may_dep" represents the full non-definite dependences.
2086 * Both are of the form
2088 * [Source] -> [[Sink -> Data]]
2090 * (after the schedule dimensions have been projected out).
2091 * "must_no_source" represents the subset of the sink accesses for which
2092 * definitely no source was found.
2093 * "may_no_source" represents the subset of the sink accesses for which
2094 * possibly, but not definitely, no source was found.
2096 struct isl_union_flow {
2097 isl_union_map *must_dep;
2098 isl_union_map *may_dep;
2099 isl_union_map *must_no_source;
2100 isl_union_map *may_no_source;
2103 /* Return the isl_ctx to which "flow" belongs.
2105 isl_ctx *isl_union_flow_get_ctx(__isl_keep isl_union_flow *flow)
2107 return flow ? isl_union_map_get_ctx(flow->must_dep) : NULL;
2110 /* Free "flow" and return NULL.
2112 __isl_null isl_union_flow *isl_union_flow_free(__isl_take isl_union_flow *flow)
2114 if (!flow)
2115 return NULL;
2116 isl_union_map_free(flow->must_dep);
2117 isl_union_map_free(flow->may_dep);
2118 isl_union_map_free(flow->must_no_source);
2119 isl_union_map_free(flow->may_no_source);
2120 free(flow);
2121 return NULL;
2124 void isl_union_flow_dump(__isl_keep isl_union_flow *flow)
2126 if (!flow)
2127 return;
2129 fprintf(stderr, "must dependences: ");
2130 isl_union_map_dump(flow->must_dep);
2131 fprintf(stderr, "may dependences: ");
2132 isl_union_map_dump(flow->may_dep);
2133 fprintf(stderr, "must no source: ");
2134 isl_union_map_dump(flow->must_no_source);
2135 fprintf(stderr, "may no source: ");
2136 isl_union_map_dump(flow->may_no_source);
2139 /* Return the full definite dependences in "flow", with accessed elements.
2141 __isl_give isl_union_map *isl_union_flow_get_full_must_dependence(
2142 __isl_keep isl_union_flow *flow)
2144 if (!flow)
2145 return NULL;
2146 return isl_union_map_copy(flow->must_dep);
2149 /* Return the full possible dependences in "flow", including the definite
2150 * dependences, with accessed elements.
2152 __isl_give isl_union_map *isl_union_flow_get_full_may_dependence(
2153 __isl_keep isl_union_flow *flow)
2155 if (!flow)
2156 return NULL;
2157 return isl_union_map_union(isl_union_map_copy(flow->must_dep),
2158 isl_union_map_copy(flow->may_dep));
2161 /* Return the definite dependences in "flow", without the accessed elements.
2163 __isl_give isl_union_map *isl_union_flow_get_must_dependence(
2164 __isl_keep isl_union_flow *flow)
2166 isl_union_map *dep;
2168 if (!flow)
2169 return NULL;
2170 dep = isl_union_map_copy(flow->must_dep);
2171 return isl_union_map_range_factor_domain(dep);
2174 /* Return the possible dependences in "flow", including the definite
2175 * dependences, without the accessed elements.
2177 __isl_give isl_union_map *isl_union_flow_get_may_dependence(
2178 __isl_keep isl_union_flow *flow)
2180 isl_union_map *dep;
2182 if (!flow)
2183 return NULL;
2184 dep = isl_union_map_union(isl_union_map_copy(flow->must_dep),
2185 isl_union_map_copy(flow->may_dep));
2186 return isl_union_map_range_factor_domain(dep);
2189 /* Return the non-definite dependences in "flow".
2191 static __isl_give isl_union_map *isl_union_flow_get_non_must_dependence(
2192 __isl_keep isl_union_flow *flow)
2194 if (!flow)
2195 return NULL;
2196 return isl_union_map_copy(flow->may_dep);
2199 /* Return the subset of the sink accesses for which definitely
2200 * no source was found.
2202 __isl_give isl_union_map *isl_union_flow_get_must_no_source(
2203 __isl_keep isl_union_flow *flow)
2205 if (!flow)
2206 return NULL;
2207 return isl_union_map_copy(flow->must_no_source);
2210 /* Return the subset of the sink accesses for which possibly
2211 * no source was found, including those for which definitely
2212 * no source was found.
2214 __isl_give isl_union_map *isl_union_flow_get_may_no_source(
2215 __isl_keep isl_union_flow *flow)
2217 if (!flow)
2218 return NULL;
2219 return isl_union_map_union(isl_union_map_copy(flow->must_no_source),
2220 isl_union_map_copy(flow->may_no_source));
2223 /* Return the subset of the sink accesses for which possibly, but not
2224 * definitely, no source was found.
2226 static __isl_give isl_union_map *isl_union_flow_get_non_must_no_source(
2227 __isl_keep isl_union_flow *flow)
2229 if (!flow)
2230 return NULL;
2231 return isl_union_map_copy(flow->may_no_source);
2234 /* Create a new isl_union_flow object, initialized with empty
2235 * dependence relations and sink subsets.
2237 static __isl_give isl_union_flow *isl_union_flow_alloc(
2238 __isl_take isl_space *space)
2240 isl_ctx *ctx;
2241 isl_union_map *empty;
2242 isl_union_flow *flow;
2244 if (!space)
2245 return NULL;
2246 ctx = isl_space_get_ctx(space);
2247 flow = isl_alloc_type(ctx, isl_union_flow);
2248 if (!flow)
2249 goto error;
2251 empty = isl_union_map_empty(space);
2252 flow->must_dep = isl_union_map_copy(empty);
2253 flow->may_dep = isl_union_map_copy(empty);
2254 flow->must_no_source = isl_union_map_copy(empty);
2255 flow->may_no_source = empty;
2257 if (!flow->must_dep || !flow->may_dep ||
2258 !flow->must_no_source || !flow->may_no_source)
2259 return isl_union_flow_free(flow);
2261 return flow;
2262 error:
2263 isl_space_free(space);
2264 return NULL;
2267 /* Copy this isl_union_flow object.
2269 __isl_give isl_union_flow *isl_union_flow_copy(__isl_keep isl_union_flow *flow)
2271 isl_union_flow *copy;
2273 if (!flow)
2274 return NULL;
2276 copy = isl_union_flow_alloc(isl_union_map_get_space(flow->must_dep));
2278 if (!copy)
2279 return NULL;
2281 copy->must_dep = isl_union_map_union(copy->must_dep,
2282 isl_union_map_copy(flow->must_dep));
2283 copy->may_dep = isl_union_map_union(copy->may_dep,
2284 isl_union_map_copy(flow->may_dep));
2285 copy->must_no_source = isl_union_map_union(copy->must_no_source,
2286 isl_union_map_copy(flow->must_no_source));
2287 copy->may_no_source = isl_union_map_union(copy->may_no_source,
2288 isl_union_map_copy(flow->may_no_source));
2290 if (!copy->must_dep || !copy->may_dep ||
2291 !copy->must_no_source || !copy->may_no_source)
2292 return isl_union_flow_free(copy);
2294 return copy;
2297 /* Drop the schedule dimensions from the iteration domains in "flow".
2298 * In particular, the schedule dimensions have been prepended
2299 * to the iteration domains prior to the dependence analysis by
2300 * replacing the iteration domain D, by the wrapped map [S -> D].
2301 * Replace these wrapped maps by the original D.
2303 * In particular, the dependences computed by access_info_compute_flow_core
2304 * are of the form
2306 * [S -> D] -> [[S' -> D'] -> A]
2308 * The schedule dimensions are projected out by first currying the range,
2309 * resulting in
2311 * [S -> D] -> [S' -> [D' -> A]]
2313 * and then computing the factor range
2315 * D -> [D' -> A]
2317 static __isl_give isl_union_flow *isl_union_flow_drop_schedule(
2318 __isl_take isl_union_flow *flow)
2320 if (!flow)
2321 return NULL;
2323 flow->must_dep = isl_union_map_range_curry(flow->must_dep);
2324 flow->must_dep = isl_union_map_factor_range(flow->must_dep);
2325 flow->may_dep = isl_union_map_range_curry(flow->may_dep);
2326 flow->may_dep = isl_union_map_factor_range(flow->may_dep);
2327 flow->must_no_source =
2328 isl_union_map_domain_factor_range(flow->must_no_source);
2329 flow->may_no_source =
2330 isl_union_map_domain_factor_range(flow->may_no_source);
2332 if (!flow->must_dep || !flow->may_dep ||
2333 !flow->must_no_source || !flow->may_no_source)
2334 return isl_union_flow_free(flow);
2336 return flow;
2339 struct isl_compute_flow_data {
2340 isl_union_map *must_source;
2341 isl_union_map *may_source;
2342 isl_union_flow *flow;
2344 int count;
2345 int must;
2346 isl_space *dim;
2347 struct isl_sched_info *sink_info;
2348 struct isl_sched_info **source_info;
2349 isl_access_info *accesses;
2352 static isl_stat count_matching_array(__isl_take isl_map *map, void *user)
2354 int eq;
2355 isl_space *dim;
2356 struct isl_compute_flow_data *data;
2358 data = (struct isl_compute_flow_data *)user;
2360 dim = isl_space_range(isl_map_get_space(map));
2362 eq = isl_space_is_equal(dim, data->dim);
2364 isl_space_free(dim);
2365 isl_map_free(map);
2367 if (eq < 0)
2368 return isl_stat_error;
2369 if (eq)
2370 data->count++;
2372 return isl_stat_ok;
2375 static isl_stat collect_matching_array(__isl_take isl_map *map, void *user)
2377 int eq;
2378 isl_space *dim;
2379 struct isl_sched_info *info;
2380 struct isl_compute_flow_data *data;
2382 data = (struct isl_compute_flow_data *)user;
2384 dim = isl_space_range(isl_map_get_space(map));
2386 eq = isl_space_is_equal(dim, data->dim);
2388 isl_space_free(dim);
2390 if (eq < 0)
2391 goto error;
2392 if (!eq) {
2393 isl_map_free(map);
2394 return isl_stat_ok;
2397 info = sched_info_alloc(map);
2398 data->source_info[data->count] = info;
2400 data->accesses = isl_access_info_add_source(data->accesses,
2401 map, data->must, info);
2403 data->count++;
2405 return isl_stat_ok;
2406 error:
2407 isl_map_free(map);
2408 return isl_stat_error;
2411 /* Determine the shared nesting level and the "textual order" of
2412 * the given accesses.
2414 * We first determine the minimal schedule dimension for both accesses.
2416 * If among those dimensions, we can find one where both have a fixed
2417 * value and if moreover those values are different, then the previous
2418 * dimension is the last shared nesting level and the textual order
2419 * is determined based on the order of the fixed values.
2420 * If no such fixed values can be found, then we set the shared
2421 * nesting level to the minimal schedule dimension, with no textual ordering.
2423 static int before(void *first, void *second)
2425 struct isl_sched_info *info1 = first;
2426 struct isl_sched_info *info2 = second;
2427 isl_size n1, n2;
2428 int i;
2430 n1 = isl_vec_size(info1->cst);
2431 n2 = isl_vec_size(info2->cst);
2432 if (n1 < 0 || n2 < 0)
2433 return -1;
2435 if (n2 < n1)
2436 n1 = n2;
2438 for (i = 0; i < n1; ++i) {
2439 int r;
2440 int cmp;
2442 if (!info1->is_cst[i])
2443 continue;
2444 if (!info2->is_cst[i])
2445 continue;
2446 cmp = isl_vec_cmp_element(info1->cst, info2->cst, i);
2447 if (cmp == 0)
2448 continue;
2450 r = 2 * i + (cmp < 0);
2452 return r;
2455 return 2 * n1;
2458 /* Check if the given two accesses may be coscheduled.
2459 * If so, return isl_bool_true. Otherwise return isl_bool_false.
2461 * Two accesses may only be coscheduled if the fixed schedule
2462 * coordinates have the same values.
2464 static isl_bool coscheduled(void *first, void *second)
2466 struct isl_sched_info *info1 = first;
2467 struct isl_sched_info *info2 = second;
2468 isl_size n1, n2;
2469 int i;
2471 n1 = isl_vec_size(info1->cst);
2472 n2 = isl_vec_size(info2->cst);
2473 if (n1 < 0 || n2 < 0)
2474 return isl_bool_error;
2476 if (n2 < n1)
2477 n1 = n2;
2479 for (i = 0; i < n1; ++i) {
2480 int cmp;
2482 if (!info1->is_cst[i])
2483 continue;
2484 if (!info2->is_cst[i])
2485 continue;
2486 cmp = isl_vec_cmp_element(info1->cst, info2->cst, i);
2487 if (cmp != 0)
2488 return isl_bool_false;
2491 return isl_bool_true;
2494 /* Given a sink access, look for all the source accesses that access
2495 * the same array and perform dataflow analysis on them using
2496 * isl_access_info_compute_flow_core.
2498 static isl_stat compute_flow(__isl_take isl_map *map, void *user)
2500 int i;
2501 isl_ctx *ctx;
2502 struct isl_compute_flow_data *data;
2503 isl_flow *flow;
2504 isl_union_flow *df;
2506 data = (struct isl_compute_flow_data *)user;
2507 df = data->flow;
2509 ctx = isl_map_get_ctx(map);
2511 data->accesses = NULL;
2512 data->sink_info = NULL;
2513 data->source_info = NULL;
2514 data->count = 0;
2515 data->dim = isl_space_range(isl_map_get_space(map));
2517 if (isl_union_map_foreach_map(data->must_source,
2518 &count_matching_array, data) < 0)
2519 goto error;
2520 if (isl_union_map_foreach_map(data->may_source,
2521 &count_matching_array, data) < 0)
2522 goto error;
2524 data->sink_info = sched_info_alloc(map);
2525 data->source_info = isl_calloc_array(ctx, struct isl_sched_info *,
2526 data->count);
2528 data->accesses = isl_access_info_alloc(isl_map_copy(map),
2529 data->sink_info, &before, data->count);
2530 if (!data->sink_info || (data->count && !data->source_info) ||
2531 !data->accesses)
2532 goto error;
2533 data->accesses->coscheduled = &coscheduled;
2534 data->count = 0;
2535 data->must = 1;
2536 if (isl_union_map_foreach_map(data->must_source,
2537 &collect_matching_array, data) < 0)
2538 goto error;
2539 data->must = 0;
2540 if (isl_union_map_foreach_map(data->may_source,
2541 &collect_matching_array, data) < 0)
2542 goto error;
2544 flow = access_info_compute_flow_core(data->accesses);
2545 data->accesses = NULL;
2547 if (!flow)
2548 goto error;
2550 df->must_no_source = isl_union_map_union(df->must_no_source,
2551 isl_union_map_from_map(isl_flow_get_no_source(flow, 1)));
2552 df->may_no_source = isl_union_map_union(df->may_no_source,
2553 isl_union_map_from_map(isl_flow_get_no_source(flow, 0)));
2555 for (i = 0; i < flow->n_source; ++i) {
2556 isl_union_map *dep;
2557 dep = isl_union_map_from_map(isl_map_copy(flow->dep[i].map));
2558 if (flow->dep[i].must)
2559 df->must_dep = isl_union_map_union(df->must_dep, dep);
2560 else
2561 df->may_dep = isl_union_map_union(df->may_dep, dep);
2564 isl_flow_free(flow);
2566 sched_info_free(data->sink_info);
2567 if (data->source_info) {
2568 for (i = 0; i < data->count; ++i)
2569 sched_info_free(data->source_info[i]);
2570 free(data->source_info);
2572 isl_space_free(data->dim);
2573 isl_map_free(map);
2575 return isl_stat_ok;
2576 error:
2577 isl_access_info_free(data->accesses);
2578 sched_info_free(data->sink_info);
2579 if (data->source_info) {
2580 for (i = 0; i < data->count; ++i)
2581 sched_info_free(data->source_info[i]);
2582 free(data->source_info);
2584 isl_space_free(data->dim);
2585 isl_map_free(map);
2587 return isl_stat_error;
2590 /* Add the kills of "info" to the must-sources.
2592 static __isl_give isl_union_access_info *
2593 isl_union_access_info_add_kill_to_must_source(
2594 __isl_take isl_union_access_info *info)
2596 isl_union_map *must, *kill;
2598 must = isl_union_access_info_get_must_source(info);
2599 kill = isl_union_access_info_get_kill(info);
2600 must = isl_union_map_union(must, kill);
2601 return isl_union_access_info_set_must_source(info, must);
2604 /* Drop dependences from "flow" that purely originate from kills.
2605 * That is, only keep those dependences that originate from
2606 * the original must-sources "must" and/or the original may-sources "may".
2607 * In particular, "must" contains the must-sources from before
2608 * the kills were added and "may" contains the may-source from before
2609 * the kills were removed.
2611 * The dependences are of the form
2613 * Source -> [Sink -> Data]
2615 * Only those dependences are kept where the Source -> Data part
2616 * is a subset of the original may-sources or must-sources.
2617 * Of those, only the must-dependences that intersect with the must-sources
2618 * remain must-dependences.
2619 * If there is some overlap between the may-sources and the must-sources,
2620 * then the may-dependences and must-dependences may also overlap.
2621 * This should be fine since the may-dependences are only kept
2622 * disjoint from the must-dependences for the isl_union_map_compute_flow
2623 * interface. This interface does not support kills, so it will
2624 * not end up calling this function.
2626 static __isl_give isl_union_flow *isl_union_flow_drop_kill_source(
2627 __isl_take isl_union_flow *flow, __isl_take isl_union_map *must,
2628 __isl_take isl_union_map *may)
2630 isl_union_map *move;
2632 if (!flow)
2633 goto error;
2634 move = isl_union_map_copy(flow->must_dep);
2635 move = isl_union_map_intersect_range_factor_range(move,
2636 isl_union_map_copy(may));
2637 may = isl_union_map_union(may, isl_union_map_copy(must));
2638 flow->may_dep = isl_union_map_intersect_range_factor_range(
2639 flow->may_dep, may);
2640 flow->must_dep = isl_union_map_intersect_range_factor_range(
2641 flow->must_dep, must);
2642 flow->may_dep = isl_union_map_union(flow->may_dep, move);
2643 if (!flow->must_dep || !flow->may_dep)
2644 return isl_union_flow_free(flow);
2646 return flow;
2647 error:
2648 isl_union_map_free(must);
2649 isl_union_map_free(may);
2650 return NULL;
2653 /* Remove the must accesses from the may accesses.
2655 * A must access always trumps a may access, so there is no need
2656 * for a must access to also be considered as a may access. Doing so
2657 * would only cost extra computations only to find out that
2658 * the duplicated may access does not make any difference.
2660 static __isl_give isl_union_access_info *isl_union_access_info_normalize(
2661 __isl_take isl_union_access_info *access)
2663 if (!access)
2664 return NULL;
2665 access->access[isl_access_may_source] =
2666 isl_union_map_subtract(access->access[isl_access_may_source],
2667 isl_union_map_copy(access->access[isl_access_must_source]));
2668 if (!access->access[isl_access_may_source])
2669 return isl_union_access_info_free(access);
2671 return access;
2674 /* Given a description of the "sink" accesses, the "source" accesses and
2675 * a schedule, compute for each instance of a sink access
2676 * and for each element accessed by that instance,
2677 * the possible or definite source accesses that last accessed the
2678 * element accessed by the sink access before this sink access
2679 * in the sense that there is no intermediate definite source access.
2681 * The must_no_source and may_no_source elements of the result
2682 * are subsets of access->sink. The elements must_dep and may_dep
2683 * map domain elements of access->{may,must)_source to
2684 * domain elements of access->sink.
2686 * This function is used when only the schedule map representation
2687 * is available.
2689 * We first prepend the schedule dimensions to the domain
2690 * of the accesses so that we can easily compare their relative order.
2691 * Then we consider each sink access individually in compute_flow.
2693 static __isl_give isl_union_flow *compute_flow_union_map(
2694 __isl_take isl_union_access_info *access)
2696 struct isl_compute_flow_data data;
2697 isl_union_map *sink;
2699 access = isl_union_access_info_align_params(access);
2700 access = isl_union_access_info_introduce_schedule(access);
2701 if (!access)
2702 return NULL;
2704 data.must_source = access->access[isl_access_must_source];
2705 data.may_source = access->access[isl_access_may_source];
2707 sink = access->access[isl_access_sink];
2708 data.flow = isl_union_flow_alloc(isl_union_map_get_space(sink));
2710 if (isl_union_map_foreach_map(sink, &compute_flow, &data) < 0)
2711 goto error;
2713 data.flow = isl_union_flow_drop_schedule(data.flow);
2715 isl_union_access_info_free(access);
2716 return data.flow;
2717 error:
2718 isl_union_access_info_free(access);
2719 isl_union_flow_free(data.flow);
2720 return NULL;
2723 /* A schedule access relation.
2725 * The access relation "access" is of the form [S -> D] -> A,
2726 * where S corresponds to the prefix schedule at "node".
2727 * "must" is only relevant for source accesses and indicates
2728 * whether the access is a must source or a may source.
2730 struct isl_scheduled_access {
2731 isl_map *access;
2732 int must;
2733 isl_schedule_node *node;
2736 /* Data structure for keeping track of individual scheduled sink and source
2737 * accesses when computing dependence analysis based on a schedule tree.
2739 * "n_sink" is the number of used entries in "sink"
2740 * "n_source" is the number of used entries in "source"
2742 * "set_sink", "must" and "node" are only used inside collect_sink_source,
2743 * to keep track of the current node and
2744 * of what extract_sink_source needs to do.
2746 struct isl_compute_flow_schedule_data {
2747 isl_union_access_info *access;
2749 int n_sink;
2750 int n_source;
2752 struct isl_scheduled_access *sink;
2753 struct isl_scheduled_access *source;
2755 int set_sink;
2756 int must;
2757 isl_schedule_node *node;
2760 /* Align the parameters of all sinks with all sources.
2762 * If there are no sinks or no sources, then no alignment is needed.
2764 static void isl_compute_flow_schedule_data_align_params(
2765 struct isl_compute_flow_schedule_data *data)
2767 int i;
2768 isl_space *space;
2770 if (data->n_sink == 0 || data->n_source == 0)
2771 return;
2773 space = isl_map_get_space(data->sink[0].access);
2775 for (i = 1; i < data->n_sink; ++i)
2776 space = isl_space_align_params(space,
2777 isl_map_get_space(data->sink[i].access));
2778 for (i = 0; i < data->n_source; ++i)
2779 space = isl_space_align_params(space,
2780 isl_map_get_space(data->source[i].access));
2782 for (i = 0; i < data->n_sink; ++i)
2783 data->sink[i].access =
2784 isl_map_align_params(data->sink[i].access,
2785 isl_space_copy(space));
2786 for (i = 0; i < data->n_source; ++i)
2787 data->source[i].access =
2788 isl_map_align_params(data->source[i].access,
2789 isl_space_copy(space));
2791 isl_space_free(space);
2794 /* Free all the memory referenced from "data".
2795 * Do not free "data" itself as it may be allocated on the stack.
2797 static void isl_compute_flow_schedule_data_clear(
2798 struct isl_compute_flow_schedule_data *data)
2800 int i;
2802 if (!data->sink)
2803 return;
2805 for (i = 0; i < data->n_sink; ++i) {
2806 isl_map_free(data->sink[i].access);
2807 isl_schedule_node_free(data->sink[i].node);
2810 for (i = 0; i < data->n_source; ++i) {
2811 isl_map_free(data->source[i].access);
2812 isl_schedule_node_free(data->source[i].node);
2815 free(data->sink);
2818 /* isl_schedule_foreach_schedule_node_top_down callback for counting
2819 * (an upper bound on) the number of sinks and sources.
2821 * Sinks and sources are only extracted at leaves of the tree,
2822 * so we skip the node if it is not a leaf.
2823 * Otherwise we increment data->n_sink and data->n_source with
2824 * the number of spaces in the sink and source access domains
2825 * that reach this node.
2827 static isl_bool count_sink_source(__isl_keep isl_schedule_node *node,
2828 void *user)
2830 struct isl_compute_flow_schedule_data *data = user;
2831 isl_union_set *domain;
2832 isl_union_map *umap;
2833 isl_bool r = isl_bool_false;
2834 isl_size n;
2836 if (isl_schedule_node_get_type(node) != isl_schedule_node_leaf)
2837 return isl_bool_true;
2839 domain = isl_schedule_node_get_universe_domain(node);
2841 umap = isl_union_map_copy(data->access->access[isl_access_sink]);
2842 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
2843 data->n_sink += n = isl_union_map_n_map(umap);
2844 isl_union_map_free(umap);
2845 if (n < 0)
2846 r = isl_bool_error;
2848 umap = isl_union_map_copy(data->access->access[isl_access_must_source]);
2849 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
2850 data->n_source += n = isl_union_map_n_map(umap);
2851 isl_union_map_free(umap);
2852 if (n < 0)
2853 r = isl_bool_error;
2855 umap = isl_union_map_copy(data->access->access[isl_access_may_source]);
2856 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
2857 data->n_source += n = isl_union_map_n_map(umap);
2858 isl_union_map_free(umap);
2859 if (n < 0)
2860 r = isl_bool_error;
2862 isl_union_set_free(domain);
2864 return r;
2867 /* Add a single scheduled sink or source (depending on data->set_sink)
2868 * with scheduled access relation "map", must property data->must and
2869 * schedule node data->node to the list of sinks or sources.
2871 static isl_stat extract_sink_source(__isl_take isl_map *map, void *user)
2873 struct isl_compute_flow_schedule_data *data = user;
2874 struct isl_scheduled_access *access;
2876 if (data->set_sink)
2877 access = data->sink + data->n_sink++;
2878 else
2879 access = data->source + data->n_source++;
2881 access->access = map;
2882 access->must = data->must;
2883 access->node = isl_schedule_node_copy(data->node);
2885 return isl_stat_ok;
2888 /* isl_schedule_foreach_schedule_node_top_down callback for collecting
2889 * individual scheduled source and sink accesses (taking into account
2890 * the domain of the schedule).
2892 * We only collect accesses at the leaves of the schedule tree.
2893 * We prepend the schedule dimensions at the leaf to the iteration
2894 * domains of the source and sink accesses and then extract
2895 * the individual accesses (per space).
2897 * In particular, if the prefix schedule at the node is of the form
2899 * D -> S
2901 * while the access relations are of the form
2903 * D -> A
2905 * then the updated access relations are of the form
2907 * [S -> D] -> A
2909 * Note that S consists of a single space such that introducing S
2910 * in the access relations does not increase the number of spaces.
2912 static isl_bool collect_sink_source(__isl_keep isl_schedule_node *node,
2913 void *user)
2915 struct isl_compute_flow_schedule_data *data = user;
2916 isl_union_map *prefix;
2917 isl_union_map *umap;
2918 isl_bool r = isl_bool_false;
2920 if (isl_schedule_node_get_type(node) != isl_schedule_node_leaf)
2921 return isl_bool_true;
2923 data->node = node;
2925 prefix = isl_schedule_node_get_prefix_schedule_relation(node);
2926 prefix = isl_union_map_reverse(prefix);
2927 prefix = isl_union_map_range_map(prefix);
2929 data->set_sink = 1;
2930 umap = isl_union_map_copy(data->access->access[isl_access_sink]);
2931 umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
2932 if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
2933 r = isl_bool_error;
2934 isl_union_map_free(umap);
2936 data->set_sink = 0;
2937 data->must = 1;
2938 umap = isl_union_map_copy(data->access->access[isl_access_must_source]);
2939 umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
2940 if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
2941 r = isl_bool_error;
2942 isl_union_map_free(umap);
2944 data->set_sink = 0;
2945 data->must = 0;
2946 umap = isl_union_map_copy(data->access->access[isl_access_may_source]);
2947 umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
2948 if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
2949 r = isl_bool_error;
2950 isl_union_map_free(umap);
2952 isl_union_map_free(prefix);
2954 return r;
2957 /* isl_access_info_compute_flow callback for determining whether
2958 * the shared nesting level and the ordering within that level
2959 * for two scheduled accesses for use in compute_single_flow.
2961 * The tokens passed to this function refer to the leaves
2962 * in the schedule tree where the accesses take place.
2964 * If n is the shared number of loops, then we need to return
2965 * "2 * n + 1" if "first" precedes "second" inside the innermost
2966 * shared loop and "2 * n" otherwise.
2968 * The innermost shared ancestor may be the leaves themselves
2969 * if the accesses take place in the same leaf. Otherwise,
2970 * it is either a set node or a sequence node. Only in the case
2971 * of a sequence node do we consider one access to precede the other.
2973 static int before_node(void *first, void *second)
2975 isl_schedule_node *node1 = first;
2976 isl_schedule_node *node2 = second;
2977 isl_schedule_node *shared;
2978 isl_size depth;
2979 int before = 0;
2981 shared = isl_schedule_node_get_shared_ancestor(node1, node2);
2982 depth = isl_schedule_node_get_schedule_depth(shared);
2983 if (depth < 0) {
2984 isl_schedule_node_free(shared);
2985 return -1;
2988 if (isl_schedule_node_get_type(shared) == isl_schedule_node_sequence) {
2989 isl_size pos1, pos2;
2991 pos1 = isl_schedule_node_get_ancestor_child_position(node1,
2992 shared);
2993 pos2 = isl_schedule_node_get_ancestor_child_position(node2,
2994 shared);
2995 if (pos1 < 0 || pos2 < 0) {
2996 isl_schedule_node_free(shared);
2997 return -1;
2999 before = pos1 < pos2;
3002 isl_schedule_node_free(shared);
3004 return 2 * depth + before;
3007 /* Check if the given two accesses may be coscheduled.
3008 * If so, return isl_bool_true. Otherwise return isl_bool_false.
3010 * Two accesses may only be coscheduled if they appear in the same leaf.
3012 static isl_bool coscheduled_node(void *first, void *second)
3014 isl_schedule_node *node1 = first;
3015 isl_schedule_node *node2 = second;
3017 return node1 == node2;
3020 /* Add the scheduled sources from "data" that access
3021 * the same data space as "sink" to "access".
3023 static __isl_give isl_access_info *add_matching_sources(
3024 __isl_take isl_access_info *access, struct isl_scheduled_access *sink,
3025 struct isl_compute_flow_schedule_data *data)
3027 int i;
3028 isl_space *space;
3030 space = isl_space_range(isl_map_get_space(sink->access));
3031 for (i = 0; i < data->n_source; ++i) {
3032 struct isl_scheduled_access *source;
3033 isl_space *source_space;
3034 int eq;
3036 source = &data->source[i];
3037 source_space = isl_map_get_space(source->access);
3038 source_space = isl_space_range(source_space);
3039 eq = isl_space_is_equal(space, source_space);
3040 isl_space_free(source_space);
3042 if (!eq)
3043 continue;
3044 if (eq < 0)
3045 goto error;
3047 access = isl_access_info_add_source(access,
3048 isl_map_copy(source->access), source->must, source->node);
3051 isl_space_free(space);
3052 return access;
3053 error:
3054 isl_space_free(space);
3055 isl_access_info_free(access);
3056 return NULL;
3059 /* Given a scheduled sink access relation "sink", compute the corresponding
3060 * dependences on the sources in "data" and add the computed dependences
3061 * to "uf".
3063 * The dependences computed by access_info_compute_flow_core are of the form
3065 * [S -> I] -> [[S' -> I'] -> A]
3067 * The schedule dimensions are projected out by first currying the range,
3068 * resulting in
3070 * [S -> I] -> [S' -> [I' -> A]]
3072 * and then computing the factor range
3074 * I -> [I' -> A]
3076 static __isl_give isl_union_flow *compute_single_flow(
3077 __isl_take isl_union_flow *uf, struct isl_scheduled_access *sink,
3078 struct isl_compute_flow_schedule_data *data)
3080 int i;
3081 isl_access_info *access;
3082 isl_flow *flow;
3083 isl_map *map;
3085 if (!uf)
3086 return NULL;
3088 access = isl_access_info_alloc(isl_map_copy(sink->access), sink->node,
3089 &before_node, data->n_source);
3090 if (access)
3091 access->coscheduled = &coscheduled_node;
3092 access = add_matching_sources(access, sink, data);
3094 flow = access_info_compute_flow_core(access);
3095 if (!flow)
3096 return isl_union_flow_free(uf);
3098 map = isl_map_domain_factor_range(isl_flow_get_no_source(flow, 1));
3099 uf->must_no_source = isl_union_map_union(uf->must_no_source,
3100 isl_union_map_from_map(map));
3101 map = isl_map_domain_factor_range(isl_flow_get_no_source(flow, 0));
3102 uf->may_no_source = isl_union_map_union(uf->may_no_source,
3103 isl_union_map_from_map(map));
3105 for (i = 0; i < flow->n_source; ++i) {
3106 isl_union_map *dep;
3108 map = isl_map_range_curry(isl_map_copy(flow->dep[i].map));
3109 map = isl_map_factor_range(map);
3110 dep = isl_union_map_from_map(map);
3111 if (flow->dep[i].must)
3112 uf->must_dep = isl_union_map_union(uf->must_dep, dep);
3113 else
3114 uf->may_dep = isl_union_map_union(uf->may_dep, dep);
3117 isl_flow_free(flow);
3119 return uf;
3122 /* Given a description of the "sink" accesses, the "source" accesses and
3123 * a schedule, compute for each instance of a sink access
3124 * and for each element accessed by that instance,
3125 * the possible or definite source accesses that last accessed the
3126 * element accessed by the sink access before this sink access
3127 * in the sense that there is no intermediate definite source access.
3128 * Only consider dependences between statement instances that belong
3129 * to the domain of the schedule.
3131 * The must_no_source and may_no_source elements of the result
3132 * are subsets of access->sink. The elements must_dep and may_dep
3133 * map domain elements of access->{may,must)_source to
3134 * domain elements of access->sink.
3136 * This function is used when a schedule tree representation
3137 * is available.
3139 * We extract the individual scheduled source and sink access relations
3140 * (taking into account the domain of the schedule) and
3141 * then compute dependences for each scheduled sink individually.
3143 static __isl_give isl_union_flow *compute_flow_schedule(
3144 __isl_take isl_union_access_info *access)
3146 struct isl_compute_flow_schedule_data data = { access };
3147 int i, n;
3148 isl_ctx *ctx;
3149 isl_space *space;
3150 isl_union_flow *flow;
3152 ctx = isl_union_access_info_get_ctx(access);
3154 data.n_sink = 0;
3155 data.n_source = 0;
3156 if (isl_schedule_foreach_schedule_node_top_down(access->schedule,
3157 &count_sink_source, &data) < 0)
3158 goto error;
3160 n = data.n_sink + data.n_source;
3161 data.sink = isl_calloc_array(ctx, struct isl_scheduled_access, n);
3162 if (n && !data.sink)
3163 goto error;
3164 data.source = data.sink + data.n_sink;
3166 data.n_sink = 0;
3167 data.n_source = 0;
3168 if (isl_schedule_foreach_schedule_node_top_down(access->schedule,
3169 &collect_sink_source, &data) < 0)
3170 goto error;
3172 space = isl_union_map_get_space(access->access[isl_access_sink]);
3173 flow = isl_union_flow_alloc(space);
3175 isl_compute_flow_schedule_data_align_params(&data);
3177 for (i = 0; i < data.n_sink; ++i)
3178 flow = compute_single_flow(flow, &data.sink[i], &data);
3180 isl_compute_flow_schedule_data_clear(&data);
3182 isl_union_access_info_free(access);
3183 return flow;
3184 error:
3185 isl_union_access_info_free(access);
3186 isl_compute_flow_schedule_data_clear(&data);
3187 return NULL;
3190 /* Given a description of the "sink" accesses, the "source" accesses and
3191 * a schedule, compute for each instance of a sink access
3192 * and for each element accessed by that instance,
3193 * the possible or definite source accesses that last accessed the
3194 * element accessed by the sink access before this sink access
3195 * in the sense that there is no intermediate definite source access.
3197 * The must_no_source and may_no_source elements of the result
3198 * are subsets of access->sink. The elements must_dep and may_dep
3199 * map domain elements of access->{may,must)_source to
3200 * domain elements of access->sink.
3202 * If any kills have been specified, then they are treated as
3203 * must-sources internally. Any dependence that purely derives
3204 * from an original kill is removed from the output.
3206 * We check whether the schedule is available as a schedule tree
3207 * or a schedule map and call the corresponding function to perform
3208 * the analysis.
3210 __isl_give isl_union_flow *isl_union_access_info_compute_flow(
3211 __isl_take isl_union_access_info *access)
3213 isl_bool has_kill;
3214 isl_union_map *must = NULL, *may = NULL;
3215 isl_union_flow *flow;
3217 has_kill = isl_union_access_has_kill(access);
3218 if (has_kill < 0)
3219 goto error;
3220 if (has_kill) {
3221 must = isl_union_access_info_get_must_source(access);
3222 may = isl_union_access_info_get_may_source(access);
3224 access = isl_union_access_info_add_kill_to_must_source(access);
3225 access = isl_union_access_info_normalize(access);
3226 if (!access)
3227 goto error;
3228 if (access->schedule)
3229 flow = compute_flow_schedule(access);
3230 else
3231 flow = compute_flow_union_map(access);
3232 if (has_kill)
3233 flow = isl_union_flow_drop_kill_source(flow, must, may);
3234 return flow;
3235 error:
3236 isl_union_access_info_free(access);
3237 isl_union_map_free(must);
3238 isl_union_map_free(may);
3239 return NULL;
3242 /* Print the information contained in "flow" to "p".
3243 * The information is printed as a YAML document.
3245 __isl_give isl_printer *isl_printer_print_union_flow(
3246 __isl_take isl_printer *p, __isl_keep isl_union_flow *flow)
3248 isl_union_map *umap;
3250 if (!flow)
3251 return isl_printer_free(p);
3253 p = isl_printer_yaml_start_mapping(p);
3254 umap = isl_union_flow_get_full_must_dependence(flow);
3255 p = print_union_map_field(p, "must_dependence", umap);
3256 isl_union_map_free(umap);
3257 umap = isl_union_flow_get_full_may_dependence(flow);
3258 p = print_union_map_field(p, "may_dependence", umap);
3259 isl_union_map_free(umap);
3260 p = print_union_map_field(p, "must_no_source", flow->must_no_source);
3261 umap = isl_union_flow_get_may_no_source(flow);
3262 p = print_union_map_field(p, "may_no_source", umap);
3263 isl_union_map_free(umap);
3264 p = isl_printer_yaml_end_mapping(p);
3266 return p;
3269 /* Return a string representation of the information in "flow".
3270 * The information is printed in flow format.
3272 __isl_give char *isl_union_flow_to_str(__isl_keep isl_union_flow *flow)
3274 isl_printer *p;
3275 char *s;
3277 if (!flow)
3278 return NULL;
3280 p = isl_printer_to_str(isl_union_flow_get_ctx(flow));
3281 p = isl_printer_set_yaml_style(p, ISL_YAML_STYLE_FLOW);
3282 p = isl_printer_print_union_flow(p, flow);
3283 s = isl_printer_get_str(p);
3284 isl_printer_free(p);
3286 return s;
3289 /* Given a collection of "sink" and "source" accesses,
3290 * compute for each iteration of a sink access
3291 * and for each element accessed by that iteration,
3292 * the source access in the list that last accessed the
3293 * element accessed by the sink access before this sink access.
3294 * Each access is given as a map from the loop iterators
3295 * to the array indices.
3296 * The result is a relations between source and sink
3297 * iterations and a subset of the domain of the sink accesses,
3298 * corresponding to those iterations that access an element
3299 * not previously accessed.
3301 * We collect the inputs in an isl_union_access_info object,
3302 * call isl_union_access_info_compute_flow and extract
3303 * the outputs from the result.
3305 int isl_union_map_compute_flow(__isl_take isl_union_map *sink,
3306 __isl_take isl_union_map *must_source,
3307 __isl_take isl_union_map *may_source,
3308 __isl_take isl_union_map *schedule,
3309 __isl_give isl_union_map **must_dep, __isl_give isl_union_map **may_dep,
3310 __isl_give isl_union_map **must_no_source,
3311 __isl_give isl_union_map **may_no_source)
3313 isl_union_access_info *access;
3314 isl_union_flow *flow;
3316 access = isl_union_access_info_from_sink(sink);
3317 access = isl_union_access_info_set_must_source(access, must_source);
3318 access = isl_union_access_info_set_may_source(access, may_source);
3319 access = isl_union_access_info_set_schedule_map(access, schedule);
3320 flow = isl_union_access_info_compute_flow(access);
3322 if (must_dep)
3323 *must_dep = isl_union_flow_get_must_dependence(flow);
3324 if (may_dep)
3325 *may_dep = isl_union_flow_get_non_must_dependence(flow);
3326 if (must_no_source)
3327 *must_no_source = isl_union_flow_get_must_no_source(flow);
3328 if (may_no_source)
3329 *may_no_source = isl_union_flow_get_non_must_no_source(flow);
3331 isl_union_flow_free(flow);
3333 if ((must_dep && !*must_dep) || (may_dep && !*may_dep) ||
3334 (must_no_source && !*must_no_source) ||
3335 (may_no_source && !*may_no_source))
3336 goto error;
3338 return 0;
3339 error:
3340 if (must_dep)
3341 *must_dep = isl_union_map_free(*must_dep);
3342 if (may_dep)
3343 *may_dep = isl_union_map_free(*may_dep);
3344 if (must_no_source)
3345 *must_no_source = isl_union_map_free(*must_no_source);
3346 if (may_no_source)
3347 *may_no_source = isl_union_map_free(*may_no_source);
3348 return -1;