add isl_union_set_project_out
[isl.git] / isl_transitive_closure.c
blobd91a58f37875d2ce18a519ddb53aa59f9839eb2a
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 *dim;
58 struct isl_basic_map *bmap;
59 unsigned d;
60 unsigned nparam;
61 int k;
62 isl_int *c;
64 if (!map)
65 return NULL;
67 dim = isl_map_get_space(map);
68 d = isl_space_dim(dim, isl_dim_in);
69 nparam = isl_space_dim(dim, isl_dim_param);
70 bmap = isl_basic_map_alloc_space(dim, 0, 1, 1);
71 if (exactly) {
72 k = isl_basic_map_alloc_equality(bmap);
73 c = bmap->eq[k];
74 } else {
75 k = isl_basic_map_alloc_inequality(bmap);
76 c = bmap->ineq[k];
78 if (k < 0)
79 goto error;
80 isl_seq_clr(c, 1 + isl_basic_map_total_dim(bmap));
81 isl_int_set_si(c[0], -length);
82 isl_int_set_si(c[1 + nparam + d - 1], -1);
83 isl_int_set_si(c[1 + nparam + d + d - 1], 1);
85 bmap = isl_basic_map_finalize(bmap);
86 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
88 return map;
89 error:
90 isl_basic_map_free(bmap);
91 isl_map_free(map);
92 return NULL;
95 /* Check whether the overapproximation of the power of "map" is exactly
96 * the power of "map". Let R be "map" and A_k the overapproximation.
97 * The approximation is exact if
99 * A_1 = R
100 * A_k = A_{k-1} \circ R k >= 2
102 * Since A_k is known to be an overapproximation, we only need to check
104 * A_1 \subset R
105 * A_k \subset A_{k-1} \circ R k >= 2
107 * In practice, "app" has an extra input and output coordinate
108 * to encode the length of the path. So, we first need to add
109 * this coordinate to "map" and set the length of the path to
110 * one.
112 static int check_power_exactness(__isl_take isl_map *map,
113 __isl_take isl_map *app)
115 int exact;
116 isl_map *app_1;
117 isl_map *app_2;
119 map = isl_map_add_dims(map, isl_dim_in, 1);
120 map = isl_map_add_dims(map, isl_dim_out, 1);
121 map = set_path_length(map, 1, 1);
123 app_1 = set_path_length(isl_map_copy(app), 1, 1);
125 exact = isl_map_is_subset(app_1, map);
126 isl_map_free(app_1);
128 if (!exact || exact < 0) {
129 isl_map_free(app);
130 isl_map_free(map);
131 return exact;
134 app_1 = set_path_length(isl_map_copy(app), 0, 1);
135 app_2 = set_path_length(app, 0, 2);
136 app_1 = isl_map_apply_range(map, app_1);
138 exact = isl_map_is_subset(app_2, app_1);
140 isl_map_free(app_1);
141 isl_map_free(app_2);
143 return exact;
146 /* Check whether the overapproximation of the power of "map" is exactly
147 * the power of "map", possibly after projecting out the power (if "project"
148 * is set).
150 * If "project" is set and if "steps" can only result in acyclic paths,
151 * then we check
153 * A = R \cup (A \circ R)
155 * where A is the overapproximation with the power projected out, i.e.,
156 * an overapproximation of the transitive closure.
157 * More specifically, since A is known to be an overapproximation, we check
159 * A \subset R \cup (A \circ R)
161 * Otherwise, we check if the power is exact.
163 * Note that "app" has an extra input and output coordinate to encode
164 * the length of the part. If we are only interested in the transitive
165 * closure, then we can simply project out these coordinates first.
167 static int check_exactness(__isl_take isl_map *map, __isl_take isl_map *app,
168 int project)
170 isl_map *test;
171 int exact;
172 unsigned d;
174 if (!project)
175 return check_power_exactness(map, app);
177 d = isl_map_dim(map, isl_dim_in);
178 app = set_path_length(app, 0, 1);
179 app = isl_map_project_out(app, isl_dim_in, d, 1);
180 app = isl_map_project_out(app, isl_dim_out, d, 1);
182 app = isl_map_reset_space(app, isl_map_get_space(map));
184 test = isl_map_apply_range(isl_map_copy(map), isl_map_copy(app));
185 test = isl_map_union(test, isl_map_copy(map));
187 exact = isl_map_is_subset(app, test);
189 isl_map_free(app);
190 isl_map_free(test);
192 isl_map_free(map);
194 return exact;
198 * The transitive closure implementation is based on the paper
199 * "Computing the Transitive Closure of a Union of Affine Integer
200 * Tuple Relations" by Anna Beletska, Denis Barthou, Wlodzimierz Bielecki and
201 * Albert Cohen.
204 /* Given a set of n offsets v_i (the rows of "steps"), construct a relation
205 * of the given dimension specification (Z^{n+1} -> Z^{n+1})
206 * that maps an element x to any element that can be reached
207 * by taking a non-negative number of steps along any of
208 * the extended offsets v'_i = [v_i 1].
209 * That is, construct
211 * { [x] -> [y] : exists k_i >= 0, y = x + \sum_i k_i v'_i }
213 * For any element in this relation, the number of steps taken
214 * is equal to the difference in the final coordinates.
216 static __isl_give isl_map *path_along_steps(__isl_take isl_space *dim,
217 __isl_keep isl_mat *steps)
219 int i, j, k;
220 struct isl_basic_map *path = NULL;
221 unsigned d;
222 unsigned n;
223 unsigned nparam;
225 if (!dim || !steps)
226 goto error;
228 d = isl_space_dim(dim, isl_dim_in);
229 n = steps->n_row;
230 nparam = isl_space_dim(dim, isl_dim_param);
232 path = isl_basic_map_alloc_space(isl_space_copy(dim), n, d, n);
234 for (i = 0; i < n; ++i) {
235 k = isl_basic_map_alloc_div(path);
236 if (k < 0)
237 goto error;
238 isl_assert(steps->ctx, i == k, goto error);
239 isl_int_set_si(path->div[k][0], 0);
242 for (i = 0; i < d; ++i) {
243 k = isl_basic_map_alloc_equality(path);
244 if (k < 0)
245 goto error;
246 isl_seq_clr(path->eq[k], 1 + isl_basic_map_total_dim(path));
247 isl_int_set_si(path->eq[k][1 + nparam + i], 1);
248 isl_int_set_si(path->eq[k][1 + nparam + d + i], -1);
249 if (i == d - 1)
250 for (j = 0; j < n; ++j)
251 isl_int_set_si(path->eq[k][1 + nparam + 2 * d + j], 1);
252 else
253 for (j = 0; j < n; ++j)
254 isl_int_set(path->eq[k][1 + nparam + 2 * d + j],
255 steps->row[j][i]);
258 for (i = 0; i < n; ++i) {
259 k = isl_basic_map_alloc_inequality(path);
260 if (k < 0)
261 goto error;
262 isl_seq_clr(path->ineq[k], 1 + isl_basic_map_total_dim(path));
263 isl_int_set_si(path->ineq[k][1 + nparam + 2 * d + i], 1);
266 isl_space_free(dim);
268 path = isl_basic_map_simplify(path);
269 path = isl_basic_map_finalize(path);
270 return isl_map_from_basic_map(path);
271 error:
272 isl_space_free(dim);
273 isl_basic_map_free(path);
274 return NULL;
277 #define IMPURE 0
278 #define PURE_PARAM 1
279 #define PURE_VAR 2
280 #define MIXED 3
282 /* Check whether the parametric constant term of constraint c is never
283 * positive in "bset".
285 static int parametric_constant_never_positive(__isl_keep isl_basic_set *bset,
286 isl_int *c, int *div_purity)
288 unsigned d;
289 unsigned n_div;
290 unsigned nparam;
291 int i;
292 int k;
293 int empty;
295 n_div = isl_basic_set_dim(bset, isl_dim_div);
296 d = isl_basic_set_dim(bset, isl_dim_set);
297 nparam = isl_basic_set_dim(bset, isl_dim_param);
299 bset = isl_basic_set_copy(bset);
300 bset = isl_basic_set_cow(bset);
301 bset = isl_basic_set_extend_constraints(bset, 0, 1);
302 k = isl_basic_set_alloc_inequality(bset);
303 if (k < 0)
304 goto error;
305 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
306 isl_seq_cpy(bset->ineq[k], c, 1 + nparam);
307 for (i = 0; i < n_div; ++i) {
308 if (div_purity[i] != PURE_PARAM)
309 continue;
310 isl_int_set(bset->ineq[k][1 + nparam + d + i],
311 c[1 + nparam + d + i]);
313 isl_int_sub_ui(bset->ineq[k][0], bset->ineq[k][0], 1);
314 empty = isl_basic_set_is_empty(bset);
315 isl_basic_set_free(bset);
317 return empty;
318 error:
319 isl_basic_set_free(bset);
320 return -1;
323 /* Return PURE_PARAM if only the coefficients of the parameters are non-zero.
324 * Return PURE_VAR if only the coefficients of the set variables are non-zero.
325 * Return MIXED if only the coefficients of the parameters and the set
326 * variables are non-zero and if moreover the parametric constant
327 * can never attain positive values.
328 * Return IMPURE otherwise.
330 static int purity(__isl_keep isl_basic_set *bset, isl_int *c, int *div_purity,
331 int eq)
333 unsigned d;
334 unsigned n_div;
335 unsigned nparam;
336 int empty;
337 int i;
338 int p = 0, v = 0;
340 n_div = isl_basic_set_dim(bset, isl_dim_div);
341 d = isl_basic_set_dim(bset, isl_dim_set);
342 nparam = isl_basic_set_dim(bset, isl_dim_param);
344 for (i = 0; i < n_div; ++i) {
345 if (isl_int_is_zero(c[1 + nparam + d + i]))
346 continue;
347 switch (div_purity[i]) {
348 case PURE_PARAM: p = 1; break;
349 case PURE_VAR: v = 1; break;
350 default: return IMPURE;
353 if (!p && isl_seq_first_non_zero(c + 1, nparam) == -1)
354 return PURE_VAR;
355 if (!v && isl_seq_first_non_zero(c + 1 + nparam, d) == -1)
356 return PURE_PARAM;
358 empty = parametric_constant_never_positive(bset, c, div_purity);
359 if (eq && empty >= 0 && !empty) {
360 isl_seq_neg(c, c, 1 + nparam + d + n_div);
361 empty = parametric_constant_never_positive(bset, c, div_purity);
364 return empty < 0 ? -1 : empty ? MIXED : IMPURE;
367 /* Return an array of integers indicating the type of each div in bset.
368 * If the div is (recursively) defined in terms of only the parameters,
369 * then the type is PURE_PARAM.
370 * If the div is (recursively) defined in terms of only the set variables,
371 * then the type is PURE_VAR.
372 * Otherwise, the type is IMPURE.
374 static __isl_give int *get_div_purity(__isl_keep isl_basic_set *bset)
376 int i, j;
377 int *div_purity;
378 unsigned d;
379 unsigned n_div;
380 unsigned nparam;
382 if (!bset)
383 return NULL;
385 n_div = isl_basic_set_dim(bset, isl_dim_div);
386 d = isl_basic_set_dim(bset, isl_dim_set);
387 nparam = isl_basic_set_dim(bset, isl_dim_param);
389 div_purity = isl_alloc_array(bset->ctx, int, n_div);
390 if (n_div && !div_purity)
391 return NULL;
393 for (i = 0; i < bset->n_div; ++i) {
394 int p = 0, v = 0;
395 if (isl_int_is_zero(bset->div[i][0])) {
396 div_purity[i] = IMPURE;
397 continue;
399 if (isl_seq_first_non_zero(bset->div[i] + 2, nparam) != -1)
400 p = 1;
401 if (isl_seq_first_non_zero(bset->div[i] + 2 + nparam, d) != -1)
402 v = 1;
403 for (j = 0; j < i; ++j) {
404 if (isl_int_is_zero(bset->div[i][2 + nparam + d + j]))
405 continue;
406 switch (div_purity[j]) {
407 case PURE_PARAM: p = 1; break;
408 case PURE_VAR: v = 1; break;
409 default: p = v = 1; break;
412 div_purity[i] = v ? p ? IMPURE : PURE_VAR : PURE_PARAM;
415 return div_purity;
418 /* Given a path with the as yet unconstrained length at position "pos",
419 * check if setting the length to zero results in only the identity
420 * mapping.
422 static int empty_path_is_identity(__isl_keep isl_basic_map *path, unsigned pos)
424 isl_basic_map *test = NULL;
425 isl_basic_map *id = NULL;
426 int k;
427 int is_id;
429 test = isl_basic_map_copy(path);
430 test = isl_basic_map_extend_constraints(test, 1, 0);
431 k = isl_basic_map_alloc_equality(test);
432 if (k < 0)
433 goto error;
434 isl_seq_clr(test->eq[k], 1 + isl_basic_map_total_dim(test));
435 isl_int_set_si(test->eq[k][pos], 1);
436 id = isl_basic_map_identity(isl_basic_map_get_space(path));
437 is_id = isl_basic_map_is_equal(test, id);
438 isl_basic_map_free(test);
439 isl_basic_map_free(id);
440 return is_id;
441 error:
442 isl_basic_map_free(test);
443 return -1;
446 /* If any of the constraints is found to be impure then this function
447 * sets *impurity to 1.
449 * If impurity is NULL then we are dealing with a non-parametric set
450 * and so the constraints are obviously PURE_VAR.
452 static __isl_give isl_basic_map *add_delta_constraints(
453 __isl_take isl_basic_map *path,
454 __isl_keep isl_basic_set *delta, unsigned off, unsigned nparam,
455 unsigned d, int *div_purity, int eq, int *impurity)
457 int i, k;
458 int n = eq ? delta->n_eq : delta->n_ineq;
459 isl_int **delta_c = eq ? delta->eq : delta->ineq;
460 unsigned n_div;
462 n_div = isl_basic_set_dim(delta, isl_dim_div);
464 for (i = 0; i < n; ++i) {
465 isl_int *path_c;
466 int p = PURE_VAR;
467 if (impurity)
468 p = purity(delta, delta_c[i], div_purity, eq);
469 if (p < 0)
470 goto error;
471 if (p != PURE_VAR && p != PURE_PARAM && !*impurity)
472 *impurity = 1;
473 if (p == IMPURE)
474 continue;
475 if (eq && p != MIXED) {
476 k = isl_basic_map_alloc_equality(path);
477 path_c = path->eq[k];
478 } else {
479 k = isl_basic_map_alloc_inequality(path);
480 path_c = path->ineq[k];
482 if (k < 0)
483 goto error;
484 isl_seq_clr(path_c, 1 + isl_basic_map_total_dim(path));
485 if (p == PURE_VAR) {
486 isl_seq_cpy(path_c + off,
487 delta_c[i] + 1 + nparam, d);
488 isl_int_set(path_c[off + d], delta_c[i][0]);
489 } else if (p == PURE_PARAM) {
490 isl_seq_cpy(path_c, delta_c[i], 1 + nparam);
491 } else {
492 isl_seq_cpy(path_c + off,
493 delta_c[i] + 1 + nparam, d);
494 isl_seq_cpy(path_c, delta_c[i], 1 + nparam);
496 isl_seq_cpy(path_c + off - n_div,
497 delta_c[i] + 1 + nparam + d, n_div);
500 return path;
501 error:
502 isl_basic_map_free(path);
503 return NULL;
506 /* Given a set of offsets "delta", construct a relation of the
507 * given dimension specification (Z^{n+1} -> Z^{n+1}) that
508 * is an overapproximation of the relations that
509 * maps an element x to any element that can be reached
510 * by taking a non-negative number of steps along any of
511 * the elements in "delta".
512 * That is, construct an approximation of
514 * { [x] -> [y] : exists f \in \delta, k \in Z :
515 * y = x + k [f, 1] and k >= 0 }
517 * For any element in this relation, the number of steps taken
518 * is equal to the difference in the final coordinates.
520 * In particular, let delta be defined as
522 * \delta = [p] -> { [x] : A x + a >= 0 and B p + b >= 0 and
523 * C x + C'p + c >= 0 and
524 * D x + D'p + d >= 0 }
526 * where the constraints C x + C'p + c >= 0 are such that the parametric
527 * constant term of each constraint j, "C_j x + C'_j p + c_j",
528 * can never attain positive values, then the relation is constructed as
530 * { [x] -> [y] : exists [f, k] \in Z^{n+1} : y = x + f and
531 * A f + k a >= 0 and B p + b >= 0 and
532 * C f + C'p + c >= 0 and k >= 1 }
533 * union { [x] -> [x] }
535 * If the zero-length paths happen to correspond exactly to the identity
536 * mapping, then we return
538 * { [x] -> [y] : exists [f, k] \in Z^{n+1} : y = x + f and
539 * A f + k a >= 0 and B p + b >= 0 and
540 * C f + C'p + c >= 0 and k >= 0 }
542 * instead.
544 * Existentially quantified variables in \delta are handled by
545 * classifying them as independent of the parameters, purely
546 * parameter dependent and others. Constraints containing
547 * any of the other existentially quantified variables are removed.
548 * This is safe, but leads to an additional overapproximation.
550 * If there are any impure constraints, then we also eliminate
551 * the parameters from \delta, resulting in a set
553 * \delta' = { [x] : E x + e >= 0 }
555 * and add the constraints
557 * E f + k e >= 0
559 * to the constructed relation.
561 static __isl_give isl_map *path_along_delta(__isl_take isl_space *dim,
562 __isl_take isl_basic_set *delta)
564 isl_basic_map *path = NULL;
565 unsigned d;
566 unsigned n_div;
567 unsigned nparam;
568 unsigned off;
569 int i, k;
570 int is_id;
571 int *div_purity = NULL;
572 int impurity = 0;
574 if (!delta)
575 goto error;
576 n_div = isl_basic_set_dim(delta, isl_dim_div);
577 d = isl_basic_set_dim(delta, isl_dim_set);
578 nparam = isl_basic_set_dim(delta, isl_dim_param);
579 path = isl_basic_map_alloc_space(isl_space_copy(dim), n_div + d + 1,
580 d + 1 + delta->n_eq, delta->n_eq + delta->n_ineq + 1);
581 off = 1 + nparam + 2 * (d + 1) + n_div;
583 for (i = 0; i < n_div + d + 1; ++i) {
584 k = isl_basic_map_alloc_div(path);
585 if (k < 0)
586 goto error;
587 isl_int_set_si(path->div[k][0], 0);
590 for (i = 0; i < d + 1; ++i) {
591 k = isl_basic_map_alloc_equality(path);
592 if (k < 0)
593 goto error;
594 isl_seq_clr(path->eq[k], 1 + isl_basic_map_total_dim(path));
595 isl_int_set_si(path->eq[k][1 + nparam + i], 1);
596 isl_int_set_si(path->eq[k][1 + nparam + d + 1 + i], -1);
597 isl_int_set_si(path->eq[k][off + i], 1);
600 div_purity = get_div_purity(delta);
601 if (n_div && !div_purity)
602 goto error;
604 path = add_delta_constraints(path, delta, off, nparam, d,
605 div_purity, 1, &impurity);
606 path = add_delta_constraints(path, delta, off, nparam, d,
607 div_purity, 0, &impurity);
608 if (impurity) {
609 isl_space *dim = isl_basic_set_get_space(delta);
610 delta = isl_basic_set_project_out(delta,
611 isl_dim_param, 0, nparam);
612 delta = isl_basic_set_add_dims(delta, isl_dim_param, nparam);
613 delta = isl_basic_set_reset_space(delta, dim);
614 if (!delta)
615 goto error;
616 path = isl_basic_map_extend_constraints(path, delta->n_eq,
617 delta->n_ineq + 1);
618 path = add_delta_constraints(path, delta, off, nparam, d,
619 NULL, 1, NULL);
620 path = add_delta_constraints(path, delta, off, nparam, d,
621 NULL, 0, NULL);
622 path = isl_basic_map_gauss(path, NULL);
625 is_id = empty_path_is_identity(path, off + d);
626 if (is_id < 0)
627 goto error;
629 k = isl_basic_map_alloc_inequality(path);
630 if (k < 0)
631 goto error;
632 isl_seq_clr(path->ineq[k], 1 + isl_basic_map_total_dim(path));
633 if (!is_id)
634 isl_int_set_si(path->ineq[k][0], -1);
635 isl_int_set_si(path->ineq[k][off + d], 1);
637 free(div_purity);
638 isl_basic_set_free(delta);
639 path = isl_basic_map_finalize(path);
640 if (is_id) {
641 isl_space_free(dim);
642 return isl_map_from_basic_map(path);
644 return isl_basic_map_union(path, isl_basic_map_identity(dim));
645 error:
646 free(div_purity);
647 isl_space_free(dim);
648 isl_basic_set_free(delta);
649 isl_basic_map_free(path);
650 return NULL;
653 /* Given a dimension specification Z^{n+1} -> Z^{n+1} and a parameter "param",
654 * construct a map that equates the parameter to the difference
655 * in the final coordinates and imposes that this difference is positive.
656 * That is, construct
658 * { [x,x_s] -> [y,y_s] : k = y_s - x_s > 0 }
660 static __isl_give isl_map *equate_parameter_to_length(__isl_take isl_space *dim,
661 unsigned param)
663 struct isl_basic_map *bmap;
664 unsigned d;
665 unsigned nparam;
666 int k;
668 d = isl_space_dim(dim, isl_dim_in);
669 nparam = isl_space_dim(dim, isl_dim_param);
670 bmap = isl_basic_map_alloc_space(dim, 0, 1, 1);
671 k = isl_basic_map_alloc_equality(bmap);
672 if (k < 0)
673 goto error;
674 isl_seq_clr(bmap->eq[k], 1 + isl_basic_map_total_dim(bmap));
675 isl_int_set_si(bmap->eq[k][1 + param], -1);
676 isl_int_set_si(bmap->eq[k][1 + nparam + d - 1], -1);
677 isl_int_set_si(bmap->eq[k][1 + nparam + d + d - 1], 1);
679 k = isl_basic_map_alloc_inequality(bmap);
680 if (k < 0)
681 goto error;
682 isl_seq_clr(bmap->ineq[k], 1 + isl_basic_map_total_dim(bmap));
683 isl_int_set_si(bmap->ineq[k][1 + param], 1);
684 isl_int_set_si(bmap->ineq[k][0], -1);
686 bmap = isl_basic_map_finalize(bmap);
687 return isl_map_from_basic_map(bmap);
688 error:
689 isl_basic_map_free(bmap);
690 return NULL;
693 /* Check whether "path" is acyclic, where the last coordinates of domain
694 * and range of path encode the number of steps taken.
695 * That is, check whether
697 * { d | d = y - x and (x,y) in path }
699 * does not contain any element with positive last coordinate (positive length)
700 * and zero remaining coordinates (cycle).
702 static int is_acyclic(__isl_take isl_map *path)
704 int i;
705 int acyclic;
706 unsigned dim;
707 struct isl_set *delta;
709 delta = isl_map_deltas(path);
710 dim = isl_set_dim(delta, isl_dim_set);
711 for (i = 0; i < dim; ++i) {
712 if (i == dim -1)
713 delta = isl_set_lower_bound_si(delta, isl_dim_set, i, 1);
714 else
715 delta = isl_set_fix_si(delta, isl_dim_set, i, 0);
718 acyclic = isl_set_is_empty(delta);
719 isl_set_free(delta);
721 return acyclic;
724 /* Given a union of basic maps R = \cup_i R_i \subseteq D \times D
725 * and a dimension specification (Z^{n+1} -> Z^{n+1}),
726 * construct a map that is an overapproximation of the map
727 * that takes an element from the space D \times Z to another
728 * element from the same space, such that the first n coordinates of the
729 * difference between them is a sum of differences between images
730 * and pre-images in one of the R_i and such that the last coordinate
731 * is equal to the number of steps taken.
732 * That is, let
734 * \Delta_i = { y - x | (x, y) in R_i }
736 * then the constructed map is an overapproximation of
738 * { (x) -> (x + d) | \exists k_i >= 0, \delta_i \in \Delta_i :
739 * d = (\sum_i k_i \delta_i, \sum_i k_i) }
741 * The elements of the singleton \Delta_i's are collected as the
742 * rows of the steps matrix. For all these \Delta_i's together,
743 * a single path is constructed.
744 * For each of the other \Delta_i's, we compute an overapproximation
745 * of the paths along elements of \Delta_i.
746 * Since each of these paths performs an addition, composition is
747 * symmetric and we can simply compose all resulting paths in any order.
749 static __isl_give isl_map *construct_extended_path(__isl_take isl_space *dim,
750 __isl_keep isl_map *map, int *project)
752 struct isl_mat *steps = NULL;
753 struct isl_map *path = NULL;
754 unsigned d;
755 int i, j, n;
757 d = isl_map_dim(map, isl_dim_in);
759 path = isl_map_identity(isl_space_copy(dim));
761 steps = isl_mat_alloc(map->ctx, map->n, d);
762 if (!steps)
763 goto error;
765 n = 0;
766 for (i = 0; i < map->n; ++i) {
767 struct isl_basic_set *delta;
769 delta = isl_basic_map_deltas(isl_basic_map_copy(map->p[i]));
771 for (j = 0; j < d; ++j) {
772 int fixed;
774 fixed = isl_basic_set_plain_dim_is_fixed(delta, j,
775 &steps->row[n][j]);
776 if (fixed < 0) {
777 isl_basic_set_free(delta);
778 goto error;
780 if (!fixed)
781 break;
785 if (j < d) {
786 path = isl_map_apply_range(path,
787 path_along_delta(isl_space_copy(dim), delta));
788 path = isl_map_coalesce(path);
789 } else {
790 isl_basic_set_free(delta);
791 ++n;
795 if (n > 0) {
796 steps->n_row = n;
797 path = isl_map_apply_range(path,
798 path_along_steps(isl_space_copy(dim), steps));
801 if (project && *project) {
802 *project = is_acyclic(isl_map_copy(path));
803 if (*project < 0)
804 goto error;
807 isl_space_free(dim);
808 isl_mat_free(steps);
809 return path;
810 error:
811 isl_space_free(dim);
812 isl_mat_free(steps);
813 isl_map_free(path);
814 return NULL;
817 static int isl_set_overlaps(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
819 isl_set *i;
820 int no_overlap;
822 if (!isl_space_tuple_is_equal(set1->dim, isl_dim_set,
823 set2->dim, isl_dim_set))
824 return 0;
826 i = isl_set_intersect(isl_set_copy(set1), isl_set_copy(set2));
827 no_overlap = isl_set_is_empty(i);
828 isl_set_free(i);
830 return no_overlap < 0 ? -1 : !no_overlap;
833 /* Given a union of basic maps R = \cup_i R_i \subseteq D \times D
834 * and a dimension specification (Z^{n+1} -> Z^{n+1}),
835 * construct a map that is an overapproximation of the map
836 * that takes an element from the dom R \times Z to an
837 * element from ran R \times Z, such that the first n coordinates of the
838 * difference between them is a sum of differences between images
839 * and pre-images in one of the R_i and such that the last coordinate
840 * is equal to the number of steps taken.
841 * That is, let
843 * \Delta_i = { y - x | (x, y) in R_i }
845 * then the constructed map is an overapproximation of
847 * { (x) -> (x + d) | \exists k_i >= 0, \delta_i \in \Delta_i :
848 * d = (\sum_i k_i \delta_i, \sum_i k_i) and
849 * x in dom R and x + d in ran R and
850 * \sum_i k_i >= 1 }
852 static __isl_give isl_map *construct_component(__isl_take isl_space *dim,
853 __isl_keep isl_map *map, int *exact, int project)
855 struct isl_set *domain = NULL;
856 struct isl_set *range = NULL;
857 struct isl_map *app = NULL;
858 struct isl_map *path = NULL;
860 domain = isl_map_domain(isl_map_copy(map));
861 domain = isl_set_coalesce(domain);
862 range = isl_map_range(isl_map_copy(map));
863 range = isl_set_coalesce(range);
864 if (!isl_set_overlaps(domain, range)) {
865 isl_set_free(domain);
866 isl_set_free(range);
867 isl_space_free(dim);
869 map = isl_map_copy(map);
870 map = isl_map_add_dims(map, isl_dim_in, 1);
871 map = isl_map_add_dims(map, isl_dim_out, 1);
872 map = set_path_length(map, 1, 1);
873 return map;
875 app = isl_map_from_domain_and_range(domain, range);
876 app = isl_map_add_dims(app, isl_dim_in, 1);
877 app = isl_map_add_dims(app, isl_dim_out, 1);
879 path = construct_extended_path(isl_space_copy(dim), map,
880 exact && *exact ? &project : NULL);
881 app = isl_map_intersect(app, path);
883 if (exact && *exact &&
884 (*exact = check_exactness(isl_map_copy(map), isl_map_copy(app),
885 project)) < 0)
886 goto error;
888 isl_space_free(dim);
889 app = set_path_length(app, 0, 1);
890 return app;
891 error:
892 isl_space_free(dim);
893 isl_map_free(app);
894 return NULL;
897 /* Call construct_component and, if "project" is set, project out
898 * the final coordinates.
900 static __isl_give isl_map *construct_projected_component(
901 __isl_take isl_space *dim,
902 __isl_keep isl_map *map, int *exact, int project)
904 isl_map *app;
905 unsigned d;
907 if (!dim)
908 return NULL;
909 d = isl_space_dim(dim, isl_dim_in);
911 app = construct_component(dim, map, exact, project);
912 if (project) {
913 app = isl_map_project_out(app, isl_dim_in, d - 1, 1);
914 app = isl_map_project_out(app, isl_dim_out, d - 1, 1);
916 return app;
919 /* Compute an extended version, i.e., with path lengths, of
920 * an overapproximation of the transitive closure of "bmap"
921 * with path lengths greater than or equal to zero and with
922 * domain and range equal to "dom".
924 static __isl_give isl_map *q_closure(__isl_take isl_space *dim,
925 __isl_take isl_set *dom, __isl_keep isl_basic_map *bmap, int *exact)
927 int project = 1;
928 isl_map *path;
929 isl_map *map;
930 isl_map *app;
932 dom = isl_set_add_dims(dom, isl_dim_set, 1);
933 app = isl_map_from_domain_and_range(dom, isl_set_copy(dom));
934 map = isl_map_from_basic_map(isl_basic_map_copy(bmap));
935 path = construct_extended_path(dim, map, &project);
936 app = isl_map_intersect(app, path);
938 if ((*exact = check_exactness(map, isl_map_copy(app), project)) < 0)
939 goto error;
941 return app;
942 error:
943 isl_map_free(app);
944 return NULL;
947 /* Check whether qc has any elements of length at least one
948 * with domain and/or range outside of dom and ran.
950 static int has_spurious_elements(__isl_keep isl_map *qc,
951 __isl_keep isl_set *dom, __isl_keep isl_set *ran)
953 isl_set *s;
954 int subset;
955 unsigned d;
957 if (!qc || !dom || !ran)
958 return -1;
960 d = isl_map_dim(qc, isl_dim_in);
962 qc = isl_map_copy(qc);
963 qc = set_path_length(qc, 0, 1);
964 qc = isl_map_project_out(qc, isl_dim_in, d - 1, 1);
965 qc = isl_map_project_out(qc, isl_dim_out, d - 1, 1);
967 s = isl_map_domain(isl_map_copy(qc));
968 subset = isl_set_is_subset(s, dom);
969 isl_set_free(s);
970 if (subset < 0)
971 goto error;
972 if (!subset) {
973 isl_map_free(qc);
974 return 1;
977 s = isl_map_range(qc);
978 subset = isl_set_is_subset(s, ran);
979 isl_set_free(s);
981 return subset < 0 ? -1 : !subset;
982 error:
983 isl_map_free(qc);
984 return -1;
987 #define LEFT 2
988 #define RIGHT 1
990 /* For each basic map in "map", except i, check whether it combines
991 * with the transitive closure that is reflexive on C combines
992 * to the left and to the right.
994 * In particular, if
996 * dom map_j \subseteq C
998 * then right[j] is set to 1. Otherwise, if
1000 * ran map_i \cap dom map_j = \emptyset
1002 * then right[j] is set to 0. Otherwise, composing to the right
1003 * is impossible.
1005 * Similar, for composing to the left, we have if
1007 * ran map_j \subseteq C
1009 * then left[j] is set to 1. Otherwise, if
1011 * dom map_i \cap ran map_j = \emptyset
1013 * then left[j] is set to 0. Otherwise, composing to the left
1014 * is impossible.
1016 * The return value is or'd with LEFT if composing to the left
1017 * is possible and with RIGHT if composing to the right is possible.
1019 static int composability(__isl_keep isl_set *C, int i,
1020 isl_set **dom, isl_set **ran, int *left, int *right,
1021 __isl_keep isl_map *map)
1023 int j;
1024 int ok;
1026 ok = LEFT | RIGHT;
1027 for (j = 0; j < map->n && ok; ++j) {
1028 int overlaps, subset;
1029 if (j == i)
1030 continue;
1032 if (ok & RIGHT) {
1033 if (!dom[j])
1034 dom[j] = isl_set_from_basic_set(
1035 isl_basic_map_domain(
1036 isl_basic_map_copy(map->p[j])));
1037 if (!dom[j])
1038 return -1;
1039 overlaps = isl_set_overlaps(ran[i], dom[j]);
1040 if (overlaps < 0)
1041 return -1;
1042 if (!overlaps)
1043 right[j] = 0;
1044 else {
1045 subset = isl_set_is_subset(dom[j], C);
1046 if (subset < 0)
1047 return -1;
1048 if (subset)
1049 right[j] = 1;
1050 else
1051 ok &= ~RIGHT;
1055 if (ok & LEFT) {
1056 if (!ran[j])
1057 ran[j] = isl_set_from_basic_set(
1058 isl_basic_map_range(
1059 isl_basic_map_copy(map->p[j])));
1060 if (!ran[j])
1061 return -1;
1062 overlaps = isl_set_overlaps(dom[i], ran[j]);
1063 if (overlaps < 0)
1064 return -1;
1065 if (!overlaps)
1066 left[j] = 0;
1067 else {
1068 subset = isl_set_is_subset(ran[j], C);
1069 if (subset < 0)
1070 return -1;
1071 if (subset)
1072 left[j] = 1;
1073 else
1074 ok &= ~LEFT;
1079 return ok;
1082 static __isl_give isl_map *anonymize(__isl_take isl_map *map)
1084 map = isl_map_reset(map, isl_dim_in);
1085 map = isl_map_reset(map, isl_dim_out);
1086 return map;
1089 /* Return a map that is a union of the basic maps in "map", except i,
1090 * composed to left and right with qc based on the entries of "left"
1091 * and "right".
1093 static __isl_give isl_map *compose(__isl_keep isl_map *map, int i,
1094 __isl_take isl_map *qc, int *left, int *right)
1096 int j;
1097 isl_map *comp;
1099 comp = isl_map_empty(isl_map_get_space(map));
1100 for (j = 0; j < map->n; ++j) {
1101 isl_map *map_j;
1103 if (j == i)
1104 continue;
1106 map_j = isl_map_from_basic_map(isl_basic_map_copy(map->p[j]));
1107 map_j = anonymize(map_j);
1108 if (left && left[j])
1109 map_j = isl_map_apply_range(map_j, isl_map_copy(qc));
1110 if (right && right[j])
1111 map_j = isl_map_apply_range(isl_map_copy(qc), map_j);
1112 comp = isl_map_union(comp, map_j);
1115 comp = isl_map_compute_divs(comp);
1116 comp = isl_map_coalesce(comp);
1118 isl_map_free(qc);
1120 return comp;
1123 /* Compute the transitive closure of "map" incrementally by
1124 * computing
1126 * map_i^+ \cup qc^+
1128 * or
1130 * map_i^+ \cup ((id \cup map_i^) \circ qc^+)
1132 * or
1134 * map_i^+ \cup (qc^+ \circ (id \cup map_i^))
1136 * depending on whether left or right are NULL.
1138 static __isl_give isl_map *compute_incremental(
1139 __isl_take isl_space *dim, __isl_keep isl_map *map,
1140 int i, __isl_take isl_map *qc, int *left, int *right, int *exact)
1142 isl_map *map_i;
1143 isl_map *tc;
1144 isl_map *rtc = NULL;
1146 if (!map)
1147 goto error;
1148 isl_assert(map->ctx, left || right, goto error);
1150 map_i = isl_map_from_basic_map(isl_basic_map_copy(map->p[i]));
1151 tc = construct_projected_component(isl_space_copy(dim), map_i,
1152 exact, 1);
1153 isl_map_free(map_i);
1155 if (*exact)
1156 qc = isl_map_transitive_closure(qc, exact);
1158 if (!*exact) {
1159 isl_space_free(dim);
1160 isl_map_free(tc);
1161 isl_map_free(qc);
1162 return isl_map_universe(isl_map_get_space(map));
1165 if (!left || !right)
1166 rtc = isl_map_union(isl_map_copy(tc),
1167 isl_map_identity(isl_map_get_space(tc)));
1168 if (!right)
1169 qc = isl_map_apply_range(rtc, qc);
1170 if (!left)
1171 qc = isl_map_apply_range(qc, rtc);
1172 qc = isl_map_union(tc, qc);
1174 isl_space_free(dim);
1176 return qc;
1177 error:
1178 isl_space_free(dim);
1179 isl_map_free(qc);
1180 return NULL;
1183 /* Given a map "map", try to find a basic map such that
1184 * map^+ can be computed as
1186 * map^+ = map_i^+ \cup
1187 * \bigcup_j ((map_i^+ \cup Id_C)^+ \circ map_j \circ (map_i^+ \cup Id_C))^+
1189 * with C the simple hull of the domain and range of the input map.
1190 * map_i^ \cup Id_C is computed by allowing the path lengths to be zero
1191 * and by intersecting domain and range with C.
1192 * Of course, we need to check that this is actually equal to map_i^ \cup Id_C.
1193 * Also, we only use the incremental computation if all the transitive
1194 * closures are exact and if the number of basic maps in the union,
1195 * after computing the integer divisions, is smaller than the number
1196 * of basic maps in the input map.
1198 static int incemental_on_entire_domain(__isl_keep isl_space *dim,
1199 __isl_keep isl_map *map,
1200 isl_set **dom, isl_set **ran, int *left, int *right,
1201 __isl_give isl_map **res)
1203 int i;
1204 isl_set *C;
1205 unsigned d;
1207 *res = NULL;
1209 C = isl_set_union(isl_map_domain(isl_map_copy(map)),
1210 isl_map_range(isl_map_copy(map)));
1211 C = isl_set_from_basic_set(isl_set_simple_hull(C));
1212 if (!C)
1213 return -1;
1214 if (C->n != 1) {
1215 isl_set_free(C);
1216 return 0;
1219 d = isl_map_dim(map, isl_dim_in);
1221 for (i = 0; i < map->n; ++i) {
1222 isl_map *qc;
1223 int exact_i, spurious;
1224 int j;
1225 dom[i] = isl_set_from_basic_set(isl_basic_map_domain(
1226 isl_basic_map_copy(map->p[i])));
1227 ran[i] = isl_set_from_basic_set(isl_basic_map_range(
1228 isl_basic_map_copy(map->p[i])));
1229 qc = q_closure(isl_space_copy(dim), isl_set_copy(C),
1230 map->p[i], &exact_i);
1231 if (!qc)
1232 goto error;
1233 if (!exact_i) {
1234 isl_map_free(qc);
1235 continue;
1237 spurious = has_spurious_elements(qc, dom[i], ran[i]);
1238 if (spurious) {
1239 isl_map_free(qc);
1240 if (spurious < 0)
1241 goto error;
1242 continue;
1244 qc = isl_map_project_out(qc, isl_dim_in, d, 1);
1245 qc = isl_map_project_out(qc, isl_dim_out, d, 1);
1246 qc = isl_map_compute_divs(qc);
1247 for (j = 0; j < map->n; ++j)
1248 left[j] = right[j] = 1;
1249 qc = compose(map, i, qc, left, right);
1250 if (!qc)
1251 goto error;
1252 if (qc->n >= map->n) {
1253 isl_map_free(qc);
1254 continue;
1256 *res = compute_incremental(isl_space_copy(dim), map, i, qc,
1257 left, right, &exact_i);
1258 if (!*res)
1259 goto error;
1260 if (exact_i)
1261 break;
1262 isl_map_free(*res);
1263 *res = NULL;
1266 isl_set_free(C);
1268 return *res != NULL;
1269 error:
1270 isl_set_free(C);
1271 return -1;
1274 /* Try and compute the transitive closure of "map" as
1276 * map^+ = map_i^+ \cup
1277 * \bigcup_j ((map_i^+ \cup Id_C)^+ \circ map_j \circ (map_i^+ \cup Id_C))^+
1279 * with C either the simple hull of the domain and range of the entire
1280 * map or the simple hull of domain and range of map_i.
1282 static __isl_give isl_map *incremental_closure(__isl_take isl_space *dim,
1283 __isl_keep isl_map *map, int *exact, int project)
1285 int i;
1286 isl_set **dom = NULL;
1287 isl_set **ran = NULL;
1288 int *left = NULL;
1289 int *right = NULL;
1290 isl_set *C;
1291 unsigned d;
1292 isl_map *res = NULL;
1294 if (!project)
1295 return construct_projected_component(dim, map, exact, project);
1297 if (!map)
1298 goto error;
1299 if (map->n <= 1)
1300 return construct_projected_component(dim, map, exact, project);
1302 d = isl_map_dim(map, isl_dim_in);
1304 dom = isl_calloc_array(map->ctx, isl_set *, map->n);
1305 ran = isl_calloc_array(map->ctx, isl_set *, map->n);
1306 left = isl_calloc_array(map->ctx, int, map->n);
1307 right = isl_calloc_array(map->ctx, int, map->n);
1308 if (!ran || !dom || !left || !right)
1309 goto error;
1311 if (incemental_on_entire_domain(dim, map, dom, ran, left, right, &res) < 0)
1312 goto error;
1314 for (i = 0; !res && i < map->n; ++i) {
1315 isl_map *qc;
1316 int exact_i, spurious, comp;
1317 if (!dom[i])
1318 dom[i] = isl_set_from_basic_set(
1319 isl_basic_map_domain(
1320 isl_basic_map_copy(map->p[i])));
1321 if (!dom[i])
1322 goto error;
1323 if (!ran[i])
1324 ran[i] = isl_set_from_basic_set(
1325 isl_basic_map_range(
1326 isl_basic_map_copy(map->p[i])));
1327 if (!ran[i])
1328 goto error;
1329 C = isl_set_union(isl_set_copy(dom[i]),
1330 isl_set_copy(ran[i]));
1331 C = isl_set_from_basic_set(isl_set_simple_hull(C));
1332 if (!C)
1333 goto error;
1334 if (C->n != 1) {
1335 isl_set_free(C);
1336 continue;
1338 comp = composability(C, i, dom, ran, left, right, map);
1339 if (!comp || comp < 0) {
1340 isl_set_free(C);
1341 if (comp < 0)
1342 goto error;
1343 continue;
1345 qc = q_closure(isl_space_copy(dim), C, map->p[i], &exact_i);
1346 if (!qc)
1347 goto error;
1348 if (!exact_i) {
1349 isl_map_free(qc);
1350 continue;
1352 spurious = has_spurious_elements(qc, dom[i], ran[i]);
1353 if (spurious) {
1354 isl_map_free(qc);
1355 if (spurious < 0)
1356 goto error;
1357 continue;
1359 qc = isl_map_project_out(qc, isl_dim_in, d, 1);
1360 qc = isl_map_project_out(qc, isl_dim_out, d, 1);
1361 qc = isl_map_compute_divs(qc);
1362 qc = compose(map, i, qc, (comp & LEFT) ? left : NULL,
1363 (comp & RIGHT) ? right : NULL);
1364 if (!qc)
1365 goto error;
1366 if (qc->n >= map->n) {
1367 isl_map_free(qc);
1368 continue;
1370 res = compute_incremental(isl_space_copy(dim), map, i, qc,
1371 (comp & LEFT) ? left : NULL,
1372 (comp & RIGHT) ? right : NULL, &exact_i);
1373 if (!res)
1374 goto error;
1375 if (exact_i)
1376 break;
1377 isl_map_free(res);
1378 res = NULL;
1381 for (i = 0; i < map->n; ++i) {
1382 isl_set_free(dom[i]);
1383 isl_set_free(ran[i]);
1385 free(dom);
1386 free(ran);
1387 free(left);
1388 free(right);
1390 if (res) {
1391 isl_space_free(dim);
1392 return res;
1395 return construct_projected_component(dim, map, exact, project);
1396 error:
1397 if (dom)
1398 for (i = 0; i < map->n; ++i)
1399 isl_set_free(dom[i]);
1400 free(dom);
1401 if (ran)
1402 for (i = 0; i < map->n; ++i)
1403 isl_set_free(ran[i]);
1404 free(ran);
1405 free(left);
1406 free(right);
1407 isl_space_free(dim);
1408 return NULL;
1411 /* Given an array of sets "set", add "dom" at position "pos"
1412 * and search for elements at earlier positions that overlap with "dom".
1413 * If any can be found, then merge all of them, together with "dom", into
1414 * a single set and assign the union to the first in the array,
1415 * which becomes the new group leader for all groups involved in the merge.
1416 * During the search, we only consider group leaders, i.e., those with
1417 * group[i] = i, as the other sets have already been combined
1418 * with one of the group leaders.
1420 static int merge(isl_set **set, int *group, __isl_take isl_set *dom, int pos)
1422 int i;
1424 group[pos] = pos;
1425 set[pos] = isl_set_copy(dom);
1427 for (i = pos - 1; i >= 0; --i) {
1428 int o;
1430 if (group[i] != i)
1431 continue;
1433 o = isl_set_overlaps(set[i], dom);
1434 if (o < 0)
1435 goto error;
1436 if (!o)
1437 continue;
1439 set[i] = isl_set_union(set[i], set[group[pos]]);
1440 set[group[pos]] = NULL;
1441 if (!set[i])
1442 goto error;
1443 group[group[pos]] = i;
1444 group[pos] = i;
1447 isl_set_free(dom);
1448 return 0;
1449 error:
1450 isl_set_free(dom);
1451 return -1;
1454 /* Replace each entry in the n by n grid of maps by the cross product
1455 * with the relation { [i] -> [i + 1] }.
1457 static int add_length(__isl_keep isl_map *map, isl_map ***grid, int n)
1459 int i, j, k;
1460 isl_space *dim;
1461 isl_basic_map *bstep;
1462 isl_map *step;
1463 unsigned nparam;
1465 if (!map)
1466 return -1;
1468 dim = isl_map_get_space(map);
1469 nparam = isl_space_dim(dim, isl_dim_param);
1470 dim = isl_space_drop_dims(dim, isl_dim_in, 0, isl_space_dim(dim, isl_dim_in));
1471 dim = isl_space_drop_dims(dim, isl_dim_out, 0, isl_space_dim(dim, isl_dim_out));
1472 dim = isl_space_add_dims(dim, isl_dim_in, 1);
1473 dim = isl_space_add_dims(dim, isl_dim_out, 1);
1474 bstep = isl_basic_map_alloc_space(dim, 0, 1, 0);
1475 k = isl_basic_map_alloc_equality(bstep);
1476 if (k < 0) {
1477 isl_basic_map_free(bstep);
1478 return -1;
1480 isl_seq_clr(bstep->eq[k], 1 + isl_basic_map_total_dim(bstep));
1481 isl_int_set_si(bstep->eq[k][0], 1);
1482 isl_int_set_si(bstep->eq[k][1 + nparam], 1);
1483 isl_int_set_si(bstep->eq[k][1 + nparam + 1], -1);
1484 bstep = isl_basic_map_finalize(bstep);
1485 step = isl_map_from_basic_map(bstep);
1487 for (i = 0; i < n; ++i)
1488 for (j = 0; j < n; ++j)
1489 grid[i][j] = isl_map_product(grid[i][j],
1490 isl_map_copy(step));
1492 isl_map_free(step);
1494 return 0;
1497 /* The core of the Floyd-Warshall algorithm.
1498 * Updates the given n x x matrix of relations in place.
1500 * The algorithm iterates over all vertices. In each step, the whole
1501 * matrix is updated to include all paths that go to the current vertex,
1502 * possibly stay there a while (including passing through earlier vertices)
1503 * and then come back. At the start of each iteration, the diagonal
1504 * element corresponding to the current vertex is replaced by its
1505 * transitive closure to account for all indirect paths that stay
1506 * in the current vertex.
1508 static void floyd_warshall_iterate(isl_map ***grid, int n, int *exact)
1510 int r, p, q;
1512 for (r = 0; r < n; ++r) {
1513 int r_exact;
1514 grid[r][r] = isl_map_transitive_closure(grid[r][r],
1515 (exact && *exact) ? &r_exact : NULL);
1516 if (exact && *exact && !r_exact)
1517 *exact = 0;
1519 for (p = 0; p < n; ++p)
1520 for (q = 0; q < n; ++q) {
1521 isl_map *loop;
1522 if (p == r && q == r)
1523 continue;
1524 loop = isl_map_apply_range(
1525 isl_map_copy(grid[p][r]),
1526 isl_map_copy(grid[r][q]));
1527 grid[p][q] = isl_map_union(grid[p][q], loop);
1528 loop = isl_map_apply_range(
1529 isl_map_copy(grid[p][r]),
1530 isl_map_apply_range(
1531 isl_map_copy(grid[r][r]),
1532 isl_map_copy(grid[r][q])));
1533 grid[p][q] = isl_map_union(grid[p][q], loop);
1534 grid[p][q] = isl_map_coalesce(grid[p][q]);
1539 /* Given a partition of the domains and ranges of the basic maps in "map",
1540 * apply the Floyd-Warshall algorithm with the elements in the partition
1541 * as vertices.
1543 * In particular, there are "n" elements in the partition and "group" is
1544 * an array of length 2 * map->n with entries in [0,n-1].
1546 * We first construct a matrix of relations based on the partition information,
1547 * apply Floyd-Warshall on this matrix of relations and then take the
1548 * union of all entries in the matrix as the final result.
1550 * If we are actually computing the power instead of the transitive closure,
1551 * i.e., when "project" is not set, then the result should have the
1552 * path lengths encoded as the difference between an extra pair of
1553 * coordinates. We therefore apply the nested transitive closures
1554 * to relations that include these lengths. In particular, we replace
1555 * the input relation by the cross product with the unit length relation
1556 * { [i] -> [i + 1] }.
1558 static __isl_give isl_map *floyd_warshall_with_groups(__isl_take isl_space *dim,
1559 __isl_keep isl_map *map, int *exact, int project, int *group, int n)
1561 int i, j, k;
1562 isl_map ***grid = NULL;
1563 isl_map *app;
1565 if (!map)
1566 goto error;
1568 if (n == 1) {
1569 free(group);
1570 return incremental_closure(dim, map, exact, project);
1573 grid = isl_calloc_array(map->ctx, isl_map **, n);
1574 if (!grid)
1575 goto error;
1576 for (i = 0; i < n; ++i) {
1577 grid[i] = isl_calloc_array(map->ctx, isl_map *, n);
1578 if (!grid[i])
1579 goto error;
1580 for (j = 0; j < n; ++j)
1581 grid[i][j] = isl_map_empty(isl_map_get_space(map));
1584 for (k = 0; k < map->n; ++k) {
1585 i = group[2 * k];
1586 j = group[2 * k + 1];
1587 grid[i][j] = isl_map_union(grid[i][j],
1588 isl_map_from_basic_map(
1589 isl_basic_map_copy(map->p[k])));
1592 if (!project && add_length(map, grid, n) < 0)
1593 goto error;
1595 floyd_warshall_iterate(grid, n, exact);
1597 app = isl_map_empty(isl_map_get_space(map));
1599 for (i = 0; i < n; ++i) {
1600 for (j = 0; j < n; ++j)
1601 app = isl_map_union(app, grid[i][j]);
1602 free(grid[i]);
1604 free(grid);
1606 free(group);
1607 isl_space_free(dim);
1609 return app;
1610 error:
1611 if (grid)
1612 for (i = 0; i < n; ++i) {
1613 if (!grid[i])
1614 continue;
1615 for (j = 0; j < n; ++j)
1616 isl_map_free(grid[i][j]);
1617 free(grid[i]);
1619 free(grid);
1620 free(group);
1621 isl_space_free(dim);
1622 return NULL;
1625 /* Partition the domains and ranges of the n basic relations in list
1626 * into disjoint cells.
1628 * To find the partition, we simply consider all of the domains
1629 * and ranges in turn and combine those that overlap.
1630 * "set" contains the partition elements and "group" indicates
1631 * to which partition element a given domain or range belongs.
1632 * The domain of basic map i corresponds to element 2 * i in these arrays,
1633 * while the domain corresponds to element 2 * i + 1.
1634 * During the construction group[k] is either equal to k,
1635 * in which case set[k] contains the union of all the domains and
1636 * ranges in the corresponding group, or is equal to some l < k,
1637 * with l another domain or range in the same group.
1639 static int *setup_groups(isl_ctx *ctx, __isl_keep isl_basic_map **list, int n,
1640 isl_set ***set, int *n_group)
1642 int i;
1643 int *group = NULL;
1644 int g;
1646 *set = isl_calloc_array(ctx, isl_set *, 2 * n);
1647 group = isl_alloc_array(ctx, int, 2 * n);
1649 if (!*set || !group)
1650 goto error;
1652 for (i = 0; i < n; ++i) {
1653 isl_set *dom;
1654 dom = isl_set_from_basic_set(isl_basic_map_domain(
1655 isl_basic_map_copy(list[i])));
1656 if (merge(*set, group, dom, 2 * i) < 0)
1657 goto error;
1658 dom = isl_set_from_basic_set(isl_basic_map_range(
1659 isl_basic_map_copy(list[i])));
1660 if (merge(*set, group, dom, 2 * i + 1) < 0)
1661 goto error;
1664 g = 0;
1665 for (i = 0; i < 2 * n; ++i)
1666 if (group[i] == i) {
1667 if (g != i) {
1668 (*set)[g] = (*set)[i];
1669 (*set)[i] = NULL;
1671 group[i] = g++;
1672 } else
1673 group[i] = group[group[i]];
1675 *n_group = g;
1677 return group;
1678 error:
1679 if (*set) {
1680 for (i = 0; i < 2 * n; ++i)
1681 isl_set_free((*set)[i]);
1682 free(*set);
1683 *set = NULL;
1685 free(group);
1686 return NULL;
1689 /* Check if the domains and ranges of the basic maps in "map" can
1690 * be partitioned, and if so, apply Floyd-Warshall on the elements
1691 * of the partition. Note that we also apply this algorithm
1692 * if we want to compute the power, i.e., when "project" is not set.
1693 * However, the results are unlikely to be exact since the recursive
1694 * calls inside the Floyd-Warshall algorithm typically result in
1695 * non-linear path lengths quite quickly.
1697 static __isl_give isl_map *floyd_warshall(__isl_take isl_space *dim,
1698 __isl_keep isl_map *map, int *exact, int project)
1700 int i;
1701 isl_set **set = NULL;
1702 int *group = NULL;
1703 int n;
1705 if (!map)
1706 goto error;
1707 if (map->n <= 1)
1708 return incremental_closure(dim, map, exact, project);
1710 group = setup_groups(map->ctx, map->p, map->n, &set, &n);
1711 if (!group)
1712 goto error;
1714 for (i = 0; i < 2 * map->n; ++i)
1715 isl_set_free(set[i]);
1717 free(set);
1719 return floyd_warshall_with_groups(dim, map, exact, project, group, n);
1720 error:
1721 isl_space_free(dim);
1722 return NULL;
1725 /* Structure for representing the nodes of the graph of which
1726 * strongly connected components are being computed.
1728 * list contains the actual nodes
1729 * check_closed is set if we may have used the fact that
1730 * a pair of basic maps can be interchanged
1732 struct isl_tc_follows_data {
1733 isl_basic_map **list;
1734 int check_closed;
1737 /* Check whether in the computation of the transitive closure
1738 * "list[i]" (R_1) should follow (or be part of the same component as)
1739 * "list[j]" (R_2).
1741 * That is check whether
1743 * R_1 \circ R_2
1745 * is a subset of
1747 * R_2 \circ R_1
1749 * If so, then there is no reason for R_1 to immediately follow R_2
1750 * in any path.
1752 * *check_closed is set if the subset relation holds while
1753 * R_1 \circ R_2 is not empty.
1755 static int basic_map_follows(int i, int j, void *user)
1757 struct isl_tc_follows_data *data = user;
1758 struct isl_map *map12 = NULL;
1759 struct isl_map *map21 = NULL;
1760 int subset;
1762 if (!isl_space_tuple_is_equal(data->list[i]->dim, isl_dim_in,
1763 data->list[j]->dim, isl_dim_out))
1764 return 0;
1766 map21 = isl_map_from_basic_map(
1767 isl_basic_map_apply_range(
1768 isl_basic_map_copy(data->list[j]),
1769 isl_basic_map_copy(data->list[i])));
1770 subset = isl_map_is_empty(map21);
1771 if (subset < 0)
1772 goto error;
1773 if (subset) {
1774 isl_map_free(map21);
1775 return 0;
1778 if (!isl_space_tuple_is_equal(data->list[i]->dim, isl_dim_in,
1779 data->list[i]->dim, isl_dim_out) ||
1780 !isl_space_tuple_is_equal(data->list[j]->dim, isl_dim_in,
1781 data->list[j]->dim, isl_dim_out)) {
1782 isl_map_free(map21);
1783 return 1;
1786 map12 = isl_map_from_basic_map(
1787 isl_basic_map_apply_range(
1788 isl_basic_map_copy(data->list[i]),
1789 isl_basic_map_copy(data->list[j])));
1791 subset = isl_map_is_subset(map21, map12);
1793 isl_map_free(map12);
1794 isl_map_free(map21);
1796 if (subset)
1797 data->check_closed = 1;
1799 return subset < 0 ? -1 : !subset;
1800 error:
1801 isl_map_free(map21);
1802 return -1;
1805 /* Given a union of basic maps R = \cup_i R_i \subseteq D \times D
1806 * and a dimension specification (Z^{n+1} -> Z^{n+1}),
1807 * construct a map that is an overapproximation of the map
1808 * that takes an element from the dom R \times Z to an
1809 * element from ran R \times Z, such that the first n coordinates of the
1810 * difference between them is a sum of differences between images
1811 * and pre-images in one of the R_i and such that the last coordinate
1812 * is equal to the number of steps taken.
1813 * If "project" is set, then these final coordinates are not included,
1814 * i.e., a relation of type Z^n -> Z^n is returned.
1815 * That is, let
1817 * \Delta_i = { y - x | (x, y) in R_i }
1819 * then the constructed map is an overapproximation of
1821 * { (x) -> (x + d) | \exists k_i >= 0, \delta_i \in \Delta_i :
1822 * d = (\sum_i k_i \delta_i, \sum_i k_i) and
1823 * x in dom R and x + d in ran R }
1825 * or
1827 * { (x) -> (x + d) | \exists k_i >= 0, \delta_i \in \Delta_i :
1828 * d = (\sum_i k_i \delta_i) and
1829 * x in dom R and x + d in ran R }
1831 * if "project" is set.
1833 * We first split the map into strongly connected components, perform
1834 * the above on each component and then join the results in the correct
1835 * order, at each join also taking in the union of both arguments
1836 * to allow for paths that do not go through one of the two arguments.
1838 static __isl_give isl_map *construct_power_components(__isl_take isl_space *dim,
1839 __isl_keep isl_map *map, int *exact, int project)
1841 int i, n, c;
1842 struct isl_map *path = NULL;
1843 struct isl_tc_follows_data data;
1844 struct isl_tarjan_graph *g = NULL;
1845 int *orig_exact;
1846 int local_exact;
1848 if (!map)
1849 goto error;
1850 if (map->n <= 1)
1851 return floyd_warshall(dim, map, exact, project);
1853 data.list = map->p;
1854 data.check_closed = 0;
1855 g = isl_tarjan_graph_init(map->ctx, map->n, &basic_map_follows, &data);
1856 if (!g)
1857 goto error;
1859 orig_exact = exact;
1860 if (data.check_closed && !exact)
1861 exact = &local_exact;
1863 c = 0;
1864 i = 0;
1865 n = map->n;
1866 if (project)
1867 path = isl_map_empty(isl_map_get_space(map));
1868 else
1869 path = isl_map_empty(isl_space_copy(dim));
1870 path = anonymize(path);
1871 while (n) {
1872 struct isl_map *comp;
1873 isl_map *path_comp, *path_comb;
1874 comp = isl_map_alloc_space(isl_map_get_space(map), n, 0);
1875 while (g->order[i] != -1) {
1876 comp = isl_map_add_basic_map(comp,
1877 isl_basic_map_copy(map->p[g->order[i]]));
1878 --n;
1879 ++i;
1881 path_comp = floyd_warshall(isl_space_copy(dim),
1882 comp, exact, project);
1883 path_comp = anonymize(path_comp);
1884 path_comb = isl_map_apply_range(isl_map_copy(path),
1885 isl_map_copy(path_comp));
1886 path = isl_map_union(path, path_comp);
1887 path = isl_map_union(path, path_comb);
1888 isl_map_free(comp);
1889 ++i;
1890 ++c;
1893 if (c > 1 && data.check_closed && !*exact) {
1894 int closed;
1896 closed = isl_map_is_transitively_closed(path);
1897 if (closed < 0)
1898 goto error;
1899 if (!closed) {
1900 isl_tarjan_graph_free(g);
1901 isl_map_free(path);
1902 return floyd_warshall(dim, map, orig_exact, project);
1906 isl_tarjan_graph_free(g);
1907 isl_space_free(dim);
1909 return path;
1910 error:
1911 isl_tarjan_graph_free(g);
1912 isl_space_free(dim);
1913 isl_map_free(path);
1914 return NULL;
1917 /* Given a union of basic maps R = \cup_i R_i \subseteq D \times D,
1918 * construct a map that is an overapproximation of the map
1919 * that takes an element from the space D to another
1920 * element from the same space, such that the difference between
1921 * them is a strictly positive sum of differences between images
1922 * and pre-images in one of the R_i.
1923 * The number of differences in the sum is equated to parameter "param".
1924 * That is, let
1926 * \Delta_i = { y - x | (x, y) in R_i }
1928 * then the constructed map is an overapproximation of
1930 * { (x) -> (x + d) | \exists k_i >= 0, \delta_i \in \Delta_i :
1931 * d = \sum_i k_i \delta_i and k = \sum_i k_i > 0 }
1932 * or
1934 * { (x) -> (x + d) | \exists k_i >= 0, \delta_i \in \Delta_i :
1935 * d = \sum_i k_i \delta_i and \sum_i k_i > 0 }
1937 * if "project" is set.
1939 * If "project" is not set, then
1940 * we construct an extended mapping with an extra coordinate
1941 * that indicates the number of steps taken. In particular,
1942 * the difference in the last coordinate is equal to the number
1943 * of steps taken to move from a domain element to the corresponding
1944 * image element(s).
1946 static __isl_give isl_map *construct_power(__isl_keep isl_map *map,
1947 int *exact, int project)
1949 struct isl_map *app = NULL;
1950 isl_space *dim = NULL;
1951 unsigned d;
1953 if (!map)
1954 return NULL;
1956 dim = isl_map_get_space(map);
1958 d = isl_space_dim(dim, isl_dim_in);
1959 dim = isl_space_add_dims(dim, isl_dim_in, 1);
1960 dim = isl_space_add_dims(dim, isl_dim_out, 1);
1962 app = construct_power_components(isl_space_copy(dim), map,
1963 exact, project);
1965 isl_space_free(dim);
1967 return app;
1970 /* Compute the positive powers of "map", or an overapproximation.
1971 * If the result is exact, then *exact is set to 1.
1973 * If project is set, then we are actually interested in the transitive
1974 * closure, so we can use a more relaxed exactness check.
1975 * The lengths of the paths are also projected out instead of being
1976 * encoded as the difference between an extra pair of final coordinates.
1978 static __isl_give isl_map *map_power(__isl_take isl_map *map,
1979 int *exact, int project)
1981 struct isl_map *app = NULL;
1983 if (exact)
1984 *exact = 1;
1986 if (!map)
1987 return NULL;
1989 isl_assert(map->ctx,
1990 isl_map_dim(map, isl_dim_in) == isl_map_dim(map, isl_dim_out),
1991 goto error);
1993 app = construct_power(map, exact, project);
1995 isl_map_free(map);
1996 return app;
1997 error:
1998 isl_map_free(map);
1999 isl_map_free(app);
2000 return NULL;
2003 /* Compute the positive powers of "map", or an overapproximation.
2004 * The result maps the exponent to a nested copy of the corresponding power.
2005 * If the result is exact, then *exact is set to 1.
2006 * map_power constructs an extended relation with the path lengths
2007 * encoded as the difference between the final coordinates.
2008 * In the final step, this difference is equated to an extra parameter
2009 * and made positive. The extra coordinates are subsequently projected out
2010 * and the parameter is turned into the domain of the result.
2012 __isl_give isl_map *isl_map_power(__isl_take isl_map *map, int *exact)
2014 isl_space *target_dim;
2015 isl_space *dim;
2016 isl_map *diff;
2017 unsigned d;
2018 unsigned param;
2020 if (!map)
2021 return NULL;
2023 d = isl_map_dim(map, isl_dim_in);
2024 param = isl_map_dim(map, isl_dim_param);
2026 map = isl_map_compute_divs(map);
2027 map = isl_map_coalesce(map);
2029 if (isl_map_plain_is_empty(map)) {
2030 map = isl_map_from_range(isl_map_wrap(map));
2031 map = isl_map_add_dims(map, isl_dim_in, 1);
2032 map = isl_map_set_dim_name(map, isl_dim_in, 0, "k");
2033 return map;
2036 target_dim = isl_map_get_space(map);
2037 target_dim = isl_space_from_range(isl_space_wrap(target_dim));
2038 target_dim = isl_space_add_dims(target_dim, isl_dim_in, 1);
2039 target_dim = isl_space_set_dim_name(target_dim, isl_dim_in, 0, "k");
2041 map = map_power(map, exact, 0);
2043 map = isl_map_add_dims(map, isl_dim_param, 1);
2044 dim = isl_map_get_space(map);
2045 diff = equate_parameter_to_length(dim, param);
2046 map = isl_map_intersect(map, diff);
2047 map = isl_map_project_out(map, isl_dim_in, d, 1);
2048 map = isl_map_project_out(map, isl_dim_out, d, 1);
2049 map = isl_map_from_range(isl_map_wrap(map));
2050 map = isl_map_move_dims(map, isl_dim_in, 0, isl_dim_param, param, 1);
2052 map = isl_map_reset_space(map, target_dim);
2054 return map;
2057 /* Compute a relation that maps each element in the range of the input
2058 * relation to the lengths of all paths composed of edges in the input
2059 * relation that end up in the given range element.
2060 * The result may be an overapproximation, in which case *exact is set to 0.
2061 * The resulting relation is very similar to the power relation.
2062 * The difference are that the domain has been projected out, the
2063 * range has become the domain and the exponent is the range instead
2064 * of a parameter.
2066 __isl_give isl_map *isl_map_reaching_path_lengths(__isl_take isl_map *map,
2067 int *exact)
2069 isl_space *dim;
2070 isl_map *diff;
2071 unsigned d;
2072 unsigned param;
2074 if (!map)
2075 return NULL;
2077 d = isl_map_dim(map, isl_dim_in);
2078 param = isl_map_dim(map, isl_dim_param);
2080 map = isl_map_compute_divs(map);
2081 map = isl_map_coalesce(map);
2083 if (isl_map_plain_is_empty(map)) {
2084 if (exact)
2085 *exact = 1;
2086 map = isl_map_project_out(map, isl_dim_out, 0, d);
2087 map = isl_map_add_dims(map, isl_dim_out, 1);
2088 return map;
2091 map = map_power(map, exact, 0);
2093 map = isl_map_add_dims(map, isl_dim_param, 1);
2094 dim = isl_map_get_space(map);
2095 diff = equate_parameter_to_length(dim, param);
2096 map = isl_map_intersect(map, diff);
2097 map = isl_map_project_out(map, isl_dim_in, 0, d + 1);
2098 map = isl_map_project_out(map, isl_dim_out, d, 1);
2099 map = isl_map_reverse(map);
2100 map = isl_map_move_dims(map, isl_dim_out, 0, isl_dim_param, param, 1);
2102 return map;
2105 /* Check whether equality i of bset is a pure stride constraint
2106 * on a single dimensions, i.e., of the form
2108 * v = k e
2110 * with k a constant and e an existentially quantified variable.
2112 static int is_eq_stride(__isl_keep isl_basic_set *bset, int i)
2114 unsigned nparam;
2115 unsigned d;
2116 unsigned n_div;
2117 int pos1;
2118 int pos2;
2120 if (!bset)
2121 return -1;
2123 if (!isl_int_is_zero(bset->eq[i][0]))
2124 return 0;
2126 nparam = isl_basic_set_dim(bset, isl_dim_param);
2127 d = isl_basic_set_dim(bset, isl_dim_set);
2128 n_div = isl_basic_set_dim(bset, isl_dim_div);
2130 if (isl_seq_first_non_zero(bset->eq[i] + 1, nparam) != -1)
2131 return 0;
2132 pos1 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam, d);
2133 if (pos1 == -1)
2134 return 0;
2135 if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + pos1 + 1,
2136 d - pos1 - 1) != -1)
2137 return 0;
2139 pos2 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d, n_div);
2140 if (pos2 == -1)
2141 return 0;
2142 if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d + pos2 + 1,
2143 n_div - pos2 - 1) != -1)
2144 return 0;
2145 if (!isl_int_is_one(bset->eq[i][1 + nparam + pos1]) &&
2146 !isl_int_is_negone(bset->eq[i][1 + nparam + pos1]))
2147 return 0;
2149 return 1;
2152 /* Given a map, compute the smallest superset of this map that is of the form
2154 * { i -> j : L <= j - i <= U and exists a_p: j_p - i_p = M_p a_p }
2156 * (where p ranges over the (non-parametric) dimensions),
2157 * compute the transitive closure of this map, i.e.,
2159 * { i -> j : exists k > 0:
2160 * k L <= j - i <= k U and exists a: j_p - i_p = M_p a_p }
2162 * and intersect domain and range of this transitive closure with
2163 * the given domain and range.
2165 * If with_id is set, then try to include as much of the identity mapping
2166 * as possible, by computing
2168 * { i -> j : exists k >= 0:
2169 * k L <= j - i <= k U and exists a: j_p - i_p = M_p a_p }
2171 * instead (i.e., allow k = 0).
2173 * In practice, we compute the difference set
2175 * delta = { j - i | i -> j in map },
2177 * look for stride constraint on the individual dimensions and compute
2178 * (constant) lower and upper bounds for each individual dimension,
2179 * adding a constraint for each bound not equal to infinity.
2181 static __isl_give isl_map *box_closure_on_domain(__isl_take isl_map *map,
2182 __isl_take isl_set *dom, __isl_take isl_set *ran, int with_id)
2184 int i;
2185 int k;
2186 unsigned d;
2187 unsigned nparam;
2188 unsigned total;
2189 isl_space *dim;
2190 isl_set *delta;
2191 isl_map *app = NULL;
2192 isl_basic_set *aff = NULL;
2193 isl_basic_map *bmap = NULL;
2194 isl_vec *obj = NULL;
2195 isl_int opt;
2197 isl_int_init(opt);
2199 delta = isl_map_deltas(isl_map_copy(map));
2201 aff = isl_set_affine_hull(isl_set_copy(delta));
2202 if (!aff)
2203 goto error;
2204 dim = isl_map_get_space(map);
2205 d = isl_space_dim(dim, isl_dim_in);
2206 nparam = isl_space_dim(dim, isl_dim_param);
2207 total = isl_space_dim(dim, isl_dim_all);
2208 bmap = isl_basic_map_alloc_space(dim,
2209 aff->n_div + 1, aff->n_div, 2 * d + 1);
2210 for (i = 0; i < aff->n_div + 1; ++i) {
2211 k = isl_basic_map_alloc_div(bmap);
2212 if (k < 0)
2213 goto error;
2214 isl_int_set_si(bmap->div[k][0], 0);
2216 for (i = 0; i < aff->n_eq; ++i) {
2217 if (!is_eq_stride(aff, i))
2218 continue;
2219 k = isl_basic_map_alloc_equality(bmap);
2220 if (k < 0)
2221 goto error;
2222 isl_seq_clr(bmap->eq[k], 1 + nparam);
2223 isl_seq_cpy(bmap->eq[k] + 1 + nparam + d,
2224 aff->eq[i] + 1 + nparam, d);
2225 isl_seq_neg(bmap->eq[k] + 1 + nparam,
2226 aff->eq[i] + 1 + nparam, d);
2227 isl_seq_cpy(bmap->eq[k] + 1 + nparam + 2 * d,
2228 aff->eq[i] + 1 + nparam + d, aff->n_div);
2229 isl_int_set_si(bmap->eq[k][1 + total + aff->n_div], 0);
2231 obj = isl_vec_alloc(map->ctx, 1 + nparam + d);
2232 if (!obj)
2233 goto error;
2234 isl_seq_clr(obj->el, 1 + nparam + d);
2235 for (i = 0; i < d; ++ i) {
2236 enum isl_lp_result res;
2238 isl_int_set_si(obj->el[1 + nparam + i], 1);
2240 res = isl_set_solve_lp(delta, 0, obj->el, map->ctx->one, &opt,
2241 NULL, NULL);
2242 if (res == isl_lp_error)
2243 goto error;
2244 if (res == isl_lp_ok) {
2245 k = isl_basic_map_alloc_inequality(bmap);
2246 if (k < 0)
2247 goto error;
2248 isl_seq_clr(bmap->ineq[k],
2249 1 + nparam + 2 * d + bmap->n_div);
2250 isl_int_set_si(bmap->ineq[k][1 + nparam + i], -1);
2251 isl_int_set_si(bmap->ineq[k][1 + nparam + d + i], 1);
2252 isl_int_neg(bmap->ineq[k][1 + nparam + 2 * d + aff->n_div], opt);
2255 res = isl_set_solve_lp(delta, 1, obj->el, map->ctx->one, &opt,
2256 NULL, NULL);
2257 if (res == isl_lp_error)
2258 goto error;
2259 if (res == isl_lp_ok) {
2260 k = isl_basic_map_alloc_inequality(bmap);
2261 if (k < 0)
2262 goto error;
2263 isl_seq_clr(bmap->ineq[k],
2264 1 + nparam + 2 * d + bmap->n_div);
2265 isl_int_set_si(bmap->ineq[k][1 + nparam + i], 1);
2266 isl_int_set_si(bmap->ineq[k][1 + nparam + d + i], -1);
2267 isl_int_set(bmap->ineq[k][1 + nparam + 2 * d + aff->n_div], opt);
2270 isl_int_set_si(obj->el[1 + nparam + i], 0);
2272 k = isl_basic_map_alloc_inequality(bmap);
2273 if (k < 0)
2274 goto error;
2275 isl_seq_clr(bmap->ineq[k],
2276 1 + nparam + 2 * d + bmap->n_div);
2277 if (!with_id)
2278 isl_int_set_si(bmap->ineq[k][0], -1);
2279 isl_int_set_si(bmap->ineq[k][1 + nparam + 2 * d + aff->n_div], 1);
2281 app = isl_map_from_domain_and_range(dom, ran);
2283 isl_vec_free(obj);
2284 isl_basic_set_free(aff);
2285 isl_map_free(map);
2286 bmap = isl_basic_map_finalize(bmap);
2287 isl_set_free(delta);
2288 isl_int_clear(opt);
2290 map = isl_map_from_basic_map(bmap);
2291 map = isl_map_intersect(map, app);
2293 return map;
2294 error:
2295 isl_vec_free(obj);
2296 isl_basic_map_free(bmap);
2297 isl_basic_set_free(aff);
2298 isl_set_free(dom);
2299 isl_set_free(ran);
2300 isl_map_free(map);
2301 isl_set_free(delta);
2302 isl_int_clear(opt);
2303 return NULL;
2306 /* Given a map, compute the smallest superset of this map that is of the form
2308 * { i -> j : L <= j - i <= U and exists a_p: j_p - i_p = M_p a_p }
2310 * (where p ranges over the (non-parametric) dimensions),
2311 * compute the transitive closure of this map, i.e.,
2313 * { i -> j : exists k > 0:
2314 * k L <= j - i <= k U and exists a: j_p - i_p = M_p a_p }
2316 * and intersect domain and range of this transitive closure with
2317 * domain and range of the original map.
2319 static __isl_give isl_map *box_closure(__isl_take isl_map *map)
2321 isl_set *domain;
2322 isl_set *range;
2324 domain = isl_map_domain(isl_map_copy(map));
2325 domain = isl_set_coalesce(domain);
2326 range = isl_map_range(isl_map_copy(map));
2327 range = isl_set_coalesce(range);
2329 return box_closure_on_domain(map, domain, range, 0);
2332 /* Given a map, compute the smallest superset of this map that is of the form
2334 * { i -> j : L <= j - i <= U and exists a_p: j_p - i_p = M_p a_p }
2336 * (where p ranges over the (non-parametric) dimensions),
2337 * compute the transitive and partially reflexive closure of this map, i.e.,
2339 * { i -> j : exists k >= 0:
2340 * k L <= j - i <= k U and exists a: j_p - i_p = M_p a_p }
2342 * and intersect domain and range of this transitive closure with
2343 * the given domain.
2345 static __isl_give isl_map *box_closure_with_identity(__isl_take isl_map *map,
2346 __isl_take isl_set *dom)
2348 return box_closure_on_domain(map, dom, isl_set_copy(dom), 1);
2351 /* Check whether app is the transitive closure of map.
2352 * In particular, check that app is acyclic and, if so,
2353 * check that
2355 * app \subset (map \cup (map \circ app))
2357 static int check_exactness_omega(__isl_keep isl_map *map,
2358 __isl_keep isl_map *app)
2360 isl_set *delta;
2361 int i;
2362 int is_empty, is_exact;
2363 unsigned d;
2364 isl_map *test;
2366 delta = isl_map_deltas(isl_map_copy(app));
2367 d = isl_set_dim(delta, isl_dim_set);
2368 for (i = 0; i < d; ++i)
2369 delta = isl_set_fix_si(delta, isl_dim_set, i, 0);
2370 is_empty = isl_set_is_empty(delta);
2371 isl_set_free(delta);
2372 if (is_empty < 0)
2373 return -1;
2374 if (!is_empty)
2375 return 0;
2377 test = isl_map_apply_range(isl_map_copy(app), isl_map_copy(map));
2378 test = isl_map_union(test, isl_map_copy(map));
2379 is_exact = isl_map_is_subset(app, test);
2380 isl_map_free(test);
2382 return is_exact;
2385 /* Check if basic map M_i can be combined with all the other
2386 * basic maps such that
2388 * (\cup_j M_j)^+
2390 * can be computed as
2392 * M_i \cup (\cup_{j \ne i} M_i^* \circ M_j \circ M_i^*)^+
2394 * In particular, check if we can compute a compact representation
2395 * of
2397 * M_i^* \circ M_j \circ M_i^*
2399 * for each j != i.
2400 * Let M_i^? be an extension of M_i^+ that allows paths
2401 * of length zero, i.e., the result of box_closure(., 1).
2402 * The criterion, as proposed by Kelly et al., is that
2403 * id = M_i^? - M_i^+ can be represented as a basic map
2404 * and that
2406 * id \circ M_j \circ id = M_j
2408 * for each j != i.
2410 * If this function returns 1, then tc and qc are set to
2411 * M_i^+ and M_i^?, respectively.
2413 static int can_be_split_off(__isl_keep isl_map *map, int i,
2414 __isl_give isl_map **tc, __isl_give isl_map **qc)
2416 isl_map *map_i, *id = NULL;
2417 int j = -1;
2418 isl_set *C;
2420 *tc = NULL;
2421 *qc = NULL;
2423 C = isl_set_union(isl_map_domain(isl_map_copy(map)),
2424 isl_map_range(isl_map_copy(map)));
2425 C = isl_set_from_basic_set(isl_set_simple_hull(C));
2426 if (!C)
2427 goto error;
2429 map_i = isl_map_from_basic_map(isl_basic_map_copy(map->p[i]));
2430 *tc = box_closure(isl_map_copy(map_i));
2431 *qc = box_closure_with_identity(map_i, C);
2432 id = isl_map_subtract(isl_map_copy(*qc), isl_map_copy(*tc));
2434 if (!id || !*qc)
2435 goto error;
2436 if (id->n != 1 || (*qc)->n != 1)
2437 goto done;
2439 for (j = 0; j < map->n; ++j) {
2440 isl_map *map_j, *test;
2441 int is_ok;
2443 if (i == j)
2444 continue;
2445 map_j = isl_map_from_basic_map(
2446 isl_basic_map_copy(map->p[j]));
2447 test = isl_map_apply_range(isl_map_copy(id),
2448 isl_map_copy(map_j));
2449 test = isl_map_apply_range(test, isl_map_copy(id));
2450 is_ok = isl_map_is_equal(test, map_j);
2451 isl_map_free(map_j);
2452 isl_map_free(test);
2453 if (is_ok < 0)
2454 goto error;
2455 if (!is_ok)
2456 break;
2459 done:
2460 isl_map_free(id);
2461 if (j == map->n)
2462 return 1;
2464 isl_map_free(*qc);
2465 isl_map_free(*tc);
2466 *qc = NULL;
2467 *tc = NULL;
2469 return 0;
2470 error:
2471 isl_map_free(id);
2472 isl_map_free(*qc);
2473 isl_map_free(*tc);
2474 *qc = NULL;
2475 *tc = NULL;
2476 return -1;
2479 static __isl_give isl_map *box_closure_with_check(__isl_take isl_map *map,
2480 int *exact)
2482 isl_map *app;
2484 app = box_closure(isl_map_copy(map));
2485 if (exact)
2486 *exact = check_exactness_omega(map, app);
2488 isl_map_free(map);
2489 return app;
2492 /* Compute an overapproximation of the transitive closure of "map"
2493 * using a variation of the algorithm from
2494 * "Transitive Closure of Infinite Graphs and its Applications"
2495 * by Kelly et al.
2497 * We first check whether we can can split of any basic map M_i and
2498 * compute
2500 * (\cup_j M_j)^+
2502 * as
2504 * M_i \cup (\cup_{j \ne i} M_i^* \circ M_j \circ M_i^*)^+
2506 * using a recursive call on the remaining map.
2508 * If not, we simply call box_closure on the whole map.
2510 static __isl_give isl_map *transitive_closure_omega(__isl_take isl_map *map,
2511 int *exact)
2513 int i, j;
2514 int exact_i;
2515 isl_map *app;
2517 if (!map)
2518 return NULL;
2519 if (map->n == 1)
2520 return box_closure_with_check(map, exact);
2522 for (i = 0; i < map->n; ++i) {
2523 int ok;
2524 isl_map *qc, *tc;
2525 ok = can_be_split_off(map, i, &tc, &qc);
2526 if (ok < 0)
2527 goto error;
2528 if (!ok)
2529 continue;
2531 app = isl_map_alloc_space(isl_map_get_space(map), map->n - 1, 0);
2533 for (j = 0; j < map->n; ++j) {
2534 if (j == i)
2535 continue;
2536 app = isl_map_add_basic_map(app,
2537 isl_basic_map_copy(map->p[j]));
2540 app = isl_map_apply_range(isl_map_copy(qc), app);
2541 app = isl_map_apply_range(app, qc);
2543 app = isl_map_union(tc, transitive_closure_omega(app, NULL));
2544 exact_i = check_exactness_omega(map, app);
2545 if (exact_i == 1) {
2546 if (exact)
2547 *exact = exact_i;
2548 isl_map_free(map);
2549 return app;
2551 isl_map_free(app);
2552 if (exact_i < 0)
2553 goto error;
2556 return box_closure_with_check(map, exact);
2557 error:
2558 isl_map_free(map);
2559 return NULL;
2562 /* Compute the transitive closure of "map", or an overapproximation.
2563 * If the result is exact, then *exact is set to 1.
2564 * Simply use map_power to compute the powers of map, but tell
2565 * it to project out the lengths of the paths instead of equating
2566 * the length to a parameter.
2568 __isl_give isl_map *isl_map_transitive_closure(__isl_take isl_map *map,
2569 int *exact)
2571 isl_space *target_dim;
2572 int closed;
2574 if (!map)
2575 goto error;
2577 if (map->ctx->opt->closure == ISL_CLOSURE_BOX)
2578 return transitive_closure_omega(map, exact);
2580 map = isl_map_compute_divs(map);
2581 map = isl_map_coalesce(map);
2582 closed = isl_map_is_transitively_closed(map);
2583 if (closed < 0)
2584 goto error;
2585 if (closed) {
2586 if (exact)
2587 *exact = 1;
2588 return map;
2591 target_dim = isl_map_get_space(map);
2592 map = map_power(map, exact, 1);
2593 map = isl_map_reset_space(map, target_dim);
2595 return map;
2596 error:
2597 isl_map_free(map);
2598 return NULL;
2601 static int inc_count(__isl_take isl_map *map, void *user)
2603 int *n = user;
2605 *n += map->n;
2607 isl_map_free(map);
2609 return 0;
2612 static int collect_basic_map(__isl_take isl_map *map, void *user)
2614 int i;
2615 isl_basic_map ***next = user;
2617 for (i = 0; i < map->n; ++i) {
2618 **next = isl_basic_map_copy(map->p[i]);
2619 if (!**next)
2620 goto error;
2621 (*next)++;
2624 isl_map_free(map);
2625 return 0;
2626 error:
2627 isl_map_free(map);
2628 return -1;
2631 /* Perform Floyd-Warshall on the given list of basic relations.
2632 * The basic relations may live in different dimensions,
2633 * but basic relations that get assigned to the diagonal of the
2634 * grid have domains and ranges of the same dimension and so
2635 * the standard algorithm can be used because the nested transitive
2636 * closures are only applied to diagonal elements and because all
2637 * compositions are peformed on relations with compatible domains and ranges.
2639 static __isl_give isl_union_map *union_floyd_warshall_on_list(isl_ctx *ctx,
2640 __isl_keep isl_basic_map **list, int n, int *exact)
2642 int i, j, k;
2643 int n_group;
2644 int *group = NULL;
2645 isl_set **set = NULL;
2646 isl_map ***grid = NULL;
2647 isl_union_map *app;
2649 group = setup_groups(ctx, list, n, &set, &n_group);
2650 if (!group)
2651 goto error;
2653 grid = isl_calloc_array(ctx, isl_map **, n_group);
2654 if (!grid)
2655 goto error;
2656 for (i = 0; i < n_group; ++i) {
2657 grid[i] = isl_calloc_array(ctx, isl_map *, n_group);
2658 if (!grid[i])
2659 goto error;
2660 for (j = 0; j < n_group; ++j) {
2661 isl_space *dim1, *dim2, *dim;
2662 dim1 = isl_space_reverse(isl_set_get_space(set[i]));
2663 dim2 = isl_set_get_space(set[j]);
2664 dim = isl_space_join(dim1, dim2);
2665 grid[i][j] = isl_map_empty(dim);
2669 for (k = 0; k < n; ++k) {
2670 i = group[2 * k];
2671 j = group[2 * k + 1];
2672 grid[i][j] = isl_map_union(grid[i][j],
2673 isl_map_from_basic_map(
2674 isl_basic_map_copy(list[k])));
2677 floyd_warshall_iterate(grid, n_group, exact);
2679 app = isl_union_map_empty(isl_map_get_space(grid[0][0]));
2681 for (i = 0; i < n_group; ++i) {
2682 for (j = 0; j < n_group; ++j)
2683 app = isl_union_map_add_map(app, grid[i][j]);
2684 free(grid[i]);
2686 free(grid);
2688 for (i = 0; i < 2 * n; ++i)
2689 isl_set_free(set[i]);
2690 free(set);
2692 free(group);
2693 return app;
2694 error:
2695 if (grid)
2696 for (i = 0; i < n_group; ++i) {
2697 if (!grid[i])
2698 continue;
2699 for (j = 0; j < n_group; ++j)
2700 isl_map_free(grid[i][j]);
2701 free(grid[i]);
2703 free(grid);
2704 if (set) {
2705 for (i = 0; i < 2 * n; ++i)
2706 isl_set_free(set[i]);
2707 free(set);
2709 free(group);
2710 return NULL;
2713 /* Perform Floyd-Warshall on the given union relation.
2714 * The implementation is very similar to that for non-unions.
2715 * The main difference is that it is applied unconditionally.
2716 * We first extract a list of basic maps from the union map
2717 * and then perform the algorithm on this list.
2719 static __isl_give isl_union_map *union_floyd_warshall(
2720 __isl_take isl_union_map *umap, int *exact)
2722 int i, n;
2723 isl_ctx *ctx;
2724 isl_basic_map **list = NULL;
2725 isl_basic_map **next;
2726 isl_union_map *res;
2728 n = 0;
2729 if (isl_union_map_foreach_map(umap, inc_count, &n) < 0)
2730 goto error;
2732 ctx = isl_union_map_get_ctx(umap);
2733 list = isl_calloc_array(ctx, isl_basic_map *, n);
2734 if (!list)
2735 goto error;
2737 next = list;
2738 if (isl_union_map_foreach_map(umap, collect_basic_map, &next) < 0)
2739 goto error;
2741 res = union_floyd_warshall_on_list(ctx, list, n, exact);
2743 if (list) {
2744 for (i = 0; i < n; ++i)
2745 isl_basic_map_free(list[i]);
2746 free(list);
2749 isl_union_map_free(umap);
2750 return res;
2751 error:
2752 if (list) {
2753 for (i = 0; i < n; ++i)
2754 isl_basic_map_free(list[i]);
2755 free(list);
2757 isl_union_map_free(umap);
2758 return NULL;
2761 /* Decompose the give union relation into strongly connected components.
2762 * The implementation is essentially the same as that of
2763 * construct_power_components with the major difference that all
2764 * operations are performed on union maps.
2766 static __isl_give isl_union_map *union_components(
2767 __isl_take isl_union_map *umap, int *exact)
2769 int i;
2770 int n;
2771 isl_ctx *ctx;
2772 isl_basic_map **list = NULL;
2773 isl_basic_map **next;
2774 isl_union_map *path = NULL;
2775 struct isl_tc_follows_data data;
2776 struct isl_tarjan_graph *g = NULL;
2777 int c, l;
2778 int recheck = 0;
2780 n = 0;
2781 if (isl_union_map_foreach_map(umap, inc_count, &n) < 0)
2782 goto error;
2784 if (n == 0)
2785 return umap;
2786 if (n <= 1)
2787 return union_floyd_warshall(umap, exact);
2789 ctx = isl_union_map_get_ctx(umap);
2790 list = isl_calloc_array(ctx, isl_basic_map *, n);
2791 if (!list)
2792 goto error;
2794 next = list;
2795 if (isl_union_map_foreach_map(umap, collect_basic_map, &next) < 0)
2796 goto error;
2798 data.list = list;
2799 data.check_closed = 0;
2800 g = isl_tarjan_graph_init(ctx, n, &basic_map_follows, &data);
2801 if (!g)
2802 goto error;
2804 c = 0;
2805 i = 0;
2806 l = n;
2807 path = isl_union_map_empty(isl_union_map_get_space(umap));
2808 while (l) {
2809 isl_union_map *comp;
2810 isl_union_map *path_comp, *path_comb;
2811 comp = isl_union_map_empty(isl_union_map_get_space(umap));
2812 while (g->order[i] != -1) {
2813 comp = isl_union_map_add_map(comp,
2814 isl_map_from_basic_map(
2815 isl_basic_map_copy(list[g->order[i]])));
2816 --l;
2817 ++i;
2819 path_comp = union_floyd_warshall(comp, exact);
2820 path_comb = isl_union_map_apply_range(isl_union_map_copy(path),
2821 isl_union_map_copy(path_comp));
2822 path = isl_union_map_union(path, path_comp);
2823 path = isl_union_map_union(path, path_comb);
2824 ++i;
2825 ++c;
2828 if (c > 1 && data.check_closed && !*exact) {
2829 int closed;
2831 closed = isl_union_map_is_transitively_closed(path);
2832 if (closed < 0)
2833 goto error;
2834 recheck = !closed;
2837 isl_tarjan_graph_free(g);
2839 for (i = 0; i < n; ++i)
2840 isl_basic_map_free(list[i]);
2841 free(list);
2843 if (recheck) {
2844 isl_union_map_free(path);
2845 return union_floyd_warshall(umap, exact);
2848 isl_union_map_free(umap);
2850 return path;
2851 error:
2852 isl_tarjan_graph_free(g);
2853 if (list) {
2854 for (i = 0; i < n; ++i)
2855 isl_basic_map_free(list[i]);
2856 free(list);
2858 isl_union_map_free(umap);
2859 isl_union_map_free(path);
2860 return NULL;
2863 /* Compute the transitive closure of "umap", or an overapproximation.
2864 * If the result is exact, then *exact is set to 1.
2866 __isl_give isl_union_map *isl_union_map_transitive_closure(
2867 __isl_take isl_union_map *umap, int *exact)
2869 int closed;
2871 if (!umap)
2872 return NULL;
2874 if (exact)
2875 *exact = 1;
2877 umap = isl_union_map_compute_divs(umap);
2878 umap = isl_union_map_coalesce(umap);
2879 closed = isl_union_map_is_transitively_closed(umap);
2880 if (closed < 0)
2881 goto error;
2882 if (closed)
2883 return umap;
2884 umap = union_components(umap, exact);
2885 return umap;
2886 error:
2887 isl_union_map_free(umap);
2888 return NULL;
2891 struct isl_union_power {
2892 isl_union_map *pow;
2893 int *exact;
2896 static int power(__isl_take isl_map *map, void *user)
2898 struct isl_union_power *up = user;
2900 map = isl_map_power(map, up->exact);
2901 up->pow = isl_union_map_from_map(map);
2903 return -1;
2906 /* Construct a map [x] -> [x+1], with parameters prescribed by "dim".
2908 static __isl_give isl_union_map *increment(__isl_take isl_space *dim)
2910 int k;
2911 isl_basic_map *bmap;
2913 dim = isl_space_add_dims(dim, isl_dim_in, 1);
2914 dim = isl_space_add_dims(dim, isl_dim_out, 1);
2915 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
2916 k = isl_basic_map_alloc_equality(bmap);
2917 if (k < 0)
2918 goto error;
2919 isl_seq_clr(bmap->eq[k], isl_basic_map_total_dim(bmap));
2920 isl_int_set_si(bmap->eq[k][0], 1);
2921 isl_int_set_si(bmap->eq[k][isl_basic_map_offset(bmap, isl_dim_in)], 1);
2922 isl_int_set_si(bmap->eq[k][isl_basic_map_offset(bmap, isl_dim_out)], -1);
2923 return isl_union_map_from_map(isl_map_from_basic_map(bmap));
2924 error:
2925 isl_basic_map_free(bmap);
2926 return NULL;
2929 /* Construct a map [[x]->[y]] -> [y-x], with parameters prescribed by "dim".
2931 static __isl_give isl_union_map *deltas_map(__isl_take isl_space *dim)
2933 isl_basic_map *bmap;
2935 dim = isl_space_add_dims(dim, isl_dim_in, 1);
2936 dim = isl_space_add_dims(dim, isl_dim_out, 1);
2937 bmap = isl_basic_map_universe(dim);
2938 bmap = isl_basic_map_deltas_map(bmap);
2940 return isl_union_map_from_map(isl_map_from_basic_map(bmap));
2943 /* Compute the positive powers of "map", or an overapproximation.
2944 * The result maps the exponent to a nested copy of the corresponding power.
2945 * If the result is exact, then *exact is set to 1.
2947 __isl_give isl_union_map *isl_union_map_power(__isl_take isl_union_map *umap,
2948 int *exact)
2950 int n;
2951 isl_union_map *inc;
2952 isl_union_map *dm;
2954 if (!umap)
2955 return NULL;
2956 n = isl_union_map_n_map(umap);
2957 if (n == 0)
2958 return umap;
2959 if (n == 1) {
2960 struct isl_union_power up = { NULL, exact };
2961 isl_union_map_foreach_map(umap, &power, &up);
2962 isl_union_map_free(umap);
2963 return up.pow;
2965 inc = increment(isl_union_map_get_space(umap));
2966 umap = isl_union_map_product(inc, umap);
2967 umap = isl_union_map_transitive_closure(umap, exact);
2968 umap = isl_union_map_zip(umap);
2969 dm = deltas_map(isl_union_map_get_space(umap));
2970 umap = isl_union_map_apply_domain(umap, dm);
2972 return umap;
2975 #undef TYPE
2976 #define TYPE isl_map
2977 #include "isl_power_templ.c"
2979 #undef TYPE
2980 #define TYPE isl_union_map
2981 #include "isl_power_templ.c"