isl_vertices.c: is_independent: use isl_bool_ok
[isl.git] / isl_transitive_closure.c
blob81fe5a12312c9a08e5b91b4fdc4deb7502dbe6e7
1 /*
2 * Copyright 2010 INRIA Saclay
4 * Use of this software is governed by the MIT license
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 */
11 #include <isl_ctx_private.h>
12 #include <isl_map_private.h>
13 #include <isl/map.h>
14 #include <isl_seq.h>
15 #include <isl_space_private.h>
16 #include <isl_lp_private.h>
17 #include <isl/union_map.h>
18 #include <isl_mat_private.h>
19 #include <isl_vec_private.h>
20 #include <isl_options_private.h>
21 #include <isl_tarjan.h>
23 int isl_map_is_transitively_closed(__isl_keep isl_map *map)
25 isl_map *map2;
26 int closed;
28 map2 = isl_map_apply_range(isl_map_copy(map), isl_map_copy(map));
29 closed = isl_map_is_subset(map2, map);
30 isl_map_free(map2);
32 return closed;
35 int isl_union_map_is_transitively_closed(__isl_keep isl_union_map *umap)
37 isl_union_map *umap2;
38 int closed;
40 umap2 = isl_union_map_apply_range(isl_union_map_copy(umap),
41 isl_union_map_copy(umap));
42 closed = isl_union_map_is_subset(umap2, umap);
43 isl_union_map_free(umap2);
45 return closed;
48 /* Given a map that represents a path with the length of the path
49 * encoded as the difference between the last output coordindate
50 * and the last input coordinate, set this length to either
51 * exactly "length" (if "exactly" is set) or at least "length"
52 * (if "exactly" is not set).
54 static __isl_give isl_map *set_path_length(__isl_take isl_map *map,
55 int exactly, int length)
57 isl_space *space;
58 struct isl_basic_map *bmap;
59 isl_size d;
60 isl_size nparam;
61 isl_size total;
62 int k;
63 isl_int *c;
65 if (!map)
66 return NULL;
68 space = isl_map_get_space(map);
69 d = isl_space_dim(space, isl_dim_in);
70 nparam = isl_space_dim(space, isl_dim_param);
71 total = isl_space_dim(space, isl_dim_all);
72 if (d < 0 || nparam < 0 || total < 0)
73 space = isl_space_free(space);
74 bmap = isl_basic_map_alloc_space(space, 0, 1, 1);
75 if (exactly) {
76 k = isl_basic_map_alloc_equality(bmap);
77 if (k < 0)
78 goto error;
79 c = bmap->eq[k];
80 } else {
81 k = isl_basic_map_alloc_inequality(bmap);
82 if (k < 0)
83 goto error;
84 c = bmap->ineq[k];
86 isl_seq_clr(c, 1 + total);
87 isl_int_set_si(c[0], -length);
88 isl_int_set_si(c[1 + nparam + d - 1], -1);
89 isl_int_set_si(c[1 + nparam + d + d - 1], 1);
91 bmap = isl_basic_map_finalize(bmap);
92 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
94 return map;
95 error:
96 isl_basic_map_free(bmap);
97 isl_map_free(map);
98 return NULL;
101 /* Check whether the overapproximation of the power of "map" is exactly
102 * the power of "map". Let R be "map" and A_k the overapproximation.
103 * The approximation is exact if
105 * A_1 = R
106 * A_k = A_{k-1} \circ R k >= 2
108 * Since A_k is known to be an overapproximation, we only need to check
110 * A_1 \subset R
111 * A_k \subset A_{k-1} \circ R k >= 2
113 * In practice, "app" has an extra input and output coordinate
114 * to encode the length of the path. So, we first need to add
115 * this coordinate to "map" and set the length of the path to
116 * one.
118 static int check_power_exactness(__isl_take isl_map *map,
119 __isl_take isl_map *app)
121 int exact;
122 isl_map *app_1;
123 isl_map *app_2;
125 map = isl_map_add_dims(map, isl_dim_in, 1);
126 map = isl_map_add_dims(map, isl_dim_out, 1);
127 map = set_path_length(map, 1, 1);
129 app_1 = set_path_length(isl_map_copy(app), 1, 1);
131 exact = isl_map_is_subset(app_1, map);
132 isl_map_free(app_1);
134 if (!exact || exact < 0) {
135 isl_map_free(app);
136 isl_map_free(map);
137 return exact;
140 app_1 = set_path_length(isl_map_copy(app), 0, 1);
141 app_2 = set_path_length(app, 0, 2);
142 app_1 = isl_map_apply_range(map, app_1);
144 exact = isl_map_is_subset(app_2, app_1);
146 isl_map_free(app_1);
147 isl_map_free(app_2);
149 return exact;
152 /* Check whether the overapproximation of the power of "map" is exactly
153 * the power of "map", possibly after projecting out the power (if "project"
154 * is set).
156 * If "project" is set and if "steps" can only result in acyclic paths,
157 * then we check
159 * A = R \cup (A \circ R)
161 * where A is the overapproximation with the power projected out, i.e.,
162 * an overapproximation of the transitive closure.
163 * More specifically, since A is known to be an overapproximation, we check
165 * A \subset R \cup (A \circ R)
167 * Otherwise, we check if the power is exact.
169 * Note that "app" has an extra input and output coordinate to encode
170 * the length of the part. If we are only interested in the transitive
171 * closure, then we can simply project out these coordinates first.
173 static int check_exactness(__isl_take isl_map *map, __isl_take isl_map *app,
174 int project)
176 isl_map *test;
177 int exact;
178 isl_size d;
180 if (!project)
181 return check_power_exactness(map, app);
183 d = isl_map_dim(map, isl_dim_in);
184 if (d < 0)
185 app = isl_map_free(app);
186 app = set_path_length(app, 0, 1);
187 app = isl_map_project_out(app, isl_dim_in, d, 1);
188 app = isl_map_project_out(app, isl_dim_out, d, 1);
190 app = isl_map_reset_space(app, isl_map_get_space(map));
192 test = isl_map_apply_range(isl_map_copy(map), isl_map_copy(app));
193 test = isl_map_union(test, isl_map_copy(map));
195 exact = isl_map_is_subset(app, test);
197 isl_map_free(app);
198 isl_map_free(test);
200 isl_map_free(map);
202 return exact;
206 * The transitive closure implementation is based on the paper
207 * "Computing the Transitive Closure of a Union of Affine Integer
208 * Tuple Relations" by Anna Beletska, Denis Barthou, Wlodzimierz Bielecki and
209 * Albert Cohen.
212 /* Given a set of n offsets v_i (the rows of "steps"), construct a relation
213 * of the given dimension specification (Z^{n+1} -> Z^{n+1})
214 * that maps an element x to any element that can be reached
215 * by taking a non-negative number of steps along any of
216 * the extended offsets v'_i = [v_i 1].
217 * That is, construct
219 * { [x] -> [y] : exists k_i >= 0, y = x + \sum_i k_i v'_i }
221 * For any element in this relation, the number of steps taken
222 * is equal to the difference in the final coordinates.
224 static __isl_give isl_map *path_along_steps(__isl_take isl_space *space,
225 __isl_keep isl_mat *steps)
227 int i, j, k;
228 struct isl_basic_map *path = NULL;
229 isl_size d;
230 unsigned n;
231 isl_size nparam;
232 isl_size total;
234 d = isl_space_dim(space, isl_dim_in);
235 nparam = isl_space_dim(space, isl_dim_param);
236 if (d < 0 || nparam < 0 || !steps)
237 goto error;
239 n = steps->n_row;
241 path = isl_basic_map_alloc_space(isl_space_copy(space), n, d, n);
243 for (i = 0; i < n; ++i) {
244 k = isl_basic_map_alloc_div(path);
245 if (k < 0)
246 goto error;
247 isl_assert(steps->ctx, i == k, goto error);
248 isl_int_set_si(path->div[k][0], 0);
251 total = isl_basic_map_dim(path, isl_dim_all);
252 if (total < 0)
253 goto error;
254 for (i = 0; i < d; ++i) {
255 k = isl_basic_map_alloc_equality(path);
256 if (k < 0)
257 goto error;
258 isl_seq_clr(path->eq[k], 1 + total);
259 isl_int_set_si(path->eq[k][1 + nparam + i], 1);
260 isl_int_set_si(path->eq[k][1 + nparam + d + i], -1);
261 if (i == d - 1)
262 for (j = 0; j < n; ++j)
263 isl_int_set_si(path->eq[k][1 + nparam + 2 * d + j], 1);
264 else
265 for (j = 0; j < n; ++j)
266 isl_int_set(path->eq[k][1 + nparam + 2 * d + j],
267 steps->row[j][i]);
270 for (i = 0; i < n; ++i) {
271 k = isl_basic_map_alloc_inequality(path);
272 if (k < 0)
273 goto error;
274 isl_seq_clr(path->ineq[k], 1 + total);
275 isl_int_set_si(path->ineq[k][1 + nparam + 2 * d + i], 1);
278 isl_space_free(space);
280 path = isl_basic_map_simplify(path);
281 path = isl_basic_map_finalize(path);
282 return isl_map_from_basic_map(path);
283 error:
284 isl_space_free(space);
285 isl_basic_map_free(path);
286 return NULL;
289 #define IMPURE 0
290 #define PURE_PARAM 1
291 #define PURE_VAR 2
292 #define MIXED 3
294 /* Check whether the parametric constant term of constraint c is never
295 * positive in "bset".
297 static isl_bool parametric_constant_never_positive(
298 __isl_keep isl_basic_set *bset, isl_int *c, int *div_purity)
300 isl_size d;
301 isl_size n_div;
302 isl_size nparam;
303 isl_size total;
304 int i;
305 int k;
306 isl_bool empty;
308 n_div = isl_basic_set_dim(bset, isl_dim_div);
309 d = isl_basic_set_dim(bset, isl_dim_set);
310 nparam = isl_basic_set_dim(bset, isl_dim_param);
311 total = isl_basic_set_dim(bset, isl_dim_all);
312 if (n_div < 0 || d < 0 || nparam < 0 || total < 0)
313 return isl_bool_error;
315 bset = isl_basic_set_copy(bset);
316 bset = isl_basic_set_cow(bset);
317 bset = isl_basic_set_extend_constraints(bset, 0, 1);
318 k = isl_basic_set_alloc_inequality(bset);
319 if (k < 0)
320 goto error;
321 isl_seq_clr(bset->ineq[k], 1 + total);
322 isl_seq_cpy(bset->ineq[k], c, 1 + nparam);
323 for (i = 0; i < n_div; ++i) {
324 if (div_purity[i] != PURE_PARAM)
325 continue;
326 isl_int_set(bset->ineq[k][1 + nparam + d + i],
327 c[1 + nparam + d + i]);
329 isl_int_sub_ui(bset->ineq[k][0], bset->ineq[k][0], 1);
330 empty = isl_basic_set_is_empty(bset);
331 isl_basic_set_free(bset);
333 return empty;
334 error:
335 isl_basic_set_free(bset);
336 return isl_bool_error;
339 /* Return PURE_PARAM if only the coefficients of the parameters are non-zero.
340 * Return PURE_VAR if only the coefficients of the set variables are non-zero.
341 * Return MIXED if only the coefficients of the parameters and the set
342 * variables are non-zero and if moreover the parametric constant
343 * can never attain positive values.
344 * Return IMPURE otherwise.
346 static int purity(__isl_keep isl_basic_set *bset, isl_int *c, int *div_purity,
347 int eq)
349 isl_size d;
350 isl_size n_div;
351 isl_size nparam;
352 isl_bool empty;
353 int i;
354 int p = 0, v = 0;
356 n_div = isl_basic_set_dim(bset, isl_dim_div);
357 d = isl_basic_set_dim(bset, isl_dim_set);
358 nparam = isl_basic_set_dim(bset, isl_dim_param);
359 if (n_div < 0 || d < 0 || nparam < 0)
360 return -1;
362 for (i = 0; i < n_div; ++i) {
363 if (isl_int_is_zero(c[1 + nparam + d + i]))
364 continue;
365 switch (div_purity[i]) {
366 case PURE_PARAM: p = 1; break;
367 case PURE_VAR: v = 1; break;
368 default: return IMPURE;
371 if (!p && isl_seq_first_non_zero(c + 1, nparam) == -1)
372 return PURE_VAR;
373 if (!v && isl_seq_first_non_zero(c + 1 + nparam, d) == -1)
374 return PURE_PARAM;
376 empty = parametric_constant_never_positive(bset, c, div_purity);
377 if (eq && empty >= 0 && !empty) {
378 isl_seq_neg(c, c, 1 + nparam + d + n_div);
379 empty = parametric_constant_never_positive(bset, c, div_purity);
382 return empty < 0 ? -1 : empty ? MIXED : IMPURE;
385 /* Return an array of integers indicating the type of each div in bset.
386 * If the div is (recursively) defined in terms of only the parameters,
387 * then the type is PURE_PARAM.
388 * If the div is (recursively) defined in terms of only the set variables,
389 * then the type is PURE_VAR.
390 * Otherwise, the type is IMPURE.
392 static __isl_give int *get_div_purity(__isl_keep isl_basic_set *bset)
394 int i, j;
395 int *div_purity;
396 isl_size d;
397 isl_size n_div;
398 isl_size nparam;
400 n_div = isl_basic_set_dim(bset, isl_dim_div);
401 d = isl_basic_set_dim(bset, isl_dim_set);
402 nparam = isl_basic_set_dim(bset, isl_dim_param);
403 if (n_div < 0 || d < 0 || nparam < 0)
404 return NULL;
406 div_purity = isl_alloc_array(bset->ctx, int, n_div);
407 if (n_div && !div_purity)
408 return NULL;
410 for (i = 0; i < bset->n_div; ++i) {
411 int p = 0, v = 0;
412 if (isl_int_is_zero(bset->div[i][0])) {
413 div_purity[i] = IMPURE;
414 continue;
416 if (isl_seq_first_non_zero(bset->div[i] + 2, nparam) != -1)
417 p = 1;
418 if (isl_seq_first_non_zero(bset->div[i] + 2 + nparam, d) != -1)
419 v = 1;
420 for (j = 0; j < i; ++j) {
421 if (isl_int_is_zero(bset->div[i][2 + nparam + d + j]))
422 continue;
423 switch (div_purity[j]) {
424 case PURE_PARAM: p = 1; break;
425 case PURE_VAR: v = 1; break;
426 default: p = v = 1; break;
429 div_purity[i] = v ? p ? IMPURE : PURE_VAR : PURE_PARAM;
432 return div_purity;
435 /* Given a path with the as yet unconstrained length at div position "pos",
436 * check if setting the length to zero results in only the identity
437 * mapping.
439 static isl_bool empty_path_is_identity(__isl_keep isl_basic_map *path,
440 unsigned pos)
442 isl_basic_map *test = NULL;
443 isl_basic_map *id = NULL;
444 isl_bool is_id;
446 test = isl_basic_map_copy(path);
447 test = isl_basic_map_fix_si(test, isl_dim_div, pos, 0);
448 id = isl_basic_map_identity(isl_basic_map_get_space(path));
449 is_id = isl_basic_map_is_equal(test, id);
450 isl_basic_map_free(test);
451 isl_basic_map_free(id);
452 return is_id;
455 /* If any of the constraints is found to be impure then this function
456 * sets *impurity to 1.
458 * If impurity is NULL then we are dealing with a non-parametric set
459 * and so the constraints are obviously PURE_VAR.
461 static __isl_give isl_basic_map *add_delta_constraints(
462 __isl_take isl_basic_map *path,
463 __isl_keep isl_basic_set *delta, unsigned off, unsigned nparam,
464 unsigned d, int *div_purity, int eq, int *impurity)
466 int i, k;
467 int n = eq ? delta->n_eq : delta->n_ineq;
468 isl_int **delta_c = eq ? delta->eq : delta->ineq;
469 isl_size n_div, total;
471 n_div = isl_basic_set_dim(delta, isl_dim_div);
472 total = isl_basic_map_dim(path, isl_dim_all);
473 if (n_div < 0 || total < 0)
474 return isl_basic_map_free(path);
476 for (i = 0; i < n; ++i) {
477 isl_int *path_c;
478 int p = PURE_VAR;
479 if (impurity)
480 p = purity(delta, delta_c[i], div_purity, eq);
481 if (p < 0)
482 goto error;
483 if (p != PURE_VAR && p != PURE_PARAM && !*impurity)
484 *impurity = 1;
485 if (p == IMPURE)
486 continue;
487 if (eq && p != MIXED) {
488 k = isl_basic_map_alloc_equality(path);
489 if (k < 0)
490 goto error;
491 path_c = path->eq[k];
492 } else {
493 k = isl_basic_map_alloc_inequality(path);
494 if (k < 0)
495 goto error;
496 path_c = path->ineq[k];
498 isl_seq_clr(path_c, 1 + total);
499 if (p == PURE_VAR) {
500 isl_seq_cpy(path_c + off,
501 delta_c[i] + 1 + nparam, d);
502 isl_int_set(path_c[off + d], delta_c[i][0]);
503 } else if (p == PURE_PARAM) {
504 isl_seq_cpy(path_c, delta_c[i], 1 + nparam);
505 } else {
506 isl_seq_cpy(path_c + off,
507 delta_c[i] + 1 + nparam, d);
508 isl_seq_cpy(path_c, delta_c[i], 1 + nparam);
510 isl_seq_cpy(path_c + off - n_div,
511 delta_c[i] + 1 + nparam + d, n_div);
514 return path;
515 error:
516 isl_basic_map_free(path);
517 return NULL;
520 /* Given a set of offsets "delta", construct a relation of the
521 * given dimension specification (Z^{n+1} -> Z^{n+1}) that
522 * is an overapproximation of the relations that
523 * maps an element x to any element that can be reached
524 * by taking a non-negative number of steps along any of
525 * the elements in "delta".
526 * That is, construct an approximation of
528 * { [x] -> [y] : exists f \in \delta, k \in Z :
529 * y = x + k [f, 1] and k >= 0 }
531 * For any element in this relation, the number of steps taken
532 * is equal to the difference in the final coordinates.
534 * In particular, let delta be defined as
536 * \delta = [p] -> { [x] : A x + a >= 0 and B p + b >= 0 and
537 * C x + C'p + c >= 0 and
538 * D x + D'p + d >= 0 }
540 * where the constraints C x + C'p + c >= 0 are such that the parametric
541 * constant term of each constraint j, "C_j x + C'_j p + c_j",
542 * can never attain positive values, then the relation is constructed as
544 * { [x] -> [y] : exists [f, k] \in Z^{n+1} : y = x + f and
545 * A f + k a >= 0 and B p + b >= 0 and
546 * C f + C'p + c >= 0 and k >= 1 }
547 * union { [x] -> [x] }
549 * If the zero-length paths happen to correspond exactly to the identity
550 * mapping, then we return
552 * { [x] -> [y] : exists [f, k] \in Z^{n+1} : y = x + f and
553 * A f + k a >= 0 and B p + b >= 0 and
554 * C f + C'p + c >= 0 and k >= 0 }
556 * instead.
558 * Existentially quantified variables in \delta are handled by
559 * classifying them as independent of the parameters, purely
560 * parameter dependent and others. Constraints containing
561 * any of the other existentially quantified variables are removed.
562 * This is safe, but leads to an additional overapproximation.
564 * If there are any impure constraints, then we also eliminate
565 * the parameters from \delta, resulting in a set
567 * \delta' = { [x] : E x + e >= 0 }
569 * and add the constraints
571 * E f + k e >= 0
573 * to the constructed relation.
575 static __isl_give isl_map *path_along_delta(__isl_take isl_space *space,
576 __isl_take isl_basic_set *delta)
578 isl_basic_map *path = NULL;
579 isl_size d;
580 isl_size n_div;
581 isl_size nparam;
582 isl_size total;
583 unsigned off;
584 int i, k;
585 isl_bool is_id;
586 int *div_purity = NULL;
587 int impurity = 0;
589 n_div = isl_basic_set_dim(delta, isl_dim_div);
590 d = isl_basic_set_dim(delta, isl_dim_set);
591 nparam = isl_basic_set_dim(delta, isl_dim_param);
592 if (n_div < 0 || d < 0 || nparam < 0)
593 goto error;
594 path = isl_basic_map_alloc_space(isl_space_copy(space), n_div + d + 1,
595 d + 1 + delta->n_eq, delta->n_eq + delta->n_ineq + 1);
596 off = 1 + nparam + 2 * (d + 1) + n_div;
598 for (i = 0; i < n_div + d + 1; ++i) {
599 k = isl_basic_map_alloc_div(path);
600 if (k < 0)
601 goto error;
602 isl_int_set_si(path->div[k][0], 0);
605 total = isl_basic_map_dim(path, isl_dim_all);
606 if (total < 0)
607 goto error;
608 for (i = 0; i < d + 1; ++i) {
609 k = isl_basic_map_alloc_equality(path);
610 if (k < 0)
611 goto error;
612 isl_seq_clr(path->eq[k], 1 + total);
613 isl_int_set_si(path->eq[k][1 + nparam + i], 1);
614 isl_int_set_si(path->eq[k][1 + nparam + d + 1 + i], -1);
615 isl_int_set_si(path->eq[k][off + i], 1);
618 div_purity = get_div_purity(delta);
619 if (n_div && !div_purity)
620 goto error;
622 path = add_delta_constraints(path, delta, off, nparam, d,
623 div_purity, 1, &impurity);
624 path = add_delta_constraints(path, delta, off, nparam, d,
625 div_purity, 0, &impurity);
626 if (impurity) {
627 isl_space *space = isl_basic_set_get_space(delta);
628 delta = isl_basic_set_project_out(delta,
629 isl_dim_param, 0, nparam);
630 delta = isl_basic_set_add_dims(delta, isl_dim_param, nparam);
631 delta = isl_basic_set_reset_space(delta, space);
632 if (!delta)
633 goto error;
634 path = isl_basic_map_extend_constraints(path, delta->n_eq,
635 delta->n_ineq + 1);
636 path = add_delta_constraints(path, delta, off, nparam, d,
637 NULL, 1, NULL);
638 path = add_delta_constraints(path, delta, off, nparam, d,
639 NULL, 0, NULL);
640 path = isl_basic_map_gauss(path, NULL);
643 is_id = empty_path_is_identity(path, n_div + d);
644 if (is_id < 0)
645 goto error;
647 k = isl_basic_map_alloc_inequality(path);
648 if (k < 0)
649 goto error;
650 isl_seq_clr(path->ineq[k], 1 + total);
651 if (!is_id)
652 isl_int_set_si(path->ineq[k][0], -1);
653 isl_int_set_si(path->ineq[k][off + d], 1);
655 free(div_purity);
656 isl_basic_set_free(delta);
657 path = isl_basic_map_finalize(path);
658 if (is_id) {
659 isl_space_free(space);
660 return isl_map_from_basic_map(path);
662 return isl_basic_map_union(path, isl_basic_map_identity(space));
663 error:
664 free(div_purity);
665 isl_space_free(space);
666 isl_basic_set_free(delta);
667 isl_basic_map_free(path);
668 return NULL;
671 /* Given a dimension specification Z^{n+1} -> Z^{n+1} and a parameter "param",
672 * construct a map that equates the parameter to the difference
673 * in the final coordinates and imposes that this difference is positive.
674 * That is, construct
676 * { [x,x_s] -> [y,y_s] : k = y_s - x_s > 0 }
678 static __isl_give isl_map *equate_parameter_to_length(
679 __isl_take isl_space *space, unsigned param)
681 struct isl_basic_map *bmap;
682 isl_size d;
683 isl_size nparam;
684 isl_size total;
685 int k;
687 d = isl_space_dim(space, isl_dim_in);
688 nparam = isl_space_dim(space, isl_dim_param);
689 total = isl_space_dim(space, isl_dim_all);
690 if (d < 0 || nparam < 0 || total < 0)
691 space = isl_space_free(space);
692 bmap = isl_basic_map_alloc_space(space, 0, 1, 1);
693 k = isl_basic_map_alloc_equality(bmap);
694 if (k < 0)
695 goto error;
696 isl_seq_clr(bmap->eq[k], 1 + total);
697 isl_int_set_si(bmap->eq[k][1 + param], -1);
698 isl_int_set_si(bmap->eq[k][1 + nparam + d - 1], -1);
699 isl_int_set_si(bmap->eq[k][1 + nparam + d + d - 1], 1);
701 k = isl_basic_map_alloc_inequality(bmap);
702 if (k < 0)
703 goto error;
704 isl_seq_clr(bmap->ineq[k], 1 + total);
705 isl_int_set_si(bmap->ineq[k][1 + param], 1);
706 isl_int_set_si(bmap->ineq[k][0], -1);
708 bmap = isl_basic_map_finalize(bmap);
709 return isl_map_from_basic_map(bmap);
710 error:
711 isl_basic_map_free(bmap);
712 return NULL;
715 /* Check whether "path" is acyclic, where the last coordinates of domain
716 * and range of path encode the number of steps taken.
717 * That is, check whether
719 * { d | d = y - x and (x,y) in path }
721 * does not contain any element with positive last coordinate (positive length)
722 * and zero remaining coordinates (cycle).
724 static isl_bool is_acyclic(__isl_take isl_map *path)
726 int i;
727 isl_bool acyclic;
728 isl_size dim;
729 struct isl_set *delta;
731 delta = isl_map_deltas(path);
732 dim = isl_set_dim(delta, isl_dim_set);
733 if (dim < 0)
734 delta = isl_set_free(delta);
735 for (i = 0; i < dim; ++i) {
736 if (i == dim -1)
737 delta = isl_set_lower_bound_si(delta, isl_dim_set, i, 1);
738 else
739 delta = isl_set_fix_si(delta, isl_dim_set, i, 0);
742 acyclic = isl_set_is_empty(delta);
743 isl_set_free(delta);
745 return acyclic;
748 /* Given a union of basic maps R = \cup_i R_i \subseteq D \times D
749 * and a dimension specification (Z^{n+1} -> Z^{n+1}),
750 * construct a map that is an overapproximation of the map
751 * that takes an element from the space D \times Z to another
752 * element from the same space, such that the first n coordinates of the
753 * difference between them is a sum of differences between images
754 * and pre-images in one of the R_i and such that the last coordinate
755 * is equal to the number of steps taken.
756 * That is, let
758 * \Delta_i = { y - x | (x, y) in R_i }
760 * then the constructed map is an overapproximation of
762 * { (x) -> (x + d) | \exists k_i >= 0, \delta_i \in \Delta_i :
763 * d = (\sum_i k_i \delta_i, \sum_i k_i) }
765 * The elements of the singleton \Delta_i's are collected as the
766 * rows of the steps matrix. For all these \Delta_i's together,
767 * a single path is constructed.
768 * For each of the other \Delta_i's, we compute an overapproximation
769 * of the paths along elements of \Delta_i.
770 * Since each of these paths performs an addition, composition is
771 * symmetric and we can simply compose all resulting paths in any order.
773 static __isl_give isl_map *construct_extended_path(__isl_take isl_space *space,
774 __isl_keep isl_map *map, int *project)
776 struct isl_mat *steps = NULL;
777 struct isl_map *path = NULL;
778 isl_size d;
779 int i, j, n;
781 d = isl_map_dim(map, isl_dim_in);
782 if (d < 0)
783 goto error;
785 path = isl_map_identity(isl_space_copy(space));
787 steps = isl_mat_alloc(map->ctx, map->n, d);
788 if (!steps)
789 goto error;
791 n = 0;
792 for (i = 0; i < map->n; ++i) {
793 struct isl_basic_set *delta;
795 delta = isl_basic_map_deltas(isl_basic_map_copy(map->p[i]));
797 for (j = 0; j < d; ++j) {
798 isl_bool fixed;
800 fixed = isl_basic_set_plain_dim_is_fixed(delta, j,
801 &steps->row[n][j]);
802 if (fixed < 0) {
803 isl_basic_set_free(delta);
804 goto error;
806 if (!fixed)
807 break;
811 if (j < d) {
812 path = isl_map_apply_range(path,
813 path_along_delta(isl_space_copy(space), delta));
814 path = isl_map_coalesce(path);
815 } else {
816 isl_basic_set_free(delta);
817 ++n;
821 if (n > 0) {
822 steps->n_row = n;
823 path = isl_map_apply_range(path,
824 path_along_steps(isl_space_copy(space), steps));
827 if (project && *project) {
828 *project = is_acyclic(isl_map_copy(path));
829 if (*project < 0)
830 goto error;
833 isl_space_free(space);
834 isl_mat_free(steps);
835 return path;
836 error:
837 isl_space_free(space);
838 isl_mat_free(steps);
839 isl_map_free(path);
840 return NULL;
843 static isl_bool isl_set_overlaps(__isl_keep isl_set *set1,
844 __isl_keep isl_set *set2)
846 isl_set *i;
847 isl_bool no_overlap;
849 if (!set1 || !set2)
850 return isl_bool_error;
852 if (!isl_space_tuple_is_equal(set1->dim, isl_dim_set,
853 set2->dim, isl_dim_set))
854 return isl_bool_false;
856 i = isl_set_intersect(isl_set_copy(set1), isl_set_copy(set2));
857 no_overlap = isl_set_is_empty(i);
858 isl_set_free(i);
860 return isl_bool_not(no_overlap);
863 /* Given a union of basic maps R = \cup_i R_i \subseteq D \times D
864 * and a dimension specification (Z^{n+1} -> Z^{n+1}),
865 * construct a map that is an overapproximation of the map
866 * that takes an element from the dom R \times Z to an
867 * element from ran R \times Z, such that the first n coordinates of the
868 * difference between them is a sum of differences between images
869 * and pre-images in one of the R_i and such that the last coordinate
870 * is equal to the number of steps taken.
871 * That is, let
873 * \Delta_i = { y - x | (x, y) in R_i }
875 * then the constructed map is an overapproximation of
877 * { (x) -> (x + d) | \exists k_i >= 0, \delta_i \in \Delta_i :
878 * d = (\sum_i k_i \delta_i, \sum_i k_i) and
879 * x in dom R and x + d in ran R and
880 * \sum_i k_i >= 1 }
882 static __isl_give isl_map *construct_component(__isl_take isl_space *dim,
883 __isl_keep isl_map *map, int *exact, int project)
885 struct isl_set *domain = NULL;
886 struct isl_set *range = NULL;
887 struct isl_map *app = NULL;
888 struct isl_map *path = NULL;
889 isl_bool overlaps;
891 domain = isl_map_domain(isl_map_copy(map));
892 domain = isl_set_coalesce(domain);
893 range = isl_map_range(isl_map_copy(map));
894 range = isl_set_coalesce(range);
895 overlaps = isl_set_overlaps(domain, range);
896 if (overlaps < 0 || !overlaps) {
897 isl_set_free(domain);
898 isl_set_free(range);
899 isl_space_free(dim);
901 if (overlaps < 0)
902 map = NULL;
903 map = isl_map_copy(map);
904 map = isl_map_add_dims(map, isl_dim_in, 1);
905 map = isl_map_add_dims(map, isl_dim_out, 1);
906 map = set_path_length(map, 1, 1);
907 return map;
909 app = isl_map_from_domain_and_range(domain, range);
910 app = isl_map_add_dims(app, isl_dim_in, 1);
911 app = isl_map_add_dims(app, isl_dim_out, 1);
913 path = construct_extended_path(isl_space_copy(dim), map,
914 exact && *exact ? &project : NULL);
915 app = isl_map_intersect(app, path);
917 if (exact && *exact &&
918 (*exact = check_exactness(isl_map_copy(map), isl_map_copy(app),
919 project)) < 0)
920 goto error;
922 isl_space_free(dim);
923 app = set_path_length(app, 0, 1);
924 return app;
925 error:
926 isl_space_free(dim);
927 isl_map_free(app);
928 return NULL;
931 /* Call construct_component and, if "project" is set, project out
932 * the final coordinates.
934 static __isl_give isl_map *construct_projected_component(
935 __isl_take isl_space *space,
936 __isl_keep isl_map *map, int *exact, int project)
938 isl_map *app;
939 unsigned d;
941 if (!space)
942 return NULL;
943 d = isl_space_dim(space, isl_dim_in);
945 app = construct_component(space, map, exact, project);
946 if (project) {
947 app = isl_map_project_out(app, isl_dim_in, d - 1, 1);
948 app = isl_map_project_out(app, isl_dim_out, d - 1, 1);
950 return app;
953 /* Compute an extended version, i.e., with path lengths, of
954 * an overapproximation of the transitive closure of "bmap"
955 * with path lengths greater than or equal to zero and with
956 * domain and range equal to "dom".
958 static __isl_give isl_map *q_closure(__isl_take isl_space *dim,
959 __isl_take isl_set *dom, __isl_keep isl_basic_map *bmap, int *exact)
961 int project = 1;
962 isl_map *path;
963 isl_map *map;
964 isl_map *app;
966 dom = isl_set_add_dims(dom, isl_dim_set, 1);
967 app = isl_map_from_domain_and_range(dom, isl_set_copy(dom));
968 map = isl_map_from_basic_map(isl_basic_map_copy(bmap));
969 path = construct_extended_path(dim, map, &project);
970 app = isl_map_intersect(app, path);
972 if ((*exact = check_exactness(map, isl_map_copy(app), project)) < 0)
973 goto error;
975 return app;
976 error:
977 isl_map_free(app);
978 return NULL;
981 /* Check whether qc has any elements of length at least one
982 * with domain and/or range outside of dom and ran.
984 static isl_bool has_spurious_elements(__isl_keep isl_map *qc,
985 __isl_keep isl_set *dom, __isl_keep isl_set *ran)
987 isl_set *s;
988 isl_bool subset;
989 isl_size d;
991 d = isl_map_dim(qc, isl_dim_in);
992 if (d < 0 || !dom || !ran)
993 return isl_bool_error;
995 qc = isl_map_copy(qc);
996 qc = set_path_length(qc, 0, 1);
997 qc = isl_map_project_out(qc, isl_dim_in, d - 1, 1);
998 qc = isl_map_project_out(qc, isl_dim_out, d - 1, 1);
1000 s = isl_map_domain(isl_map_copy(qc));
1001 subset = isl_set_is_subset(s, dom);
1002 isl_set_free(s);
1003 if (subset < 0)
1004 goto error;
1005 if (!subset) {
1006 isl_map_free(qc);
1007 return isl_bool_true;
1010 s = isl_map_range(qc);
1011 subset = isl_set_is_subset(s, ran);
1012 isl_set_free(s);
1014 return isl_bool_not(subset);
1015 error:
1016 isl_map_free(qc);
1017 return isl_bool_error;
1020 #define LEFT 2
1021 #define RIGHT 1
1023 /* For each basic map in "map", except i, check whether it combines
1024 * with the transitive closure that is reflexive on C combines
1025 * to the left and to the right.
1027 * In particular, if
1029 * dom map_j \subseteq C
1031 * then right[j] is set to 1. Otherwise, if
1033 * ran map_i \cap dom map_j = \emptyset
1035 * then right[j] is set to 0. Otherwise, composing to the right
1036 * is impossible.
1038 * Similar, for composing to the left, we have if
1040 * ran map_j \subseteq C
1042 * then left[j] is set to 1. Otherwise, if
1044 * dom map_i \cap ran map_j = \emptyset
1046 * then left[j] is set to 0. Otherwise, composing to the left
1047 * is impossible.
1049 * The return value is or'd with LEFT if composing to the left
1050 * is possible and with RIGHT if composing to the right is possible.
1052 static int composability(__isl_keep isl_set *C, int i,
1053 isl_set **dom, isl_set **ran, int *left, int *right,
1054 __isl_keep isl_map *map)
1056 int j;
1057 int ok;
1059 ok = LEFT | RIGHT;
1060 for (j = 0; j < map->n && ok; ++j) {
1061 isl_bool overlaps, subset;
1062 if (j == i)
1063 continue;
1065 if (ok & RIGHT) {
1066 if (!dom[j])
1067 dom[j] = isl_set_from_basic_set(
1068 isl_basic_map_domain(
1069 isl_basic_map_copy(map->p[j])));
1070 if (!dom[j])
1071 return -1;
1072 overlaps = isl_set_overlaps(ran[i], dom[j]);
1073 if (overlaps < 0)
1074 return -1;
1075 if (!overlaps)
1076 right[j] = 0;
1077 else {
1078 subset = isl_set_is_subset(dom[j], C);
1079 if (subset < 0)
1080 return -1;
1081 if (subset)
1082 right[j] = 1;
1083 else
1084 ok &= ~RIGHT;
1088 if (ok & LEFT) {
1089 if (!ran[j])
1090 ran[j] = isl_set_from_basic_set(
1091 isl_basic_map_range(
1092 isl_basic_map_copy(map->p[j])));
1093 if (!ran[j])
1094 return -1;
1095 overlaps = isl_set_overlaps(dom[i], ran[j]);
1096 if (overlaps < 0)
1097 return -1;
1098 if (!overlaps)
1099 left[j] = 0;
1100 else {
1101 subset = isl_set_is_subset(ran[j], C);
1102 if (subset < 0)
1103 return -1;
1104 if (subset)
1105 left[j] = 1;
1106 else
1107 ok &= ~LEFT;
1112 return ok;
1115 static __isl_give isl_map *anonymize(__isl_take isl_map *map)
1117 map = isl_map_reset(map, isl_dim_in);
1118 map = isl_map_reset(map, isl_dim_out);
1119 return map;
1122 /* Return a map that is a union of the basic maps in "map", except i,
1123 * composed to left and right with qc based on the entries of "left"
1124 * and "right".
1126 static __isl_give isl_map *compose(__isl_keep isl_map *map, int i,
1127 __isl_take isl_map *qc, int *left, int *right)
1129 int j;
1130 isl_map *comp;
1132 comp = isl_map_empty(isl_map_get_space(map));
1133 for (j = 0; j < map->n; ++j) {
1134 isl_map *map_j;
1136 if (j == i)
1137 continue;
1139 map_j = isl_map_from_basic_map(isl_basic_map_copy(map->p[j]));
1140 map_j = anonymize(map_j);
1141 if (left && left[j])
1142 map_j = isl_map_apply_range(map_j, isl_map_copy(qc));
1143 if (right && right[j])
1144 map_j = isl_map_apply_range(isl_map_copy(qc), map_j);
1145 comp = isl_map_union(comp, map_j);
1148 comp = isl_map_compute_divs(comp);
1149 comp = isl_map_coalesce(comp);
1151 isl_map_free(qc);
1153 return comp;
1156 /* Compute the transitive closure of "map" incrementally by
1157 * computing
1159 * map_i^+ \cup qc^+
1161 * or
1163 * map_i^+ \cup ((id \cup map_i^) \circ qc^+)
1165 * or
1167 * map_i^+ \cup (qc^+ \circ (id \cup map_i^))
1169 * depending on whether left or right are NULL.
1171 static __isl_give isl_map *compute_incremental(
1172 __isl_take isl_space *space, __isl_keep isl_map *map,
1173 int i, __isl_take isl_map *qc, int *left, int *right, int *exact)
1175 isl_map *map_i;
1176 isl_map *tc;
1177 isl_map *rtc = NULL;
1179 if (!map)
1180 goto error;
1181 isl_assert(map->ctx, left || right, goto error);
1183 map_i = isl_map_from_basic_map(isl_basic_map_copy(map->p[i]));
1184 tc = construct_projected_component(isl_space_copy(space), map_i,
1185 exact, 1);
1186 isl_map_free(map_i);
1188 if (*exact)
1189 qc = isl_map_transitive_closure(qc, exact);
1191 if (!*exact) {
1192 isl_space_free(space);
1193 isl_map_free(tc);
1194 isl_map_free(qc);
1195 return isl_map_universe(isl_map_get_space(map));
1198 if (!left || !right)
1199 rtc = isl_map_union(isl_map_copy(tc),
1200 isl_map_identity(isl_map_get_space(tc)));
1201 if (!right)
1202 qc = isl_map_apply_range(rtc, qc);
1203 if (!left)
1204 qc = isl_map_apply_range(qc, rtc);
1205 qc = isl_map_union(tc, qc);
1207 isl_space_free(space);
1209 return qc;
1210 error:
1211 isl_space_free(space);
1212 isl_map_free(qc);
1213 return NULL;
1216 /* Given a map "map", try to find a basic map such that
1217 * map^+ can be computed as
1219 * map^+ = map_i^+ \cup
1220 * \bigcup_j ((map_i^+ \cup Id_C)^+ \circ map_j \circ (map_i^+ \cup Id_C))^+
1222 * with C the simple hull of the domain and range of the input map.
1223 * map_i^ \cup Id_C is computed by allowing the path lengths to be zero
1224 * and by intersecting domain and range with C.
1225 * Of course, we need to check that this is actually equal to map_i^ \cup Id_C.
1226 * Also, we only use the incremental computation if all the transitive
1227 * closures are exact and if the number of basic maps in the union,
1228 * after computing the integer divisions, is smaller than the number
1229 * of basic maps in the input map.
1231 static isl_bool incremental_on_entire_domain(__isl_keep isl_space *space,
1232 __isl_keep isl_map *map,
1233 isl_set **dom, isl_set **ran, int *left, int *right,
1234 __isl_give isl_map **res)
1236 int i;
1237 isl_set *C;
1238 isl_size d;
1240 *res = NULL;
1242 d = isl_map_dim(map, isl_dim_in);
1243 if (d < 0)
1244 return isl_bool_error;
1246 C = isl_set_union(isl_map_domain(isl_map_copy(map)),
1247 isl_map_range(isl_map_copy(map)));
1248 C = isl_set_from_basic_set(isl_set_simple_hull(C));
1249 if (!C)
1250 return isl_bool_error;
1251 if (C->n != 1) {
1252 isl_set_free(C);
1253 return isl_bool_false;
1256 for (i = 0; i < map->n; ++i) {
1257 isl_map *qc;
1258 int exact_i;
1259 isl_bool spurious;
1260 int j;
1261 dom[i] = isl_set_from_basic_set(isl_basic_map_domain(
1262 isl_basic_map_copy(map->p[i])));
1263 ran[i] = isl_set_from_basic_set(isl_basic_map_range(
1264 isl_basic_map_copy(map->p[i])));
1265 qc = q_closure(isl_space_copy(space), isl_set_copy(C),
1266 map->p[i], &exact_i);
1267 if (!qc)
1268 goto error;
1269 if (!exact_i) {
1270 isl_map_free(qc);
1271 continue;
1273 spurious = has_spurious_elements(qc, dom[i], ran[i]);
1274 if (spurious) {
1275 isl_map_free(qc);
1276 if (spurious < 0)
1277 goto error;
1278 continue;
1280 qc = isl_map_project_out(qc, isl_dim_in, d, 1);
1281 qc = isl_map_project_out(qc, isl_dim_out, d, 1);
1282 qc = isl_map_compute_divs(qc);
1283 for (j = 0; j < map->n; ++j)
1284 left[j] = right[j] = 1;
1285 qc = compose(map, i, qc, left, right);
1286 if (!qc)
1287 goto error;
1288 if (qc->n >= map->n) {
1289 isl_map_free(qc);
1290 continue;
1292 *res = compute_incremental(isl_space_copy(space), map, i, qc,
1293 left, right, &exact_i);
1294 if (!*res)
1295 goto error;
1296 if (exact_i)
1297 break;
1298 isl_map_free(*res);
1299 *res = NULL;
1302 isl_set_free(C);
1304 return *res != NULL;
1305 error:
1306 isl_set_free(C);
1307 return isl_bool_error;
1310 /* Try and compute the transitive closure of "map" as
1312 * map^+ = map_i^+ \cup
1313 * \bigcup_j ((map_i^+ \cup Id_C)^+ \circ map_j \circ (map_i^+ \cup Id_C))^+
1315 * with C either the simple hull of the domain and range of the entire
1316 * map or the simple hull of domain and range of map_i.
1318 static __isl_give isl_map *incremental_closure(__isl_take isl_space *space,
1319 __isl_keep isl_map *map, int *exact, int project)
1321 int i;
1322 isl_set **dom = NULL;
1323 isl_set **ran = NULL;
1324 int *left = NULL;
1325 int *right = NULL;
1326 isl_set *C;
1327 isl_size d;
1328 isl_map *res = NULL;
1330 if (!project)
1331 return construct_projected_component(space, map, exact,
1332 project);
1334 if (!map)
1335 goto error;
1336 if (map->n <= 1)
1337 return construct_projected_component(space, map, exact,
1338 project);
1340 d = isl_map_dim(map, isl_dim_in);
1341 if (d < 0)
1342 goto error;
1344 dom = isl_calloc_array(map->ctx, isl_set *, map->n);
1345 ran = isl_calloc_array(map->ctx, isl_set *, map->n);
1346 left = isl_calloc_array(map->ctx, int, map->n);
1347 right = isl_calloc_array(map->ctx, int, map->n);
1348 if (!ran || !dom || !left || !right)
1349 goto error;
1351 if (incremental_on_entire_domain(space, map, dom, ran, left, right,
1352 &res) < 0)
1353 goto error;
1355 for (i = 0; !res && i < map->n; ++i) {
1356 isl_map *qc;
1357 int exact_i, comp;
1358 isl_bool spurious;
1359 if (!dom[i])
1360 dom[i] = isl_set_from_basic_set(
1361 isl_basic_map_domain(
1362 isl_basic_map_copy(map->p[i])));
1363 if (!dom[i])
1364 goto error;
1365 if (!ran[i])
1366 ran[i] = isl_set_from_basic_set(
1367 isl_basic_map_range(
1368 isl_basic_map_copy(map->p[i])));
1369 if (!ran[i])
1370 goto error;
1371 C = isl_set_union(isl_set_copy(dom[i]),
1372 isl_set_copy(ran[i]));
1373 C = isl_set_from_basic_set(isl_set_simple_hull(C));
1374 if (!C)
1375 goto error;
1376 if (C->n != 1) {
1377 isl_set_free(C);
1378 continue;
1380 comp = composability(C, i, dom, ran, left, right, map);
1381 if (!comp || comp < 0) {
1382 isl_set_free(C);
1383 if (comp < 0)
1384 goto error;
1385 continue;
1387 qc = q_closure(isl_space_copy(space), C, map->p[i], &exact_i);
1388 if (!qc)
1389 goto error;
1390 if (!exact_i) {
1391 isl_map_free(qc);
1392 continue;
1394 spurious = has_spurious_elements(qc, dom[i], ran[i]);
1395 if (spurious) {
1396 isl_map_free(qc);
1397 if (spurious < 0)
1398 goto error;
1399 continue;
1401 qc = isl_map_project_out(qc, isl_dim_in, d, 1);
1402 qc = isl_map_project_out(qc, isl_dim_out, d, 1);
1403 qc = isl_map_compute_divs(qc);
1404 qc = compose(map, i, qc, (comp & LEFT) ? left : NULL,
1405 (comp & RIGHT) ? right : NULL);
1406 if (!qc)
1407 goto error;
1408 if (qc->n >= map->n) {
1409 isl_map_free(qc);
1410 continue;
1412 res = compute_incremental(isl_space_copy(space), map, i, qc,
1413 (comp & LEFT) ? left : NULL,
1414 (comp & RIGHT) ? right : NULL, &exact_i);
1415 if (!res)
1416 goto error;
1417 if (exact_i)
1418 break;
1419 isl_map_free(res);
1420 res = NULL;
1423 for (i = 0; i < map->n; ++i) {
1424 isl_set_free(dom[i]);
1425 isl_set_free(ran[i]);
1427 free(dom);
1428 free(ran);
1429 free(left);
1430 free(right);
1432 if (res) {
1433 isl_space_free(space);
1434 return res;
1437 return construct_projected_component(space, map, exact, project);
1438 error:
1439 if (dom)
1440 for (i = 0; i < map->n; ++i)
1441 isl_set_free(dom[i]);
1442 free(dom);
1443 if (ran)
1444 for (i = 0; i < map->n; ++i)
1445 isl_set_free(ran[i]);
1446 free(ran);
1447 free(left);
1448 free(right);
1449 isl_space_free(space);
1450 return NULL;
1453 /* Given an array of sets "set", add "dom" at position "pos"
1454 * and search for elements at earlier positions that overlap with "dom".
1455 * If any can be found, then merge all of them, together with "dom", into
1456 * a single set and assign the union to the first in the array,
1457 * which becomes the new group leader for all groups involved in the merge.
1458 * During the search, we only consider group leaders, i.e., those with
1459 * group[i] = i, as the other sets have already been combined
1460 * with one of the group leaders.
1462 static int merge(isl_set **set, int *group, __isl_take isl_set *dom, int pos)
1464 int i;
1466 group[pos] = pos;
1467 set[pos] = isl_set_copy(dom);
1469 for (i = pos - 1; i >= 0; --i) {
1470 isl_bool o;
1472 if (group[i] != i)
1473 continue;
1475 o = isl_set_overlaps(set[i], dom);
1476 if (o < 0)
1477 goto error;
1478 if (!o)
1479 continue;
1481 set[i] = isl_set_union(set[i], set[group[pos]]);
1482 set[group[pos]] = NULL;
1483 if (!set[i])
1484 goto error;
1485 group[group[pos]] = i;
1486 group[pos] = i;
1489 isl_set_free(dom);
1490 return 0;
1491 error:
1492 isl_set_free(dom);
1493 return -1;
1496 /* Construct a map [x] -> [x+1], with parameters prescribed by "space".
1498 static __isl_give isl_map *increment(__isl_take isl_space *space)
1500 int k;
1501 isl_basic_map *bmap;
1502 isl_size total;
1504 space = isl_space_set_from_params(space);
1505 space = isl_space_add_dims(space, isl_dim_set, 1);
1506 space = isl_space_map_from_set(space);
1507 bmap = isl_basic_map_alloc_space(space, 0, 1, 0);
1508 total = isl_basic_map_dim(bmap, isl_dim_all);
1509 k = isl_basic_map_alloc_equality(bmap);
1510 if (total < 0 || k < 0)
1511 goto error;
1512 isl_seq_clr(bmap->eq[k], 1 + total);
1513 isl_int_set_si(bmap->eq[k][0], 1);
1514 isl_int_set_si(bmap->eq[k][isl_basic_map_offset(bmap, isl_dim_in)], 1);
1515 isl_int_set_si(bmap->eq[k][isl_basic_map_offset(bmap, isl_dim_out)], -1);
1516 return isl_map_from_basic_map(bmap);
1517 error:
1518 isl_basic_map_free(bmap);
1519 return NULL;
1522 /* Replace each entry in the n by n grid of maps by the cross product
1523 * with the relation { [i] -> [i + 1] }.
1525 static isl_stat add_length(__isl_keep isl_map *map, isl_map ***grid, int n)
1527 int i, j;
1528 isl_space *space;
1529 isl_map *step;
1531 space = isl_space_params(isl_map_get_space(map));
1532 step = increment(space);
1534 if (!step)
1535 return isl_stat_error;
1537 for (i = 0; i < n; ++i)
1538 for (j = 0; j < n; ++j)
1539 grid[i][j] = isl_map_product(grid[i][j],
1540 isl_map_copy(step));
1542 isl_map_free(step);
1544 return isl_stat_ok;
1547 /* The core of the Floyd-Warshall algorithm.
1548 * Updates the given n x x matrix of relations in place.
1550 * The algorithm iterates over all vertices. In each step, the whole
1551 * matrix is updated to include all paths that go to the current vertex,
1552 * possibly stay there a while (including passing through earlier vertices)
1553 * and then come back. At the start of each iteration, the diagonal
1554 * element corresponding to the current vertex is replaced by its
1555 * transitive closure to account for all indirect paths that stay
1556 * in the current vertex.
1558 static void floyd_warshall_iterate(isl_map ***grid, int n, int *exact)
1560 int r, p, q;
1562 for (r = 0; r < n; ++r) {
1563 int r_exact;
1564 grid[r][r] = isl_map_transitive_closure(grid[r][r],
1565 (exact && *exact) ? &r_exact : NULL);
1566 if (exact && *exact && !r_exact)
1567 *exact = 0;
1569 for (p = 0; p < n; ++p)
1570 for (q = 0; q < n; ++q) {
1571 isl_map *loop;
1572 if (p == r && q == r)
1573 continue;
1574 loop = isl_map_apply_range(
1575 isl_map_copy(grid[p][r]),
1576 isl_map_copy(grid[r][q]));
1577 grid[p][q] = isl_map_union(grid[p][q], loop);
1578 loop = isl_map_apply_range(
1579 isl_map_copy(grid[p][r]),
1580 isl_map_apply_range(
1581 isl_map_copy(grid[r][r]),
1582 isl_map_copy(grid[r][q])));
1583 grid[p][q] = isl_map_union(grid[p][q], loop);
1584 grid[p][q] = isl_map_coalesce(grid[p][q]);
1589 /* Given a partition of the domains and ranges of the basic maps in "map",
1590 * apply the Floyd-Warshall algorithm with the elements in the partition
1591 * as vertices.
1593 * In particular, there are "n" elements in the partition and "group" is
1594 * an array of length 2 * map->n with entries in [0,n-1].
1596 * We first construct a matrix of relations based on the partition information,
1597 * apply Floyd-Warshall on this matrix of relations and then take the
1598 * union of all entries in the matrix as the final result.
1600 * If we are actually computing the power instead of the transitive closure,
1601 * i.e., when "project" is not set, then the result should have the
1602 * path lengths encoded as the difference between an extra pair of
1603 * coordinates. We therefore apply the nested transitive closures
1604 * to relations that include these lengths. In particular, we replace
1605 * the input relation by the cross product with the unit length relation
1606 * { [i] -> [i + 1] }.
1608 static __isl_give isl_map *floyd_warshall_with_groups(
1609 __isl_take isl_space *space, __isl_keep isl_map *map,
1610 int *exact, int project, int *group, int n)
1612 int i, j, k;
1613 isl_map ***grid = NULL;
1614 isl_map *app;
1616 if (!map)
1617 goto error;
1619 if (n == 1) {
1620 free(group);
1621 return incremental_closure(space, map, exact, project);
1624 grid = isl_calloc_array(map->ctx, isl_map **, n);
1625 if (!grid)
1626 goto error;
1627 for (i = 0; i < n; ++i) {
1628 grid[i] = isl_calloc_array(map->ctx, isl_map *, n);
1629 if (!grid[i])
1630 goto error;
1631 for (j = 0; j < n; ++j)
1632 grid[i][j] = isl_map_empty(isl_map_get_space(map));
1635 for (k = 0; k < map->n; ++k) {
1636 i = group[2 * k];
1637 j = group[2 * k + 1];
1638 grid[i][j] = isl_map_union(grid[i][j],
1639 isl_map_from_basic_map(
1640 isl_basic_map_copy(map->p[k])));
1643 if (!project && add_length(map, grid, n) < 0)
1644 goto error;
1646 floyd_warshall_iterate(grid, n, exact);
1648 app = isl_map_empty(isl_map_get_space(grid[0][0]));
1650 for (i = 0; i < n; ++i) {
1651 for (j = 0; j < n; ++j)
1652 app = isl_map_union(app, grid[i][j]);
1653 free(grid[i]);
1655 free(grid);
1657 free(group);
1658 isl_space_free(space);
1660 return app;
1661 error:
1662 if (grid)
1663 for (i = 0; i < n; ++i) {
1664 if (!grid[i])
1665 continue;
1666 for (j = 0; j < n; ++j)
1667 isl_map_free(grid[i][j]);
1668 free(grid[i]);
1670 free(grid);
1671 free(group);
1672 isl_space_free(space);
1673 return NULL;
1676 /* Partition the domains and ranges of the n basic relations in list
1677 * into disjoint cells.
1679 * To find the partition, we simply consider all of the domains
1680 * and ranges in turn and combine those that overlap.
1681 * "set" contains the partition elements and "group" indicates
1682 * to which partition element a given domain or range belongs.
1683 * The domain of basic map i corresponds to element 2 * i in these arrays,
1684 * while the domain corresponds to element 2 * i + 1.
1685 * During the construction group[k] is either equal to k,
1686 * in which case set[k] contains the union of all the domains and
1687 * ranges in the corresponding group, or is equal to some l < k,
1688 * with l another domain or range in the same group.
1690 static int *setup_groups(isl_ctx *ctx, __isl_keep isl_basic_map **list, int n,
1691 isl_set ***set, int *n_group)
1693 int i;
1694 int *group = NULL;
1695 int g;
1697 *set = isl_calloc_array(ctx, isl_set *, 2 * n);
1698 group = isl_alloc_array(ctx, int, 2 * n);
1700 if (!*set || !group)
1701 goto error;
1703 for (i = 0; i < n; ++i) {
1704 isl_set *dom;
1705 dom = isl_set_from_basic_set(isl_basic_map_domain(
1706 isl_basic_map_copy(list[i])));
1707 if (merge(*set, group, dom, 2 * i) < 0)
1708 goto error;
1709 dom = isl_set_from_basic_set(isl_basic_map_range(
1710 isl_basic_map_copy(list[i])));
1711 if (merge(*set, group, dom, 2 * i + 1) < 0)
1712 goto error;
1715 g = 0;
1716 for (i = 0; i < 2 * n; ++i)
1717 if (group[i] == i) {
1718 if (g != i) {
1719 (*set)[g] = (*set)[i];
1720 (*set)[i] = NULL;
1722 group[i] = g++;
1723 } else
1724 group[i] = group[group[i]];
1726 *n_group = g;
1728 return group;
1729 error:
1730 if (*set) {
1731 for (i = 0; i < 2 * n; ++i)
1732 isl_set_free((*set)[i]);
1733 free(*set);
1734 *set = NULL;
1736 free(group);
1737 return NULL;
1740 /* Check if the domains and ranges of the basic maps in "map" can
1741 * be partitioned, and if so, apply Floyd-Warshall on the elements
1742 * of the partition. Note that we also apply this algorithm
1743 * if we want to compute the power, i.e., when "project" is not set.
1744 * However, the results are unlikely to be exact since the recursive
1745 * calls inside the Floyd-Warshall algorithm typically result in
1746 * non-linear path lengths quite quickly.
1748 static __isl_give isl_map *floyd_warshall(__isl_take isl_space *space,
1749 __isl_keep isl_map *map, int *exact, int project)
1751 int i;
1752 isl_set **set = NULL;
1753 int *group = NULL;
1754 int n;
1756 if (!map)
1757 goto error;
1758 if (map->n <= 1)
1759 return incremental_closure(space, map, exact, project);
1761 group = setup_groups(map->ctx, map->p, map->n, &set, &n);
1762 if (!group)
1763 goto error;
1765 for (i = 0; i < 2 * map->n; ++i)
1766 isl_set_free(set[i]);
1768 free(set);
1770 return floyd_warshall_with_groups(space, map, exact, project, group, n);
1771 error:
1772 isl_space_free(space);
1773 return NULL;
1776 /* Structure for representing the nodes of the graph of which
1777 * strongly connected components are being computed.
1779 * list contains the actual nodes
1780 * check_closed is set if we may have used the fact that
1781 * a pair of basic maps can be interchanged
1783 struct isl_tc_follows_data {
1784 isl_basic_map **list;
1785 int check_closed;
1788 /* Check whether in the computation of the transitive closure
1789 * "list[i]" (R_1) should follow (or be part of the same component as)
1790 * "list[j]" (R_2).
1792 * That is check whether
1794 * R_1 \circ R_2
1796 * is a subset of
1798 * R_2 \circ R_1
1800 * If so, then there is no reason for R_1 to immediately follow R_2
1801 * in any path.
1803 * *check_closed is set if the subset relation holds while
1804 * R_1 \circ R_2 is not empty.
1806 static isl_bool basic_map_follows(int i, int j, void *user)
1808 struct isl_tc_follows_data *data = user;
1809 struct isl_map *map12 = NULL;
1810 struct isl_map *map21 = NULL;
1811 isl_bool subset;
1813 if (!isl_space_tuple_is_equal(data->list[i]->dim, isl_dim_in,
1814 data->list[j]->dim, isl_dim_out))
1815 return isl_bool_false;
1817 map21 = isl_map_from_basic_map(
1818 isl_basic_map_apply_range(
1819 isl_basic_map_copy(data->list[j]),
1820 isl_basic_map_copy(data->list[i])));
1821 subset = isl_map_is_empty(map21);
1822 if (subset < 0)
1823 goto error;
1824 if (subset) {
1825 isl_map_free(map21);
1826 return isl_bool_false;
1829 if (!isl_space_tuple_is_equal(data->list[i]->dim, isl_dim_in,
1830 data->list[i]->dim, isl_dim_out) ||
1831 !isl_space_tuple_is_equal(data->list[j]->dim, isl_dim_in,
1832 data->list[j]->dim, isl_dim_out)) {
1833 isl_map_free(map21);
1834 return isl_bool_true;
1837 map12 = isl_map_from_basic_map(
1838 isl_basic_map_apply_range(
1839 isl_basic_map_copy(data->list[i]),
1840 isl_basic_map_copy(data->list[j])));
1842 subset = isl_map_is_subset(map21, map12);
1844 isl_map_free(map12);
1845 isl_map_free(map21);
1847 if (subset)
1848 data->check_closed = 1;
1850 return isl_bool_not(subset);
1851 error:
1852 isl_map_free(map21);
1853 return isl_bool_error;
1856 /* Given a union of basic maps R = \cup_i R_i \subseteq D \times D
1857 * and a dimension specification (Z^{n+1} -> Z^{n+1}),
1858 * construct a map that is an overapproximation of the map
1859 * that takes an element from the dom R \times Z to an
1860 * element from ran R \times Z, such that the first n coordinates of the
1861 * difference between them is a sum of differences between images
1862 * and pre-images in one of the R_i and such that the last coordinate
1863 * is equal to the number of steps taken.
1864 * If "project" is set, then these final coordinates are not included,
1865 * i.e., a relation of type Z^n -> Z^n is returned.
1866 * That is, let
1868 * \Delta_i = { y - x | (x, y) in R_i }
1870 * then the constructed map is an overapproximation of
1872 * { (x) -> (x + d) | \exists k_i >= 0, \delta_i \in \Delta_i :
1873 * d = (\sum_i k_i \delta_i, \sum_i k_i) and
1874 * x in dom R and x + d in ran R }
1876 * or
1878 * { (x) -> (x + d) | \exists k_i >= 0, \delta_i \in \Delta_i :
1879 * d = (\sum_i k_i \delta_i) and
1880 * x in dom R and x + d in ran R }
1882 * if "project" is set.
1884 * We first split the map into strongly connected components, perform
1885 * the above on each component and then join the results in the correct
1886 * order, at each join also taking in the union of both arguments
1887 * to allow for paths that do not go through one of the two arguments.
1889 static __isl_give isl_map *construct_power_components(
1890 __isl_take isl_space *space, __isl_keep isl_map *map, int *exact,
1891 int project)
1893 int i, n, c;
1894 struct isl_map *path = NULL;
1895 struct isl_tc_follows_data data;
1896 struct isl_tarjan_graph *g = NULL;
1897 int *orig_exact;
1898 int local_exact;
1900 if (!map)
1901 goto error;
1902 if (map->n <= 1)
1903 return floyd_warshall(space, map, exact, project);
1905 data.list = map->p;
1906 data.check_closed = 0;
1907 g = isl_tarjan_graph_init(map->ctx, map->n, &basic_map_follows, &data);
1908 if (!g)
1909 goto error;
1911 orig_exact = exact;
1912 if (data.check_closed && !exact)
1913 exact = &local_exact;
1915 c = 0;
1916 i = 0;
1917 n = map->n;
1918 if (project)
1919 path = isl_map_empty(isl_map_get_space(map));
1920 else
1921 path = isl_map_empty(isl_space_copy(space));
1922 path = anonymize(path);
1923 while (n) {
1924 struct isl_map *comp;
1925 isl_map *path_comp, *path_comb;
1926 comp = isl_map_alloc_space(isl_map_get_space(map), n, 0);
1927 while (g->order[i] != -1) {
1928 comp = isl_map_add_basic_map(comp,
1929 isl_basic_map_copy(map->p[g->order[i]]));
1930 --n;
1931 ++i;
1933 path_comp = floyd_warshall(isl_space_copy(space),
1934 comp, exact, project);
1935 path_comp = anonymize(path_comp);
1936 path_comb = isl_map_apply_range(isl_map_copy(path),
1937 isl_map_copy(path_comp));
1938 path = isl_map_union(path, path_comp);
1939 path = isl_map_union(path, path_comb);
1940 isl_map_free(comp);
1941 ++i;
1942 ++c;
1945 if (c > 1 && data.check_closed && !*exact) {
1946 int closed;
1948 closed = isl_map_is_transitively_closed(path);
1949 if (closed < 0)
1950 goto error;
1951 if (!closed) {
1952 isl_tarjan_graph_free(g);
1953 isl_map_free(path);
1954 return floyd_warshall(space, map, orig_exact, project);
1958 isl_tarjan_graph_free(g);
1959 isl_space_free(space);
1961 return path;
1962 error:
1963 isl_tarjan_graph_free(g);
1964 isl_space_free(space);
1965 isl_map_free(path);
1966 return NULL;
1969 /* Given a union of basic maps R = \cup_i R_i \subseteq D \times D,
1970 * construct a map that is an overapproximation of the map
1971 * that takes an element from the space D to another
1972 * element from the same space, such that the difference between
1973 * them is a strictly positive sum of differences between images
1974 * and pre-images in one of the R_i.
1975 * The number of differences in the sum is equated to parameter "param".
1976 * That is, let
1978 * \Delta_i = { y - x | (x, y) in R_i }
1980 * then the constructed map is an overapproximation of
1982 * { (x) -> (x + d) | \exists k_i >= 0, \delta_i \in \Delta_i :
1983 * d = \sum_i k_i \delta_i and k = \sum_i k_i > 0 }
1984 * or
1986 * { (x) -> (x + d) | \exists k_i >= 0, \delta_i \in \Delta_i :
1987 * d = \sum_i k_i \delta_i and \sum_i k_i > 0 }
1989 * if "project" is set.
1991 * If "project" is not set, then
1992 * we construct an extended mapping with an extra coordinate
1993 * that indicates the number of steps taken. In particular,
1994 * the difference in the last coordinate is equal to the number
1995 * of steps taken to move from a domain element to the corresponding
1996 * image element(s).
1998 static __isl_give isl_map *construct_power(__isl_keep isl_map *map,
1999 int *exact, int project)
2001 struct isl_map *app = NULL;
2002 isl_space *space = NULL;
2004 if (!map)
2005 return NULL;
2007 space = isl_map_get_space(map);
2009 space = isl_space_add_dims(space, isl_dim_in, 1);
2010 space = isl_space_add_dims(space, isl_dim_out, 1);
2012 app = construct_power_components(isl_space_copy(space), map,
2013 exact, project);
2015 isl_space_free(space);
2017 return app;
2020 /* Compute the positive powers of "map", or an overapproximation.
2021 * If the result is exact, then *exact is set to 1.
2023 * If project is set, then we are actually interested in the transitive
2024 * closure, so we can use a more relaxed exactness check.
2025 * The lengths of the paths are also projected out instead of being
2026 * encoded as the difference between an extra pair of final coordinates.
2028 static __isl_give isl_map *map_power(__isl_take isl_map *map,
2029 int *exact, int project)
2031 struct isl_map *app = NULL;
2033 if (exact)
2034 *exact = 1;
2036 if (isl_map_check_equal_tuples(map) < 0)
2037 return isl_map_free(map);
2039 app = construct_power(map, exact, project);
2041 isl_map_free(map);
2042 return app;
2045 /* Compute the positive powers of "map", or an overapproximation.
2046 * The result maps the exponent to a nested copy of the corresponding power.
2047 * If the result is exact, then *exact is set to 1.
2048 * map_power constructs an extended relation with the path lengths
2049 * encoded as the difference between the final coordinates.
2050 * In the final step, this difference is equated to an extra parameter
2051 * and made positive. The extra coordinates are subsequently projected out
2052 * and the parameter is turned into the domain of the result.
2054 __isl_give isl_map *isl_map_power(__isl_take isl_map *map, int *exact)
2056 isl_space *target_space;
2057 isl_space *space;
2058 isl_map *diff;
2059 isl_size d;
2060 isl_size param;
2062 d = isl_map_dim(map, isl_dim_in);
2063 param = isl_map_dim(map, isl_dim_param);
2064 if (d < 0 || param < 0)
2065 return isl_map_free(map);
2067 map = isl_map_compute_divs(map);
2068 map = isl_map_coalesce(map);
2070 if (isl_map_plain_is_empty(map)) {
2071 map = isl_map_from_range(isl_map_wrap(map));
2072 map = isl_map_add_dims(map, isl_dim_in, 1);
2073 map = isl_map_set_dim_name(map, isl_dim_in, 0, "k");
2074 return map;
2077 target_space = isl_map_get_space(map);
2078 target_space = isl_space_from_range(isl_space_wrap(target_space));
2079 target_space = isl_space_add_dims(target_space, isl_dim_in, 1);
2080 target_space = isl_space_set_dim_name(target_space, isl_dim_in, 0, "k");
2082 map = map_power(map, exact, 0);
2084 map = isl_map_add_dims(map, isl_dim_param, 1);
2085 space = isl_map_get_space(map);
2086 diff = equate_parameter_to_length(space, param);
2087 map = isl_map_intersect(map, diff);
2088 map = isl_map_project_out(map, isl_dim_in, d, 1);
2089 map = isl_map_project_out(map, isl_dim_out, d, 1);
2090 map = isl_map_from_range(isl_map_wrap(map));
2091 map = isl_map_move_dims(map, isl_dim_in, 0, isl_dim_param, param, 1);
2093 map = isl_map_reset_space(map, target_space);
2095 return map;
2098 /* Compute a relation that maps each element in the range of the input
2099 * relation to the lengths of all paths composed of edges in the input
2100 * relation that end up in the given range element.
2101 * The result may be an overapproximation, in which case *exact is set to 0.
2102 * The resulting relation is very similar to the power relation.
2103 * The difference are that the domain has been projected out, the
2104 * range has become the domain and the exponent is the range instead
2105 * of a parameter.
2107 __isl_give isl_map *isl_map_reaching_path_lengths(__isl_take isl_map *map,
2108 int *exact)
2110 isl_space *space;
2111 isl_map *diff;
2112 isl_size d;
2113 isl_size param;
2115 d = isl_map_dim(map, isl_dim_in);
2116 param = isl_map_dim(map, isl_dim_param);
2117 if (d < 0 || param < 0)
2118 return isl_map_free(map);
2120 map = isl_map_compute_divs(map);
2121 map = isl_map_coalesce(map);
2123 if (isl_map_plain_is_empty(map)) {
2124 if (exact)
2125 *exact = 1;
2126 map = isl_map_project_out(map, isl_dim_out, 0, d);
2127 map = isl_map_add_dims(map, isl_dim_out, 1);
2128 return map;
2131 map = map_power(map, exact, 0);
2133 map = isl_map_add_dims(map, isl_dim_param, 1);
2134 space = isl_map_get_space(map);
2135 diff = equate_parameter_to_length(space, param);
2136 map = isl_map_intersect(map, diff);
2137 map = isl_map_project_out(map, isl_dim_in, 0, d + 1);
2138 map = isl_map_project_out(map, isl_dim_out, d, 1);
2139 map = isl_map_reverse(map);
2140 map = isl_map_move_dims(map, isl_dim_out, 0, isl_dim_param, param, 1);
2142 return map;
2145 /* Given a map, compute the smallest superset of this map that is of the form
2147 * { i -> j : L <= j - i <= U and exists a_p: j_p - i_p = M_p a_p }
2149 * (where p ranges over the (non-parametric) dimensions),
2150 * compute the transitive closure of this map, i.e.,
2152 * { i -> j : exists k > 0:
2153 * k L <= j - i <= k U and exists a: j_p - i_p = M_p a_p }
2155 * and intersect domain and range of this transitive closure with
2156 * the given domain and range.
2158 * If with_id is set, then try to include as much of the identity mapping
2159 * as possible, by computing
2161 * { i -> j : exists k >= 0:
2162 * k L <= j - i <= k U and exists a: j_p - i_p = M_p a_p }
2164 * instead (i.e., allow k = 0).
2166 * In practice, we compute the difference set
2168 * delta = { j - i | i -> j in map },
2170 * look for stride constraint on the individual dimensions and compute
2171 * (constant) lower and upper bounds for each individual dimension,
2172 * adding a constraint for each bound not equal to infinity.
2174 static __isl_give isl_map *box_closure_on_domain(__isl_take isl_map *map,
2175 __isl_take isl_set *dom, __isl_take isl_set *ran, int with_id)
2177 int i;
2178 int k;
2179 unsigned d;
2180 unsigned nparam;
2181 unsigned total;
2182 isl_space *dim;
2183 isl_set *delta;
2184 isl_map *app = NULL;
2185 isl_basic_set *aff = NULL;
2186 isl_basic_map *bmap = NULL;
2187 isl_vec *obj = NULL;
2188 isl_int opt;
2190 isl_int_init(opt);
2192 delta = isl_map_deltas(isl_map_copy(map));
2194 aff = isl_set_affine_hull(isl_set_copy(delta));
2195 if (!aff)
2196 goto error;
2197 dim = isl_map_get_space(map);
2198 d = isl_space_dim(dim, isl_dim_in);
2199 nparam = isl_space_dim(dim, isl_dim_param);
2200 total = isl_space_dim(dim, isl_dim_all);
2201 bmap = isl_basic_map_alloc_space(dim,
2202 aff->n_div + 1, aff->n_div, 2 * d + 1);
2203 for (i = 0; i < aff->n_div + 1; ++i) {
2204 k = isl_basic_map_alloc_div(bmap);
2205 if (k < 0)
2206 goto error;
2207 isl_int_set_si(bmap->div[k][0], 0);
2209 for (i = 0; i < aff->n_eq; ++i) {
2210 if (!isl_basic_set_eq_is_stride(aff, i))
2211 continue;
2212 k = isl_basic_map_alloc_equality(bmap);
2213 if (k < 0)
2214 goto error;
2215 isl_seq_clr(bmap->eq[k], 1 + nparam);
2216 isl_seq_cpy(bmap->eq[k] + 1 + nparam + d,
2217 aff->eq[i] + 1 + nparam, d);
2218 isl_seq_neg(bmap->eq[k] + 1 + nparam,
2219 aff->eq[i] + 1 + nparam, d);
2220 isl_seq_cpy(bmap->eq[k] + 1 + nparam + 2 * d,
2221 aff->eq[i] + 1 + nparam + d, aff->n_div);
2222 isl_int_set_si(bmap->eq[k][1 + total + aff->n_div], 0);
2224 obj = isl_vec_alloc(map->ctx, 1 + nparam + d);
2225 if (!obj)
2226 goto error;
2227 isl_seq_clr(obj->el, 1 + nparam + d);
2228 for (i = 0; i < d; ++ i) {
2229 enum isl_lp_result res;
2231 isl_int_set_si(obj->el[1 + nparam + i], 1);
2233 res = isl_set_solve_lp(delta, 0, obj->el, map->ctx->one, &opt,
2234 NULL, NULL);
2235 if (res == isl_lp_error)
2236 goto error;
2237 if (res == isl_lp_ok) {
2238 k = isl_basic_map_alloc_inequality(bmap);
2239 if (k < 0)
2240 goto error;
2241 isl_seq_clr(bmap->ineq[k],
2242 1 + nparam + 2 * d + bmap->n_div);
2243 isl_int_set_si(bmap->ineq[k][1 + nparam + i], -1);
2244 isl_int_set_si(bmap->ineq[k][1 + nparam + d + i], 1);
2245 isl_int_neg(bmap->ineq[k][1 + nparam + 2 * d + aff->n_div], opt);
2248 res = isl_set_solve_lp(delta, 1, obj->el, map->ctx->one, &opt,
2249 NULL, NULL);
2250 if (res == isl_lp_error)
2251 goto error;
2252 if (res == isl_lp_ok) {
2253 k = isl_basic_map_alloc_inequality(bmap);
2254 if (k < 0)
2255 goto error;
2256 isl_seq_clr(bmap->ineq[k],
2257 1 + nparam + 2 * d + bmap->n_div);
2258 isl_int_set_si(bmap->ineq[k][1 + nparam + i], 1);
2259 isl_int_set_si(bmap->ineq[k][1 + nparam + d + i], -1);
2260 isl_int_set(bmap->ineq[k][1 + nparam + 2 * d + aff->n_div], opt);
2263 isl_int_set_si(obj->el[1 + nparam + i], 0);
2265 k = isl_basic_map_alloc_inequality(bmap);
2266 if (k < 0)
2267 goto error;
2268 isl_seq_clr(bmap->ineq[k],
2269 1 + nparam + 2 * d + bmap->n_div);
2270 if (!with_id)
2271 isl_int_set_si(bmap->ineq[k][0], -1);
2272 isl_int_set_si(bmap->ineq[k][1 + nparam + 2 * d + aff->n_div], 1);
2274 app = isl_map_from_domain_and_range(dom, ran);
2276 isl_vec_free(obj);
2277 isl_basic_set_free(aff);
2278 isl_map_free(map);
2279 bmap = isl_basic_map_finalize(bmap);
2280 isl_set_free(delta);
2281 isl_int_clear(opt);
2283 map = isl_map_from_basic_map(bmap);
2284 map = isl_map_intersect(map, app);
2286 return map;
2287 error:
2288 isl_vec_free(obj);
2289 isl_basic_map_free(bmap);
2290 isl_basic_set_free(aff);
2291 isl_set_free(dom);
2292 isl_set_free(ran);
2293 isl_map_free(map);
2294 isl_set_free(delta);
2295 isl_int_clear(opt);
2296 return NULL;
2299 /* Given a map, compute the smallest superset of this map that is of the form
2301 * { i -> j : L <= j - i <= U and exists a_p: j_p - i_p = M_p a_p }
2303 * (where p ranges over the (non-parametric) dimensions),
2304 * compute the transitive closure of this map, i.e.,
2306 * { i -> j : exists k > 0:
2307 * k L <= j - i <= k U and exists a: j_p - i_p = M_p a_p }
2309 * and intersect domain and range of this transitive closure with
2310 * domain and range of the original map.
2312 static __isl_give isl_map *box_closure(__isl_take isl_map *map)
2314 isl_set *domain;
2315 isl_set *range;
2317 domain = isl_map_domain(isl_map_copy(map));
2318 domain = isl_set_coalesce(domain);
2319 range = isl_map_range(isl_map_copy(map));
2320 range = isl_set_coalesce(range);
2322 return box_closure_on_domain(map, domain, range, 0);
2325 /* Given a map, compute the smallest superset of this map that is of the form
2327 * { i -> j : L <= j - i <= U and exists a_p: j_p - i_p = M_p a_p }
2329 * (where p ranges over the (non-parametric) dimensions),
2330 * compute the transitive and partially reflexive closure of this map, i.e.,
2332 * { i -> j : exists k >= 0:
2333 * k L <= j - i <= k U and exists a: j_p - i_p = M_p a_p }
2335 * and intersect domain and range of this transitive closure with
2336 * the given domain.
2338 static __isl_give isl_map *box_closure_with_identity(__isl_take isl_map *map,
2339 __isl_take isl_set *dom)
2341 return box_closure_on_domain(map, dom, isl_set_copy(dom), 1);
2344 /* Check whether app is the transitive closure of map.
2345 * In particular, check that app is acyclic and, if so,
2346 * check that
2348 * app \subset (map \cup (map \circ app))
2350 static isl_bool check_exactness_omega(__isl_keep isl_map *map,
2351 __isl_keep isl_map *app)
2353 isl_set *delta;
2354 int i;
2355 isl_bool is_empty, is_exact;
2356 isl_size d;
2357 isl_map *test;
2359 delta = isl_map_deltas(isl_map_copy(app));
2360 d = isl_set_dim(delta, isl_dim_set);
2361 if (d < 0)
2362 delta = isl_set_free(delta);
2363 for (i = 0; i < d; ++i)
2364 delta = isl_set_fix_si(delta, isl_dim_set, i, 0);
2365 is_empty = isl_set_is_empty(delta);
2366 isl_set_free(delta);
2367 if (is_empty < 0 || !is_empty)
2368 return is_empty;
2370 test = isl_map_apply_range(isl_map_copy(app), isl_map_copy(map));
2371 test = isl_map_union(test, isl_map_copy(map));
2372 is_exact = isl_map_is_subset(app, test);
2373 isl_map_free(test);
2375 return is_exact;
2378 /* Check if basic map M_i can be combined with all the other
2379 * basic maps such that
2381 * (\cup_j M_j)^+
2383 * can be computed as
2385 * M_i \cup (\cup_{j \ne i} M_i^* \circ M_j \circ M_i^*)^+
2387 * In particular, check if we can compute a compact representation
2388 * of
2390 * M_i^* \circ M_j \circ M_i^*
2392 * for each j != i.
2393 * Let M_i^? be an extension of M_i^+ that allows paths
2394 * of length zero, i.e., the result of box_closure(., 1).
2395 * The criterion, as proposed by Kelly et al., is that
2396 * id = M_i^? - M_i^+ can be represented as a basic map
2397 * and that
2399 * id \circ M_j \circ id = M_j
2401 * for each j != i.
2403 * If this function returns 1, then tc and qc are set to
2404 * M_i^+ and M_i^?, respectively.
2406 static int can_be_split_off(__isl_keep isl_map *map, int i,
2407 __isl_give isl_map **tc, __isl_give isl_map **qc)
2409 isl_map *map_i, *id = NULL;
2410 int j = -1;
2411 isl_set *C;
2413 *tc = NULL;
2414 *qc = NULL;
2416 C = isl_set_union(isl_map_domain(isl_map_copy(map)),
2417 isl_map_range(isl_map_copy(map)));
2418 C = isl_set_from_basic_set(isl_set_simple_hull(C));
2419 if (!C)
2420 goto error;
2422 map_i = isl_map_from_basic_map(isl_basic_map_copy(map->p[i]));
2423 *tc = box_closure(isl_map_copy(map_i));
2424 *qc = box_closure_with_identity(map_i, C);
2425 id = isl_map_subtract(isl_map_copy(*qc), isl_map_copy(*tc));
2427 if (!id || !*qc)
2428 goto error;
2429 if (id->n != 1 || (*qc)->n != 1)
2430 goto done;
2432 for (j = 0; j < map->n; ++j) {
2433 isl_map *map_j, *test;
2434 int is_ok;
2436 if (i == j)
2437 continue;
2438 map_j = isl_map_from_basic_map(
2439 isl_basic_map_copy(map->p[j]));
2440 test = isl_map_apply_range(isl_map_copy(id),
2441 isl_map_copy(map_j));
2442 test = isl_map_apply_range(test, isl_map_copy(id));
2443 is_ok = isl_map_is_equal(test, map_j);
2444 isl_map_free(map_j);
2445 isl_map_free(test);
2446 if (is_ok < 0)
2447 goto error;
2448 if (!is_ok)
2449 break;
2452 done:
2453 isl_map_free(id);
2454 if (j == map->n)
2455 return 1;
2457 isl_map_free(*qc);
2458 isl_map_free(*tc);
2459 *qc = NULL;
2460 *tc = NULL;
2462 return 0;
2463 error:
2464 isl_map_free(id);
2465 isl_map_free(*qc);
2466 isl_map_free(*tc);
2467 *qc = NULL;
2468 *tc = NULL;
2469 return -1;
2472 static __isl_give isl_map *box_closure_with_check(__isl_take isl_map *map,
2473 int *exact)
2475 isl_map *app;
2477 app = box_closure(isl_map_copy(map));
2478 if (exact) {
2479 isl_bool is_exact = check_exactness_omega(map, app);
2481 if (is_exact < 0)
2482 app = isl_map_free(app);
2483 else
2484 *exact = is_exact;
2487 isl_map_free(map);
2488 return app;
2491 /* Compute an overapproximation of the transitive closure of "map"
2492 * using a variation of the algorithm from
2493 * "Transitive Closure of Infinite Graphs and its Applications"
2494 * by Kelly et al.
2496 * We first check whether we can can split of any basic map M_i and
2497 * compute
2499 * (\cup_j M_j)^+
2501 * as
2503 * M_i \cup (\cup_{j \ne i} M_i^* \circ M_j \circ M_i^*)^+
2505 * using a recursive call on the remaining map.
2507 * If not, we simply call box_closure on the whole map.
2509 static __isl_give isl_map *transitive_closure_omega(__isl_take isl_map *map,
2510 int *exact)
2512 int i, j;
2513 isl_bool exact_i;
2514 isl_map *app;
2516 if (!map)
2517 return NULL;
2518 if (map->n == 1)
2519 return box_closure_with_check(map, exact);
2521 for (i = 0; i < map->n; ++i) {
2522 int ok;
2523 isl_map *qc, *tc;
2524 ok = can_be_split_off(map, i, &tc, &qc);
2525 if (ok < 0)
2526 goto error;
2527 if (!ok)
2528 continue;
2530 app = isl_map_alloc_space(isl_map_get_space(map), map->n - 1, 0);
2532 for (j = 0; j < map->n; ++j) {
2533 if (j == i)
2534 continue;
2535 app = isl_map_add_basic_map(app,
2536 isl_basic_map_copy(map->p[j]));
2539 app = isl_map_apply_range(isl_map_copy(qc), app);
2540 app = isl_map_apply_range(app, qc);
2542 app = isl_map_union(tc, transitive_closure_omega(app, NULL));
2543 exact_i = check_exactness_omega(map, app);
2544 if (exact_i == isl_bool_true) {
2545 if (exact)
2546 *exact = exact_i;
2547 isl_map_free(map);
2548 return app;
2550 isl_map_free(app);
2551 if (exact_i < 0)
2552 goto error;
2555 return box_closure_with_check(map, exact);
2556 error:
2557 isl_map_free(map);
2558 return NULL;
2561 /* Compute the transitive closure of "map", or an overapproximation.
2562 * If the result is exact, then *exact is set to 1.
2563 * Simply use map_power to compute the powers of map, but tell
2564 * it to project out the lengths of the paths instead of equating
2565 * the length to a parameter.
2567 __isl_give isl_map *isl_map_transitive_closure(__isl_take isl_map *map,
2568 int *exact)
2570 isl_space *target_dim;
2571 int closed;
2573 if (!map)
2574 goto error;
2576 if (map->ctx->opt->closure == ISL_CLOSURE_BOX)
2577 return transitive_closure_omega(map, exact);
2579 map = isl_map_compute_divs(map);
2580 map = isl_map_coalesce(map);
2581 closed = isl_map_is_transitively_closed(map);
2582 if (closed < 0)
2583 goto error;
2584 if (closed) {
2585 if (exact)
2586 *exact = 1;
2587 return map;
2590 target_dim = isl_map_get_space(map);
2591 map = map_power(map, exact, 1);
2592 map = isl_map_reset_space(map, target_dim);
2594 return map;
2595 error:
2596 isl_map_free(map);
2597 return NULL;
2600 static isl_stat inc_count(__isl_take isl_map *map, void *user)
2602 int *n = user;
2604 *n += map->n;
2606 isl_map_free(map);
2608 return isl_stat_ok;
2611 static isl_stat collect_basic_map(__isl_take isl_map *map, void *user)
2613 int i;
2614 isl_basic_map ***next = user;
2616 for (i = 0; i < map->n; ++i) {
2617 **next = isl_basic_map_copy(map->p[i]);
2618 if (!**next)
2619 goto error;
2620 (*next)++;
2623 isl_map_free(map);
2624 return isl_stat_ok;
2625 error:
2626 isl_map_free(map);
2627 return isl_stat_error;
2630 /* Perform Floyd-Warshall on the given list of basic relations.
2631 * The basic relations may live in different dimensions,
2632 * but basic relations that get assigned to the diagonal of the
2633 * grid have domains and ranges of the same dimension and so
2634 * the standard algorithm can be used because the nested transitive
2635 * closures are only applied to diagonal elements and because all
2636 * compositions are peformed on relations with compatible domains and ranges.
2638 static __isl_give isl_union_map *union_floyd_warshall_on_list(isl_ctx *ctx,
2639 __isl_keep isl_basic_map **list, int n, int *exact)
2641 int i, j, k;
2642 int n_group;
2643 int *group = NULL;
2644 isl_set **set = NULL;
2645 isl_map ***grid = NULL;
2646 isl_union_map *app;
2648 group = setup_groups(ctx, list, n, &set, &n_group);
2649 if (!group)
2650 goto error;
2652 grid = isl_calloc_array(ctx, isl_map **, n_group);
2653 if (!grid)
2654 goto error;
2655 for (i = 0; i < n_group; ++i) {
2656 grid[i] = isl_calloc_array(ctx, isl_map *, n_group);
2657 if (!grid[i])
2658 goto error;
2659 for (j = 0; j < n_group; ++j) {
2660 isl_space *space1, *space2, *space;
2661 space1 = isl_space_reverse(isl_set_get_space(set[i]));
2662 space2 = isl_set_get_space(set[j]);
2663 space = isl_space_join(space1, space2);
2664 grid[i][j] = isl_map_empty(space);
2668 for (k = 0; k < n; ++k) {
2669 i = group[2 * k];
2670 j = group[2 * k + 1];
2671 grid[i][j] = isl_map_union(grid[i][j],
2672 isl_map_from_basic_map(
2673 isl_basic_map_copy(list[k])));
2676 floyd_warshall_iterate(grid, n_group, exact);
2678 app = isl_union_map_empty(isl_map_get_space(grid[0][0]));
2680 for (i = 0; i < n_group; ++i) {
2681 for (j = 0; j < n_group; ++j)
2682 app = isl_union_map_add_map(app, grid[i][j]);
2683 free(grid[i]);
2685 free(grid);
2687 for (i = 0; i < 2 * n; ++i)
2688 isl_set_free(set[i]);
2689 free(set);
2691 free(group);
2692 return app;
2693 error:
2694 if (grid)
2695 for (i = 0; i < n_group; ++i) {
2696 if (!grid[i])
2697 continue;
2698 for (j = 0; j < n_group; ++j)
2699 isl_map_free(grid[i][j]);
2700 free(grid[i]);
2702 free(grid);
2703 if (set) {
2704 for (i = 0; i < 2 * n; ++i)
2705 isl_set_free(set[i]);
2706 free(set);
2708 free(group);
2709 return NULL;
2712 /* Perform Floyd-Warshall on the given union relation.
2713 * The implementation is very similar to that for non-unions.
2714 * The main difference is that it is applied unconditionally.
2715 * We first extract a list of basic maps from the union map
2716 * and then perform the algorithm on this list.
2718 static __isl_give isl_union_map *union_floyd_warshall(
2719 __isl_take isl_union_map *umap, int *exact)
2721 int i, n;
2722 isl_ctx *ctx;
2723 isl_basic_map **list = NULL;
2724 isl_basic_map **next;
2725 isl_union_map *res;
2727 n = 0;
2728 if (isl_union_map_foreach_map(umap, inc_count, &n) < 0)
2729 goto error;
2731 ctx = isl_union_map_get_ctx(umap);
2732 list = isl_calloc_array(ctx, isl_basic_map *, n);
2733 if (!list)
2734 goto error;
2736 next = list;
2737 if (isl_union_map_foreach_map(umap, collect_basic_map, &next) < 0)
2738 goto error;
2740 res = union_floyd_warshall_on_list(ctx, list, n, exact);
2742 if (list) {
2743 for (i = 0; i < n; ++i)
2744 isl_basic_map_free(list[i]);
2745 free(list);
2748 isl_union_map_free(umap);
2749 return res;
2750 error:
2751 if (list) {
2752 for (i = 0; i < n; ++i)
2753 isl_basic_map_free(list[i]);
2754 free(list);
2756 isl_union_map_free(umap);
2757 return NULL;
2760 /* Decompose the give union relation into strongly connected components.
2761 * The implementation is essentially the same as that of
2762 * construct_power_components with the major difference that all
2763 * operations are performed on union maps.
2765 static __isl_give isl_union_map *union_components(
2766 __isl_take isl_union_map *umap, int *exact)
2768 int i;
2769 int n;
2770 isl_ctx *ctx;
2771 isl_basic_map **list = NULL;
2772 isl_basic_map **next;
2773 isl_union_map *path = NULL;
2774 struct isl_tc_follows_data data;
2775 struct isl_tarjan_graph *g = NULL;
2776 int c, l;
2777 int recheck = 0;
2779 n = 0;
2780 if (isl_union_map_foreach_map(umap, inc_count, &n) < 0)
2781 goto error;
2783 if (n == 0)
2784 return umap;
2785 if (n <= 1)
2786 return union_floyd_warshall(umap, exact);
2788 ctx = isl_union_map_get_ctx(umap);
2789 list = isl_calloc_array(ctx, isl_basic_map *, n);
2790 if (!list)
2791 goto error;
2793 next = list;
2794 if (isl_union_map_foreach_map(umap, collect_basic_map, &next) < 0)
2795 goto error;
2797 data.list = list;
2798 data.check_closed = 0;
2799 g = isl_tarjan_graph_init(ctx, n, &basic_map_follows, &data);
2800 if (!g)
2801 goto error;
2803 c = 0;
2804 i = 0;
2805 l = n;
2806 path = isl_union_map_empty(isl_union_map_get_space(umap));
2807 while (l) {
2808 isl_union_map *comp;
2809 isl_union_map *path_comp, *path_comb;
2810 comp = isl_union_map_empty(isl_union_map_get_space(umap));
2811 while (g->order[i] != -1) {
2812 comp = isl_union_map_add_map(comp,
2813 isl_map_from_basic_map(
2814 isl_basic_map_copy(list[g->order[i]])));
2815 --l;
2816 ++i;
2818 path_comp = union_floyd_warshall(comp, exact);
2819 path_comb = isl_union_map_apply_range(isl_union_map_copy(path),
2820 isl_union_map_copy(path_comp));
2821 path = isl_union_map_union(path, path_comp);
2822 path = isl_union_map_union(path, path_comb);
2823 ++i;
2824 ++c;
2827 if (c > 1 && data.check_closed && !*exact) {
2828 int closed;
2830 closed = isl_union_map_is_transitively_closed(path);
2831 if (closed < 0)
2832 goto error;
2833 recheck = !closed;
2836 isl_tarjan_graph_free(g);
2838 for (i = 0; i < n; ++i)
2839 isl_basic_map_free(list[i]);
2840 free(list);
2842 if (recheck) {
2843 isl_union_map_free(path);
2844 return union_floyd_warshall(umap, exact);
2847 isl_union_map_free(umap);
2849 return path;
2850 error:
2851 isl_tarjan_graph_free(g);
2852 if (list) {
2853 for (i = 0; i < n; ++i)
2854 isl_basic_map_free(list[i]);
2855 free(list);
2857 isl_union_map_free(umap);
2858 isl_union_map_free(path);
2859 return NULL;
2862 /* Compute the transitive closure of "umap", or an overapproximation.
2863 * If the result is exact, then *exact is set to 1.
2865 __isl_give isl_union_map *isl_union_map_transitive_closure(
2866 __isl_take isl_union_map *umap, int *exact)
2868 int closed;
2870 if (!umap)
2871 return NULL;
2873 if (exact)
2874 *exact = 1;
2876 umap = isl_union_map_compute_divs(umap);
2877 umap = isl_union_map_coalesce(umap);
2878 closed = isl_union_map_is_transitively_closed(umap);
2879 if (closed < 0)
2880 goto error;
2881 if (closed)
2882 return umap;
2883 umap = union_components(umap, exact);
2884 return umap;
2885 error:
2886 isl_union_map_free(umap);
2887 return NULL;
2890 struct isl_union_power {
2891 isl_union_map *pow;
2892 int *exact;
2895 static isl_stat power(__isl_take isl_map *map, void *user)
2897 struct isl_union_power *up = user;
2899 map = isl_map_power(map, up->exact);
2900 up->pow = isl_union_map_from_map(map);
2902 return isl_stat_error;
2905 /* Construct a map [[x]->[y]] -> [y-x], with parameters prescribed by "dim".
2907 static __isl_give isl_union_map *deltas_map(__isl_take isl_space *dim)
2909 isl_basic_map *bmap;
2911 dim = isl_space_add_dims(dim, isl_dim_in, 1);
2912 dim = isl_space_add_dims(dim, isl_dim_out, 1);
2913 bmap = isl_basic_map_universe(dim);
2914 bmap = isl_basic_map_deltas_map(bmap);
2916 return isl_union_map_from_map(isl_map_from_basic_map(bmap));
2919 /* Compute the positive powers of "map", or an overapproximation.
2920 * The result maps the exponent to a nested copy of the corresponding power.
2921 * If the result is exact, then *exact is set to 1.
2923 __isl_give isl_union_map *isl_union_map_power(__isl_take isl_union_map *umap,
2924 int *exact)
2926 isl_size n;
2927 isl_union_map *inc;
2928 isl_union_map *dm;
2930 n = isl_union_map_n_map(umap);
2931 if (n < 0)
2932 return isl_union_map_free(umap);
2933 if (n == 0)
2934 return umap;
2935 if (n == 1) {
2936 struct isl_union_power up = { NULL, exact };
2937 isl_union_map_foreach_map(umap, &power, &up);
2938 isl_union_map_free(umap);
2939 return up.pow;
2941 inc = isl_union_map_from_map(increment(isl_union_map_get_space(umap)));
2942 umap = isl_union_map_product(inc, umap);
2943 umap = isl_union_map_transitive_closure(umap, exact);
2944 umap = isl_union_map_zip(umap);
2945 dm = deltas_map(isl_union_map_get_space(umap));
2946 umap = isl_union_map_apply_domain(umap, dm);
2948 return umap;
2951 #undef TYPE
2952 #define TYPE isl_map
2953 #include "isl_power_templ.c"
2955 #undef TYPE
2956 #define TYPE isl_union_map
2957 #include "isl_power_templ.c"