add_lower_div_constraint: return return modified result
[isl.git] / isl_flow.c
blobd032bc7acce83e74ccdf1ff5ced9d2455be4f50e
1 /*
2 * Copyright 2005-2007 Universiteit Leiden
3 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Copyright 2010 INRIA Saclay
5 * Copyright 2012 Universiteit Leiden
6 * Copyright 2014 Ecole Normale Superieure
8 * Use of this software is governed by the MIT license
10 * Written by Sven Verdoolaege, Leiden Institute of Advanced Computer Science,
11 * Universiteit Leiden, Niels Bohrweg 1, 2333 CA Leiden, The Netherlands
12 * and K.U.Leuven, Departement Computerwetenschappen, Celestijnenlaan 200A,
13 * B-3001 Leuven, Belgium
14 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
15 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
16 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
19 #include <isl/val.h>
20 #include <isl/space.h>
21 #include <isl/set.h>
22 #include <isl/map.h>
23 #include <isl/union_set.h>
24 #include <isl/union_map.h>
25 #include <isl/flow.h>
26 #include <isl/schedule_node.h>
27 #include <isl_sort.h>
28 #include <isl/stream.h>
30 enum isl_restriction_type {
31 isl_restriction_type_empty,
32 isl_restriction_type_none,
33 isl_restriction_type_input,
34 isl_restriction_type_output
37 struct isl_restriction {
38 enum isl_restriction_type type;
40 isl_set *source;
41 isl_set *sink;
44 /* Create a restriction of the given type.
46 static __isl_give isl_restriction *isl_restriction_alloc(
47 __isl_take isl_map *source_map, enum isl_restriction_type type)
49 isl_ctx *ctx;
50 isl_restriction *restr;
52 if (!source_map)
53 return NULL;
55 ctx = isl_map_get_ctx(source_map);
56 restr = isl_calloc_type(ctx, struct isl_restriction);
57 if (!restr)
58 goto error;
60 restr->type = type;
62 isl_map_free(source_map);
63 return restr;
64 error:
65 isl_map_free(source_map);
66 return NULL;
69 /* Create a restriction that doesn't restrict anything.
71 __isl_give isl_restriction *isl_restriction_none(__isl_take isl_map *source_map)
73 return isl_restriction_alloc(source_map, isl_restriction_type_none);
76 /* Create a restriction that removes everything.
78 __isl_give isl_restriction *isl_restriction_empty(
79 __isl_take isl_map *source_map)
81 return isl_restriction_alloc(source_map, isl_restriction_type_empty);
84 /* Create a restriction on the input of the maximization problem
85 * based on the given source and sink restrictions.
87 __isl_give isl_restriction *isl_restriction_input(
88 __isl_take isl_set *source_restr, __isl_take isl_set *sink_restr)
90 isl_ctx *ctx;
91 isl_restriction *restr;
93 if (!source_restr || !sink_restr)
94 goto error;
96 ctx = isl_set_get_ctx(source_restr);
97 restr = isl_calloc_type(ctx, struct isl_restriction);
98 if (!restr)
99 goto error;
101 restr->type = isl_restriction_type_input;
102 restr->source = source_restr;
103 restr->sink = sink_restr;
105 return restr;
106 error:
107 isl_set_free(source_restr);
108 isl_set_free(sink_restr);
109 return NULL;
112 /* Create a restriction on the output of the maximization problem
113 * based on the given source restriction.
115 __isl_give isl_restriction *isl_restriction_output(
116 __isl_take isl_set *source_restr)
118 isl_ctx *ctx;
119 isl_restriction *restr;
121 if (!source_restr)
122 return NULL;
124 ctx = isl_set_get_ctx(source_restr);
125 restr = isl_calloc_type(ctx, struct isl_restriction);
126 if (!restr)
127 goto error;
129 restr->type = isl_restriction_type_output;
130 restr->source = source_restr;
132 return restr;
133 error:
134 isl_set_free(source_restr);
135 return NULL;
138 __isl_null isl_restriction *isl_restriction_free(
139 __isl_take isl_restriction *restr)
141 if (!restr)
142 return NULL;
144 isl_set_free(restr->source);
145 isl_set_free(restr->sink);
146 free(restr);
147 return NULL;
150 isl_ctx *isl_restriction_get_ctx(__isl_keep isl_restriction *restr)
152 return restr ? isl_set_get_ctx(restr->source) : NULL;
155 /* A private structure to keep track of a mapping together with
156 * a user-specified identifier and a boolean indicating whether
157 * the map represents a must or may access/dependence.
159 struct isl_labeled_map {
160 struct isl_map *map;
161 void *data;
162 int must;
165 typedef int (*isl_access_coscheduled)(void *first, void *second);
167 /* A structure containing the input for dependence analysis:
168 * - a sink
169 * - n_must + n_may (<= max_source) sources
170 * - a function for determining the relative order of sources and sink
171 * - an optional function "coscheduled" for determining whether sources
172 * may be coscheduled. If "coscheduled" is NULL, then the sources
173 * are assumed not to be coscheduled.
174 * The must sources are placed before the may sources.
176 * domain_map is an auxiliary map that maps the sink access relation
177 * to the domain of this access relation.
178 * This field is only needed when restrict_fn is set and
179 * the field itself is set by isl_access_info_compute_flow.
181 * restrict_fn is a callback that (if not NULL) will be called
182 * right before any lexicographical maximization.
184 struct isl_access_info {
185 isl_map *domain_map;
186 struct isl_labeled_map sink;
187 isl_access_level_before level_before;
188 isl_access_coscheduled coscheduled;
190 isl_access_restrict restrict_fn;
191 void *restrict_user;
193 int max_source;
194 int n_must;
195 int n_may;
196 struct isl_labeled_map source[1];
199 /* A structure containing the output of dependence analysis:
200 * - n_source dependences
201 * - a wrapped subset of the sink for which definitely no source could be found
202 * - a wrapped subset of the sink for which possibly no source could be found
204 struct isl_flow {
205 isl_set *must_no_source;
206 isl_set *may_no_source;
207 int n_source;
208 struct isl_labeled_map *dep;
211 /* Construct an isl_access_info structure and fill it up with
212 * the given data. The number of sources is set to 0.
214 __isl_give isl_access_info *isl_access_info_alloc(__isl_take isl_map *sink,
215 void *sink_user, isl_access_level_before fn, int max_source)
217 isl_ctx *ctx;
218 struct isl_access_info *acc;
220 if (!sink)
221 return NULL;
223 ctx = isl_map_get_ctx(sink);
224 isl_assert(ctx, max_source >= 0, goto error);
226 acc = isl_calloc(ctx, struct isl_access_info,
227 sizeof(struct isl_access_info) +
228 (max_source - 1) * sizeof(struct isl_labeled_map));
229 if (!acc)
230 goto error;
232 acc->sink.map = sink;
233 acc->sink.data = sink_user;
234 acc->level_before = fn;
235 acc->max_source = max_source;
236 acc->n_must = 0;
237 acc->n_may = 0;
239 return acc;
240 error:
241 isl_map_free(sink);
242 return NULL;
245 /* Free the given isl_access_info structure.
247 __isl_null isl_access_info *isl_access_info_free(
248 __isl_take isl_access_info *acc)
250 int i;
252 if (!acc)
253 return NULL;
254 isl_map_free(acc->domain_map);
255 isl_map_free(acc->sink.map);
256 for (i = 0; i < acc->n_must + acc->n_may; ++i)
257 isl_map_free(acc->source[i].map);
258 free(acc);
259 return NULL;
262 isl_ctx *isl_access_info_get_ctx(__isl_keep isl_access_info *acc)
264 return acc ? isl_map_get_ctx(acc->sink.map) : NULL;
267 __isl_give isl_access_info *isl_access_info_set_restrict(
268 __isl_take isl_access_info *acc, isl_access_restrict fn, void *user)
270 if (!acc)
271 return NULL;
272 acc->restrict_fn = fn;
273 acc->restrict_user = user;
274 return acc;
277 /* Add another source to an isl_access_info structure, making
278 * sure the "must" sources are placed before the "may" sources.
279 * This function may be called at most max_source times on a
280 * given isl_access_info structure, with max_source as specified
281 * in the call to isl_access_info_alloc that constructed the structure.
283 __isl_give isl_access_info *isl_access_info_add_source(
284 __isl_take isl_access_info *acc, __isl_take isl_map *source,
285 int must, void *source_user)
287 isl_ctx *ctx;
289 if (!acc)
290 goto error;
291 ctx = isl_map_get_ctx(acc->sink.map);
292 isl_assert(ctx, acc->n_must + acc->n_may < acc->max_source, goto error);
294 if (must) {
295 if (acc->n_may)
296 acc->source[acc->n_must + acc->n_may] =
297 acc->source[acc->n_must];
298 acc->source[acc->n_must].map = source;
299 acc->source[acc->n_must].data = source_user;
300 acc->source[acc->n_must].must = 1;
301 acc->n_must++;
302 } else {
303 acc->source[acc->n_must + acc->n_may].map = source;
304 acc->source[acc->n_must + acc->n_may].data = source_user;
305 acc->source[acc->n_must + acc->n_may].must = 0;
306 acc->n_may++;
309 return acc;
310 error:
311 isl_map_free(source);
312 isl_access_info_free(acc);
313 return NULL;
316 /* A helper struct carrying the isl_access_info and an error condition.
318 struct access_sort_info {
319 isl_access_info *access_info;
320 int error;
323 /* Return -n, 0 or n (with n a positive value), depending on whether
324 * the source access identified by p1 should be sorted before, together
325 * or after that identified by p2.
327 * If p1 appears before p2, then it should be sorted first.
328 * For more generic initial schedules, it is possible that neither
329 * p1 nor p2 appears before the other, or at least not in any obvious way.
330 * We therefore also check if p2 appears before p1, in which case p2
331 * should be sorted first.
332 * If not, we try to order the two statements based on the description
333 * of the iteration domains. This results in an arbitrary, but fairly
334 * stable ordering.
336 * In case of an error, sort_info.error is set to true and all elements are
337 * reported to be equal.
339 static int access_sort_cmp(const void *p1, const void *p2, void *user)
341 struct access_sort_info *sort_info = user;
342 isl_access_info *acc = sort_info->access_info;
344 if (sort_info->error)
345 return 0;
347 const struct isl_labeled_map *i1, *i2;
348 int level1, level2;
349 uint32_t h1, h2;
350 i1 = (const struct isl_labeled_map *) p1;
351 i2 = (const struct isl_labeled_map *) p2;
353 level1 = acc->level_before(i1->data, i2->data);
354 if (level1 < 0)
355 goto error;
356 if (level1 % 2)
357 return -1;
359 level2 = acc->level_before(i2->data, i1->data);
360 if (level2 < 0)
361 goto error;
362 if (level2 % 2)
363 return 1;
365 h1 = isl_map_get_hash(i1->map);
366 h2 = isl_map_get_hash(i2->map);
367 return h1 > h2 ? 1 : h1 < h2 ? -1 : 0;
368 error:
369 sort_info->error = 1;
370 return 0;
373 /* Sort the must source accesses in their textual order.
375 static __isl_give isl_access_info *isl_access_info_sort_sources(
376 __isl_take isl_access_info *acc)
378 struct access_sort_info sort_info;
380 sort_info.access_info = acc;
381 sort_info.error = 0;
383 if (!acc)
384 return NULL;
385 if (acc->n_must <= 1)
386 return acc;
388 if (isl_sort(acc->source, acc->n_must, sizeof(struct isl_labeled_map),
389 access_sort_cmp, &sort_info) < 0)
390 return isl_access_info_free(acc);
391 if (sort_info.error)
392 return isl_access_info_free(acc);
394 return acc;
397 /* Align the parameters of the two spaces if needed and then call
398 * isl_space_join.
400 static __isl_give isl_space *space_align_and_join(__isl_take isl_space *left,
401 __isl_take isl_space *right)
403 isl_bool equal_params;
405 equal_params = isl_space_has_equal_params(left, right);
406 if (equal_params < 0)
407 goto error;
408 if (equal_params)
409 return isl_space_join(left, right);
411 left = isl_space_align_params(left, isl_space_copy(right));
412 right = isl_space_align_params(right, isl_space_copy(left));
413 return isl_space_join(left, right);
414 error:
415 isl_space_free(left);
416 isl_space_free(right);
417 return NULL;
420 /* Initialize an empty isl_flow structure corresponding to a given
421 * isl_access_info structure.
422 * For each must access, two dependences are created (initialized
423 * to the empty relation), one for the resulting must dependences
424 * and one for the resulting may dependences. May accesses can
425 * only lead to may dependences, so only one dependence is created
426 * for each of them.
427 * This function is private as isl_flow structures are only supposed
428 * to be created by isl_access_info_compute_flow.
430 static __isl_give isl_flow *isl_flow_alloc(__isl_keep isl_access_info *acc)
432 int i, n;
433 struct isl_ctx *ctx;
434 struct isl_flow *dep;
436 if (!acc)
437 return NULL;
439 ctx = isl_map_get_ctx(acc->sink.map);
440 dep = isl_calloc_type(ctx, struct isl_flow);
441 if (!dep)
442 return NULL;
444 n = 2 * acc->n_must + acc->n_may;
445 dep->dep = isl_calloc_array(ctx, struct isl_labeled_map, n);
446 if (n && !dep->dep)
447 goto error;
449 dep->n_source = n;
450 for (i = 0; i < acc->n_must; ++i) {
451 isl_space *dim;
452 dim = space_align_and_join(
453 isl_map_get_space(acc->source[i].map),
454 isl_space_reverse(isl_map_get_space(acc->sink.map)));
455 dep->dep[2 * i].map = isl_map_empty(dim);
456 dep->dep[2 * i + 1].map = isl_map_copy(dep->dep[2 * i].map);
457 dep->dep[2 * i].data = acc->source[i].data;
458 dep->dep[2 * i + 1].data = acc->source[i].data;
459 dep->dep[2 * i].must = 1;
460 dep->dep[2 * i + 1].must = 0;
461 if (!dep->dep[2 * i].map || !dep->dep[2 * i + 1].map)
462 goto error;
464 for (i = acc->n_must; i < acc->n_must + acc->n_may; ++i) {
465 isl_space *dim;
466 dim = space_align_and_join(
467 isl_map_get_space(acc->source[i].map),
468 isl_space_reverse(isl_map_get_space(acc->sink.map)));
469 dep->dep[acc->n_must + i].map = isl_map_empty(dim);
470 dep->dep[acc->n_must + i].data = acc->source[i].data;
471 dep->dep[acc->n_must + i].must = 0;
472 if (!dep->dep[acc->n_must + i].map)
473 goto error;
476 return dep;
477 error:
478 isl_flow_free(dep);
479 return NULL;
482 /* Iterate over all sources and for each resulting flow dependence
483 * that is not empty, call the user specfied function.
484 * The second argument in this function call identifies the source,
485 * while the third argument correspond to the final argument of
486 * the isl_flow_foreach call.
488 isl_stat isl_flow_foreach(__isl_keep isl_flow *deps,
489 isl_stat (*fn)(__isl_take isl_map *dep, int must, void *dep_user,
490 void *user),
491 void *user)
493 int i;
495 if (!deps)
496 return isl_stat_error;
498 for (i = 0; i < deps->n_source; ++i) {
499 if (isl_map_plain_is_empty(deps->dep[i].map))
500 continue;
501 if (fn(isl_map_copy(deps->dep[i].map), deps->dep[i].must,
502 deps->dep[i].data, user) < 0)
503 return isl_stat_error;
506 return isl_stat_ok;
509 /* Return a copy of the subset of the sink for which no source could be found.
511 __isl_give isl_map *isl_flow_get_no_source(__isl_keep isl_flow *deps, int must)
513 if (!deps)
514 return NULL;
516 if (must)
517 return isl_set_unwrap(isl_set_copy(deps->must_no_source));
518 else
519 return isl_set_unwrap(isl_set_copy(deps->may_no_source));
522 __isl_null isl_flow *isl_flow_free(__isl_take isl_flow *deps)
524 int i;
526 if (!deps)
527 return NULL;
528 isl_set_free(deps->must_no_source);
529 isl_set_free(deps->may_no_source);
530 if (deps->dep) {
531 for (i = 0; i < deps->n_source; ++i)
532 isl_map_free(deps->dep[i].map);
533 free(deps->dep);
535 free(deps);
537 return NULL;
540 isl_ctx *isl_flow_get_ctx(__isl_keep isl_flow *deps)
542 return deps ? isl_set_get_ctx(deps->must_no_source) : NULL;
545 /* Return a map that enforces that the domain iteration occurs after
546 * the range iteration at the given level.
547 * If level is odd, then the domain iteration should occur after
548 * the target iteration in their shared level/2 outermost loops.
549 * In this case we simply need to enforce that these outermost
550 * loop iterations are the same.
551 * If level is even, then the loop iterator of the domain should
552 * be greater than the loop iterator of the range at the last
553 * of the level/2 shared loops, i.e., loop level/2 - 1.
555 static __isl_give isl_map *after_at_level(__isl_take isl_space *dim, int level)
557 struct isl_basic_map *bmap;
559 if (level % 2)
560 bmap = isl_basic_map_equal(dim, level/2);
561 else
562 bmap = isl_basic_map_more_at(dim, level/2 - 1);
564 return isl_map_from_basic_map(bmap);
567 /* Compute the partial lexicographic maximum of "dep" on domain "sink",
568 * but first check if the user has set acc->restrict_fn and if so
569 * update either the input or the output of the maximization problem
570 * with respect to the resulting restriction.
572 * Since the user expects a mapping from sink iterations to source iterations,
573 * whereas the domain of "dep" is a wrapped map, mapping sink iterations
574 * to accessed array elements, we first need to project out the accessed
575 * sink array elements by applying acc->domain_map.
576 * Similarly, the sink restriction specified by the user needs to be
577 * converted back to the wrapped map.
579 static __isl_give isl_map *restricted_partial_lexmax(
580 __isl_keep isl_access_info *acc, __isl_take isl_map *dep,
581 int source, __isl_take isl_set *sink, __isl_give isl_set **empty)
583 isl_map *source_map;
584 isl_restriction *restr;
585 isl_set *sink_domain;
586 isl_set *sink_restr;
587 isl_map *res;
589 if (!acc->restrict_fn)
590 return isl_map_partial_lexmax(dep, sink, empty);
592 source_map = isl_map_copy(dep);
593 source_map = isl_map_apply_domain(source_map,
594 isl_map_copy(acc->domain_map));
595 sink_domain = isl_set_copy(sink);
596 sink_domain = isl_set_apply(sink_domain, isl_map_copy(acc->domain_map));
597 restr = acc->restrict_fn(source_map, sink_domain,
598 acc->source[source].data, acc->restrict_user);
599 isl_set_free(sink_domain);
600 isl_map_free(source_map);
602 if (!restr)
603 goto error;
604 if (restr->type == isl_restriction_type_input) {
605 dep = isl_map_intersect_range(dep, isl_set_copy(restr->source));
606 sink_restr = isl_set_copy(restr->sink);
607 sink_restr = isl_set_apply(sink_restr,
608 isl_map_reverse(isl_map_copy(acc->domain_map)));
609 sink = isl_set_intersect(sink, sink_restr);
610 } else if (restr->type == isl_restriction_type_empty) {
611 isl_space *space = isl_map_get_space(dep);
612 isl_map_free(dep);
613 dep = isl_map_empty(space);
616 res = isl_map_partial_lexmax(dep, sink, empty);
618 if (restr->type == isl_restriction_type_output)
619 res = isl_map_intersect_range(res, isl_set_copy(restr->source));
621 isl_restriction_free(restr);
622 return res;
623 error:
624 isl_map_free(dep);
625 isl_set_free(sink);
626 *empty = NULL;
627 return NULL;
630 /* Compute the last iteration of must source j that precedes the sink
631 * at the given level for sink iterations in set_C.
632 * The subset of set_C for which no such iteration can be found is returned
633 * in *empty.
635 static struct isl_map *last_source(struct isl_access_info *acc,
636 struct isl_set *set_C,
637 int j, int level, struct isl_set **empty)
639 struct isl_map *read_map;
640 struct isl_map *write_map;
641 struct isl_map *dep_map;
642 struct isl_map *after;
643 struct isl_map *result;
645 read_map = isl_map_copy(acc->sink.map);
646 write_map = isl_map_copy(acc->source[j].map);
647 write_map = isl_map_reverse(write_map);
648 dep_map = isl_map_apply_range(read_map, write_map);
649 after = after_at_level(isl_map_get_space(dep_map), level);
650 dep_map = isl_map_intersect(dep_map, after);
651 result = restricted_partial_lexmax(acc, dep_map, j, set_C, empty);
652 result = isl_map_reverse(result);
654 return result;
657 /* For a given mapping between iterations of must source j and iterations
658 * of the sink, compute the last iteration of must source k preceding
659 * the sink at level before_level for any of the sink iterations,
660 * but following the corresponding iteration of must source j at level
661 * after_level.
663 static struct isl_map *last_later_source(struct isl_access_info *acc,
664 struct isl_map *old_map,
665 int j, int before_level,
666 int k, int after_level,
667 struct isl_set **empty)
669 isl_space *dim;
670 struct isl_set *set_C;
671 struct isl_map *read_map;
672 struct isl_map *write_map;
673 struct isl_map *dep_map;
674 struct isl_map *after_write;
675 struct isl_map *before_read;
676 struct isl_map *result;
678 set_C = isl_map_range(isl_map_copy(old_map));
679 read_map = isl_map_copy(acc->sink.map);
680 write_map = isl_map_copy(acc->source[k].map);
682 write_map = isl_map_reverse(write_map);
683 dep_map = isl_map_apply_range(read_map, write_map);
684 dim = space_align_and_join(isl_map_get_space(acc->source[k].map),
685 isl_space_reverse(isl_map_get_space(acc->source[j].map)));
686 after_write = after_at_level(dim, after_level);
687 after_write = isl_map_apply_range(after_write, old_map);
688 after_write = isl_map_reverse(after_write);
689 dep_map = isl_map_intersect(dep_map, after_write);
690 before_read = after_at_level(isl_map_get_space(dep_map), before_level);
691 dep_map = isl_map_intersect(dep_map, before_read);
692 result = restricted_partial_lexmax(acc, dep_map, k, set_C, empty);
693 result = isl_map_reverse(result);
695 return result;
698 /* Given a shared_level between two accesses, return 1 if the
699 * the first can precede the second at the requested target_level.
700 * If the target level is odd, i.e., refers to a statement level
701 * dimension, then first needs to precede second at the requested
702 * level, i.e., shared_level must be equal to target_level.
703 * If the target level is odd, then the two loops should share
704 * at least the requested number of outer loops.
706 static int can_precede_at_level(int shared_level, int target_level)
708 if (shared_level < target_level)
709 return 0;
710 if ((target_level % 2) && shared_level > target_level)
711 return 0;
712 return 1;
715 /* Given a possible flow dependence temp_rel[j] between source j and the sink
716 * at level sink_level, remove those elements for which
717 * there is an iteration of another source k < j that is closer to the sink.
718 * The flow dependences temp_rel[k] are updated with the improved sources.
719 * Any improved source needs to precede the sink at the same level
720 * and needs to follow source j at the same or a deeper level.
721 * The lower this level, the later the execution date of source k.
722 * We therefore consider lower levels first.
724 * If temp_rel[j] is empty, then there can be no improvement and
725 * we return immediately.
727 * This function returns isl_stat_ok in case it was executed successfully and
728 * isl_stat_error in case of errors during the execution of this function.
730 static isl_stat intermediate_sources(__isl_keep isl_access_info *acc,
731 struct isl_map **temp_rel, int j, int sink_level)
733 int k, level;
734 int depth = 2 * isl_map_dim(acc->source[j].map, isl_dim_in) + 1;
736 if (isl_map_plain_is_empty(temp_rel[j]))
737 return isl_stat_ok;
739 for (k = j - 1; k >= 0; --k) {
740 int plevel, plevel2;
741 plevel = acc->level_before(acc->source[k].data, acc->sink.data);
742 if (plevel < 0)
743 return isl_stat_error;
744 if (!can_precede_at_level(plevel, sink_level))
745 continue;
747 plevel2 = acc->level_before(acc->source[j].data,
748 acc->source[k].data);
749 if (plevel2 < 0)
750 return isl_stat_error;
752 for (level = sink_level; level <= depth; ++level) {
753 struct isl_map *T;
754 struct isl_set *trest;
755 struct isl_map *copy;
757 if (!can_precede_at_level(plevel2, level))
758 continue;
760 copy = isl_map_copy(temp_rel[j]);
761 T = last_later_source(acc, copy, j, sink_level, k,
762 level, &trest);
763 if (isl_map_plain_is_empty(T)) {
764 isl_set_free(trest);
765 isl_map_free(T);
766 continue;
768 temp_rel[j] = isl_map_intersect_range(temp_rel[j], trest);
769 temp_rel[k] = isl_map_union_disjoint(temp_rel[k], T);
773 return isl_stat_ok;
776 /* Compute all iterations of may source j that precedes the sink at the given
777 * level for sink iterations in set_C.
779 static __isl_give isl_map *all_sources(__isl_keep isl_access_info *acc,
780 __isl_take isl_set *set_C, int j, int level)
782 isl_map *read_map;
783 isl_map *write_map;
784 isl_map *dep_map;
785 isl_map *after;
787 read_map = isl_map_copy(acc->sink.map);
788 read_map = isl_map_intersect_domain(read_map, set_C);
789 write_map = isl_map_copy(acc->source[acc->n_must + j].map);
790 write_map = isl_map_reverse(write_map);
791 dep_map = isl_map_apply_range(read_map, write_map);
792 after = after_at_level(isl_map_get_space(dep_map), level);
793 dep_map = isl_map_intersect(dep_map, after);
795 return isl_map_reverse(dep_map);
798 /* For a given mapping between iterations of must source k and iterations
799 * of the sink, compute all iterations of may source j preceding
800 * the sink at level before_level for any of the sink iterations,
801 * but following the corresponding iteration of must source k at level
802 * after_level.
804 static __isl_give isl_map *all_later_sources(__isl_keep isl_access_info *acc,
805 __isl_take isl_map *old_map,
806 int j, int before_level, int k, int after_level)
808 isl_space *dim;
809 isl_set *set_C;
810 isl_map *read_map;
811 isl_map *write_map;
812 isl_map *dep_map;
813 isl_map *after_write;
814 isl_map *before_read;
816 set_C = isl_map_range(isl_map_copy(old_map));
817 read_map = isl_map_copy(acc->sink.map);
818 read_map = isl_map_intersect_domain(read_map, set_C);
819 write_map = isl_map_copy(acc->source[acc->n_must + j].map);
821 write_map = isl_map_reverse(write_map);
822 dep_map = isl_map_apply_range(read_map, write_map);
823 dim = isl_space_join(isl_map_get_space(acc->source[acc->n_must + j].map),
824 isl_space_reverse(isl_map_get_space(acc->source[k].map)));
825 after_write = after_at_level(dim, after_level);
826 after_write = isl_map_apply_range(after_write, old_map);
827 after_write = isl_map_reverse(after_write);
828 dep_map = isl_map_intersect(dep_map, after_write);
829 before_read = after_at_level(isl_map_get_space(dep_map), before_level);
830 dep_map = isl_map_intersect(dep_map, before_read);
831 return isl_map_reverse(dep_map);
834 /* Given the must and may dependence relations for the must accesses
835 * for level sink_level, check if there are any accesses of may access j
836 * that occur in between and return their union.
837 * If some of these accesses are intermediate with respect to
838 * (previously thought to be) must dependences, then these
839 * must dependences are turned into may dependences.
841 static __isl_give isl_map *all_intermediate_sources(
842 __isl_keep isl_access_info *acc, __isl_take isl_map *map,
843 struct isl_map **must_rel, struct isl_map **may_rel,
844 int j, int sink_level)
846 int k, level;
847 int depth = 2 * isl_map_dim(acc->source[acc->n_must + j].map,
848 isl_dim_in) + 1;
850 for (k = 0; k < acc->n_must; ++k) {
851 int plevel;
853 if (isl_map_plain_is_empty(may_rel[k]) &&
854 isl_map_plain_is_empty(must_rel[k]))
855 continue;
857 plevel = acc->level_before(acc->source[k].data,
858 acc->source[acc->n_must + j].data);
859 if (plevel < 0)
860 return isl_map_free(map);
862 for (level = sink_level; level <= depth; ++level) {
863 isl_map *T;
864 isl_map *copy;
865 isl_set *ran;
867 if (!can_precede_at_level(plevel, level))
868 continue;
870 copy = isl_map_copy(may_rel[k]);
871 T = all_later_sources(acc, copy, j, sink_level, k, level);
872 map = isl_map_union(map, T);
874 copy = isl_map_copy(must_rel[k]);
875 T = all_later_sources(acc, copy, j, sink_level, k, level);
876 ran = isl_map_range(isl_map_copy(T));
877 map = isl_map_union(map, T);
878 may_rel[k] = isl_map_union_disjoint(may_rel[k],
879 isl_map_intersect_range(isl_map_copy(must_rel[k]),
880 isl_set_copy(ran)));
881 T = isl_map_from_domain_and_range(
882 isl_set_universe(
883 isl_space_domain(isl_map_get_space(must_rel[k]))),
884 ran);
885 must_rel[k] = isl_map_subtract(must_rel[k], T);
889 return map;
892 /* Given a dependence relation "old_map" between a must-source and the sink,
893 * return a subset of the dependences, augmented with instances
894 * of the source at position "pos" in "acc" that are coscheduled
895 * with the must-source and that access the same element.
896 * That is, if the input lives in a space T -> K, then the output
897 * lives in the space [T -> S] -> K, with S the space of source "pos", and
898 * the domain factor of the domain product is a subset of the input.
899 * The sources are considered to be coscheduled if they have the same values
900 * for the initial "depth" coordinates.
902 * First construct a dependence relation S -> K and a mapping
903 * between coscheduled sources T -> S.
904 * The second is combined with the original dependence relation T -> K
905 * to form a relation in T -> [S -> K], which is subsequently
906 * uncurried to [T -> S] -> K.
907 * This result is then intersected with the dependence relation S -> K
908 * to form the output.
910 * In case a negative depth is given, NULL is returned to indicate an error.
912 static __isl_give isl_map *coscheduled_source(__isl_keep isl_access_info *acc,
913 __isl_keep isl_map *old_map, int pos, int depth)
915 isl_space *space;
916 isl_set *set_C;
917 isl_map *read_map;
918 isl_map *write_map;
919 isl_map *dep_map;
920 isl_map *equal;
921 isl_map *map;
923 if (depth < 0)
924 return NULL;
926 set_C = isl_map_range(isl_map_copy(old_map));
927 read_map = isl_map_copy(acc->sink.map);
928 read_map = isl_map_intersect_domain(read_map, set_C);
929 write_map = isl_map_copy(acc->source[pos].map);
930 dep_map = isl_map_domain_product(write_map, read_map);
931 dep_map = isl_set_unwrap(isl_map_domain(dep_map));
932 space = isl_space_join(isl_map_get_space(old_map),
933 isl_space_reverse(isl_map_get_space(dep_map)));
934 equal = isl_map_from_basic_map(isl_basic_map_equal(space, depth));
935 map = isl_map_range_product(equal, isl_map_copy(old_map));
936 map = isl_map_uncurry(map);
937 map = isl_map_intersect_domain_factor_range(map, dep_map);
939 return map;
942 /* After the dependences derived from a must-source have been computed
943 * at a certain level, check if any of the sources of the must-dependences
944 * may be coscheduled with other sources.
945 * If they are any such sources, then there is no way of determining
946 * which of the sources actually comes last and the must-dependences
947 * need to be turned into may-dependences, while dependences from
948 * the other sources need to be added to the may-dependences as well.
949 * "acc" describes the sources and a callback for checking whether
950 * two sources may be coscheduled. If acc->coscheduled is NULL then
951 * the sources are assumed not to be coscheduled.
952 * "must_rel" and "may_rel" describe the must and may-dependence relations
953 * computed at the current level for the must-sources. Some of the dependences
954 * may be moved from "must_rel" to "may_rel".
955 * "flow" contains all dependences computed so far (apart from those
956 * in "must_rel" and "may_rel") and may be updated with additional
957 * dependences derived from may-sources.
959 * In particular, consider all the must-sources with a non-empty
960 * dependence relation in "must_rel". They are considered in reverse
961 * order because that is the order in which they are considered in the caller.
962 * If any of the must-sources are coscheduled, then the last one
963 * is the one that will have a corresponding dependence relation.
964 * For each must-source i, consider both all the previous must-sources
965 * and all the may-sources. If any of those may be coscheduled with
966 * must-source i, then compute the coscheduled instances that access
967 * the same memory elements. The result is a relation [T -> S] -> K.
968 * The projection onto T -> K is a subset of the must-dependence relation
969 * that needs to be turned into may-dependences.
970 * The projection onto S -> K needs to be added to the may-dependences
971 * of source S.
972 * Since a given must-source instance may be coscheduled with several
973 * other source instances, the dependences that need to be turned
974 * into may-dependences are first collected and only actually removed
975 * from the must-dependences after all other sources have been considered.
977 static __isl_give isl_flow *handle_coscheduled(__isl_keep isl_access_info *acc,
978 __isl_keep isl_map **must_rel, __isl_keep isl_map **may_rel,
979 __isl_take isl_flow *flow)
981 int i, j;
983 if (!acc->coscheduled)
984 return flow;
985 for (i = acc->n_must - 1; i >= 0; --i) {
986 isl_map *move;
988 if (isl_map_plain_is_empty(must_rel[i]))
989 continue;
990 move = isl_map_empty(isl_map_get_space(must_rel[i]));
991 for (j = i - 1; j >= 0; --j) {
992 int depth;
993 isl_map *map, *factor;
995 if (!acc->coscheduled(acc->source[i].data,
996 acc->source[j].data))
997 continue;
998 depth = acc->level_before(acc->source[i].data,
999 acc->source[j].data) / 2;
1000 map = coscheduled_source(acc, must_rel[i], j, depth);
1001 factor = isl_map_domain_factor_range(isl_map_copy(map));
1002 may_rel[j] = isl_map_union(may_rel[j], factor);
1003 map = isl_map_domain_factor_domain(map);
1004 move = isl_map_union(move, map);
1006 for (j = 0; j < acc->n_may; ++j) {
1007 int depth, pos;
1008 isl_map *map, *factor;
1010 pos = acc->n_must + j;
1011 if (!acc->coscheduled(acc->source[i].data,
1012 acc->source[pos].data))
1013 continue;
1014 depth = acc->level_before(acc->source[i].data,
1015 acc->source[pos].data) / 2;
1016 map = coscheduled_source(acc, must_rel[i], pos, depth);
1017 factor = isl_map_domain_factor_range(isl_map_copy(map));
1018 pos = 2 * acc->n_must + j;
1019 flow->dep[pos].map = isl_map_union(flow->dep[pos].map,
1020 factor);
1021 map = isl_map_domain_factor_domain(map);
1022 move = isl_map_union(move, map);
1024 must_rel[i] = isl_map_subtract(must_rel[i], isl_map_copy(move));
1025 may_rel[i] = isl_map_union(may_rel[i], move);
1028 return flow;
1031 /* Compute dependences for the case where all accesses are "may"
1032 * accesses, which boils down to computing memory based dependences.
1033 * The generic algorithm would also work in this case, but it would
1034 * be overkill to use it.
1036 static __isl_give isl_flow *compute_mem_based_dependences(
1037 __isl_keep isl_access_info *acc)
1039 int i;
1040 isl_set *mustdo;
1041 isl_set *maydo;
1042 isl_flow *res;
1044 res = isl_flow_alloc(acc);
1045 if (!res)
1046 return NULL;
1048 mustdo = isl_map_domain(isl_map_copy(acc->sink.map));
1049 maydo = isl_set_copy(mustdo);
1051 for (i = 0; i < acc->n_may; ++i) {
1052 int plevel;
1053 int is_before;
1054 isl_space *dim;
1055 isl_map *before;
1056 isl_map *dep;
1058 plevel = acc->level_before(acc->source[i].data, acc->sink.data);
1059 if (plevel < 0)
1060 goto error;
1062 is_before = plevel & 1;
1063 plevel >>= 1;
1065 dim = isl_map_get_space(res->dep[i].map);
1066 if (is_before)
1067 before = isl_map_lex_le_first(dim, plevel);
1068 else
1069 before = isl_map_lex_lt_first(dim, plevel);
1070 dep = isl_map_apply_range(isl_map_copy(acc->source[i].map),
1071 isl_map_reverse(isl_map_copy(acc->sink.map)));
1072 dep = isl_map_intersect(dep, before);
1073 mustdo = isl_set_subtract(mustdo,
1074 isl_map_range(isl_map_copy(dep)));
1075 res->dep[i].map = isl_map_union(res->dep[i].map, dep);
1078 res->may_no_source = isl_set_subtract(maydo, isl_set_copy(mustdo));
1079 res->must_no_source = mustdo;
1081 return res;
1082 error:
1083 isl_set_free(mustdo);
1084 isl_set_free(maydo);
1085 isl_flow_free(res);
1086 return NULL;
1089 /* Compute dependences for the case where there is at least one
1090 * "must" access.
1092 * The core algorithm considers all levels in which a source may precede
1093 * the sink, where a level may either be a statement level or a loop level.
1094 * The outermost statement level is 1, the first loop level is 2, etc...
1095 * The algorithm basically does the following:
1096 * for all levels l of the read access from innermost to outermost
1097 * for all sources w that may precede the sink access at that level
1098 * compute the last iteration of the source that precedes the sink access
1099 * at that level
1100 * add result to possible last accesses at level l of source w
1101 * for all sources w2 that we haven't considered yet at this level that may
1102 * also precede the sink access
1103 * for all levels l2 of w from l to innermost
1104 * for all possible last accesses dep of w at l
1105 * compute last iteration of w2 between the source and sink
1106 * of dep
1107 * add result to possible last accesses at level l of write w2
1108 * and replace possible last accesses dep by the remainder
1111 * The above algorithm is applied to the must access. During the course
1112 * of the algorithm, we keep track of sink iterations that still
1113 * need to be considered. These iterations are split into those that
1114 * haven't been matched to any source access (mustdo) and those that have only
1115 * been matched to may accesses (maydo).
1116 * At the end of each level, must-sources and may-sources that are coscheduled
1117 * with the sources of the must-dependences at that level are considered.
1118 * If any coscheduled instances are found, then corresponding may-dependences
1119 * are added and the original must-dependences are turned into may-dependences.
1120 * Afterwards, the may accesses that occur after must-dependence sources
1121 * are considered.
1122 * In particular, we consider may accesses that precede the remaining
1123 * sink iterations, moving elements from mustdo to maydo when appropriate,
1124 * and may accesses that occur between a must source and a sink of any
1125 * dependences found at the current level, turning must dependences into
1126 * may dependences when appropriate.
1129 static __isl_give isl_flow *compute_val_based_dependences(
1130 __isl_keep isl_access_info *acc)
1132 isl_ctx *ctx;
1133 isl_flow *res;
1134 isl_set *mustdo = NULL;
1135 isl_set *maydo = NULL;
1136 int level, j;
1137 int depth;
1138 isl_map **must_rel = NULL;
1139 isl_map **may_rel = NULL;
1141 if (!acc)
1142 return NULL;
1144 res = isl_flow_alloc(acc);
1145 if (!res)
1146 goto error;
1147 ctx = isl_map_get_ctx(acc->sink.map);
1149 depth = 2 * isl_map_dim(acc->sink.map, isl_dim_in) + 1;
1150 mustdo = isl_map_domain(isl_map_copy(acc->sink.map));
1151 maydo = isl_set_empty(isl_set_get_space(mustdo));
1152 if (!mustdo || !maydo)
1153 goto error;
1154 if (isl_set_plain_is_empty(mustdo))
1155 goto done;
1157 must_rel = isl_calloc_array(ctx, struct isl_map *, acc->n_must);
1158 may_rel = isl_calloc_array(ctx, struct isl_map *, acc->n_must);
1159 if (!must_rel || !may_rel)
1160 goto error;
1162 for (level = depth; level >= 1; --level) {
1163 for (j = acc->n_must-1; j >=0; --j) {
1164 isl_space *space;
1165 space = isl_map_get_space(res->dep[2 * j].map);
1166 must_rel[j] = isl_map_empty(space);
1167 may_rel[j] = isl_map_copy(must_rel[j]);
1170 for (j = acc->n_must - 1; j >= 0; --j) {
1171 struct isl_map *T;
1172 struct isl_set *rest;
1173 int plevel;
1175 plevel = acc->level_before(acc->source[j].data,
1176 acc->sink.data);
1177 if (plevel < 0)
1178 goto error;
1179 if (!can_precede_at_level(plevel, level))
1180 continue;
1182 T = last_source(acc, mustdo, j, level, &rest);
1183 must_rel[j] = isl_map_union_disjoint(must_rel[j], T);
1184 mustdo = rest;
1186 if (intermediate_sources(acc, must_rel, j, level) < 0)
1187 goto error;
1189 T = last_source(acc, maydo, j, level, &rest);
1190 may_rel[j] = isl_map_union_disjoint(may_rel[j], T);
1191 maydo = rest;
1193 if (intermediate_sources(acc, may_rel, j, level) < 0)
1194 goto error;
1196 if (isl_set_plain_is_empty(mustdo) &&
1197 isl_set_plain_is_empty(maydo))
1198 break;
1200 for (j = j - 1; j >= 0; --j) {
1201 int plevel;
1203 plevel = acc->level_before(acc->source[j].data,
1204 acc->sink.data);
1205 if (plevel < 0)
1206 goto error;
1207 if (!can_precede_at_level(plevel, level))
1208 continue;
1210 if (intermediate_sources(acc, must_rel, j, level) < 0)
1211 goto error;
1212 if (intermediate_sources(acc, may_rel, j, level) < 0)
1213 goto error;
1216 handle_coscheduled(acc, must_rel, may_rel, res);
1218 for (j = 0; j < acc->n_may; ++j) {
1219 int plevel;
1220 isl_map *T;
1221 isl_set *ran;
1223 plevel = acc->level_before(acc->source[acc->n_must + j].data,
1224 acc->sink.data);
1225 if (plevel < 0)
1226 goto error;
1227 if (!can_precede_at_level(plevel, level))
1228 continue;
1230 T = all_sources(acc, isl_set_copy(maydo), j, level);
1231 res->dep[2 * acc->n_must + j].map =
1232 isl_map_union(res->dep[2 * acc->n_must + j].map, T);
1233 T = all_sources(acc, isl_set_copy(mustdo), j, level);
1234 ran = isl_map_range(isl_map_copy(T));
1235 res->dep[2 * acc->n_must + j].map =
1236 isl_map_union(res->dep[2 * acc->n_must + j].map, T);
1237 mustdo = isl_set_subtract(mustdo, isl_set_copy(ran));
1238 maydo = isl_set_union_disjoint(maydo, ran);
1240 T = res->dep[2 * acc->n_must + j].map;
1241 T = all_intermediate_sources(acc, T, must_rel, may_rel,
1242 j, level);
1243 res->dep[2 * acc->n_must + j].map = T;
1246 for (j = acc->n_must - 1; j >= 0; --j) {
1247 res->dep[2 * j].map =
1248 isl_map_union_disjoint(res->dep[2 * j].map,
1249 must_rel[j]);
1250 res->dep[2 * j + 1].map =
1251 isl_map_union_disjoint(res->dep[2 * j + 1].map,
1252 may_rel[j]);
1255 if (isl_set_plain_is_empty(mustdo) &&
1256 isl_set_plain_is_empty(maydo))
1257 break;
1260 free(must_rel);
1261 free(may_rel);
1262 done:
1263 res->must_no_source = mustdo;
1264 res->may_no_source = maydo;
1265 return res;
1266 error:
1267 if (must_rel)
1268 for (j = 0; j < acc->n_must; ++j)
1269 isl_map_free(must_rel[j]);
1270 if (may_rel)
1271 for (j = 0; j < acc->n_must; ++j)
1272 isl_map_free(may_rel[j]);
1273 isl_flow_free(res);
1274 isl_set_free(mustdo);
1275 isl_set_free(maydo);
1276 free(must_rel);
1277 free(may_rel);
1278 return NULL;
1281 /* Given a "sink" access, a list of n "source" accesses,
1282 * compute for each iteration of the sink access
1283 * and for each element accessed by that iteration,
1284 * the source access in the list that last accessed the
1285 * element accessed by the sink access before this sink access.
1286 * Each access is given as a map from the loop iterators
1287 * to the array indices.
1288 * The result is a list of n relations between source and sink
1289 * iterations and a subset of the domain of the sink access,
1290 * corresponding to those iterations that access an element
1291 * not previously accessed.
1293 * To deal with multi-valued sink access relations, the sink iteration
1294 * domain is first extended with dimensions that correspond to the data
1295 * space. However, these extra dimensions are not projected out again.
1296 * It is up to the caller to decide whether these dimensions should be kept.
1298 static __isl_give isl_flow *access_info_compute_flow_core(
1299 __isl_take isl_access_info *acc)
1301 struct isl_flow *res = NULL;
1303 if (!acc)
1304 return NULL;
1306 acc->sink.map = isl_map_range_map(acc->sink.map);
1307 if (!acc->sink.map)
1308 goto error;
1310 if (acc->n_must == 0)
1311 res = compute_mem_based_dependences(acc);
1312 else {
1313 acc = isl_access_info_sort_sources(acc);
1314 res = compute_val_based_dependences(acc);
1316 acc = isl_access_info_free(acc);
1317 if (!res)
1318 return NULL;
1319 if (!res->must_no_source || !res->may_no_source)
1320 goto error;
1321 return res;
1322 error:
1323 isl_access_info_free(acc);
1324 isl_flow_free(res);
1325 return NULL;
1328 /* Given a "sink" access, a list of n "source" accesses,
1329 * compute for each iteration of the sink access
1330 * and for each element accessed by that iteration,
1331 * the source access in the list that last accessed the
1332 * element accessed by the sink access before this sink access.
1333 * Each access is given as a map from the loop iterators
1334 * to the array indices.
1335 * The result is a list of n relations between source and sink
1336 * iterations and a subset of the domain of the sink access,
1337 * corresponding to those iterations that access an element
1338 * not previously accessed.
1340 * To deal with multi-valued sink access relations,
1341 * access_info_compute_flow_core extends the sink iteration domain
1342 * with dimensions that correspond to the data space. These extra dimensions
1343 * are projected out from the result of access_info_compute_flow_core.
1345 __isl_give isl_flow *isl_access_info_compute_flow(__isl_take isl_access_info *acc)
1347 int j;
1348 struct isl_flow *res;
1350 if (!acc)
1351 return NULL;
1353 acc->domain_map = isl_map_domain_map(isl_map_copy(acc->sink.map));
1354 res = access_info_compute_flow_core(acc);
1355 if (!res)
1356 return NULL;
1358 for (j = 0; j < res->n_source; ++j) {
1359 res->dep[j].map = isl_map_range_factor_domain(res->dep[j].map);
1360 if (!res->dep[j].map)
1361 goto error;
1364 return res;
1365 error:
1366 isl_flow_free(res);
1367 return NULL;
1371 /* Keep track of some information about a schedule for a given
1372 * access. In particular, keep track of which dimensions
1373 * have a constant value and of the actual constant values.
1375 struct isl_sched_info {
1376 int *is_cst;
1377 isl_vec *cst;
1380 static void sched_info_free(__isl_take struct isl_sched_info *info)
1382 if (!info)
1383 return;
1384 isl_vec_free(info->cst);
1385 free(info->is_cst);
1386 free(info);
1389 /* Extract information on the constant dimensions of the schedule
1390 * for a given access. The "map" is of the form
1392 * [S -> D] -> A
1394 * with S the schedule domain, D the iteration domain and A the data domain.
1396 static __isl_give struct isl_sched_info *sched_info_alloc(
1397 __isl_keep isl_map *map)
1399 isl_ctx *ctx;
1400 isl_space *space;
1401 struct isl_sched_info *info;
1402 int i, n;
1404 if (!map)
1405 return NULL;
1407 space = isl_space_unwrap(isl_space_domain(isl_map_get_space(map)));
1408 if (!space)
1409 return NULL;
1410 n = isl_space_dim(space, isl_dim_in);
1411 isl_space_free(space);
1413 ctx = isl_map_get_ctx(map);
1414 info = isl_alloc_type(ctx, struct isl_sched_info);
1415 if (!info)
1416 return NULL;
1417 info->is_cst = isl_alloc_array(ctx, int, n);
1418 info->cst = isl_vec_alloc(ctx, n);
1419 if (n && (!info->is_cst || !info->cst))
1420 goto error;
1422 for (i = 0; i < n; ++i) {
1423 isl_val *v;
1425 v = isl_map_plain_get_val_if_fixed(map, isl_dim_in, i);
1426 if (!v)
1427 goto error;
1428 info->is_cst[i] = !isl_val_is_nan(v);
1429 if (info->is_cst[i])
1430 info->cst = isl_vec_set_element_val(info->cst, i, v);
1431 else
1432 isl_val_free(v);
1435 return info;
1436 error:
1437 sched_info_free(info);
1438 return NULL;
1441 /* The different types of access relations that isl_union_access_info
1442 * keeps track of.
1444 * "isl_access_sink" represents the sink accesses.
1445 * "isl_access_must_source" represents the definite source accesses.
1446 * "isl_access_may_source" represents the possible source accesses.
1447 * "isl_access_kill" represents the kills.
1449 * isl_access_sink is sometimes treated differently and
1450 * should therefore appear first.
1452 enum isl_access_type {
1453 isl_access_sink,
1454 isl_access_must_source,
1455 isl_access_may_source,
1456 isl_access_kill,
1457 isl_access_end
1460 /* This structure represents the input for a dependence analysis computation.
1462 * "access" contains the access relations.
1464 * "schedule" or "schedule_map" represents the execution order.
1465 * Exactly one of these fields should be NULL. The other field
1466 * determines the execution order.
1468 * The domains of these four maps refer to the same iteration spaces(s).
1469 * The ranges of the first three maps also refer to the same data space(s).
1471 * After a call to isl_union_access_info_introduce_schedule,
1472 * the "schedule_map" field no longer contains useful information.
1474 struct isl_union_access_info {
1475 isl_union_map *access[isl_access_end];
1477 isl_schedule *schedule;
1478 isl_union_map *schedule_map;
1481 /* Free "access" and return NULL.
1483 __isl_null isl_union_access_info *isl_union_access_info_free(
1484 __isl_take isl_union_access_info *access)
1486 enum isl_access_type i;
1488 if (!access)
1489 return NULL;
1491 for (i = isl_access_sink; i < isl_access_end; ++i)
1492 isl_union_map_free(access->access[i]);
1493 isl_schedule_free(access->schedule);
1494 isl_union_map_free(access->schedule_map);
1495 free(access);
1497 return NULL;
1500 /* Return the isl_ctx to which "access" belongs.
1502 isl_ctx *isl_union_access_info_get_ctx(__isl_keep isl_union_access_info *access)
1504 if (!access)
1505 return NULL;
1506 return isl_union_map_get_ctx(access->access[isl_access_sink]);
1509 /* Construct an empty (invalid) isl_union_access_info object.
1510 * The caller is responsible for setting the sink access relation and
1511 * initializing all the other fields, e.g., by calling
1512 * isl_union_access_info_init.
1514 static __isl_give isl_union_access_info *isl_union_access_info_alloc(
1515 isl_ctx *ctx)
1517 return isl_calloc_type(ctx, isl_union_access_info);
1520 /* Initialize all the fields of "info", except the sink access relation,
1521 * which is assumed to have been set by the caller.
1523 * By default, we use the schedule field of the isl_union_access_info,
1524 * but this may be overridden by a call
1525 * to isl_union_access_info_set_schedule_map.
1527 static __isl_give isl_union_access_info *isl_union_access_info_init(
1528 __isl_take isl_union_access_info *info)
1530 isl_space *space;
1531 isl_union_map *empty;
1532 enum isl_access_type i;
1534 if (!info)
1535 return NULL;
1536 if (!info->access[isl_access_sink])
1537 return isl_union_access_info_free(info);
1539 space = isl_union_map_get_space(info->access[isl_access_sink]);
1540 empty = isl_union_map_empty(isl_space_copy(space));
1541 for (i = isl_access_sink + 1; i < isl_access_end; ++i)
1542 if (!info->access[i])
1543 info->access[i] = isl_union_map_copy(empty);
1544 isl_union_map_free(empty);
1545 if (!info->schedule && !info->schedule_map)
1546 info->schedule = isl_schedule_empty(isl_space_copy(space));
1547 isl_space_free(space);
1549 for (i = isl_access_sink + 1; i < isl_access_end; ++i)
1550 if (!info->access[i])
1551 return isl_union_access_info_free(info);
1552 if (!info->schedule && !info->schedule_map)
1553 return isl_union_access_info_free(info);
1555 return info;
1558 /* Create a new isl_union_access_info with the given sink accesses and
1559 * and no other accesses or schedule information.
1561 __isl_give isl_union_access_info *isl_union_access_info_from_sink(
1562 __isl_take isl_union_map *sink)
1564 isl_ctx *ctx;
1565 isl_union_access_info *access;
1567 if (!sink)
1568 return NULL;
1569 ctx = isl_union_map_get_ctx(sink);
1570 access = isl_union_access_info_alloc(ctx);
1571 if (!access)
1572 goto error;
1573 access->access[isl_access_sink] = sink;
1574 return isl_union_access_info_init(access);
1575 error:
1576 isl_union_map_free(sink);
1577 return NULL;
1580 /* Replace the access relation of type "type" of "info" by "access".
1582 static __isl_give isl_union_access_info *isl_union_access_info_set(
1583 __isl_take isl_union_access_info *info,
1584 enum isl_access_type type, __isl_take isl_union_map *access)
1586 if (!info || !access)
1587 goto error;
1589 isl_union_map_free(info->access[type]);
1590 info->access[type] = access;
1592 return info;
1593 error:
1594 isl_union_access_info_free(info);
1595 isl_union_map_free(access);
1596 return NULL;
1599 /* Replace the definite source accesses of "access" by "must_source".
1601 __isl_give isl_union_access_info *isl_union_access_info_set_must_source(
1602 __isl_take isl_union_access_info *access,
1603 __isl_take isl_union_map *must_source)
1605 return isl_union_access_info_set(access, isl_access_must_source,
1606 must_source);
1609 /* Replace the possible source accesses of "access" by "may_source".
1611 __isl_give isl_union_access_info *isl_union_access_info_set_may_source(
1612 __isl_take isl_union_access_info *access,
1613 __isl_take isl_union_map *may_source)
1615 return isl_union_access_info_set(access, isl_access_may_source,
1616 may_source);
1619 /* Replace the kills of "info" by "kill".
1621 __isl_give isl_union_access_info *isl_union_access_info_set_kill(
1622 __isl_take isl_union_access_info *info, __isl_take isl_union_map *kill)
1624 return isl_union_access_info_set(info, isl_access_kill, kill);
1627 /* Return the access relation of type "type" of "info".
1629 static __isl_give isl_union_map *isl_union_access_info_get(
1630 __isl_keep isl_union_access_info *info, enum isl_access_type type)
1632 if (!info)
1633 return NULL;
1634 return isl_union_map_copy(info->access[type]);
1637 /* Return the definite source accesses of "info".
1639 __isl_give isl_union_map *isl_union_access_info_get_must_source(
1640 __isl_keep isl_union_access_info *info)
1642 return isl_union_access_info_get(info, isl_access_must_source);
1645 /* Return the possible source accesses of "info".
1647 __isl_give isl_union_map *isl_union_access_info_get_may_source(
1648 __isl_keep isl_union_access_info *info)
1650 return isl_union_access_info_get(info, isl_access_may_source);
1653 /* Return the kills of "info".
1655 __isl_give isl_union_map *isl_union_access_info_get_kill(
1656 __isl_keep isl_union_access_info *info)
1658 return isl_union_access_info_get(info, isl_access_kill);
1661 /* Does "info" specify any kills?
1663 static isl_bool isl_union_access_has_kill(
1664 __isl_keep isl_union_access_info *info)
1666 isl_bool empty;
1668 if (!info)
1669 return isl_bool_error;
1670 empty = isl_union_map_is_empty(info->access[isl_access_kill]);
1671 return isl_bool_not(empty);
1674 /* Replace the schedule of "access" by "schedule".
1675 * Also free the schedule_map in case it was set last.
1677 __isl_give isl_union_access_info *isl_union_access_info_set_schedule(
1678 __isl_take isl_union_access_info *access,
1679 __isl_take isl_schedule *schedule)
1681 if (!access || !schedule)
1682 goto error;
1684 access->schedule_map = isl_union_map_free(access->schedule_map);
1685 isl_schedule_free(access->schedule);
1686 access->schedule = schedule;
1688 return access;
1689 error:
1690 isl_union_access_info_free(access);
1691 isl_schedule_free(schedule);
1692 return NULL;
1695 /* Replace the schedule map of "access" by "schedule_map".
1696 * Also free the schedule in case it was set last.
1698 __isl_give isl_union_access_info *isl_union_access_info_set_schedule_map(
1699 __isl_take isl_union_access_info *access,
1700 __isl_take isl_union_map *schedule_map)
1702 if (!access || !schedule_map)
1703 goto error;
1705 isl_union_map_free(access->schedule_map);
1706 access->schedule = isl_schedule_free(access->schedule);
1707 access->schedule_map = schedule_map;
1709 return access;
1710 error:
1711 isl_union_access_info_free(access);
1712 isl_union_map_free(schedule_map);
1713 return NULL;
1716 __isl_give isl_union_access_info *isl_union_access_info_copy(
1717 __isl_keep isl_union_access_info *access)
1719 isl_union_access_info *copy;
1720 enum isl_access_type i;
1722 if (!access)
1723 return NULL;
1724 copy = isl_union_access_info_from_sink(
1725 isl_union_map_copy(access->access[isl_access_sink]));
1726 for (i = isl_access_sink + 1; i < isl_access_end; ++i)
1727 copy = isl_union_access_info_set(copy, i,
1728 isl_union_map_copy(access->access[i]));
1729 if (access->schedule)
1730 copy = isl_union_access_info_set_schedule(copy,
1731 isl_schedule_copy(access->schedule));
1732 else
1733 copy = isl_union_access_info_set_schedule_map(copy,
1734 isl_union_map_copy(access->schedule_map));
1736 return copy;
1739 /* Print a key-value pair of a YAML mapping to "p",
1740 * with key "name" and value "umap".
1742 static __isl_give isl_printer *print_union_map_field(__isl_take isl_printer *p,
1743 const char *name, __isl_keep isl_union_map *umap)
1745 p = isl_printer_print_str(p, name);
1746 p = isl_printer_yaml_next(p);
1747 p = isl_printer_print_str(p, "\"");
1748 p = isl_printer_print_union_map(p, umap);
1749 p = isl_printer_print_str(p, "\"");
1750 p = isl_printer_yaml_next(p);
1752 return p;
1755 /* An enumeration of the various keys that may appear in a YAML mapping
1756 * of an isl_union_access_info object.
1757 * The keys for the access relation types are assumed to have the same values
1758 * as the access relation types in isl_access_type.
1760 enum isl_ai_key {
1761 isl_ai_key_error = -1,
1762 isl_ai_key_sink = isl_access_sink,
1763 isl_ai_key_must_source = isl_access_must_source,
1764 isl_ai_key_may_source = isl_access_may_source,
1765 isl_ai_key_kill = isl_access_kill,
1766 isl_ai_key_schedule_map,
1767 isl_ai_key_schedule,
1768 isl_ai_key_end
1771 /* Textual representations of the YAML keys for an isl_union_access_info
1772 * object.
1774 static char *key_str[] = {
1775 [isl_ai_key_sink] = "sink",
1776 [isl_ai_key_must_source] = "must_source",
1777 [isl_ai_key_may_source] = "may_source",
1778 [isl_ai_key_kill] = "kill",
1779 [isl_ai_key_schedule_map] = "schedule_map",
1780 [isl_ai_key_schedule] = "schedule",
1783 /* Print a key-value pair corresponding to the access relation of type "type"
1784 * of a YAML mapping of "info" to "p".
1786 * The sink access relation is always printed, but any other access relation
1787 * is only printed if it is non-empty.
1789 static __isl_give isl_printer *print_access_field(__isl_take isl_printer *p,
1790 __isl_keep isl_union_access_info *info, enum isl_access_type type)
1792 if (type != isl_access_sink) {
1793 isl_bool empty;
1795 empty = isl_union_map_is_empty(info->access[type]);
1796 if (empty < 0)
1797 return isl_printer_free(p);
1798 if (empty)
1799 return p;
1801 return print_union_map_field(p, key_str[type], info->access[type]);
1804 /* Print the information contained in "access" to "p".
1805 * The information is printed as a YAML document.
1807 __isl_give isl_printer *isl_printer_print_union_access_info(
1808 __isl_take isl_printer *p, __isl_keep isl_union_access_info *access)
1810 enum isl_access_type i;
1812 if (!access)
1813 return isl_printer_free(p);
1815 p = isl_printer_yaml_start_mapping(p);
1816 for (i = isl_access_sink; i < isl_access_end; ++i)
1817 p = print_access_field(p, access, i);
1818 if (access->schedule) {
1819 p = isl_printer_print_str(p, key_str[isl_ai_key_schedule]);
1820 p = isl_printer_yaml_next(p);
1821 p = isl_printer_print_schedule(p, access->schedule);
1822 p = isl_printer_yaml_next(p);
1823 } else {
1824 p = print_union_map_field(p, key_str[isl_ai_key_schedule_map],
1825 access->schedule_map);
1827 p = isl_printer_yaml_end_mapping(p);
1829 return p;
1832 /* Return a string representation of the information in "access".
1833 * The information is printed in flow format.
1835 __isl_give char *isl_union_access_info_to_str(
1836 __isl_keep isl_union_access_info *access)
1838 isl_printer *p;
1839 char *s;
1841 if (!access)
1842 return NULL;
1844 p = isl_printer_to_str(isl_union_access_info_get_ctx(access));
1845 p = isl_printer_set_yaml_style(p, ISL_YAML_STYLE_FLOW);
1846 p = isl_printer_print_union_access_info(p, access);
1847 s = isl_printer_get_str(p);
1848 isl_printer_free(p);
1850 return s;
1853 #undef KEY
1854 #define KEY enum isl_ai_key
1855 #undef KEY_ERROR
1856 #define KEY_ERROR isl_ai_key_error
1857 #undef KEY_END
1858 #define KEY_END isl_ai_key_end
1859 #include "extract_key.c"
1861 #undef BASE
1862 #define BASE union_map
1863 #include "read_in_string_templ.c"
1865 /* Read an isl_union_access_info object from "s".
1867 * Start off with an empty (invalid) isl_union_access_info object and
1868 * then fill up the fields based on the input.
1869 * The input needs to contain at least a description of the sink
1870 * access relation as well as some form of schedule.
1871 * The other access relations are set to empty relations
1872 * by isl_union_access_info_init if they are not specified in the input.
1874 __isl_give isl_union_access_info *isl_stream_read_union_access_info(
1875 isl_stream *s)
1877 isl_ctx *ctx;
1878 isl_union_access_info *info;
1879 int more;
1880 int sink_set = 0;
1881 int schedule_set = 0;
1883 if (isl_stream_yaml_read_start_mapping(s))
1884 return NULL;
1886 ctx = isl_stream_get_ctx(s);
1887 info = isl_union_access_info_alloc(ctx);
1888 while ((more = isl_stream_yaml_next(s)) > 0) {
1889 enum isl_ai_key key;
1890 isl_union_map *access, *schedule_map;
1891 isl_schedule *schedule;
1893 key = get_key(s);
1894 if (isl_stream_yaml_next(s) < 0)
1895 return isl_union_access_info_free(info);
1896 switch (key) {
1897 case isl_ai_key_end:
1898 case isl_ai_key_error:
1899 return isl_union_access_info_free(info);
1900 case isl_ai_key_sink:
1901 sink_set = 1;
1902 case isl_ai_key_must_source:
1903 case isl_ai_key_may_source:
1904 case isl_ai_key_kill:
1905 access = read_union_map(s);
1906 info = isl_union_access_info_set(info, key, access);
1907 if (!info)
1908 return NULL;
1909 break;
1910 case isl_ai_key_schedule_map:
1911 schedule_set = 1;
1912 schedule_map = read_union_map(s);
1913 info = isl_union_access_info_set_schedule_map(info,
1914 schedule_map);
1915 if (!info)
1916 return NULL;
1917 break;
1918 case isl_ai_key_schedule:
1919 schedule_set = 1;
1920 schedule = isl_stream_read_schedule(s);
1921 info = isl_union_access_info_set_schedule(info,
1922 schedule);
1923 if (!info)
1924 return NULL;
1925 break;
1928 if (more < 0)
1929 return isl_union_access_info_free(info);
1931 if (isl_stream_yaml_read_end_mapping(s) < 0) {
1932 isl_stream_error(s, NULL, "unexpected extra elements");
1933 return isl_union_access_info_free(info);
1936 if (!sink_set) {
1937 isl_stream_error(s, NULL, "no sink specified");
1938 return isl_union_access_info_free(info);
1941 if (!schedule_set) {
1942 isl_stream_error(s, NULL, "no schedule specified");
1943 return isl_union_access_info_free(info);
1946 return isl_union_access_info_init(info);
1949 /* Read an isl_union_access_info object from the file "input".
1951 __isl_give isl_union_access_info *isl_union_access_info_read_from_file(
1952 isl_ctx *ctx, FILE *input)
1954 isl_stream *s;
1955 isl_union_access_info *access;
1957 s = isl_stream_new_file(ctx, input);
1958 if (!s)
1959 return NULL;
1960 access = isl_stream_read_union_access_info(s);
1961 isl_stream_free(s);
1963 return access;
1966 /* Update the fields of "access" such that they all have the same parameters,
1967 * keeping in mind that the schedule_map field may be NULL and ignoring
1968 * the schedule field.
1970 static __isl_give isl_union_access_info *isl_union_access_info_align_params(
1971 __isl_take isl_union_access_info *access)
1973 isl_space *space;
1974 enum isl_access_type i;
1976 if (!access)
1977 return NULL;
1979 space = isl_union_map_get_space(access->access[isl_access_sink]);
1980 for (i = isl_access_sink + 1; i < isl_access_end; ++i)
1981 space = isl_space_align_params(space,
1982 isl_union_map_get_space(access->access[i]));
1983 if (access->schedule_map)
1984 space = isl_space_align_params(space,
1985 isl_union_map_get_space(access->schedule_map));
1986 for (i = isl_access_sink; i < isl_access_end; ++i)
1987 access->access[i] =
1988 isl_union_map_align_params(access->access[i],
1989 isl_space_copy(space));
1990 if (!access->schedule_map) {
1991 isl_space_free(space);
1992 } else {
1993 access->schedule_map =
1994 isl_union_map_align_params(access->schedule_map, space);
1995 if (!access->schedule_map)
1996 return isl_union_access_info_free(access);
1999 for (i = isl_access_sink; i < isl_access_end; ++i)
2000 if (!access->access[i])
2001 return isl_union_access_info_free(access);
2003 return access;
2006 /* Prepend the schedule dimensions to the iteration domains.
2008 * That is, if the schedule is of the form
2010 * D -> S
2012 * while the access relations are of the form
2014 * D -> A
2016 * then the updated access relations are of the form
2018 * [S -> D] -> A
2020 * The schedule map is also replaced by the map
2022 * [S -> D] -> D
2024 * that is used during the internal computation.
2025 * Neither the original schedule map nor this updated schedule map
2026 * are used after the call to this function.
2028 static __isl_give isl_union_access_info *
2029 isl_union_access_info_introduce_schedule(
2030 __isl_take isl_union_access_info *access)
2032 isl_union_map *sm;
2033 enum isl_access_type i;
2035 if (!access)
2036 return NULL;
2038 sm = isl_union_map_reverse(access->schedule_map);
2039 sm = isl_union_map_range_map(sm);
2040 for (i = isl_access_sink; i < isl_access_end; ++i)
2041 access->access[i] =
2042 isl_union_map_apply_range(isl_union_map_copy(sm),
2043 access->access[i]);
2044 access->schedule_map = sm;
2046 for (i = isl_access_sink; i < isl_access_end; ++i)
2047 if (!access->access[i])
2048 return isl_union_access_info_free(access);
2049 if (!access->schedule_map)
2050 return isl_union_access_info_free(access);
2052 return access;
2055 /* This structure represents the result of a dependence analysis computation.
2057 * "must_dep" represents the full definite dependences
2058 * "may_dep" represents the full non-definite dependences.
2059 * Both are of the form
2061 * [Source] -> [[Sink -> Data]]
2063 * (after the schedule dimensions have been projected out).
2064 * "must_no_source" represents the subset of the sink accesses for which
2065 * definitely no source was found.
2066 * "may_no_source" represents the subset of the sink accesses for which
2067 * possibly, but not definitely, no source was found.
2069 struct isl_union_flow {
2070 isl_union_map *must_dep;
2071 isl_union_map *may_dep;
2072 isl_union_map *must_no_source;
2073 isl_union_map *may_no_source;
2076 /* Return the isl_ctx to which "flow" belongs.
2078 isl_ctx *isl_union_flow_get_ctx(__isl_keep isl_union_flow *flow)
2080 return flow ? isl_union_map_get_ctx(flow->must_dep) : NULL;
2083 /* Free "flow" and return NULL.
2085 __isl_null isl_union_flow *isl_union_flow_free(__isl_take isl_union_flow *flow)
2087 if (!flow)
2088 return NULL;
2089 isl_union_map_free(flow->must_dep);
2090 isl_union_map_free(flow->may_dep);
2091 isl_union_map_free(flow->must_no_source);
2092 isl_union_map_free(flow->may_no_source);
2093 free(flow);
2094 return NULL;
2097 void isl_union_flow_dump(__isl_keep isl_union_flow *flow)
2099 if (!flow)
2100 return;
2102 fprintf(stderr, "must dependences: ");
2103 isl_union_map_dump(flow->must_dep);
2104 fprintf(stderr, "may dependences: ");
2105 isl_union_map_dump(flow->may_dep);
2106 fprintf(stderr, "must no source: ");
2107 isl_union_map_dump(flow->must_no_source);
2108 fprintf(stderr, "may no source: ");
2109 isl_union_map_dump(flow->may_no_source);
2112 /* Return the full definite dependences in "flow", with accessed elements.
2114 __isl_give isl_union_map *isl_union_flow_get_full_must_dependence(
2115 __isl_keep isl_union_flow *flow)
2117 if (!flow)
2118 return NULL;
2119 return isl_union_map_copy(flow->must_dep);
2122 /* Return the full possible dependences in "flow", including the definite
2123 * dependences, with accessed elements.
2125 __isl_give isl_union_map *isl_union_flow_get_full_may_dependence(
2126 __isl_keep isl_union_flow *flow)
2128 if (!flow)
2129 return NULL;
2130 return isl_union_map_union(isl_union_map_copy(flow->must_dep),
2131 isl_union_map_copy(flow->may_dep));
2134 /* Return the definite dependences in "flow", without the accessed elements.
2136 __isl_give isl_union_map *isl_union_flow_get_must_dependence(
2137 __isl_keep isl_union_flow *flow)
2139 isl_union_map *dep;
2141 if (!flow)
2142 return NULL;
2143 dep = isl_union_map_copy(flow->must_dep);
2144 return isl_union_map_range_factor_domain(dep);
2147 /* Return the possible dependences in "flow", including the definite
2148 * dependences, without the accessed elements.
2150 __isl_give isl_union_map *isl_union_flow_get_may_dependence(
2151 __isl_keep isl_union_flow *flow)
2153 isl_union_map *dep;
2155 if (!flow)
2156 return NULL;
2157 dep = isl_union_map_union(isl_union_map_copy(flow->must_dep),
2158 isl_union_map_copy(flow->may_dep));
2159 return isl_union_map_range_factor_domain(dep);
2162 /* Return the non-definite dependences in "flow".
2164 static __isl_give isl_union_map *isl_union_flow_get_non_must_dependence(
2165 __isl_keep isl_union_flow *flow)
2167 if (!flow)
2168 return NULL;
2169 return isl_union_map_copy(flow->may_dep);
2172 /* Return the subset of the sink accesses for which definitely
2173 * no source was found.
2175 __isl_give isl_union_map *isl_union_flow_get_must_no_source(
2176 __isl_keep isl_union_flow *flow)
2178 if (!flow)
2179 return NULL;
2180 return isl_union_map_copy(flow->must_no_source);
2183 /* Return the subset of the sink accesses for which possibly
2184 * no source was found, including those for which definitely
2185 * no source was found.
2187 __isl_give isl_union_map *isl_union_flow_get_may_no_source(
2188 __isl_keep isl_union_flow *flow)
2190 if (!flow)
2191 return NULL;
2192 return isl_union_map_union(isl_union_map_copy(flow->must_no_source),
2193 isl_union_map_copy(flow->may_no_source));
2196 /* Return the subset of the sink accesses for which possibly, but not
2197 * definitely, no source was found.
2199 static __isl_give isl_union_map *isl_union_flow_get_non_must_no_source(
2200 __isl_keep isl_union_flow *flow)
2202 if (!flow)
2203 return NULL;
2204 return isl_union_map_copy(flow->may_no_source);
2207 /* Create a new isl_union_flow object, initialized with empty
2208 * dependence relations and sink subsets.
2210 static __isl_give isl_union_flow *isl_union_flow_alloc(
2211 __isl_take isl_space *space)
2213 isl_ctx *ctx;
2214 isl_union_map *empty;
2215 isl_union_flow *flow;
2217 if (!space)
2218 return NULL;
2219 ctx = isl_space_get_ctx(space);
2220 flow = isl_alloc_type(ctx, isl_union_flow);
2221 if (!flow)
2222 goto error;
2224 empty = isl_union_map_empty(space);
2225 flow->must_dep = isl_union_map_copy(empty);
2226 flow->may_dep = isl_union_map_copy(empty);
2227 flow->must_no_source = isl_union_map_copy(empty);
2228 flow->may_no_source = empty;
2230 if (!flow->must_dep || !flow->may_dep ||
2231 !flow->must_no_source || !flow->may_no_source)
2232 return isl_union_flow_free(flow);
2234 return flow;
2235 error:
2236 isl_space_free(space);
2237 return NULL;
2240 /* Copy this isl_union_flow object.
2242 __isl_give isl_union_flow *isl_union_flow_copy(__isl_keep isl_union_flow *flow)
2244 isl_union_flow *copy;
2246 if (!flow)
2247 return NULL;
2249 copy = isl_union_flow_alloc(isl_union_map_get_space(flow->must_dep));
2251 if (!copy)
2252 return NULL;
2254 copy->must_dep = isl_union_map_union(copy->must_dep,
2255 isl_union_map_copy(flow->must_dep));
2256 copy->may_dep = isl_union_map_union(copy->may_dep,
2257 isl_union_map_copy(flow->may_dep));
2258 copy->must_no_source = isl_union_map_union(copy->must_no_source,
2259 isl_union_map_copy(flow->must_no_source));
2260 copy->may_no_source = isl_union_map_union(copy->may_no_source,
2261 isl_union_map_copy(flow->may_no_source));
2263 if (!copy->must_dep || !copy->may_dep ||
2264 !copy->must_no_source || !copy->may_no_source)
2265 return isl_union_flow_free(copy);
2267 return copy;
2270 /* Drop the schedule dimensions from the iteration domains in "flow".
2271 * In particular, the schedule dimensions have been prepended
2272 * to the iteration domains prior to the dependence analysis by
2273 * replacing the iteration domain D, by the wrapped map [S -> D].
2274 * Replace these wrapped maps by the original D.
2276 * In particular, the dependences computed by access_info_compute_flow_core
2277 * are of the form
2279 * [S -> D] -> [[S' -> D'] -> A]
2281 * The schedule dimensions are projected out by first currying the range,
2282 * resulting in
2284 * [S -> D] -> [S' -> [D' -> A]]
2286 * and then computing the factor range
2288 * D -> [D' -> A]
2290 static __isl_give isl_union_flow *isl_union_flow_drop_schedule(
2291 __isl_take isl_union_flow *flow)
2293 if (!flow)
2294 return NULL;
2296 flow->must_dep = isl_union_map_range_curry(flow->must_dep);
2297 flow->must_dep = isl_union_map_factor_range(flow->must_dep);
2298 flow->may_dep = isl_union_map_range_curry(flow->may_dep);
2299 flow->may_dep = isl_union_map_factor_range(flow->may_dep);
2300 flow->must_no_source =
2301 isl_union_map_domain_factor_range(flow->must_no_source);
2302 flow->may_no_source =
2303 isl_union_map_domain_factor_range(flow->may_no_source);
2305 if (!flow->must_dep || !flow->may_dep ||
2306 !flow->must_no_source || !flow->may_no_source)
2307 return isl_union_flow_free(flow);
2309 return flow;
2312 struct isl_compute_flow_data {
2313 isl_union_map *must_source;
2314 isl_union_map *may_source;
2315 isl_union_flow *flow;
2317 int count;
2318 int must;
2319 isl_space *dim;
2320 struct isl_sched_info *sink_info;
2321 struct isl_sched_info **source_info;
2322 isl_access_info *accesses;
2325 static isl_stat count_matching_array(__isl_take isl_map *map, void *user)
2327 int eq;
2328 isl_space *dim;
2329 struct isl_compute_flow_data *data;
2331 data = (struct isl_compute_flow_data *)user;
2333 dim = isl_space_range(isl_map_get_space(map));
2335 eq = isl_space_is_equal(dim, data->dim);
2337 isl_space_free(dim);
2338 isl_map_free(map);
2340 if (eq < 0)
2341 return isl_stat_error;
2342 if (eq)
2343 data->count++;
2345 return isl_stat_ok;
2348 static isl_stat collect_matching_array(__isl_take isl_map *map, void *user)
2350 int eq;
2351 isl_space *dim;
2352 struct isl_sched_info *info;
2353 struct isl_compute_flow_data *data;
2355 data = (struct isl_compute_flow_data *)user;
2357 dim = isl_space_range(isl_map_get_space(map));
2359 eq = isl_space_is_equal(dim, data->dim);
2361 isl_space_free(dim);
2363 if (eq < 0)
2364 goto error;
2365 if (!eq) {
2366 isl_map_free(map);
2367 return isl_stat_ok;
2370 info = sched_info_alloc(map);
2371 data->source_info[data->count] = info;
2373 data->accesses = isl_access_info_add_source(data->accesses,
2374 map, data->must, info);
2376 data->count++;
2378 return isl_stat_ok;
2379 error:
2380 isl_map_free(map);
2381 return isl_stat_error;
2384 /* Determine the shared nesting level and the "textual order" of
2385 * the given accesses.
2387 * We first determine the minimal schedule dimension for both accesses.
2389 * If among those dimensions, we can find one where both have a fixed
2390 * value and if moreover those values are different, then the previous
2391 * dimension is the last shared nesting level and the textual order
2392 * is determined based on the order of the fixed values.
2393 * If no such fixed values can be found, then we set the shared
2394 * nesting level to the minimal schedule dimension, with no textual ordering.
2396 static int before(void *first, void *second)
2398 struct isl_sched_info *info1 = first;
2399 struct isl_sched_info *info2 = second;
2400 int n1, n2;
2401 int i;
2403 n1 = isl_vec_size(info1->cst);
2404 n2 = isl_vec_size(info2->cst);
2406 if (n2 < n1)
2407 n1 = n2;
2409 for (i = 0; i < n1; ++i) {
2410 int r;
2411 int cmp;
2413 if (!info1->is_cst[i])
2414 continue;
2415 if (!info2->is_cst[i])
2416 continue;
2417 cmp = isl_vec_cmp_element(info1->cst, info2->cst, i);
2418 if (cmp == 0)
2419 continue;
2421 r = 2 * i + (cmp < 0);
2423 return r;
2426 return 2 * n1;
2429 /* Check if the given two accesses may be coscheduled.
2430 * If so, return 1. Otherwise return 0.
2432 * Two accesses may only be coscheduled if the fixed schedule
2433 * coordinates have the same values.
2435 static int coscheduled(void *first, void *second)
2437 struct isl_sched_info *info1 = first;
2438 struct isl_sched_info *info2 = second;
2439 int n1, n2;
2440 int i;
2442 n1 = isl_vec_size(info1->cst);
2443 n2 = isl_vec_size(info2->cst);
2445 if (n2 < n1)
2446 n1 = n2;
2448 for (i = 0; i < n1; ++i) {
2449 int cmp;
2451 if (!info1->is_cst[i])
2452 continue;
2453 if (!info2->is_cst[i])
2454 continue;
2455 cmp = isl_vec_cmp_element(info1->cst, info2->cst, i);
2456 if (cmp != 0)
2457 return 0;
2460 return 1;
2463 /* Given a sink access, look for all the source accesses that access
2464 * the same array and perform dataflow analysis on them using
2465 * isl_access_info_compute_flow_core.
2467 static isl_stat compute_flow(__isl_take isl_map *map, void *user)
2469 int i;
2470 isl_ctx *ctx;
2471 struct isl_compute_flow_data *data;
2472 isl_flow *flow;
2473 isl_union_flow *df;
2475 data = (struct isl_compute_flow_data *)user;
2476 df = data->flow;
2478 ctx = isl_map_get_ctx(map);
2480 data->accesses = NULL;
2481 data->sink_info = NULL;
2482 data->source_info = NULL;
2483 data->count = 0;
2484 data->dim = isl_space_range(isl_map_get_space(map));
2486 if (isl_union_map_foreach_map(data->must_source,
2487 &count_matching_array, data) < 0)
2488 goto error;
2489 if (isl_union_map_foreach_map(data->may_source,
2490 &count_matching_array, data) < 0)
2491 goto error;
2493 data->sink_info = sched_info_alloc(map);
2494 data->source_info = isl_calloc_array(ctx, struct isl_sched_info *,
2495 data->count);
2497 data->accesses = isl_access_info_alloc(isl_map_copy(map),
2498 data->sink_info, &before, data->count);
2499 if (!data->sink_info || (data->count && !data->source_info) ||
2500 !data->accesses)
2501 goto error;
2502 data->accesses->coscheduled = &coscheduled;
2503 data->count = 0;
2504 data->must = 1;
2505 if (isl_union_map_foreach_map(data->must_source,
2506 &collect_matching_array, data) < 0)
2507 goto error;
2508 data->must = 0;
2509 if (isl_union_map_foreach_map(data->may_source,
2510 &collect_matching_array, data) < 0)
2511 goto error;
2513 flow = access_info_compute_flow_core(data->accesses);
2514 data->accesses = NULL;
2516 if (!flow)
2517 goto error;
2519 df->must_no_source = isl_union_map_union(df->must_no_source,
2520 isl_union_map_from_map(isl_flow_get_no_source(flow, 1)));
2521 df->may_no_source = isl_union_map_union(df->may_no_source,
2522 isl_union_map_from_map(isl_flow_get_no_source(flow, 0)));
2524 for (i = 0; i < flow->n_source; ++i) {
2525 isl_union_map *dep;
2526 dep = isl_union_map_from_map(isl_map_copy(flow->dep[i].map));
2527 if (flow->dep[i].must)
2528 df->must_dep = isl_union_map_union(df->must_dep, dep);
2529 else
2530 df->may_dep = isl_union_map_union(df->may_dep, dep);
2533 isl_flow_free(flow);
2535 sched_info_free(data->sink_info);
2536 if (data->source_info) {
2537 for (i = 0; i < data->count; ++i)
2538 sched_info_free(data->source_info[i]);
2539 free(data->source_info);
2541 isl_space_free(data->dim);
2542 isl_map_free(map);
2544 return isl_stat_ok;
2545 error:
2546 isl_access_info_free(data->accesses);
2547 sched_info_free(data->sink_info);
2548 if (data->source_info) {
2549 for (i = 0; i < data->count; ++i)
2550 sched_info_free(data->source_info[i]);
2551 free(data->source_info);
2553 isl_space_free(data->dim);
2554 isl_map_free(map);
2556 return isl_stat_error;
2559 /* Add the kills of "info" to the must-sources.
2561 static __isl_give isl_union_access_info *
2562 isl_union_access_info_add_kill_to_must_source(
2563 __isl_take isl_union_access_info *info)
2565 isl_union_map *must, *kill;
2567 must = isl_union_access_info_get_must_source(info);
2568 kill = isl_union_access_info_get_kill(info);
2569 must = isl_union_map_union(must, kill);
2570 return isl_union_access_info_set_must_source(info, must);
2573 /* Drop dependences from "flow" that purely originate from kills.
2574 * That is, only keep those dependences that originate from
2575 * the original must-sources "must" and/or the original may-sources "may".
2576 * In particular, "must" contains the must-sources from before
2577 * the kills were added and "may" contains the may-source from before
2578 * the kills were removed.
2580 * The dependences are of the form
2582 * Source -> [Sink -> Data]
2584 * Only those dependences are kept where the Source -> Data part
2585 * is a subset of the original may-sources or must-sources.
2586 * Of those, only the must-dependences that intersect with the must-sources
2587 * remain must-dependences.
2588 * If there is some overlap between the may-sources and the must-sources,
2589 * then the may-dependences and must-dependences may also overlap.
2590 * This should be fine since the may-dependences are only kept
2591 * disjoint from the must-dependences for the isl_union_map_compute_flow
2592 * interface. This interface does not support kills, so it will
2593 * not end up calling this function.
2595 static __isl_give isl_union_flow *isl_union_flow_drop_kill_source(
2596 __isl_take isl_union_flow *flow, __isl_take isl_union_map *must,
2597 __isl_take isl_union_map *may)
2599 isl_union_map *move;
2601 if (!flow)
2602 goto error;
2603 move = isl_union_map_copy(flow->must_dep);
2604 move = isl_union_map_intersect_range_factor_range(move,
2605 isl_union_map_copy(may));
2606 may = isl_union_map_union(may, isl_union_map_copy(must));
2607 flow->may_dep = isl_union_map_intersect_range_factor_range(
2608 flow->may_dep, may);
2609 flow->must_dep = isl_union_map_intersect_range_factor_range(
2610 flow->must_dep, must);
2611 flow->may_dep = isl_union_map_union(flow->may_dep, move);
2612 if (!flow->must_dep || !flow->may_dep)
2613 return isl_union_flow_free(flow);
2615 return flow;
2616 error:
2617 isl_union_map_free(must);
2618 isl_union_map_free(may);
2619 return NULL;
2622 /* Remove the must accesses from the may accesses.
2624 * A must access always trumps a may access, so there is no need
2625 * for a must access to also be considered as a may access. Doing so
2626 * would only cost extra computations only to find out that
2627 * the duplicated may access does not make any difference.
2629 static __isl_give isl_union_access_info *isl_union_access_info_normalize(
2630 __isl_take isl_union_access_info *access)
2632 if (!access)
2633 return NULL;
2634 access->access[isl_access_may_source] =
2635 isl_union_map_subtract(access->access[isl_access_may_source],
2636 isl_union_map_copy(access->access[isl_access_must_source]));
2637 if (!access->access[isl_access_may_source])
2638 return isl_union_access_info_free(access);
2640 return access;
2643 /* Given a description of the "sink" accesses, the "source" accesses and
2644 * a schedule, compute for each instance of a sink access
2645 * and for each element accessed by that instance,
2646 * the possible or definite source accesses that last accessed the
2647 * element accessed by the sink access before this sink access
2648 * in the sense that there is no intermediate definite source access.
2650 * The must_no_source and may_no_source elements of the result
2651 * are subsets of access->sink. The elements must_dep and may_dep
2652 * map domain elements of access->{may,must)_source to
2653 * domain elements of access->sink.
2655 * This function is used when only the schedule map representation
2656 * is available.
2658 * We first prepend the schedule dimensions to the domain
2659 * of the accesses so that we can easily compare their relative order.
2660 * Then we consider each sink access individually in compute_flow.
2662 static __isl_give isl_union_flow *compute_flow_union_map(
2663 __isl_take isl_union_access_info *access)
2665 struct isl_compute_flow_data data;
2666 isl_union_map *sink;
2668 access = isl_union_access_info_align_params(access);
2669 access = isl_union_access_info_introduce_schedule(access);
2670 if (!access)
2671 return NULL;
2673 data.must_source = access->access[isl_access_must_source];
2674 data.may_source = access->access[isl_access_may_source];
2676 sink = access->access[isl_access_sink];
2677 data.flow = isl_union_flow_alloc(isl_union_map_get_space(sink));
2679 if (isl_union_map_foreach_map(sink, &compute_flow, &data) < 0)
2680 goto error;
2682 data.flow = isl_union_flow_drop_schedule(data.flow);
2684 isl_union_access_info_free(access);
2685 return data.flow;
2686 error:
2687 isl_union_access_info_free(access);
2688 isl_union_flow_free(data.flow);
2689 return NULL;
2692 /* A schedule access relation.
2694 * The access relation "access" is of the form [S -> D] -> A,
2695 * where S corresponds to the prefix schedule at "node".
2696 * "must" is only relevant for source accesses and indicates
2697 * whether the access is a must source or a may source.
2699 struct isl_scheduled_access {
2700 isl_map *access;
2701 int must;
2702 isl_schedule_node *node;
2705 /* Data structure for keeping track of individual scheduled sink and source
2706 * accesses when computing dependence analysis based on a schedule tree.
2708 * "n_sink" is the number of used entries in "sink"
2709 * "n_source" is the number of used entries in "source"
2711 * "set_sink", "must" and "node" are only used inside collect_sink_source,
2712 * to keep track of the current node and
2713 * of what extract_sink_source needs to do.
2715 struct isl_compute_flow_schedule_data {
2716 isl_union_access_info *access;
2718 int n_sink;
2719 int n_source;
2721 struct isl_scheduled_access *sink;
2722 struct isl_scheduled_access *source;
2724 int set_sink;
2725 int must;
2726 isl_schedule_node *node;
2729 /* Align the parameters of all sinks with all sources.
2731 * If there are no sinks or no sources, then no alignment is needed.
2733 static void isl_compute_flow_schedule_data_align_params(
2734 struct isl_compute_flow_schedule_data *data)
2736 int i;
2737 isl_space *space;
2739 if (data->n_sink == 0 || data->n_source == 0)
2740 return;
2742 space = isl_map_get_space(data->sink[0].access);
2744 for (i = 1; i < data->n_sink; ++i)
2745 space = isl_space_align_params(space,
2746 isl_map_get_space(data->sink[i].access));
2747 for (i = 0; i < data->n_source; ++i)
2748 space = isl_space_align_params(space,
2749 isl_map_get_space(data->source[i].access));
2751 for (i = 0; i < data->n_sink; ++i)
2752 data->sink[i].access =
2753 isl_map_align_params(data->sink[i].access,
2754 isl_space_copy(space));
2755 for (i = 0; i < data->n_source; ++i)
2756 data->source[i].access =
2757 isl_map_align_params(data->source[i].access,
2758 isl_space_copy(space));
2760 isl_space_free(space);
2763 /* Free all the memory referenced from "data".
2764 * Do not free "data" itself as it may be allocated on the stack.
2766 static void isl_compute_flow_schedule_data_clear(
2767 struct isl_compute_flow_schedule_data *data)
2769 int i;
2771 if (!data->sink)
2772 return;
2774 for (i = 0; i < data->n_sink; ++i) {
2775 isl_map_free(data->sink[i].access);
2776 isl_schedule_node_free(data->sink[i].node);
2779 for (i = 0; i < data->n_source; ++i) {
2780 isl_map_free(data->source[i].access);
2781 isl_schedule_node_free(data->source[i].node);
2784 free(data->sink);
2787 /* isl_schedule_foreach_schedule_node_top_down callback for counting
2788 * (an upper bound on) the number of sinks and sources.
2790 * Sinks and sources are only extracted at leaves of the tree,
2791 * so we skip the node if it is not a leaf.
2792 * Otherwise we increment data->n_sink and data->n_source with
2793 * the number of spaces in the sink and source access domains
2794 * that reach this node.
2796 static isl_bool count_sink_source(__isl_keep isl_schedule_node *node,
2797 void *user)
2799 struct isl_compute_flow_schedule_data *data = user;
2800 isl_union_set *domain;
2801 isl_union_map *umap;
2802 isl_bool r = isl_bool_false;
2804 if (isl_schedule_node_get_type(node) != isl_schedule_node_leaf)
2805 return isl_bool_true;
2807 domain = isl_schedule_node_get_universe_domain(node);
2809 umap = isl_union_map_copy(data->access->access[isl_access_sink]);
2810 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
2811 data->n_sink += isl_union_map_n_map(umap);
2812 isl_union_map_free(umap);
2813 if (!umap)
2814 r = isl_bool_error;
2816 umap = isl_union_map_copy(data->access->access[isl_access_must_source]);
2817 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
2818 data->n_source += isl_union_map_n_map(umap);
2819 isl_union_map_free(umap);
2820 if (!umap)
2821 r = isl_bool_error;
2823 umap = isl_union_map_copy(data->access->access[isl_access_may_source]);
2824 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
2825 data->n_source += isl_union_map_n_map(umap);
2826 isl_union_map_free(umap);
2827 if (!umap)
2828 r = isl_bool_error;
2830 isl_union_set_free(domain);
2832 return r;
2835 /* Add a single scheduled sink or source (depending on data->set_sink)
2836 * with scheduled access relation "map", must property data->must and
2837 * schedule node data->node to the list of sinks or sources.
2839 static isl_stat extract_sink_source(__isl_take isl_map *map, void *user)
2841 struct isl_compute_flow_schedule_data *data = user;
2842 struct isl_scheduled_access *access;
2844 if (data->set_sink)
2845 access = data->sink + data->n_sink++;
2846 else
2847 access = data->source + data->n_source++;
2849 access->access = map;
2850 access->must = data->must;
2851 access->node = isl_schedule_node_copy(data->node);
2853 return isl_stat_ok;
2856 /* isl_schedule_foreach_schedule_node_top_down callback for collecting
2857 * individual scheduled source and sink accesses (taking into account
2858 * the domain of the schedule).
2860 * We only collect accesses at the leaves of the schedule tree.
2861 * We prepend the schedule dimensions at the leaf to the iteration
2862 * domains of the source and sink accesses and then extract
2863 * the individual accesses (per space).
2865 * In particular, if the prefix schedule at the node is of the form
2867 * D -> S
2869 * while the access relations are of the form
2871 * D -> A
2873 * then the updated access relations are of the form
2875 * [S -> D] -> A
2877 * Note that S consists of a single space such that introducing S
2878 * in the access relations does not increase the number of spaces.
2880 static isl_bool collect_sink_source(__isl_keep isl_schedule_node *node,
2881 void *user)
2883 struct isl_compute_flow_schedule_data *data = user;
2884 isl_union_map *prefix;
2885 isl_union_map *umap;
2886 isl_bool r = isl_bool_false;
2888 if (isl_schedule_node_get_type(node) != isl_schedule_node_leaf)
2889 return isl_bool_true;
2891 data->node = node;
2893 prefix = isl_schedule_node_get_prefix_schedule_relation(node);
2894 prefix = isl_union_map_reverse(prefix);
2895 prefix = isl_union_map_range_map(prefix);
2897 data->set_sink = 1;
2898 umap = isl_union_map_copy(data->access->access[isl_access_sink]);
2899 umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
2900 if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
2901 r = isl_bool_error;
2902 isl_union_map_free(umap);
2904 data->set_sink = 0;
2905 data->must = 1;
2906 umap = isl_union_map_copy(data->access->access[isl_access_must_source]);
2907 umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
2908 if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
2909 r = isl_bool_error;
2910 isl_union_map_free(umap);
2912 data->set_sink = 0;
2913 data->must = 0;
2914 umap = isl_union_map_copy(data->access->access[isl_access_may_source]);
2915 umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
2916 if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
2917 r = isl_bool_error;
2918 isl_union_map_free(umap);
2920 isl_union_map_free(prefix);
2922 return r;
2925 /* isl_access_info_compute_flow callback for determining whether
2926 * the shared nesting level and the ordering within that level
2927 * for two scheduled accesses for use in compute_single_flow.
2929 * The tokens passed to this function refer to the leaves
2930 * in the schedule tree where the accesses take place.
2932 * If n is the shared number of loops, then we need to return
2933 * "2 * n + 1" if "first" precedes "second" inside the innermost
2934 * shared loop and "2 * n" otherwise.
2936 * The innermost shared ancestor may be the leaves themselves
2937 * if the accesses take place in the same leaf. Otherwise,
2938 * it is either a set node or a sequence node. Only in the case
2939 * of a sequence node do we consider one access to precede the other.
2941 static int before_node(void *first, void *second)
2943 isl_schedule_node *node1 = first;
2944 isl_schedule_node *node2 = second;
2945 isl_schedule_node *shared;
2946 int depth;
2947 int before = 0;
2949 shared = isl_schedule_node_get_shared_ancestor(node1, node2);
2950 if (!shared)
2951 return -1;
2953 depth = isl_schedule_node_get_schedule_depth(shared);
2954 if (isl_schedule_node_get_type(shared) == isl_schedule_node_sequence) {
2955 int pos1, pos2;
2957 pos1 = isl_schedule_node_get_ancestor_child_position(node1,
2958 shared);
2959 pos2 = isl_schedule_node_get_ancestor_child_position(node2,
2960 shared);
2961 before = pos1 < pos2;
2964 isl_schedule_node_free(shared);
2966 return 2 * depth + before;
2969 /* Check if the given two accesses may be coscheduled.
2970 * If so, return 1. Otherwise return 0.
2972 * Two accesses may only be coscheduled if they appear in the same leaf.
2974 static int coscheduled_node(void *first, void *second)
2976 isl_schedule_node *node1 = first;
2977 isl_schedule_node *node2 = second;
2979 return node1 == node2;
2982 /* Add the scheduled sources from "data" that access
2983 * the same data space as "sink" to "access".
2985 static __isl_give isl_access_info *add_matching_sources(
2986 __isl_take isl_access_info *access, struct isl_scheduled_access *sink,
2987 struct isl_compute_flow_schedule_data *data)
2989 int i;
2990 isl_space *space;
2992 space = isl_space_range(isl_map_get_space(sink->access));
2993 for (i = 0; i < data->n_source; ++i) {
2994 struct isl_scheduled_access *source;
2995 isl_space *source_space;
2996 int eq;
2998 source = &data->source[i];
2999 source_space = isl_map_get_space(source->access);
3000 source_space = isl_space_range(source_space);
3001 eq = isl_space_is_equal(space, source_space);
3002 isl_space_free(source_space);
3004 if (!eq)
3005 continue;
3006 if (eq < 0)
3007 goto error;
3009 access = isl_access_info_add_source(access,
3010 isl_map_copy(source->access), source->must, source->node);
3013 isl_space_free(space);
3014 return access;
3015 error:
3016 isl_space_free(space);
3017 isl_access_info_free(access);
3018 return NULL;
3021 /* Given a scheduled sink access relation "sink", compute the corresponding
3022 * dependences on the sources in "data" and add the computed dependences
3023 * to "uf".
3025 * The dependences computed by access_info_compute_flow_core are of the form
3027 * [S -> I] -> [[S' -> I'] -> A]
3029 * The schedule dimensions are projected out by first currying the range,
3030 * resulting in
3032 * [S -> I] -> [S' -> [I' -> A]]
3034 * and then computing the factor range
3036 * I -> [I' -> A]
3038 static __isl_give isl_union_flow *compute_single_flow(
3039 __isl_take isl_union_flow *uf, struct isl_scheduled_access *sink,
3040 struct isl_compute_flow_schedule_data *data)
3042 int i;
3043 isl_access_info *access;
3044 isl_flow *flow;
3045 isl_map *map;
3047 if (!uf)
3048 return NULL;
3050 access = isl_access_info_alloc(isl_map_copy(sink->access), sink->node,
3051 &before_node, data->n_source);
3052 if (access)
3053 access->coscheduled = &coscheduled_node;
3054 access = add_matching_sources(access, sink, data);
3056 flow = access_info_compute_flow_core(access);
3057 if (!flow)
3058 return isl_union_flow_free(uf);
3060 map = isl_map_domain_factor_range(isl_flow_get_no_source(flow, 1));
3061 uf->must_no_source = isl_union_map_union(uf->must_no_source,
3062 isl_union_map_from_map(map));
3063 map = isl_map_domain_factor_range(isl_flow_get_no_source(flow, 0));
3064 uf->may_no_source = isl_union_map_union(uf->may_no_source,
3065 isl_union_map_from_map(map));
3067 for (i = 0; i < flow->n_source; ++i) {
3068 isl_union_map *dep;
3070 map = isl_map_range_curry(isl_map_copy(flow->dep[i].map));
3071 map = isl_map_factor_range(map);
3072 dep = isl_union_map_from_map(map);
3073 if (flow->dep[i].must)
3074 uf->must_dep = isl_union_map_union(uf->must_dep, dep);
3075 else
3076 uf->may_dep = isl_union_map_union(uf->may_dep, dep);
3079 isl_flow_free(flow);
3081 return uf;
3084 /* Given a description of the "sink" accesses, the "source" accesses and
3085 * a schedule, compute for each instance of a sink access
3086 * and for each element accessed by that instance,
3087 * the possible or definite source accesses that last accessed the
3088 * element accessed by the sink access before this sink access
3089 * in the sense that there is no intermediate definite source access.
3090 * Only consider dependences between statement instances that belong
3091 * to the domain of the schedule.
3093 * The must_no_source and may_no_source elements of the result
3094 * are subsets of access->sink. The elements must_dep and may_dep
3095 * map domain elements of access->{may,must)_source to
3096 * domain elements of access->sink.
3098 * This function is used when a schedule tree representation
3099 * is available.
3101 * We extract the individual scheduled source and sink access relations
3102 * (taking into account the domain of the schedule) and
3103 * then compute dependences for each scheduled sink individually.
3105 static __isl_give isl_union_flow *compute_flow_schedule(
3106 __isl_take isl_union_access_info *access)
3108 struct isl_compute_flow_schedule_data data = { access };
3109 int i, n;
3110 isl_ctx *ctx;
3111 isl_space *space;
3112 isl_union_flow *flow;
3114 ctx = isl_union_access_info_get_ctx(access);
3116 data.n_sink = 0;
3117 data.n_source = 0;
3118 if (isl_schedule_foreach_schedule_node_top_down(access->schedule,
3119 &count_sink_source, &data) < 0)
3120 goto error;
3122 n = data.n_sink + data.n_source;
3123 data.sink = isl_calloc_array(ctx, struct isl_scheduled_access, n);
3124 if (n && !data.sink)
3125 goto error;
3126 data.source = data.sink + data.n_sink;
3128 data.n_sink = 0;
3129 data.n_source = 0;
3130 if (isl_schedule_foreach_schedule_node_top_down(access->schedule,
3131 &collect_sink_source, &data) < 0)
3132 goto error;
3134 space = isl_union_map_get_space(access->access[isl_access_sink]);
3135 flow = isl_union_flow_alloc(space);
3137 isl_compute_flow_schedule_data_align_params(&data);
3139 for (i = 0; i < data.n_sink; ++i)
3140 flow = compute_single_flow(flow, &data.sink[i], &data);
3142 isl_compute_flow_schedule_data_clear(&data);
3144 isl_union_access_info_free(access);
3145 return flow;
3146 error:
3147 isl_union_access_info_free(access);
3148 isl_compute_flow_schedule_data_clear(&data);
3149 return NULL;
3152 /* Given a description of the "sink" accesses, the "source" accesses and
3153 * a schedule, compute for each instance of a sink access
3154 * and for each element accessed by that instance,
3155 * the possible or definite source accesses that last accessed the
3156 * element accessed by the sink access before this sink access
3157 * in the sense that there is no intermediate definite source access.
3159 * The must_no_source and may_no_source elements of the result
3160 * are subsets of access->sink. The elements must_dep and may_dep
3161 * map domain elements of access->{may,must)_source to
3162 * domain elements of access->sink.
3164 * If any kills have been specified, then they are treated as
3165 * must-sources internally. Any dependence that purely derives
3166 * from an original kill is removed from the output.
3168 * We check whether the schedule is available as a schedule tree
3169 * or a schedule map and call the corresponding function to perform
3170 * the analysis.
3172 __isl_give isl_union_flow *isl_union_access_info_compute_flow(
3173 __isl_take isl_union_access_info *access)
3175 isl_bool has_kill;
3176 isl_union_map *must = NULL, *may = NULL;
3177 isl_union_flow *flow;
3179 has_kill = isl_union_access_has_kill(access);
3180 if (has_kill < 0)
3181 goto error;
3182 if (has_kill) {
3183 must = isl_union_access_info_get_must_source(access);
3184 may = isl_union_access_info_get_may_source(access);
3186 access = isl_union_access_info_add_kill_to_must_source(access);
3187 access = isl_union_access_info_normalize(access);
3188 if (!access)
3189 goto error;
3190 if (access->schedule)
3191 flow = compute_flow_schedule(access);
3192 else
3193 flow = compute_flow_union_map(access);
3194 if (has_kill)
3195 flow = isl_union_flow_drop_kill_source(flow, must, may);
3196 return flow;
3197 error:
3198 isl_union_access_info_free(access);
3199 isl_union_map_free(must);
3200 isl_union_map_free(may);
3201 return NULL;
3204 /* Print the information contained in "flow" to "p".
3205 * The information is printed as a YAML document.
3207 __isl_give isl_printer *isl_printer_print_union_flow(
3208 __isl_take isl_printer *p, __isl_keep isl_union_flow *flow)
3210 isl_union_map *umap;
3212 if (!flow)
3213 return isl_printer_free(p);
3215 p = isl_printer_yaml_start_mapping(p);
3216 umap = isl_union_flow_get_full_must_dependence(flow);
3217 p = print_union_map_field(p, "must_dependence", umap);
3218 isl_union_map_free(umap);
3219 umap = isl_union_flow_get_full_may_dependence(flow);
3220 p = print_union_map_field(p, "may_dependence", umap);
3221 isl_union_map_free(umap);
3222 p = print_union_map_field(p, "must_no_source", flow->must_no_source);
3223 umap = isl_union_flow_get_may_no_source(flow);
3224 p = print_union_map_field(p, "may_no_source", umap);
3225 isl_union_map_free(umap);
3226 p = isl_printer_yaml_end_mapping(p);
3228 return p;
3231 /* Return a string representation of the information in "flow".
3232 * The information is printed in flow format.
3234 __isl_give char *isl_union_flow_to_str(__isl_keep isl_union_flow *flow)
3236 isl_printer *p;
3237 char *s;
3239 if (!flow)
3240 return NULL;
3242 p = isl_printer_to_str(isl_union_flow_get_ctx(flow));
3243 p = isl_printer_set_yaml_style(p, ISL_YAML_STYLE_FLOW);
3244 p = isl_printer_print_union_flow(p, flow);
3245 s = isl_printer_get_str(p);
3246 isl_printer_free(p);
3248 return s;
3251 /* Given a collection of "sink" and "source" accesses,
3252 * compute for each iteration of a sink access
3253 * and for each element accessed by that iteration,
3254 * the source access in the list that last accessed the
3255 * element accessed by the sink access before this sink access.
3256 * Each access is given as a map from the loop iterators
3257 * to the array indices.
3258 * The result is a relations between source and sink
3259 * iterations and a subset of the domain of the sink accesses,
3260 * corresponding to those iterations that access an element
3261 * not previously accessed.
3263 * We collect the inputs in an isl_union_access_info object,
3264 * call isl_union_access_info_compute_flow and extract
3265 * the outputs from the result.
3267 int isl_union_map_compute_flow(__isl_take isl_union_map *sink,
3268 __isl_take isl_union_map *must_source,
3269 __isl_take isl_union_map *may_source,
3270 __isl_take isl_union_map *schedule,
3271 __isl_give isl_union_map **must_dep, __isl_give isl_union_map **may_dep,
3272 __isl_give isl_union_map **must_no_source,
3273 __isl_give isl_union_map **may_no_source)
3275 isl_union_access_info *access;
3276 isl_union_flow *flow;
3278 access = isl_union_access_info_from_sink(sink);
3279 access = isl_union_access_info_set_must_source(access, must_source);
3280 access = isl_union_access_info_set_may_source(access, may_source);
3281 access = isl_union_access_info_set_schedule_map(access, schedule);
3282 flow = isl_union_access_info_compute_flow(access);
3284 if (must_dep)
3285 *must_dep = isl_union_flow_get_must_dependence(flow);
3286 if (may_dep)
3287 *may_dep = isl_union_flow_get_non_must_dependence(flow);
3288 if (must_no_source)
3289 *must_no_source = isl_union_flow_get_must_no_source(flow);
3290 if (may_no_source)
3291 *may_no_source = isl_union_flow_get_non_must_no_source(flow);
3293 isl_union_flow_free(flow);
3295 if ((must_dep && !*must_dep) || (may_dep && !*may_dep) ||
3296 (must_no_source && !*must_no_source) ||
3297 (may_no_source && !*may_no_source))
3298 goto error;
3300 return 0;
3301 error:
3302 if (must_dep)
3303 *must_dep = isl_union_map_free(*must_dep);
3304 if (may_dep)
3305 *may_dep = isl_union_map_free(*may_dep);
3306 if (must_no_source)
3307 *must_no_source = isl_union_map_free(*must_no_source);
3308 if (may_no_source)
3309 *may_no_source = isl_union_map_free(*may_no_source);
3310 return -1;