Add isl_restriction_get_ctx
[platform/upstream/isl.git] / isl_flow.c
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  *
7  * Use of this software is governed by the GNU LGPLv2.1 license
8  *
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 
15  */
16
17 #include <isl/set.h>
18 #include <isl/map.h>
19 #include <isl/flow.h>
20 #include <isl_qsort.h>
21
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
27 };
28
29 struct isl_restriction {
30         enum isl_restriction_type type;
31
32         isl_set *source;
33         isl_set *sink;
34 };
35
36 /* Create a restriction that doesn't restrict anything.
37  */
38 __isl_give isl_restriction *isl_restriction_none(__isl_keep isl_map *source_map)
39 {
40         isl_ctx *ctx;
41         isl_restriction *restr;
42
43         if (!source_map)
44                 return NULL;
45
46         ctx = isl_map_get_ctx(source_map);
47         restr = isl_calloc_type(ctx, struct isl_restriction);
48         if (!restr)
49                 return NULL;
50
51         restr->type = isl_restriction_type_none;
52
53         return restr;
54 }
55
56 /* Create a restriction that removes everything.
57  */
58 __isl_give isl_restriction *isl_restriction_empty(
59         __isl_keep isl_map *source_map)
60 {
61         isl_ctx *ctx;
62         isl_restriction *restr;
63
64         if (!source_map)
65                 return NULL;
66
67         ctx = isl_map_get_ctx(source_map);
68         restr = isl_calloc_type(ctx, struct isl_restriction);
69         if (!restr)
70                 return NULL;
71
72         restr->type = isl_restriction_type_empty;
73
74         return restr;
75 }
76
77 /* Create a restriction on the input of the maximization problem
78  * based on the given source and sink restrictions.
79  */
80 __isl_give isl_restriction *isl_restriction_input(
81         __isl_take isl_set *source_restr, __isl_take isl_set *sink_restr)
82 {
83         isl_ctx *ctx;
84         isl_restriction *restr;
85
86         if (!source_restr || !sink_restr)
87                 goto error;
88
89         ctx = isl_set_get_ctx(source_restr);
90         restr = isl_calloc_type(ctx, struct isl_restriction);
91         if (!restr)
92                 goto error;
93
94         restr->type = isl_restriction_type_input;
95         restr->source = source_restr;
96         restr->sink = sink_restr;
97
98         return restr;
99 error:
100         isl_set_free(source_restr);
101         isl_set_free(sink_restr);
102         return NULL;
103 }
104
105 /* Create a restriction on the output of the maximization problem
106  * based on the given source restriction.
107  */
108 __isl_give isl_restriction *isl_restriction_output(
109         __isl_take isl_set *source_restr)
110 {
111         isl_ctx *ctx;
112         isl_restriction *restr;
113
114         if (!source_restr)
115                 return NULL;
116
117         ctx = isl_set_get_ctx(source_restr);
118         restr = isl_calloc_type(ctx, struct isl_restriction);
119         if (!restr)
120                 goto error;
121
122         restr->type = isl_restriction_type_output;
123         restr->source = source_restr;
124
125         return restr;
126 error:
127         isl_set_free(source_restr);
128         return NULL;
129 }
130
131 void *isl_restriction_free(__isl_take isl_restriction *restr)
132 {
133         if (!restr)
134                 return NULL;
135
136         isl_set_free(restr->source);
137         isl_set_free(restr->sink);
138         free(restr);
139         return NULL;
140 }
141
142 isl_ctx *isl_restriction_get_ctx(__isl_keep isl_restriction *restr)
143 {
144         return restr ? isl_set_get_ctx(restr->source) : NULL;
145 }
146
147 /* A private structure to keep track of a mapping together with
148  * a user-specified identifier and a boolean indicating whether
149  * the map represents a must or may access/dependence.
150  */
151 struct isl_labeled_map {
152         struct isl_map  *map;
153         void            *data;
154         int             must;
155 };
156
157 /* A structure containing the input for dependence analysis:
158  * - a sink
159  * - n_must + n_may (<= max_source) sources
160  * - a function for determining the relative order of sources and sink
161  * The must sources are placed before the may sources.
162  *
163  * domain_map is an auxiliary map that maps the sink access relation
164  * to the domain of this access relation.
165  *
166  * restrict_fn is a callback that (if not NULL) will be called
167  * right before any lexicographical maximization.
168  */
169 struct isl_access_info {
170         isl_map                         *domain_map;
171         struct isl_labeled_map          sink;
172         isl_access_level_before         level_before;
173
174         isl_access_restrict             restrict_fn;
175         void                            *restrict_user;
176
177         int                             max_source;
178         int                             n_must;
179         int                             n_may;
180         struct isl_labeled_map          source[1];
181 };
182
183 /* A structure containing the output of dependence analysis:
184  * - n_source dependences
185  * - a wrapped subset of the sink for which definitely no source could be found
186  * - a wrapped subset of the sink for which possibly no source could be found
187  */
188 struct isl_flow {
189         isl_set                 *must_no_source;
190         isl_set                 *may_no_source;
191         int                     n_source;
192         struct isl_labeled_map  *dep;
193 };
194
195 /* Construct an isl_access_info structure and fill it up with
196  * the given data.  The number of sources is set to 0.
197  */
198 __isl_give isl_access_info *isl_access_info_alloc(__isl_take isl_map *sink,
199         void *sink_user, isl_access_level_before fn, int max_source)
200 {
201         isl_ctx *ctx;
202         struct isl_access_info *acc;
203
204         if (!sink)
205                 return NULL;
206
207         ctx = isl_map_get_ctx(sink);
208         isl_assert(ctx, max_source >= 0, goto error);
209
210         acc = isl_calloc(ctx, struct isl_access_info,
211                         sizeof(struct isl_access_info) +
212                         (max_source - 1) * sizeof(struct isl_labeled_map));
213         if (!acc)
214                 goto error;
215
216         acc->sink.map = sink;
217         acc->sink.data = sink_user;
218         acc->level_before = fn;
219         acc->max_source = max_source;
220         acc->n_must = 0;
221         acc->n_may = 0;
222
223         return acc;
224 error:
225         isl_map_free(sink);
226         return NULL;
227 }
228
229 /* Free the given isl_access_info structure.
230  */
231 void isl_access_info_free(__isl_take isl_access_info *acc)
232 {
233         int i;
234
235         if (!acc)
236                 return;
237         isl_map_free(acc->domain_map);
238         isl_map_free(acc->sink.map);
239         for (i = 0; i < acc->n_must + acc->n_may; ++i)
240                 isl_map_free(acc->source[i].map);
241         free(acc);
242 }
243
244 isl_ctx *isl_access_info_get_ctx(__isl_keep isl_access_info *acc)
245 {
246         return acc ? isl_map_get_ctx(acc->sink.map) : NULL;
247 }
248
249 __isl_give isl_access_info *isl_access_info_set_restrict(
250         __isl_take isl_access_info *acc, isl_access_restrict fn, void *user)
251 {
252         if (!acc)
253                 return NULL;
254         acc->restrict_fn = fn;
255         acc->restrict_user = user;
256         return acc;
257 }
258
259 /* Add another source to an isl_access_info structure, making
260  * sure the "must" sources are placed before the "may" sources.
261  * This function may be called at most max_source times on a
262  * given isl_access_info structure, with max_source as specified
263  * in the call to isl_access_info_alloc that constructed the structure.
264  */
265 __isl_give isl_access_info *isl_access_info_add_source(
266         __isl_take isl_access_info *acc, __isl_take isl_map *source,
267         int must, void *source_user)
268 {
269         isl_ctx *ctx;
270
271         if (!acc)
272                 return NULL;
273         ctx = isl_map_get_ctx(acc->sink.map);
274         isl_assert(ctx, acc->n_must + acc->n_may < acc->max_source, goto error);
275         
276         if (must) {
277                 if (acc->n_may)
278                         acc->source[acc->n_must + acc->n_may] =
279                                 acc->source[acc->n_must];
280                 acc->source[acc->n_must].map = source;
281                 acc->source[acc->n_must].data = source_user;
282                 acc->source[acc->n_must].must = 1;
283                 acc->n_must++;
284         } else {
285                 acc->source[acc->n_must + acc->n_may].map = source;
286                 acc->source[acc->n_must + acc->n_may].data = source_user;
287                 acc->source[acc->n_must + acc->n_may].must = 0;
288                 acc->n_may++;
289         }
290
291         return acc;
292 error:
293         isl_map_free(source);
294         isl_access_info_free(acc);
295         return NULL;
296 }
297
298 /* Return -n, 0 or n (with n a positive value), depending on whether
299  * the source access identified by p1 should be sorted before, together
300  * or after that identified by p2.
301  *
302  * If p1 appears before p2, then it should be sorted first.
303  * For more generic initial schedules, it is possible that neither
304  * p1 nor p2 appears before the other, or at least not in any obvious way.
305  * We therefore also check if p2 appears before p1, in which case p2
306  * should be sorted first.
307  * If not, we try to order the two statements based on the description
308  * of the iteration domains.  This results in an arbitrary, but fairly
309  * stable ordering.
310  */
311 static int access_sort_cmp(const void *p1, const void *p2, void *user)
312 {
313         isl_access_info *acc = user;
314         const struct isl_labeled_map *i1, *i2;
315         int level1, level2;
316         uint32_t h1, h2;
317         i1 = (const struct isl_labeled_map *) p1;
318         i2 = (const struct isl_labeled_map *) p2;
319
320         level1 = acc->level_before(i1->data, i2->data);
321         if (level1 % 2)
322                 return -1;
323
324         level2 = acc->level_before(i2->data, i1->data);
325         if (level2 % 2)
326                 return 1;
327
328         h1 = isl_map_get_hash(i1->map);
329         h2 = isl_map_get_hash(i2->map);
330         return h1 > h2 ? 1 : h1 < h2 ? -1 : 0;
331 }
332
333 /* Sort the must source accesses in their textual order.
334  */
335 static __isl_give isl_access_info *isl_access_info_sort_sources(
336         __isl_take isl_access_info *acc)
337 {
338         if (!acc)
339                 return NULL;
340         if (acc->n_must <= 1)
341                 return acc;
342
343         isl_quicksort(acc->source, acc->n_must, sizeof(struct isl_labeled_map),
344                 access_sort_cmp, acc);
345
346         return acc;
347 }
348
349 /* Align the parameters of the two spaces if needed and then call
350  * isl_space_join.
351  */
352 static __isl_give isl_space *space_align_and_join(__isl_take isl_space *left,
353         __isl_take isl_space *right)
354 {
355         if (isl_space_match(left, isl_dim_param, right, isl_dim_param))
356                 return isl_space_join(left, right);
357
358         left = isl_space_align_params(left, isl_space_copy(right));
359         right = isl_space_align_params(right, isl_space_copy(left));
360         return isl_space_join(left, right);
361 }
362
363 /* Initialize an empty isl_flow structure corresponding to a given
364  * isl_access_info structure.
365  * For each must access, two dependences are created (initialized
366  * to the empty relation), one for the resulting must dependences
367  * and one for the resulting may dependences.  May accesses can
368  * only lead to may dependences, so only one dependence is created
369  * for each of them.
370  * This function is private as isl_flow structures are only supposed
371  * to be created by isl_access_info_compute_flow.
372  */
373 static __isl_give isl_flow *isl_flow_alloc(__isl_keep isl_access_info *acc)
374 {
375         int i;
376         struct isl_ctx *ctx;
377         struct isl_flow *dep;
378
379         if (!acc)
380                 return NULL;
381
382         ctx = isl_map_get_ctx(acc->sink.map);
383         dep = isl_calloc_type(ctx, struct isl_flow);
384         if (!dep)
385                 return NULL;
386
387         dep->dep = isl_calloc_array(ctx, struct isl_labeled_map,
388                                         2 * acc->n_must + acc->n_may);
389         if (!dep->dep)
390                 goto error;
391
392         dep->n_source = 2 * acc->n_must + acc->n_may;
393         for (i = 0; i < acc->n_must; ++i) {
394                 isl_space *dim;
395                 dim = space_align_and_join(
396                         isl_map_get_space(acc->source[i].map),
397                         isl_space_reverse(isl_map_get_space(acc->sink.map)));
398                 dep->dep[2 * i].map = isl_map_empty(dim);
399                 dep->dep[2 * i + 1].map = isl_map_copy(dep->dep[2 * i].map);
400                 dep->dep[2 * i].data = acc->source[i].data;
401                 dep->dep[2 * i + 1].data = acc->source[i].data;
402                 dep->dep[2 * i].must = 1;
403                 dep->dep[2 * i + 1].must = 0;
404                 if (!dep->dep[2 * i].map || !dep->dep[2 * i + 1].map)
405                         goto error;
406         }
407         for (i = acc->n_must; i < acc->n_must + acc->n_may; ++i) {
408                 isl_space *dim;
409                 dim = space_align_and_join(
410                         isl_map_get_space(acc->source[i].map),
411                         isl_space_reverse(isl_map_get_space(acc->sink.map)));
412                 dep->dep[acc->n_must + i].map = isl_map_empty(dim);
413                 dep->dep[acc->n_must + i].data = acc->source[i].data;
414                 dep->dep[acc->n_must + i].must = 0;
415                 if (!dep->dep[acc->n_must + i].map)
416                         goto error;
417         }
418
419         return dep;
420 error:
421         isl_flow_free(dep);
422         return NULL;
423 }
424
425 /* Iterate over all sources and for each resulting flow dependence
426  * that is not empty, call the user specfied function.
427  * The second argument in this function call identifies the source,
428  * while the third argument correspond to the final argument of
429  * the isl_flow_foreach call.
430  */
431 int isl_flow_foreach(__isl_keep isl_flow *deps,
432         int (*fn)(__isl_take isl_map *dep, int must, void *dep_user, void *user),
433         void *user)
434 {
435         int i;
436
437         if (!deps)
438                 return -1;
439
440         for (i = 0; i < deps->n_source; ++i) {
441                 if (isl_map_plain_is_empty(deps->dep[i].map))
442                         continue;
443                 if (fn(isl_map_copy(deps->dep[i].map), deps->dep[i].must,
444                                 deps->dep[i].data, user) < 0)
445                         return -1;
446         }
447
448         return 0;
449 }
450
451 /* Return a copy of the subset of the sink for which no source could be found.
452  */
453 __isl_give isl_map *isl_flow_get_no_source(__isl_keep isl_flow *deps, int must)
454 {
455         if (!deps)
456                 return NULL;
457         
458         if (must)
459                 return isl_set_unwrap(isl_set_copy(deps->must_no_source));
460         else
461                 return isl_set_unwrap(isl_set_copy(deps->may_no_source));
462 }
463
464 void isl_flow_free(__isl_take isl_flow *deps)
465 {
466         int i;
467
468         if (!deps)
469                 return;
470         isl_set_free(deps->must_no_source);
471         isl_set_free(deps->may_no_source);
472         if (deps->dep) {
473                 for (i = 0; i < deps->n_source; ++i)
474                         isl_map_free(deps->dep[i].map);
475                 free(deps->dep);
476         }
477         free(deps);
478 }
479
480 isl_ctx *isl_flow_get_ctx(__isl_keep isl_flow *deps)
481 {
482         return deps ? isl_set_get_ctx(deps->must_no_source) : NULL;
483 }
484
485 /* Return a map that enforces that the domain iteration occurs after
486  * the range iteration at the given level.
487  * If level is odd, then the domain iteration should occur after
488  * the target iteration in their shared level/2 outermost loops.
489  * In this case we simply need to enforce that these outermost
490  * loop iterations are the same.
491  * If level is even, then the loop iterator of the domain should
492  * be greater than the loop iterator of the range at the last
493  * of the level/2 shared loops, i.e., loop level/2 - 1.
494  */
495 static __isl_give isl_map *after_at_level(__isl_take isl_space *dim, int level)
496 {
497         struct isl_basic_map *bmap;
498
499         if (level % 2)
500                 bmap = isl_basic_map_equal(dim, level/2);
501         else
502                 bmap = isl_basic_map_more_at(dim, level/2 - 1);
503
504         return isl_map_from_basic_map(bmap);
505 }
506
507 /* Compute the partial lexicographic maximum of "dep" on domain "sink",
508  * but first check if the user has set acc->restrict_fn and if so
509  * update either the input or the output of the maximization problem
510  * with respect to the resulting restriction.
511  *
512  * Since the user expects a mapping from sink iterations to source iterations,
513  * whereas the domain of "dep" is a wrapped map, mapping sink iterations
514  * to accessed array elements, we first need to project out the accessed
515  * sink array elements by applying acc->domain_map.
516  * Similarly, the sink restriction specified by the user needs to be
517  * converted back to the wrapped map.
518  */
519 static __isl_give isl_map *restricted_partial_lexmax(
520         __isl_keep isl_access_info *acc, __isl_take isl_map *dep,
521         int source, __isl_take isl_set *sink, __isl_give isl_set **empty)
522 {
523         isl_map *source_map;
524         isl_restriction *restr;
525         isl_set *sink_domain;
526         isl_set *sink_restr;
527         isl_map *res;
528
529         if (!acc->restrict_fn)
530                 return isl_map_partial_lexmax(dep, sink, empty);
531
532         source_map = isl_map_copy(dep);
533         source_map = isl_map_apply_domain(source_map,
534                                             isl_map_copy(acc->domain_map));
535         sink_domain = isl_set_copy(sink);
536         sink_domain = isl_set_apply(sink_domain, isl_map_copy(acc->domain_map));
537         restr = acc->restrict_fn(source_map, sink_domain,
538                                 acc->source[source].data, acc->restrict_user);
539         isl_set_free(sink_domain);
540         isl_map_free(source_map);
541
542         if (!restr)
543                 goto error;
544         if (restr->type == isl_restriction_type_input) {
545                 dep = isl_map_intersect_range(dep, isl_set_copy(restr->source));
546                 sink_restr = isl_set_copy(restr->sink);
547                 sink_restr = isl_set_apply(sink_restr,
548                                 isl_map_reverse(isl_map_copy(acc->domain_map)));
549                 sink = isl_set_intersect(sink, sink_restr);
550         } else if (restr->type == isl_restriction_type_empty) {
551                 isl_space *space = isl_map_get_space(dep);
552                 isl_map_free(dep);
553                 dep = isl_map_empty(space);
554         }
555
556         res = isl_map_partial_lexmax(dep, sink, empty);
557
558         if (restr->type == isl_restriction_type_output)
559                 res = isl_map_intersect_range(res, isl_set_copy(restr->source));
560
561         isl_restriction_free(restr);
562         return res;
563 error:
564         isl_map_free(dep);
565         isl_set_free(sink);
566         *empty = NULL;
567         return NULL;
568 }
569
570 /* Compute the last iteration of must source j that precedes the sink
571  * at the given level for sink iterations in set_C.
572  * The subset of set_C for which no such iteration can be found is returned
573  * in *empty.
574  */
575 static struct isl_map *last_source(struct isl_access_info *acc, 
576                                     struct isl_set *set_C,
577                                     int j, int level, struct isl_set **empty)
578 {
579         struct isl_map *read_map;
580         struct isl_map *write_map;
581         struct isl_map *dep_map;
582         struct isl_map *after;
583         struct isl_map *result;
584
585         read_map = isl_map_copy(acc->sink.map);
586         write_map = isl_map_copy(acc->source[j].map);
587         write_map = isl_map_reverse(write_map);
588         dep_map = isl_map_apply_range(read_map, write_map);
589         after = after_at_level(isl_map_get_space(dep_map), level);
590         dep_map = isl_map_intersect(dep_map, after);
591         result = restricted_partial_lexmax(acc, dep_map, j, set_C, empty);
592         result = isl_map_reverse(result);
593
594         return result;
595 }
596
597 /* For a given mapping between iterations of must source j and iterations
598  * of the sink, compute the last iteration of must source k preceding
599  * the sink at level before_level for any of the sink iterations,
600  * but following the corresponding iteration of must source j at level
601  * after_level.
602  */
603 static struct isl_map *last_later_source(struct isl_access_info *acc,
604                                          struct isl_map *old_map,
605                                          int j, int before_level,
606                                          int k, int after_level,
607                                          struct isl_set **empty)
608 {
609         isl_space *dim;
610         struct isl_set *set_C;
611         struct isl_map *read_map;
612         struct isl_map *write_map;
613         struct isl_map *dep_map;
614         struct isl_map *after_write;
615         struct isl_map *before_read;
616         struct isl_map *result;
617
618         set_C = isl_map_range(isl_map_copy(old_map));
619         read_map = isl_map_copy(acc->sink.map);
620         write_map = isl_map_copy(acc->source[k].map);
621
622         write_map = isl_map_reverse(write_map);
623         dep_map = isl_map_apply_range(read_map, write_map);
624         dim = space_align_and_join(isl_map_get_space(acc->source[k].map),
625                     isl_space_reverse(isl_map_get_space(acc->source[j].map)));
626         after_write = after_at_level(dim, after_level);
627         after_write = isl_map_apply_range(after_write, old_map);
628         after_write = isl_map_reverse(after_write);
629         dep_map = isl_map_intersect(dep_map, after_write);
630         before_read = after_at_level(isl_map_get_space(dep_map), before_level);
631         dep_map = isl_map_intersect(dep_map, before_read);
632         result = restricted_partial_lexmax(acc, dep_map, k, set_C, empty);
633         result = isl_map_reverse(result);
634
635         return result;
636 }
637
638 /* Given a shared_level between two accesses, return 1 if the
639  * the first can precede the second at the requested target_level.
640  * If the target level is odd, i.e., refers to a statement level
641  * dimension, then first needs to precede second at the requested
642  * level, i.e., shared_level must be equal to target_level.
643  * If the target level is odd, then the two loops should share
644  * at least the requested number of outer loops.
645  */
646 static int can_precede_at_level(int shared_level, int target_level)
647 {
648         if (shared_level < target_level)
649                 return 0;
650         if ((target_level % 2) && shared_level > target_level)
651                 return 0;
652         return 1;
653 }
654
655 /* Given a possible flow dependence temp_rel[j] between source j and the sink
656  * at level sink_level, remove those elements for which
657  * there is an iteration of another source k < j that is closer to the sink.
658  * The flow dependences temp_rel[k] are updated with the improved sources.
659  * Any improved source needs to precede the sink at the same level
660  * and needs to follow source j at the same or a deeper level.
661  * The lower this level, the later the execution date of source k.
662  * We therefore consider lower levels first.
663  *
664  * If temp_rel[j] is empty, then there can be no improvement and
665  * we return immediately.
666  */
667 static int intermediate_sources(__isl_keep isl_access_info *acc,
668         struct isl_map **temp_rel, int j, int sink_level)
669 {
670         int k, level;
671         int depth = 2 * isl_map_dim(acc->source[j].map, isl_dim_in) + 1;
672
673         if (isl_map_plain_is_empty(temp_rel[j]))
674                 return 0;
675
676         for (k = j - 1; k >= 0; --k) {
677                 int plevel, plevel2;
678                 plevel = acc->level_before(acc->source[k].data, acc->sink.data);
679                 if (!can_precede_at_level(plevel, sink_level))
680                         continue;
681
682                 plevel2 = acc->level_before(acc->source[j].data,
683                                                 acc->source[k].data);
684
685                 for (level = sink_level; level <= depth; ++level) {
686                         struct isl_map *T;
687                         struct isl_set *trest;
688                         struct isl_map *copy;
689
690                         if (!can_precede_at_level(plevel2, level))
691                                 continue;
692
693                         copy = isl_map_copy(temp_rel[j]);
694                         T = last_later_source(acc, copy, j, sink_level, k,
695                                               level, &trest);
696                         if (isl_map_plain_is_empty(T)) {
697                                 isl_set_free(trest);
698                                 isl_map_free(T);
699                                 continue;
700                         }
701                         temp_rel[j] = isl_map_intersect_range(temp_rel[j], trest);
702                         temp_rel[k] = isl_map_union_disjoint(temp_rel[k], T);
703                 }
704         }
705
706         return 0;
707 }
708
709 /* Compute all iterations of may source j that precedes the sink at the given
710  * level for sink iterations in set_C.
711  */
712 static __isl_give isl_map *all_sources(__isl_keep isl_access_info *acc,
713                                     __isl_take isl_set *set_C, int j, int level)
714 {
715         isl_map *read_map;
716         isl_map *write_map;
717         isl_map *dep_map;
718         isl_map *after;
719
720         read_map = isl_map_copy(acc->sink.map);
721         read_map = isl_map_intersect_domain(read_map, set_C);
722         write_map = isl_map_copy(acc->source[acc->n_must + j].map);
723         write_map = isl_map_reverse(write_map);
724         dep_map = isl_map_apply_range(read_map, write_map);
725         after = after_at_level(isl_map_get_space(dep_map), level);
726         dep_map = isl_map_intersect(dep_map, after);
727
728         return isl_map_reverse(dep_map);
729 }
730
731 /* For a given mapping between iterations of must source k and iterations
732  * of the sink, compute the all iteration of may source j preceding
733  * the sink at level before_level for any of the sink iterations,
734  * but following the corresponding iteration of must source k at level
735  * after_level.
736  */
737 static __isl_give isl_map *all_later_sources(__isl_keep isl_access_info *acc,
738         __isl_keep isl_map *old_map,
739         int j, int before_level, int k, int after_level)
740 {
741         isl_space *dim;
742         isl_set *set_C;
743         isl_map *read_map;
744         isl_map *write_map;
745         isl_map *dep_map;
746         isl_map *after_write;
747         isl_map *before_read;
748
749         set_C = isl_map_range(isl_map_copy(old_map));
750         read_map = isl_map_copy(acc->sink.map);
751         read_map = isl_map_intersect_domain(read_map, set_C);
752         write_map = isl_map_copy(acc->source[acc->n_must + j].map);
753
754         write_map = isl_map_reverse(write_map);
755         dep_map = isl_map_apply_range(read_map, write_map);
756         dim = isl_space_join(isl_map_get_space(acc->source[acc->n_must + j].map),
757                     isl_space_reverse(isl_map_get_space(acc->source[k].map)));
758         after_write = after_at_level(dim, after_level);
759         after_write = isl_map_apply_range(after_write, old_map);
760         after_write = isl_map_reverse(after_write);
761         dep_map = isl_map_intersect(dep_map, after_write);
762         before_read = after_at_level(isl_map_get_space(dep_map), before_level);
763         dep_map = isl_map_intersect(dep_map, before_read);
764         return isl_map_reverse(dep_map);
765 }
766
767 /* Given the must and may dependence relations for the must accesses
768  * for level sink_level, check if there are any accesses of may access j
769  * that occur in between and return their union.
770  * If some of these accesses are intermediate with respect to
771  * (previously thought to be) must dependences, then these
772  * must dependences are turned into may dependences.
773  */
774 static __isl_give isl_map *all_intermediate_sources(
775         __isl_keep isl_access_info *acc, __isl_take isl_map *map,
776         struct isl_map **must_rel, struct isl_map **may_rel,
777         int j, int sink_level)
778 {
779         int k, level;
780         int depth = 2 * isl_map_dim(acc->source[acc->n_must + j].map,
781                                         isl_dim_in) + 1;
782
783         for (k = 0; k < acc->n_must; ++k) {
784                 int plevel;
785
786                 if (isl_map_plain_is_empty(may_rel[k]) &&
787                     isl_map_plain_is_empty(must_rel[k]))
788                         continue;
789
790                 plevel = acc->level_before(acc->source[k].data,
791                                         acc->source[acc->n_must + j].data);
792
793                 for (level = sink_level; level <= depth; ++level) {
794                         isl_map *T;
795                         isl_map *copy;
796                         isl_set *ran;
797
798                         if (!can_precede_at_level(plevel, level))
799                                 continue;
800
801                         copy = isl_map_copy(may_rel[k]);
802                         T = all_later_sources(acc, copy, j, sink_level, k, level);
803                         map = isl_map_union(map, T);
804
805                         copy = isl_map_copy(must_rel[k]);
806                         T = all_later_sources(acc, copy, j, sink_level, k, level);
807                         ran = isl_map_range(isl_map_copy(T));
808                         map = isl_map_union(map, T);
809                         may_rel[k] = isl_map_union_disjoint(may_rel[k],
810                             isl_map_intersect_range(isl_map_copy(must_rel[k]),
811                                                     isl_set_copy(ran)));
812                         T = isl_map_from_domain_and_range(
813                             isl_set_universe(
814                                 isl_space_domain(isl_map_get_space(must_rel[k]))),
815                             ran);
816                         must_rel[k] = isl_map_subtract(must_rel[k], T);
817                 }
818         }
819
820         return map;
821 }
822
823 /* Compute dependences for the case where all accesses are "may"
824  * accesses, which boils down to computing memory based dependences.
825  * The generic algorithm would also work in this case, but it would
826  * be overkill to use it.
827  */
828 static __isl_give isl_flow *compute_mem_based_dependences(
829         __isl_keep isl_access_info *acc)
830 {
831         int i;
832         isl_set *mustdo;
833         isl_set *maydo;
834         isl_flow *res;
835
836         res = isl_flow_alloc(acc);
837         if (!res)
838                 return NULL;
839
840         mustdo = isl_map_domain(isl_map_copy(acc->sink.map));
841         maydo = isl_set_copy(mustdo);
842
843         for (i = 0; i < acc->n_may; ++i) {
844                 int plevel;
845                 int is_before;
846                 isl_space *dim;
847                 isl_map *before;
848                 isl_map *dep;
849
850                 plevel = acc->level_before(acc->source[i].data, acc->sink.data);
851                 is_before = plevel & 1;
852                 plevel >>= 1;
853
854                 dim = isl_map_get_space(res->dep[i].map);
855                 if (is_before)
856                         before = isl_map_lex_le_first(dim, plevel);
857                 else
858                         before = isl_map_lex_lt_first(dim, plevel);
859                 dep = isl_map_apply_range(isl_map_copy(acc->source[i].map),
860                         isl_map_reverse(isl_map_copy(acc->sink.map)));
861                 dep = isl_map_intersect(dep, before);
862                 mustdo = isl_set_subtract(mustdo,
863                                             isl_map_range(isl_map_copy(dep)));
864                 res->dep[i].map = isl_map_union(res->dep[i].map, dep);
865         }
866
867         res->may_no_source = isl_set_subtract(maydo, isl_set_copy(mustdo));
868         res->must_no_source = mustdo;
869
870         return res;
871 }
872
873 /* Compute dependences for the case where there is at least one
874  * "must" access.
875  *
876  * The core algorithm considers all levels in which a source may precede
877  * the sink, where a level may either be a statement level or a loop level.
878  * The outermost statement level is 1, the first loop level is 2, etc...
879  * The algorithm basically does the following:
880  * for all levels l of the read access from innermost to outermost
881  *      for all sources w that may precede the sink access at that level
882  *          compute the last iteration of the source that precedes the sink access
883  *                                          at that level
884  *          add result to possible last accesses at level l of source w
885  *          for all sources w2 that we haven't considered yet at this level that may
886  *                                          also precede the sink access
887  *              for all levels l2 of w from l to innermost
888  *                  for all possible last accesses dep of w at l
889  *                      compute last iteration of w2 between the source and sink
890  *                                                              of dep
891  *                      add result to possible last accesses at level l of write w2
892  *                      and replace possible last accesses dep by the remainder
893  *
894  *
895  * The above algorithm is applied to the must access.  During the course
896  * of the algorithm, we keep track of sink iterations that still
897  * need to be considered.  These iterations are split into those that
898  * haven't been matched to any source access (mustdo) and those that have only
899  * been matched to may accesses (maydo).
900  * At the end of each level, we also consider the may accesses.
901  * In particular, we consider may accesses that precede the remaining
902  * sink iterations, moving elements from mustdo to maydo when appropriate,
903  * and may accesses that occur between a must source and a sink of any 
904  * dependences found at the current level, turning must dependences into
905  * may dependences when appropriate.
906  * 
907  */
908 static __isl_give isl_flow *compute_val_based_dependences(
909         __isl_keep isl_access_info *acc)
910 {
911         isl_ctx *ctx;
912         isl_flow *res;
913         isl_set *mustdo = NULL;
914         isl_set *maydo = NULL;
915         int level, j;
916         int depth;
917         isl_map **must_rel = NULL;
918         isl_map **may_rel = NULL;
919
920         if (!acc)
921                 return NULL;
922
923         res = isl_flow_alloc(acc);
924         if (!res)
925                 goto error;
926         ctx = isl_map_get_ctx(acc->sink.map);
927
928         depth = 2 * isl_map_dim(acc->sink.map, isl_dim_in) + 1;
929         mustdo = isl_map_domain(isl_map_copy(acc->sink.map));
930         maydo = isl_set_empty_like(mustdo);
931         if (!mustdo || !maydo)
932                 goto error;
933         if (isl_set_plain_is_empty(mustdo))
934                 goto done;
935
936         must_rel = isl_alloc_array(ctx, struct isl_map *, acc->n_must);
937         may_rel = isl_alloc_array(ctx, struct isl_map *, acc->n_must);
938         if (!must_rel || !may_rel)
939                 goto error;
940
941         for (level = depth; level >= 1; --level) {
942                 for (j = acc->n_must-1; j >=0; --j) {
943                         must_rel[j] = isl_map_empty_like(res->dep[j].map);
944                         may_rel[j] = isl_map_copy(must_rel[j]);
945                 }
946
947                 for (j = acc->n_must - 1; j >= 0; --j) {
948                         struct isl_map *T;
949                         struct isl_set *rest;
950                         int plevel;
951
952                         plevel = acc->level_before(acc->source[j].data,
953                                                      acc->sink.data);
954                         if (!can_precede_at_level(plevel, level))
955                                 continue;
956
957                         T = last_source(acc, mustdo, j, level, &rest);
958                         must_rel[j] = isl_map_union_disjoint(must_rel[j], T);
959                         mustdo = rest;
960
961                         intermediate_sources(acc, must_rel, j, level);
962
963                         T = last_source(acc, maydo, j, level, &rest);
964                         may_rel[j] = isl_map_union_disjoint(may_rel[j], T);
965                         maydo = rest;
966
967                         intermediate_sources(acc, may_rel, j, level);
968
969                         if (isl_set_plain_is_empty(mustdo) &&
970                             isl_set_plain_is_empty(maydo))
971                                 break;
972                 }
973                 for (j = j - 1; j >= 0; --j) {
974                         int plevel;
975
976                         plevel = acc->level_before(acc->source[j].data,
977                                                      acc->sink.data);
978                         if (!can_precede_at_level(plevel, level))
979                                 continue;
980
981                         intermediate_sources(acc, must_rel, j, level);
982                         intermediate_sources(acc, may_rel, j, level);
983                 }
984
985                 for (j = 0; j < acc->n_may; ++j) {
986                         int plevel;
987                         isl_map *T;
988                         isl_set *ran;
989
990                         plevel = acc->level_before(acc->source[acc->n_must + j].data,
991                                                      acc->sink.data);
992                         if (!can_precede_at_level(plevel, level))
993                                 continue;
994
995                         T = all_sources(acc, isl_set_copy(maydo), j, level);
996                         res->dep[2 * acc->n_must + j].map =
997                             isl_map_union(res->dep[2 * acc->n_must + j].map, T);
998                         T = all_sources(acc, isl_set_copy(mustdo), j, level);
999                         ran = isl_map_range(isl_map_copy(T));
1000                         res->dep[2 * acc->n_must + j].map =
1001                             isl_map_union(res->dep[2 * acc->n_must + j].map, T);
1002                         mustdo = isl_set_subtract(mustdo, isl_set_copy(ran));
1003                         maydo = isl_set_union_disjoint(maydo, ran);
1004
1005                         T = res->dep[2 * acc->n_must + j].map;
1006                         T = all_intermediate_sources(acc, T, must_rel, may_rel,
1007                                                         j, level);
1008                         res->dep[2 * acc->n_must + j].map = T;
1009                 }
1010
1011                 for (j = acc->n_must - 1; j >= 0; --j) {
1012                         res->dep[2 * j].map =
1013                                 isl_map_union_disjoint(res->dep[2 * j].map,
1014                                                              must_rel[j]);
1015                         res->dep[2 * j + 1].map =
1016                                 isl_map_union_disjoint(res->dep[2 * j + 1].map,
1017                                                              may_rel[j]);
1018                 }
1019
1020                 if (isl_set_plain_is_empty(mustdo) &&
1021                     isl_set_plain_is_empty(maydo))
1022                         break;
1023         }
1024
1025         free(must_rel);
1026         free(may_rel);
1027 done:
1028         res->must_no_source = mustdo;
1029         res->may_no_source = maydo;
1030         return res;
1031 error:
1032         isl_flow_free(res);
1033         isl_set_free(mustdo);
1034         isl_set_free(maydo);
1035         free(must_rel);
1036         free(may_rel);
1037         return NULL;
1038 }
1039
1040 /* Given a "sink" access, a list of n "source" accesses,
1041  * compute for each iteration of the sink access
1042  * and for each element accessed by that iteration,
1043  * the source access in the list that last accessed the
1044  * element accessed by the sink access before this sink access.
1045  * Each access is given as a map from the loop iterators
1046  * to the array indices.
1047  * The result is a list of n relations between source and sink
1048  * iterations and a subset of the domain of the sink access,
1049  * corresponding to those iterations that access an element
1050  * not previously accessed.
1051  *
1052  * To deal with multi-valued sink access relations, the sink iteration
1053  * domain is first extended with dimensions that correspond to the data
1054  * space.  After the computation is finished, these extra dimensions are
1055  * projected out again.
1056  */
1057 __isl_give isl_flow *isl_access_info_compute_flow(__isl_take isl_access_info *acc)
1058 {
1059         int j;
1060         struct isl_flow *res = NULL;
1061
1062         if (!acc)
1063                 return NULL;
1064
1065         acc->domain_map = isl_map_domain_map(isl_map_copy(acc->sink.map));
1066         acc->sink.map = isl_map_range_map(acc->sink.map);
1067         if (!acc->sink.map)
1068                 goto error;
1069
1070         if (acc->n_must == 0)
1071                 res = compute_mem_based_dependences(acc);
1072         else {
1073                 acc = isl_access_info_sort_sources(acc);
1074                 res = compute_val_based_dependences(acc);
1075         }
1076         if (!res)
1077                 goto error;
1078
1079         for (j = 0; j < res->n_source; ++j) {
1080                 res->dep[j].map = isl_map_apply_range(res->dep[j].map,
1081                                         isl_map_copy(acc->domain_map));
1082                 if (!res->dep[j].map)
1083                         goto error;
1084         }
1085         if (!res->must_no_source || !res->may_no_source)
1086                 goto error;
1087
1088         isl_access_info_free(acc);
1089         return res;
1090 error:
1091         isl_access_info_free(acc);
1092         isl_flow_free(res);
1093         return NULL;
1094 }
1095
1096
1097 /* Keep track of some information about a schedule for a given
1098  * access.  In particular, keep track of which dimensions
1099  * have a constant value and of the actual constant values.
1100  */
1101 struct isl_sched_info {
1102         int *is_cst;
1103         isl_vec *cst;
1104 };
1105
1106 static void sched_info_free(__isl_take struct isl_sched_info *info)
1107 {
1108         if (!info)
1109                 return;
1110         isl_vec_free(info->cst);
1111         free(info->is_cst);
1112         free(info);
1113 }
1114
1115 /* Extract information on the constant dimensions of the schedule
1116  * for a given access.  The "map" is of the form
1117  *
1118  *      [S -> D] -> A
1119  *
1120  * with S the schedule domain, D the iteration domain and A the data domain.
1121  */
1122 static __isl_give struct isl_sched_info *sched_info_alloc(
1123         __isl_keep isl_map *map)
1124 {
1125         isl_ctx *ctx;
1126         isl_space *dim;
1127         struct isl_sched_info *info;
1128         int i, n;
1129         isl_int v;
1130
1131         if (!map)
1132                 return NULL;
1133
1134         dim = isl_space_unwrap(isl_space_domain(isl_map_get_space(map)));
1135         if (!dim)
1136                 return NULL;
1137         n = isl_space_dim(dim, isl_dim_in);
1138         isl_space_free(dim);
1139
1140         ctx = isl_map_get_ctx(map);
1141         info = isl_alloc_type(ctx, struct isl_sched_info);
1142         if (!info)
1143                 return NULL;
1144         info->is_cst = isl_alloc_array(ctx, int, n);
1145         info->cst = isl_vec_alloc(ctx, n);
1146         if (!info->is_cst || !info->cst)
1147                 goto error;
1148
1149         isl_int_init(v);
1150         for (i = 0; i < n; ++i) {
1151                 info->is_cst[i] = isl_map_plain_is_fixed(map, isl_dim_in, i,
1152                                                          &v);
1153                 info->cst = isl_vec_set_element(info->cst, i, v);
1154         }
1155         isl_int_clear(v);
1156
1157         return info;
1158 error:
1159         sched_info_free(info);
1160         return NULL;
1161 }
1162
1163 struct isl_compute_flow_data {
1164         isl_union_map *must_source;
1165         isl_union_map *may_source;
1166         isl_union_map *must_dep;
1167         isl_union_map *may_dep;
1168         isl_union_map *must_no_source;
1169         isl_union_map *may_no_source;
1170
1171         int count;
1172         int must;
1173         isl_space *dim;
1174         struct isl_sched_info *sink_info;
1175         struct isl_sched_info **source_info;
1176         isl_access_info *accesses;
1177 };
1178
1179 static int count_matching_array(__isl_take isl_map *map, void *user)
1180 {
1181         int eq;
1182         isl_space *dim;
1183         struct isl_compute_flow_data *data;
1184
1185         data = (struct isl_compute_flow_data *)user;
1186
1187         dim = isl_space_range(isl_map_get_space(map));
1188
1189         eq = isl_space_is_equal(dim, data->dim);
1190
1191         isl_space_free(dim);
1192         isl_map_free(map);
1193
1194         if (eq < 0)
1195                 return -1;
1196         if (eq)
1197                 data->count++;
1198
1199         return 0;
1200 }
1201
1202 static int collect_matching_array(__isl_take isl_map *map, void *user)
1203 {
1204         int eq;
1205         isl_space *dim;
1206         struct isl_sched_info *info;
1207         struct isl_compute_flow_data *data;
1208
1209         data = (struct isl_compute_flow_data *)user;
1210
1211         dim = isl_space_range(isl_map_get_space(map));
1212
1213         eq = isl_space_is_equal(dim, data->dim);
1214
1215         isl_space_free(dim);
1216
1217         if (eq < 0)
1218                 goto error;
1219         if (!eq) {
1220                 isl_map_free(map);
1221                 return 0;
1222         }
1223
1224         info = sched_info_alloc(map);
1225         data->source_info[data->count] = info;
1226
1227         data->accesses = isl_access_info_add_source(data->accesses,
1228                                                     map, data->must, info);
1229
1230         data->count++;
1231
1232         return 0;
1233 error:
1234         isl_map_free(map);
1235         return -1;
1236 }
1237
1238 /* Determine the shared nesting level and the "textual order" of
1239  * the given accesses.
1240  *
1241  * We first determine the minimal schedule dimension for both accesses.
1242  *
1243  * If among those dimensions, we can find one where both have a fixed
1244  * value and if moreover those values are different, then the previous
1245  * dimension is the last shared nesting level and the textual order
1246  * is determined based on the order of the fixed values.
1247  * If no such fixed values can be found, then we set the shared
1248  * nesting level to the minimal schedule dimension, with no textual ordering.
1249  */
1250 static int before(void *first, void *second)
1251 {
1252         struct isl_sched_info *info1 = first;
1253         struct isl_sched_info *info2 = second;
1254         int n1, n2;
1255         int i;
1256         isl_int v1, v2;
1257
1258         n1 = isl_vec_size(info1->cst);
1259         n2 = isl_vec_size(info2->cst);
1260
1261         if (n2 < n1)
1262                 n1 = n2;
1263
1264         isl_int_init(v1);
1265         isl_int_init(v2);
1266         for (i = 0; i < n1; ++i) {
1267                 int r;
1268
1269                 if (!info1->is_cst[i])
1270                         continue;
1271                 if (!info2->is_cst[i])
1272                         continue;
1273                 isl_vec_get_element(info1->cst, i, &v1);
1274                 isl_vec_get_element(info2->cst, i, &v2);
1275                 if (isl_int_eq(v1, v2))
1276                         continue;
1277
1278                 r = 2 * i + isl_int_lt(v1, v2);
1279
1280                 isl_int_clear(v1);
1281                 isl_int_clear(v2);
1282                 return r;
1283         }
1284         isl_int_clear(v1);
1285         isl_int_clear(v2);
1286
1287         return 2 * n1;
1288 }
1289
1290 /* Given a sink access, look for all the source accesses that access
1291  * the same array and perform dataflow analysis on them using
1292  * isl_access_info_compute_flow.
1293  */
1294 static int compute_flow(__isl_take isl_map *map, void *user)
1295 {
1296         int i;
1297         isl_ctx *ctx;
1298         struct isl_compute_flow_data *data;
1299         isl_flow *flow;
1300
1301         data = (struct isl_compute_flow_data *)user;
1302
1303         ctx = isl_map_get_ctx(map);
1304
1305         data->accesses = NULL;
1306         data->sink_info = NULL;
1307         data->source_info = NULL;
1308         data->count = 0;
1309         data->dim = isl_space_range(isl_map_get_space(map));
1310
1311         if (isl_union_map_foreach_map(data->must_source,
1312                                         &count_matching_array, data) < 0)
1313                 goto error;
1314         if (isl_union_map_foreach_map(data->may_source,
1315                                         &count_matching_array, data) < 0)
1316                 goto error;
1317
1318         data->sink_info = sched_info_alloc(map);
1319         data->source_info = isl_calloc_array(ctx, struct isl_sched_info *,
1320                                              data->count);
1321
1322         data->accesses = isl_access_info_alloc(isl_map_copy(map),
1323                                 data->sink_info, &before, data->count);
1324         if (!data->sink_info || !data->source_info || !data->accesses)
1325                 goto error;
1326         data->count = 0;
1327         data->must = 1;
1328         if (isl_union_map_foreach_map(data->must_source,
1329                                         &collect_matching_array, data) < 0)
1330                 goto error;
1331         data->must = 0;
1332         if (isl_union_map_foreach_map(data->may_source,
1333                                         &collect_matching_array, data) < 0)
1334                 goto error;
1335
1336         flow = isl_access_info_compute_flow(data->accesses);
1337         data->accesses = NULL;
1338
1339         if (!flow)
1340                 goto error;
1341
1342         data->must_no_source = isl_union_map_union(data->must_no_source,
1343                     isl_union_map_from_map(isl_flow_get_no_source(flow, 1)));
1344         data->may_no_source = isl_union_map_union(data->may_no_source,
1345                     isl_union_map_from_map(isl_flow_get_no_source(flow, 0)));
1346
1347         for (i = 0; i < flow->n_source; ++i) {
1348                 isl_union_map *dep;
1349                 dep = isl_union_map_from_map(isl_map_copy(flow->dep[i].map));
1350                 if (flow->dep[i].must)
1351                         data->must_dep = isl_union_map_union(data->must_dep, dep);
1352                 else
1353                         data->may_dep = isl_union_map_union(data->may_dep, dep);
1354         }
1355
1356         isl_flow_free(flow);
1357
1358         sched_info_free(data->sink_info);
1359         if (data->source_info) {
1360                 for (i = 0; i < data->count; ++i)
1361                         sched_info_free(data->source_info[i]);
1362                 free(data->source_info);
1363         }
1364         isl_space_free(data->dim);
1365         isl_map_free(map);
1366
1367         return 0;
1368 error:
1369         isl_access_info_free(data->accesses);
1370         sched_info_free(data->sink_info);
1371         if (data->source_info) {
1372                 for (i = 0; i < data->count; ++i)
1373                         sched_info_free(data->source_info[i]);
1374                 free(data->source_info);
1375         }
1376         isl_space_free(data->dim);
1377         isl_map_free(map);
1378
1379         return -1;
1380 }
1381
1382 /* Given a collection of "sink" and "source" accesses,
1383  * compute for each iteration of a sink access
1384  * and for each element accessed by that iteration,
1385  * the source access in the list that last accessed the
1386  * element accessed by the sink access before this sink access.
1387  * Each access is given as a map from the loop iterators
1388  * to the array indices.
1389  * The result is a relations between source and sink
1390  * iterations and a subset of the domain of the sink accesses,
1391  * corresponding to those iterations that access an element
1392  * not previously accessed.
1393  *
1394  * We first prepend the schedule dimensions to the domain
1395  * of the accesses so that we can easily compare their relative order.
1396  * Then we consider each sink access individually in compute_flow.
1397  */
1398 int isl_union_map_compute_flow(__isl_take isl_union_map *sink,
1399         __isl_take isl_union_map *must_source,
1400         __isl_take isl_union_map *may_source,
1401         __isl_take isl_union_map *schedule,
1402         __isl_give isl_union_map **must_dep, __isl_give isl_union_map **may_dep,
1403         __isl_give isl_union_map **must_no_source,
1404         __isl_give isl_union_map **may_no_source)
1405 {
1406         isl_space *dim;
1407         isl_union_map *range_map = NULL;
1408         struct isl_compute_flow_data data;
1409
1410         sink = isl_union_map_align_params(sink,
1411                                             isl_union_map_get_space(must_source));
1412         sink = isl_union_map_align_params(sink,
1413                                             isl_union_map_get_space(may_source));
1414         sink = isl_union_map_align_params(sink,
1415                                             isl_union_map_get_space(schedule));
1416         dim = isl_union_map_get_space(sink);
1417         must_source = isl_union_map_align_params(must_source, isl_space_copy(dim));
1418         may_source = isl_union_map_align_params(may_source, isl_space_copy(dim));
1419         schedule = isl_union_map_align_params(schedule, isl_space_copy(dim));
1420
1421         schedule = isl_union_map_reverse(schedule);
1422         range_map = isl_union_map_range_map(schedule);
1423         schedule = isl_union_map_reverse(isl_union_map_copy(range_map));
1424         sink = isl_union_map_apply_domain(sink, isl_union_map_copy(schedule));
1425         must_source = isl_union_map_apply_domain(must_source,
1426                                                 isl_union_map_copy(schedule));
1427         may_source = isl_union_map_apply_domain(may_source, schedule);
1428
1429         data.must_source = must_source;
1430         data.may_source = may_source;
1431         data.must_dep = must_dep ?
1432                 isl_union_map_empty(isl_space_copy(dim)) : NULL;
1433         data.may_dep = may_dep ? isl_union_map_empty(isl_space_copy(dim)) : NULL;
1434         data.must_no_source = must_no_source ?
1435                 isl_union_map_empty(isl_space_copy(dim)) : NULL;
1436         data.may_no_source = may_no_source ?
1437                 isl_union_map_empty(isl_space_copy(dim)) : NULL;
1438
1439         isl_space_free(dim);
1440
1441         if (isl_union_map_foreach_map(sink, &compute_flow, &data) < 0)
1442                 goto error;
1443
1444         isl_union_map_free(sink);
1445         isl_union_map_free(must_source);
1446         isl_union_map_free(may_source);
1447
1448         if (must_dep) {
1449                 data.must_dep = isl_union_map_apply_domain(data.must_dep,
1450                                         isl_union_map_copy(range_map));
1451                 data.must_dep = isl_union_map_apply_range(data.must_dep,
1452                                         isl_union_map_copy(range_map));
1453                 *must_dep = data.must_dep;
1454         }
1455         if (may_dep) {
1456                 data.may_dep = isl_union_map_apply_domain(data.may_dep,
1457                                         isl_union_map_copy(range_map));
1458                 data.may_dep = isl_union_map_apply_range(data.may_dep,
1459                                         isl_union_map_copy(range_map));
1460                 *may_dep = data.may_dep;
1461         }
1462         if (must_no_source) {
1463                 data.must_no_source = isl_union_map_apply_domain(
1464                         data.must_no_source, isl_union_map_copy(range_map));
1465                 *must_no_source = data.must_no_source;
1466         }
1467         if (may_no_source) {
1468                 data.may_no_source = isl_union_map_apply_domain(
1469                         data.may_no_source, isl_union_map_copy(range_map));
1470                 *may_no_source = data.may_no_source;
1471         }
1472
1473         isl_union_map_free(range_map);
1474
1475         return 0;
1476 error:
1477         isl_union_map_free(range_map);
1478         isl_union_map_free(sink);
1479         isl_union_map_free(must_source);
1480         isl_union_map_free(may_source);
1481         isl_union_map_free(data.must_dep);
1482         isl_union_map_free(data.may_dep);
1483         isl_union_map_free(data.must_no_source);
1484         isl_union_map_free(data.may_no_source);
1485
1486         if (must_dep)
1487                 *must_dep = NULL;
1488         if (may_dep)
1489                 *may_dep = NULL;
1490         if (must_no_source)
1491                 *must_no_source = NULL;
1492         if (may_no_source)
1493                 *may_no_source = NULL;
1494         return -1;
1495 }