isl_ast_build_has_affine_value: return isl_bool
[isl.git] / isl_flow.c
blob521e2c90734d3453a52c5b669e86f2ba944d866b
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_calloc_array(ctx, struct isl_map *, acc->n_must);
995 may_rel = isl_calloc_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 if (must_rel)
1103 for (j = 0; j < acc->n_must; ++j)
1104 isl_map_free(must_rel[j]);
1105 if (may_rel)
1106 for (j = 0; j < acc->n_must; ++j)
1107 isl_map_free(may_rel[j]);
1108 isl_flow_free(res);
1109 isl_set_free(mustdo);
1110 isl_set_free(maydo);
1111 free(must_rel);
1112 free(may_rel);
1113 return NULL;
1116 /* Given a "sink" access, a list of n "source" accesses,
1117 * compute for each iteration of the sink access
1118 * and for each element accessed by that iteration,
1119 * the source access in the list that last accessed the
1120 * element accessed by the sink access before this sink access.
1121 * Each access is given as a map from the loop iterators
1122 * to the array indices.
1123 * The result is a list of n relations between source and sink
1124 * iterations and a subset of the domain of the sink access,
1125 * corresponding to those iterations that access an element
1126 * not previously accessed.
1128 * To deal with multi-valued sink access relations, the sink iteration
1129 * domain is first extended with dimensions that correspond to the data
1130 * space. However, these extra dimensions are not projected out again.
1131 * It is up to the caller to decide whether these dimensions should be kept.
1133 static __isl_give isl_flow *access_info_compute_flow_core(
1134 __isl_take isl_access_info *acc)
1136 struct isl_flow *res = NULL;
1138 if (!acc)
1139 return NULL;
1141 acc->sink.map = isl_map_range_map(acc->sink.map);
1142 if (!acc->sink.map)
1143 goto error;
1145 if (acc->n_must == 0)
1146 res = compute_mem_based_dependences(acc);
1147 else {
1148 acc = isl_access_info_sort_sources(acc);
1149 res = compute_val_based_dependences(acc);
1151 acc = isl_access_info_free(acc);
1152 if (!res)
1153 return NULL;
1154 if (!res->must_no_source || !res->may_no_source)
1155 goto error;
1156 return res;
1157 error:
1158 isl_access_info_free(acc);
1159 isl_flow_free(res);
1160 return NULL;
1163 /* Given a "sink" access, a list of n "source" accesses,
1164 * compute for each iteration of the sink access
1165 * and for each element accessed by that iteration,
1166 * the source access in the list that last accessed the
1167 * element accessed by the sink access before this sink access.
1168 * Each access is given as a map from the loop iterators
1169 * to the array indices.
1170 * The result is a list of n relations between source and sink
1171 * iterations and a subset of the domain of the sink access,
1172 * corresponding to those iterations that access an element
1173 * not previously accessed.
1175 * To deal with multi-valued sink access relations,
1176 * access_info_compute_flow_core extends the sink iteration domain
1177 * with dimensions that correspond to the data space. These extra dimensions
1178 * are projected out from the result of access_info_compute_flow_core.
1180 __isl_give isl_flow *isl_access_info_compute_flow(__isl_take isl_access_info *acc)
1182 int j;
1183 struct isl_flow *res;
1185 if (!acc)
1186 return NULL;
1188 acc->domain_map = isl_map_domain_map(isl_map_copy(acc->sink.map));
1189 res = access_info_compute_flow_core(acc);
1190 if (!res)
1191 return NULL;
1193 for (j = 0; j < res->n_source; ++j) {
1194 res->dep[j].map = isl_map_range_factor_domain(res->dep[j].map);
1195 if (!res->dep[j].map)
1196 goto error;
1199 return res;
1200 error:
1201 isl_flow_free(res);
1202 return NULL;
1206 /* Keep track of some information about a schedule for a given
1207 * access. In particular, keep track of which dimensions
1208 * have a constant value and of the actual constant values.
1210 struct isl_sched_info {
1211 int *is_cst;
1212 isl_vec *cst;
1215 static void sched_info_free(__isl_take struct isl_sched_info *info)
1217 if (!info)
1218 return;
1219 isl_vec_free(info->cst);
1220 free(info->is_cst);
1221 free(info);
1224 /* Extract information on the constant dimensions of the schedule
1225 * for a given access. The "map" is of the form
1227 * [S -> D] -> A
1229 * with S the schedule domain, D the iteration domain and A the data domain.
1231 static __isl_give struct isl_sched_info *sched_info_alloc(
1232 __isl_keep isl_map *map)
1234 isl_ctx *ctx;
1235 isl_space *dim;
1236 struct isl_sched_info *info;
1237 int i, n;
1239 if (!map)
1240 return NULL;
1242 dim = isl_space_unwrap(isl_space_domain(isl_map_get_space(map)));
1243 if (!dim)
1244 return NULL;
1245 n = isl_space_dim(dim, isl_dim_in);
1246 isl_space_free(dim);
1248 ctx = isl_map_get_ctx(map);
1249 info = isl_alloc_type(ctx, struct isl_sched_info);
1250 if (!info)
1251 return NULL;
1252 info->is_cst = isl_alloc_array(ctx, int, n);
1253 info->cst = isl_vec_alloc(ctx, n);
1254 if (n && (!info->is_cst || !info->cst))
1255 goto error;
1257 for (i = 0; i < n; ++i) {
1258 isl_val *v;
1260 v = isl_map_plain_get_val_if_fixed(map, isl_dim_in, i);
1261 if (!v)
1262 goto error;
1263 info->is_cst[i] = !isl_val_is_nan(v);
1264 if (info->is_cst[i])
1265 info->cst = isl_vec_set_element_val(info->cst, i, v);
1266 else
1267 isl_val_free(v);
1270 return info;
1271 error:
1272 sched_info_free(info);
1273 return NULL;
1276 /* This structure represents the input for a dependence analysis computation.
1278 * "sink" represents the sink accesses.
1279 * "must_source" represents the definite source accesses.
1280 * "may_source" represents the possible source accesses.
1282 * "schedule" or "schedule_map" represents the execution order.
1283 * Exactly one of these fields should be NULL. The other field
1284 * determines the execution order.
1286 * The domains of these four maps refer to the same iteration spaces(s).
1287 * The ranges of the first three maps also refer to the same data space(s).
1289 * After a call to isl_union_access_info_introduce_schedule,
1290 * the "schedule_map" field no longer contains useful information.
1292 struct isl_union_access_info {
1293 isl_union_map *sink;
1294 isl_union_map *must_source;
1295 isl_union_map *may_source;
1297 isl_schedule *schedule;
1298 isl_union_map *schedule_map;
1301 /* Free "access" and return NULL.
1303 __isl_null isl_union_access_info *isl_union_access_info_free(
1304 __isl_take isl_union_access_info *access)
1306 if (!access)
1307 return NULL;
1309 isl_union_map_free(access->sink);
1310 isl_union_map_free(access->must_source);
1311 isl_union_map_free(access->may_source);
1312 isl_schedule_free(access->schedule);
1313 isl_union_map_free(access->schedule_map);
1314 free(access);
1316 return NULL;
1319 /* Return the isl_ctx to which "access" belongs.
1321 isl_ctx *isl_union_access_info_get_ctx(__isl_keep isl_union_access_info *access)
1323 return access ? isl_union_map_get_ctx(access->sink) : NULL;
1326 /* Create a new isl_union_access_info with the given sink accesses and
1327 * and no source accesses or schedule information.
1329 * By default, we use the schedule field of the isl_union_access_info,
1330 * but this may be overridden by a call
1331 * to isl_union_access_info_set_schedule_map.
1333 __isl_give isl_union_access_info *isl_union_access_info_from_sink(
1334 __isl_take isl_union_map *sink)
1336 isl_ctx *ctx;
1337 isl_space *space;
1338 isl_union_map *empty;
1339 isl_union_access_info *access;
1341 if (!sink)
1342 return NULL;
1343 ctx = isl_union_map_get_ctx(sink);
1344 access = isl_alloc_type(ctx, isl_union_access_info);
1345 if (!access)
1346 goto error;
1348 space = isl_union_map_get_space(sink);
1349 empty = isl_union_map_empty(isl_space_copy(space));
1350 access->sink = sink;
1351 access->must_source = isl_union_map_copy(empty);
1352 access->may_source = empty;
1353 access->schedule = isl_schedule_empty(space);
1354 access->schedule_map = NULL;
1356 if (!access->sink || !access->must_source ||
1357 !access->may_source || !access->schedule)
1358 return isl_union_access_info_free(access);
1360 return access;
1361 error:
1362 isl_union_map_free(sink);
1363 return NULL;
1366 /* Replace the definite source accesses of "access" by "must_source".
1368 __isl_give isl_union_access_info *isl_union_access_info_set_must_source(
1369 __isl_take isl_union_access_info *access,
1370 __isl_take isl_union_map *must_source)
1372 if (!access || !must_source)
1373 goto error;
1375 isl_union_map_free(access->must_source);
1376 access->must_source = must_source;
1378 return access;
1379 error:
1380 isl_union_access_info_free(access);
1381 isl_union_map_free(must_source);
1382 return NULL;
1385 /* Replace the possible source accesses of "access" by "may_source".
1387 __isl_give isl_union_access_info *isl_union_access_info_set_may_source(
1388 __isl_take isl_union_access_info *access,
1389 __isl_take isl_union_map *may_source)
1391 if (!access || !may_source)
1392 goto error;
1394 isl_union_map_free(access->may_source);
1395 access->may_source = may_source;
1397 return access;
1398 error:
1399 isl_union_access_info_free(access);
1400 isl_union_map_free(may_source);
1401 return NULL;
1404 /* Replace the schedule of "access" by "schedule".
1405 * Also free the schedule_map in case it was set last.
1407 __isl_give isl_union_access_info *isl_union_access_info_set_schedule(
1408 __isl_take isl_union_access_info *access,
1409 __isl_take isl_schedule *schedule)
1411 if (!access || !schedule)
1412 goto error;
1414 access->schedule_map = isl_union_map_free(access->schedule_map);
1415 isl_schedule_free(access->schedule);
1416 access->schedule = schedule;
1418 return access;
1419 error:
1420 isl_union_access_info_free(access);
1421 isl_schedule_free(schedule);
1422 return NULL;
1425 /* Replace the schedule map of "access" by "schedule_map".
1426 * Also free the schedule in case it was set last.
1428 __isl_give isl_union_access_info *isl_union_access_info_set_schedule_map(
1429 __isl_take isl_union_access_info *access,
1430 __isl_take isl_union_map *schedule_map)
1432 if (!access || !schedule_map)
1433 goto error;
1435 isl_union_map_free(access->schedule_map);
1436 access->schedule = isl_schedule_free(access->schedule);
1437 access->schedule_map = schedule_map;
1439 return access;
1440 error:
1441 isl_union_access_info_free(access);
1442 isl_union_map_free(schedule_map);
1443 return NULL;
1446 __isl_give isl_union_access_info *isl_union_access_info_copy(
1447 __isl_keep isl_union_access_info *access)
1449 isl_union_access_info *copy;
1451 if (!access)
1452 return NULL;
1453 copy = isl_union_access_info_from_sink(
1454 isl_union_map_copy(access->sink));
1455 copy = isl_union_access_info_set_must_source(copy,
1456 isl_union_map_copy(access->must_source));
1457 copy = isl_union_access_info_set_may_source(copy,
1458 isl_union_map_copy(access->may_source));
1459 if (access->schedule)
1460 copy = isl_union_access_info_set_schedule(copy,
1461 isl_schedule_copy(access->schedule));
1462 else
1463 copy = isl_union_access_info_set_schedule_map(copy,
1464 isl_union_map_copy(access->schedule_map));
1466 return copy;
1469 /* Print a key-value pair of a YAML mapping to "p",
1470 * with key "name" and value "umap".
1472 static __isl_give isl_printer *print_union_map_field(__isl_take isl_printer *p,
1473 const char *name, __isl_keep isl_union_map *umap)
1475 p = isl_printer_print_str(p, name);
1476 p = isl_printer_yaml_next(p);
1477 p = isl_printer_print_str(p, "\"");
1478 p = isl_printer_print_union_map(p, umap);
1479 p = isl_printer_print_str(p, "\"");
1480 p = isl_printer_yaml_next(p);
1482 return p;
1485 /* Print the information contained in "access" to "p".
1486 * The information is printed as a YAML document.
1488 __isl_give isl_printer *isl_printer_print_union_access_info(
1489 __isl_take isl_printer *p, __isl_keep isl_union_access_info *access)
1491 if (!access)
1492 return isl_printer_free(p);
1494 p = isl_printer_yaml_start_mapping(p);
1495 p = print_union_map_field(p, "sink", access->sink);
1496 p = print_union_map_field(p, "must_source", access->must_source);
1497 p = print_union_map_field(p, "may_source", access->may_source);
1498 if (access->schedule) {
1499 p = isl_printer_print_str(p, "schedule");
1500 p = isl_printer_yaml_next(p);
1501 p = isl_printer_print_schedule(p, access->schedule);
1502 p = isl_printer_yaml_next(p);
1503 } else {
1504 p = print_union_map_field(p, "schedule_map",
1505 access->schedule_map);
1507 p = isl_printer_yaml_end_mapping(p);
1509 return p;
1512 /* Return a string representation of the information in "access".
1513 * The information is printed in flow format.
1515 __isl_give char *isl_union_access_info_to_str(
1516 __isl_keep isl_union_access_info *access)
1518 isl_printer *p;
1519 char *s;
1521 if (!access)
1522 return NULL;
1524 p = isl_printer_to_str(isl_union_access_info_get_ctx(access));
1525 p = isl_printer_set_yaml_style(p, ISL_YAML_STYLE_FLOW);
1526 p = isl_printer_print_union_access_info(p, access);
1527 s = isl_printer_get_str(p);
1528 isl_printer_free(p);
1530 return s;
1533 /* Update the fields of "access" such that they all have the same parameters,
1534 * keeping in mind that the schedule_map field may be NULL and ignoring
1535 * the schedule field.
1537 static __isl_give isl_union_access_info *isl_union_access_info_align_params(
1538 __isl_take isl_union_access_info *access)
1540 isl_space *space;
1542 if (!access)
1543 return NULL;
1545 space = isl_union_map_get_space(access->sink);
1546 space = isl_space_align_params(space,
1547 isl_union_map_get_space(access->must_source));
1548 space = isl_space_align_params(space,
1549 isl_union_map_get_space(access->may_source));
1550 if (access->schedule_map)
1551 space = isl_space_align_params(space,
1552 isl_union_map_get_space(access->schedule_map));
1553 access->sink = isl_union_map_align_params(access->sink,
1554 isl_space_copy(space));
1555 access->must_source = isl_union_map_align_params(access->must_source,
1556 isl_space_copy(space));
1557 access->may_source = isl_union_map_align_params(access->may_source,
1558 isl_space_copy(space));
1559 if (!access->schedule_map) {
1560 isl_space_free(space);
1561 } else {
1562 access->schedule_map =
1563 isl_union_map_align_params(access->schedule_map, space);
1564 if (!access->schedule_map)
1565 return isl_union_access_info_free(access);
1568 if (!access->sink || !access->must_source || !access->may_source)
1569 return isl_union_access_info_free(access);
1571 return access;
1574 /* Prepend the schedule dimensions to the iteration domains.
1576 * That is, if the schedule is of the form
1578 * D -> S
1580 * while the access relations are of the form
1582 * D -> A
1584 * then the updated access relations are of the form
1586 * [S -> D] -> A
1588 * The schedule map is also replaced by the map
1590 * [S -> D] -> D
1592 * that is used during the internal computation.
1593 * Neither the original schedule map nor this updated schedule map
1594 * are used after the call to this function.
1596 static __isl_give isl_union_access_info *
1597 isl_union_access_info_introduce_schedule(
1598 __isl_take isl_union_access_info *access)
1600 isl_union_map *sm;
1602 if (!access)
1603 return NULL;
1605 sm = isl_union_map_reverse(access->schedule_map);
1606 sm = isl_union_map_range_map(sm);
1607 access->sink = isl_union_map_apply_range(isl_union_map_copy(sm),
1608 access->sink);
1609 access->may_source = isl_union_map_apply_range(isl_union_map_copy(sm),
1610 access->may_source);
1611 access->must_source = isl_union_map_apply_range(isl_union_map_copy(sm),
1612 access->must_source);
1613 access->schedule_map = sm;
1615 if (!access->sink || !access->must_source ||
1616 !access->may_source || !access->schedule_map)
1617 return isl_union_access_info_free(access);
1619 return access;
1622 /* This structure represents the result of a dependence analysis computation.
1624 * "must_dep" represents the full definite dependences
1625 * "may_dep" represents the full non-definite dependences.
1626 * Both are of the form
1628 * [Source] -> [[Sink -> Data]]
1630 * (after the schedule dimensions have been projected out).
1631 * "must_no_source" represents the subset of the sink accesses for which
1632 * definitely no source was found.
1633 * "may_no_source" represents the subset of the sink accesses for which
1634 * possibly, but not definitely, no source was found.
1636 struct isl_union_flow {
1637 isl_union_map *must_dep;
1638 isl_union_map *may_dep;
1639 isl_union_map *must_no_source;
1640 isl_union_map *may_no_source;
1643 /* Return the isl_ctx to which "flow" belongs.
1645 isl_ctx *isl_union_flow_get_ctx(__isl_keep isl_union_flow *flow)
1647 return flow ? isl_union_map_get_ctx(flow->must_dep) : NULL;
1650 /* Free "flow" and return NULL.
1652 __isl_null isl_union_flow *isl_union_flow_free(__isl_take isl_union_flow *flow)
1654 if (!flow)
1655 return NULL;
1656 isl_union_map_free(flow->must_dep);
1657 isl_union_map_free(flow->may_dep);
1658 isl_union_map_free(flow->must_no_source);
1659 isl_union_map_free(flow->may_no_source);
1660 free(flow);
1661 return NULL;
1664 void isl_union_flow_dump(__isl_keep isl_union_flow *flow)
1666 if (!flow)
1667 return;
1669 fprintf(stderr, "must dependences: ");
1670 isl_union_map_dump(flow->must_dep);
1671 fprintf(stderr, "may dependences: ");
1672 isl_union_map_dump(flow->may_dep);
1673 fprintf(stderr, "must no source: ");
1674 isl_union_map_dump(flow->must_no_source);
1675 fprintf(stderr, "may no source: ");
1676 isl_union_map_dump(flow->may_no_source);
1679 /* Return the full definite dependences in "flow", with accessed elements.
1681 __isl_give isl_union_map *isl_union_flow_get_full_must_dependence(
1682 __isl_keep isl_union_flow *flow)
1684 if (!flow)
1685 return NULL;
1686 return isl_union_map_copy(flow->must_dep);
1689 /* Return the full possible dependences in "flow", including the definite
1690 * dependences, with accessed elements.
1692 __isl_give isl_union_map *isl_union_flow_get_full_may_dependence(
1693 __isl_keep isl_union_flow *flow)
1695 if (!flow)
1696 return NULL;
1697 return isl_union_map_union(isl_union_map_copy(flow->must_dep),
1698 isl_union_map_copy(flow->may_dep));
1701 /* Return the definite dependences in "flow", without the accessed elements.
1703 __isl_give isl_union_map *isl_union_flow_get_must_dependence(
1704 __isl_keep isl_union_flow *flow)
1706 isl_union_map *dep;
1708 if (!flow)
1709 return NULL;
1710 dep = isl_union_map_copy(flow->must_dep);
1711 return isl_union_map_range_factor_domain(dep);
1714 /* Return the possible dependences in "flow", including the definite
1715 * dependences, without the accessed elements.
1717 __isl_give isl_union_map *isl_union_flow_get_may_dependence(
1718 __isl_keep isl_union_flow *flow)
1720 isl_union_map *dep;
1722 if (!flow)
1723 return NULL;
1724 dep = isl_union_map_union(isl_union_map_copy(flow->must_dep),
1725 isl_union_map_copy(flow->may_dep));
1726 return isl_union_map_range_factor_domain(dep);
1729 /* Return the non-definite dependences in "flow".
1731 static __isl_give isl_union_map *isl_union_flow_get_non_must_dependence(
1732 __isl_keep isl_union_flow *flow)
1734 if (!flow)
1735 return NULL;
1736 return isl_union_map_copy(flow->may_dep);
1739 /* Return the subset of the sink accesses for which definitely
1740 * no source was found.
1742 __isl_give isl_union_map *isl_union_flow_get_must_no_source(
1743 __isl_keep isl_union_flow *flow)
1745 if (!flow)
1746 return NULL;
1747 return isl_union_map_copy(flow->must_no_source);
1750 /* Return the subset of the sink accesses for which possibly
1751 * no source was found, including those for which definitely
1752 * no source was found.
1754 __isl_give isl_union_map *isl_union_flow_get_may_no_source(
1755 __isl_keep isl_union_flow *flow)
1757 if (!flow)
1758 return NULL;
1759 return isl_union_map_union(isl_union_map_copy(flow->must_no_source),
1760 isl_union_map_copy(flow->may_no_source));
1763 /* Return the subset of the sink accesses for which possibly, but not
1764 * definitely, no source was found.
1766 static __isl_give isl_union_map *isl_union_flow_get_non_must_no_source(
1767 __isl_keep isl_union_flow *flow)
1769 if (!flow)
1770 return NULL;
1771 return isl_union_map_copy(flow->may_no_source);
1774 /* Create a new isl_union_flow object, initialized with empty
1775 * dependence relations and sink subsets.
1777 static __isl_give isl_union_flow *isl_union_flow_alloc(
1778 __isl_take isl_space *space)
1780 isl_ctx *ctx;
1781 isl_union_map *empty;
1782 isl_union_flow *flow;
1784 if (!space)
1785 return NULL;
1786 ctx = isl_space_get_ctx(space);
1787 flow = isl_alloc_type(ctx, isl_union_flow);
1788 if (!flow)
1789 goto error;
1791 empty = isl_union_map_empty(space);
1792 flow->must_dep = isl_union_map_copy(empty);
1793 flow->may_dep = isl_union_map_copy(empty);
1794 flow->must_no_source = isl_union_map_copy(empty);
1795 flow->may_no_source = empty;
1797 if (!flow->must_dep || !flow->may_dep ||
1798 !flow->must_no_source || !flow->may_no_source)
1799 return isl_union_flow_free(flow);
1801 return flow;
1802 error:
1803 isl_space_free(space);
1804 return NULL;
1807 /* Copy this isl_union_flow object.
1809 __isl_give isl_union_flow *isl_union_flow_copy(__isl_keep isl_union_flow *flow)
1811 isl_union_flow *copy;
1813 if (!flow)
1814 return NULL;
1816 copy = isl_union_flow_alloc(isl_union_map_get_space(flow->must_dep));
1818 if (!copy)
1819 return NULL;
1821 copy->must_dep = isl_union_map_union(copy->must_dep,
1822 isl_union_map_copy(flow->must_dep));
1823 copy->may_dep = isl_union_map_union(copy->may_dep,
1824 isl_union_map_copy(flow->may_dep));
1825 copy->must_no_source = isl_union_map_union(copy->must_no_source,
1826 isl_union_map_copy(flow->must_no_source));
1827 copy->may_no_source = isl_union_map_union(copy->may_no_source,
1828 isl_union_map_copy(flow->may_no_source));
1830 if (!copy->must_dep || !copy->may_dep ||
1831 !copy->must_no_source || !copy->may_no_source)
1832 return isl_union_flow_free(copy);
1834 return copy;
1837 /* Drop the schedule dimensions from the iteration domains in "flow".
1838 * In particular, the schedule dimensions have been prepended
1839 * to the iteration domains prior to the dependence analysis by
1840 * replacing the iteration domain D, by the wrapped map [S -> D].
1841 * Replace these wrapped maps by the original D.
1843 * In particular, the dependences computed by access_info_compute_flow_core
1844 * are of the form
1846 * [S -> D] -> [[S' -> D'] -> A]
1848 * The schedule dimensions are projected out by first currying the range,
1849 * resulting in
1851 * [S -> D] -> [S' -> [D' -> A]]
1853 * and then computing the factor range
1855 * D -> [D' -> A]
1857 static __isl_give isl_union_flow *isl_union_flow_drop_schedule(
1858 __isl_take isl_union_flow *flow)
1860 if (!flow)
1861 return NULL;
1863 flow->must_dep = isl_union_map_range_curry(flow->must_dep);
1864 flow->must_dep = isl_union_map_factor_range(flow->must_dep);
1865 flow->may_dep = isl_union_map_range_curry(flow->may_dep);
1866 flow->may_dep = isl_union_map_factor_range(flow->may_dep);
1867 flow->must_no_source =
1868 isl_union_map_domain_factor_range(flow->must_no_source);
1869 flow->may_no_source =
1870 isl_union_map_domain_factor_range(flow->may_no_source);
1872 if (!flow->must_dep || !flow->may_dep ||
1873 !flow->must_no_source || !flow->may_no_source)
1874 return isl_union_flow_free(flow);
1876 return flow;
1879 struct isl_compute_flow_data {
1880 isl_union_map *must_source;
1881 isl_union_map *may_source;
1882 isl_union_flow *flow;
1884 int count;
1885 int must;
1886 isl_space *dim;
1887 struct isl_sched_info *sink_info;
1888 struct isl_sched_info **source_info;
1889 isl_access_info *accesses;
1892 static isl_stat count_matching_array(__isl_take isl_map *map, void *user)
1894 int eq;
1895 isl_space *dim;
1896 struct isl_compute_flow_data *data;
1898 data = (struct isl_compute_flow_data *)user;
1900 dim = isl_space_range(isl_map_get_space(map));
1902 eq = isl_space_is_equal(dim, data->dim);
1904 isl_space_free(dim);
1905 isl_map_free(map);
1907 if (eq < 0)
1908 return isl_stat_error;
1909 if (eq)
1910 data->count++;
1912 return isl_stat_ok;
1915 static isl_stat collect_matching_array(__isl_take isl_map *map, void *user)
1917 int eq;
1918 isl_space *dim;
1919 struct isl_sched_info *info;
1920 struct isl_compute_flow_data *data;
1922 data = (struct isl_compute_flow_data *)user;
1924 dim = isl_space_range(isl_map_get_space(map));
1926 eq = isl_space_is_equal(dim, data->dim);
1928 isl_space_free(dim);
1930 if (eq < 0)
1931 goto error;
1932 if (!eq) {
1933 isl_map_free(map);
1934 return isl_stat_ok;
1937 info = sched_info_alloc(map);
1938 data->source_info[data->count] = info;
1940 data->accesses = isl_access_info_add_source(data->accesses,
1941 map, data->must, info);
1943 data->count++;
1945 return isl_stat_ok;
1946 error:
1947 isl_map_free(map);
1948 return isl_stat_error;
1951 /* Determine the shared nesting level and the "textual order" of
1952 * the given accesses.
1954 * We first determine the minimal schedule dimension for both accesses.
1956 * If among those dimensions, we can find one where both have a fixed
1957 * value and if moreover those values are different, then the previous
1958 * dimension is the last shared nesting level and the textual order
1959 * is determined based on the order of the fixed values.
1960 * If no such fixed values can be found, then we set the shared
1961 * nesting level to the minimal schedule dimension, with no textual ordering.
1963 static int before(void *first, void *second)
1965 struct isl_sched_info *info1 = first;
1966 struct isl_sched_info *info2 = second;
1967 int n1, n2;
1968 int i;
1970 n1 = isl_vec_size(info1->cst);
1971 n2 = isl_vec_size(info2->cst);
1973 if (n2 < n1)
1974 n1 = n2;
1976 for (i = 0; i < n1; ++i) {
1977 int r;
1978 int cmp;
1980 if (!info1->is_cst[i])
1981 continue;
1982 if (!info2->is_cst[i])
1983 continue;
1984 cmp = isl_vec_cmp_element(info1->cst, info2->cst, i);
1985 if (cmp == 0)
1986 continue;
1988 r = 2 * i + (cmp < 0);
1990 return r;
1993 return 2 * n1;
1996 /* Given a sink access, look for all the source accesses that access
1997 * the same array and perform dataflow analysis on them using
1998 * isl_access_info_compute_flow_core.
2000 static isl_stat compute_flow(__isl_take isl_map *map, void *user)
2002 int i;
2003 isl_ctx *ctx;
2004 struct isl_compute_flow_data *data;
2005 isl_flow *flow;
2006 isl_union_flow *df;
2008 data = (struct isl_compute_flow_data *)user;
2009 df = data->flow;
2011 ctx = isl_map_get_ctx(map);
2013 data->accesses = NULL;
2014 data->sink_info = NULL;
2015 data->source_info = NULL;
2016 data->count = 0;
2017 data->dim = isl_space_range(isl_map_get_space(map));
2019 if (isl_union_map_foreach_map(data->must_source,
2020 &count_matching_array, data) < 0)
2021 goto error;
2022 if (isl_union_map_foreach_map(data->may_source,
2023 &count_matching_array, data) < 0)
2024 goto error;
2026 data->sink_info = sched_info_alloc(map);
2027 data->source_info = isl_calloc_array(ctx, struct isl_sched_info *,
2028 data->count);
2030 data->accesses = isl_access_info_alloc(isl_map_copy(map),
2031 data->sink_info, &before, data->count);
2032 if (!data->sink_info || (data->count && !data->source_info) ||
2033 !data->accesses)
2034 goto error;
2035 data->count = 0;
2036 data->must = 1;
2037 if (isl_union_map_foreach_map(data->must_source,
2038 &collect_matching_array, data) < 0)
2039 goto error;
2040 data->must = 0;
2041 if (isl_union_map_foreach_map(data->may_source,
2042 &collect_matching_array, data) < 0)
2043 goto error;
2045 flow = access_info_compute_flow_core(data->accesses);
2046 data->accesses = NULL;
2048 if (!flow)
2049 goto error;
2051 df->must_no_source = isl_union_map_union(df->must_no_source,
2052 isl_union_map_from_map(isl_flow_get_no_source(flow, 1)));
2053 df->may_no_source = isl_union_map_union(df->may_no_source,
2054 isl_union_map_from_map(isl_flow_get_no_source(flow, 0)));
2056 for (i = 0; i < flow->n_source; ++i) {
2057 isl_union_map *dep;
2058 dep = isl_union_map_from_map(isl_map_copy(flow->dep[i].map));
2059 if (flow->dep[i].must)
2060 df->must_dep = isl_union_map_union(df->must_dep, dep);
2061 else
2062 df->may_dep = isl_union_map_union(df->may_dep, dep);
2065 isl_flow_free(flow);
2067 sched_info_free(data->sink_info);
2068 if (data->source_info) {
2069 for (i = 0; i < data->count; ++i)
2070 sched_info_free(data->source_info[i]);
2071 free(data->source_info);
2073 isl_space_free(data->dim);
2074 isl_map_free(map);
2076 return isl_stat_ok;
2077 error:
2078 isl_access_info_free(data->accesses);
2079 sched_info_free(data->sink_info);
2080 if (data->source_info) {
2081 for (i = 0; i < data->count; ++i)
2082 sched_info_free(data->source_info[i]);
2083 free(data->source_info);
2085 isl_space_free(data->dim);
2086 isl_map_free(map);
2088 return isl_stat_error;
2091 /* Remove the must accesses from the may accesses.
2093 * A must access always trumps a may access, so there is no need
2094 * for a must access to also be considered as a may access. Doing so
2095 * would only cost extra computations only to find out that
2096 * the duplicated may access does not make any difference.
2098 static __isl_give isl_union_access_info *isl_union_access_info_normalize(
2099 __isl_take isl_union_access_info *access)
2101 if (!access)
2102 return NULL;
2103 access->may_source = isl_union_map_subtract(access->may_source,
2104 isl_union_map_copy(access->must_source));
2105 if (!access->may_source)
2106 return isl_union_access_info_free(access);
2108 return access;
2111 /* Given a description of the "sink" accesses, the "source" accesses and
2112 * a schedule, compute for each instance of a sink access
2113 * and for each element accessed by that instance,
2114 * the possible or definite source accesses that last accessed the
2115 * element accessed by the sink access before this sink access
2116 * in the sense that there is no intermediate definite source access.
2118 * The must_no_source and may_no_source elements of the result
2119 * are subsets of access->sink. The elements must_dep and may_dep
2120 * map domain elements of access->{may,must)_source to
2121 * domain elements of access->sink.
2123 * This function is used when only the schedule map representation
2124 * is available.
2126 * We first prepend the schedule dimensions to the domain
2127 * of the accesses so that we can easily compare their relative order.
2128 * Then we consider each sink access individually in compute_flow.
2130 static __isl_give isl_union_flow *compute_flow_union_map(
2131 __isl_take isl_union_access_info *access)
2133 struct isl_compute_flow_data data;
2135 access = isl_union_access_info_align_params(access);
2136 access = isl_union_access_info_introduce_schedule(access);
2137 if (!access)
2138 return NULL;
2140 data.must_source = access->must_source;
2141 data.may_source = access->may_source;
2143 data.flow = isl_union_flow_alloc(isl_union_map_get_space(access->sink));
2145 if (isl_union_map_foreach_map(access->sink, &compute_flow, &data) < 0)
2146 goto error;
2148 data.flow = isl_union_flow_drop_schedule(data.flow);
2150 isl_union_access_info_free(access);
2151 return data.flow;
2152 error:
2153 isl_union_access_info_free(access);
2154 isl_union_flow_free(data.flow);
2155 return NULL;
2158 /* A schedule access relation.
2160 * The access relation "access" is of the form [S -> D] -> A,
2161 * where S corresponds to the prefix schedule at "node".
2162 * "must" is only relevant for source accesses and indicates
2163 * whether the access is a must source or a may source.
2165 struct isl_scheduled_access {
2166 isl_map *access;
2167 int must;
2168 isl_schedule_node *node;
2171 /* Data structure for keeping track of individual scheduled sink and source
2172 * accesses when computing dependence analysis based on a schedule tree.
2174 * "n_sink" is the number of used entries in "sink"
2175 * "n_source" is the number of used entries in "source"
2177 * "set_sink", "must" and "node" are only used inside collect_sink_source,
2178 * to keep track of the current node and
2179 * of what extract_sink_source needs to do.
2181 struct isl_compute_flow_schedule_data {
2182 isl_union_access_info *access;
2184 int n_sink;
2185 int n_source;
2187 struct isl_scheduled_access *sink;
2188 struct isl_scheduled_access *source;
2190 int set_sink;
2191 int must;
2192 isl_schedule_node *node;
2195 /* Align the parameters of all sinks with all sources.
2197 * If there are no sinks or no sources, then no alignment is needed.
2199 static void isl_compute_flow_schedule_data_align_params(
2200 struct isl_compute_flow_schedule_data *data)
2202 int i;
2203 isl_space *space;
2205 if (data->n_sink == 0 || data->n_source == 0)
2206 return;
2208 space = isl_map_get_space(data->sink[0].access);
2210 for (i = 1; i < data->n_sink; ++i)
2211 space = isl_space_align_params(space,
2212 isl_map_get_space(data->sink[i].access));
2213 for (i = 0; i < data->n_source; ++i)
2214 space = isl_space_align_params(space,
2215 isl_map_get_space(data->source[i].access));
2217 for (i = 0; i < data->n_sink; ++i)
2218 data->sink[i].access =
2219 isl_map_align_params(data->sink[i].access,
2220 isl_space_copy(space));
2221 for (i = 0; i < data->n_source; ++i)
2222 data->source[i].access =
2223 isl_map_align_params(data->source[i].access,
2224 isl_space_copy(space));
2226 isl_space_free(space);
2229 /* Free all the memory referenced from "data".
2230 * Do not free "data" itself as it may be allocated on the stack.
2232 static void isl_compute_flow_schedule_data_clear(
2233 struct isl_compute_flow_schedule_data *data)
2235 int i;
2237 if (!data->sink)
2238 return;
2240 for (i = 0; i < data->n_sink; ++i) {
2241 isl_map_free(data->sink[i].access);
2242 isl_schedule_node_free(data->sink[i].node);
2245 for (i = 0; i < data->n_source; ++i) {
2246 isl_map_free(data->source[i].access);
2247 isl_schedule_node_free(data->source[i].node);
2250 free(data->sink);
2253 /* isl_schedule_foreach_schedule_node_top_down callback for counting
2254 * (an upper bound on) the number of sinks and sources.
2256 * Sinks and sources are only extracted at leaves of the tree,
2257 * so we skip the node if it is not a leaf.
2258 * Otherwise we increment data->n_sink and data->n_source with
2259 * the number of spaces in the sink and source access domains
2260 * that reach this node.
2262 static isl_bool count_sink_source(__isl_keep isl_schedule_node *node,
2263 void *user)
2265 struct isl_compute_flow_schedule_data *data = user;
2266 isl_union_set *domain;
2267 isl_union_map *umap;
2268 isl_bool r = isl_bool_false;
2270 if (isl_schedule_node_get_type(node) != isl_schedule_node_leaf)
2271 return isl_bool_true;
2273 domain = isl_schedule_node_get_universe_domain(node);
2275 umap = isl_union_map_copy(data->access->sink);
2276 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
2277 data->n_sink += isl_union_map_n_map(umap);
2278 isl_union_map_free(umap);
2279 if (!umap)
2280 r = isl_bool_error;
2282 umap = isl_union_map_copy(data->access->must_source);
2283 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
2284 data->n_source += isl_union_map_n_map(umap);
2285 isl_union_map_free(umap);
2286 if (!umap)
2287 r = isl_bool_error;
2289 umap = isl_union_map_copy(data->access->may_source);
2290 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
2291 data->n_source += isl_union_map_n_map(umap);
2292 isl_union_map_free(umap);
2293 if (!umap)
2294 r = isl_bool_error;
2296 isl_union_set_free(domain);
2298 return r;
2301 /* Add a single scheduled sink or source (depending on data->set_sink)
2302 * with scheduled access relation "map", must property data->must and
2303 * schedule node data->node to the list of sinks or sources.
2305 static isl_stat extract_sink_source(__isl_take isl_map *map, void *user)
2307 struct isl_compute_flow_schedule_data *data = user;
2308 struct isl_scheduled_access *access;
2310 if (data->set_sink)
2311 access = data->sink + data->n_sink++;
2312 else
2313 access = data->source + data->n_source++;
2315 access->access = map;
2316 access->must = data->must;
2317 access->node = isl_schedule_node_copy(data->node);
2319 return isl_stat_ok;
2322 /* isl_schedule_foreach_schedule_node_top_down callback for collecting
2323 * individual scheduled source and sink accesses (taking into account
2324 * the domain of the schedule).
2326 * We only collect accesses at the leaves of the schedule tree.
2327 * We prepend the schedule dimensions at the leaf to the iteration
2328 * domains of the source and sink accesses and then extract
2329 * the individual accesses (per space).
2331 * In particular, if the prefix schedule at the node is of the form
2333 * D -> S
2335 * while the access relations are of the form
2337 * D -> A
2339 * then the updated access relations are of the form
2341 * [S -> D] -> A
2343 * Note that S consists of a single space such that introducing S
2344 * in the access relations does not increase the number of spaces.
2346 static isl_bool collect_sink_source(__isl_keep isl_schedule_node *node,
2347 void *user)
2349 struct isl_compute_flow_schedule_data *data = user;
2350 isl_union_map *prefix;
2351 isl_union_map *umap;
2352 isl_bool r = isl_bool_false;
2354 if (isl_schedule_node_get_type(node) != isl_schedule_node_leaf)
2355 return isl_bool_true;
2357 data->node = node;
2359 prefix = isl_schedule_node_get_prefix_schedule_relation(node);
2360 prefix = isl_union_map_reverse(prefix);
2361 prefix = isl_union_map_range_map(prefix);
2363 data->set_sink = 1;
2364 umap = isl_union_map_copy(data->access->sink);
2365 umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
2366 if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
2367 r = isl_bool_error;
2368 isl_union_map_free(umap);
2370 data->set_sink = 0;
2371 data->must = 1;
2372 umap = isl_union_map_copy(data->access->must_source);
2373 umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
2374 if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
2375 r = isl_bool_error;
2376 isl_union_map_free(umap);
2378 data->set_sink = 0;
2379 data->must = 0;
2380 umap = isl_union_map_copy(data->access->may_source);
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 isl_union_map_free(prefix);
2388 return r;
2391 /* isl_access_info_compute_flow callback for determining whether
2392 * the shared nesting level and the ordering within that level
2393 * for two scheduled accesses for use in compute_single_flow.
2395 * The tokens passed to this function refer to the leaves
2396 * in the schedule tree where the accesses take place.
2398 * If n is the shared number of loops, then we need to return
2399 * "2 * n + 1" if "first" precedes "second" inside the innermost
2400 * shared loop and "2 * n" otherwise.
2402 * The innermost shared ancestor may be the leaves themselves
2403 * if the accesses take place in the same leaf. Otherwise,
2404 * it is either a set node or a sequence node. Only in the case
2405 * of a sequence node do we consider one access to precede the other.
2407 static int before_node(void *first, void *second)
2409 isl_schedule_node *node1 = first;
2410 isl_schedule_node *node2 = second;
2411 isl_schedule_node *shared;
2412 int depth;
2413 int before = 0;
2415 shared = isl_schedule_node_get_shared_ancestor(node1, node2);
2416 if (!shared)
2417 return -1;
2419 depth = isl_schedule_node_get_schedule_depth(shared);
2420 if (isl_schedule_node_get_type(shared) == isl_schedule_node_sequence) {
2421 int pos1, pos2;
2423 pos1 = isl_schedule_node_get_ancestor_child_position(node1,
2424 shared);
2425 pos2 = isl_schedule_node_get_ancestor_child_position(node2,
2426 shared);
2427 before = pos1 < pos2;
2430 isl_schedule_node_free(shared);
2432 return 2 * depth + before;
2435 /* Add the scheduled sources from "data" that access
2436 * the same data space as "sink" to "access".
2438 static __isl_give isl_access_info *add_matching_sources(
2439 __isl_take isl_access_info *access, struct isl_scheduled_access *sink,
2440 struct isl_compute_flow_schedule_data *data)
2442 int i;
2443 isl_space *space;
2445 space = isl_space_range(isl_map_get_space(sink->access));
2446 for (i = 0; i < data->n_source; ++i) {
2447 struct isl_scheduled_access *source;
2448 isl_space *source_space;
2449 int eq;
2451 source = &data->source[i];
2452 source_space = isl_map_get_space(source->access);
2453 source_space = isl_space_range(source_space);
2454 eq = isl_space_is_equal(space, source_space);
2455 isl_space_free(source_space);
2457 if (!eq)
2458 continue;
2459 if (eq < 0)
2460 goto error;
2462 access = isl_access_info_add_source(access,
2463 isl_map_copy(source->access), source->must, source->node);
2466 isl_space_free(space);
2467 return access;
2468 error:
2469 isl_space_free(space);
2470 isl_access_info_free(access);
2471 return NULL;
2474 /* Given a scheduled sink access relation "sink", compute the corresponding
2475 * dependences on the sources in "data" and add the computed dependences
2476 * to "uf".
2478 * The dependences computed by access_info_compute_flow_core are of the form
2480 * [S -> I] -> [[S' -> I'] -> A]
2482 * The schedule dimensions are projected out by first currying the range,
2483 * resulting in
2485 * [S -> I] -> [S' -> [I' -> A]]
2487 * and then computing the factor range
2489 * I -> [I' -> A]
2491 static __isl_give isl_union_flow *compute_single_flow(
2492 __isl_take isl_union_flow *uf, struct isl_scheduled_access *sink,
2493 struct isl_compute_flow_schedule_data *data)
2495 int i;
2496 isl_access_info *access;
2497 isl_flow *flow;
2498 isl_map *map;
2500 if (!uf)
2501 return NULL;
2503 access = isl_access_info_alloc(isl_map_copy(sink->access), sink->node,
2504 &before_node, data->n_source);
2505 access = add_matching_sources(access, sink, data);
2507 flow = access_info_compute_flow_core(access);
2508 if (!flow)
2509 return isl_union_flow_free(uf);
2511 map = isl_map_domain_factor_range(isl_flow_get_no_source(flow, 1));
2512 uf->must_no_source = isl_union_map_union(uf->must_no_source,
2513 isl_union_map_from_map(map));
2514 map = isl_map_domain_factor_range(isl_flow_get_no_source(flow, 0));
2515 uf->may_no_source = isl_union_map_union(uf->may_no_source,
2516 isl_union_map_from_map(map));
2518 for (i = 0; i < flow->n_source; ++i) {
2519 isl_union_map *dep;
2521 map = isl_map_range_curry(isl_map_copy(flow->dep[i].map));
2522 map = isl_map_factor_range(map);
2523 dep = isl_union_map_from_map(map);
2524 if (flow->dep[i].must)
2525 uf->must_dep = isl_union_map_union(uf->must_dep, dep);
2526 else
2527 uf->may_dep = isl_union_map_union(uf->may_dep, dep);
2530 isl_flow_free(flow);
2532 return uf;
2535 /* Given a description of the "sink" accesses, the "source" accesses and
2536 * a schedule, compute for each instance of a sink access
2537 * and for each element accessed by that instance,
2538 * the possible or definite source accesses that last accessed the
2539 * element accessed by the sink access before this sink access
2540 * in the sense that there is no intermediate definite source access.
2541 * Only consider dependences between statement instances that belong
2542 * to the domain of the schedule.
2544 * The must_no_source and may_no_source elements of the result
2545 * are subsets of access->sink. The elements must_dep and may_dep
2546 * map domain elements of access->{may,must)_source to
2547 * domain elements of access->sink.
2549 * This function is used when a schedule tree representation
2550 * is available.
2552 * We extract the individual scheduled source and sink access relations
2553 * (taking into account the domain of the schedule) and
2554 * then compute dependences for each scheduled sink individually.
2556 static __isl_give isl_union_flow *compute_flow_schedule(
2557 __isl_take isl_union_access_info *access)
2559 struct isl_compute_flow_schedule_data data = { access };
2560 int i, n;
2561 isl_ctx *ctx;
2562 isl_union_flow *flow;
2564 ctx = isl_union_access_info_get_ctx(access);
2566 data.n_sink = 0;
2567 data.n_source = 0;
2568 if (isl_schedule_foreach_schedule_node_top_down(access->schedule,
2569 &count_sink_source, &data) < 0)
2570 goto error;
2572 n = data.n_sink + data.n_source;
2573 data.sink = isl_calloc_array(ctx, struct isl_scheduled_access, n);
2574 if (n && !data.sink)
2575 goto error;
2576 data.source = data.sink + data.n_sink;
2578 data.n_sink = 0;
2579 data.n_source = 0;
2580 if (isl_schedule_foreach_schedule_node_top_down(access->schedule,
2581 &collect_sink_source, &data) < 0)
2582 goto error;
2584 flow = isl_union_flow_alloc(isl_union_map_get_space(access->sink));
2586 isl_compute_flow_schedule_data_align_params(&data);
2588 for (i = 0; i < data.n_sink; ++i)
2589 flow = compute_single_flow(flow, &data.sink[i], &data);
2591 isl_compute_flow_schedule_data_clear(&data);
2593 isl_union_access_info_free(access);
2594 return flow;
2595 error:
2596 isl_union_access_info_free(access);
2597 isl_compute_flow_schedule_data_clear(&data);
2598 return NULL;
2601 /* Given a description of the "sink" accesses, the "source" accesses and
2602 * a schedule, compute for each instance of a sink access
2603 * and for each element accessed by that instance,
2604 * the possible or definite source accesses that last accessed the
2605 * element accessed by the sink access before this sink access
2606 * in the sense that there is no intermediate definite source access.
2608 * The must_no_source and may_no_source elements of the result
2609 * are subsets of access->sink. The elements must_dep and may_dep
2610 * map domain elements of access->{may,must)_source to
2611 * domain elements of access->sink.
2613 * We check whether the schedule is available as a schedule tree
2614 * or a schedule map and call the correpsonding function to perform
2615 * the analysis.
2617 __isl_give isl_union_flow *isl_union_access_info_compute_flow(
2618 __isl_take isl_union_access_info *access)
2620 access = isl_union_access_info_normalize(access);
2621 if (!access)
2622 return NULL;
2623 if (access->schedule)
2624 return compute_flow_schedule(access);
2625 else
2626 return compute_flow_union_map(access);
2629 /* Print the information contained in "flow" to "p".
2630 * The information is printed as a YAML document.
2632 __isl_give isl_printer *isl_printer_print_union_flow(
2633 __isl_take isl_printer *p, __isl_keep isl_union_flow *flow)
2635 isl_union_map *umap;
2637 if (!flow)
2638 return isl_printer_free(p);
2640 p = isl_printer_yaml_start_mapping(p);
2641 p = print_union_map_field(p, "must_dependence", flow->must_dep);
2642 umap = isl_union_flow_get_may_dependence(flow);
2643 p = print_union_map_field(p, "may_dependence", umap);
2644 isl_union_map_free(umap);
2645 p = print_union_map_field(p, "must_no_source", flow->must_no_source);
2646 umap = isl_union_flow_get_may_no_source(flow);
2647 p = print_union_map_field(p, "may_no_source", umap);
2648 isl_union_map_free(umap);
2649 p = isl_printer_yaml_end_mapping(p);
2651 return p;
2654 /* Return a string representation of the information in "flow".
2655 * The information is printed in flow format.
2657 __isl_give char *isl_union_flow_to_str(__isl_keep isl_union_flow *flow)
2659 isl_printer *p;
2660 char *s;
2662 if (!flow)
2663 return NULL;
2665 p = isl_printer_to_str(isl_union_flow_get_ctx(flow));
2666 p = isl_printer_set_yaml_style(p, ISL_YAML_STYLE_FLOW);
2667 p = isl_printer_print_union_flow(p, flow);
2668 s = isl_printer_get_str(p);
2669 isl_printer_free(p);
2671 return s;
2674 /* Given a collection of "sink" and "source" accesses,
2675 * compute for each iteration of a sink access
2676 * and for each element accessed by that iteration,
2677 * the source access in the list that last accessed the
2678 * element accessed by the sink access before this sink access.
2679 * Each access is given as a map from the loop iterators
2680 * to the array indices.
2681 * The result is a relations between source and sink
2682 * iterations and a subset of the domain of the sink accesses,
2683 * corresponding to those iterations that access an element
2684 * not previously accessed.
2686 * We collect the inputs in an isl_union_access_info object,
2687 * call isl_union_access_info_compute_flow and extract
2688 * the outputs from the result.
2690 int isl_union_map_compute_flow(__isl_take isl_union_map *sink,
2691 __isl_take isl_union_map *must_source,
2692 __isl_take isl_union_map *may_source,
2693 __isl_take isl_union_map *schedule,
2694 __isl_give isl_union_map **must_dep, __isl_give isl_union_map **may_dep,
2695 __isl_give isl_union_map **must_no_source,
2696 __isl_give isl_union_map **may_no_source)
2698 isl_union_access_info *access;
2699 isl_union_flow *flow;
2701 access = isl_union_access_info_from_sink(sink);
2702 access = isl_union_access_info_set_must_source(access, must_source);
2703 access = isl_union_access_info_set_may_source(access, may_source);
2704 access = isl_union_access_info_set_schedule_map(access, schedule);
2705 flow = isl_union_access_info_compute_flow(access);
2707 if (must_dep)
2708 *must_dep = isl_union_flow_get_must_dependence(flow);
2709 if (may_dep)
2710 *may_dep = isl_union_flow_get_non_must_dependence(flow);
2711 if (must_no_source)
2712 *must_no_source = isl_union_flow_get_must_no_source(flow);
2713 if (may_no_source)
2714 *may_no_source = isl_union_flow_get_non_must_no_source(flow);
2716 isl_union_flow_free(flow);
2718 if ((must_dep && !*must_dep) || (may_dep && !*may_dep) ||
2719 (must_no_source && !*must_no_source) ||
2720 (may_no_source && !*may_no_source))
2721 goto error;
2723 return 0;
2724 error:
2725 if (must_dep)
2726 *must_dep = isl_union_map_free(*must_dep);
2727 if (may_dep)
2728 *may_dep = isl_union_map_free(*may_dep);
2729 if (must_no_source)
2730 *must_no_source = isl_union_map_free(*must_no_source);
2731 if (may_no_source)
2732 *may_no_source = isl_union_map_free(*may_no_source);
2733 return -1;