isl_transitive_closure.c: set_path_length: avoid NULL pointer dereference
[isl.git] / isl_transitive_closure.c
blob817e4c3b12a5a0368899f15540128f2e90c09201
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 if (k < 0)
74 goto error;
75 c = bmap->eq[k];
76 } else {
77 k = isl_basic_map_alloc_inequality(bmap);
78 if (k < 0)
79 goto error;
80 c = bmap->ineq[k];
82 isl_seq_clr(c, 1 + isl_basic_map_total_dim(bmap));
83 isl_int_set_si(c[0], -length);
84 isl_int_set_si(c[1 + nparam + d - 1], -1);
85 isl_int_set_si(c[1 + nparam + d + d - 1], 1);
87 bmap = isl_basic_map_finalize(bmap);
88 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
90 return map;
91 error:
92 isl_basic_map_free(bmap);
93 isl_map_free(map);
94 return NULL;
97 /* Check whether the overapproximation of the power of "map" is exactly
98 * the power of "map". Let R be "map" and A_k the overapproximation.
99 * The approximation is exact if
101 * A_1 = R
102 * A_k = A_{k-1} \circ R k >= 2
104 * Since A_k is known to be an overapproximation, we only need to check
106 * A_1 \subset R
107 * A_k \subset A_{k-1} \circ R k >= 2
109 * In practice, "app" has an extra input and output coordinate
110 * to encode the length of the path. So, we first need to add
111 * this coordinate to "map" and set the length of the path to
112 * one.
114 static int check_power_exactness(__isl_take isl_map *map,
115 __isl_take isl_map *app)
117 int exact;
118 isl_map *app_1;
119 isl_map *app_2;
121 map = isl_map_add_dims(map, isl_dim_in, 1);
122 map = isl_map_add_dims(map, isl_dim_out, 1);
123 map = set_path_length(map, 1, 1);
125 app_1 = set_path_length(isl_map_copy(app), 1, 1);
127 exact = isl_map_is_subset(app_1, map);
128 isl_map_free(app_1);
130 if (!exact || exact < 0) {
131 isl_map_free(app);
132 isl_map_free(map);
133 return exact;
136 app_1 = set_path_length(isl_map_copy(app), 0, 1);
137 app_2 = set_path_length(app, 0, 2);
138 app_1 = isl_map_apply_range(map, app_1);
140 exact = isl_map_is_subset(app_2, app_1);
142 isl_map_free(app_1);
143 isl_map_free(app_2);
145 return exact;
148 /* Check whether the overapproximation of the power of "map" is exactly
149 * the power of "map", possibly after projecting out the power (if "project"
150 * is set).
152 * If "project" is set and if "steps" can only result in acyclic paths,
153 * then we check
155 * A = R \cup (A \circ R)
157 * where A is the overapproximation with the power projected out, i.e.,
158 * an overapproximation of the transitive closure.
159 * More specifically, since A is known to be an overapproximation, we check
161 * A \subset R \cup (A \circ R)
163 * Otherwise, we check if the power is exact.
165 * Note that "app" has an extra input and output coordinate to encode
166 * the length of the part. If we are only interested in the transitive
167 * closure, then we can simply project out these coordinates first.
169 static int check_exactness(__isl_take isl_map *map, __isl_take isl_map *app,
170 int project)
172 isl_map *test;
173 int exact;
174 unsigned d;
176 if (!project)
177 return check_power_exactness(map, app);
179 d = isl_map_dim(map, isl_dim_in);
180 app = set_path_length(app, 0, 1);
181 app = isl_map_project_out(app, isl_dim_in, d, 1);
182 app = isl_map_project_out(app, isl_dim_out, d, 1);
184 app = isl_map_reset_space(app, isl_map_get_space(map));
186 test = isl_map_apply_range(isl_map_copy(map), isl_map_copy(app));
187 test = isl_map_union(test, isl_map_copy(map));
189 exact = isl_map_is_subset(app, test);
191 isl_map_free(app);
192 isl_map_free(test);
194 isl_map_free(map);
196 return exact;
200 * The transitive closure implementation is based on the paper
201 * "Computing the Transitive Closure of a Union of Affine Integer
202 * Tuple Relations" by Anna Beletska, Denis Barthou, Wlodzimierz Bielecki and
203 * Albert Cohen.
206 /* Given a set of n offsets v_i (the rows of "steps"), construct a relation
207 * of the given dimension specification (Z^{n+1} -> Z^{n+1})
208 * that maps an element x to any element that can be reached
209 * by taking a non-negative number of steps along any of
210 * the extended offsets v'_i = [v_i 1].
211 * That is, construct
213 * { [x] -> [y] : exists k_i >= 0, y = x + \sum_i k_i v'_i }
215 * For any element in this relation, the number of steps taken
216 * is equal to the difference in the final coordinates.
218 static __isl_give isl_map *path_along_steps(__isl_take isl_space *dim,
219 __isl_keep isl_mat *steps)
221 int i, j, k;
222 struct isl_basic_map *path = NULL;
223 unsigned d;
224 unsigned n;
225 unsigned nparam;
227 if (!dim || !steps)
228 goto error;
230 d = isl_space_dim(dim, isl_dim_in);
231 n = steps->n_row;
232 nparam = isl_space_dim(dim, isl_dim_param);
234 path = isl_basic_map_alloc_space(isl_space_copy(dim), n, d, n);
236 for (i = 0; i < n; ++i) {
237 k = isl_basic_map_alloc_div(path);
238 if (k < 0)
239 goto error;
240 isl_assert(steps->ctx, i == k, goto error);
241 isl_int_set_si(path->div[k][0], 0);
244 for (i = 0; i < d; ++i) {
245 k = isl_basic_map_alloc_equality(path);
246 if (k < 0)
247 goto error;
248 isl_seq_clr(path->eq[k], 1 + isl_basic_map_total_dim(path));
249 isl_int_set_si(path->eq[k][1 + nparam + i], 1);
250 isl_int_set_si(path->eq[k][1 + nparam + d + i], -1);
251 if (i == d - 1)
252 for (j = 0; j < n; ++j)
253 isl_int_set_si(path->eq[k][1 + nparam + 2 * d + j], 1);
254 else
255 for (j = 0; j < n; ++j)
256 isl_int_set(path->eq[k][1 + nparam + 2 * d + j],
257 steps->row[j][i]);
260 for (i = 0; i < n; ++i) {
261 k = isl_basic_map_alloc_inequality(path);
262 if (k < 0)
263 goto error;
264 isl_seq_clr(path->ineq[k], 1 + isl_basic_map_total_dim(path));
265 isl_int_set_si(path->ineq[k][1 + nparam + 2 * d + i], 1);
268 isl_space_free(dim);
270 path = isl_basic_map_simplify(path);
271 path = isl_basic_map_finalize(path);
272 return isl_map_from_basic_map(path);
273 error:
274 isl_space_free(dim);
275 isl_basic_map_free(path);
276 return NULL;
279 #define IMPURE 0
280 #define PURE_PARAM 1
281 #define PURE_VAR 2
282 #define MIXED 3
284 /* Check whether the parametric constant term of constraint c is never
285 * positive in "bset".
287 static int parametric_constant_never_positive(__isl_keep isl_basic_set *bset,
288 isl_int *c, int *div_purity)
290 unsigned d;
291 unsigned n_div;
292 unsigned nparam;
293 int i;
294 int k;
295 int empty;
297 n_div = isl_basic_set_dim(bset, isl_dim_div);
298 d = isl_basic_set_dim(bset, isl_dim_set);
299 nparam = isl_basic_set_dim(bset, isl_dim_param);
301 bset = isl_basic_set_copy(bset);
302 bset = isl_basic_set_cow(bset);
303 bset = isl_basic_set_extend_constraints(bset, 0, 1);
304 k = isl_basic_set_alloc_inequality(bset);
305 if (k < 0)
306 goto error;
307 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
308 isl_seq_cpy(bset->ineq[k], c, 1 + nparam);
309 for (i = 0; i < n_div; ++i) {
310 if (div_purity[i] != PURE_PARAM)
311 continue;
312 isl_int_set(bset->ineq[k][1 + nparam + d + i],
313 c[1 + nparam + d + i]);
315 isl_int_sub_ui(bset->ineq[k][0], bset->ineq[k][0], 1);
316 empty = isl_basic_set_is_empty(bset);
317 isl_basic_set_free(bset);
319 return empty;
320 error:
321 isl_basic_set_free(bset);
322 return -1;
325 /* Return PURE_PARAM if only the coefficients of the parameters are non-zero.
326 * Return PURE_VAR if only the coefficients of the set variables are non-zero.
327 * Return MIXED if only the coefficients of the parameters and the set
328 * variables are non-zero and if moreover the parametric constant
329 * can never attain positive values.
330 * Return IMPURE otherwise.
332 static int purity(__isl_keep isl_basic_set *bset, isl_int *c, int *div_purity,
333 int eq)
335 unsigned d;
336 unsigned n_div;
337 unsigned nparam;
338 int empty;
339 int i;
340 int p = 0, v = 0;
342 n_div = isl_basic_set_dim(bset, isl_dim_div);
343 d = isl_basic_set_dim(bset, isl_dim_set);
344 nparam = isl_basic_set_dim(bset, isl_dim_param);
346 for (i = 0; i < n_div; ++i) {
347 if (isl_int_is_zero(c[1 + nparam + d + i]))
348 continue;
349 switch (div_purity[i]) {
350 case PURE_PARAM: p = 1; break;
351 case PURE_VAR: v = 1; break;
352 default: return IMPURE;
355 if (!p && isl_seq_first_non_zero(c + 1, nparam) == -1)
356 return PURE_VAR;
357 if (!v && isl_seq_first_non_zero(c + 1 + nparam, d) == -1)
358 return PURE_PARAM;
360 empty = parametric_constant_never_positive(bset, c, div_purity);
361 if (eq && empty >= 0 && !empty) {
362 isl_seq_neg(c, c, 1 + nparam + d + n_div);
363 empty = parametric_constant_never_positive(bset, c, div_purity);
366 return empty < 0 ? -1 : empty ? MIXED : IMPURE;
369 /* Return an array of integers indicating the type of each div in bset.
370 * If the div is (recursively) defined in terms of only the parameters,
371 * then the type is PURE_PARAM.
372 * If the div is (recursively) defined in terms of only the set variables,
373 * then the type is PURE_VAR.
374 * Otherwise, the type is IMPURE.
376 static __isl_give int *get_div_purity(__isl_keep isl_basic_set *bset)
378 int i, j;
379 int *div_purity;
380 unsigned d;
381 unsigned n_div;
382 unsigned nparam;
384 if (!bset)
385 return NULL;
387 n_div = isl_basic_set_dim(bset, isl_dim_div);
388 d = isl_basic_set_dim(bset, isl_dim_set);
389 nparam = isl_basic_set_dim(bset, isl_dim_param);
391 div_purity = isl_alloc_array(bset->ctx, int, n_div);
392 if (n_div && !div_purity)
393 return NULL;
395 for (i = 0; i < bset->n_div; ++i) {
396 int p = 0, v = 0;
397 if (isl_int_is_zero(bset->div[i][0])) {
398 div_purity[i] = IMPURE;
399 continue;
401 if (isl_seq_first_non_zero(bset->div[i] + 2, nparam) != -1)
402 p = 1;
403 if (isl_seq_first_non_zero(bset->div[i] + 2 + nparam, d) != -1)
404 v = 1;
405 for (j = 0; j < i; ++j) {
406 if (isl_int_is_zero(bset->div[i][2 + nparam + d + j]))
407 continue;
408 switch (div_purity[j]) {
409 case PURE_PARAM: p = 1; break;
410 case PURE_VAR: v = 1; break;
411 default: p = v = 1; break;
414 div_purity[i] = v ? p ? IMPURE : PURE_VAR : PURE_PARAM;
417 return div_purity;
420 /* Given a path with the as yet unconstrained length at position "pos",
421 * check if setting the length to zero results in only the identity
422 * mapping.
424 static int empty_path_is_identity(__isl_keep isl_basic_map *path, unsigned pos)
426 isl_basic_map *test = NULL;
427 isl_basic_map *id = NULL;
428 int k;
429 int is_id;
431 test = isl_basic_map_copy(path);
432 test = isl_basic_map_extend_constraints(test, 1, 0);
433 k = isl_basic_map_alloc_equality(test);
434 if (k < 0)
435 goto error;
436 isl_seq_clr(test->eq[k], 1 + isl_basic_map_total_dim(test));
437 isl_int_set_si(test->eq[k][pos], 1);
438 id = isl_basic_map_identity(isl_basic_map_get_space(path));
439 is_id = isl_basic_map_is_equal(test, id);
440 isl_basic_map_free(test);
441 isl_basic_map_free(id);
442 return is_id;
443 error:
444 isl_basic_map_free(test);
445 return -1;
448 /* If any of the constraints is found to be impure then this function
449 * sets *impurity to 1.
451 * If impurity is NULL then we are dealing with a non-parametric set
452 * and so the constraints are obviously PURE_VAR.
454 static __isl_give isl_basic_map *add_delta_constraints(
455 __isl_take isl_basic_map *path,
456 __isl_keep isl_basic_set *delta, unsigned off, unsigned nparam,
457 unsigned d, int *div_purity, int eq, int *impurity)
459 int i, k;
460 int n = eq ? delta->n_eq : delta->n_ineq;
461 isl_int **delta_c = eq ? delta->eq : delta->ineq;
462 unsigned n_div;
464 n_div = isl_basic_set_dim(delta, isl_dim_div);
466 for (i = 0; i < n; ++i) {
467 isl_int *path_c;
468 int p = PURE_VAR;
469 if (impurity)
470 p = purity(delta, delta_c[i], div_purity, eq);
471 if (p < 0)
472 goto error;
473 if (p != PURE_VAR && p != PURE_PARAM && !*impurity)
474 *impurity = 1;
475 if (p == IMPURE)
476 continue;
477 if (eq && p != MIXED) {
478 k = isl_basic_map_alloc_equality(path);
479 path_c = path->eq[k];
480 } else {
481 k = isl_basic_map_alloc_inequality(path);
482 path_c = path->ineq[k];
484 if (k < 0)
485 goto error;
486 isl_seq_clr(path_c, 1 + isl_basic_map_total_dim(path));
487 if (p == PURE_VAR) {
488 isl_seq_cpy(path_c + off,
489 delta_c[i] + 1 + nparam, d);
490 isl_int_set(path_c[off + d], delta_c[i][0]);
491 } else if (p == PURE_PARAM) {
492 isl_seq_cpy(path_c, delta_c[i], 1 + nparam);
493 } else {
494 isl_seq_cpy(path_c + off,
495 delta_c[i] + 1 + nparam, d);
496 isl_seq_cpy(path_c, delta_c[i], 1 + nparam);
498 isl_seq_cpy(path_c + off - n_div,
499 delta_c[i] + 1 + nparam + d, n_div);
502 return path;
503 error:
504 isl_basic_map_free(path);
505 return NULL;
508 /* Given a set of offsets "delta", construct a relation of the
509 * given dimension specification (Z^{n+1} -> Z^{n+1}) that
510 * is an overapproximation of the relations that
511 * maps an element x to any element that can be reached
512 * by taking a non-negative number of steps along any of
513 * the elements in "delta".
514 * That is, construct an approximation of
516 * { [x] -> [y] : exists f \in \delta, k \in Z :
517 * y = x + k [f, 1] and k >= 0 }
519 * For any element in this relation, the number of steps taken
520 * is equal to the difference in the final coordinates.
522 * In particular, let delta be defined as
524 * \delta = [p] -> { [x] : A x + a >= 0 and B p + b >= 0 and
525 * C x + C'p + c >= 0 and
526 * D x + D'p + d >= 0 }
528 * where the constraints C x + C'p + c >= 0 are such that the parametric
529 * constant term of each constraint j, "C_j x + C'_j p + c_j",
530 * can never attain positive values, then the relation is constructed as
532 * { [x] -> [y] : exists [f, k] \in Z^{n+1} : y = x + f and
533 * A f + k a >= 0 and B p + b >= 0 and
534 * C f + C'p + c >= 0 and k >= 1 }
535 * union { [x] -> [x] }
537 * If the zero-length paths happen to correspond exactly to the identity
538 * mapping, then we return
540 * { [x] -> [y] : exists [f, k] \in Z^{n+1} : y = x + f and
541 * A f + k a >= 0 and B p + b >= 0 and
542 * C f + C'p + c >= 0 and k >= 0 }
544 * instead.
546 * Existentially quantified variables in \delta are handled by
547 * classifying them as independent of the parameters, purely
548 * parameter dependent and others. Constraints containing
549 * any of the other existentially quantified variables are removed.
550 * This is safe, but leads to an additional overapproximation.
552 * If there are any impure constraints, then we also eliminate
553 * the parameters from \delta, resulting in a set
555 * \delta' = { [x] : E x + e >= 0 }
557 * and add the constraints
559 * E f + k e >= 0
561 * to the constructed relation.
563 static __isl_give isl_map *path_along_delta(__isl_take isl_space *dim,
564 __isl_take isl_basic_set *delta)
566 isl_basic_map *path = NULL;
567 unsigned d;
568 unsigned n_div;
569 unsigned nparam;
570 unsigned off;
571 int i, k;
572 int is_id;
573 int *div_purity = NULL;
574 int impurity = 0;
576 if (!delta)
577 goto error;
578 n_div = isl_basic_set_dim(delta, isl_dim_div);
579 d = isl_basic_set_dim(delta, isl_dim_set);
580 nparam = isl_basic_set_dim(delta, isl_dim_param);
581 path = isl_basic_map_alloc_space(isl_space_copy(dim), n_div + d + 1,
582 d + 1 + delta->n_eq, delta->n_eq + delta->n_ineq + 1);
583 off = 1 + nparam + 2 * (d + 1) + n_div;
585 for (i = 0; i < n_div + d + 1; ++i) {
586 k = isl_basic_map_alloc_div(path);
587 if (k < 0)
588 goto error;
589 isl_int_set_si(path->div[k][0], 0);
592 for (i = 0; i < d + 1; ++i) {
593 k = isl_basic_map_alloc_equality(path);
594 if (k < 0)
595 goto error;
596 isl_seq_clr(path->eq[k], 1 + isl_basic_map_total_dim(path));
597 isl_int_set_si(path->eq[k][1 + nparam + i], 1);
598 isl_int_set_si(path->eq[k][1 + nparam + d + 1 + i], -1);
599 isl_int_set_si(path->eq[k][off + i], 1);
602 div_purity = get_div_purity(delta);
603 if (n_div && !div_purity)
604 goto error;
606 path = add_delta_constraints(path, delta, off, nparam, d,
607 div_purity, 1, &impurity);
608 path = add_delta_constraints(path, delta, off, nparam, d,
609 div_purity, 0, &impurity);
610 if (impurity) {
611 isl_space *dim = isl_basic_set_get_space(delta);
612 delta = isl_basic_set_project_out(delta,
613 isl_dim_param, 0, nparam);
614 delta = isl_basic_set_add_dims(delta, isl_dim_param, nparam);
615 delta = isl_basic_set_reset_space(delta, dim);
616 if (!delta)
617 goto error;
618 path = isl_basic_map_extend_constraints(path, delta->n_eq,
619 delta->n_ineq + 1);
620 path = add_delta_constraints(path, delta, off, nparam, d,
621 NULL, 1, NULL);
622 path = add_delta_constraints(path, delta, off, nparam, d,
623 NULL, 0, NULL);
624 path = isl_basic_map_gauss(path, NULL);
627 is_id = empty_path_is_identity(path, off + d);
628 if (is_id < 0)
629 goto error;
631 k = isl_basic_map_alloc_inequality(path);
632 if (k < 0)
633 goto error;
634 isl_seq_clr(path->ineq[k], 1 + isl_basic_map_total_dim(path));
635 if (!is_id)
636 isl_int_set_si(path->ineq[k][0], -1);
637 isl_int_set_si(path->ineq[k][off + d], 1);
639 free(div_purity);
640 isl_basic_set_free(delta);
641 path = isl_basic_map_finalize(path);
642 if (is_id) {
643 isl_space_free(dim);
644 return isl_map_from_basic_map(path);
646 return isl_basic_map_union(path, isl_basic_map_identity(dim));
647 error:
648 free(div_purity);
649 isl_space_free(dim);
650 isl_basic_set_free(delta);
651 isl_basic_map_free(path);
652 return NULL;
655 /* Given a dimension specification Z^{n+1} -> Z^{n+1} and a parameter "param",
656 * construct a map that equates the parameter to the difference
657 * in the final coordinates and imposes that this difference is positive.
658 * That is, construct
660 * { [x,x_s] -> [y,y_s] : k = y_s - x_s > 0 }
662 static __isl_give isl_map *equate_parameter_to_length(__isl_take isl_space *dim,
663 unsigned param)
665 struct isl_basic_map *bmap;
666 unsigned d;
667 unsigned nparam;
668 int k;
670 d = isl_space_dim(dim, isl_dim_in);
671 nparam = isl_space_dim(dim, isl_dim_param);
672 bmap = isl_basic_map_alloc_space(dim, 0, 1, 1);
673 k = isl_basic_map_alloc_equality(bmap);
674 if (k < 0)
675 goto error;
676 isl_seq_clr(bmap->eq[k], 1 + isl_basic_map_total_dim(bmap));
677 isl_int_set_si(bmap->eq[k][1 + param], -1);
678 isl_int_set_si(bmap->eq[k][1 + nparam + d - 1], -1);
679 isl_int_set_si(bmap->eq[k][1 + nparam + d + d - 1], 1);
681 k = isl_basic_map_alloc_inequality(bmap);
682 if (k < 0)
683 goto error;
684 isl_seq_clr(bmap->ineq[k], 1 + isl_basic_map_total_dim(bmap));
685 isl_int_set_si(bmap->ineq[k][1 + param], 1);
686 isl_int_set_si(bmap->ineq[k][0], -1);
688 bmap = isl_basic_map_finalize(bmap);
689 return isl_map_from_basic_map(bmap);
690 error:
691 isl_basic_map_free(bmap);
692 return NULL;
695 /* Check whether "path" is acyclic, where the last coordinates of domain
696 * and range of path encode the number of steps taken.
697 * That is, check whether
699 * { d | d = y - x and (x,y) in path }
701 * does not contain any element with positive last coordinate (positive length)
702 * and zero remaining coordinates (cycle).
704 static int is_acyclic(__isl_take isl_map *path)
706 int i;
707 int acyclic;
708 unsigned dim;
709 struct isl_set *delta;
711 delta = isl_map_deltas(path);
712 dim = isl_set_dim(delta, isl_dim_set);
713 for (i = 0; i < dim; ++i) {
714 if (i == dim -1)
715 delta = isl_set_lower_bound_si(delta, isl_dim_set, i, 1);
716 else
717 delta = isl_set_fix_si(delta, isl_dim_set, i, 0);
720 acyclic = isl_set_is_empty(delta);
721 isl_set_free(delta);
723 return acyclic;
726 /* Given a union of basic maps R = \cup_i R_i \subseteq D \times D
727 * and a dimension specification (Z^{n+1} -> Z^{n+1}),
728 * construct a map that is an overapproximation of the map
729 * that takes an element from the space D \times Z to another
730 * element from the same space, such that the first n coordinates of the
731 * difference between them is a sum of differences between images
732 * and pre-images in one of the R_i and such that the last coordinate
733 * is equal to the number of steps taken.
734 * That is, let
736 * \Delta_i = { y - x | (x, y) in R_i }
738 * then the constructed map is an overapproximation of
740 * { (x) -> (x + d) | \exists k_i >= 0, \delta_i \in \Delta_i :
741 * d = (\sum_i k_i \delta_i, \sum_i k_i) }
743 * The elements of the singleton \Delta_i's are collected as the
744 * rows of the steps matrix. For all these \Delta_i's together,
745 * a single path is constructed.
746 * For each of the other \Delta_i's, we compute an overapproximation
747 * of the paths along elements of \Delta_i.
748 * Since each of these paths performs an addition, composition is
749 * symmetric and we can simply compose all resulting paths in any order.
751 static __isl_give isl_map *construct_extended_path(__isl_take isl_space *dim,
752 __isl_keep isl_map *map, int *project)
754 struct isl_mat *steps = NULL;
755 struct isl_map *path = NULL;
756 unsigned d;
757 int i, j, n;
759 d = isl_map_dim(map, isl_dim_in);
761 path = isl_map_identity(isl_space_copy(dim));
763 steps = isl_mat_alloc(map->ctx, map->n, d);
764 if (!steps)
765 goto error;
767 n = 0;
768 for (i = 0; i < map->n; ++i) {
769 struct isl_basic_set *delta;
771 delta = isl_basic_map_deltas(isl_basic_map_copy(map->p[i]));
773 for (j = 0; j < d; ++j) {
774 int fixed;
776 fixed = isl_basic_set_plain_dim_is_fixed(delta, j,
777 &steps->row[n][j]);
778 if (fixed < 0) {
779 isl_basic_set_free(delta);
780 goto error;
782 if (!fixed)
783 break;
787 if (j < d) {
788 path = isl_map_apply_range(path,
789 path_along_delta(isl_space_copy(dim), delta));
790 path = isl_map_coalesce(path);
791 } else {
792 isl_basic_set_free(delta);
793 ++n;
797 if (n > 0) {
798 steps->n_row = n;
799 path = isl_map_apply_range(path,
800 path_along_steps(isl_space_copy(dim), steps));
803 if (project && *project) {
804 *project = is_acyclic(isl_map_copy(path));
805 if (*project < 0)
806 goto error;
809 isl_space_free(dim);
810 isl_mat_free(steps);
811 return path;
812 error:
813 isl_space_free(dim);
814 isl_mat_free(steps);
815 isl_map_free(path);
816 return NULL;
819 static int isl_set_overlaps(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
821 isl_set *i;
822 int no_overlap;
824 if (!isl_space_tuple_is_equal(set1->dim, isl_dim_set,
825 set2->dim, isl_dim_set))
826 return 0;
828 i = isl_set_intersect(isl_set_copy(set1), isl_set_copy(set2));
829 no_overlap = isl_set_is_empty(i);
830 isl_set_free(i);
832 return no_overlap < 0 ? -1 : !no_overlap;
835 /* Given a union of basic maps R = \cup_i R_i \subseteq D \times D
836 * and a dimension specification (Z^{n+1} -> Z^{n+1}),
837 * construct a map that is an overapproximation of the map
838 * that takes an element from the dom R \times Z to an
839 * element from ran R \times Z, such that the first n coordinates of the
840 * difference between them is a sum of differences between images
841 * and pre-images in one of the R_i and such that the last coordinate
842 * is equal to the number of steps taken.
843 * That is, let
845 * \Delta_i = { y - x | (x, y) in R_i }
847 * then the constructed map is an overapproximation of
849 * { (x) -> (x + d) | \exists k_i >= 0, \delta_i \in \Delta_i :
850 * d = (\sum_i k_i \delta_i, \sum_i k_i) and
851 * x in dom R and x + d in ran R and
852 * \sum_i k_i >= 1 }
854 static __isl_give isl_map *construct_component(__isl_take isl_space *dim,
855 __isl_keep isl_map *map, int *exact, int project)
857 struct isl_set *domain = NULL;
858 struct isl_set *range = NULL;
859 struct isl_map *app = NULL;
860 struct isl_map *path = NULL;
862 domain = isl_map_domain(isl_map_copy(map));
863 domain = isl_set_coalesce(domain);
864 range = isl_map_range(isl_map_copy(map));
865 range = isl_set_coalesce(range);
866 if (!isl_set_overlaps(domain, range)) {
867 isl_set_free(domain);
868 isl_set_free(range);
869 isl_space_free(dim);
871 map = isl_map_copy(map);
872 map = isl_map_add_dims(map, isl_dim_in, 1);
873 map = isl_map_add_dims(map, isl_dim_out, 1);
874 map = set_path_length(map, 1, 1);
875 return map;
877 app = isl_map_from_domain_and_range(domain, range);
878 app = isl_map_add_dims(app, isl_dim_in, 1);
879 app = isl_map_add_dims(app, isl_dim_out, 1);
881 path = construct_extended_path(isl_space_copy(dim), map,
882 exact && *exact ? &project : NULL);
883 app = isl_map_intersect(app, path);
885 if (exact && *exact &&
886 (*exact = check_exactness(isl_map_copy(map), isl_map_copy(app),
887 project)) < 0)
888 goto error;
890 isl_space_free(dim);
891 app = set_path_length(app, 0, 1);
892 return app;
893 error:
894 isl_space_free(dim);
895 isl_map_free(app);
896 return NULL;
899 /* Call construct_component and, if "project" is set, project out
900 * the final coordinates.
902 static __isl_give isl_map *construct_projected_component(
903 __isl_take isl_space *dim,
904 __isl_keep isl_map *map, int *exact, int project)
906 isl_map *app;
907 unsigned d;
909 if (!dim)
910 return NULL;
911 d = isl_space_dim(dim, isl_dim_in);
913 app = construct_component(dim, map, exact, project);
914 if (project) {
915 app = isl_map_project_out(app, isl_dim_in, d - 1, 1);
916 app = isl_map_project_out(app, isl_dim_out, d - 1, 1);
918 return app;
921 /* Compute an extended version, i.e., with path lengths, of
922 * an overapproximation of the transitive closure of "bmap"
923 * with path lengths greater than or equal to zero and with
924 * domain and range equal to "dom".
926 static __isl_give isl_map *q_closure(__isl_take isl_space *dim,
927 __isl_take isl_set *dom, __isl_keep isl_basic_map *bmap, int *exact)
929 int project = 1;
930 isl_map *path;
931 isl_map *map;
932 isl_map *app;
934 dom = isl_set_add_dims(dom, isl_dim_set, 1);
935 app = isl_map_from_domain_and_range(dom, isl_set_copy(dom));
936 map = isl_map_from_basic_map(isl_basic_map_copy(bmap));
937 path = construct_extended_path(dim, map, &project);
938 app = isl_map_intersect(app, path);
940 if ((*exact = check_exactness(map, isl_map_copy(app), project)) < 0)
941 goto error;
943 return app;
944 error:
945 isl_map_free(app);
946 return NULL;
949 /* Check whether qc has any elements of length at least one
950 * with domain and/or range outside of dom and ran.
952 static int has_spurious_elements(__isl_keep isl_map *qc,
953 __isl_keep isl_set *dom, __isl_keep isl_set *ran)
955 isl_set *s;
956 int subset;
957 unsigned d;
959 if (!qc || !dom || !ran)
960 return -1;
962 d = isl_map_dim(qc, isl_dim_in);
964 qc = isl_map_copy(qc);
965 qc = set_path_length(qc, 0, 1);
966 qc = isl_map_project_out(qc, isl_dim_in, d - 1, 1);
967 qc = isl_map_project_out(qc, isl_dim_out, d - 1, 1);
969 s = isl_map_domain(isl_map_copy(qc));
970 subset = isl_set_is_subset(s, dom);
971 isl_set_free(s);
972 if (subset < 0)
973 goto error;
974 if (!subset) {
975 isl_map_free(qc);
976 return 1;
979 s = isl_map_range(qc);
980 subset = isl_set_is_subset(s, ran);
981 isl_set_free(s);
983 return subset < 0 ? -1 : !subset;
984 error:
985 isl_map_free(qc);
986 return -1;
989 #define LEFT 2
990 #define RIGHT 1
992 /* For each basic map in "map", except i, check whether it combines
993 * with the transitive closure that is reflexive on C combines
994 * to the left and to the right.
996 * In particular, if
998 * dom map_j \subseteq C
1000 * then right[j] is set to 1. Otherwise, if
1002 * ran map_i \cap dom map_j = \emptyset
1004 * then right[j] is set to 0. Otherwise, composing to the right
1005 * is impossible.
1007 * Similar, for composing to the left, we have if
1009 * ran map_j \subseteq C
1011 * then left[j] is set to 1. Otherwise, if
1013 * dom map_i \cap ran map_j = \emptyset
1015 * then left[j] is set to 0. Otherwise, composing to the left
1016 * is impossible.
1018 * The return value is or'd with LEFT if composing to the left
1019 * is possible and with RIGHT if composing to the right is possible.
1021 static int composability(__isl_keep isl_set *C, int i,
1022 isl_set **dom, isl_set **ran, int *left, int *right,
1023 __isl_keep isl_map *map)
1025 int j;
1026 int ok;
1028 ok = LEFT | RIGHT;
1029 for (j = 0; j < map->n && ok; ++j) {
1030 int overlaps, subset;
1031 if (j == i)
1032 continue;
1034 if (ok & RIGHT) {
1035 if (!dom[j])
1036 dom[j] = isl_set_from_basic_set(
1037 isl_basic_map_domain(
1038 isl_basic_map_copy(map->p[j])));
1039 if (!dom[j])
1040 return -1;
1041 overlaps = isl_set_overlaps(ran[i], dom[j]);
1042 if (overlaps < 0)
1043 return -1;
1044 if (!overlaps)
1045 right[j] = 0;
1046 else {
1047 subset = isl_set_is_subset(dom[j], C);
1048 if (subset < 0)
1049 return -1;
1050 if (subset)
1051 right[j] = 1;
1052 else
1053 ok &= ~RIGHT;
1057 if (ok & LEFT) {
1058 if (!ran[j])
1059 ran[j] = isl_set_from_basic_set(
1060 isl_basic_map_range(
1061 isl_basic_map_copy(map->p[j])));
1062 if (!ran[j])
1063 return -1;
1064 overlaps = isl_set_overlaps(dom[i], ran[j]);
1065 if (overlaps < 0)
1066 return -1;
1067 if (!overlaps)
1068 left[j] = 0;
1069 else {
1070 subset = isl_set_is_subset(ran[j], C);
1071 if (subset < 0)
1072 return -1;
1073 if (subset)
1074 left[j] = 1;
1075 else
1076 ok &= ~LEFT;
1081 return ok;
1084 static __isl_give isl_map *anonymize(__isl_take isl_map *map)
1086 map = isl_map_reset(map, isl_dim_in);
1087 map = isl_map_reset(map, isl_dim_out);
1088 return map;
1091 /* Return a map that is a union of the basic maps in "map", except i,
1092 * composed to left and right with qc based on the entries of "left"
1093 * and "right".
1095 static __isl_give isl_map *compose(__isl_keep isl_map *map, int i,
1096 __isl_take isl_map *qc, int *left, int *right)
1098 int j;
1099 isl_map *comp;
1101 comp = isl_map_empty(isl_map_get_space(map));
1102 for (j = 0; j < map->n; ++j) {
1103 isl_map *map_j;
1105 if (j == i)
1106 continue;
1108 map_j = isl_map_from_basic_map(isl_basic_map_copy(map->p[j]));
1109 map_j = anonymize(map_j);
1110 if (left && left[j])
1111 map_j = isl_map_apply_range(map_j, isl_map_copy(qc));
1112 if (right && right[j])
1113 map_j = isl_map_apply_range(isl_map_copy(qc), map_j);
1114 comp = isl_map_union(comp, map_j);
1117 comp = isl_map_compute_divs(comp);
1118 comp = isl_map_coalesce(comp);
1120 isl_map_free(qc);
1122 return comp;
1125 /* Compute the transitive closure of "map" incrementally by
1126 * computing
1128 * map_i^+ \cup qc^+
1130 * or
1132 * map_i^+ \cup ((id \cup map_i^) \circ qc^+)
1134 * or
1136 * map_i^+ \cup (qc^+ \circ (id \cup map_i^))
1138 * depending on whether left or right are NULL.
1140 static __isl_give isl_map *compute_incremental(
1141 __isl_take isl_space *dim, __isl_keep isl_map *map,
1142 int i, __isl_take isl_map *qc, int *left, int *right, int *exact)
1144 isl_map *map_i;
1145 isl_map *tc;
1146 isl_map *rtc = NULL;
1148 if (!map)
1149 goto error;
1150 isl_assert(map->ctx, left || right, goto error);
1152 map_i = isl_map_from_basic_map(isl_basic_map_copy(map->p[i]));
1153 tc = construct_projected_component(isl_space_copy(dim), map_i,
1154 exact, 1);
1155 isl_map_free(map_i);
1157 if (*exact)
1158 qc = isl_map_transitive_closure(qc, exact);
1160 if (!*exact) {
1161 isl_space_free(dim);
1162 isl_map_free(tc);
1163 isl_map_free(qc);
1164 return isl_map_universe(isl_map_get_space(map));
1167 if (!left || !right)
1168 rtc = isl_map_union(isl_map_copy(tc),
1169 isl_map_identity(isl_map_get_space(tc)));
1170 if (!right)
1171 qc = isl_map_apply_range(rtc, qc);
1172 if (!left)
1173 qc = isl_map_apply_range(qc, rtc);
1174 qc = isl_map_union(tc, qc);
1176 isl_space_free(dim);
1178 return qc;
1179 error:
1180 isl_space_free(dim);
1181 isl_map_free(qc);
1182 return NULL;
1185 /* Given a map "map", try to find a basic map such that
1186 * map^+ can be computed as
1188 * map^+ = map_i^+ \cup
1189 * \bigcup_j ((map_i^+ \cup Id_C)^+ \circ map_j \circ (map_i^+ \cup Id_C))^+
1191 * with C the simple hull of the domain and range of the input map.
1192 * map_i^ \cup Id_C is computed by allowing the path lengths to be zero
1193 * and by intersecting domain and range with C.
1194 * Of course, we need to check that this is actually equal to map_i^ \cup Id_C.
1195 * Also, we only use the incremental computation if all the transitive
1196 * closures are exact and if the number of basic maps in the union,
1197 * after computing the integer divisions, is smaller than the number
1198 * of basic maps in the input map.
1200 static int incemental_on_entire_domain(__isl_keep isl_space *dim,
1201 __isl_keep isl_map *map,
1202 isl_set **dom, isl_set **ran, int *left, int *right,
1203 __isl_give isl_map **res)
1205 int i;
1206 isl_set *C;
1207 unsigned d;
1209 *res = NULL;
1211 C = isl_set_union(isl_map_domain(isl_map_copy(map)),
1212 isl_map_range(isl_map_copy(map)));
1213 C = isl_set_from_basic_set(isl_set_simple_hull(C));
1214 if (!C)
1215 return -1;
1216 if (C->n != 1) {
1217 isl_set_free(C);
1218 return 0;
1221 d = isl_map_dim(map, isl_dim_in);
1223 for (i = 0; i < map->n; ++i) {
1224 isl_map *qc;
1225 int exact_i, spurious;
1226 int j;
1227 dom[i] = isl_set_from_basic_set(isl_basic_map_domain(
1228 isl_basic_map_copy(map->p[i])));
1229 ran[i] = isl_set_from_basic_set(isl_basic_map_range(
1230 isl_basic_map_copy(map->p[i])));
1231 qc = q_closure(isl_space_copy(dim), isl_set_copy(C),
1232 map->p[i], &exact_i);
1233 if (!qc)
1234 goto error;
1235 if (!exact_i) {
1236 isl_map_free(qc);
1237 continue;
1239 spurious = has_spurious_elements(qc, dom[i], ran[i]);
1240 if (spurious) {
1241 isl_map_free(qc);
1242 if (spurious < 0)
1243 goto error;
1244 continue;
1246 qc = isl_map_project_out(qc, isl_dim_in, d, 1);
1247 qc = isl_map_project_out(qc, isl_dim_out, d, 1);
1248 qc = isl_map_compute_divs(qc);
1249 for (j = 0; j < map->n; ++j)
1250 left[j] = right[j] = 1;
1251 qc = compose(map, i, qc, left, right);
1252 if (!qc)
1253 goto error;
1254 if (qc->n >= map->n) {
1255 isl_map_free(qc);
1256 continue;
1258 *res = compute_incremental(isl_space_copy(dim), map, i, qc,
1259 left, right, &exact_i);
1260 if (!*res)
1261 goto error;
1262 if (exact_i)
1263 break;
1264 isl_map_free(*res);
1265 *res = NULL;
1268 isl_set_free(C);
1270 return *res != NULL;
1271 error:
1272 isl_set_free(C);
1273 return -1;
1276 /* Try and compute the transitive closure of "map" as
1278 * map^+ = map_i^+ \cup
1279 * \bigcup_j ((map_i^+ \cup Id_C)^+ \circ map_j \circ (map_i^+ \cup Id_C))^+
1281 * with C either the simple hull of the domain and range of the entire
1282 * map or the simple hull of domain and range of map_i.
1284 static __isl_give isl_map *incremental_closure(__isl_take isl_space *dim,
1285 __isl_keep isl_map *map, int *exact, int project)
1287 int i;
1288 isl_set **dom = NULL;
1289 isl_set **ran = NULL;
1290 int *left = NULL;
1291 int *right = NULL;
1292 isl_set *C;
1293 unsigned d;
1294 isl_map *res = NULL;
1296 if (!project)
1297 return construct_projected_component(dim, map, exact, project);
1299 if (!map)
1300 goto error;
1301 if (map->n <= 1)
1302 return construct_projected_component(dim, map, exact, project);
1304 d = isl_map_dim(map, isl_dim_in);
1306 dom = isl_calloc_array(map->ctx, isl_set *, map->n);
1307 ran = isl_calloc_array(map->ctx, isl_set *, map->n);
1308 left = isl_calloc_array(map->ctx, int, map->n);
1309 right = isl_calloc_array(map->ctx, int, map->n);
1310 if (!ran || !dom || !left || !right)
1311 goto error;
1313 if (incemental_on_entire_domain(dim, map, dom, ran, left, right, &res) < 0)
1314 goto error;
1316 for (i = 0; !res && i < map->n; ++i) {
1317 isl_map *qc;
1318 int exact_i, spurious, comp;
1319 if (!dom[i])
1320 dom[i] = isl_set_from_basic_set(
1321 isl_basic_map_domain(
1322 isl_basic_map_copy(map->p[i])));
1323 if (!dom[i])
1324 goto error;
1325 if (!ran[i])
1326 ran[i] = isl_set_from_basic_set(
1327 isl_basic_map_range(
1328 isl_basic_map_copy(map->p[i])));
1329 if (!ran[i])
1330 goto error;
1331 C = isl_set_union(isl_set_copy(dom[i]),
1332 isl_set_copy(ran[i]));
1333 C = isl_set_from_basic_set(isl_set_simple_hull(C));
1334 if (!C)
1335 goto error;
1336 if (C->n != 1) {
1337 isl_set_free(C);
1338 continue;
1340 comp = composability(C, i, dom, ran, left, right, map);
1341 if (!comp || comp < 0) {
1342 isl_set_free(C);
1343 if (comp < 0)
1344 goto error;
1345 continue;
1347 qc = q_closure(isl_space_copy(dim), C, map->p[i], &exact_i);
1348 if (!qc)
1349 goto error;
1350 if (!exact_i) {
1351 isl_map_free(qc);
1352 continue;
1354 spurious = has_spurious_elements(qc, dom[i], ran[i]);
1355 if (spurious) {
1356 isl_map_free(qc);
1357 if (spurious < 0)
1358 goto error;
1359 continue;
1361 qc = isl_map_project_out(qc, isl_dim_in, d, 1);
1362 qc = isl_map_project_out(qc, isl_dim_out, d, 1);
1363 qc = isl_map_compute_divs(qc);
1364 qc = compose(map, i, qc, (comp & LEFT) ? left : NULL,
1365 (comp & RIGHT) ? right : NULL);
1366 if (!qc)
1367 goto error;
1368 if (qc->n >= map->n) {
1369 isl_map_free(qc);
1370 continue;
1372 res = compute_incremental(isl_space_copy(dim), map, i, qc,
1373 (comp & LEFT) ? left : NULL,
1374 (comp & RIGHT) ? right : NULL, &exact_i);
1375 if (!res)
1376 goto error;
1377 if (exact_i)
1378 break;
1379 isl_map_free(res);
1380 res = NULL;
1383 for (i = 0; i < map->n; ++i) {
1384 isl_set_free(dom[i]);
1385 isl_set_free(ran[i]);
1387 free(dom);
1388 free(ran);
1389 free(left);
1390 free(right);
1392 if (res) {
1393 isl_space_free(dim);
1394 return res;
1397 return construct_projected_component(dim, map, exact, project);
1398 error:
1399 if (dom)
1400 for (i = 0; i < map->n; ++i)
1401 isl_set_free(dom[i]);
1402 free(dom);
1403 if (ran)
1404 for (i = 0; i < map->n; ++i)
1405 isl_set_free(ran[i]);
1406 free(ran);
1407 free(left);
1408 free(right);
1409 isl_space_free(dim);
1410 return NULL;
1413 /* Given an array of sets "set", add "dom" at position "pos"
1414 * and search for elements at earlier positions that overlap with "dom".
1415 * If any can be found, then merge all of them, together with "dom", into
1416 * a single set and assign the union to the first in the array,
1417 * which becomes the new group leader for all groups involved in the merge.
1418 * During the search, we only consider group leaders, i.e., those with
1419 * group[i] = i, as the other sets have already been combined
1420 * with one of the group leaders.
1422 static int merge(isl_set **set, int *group, __isl_take isl_set *dom, int pos)
1424 int i;
1426 group[pos] = pos;
1427 set[pos] = isl_set_copy(dom);
1429 for (i = pos - 1; i >= 0; --i) {
1430 int o;
1432 if (group[i] != i)
1433 continue;
1435 o = isl_set_overlaps(set[i], dom);
1436 if (o < 0)
1437 goto error;
1438 if (!o)
1439 continue;
1441 set[i] = isl_set_union(set[i], set[group[pos]]);
1442 set[group[pos]] = NULL;
1443 if (!set[i])
1444 goto error;
1445 group[group[pos]] = i;
1446 group[pos] = i;
1449 isl_set_free(dom);
1450 return 0;
1451 error:
1452 isl_set_free(dom);
1453 return -1;
1456 /* Replace each entry in the n by n grid of maps by the cross product
1457 * with the relation { [i] -> [i + 1] }.
1459 static int add_length(__isl_keep isl_map *map, isl_map ***grid, int n)
1461 int i, j, k;
1462 isl_space *dim;
1463 isl_basic_map *bstep;
1464 isl_map *step;
1465 unsigned nparam;
1467 if (!map)
1468 return -1;
1470 dim = isl_map_get_space(map);
1471 nparam = isl_space_dim(dim, isl_dim_param);
1472 dim = isl_space_drop_dims(dim, isl_dim_in, 0, isl_space_dim(dim, isl_dim_in));
1473 dim = isl_space_drop_dims(dim, isl_dim_out, 0, isl_space_dim(dim, isl_dim_out));
1474 dim = isl_space_add_dims(dim, isl_dim_in, 1);
1475 dim = isl_space_add_dims(dim, isl_dim_out, 1);
1476 bstep = isl_basic_map_alloc_space(dim, 0, 1, 0);
1477 k = isl_basic_map_alloc_equality(bstep);
1478 if (k < 0) {
1479 isl_basic_map_free(bstep);
1480 return -1;
1482 isl_seq_clr(bstep->eq[k], 1 + isl_basic_map_total_dim(bstep));
1483 isl_int_set_si(bstep->eq[k][0], 1);
1484 isl_int_set_si(bstep->eq[k][1 + nparam], 1);
1485 isl_int_set_si(bstep->eq[k][1 + nparam + 1], -1);
1486 bstep = isl_basic_map_finalize(bstep);
1487 step = isl_map_from_basic_map(bstep);
1489 for (i = 0; i < n; ++i)
1490 for (j = 0; j < n; ++j)
1491 grid[i][j] = isl_map_product(grid[i][j],
1492 isl_map_copy(step));
1494 isl_map_free(step);
1496 return 0;
1499 /* The core of the Floyd-Warshall algorithm.
1500 * Updates the given n x x matrix of relations in place.
1502 * The algorithm iterates over all vertices. In each step, the whole
1503 * matrix is updated to include all paths that go to the current vertex,
1504 * possibly stay there a while (including passing through earlier vertices)
1505 * and then come back. At the start of each iteration, the diagonal
1506 * element corresponding to the current vertex is replaced by its
1507 * transitive closure to account for all indirect paths that stay
1508 * in the current vertex.
1510 static void floyd_warshall_iterate(isl_map ***grid, int n, int *exact)
1512 int r, p, q;
1514 for (r = 0; r < n; ++r) {
1515 int r_exact;
1516 grid[r][r] = isl_map_transitive_closure(grid[r][r],
1517 (exact && *exact) ? &r_exact : NULL);
1518 if (exact && *exact && !r_exact)
1519 *exact = 0;
1521 for (p = 0; p < n; ++p)
1522 for (q = 0; q < n; ++q) {
1523 isl_map *loop;
1524 if (p == r && q == r)
1525 continue;
1526 loop = isl_map_apply_range(
1527 isl_map_copy(grid[p][r]),
1528 isl_map_copy(grid[r][q]));
1529 grid[p][q] = isl_map_union(grid[p][q], loop);
1530 loop = isl_map_apply_range(
1531 isl_map_copy(grid[p][r]),
1532 isl_map_apply_range(
1533 isl_map_copy(grid[r][r]),
1534 isl_map_copy(grid[r][q])));
1535 grid[p][q] = isl_map_union(grid[p][q], loop);
1536 grid[p][q] = isl_map_coalesce(grid[p][q]);
1541 /* Given a partition of the domains and ranges of the basic maps in "map",
1542 * apply the Floyd-Warshall algorithm with the elements in the partition
1543 * as vertices.
1545 * In particular, there are "n" elements in the partition and "group" is
1546 * an array of length 2 * map->n with entries in [0,n-1].
1548 * We first construct a matrix of relations based on the partition information,
1549 * apply Floyd-Warshall on this matrix of relations and then take the
1550 * union of all entries in the matrix as the final result.
1552 * If we are actually computing the power instead of the transitive closure,
1553 * i.e., when "project" is not set, then the result should have the
1554 * path lengths encoded as the difference between an extra pair of
1555 * coordinates. We therefore apply the nested transitive closures
1556 * to relations that include these lengths. In particular, we replace
1557 * the input relation by the cross product with the unit length relation
1558 * { [i] -> [i + 1] }.
1560 static __isl_give isl_map *floyd_warshall_with_groups(__isl_take isl_space *dim,
1561 __isl_keep isl_map *map, int *exact, int project, int *group, int n)
1563 int i, j, k;
1564 isl_map ***grid = NULL;
1565 isl_map *app;
1567 if (!map)
1568 goto error;
1570 if (n == 1) {
1571 free(group);
1572 return incremental_closure(dim, map, exact, project);
1575 grid = isl_calloc_array(map->ctx, isl_map **, n);
1576 if (!grid)
1577 goto error;
1578 for (i = 0; i < n; ++i) {
1579 grid[i] = isl_calloc_array(map->ctx, isl_map *, n);
1580 if (!grid[i])
1581 goto error;
1582 for (j = 0; j < n; ++j)
1583 grid[i][j] = isl_map_empty(isl_map_get_space(map));
1586 for (k = 0; k < map->n; ++k) {
1587 i = group[2 * k];
1588 j = group[2 * k + 1];
1589 grid[i][j] = isl_map_union(grid[i][j],
1590 isl_map_from_basic_map(
1591 isl_basic_map_copy(map->p[k])));
1594 if (!project && add_length(map, grid, n) < 0)
1595 goto error;
1597 floyd_warshall_iterate(grid, n, exact);
1599 app = isl_map_empty(isl_map_get_space(map));
1601 for (i = 0; i < n; ++i) {
1602 for (j = 0; j < n; ++j)
1603 app = isl_map_union(app, grid[i][j]);
1604 free(grid[i]);
1606 free(grid);
1608 free(group);
1609 isl_space_free(dim);
1611 return app;
1612 error:
1613 if (grid)
1614 for (i = 0; i < n; ++i) {
1615 if (!grid[i])
1616 continue;
1617 for (j = 0; j < n; ++j)
1618 isl_map_free(grid[i][j]);
1619 free(grid[i]);
1621 free(grid);
1622 free(group);
1623 isl_space_free(dim);
1624 return NULL;
1627 /* Partition the domains and ranges of the n basic relations in list
1628 * into disjoint cells.
1630 * To find the partition, we simply consider all of the domains
1631 * and ranges in turn and combine those that overlap.
1632 * "set" contains the partition elements and "group" indicates
1633 * to which partition element a given domain or range belongs.
1634 * The domain of basic map i corresponds to element 2 * i in these arrays,
1635 * while the domain corresponds to element 2 * i + 1.
1636 * During the construction group[k] is either equal to k,
1637 * in which case set[k] contains the union of all the domains and
1638 * ranges in the corresponding group, or is equal to some l < k,
1639 * with l another domain or range in the same group.
1641 static int *setup_groups(isl_ctx *ctx, __isl_keep isl_basic_map **list, int n,
1642 isl_set ***set, int *n_group)
1644 int i;
1645 int *group = NULL;
1646 int g;
1648 *set = isl_calloc_array(ctx, isl_set *, 2 * n);
1649 group = isl_alloc_array(ctx, int, 2 * n);
1651 if (!*set || !group)
1652 goto error;
1654 for (i = 0; i < n; ++i) {
1655 isl_set *dom;
1656 dom = isl_set_from_basic_set(isl_basic_map_domain(
1657 isl_basic_map_copy(list[i])));
1658 if (merge(*set, group, dom, 2 * i) < 0)
1659 goto error;
1660 dom = isl_set_from_basic_set(isl_basic_map_range(
1661 isl_basic_map_copy(list[i])));
1662 if (merge(*set, group, dom, 2 * i + 1) < 0)
1663 goto error;
1666 g = 0;
1667 for (i = 0; i < 2 * n; ++i)
1668 if (group[i] == i) {
1669 if (g != i) {
1670 (*set)[g] = (*set)[i];
1671 (*set)[i] = NULL;
1673 group[i] = g++;
1674 } else
1675 group[i] = group[group[i]];
1677 *n_group = g;
1679 return group;
1680 error:
1681 if (*set) {
1682 for (i = 0; i < 2 * n; ++i)
1683 isl_set_free((*set)[i]);
1684 free(*set);
1685 *set = NULL;
1687 free(group);
1688 return NULL;
1691 /* Check if the domains and ranges of the basic maps in "map" can
1692 * be partitioned, and if so, apply Floyd-Warshall on the elements
1693 * of the partition. Note that we also apply this algorithm
1694 * if we want to compute the power, i.e., when "project" is not set.
1695 * However, the results are unlikely to be exact since the recursive
1696 * calls inside the Floyd-Warshall algorithm typically result in
1697 * non-linear path lengths quite quickly.
1699 static __isl_give isl_map *floyd_warshall(__isl_take isl_space *dim,
1700 __isl_keep isl_map *map, int *exact, int project)
1702 int i;
1703 isl_set **set = NULL;
1704 int *group = NULL;
1705 int n;
1707 if (!map)
1708 goto error;
1709 if (map->n <= 1)
1710 return incremental_closure(dim, map, exact, project);
1712 group = setup_groups(map->ctx, map->p, map->n, &set, &n);
1713 if (!group)
1714 goto error;
1716 for (i = 0; i < 2 * map->n; ++i)
1717 isl_set_free(set[i]);
1719 free(set);
1721 return floyd_warshall_with_groups(dim, map, exact, project, group, n);
1722 error:
1723 isl_space_free(dim);
1724 return NULL;
1727 /* Structure for representing the nodes of the graph of which
1728 * strongly connected components are being computed.
1730 * list contains the actual nodes
1731 * check_closed is set if we may have used the fact that
1732 * a pair of basic maps can be interchanged
1734 struct isl_tc_follows_data {
1735 isl_basic_map **list;
1736 int check_closed;
1739 /* Check whether in the computation of the transitive closure
1740 * "list[i]" (R_1) should follow (or be part of the same component as)
1741 * "list[j]" (R_2).
1743 * That is check whether
1745 * R_1 \circ R_2
1747 * is a subset of
1749 * R_2 \circ R_1
1751 * If so, then there is no reason for R_1 to immediately follow R_2
1752 * in any path.
1754 * *check_closed is set if the subset relation holds while
1755 * R_1 \circ R_2 is not empty.
1757 static int basic_map_follows(int i, int j, void *user)
1759 struct isl_tc_follows_data *data = user;
1760 struct isl_map *map12 = NULL;
1761 struct isl_map *map21 = NULL;
1762 int subset;
1764 if (!isl_space_tuple_is_equal(data->list[i]->dim, isl_dim_in,
1765 data->list[j]->dim, isl_dim_out))
1766 return 0;
1768 map21 = isl_map_from_basic_map(
1769 isl_basic_map_apply_range(
1770 isl_basic_map_copy(data->list[j]),
1771 isl_basic_map_copy(data->list[i])));
1772 subset = isl_map_is_empty(map21);
1773 if (subset < 0)
1774 goto error;
1775 if (subset) {
1776 isl_map_free(map21);
1777 return 0;
1780 if (!isl_space_tuple_is_equal(data->list[i]->dim, isl_dim_in,
1781 data->list[i]->dim, isl_dim_out) ||
1782 !isl_space_tuple_is_equal(data->list[j]->dim, isl_dim_in,
1783 data->list[j]->dim, isl_dim_out)) {
1784 isl_map_free(map21);
1785 return 1;
1788 map12 = isl_map_from_basic_map(
1789 isl_basic_map_apply_range(
1790 isl_basic_map_copy(data->list[i]),
1791 isl_basic_map_copy(data->list[j])));
1793 subset = isl_map_is_subset(map21, map12);
1795 isl_map_free(map12);
1796 isl_map_free(map21);
1798 if (subset)
1799 data->check_closed = 1;
1801 return subset < 0 ? -1 : !subset;
1802 error:
1803 isl_map_free(map21);
1804 return -1;
1807 /* Given a union of basic maps R = \cup_i R_i \subseteq D \times D
1808 * and a dimension specification (Z^{n+1} -> Z^{n+1}),
1809 * construct a map that is an overapproximation of the map
1810 * that takes an element from the dom R \times Z to an
1811 * element from ran R \times Z, such that the first n coordinates of the
1812 * difference between them is a sum of differences between images
1813 * and pre-images in one of the R_i and such that the last coordinate
1814 * is equal to the number of steps taken.
1815 * If "project" is set, then these final coordinates are not included,
1816 * i.e., a relation of type Z^n -> Z^n is returned.
1817 * That is, let
1819 * \Delta_i = { y - x | (x, y) in R_i }
1821 * then the constructed map is an overapproximation of
1823 * { (x) -> (x + d) | \exists k_i >= 0, \delta_i \in \Delta_i :
1824 * d = (\sum_i k_i \delta_i, \sum_i k_i) and
1825 * x in dom R and x + d in ran R }
1827 * or
1829 * { (x) -> (x + d) | \exists k_i >= 0, \delta_i \in \Delta_i :
1830 * d = (\sum_i k_i \delta_i) and
1831 * x in dom R and x + d in ran R }
1833 * if "project" is set.
1835 * We first split the map into strongly connected components, perform
1836 * the above on each component and then join the results in the correct
1837 * order, at each join also taking in the union of both arguments
1838 * to allow for paths that do not go through one of the two arguments.
1840 static __isl_give isl_map *construct_power_components(__isl_take isl_space *dim,
1841 __isl_keep isl_map *map, int *exact, int project)
1843 int i, n, c;
1844 struct isl_map *path = NULL;
1845 struct isl_tc_follows_data data;
1846 struct isl_tarjan_graph *g = NULL;
1847 int *orig_exact;
1848 int local_exact;
1850 if (!map)
1851 goto error;
1852 if (map->n <= 1)
1853 return floyd_warshall(dim, map, exact, project);
1855 data.list = map->p;
1856 data.check_closed = 0;
1857 g = isl_tarjan_graph_init(map->ctx, map->n, &basic_map_follows, &data);
1858 if (!g)
1859 goto error;
1861 orig_exact = exact;
1862 if (data.check_closed && !exact)
1863 exact = &local_exact;
1865 c = 0;
1866 i = 0;
1867 n = map->n;
1868 if (project)
1869 path = isl_map_empty(isl_map_get_space(map));
1870 else
1871 path = isl_map_empty(isl_space_copy(dim));
1872 path = anonymize(path);
1873 while (n) {
1874 struct isl_map *comp;
1875 isl_map *path_comp, *path_comb;
1876 comp = isl_map_alloc_space(isl_map_get_space(map), n, 0);
1877 while (g->order[i] != -1) {
1878 comp = isl_map_add_basic_map(comp,
1879 isl_basic_map_copy(map->p[g->order[i]]));
1880 --n;
1881 ++i;
1883 path_comp = floyd_warshall(isl_space_copy(dim),
1884 comp, exact, project);
1885 path_comp = anonymize(path_comp);
1886 path_comb = isl_map_apply_range(isl_map_copy(path),
1887 isl_map_copy(path_comp));
1888 path = isl_map_union(path, path_comp);
1889 path = isl_map_union(path, path_comb);
1890 isl_map_free(comp);
1891 ++i;
1892 ++c;
1895 if (c > 1 && data.check_closed && !*exact) {
1896 int closed;
1898 closed = isl_map_is_transitively_closed(path);
1899 if (closed < 0)
1900 goto error;
1901 if (!closed) {
1902 isl_tarjan_graph_free(g);
1903 isl_map_free(path);
1904 return floyd_warshall(dim, map, orig_exact, project);
1908 isl_tarjan_graph_free(g);
1909 isl_space_free(dim);
1911 return path;
1912 error:
1913 isl_tarjan_graph_free(g);
1914 isl_space_free(dim);
1915 isl_map_free(path);
1916 return NULL;
1919 /* Given a union of basic maps R = \cup_i R_i \subseteq D \times D,
1920 * construct a map that is an overapproximation of the map
1921 * that takes an element from the space D to another
1922 * element from the same space, such that the difference between
1923 * them is a strictly positive sum of differences between images
1924 * and pre-images in one of the R_i.
1925 * The number of differences in the sum is equated to parameter "param".
1926 * That is, let
1928 * \Delta_i = { y - x | (x, y) in R_i }
1930 * then the constructed map is an overapproximation of
1932 * { (x) -> (x + d) | \exists k_i >= 0, \delta_i \in \Delta_i :
1933 * d = \sum_i k_i \delta_i and k = \sum_i k_i > 0 }
1934 * or
1936 * { (x) -> (x + d) | \exists k_i >= 0, \delta_i \in \Delta_i :
1937 * d = \sum_i k_i \delta_i and \sum_i k_i > 0 }
1939 * if "project" is set.
1941 * If "project" is not set, then
1942 * we construct an extended mapping with an extra coordinate
1943 * that indicates the number of steps taken. In particular,
1944 * the difference in the last coordinate is equal to the number
1945 * of steps taken to move from a domain element to the corresponding
1946 * image element(s).
1948 static __isl_give isl_map *construct_power(__isl_keep isl_map *map,
1949 int *exact, int project)
1951 struct isl_map *app = NULL;
1952 isl_space *dim = NULL;
1953 unsigned d;
1955 if (!map)
1956 return NULL;
1958 dim = isl_map_get_space(map);
1960 d = isl_space_dim(dim, isl_dim_in);
1961 dim = isl_space_add_dims(dim, isl_dim_in, 1);
1962 dim = isl_space_add_dims(dim, isl_dim_out, 1);
1964 app = construct_power_components(isl_space_copy(dim), map,
1965 exact, project);
1967 isl_space_free(dim);
1969 return app;
1972 /* Compute the positive powers of "map", or an overapproximation.
1973 * If the result is exact, then *exact is set to 1.
1975 * If project is set, then we are actually interested in the transitive
1976 * closure, so we can use a more relaxed exactness check.
1977 * The lengths of the paths are also projected out instead of being
1978 * encoded as the difference between an extra pair of final coordinates.
1980 static __isl_give isl_map *map_power(__isl_take isl_map *map,
1981 int *exact, int project)
1983 struct isl_map *app = NULL;
1985 if (exact)
1986 *exact = 1;
1988 if (!map)
1989 return NULL;
1991 isl_assert(map->ctx,
1992 isl_map_dim(map, isl_dim_in) == isl_map_dim(map, isl_dim_out),
1993 goto error);
1995 app = construct_power(map, exact, project);
1997 isl_map_free(map);
1998 return app;
1999 error:
2000 isl_map_free(map);
2001 isl_map_free(app);
2002 return NULL;
2005 /* Compute the positive powers of "map", or an overapproximation.
2006 * The result maps the exponent to a nested copy of the corresponding power.
2007 * If the result is exact, then *exact is set to 1.
2008 * map_power constructs an extended relation with the path lengths
2009 * encoded as the difference between the final coordinates.
2010 * In the final step, this difference is equated to an extra parameter
2011 * and made positive. The extra coordinates are subsequently projected out
2012 * and the parameter is turned into the domain of the result.
2014 __isl_give isl_map *isl_map_power(__isl_take isl_map *map, int *exact)
2016 isl_space *target_dim;
2017 isl_space *dim;
2018 isl_map *diff;
2019 unsigned d;
2020 unsigned param;
2022 if (!map)
2023 return NULL;
2025 d = isl_map_dim(map, isl_dim_in);
2026 param = isl_map_dim(map, isl_dim_param);
2028 map = isl_map_compute_divs(map);
2029 map = isl_map_coalesce(map);
2031 if (isl_map_plain_is_empty(map)) {
2032 map = isl_map_from_range(isl_map_wrap(map));
2033 map = isl_map_add_dims(map, isl_dim_in, 1);
2034 map = isl_map_set_dim_name(map, isl_dim_in, 0, "k");
2035 return map;
2038 target_dim = isl_map_get_space(map);
2039 target_dim = isl_space_from_range(isl_space_wrap(target_dim));
2040 target_dim = isl_space_add_dims(target_dim, isl_dim_in, 1);
2041 target_dim = isl_space_set_dim_name(target_dim, isl_dim_in, 0, "k");
2043 map = map_power(map, exact, 0);
2045 map = isl_map_add_dims(map, isl_dim_param, 1);
2046 dim = isl_map_get_space(map);
2047 diff = equate_parameter_to_length(dim, param);
2048 map = isl_map_intersect(map, diff);
2049 map = isl_map_project_out(map, isl_dim_in, d, 1);
2050 map = isl_map_project_out(map, isl_dim_out, d, 1);
2051 map = isl_map_from_range(isl_map_wrap(map));
2052 map = isl_map_move_dims(map, isl_dim_in, 0, isl_dim_param, param, 1);
2054 map = isl_map_reset_space(map, target_dim);
2056 return map;
2059 /* Compute a relation that maps each element in the range of the input
2060 * relation to the lengths of all paths composed of edges in the input
2061 * relation that end up in the given range element.
2062 * The result may be an overapproximation, in which case *exact is set to 0.
2063 * The resulting relation is very similar to the power relation.
2064 * The difference are that the domain has been projected out, the
2065 * range has become the domain and the exponent is the range instead
2066 * of a parameter.
2068 __isl_give isl_map *isl_map_reaching_path_lengths(__isl_take isl_map *map,
2069 int *exact)
2071 isl_space *dim;
2072 isl_map *diff;
2073 unsigned d;
2074 unsigned param;
2076 if (!map)
2077 return NULL;
2079 d = isl_map_dim(map, isl_dim_in);
2080 param = isl_map_dim(map, isl_dim_param);
2082 map = isl_map_compute_divs(map);
2083 map = isl_map_coalesce(map);
2085 if (isl_map_plain_is_empty(map)) {
2086 if (exact)
2087 *exact = 1;
2088 map = isl_map_project_out(map, isl_dim_out, 0, d);
2089 map = isl_map_add_dims(map, isl_dim_out, 1);
2090 return map;
2093 map = map_power(map, exact, 0);
2095 map = isl_map_add_dims(map, isl_dim_param, 1);
2096 dim = isl_map_get_space(map);
2097 diff = equate_parameter_to_length(dim, param);
2098 map = isl_map_intersect(map, diff);
2099 map = isl_map_project_out(map, isl_dim_in, 0, d + 1);
2100 map = isl_map_project_out(map, isl_dim_out, d, 1);
2101 map = isl_map_reverse(map);
2102 map = isl_map_move_dims(map, isl_dim_out, 0, isl_dim_param, param, 1);
2104 return map;
2107 /* Check whether equality i of bset is a pure stride constraint
2108 * on a single dimensions, i.e., of the form
2110 * v = k e
2112 * with k a constant and e an existentially quantified variable.
2114 static int is_eq_stride(__isl_keep isl_basic_set *bset, int i)
2116 unsigned nparam;
2117 unsigned d;
2118 unsigned n_div;
2119 int pos1;
2120 int pos2;
2122 if (!bset)
2123 return -1;
2125 if (!isl_int_is_zero(bset->eq[i][0]))
2126 return 0;
2128 nparam = isl_basic_set_dim(bset, isl_dim_param);
2129 d = isl_basic_set_dim(bset, isl_dim_set);
2130 n_div = isl_basic_set_dim(bset, isl_dim_div);
2132 if (isl_seq_first_non_zero(bset->eq[i] + 1, nparam) != -1)
2133 return 0;
2134 pos1 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam, d);
2135 if (pos1 == -1)
2136 return 0;
2137 if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + pos1 + 1,
2138 d - pos1 - 1) != -1)
2139 return 0;
2141 pos2 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d, n_div);
2142 if (pos2 == -1)
2143 return 0;
2144 if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d + pos2 + 1,
2145 n_div - pos2 - 1) != -1)
2146 return 0;
2147 if (!isl_int_is_one(bset->eq[i][1 + nparam + pos1]) &&
2148 !isl_int_is_negone(bset->eq[i][1 + nparam + pos1]))
2149 return 0;
2151 return 1;
2154 /* Given a map, compute the smallest superset of this map that is of the form
2156 * { i -> j : L <= j - i <= U and exists a_p: j_p - i_p = M_p a_p }
2158 * (where p ranges over the (non-parametric) dimensions),
2159 * compute the transitive closure of this map, i.e.,
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 * and intersect domain and range of this transitive closure with
2165 * the given domain and range.
2167 * If with_id is set, then try to include as much of the identity mapping
2168 * as possible, by computing
2170 * { i -> j : exists k >= 0:
2171 * k L <= j - i <= k U and exists a: j_p - i_p = M_p a_p }
2173 * instead (i.e., allow k = 0).
2175 * In practice, we compute the difference set
2177 * delta = { j - i | i -> j in map },
2179 * look for stride constraint on the individual dimensions and compute
2180 * (constant) lower and upper bounds for each individual dimension,
2181 * adding a constraint for each bound not equal to infinity.
2183 static __isl_give isl_map *box_closure_on_domain(__isl_take isl_map *map,
2184 __isl_take isl_set *dom, __isl_take isl_set *ran, int with_id)
2186 int i;
2187 int k;
2188 unsigned d;
2189 unsigned nparam;
2190 unsigned total;
2191 isl_space *dim;
2192 isl_set *delta;
2193 isl_map *app = NULL;
2194 isl_basic_set *aff = NULL;
2195 isl_basic_map *bmap = NULL;
2196 isl_vec *obj = NULL;
2197 isl_int opt;
2199 isl_int_init(opt);
2201 delta = isl_map_deltas(isl_map_copy(map));
2203 aff = isl_set_affine_hull(isl_set_copy(delta));
2204 if (!aff)
2205 goto error;
2206 dim = isl_map_get_space(map);
2207 d = isl_space_dim(dim, isl_dim_in);
2208 nparam = isl_space_dim(dim, isl_dim_param);
2209 total = isl_space_dim(dim, isl_dim_all);
2210 bmap = isl_basic_map_alloc_space(dim,
2211 aff->n_div + 1, aff->n_div, 2 * d + 1);
2212 for (i = 0; i < aff->n_div + 1; ++i) {
2213 k = isl_basic_map_alloc_div(bmap);
2214 if (k < 0)
2215 goto error;
2216 isl_int_set_si(bmap->div[k][0], 0);
2218 for (i = 0; i < aff->n_eq; ++i) {
2219 if (!is_eq_stride(aff, i))
2220 continue;
2221 k = isl_basic_map_alloc_equality(bmap);
2222 if (k < 0)
2223 goto error;
2224 isl_seq_clr(bmap->eq[k], 1 + nparam);
2225 isl_seq_cpy(bmap->eq[k] + 1 + nparam + d,
2226 aff->eq[i] + 1 + nparam, d);
2227 isl_seq_neg(bmap->eq[k] + 1 + nparam,
2228 aff->eq[i] + 1 + nparam, d);
2229 isl_seq_cpy(bmap->eq[k] + 1 + nparam + 2 * d,
2230 aff->eq[i] + 1 + nparam + d, aff->n_div);
2231 isl_int_set_si(bmap->eq[k][1 + total + aff->n_div], 0);
2233 obj = isl_vec_alloc(map->ctx, 1 + nparam + d);
2234 if (!obj)
2235 goto error;
2236 isl_seq_clr(obj->el, 1 + nparam + d);
2237 for (i = 0; i < d; ++ i) {
2238 enum isl_lp_result res;
2240 isl_int_set_si(obj->el[1 + nparam + i], 1);
2242 res = isl_set_solve_lp(delta, 0, obj->el, map->ctx->one, &opt,
2243 NULL, NULL);
2244 if (res == isl_lp_error)
2245 goto error;
2246 if (res == isl_lp_ok) {
2247 k = isl_basic_map_alloc_inequality(bmap);
2248 if (k < 0)
2249 goto error;
2250 isl_seq_clr(bmap->ineq[k],
2251 1 + nparam + 2 * d + bmap->n_div);
2252 isl_int_set_si(bmap->ineq[k][1 + nparam + i], -1);
2253 isl_int_set_si(bmap->ineq[k][1 + nparam + d + i], 1);
2254 isl_int_neg(bmap->ineq[k][1 + nparam + 2 * d + aff->n_div], opt);
2257 res = isl_set_solve_lp(delta, 1, obj->el, map->ctx->one, &opt,
2258 NULL, NULL);
2259 if (res == isl_lp_error)
2260 goto error;
2261 if (res == isl_lp_ok) {
2262 k = isl_basic_map_alloc_inequality(bmap);
2263 if (k < 0)
2264 goto error;
2265 isl_seq_clr(bmap->ineq[k],
2266 1 + nparam + 2 * d + bmap->n_div);
2267 isl_int_set_si(bmap->ineq[k][1 + nparam + i], 1);
2268 isl_int_set_si(bmap->ineq[k][1 + nparam + d + i], -1);
2269 isl_int_set(bmap->ineq[k][1 + nparam + 2 * d + aff->n_div], opt);
2272 isl_int_set_si(obj->el[1 + nparam + i], 0);
2274 k = isl_basic_map_alloc_inequality(bmap);
2275 if (k < 0)
2276 goto error;
2277 isl_seq_clr(bmap->ineq[k],
2278 1 + nparam + 2 * d + bmap->n_div);
2279 if (!with_id)
2280 isl_int_set_si(bmap->ineq[k][0], -1);
2281 isl_int_set_si(bmap->ineq[k][1 + nparam + 2 * d + aff->n_div], 1);
2283 app = isl_map_from_domain_and_range(dom, ran);
2285 isl_vec_free(obj);
2286 isl_basic_set_free(aff);
2287 isl_map_free(map);
2288 bmap = isl_basic_map_finalize(bmap);
2289 isl_set_free(delta);
2290 isl_int_clear(opt);
2292 map = isl_map_from_basic_map(bmap);
2293 map = isl_map_intersect(map, app);
2295 return map;
2296 error:
2297 isl_vec_free(obj);
2298 isl_basic_map_free(bmap);
2299 isl_basic_set_free(aff);
2300 isl_set_free(dom);
2301 isl_set_free(ran);
2302 isl_map_free(map);
2303 isl_set_free(delta);
2304 isl_int_clear(opt);
2305 return NULL;
2308 /* Given a map, compute the smallest superset of this map that is of the form
2310 * { i -> j : L <= j - i <= U and exists a_p: j_p - i_p = M_p a_p }
2312 * (where p ranges over the (non-parametric) dimensions),
2313 * compute the transitive closure of this map, i.e.,
2315 * { i -> j : exists k > 0:
2316 * k L <= j - i <= k U and exists a: j_p - i_p = M_p a_p }
2318 * and intersect domain and range of this transitive closure with
2319 * domain and range of the original map.
2321 static __isl_give isl_map *box_closure(__isl_take isl_map *map)
2323 isl_set *domain;
2324 isl_set *range;
2326 domain = isl_map_domain(isl_map_copy(map));
2327 domain = isl_set_coalesce(domain);
2328 range = isl_map_range(isl_map_copy(map));
2329 range = isl_set_coalesce(range);
2331 return box_closure_on_domain(map, domain, range, 0);
2334 /* Given a map, compute the smallest superset of this map that is of the form
2336 * { i -> j : L <= j - i <= U and exists a_p: j_p - i_p = M_p a_p }
2338 * (where p ranges over the (non-parametric) dimensions),
2339 * compute the transitive and partially reflexive closure of this map, i.e.,
2341 * { i -> j : exists k >= 0:
2342 * k L <= j - i <= k U and exists a: j_p - i_p = M_p a_p }
2344 * and intersect domain and range of this transitive closure with
2345 * the given domain.
2347 static __isl_give isl_map *box_closure_with_identity(__isl_take isl_map *map,
2348 __isl_take isl_set *dom)
2350 return box_closure_on_domain(map, dom, isl_set_copy(dom), 1);
2353 /* Check whether app is the transitive closure of map.
2354 * In particular, check that app is acyclic and, if so,
2355 * check that
2357 * app \subset (map \cup (map \circ app))
2359 static int check_exactness_omega(__isl_keep isl_map *map,
2360 __isl_keep isl_map *app)
2362 isl_set *delta;
2363 int i;
2364 int is_empty, is_exact;
2365 unsigned d;
2366 isl_map *test;
2368 delta = isl_map_deltas(isl_map_copy(app));
2369 d = isl_set_dim(delta, isl_dim_set);
2370 for (i = 0; i < d; ++i)
2371 delta = isl_set_fix_si(delta, isl_dim_set, i, 0);
2372 is_empty = isl_set_is_empty(delta);
2373 isl_set_free(delta);
2374 if (is_empty < 0)
2375 return -1;
2376 if (!is_empty)
2377 return 0;
2379 test = isl_map_apply_range(isl_map_copy(app), isl_map_copy(map));
2380 test = isl_map_union(test, isl_map_copy(map));
2381 is_exact = isl_map_is_subset(app, test);
2382 isl_map_free(test);
2384 return is_exact;
2387 /* Check if basic map M_i can be combined with all the other
2388 * basic maps such that
2390 * (\cup_j M_j)^+
2392 * can be computed as
2394 * M_i \cup (\cup_{j \ne i} M_i^* \circ M_j \circ M_i^*)^+
2396 * In particular, check if we can compute a compact representation
2397 * of
2399 * M_i^* \circ M_j \circ M_i^*
2401 * for each j != i.
2402 * Let M_i^? be an extension of M_i^+ that allows paths
2403 * of length zero, i.e., the result of box_closure(., 1).
2404 * The criterion, as proposed by Kelly et al., is that
2405 * id = M_i^? - M_i^+ can be represented as a basic map
2406 * and that
2408 * id \circ M_j \circ id = M_j
2410 * for each j != i.
2412 * If this function returns 1, then tc and qc are set to
2413 * M_i^+ and M_i^?, respectively.
2415 static int can_be_split_off(__isl_keep isl_map *map, int i,
2416 __isl_give isl_map **tc, __isl_give isl_map **qc)
2418 isl_map *map_i, *id = NULL;
2419 int j = -1;
2420 isl_set *C;
2422 *tc = NULL;
2423 *qc = NULL;
2425 C = isl_set_union(isl_map_domain(isl_map_copy(map)),
2426 isl_map_range(isl_map_copy(map)));
2427 C = isl_set_from_basic_set(isl_set_simple_hull(C));
2428 if (!C)
2429 goto error;
2431 map_i = isl_map_from_basic_map(isl_basic_map_copy(map->p[i]));
2432 *tc = box_closure(isl_map_copy(map_i));
2433 *qc = box_closure_with_identity(map_i, C);
2434 id = isl_map_subtract(isl_map_copy(*qc), isl_map_copy(*tc));
2436 if (!id || !*qc)
2437 goto error;
2438 if (id->n != 1 || (*qc)->n != 1)
2439 goto done;
2441 for (j = 0; j < map->n; ++j) {
2442 isl_map *map_j, *test;
2443 int is_ok;
2445 if (i == j)
2446 continue;
2447 map_j = isl_map_from_basic_map(
2448 isl_basic_map_copy(map->p[j]));
2449 test = isl_map_apply_range(isl_map_copy(id),
2450 isl_map_copy(map_j));
2451 test = isl_map_apply_range(test, isl_map_copy(id));
2452 is_ok = isl_map_is_equal(test, map_j);
2453 isl_map_free(map_j);
2454 isl_map_free(test);
2455 if (is_ok < 0)
2456 goto error;
2457 if (!is_ok)
2458 break;
2461 done:
2462 isl_map_free(id);
2463 if (j == map->n)
2464 return 1;
2466 isl_map_free(*qc);
2467 isl_map_free(*tc);
2468 *qc = NULL;
2469 *tc = NULL;
2471 return 0;
2472 error:
2473 isl_map_free(id);
2474 isl_map_free(*qc);
2475 isl_map_free(*tc);
2476 *qc = NULL;
2477 *tc = NULL;
2478 return -1;
2481 static __isl_give isl_map *box_closure_with_check(__isl_take isl_map *map,
2482 int *exact)
2484 isl_map *app;
2486 app = box_closure(isl_map_copy(map));
2487 if (exact)
2488 *exact = check_exactness_omega(map, app);
2490 isl_map_free(map);
2491 return app;
2494 /* Compute an overapproximation of the transitive closure of "map"
2495 * using a variation of the algorithm from
2496 * "Transitive Closure of Infinite Graphs and its Applications"
2497 * by Kelly et al.
2499 * We first check whether we can can split of any basic map M_i and
2500 * compute
2502 * (\cup_j M_j)^+
2504 * as
2506 * M_i \cup (\cup_{j \ne i} M_i^* \circ M_j \circ M_i^*)^+
2508 * using a recursive call on the remaining map.
2510 * If not, we simply call box_closure on the whole map.
2512 static __isl_give isl_map *transitive_closure_omega(__isl_take isl_map *map,
2513 int *exact)
2515 int i, j;
2516 int exact_i;
2517 isl_map *app;
2519 if (!map)
2520 return NULL;
2521 if (map->n == 1)
2522 return box_closure_with_check(map, exact);
2524 for (i = 0; i < map->n; ++i) {
2525 int ok;
2526 isl_map *qc, *tc;
2527 ok = can_be_split_off(map, i, &tc, &qc);
2528 if (ok < 0)
2529 goto error;
2530 if (!ok)
2531 continue;
2533 app = isl_map_alloc_space(isl_map_get_space(map), map->n - 1, 0);
2535 for (j = 0; j < map->n; ++j) {
2536 if (j == i)
2537 continue;
2538 app = isl_map_add_basic_map(app,
2539 isl_basic_map_copy(map->p[j]));
2542 app = isl_map_apply_range(isl_map_copy(qc), app);
2543 app = isl_map_apply_range(app, qc);
2545 app = isl_map_union(tc, transitive_closure_omega(app, NULL));
2546 exact_i = check_exactness_omega(map, app);
2547 if (exact_i == 1) {
2548 if (exact)
2549 *exact = exact_i;
2550 isl_map_free(map);
2551 return app;
2553 isl_map_free(app);
2554 if (exact_i < 0)
2555 goto error;
2558 return box_closure_with_check(map, exact);
2559 error:
2560 isl_map_free(map);
2561 return NULL;
2564 /* Compute the transitive closure of "map", or an overapproximation.
2565 * If the result is exact, then *exact is set to 1.
2566 * Simply use map_power to compute the powers of map, but tell
2567 * it to project out the lengths of the paths instead of equating
2568 * the length to a parameter.
2570 __isl_give isl_map *isl_map_transitive_closure(__isl_take isl_map *map,
2571 int *exact)
2573 isl_space *target_dim;
2574 int closed;
2576 if (!map)
2577 goto error;
2579 if (map->ctx->opt->closure == ISL_CLOSURE_BOX)
2580 return transitive_closure_omega(map, exact);
2582 map = isl_map_compute_divs(map);
2583 map = isl_map_coalesce(map);
2584 closed = isl_map_is_transitively_closed(map);
2585 if (closed < 0)
2586 goto error;
2587 if (closed) {
2588 if (exact)
2589 *exact = 1;
2590 return map;
2593 target_dim = isl_map_get_space(map);
2594 map = map_power(map, exact, 1);
2595 map = isl_map_reset_space(map, target_dim);
2597 return map;
2598 error:
2599 isl_map_free(map);
2600 return NULL;
2603 static int inc_count(__isl_take isl_map *map, void *user)
2605 int *n = user;
2607 *n += map->n;
2609 isl_map_free(map);
2611 return 0;
2614 static int collect_basic_map(__isl_take isl_map *map, void *user)
2616 int i;
2617 isl_basic_map ***next = user;
2619 for (i = 0; i < map->n; ++i) {
2620 **next = isl_basic_map_copy(map->p[i]);
2621 if (!**next)
2622 goto error;
2623 (*next)++;
2626 isl_map_free(map);
2627 return 0;
2628 error:
2629 isl_map_free(map);
2630 return -1;
2633 /* Perform Floyd-Warshall on the given list of basic relations.
2634 * The basic relations may live in different dimensions,
2635 * but basic relations that get assigned to the diagonal of the
2636 * grid have domains and ranges of the same dimension and so
2637 * the standard algorithm can be used because the nested transitive
2638 * closures are only applied to diagonal elements and because all
2639 * compositions are peformed on relations with compatible domains and ranges.
2641 static __isl_give isl_union_map *union_floyd_warshall_on_list(isl_ctx *ctx,
2642 __isl_keep isl_basic_map **list, int n, int *exact)
2644 int i, j, k;
2645 int n_group;
2646 int *group = NULL;
2647 isl_set **set = NULL;
2648 isl_map ***grid = NULL;
2649 isl_union_map *app;
2651 group = setup_groups(ctx, list, n, &set, &n_group);
2652 if (!group)
2653 goto error;
2655 grid = isl_calloc_array(ctx, isl_map **, n_group);
2656 if (!grid)
2657 goto error;
2658 for (i = 0; i < n_group; ++i) {
2659 grid[i] = isl_calloc_array(ctx, isl_map *, n_group);
2660 if (!grid[i])
2661 goto error;
2662 for (j = 0; j < n_group; ++j) {
2663 isl_space *dim1, *dim2, *dim;
2664 dim1 = isl_space_reverse(isl_set_get_space(set[i]));
2665 dim2 = isl_set_get_space(set[j]);
2666 dim = isl_space_join(dim1, dim2);
2667 grid[i][j] = isl_map_empty(dim);
2671 for (k = 0; k < n; ++k) {
2672 i = group[2 * k];
2673 j = group[2 * k + 1];
2674 grid[i][j] = isl_map_union(grid[i][j],
2675 isl_map_from_basic_map(
2676 isl_basic_map_copy(list[k])));
2679 floyd_warshall_iterate(grid, n_group, exact);
2681 app = isl_union_map_empty(isl_map_get_space(grid[0][0]));
2683 for (i = 0; i < n_group; ++i) {
2684 for (j = 0; j < n_group; ++j)
2685 app = isl_union_map_add_map(app, grid[i][j]);
2686 free(grid[i]);
2688 free(grid);
2690 for (i = 0; i < 2 * n; ++i)
2691 isl_set_free(set[i]);
2692 free(set);
2694 free(group);
2695 return app;
2696 error:
2697 if (grid)
2698 for (i = 0; i < n_group; ++i) {
2699 if (!grid[i])
2700 continue;
2701 for (j = 0; j < n_group; ++j)
2702 isl_map_free(grid[i][j]);
2703 free(grid[i]);
2705 free(grid);
2706 if (set) {
2707 for (i = 0; i < 2 * n; ++i)
2708 isl_set_free(set[i]);
2709 free(set);
2711 free(group);
2712 return NULL;
2715 /* Perform Floyd-Warshall on the given union relation.
2716 * The implementation is very similar to that for non-unions.
2717 * The main difference is that it is applied unconditionally.
2718 * We first extract a list of basic maps from the union map
2719 * and then perform the algorithm on this list.
2721 static __isl_give isl_union_map *union_floyd_warshall(
2722 __isl_take isl_union_map *umap, int *exact)
2724 int i, n;
2725 isl_ctx *ctx;
2726 isl_basic_map **list = NULL;
2727 isl_basic_map **next;
2728 isl_union_map *res;
2730 n = 0;
2731 if (isl_union_map_foreach_map(umap, inc_count, &n) < 0)
2732 goto error;
2734 ctx = isl_union_map_get_ctx(umap);
2735 list = isl_calloc_array(ctx, isl_basic_map *, n);
2736 if (!list)
2737 goto error;
2739 next = list;
2740 if (isl_union_map_foreach_map(umap, collect_basic_map, &next) < 0)
2741 goto error;
2743 res = union_floyd_warshall_on_list(ctx, list, n, exact);
2745 if (list) {
2746 for (i = 0; i < n; ++i)
2747 isl_basic_map_free(list[i]);
2748 free(list);
2751 isl_union_map_free(umap);
2752 return res;
2753 error:
2754 if (list) {
2755 for (i = 0; i < n; ++i)
2756 isl_basic_map_free(list[i]);
2757 free(list);
2759 isl_union_map_free(umap);
2760 return NULL;
2763 /* Decompose the give union relation into strongly connected components.
2764 * The implementation is essentially the same as that of
2765 * construct_power_components with the major difference that all
2766 * operations are performed on union maps.
2768 static __isl_give isl_union_map *union_components(
2769 __isl_take isl_union_map *umap, int *exact)
2771 int i;
2772 int n;
2773 isl_ctx *ctx;
2774 isl_basic_map **list = NULL;
2775 isl_basic_map **next;
2776 isl_union_map *path = NULL;
2777 struct isl_tc_follows_data data;
2778 struct isl_tarjan_graph *g = NULL;
2779 int c, l;
2780 int recheck = 0;
2782 n = 0;
2783 if (isl_union_map_foreach_map(umap, inc_count, &n) < 0)
2784 goto error;
2786 if (n == 0)
2787 return umap;
2788 if (n <= 1)
2789 return union_floyd_warshall(umap, exact);
2791 ctx = isl_union_map_get_ctx(umap);
2792 list = isl_calloc_array(ctx, isl_basic_map *, n);
2793 if (!list)
2794 goto error;
2796 next = list;
2797 if (isl_union_map_foreach_map(umap, collect_basic_map, &next) < 0)
2798 goto error;
2800 data.list = list;
2801 data.check_closed = 0;
2802 g = isl_tarjan_graph_init(ctx, n, &basic_map_follows, &data);
2803 if (!g)
2804 goto error;
2806 c = 0;
2807 i = 0;
2808 l = n;
2809 path = isl_union_map_empty(isl_union_map_get_space(umap));
2810 while (l) {
2811 isl_union_map *comp;
2812 isl_union_map *path_comp, *path_comb;
2813 comp = isl_union_map_empty(isl_union_map_get_space(umap));
2814 while (g->order[i] != -1) {
2815 comp = isl_union_map_add_map(comp,
2816 isl_map_from_basic_map(
2817 isl_basic_map_copy(list[g->order[i]])));
2818 --l;
2819 ++i;
2821 path_comp = union_floyd_warshall(comp, exact);
2822 path_comb = isl_union_map_apply_range(isl_union_map_copy(path),
2823 isl_union_map_copy(path_comp));
2824 path = isl_union_map_union(path, path_comp);
2825 path = isl_union_map_union(path, path_comb);
2826 ++i;
2827 ++c;
2830 if (c > 1 && data.check_closed && !*exact) {
2831 int closed;
2833 closed = isl_union_map_is_transitively_closed(path);
2834 if (closed < 0)
2835 goto error;
2836 recheck = !closed;
2839 isl_tarjan_graph_free(g);
2841 for (i = 0; i < n; ++i)
2842 isl_basic_map_free(list[i]);
2843 free(list);
2845 if (recheck) {
2846 isl_union_map_free(path);
2847 return union_floyd_warshall(umap, exact);
2850 isl_union_map_free(umap);
2852 return path;
2853 error:
2854 isl_tarjan_graph_free(g);
2855 if (list) {
2856 for (i = 0; i < n; ++i)
2857 isl_basic_map_free(list[i]);
2858 free(list);
2860 isl_union_map_free(umap);
2861 isl_union_map_free(path);
2862 return NULL;
2865 /* Compute the transitive closure of "umap", or an overapproximation.
2866 * If the result is exact, then *exact is set to 1.
2868 __isl_give isl_union_map *isl_union_map_transitive_closure(
2869 __isl_take isl_union_map *umap, int *exact)
2871 int closed;
2873 if (!umap)
2874 return NULL;
2876 if (exact)
2877 *exact = 1;
2879 umap = isl_union_map_compute_divs(umap);
2880 umap = isl_union_map_coalesce(umap);
2881 closed = isl_union_map_is_transitively_closed(umap);
2882 if (closed < 0)
2883 goto error;
2884 if (closed)
2885 return umap;
2886 umap = union_components(umap, exact);
2887 return umap;
2888 error:
2889 isl_union_map_free(umap);
2890 return NULL;
2893 struct isl_union_power {
2894 isl_union_map *pow;
2895 int *exact;
2898 static int power(__isl_take isl_map *map, void *user)
2900 struct isl_union_power *up = user;
2902 map = isl_map_power(map, up->exact);
2903 up->pow = isl_union_map_from_map(map);
2905 return -1;
2908 /* Construct a map [x] -> [x+1], with parameters prescribed by "dim".
2910 static __isl_give isl_union_map *increment(__isl_take isl_space *dim)
2912 int k;
2913 isl_basic_map *bmap;
2915 dim = isl_space_add_dims(dim, isl_dim_in, 1);
2916 dim = isl_space_add_dims(dim, isl_dim_out, 1);
2917 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
2918 k = isl_basic_map_alloc_equality(bmap);
2919 if (k < 0)
2920 goto error;
2921 isl_seq_clr(bmap->eq[k], isl_basic_map_total_dim(bmap));
2922 isl_int_set_si(bmap->eq[k][0], 1);
2923 isl_int_set_si(bmap->eq[k][isl_basic_map_offset(bmap, isl_dim_in)], 1);
2924 isl_int_set_si(bmap->eq[k][isl_basic_map_offset(bmap, isl_dim_out)], -1);
2925 return isl_union_map_from_map(isl_map_from_basic_map(bmap));
2926 error:
2927 isl_basic_map_free(bmap);
2928 return NULL;
2931 /* Construct a map [[x]->[y]] -> [y-x], with parameters prescribed by "dim".
2933 static __isl_give isl_union_map *deltas_map(__isl_take isl_space *dim)
2935 isl_basic_map *bmap;
2937 dim = isl_space_add_dims(dim, isl_dim_in, 1);
2938 dim = isl_space_add_dims(dim, isl_dim_out, 1);
2939 bmap = isl_basic_map_universe(dim);
2940 bmap = isl_basic_map_deltas_map(bmap);
2942 return isl_union_map_from_map(isl_map_from_basic_map(bmap));
2945 /* Compute the positive powers of "map", or an overapproximation.
2946 * The result maps the exponent to a nested copy of the corresponding power.
2947 * If the result is exact, then *exact is set to 1.
2949 __isl_give isl_union_map *isl_union_map_power(__isl_take isl_union_map *umap,
2950 int *exact)
2952 int n;
2953 isl_union_map *inc;
2954 isl_union_map *dm;
2956 if (!umap)
2957 return NULL;
2958 n = isl_union_map_n_map(umap);
2959 if (n == 0)
2960 return umap;
2961 if (n == 1) {
2962 struct isl_union_power up = { NULL, exact };
2963 isl_union_map_foreach_map(umap, &power, &up);
2964 isl_union_map_free(umap);
2965 return up.pow;
2967 inc = increment(isl_union_map_get_space(umap));
2968 umap = isl_union_map_product(inc, umap);
2969 umap = isl_union_map_transitive_closure(umap, exact);
2970 umap = isl_union_map_zip(umap);
2971 dm = deltas_map(isl_union_map_get_space(umap));
2972 umap = isl_union_map_apply_domain(umap, dm);
2974 return umap;
2977 #undef TYPE
2978 #define TYPE isl_map
2979 #include "isl_power_templ.c"
2981 #undef TYPE
2982 #define TYPE isl_union_map
2983 #include "isl_power_templ.c"