add missing annotation to isl_vec_alloc
[isl.git] / isl_flow.c
blob77a87cbbfdd0e89923c75c91deb927033b2a9c2b
1 /*
2 * Copyright 2005-2007 Universiteit Leiden
3 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Copyright 2010 INRIA Saclay
5 * Copyright 2012 Universiteit Leiden
6 * Copyright 2014 Ecole Normale Superieure
8 * Use of this software is governed by the MIT license
10 * Written by Sven Verdoolaege, Leiden Institute of Advanced Computer Science,
11 * Universiteit Leiden, Niels Bohrweg 1, 2333 CA Leiden, The Netherlands
12 * and K.U.Leuven, Departement Computerwetenschappen, Celestijnenlaan 200A,
13 * B-3001 Leuven, Belgium
14 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
15 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
16 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
19 #include <isl/set.h>
20 #include <isl/map.h>
21 #include <isl/union_set.h>
22 #include <isl/union_map.h>
23 #include <isl/flow.h>
24 #include <isl/schedule_node.h>
25 #include <isl_sort.h>
26 #include <isl/stream.h>
28 enum isl_restriction_type {
29 isl_restriction_type_empty,
30 isl_restriction_type_none,
31 isl_restriction_type_input,
32 isl_restriction_type_output
35 struct isl_restriction {
36 enum isl_restriction_type type;
38 isl_set *source;
39 isl_set *sink;
42 /* Create a restriction of the given type.
44 static __isl_give isl_restriction *isl_restriction_alloc(
45 __isl_take isl_map *source_map, enum isl_restriction_type type)
47 isl_ctx *ctx;
48 isl_restriction *restr;
50 if (!source_map)
51 return NULL;
53 ctx = isl_map_get_ctx(source_map);
54 restr = isl_calloc_type(ctx, struct isl_restriction);
55 if (!restr)
56 goto error;
58 restr->type = type;
60 isl_map_free(source_map);
61 return restr;
62 error:
63 isl_map_free(source_map);
64 return NULL;
67 /* Create a restriction that doesn't restrict anything.
69 __isl_give isl_restriction *isl_restriction_none(__isl_take isl_map *source_map)
71 return isl_restriction_alloc(source_map, isl_restriction_type_none);
74 /* Create a restriction that removes everything.
76 __isl_give isl_restriction *isl_restriction_empty(
77 __isl_take isl_map *source_map)
79 return isl_restriction_alloc(source_map, isl_restriction_type_empty);
82 /* Create a restriction on the input of the maximization problem
83 * based on the given source and sink restrictions.
85 __isl_give isl_restriction *isl_restriction_input(
86 __isl_take isl_set *source_restr, __isl_take isl_set *sink_restr)
88 isl_ctx *ctx;
89 isl_restriction *restr;
91 if (!source_restr || !sink_restr)
92 goto error;
94 ctx = isl_set_get_ctx(source_restr);
95 restr = isl_calloc_type(ctx, struct isl_restriction);
96 if (!restr)
97 goto error;
99 restr->type = isl_restriction_type_input;
100 restr->source = source_restr;
101 restr->sink = sink_restr;
103 return restr;
104 error:
105 isl_set_free(source_restr);
106 isl_set_free(sink_restr);
107 return NULL;
110 /* Create a restriction on the output of the maximization problem
111 * based on the given source restriction.
113 __isl_give isl_restriction *isl_restriction_output(
114 __isl_take isl_set *source_restr)
116 isl_ctx *ctx;
117 isl_restriction *restr;
119 if (!source_restr)
120 return NULL;
122 ctx = isl_set_get_ctx(source_restr);
123 restr = isl_calloc_type(ctx, struct isl_restriction);
124 if (!restr)
125 goto error;
127 restr->type = isl_restriction_type_output;
128 restr->source = source_restr;
130 return restr;
131 error:
132 isl_set_free(source_restr);
133 return NULL;
136 __isl_null isl_restriction *isl_restriction_free(
137 __isl_take isl_restriction *restr)
139 if (!restr)
140 return NULL;
142 isl_set_free(restr->source);
143 isl_set_free(restr->sink);
144 free(restr);
145 return NULL;
148 isl_ctx *isl_restriction_get_ctx(__isl_keep isl_restriction *restr)
150 return restr ? isl_set_get_ctx(restr->source) : NULL;
153 /* A private structure to keep track of a mapping together with
154 * a user-specified identifier and a boolean indicating whether
155 * the map represents a must or may access/dependence.
157 struct isl_labeled_map {
158 struct isl_map *map;
159 void *data;
160 int must;
163 typedef int (*isl_access_coscheduled)(void *first, void *second);
165 /* A structure containing the input for dependence analysis:
166 * - a sink
167 * - n_must + n_may (<= max_source) sources
168 * - a function for determining the relative order of sources and sink
169 * - an optional function "coscheduled" for determining whether sources
170 * may be coscheduled. If "coscheduled" is NULL, then the sources
171 * are assumed not to be coscheduled.
172 * The must sources are placed before the may sources.
174 * domain_map is an auxiliary map that maps the sink access relation
175 * to the domain of this access relation.
176 * This field is only needed when restrict_fn is set and
177 * the field itself is set by isl_access_info_compute_flow.
179 * restrict_fn is a callback that (if not NULL) will be called
180 * right before any lexicographical maximization.
182 struct isl_access_info {
183 isl_map *domain_map;
184 struct isl_labeled_map sink;
185 isl_access_level_before level_before;
186 isl_access_coscheduled coscheduled;
188 isl_access_restrict restrict_fn;
189 void *restrict_user;
191 int max_source;
192 int n_must;
193 int n_may;
194 struct isl_labeled_map source[1];
197 /* A structure containing the output of dependence analysis:
198 * - n_source dependences
199 * - a wrapped subset of the sink for which definitely no source could be found
200 * - a wrapped subset of the sink for which possibly no source could be found
202 struct isl_flow {
203 isl_set *must_no_source;
204 isl_set *may_no_source;
205 int n_source;
206 struct isl_labeled_map *dep;
209 /* Construct an isl_access_info structure and fill it up with
210 * the given data. The number of sources is set to 0.
212 __isl_give isl_access_info *isl_access_info_alloc(__isl_take isl_map *sink,
213 void *sink_user, isl_access_level_before fn, int max_source)
215 isl_ctx *ctx;
216 struct isl_access_info *acc;
218 if (!sink)
219 return NULL;
221 ctx = isl_map_get_ctx(sink);
222 isl_assert(ctx, max_source >= 0, goto error);
224 acc = isl_calloc(ctx, struct isl_access_info,
225 sizeof(struct isl_access_info) +
226 (max_source - 1) * sizeof(struct isl_labeled_map));
227 if (!acc)
228 goto error;
230 acc->sink.map = sink;
231 acc->sink.data = sink_user;
232 acc->level_before = fn;
233 acc->max_source = max_source;
234 acc->n_must = 0;
235 acc->n_may = 0;
237 return acc;
238 error:
239 isl_map_free(sink);
240 return NULL;
243 /* Free the given isl_access_info structure.
245 __isl_null isl_access_info *isl_access_info_free(
246 __isl_take isl_access_info *acc)
248 int i;
250 if (!acc)
251 return NULL;
252 isl_map_free(acc->domain_map);
253 isl_map_free(acc->sink.map);
254 for (i = 0; i < acc->n_must + acc->n_may; ++i)
255 isl_map_free(acc->source[i].map);
256 free(acc);
257 return NULL;
260 isl_ctx *isl_access_info_get_ctx(__isl_keep isl_access_info *acc)
262 return acc ? isl_map_get_ctx(acc->sink.map) : NULL;
265 __isl_give isl_access_info *isl_access_info_set_restrict(
266 __isl_take isl_access_info *acc, isl_access_restrict fn, void *user)
268 if (!acc)
269 return NULL;
270 acc->restrict_fn = fn;
271 acc->restrict_user = user;
272 return acc;
275 /* Add another source to an isl_access_info structure, making
276 * sure the "must" sources are placed before the "may" sources.
277 * This function may be called at most max_source times on a
278 * given isl_access_info structure, with max_source as specified
279 * in the call to isl_access_info_alloc that constructed the structure.
281 __isl_give isl_access_info *isl_access_info_add_source(
282 __isl_take isl_access_info *acc, __isl_take isl_map *source,
283 int must, void *source_user)
285 isl_ctx *ctx;
287 if (!acc)
288 goto error;
289 ctx = isl_map_get_ctx(acc->sink.map);
290 isl_assert(ctx, acc->n_must + acc->n_may < acc->max_source, goto error);
292 if (must) {
293 if (acc->n_may)
294 acc->source[acc->n_must + acc->n_may] =
295 acc->source[acc->n_must];
296 acc->source[acc->n_must].map = source;
297 acc->source[acc->n_must].data = source_user;
298 acc->source[acc->n_must].must = 1;
299 acc->n_must++;
300 } else {
301 acc->source[acc->n_must + acc->n_may].map = source;
302 acc->source[acc->n_must + acc->n_may].data = source_user;
303 acc->source[acc->n_must + acc->n_may].must = 0;
304 acc->n_may++;
307 return acc;
308 error:
309 isl_map_free(source);
310 isl_access_info_free(acc);
311 return NULL;
314 /* Return -n, 0 or n (with n a positive value), depending on whether
315 * the source access identified by p1 should be sorted before, together
316 * or after that identified by p2.
318 * If p1 appears before p2, then it should be sorted first.
319 * For more generic initial schedules, it is possible that neither
320 * p1 nor p2 appears before the other, or at least not in any obvious way.
321 * We therefore also check if p2 appears before p1, in which case p2
322 * should be sorted first.
323 * If not, we try to order the two statements based on the description
324 * of the iteration domains. This results in an arbitrary, but fairly
325 * stable ordering.
327 static int access_sort_cmp(const void *p1, const void *p2, void *user)
329 isl_access_info *acc = user;
330 const struct isl_labeled_map *i1, *i2;
331 int level1, level2;
332 uint32_t h1, h2;
333 i1 = (const struct isl_labeled_map *) p1;
334 i2 = (const struct isl_labeled_map *) p2;
336 level1 = acc->level_before(i1->data, i2->data);
337 if (level1 % 2)
338 return -1;
340 level2 = acc->level_before(i2->data, i1->data);
341 if (level2 % 2)
342 return 1;
344 h1 = isl_map_get_hash(i1->map);
345 h2 = isl_map_get_hash(i2->map);
346 return h1 > h2 ? 1 : h1 < h2 ? -1 : 0;
349 /* Sort the must source accesses in their textual order.
351 static __isl_give isl_access_info *isl_access_info_sort_sources(
352 __isl_take isl_access_info *acc)
354 if (!acc)
355 return NULL;
356 if (acc->n_must <= 1)
357 return acc;
359 if (isl_sort(acc->source, acc->n_must, sizeof(struct isl_labeled_map),
360 access_sort_cmp, acc) < 0)
361 return isl_access_info_free(acc);
363 return acc;
366 /* Align the parameters of the two spaces if needed and then call
367 * isl_space_join.
369 static __isl_give isl_space *space_align_and_join(__isl_take isl_space *left,
370 __isl_take isl_space *right)
372 isl_bool equal_params;
374 equal_params = isl_space_has_equal_params(left, right);
375 if (equal_params < 0)
376 goto error;
377 if (equal_params)
378 return isl_space_join(left, right);
380 left = isl_space_align_params(left, isl_space_copy(right));
381 right = isl_space_align_params(right, isl_space_copy(left));
382 return isl_space_join(left, right);
383 error:
384 isl_space_free(left);
385 isl_space_free(right);
386 return NULL;
389 /* Initialize an empty isl_flow structure corresponding to a given
390 * isl_access_info structure.
391 * For each must access, two dependences are created (initialized
392 * to the empty relation), one for the resulting must dependences
393 * and one for the resulting may dependences. May accesses can
394 * only lead to may dependences, so only one dependence is created
395 * for each of them.
396 * This function is private as isl_flow structures are only supposed
397 * to be created by isl_access_info_compute_flow.
399 static __isl_give isl_flow *isl_flow_alloc(__isl_keep isl_access_info *acc)
401 int i, n;
402 struct isl_ctx *ctx;
403 struct isl_flow *dep;
405 if (!acc)
406 return NULL;
408 ctx = isl_map_get_ctx(acc->sink.map);
409 dep = isl_calloc_type(ctx, struct isl_flow);
410 if (!dep)
411 return NULL;
413 n = 2 * acc->n_must + acc->n_may;
414 dep->dep = isl_calloc_array(ctx, struct isl_labeled_map, n);
415 if (n && !dep->dep)
416 goto error;
418 dep->n_source = n;
419 for (i = 0; i < acc->n_must; ++i) {
420 isl_space *dim;
421 dim = space_align_and_join(
422 isl_map_get_space(acc->source[i].map),
423 isl_space_reverse(isl_map_get_space(acc->sink.map)));
424 dep->dep[2 * i].map = isl_map_empty(dim);
425 dep->dep[2 * i + 1].map = isl_map_copy(dep->dep[2 * i].map);
426 dep->dep[2 * i].data = acc->source[i].data;
427 dep->dep[2 * i + 1].data = acc->source[i].data;
428 dep->dep[2 * i].must = 1;
429 dep->dep[2 * i + 1].must = 0;
430 if (!dep->dep[2 * i].map || !dep->dep[2 * i + 1].map)
431 goto error;
433 for (i = acc->n_must; i < acc->n_must + acc->n_may; ++i) {
434 isl_space *dim;
435 dim = space_align_and_join(
436 isl_map_get_space(acc->source[i].map),
437 isl_space_reverse(isl_map_get_space(acc->sink.map)));
438 dep->dep[acc->n_must + i].map = isl_map_empty(dim);
439 dep->dep[acc->n_must + i].data = acc->source[i].data;
440 dep->dep[acc->n_must + i].must = 0;
441 if (!dep->dep[acc->n_must + i].map)
442 goto error;
445 return dep;
446 error:
447 isl_flow_free(dep);
448 return NULL;
451 /* Iterate over all sources and for each resulting flow dependence
452 * that is not empty, call the user specfied function.
453 * The second argument in this function call identifies the source,
454 * while the third argument correspond to the final argument of
455 * the isl_flow_foreach call.
457 isl_stat isl_flow_foreach(__isl_keep isl_flow *deps,
458 isl_stat (*fn)(__isl_take isl_map *dep, int must, void *dep_user,
459 void *user),
460 void *user)
462 int i;
464 if (!deps)
465 return isl_stat_error;
467 for (i = 0; i < deps->n_source; ++i) {
468 if (isl_map_plain_is_empty(deps->dep[i].map))
469 continue;
470 if (fn(isl_map_copy(deps->dep[i].map), deps->dep[i].must,
471 deps->dep[i].data, user) < 0)
472 return isl_stat_error;
475 return isl_stat_ok;
478 /* Return a copy of the subset of the sink for which no source could be found.
480 __isl_give isl_map *isl_flow_get_no_source(__isl_keep isl_flow *deps, int must)
482 if (!deps)
483 return NULL;
485 if (must)
486 return isl_set_unwrap(isl_set_copy(deps->must_no_source));
487 else
488 return isl_set_unwrap(isl_set_copy(deps->may_no_source));
491 void isl_flow_free(__isl_take isl_flow *deps)
493 int i;
495 if (!deps)
496 return;
497 isl_set_free(deps->must_no_source);
498 isl_set_free(deps->may_no_source);
499 if (deps->dep) {
500 for (i = 0; i < deps->n_source; ++i)
501 isl_map_free(deps->dep[i].map);
502 free(deps->dep);
504 free(deps);
507 isl_ctx *isl_flow_get_ctx(__isl_keep isl_flow *deps)
509 return deps ? isl_set_get_ctx(deps->must_no_source) : NULL;
512 /* Return a map that enforces that the domain iteration occurs after
513 * the range iteration at the given level.
514 * If level is odd, then the domain iteration should occur after
515 * the target iteration in their shared level/2 outermost loops.
516 * In this case we simply need to enforce that these outermost
517 * loop iterations are the same.
518 * If level is even, then the loop iterator of the domain should
519 * be greater than the loop iterator of the range at the last
520 * of the level/2 shared loops, i.e., loop level/2 - 1.
522 static __isl_give isl_map *after_at_level(__isl_take isl_space *dim, int level)
524 struct isl_basic_map *bmap;
526 if (level % 2)
527 bmap = isl_basic_map_equal(dim, level/2);
528 else
529 bmap = isl_basic_map_more_at(dim, level/2 - 1);
531 return isl_map_from_basic_map(bmap);
534 /* Compute the partial lexicographic maximum of "dep" on domain "sink",
535 * but first check if the user has set acc->restrict_fn and if so
536 * update either the input or the output of the maximization problem
537 * with respect to the resulting restriction.
539 * Since the user expects a mapping from sink iterations to source iterations,
540 * whereas the domain of "dep" is a wrapped map, mapping sink iterations
541 * to accessed array elements, we first need to project out the accessed
542 * sink array elements by applying acc->domain_map.
543 * Similarly, the sink restriction specified by the user needs to be
544 * converted back to the wrapped map.
546 static __isl_give isl_map *restricted_partial_lexmax(
547 __isl_keep isl_access_info *acc, __isl_take isl_map *dep,
548 int source, __isl_take isl_set *sink, __isl_give isl_set **empty)
550 isl_map *source_map;
551 isl_restriction *restr;
552 isl_set *sink_domain;
553 isl_set *sink_restr;
554 isl_map *res;
556 if (!acc->restrict_fn)
557 return isl_map_partial_lexmax(dep, sink, empty);
559 source_map = isl_map_copy(dep);
560 source_map = isl_map_apply_domain(source_map,
561 isl_map_copy(acc->domain_map));
562 sink_domain = isl_set_copy(sink);
563 sink_domain = isl_set_apply(sink_domain, isl_map_copy(acc->domain_map));
564 restr = acc->restrict_fn(source_map, sink_domain,
565 acc->source[source].data, acc->restrict_user);
566 isl_set_free(sink_domain);
567 isl_map_free(source_map);
569 if (!restr)
570 goto error;
571 if (restr->type == isl_restriction_type_input) {
572 dep = isl_map_intersect_range(dep, isl_set_copy(restr->source));
573 sink_restr = isl_set_copy(restr->sink);
574 sink_restr = isl_set_apply(sink_restr,
575 isl_map_reverse(isl_map_copy(acc->domain_map)));
576 sink = isl_set_intersect(sink, sink_restr);
577 } else if (restr->type == isl_restriction_type_empty) {
578 isl_space *space = isl_map_get_space(dep);
579 isl_map_free(dep);
580 dep = isl_map_empty(space);
583 res = isl_map_partial_lexmax(dep, sink, empty);
585 if (restr->type == isl_restriction_type_output)
586 res = isl_map_intersect_range(res, isl_set_copy(restr->source));
588 isl_restriction_free(restr);
589 return res;
590 error:
591 isl_map_free(dep);
592 isl_set_free(sink);
593 *empty = NULL;
594 return NULL;
597 /* Compute the last iteration of must source j that precedes the sink
598 * at the given level for sink iterations in set_C.
599 * The subset of set_C for which no such iteration can be found is returned
600 * in *empty.
602 static struct isl_map *last_source(struct isl_access_info *acc,
603 struct isl_set *set_C,
604 int j, int level, struct isl_set **empty)
606 struct isl_map *read_map;
607 struct isl_map *write_map;
608 struct isl_map *dep_map;
609 struct isl_map *after;
610 struct isl_map *result;
612 read_map = isl_map_copy(acc->sink.map);
613 write_map = isl_map_copy(acc->source[j].map);
614 write_map = isl_map_reverse(write_map);
615 dep_map = isl_map_apply_range(read_map, write_map);
616 after = after_at_level(isl_map_get_space(dep_map), level);
617 dep_map = isl_map_intersect(dep_map, after);
618 result = restricted_partial_lexmax(acc, dep_map, j, set_C, empty);
619 result = isl_map_reverse(result);
621 return result;
624 /* For a given mapping between iterations of must source j and iterations
625 * of the sink, compute the last iteration of must source k preceding
626 * the sink at level before_level for any of the sink iterations,
627 * but following the corresponding iteration of must source j at level
628 * after_level.
630 static struct isl_map *last_later_source(struct isl_access_info *acc,
631 struct isl_map *old_map,
632 int j, int before_level,
633 int k, int after_level,
634 struct isl_set **empty)
636 isl_space *dim;
637 struct isl_set *set_C;
638 struct isl_map *read_map;
639 struct isl_map *write_map;
640 struct isl_map *dep_map;
641 struct isl_map *after_write;
642 struct isl_map *before_read;
643 struct isl_map *result;
645 set_C = isl_map_range(isl_map_copy(old_map));
646 read_map = isl_map_copy(acc->sink.map);
647 write_map = isl_map_copy(acc->source[k].map);
649 write_map = isl_map_reverse(write_map);
650 dep_map = isl_map_apply_range(read_map, write_map);
651 dim = space_align_and_join(isl_map_get_space(acc->source[k].map),
652 isl_space_reverse(isl_map_get_space(acc->source[j].map)));
653 after_write = after_at_level(dim, after_level);
654 after_write = isl_map_apply_range(after_write, old_map);
655 after_write = isl_map_reverse(after_write);
656 dep_map = isl_map_intersect(dep_map, after_write);
657 before_read = after_at_level(isl_map_get_space(dep_map), before_level);
658 dep_map = isl_map_intersect(dep_map, before_read);
659 result = restricted_partial_lexmax(acc, dep_map, k, set_C, empty);
660 result = isl_map_reverse(result);
662 return result;
665 /* Given a shared_level between two accesses, return 1 if the
666 * the first can precede the second at the requested target_level.
667 * If the target level is odd, i.e., refers to a statement level
668 * dimension, then first needs to precede second at the requested
669 * level, i.e., shared_level must be equal to target_level.
670 * If the target level is odd, then the two loops should share
671 * at least the requested number of outer loops.
673 static int can_precede_at_level(int shared_level, int target_level)
675 if (shared_level < target_level)
676 return 0;
677 if ((target_level % 2) && shared_level > target_level)
678 return 0;
679 return 1;
682 /* Given a possible flow dependence temp_rel[j] between source j and the sink
683 * at level sink_level, remove those elements for which
684 * there is an iteration of another source k < j that is closer to the sink.
685 * The flow dependences temp_rel[k] are updated with the improved sources.
686 * Any improved source needs to precede the sink at the same level
687 * and needs to follow source j at the same or a deeper level.
688 * The lower this level, the later the execution date of source k.
689 * We therefore consider lower levels first.
691 * If temp_rel[j] is empty, then there can be no improvement and
692 * we return immediately.
694 static int intermediate_sources(__isl_keep isl_access_info *acc,
695 struct isl_map **temp_rel, int j, int sink_level)
697 int k, level;
698 int depth = 2 * isl_map_dim(acc->source[j].map, isl_dim_in) + 1;
700 if (isl_map_plain_is_empty(temp_rel[j]))
701 return 0;
703 for (k = j - 1; k >= 0; --k) {
704 int plevel, plevel2;
705 plevel = acc->level_before(acc->source[k].data, acc->sink.data);
706 if (!can_precede_at_level(plevel, sink_level))
707 continue;
709 plevel2 = acc->level_before(acc->source[j].data,
710 acc->source[k].data);
712 for (level = sink_level; level <= depth; ++level) {
713 struct isl_map *T;
714 struct isl_set *trest;
715 struct isl_map *copy;
717 if (!can_precede_at_level(plevel2, level))
718 continue;
720 copy = isl_map_copy(temp_rel[j]);
721 T = last_later_source(acc, copy, j, sink_level, k,
722 level, &trest);
723 if (isl_map_plain_is_empty(T)) {
724 isl_set_free(trest);
725 isl_map_free(T);
726 continue;
728 temp_rel[j] = isl_map_intersect_range(temp_rel[j], trest);
729 temp_rel[k] = isl_map_union_disjoint(temp_rel[k], T);
733 return 0;
736 /* Compute all iterations of may source j that precedes the sink at the given
737 * level for sink iterations in set_C.
739 static __isl_give isl_map *all_sources(__isl_keep isl_access_info *acc,
740 __isl_take isl_set *set_C, int j, int level)
742 isl_map *read_map;
743 isl_map *write_map;
744 isl_map *dep_map;
745 isl_map *after;
747 read_map = isl_map_copy(acc->sink.map);
748 read_map = isl_map_intersect_domain(read_map, set_C);
749 write_map = isl_map_copy(acc->source[acc->n_must + j].map);
750 write_map = isl_map_reverse(write_map);
751 dep_map = isl_map_apply_range(read_map, write_map);
752 after = after_at_level(isl_map_get_space(dep_map), level);
753 dep_map = isl_map_intersect(dep_map, after);
755 return isl_map_reverse(dep_map);
758 /* For a given mapping between iterations of must source k and iterations
759 * of the sink, compute all iterations of may source j preceding
760 * the sink at level before_level for any of the sink iterations,
761 * but following the corresponding iteration of must source k at level
762 * after_level.
764 static __isl_give isl_map *all_later_sources(__isl_keep isl_access_info *acc,
765 __isl_take isl_map *old_map,
766 int j, int before_level, int k, int after_level)
768 isl_space *dim;
769 isl_set *set_C;
770 isl_map *read_map;
771 isl_map *write_map;
772 isl_map *dep_map;
773 isl_map *after_write;
774 isl_map *before_read;
776 set_C = isl_map_range(isl_map_copy(old_map));
777 read_map = isl_map_copy(acc->sink.map);
778 read_map = isl_map_intersect_domain(read_map, set_C);
779 write_map = isl_map_copy(acc->source[acc->n_must + j].map);
781 write_map = isl_map_reverse(write_map);
782 dep_map = isl_map_apply_range(read_map, write_map);
783 dim = isl_space_join(isl_map_get_space(acc->source[acc->n_must + j].map),
784 isl_space_reverse(isl_map_get_space(acc->source[k].map)));
785 after_write = after_at_level(dim, after_level);
786 after_write = isl_map_apply_range(after_write, old_map);
787 after_write = isl_map_reverse(after_write);
788 dep_map = isl_map_intersect(dep_map, after_write);
789 before_read = after_at_level(isl_map_get_space(dep_map), before_level);
790 dep_map = isl_map_intersect(dep_map, before_read);
791 return isl_map_reverse(dep_map);
794 /* Given the must and may dependence relations for the must accesses
795 * for level sink_level, check if there are any accesses of may access j
796 * that occur in between and return their union.
797 * If some of these accesses are intermediate with respect to
798 * (previously thought to be) must dependences, then these
799 * must dependences are turned into may dependences.
801 static __isl_give isl_map *all_intermediate_sources(
802 __isl_keep isl_access_info *acc, __isl_take isl_map *map,
803 struct isl_map **must_rel, struct isl_map **may_rel,
804 int j, int sink_level)
806 int k, level;
807 int depth = 2 * isl_map_dim(acc->source[acc->n_must + j].map,
808 isl_dim_in) + 1;
810 for (k = 0; k < acc->n_must; ++k) {
811 int plevel;
813 if (isl_map_plain_is_empty(may_rel[k]) &&
814 isl_map_plain_is_empty(must_rel[k]))
815 continue;
817 plevel = acc->level_before(acc->source[k].data,
818 acc->source[acc->n_must + j].data);
820 for (level = sink_level; level <= depth; ++level) {
821 isl_map *T;
822 isl_map *copy;
823 isl_set *ran;
825 if (!can_precede_at_level(plevel, level))
826 continue;
828 copy = isl_map_copy(may_rel[k]);
829 T = all_later_sources(acc, copy, j, sink_level, k, level);
830 map = isl_map_union(map, T);
832 copy = isl_map_copy(must_rel[k]);
833 T = all_later_sources(acc, copy, j, sink_level, k, level);
834 ran = isl_map_range(isl_map_copy(T));
835 map = isl_map_union(map, T);
836 may_rel[k] = isl_map_union_disjoint(may_rel[k],
837 isl_map_intersect_range(isl_map_copy(must_rel[k]),
838 isl_set_copy(ran)));
839 T = isl_map_from_domain_and_range(
840 isl_set_universe(
841 isl_space_domain(isl_map_get_space(must_rel[k]))),
842 ran);
843 must_rel[k] = isl_map_subtract(must_rel[k], T);
847 return map;
850 /* Given a dependence relation "old_map" between a must-source and the sink,
851 * return a subset of the dependences, augmented with instances
852 * of the source at position "pos" in "acc" that are coscheduled
853 * with the must-source and that access the same element.
854 * That is, if the input lives in a space T -> K, then the output
855 * lives in the space [T -> S] -> K, with S the space of source "pos", and
856 * the domain factor of the domain product is a subset of the input.
857 * The sources are considered to be coscheduled if they have the same values
858 * for the initial "depth" coordinates.
860 * First construct a dependence relation S -> K and a mapping
861 * between coscheduled sources T -> S.
862 * The second is combined with the original dependence relation T -> K
863 * to form a relation in T -> [S -> K], which is subsequently
864 * uncurried to [T -> S] -> K.
865 * This result is then intersected with the dependence relation S -> K
866 * to form the output.
868 static __isl_give isl_map *coscheduled_source(__isl_keep isl_access_info *acc,
869 __isl_keep isl_map *old_map, int pos, int depth)
871 isl_space *space;
872 isl_set *set_C;
873 isl_map *read_map;
874 isl_map *write_map;
875 isl_map *dep_map;
876 isl_map *equal;
877 isl_map *map;
879 set_C = isl_map_range(isl_map_copy(old_map));
880 read_map = isl_map_copy(acc->sink.map);
881 read_map = isl_map_intersect_domain(read_map, set_C);
882 write_map = isl_map_copy(acc->source[pos].map);
883 dep_map = isl_map_domain_product(write_map, read_map);
884 dep_map = isl_set_unwrap(isl_map_domain(dep_map));
885 space = isl_space_join(isl_map_get_space(old_map),
886 isl_space_reverse(isl_map_get_space(dep_map)));
887 equal = isl_map_from_basic_map(isl_basic_map_equal(space, depth));
888 map = isl_map_range_product(equal, isl_map_copy(old_map));
889 map = isl_map_uncurry(map);
890 map = isl_map_intersect_domain_factor_range(map, dep_map);
892 return map;
895 /* After the dependences derived from a must-source have been computed
896 * at a certain level, check if any of the sources of the must-dependences
897 * may be coscheduled with other sources.
898 * If they are any such sources, then there is no way of determining
899 * which of the sources actually comes last and the must-dependences
900 * need to be turned into may-dependences, while dependences from
901 * the other sources need to be added to the may-dependences as well.
902 * "acc" describes the sources and a callback for checking whether
903 * two sources may be coscheduled. If acc->coscheduled is NULL then
904 * the sources are assumed not to be coscheduled.
905 * "must_rel" and "may_rel" describe the must and may-dependence relations
906 * computed at the current level for the must-sources. Some of the dependences
907 * may be moved from "must_rel" to "may_rel".
908 * "flow" contains all dependences computed so far (apart from those
909 * in "must_rel" and "may_rel") and may be updated with additional
910 * dependences derived from may-sources.
912 * In particular, consider all the must-sources with a non-empty
913 * dependence relation in "must_rel". They are considered in reverse
914 * order because that is the order in which they are considered in the caller.
915 * If any of the must-sources are coscheduled, then the last one
916 * is the one that will have a corresponding dependence relation.
917 * For each must-source i, consider both all the previous must-sources
918 * and all the may-sources. If any of those may be coscheduled with
919 * must-source i, then compute the coscheduled instances that access
920 * the same memory elements. The result is a relation [T -> S] -> K.
921 * The projection onto T -> K is a subset of the must-dependence relation
922 * that needs to be turned into may-dependences.
923 * The projection onto S -> K needs to be added to the may-dependences
924 * of source S.
925 * Since a given must-source instance may be coscheduled with several
926 * other source instances, the dependences that need to be turned
927 * into may-dependences are first collected and only actually removed
928 * from the must-dependences after all other sources have been considered.
930 static __isl_give isl_flow *handle_coscheduled(__isl_keep isl_access_info *acc,
931 __isl_keep isl_map **must_rel, __isl_keep isl_map **may_rel,
932 __isl_take isl_flow *flow)
934 int i, j;
936 if (!acc->coscheduled)
937 return flow;
938 for (i = acc->n_must - 1; i >= 0; --i) {
939 isl_map *move;
941 if (isl_map_plain_is_empty(must_rel[i]))
942 continue;
943 move = isl_map_empty(isl_map_get_space(must_rel[i]));
944 for (j = i - 1; j >= 0; --j) {
945 int depth;
946 isl_map *map, *factor;
948 if (!acc->coscheduled(acc->source[i].data,
949 acc->source[j].data))
950 continue;
951 depth = acc->level_before(acc->source[i].data,
952 acc->source[j].data) / 2;
953 map = coscheduled_source(acc, must_rel[i], j, depth);
954 factor = isl_map_domain_factor_range(isl_map_copy(map));
955 may_rel[j] = isl_map_union(may_rel[j], factor);
956 map = isl_map_domain_factor_domain(map);
957 move = isl_map_union(move, map);
959 for (j = 0; j < acc->n_may; ++j) {
960 int depth, pos;
961 isl_map *map, *factor;
963 pos = acc->n_must + j;
964 if (!acc->coscheduled(acc->source[i].data,
965 acc->source[pos].data))
966 continue;
967 depth = acc->level_before(acc->source[i].data,
968 acc->source[pos].data) / 2;
969 map = coscheduled_source(acc, must_rel[i], pos, depth);
970 factor = isl_map_domain_factor_range(isl_map_copy(map));
971 pos = 2 * acc->n_must + j;
972 flow->dep[pos].map = isl_map_union(flow->dep[pos].map,
973 factor);
974 map = isl_map_domain_factor_domain(map);
975 move = isl_map_union(move, map);
977 must_rel[i] = isl_map_subtract(must_rel[i], isl_map_copy(move));
978 may_rel[i] = isl_map_union(may_rel[i], move);
981 return flow;
984 /* Compute dependences for the case where all accesses are "may"
985 * accesses, which boils down to computing memory based dependences.
986 * The generic algorithm would also work in this case, but it would
987 * be overkill to use it.
989 static __isl_give isl_flow *compute_mem_based_dependences(
990 __isl_keep isl_access_info *acc)
992 int i;
993 isl_set *mustdo;
994 isl_set *maydo;
995 isl_flow *res;
997 res = isl_flow_alloc(acc);
998 if (!res)
999 return NULL;
1001 mustdo = isl_map_domain(isl_map_copy(acc->sink.map));
1002 maydo = isl_set_copy(mustdo);
1004 for (i = 0; i < acc->n_may; ++i) {
1005 int plevel;
1006 int is_before;
1007 isl_space *dim;
1008 isl_map *before;
1009 isl_map *dep;
1011 plevel = acc->level_before(acc->source[i].data, acc->sink.data);
1012 is_before = plevel & 1;
1013 plevel >>= 1;
1015 dim = isl_map_get_space(res->dep[i].map);
1016 if (is_before)
1017 before = isl_map_lex_le_first(dim, plevel);
1018 else
1019 before = isl_map_lex_lt_first(dim, plevel);
1020 dep = isl_map_apply_range(isl_map_copy(acc->source[i].map),
1021 isl_map_reverse(isl_map_copy(acc->sink.map)));
1022 dep = isl_map_intersect(dep, before);
1023 mustdo = isl_set_subtract(mustdo,
1024 isl_map_range(isl_map_copy(dep)));
1025 res->dep[i].map = isl_map_union(res->dep[i].map, dep);
1028 res->may_no_source = isl_set_subtract(maydo, isl_set_copy(mustdo));
1029 res->must_no_source = mustdo;
1031 return res;
1034 /* Compute dependences for the case where there is at least one
1035 * "must" access.
1037 * The core algorithm considers all levels in which a source may precede
1038 * the sink, where a level may either be a statement level or a loop level.
1039 * The outermost statement level is 1, the first loop level is 2, etc...
1040 * The algorithm basically does the following:
1041 * for all levels l of the read access from innermost to outermost
1042 * for all sources w that may precede the sink access at that level
1043 * compute the last iteration of the source that precedes the sink access
1044 * at that level
1045 * add result to possible last accesses at level l of source w
1046 * for all sources w2 that we haven't considered yet at this level that may
1047 * also precede the sink access
1048 * for all levels l2 of w from l to innermost
1049 * for all possible last accesses dep of w at l
1050 * compute last iteration of w2 between the source and sink
1051 * of dep
1052 * add result to possible last accesses at level l of write w2
1053 * and replace possible last accesses dep by the remainder
1056 * The above algorithm is applied to the must access. During the course
1057 * of the algorithm, we keep track of sink iterations that still
1058 * need to be considered. These iterations are split into those that
1059 * haven't been matched to any source access (mustdo) and those that have only
1060 * been matched to may accesses (maydo).
1061 * At the end of each level, must-sources and may-sources that are coscheduled
1062 * with the sources of the must-dependences at that level are considered.
1063 * If any coscheduled instances are found, then corresponding may-dependences
1064 * are added and the original must-dependences are turned into may-dependences.
1065 * Afterwards, the may accesses that occur after must-dependence sources
1066 * are considered.
1067 * In particular, we consider may accesses that precede the remaining
1068 * sink iterations, moving elements from mustdo to maydo when appropriate,
1069 * and may accesses that occur between a must source and a sink of any
1070 * dependences found at the current level, turning must dependences into
1071 * may dependences when appropriate.
1074 static __isl_give isl_flow *compute_val_based_dependences(
1075 __isl_keep isl_access_info *acc)
1077 isl_ctx *ctx;
1078 isl_flow *res;
1079 isl_set *mustdo = NULL;
1080 isl_set *maydo = NULL;
1081 int level, j;
1082 int depth;
1083 isl_map **must_rel = NULL;
1084 isl_map **may_rel = NULL;
1086 if (!acc)
1087 return NULL;
1089 res = isl_flow_alloc(acc);
1090 if (!res)
1091 goto error;
1092 ctx = isl_map_get_ctx(acc->sink.map);
1094 depth = 2 * isl_map_dim(acc->sink.map, isl_dim_in) + 1;
1095 mustdo = isl_map_domain(isl_map_copy(acc->sink.map));
1096 maydo = isl_set_empty(isl_set_get_space(mustdo));
1097 if (!mustdo || !maydo)
1098 goto error;
1099 if (isl_set_plain_is_empty(mustdo))
1100 goto done;
1102 must_rel = isl_alloc_array(ctx, struct isl_map *, acc->n_must);
1103 may_rel = isl_alloc_array(ctx, struct isl_map *, acc->n_must);
1104 if (!must_rel || !may_rel)
1105 goto error;
1107 for (level = depth; level >= 1; --level) {
1108 for (j = acc->n_must-1; j >=0; --j) {
1109 isl_space *space;
1110 space = isl_map_get_space(res->dep[2 * j].map);
1111 must_rel[j] = isl_map_empty(space);
1112 may_rel[j] = isl_map_copy(must_rel[j]);
1115 for (j = acc->n_must - 1; j >= 0; --j) {
1116 struct isl_map *T;
1117 struct isl_set *rest;
1118 int plevel;
1120 plevel = acc->level_before(acc->source[j].data,
1121 acc->sink.data);
1122 if (!can_precede_at_level(plevel, level))
1123 continue;
1125 T = last_source(acc, mustdo, j, level, &rest);
1126 must_rel[j] = isl_map_union_disjoint(must_rel[j], T);
1127 mustdo = rest;
1129 intermediate_sources(acc, must_rel, j, level);
1131 T = last_source(acc, maydo, j, level, &rest);
1132 may_rel[j] = isl_map_union_disjoint(may_rel[j], T);
1133 maydo = rest;
1135 intermediate_sources(acc, may_rel, j, level);
1137 if (isl_set_plain_is_empty(mustdo) &&
1138 isl_set_plain_is_empty(maydo))
1139 break;
1141 for (j = j - 1; j >= 0; --j) {
1142 int plevel;
1144 plevel = acc->level_before(acc->source[j].data,
1145 acc->sink.data);
1146 if (!can_precede_at_level(plevel, level))
1147 continue;
1149 intermediate_sources(acc, must_rel, j, level);
1150 intermediate_sources(acc, may_rel, j, level);
1153 handle_coscheduled(acc, must_rel, may_rel, res);
1155 for (j = 0; j < acc->n_may; ++j) {
1156 int plevel;
1157 isl_map *T;
1158 isl_set *ran;
1160 plevel = acc->level_before(acc->source[acc->n_must + j].data,
1161 acc->sink.data);
1162 if (!can_precede_at_level(plevel, level))
1163 continue;
1165 T = all_sources(acc, isl_set_copy(maydo), j, level);
1166 res->dep[2 * acc->n_must + j].map =
1167 isl_map_union(res->dep[2 * acc->n_must + j].map, T);
1168 T = all_sources(acc, isl_set_copy(mustdo), j, level);
1169 ran = isl_map_range(isl_map_copy(T));
1170 res->dep[2 * acc->n_must + j].map =
1171 isl_map_union(res->dep[2 * acc->n_must + j].map, T);
1172 mustdo = isl_set_subtract(mustdo, isl_set_copy(ran));
1173 maydo = isl_set_union_disjoint(maydo, ran);
1175 T = res->dep[2 * acc->n_must + j].map;
1176 T = all_intermediate_sources(acc, T, must_rel, may_rel,
1177 j, level);
1178 res->dep[2 * acc->n_must + j].map = T;
1181 for (j = acc->n_must - 1; j >= 0; --j) {
1182 res->dep[2 * j].map =
1183 isl_map_union_disjoint(res->dep[2 * j].map,
1184 must_rel[j]);
1185 res->dep[2 * j + 1].map =
1186 isl_map_union_disjoint(res->dep[2 * j + 1].map,
1187 may_rel[j]);
1190 if (isl_set_plain_is_empty(mustdo) &&
1191 isl_set_plain_is_empty(maydo))
1192 break;
1195 free(must_rel);
1196 free(may_rel);
1197 done:
1198 res->must_no_source = mustdo;
1199 res->may_no_source = maydo;
1200 return res;
1201 error:
1202 isl_flow_free(res);
1203 isl_set_free(mustdo);
1204 isl_set_free(maydo);
1205 free(must_rel);
1206 free(may_rel);
1207 return NULL;
1210 /* Given a "sink" access, a list of n "source" accesses,
1211 * compute for each iteration of the sink access
1212 * and for each element accessed by that iteration,
1213 * the source access in the list that last accessed the
1214 * element accessed by the sink access before this sink access.
1215 * Each access is given as a map from the loop iterators
1216 * to the array indices.
1217 * The result is a list of n relations between source and sink
1218 * iterations and a subset of the domain of the sink access,
1219 * corresponding to those iterations that access an element
1220 * not previously accessed.
1222 * To deal with multi-valued sink access relations, the sink iteration
1223 * domain is first extended with dimensions that correspond to the data
1224 * space. However, these extra dimensions are not projected out again.
1225 * It is up to the caller to decide whether these dimensions should be kept.
1227 static __isl_give isl_flow *access_info_compute_flow_core(
1228 __isl_take isl_access_info *acc)
1230 struct isl_flow *res = NULL;
1232 if (!acc)
1233 return NULL;
1235 acc->sink.map = isl_map_range_map(acc->sink.map);
1236 if (!acc->sink.map)
1237 goto error;
1239 if (acc->n_must == 0)
1240 res = compute_mem_based_dependences(acc);
1241 else {
1242 acc = isl_access_info_sort_sources(acc);
1243 res = compute_val_based_dependences(acc);
1245 acc = isl_access_info_free(acc);
1246 if (!res)
1247 return NULL;
1248 if (!res->must_no_source || !res->may_no_source)
1249 goto error;
1250 return res;
1251 error:
1252 isl_access_info_free(acc);
1253 isl_flow_free(res);
1254 return NULL;
1257 /* Given a "sink" access, a list of n "source" accesses,
1258 * compute for each iteration of the sink access
1259 * and for each element accessed by that iteration,
1260 * the source access in the list that last accessed the
1261 * element accessed by the sink access before this sink access.
1262 * Each access is given as a map from the loop iterators
1263 * to the array indices.
1264 * The result is a list of n relations between source and sink
1265 * iterations and a subset of the domain of the sink access,
1266 * corresponding to those iterations that access an element
1267 * not previously accessed.
1269 * To deal with multi-valued sink access relations,
1270 * access_info_compute_flow_core extends the sink iteration domain
1271 * with dimensions that correspond to the data space. These extra dimensions
1272 * are projected out from the result of access_info_compute_flow_core.
1274 __isl_give isl_flow *isl_access_info_compute_flow(__isl_take isl_access_info *acc)
1276 int j;
1277 struct isl_flow *res;
1279 if (!acc)
1280 return NULL;
1282 acc->domain_map = isl_map_domain_map(isl_map_copy(acc->sink.map));
1283 res = access_info_compute_flow_core(acc);
1284 if (!res)
1285 return NULL;
1287 for (j = 0; j < res->n_source; ++j) {
1288 res->dep[j].map = isl_map_range_factor_domain(res->dep[j].map);
1289 if (!res->dep[j].map)
1290 goto error;
1293 return res;
1294 error:
1295 isl_flow_free(res);
1296 return NULL;
1300 /* Keep track of some information about a schedule for a given
1301 * access. In particular, keep track of which dimensions
1302 * have a constant value and of the actual constant values.
1304 struct isl_sched_info {
1305 int *is_cst;
1306 isl_vec *cst;
1309 static void sched_info_free(__isl_take struct isl_sched_info *info)
1311 if (!info)
1312 return;
1313 isl_vec_free(info->cst);
1314 free(info->is_cst);
1315 free(info);
1318 /* Extract information on the constant dimensions of the schedule
1319 * for a given access. The "map" is of the form
1321 * [S -> D] -> A
1323 * with S the schedule domain, D the iteration domain and A the data domain.
1325 static __isl_give struct isl_sched_info *sched_info_alloc(
1326 __isl_keep isl_map *map)
1328 isl_ctx *ctx;
1329 isl_space *dim;
1330 struct isl_sched_info *info;
1331 int i, n;
1333 if (!map)
1334 return NULL;
1336 dim = isl_space_unwrap(isl_space_domain(isl_map_get_space(map)));
1337 if (!dim)
1338 return NULL;
1339 n = isl_space_dim(dim, isl_dim_in);
1340 isl_space_free(dim);
1342 ctx = isl_map_get_ctx(map);
1343 info = isl_alloc_type(ctx, struct isl_sched_info);
1344 if (!info)
1345 return NULL;
1346 info->is_cst = isl_alloc_array(ctx, int, n);
1347 info->cst = isl_vec_alloc(ctx, n);
1348 if (n && (!info->is_cst || !info->cst))
1349 goto error;
1351 for (i = 0; i < n; ++i) {
1352 isl_val *v;
1354 v = isl_map_plain_get_val_if_fixed(map, isl_dim_in, i);
1355 if (!v)
1356 goto error;
1357 info->is_cst[i] = !isl_val_is_nan(v);
1358 if (info->is_cst[i])
1359 info->cst = isl_vec_set_element_val(info->cst, i, v);
1360 else
1361 isl_val_free(v);
1364 return info;
1365 error:
1366 sched_info_free(info);
1367 return NULL;
1370 /* The different types of access relations that isl_union_access_info
1371 * keeps track of.
1373 * "isl_access_sink" represents the sink accesses.
1374 * "isl_access_must_source" represents the definite source accesses.
1375 * "isl_access_may_source" represents the possible source accesses.
1376 * "isl_access_kill" represents the kills.
1378 * isl_access_sink is sometimes treated differently and
1379 * should therefore appear first.
1381 enum isl_access_type {
1382 isl_access_sink,
1383 isl_access_must_source,
1384 isl_access_may_source,
1385 isl_access_kill,
1386 isl_access_end
1389 /* This structure represents the input for a dependence analysis computation.
1391 * "access" contains the access relations.
1393 * "schedule" or "schedule_map" represents the execution order.
1394 * Exactly one of these fields should be NULL. The other field
1395 * determines the execution order.
1397 * The domains of these four maps refer to the same iteration spaces(s).
1398 * The ranges of the first three maps also refer to the same data space(s).
1400 * After a call to isl_union_access_info_introduce_schedule,
1401 * the "schedule_map" field no longer contains useful information.
1403 struct isl_union_access_info {
1404 isl_union_map *access[isl_access_end];
1406 isl_schedule *schedule;
1407 isl_union_map *schedule_map;
1410 /* Free "access" and return NULL.
1412 __isl_null isl_union_access_info *isl_union_access_info_free(
1413 __isl_take isl_union_access_info *access)
1415 enum isl_access_type i;
1417 if (!access)
1418 return NULL;
1420 for (i = isl_access_sink; i < isl_access_end; ++i)
1421 isl_union_map_free(access->access[i]);
1422 isl_schedule_free(access->schedule);
1423 isl_union_map_free(access->schedule_map);
1424 free(access);
1426 return NULL;
1429 /* Return the isl_ctx to which "access" belongs.
1431 isl_ctx *isl_union_access_info_get_ctx(__isl_keep isl_union_access_info *access)
1433 if (!access)
1434 return NULL;
1435 return isl_union_map_get_ctx(access->access[isl_access_sink]);
1438 /* Construct an empty (invalid) isl_union_access_info object.
1439 * The caller is responsible for setting the sink access relation and
1440 * initializing all the other fields, e.g., by calling
1441 * isl_union_access_info_init.
1443 static __isl_give isl_union_access_info *isl_union_access_info_alloc(
1444 isl_ctx *ctx)
1446 return isl_calloc_type(ctx, isl_union_access_info);
1449 /* Initialize all the fields of "info", except the sink access relation,
1450 * which is assumed to have been set by the caller.
1452 * By default, we use the schedule field of the isl_union_access_info,
1453 * but this may be overridden by a call
1454 * to isl_union_access_info_set_schedule_map.
1456 static __isl_give isl_union_access_info *isl_union_access_info_init(
1457 __isl_take isl_union_access_info *info)
1459 isl_space *space;
1460 isl_union_map *empty;
1461 enum isl_access_type i;
1463 if (!info)
1464 return NULL;
1465 if (!info->access[isl_access_sink])
1466 return isl_union_access_info_free(info);
1468 space = isl_union_map_get_space(info->access[isl_access_sink]);
1469 empty = isl_union_map_empty(isl_space_copy(space));
1470 for (i = isl_access_sink + 1; i < isl_access_end; ++i)
1471 if (!info->access[i])
1472 info->access[i] = isl_union_map_copy(empty);
1473 isl_union_map_free(empty);
1474 if (!info->schedule && !info->schedule_map)
1475 info->schedule = isl_schedule_empty(isl_space_copy(space));
1476 isl_space_free(space);
1478 for (i = isl_access_sink + 1; i < isl_access_end; ++i)
1479 if (!info->access[i])
1480 return isl_union_access_info_free(info);
1481 if (!info->schedule && !info->schedule_map)
1482 return isl_union_access_info_free(info);
1484 return info;
1487 /* Create a new isl_union_access_info with the given sink accesses and
1488 * and no other accesses or schedule information.
1490 __isl_give isl_union_access_info *isl_union_access_info_from_sink(
1491 __isl_take isl_union_map *sink)
1493 isl_ctx *ctx;
1494 isl_union_access_info *access;
1496 if (!sink)
1497 return NULL;
1498 ctx = isl_union_map_get_ctx(sink);
1499 access = isl_union_access_info_alloc(ctx);
1500 if (!access)
1501 goto error;
1502 access->access[isl_access_sink] = sink;
1503 return isl_union_access_info_init(access);
1504 error:
1505 isl_union_map_free(sink);
1506 return NULL;
1509 /* Replace the access relation of type "type" of "info" by "access".
1511 static __isl_give isl_union_access_info *isl_union_access_info_set(
1512 __isl_take isl_union_access_info *info,
1513 enum isl_access_type type, __isl_take isl_union_map *access)
1515 if (!info || !access)
1516 goto error;
1518 isl_union_map_free(info->access[type]);
1519 info->access[type] = access;
1521 return info;
1522 error:
1523 isl_union_access_info_free(info);
1524 isl_union_map_free(access);
1525 return NULL;
1528 /* Replace the definite source accesses of "access" by "must_source".
1530 __isl_give isl_union_access_info *isl_union_access_info_set_must_source(
1531 __isl_take isl_union_access_info *access,
1532 __isl_take isl_union_map *must_source)
1534 return isl_union_access_info_set(access, isl_access_must_source,
1535 must_source);
1538 /* Replace the possible source accesses of "access" by "may_source".
1540 __isl_give isl_union_access_info *isl_union_access_info_set_may_source(
1541 __isl_take isl_union_access_info *access,
1542 __isl_take isl_union_map *may_source)
1544 return isl_union_access_info_set(access, isl_access_may_source,
1545 may_source);
1548 /* Replace the kills of "info" by "kill".
1550 __isl_give isl_union_access_info *isl_union_access_info_set_kill(
1551 __isl_take isl_union_access_info *info, __isl_take isl_union_map *kill)
1553 return isl_union_access_info_set(info, isl_access_kill, kill);
1556 /* Return the access relation of type "type" of "info".
1558 static __isl_give isl_union_map *isl_union_access_info_get(
1559 __isl_keep isl_union_access_info *info, enum isl_access_type type)
1561 if (!info)
1562 return NULL;
1563 return isl_union_map_copy(info->access[type]);
1566 /* Return the definite source accesses of "info".
1568 __isl_give isl_union_map *isl_union_access_info_get_must_source(
1569 __isl_keep isl_union_access_info *info)
1571 return isl_union_access_info_get(info, isl_access_must_source);
1574 /* Return the possible source accesses of "info".
1576 __isl_give isl_union_map *isl_union_access_info_get_may_source(
1577 __isl_keep isl_union_access_info *info)
1579 return isl_union_access_info_get(info, isl_access_may_source);
1582 /* Return the kills of "info".
1584 __isl_give isl_union_map *isl_union_access_info_get_kill(
1585 __isl_keep isl_union_access_info *info)
1587 return isl_union_access_info_get(info, isl_access_kill);
1590 /* Does "info" specify any kills?
1592 static isl_bool isl_union_access_has_kill(
1593 __isl_keep isl_union_access_info *info)
1595 isl_bool empty;
1597 if (!info)
1598 return isl_bool_error;
1599 empty = isl_union_map_is_empty(info->access[isl_access_kill]);
1600 return isl_bool_not(empty);
1603 /* Replace the schedule of "access" by "schedule".
1604 * Also free the schedule_map in case it was set last.
1606 __isl_give isl_union_access_info *isl_union_access_info_set_schedule(
1607 __isl_take isl_union_access_info *access,
1608 __isl_take isl_schedule *schedule)
1610 if (!access || !schedule)
1611 goto error;
1613 access->schedule_map = isl_union_map_free(access->schedule_map);
1614 isl_schedule_free(access->schedule);
1615 access->schedule = schedule;
1617 return access;
1618 error:
1619 isl_union_access_info_free(access);
1620 isl_schedule_free(schedule);
1621 return NULL;
1624 /* Replace the schedule map of "access" by "schedule_map".
1625 * Also free the schedule in case it was set last.
1627 __isl_give isl_union_access_info *isl_union_access_info_set_schedule_map(
1628 __isl_take isl_union_access_info *access,
1629 __isl_take isl_union_map *schedule_map)
1631 if (!access || !schedule_map)
1632 goto error;
1634 isl_union_map_free(access->schedule_map);
1635 access->schedule = isl_schedule_free(access->schedule);
1636 access->schedule_map = schedule_map;
1638 return access;
1639 error:
1640 isl_union_access_info_free(access);
1641 isl_union_map_free(schedule_map);
1642 return NULL;
1645 __isl_give isl_union_access_info *isl_union_access_info_copy(
1646 __isl_keep isl_union_access_info *access)
1648 isl_union_access_info *copy;
1649 enum isl_access_type i;
1651 if (!access)
1652 return NULL;
1653 copy = isl_union_access_info_from_sink(
1654 isl_union_map_copy(access->access[isl_access_sink]));
1655 for (i = isl_access_sink + 1; i < isl_access_end; ++i)
1656 copy = isl_union_access_info_set(copy, i,
1657 isl_union_map_copy(access->access[i]));
1658 if (access->schedule)
1659 copy = isl_union_access_info_set_schedule(copy,
1660 isl_schedule_copy(access->schedule));
1661 else
1662 copy = isl_union_access_info_set_schedule_map(copy,
1663 isl_union_map_copy(access->schedule_map));
1665 return copy;
1668 /* Print a key-value pair of a YAML mapping to "p",
1669 * with key "name" and value "umap".
1671 static __isl_give isl_printer *print_union_map_field(__isl_take isl_printer *p,
1672 const char *name, __isl_keep isl_union_map *umap)
1674 p = isl_printer_print_str(p, name);
1675 p = isl_printer_yaml_next(p);
1676 p = isl_printer_print_str(p, "\"");
1677 p = isl_printer_print_union_map(p, umap);
1678 p = isl_printer_print_str(p, "\"");
1679 p = isl_printer_yaml_next(p);
1681 return p;
1684 /* An enumeration of the various keys that may appear in a YAML mapping
1685 * of an isl_union_access_info object.
1686 * The keys for the access relation types are assumed to have the same values
1687 * as the access relation types in isl_access_type.
1689 enum isl_ai_key {
1690 isl_ai_key_error = -1,
1691 isl_ai_key_sink = isl_access_sink,
1692 isl_ai_key_must_source = isl_access_must_source,
1693 isl_ai_key_may_source = isl_access_may_source,
1694 isl_ai_key_kill = isl_access_kill,
1695 isl_ai_key_schedule_map,
1696 isl_ai_key_schedule,
1697 isl_ai_key_end
1700 /* Textual representations of the YAML keys for an isl_union_access_info
1701 * object.
1703 static char *key_str[] = {
1704 [isl_ai_key_sink] = "sink",
1705 [isl_ai_key_must_source] = "must_source",
1706 [isl_ai_key_may_source] = "may_source",
1707 [isl_ai_key_kill] = "kill",
1708 [isl_ai_key_schedule_map] = "schedule_map",
1709 [isl_ai_key_schedule] = "schedule",
1712 /* Print a key-value pair corresponding to the access relation of type "type"
1713 * of a YAML mapping of "info" to "p".
1715 * The sink access relation is always printed, but any other access relation
1716 * is only printed if it is non-empty.
1718 static __isl_give isl_printer *print_access_field(__isl_take isl_printer *p,
1719 __isl_keep isl_union_access_info *info, enum isl_access_type type)
1721 if (type != isl_access_sink) {
1722 isl_bool empty;
1724 empty = isl_union_map_is_empty(info->access[type]);
1725 if (empty < 0)
1726 return isl_printer_free(p);
1727 if (empty)
1728 return p;
1730 return print_union_map_field(p, key_str[type], info->access[type]);
1733 /* Print the information contained in "access" to "p".
1734 * The information is printed as a YAML document.
1736 __isl_give isl_printer *isl_printer_print_union_access_info(
1737 __isl_take isl_printer *p, __isl_keep isl_union_access_info *access)
1739 enum isl_access_type i;
1741 if (!access)
1742 return isl_printer_free(p);
1744 p = isl_printer_yaml_start_mapping(p);
1745 for (i = isl_access_sink; i < isl_access_end; ++i)
1746 p = print_access_field(p, access, i);
1747 if (access->schedule) {
1748 p = isl_printer_print_str(p, key_str[isl_ai_key_schedule]);
1749 p = isl_printer_yaml_next(p);
1750 p = isl_printer_print_schedule(p, access->schedule);
1751 p = isl_printer_yaml_next(p);
1752 } else {
1753 p = print_union_map_field(p, key_str[isl_ai_key_schedule_map],
1754 access->schedule_map);
1756 p = isl_printer_yaml_end_mapping(p);
1758 return p;
1761 /* Return a string representation of the information in "access".
1762 * The information is printed in flow format.
1764 __isl_give char *isl_union_access_info_to_str(
1765 __isl_keep isl_union_access_info *access)
1767 isl_printer *p;
1768 char *s;
1770 if (!access)
1771 return NULL;
1773 p = isl_printer_to_str(isl_union_access_info_get_ctx(access));
1774 p = isl_printer_set_yaml_style(p, ISL_YAML_STYLE_FLOW);
1775 p = isl_printer_print_union_access_info(p, access);
1776 s = isl_printer_get_str(p);
1777 isl_printer_free(p);
1779 return s;
1782 #undef KEY
1783 #define KEY enum isl_ai_key
1784 #undef KEY_ERROR
1785 #define KEY_ERROR isl_ai_key_error
1786 #undef KEY_END
1787 #define KEY_END isl_ai_key_end
1788 #include "extract_key.c"
1790 #undef BASE
1791 #define BASE union_map
1792 #include "read_in_string_templ.c"
1794 /* Read an isl_union_access_info object from "s".
1796 * Start off with an empty (invalid) isl_union_access_info object and
1797 * then fill up the fields based on the input.
1798 * The input needs to contain at least a description of the sink
1799 * access relation as well as some form of schedule.
1800 * The other access relations are set to empty relations
1801 * by isl_union_access_info_init if they are not specified in the input.
1803 __isl_give isl_union_access_info *isl_stream_read_union_access_info(
1804 isl_stream *s)
1806 isl_ctx *ctx;
1807 isl_union_access_info *info;
1808 int more;
1809 int sink_set = 0;
1810 int schedule_set = 0;
1812 if (isl_stream_yaml_read_start_mapping(s))
1813 return NULL;
1815 ctx = isl_stream_get_ctx(s);
1816 info = isl_union_access_info_alloc(ctx);
1817 while ((more = isl_stream_yaml_next(s)) > 0) {
1818 enum isl_ai_key key;
1819 isl_union_map *access, *schedule_map;
1820 isl_schedule *schedule;
1822 key = get_key(s);
1823 if (isl_stream_yaml_next(s) < 0)
1824 return isl_union_access_info_free(info);
1825 switch (key) {
1826 case isl_ai_key_end:
1827 case isl_ai_key_error:
1828 return isl_union_access_info_free(info);
1829 case isl_ai_key_sink:
1830 sink_set = 1;
1831 case isl_ai_key_must_source:
1832 case isl_ai_key_may_source:
1833 case isl_ai_key_kill:
1834 access = read_union_map(s);
1835 info = isl_union_access_info_set(info, key, access);
1836 if (!info)
1837 return NULL;
1838 break;
1839 case isl_ai_key_schedule_map:
1840 schedule_set = 1;
1841 schedule_map = read_union_map(s);
1842 info = isl_union_access_info_set_schedule_map(info,
1843 schedule_map);
1844 if (!info)
1845 return NULL;
1846 break;
1847 case isl_ai_key_schedule:
1848 schedule_set = 1;
1849 schedule = isl_stream_read_schedule(s);
1850 info = isl_union_access_info_set_schedule(info,
1851 schedule);
1852 if (!info)
1853 return NULL;
1854 break;
1857 if (more < 0)
1858 return isl_union_access_info_free(info);
1860 if (isl_stream_yaml_read_end_mapping(s) < 0) {
1861 isl_stream_error(s, NULL, "unexpected extra elements");
1862 return isl_union_access_info_free(info);
1865 if (!sink_set) {
1866 isl_stream_error(s, NULL, "no sink specified");
1867 return isl_union_access_info_free(info);
1870 if (!schedule_set) {
1871 isl_stream_error(s, NULL, "no schedule specified");
1872 return isl_union_access_info_free(info);
1875 return isl_union_access_info_init(info);
1878 /* Read an isl_union_access_info object from the file "input".
1880 __isl_give isl_union_access_info *isl_union_access_info_read_from_file(
1881 isl_ctx *ctx, FILE *input)
1883 isl_stream *s;
1884 isl_union_access_info *access;
1886 s = isl_stream_new_file(ctx, input);
1887 if (!s)
1888 return NULL;
1889 access = isl_stream_read_union_access_info(s);
1890 isl_stream_free(s);
1892 return access;
1895 /* Update the fields of "access" such that they all have the same parameters,
1896 * keeping in mind that the schedule_map field may be NULL and ignoring
1897 * the schedule field.
1899 static __isl_give isl_union_access_info *isl_union_access_info_align_params(
1900 __isl_take isl_union_access_info *access)
1902 isl_space *space;
1903 enum isl_access_type i;
1905 if (!access)
1906 return NULL;
1908 space = isl_union_map_get_space(access->access[isl_access_sink]);
1909 for (i = isl_access_sink + 1; i < isl_access_end; ++i)
1910 space = isl_space_align_params(space,
1911 isl_union_map_get_space(access->access[i]));
1912 if (access->schedule_map)
1913 space = isl_space_align_params(space,
1914 isl_union_map_get_space(access->schedule_map));
1915 for (i = isl_access_sink; i < isl_access_end; ++i)
1916 access->access[i] =
1917 isl_union_map_align_params(access->access[i],
1918 isl_space_copy(space));
1919 if (!access->schedule_map) {
1920 isl_space_free(space);
1921 } else {
1922 access->schedule_map =
1923 isl_union_map_align_params(access->schedule_map, space);
1924 if (!access->schedule_map)
1925 return isl_union_access_info_free(access);
1928 for (i = isl_access_sink; i < isl_access_end; ++i)
1929 if (!access->access[i])
1930 return isl_union_access_info_free(access);
1932 return access;
1935 /* Prepend the schedule dimensions to the iteration domains.
1937 * That is, if the schedule is of the form
1939 * D -> S
1941 * while the access relations are of the form
1943 * D -> A
1945 * then the updated access relations are of the form
1947 * [S -> D] -> A
1949 * The schedule map is also replaced by the map
1951 * [S -> D] -> D
1953 * that is used during the internal computation.
1954 * Neither the original schedule map nor this updated schedule map
1955 * are used after the call to this function.
1957 static __isl_give isl_union_access_info *
1958 isl_union_access_info_introduce_schedule(
1959 __isl_take isl_union_access_info *access)
1961 isl_union_map *sm;
1962 enum isl_access_type i;
1964 if (!access)
1965 return NULL;
1967 sm = isl_union_map_reverse(access->schedule_map);
1968 sm = isl_union_map_range_map(sm);
1969 for (i = isl_access_sink; i < isl_access_end; ++i)
1970 access->access[i] =
1971 isl_union_map_apply_range(isl_union_map_copy(sm),
1972 access->access[i]);
1973 access->schedule_map = sm;
1975 for (i = isl_access_sink; i < isl_access_end; ++i)
1976 if (!access->access[i])
1977 return isl_union_access_info_free(access);
1978 if (!access->schedule_map)
1979 return isl_union_access_info_free(access);
1981 return access;
1984 /* This structure represents the result of a dependence analysis computation.
1986 * "must_dep" represents the full definite dependences
1987 * "may_dep" represents the full non-definite dependences.
1988 * Both are of the form
1990 * [Source] -> [[Sink -> Data]]
1992 * (after the schedule dimensions have been projected out).
1993 * "must_no_source" represents the subset of the sink accesses for which
1994 * definitely no source was found.
1995 * "may_no_source" represents the subset of the sink accesses for which
1996 * possibly, but not definitely, no source was found.
1998 struct isl_union_flow {
1999 isl_union_map *must_dep;
2000 isl_union_map *may_dep;
2001 isl_union_map *must_no_source;
2002 isl_union_map *may_no_source;
2005 /* Return the isl_ctx to which "flow" belongs.
2007 isl_ctx *isl_union_flow_get_ctx(__isl_keep isl_union_flow *flow)
2009 return flow ? isl_union_map_get_ctx(flow->must_dep) : NULL;
2012 /* Free "flow" and return NULL.
2014 __isl_null isl_union_flow *isl_union_flow_free(__isl_take isl_union_flow *flow)
2016 if (!flow)
2017 return NULL;
2018 isl_union_map_free(flow->must_dep);
2019 isl_union_map_free(flow->may_dep);
2020 isl_union_map_free(flow->must_no_source);
2021 isl_union_map_free(flow->may_no_source);
2022 free(flow);
2023 return NULL;
2026 void isl_union_flow_dump(__isl_keep isl_union_flow *flow)
2028 if (!flow)
2029 return;
2031 fprintf(stderr, "must dependences: ");
2032 isl_union_map_dump(flow->must_dep);
2033 fprintf(stderr, "may dependences: ");
2034 isl_union_map_dump(flow->may_dep);
2035 fprintf(stderr, "must no source: ");
2036 isl_union_map_dump(flow->must_no_source);
2037 fprintf(stderr, "may no source: ");
2038 isl_union_map_dump(flow->may_no_source);
2041 /* Return the full definite dependences in "flow", with accessed elements.
2043 __isl_give isl_union_map *isl_union_flow_get_full_must_dependence(
2044 __isl_keep isl_union_flow *flow)
2046 if (!flow)
2047 return NULL;
2048 return isl_union_map_copy(flow->must_dep);
2051 /* Return the full possible dependences in "flow", including the definite
2052 * dependences, with accessed elements.
2054 __isl_give isl_union_map *isl_union_flow_get_full_may_dependence(
2055 __isl_keep isl_union_flow *flow)
2057 if (!flow)
2058 return NULL;
2059 return isl_union_map_union(isl_union_map_copy(flow->must_dep),
2060 isl_union_map_copy(flow->may_dep));
2063 /* Return the definite dependences in "flow", without the accessed elements.
2065 __isl_give isl_union_map *isl_union_flow_get_must_dependence(
2066 __isl_keep isl_union_flow *flow)
2068 isl_union_map *dep;
2070 if (!flow)
2071 return NULL;
2072 dep = isl_union_map_copy(flow->must_dep);
2073 return isl_union_map_range_factor_domain(dep);
2076 /* Return the possible dependences in "flow", including the definite
2077 * dependences, without the accessed elements.
2079 __isl_give isl_union_map *isl_union_flow_get_may_dependence(
2080 __isl_keep isl_union_flow *flow)
2082 isl_union_map *dep;
2084 if (!flow)
2085 return NULL;
2086 dep = isl_union_map_union(isl_union_map_copy(flow->must_dep),
2087 isl_union_map_copy(flow->may_dep));
2088 return isl_union_map_range_factor_domain(dep);
2091 /* Return the non-definite dependences in "flow".
2093 static __isl_give isl_union_map *isl_union_flow_get_non_must_dependence(
2094 __isl_keep isl_union_flow *flow)
2096 if (!flow)
2097 return NULL;
2098 return isl_union_map_copy(flow->may_dep);
2101 /* Return the subset of the sink accesses for which definitely
2102 * no source was found.
2104 __isl_give isl_union_map *isl_union_flow_get_must_no_source(
2105 __isl_keep isl_union_flow *flow)
2107 if (!flow)
2108 return NULL;
2109 return isl_union_map_copy(flow->must_no_source);
2112 /* Return the subset of the sink accesses for which possibly
2113 * no source was found, including those for which definitely
2114 * no source was found.
2116 __isl_give isl_union_map *isl_union_flow_get_may_no_source(
2117 __isl_keep isl_union_flow *flow)
2119 if (!flow)
2120 return NULL;
2121 return isl_union_map_union(isl_union_map_copy(flow->must_no_source),
2122 isl_union_map_copy(flow->may_no_source));
2125 /* Return the subset of the sink accesses for which possibly, but not
2126 * definitely, no source was found.
2128 static __isl_give isl_union_map *isl_union_flow_get_non_must_no_source(
2129 __isl_keep isl_union_flow *flow)
2131 if (!flow)
2132 return NULL;
2133 return isl_union_map_copy(flow->may_no_source);
2136 /* Create a new isl_union_flow object, initialized with empty
2137 * dependence relations and sink subsets.
2139 static __isl_give isl_union_flow *isl_union_flow_alloc(
2140 __isl_take isl_space *space)
2142 isl_ctx *ctx;
2143 isl_union_map *empty;
2144 isl_union_flow *flow;
2146 if (!space)
2147 return NULL;
2148 ctx = isl_space_get_ctx(space);
2149 flow = isl_alloc_type(ctx, isl_union_flow);
2150 if (!flow)
2151 goto error;
2153 empty = isl_union_map_empty(space);
2154 flow->must_dep = isl_union_map_copy(empty);
2155 flow->may_dep = isl_union_map_copy(empty);
2156 flow->must_no_source = isl_union_map_copy(empty);
2157 flow->may_no_source = empty;
2159 if (!flow->must_dep || !flow->may_dep ||
2160 !flow->must_no_source || !flow->may_no_source)
2161 return isl_union_flow_free(flow);
2163 return flow;
2164 error:
2165 isl_space_free(space);
2166 return NULL;
2169 /* Copy this isl_union_flow object.
2171 __isl_give isl_union_flow *isl_union_flow_copy(__isl_keep isl_union_flow *flow)
2173 isl_union_flow *copy;
2175 if (!flow)
2176 return NULL;
2178 copy = isl_union_flow_alloc(isl_union_map_get_space(flow->must_dep));
2180 if (!copy)
2181 return NULL;
2183 copy->must_dep = isl_union_map_union(copy->must_dep,
2184 isl_union_map_copy(flow->must_dep));
2185 copy->may_dep = isl_union_map_union(copy->may_dep,
2186 isl_union_map_copy(flow->may_dep));
2187 copy->must_no_source = isl_union_map_union(copy->must_no_source,
2188 isl_union_map_copy(flow->must_no_source));
2189 copy->may_no_source = isl_union_map_union(copy->may_no_source,
2190 isl_union_map_copy(flow->may_no_source));
2192 if (!copy->must_dep || !copy->may_dep ||
2193 !copy->must_no_source || !copy->may_no_source)
2194 return isl_union_flow_free(copy);
2196 return copy;
2199 /* Drop the schedule dimensions from the iteration domains in "flow".
2200 * In particular, the schedule dimensions have been prepended
2201 * to the iteration domains prior to the dependence analysis by
2202 * replacing the iteration domain D, by the wrapped map [S -> D].
2203 * Replace these wrapped maps by the original D.
2205 * In particular, the dependences computed by access_info_compute_flow_core
2206 * are of the form
2208 * [S -> D] -> [[S' -> D'] -> A]
2210 * The schedule dimensions are projected out by first currying the range,
2211 * resulting in
2213 * [S -> D] -> [S' -> [D' -> A]]
2215 * and then computing the factor range
2217 * D -> [D' -> A]
2219 static __isl_give isl_union_flow *isl_union_flow_drop_schedule(
2220 __isl_take isl_union_flow *flow)
2222 if (!flow)
2223 return NULL;
2225 flow->must_dep = isl_union_map_range_curry(flow->must_dep);
2226 flow->must_dep = isl_union_map_factor_range(flow->must_dep);
2227 flow->may_dep = isl_union_map_range_curry(flow->may_dep);
2228 flow->may_dep = isl_union_map_factor_range(flow->may_dep);
2229 flow->must_no_source =
2230 isl_union_map_domain_factor_range(flow->must_no_source);
2231 flow->may_no_source =
2232 isl_union_map_domain_factor_range(flow->may_no_source);
2234 if (!flow->must_dep || !flow->may_dep ||
2235 !flow->must_no_source || !flow->may_no_source)
2236 return isl_union_flow_free(flow);
2238 return flow;
2241 struct isl_compute_flow_data {
2242 isl_union_map *must_source;
2243 isl_union_map *may_source;
2244 isl_union_flow *flow;
2246 int count;
2247 int must;
2248 isl_space *dim;
2249 struct isl_sched_info *sink_info;
2250 struct isl_sched_info **source_info;
2251 isl_access_info *accesses;
2254 static isl_stat count_matching_array(__isl_take isl_map *map, void *user)
2256 int eq;
2257 isl_space *dim;
2258 struct isl_compute_flow_data *data;
2260 data = (struct isl_compute_flow_data *)user;
2262 dim = isl_space_range(isl_map_get_space(map));
2264 eq = isl_space_is_equal(dim, data->dim);
2266 isl_space_free(dim);
2267 isl_map_free(map);
2269 if (eq < 0)
2270 return isl_stat_error;
2271 if (eq)
2272 data->count++;
2274 return isl_stat_ok;
2277 static isl_stat collect_matching_array(__isl_take isl_map *map, void *user)
2279 int eq;
2280 isl_space *dim;
2281 struct isl_sched_info *info;
2282 struct isl_compute_flow_data *data;
2284 data = (struct isl_compute_flow_data *)user;
2286 dim = isl_space_range(isl_map_get_space(map));
2288 eq = isl_space_is_equal(dim, data->dim);
2290 isl_space_free(dim);
2292 if (eq < 0)
2293 goto error;
2294 if (!eq) {
2295 isl_map_free(map);
2296 return isl_stat_ok;
2299 info = sched_info_alloc(map);
2300 data->source_info[data->count] = info;
2302 data->accesses = isl_access_info_add_source(data->accesses,
2303 map, data->must, info);
2305 data->count++;
2307 return isl_stat_ok;
2308 error:
2309 isl_map_free(map);
2310 return isl_stat_error;
2313 /* Determine the shared nesting level and the "textual order" of
2314 * the given accesses.
2316 * We first determine the minimal schedule dimension for both accesses.
2318 * If among those dimensions, we can find one where both have a fixed
2319 * value and if moreover those values are different, then the previous
2320 * dimension is the last shared nesting level and the textual order
2321 * is determined based on the order of the fixed values.
2322 * If no such fixed values can be found, then we set the shared
2323 * nesting level to the minimal schedule dimension, with no textual ordering.
2325 static int before(void *first, void *second)
2327 struct isl_sched_info *info1 = first;
2328 struct isl_sched_info *info2 = second;
2329 int n1, n2;
2330 int i;
2332 n1 = isl_vec_size(info1->cst);
2333 n2 = isl_vec_size(info2->cst);
2335 if (n2 < n1)
2336 n1 = n2;
2338 for (i = 0; i < n1; ++i) {
2339 int r;
2340 int cmp;
2342 if (!info1->is_cst[i])
2343 continue;
2344 if (!info2->is_cst[i])
2345 continue;
2346 cmp = isl_vec_cmp_element(info1->cst, info2->cst, i);
2347 if (cmp == 0)
2348 continue;
2350 r = 2 * i + (cmp < 0);
2352 return r;
2355 return 2 * n1;
2358 /* Check if the given two accesses may be coscheduled.
2359 * If so, return 1. Otherwise return 0.
2361 * Two accesses may only be coscheduled if the fixed schedule
2362 * coordinates have the same values.
2364 static int coscheduled(void *first, void *second)
2366 struct isl_sched_info *info1 = first;
2367 struct isl_sched_info *info2 = second;
2368 int n1, n2;
2369 int i;
2371 n1 = isl_vec_size(info1->cst);
2372 n2 = isl_vec_size(info2->cst);
2374 if (n2 < n1)
2375 n1 = n2;
2377 for (i = 0; i < n1; ++i) {
2378 int cmp;
2380 if (!info1->is_cst[i])
2381 continue;
2382 if (!info2->is_cst[i])
2383 continue;
2384 cmp = isl_vec_cmp_element(info1->cst, info2->cst, i);
2385 if (cmp != 0)
2386 return 0;
2389 return 1;
2392 /* Given a sink access, look for all the source accesses that access
2393 * the same array and perform dataflow analysis on them using
2394 * isl_access_info_compute_flow_core.
2396 static isl_stat compute_flow(__isl_take isl_map *map, void *user)
2398 int i;
2399 isl_ctx *ctx;
2400 struct isl_compute_flow_data *data;
2401 isl_flow *flow;
2402 isl_union_flow *df;
2404 data = (struct isl_compute_flow_data *)user;
2405 df = data->flow;
2407 ctx = isl_map_get_ctx(map);
2409 data->accesses = NULL;
2410 data->sink_info = NULL;
2411 data->source_info = NULL;
2412 data->count = 0;
2413 data->dim = isl_space_range(isl_map_get_space(map));
2415 if (isl_union_map_foreach_map(data->must_source,
2416 &count_matching_array, data) < 0)
2417 goto error;
2418 if (isl_union_map_foreach_map(data->may_source,
2419 &count_matching_array, data) < 0)
2420 goto error;
2422 data->sink_info = sched_info_alloc(map);
2423 data->source_info = isl_calloc_array(ctx, struct isl_sched_info *,
2424 data->count);
2426 data->accesses = isl_access_info_alloc(isl_map_copy(map),
2427 data->sink_info, &before, data->count);
2428 if (!data->sink_info || (data->count && !data->source_info) ||
2429 !data->accesses)
2430 goto error;
2431 data->accesses->coscheduled = &coscheduled;
2432 data->count = 0;
2433 data->must = 1;
2434 if (isl_union_map_foreach_map(data->must_source,
2435 &collect_matching_array, data) < 0)
2436 goto error;
2437 data->must = 0;
2438 if (isl_union_map_foreach_map(data->may_source,
2439 &collect_matching_array, data) < 0)
2440 goto error;
2442 flow = access_info_compute_flow_core(data->accesses);
2443 data->accesses = NULL;
2445 if (!flow)
2446 goto error;
2448 df->must_no_source = isl_union_map_union(df->must_no_source,
2449 isl_union_map_from_map(isl_flow_get_no_source(flow, 1)));
2450 df->may_no_source = isl_union_map_union(df->may_no_source,
2451 isl_union_map_from_map(isl_flow_get_no_source(flow, 0)));
2453 for (i = 0; i < flow->n_source; ++i) {
2454 isl_union_map *dep;
2455 dep = isl_union_map_from_map(isl_map_copy(flow->dep[i].map));
2456 if (flow->dep[i].must)
2457 df->must_dep = isl_union_map_union(df->must_dep, dep);
2458 else
2459 df->may_dep = isl_union_map_union(df->may_dep, dep);
2462 isl_flow_free(flow);
2464 sched_info_free(data->sink_info);
2465 if (data->source_info) {
2466 for (i = 0; i < data->count; ++i)
2467 sched_info_free(data->source_info[i]);
2468 free(data->source_info);
2470 isl_space_free(data->dim);
2471 isl_map_free(map);
2473 return isl_stat_ok;
2474 error:
2475 isl_access_info_free(data->accesses);
2476 sched_info_free(data->sink_info);
2477 if (data->source_info) {
2478 for (i = 0; i < data->count; ++i)
2479 sched_info_free(data->source_info[i]);
2480 free(data->source_info);
2482 isl_space_free(data->dim);
2483 isl_map_free(map);
2485 return isl_stat_error;
2488 /* Add the kills of "info" to the must-sources.
2490 static __isl_give isl_union_access_info *
2491 isl_union_access_info_add_kill_to_must_source(
2492 __isl_take isl_union_access_info *info)
2494 isl_union_map *must, *kill;
2496 must = isl_union_access_info_get_must_source(info);
2497 kill = isl_union_access_info_get_kill(info);
2498 must = isl_union_map_union(must, kill);
2499 return isl_union_access_info_set_must_source(info, must);
2502 /* Drop dependences from "flow" that purely originate from kills.
2503 * That is, only keep those dependences that originate from
2504 * the original must-sources "must" and/or the original may-sources "may".
2505 * In particular, "must" contains the must-sources from before
2506 * the kills were added and "may" contains the may-source from before
2507 * the kills were removed.
2509 * The dependences are of the form
2511 * Source -> [Sink -> Data]
2513 * Only those dependences are kept where the Source -> Data part
2514 * is a subset of the original may-sources or must-sources.
2515 * Of those, only the must-dependences that intersect with the must-sources
2516 * remain must-dependences.
2517 * If there is some overlap between the may-sources and the must-sources,
2518 * then the may-dependences and must-dependences may also overlap.
2519 * This should be fine since the may-dependences are only kept
2520 * disjoint from the must-dependences for the isl_union_map_compute_flow
2521 * interface. This interface does not support kills, so it will
2522 * not end up calling this function.
2524 static __isl_give isl_union_flow *isl_union_flow_drop_kill_source(
2525 __isl_take isl_union_flow *flow, __isl_take isl_union_map *must,
2526 __isl_take isl_union_map *may)
2528 isl_union_map *move;
2530 if (!flow)
2531 goto error;
2532 move = isl_union_map_copy(flow->must_dep);
2533 move = isl_union_map_intersect_range_factor_range(move,
2534 isl_union_map_copy(may));
2535 may = isl_union_map_union(may, isl_union_map_copy(must));
2536 flow->may_dep = isl_union_map_intersect_range_factor_range(
2537 flow->may_dep, may);
2538 flow->must_dep = isl_union_map_intersect_range_factor_range(
2539 flow->must_dep, must);
2540 flow->may_dep = isl_union_map_union(flow->may_dep, move);
2541 if (!flow->must_dep || !flow->may_dep)
2542 return isl_union_flow_free(flow);
2544 return flow;
2545 error:
2546 isl_union_map_free(must);
2547 isl_union_map_free(may);
2548 return NULL;
2551 /* Remove the must accesses from the may accesses.
2553 * A must access always trumps a may access, so there is no need
2554 * for a must access to also be considered as a may access. Doing so
2555 * would only cost extra computations only to find out that
2556 * the duplicated may access does not make any difference.
2558 static __isl_give isl_union_access_info *isl_union_access_info_normalize(
2559 __isl_take isl_union_access_info *access)
2561 if (!access)
2562 return NULL;
2563 access->access[isl_access_may_source] =
2564 isl_union_map_subtract(access->access[isl_access_may_source],
2565 isl_union_map_copy(access->access[isl_access_must_source]));
2566 if (!access->access[isl_access_may_source])
2567 return isl_union_access_info_free(access);
2569 return access;
2572 /* Given a description of the "sink" accesses, the "source" accesses and
2573 * a schedule, compute for each instance of a sink access
2574 * and for each element accessed by that instance,
2575 * the possible or definite source accesses that last accessed the
2576 * element accessed by the sink access before this sink access
2577 * in the sense that there is no intermediate definite source access.
2579 * The must_no_source and may_no_source elements of the result
2580 * are subsets of access->sink. The elements must_dep and may_dep
2581 * map domain elements of access->{may,must)_source to
2582 * domain elements of access->sink.
2584 * This function is used when only the schedule map representation
2585 * is available.
2587 * We first prepend the schedule dimensions to the domain
2588 * of the accesses so that we can easily compare their relative order.
2589 * Then we consider each sink access individually in compute_flow.
2591 static __isl_give isl_union_flow *compute_flow_union_map(
2592 __isl_take isl_union_access_info *access)
2594 struct isl_compute_flow_data data;
2595 isl_union_map *sink;
2597 access = isl_union_access_info_align_params(access);
2598 access = isl_union_access_info_introduce_schedule(access);
2599 if (!access)
2600 return NULL;
2602 data.must_source = access->access[isl_access_must_source];
2603 data.may_source = access->access[isl_access_may_source];
2605 sink = access->access[isl_access_sink];
2606 data.flow = isl_union_flow_alloc(isl_union_map_get_space(sink));
2608 if (isl_union_map_foreach_map(sink, &compute_flow, &data) < 0)
2609 goto error;
2611 data.flow = isl_union_flow_drop_schedule(data.flow);
2613 isl_union_access_info_free(access);
2614 return data.flow;
2615 error:
2616 isl_union_access_info_free(access);
2617 isl_union_flow_free(data.flow);
2618 return NULL;
2621 /* A schedule access relation.
2623 * The access relation "access" is of the form [S -> D] -> A,
2624 * where S corresponds to the prefix schedule at "node".
2625 * "must" is only relevant for source accesses and indicates
2626 * whether the access is a must source or a may source.
2628 struct isl_scheduled_access {
2629 isl_map *access;
2630 int must;
2631 isl_schedule_node *node;
2634 /* Data structure for keeping track of individual scheduled sink and source
2635 * accesses when computing dependence analysis based on a schedule tree.
2637 * "n_sink" is the number of used entries in "sink"
2638 * "n_source" is the number of used entries in "source"
2640 * "set_sink", "must" and "node" are only used inside collect_sink_source,
2641 * to keep track of the current node and
2642 * of what extract_sink_source needs to do.
2644 struct isl_compute_flow_schedule_data {
2645 isl_union_access_info *access;
2647 int n_sink;
2648 int n_source;
2650 struct isl_scheduled_access *sink;
2651 struct isl_scheduled_access *source;
2653 int set_sink;
2654 int must;
2655 isl_schedule_node *node;
2658 /* Align the parameters of all sinks with all sources.
2660 * If there are no sinks or no sources, then no alignment is needed.
2662 static void isl_compute_flow_schedule_data_align_params(
2663 struct isl_compute_flow_schedule_data *data)
2665 int i;
2666 isl_space *space;
2668 if (data->n_sink == 0 || data->n_source == 0)
2669 return;
2671 space = isl_map_get_space(data->sink[0].access);
2673 for (i = 1; i < data->n_sink; ++i)
2674 space = isl_space_align_params(space,
2675 isl_map_get_space(data->sink[i].access));
2676 for (i = 0; i < data->n_source; ++i)
2677 space = isl_space_align_params(space,
2678 isl_map_get_space(data->source[i].access));
2680 for (i = 0; i < data->n_sink; ++i)
2681 data->sink[i].access =
2682 isl_map_align_params(data->sink[i].access,
2683 isl_space_copy(space));
2684 for (i = 0; i < data->n_source; ++i)
2685 data->source[i].access =
2686 isl_map_align_params(data->source[i].access,
2687 isl_space_copy(space));
2689 isl_space_free(space);
2692 /* Free all the memory referenced from "data".
2693 * Do not free "data" itself as it may be allocated on the stack.
2695 static void isl_compute_flow_schedule_data_clear(
2696 struct isl_compute_flow_schedule_data *data)
2698 int i;
2700 if (!data->sink)
2701 return;
2703 for (i = 0; i < data->n_sink; ++i) {
2704 isl_map_free(data->sink[i].access);
2705 isl_schedule_node_free(data->sink[i].node);
2708 for (i = 0; i < data->n_source; ++i) {
2709 isl_map_free(data->source[i].access);
2710 isl_schedule_node_free(data->source[i].node);
2713 free(data->sink);
2716 /* isl_schedule_foreach_schedule_node_top_down callback for counting
2717 * (an upper bound on) the number of sinks and sources.
2719 * Sinks and sources are only extracted at leaves of the tree,
2720 * so we skip the node if it is not a leaf.
2721 * Otherwise we increment data->n_sink and data->n_source with
2722 * the number of spaces in the sink and source access domains
2723 * that reach this node.
2725 static isl_bool count_sink_source(__isl_keep isl_schedule_node *node,
2726 void *user)
2728 struct isl_compute_flow_schedule_data *data = user;
2729 isl_union_set *domain;
2730 isl_union_map *umap;
2731 isl_bool r = isl_bool_false;
2733 if (isl_schedule_node_get_type(node) != isl_schedule_node_leaf)
2734 return isl_bool_true;
2736 domain = isl_schedule_node_get_universe_domain(node);
2738 umap = isl_union_map_copy(data->access->access[isl_access_sink]);
2739 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
2740 data->n_sink += isl_union_map_n_map(umap);
2741 isl_union_map_free(umap);
2742 if (!umap)
2743 r = isl_bool_error;
2745 umap = isl_union_map_copy(data->access->access[isl_access_must_source]);
2746 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
2747 data->n_source += isl_union_map_n_map(umap);
2748 isl_union_map_free(umap);
2749 if (!umap)
2750 r = isl_bool_error;
2752 umap = isl_union_map_copy(data->access->access[isl_access_may_source]);
2753 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
2754 data->n_source += isl_union_map_n_map(umap);
2755 isl_union_map_free(umap);
2756 if (!umap)
2757 r = isl_bool_error;
2759 isl_union_set_free(domain);
2761 return r;
2764 /* Add a single scheduled sink or source (depending on data->set_sink)
2765 * with scheduled access relation "map", must property data->must and
2766 * schedule node data->node to the list of sinks or sources.
2768 static isl_stat extract_sink_source(__isl_take isl_map *map, void *user)
2770 struct isl_compute_flow_schedule_data *data = user;
2771 struct isl_scheduled_access *access;
2773 if (data->set_sink)
2774 access = data->sink + data->n_sink++;
2775 else
2776 access = data->source + data->n_source++;
2778 access->access = map;
2779 access->must = data->must;
2780 access->node = isl_schedule_node_copy(data->node);
2782 return isl_stat_ok;
2785 /* isl_schedule_foreach_schedule_node_top_down callback for collecting
2786 * individual scheduled source and sink accesses (taking into account
2787 * the domain of the schedule).
2789 * We only collect accesses at the leaves of the schedule tree.
2790 * We prepend the schedule dimensions at the leaf to the iteration
2791 * domains of the source and sink accesses and then extract
2792 * the individual accesses (per space).
2794 * In particular, if the prefix schedule at the node is of the form
2796 * D -> S
2798 * while the access relations are of the form
2800 * D -> A
2802 * then the updated access relations are of the form
2804 * [S -> D] -> A
2806 * Note that S consists of a single space such that introducing S
2807 * in the access relations does not increase the number of spaces.
2809 static isl_bool collect_sink_source(__isl_keep isl_schedule_node *node,
2810 void *user)
2812 struct isl_compute_flow_schedule_data *data = user;
2813 isl_union_map *prefix;
2814 isl_union_map *umap;
2815 isl_bool r = isl_bool_false;
2817 if (isl_schedule_node_get_type(node) != isl_schedule_node_leaf)
2818 return isl_bool_true;
2820 data->node = node;
2822 prefix = isl_schedule_node_get_prefix_schedule_relation(node);
2823 prefix = isl_union_map_reverse(prefix);
2824 prefix = isl_union_map_range_map(prefix);
2826 data->set_sink = 1;
2827 umap = isl_union_map_copy(data->access->access[isl_access_sink]);
2828 umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
2829 if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
2830 r = isl_bool_error;
2831 isl_union_map_free(umap);
2833 data->set_sink = 0;
2834 data->must = 1;
2835 umap = isl_union_map_copy(data->access->access[isl_access_must_source]);
2836 umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
2837 if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
2838 r = isl_bool_error;
2839 isl_union_map_free(umap);
2841 data->set_sink = 0;
2842 data->must = 0;
2843 umap = isl_union_map_copy(data->access->access[isl_access_may_source]);
2844 umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
2845 if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
2846 r = isl_bool_error;
2847 isl_union_map_free(umap);
2849 isl_union_map_free(prefix);
2851 return r;
2854 /* isl_access_info_compute_flow callback for determining whether
2855 * the shared nesting level and the ordering within that level
2856 * for two scheduled accesses for use in compute_single_flow.
2858 * The tokens passed to this function refer to the leaves
2859 * in the schedule tree where the accesses take place.
2861 * If n is the shared number of loops, then we need to return
2862 * "2 * n + 1" if "first" precedes "second" inside the innermost
2863 * shared loop and "2 * n" otherwise.
2865 * The innermost shared ancestor may be the leaves themselves
2866 * if the accesses take place in the same leaf. Otherwise,
2867 * it is either a set node or a sequence node. Only in the case
2868 * of a sequence node do we consider one access to precede the other.
2870 static int before_node(void *first, void *second)
2872 isl_schedule_node *node1 = first;
2873 isl_schedule_node *node2 = second;
2874 isl_schedule_node *shared;
2875 int depth;
2876 int before = 0;
2878 shared = isl_schedule_node_get_shared_ancestor(node1, node2);
2879 if (!shared)
2880 return -1;
2882 depth = isl_schedule_node_get_schedule_depth(shared);
2883 if (isl_schedule_node_get_type(shared) == isl_schedule_node_sequence) {
2884 int pos1, pos2;
2886 pos1 = isl_schedule_node_get_ancestor_child_position(node1,
2887 shared);
2888 pos2 = isl_schedule_node_get_ancestor_child_position(node2,
2889 shared);
2890 before = pos1 < pos2;
2893 isl_schedule_node_free(shared);
2895 return 2 * depth + before;
2898 /* Check if the given two accesses may be coscheduled.
2899 * If so, return 1. Otherwise return 0.
2901 * Two accesses may only be coscheduled if they appear in the same leaf.
2903 static int coscheduled_node(void *first, void *second)
2905 isl_schedule_node *node1 = first;
2906 isl_schedule_node *node2 = second;
2908 return node1 == node2;
2911 /* Add the scheduled sources from "data" that access
2912 * the same data space as "sink" to "access".
2914 static __isl_give isl_access_info *add_matching_sources(
2915 __isl_take isl_access_info *access, struct isl_scheduled_access *sink,
2916 struct isl_compute_flow_schedule_data *data)
2918 int i;
2919 isl_space *space;
2921 space = isl_space_range(isl_map_get_space(sink->access));
2922 for (i = 0; i < data->n_source; ++i) {
2923 struct isl_scheduled_access *source;
2924 isl_space *source_space;
2925 int eq;
2927 source = &data->source[i];
2928 source_space = isl_map_get_space(source->access);
2929 source_space = isl_space_range(source_space);
2930 eq = isl_space_is_equal(space, source_space);
2931 isl_space_free(source_space);
2933 if (!eq)
2934 continue;
2935 if (eq < 0)
2936 goto error;
2938 access = isl_access_info_add_source(access,
2939 isl_map_copy(source->access), source->must, source->node);
2942 isl_space_free(space);
2943 return access;
2944 error:
2945 isl_space_free(space);
2946 isl_access_info_free(access);
2947 return NULL;
2950 /* Given a scheduled sink access relation "sink", compute the corresponding
2951 * dependences on the sources in "data" and add the computed dependences
2952 * to "uf".
2954 * The dependences computed by access_info_compute_flow_core are of the form
2956 * [S -> I] -> [[S' -> I'] -> A]
2958 * The schedule dimensions are projected out by first currying the range,
2959 * resulting in
2961 * [S -> I] -> [S' -> [I' -> A]]
2963 * and then computing the factor range
2965 * I -> [I' -> A]
2967 static __isl_give isl_union_flow *compute_single_flow(
2968 __isl_take isl_union_flow *uf, struct isl_scheduled_access *sink,
2969 struct isl_compute_flow_schedule_data *data)
2971 int i;
2972 isl_access_info *access;
2973 isl_flow *flow;
2974 isl_map *map;
2976 if (!uf)
2977 return NULL;
2979 access = isl_access_info_alloc(isl_map_copy(sink->access), sink->node,
2980 &before_node, data->n_source);
2981 if (access)
2982 access->coscheduled = &coscheduled_node;
2983 access = add_matching_sources(access, sink, data);
2985 flow = access_info_compute_flow_core(access);
2986 if (!flow)
2987 return isl_union_flow_free(uf);
2989 map = isl_map_domain_factor_range(isl_flow_get_no_source(flow, 1));
2990 uf->must_no_source = isl_union_map_union(uf->must_no_source,
2991 isl_union_map_from_map(map));
2992 map = isl_map_domain_factor_range(isl_flow_get_no_source(flow, 0));
2993 uf->may_no_source = isl_union_map_union(uf->may_no_source,
2994 isl_union_map_from_map(map));
2996 for (i = 0; i < flow->n_source; ++i) {
2997 isl_union_map *dep;
2999 map = isl_map_range_curry(isl_map_copy(flow->dep[i].map));
3000 map = isl_map_factor_range(map);
3001 dep = isl_union_map_from_map(map);
3002 if (flow->dep[i].must)
3003 uf->must_dep = isl_union_map_union(uf->must_dep, dep);
3004 else
3005 uf->may_dep = isl_union_map_union(uf->may_dep, dep);
3008 isl_flow_free(flow);
3010 return uf;
3013 /* Given a description of the "sink" accesses, the "source" accesses and
3014 * a schedule, compute for each instance of a sink access
3015 * and for each element accessed by that instance,
3016 * the possible or definite source accesses that last accessed the
3017 * element accessed by the sink access before this sink access
3018 * in the sense that there is no intermediate definite source access.
3019 * Only consider dependences between statement instances that belong
3020 * to the domain of the schedule.
3022 * The must_no_source and may_no_source elements of the result
3023 * are subsets of access->sink. The elements must_dep and may_dep
3024 * map domain elements of access->{may,must)_source to
3025 * domain elements of access->sink.
3027 * This function is used when a schedule tree representation
3028 * is available.
3030 * We extract the individual scheduled source and sink access relations
3031 * (taking into account the domain of the schedule) and
3032 * then compute dependences for each scheduled sink individually.
3034 static __isl_give isl_union_flow *compute_flow_schedule(
3035 __isl_take isl_union_access_info *access)
3037 struct isl_compute_flow_schedule_data data = { access };
3038 int i, n;
3039 isl_ctx *ctx;
3040 isl_space *space;
3041 isl_union_flow *flow;
3043 ctx = isl_union_access_info_get_ctx(access);
3045 data.n_sink = 0;
3046 data.n_source = 0;
3047 if (isl_schedule_foreach_schedule_node_top_down(access->schedule,
3048 &count_sink_source, &data) < 0)
3049 goto error;
3051 n = data.n_sink + data.n_source;
3052 data.sink = isl_calloc_array(ctx, struct isl_scheduled_access, n);
3053 if (n && !data.sink)
3054 goto error;
3055 data.source = data.sink + data.n_sink;
3057 data.n_sink = 0;
3058 data.n_source = 0;
3059 if (isl_schedule_foreach_schedule_node_top_down(access->schedule,
3060 &collect_sink_source, &data) < 0)
3061 goto error;
3063 space = isl_union_map_get_space(access->access[isl_access_sink]);
3064 flow = isl_union_flow_alloc(space);
3066 isl_compute_flow_schedule_data_align_params(&data);
3068 for (i = 0; i < data.n_sink; ++i)
3069 flow = compute_single_flow(flow, &data.sink[i], &data);
3071 isl_compute_flow_schedule_data_clear(&data);
3073 isl_union_access_info_free(access);
3074 return flow;
3075 error:
3076 isl_union_access_info_free(access);
3077 isl_compute_flow_schedule_data_clear(&data);
3078 return NULL;
3081 /* Given a description of the "sink" accesses, the "source" accesses and
3082 * a schedule, compute for each instance of a sink access
3083 * and for each element accessed by that instance,
3084 * the possible or definite source accesses that last accessed the
3085 * element accessed by the sink access before this sink access
3086 * in the sense that there is no intermediate definite source access.
3088 * The must_no_source and may_no_source elements of the result
3089 * are subsets of access->sink. The elements must_dep and may_dep
3090 * map domain elements of access->{may,must)_source to
3091 * domain elements of access->sink.
3093 * If any kills have been specified, then they are treated as
3094 * must-sources internally. Any dependence that purely derives
3095 * from an original kill is removed from the output.
3097 * We check whether the schedule is available as a schedule tree
3098 * or a schedule map and call the corresponding function to perform
3099 * the analysis.
3101 __isl_give isl_union_flow *isl_union_access_info_compute_flow(
3102 __isl_take isl_union_access_info *access)
3104 isl_bool has_kill;
3105 isl_union_map *must = NULL, *may = NULL;
3106 isl_union_flow *flow;
3108 has_kill = isl_union_access_has_kill(access);
3109 if (has_kill < 0)
3110 goto error;
3111 if (has_kill) {
3112 must = isl_union_access_info_get_must_source(access);
3113 may = isl_union_access_info_get_may_source(access);
3115 access = isl_union_access_info_add_kill_to_must_source(access);
3116 access = isl_union_access_info_normalize(access);
3117 if (!access)
3118 goto error;
3119 if (access->schedule)
3120 flow = compute_flow_schedule(access);
3121 else
3122 flow = compute_flow_union_map(access);
3123 if (has_kill)
3124 flow = isl_union_flow_drop_kill_source(flow, must, may);
3125 return flow;
3126 error:
3127 isl_union_access_info_free(access);
3128 isl_union_map_free(must);
3129 isl_union_map_free(may);
3130 return NULL;
3133 /* Print the information contained in "flow" to "p".
3134 * The information is printed as a YAML document.
3136 __isl_give isl_printer *isl_printer_print_union_flow(
3137 __isl_take isl_printer *p, __isl_keep isl_union_flow *flow)
3139 isl_union_map *umap;
3141 if (!flow)
3142 return isl_printer_free(p);
3144 p = isl_printer_yaml_start_mapping(p);
3145 umap = isl_union_flow_get_full_must_dependence(flow);
3146 p = print_union_map_field(p, "must_dependence", umap);
3147 isl_union_map_free(umap);
3148 umap = isl_union_flow_get_full_may_dependence(flow);
3149 p = print_union_map_field(p, "may_dependence", umap);
3150 isl_union_map_free(umap);
3151 p = print_union_map_field(p, "must_no_source", flow->must_no_source);
3152 umap = isl_union_flow_get_may_no_source(flow);
3153 p = print_union_map_field(p, "may_no_source", umap);
3154 isl_union_map_free(umap);
3155 p = isl_printer_yaml_end_mapping(p);
3157 return p;
3160 /* Return a string representation of the information in "flow".
3161 * The information is printed in flow format.
3163 __isl_give char *isl_union_flow_to_str(__isl_keep isl_union_flow *flow)
3165 isl_printer *p;
3166 char *s;
3168 if (!flow)
3169 return NULL;
3171 p = isl_printer_to_str(isl_union_flow_get_ctx(flow));
3172 p = isl_printer_set_yaml_style(p, ISL_YAML_STYLE_FLOW);
3173 p = isl_printer_print_union_flow(p, flow);
3174 s = isl_printer_get_str(p);
3175 isl_printer_free(p);
3177 return s;
3180 /* Given a collection of "sink" and "source" accesses,
3181 * compute for each iteration of a sink access
3182 * and for each element accessed by that iteration,
3183 * the source access in the list that last accessed the
3184 * element accessed by the sink access before this sink access.
3185 * Each access is given as a map from the loop iterators
3186 * to the array indices.
3187 * The result is a relations between source and sink
3188 * iterations and a subset of the domain of the sink accesses,
3189 * corresponding to those iterations that access an element
3190 * not previously accessed.
3192 * We collect the inputs in an isl_union_access_info object,
3193 * call isl_union_access_info_compute_flow and extract
3194 * the outputs from the result.
3196 int isl_union_map_compute_flow(__isl_take isl_union_map *sink,
3197 __isl_take isl_union_map *must_source,
3198 __isl_take isl_union_map *may_source,
3199 __isl_take isl_union_map *schedule,
3200 __isl_give isl_union_map **must_dep, __isl_give isl_union_map **may_dep,
3201 __isl_give isl_union_map **must_no_source,
3202 __isl_give isl_union_map **may_no_source)
3204 isl_union_access_info *access;
3205 isl_union_flow *flow;
3207 access = isl_union_access_info_from_sink(sink);
3208 access = isl_union_access_info_set_must_source(access, must_source);
3209 access = isl_union_access_info_set_may_source(access, may_source);
3210 access = isl_union_access_info_set_schedule_map(access, schedule);
3211 flow = isl_union_access_info_compute_flow(access);
3213 if (must_dep)
3214 *must_dep = isl_union_flow_get_must_dependence(flow);
3215 if (may_dep)
3216 *may_dep = isl_union_flow_get_non_must_dependence(flow);
3217 if (must_no_source)
3218 *must_no_source = isl_union_flow_get_must_no_source(flow);
3219 if (may_no_source)
3220 *may_no_source = isl_union_flow_get_non_must_no_source(flow);
3222 isl_union_flow_free(flow);
3224 if ((must_dep && !*must_dep) || (may_dep && !*may_dep) ||
3225 (must_no_source && !*must_no_source) ||
3226 (may_no_source && !*may_no_source))
3227 goto error;
3229 return 0;
3230 error:
3231 if (must_dep)
3232 *must_dep = isl_union_map_free(*must_dep);
3233 if (may_dep)
3234 *may_dep = isl_union_map_free(*may_dep);
3235 if (must_no_source)
3236 *must_no_source = isl_union_map_free(*must_no_source);
3237 if (may_no_source)
3238 *may_no_source = isl_union_map_free(*may_no_source);
3239 return -1;