isl_coalesce.c rename max to isl_max
[isl.git] / isl_flow.c
blob5034c285e38b8dab21b0f7b939cb6c3456816d4b
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>
27 enum isl_restriction_type {
28 isl_restriction_type_empty,
29 isl_restriction_type_none,
30 isl_restriction_type_input,
31 isl_restriction_type_output
34 struct isl_restriction {
35 enum isl_restriction_type type;
37 isl_set *source;
38 isl_set *sink;
41 /* Create a restriction of the given type.
43 static __isl_give isl_restriction *isl_restriction_alloc(
44 __isl_take isl_map *source_map, enum isl_restriction_type type)
46 isl_ctx *ctx;
47 isl_restriction *restr;
49 if (!source_map)
50 return NULL;
52 ctx = isl_map_get_ctx(source_map);
53 restr = isl_calloc_type(ctx, struct isl_restriction);
54 if (!restr)
55 goto error;
57 restr->type = type;
59 isl_map_free(source_map);
60 return restr;
61 error:
62 isl_map_free(source_map);
63 return NULL;
66 /* Create a restriction that doesn't restrict anything.
68 __isl_give isl_restriction *isl_restriction_none(__isl_take isl_map *source_map)
70 return isl_restriction_alloc(source_map, isl_restriction_type_none);
73 /* Create a restriction that removes everything.
75 __isl_give isl_restriction *isl_restriction_empty(
76 __isl_take isl_map *source_map)
78 return isl_restriction_alloc(source_map, isl_restriction_type_empty);
81 /* Create a restriction on the input of the maximization problem
82 * based on the given source and sink restrictions.
84 __isl_give isl_restriction *isl_restriction_input(
85 __isl_take isl_set *source_restr, __isl_take isl_set *sink_restr)
87 isl_ctx *ctx;
88 isl_restriction *restr;
90 if (!source_restr || !sink_restr)
91 goto error;
93 ctx = isl_set_get_ctx(source_restr);
94 restr = isl_calloc_type(ctx, struct isl_restriction);
95 if (!restr)
96 goto error;
98 restr->type = isl_restriction_type_input;
99 restr->source = source_restr;
100 restr->sink = sink_restr;
102 return restr;
103 error:
104 isl_set_free(source_restr);
105 isl_set_free(sink_restr);
106 return NULL;
109 /* Create a restriction on the output of the maximization problem
110 * based on the given source restriction.
112 __isl_give isl_restriction *isl_restriction_output(
113 __isl_take isl_set *source_restr)
115 isl_ctx *ctx;
116 isl_restriction *restr;
118 if (!source_restr)
119 return NULL;
121 ctx = isl_set_get_ctx(source_restr);
122 restr = isl_calloc_type(ctx, struct isl_restriction);
123 if (!restr)
124 goto error;
126 restr->type = isl_restriction_type_output;
127 restr->source = source_restr;
129 return restr;
130 error:
131 isl_set_free(source_restr);
132 return NULL;
135 __isl_null isl_restriction *isl_restriction_free(
136 __isl_take isl_restriction *restr)
138 if (!restr)
139 return NULL;
141 isl_set_free(restr->source);
142 isl_set_free(restr->sink);
143 free(restr);
144 return NULL;
147 isl_ctx *isl_restriction_get_ctx(__isl_keep isl_restriction *restr)
149 return restr ? isl_set_get_ctx(restr->source) : NULL;
152 /* A private structure to keep track of a mapping together with
153 * a user-specified identifier and a boolean indicating whether
154 * the map represents a must or may access/dependence.
156 struct isl_labeled_map {
157 struct isl_map *map;
158 void *data;
159 int must;
162 /* A structure containing the input for dependence analysis:
163 * - a sink
164 * - n_must + n_may (<= max_source) sources
165 * - a function for determining the relative order of sources and sink
166 * The must sources are placed before the may sources.
168 * domain_map is an auxiliary map that maps the sink access relation
169 * to the domain of this access relation.
171 * restrict_fn is a callback that (if not NULL) will be called
172 * right before any lexicographical maximization.
174 struct isl_access_info {
175 isl_map *domain_map;
176 struct isl_labeled_map sink;
177 isl_access_level_before level_before;
179 isl_access_restrict restrict_fn;
180 void *restrict_user;
182 int max_source;
183 int n_must;
184 int n_may;
185 struct isl_labeled_map source[1];
188 /* A structure containing the output of dependence analysis:
189 * - n_source dependences
190 * - a wrapped subset of the sink for which definitely no source could be found
191 * - a wrapped subset of the sink for which possibly no source could be found
193 struct isl_flow {
194 isl_set *must_no_source;
195 isl_set *may_no_source;
196 int n_source;
197 struct isl_labeled_map *dep;
200 /* Construct an isl_access_info structure and fill it up with
201 * the given data. The number of sources is set to 0.
203 __isl_give isl_access_info *isl_access_info_alloc(__isl_take isl_map *sink,
204 void *sink_user, isl_access_level_before fn, int max_source)
206 isl_ctx *ctx;
207 struct isl_access_info *acc;
209 if (!sink)
210 return NULL;
212 ctx = isl_map_get_ctx(sink);
213 isl_assert(ctx, max_source >= 0, goto error);
215 acc = isl_calloc(ctx, struct isl_access_info,
216 sizeof(struct isl_access_info) +
217 (max_source - 1) * sizeof(struct isl_labeled_map));
218 if (!acc)
219 goto error;
221 acc->sink.map = sink;
222 acc->sink.data = sink_user;
223 acc->level_before = fn;
224 acc->max_source = max_source;
225 acc->n_must = 0;
226 acc->n_may = 0;
228 return acc;
229 error:
230 isl_map_free(sink);
231 return NULL;
234 /* Free the given isl_access_info structure.
236 __isl_null isl_access_info *isl_access_info_free(
237 __isl_take isl_access_info *acc)
239 int i;
241 if (!acc)
242 return NULL;
243 isl_map_free(acc->domain_map);
244 isl_map_free(acc->sink.map);
245 for (i = 0; i < acc->n_must + acc->n_may; ++i)
246 isl_map_free(acc->source[i].map);
247 free(acc);
248 return NULL;
251 isl_ctx *isl_access_info_get_ctx(__isl_keep isl_access_info *acc)
253 return acc ? isl_map_get_ctx(acc->sink.map) : NULL;
256 __isl_give isl_access_info *isl_access_info_set_restrict(
257 __isl_take isl_access_info *acc, isl_access_restrict fn, void *user)
259 if (!acc)
260 return NULL;
261 acc->restrict_fn = fn;
262 acc->restrict_user = user;
263 return acc;
266 /* Add another source to an isl_access_info structure, making
267 * sure the "must" sources are placed before the "may" sources.
268 * This function may be called at most max_source times on a
269 * given isl_access_info structure, with max_source as specified
270 * in the call to isl_access_info_alloc that constructed the structure.
272 __isl_give isl_access_info *isl_access_info_add_source(
273 __isl_take isl_access_info *acc, __isl_take isl_map *source,
274 int must, void *source_user)
276 isl_ctx *ctx;
278 if (!acc)
279 goto error;
280 ctx = isl_map_get_ctx(acc->sink.map);
281 isl_assert(ctx, acc->n_must + acc->n_may < acc->max_source, goto error);
283 if (must) {
284 if (acc->n_may)
285 acc->source[acc->n_must + acc->n_may] =
286 acc->source[acc->n_must];
287 acc->source[acc->n_must].map = source;
288 acc->source[acc->n_must].data = source_user;
289 acc->source[acc->n_must].must = 1;
290 acc->n_must++;
291 } else {
292 acc->source[acc->n_must + acc->n_may].map = source;
293 acc->source[acc->n_must + acc->n_may].data = source_user;
294 acc->source[acc->n_must + acc->n_may].must = 0;
295 acc->n_may++;
298 return acc;
299 error:
300 isl_map_free(source);
301 isl_access_info_free(acc);
302 return NULL;
305 /* Return -n, 0 or n (with n a positive value), depending on whether
306 * the source access identified by p1 should be sorted before, together
307 * or after that identified by p2.
309 * If p1 appears before p2, then it should be sorted first.
310 * For more generic initial schedules, it is possible that neither
311 * p1 nor p2 appears before the other, or at least not in any obvious way.
312 * We therefore also check if p2 appears before p1, in which case p2
313 * should be sorted first.
314 * If not, we try to order the two statements based on the description
315 * of the iteration domains. This results in an arbitrary, but fairly
316 * stable ordering.
318 static int access_sort_cmp(const void *p1, const void *p2, void *user)
320 isl_access_info *acc = user;
321 const struct isl_labeled_map *i1, *i2;
322 int level1, level2;
323 uint32_t h1, h2;
324 i1 = (const struct isl_labeled_map *) p1;
325 i2 = (const struct isl_labeled_map *) p2;
327 level1 = acc->level_before(i1->data, i2->data);
328 if (level1 % 2)
329 return -1;
331 level2 = acc->level_before(i2->data, i1->data);
332 if (level2 % 2)
333 return 1;
335 h1 = isl_map_get_hash(i1->map);
336 h2 = isl_map_get_hash(i2->map);
337 return h1 > h2 ? 1 : h1 < h2 ? -1 : 0;
340 /* Sort the must source accesses in their textual order.
342 static __isl_give isl_access_info *isl_access_info_sort_sources(
343 __isl_take isl_access_info *acc)
345 if (!acc)
346 return NULL;
347 if (acc->n_must <= 1)
348 return acc;
350 if (isl_sort(acc->source, acc->n_must, sizeof(struct isl_labeled_map),
351 access_sort_cmp, acc) < 0)
352 return isl_access_info_free(acc);
354 return acc;
357 /* Align the parameters of the two spaces if needed and then call
358 * isl_space_join.
360 static __isl_give isl_space *space_align_and_join(__isl_take isl_space *left,
361 __isl_take isl_space *right)
363 if (isl_space_match(left, isl_dim_param, right, isl_dim_param))
364 return isl_space_join(left, right);
366 left = isl_space_align_params(left, isl_space_copy(right));
367 right = isl_space_align_params(right, isl_space_copy(left));
368 return isl_space_join(left, right);
371 /* Initialize an empty isl_flow structure corresponding to a given
372 * isl_access_info structure.
373 * For each must access, two dependences are created (initialized
374 * to the empty relation), one for the resulting must dependences
375 * and one for the resulting may dependences. May accesses can
376 * only lead to may dependences, so only one dependence is created
377 * for each of them.
378 * This function is private as isl_flow structures are only supposed
379 * to be created by isl_access_info_compute_flow.
381 static __isl_give isl_flow *isl_flow_alloc(__isl_keep isl_access_info *acc)
383 int i, n;
384 struct isl_ctx *ctx;
385 struct isl_flow *dep;
387 if (!acc)
388 return NULL;
390 ctx = isl_map_get_ctx(acc->sink.map);
391 dep = isl_calloc_type(ctx, struct isl_flow);
392 if (!dep)
393 return NULL;
395 n = 2 * acc->n_must + acc->n_may;
396 dep->dep = isl_calloc_array(ctx, struct isl_labeled_map, n);
397 if (n && !dep->dep)
398 goto error;
400 dep->n_source = n;
401 for (i = 0; i < acc->n_must; ++i) {
402 isl_space *dim;
403 dim = space_align_and_join(
404 isl_map_get_space(acc->source[i].map),
405 isl_space_reverse(isl_map_get_space(acc->sink.map)));
406 dep->dep[2 * i].map = isl_map_empty(dim);
407 dep->dep[2 * i + 1].map = isl_map_copy(dep->dep[2 * i].map);
408 dep->dep[2 * i].data = acc->source[i].data;
409 dep->dep[2 * i + 1].data = acc->source[i].data;
410 dep->dep[2 * i].must = 1;
411 dep->dep[2 * i + 1].must = 0;
412 if (!dep->dep[2 * i].map || !dep->dep[2 * i + 1].map)
413 goto error;
415 for (i = acc->n_must; i < acc->n_must + acc->n_may; ++i) {
416 isl_space *dim;
417 dim = space_align_and_join(
418 isl_map_get_space(acc->source[i].map),
419 isl_space_reverse(isl_map_get_space(acc->sink.map)));
420 dep->dep[acc->n_must + i].map = isl_map_empty(dim);
421 dep->dep[acc->n_must + i].data = acc->source[i].data;
422 dep->dep[acc->n_must + i].must = 0;
423 if (!dep->dep[acc->n_must + i].map)
424 goto error;
427 return dep;
428 error:
429 isl_flow_free(dep);
430 return NULL;
433 /* Iterate over all sources and for each resulting flow dependence
434 * that is not empty, call the user specfied function.
435 * The second argument in this function call identifies the source,
436 * while the third argument correspond to the final argument of
437 * the isl_flow_foreach call.
439 isl_stat isl_flow_foreach(__isl_keep isl_flow *deps,
440 isl_stat (*fn)(__isl_take isl_map *dep, int must, void *dep_user,
441 void *user),
442 void *user)
444 int i;
446 if (!deps)
447 return isl_stat_error;
449 for (i = 0; i < deps->n_source; ++i) {
450 if (isl_map_plain_is_empty(deps->dep[i].map))
451 continue;
452 if (fn(isl_map_copy(deps->dep[i].map), deps->dep[i].must,
453 deps->dep[i].data, user) < 0)
454 return isl_stat_error;
457 return isl_stat_ok;
460 /* Return a copy of the subset of the sink for which no source could be found.
462 __isl_give isl_map *isl_flow_get_no_source(__isl_keep isl_flow *deps, int must)
464 if (!deps)
465 return NULL;
467 if (must)
468 return isl_set_unwrap(isl_set_copy(deps->must_no_source));
469 else
470 return isl_set_unwrap(isl_set_copy(deps->may_no_source));
473 void isl_flow_free(__isl_take isl_flow *deps)
475 int i;
477 if (!deps)
478 return;
479 isl_set_free(deps->must_no_source);
480 isl_set_free(deps->may_no_source);
481 if (deps->dep) {
482 for (i = 0; i < deps->n_source; ++i)
483 isl_map_free(deps->dep[i].map);
484 free(deps->dep);
486 free(deps);
489 isl_ctx *isl_flow_get_ctx(__isl_keep isl_flow *deps)
491 return deps ? isl_set_get_ctx(deps->must_no_source) : NULL;
494 /* Return a map that enforces that the domain iteration occurs after
495 * the range iteration at the given level.
496 * If level is odd, then the domain iteration should occur after
497 * the target iteration in their shared level/2 outermost loops.
498 * In this case we simply need to enforce that these outermost
499 * loop iterations are the same.
500 * If level is even, then the loop iterator of the domain should
501 * be greater than the loop iterator of the range at the last
502 * of the level/2 shared loops, i.e., loop level/2 - 1.
504 static __isl_give isl_map *after_at_level(__isl_take isl_space *dim, int level)
506 struct isl_basic_map *bmap;
508 if (level % 2)
509 bmap = isl_basic_map_equal(dim, level/2);
510 else
511 bmap = isl_basic_map_more_at(dim, level/2 - 1);
513 return isl_map_from_basic_map(bmap);
516 /* Compute the partial lexicographic maximum of "dep" on domain "sink",
517 * but first check if the user has set acc->restrict_fn and if so
518 * update either the input or the output of the maximization problem
519 * with respect to the resulting restriction.
521 * Since the user expects a mapping from sink iterations to source iterations,
522 * whereas the domain of "dep" is a wrapped map, mapping sink iterations
523 * to accessed array elements, we first need to project out the accessed
524 * sink array elements by applying acc->domain_map.
525 * Similarly, the sink restriction specified by the user needs to be
526 * converted back to the wrapped map.
528 static __isl_give isl_map *restricted_partial_lexmax(
529 __isl_keep isl_access_info *acc, __isl_take isl_map *dep,
530 int source, __isl_take isl_set *sink, __isl_give isl_set **empty)
532 isl_map *source_map;
533 isl_restriction *restr;
534 isl_set *sink_domain;
535 isl_set *sink_restr;
536 isl_map *res;
538 if (!acc->restrict_fn)
539 return isl_map_partial_lexmax(dep, sink, empty);
541 source_map = isl_map_copy(dep);
542 source_map = isl_map_apply_domain(source_map,
543 isl_map_copy(acc->domain_map));
544 sink_domain = isl_set_copy(sink);
545 sink_domain = isl_set_apply(sink_domain, isl_map_copy(acc->domain_map));
546 restr = acc->restrict_fn(source_map, sink_domain,
547 acc->source[source].data, acc->restrict_user);
548 isl_set_free(sink_domain);
549 isl_map_free(source_map);
551 if (!restr)
552 goto error;
553 if (restr->type == isl_restriction_type_input) {
554 dep = isl_map_intersect_range(dep, isl_set_copy(restr->source));
555 sink_restr = isl_set_copy(restr->sink);
556 sink_restr = isl_set_apply(sink_restr,
557 isl_map_reverse(isl_map_copy(acc->domain_map)));
558 sink = isl_set_intersect(sink, sink_restr);
559 } else if (restr->type == isl_restriction_type_empty) {
560 isl_space *space = isl_map_get_space(dep);
561 isl_map_free(dep);
562 dep = isl_map_empty(space);
565 res = isl_map_partial_lexmax(dep, sink, empty);
567 if (restr->type == isl_restriction_type_output)
568 res = isl_map_intersect_range(res, isl_set_copy(restr->source));
570 isl_restriction_free(restr);
571 return res;
572 error:
573 isl_map_free(dep);
574 isl_set_free(sink);
575 *empty = NULL;
576 return NULL;
579 /* Compute the last iteration of must source j that precedes the sink
580 * at the given level for sink iterations in set_C.
581 * The subset of set_C for which no such iteration can be found is returned
582 * in *empty.
584 static struct isl_map *last_source(struct isl_access_info *acc,
585 struct isl_set *set_C,
586 int j, int level, struct isl_set **empty)
588 struct isl_map *read_map;
589 struct isl_map *write_map;
590 struct isl_map *dep_map;
591 struct isl_map *after;
592 struct isl_map *result;
594 read_map = isl_map_copy(acc->sink.map);
595 write_map = isl_map_copy(acc->source[j].map);
596 write_map = isl_map_reverse(write_map);
597 dep_map = isl_map_apply_range(read_map, write_map);
598 after = after_at_level(isl_map_get_space(dep_map), level);
599 dep_map = isl_map_intersect(dep_map, after);
600 result = restricted_partial_lexmax(acc, dep_map, j, set_C, empty);
601 result = isl_map_reverse(result);
603 return result;
606 /* For a given mapping between iterations of must source j and iterations
607 * of the sink, compute the last iteration of must source k preceding
608 * the sink at level before_level for any of the sink iterations,
609 * but following the corresponding iteration of must source j at level
610 * after_level.
612 static struct isl_map *last_later_source(struct isl_access_info *acc,
613 struct isl_map *old_map,
614 int j, int before_level,
615 int k, int after_level,
616 struct isl_set **empty)
618 isl_space *dim;
619 struct isl_set *set_C;
620 struct isl_map *read_map;
621 struct isl_map *write_map;
622 struct isl_map *dep_map;
623 struct isl_map *after_write;
624 struct isl_map *before_read;
625 struct isl_map *result;
627 set_C = isl_map_range(isl_map_copy(old_map));
628 read_map = isl_map_copy(acc->sink.map);
629 write_map = isl_map_copy(acc->source[k].map);
631 write_map = isl_map_reverse(write_map);
632 dep_map = isl_map_apply_range(read_map, write_map);
633 dim = space_align_and_join(isl_map_get_space(acc->source[k].map),
634 isl_space_reverse(isl_map_get_space(acc->source[j].map)));
635 after_write = after_at_level(dim, after_level);
636 after_write = isl_map_apply_range(after_write, old_map);
637 after_write = isl_map_reverse(after_write);
638 dep_map = isl_map_intersect(dep_map, after_write);
639 before_read = after_at_level(isl_map_get_space(dep_map), before_level);
640 dep_map = isl_map_intersect(dep_map, before_read);
641 result = restricted_partial_lexmax(acc, dep_map, k, set_C, empty);
642 result = isl_map_reverse(result);
644 return result;
647 /* Given a shared_level between two accesses, return 1 if the
648 * the first can precede the second at the requested target_level.
649 * If the target level is odd, i.e., refers to a statement level
650 * dimension, then first needs to precede second at the requested
651 * level, i.e., shared_level must be equal to target_level.
652 * If the target level is odd, then the two loops should share
653 * at least the requested number of outer loops.
655 static int can_precede_at_level(int shared_level, int target_level)
657 if (shared_level < target_level)
658 return 0;
659 if ((target_level % 2) && shared_level > target_level)
660 return 0;
661 return 1;
664 /* Given a possible flow dependence temp_rel[j] between source j and the sink
665 * at level sink_level, remove those elements for which
666 * there is an iteration of another source k < j that is closer to the sink.
667 * The flow dependences temp_rel[k] are updated with the improved sources.
668 * Any improved source needs to precede the sink at the same level
669 * and needs to follow source j at the same or a deeper level.
670 * The lower this level, the later the execution date of source k.
671 * We therefore consider lower levels first.
673 * If temp_rel[j] is empty, then there can be no improvement and
674 * we return immediately.
676 static int intermediate_sources(__isl_keep isl_access_info *acc,
677 struct isl_map **temp_rel, int j, int sink_level)
679 int k, level;
680 int depth = 2 * isl_map_dim(acc->source[j].map, isl_dim_in) + 1;
682 if (isl_map_plain_is_empty(temp_rel[j]))
683 return 0;
685 for (k = j - 1; k >= 0; --k) {
686 int plevel, plevel2;
687 plevel = acc->level_before(acc->source[k].data, acc->sink.data);
688 if (!can_precede_at_level(plevel, sink_level))
689 continue;
691 plevel2 = acc->level_before(acc->source[j].data,
692 acc->source[k].data);
694 for (level = sink_level; level <= depth; ++level) {
695 struct isl_map *T;
696 struct isl_set *trest;
697 struct isl_map *copy;
699 if (!can_precede_at_level(plevel2, level))
700 continue;
702 copy = isl_map_copy(temp_rel[j]);
703 T = last_later_source(acc, copy, j, sink_level, k,
704 level, &trest);
705 if (isl_map_plain_is_empty(T)) {
706 isl_set_free(trest);
707 isl_map_free(T);
708 continue;
710 temp_rel[j] = isl_map_intersect_range(temp_rel[j], trest);
711 temp_rel[k] = isl_map_union_disjoint(temp_rel[k], T);
715 return 0;
718 /* Compute all iterations of may source j that precedes the sink at the given
719 * level for sink iterations in set_C.
721 static __isl_give isl_map *all_sources(__isl_keep isl_access_info *acc,
722 __isl_take isl_set *set_C, int j, int level)
724 isl_map *read_map;
725 isl_map *write_map;
726 isl_map *dep_map;
727 isl_map *after;
729 read_map = isl_map_copy(acc->sink.map);
730 read_map = isl_map_intersect_domain(read_map, set_C);
731 write_map = isl_map_copy(acc->source[acc->n_must + j].map);
732 write_map = isl_map_reverse(write_map);
733 dep_map = isl_map_apply_range(read_map, write_map);
734 after = after_at_level(isl_map_get_space(dep_map), level);
735 dep_map = isl_map_intersect(dep_map, after);
737 return isl_map_reverse(dep_map);
740 /* For a given mapping between iterations of must source k and iterations
741 * of the sink, compute the all iteration of may source j preceding
742 * the sink at level before_level for any of the sink iterations,
743 * but following the corresponding iteration of must source k at level
744 * after_level.
746 static __isl_give isl_map *all_later_sources(__isl_keep isl_access_info *acc,
747 __isl_take isl_map *old_map,
748 int j, int before_level, int k, int after_level)
750 isl_space *dim;
751 isl_set *set_C;
752 isl_map *read_map;
753 isl_map *write_map;
754 isl_map *dep_map;
755 isl_map *after_write;
756 isl_map *before_read;
758 set_C = isl_map_range(isl_map_copy(old_map));
759 read_map = isl_map_copy(acc->sink.map);
760 read_map = isl_map_intersect_domain(read_map, set_C);
761 write_map = isl_map_copy(acc->source[acc->n_must + j].map);
763 write_map = isl_map_reverse(write_map);
764 dep_map = isl_map_apply_range(read_map, write_map);
765 dim = isl_space_join(isl_map_get_space(acc->source[acc->n_must + j].map),
766 isl_space_reverse(isl_map_get_space(acc->source[k].map)));
767 after_write = after_at_level(dim, after_level);
768 after_write = isl_map_apply_range(after_write, old_map);
769 after_write = isl_map_reverse(after_write);
770 dep_map = isl_map_intersect(dep_map, after_write);
771 before_read = after_at_level(isl_map_get_space(dep_map), before_level);
772 dep_map = isl_map_intersect(dep_map, before_read);
773 return isl_map_reverse(dep_map);
776 /* Given the must and may dependence relations for the must accesses
777 * for level sink_level, check if there are any accesses of may access j
778 * that occur in between and return their union.
779 * If some of these accesses are intermediate with respect to
780 * (previously thought to be) must dependences, then these
781 * must dependences are turned into may dependences.
783 static __isl_give isl_map *all_intermediate_sources(
784 __isl_keep isl_access_info *acc, __isl_take isl_map *map,
785 struct isl_map **must_rel, struct isl_map **may_rel,
786 int j, int sink_level)
788 int k, level;
789 int depth = 2 * isl_map_dim(acc->source[acc->n_must + j].map,
790 isl_dim_in) + 1;
792 for (k = 0; k < acc->n_must; ++k) {
793 int plevel;
795 if (isl_map_plain_is_empty(may_rel[k]) &&
796 isl_map_plain_is_empty(must_rel[k]))
797 continue;
799 plevel = acc->level_before(acc->source[k].data,
800 acc->source[acc->n_must + j].data);
802 for (level = sink_level; level <= depth; ++level) {
803 isl_map *T;
804 isl_map *copy;
805 isl_set *ran;
807 if (!can_precede_at_level(plevel, level))
808 continue;
810 copy = isl_map_copy(may_rel[k]);
811 T = all_later_sources(acc, copy, j, sink_level, k, level);
812 map = isl_map_union(map, T);
814 copy = isl_map_copy(must_rel[k]);
815 T = all_later_sources(acc, copy, j, sink_level, k, level);
816 ran = isl_map_range(isl_map_copy(T));
817 map = isl_map_union(map, T);
818 may_rel[k] = isl_map_union_disjoint(may_rel[k],
819 isl_map_intersect_range(isl_map_copy(must_rel[k]),
820 isl_set_copy(ran)));
821 T = isl_map_from_domain_and_range(
822 isl_set_universe(
823 isl_space_domain(isl_map_get_space(must_rel[k]))),
824 ran);
825 must_rel[k] = isl_map_subtract(must_rel[k], T);
829 return map;
832 /* Compute dependences for the case where all accesses are "may"
833 * accesses, which boils down to computing memory based dependences.
834 * The generic algorithm would also work in this case, but it would
835 * be overkill to use it.
837 static __isl_give isl_flow *compute_mem_based_dependences(
838 __isl_keep isl_access_info *acc)
840 int i;
841 isl_set *mustdo;
842 isl_set *maydo;
843 isl_flow *res;
845 res = isl_flow_alloc(acc);
846 if (!res)
847 return NULL;
849 mustdo = isl_map_domain(isl_map_copy(acc->sink.map));
850 maydo = isl_set_copy(mustdo);
852 for (i = 0; i < acc->n_may; ++i) {
853 int plevel;
854 int is_before;
855 isl_space *dim;
856 isl_map *before;
857 isl_map *dep;
859 plevel = acc->level_before(acc->source[i].data, acc->sink.data);
860 is_before = plevel & 1;
861 plevel >>= 1;
863 dim = isl_map_get_space(res->dep[i].map);
864 if (is_before)
865 before = isl_map_lex_le_first(dim, plevel);
866 else
867 before = isl_map_lex_lt_first(dim, plevel);
868 dep = isl_map_apply_range(isl_map_copy(acc->source[i].map),
869 isl_map_reverse(isl_map_copy(acc->sink.map)));
870 dep = isl_map_intersect(dep, before);
871 mustdo = isl_set_subtract(mustdo,
872 isl_map_range(isl_map_copy(dep)));
873 res->dep[i].map = isl_map_union(res->dep[i].map, dep);
876 res->may_no_source = isl_set_subtract(maydo, isl_set_copy(mustdo));
877 res->must_no_source = mustdo;
879 return res;
882 /* Compute dependences for the case where there is at least one
883 * "must" access.
885 * The core algorithm considers all levels in which a source may precede
886 * the sink, where a level may either be a statement level or a loop level.
887 * The outermost statement level is 1, the first loop level is 2, etc...
888 * The algorithm basically does the following:
889 * for all levels l of the read access from innermost to outermost
890 * for all sources w that may precede the sink access at that level
891 * compute the last iteration of the source that precedes the sink access
892 * at that level
893 * add result to possible last accesses at level l of source w
894 * for all sources w2 that we haven't considered yet at this level that may
895 * also precede the sink access
896 * for all levels l2 of w from l to innermost
897 * for all possible last accesses dep of w at l
898 * compute last iteration of w2 between the source and sink
899 * of dep
900 * add result to possible last accesses at level l of write w2
901 * and replace possible last accesses dep by the remainder
904 * The above algorithm is applied to the must access. During the course
905 * of the algorithm, we keep track of sink iterations that still
906 * need to be considered. These iterations are split into those that
907 * haven't been matched to any source access (mustdo) and those that have only
908 * been matched to may accesses (maydo).
909 * At the end of each level, we also consider the may accesses.
910 * In particular, we consider may accesses that precede the remaining
911 * sink iterations, moving elements from mustdo to maydo when appropriate,
912 * and may accesses that occur between a must source and a sink of any
913 * dependences found at the current level, turning must dependences into
914 * may dependences when appropriate.
917 static __isl_give isl_flow *compute_val_based_dependences(
918 __isl_keep isl_access_info *acc)
920 isl_ctx *ctx;
921 isl_flow *res;
922 isl_set *mustdo = NULL;
923 isl_set *maydo = NULL;
924 int level, j;
925 int depth;
926 isl_map **must_rel = NULL;
927 isl_map **may_rel = NULL;
929 if (!acc)
930 return NULL;
932 res = isl_flow_alloc(acc);
933 if (!res)
934 goto error;
935 ctx = isl_map_get_ctx(acc->sink.map);
937 depth = 2 * isl_map_dim(acc->sink.map, isl_dim_in) + 1;
938 mustdo = isl_map_domain(isl_map_copy(acc->sink.map));
939 maydo = isl_set_empty(isl_set_get_space(mustdo));
940 if (!mustdo || !maydo)
941 goto error;
942 if (isl_set_plain_is_empty(mustdo))
943 goto done;
945 must_rel = isl_alloc_array(ctx, struct isl_map *, acc->n_must);
946 may_rel = isl_alloc_array(ctx, struct isl_map *, acc->n_must);
947 if (!must_rel || !may_rel)
948 goto error;
950 for (level = depth; level >= 1; --level) {
951 for (j = acc->n_must-1; j >=0; --j) {
952 isl_space *space;
953 space = isl_map_get_space(res->dep[2 * j].map);
954 must_rel[j] = isl_map_empty(space);
955 may_rel[j] = isl_map_copy(must_rel[j]);
958 for (j = acc->n_must - 1; j >= 0; --j) {
959 struct isl_map *T;
960 struct isl_set *rest;
961 int plevel;
963 plevel = acc->level_before(acc->source[j].data,
964 acc->sink.data);
965 if (!can_precede_at_level(plevel, level))
966 continue;
968 T = last_source(acc, mustdo, j, level, &rest);
969 must_rel[j] = isl_map_union_disjoint(must_rel[j], T);
970 mustdo = rest;
972 intermediate_sources(acc, must_rel, j, level);
974 T = last_source(acc, maydo, j, level, &rest);
975 may_rel[j] = isl_map_union_disjoint(may_rel[j], T);
976 maydo = rest;
978 intermediate_sources(acc, may_rel, j, level);
980 if (isl_set_plain_is_empty(mustdo) &&
981 isl_set_plain_is_empty(maydo))
982 break;
984 for (j = j - 1; j >= 0; --j) {
985 int plevel;
987 plevel = acc->level_before(acc->source[j].data,
988 acc->sink.data);
989 if (!can_precede_at_level(plevel, level))
990 continue;
992 intermediate_sources(acc, must_rel, j, level);
993 intermediate_sources(acc, may_rel, j, level);
996 for (j = 0; j < acc->n_may; ++j) {
997 int plevel;
998 isl_map *T;
999 isl_set *ran;
1001 plevel = acc->level_before(acc->source[acc->n_must + j].data,
1002 acc->sink.data);
1003 if (!can_precede_at_level(plevel, level))
1004 continue;
1006 T = all_sources(acc, isl_set_copy(maydo), j, level);
1007 res->dep[2 * acc->n_must + j].map =
1008 isl_map_union(res->dep[2 * acc->n_must + j].map, T);
1009 T = all_sources(acc, isl_set_copy(mustdo), j, level);
1010 ran = isl_map_range(isl_map_copy(T));
1011 res->dep[2 * acc->n_must + j].map =
1012 isl_map_union(res->dep[2 * acc->n_must + j].map, T);
1013 mustdo = isl_set_subtract(mustdo, isl_set_copy(ran));
1014 maydo = isl_set_union_disjoint(maydo, ran);
1016 T = res->dep[2 * acc->n_must + j].map;
1017 T = all_intermediate_sources(acc, T, must_rel, may_rel,
1018 j, level);
1019 res->dep[2 * acc->n_must + j].map = T;
1022 for (j = acc->n_must - 1; j >= 0; --j) {
1023 res->dep[2 * j].map =
1024 isl_map_union_disjoint(res->dep[2 * j].map,
1025 must_rel[j]);
1026 res->dep[2 * j + 1].map =
1027 isl_map_union_disjoint(res->dep[2 * j + 1].map,
1028 may_rel[j]);
1031 if (isl_set_plain_is_empty(mustdo) &&
1032 isl_set_plain_is_empty(maydo))
1033 break;
1036 free(must_rel);
1037 free(may_rel);
1038 done:
1039 res->must_no_source = mustdo;
1040 res->may_no_source = maydo;
1041 return res;
1042 error:
1043 isl_flow_free(res);
1044 isl_set_free(mustdo);
1045 isl_set_free(maydo);
1046 free(must_rel);
1047 free(may_rel);
1048 return NULL;
1051 /* Given a "sink" access, a list of n "source" accesses,
1052 * compute for each iteration of the sink access
1053 * and for each element accessed by that iteration,
1054 * the source access in the list that last accessed the
1055 * element accessed by the sink access before this sink access.
1056 * Each access is given as a map from the loop iterators
1057 * to the array indices.
1058 * The result is a list of n relations between source and sink
1059 * iterations and a subset of the domain of the sink access,
1060 * corresponding to those iterations that access an element
1061 * not previously accessed.
1063 * To deal with multi-valued sink access relations, the sink iteration
1064 * domain is first extended with dimensions that correspond to the data
1065 * space. After the computation is finished, these extra dimensions are
1066 * projected out again.
1068 __isl_give isl_flow *isl_access_info_compute_flow(__isl_take isl_access_info *acc)
1070 int j;
1071 struct isl_flow *res = NULL;
1073 if (!acc)
1074 return NULL;
1076 acc->domain_map = isl_map_domain_map(isl_map_copy(acc->sink.map));
1077 acc->sink.map = isl_map_range_map(acc->sink.map);
1078 if (!acc->sink.map)
1079 goto error;
1081 if (acc->n_must == 0)
1082 res = compute_mem_based_dependences(acc);
1083 else {
1084 acc = isl_access_info_sort_sources(acc);
1085 res = compute_val_based_dependences(acc);
1087 if (!res)
1088 goto error;
1090 for (j = 0; j < res->n_source; ++j) {
1091 res->dep[j].map = isl_map_apply_range(res->dep[j].map,
1092 isl_map_copy(acc->domain_map));
1093 if (!res->dep[j].map)
1094 goto error;
1096 if (!res->must_no_source || !res->may_no_source)
1097 goto error;
1099 isl_access_info_free(acc);
1100 return res;
1101 error:
1102 isl_access_info_free(acc);
1103 isl_flow_free(res);
1104 return NULL;
1108 /* Keep track of some information about a schedule for a given
1109 * access. In particular, keep track of which dimensions
1110 * have a constant value and of the actual constant values.
1112 struct isl_sched_info {
1113 int *is_cst;
1114 isl_vec *cst;
1117 static void sched_info_free(__isl_take struct isl_sched_info *info)
1119 if (!info)
1120 return;
1121 isl_vec_free(info->cst);
1122 free(info->is_cst);
1123 free(info);
1126 /* Extract information on the constant dimensions of the schedule
1127 * for a given access. The "map" is of the form
1129 * [S -> D] -> A
1131 * with S the schedule domain, D the iteration domain and A the data domain.
1133 static __isl_give struct isl_sched_info *sched_info_alloc(
1134 __isl_keep isl_map *map)
1136 isl_ctx *ctx;
1137 isl_space *dim;
1138 struct isl_sched_info *info;
1139 int i, n;
1141 if (!map)
1142 return NULL;
1144 dim = isl_space_unwrap(isl_space_domain(isl_map_get_space(map)));
1145 if (!dim)
1146 return NULL;
1147 n = isl_space_dim(dim, isl_dim_in);
1148 isl_space_free(dim);
1150 ctx = isl_map_get_ctx(map);
1151 info = isl_alloc_type(ctx, struct isl_sched_info);
1152 if (!info)
1153 return NULL;
1154 info->is_cst = isl_alloc_array(ctx, int, n);
1155 info->cst = isl_vec_alloc(ctx, n);
1156 if (n && (!info->is_cst || !info->cst))
1157 goto error;
1159 for (i = 0; i < n; ++i) {
1160 isl_val *v;
1162 v = isl_map_plain_get_val_if_fixed(map, isl_dim_in, i);
1163 if (!v)
1164 goto error;
1165 info->is_cst[i] = !isl_val_is_nan(v);
1166 if (info->is_cst[i])
1167 info->cst = isl_vec_set_element_val(info->cst, i, v);
1168 else
1169 isl_val_free(v);
1172 return info;
1173 error:
1174 sched_info_free(info);
1175 return NULL;
1178 /* This structure represents the input for a dependence analysis computation.
1180 * "sink" represents the sink accesses.
1181 * "must_source" represents the definite source accesses.
1182 * "may_source" represents the possible source accesses.
1184 * "schedule" or "schedule_map" represents the execution order.
1185 * Exactly one of these fields should be NULL. The other field
1186 * determines the execution order.
1188 * The domains of these four maps refer to the same iteration spaces(s).
1189 * The ranges of the first three maps also refer to the same data space(s).
1191 * After a call to isl_union_access_info_introduce_schedule,
1192 * the "schedule_map" field no longer contains useful information.
1194 struct isl_union_access_info {
1195 isl_union_map *sink;
1196 isl_union_map *must_source;
1197 isl_union_map *may_source;
1199 isl_schedule *schedule;
1200 isl_union_map *schedule_map;
1203 /* Free "access" and return NULL.
1205 __isl_null isl_union_access_info *isl_union_access_info_free(
1206 __isl_take isl_union_access_info *access)
1208 if (!access)
1209 return NULL;
1211 isl_union_map_free(access->sink);
1212 isl_union_map_free(access->must_source);
1213 isl_union_map_free(access->may_source);
1214 isl_schedule_free(access->schedule);
1215 isl_union_map_free(access->schedule_map);
1216 free(access);
1218 return NULL;
1221 /* Return the isl_ctx to which "access" belongs.
1223 isl_ctx *isl_union_access_info_get_ctx(__isl_keep isl_union_access_info *access)
1225 return access ? isl_union_map_get_ctx(access->sink) : NULL;
1228 /* Create a new isl_union_access_info with the given sink accesses and
1229 * and no source accesses or schedule information.
1231 * By default, we use the schedule field of the isl_union_access_info,
1232 * but this may be overridden by a call
1233 * to isl_union_access_info_set_schedule_map.
1235 __isl_give isl_union_access_info *isl_union_access_info_from_sink(
1236 __isl_take isl_union_map *sink)
1238 isl_ctx *ctx;
1239 isl_space *space;
1240 isl_union_map *empty;
1241 isl_union_access_info *access;
1243 if (!sink)
1244 return NULL;
1245 ctx = isl_union_map_get_ctx(sink);
1246 access = isl_alloc_type(ctx, isl_union_access_info);
1247 if (!access)
1248 goto error;
1250 space = isl_union_map_get_space(sink);
1251 empty = isl_union_map_empty(isl_space_copy(space));
1252 access->sink = sink;
1253 access->must_source = isl_union_map_copy(empty);
1254 access->may_source = empty;
1255 access->schedule = isl_schedule_empty(space);
1256 access->schedule_map = NULL;
1258 if (!access->sink || !access->must_source ||
1259 !access->may_source || !access->schedule)
1260 return isl_union_access_info_free(access);
1262 return access;
1263 error:
1264 isl_union_map_free(sink);
1265 return NULL;
1268 /* Replace the definite source accesses of "access" by "must_source".
1270 __isl_give isl_union_access_info *isl_union_access_info_set_must_source(
1271 __isl_take isl_union_access_info *access,
1272 __isl_take isl_union_map *must_source)
1274 if (!access || !must_source)
1275 goto error;
1277 isl_union_map_free(access->must_source);
1278 access->must_source = must_source;
1280 return access;
1281 error:
1282 isl_union_access_info_free(access);
1283 isl_union_map_free(must_source);
1284 return NULL;
1287 /* Replace the possible source accesses of "access" by "may_source".
1289 __isl_give isl_union_access_info *isl_union_access_info_set_may_source(
1290 __isl_take isl_union_access_info *access,
1291 __isl_take isl_union_map *may_source)
1293 if (!access || !may_source)
1294 goto error;
1296 isl_union_map_free(access->may_source);
1297 access->may_source = may_source;
1299 return access;
1300 error:
1301 isl_union_access_info_free(access);
1302 isl_union_map_free(may_source);
1303 return NULL;
1306 /* Replace the schedule of "access" by "schedule".
1307 * Also free the schedule_map in case it was set last.
1309 __isl_give isl_union_access_info *isl_union_access_info_set_schedule(
1310 __isl_take isl_union_access_info *access,
1311 __isl_take isl_schedule *schedule)
1313 if (!access || !schedule)
1314 goto error;
1316 access->schedule_map = isl_union_map_free(access->schedule_map);
1317 isl_schedule_free(access->schedule);
1318 access->schedule = schedule;
1320 return access;
1321 error:
1322 isl_union_access_info_free(access);
1323 isl_schedule_free(schedule);
1324 return NULL;
1327 /* Replace the schedule map of "access" by "schedule_map".
1328 * Also free the schedule in case it was set last.
1330 __isl_give isl_union_access_info *isl_union_access_info_set_schedule_map(
1331 __isl_take isl_union_access_info *access,
1332 __isl_take isl_union_map *schedule_map)
1334 if (!access || !schedule_map)
1335 goto error;
1337 isl_union_map_free(access->schedule_map);
1338 access->schedule = isl_schedule_free(access->schedule);
1339 access->schedule_map = schedule_map;
1341 return access;
1342 error:
1343 isl_union_access_info_free(access);
1344 isl_union_map_free(schedule_map);
1345 return NULL;
1348 __isl_give isl_union_access_info *isl_union_access_info_copy(
1349 __isl_keep isl_union_access_info *access)
1351 isl_union_access_info *copy;
1353 if (!access)
1354 return NULL;
1355 copy = isl_union_access_info_from_sink(
1356 isl_union_map_copy(access->sink));
1357 copy = isl_union_access_info_set_must_source(copy,
1358 isl_union_map_copy(access->must_source));
1359 copy = isl_union_access_info_set_may_source(copy,
1360 isl_union_map_copy(access->may_source));
1361 if (access->schedule)
1362 copy = isl_union_access_info_set_schedule(copy,
1363 isl_schedule_copy(access->schedule));
1364 else
1365 copy = isl_union_access_info_set_schedule_map(copy,
1366 isl_union_map_copy(access->schedule_map));
1368 return copy;
1371 /* Update the fields of "access" such that they all have the same parameters,
1372 * keeping in mind that the schedule_map field may be NULL and ignoring
1373 * the schedule field.
1375 static __isl_give isl_union_access_info *isl_union_access_info_align_params(
1376 __isl_take isl_union_access_info *access)
1378 isl_space *space;
1380 if (!access)
1381 return NULL;
1383 space = isl_union_map_get_space(access->sink);
1384 space = isl_space_align_params(space,
1385 isl_union_map_get_space(access->must_source));
1386 space = isl_space_align_params(space,
1387 isl_union_map_get_space(access->may_source));
1388 if (access->schedule_map)
1389 space = isl_space_align_params(space,
1390 isl_union_map_get_space(access->schedule_map));
1391 access->sink = isl_union_map_align_params(access->sink,
1392 isl_space_copy(space));
1393 access->must_source = isl_union_map_align_params(access->must_source,
1394 isl_space_copy(space));
1395 access->may_source = isl_union_map_align_params(access->may_source,
1396 isl_space_copy(space));
1397 if (!access->schedule_map) {
1398 isl_space_free(space);
1399 } else {
1400 access->schedule_map =
1401 isl_union_map_align_params(access->schedule_map, space);
1402 if (!access->schedule_map)
1403 return isl_union_access_info_free(access);
1406 if (!access->sink || !access->must_source || !access->may_source)
1407 return isl_union_access_info_free(access);
1409 return access;
1412 /* Prepend the schedule dimensions to the iteration domains.
1414 * That is, if the schedule is of the form
1416 * D -> S
1418 * while the access relations are of the form
1420 * D -> A
1422 * then the updated access relations are of the form
1424 * [S -> D] -> A
1426 * The schedule map is also replaced by the map
1428 * [S -> D] -> D
1430 * that is used during the internal computation.
1431 * Neither the original schedule map nor this updated schedule map
1432 * are used after the call to this function.
1434 static __isl_give isl_union_access_info *
1435 isl_union_access_info_introduce_schedule(
1436 __isl_take isl_union_access_info *access)
1438 isl_union_map *sm;
1440 if (!access)
1441 return NULL;
1443 sm = isl_union_map_reverse(access->schedule_map);
1444 sm = isl_union_map_range_map(sm);
1445 access->sink = isl_union_map_apply_range(isl_union_map_copy(sm),
1446 access->sink);
1447 access->may_source = isl_union_map_apply_range(isl_union_map_copy(sm),
1448 access->may_source);
1449 access->must_source = isl_union_map_apply_range(isl_union_map_copy(sm),
1450 access->must_source);
1451 access->schedule_map = sm;
1453 if (!access->sink || !access->must_source ||
1454 !access->may_source || !access->schedule_map)
1455 return isl_union_access_info_free(access);
1457 return access;
1460 /* This structure epresents the result of a dependence analysis computation.
1462 * "must_dep" represents the definite dependences.
1463 * "may_dep" represents the non-definite dependences.
1464 * "must_no_source" represents the subset of the sink accesses for which
1465 * definitely no source was found.
1466 * "may_no_source" represents the subset of the sink accesses for which
1467 * possibly, but not definitely, no source was found.
1469 struct isl_union_flow {
1470 isl_union_map *must_dep;
1471 isl_union_map *may_dep;
1472 isl_union_map *must_no_source;
1473 isl_union_map *may_no_source;
1476 /* Return the isl_ctx to which "flow" belongs.
1478 isl_ctx *isl_union_flow_get_ctx(__isl_keep isl_union_flow *flow)
1480 return flow ? isl_union_map_get_ctx(flow->must_dep) : NULL;
1483 /* Free "flow" and return NULL.
1485 __isl_null isl_union_flow *isl_union_flow_free(__isl_take isl_union_flow *flow)
1487 if (!flow)
1488 return NULL;
1489 isl_union_map_free(flow->must_dep);
1490 isl_union_map_free(flow->may_dep);
1491 isl_union_map_free(flow->must_no_source);
1492 isl_union_map_free(flow->may_no_source);
1493 free(flow);
1494 return NULL;
1497 void isl_union_flow_dump(__isl_keep isl_union_flow *flow)
1499 if (!flow)
1500 return;
1502 fprintf(stderr, "must dependences: ");
1503 isl_union_map_dump(flow->must_dep);
1504 fprintf(stderr, "may dependences: ");
1505 isl_union_map_dump(flow->may_dep);
1506 fprintf(stderr, "must no source: ");
1507 isl_union_map_dump(flow->must_no_source);
1508 fprintf(stderr, "may no source: ");
1509 isl_union_map_dump(flow->may_no_source);
1512 /* Return the definite dependences in "flow".
1514 __isl_give isl_union_map *isl_union_flow_get_must_dependence(
1515 __isl_keep isl_union_flow *flow)
1517 if (!flow)
1518 return NULL;
1519 return isl_union_map_copy(flow->must_dep);
1522 /* Return the possible dependences in "flow", including the definite
1523 * dependences.
1525 __isl_give isl_union_map *isl_union_flow_get_may_dependence(
1526 __isl_keep isl_union_flow *flow)
1528 if (!flow)
1529 return NULL;
1530 return isl_union_map_union(isl_union_map_copy(flow->must_dep),
1531 isl_union_map_copy(flow->may_dep));
1534 /* Return the non-definite dependences in "flow".
1536 static __isl_give isl_union_map *isl_union_flow_get_non_must_dependence(
1537 __isl_keep isl_union_flow *flow)
1539 if (!flow)
1540 return NULL;
1541 return isl_union_map_copy(flow->may_dep);
1544 /* Return the subset of the sink accesses for which definitely
1545 * no source was found.
1547 __isl_give isl_union_map *isl_union_flow_get_must_no_source(
1548 __isl_keep isl_union_flow *flow)
1550 if (!flow)
1551 return NULL;
1552 return isl_union_map_copy(flow->must_no_source);
1555 /* Return the subset of the sink accesses for which possibly
1556 * no source was found, including those for which definitely
1557 * no source was found.
1559 __isl_give isl_union_map *isl_union_flow_get_may_no_source(
1560 __isl_keep isl_union_flow *flow)
1562 if (!flow)
1563 return NULL;
1564 return isl_union_map_union(isl_union_map_copy(flow->must_no_source),
1565 isl_union_map_copy(flow->may_no_source));
1568 /* Return the subset of the sink accesses for which possibly, but not
1569 * definitely, no source was found.
1571 static __isl_give isl_union_map *isl_union_flow_get_non_must_no_source(
1572 __isl_keep isl_union_flow *flow)
1574 if (!flow)
1575 return NULL;
1576 return isl_union_map_copy(flow->may_no_source);
1579 /* Create a new isl_union_flow object, initialized with empty
1580 * dependence relations and sink subsets.
1582 static __isl_give isl_union_flow *isl_union_flow_alloc(
1583 __isl_take isl_space *space)
1585 isl_ctx *ctx;
1586 isl_union_map *empty;
1587 isl_union_flow *flow;
1589 if (!space)
1590 return NULL;
1591 ctx = isl_space_get_ctx(space);
1592 flow = isl_alloc_type(ctx, isl_union_flow);
1593 if (!flow)
1594 goto error;
1596 empty = isl_union_map_empty(space);
1597 flow->must_dep = isl_union_map_copy(empty);
1598 flow->may_dep = isl_union_map_copy(empty);
1599 flow->must_no_source = isl_union_map_copy(empty);
1600 flow->may_no_source = empty;
1602 if (!flow->must_dep || !flow->may_dep ||
1603 !flow->must_no_source || !flow->may_no_source)
1604 return isl_union_flow_free(flow);
1606 return flow;
1607 error:
1608 isl_space_free(space);
1609 return NULL;
1612 /* Drop the schedule dimensions from the iteration domains in "flow".
1613 * In particular, the schedule dimensions have been prepended
1614 * to the iteration domains prior to the dependence analysis by
1615 * replacing the iteration domain D, by the wrapped map [S -> D].
1616 * Replace these wrapped maps by the original D.
1618 static __isl_give isl_union_flow *isl_union_flow_drop_schedule(
1619 __isl_take isl_union_flow *flow)
1621 if (!flow)
1622 return NULL;
1624 flow->must_dep = isl_union_map_factor_range(flow->must_dep);
1625 flow->may_dep = isl_union_map_factor_range(flow->may_dep);
1626 flow->must_no_source =
1627 isl_union_map_domain_factor_range(flow->must_no_source);
1628 flow->may_no_source =
1629 isl_union_map_domain_factor_range(flow->may_no_source);
1631 if (!flow->must_dep || !flow->may_dep ||
1632 !flow->must_no_source || !flow->may_no_source)
1633 return isl_union_flow_free(flow);
1635 return flow;
1638 struct isl_compute_flow_data {
1639 isl_union_map *must_source;
1640 isl_union_map *may_source;
1641 isl_union_flow *flow;
1643 int count;
1644 int must;
1645 isl_space *dim;
1646 struct isl_sched_info *sink_info;
1647 struct isl_sched_info **source_info;
1648 isl_access_info *accesses;
1651 static isl_stat count_matching_array(__isl_take isl_map *map, void *user)
1653 int eq;
1654 isl_space *dim;
1655 struct isl_compute_flow_data *data;
1657 data = (struct isl_compute_flow_data *)user;
1659 dim = isl_space_range(isl_map_get_space(map));
1661 eq = isl_space_is_equal(dim, data->dim);
1663 isl_space_free(dim);
1664 isl_map_free(map);
1666 if (eq < 0)
1667 return isl_stat_error;
1668 if (eq)
1669 data->count++;
1671 return isl_stat_ok;
1674 static isl_stat collect_matching_array(__isl_take isl_map *map, void *user)
1676 int eq;
1677 isl_space *dim;
1678 struct isl_sched_info *info;
1679 struct isl_compute_flow_data *data;
1681 data = (struct isl_compute_flow_data *)user;
1683 dim = isl_space_range(isl_map_get_space(map));
1685 eq = isl_space_is_equal(dim, data->dim);
1687 isl_space_free(dim);
1689 if (eq < 0)
1690 goto error;
1691 if (!eq) {
1692 isl_map_free(map);
1693 return isl_stat_ok;
1696 info = sched_info_alloc(map);
1697 data->source_info[data->count] = info;
1699 data->accesses = isl_access_info_add_source(data->accesses,
1700 map, data->must, info);
1702 data->count++;
1704 return isl_stat_ok;
1705 error:
1706 isl_map_free(map);
1707 return isl_stat_error;
1710 /* Determine the shared nesting level and the "textual order" of
1711 * the given accesses.
1713 * We first determine the minimal schedule dimension for both accesses.
1715 * If among those dimensions, we can find one where both have a fixed
1716 * value and if moreover those values are different, then the previous
1717 * dimension is the last shared nesting level and the textual order
1718 * is determined based on the order of the fixed values.
1719 * If no such fixed values can be found, then we set the shared
1720 * nesting level to the minimal schedule dimension, with no textual ordering.
1722 static int before(void *first, void *second)
1724 struct isl_sched_info *info1 = first;
1725 struct isl_sched_info *info2 = second;
1726 int n1, n2;
1727 int i;
1729 n1 = isl_vec_size(info1->cst);
1730 n2 = isl_vec_size(info2->cst);
1732 if (n2 < n1)
1733 n1 = n2;
1735 for (i = 0; i < n1; ++i) {
1736 int r;
1737 int cmp;
1739 if (!info1->is_cst[i])
1740 continue;
1741 if (!info2->is_cst[i])
1742 continue;
1743 cmp = isl_vec_cmp_element(info1->cst, info2->cst, i);
1744 if (cmp == 0)
1745 continue;
1747 r = 2 * i + (cmp < 0);
1749 return r;
1752 return 2 * n1;
1755 /* Given a sink access, look for all the source accesses that access
1756 * the same array and perform dataflow analysis on them using
1757 * isl_access_info_compute_flow.
1759 static isl_stat compute_flow(__isl_take isl_map *map, void *user)
1761 int i;
1762 isl_ctx *ctx;
1763 struct isl_compute_flow_data *data;
1764 isl_flow *flow;
1765 isl_union_flow *df;
1767 data = (struct isl_compute_flow_data *)user;
1768 df = data->flow;
1770 ctx = isl_map_get_ctx(map);
1772 data->accesses = NULL;
1773 data->sink_info = NULL;
1774 data->source_info = NULL;
1775 data->count = 0;
1776 data->dim = isl_space_range(isl_map_get_space(map));
1778 if (isl_union_map_foreach_map(data->must_source,
1779 &count_matching_array, data) < 0)
1780 goto error;
1781 if (isl_union_map_foreach_map(data->may_source,
1782 &count_matching_array, data) < 0)
1783 goto error;
1785 data->sink_info = sched_info_alloc(map);
1786 data->source_info = isl_calloc_array(ctx, struct isl_sched_info *,
1787 data->count);
1789 data->accesses = isl_access_info_alloc(isl_map_copy(map),
1790 data->sink_info, &before, data->count);
1791 if (!data->sink_info || (data->count && !data->source_info) ||
1792 !data->accesses)
1793 goto error;
1794 data->count = 0;
1795 data->must = 1;
1796 if (isl_union_map_foreach_map(data->must_source,
1797 &collect_matching_array, data) < 0)
1798 goto error;
1799 data->must = 0;
1800 if (isl_union_map_foreach_map(data->may_source,
1801 &collect_matching_array, data) < 0)
1802 goto error;
1804 flow = isl_access_info_compute_flow(data->accesses);
1805 data->accesses = NULL;
1807 if (!flow)
1808 goto error;
1810 df->must_no_source = isl_union_map_union(df->must_no_source,
1811 isl_union_map_from_map(isl_flow_get_no_source(flow, 1)));
1812 df->may_no_source = isl_union_map_union(df->may_no_source,
1813 isl_union_map_from_map(isl_flow_get_no_source(flow, 0)));
1815 for (i = 0; i < flow->n_source; ++i) {
1816 isl_union_map *dep;
1817 dep = isl_union_map_from_map(isl_map_copy(flow->dep[i].map));
1818 if (flow->dep[i].must)
1819 df->must_dep = isl_union_map_union(df->must_dep, dep);
1820 else
1821 df->may_dep = isl_union_map_union(df->may_dep, dep);
1824 isl_flow_free(flow);
1826 sched_info_free(data->sink_info);
1827 if (data->source_info) {
1828 for (i = 0; i < data->count; ++i)
1829 sched_info_free(data->source_info[i]);
1830 free(data->source_info);
1832 isl_space_free(data->dim);
1833 isl_map_free(map);
1835 return isl_stat_ok;
1836 error:
1837 isl_access_info_free(data->accesses);
1838 sched_info_free(data->sink_info);
1839 if (data->source_info) {
1840 for (i = 0; i < data->count; ++i)
1841 sched_info_free(data->source_info[i]);
1842 free(data->source_info);
1844 isl_space_free(data->dim);
1845 isl_map_free(map);
1847 return isl_stat_error;
1850 /* Remove the must accesses from the may accesses.
1852 * A must access always trumps a may access, so there is no need
1853 * for a must access to also be considered as a may access. Doing so
1854 * would only cost extra computations only to find out that
1855 * the duplicated may access does not make any difference.
1857 static __isl_give isl_union_access_info *isl_union_access_info_normalize(
1858 __isl_take isl_union_access_info *access)
1860 if (!access)
1861 return NULL;
1862 access->may_source = isl_union_map_subtract(access->may_source,
1863 isl_union_map_copy(access->must_source));
1864 if (!access->may_source)
1865 return isl_union_access_info_free(access);
1867 return access;
1870 /* Given a description of the "sink" accesses, the "source" accesses and
1871 * a schedule, compute for each instance of a sink access
1872 * and for each element accessed by that instance,
1873 * the possible or definite source accesses that last accessed the
1874 * element accessed by the sink access before this sink access
1875 * in the sense that there is no intermediate definite source access.
1877 * The must_no_source and may_no_source elements of the result
1878 * are subsets of access->sink. The elements must_dep and may_dep
1879 * map domain elements of access->{may,must)_source to
1880 * domain elements of access->sink.
1882 * This function is used when only the schedule map representation
1883 * is available.
1885 * We first prepend the schedule dimensions to the domain
1886 * of the accesses so that we can easily compare their relative order.
1887 * Then we consider each sink access individually in compute_flow.
1889 static __isl_give isl_union_flow *compute_flow_union_map(
1890 __isl_take isl_union_access_info *access)
1892 struct isl_compute_flow_data data;
1894 access = isl_union_access_info_align_params(access);
1895 access = isl_union_access_info_introduce_schedule(access);
1896 if (!access)
1897 return NULL;
1899 data.must_source = access->must_source;
1900 data.may_source = access->may_source;
1902 data.flow = isl_union_flow_alloc(isl_union_map_get_space(access->sink));
1904 if (isl_union_map_foreach_map(access->sink, &compute_flow, &data) < 0)
1905 goto error;
1907 data.flow = isl_union_flow_drop_schedule(data.flow);
1909 isl_union_access_info_free(access);
1910 return data.flow;
1911 error:
1912 isl_union_access_info_free(access);
1913 isl_union_flow_free(data.flow);
1914 return NULL;
1917 /* A schedule access relation.
1919 * The access relation "access" is of the form [S -> D] -> A,
1920 * where S corresponds to the prefix schedule at "node".
1921 * "must" is only relevant for source accesses and indicates
1922 * whether the access is a must source or a may source.
1924 struct isl_scheduled_access {
1925 isl_map *access;
1926 int must;
1927 isl_schedule_node *node;
1930 /* Data structure for keeping track of individual scheduled sink and source
1931 * accesses when computing dependence analysis based on a schedule tree.
1933 * "n_sink" is the number of used entries in "sink"
1934 * "n_source" is the number of used entries in "source"
1936 * "set_sink", "must" and "node" are only used inside collect_sink_source,
1937 * to keep track of the current node and
1938 * of what extract_sink_source needs to do.
1940 struct isl_compute_flow_schedule_data {
1941 isl_union_access_info *access;
1943 int n_sink;
1944 int n_source;
1946 struct isl_scheduled_access *sink;
1947 struct isl_scheduled_access *source;
1949 int set_sink;
1950 int must;
1951 isl_schedule_node *node;
1954 /* Align the parameters of all sinks with all sources.
1956 * If there are no sinks or no sources, then no alignment is needed.
1958 static void isl_compute_flow_schedule_data_align_params(
1959 struct isl_compute_flow_schedule_data *data)
1961 int i;
1962 isl_space *space;
1964 if (data->n_sink == 0 || data->n_source == 0)
1965 return;
1967 space = isl_map_get_space(data->sink[0].access);
1969 for (i = 1; i < data->n_sink; ++i)
1970 space = isl_space_align_params(space,
1971 isl_map_get_space(data->sink[i].access));
1972 for (i = 0; i < data->n_source; ++i)
1973 space = isl_space_align_params(space,
1974 isl_map_get_space(data->source[i].access));
1976 for (i = 0; i < data->n_sink; ++i)
1977 data->sink[i].access =
1978 isl_map_align_params(data->sink[i].access,
1979 isl_space_copy(space));
1980 for (i = 0; i < data->n_source; ++i)
1981 data->source[i].access =
1982 isl_map_align_params(data->source[i].access,
1983 isl_space_copy(space));
1985 isl_space_free(space);
1988 /* Free all the memory referenced from "data".
1989 * Do not free "data" itself as it may be allocated on the stack.
1991 static void isl_compute_flow_schedule_data_clear(
1992 struct isl_compute_flow_schedule_data *data)
1994 int i;
1996 if (!data->sink)
1997 return;
1999 for (i = 0; i < data->n_sink; ++i) {
2000 isl_map_free(data->sink[i].access);
2001 isl_schedule_node_free(data->sink[i].node);
2004 for (i = 0; i < data->n_source; ++i) {
2005 isl_map_free(data->source[i].access);
2006 isl_schedule_node_free(data->source[i].node);
2009 free(data->sink);
2012 /* isl_schedule_foreach_schedule_node_top_down callback for counting
2013 * (an upper bound on) the number of sinks and sources.
2015 * Sinks and sources are only extracted at leaves of the tree,
2016 * so we skip the node if it is not a leaf.
2017 * Otherwise we increment data->n_sink and data->n_source with
2018 * the number of spaces in the sink and source access domains
2019 * that reach this node.
2021 static isl_bool count_sink_source(__isl_keep isl_schedule_node *node,
2022 void *user)
2024 struct isl_compute_flow_schedule_data *data = user;
2025 isl_union_set *domain;
2026 isl_union_map *umap;
2027 isl_bool r = isl_bool_false;
2029 if (isl_schedule_node_get_type(node) != isl_schedule_node_leaf)
2030 return isl_bool_true;
2032 domain = isl_schedule_node_get_universe_domain(node);
2034 umap = isl_union_map_copy(data->access->sink);
2035 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
2036 data->n_sink += isl_union_map_n_map(umap);
2037 isl_union_map_free(umap);
2038 if (!umap)
2039 r = isl_bool_error;
2041 umap = isl_union_map_copy(data->access->must_source);
2042 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
2043 data->n_source += isl_union_map_n_map(umap);
2044 isl_union_map_free(umap);
2045 if (!umap)
2046 r = isl_bool_error;
2048 umap = isl_union_map_copy(data->access->may_source);
2049 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
2050 data->n_source += isl_union_map_n_map(umap);
2051 isl_union_map_free(umap);
2052 if (!umap)
2053 r = isl_bool_error;
2055 isl_union_set_free(domain);
2057 return r;
2060 /* Add a single scheduled sink or source (depending on data->set_sink)
2061 * with scheduled access relation "map", must property data->must and
2062 * schedule node data->node to the list of sinks or sources.
2064 static isl_stat extract_sink_source(__isl_take isl_map *map, void *user)
2066 struct isl_compute_flow_schedule_data *data = user;
2067 struct isl_scheduled_access *access;
2069 if (data->set_sink)
2070 access = data->sink + data->n_sink++;
2071 else
2072 access = data->source + data->n_source++;
2074 access->access = map;
2075 access->must = data->must;
2076 access->node = isl_schedule_node_copy(data->node);
2078 return isl_stat_ok;
2081 /* isl_schedule_foreach_schedule_node_top_down callback for collecting
2082 * individual scheduled source and sink accesses.
2084 * We only collect accesses at the leaves of the schedule tree.
2085 * We prepend the schedule dimensions at the leaf to the iteration
2086 * domains of the source and sink accesses and then extract
2087 * the individual accesses (per space).
2089 * In particular, if the prefix schedule at the node is of the form
2091 * D -> S
2093 * while the access relations are of the form
2095 * D -> A
2097 * then the updated access relations are of the form
2099 * [S -> D] -> A
2101 * Note that S consists of a single space such that introducing S
2102 * in the access relations does not increase the number of spaces.
2104 static isl_bool collect_sink_source(__isl_keep isl_schedule_node *node,
2105 void *user)
2107 struct isl_compute_flow_schedule_data *data = user;
2108 isl_union_map *prefix;
2109 isl_union_map *umap;
2110 isl_bool r = isl_bool_false;
2112 if (isl_schedule_node_get_type(node) != isl_schedule_node_leaf)
2113 return isl_bool_true;
2115 data->node = node;
2117 prefix = isl_schedule_node_get_prefix_schedule_union_map(node);
2118 prefix = isl_union_map_reverse(prefix);
2119 prefix = isl_union_map_range_map(prefix);
2121 data->set_sink = 1;
2122 umap = isl_union_map_copy(data->access->sink);
2123 umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
2124 if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
2125 r = isl_bool_error;
2126 isl_union_map_free(umap);
2128 data->set_sink = 0;
2129 data->must = 1;
2130 umap = isl_union_map_copy(data->access->must_source);
2131 umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
2132 if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
2133 r = isl_bool_error;
2134 isl_union_map_free(umap);
2136 data->set_sink = 0;
2137 data->must = 0;
2138 umap = isl_union_map_copy(data->access->may_source);
2139 umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
2140 if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
2141 r = isl_bool_error;
2142 isl_union_map_free(umap);
2144 isl_union_map_free(prefix);
2146 return r;
2149 /* isl_access_info_compute_flow callback for determining whether
2150 * the shared nesting level and the ordering within that level
2151 * for two scheduled accesses for use in compute_single_flow.
2153 * The tokens passed to this function refer to the leaves
2154 * in the schedule tree where the accesses take place.
2156 * If n is the shared number of loops, then we need to return
2157 * "2 * n + 1" if "first" precedes "second" inside the innermost
2158 * shared loop and "2 * n" otherwise.
2160 * The innermost shared ancestor may be the leaves themselves
2161 * if the accesses take place in the same leaf. Otherwise,
2162 * it is either a set node or a sequence node. Only in the case
2163 * of a sequence node do we consider one access to precede the other.
2165 static int before_node(void *first, void *second)
2167 isl_schedule_node *node1 = first;
2168 isl_schedule_node *node2 = second;
2169 isl_schedule_node *shared;
2170 int depth;
2171 int before = 0;
2173 shared = isl_schedule_node_get_shared_ancestor(node1, node2);
2174 if (!shared)
2175 return -1;
2177 depth = isl_schedule_node_get_schedule_depth(shared);
2178 if (isl_schedule_node_get_type(shared) == isl_schedule_node_sequence) {
2179 int pos1, pos2;
2181 pos1 = isl_schedule_node_get_ancestor_child_position(node1,
2182 shared);
2183 pos2 = isl_schedule_node_get_ancestor_child_position(node2,
2184 shared);
2185 before = pos1 < pos2;
2188 isl_schedule_node_free(shared);
2190 return 2 * depth + before;
2193 /* Add the scheduled sources from "data" that access
2194 * the same data space as "sink" to "access".
2196 static __isl_give isl_access_info *add_matching_sources(
2197 __isl_take isl_access_info *access, struct isl_scheduled_access *sink,
2198 struct isl_compute_flow_schedule_data *data)
2200 int i;
2201 isl_space *space;
2203 space = isl_space_range(isl_map_get_space(sink->access));
2204 for (i = 0; i < data->n_source; ++i) {
2205 struct isl_scheduled_access *source;
2206 isl_space *source_space;
2207 int eq;
2209 source = &data->source[i];
2210 source_space = isl_map_get_space(source->access);
2211 source_space = isl_space_range(source_space);
2212 eq = isl_space_is_equal(space, source_space);
2213 isl_space_free(source_space);
2215 if (!eq)
2216 continue;
2217 if (eq < 0)
2218 goto error;
2220 access = isl_access_info_add_source(access,
2221 isl_map_copy(source->access), source->must, source->node);
2224 isl_space_free(space);
2225 return access;
2226 error:
2227 isl_space_free(space);
2228 isl_access_info_free(access);
2229 return NULL;
2232 /* Given a scheduled sink access relation "sink", compute the corresponding
2233 * dependences on the sources in "data" and add the computed dependences
2234 * to "uf".
2236 static __isl_give isl_union_flow *compute_single_flow(
2237 __isl_take isl_union_flow *uf, struct isl_scheduled_access *sink,
2238 struct isl_compute_flow_schedule_data *data)
2240 int i;
2241 isl_access_info *access;
2242 isl_flow *flow;
2243 isl_map *map;
2245 if (!uf)
2246 return NULL;
2248 access = isl_access_info_alloc(isl_map_copy(sink->access), sink->node,
2249 &before_node, data->n_source);
2250 access = add_matching_sources(access, sink, data);
2252 flow = isl_access_info_compute_flow(access);
2253 if (!flow)
2254 return isl_union_flow_free(uf);
2256 map = isl_map_domain_factor_range(isl_flow_get_no_source(flow, 1));
2257 uf->must_no_source = isl_union_map_union(uf->must_no_source,
2258 isl_union_map_from_map(map));
2259 map = isl_map_domain_factor_range(isl_flow_get_no_source(flow, 0));
2260 uf->may_no_source = isl_union_map_union(uf->may_no_source,
2261 isl_union_map_from_map(map));
2263 for (i = 0; i < flow->n_source; ++i) {
2264 isl_union_map *dep;
2266 map = isl_map_factor_range(isl_map_copy(flow->dep[i].map));
2267 dep = isl_union_map_from_map(map);
2268 if (flow->dep[i].must)
2269 uf->must_dep = isl_union_map_union(uf->must_dep, dep);
2270 else
2271 uf->may_dep = isl_union_map_union(uf->may_dep, dep);
2274 isl_flow_free(flow);
2276 return uf;
2279 /* Given a description of the "sink" accesses, the "source" accesses and
2280 * a schedule, compute for each instance of a sink access
2281 * and for each element accessed by that instance,
2282 * the possible or definite source accesses that last accessed the
2283 * element accessed by the sink access before this sink access
2284 * in the sense that there is no intermediate definite source access.
2286 * The must_no_source and may_no_source elements of the result
2287 * are subsets of access->sink. The elements must_dep and may_dep
2288 * map domain elements of access->{may,must)_source to
2289 * domain elements of access->sink.
2291 * This function is used when a schedule tree representation
2292 * is available.
2294 * We extract the individual scheduled source and sink access relations and
2295 * then compute dependences for each scheduled sink individually.
2297 static __isl_give isl_union_flow *compute_flow_schedule(
2298 __isl_take isl_union_access_info *access)
2300 struct isl_compute_flow_schedule_data data = { access };
2301 int i, n;
2302 isl_ctx *ctx;
2303 isl_union_flow *flow;
2305 ctx = isl_union_access_info_get_ctx(access);
2307 data.n_sink = 0;
2308 data.n_source = 0;
2309 if (isl_schedule_foreach_schedule_node_top_down(access->schedule,
2310 &count_sink_source, &data) < 0)
2311 goto error;
2313 n = data.n_sink + data.n_source;
2314 data.sink = isl_calloc_array(ctx, struct isl_scheduled_access, n);
2315 if (n && !data.sink)
2316 goto error;
2317 data.source = data.sink + data.n_sink;
2319 data.n_sink = 0;
2320 data.n_source = 0;
2321 if (isl_schedule_foreach_schedule_node_top_down(access->schedule,
2322 &collect_sink_source, &data) < 0)
2323 goto error;
2325 flow = isl_union_flow_alloc(isl_union_map_get_space(access->sink));
2327 isl_compute_flow_schedule_data_align_params(&data);
2329 for (i = 0; i < data.n_sink; ++i)
2330 flow = compute_single_flow(flow, &data.sink[i], &data);
2332 isl_compute_flow_schedule_data_clear(&data);
2334 isl_union_access_info_free(access);
2335 return flow;
2336 error:
2337 isl_union_access_info_free(access);
2338 isl_compute_flow_schedule_data_clear(&data);
2339 return NULL;
2342 /* Given a description of the "sink" accesses, the "source" accesses and
2343 * a schedule, compute for each instance of a sink access
2344 * and for each element accessed by that instance,
2345 * the possible or definite source accesses that last accessed the
2346 * element accessed by the sink access before this sink access
2347 * in the sense that there is no intermediate definite source access.
2349 * The must_no_source and may_no_source elements of the result
2350 * are subsets of access->sink. The elements must_dep and may_dep
2351 * map domain elements of access->{may,must)_source to
2352 * domain elements of access->sink.
2354 * We check whether the schedule is available as a schedule tree
2355 * or a schedule map and call the correpsonding function to perform
2356 * the analysis.
2358 __isl_give isl_union_flow *isl_union_access_info_compute_flow(
2359 __isl_take isl_union_access_info *access)
2361 access = isl_union_access_info_normalize(access);
2362 if (!access)
2363 return NULL;
2364 if (access->schedule)
2365 return compute_flow_schedule(access);
2366 else
2367 return compute_flow_union_map(access);
2370 /* Given a collection of "sink" and "source" accesses,
2371 * compute for each iteration of a sink access
2372 * and for each element accessed by that iteration,
2373 * the source access in the list that last accessed the
2374 * element accessed by the sink access before this sink access.
2375 * Each access is given as a map from the loop iterators
2376 * to the array indices.
2377 * The result is a relations between source and sink
2378 * iterations and a subset of the domain of the sink accesses,
2379 * corresponding to those iterations that access an element
2380 * not previously accessed.
2382 * We collect the inputs in an isl_union_access_info object,
2383 * call isl_union_access_info_compute_flow and extract
2384 * the outputs from the result.
2386 int isl_union_map_compute_flow(__isl_take isl_union_map *sink,
2387 __isl_take isl_union_map *must_source,
2388 __isl_take isl_union_map *may_source,
2389 __isl_take isl_union_map *schedule,
2390 __isl_give isl_union_map **must_dep, __isl_give isl_union_map **may_dep,
2391 __isl_give isl_union_map **must_no_source,
2392 __isl_give isl_union_map **may_no_source)
2394 isl_union_access_info *access;
2395 isl_union_flow *flow;
2397 access = isl_union_access_info_from_sink(sink);
2398 access = isl_union_access_info_set_must_source(access, must_source);
2399 access = isl_union_access_info_set_may_source(access, may_source);
2400 access = isl_union_access_info_set_schedule_map(access, schedule);
2401 flow = isl_union_access_info_compute_flow(access);
2403 if (must_dep)
2404 *must_dep = isl_union_flow_get_must_dependence(flow);
2405 if (may_dep)
2406 *may_dep = isl_union_flow_get_non_must_dependence(flow);
2407 if (must_no_source)
2408 *must_no_source = isl_union_flow_get_must_no_source(flow);
2409 if (may_no_source)
2410 *may_no_source = isl_union_flow_get_non_must_no_source(flow);
2412 isl_union_flow_free(flow);
2414 if ((must_dep && !*must_dep) || (may_dep && !*may_dep) ||
2415 (must_no_source && !*must_no_source) ||
2416 (may_no_source && !*may_no_source))
2417 goto error;
2419 return 0;
2420 error:
2421 if (must_dep)
2422 *must_dep = isl_union_map_free(*must_dep);
2423 if (may_dep)
2424 *may_dep = isl_union_map_free(*may_dep);
2425 if (must_no_source)
2426 *must_no_source = isl_union_map_free(*must_no_source);
2427 if (may_no_source)
2428 *may_no_source = isl_union_map_free(*may_no_source);
2429 return -1;