export isl_union_map_*_factor_*
[isl.git] / isl_flow.c
blob2b0911c40fdb1a3b2161c43fe4a2fd7bbdf37c03
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.
170 * This field is only needed when restrict_fn is set and
171 * the field itself is set by isl_access_info_compute_flow.
173 * restrict_fn is a callback that (if not NULL) will be called
174 * right before any lexicographical maximization.
176 struct isl_access_info {
177 isl_map *domain_map;
178 struct isl_labeled_map sink;
179 isl_access_level_before level_before;
181 isl_access_restrict restrict_fn;
182 void *restrict_user;
184 int max_source;
185 int n_must;
186 int n_may;
187 struct isl_labeled_map source[1];
190 /* A structure containing the output of dependence analysis:
191 * - n_source dependences
192 * - a wrapped subset of the sink for which definitely no source could be found
193 * - a wrapped subset of the sink for which possibly no source could be found
195 struct isl_flow {
196 isl_set *must_no_source;
197 isl_set *may_no_source;
198 int n_source;
199 struct isl_labeled_map *dep;
202 /* Construct an isl_access_info structure and fill it up with
203 * the given data. The number of sources is set to 0.
205 __isl_give isl_access_info *isl_access_info_alloc(__isl_take isl_map *sink,
206 void *sink_user, isl_access_level_before fn, int max_source)
208 isl_ctx *ctx;
209 struct isl_access_info *acc;
211 if (!sink)
212 return NULL;
214 ctx = isl_map_get_ctx(sink);
215 isl_assert(ctx, max_source >= 0, goto error);
217 acc = isl_calloc(ctx, struct isl_access_info,
218 sizeof(struct isl_access_info) +
219 (max_source - 1) * sizeof(struct isl_labeled_map));
220 if (!acc)
221 goto error;
223 acc->sink.map = sink;
224 acc->sink.data = sink_user;
225 acc->level_before = fn;
226 acc->max_source = max_source;
227 acc->n_must = 0;
228 acc->n_may = 0;
230 return acc;
231 error:
232 isl_map_free(sink);
233 return NULL;
236 /* Free the given isl_access_info structure.
238 __isl_null isl_access_info *isl_access_info_free(
239 __isl_take isl_access_info *acc)
241 int i;
243 if (!acc)
244 return NULL;
245 isl_map_free(acc->domain_map);
246 isl_map_free(acc->sink.map);
247 for (i = 0; i < acc->n_must + acc->n_may; ++i)
248 isl_map_free(acc->source[i].map);
249 free(acc);
250 return NULL;
253 isl_ctx *isl_access_info_get_ctx(__isl_keep isl_access_info *acc)
255 return acc ? isl_map_get_ctx(acc->sink.map) : NULL;
258 __isl_give isl_access_info *isl_access_info_set_restrict(
259 __isl_take isl_access_info *acc, isl_access_restrict fn, void *user)
261 if (!acc)
262 return NULL;
263 acc->restrict_fn = fn;
264 acc->restrict_user = user;
265 return acc;
268 /* Add another source to an isl_access_info structure, making
269 * sure the "must" sources are placed before the "may" sources.
270 * This function may be called at most max_source times on a
271 * given isl_access_info structure, with max_source as specified
272 * in the call to isl_access_info_alloc that constructed the structure.
274 __isl_give isl_access_info *isl_access_info_add_source(
275 __isl_take isl_access_info *acc, __isl_take isl_map *source,
276 int must, void *source_user)
278 isl_ctx *ctx;
280 if (!acc)
281 goto error;
282 ctx = isl_map_get_ctx(acc->sink.map);
283 isl_assert(ctx, acc->n_must + acc->n_may < acc->max_source, goto error);
285 if (must) {
286 if (acc->n_may)
287 acc->source[acc->n_must + acc->n_may] =
288 acc->source[acc->n_must];
289 acc->source[acc->n_must].map = source;
290 acc->source[acc->n_must].data = source_user;
291 acc->source[acc->n_must].must = 1;
292 acc->n_must++;
293 } else {
294 acc->source[acc->n_must + acc->n_may].map = source;
295 acc->source[acc->n_must + acc->n_may].data = source_user;
296 acc->source[acc->n_must + acc->n_may].must = 0;
297 acc->n_may++;
300 return acc;
301 error:
302 isl_map_free(source);
303 isl_access_info_free(acc);
304 return NULL;
307 /* Return -n, 0 or n (with n a positive value), depending on whether
308 * the source access identified by p1 should be sorted before, together
309 * or after that identified by p2.
311 * If p1 appears before p2, then it should be sorted first.
312 * For more generic initial schedules, it is possible that neither
313 * p1 nor p2 appears before the other, or at least not in any obvious way.
314 * We therefore also check if p2 appears before p1, in which case p2
315 * should be sorted first.
316 * If not, we try to order the two statements based on the description
317 * of the iteration domains. This results in an arbitrary, but fairly
318 * stable ordering.
320 static int access_sort_cmp(const void *p1, const void *p2, void *user)
322 isl_access_info *acc = user;
323 const struct isl_labeled_map *i1, *i2;
324 int level1, level2;
325 uint32_t h1, h2;
326 i1 = (const struct isl_labeled_map *) p1;
327 i2 = (const struct isl_labeled_map *) p2;
329 level1 = acc->level_before(i1->data, i2->data);
330 if (level1 % 2)
331 return -1;
333 level2 = acc->level_before(i2->data, i1->data);
334 if (level2 % 2)
335 return 1;
337 h1 = isl_map_get_hash(i1->map);
338 h2 = isl_map_get_hash(i2->map);
339 return h1 > h2 ? 1 : h1 < h2 ? -1 : 0;
342 /* Sort the must source accesses in their textual order.
344 static __isl_give isl_access_info *isl_access_info_sort_sources(
345 __isl_take isl_access_info *acc)
347 if (!acc)
348 return NULL;
349 if (acc->n_must <= 1)
350 return acc;
352 if (isl_sort(acc->source, acc->n_must, sizeof(struct isl_labeled_map),
353 access_sort_cmp, acc) < 0)
354 return isl_access_info_free(acc);
356 return acc;
359 /* Align the parameters of the two spaces if needed and then call
360 * isl_space_join.
362 static __isl_give isl_space *space_align_and_join(__isl_take isl_space *left,
363 __isl_take isl_space *right)
365 if (isl_space_match(left, isl_dim_param, right, isl_dim_param))
366 return isl_space_join(left, right);
368 left = isl_space_align_params(left, isl_space_copy(right));
369 right = isl_space_align_params(right, isl_space_copy(left));
370 return isl_space_join(left, right);
373 /* Initialize an empty isl_flow structure corresponding to a given
374 * isl_access_info structure.
375 * For each must access, two dependences are created (initialized
376 * to the empty relation), one for the resulting must dependences
377 * and one for the resulting may dependences. May accesses can
378 * only lead to may dependences, so only one dependence is created
379 * for each of them.
380 * This function is private as isl_flow structures are only supposed
381 * to be created by isl_access_info_compute_flow.
383 static __isl_give isl_flow *isl_flow_alloc(__isl_keep isl_access_info *acc)
385 int i, n;
386 struct isl_ctx *ctx;
387 struct isl_flow *dep;
389 if (!acc)
390 return NULL;
392 ctx = isl_map_get_ctx(acc->sink.map);
393 dep = isl_calloc_type(ctx, struct isl_flow);
394 if (!dep)
395 return NULL;
397 n = 2 * acc->n_must + acc->n_may;
398 dep->dep = isl_calloc_array(ctx, struct isl_labeled_map, n);
399 if (n && !dep->dep)
400 goto error;
402 dep->n_source = n;
403 for (i = 0; i < acc->n_must; ++i) {
404 isl_space *dim;
405 dim = space_align_and_join(
406 isl_map_get_space(acc->source[i].map),
407 isl_space_reverse(isl_map_get_space(acc->sink.map)));
408 dep->dep[2 * i].map = isl_map_empty(dim);
409 dep->dep[2 * i + 1].map = isl_map_copy(dep->dep[2 * i].map);
410 dep->dep[2 * i].data = acc->source[i].data;
411 dep->dep[2 * i + 1].data = acc->source[i].data;
412 dep->dep[2 * i].must = 1;
413 dep->dep[2 * i + 1].must = 0;
414 if (!dep->dep[2 * i].map || !dep->dep[2 * i + 1].map)
415 goto error;
417 for (i = acc->n_must; i < acc->n_must + acc->n_may; ++i) {
418 isl_space *dim;
419 dim = space_align_and_join(
420 isl_map_get_space(acc->source[i].map),
421 isl_space_reverse(isl_map_get_space(acc->sink.map)));
422 dep->dep[acc->n_must + i].map = isl_map_empty(dim);
423 dep->dep[acc->n_must + i].data = acc->source[i].data;
424 dep->dep[acc->n_must + i].must = 0;
425 if (!dep->dep[acc->n_must + i].map)
426 goto error;
429 return dep;
430 error:
431 isl_flow_free(dep);
432 return NULL;
435 /* Iterate over all sources and for each resulting flow dependence
436 * that is not empty, call the user specfied function.
437 * The second argument in this function call identifies the source,
438 * while the third argument correspond to the final argument of
439 * the isl_flow_foreach call.
441 isl_stat isl_flow_foreach(__isl_keep isl_flow *deps,
442 isl_stat (*fn)(__isl_take isl_map *dep, int must, void *dep_user,
443 void *user),
444 void *user)
446 int i;
448 if (!deps)
449 return isl_stat_error;
451 for (i = 0; i < deps->n_source; ++i) {
452 if (isl_map_plain_is_empty(deps->dep[i].map))
453 continue;
454 if (fn(isl_map_copy(deps->dep[i].map), deps->dep[i].must,
455 deps->dep[i].data, user) < 0)
456 return isl_stat_error;
459 return isl_stat_ok;
462 /* Return a copy of the subset of the sink for which no source could be found.
464 __isl_give isl_map *isl_flow_get_no_source(__isl_keep isl_flow *deps, int must)
466 if (!deps)
467 return NULL;
469 if (must)
470 return isl_set_unwrap(isl_set_copy(deps->must_no_source));
471 else
472 return isl_set_unwrap(isl_set_copy(deps->may_no_source));
475 void isl_flow_free(__isl_take isl_flow *deps)
477 int i;
479 if (!deps)
480 return;
481 isl_set_free(deps->must_no_source);
482 isl_set_free(deps->may_no_source);
483 if (deps->dep) {
484 for (i = 0; i < deps->n_source; ++i)
485 isl_map_free(deps->dep[i].map);
486 free(deps->dep);
488 free(deps);
491 isl_ctx *isl_flow_get_ctx(__isl_keep isl_flow *deps)
493 return deps ? isl_set_get_ctx(deps->must_no_source) : NULL;
496 /* Return a map that enforces that the domain iteration occurs after
497 * the range iteration at the given level.
498 * If level is odd, then the domain iteration should occur after
499 * the target iteration in their shared level/2 outermost loops.
500 * In this case we simply need to enforce that these outermost
501 * loop iterations are the same.
502 * If level is even, then the loop iterator of the domain should
503 * be greater than the loop iterator of the range at the last
504 * of the level/2 shared loops, i.e., loop level/2 - 1.
506 static __isl_give isl_map *after_at_level(__isl_take isl_space *dim, int level)
508 struct isl_basic_map *bmap;
510 if (level % 2)
511 bmap = isl_basic_map_equal(dim, level/2);
512 else
513 bmap = isl_basic_map_more_at(dim, level/2 - 1);
515 return isl_map_from_basic_map(bmap);
518 /* Compute the partial lexicographic maximum of "dep" on domain "sink",
519 * but first check if the user has set acc->restrict_fn and if so
520 * update either the input or the output of the maximization problem
521 * with respect to the resulting restriction.
523 * Since the user expects a mapping from sink iterations to source iterations,
524 * whereas the domain of "dep" is a wrapped map, mapping sink iterations
525 * to accessed array elements, we first need to project out the accessed
526 * sink array elements by applying acc->domain_map.
527 * Similarly, the sink restriction specified by the user needs to be
528 * converted back to the wrapped map.
530 static __isl_give isl_map *restricted_partial_lexmax(
531 __isl_keep isl_access_info *acc, __isl_take isl_map *dep,
532 int source, __isl_take isl_set *sink, __isl_give isl_set **empty)
534 isl_map *source_map;
535 isl_restriction *restr;
536 isl_set *sink_domain;
537 isl_set *sink_restr;
538 isl_map *res;
540 if (!acc->restrict_fn)
541 return isl_map_partial_lexmax(dep, sink, empty);
543 source_map = isl_map_copy(dep);
544 source_map = isl_map_apply_domain(source_map,
545 isl_map_copy(acc->domain_map));
546 sink_domain = isl_set_copy(sink);
547 sink_domain = isl_set_apply(sink_domain, isl_map_copy(acc->domain_map));
548 restr = acc->restrict_fn(source_map, sink_domain,
549 acc->source[source].data, acc->restrict_user);
550 isl_set_free(sink_domain);
551 isl_map_free(source_map);
553 if (!restr)
554 goto error;
555 if (restr->type == isl_restriction_type_input) {
556 dep = isl_map_intersect_range(dep, isl_set_copy(restr->source));
557 sink_restr = isl_set_copy(restr->sink);
558 sink_restr = isl_set_apply(sink_restr,
559 isl_map_reverse(isl_map_copy(acc->domain_map)));
560 sink = isl_set_intersect(sink, sink_restr);
561 } else if (restr->type == isl_restriction_type_empty) {
562 isl_space *space = isl_map_get_space(dep);
563 isl_map_free(dep);
564 dep = isl_map_empty(space);
567 res = isl_map_partial_lexmax(dep, sink, empty);
569 if (restr->type == isl_restriction_type_output)
570 res = isl_map_intersect_range(res, isl_set_copy(restr->source));
572 isl_restriction_free(restr);
573 return res;
574 error:
575 isl_map_free(dep);
576 isl_set_free(sink);
577 *empty = NULL;
578 return NULL;
581 /* Compute the last iteration of must source j that precedes the sink
582 * at the given level for sink iterations in set_C.
583 * The subset of set_C for which no such iteration can be found is returned
584 * in *empty.
586 static struct isl_map *last_source(struct isl_access_info *acc,
587 struct isl_set *set_C,
588 int j, int level, struct isl_set **empty)
590 struct isl_map *read_map;
591 struct isl_map *write_map;
592 struct isl_map *dep_map;
593 struct isl_map *after;
594 struct isl_map *result;
596 read_map = isl_map_copy(acc->sink.map);
597 write_map = isl_map_copy(acc->source[j].map);
598 write_map = isl_map_reverse(write_map);
599 dep_map = isl_map_apply_range(read_map, write_map);
600 after = after_at_level(isl_map_get_space(dep_map), level);
601 dep_map = isl_map_intersect(dep_map, after);
602 result = restricted_partial_lexmax(acc, dep_map, j, set_C, empty);
603 result = isl_map_reverse(result);
605 return result;
608 /* For a given mapping between iterations of must source j and iterations
609 * of the sink, compute the last iteration of must source k preceding
610 * the sink at level before_level for any of the sink iterations,
611 * but following the corresponding iteration of must source j at level
612 * after_level.
614 static struct isl_map *last_later_source(struct isl_access_info *acc,
615 struct isl_map *old_map,
616 int j, int before_level,
617 int k, int after_level,
618 struct isl_set **empty)
620 isl_space *dim;
621 struct isl_set *set_C;
622 struct isl_map *read_map;
623 struct isl_map *write_map;
624 struct isl_map *dep_map;
625 struct isl_map *after_write;
626 struct isl_map *before_read;
627 struct isl_map *result;
629 set_C = isl_map_range(isl_map_copy(old_map));
630 read_map = isl_map_copy(acc->sink.map);
631 write_map = isl_map_copy(acc->source[k].map);
633 write_map = isl_map_reverse(write_map);
634 dep_map = isl_map_apply_range(read_map, write_map);
635 dim = space_align_and_join(isl_map_get_space(acc->source[k].map),
636 isl_space_reverse(isl_map_get_space(acc->source[j].map)));
637 after_write = after_at_level(dim, after_level);
638 after_write = isl_map_apply_range(after_write, old_map);
639 after_write = isl_map_reverse(after_write);
640 dep_map = isl_map_intersect(dep_map, after_write);
641 before_read = after_at_level(isl_map_get_space(dep_map), before_level);
642 dep_map = isl_map_intersect(dep_map, before_read);
643 result = restricted_partial_lexmax(acc, dep_map, k, set_C, empty);
644 result = isl_map_reverse(result);
646 return result;
649 /* Given a shared_level between two accesses, return 1 if the
650 * the first can precede the second at the requested target_level.
651 * If the target level is odd, i.e., refers to a statement level
652 * dimension, then first needs to precede second at the requested
653 * level, i.e., shared_level must be equal to target_level.
654 * If the target level is odd, then the two loops should share
655 * at least the requested number of outer loops.
657 static int can_precede_at_level(int shared_level, int target_level)
659 if (shared_level < target_level)
660 return 0;
661 if ((target_level % 2) && shared_level > target_level)
662 return 0;
663 return 1;
666 /* Given a possible flow dependence temp_rel[j] between source j and the sink
667 * at level sink_level, remove those elements for which
668 * there is an iteration of another source k < j that is closer to the sink.
669 * The flow dependences temp_rel[k] are updated with the improved sources.
670 * Any improved source needs to precede the sink at the same level
671 * and needs to follow source j at the same or a deeper level.
672 * The lower this level, the later the execution date of source k.
673 * We therefore consider lower levels first.
675 * If temp_rel[j] is empty, then there can be no improvement and
676 * we return immediately.
678 static int intermediate_sources(__isl_keep isl_access_info *acc,
679 struct isl_map **temp_rel, int j, int sink_level)
681 int k, level;
682 int depth = 2 * isl_map_dim(acc->source[j].map, isl_dim_in) + 1;
684 if (isl_map_plain_is_empty(temp_rel[j]))
685 return 0;
687 for (k = j - 1; k >= 0; --k) {
688 int plevel, plevel2;
689 plevel = acc->level_before(acc->source[k].data, acc->sink.data);
690 if (!can_precede_at_level(plevel, sink_level))
691 continue;
693 plevel2 = acc->level_before(acc->source[j].data,
694 acc->source[k].data);
696 for (level = sink_level; level <= depth; ++level) {
697 struct isl_map *T;
698 struct isl_set *trest;
699 struct isl_map *copy;
701 if (!can_precede_at_level(plevel2, level))
702 continue;
704 copy = isl_map_copy(temp_rel[j]);
705 T = last_later_source(acc, copy, j, sink_level, k,
706 level, &trest);
707 if (isl_map_plain_is_empty(T)) {
708 isl_set_free(trest);
709 isl_map_free(T);
710 continue;
712 temp_rel[j] = isl_map_intersect_range(temp_rel[j], trest);
713 temp_rel[k] = isl_map_union_disjoint(temp_rel[k], T);
717 return 0;
720 /* Compute all iterations of may source j that precedes the sink at the given
721 * level for sink iterations in set_C.
723 static __isl_give isl_map *all_sources(__isl_keep isl_access_info *acc,
724 __isl_take isl_set *set_C, int j, int level)
726 isl_map *read_map;
727 isl_map *write_map;
728 isl_map *dep_map;
729 isl_map *after;
731 read_map = isl_map_copy(acc->sink.map);
732 read_map = isl_map_intersect_domain(read_map, set_C);
733 write_map = isl_map_copy(acc->source[acc->n_must + j].map);
734 write_map = isl_map_reverse(write_map);
735 dep_map = isl_map_apply_range(read_map, write_map);
736 after = after_at_level(isl_map_get_space(dep_map), level);
737 dep_map = isl_map_intersect(dep_map, after);
739 return isl_map_reverse(dep_map);
742 /* For a given mapping between iterations of must source k and iterations
743 * of the sink, compute the all iteration of may source j preceding
744 * the sink at level before_level for any of the sink iterations,
745 * but following the corresponding iteration of must source k at level
746 * after_level.
748 static __isl_give isl_map *all_later_sources(__isl_keep isl_access_info *acc,
749 __isl_take isl_map *old_map,
750 int j, int before_level, int k, int after_level)
752 isl_space *dim;
753 isl_set *set_C;
754 isl_map *read_map;
755 isl_map *write_map;
756 isl_map *dep_map;
757 isl_map *after_write;
758 isl_map *before_read;
760 set_C = isl_map_range(isl_map_copy(old_map));
761 read_map = isl_map_copy(acc->sink.map);
762 read_map = isl_map_intersect_domain(read_map, set_C);
763 write_map = isl_map_copy(acc->source[acc->n_must + j].map);
765 write_map = isl_map_reverse(write_map);
766 dep_map = isl_map_apply_range(read_map, write_map);
767 dim = isl_space_join(isl_map_get_space(acc->source[acc->n_must + j].map),
768 isl_space_reverse(isl_map_get_space(acc->source[k].map)));
769 after_write = after_at_level(dim, after_level);
770 after_write = isl_map_apply_range(after_write, old_map);
771 after_write = isl_map_reverse(after_write);
772 dep_map = isl_map_intersect(dep_map, after_write);
773 before_read = after_at_level(isl_map_get_space(dep_map), before_level);
774 dep_map = isl_map_intersect(dep_map, before_read);
775 return isl_map_reverse(dep_map);
778 /* Given the must and may dependence relations for the must accesses
779 * for level sink_level, check if there are any accesses of may access j
780 * that occur in between and return their union.
781 * If some of these accesses are intermediate with respect to
782 * (previously thought to be) must dependences, then these
783 * must dependences are turned into may dependences.
785 static __isl_give isl_map *all_intermediate_sources(
786 __isl_keep isl_access_info *acc, __isl_take isl_map *map,
787 struct isl_map **must_rel, struct isl_map **may_rel,
788 int j, int sink_level)
790 int k, level;
791 int depth = 2 * isl_map_dim(acc->source[acc->n_must + j].map,
792 isl_dim_in) + 1;
794 for (k = 0; k < acc->n_must; ++k) {
795 int plevel;
797 if (isl_map_plain_is_empty(may_rel[k]) &&
798 isl_map_plain_is_empty(must_rel[k]))
799 continue;
801 plevel = acc->level_before(acc->source[k].data,
802 acc->source[acc->n_must + j].data);
804 for (level = sink_level; level <= depth; ++level) {
805 isl_map *T;
806 isl_map *copy;
807 isl_set *ran;
809 if (!can_precede_at_level(plevel, level))
810 continue;
812 copy = isl_map_copy(may_rel[k]);
813 T = all_later_sources(acc, copy, j, sink_level, k, level);
814 map = isl_map_union(map, T);
816 copy = isl_map_copy(must_rel[k]);
817 T = all_later_sources(acc, copy, j, sink_level, k, level);
818 ran = isl_map_range(isl_map_copy(T));
819 map = isl_map_union(map, T);
820 may_rel[k] = isl_map_union_disjoint(may_rel[k],
821 isl_map_intersect_range(isl_map_copy(must_rel[k]),
822 isl_set_copy(ran)));
823 T = isl_map_from_domain_and_range(
824 isl_set_universe(
825 isl_space_domain(isl_map_get_space(must_rel[k]))),
826 ran);
827 must_rel[k] = isl_map_subtract(must_rel[k], T);
831 return map;
834 /* Compute dependences for the case where all accesses are "may"
835 * accesses, which boils down to computing memory based dependences.
836 * The generic algorithm would also work in this case, but it would
837 * be overkill to use it.
839 static __isl_give isl_flow *compute_mem_based_dependences(
840 __isl_keep isl_access_info *acc)
842 int i;
843 isl_set *mustdo;
844 isl_set *maydo;
845 isl_flow *res;
847 res = isl_flow_alloc(acc);
848 if (!res)
849 return NULL;
851 mustdo = isl_map_domain(isl_map_copy(acc->sink.map));
852 maydo = isl_set_copy(mustdo);
854 for (i = 0; i < acc->n_may; ++i) {
855 int plevel;
856 int is_before;
857 isl_space *dim;
858 isl_map *before;
859 isl_map *dep;
861 plevel = acc->level_before(acc->source[i].data, acc->sink.data);
862 is_before = plevel & 1;
863 plevel >>= 1;
865 dim = isl_map_get_space(res->dep[i].map);
866 if (is_before)
867 before = isl_map_lex_le_first(dim, plevel);
868 else
869 before = isl_map_lex_lt_first(dim, plevel);
870 dep = isl_map_apply_range(isl_map_copy(acc->source[i].map),
871 isl_map_reverse(isl_map_copy(acc->sink.map)));
872 dep = isl_map_intersect(dep, before);
873 mustdo = isl_set_subtract(mustdo,
874 isl_map_range(isl_map_copy(dep)));
875 res->dep[i].map = isl_map_union(res->dep[i].map, dep);
878 res->may_no_source = isl_set_subtract(maydo, isl_set_copy(mustdo));
879 res->must_no_source = mustdo;
881 return res;
884 /* Compute dependences for the case where there is at least one
885 * "must" access.
887 * The core algorithm considers all levels in which a source may precede
888 * the sink, where a level may either be a statement level or a loop level.
889 * The outermost statement level is 1, the first loop level is 2, etc...
890 * The algorithm basically does the following:
891 * for all levels l of the read access from innermost to outermost
892 * for all sources w that may precede the sink access at that level
893 * compute the last iteration of the source that precedes the sink access
894 * at that level
895 * add result to possible last accesses at level l of source w
896 * for all sources w2 that we haven't considered yet at this level that may
897 * also precede the sink access
898 * for all levels l2 of w from l to innermost
899 * for all possible last accesses dep of w at l
900 * compute last iteration of w2 between the source and sink
901 * of dep
902 * add result to possible last accesses at level l of write w2
903 * and replace possible last accesses dep by the remainder
906 * The above algorithm is applied to the must access. During the course
907 * of the algorithm, we keep track of sink iterations that still
908 * need to be considered. These iterations are split into those that
909 * haven't been matched to any source access (mustdo) and those that have only
910 * been matched to may accesses (maydo).
911 * At the end of each level, we also consider the may accesses.
912 * In particular, we consider may accesses that precede the remaining
913 * sink iterations, moving elements from mustdo to maydo when appropriate,
914 * and may accesses that occur between a must source and a sink of any
915 * dependences found at the current level, turning must dependences into
916 * may dependences when appropriate.
919 static __isl_give isl_flow *compute_val_based_dependences(
920 __isl_keep isl_access_info *acc)
922 isl_ctx *ctx;
923 isl_flow *res;
924 isl_set *mustdo = NULL;
925 isl_set *maydo = NULL;
926 int level, j;
927 int depth;
928 isl_map **must_rel = NULL;
929 isl_map **may_rel = NULL;
931 if (!acc)
932 return NULL;
934 res = isl_flow_alloc(acc);
935 if (!res)
936 goto error;
937 ctx = isl_map_get_ctx(acc->sink.map);
939 depth = 2 * isl_map_dim(acc->sink.map, isl_dim_in) + 1;
940 mustdo = isl_map_domain(isl_map_copy(acc->sink.map));
941 maydo = isl_set_empty(isl_set_get_space(mustdo));
942 if (!mustdo || !maydo)
943 goto error;
944 if (isl_set_plain_is_empty(mustdo))
945 goto done;
947 must_rel = isl_alloc_array(ctx, struct isl_map *, acc->n_must);
948 may_rel = isl_alloc_array(ctx, struct isl_map *, acc->n_must);
949 if (!must_rel || !may_rel)
950 goto error;
952 for (level = depth; level >= 1; --level) {
953 for (j = acc->n_must-1; j >=0; --j) {
954 isl_space *space;
955 space = isl_map_get_space(res->dep[2 * j].map);
956 must_rel[j] = isl_map_empty(space);
957 may_rel[j] = isl_map_copy(must_rel[j]);
960 for (j = acc->n_must - 1; j >= 0; --j) {
961 struct isl_map *T;
962 struct isl_set *rest;
963 int plevel;
965 plevel = acc->level_before(acc->source[j].data,
966 acc->sink.data);
967 if (!can_precede_at_level(plevel, level))
968 continue;
970 T = last_source(acc, mustdo, j, level, &rest);
971 must_rel[j] = isl_map_union_disjoint(must_rel[j], T);
972 mustdo = rest;
974 intermediate_sources(acc, must_rel, j, level);
976 T = last_source(acc, maydo, j, level, &rest);
977 may_rel[j] = isl_map_union_disjoint(may_rel[j], T);
978 maydo = rest;
980 intermediate_sources(acc, may_rel, j, level);
982 if (isl_set_plain_is_empty(mustdo) &&
983 isl_set_plain_is_empty(maydo))
984 break;
986 for (j = j - 1; j >= 0; --j) {
987 int plevel;
989 plevel = acc->level_before(acc->source[j].data,
990 acc->sink.data);
991 if (!can_precede_at_level(plevel, level))
992 continue;
994 intermediate_sources(acc, must_rel, j, level);
995 intermediate_sources(acc, may_rel, j, level);
998 for (j = 0; j < acc->n_may; ++j) {
999 int plevel;
1000 isl_map *T;
1001 isl_set *ran;
1003 plevel = acc->level_before(acc->source[acc->n_must + j].data,
1004 acc->sink.data);
1005 if (!can_precede_at_level(plevel, level))
1006 continue;
1008 T = all_sources(acc, isl_set_copy(maydo), j, level);
1009 res->dep[2 * acc->n_must + j].map =
1010 isl_map_union(res->dep[2 * acc->n_must + j].map, T);
1011 T = all_sources(acc, isl_set_copy(mustdo), j, level);
1012 ran = isl_map_range(isl_map_copy(T));
1013 res->dep[2 * acc->n_must + j].map =
1014 isl_map_union(res->dep[2 * acc->n_must + j].map, T);
1015 mustdo = isl_set_subtract(mustdo, isl_set_copy(ran));
1016 maydo = isl_set_union_disjoint(maydo, ran);
1018 T = res->dep[2 * acc->n_must + j].map;
1019 T = all_intermediate_sources(acc, T, must_rel, may_rel,
1020 j, level);
1021 res->dep[2 * acc->n_must + j].map = T;
1024 for (j = acc->n_must - 1; j >= 0; --j) {
1025 res->dep[2 * j].map =
1026 isl_map_union_disjoint(res->dep[2 * j].map,
1027 must_rel[j]);
1028 res->dep[2 * j + 1].map =
1029 isl_map_union_disjoint(res->dep[2 * j + 1].map,
1030 may_rel[j]);
1033 if (isl_set_plain_is_empty(mustdo) &&
1034 isl_set_plain_is_empty(maydo))
1035 break;
1038 free(must_rel);
1039 free(may_rel);
1040 done:
1041 res->must_no_source = mustdo;
1042 res->may_no_source = maydo;
1043 return res;
1044 error:
1045 isl_flow_free(res);
1046 isl_set_free(mustdo);
1047 isl_set_free(maydo);
1048 free(must_rel);
1049 free(may_rel);
1050 return NULL;
1053 /* Given a "sink" access, a list of n "source" accesses,
1054 * compute for each iteration of the sink access
1055 * and for each element accessed by that iteration,
1056 * the source access in the list that last accessed the
1057 * element accessed by the sink access before this sink access.
1058 * Each access is given as a map from the loop iterators
1059 * to the array indices.
1060 * The result is a list of n relations between source and sink
1061 * iterations and a subset of the domain of the sink access,
1062 * corresponding to those iterations that access an element
1063 * not previously accessed.
1065 * To deal with multi-valued sink access relations, the sink iteration
1066 * domain is first extended with dimensions that correspond to the data
1067 * space. However, these extra dimensions are not projected out again.
1068 * It is up to the caller to decide whether these dimensions should be kept.
1070 static __isl_give isl_flow *access_info_compute_flow_core(
1071 __isl_take isl_access_info *acc)
1073 struct isl_flow *res = NULL;
1075 if (!acc)
1076 return NULL;
1078 acc->sink.map = isl_map_range_map(acc->sink.map);
1079 if (!acc->sink.map)
1080 goto error;
1082 if (acc->n_must == 0)
1083 res = compute_mem_based_dependences(acc);
1084 else {
1085 acc = isl_access_info_sort_sources(acc);
1086 res = compute_val_based_dependences(acc);
1088 isl_access_info_free(acc);
1089 if (!res)
1090 return NULL;
1091 if (!res->must_no_source || !res->may_no_source)
1092 goto error;
1093 return res;
1094 error:
1095 isl_flow_free(res);
1096 return NULL;
1099 /* Given a "sink" access, a list of n "source" accesses,
1100 * compute for each iteration of the sink access
1101 * and for each element accessed by that iteration,
1102 * the source access in the list that last accessed the
1103 * element accessed by the sink access before this sink access.
1104 * Each access is given as a map from the loop iterators
1105 * to the array indices.
1106 * The result is a list of n relations between source and sink
1107 * iterations and a subset of the domain of the sink access,
1108 * corresponding to those iterations that access an element
1109 * not previously accessed.
1111 * To deal with multi-valued sink access relations,
1112 * access_info_compute_flow_core extends the sink iteration domain
1113 * with dimensions that correspond to the data space. These extra dimensions
1114 * are projected out from the result of access_info_compute_flow_core.
1116 __isl_give isl_flow *isl_access_info_compute_flow(__isl_take isl_access_info *acc)
1118 int j;
1119 struct isl_flow *res;
1121 if (!acc)
1122 return NULL;
1124 acc->domain_map = isl_map_domain_map(isl_map_copy(acc->sink.map));
1125 res = access_info_compute_flow_core(acc);
1127 for (j = 0; j < res->n_source; ++j) {
1128 res->dep[j].map = isl_map_range_factor_domain(res->dep[j].map);
1129 if (!res->dep[j].map)
1130 goto error;
1133 return res;
1134 error:
1135 isl_flow_free(res);
1136 return NULL;
1140 /* Keep track of some information about a schedule for a given
1141 * access. In particular, keep track of which dimensions
1142 * have a constant value and of the actual constant values.
1144 struct isl_sched_info {
1145 int *is_cst;
1146 isl_vec *cst;
1149 static void sched_info_free(__isl_take struct isl_sched_info *info)
1151 if (!info)
1152 return;
1153 isl_vec_free(info->cst);
1154 free(info->is_cst);
1155 free(info);
1158 /* Extract information on the constant dimensions of the schedule
1159 * for a given access. The "map" is of the form
1161 * [S -> D] -> A
1163 * with S the schedule domain, D the iteration domain and A the data domain.
1165 static __isl_give struct isl_sched_info *sched_info_alloc(
1166 __isl_keep isl_map *map)
1168 isl_ctx *ctx;
1169 isl_space *dim;
1170 struct isl_sched_info *info;
1171 int i, n;
1173 if (!map)
1174 return NULL;
1176 dim = isl_space_unwrap(isl_space_domain(isl_map_get_space(map)));
1177 if (!dim)
1178 return NULL;
1179 n = isl_space_dim(dim, isl_dim_in);
1180 isl_space_free(dim);
1182 ctx = isl_map_get_ctx(map);
1183 info = isl_alloc_type(ctx, struct isl_sched_info);
1184 if (!info)
1185 return NULL;
1186 info->is_cst = isl_alloc_array(ctx, int, n);
1187 info->cst = isl_vec_alloc(ctx, n);
1188 if (n && (!info->is_cst || !info->cst))
1189 goto error;
1191 for (i = 0; i < n; ++i) {
1192 isl_val *v;
1194 v = isl_map_plain_get_val_if_fixed(map, isl_dim_in, i);
1195 if (!v)
1196 goto error;
1197 info->is_cst[i] = !isl_val_is_nan(v);
1198 if (info->is_cst[i])
1199 info->cst = isl_vec_set_element_val(info->cst, i, v);
1200 else
1201 isl_val_free(v);
1204 return info;
1205 error:
1206 sched_info_free(info);
1207 return NULL;
1210 /* This structure represents the input for a dependence analysis computation.
1212 * "sink" represents the sink accesses.
1213 * "must_source" represents the definite source accesses.
1214 * "may_source" represents the possible source accesses.
1216 * "schedule" or "schedule_map" represents the execution order.
1217 * Exactly one of these fields should be NULL. The other field
1218 * determines the execution order.
1220 * The domains of these four maps refer to the same iteration spaces(s).
1221 * The ranges of the first three maps also refer to the same data space(s).
1223 * After a call to isl_union_access_info_introduce_schedule,
1224 * the "schedule_map" field no longer contains useful information.
1226 struct isl_union_access_info {
1227 isl_union_map *sink;
1228 isl_union_map *must_source;
1229 isl_union_map *may_source;
1231 isl_schedule *schedule;
1232 isl_union_map *schedule_map;
1235 /* Free "access" and return NULL.
1237 __isl_null isl_union_access_info *isl_union_access_info_free(
1238 __isl_take isl_union_access_info *access)
1240 if (!access)
1241 return NULL;
1243 isl_union_map_free(access->sink);
1244 isl_union_map_free(access->must_source);
1245 isl_union_map_free(access->may_source);
1246 isl_schedule_free(access->schedule);
1247 isl_union_map_free(access->schedule_map);
1248 free(access);
1250 return NULL;
1253 /* Return the isl_ctx to which "access" belongs.
1255 isl_ctx *isl_union_access_info_get_ctx(__isl_keep isl_union_access_info *access)
1257 return access ? isl_union_map_get_ctx(access->sink) : NULL;
1260 /* Create a new isl_union_access_info with the given sink accesses and
1261 * and no source accesses or schedule information.
1263 * By default, we use the schedule field of the isl_union_access_info,
1264 * but this may be overridden by a call
1265 * to isl_union_access_info_set_schedule_map.
1267 __isl_give isl_union_access_info *isl_union_access_info_from_sink(
1268 __isl_take isl_union_map *sink)
1270 isl_ctx *ctx;
1271 isl_space *space;
1272 isl_union_map *empty;
1273 isl_union_access_info *access;
1275 if (!sink)
1276 return NULL;
1277 ctx = isl_union_map_get_ctx(sink);
1278 access = isl_alloc_type(ctx, isl_union_access_info);
1279 if (!access)
1280 goto error;
1282 space = isl_union_map_get_space(sink);
1283 empty = isl_union_map_empty(isl_space_copy(space));
1284 access->sink = sink;
1285 access->must_source = isl_union_map_copy(empty);
1286 access->may_source = empty;
1287 access->schedule = isl_schedule_empty(space);
1288 access->schedule_map = NULL;
1290 if (!access->sink || !access->must_source ||
1291 !access->may_source || !access->schedule)
1292 return isl_union_access_info_free(access);
1294 return access;
1295 error:
1296 isl_union_map_free(sink);
1297 return NULL;
1300 /* Replace the definite source accesses of "access" by "must_source".
1302 __isl_give isl_union_access_info *isl_union_access_info_set_must_source(
1303 __isl_take isl_union_access_info *access,
1304 __isl_take isl_union_map *must_source)
1306 if (!access || !must_source)
1307 goto error;
1309 isl_union_map_free(access->must_source);
1310 access->must_source = must_source;
1312 return access;
1313 error:
1314 isl_union_access_info_free(access);
1315 isl_union_map_free(must_source);
1316 return NULL;
1319 /* Replace the possible source accesses of "access" by "may_source".
1321 __isl_give isl_union_access_info *isl_union_access_info_set_may_source(
1322 __isl_take isl_union_access_info *access,
1323 __isl_take isl_union_map *may_source)
1325 if (!access || !may_source)
1326 goto error;
1328 isl_union_map_free(access->may_source);
1329 access->may_source = may_source;
1331 return access;
1332 error:
1333 isl_union_access_info_free(access);
1334 isl_union_map_free(may_source);
1335 return NULL;
1338 /* Replace the schedule of "access" by "schedule".
1339 * Also free the schedule_map in case it was set last.
1341 __isl_give isl_union_access_info *isl_union_access_info_set_schedule(
1342 __isl_take isl_union_access_info *access,
1343 __isl_take isl_schedule *schedule)
1345 if (!access || !schedule)
1346 goto error;
1348 access->schedule_map = isl_union_map_free(access->schedule_map);
1349 isl_schedule_free(access->schedule);
1350 access->schedule = schedule;
1352 return access;
1353 error:
1354 isl_union_access_info_free(access);
1355 isl_schedule_free(schedule);
1356 return NULL;
1359 /* Replace the schedule map of "access" by "schedule_map".
1360 * Also free the schedule in case it was set last.
1362 __isl_give isl_union_access_info *isl_union_access_info_set_schedule_map(
1363 __isl_take isl_union_access_info *access,
1364 __isl_take isl_union_map *schedule_map)
1366 if (!access || !schedule_map)
1367 goto error;
1369 isl_union_map_free(access->schedule_map);
1370 access->schedule = isl_schedule_free(access->schedule);
1371 access->schedule_map = schedule_map;
1373 return access;
1374 error:
1375 isl_union_access_info_free(access);
1376 isl_union_map_free(schedule_map);
1377 return NULL;
1380 __isl_give isl_union_access_info *isl_union_access_info_copy(
1381 __isl_keep isl_union_access_info *access)
1383 isl_union_access_info *copy;
1385 if (!access)
1386 return NULL;
1387 copy = isl_union_access_info_from_sink(
1388 isl_union_map_copy(access->sink));
1389 copy = isl_union_access_info_set_must_source(copy,
1390 isl_union_map_copy(access->must_source));
1391 copy = isl_union_access_info_set_may_source(copy,
1392 isl_union_map_copy(access->may_source));
1393 if (access->schedule)
1394 copy = isl_union_access_info_set_schedule(copy,
1395 isl_schedule_copy(access->schedule));
1396 else
1397 copy = isl_union_access_info_set_schedule_map(copy,
1398 isl_union_map_copy(access->schedule_map));
1400 return copy;
1403 /* Update the fields of "access" such that they all have the same parameters,
1404 * keeping in mind that the schedule_map field may be NULL and ignoring
1405 * the schedule field.
1407 static __isl_give isl_union_access_info *isl_union_access_info_align_params(
1408 __isl_take isl_union_access_info *access)
1410 isl_space *space;
1412 if (!access)
1413 return NULL;
1415 space = isl_union_map_get_space(access->sink);
1416 space = isl_space_align_params(space,
1417 isl_union_map_get_space(access->must_source));
1418 space = isl_space_align_params(space,
1419 isl_union_map_get_space(access->may_source));
1420 if (access->schedule_map)
1421 space = isl_space_align_params(space,
1422 isl_union_map_get_space(access->schedule_map));
1423 access->sink = isl_union_map_align_params(access->sink,
1424 isl_space_copy(space));
1425 access->must_source = isl_union_map_align_params(access->must_source,
1426 isl_space_copy(space));
1427 access->may_source = isl_union_map_align_params(access->may_source,
1428 isl_space_copy(space));
1429 if (!access->schedule_map) {
1430 isl_space_free(space);
1431 } else {
1432 access->schedule_map =
1433 isl_union_map_align_params(access->schedule_map, space);
1434 if (!access->schedule_map)
1435 return isl_union_access_info_free(access);
1438 if (!access->sink || !access->must_source || !access->may_source)
1439 return isl_union_access_info_free(access);
1441 return access;
1444 /* Prepend the schedule dimensions to the iteration domains.
1446 * That is, if the schedule is of the form
1448 * D -> S
1450 * while the access relations are of the form
1452 * D -> A
1454 * then the updated access relations are of the form
1456 * [S -> D] -> A
1458 * The schedule map is also replaced by the map
1460 * [S -> D] -> D
1462 * that is used during the internal computation.
1463 * Neither the original schedule map nor this updated schedule map
1464 * are used after the call to this function.
1466 static __isl_give isl_union_access_info *
1467 isl_union_access_info_introduce_schedule(
1468 __isl_take isl_union_access_info *access)
1470 isl_union_map *sm;
1472 if (!access)
1473 return NULL;
1475 sm = isl_union_map_reverse(access->schedule_map);
1476 sm = isl_union_map_range_map(sm);
1477 access->sink = isl_union_map_apply_range(isl_union_map_copy(sm),
1478 access->sink);
1479 access->may_source = isl_union_map_apply_range(isl_union_map_copy(sm),
1480 access->may_source);
1481 access->must_source = isl_union_map_apply_range(isl_union_map_copy(sm),
1482 access->must_source);
1483 access->schedule_map = sm;
1485 if (!access->sink || !access->must_source ||
1486 !access->may_source || !access->schedule_map)
1487 return isl_union_access_info_free(access);
1489 return access;
1492 /* This structure represents the result of a dependence analysis computation.
1494 * "must_dep" represents the full definite dependences
1495 * "may_dep" represents the full non-definite dependences.
1496 * Both are of the form
1498 * [Source] -> [[Sink -> Data]]
1500 * (after the schedule dimensions have been projected out).
1501 * "must_no_source" represents the subset of the sink accesses for which
1502 * definitely no source was found.
1503 * "may_no_source" represents the subset of the sink accesses for which
1504 * possibly, but not definitely, no source was found.
1506 struct isl_union_flow {
1507 isl_union_map *must_dep;
1508 isl_union_map *may_dep;
1509 isl_union_map *must_no_source;
1510 isl_union_map *may_no_source;
1513 /* Return the isl_ctx to which "flow" belongs.
1515 isl_ctx *isl_union_flow_get_ctx(__isl_keep isl_union_flow *flow)
1517 return flow ? isl_union_map_get_ctx(flow->must_dep) : NULL;
1520 /* Free "flow" and return NULL.
1522 __isl_null isl_union_flow *isl_union_flow_free(__isl_take isl_union_flow *flow)
1524 if (!flow)
1525 return NULL;
1526 isl_union_map_free(flow->must_dep);
1527 isl_union_map_free(flow->may_dep);
1528 isl_union_map_free(flow->must_no_source);
1529 isl_union_map_free(flow->may_no_source);
1530 free(flow);
1531 return NULL;
1534 void isl_union_flow_dump(__isl_keep isl_union_flow *flow)
1536 if (!flow)
1537 return;
1539 fprintf(stderr, "must dependences: ");
1540 isl_union_map_dump(flow->must_dep);
1541 fprintf(stderr, "may dependences: ");
1542 isl_union_map_dump(flow->may_dep);
1543 fprintf(stderr, "must no source: ");
1544 isl_union_map_dump(flow->must_no_source);
1545 fprintf(stderr, "may no source: ");
1546 isl_union_map_dump(flow->may_no_source);
1549 /* Return the full definite dependences in "flow", with accessed elements.
1551 __isl_give isl_union_map *isl_union_flow_get_full_must_dependence(
1552 __isl_keep isl_union_flow *flow)
1554 if (!flow)
1555 return NULL;
1556 return isl_union_map_copy(flow->must_dep);
1559 /* Return the full possible dependences in "flow", including the definite
1560 * dependences, with accessed elements.
1562 __isl_give isl_union_map *isl_union_flow_get_full_may_dependence(
1563 __isl_keep isl_union_flow *flow)
1565 if (!flow)
1566 return NULL;
1567 return isl_union_map_union(isl_union_map_copy(flow->must_dep),
1568 isl_union_map_copy(flow->may_dep));
1571 /* Return the definite dependences in "flow", without the accessed elements.
1573 __isl_give isl_union_map *isl_union_flow_get_must_dependence(
1574 __isl_keep isl_union_flow *flow)
1576 isl_union_map *dep;
1578 if (!flow)
1579 return NULL;
1580 dep = isl_union_map_copy(flow->must_dep);
1581 return isl_union_map_range_factor_domain(dep);
1584 /* Return the possible dependences in "flow", including the definite
1585 * dependences, without the accessed elements.
1587 __isl_give isl_union_map *isl_union_flow_get_may_dependence(
1588 __isl_keep isl_union_flow *flow)
1590 isl_union_map *dep;
1592 if (!flow)
1593 return NULL;
1594 dep = isl_union_map_union(isl_union_map_copy(flow->must_dep),
1595 isl_union_map_copy(flow->may_dep));
1596 return isl_union_map_range_factor_domain(dep);
1599 /* Return the non-definite dependences in "flow".
1601 static __isl_give isl_union_map *isl_union_flow_get_non_must_dependence(
1602 __isl_keep isl_union_flow *flow)
1604 if (!flow)
1605 return NULL;
1606 return isl_union_map_copy(flow->may_dep);
1609 /* Return the subset of the sink accesses for which definitely
1610 * no source was found.
1612 __isl_give isl_union_map *isl_union_flow_get_must_no_source(
1613 __isl_keep isl_union_flow *flow)
1615 if (!flow)
1616 return NULL;
1617 return isl_union_map_copy(flow->must_no_source);
1620 /* Return the subset of the sink accesses for which possibly
1621 * no source was found, including those for which definitely
1622 * no source was found.
1624 __isl_give isl_union_map *isl_union_flow_get_may_no_source(
1625 __isl_keep isl_union_flow *flow)
1627 if (!flow)
1628 return NULL;
1629 return isl_union_map_union(isl_union_map_copy(flow->must_no_source),
1630 isl_union_map_copy(flow->may_no_source));
1633 /* Return the subset of the sink accesses for which possibly, but not
1634 * definitely, no source was found.
1636 static __isl_give isl_union_map *isl_union_flow_get_non_must_no_source(
1637 __isl_keep isl_union_flow *flow)
1639 if (!flow)
1640 return NULL;
1641 return isl_union_map_copy(flow->may_no_source);
1644 /* Create a new isl_union_flow object, initialized with empty
1645 * dependence relations and sink subsets.
1647 static __isl_give isl_union_flow *isl_union_flow_alloc(
1648 __isl_take isl_space *space)
1650 isl_ctx *ctx;
1651 isl_union_map *empty;
1652 isl_union_flow *flow;
1654 if (!space)
1655 return NULL;
1656 ctx = isl_space_get_ctx(space);
1657 flow = isl_alloc_type(ctx, isl_union_flow);
1658 if (!flow)
1659 goto error;
1661 empty = isl_union_map_empty(space);
1662 flow->must_dep = isl_union_map_copy(empty);
1663 flow->may_dep = isl_union_map_copy(empty);
1664 flow->must_no_source = isl_union_map_copy(empty);
1665 flow->may_no_source = empty;
1667 if (!flow->must_dep || !flow->may_dep ||
1668 !flow->must_no_source || !flow->may_no_source)
1669 return isl_union_flow_free(flow);
1671 return flow;
1672 error:
1673 isl_space_free(space);
1674 return NULL;
1677 /* Drop the schedule dimensions from the iteration domains in "flow".
1678 * In particular, the schedule dimensions have been prepended
1679 * to the iteration domains prior to the dependence analysis by
1680 * replacing the iteration domain D, by the wrapped map [S -> D].
1681 * Replace these wrapped maps by the original D.
1683 * In particular, the dependences computed by access_info_compute_flow_core
1684 * are of the form
1686 * [S -> D] -> [[S' -> D'] -> A]
1688 * The schedule dimensions are projected out by first currying the range,
1689 * resulting in
1691 * [S -> D] -> [S' -> [D' -> A]]
1693 * and then computing the factor range
1695 * D -> [D' -> A]
1697 static __isl_give isl_union_flow *isl_union_flow_drop_schedule(
1698 __isl_take isl_union_flow *flow)
1700 if (!flow)
1701 return NULL;
1703 flow->must_dep = isl_union_map_range_curry(flow->must_dep);
1704 flow->must_dep = isl_union_map_factor_range(flow->must_dep);
1705 flow->may_dep = isl_union_map_range_curry(flow->may_dep);
1706 flow->may_dep = isl_union_map_factor_range(flow->may_dep);
1707 flow->must_no_source =
1708 isl_union_map_domain_factor_range(flow->must_no_source);
1709 flow->may_no_source =
1710 isl_union_map_domain_factor_range(flow->may_no_source);
1712 if (!flow->must_dep || !flow->may_dep ||
1713 !flow->must_no_source || !flow->may_no_source)
1714 return isl_union_flow_free(flow);
1716 return flow;
1719 struct isl_compute_flow_data {
1720 isl_union_map *must_source;
1721 isl_union_map *may_source;
1722 isl_union_flow *flow;
1724 int count;
1725 int must;
1726 isl_space *dim;
1727 struct isl_sched_info *sink_info;
1728 struct isl_sched_info **source_info;
1729 isl_access_info *accesses;
1732 static isl_stat count_matching_array(__isl_take isl_map *map, void *user)
1734 int eq;
1735 isl_space *dim;
1736 struct isl_compute_flow_data *data;
1738 data = (struct isl_compute_flow_data *)user;
1740 dim = isl_space_range(isl_map_get_space(map));
1742 eq = isl_space_is_equal(dim, data->dim);
1744 isl_space_free(dim);
1745 isl_map_free(map);
1747 if (eq < 0)
1748 return isl_stat_error;
1749 if (eq)
1750 data->count++;
1752 return isl_stat_ok;
1755 static isl_stat collect_matching_array(__isl_take isl_map *map, void *user)
1757 int eq;
1758 isl_space *dim;
1759 struct isl_sched_info *info;
1760 struct isl_compute_flow_data *data;
1762 data = (struct isl_compute_flow_data *)user;
1764 dim = isl_space_range(isl_map_get_space(map));
1766 eq = isl_space_is_equal(dim, data->dim);
1768 isl_space_free(dim);
1770 if (eq < 0)
1771 goto error;
1772 if (!eq) {
1773 isl_map_free(map);
1774 return isl_stat_ok;
1777 info = sched_info_alloc(map);
1778 data->source_info[data->count] = info;
1780 data->accesses = isl_access_info_add_source(data->accesses,
1781 map, data->must, info);
1783 data->count++;
1785 return isl_stat_ok;
1786 error:
1787 isl_map_free(map);
1788 return isl_stat_error;
1791 /* Determine the shared nesting level and the "textual order" of
1792 * the given accesses.
1794 * We first determine the minimal schedule dimension for both accesses.
1796 * If among those dimensions, we can find one where both have a fixed
1797 * value and if moreover those values are different, then the previous
1798 * dimension is the last shared nesting level and the textual order
1799 * is determined based on the order of the fixed values.
1800 * If no such fixed values can be found, then we set the shared
1801 * nesting level to the minimal schedule dimension, with no textual ordering.
1803 static int before(void *first, void *second)
1805 struct isl_sched_info *info1 = first;
1806 struct isl_sched_info *info2 = second;
1807 int n1, n2;
1808 int i;
1810 n1 = isl_vec_size(info1->cst);
1811 n2 = isl_vec_size(info2->cst);
1813 if (n2 < n1)
1814 n1 = n2;
1816 for (i = 0; i < n1; ++i) {
1817 int r;
1818 int cmp;
1820 if (!info1->is_cst[i])
1821 continue;
1822 if (!info2->is_cst[i])
1823 continue;
1824 cmp = isl_vec_cmp_element(info1->cst, info2->cst, i);
1825 if (cmp == 0)
1826 continue;
1828 r = 2 * i + (cmp < 0);
1830 return r;
1833 return 2 * n1;
1836 /* Given a sink access, look for all the source accesses that access
1837 * the same array and perform dataflow analysis on them using
1838 * isl_access_info_compute_flow_core.
1840 static isl_stat compute_flow(__isl_take isl_map *map, void *user)
1842 int i;
1843 isl_ctx *ctx;
1844 struct isl_compute_flow_data *data;
1845 isl_flow *flow;
1846 isl_union_flow *df;
1848 data = (struct isl_compute_flow_data *)user;
1849 df = data->flow;
1851 ctx = isl_map_get_ctx(map);
1853 data->accesses = NULL;
1854 data->sink_info = NULL;
1855 data->source_info = NULL;
1856 data->count = 0;
1857 data->dim = isl_space_range(isl_map_get_space(map));
1859 if (isl_union_map_foreach_map(data->must_source,
1860 &count_matching_array, data) < 0)
1861 goto error;
1862 if (isl_union_map_foreach_map(data->may_source,
1863 &count_matching_array, data) < 0)
1864 goto error;
1866 data->sink_info = sched_info_alloc(map);
1867 data->source_info = isl_calloc_array(ctx, struct isl_sched_info *,
1868 data->count);
1870 data->accesses = isl_access_info_alloc(isl_map_copy(map),
1871 data->sink_info, &before, data->count);
1872 if (!data->sink_info || (data->count && !data->source_info) ||
1873 !data->accesses)
1874 goto error;
1875 data->count = 0;
1876 data->must = 1;
1877 if (isl_union_map_foreach_map(data->must_source,
1878 &collect_matching_array, data) < 0)
1879 goto error;
1880 data->must = 0;
1881 if (isl_union_map_foreach_map(data->may_source,
1882 &collect_matching_array, data) < 0)
1883 goto error;
1885 flow = access_info_compute_flow_core(data->accesses);
1886 data->accesses = NULL;
1888 if (!flow)
1889 goto error;
1891 df->must_no_source = isl_union_map_union(df->must_no_source,
1892 isl_union_map_from_map(isl_flow_get_no_source(flow, 1)));
1893 df->may_no_source = isl_union_map_union(df->may_no_source,
1894 isl_union_map_from_map(isl_flow_get_no_source(flow, 0)));
1896 for (i = 0; i < flow->n_source; ++i) {
1897 isl_union_map *dep;
1898 dep = isl_union_map_from_map(isl_map_copy(flow->dep[i].map));
1899 if (flow->dep[i].must)
1900 df->must_dep = isl_union_map_union(df->must_dep, dep);
1901 else
1902 df->may_dep = isl_union_map_union(df->may_dep, dep);
1905 isl_flow_free(flow);
1907 sched_info_free(data->sink_info);
1908 if (data->source_info) {
1909 for (i = 0; i < data->count; ++i)
1910 sched_info_free(data->source_info[i]);
1911 free(data->source_info);
1913 isl_space_free(data->dim);
1914 isl_map_free(map);
1916 return isl_stat_ok;
1917 error:
1918 isl_access_info_free(data->accesses);
1919 sched_info_free(data->sink_info);
1920 if (data->source_info) {
1921 for (i = 0; i < data->count; ++i)
1922 sched_info_free(data->source_info[i]);
1923 free(data->source_info);
1925 isl_space_free(data->dim);
1926 isl_map_free(map);
1928 return isl_stat_error;
1931 /* Remove the must accesses from the may accesses.
1933 * A must access always trumps a may access, so there is no need
1934 * for a must access to also be considered as a may access. Doing so
1935 * would only cost extra computations only to find out that
1936 * the duplicated may access does not make any difference.
1938 static __isl_give isl_union_access_info *isl_union_access_info_normalize(
1939 __isl_take isl_union_access_info *access)
1941 if (!access)
1942 return NULL;
1943 access->may_source = isl_union_map_subtract(access->may_source,
1944 isl_union_map_copy(access->must_source));
1945 if (!access->may_source)
1946 return isl_union_access_info_free(access);
1948 return access;
1951 /* Given a description of the "sink" accesses, the "source" accesses and
1952 * a schedule, compute for each instance of a sink access
1953 * and for each element accessed by that instance,
1954 * the possible or definite source accesses that last accessed the
1955 * element accessed by the sink access before this sink access
1956 * in the sense that there is no intermediate definite source access.
1958 * The must_no_source and may_no_source elements of the result
1959 * are subsets of access->sink. The elements must_dep and may_dep
1960 * map domain elements of access->{may,must)_source to
1961 * domain elements of access->sink.
1963 * This function is used when only the schedule map representation
1964 * is available.
1966 * We first prepend the schedule dimensions to the domain
1967 * of the accesses so that we can easily compare their relative order.
1968 * Then we consider each sink access individually in compute_flow.
1970 static __isl_give isl_union_flow *compute_flow_union_map(
1971 __isl_take isl_union_access_info *access)
1973 struct isl_compute_flow_data data;
1975 access = isl_union_access_info_align_params(access);
1976 access = isl_union_access_info_introduce_schedule(access);
1977 if (!access)
1978 return NULL;
1980 data.must_source = access->must_source;
1981 data.may_source = access->may_source;
1983 data.flow = isl_union_flow_alloc(isl_union_map_get_space(access->sink));
1985 if (isl_union_map_foreach_map(access->sink, &compute_flow, &data) < 0)
1986 goto error;
1988 data.flow = isl_union_flow_drop_schedule(data.flow);
1990 isl_union_access_info_free(access);
1991 return data.flow;
1992 error:
1993 isl_union_access_info_free(access);
1994 isl_union_flow_free(data.flow);
1995 return NULL;
1998 /* A schedule access relation.
2000 * The access relation "access" is of the form [S -> D] -> A,
2001 * where S corresponds to the prefix schedule at "node".
2002 * "must" is only relevant for source accesses and indicates
2003 * whether the access is a must source or a may source.
2005 struct isl_scheduled_access {
2006 isl_map *access;
2007 int must;
2008 isl_schedule_node *node;
2011 /* Data structure for keeping track of individual scheduled sink and source
2012 * accesses when computing dependence analysis based on a schedule tree.
2014 * "n_sink" is the number of used entries in "sink"
2015 * "n_source" is the number of used entries in "source"
2017 * "set_sink", "must" and "node" are only used inside collect_sink_source,
2018 * to keep track of the current node and
2019 * of what extract_sink_source needs to do.
2021 struct isl_compute_flow_schedule_data {
2022 isl_union_access_info *access;
2024 int n_sink;
2025 int n_source;
2027 struct isl_scheduled_access *sink;
2028 struct isl_scheduled_access *source;
2030 int set_sink;
2031 int must;
2032 isl_schedule_node *node;
2035 /* Align the parameters of all sinks with all sources.
2037 * If there are no sinks or no sources, then no alignment is needed.
2039 static void isl_compute_flow_schedule_data_align_params(
2040 struct isl_compute_flow_schedule_data *data)
2042 int i;
2043 isl_space *space;
2045 if (data->n_sink == 0 || data->n_source == 0)
2046 return;
2048 space = isl_map_get_space(data->sink[0].access);
2050 for (i = 1; i < data->n_sink; ++i)
2051 space = isl_space_align_params(space,
2052 isl_map_get_space(data->sink[i].access));
2053 for (i = 0; i < data->n_source; ++i)
2054 space = isl_space_align_params(space,
2055 isl_map_get_space(data->source[i].access));
2057 for (i = 0; i < data->n_sink; ++i)
2058 data->sink[i].access =
2059 isl_map_align_params(data->sink[i].access,
2060 isl_space_copy(space));
2061 for (i = 0; i < data->n_source; ++i)
2062 data->source[i].access =
2063 isl_map_align_params(data->source[i].access,
2064 isl_space_copy(space));
2066 isl_space_free(space);
2069 /* Free all the memory referenced from "data".
2070 * Do not free "data" itself as it may be allocated on the stack.
2072 static void isl_compute_flow_schedule_data_clear(
2073 struct isl_compute_flow_schedule_data *data)
2075 int i;
2077 if (!data->sink)
2078 return;
2080 for (i = 0; i < data->n_sink; ++i) {
2081 isl_map_free(data->sink[i].access);
2082 isl_schedule_node_free(data->sink[i].node);
2085 for (i = 0; i < data->n_source; ++i) {
2086 isl_map_free(data->source[i].access);
2087 isl_schedule_node_free(data->source[i].node);
2090 free(data->sink);
2093 /* isl_schedule_foreach_schedule_node_top_down callback for counting
2094 * (an upper bound on) the number of sinks and sources.
2096 * Sinks and sources are only extracted at leaves of the tree,
2097 * so we skip the node if it is not a leaf.
2098 * Otherwise we increment data->n_sink and data->n_source with
2099 * the number of spaces in the sink and source access domains
2100 * that reach this node.
2102 static isl_bool count_sink_source(__isl_keep isl_schedule_node *node,
2103 void *user)
2105 struct isl_compute_flow_schedule_data *data = user;
2106 isl_union_set *domain;
2107 isl_union_map *umap;
2108 isl_bool r = isl_bool_false;
2110 if (isl_schedule_node_get_type(node) != isl_schedule_node_leaf)
2111 return isl_bool_true;
2113 domain = isl_schedule_node_get_universe_domain(node);
2115 umap = isl_union_map_copy(data->access->sink);
2116 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
2117 data->n_sink += isl_union_map_n_map(umap);
2118 isl_union_map_free(umap);
2119 if (!umap)
2120 r = isl_bool_error;
2122 umap = isl_union_map_copy(data->access->must_source);
2123 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
2124 data->n_source += isl_union_map_n_map(umap);
2125 isl_union_map_free(umap);
2126 if (!umap)
2127 r = isl_bool_error;
2129 umap = isl_union_map_copy(data->access->may_source);
2130 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
2131 data->n_source += isl_union_map_n_map(umap);
2132 isl_union_map_free(umap);
2133 if (!umap)
2134 r = isl_bool_error;
2136 isl_union_set_free(domain);
2138 return r;
2141 /* Add a single scheduled sink or source (depending on data->set_sink)
2142 * with scheduled access relation "map", must property data->must and
2143 * schedule node data->node to the list of sinks or sources.
2145 static isl_stat extract_sink_source(__isl_take isl_map *map, void *user)
2147 struct isl_compute_flow_schedule_data *data = user;
2148 struct isl_scheduled_access *access;
2150 if (data->set_sink)
2151 access = data->sink + data->n_sink++;
2152 else
2153 access = data->source + data->n_source++;
2155 access->access = map;
2156 access->must = data->must;
2157 access->node = isl_schedule_node_copy(data->node);
2159 return isl_stat_ok;
2162 /* isl_schedule_foreach_schedule_node_top_down callback for collecting
2163 * individual scheduled source and sink accesses (taking into account
2164 * the domain of the schedule).
2166 * We only collect accesses at the leaves of the schedule tree.
2167 * We prepend the schedule dimensions at the leaf to the iteration
2168 * domains of the source and sink accesses and then extract
2169 * the individual accesses (per space).
2171 * In particular, if the prefix schedule at the node is of the form
2173 * D -> S
2175 * while the access relations are of the form
2177 * D -> A
2179 * then the updated access relations are of the form
2181 * [S -> D] -> A
2183 * Note that S consists of a single space such that introducing S
2184 * in the access relations does not increase the number of spaces.
2186 static isl_bool collect_sink_source(__isl_keep isl_schedule_node *node,
2187 void *user)
2189 struct isl_compute_flow_schedule_data *data = user;
2190 isl_union_map *prefix;
2191 isl_union_map *umap;
2192 isl_bool r = isl_bool_false;
2194 if (isl_schedule_node_get_type(node) != isl_schedule_node_leaf)
2195 return isl_bool_true;
2197 data->node = node;
2199 prefix = isl_schedule_node_get_prefix_schedule_relation(node);
2200 prefix = isl_union_map_reverse(prefix);
2201 prefix = isl_union_map_range_map(prefix);
2203 data->set_sink = 1;
2204 umap = isl_union_map_copy(data->access->sink);
2205 umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
2206 if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
2207 r = isl_bool_error;
2208 isl_union_map_free(umap);
2210 data->set_sink = 0;
2211 data->must = 1;
2212 umap = isl_union_map_copy(data->access->must_source);
2213 umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
2214 if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
2215 r = isl_bool_error;
2216 isl_union_map_free(umap);
2218 data->set_sink = 0;
2219 data->must = 0;
2220 umap = isl_union_map_copy(data->access->may_source);
2221 umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
2222 if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
2223 r = isl_bool_error;
2224 isl_union_map_free(umap);
2226 isl_union_map_free(prefix);
2228 return r;
2231 /* isl_access_info_compute_flow callback for determining whether
2232 * the shared nesting level and the ordering within that level
2233 * for two scheduled accesses for use in compute_single_flow.
2235 * The tokens passed to this function refer to the leaves
2236 * in the schedule tree where the accesses take place.
2238 * If n is the shared number of loops, then we need to return
2239 * "2 * n + 1" if "first" precedes "second" inside the innermost
2240 * shared loop and "2 * n" otherwise.
2242 * The innermost shared ancestor may be the leaves themselves
2243 * if the accesses take place in the same leaf. Otherwise,
2244 * it is either a set node or a sequence node. Only in the case
2245 * of a sequence node do we consider one access to precede the other.
2247 static int before_node(void *first, void *second)
2249 isl_schedule_node *node1 = first;
2250 isl_schedule_node *node2 = second;
2251 isl_schedule_node *shared;
2252 int depth;
2253 int before = 0;
2255 shared = isl_schedule_node_get_shared_ancestor(node1, node2);
2256 if (!shared)
2257 return -1;
2259 depth = isl_schedule_node_get_schedule_depth(shared);
2260 if (isl_schedule_node_get_type(shared) == isl_schedule_node_sequence) {
2261 int pos1, pos2;
2263 pos1 = isl_schedule_node_get_ancestor_child_position(node1,
2264 shared);
2265 pos2 = isl_schedule_node_get_ancestor_child_position(node2,
2266 shared);
2267 before = pos1 < pos2;
2270 isl_schedule_node_free(shared);
2272 return 2 * depth + before;
2275 /* Add the scheduled sources from "data" that access
2276 * the same data space as "sink" to "access".
2278 static __isl_give isl_access_info *add_matching_sources(
2279 __isl_take isl_access_info *access, struct isl_scheduled_access *sink,
2280 struct isl_compute_flow_schedule_data *data)
2282 int i;
2283 isl_space *space;
2285 space = isl_space_range(isl_map_get_space(sink->access));
2286 for (i = 0; i < data->n_source; ++i) {
2287 struct isl_scheduled_access *source;
2288 isl_space *source_space;
2289 int eq;
2291 source = &data->source[i];
2292 source_space = isl_map_get_space(source->access);
2293 source_space = isl_space_range(source_space);
2294 eq = isl_space_is_equal(space, source_space);
2295 isl_space_free(source_space);
2297 if (!eq)
2298 continue;
2299 if (eq < 0)
2300 goto error;
2302 access = isl_access_info_add_source(access,
2303 isl_map_copy(source->access), source->must, source->node);
2306 isl_space_free(space);
2307 return access;
2308 error:
2309 isl_space_free(space);
2310 isl_access_info_free(access);
2311 return NULL;
2314 /* Given a scheduled sink access relation "sink", compute the corresponding
2315 * dependences on the sources in "data" and add the computed dependences
2316 * to "uf".
2318 * The dependences computed by access_info_compute_flow_core are of the form
2320 * [S -> I] -> [[S' -> I'] -> A]
2322 * The schedule dimensions are projected out by first currying the range,
2323 * resulting in
2325 * [S -> I] -> [S' -> [I' -> A]]
2327 * and then computing the factor range
2329 * I -> [I' -> A]
2331 static __isl_give isl_union_flow *compute_single_flow(
2332 __isl_take isl_union_flow *uf, struct isl_scheduled_access *sink,
2333 struct isl_compute_flow_schedule_data *data)
2335 int i;
2336 isl_access_info *access;
2337 isl_flow *flow;
2338 isl_map *map;
2340 if (!uf)
2341 return NULL;
2343 access = isl_access_info_alloc(isl_map_copy(sink->access), sink->node,
2344 &before_node, data->n_source);
2345 access = add_matching_sources(access, sink, data);
2347 flow = access_info_compute_flow_core(access);
2348 if (!flow)
2349 return isl_union_flow_free(uf);
2351 map = isl_map_domain_factor_range(isl_flow_get_no_source(flow, 1));
2352 uf->must_no_source = isl_union_map_union(uf->must_no_source,
2353 isl_union_map_from_map(map));
2354 map = isl_map_domain_factor_range(isl_flow_get_no_source(flow, 0));
2355 uf->may_no_source = isl_union_map_union(uf->may_no_source,
2356 isl_union_map_from_map(map));
2358 for (i = 0; i < flow->n_source; ++i) {
2359 isl_union_map *dep;
2361 map = isl_map_range_curry(isl_map_copy(flow->dep[i].map));
2362 map = isl_map_factor_range(map);
2363 dep = isl_union_map_from_map(map);
2364 if (flow->dep[i].must)
2365 uf->must_dep = isl_union_map_union(uf->must_dep, dep);
2366 else
2367 uf->may_dep = isl_union_map_union(uf->may_dep, dep);
2370 isl_flow_free(flow);
2372 return uf;
2375 /* Given a description of the "sink" accesses, the "source" accesses and
2376 * a schedule, compute for each instance of a sink access
2377 * and for each element accessed by that instance,
2378 * the possible or definite source accesses that last accessed the
2379 * element accessed by the sink access before this sink access
2380 * in the sense that there is no intermediate definite source access.
2381 * Only consider dependences between statement instances that belong
2382 * to the domain of the schedule.
2384 * The must_no_source and may_no_source elements of the result
2385 * are subsets of access->sink. The elements must_dep and may_dep
2386 * map domain elements of access->{may,must)_source to
2387 * domain elements of access->sink.
2389 * This function is used when a schedule tree representation
2390 * is available.
2392 * We extract the individual scheduled source and sink access relations
2393 * (taking into account the domain of the schedule) and
2394 * then compute dependences for each scheduled sink individually.
2396 static __isl_give isl_union_flow *compute_flow_schedule(
2397 __isl_take isl_union_access_info *access)
2399 struct isl_compute_flow_schedule_data data = { access };
2400 int i, n;
2401 isl_ctx *ctx;
2402 isl_union_flow *flow;
2404 ctx = isl_union_access_info_get_ctx(access);
2406 data.n_sink = 0;
2407 data.n_source = 0;
2408 if (isl_schedule_foreach_schedule_node_top_down(access->schedule,
2409 &count_sink_source, &data) < 0)
2410 goto error;
2412 n = data.n_sink + data.n_source;
2413 data.sink = isl_calloc_array(ctx, struct isl_scheduled_access, n);
2414 if (n && !data.sink)
2415 goto error;
2416 data.source = data.sink + data.n_sink;
2418 data.n_sink = 0;
2419 data.n_source = 0;
2420 if (isl_schedule_foreach_schedule_node_top_down(access->schedule,
2421 &collect_sink_source, &data) < 0)
2422 goto error;
2424 flow = isl_union_flow_alloc(isl_union_map_get_space(access->sink));
2426 isl_compute_flow_schedule_data_align_params(&data);
2428 for (i = 0; i < data.n_sink; ++i)
2429 flow = compute_single_flow(flow, &data.sink[i], &data);
2431 isl_compute_flow_schedule_data_clear(&data);
2433 isl_union_access_info_free(access);
2434 return flow;
2435 error:
2436 isl_union_access_info_free(access);
2437 isl_compute_flow_schedule_data_clear(&data);
2438 return NULL;
2441 /* Given a description of the "sink" accesses, the "source" accesses and
2442 * a schedule, compute for each instance of a sink access
2443 * and for each element accessed by that instance,
2444 * the possible or definite source accesses that last accessed the
2445 * element accessed by the sink access before this sink access
2446 * in the sense that there is no intermediate definite source access.
2448 * The must_no_source and may_no_source elements of the result
2449 * are subsets of access->sink. The elements must_dep and may_dep
2450 * map domain elements of access->{may,must)_source to
2451 * domain elements of access->sink.
2453 * We check whether the schedule is available as a schedule tree
2454 * or a schedule map and call the correpsonding function to perform
2455 * the analysis.
2457 __isl_give isl_union_flow *isl_union_access_info_compute_flow(
2458 __isl_take isl_union_access_info *access)
2460 access = isl_union_access_info_normalize(access);
2461 if (!access)
2462 return NULL;
2463 if (access->schedule)
2464 return compute_flow_schedule(access);
2465 else
2466 return compute_flow_union_map(access);
2469 /* Given a collection of "sink" and "source" accesses,
2470 * compute for each iteration of a sink access
2471 * and for each element accessed by that iteration,
2472 * the source access in the list that last accessed the
2473 * element accessed by the sink access before this sink access.
2474 * Each access is given as a map from the loop iterators
2475 * to the array indices.
2476 * The result is a relations between source and sink
2477 * iterations and a subset of the domain of the sink accesses,
2478 * corresponding to those iterations that access an element
2479 * not previously accessed.
2481 * We collect the inputs in an isl_union_access_info object,
2482 * call isl_union_access_info_compute_flow and extract
2483 * the outputs from the result.
2485 int isl_union_map_compute_flow(__isl_take isl_union_map *sink,
2486 __isl_take isl_union_map *must_source,
2487 __isl_take isl_union_map *may_source,
2488 __isl_take isl_union_map *schedule,
2489 __isl_give isl_union_map **must_dep, __isl_give isl_union_map **may_dep,
2490 __isl_give isl_union_map **must_no_source,
2491 __isl_give isl_union_map **may_no_source)
2493 isl_union_access_info *access;
2494 isl_union_flow *flow;
2496 access = isl_union_access_info_from_sink(sink);
2497 access = isl_union_access_info_set_must_source(access, must_source);
2498 access = isl_union_access_info_set_may_source(access, may_source);
2499 access = isl_union_access_info_set_schedule_map(access, schedule);
2500 flow = isl_union_access_info_compute_flow(access);
2502 if (must_dep)
2503 *must_dep = isl_union_flow_get_must_dependence(flow);
2504 if (may_dep)
2505 *may_dep = isl_union_flow_get_non_must_dependence(flow);
2506 if (must_no_source)
2507 *must_no_source = isl_union_flow_get_must_no_source(flow);
2508 if (may_no_source)
2509 *may_no_source = isl_union_flow_get_non_must_no_source(flow);
2511 isl_union_flow_free(flow);
2513 if ((must_dep && !*must_dep) || (may_dep && !*may_dep) ||
2514 (must_no_source && !*must_no_source) ||
2515 (may_no_source && !*may_no_source))
2516 goto error;
2518 return 0;
2519 error:
2520 if (must_dep)
2521 *must_dep = isl_union_map_free(*must_dep);
2522 if (may_dep)
2523 *may_dep = isl_union_map_free(*may_dep);
2524 if (must_no_source)
2525 *must_no_source = isl_union_map_free(*must_no_source);
2526 if (may_no_source)
2527 *may_no_source = isl_union_map_free(*may_no_source);
2528 return -1;