isl_printer_print_union_access_info: do not print empty access relations
[isl.git] / isl_flow.c
blob501d9768ee0227bb80e9f1a420c4b388354252fc
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 /* An enumeration of the various keys that may appear in a YAML mapping
1453 * of an isl_union_access_info object.
1454 * The keys for the access relation types are assumed to have the same values
1455 * as the access relation types in isl_access_type.
1457 enum isl_ai_key {
1458 isl_ai_key_sink = isl_access_sink,
1459 isl_ai_key_must_source = isl_access_must_source,
1460 isl_ai_key_may_source = isl_access_may_source,
1461 isl_ai_key_schedule_map,
1462 isl_ai_key_schedule,
1465 /* Textual representations of the YAML keys for an isl_union_access_info
1466 * object.
1468 static char *key_str[] = {
1469 [isl_ai_key_sink] = "sink",
1470 [isl_ai_key_must_source] = "must_source",
1471 [isl_ai_key_may_source] = "may_source",
1472 [isl_ai_key_schedule_map] = "schedule_map",
1473 [isl_ai_key_schedule] = "schedule",
1476 /* Print a key-value pair corresponding to the access relation of type "type"
1477 * of a YAML mapping of "info" to "p".
1479 * The sink access relation is always printed, but any other access relation
1480 * is only printed if it is non-empty.
1482 static __isl_give isl_printer *print_access_field(__isl_take isl_printer *p,
1483 __isl_keep isl_union_access_info *info, enum isl_access_type type)
1485 if (type != isl_access_sink) {
1486 isl_bool empty;
1488 empty = isl_union_map_is_empty(info->access[type]);
1489 if (empty < 0)
1490 return isl_printer_free(p);
1491 if (empty)
1492 return p;
1494 return print_union_map_field(p, key_str[type], info->access[type]);
1497 /* Print the information contained in "access" to "p".
1498 * The information is printed as a YAML document.
1500 __isl_give isl_printer *isl_printer_print_union_access_info(
1501 __isl_take isl_printer *p, __isl_keep isl_union_access_info *access)
1503 enum isl_access_type i;
1505 if (!access)
1506 return isl_printer_free(p);
1508 p = isl_printer_yaml_start_mapping(p);
1509 for (i = isl_access_sink; i < isl_access_end; ++i)
1510 p = print_access_field(p, access, i);
1511 if (access->schedule) {
1512 p = isl_printer_print_str(p, key_str[isl_ai_key_schedule]);
1513 p = isl_printer_yaml_next(p);
1514 p = isl_printer_print_schedule(p, access->schedule);
1515 p = isl_printer_yaml_next(p);
1516 } else {
1517 p = print_union_map_field(p, key_str[isl_ai_key_schedule_map],
1518 access->schedule_map);
1520 p = isl_printer_yaml_end_mapping(p);
1522 return p;
1525 /* Return a string representation of the information in "access".
1526 * The information is printed in flow format.
1528 __isl_give char *isl_union_access_info_to_str(
1529 __isl_keep isl_union_access_info *access)
1531 isl_printer *p;
1532 char *s;
1534 if (!access)
1535 return NULL;
1537 p = isl_printer_to_str(isl_union_access_info_get_ctx(access));
1538 p = isl_printer_set_yaml_style(p, ISL_YAML_STYLE_FLOW);
1539 p = isl_printer_print_union_access_info(p, access);
1540 s = isl_printer_get_str(p);
1541 isl_printer_free(p);
1543 return s;
1546 /* Update the fields of "access" such that they all have the same parameters,
1547 * keeping in mind that the schedule_map field may be NULL and ignoring
1548 * the schedule field.
1550 static __isl_give isl_union_access_info *isl_union_access_info_align_params(
1551 __isl_take isl_union_access_info *access)
1553 isl_space *space;
1554 enum isl_access_type i;
1556 if (!access)
1557 return NULL;
1559 space = isl_union_map_get_space(access->access[isl_access_sink]);
1560 for (i = isl_access_sink + 1; i < isl_access_end; ++i)
1561 space = isl_space_align_params(space,
1562 isl_union_map_get_space(access->access[i]));
1563 if (access->schedule_map)
1564 space = isl_space_align_params(space,
1565 isl_union_map_get_space(access->schedule_map));
1566 for (i = isl_access_sink; i < isl_access_end; ++i)
1567 access->access[i] =
1568 isl_union_map_align_params(access->access[i],
1569 isl_space_copy(space));
1570 if (!access->schedule_map) {
1571 isl_space_free(space);
1572 } else {
1573 access->schedule_map =
1574 isl_union_map_align_params(access->schedule_map, space);
1575 if (!access->schedule_map)
1576 return isl_union_access_info_free(access);
1579 for (i = isl_access_sink; i < isl_access_end; ++i)
1580 if (!access->access[i])
1581 return isl_union_access_info_free(access);
1583 return access;
1586 /* Prepend the schedule dimensions to the iteration domains.
1588 * That is, if the schedule is of the form
1590 * D -> S
1592 * while the access relations are of the form
1594 * D -> A
1596 * then the updated access relations are of the form
1598 * [S -> D] -> A
1600 * The schedule map is also replaced by the map
1602 * [S -> D] -> D
1604 * that is used during the internal computation.
1605 * Neither the original schedule map nor this updated schedule map
1606 * are used after the call to this function.
1608 static __isl_give isl_union_access_info *
1609 isl_union_access_info_introduce_schedule(
1610 __isl_take isl_union_access_info *access)
1612 isl_union_map *sm;
1613 enum isl_access_type i;
1615 if (!access)
1616 return NULL;
1618 sm = isl_union_map_reverse(access->schedule_map);
1619 sm = isl_union_map_range_map(sm);
1620 for (i = isl_access_sink; i < isl_access_end; ++i)
1621 access->access[i] =
1622 isl_union_map_apply_range(isl_union_map_copy(sm),
1623 access->access[i]);
1624 access->schedule_map = sm;
1626 for (i = isl_access_sink; i < isl_access_end; ++i)
1627 if (!access->access[i])
1628 return isl_union_access_info_free(access);
1629 if (!access->schedule_map)
1630 return isl_union_access_info_free(access);
1632 return access;
1635 /* This structure represents the result of a dependence analysis computation.
1637 * "must_dep" represents the full definite dependences
1638 * "may_dep" represents the full non-definite dependences.
1639 * Both are of the form
1641 * [Source] -> [[Sink -> Data]]
1643 * (after the schedule dimensions have been projected out).
1644 * "must_no_source" represents the subset of the sink accesses for which
1645 * definitely no source was found.
1646 * "may_no_source" represents the subset of the sink accesses for which
1647 * possibly, but not definitely, no source was found.
1649 struct isl_union_flow {
1650 isl_union_map *must_dep;
1651 isl_union_map *may_dep;
1652 isl_union_map *must_no_source;
1653 isl_union_map *may_no_source;
1656 /* Return the isl_ctx to which "flow" belongs.
1658 isl_ctx *isl_union_flow_get_ctx(__isl_keep isl_union_flow *flow)
1660 return flow ? isl_union_map_get_ctx(flow->must_dep) : NULL;
1663 /* Free "flow" and return NULL.
1665 __isl_null isl_union_flow *isl_union_flow_free(__isl_take isl_union_flow *flow)
1667 if (!flow)
1668 return NULL;
1669 isl_union_map_free(flow->must_dep);
1670 isl_union_map_free(flow->may_dep);
1671 isl_union_map_free(flow->must_no_source);
1672 isl_union_map_free(flow->may_no_source);
1673 free(flow);
1674 return NULL;
1677 void isl_union_flow_dump(__isl_keep isl_union_flow *flow)
1679 if (!flow)
1680 return;
1682 fprintf(stderr, "must dependences: ");
1683 isl_union_map_dump(flow->must_dep);
1684 fprintf(stderr, "may dependences: ");
1685 isl_union_map_dump(flow->may_dep);
1686 fprintf(stderr, "must no source: ");
1687 isl_union_map_dump(flow->must_no_source);
1688 fprintf(stderr, "may no source: ");
1689 isl_union_map_dump(flow->may_no_source);
1692 /* Return the full definite dependences in "flow", with accessed elements.
1694 __isl_give isl_union_map *isl_union_flow_get_full_must_dependence(
1695 __isl_keep isl_union_flow *flow)
1697 if (!flow)
1698 return NULL;
1699 return isl_union_map_copy(flow->must_dep);
1702 /* Return the full possible dependences in "flow", including the definite
1703 * dependences, with accessed elements.
1705 __isl_give isl_union_map *isl_union_flow_get_full_may_dependence(
1706 __isl_keep isl_union_flow *flow)
1708 if (!flow)
1709 return NULL;
1710 return isl_union_map_union(isl_union_map_copy(flow->must_dep),
1711 isl_union_map_copy(flow->may_dep));
1714 /* Return the definite dependences in "flow", without the accessed elements.
1716 __isl_give isl_union_map *isl_union_flow_get_must_dependence(
1717 __isl_keep isl_union_flow *flow)
1719 isl_union_map *dep;
1721 if (!flow)
1722 return NULL;
1723 dep = isl_union_map_copy(flow->must_dep);
1724 return isl_union_map_range_factor_domain(dep);
1727 /* Return the possible dependences in "flow", including the definite
1728 * dependences, without the accessed elements.
1730 __isl_give isl_union_map *isl_union_flow_get_may_dependence(
1731 __isl_keep isl_union_flow *flow)
1733 isl_union_map *dep;
1735 if (!flow)
1736 return NULL;
1737 dep = isl_union_map_union(isl_union_map_copy(flow->must_dep),
1738 isl_union_map_copy(flow->may_dep));
1739 return isl_union_map_range_factor_domain(dep);
1742 /* Return the non-definite dependences in "flow".
1744 static __isl_give isl_union_map *isl_union_flow_get_non_must_dependence(
1745 __isl_keep isl_union_flow *flow)
1747 if (!flow)
1748 return NULL;
1749 return isl_union_map_copy(flow->may_dep);
1752 /* Return the subset of the sink accesses for which definitely
1753 * no source was found.
1755 __isl_give isl_union_map *isl_union_flow_get_must_no_source(
1756 __isl_keep isl_union_flow *flow)
1758 if (!flow)
1759 return NULL;
1760 return isl_union_map_copy(flow->must_no_source);
1763 /* Return the subset of the sink accesses for which possibly
1764 * no source was found, including those for which definitely
1765 * no source was found.
1767 __isl_give isl_union_map *isl_union_flow_get_may_no_source(
1768 __isl_keep isl_union_flow *flow)
1770 if (!flow)
1771 return NULL;
1772 return isl_union_map_union(isl_union_map_copy(flow->must_no_source),
1773 isl_union_map_copy(flow->may_no_source));
1776 /* Return the subset of the sink accesses for which possibly, but not
1777 * definitely, no source was found.
1779 static __isl_give isl_union_map *isl_union_flow_get_non_must_no_source(
1780 __isl_keep isl_union_flow *flow)
1782 if (!flow)
1783 return NULL;
1784 return isl_union_map_copy(flow->may_no_source);
1787 /* Create a new isl_union_flow object, initialized with empty
1788 * dependence relations and sink subsets.
1790 static __isl_give isl_union_flow *isl_union_flow_alloc(
1791 __isl_take isl_space *space)
1793 isl_ctx *ctx;
1794 isl_union_map *empty;
1795 isl_union_flow *flow;
1797 if (!space)
1798 return NULL;
1799 ctx = isl_space_get_ctx(space);
1800 flow = isl_alloc_type(ctx, isl_union_flow);
1801 if (!flow)
1802 goto error;
1804 empty = isl_union_map_empty(space);
1805 flow->must_dep = isl_union_map_copy(empty);
1806 flow->may_dep = isl_union_map_copy(empty);
1807 flow->must_no_source = isl_union_map_copy(empty);
1808 flow->may_no_source = empty;
1810 if (!flow->must_dep || !flow->may_dep ||
1811 !flow->must_no_source || !flow->may_no_source)
1812 return isl_union_flow_free(flow);
1814 return flow;
1815 error:
1816 isl_space_free(space);
1817 return NULL;
1820 /* Copy this isl_union_flow object.
1822 __isl_give isl_union_flow *isl_union_flow_copy(__isl_keep isl_union_flow *flow)
1824 isl_union_flow *copy;
1826 if (!flow)
1827 return NULL;
1829 copy = isl_union_flow_alloc(isl_union_map_get_space(flow->must_dep));
1831 if (!copy)
1832 return NULL;
1834 copy->must_dep = isl_union_map_union(copy->must_dep,
1835 isl_union_map_copy(flow->must_dep));
1836 copy->may_dep = isl_union_map_union(copy->may_dep,
1837 isl_union_map_copy(flow->may_dep));
1838 copy->must_no_source = isl_union_map_union(copy->must_no_source,
1839 isl_union_map_copy(flow->must_no_source));
1840 copy->may_no_source = isl_union_map_union(copy->may_no_source,
1841 isl_union_map_copy(flow->may_no_source));
1843 if (!copy->must_dep || !copy->may_dep ||
1844 !copy->must_no_source || !copy->may_no_source)
1845 return isl_union_flow_free(copy);
1847 return copy;
1850 /* Drop the schedule dimensions from the iteration domains in "flow".
1851 * In particular, the schedule dimensions have been prepended
1852 * to the iteration domains prior to the dependence analysis by
1853 * replacing the iteration domain D, by the wrapped map [S -> D].
1854 * Replace these wrapped maps by the original D.
1856 * In particular, the dependences computed by access_info_compute_flow_core
1857 * are of the form
1859 * [S -> D] -> [[S' -> D'] -> A]
1861 * The schedule dimensions are projected out by first currying the range,
1862 * resulting in
1864 * [S -> D] -> [S' -> [D' -> A]]
1866 * and then computing the factor range
1868 * D -> [D' -> A]
1870 static __isl_give isl_union_flow *isl_union_flow_drop_schedule(
1871 __isl_take isl_union_flow *flow)
1873 if (!flow)
1874 return NULL;
1876 flow->must_dep = isl_union_map_range_curry(flow->must_dep);
1877 flow->must_dep = isl_union_map_factor_range(flow->must_dep);
1878 flow->may_dep = isl_union_map_range_curry(flow->may_dep);
1879 flow->may_dep = isl_union_map_factor_range(flow->may_dep);
1880 flow->must_no_source =
1881 isl_union_map_domain_factor_range(flow->must_no_source);
1882 flow->may_no_source =
1883 isl_union_map_domain_factor_range(flow->may_no_source);
1885 if (!flow->must_dep || !flow->may_dep ||
1886 !flow->must_no_source || !flow->may_no_source)
1887 return isl_union_flow_free(flow);
1889 return flow;
1892 struct isl_compute_flow_data {
1893 isl_union_map *must_source;
1894 isl_union_map *may_source;
1895 isl_union_flow *flow;
1897 int count;
1898 int must;
1899 isl_space *dim;
1900 struct isl_sched_info *sink_info;
1901 struct isl_sched_info **source_info;
1902 isl_access_info *accesses;
1905 static isl_stat count_matching_array(__isl_take isl_map *map, void *user)
1907 int eq;
1908 isl_space *dim;
1909 struct isl_compute_flow_data *data;
1911 data = (struct isl_compute_flow_data *)user;
1913 dim = isl_space_range(isl_map_get_space(map));
1915 eq = isl_space_is_equal(dim, data->dim);
1917 isl_space_free(dim);
1918 isl_map_free(map);
1920 if (eq < 0)
1921 return isl_stat_error;
1922 if (eq)
1923 data->count++;
1925 return isl_stat_ok;
1928 static isl_stat collect_matching_array(__isl_take isl_map *map, void *user)
1930 int eq;
1931 isl_space *dim;
1932 struct isl_sched_info *info;
1933 struct isl_compute_flow_data *data;
1935 data = (struct isl_compute_flow_data *)user;
1937 dim = isl_space_range(isl_map_get_space(map));
1939 eq = isl_space_is_equal(dim, data->dim);
1941 isl_space_free(dim);
1943 if (eq < 0)
1944 goto error;
1945 if (!eq) {
1946 isl_map_free(map);
1947 return isl_stat_ok;
1950 info = sched_info_alloc(map);
1951 data->source_info[data->count] = info;
1953 data->accesses = isl_access_info_add_source(data->accesses,
1954 map, data->must, info);
1956 data->count++;
1958 return isl_stat_ok;
1959 error:
1960 isl_map_free(map);
1961 return isl_stat_error;
1964 /* Determine the shared nesting level and the "textual order" of
1965 * the given accesses.
1967 * We first determine the minimal schedule dimension for both accesses.
1969 * If among those dimensions, we can find one where both have a fixed
1970 * value and if moreover those values are different, then the previous
1971 * dimension is the last shared nesting level and the textual order
1972 * is determined based on the order of the fixed values.
1973 * If no such fixed values can be found, then we set the shared
1974 * nesting level to the minimal schedule dimension, with no textual ordering.
1976 static int before(void *first, void *second)
1978 struct isl_sched_info *info1 = first;
1979 struct isl_sched_info *info2 = second;
1980 int n1, n2;
1981 int i;
1983 n1 = isl_vec_size(info1->cst);
1984 n2 = isl_vec_size(info2->cst);
1986 if (n2 < n1)
1987 n1 = n2;
1989 for (i = 0; i < n1; ++i) {
1990 int r;
1991 int cmp;
1993 if (!info1->is_cst[i])
1994 continue;
1995 if (!info2->is_cst[i])
1996 continue;
1997 cmp = isl_vec_cmp_element(info1->cst, info2->cst, i);
1998 if (cmp == 0)
1999 continue;
2001 r = 2 * i + (cmp < 0);
2003 return r;
2006 return 2 * n1;
2009 /* Given a sink access, look for all the source accesses that access
2010 * the same array and perform dataflow analysis on them using
2011 * isl_access_info_compute_flow_core.
2013 static isl_stat compute_flow(__isl_take isl_map *map, void *user)
2015 int i;
2016 isl_ctx *ctx;
2017 struct isl_compute_flow_data *data;
2018 isl_flow *flow;
2019 isl_union_flow *df;
2021 data = (struct isl_compute_flow_data *)user;
2022 df = data->flow;
2024 ctx = isl_map_get_ctx(map);
2026 data->accesses = NULL;
2027 data->sink_info = NULL;
2028 data->source_info = NULL;
2029 data->count = 0;
2030 data->dim = isl_space_range(isl_map_get_space(map));
2032 if (isl_union_map_foreach_map(data->must_source,
2033 &count_matching_array, data) < 0)
2034 goto error;
2035 if (isl_union_map_foreach_map(data->may_source,
2036 &count_matching_array, data) < 0)
2037 goto error;
2039 data->sink_info = sched_info_alloc(map);
2040 data->source_info = isl_calloc_array(ctx, struct isl_sched_info *,
2041 data->count);
2043 data->accesses = isl_access_info_alloc(isl_map_copy(map),
2044 data->sink_info, &before, data->count);
2045 if (!data->sink_info || (data->count && !data->source_info) ||
2046 !data->accesses)
2047 goto error;
2048 data->count = 0;
2049 data->must = 1;
2050 if (isl_union_map_foreach_map(data->must_source,
2051 &collect_matching_array, data) < 0)
2052 goto error;
2053 data->must = 0;
2054 if (isl_union_map_foreach_map(data->may_source,
2055 &collect_matching_array, data) < 0)
2056 goto error;
2058 flow = access_info_compute_flow_core(data->accesses);
2059 data->accesses = NULL;
2061 if (!flow)
2062 goto error;
2064 df->must_no_source = isl_union_map_union(df->must_no_source,
2065 isl_union_map_from_map(isl_flow_get_no_source(flow, 1)));
2066 df->may_no_source = isl_union_map_union(df->may_no_source,
2067 isl_union_map_from_map(isl_flow_get_no_source(flow, 0)));
2069 for (i = 0; i < flow->n_source; ++i) {
2070 isl_union_map *dep;
2071 dep = isl_union_map_from_map(isl_map_copy(flow->dep[i].map));
2072 if (flow->dep[i].must)
2073 df->must_dep = isl_union_map_union(df->must_dep, dep);
2074 else
2075 df->may_dep = isl_union_map_union(df->may_dep, dep);
2078 isl_flow_free(flow);
2080 sched_info_free(data->sink_info);
2081 if (data->source_info) {
2082 for (i = 0; i < data->count; ++i)
2083 sched_info_free(data->source_info[i]);
2084 free(data->source_info);
2086 isl_space_free(data->dim);
2087 isl_map_free(map);
2089 return isl_stat_ok;
2090 error:
2091 isl_access_info_free(data->accesses);
2092 sched_info_free(data->sink_info);
2093 if (data->source_info) {
2094 for (i = 0; i < data->count; ++i)
2095 sched_info_free(data->source_info[i]);
2096 free(data->source_info);
2098 isl_space_free(data->dim);
2099 isl_map_free(map);
2101 return isl_stat_error;
2104 /* Remove the must accesses from the may accesses.
2106 * A must access always trumps a may access, so there is no need
2107 * for a must access to also be considered as a may access. Doing so
2108 * would only cost extra computations only to find out that
2109 * the duplicated may access does not make any difference.
2111 static __isl_give isl_union_access_info *isl_union_access_info_normalize(
2112 __isl_take isl_union_access_info *access)
2114 if (!access)
2115 return NULL;
2116 access->access[isl_access_may_source] =
2117 isl_union_map_subtract(access->access[isl_access_may_source],
2118 isl_union_map_copy(access->access[isl_access_must_source]));
2119 if (!access->access[isl_access_may_source])
2120 return isl_union_access_info_free(access);
2122 return access;
2125 /* Given a description of the "sink" accesses, the "source" accesses and
2126 * a schedule, compute for each instance of a sink access
2127 * and for each element accessed by that instance,
2128 * the possible or definite source accesses that last accessed the
2129 * element accessed by the sink access before this sink access
2130 * in the sense that there is no intermediate definite source access.
2132 * The must_no_source and may_no_source elements of the result
2133 * are subsets of access->sink. The elements must_dep and may_dep
2134 * map domain elements of access->{may,must)_source to
2135 * domain elements of access->sink.
2137 * This function is used when only the schedule map representation
2138 * is available.
2140 * We first prepend the schedule dimensions to the domain
2141 * of the accesses so that we can easily compare their relative order.
2142 * Then we consider each sink access individually in compute_flow.
2144 static __isl_give isl_union_flow *compute_flow_union_map(
2145 __isl_take isl_union_access_info *access)
2147 struct isl_compute_flow_data data;
2148 isl_union_map *sink;
2150 access = isl_union_access_info_align_params(access);
2151 access = isl_union_access_info_introduce_schedule(access);
2152 if (!access)
2153 return NULL;
2155 data.must_source = access->access[isl_access_must_source];
2156 data.may_source = access->access[isl_access_may_source];
2158 sink = access->access[isl_access_sink];
2159 data.flow = isl_union_flow_alloc(isl_union_map_get_space(sink));
2161 if (isl_union_map_foreach_map(sink, &compute_flow, &data) < 0)
2162 goto error;
2164 data.flow = isl_union_flow_drop_schedule(data.flow);
2166 isl_union_access_info_free(access);
2167 return data.flow;
2168 error:
2169 isl_union_access_info_free(access);
2170 isl_union_flow_free(data.flow);
2171 return NULL;
2174 /* A schedule access relation.
2176 * The access relation "access" is of the form [S -> D] -> A,
2177 * where S corresponds to the prefix schedule at "node".
2178 * "must" is only relevant for source accesses and indicates
2179 * whether the access is a must source or a may source.
2181 struct isl_scheduled_access {
2182 isl_map *access;
2183 int must;
2184 isl_schedule_node *node;
2187 /* Data structure for keeping track of individual scheduled sink and source
2188 * accesses when computing dependence analysis based on a schedule tree.
2190 * "n_sink" is the number of used entries in "sink"
2191 * "n_source" is the number of used entries in "source"
2193 * "set_sink", "must" and "node" are only used inside collect_sink_source,
2194 * to keep track of the current node and
2195 * of what extract_sink_source needs to do.
2197 struct isl_compute_flow_schedule_data {
2198 isl_union_access_info *access;
2200 int n_sink;
2201 int n_source;
2203 struct isl_scheduled_access *sink;
2204 struct isl_scheduled_access *source;
2206 int set_sink;
2207 int must;
2208 isl_schedule_node *node;
2211 /* Align the parameters of all sinks with all sources.
2213 * If there are no sinks or no sources, then no alignment is needed.
2215 static void isl_compute_flow_schedule_data_align_params(
2216 struct isl_compute_flow_schedule_data *data)
2218 int i;
2219 isl_space *space;
2221 if (data->n_sink == 0 || data->n_source == 0)
2222 return;
2224 space = isl_map_get_space(data->sink[0].access);
2226 for (i = 1; i < data->n_sink; ++i)
2227 space = isl_space_align_params(space,
2228 isl_map_get_space(data->sink[i].access));
2229 for (i = 0; i < data->n_source; ++i)
2230 space = isl_space_align_params(space,
2231 isl_map_get_space(data->source[i].access));
2233 for (i = 0; i < data->n_sink; ++i)
2234 data->sink[i].access =
2235 isl_map_align_params(data->sink[i].access,
2236 isl_space_copy(space));
2237 for (i = 0; i < data->n_source; ++i)
2238 data->source[i].access =
2239 isl_map_align_params(data->source[i].access,
2240 isl_space_copy(space));
2242 isl_space_free(space);
2245 /* Free all the memory referenced from "data".
2246 * Do not free "data" itself as it may be allocated on the stack.
2248 static void isl_compute_flow_schedule_data_clear(
2249 struct isl_compute_flow_schedule_data *data)
2251 int i;
2253 if (!data->sink)
2254 return;
2256 for (i = 0; i < data->n_sink; ++i) {
2257 isl_map_free(data->sink[i].access);
2258 isl_schedule_node_free(data->sink[i].node);
2261 for (i = 0; i < data->n_source; ++i) {
2262 isl_map_free(data->source[i].access);
2263 isl_schedule_node_free(data->source[i].node);
2266 free(data->sink);
2269 /* isl_schedule_foreach_schedule_node_top_down callback for counting
2270 * (an upper bound on) the number of sinks and sources.
2272 * Sinks and sources are only extracted at leaves of the tree,
2273 * so we skip the node if it is not a leaf.
2274 * Otherwise we increment data->n_sink and data->n_source with
2275 * the number of spaces in the sink and source access domains
2276 * that reach this node.
2278 static isl_bool count_sink_source(__isl_keep isl_schedule_node *node,
2279 void *user)
2281 struct isl_compute_flow_schedule_data *data = user;
2282 isl_union_set *domain;
2283 isl_union_map *umap;
2284 isl_bool r = isl_bool_false;
2286 if (isl_schedule_node_get_type(node) != isl_schedule_node_leaf)
2287 return isl_bool_true;
2289 domain = isl_schedule_node_get_universe_domain(node);
2291 umap = isl_union_map_copy(data->access->access[isl_access_sink]);
2292 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
2293 data->n_sink += isl_union_map_n_map(umap);
2294 isl_union_map_free(umap);
2295 if (!umap)
2296 r = isl_bool_error;
2298 umap = isl_union_map_copy(data->access->access[isl_access_must_source]);
2299 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
2300 data->n_source += isl_union_map_n_map(umap);
2301 isl_union_map_free(umap);
2302 if (!umap)
2303 r = isl_bool_error;
2305 umap = isl_union_map_copy(data->access->access[isl_access_may_source]);
2306 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
2307 data->n_source += isl_union_map_n_map(umap);
2308 isl_union_map_free(umap);
2309 if (!umap)
2310 r = isl_bool_error;
2312 isl_union_set_free(domain);
2314 return r;
2317 /* Add a single scheduled sink or source (depending on data->set_sink)
2318 * with scheduled access relation "map", must property data->must and
2319 * schedule node data->node to the list of sinks or sources.
2321 static isl_stat extract_sink_source(__isl_take isl_map *map, void *user)
2323 struct isl_compute_flow_schedule_data *data = user;
2324 struct isl_scheduled_access *access;
2326 if (data->set_sink)
2327 access = data->sink + data->n_sink++;
2328 else
2329 access = data->source + data->n_source++;
2331 access->access = map;
2332 access->must = data->must;
2333 access->node = isl_schedule_node_copy(data->node);
2335 return isl_stat_ok;
2338 /* isl_schedule_foreach_schedule_node_top_down callback for collecting
2339 * individual scheduled source and sink accesses (taking into account
2340 * the domain of the schedule).
2342 * We only collect accesses at the leaves of the schedule tree.
2343 * We prepend the schedule dimensions at the leaf to the iteration
2344 * domains of the source and sink accesses and then extract
2345 * the individual accesses (per space).
2347 * In particular, if the prefix schedule at the node is of the form
2349 * D -> S
2351 * while the access relations are of the form
2353 * D -> A
2355 * then the updated access relations are of the form
2357 * [S -> D] -> A
2359 * Note that S consists of a single space such that introducing S
2360 * in the access relations does not increase the number of spaces.
2362 static isl_bool collect_sink_source(__isl_keep isl_schedule_node *node,
2363 void *user)
2365 struct isl_compute_flow_schedule_data *data = user;
2366 isl_union_map *prefix;
2367 isl_union_map *umap;
2368 isl_bool r = isl_bool_false;
2370 if (isl_schedule_node_get_type(node) != isl_schedule_node_leaf)
2371 return isl_bool_true;
2373 data->node = node;
2375 prefix = isl_schedule_node_get_prefix_schedule_relation(node);
2376 prefix = isl_union_map_reverse(prefix);
2377 prefix = isl_union_map_range_map(prefix);
2379 data->set_sink = 1;
2380 umap = isl_union_map_copy(data->access->access[isl_access_sink]);
2381 umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
2382 if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
2383 r = isl_bool_error;
2384 isl_union_map_free(umap);
2386 data->set_sink = 0;
2387 data->must = 1;
2388 umap = isl_union_map_copy(data->access->access[isl_access_must_source]);
2389 umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
2390 if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
2391 r = isl_bool_error;
2392 isl_union_map_free(umap);
2394 data->set_sink = 0;
2395 data->must = 0;
2396 umap = isl_union_map_copy(data->access->access[isl_access_may_source]);
2397 umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
2398 if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
2399 r = isl_bool_error;
2400 isl_union_map_free(umap);
2402 isl_union_map_free(prefix);
2404 return r;
2407 /* isl_access_info_compute_flow callback for determining whether
2408 * the shared nesting level and the ordering within that level
2409 * for two scheduled accesses for use in compute_single_flow.
2411 * The tokens passed to this function refer to the leaves
2412 * in the schedule tree where the accesses take place.
2414 * If n is the shared number of loops, then we need to return
2415 * "2 * n + 1" if "first" precedes "second" inside the innermost
2416 * shared loop and "2 * n" otherwise.
2418 * The innermost shared ancestor may be the leaves themselves
2419 * if the accesses take place in the same leaf. Otherwise,
2420 * it is either a set node or a sequence node. Only in the case
2421 * of a sequence node do we consider one access to precede the other.
2423 static int before_node(void *first, void *second)
2425 isl_schedule_node *node1 = first;
2426 isl_schedule_node *node2 = second;
2427 isl_schedule_node *shared;
2428 int depth;
2429 int before = 0;
2431 shared = isl_schedule_node_get_shared_ancestor(node1, node2);
2432 if (!shared)
2433 return -1;
2435 depth = isl_schedule_node_get_schedule_depth(shared);
2436 if (isl_schedule_node_get_type(shared) == isl_schedule_node_sequence) {
2437 int pos1, pos2;
2439 pos1 = isl_schedule_node_get_ancestor_child_position(node1,
2440 shared);
2441 pos2 = isl_schedule_node_get_ancestor_child_position(node2,
2442 shared);
2443 before = pos1 < pos2;
2446 isl_schedule_node_free(shared);
2448 return 2 * depth + before;
2451 /* Add the scheduled sources from "data" that access
2452 * the same data space as "sink" to "access".
2454 static __isl_give isl_access_info *add_matching_sources(
2455 __isl_take isl_access_info *access, struct isl_scheduled_access *sink,
2456 struct isl_compute_flow_schedule_data *data)
2458 int i;
2459 isl_space *space;
2461 space = isl_space_range(isl_map_get_space(sink->access));
2462 for (i = 0; i < data->n_source; ++i) {
2463 struct isl_scheduled_access *source;
2464 isl_space *source_space;
2465 int eq;
2467 source = &data->source[i];
2468 source_space = isl_map_get_space(source->access);
2469 source_space = isl_space_range(source_space);
2470 eq = isl_space_is_equal(space, source_space);
2471 isl_space_free(source_space);
2473 if (!eq)
2474 continue;
2475 if (eq < 0)
2476 goto error;
2478 access = isl_access_info_add_source(access,
2479 isl_map_copy(source->access), source->must, source->node);
2482 isl_space_free(space);
2483 return access;
2484 error:
2485 isl_space_free(space);
2486 isl_access_info_free(access);
2487 return NULL;
2490 /* Given a scheduled sink access relation "sink", compute the corresponding
2491 * dependences on the sources in "data" and add the computed dependences
2492 * to "uf".
2494 * The dependences computed by access_info_compute_flow_core are of the form
2496 * [S -> I] -> [[S' -> I'] -> A]
2498 * The schedule dimensions are projected out by first currying the range,
2499 * resulting in
2501 * [S -> I] -> [S' -> [I' -> A]]
2503 * and then computing the factor range
2505 * I -> [I' -> A]
2507 static __isl_give isl_union_flow *compute_single_flow(
2508 __isl_take isl_union_flow *uf, struct isl_scheduled_access *sink,
2509 struct isl_compute_flow_schedule_data *data)
2511 int i;
2512 isl_access_info *access;
2513 isl_flow *flow;
2514 isl_map *map;
2516 if (!uf)
2517 return NULL;
2519 access = isl_access_info_alloc(isl_map_copy(sink->access), sink->node,
2520 &before_node, data->n_source);
2521 access = add_matching_sources(access, sink, data);
2523 flow = access_info_compute_flow_core(access);
2524 if (!flow)
2525 return isl_union_flow_free(uf);
2527 map = isl_map_domain_factor_range(isl_flow_get_no_source(flow, 1));
2528 uf->must_no_source = isl_union_map_union(uf->must_no_source,
2529 isl_union_map_from_map(map));
2530 map = isl_map_domain_factor_range(isl_flow_get_no_source(flow, 0));
2531 uf->may_no_source = isl_union_map_union(uf->may_no_source,
2532 isl_union_map_from_map(map));
2534 for (i = 0; i < flow->n_source; ++i) {
2535 isl_union_map *dep;
2537 map = isl_map_range_curry(isl_map_copy(flow->dep[i].map));
2538 map = isl_map_factor_range(map);
2539 dep = isl_union_map_from_map(map);
2540 if (flow->dep[i].must)
2541 uf->must_dep = isl_union_map_union(uf->must_dep, dep);
2542 else
2543 uf->may_dep = isl_union_map_union(uf->may_dep, dep);
2546 isl_flow_free(flow);
2548 return uf;
2551 /* Given a description of the "sink" accesses, the "source" accesses and
2552 * a schedule, compute for each instance of a sink access
2553 * and for each element accessed by that instance,
2554 * the possible or definite source accesses that last accessed the
2555 * element accessed by the sink access before this sink access
2556 * in the sense that there is no intermediate definite source access.
2557 * Only consider dependences between statement instances that belong
2558 * to the domain of the schedule.
2560 * The must_no_source and may_no_source elements of the result
2561 * are subsets of access->sink. The elements must_dep and may_dep
2562 * map domain elements of access->{may,must)_source to
2563 * domain elements of access->sink.
2565 * This function is used when a schedule tree representation
2566 * is available.
2568 * We extract the individual scheduled source and sink access relations
2569 * (taking into account the domain of the schedule) and
2570 * then compute dependences for each scheduled sink individually.
2572 static __isl_give isl_union_flow *compute_flow_schedule(
2573 __isl_take isl_union_access_info *access)
2575 struct isl_compute_flow_schedule_data data = { access };
2576 int i, n;
2577 isl_ctx *ctx;
2578 isl_space *space;
2579 isl_union_flow *flow;
2581 ctx = isl_union_access_info_get_ctx(access);
2583 data.n_sink = 0;
2584 data.n_source = 0;
2585 if (isl_schedule_foreach_schedule_node_top_down(access->schedule,
2586 &count_sink_source, &data) < 0)
2587 goto error;
2589 n = data.n_sink + data.n_source;
2590 data.sink = isl_calloc_array(ctx, struct isl_scheduled_access, n);
2591 if (n && !data.sink)
2592 goto error;
2593 data.source = data.sink + data.n_sink;
2595 data.n_sink = 0;
2596 data.n_source = 0;
2597 if (isl_schedule_foreach_schedule_node_top_down(access->schedule,
2598 &collect_sink_source, &data) < 0)
2599 goto error;
2601 space = isl_union_map_get_space(access->access[isl_access_sink]);
2602 flow = isl_union_flow_alloc(space);
2604 isl_compute_flow_schedule_data_align_params(&data);
2606 for (i = 0; i < data.n_sink; ++i)
2607 flow = compute_single_flow(flow, &data.sink[i], &data);
2609 isl_compute_flow_schedule_data_clear(&data);
2611 isl_union_access_info_free(access);
2612 return flow;
2613 error:
2614 isl_union_access_info_free(access);
2615 isl_compute_flow_schedule_data_clear(&data);
2616 return NULL;
2619 /* Given a description of the "sink" accesses, the "source" accesses and
2620 * a schedule, compute for each instance of a sink access
2621 * and for each element accessed by that instance,
2622 * the possible or definite source accesses that last accessed the
2623 * element accessed by the sink access before this sink access
2624 * in the sense that there is no intermediate definite source access.
2626 * The must_no_source and may_no_source elements of the result
2627 * are subsets of access->sink. The elements must_dep and may_dep
2628 * map domain elements of access->{may,must)_source to
2629 * domain elements of access->sink.
2631 * We check whether the schedule is available as a schedule tree
2632 * or a schedule map and call the corresponding function to perform
2633 * the analysis.
2635 __isl_give isl_union_flow *isl_union_access_info_compute_flow(
2636 __isl_take isl_union_access_info *access)
2638 access = isl_union_access_info_normalize(access);
2639 if (!access)
2640 return NULL;
2641 if (access->schedule)
2642 return compute_flow_schedule(access);
2643 else
2644 return compute_flow_union_map(access);
2647 /* Print the information contained in "flow" to "p".
2648 * The information is printed as a YAML document.
2650 __isl_give isl_printer *isl_printer_print_union_flow(
2651 __isl_take isl_printer *p, __isl_keep isl_union_flow *flow)
2653 isl_union_map *umap;
2655 if (!flow)
2656 return isl_printer_free(p);
2658 p = isl_printer_yaml_start_mapping(p);
2659 umap = isl_union_flow_get_full_must_dependence(flow);
2660 p = print_union_map_field(p, "must_dependence", umap);
2661 isl_union_map_free(umap);
2662 umap = isl_union_flow_get_full_may_dependence(flow);
2663 p = print_union_map_field(p, "may_dependence", umap);
2664 isl_union_map_free(umap);
2665 p = print_union_map_field(p, "must_no_source", flow->must_no_source);
2666 umap = isl_union_flow_get_may_no_source(flow);
2667 p = print_union_map_field(p, "may_no_source", umap);
2668 isl_union_map_free(umap);
2669 p = isl_printer_yaml_end_mapping(p);
2671 return p;
2674 /* Return a string representation of the information in "flow".
2675 * The information is printed in flow format.
2677 __isl_give char *isl_union_flow_to_str(__isl_keep isl_union_flow *flow)
2679 isl_printer *p;
2680 char *s;
2682 if (!flow)
2683 return NULL;
2685 p = isl_printer_to_str(isl_union_flow_get_ctx(flow));
2686 p = isl_printer_set_yaml_style(p, ISL_YAML_STYLE_FLOW);
2687 p = isl_printer_print_union_flow(p, flow);
2688 s = isl_printer_get_str(p);
2689 isl_printer_free(p);
2691 return s;
2694 /* Given a collection of "sink" and "source" accesses,
2695 * compute for each iteration of a sink access
2696 * and for each element accessed by that iteration,
2697 * the source access in the list that last accessed the
2698 * element accessed by the sink access before this sink access.
2699 * Each access is given as a map from the loop iterators
2700 * to the array indices.
2701 * The result is a relations between source and sink
2702 * iterations and a subset of the domain of the sink accesses,
2703 * corresponding to those iterations that access an element
2704 * not previously accessed.
2706 * We collect the inputs in an isl_union_access_info object,
2707 * call isl_union_access_info_compute_flow and extract
2708 * the outputs from the result.
2710 int isl_union_map_compute_flow(__isl_take isl_union_map *sink,
2711 __isl_take isl_union_map *must_source,
2712 __isl_take isl_union_map *may_source,
2713 __isl_take isl_union_map *schedule,
2714 __isl_give isl_union_map **must_dep, __isl_give isl_union_map **may_dep,
2715 __isl_give isl_union_map **must_no_source,
2716 __isl_give isl_union_map **may_no_source)
2718 isl_union_access_info *access;
2719 isl_union_flow *flow;
2721 access = isl_union_access_info_from_sink(sink);
2722 access = isl_union_access_info_set_must_source(access, must_source);
2723 access = isl_union_access_info_set_may_source(access, may_source);
2724 access = isl_union_access_info_set_schedule_map(access, schedule);
2725 flow = isl_union_access_info_compute_flow(access);
2727 if (must_dep)
2728 *must_dep = isl_union_flow_get_must_dependence(flow);
2729 if (may_dep)
2730 *may_dep = isl_union_flow_get_non_must_dependence(flow);
2731 if (must_no_source)
2732 *must_no_source = isl_union_flow_get_must_no_source(flow);
2733 if (may_no_source)
2734 *may_no_source = isl_union_flow_get_non_must_no_source(flow);
2736 isl_union_flow_free(flow);
2738 if ((must_dep && !*must_dep) || (may_dep && !*may_dep) ||
2739 (must_no_source && !*must_no_source) ||
2740 (may_no_source && !*may_no_source))
2741 goto error;
2743 return 0;
2744 error:
2745 if (must_dep)
2746 *must_dep = isl_union_map_free(*must_dep);
2747 if (may_dep)
2748 *may_dep = isl_union_map_free(*may_dep);
2749 if (must_no_source)
2750 *must_no_source = isl_union_map_free(*must_no_source);
2751 if (may_no_source)
2752 *may_no_source = isl_union_map_free(*may_no_source);
2753 return -1;