isl_flow.c: merge isl_union_access_info_set_*
[isl.git] / isl_flow.c
blob7f2123dd913bb4ff3cfc1b88a2a96f4c2392e13d
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 isl_bool equal_params;
367 equal_params = isl_space_has_equal_params(left, right);
368 if (equal_params < 0)
369 goto error;
370 if (equal_params)
371 return isl_space_join(left, right);
373 left = isl_space_align_params(left, isl_space_copy(right));
374 right = isl_space_align_params(right, isl_space_copy(left));
375 return isl_space_join(left, right);
376 error:
377 isl_space_free(left);
378 isl_space_free(right);
379 return NULL;
382 /* Initialize an empty isl_flow structure corresponding to a given
383 * isl_access_info structure.
384 * For each must access, two dependences are created (initialized
385 * to the empty relation), one for the resulting must dependences
386 * and one for the resulting may dependences. May accesses can
387 * only lead to may dependences, so only one dependence is created
388 * for each of them.
389 * This function is private as isl_flow structures are only supposed
390 * to be created by isl_access_info_compute_flow.
392 static __isl_give isl_flow *isl_flow_alloc(__isl_keep isl_access_info *acc)
394 int i, n;
395 struct isl_ctx *ctx;
396 struct isl_flow *dep;
398 if (!acc)
399 return NULL;
401 ctx = isl_map_get_ctx(acc->sink.map);
402 dep = isl_calloc_type(ctx, struct isl_flow);
403 if (!dep)
404 return NULL;
406 n = 2 * acc->n_must + acc->n_may;
407 dep->dep = isl_calloc_array(ctx, struct isl_labeled_map, n);
408 if (n && !dep->dep)
409 goto error;
411 dep->n_source = n;
412 for (i = 0; i < acc->n_must; ++i) {
413 isl_space *dim;
414 dim = space_align_and_join(
415 isl_map_get_space(acc->source[i].map),
416 isl_space_reverse(isl_map_get_space(acc->sink.map)));
417 dep->dep[2 * i].map = isl_map_empty(dim);
418 dep->dep[2 * i + 1].map = isl_map_copy(dep->dep[2 * i].map);
419 dep->dep[2 * i].data = acc->source[i].data;
420 dep->dep[2 * i + 1].data = acc->source[i].data;
421 dep->dep[2 * i].must = 1;
422 dep->dep[2 * i + 1].must = 0;
423 if (!dep->dep[2 * i].map || !dep->dep[2 * i + 1].map)
424 goto error;
426 for (i = acc->n_must; i < acc->n_must + acc->n_may; ++i) {
427 isl_space *dim;
428 dim = space_align_and_join(
429 isl_map_get_space(acc->source[i].map),
430 isl_space_reverse(isl_map_get_space(acc->sink.map)));
431 dep->dep[acc->n_must + i].map = isl_map_empty(dim);
432 dep->dep[acc->n_must + i].data = acc->source[i].data;
433 dep->dep[acc->n_must + i].must = 0;
434 if (!dep->dep[acc->n_must + i].map)
435 goto error;
438 return dep;
439 error:
440 isl_flow_free(dep);
441 return NULL;
444 /* Iterate over all sources and for each resulting flow dependence
445 * that is not empty, call the user specfied function.
446 * The second argument in this function call identifies the source,
447 * while the third argument correspond to the final argument of
448 * the isl_flow_foreach call.
450 isl_stat isl_flow_foreach(__isl_keep isl_flow *deps,
451 isl_stat (*fn)(__isl_take isl_map *dep, int must, void *dep_user,
452 void *user),
453 void *user)
455 int i;
457 if (!deps)
458 return isl_stat_error;
460 for (i = 0; i < deps->n_source; ++i) {
461 if (isl_map_plain_is_empty(deps->dep[i].map))
462 continue;
463 if (fn(isl_map_copy(deps->dep[i].map), deps->dep[i].must,
464 deps->dep[i].data, user) < 0)
465 return isl_stat_error;
468 return isl_stat_ok;
471 /* Return a copy of the subset of the sink for which no source could be found.
473 __isl_give isl_map *isl_flow_get_no_source(__isl_keep isl_flow *deps, int must)
475 if (!deps)
476 return NULL;
478 if (must)
479 return isl_set_unwrap(isl_set_copy(deps->must_no_source));
480 else
481 return isl_set_unwrap(isl_set_copy(deps->may_no_source));
484 void isl_flow_free(__isl_take isl_flow *deps)
486 int i;
488 if (!deps)
489 return;
490 isl_set_free(deps->must_no_source);
491 isl_set_free(deps->may_no_source);
492 if (deps->dep) {
493 for (i = 0; i < deps->n_source; ++i)
494 isl_map_free(deps->dep[i].map);
495 free(deps->dep);
497 free(deps);
500 isl_ctx *isl_flow_get_ctx(__isl_keep isl_flow *deps)
502 return deps ? isl_set_get_ctx(deps->must_no_source) : NULL;
505 /* Return a map that enforces that the domain iteration occurs after
506 * the range iteration at the given level.
507 * If level is odd, then the domain iteration should occur after
508 * the target iteration in their shared level/2 outermost loops.
509 * In this case we simply need to enforce that these outermost
510 * loop iterations are the same.
511 * If level is even, then the loop iterator of the domain should
512 * be greater than the loop iterator of the range at the last
513 * of the level/2 shared loops, i.e., loop level/2 - 1.
515 static __isl_give isl_map *after_at_level(__isl_take isl_space *dim, int level)
517 struct isl_basic_map *bmap;
519 if (level % 2)
520 bmap = isl_basic_map_equal(dim, level/2);
521 else
522 bmap = isl_basic_map_more_at(dim, level/2 - 1);
524 return isl_map_from_basic_map(bmap);
527 /* Compute the partial lexicographic maximum of "dep" on domain "sink",
528 * but first check if the user has set acc->restrict_fn and if so
529 * update either the input or the output of the maximization problem
530 * with respect to the resulting restriction.
532 * Since the user expects a mapping from sink iterations to source iterations,
533 * whereas the domain of "dep" is a wrapped map, mapping sink iterations
534 * to accessed array elements, we first need to project out the accessed
535 * sink array elements by applying acc->domain_map.
536 * Similarly, the sink restriction specified by the user needs to be
537 * converted back to the wrapped map.
539 static __isl_give isl_map *restricted_partial_lexmax(
540 __isl_keep isl_access_info *acc, __isl_take isl_map *dep,
541 int source, __isl_take isl_set *sink, __isl_give isl_set **empty)
543 isl_map *source_map;
544 isl_restriction *restr;
545 isl_set *sink_domain;
546 isl_set *sink_restr;
547 isl_map *res;
549 if (!acc->restrict_fn)
550 return isl_map_partial_lexmax(dep, sink, empty);
552 source_map = isl_map_copy(dep);
553 source_map = isl_map_apply_domain(source_map,
554 isl_map_copy(acc->domain_map));
555 sink_domain = isl_set_copy(sink);
556 sink_domain = isl_set_apply(sink_domain, isl_map_copy(acc->domain_map));
557 restr = acc->restrict_fn(source_map, sink_domain,
558 acc->source[source].data, acc->restrict_user);
559 isl_set_free(sink_domain);
560 isl_map_free(source_map);
562 if (!restr)
563 goto error;
564 if (restr->type == isl_restriction_type_input) {
565 dep = isl_map_intersect_range(dep, isl_set_copy(restr->source));
566 sink_restr = isl_set_copy(restr->sink);
567 sink_restr = isl_set_apply(sink_restr,
568 isl_map_reverse(isl_map_copy(acc->domain_map)));
569 sink = isl_set_intersect(sink, sink_restr);
570 } else if (restr->type == isl_restriction_type_empty) {
571 isl_space *space = isl_map_get_space(dep);
572 isl_map_free(dep);
573 dep = isl_map_empty(space);
576 res = isl_map_partial_lexmax(dep, sink, empty);
578 if (restr->type == isl_restriction_type_output)
579 res = isl_map_intersect_range(res, isl_set_copy(restr->source));
581 isl_restriction_free(restr);
582 return res;
583 error:
584 isl_map_free(dep);
585 isl_set_free(sink);
586 *empty = NULL;
587 return NULL;
590 /* Compute the last iteration of must source j that precedes the sink
591 * at the given level for sink iterations in set_C.
592 * The subset of set_C for which no such iteration can be found is returned
593 * in *empty.
595 static struct isl_map *last_source(struct isl_access_info *acc,
596 struct isl_set *set_C,
597 int j, int level, struct isl_set **empty)
599 struct isl_map *read_map;
600 struct isl_map *write_map;
601 struct isl_map *dep_map;
602 struct isl_map *after;
603 struct isl_map *result;
605 read_map = isl_map_copy(acc->sink.map);
606 write_map = isl_map_copy(acc->source[j].map);
607 write_map = isl_map_reverse(write_map);
608 dep_map = isl_map_apply_range(read_map, write_map);
609 after = after_at_level(isl_map_get_space(dep_map), level);
610 dep_map = isl_map_intersect(dep_map, after);
611 result = restricted_partial_lexmax(acc, dep_map, j, set_C, empty);
612 result = isl_map_reverse(result);
614 return result;
617 /* For a given mapping between iterations of must source j and iterations
618 * of the sink, compute the last iteration of must source k preceding
619 * the sink at level before_level for any of the sink iterations,
620 * but following the corresponding iteration of must source j at level
621 * after_level.
623 static struct isl_map *last_later_source(struct isl_access_info *acc,
624 struct isl_map *old_map,
625 int j, int before_level,
626 int k, int after_level,
627 struct isl_set **empty)
629 isl_space *dim;
630 struct isl_set *set_C;
631 struct isl_map *read_map;
632 struct isl_map *write_map;
633 struct isl_map *dep_map;
634 struct isl_map *after_write;
635 struct isl_map *before_read;
636 struct isl_map *result;
638 set_C = isl_map_range(isl_map_copy(old_map));
639 read_map = isl_map_copy(acc->sink.map);
640 write_map = isl_map_copy(acc->source[k].map);
642 write_map = isl_map_reverse(write_map);
643 dep_map = isl_map_apply_range(read_map, write_map);
644 dim = space_align_and_join(isl_map_get_space(acc->source[k].map),
645 isl_space_reverse(isl_map_get_space(acc->source[j].map)));
646 after_write = after_at_level(dim, after_level);
647 after_write = isl_map_apply_range(after_write, old_map);
648 after_write = isl_map_reverse(after_write);
649 dep_map = isl_map_intersect(dep_map, after_write);
650 before_read = after_at_level(isl_map_get_space(dep_map), before_level);
651 dep_map = isl_map_intersect(dep_map, before_read);
652 result = restricted_partial_lexmax(acc, dep_map, k, set_C, empty);
653 result = isl_map_reverse(result);
655 return result;
658 /* Given a shared_level between two accesses, return 1 if the
659 * the first can precede the second at the requested target_level.
660 * If the target level is odd, i.e., refers to a statement level
661 * dimension, then first needs to precede second at the requested
662 * level, i.e., shared_level must be equal to target_level.
663 * If the target level is odd, then the two loops should share
664 * at least the requested number of outer loops.
666 static int can_precede_at_level(int shared_level, int target_level)
668 if (shared_level < target_level)
669 return 0;
670 if ((target_level % 2) && shared_level > target_level)
671 return 0;
672 return 1;
675 /* Given a possible flow dependence temp_rel[j] between source j and the sink
676 * at level sink_level, remove those elements for which
677 * there is an iteration of another source k < j that is closer to the sink.
678 * The flow dependences temp_rel[k] are updated with the improved sources.
679 * Any improved source needs to precede the sink at the same level
680 * and needs to follow source j at the same or a deeper level.
681 * The lower this level, the later the execution date of source k.
682 * We therefore consider lower levels first.
684 * If temp_rel[j] is empty, then there can be no improvement and
685 * we return immediately.
687 static int intermediate_sources(__isl_keep isl_access_info *acc,
688 struct isl_map **temp_rel, int j, int sink_level)
690 int k, level;
691 int depth = 2 * isl_map_dim(acc->source[j].map, isl_dim_in) + 1;
693 if (isl_map_plain_is_empty(temp_rel[j]))
694 return 0;
696 for (k = j - 1; k >= 0; --k) {
697 int plevel, plevel2;
698 plevel = acc->level_before(acc->source[k].data, acc->sink.data);
699 if (!can_precede_at_level(plevel, sink_level))
700 continue;
702 plevel2 = acc->level_before(acc->source[j].data,
703 acc->source[k].data);
705 for (level = sink_level; level <= depth; ++level) {
706 struct isl_map *T;
707 struct isl_set *trest;
708 struct isl_map *copy;
710 if (!can_precede_at_level(plevel2, level))
711 continue;
713 copy = isl_map_copy(temp_rel[j]);
714 T = last_later_source(acc, copy, j, sink_level, k,
715 level, &trest);
716 if (isl_map_plain_is_empty(T)) {
717 isl_set_free(trest);
718 isl_map_free(T);
719 continue;
721 temp_rel[j] = isl_map_intersect_range(temp_rel[j], trest);
722 temp_rel[k] = isl_map_union_disjoint(temp_rel[k], T);
726 return 0;
729 /* Compute all iterations of may source j that precedes the sink at the given
730 * level for sink iterations in set_C.
732 static __isl_give isl_map *all_sources(__isl_keep isl_access_info *acc,
733 __isl_take isl_set *set_C, int j, int level)
735 isl_map *read_map;
736 isl_map *write_map;
737 isl_map *dep_map;
738 isl_map *after;
740 read_map = isl_map_copy(acc->sink.map);
741 read_map = isl_map_intersect_domain(read_map, set_C);
742 write_map = isl_map_copy(acc->source[acc->n_must + j].map);
743 write_map = isl_map_reverse(write_map);
744 dep_map = isl_map_apply_range(read_map, write_map);
745 after = after_at_level(isl_map_get_space(dep_map), level);
746 dep_map = isl_map_intersect(dep_map, after);
748 return isl_map_reverse(dep_map);
751 /* For a given mapping between iterations of must source k and iterations
752 * of the sink, compute all iterations of may source j preceding
753 * the sink at level before_level for any of the sink iterations,
754 * but following the corresponding iteration of must source k at level
755 * after_level.
757 static __isl_give isl_map *all_later_sources(__isl_keep isl_access_info *acc,
758 __isl_take isl_map *old_map,
759 int j, int before_level, int k, int after_level)
761 isl_space *dim;
762 isl_set *set_C;
763 isl_map *read_map;
764 isl_map *write_map;
765 isl_map *dep_map;
766 isl_map *after_write;
767 isl_map *before_read;
769 set_C = isl_map_range(isl_map_copy(old_map));
770 read_map = isl_map_copy(acc->sink.map);
771 read_map = isl_map_intersect_domain(read_map, set_C);
772 write_map = isl_map_copy(acc->source[acc->n_must + j].map);
774 write_map = isl_map_reverse(write_map);
775 dep_map = isl_map_apply_range(read_map, write_map);
776 dim = isl_space_join(isl_map_get_space(acc->source[acc->n_must + j].map),
777 isl_space_reverse(isl_map_get_space(acc->source[k].map)));
778 after_write = after_at_level(dim, after_level);
779 after_write = isl_map_apply_range(after_write, old_map);
780 after_write = isl_map_reverse(after_write);
781 dep_map = isl_map_intersect(dep_map, after_write);
782 before_read = after_at_level(isl_map_get_space(dep_map), before_level);
783 dep_map = isl_map_intersect(dep_map, before_read);
784 return isl_map_reverse(dep_map);
787 /* Given the must and may dependence relations for the must accesses
788 * for level sink_level, check if there are any accesses of may access j
789 * that occur in between and return their union.
790 * If some of these accesses are intermediate with respect to
791 * (previously thought to be) must dependences, then these
792 * must dependences are turned into may dependences.
794 static __isl_give isl_map *all_intermediate_sources(
795 __isl_keep isl_access_info *acc, __isl_take isl_map *map,
796 struct isl_map **must_rel, struct isl_map **may_rel,
797 int j, int sink_level)
799 int k, level;
800 int depth = 2 * isl_map_dim(acc->source[acc->n_must + j].map,
801 isl_dim_in) + 1;
803 for (k = 0; k < acc->n_must; ++k) {
804 int plevel;
806 if (isl_map_plain_is_empty(may_rel[k]) &&
807 isl_map_plain_is_empty(must_rel[k]))
808 continue;
810 plevel = acc->level_before(acc->source[k].data,
811 acc->source[acc->n_must + j].data);
813 for (level = sink_level; level <= depth; ++level) {
814 isl_map *T;
815 isl_map *copy;
816 isl_set *ran;
818 if (!can_precede_at_level(plevel, level))
819 continue;
821 copy = isl_map_copy(may_rel[k]);
822 T = all_later_sources(acc, copy, j, sink_level, k, level);
823 map = isl_map_union(map, T);
825 copy = isl_map_copy(must_rel[k]);
826 T = all_later_sources(acc, copy, j, sink_level, k, level);
827 ran = isl_map_range(isl_map_copy(T));
828 map = isl_map_union(map, T);
829 may_rel[k] = isl_map_union_disjoint(may_rel[k],
830 isl_map_intersect_range(isl_map_copy(must_rel[k]),
831 isl_set_copy(ran)));
832 T = isl_map_from_domain_and_range(
833 isl_set_universe(
834 isl_space_domain(isl_map_get_space(must_rel[k]))),
835 ran);
836 must_rel[k] = isl_map_subtract(must_rel[k], T);
840 return map;
843 /* Compute dependences for the case where all accesses are "may"
844 * accesses, which boils down to computing memory based dependences.
845 * The generic algorithm would also work in this case, but it would
846 * be overkill to use it.
848 static __isl_give isl_flow *compute_mem_based_dependences(
849 __isl_keep isl_access_info *acc)
851 int i;
852 isl_set *mustdo;
853 isl_set *maydo;
854 isl_flow *res;
856 res = isl_flow_alloc(acc);
857 if (!res)
858 return NULL;
860 mustdo = isl_map_domain(isl_map_copy(acc->sink.map));
861 maydo = isl_set_copy(mustdo);
863 for (i = 0; i < acc->n_may; ++i) {
864 int plevel;
865 int is_before;
866 isl_space *dim;
867 isl_map *before;
868 isl_map *dep;
870 plevel = acc->level_before(acc->source[i].data, acc->sink.data);
871 is_before = plevel & 1;
872 plevel >>= 1;
874 dim = isl_map_get_space(res->dep[i].map);
875 if (is_before)
876 before = isl_map_lex_le_first(dim, plevel);
877 else
878 before = isl_map_lex_lt_first(dim, plevel);
879 dep = isl_map_apply_range(isl_map_copy(acc->source[i].map),
880 isl_map_reverse(isl_map_copy(acc->sink.map)));
881 dep = isl_map_intersect(dep, before);
882 mustdo = isl_set_subtract(mustdo,
883 isl_map_range(isl_map_copy(dep)));
884 res->dep[i].map = isl_map_union(res->dep[i].map, dep);
887 res->may_no_source = isl_set_subtract(maydo, isl_set_copy(mustdo));
888 res->must_no_source = mustdo;
890 return res;
893 /* Compute dependences for the case where there is at least one
894 * "must" access.
896 * The core algorithm considers all levels in which a source may precede
897 * the sink, where a level may either be a statement level or a loop level.
898 * The outermost statement level is 1, the first loop level is 2, etc...
899 * The algorithm basically does the following:
900 * for all levels l of the read access from innermost to outermost
901 * for all sources w that may precede the sink access at that level
902 * compute the last iteration of the source that precedes the sink access
903 * at that level
904 * add result to possible last accesses at level l of source w
905 * for all sources w2 that we haven't considered yet at this level that may
906 * also precede the sink access
907 * for all levels l2 of w from l to innermost
908 * for all possible last accesses dep of w at l
909 * compute last iteration of w2 between the source and sink
910 * of dep
911 * add result to possible last accesses at level l of write w2
912 * and replace possible last accesses dep by the remainder
915 * The above algorithm is applied to the must access. During the course
916 * of the algorithm, we keep track of sink iterations that still
917 * need to be considered. These iterations are split into those that
918 * haven't been matched to any source access (mustdo) and those that have only
919 * been matched to may accesses (maydo).
920 * At the end of each level, we also consider the may accesses.
921 * In particular, we consider may accesses that precede the remaining
922 * sink iterations, moving elements from mustdo to maydo when appropriate,
923 * and may accesses that occur between a must source and a sink of any
924 * dependences found at the current level, turning must dependences into
925 * may dependences when appropriate.
928 static __isl_give isl_flow *compute_val_based_dependences(
929 __isl_keep isl_access_info *acc)
931 isl_ctx *ctx;
932 isl_flow *res;
933 isl_set *mustdo = NULL;
934 isl_set *maydo = NULL;
935 int level, j;
936 int depth;
937 isl_map **must_rel = NULL;
938 isl_map **may_rel = NULL;
940 if (!acc)
941 return NULL;
943 res = isl_flow_alloc(acc);
944 if (!res)
945 goto error;
946 ctx = isl_map_get_ctx(acc->sink.map);
948 depth = 2 * isl_map_dim(acc->sink.map, isl_dim_in) + 1;
949 mustdo = isl_map_domain(isl_map_copy(acc->sink.map));
950 maydo = isl_set_empty(isl_set_get_space(mustdo));
951 if (!mustdo || !maydo)
952 goto error;
953 if (isl_set_plain_is_empty(mustdo))
954 goto done;
956 must_rel = isl_alloc_array(ctx, struct isl_map *, acc->n_must);
957 may_rel = isl_alloc_array(ctx, struct isl_map *, acc->n_must);
958 if (!must_rel || !may_rel)
959 goto error;
961 for (level = depth; level >= 1; --level) {
962 for (j = acc->n_must-1; j >=0; --j) {
963 isl_space *space;
964 space = isl_map_get_space(res->dep[2 * j].map);
965 must_rel[j] = isl_map_empty(space);
966 may_rel[j] = isl_map_copy(must_rel[j]);
969 for (j = acc->n_must - 1; j >= 0; --j) {
970 struct isl_map *T;
971 struct isl_set *rest;
972 int plevel;
974 plevel = acc->level_before(acc->source[j].data,
975 acc->sink.data);
976 if (!can_precede_at_level(plevel, level))
977 continue;
979 T = last_source(acc, mustdo, j, level, &rest);
980 must_rel[j] = isl_map_union_disjoint(must_rel[j], T);
981 mustdo = rest;
983 intermediate_sources(acc, must_rel, j, level);
985 T = last_source(acc, maydo, j, level, &rest);
986 may_rel[j] = isl_map_union_disjoint(may_rel[j], T);
987 maydo = rest;
989 intermediate_sources(acc, may_rel, j, level);
991 if (isl_set_plain_is_empty(mustdo) &&
992 isl_set_plain_is_empty(maydo))
993 break;
995 for (j = j - 1; j >= 0; --j) {
996 int plevel;
998 plevel = acc->level_before(acc->source[j].data,
999 acc->sink.data);
1000 if (!can_precede_at_level(plevel, level))
1001 continue;
1003 intermediate_sources(acc, must_rel, j, level);
1004 intermediate_sources(acc, may_rel, j, level);
1007 for (j = 0; j < acc->n_may; ++j) {
1008 int plevel;
1009 isl_map *T;
1010 isl_set *ran;
1012 plevel = acc->level_before(acc->source[acc->n_must + j].data,
1013 acc->sink.data);
1014 if (!can_precede_at_level(plevel, level))
1015 continue;
1017 T = all_sources(acc, isl_set_copy(maydo), j, level);
1018 res->dep[2 * acc->n_must + j].map =
1019 isl_map_union(res->dep[2 * acc->n_must + j].map, T);
1020 T = all_sources(acc, isl_set_copy(mustdo), j, level);
1021 ran = isl_map_range(isl_map_copy(T));
1022 res->dep[2 * acc->n_must + j].map =
1023 isl_map_union(res->dep[2 * acc->n_must + j].map, T);
1024 mustdo = isl_set_subtract(mustdo, isl_set_copy(ran));
1025 maydo = isl_set_union_disjoint(maydo, ran);
1027 T = res->dep[2 * acc->n_must + j].map;
1028 T = all_intermediate_sources(acc, T, must_rel, may_rel,
1029 j, level);
1030 res->dep[2 * acc->n_must + j].map = T;
1033 for (j = acc->n_must - 1; j >= 0; --j) {
1034 res->dep[2 * j].map =
1035 isl_map_union_disjoint(res->dep[2 * j].map,
1036 must_rel[j]);
1037 res->dep[2 * j + 1].map =
1038 isl_map_union_disjoint(res->dep[2 * j + 1].map,
1039 may_rel[j]);
1042 if (isl_set_plain_is_empty(mustdo) &&
1043 isl_set_plain_is_empty(maydo))
1044 break;
1047 free(must_rel);
1048 free(may_rel);
1049 done:
1050 res->must_no_source = mustdo;
1051 res->may_no_source = maydo;
1052 return res;
1053 error:
1054 isl_flow_free(res);
1055 isl_set_free(mustdo);
1056 isl_set_free(maydo);
1057 free(must_rel);
1058 free(may_rel);
1059 return NULL;
1062 /* Given a "sink" access, a list of n "source" accesses,
1063 * compute for each iteration of the sink access
1064 * and for each element accessed by that iteration,
1065 * the source access in the list that last accessed the
1066 * element accessed by the sink access before this sink access.
1067 * Each access is given as a map from the loop iterators
1068 * to the array indices.
1069 * The result is a list of n relations between source and sink
1070 * iterations and a subset of the domain of the sink access,
1071 * corresponding to those iterations that access an element
1072 * not previously accessed.
1074 * To deal with multi-valued sink access relations, the sink iteration
1075 * domain is first extended with dimensions that correspond to the data
1076 * space. However, these extra dimensions are not projected out again.
1077 * It is up to the caller to decide whether these dimensions should be kept.
1079 static __isl_give isl_flow *access_info_compute_flow_core(
1080 __isl_take isl_access_info *acc)
1082 struct isl_flow *res = NULL;
1084 if (!acc)
1085 return NULL;
1087 acc->sink.map = isl_map_range_map(acc->sink.map);
1088 if (!acc->sink.map)
1089 goto error;
1091 if (acc->n_must == 0)
1092 res = compute_mem_based_dependences(acc);
1093 else {
1094 acc = isl_access_info_sort_sources(acc);
1095 res = compute_val_based_dependences(acc);
1097 acc = isl_access_info_free(acc);
1098 if (!res)
1099 return NULL;
1100 if (!res->must_no_source || !res->may_no_source)
1101 goto error;
1102 return res;
1103 error:
1104 isl_access_info_free(acc);
1105 isl_flow_free(res);
1106 return NULL;
1109 /* Given a "sink" access, a list of n "source" accesses,
1110 * compute for each iteration of the sink access
1111 * and for each element accessed by that iteration,
1112 * the source access in the list that last accessed the
1113 * element accessed by the sink access before this sink access.
1114 * Each access is given as a map from the loop iterators
1115 * to the array indices.
1116 * The result is a list of n relations between source and sink
1117 * iterations and a subset of the domain of the sink access,
1118 * corresponding to those iterations that access an element
1119 * not previously accessed.
1121 * To deal with multi-valued sink access relations,
1122 * access_info_compute_flow_core extends the sink iteration domain
1123 * with dimensions that correspond to the data space. These extra dimensions
1124 * are projected out from the result of access_info_compute_flow_core.
1126 __isl_give isl_flow *isl_access_info_compute_flow(__isl_take isl_access_info *acc)
1128 int j;
1129 struct isl_flow *res;
1131 if (!acc)
1132 return NULL;
1134 acc->domain_map = isl_map_domain_map(isl_map_copy(acc->sink.map));
1135 res = access_info_compute_flow_core(acc);
1136 if (!res)
1137 return NULL;
1139 for (j = 0; j < res->n_source; ++j) {
1140 res->dep[j].map = isl_map_range_factor_domain(res->dep[j].map);
1141 if (!res->dep[j].map)
1142 goto error;
1145 return res;
1146 error:
1147 isl_flow_free(res);
1148 return NULL;
1152 /* Keep track of some information about a schedule for a given
1153 * access. In particular, keep track of which dimensions
1154 * have a constant value and of the actual constant values.
1156 struct isl_sched_info {
1157 int *is_cst;
1158 isl_vec *cst;
1161 static void sched_info_free(__isl_take struct isl_sched_info *info)
1163 if (!info)
1164 return;
1165 isl_vec_free(info->cst);
1166 free(info->is_cst);
1167 free(info);
1170 /* Extract information on the constant dimensions of the schedule
1171 * for a given access. The "map" is of the form
1173 * [S -> D] -> A
1175 * with S the schedule domain, D the iteration domain and A the data domain.
1177 static __isl_give struct isl_sched_info *sched_info_alloc(
1178 __isl_keep isl_map *map)
1180 isl_ctx *ctx;
1181 isl_space *dim;
1182 struct isl_sched_info *info;
1183 int i, n;
1185 if (!map)
1186 return NULL;
1188 dim = isl_space_unwrap(isl_space_domain(isl_map_get_space(map)));
1189 if (!dim)
1190 return NULL;
1191 n = isl_space_dim(dim, isl_dim_in);
1192 isl_space_free(dim);
1194 ctx = isl_map_get_ctx(map);
1195 info = isl_alloc_type(ctx, struct isl_sched_info);
1196 if (!info)
1197 return NULL;
1198 info->is_cst = isl_alloc_array(ctx, int, n);
1199 info->cst = isl_vec_alloc(ctx, n);
1200 if (n && (!info->is_cst || !info->cst))
1201 goto error;
1203 for (i = 0; i < n; ++i) {
1204 isl_val *v;
1206 v = isl_map_plain_get_val_if_fixed(map, isl_dim_in, i);
1207 if (!v)
1208 goto error;
1209 info->is_cst[i] = !isl_val_is_nan(v);
1210 if (info->is_cst[i])
1211 info->cst = isl_vec_set_element_val(info->cst, i, v);
1212 else
1213 isl_val_free(v);
1216 return info;
1217 error:
1218 sched_info_free(info);
1219 return NULL;
1222 /* The different types of access relations that isl_union_access_info
1223 * keeps track of.
1225 * "isl_access_sink" represents the sink accesses.
1226 * "isl_access_must_source" represents the definite source accesses.
1227 * "isl_access_may_source" represents the possible source accesses.
1229 * isl_access_sink is sometimes treated differently and
1230 * should therefore appear first.
1232 enum isl_access_type {
1233 isl_access_sink,
1234 isl_access_must_source,
1235 isl_access_may_source,
1236 isl_access_end
1239 /* This structure represents the input for a dependence analysis computation.
1241 * "access" contains the access relations.
1243 * "schedule" or "schedule_map" represents the execution order.
1244 * Exactly one of these fields should be NULL. The other field
1245 * determines the execution order.
1247 * The domains of these four maps refer to the same iteration spaces(s).
1248 * The ranges of the first three maps also refer to the same data space(s).
1250 * After a call to isl_union_access_info_introduce_schedule,
1251 * the "schedule_map" field no longer contains useful information.
1253 struct isl_union_access_info {
1254 isl_union_map *access[isl_access_end];
1256 isl_schedule *schedule;
1257 isl_union_map *schedule_map;
1260 /* Free "access" and return NULL.
1262 __isl_null isl_union_access_info *isl_union_access_info_free(
1263 __isl_take isl_union_access_info *access)
1265 enum isl_access_type i;
1267 if (!access)
1268 return NULL;
1270 for (i = isl_access_sink; i < isl_access_end; ++i)
1271 isl_union_map_free(access->access[i]);
1272 isl_schedule_free(access->schedule);
1273 isl_union_map_free(access->schedule_map);
1274 free(access);
1276 return NULL;
1279 /* Return the isl_ctx to which "access" belongs.
1281 isl_ctx *isl_union_access_info_get_ctx(__isl_keep isl_union_access_info *access)
1283 if (!access)
1284 return NULL;
1285 return isl_union_map_get_ctx(access->access[isl_access_sink]);
1288 /* Create a new isl_union_access_info with the given sink accesses and
1289 * and no other accesses or schedule information.
1291 * By default, we use the schedule field of the isl_union_access_info,
1292 * but this may be overridden by a call
1293 * to isl_union_access_info_set_schedule_map.
1295 __isl_give isl_union_access_info *isl_union_access_info_from_sink(
1296 __isl_take isl_union_map *sink)
1298 isl_ctx *ctx;
1299 isl_space *space;
1300 isl_union_map *empty;
1301 isl_union_access_info *access;
1302 enum isl_access_type i;
1304 if (!sink)
1305 return NULL;
1306 ctx = isl_union_map_get_ctx(sink);
1307 access = isl_alloc_type(ctx, isl_union_access_info);
1308 if (!access)
1309 goto error;
1311 space = isl_union_map_get_space(sink);
1312 empty = isl_union_map_empty(isl_space_copy(space));
1313 access->access[isl_access_sink] = sink;
1314 for (i = isl_access_sink + 1; i < isl_access_end; ++i)
1315 access->access[i] = isl_union_map_copy(empty);
1316 isl_union_map_free(empty);
1317 access->schedule = isl_schedule_empty(space);
1318 access->schedule_map = NULL;
1320 for (i = isl_access_sink; i < isl_access_end; ++i)
1321 if (!access->access[i])
1322 return isl_union_access_info_free(access);
1323 if (!access->schedule)
1324 return isl_union_access_info_free(access);
1326 return access;
1327 error:
1328 isl_union_map_free(sink);
1329 return NULL;
1332 /* Replace the access relation of type "type" of "info" by "access".
1334 static __isl_give isl_union_access_info *isl_union_access_info_set(
1335 __isl_take isl_union_access_info *info,
1336 enum isl_access_type type, __isl_take isl_union_map *access)
1338 if (!info || !access)
1339 goto error;
1341 isl_union_map_free(info->access[type]);
1342 info->access[type] = access;
1344 return info;
1345 error:
1346 isl_union_access_info_free(info);
1347 isl_union_map_free(access);
1348 return NULL;
1351 /* Replace the definite source accesses of "access" by "must_source".
1353 __isl_give isl_union_access_info *isl_union_access_info_set_must_source(
1354 __isl_take isl_union_access_info *access,
1355 __isl_take isl_union_map *must_source)
1357 return isl_union_access_info_set(access, isl_access_must_source,
1358 must_source);
1361 /* Replace the possible source accesses of "access" by "may_source".
1363 __isl_give isl_union_access_info *isl_union_access_info_set_may_source(
1364 __isl_take isl_union_access_info *access,
1365 __isl_take isl_union_map *may_source)
1367 return isl_union_access_info_set(access, isl_access_may_source,
1368 may_source);
1371 /* Replace the schedule of "access" by "schedule".
1372 * Also free the schedule_map in case it was set last.
1374 __isl_give isl_union_access_info *isl_union_access_info_set_schedule(
1375 __isl_take isl_union_access_info *access,
1376 __isl_take isl_schedule *schedule)
1378 if (!access || !schedule)
1379 goto error;
1381 access->schedule_map = isl_union_map_free(access->schedule_map);
1382 isl_schedule_free(access->schedule);
1383 access->schedule = schedule;
1385 return access;
1386 error:
1387 isl_union_access_info_free(access);
1388 isl_schedule_free(schedule);
1389 return NULL;
1392 /* Replace the schedule map of "access" by "schedule_map".
1393 * Also free the schedule in case it was set last.
1395 __isl_give isl_union_access_info *isl_union_access_info_set_schedule_map(
1396 __isl_take isl_union_access_info *access,
1397 __isl_take isl_union_map *schedule_map)
1399 if (!access || !schedule_map)
1400 goto error;
1402 isl_union_map_free(access->schedule_map);
1403 access->schedule = isl_schedule_free(access->schedule);
1404 access->schedule_map = schedule_map;
1406 return access;
1407 error:
1408 isl_union_access_info_free(access);
1409 isl_union_map_free(schedule_map);
1410 return NULL;
1413 __isl_give isl_union_access_info *isl_union_access_info_copy(
1414 __isl_keep isl_union_access_info *access)
1416 isl_union_access_info *copy;
1417 enum isl_access_type i;
1419 if (!access)
1420 return NULL;
1421 copy = isl_union_access_info_from_sink(
1422 isl_union_map_copy(access->access[isl_access_sink]));
1423 for (i = isl_access_sink + 1; i < isl_access_end; ++i)
1424 copy = isl_union_access_info_set(copy, i,
1425 isl_union_map_copy(access->access[i]));
1426 if (access->schedule)
1427 copy = isl_union_access_info_set_schedule(copy,
1428 isl_schedule_copy(access->schedule));
1429 else
1430 copy = isl_union_access_info_set_schedule_map(copy,
1431 isl_union_map_copy(access->schedule_map));
1433 return copy;
1436 /* Print a key-value pair of a YAML mapping to "p",
1437 * with key "name" and value "umap".
1439 static __isl_give isl_printer *print_union_map_field(__isl_take isl_printer *p,
1440 const char *name, __isl_keep isl_union_map *umap)
1442 p = isl_printer_print_str(p, name);
1443 p = isl_printer_yaml_next(p);
1444 p = isl_printer_print_str(p, "\"");
1445 p = isl_printer_print_union_map(p, umap);
1446 p = isl_printer_print_str(p, "\"");
1447 p = isl_printer_yaml_next(p);
1449 return p;
1452 /* Print the information contained in "access" to "p".
1453 * The information is printed as a YAML document.
1455 __isl_give isl_printer *isl_printer_print_union_access_info(
1456 __isl_take isl_printer *p, __isl_keep isl_union_access_info *access)
1458 if (!access)
1459 return isl_printer_free(p);
1461 p = isl_printer_yaml_start_mapping(p);
1462 p = print_union_map_field(p, "sink", access->access[isl_access_sink]);
1463 p = print_union_map_field(p, "must_source",
1464 access->access[isl_access_must_source]);
1465 p = print_union_map_field(p, "may_source",
1466 access->access[isl_access_may_source]);
1467 if (access->schedule) {
1468 p = isl_printer_print_str(p, "schedule");
1469 p = isl_printer_yaml_next(p);
1470 p = isl_printer_print_schedule(p, access->schedule);
1471 p = isl_printer_yaml_next(p);
1472 } else {
1473 p = print_union_map_field(p, "schedule_map",
1474 access->schedule_map);
1476 p = isl_printer_yaml_end_mapping(p);
1478 return p;
1481 /* Return a string representation of the information in "access".
1482 * The information is printed in flow format.
1484 __isl_give char *isl_union_access_info_to_str(
1485 __isl_keep isl_union_access_info *access)
1487 isl_printer *p;
1488 char *s;
1490 if (!access)
1491 return NULL;
1493 p = isl_printer_to_str(isl_union_access_info_get_ctx(access));
1494 p = isl_printer_set_yaml_style(p, ISL_YAML_STYLE_FLOW);
1495 p = isl_printer_print_union_access_info(p, access);
1496 s = isl_printer_get_str(p);
1497 isl_printer_free(p);
1499 return s;
1502 /* Update the fields of "access" such that they all have the same parameters,
1503 * keeping in mind that the schedule_map field may be NULL and ignoring
1504 * the schedule field.
1506 static __isl_give isl_union_access_info *isl_union_access_info_align_params(
1507 __isl_take isl_union_access_info *access)
1509 isl_space *space;
1510 enum isl_access_type i;
1512 if (!access)
1513 return NULL;
1515 space = isl_union_map_get_space(access->access[isl_access_sink]);
1516 for (i = isl_access_sink + 1; i < isl_access_end; ++i)
1517 space = isl_space_align_params(space,
1518 isl_union_map_get_space(access->access[i]));
1519 if (access->schedule_map)
1520 space = isl_space_align_params(space,
1521 isl_union_map_get_space(access->schedule_map));
1522 for (i = isl_access_sink; i < isl_access_end; ++i)
1523 access->access[i] =
1524 isl_union_map_align_params(access->access[i],
1525 isl_space_copy(space));
1526 if (!access->schedule_map) {
1527 isl_space_free(space);
1528 } else {
1529 access->schedule_map =
1530 isl_union_map_align_params(access->schedule_map, space);
1531 if (!access->schedule_map)
1532 return isl_union_access_info_free(access);
1535 for (i = isl_access_sink; i < isl_access_end; ++i)
1536 if (!access->access[i])
1537 return isl_union_access_info_free(access);
1539 return access;
1542 /* Prepend the schedule dimensions to the iteration domains.
1544 * That is, if the schedule is of the form
1546 * D -> S
1548 * while the access relations are of the form
1550 * D -> A
1552 * then the updated access relations are of the form
1554 * [S -> D] -> A
1556 * The schedule map is also replaced by the map
1558 * [S -> D] -> D
1560 * that is used during the internal computation.
1561 * Neither the original schedule map nor this updated schedule map
1562 * are used after the call to this function.
1564 static __isl_give isl_union_access_info *
1565 isl_union_access_info_introduce_schedule(
1566 __isl_take isl_union_access_info *access)
1568 isl_union_map *sm;
1569 enum isl_access_type i;
1571 if (!access)
1572 return NULL;
1574 sm = isl_union_map_reverse(access->schedule_map);
1575 sm = isl_union_map_range_map(sm);
1576 for (i = isl_access_sink; i < isl_access_end; ++i)
1577 access->access[i] =
1578 isl_union_map_apply_range(isl_union_map_copy(sm),
1579 access->access[i]);
1580 access->schedule_map = sm;
1582 for (i = isl_access_sink; i < isl_access_end; ++i)
1583 if (!access->access[i])
1584 return isl_union_access_info_free(access);
1585 if (!access->schedule_map)
1586 return isl_union_access_info_free(access);
1588 return access;
1591 /* This structure represents the result of a dependence analysis computation.
1593 * "must_dep" represents the full definite dependences
1594 * "may_dep" represents the full non-definite dependences.
1595 * Both are of the form
1597 * [Source] -> [[Sink -> Data]]
1599 * (after the schedule dimensions have been projected out).
1600 * "must_no_source" represents the subset of the sink accesses for which
1601 * definitely no source was found.
1602 * "may_no_source" represents the subset of the sink accesses for which
1603 * possibly, but not definitely, no source was found.
1605 struct isl_union_flow {
1606 isl_union_map *must_dep;
1607 isl_union_map *may_dep;
1608 isl_union_map *must_no_source;
1609 isl_union_map *may_no_source;
1612 /* Return the isl_ctx to which "flow" belongs.
1614 isl_ctx *isl_union_flow_get_ctx(__isl_keep isl_union_flow *flow)
1616 return flow ? isl_union_map_get_ctx(flow->must_dep) : NULL;
1619 /* Free "flow" and return NULL.
1621 __isl_null isl_union_flow *isl_union_flow_free(__isl_take isl_union_flow *flow)
1623 if (!flow)
1624 return NULL;
1625 isl_union_map_free(flow->must_dep);
1626 isl_union_map_free(flow->may_dep);
1627 isl_union_map_free(flow->must_no_source);
1628 isl_union_map_free(flow->may_no_source);
1629 free(flow);
1630 return NULL;
1633 void isl_union_flow_dump(__isl_keep isl_union_flow *flow)
1635 if (!flow)
1636 return;
1638 fprintf(stderr, "must dependences: ");
1639 isl_union_map_dump(flow->must_dep);
1640 fprintf(stderr, "may dependences: ");
1641 isl_union_map_dump(flow->may_dep);
1642 fprintf(stderr, "must no source: ");
1643 isl_union_map_dump(flow->must_no_source);
1644 fprintf(stderr, "may no source: ");
1645 isl_union_map_dump(flow->may_no_source);
1648 /* Return the full definite dependences in "flow", with accessed elements.
1650 __isl_give isl_union_map *isl_union_flow_get_full_must_dependence(
1651 __isl_keep isl_union_flow *flow)
1653 if (!flow)
1654 return NULL;
1655 return isl_union_map_copy(flow->must_dep);
1658 /* Return the full possible dependences in "flow", including the definite
1659 * dependences, with accessed elements.
1661 __isl_give isl_union_map *isl_union_flow_get_full_may_dependence(
1662 __isl_keep isl_union_flow *flow)
1664 if (!flow)
1665 return NULL;
1666 return isl_union_map_union(isl_union_map_copy(flow->must_dep),
1667 isl_union_map_copy(flow->may_dep));
1670 /* Return the definite dependences in "flow", without the accessed elements.
1672 __isl_give isl_union_map *isl_union_flow_get_must_dependence(
1673 __isl_keep isl_union_flow *flow)
1675 isl_union_map *dep;
1677 if (!flow)
1678 return NULL;
1679 dep = isl_union_map_copy(flow->must_dep);
1680 return isl_union_map_range_factor_domain(dep);
1683 /* Return the possible dependences in "flow", including the definite
1684 * dependences, without the accessed elements.
1686 __isl_give isl_union_map *isl_union_flow_get_may_dependence(
1687 __isl_keep isl_union_flow *flow)
1689 isl_union_map *dep;
1691 if (!flow)
1692 return NULL;
1693 dep = isl_union_map_union(isl_union_map_copy(flow->must_dep),
1694 isl_union_map_copy(flow->may_dep));
1695 return isl_union_map_range_factor_domain(dep);
1698 /* Return the non-definite dependences in "flow".
1700 static __isl_give isl_union_map *isl_union_flow_get_non_must_dependence(
1701 __isl_keep isl_union_flow *flow)
1703 if (!flow)
1704 return NULL;
1705 return isl_union_map_copy(flow->may_dep);
1708 /* Return the subset of the sink accesses for which definitely
1709 * no source was found.
1711 __isl_give isl_union_map *isl_union_flow_get_must_no_source(
1712 __isl_keep isl_union_flow *flow)
1714 if (!flow)
1715 return NULL;
1716 return isl_union_map_copy(flow->must_no_source);
1719 /* Return the subset of the sink accesses for which possibly
1720 * no source was found, including those for which definitely
1721 * no source was found.
1723 __isl_give isl_union_map *isl_union_flow_get_may_no_source(
1724 __isl_keep isl_union_flow *flow)
1726 if (!flow)
1727 return NULL;
1728 return isl_union_map_union(isl_union_map_copy(flow->must_no_source),
1729 isl_union_map_copy(flow->may_no_source));
1732 /* Return the subset of the sink accesses for which possibly, but not
1733 * definitely, no source was found.
1735 static __isl_give isl_union_map *isl_union_flow_get_non_must_no_source(
1736 __isl_keep isl_union_flow *flow)
1738 if (!flow)
1739 return NULL;
1740 return isl_union_map_copy(flow->may_no_source);
1743 /* Create a new isl_union_flow object, initialized with empty
1744 * dependence relations and sink subsets.
1746 static __isl_give isl_union_flow *isl_union_flow_alloc(
1747 __isl_take isl_space *space)
1749 isl_ctx *ctx;
1750 isl_union_map *empty;
1751 isl_union_flow *flow;
1753 if (!space)
1754 return NULL;
1755 ctx = isl_space_get_ctx(space);
1756 flow = isl_alloc_type(ctx, isl_union_flow);
1757 if (!flow)
1758 goto error;
1760 empty = isl_union_map_empty(space);
1761 flow->must_dep = isl_union_map_copy(empty);
1762 flow->may_dep = isl_union_map_copy(empty);
1763 flow->must_no_source = isl_union_map_copy(empty);
1764 flow->may_no_source = empty;
1766 if (!flow->must_dep || !flow->may_dep ||
1767 !flow->must_no_source || !flow->may_no_source)
1768 return isl_union_flow_free(flow);
1770 return flow;
1771 error:
1772 isl_space_free(space);
1773 return NULL;
1776 /* Copy this isl_union_flow object.
1778 __isl_give isl_union_flow *isl_union_flow_copy(__isl_keep isl_union_flow *flow)
1780 isl_union_flow *copy;
1782 if (!flow)
1783 return NULL;
1785 copy = isl_union_flow_alloc(isl_union_map_get_space(flow->must_dep));
1787 if (!copy)
1788 return NULL;
1790 copy->must_dep = isl_union_map_union(copy->must_dep,
1791 isl_union_map_copy(flow->must_dep));
1792 copy->may_dep = isl_union_map_union(copy->may_dep,
1793 isl_union_map_copy(flow->may_dep));
1794 copy->must_no_source = isl_union_map_union(copy->must_no_source,
1795 isl_union_map_copy(flow->must_no_source));
1796 copy->may_no_source = isl_union_map_union(copy->may_no_source,
1797 isl_union_map_copy(flow->may_no_source));
1799 if (!copy->must_dep || !copy->may_dep ||
1800 !copy->must_no_source || !copy->may_no_source)
1801 return isl_union_flow_free(copy);
1803 return copy;
1806 /* Drop the schedule dimensions from the iteration domains in "flow".
1807 * In particular, the schedule dimensions have been prepended
1808 * to the iteration domains prior to the dependence analysis by
1809 * replacing the iteration domain D, by the wrapped map [S -> D].
1810 * Replace these wrapped maps by the original D.
1812 * In particular, the dependences computed by access_info_compute_flow_core
1813 * are of the form
1815 * [S -> D] -> [[S' -> D'] -> A]
1817 * The schedule dimensions are projected out by first currying the range,
1818 * resulting in
1820 * [S -> D] -> [S' -> [D' -> A]]
1822 * and then computing the factor range
1824 * D -> [D' -> A]
1826 static __isl_give isl_union_flow *isl_union_flow_drop_schedule(
1827 __isl_take isl_union_flow *flow)
1829 if (!flow)
1830 return NULL;
1832 flow->must_dep = isl_union_map_range_curry(flow->must_dep);
1833 flow->must_dep = isl_union_map_factor_range(flow->must_dep);
1834 flow->may_dep = isl_union_map_range_curry(flow->may_dep);
1835 flow->may_dep = isl_union_map_factor_range(flow->may_dep);
1836 flow->must_no_source =
1837 isl_union_map_domain_factor_range(flow->must_no_source);
1838 flow->may_no_source =
1839 isl_union_map_domain_factor_range(flow->may_no_source);
1841 if (!flow->must_dep || !flow->may_dep ||
1842 !flow->must_no_source || !flow->may_no_source)
1843 return isl_union_flow_free(flow);
1845 return flow;
1848 struct isl_compute_flow_data {
1849 isl_union_map *must_source;
1850 isl_union_map *may_source;
1851 isl_union_flow *flow;
1853 int count;
1854 int must;
1855 isl_space *dim;
1856 struct isl_sched_info *sink_info;
1857 struct isl_sched_info **source_info;
1858 isl_access_info *accesses;
1861 static isl_stat count_matching_array(__isl_take isl_map *map, void *user)
1863 int eq;
1864 isl_space *dim;
1865 struct isl_compute_flow_data *data;
1867 data = (struct isl_compute_flow_data *)user;
1869 dim = isl_space_range(isl_map_get_space(map));
1871 eq = isl_space_is_equal(dim, data->dim);
1873 isl_space_free(dim);
1874 isl_map_free(map);
1876 if (eq < 0)
1877 return isl_stat_error;
1878 if (eq)
1879 data->count++;
1881 return isl_stat_ok;
1884 static isl_stat collect_matching_array(__isl_take isl_map *map, void *user)
1886 int eq;
1887 isl_space *dim;
1888 struct isl_sched_info *info;
1889 struct isl_compute_flow_data *data;
1891 data = (struct isl_compute_flow_data *)user;
1893 dim = isl_space_range(isl_map_get_space(map));
1895 eq = isl_space_is_equal(dim, data->dim);
1897 isl_space_free(dim);
1899 if (eq < 0)
1900 goto error;
1901 if (!eq) {
1902 isl_map_free(map);
1903 return isl_stat_ok;
1906 info = sched_info_alloc(map);
1907 data->source_info[data->count] = info;
1909 data->accesses = isl_access_info_add_source(data->accesses,
1910 map, data->must, info);
1912 data->count++;
1914 return isl_stat_ok;
1915 error:
1916 isl_map_free(map);
1917 return isl_stat_error;
1920 /* Determine the shared nesting level and the "textual order" of
1921 * the given accesses.
1923 * We first determine the minimal schedule dimension for both accesses.
1925 * If among those dimensions, we can find one where both have a fixed
1926 * value and if moreover those values are different, then the previous
1927 * dimension is the last shared nesting level and the textual order
1928 * is determined based on the order of the fixed values.
1929 * If no such fixed values can be found, then we set the shared
1930 * nesting level to the minimal schedule dimension, with no textual ordering.
1932 static int before(void *first, void *second)
1934 struct isl_sched_info *info1 = first;
1935 struct isl_sched_info *info2 = second;
1936 int n1, n2;
1937 int i;
1939 n1 = isl_vec_size(info1->cst);
1940 n2 = isl_vec_size(info2->cst);
1942 if (n2 < n1)
1943 n1 = n2;
1945 for (i = 0; i < n1; ++i) {
1946 int r;
1947 int cmp;
1949 if (!info1->is_cst[i])
1950 continue;
1951 if (!info2->is_cst[i])
1952 continue;
1953 cmp = isl_vec_cmp_element(info1->cst, info2->cst, i);
1954 if (cmp == 0)
1955 continue;
1957 r = 2 * i + (cmp < 0);
1959 return r;
1962 return 2 * n1;
1965 /* Given a sink access, look for all the source accesses that access
1966 * the same array and perform dataflow analysis on them using
1967 * isl_access_info_compute_flow_core.
1969 static isl_stat compute_flow(__isl_take isl_map *map, void *user)
1971 int i;
1972 isl_ctx *ctx;
1973 struct isl_compute_flow_data *data;
1974 isl_flow *flow;
1975 isl_union_flow *df;
1977 data = (struct isl_compute_flow_data *)user;
1978 df = data->flow;
1980 ctx = isl_map_get_ctx(map);
1982 data->accesses = NULL;
1983 data->sink_info = NULL;
1984 data->source_info = NULL;
1985 data->count = 0;
1986 data->dim = isl_space_range(isl_map_get_space(map));
1988 if (isl_union_map_foreach_map(data->must_source,
1989 &count_matching_array, data) < 0)
1990 goto error;
1991 if (isl_union_map_foreach_map(data->may_source,
1992 &count_matching_array, data) < 0)
1993 goto error;
1995 data->sink_info = sched_info_alloc(map);
1996 data->source_info = isl_calloc_array(ctx, struct isl_sched_info *,
1997 data->count);
1999 data->accesses = isl_access_info_alloc(isl_map_copy(map),
2000 data->sink_info, &before, data->count);
2001 if (!data->sink_info || (data->count && !data->source_info) ||
2002 !data->accesses)
2003 goto error;
2004 data->count = 0;
2005 data->must = 1;
2006 if (isl_union_map_foreach_map(data->must_source,
2007 &collect_matching_array, data) < 0)
2008 goto error;
2009 data->must = 0;
2010 if (isl_union_map_foreach_map(data->may_source,
2011 &collect_matching_array, data) < 0)
2012 goto error;
2014 flow = access_info_compute_flow_core(data->accesses);
2015 data->accesses = NULL;
2017 if (!flow)
2018 goto error;
2020 df->must_no_source = isl_union_map_union(df->must_no_source,
2021 isl_union_map_from_map(isl_flow_get_no_source(flow, 1)));
2022 df->may_no_source = isl_union_map_union(df->may_no_source,
2023 isl_union_map_from_map(isl_flow_get_no_source(flow, 0)));
2025 for (i = 0; i < flow->n_source; ++i) {
2026 isl_union_map *dep;
2027 dep = isl_union_map_from_map(isl_map_copy(flow->dep[i].map));
2028 if (flow->dep[i].must)
2029 df->must_dep = isl_union_map_union(df->must_dep, dep);
2030 else
2031 df->may_dep = isl_union_map_union(df->may_dep, dep);
2034 isl_flow_free(flow);
2036 sched_info_free(data->sink_info);
2037 if (data->source_info) {
2038 for (i = 0; i < data->count; ++i)
2039 sched_info_free(data->source_info[i]);
2040 free(data->source_info);
2042 isl_space_free(data->dim);
2043 isl_map_free(map);
2045 return isl_stat_ok;
2046 error:
2047 isl_access_info_free(data->accesses);
2048 sched_info_free(data->sink_info);
2049 if (data->source_info) {
2050 for (i = 0; i < data->count; ++i)
2051 sched_info_free(data->source_info[i]);
2052 free(data->source_info);
2054 isl_space_free(data->dim);
2055 isl_map_free(map);
2057 return isl_stat_error;
2060 /* Remove the must accesses from the may accesses.
2062 * A must access always trumps a may access, so there is no need
2063 * for a must access to also be considered as a may access. Doing so
2064 * would only cost extra computations only to find out that
2065 * the duplicated may access does not make any difference.
2067 static __isl_give isl_union_access_info *isl_union_access_info_normalize(
2068 __isl_take isl_union_access_info *access)
2070 if (!access)
2071 return NULL;
2072 access->access[isl_access_may_source] =
2073 isl_union_map_subtract(access->access[isl_access_may_source],
2074 isl_union_map_copy(access->access[isl_access_must_source]));
2075 if (!access->access[isl_access_may_source])
2076 return isl_union_access_info_free(access);
2078 return access;
2081 /* Given a description of the "sink" accesses, the "source" accesses and
2082 * a schedule, compute for each instance of a sink access
2083 * and for each element accessed by that instance,
2084 * the possible or definite source accesses that last accessed the
2085 * element accessed by the sink access before this sink access
2086 * in the sense that there is no intermediate definite source access.
2088 * The must_no_source and may_no_source elements of the result
2089 * are subsets of access->sink. The elements must_dep and may_dep
2090 * map domain elements of access->{may,must)_source to
2091 * domain elements of access->sink.
2093 * This function is used when only the schedule map representation
2094 * is available.
2096 * We first prepend the schedule dimensions to the domain
2097 * of the accesses so that we can easily compare their relative order.
2098 * Then we consider each sink access individually in compute_flow.
2100 static __isl_give isl_union_flow *compute_flow_union_map(
2101 __isl_take isl_union_access_info *access)
2103 struct isl_compute_flow_data data;
2104 isl_union_map *sink;
2106 access = isl_union_access_info_align_params(access);
2107 access = isl_union_access_info_introduce_schedule(access);
2108 if (!access)
2109 return NULL;
2111 data.must_source = access->access[isl_access_must_source];
2112 data.may_source = access->access[isl_access_may_source];
2114 sink = access->access[isl_access_sink];
2115 data.flow = isl_union_flow_alloc(isl_union_map_get_space(sink));
2117 if (isl_union_map_foreach_map(sink, &compute_flow, &data) < 0)
2118 goto error;
2120 data.flow = isl_union_flow_drop_schedule(data.flow);
2122 isl_union_access_info_free(access);
2123 return data.flow;
2124 error:
2125 isl_union_access_info_free(access);
2126 isl_union_flow_free(data.flow);
2127 return NULL;
2130 /* A schedule access relation.
2132 * The access relation "access" is of the form [S -> D] -> A,
2133 * where S corresponds to the prefix schedule at "node".
2134 * "must" is only relevant for source accesses and indicates
2135 * whether the access is a must source or a may source.
2137 struct isl_scheduled_access {
2138 isl_map *access;
2139 int must;
2140 isl_schedule_node *node;
2143 /* Data structure for keeping track of individual scheduled sink and source
2144 * accesses when computing dependence analysis based on a schedule tree.
2146 * "n_sink" is the number of used entries in "sink"
2147 * "n_source" is the number of used entries in "source"
2149 * "set_sink", "must" and "node" are only used inside collect_sink_source,
2150 * to keep track of the current node and
2151 * of what extract_sink_source needs to do.
2153 struct isl_compute_flow_schedule_data {
2154 isl_union_access_info *access;
2156 int n_sink;
2157 int n_source;
2159 struct isl_scheduled_access *sink;
2160 struct isl_scheduled_access *source;
2162 int set_sink;
2163 int must;
2164 isl_schedule_node *node;
2167 /* Align the parameters of all sinks with all sources.
2169 * If there are no sinks or no sources, then no alignment is needed.
2171 static void isl_compute_flow_schedule_data_align_params(
2172 struct isl_compute_flow_schedule_data *data)
2174 int i;
2175 isl_space *space;
2177 if (data->n_sink == 0 || data->n_source == 0)
2178 return;
2180 space = isl_map_get_space(data->sink[0].access);
2182 for (i = 1; i < data->n_sink; ++i)
2183 space = isl_space_align_params(space,
2184 isl_map_get_space(data->sink[i].access));
2185 for (i = 0; i < data->n_source; ++i)
2186 space = isl_space_align_params(space,
2187 isl_map_get_space(data->source[i].access));
2189 for (i = 0; i < data->n_sink; ++i)
2190 data->sink[i].access =
2191 isl_map_align_params(data->sink[i].access,
2192 isl_space_copy(space));
2193 for (i = 0; i < data->n_source; ++i)
2194 data->source[i].access =
2195 isl_map_align_params(data->source[i].access,
2196 isl_space_copy(space));
2198 isl_space_free(space);
2201 /* Free all the memory referenced from "data".
2202 * Do not free "data" itself as it may be allocated on the stack.
2204 static void isl_compute_flow_schedule_data_clear(
2205 struct isl_compute_flow_schedule_data *data)
2207 int i;
2209 if (!data->sink)
2210 return;
2212 for (i = 0; i < data->n_sink; ++i) {
2213 isl_map_free(data->sink[i].access);
2214 isl_schedule_node_free(data->sink[i].node);
2217 for (i = 0; i < data->n_source; ++i) {
2218 isl_map_free(data->source[i].access);
2219 isl_schedule_node_free(data->source[i].node);
2222 free(data->sink);
2225 /* isl_schedule_foreach_schedule_node_top_down callback for counting
2226 * (an upper bound on) the number of sinks and sources.
2228 * Sinks and sources are only extracted at leaves of the tree,
2229 * so we skip the node if it is not a leaf.
2230 * Otherwise we increment data->n_sink and data->n_source with
2231 * the number of spaces in the sink and source access domains
2232 * that reach this node.
2234 static isl_bool count_sink_source(__isl_keep isl_schedule_node *node,
2235 void *user)
2237 struct isl_compute_flow_schedule_data *data = user;
2238 isl_union_set *domain;
2239 isl_union_map *umap;
2240 isl_bool r = isl_bool_false;
2242 if (isl_schedule_node_get_type(node) != isl_schedule_node_leaf)
2243 return isl_bool_true;
2245 domain = isl_schedule_node_get_universe_domain(node);
2247 umap = isl_union_map_copy(data->access->access[isl_access_sink]);
2248 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
2249 data->n_sink += isl_union_map_n_map(umap);
2250 isl_union_map_free(umap);
2251 if (!umap)
2252 r = isl_bool_error;
2254 umap = isl_union_map_copy(data->access->access[isl_access_must_source]);
2255 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
2256 data->n_source += isl_union_map_n_map(umap);
2257 isl_union_map_free(umap);
2258 if (!umap)
2259 r = isl_bool_error;
2261 umap = isl_union_map_copy(data->access->access[isl_access_may_source]);
2262 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
2263 data->n_source += isl_union_map_n_map(umap);
2264 isl_union_map_free(umap);
2265 if (!umap)
2266 r = isl_bool_error;
2268 isl_union_set_free(domain);
2270 return r;
2273 /* Add a single scheduled sink or source (depending on data->set_sink)
2274 * with scheduled access relation "map", must property data->must and
2275 * schedule node data->node to the list of sinks or sources.
2277 static isl_stat extract_sink_source(__isl_take isl_map *map, void *user)
2279 struct isl_compute_flow_schedule_data *data = user;
2280 struct isl_scheduled_access *access;
2282 if (data->set_sink)
2283 access = data->sink + data->n_sink++;
2284 else
2285 access = data->source + data->n_source++;
2287 access->access = map;
2288 access->must = data->must;
2289 access->node = isl_schedule_node_copy(data->node);
2291 return isl_stat_ok;
2294 /* isl_schedule_foreach_schedule_node_top_down callback for collecting
2295 * individual scheduled source and sink accesses (taking into account
2296 * the domain of the schedule).
2298 * We only collect accesses at the leaves of the schedule tree.
2299 * We prepend the schedule dimensions at the leaf to the iteration
2300 * domains of the source and sink accesses and then extract
2301 * the individual accesses (per space).
2303 * In particular, if the prefix schedule at the node is of the form
2305 * D -> S
2307 * while the access relations are of the form
2309 * D -> A
2311 * then the updated access relations are of the form
2313 * [S -> D] -> A
2315 * Note that S consists of a single space such that introducing S
2316 * in the access relations does not increase the number of spaces.
2318 static isl_bool collect_sink_source(__isl_keep isl_schedule_node *node,
2319 void *user)
2321 struct isl_compute_flow_schedule_data *data = user;
2322 isl_union_map *prefix;
2323 isl_union_map *umap;
2324 isl_bool r = isl_bool_false;
2326 if (isl_schedule_node_get_type(node) != isl_schedule_node_leaf)
2327 return isl_bool_true;
2329 data->node = node;
2331 prefix = isl_schedule_node_get_prefix_schedule_relation(node);
2332 prefix = isl_union_map_reverse(prefix);
2333 prefix = isl_union_map_range_map(prefix);
2335 data->set_sink = 1;
2336 umap = isl_union_map_copy(data->access->access[isl_access_sink]);
2337 umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
2338 if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
2339 r = isl_bool_error;
2340 isl_union_map_free(umap);
2342 data->set_sink = 0;
2343 data->must = 1;
2344 umap = isl_union_map_copy(data->access->access[isl_access_must_source]);
2345 umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
2346 if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
2347 r = isl_bool_error;
2348 isl_union_map_free(umap);
2350 data->set_sink = 0;
2351 data->must = 0;
2352 umap = isl_union_map_copy(data->access->access[isl_access_may_source]);
2353 umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
2354 if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
2355 r = isl_bool_error;
2356 isl_union_map_free(umap);
2358 isl_union_map_free(prefix);
2360 return r;
2363 /* isl_access_info_compute_flow callback for determining whether
2364 * the shared nesting level and the ordering within that level
2365 * for two scheduled accesses for use in compute_single_flow.
2367 * The tokens passed to this function refer to the leaves
2368 * in the schedule tree where the accesses take place.
2370 * If n is the shared number of loops, then we need to return
2371 * "2 * n + 1" if "first" precedes "second" inside the innermost
2372 * shared loop and "2 * n" otherwise.
2374 * The innermost shared ancestor may be the leaves themselves
2375 * if the accesses take place in the same leaf. Otherwise,
2376 * it is either a set node or a sequence node. Only in the case
2377 * of a sequence node do we consider one access to precede the other.
2379 static int before_node(void *first, void *second)
2381 isl_schedule_node *node1 = first;
2382 isl_schedule_node *node2 = second;
2383 isl_schedule_node *shared;
2384 int depth;
2385 int before = 0;
2387 shared = isl_schedule_node_get_shared_ancestor(node1, node2);
2388 if (!shared)
2389 return -1;
2391 depth = isl_schedule_node_get_schedule_depth(shared);
2392 if (isl_schedule_node_get_type(shared) == isl_schedule_node_sequence) {
2393 int pos1, pos2;
2395 pos1 = isl_schedule_node_get_ancestor_child_position(node1,
2396 shared);
2397 pos2 = isl_schedule_node_get_ancestor_child_position(node2,
2398 shared);
2399 before = pos1 < pos2;
2402 isl_schedule_node_free(shared);
2404 return 2 * depth + before;
2407 /* Add the scheduled sources from "data" that access
2408 * the same data space as "sink" to "access".
2410 static __isl_give isl_access_info *add_matching_sources(
2411 __isl_take isl_access_info *access, struct isl_scheduled_access *sink,
2412 struct isl_compute_flow_schedule_data *data)
2414 int i;
2415 isl_space *space;
2417 space = isl_space_range(isl_map_get_space(sink->access));
2418 for (i = 0; i < data->n_source; ++i) {
2419 struct isl_scheduled_access *source;
2420 isl_space *source_space;
2421 int eq;
2423 source = &data->source[i];
2424 source_space = isl_map_get_space(source->access);
2425 source_space = isl_space_range(source_space);
2426 eq = isl_space_is_equal(space, source_space);
2427 isl_space_free(source_space);
2429 if (!eq)
2430 continue;
2431 if (eq < 0)
2432 goto error;
2434 access = isl_access_info_add_source(access,
2435 isl_map_copy(source->access), source->must, source->node);
2438 isl_space_free(space);
2439 return access;
2440 error:
2441 isl_space_free(space);
2442 isl_access_info_free(access);
2443 return NULL;
2446 /* Given a scheduled sink access relation "sink", compute the corresponding
2447 * dependences on the sources in "data" and add the computed dependences
2448 * to "uf".
2450 * The dependences computed by access_info_compute_flow_core are of the form
2452 * [S -> I] -> [[S' -> I'] -> A]
2454 * The schedule dimensions are projected out by first currying the range,
2455 * resulting in
2457 * [S -> I] -> [S' -> [I' -> A]]
2459 * and then computing the factor range
2461 * I -> [I' -> A]
2463 static __isl_give isl_union_flow *compute_single_flow(
2464 __isl_take isl_union_flow *uf, struct isl_scheduled_access *sink,
2465 struct isl_compute_flow_schedule_data *data)
2467 int i;
2468 isl_access_info *access;
2469 isl_flow *flow;
2470 isl_map *map;
2472 if (!uf)
2473 return NULL;
2475 access = isl_access_info_alloc(isl_map_copy(sink->access), sink->node,
2476 &before_node, data->n_source);
2477 access = add_matching_sources(access, sink, data);
2479 flow = access_info_compute_flow_core(access);
2480 if (!flow)
2481 return isl_union_flow_free(uf);
2483 map = isl_map_domain_factor_range(isl_flow_get_no_source(flow, 1));
2484 uf->must_no_source = isl_union_map_union(uf->must_no_source,
2485 isl_union_map_from_map(map));
2486 map = isl_map_domain_factor_range(isl_flow_get_no_source(flow, 0));
2487 uf->may_no_source = isl_union_map_union(uf->may_no_source,
2488 isl_union_map_from_map(map));
2490 for (i = 0; i < flow->n_source; ++i) {
2491 isl_union_map *dep;
2493 map = isl_map_range_curry(isl_map_copy(flow->dep[i].map));
2494 map = isl_map_factor_range(map);
2495 dep = isl_union_map_from_map(map);
2496 if (flow->dep[i].must)
2497 uf->must_dep = isl_union_map_union(uf->must_dep, dep);
2498 else
2499 uf->may_dep = isl_union_map_union(uf->may_dep, dep);
2502 isl_flow_free(flow);
2504 return uf;
2507 /* Given a description of the "sink" accesses, the "source" accesses and
2508 * a schedule, compute for each instance of a sink access
2509 * and for each element accessed by that instance,
2510 * the possible or definite source accesses that last accessed the
2511 * element accessed by the sink access before this sink access
2512 * in the sense that there is no intermediate definite source access.
2513 * Only consider dependences between statement instances that belong
2514 * to the domain of the schedule.
2516 * The must_no_source and may_no_source elements of the result
2517 * are subsets of access->sink. The elements must_dep and may_dep
2518 * map domain elements of access->{may,must)_source to
2519 * domain elements of access->sink.
2521 * This function is used when a schedule tree representation
2522 * is available.
2524 * We extract the individual scheduled source and sink access relations
2525 * (taking into account the domain of the schedule) and
2526 * then compute dependences for each scheduled sink individually.
2528 static __isl_give isl_union_flow *compute_flow_schedule(
2529 __isl_take isl_union_access_info *access)
2531 struct isl_compute_flow_schedule_data data = { access };
2532 int i, n;
2533 isl_ctx *ctx;
2534 isl_space *space;
2535 isl_union_flow *flow;
2537 ctx = isl_union_access_info_get_ctx(access);
2539 data.n_sink = 0;
2540 data.n_source = 0;
2541 if (isl_schedule_foreach_schedule_node_top_down(access->schedule,
2542 &count_sink_source, &data) < 0)
2543 goto error;
2545 n = data.n_sink + data.n_source;
2546 data.sink = isl_calloc_array(ctx, struct isl_scheduled_access, n);
2547 if (n && !data.sink)
2548 goto error;
2549 data.source = data.sink + data.n_sink;
2551 data.n_sink = 0;
2552 data.n_source = 0;
2553 if (isl_schedule_foreach_schedule_node_top_down(access->schedule,
2554 &collect_sink_source, &data) < 0)
2555 goto error;
2557 space = isl_union_map_get_space(access->access[isl_access_sink]);
2558 flow = isl_union_flow_alloc(space);
2560 isl_compute_flow_schedule_data_align_params(&data);
2562 for (i = 0; i < data.n_sink; ++i)
2563 flow = compute_single_flow(flow, &data.sink[i], &data);
2565 isl_compute_flow_schedule_data_clear(&data);
2567 isl_union_access_info_free(access);
2568 return flow;
2569 error:
2570 isl_union_access_info_free(access);
2571 isl_compute_flow_schedule_data_clear(&data);
2572 return NULL;
2575 /* Given a description of the "sink" accesses, the "source" accesses and
2576 * a schedule, compute for each instance of a sink access
2577 * and for each element accessed by that instance,
2578 * the possible or definite source accesses that last accessed the
2579 * element accessed by the sink access before this sink access
2580 * in the sense that there is no intermediate definite source access.
2582 * The must_no_source and may_no_source elements of the result
2583 * are subsets of access->sink. The elements must_dep and may_dep
2584 * map domain elements of access->{may,must)_source to
2585 * domain elements of access->sink.
2587 * We check whether the schedule is available as a schedule tree
2588 * or a schedule map and call the corresponding function to perform
2589 * the analysis.
2591 __isl_give isl_union_flow *isl_union_access_info_compute_flow(
2592 __isl_take isl_union_access_info *access)
2594 access = isl_union_access_info_normalize(access);
2595 if (!access)
2596 return NULL;
2597 if (access->schedule)
2598 return compute_flow_schedule(access);
2599 else
2600 return compute_flow_union_map(access);
2603 /* Print the information contained in "flow" to "p".
2604 * The information is printed as a YAML document.
2606 __isl_give isl_printer *isl_printer_print_union_flow(
2607 __isl_take isl_printer *p, __isl_keep isl_union_flow *flow)
2609 isl_union_map *umap;
2611 if (!flow)
2612 return isl_printer_free(p);
2614 p = isl_printer_yaml_start_mapping(p);
2615 umap = isl_union_flow_get_full_must_dependence(flow);
2616 p = print_union_map_field(p, "must_dependence", umap);
2617 isl_union_map_free(umap);
2618 umap = isl_union_flow_get_full_may_dependence(flow);
2619 p = print_union_map_field(p, "may_dependence", umap);
2620 isl_union_map_free(umap);
2621 p = print_union_map_field(p, "must_no_source", flow->must_no_source);
2622 umap = isl_union_flow_get_may_no_source(flow);
2623 p = print_union_map_field(p, "may_no_source", umap);
2624 isl_union_map_free(umap);
2625 p = isl_printer_yaml_end_mapping(p);
2627 return p;
2630 /* Return a string representation of the information in "flow".
2631 * The information is printed in flow format.
2633 __isl_give char *isl_union_flow_to_str(__isl_keep isl_union_flow *flow)
2635 isl_printer *p;
2636 char *s;
2638 if (!flow)
2639 return NULL;
2641 p = isl_printer_to_str(isl_union_flow_get_ctx(flow));
2642 p = isl_printer_set_yaml_style(p, ISL_YAML_STYLE_FLOW);
2643 p = isl_printer_print_union_flow(p, flow);
2644 s = isl_printer_get_str(p);
2645 isl_printer_free(p);
2647 return s;
2650 /* Given a collection of "sink" and "source" accesses,
2651 * compute for each iteration of a sink access
2652 * and for each element accessed by that iteration,
2653 * the source access in the list that last accessed the
2654 * element accessed by the sink access before this sink access.
2655 * Each access is given as a map from the loop iterators
2656 * to the array indices.
2657 * The result is a relations between source and sink
2658 * iterations and a subset of the domain of the sink accesses,
2659 * corresponding to those iterations that access an element
2660 * not previously accessed.
2662 * We collect the inputs in an isl_union_access_info object,
2663 * call isl_union_access_info_compute_flow and extract
2664 * the outputs from the result.
2666 int isl_union_map_compute_flow(__isl_take isl_union_map *sink,
2667 __isl_take isl_union_map *must_source,
2668 __isl_take isl_union_map *may_source,
2669 __isl_take isl_union_map *schedule,
2670 __isl_give isl_union_map **must_dep, __isl_give isl_union_map **may_dep,
2671 __isl_give isl_union_map **must_no_source,
2672 __isl_give isl_union_map **may_no_source)
2674 isl_union_access_info *access;
2675 isl_union_flow *flow;
2677 access = isl_union_access_info_from_sink(sink);
2678 access = isl_union_access_info_set_must_source(access, must_source);
2679 access = isl_union_access_info_set_may_source(access, may_source);
2680 access = isl_union_access_info_set_schedule_map(access, schedule);
2681 flow = isl_union_access_info_compute_flow(access);
2683 if (must_dep)
2684 *must_dep = isl_union_flow_get_must_dependence(flow);
2685 if (may_dep)
2686 *may_dep = isl_union_flow_get_non_must_dependence(flow);
2687 if (must_no_source)
2688 *must_no_source = isl_union_flow_get_must_no_source(flow);
2689 if (may_no_source)
2690 *may_no_source = isl_union_flow_get_non_must_no_source(flow);
2692 isl_union_flow_free(flow);
2694 if ((must_dep && !*must_dep) || (may_dep && !*may_dep) ||
2695 (must_no_source && !*must_no_source) ||
2696 (may_no_source && !*may_no_source))
2697 goto error;
2699 return 0;
2700 error:
2701 if (must_dep)
2702 *must_dep = isl_union_map_free(*must_dep);
2703 if (may_dep)
2704 *may_dep = isl_union_map_free(*may_dep);
2705 if (must_no_source)
2706 *must_no_source = isl_union_map_free(*must_no_source);
2707 if (may_no_source)
2708 *may_no_source = isl_union_map_free(*may_no_source);
2709 return -1;