isl_access_info: change interface for specifying restrictions
[isl.git] / isl_flow.c
blob2059365938a315425edfeaa68afe34655f9a4d34
1 /*
2 * Copyright 2005-2007 Universiteit Leiden
3 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Copyright 2010 INRIA Saclay
5 * Copyright 2012 Universiteit Leiden
7 * Use of this software is governed by the GNU LGPLv2.1 license
9 * Written by Sven Verdoolaege, Leiden Institute of Advanced Computer Science,
10 * Universiteit Leiden, Niels Bohrweg 1, 2333 CA Leiden, The Netherlands
11 * and K.U.Leuven, Departement Computerwetenschappen, Celestijnenlaan 200A,
12 * B-3001 Leuven, Belgium
13 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
14 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
17 #include <isl/set.h>
18 #include <isl/map.h>
19 #include <isl/flow.h>
20 #include <isl_qsort.h>
22 enum isl_restriction_type {
23 isl_restriction_type_empty,
24 isl_restriction_type_none,
25 isl_restriction_type_input,
26 isl_restriction_type_output
29 struct isl_restriction {
30 enum isl_restriction_type type;
32 isl_set *source;
33 isl_set *sink;
36 /* Create a restriction that doesn't restrict anything.
38 __isl_give isl_restriction *isl_restriction_none(__isl_keep isl_map *source_map)
40 isl_ctx *ctx;
41 isl_restriction *restr;
43 if (!source_map)
44 return NULL;
46 ctx = isl_map_get_ctx(source_map);
47 restr = isl_calloc_type(ctx, struct isl_restriction);
48 if (!restr)
49 return NULL;
51 restr->type = isl_restriction_type_none;
53 return restr;
56 /* Create a restriction that removes everything.
58 __isl_give isl_restriction *isl_restriction_empty(
59 __isl_keep isl_map *source_map)
61 isl_ctx *ctx;
62 isl_restriction *restr;
64 if (!source_map)
65 return NULL;
67 ctx = isl_map_get_ctx(source_map);
68 restr = isl_calloc_type(ctx, struct isl_restriction);
69 if (!restr)
70 return NULL;
72 restr->type = isl_restriction_type_empty;
74 return restr;
77 /* Create a restriction on the input of the maximization problem
78 * based on the given source and sink restrictions.
80 __isl_give isl_restriction *isl_restriction_input(
81 __isl_take isl_set *source_restr, __isl_take isl_set *sink_restr)
83 isl_ctx *ctx;
84 isl_restriction *restr;
86 if (!source_restr || !sink_restr)
87 goto error;
89 ctx = isl_set_get_ctx(source_restr);
90 restr = isl_calloc_type(ctx, struct isl_restriction);
91 if (!restr)
92 goto error;
94 restr->type = isl_restriction_type_input;
95 restr->source = source_restr;
96 restr->sink = sink_restr;
98 return restr;
99 error:
100 isl_set_free(source_restr);
101 isl_set_free(sink_restr);
102 return NULL;
105 /* Create a restriction on the output of the maximization problem
106 * based on the given source restriction.
108 __isl_give isl_restriction *isl_restriction_output(
109 __isl_take isl_set *source_restr)
111 isl_ctx *ctx;
112 isl_restriction *restr;
114 if (!source_restr)
115 return NULL;
117 ctx = isl_set_get_ctx(source_restr);
118 restr = isl_calloc_type(ctx, struct isl_restriction);
119 if (!restr)
120 goto error;
122 restr->type = isl_restriction_type_output;
123 restr->source = source_restr;
125 return restr;
126 error:
127 isl_set_free(source_restr);
128 return NULL;
131 void *isl_restriction_free(__isl_take isl_restriction *restr)
133 if (!restr)
134 return NULL;
136 isl_set_free(restr->source);
137 isl_set_free(restr->sink);
138 free(restr);
139 return NULL;
142 /* A private structure to keep track of a mapping together with
143 * a user-specified identifier and a boolean indicating whether
144 * the map represents a must or may access/dependence.
146 struct isl_labeled_map {
147 struct isl_map *map;
148 void *data;
149 int must;
152 /* A structure containing the input for dependence analysis:
153 * - a sink
154 * - n_must + n_may (<= max_source) sources
155 * - a function for determining the relative order of sources and sink
156 * The must sources are placed before the may sources.
158 * domain_map is an auxiliary map that maps the sink access relation
159 * to the domain of this access relation.
161 * restrict_fn is a callback that (if not NULL) will be called
162 * right before any lexicographical maximization.
164 struct isl_access_info {
165 isl_map *domain_map;
166 struct isl_labeled_map sink;
167 isl_access_level_before level_before;
169 isl_access_restrict restrict_fn;
170 void *restrict_user;
172 int max_source;
173 int n_must;
174 int n_may;
175 struct isl_labeled_map source[1];
178 /* A structure containing the output of dependence analysis:
179 * - n_source dependences
180 * - a wrapped subset of the sink for which definitely no source could be found
181 * - a wrapped subset of the sink for which possibly no source could be found
183 struct isl_flow {
184 isl_set *must_no_source;
185 isl_set *may_no_source;
186 int n_source;
187 struct isl_labeled_map *dep;
190 /* Construct an isl_access_info structure and fill it up with
191 * the given data. The number of sources is set to 0.
193 __isl_give isl_access_info *isl_access_info_alloc(__isl_take isl_map *sink,
194 void *sink_user, isl_access_level_before fn, int max_source)
196 isl_ctx *ctx;
197 struct isl_access_info *acc;
199 if (!sink)
200 return NULL;
202 ctx = isl_map_get_ctx(sink);
203 isl_assert(ctx, max_source >= 0, goto error);
205 acc = isl_calloc(ctx, struct isl_access_info,
206 sizeof(struct isl_access_info) +
207 (max_source - 1) * sizeof(struct isl_labeled_map));
208 if (!acc)
209 goto error;
211 acc->sink.map = sink;
212 acc->sink.data = sink_user;
213 acc->level_before = fn;
214 acc->max_source = max_source;
215 acc->n_must = 0;
216 acc->n_may = 0;
218 return acc;
219 error:
220 isl_map_free(sink);
221 return NULL;
224 /* Free the given isl_access_info structure.
226 void isl_access_info_free(__isl_take isl_access_info *acc)
228 int i;
230 if (!acc)
231 return;
232 isl_map_free(acc->domain_map);
233 isl_map_free(acc->sink.map);
234 for (i = 0; i < acc->n_must + acc->n_may; ++i)
235 isl_map_free(acc->source[i].map);
236 free(acc);
239 isl_ctx *isl_access_info_get_ctx(__isl_keep isl_access_info *acc)
241 return acc ? isl_map_get_ctx(acc->sink.map) : NULL;
244 __isl_give isl_access_info *isl_access_info_set_restrict(
245 __isl_take isl_access_info *acc, isl_access_restrict fn, void *user)
247 if (!acc)
248 return NULL;
249 acc->restrict_fn = fn;
250 acc->restrict_user = user;
251 return acc;
254 /* Add another source to an isl_access_info structure, making
255 * sure the "must" sources are placed before the "may" sources.
256 * This function may be called at most max_source times on a
257 * given isl_access_info structure, with max_source as specified
258 * in the call to isl_access_info_alloc that constructed the structure.
260 __isl_give isl_access_info *isl_access_info_add_source(
261 __isl_take isl_access_info *acc, __isl_take isl_map *source,
262 int must, void *source_user)
264 isl_ctx *ctx;
266 if (!acc)
267 return NULL;
268 ctx = isl_map_get_ctx(acc->sink.map);
269 isl_assert(ctx, acc->n_must + acc->n_may < acc->max_source, goto error);
271 if (must) {
272 if (acc->n_may)
273 acc->source[acc->n_must + acc->n_may] =
274 acc->source[acc->n_must];
275 acc->source[acc->n_must].map = source;
276 acc->source[acc->n_must].data = source_user;
277 acc->source[acc->n_must].must = 1;
278 acc->n_must++;
279 } else {
280 acc->source[acc->n_must + acc->n_may].map = source;
281 acc->source[acc->n_must + acc->n_may].data = source_user;
282 acc->source[acc->n_must + acc->n_may].must = 0;
283 acc->n_may++;
286 return acc;
287 error:
288 isl_map_free(source);
289 isl_access_info_free(acc);
290 return NULL;
293 /* Return -n, 0 or n (with n a positive value), depending on whether
294 * the source access identified by p1 should be sorted before, together
295 * or after that identified by p2.
297 * If p1 appears before p2, then it should be sorted first.
298 * For more generic initial schedules, it is possible that neither
299 * p1 nor p2 appears before the other, or at least not in any obvious way.
300 * We therefore also check if p2 appears before p1, in which case p2
301 * should be sorted first.
302 * If not, we try to order the two statements based on the description
303 * of the iteration domains. This results in an arbitrary, but fairly
304 * stable ordering.
306 static int access_sort_cmp(const void *p1, const void *p2, void *user)
308 isl_access_info *acc = user;
309 const struct isl_labeled_map *i1, *i2;
310 int level1, level2;
311 uint32_t h1, h2;
312 i1 = (const struct isl_labeled_map *) p1;
313 i2 = (const struct isl_labeled_map *) p2;
315 level1 = acc->level_before(i1->data, i2->data);
316 if (level1 % 2)
317 return -1;
319 level2 = acc->level_before(i2->data, i1->data);
320 if (level2 % 2)
321 return 1;
323 h1 = isl_map_get_hash(i1->map);
324 h2 = isl_map_get_hash(i2->map);
325 return h1 > h2 ? 1 : h1 < h2 ? -1 : 0;
328 /* Sort the must source accesses in their textual order.
330 static __isl_give isl_access_info *isl_access_info_sort_sources(
331 __isl_take isl_access_info *acc)
333 if (!acc)
334 return NULL;
335 if (acc->n_must <= 1)
336 return acc;
338 isl_quicksort(acc->source, acc->n_must, sizeof(struct isl_labeled_map),
339 access_sort_cmp, acc);
341 return acc;
344 /* Align the parameters of the two spaces if needed and then call
345 * isl_space_join.
347 static __isl_give isl_space *space_align_and_join(__isl_take isl_space *left,
348 __isl_take isl_space *right)
350 if (isl_space_match(left, isl_dim_param, right, isl_dim_param))
351 return isl_space_join(left, right);
353 left = isl_space_align_params(left, isl_space_copy(right));
354 right = isl_space_align_params(right, isl_space_copy(left));
355 return isl_space_join(left, right);
358 /* Initialize an empty isl_flow structure corresponding to a given
359 * isl_access_info structure.
360 * For each must access, two dependences are created (initialized
361 * to the empty relation), one for the resulting must dependences
362 * and one for the resulting may dependences. May accesses can
363 * only lead to may dependences, so only one dependence is created
364 * for each of them.
365 * This function is private as isl_flow structures are only supposed
366 * to be created by isl_access_info_compute_flow.
368 static __isl_give isl_flow *isl_flow_alloc(__isl_keep isl_access_info *acc)
370 int i;
371 struct isl_ctx *ctx;
372 struct isl_flow *dep;
374 if (!acc)
375 return NULL;
377 ctx = isl_map_get_ctx(acc->sink.map);
378 dep = isl_calloc_type(ctx, struct isl_flow);
379 if (!dep)
380 return NULL;
382 dep->dep = isl_calloc_array(ctx, struct isl_labeled_map,
383 2 * acc->n_must + acc->n_may);
384 if (!dep->dep)
385 goto error;
387 dep->n_source = 2 * acc->n_must + acc->n_may;
388 for (i = 0; i < acc->n_must; ++i) {
389 isl_space *dim;
390 dim = space_align_and_join(
391 isl_map_get_space(acc->source[i].map),
392 isl_space_reverse(isl_map_get_space(acc->sink.map)));
393 dep->dep[2 * i].map = isl_map_empty(dim);
394 dep->dep[2 * i + 1].map = isl_map_copy(dep->dep[2 * i].map);
395 dep->dep[2 * i].data = acc->source[i].data;
396 dep->dep[2 * i + 1].data = acc->source[i].data;
397 dep->dep[2 * i].must = 1;
398 dep->dep[2 * i + 1].must = 0;
399 if (!dep->dep[2 * i].map || !dep->dep[2 * i + 1].map)
400 goto error;
402 for (i = acc->n_must; i < acc->n_must + acc->n_may; ++i) {
403 isl_space *dim;
404 dim = space_align_and_join(
405 isl_map_get_space(acc->source[i].map),
406 isl_space_reverse(isl_map_get_space(acc->sink.map)));
407 dep->dep[acc->n_must + i].map = isl_map_empty(dim);
408 dep->dep[acc->n_must + i].data = acc->source[i].data;
409 dep->dep[acc->n_must + i].must = 0;
410 if (!dep->dep[acc->n_must + i].map)
411 goto error;
414 return dep;
415 error:
416 isl_flow_free(dep);
417 return NULL;
420 /* Iterate over all sources and for each resulting flow dependence
421 * that is not empty, call the user specfied function.
422 * The second argument in this function call identifies the source,
423 * while the third argument correspond to the final argument of
424 * the isl_flow_foreach call.
426 int isl_flow_foreach(__isl_keep isl_flow *deps,
427 int (*fn)(__isl_take isl_map *dep, int must, void *dep_user, void *user),
428 void *user)
430 int i;
432 if (!deps)
433 return -1;
435 for (i = 0; i < deps->n_source; ++i) {
436 if (isl_map_plain_is_empty(deps->dep[i].map))
437 continue;
438 if (fn(isl_map_copy(deps->dep[i].map), deps->dep[i].must,
439 deps->dep[i].data, user) < 0)
440 return -1;
443 return 0;
446 /* Return a copy of the subset of the sink for which no source could be found.
448 __isl_give isl_map *isl_flow_get_no_source(__isl_keep isl_flow *deps, int must)
450 if (!deps)
451 return NULL;
453 if (must)
454 return isl_set_unwrap(isl_set_copy(deps->must_no_source));
455 else
456 return isl_set_unwrap(isl_set_copy(deps->may_no_source));
459 void isl_flow_free(__isl_take isl_flow *deps)
461 int i;
463 if (!deps)
464 return;
465 isl_set_free(deps->must_no_source);
466 isl_set_free(deps->may_no_source);
467 if (deps->dep) {
468 for (i = 0; i < deps->n_source; ++i)
469 isl_map_free(deps->dep[i].map);
470 free(deps->dep);
472 free(deps);
475 isl_ctx *isl_flow_get_ctx(__isl_keep isl_flow *deps)
477 return deps ? isl_set_get_ctx(deps->must_no_source) : NULL;
480 /* Return a map that enforces that the domain iteration occurs after
481 * the range iteration at the given level.
482 * If level is odd, then the domain iteration should occur after
483 * the target iteration in their shared level/2 outermost loops.
484 * In this case we simply need to enforce that these outermost
485 * loop iterations are the same.
486 * If level is even, then the loop iterator of the domain should
487 * be greater than the loop iterator of the range at the last
488 * of the level/2 shared loops, i.e., loop level/2 - 1.
490 static __isl_give isl_map *after_at_level(__isl_take isl_space *dim, int level)
492 struct isl_basic_map *bmap;
494 if (level % 2)
495 bmap = isl_basic_map_equal(dim, level/2);
496 else
497 bmap = isl_basic_map_more_at(dim, level/2 - 1);
499 return isl_map_from_basic_map(bmap);
502 /* Compute the partial lexicographic maximum of "dep" on domain "sink",
503 * but first check if the user has set acc->restrict_fn and if so
504 * update either the input or the output of the maximization problem
505 * with respect to the resulting restriction.
507 * Since the user expects a mapping from sink iterations to source iterations,
508 * whereas the domain of "dep" is a wrapped map, mapping sink iterations
509 * to accessed array elements, we first need to project out the accessed
510 * sink array elements by applying acc->domain_map.
511 * Similarly, the sink restriction specified by the user needs to be
512 * converted back to the wrapped map.
514 static __isl_give isl_map *restricted_partial_lexmax(
515 __isl_keep isl_access_info *acc, __isl_take isl_map *dep,
516 int source, __isl_take isl_set *sink, __isl_give isl_set **empty)
518 isl_map *source_map;
519 isl_restriction *restr;
520 isl_set *sink_domain;
521 isl_set *sink_restr;
522 isl_map *res;
524 if (!acc->restrict_fn)
525 return isl_map_partial_lexmax(dep, sink, empty);
527 source_map = isl_map_copy(dep);
528 source_map = isl_map_apply_domain(source_map,
529 isl_map_copy(acc->domain_map));
530 sink_domain = isl_set_copy(sink);
531 sink_domain = isl_set_apply(sink_domain, isl_map_copy(acc->domain_map));
532 restr = acc->restrict_fn(source_map, sink_domain,
533 acc->source[source].data, acc->restrict_user);
534 isl_set_free(sink_domain);
535 isl_map_free(source_map);
537 if (!restr)
538 goto error;
539 if (restr->type == isl_restriction_type_input) {
540 dep = isl_map_intersect_range(dep, isl_set_copy(restr->source));
541 sink_restr = isl_set_copy(restr->sink);
542 sink_restr = isl_set_apply(sink_restr,
543 isl_map_reverse(isl_map_copy(acc->domain_map)));
544 sink = isl_set_intersect(sink, sink_restr);
545 } else if (restr->type == isl_restriction_type_empty) {
546 isl_space *space = isl_map_get_space(dep);
547 isl_map_free(dep);
548 dep = isl_map_empty(space);
551 res = isl_map_partial_lexmax(dep, sink, empty);
553 if (restr->type == isl_restriction_type_output)
554 res = isl_map_intersect_range(res, isl_set_copy(restr->source));
556 isl_restriction_free(restr);
557 return res;
558 error:
559 isl_map_free(dep);
560 isl_set_free(sink);
561 *empty = NULL;
562 return NULL;
565 /* Compute the last iteration of must source j that precedes the sink
566 * at the given level for sink iterations in set_C.
567 * The subset of set_C for which no such iteration can be found is returned
568 * in *empty.
570 static struct isl_map *last_source(struct isl_access_info *acc,
571 struct isl_set *set_C,
572 int j, int level, struct isl_set **empty)
574 struct isl_map *read_map;
575 struct isl_map *write_map;
576 struct isl_map *dep_map;
577 struct isl_map *after;
578 struct isl_map *result;
580 read_map = isl_map_copy(acc->sink.map);
581 write_map = isl_map_copy(acc->source[j].map);
582 write_map = isl_map_reverse(write_map);
583 dep_map = isl_map_apply_range(read_map, write_map);
584 after = after_at_level(isl_map_get_space(dep_map), level);
585 dep_map = isl_map_intersect(dep_map, after);
586 result = restricted_partial_lexmax(acc, dep_map, j, set_C, empty);
587 result = isl_map_reverse(result);
589 return result;
592 /* For a given mapping between iterations of must source j and iterations
593 * of the sink, compute the last iteration of must source k preceding
594 * the sink at level before_level for any of the sink iterations,
595 * but following the corresponding iteration of must source j at level
596 * after_level.
598 static struct isl_map *last_later_source(struct isl_access_info *acc,
599 struct isl_map *old_map,
600 int j, int before_level,
601 int k, int after_level,
602 struct isl_set **empty)
604 isl_space *dim;
605 struct isl_set *set_C;
606 struct isl_map *read_map;
607 struct isl_map *write_map;
608 struct isl_map *dep_map;
609 struct isl_map *after_write;
610 struct isl_map *before_read;
611 struct isl_map *result;
613 set_C = isl_map_range(isl_map_copy(old_map));
614 read_map = isl_map_copy(acc->sink.map);
615 write_map = isl_map_copy(acc->source[k].map);
617 write_map = isl_map_reverse(write_map);
618 dep_map = isl_map_apply_range(read_map, write_map);
619 dim = space_align_and_join(isl_map_get_space(acc->source[k].map),
620 isl_space_reverse(isl_map_get_space(acc->source[j].map)));
621 after_write = after_at_level(dim, after_level);
622 after_write = isl_map_apply_range(after_write, old_map);
623 after_write = isl_map_reverse(after_write);
624 dep_map = isl_map_intersect(dep_map, after_write);
625 before_read = after_at_level(isl_map_get_space(dep_map), before_level);
626 dep_map = isl_map_intersect(dep_map, before_read);
627 result = restricted_partial_lexmax(acc, dep_map, k, set_C, empty);
628 result = isl_map_reverse(result);
630 return result;
633 /* Given a shared_level between two accesses, return 1 if the
634 * the first can precede the second at the requested target_level.
635 * If the target level is odd, i.e., refers to a statement level
636 * dimension, then first needs to precede second at the requested
637 * level, i.e., shared_level must be equal to target_level.
638 * If the target level is odd, then the two loops should share
639 * at least the requested number of outer loops.
641 static int can_precede_at_level(int shared_level, int target_level)
643 if (shared_level < target_level)
644 return 0;
645 if ((target_level % 2) && shared_level > target_level)
646 return 0;
647 return 1;
650 /* Given a possible flow dependence temp_rel[j] between source j and the sink
651 * at level sink_level, remove those elements for which
652 * there is an iteration of another source k < j that is closer to the sink.
653 * The flow dependences temp_rel[k] are updated with the improved sources.
654 * Any improved source needs to precede the sink at the same level
655 * and needs to follow source j at the same or a deeper level.
656 * The lower this level, the later the execution date of source k.
657 * We therefore consider lower levels first.
659 * If temp_rel[j] is empty, then there can be no improvement and
660 * we return immediately.
662 static int intermediate_sources(__isl_keep isl_access_info *acc,
663 struct isl_map **temp_rel, int j, int sink_level)
665 int k, level;
666 int depth = 2 * isl_map_dim(acc->source[j].map, isl_dim_in) + 1;
668 if (isl_map_plain_is_empty(temp_rel[j]))
669 return 0;
671 for (k = j - 1; k >= 0; --k) {
672 int plevel, plevel2;
673 plevel = acc->level_before(acc->source[k].data, acc->sink.data);
674 if (!can_precede_at_level(plevel, sink_level))
675 continue;
677 plevel2 = acc->level_before(acc->source[j].data,
678 acc->source[k].data);
680 for (level = sink_level; level <= depth; ++level) {
681 struct isl_map *T;
682 struct isl_set *trest;
683 struct isl_map *copy;
685 if (!can_precede_at_level(plevel2, level))
686 continue;
688 copy = isl_map_copy(temp_rel[j]);
689 T = last_later_source(acc, copy, j, sink_level, k,
690 level, &trest);
691 if (isl_map_plain_is_empty(T)) {
692 isl_set_free(trest);
693 isl_map_free(T);
694 continue;
696 temp_rel[j] = isl_map_intersect_range(temp_rel[j], trest);
697 temp_rel[k] = isl_map_union_disjoint(temp_rel[k], T);
701 return 0;
704 /* Compute all iterations of may source j that precedes the sink at the given
705 * level for sink iterations in set_C.
707 static __isl_give isl_map *all_sources(__isl_keep isl_access_info *acc,
708 __isl_take isl_set *set_C, int j, int level)
710 isl_map *read_map;
711 isl_map *write_map;
712 isl_map *dep_map;
713 isl_map *after;
715 read_map = isl_map_copy(acc->sink.map);
716 read_map = isl_map_intersect_domain(read_map, set_C);
717 write_map = isl_map_copy(acc->source[acc->n_must + j].map);
718 write_map = isl_map_reverse(write_map);
719 dep_map = isl_map_apply_range(read_map, write_map);
720 after = after_at_level(isl_map_get_space(dep_map), level);
721 dep_map = isl_map_intersect(dep_map, after);
723 return isl_map_reverse(dep_map);
726 /* For a given mapping between iterations of must source k and iterations
727 * of the sink, compute the all iteration of may source j preceding
728 * the sink at level before_level for any of the sink iterations,
729 * but following the corresponding iteration of must source k at level
730 * after_level.
732 static __isl_give isl_map *all_later_sources(__isl_keep isl_access_info *acc,
733 __isl_keep isl_map *old_map,
734 int j, int before_level, int k, int after_level)
736 isl_space *dim;
737 isl_set *set_C;
738 isl_map *read_map;
739 isl_map *write_map;
740 isl_map *dep_map;
741 isl_map *after_write;
742 isl_map *before_read;
744 set_C = isl_map_range(isl_map_copy(old_map));
745 read_map = isl_map_copy(acc->sink.map);
746 read_map = isl_map_intersect_domain(read_map, set_C);
747 write_map = isl_map_copy(acc->source[acc->n_must + j].map);
749 write_map = isl_map_reverse(write_map);
750 dep_map = isl_map_apply_range(read_map, write_map);
751 dim = isl_space_join(isl_map_get_space(acc->source[acc->n_must + j].map),
752 isl_space_reverse(isl_map_get_space(acc->source[k].map)));
753 after_write = after_at_level(dim, after_level);
754 after_write = isl_map_apply_range(after_write, old_map);
755 after_write = isl_map_reverse(after_write);
756 dep_map = isl_map_intersect(dep_map, after_write);
757 before_read = after_at_level(isl_map_get_space(dep_map), before_level);
758 dep_map = isl_map_intersect(dep_map, before_read);
759 return isl_map_reverse(dep_map);
762 /* Given the must and may dependence relations for the must accesses
763 * for level sink_level, check if there are any accesses of may access j
764 * that occur in between and return their union.
765 * If some of these accesses are intermediate with respect to
766 * (previously thought to be) must dependences, then these
767 * must dependences are turned into may dependences.
769 static __isl_give isl_map *all_intermediate_sources(
770 __isl_keep isl_access_info *acc, __isl_take isl_map *map,
771 struct isl_map **must_rel, struct isl_map **may_rel,
772 int j, int sink_level)
774 int k, level;
775 int depth = 2 * isl_map_dim(acc->source[acc->n_must + j].map,
776 isl_dim_in) + 1;
778 for (k = 0; k < acc->n_must; ++k) {
779 int plevel;
781 if (isl_map_plain_is_empty(may_rel[k]) &&
782 isl_map_plain_is_empty(must_rel[k]))
783 continue;
785 plevel = acc->level_before(acc->source[k].data,
786 acc->source[acc->n_must + j].data);
788 for (level = sink_level; level <= depth; ++level) {
789 isl_map *T;
790 isl_map *copy;
791 isl_set *ran;
793 if (!can_precede_at_level(plevel, level))
794 continue;
796 copy = isl_map_copy(may_rel[k]);
797 T = all_later_sources(acc, copy, j, sink_level, k, level);
798 map = isl_map_union(map, T);
800 copy = isl_map_copy(must_rel[k]);
801 T = all_later_sources(acc, copy, j, sink_level, k, level);
802 ran = isl_map_range(isl_map_copy(T));
803 map = isl_map_union(map, T);
804 may_rel[k] = isl_map_union_disjoint(may_rel[k],
805 isl_map_intersect_range(isl_map_copy(must_rel[k]),
806 isl_set_copy(ran)));
807 T = isl_map_from_domain_and_range(
808 isl_set_universe(
809 isl_space_domain(isl_map_get_space(must_rel[k]))),
810 ran);
811 must_rel[k] = isl_map_subtract(must_rel[k], T);
815 return map;
818 /* Compute dependences for the case where all accesses are "may"
819 * accesses, which boils down to computing memory based dependences.
820 * The generic algorithm would also work in this case, but it would
821 * be overkill to use it.
823 static __isl_give isl_flow *compute_mem_based_dependences(
824 __isl_keep isl_access_info *acc)
826 int i;
827 isl_set *mustdo;
828 isl_set *maydo;
829 isl_flow *res;
831 res = isl_flow_alloc(acc);
832 if (!res)
833 return NULL;
835 mustdo = isl_map_domain(isl_map_copy(acc->sink.map));
836 maydo = isl_set_copy(mustdo);
838 for (i = 0; i < acc->n_may; ++i) {
839 int plevel;
840 int is_before;
841 isl_space *dim;
842 isl_map *before;
843 isl_map *dep;
845 plevel = acc->level_before(acc->source[i].data, acc->sink.data);
846 is_before = plevel & 1;
847 plevel >>= 1;
849 dim = isl_map_get_space(res->dep[i].map);
850 if (is_before)
851 before = isl_map_lex_le_first(dim, plevel);
852 else
853 before = isl_map_lex_lt_first(dim, plevel);
854 dep = isl_map_apply_range(isl_map_copy(acc->source[i].map),
855 isl_map_reverse(isl_map_copy(acc->sink.map)));
856 dep = isl_map_intersect(dep, before);
857 mustdo = isl_set_subtract(mustdo,
858 isl_map_range(isl_map_copy(dep)));
859 res->dep[i].map = isl_map_union(res->dep[i].map, dep);
862 res->may_no_source = isl_set_subtract(maydo, isl_set_copy(mustdo));
863 res->must_no_source = mustdo;
865 return res;
868 /* Compute dependences for the case where there is at least one
869 * "must" access.
871 * The core algorithm considers all levels in which a source may precede
872 * the sink, where a level may either be a statement level or a loop level.
873 * The outermost statement level is 1, the first loop level is 2, etc...
874 * The algorithm basically does the following:
875 * for all levels l of the read access from innermost to outermost
876 * for all sources w that may precede the sink access at that level
877 * compute the last iteration of the source that precedes the sink access
878 * at that level
879 * add result to possible last accesses at level l of source w
880 * for all sources w2 that we haven't considered yet at this level that may
881 * also precede the sink access
882 * for all levels l2 of w from l to innermost
883 * for all possible last accesses dep of w at l
884 * compute last iteration of w2 between the source and sink
885 * of dep
886 * add result to possible last accesses at level l of write w2
887 * and replace possible last accesses dep by the remainder
890 * The above algorithm is applied to the must access. During the course
891 * of the algorithm, we keep track of sink iterations that still
892 * need to be considered. These iterations are split into those that
893 * haven't been matched to any source access (mustdo) and those that have only
894 * been matched to may accesses (maydo).
895 * At the end of each level, we also consider the may accesses.
896 * In particular, we consider may accesses that precede the remaining
897 * sink iterations, moving elements from mustdo to maydo when appropriate,
898 * and may accesses that occur between a must source and a sink of any
899 * dependences found at the current level, turning must dependences into
900 * may dependences when appropriate.
903 static __isl_give isl_flow *compute_val_based_dependences(
904 __isl_keep isl_access_info *acc)
906 isl_ctx *ctx;
907 isl_flow *res;
908 isl_set *mustdo = NULL;
909 isl_set *maydo = NULL;
910 int level, j;
911 int depth;
912 isl_map **must_rel = NULL;
913 isl_map **may_rel = NULL;
915 if (!acc)
916 return NULL;
918 res = isl_flow_alloc(acc);
919 if (!res)
920 goto error;
921 ctx = isl_map_get_ctx(acc->sink.map);
923 depth = 2 * isl_map_dim(acc->sink.map, isl_dim_in) + 1;
924 mustdo = isl_map_domain(isl_map_copy(acc->sink.map));
925 maydo = isl_set_empty_like(mustdo);
926 if (!mustdo || !maydo)
927 goto error;
928 if (isl_set_plain_is_empty(mustdo))
929 goto done;
931 must_rel = isl_alloc_array(ctx, struct isl_map *, acc->n_must);
932 may_rel = isl_alloc_array(ctx, struct isl_map *, acc->n_must);
933 if (!must_rel || !may_rel)
934 goto error;
936 for (level = depth; level >= 1; --level) {
937 for (j = acc->n_must-1; j >=0; --j) {
938 must_rel[j] = isl_map_empty_like(res->dep[j].map);
939 may_rel[j] = isl_map_copy(must_rel[j]);
942 for (j = acc->n_must - 1; j >= 0; --j) {
943 struct isl_map *T;
944 struct isl_set *rest;
945 int plevel;
947 plevel = acc->level_before(acc->source[j].data,
948 acc->sink.data);
949 if (!can_precede_at_level(plevel, level))
950 continue;
952 T = last_source(acc, mustdo, j, level, &rest);
953 must_rel[j] = isl_map_union_disjoint(must_rel[j], T);
954 mustdo = rest;
956 intermediate_sources(acc, must_rel, j, level);
958 T = last_source(acc, maydo, j, level, &rest);
959 may_rel[j] = isl_map_union_disjoint(may_rel[j], T);
960 maydo = rest;
962 intermediate_sources(acc, may_rel, j, level);
964 if (isl_set_plain_is_empty(mustdo) &&
965 isl_set_plain_is_empty(maydo))
966 break;
968 for (j = j - 1; j >= 0; --j) {
969 int plevel;
971 plevel = acc->level_before(acc->source[j].data,
972 acc->sink.data);
973 if (!can_precede_at_level(plevel, level))
974 continue;
976 intermediate_sources(acc, must_rel, j, level);
977 intermediate_sources(acc, may_rel, j, level);
980 for (j = 0; j < acc->n_may; ++j) {
981 int plevel;
982 isl_map *T;
983 isl_set *ran;
985 plevel = acc->level_before(acc->source[acc->n_must + j].data,
986 acc->sink.data);
987 if (!can_precede_at_level(plevel, level))
988 continue;
990 T = all_sources(acc, isl_set_copy(maydo), j, level);
991 res->dep[2 * acc->n_must + j].map =
992 isl_map_union(res->dep[2 * acc->n_must + j].map, T);
993 T = all_sources(acc, isl_set_copy(mustdo), j, level);
994 ran = isl_map_range(isl_map_copy(T));
995 res->dep[2 * acc->n_must + j].map =
996 isl_map_union(res->dep[2 * acc->n_must + j].map, T);
997 mustdo = isl_set_subtract(mustdo, isl_set_copy(ran));
998 maydo = isl_set_union_disjoint(maydo, ran);
1000 T = res->dep[2 * acc->n_must + j].map;
1001 T = all_intermediate_sources(acc, T, must_rel, may_rel,
1002 j, level);
1003 res->dep[2 * acc->n_must + j].map = T;
1006 for (j = acc->n_must - 1; j >= 0; --j) {
1007 res->dep[2 * j].map =
1008 isl_map_union_disjoint(res->dep[2 * j].map,
1009 must_rel[j]);
1010 res->dep[2 * j + 1].map =
1011 isl_map_union_disjoint(res->dep[2 * j + 1].map,
1012 may_rel[j]);
1015 if (isl_set_plain_is_empty(mustdo) &&
1016 isl_set_plain_is_empty(maydo))
1017 break;
1020 free(must_rel);
1021 free(may_rel);
1022 done:
1023 res->must_no_source = mustdo;
1024 res->may_no_source = maydo;
1025 return res;
1026 error:
1027 isl_flow_free(res);
1028 isl_set_free(mustdo);
1029 isl_set_free(maydo);
1030 free(must_rel);
1031 free(may_rel);
1032 return NULL;
1035 /* Given a "sink" access, a list of n "source" accesses,
1036 * compute for each iteration of the sink access
1037 * and for each element accessed by that iteration,
1038 * the source access in the list that last accessed the
1039 * element accessed by the sink access before this sink access.
1040 * Each access is given as a map from the loop iterators
1041 * to the array indices.
1042 * The result is a list of n relations between source and sink
1043 * iterations and a subset of the domain of the sink access,
1044 * corresponding to those iterations that access an element
1045 * not previously accessed.
1047 * To deal with multi-valued sink access relations, the sink iteration
1048 * domain is first extended with dimensions that correspond to the data
1049 * space. After the computation is finished, these extra dimensions are
1050 * projected out again.
1052 __isl_give isl_flow *isl_access_info_compute_flow(__isl_take isl_access_info *acc)
1054 int j;
1055 struct isl_flow *res = NULL;
1057 if (!acc)
1058 return NULL;
1060 acc->domain_map = isl_map_domain_map(isl_map_copy(acc->sink.map));
1061 acc->sink.map = isl_map_range_map(acc->sink.map);
1062 if (!acc->sink.map)
1063 goto error;
1065 if (acc->n_must == 0)
1066 res = compute_mem_based_dependences(acc);
1067 else {
1068 acc = isl_access_info_sort_sources(acc);
1069 res = compute_val_based_dependences(acc);
1071 if (!res)
1072 goto error;
1074 for (j = 0; j < res->n_source; ++j) {
1075 res->dep[j].map = isl_map_apply_range(res->dep[j].map,
1076 isl_map_copy(acc->domain_map));
1077 if (!res->dep[j].map)
1078 goto error;
1080 if (!res->must_no_source || !res->may_no_source)
1081 goto error;
1083 isl_access_info_free(acc);
1084 return res;
1085 error:
1086 isl_access_info_free(acc);
1087 isl_flow_free(res);
1088 return NULL;
1092 /* Keep track of some information about a schedule for a given
1093 * access. In particular, keep track of which dimensions
1094 * have a constant value and of the actual constant values.
1096 struct isl_sched_info {
1097 int *is_cst;
1098 isl_vec *cst;
1101 static void sched_info_free(__isl_take struct isl_sched_info *info)
1103 if (!info)
1104 return;
1105 isl_vec_free(info->cst);
1106 free(info->is_cst);
1107 free(info);
1110 /* Extract information on the constant dimensions of the schedule
1111 * for a given access. The "map" is of the form
1113 * [S -> D] -> A
1115 * with S the schedule domain, D the iteration domain and A the data domain.
1117 static __isl_give struct isl_sched_info *sched_info_alloc(
1118 __isl_keep isl_map *map)
1120 isl_ctx *ctx;
1121 isl_space *dim;
1122 struct isl_sched_info *info;
1123 int i, n;
1125 if (!map)
1126 return NULL;
1128 dim = isl_space_unwrap(isl_space_domain(isl_map_get_space(map)));
1129 if (!dim)
1130 return NULL;
1131 n = isl_space_dim(dim, isl_dim_in);
1132 isl_space_free(dim);
1134 ctx = isl_map_get_ctx(map);
1135 info = isl_alloc_type(ctx, struct isl_sched_info);
1136 if (!info)
1137 return NULL;
1138 info->is_cst = isl_alloc_array(ctx, int, n);
1139 info->cst = isl_vec_alloc(ctx, n);
1140 if (!info->is_cst || !info->cst)
1141 goto error;
1143 for (i = 0; i < n; ++i)
1144 info->is_cst[i] = isl_map_plain_is_fixed(map, isl_dim_in, i,
1145 &info->cst->el[i]);
1147 return info;
1148 error:
1149 sched_info_free(info);
1150 return NULL;
1153 struct isl_compute_flow_data {
1154 isl_union_map *must_source;
1155 isl_union_map *may_source;
1156 isl_union_map *must_dep;
1157 isl_union_map *may_dep;
1158 isl_union_map *must_no_source;
1159 isl_union_map *may_no_source;
1161 int count;
1162 int must;
1163 isl_space *dim;
1164 struct isl_sched_info *sink_info;
1165 struct isl_sched_info **source_info;
1166 isl_access_info *accesses;
1169 static int count_matching_array(__isl_take isl_map *map, void *user)
1171 int eq;
1172 isl_space *dim;
1173 struct isl_compute_flow_data *data;
1175 data = (struct isl_compute_flow_data *)user;
1177 dim = isl_space_range(isl_map_get_space(map));
1179 eq = isl_space_is_equal(dim, data->dim);
1181 isl_space_free(dim);
1182 isl_map_free(map);
1184 if (eq < 0)
1185 return -1;
1186 if (eq)
1187 data->count++;
1189 return 0;
1192 static int collect_matching_array(__isl_take isl_map *map, void *user)
1194 int eq;
1195 isl_space *dim;
1196 struct isl_sched_info *info;
1197 struct isl_compute_flow_data *data;
1199 data = (struct isl_compute_flow_data *)user;
1201 dim = isl_space_range(isl_map_get_space(map));
1203 eq = isl_space_is_equal(dim, data->dim);
1205 isl_space_free(dim);
1207 if (eq < 0)
1208 goto error;
1209 if (!eq) {
1210 isl_map_free(map);
1211 return 0;
1214 info = sched_info_alloc(map);
1215 data->source_info[data->count] = info;
1217 data->accesses = isl_access_info_add_source(data->accesses,
1218 map, data->must, info);
1220 data->count++;
1222 return 0;
1223 error:
1224 isl_map_free(map);
1225 return -1;
1228 /* Determine the shared nesting level and the "textual order" of
1229 * the given accesses.
1231 * We first determine the minimal schedule dimension for both accesses.
1233 * If among those dimensions, we can find one where both have a fixed
1234 * value and if moreover those values are different, then the previous
1235 * dimension is the last shared nesting level and the textual order
1236 * is determined based on the order of the fixed values.
1237 * If no such fixed values can be found, then we set the shared
1238 * nesting level to the minimal schedule dimension, with no textual ordering.
1240 static int before(void *first, void *second)
1242 struct isl_sched_info *info1 = first;
1243 struct isl_sched_info *info2 = second;
1244 int n1, n2;
1245 int i;
1247 n1 = info1->cst->size;
1248 n2 = info2->cst->size;
1250 if (n2 < n1)
1251 n1 = n2;
1253 for (i = 0; i < n1; ++i) {
1254 if (!info1->is_cst[i])
1255 continue;
1256 if (!info2->is_cst[i])
1257 continue;
1258 if (isl_int_eq(info1->cst->el[i], info2->cst->el[i]))
1259 continue;
1260 return 2 * i + isl_int_lt(info1->cst->el[i], info2->cst->el[i]);
1263 return 2 * n1;
1266 /* Given a sink access, look for all the source accesses that access
1267 * the same array and perform dataflow analysis on them using
1268 * isl_access_info_compute_flow.
1270 static int compute_flow(__isl_take isl_map *map, void *user)
1272 int i;
1273 isl_ctx *ctx;
1274 struct isl_compute_flow_data *data;
1275 isl_flow *flow;
1277 data = (struct isl_compute_flow_data *)user;
1279 ctx = isl_map_get_ctx(map);
1281 data->accesses = NULL;
1282 data->sink_info = NULL;
1283 data->source_info = NULL;
1284 data->count = 0;
1285 data->dim = isl_space_range(isl_map_get_space(map));
1287 if (isl_union_map_foreach_map(data->must_source,
1288 &count_matching_array, data) < 0)
1289 goto error;
1290 if (isl_union_map_foreach_map(data->may_source,
1291 &count_matching_array, data) < 0)
1292 goto error;
1294 data->sink_info = sched_info_alloc(map);
1295 data->source_info = isl_calloc_array(ctx, struct isl_sched_info *,
1296 data->count);
1298 data->accesses = isl_access_info_alloc(isl_map_copy(map),
1299 data->sink_info, &before, data->count);
1300 if (!data->sink_info || !data->source_info || !data->accesses)
1301 goto error;
1302 data->count = 0;
1303 data->must = 1;
1304 if (isl_union_map_foreach_map(data->must_source,
1305 &collect_matching_array, data) < 0)
1306 goto error;
1307 data->must = 0;
1308 if (isl_union_map_foreach_map(data->may_source,
1309 &collect_matching_array, data) < 0)
1310 goto error;
1312 flow = isl_access_info_compute_flow(data->accesses);
1313 data->accesses = NULL;
1315 if (!flow)
1316 goto error;
1318 data->must_no_source = isl_union_map_union(data->must_no_source,
1319 isl_union_map_from_map(isl_flow_get_no_source(flow, 1)));
1320 data->may_no_source = isl_union_map_union(data->may_no_source,
1321 isl_union_map_from_map(isl_flow_get_no_source(flow, 0)));
1323 for (i = 0; i < flow->n_source; ++i) {
1324 isl_union_map *dep;
1325 dep = isl_union_map_from_map(isl_map_copy(flow->dep[i].map));
1326 if (flow->dep[i].must)
1327 data->must_dep = isl_union_map_union(data->must_dep, dep);
1328 else
1329 data->may_dep = isl_union_map_union(data->may_dep, dep);
1332 isl_flow_free(flow);
1334 sched_info_free(data->sink_info);
1335 if (data->source_info) {
1336 for (i = 0; i < data->count; ++i)
1337 sched_info_free(data->source_info[i]);
1338 free(data->source_info);
1340 isl_space_free(data->dim);
1341 isl_map_free(map);
1343 return 0;
1344 error:
1345 isl_access_info_free(data->accesses);
1346 sched_info_free(data->sink_info);
1347 if (data->source_info) {
1348 for (i = 0; i < data->count; ++i)
1349 sched_info_free(data->source_info[i]);
1350 free(data->source_info);
1352 isl_space_free(data->dim);
1353 isl_map_free(map);
1355 return -1;
1358 /* Given a collection of "sink" and "source" accesses,
1359 * compute for each iteration of a sink access
1360 * and for each element accessed by that iteration,
1361 * the source access in the list that last accessed the
1362 * element accessed by the sink access before this sink access.
1363 * Each access is given as a map from the loop iterators
1364 * to the array indices.
1365 * The result is a relations between source and sink
1366 * iterations and a subset of the domain of the sink accesses,
1367 * corresponding to those iterations that access an element
1368 * not previously accessed.
1370 * We first prepend the schedule dimensions to the domain
1371 * of the accesses so that we can easily compare their relative order.
1372 * Then we consider each sink access individually in compute_flow.
1374 int isl_union_map_compute_flow(__isl_take isl_union_map *sink,
1375 __isl_take isl_union_map *must_source,
1376 __isl_take isl_union_map *may_source,
1377 __isl_take isl_union_map *schedule,
1378 __isl_give isl_union_map **must_dep, __isl_give isl_union_map **may_dep,
1379 __isl_give isl_union_map **must_no_source,
1380 __isl_give isl_union_map **may_no_source)
1382 isl_space *dim;
1383 isl_union_map *range_map = NULL;
1384 struct isl_compute_flow_data data;
1386 sink = isl_union_map_align_params(sink,
1387 isl_union_map_get_space(must_source));
1388 sink = isl_union_map_align_params(sink,
1389 isl_union_map_get_space(may_source));
1390 sink = isl_union_map_align_params(sink,
1391 isl_union_map_get_space(schedule));
1392 dim = isl_union_map_get_space(sink);
1393 must_source = isl_union_map_align_params(must_source, isl_space_copy(dim));
1394 may_source = isl_union_map_align_params(may_source, isl_space_copy(dim));
1395 schedule = isl_union_map_align_params(schedule, isl_space_copy(dim));
1397 schedule = isl_union_map_reverse(schedule);
1398 range_map = isl_union_map_range_map(schedule);
1399 schedule = isl_union_map_reverse(isl_union_map_copy(range_map));
1400 sink = isl_union_map_apply_domain(sink, isl_union_map_copy(schedule));
1401 must_source = isl_union_map_apply_domain(must_source,
1402 isl_union_map_copy(schedule));
1403 may_source = isl_union_map_apply_domain(may_source, schedule);
1405 data.must_source = must_source;
1406 data.may_source = may_source;
1407 data.must_dep = must_dep ?
1408 isl_union_map_empty(isl_space_copy(dim)) : NULL;
1409 data.may_dep = may_dep ? isl_union_map_empty(isl_space_copy(dim)) : NULL;
1410 data.must_no_source = must_no_source ?
1411 isl_union_map_empty(isl_space_copy(dim)) : NULL;
1412 data.may_no_source = may_no_source ?
1413 isl_union_map_empty(isl_space_copy(dim)) : NULL;
1415 isl_space_free(dim);
1417 if (isl_union_map_foreach_map(sink, &compute_flow, &data) < 0)
1418 goto error;
1420 isl_union_map_free(sink);
1421 isl_union_map_free(must_source);
1422 isl_union_map_free(may_source);
1424 if (must_dep) {
1425 data.must_dep = isl_union_map_apply_domain(data.must_dep,
1426 isl_union_map_copy(range_map));
1427 data.must_dep = isl_union_map_apply_range(data.must_dep,
1428 isl_union_map_copy(range_map));
1429 *must_dep = data.must_dep;
1431 if (may_dep) {
1432 data.may_dep = isl_union_map_apply_domain(data.may_dep,
1433 isl_union_map_copy(range_map));
1434 data.may_dep = isl_union_map_apply_range(data.may_dep,
1435 isl_union_map_copy(range_map));
1436 *may_dep = data.may_dep;
1438 if (must_no_source) {
1439 data.must_no_source = isl_union_map_apply_domain(
1440 data.must_no_source, isl_union_map_copy(range_map));
1441 *must_no_source = data.must_no_source;
1443 if (may_no_source) {
1444 data.may_no_source = isl_union_map_apply_domain(
1445 data.may_no_source, isl_union_map_copy(range_map));
1446 *may_no_source = data.may_no_source;
1449 isl_union_map_free(range_map);
1451 return 0;
1452 error:
1453 isl_union_map_free(range_map);
1454 isl_union_map_free(sink);
1455 isl_union_map_free(must_source);
1456 isl_union_map_free(may_source);
1457 isl_union_map_free(data.must_dep);
1458 isl_union_map_free(data.may_dep);
1459 isl_union_map_free(data.must_no_source);
1460 isl_union_map_free(data.may_no_source);
1462 if (must_dep)
1463 *must_dep = NULL;
1464 if (may_dep)
1465 *may_dep = NULL;
1466 if (must_no_source)
1467 *must_no_source = NULL;
1468 if (may_no_source)
1469 *may_no_source = NULL;
1470 return -1;