4d20c07730ab24c2116d79fb9e21daa7beef6ed0
[platform/upstream/isl.git] / isl_transitive_closure.c
1 /*
2  * Copyright 2010      INRIA Saclay
3  *
4  * Use of this software is governed by the GNU LGPLv2.1 license
5  *
6  * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
7  * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
8  * 91893 Orsay, France 
9  */
10
11 #include "isl_map.h"
12 #include "isl_map_private.h"
13 #include "isl_seq.h"
14 #include <isl_lp.h>
15  
16 /* Given a map that represents a path with the length of the path
17  * encoded as the difference between the last output coordindate
18  * and the last input coordinate, set this length to either
19  * exactly "length" (if "exactly" is set) or at least "length"
20  * (if "exactly" is not set).
21  */
22 static __isl_give isl_map *set_path_length(__isl_take isl_map *map,
23         int exactly, int length)
24 {
25         struct isl_dim *dim;
26         struct isl_basic_map *bmap;
27         unsigned d;
28         unsigned nparam;
29         int k;
30         isl_int *c;
31
32         if (!map)
33                 return NULL;
34
35         dim = isl_map_get_dim(map);
36         d = isl_dim_size(dim, isl_dim_in);
37         nparam = isl_dim_size(dim, isl_dim_param);
38         bmap = isl_basic_map_alloc_dim(dim, 0, 1, 1);
39         if (exactly) {
40                 k = isl_basic_map_alloc_equality(bmap);
41                 c = bmap->eq[k];
42         } else {
43                 k = isl_basic_map_alloc_inequality(bmap);
44                 c = bmap->ineq[k];
45         }
46         if (k < 0)
47                 goto error;
48         isl_seq_clr(c, 1 + isl_basic_map_total_dim(bmap));
49         isl_int_set_si(c[0], -length);
50         isl_int_set_si(c[1 + nparam + d - 1], -1);
51         isl_int_set_si(c[1 + nparam + d + d - 1], 1);
52
53         bmap = isl_basic_map_finalize(bmap);
54         map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
55
56         return map;
57 error:
58         isl_basic_map_free(bmap);
59         isl_map_free(map);
60         return NULL;
61 }
62
63 /* Check whether the overapproximation of the power of "map" is exactly
64  * the power of "map".  Let R be "map" and A_k the overapproximation.
65  * The approximation is exact if
66  *
67  *      A_1 = R
68  *      A_k = A_{k-1} \circ R                   k >= 2
69  *
70  * Since A_k is known to be an overapproximation, we only need to check
71  *
72  *      A_1 \subset R
73  *      A_k \subset A_{k-1} \circ R             k >= 2
74  *
75  * In practice, "app" has an extra input and output coordinate
76  * to encode the length of the path.  So, we first need to add
77  * this coordinate to "map" and set the length of the path to
78  * one.
79  */
80 static int check_power_exactness(__isl_take isl_map *map,
81         __isl_take isl_map *app)
82 {
83         int exact;
84         isl_map *app_1;
85         isl_map *app_2;
86
87         map = isl_map_add(map, isl_dim_in, 1);
88         map = isl_map_add(map, isl_dim_out, 1);
89         map = set_path_length(map, 1, 1);
90
91         app_1 = set_path_length(isl_map_copy(app), 1, 1);
92
93         exact = isl_map_is_subset(app_1, map);
94         isl_map_free(app_1);
95
96         if (!exact || exact < 0) {
97                 isl_map_free(app);
98                 isl_map_free(map);
99                 return exact;
100         }
101
102         app_1 = set_path_length(isl_map_copy(app), 0, 1);
103         app_2 = set_path_length(app, 0, 2);
104         app_1 = isl_map_apply_range(map, app_1);
105
106         exact = isl_map_is_subset(app_2, app_1);
107
108         isl_map_free(app_1);
109         isl_map_free(app_2);
110
111         return exact;
112 }
113
114 /* Check whether the overapproximation of the power of "map" is exactly
115  * the power of "map", possibly after projecting out the power (if "project"
116  * is set).
117  *
118  * If "project" is set and if "steps" can only result in acyclic paths,
119  * then we check
120  *
121  *      A = R \cup (A \circ R)
122  *
123  * where A is the overapproximation with the power projected out, i.e.,
124  * an overapproximation of the transitive closure.
125  * More specifically, since A is known to be an overapproximation, we check
126  *
127  *      A \subset R \cup (A \circ R)
128  *
129  * Otherwise, we check if the power is exact.
130  *
131  * Note that "app" has an extra input and output coordinate to encode
132  * the length of the part.  If we are only interested in the transitive
133  * closure, then we can simply project out these coordinates first.
134  */
135 static int check_exactness(__isl_take isl_map *map, __isl_take isl_map *app,
136         int project)
137 {
138         isl_map *test;
139         int exact;
140         unsigned d;
141
142         if (!project)
143                 return check_power_exactness(map, app);
144
145         d = isl_map_dim(map, isl_dim_in);
146         app = set_path_length(app, 0, 1);
147         app = isl_map_project_out(app, isl_dim_in, d, 1);
148         app = isl_map_project_out(app, isl_dim_out, d, 1);
149
150         test = isl_map_apply_range(isl_map_copy(map), isl_map_copy(app));
151         test = isl_map_union(test, isl_map_copy(map));
152
153         exact = isl_map_is_subset(app, test);
154
155         isl_map_free(app);
156         isl_map_free(test);
157
158         isl_map_free(map);
159
160         return exact;
161 error:
162         isl_map_free(app);
163         isl_map_free(map);
164         return -1;
165 }
166
167 /*
168  * The transitive closure implementation is based on the paper
169  * "Computing the Transitive Closure of a Union of Affine Integer
170  * Tuple Relations" by Anna Beletska, Denis Barthou, Wlodzimierz Bielecki and
171  * Albert Cohen.
172  */
173
174 /* Given a set of n offsets v_i (the rows of "steps"), construct a relation
175  * of the given dimension specification (Z^{n+1} -> Z^{n+1})
176  * that maps an element x to any element that can be reached
177  * by taking a non-negative number of steps along any of
178  * the extended offsets v'_i = [v_i 1].
179  * That is, construct
180  *
181  * { [x] -> [y] : exists k_i >= 0, y = x + \sum_i k_i v'_i }
182  *
183  * For any element in this relation, the number of steps taken
184  * is equal to the difference in the final coordinates.
185  */
186 static __isl_give isl_map *path_along_steps(__isl_take isl_dim *dim,
187         __isl_keep isl_mat *steps)
188 {
189         int i, j, k;
190         struct isl_basic_map *path = NULL;
191         unsigned d;
192         unsigned n;
193         unsigned nparam;
194
195         if (!dim || !steps)
196                 goto error;
197
198         d = isl_dim_size(dim, isl_dim_in);
199         n = steps->n_row;
200         nparam = isl_dim_size(dim, isl_dim_param);
201
202         path = isl_basic_map_alloc_dim(isl_dim_copy(dim), n, d, n);
203
204         for (i = 0; i < n; ++i) {
205                 k = isl_basic_map_alloc_div(path);
206                 if (k < 0)
207                         goto error;
208                 isl_assert(steps->ctx, i == k, goto error);
209                 isl_int_set_si(path->div[k][0], 0);
210         }
211
212         for (i = 0; i < d; ++i) {
213                 k = isl_basic_map_alloc_equality(path);
214                 if (k < 0)
215                         goto error;
216                 isl_seq_clr(path->eq[k], 1 + isl_basic_map_total_dim(path));
217                 isl_int_set_si(path->eq[k][1 + nparam + i], 1);
218                 isl_int_set_si(path->eq[k][1 + nparam + d + i], -1);
219                 if (i == d - 1)
220                         for (j = 0; j < n; ++j)
221                                 isl_int_set_si(path->eq[k][1 + nparam + 2 * d + j], 1);
222                 else
223                         for (j = 0; j < n; ++j)
224                                 isl_int_set(path->eq[k][1 + nparam + 2 * d + j],
225                                             steps->row[j][i]);
226         }
227
228         for (i = 0; i < n; ++i) {
229                 k = isl_basic_map_alloc_inequality(path);
230                 if (k < 0)
231                         goto error;
232                 isl_seq_clr(path->ineq[k], 1 + isl_basic_map_total_dim(path));
233                 isl_int_set_si(path->ineq[k][1 + nparam + 2 * d + i], 1);
234         }
235
236         isl_dim_free(dim);
237
238         path = isl_basic_map_simplify(path);
239         path = isl_basic_map_finalize(path);
240         return isl_map_from_basic_map(path);
241 error:
242         isl_dim_free(dim);
243         isl_basic_map_free(path);
244         return NULL;
245 }
246
247 #define IMPURE          0
248 #define PURE_PARAM      1
249 #define PURE_VAR        2
250 #define MIXED           3
251
252 /* Return PURE_PARAM if only the coefficients of the parameters are non-zero.
253  * Return PURE_VAR if only the coefficients of the set variables are non-zero.
254  * Return MIXED if only the coefficients of the parameters and the set
255  *      variables are non-zero and if moreover the parametric constant
256  *      can never attain positive values.
257  * Return IMPURE otherwise.
258  */
259 static int purity(__isl_keep isl_basic_set *bset, isl_int *c, int *div_purity,
260         int eq)
261 {
262         unsigned d;
263         unsigned n_div;
264         unsigned nparam;
265         int k;
266         int empty;
267         int i;
268         int p = 0, v = 0;
269
270         n_div = isl_basic_set_dim(bset, isl_dim_div);
271         d = isl_basic_set_dim(bset, isl_dim_set);
272         nparam = isl_basic_set_dim(bset, isl_dim_param);
273
274         for (i = 0; i < n_div; ++i) {
275                 if (isl_int_is_zero(c[1 + nparam + d + i]))
276                         continue;
277                 switch (div_purity[i]) {
278                 case PURE_PARAM: p = 1; break;
279                 case PURE_VAR: v = 1; break;
280                 default: return IMPURE;
281                 }
282         }
283         if (!p && isl_seq_first_non_zero(c + 1, nparam) == -1)
284                 return PURE_VAR;
285         if (!v && isl_seq_first_non_zero(c + 1 + nparam, d) == -1)
286                 return PURE_PARAM;
287         if (eq)
288                 return IMPURE;
289
290         bset = isl_basic_set_copy(bset);
291         bset = isl_basic_set_cow(bset);
292         bset = isl_basic_set_extend_constraints(bset, 0, 1);
293         k = isl_basic_set_alloc_inequality(bset);
294         if (k < 0)
295                 goto error;
296         isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
297         isl_seq_cpy(bset->ineq[k], c, 1 + nparam);
298         for (i = 0; i < n_div; ++i) {
299                 if (div_purity[i] != PURE_PARAM)
300                         continue;
301                 isl_int_set(bset->ineq[k][1 + nparam + d + i],
302                             c[1 + nparam + d + i]);
303         }
304         isl_int_sub_ui(bset->ineq[k][0], bset->ineq[k][0], 1);
305         empty = isl_basic_set_is_empty(bset);
306         isl_basic_set_free(bset);
307
308         return empty < 0 ? -1 : empty ? MIXED : IMPURE;
309 error:
310         isl_basic_set_free(bset);
311         return -1;
312 }
313
314 /* Return an array of integers indicating the type of each div in bset.
315  * If the div is (recursively) defined in terms of only the parameters,
316  * then the type is PURE_PARAM.
317  * If the div is (recursively) defined in terms of only the set variables,
318  * then the type is PURE_VAR.
319  * Otherwise, the type is IMPURE.
320  */
321 static __isl_give int *get_div_purity(__isl_keep isl_basic_set *bset)
322 {
323         int i, j;
324         int *div_purity;
325         unsigned d;
326         unsigned n_div;
327         unsigned nparam;
328
329         if (!bset)
330                 return NULL;
331
332         n_div = isl_basic_set_dim(bset, isl_dim_div);
333         d = isl_basic_set_dim(bset, isl_dim_set);
334         nparam = isl_basic_set_dim(bset, isl_dim_param);
335
336         div_purity = isl_alloc_array(bset->ctx, int, n_div);
337         if (!div_purity)
338                 return NULL;
339
340         for (i = 0; i < bset->n_div; ++i) {
341                 int p = 0, v = 0;
342                 if (isl_int_is_zero(bset->div[i][0])) {
343                         div_purity[i] = IMPURE;
344                         continue;
345                 }
346                 if (isl_seq_first_non_zero(bset->div[i] + 2, nparam) != -1)
347                         p = 1;
348                 if (isl_seq_first_non_zero(bset->div[i] + 2 + nparam, d) != -1)
349                         v = 1;
350                 for (j = 0; j < i; ++j) {
351                         if (isl_int_is_zero(bset->div[i][2 + nparam + d + j]))
352                                 continue;
353                         switch (div_purity[j]) {
354                         case PURE_PARAM: p = 1; break;
355                         case PURE_VAR: v = 1; break;
356                         default: p = v = 1; break;
357                         }
358                 }
359                 div_purity[i] = v ? p ? IMPURE : PURE_VAR : PURE_PARAM;
360         }
361
362         return div_purity;
363 }
364
365 /* Given a path with the as yet unconstrained length at position "pos",
366  * check if setting the length to zero results in only the identity
367  * mapping.
368  */
369 int empty_path_is_identity(__isl_keep isl_basic_map *path, unsigned pos)
370 {
371         isl_basic_map *test = NULL;
372         isl_basic_map *id = NULL;
373         int k;
374         int is_id;
375
376         test = isl_basic_map_copy(path);
377         test = isl_basic_map_extend_constraints(test, 1, 0);
378         k = isl_basic_map_alloc_equality(test);
379         if (k < 0)
380                 goto error;
381         isl_seq_clr(test->eq[k], 1 + isl_basic_map_total_dim(test));
382         isl_int_set_si(test->eq[k][pos], 1);
383         id = isl_basic_map_identity(isl_dim_domain(isl_basic_map_get_dim(path)));
384         is_id = isl_basic_map_is_subset(test, id);
385         isl_basic_map_free(test);
386         isl_basic_map_free(id);
387         return is_id;
388 error:
389         isl_basic_map_free(test);
390         return -1;
391 }
392
393 __isl_give isl_basic_map *add_delta_constraints(__isl_take isl_basic_map *path,
394         __isl_keep isl_basic_set *delta, unsigned off, unsigned nparam,
395         unsigned d, int *div_purity, int eq)
396 {
397         int i, k;
398         int n = eq ? delta->n_eq : delta->n_ineq;
399         isl_int **delta_c = eq ? delta->eq : delta->ineq;
400         isl_int **path_c = eq ? path->eq : path->ineq;
401         unsigned n_div;
402
403         n_div = isl_basic_set_dim(delta, isl_dim_div);
404
405         for (i = 0; i < n; ++i) {
406                 int p = purity(delta, delta_c[i], div_purity, eq);
407                 if (p < 0)
408                         goto error;
409                 if (p == IMPURE)
410                         continue;
411                 if (eq)
412                         k = isl_basic_map_alloc_equality(path);
413                 else
414                         k = isl_basic_map_alloc_inequality(path);
415                 if (k < 0)
416                         goto error;
417                 isl_seq_clr(path_c[k], 1 + isl_basic_map_total_dim(path));
418                 if (p == PURE_VAR) {
419                         isl_seq_cpy(path_c[k] + off,
420                                     delta_c[i] + 1 + nparam, d);
421                         isl_int_set(path_c[k][off + d], delta_c[i][0]);
422                 } else if (p == PURE_PARAM) {
423                         isl_seq_cpy(path_c[k], delta_c[i], 1 + nparam);
424                 } else {
425                         isl_seq_cpy(path_c[k] + off,
426                                     delta_c[i] + 1 + nparam, d);
427                         isl_seq_cpy(path_c[k], delta_c[i], 1 + nparam);
428                 }
429                 isl_seq_cpy(path_c[k] + off - n_div,
430                             delta_c[i] + 1 + nparam + d, n_div);
431         }
432
433         return path;
434 error:
435         isl_basic_map_free(path);
436         return NULL;
437 }
438
439 /* Given a set of offsets "delta", construct a relation of the
440  * given dimension specification (Z^{n+1} -> Z^{n+1}) that
441  * is an overapproximation of the relations that
442  * maps an element x to any element that can be reached
443  * by taking a non-negative number of steps along any of
444  * the elements in "delta".
445  * That is, construct an approximation of
446  *
447  *      { [x] -> [y] : exists f \in \delta, k \in Z :
448  *                                      y = x + k [f, 1] and k >= 0 }
449  *
450  * For any element in this relation, the number of steps taken
451  * is equal to the difference in the final coordinates.
452  *
453  * In particular, let delta be defined as
454  *
455  *      \delta = [p] -> { [x] : A x + a >= and B p + b >= 0 and
456  *                              C x + C'p + c >= 0 and
457  *                              D x + D'p + d >= 0 }
458  *
459  * where the constraints C x + C'p + c >= 0 are such that the parametric
460  * constant term of each constraint j, "C_j x + C'_j p + c_j",
461  * can never attain positive values, then the relation is constructed as
462  *
463  *      { [x] -> [y] : exists [f, k] \in Z^{n+1} : y = x + f and
464  *                      A f + k a >= 0 and B p + b >= 0 and
465  *                      C f + C'p + c >= 0 and k >= 1 }
466  *      union { [x] -> [x] }
467  *
468  * If the zero-length paths happen to correspond exactly to the identity
469  * mapping, then we return
470  *
471  *      { [x] -> [y] : exists [f, k] \in Z^{n+1} : y = x + f and
472  *                      A f + k a >= 0 and B p + b >= 0 and
473  *                      C f + C'p + c >= 0 and k >= 0 }
474  *
475  * instead.
476  *
477  * Existentially quantified variables in \delta are handled by
478  * classifying them as independent of the parameters, purely
479  * parameter dependent and others.  Constraints containing
480  * any of the other existentially quantified variables are removed.
481  * This is safe, but leads to an additional overapproximation.
482  */
483 static __isl_give isl_map *path_along_delta(__isl_take isl_dim *dim,
484         __isl_take isl_basic_set *delta)
485 {
486         isl_basic_map *path = NULL;
487         unsigned d;
488         unsigned n_div;
489         unsigned nparam;
490         unsigned off;
491         int i, k;
492         int is_id;
493         int *div_purity = NULL;
494
495         if (!delta)
496                 goto error;
497         n_div = isl_basic_set_dim(delta, isl_dim_div);
498         d = isl_basic_set_dim(delta, isl_dim_set);
499         nparam = isl_basic_set_dim(delta, isl_dim_param);
500         path = isl_basic_map_alloc_dim(isl_dim_copy(dim), n_div + d + 1,
501                         d + 1 + delta->n_eq, delta->n_ineq + 1);
502         off = 1 + nparam + 2 * (d + 1) + n_div;
503
504         for (i = 0; i < n_div + d + 1; ++i) {
505                 k = isl_basic_map_alloc_div(path);
506                 if (k < 0)
507                         goto error;
508                 isl_int_set_si(path->div[k][0], 0);
509         }
510
511         for (i = 0; i < d + 1; ++i) {
512                 k = isl_basic_map_alloc_equality(path);
513                 if (k < 0)
514                         goto error;
515                 isl_seq_clr(path->eq[k], 1 + isl_basic_map_total_dim(path));
516                 isl_int_set_si(path->eq[k][1 + nparam + i], 1);
517                 isl_int_set_si(path->eq[k][1 + nparam + d + 1 + i], -1);
518                 isl_int_set_si(path->eq[k][off + i], 1);
519         }
520
521         div_purity = get_div_purity(delta);
522         if (!div_purity)
523                 goto error;
524
525         path = add_delta_constraints(path, delta, off, nparam, d, div_purity, 1);
526         path = add_delta_constraints(path, delta, off, nparam, d, div_purity, 0);
527
528         is_id = empty_path_is_identity(path, off + d);
529         if (is_id < 0)
530                 goto error;
531
532         k = isl_basic_map_alloc_inequality(path);
533         if (k < 0)
534                 goto error;
535         isl_seq_clr(path->ineq[k], 1 + isl_basic_map_total_dim(path));
536         if (!is_id)
537                 isl_int_set_si(path->ineq[k][0], -1);
538         isl_int_set_si(path->ineq[k][off + d], 1);
539                         
540         free(div_purity);
541         isl_basic_set_free(delta);
542         path = isl_basic_map_finalize(path);
543         if (is_id) {
544                 isl_dim_free(dim);
545                 return isl_map_from_basic_map(path);
546         }
547         return isl_basic_map_union(path,
548                                 isl_basic_map_identity(isl_dim_domain(dim)));
549 error:
550         free(div_purity);
551         isl_dim_free(dim);
552         isl_basic_set_free(delta);
553         isl_basic_map_free(path);
554         return NULL;
555 }
556
557 /* Given a dimenion specification Z^{n+1} -> Z^{n+1} and a parameter "param",
558  * construct a map that equates the parameter to the difference
559  * in the final coordinates and imposes that this difference is positive.
560  * That is, construct
561  *
562  *      { [x,x_s] -> [y,y_s] : k = y_s - x_s > 0 }
563  */
564 static __isl_give isl_map *equate_parameter_to_length(__isl_take isl_dim *dim,
565         unsigned param)
566 {
567         struct isl_basic_map *bmap;
568         unsigned d;
569         unsigned nparam;
570         int k;
571
572         d = isl_dim_size(dim, isl_dim_in);
573         nparam = isl_dim_size(dim, isl_dim_param);
574         bmap = isl_basic_map_alloc_dim(dim, 0, 1, 1);
575         k = isl_basic_map_alloc_equality(bmap);
576         if (k < 0)
577                 goto error;
578         isl_seq_clr(bmap->eq[k], 1 + isl_basic_map_total_dim(bmap));
579         isl_int_set_si(bmap->eq[k][1 + param], -1);
580         isl_int_set_si(bmap->eq[k][1 + nparam + d - 1], -1);
581         isl_int_set_si(bmap->eq[k][1 + nparam + d + d - 1], 1);
582
583         k = isl_basic_map_alloc_inequality(bmap);
584         if (k < 0)
585                 goto error;
586         isl_seq_clr(bmap->ineq[k], 1 + isl_basic_map_total_dim(bmap));
587         isl_int_set_si(bmap->ineq[k][1 + param], 1);
588         isl_int_set_si(bmap->ineq[k][0], -1);
589
590         bmap = isl_basic_map_finalize(bmap);
591         return isl_map_from_basic_map(bmap);
592 error:
593         isl_basic_map_free(bmap);
594         return NULL;
595 }
596
597 /* Check whether "path" is acyclic, where the last coordinates of domain
598  * and range of path encode the number of steps taken.
599  * That is, check whether
600  *
601  *      { d | d = y - x and (x,y) in path }
602  *
603  * does not contain any element with positive last coordinate (positive length)
604  * and zero remaining coordinates (cycle).
605  */
606 static int is_acyclic(__isl_take isl_map *path)
607 {
608         int i;
609         int acyclic;
610         unsigned dim;
611         struct isl_set *delta;
612
613         delta = isl_map_deltas(path);
614         dim = isl_set_dim(delta, isl_dim_set);
615         for (i = 0; i < dim; ++i) {
616                 if (i == dim -1)
617                         delta = isl_set_lower_bound_si(delta, isl_dim_set, i, 1);
618                 else
619                         delta = isl_set_fix_si(delta, isl_dim_set, i, 0);
620         }
621
622         acyclic = isl_set_is_empty(delta);
623         isl_set_free(delta);
624
625         return acyclic;
626 }
627
628 /* Given a union of basic maps R = \cup_i R_i \subseteq D \times D
629  * and a dimension specification (Z^{n+1} -> Z^{n+1}),
630  * construct a map that is an overapproximation of the map
631  * that takes an element from the space D \times Z to another
632  * element from the same space, such that the first n coordinates of the
633  * difference between them is a sum of differences between images
634  * and pre-images in one of the R_i and such that the last coordinate
635  * is equal to the number of steps taken.
636  * That is, let
637  *
638  *      \Delta_i = { y - x | (x, y) in R_i }
639  *
640  * then the constructed map is an overapproximation of
641  *
642  *      { (x) -> (x + d) | \exists k_i >= 0, \delta_i \in \Delta_i :
643  *                              d = (\sum_i k_i \delta_i, \sum_i k_i) }
644  *
645  * The elements of the singleton \Delta_i's are collected as the
646  * rows of the steps matrix.  For all these \Delta_i's together,
647  * a single path is constructed.
648  * For each of the other \Delta_i's, we compute an overapproximation
649  * of the paths along elements of \Delta_i.
650  * Since each of these paths performs an addition, composition is
651  * symmetric and we can simply compose all resulting paths in any order.
652  */
653 static __isl_give isl_map *construct_extended_path(__isl_take isl_dim *dim,
654         __isl_keep isl_map *map, int *project)
655 {
656         struct isl_mat *steps = NULL;
657         struct isl_map *path = NULL;
658         unsigned d;
659         int i, j, n;
660
661         d = isl_map_dim(map, isl_dim_in);
662
663         path = isl_map_identity(isl_dim_domain(isl_dim_copy(dim)));
664
665         steps = isl_mat_alloc(map->ctx, map->n, d);
666         if (!steps)
667                 goto error;
668
669         n = 0;
670         for (i = 0; i < map->n; ++i) {
671                 struct isl_basic_set *delta;
672
673                 delta = isl_basic_map_deltas(isl_basic_map_copy(map->p[i]));
674
675                 for (j = 0; j < d; ++j) {
676                         int fixed;
677
678                         fixed = isl_basic_set_fast_dim_is_fixed(delta, j,
679                                                             &steps->row[n][j]);
680                         if (fixed < 0) {
681                                 isl_basic_set_free(delta);
682                                 goto error;
683                         }
684                         if (!fixed)
685                                 break;
686                 }
687
688
689                 if (j < d) {
690                         path = isl_map_apply_range(path,
691                                 path_along_delta(isl_dim_copy(dim), delta));
692                         path = isl_map_coalesce(path);
693                 } else {
694                         isl_basic_set_free(delta);
695                         ++n;
696                 }
697         }
698
699         if (n > 0) {
700                 steps->n_row = n;
701                 path = isl_map_apply_range(path,
702                                 path_along_steps(isl_dim_copy(dim), steps));
703         }
704
705         if (project && *project) {
706                 *project = is_acyclic(isl_map_copy(path));
707                 if (*project < 0)
708                         goto error;
709         }
710
711         isl_dim_free(dim);
712         isl_mat_free(steps);
713         return path;
714 error:
715         isl_dim_free(dim);
716         isl_mat_free(steps);
717         isl_map_free(path);
718         return NULL;
719 }
720
721 /* Given a union of basic maps R = \cup_i R_i \subseteq D \times D
722  * and a dimension specification (Z^{n+1} -> Z^{n+1}),
723  * construct a map that is the union of the identity map and
724  * an overapproximation of the map
725  * that takes an element from the dom R \times Z to an
726  * element from ran R \times Z, such that the first n coordinates of the
727  * difference between them is a sum of differences between images
728  * and pre-images in one of the R_i and such that the last coordinate
729  * is equal to the number of steps taken.
730  * That is, let
731  *
732  *      \Delta_i = { y - x | (x, y) in R_i }
733  *
734  * then the constructed map is an overapproximation of
735  *
736  *      { (x) -> (x + d) | \exists k_i >= 0, \delta_i \in \Delta_i :
737  *                              d = (\sum_i k_i \delta_i, \sum_i k_i) and
738  *                              x in dom R and x + d in ran R } union
739  *      { (x) -> (x) }
740  */
741 static __isl_give isl_map *construct_component(__isl_take isl_dim *dim,
742         __isl_keep isl_map *map, int *exact, int project)
743 {
744         struct isl_set *domain = NULL;
745         struct isl_set *range = NULL;
746         struct isl_set *overlap;
747         struct isl_map *app = NULL;
748         struct isl_map *path = NULL;
749
750         domain = isl_map_domain(isl_map_copy(map));
751         domain = isl_set_coalesce(domain);
752         range = isl_map_range(isl_map_copy(map));
753         range = isl_set_coalesce(range);
754         overlap = isl_set_intersect(isl_set_copy(domain), isl_set_copy(range));
755         if (isl_set_is_empty(overlap) == 1) {
756                 isl_set_free(domain);
757                 isl_set_free(range);
758                 isl_set_free(overlap);
759
760                 map = isl_map_copy(map);
761                 map = isl_map_add(map, isl_dim_in, 1);
762                 map = isl_map_add(map, isl_dim_out, 1);
763                 map = set_path_length(map, 1, 1);
764                 map = isl_map_union(map, isl_map_identity(isl_dim_domain(dim)));
765                 return map;
766         }
767         isl_set_free(overlap);
768         app = isl_map_from_domain_and_range(domain, range);
769         app = isl_map_add(app, isl_dim_in, 1);
770         app = isl_map_add(app, isl_dim_out, 1);
771
772         path = construct_extended_path(isl_dim_copy(dim), map,
773                                         exact && *exact ? &project : NULL);
774         app = isl_map_intersect(app, path);
775
776         if (exact && *exact &&
777             (*exact = check_exactness(isl_map_copy(map), isl_map_copy(app),
778                                       project)) < 0)
779                 goto error;
780
781         return isl_map_union(app, isl_map_identity(isl_dim_domain(dim)));
782 error:
783         isl_dim_free(dim);
784         isl_map_free(app);
785         return NULL;
786 }
787
788 /* Structure for representing the nodes in the graph being traversed
789  * using Tarjan's algorithm.
790  * index represents the order in which nodes are visited.
791  * min_index is the index of the root of a (sub)component.
792  * on_stack indicates whether the node is currently on the stack.
793  */
794 struct basic_map_sort_node {
795         int index;
796         int min_index;
797         int on_stack;
798 };
799 /* Structure for representing the graph being traversed
800  * using Tarjan's algorithm.
801  * len is the number of nodes
802  * node is an array of nodes
803  * stack contains the nodes on the path from the root to the current node
804  * sp is the stack pointer
805  * index is the index of the last node visited
806  * order contains the elements of the components separated by -1
807  * op represents the current position in order
808  */
809 struct basic_map_sort {
810         int len;
811         struct basic_map_sort_node *node;
812         int *stack;
813         int sp;
814         int index;
815         int *order;
816         int op;
817 };
818
819 static void basic_map_sort_free(struct basic_map_sort *s)
820 {
821         if (!s)
822                 return;
823         free(s->node);
824         free(s->stack);
825         free(s->order);
826         free(s);
827 }
828
829 static struct basic_map_sort *basic_map_sort_alloc(struct isl_ctx *ctx, int len)
830 {
831         struct basic_map_sort *s;
832         int i;
833
834         s = isl_calloc_type(ctx, struct basic_map_sort);
835         if (!s)
836                 return NULL;
837         s->len = len;
838         s->node = isl_alloc_array(ctx, struct basic_map_sort_node, len);
839         if (!s->node)
840                 goto error;
841         for (i = 0; i < len; ++i)
842                 s->node[i].index = -1;
843         s->stack = isl_alloc_array(ctx, int, len);
844         if (!s->stack)
845                 goto error;
846         s->order = isl_alloc_array(ctx, int, 2 * len);
847         if (!s->order)
848                 goto error;
849
850         s->sp = 0;
851         s->index = 0;
852         s->op = 0;
853
854         return s;
855 error:
856         basic_map_sort_free(s);
857         return NULL;
858 }
859
860 /* Check whether in the computation of the transitive closure
861  * "bmap1" (R_1) should follow (or be part of the same component as)
862  * "bmap2" (R_2).
863  *
864  * That is check whether
865  *
866  *      R_1 \circ R_2
867  *
868  * is a subset of
869  *
870  *      R_2 \circ R_1
871  *
872  * If so, then there is no reason for R_1 to immediately follow R_2
873  * in any path.
874  */
875 static int basic_map_follows(__isl_keep isl_basic_map *bmap1,
876         __isl_keep isl_basic_map *bmap2)
877 {
878         struct isl_map *map12 = NULL;
879         struct isl_map *map21 = NULL;
880         int subset;
881
882         map21 = isl_map_from_basic_map(
883                         isl_basic_map_apply_range(
884                                 isl_basic_map_copy(bmap2),
885                                 isl_basic_map_copy(bmap1)));
886         subset = isl_map_is_empty(map21);
887         if (subset < 0)
888                 goto error;
889         if (subset) {
890                 isl_map_free(map21);
891                 return 0;
892         }
893
894         map12 = isl_map_from_basic_map(
895                         isl_basic_map_apply_range(
896                                 isl_basic_map_copy(bmap1),
897                                 isl_basic_map_copy(bmap2)));
898
899         subset = isl_map_is_subset(map21, map12);
900
901         isl_map_free(map12);
902         isl_map_free(map21);
903
904         return subset < 0 ? -1 : !subset;
905 error:
906         isl_map_free(map21);
907         return -1;
908 }
909
910 /* Perform Tarjan's algorithm for computing the strongly connected components
911  * in the graph with the disjuncts of "map" as vertices and with an
912  * edge between any pair of disjuncts such that the first has
913  * to be applied after the second.
914  */
915 static int power_components_tarjan(struct basic_map_sort *s,
916         __isl_keep isl_map *map, int i)
917 {
918         int j;
919
920         s->node[i].index = s->index;
921         s->node[i].min_index = s->index;
922         s->node[i].on_stack = 1;
923         s->index++;
924         s->stack[s->sp++] = i;
925
926         for (j = s->len - 1; j >= 0; --j) {
927                 int f;
928
929                 if (j == i)
930                         continue;
931                 if (s->node[j].index >= 0 &&
932                         (!s->node[j].on_stack ||
933                          s->node[j].index > s->node[i].min_index))
934                         continue;
935
936                 f = basic_map_follows(map->p[i], map->p[j]);
937                 if (f < 0)
938                         return -1;
939                 if (!f)
940                         continue;
941
942                 if (s->node[j].index < 0) {
943                         power_components_tarjan(s, map, j);
944                         if (s->node[j].min_index < s->node[i].min_index)
945                                 s->node[i].min_index = s->node[j].min_index;
946                 } else if (s->node[j].index < s->node[i].min_index)
947                         s->node[i].min_index = s->node[j].index;
948         }
949
950         if (s->node[i].index != s->node[i].min_index)
951                 return 0;
952
953         do {
954                 j = s->stack[--s->sp];
955                 s->node[j].on_stack = 0;
956                 s->order[s->op++] = j;
957         } while (j != i);
958         s->order[s->op++] = -1;
959
960         return 0;
961 }
962
963 /* Given a union of basic maps R = \cup_i R_i \subseteq D \times D
964  * and a dimension specification (Z^{n+1} -> Z^{n+1}),
965  * construct a map that is the union of the identity map and
966  * an overapproximation of the map
967  * that takes an element from the dom R \times Z to an
968  * element from ran R \times Z, such that the first n coordinates of the
969  * difference between them is a sum of differences between images
970  * and pre-images in one of the R_i and such that the last coordinate
971  * is equal to the number of steps taken.
972  * That is, let
973  *
974  *      \Delta_i = { y - x | (x, y) in R_i }
975  *
976  * then the constructed map is an overapproximation of
977  *
978  *      { (x) -> (x + d) | \exists k_i >= 0, \delta_i \in \Delta_i :
979  *                              d = (\sum_i k_i \delta_i, \sum_i k_i) and
980  *                              x in dom R and x + d in ran R } union
981  *      { (x) -> (x) }
982  *
983  * We first split the map into strongly connected components, perform
984  * the above on each component and the join the results in the correct
985  * order.  The power of each of the components needs to be extended
986  * with the identity map because a path in the global result need
987  * not go through every component.
988  * The final result will then also contain the identity map, but
989  * this part will be removed when the length of the path is forced
990  * to be strictly positive.
991  */
992 static __isl_give isl_map *construct_power_components(__isl_take isl_dim *dim,
993         __isl_keep isl_map *map, int *exact, int project)
994 {
995         int i, n;
996         struct isl_map *path = NULL;
997         struct basic_map_sort *s = NULL;
998
999         if (!map)
1000                 goto error;
1001         if (map->n <= 1)
1002                 return construct_component(dim, map, exact, project);
1003
1004         s = basic_map_sort_alloc(map->ctx, map->n);
1005         if (!s)
1006                 goto error;
1007         for (i = map->n - 1; i >= 0; --i) {
1008                 if (s->node[i].index >= 0)
1009                         continue;
1010                 if (power_components_tarjan(s, map, i) < 0)
1011                         goto error;
1012         }
1013
1014         i = 0;
1015         n = map->n;
1016         path = isl_map_identity(isl_dim_domain(isl_dim_copy(dim)));
1017         while (n) {
1018                 struct isl_map *comp;
1019                 comp = isl_map_alloc_dim(isl_map_get_dim(map), n, 0);
1020                 while (s->order[i] != -1) {
1021                         comp = isl_map_add_basic_map(comp,
1022                                     isl_basic_map_copy(map->p[s->order[i]]));
1023                         --n;
1024                         ++i;
1025                 }
1026                 path = isl_map_apply_range(path,
1027                             construct_component(isl_dim_copy(dim), comp,
1028                                                 exact, project));
1029                 isl_map_free(comp);
1030                 ++i;
1031         }
1032
1033         basic_map_sort_free(s);
1034         isl_dim_free(dim);
1035
1036         return path;
1037 error:
1038         basic_map_sort_free(s);
1039         isl_dim_free(dim);
1040         return NULL;
1041 }
1042
1043 /* Given a union of basic maps R = \cup_i R_i \subseteq D \times D,
1044  * construct a map that is an overapproximation of the map
1045  * that takes an element from the space D to another
1046  * element from the same space, such that the difference between
1047  * them is a strictly positive sum of differences between images
1048  * and pre-images in one of the R_i.
1049  * The number of differences in the sum is equated to parameter "param".
1050  * That is, let
1051  *
1052  *      \Delta_i = { y - x | (x, y) in R_i }
1053  *
1054  * then the constructed map is an overapproximation of
1055  *
1056  *      { (x) -> (x + d) | \exists k_i >= 0, \delta_i \in \Delta_i :
1057  *                              d = \sum_i k_i \delta_i and k = \sum_i k_i > 0 }
1058  * or
1059  *
1060  *      { (x) -> (x + d) | \exists k_i >= 0, \delta_i \in \Delta_i :
1061  *                              d = \sum_i k_i \delta_i and \sum_i k_i > 0 }
1062  *
1063  * if "project" is set.
1064  *
1065  * We first construct an extended mapping with an extra coordinate
1066  * that indicates the number of steps taken.  In particular,
1067  * the difference in the last coordinate is equal to the number
1068  * of steps taken to move from a domain element to the corresponding
1069  * image element(s).
1070  * In the final step, this difference is equated to the parameter "param"
1071  * and made positive (unless project is set).
1072  * The extra coordinates are subsequently projected out.
1073  */
1074 static __isl_give isl_map *construct_power(__isl_keep isl_map *map,
1075         unsigned param, int *exact, int project)
1076 {
1077         struct isl_map *app = NULL;
1078         struct isl_map *diff;
1079         struct isl_dim *dim = NULL;
1080         unsigned d;
1081
1082         if (!map)
1083                 return NULL;
1084
1085         dim = isl_map_get_dim(map);
1086
1087         d = isl_dim_size(dim, isl_dim_in);
1088         dim = isl_dim_add(dim, isl_dim_in, 1);
1089         dim = isl_dim_add(dim, isl_dim_out, 1);
1090
1091         app = construct_power_components(isl_dim_copy(dim), map,
1092                                         exact, project);
1093
1094         if (project) {
1095                 isl_dim_free(dim);
1096                 app = set_path_length(app, 0, 1);
1097         } else {
1098                 diff = equate_parameter_to_length(dim, param);
1099                 app = isl_map_intersect(app, diff);
1100         }
1101         app = isl_map_project_out(app, isl_dim_in, d, 1);
1102         app = isl_map_project_out(app, isl_dim_out, d, 1);
1103
1104         return app;
1105 }
1106
1107 /* Compute the positive powers of "map", or an overapproximation.
1108  * The power is given by parameter "param".  If the result is exact,
1109  * then *exact is set to 1.
1110  *
1111  * If project is set, then we are actually interested in the transitive
1112  * closure, so we can use a more relaxed exactness check.
1113  * The lengths of the paths are also projected out instead of being
1114  * equated to "param" (which is then ignored in this case).
1115  */
1116 static __isl_give isl_map *map_power(__isl_take isl_map *map, unsigned param,
1117         int *exact, int project)
1118 {
1119         struct isl_map *app = NULL;
1120
1121         if (exact)
1122                 *exact = 1;
1123
1124         map = isl_map_coalesce(map);
1125         if (!map)
1126                 return NULL;
1127
1128         if (isl_map_fast_is_empty(map))
1129                 return map;
1130
1131         isl_assert(map->ctx, project || param < isl_map_dim(map, isl_dim_param),
1132                 goto error);
1133         isl_assert(map->ctx,
1134                 isl_map_dim(map, isl_dim_in) == isl_map_dim(map, isl_dim_out),
1135                 goto error);
1136
1137         app = construct_power(map, param, exact, project);
1138
1139         isl_map_free(map);
1140         return app;
1141 error:
1142         isl_map_free(map);
1143         isl_map_free(app);
1144         return NULL;
1145 }
1146
1147 /* Compute the positive powers of "map", or an overapproximation.
1148  * The power is given by parameter "param".  If the result is exact,
1149  * then *exact is set to 1.
1150  */
1151 __isl_give isl_map *isl_map_power(__isl_take isl_map *map, unsigned param,
1152         int *exact)
1153 {
1154         return map_power(map, param, exact, 0);
1155 }
1156
1157 /* Check whether equality i of bset is a pure stride constraint
1158  * on a single dimensions, i.e., of the form
1159  *
1160  *      v = k e
1161  *
1162  * with k a constant and e an existentially quantified variable.
1163  */
1164 static int is_eq_stride(__isl_keep isl_basic_set *bset, int i)
1165 {
1166         int k;
1167         unsigned nparam;
1168         unsigned d;
1169         unsigned n_div;
1170         int pos1;
1171         int pos2;
1172
1173         if (!bset)
1174                 return -1;
1175
1176         if (!isl_int_is_zero(bset->eq[i][0]))
1177                 return 0;
1178
1179         nparam = isl_basic_set_dim(bset, isl_dim_param);
1180         d = isl_basic_set_dim(bset, isl_dim_set);
1181         n_div = isl_basic_set_dim(bset, isl_dim_div);
1182
1183         if (isl_seq_first_non_zero(bset->eq[i] + 1, nparam) != -1)
1184                 return 0;
1185         pos1 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam, d);
1186         if (pos1 == -1)
1187                 return 0;
1188         if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + pos1 + 1, 
1189                                         d - pos1 - 1) != -1)
1190                 return 0;
1191
1192         pos2 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d, n_div);
1193         if (pos2 == -1)
1194                 return 0;
1195         if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d  + pos2 + 1,
1196                                    n_div - pos2 - 1) != -1)
1197                 return 0;
1198         if (!isl_int_is_one(bset->eq[i][1 + nparam + pos1]) &&
1199             !isl_int_is_negone(bset->eq[i][1 + nparam + pos1]))
1200                 return 0;
1201
1202         return 1;
1203 }
1204
1205 /* Given a map, compute the smallest superset of this map that is of the form
1206  *
1207  *      { i -> j : L <= j - i <= U and exists a_p: j_p - i_p = M_p a_p }
1208  *
1209  * (where p ranges over the (non-parametric) dimensions),
1210  * compute the transitive closure of this map, i.e.,
1211  *
1212  *      { i -> j : exists k > 0:
1213  *              k L <= j - i <= k U and exists a: j_p - i_p = M_p a_p }
1214  *
1215  * and intersect domain and range of this transitive closure with
1216  * domain and range of the original map.
1217  *
1218  * If with_id is set, then try to include as much of the identity mapping
1219  * as possible, by computing
1220  *
1221  *      { i -> j : exists k >= 0:
1222  *              k L <= j - i <= k U and exists a: j_p - i_p = M_p a_p }
1223  *
1224  * instead (i.e., allow k = 0) and by intersecting domain and range
1225  * with the union of the domain and the range of the original map.
1226  *
1227  * In practice, we compute the difference set
1228  *
1229  *      delta  = { j - i | i -> j in map },
1230  *
1231  * look for stride constraint on the individual dimensions and compute
1232  * (constant) lower and upper bounds for each individual dimension,
1233  * adding a constraint for each bound not equal to infinity.
1234  */
1235 static __isl_give isl_map *box_closure(__isl_take isl_map *map, int with_id)
1236 {
1237         int i;
1238         int k;
1239         unsigned d;
1240         unsigned nparam;
1241         unsigned total;
1242         isl_dim *dim;
1243         isl_set *delta;
1244         isl_set *domain = NULL;
1245         isl_set *range = NULL;
1246         isl_map *app = NULL;
1247         isl_basic_set *aff = NULL;
1248         isl_basic_map *bmap = NULL;
1249         isl_vec *obj = NULL;
1250         isl_int opt;
1251
1252         isl_int_init(opt);
1253
1254         delta = isl_map_deltas(isl_map_copy(map));
1255
1256         aff = isl_set_affine_hull(isl_set_copy(delta));
1257         if (!aff)
1258                 goto error;
1259         dim = isl_map_get_dim(map);
1260         d = isl_dim_size(dim, isl_dim_in);
1261         nparam = isl_dim_size(dim, isl_dim_param);
1262         total = isl_dim_total(dim);
1263         bmap = isl_basic_map_alloc_dim(dim,
1264                                         aff->n_div + 1, aff->n_div, 2 * d + 1);
1265         for (i = 0; i < aff->n_div + 1; ++i) {
1266                 k = isl_basic_map_alloc_div(bmap);
1267                 if (k < 0)
1268                         goto error;
1269                 isl_int_set_si(bmap->div[k][0], 0);
1270         }
1271         for (i = 0; i < aff->n_eq; ++i) {
1272                 if (!is_eq_stride(aff, i))
1273                         continue;
1274                 k = isl_basic_map_alloc_equality(bmap);
1275                 if (k < 0)
1276                         goto error;
1277                 isl_seq_clr(bmap->eq[k], 1 + nparam);
1278                 isl_seq_cpy(bmap->eq[k] + 1 + nparam + d,
1279                                 aff->eq[i] + 1 + nparam, d);
1280                 isl_seq_neg(bmap->eq[k] + 1 + nparam,
1281                                 aff->eq[i] + 1 + nparam, d);
1282                 isl_seq_cpy(bmap->eq[k] + 1 + nparam + 2 * d,
1283                                 aff->eq[i] + 1 + nparam + d, aff->n_div);
1284                 isl_int_set_si(bmap->eq[k][1 + total + aff->n_div], 0);
1285         }
1286         obj = isl_vec_alloc(map->ctx, 1 + nparam + d);
1287         if (!obj)
1288                 goto error;
1289         isl_seq_clr(obj->el, 1 + nparam + d);
1290         for (i = 0; i < d; ++ i) {
1291                 enum isl_lp_result res;
1292
1293                 isl_int_set_si(obj->el[1 + nparam + i], 1);
1294
1295                 res = isl_set_solve_lp(delta, 0, obj->el, map->ctx->one, &opt,
1296                                         NULL, NULL);
1297                 if (res == isl_lp_error)
1298                         goto error;
1299                 if (res == isl_lp_ok) {
1300                         k = isl_basic_map_alloc_inequality(bmap);
1301                         if (k < 0)
1302                                 goto error;
1303                         isl_seq_clr(bmap->ineq[k],
1304                                         1 + nparam + 2 * d + bmap->n_div);
1305                         isl_int_set_si(bmap->ineq[k][1 + nparam + i], -1);
1306                         isl_int_set_si(bmap->ineq[k][1 + nparam + d + i], 1);
1307                         isl_int_neg(bmap->ineq[k][1 + nparam + 2 * d + aff->n_div], opt);
1308                 }
1309
1310                 res = isl_set_solve_lp(delta, 1, obj->el, map->ctx->one, &opt,
1311                                         NULL, NULL);
1312                 if (res == isl_lp_error)
1313                         goto error;
1314                 if (res == isl_lp_ok) {
1315                         k = isl_basic_map_alloc_inequality(bmap);
1316                         if (k < 0)
1317                                 goto error;
1318                         isl_seq_clr(bmap->ineq[k],
1319                                         1 + nparam + 2 * d + bmap->n_div);
1320                         isl_int_set_si(bmap->ineq[k][1 + nparam + i], 1);
1321                         isl_int_set_si(bmap->ineq[k][1 + nparam + d + i], -1);
1322                         isl_int_set(bmap->ineq[k][1 + nparam + 2 * d + aff->n_div], opt);
1323                 }
1324
1325                 isl_int_set_si(obj->el[1 + nparam + i], 0);
1326         }
1327         k = isl_basic_map_alloc_inequality(bmap);
1328         if (k < 0)
1329                 goto error;
1330         isl_seq_clr(bmap->ineq[k],
1331                         1 + nparam + 2 * d + bmap->n_div);
1332         if (!with_id)
1333                 isl_int_set_si(bmap->ineq[k][0], -1);
1334         isl_int_set_si(bmap->ineq[k][1 + nparam + 2 * d + aff->n_div], 1);
1335
1336         domain = isl_map_domain(isl_map_copy(map));
1337         domain = isl_set_coalesce(domain);
1338         range = isl_map_range(isl_map_copy(map));
1339         range = isl_set_coalesce(range);
1340         if (with_id) {
1341                 domain = isl_set_union(domain, range);
1342                 domain = isl_set_coalesce(domain);
1343                 range = isl_set_copy(domain);
1344         }
1345         app = isl_map_from_domain_and_range(domain, range);
1346
1347         isl_vec_free(obj);
1348         isl_basic_set_free(aff);
1349         isl_map_free(map);
1350         bmap = isl_basic_map_finalize(bmap);
1351         isl_set_free(delta);
1352         isl_int_clear(opt);
1353
1354         map = isl_map_from_basic_map(bmap);
1355         map = isl_map_intersect(map, app);
1356
1357         return map;
1358 error:
1359         isl_vec_free(obj);
1360         isl_basic_map_free(bmap);
1361         isl_basic_set_free(aff);
1362         isl_map_free(map);
1363         isl_set_free(delta);
1364         isl_int_clear(opt);
1365         return NULL;
1366 }
1367
1368 /* Check whether app is the transitive closure of map.
1369  * In particular, check that app is acyclic and, if so,
1370  * check that
1371  *
1372  *      app \subset (map \cup (map \circ app))
1373  */
1374 static int check_exactness_omega(__isl_keep isl_map *map,
1375         __isl_keep isl_map *app)
1376 {
1377         isl_set *delta;
1378         int i;
1379         int is_empty, is_exact;
1380         unsigned d;
1381         isl_map *test;
1382
1383         delta = isl_map_deltas(isl_map_copy(app));
1384         d = isl_set_dim(delta, isl_dim_set);
1385         for (i = 0; i < d; ++i)
1386                 delta = isl_set_fix_si(delta, isl_dim_set, i, 0);
1387         is_empty = isl_set_is_empty(delta);
1388         isl_set_free(delta);
1389         if (is_empty < 0)
1390                 return -1;
1391         if (!is_empty)
1392                 return 0;
1393
1394         test = isl_map_apply_range(isl_map_copy(app), isl_map_copy(map));
1395         test = isl_map_union(test, isl_map_copy(map));
1396         is_exact = isl_map_is_subset(app, test);
1397         isl_map_free(test);
1398
1399         return is_exact;
1400 }
1401
1402 /* Check if basic map M_i can be combined with all the other
1403  * basic maps such that
1404  *
1405  *      (\cup_j M_j)^+
1406  *
1407  * can be computed as
1408  *
1409  *      M_i \cup (\cup_{j \ne i} M_i^* \circ M_j \circ M_i^*)^+
1410  *
1411  * In particular, check if we can compute a compact representation
1412  * of
1413  *
1414  *              M_i^* \circ M_j \circ M_i^*
1415  *
1416  * for each j != i.
1417  * Let M_i^? be an extension of M_i^+ that allows paths
1418  * of length zero, i.e., the result of box_closure(., 1).
1419  * The criterion, as proposed by Kelly et al., is that
1420  * id = M_i^? - M_i^+ can be represented as a basic map
1421  * and that
1422  *
1423  *      id \circ M_j \circ id = M_j
1424  *
1425  * for each j != i.
1426  *
1427  * If this function returns 1, then tc and qc are set to
1428  * M_i^+ and M_i^?, respectively.
1429  */
1430 static int can_be_split_off(__isl_keep isl_map *map, int i,
1431         __isl_give isl_map **tc, __isl_give isl_map **qc)
1432 {
1433         isl_map *map_i, *id;
1434         int j = -1;
1435
1436         map_i = isl_map_from_basic_map(isl_basic_map_copy(map->p[i]));
1437         *tc = box_closure(isl_map_copy(map_i), 0);
1438         *qc = box_closure(map_i, 1);
1439         id = isl_map_subtract(isl_map_copy(*qc), isl_map_copy(*tc));
1440
1441         if (!id || !*qc)
1442                 goto error;
1443         if (id->n != 1 || (*qc)->n != 1)
1444                 goto done;
1445
1446         for (j = 0; j < map->n; ++j) {
1447                 isl_map *map_j, *test;
1448                 int is_ok;
1449
1450                 if (i == j)
1451                         continue;
1452                 map_j = isl_map_from_basic_map(
1453                                         isl_basic_map_copy(map->p[j]));
1454                 test = isl_map_apply_range(isl_map_copy(id),
1455                                                 isl_map_copy(map_j));
1456                 test = isl_map_apply_range(test, isl_map_copy(id));
1457                 is_ok = isl_map_is_equal(test, map_j);
1458                 isl_map_free(map_j);
1459                 isl_map_free(test);
1460                 if (is_ok < 0)
1461                         goto error;
1462                 if (!is_ok)
1463                         break;
1464         }
1465
1466 done:
1467         isl_map_free(id);
1468         if (j == map->n)
1469                 return 1;
1470
1471         isl_map_free(*qc);
1472         isl_map_free(*tc);
1473         *qc = NULL;
1474         *tc = NULL;
1475
1476         return 0;
1477 error:
1478         isl_map_free(id);
1479         isl_map_free(*qc);
1480         isl_map_free(*tc);
1481         *qc = NULL;
1482         *tc = NULL;
1483         return -1;
1484 }
1485
1486 /* Compute an overapproximation of the transitive closure of "map"
1487  * using a variation of the algorithm from
1488  * "Transitive Closure of Infinite Graphs and its Applications"
1489  * by Kelly et al.
1490  *
1491  * We first check whether we can can split of any basic map M_i and
1492  * compute
1493  *
1494  *      (\cup_j M_j)^+
1495  *
1496  * as
1497  *
1498  *      M_i \cup (\cup_{j \ne i} M_i^* \circ M_j \circ M_i^*)^+
1499  *
1500  * using a recursive call on the remaining map.
1501  *
1502  * If not, we simply call box_closure on the whole map.
1503  */
1504 static __isl_give isl_map *compute_closure_omega(__isl_take isl_map *map)
1505 {
1506         int i, j;
1507
1508         if (!map)
1509                 return NULL;
1510         if (map->n == 1)
1511                 return box_closure(map, 0);
1512
1513         map = isl_map_cow(map);
1514         if (!map)
1515                 goto error;
1516
1517         for (i = 0; i < map->n; ++i) {
1518                 int ok;
1519                 isl_map *qc, *tc;
1520                 ok = can_be_split_off(map, i, &tc, &qc);
1521                 if (ok < 0)
1522                         goto error;
1523                 if (!ok)
1524                         continue;
1525
1526                 isl_basic_map_free(map->p[i]);
1527                 if (i != map->n - 1)
1528                         map->p[i] = map->p[map->n - 1];
1529                 map->n--;
1530
1531                 map = isl_map_apply_range(isl_map_copy(qc), map);
1532                 map = isl_map_apply_range(map, qc);
1533
1534                 return isl_map_union(tc, compute_closure_omega(map));
1535         }
1536
1537         return box_closure(map, 0);
1538 error:
1539         isl_map_free(map);
1540         return NULL;
1541 }
1542
1543 /* Compute an overapproximation of the transitive closure of "map"
1544  * using a variation of the algorithm from
1545  * "Transitive Closure of Infinite Graphs and its Applications"
1546  * by Kelly et al. and check whether the result is definitely exact.
1547  */
1548 static __isl_give isl_map *transitive_closure_omega(__isl_take isl_map *map,
1549         int *exact)
1550 {
1551         isl_map *app;
1552
1553         app = compute_closure_omega(isl_map_copy(map));
1554
1555         if (exact)
1556                 *exact = check_exactness_omega(map, app);
1557
1558         isl_map_free(map);
1559         return app;
1560 }
1561
1562 /* Compute the transitive closure  of "map", or an overapproximation.
1563  * If the result is exact, then *exact is set to 1.
1564  * Simply use map_power to compute the powers of map, but tell
1565  * it to project out the lengths of the paths instead of equating
1566  * the length to a parameter.
1567  */
1568 __isl_give isl_map *isl_map_transitive_closure(__isl_take isl_map *map,
1569         int *exact)
1570 {
1571         unsigned param;
1572
1573         if (!map)
1574                 goto error;
1575
1576         if (map->ctx->opt->closure == ISL_CLOSURE_OMEGA)
1577                 return transitive_closure_omega(map, exact);
1578
1579         param = isl_map_dim(map, isl_dim_param);
1580         map = map_power(map, param, exact, 1);
1581
1582         return map;
1583 error:
1584         isl_map_free(map);
1585         return NULL;
1586 }