Handle error conditions returned by level_before in isl_flow
[isl.git] / isl_flow.c
blob08d8651e66d88d3bcacb6846c020fd4231c7ceac
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 /* A helper struct carrying the isl_access_info and an error condition.
309 struct access_sort_info {
310 isl_access_info *access_info;
311 int error;
314 /* Return -n, 0 or n (with n a positive value), depending on whether
315 * the source access identified by p1 should be sorted before, together
316 * or after that identified by p2.
318 * If p1 appears before p2, then it should be sorted first.
319 * For more generic initial schedules, it is possible that neither
320 * p1 nor p2 appears before the other, or at least not in any obvious way.
321 * We therefore also check if p2 appears before p1, in which case p2
322 * should be sorted first.
323 * If not, we try to order the two statements based on the description
324 * of the iteration domains. This results in an arbitrary, but fairly
325 * stable ordering.
327 * In case of an error, sort_info.error is set to true and all elements are
328 * reported to be equal.
330 static int access_sort_cmp(const void *p1, const void *p2, void *user)
332 struct access_sort_info *sort_info = user;
333 isl_access_info *acc = sort_info->access_info;
335 if (sort_info->error)
336 return 0;
338 const struct isl_labeled_map *i1, *i2;
339 int level1, level2;
340 uint32_t h1, h2;
341 i1 = (const struct isl_labeled_map *) p1;
342 i2 = (const struct isl_labeled_map *) p2;
344 level1 = acc->level_before(i1->data, i2->data);
345 if (level1 < 0)
346 goto error;
347 if (level1 % 2)
348 return -1;
350 level2 = acc->level_before(i2->data, i1->data);
351 if (level2 < 0)
352 goto error;
353 if (level2 % 2)
354 return 1;
356 h1 = isl_map_get_hash(i1->map);
357 h2 = isl_map_get_hash(i2->map);
358 return h1 > h2 ? 1 : h1 < h2 ? -1 : 0;
359 error:
360 sort_info->error = 1;
361 return 0;
365 /* Sort the must source accesses in their textual order.
367 static __isl_give isl_access_info *isl_access_info_sort_sources(
368 __isl_take isl_access_info *acc)
370 struct access_sort_info sort_info;
372 sort_info.access_info = acc;
373 sort_info.error = 0;
375 if (!acc)
376 return NULL;
377 if (acc->n_must <= 1)
378 return acc;
380 if (isl_sort(acc->source, acc->n_must, sizeof(struct isl_labeled_map),
381 access_sort_cmp, &sort_info) < 0)
382 return isl_access_info_free(acc);
383 if (sort_info.error)
384 return isl_access_info_free(acc);
386 return acc;
389 /* Align the parameters of the two spaces if needed and then call
390 * isl_space_join.
392 static __isl_give isl_space *space_align_and_join(__isl_take isl_space *left,
393 __isl_take isl_space *right)
395 if (isl_space_match(left, isl_dim_param, right, isl_dim_param))
396 return isl_space_join(left, right);
398 left = isl_space_align_params(left, isl_space_copy(right));
399 right = isl_space_align_params(right, isl_space_copy(left));
400 return isl_space_join(left, right);
403 /* Initialize an empty isl_flow structure corresponding to a given
404 * isl_access_info structure.
405 * For each must access, two dependences are created (initialized
406 * to the empty relation), one for the resulting must dependences
407 * and one for the resulting may dependences. May accesses can
408 * only lead to may dependences, so only one dependence is created
409 * for each of them.
410 * This function is private as isl_flow structures are only supposed
411 * to be created by isl_access_info_compute_flow.
413 static __isl_give isl_flow *isl_flow_alloc(__isl_keep isl_access_info *acc)
415 int i, n;
416 struct isl_ctx *ctx;
417 struct isl_flow *dep;
419 if (!acc)
420 return NULL;
422 ctx = isl_map_get_ctx(acc->sink.map);
423 dep = isl_calloc_type(ctx, struct isl_flow);
424 if (!dep)
425 return NULL;
427 n = 2 * acc->n_must + acc->n_may;
428 dep->dep = isl_calloc_array(ctx, struct isl_labeled_map, n);
429 if (n && !dep->dep)
430 goto error;
432 dep->n_source = n;
433 for (i = 0; i < acc->n_must; ++i) {
434 isl_space *dim;
435 dim = space_align_and_join(
436 isl_map_get_space(acc->source[i].map),
437 isl_space_reverse(isl_map_get_space(acc->sink.map)));
438 dep->dep[2 * i].map = isl_map_empty(dim);
439 dep->dep[2 * i + 1].map = isl_map_copy(dep->dep[2 * i].map);
440 dep->dep[2 * i].data = acc->source[i].data;
441 dep->dep[2 * i + 1].data = acc->source[i].data;
442 dep->dep[2 * i].must = 1;
443 dep->dep[2 * i + 1].must = 0;
444 if (!dep->dep[2 * i].map || !dep->dep[2 * i + 1].map)
445 goto error;
447 for (i = acc->n_must; i < acc->n_must + acc->n_may; ++i) {
448 isl_space *dim;
449 dim = space_align_and_join(
450 isl_map_get_space(acc->source[i].map),
451 isl_space_reverse(isl_map_get_space(acc->sink.map)));
452 dep->dep[acc->n_must + i].map = isl_map_empty(dim);
453 dep->dep[acc->n_must + i].data = acc->source[i].data;
454 dep->dep[acc->n_must + i].must = 0;
455 if (!dep->dep[acc->n_must + i].map)
456 goto error;
459 return dep;
460 error:
461 isl_flow_free(dep);
462 return NULL;
465 /* Iterate over all sources and for each resulting flow dependence
466 * that is not empty, call the user specfied function.
467 * The second argument in this function call identifies the source,
468 * while the third argument correspond to the final argument of
469 * the isl_flow_foreach call.
471 isl_stat isl_flow_foreach(__isl_keep isl_flow *deps,
472 isl_stat (*fn)(__isl_take isl_map *dep, int must, void *dep_user,
473 void *user),
474 void *user)
476 int i;
478 if (!deps)
479 return isl_stat_error;
481 for (i = 0; i < deps->n_source; ++i) {
482 if (isl_map_plain_is_empty(deps->dep[i].map))
483 continue;
484 if (fn(isl_map_copy(deps->dep[i].map), deps->dep[i].must,
485 deps->dep[i].data, user) < 0)
486 return isl_stat_error;
489 return isl_stat_ok;
492 /* Return a copy of the subset of the sink for which no source could be found.
494 __isl_give isl_map *isl_flow_get_no_source(__isl_keep isl_flow *deps, int must)
496 if (!deps)
497 return NULL;
499 if (must)
500 return isl_set_unwrap(isl_set_copy(deps->must_no_source));
501 else
502 return isl_set_unwrap(isl_set_copy(deps->may_no_source));
505 void isl_flow_free(__isl_take isl_flow *deps)
507 int i;
509 if (!deps)
510 return;
511 isl_set_free(deps->must_no_source);
512 isl_set_free(deps->may_no_source);
513 if (deps->dep) {
514 for (i = 0; i < deps->n_source; ++i)
515 isl_map_free(deps->dep[i].map);
516 free(deps->dep);
518 free(deps);
521 isl_ctx *isl_flow_get_ctx(__isl_keep isl_flow *deps)
523 return deps ? isl_set_get_ctx(deps->must_no_source) : NULL;
526 /* Return a map that enforces that the domain iteration occurs after
527 * the range iteration at the given level.
528 * If level is odd, then the domain iteration should occur after
529 * the target iteration in their shared level/2 outermost loops.
530 * In this case we simply need to enforce that these outermost
531 * loop iterations are the same.
532 * If level is even, then the loop iterator of the domain should
533 * be greater than the loop iterator of the range at the last
534 * of the level/2 shared loops, i.e., loop level/2 - 1.
536 static __isl_give isl_map *after_at_level(__isl_take isl_space *dim, int level)
538 struct isl_basic_map *bmap;
540 if (level % 2)
541 bmap = isl_basic_map_equal(dim, level/2);
542 else
543 bmap = isl_basic_map_more_at(dim, level/2 - 1);
545 return isl_map_from_basic_map(bmap);
548 /* Compute the partial lexicographic maximum of "dep" on domain "sink",
549 * but first check if the user has set acc->restrict_fn and if so
550 * update either the input or the output of the maximization problem
551 * with respect to the resulting restriction.
553 * Since the user expects a mapping from sink iterations to source iterations,
554 * whereas the domain of "dep" is a wrapped map, mapping sink iterations
555 * to accessed array elements, we first need to project out the accessed
556 * sink array elements by applying acc->domain_map.
557 * Similarly, the sink restriction specified by the user needs to be
558 * converted back to the wrapped map.
560 static __isl_give isl_map *restricted_partial_lexmax(
561 __isl_keep isl_access_info *acc, __isl_take isl_map *dep,
562 int source, __isl_take isl_set *sink, __isl_give isl_set **empty)
564 isl_map *source_map;
565 isl_restriction *restr;
566 isl_set *sink_domain;
567 isl_set *sink_restr;
568 isl_map *res;
570 if (!acc->restrict_fn)
571 return isl_map_partial_lexmax(dep, sink, empty);
573 source_map = isl_map_copy(dep);
574 source_map = isl_map_apply_domain(source_map,
575 isl_map_copy(acc->domain_map));
576 sink_domain = isl_set_copy(sink);
577 sink_domain = isl_set_apply(sink_domain, isl_map_copy(acc->domain_map));
578 restr = acc->restrict_fn(source_map, sink_domain,
579 acc->source[source].data, acc->restrict_user);
580 isl_set_free(sink_domain);
581 isl_map_free(source_map);
583 if (!restr)
584 goto error;
585 if (restr->type == isl_restriction_type_input) {
586 dep = isl_map_intersect_range(dep, isl_set_copy(restr->source));
587 sink_restr = isl_set_copy(restr->sink);
588 sink_restr = isl_set_apply(sink_restr,
589 isl_map_reverse(isl_map_copy(acc->domain_map)));
590 sink = isl_set_intersect(sink, sink_restr);
591 } else if (restr->type == isl_restriction_type_empty) {
592 isl_space *space = isl_map_get_space(dep);
593 isl_map_free(dep);
594 dep = isl_map_empty(space);
597 res = isl_map_partial_lexmax(dep, sink, empty);
599 if (restr->type == isl_restriction_type_output)
600 res = isl_map_intersect_range(res, isl_set_copy(restr->source));
602 isl_restriction_free(restr);
603 return res;
604 error:
605 isl_map_free(dep);
606 isl_set_free(sink);
607 *empty = NULL;
608 return NULL;
611 /* Compute the last iteration of must source j that precedes the sink
612 * at the given level for sink iterations in set_C.
613 * The subset of set_C for which no such iteration can be found is returned
614 * in *empty.
616 static struct isl_map *last_source(struct isl_access_info *acc,
617 struct isl_set *set_C,
618 int j, int level, struct isl_set **empty)
620 struct isl_map *read_map;
621 struct isl_map *write_map;
622 struct isl_map *dep_map;
623 struct isl_map *after;
624 struct isl_map *result;
626 read_map = isl_map_copy(acc->sink.map);
627 write_map = isl_map_copy(acc->source[j].map);
628 write_map = isl_map_reverse(write_map);
629 dep_map = isl_map_apply_range(read_map, write_map);
630 after = after_at_level(isl_map_get_space(dep_map), level);
631 dep_map = isl_map_intersect(dep_map, after);
632 result = restricted_partial_lexmax(acc, dep_map, j, set_C, empty);
633 result = isl_map_reverse(result);
635 return result;
638 /* For a given mapping between iterations of must source j and iterations
639 * of the sink, compute the last iteration of must source k preceding
640 * the sink at level before_level for any of the sink iterations,
641 * but following the corresponding iteration of must source j at level
642 * after_level.
644 static struct isl_map *last_later_source(struct isl_access_info *acc,
645 struct isl_map *old_map,
646 int j, int before_level,
647 int k, int after_level,
648 struct isl_set **empty)
650 isl_space *dim;
651 struct isl_set *set_C;
652 struct isl_map *read_map;
653 struct isl_map *write_map;
654 struct isl_map *dep_map;
655 struct isl_map *after_write;
656 struct isl_map *before_read;
657 struct isl_map *result;
659 set_C = isl_map_range(isl_map_copy(old_map));
660 read_map = isl_map_copy(acc->sink.map);
661 write_map = isl_map_copy(acc->source[k].map);
663 write_map = isl_map_reverse(write_map);
664 dep_map = isl_map_apply_range(read_map, write_map);
665 dim = space_align_and_join(isl_map_get_space(acc->source[k].map),
666 isl_space_reverse(isl_map_get_space(acc->source[j].map)));
667 after_write = after_at_level(dim, after_level);
668 after_write = isl_map_apply_range(after_write, old_map);
669 after_write = isl_map_reverse(after_write);
670 dep_map = isl_map_intersect(dep_map, after_write);
671 before_read = after_at_level(isl_map_get_space(dep_map), before_level);
672 dep_map = isl_map_intersect(dep_map, before_read);
673 result = restricted_partial_lexmax(acc, dep_map, k, set_C, empty);
674 result = isl_map_reverse(result);
676 return result;
679 /* Given a shared_level between two accesses, return 1 if the
680 * the first can precede the second at the requested target_level.
681 * If the target level is odd, i.e., refers to a statement level
682 * dimension, then first needs to precede second at the requested
683 * level, i.e., shared_level must be equal to target_level.
684 * If the target level is odd, then the two loops should share
685 * at least the requested number of outer loops.
687 static int can_precede_at_level(int shared_level, int target_level)
689 if (shared_level < target_level)
690 return 0;
691 if ((target_level % 2) && shared_level > target_level)
692 return 0;
693 return 1;
696 /* Given a possible flow dependence temp_rel[j] between source j and the sink
697 * at level sink_level, remove those elements for which
698 * there is an iteration of another source k < j that is closer to the sink.
699 * The flow dependences temp_rel[k] are updated with the improved sources.
700 * Any improved source needs to precede the sink at the same level
701 * and needs to follow source j at the same or a deeper level.
702 * The lower this level, the later the execution date of source k.
703 * We therefore consider lower levels first.
705 * If temp_rel[j] is empty, then there can be no improvement and
706 * we return immediately.
708 * This function returns 0 in case it was executed successfully and
709 * -1 in case of errors during the execution of this function.
711 static int intermediate_sources(__isl_keep isl_access_info *acc,
712 struct isl_map **temp_rel, int j, int sink_level)
714 int k, level;
715 int depth = 2 * isl_map_dim(acc->source[j].map, isl_dim_in) + 1;
717 if (isl_map_plain_is_empty(temp_rel[j]))
718 return 0;
720 for (k = j - 1; k >= 0; --k) {
721 int plevel, plevel2;
722 plevel = acc->level_before(acc->source[k].data, acc->sink.data);
723 if (plevel < 0)
724 return -1;
725 if (!can_precede_at_level(plevel, sink_level))
726 continue;
728 plevel2 = acc->level_before(acc->source[j].data,
729 acc->source[k].data);
730 if (plevel2 < 0)
731 return -1;
733 for (level = sink_level; level <= depth; ++level) {
734 struct isl_map *T;
735 struct isl_set *trest;
736 struct isl_map *copy;
738 if (!can_precede_at_level(plevel2, level))
739 continue;
741 copy = isl_map_copy(temp_rel[j]);
742 T = last_later_source(acc, copy, j, sink_level, k,
743 level, &trest);
744 if (isl_map_plain_is_empty(T)) {
745 isl_set_free(trest);
746 isl_map_free(T);
747 continue;
749 temp_rel[j] = isl_map_intersect_range(temp_rel[j], trest);
750 temp_rel[k] = isl_map_union_disjoint(temp_rel[k], T);
754 return 0;
757 /* Compute all iterations of may source j that precedes the sink at the given
758 * level for sink iterations in set_C.
760 static __isl_give isl_map *all_sources(__isl_keep isl_access_info *acc,
761 __isl_take isl_set *set_C, int j, int level)
763 isl_map *read_map;
764 isl_map *write_map;
765 isl_map *dep_map;
766 isl_map *after;
768 read_map = isl_map_copy(acc->sink.map);
769 read_map = isl_map_intersect_domain(read_map, set_C);
770 write_map = isl_map_copy(acc->source[acc->n_must + j].map);
771 write_map = isl_map_reverse(write_map);
772 dep_map = isl_map_apply_range(read_map, write_map);
773 after = after_at_level(isl_map_get_space(dep_map), level);
774 dep_map = isl_map_intersect(dep_map, after);
776 return isl_map_reverse(dep_map);
779 /* For a given mapping between iterations of must source k and iterations
780 * of the sink, compute the all iteration of may source j preceding
781 * the sink at level before_level for any of the sink iterations,
782 * but following the corresponding iteration of must source k at level
783 * after_level.
785 static __isl_give isl_map *all_later_sources(__isl_keep isl_access_info *acc,
786 __isl_take isl_map *old_map,
787 int j, int before_level, int k, int after_level)
789 isl_space *dim;
790 isl_set *set_C;
791 isl_map *read_map;
792 isl_map *write_map;
793 isl_map *dep_map;
794 isl_map *after_write;
795 isl_map *before_read;
797 set_C = isl_map_range(isl_map_copy(old_map));
798 read_map = isl_map_copy(acc->sink.map);
799 read_map = isl_map_intersect_domain(read_map, set_C);
800 write_map = isl_map_copy(acc->source[acc->n_must + j].map);
802 write_map = isl_map_reverse(write_map);
803 dep_map = isl_map_apply_range(read_map, write_map);
804 dim = isl_space_join(isl_map_get_space(acc->source[acc->n_must + j].map),
805 isl_space_reverse(isl_map_get_space(acc->source[k].map)));
806 after_write = after_at_level(dim, after_level);
807 after_write = isl_map_apply_range(after_write, old_map);
808 after_write = isl_map_reverse(after_write);
809 dep_map = isl_map_intersect(dep_map, after_write);
810 before_read = after_at_level(isl_map_get_space(dep_map), before_level);
811 dep_map = isl_map_intersect(dep_map, before_read);
812 return isl_map_reverse(dep_map);
815 /* Given the must and may dependence relations for the must accesses
816 * for level sink_level, check if there are any accesses of may access j
817 * that occur in between and return their union.
818 * If some of these accesses are intermediate with respect to
819 * (previously thought to be) must dependences, then these
820 * must dependences are turned into may dependences.
822 static __isl_give isl_map *all_intermediate_sources(
823 __isl_keep isl_access_info *acc, __isl_take isl_map *map,
824 struct isl_map **must_rel, struct isl_map **may_rel,
825 int j, int sink_level)
827 int k, level;
828 int depth = 2 * isl_map_dim(acc->source[acc->n_must + j].map,
829 isl_dim_in) + 1;
831 for (k = 0; k < acc->n_must; ++k) {
832 int plevel;
834 if (isl_map_plain_is_empty(may_rel[k]) &&
835 isl_map_plain_is_empty(must_rel[k]))
836 continue;
838 plevel = acc->level_before(acc->source[k].data,
839 acc->source[acc->n_must + j].data);
840 if (plevel < 0)
841 return isl_map_free(map);
843 for (level = sink_level; level <= depth; ++level) {
844 isl_map *T;
845 isl_map *copy;
846 isl_set *ran;
848 if (!can_precede_at_level(plevel, level))
849 continue;
851 copy = isl_map_copy(may_rel[k]);
852 T = all_later_sources(acc, copy, j, sink_level, k, level);
853 map = isl_map_union(map, T);
855 copy = isl_map_copy(must_rel[k]);
856 T = all_later_sources(acc, copy, j, sink_level, k, level);
857 ran = isl_map_range(isl_map_copy(T));
858 map = isl_map_union(map, T);
859 may_rel[k] = isl_map_union_disjoint(may_rel[k],
860 isl_map_intersect_range(isl_map_copy(must_rel[k]),
861 isl_set_copy(ran)));
862 T = isl_map_from_domain_and_range(
863 isl_set_universe(
864 isl_space_domain(isl_map_get_space(must_rel[k]))),
865 ran);
866 must_rel[k] = isl_map_subtract(must_rel[k], T);
870 return map;
873 /* Compute dependences for the case where all accesses are "may"
874 * accesses, which boils down to computing memory based dependences.
875 * The generic algorithm would also work in this case, but it would
876 * be overkill to use it.
878 static __isl_give isl_flow *compute_mem_based_dependences(
879 __isl_keep isl_access_info *acc)
881 int i;
882 isl_set *mustdo;
883 isl_set *maydo;
884 isl_flow *res;
886 res = isl_flow_alloc(acc);
887 if (!res)
888 return NULL;
890 mustdo = isl_map_domain(isl_map_copy(acc->sink.map));
891 maydo = isl_set_copy(mustdo);
893 for (i = 0; i < acc->n_may; ++i) {
894 int plevel;
895 int is_before;
896 isl_space *dim;
897 isl_map *before;
898 isl_map *dep;
900 plevel = acc->level_before(acc->source[i].data, acc->sink.data);
901 if (plevel < 0)
902 goto error;
904 is_before = plevel & 1;
905 plevel >>= 1;
907 dim = isl_map_get_space(res->dep[i].map);
908 if (is_before)
909 before = isl_map_lex_le_first(dim, plevel);
910 else
911 before = isl_map_lex_lt_first(dim, plevel);
912 dep = isl_map_apply_range(isl_map_copy(acc->source[i].map),
913 isl_map_reverse(isl_map_copy(acc->sink.map)));
914 dep = isl_map_intersect(dep, before);
915 mustdo = isl_set_subtract(mustdo,
916 isl_map_range(isl_map_copy(dep)));
917 res->dep[i].map = isl_map_union(res->dep[i].map, dep);
920 res->may_no_source = isl_set_subtract(maydo, isl_set_copy(mustdo));
921 res->must_no_source = mustdo;
923 return res;
924 error:
925 isl_set_free(mustdo);
926 isl_set_free(maydo);
927 isl_flow_free(res);
928 return NULL;
931 /* Compute dependences for the case where there is at least one
932 * "must" access.
934 * The core algorithm considers all levels in which a source may precede
935 * the sink, where a level may either be a statement level or a loop level.
936 * The outermost statement level is 1, the first loop level is 2, etc...
937 * The algorithm basically does the following:
938 * for all levels l of the read access from innermost to outermost
939 * for all sources w that may precede the sink access at that level
940 * compute the last iteration of the source that precedes the sink access
941 * at that level
942 * add result to possible last accesses at level l of source w
943 * for all sources w2 that we haven't considered yet at this level that may
944 * also precede the sink access
945 * for all levels l2 of w from l to innermost
946 * for all possible last accesses dep of w at l
947 * compute last iteration of w2 between the source and sink
948 * of dep
949 * add result to possible last accesses at level l of write w2
950 * and replace possible last accesses dep by the remainder
953 * The above algorithm is applied to the must access. During the course
954 * of the algorithm, we keep track of sink iterations that still
955 * need to be considered. These iterations are split into those that
956 * haven't been matched to any source access (mustdo) and those that have only
957 * been matched to may accesses (maydo).
958 * At the end of each level, we also consider the may accesses.
959 * In particular, we consider may accesses that precede the remaining
960 * sink iterations, moving elements from mustdo to maydo when appropriate,
961 * and may accesses that occur between a must source and a sink of any
962 * dependences found at the current level, turning must dependences into
963 * may dependences when appropriate.
966 static __isl_give isl_flow *compute_val_based_dependences(
967 __isl_keep isl_access_info *acc)
969 isl_ctx *ctx;
970 isl_flow *res;
971 isl_set *mustdo = NULL;
972 isl_set *maydo = NULL;
973 int level, j;
974 int depth;
975 isl_map **must_rel = NULL;
976 isl_map **may_rel = NULL;
978 if (!acc)
979 return NULL;
981 res = isl_flow_alloc(acc);
982 if (!res)
983 goto error;
984 ctx = isl_map_get_ctx(acc->sink.map);
986 depth = 2 * isl_map_dim(acc->sink.map, isl_dim_in) + 1;
987 mustdo = isl_map_domain(isl_map_copy(acc->sink.map));
988 maydo = isl_set_empty(isl_set_get_space(mustdo));
989 if (!mustdo || !maydo)
990 goto error;
991 if (isl_set_plain_is_empty(mustdo))
992 goto done;
994 must_rel = isl_alloc_array(ctx, struct isl_map *, acc->n_must);
995 may_rel = isl_alloc_array(ctx, struct isl_map *, acc->n_must);
996 if (!must_rel || !may_rel)
997 goto error;
999 for (level = depth; level >= 1; --level) {
1000 for (j = acc->n_must-1; j >=0; --j) {
1001 isl_space *space;
1002 space = isl_map_get_space(res->dep[2 * j].map);
1003 must_rel[j] = isl_map_empty(space);
1004 may_rel[j] = isl_map_copy(must_rel[j]);
1007 for (j = acc->n_must - 1; j >= 0; --j) {
1008 struct isl_map *T;
1009 struct isl_set *rest;
1010 int plevel;
1012 plevel = acc->level_before(acc->source[j].data,
1013 acc->sink.data);
1014 if (plevel < 0)
1015 goto error;
1016 if (!can_precede_at_level(plevel, level))
1017 continue;
1019 T = last_source(acc, mustdo, j, level, &rest);
1020 must_rel[j] = isl_map_union_disjoint(must_rel[j], T);
1021 mustdo = rest;
1023 if (intermediate_sources(acc, must_rel, j, level))
1024 goto error;
1026 T = last_source(acc, maydo, j, level, &rest);
1027 may_rel[j] = isl_map_union_disjoint(may_rel[j], T);
1028 maydo = rest;
1030 if (intermediate_sources(acc, may_rel, j, level))
1031 goto error;
1033 if (isl_set_plain_is_empty(mustdo) &&
1034 isl_set_plain_is_empty(maydo))
1035 break;
1037 for (j = j - 1; j >= 0; --j) {
1038 int plevel;
1040 plevel = acc->level_before(acc->source[j].data,
1041 acc->sink.data);
1042 if (plevel < 0)
1043 goto error;
1044 if (!can_precede_at_level(plevel, level))
1045 continue;
1047 if (intermediate_sources(acc, must_rel, j, level))
1048 goto error;
1049 if (intermediate_sources(acc, may_rel, j, level))
1050 goto error;
1053 for (j = 0; j < acc->n_may; ++j) {
1054 int plevel;
1055 isl_map *T;
1056 isl_set *ran;
1058 plevel = acc->level_before(acc->source[acc->n_must + j].data,
1059 acc->sink.data);
1060 if (plevel < 0)
1061 goto error;
1062 if (!can_precede_at_level(plevel, level))
1063 continue;
1065 T = all_sources(acc, isl_set_copy(maydo), j, level);
1066 res->dep[2 * acc->n_must + j].map =
1067 isl_map_union(res->dep[2 * acc->n_must + j].map, T);
1068 T = all_sources(acc, isl_set_copy(mustdo), j, level);
1069 ran = isl_map_range(isl_map_copy(T));
1070 res->dep[2 * acc->n_must + j].map =
1071 isl_map_union(res->dep[2 * acc->n_must + j].map, T);
1072 mustdo = isl_set_subtract(mustdo, isl_set_copy(ran));
1073 maydo = isl_set_union_disjoint(maydo, ran);
1075 T = res->dep[2 * acc->n_must + j].map;
1076 T = all_intermediate_sources(acc, T, must_rel, may_rel,
1077 j, level);
1078 res->dep[2 * acc->n_must + j].map = T;
1081 for (j = acc->n_must - 1; j >= 0; --j) {
1082 res->dep[2 * j].map =
1083 isl_map_union_disjoint(res->dep[2 * j].map,
1084 must_rel[j]);
1085 res->dep[2 * j + 1].map =
1086 isl_map_union_disjoint(res->dep[2 * j + 1].map,
1087 may_rel[j]);
1090 if (isl_set_plain_is_empty(mustdo) &&
1091 isl_set_plain_is_empty(maydo))
1092 break;
1095 free(must_rel);
1096 free(may_rel);
1097 done:
1098 res->must_no_source = mustdo;
1099 res->may_no_source = maydo;
1100 return res;
1101 error:
1102 isl_flow_free(res);
1103 isl_set_free(mustdo);
1104 isl_set_free(maydo);
1105 free(must_rel);
1106 free(may_rel);
1107 return NULL;
1110 /* Given a "sink" access, a list of n "source" accesses,
1111 * compute for each iteration of the sink access
1112 * and for each element accessed by that iteration,
1113 * the source access in the list that last accessed the
1114 * element accessed by the sink access before this sink access.
1115 * Each access is given as a map from the loop iterators
1116 * to the array indices.
1117 * The result is a list of n relations between source and sink
1118 * iterations and a subset of the domain of the sink access,
1119 * corresponding to those iterations that access an element
1120 * not previously accessed.
1122 * To deal with multi-valued sink access relations, the sink iteration
1123 * domain is first extended with dimensions that correspond to the data
1124 * space. However, these extra dimensions are not projected out again.
1125 * It is up to the caller to decide whether these dimensions should be kept.
1127 static __isl_give isl_flow *access_info_compute_flow_core(
1128 __isl_take isl_access_info *acc)
1130 struct isl_flow *res = NULL;
1132 if (!acc)
1133 return NULL;
1135 acc->sink.map = isl_map_range_map(acc->sink.map);
1136 if (!acc->sink.map)
1137 goto error;
1139 if (acc->n_must == 0)
1140 res = compute_mem_based_dependences(acc);
1141 else {
1142 acc = isl_access_info_sort_sources(acc);
1143 res = compute_val_based_dependences(acc);
1145 acc = isl_access_info_free(acc);
1146 if (!res)
1147 return NULL;
1148 if (!res->must_no_source || !res->may_no_source)
1149 goto error;
1150 return res;
1151 error:
1152 isl_access_info_free(acc);
1153 isl_flow_free(res);
1154 return NULL;
1157 /* Given a "sink" access, a list of n "source" accesses,
1158 * compute for each iteration of the sink access
1159 * and for each element accessed by that iteration,
1160 * the source access in the list that last accessed the
1161 * element accessed by the sink access before this sink access.
1162 * Each access is given as a map from the loop iterators
1163 * to the array indices.
1164 * The result is a list of n relations between source and sink
1165 * iterations and a subset of the domain of the sink access,
1166 * corresponding to those iterations that access an element
1167 * not previously accessed.
1169 * To deal with multi-valued sink access relations,
1170 * access_info_compute_flow_core extends the sink iteration domain
1171 * with dimensions that correspond to the data space. These extra dimensions
1172 * are projected out from the result of access_info_compute_flow_core.
1174 __isl_give isl_flow *isl_access_info_compute_flow(__isl_take isl_access_info *acc)
1176 int j;
1177 struct isl_flow *res;
1179 if (!acc)
1180 return NULL;
1182 acc->domain_map = isl_map_domain_map(isl_map_copy(acc->sink.map));
1183 res = access_info_compute_flow_core(acc);
1184 if (!res)
1185 return NULL;
1187 for (j = 0; j < res->n_source; ++j) {
1188 res->dep[j].map = isl_map_range_factor_domain(res->dep[j].map);
1189 if (!res->dep[j].map)
1190 goto error;
1193 return res;
1194 error:
1195 isl_flow_free(res);
1196 return NULL;
1200 /* Keep track of some information about a schedule for a given
1201 * access. In particular, keep track of which dimensions
1202 * have a constant value and of the actual constant values.
1204 struct isl_sched_info {
1205 int *is_cst;
1206 isl_vec *cst;
1209 static void sched_info_free(__isl_take struct isl_sched_info *info)
1211 if (!info)
1212 return;
1213 isl_vec_free(info->cst);
1214 free(info->is_cst);
1215 free(info);
1218 /* Extract information on the constant dimensions of the schedule
1219 * for a given access. The "map" is of the form
1221 * [S -> D] -> A
1223 * with S the schedule domain, D the iteration domain and A the data domain.
1225 static __isl_give struct isl_sched_info *sched_info_alloc(
1226 __isl_keep isl_map *map)
1228 isl_ctx *ctx;
1229 isl_space *dim;
1230 struct isl_sched_info *info;
1231 int i, n;
1233 if (!map)
1234 return NULL;
1236 dim = isl_space_unwrap(isl_space_domain(isl_map_get_space(map)));
1237 if (!dim)
1238 return NULL;
1239 n = isl_space_dim(dim, isl_dim_in);
1240 isl_space_free(dim);
1242 ctx = isl_map_get_ctx(map);
1243 info = isl_alloc_type(ctx, struct isl_sched_info);
1244 if (!info)
1245 return NULL;
1246 info->is_cst = isl_alloc_array(ctx, int, n);
1247 info->cst = isl_vec_alloc(ctx, n);
1248 if (n && (!info->is_cst || !info->cst))
1249 goto error;
1251 for (i = 0; i < n; ++i) {
1252 isl_val *v;
1254 v = isl_map_plain_get_val_if_fixed(map, isl_dim_in, i);
1255 if (!v)
1256 goto error;
1257 info->is_cst[i] = !isl_val_is_nan(v);
1258 if (info->is_cst[i])
1259 info->cst = isl_vec_set_element_val(info->cst, i, v);
1260 else
1261 isl_val_free(v);
1264 return info;
1265 error:
1266 sched_info_free(info);
1267 return NULL;
1270 /* This structure represents the input for a dependence analysis computation.
1272 * "sink" represents the sink accesses.
1273 * "must_source" represents the definite source accesses.
1274 * "may_source" represents the possible source accesses.
1276 * "schedule" or "schedule_map" represents the execution order.
1277 * Exactly one of these fields should be NULL. The other field
1278 * determines the execution order.
1280 * The domains of these four maps refer to the same iteration spaces(s).
1281 * The ranges of the first three maps also refer to the same data space(s).
1283 * After a call to isl_union_access_info_introduce_schedule,
1284 * the "schedule_map" field no longer contains useful information.
1286 struct isl_union_access_info {
1287 isl_union_map *sink;
1288 isl_union_map *must_source;
1289 isl_union_map *may_source;
1291 isl_schedule *schedule;
1292 isl_union_map *schedule_map;
1295 /* Free "access" and return NULL.
1297 __isl_null isl_union_access_info *isl_union_access_info_free(
1298 __isl_take isl_union_access_info *access)
1300 if (!access)
1301 return NULL;
1303 isl_union_map_free(access->sink);
1304 isl_union_map_free(access->must_source);
1305 isl_union_map_free(access->may_source);
1306 isl_schedule_free(access->schedule);
1307 isl_union_map_free(access->schedule_map);
1308 free(access);
1310 return NULL;
1313 /* Return the isl_ctx to which "access" belongs.
1315 isl_ctx *isl_union_access_info_get_ctx(__isl_keep isl_union_access_info *access)
1317 return access ? isl_union_map_get_ctx(access->sink) : NULL;
1320 /* Create a new isl_union_access_info with the given sink accesses and
1321 * and no source accesses or schedule information.
1323 * By default, we use the schedule field of the isl_union_access_info,
1324 * but this may be overridden by a call
1325 * to isl_union_access_info_set_schedule_map.
1327 __isl_give isl_union_access_info *isl_union_access_info_from_sink(
1328 __isl_take isl_union_map *sink)
1330 isl_ctx *ctx;
1331 isl_space *space;
1332 isl_union_map *empty;
1333 isl_union_access_info *access;
1335 if (!sink)
1336 return NULL;
1337 ctx = isl_union_map_get_ctx(sink);
1338 access = isl_alloc_type(ctx, isl_union_access_info);
1339 if (!access)
1340 goto error;
1342 space = isl_union_map_get_space(sink);
1343 empty = isl_union_map_empty(isl_space_copy(space));
1344 access->sink = sink;
1345 access->must_source = isl_union_map_copy(empty);
1346 access->may_source = empty;
1347 access->schedule = isl_schedule_empty(space);
1348 access->schedule_map = NULL;
1350 if (!access->sink || !access->must_source ||
1351 !access->may_source || !access->schedule)
1352 return isl_union_access_info_free(access);
1354 return access;
1355 error:
1356 isl_union_map_free(sink);
1357 return NULL;
1360 /* Replace the definite source accesses of "access" by "must_source".
1362 __isl_give isl_union_access_info *isl_union_access_info_set_must_source(
1363 __isl_take isl_union_access_info *access,
1364 __isl_take isl_union_map *must_source)
1366 if (!access || !must_source)
1367 goto error;
1369 isl_union_map_free(access->must_source);
1370 access->must_source = must_source;
1372 return access;
1373 error:
1374 isl_union_access_info_free(access);
1375 isl_union_map_free(must_source);
1376 return NULL;
1379 /* Replace the possible source accesses of "access" by "may_source".
1381 __isl_give isl_union_access_info *isl_union_access_info_set_may_source(
1382 __isl_take isl_union_access_info *access,
1383 __isl_take isl_union_map *may_source)
1385 if (!access || !may_source)
1386 goto error;
1388 isl_union_map_free(access->may_source);
1389 access->may_source = may_source;
1391 return access;
1392 error:
1393 isl_union_access_info_free(access);
1394 isl_union_map_free(may_source);
1395 return NULL;
1398 /* Replace the schedule of "access" by "schedule".
1399 * Also free the schedule_map in case it was set last.
1401 __isl_give isl_union_access_info *isl_union_access_info_set_schedule(
1402 __isl_take isl_union_access_info *access,
1403 __isl_take isl_schedule *schedule)
1405 if (!access || !schedule)
1406 goto error;
1408 access->schedule_map = isl_union_map_free(access->schedule_map);
1409 isl_schedule_free(access->schedule);
1410 access->schedule = schedule;
1412 return access;
1413 error:
1414 isl_union_access_info_free(access);
1415 isl_schedule_free(schedule);
1416 return NULL;
1419 /* Replace the schedule map of "access" by "schedule_map".
1420 * Also free the schedule in case it was set last.
1422 __isl_give isl_union_access_info *isl_union_access_info_set_schedule_map(
1423 __isl_take isl_union_access_info *access,
1424 __isl_take isl_union_map *schedule_map)
1426 if (!access || !schedule_map)
1427 goto error;
1429 isl_union_map_free(access->schedule_map);
1430 access->schedule = isl_schedule_free(access->schedule);
1431 access->schedule_map = schedule_map;
1433 return access;
1434 error:
1435 isl_union_access_info_free(access);
1436 isl_union_map_free(schedule_map);
1437 return NULL;
1440 __isl_give isl_union_access_info *isl_union_access_info_copy(
1441 __isl_keep isl_union_access_info *access)
1443 isl_union_access_info *copy;
1445 if (!access)
1446 return NULL;
1447 copy = isl_union_access_info_from_sink(
1448 isl_union_map_copy(access->sink));
1449 copy = isl_union_access_info_set_must_source(copy,
1450 isl_union_map_copy(access->must_source));
1451 copy = isl_union_access_info_set_may_source(copy,
1452 isl_union_map_copy(access->may_source));
1453 if (access->schedule)
1454 copy = isl_union_access_info_set_schedule(copy,
1455 isl_schedule_copy(access->schedule));
1456 else
1457 copy = isl_union_access_info_set_schedule_map(copy,
1458 isl_union_map_copy(access->schedule_map));
1460 return copy;
1463 /* Print a key-value pair of a YAML mapping to "p",
1464 * with key "name" and value "umap".
1466 static __isl_give isl_printer *print_union_map_field(__isl_take isl_printer *p,
1467 const char *name, __isl_keep isl_union_map *umap)
1469 p = isl_printer_print_str(p, name);
1470 p = isl_printer_yaml_next(p);
1471 p = isl_printer_print_str(p, "\"");
1472 p = isl_printer_print_union_map(p, umap);
1473 p = isl_printer_print_str(p, "\"");
1474 p = isl_printer_yaml_next(p);
1476 return p;
1479 /* Print the information contained in "access" to "p".
1480 * The information is printed as a YAML document.
1482 __isl_give isl_printer *isl_printer_print_union_access_info(
1483 __isl_take isl_printer *p, __isl_keep isl_union_access_info *access)
1485 if (!access)
1486 return isl_printer_free(p);
1488 p = isl_printer_yaml_start_mapping(p);
1489 p = print_union_map_field(p, "sink", access->sink);
1490 p = print_union_map_field(p, "must_source", access->must_source);
1491 p = print_union_map_field(p, "may_source", access->may_source);
1492 if (access->schedule) {
1493 p = isl_printer_print_str(p, "schedule");
1494 p = isl_printer_yaml_next(p);
1495 p = isl_printer_print_schedule(p, access->schedule);
1496 p = isl_printer_yaml_next(p);
1497 } else {
1498 p = print_union_map_field(p, "schedule_map",
1499 access->schedule_map);
1501 p = isl_printer_yaml_end_mapping(p);
1503 return p;
1506 /* Return a string representation of the information in "access".
1507 * The information is printed in flow format.
1509 __isl_give char *isl_union_access_info_to_str(
1510 __isl_keep isl_union_access_info *access)
1512 isl_printer *p;
1513 char *s;
1515 if (!access)
1516 return NULL;
1518 p = isl_printer_to_str(isl_union_access_info_get_ctx(access));
1519 p = isl_printer_set_yaml_style(p, ISL_YAML_STYLE_FLOW);
1520 p = isl_printer_print_union_access_info(p, access);
1521 s = isl_printer_get_str(p);
1522 isl_printer_free(p);
1524 return s;
1527 /* Update the fields of "access" such that they all have the same parameters,
1528 * keeping in mind that the schedule_map field may be NULL and ignoring
1529 * the schedule field.
1531 static __isl_give isl_union_access_info *isl_union_access_info_align_params(
1532 __isl_take isl_union_access_info *access)
1534 isl_space *space;
1536 if (!access)
1537 return NULL;
1539 space = isl_union_map_get_space(access->sink);
1540 space = isl_space_align_params(space,
1541 isl_union_map_get_space(access->must_source));
1542 space = isl_space_align_params(space,
1543 isl_union_map_get_space(access->may_source));
1544 if (access->schedule_map)
1545 space = isl_space_align_params(space,
1546 isl_union_map_get_space(access->schedule_map));
1547 access->sink = isl_union_map_align_params(access->sink,
1548 isl_space_copy(space));
1549 access->must_source = isl_union_map_align_params(access->must_source,
1550 isl_space_copy(space));
1551 access->may_source = isl_union_map_align_params(access->may_source,
1552 isl_space_copy(space));
1553 if (!access->schedule_map) {
1554 isl_space_free(space);
1555 } else {
1556 access->schedule_map =
1557 isl_union_map_align_params(access->schedule_map, space);
1558 if (!access->schedule_map)
1559 return isl_union_access_info_free(access);
1562 if (!access->sink || !access->must_source || !access->may_source)
1563 return isl_union_access_info_free(access);
1565 return access;
1568 /* Prepend the schedule dimensions to the iteration domains.
1570 * That is, if the schedule is of the form
1572 * D -> S
1574 * while the access relations are of the form
1576 * D -> A
1578 * then the updated access relations are of the form
1580 * [S -> D] -> A
1582 * The schedule map is also replaced by the map
1584 * [S -> D] -> D
1586 * that is used during the internal computation.
1587 * Neither the original schedule map nor this updated schedule map
1588 * are used after the call to this function.
1590 static __isl_give isl_union_access_info *
1591 isl_union_access_info_introduce_schedule(
1592 __isl_take isl_union_access_info *access)
1594 isl_union_map *sm;
1596 if (!access)
1597 return NULL;
1599 sm = isl_union_map_reverse(access->schedule_map);
1600 sm = isl_union_map_range_map(sm);
1601 access->sink = isl_union_map_apply_range(isl_union_map_copy(sm),
1602 access->sink);
1603 access->may_source = isl_union_map_apply_range(isl_union_map_copy(sm),
1604 access->may_source);
1605 access->must_source = isl_union_map_apply_range(isl_union_map_copy(sm),
1606 access->must_source);
1607 access->schedule_map = sm;
1609 if (!access->sink || !access->must_source ||
1610 !access->may_source || !access->schedule_map)
1611 return isl_union_access_info_free(access);
1613 return access;
1616 /* This structure represents the result of a dependence analysis computation.
1618 * "must_dep" represents the full definite dependences
1619 * "may_dep" represents the full non-definite dependences.
1620 * Both are of the form
1622 * [Source] -> [[Sink -> Data]]
1624 * (after the schedule dimensions have been projected out).
1625 * "must_no_source" represents the subset of the sink accesses for which
1626 * definitely no source was found.
1627 * "may_no_source" represents the subset of the sink accesses for which
1628 * possibly, but not definitely, no source was found.
1630 struct isl_union_flow {
1631 isl_union_map *must_dep;
1632 isl_union_map *may_dep;
1633 isl_union_map *must_no_source;
1634 isl_union_map *may_no_source;
1637 /* Return the isl_ctx to which "flow" belongs.
1639 isl_ctx *isl_union_flow_get_ctx(__isl_keep isl_union_flow *flow)
1641 return flow ? isl_union_map_get_ctx(flow->must_dep) : NULL;
1644 /* Free "flow" and return NULL.
1646 __isl_null isl_union_flow *isl_union_flow_free(__isl_take isl_union_flow *flow)
1648 if (!flow)
1649 return NULL;
1650 isl_union_map_free(flow->must_dep);
1651 isl_union_map_free(flow->may_dep);
1652 isl_union_map_free(flow->must_no_source);
1653 isl_union_map_free(flow->may_no_source);
1654 free(flow);
1655 return NULL;
1658 void isl_union_flow_dump(__isl_keep isl_union_flow *flow)
1660 if (!flow)
1661 return;
1663 fprintf(stderr, "must dependences: ");
1664 isl_union_map_dump(flow->must_dep);
1665 fprintf(stderr, "may dependences: ");
1666 isl_union_map_dump(flow->may_dep);
1667 fprintf(stderr, "must no source: ");
1668 isl_union_map_dump(flow->must_no_source);
1669 fprintf(stderr, "may no source: ");
1670 isl_union_map_dump(flow->may_no_source);
1673 /* Return the full definite dependences in "flow", with accessed elements.
1675 __isl_give isl_union_map *isl_union_flow_get_full_must_dependence(
1676 __isl_keep isl_union_flow *flow)
1678 if (!flow)
1679 return NULL;
1680 return isl_union_map_copy(flow->must_dep);
1683 /* Return the full possible dependences in "flow", including the definite
1684 * dependences, with accessed elements.
1686 __isl_give isl_union_map *isl_union_flow_get_full_may_dependence(
1687 __isl_keep isl_union_flow *flow)
1689 if (!flow)
1690 return NULL;
1691 return isl_union_map_union(isl_union_map_copy(flow->must_dep),
1692 isl_union_map_copy(flow->may_dep));
1695 /* Return the definite dependences in "flow", without the accessed elements.
1697 __isl_give isl_union_map *isl_union_flow_get_must_dependence(
1698 __isl_keep isl_union_flow *flow)
1700 isl_union_map *dep;
1702 if (!flow)
1703 return NULL;
1704 dep = isl_union_map_copy(flow->must_dep);
1705 return isl_union_map_range_factor_domain(dep);
1708 /* Return the possible dependences in "flow", including the definite
1709 * dependences, without the accessed elements.
1711 __isl_give isl_union_map *isl_union_flow_get_may_dependence(
1712 __isl_keep isl_union_flow *flow)
1714 isl_union_map *dep;
1716 if (!flow)
1717 return NULL;
1718 dep = isl_union_map_union(isl_union_map_copy(flow->must_dep),
1719 isl_union_map_copy(flow->may_dep));
1720 return isl_union_map_range_factor_domain(dep);
1723 /* Return the non-definite dependences in "flow".
1725 static __isl_give isl_union_map *isl_union_flow_get_non_must_dependence(
1726 __isl_keep isl_union_flow *flow)
1728 if (!flow)
1729 return NULL;
1730 return isl_union_map_copy(flow->may_dep);
1733 /* Return the subset of the sink accesses for which definitely
1734 * no source was found.
1736 __isl_give isl_union_map *isl_union_flow_get_must_no_source(
1737 __isl_keep isl_union_flow *flow)
1739 if (!flow)
1740 return NULL;
1741 return isl_union_map_copy(flow->must_no_source);
1744 /* Return the subset of the sink accesses for which possibly
1745 * no source was found, including those for which definitely
1746 * no source was found.
1748 __isl_give isl_union_map *isl_union_flow_get_may_no_source(
1749 __isl_keep isl_union_flow *flow)
1751 if (!flow)
1752 return NULL;
1753 return isl_union_map_union(isl_union_map_copy(flow->must_no_source),
1754 isl_union_map_copy(flow->may_no_source));
1757 /* Return the subset of the sink accesses for which possibly, but not
1758 * definitely, no source was found.
1760 static __isl_give isl_union_map *isl_union_flow_get_non_must_no_source(
1761 __isl_keep isl_union_flow *flow)
1763 if (!flow)
1764 return NULL;
1765 return isl_union_map_copy(flow->may_no_source);
1768 /* Create a new isl_union_flow object, initialized with empty
1769 * dependence relations and sink subsets.
1771 static __isl_give isl_union_flow *isl_union_flow_alloc(
1772 __isl_take isl_space *space)
1774 isl_ctx *ctx;
1775 isl_union_map *empty;
1776 isl_union_flow *flow;
1778 if (!space)
1779 return NULL;
1780 ctx = isl_space_get_ctx(space);
1781 flow = isl_alloc_type(ctx, isl_union_flow);
1782 if (!flow)
1783 goto error;
1785 empty = isl_union_map_empty(space);
1786 flow->must_dep = isl_union_map_copy(empty);
1787 flow->may_dep = isl_union_map_copy(empty);
1788 flow->must_no_source = isl_union_map_copy(empty);
1789 flow->may_no_source = empty;
1791 if (!flow->must_dep || !flow->may_dep ||
1792 !flow->must_no_source || !flow->may_no_source)
1793 return isl_union_flow_free(flow);
1795 return flow;
1796 error:
1797 isl_space_free(space);
1798 return NULL;
1801 /* Copy this isl_union_flow object.
1803 __isl_give isl_union_flow *isl_union_flow_copy(__isl_keep isl_union_flow *flow)
1805 isl_union_flow *copy;
1807 if (!flow)
1808 return NULL;
1810 copy = isl_union_flow_alloc(isl_union_map_get_space(flow->must_dep));
1812 if (!copy)
1813 return NULL;
1815 copy->must_dep = isl_union_map_union(copy->must_dep,
1816 isl_union_map_copy(flow->must_dep));
1817 copy->may_dep = isl_union_map_union(copy->may_dep,
1818 isl_union_map_copy(flow->may_dep));
1819 copy->must_no_source = isl_union_map_union(copy->must_no_source,
1820 isl_union_map_copy(flow->must_no_source));
1821 copy->may_no_source = isl_union_map_union(copy->may_no_source,
1822 isl_union_map_copy(flow->may_no_source));
1824 if (!copy->must_dep || !copy->may_dep ||
1825 !copy->must_no_source || !copy->may_no_source)
1826 return isl_union_flow_free(copy);
1828 return copy;
1831 /* Drop the schedule dimensions from the iteration domains in "flow".
1832 * In particular, the schedule dimensions have been prepended
1833 * to the iteration domains prior to the dependence analysis by
1834 * replacing the iteration domain D, by the wrapped map [S -> D].
1835 * Replace these wrapped maps by the original D.
1837 * In particular, the dependences computed by access_info_compute_flow_core
1838 * are of the form
1840 * [S -> D] -> [[S' -> D'] -> A]
1842 * The schedule dimensions are projected out by first currying the range,
1843 * resulting in
1845 * [S -> D] -> [S' -> [D' -> A]]
1847 * and then computing the factor range
1849 * D -> [D' -> A]
1851 static __isl_give isl_union_flow *isl_union_flow_drop_schedule(
1852 __isl_take isl_union_flow *flow)
1854 if (!flow)
1855 return NULL;
1857 flow->must_dep = isl_union_map_range_curry(flow->must_dep);
1858 flow->must_dep = isl_union_map_factor_range(flow->must_dep);
1859 flow->may_dep = isl_union_map_range_curry(flow->may_dep);
1860 flow->may_dep = isl_union_map_factor_range(flow->may_dep);
1861 flow->must_no_source =
1862 isl_union_map_domain_factor_range(flow->must_no_source);
1863 flow->may_no_source =
1864 isl_union_map_domain_factor_range(flow->may_no_source);
1866 if (!flow->must_dep || !flow->may_dep ||
1867 !flow->must_no_source || !flow->may_no_source)
1868 return isl_union_flow_free(flow);
1870 return flow;
1873 struct isl_compute_flow_data {
1874 isl_union_map *must_source;
1875 isl_union_map *may_source;
1876 isl_union_flow *flow;
1878 int count;
1879 int must;
1880 isl_space *dim;
1881 struct isl_sched_info *sink_info;
1882 struct isl_sched_info **source_info;
1883 isl_access_info *accesses;
1886 static isl_stat count_matching_array(__isl_take isl_map *map, void *user)
1888 int eq;
1889 isl_space *dim;
1890 struct isl_compute_flow_data *data;
1892 data = (struct isl_compute_flow_data *)user;
1894 dim = isl_space_range(isl_map_get_space(map));
1896 eq = isl_space_is_equal(dim, data->dim);
1898 isl_space_free(dim);
1899 isl_map_free(map);
1901 if (eq < 0)
1902 return isl_stat_error;
1903 if (eq)
1904 data->count++;
1906 return isl_stat_ok;
1909 static isl_stat collect_matching_array(__isl_take isl_map *map, void *user)
1911 int eq;
1912 isl_space *dim;
1913 struct isl_sched_info *info;
1914 struct isl_compute_flow_data *data;
1916 data = (struct isl_compute_flow_data *)user;
1918 dim = isl_space_range(isl_map_get_space(map));
1920 eq = isl_space_is_equal(dim, data->dim);
1922 isl_space_free(dim);
1924 if (eq < 0)
1925 goto error;
1926 if (!eq) {
1927 isl_map_free(map);
1928 return isl_stat_ok;
1931 info = sched_info_alloc(map);
1932 data->source_info[data->count] = info;
1934 data->accesses = isl_access_info_add_source(data->accesses,
1935 map, data->must, info);
1937 data->count++;
1939 return isl_stat_ok;
1940 error:
1941 isl_map_free(map);
1942 return isl_stat_error;
1945 /* Determine the shared nesting level and the "textual order" of
1946 * the given accesses.
1948 * We first determine the minimal schedule dimension for both accesses.
1950 * If among those dimensions, we can find one where both have a fixed
1951 * value and if moreover those values are different, then the previous
1952 * dimension is the last shared nesting level and the textual order
1953 * is determined based on the order of the fixed values.
1954 * If no such fixed values can be found, then we set the shared
1955 * nesting level to the minimal schedule dimension, with no textual ordering.
1957 static int before(void *first, void *second)
1959 struct isl_sched_info *info1 = first;
1960 struct isl_sched_info *info2 = second;
1961 int n1, n2;
1962 int i;
1964 n1 = isl_vec_size(info1->cst);
1965 n2 = isl_vec_size(info2->cst);
1967 if (n2 < n1)
1968 n1 = n2;
1970 for (i = 0; i < n1; ++i) {
1971 int r;
1972 int cmp;
1974 if (!info1->is_cst[i])
1975 continue;
1976 if (!info2->is_cst[i])
1977 continue;
1978 cmp = isl_vec_cmp_element(info1->cst, info2->cst, i);
1979 if (cmp == 0)
1980 continue;
1982 r = 2 * i + (cmp < 0);
1984 return r;
1987 return 2 * n1;
1990 /* Given a sink access, look for all the source accesses that access
1991 * the same array and perform dataflow analysis on them using
1992 * isl_access_info_compute_flow_core.
1994 static isl_stat compute_flow(__isl_take isl_map *map, void *user)
1996 int i;
1997 isl_ctx *ctx;
1998 struct isl_compute_flow_data *data;
1999 isl_flow *flow;
2000 isl_union_flow *df;
2002 data = (struct isl_compute_flow_data *)user;
2003 df = data->flow;
2005 ctx = isl_map_get_ctx(map);
2007 data->accesses = NULL;
2008 data->sink_info = NULL;
2009 data->source_info = NULL;
2010 data->count = 0;
2011 data->dim = isl_space_range(isl_map_get_space(map));
2013 if (isl_union_map_foreach_map(data->must_source,
2014 &count_matching_array, data) < 0)
2015 goto error;
2016 if (isl_union_map_foreach_map(data->may_source,
2017 &count_matching_array, data) < 0)
2018 goto error;
2020 data->sink_info = sched_info_alloc(map);
2021 data->source_info = isl_calloc_array(ctx, struct isl_sched_info *,
2022 data->count);
2024 data->accesses = isl_access_info_alloc(isl_map_copy(map),
2025 data->sink_info, &before, data->count);
2026 if (!data->sink_info || (data->count && !data->source_info) ||
2027 !data->accesses)
2028 goto error;
2029 data->count = 0;
2030 data->must = 1;
2031 if (isl_union_map_foreach_map(data->must_source,
2032 &collect_matching_array, data) < 0)
2033 goto error;
2034 data->must = 0;
2035 if (isl_union_map_foreach_map(data->may_source,
2036 &collect_matching_array, data) < 0)
2037 goto error;
2039 flow = access_info_compute_flow_core(data->accesses);
2040 data->accesses = NULL;
2042 if (!flow)
2043 goto error;
2045 df->must_no_source = isl_union_map_union(df->must_no_source,
2046 isl_union_map_from_map(isl_flow_get_no_source(flow, 1)));
2047 df->may_no_source = isl_union_map_union(df->may_no_source,
2048 isl_union_map_from_map(isl_flow_get_no_source(flow, 0)));
2050 for (i = 0; i < flow->n_source; ++i) {
2051 isl_union_map *dep;
2052 dep = isl_union_map_from_map(isl_map_copy(flow->dep[i].map));
2053 if (flow->dep[i].must)
2054 df->must_dep = isl_union_map_union(df->must_dep, dep);
2055 else
2056 df->may_dep = isl_union_map_union(df->may_dep, dep);
2059 isl_flow_free(flow);
2061 sched_info_free(data->sink_info);
2062 if (data->source_info) {
2063 for (i = 0; i < data->count; ++i)
2064 sched_info_free(data->source_info[i]);
2065 free(data->source_info);
2067 isl_space_free(data->dim);
2068 isl_map_free(map);
2070 return isl_stat_ok;
2071 error:
2072 isl_access_info_free(data->accesses);
2073 sched_info_free(data->sink_info);
2074 if (data->source_info) {
2075 for (i = 0; i < data->count; ++i)
2076 sched_info_free(data->source_info[i]);
2077 free(data->source_info);
2079 isl_space_free(data->dim);
2080 isl_map_free(map);
2082 return isl_stat_error;
2085 /* Remove the must accesses from the may accesses.
2087 * A must access always trumps a may access, so there is no need
2088 * for a must access to also be considered as a may access. Doing so
2089 * would only cost extra computations only to find out that
2090 * the duplicated may access does not make any difference.
2092 static __isl_give isl_union_access_info *isl_union_access_info_normalize(
2093 __isl_take isl_union_access_info *access)
2095 if (!access)
2096 return NULL;
2097 access->may_source = isl_union_map_subtract(access->may_source,
2098 isl_union_map_copy(access->must_source));
2099 if (!access->may_source)
2100 return isl_union_access_info_free(access);
2102 return access;
2105 /* Given a description of the "sink" accesses, the "source" accesses and
2106 * a schedule, compute for each instance of a sink access
2107 * and for each element accessed by that instance,
2108 * the possible or definite source accesses that last accessed the
2109 * element accessed by the sink access before this sink access
2110 * in the sense that there is no intermediate definite source access.
2112 * The must_no_source and may_no_source elements of the result
2113 * are subsets of access->sink. The elements must_dep and may_dep
2114 * map domain elements of access->{may,must)_source to
2115 * domain elements of access->sink.
2117 * This function is used when only the schedule map representation
2118 * is available.
2120 * We first prepend the schedule dimensions to the domain
2121 * of the accesses so that we can easily compare their relative order.
2122 * Then we consider each sink access individually in compute_flow.
2124 static __isl_give isl_union_flow *compute_flow_union_map(
2125 __isl_take isl_union_access_info *access)
2127 struct isl_compute_flow_data data;
2129 access = isl_union_access_info_align_params(access);
2130 access = isl_union_access_info_introduce_schedule(access);
2131 if (!access)
2132 return NULL;
2134 data.must_source = access->must_source;
2135 data.may_source = access->may_source;
2137 data.flow = isl_union_flow_alloc(isl_union_map_get_space(access->sink));
2139 if (isl_union_map_foreach_map(access->sink, &compute_flow, &data) < 0)
2140 goto error;
2142 data.flow = isl_union_flow_drop_schedule(data.flow);
2144 isl_union_access_info_free(access);
2145 return data.flow;
2146 error:
2147 isl_union_access_info_free(access);
2148 isl_union_flow_free(data.flow);
2149 return NULL;
2152 /* A schedule access relation.
2154 * The access relation "access" is of the form [S -> D] -> A,
2155 * where S corresponds to the prefix schedule at "node".
2156 * "must" is only relevant for source accesses and indicates
2157 * whether the access is a must source or a may source.
2159 struct isl_scheduled_access {
2160 isl_map *access;
2161 int must;
2162 isl_schedule_node *node;
2165 /* Data structure for keeping track of individual scheduled sink and source
2166 * accesses when computing dependence analysis based on a schedule tree.
2168 * "n_sink" is the number of used entries in "sink"
2169 * "n_source" is the number of used entries in "source"
2171 * "set_sink", "must" and "node" are only used inside collect_sink_source,
2172 * to keep track of the current node and
2173 * of what extract_sink_source needs to do.
2175 struct isl_compute_flow_schedule_data {
2176 isl_union_access_info *access;
2178 int n_sink;
2179 int n_source;
2181 struct isl_scheduled_access *sink;
2182 struct isl_scheduled_access *source;
2184 int set_sink;
2185 int must;
2186 isl_schedule_node *node;
2189 /* Align the parameters of all sinks with all sources.
2191 * If there are no sinks or no sources, then no alignment is needed.
2193 static void isl_compute_flow_schedule_data_align_params(
2194 struct isl_compute_flow_schedule_data *data)
2196 int i;
2197 isl_space *space;
2199 if (data->n_sink == 0 || data->n_source == 0)
2200 return;
2202 space = isl_map_get_space(data->sink[0].access);
2204 for (i = 1; i < data->n_sink; ++i)
2205 space = isl_space_align_params(space,
2206 isl_map_get_space(data->sink[i].access));
2207 for (i = 0; i < data->n_source; ++i)
2208 space = isl_space_align_params(space,
2209 isl_map_get_space(data->source[i].access));
2211 for (i = 0; i < data->n_sink; ++i)
2212 data->sink[i].access =
2213 isl_map_align_params(data->sink[i].access,
2214 isl_space_copy(space));
2215 for (i = 0; i < data->n_source; ++i)
2216 data->source[i].access =
2217 isl_map_align_params(data->source[i].access,
2218 isl_space_copy(space));
2220 isl_space_free(space);
2223 /* Free all the memory referenced from "data".
2224 * Do not free "data" itself as it may be allocated on the stack.
2226 static void isl_compute_flow_schedule_data_clear(
2227 struct isl_compute_flow_schedule_data *data)
2229 int i;
2231 if (!data->sink)
2232 return;
2234 for (i = 0; i < data->n_sink; ++i) {
2235 isl_map_free(data->sink[i].access);
2236 isl_schedule_node_free(data->sink[i].node);
2239 for (i = 0; i < data->n_source; ++i) {
2240 isl_map_free(data->source[i].access);
2241 isl_schedule_node_free(data->source[i].node);
2244 free(data->sink);
2247 /* isl_schedule_foreach_schedule_node_top_down callback for counting
2248 * (an upper bound on) the number of sinks and sources.
2250 * Sinks and sources are only extracted at leaves of the tree,
2251 * so we skip the node if it is not a leaf.
2252 * Otherwise we increment data->n_sink and data->n_source with
2253 * the number of spaces in the sink and source access domains
2254 * that reach this node.
2256 static isl_bool count_sink_source(__isl_keep isl_schedule_node *node,
2257 void *user)
2259 struct isl_compute_flow_schedule_data *data = user;
2260 isl_union_set *domain;
2261 isl_union_map *umap;
2262 isl_bool r = isl_bool_false;
2264 if (isl_schedule_node_get_type(node) != isl_schedule_node_leaf)
2265 return isl_bool_true;
2267 domain = isl_schedule_node_get_universe_domain(node);
2269 umap = isl_union_map_copy(data->access->sink);
2270 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
2271 data->n_sink += isl_union_map_n_map(umap);
2272 isl_union_map_free(umap);
2273 if (!umap)
2274 r = isl_bool_error;
2276 umap = isl_union_map_copy(data->access->must_source);
2277 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
2278 data->n_source += isl_union_map_n_map(umap);
2279 isl_union_map_free(umap);
2280 if (!umap)
2281 r = isl_bool_error;
2283 umap = isl_union_map_copy(data->access->may_source);
2284 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
2285 data->n_source += isl_union_map_n_map(umap);
2286 isl_union_map_free(umap);
2287 if (!umap)
2288 r = isl_bool_error;
2290 isl_union_set_free(domain);
2292 return r;
2295 /* Add a single scheduled sink or source (depending on data->set_sink)
2296 * with scheduled access relation "map", must property data->must and
2297 * schedule node data->node to the list of sinks or sources.
2299 static isl_stat extract_sink_source(__isl_take isl_map *map, void *user)
2301 struct isl_compute_flow_schedule_data *data = user;
2302 struct isl_scheduled_access *access;
2304 if (data->set_sink)
2305 access = data->sink + data->n_sink++;
2306 else
2307 access = data->source + data->n_source++;
2309 access->access = map;
2310 access->must = data->must;
2311 access->node = isl_schedule_node_copy(data->node);
2313 return isl_stat_ok;
2316 /* isl_schedule_foreach_schedule_node_top_down callback for collecting
2317 * individual scheduled source and sink accesses (taking into account
2318 * the domain of the schedule).
2320 * We only collect accesses at the leaves of the schedule tree.
2321 * We prepend the schedule dimensions at the leaf to the iteration
2322 * domains of the source and sink accesses and then extract
2323 * the individual accesses (per space).
2325 * In particular, if the prefix schedule at the node is of the form
2327 * D -> S
2329 * while the access relations are of the form
2331 * D -> A
2333 * then the updated access relations are of the form
2335 * [S -> D] -> A
2337 * Note that S consists of a single space such that introducing S
2338 * in the access relations does not increase the number of spaces.
2340 static isl_bool collect_sink_source(__isl_keep isl_schedule_node *node,
2341 void *user)
2343 struct isl_compute_flow_schedule_data *data = user;
2344 isl_union_map *prefix;
2345 isl_union_map *umap;
2346 isl_bool r = isl_bool_false;
2348 if (isl_schedule_node_get_type(node) != isl_schedule_node_leaf)
2349 return isl_bool_true;
2351 data->node = node;
2353 prefix = isl_schedule_node_get_prefix_schedule_relation(node);
2354 prefix = isl_union_map_reverse(prefix);
2355 prefix = isl_union_map_range_map(prefix);
2357 data->set_sink = 1;
2358 umap = isl_union_map_copy(data->access->sink);
2359 umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
2360 if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
2361 r = isl_bool_error;
2362 isl_union_map_free(umap);
2364 data->set_sink = 0;
2365 data->must = 1;
2366 umap = isl_union_map_copy(data->access->must_source);
2367 umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
2368 if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
2369 r = isl_bool_error;
2370 isl_union_map_free(umap);
2372 data->set_sink = 0;
2373 data->must = 0;
2374 umap = isl_union_map_copy(data->access->may_source);
2375 umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
2376 if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
2377 r = isl_bool_error;
2378 isl_union_map_free(umap);
2380 isl_union_map_free(prefix);
2382 return r;
2385 /* isl_access_info_compute_flow callback for determining whether
2386 * the shared nesting level and the ordering within that level
2387 * for two scheduled accesses for use in compute_single_flow.
2389 * The tokens passed to this function refer to the leaves
2390 * in the schedule tree where the accesses take place.
2392 * If n is the shared number of loops, then we need to return
2393 * "2 * n + 1" if "first" precedes "second" inside the innermost
2394 * shared loop and "2 * n" otherwise.
2396 * The innermost shared ancestor may be the leaves themselves
2397 * if the accesses take place in the same leaf. Otherwise,
2398 * it is either a set node or a sequence node. Only in the case
2399 * of a sequence node do we consider one access to precede the other.
2401 static int before_node(void *first, void *second)
2403 isl_schedule_node *node1 = first;
2404 isl_schedule_node *node2 = second;
2405 isl_schedule_node *shared;
2406 int depth;
2407 int before = 0;
2409 shared = isl_schedule_node_get_shared_ancestor(node1, node2);
2410 if (!shared)
2411 return -1;
2413 depth = isl_schedule_node_get_schedule_depth(shared);
2414 if (isl_schedule_node_get_type(shared) == isl_schedule_node_sequence) {
2415 int pos1, pos2;
2417 pos1 = isl_schedule_node_get_ancestor_child_position(node1,
2418 shared);
2419 pos2 = isl_schedule_node_get_ancestor_child_position(node2,
2420 shared);
2421 before = pos1 < pos2;
2424 isl_schedule_node_free(shared);
2426 return 2 * depth + before;
2429 /* Add the scheduled sources from "data" that access
2430 * the same data space as "sink" to "access".
2432 static __isl_give isl_access_info *add_matching_sources(
2433 __isl_take isl_access_info *access, struct isl_scheduled_access *sink,
2434 struct isl_compute_flow_schedule_data *data)
2436 int i;
2437 isl_space *space;
2439 space = isl_space_range(isl_map_get_space(sink->access));
2440 for (i = 0; i < data->n_source; ++i) {
2441 struct isl_scheduled_access *source;
2442 isl_space *source_space;
2443 int eq;
2445 source = &data->source[i];
2446 source_space = isl_map_get_space(source->access);
2447 source_space = isl_space_range(source_space);
2448 eq = isl_space_is_equal(space, source_space);
2449 isl_space_free(source_space);
2451 if (!eq)
2452 continue;
2453 if (eq < 0)
2454 goto error;
2456 access = isl_access_info_add_source(access,
2457 isl_map_copy(source->access), source->must, source->node);
2460 isl_space_free(space);
2461 return access;
2462 error:
2463 isl_space_free(space);
2464 isl_access_info_free(access);
2465 return NULL;
2468 /* Given a scheduled sink access relation "sink", compute the corresponding
2469 * dependences on the sources in "data" and add the computed dependences
2470 * to "uf".
2472 * The dependences computed by access_info_compute_flow_core are of the form
2474 * [S -> I] -> [[S' -> I'] -> A]
2476 * The schedule dimensions are projected out by first currying the range,
2477 * resulting in
2479 * [S -> I] -> [S' -> [I' -> A]]
2481 * and then computing the factor range
2483 * I -> [I' -> A]
2485 static __isl_give isl_union_flow *compute_single_flow(
2486 __isl_take isl_union_flow *uf, struct isl_scheduled_access *sink,
2487 struct isl_compute_flow_schedule_data *data)
2489 int i;
2490 isl_access_info *access;
2491 isl_flow *flow;
2492 isl_map *map;
2494 if (!uf)
2495 return NULL;
2497 access = isl_access_info_alloc(isl_map_copy(sink->access), sink->node,
2498 &before_node, data->n_source);
2499 access = add_matching_sources(access, sink, data);
2501 flow = access_info_compute_flow_core(access);
2502 if (!flow)
2503 return isl_union_flow_free(uf);
2505 map = isl_map_domain_factor_range(isl_flow_get_no_source(flow, 1));
2506 uf->must_no_source = isl_union_map_union(uf->must_no_source,
2507 isl_union_map_from_map(map));
2508 map = isl_map_domain_factor_range(isl_flow_get_no_source(flow, 0));
2509 uf->may_no_source = isl_union_map_union(uf->may_no_source,
2510 isl_union_map_from_map(map));
2512 for (i = 0; i < flow->n_source; ++i) {
2513 isl_union_map *dep;
2515 map = isl_map_range_curry(isl_map_copy(flow->dep[i].map));
2516 map = isl_map_factor_range(map);
2517 dep = isl_union_map_from_map(map);
2518 if (flow->dep[i].must)
2519 uf->must_dep = isl_union_map_union(uf->must_dep, dep);
2520 else
2521 uf->may_dep = isl_union_map_union(uf->may_dep, dep);
2524 isl_flow_free(flow);
2526 return uf;
2529 /* Given a description of the "sink" accesses, the "source" accesses and
2530 * a schedule, compute for each instance of a sink access
2531 * and for each element accessed by that instance,
2532 * the possible or definite source accesses that last accessed the
2533 * element accessed by the sink access before this sink access
2534 * in the sense that there is no intermediate definite source access.
2535 * Only consider dependences between statement instances that belong
2536 * to the domain of the schedule.
2538 * The must_no_source and may_no_source elements of the result
2539 * are subsets of access->sink. The elements must_dep and may_dep
2540 * map domain elements of access->{may,must)_source to
2541 * domain elements of access->sink.
2543 * This function is used when a schedule tree representation
2544 * is available.
2546 * We extract the individual scheduled source and sink access relations
2547 * (taking into account the domain of the schedule) and
2548 * then compute dependences for each scheduled sink individually.
2550 static __isl_give isl_union_flow *compute_flow_schedule(
2551 __isl_take isl_union_access_info *access)
2553 struct isl_compute_flow_schedule_data data = { access };
2554 int i, n;
2555 isl_ctx *ctx;
2556 isl_union_flow *flow;
2558 ctx = isl_union_access_info_get_ctx(access);
2560 data.n_sink = 0;
2561 data.n_source = 0;
2562 if (isl_schedule_foreach_schedule_node_top_down(access->schedule,
2563 &count_sink_source, &data) < 0)
2564 goto error;
2566 n = data.n_sink + data.n_source;
2567 data.sink = isl_calloc_array(ctx, struct isl_scheduled_access, n);
2568 if (n && !data.sink)
2569 goto error;
2570 data.source = data.sink + data.n_sink;
2572 data.n_sink = 0;
2573 data.n_source = 0;
2574 if (isl_schedule_foreach_schedule_node_top_down(access->schedule,
2575 &collect_sink_source, &data) < 0)
2576 goto error;
2578 flow = isl_union_flow_alloc(isl_union_map_get_space(access->sink));
2580 isl_compute_flow_schedule_data_align_params(&data);
2582 for (i = 0; i < data.n_sink; ++i)
2583 flow = compute_single_flow(flow, &data.sink[i], &data);
2585 isl_compute_flow_schedule_data_clear(&data);
2587 isl_union_access_info_free(access);
2588 return flow;
2589 error:
2590 isl_union_access_info_free(access);
2591 isl_compute_flow_schedule_data_clear(&data);
2592 return NULL;
2595 /* Given a description of the "sink" accesses, the "source" accesses and
2596 * a schedule, compute for each instance of a sink access
2597 * and for each element accessed by that instance,
2598 * the possible or definite source accesses that last accessed the
2599 * element accessed by the sink access before this sink access
2600 * in the sense that there is no intermediate definite source access.
2602 * The must_no_source and may_no_source elements of the result
2603 * are subsets of access->sink. The elements must_dep and may_dep
2604 * map domain elements of access->{may,must)_source to
2605 * domain elements of access->sink.
2607 * We check whether the schedule is available as a schedule tree
2608 * or a schedule map and call the correpsonding function to perform
2609 * the analysis.
2611 __isl_give isl_union_flow *isl_union_access_info_compute_flow(
2612 __isl_take isl_union_access_info *access)
2614 access = isl_union_access_info_normalize(access);
2615 if (!access)
2616 return NULL;
2617 if (access->schedule)
2618 return compute_flow_schedule(access);
2619 else
2620 return compute_flow_union_map(access);
2623 /* Print the information contained in "flow" to "p".
2624 * The information is printed as a YAML document.
2626 __isl_give isl_printer *isl_printer_print_union_flow(
2627 __isl_take isl_printer *p, __isl_keep isl_union_flow *flow)
2629 isl_union_map *umap;
2631 if (!flow)
2632 return isl_printer_free(p);
2634 p = isl_printer_yaml_start_mapping(p);
2635 p = print_union_map_field(p, "must_dependence", flow->must_dep);
2636 umap = isl_union_flow_get_may_dependence(flow);
2637 p = print_union_map_field(p, "may_dependence", umap);
2638 isl_union_map_free(umap);
2639 p = print_union_map_field(p, "must_no_source", flow->must_no_source);
2640 umap = isl_union_flow_get_may_no_source(flow);
2641 p = print_union_map_field(p, "may_no_source", umap);
2642 isl_union_map_free(umap);
2643 p = isl_printer_yaml_end_mapping(p);
2645 return p;
2648 /* Return a string representation of the information in "flow".
2649 * The information is printed in flow format.
2651 __isl_give char *isl_union_flow_to_str(__isl_keep isl_union_flow *flow)
2653 isl_printer *p;
2654 char *s;
2656 if (!flow)
2657 return NULL;
2659 p = isl_printer_to_str(isl_union_flow_get_ctx(flow));
2660 p = isl_printer_set_yaml_style(p, ISL_YAML_STYLE_FLOW);
2661 p = isl_printer_print_union_flow(p, flow);
2662 s = isl_printer_get_str(p);
2663 isl_printer_free(p);
2665 return s;
2668 /* Given a collection of "sink" and "source" accesses,
2669 * compute for each iteration of a sink access
2670 * and for each element accessed by that iteration,
2671 * the source access in the list that last accessed the
2672 * element accessed by the sink access before this sink access.
2673 * Each access is given as a map from the loop iterators
2674 * to the array indices.
2675 * The result is a relations between source and sink
2676 * iterations and a subset of the domain of the sink accesses,
2677 * corresponding to those iterations that access an element
2678 * not previously accessed.
2680 * We collect the inputs in an isl_union_access_info object,
2681 * call isl_union_access_info_compute_flow and extract
2682 * the outputs from the result.
2684 int isl_union_map_compute_flow(__isl_take isl_union_map *sink,
2685 __isl_take isl_union_map *must_source,
2686 __isl_take isl_union_map *may_source,
2687 __isl_take isl_union_map *schedule,
2688 __isl_give isl_union_map **must_dep, __isl_give isl_union_map **may_dep,
2689 __isl_give isl_union_map **must_no_source,
2690 __isl_give isl_union_map **may_no_source)
2692 isl_union_access_info *access;
2693 isl_union_flow *flow;
2695 access = isl_union_access_info_from_sink(sink);
2696 access = isl_union_access_info_set_must_source(access, must_source);
2697 access = isl_union_access_info_set_may_source(access, may_source);
2698 access = isl_union_access_info_set_schedule_map(access, schedule);
2699 flow = isl_union_access_info_compute_flow(access);
2701 if (must_dep)
2702 *must_dep = isl_union_flow_get_must_dependence(flow);
2703 if (may_dep)
2704 *may_dep = isl_union_flow_get_non_must_dependence(flow);
2705 if (must_no_source)
2706 *must_no_source = isl_union_flow_get_must_no_source(flow);
2707 if (may_no_source)
2708 *may_no_source = isl_union_flow_get_non_must_no_source(flow);
2710 isl_union_flow_free(flow);
2712 if ((must_dep && !*must_dep) || (may_dep && !*may_dep) ||
2713 (must_no_source && !*must_no_source) ||
2714 (may_no_source && !*may_no_source))
2715 goto error;
2717 return 0;
2718 error:
2719 if (must_dep)
2720 *must_dep = isl_union_map_free(*must_dep);
2721 if (may_dep)
2722 *may_dep = isl_union_map_free(*may_dep);
2723 if (must_no_source)
2724 *must_no_source = isl_union_map_free(*must_no_source);
2725 if (may_no_source)
2726 *may_no_source = isl_union_map_free(*may_no_source);
2727 return -1;