isl_union_map_compute_flow: reuse isl_union_access_info_compute_flow
[isl.git] / isl_flow.c
bloba865536296f58880b92c67e914c946b729b1d573
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_sort.h>
24 enum isl_restriction_type {
25 isl_restriction_type_empty,
26 isl_restriction_type_none,
27 isl_restriction_type_input,
28 isl_restriction_type_output
31 struct isl_restriction {
32 enum isl_restriction_type type;
34 isl_set *source;
35 isl_set *sink;
38 /* Create a restriction of the given type.
40 static __isl_give isl_restriction *isl_restriction_alloc(
41 __isl_take isl_map *source_map, enum isl_restriction_type type)
43 isl_ctx *ctx;
44 isl_restriction *restr;
46 if (!source_map)
47 return NULL;
49 ctx = isl_map_get_ctx(source_map);
50 restr = isl_calloc_type(ctx, struct isl_restriction);
51 if (!restr)
52 goto error;
54 restr->type = type;
56 isl_map_free(source_map);
57 return restr;
58 error:
59 isl_map_free(source_map);
60 return NULL;
63 /* Create a restriction that doesn't restrict anything.
65 __isl_give isl_restriction *isl_restriction_none(__isl_take isl_map *source_map)
67 return isl_restriction_alloc(source_map, isl_restriction_type_none);
70 /* Create a restriction that removes everything.
72 __isl_give isl_restriction *isl_restriction_empty(
73 __isl_take isl_map *source_map)
75 return isl_restriction_alloc(source_map, isl_restriction_type_empty);
78 /* Create a restriction on the input of the maximization problem
79 * based on the given source and sink restrictions.
81 __isl_give isl_restriction *isl_restriction_input(
82 __isl_take isl_set *source_restr, __isl_take isl_set *sink_restr)
84 isl_ctx *ctx;
85 isl_restriction *restr;
87 if (!source_restr || !sink_restr)
88 goto error;
90 ctx = isl_set_get_ctx(source_restr);
91 restr = isl_calloc_type(ctx, struct isl_restriction);
92 if (!restr)
93 goto error;
95 restr->type = isl_restriction_type_input;
96 restr->source = source_restr;
97 restr->sink = sink_restr;
99 return restr;
100 error:
101 isl_set_free(source_restr);
102 isl_set_free(sink_restr);
103 return NULL;
106 /* Create a restriction on the output of the maximization problem
107 * based on the given source restriction.
109 __isl_give isl_restriction *isl_restriction_output(
110 __isl_take isl_set *source_restr)
112 isl_ctx *ctx;
113 isl_restriction *restr;
115 if (!source_restr)
116 return NULL;
118 ctx = isl_set_get_ctx(source_restr);
119 restr = isl_calloc_type(ctx, struct isl_restriction);
120 if (!restr)
121 goto error;
123 restr->type = isl_restriction_type_output;
124 restr->source = source_restr;
126 return restr;
127 error:
128 isl_set_free(source_restr);
129 return NULL;
132 __isl_null isl_restriction *isl_restriction_free(
133 __isl_take isl_restriction *restr)
135 if (!restr)
136 return NULL;
138 isl_set_free(restr->source);
139 isl_set_free(restr->sink);
140 free(restr);
141 return NULL;
144 isl_ctx *isl_restriction_get_ctx(__isl_keep isl_restriction *restr)
146 return restr ? isl_set_get_ctx(restr->source) : NULL;
149 /* A private structure to keep track of a mapping together with
150 * a user-specified identifier and a boolean indicating whether
151 * the map represents a must or may access/dependence.
153 struct isl_labeled_map {
154 struct isl_map *map;
155 void *data;
156 int must;
159 /* A structure containing the input for dependence analysis:
160 * - a sink
161 * - n_must + n_may (<= max_source) sources
162 * - a function for determining the relative order of sources and sink
163 * The must sources are placed before the may sources.
165 * domain_map is an auxiliary map that maps the sink access relation
166 * to the domain of this access relation.
168 * restrict_fn is a callback that (if not NULL) will be called
169 * right before any lexicographical maximization.
171 struct isl_access_info {
172 isl_map *domain_map;
173 struct isl_labeled_map sink;
174 isl_access_level_before level_before;
176 isl_access_restrict restrict_fn;
177 void *restrict_user;
179 int max_source;
180 int n_must;
181 int n_may;
182 struct isl_labeled_map source[1];
185 /* A structure containing the output of dependence analysis:
186 * - n_source dependences
187 * - a wrapped subset of the sink for which definitely no source could be found
188 * - a wrapped subset of the sink for which possibly no source could be found
190 struct isl_flow {
191 isl_set *must_no_source;
192 isl_set *may_no_source;
193 int n_source;
194 struct isl_labeled_map *dep;
197 /* Construct an isl_access_info structure and fill it up with
198 * the given data. The number of sources is set to 0.
200 __isl_give isl_access_info *isl_access_info_alloc(__isl_take isl_map *sink,
201 void *sink_user, isl_access_level_before fn, int max_source)
203 isl_ctx *ctx;
204 struct isl_access_info *acc;
206 if (!sink)
207 return NULL;
209 ctx = isl_map_get_ctx(sink);
210 isl_assert(ctx, max_source >= 0, goto error);
212 acc = isl_calloc(ctx, struct isl_access_info,
213 sizeof(struct isl_access_info) +
214 (max_source - 1) * sizeof(struct isl_labeled_map));
215 if (!acc)
216 goto error;
218 acc->sink.map = sink;
219 acc->sink.data = sink_user;
220 acc->level_before = fn;
221 acc->max_source = max_source;
222 acc->n_must = 0;
223 acc->n_may = 0;
225 return acc;
226 error:
227 isl_map_free(sink);
228 return NULL;
231 /* Free the given isl_access_info structure.
233 __isl_null isl_access_info *isl_access_info_free(
234 __isl_take isl_access_info *acc)
236 int i;
238 if (!acc)
239 return NULL;
240 isl_map_free(acc->domain_map);
241 isl_map_free(acc->sink.map);
242 for (i = 0; i < acc->n_must + acc->n_may; ++i)
243 isl_map_free(acc->source[i].map);
244 free(acc);
245 return NULL;
248 isl_ctx *isl_access_info_get_ctx(__isl_keep isl_access_info *acc)
250 return acc ? isl_map_get_ctx(acc->sink.map) : NULL;
253 __isl_give isl_access_info *isl_access_info_set_restrict(
254 __isl_take isl_access_info *acc, isl_access_restrict fn, void *user)
256 if (!acc)
257 return NULL;
258 acc->restrict_fn = fn;
259 acc->restrict_user = user;
260 return acc;
263 /* Add another source to an isl_access_info structure, making
264 * sure the "must" sources are placed before the "may" sources.
265 * This function may be called at most max_source times on a
266 * given isl_access_info structure, with max_source as specified
267 * in the call to isl_access_info_alloc that constructed the structure.
269 __isl_give isl_access_info *isl_access_info_add_source(
270 __isl_take isl_access_info *acc, __isl_take isl_map *source,
271 int must, void *source_user)
273 isl_ctx *ctx;
275 if (!acc)
276 goto error;
277 ctx = isl_map_get_ctx(acc->sink.map);
278 isl_assert(ctx, acc->n_must + acc->n_may < acc->max_source, goto error);
280 if (must) {
281 if (acc->n_may)
282 acc->source[acc->n_must + acc->n_may] =
283 acc->source[acc->n_must];
284 acc->source[acc->n_must].map = source;
285 acc->source[acc->n_must].data = source_user;
286 acc->source[acc->n_must].must = 1;
287 acc->n_must++;
288 } else {
289 acc->source[acc->n_must + acc->n_may].map = source;
290 acc->source[acc->n_must + acc->n_may].data = source_user;
291 acc->source[acc->n_must + acc->n_may].must = 0;
292 acc->n_may++;
295 return acc;
296 error:
297 isl_map_free(source);
298 isl_access_info_free(acc);
299 return NULL;
302 /* Return -n, 0 or n (with n a positive value), depending on whether
303 * the source access identified by p1 should be sorted before, together
304 * or after that identified by p2.
306 * If p1 appears before p2, then it should be sorted first.
307 * For more generic initial schedules, it is possible that neither
308 * p1 nor p2 appears before the other, or at least not in any obvious way.
309 * We therefore also check if p2 appears before p1, in which case p2
310 * should be sorted first.
311 * If not, we try to order the two statements based on the description
312 * of the iteration domains. This results in an arbitrary, but fairly
313 * stable ordering.
315 static int access_sort_cmp(const void *p1, const void *p2, void *user)
317 isl_access_info *acc = user;
318 const struct isl_labeled_map *i1, *i2;
319 int level1, level2;
320 uint32_t h1, h2;
321 i1 = (const struct isl_labeled_map *) p1;
322 i2 = (const struct isl_labeled_map *) p2;
324 level1 = acc->level_before(i1->data, i2->data);
325 if (level1 % 2)
326 return -1;
328 level2 = acc->level_before(i2->data, i1->data);
329 if (level2 % 2)
330 return 1;
332 h1 = isl_map_get_hash(i1->map);
333 h2 = isl_map_get_hash(i2->map);
334 return h1 > h2 ? 1 : h1 < h2 ? -1 : 0;
337 /* Sort the must source accesses in their textual order.
339 static __isl_give isl_access_info *isl_access_info_sort_sources(
340 __isl_take isl_access_info *acc)
342 if (!acc)
343 return NULL;
344 if (acc->n_must <= 1)
345 return acc;
347 if (isl_sort(acc->source, acc->n_must, sizeof(struct isl_labeled_map),
348 access_sort_cmp, acc) < 0)
349 return isl_access_info_free(acc);
351 return acc;
354 /* Align the parameters of the two spaces if needed and then call
355 * isl_space_join.
357 static __isl_give isl_space *space_align_and_join(__isl_take isl_space *left,
358 __isl_take isl_space *right)
360 if (isl_space_match(left, isl_dim_param, right, isl_dim_param))
361 return isl_space_join(left, right);
363 left = isl_space_align_params(left, isl_space_copy(right));
364 right = isl_space_align_params(right, isl_space_copy(left));
365 return isl_space_join(left, right);
368 /* Initialize an empty isl_flow structure corresponding to a given
369 * isl_access_info structure.
370 * For each must access, two dependences are created (initialized
371 * to the empty relation), one for the resulting must dependences
372 * and one for the resulting may dependences. May accesses can
373 * only lead to may dependences, so only one dependence is created
374 * for each of them.
375 * This function is private as isl_flow structures are only supposed
376 * to be created by isl_access_info_compute_flow.
378 static __isl_give isl_flow *isl_flow_alloc(__isl_keep isl_access_info *acc)
380 int i, n;
381 struct isl_ctx *ctx;
382 struct isl_flow *dep;
384 if (!acc)
385 return NULL;
387 ctx = isl_map_get_ctx(acc->sink.map);
388 dep = isl_calloc_type(ctx, struct isl_flow);
389 if (!dep)
390 return NULL;
392 n = 2 * acc->n_must + acc->n_may;
393 dep->dep = isl_calloc_array(ctx, struct isl_labeled_map, n);
394 if (n && !dep->dep)
395 goto error;
397 dep->n_source = n;
398 for (i = 0; i < acc->n_must; ++i) {
399 isl_space *dim;
400 dim = space_align_and_join(
401 isl_map_get_space(acc->source[i].map),
402 isl_space_reverse(isl_map_get_space(acc->sink.map)));
403 dep->dep[2 * i].map = isl_map_empty(dim);
404 dep->dep[2 * i + 1].map = isl_map_copy(dep->dep[2 * i].map);
405 dep->dep[2 * i].data = acc->source[i].data;
406 dep->dep[2 * i + 1].data = acc->source[i].data;
407 dep->dep[2 * i].must = 1;
408 dep->dep[2 * i + 1].must = 0;
409 if (!dep->dep[2 * i].map || !dep->dep[2 * i + 1].map)
410 goto error;
412 for (i = acc->n_must; i < acc->n_must + acc->n_may; ++i) {
413 isl_space *dim;
414 dim = space_align_and_join(
415 isl_map_get_space(acc->source[i].map),
416 isl_space_reverse(isl_map_get_space(acc->sink.map)));
417 dep->dep[acc->n_must + i].map = isl_map_empty(dim);
418 dep->dep[acc->n_must + i].data = acc->source[i].data;
419 dep->dep[acc->n_must + i].must = 0;
420 if (!dep->dep[acc->n_must + i].map)
421 goto error;
424 return dep;
425 error:
426 isl_flow_free(dep);
427 return NULL;
430 /* Iterate over all sources and for each resulting flow dependence
431 * that is not empty, call the user specfied function.
432 * The second argument in this function call identifies the source,
433 * while the third argument correspond to the final argument of
434 * the isl_flow_foreach call.
436 int isl_flow_foreach(__isl_keep isl_flow *deps,
437 int (*fn)(__isl_take isl_map *dep, int must, void *dep_user, void *user),
438 void *user)
440 int i;
442 if (!deps)
443 return -1;
445 for (i = 0; i < deps->n_source; ++i) {
446 if (isl_map_plain_is_empty(deps->dep[i].map))
447 continue;
448 if (fn(isl_map_copy(deps->dep[i].map), deps->dep[i].must,
449 deps->dep[i].data, user) < 0)
450 return -1;
453 return 0;
456 /* Return a copy of the subset of the sink for which no source could be found.
458 __isl_give isl_map *isl_flow_get_no_source(__isl_keep isl_flow *deps, int must)
460 if (!deps)
461 return NULL;
463 if (must)
464 return isl_set_unwrap(isl_set_copy(deps->must_no_source));
465 else
466 return isl_set_unwrap(isl_set_copy(deps->may_no_source));
469 void isl_flow_free(__isl_take isl_flow *deps)
471 int i;
473 if (!deps)
474 return;
475 isl_set_free(deps->must_no_source);
476 isl_set_free(deps->may_no_source);
477 if (deps->dep) {
478 for (i = 0; i < deps->n_source; ++i)
479 isl_map_free(deps->dep[i].map);
480 free(deps->dep);
482 free(deps);
485 isl_ctx *isl_flow_get_ctx(__isl_keep isl_flow *deps)
487 return deps ? isl_set_get_ctx(deps->must_no_source) : NULL;
490 /* Return a map that enforces that the domain iteration occurs after
491 * the range iteration at the given level.
492 * If level is odd, then the domain iteration should occur after
493 * the target iteration in their shared level/2 outermost loops.
494 * In this case we simply need to enforce that these outermost
495 * loop iterations are the same.
496 * If level is even, then the loop iterator of the domain should
497 * be greater than the loop iterator of the range at the last
498 * of the level/2 shared loops, i.e., loop level/2 - 1.
500 static __isl_give isl_map *after_at_level(__isl_take isl_space *dim, int level)
502 struct isl_basic_map *bmap;
504 if (level % 2)
505 bmap = isl_basic_map_equal(dim, level/2);
506 else
507 bmap = isl_basic_map_more_at(dim, level/2 - 1);
509 return isl_map_from_basic_map(bmap);
512 /* Compute the partial lexicographic maximum of "dep" on domain "sink",
513 * but first check if the user has set acc->restrict_fn and if so
514 * update either the input or the output of the maximization problem
515 * with respect to the resulting restriction.
517 * Since the user expects a mapping from sink iterations to source iterations,
518 * whereas the domain of "dep" is a wrapped map, mapping sink iterations
519 * to accessed array elements, we first need to project out the accessed
520 * sink array elements by applying acc->domain_map.
521 * Similarly, the sink restriction specified by the user needs to be
522 * converted back to the wrapped map.
524 static __isl_give isl_map *restricted_partial_lexmax(
525 __isl_keep isl_access_info *acc, __isl_take isl_map *dep,
526 int source, __isl_take isl_set *sink, __isl_give isl_set **empty)
528 isl_map *source_map;
529 isl_restriction *restr;
530 isl_set *sink_domain;
531 isl_set *sink_restr;
532 isl_map *res;
534 if (!acc->restrict_fn)
535 return isl_map_partial_lexmax(dep, sink, empty);
537 source_map = isl_map_copy(dep);
538 source_map = isl_map_apply_domain(source_map,
539 isl_map_copy(acc->domain_map));
540 sink_domain = isl_set_copy(sink);
541 sink_domain = isl_set_apply(sink_domain, isl_map_copy(acc->domain_map));
542 restr = acc->restrict_fn(source_map, sink_domain,
543 acc->source[source].data, acc->restrict_user);
544 isl_set_free(sink_domain);
545 isl_map_free(source_map);
547 if (!restr)
548 goto error;
549 if (restr->type == isl_restriction_type_input) {
550 dep = isl_map_intersect_range(dep, isl_set_copy(restr->source));
551 sink_restr = isl_set_copy(restr->sink);
552 sink_restr = isl_set_apply(sink_restr,
553 isl_map_reverse(isl_map_copy(acc->domain_map)));
554 sink = isl_set_intersect(sink, sink_restr);
555 } else if (restr->type == isl_restriction_type_empty) {
556 isl_space *space = isl_map_get_space(dep);
557 isl_map_free(dep);
558 dep = isl_map_empty(space);
561 res = isl_map_partial_lexmax(dep, sink, empty);
563 if (restr->type == isl_restriction_type_output)
564 res = isl_map_intersect_range(res, isl_set_copy(restr->source));
566 isl_restriction_free(restr);
567 return res;
568 error:
569 isl_map_free(dep);
570 isl_set_free(sink);
571 *empty = NULL;
572 return NULL;
575 /* Compute the last iteration of must source j that precedes the sink
576 * at the given level for sink iterations in set_C.
577 * The subset of set_C for which no such iteration can be found is returned
578 * in *empty.
580 static struct isl_map *last_source(struct isl_access_info *acc,
581 struct isl_set *set_C,
582 int j, int level, struct isl_set **empty)
584 struct isl_map *read_map;
585 struct isl_map *write_map;
586 struct isl_map *dep_map;
587 struct isl_map *after;
588 struct isl_map *result;
590 read_map = isl_map_copy(acc->sink.map);
591 write_map = isl_map_copy(acc->source[j].map);
592 write_map = isl_map_reverse(write_map);
593 dep_map = isl_map_apply_range(read_map, write_map);
594 after = after_at_level(isl_map_get_space(dep_map), level);
595 dep_map = isl_map_intersect(dep_map, after);
596 result = restricted_partial_lexmax(acc, dep_map, j, set_C, empty);
597 result = isl_map_reverse(result);
599 return result;
602 /* For a given mapping between iterations of must source j and iterations
603 * of the sink, compute the last iteration of must source k preceding
604 * the sink at level before_level for any of the sink iterations,
605 * but following the corresponding iteration of must source j at level
606 * after_level.
608 static struct isl_map *last_later_source(struct isl_access_info *acc,
609 struct isl_map *old_map,
610 int j, int before_level,
611 int k, int after_level,
612 struct isl_set **empty)
614 isl_space *dim;
615 struct isl_set *set_C;
616 struct isl_map *read_map;
617 struct isl_map *write_map;
618 struct isl_map *dep_map;
619 struct isl_map *after_write;
620 struct isl_map *before_read;
621 struct isl_map *result;
623 set_C = isl_map_range(isl_map_copy(old_map));
624 read_map = isl_map_copy(acc->sink.map);
625 write_map = isl_map_copy(acc->source[k].map);
627 write_map = isl_map_reverse(write_map);
628 dep_map = isl_map_apply_range(read_map, write_map);
629 dim = space_align_and_join(isl_map_get_space(acc->source[k].map),
630 isl_space_reverse(isl_map_get_space(acc->source[j].map)));
631 after_write = after_at_level(dim, after_level);
632 after_write = isl_map_apply_range(after_write, old_map);
633 after_write = isl_map_reverse(after_write);
634 dep_map = isl_map_intersect(dep_map, after_write);
635 before_read = after_at_level(isl_map_get_space(dep_map), before_level);
636 dep_map = isl_map_intersect(dep_map, before_read);
637 result = restricted_partial_lexmax(acc, dep_map, k, set_C, empty);
638 result = isl_map_reverse(result);
640 return result;
643 /* Given a shared_level between two accesses, return 1 if the
644 * the first can precede the second at the requested target_level.
645 * If the target level is odd, i.e., refers to a statement level
646 * dimension, then first needs to precede second at the requested
647 * level, i.e., shared_level must be equal to target_level.
648 * If the target level is odd, then the two loops should share
649 * at least the requested number of outer loops.
651 static int can_precede_at_level(int shared_level, int target_level)
653 if (shared_level < target_level)
654 return 0;
655 if ((target_level % 2) && shared_level > target_level)
656 return 0;
657 return 1;
660 /* Given a possible flow dependence temp_rel[j] between source j and the sink
661 * at level sink_level, remove those elements for which
662 * there is an iteration of another source k < j that is closer to the sink.
663 * The flow dependences temp_rel[k] are updated with the improved sources.
664 * Any improved source needs to precede the sink at the same level
665 * and needs to follow source j at the same or a deeper level.
666 * The lower this level, the later the execution date of source k.
667 * We therefore consider lower levels first.
669 * If temp_rel[j] is empty, then there can be no improvement and
670 * we return immediately.
672 static int intermediate_sources(__isl_keep isl_access_info *acc,
673 struct isl_map **temp_rel, int j, int sink_level)
675 int k, level;
676 int depth = 2 * isl_map_dim(acc->source[j].map, isl_dim_in) + 1;
678 if (isl_map_plain_is_empty(temp_rel[j]))
679 return 0;
681 for (k = j - 1; k >= 0; --k) {
682 int plevel, plevel2;
683 plevel = acc->level_before(acc->source[k].data, acc->sink.data);
684 if (!can_precede_at_level(plevel, sink_level))
685 continue;
687 plevel2 = acc->level_before(acc->source[j].data,
688 acc->source[k].data);
690 for (level = sink_level; level <= depth; ++level) {
691 struct isl_map *T;
692 struct isl_set *trest;
693 struct isl_map *copy;
695 if (!can_precede_at_level(plevel2, level))
696 continue;
698 copy = isl_map_copy(temp_rel[j]);
699 T = last_later_source(acc, copy, j, sink_level, k,
700 level, &trest);
701 if (isl_map_plain_is_empty(T)) {
702 isl_set_free(trest);
703 isl_map_free(T);
704 continue;
706 temp_rel[j] = isl_map_intersect_range(temp_rel[j], trest);
707 temp_rel[k] = isl_map_union_disjoint(temp_rel[k], T);
711 return 0;
714 /* Compute all iterations of may source j that precedes the sink at the given
715 * level for sink iterations in set_C.
717 static __isl_give isl_map *all_sources(__isl_keep isl_access_info *acc,
718 __isl_take isl_set *set_C, int j, int level)
720 isl_map *read_map;
721 isl_map *write_map;
722 isl_map *dep_map;
723 isl_map *after;
725 read_map = isl_map_copy(acc->sink.map);
726 read_map = isl_map_intersect_domain(read_map, set_C);
727 write_map = isl_map_copy(acc->source[acc->n_must + j].map);
728 write_map = isl_map_reverse(write_map);
729 dep_map = isl_map_apply_range(read_map, write_map);
730 after = after_at_level(isl_map_get_space(dep_map), level);
731 dep_map = isl_map_intersect(dep_map, after);
733 return isl_map_reverse(dep_map);
736 /* For a given mapping between iterations of must source k and iterations
737 * of the sink, compute the all iteration of may source j preceding
738 * the sink at level before_level for any of the sink iterations,
739 * but following the corresponding iteration of must source k at level
740 * after_level.
742 static __isl_give isl_map *all_later_sources(__isl_keep isl_access_info *acc,
743 __isl_take isl_map *old_map,
744 int j, int before_level, int k, int after_level)
746 isl_space *dim;
747 isl_set *set_C;
748 isl_map *read_map;
749 isl_map *write_map;
750 isl_map *dep_map;
751 isl_map *after_write;
752 isl_map *before_read;
754 set_C = isl_map_range(isl_map_copy(old_map));
755 read_map = isl_map_copy(acc->sink.map);
756 read_map = isl_map_intersect_domain(read_map, set_C);
757 write_map = isl_map_copy(acc->source[acc->n_must + j].map);
759 write_map = isl_map_reverse(write_map);
760 dep_map = isl_map_apply_range(read_map, write_map);
761 dim = isl_space_join(isl_map_get_space(acc->source[acc->n_must + j].map),
762 isl_space_reverse(isl_map_get_space(acc->source[k].map)));
763 after_write = after_at_level(dim, after_level);
764 after_write = isl_map_apply_range(after_write, old_map);
765 after_write = isl_map_reverse(after_write);
766 dep_map = isl_map_intersect(dep_map, after_write);
767 before_read = after_at_level(isl_map_get_space(dep_map), before_level);
768 dep_map = isl_map_intersect(dep_map, before_read);
769 return isl_map_reverse(dep_map);
772 /* Given the must and may dependence relations for the must accesses
773 * for level sink_level, check if there are any accesses of may access j
774 * that occur in between and return their union.
775 * If some of these accesses are intermediate with respect to
776 * (previously thought to be) must dependences, then these
777 * must dependences are turned into may dependences.
779 static __isl_give isl_map *all_intermediate_sources(
780 __isl_keep isl_access_info *acc, __isl_take isl_map *map,
781 struct isl_map **must_rel, struct isl_map **may_rel,
782 int j, int sink_level)
784 int k, level;
785 int depth = 2 * isl_map_dim(acc->source[acc->n_must + j].map,
786 isl_dim_in) + 1;
788 for (k = 0; k < acc->n_must; ++k) {
789 int plevel;
791 if (isl_map_plain_is_empty(may_rel[k]) &&
792 isl_map_plain_is_empty(must_rel[k]))
793 continue;
795 plevel = acc->level_before(acc->source[k].data,
796 acc->source[acc->n_must + j].data);
798 for (level = sink_level; level <= depth; ++level) {
799 isl_map *T;
800 isl_map *copy;
801 isl_set *ran;
803 if (!can_precede_at_level(plevel, level))
804 continue;
806 copy = isl_map_copy(may_rel[k]);
807 T = all_later_sources(acc, copy, j, sink_level, k, level);
808 map = isl_map_union(map, T);
810 copy = isl_map_copy(must_rel[k]);
811 T = all_later_sources(acc, copy, j, sink_level, k, level);
812 ran = isl_map_range(isl_map_copy(T));
813 map = isl_map_union(map, T);
814 may_rel[k] = isl_map_union_disjoint(may_rel[k],
815 isl_map_intersect_range(isl_map_copy(must_rel[k]),
816 isl_set_copy(ran)));
817 T = isl_map_from_domain_and_range(
818 isl_set_universe(
819 isl_space_domain(isl_map_get_space(must_rel[k]))),
820 ran);
821 must_rel[k] = isl_map_subtract(must_rel[k], T);
825 return map;
828 /* Compute dependences for the case where all accesses are "may"
829 * accesses, which boils down to computing memory based dependences.
830 * The generic algorithm would also work in this case, but it would
831 * be overkill to use it.
833 static __isl_give isl_flow *compute_mem_based_dependences(
834 __isl_keep isl_access_info *acc)
836 int i;
837 isl_set *mustdo;
838 isl_set *maydo;
839 isl_flow *res;
841 res = isl_flow_alloc(acc);
842 if (!res)
843 return NULL;
845 mustdo = isl_map_domain(isl_map_copy(acc->sink.map));
846 maydo = isl_set_copy(mustdo);
848 for (i = 0; i < acc->n_may; ++i) {
849 int plevel;
850 int is_before;
851 isl_space *dim;
852 isl_map *before;
853 isl_map *dep;
855 plevel = acc->level_before(acc->source[i].data, acc->sink.data);
856 is_before = plevel & 1;
857 plevel >>= 1;
859 dim = isl_map_get_space(res->dep[i].map);
860 if (is_before)
861 before = isl_map_lex_le_first(dim, plevel);
862 else
863 before = isl_map_lex_lt_first(dim, plevel);
864 dep = isl_map_apply_range(isl_map_copy(acc->source[i].map),
865 isl_map_reverse(isl_map_copy(acc->sink.map)));
866 dep = isl_map_intersect(dep, before);
867 mustdo = isl_set_subtract(mustdo,
868 isl_map_range(isl_map_copy(dep)));
869 res->dep[i].map = isl_map_union(res->dep[i].map, dep);
872 res->may_no_source = isl_set_subtract(maydo, isl_set_copy(mustdo));
873 res->must_no_source = mustdo;
875 return res;
878 /* Compute dependences for the case where there is at least one
879 * "must" access.
881 * The core algorithm considers all levels in which a source may precede
882 * the sink, where a level may either be a statement level or a loop level.
883 * The outermost statement level is 1, the first loop level is 2, etc...
884 * The algorithm basically does the following:
885 * for all levels l of the read access from innermost to outermost
886 * for all sources w that may precede the sink access at that level
887 * compute the last iteration of the source that precedes the sink access
888 * at that level
889 * add result to possible last accesses at level l of source w
890 * for all sources w2 that we haven't considered yet at this level that may
891 * also precede the sink access
892 * for all levels l2 of w from l to innermost
893 * for all possible last accesses dep of w at l
894 * compute last iteration of w2 between the source and sink
895 * of dep
896 * add result to possible last accesses at level l of write w2
897 * and replace possible last accesses dep by the remainder
900 * The above algorithm is applied to the must access. During the course
901 * of the algorithm, we keep track of sink iterations that still
902 * need to be considered. These iterations are split into those that
903 * haven't been matched to any source access (mustdo) and those that have only
904 * been matched to may accesses (maydo).
905 * At the end of each level, we also consider the may accesses.
906 * In particular, we consider may accesses that precede the remaining
907 * sink iterations, moving elements from mustdo to maydo when appropriate,
908 * and may accesses that occur between a must source and a sink of any
909 * dependences found at the current level, turning must dependences into
910 * may dependences when appropriate.
913 static __isl_give isl_flow *compute_val_based_dependences(
914 __isl_keep isl_access_info *acc)
916 isl_ctx *ctx;
917 isl_flow *res;
918 isl_set *mustdo = NULL;
919 isl_set *maydo = NULL;
920 int level, j;
921 int depth;
922 isl_map **must_rel = NULL;
923 isl_map **may_rel = NULL;
925 if (!acc)
926 return NULL;
928 res = isl_flow_alloc(acc);
929 if (!res)
930 goto error;
931 ctx = isl_map_get_ctx(acc->sink.map);
933 depth = 2 * isl_map_dim(acc->sink.map, isl_dim_in) + 1;
934 mustdo = isl_map_domain(isl_map_copy(acc->sink.map));
935 maydo = isl_set_empty_like(mustdo);
936 if (!mustdo || !maydo)
937 goto error;
938 if (isl_set_plain_is_empty(mustdo))
939 goto done;
941 must_rel = isl_alloc_array(ctx, struct isl_map *, acc->n_must);
942 may_rel = isl_alloc_array(ctx, struct isl_map *, acc->n_must);
943 if (!must_rel || !may_rel)
944 goto error;
946 for (level = depth; level >= 1; --level) {
947 for (j = acc->n_must-1; j >=0; --j) {
948 must_rel[j] = isl_map_empty_like(res->dep[2 * j].map);
949 may_rel[j] = isl_map_copy(must_rel[j]);
952 for (j = acc->n_must - 1; j >= 0; --j) {
953 struct isl_map *T;
954 struct isl_set *rest;
955 int plevel;
957 plevel = acc->level_before(acc->source[j].data,
958 acc->sink.data);
959 if (!can_precede_at_level(plevel, level))
960 continue;
962 T = last_source(acc, mustdo, j, level, &rest);
963 must_rel[j] = isl_map_union_disjoint(must_rel[j], T);
964 mustdo = rest;
966 intermediate_sources(acc, must_rel, j, level);
968 T = last_source(acc, maydo, j, level, &rest);
969 may_rel[j] = isl_map_union_disjoint(may_rel[j], T);
970 maydo = rest;
972 intermediate_sources(acc, may_rel, j, level);
974 if (isl_set_plain_is_empty(mustdo) &&
975 isl_set_plain_is_empty(maydo))
976 break;
978 for (j = j - 1; j >= 0; --j) {
979 int plevel;
981 plevel = acc->level_before(acc->source[j].data,
982 acc->sink.data);
983 if (!can_precede_at_level(plevel, level))
984 continue;
986 intermediate_sources(acc, must_rel, j, level);
987 intermediate_sources(acc, may_rel, j, level);
990 for (j = 0; j < acc->n_may; ++j) {
991 int plevel;
992 isl_map *T;
993 isl_set *ran;
995 plevel = acc->level_before(acc->source[acc->n_must + j].data,
996 acc->sink.data);
997 if (!can_precede_at_level(plevel, level))
998 continue;
1000 T = all_sources(acc, isl_set_copy(maydo), j, level);
1001 res->dep[2 * acc->n_must + j].map =
1002 isl_map_union(res->dep[2 * acc->n_must + j].map, T);
1003 T = all_sources(acc, isl_set_copy(mustdo), j, level);
1004 ran = isl_map_range(isl_map_copy(T));
1005 res->dep[2 * acc->n_must + j].map =
1006 isl_map_union(res->dep[2 * acc->n_must + j].map, T);
1007 mustdo = isl_set_subtract(mustdo, isl_set_copy(ran));
1008 maydo = isl_set_union_disjoint(maydo, ran);
1010 T = res->dep[2 * acc->n_must + j].map;
1011 T = all_intermediate_sources(acc, T, must_rel, may_rel,
1012 j, level);
1013 res->dep[2 * acc->n_must + j].map = T;
1016 for (j = acc->n_must - 1; j >= 0; --j) {
1017 res->dep[2 * j].map =
1018 isl_map_union_disjoint(res->dep[2 * j].map,
1019 must_rel[j]);
1020 res->dep[2 * j + 1].map =
1021 isl_map_union_disjoint(res->dep[2 * j + 1].map,
1022 may_rel[j]);
1025 if (isl_set_plain_is_empty(mustdo) &&
1026 isl_set_plain_is_empty(maydo))
1027 break;
1030 free(must_rel);
1031 free(may_rel);
1032 done:
1033 res->must_no_source = mustdo;
1034 res->may_no_source = maydo;
1035 return res;
1036 error:
1037 isl_flow_free(res);
1038 isl_set_free(mustdo);
1039 isl_set_free(maydo);
1040 free(must_rel);
1041 free(may_rel);
1042 return NULL;
1045 /* Given a "sink" access, a list of n "source" accesses,
1046 * compute for each iteration of the sink access
1047 * and for each element accessed by that iteration,
1048 * the source access in the list that last accessed the
1049 * element accessed by the sink access before this sink access.
1050 * Each access is given as a map from the loop iterators
1051 * to the array indices.
1052 * The result is a list of n relations between source and sink
1053 * iterations and a subset of the domain of the sink access,
1054 * corresponding to those iterations that access an element
1055 * not previously accessed.
1057 * To deal with multi-valued sink access relations, the sink iteration
1058 * domain is first extended with dimensions that correspond to the data
1059 * space. After the computation is finished, these extra dimensions are
1060 * projected out again.
1062 __isl_give isl_flow *isl_access_info_compute_flow(__isl_take isl_access_info *acc)
1064 int j;
1065 struct isl_flow *res = NULL;
1067 if (!acc)
1068 return NULL;
1070 acc->domain_map = isl_map_domain_map(isl_map_copy(acc->sink.map));
1071 acc->sink.map = isl_map_range_map(acc->sink.map);
1072 if (!acc->sink.map)
1073 goto error;
1075 if (acc->n_must == 0)
1076 res = compute_mem_based_dependences(acc);
1077 else {
1078 acc = isl_access_info_sort_sources(acc);
1079 res = compute_val_based_dependences(acc);
1081 if (!res)
1082 goto error;
1084 for (j = 0; j < res->n_source; ++j) {
1085 res->dep[j].map = isl_map_apply_range(res->dep[j].map,
1086 isl_map_copy(acc->domain_map));
1087 if (!res->dep[j].map)
1088 goto error;
1090 if (!res->must_no_source || !res->may_no_source)
1091 goto error;
1093 isl_access_info_free(acc);
1094 return res;
1095 error:
1096 isl_access_info_free(acc);
1097 isl_flow_free(res);
1098 return NULL;
1102 /* Keep track of some information about a schedule for a given
1103 * access. In particular, keep track of which dimensions
1104 * have a constant value and of the actual constant values.
1106 struct isl_sched_info {
1107 int *is_cst;
1108 isl_vec *cst;
1111 static void sched_info_free(__isl_take struct isl_sched_info *info)
1113 if (!info)
1114 return;
1115 isl_vec_free(info->cst);
1116 free(info->is_cst);
1117 free(info);
1120 /* Extract information on the constant dimensions of the schedule
1121 * for a given access. The "map" is of the form
1123 * [S -> D] -> A
1125 * with S the schedule domain, D the iteration domain and A the data domain.
1127 static __isl_give struct isl_sched_info *sched_info_alloc(
1128 __isl_keep isl_map *map)
1130 isl_ctx *ctx;
1131 isl_space *dim;
1132 struct isl_sched_info *info;
1133 int i, n;
1135 if (!map)
1136 return NULL;
1138 dim = isl_space_unwrap(isl_space_domain(isl_map_get_space(map)));
1139 if (!dim)
1140 return NULL;
1141 n = isl_space_dim(dim, isl_dim_in);
1142 isl_space_free(dim);
1144 ctx = isl_map_get_ctx(map);
1145 info = isl_alloc_type(ctx, struct isl_sched_info);
1146 if (!info)
1147 return NULL;
1148 info->is_cst = isl_alloc_array(ctx, int, n);
1149 info->cst = isl_vec_alloc(ctx, n);
1150 if (n && (!info->is_cst || !info->cst))
1151 goto error;
1153 for (i = 0; i < n; ++i) {
1154 isl_val *v;
1156 v = isl_map_plain_get_val_if_fixed(map, isl_dim_in, i);
1157 if (!v)
1158 goto error;
1159 info->is_cst[i] = !isl_val_is_nan(v);
1160 if (info->is_cst[i])
1161 info->cst = isl_vec_set_element_val(info->cst, i, v);
1162 else
1163 isl_val_free(v);
1166 return info;
1167 error:
1168 sched_info_free(info);
1169 return NULL;
1172 /* This structure represents the input for a dependence analysis computation.
1174 * "sink" represents the sink accesses.
1175 * "must_source" represents the definite source accesses.
1176 * "may_source" represents the possible source accesses.
1177 * "schedule_map" represents the execution order.
1179 * The domains of these four maps refer to the same iteration spaces(s).
1180 * The ranges of the first three maps also refer to the same data space(s).
1182 * After a call to isl_union_access_info_introduce_schedule,
1183 * the "schedule_map" field no longer contains useful information.
1185 struct isl_union_access_info {
1186 isl_union_map *sink;
1187 isl_union_map *must_source;
1188 isl_union_map *may_source;
1189 isl_union_map *schedule_map;
1192 /* Free "access" and return NULL.
1194 __isl_null isl_union_access_info *isl_union_access_info_free(
1195 __isl_take isl_union_access_info *access)
1197 if (!access)
1198 return NULL;
1200 isl_union_map_free(access->sink);
1201 isl_union_map_free(access->must_source);
1202 isl_union_map_free(access->may_source);
1203 isl_union_map_free(access->schedule_map);
1204 free(access);
1206 return NULL;
1209 /* Return the isl_ctx to which "access" belongs.
1211 isl_ctx *isl_union_access_info_get_ctx(__isl_keep isl_union_access_info *access)
1213 return access ? isl_union_map_get_ctx(access->sink) : NULL;
1216 /* Create a new isl_union_access_info with the given sink accesses and
1217 * and no source accesses or schedule information.
1219 __isl_give isl_union_access_info *isl_union_access_info_from_sink(
1220 __isl_take isl_union_map *sink)
1222 isl_ctx *ctx;
1223 isl_union_map *empty;
1224 isl_union_access_info *access;
1226 if (!sink)
1227 return NULL;
1228 ctx = isl_union_map_get_ctx(sink);
1229 access = isl_alloc_type(ctx, isl_union_access_info);
1230 if (!access)
1231 goto error;
1233 empty = isl_union_map_empty(isl_union_map_get_space(sink));
1234 access->sink = sink;
1235 access->must_source = isl_union_map_copy(empty);
1236 access->may_source = isl_union_map_copy(empty);
1237 access->schedule_map = empty;
1239 if (!access->sink || !access->must_source ||
1240 !access->may_source || !access->schedule_map)
1241 return isl_union_access_info_free(access);
1243 return access;
1244 error:
1245 isl_union_map_free(sink);
1246 return NULL;
1249 /* Replace the definite source accesses of "access" by "must_source".
1251 __isl_give isl_union_access_info *isl_union_access_info_set_must_source(
1252 __isl_take isl_union_access_info *access,
1253 __isl_take isl_union_map *must_source)
1255 if (!access || !must_source)
1256 goto error;
1258 isl_union_map_free(access->must_source);
1259 access->must_source = must_source;
1261 return access;
1262 error:
1263 isl_union_access_info_free(access);
1264 isl_union_map_free(must_source);
1265 return NULL;
1268 /* Replace the possible source accesses of "access" by "may_source".
1270 __isl_give isl_union_access_info *isl_union_access_info_set_may_source(
1271 __isl_take isl_union_access_info *access,
1272 __isl_take isl_union_map *may_source)
1274 if (!access || !may_source)
1275 goto error;
1277 isl_union_map_free(access->may_source);
1278 access->may_source = may_source;
1280 return access;
1281 error:
1282 isl_union_access_info_free(access);
1283 isl_union_map_free(may_source);
1284 return NULL;
1287 /* Replace the schedule map of "access" by "schedule_map".
1289 __isl_give isl_union_access_info *isl_union_access_info_set_schedule_map(
1290 __isl_take isl_union_access_info *access,
1291 __isl_take isl_union_map *schedule_map)
1293 if (!access || !schedule_map)
1294 goto error;
1296 isl_union_map_free(access->schedule_map);
1297 access->schedule_map = schedule_map;
1299 return access;
1300 error:
1301 isl_union_access_info_free(access);
1302 isl_union_map_free(schedule_map);
1303 return NULL;
1306 /* Update the fields of "access" such that they all have the same parameters.
1308 static __isl_give isl_union_access_info *isl_union_access_info_align_params(
1309 __isl_take isl_union_access_info *access)
1311 isl_space *space;
1313 if (!access)
1314 return NULL;
1316 space = isl_union_map_get_space(access->sink);
1317 space = isl_space_align_params(space,
1318 isl_union_map_get_space(access->must_source));
1319 space = isl_space_align_params(space,
1320 isl_union_map_get_space(access->may_source));
1321 space = isl_space_align_params(space,
1322 isl_union_map_get_space(access->schedule_map));
1323 access->sink = isl_union_map_align_params(access->sink,
1324 isl_space_copy(space));
1325 access->must_source = isl_union_map_align_params(access->must_source,
1326 isl_space_copy(space));
1327 access->may_source = isl_union_map_align_params(access->may_source,
1328 isl_space_copy(space));
1329 access->schedule_map = isl_union_map_align_params(access->schedule_map,
1330 space);
1332 if (!access->sink || !access->must_source ||
1333 !access->may_source || !access->schedule_map)
1334 return isl_union_access_info_free(access);
1336 return access;
1339 /* Prepend the schedule dimensions to the iteration domains.
1341 * That is, if the schedule is of the form
1343 * D -> S
1345 * while the access relations are of the form
1347 * D -> A
1349 * then the updated access relations are of the form
1351 * [S -> D] -> A
1353 * The schedule map is also replaced by the map
1355 * [S -> D] -> D
1357 * that is used during the internal computation.
1358 * Neither the original schedule map nor this updated schedule map
1359 * are used after the call to this function.
1361 static __isl_give isl_union_access_info *
1362 isl_union_access_info_introduce_schedule(
1363 __isl_take isl_union_access_info *access)
1365 isl_union_map *sm;
1367 if (!access)
1368 return NULL;
1370 sm = isl_union_map_reverse(access->schedule_map);
1371 sm = isl_union_map_range_map(sm);
1372 access->sink = isl_union_map_apply_range(isl_union_map_copy(sm),
1373 access->sink);
1374 access->may_source = isl_union_map_apply_range(isl_union_map_copy(sm),
1375 access->may_source);
1376 access->must_source = isl_union_map_apply_range(isl_union_map_copy(sm),
1377 access->must_source);
1378 access->schedule_map = sm;
1380 if (!access->sink || !access->must_source ||
1381 !access->may_source || !access->schedule_map)
1382 return isl_union_access_info_free(access);
1384 return access;
1387 /* This structure epresents the result of a dependence analysis computation.
1389 * "must_dep" represents the definite dependences.
1390 * "may_dep" represents the non-definite dependences.
1391 * "must_no_source" represents the subset of the sink accesses for which
1392 * definitely no source was found.
1393 * "may_no_source" represents the subset of the sink accesses for which
1394 * possibly, but not definitely, no source was found.
1396 struct isl_union_flow {
1397 isl_union_map *must_dep;
1398 isl_union_map *may_dep;
1399 isl_union_map *must_no_source;
1400 isl_union_map *may_no_source;
1403 /* Free "flow" and return NULL.
1405 __isl_null isl_union_flow *isl_union_flow_free(__isl_take isl_union_flow *flow)
1407 if (!flow)
1408 return NULL;
1409 isl_union_map_free(flow->must_dep);
1410 isl_union_map_free(flow->may_dep);
1411 isl_union_map_free(flow->must_no_source);
1412 isl_union_map_free(flow->may_no_source);
1413 free(flow);
1414 return NULL;
1417 void isl_union_flow_dump(__isl_keep isl_union_flow *flow)
1419 if (!flow)
1420 return;
1422 fprintf(stderr, "must dependences: ");
1423 isl_union_map_dump(flow->must_dep);
1424 fprintf(stderr, "may dependences: ");
1425 isl_union_map_dump(flow->may_dep);
1426 fprintf(stderr, "must no source: ");
1427 isl_union_map_dump(flow->must_no_source);
1428 fprintf(stderr, "may no source: ");
1429 isl_union_map_dump(flow->may_no_source);
1432 /* Return the definite dependences in "flow".
1434 __isl_give isl_union_map *isl_union_flow_get_must_dependence(
1435 __isl_keep isl_union_flow *flow)
1437 if (!flow)
1438 return NULL;
1439 return isl_union_map_copy(flow->must_dep);
1442 /* Return the possible dependences in "flow", including the definite
1443 * dependences.
1445 __isl_give isl_union_map *isl_union_flow_get_may_dependence(
1446 __isl_keep isl_union_flow *flow)
1448 if (!flow)
1449 return NULL;
1450 return isl_union_map_union(isl_union_map_copy(flow->must_dep),
1451 isl_union_map_copy(flow->may_dep));
1454 /* Return the non-definite dependences in "flow".
1456 static __isl_give isl_union_map *isl_union_flow_get_non_must_dependence(
1457 __isl_keep isl_union_flow *flow)
1459 if (!flow)
1460 return NULL;
1461 return isl_union_map_copy(flow->may_dep);
1464 /* Return the subset of the sink accesses for which definitely
1465 * no source was found.
1467 __isl_give isl_union_map *isl_union_flow_get_must_no_source(
1468 __isl_keep isl_union_flow *flow)
1470 if (!flow)
1471 return NULL;
1472 return isl_union_map_copy(flow->must_no_source);
1475 /* Return the subset of the sink accesses for which possibly
1476 * no source was found, including those for which definitely
1477 * no source was found.
1479 __isl_give isl_union_map *isl_union_flow_get_may_no_source(
1480 __isl_keep isl_union_flow *flow)
1482 if (!flow)
1483 return NULL;
1484 return isl_union_map_union(isl_union_map_copy(flow->must_no_source),
1485 isl_union_map_copy(flow->may_no_source));
1488 /* Return the subset of the sink accesses for which possibly, but not
1489 * definitely, no source was found.
1491 static __isl_give isl_union_map *isl_union_flow_get_non_must_no_source(
1492 __isl_keep isl_union_flow *flow)
1494 if (!flow)
1495 return NULL;
1496 return isl_union_map_copy(flow->may_no_source);
1499 /* Create a new isl_union_flow object, initialized with empty
1500 * dependence relations and sink subsets.
1502 static __isl_give isl_union_flow *isl_union_flow_alloc(
1503 __isl_take isl_space *space)
1505 isl_ctx *ctx;
1506 isl_union_map *empty;
1507 isl_union_flow *flow;
1509 if (!space)
1510 return NULL;
1511 ctx = isl_space_get_ctx(space);
1512 flow = isl_alloc_type(ctx, isl_union_flow);
1513 if (!flow)
1514 goto error;
1516 empty = isl_union_map_empty(space);
1517 flow->must_dep = isl_union_map_copy(empty);
1518 flow->may_dep = isl_union_map_copy(empty);
1519 flow->must_no_source = isl_union_map_copy(empty);
1520 flow->may_no_source = empty;
1522 if (!flow->must_dep || !flow->may_dep ||
1523 !flow->must_no_source || !flow->may_no_source)
1524 return isl_union_flow_free(flow);
1526 return flow;
1527 error:
1528 isl_space_free(space);
1529 return NULL;
1532 /* Drop the schedule dimensions from the iteration domains in "flow".
1533 * In particular, the schedule dimensions have been prepended
1534 * to the iteration domains prior to the dependence analysis by
1535 * replacing the iteration domain D, by the wrapped map [S -> D].
1536 * Replace these wrapped maps by the original D.
1538 static __isl_give isl_union_flow *isl_union_flow_drop_schedule(
1539 __isl_take isl_union_flow *flow)
1541 if (!flow)
1542 return NULL;
1544 flow->must_dep = isl_union_map_factor_range(flow->must_dep);
1545 flow->may_dep = isl_union_map_factor_range(flow->may_dep);
1546 flow->must_no_source =
1547 isl_union_map_domain_factor_range(flow->must_no_source);
1548 flow->may_no_source =
1549 isl_union_map_domain_factor_range(flow->may_no_source);
1551 if (!flow->must_dep || !flow->may_dep ||
1552 !flow->must_no_source || !flow->may_no_source)
1553 return isl_union_flow_free(flow);
1555 return flow;
1558 struct isl_compute_flow_data {
1559 isl_union_map *must_source;
1560 isl_union_map *may_source;
1561 isl_union_flow *flow;
1563 int count;
1564 int must;
1565 isl_space *dim;
1566 struct isl_sched_info *sink_info;
1567 struct isl_sched_info **source_info;
1568 isl_access_info *accesses;
1571 static int count_matching_array(__isl_take isl_map *map, void *user)
1573 int eq;
1574 isl_space *dim;
1575 struct isl_compute_flow_data *data;
1577 data = (struct isl_compute_flow_data *)user;
1579 dim = isl_space_range(isl_map_get_space(map));
1581 eq = isl_space_is_equal(dim, data->dim);
1583 isl_space_free(dim);
1584 isl_map_free(map);
1586 if (eq < 0)
1587 return -1;
1588 if (eq)
1589 data->count++;
1591 return 0;
1594 static int collect_matching_array(__isl_take isl_map *map, void *user)
1596 int eq;
1597 isl_space *dim;
1598 struct isl_sched_info *info;
1599 struct isl_compute_flow_data *data;
1601 data = (struct isl_compute_flow_data *)user;
1603 dim = isl_space_range(isl_map_get_space(map));
1605 eq = isl_space_is_equal(dim, data->dim);
1607 isl_space_free(dim);
1609 if (eq < 0)
1610 goto error;
1611 if (!eq) {
1612 isl_map_free(map);
1613 return 0;
1616 info = sched_info_alloc(map);
1617 data->source_info[data->count] = info;
1619 data->accesses = isl_access_info_add_source(data->accesses,
1620 map, data->must, info);
1622 data->count++;
1624 return 0;
1625 error:
1626 isl_map_free(map);
1627 return -1;
1630 /* Determine the shared nesting level and the "textual order" of
1631 * the given accesses.
1633 * We first determine the minimal schedule dimension for both accesses.
1635 * If among those dimensions, we can find one where both have a fixed
1636 * value and if moreover those values are different, then the previous
1637 * dimension is the last shared nesting level and the textual order
1638 * is determined based on the order of the fixed values.
1639 * If no such fixed values can be found, then we set the shared
1640 * nesting level to the minimal schedule dimension, with no textual ordering.
1642 static int before(void *first, void *second)
1644 struct isl_sched_info *info1 = first;
1645 struct isl_sched_info *info2 = second;
1646 int n1, n2;
1647 int i;
1649 n1 = isl_vec_size(info1->cst);
1650 n2 = isl_vec_size(info2->cst);
1652 if (n2 < n1)
1653 n1 = n2;
1655 for (i = 0; i < n1; ++i) {
1656 int r;
1657 int cmp;
1659 if (!info1->is_cst[i])
1660 continue;
1661 if (!info2->is_cst[i])
1662 continue;
1663 cmp = isl_vec_cmp_element(info1->cst, info2->cst, i);
1664 if (cmp == 0)
1665 continue;
1667 r = 2 * i + (cmp < 0);
1669 return r;
1672 return 2 * n1;
1675 /* Given a sink access, look for all the source accesses that access
1676 * the same array and perform dataflow analysis on them using
1677 * isl_access_info_compute_flow.
1679 static int compute_flow(__isl_take isl_map *map, void *user)
1681 int i;
1682 isl_ctx *ctx;
1683 struct isl_compute_flow_data *data;
1684 isl_flow *flow;
1685 isl_union_flow *df;
1687 data = (struct isl_compute_flow_data *)user;
1688 df = data->flow;
1690 ctx = isl_map_get_ctx(map);
1692 data->accesses = NULL;
1693 data->sink_info = NULL;
1694 data->source_info = NULL;
1695 data->count = 0;
1696 data->dim = isl_space_range(isl_map_get_space(map));
1698 if (isl_union_map_foreach_map(data->must_source,
1699 &count_matching_array, data) < 0)
1700 goto error;
1701 if (isl_union_map_foreach_map(data->may_source,
1702 &count_matching_array, data) < 0)
1703 goto error;
1705 data->sink_info = sched_info_alloc(map);
1706 data->source_info = isl_calloc_array(ctx, struct isl_sched_info *,
1707 data->count);
1709 data->accesses = isl_access_info_alloc(isl_map_copy(map),
1710 data->sink_info, &before, data->count);
1711 if (!data->sink_info || (data->count && !data->source_info) ||
1712 !data->accesses)
1713 goto error;
1714 data->count = 0;
1715 data->must = 1;
1716 if (isl_union_map_foreach_map(data->must_source,
1717 &collect_matching_array, data) < 0)
1718 goto error;
1719 data->must = 0;
1720 if (isl_union_map_foreach_map(data->may_source,
1721 &collect_matching_array, data) < 0)
1722 goto error;
1724 flow = isl_access_info_compute_flow(data->accesses);
1725 data->accesses = NULL;
1727 if (!flow)
1728 goto error;
1730 df->must_no_source = isl_union_map_union(df->must_no_source,
1731 isl_union_map_from_map(isl_flow_get_no_source(flow, 1)));
1732 df->may_no_source = isl_union_map_union(df->may_no_source,
1733 isl_union_map_from_map(isl_flow_get_no_source(flow, 0)));
1735 for (i = 0; i < flow->n_source; ++i) {
1736 isl_union_map *dep;
1737 dep = isl_union_map_from_map(isl_map_copy(flow->dep[i].map));
1738 if (flow->dep[i].must)
1739 df->must_dep = isl_union_map_union(df->must_dep, dep);
1740 else
1741 df->may_dep = isl_union_map_union(df->may_dep, dep);
1744 isl_flow_free(flow);
1746 sched_info_free(data->sink_info);
1747 if (data->source_info) {
1748 for (i = 0; i < data->count; ++i)
1749 sched_info_free(data->source_info[i]);
1750 free(data->source_info);
1752 isl_space_free(data->dim);
1753 isl_map_free(map);
1755 return 0;
1756 error:
1757 isl_access_info_free(data->accesses);
1758 sched_info_free(data->sink_info);
1759 if (data->source_info) {
1760 for (i = 0; i < data->count; ++i)
1761 sched_info_free(data->source_info[i]);
1762 free(data->source_info);
1764 isl_space_free(data->dim);
1765 isl_map_free(map);
1767 return -1;
1770 /* Remove the must accesses from the may accesses.
1772 * A must access always trumps a may access, so there is no need
1773 * for a must access to also be considered as a may access. Doing so
1774 * would only cost extra computations only to find out that
1775 * the duplicated may access does not make any difference.
1777 static __isl_give isl_union_access_info *isl_union_access_info_normalize(
1778 __isl_take isl_union_access_info *access)
1780 if (!access)
1781 return NULL;
1782 access->may_source = isl_union_map_subtract(access->may_source,
1783 isl_union_map_copy(access->must_source));
1784 if (!access->may_source)
1785 return isl_union_access_info_free(access);
1787 return access;
1790 /* Given a description of the "sink" accesses, the "source" accesses and
1791 * a schedule, compute for each instance of a sink access
1792 * and for each element accessed by that instance,
1793 * the possible or definite source accesses that last accessed the
1794 * element accessed by the sink access before this sink access
1795 * in the sense that there is no intermediate definite source access.
1797 * The must_no_source and may_no_source elements of the result
1798 * are subsets of access->sink. The elements must_dep and may_dep
1799 * map domain elements of access->{may,must)_source to
1800 * domain elements of access->sink.
1802 * We first prepend the schedule dimensions to the domain
1803 * of the accesses so that we can easily compare their relative order.
1804 * Then we consider each sink access individually in compute_flow.
1806 __isl_give isl_union_flow *isl_union_access_info_compute_flow(
1807 __isl_take isl_union_access_info *access)
1809 struct isl_compute_flow_data data;
1811 access = isl_union_access_info_normalize(access);
1812 access = isl_union_access_info_align_params(access);
1813 access = isl_union_access_info_introduce_schedule(access);
1814 if (!access)
1815 return NULL;
1817 data.must_source = access->must_source;
1818 data.may_source = access->may_source;
1820 data.flow = isl_union_flow_alloc(isl_union_map_get_space(access->sink));
1822 if (isl_union_map_foreach_map(access->sink, &compute_flow, &data) < 0)
1823 goto error;
1825 data.flow = isl_union_flow_drop_schedule(data.flow);
1827 isl_union_access_info_free(access);
1828 return data.flow;
1829 error:
1830 isl_union_access_info_free(access);
1831 isl_union_flow_free(data.flow);
1832 return NULL;
1835 /* Given a collection of "sink" and "source" accesses,
1836 * compute for each iteration of a sink access
1837 * and for each element accessed by that iteration,
1838 * the source access in the list that last accessed the
1839 * element accessed by the sink access before this sink access.
1840 * Each access is given as a map from the loop iterators
1841 * to the array indices.
1842 * The result is a relations between source and sink
1843 * iterations and a subset of the domain of the sink accesses,
1844 * corresponding to those iterations that access an element
1845 * not previously accessed.
1847 * We collect the inputs in an isl_union_access_info object,
1848 * call isl_union_access_info_compute_flow and extract
1849 * the outputs from the result.
1851 int isl_union_map_compute_flow(__isl_take isl_union_map *sink,
1852 __isl_take isl_union_map *must_source,
1853 __isl_take isl_union_map *may_source,
1854 __isl_take isl_union_map *schedule,
1855 __isl_give isl_union_map **must_dep, __isl_give isl_union_map **may_dep,
1856 __isl_give isl_union_map **must_no_source,
1857 __isl_give isl_union_map **may_no_source)
1859 isl_union_access_info *access;
1860 isl_union_flow *flow;
1862 access = isl_union_access_info_from_sink(sink);
1863 access = isl_union_access_info_set_must_source(access, must_source);
1864 access = isl_union_access_info_set_may_source(access, may_source);
1865 access = isl_union_access_info_set_schedule_map(access, schedule);
1866 flow = isl_union_access_info_compute_flow(access);
1868 if (must_dep)
1869 *must_dep = isl_union_flow_get_must_dependence(flow);
1870 if (may_dep)
1871 *may_dep = isl_union_flow_get_non_must_dependence(flow);
1872 if (must_no_source)
1873 *must_no_source = isl_union_flow_get_must_no_source(flow);
1874 if (may_no_source)
1875 *may_no_source = isl_union_flow_get_non_must_no_source(flow);
1877 isl_union_flow_free(flow);
1879 if ((must_dep && !*must_dep) || (may_dep && !*may_dep) ||
1880 (must_no_source && !*must_no_source) ||
1881 (may_no_source && !*may_no_source))
1882 goto error;
1884 return 0;
1885 error:
1886 if (must_dep)
1887 *must_dep = isl_union_map_free(*must_dep);
1888 if (may_dep)
1889 *may_dep = isl_union_map_free(*may_dep);
1890 if (must_no_source)
1891 *must_no_source = isl_union_map_free(*must_no_source);
1892 if (may_no_source)
1893 *may_no_source = isl_union_map_free(*may_no_source);
1894 return -1;