isl_access_info_compute_flow: preserve dimension specifications
[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  *
6  * Use of this software is governed by the GNU LGPLv2.1 license
7  *
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 
14  */
15
16 #include <isl_flow.h>
17
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.
21  */
22 struct isl_labeled_map {
23         struct isl_map  *map;
24         void            *data;
25         int             must;
26 };
27
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.
33  */
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];
41 };
42
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
47  */
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;
53 };
54
55 /* Construct an isl_access_info structure and fill it up with
56  * the given data.  The number of sources is set to 0.
57  */
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)
60 {
61         struct isl_access_info *acc;
62
63         if (!sink)
64                 return NULL;
65
66         isl_assert(sink->ctx, max_source >= 0, goto error);
67
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;
73
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;
80
81         return acc;
82 error:
83         isl_map_free(sink);
84         return NULL;
85 }
86
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.
91  */
92 static void isl_access_info_free(__isl_take isl_access_info *acc)
93 {
94         int i;
95
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);
102 }
103
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.
109  */
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)
113 {
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);
118         
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++;
132         }
133
134         return acc;
135 error:
136         isl_map_free(source);
137         isl_access_info_free(acc);
138         return NULL;
139 }
140
141 /* A temporary structure used while sorting the accesses in an isl_access_info.
142  */
143 struct isl_access_sort_info {
144         struct isl_map          *source_map;
145         void                    *source_data;
146         struct isl_access_info  *acc;
147 };
148
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.
152  *
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.
158  */
159 static int access_sort_cmp(const void *p1, const void *p2)
160 {
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;
165
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);
168
169         if (level1 != level2 || !level1)
170                 return level1 - level2;
171
172         level1 = i1->acc->level_before(i1->source_data, i2->source_data);
173
174         return (level1 % 2) ? -1 : 1;
175 }
176
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.
181  */
182 static __isl_give isl_access_info *isl_access_info_sort_sources(
183         __isl_take isl_access_info *acc)
184 {
185         int i;
186         struct isl_access_sort_info *array;
187
188         if (!acc)
189                 return NULL;
190         if (acc->n_must <= 1)
191                 return acc;
192
193         array = isl_alloc_array(acc->sink.map->ctx,
194                                 struct isl_access_sort_info, acc->n_must);
195         if (!array)
196                 goto error;
197
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;
202         }
203
204         qsort(array, acc->n_must, sizeof(struct isl_access_sort_info),
205                 access_sort_cmp);
206
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;
210         }
211
212         free(array);
213
214         return acc;
215 error:
216         isl_access_info_free(acc);
217         return NULL;
218 }
219
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.
229  */
230 static __isl_give isl_flow *isl_flow_alloc(__isl_keep isl_access_info *acc)
231 {
232         int i;
233         struct isl_ctx *ctx;
234         struct isl_flow *dep;
235
236         if (!acc)
237                 return NULL;
238
239         ctx = acc->sink.map->ctx;
240         dep = isl_calloc_type(ctx, struct isl_flow);
241         if (!dep)
242                 return NULL;
243
244         dep->dep = isl_calloc_array(ctx, struct isl_labeled_map,
245                                         2 * acc->n_must + acc->n_may);
246         if (!dep->dep)
247                 goto error;
248
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_map_get_dim(acc->source[i].map),
253                             isl_dim_reverse(isl_map_get_dim(acc->sink.map)));
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;
260                 if (!dep->dep[2 * i].map || !dep->dep[2 * i + 1].map)
261                         goto error;
262         }
263         for (i = acc->n_must; i < acc->n_must + acc->n_may; ++i) {
264                 struct isl_dim *dim;
265                 dim = isl_dim_join(isl_map_get_dim(acc->source[i].map),
266                             isl_dim_reverse(isl_map_get_dim(acc->sink.map)));
267                 dep->dep[acc->n_must + i].map = isl_map_empty(dim);
268                 dep->dep[acc->n_must + i].data = acc->source[i].data;
269                 dep->dep[acc->n_must + i].must = 0;
270                 if (!dep->dep[acc->n_must + i].map)
271                         goto error;
272         }
273
274         return dep;
275 error:
276         isl_flow_free(dep);
277         return NULL;
278 }
279
280 /* Iterate over all sources and for each resulting flow dependence
281  * that is not empty, call the user specfied function.
282  * The second argument in this function call identifies the source,
283  * while the third argument correspond to the final argument of
284  * the isl_flow_foreach call.
285  */
286 int isl_flow_foreach(__isl_keep isl_flow *deps,
287         int (*fn)(__isl_take isl_map *dep, int must, void *dep_user, void *user),
288         void *user)
289 {
290         int i;
291
292         if (!deps)
293                 return -1;
294
295         for (i = 0; i < deps->n_source; ++i) {
296                 if (isl_map_fast_is_empty(deps->dep[i].map))
297                         continue;
298                 if (fn(isl_map_copy(deps->dep[i].map), deps->dep[i].must,
299                                 deps->dep[i].data, user) < 0)
300                         return -1;
301         }
302
303         return 0;
304 }
305
306 /* Return a copy of the subset of the sink for which no source could be found.
307  */
308 __isl_give isl_set *isl_flow_get_no_source(__isl_keep isl_flow *deps, int must)
309 {
310         if (!deps)
311                 return NULL;
312         
313         if (must)
314                 return isl_set_copy(deps->must_no_source);
315         else
316                 return isl_set_copy(deps->may_no_source);
317 }
318
319 void isl_flow_free(__isl_take isl_flow *deps)
320 {
321         int i;
322
323         if (!deps)
324                 return;
325         isl_set_free(deps->must_no_source);
326         isl_set_free(deps->may_no_source);
327         if (deps->dep) {
328                 for (i = 0; i < deps->n_source; ++i)
329                         isl_map_free(deps->dep[i].map);
330                 free(deps->dep);
331         }
332         free(deps);
333 }
334
335 /* Return a map that enforces that the domain iteration occurs after
336  * the range iteration at the given level.
337  * If level is odd, then the domain iteration should occur after
338  * the target iteration in their shared level/2 outermost loops.
339  * In this case we simply need to enforce that these outermost
340  * loop iterations are the same.
341  * If level is even, then the loop iterator of the domain should
342  * be greater than the loop iterator of the range at the last
343  * of the level/2 shared loops, i.e., loop level/2 - 1.
344  */
345 static __isl_give isl_map *after_at_level(struct isl_dim *dim, int level)
346 {
347         struct isl_basic_map *bmap;
348
349         if (level % 2)
350                 bmap = isl_basic_map_equal(dim, level/2);
351         else
352                 bmap = isl_basic_map_more_at(dim, level/2 - 1);
353
354         return isl_map_from_basic_map(bmap);
355 }
356
357 /* Compute the last iteration of must source j that precedes the sink
358  * at the given level for sink iterations in set_C.
359  * The subset of set_C for which no such iteration can be found is returned
360  * in *empty.
361  */
362 static struct isl_map *last_source(struct isl_access_info *acc, 
363                                     struct isl_set *set_C,
364                                     int j, int level, struct isl_set **empty)
365 {
366         struct isl_map *read_map;
367         struct isl_map *write_map;
368         struct isl_map *dep_map;
369         struct isl_map *after;
370         struct isl_map *result;
371
372         read_map = isl_map_copy(acc->sink.map);
373         write_map = isl_map_copy(acc->source[j].map);
374         write_map = isl_map_reverse(write_map);
375         dep_map = isl_map_apply_range(read_map, write_map);
376         after = after_at_level(isl_map_get_dim(dep_map), level);
377         dep_map = isl_map_intersect(dep_map, after);
378         result = isl_map_partial_lexmax(dep_map, set_C, empty);
379         result = isl_map_reverse(result);
380
381         return result;
382 }
383
384 /* For a given mapping between iterations of must source j and iterations
385  * of the sink, compute the last iteration of must source k preceding
386  * the sink at level before_level for any of the sink iterations,
387  * but following the corresponding iteration of must source j at level
388  * after_level.
389  */
390 static struct isl_map *last_later_source(struct isl_access_info *acc,
391                                          struct isl_map *old_map,
392                                          int j, int before_level,
393                                          int k, int after_level,
394                                          struct isl_set **empty)
395 {
396         struct isl_dim *dim;
397         struct isl_set *set_C;
398         struct isl_map *read_map;
399         struct isl_map *write_map;
400         struct isl_map *dep_map;
401         struct isl_map *after_write;
402         struct isl_map *before_read;
403         struct isl_map *result;
404
405         set_C = isl_map_range(isl_map_copy(old_map));
406         read_map = isl_map_copy(acc->sink.map);
407         write_map = isl_map_copy(acc->source[k].map);
408
409         write_map = isl_map_reverse(write_map);
410         dep_map = isl_map_apply_range(read_map, write_map);
411         dim = isl_dim_join(isl_map_get_dim(acc->source[k].map),
412                     isl_dim_reverse(isl_map_get_dim(acc->source[j].map)));
413         after_write = after_at_level(dim, after_level);
414         after_write = isl_map_apply_range(after_write, old_map);
415         after_write = isl_map_reverse(after_write);
416         dep_map = isl_map_intersect(dep_map, after_write);
417         before_read = after_at_level(isl_map_get_dim(dep_map), before_level);
418         dep_map = isl_map_intersect(dep_map, before_read);
419         result = isl_map_partial_lexmax(dep_map, set_C, empty);
420         result = isl_map_reverse(result);
421
422         return result;
423 }
424
425 /* Given a shared_level between two accesses, return 1 if the
426  * the first can precede the second at the requested target_level.
427  * If the target level is odd, i.e., refers to a statement level
428  * dimension, then first needs to precede second at the requested
429  * level, i.e., shared_level must be equal to target_level.
430  * If the target level is odd, then the two loops should share
431  * at least the requested number of outer loops.
432  */
433 static int can_precede_at_level(int shared_level, int target_level)
434 {
435         if (shared_level < target_level)
436                 return 0;
437         if ((target_level % 2) && shared_level > target_level)
438                 return 0;
439         return 1;
440 }
441
442 /* Given a possible flow dependence temp_rel[j] between source j and the sink
443  * at level sink_level, remove those elements for which
444  * there is an iteration of another source k < j that is closer to the sink.
445  * The flow dependences temp_rel[k] are updated with the improved sources.
446  * Any improved source needs to precede the sink at the same level
447  * and needs to follow source j at the same or a deeper level.
448  * The lower this level, the later the execution date of source k.
449  * We therefore consider lower levels first.
450  *
451  * If temp_rel[j] is empty, then there can be no improvement and
452  * we return immediately.
453  */
454 static int intermediate_sources(__isl_keep isl_access_info *acc,
455         struct isl_map **temp_rel, int j, int sink_level)
456 {
457         int k, level;
458         int depth = 2 * isl_map_dim(acc->source[j].map, isl_dim_in) + 1;
459
460         if (isl_map_fast_is_empty(temp_rel[j]))
461                 return 0;
462
463         for (k = j - 1; k >= 0; --k) {
464                 int plevel, plevel2;
465                 plevel = acc->level_before(acc->source[k].data, acc->sink.data);
466                 if (!can_precede_at_level(plevel, sink_level))
467                         continue;
468
469                 plevel2 = acc->level_before(acc->source[j].data,
470                                                 acc->source[k].data);
471
472                 for (level = sink_level; level <= depth; ++level) {
473                         struct isl_map *T;
474                         struct isl_set *trest;
475                         struct isl_map *copy;
476
477                         if (!can_precede_at_level(plevel2, level))
478                                 continue;
479
480                         copy = isl_map_copy(temp_rel[j]);
481                         T = last_later_source(acc, copy, j, sink_level, k,
482                                               level, &trest);
483                         if (isl_map_fast_is_empty(T)) {
484                                 isl_set_free(trest);
485                                 isl_map_free(T);
486                                 continue;
487                         }
488                         temp_rel[j] = isl_map_intersect_range(temp_rel[j], trest);
489                         temp_rel[k] = isl_map_union_disjoint(temp_rel[k], T);
490                 }
491         }
492
493         return 0;
494 }
495
496 /* Compute all iterations of may source j that precedes the sink at the given
497  * level for sink iterations in set_C.
498  */
499 static __isl_give isl_map *all_sources(__isl_keep isl_access_info *acc,
500                                     __isl_take isl_set *set_C, int j, int level)
501 {
502         isl_map *read_map;
503         isl_map *write_map;
504         isl_map *dep_map;
505         isl_map *after;
506
507         read_map = isl_map_copy(acc->sink.map);
508         read_map = isl_map_intersect_domain(read_map, set_C);
509         write_map = isl_map_copy(acc->source[acc->n_must + j].map);
510         write_map = isl_map_reverse(write_map);
511         dep_map = isl_map_apply_range(read_map, write_map);
512         after = after_at_level(isl_map_get_dim(dep_map), level);
513         dep_map = isl_map_intersect(dep_map, after);
514
515         return isl_map_reverse(dep_map);
516 }
517
518 /* For a given mapping between iterations of must source k and iterations
519  * of the sink, compute the all iteration of may source j preceding
520  * the sink at level before_level for any of the sink iterations,
521  * but following the corresponding iteration of must source k at level
522  * after_level.
523  */
524 static __isl_give isl_map *all_later_sources(__isl_keep isl_access_info *acc,
525         __isl_keep isl_map *old_map,
526         int j, int before_level, int k, int after_level)
527 {
528         isl_dim *dim;
529         isl_set *set_C;
530         isl_map *read_map;
531         isl_map *write_map;
532         isl_map *dep_map;
533         isl_map *after_write;
534         isl_map *before_read;
535
536         set_C = isl_map_range(isl_map_copy(old_map));
537         read_map = isl_map_copy(acc->sink.map);
538         read_map = isl_map_intersect_domain(read_map, set_C);
539         write_map = isl_map_copy(acc->source[acc->n_must + j].map);
540
541         write_map = isl_map_reverse(write_map);
542         dep_map = isl_map_apply_range(read_map, write_map);
543         dim = isl_dim_join(isl_map_get_dim(acc->source[acc->n_must + j].map),
544                     isl_dim_reverse(isl_map_get_dim(acc->source[k].map)));
545         after_write = after_at_level(dim, after_level);
546         after_write = isl_map_apply_range(after_write, old_map);
547         after_write = isl_map_reverse(after_write);
548         dep_map = isl_map_intersect(dep_map, after_write);
549         before_read = after_at_level(isl_map_get_dim(dep_map), before_level);
550         dep_map = isl_map_intersect(dep_map, before_read);
551         return isl_map_reverse(dep_map);
552 }
553
554 /* Given the must and may dependence relations for the must accesses
555  * for level sink_level, check if there are any accesses of may access j
556  * that occur in between and return their union.
557  * If some of these accesses are intermediate with respect to
558  * (previously thought to be) must dependences, then these
559  * must dependences are turned into may dependences.
560  */
561 static __isl_give isl_map *all_intermediate_sources(
562         __isl_keep isl_access_info *acc, __isl_take isl_map *map,
563         struct isl_map **must_rel, struct isl_map **may_rel,
564         int j, int sink_level)
565 {
566         int k, level;
567         int depth = 2 * isl_map_dim(acc->source[acc->n_must + j].map,
568                                         isl_dim_in) + 1;
569
570         for (k = 0; k < acc->n_must; ++k) {
571                 int plevel;
572
573                 if (isl_map_fast_is_empty(may_rel[k]) &&
574                     isl_map_fast_is_empty(must_rel[k]))
575                         continue;
576
577                 plevel = acc->level_before(acc->source[k].data,
578                                         acc->source[acc->n_must + j].data);
579
580                 for (level = sink_level; level <= depth; ++level) {
581                         isl_map *T;
582                         isl_map *copy;
583                         isl_set *ran;
584
585                         if (!can_precede_at_level(plevel, level))
586                                 continue;
587
588                         copy = isl_map_copy(may_rel[k]);
589                         T = all_later_sources(acc, copy, j, sink_level, k, level);
590                         map = isl_map_union(map, T);
591
592                         copy = isl_map_copy(must_rel[k]);
593                         T = all_later_sources(acc, copy, j, sink_level, k, level);
594                         ran = isl_map_range(isl_map_copy(T));
595                         map = isl_map_union(map, T);
596                         may_rel[k] = isl_map_union_disjoint(may_rel[k],
597                             isl_map_intersect_range(isl_map_copy(must_rel[k]),
598                                                     isl_set_copy(ran)));
599                         T = isl_map_from_domain_and_range(
600                             isl_set_universe(
601                                 isl_dim_domain(isl_map_get_dim(must_rel[k]))),
602                             ran);
603                         must_rel[k] = isl_map_subtract(must_rel[k], T);
604                 }
605         }
606
607         return map;
608 }
609
610 /* Compute dependences for the case where all accesses are "may"
611  * accesses, which boils down to computing memory based dependences.
612  * The generic algorithm would also work in this case, but it would
613  * be overkill to use it.
614  */
615 static __isl_give isl_flow *compute_mem_based_dependences(
616         __isl_take isl_access_info *acc)
617 {
618         int i;
619         isl_set *mustdo;
620         isl_set *maydo;
621         isl_flow *res;
622
623         res = isl_flow_alloc(acc);
624         if (!res)
625                 goto error;
626
627         mustdo = isl_map_domain(isl_map_copy(acc->sink.map));
628         maydo = isl_set_copy(mustdo);
629
630         for (i = 0; i < acc->n_may; ++i) {
631                 int plevel;
632                 int is_before;
633                 isl_dim *dim;
634                 isl_map *before;
635                 isl_map *dep;
636
637                 plevel = acc->level_before(acc->source[i].data, acc->sink.data);
638                 is_before = plevel & 1;
639                 plevel >>= 1;
640
641                 dim = isl_map_get_dim(res->dep[i].map);
642                 if (is_before)
643                         before = isl_map_lex_le_first(dim, plevel);
644                 else
645                         before = isl_map_lex_lt_first(dim, plevel);
646                 dep = isl_map_apply_range(isl_map_copy(acc->source[i].map),
647                         isl_map_reverse(isl_map_copy(acc->sink.map)));
648                 dep = isl_map_intersect(dep, before);
649                 mustdo = isl_set_subtract(mustdo,
650                                             isl_map_range(isl_map_copy(dep)));
651                 res->dep[i].map = isl_map_union(res->dep[i].map, dep);
652         }
653
654         res->may_no_source = isl_set_subtract(maydo, isl_set_copy(mustdo));
655         res->must_no_source = mustdo;
656
657         isl_access_info_free(acc);
658
659         return res;
660 error:
661         isl_access_info_free(acc);
662         return NULL;
663 }
664
665 /* Compute dependences for the case where there is at least one
666  * "must" access.
667  *
668  * The core algorithm considers all levels in which a source may precede
669  * the sink, where a level may either be a statement level or a loop level.
670  * The outermost statement level is 1, the first loop level is 2, etc...
671  * The algorithm basically does the following:
672  * for all levels l of the read access from innermost to outermost
673  *      for all sources w that may precede the sink access at that level
674  *          compute the last iteration of the source that precedes the sink access
675  *                                          at that level
676  *          add result to possible last accesses at level l of source w
677  *          for all sources w2 that we haven't considered yet at this level that may
678  *                                          also precede the sink access
679  *              for all levels l2 of w from l to innermost
680  *                  for all possible last accesses dep of w at l
681  *                      compute last iteration of w2 between the source and sink
682  *                                                              of dep
683  *                      add result to possible last accesses at level l of write w2
684  *                      and replace possible last accesses dep by the remainder
685  *
686  *
687  * The above algorithm is applied to the must access.  During the course
688  * of the algorithm, we keep track of sink iterations that still
689  * need to be considered.  These iterations are split into those that
690  * haven't been matched to any source access (mustdo) and those that have only
691  * been matched to may accesses (maydo).
692  * At the end of each level, we also consider the may accesses.
693  * In particular, we consider may accesses that precede the remaining
694  * sink iterations, moving elements from mustdo to maydo when appropriate,
695  * and may accesses that occur between a must source and a sink of any 
696  * dependences found at the current level, turning must dependences into
697  * may dependences when appropriate.
698  * 
699  */
700 static __isl_give isl_flow *compute_val_based_dependences(
701         __isl_take isl_access_info *acc)
702 {
703         isl_ctx *ctx;
704         isl_flow *res;
705         isl_set *mustdo = NULL;
706         isl_set *maydo = NULL;
707         int level, j;
708         int depth;
709         isl_map **must_rel = NULL;
710         isl_map **may_rel = NULL;
711
712         acc = isl_access_info_sort_sources(acc);
713         if (!acc)
714                 return NULL;
715
716         res = isl_flow_alloc(acc);
717         if (!res)
718                 goto error;
719         ctx = acc->sink.map->ctx;
720
721         depth = 2 * isl_map_dim(acc->sink.map, isl_dim_in) + 1;
722         mustdo = isl_map_domain(isl_map_copy(acc->sink.map));
723         maydo = isl_set_empty_like(mustdo);
724         if (!mustdo || !maydo)
725                 goto error;
726         if (isl_set_fast_is_empty(mustdo))
727                 goto done;
728
729         must_rel = isl_alloc_array(ctx, struct isl_map *, acc->n_must);
730         may_rel = isl_alloc_array(ctx, struct isl_map *, acc->n_must);
731         if (!must_rel || !may_rel)
732                 goto error;
733
734         for (level = depth; level >= 1; --level) {
735                 for (j = acc->n_must-1; j >=0; --j) {
736                         must_rel[j] = isl_map_empty_like(res->dep[j].map);
737                         may_rel[j] = isl_map_copy(must_rel[j]);
738                 }
739
740                 for (j = acc->n_must - 1; j >= 0; --j) {
741                         struct isl_map *T;
742                         struct isl_set *rest;
743                         int plevel;
744
745                         plevel = acc->level_before(acc->source[j].data,
746                                                      acc->sink.data);
747                         if (!can_precede_at_level(plevel, level))
748                                 continue;
749
750                         T = last_source(acc, mustdo, j, level, &rest);
751                         must_rel[j] = isl_map_union_disjoint(must_rel[j], T);
752                         mustdo = rest;
753
754                         intermediate_sources(acc, must_rel, j, level);
755
756                         T = last_source(acc, maydo, j, level, &rest);
757                         may_rel[j] = isl_map_union_disjoint(may_rel[j], T);
758                         maydo = rest;
759
760                         intermediate_sources(acc, may_rel, j, level);
761
762                         if (isl_set_fast_is_empty(mustdo) &&
763                             isl_set_fast_is_empty(maydo))
764                                 break;
765                 }
766                 for (j = j - 1; j >= 0; --j) {
767                         int plevel;
768
769                         plevel = acc->level_before(acc->source[j].data,
770                                                      acc->sink.data);
771                         if (!can_precede_at_level(plevel, level))
772                                 continue;
773
774                         intermediate_sources(acc, must_rel, j, level);
775                         intermediate_sources(acc, may_rel, j, level);
776                 }
777
778                 for (j = 0; j < acc->n_may; ++j) {
779                         int plevel;
780                         isl_map *T;
781                         isl_set *ran;
782
783                         plevel = acc->level_before(acc->source[acc->n_must + j].data,
784                                                      acc->sink.data);
785                         if (!can_precede_at_level(plevel, level))
786                                 continue;
787
788                         T = all_sources(acc, isl_set_copy(maydo), j, level);
789                         res->dep[2 * acc->n_must + j].map =
790                             isl_map_union(res->dep[2 * acc->n_must + j].map, T);
791                         T = all_sources(acc, isl_set_copy(mustdo), j, level);
792                         ran = isl_map_range(isl_map_copy(T));
793                         res->dep[2 * acc->n_must + j].map =
794                             isl_map_union(res->dep[2 * acc->n_must + j].map, T);
795                         mustdo = isl_set_subtract(mustdo, isl_set_copy(ran));
796                         maydo = isl_set_union_disjoint(maydo, ran);
797
798                         T = res->dep[2 * acc->n_must + j].map;
799                         T = all_intermediate_sources(acc, T, must_rel, may_rel,
800                                                         j, level);
801                         res->dep[2 * acc->n_must + j].map = T;
802                 }
803
804                 for (j = acc->n_must - 1; j >= 0; --j) {
805                         res->dep[2 * j].map =
806                                 isl_map_union_disjoint(res->dep[2 * j].map,
807                                                              must_rel[j]);
808                         res->dep[2 * j + 1].map =
809                                 isl_map_union_disjoint(res->dep[2 * j + 1].map,
810                                                              may_rel[j]);
811                 }
812
813                 if (isl_set_fast_is_empty(mustdo) &&
814                     isl_set_fast_is_empty(maydo))
815                         break;
816         }
817
818         free(must_rel);
819         free(may_rel);
820 done:
821         res->must_no_source = mustdo;
822         res->may_no_source = maydo;
823         isl_access_info_free(acc);
824         return res;
825 error:
826         isl_access_info_free(acc);
827         isl_flow_free(res);
828         isl_set_free(mustdo);
829         isl_set_free(maydo);
830         free(must_rel);
831         free(may_rel);
832         return NULL;
833 }
834
835 /* Given a "sink" access, a list of n "source" accesses,
836  * compute for each iteration of the sink access
837  * and for each element accessed by that iteration,
838  * the source access in the list that last accessed the
839  * element accessed by the sink access before this sink access.
840  * Each access is given as a map from the loop iterators
841  * to the array indices.
842  * The result is a list of n relations between source and sink
843  * iterations and a subset of the domain of the sink access,
844  * corresponding to those iterations that access an element
845  * not previously accessed.
846  *
847  * To deal with multi-valued sink access relations, the sink iteration
848  * domain is first extended with dimensions that correspond to the data
849  * space.  After the computation is finished, these extra dimensions are
850  * projected out again.
851  */
852 __isl_give isl_flow *isl_access_info_compute_flow(__isl_take isl_access_info *acc)
853 {
854         int j;
855         struct isl_flow *res;
856         isl_map *domain_map = NULL;
857
858         if (!acc)
859                 return NULL;
860
861         domain_map = isl_map_domain_map(isl_map_copy(acc->sink.map));
862         acc->sink.map = isl_map_range_map(acc->sink.map);
863         if (!acc->sink.map)
864                 goto error;
865
866         if (acc->n_must == 0)
867                 res = compute_mem_based_dependences(acc);
868         else
869                 res = compute_val_based_dependences(acc);
870         if (!res)
871                 return NULL;
872
873         for (j = 0; j < res->n_source; ++j) {
874                 res->dep[j].map = isl_map_apply_range(res->dep[j].map,
875                                         isl_map_copy(domain_map));
876                 if (!res->dep[j].map)
877                         goto error2;
878         }
879         res->must_no_source = isl_set_apply(res->must_no_source,
880                                         isl_map_copy(domain_map));
881         res->may_no_source = isl_set_apply(res->may_no_source,
882                                         isl_map_copy(domain_map));
883         if (!res->must_no_source || !res->may_no_source)
884                 goto error2;
885
886         isl_map_free(domain_map);
887         return res;
888 error:
889         isl_map_free(domain_map);
890         isl_access_info_free(acc);
891         return NULL;
892 error2:
893         isl_map_free(domain_map);
894         isl_flow_free(res);
895         return NULL;
896 }