isl_test.c: map_is_equal: check input
[isl.git] / isl_flow.c
blob84a48115eb55351bca075e6fdd7cffbed6150cd2
1 /*
2 * Copyright 2005-2007 Universiteit Leiden
3 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Copyright 2010 INRIA Saclay
6 * Use of this software is governed by the GNU LGPLv2.1 license
8 * Written by Sven Verdoolaege, Leiden Institute of Advanced Computer Science,
9 * Universiteit Leiden, Niels Bohrweg 1, 2333 CA Leiden, The Netherlands
10 * and K.U.Leuven, Departement Computerwetenschappen, Celestijnenlaan 200A,
11 * B-3001 Leuven, Belgium
12 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
13 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
16 #include <isl_flow.h>
18 /* A private structure to keep track of a mapping together with
19 * a user-specified identifier and a boolean indicating whether
20 * the map represents a must or may access/dependence.
22 struct isl_labeled_map {
23 struct isl_map *map;
24 void *data;
25 int must;
28 /* A structure containing the input for dependence analysis:
29 * - a sink
30 * - n_must + n_may (<= max_source) sources
31 * - a function for determining the relative order of sources and sink
32 * The must sources are placed before the may sources.
34 struct isl_access_info {
35 struct isl_labeled_map sink;
36 isl_access_level_before level_before;
37 int max_source;
38 int n_must;
39 int n_may;
40 struct isl_labeled_map source[1];
43 /* A structure containing the output of dependence analysis:
44 * - n_source dependences
45 * - a subset of the sink for which definitely no source could be found
46 * - a subset of the sink for which possibly no source could be found
48 struct isl_flow {
49 isl_set *must_no_source;
50 isl_set *may_no_source;
51 int n_source;
52 struct isl_labeled_map *dep;
55 /* Construct an isl_access_info structure and fill it up with
56 * the given data. The number of sources is set to 0.
58 __isl_give isl_access_info *isl_access_info_alloc(__isl_take isl_map *sink,
59 void *sink_user, isl_access_level_before fn, int max_source)
61 struct isl_access_info *acc;
63 if (!sink)
64 return NULL;
66 isl_assert(sink->ctx, max_source >= 0, goto error);
68 acc = isl_alloc(sink->ctx, struct isl_access_info,
69 sizeof(struct isl_access_info) +
70 (max_source - 1) * sizeof(struct isl_labeled_map));
71 if (!acc)
72 goto error;
74 acc->sink.map = sink;
75 acc->sink.data = sink_user;
76 acc->level_before = fn;
77 acc->max_source = max_source;
78 acc->n_must = 0;
79 acc->n_may = 0;
81 return acc;
82 error:
83 isl_map_free(sink);
84 return NULL;
87 /* Free the given isl_access_info structure.
88 * This function is static because the user is expected to call
89 * isl_access_info_compute_flow on any isl_access_info structure
90 * he creates.
92 static void isl_access_info_free(__isl_take isl_access_info *acc)
94 int i;
96 if (!acc)
97 return;
98 isl_map_free(acc->sink.map);
99 for (i = 0; i < acc->n_must + acc->n_may; ++i)
100 isl_map_free(acc->source[i].map);
101 free(acc);
104 /* Add another source to an isl_access_info structure, making
105 * sure the "must" sources are placed before the "may" sources.
106 * This function may be called at most max_source times on a
107 * given isl_access_info structure, with max_source as specified
108 * in the call to isl_access_info_alloc that constructed the structure.
110 __isl_give isl_access_info *isl_access_info_add_source(
111 __isl_take isl_access_info *acc, __isl_take isl_map *source,
112 int must, void *source_user)
114 if (!acc)
115 return NULL;
116 isl_assert(acc->sink.map->ctx,
117 acc->n_must + acc->n_may < acc->max_source, goto error);
119 if (must) {
120 if (acc->n_may)
121 acc->source[acc->n_must + acc->n_may] =
122 acc->source[acc->n_must];
123 acc->source[acc->n_must].map = source;
124 acc->source[acc->n_must].data = source_user;
125 acc->source[acc->n_must].must = 1;
126 acc->n_must++;
127 } else {
128 acc->source[acc->n_must + acc->n_may].map = source;
129 acc->source[acc->n_must + acc->n_may].data = source_user;
130 acc->source[acc->n_must + acc->n_may].must = 0;
131 acc->n_may++;
134 return acc;
135 error:
136 isl_map_free(source);
137 isl_access_info_free(acc);
138 return NULL;
141 /* A temporary structure used while sorting the accesses in an isl_access_info.
143 struct isl_access_sort_info {
144 struct isl_map *source_map;
145 void *source_data;
146 struct isl_access_info *acc;
149 /* Return -n, 0 or n (with n a positive value), depending on whether
150 * the source access identified by p1 should be sorted before, together
151 * or after that identified by p2.
153 * If p1 and p2 share a different number of levels with the sink,
154 * then the one with the lowest number of shared levels should be
155 * sorted first.
156 * If they both share no levels, then the order is irrelevant.
157 * Otherwise, if p1 appears before p2, then it should be sorted first.
159 static int access_sort_cmp(const void *p1, const void *p2)
161 const struct isl_access_sort_info *i1, *i2;
162 int level1, level2;
163 i1 = (const struct isl_access_sort_info *) p1;
164 i2 = (const struct isl_access_sort_info *) p2;
166 level1 = i1->acc->level_before(i1->source_data, i1->acc->sink.data);
167 level2 = i2->acc->level_before(i2->source_data, i2->acc->sink.data);
169 if (level1 != level2 || !level1)
170 return level1 - level2;
172 level1 = i1->acc->level_before(i1->source_data, i2->source_data);
174 return (level1 % 2) ? -1 : 1;
177 /* Sort the must source accesses in order of increasing number of shared
178 * levels with the sink access.
179 * Source accesses with the same number of shared levels are sorted
180 * in their textual order.
182 static __isl_give isl_access_info *isl_access_info_sort_sources(
183 __isl_take isl_access_info *acc)
185 int i;
186 struct isl_access_sort_info *array;
188 if (!acc)
189 return NULL;
190 if (acc->n_must <= 1)
191 return acc;
193 array = isl_alloc_array(acc->sink.map->ctx,
194 struct isl_access_sort_info, acc->n_must);
195 if (!array)
196 goto error;
198 for (i = 0; i < acc->n_must; ++i) {
199 array[i].source_map = acc->source[i].map;
200 array[i].source_data = acc->source[i].data;
201 array[i].acc = acc;
204 qsort(array, acc->n_must, sizeof(struct isl_access_sort_info),
205 access_sort_cmp);
207 for (i = 0; i < acc->n_must; ++i) {
208 acc->source[i].map = array[i].source_map;
209 acc->source[i].data = array[i].source_data;
212 free(array);
214 return acc;
215 error:
216 isl_access_info_free(acc);
217 return NULL;
220 /* Initialize an empty isl_flow structure corresponding to a given
221 * isl_access_info structure.
222 * For each must access, two dependences are created (initialized
223 * to the empty relation), one for the resulting must dependences
224 * and one for the resulting may dependences. May accesses can
225 * only lead to may dependences, so only one dependence is created
226 * for each of them.
227 * This function is private as isl_flow structures are only supposed
228 * to be created by isl_access_info_compute_flow.
230 static __isl_give isl_flow *isl_flow_alloc(__isl_keep isl_access_info *acc)
232 int i;
233 struct isl_ctx *ctx;
234 struct isl_flow *dep;
236 if (!acc)
237 return NULL;
239 ctx = acc->sink.map->ctx;
240 dep = isl_calloc_type(ctx, struct isl_flow);
241 if (!dep)
242 return NULL;
244 dep->dep = isl_alloc_array(ctx, struct isl_labeled_map,
245 2 * acc->n_must + acc->n_may);
246 if (!dep->dep)
247 goto error;
249 dep->n_source = 2 * acc->n_must + acc->n_may;
250 for (i = 0; i < acc->n_must; ++i) {
251 struct isl_dim *dim;
252 dim = isl_dim_join(isl_dim_copy(acc->source[i].map->dim),
253 isl_dim_reverse(isl_dim_copy(acc->sink.map->dim)));
254 dep->dep[2 * i].map = isl_map_empty(dim);
255 dep->dep[2 * i + 1].map = isl_map_copy(dep->dep[2 * i].map);
256 dep->dep[2 * i].data = acc->source[i].data;
257 dep->dep[2 * i + 1].data = acc->source[i].data;
258 dep->dep[2 * i].must = 1;
259 dep->dep[2 * i + 1].must = 0;
261 for (i = acc->n_must; i < acc->n_must + acc->n_may; ++i) {
262 struct isl_dim *dim;
263 dim = isl_dim_join(isl_dim_copy(acc->source[i].map->dim),
264 isl_dim_reverse(isl_dim_copy(acc->sink.map->dim)));
265 dep->dep[acc->n_must + i].map = isl_map_empty(dim);
266 dep->dep[acc->n_must + i].data = acc->source[i].data;
267 dep->dep[acc->n_must + i].must = 0;
270 return dep;
271 error:
272 isl_flow_free(dep);
273 return NULL;
276 /* Iterate over all sources and for each resulting flow dependence
277 * that is not empty, call the user specfied function.
278 * The second argument in this function call identifies the source,
279 * while the third argument correspond to the final argument of
280 * the isl_flow_foreach call.
282 int isl_flow_foreach(__isl_keep isl_flow *deps,
283 int (*fn)(__isl_take isl_map *dep, int must, void *dep_user, void *user),
284 void *user)
286 int i;
288 if (!deps)
289 return -1;
291 for (i = 0; i < deps->n_source; ++i) {
292 if (isl_map_fast_is_empty(deps->dep[i].map))
293 continue;
294 if (fn(isl_map_copy(deps->dep[i].map), deps->dep[i].must,
295 deps->dep[i].data, user) < 0)
296 return -1;
299 return 0;
302 /* Return a copy of the subset of the sink for which no source could be found.
304 __isl_give isl_set *isl_flow_get_no_source(__isl_keep isl_flow *deps, int must)
306 if (!deps)
307 return NULL;
309 if (must)
310 return isl_set_copy(deps->must_no_source);
311 else
312 return isl_set_copy(deps->may_no_source);
315 void isl_flow_free(__isl_take isl_flow *deps)
317 int i;
319 if (!deps)
320 return;
321 isl_set_free(deps->must_no_source);
322 isl_set_free(deps->may_no_source);
323 if (deps->dep) {
324 for (i = 0; i < deps->n_source; ++i)
325 isl_map_free(deps->dep[i].map);
326 free(deps->dep);
328 free(deps);
331 /* Return a map that enforces that the domain iteration occurs after
332 * the range iteration at the given level.
333 * If level is odd, then the domain iteration should occur after
334 * the target iteration in their shared level/2 outermost loops.
335 * In this case we simply need to enforce that these outermost
336 * loop iterations are the same.
337 * If level is even, then the loop iterator of the domain should
338 * be greater than the loop iterator of the range at the last
339 * of the level/2 shared loops, i.e., loop level/2 - 1.
341 static __isl_give isl_map *after_at_level(struct isl_dim *dim, int level)
343 struct isl_basic_map *bmap;
345 if (level % 2)
346 bmap = isl_basic_map_equal(dim, level/2);
347 else
348 bmap = isl_basic_map_more_at(dim, level/2 - 1);
350 return isl_map_from_basic_map(bmap);
353 /* Compute the last iteration of must source j that precedes the sink
354 * at the given level for sink iterations in set_C.
355 * The subset of set_C for which no such iteration can be found is returned
356 * in *empty.
358 static struct isl_map *last_source(struct isl_access_info *acc,
359 struct isl_set *set_C,
360 int j, int level, struct isl_set **empty)
362 struct isl_map *read_map;
363 struct isl_map *write_map;
364 struct isl_map *dep_map;
365 struct isl_map *after;
366 struct isl_map *result;
368 read_map = isl_map_copy(acc->sink.map);
369 write_map = isl_map_copy(acc->source[j].map);
370 write_map = isl_map_reverse(write_map);
371 dep_map = isl_map_apply_range(read_map, write_map);
372 after = after_at_level(isl_dim_copy(dep_map->dim), level);
373 dep_map = isl_map_intersect(dep_map, after);
374 result = isl_map_partial_lexmax(dep_map, set_C, empty);
375 result = isl_map_reverse(result);
377 return result;
380 /* For a given mapping between iterations of must source j and iterations
381 * of the sink, compute the last iteration of must source k preceding
382 * the sink at level before_level for any of the sink iterations,
383 * but following the corresponding iteration of must source j at level
384 * after_level.
386 static struct isl_map *last_later_source(struct isl_access_info *acc,
387 struct isl_map *old_map,
388 int j, int before_level,
389 int k, int after_level,
390 struct isl_set **empty)
392 struct isl_dim *dim;
393 struct isl_set *set_C;
394 struct isl_map *read_map;
395 struct isl_map *write_map;
396 struct isl_map *dep_map;
397 struct isl_map *after_write;
398 struct isl_map *before_read;
399 struct isl_map *result;
401 set_C = isl_map_range(isl_map_copy(old_map));
402 read_map = isl_map_copy(acc->sink.map);
403 write_map = isl_map_copy(acc->source[k].map);
405 write_map = isl_map_reverse(write_map);
406 dep_map = isl_map_apply_range(read_map, write_map);
407 dim = isl_dim_join(isl_dim_copy(acc->source[k].map->dim),
408 isl_dim_reverse(isl_dim_copy(acc->source[j].map->dim)));
409 after_write = after_at_level(dim, after_level);
410 after_write = isl_map_apply_range(after_write, old_map);
411 after_write = isl_map_reverse(after_write);
412 dep_map = isl_map_intersect(dep_map, after_write);
413 before_read = after_at_level(isl_dim_copy(dep_map->dim), before_level);
414 dep_map = isl_map_intersect(dep_map, before_read);
415 result = isl_map_partial_lexmax(dep_map, set_C, empty);
416 result = isl_map_reverse(result);
418 return result;
421 /* Given a shared_level between two accesses, return 1 if the
422 * the first can precede the second at the requested target_level.
423 * If the target level is odd, i.e., refers to a statement level
424 * dimension, then first needs to precede second at the requested
425 * level, i.e., shared_level must be equal to target_level.
426 * If the target level is odd, then the two loops should share
427 * at least the requested number of outer loops.
429 static int can_precede_at_level(int shared_level, int target_level)
431 if (shared_level < target_level)
432 return 0;
433 if ((target_level % 2) && shared_level > target_level)
434 return 0;
435 return 1;
438 /* Given a possible flow dependence temp_rel[j] between source j and the sink
439 * at level sink_level, remove those elements for which
440 * there is an iteration of another source k < j that is closer to the sink.
441 * The flow dependences temp_rel[k] are updated with the improved sources.
442 * Any improved source needs to precede the sink at the same level
443 * and needs to follow source j at the same or a deeper level.
444 * The lower this level, the later the execution date of source k.
445 * We therefore consider lower levels first.
447 * If temp_rel[j] is empty, then there can be no improvement and
448 * we return immediately.
450 static int intermediate_sources(__isl_keep isl_access_info *acc,
451 struct isl_map **temp_rel, int j, int sink_level)
453 int k, level;
454 int depth = 2 * isl_map_dim(acc->source[j].map, isl_dim_in) + 1;
456 if (isl_map_fast_is_empty(temp_rel[j]))
457 return 0;
459 for (k = j - 1; k >= 0; --k) {
460 int plevel, plevel2;
461 plevel = acc->level_before(acc->source[k].data, acc->sink.data);
462 if (!can_precede_at_level(plevel, sink_level))
463 continue;
465 plevel2 = acc->level_before(acc->source[j].data,
466 acc->source[k].data);
468 for (level = sink_level; level <= depth; ++level) {
469 struct isl_map *T;
470 struct isl_set *trest;
471 struct isl_map *copy;
473 if (!can_precede_at_level(plevel2, level))
474 continue;
476 copy = isl_map_copy(temp_rel[j]);
477 T = last_later_source(acc, copy, j, sink_level, k,
478 level, &trest);
479 if (isl_map_fast_is_empty(T)) {
480 isl_set_free(trest);
481 isl_map_free(T);
482 continue;
484 temp_rel[j] = isl_map_intersect_range(temp_rel[j], trest);
485 temp_rel[k] = isl_map_union_disjoint(temp_rel[k], T);
489 return 0;
492 /* Compute all iterations of may source j that precedes the sink at the given
493 * level for sink iterations in set_C.
495 static __isl_give isl_map *all_sources(__isl_keep isl_access_info *acc,
496 __isl_take isl_set *set_C, int j, int level)
498 isl_map *read_map;
499 isl_map *write_map;
500 isl_map *dep_map;
501 isl_map *after;
503 read_map = isl_map_copy(acc->sink.map);
504 read_map = isl_map_intersect_domain(read_map, set_C);
505 write_map = isl_map_copy(acc->source[acc->n_must + j].map);
506 write_map = isl_map_reverse(write_map);
507 dep_map = isl_map_apply_range(read_map, write_map);
508 after = after_at_level(isl_dim_copy(dep_map->dim), level);
509 dep_map = isl_map_intersect(dep_map, after);
511 return isl_map_reverse(dep_map);
514 /* For a given mapping between iterations of must source k and iterations
515 * of the sink, compute the all iteration of may source j preceding
516 * the sink at level before_level for any of the sink iterations,
517 * but following the corresponding iteration of must source k at level
518 * after_level.
520 static __isl_give isl_map *all_later_sources(__isl_keep isl_access_info *acc,
521 __isl_keep isl_map *old_map,
522 int j, int before_level, int k, int after_level)
524 isl_dim *dim;
525 isl_set *set_C;
526 isl_map *read_map;
527 isl_map *write_map;
528 isl_map *dep_map;
529 isl_map *after_write;
530 isl_map *before_read;
532 set_C = isl_map_range(isl_map_copy(old_map));
533 read_map = isl_map_copy(acc->sink.map);
534 read_map = isl_map_intersect_domain(read_map, set_C);
535 write_map = isl_map_copy(acc->source[acc->n_must + j].map);
537 write_map = isl_map_reverse(write_map);
538 dep_map = isl_map_apply_range(read_map, write_map);
539 dim = isl_dim_join(isl_dim_copy(acc->source[acc->n_must + j].map->dim),
540 isl_dim_reverse(isl_dim_copy(acc->source[k].map->dim)));
541 after_write = after_at_level(dim, after_level);
542 after_write = isl_map_apply_range(after_write, old_map);
543 after_write = isl_map_reverse(after_write);
544 dep_map = isl_map_intersect(dep_map, after_write);
545 before_read = after_at_level(isl_dim_copy(dep_map->dim), before_level);
546 dep_map = isl_map_intersect(dep_map, before_read);
547 return isl_map_reverse(dep_map);
550 /* Given the must and may dependence relations for the must accesses
551 * for level sink_level, check if there are any accesses of may access j
552 * that occur in between and return their union.
553 * If some of these accesses are intermediate with respect to
554 * (previously thought to be) must dependences, then these
555 * must dependences are turned into may dependences.
557 static __isl_give isl_map *all_intermediate_sources(
558 __isl_keep isl_access_info *acc, __isl_take isl_map *map,
559 struct isl_map **must_rel, struct isl_map **may_rel,
560 int j, int sink_level)
562 int k, level;
563 int depth = 2 * isl_map_dim(acc->source[acc->n_must + j].map,
564 isl_dim_in) + 1;
566 for (k = 0; k < acc->n_must; ++k) {
567 int plevel;
569 if (isl_map_fast_is_empty(may_rel[k]) &&
570 isl_map_fast_is_empty(must_rel[k]))
571 continue;
573 plevel = acc->level_before(acc->source[k].data,
574 acc->source[acc->n_must + j].data);
576 for (level = sink_level; level <= depth; ++level) {
577 isl_map *T;
578 isl_map *copy;
579 isl_set *ran;
581 if (!can_precede_at_level(plevel, level))
582 continue;
584 copy = isl_map_copy(may_rel[k]);
585 T = all_later_sources(acc, copy, j, sink_level, k, level);
586 map = isl_map_union(map, T);
588 copy = isl_map_copy(must_rel[k]);
589 T = all_later_sources(acc, copy, j, sink_level, k, level);
590 ran = isl_map_range(isl_map_copy(T));
591 map = isl_map_union(map, T);
592 may_rel[k] = isl_map_union_disjoint(may_rel[k],
593 isl_map_intersect_range(isl_map_copy(must_rel[k]),
594 isl_set_copy(ran)));
595 T = isl_map_from_domain_and_range(
596 isl_set_universe(
597 isl_dim_domain(isl_map_get_dim(must_rel[k]))),
598 ran);
599 must_rel[k] = isl_map_subtract(must_rel[k], T);
603 return map;
606 /* Compute dependences for the case where all accesses are "may"
607 * accesses, which boils down to computing memory based dependences.
608 * The generic algorithm would also work in this case, but it would
609 * be overkill to use it.
611 static __isl_give isl_flow *compute_mem_based_dependences(
612 __isl_take isl_access_info *acc)
614 int i;
615 isl_set *mustdo;
616 isl_set *maydo;
617 isl_flow *res;
619 res = isl_flow_alloc(acc);
620 if (!res)
621 goto error;
623 mustdo = isl_map_domain(isl_map_copy(acc->sink.map));
624 maydo = isl_set_copy(mustdo);
626 for (i = 0; i < acc->n_may; ++i) {
627 int plevel;
628 int is_before;
629 isl_dim *dim;
630 isl_map *before;
631 isl_map *dep;
633 plevel = acc->level_before(acc->source[i].data, acc->sink.data);
634 is_before = plevel & 1;
635 plevel >>= 1;
637 dim = isl_map_get_dim(res->dep[i].map);
638 if (is_before)
639 before = isl_map_lex_le_first(dim, plevel);
640 else
641 before = isl_map_lex_lt_first(dim, plevel);
642 dep = isl_map_apply_range(isl_map_copy(acc->source[i].map),
643 isl_map_reverse(isl_map_copy(acc->sink.map)));
644 dep = isl_map_intersect(dep, before);
645 mustdo = isl_set_subtract(mustdo,
646 isl_map_range(isl_map_copy(dep)));
647 res->dep[i].map = isl_map_union(res->dep[i].map, dep);
650 res->may_no_source = isl_set_subtract(maydo, isl_set_copy(mustdo));
651 res->must_no_source = mustdo;
653 isl_access_info_free(acc);
655 return res;
656 error:
657 isl_access_info_free(acc);
658 return NULL;
661 /* Compute dependences for the case where there is at least one
662 * "must" access.
664 * The core algorithm considers all levels in which a source may precede
665 * the sink, where a level may either be a statement level or a loop level.
666 * The outermost statement level is 1, the first loop level is 2, etc...
667 * The algorithm basically does the following:
668 * for all levels l of the read access from innermost to outermost
669 * for all sources w that may precede the sink access at that level
670 * compute the last iteration of the source that precedes the sink access
671 * at that level
672 * add result to possible last accesses at level l of source w
673 * for all sources w2 that we haven't considered yet at this level that may
674 * also precede the sink access
675 * for all levels l2 of w from l to innermost
676 * for all possible last accesses dep of w at l
677 * compute last iteration of w2 between the source and sink
678 * of dep
679 * add result to possible last accesses at level l of write w2
680 * and replace possible last accesses dep by the remainder
683 * The above algorithm is applied to the must access. During the course
684 * of the algorithm, we keep track of sink iterations that still
685 * need to be considered. These iterations are split into those that
686 * haven't been matched to any source access (mustdo) and those that have only
687 * been matched to may accesses (maydo).
688 * At the end of each level, we also consider the may accesses.
689 * In particular, we consider may accesses that precede the remaining
690 * sink iterations, moving elements from mustdo to maydo when appropriate,
691 * and may accesses that occur between a must source and a sink of any
692 * dependences found at the current level, turning must dependences into
693 * may dependences when appropriate.
696 static __isl_give isl_flow *compute_val_based_dependences(
697 __isl_take isl_access_info *acc)
699 isl_ctx *ctx;
700 isl_flow *res;
701 isl_set *mustdo;
702 isl_set *maydo;
703 int level, j;
704 int depth;
705 isl_map **must_rel;
706 isl_map **may_rel;
708 acc = isl_access_info_sort_sources(acc);
709 if (!acc)
710 return NULL;
712 res = isl_flow_alloc(acc);
713 if (!res)
714 goto error;
715 ctx = acc->sink.map->ctx;
717 depth = 2 * isl_map_dim(acc->sink.map, isl_dim_in) + 1;
718 mustdo = isl_map_domain(isl_map_copy(acc->sink.map));
719 maydo = isl_set_empty_like(mustdo);
720 if (isl_set_fast_is_empty(mustdo))
721 goto done;
723 must_rel = isl_alloc_array(ctx, struct isl_map *, acc->n_must);
724 may_rel = isl_alloc_array(ctx, struct isl_map *, acc->n_must);
726 for (level = depth; level >= 1; --level) {
727 for (j = acc->n_must-1; j >=0; --j) {
728 must_rel[j] = isl_map_empty_like(res->dep[j].map);
729 may_rel[j] = isl_map_copy(must_rel[j]);
732 for (j = acc->n_must - 1; j >= 0; --j) {
733 struct isl_map *T;
734 struct isl_set *rest;
735 int plevel;
737 plevel = acc->level_before(acc->source[j].data,
738 acc->sink.data);
739 if (!can_precede_at_level(plevel, level))
740 continue;
742 T = last_source(acc, mustdo, j, level, &rest);
743 must_rel[j] = isl_map_union_disjoint(must_rel[j], T);
744 mustdo = rest;
746 intermediate_sources(acc, must_rel, j, level);
748 T = last_source(acc, maydo, j, level, &rest);
749 may_rel[j] = isl_map_union_disjoint(may_rel[j], T);
750 maydo = rest;
752 intermediate_sources(acc, may_rel, j, level);
754 if (isl_set_fast_is_empty(mustdo) &&
755 isl_set_fast_is_empty(maydo))
756 break;
758 for (j = j - 1; j >= 0; --j) {
759 int plevel;
761 plevel = acc->level_before(acc->source[j].data,
762 acc->sink.data);
763 if (!can_precede_at_level(plevel, level))
764 continue;
766 intermediate_sources(acc, must_rel, j, level);
767 intermediate_sources(acc, may_rel, j, level);
770 for (j = 0; j < acc->n_may; ++j) {
771 int plevel;
772 isl_map *T;
773 isl_set *ran;
775 plevel = acc->level_before(acc->source[acc->n_must + j].data,
776 acc->sink.data);
777 if (!can_precede_at_level(plevel, level))
778 continue;
780 T = all_sources(acc, isl_set_copy(maydo), j, level);
781 res->dep[2 * acc->n_must + j].map =
782 isl_map_union(res->dep[2 * acc->n_must + j].map, T);
783 T = all_sources(acc, isl_set_copy(mustdo), j, level);
784 ran = isl_map_range(isl_map_copy(T));
785 res->dep[2 * acc->n_must + j].map =
786 isl_map_union(res->dep[2 * acc->n_must + j].map, T);
787 mustdo = isl_set_subtract(mustdo, isl_set_copy(ran));
788 maydo = isl_set_union_disjoint(maydo, ran);
790 T = res->dep[2 * acc->n_must + j].map;
791 T = all_intermediate_sources(acc, T, must_rel, may_rel,
792 j, level);
793 res->dep[2 * acc->n_must + j].map = T;
796 for (j = acc->n_must - 1; j >= 0; --j) {
797 res->dep[2 * j].map =
798 isl_map_union_disjoint(res->dep[2 * j].map,
799 must_rel[j]);
800 res->dep[2 * j + 1].map =
801 isl_map_union_disjoint(res->dep[2 * j + 1].map,
802 may_rel[j]);
805 if (isl_set_fast_is_empty(mustdo) &&
806 isl_set_fast_is_empty(maydo))
807 break;
810 free(must_rel);
811 free(may_rel);
812 done:
813 res->must_no_source = mustdo;
814 res->may_no_source = maydo;
815 isl_access_info_free(acc);
816 return res;
817 error:
818 isl_access_info_free(acc);
819 return NULL;
822 /* Given a "sink" access, a list of n "source" accesses,
823 * compute for each iteration of the sink access
824 * and for each element accessed by that iteration,
825 * the source access in the list that last accessed the
826 * element accessed by the sink access before this sink access.
827 * Each access is given as a map from the loop iterators
828 * to the array indices.
829 * The result is a list of n relations between source and sink
830 * iterations and a subset of the domain of the sink access,
831 * corresponding to those iterations that access an element
832 * not previously accessed.
834 * To deal with multi-valued sink access relations, the sink iteration
835 * domain is first extended with dimensions that correspond to the data
836 * space. After the computation is finished, these extra dimensions are
837 * projected out again.
839 __isl_give isl_flow *isl_access_info_compute_flow(__isl_take isl_access_info *acc)
841 int j;
842 struct isl_flow *res;
843 isl_dim *dim;
844 isl_map *id;
845 unsigned n_sink;
846 unsigned n_data;
848 if (!acc)
849 return NULL;
851 n_sink = isl_map_dim(acc->sink.map, isl_dim_in);
852 n_data = isl_map_dim(acc->sink.map, isl_dim_out);
853 dim = isl_dim_range(isl_map_get_dim(acc->sink.map));
854 id = isl_map_identity(dim);
855 id = isl_map_insert(id, isl_dim_in, 0, n_sink);
856 acc->sink.map = isl_map_insert(acc->sink.map, isl_dim_in,
857 n_sink, n_data);
858 acc->sink.map = isl_map_intersect(acc->sink.map, id);
860 if (acc->n_must == 0)
861 res = compute_mem_based_dependences(acc);
862 else
863 res = compute_val_based_dependences(acc);
865 for (j = 0; j < res->n_source; ++j)
866 res->dep[j].map = isl_map_project_out(res->dep[j].map,
867 isl_dim_out, n_sink, n_data);
868 res->must_no_source = isl_set_project_out(res->must_no_source, isl_dim_set, n_sink, n_data);
869 res->may_no_source = isl_set_project_out(res->may_no_source, isl_dim_set, n_sink, n_data);
871 return res;