isl_aff.c: isl_multi_pw_aff_apply_aff_aligned: drop unused variable
[isl.git] / isl_flow.c
blob80f20fe502c757103d9c90dd6111954c5b4aadfc
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/flow.h>
22 #include <isl/schedule_node.h>
23 #include <isl_sort.h>
25 enum isl_restriction_type {
26 isl_restriction_type_empty,
27 isl_restriction_type_none,
28 isl_restriction_type_input,
29 isl_restriction_type_output
32 struct isl_restriction {
33 enum isl_restriction_type type;
35 isl_set *source;
36 isl_set *sink;
39 /* Create a restriction of the given type.
41 static __isl_give isl_restriction *isl_restriction_alloc(
42 __isl_take isl_map *source_map, enum isl_restriction_type type)
44 isl_ctx *ctx;
45 isl_restriction *restr;
47 if (!source_map)
48 return NULL;
50 ctx = isl_map_get_ctx(source_map);
51 restr = isl_calloc_type(ctx, struct isl_restriction);
52 if (!restr)
53 goto error;
55 restr->type = type;
57 isl_map_free(source_map);
58 return restr;
59 error:
60 isl_map_free(source_map);
61 return NULL;
64 /* Create a restriction that doesn't restrict anything.
66 __isl_give isl_restriction *isl_restriction_none(__isl_take isl_map *source_map)
68 return isl_restriction_alloc(source_map, isl_restriction_type_none);
71 /* Create a restriction that removes everything.
73 __isl_give isl_restriction *isl_restriction_empty(
74 __isl_take isl_map *source_map)
76 return isl_restriction_alloc(source_map, isl_restriction_type_empty);
79 /* Create a restriction on the input of the maximization problem
80 * based on the given source and sink restrictions.
82 __isl_give isl_restriction *isl_restriction_input(
83 __isl_take isl_set *source_restr, __isl_take isl_set *sink_restr)
85 isl_ctx *ctx;
86 isl_restriction *restr;
88 if (!source_restr || !sink_restr)
89 goto error;
91 ctx = isl_set_get_ctx(source_restr);
92 restr = isl_calloc_type(ctx, struct isl_restriction);
93 if (!restr)
94 goto error;
96 restr->type = isl_restriction_type_input;
97 restr->source = source_restr;
98 restr->sink = sink_restr;
100 return restr;
101 error:
102 isl_set_free(source_restr);
103 isl_set_free(sink_restr);
104 return NULL;
107 /* Create a restriction on the output of the maximization problem
108 * based on the given source restriction.
110 __isl_give isl_restriction *isl_restriction_output(
111 __isl_take isl_set *source_restr)
113 isl_ctx *ctx;
114 isl_restriction *restr;
116 if (!source_restr)
117 return NULL;
119 ctx = isl_set_get_ctx(source_restr);
120 restr = isl_calloc_type(ctx, struct isl_restriction);
121 if (!restr)
122 goto error;
124 restr->type = isl_restriction_type_output;
125 restr->source = source_restr;
127 return restr;
128 error:
129 isl_set_free(source_restr);
130 return NULL;
133 __isl_null isl_restriction *isl_restriction_free(
134 __isl_take isl_restriction *restr)
136 if (!restr)
137 return NULL;
139 isl_set_free(restr->source);
140 isl_set_free(restr->sink);
141 free(restr);
142 return NULL;
145 isl_ctx *isl_restriction_get_ctx(__isl_keep isl_restriction *restr)
147 return restr ? isl_set_get_ctx(restr->source) : NULL;
150 /* A private structure to keep track of a mapping together with
151 * a user-specified identifier and a boolean indicating whether
152 * the map represents a must or may access/dependence.
154 struct isl_labeled_map {
155 struct isl_map *map;
156 void *data;
157 int must;
160 /* A structure containing the input for dependence analysis:
161 * - a sink
162 * - n_must + n_may (<= max_source) sources
163 * - a function for determining the relative order of sources and sink
164 * The must sources are placed before the may sources.
166 * domain_map is an auxiliary map that maps the sink access relation
167 * to the domain of this access relation.
169 * restrict_fn is a callback that (if not NULL) will be called
170 * right before any lexicographical maximization.
172 struct isl_access_info {
173 isl_map *domain_map;
174 struct isl_labeled_map sink;
175 isl_access_level_before level_before;
177 isl_access_restrict restrict_fn;
178 void *restrict_user;
180 int max_source;
181 int n_must;
182 int n_may;
183 struct isl_labeled_map source[1];
186 /* A structure containing the output of dependence analysis:
187 * - n_source dependences
188 * - a wrapped subset of the sink for which definitely no source could be found
189 * - a wrapped subset of the sink for which possibly no source could be found
191 struct isl_flow {
192 isl_set *must_no_source;
193 isl_set *may_no_source;
194 int n_source;
195 struct isl_labeled_map *dep;
198 /* Construct an isl_access_info structure and fill it up with
199 * the given data. The number of sources is set to 0.
201 __isl_give isl_access_info *isl_access_info_alloc(__isl_take isl_map *sink,
202 void *sink_user, isl_access_level_before fn, int max_source)
204 isl_ctx *ctx;
205 struct isl_access_info *acc;
207 if (!sink)
208 return NULL;
210 ctx = isl_map_get_ctx(sink);
211 isl_assert(ctx, max_source >= 0, goto error);
213 acc = isl_calloc(ctx, struct isl_access_info,
214 sizeof(struct isl_access_info) +
215 (max_source - 1) * sizeof(struct isl_labeled_map));
216 if (!acc)
217 goto error;
219 acc->sink.map = sink;
220 acc->sink.data = sink_user;
221 acc->level_before = fn;
222 acc->max_source = max_source;
223 acc->n_must = 0;
224 acc->n_may = 0;
226 return acc;
227 error:
228 isl_map_free(sink);
229 return NULL;
232 /* Free the given isl_access_info structure.
234 __isl_null isl_access_info *isl_access_info_free(
235 __isl_take isl_access_info *acc)
237 int i;
239 if (!acc)
240 return NULL;
241 isl_map_free(acc->domain_map);
242 isl_map_free(acc->sink.map);
243 for (i = 0; i < acc->n_must + acc->n_may; ++i)
244 isl_map_free(acc->source[i].map);
245 free(acc);
246 return NULL;
249 isl_ctx *isl_access_info_get_ctx(__isl_keep isl_access_info *acc)
251 return acc ? isl_map_get_ctx(acc->sink.map) : NULL;
254 __isl_give isl_access_info *isl_access_info_set_restrict(
255 __isl_take isl_access_info *acc, isl_access_restrict fn, void *user)
257 if (!acc)
258 return NULL;
259 acc->restrict_fn = fn;
260 acc->restrict_user = user;
261 return acc;
264 /* Add another source to an isl_access_info structure, making
265 * sure the "must" sources are placed before the "may" sources.
266 * This function may be called at most max_source times on a
267 * given isl_access_info structure, with max_source as specified
268 * in the call to isl_access_info_alloc that constructed the structure.
270 __isl_give isl_access_info *isl_access_info_add_source(
271 __isl_take isl_access_info *acc, __isl_take isl_map *source,
272 int must, void *source_user)
274 isl_ctx *ctx;
276 if (!acc)
277 goto error;
278 ctx = isl_map_get_ctx(acc->sink.map);
279 isl_assert(ctx, acc->n_must + acc->n_may < acc->max_source, goto error);
281 if (must) {
282 if (acc->n_may)
283 acc->source[acc->n_must + acc->n_may] =
284 acc->source[acc->n_must];
285 acc->source[acc->n_must].map = source;
286 acc->source[acc->n_must].data = source_user;
287 acc->source[acc->n_must].must = 1;
288 acc->n_must++;
289 } else {
290 acc->source[acc->n_must + acc->n_may].map = source;
291 acc->source[acc->n_must + acc->n_may].data = source_user;
292 acc->source[acc->n_must + acc->n_may].must = 0;
293 acc->n_may++;
296 return acc;
297 error:
298 isl_map_free(source);
299 isl_access_info_free(acc);
300 return NULL;
303 /* Return -n, 0 or n (with n a positive value), depending on whether
304 * the source access identified by p1 should be sorted before, together
305 * or after that identified by p2.
307 * If p1 appears before p2, then it should be sorted first.
308 * For more generic initial schedules, it is possible that neither
309 * p1 nor p2 appears before the other, or at least not in any obvious way.
310 * We therefore also check if p2 appears before p1, in which case p2
311 * should be sorted first.
312 * If not, we try to order the two statements based on the description
313 * of the iteration domains. This results in an arbitrary, but fairly
314 * stable ordering.
316 static int access_sort_cmp(const void *p1, const void *p2, void *user)
318 isl_access_info *acc = user;
319 const struct isl_labeled_map *i1, *i2;
320 int level1, level2;
321 uint32_t h1, h2;
322 i1 = (const struct isl_labeled_map *) p1;
323 i2 = (const struct isl_labeled_map *) p2;
325 level1 = acc->level_before(i1->data, i2->data);
326 if (level1 % 2)
327 return -1;
329 level2 = acc->level_before(i2->data, i1->data);
330 if (level2 % 2)
331 return 1;
333 h1 = isl_map_get_hash(i1->map);
334 h2 = isl_map_get_hash(i2->map);
335 return h1 > h2 ? 1 : h1 < h2 ? -1 : 0;
338 /* Sort the must source accesses in their textual order.
340 static __isl_give isl_access_info *isl_access_info_sort_sources(
341 __isl_take isl_access_info *acc)
343 if (!acc)
344 return NULL;
345 if (acc->n_must <= 1)
346 return acc;
348 if (isl_sort(acc->source, acc->n_must, sizeof(struct isl_labeled_map),
349 access_sort_cmp, acc) < 0)
350 return isl_access_info_free(acc);
352 return acc;
355 /* Align the parameters of the two spaces if needed and then call
356 * isl_space_join.
358 static __isl_give isl_space *space_align_and_join(__isl_take isl_space *left,
359 __isl_take isl_space *right)
361 if (isl_space_match(left, isl_dim_param, right, isl_dim_param))
362 return isl_space_join(left, right);
364 left = isl_space_align_params(left, isl_space_copy(right));
365 right = isl_space_align_params(right, isl_space_copy(left));
366 return isl_space_join(left, right);
369 /* Initialize an empty isl_flow structure corresponding to a given
370 * isl_access_info structure.
371 * For each must access, two dependences are created (initialized
372 * to the empty relation), one for the resulting must dependences
373 * and one for the resulting may dependences. May accesses can
374 * only lead to may dependences, so only one dependence is created
375 * for each of them.
376 * This function is private as isl_flow structures are only supposed
377 * to be created by isl_access_info_compute_flow.
379 static __isl_give isl_flow *isl_flow_alloc(__isl_keep isl_access_info *acc)
381 int i, n;
382 struct isl_ctx *ctx;
383 struct isl_flow *dep;
385 if (!acc)
386 return NULL;
388 ctx = isl_map_get_ctx(acc->sink.map);
389 dep = isl_calloc_type(ctx, struct isl_flow);
390 if (!dep)
391 return NULL;
393 n = 2 * acc->n_must + acc->n_may;
394 dep->dep = isl_calloc_array(ctx, struct isl_labeled_map, n);
395 if (n && !dep->dep)
396 goto error;
398 dep->n_source = n;
399 for (i = 0; i < acc->n_must; ++i) {
400 isl_space *dim;
401 dim = space_align_and_join(
402 isl_map_get_space(acc->source[i].map),
403 isl_space_reverse(isl_map_get_space(acc->sink.map)));
404 dep->dep[2 * i].map = isl_map_empty(dim);
405 dep->dep[2 * i + 1].map = isl_map_copy(dep->dep[2 * i].map);
406 dep->dep[2 * i].data = acc->source[i].data;
407 dep->dep[2 * i + 1].data = acc->source[i].data;
408 dep->dep[2 * i].must = 1;
409 dep->dep[2 * i + 1].must = 0;
410 if (!dep->dep[2 * i].map || !dep->dep[2 * i + 1].map)
411 goto error;
413 for (i = acc->n_must; i < acc->n_must + acc->n_may; ++i) {
414 isl_space *dim;
415 dim = space_align_and_join(
416 isl_map_get_space(acc->source[i].map),
417 isl_space_reverse(isl_map_get_space(acc->sink.map)));
418 dep->dep[acc->n_must + i].map = isl_map_empty(dim);
419 dep->dep[acc->n_must + i].data = acc->source[i].data;
420 dep->dep[acc->n_must + i].must = 0;
421 if (!dep->dep[acc->n_must + i].map)
422 goto error;
425 return dep;
426 error:
427 isl_flow_free(dep);
428 return NULL;
431 /* Iterate over all sources and for each resulting flow dependence
432 * that is not empty, call the user specfied function.
433 * The second argument in this function call identifies the source,
434 * while the third argument correspond to the final argument of
435 * the isl_flow_foreach call.
437 int isl_flow_foreach(__isl_keep isl_flow *deps,
438 int (*fn)(__isl_take isl_map *dep, int must, void *dep_user, void *user),
439 void *user)
441 int i;
443 if (!deps)
444 return -1;
446 for (i = 0; i < deps->n_source; ++i) {
447 if (isl_map_plain_is_empty(deps->dep[i].map))
448 continue;
449 if (fn(isl_map_copy(deps->dep[i].map), deps->dep[i].must,
450 deps->dep[i].data, user) < 0)
451 return -1;
454 return 0;
457 /* Return a copy of the subset of the sink for which no source could be found.
459 __isl_give isl_map *isl_flow_get_no_source(__isl_keep isl_flow *deps, int must)
461 if (!deps)
462 return NULL;
464 if (must)
465 return isl_set_unwrap(isl_set_copy(deps->must_no_source));
466 else
467 return isl_set_unwrap(isl_set_copy(deps->may_no_source));
470 void isl_flow_free(__isl_take isl_flow *deps)
472 int i;
474 if (!deps)
475 return;
476 isl_set_free(deps->must_no_source);
477 isl_set_free(deps->may_no_source);
478 if (deps->dep) {
479 for (i = 0; i < deps->n_source; ++i)
480 isl_map_free(deps->dep[i].map);
481 free(deps->dep);
483 free(deps);
486 isl_ctx *isl_flow_get_ctx(__isl_keep isl_flow *deps)
488 return deps ? isl_set_get_ctx(deps->must_no_source) : NULL;
491 /* Return a map that enforces that the domain iteration occurs after
492 * the range iteration at the given level.
493 * If level is odd, then the domain iteration should occur after
494 * the target iteration in their shared level/2 outermost loops.
495 * In this case we simply need to enforce that these outermost
496 * loop iterations are the same.
497 * If level is even, then the loop iterator of the domain should
498 * be greater than the loop iterator of the range at the last
499 * of the level/2 shared loops, i.e., loop level/2 - 1.
501 static __isl_give isl_map *after_at_level(__isl_take isl_space *dim, int level)
503 struct isl_basic_map *bmap;
505 if (level % 2)
506 bmap = isl_basic_map_equal(dim, level/2);
507 else
508 bmap = isl_basic_map_more_at(dim, level/2 - 1);
510 return isl_map_from_basic_map(bmap);
513 /* Compute the partial lexicographic maximum of "dep" on domain "sink",
514 * but first check if the user has set acc->restrict_fn and if so
515 * update either the input or the output of the maximization problem
516 * with respect to the resulting restriction.
518 * Since the user expects a mapping from sink iterations to source iterations,
519 * whereas the domain of "dep" is a wrapped map, mapping sink iterations
520 * to accessed array elements, we first need to project out the accessed
521 * sink array elements by applying acc->domain_map.
522 * Similarly, the sink restriction specified by the user needs to be
523 * converted back to the wrapped map.
525 static __isl_give isl_map *restricted_partial_lexmax(
526 __isl_keep isl_access_info *acc, __isl_take isl_map *dep,
527 int source, __isl_take isl_set *sink, __isl_give isl_set **empty)
529 isl_map *source_map;
530 isl_restriction *restr;
531 isl_set *sink_domain;
532 isl_set *sink_restr;
533 isl_map *res;
535 if (!acc->restrict_fn)
536 return isl_map_partial_lexmax(dep, sink, empty);
538 source_map = isl_map_copy(dep);
539 source_map = isl_map_apply_domain(source_map,
540 isl_map_copy(acc->domain_map));
541 sink_domain = isl_set_copy(sink);
542 sink_domain = isl_set_apply(sink_domain, isl_map_copy(acc->domain_map));
543 restr = acc->restrict_fn(source_map, sink_domain,
544 acc->source[source].data, acc->restrict_user);
545 isl_set_free(sink_domain);
546 isl_map_free(source_map);
548 if (!restr)
549 goto error;
550 if (restr->type == isl_restriction_type_input) {
551 dep = isl_map_intersect_range(dep, isl_set_copy(restr->source));
552 sink_restr = isl_set_copy(restr->sink);
553 sink_restr = isl_set_apply(sink_restr,
554 isl_map_reverse(isl_map_copy(acc->domain_map)));
555 sink = isl_set_intersect(sink, sink_restr);
556 } else if (restr->type == isl_restriction_type_empty) {
557 isl_space *space = isl_map_get_space(dep);
558 isl_map_free(dep);
559 dep = isl_map_empty(space);
562 res = isl_map_partial_lexmax(dep, sink, empty);
564 if (restr->type == isl_restriction_type_output)
565 res = isl_map_intersect_range(res, isl_set_copy(restr->source));
567 isl_restriction_free(restr);
568 return res;
569 error:
570 isl_map_free(dep);
571 isl_set_free(sink);
572 *empty = NULL;
573 return NULL;
576 /* Compute the last iteration of must source j that precedes the sink
577 * at the given level for sink iterations in set_C.
578 * The subset of set_C for which no such iteration can be found is returned
579 * in *empty.
581 static struct isl_map *last_source(struct isl_access_info *acc,
582 struct isl_set *set_C,
583 int j, int level, struct isl_set **empty)
585 struct isl_map *read_map;
586 struct isl_map *write_map;
587 struct isl_map *dep_map;
588 struct isl_map *after;
589 struct isl_map *result;
591 read_map = isl_map_copy(acc->sink.map);
592 write_map = isl_map_copy(acc->source[j].map);
593 write_map = isl_map_reverse(write_map);
594 dep_map = isl_map_apply_range(read_map, write_map);
595 after = after_at_level(isl_map_get_space(dep_map), level);
596 dep_map = isl_map_intersect(dep_map, after);
597 result = restricted_partial_lexmax(acc, dep_map, j, set_C, empty);
598 result = isl_map_reverse(result);
600 return result;
603 /* For a given mapping between iterations of must source j and iterations
604 * of the sink, compute the last iteration of must source k preceding
605 * the sink at level before_level for any of the sink iterations,
606 * but following the corresponding iteration of must source j at level
607 * after_level.
609 static struct isl_map *last_later_source(struct isl_access_info *acc,
610 struct isl_map *old_map,
611 int j, int before_level,
612 int k, int after_level,
613 struct isl_set **empty)
615 isl_space *dim;
616 struct isl_set *set_C;
617 struct isl_map *read_map;
618 struct isl_map *write_map;
619 struct isl_map *dep_map;
620 struct isl_map *after_write;
621 struct isl_map *before_read;
622 struct isl_map *result;
624 set_C = isl_map_range(isl_map_copy(old_map));
625 read_map = isl_map_copy(acc->sink.map);
626 write_map = isl_map_copy(acc->source[k].map);
628 write_map = isl_map_reverse(write_map);
629 dep_map = isl_map_apply_range(read_map, write_map);
630 dim = space_align_and_join(isl_map_get_space(acc->source[k].map),
631 isl_space_reverse(isl_map_get_space(acc->source[j].map)));
632 after_write = after_at_level(dim, after_level);
633 after_write = isl_map_apply_range(after_write, old_map);
634 after_write = isl_map_reverse(after_write);
635 dep_map = isl_map_intersect(dep_map, after_write);
636 before_read = after_at_level(isl_map_get_space(dep_map), before_level);
637 dep_map = isl_map_intersect(dep_map, before_read);
638 result = restricted_partial_lexmax(acc, dep_map, k, set_C, empty);
639 result = isl_map_reverse(result);
641 return result;
644 /* Given a shared_level between two accesses, return 1 if the
645 * the first can precede the second at the requested target_level.
646 * If the target level is odd, i.e., refers to a statement level
647 * dimension, then first needs to precede second at the requested
648 * level, i.e., shared_level must be equal to target_level.
649 * If the target level is odd, then the two loops should share
650 * at least the requested number of outer loops.
652 static int can_precede_at_level(int shared_level, int target_level)
654 if (shared_level < target_level)
655 return 0;
656 if ((target_level % 2) && shared_level > target_level)
657 return 0;
658 return 1;
661 /* Given a possible flow dependence temp_rel[j] between source j and the sink
662 * at level sink_level, remove those elements for which
663 * there is an iteration of another source k < j that is closer to the sink.
664 * The flow dependences temp_rel[k] are updated with the improved sources.
665 * Any improved source needs to precede the sink at the same level
666 * and needs to follow source j at the same or a deeper level.
667 * The lower this level, the later the execution date of source k.
668 * We therefore consider lower levels first.
670 * If temp_rel[j] is empty, then there can be no improvement and
671 * we return immediately.
673 static int intermediate_sources(__isl_keep isl_access_info *acc,
674 struct isl_map **temp_rel, int j, int sink_level)
676 int k, level;
677 int depth = 2 * isl_map_dim(acc->source[j].map, isl_dim_in) + 1;
679 if (isl_map_plain_is_empty(temp_rel[j]))
680 return 0;
682 for (k = j - 1; k >= 0; --k) {
683 int plevel, plevel2;
684 plevel = acc->level_before(acc->source[k].data, acc->sink.data);
685 if (!can_precede_at_level(plevel, sink_level))
686 continue;
688 plevel2 = acc->level_before(acc->source[j].data,
689 acc->source[k].data);
691 for (level = sink_level; level <= depth; ++level) {
692 struct isl_map *T;
693 struct isl_set *trest;
694 struct isl_map *copy;
696 if (!can_precede_at_level(plevel2, level))
697 continue;
699 copy = isl_map_copy(temp_rel[j]);
700 T = last_later_source(acc, copy, j, sink_level, k,
701 level, &trest);
702 if (isl_map_plain_is_empty(T)) {
703 isl_set_free(trest);
704 isl_map_free(T);
705 continue;
707 temp_rel[j] = isl_map_intersect_range(temp_rel[j], trest);
708 temp_rel[k] = isl_map_union_disjoint(temp_rel[k], T);
712 return 0;
715 /* Compute all iterations of may source j that precedes the sink at the given
716 * level for sink iterations in set_C.
718 static __isl_give isl_map *all_sources(__isl_keep isl_access_info *acc,
719 __isl_take isl_set *set_C, int j, int level)
721 isl_map *read_map;
722 isl_map *write_map;
723 isl_map *dep_map;
724 isl_map *after;
726 read_map = isl_map_copy(acc->sink.map);
727 read_map = isl_map_intersect_domain(read_map, set_C);
728 write_map = isl_map_copy(acc->source[acc->n_must + j].map);
729 write_map = isl_map_reverse(write_map);
730 dep_map = isl_map_apply_range(read_map, write_map);
731 after = after_at_level(isl_map_get_space(dep_map), level);
732 dep_map = isl_map_intersect(dep_map, after);
734 return isl_map_reverse(dep_map);
737 /* For a given mapping between iterations of must source k and iterations
738 * of the sink, compute the all iteration of may source j preceding
739 * the sink at level before_level for any of the sink iterations,
740 * but following the corresponding iteration of must source k at level
741 * after_level.
743 static __isl_give isl_map *all_later_sources(__isl_keep isl_access_info *acc,
744 __isl_take isl_map *old_map,
745 int j, int before_level, int k, int after_level)
747 isl_space *dim;
748 isl_set *set_C;
749 isl_map *read_map;
750 isl_map *write_map;
751 isl_map *dep_map;
752 isl_map *after_write;
753 isl_map *before_read;
755 set_C = isl_map_range(isl_map_copy(old_map));
756 read_map = isl_map_copy(acc->sink.map);
757 read_map = isl_map_intersect_domain(read_map, set_C);
758 write_map = isl_map_copy(acc->source[acc->n_must + j].map);
760 write_map = isl_map_reverse(write_map);
761 dep_map = isl_map_apply_range(read_map, write_map);
762 dim = isl_space_join(isl_map_get_space(acc->source[acc->n_must + j].map),
763 isl_space_reverse(isl_map_get_space(acc->source[k].map)));
764 after_write = after_at_level(dim, after_level);
765 after_write = isl_map_apply_range(after_write, old_map);
766 after_write = isl_map_reverse(after_write);
767 dep_map = isl_map_intersect(dep_map, after_write);
768 before_read = after_at_level(isl_map_get_space(dep_map), before_level);
769 dep_map = isl_map_intersect(dep_map, before_read);
770 return isl_map_reverse(dep_map);
773 /* Given the must and may dependence relations for the must accesses
774 * for level sink_level, check if there are any accesses of may access j
775 * that occur in between and return their union.
776 * If some of these accesses are intermediate with respect to
777 * (previously thought to be) must dependences, then these
778 * must dependences are turned into may dependences.
780 static __isl_give isl_map *all_intermediate_sources(
781 __isl_keep isl_access_info *acc, __isl_take isl_map *map,
782 struct isl_map **must_rel, struct isl_map **may_rel,
783 int j, int sink_level)
785 int k, level;
786 int depth = 2 * isl_map_dim(acc->source[acc->n_must + j].map,
787 isl_dim_in) + 1;
789 for (k = 0; k < acc->n_must; ++k) {
790 int plevel;
792 if (isl_map_plain_is_empty(may_rel[k]) &&
793 isl_map_plain_is_empty(must_rel[k]))
794 continue;
796 plevel = acc->level_before(acc->source[k].data,
797 acc->source[acc->n_must + j].data);
799 for (level = sink_level; level <= depth; ++level) {
800 isl_map *T;
801 isl_map *copy;
802 isl_set *ran;
804 if (!can_precede_at_level(plevel, level))
805 continue;
807 copy = isl_map_copy(may_rel[k]);
808 T = all_later_sources(acc, copy, j, sink_level, k, level);
809 map = isl_map_union(map, T);
811 copy = isl_map_copy(must_rel[k]);
812 T = all_later_sources(acc, copy, j, sink_level, k, level);
813 ran = isl_map_range(isl_map_copy(T));
814 map = isl_map_union(map, T);
815 may_rel[k] = isl_map_union_disjoint(may_rel[k],
816 isl_map_intersect_range(isl_map_copy(must_rel[k]),
817 isl_set_copy(ran)));
818 T = isl_map_from_domain_and_range(
819 isl_set_universe(
820 isl_space_domain(isl_map_get_space(must_rel[k]))),
821 ran);
822 must_rel[k] = isl_map_subtract(must_rel[k], T);
826 return map;
829 /* Compute dependences for the case where all accesses are "may"
830 * accesses, which boils down to computing memory based dependences.
831 * The generic algorithm would also work in this case, but it would
832 * be overkill to use it.
834 static __isl_give isl_flow *compute_mem_based_dependences(
835 __isl_keep isl_access_info *acc)
837 int i;
838 isl_set *mustdo;
839 isl_set *maydo;
840 isl_flow *res;
842 res = isl_flow_alloc(acc);
843 if (!res)
844 return NULL;
846 mustdo = isl_map_domain(isl_map_copy(acc->sink.map));
847 maydo = isl_set_copy(mustdo);
849 for (i = 0; i < acc->n_may; ++i) {
850 int plevel;
851 int is_before;
852 isl_space *dim;
853 isl_map *before;
854 isl_map *dep;
856 plevel = acc->level_before(acc->source[i].data, acc->sink.data);
857 is_before = plevel & 1;
858 plevel >>= 1;
860 dim = isl_map_get_space(res->dep[i].map);
861 if (is_before)
862 before = isl_map_lex_le_first(dim, plevel);
863 else
864 before = isl_map_lex_lt_first(dim, plevel);
865 dep = isl_map_apply_range(isl_map_copy(acc->source[i].map),
866 isl_map_reverse(isl_map_copy(acc->sink.map)));
867 dep = isl_map_intersect(dep, before);
868 mustdo = isl_set_subtract(mustdo,
869 isl_map_range(isl_map_copy(dep)));
870 res->dep[i].map = isl_map_union(res->dep[i].map, dep);
873 res->may_no_source = isl_set_subtract(maydo, isl_set_copy(mustdo));
874 res->must_no_source = mustdo;
876 return res;
879 /* Compute dependences for the case where there is at least one
880 * "must" access.
882 * The core algorithm considers all levels in which a source may precede
883 * the sink, where a level may either be a statement level or a loop level.
884 * The outermost statement level is 1, the first loop level is 2, etc...
885 * The algorithm basically does the following:
886 * for all levels l of the read access from innermost to outermost
887 * for all sources w that may precede the sink access at that level
888 * compute the last iteration of the source that precedes the sink access
889 * at that level
890 * add result to possible last accesses at level l of source w
891 * for all sources w2 that we haven't considered yet at this level that may
892 * also precede the sink access
893 * for all levels l2 of w from l to innermost
894 * for all possible last accesses dep of w at l
895 * compute last iteration of w2 between the source and sink
896 * of dep
897 * add result to possible last accesses at level l of write w2
898 * and replace possible last accesses dep by the remainder
901 * The above algorithm is applied to the must access. During the course
902 * of the algorithm, we keep track of sink iterations that still
903 * need to be considered. These iterations are split into those that
904 * haven't been matched to any source access (mustdo) and those that have only
905 * been matched to may accesses (maydo).
906 * At the end of each level, we also consider the may accesses.
907 * In particular, we consider may accesses that precede the remaining
908 * sink iterations, moving elements from mustdo to maydo when appropriate,
909 * and may accesses that occur between a must source and a sink of any
910 * dependences found at the current level, turning must dependences into
911 * may dependences when appropriate.
914 static __isl_give isl_flow *compute_val_based_dependences(
915 __isl_keep isl_access_info *acc)
917 isl_ctx *ctx;
918 isl_flow *res;
919 isl_set *mustdo = NULL;
920 isl_set *maydo = NULL;
921 int level, j;
922 int depth;
923 isl_map **must_rel = NULL;
924 isl_map **may_rel = NULL;
926 if (!acc)
927 return NULL;
929 res = isl_flow_alloc(acc);
930 if (!res)
931 goto error;
932 ctx = isl_map_get_ctx(acc->sink.map);
934 depth = 2 * isl_map_dim(acc->sink.map, isl_dim_in) + 1;
935 mustdo = isl_map_domain(isl_map_copy(acc->sink.map));
936 maydo = isl_set_empty_like(mustdo);
937 if (!mustdo || !maydo)
938 goto error;
939 if (isl_set_plain_is_empty(mustdo))
940 goto done;
942 must_rel = isl_alloc_array(ctx, struct isl_map *, acc->n_must);
943 may_rel = isl_alloc_array(ctx, struct isl_map *, acc->n_must);
944 if (!must_rel || !may_rel)
945 goto error;
947 for (level = depth; level >= 1; --level) {
948 for (j = acc->n_must-1; j >=0; --j) {
949 must_rel[j] = isl_map_empty_like(res->dep[2 * j].map);
950 may_rel[j] = isl_map_copy(must_rel[j]);
953 for (j = acc->n_must - 1; j >= 0; --j) {
954 struct isl_map *T;
955 struct isl_set *rest;
956 int plevel;
958 plevel = acc->level_before(acc->source[j].data,
959 acc->sink.data);
960 if (!can_precede_at_level(plevel, level))
961 continue;
963 T = last_source(acc, mustdo, j, level, &rest);
964 must_rel[j] = isl_map_union_disjoint(must_rel[j], T);
965 mustdo = rest;
967 intermediate_sources(acc, must_rel, j, level);
969 T = last_source(acc, maydo, j, level, &rest);
970 may_rel[j] = isl_map_union_disjoint(may_rel[j], T);
971 maydo = rest;
973 intermediate_sources(acc, may_rel, j, level);
975 if (isl_set_plain_is_empty(mustdo) &&
976 isl_set_plain_is_empty(maydo))
977 break;
979 for (j = j - 1; j >= 0; --j) {
980 int plevel;
982 plevel = acc->level_before(acc->source[j].data,
983 acc->sink.data);
984 if (!can_precede_at_level(plevel, level))
985 continue;
987 intermediate_sources(acc, must_rel, j, level);
988 intermediate_sources(acc, may_rel, j, level);
991 for (j = 0; j < acc->n_may; ++j) {
992 int plevel;
993 isl_map *T;
994 isl_set *ran;
996 plevel = acc->level_before(acc->source[acc->n_must + j].data,
997 acc->sink.data);
998 if (!can_precede_at_level(plevel, level))
999 continue;
1001 T = all_sources(acc, isl_set_copy(maydo), j, level);
1002 res->dep[2 * acc->n_must + j].map =
1003 isl_map_union(res->dep[2 * acc->n_must + j].map, T);
1004 T = all_sources(acc, isl_set_copy(mustdo), j, level);
1005 ran = isl_map_range(isl_map_copy(T));
1006 res->dep[2 * acc->n_must + j].map =
1007 isl_map_union(res->dep[2 * acc->n_must + j].map, T);
1008 mustdo = isl_set_subtract(mustdo, isl_set_copy(ran));
1009 maydo = isl_set_union_disjoint(maydo, ran);
1011 T = res->dep[2 * acc->n_must + j].map;
1012 T = all_intermediate_sources(acc, T, must_rel, may_rel,
1013 j, level);
1014 res->dep[2 * acc->n_must + j].map = T;
1017 for (j = acc->n_must - 1; j >= 0; --j) {
1018 res->dep[2 * j].map =
1019 isl_map_union_disjoint(res->dep[2 * j].map,
1020 must_rel[j]);
1021 res->dep[2 * j + 1].map =
1022 isl_map_union_disjoint(res->dep[2 * j + 1].map,
1023 may_rel[j]);
1026 if (isl_set_plain_is_empty(mustdo) &&
1027 isl_set_plain_is_empty(maydo))
1028 break;
1031 free(must_rel);
1032 free(may_rel);
1033 done:
1034 res->must_no_source = mustdo;
1035 res->may_no_source = maydo;
1036 return res;
1037 error:
1038 isl_flow_free(res);
1039 isl_set_free(mustdo);
1040 isl_set_free(maydo);
1041 free(must_rel);
1042 free(may_rel);
1043 return NULL;
1046 /* Given a "sink" access, a list of n "source" accesses,
1047 * compute for each iteration of the sink access
1048 * and for each element accessed by that iteration,
1049 * the source access in the list that last accessed the
1050 * element accessed by the sink access before this sink access.
1051 * Each access is given as a map from the loop iterators
1052 * to the array indices.
1053 * The result is a list of n relations between source and sink
1054 * iterations and a subset of the domain of the sink access,
1055 * corresponding to those iterations that access an element
1056 * not previously accessed.
1058 * To deal with multi-valued sink access relations, the sink iteration
1059 * domain is first extended with dimensions that correspond to the data
1060 * space. After the computation is finished, these extra dimensions are
1061 * projected out again.
1063 __isl_give isl_flow *isl_access_info_compute_flow(__isl_take isl_access_info *acc)
1065 int j;
1066 struct isl_flow *res = NULL;
1068 if (!acc)
1069 return NULL;
1071 acc->domain_map = isl_map_domain_map(isl_map_copy(acc->sink.map));
1072 acc->sink.map = isl_map_range_map(acc->sink.map);
1073 if (!acc->sink.map)
1074 goto error;
1076 if (acc->n_must == 0)
1077 res = compute_mem_based_dependences(acc);
1078 else {
1079 acc = isl_access_info_sort_sources(acc);
1080 res = compute_val_based_dependences(acc);
1082 if (!res)
1083 goto error;
1085 for (j = 0; j < res->n_source; ++j) {
1086 res->dep[j].map = isl_map_apply_range(res->dep[j].map,
1087 isl_map_copy(acc->domain_map));
1088 if (!res->dep[j].map)
1089 goto error;
1091 if (!res->must_no_source || !res->may_no_source)
1092 goto error;
1094 isl_access_info_free(acc);
1095 return res;
1096 error:
1097 isl_access_info_free(acc);
1098 isl_flow_free(res);
1099 return NULL;
1103 /* Keep track of some information about a schedule for a given
1104 * access. In particular, keep track of which dimensions
1105 * have a constant value and of the actual constant values.
1107 struct isl_sched_info {
1108 int *is_cst;
1109 isl_vec *cst;
1112 static void sched_info_free(__isl_take struct isl_sched_info *info)
1114 if (!info)
1115 return;
1116 isl_vec_free(info->cst);
1117 free(info->is_cst);
1118 free(info);
1121 /* Extract information on the constant dimensions of the schedule
1122 * for a given access. The "map" is of the form
1124 * [S -> D] -> A
1126 * with S the schedule domain, D the iteration domain and A the data domain.
1128 static __isl_give struct isl_sched_info *sched_info_alloc(
1129 __isl_keep isl_map *map)
1131 isl_ctx *ctx;
1132 isl_space *dim;
1133 struct isl_sched_info *info;
1134 int i, n;
1136 if (!map)
1137 return NULL;
1139 dim = isl_space_unwrap(isl_space_domain(isl_map_get_space(map)));
1140 if (!dim)
1141 return NULL;
1142 n = isl_space_dim(dim, isl_dim_in);
1143 isl_space_free(dim);
1145 ctx = isl_map_get_ctx(map);
1146 info = isl_alloc_type(ctx, struct isl_sched_info);
1147 if (!info)
1148 return NULL;
1149 info->is_cst = isl_alloc_array(ctx, int, n);
1150 info->cst = isl_vec_alloc(ctx, n);
1151 if (n && (!info->is_cst || !info->cst))
1152 goto error;
1154 for (i = 0; i < n; ++i) {
1155 isl_val *v;
1157 v = isl_map_plain_get_val_if_fixed(map, isl_dim_in, i);
1158 if (!v)
1159 goto error;
1160 info->is_cst[i] = !isl_val_is_nan(v);
1161 if (info->is_cst[i])
1162 info->cst = isl_vec_set_element_val(info->cst, i, v);
1163 else
1164 isl_val_free(v);
1167 return info;
1168 error:
1169 sched_info_free(info);
1170 return NULL;
1173 /* This structure represents the input for a dependence analysis computation.
1175 * "sink" represents the sink accesses.
1176 * "must_source" represents the definite source accesses.
1177 * "may_source" represents the possible source accesses.
1179 * "schedule" or "schedule_map" represents the execution order.
1180 * Exactly one of these fields should be NULL. The other field
1181 * determines the execution order.
1183 * The domains of these four maps refer to the same iteration spaces(s).
1184 * The ranges of the first three maps also refer to the same data space(s).
1186 * After a call to isl_union_access_info_introduce_schedule,
1187 * the "schedule_map" field no longer contains useful information.
1189 struct isl_union_access_info {
1190 isl_union_map *sink;
1191 isl_union_map *must_source;
1192 isl_union_map *may_source;
1194 isl_schedule *schedule;
1195 isl_union_map *schedule_map;
1198 /* Free "access" and return NULL.
1200 __isl_null isl_union_access_info *isl_union_access_info_free(
1201 __isl_take isl_union_access_info *access)
1203 if (!access)
1204 return NULL;
1206 isl_union_map_free(access->sink);
1207 isl_union_map_free(access->must_source);
1208 isl_union_map_free(access->may_source);
1209 isl_schedule_free(access->schedule);
1210 isl_union_map_free(access->schedule_map);
1211 free(access);
1213 return NULL;
1216 /* Return the isl_ctx to which "access" belongs.
1218 isl_ctx *isl_union_access_info_get_ctx(__isl_keep isl_union_access_info *access)
1220 return access ? isl_union_map_get_ctx(access->sink) : NULL;
1223 /* Create a new isl_union_access_info with the given sink accesses and
1224 * and no source accesses or schedule information.
1226 * By default, we use the schedule field of the isl_union_access_info,
1227 * but this may be overridden by a call
1228 * to isl_union_access_info_set_schedule_map.
1230 __isl_give isl_union_access_info *isl_union_access_info_from_sink(
1231 __isl_take isl_union_map *sink)
1233 isl_ctx *ctx;
1234 isl_space *space;
1235 isl_union_map *empty;
1236 isl_union_access_info *access;
1238 if (!sink)
1239 return NULL;
1240 ctx = isl_union_map_get_ctx(sink);
1241 access = isl_alloc_type(ctx, isl_union_access_info);
1242 if (!access)
1243 goto error;
1245 space = isl_union_map_get_space(sink);
1246 empty = isl_union_map_empty(isl_space_copy(space));
1247 access->sink = sink;
1248 access->must_source = isl_union_map_copy(empty);
1249 access->may_source = empty;
1250 access->schedule = isl_schedule_empty(space);
1251 access->schedule_map = NULL;
1253 if (!access->sink || !access->must_source ||
1254 !access->may_source || !access->schedule)
1255 return isl_union_access_info_free(access);
1257 return access;
1258 error:
1259 isl_union_map_free(sink);
1260 return NULL;
1263 /* Replace the definite source accesses of "access" by "must_source".
1265 __isl_give isl_union_access_info *isl_union_access_info_set_must_source(
1266 __isl_take isl_union_access_info *access,
1267 __isl_take isl_union_map *must_source)
1269 if (!access || !must_source)
1270 goto error;
1272 isl_union_map_free(access->must_source);
1273 access->must_source = must_source;
1275 return access;
1276 error:
1277 isl_union_access_info_free(access);
1278 isl_union_map_free(must_source);
1279 return NULL;
1282 /* Replace the possible source accesses of "access" by "may_source".
1284 __isl_give isl_union_access_info *isl_union_access_info_set_may_source(
1285 __isl_take isl_union_access_info *access,
1286 __isl_take isl_union_map *may_source)
1288 if (!access || !may_source)
1289 goto error;
1291 isl_union_map_free(access->may_source);
1292 access->may_source = may_source;
1294 return access;
1295 error:
1296 isl_union_access_info_free(access);
1297 isl_union_map_free(may_source);
1298 return NULL;
1301 /* Replace the schedule of "access" by "schedule".
1302 * Also free the schedule_map in case it was set last.
1304 __isl_give isl_union_access_info *isl_union_access_info_set_schedule(
1305 __isl_take isl_union_access_info *access,
1306 __isl_take isl_schedule *schedule)
1308 if (!access || !schedule)
1309 goto error;
1311 access->schedule_map = isl_union_map_free(access->schedule_map);
1312 isl_schedule_free(access->schedule);
1313 access->schedule = schedule;
1315 return access;
1316 error:
1317 isl_union_access_info_free(access);
1318 isl_schedule_free(schedule);
1319 return NULL;
1322 /* Replace the schedule map of "access" by "schedule_map".
1323 * Also free the schedule in case it was set last.
1325 __isl_give isl_union_access_info *isl_union_access_info_set_schedule_map(
1326 __isl_take isl_union_access_info *access,
1327 __isl_take isl_union_map *schedule_map)
1329 if (!access || !schedule_map)
1330 goto error;
1332 isl_union_map_free(access->schedule_map);
1333 access->schedule = isl_schedule_free(access->schedule);
1334 access->schedule_map = schedule_map;
1336 return access;
1337 error:
1338 isl_union_access_info_free(access);
1339 isl_union_map_free(schedule_map);
1340 return NULL;
1343 /* Update the fields of "access" such that they all have the same parameters,
1344 * keeping in mind that the schedule_map field may be NULL and ignoring
1345 * the schedule field.
1347 static __isl_give isl_union_access_info *isl_union_access_info_align_params(
1348 __isl_take isl_union_access_info *access)
1350 isl_space *space;
1352 if (!access)
1353 return NULL;
1355 space = isl_union_map_get_space(access->sink);
1356 space = isl_space_align_params(space,
1357 isl_union_map_get_space(access->must_source));
1358 space = isl_space_align_params(space,
1359 isl_union_map_get_space(access->may_source));
1360 if (access->schedule_map)
1361 space = isl_space_align_params(space,
1362 isl_union_map_get_space(access->schedule_map));
1363 access->sink = isl_union_map_align_params(access->sink,
1364 isl_space_copy(space));
1365 access->must_source = isl_union_map_align_params(access->must_source,
1366 isl_space_copy(space));
1367 access->may_source = isl_union_map_align_params(access->may_source,
1368 isl_space_copy(space));
1369 if (!access->schedule_map) {
1370 isl_space_free(space);
1371 } else {
1372 access->schedule_map =
1373 isl_union_map_align_params(access->schedule_map, space);
1374 if (!access->schedule_map)
1375 return isl_union_access_info_free(access);
1378 if (!access->sink || !access->must_source || !access->may_source)
1379 return isl_union_access_info_free(access);
1381 return access;
1384 /* Prepend the schedule dimensions to the iteration domains.
1386 * That is, if the schedule is of the form
1388 * D -> S
1390 * while the access relations are of the form
1392 * D -> A
1394 * then the updated access relations are of the form
1396 * [S -> D] -> A
1398 * The schedule map is also replaced by the map
1400 * [S -> D] -> D
1402 * that is used during the internal computation.
1403 * Neither the original schedule map nor this updated schedule map
1404 * are used after the call to this function.
1406 static __isl_give isl_union_access_info *
1407 isl_union_access_info_introduce_schedule(
1408 __isl_take isl_union_access_info *access)
1410 isl_union_map *sm;
1412 if (!access)
1413 return NULL;
1415 sm = isl_union_map_reverse(access->schedule_map);
1416 sm = isl_union_map_range_map(sm);
1417 access->sink = isl_union_map_apply_range(isl_union_map_copy(sm),
1418 access->sink);
1419 access->may_source = isl_union_map_apply_range(isl_union_map_copy(sm),
1420 access->may_source);
1421 access->must_source = isl_union_map_apply_range(isl_union_map_copy(sm),
1422 access->must_source);
1423 access->schedule_map = sm;
1425 if (!access->sink || !access->must_source ||
1426 !access->may_source || !access->schedule_map)
1427 return isl_union_access_info_free(access);
1429 return access;
1432 /* This structure epresents the result of a dependence analysis computation.
1434 * "must_dep" represents the definite dependences.
1435 * "may_dep" represents the non-definite dependences.
1436 * "must_no_source" represents the subset of the sink accesses for which
1437 * definitely no source was found.
1438 * "may_no_source" represents the subset of the sink accesses for which
1439 * possibly, but not definitely, no source was found.
1441 struct isl_union_flow {
1442 isl_union_map *must_dep;
1443 isl_union_map *may_dep;
1444 isl_union_map *must_no_source;
1445 isl_union_map *may_no_source;
1448 /* Free "flow" and return NULL.
1450 __isl_null isl_union_flow *isl_union_flow_free(__isl_take isl_union_flow *flow)
1452 if (!flow)
1453 return NULL;
1454 isl_union_map_free(flow->must_dep);
1455 isl_union_map_free(flow->may_dep);
1456 isl_union_map_free(flow->must_no_source);
1457 isl_union_map_free(flow->may_no_source);
1458 free(flow);
1459 return NULL;
1462 void isl_union_flow_dump(__isl_keep isl_union_flow *flow)
1464 if (!flow)
1465 return;
1467 fprintf(stderr, "must dependences: ");
1468 isl_union_map_dump(flow->must_dep);
1469 fprintf(stderr, "may dependences: ");
1470 isl_union_map_dump(flow->may_dep);
1471 fprintf(stderr, "must no source: ");
1472 isl_union_map_dump(flow->must_no_source);
1473 fprintf(stderr, "may no source: ");
1474 isl_union_map_dump(flow->may_no_source);
1477 /* Return the definite dependences in "flow".
1479 __isl_give isl_union_map *isl_union_flow_get_must_dependence(
1480 __isl_keep isl_union_flow *flow)
1482 if (!flow)
1483 return NULL;
1484 return isl_union_map_copy(flow->must_dep);
1487 /* Return the possible dependences in "flow", including the definite
1488 * dependences.
1490 __isl_give isl_union_map *isl_union_flow_get_may_dependence(
1491 __isl_keep isl_union_flow *flow)
1493 if (!flow)
1494 return NULL;
1495 return isl_union_map_union(isl_union_map_copy(flow->must_dep),
1496 isl_union_map_copy(flow->may_dep));
1499 /* Return the non-definite dependences in "flow".
1501 static __isl_give isl_union_map *isl_union_flow_get_non_must_dependence(
1502 __isl_keep isl_union_flow *flow)
1504 if (!flow)
1505 return NULL;
1506 return isl_union_map_copy(flow->may_dep);
1509 /* Return the subset of the sink accesses for which definitely
1510 * no source was found.
1512 __isl_give isl_union_map *isl_union_flow_get_must_no_source(
1513 __isl_keep isl_union_flow *flow)
1515 if (!flow)
1516 return NULL;
1517 return isl_union_map_copy(flow->must_no_source);
1520 /* Return the subset of the sink accesses for which possibly
1521 * no source was found, including those for which definitely
1522 * no source was found.
1524 __isl_give isl_union_map *isl_union_flow_get_may_no_source(
1525 __isl_keep isl_union_flow *flow)
1527 if (!flow)
1528 return NULL;
1529 return isl_union_map_union(isl_union_map_copy(flow->must_no_source),
1530 isl_union_map_copy(flow->may_no_source));
1533 /* Return the subset of the sink accesses for which possibly, but not
1534 * definitely, no source was found.
1536 static __isl_give isl_union_map *isl_union_flow_get_non_must_no_source(
1537 __isl_keep isl_union_flow *flow)
1539 if (!flow)
1540 return NULL;
1541 return isl_union_map_copy(flow->may_no_source);
1544 /* Create a new isl_union_flow object, initialized with empty
1545 * dependence relations and sink subsets.
1547 static __isl_give isl_union_flow *isl_union_flow_alloc(
1548 __isl_take isl_space *space)
1550 isl_ctx *ctx;
1551 isl_union_map *empty;
1552 isl_union_flow *flow;
1554 if (!space)
1555 return NULL;
1556 ctx = isl_space_get_ctx(space);
1557 flow = isl_alloc_type(ctx, isl_union_flow);
1558 if (!flow)
1559 goto error;
1561 empty = isl_union_map_empty(space);
1562 flow->must_dep = isl_union_map_copy(empty);
1563 flow->may_dep = isl_union_map_copy(empty);
1564 flow->must_no_source = isl_union_map_copy(empty);
1565 flow->may_no_source = empty;
1567 if (!flow->must_dep || !flow->may_dep ||
1568 !flow->must_no_source || !flow->may_no_source)
1569 return isl_union_flow_free(flow);
1571 return flow;
1572 error:
1573 isl_space_free(space);
1574 return NULL;
1577 /* Drop the schedule dimensions from the iteration domains in "flow".
1578 * In particular, the schedule dimensions have been prepended
1579 * to the iteration domains prior to the dependence analysis by
1580 * replacing the iteration domain D, by the wrapped map [S -> D].
1581 * Replace these wrapped maps by the original D.
1583 static __isl_give isl_union_flow *isl_union_flow_drop_schedule(
1584 __isl_take isl_union_flow *flow)
1586 if (!flow)
1587 return NULL;
1589 flow->must_dep = isl_union_map_factor_range(flow->must_dep);
1590 flow->may_dep = isl_union_map_factor_range(flow->may_dep);
1591 flow->must_no_source =
1592 isl_union_map_domain_factor_range(flow->must_no_source);
1593 flow->may_no_source =
1594 isl_union_map_domain_factor_range(flow->may_no_source);
1596 if (!flow->must_dep || !flow->may_dep ||
1597 !flow->must_no_source || !flow->may_no_source)
1598 return isl_union_flow_free(flow);
1600 return flow;
1603 struct isl_compute_flow_data {
1604 isl_union_map *must_source;
1605 isl_union_map *may_source;
1606 isl_union_flow *flow;
1608 int count;
1609 int must;
1610 isl_space *dim;
1611 struct isl_sched_info *sink_info;
1612 struct isl_sched_info **source_info;
1613 isl_access_info *accesses;
1616 static int count_matching_array(__isl_take isl_map *map, void *user)
1618 int eq;
1619 isl_space *dim;
1620 struct isl_compute_flow_data *data;
1622 data = (struct isl_compute_flow_data *)user;
1624 dim = isl_space_range(isl_map_get_space(map));
1626 eq = isl_space_is_equal(dim, data->dim);
1628 isl_space_free(dim);
1629 isl_map_free(map);
1631 if (eq < 0)
1632 return -1;
1633 if (eq)
1634 data->count++;
1636 return 0;
1639 static int collect_matching_array(__isl_take isl_map *map, void *user)
1641 int eq;
1642 isl_space *dim;
1643 struct isl_sched_info *info;
1644 struct isl_compute_flow_data *data;
1646 data = (struct isl_compute_flow_data *)user;
1648 dim = isl_space_range(isl_map_get_space(map));
1650 eq = isl_space_is_equal(dim, data->dim);
1652 isl_space_free(dim);
1654 if (eq < 0)
1655 goto error;
1656 if (!eq) {
1657 isl_map_free(map);
1658 return 0;
1661 info = sched_info_alloc(map);
1662 data->source_info[data->count] = info;
1664 data->accesses = isl_access_info_add_source(data->accesses,
1665 map, data->must, info);
1667 data->count++;
1669 return 0;
1670 error:
1671 isl_map_free(map);
1672 return -1;
1675 /* Determine the shared nesting level and the "textual order" of
1676 * the given accesses.
1678 * We first determine the minimal schedule dimension for both accesses.
1680 * If among those dimensions, we can find one where both have a fixed
1681 * value and if moreover those values are different, then the previous
1682 * dimension is the last shared nesting level and the textual order
1683 * is determined based on the order of the fixed values.
1684 * If no such fixed values can be found, then we set the shared
1685 * nesting level to the minimal schedule dimension, with no textual ordering.
1687 static int before(void *first, void *second)
1689 struct isl_sched_info *info1 = first;
1690 struct isl_sched_info *info2 = second;
1691 int n1, n2;
1692 int i;
1694 n1 = isl_vec_size(info1->cst);
1695 n2 = isl_vec_size(info2->cst);
1697 if (n2 < n1)
1698 n1 = n2;
1700 for (i = 0; i < n1; ++i) {
1701 int r;
1702 int cmp;
1704 if (!info1->is_cst[i])
1705 continue;
1706 if (!info2->is_cst[i])
1707 continue;
1708 cmp = isl_vec_cmp_element(info1->cst, info2->cst, i);
1709 if (cmp == 0)
1710 continue;
1712 r = 2 * i + (cmp < 0);
1714 return r;
1717 return 2 * n1;
1720 /* Given a sink access, look for all the source accesses that access
1721 * the same array and perform dataflow analysis on them using
1722 * isl_access_info_compute_flow.
1724 static int compute_flow(__isl_take isl_map *map, void *user)
1726 int i;
1727 isl_ctx *ctx;
1728 struct isl_compute_flow_data *data;
1729 isl_flow *flow;
1730 isl_union_flow *df;
1732 data = (struct isl_compute_flow_data *)user;
1733 df = data->flow;
1735 ctx = isl_map_get_ctx(map);
1737 data->accesses = NULL;
1738 data->sink_info = NULL;
1739 data->source_info = NULL;
1740 data->count = 0;
1741 data->dim = isl_space_range(isl_map_get_space(map));
1743 if (isl_union_map_foreach_map(data->must_source,
1744 &count_matching_array, data) < 0)
1745 goto error;
1746 if (isl_union_map_foreach_map(data->may_source,
1747 &count_matching_array, data) < 0)
1748 goto error;
1750 data->sink_info = sched_info_alloc(map);
1751 data->source_info = isl_calloc_array(ctx, struct isl_sched_info *,
1752 data->count);
1754 data->accesses = isl_access_info_alloc(isl_map_copy(map),
1755 data->sink_info, &before, data->count);
1756 if (!data->sink_info || (data->count && !data->source_info) ||
1757 !data->accesses)
1758 goto error;
1759 data->count = 0;
1760 data->must = 1;
1761 if (isl_union_map_foreach_map(data->must_source,
1762 &collect_matching_array, data) < 0)
1763 goto error;
1764 data->must = 0;
1765 if (isl_union_map_foreach_map(data->may_source,
1766 &collect_matching_array, data) < 0)
1767 goto error;
1769 flow = isl_access_info_compute_flow(data->accesses);
1770 data->accesses = NULL;
1772 if (!flow)
1773 goto error;
1775 df->must_no_source = isl_union_map_union(df->must_no_source,
1776 isl_union_map_from_map(isl_flow_get_no_source(flow, 1)));
1777 df->may_no_source = isl_union_map_union(df->may_no_source,
1778 isl_union_map_from_map(isl_flow_get_no_source(flow, 0)));
1780 for (i = 0; i < flow->n_source; ++i) {
1781 isl_union_map *dep;
1782 dep = isl_union_map_from_map(isl_map_copy(flow->dep[i].map));
1783 if (flow->dep[i].must)
1784 df->must_dep = isl_union_map_union(df->must_dep, dep);
1785 else
1786 df->may_dep = isl_union_map_union(df->may_dep, dep);
1789 isl_flow_free(flow);
1791 sched_info_free(data->sink_info);
1792 if (data->source_info) {
1793 for (i = 0; i < data->count; ++i)
1794 sched_info_free(data->source_info[i]);
1795 free(data->source_info);
1797 isl_space_free(data->dim);
1798 isl_map_free(map);
1800 return 0;
1801 error:
1802 isl_access_info_free(data->accesses);
1803 sched_info_free(data->sink_info);
1804 if (data->source_info) {
1805 for (i = 0; i < data->count; ++i)
1806 sched_info_free(data->source_info[i]);
1807 free(data->source_info);
1809 isl_space_free(data->dim);
1810 isl_map_free(map);
1812 return -1;
1815 /* Remove the must accesses from the may accesses.
1817 * A must access always trumps a may access, so there is no need
1818 * for a must access to also be considered as a may access. Doing so
1819 * would only cost extra computations only to find out that
1820 * the duplicated may access does not make any difference.
1822 static __isl_give isl_union_access_info *isl_union_access_info_normalize(
1823 __isl_take isl_union_access_info *access)
1825 if (!access)
1826 return NULL;
1827 access->may_source = isl_union_map_subtract(access->may_source,
1828 isl_union_map_copy(access->must_source));
1829 if (!access->may_source)
1830 return isl_union_access_info_free(access);
1832 return access;
1835 /* Given a description of the "sink" accesses, the "source" accesses and
1836 * a schedule, compute for each instance of a sink access
1837 * and for each element accessed by that instance,
1838 * the possible or definite source accesses that last accessed the
1839 * element accessed by the sink access before this sink access
1840 * in the sense that there is no intermediate definite source access.
1842 * The must_no_source and may_no_source elements of the result
1843 * are subsets of access->sink. The elements must_dep and may_dep
1844 * map domain elements of access->{may,must)_source to
1845 * domain elements of access->sink.
1847 * This function is used when only the schedule map representation
1848 * is available.
1850 * We first prepend the schedule dimensions to the domain
1851 * of the accesses so that we can easily compare their relative order.
1852 * Then we consider each sink access individually in compute_flow.
1854 static __isl_give isl_union_flow *compute_flow_union_map(
1855 __isl_take isl_union_access_info *access)
1857 struct isl_compute_flow_data data;
1859 access = isl_union_access_info_align_params(access);
1860 access = isl_union_access_info_introduce_schedule(access);
1861 if (!access)
1862 return NULL;
1864 data.must_source = access->must_source;
1865 data.may_source = access->may_source;
1867 data.flow = isl_union_flow_alloc(isl_union_map_get_space(access->sink));
1869 if (isl_union_map_foreach_map(access->sink, &compute_flow, &data) < 0)
1870 goto error;
1872 data.flow = isl_union_flow_drop_schedule(data.flow);
1874 isl_union_access_info_free(access);
1875 return data.flow;
1876 error:
1877 isl_union_access_info_free(access);
1878 isl_union_flow_free(data.flow);
1879 return NULL;
1882 /* A schedule access relation.
1884 * The access relation "access" is of the form [S -> D] -> A,
1885 * where S corresponds to the prefix schedule at "node".
1886 * "must" is only relevant for source accesses and indicates
1887 * whether the access is a must source or a may source.
1889 struct isl_scheduled_access {
1890 isl_map *access;
1891 int must;
1892 isl_schedule_node *node;
1895 /* Data structure for keeping track of individual scheduled sink and source
1896 * accesses when computing dependence analysis based on a schedule tree.
1898 * "n_sink" is the number of used entries in "sink"
1899 * "n_source" is the number of used entries in "source"
1901 * "set_sink", "must" and "node" are only used inside collect_sink_source,
1902 * to keep track of the current node and
1903 * of what extract_sink_source needs to do.
1905 struct isl_compute_flow_schedule_data {
1906 isl_union_access_info *access;
1908 int n_sink;
1909 int n_source;
1911 struct isl_scheduled_access *sink;
1912 struct isl_scheduled_access *source;
1914 int set_sink;
1915 int must;
1916 isl_schedule_node *node;
1919 /* Align the parameters of all sinks with all sources.
1921 * If there are no sinks or no sources, then no alignment is needed.
1923 static void isl_compute_flow_schedule_data_align_params(
1924 struct isl_compute_flow_schedule_data *data)
1926 int i;
1927 isl_space *space;
1929 if (data->n_sink == 0 || data->n_source == 0)
1930 return;
1932 space = isl_map_get_space(data->sink[0].access);
1934 for (i = 1; i < data->n_sink; ++i)
1935 space = isl_space_align_params(space,
1936 isl_map_get_space(data->sink[i].access));
1937 for (i = 0; i < data->n_source; ++i)
1938 space = isl_space_align_params(space,
1939 isl_map_get_space(data->source[i].access));
1941 for (i = 0; i < data->n_sink; ++i)
1942 data->sink[i].access =
1943 isl_map_align_params(data->sink[i].access,
1944 isl_space_copy(space));
1945 for (i = 0; i < data->n_source; ++i)
1946 data->source[i].access =
1947 isl_map_align_params(data->source[i].access,
1948 isl_space_copy(space));
1950 isl_space_free(space);
1953 /* Free all the memory referenced from "data".
1954 * Do not free "data" itself as it may be allocated on the stack.
1956 static void isl_compute_flow_schedule_data_clear(
1957 struct isl_compute_flow_schedule_data *data)
1959 int i;
1961 for (i = 0; i < data->n_sink; ++i) {
1962 isl_map_free(data->sink[i].access);
1963 isl_schedule_node_free(data->sink[i].node);
1966 for (i = 0; i < data->n_source; ++i) {
1967 isl_map_free(data->source[i].access);
1968 isl_schedule_node_free(data->source[i].node);
1971 free(data->sink);
1974 /* isl_schedule_foreach_schedule_node callback for counting
1975 * (an upper bound on) the number of sinks and sources.
1977 * Sinks and sources are only extracted at leaves of the tree,
1978 * so we skip the node if it is not a leaf.
1979 * Otherwise we increment data->n_sink and data->n_source with
1980 * the number of spaces in the sink and source access domains
1981 * that reach this node.
1983 static int count_sink_source(__isl_keep isl_schedule_node *node, void *user)
1985 struct isl_compute_flow_schedule_data *data = user;
1986 isl_union_set *domain;
1987 isl_union_map *umap;
1988 int r = 0;
1990 if (isl_schedule_node_get_type(node) != isl_schedule_node_leaf)
1991 return 1;
1993 domain = isl_schedule_node_get_universe_domain(node);
1995 umap = isl_union_map_copy(data->access->sink);
1996 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
1997 data->n_sink += isl_union_map_n_map(umap);
1998 isl_union_map_free(umap);
1999 if (!umap)
2000 r = -1;
2002 umap = isl_union_map_copy(data->access->must_source);
2003 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
2004 data->n_source += isl_union_map_n_map(umap);
2005 isl_union_map_free(umap);
2006 if (!umap)
2007 r = -1;
2009 umap = isl_union_map_copy(data->access->may_source);
2010 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
2011 data->n_source += isl_union_map_n_map(umap);
2012 isl_union_map_free(umap);
2013 if (!umap)
2014 r = -1;
2016 isl_union_set_free(domain);
2018 return r;
2021 /* Add a single scheduled sink or source (depending on data->set_sink)
2022 * with scheduled access relation "map", must property data->must and
2023 * schedule node data->node to the list of sinks or sources.
2025 static int extract_sink_source(__isl_take isl_map *map, void *user)
2027 struct isl_compute_flow_schedule_data *data = user;
2028 struct isl_scheduled_access *access;
2030 if (data->set_sink)
2031 access = data->sink + data->n_sink++;
2032 else
2033 access = data->source + data->n_source++;
2035 access->access = map;
2036 access->must = data->must;
2037 access->node = isl_schedule_node_copy(data->node);
2039 return 0;
2042 /* isl_schedule_foreach_schedule_node callback for collecting
2043 * individual scheduled source and sink accesses.
2045 * We only collect accesses at the leaves of the schedule tree.
2046 * We prepend the schedule dimensions at the leaf to the iteration
2047 * domains of the source and sink accesses and then extract
2048 * the individual accesses (per space).
2050 * In particular, if the prefix schedule at the node is of the form
2052 * D -> S
2054 * while the access relations are of the form
2056 * D -> A
2058 * then the updated access relations are of the form
2060 * [S -> D] -> A
2062 * Note that S consists of a single space such that introducing S
2063 * in the access relations does not increase the number of spaces.
2065 static int collect_sink_source(__isl_keep isl_schedule_node *node, void *user)
2067 struct isl_compute_flow_schedule_data *data = user;
2068 isl_union_map *prefix;
2069 isl_union_map *umap;
2070 int r = 0;
2072 if (isl_schedule_node_get_type(node) != isl_schedule_node_leaf)
2073 return 1;
2075 data->node = node;
2077 prefix = isl_schedule_node_get_prefix_schedule_union_map(node);
2078 prefix = isl_union_map_reverse(prefix);
2079 prefix = isl_union_map_range_map(prefix);
2081 data->set_sink = 1;
2082 umap = isl_union_map_copy(data->access->sink);
2083 umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
2084 if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
2085 r = -1;
2086 isl_union_map_free(umap);
2088 data->set_sink = 0;
2089 data->must = 1;
2090 umap = isl_union_map_copy(data->access->must_source);
2091 umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
2092 if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
2093 r = -1;
2094 isl_union_map_free(umap);
2096 data->set_sink = 0;
2097 data->must = 0;
2098 umap = isl_union_map_copy(data->access->may_source);
2099 umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
2100 if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
2101 r = -1;
2102 isl_union_map_free(umap);
2104 isl_union_map_free(prefix);
2106 return r;
2109 /* isl_access_info_compute_flow callback for determining whether
2110 * the shared nesting level and the ordering within that level
2111 * for two scheduled accesses for use in compute_single_flow.
2113 * The tokens passed to this function refer to the leaves
2114 * in the schedule tree where the accesses take place.
2116 * If n is the shared number of loops, then we need to return
2117 * "2 * n + 1" if "first" precedes "second" inside the innermost
2118 * shared loop and "2 * n" otherwise.
2120 * The innermost shared ancestor may be the leaves themselves
2121 * if the accesses take place in the same leaf. Otherwise,
2122 * it is either a set node or a sequence node. Only in the case
2123 * of a sequence node do we consider one access to precede the other.
2125 static int before_node(void *first, void *second)
2127 isl_schedule_node *node1 = first;
2128 isl_schedule_node *node2 = second;
2129 isl_schedule_node *shared;
2130 int depth;
2131 int before = 0;
2133 shared = isl_schedule_node_get_shared_ancestor(node1, node2);
2134 if (!shared)
2135 return -1;
2137 depth = isl_schedule_node_get_schedule_depth(shared);
2138 if (isl_schedule_node_get_type(shared) == isl_schedule_node_sequence) {
2139 int pos1, pos2;
2141 pos1 = isl_schedule_node_get_ancestor_child_position(node1,
2142 shared);
2143 pos2 = isl_schedule_node_get_ancestor_child_position(node2,
2144 shared);
2145 before = pos1 < pos2;
2148 isl_schedule_node_free(shared);
2150 return 2 * depth + before;
2153 /* Add the scheduled sources from "data" that access
2154 * the same data space as "sink" to "access".
2156 static __isl_give isl_access_info *add_matching_sources(
2157 __isl_take isl_access_info *access, struct isl_scheduled_access *sink,
2158 struct isl_compute_flow_schedule_data *data)
2160 int i;
2161 isl_space *space;
2163 space = isl_space_range(isl_map_get_space(sink->access));
2164 for (i = 0; i < data->n_source; ++i) {
2165 struct isl_scheduled_access *source;
2166 isl_space *source_space;
2167 int eq;
2169 source = &data->source[i];
2170 source_space = isl_map_get_space(source->access);
2171 source_space = isl_space_range(source_space);
2172 eq = isl_space_is_equal(space, source_space);
2173 isl_space_free(source_space);
2175 if (!eq)
2176 continue;
2177 if (eq < 0)
2178 goto error;
2180 access = isl_access_info_add_source(access,
2181 isl_map_copy(source->access), source->must, source->node);
2184 isl_space_free(space);
2185 return access;
2186 error:
2187 isl_space_free(space);
2188 isl_access_info_free(access);
2189 return NULL;
2192 /* Given a scheduled sink access relation "sink", compute the corresponding
2193 * dependences on the sources in "data" and add the computed dependences
2194 * to "uf".
2196 static __isl_give isl_union_flow *compute_single_flow(
2197 __isl_take isl_union_flow *uf, struct isl_scheduled_access *sink,
2198 struct isl_compute_flow_schedule_data *data)
2200 int i;
2201 isl_access_info *access;
2202 isl_flow *flow;
2203 isl_map *map;
2205 if (!uf)
2206 return NULL;
2208 access = isl_access_info_alloc(isl_map_copy(sink->access), sink->node,
2209 &before_node, data->n_source);
2210 access = add_matching_sources(access, sink, data);
2212 flow = isl_access_info_compute_flow(access);
2213 if (!flow)
2214 return isl_union_flow_free(uf);
2216 map = isl_map_domain_factor_range(isl_flow_get_no_source(flow, 1));
2217 uf->must_no_source = isl_union_map_union(uf->must_no_source,
2218 isl_union_map_from_map(map));
2219 map = isl_map_domain_factor_range(isl_flow_get_no_source(flow, 0));
2220 uf->may_no_source = isl_union_map_union(uf->may_no_source,
2221 isl_union_map_from_map(map));
2223 for (i = 0; i < flow->n_source; ++i) {
2224 isl_union_map *dep;
2226 map = isl_map_factor_range(isl_map_copy(flow->dep[i].map));
2227 dep = isl_union_map_from_map(map);
2228 if (flow->dep[i].must)
2229 uf->must_dep = isl_union_map_union(uf->must_dep, dep);
2230 else
2231 uf->may_dep = isl_union_map_union(uf->may_dep, dep);
2234 isl_flow_free(flow);
2236 return uf;
2239 /* Given a description of the "sink" accesses, the "source" accesses and
2240 * a schedule, compute for each instance of a sink access
2241 * and for each element accessed by that instance,
2242 * the possible or definite source accesses that last accessed the
2243 * element accessed by the sink access before this sink access
2244 * in the sense that there is no intermediate definite source access.
2246 * The must_no_source and may_no_source elements of the result
2247 * are subsets of access->sink. The elements must_dep and may_dep
2248 * map domain elements of access->{may,must)_source to
2249 * domain elements of access->sink.
2251 * This function is used when a schedule tree representation
2252 * is available.
2254 * We extract the individual scheduled source and sink access relations and
2255 * then compute dependences for each scheduled sink individually.
2257 static __isl_give isl_union_flow *compute_flow_schedule(
2258 __isl_take isl_union_access_info *access)
2260 struct isl_compute_flow_schedule_data data = { access };
2261 int i, n;
2262 isl_ctx *ctx;
2263 isl_union_flow *flow;
2265 ctx = isl_union_access_info_get_ctx(access);
2267 data.n_sink = 0;
2268 data.n_source = 0;
2269 if (isl_schedule_foreach_schedule_node(access->schedule,
2270 &count_sink_source, &data) < 0)
2271 goto error;
2273 n = data.n_sink + data.n_source;
2274 data.sink = isl_calloc_array(ctx, struct isl_scheduled_access, n);
2275 if (n && !data.sink)
2276 goto error;
2277 data.source = data.sink + data.n_sink;
2279 data.n_sink = 0;
2280 data.n_source = 0;
2281 if (isl_schedule_foreach_schedule_node(access->schedule,
2282 &collect_sink_source, &data) < 0)
2283 goto error;
2285 flow = isl_union_flow_alloc(isl_union_map_get_space(access->sink));
2287 isl_compute_flow_schedule_data_align_params(&data);
2289 for (i = 0; i < data.n_sink; ++i)
2290 flow = compute_single_flow(flow, &data.sink[i], &data);
2292 isl_compute_flow_schedule_data_clear(&data);
2294 isl_union_access_info_free(access);
2295 return flow;
2296 error:
2297 isl_union_access_info_free(access);
2298 isl_compute_flow_schedule_data_clear(&data);
2299 return NULL;
2302 /* Given a description of the "sink" accesses, the "source" accesses and
2303 * a schedule, compute for each instance of a sink access
2304 * and for each element accessed by that instance,
2305 * the possible or definite source accesses that last accessed the
2306 * element accessed by the sink access before this sink access
2307 * in the sense that there is no intermediate definite source access.
2309 * The must_no_source and may_no_source elements of the result
2310 * are subsets of access->sink. The elements must_dep and may_dep
2311 * map domain elements of access->{may,must)_source to
2312 * domain elements of access->sink.
2314 * We check whether the schedule is available as a schedule tree
2315 * or a schedule map and call the correpsonding function to perform
2316 * the analysis.
2318 __isl_give isl_union_flow *isl_union_access_info_compute_flow(
2319 __isl_take isl_union_access_info *access)
2321 access = isl_union_access_info_normalize(access);
2322 if (!access)
2323 return NULL;
2324 if (access->schedule)
2325 return compute_flow_schedule(access);
2326 else
2327 return compute_flow_union_map(access);
2330 /* Given a collection of "sink" and "source" accesses,
2331 * compute for each iteration of a sink access
2332 * and for each element accessed by that iteration,
2333 * the source access in the list that last accessed the
2334 * element accessed by the sink access before this sink access.
2335 * Each access is given as a map from the loop iterators
2336 * to the array indices.
2337 * The result is a relations between source and sink
2338 * iterations and a subset of the domain of the sink accesses,
2339 * corresponding to those iterations that access an element
2340 * not previously accessed.
2342 * We collect the inputs in an isl_union_access_info object,
2343 * call isl_union_access_info_compute_flow and extract
2344 * the outputs from the result.
2346 int isl_union_map_compute_flow(__isl_take isl_union_map *sink,
2347 __isl_take isl_union_map *must_source,
2348 __isl_take isl_union_map *may_source,
2349 __isl_take isl_union_map *schedule,
2350 __isl_give isl_union_map **must_dep, __isl_give isl_union_map **may_dep,
2351 __isl_give isl_union_map **must_no_source,
2352 __isl_give isl_union_map **may_no_source)
2354 isl_union_access_info *access;
2355 isl_union_flow *flow;
2357 access = isl_union_access_info_from_sink(sink);
2358 access = isl_union_access_info_set_must_source(access, must_source);
2359 access = isl_union_access_info_set_may_source(access, may_source);
2360 access = isl_union_access_info_set_schedule_map(access, schedule);
2361 flow = isl_union_access_info_compute_flow(access);
2363 if (must_dep)
2364 *must_dep = isl_union_flow_get_must_dependence(flow);
2365 if (may_dep)
2366 *may_dep = isl_union_flow_get_non_must_dependence(flow);
2367 if (must_no_source)
2368 *must_no_source = isl_union_flow_get_must_no_source(flow);
2369 if (may_no_source)
2370 *may_no_source = isl_union_flow_get_non_must_no_source(flow);
2372 isl_union_flow_free(flow);
2374 if ((must_dep && !*must_dep) || (may_dep && !*may_dep) ||
2375 (must_no_source && !*must_no_source) ||
2376 (may_no_source && !*may_no_source))
2377 goto error;
2379 return 0;
2380 error:
2381 if (must_dep)
2382 *must_dep = isl_union_map_free(*must_dep);
2383 if (may_dep)
2384 *may_dep = isl_union_map_free(*may_dep);
2385 if (must_no_source)
2386 *must_no_source = isl_union_map_free(*must_no_source);
2387 if (may_no_source)
2388 *may_no_source = isl_union_map_free(*may_no_source);
2389 return -1;