isl_basic_map_remove_redundancies: sort constraints
[isl.git] / isl_flow.c
blobb5ae691d844a64cf1f4d9bf17df4973c70ea7353
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 acc = 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_access_info_free(acc);
1096 isl_flow_free(res);
1097 return NULL;
1100 /* Given a "sink" access, a list of n "source" accesses,
1101 * compute for each iteration of the sink access
1102 * and for each element accessed by that iteration,
1103 * the source access in the list that last accessed the
1104 * element accessed by the sink access before this sink access.
1105 * Each access is given as a map from the loop iterators
1106 * to the array indices.
1107 * The result is a list of n relations between source and sink
1108 * iterations and a subset of the domain of the sink access,
1109 * corresponding to those iterations that access an element
1110 * not previously accessed.
1112 * To deal with multi-valued sink access relations,
1113 * access_info_compute_flow_core extends the sink iteration domain
1114 * with dimensions that correspond to the data space. These extra dimensions
1115 * are projected out from the result of access_info_compute_flow_core.
1117 __isl_give isl_flow *isl_access_info_compute_flow(__isl_take isl_access_info *acc)
1119 int j;
1120 struct isl_flow *res;
1122 if (!acc)
1123 return NULL;
1125 acc->domain_map = isl_map_domain_map(isl_map_copy(acc->sink.map));
1126 res = access_info_compute_flow_core(acc);
1127 if (!res)
1128 return NULL;
1130 for (j = 0; j < res->n_source; ++j) {
1131 res->dep[j].map = isl_map_range_factor_domain(res->dep[j].map);
1132 if (!res->dep[j].map)
1133 goto error;
1136 return res;
1137 error:
1138 isl_flow_free(res);
1139 return NULL;
1143 /* Keep track of some information about a schedule for a given
1144 * access. In particular, keep track of which dimensions
1145 * have a constant value and of the actual constant values.
1147 struct isl_sched_info {
1148 int *is_cst;
1149 isl_vec *cst;
1152 static void sched_info_free(__isl_take struct isl_sched_info *info)
1154 if (!info)
1155 return;
1156 isl_vec_free(info->cst);
1157 free(info->is_cst);
1158 free(info);
1161 /* Extract information on the constant dimensions of the schedule
1162 * for a given access. The "map" is of the form
1164 * [S -> D] -> A
1166 * with S the schedule domain, D the iteration domain and A the data domain.
1168 static __isl_give struct isl_sched_info *sched_info_alloc(
1169 __isl_keep isl_map *map)
1171 isl_ctx *ctx;
1172 isl_space *dim;
1173 struct isl_sched_info *info;
1174 int i, n;
1176 if (!map)
1177 return NULL;
1179 dim = isl_space_unwrap(isl_space_domain(isl_map_get_space(map)));
1180 if (!dim)
1181 return NULL;
1182 n = isl_space_dim(dim, isl_dim_in);
1183 isl_space_free(dim);
1185 ctx = isl_map_get_ctx(map);
1186 info = isl_alloc_type(ctx, struct isl_sched_info);
1187 if (!info)
1188 return NULL;
1189 info->is_cst = isl_alloc_array(ctx, int, n);
1190 info->cst = isl_vec_alloc(ctx, n);
1191 if (n && (!info->is_cst || !info->cst))
1192 goto error;
1194 for (i = 0; i < n; ++i) {
1195 isl_val *v;
1197 v = isl_map_plain_get_val_if_fixed(map, isl_dim_in, i);
1198 if (!v)
1199 goto error;
1200 info->is_cst[i] = !isl_val_is_nan(v);
1201 if (info->is_cst[i])
1202 info->cst = isl_vec_set_element_val(info->cst, i, v);
1203 else
1204 isl_val_free(v);
1207 return info;
1208 error:
1209 sched_info_free(info);
1210 return NULL;
1213 /* This structure represents the input for a dependence analysis computation.
1215 * "sink" represents the sink accesses.
1216 * "must_source" represents the definite source accesses.
1217 * "may_source" represents the possible source accesses.
1219 * "schedule" or "schedule_map" represents the execution order.
1220 * Exactly one of these fields should be NULL. The other field
1221 * determines the execution order.
1223 * The domains of these four maps refer to the same iteration spaces(s).
1224 * The ranges of the first three maps also refer to the same data space(s).
1226 * After a call to isl_union_access_info_introduce_schedule,
1227 * the "schedule_map" field no longer contains useful information.
1229 struct isl_union_access_info {
1230 isl_union_map *sink;
1231 isl_union_map *must_source;
1232 isl_union_map *may_source;
1234 isl_schedule *schedule;
1235 isl_union_map *schedule_map;
1238 /* Free "access" and return NULL.
1240 __isl_null isl_union_access_info *isl_union_access_info_free(
1241 __isl_take isl_union_access_info *access)
1243 if (!access)
1244 return NULL;
1246 isl_union_map_free(access->sink);
1247 isl_union_map_free(access->must_source);
1248 isl_union_map_free(access->may_source);
1249 isl_schedule_free(access->schedule);
1250 isl_union_map_free(access->schedule_map);
1251 free(access);
1253 return NULL;
1256 /* Return the isl_ctx to which "access" belongs.
1258 isl_ctx *isl_union_access_info_get_ctx(__isl_keep isl_union_access_info *access)
1260 return access ? isl_union_map_get_ctx(access->sink) : NULL;
1263 /* Create a new isl_union_access_info with the given sink accesses and
1264 * and no source accesses or schedule information.
1266 * By default, we use the schedule field of the isl_union_access_info,
1267 * but this may be overridden by a call
1268 * to isl_union_access_info_set_schedule_map.
1270 __isl_give isl_union_access_info *isl_union_access_info_from_sink(
1271 __isl_take isl_union_map *sink)
1273 isl_ctx *ctx;
1274 isl_space *space;
1275 isl_union_map *empty;
1276 isl_union_access_info *access;
1278 if (!sink)
1279 return NULL;
1280 ctx = isl_union_map_get_ctx(sink);
1281 access = isl_alloc_type(ctx, isl_union_access_info);
1282 if (!access)
1283 goto error;
1285 space = isl_union_map_get_space(sink);
1286 empty = isl_union_map_empty(isl_space_copy(space));
1287 access->sink = sink;
1288 access->must_source = isl_union_map_copy(empty);
1289 access->may_source = empty;
1290 access->schedule = isl_schedule_empty(space);
1291 access->schedule_map = NULL;
1293 if (!access->sink || !access->must_source ||
1294 !access->may_source || !access->schedule)
1295 return isl_union_access_info_free(access);
1297 return access;
1298 error:
1299 isl_union_map_free(sink);
1300 return NULL;
1303 /* Replace the definite source accesses of "access" by "must_source".
1305 __isl_give isl_union_access_info *isl_union_access_info_set_must_source(
1306 __isl_take isl_union_access_info *access,
1307 __isl_take isl_union_map *must_source)
1309 if (!access || !must_source)
1310 goto error;
1312 isl_union_map_free(access->must_source);
1313 access->must_source = must_source;
1315 return access;
1316 error:
1317 isl_union_access_info_free(access);
1318 isl_union_map_free(must_source);
1319 return NULL;
1322 /* Replace the possible source accesses of "access" by "may_source".
1324 __isl_give isl_union_access_info *isl_union_access_info_set_may_source(
1325 __isl_take isl_union_access_info *access,
1326 __isl_take isl_union_map *may_source)
1328 if (!access || !may_source)
1329 goto error;
1331 isl_union_map_free(access->may_source);
1332 access->may_source = may_source;
1334 return access;
1335 error:
1336 isl_union_access_info_free(access);
1337 isl_union_map_free(may_source);
1338 return NULL;
1341 /* Replace the schedule of "access" by "schedule".
1342 * Also free the schedule_map in case it was set last.
1344 __isl_give isl_union_access_info *isl_union_access_info_set_schedule(
1345 __isl_take isl_union_access_info *access,
1346 __isl_take isl_schedule *schedule)
1348 if (!access || !schedule)
1349 goto error;
1351 access->schedule_map = isl_union_map_free(access->schedule_map);
1352 isl_schedule_free(access->schedule);
1353 access->schedule = schedule;
1355 return access;
1356 error:
1357 isl_union_access_info_free(access);
1358 isl_schedule_free(schedule);
1359 return NULL;
1362 /* Replace the schedule map of "access" by "schedule_map".
1363 * Also free the schedule in case it was set last.
1365 __isl_give isl_union_access_info *isl_union_access_info_set_schedule_map(
1366 __isl_take isl_union_access_info *access,
1367 __isl_take isl_union_map *schedule_map)
1369 if (!access || !schedule_map)
1370 goto error;
1372 isl_union_map_free(access->schedule_map);
1373 access->schedule = isl_schedule_free(access->schedule);
1374 access->schedule_map = schedule_map;
1376 return access;
1377 error:
1378 isl_union_access_info_free(access);
1379 isl_union_map_free(schedule_map);
1380 return NULL;
1383 __isl_give isl_union_access_info *isl_union_access_info_copy(
1384 __isl_keep isl_union_access_info *access)
1386 isl_union_access_info *copy;
1388 if (!access)
1389 return NULL;
1390 copy = isl_union_access_info_from_sink(
1391 isl_union_map_copy(access->sink));
1392 copy = isl_union_access_info_set_must_source(copy,
1393 isl_union_map_copy(access->must_source));
1394 copy = isl_union_access_info_set_may_source(copy,
1395 isl_union_map_copy(access->may_source));
1396 if (access->schedule)
1397 copy = isl_union_access_info_set_schedule(copy,
1398 isl_schedule_copy(access->schedule));
1399 else
1400 copy = isl_union_access_info_set_schedule_map(copy,
1401 isl_union_map_copy(access->schedule_map));
1403 return copy;
1406 /* Print a key-value pair of a YAML mapping to "p",
1407 * with key "name" and value "umap".
1409 static __isl_give isl_printer *print_union_map_field(__isl_take isl_printer *p,
1410 const char *name, __isl_keep isl_union_map *umap)
1412 p = isl_printer_print_str(p, name);
1413 p = isl_printer_yaml_next(p);
1414 p = isl_printer_print_str(p, "\"");
1415 p = isl_printer_print_union_map(p, umap);
1416 p = isl_printer_print_str(p, "\"");
1417 p = isl_printer_yaml_next(p);
1419 return p;
1422 /* Print the information contained in "access" to "p".
1423 * The information is printed as a YAML document.
1425 __isl_give isl_printer *isl_printer_print_union_access_info(
1426 __isl_take isl_printer *p, __isl_keep isl_union_access_info *access)
1428 if (!access)
1429 return isl_printer_free(p);
1431 p = isl_printer_yaml_start_mapping(p);
1432 p = print_union_map_field(p, "sink", access->sink);
1433 p = print_union_map_field(p, "must_source", access->must_source);
1434 p = print_union_map_field(p, "may_source", access->may_source);
1435 if (access->schedule) {
1436 p = isl_printer_print_str(p, "schedule");
1437 p = isl_printer_yaml_next(p);
1438 p = isl_printer_print_schedule(p, access->schedule);
1439 p = isl_printer_yaml_next(p);
1440 } else {
1441 p = print_union_map_field(p, "schedule_map",
1442 access->schedule_map);
1444 p = isl_printer_yaml_end_mapping(p);
1446 return p;
1449 /* Return a string representation of the information in "access".
1450 * The information is printed in flow format.
1452 __isl_give char *isl_union_access_info_to_str(
1453 __isl_keep isl_union_access_info *access)
1455 isl_printer *p;
1456 char *s;
1458 if (!access)
1459 return NULL;
1461 p = isl_printer_to_str(isl_union_access_info_get_ctx(access));
1462 p = isl_printer_set_yaml_style(p, ISL_YAML_STYLE_FLOW);
1463 p = isl_printer_print_union_access_info(p, access);
1464 s = isl_printer_get_str(p);
1465 isl_printer_free(p);
1467 return s;
1470 /* Update the fields of "access" such that they all have the same parameters,
1471 * keeping in mind that the schedule_map field may be NULL and ignoring
1472 * the schedule field.
1474 static __isl_give isl_union_access_info *isl_union_access_info_align_params(
1475 __isl_take isl_union_access_info *access)
1477 isl_space *space;
1479 if (!access)
1480 return NULL;
1482 space = isl_union_map_get_space(access->sink);
1483 space = isl_space_align_params(space,
1484 isl_union_map_get_space(access->must_source));
1485 space = isl_space_align_params(space,
1486 isl_union_map_get_space(access->may_source));
1487 if (access->schedule_map)
1488 space = isl_space_align_params(space,
1489 isl_union_map_get_space(access->schedule_map));
1490 access->sink = isl_union_map_align_params(access->sink,
1491 isl_space_copy(space));
1492 access->must_source = isl_union_map_align_params(access->must_source,
1493 isl_space_copy(space));
1494 access->may_source = isl_union_map_align_params(access->may_source,
1495 isl_space_copy(space));
1496 if (!access->schedule_map) {
1497 isl_space_free(space);
1498 } else {
1499 access->schedule_map =
1500 isl_union_map_align_params(access->schedule_map, space);
1501 if (!access->schedule_map)
1502 return isl_union_access_info_free(access);
1505 if (!access->sink || !access->must_source || !access->may_source)
1506 return isl_union_access_info_free(access);
1508 return access;
1511 /* Prepend the schedule dimensions to the iteration domains.
1513 * That is, if the schedule is of the form
1515 * D -> S
1517 * while the access relations are of the form
1519 * D -> A
1521 * then the updated access relations are of the form
1523 * [S -> D] -> A
1525 * The schedule map is also replaced by the map
1527 * [S -> D] -> D
1529 * that is used during the internal computation.
1530 * Neither the original schedule map nor this updated schedule map
1531 * are used after the call to this function.
1533 static __isl_give isl_union_access_info *
1534 isl_union_access_info_introduce_schedule(
1535 __isl_take isl_union_access_info *access)
1537 isl_union_map *sm;
1539 if (!access)
1540 return NULL;
1542 sm = isl_union_map_reverse(access->schedule_map);
1543 sm = isl_union_map_range_map(sm);
1544 access->sink = isl_union_map_apply_range(isl_union_map_copy(sm),
1545 access->sink);
1546 access->may_source = isl_union_map_apply_range(isl_union_map_copy(sm),
1547 access->may_source);
1548 access->must_source = isl_union_map_apply_range(isl_union_map_copy(sm),
1549 access->must_source);
1550 access->schedule_map = sm;
1552 if (!access->sink || !access->must_source ||
1553 !access->may_source || !access->schedule_map)
1554 return isl_union_access_info_free(access);
1556 return access;
1559 /* This structure represents the result of a dependence analysis computation.
1561 * "must_dep" represents the full definite dependences
1562 * "may_dep" represents the full non-definite dependences.
1563 * Both are of the form
1565 * [Source] -> [[Sink -> Data]]
1567 * (after the schedule dimensions have been projected out).
1568 * "must_no_source" represents the subset of the sink accesses for which
1569 * definitely no source was found.
1570 * "may_no_source" represents the subset of the sink accesses for which
1571 * possibly, but not definitely, no source was found.
1573 struct isl_union_flow {
1574 isl_union_map *must_dep;
1575 isl_union_map *may_dep;
1576 isl_union_map *must_no_source;
1577 isl_union_map *may_no_source;
1580 /* Return the isl_ctx to which "flow" belongs.
1582 isl_ctx *isl_union_flow_get_ctx(__isl_keep isl_union_flow *flow)
1584 return flow ? isl_union_map_get_ctx(flow->must_dep) : NULL;
1587 /* Free "flow" and return NULL.
1589 __isl_null isl_union_flow *isl_union_flow_free(__isl_take isl_union_flow *flow)
1591 if (!flow)
1592 return NULL;
1593 isl_union_map_free(flow->must_dep);
1594 isl_union_map_free(flow->may_dep);
1595 isl_union_map_free(flow->must_no_source);
1596 isl_union_map_free(flow->may_no_source);
1597 free(flow);
1598 return NULL;
1601 void isl_union_flow_dump(__isl_keep isl_union_flow *flow)
1603 if (!flow)
1604 return;
1606 fprintf(stderr, "must dependences: ");
1607 isl_union_map_dump(flow->must_dep);
1608 fprintf(stderr, "may dependences: ");
1609 isl_union_map_dump(flow->may_dep);
1610 fprintf(stderr, "must no source: ");
1611 isl_union_map_dump(flow->must_no_source);
1612 fprintf(stderr, "may no source: ");
1613 isl_union_map_dump(flow->may_no_source);
1616 /* Return the full definite dependences in "flow", with accessed elements.
1618 __isl_give isl_union_map *isl_union_flow_get_full_must_dependence(
1619 __isl_keep isl_union_flow *flow)
1621 if (!flow)
1622 return NULL;
1623 return isl_union_map_copy(flow->must_dep);
1626 /* Return the full possible dependences in "flow", including the definite
1627 * dependences, with accessed elements.
1629 __isl_give isl_union_map *isl_union_flow_get_full_may_dependence(
1630 __isl_keep isl_union_flow *flow)
1632 if (!flow)
1633 return NULL;
1634 return isl_union_map_union(isl_union_map_copy(flow->must_dep),
1635 isl_union_map_copy(flow->may_dep));
1638 /* Return the definite dependences in "flow", without the accessed elements.
1640 __isl_give isl_union_map *isl_union_flow_get_must_dependence(
1641 __isl_keep isl_union_flow *flow)
1643 isl_union_map *dep;
1645 if (!flow)
1646 return NULL;
1647 dep = isl_union_map_copy(flow->must_dep);
1648 return isl_union_map_range_factor_domain(dep);
1651 /* Return the possible dependences in "flow", including the definite
1652 * dependences, without the accessed elements.
1654 __isl_give isl_union_map *isl_union_flow_get_may_dependence(
1655 __isl_keep isl_union_flow *flow)
1657 isl_union_map *dep;
1659 if (!flow)
1660 return NULL;
1661 dep = isl_union_map_union(isl_union_map_copy(flow->must_dep),
1662 isl_union_map_copy(flow->may_dep));
1663 return isl_union_map_range_factor_domain(dep);
1666 /* Return the non-definite dependences in "flow".
1668 static __isl_give isl_union_map *isl_union_flow_get_non_must_dependence(
1669 __isl_keep isl_union_flow *flow)
1671 if (!flow)
1672 return NULL;
1673 return isl_union_map_copy(flow->may_dep);
1676 /* Return the subset of the sink accesses for which definitely
1677 * no source was found.
1679 __isl_give isl_union_map *isl_union_flow_get_must_no_source(
1680 __isl_keep isl_union_flow *flow)
1682 if (!flow)
1683 return NULL;
1684 return isl_union_map_copy(flow->must_no_source);
1687 /* Return the subset of the sink accesses for which possibly
1688 * no source was found, including those for which definitely
1689 * no source was found.
1691 __isl_give isl_union_map *isl_union_flow_get_may_no_source(
1692 __isl_keep isl_union_flow *flow)
1694 if (!flow)
1695 return NULL;
1696 return isl_union_map_union(isl_union_map_copy(flow->must_no_source),
1697 isl_union_map_copy(flow->may_no_source));
1700 /* Return the subset of the sink accesses for which possibly, but not
1701 * definitely, no source was found.
1703 static __isl_give isl_union_map *isl_union_flow_get_non_must_no_source(
1704 __isl_keep isl_union_flow *flow)
1706 if (!flow)
1707 return NULL;
1708 return isl_union_map_copy(flow->may_no_source);
1711 /* Create a new isl_union_flow object, initialized with empty
1712 * dependence relations and sink subsets.
1714 static __isl_give isl_union_flow *isl_union_flow_alloc(
1715 __isl_take isl_space *space)
1717 isl_ctx *ctx;
1718 isl_union_map *empty;
1719 isl_union_flow *flow;
1721 if (!space)
1722 return NULL;
1723 ctx = isl_space_get_ctx(space);
1724 flow = isl_alloc_type(ctx, isl_union_flow);
1725 if (!flow)
1726 goto error;
1728 empty = isl_union_map_empty(space);
1729 flow->must_dep = isl_union_map_copy(empty);
1730 flow->may_dep = isl_union_map_copy(empty);
1731 flow->must_no_source = isl_union_map_copy(empty);
1732 flow->may_no_source = empty;
1734 if (!flow->must_dep || !flow->may_dep ||
1735 !flow->must_no_source || !flow->may_no_source)
1736 return isl_union_flow_free(flow);
1738 return flow;
1739 error:
1740 isl_space_free(space);
1741 return NULL;
1744 /* Drop the schedule dimensions from the iteration domains in "flow".
1745 * In particular, the schedule dimensions have been prepended
1746 * to the iteration domains prior to the dependence analysis by
1747 * replacing the iteration domain D, by the wrapped map [S -> D].
1748 * Replace these wrapped maps by the original D.
1750 * In particular, the dependences computed by access_info_compute_flow_core
1751 * are of the form
1753 * [S -> D] -> [[S' -> D'] -> A]
1755 * The schedule dimensions are projected out by first currying the range,
1756 * resulting in
1758 * [S -> D] -> [S' -> [D' -> A]]
1760 * and then computing the factor range
1762 * D -> [D' -> A]
1764 static __isl_give isl_union_flow *isl_union_flow_drop_schedule(
1765 __isl_take isl_union_flow *flow)
1767 if (!flow)
1768 return NULL;
1770 flow->must_dep = isl_union_map_range_curry(flow->must_dep);
1771 flow->must_dep = isl_union_map_factor_range(flow->must_dep);
1772 flow->may_dep = isl_union_map_range_curry(flow->may_dep);
1773 flow->may_dep = isl_union_map_factor_range(flow->may_dep);
1774 flow->must_no_source =
1775 isl_union_map_domain_factor_range(flow->must_no_source);
1776 flow->may_no_source =
1777 isl_union_map_domain_factor_range(flow->may_no_source);
1779 if (!flow->must_dep || !flow->may_dep ||
1780 !flow->must_no_source || !flow->may_no_source)
1781 return isl_union_flow_free(flow);
1783 return flow;
1786 struct isl_compute_flow_data {
1787 isl_union_map *must_source;
1788 isl_union_map *may_source;
1789 isl_union_flow *flow;
1791 int count;
1792 int must;
1793 isl_space *dim;
1794 struct isl_sched_info *sink_info;
1795 struct isl_sched_info **source_info;
1796 isl_access_info *accesses;
1799 static isl_stat count_matching_array(__isl_take isl_map *map, void *user)
1801 int eq;
1802 isl_space *dim;
1803 struct isl_compute_flow_data *data;
1805 data = (struct isl_compute_flow_data *)user;
1807 dim = isl_space_range(isl_map_get_space(map));
1809 eq = isl_space_is_equal(dim, data->dim);
1811 isl_space_free(dim);
1812 isl_map_free(map);
1814 if (eq < 0)
1815 return isl_stat_error;
1816 if (eq)
1817 data->count++;
1819 return isl_stat_ok;
1822 static isl_stat collect_matching_array(__isl_take isl_map *map, void *user)
1824 int eq;
1825 isl_space *dim;
1826 struct isl_sched_info *info;
1827 struct isl_compute_flow_data *data;
1829 data = (struct isl_compute_flow_data *)user;
1831 dim = isl_space_range(isl_map_get_space(map));
1833 eq = isl_space_is_equal(dim, data->dim);
1835 isl_space_free(dim);
1837 if (eq < 0)
1838 goto error;
1839 if (!eq) {
1840 isl_map_free(map);
1841 return isl_stat_ok;
1844 info = sched_info_alloc(map);
1845 data->source_info[data->count] = info;
1847 data->accesses = isl_access_info_add_source(data->accesses,
1848 map, data->must, info);
1850 data->count++;
1852 return isl_stat_ok;
1853 error:
1854 isl_map_free(map);
1855 return isl_stat_error;
1858 /* Determine the shared nesting level and the "textual order" of
1859 * the given accesses.
1861 * We first determine the minimal schedule dimension for both accesses.
1863 * If among those dimensions, we can find one where both have a fixed
1864 * value and if moreover those values are different, then the previous
1865 * dimension is the last shared nesting level and the textual order
1866 * is determined based on the order of the fixed values.
1867 * If no such fixed values can be found, then we set the shared
1868 * nesting level to the minimal schedule dimension, with no textual ordering.
1870 static int before(void *first, void *second)
1872 struct isl_sched_info *info1 = first;
1873 struct isl_sched_info *info2 = second;
1874 int n1, n2;
1875 int i;
1877 n1 = isl_vec_size(info1->cst);
1878 n2 = isl_vec_size(info2->cst);
1880 if (n2 < n1)
1881 n1 = n2;
1883 for (i = 0; i < n1; ++i) {
1884 int r;
1885 int cmp;
1887 if (!info1->is_cst[i])
1888 continue;
1889 if (!info2->is_cst[i])
1890 continue;
1891 cmp = isl_vec_cmp_element(info1->cst, info2->cst, i);
1892 if (cmp == 0)
1893 continue;
1895 r = 2 * i + (cmp < 0);
1897 return r;
1900 return 2 * n1;
1903 /* Given a sink access, look for all the source accesses that access
1904 * the same array and perform dataflow analysis on them using
1905 * isl_access_info_compute_flow_core.
1907 static isl_stat compute_flow(__isl_take isl_map *map, void *user)
1909 int i;
1910 isl_ctx *ctx;
1911 struct isl_compute_flow_data *data;
1912 isl_flow *flow;
1913 isl_union_flow *df;
1915 data = (struct isl_compute_flow_data *)user;
1916 df = data->flow;
1918 ctx = isl_map_get_ctx(map);
1920 data->accesses = NULL;
1921 data->sink_info = NULL;
1922 data->source_info = NULL;
1923 data->count = 0;
1924 data->dim = isl_space_range(isl_map_get_space(map));
1926 if (isl_union_map_foreach_map(data->must_source,
1927 &count_matching_array, data) < 0)
1928 goto error;
1929 if (isl_union_map_foreach_map(data->may_source,
1930 &count_matching_array, data) < 0)
1931 goto error;
1933 data->sink_info = sched_info_alloc(map);
1934 data->source_info = isl_calloc_array(ctx, struct isl_sched_info *,
1935 data->count);
1937 data->accesses = isl_access_info_alloc(isl_map_copy(map),
1938 data->sink_info, &before, data->count);
1939 if (!data->sink_info || (data->count && !data->source_info) ||
1940 !data->accesses)
1941 goto error;
1942 data->count = 0;
1943 data->must = 1;
1944 if (isl_union_map_foreach_map(data->must_source,
1945 &collect_matching_array, data) < 0)
1946 goto error;
1947 data->must = 0;
1948 if (isl_union_map_foreach_map(data->may_source,
1949 &collect_matching_array, data) < 0)
1950 goto error;
1952 flow = access_info_compute_flow_core(data->accesses);
1953 data->accesses = NULL;
1955 if (!flow)
1956 goto error;
1958 df->must_no_source = isl_union_map_union(df->must_no_source,
1959 isl_union_map_from_map(isl_flow_get_no_source(flow, 1)));
1960 df->may_no_source = isl_union_map_union(df->may_no_source,
1961 isl_union_map_from_map(isl_flow_get_no_source(flow, 0)));
1963 for (i = 0; i < flow->n_source; ++i) {
1964 isl_union_map *dep;
1965 dep = isl_union_map_from_map(isl_map_copy(flow->dep[i].map));
1966 if (flow->dep[i].must)
1967 df->must_dep = isl_union_map_union(df->must_dep, dep);
1968 else
1969 df->may_dep = isl_union_map_union(df->may_dep, dep);
1972 isl_flow_free(flow);
1974 sched_info_free(data->sink_info);
1975 if (data->source_info) {
1976 for (i = 0; i < data->count; ++i)
1977 sched_info_free(data->source_info[i]);
1978 free(data->source_info);
1980 isl_space_free(data->dim);
1981 isl_map_free(map);
1983 return isl_stat_ok;
1984 error:
1985 isl_access_info_free(data->accesses);
1986 sched_info_free(data->sink_info);
1987 if (data->source_info) {
1988 for (i = 0; i < data->count; ++i)
1989 sched_info_free(data->source_info[i]);
1990 free(data->source_info);
1992 isl_space_free(data->dim);
1993 isl_map_free(map);
1995 return isl_stat_error;
1998 /* Remove the must accesses from the may accesses.
2000 * A must access always trumps a may access, so there is no need
2001 * for a must access to also be considered as a may access. Doing so
2002 * would only cost extra computations only to find out that
2003 * the duplicated may access does not make any difference.
2005 static __isl_give isl_union_access_info *isl_union_access_info_normalize(
2006 __isl_take isl_union_access_info *access)
2008 if (!access)
2009 return NULL;
2010 access->may_source = isl_union_map_subtract(access->may_source,
2011 isl_union_map_copy(access->must_source));
2012 if (!access->may_source)
2013 return isl_union_access_info_free(access);
2015 return access;
2018 /* Given a description of the "sink" accesses, the "source" accesses and
2019 * a schedule, compute for each instance of a sink access
2020 * and for each element accessed by that instance,
2021 * the possible or definite source accesses that last accessed the
2022 * element accessed by the sink access before this sink access
2023 * in the sense that there is no intermediate definite source access.
2025 * The must_no_source and may_no_source elements of the result
2026 * are subsets of access->sink. The elements must_dep and may_dep
2027 * map domain elements of access->{may,must)_source to
2028 * domain elements of access->sink.
2030 * This function is used when only the schedule map representation
2031 * is available.
2033 * We first prepend the schedule dimensions to the domain
2034 * of the accesses so that we can easily compare their relative order.
2035 * Then we consider each sink access individually in compute_flow.
2037 static __isl_give isl_union_flow *compute_flow_union_map(
2038 __isl_take isl_union_access_info *access)
2040 struct isl_compute_flow_data data;
2042 access = isl_union_access_info_align_params(access);
2043 access = isl_union_access_info_introduce_schedule(access);
2044 if (!access)
2045 return NULL;
2047 data.must_source = access->must_source;
2048 data.may_source = access->may_source;
2050 data.flow = isl_union_flow_alloc(isl_union_map_get_space(access->sink));
2052 if (isl_union_map_foreach_map(access->sink, &compute_flow, &data) < 0)
2053 goto error;
2055 data.flow = isl_union_flow_drop_schedule(data.flow);
2057 isl_union_access_info_free(access);
2058 return data.flow;
2059 error:
2060 isl_union_access_info_free(access);
2061 isl_union_flow_free(data.flow);
2062 return NULL;
2065 /* A schedule access relation.
2067 * The access relation "access" is of the form [S -> D] -> A,
2068 * where S corresponds to the prefix schedule at "node".
2069 * "must" is only relevant for source accesses and indicates
2070 * whether the access is a must source or a may source.
2072 struct isl_scheduled_access {
2073 isl_map *access;
2074 int must;
2075 isl_schedule_node *node;
2078 /* Data structure for keeping track of individual scheduled sink and source
2079 * accesses when computing dependence analysis based on a schedule tree.
2081 * "n_sink" is the number of used entries in "sink"
2082 * "n_source" is the number of used entries in "source"
2084 * "set_sink", "must" and "node" are only used inside collect_sink_source,
2085 * to keep track of the current node and
2086 * of what extract_sink_source needs to do.
2088 struct isl_compute_flow_schedule_data {
2089 isl_union_access_info *access;
2091 int n_sink;
2092 int n_source;
2094 struct isl_scheduled_access *sink;
2095 struct isl_scheduled_access *source;
2097 int set_sink;
2098 int must;
2099 isl_schedule_node *node;
2102 /* Align the parameters of all sinks with all sources.
2104 * If there are no sinks or no sources, then no alignment is needed.
2106 static void isl_compute_flow_schedule_data_align_params(
2107 struct isl_compute_flow_schedule_data *data)
2109 int i;
2110 isl_space *space;
2112 if (data->n_sink == 0 || data->n_source == 0)
2113 return;
2115 space = isl_map_get_space(data->sink[0].access);
2117 for (i = 1; i < data->n_sink; ++i)
2118 space = isl_space_align_params(space,
2119 isl_map_get_space(data->sink[i].access));
2120 for (i = 0; i < data->n_source; ++i)
2121 space = isl_space_align_params(space,
2122 isl_map_get_space(data->source[i].access));
2124 for (i = 0; i < data->n_sink; ++i)
2125 data->sink[i].access =
2126 isl_map_align_params(data->sink[i].access,
2127 isl_space_copy(space));
2128 for (i = 0; i < data->n_source; ++i)
2129 data->source[i].access =
2130 isl_map_align_params(data->source[i].access,
2131 isl_space_copy(space));
2133 isl_space_free(space);
2136 /* Free all the memory referenced from "data".
2137 * Do not free "data" itself as it may be allocated on the stack.
2139 static void isl_compute_flow_schedule_data_clear(
2140 struct isl_compute_flow_schedule_data *data)
2142 int i;
2144 if (!data->sink)
2145 return;
2147 for (i = 0; i < data->n_sink; ++i) {
2148 isl_map_free(data->sink[i].access);
2149 isl_schedule_node_free(data->sink[i].node);
2152 for (i = 0; i < data->n_source; ++i) {
2153 isl_map_free(data->source[i].access);
2154 isl_schedule_node_free(data->source[i].node);
2157 free(data->sink);
2160 /* isl_schedule_foreach_schedule_node_top_down callback for counting
2161 * (an upper bound on) the number of sinks and sources.
2163 * Sinks and sources are only extracted at leaves of the tree,
2164 * so we skip the node if it is not a leaf.
2165 * Otherwise we increment data->n_sink and data->n_source with
2166 * the number of spaces in the sink and source access domains
2167 * that reach this node.
2169 static isl_bool count_sink_source(__isl_keep isl_schedule_node *node,
2170 void *user)
2172 struct isl_compute_flow_schedule_data *data = user;
2173 isl_union_set *domain;
2174 isl_union_map *umap;
2175 isl_bool r = isl_bool_false;
2177 if (isl_schedule_node_get_type(node) != isl_schedule_node_leaf)
2178 return isl_bool_true;
2180 domain = isl_schedule_node_get_universe_domain(node);
2182 umap = isl_union_map_copy(data->access->sink);
2183 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
2184 data->n_sink += isl_union_map_n_map(umap);
2185 isl_union_map_free(umap);
2186 if (!umap)
2187 r = isl_bool_error;
2189 umap = isl_union_map_copy(data->access->must_source);
2190 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
2191 data->n_source += isl_union_map_n_map(umap);
2192 isl_union_map_free(umap);
2193 if (!umap)
2194 r = isl_bool_error;
2196 umap = isl_union_map_copy(data->access->may_source);
2197 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
2198 data->n_source += isl_union_map_n_map(umap);
2199 isl_union_map_free(umap);
2200 if (!umap)
2201 r = isl_bool_error;
2203 isl_union_set_free(domain);
2205 return r;
2208 /* Add a single scheduled sink or source (depending on data->set_sink)
2209 * with scheduled access relation "map", must property data->must and
2210 * schedule node data->node to the list of sinks or sources.
2212 static isl_stat extract_sink_source(__isl_take isl_map *map, void *user)
2214 struct isl_compute_flow_schedule_data *data = user;
2215 struct isl_scheduled_access *access;
2217 if (data->set_sink)
2218 access = data->sink + data->n_sink++;
2219 else
2220 access = data->source + data->n_source++;
2222 access->access = map;
2223 access->must = data->must;
2224 access->node = isl_schedule_node_copy(data->node);
2226 return isl_stat_ok;
2229 /* isl_schedule_foreach_schedule_node_top_down callback for collecting
2230 * individual scheduled source and sink accesses (taking into account
2231 * the domain of the schedule).
2233 * We only collect accesses at the leaves of the schedule tree.
2234 * We prepend the schedule dimensions at the leaf to the iteration
2235 * domains of the source and sink accesses and then extract
2236 * the individual accesses (per space).
2238 * In particular, if the prefix schedule at the node is of the form
2240 * D -> S
2242 * while the access relations are of the form
2244 * D -> A
2246 * then the updated access relations are of the form
2248 * [S -> D] -> A
2250 * Note that S consists of a single space such that introducing S
2251 * in the access relations does not increase the number of spaces.
2253 static isl_bool collect_sink_source(__isl_keep isl_schedule_node *node,
2254 void *user)
2256 struct isl_compute_flow_schedule_data *data = user;
2257 isl_union_map *prefix;
2258 isl_union_map *umap;
2259 isl_bool r = isl_bool_false;
2261 if (isl_schedule_node_get_type(node) != isl_schedule_node_leaf)
2262 return isl_bool_true;
2264 data->node = node;
2266 prefix = isl_schedule_node_get_prefix_schedule_relation(node);
2267 prefix = isl_union_map_reverse(prefix);
2268 prefix = isl_union_map_range_map(prefix);
2270 data->set_sink = 1;
2271 umap = isl_union_map_copy(data->access->sink);
2272 umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
2273 if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
2274 r = isl_bool_error;
2275 isl_union_map_free(umap);
2277 data->set_sink = 0;
2278 data->must = 1;
2279 umap = isl_union_map_copy(data->access->must_source);
2280 umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
2281 if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
2282 r = isl_bool_error;
2283 isl_union_map_free(umap);
2285 data->set_sink = 0;
2286 data->must = 0;
2287 umap = isl_union_map_copy(data->access->may_source);
2288 umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
2289 if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
2290 r = isl_bool_error;
2291 isl_union_map_free(umap);
2293 isl_union_map_free(prefix);
2295 return r;
2298 /* isl_access_info_compute_flow callback for determining whether
2299 * the shared nesting level and the ordering within that level
2300 * for two scheduled accesses for use in compute_single_flow.
2302 * The tokens passed to this function refer to the leaves
2303 * in the schedule tree where the accesses take place.
2305 * If n is the shared number of loops, then we need to return
2306 * "2 * n + 1" if "first" precedes "second" inside the innermost
2307 * shared loop and "2 * n" otherwise.
2309 * The innermost shared ancestor may be the leaves themselves
2310 * if the accesses take place in the same leaf. Otherwise,
2311 * it is either a set node or a sequence node. Only in the case
2312 * of a sequence node do we consider one access to precede the other.
2314 static int before_node(void *first, void *second)
2316 isl_schedule_node *node1 = first;
2317 isl_schedule_node *node2 = second;
2318 isl_schedule_node *shared;
2319 int depth;
2320 int before = 0;
2322 shared = isl_schedule_node_get_shared_ancestor(node1, node2);
2323 if (!shared)
2324 return -1;
2326 depth = isl_schedule_node_get_schedule_depth(shared);
2327 if (isl_schedule_node_get_type(shared) == isl_schedule_node_sequence) {
2328 int pos1, pos2;
2330 pos1 = isl_schedule_node_get_ancestor_child_position(node1,
2331 shared);
2332 pos2 = isl_schedule_node_get_ancestor_child_position(node2,
2333 shared);
2334 before = pos1 < pos2;
2337 isl_schedule_node_free(shared);
2339 return 2 * depth + before;
2342 /* Add the scheduled sources from "data" that access
2343 * the same data space as "sink" to "access".
2345 static __isl_give isl_access_info *add_matching_sources(
2346 __isl_take isl_access_info *access, struct isl_scheduled_access *sink,
2347 struct isl_compute_flow_schedule_data *data)
2349 int i;
2350 isl_space *space;
2352 space = isl_space_range(isl_map_get_space(sink->access));
2353 for (i = 0; i < data->n_source; ++i) {
2354 struct isl_scheduled_access *source;
2355 isl_space *source_space;
2356 int eq;
2358 source = &data->source[i];
2359 source_space = isl_map_get_space(source->access);
2360 source_space = isl_space_range(source_space);
2361 eq = isl_space_is_equal(space, source_space);
2362 isl_space_free(source_space);
2364 if (!eq)
2365 continue;
2366 if (eq < 0)
2367 goto error;
2369 access = isl_access_info_add_source(access,
2370 isl_map_copy(source->access), source->must, source->node);
2373 isl_space_free(space);
2374 return access;
2375 error:
2376 isl_space_free(space);
2377 isl_access_info_free(access);
2378 return NULL;
2381 /* Given a scheduled sink access relation "sink", compute the corresponding
2382 * dependences on the sources in "data" and add the computed dependences
2383 * to "uf".
2385 * The dependences computed by access_info_compute_flow_core are of the form
2387 * [S -> I] -> [[S' -> I'] -> A]
2389 * The schedule dimensions are projected out by first currying the range,
2390 * resulting in
2392 * [S -> I] -> [S' -> [I' -> A]]
2394 * and then computing the factor range
2396 * I -> [I' -> A]
2398 static __isl_give isl_union_flow *compute_single_flow(
2399 __isl_take isl_union_flow *uf, struct isl_scheduled_access *sink,
2400 struct isl_compute_flow_schedule_data *data)
2402 int i;
2403 isl_access_info *access;
2404 isl_flow *flow;
2405 isl_map *map;
2407 if (!uf)
2408 return NULL;
2410 access = isl_access_info_alloc(isl_map_copy(sink->access), sink->node,
2411 &before_node, data->n_source);
2412 access = add_matching_sources(access, sink, data);
2414 flow = access_info_compute_flow_core(access);
2415 if (!flow)
2416 return isl_union_flow_free(uf);
2418 map = isl_map_domain_factor_range(isl_flow_get_no_source(flow, 1));
2419 uf->must_no_source = isl_union_map_union(uf->must_no_source,
2420 isl_union_map_from_map(map));
2421 map = isl_map_domain_factor_range(isl_flow_get_no_source(flow, 0));
2422 uf->may_no_source = isl_union_map_union(uf->may_no_source,
2423 isl_union_map_from_map(map));
2425 for (i = 0; i < flow->n_source; ++i) {
2426 isl_union_map *dep;
2428 map = isl_map_range_curry(isl_map_copy(flow->dep[i].map));
2429 map = isl_map_factor_range(map);
2430 dep = isl_union_map_from_map(map);
2431 if (flow->dep[i].must)
2432 uf->must_dep = isl_union_map_union(uf->must_dep, dep);
2433 else
2434 uf->may_dep = isl_union_map_union(uf->may_dep, dep);
2437 isl_flow_free(flow);
2439 return uf;
2442 /* Given a description of the "sink" accesses, the "source" accesses and
2443 * a schedule, compute for each instance of a sink access
2444 * and for each element accessed by that instance,
2445 * the possible or definite source accesses that last accessed the
2446 * element accessed by the sink access before this sink access
2447 * in the sense that there is no intermediate definite source access.
2448 * Only consider dependences between statement instances that belong
2449 * to the domain of the schedule.
2451 * The must_no_source and may_no_source elements of the result
2452 * are subsets of access->sink. The elements must_dep and may_dep
2453 * map domain elements of access->{may,must)_source to
2454 * domain elements of access->sink.
2456 * This function is used when a schedule tree representation
2457 * is available.
2459 * We extract the individual scheduled source and sink access relations
2460 * (taking into account the domain of the schedule) and
2461 * then compute dependences for each scheduled sink individually.
2463 static __isl_give isl_union_flow *compute_flow_schedule(
2464 __isl_take isl_union_access_info *access)
2466 struct isl_compute_flow_schedule_data data = { access };
2467 int i, n;
2468 isl_ctx *ctx;
2469 isl_union_flow *flow;
2471 ctx = isl_union_access_info_get_ctx(access);
2473 data.n_sink = 0;
2474 data.n_source = 0;
2475 if (isl_schedule_foreach_schedule_node_top_down(access->schedule,
2476 &count_sink_source, &data) < 0)
2477 goto error;
2479 n = data.n_sink + data.n_source;
2480 data.sink = isl_calloc_array(ctx, struct isl_scheduled_access, n);
2481 if (n && !data.sink)
2482 goto error;
2483 data.source = data.sink + data.n_sink;
2485 data.n_sink = 0;
2486 data.n_source = 0;
2487 if (isl_schedule_foreach_schedule_node_top_down(access->schedule,
2488 &collect_sink_source, &data) < 0)
2489 goto error;
2491 flow = isl_union_flow_alloc(isl_union_map_get_space(access->sink));
2493 isl_compute_flow_schedule_data_align_params(&data);
2495 for (i = 0; i < data.n_sink; ++i)
2496 flow = compute_single_flow(flow, &data.sink[i], &data);
2498 isl_compute_flow_schedule_data_clear(&data);
2500 isl_union_access_info_free(access);
2501 return flow;
2502 error:
2503 isl_union_access_info_free(access);
2504 isl_compute_flow_schedule_data_clear(&data);
2505 return NULL;
2508 /* Given a description of the "sink" accesses, the "source" accesses and
2509 * a schedule, compute for each instance of a sink access
2510 * and for each element accessed by that instance,
2511 * the possible or definite source accesses that last accessed the
2512 * element accessed by the sink access before this sink access
2513 * in the sense that there is no intermediate definite source access.
2515 * The must_no_source and may_no_source elements of the result
2516 * are subsets of access->sink. The elements must_dep and may_dep
2517 * map domain elements of access->{may,must)_source to
2518 * domain elements of access->sink.
2520 * We check whether the schedule is available as a schedule tree
2521 * or a schedule map and call the correpsonding function to perform
2522 * the analysis.
2524 __isl_give isl_union_flow *isl_union_access_info_compute_flow(
2525 __isl_take isl_union_access_info *access)
2527 access = isl_union_access_info_normalize(access);
2528 if (!access)
2529 return NULL;
2530 if (access->schedule)
2531 return compute_flow_schedule(access);
2532 else
2533 return compute_flow_union_map(access);
2536 /* Print the information contained in "flow" to "p".
2537 * The information is printed as a YAML document.
2539 __isl_give isl_printer *isl_printer_print_union_flow(
2540 __isl_take isl_printer *p, __isl_keep isl_union_flow *flow)
2542 isl_union_map *umap;
2544 if (!flow)
2545 return isl_printer_free(p);
2547 p = isl_printer_yaml_start_mapping(p);
2548 p = print_union_map_field(p, "must_dependence", flow->must_dep);
2549 umap = isl_union_flow_get_may_dependence(flow);
2550 p = print_union_map_field(p, "may_dependence", umap);
2551 isl_union_map_free(umap);
2552 p = print_union_map_field(p, "must_no_source", flow->must_no_source);
2553 umap = isl_union_flow_get_may_no_source(flow);
2554 p = print_union_map_field(p, "may_no_source", umap);
2555 isl_union_map_free(umap);
2556 p = isl_printer_yaml_end_mapping(p);
2558 return p;
2561 /* Return a string representation of the information in "flow".
2562 * The information is printed in flow format.
2564 __isl_give char *isl_union_flow_to_str(__isl_keep isl_union_flow *flow)
2566 isl_printer *p;
2567 char *s;
2569 if (!flow)
2570 return NULL;
2572 p = isl_printer_to_str(isl_union_flow_get_ctx(flow));
2573 p = isl_printer_set_yaml_style(p, ISL_YAML_STYLE_FLOW);
2574 p = isl_printer_print_union_flow(p, flow);
2575 s = isl_printer_get_str(p);
2576 isl_printer_free(p);
2578 return s;
2581 /* Given a collection of "sink" and "source" accesses,
2582 * compute for each iteration of a sink access
2583 * and for each element accessed by that iteration,
2584 * the source access in the list that last accessed the
2585 * element accessed by the sink access before this sink access.
2586 * Each access is given as a map from the loop iterators
2587 * to the array indices.
2588 * The result is a relations between source and sink
2589 * iterations and a subset of the domain of the sink accesses,
2590 * corresponding to those iterations that access an element
2591 * not previously accessed.
2593 * We collect the inputs in an isl_union_access_info object,
2594 * call isl_union_access_info_compute_flow and extract
2595 * the outputs from the result.
2597 int isl_union_map_compute_flow(__isl_take isl_union_map *sink,
2598 __isl_take isl_union_map *must_source,
2599 __isl_take isl_union_map *may_source,
2600 __isl_take isl_union_map *schedule,
2601 __isl_give isl_union_map **must_dep, __isl_give isl_union_map **may_dep,
2602 __isl_give isl_union_map **must_no_source,
2603 __isl_give isl_union_map **may_no_source)
2605 isl_union_access_info *access;
2606 isl_union_flow *flow;
2608 access = isl_union_access_info_from_sink(sink);
2609 access = isl_union_access_info_set_must_source(access, must_source);
2610 access = isl_union_access_info_set_may_source(access, may_source);
2611 access = isl_union_access_info_set_schedule_map(access, schedule);
2612 flow = isl_union_access_info_compute_flow(access);
2614 if (must_dep)
2615 *must_dep = isl_union_flow_get_must_dependence(flow);
2616 if (may_dep)
2617 *may_dep = isl_union_flow_get_non_must_dependence(flow);
2618 if (must_no_source)
2619 *must_no_source = isl_union_flow_get_must_no_source(flow);
2620 if (may_no_source)
2621 *may_no_source = isl_union_flow_get_non_must_no_source(flow);
2623 isl_union_flow_free(flow);
2625 if ((must_dep && !*must_dep) || (may_dep && !*may_dep) ||
2626 (must_no_source && !*must_no_source) ||
2627 (may_no_source && !*may_no_source))
2628 goto error;
2630 return 0;
2631 error:
2632 if (must_dep)
2633 *must_dep = isl_union_map_free(*must_dep);
2634 if (may_dep)
2635 *may_dep = isl_union_map_free(*may_dep);
2636 if (must_no_source)
2637 *must_no_source = isl_union_map_free(*must_no_source);
2638 if (may_no_source)
2639 *may_no_source = isl_union_map_free(*may_no_source);
2640 return -1;