isl_transitive_closure.c: floyd_warshall_with_groups: rename "dim" argument
[isl.git] / isl_transitive_closure.c
blob8b73c73cc847a7c5feea04b200d2f378e6efd325
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 *space,
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 (!space || !steps)
228 goto error;
230 d = isl_space_dim(space, isl_dim_in);
231 n = steps->n_row;
232 nparam = isl_space_dim(space, isl_dim_param);
234 path = isl_basic_map_alloc_space(isl_space_copy(space), 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(space);
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(space);
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 isl_bool parametric_constant_never_positive(
288 __isl_keep isl_basic_set *bset, isl_int *c, int *div_purity)
290 unsigned d;
291 unsigned n_div;
292 unsigned nparam;
293 int i;
294 int k;
295 isl_bool 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 isl_bool_error;
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 isl_bool 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 test = isl_basic_map_gauss(test, NULL);
439 id = isl_basic_map_identity(isl_basic_map_get_space(path));
440 is_id = isl_basic_map_is_equal(test, id);
441 isl_basic_map_free(test);
442 isl_basic_map_free(id);
443 return is_id;
444 error:
445 isl_basic_map_free(test);
446 return -1;
449 /* If any of the constraints is found to be impure then this function
450 * sets *impurity to 1.
452 * If impurity is NULL then we are dealing with a non-parametric set
453 * and so the constraints are obviously PURE_VAR.
455 static __isl_give isl_basic_map *add_delta_constraints(
456 __isl_take isl_basic_map *path,
457 __isl_keep isl_basic_set *delta, unsigned off, unsigned nparam,
458 unsigned d, int *div_purity, int eq, int *impurity)
460 int i, k;
461 int n = eq ? delta->n_eq : delta->n_ineq;
462 isl_int **delta_c = eq ? delta->eq : delta->ineq;
463 unsigned n_div;
465 n_div = isl_basic_set_dim(delta, isl_dim_div);
467 for (i = 0; i < n; ++i) {
468 isl_int *path_c;
469 int p = PURE_VAR;
470 if (impurity)
471 p = purity(delta, delta_c[i], div_purity, eq);
472 if (p < 0)
473 goto error;
474 if (p != PURE_VAR && p != PURE_PARAM && !*impurity)
475 *impurity = 1;
476 if (p == IMPURE)
477 continue;
478 if (eq && p != MIXED) {
479 k = isl_basic_map_alloc_equality(path);
480 if (k < 0)
481 goto error;
482 path_c = path->eq[k];
483 } else {
484 k = isl_basic_map_alloc_inequality(path);
485 if (k < 0)
486 goto error;
487 path_c = path->ineq[k];
489 isl_seq_clr(path_c, 1 + isl_basic_map_total_dim(path));
490 if (p == PURE_VAR) {
491 isl_seq_cpy(path_c + off,
492 delta_c[i] + 1 + nparam, d);
493 isl_int_set(path_c[off + d], delta_c[i][0]);
494 } else if (p == PURE_PARAM) {
495 isl_seq_cpy(path_c, delta_c[i], 1 + nparam);
496 } else {
497 isl_seq_cpy(path_c + off,
498 delta_c[i] + 1 + nparam, d);
499 isl_seq_cpy(path_c, delta_c[i], 1 + nparam);
501 isl_seq_cpy(path_c + off - n_div,
502 delta_c[i] + 1 + nparam + d, n_div);
505 return path;
506 error:
507 isl_basic_map_free(path);
508 return NULL;
511 /* Given a set of offsets "delta", construct a relation of the
512 * given dimension specification (Z^{n+1} -> Z^{n+1}) that
513 * is an overapproximation of the relations that
514 * maps an element x to any element that can be reached
515 * by taking a non-negative number of steps along any of
516 * the elements in "delta".
517 * That is, construct an approximation of
519 * { [x] -> [y] : exists f \in \delta, k \in Z :
520 * y = x + k [f, 1] and k >= 0 }
522 * For any element in this relation, the number of steps taken
523 * is equal to the difference in the final coordinates.
525 * In particular, let delta be defined as
527 * \delta = [p] -> { [x] : A x + a >= 0 and B p + b >= 0 and
528 * C x + C'p + c >= 0 and
529 * D x + D'p + d >= 0 }
531 * where the constraints C x + C'p + c >= 0 are such that the parametric
532 * constant term of each constraint j, "C_j x + C'_j p + c_j",
533 * can never attain positive values, then the relation is constructed as
535 * { [x] -> [y] : exists [f, k] \in Z^{n+1} : y = x + f and
536 * A f + k a >= 0 and B p + b >= 0 and
537 * C f + C'p + c >= 0 and k >= 1 }
538 * union { [x] -> [x] }
540 * If the zero-length paths happen to correspond exactly to the identity
541 * mapping, then we return
543 * { [x] -> [y] : exists [f, k] \in Z^{n+1} : y = x + f and
544 * A f + k a >= 0 and B p + b >= 0 and
545 * C f + C'p + c >= 0 and k >= 0 }
547 * instead.
549 * Existentially quantified variables in \delta are handled by
550 * classifying them as independent of the parameters, purely
551 * parameter dependent and others. Constraints containing
552 * any of the other existentially quantified variables are removed.
553 * This is safe, but leads to an additional overapproximation.
555 * If there are any impure constraints, then we also eliminate
556 * the parameters from \delta, resulting in a set
558 * \delta' = { [x] : E x + e >= 0 }
560 * and add the constraints
562 * E f + k e >= 0
564 * to the constructed relation.
566 static __isl_give isl_map *path_along_delta(__isl_take isl_space *space,
567 __isl_take isl_basic_set *delta)
569 isl_basic_map *path = NULL;
570 unsigned d;
571 unsigned n_div;
572 unsigned nparam;
573 unsigned off;
574 int i, k;
575 int is_id;
576 int *div_purity = NULL;
577 int impurity = 0;
579 if (!delta)
580 goto error;
581 n_div = isl_basic_set_dim(delta, isl_dim_div);
582 d = isl_basic_set_dim(delta, isl_dim_set);
583 nparam = isl_basic_set_dim(delta, isl_dim_param);
584 path = isl_basic_map_alloc_space(isl_space_copy(space), n_div + d + 1,
585 d + 1 + delta->n_eq, delta->n_eq + delta->n_ineq + 1);
586 off = 1 + nparam + 2 * (d + 1) + n_div;
588 for (i = 0; i < n_div + d + 1; ++i) {
589 k = isl_basic_map_alloc_div(path);
590 if (k < 0)
591 goto error;
592 isl_int_set_si(path->div[k][0], 0);
595 for (i = 0; i < d + 1; ++i) {
596 k = isl_basic_map_alloc_equality(path);
597 if (k < 0)
598 goto error;
599 isl_seq_clr(path->eq[k], 1 + isl_basic_map_total_dim(path));
600 isl_int_set_si(path->eq[k][1 + nparam + i], 1);
601 isl_int_set_si(path->eq[k][1 + nparam + d + 1 + i], -1);
602 isl_int_set_si(path->eq[k][off + i], 1);
605 div_purity = get_div_purity(delta);
606 if (n_div && !div_purity)
607 goto error;
609 path = add_delta_constraints(path, delta, off, nparam, d,
610 div_purity, 1, &impurity);
611 path = add_delta_constraints(path, delta, off, nparam, d,
612 div_purity, 0, &impurity);
613 if (impurity) {
614 isl_space *dim = isl_basic_set_get_space(delta);
615 delta = isl_basic_set_project_out(delta,
616 isl_dim_param, 0, nparam);
617 delta = isl_basic_set_add_dims(delta, isl_dim_param, nparam);
618 delta = isl_basic_set_reset_space(delta, dim);
619 if (!delta)
620 goto error;
621 path = isl_basic_map_extend_constraints(path, delta->n_eq,
622 delta->n_ineq + 1);
623 path = add_delta_constraints(path, delta, off, nparam, d,
624 NULL, 1, NULL);
625 path = add_delta_constraints(path, delta, off, nparam, d,
626 NULL, 0, NULL);
627 path = isl_basic_map_gauss(path, NULL);
630 is_id = empty_path_is_identity(path, off + d);
631 if (is_id < 0)
632 goto error;
634 k = isl_basic_map_alloc_inequality(path);
635 if (k < 0)
636 goto error;
637 isl_seq_clr(path->ineq[k], 1 + isl_basic_map_total_dim(path));
638 if (!is_id)
639 isl_int_set_si(path->ineq[k][0], -1);
640 isl_int_set_si(path->ineq[k][off + d], 1);
642 free(div_purity);
643 isl_basic_set_free(delta);
644 path = isl_basic_map_finalize(path);
645 if (is_id) {
646 isl_space_free(space);
647 return isl_map_from_basic_map(path);
649 return isl_basic_map_union(path, isl_basic_map_identity(space));
650 error:
651 free(div_purity);
652 isl_space_free(space);
653 isl_basic_set_free(delta);
654 isl_basic_map_free(path);
655 return NULL;
658 /* Given a dimension specification Z^{n+1} -> Z^{n+1} and a parameter "param",
659 * construct a map that equates the parameter to the difference
660 * in the final coordinates and imposes that this difference is positive.
661 * That is, construct
663 * { [x,x_s] -> [y,y_s] : k = y_s - x_s > 0 }
665 static __isl_give isl_map *equate_parameter_to_length(
666 __isl_take isl_space *space, unsigned param)
668 struct isl_basic_map *bmap;
669 unsigned d;
670 unsigned nparam;
671 int k;
673 d = isl_space_dim(space, isl_dim_in);
674 nparam = isl_space_dim(space, isl_dim_param);
675 bmap = isl_basic_map_alloc_space(space, 0, 1, 1);
676 k = isl_basic_map_alloc_equality(bmap);
677 if (k < 0)
678 goto error;
679 isl_seq_clr(bmap->eq[k], 1 + isl_basic_map_total_dim(bmap));
680 isl_int_set_si(bmap->eq[k][1 + param], -1);
681 isl_int_set_si(bmap->eq[k][1 + nparam + d - 1], -1);
682 isl_int_set_si(bmap->eq[k][1 + nparam + d + d - 1], 1);
684 k = isl_basic_map_alloc_inequality(bmap);
685 if (k < 0)
686 goto error;
687 isl_seq_clr(bmap->ineq[k], 1 + isl_basic_map_total_dim(bmap));
688 isl_int_set_si(bmap->ineq[k][1 + param], 1);
689 isl_int_set_si(bmap->ineq[k][0], -1);
691 bmap = isl_basic_map_finalize(bmap);
692 return isl_map_from_basic_map(bmap);
693 error:
694 isl_basic_map_free(bmap);
695 return NULL;
698 /* Check whether "path" is acyclic, where the last coordinates of domain
699 * and range of path encode the number of steps taken.
700 * That is, check whether
702 * { d | d = y - x and (x,y) in path }
704 * does not contain any element with positive last coordinate (positive length)
705 * and zero remaining coordinates (cycle).
707 static int is_acyclic(__isl_take isl_map *path)
709 int i;
710 int acyclic;
711 unsigned dim;
712 struct isl_set *delta;
714 delta = isl_map_deltas(path);
715 dim = isl_set_dim(delta, isl_dim_set);
716 for (i = 0; i < dim; ++i) {
717 if (i == dim -1)
718 delta = isl_set_lower_bound_si(delta, isl_dim_set, i, 1);
719 else
720 delta = isl_set_fix_si(delta, isl_dim_set, i, 0);
723 acyclic = isl_set_is_empty(delta);
724 isl_set_free(delta);
726 return acyclic;
729 /* Given a union of basic maps R = \cup_i R_i \subseteq D \times D
730 * and a dimension specification (Z^{n+1} -> Z^{n+1}),
731 * construct a map that is an overapproximation of the map
732 * that takes an element from the space D \times Z to another
733 * element from the same space, such that the first n coordinates of the
734 * difference between them is a sum of differences between images
735 * and pre-images in one of the R_i and such that the last coordinate
736 * is equal to the number of steps taken.
737 * That is, let
739 * \Delta_i = { y - x | (x, y) in R_i }
741 * then the constructed map is an overapproximation of
743 * { (x) -> (x + d) | \exists k_i >= 0, \delta_i \in \Delta_i :
744 * d = (\sum_i k_i \delta_i, \sum_i k_i) }
746 * The elements of the singleton \Delta_i's are collected as the
747 * rows of the steps matrix. For all these \Delta_i's together,
748 * a single path is constructed.
749 * For each of the other \Delta_i's, we compute an overapproximation
750 * of the paths along elements of \Delta_i.
751 * Since each of these paths performs an addition, composition is
752 * symmetric and we can simply compose all resulting paths in any order.
754 static __isl_give isl_map *construct_extended_path(__isl_take isl_space *space,
755 __isl_keep isl_map *map, int *project)
757 struct isl_mat *steps = NULL;
758 struct isl_map *path = NULL;
759 unsigned d;
760 int i, j, n;
762 if (!map)
763 goto error;
765 d = isl_map_dim(map, isl_dim_in);
767 path = isl_map_identity(isl_space_copy(space));
769 steps = isl_mat_alloc(map->ctx, map->n, d);
770 if (!steps)
771 goto error;
773 n = 0;
774 for (i = 0; i < map->n; ++i) {
775 struct isl_basic_set *delta;
777 delta = isl_basic_map_deltas(isl_basic_map_copy(map->p[i]));
779 for (j = 0; j < d; ++j) {
780 isl_bool fixed;
782 fixed = isl_basic_set_plain_dim_is_fixed(delta, j,
783 &steps->row[n][j]);
784 if (fixed < 0) {
785 isl_basic_set_free(delta);
786 goto error;
788 if (!fixed)
789 break;
793 if (j < d) {
794 path = isl_map_apply_range(path,
795 path_along_delta(isl_space_copy(space), delta));
796 path = isl_map_coalesce(path);
797 } else {
798 isl_basic_set_free(delta);
799 ++n;
803 if (n > 0) {
804 steps->n_row = n;
805 path = isl_map_apply_range(path,
806 path_along_steps(isl_space_copy(space), steps));
809 if (project && *project) {
810 *project = is_acyclic(isl_map_copy(path));
811 if (*project < 0)
812 goto error;
815 isl_space_free(space);
816 isl_mat_free(steps);
817 return path;
818 error:
819 isl_space_free(space);
820 isl_mat_free(steps);
821 isl_map_free(path);
822 return NULL;
825 static isl_bool isl_set_overlaps(__isl_keep isl_set *set1,
826 __isl_keep isl_set *set2)
828 isl_set *i;
829 isl_bool no_overlap;
831 if (!set1 || !set2)
832 return isl_bool_error;
834 if (!isl_space_tuple_is_equal(set1->dim, isl_dim_set,
835 set2->dim, isl_dim_set))
836 return isl_bool_false;
838 i = isl_set_intersect(isl_set_copy(set1), isl_set_copy(set2));
839 no_overlap = isl_set_is_empty(i);
840 isl_set_free(i);
842 return isl_bool_not(no_overlap);
845 /* Given a union of basic maps R = \cup_i R_i \subseteq D \times D
846 * and a dimension specification (Z^{n+1} -> Z^{n+1}),
847 * construct a map that is an overapproximation of the map
848 * that takes an element from the dom R \times Z to an
849 * element from ran R \times Z, such that the first n coordinates of the
850 * difference between them is a sum of differences between images
851 * and pre-images in one of the R_i and such that the last coordinate
852 * is equal to the number of steps taken.
853 * That is, let
855 * \Delta_i = { y - x | (x, y) in R_i }
857 * then the constructed map is an overapproximation of
859 * { (x) -> (x + d) | \exists k_i >= 0, \delta_i \in \Delta_i :
860 * d = (\sum_i k_i \delta_i, \sum_i k_i) and
861 * x in dom R and x + d in ran R and
862 * \sum_i k_i >= 1 }
864 static __isl_give isl_map *construct_component(__isl_take isl_space *dim,
865 __isl_keep isl_map *map, int *exact, int project)
867 struct isl_set *domain = NULL;
868 struct isl_set *range = NULL;
869 struct isl_map *app = NULL;
870 struct isl_map *path = NULL;
871 isl_bool overlaps;
873 domain = isl_map_domain(isl_map_copy(map));
874 domain = isl_set_coalesce(domain);
875 range = isl_map_range(isl_map_copy(map));
876 range = isl_set_coalesce(range);
877 overlaps = isl_set_overlaps(domain, range);
878 if (overlaps < 0 || !overlaps) {
879 isl_set_free(domain);
880 isl_set_free(range);
881 isl_space_free(dim);
883 if (overlaps < 0)
884 map = NULL;
885 map = isl_map_copy(map);
886 map = isl_map_add_dims(map, isl_dim_in, 1);
887 map = isl_map_add_dims(map, isl_dim_out, 1);
888 map = set_path_length(map, 1, 1);
889 return map;
891 app = isl_map_from_domain_and_range(domain, range);
892 app = isl_map_add_dims(app, isl_dim_in, 1);
893 app = isl_map_add_dims(app, isl_dim_out, 1);
895 path = construct_extended_path(isl_space_copy(dim), map,
896 exact && *exact ? &project : NULL);
897 app = isl_map_intersect(app, path);
899 if (exact && *exact &&
900 (*exact = check_exactness(isl_map_copy(map), isl_map_copy(app),
901 project)) < 0)
902 goto error;
904 isl_space_free(dim);
905 app = set_path_length(app, 0, 1);
906 return app;
907 error:
908 isl_space_free(dim);
909 isl_map_free(app);
910 return NULL;
913 /* Call construct_component and, if "project" is set, project out
914 * the final coordinates.
916 static __isl_give isl_map *construct_projected_component(
917 __isl_take isl_space *space,
918 __isl_keep isl_map *map, int *exact, int project)
920 isl_map *app;
921 unsigned d;
923 if (!space)
924 return NULL;
925 d = isl_space_dim(space, isl_dim_in);
927 app = construct_component(space, map, exact, project);
928 if (project) {
929 app = isl_map_project_out(app, isl_dim_in, d - 1, 1);
930 app = isl_map_project_out(app, isl_dim_out, d - 1, 1);
932 return app;
935 /* Compute an extended version, i.e., with path lengths, of
936 * an overapproximation of the transitive closure of "bmap"
937 * with path lengths greater than or equal to zero and with
938 * domain and range equal to "dom".
940 static __isl_give isl_map *q_closure(__isl_take isl_space *dim,
941 __isl_take isl_set *dom, __isl_keep isl_basic_map *bmap, int *exact)
943 int project = 1;
944 isl_map *path;
945 isl_map *map;
946 isl_map *app;
948 dom = isl_set_add_dims(dom, isl_dim_set, 1);
949 app = isl_map_from_domain_and_range(dom, isl_set_copy(dom));
950 map = isl_map_from_basic_map(isl_basic_map_copy(bmap));
951 path = construct_extended_path(dim, map, &project);
952 app = isl_map_intersect(app, path);
954 if ((*exact = check_exactness(map, isl_map_copy(app), project)) < 0)
955 goto error;
957 return app;
958 error:
959 isl_map_free(app);
960 return NULL;
963 /* Check whether qc has any elements of length at least one
964 * with domain and/or range outside of dom and ran.
966 static int has_spurious_elements(__isl_keep isl_map *qc,
967 __isl_keep isl_set *dom, __isl_keep isl_set *ran)
969 isl_set *s;
970 int subset;
971 unsigned d;
973 if (!qc || !dom || !ran)
974 return -1;
976 d = isl_map_dim(qc, isl_dim_in);
978 qc = isl_map_copy(qc);
979 qc = set_path_length(qc, 0, 1);
980 qc = isl_map_project_out(qc, isl_dim_in, d - 1, 1);
981 qc = isl_map_project_out(qc, isl_dim_out, d - 1, 1);
983 s = isl_map_domain(isl_map_copy(qc));
984 subset = isl_set_is_subset(s, dom);
985 isl_set_free(s);
986 if (subset < 0)
987 goto error;
988 if (!subset) {
989 isl_map_free(qc);
990 return 1;
993 s = isl_map_range(qc);
994 subset = isl_set_is_subset(s, ran);
995 isl_set_free(s);
997 return subset < 0 ? -1 : !subset;
998 error:
999 isl_map_free(qc);
1000 return -1;
1003 #define LEFT 2
1004 #define RIGHT 1
1006 /* For each basic map in "map", except i, check whether it combines
1007 * with the transitive closure that is reflexive on C combines
1008 * to the left and to the right.
1010 * In particular, if
1012 * dom map_j \subseteq C
1014 * then right[j] is set to 1. Otherwise, if
1016 * ran map_i \cap dom map_j = \emptyset
1018 * then right[j] is set to 0. Otherwise, composing to the right
1019 * is impossible.
1021 * Similar, for composing to the left, we have if
1023 * ran map_j \subseteq C
1025 * then left[j] is set to 1. Otherwise, if
1027 * dom map_i \cap ran map_j = \emptyset
1029 * then left[j] is set to 0. Otherwise, composing to the left
1030 * is impossible.
1032 * The return value is or'd with LEFT if composing to the left
1033 * is possible and with RIGHT if composing to the right is possible.
1035 static int composability(__isl_keep isl_set *C, int i,
1036 isl_set **dom, isl_set **ran, int *left, int *right,
1037 __isl_keep isl_map *map)
1039 int j;
1040 int ok;
1042 ok = LEFT | RIGHT;
1043 for (j = 0; j < map->n && ok; ++j) {
1044 isl_bool overlaps, subset;
1045 if (j == i)
1046 continue;
1048 if (ok & RIGHT) {
1049 if (!dom[j])
1050 dom[j] = isl_set_from_basic_set(
1051 isl_basic_map_domain(
1052 isl_basic_map_copy(map->p[j])));
1053 if (!dom[j])
1054 return -1;
1055 overlaps = isl_set_overlaps(ran[i], dom[j]);
1056 if (overlaps < 0)
1057 return -1;
1058 if (!overlaps)
1059 right[j] = 0;
1060 else {
1061 subset = isl_set_is_subset(dom[j], C);
1062 if (subset < 0)
1063 return -1;
1064 if (subset)
1065 right[j] = 1;
1066 else
1067 ok &= ~RIGHT;
1071 if (ok & LEFT) {
1072 if (!ran[j])
1073 ran[j] = isl_set_from_basic_set(
1074 isl_basic_map_range(
1075 isl_basic_map_copy(map->p[j])));
1076 if (!ran[j])
1077 return -1;
1078 overlaps = isl_set_overlaps(dom[i], ran[j]);
1079 if (overlaps < 0)
1080 return -1;
1081 if (!overlaps)
1082 left[j] = 0;
1083 else {
1084 subset = isl_set_is_subset(ran[j], C);
1085 if (subset < 0)
1086 return -1;
1087 if (subset)
1088 left[j] = 1;
1089 else
1090 ok &= ~LEFT;
1095 return ok;
1098 static __isl_give isl_map *anonymize(__isl_take isl_map *map)
1100 map = isl_map_reset(map, isl_dim_in);
1101 map = isl_map_reset(map, isl_dim_out);
1102 return map;
1105 /* Return a map that is a union of the basic maps in "map", except i,
1106 * composed to left and right with qc based on the entries of "left"
1107 * and "right".
1109 static __isl_give isl_map *compose(__isl_keep isl_map *map, int i,
1110 __isl_take isl_map *qc, int *left, int *right)
1112 int j;
1113 isl_map *comp;
1115 comp = isl_map_empty(isl_map_get_space(map));
1116 for (j = 0; j < map->n; ++j) {
1117 isl_map *map_j;
1119 if (j == i)
1120 continue;
1122 map_j = isl_map_from_basic_map(isl_basic_map_copy(map->p[j]));
1123 map_j = anonymize(map_j);
1124 if (left && left[j])
1125 map_j = isl_map_apply_range(map_j, isl_map_copy(qc));
1126 if (right && right[j])
1127 map_j = isl_map_apply_range(isl_map_copy(qc), map_j);
1128 comp = isl_map_union(comp, map_j);
1131 comp = isl_map_compute_divs(comp);
1132 comp = isl_map_coalesce(comp);
1134 isl_map_free(qc);
1136 return comp;
1139 /* Compute the transitive closure of "map" incrementally by
1140 * computing
1142 * map_i^+ \cup qc^+
1144 * or
1146 * map_i^+ \cup ((id \cup map_i^) \circ qc^+)
1148 * or
1150 * map_i^+ \cup (qc^+ \circ (id \cup map_i^))
1152 * depending on whether left or right are NULL.
1154 static __isl_give isl_map *compute_incremental(
1155 __isl_take isl_space *space, __isl_keep isl_map *map,
1156 int i, __isl_take isl_map *qc, int *left, int *right, int *exact)
1158 isl_map *map_i;
1159 isl_map *tc;
1160 isl_map *rtc = NULL;
1162 if (!map)
1163 goto error;
1164 isl_assert(map->ctx, left || right, goto error);
1166 map_i = isl_map_from_basic_map(isl_basic_map_copy(map->p[i]));
1167 tc = construct_projected_component(isl_space_copy(space), map_i,
1168 exact, 1);
1169 isl_map_free(map_i);
1171 if (*exact)
1172 qc = isl_map_transitive_closure(qc, exact);
1174 if (!*exact) {
1175 isl_space_free(space);
1176 isl_map_free(tc);
1177 isl_map_free(qc);
1178 return isl_map_universe(isl_map_get_space(map));
1181 if (!left || !right)
1182 rtc = isl_map_union(isl_map_copy(tc),
1183 isl_map_identity(isl_map_get_space(tc)));
1184 if (!right)
1185 qc = isl_map_apply_range(rtc, qc);
1186 if (!left)
1187 qc = isl_map_apply_range(qc, rtc);
1188 qc = isl_map_union(tc, qc);
1190 isl_space_free(space);
1192 return qc;
1193 error:
1194 isl_space_free(space);
1195 isl_map_free(qc);
1196 return NULL;
1199 /* Given a map "map", try to find a basic map such that
1200 * map^+ can be computed as
1202 * map^+ = map_i^+ \cup
1203 * \bigcup_j ((map_i^+ \cup Id_C)^+ \circ map_j \circ (map_i^+ \cup Id_C))^+
1205 * with C the simple hull of the domain and range of the input map.
1206 * map_i^ \cup Id_C is computed by allowing the path lengths to be zero
1207 * and by intersecting domain and range with C.
1208 * Of course, we need to check that this is actually equal to map_i^ \cup Id_C.
1209 * Also, we only use the incremental computation if all the transitive
1210 * closures are exact and if the number of basic maps in the union,
1211 * after computing the integer divisions, is smaller than the number
1212 * of basic maps in the input map.
1214 static int incremental_on_entire_domain(__isl_keep isl_space *space,
1215 __isl_keep isl_map *map,
1216 isl_set **dom, isl_set **ran, int *left, int *right,
1217 __isl_give isl_map **res)
1219 int i;
1220 isl_set *C;
1221 unsigned d;
1223 *res = NULL;
1225 C = isl_set_union(isl_map_domain(isl_map_copy(map)),
1226 isl_map_range(isl_map_copy(map)));
1227 C = isl_set_from_basic_set(isl_set_simple_hull(C));
1228 if (!C)
1229 return -1;
1230 if (C->n != 1) {
1231 isl_set_free(C);
1232 return 0;
1235 d = isl_map_dim(map, isl_dim_in);
1237 for (i = 0; i < map->n; ++i) {
1238 isl_map *qc;
1239 int exact_i, spurious;
1240 int j;
1241 dom[i] = isl_set_from_basic_set(isl_basic_map_domain(
1242 isl_basic_map_copy(map->p[i])));
1243 ran[i] = isl_set_from_basic_set(isl_basic_map_range(
1244 isl_basic_map_copy(map->p[i])));
1245 qc = q_closure(isl_space_copy(space), isl_set_copy(C),
1246 map->p[i], &exact_i);
1247 if (!qc)
1248 goto error;
1249 if (!exact_i) {
1250 isl_map_free(qc);
1251 continue;
1253 spurious = has_spurious_elements(qc, dom[i], ran[i]);
1254 if (spurious) {
1255 isl_map_free(qc);
1256 if (spurious < 0)
1257 goto error;
1258 continue;
1260 qc = isl_map_project_out(qc, isl_dim_in, d, 1);
1261 qc = isl_map_project_out(qc, isl_dim_out, d, 1);
1262 qc = isl_map_compute_divs(qc);
1263 for (j = 0; j < map->n; ++j)
1264 left[j] = right[j] = 1;
1265 qc = compose(map, i, qc, left, right);
1266 if (!qc)
1267 goto error;
1268 if (qc->n >= map->n) {
1269 isl_map_free(qc);
1270 continue;
1272 *res = compute_incremental(isl_space_copy(space), map, i, qc,
1273 left, right, &exact_i);
1274 if (!*res)
1275 goto error;
1276 if (exact_i)
1277 break;
1278 isl_map_free(*res);
1279 *res = NULL;
1282 isl_set_free(C);
1284 return *res != NULL;
1285 error:
1286 isl_set_free(C);
1287 return -1;
1290 /* Try and compute the transitive closure of "map" as
1292 * map^+ = map_i^+ \cup
1293 * \bigcup_j ((map_i^+ \cup Id_C)^+ \circ map_j \circ (map_i^+ \cup Id_C))^+
1295 * with C either the simple hull of the domain and range of the entire
1296 * map or the simple hull of domain and range of map_i.
1298 static __isl_give isl_map *incremental_closure(__isl_take isl_space *space,
1299 __isl_keep isl_map *map, int *exact, int project)
1301 int i;
1302 isl_set **dom = NULL;
1303 isl_set **ran = NULL;
1304 int *left = NULL;
1305 int *right = NULL;
1306 isl_set *C;
1307 unsigned d;
1308 isl_map *res = NULL;
1310 if (!project)
1311 return construct_projected_component(space, map, exact,
1312 project);
1314 if (!map)
1315 goto error;
1316 if (map->n <= 1)
1317 return construct_projected_component(space, map, exact,
1318 project);
1320 d = isl_map_dim(map, isl_dim_in);
1322 dom = isl_calloc_array(map->ctx, isl_set *, map->n);
1323 ran = isl_calloc_array(map->ctx, isl_set *, map->n);
1324 left = isl_calloc_array(map->ctx, int, map->n);
1325 right = isl_calloc_array(map->ctx, int, map->n);
1326 if (!ran || !dom || !left || !right)
1327 goto error;
1329 if (incremental_on_entire_domain(space, map, dom, ran, left, right,
1330 &res) < 0)
1331 goto error;
1333 for (i = 0; !res && i < map->n; ++i) {
1334 isl_map *qc;
1335 int exact_i, spurious, comp;
1336 if (!dom[i])
1337 dom[i] = isl_set_from_basic_set(
1338 isl_basic_map_domain(
1339 isl_basic_map_copy(map->p[i])));
1340 if (!dom[i])
1341 goto error;
1342 if (!ran[i])
1343 ran[i] = isl_set_from_basic_set(
1344 isl_basic_map_range(
1345 isl_basic_map_copy(map->p[i])));
1346 if (!ran[i])
1347 goto error;
1348 C = isl_set_union(isl_set_copy(dom[i]),
1349 isl_set_copy(ran[i]));
1350 C = isl_set_from_basic_set(isl_set_simple_hull(C));
1351 if (!C)
1352 goto error;
1353 if (C->n != 1) {
1354 isl_set_free(C);
1355 continue;
1357 comp = composability(C, i, dom, ran, left, right, map);
1358 if (!comp || comp < 0) {
1359 isl_set_free(C);
1360 if (comp < 0)
1361 goto error;
1362 continue;
1364 qc = q_closure(isl_space_copy(space), C, map->p[i], &exact_i);
1365 if (!qc)
1366 goto error;
1367 if (!exact_i) {
1368 isl_map_free(qc);
1369 continue;
1371 spurious = has_spurious_elements(qc, dom[i], ran[i]);
1372 if (spurious) {
1373 isl_map_free(qc);
1374 if (spurious < 0)
1375 goto error;
1376 continue;
1378 qc = isl_map_project_out(qc, isl_dim_in, d, 1);
1379 qc = isl_map_project_out(qc, isl_dim_out, d, 1);
1380 qc = isl_map_compute_divs(qc);
1381 qc = compose(map, i, qc, (comp & LEFT) ? left : NULL,
1382 (comp & RIGHT) ? right : NULL);
1383 if (!qc)
1384 goto error;
1385 if (qc->n >= map->n) {
1386 isl_map_free(qc);
1387 continue;
1389 res = compute_incremental(isl_space_copy(space), map, i, qc,
1390 (comp & LEFT) ? left : NULL,
1391 (comp & RIGHT) ? right : NULL, &exact_i);
1392 if (!res)
1393 goto error;
1394 if (exact_i)
1395 break;
1396 isl_map_free(res);
1397 res = NULL;
1400 for (i = 0; i < map->n; ++i) {
1401 isl_set_free(dom[i]);
1402 isl_set_free(ran[i]);
1404 free(dom);
1405 free(ran);
1406 free(left);
1407 free(right);
1409 if (res) {
1410 isl_space_free(space);
1411 return res;
1414 return construct_projected_component(space, map, exact, project);
1415 error:
1416 if (dom)
1417 for (i = 0; i < map->n; ++i)
1418 isl_set_free(dom[i]);
1419 free(dom);
1420 if (ran)
1421 for (i = 0; i < map->n; ++i)
1422 isl_set_free(ran[i]);
1423 free(ran);
1424 free(left);
1425 free(right);
1426 isl_space_free(space);
1427 return NULL;
1430 /* Given an array of sets "set", add "dom" at position "pos"
1431 * and search for elements at earlier positions that overlap with "dom".
1432 * If any can be found, then merge all of them, together with "dom", into
1433 * a single set and assign the union to the first in the array,
1434 * which becomes the new group leader for all groups involved in the merge.
1435 * During the search, we only consider group leaders, i.e., those with
1436 * group[i] = i, as the other sets have already been combined
1437 * with one of the group leaders.
1439 static int merge(isl_set **set, int *group, __isl_take isl_set *dom, int pos)
1441 int i;
1443 group[pos] = pos;
1444 set[pos] = isl_set_copy(dom);
1446 for (i = pos - 1; i >= 0; --i) {
1447 isl_bool o;
1449 if (group[i] != i)
1450 continue;
1452 o = isl_set_overlaps(set[i], dom);
1453 if (o < 0)
1454 goto error;
1455 if (!o)
1456 continue;
1458 set[i] = isl_set_union(set[i], set[group[pos]]);
1459 set[group[pos]] = NULL;
1460 if (!set[i])
1461 goto error;
1462 group[group[pos]] = i;
1463 group[pos] = i;
1466 isl_set_free(dom);
1467 return 0;
1468 error:
1469 isl_set_free(dom);
1470 return -1;
1473 /* Replace each entry in the n by n grid of maps by the cross product
1474 * with the relation { [i] -> [i + 1] }.
1476 static int add_length(__isl_keep isl_map *map, isl_map ***grid, int n)
1478 int i, j, k;
1479 isl_space *dim;
1480 isl_basic_map *bstep;
1481 isl_map *step;
1482 unsigned nparam;
1484 if (!map)
1485 return -1;
1487 dim = isl_map_get_space(map);
1488 nparam = isl_space_dim(dim, isl_dim_param);
1489 dim = isl_space_drop_dims(dim, isl_dim_in, 0, isl_space_dim(dim, isl_dim_in));
1490 dim = isl_space_drop_dims(dim, isl_dim_out, 0, isl_space_dim(dim, isl_dim_out));
1491 dim = isl_space_add_dims(dim, isl_dim_in, 1);
1492 dim = isl_space_add_dims(dim, isl_dim_out, 1);
1493 bstep = isl_basic_map_alloc_space(dim, 0, 1, 0);
1494 k = isl_basic_map_alloc_equality(bstep);
1495 if (k < 0) {
1496 isl_basic_map_free(bstep);
1497 return -1;
1499 isl_seq_clr(bstep->eq[k], 1 + isl_basic_map_total_dim(bstep));
1500 isl_int_set_si(bstep->eq[k][0], 1);
1501 isl_int_set_si(bstep->eq[k][1 + nparam], 1);
1502 isl_int_set_si(bstep->eq[k][1 + nparam + 1], -1);
1503 bstep = isl_basic_map_finalize(bstep);
1504 step = isl_map_from_basic_map(bstep);
1506 for (i = 0; i < n; ++i)
1507 for (j = 0; j < n; ++j)
1508 grid[i][j] = isl_map_product(grid[i][j],
1509 isl_map_copy(step));
1511 isl_map_free(step);
1513 return 0;
1516 /* The core of the Floyd-Warshall algorithm.
1517 * Updates the given n x x matrix of relations in place.
1519 * The algorithm iterates over all vertices. In each step, the whole
1520 * matrix is updated to include all paths that go to the current vertex,
1521 * possibly stay there a while (including passing through earlier vertices)
1522 * and then come back. At the start of each iteration, the diagonal
1523 * element corresponding to the current vertex is replaced by its
1524 * transitive closure to account for all indirect paths that stay
1525 * in the current vertex.
1527 static void floyd_warshall_iterate(isl_map ***grid, int n, int *exact)
1529 int r, p, q;
1531 for (r = 0; r < n; ++r) {
1532 int r_exact;
1533 grid[r][r] = isl_map_transitive_closure(grid[r][r],
1534 (exact && *exact) ? &r_exact : NULL);
1535 if (exact && *exact && !r_exact)
1536 *exact = 0;
1538 for (p = 0; p < n; ++p)
1539 for (q = 0; q < n; ++q) {
1540 isl_map *loop;
1541 if (p == r && q == r)
1542 continue;
1543 loop = isl_map_apply_range(
1544 isl_map_copy(grid[p][r]),
1545 isl_map_copy(grid[r][q]));
1546 grid[p][q] = isl_map_union(grid[p][q], loop);
1547 loop = isl_map_apply_range(
1548 isl_map_copy(grid[p][r]),
1549 isl_map_apply_range(
1550 isl_map_copy(grid[r][r]),
1551 isl_map_copy(grid[r][q])));
1552 grid[p][q] = isl_map_union(grid[p][q], loop);
1553 grid[p][q] = isl_map_coalesce(grid[p][q]);
1558 /* Given a partition of the domains and ranges of the basic maps in "map",
1559 * apply the Floyd-Warshall algorithm with the elements in the partition
1560 * as vertices.
1562 * In particular, there are "n" elements in the partition and "group" is
1563 * an array of length 2 * map->n with entries in [0,n-1].
1565 * We first construct a matrix of relations based on the partition information,
1566 * apply Floyd-Warshall on this matrix of relations and then take the
1567 * union of all entries in the matrix as the final result.
1569 * If we are actually computing the power instead of the transitive closure,
1570 * i.e., when "project" is not set, then the result should have the
1571 * path lengths encoded as the difference between an extra pair of
1572 * coordinates. We therefore apply the nested transitive closures
1573 * to relations that include these lengths. In particular, we replace
1574 * the input relation by the cross product with the unit length relation
1575 * { [i] -> [i + 1] }.
1577 static __isl_give isl_map *floyd_warshall_with_groups(
1578 __isl_take isl_space *space, __isl_keep isl_map *map,
1579 int *exact, int project, int *group, int n)
1581 int i, j, k;
1582 isl_map ***grid = NULL;
1583 isl_map *app;
1585 if (!map)
1586 goto error;
1588 if (n == 1) {
1589 free(group);
1590 return incremental_closure(space, map, exact, project);
1593 grid = isl_calloc_array(map->ctx, isl_map **, n);
1594 if (!grid)
1595 goto error;
1596 for (i = 0; i < n; ++i) {
1597 grid[i] = isl_calloc_array(map->ctx, isl_map *, n);
1598 if (!grid[i])
1599 goto error;
1600 for (j = 0; j < n; ++j)
1601 grid[i][j] = isl_map_empty(isl_map_get_space(map));
1604 for (k = 0; k < map->n; ++k) {
1605 i = group[2 * k];
1606 j = group[2 * k + 1];
1607 grid[i][j] = isl_map_union(grid[i][j],
1608 isl_map_from_basic_map(
1609 isl_basic_map_copy(map->p[k])));
1612 if (!project && add_length(map, grid, n) < 0)
1613 goto error;
1615 floyd_warshall_iterate(grid, n, exact);
1617 app = isl_map_empty(isl_map_get_space(grid[0][0]));
1619 for (i = 0; i < n; ++i) {
1620 for (j = 0; j < n; ++j)
1621 app = isl_map_union(app, grid[i][j]);
1622 free(grid[i]);
1624 free(grid);
1626 free(group);
1627 isl_space_free(space);
1629 return app;
1630 error:
1631 if (grid)
1632 for (i = 0; i < n; ++i) {
1633 if (!grid[i])
1634 continue;
1635 for (j = 0; j < n; ++j)
1636 isl_map_free(grid[i][j]);
1637 free(grid[i]);
1639 free(grid);
1640 free(group);
1641 isl_space_free(space);
1642 return NULL;
1645 /* Partition the domains and ranges of the n basic relations in list
1646 * into disjoint cells.
1648 * To find the partition, we simply consider all of the domains
1649 * and ranges in turn and combine those that overlap.
1650 * "set" contains the partition elements and "group" indicates
1651 * to which partition element a given domain or range belongs.
1652 * The domain of basic map i corresponds to element 2 * i in these arrays,
1653 * while the domain corresponds to element 2 * i + 1.
1654 * During the construction group[k] is either equal to k,
1655 * in which case set[k] contains the union of all the domains and
1656 * ranges in the corresponding group, or is equal to some l < k,
1657 * with l another domain or range in the same group.
1659 static int *setup_groups(isl_ctx *ctx, __isl_keep isl_basic_map **list, int n,
1660 isl_set ***set, int *n_group)
1662 int i;
1663 int *group = NULL;
1664 int g;
1666 *set = isl_calloc_array(ctx, isl_set *, 2 * n);
1667 group = isl_alloc_array(ctx, int, 2 * n);
1669 if (!*set || !group)
1670 goto error;
1672 for (i = 0; i < n; ++i) {
1673 isl_set *dom;
1674 dom = isl_set_from_basic_set(isl_basic_map_domain(
1675 isl_basic_map_copy(list[i])));
1676 if (merge(*set, group, dom, 2 * i) < 0)
1677 goto error;
1678 dom = isl_set_from_basic_set(isl_basic_map_range(
1679 isl_basic_map_copy(list[i])));
1680 if (merge(*set, group, dom, 2 * i + 1) < 0)
1681 goto error;
1684 g = 0;
1685 for (i = 0; i < 2 * n; ++i)
1686 if (group[i] == i) {
1687 if (g != i) {
1688 (*set)[g] = (*set)[i];
1689 (*set)[i] = NULL;
1691 group[i] = g++;
1692 } else
1693 group[i] = group[group[i]];
1695 *n_group = g;
1697 return group;
1698 error:
1699 if (*set) {
1700 for (i = 0; i < 2 * n; ++i)
1701 isl_set_free((*set)[i]);
1702 free(*set);
1703 *set = NULL;
1705 free(group);
1706 return NULL;
1709 /* Check if the domains and ranges of the basic maps in "map" can
1710 * be partitioned, and if so, apply Floyd-Warshall on the elements
1711 * of the partition. Note that we also apply this algorithm
1712 * if we want to compute the power, i.e., when "project" is not set.
1713 * However, the results are unlikely to be exact since the recursive
1714 * calls inside the Floyd-Warshall algorithm typically result in
1715 * non-linear path lengths quite quickly.
1717 static __isl_give isl_map *floyd_warshall(__isl_take isl_space *dim,
1718 __isl_keep isl_map *map, int *exact, int project)
1720 int i;
1721 isl_set **set = NULL;
1722 int *group = NULL;
1723 int n;
1725 if (!map)
1726 goto error;
1727 if (map->n <= 1)
1728 return incremental_closure(dim, map, exact, project);
1730 group = setup_groups(map->ctx, map->p, map->n, &set, &n);
1731 if (!group)
1732 goto error;
1734 for (i = 0; i < 2 * map->n; ++i)
1735 isl_set_free(set[i]);
1737 free(set);
1739 return floyd_warshall_with_groups(dim, map, exact, project, group, n);
1740 error:
1741 isl_space_free(dim);
1742 return NULL;
1745 /* Structure for representing the nodes of the graph of which
1746 * strongly connected components are being computed.
1748 * list contains the actual nodes
1749 * check_closed is set if we may have used the fact that
1750 * a pair of basic maps can be interchanged
1752 struct isl_tc_follows_data {
1753 isl_basic_map **list;
1754 int check_closed;
1757 /* Check whether in the computation of the transitive closure
1758 * "list[i]" (R_1) should follow (or be part of the same component as)
1759 * "list[j]" (R_2).
1761 * That is check whether
1763 * R_1 \circ R_2
1765 * is a subset of
1767 * R_2 \circ R_1
1769 * If so, then there is no reason for R_1 to immediately follow R_2
1770 * in any path.
1772 * *check_closed is set if the subset relation holds while
1773 * R_1 \circ R_2 is not empty.
1775 static isl_bool basic_map_follows(int i, int j, void *user)
1777 struct isl_tc_follows_data *data = user;
1778 struct isl_map *map12 = NULL;
1779 struct isl_map *map21 = NULL;
1780 isl_bool subset;
1782 if (!isl_space_tuple_is_equal(data->list[i]->dim, isl_dim_in,
1783 data->list[j]->dim, isl_dim_out))
1784 return isl_bool_false;
1786 map21 = isl_map_from_basic_map(
1787 isl_basic_map_apply_range(
1788 isl_basic_map_copy(data->list[j]),
1789 isl_basic_map_copy(data->list[i])));
1790 subset = isl_map_is_empty(map21);
1791 if (subset < 0)
1792 goto error;
1793 if (subset) {
1794 isl_map_free(map21);
1795 return isl_bool_false;
1798 if (!isl_space_tuple_is_equal(data->list[i]->dim, isl_dim_in,
1799 data->list[i]->dim, isl_dim_out) ||
1800 !isl_space_tuple_is_equal(data->list[j]->dim, isl_dim_in,
1801 data->list[j]->dim, isl_dim_out)) {
1802 isl_map_free(map21);
1803 return isl_bool_true;
1806 map12 = isl_map_from_basic_map(
1807 isl_basic_map_apply_range(
1808 isl_basic_map_copy(data->list[i]),
1809 isl_basic_map_copy(data->list[j])));
1811 subset = isl_map_is_subset(map21, map12);
1813 isl_map_free(map12);
1814 isl_map_free(map21);
1816 if (subset)
1817 data->check_closed = 1;
1819 return subset < 0 ? isl_bool_error : !subset;
1820 error:
1821 isl_map_free(map21);
1822 return isl_bool_error;
1825 /* Given a union of basic maps R = \cup_i R_i \subseteq D \times D
1826 * and a dimension specification (Z^{n+1} -> Z^{n+1}),
1827 * construct a map that is an overapproximation of the map
1828 * that takes an element from the dom R \times Z to an
1829 * element from ran R \times Z, such that the first n coordinates of the
1830 * difference between them is a sum of differences between images
1831 * and pre-images in one of the R_i and such that the last coordinate
1832 * is equal to the number of steps taken.
1833 * If "project" is set, then these final coordinates are not included,
1834 * i.e., a relation of type Z^n -> Z^n is returned.
1835 * That is, let
1837 * \Delta_i = { y - x | (x, y) in R_i }
1839 * then the constructed map is an overapproximation of
1841 * { (x) -> (x + d) | \exists k_i >= 0, \delta_i \in \Delta_i :
1842 * d = (\sum_i k_i \delta_i, \sum_i k_i) and
1843 * x in dom R and x + d in ran R }
1845 * or
1847 * { (x) -> (x + d) | \exists k_i >= 0, \delta_i \in \Delta_i :
1848 * d = (\sum_i k_i \delta_i) and
1849 * x in dom R and x + d in ran R }
1851 * if "project" is set.
1853 * We first split the map into strongly connected components, perform
1854 * the above on each component and then join the results in the correct
1855 * order, at each join also taking in the union of both arguments
1856 * to allow for paths that do not go through one of the two arguments.
1858 static __isl_give isl_map *construct_power_components(__isl_take isl_space *dim,
1859 __isl_keep isl_map *map, int *exact, int project)
1861 int i, n, c;
1862 struct isl_map *path = NULL;
1863 struct isl_tc_follows_data data;
1864 struct isl_tarjan_graph *g = NULL;
1865 int *orig_exact;
1866 int local_exact;
1868 if (!map)
1869 goto error;
1870 if (map->n <= 1)
1871 return floyd_warshall(dim, map, exact, project);
1873 data.list = map->p;
1874 data.check_closed = 0;
1875 g = isl_tarjan_graph_init(map->ctx, map->n, &basic_map_follows, &data);
1876 if (!g)
1877 goto error;
1879 orig_exact = exact;
1880 if (data.check_closed && !exact)
1881 exact = &local_exact;
1883 c = 0;
1884 i = 0;
1885 n = map->n;
1886 if (project)
1887 path = isl_map_empty(isl_map_get_space(map));
1888 else
1889 path = isl_map_empty(isl_space_copy(dim));
1890 path = anonymize(path);
1891 while (n) {
1892 struct isl_map *comp;
1893 isl_map *path_comp, *path_comb;
1894 comp = isl_map_alloc_space(isl_map_get_space(map), n, 0);
1895 while (g->order[i] != -1) {
1896 comp = isl_map_add_basic_map(comp,
1897 isl_basic_map_copy(map->p[g->order[i]]));
1898 --n;
1899 ++i;
1901 path_comp = floyd_warshall(isl_space_copy(dim),
1902 comp, exact, project);
1903 path_comp = anonymize(path_comp);
1904 path_comb = isl_map_apply_range(isl_map_copy(path),
1905 isl_map_copy(path_comp));
1906 path = isl_map_union(path, path_comp);
1907 path = isl_map_union(path, path_comb);
1908 isl_map_free(comp);
1909 ++i;
1910 ++c;
1913 if (c > 1 && data.check_closed && !*exact) {
1914 int closed;
1916 closed = isl_map_is_transitively_closed(path);
1917 if (closed < 0)
1918 goto error;
1919 if (!closed) {
1920 isl_tarjan_graph_free(g);
1921 isl_map_free(path);
1922 return floyd_warshall(dim, map, orig_exact, project);
1926 isl_tarjan_graph_free(g);
1927 isl_space_free(dim);
1929 return path;
1930 error:
1931 isl_tarjan_graph_free(g);
1932 isl_space_free(dim);
1933 isl_map_free(path);
1934 return NULL;
1937 /* Given a union of basic maps R = \cup_i R_i \subseteq D \times D,
1938 * construct a map that is an overapproximation of the map
1939 * that takes an element from the space D to another
1940 * element from the same space, such that the difference between
1941 * them is a strictly positive sum of differences between images
1942 * and pre-images in one of the R_i.
1943 * The number of differences in the sum is equated to parameter "param".
1944 * That is, let
1946 * \Delta_i = { y - x | (x, y) in R_i }
1948 * then the constructed map is an overapproximation of
1950 * { (x) -> (x + d) | \exists k_i >= 0, \delta_i \in \Delta_i :
1951 * d = \sum_i k_i \delta_i and k = \sum_i k_i > 0 }
1952 * or
1954 * { (x) -> (x + d) | \exists k_i >= 0, \delta_i \in \Delta_i :
1955 * d = \sum_i k_i \delta_i and \sum_i k_i > 0 }
1957 * if "project" is set.
1959 * If "project" is not set, then
1960 * we construct an extended mapping with an extra coordinate
1961 * that indicates the number of steps taken. In particular,
1962 * the difference in the last coordinate is equal to the number
1963 * of steps taken to move from a domain element to the corresponding
1964 * image element(s).
1966 static __isl_give isl_map *construct_power(__isl_keep isl_map *map,
1967 int *exact, int project)
1969 struct isl_map *app = NULL;
1970 isl_space *dim = NULL;
1972 if (!map)
1973 return NULL;
1975 dim = isl_map_get_space(map);
1977 dim = isl_space_add_dims(dim, isl_dim_in, 1);
1978 dim = isl_space_add_dims(dim, isl_dim_out, 1);
1980 app = construct_power_components(isl_space_copy(dim), map,
1981 exact, project);
1983 isl_space_free(dim);
1985 return app;
1988 /* Compute the positive powers of "map", or an overapproximation.
1989 * If the result is exact, then *exact is set to 1.
1991 * If project is set, then we are actually interested in the transitive
1992 * closure, so we can use a more relaxed exactness check.
1993 * The lengths of the paths are also projected out instead of being
1994 * encoded as the difference between an extra pair of final coordinates.
1996 static __isl_give isl_map *map_power(__isl_take isl_map *map,
1997 int *exact, int project)
1999 struct isl_map *app = NULL;
2001 if (exact)
2002 *exact = 1;
2004 if (!map)
2005 return NULL;
2007 isl_assert(map->ctx,
2008 isl_map_dim(map, isl_dim_in) == isl_map_dim(map, isl_dim_out),
2009 goto error);
2011 app = construct_power(map, exact, project);
2013 isl_map_free(map);
2014 return app;
2015 error:
2016 isl_map_free(map);
2017 isl_map_free(app);
2018 return NULL;
2021 /* Compute the positive powers of "map", or an overapproximation.
2022 * The result maps the exponent to a nested copy of the corresponding power.
2023 * If the result is exact, then *exact is set to 1.
2024 * map_power constructs an extended relation with the path lengths
2025 * encoded as the difference between the final coordinates.
2026 * In the final step, this difference is equated to an extra parameter
2027 * and made positive. The extra coordinates are subsequently projected out
2028 * and the parameter is turned into the domain of the result.
2030 __isl_give isl_map *isl_map_power(__isl_take isl_map *map, int *exact)
2032 isl_space *target_dim;
2033 isl_space *dim;
2034 isl_map *diff;
2035 unsigned d;
2036 unsigned param;
2038 if (!map)
2039 return NULL;
2041 d = isl_map_dim(map, isl_dim_in);
2042 param = isl_map_dim(map, isl_dim_param);
2044 map = isl_map_compute_divs(map);
2045 map = isl_map_coalesce(map);
2047 if (isl_map_plain_is_empty(map)) {
2048 map = isl_map_from_range(isl_map_wrap(map));
2049 map = isl_map_add_dims(map, isl_dim_in, 1);
2050 map = isl_map_set_dim_name(map, isl_dim_in, 0, "k");
2051 return map;
2054 target_dim = isl_map_get_space(map);
2055 target_dim = isl_space_from_range(isl_space_wrap(target_dim));
2056 target_dim = isl_space_add_dims(target_dim, isl_dim_in, 1);
2057 target_dim = isl_space_set_dim_name(target_dim, isl_dim_in, 0, "k");
2059 map = map_power(map, exact, 0);
2061 map = isl_map_add_dims(map, isl_dim_param, 1);
2062 dim = isl_map_get_space(map);
2063 diff = equate_parameter_to_length(dim, param);
2064 map = isl_map_intersect(map, diff);
2065 map = isl_map_project_out(map, isl_dim_in, d, 1);
2066 map = isl_map_project_out(map, isl_dim_out, d, 1);
2067 map = isl_map_from_range(isl_map_wrap(map));
2068 map = isl_map_move_dims(map, isl_dim_in, 0, isl_dim_param, param, 1);
2070 map = isl_map_reset_space(map, target_dim);
2072 return map;
2075 /* Compute a relation that maps each element in the range of the input
2076 * relation to the lengths of all paths composed of edges in the input
2077 * relation that end up in the given range element.
2078 * The result may be an overapproximation, in which case *exact is set to 0.
2079 * The resulting relation is very similar to the power relation.
2080 * The difference are that the domain has been projected out, the
2081 * range has become the domain and the exponent is the range instead
2082 * of a parameter.
2084 __isl_give isl_map *isl_map_reaching_path_lengths(__isl_take isl_map *map,
2085 int *exact)
2087 isl_space *dim;
2088 isl_map *diff;
2089 unsigned d;
2090 unsigned param;
2092 if (!map)
2093 return NULL;
2095 d = isl_map_dim(map, isl_dim_in);
2096 param = isl_map_dim(map, isl_dim_param);
2098 map = isl_map_compute_divs(map);
2099 map = isl_map_coalesce(map);
2101 if (isl_map_plain_is_empty(map)) {
2102 if (exact)
2103 *exact = 1;
2104 map = isl_map_project_out(map, isl_dim_out, 0, d);
2105 map = isl_map_add_dims(map, isl_dim_out, 1);
2106 return map;
2109 map = map_power(map, exact, 0);
2111 map = isl_map_add_dims(map, isl_dim_param, 1);
2112 dim = isl_map_get_space(map);
2113 diff = equate_parameter_to_length(dim, param);
2114 map = isl_map_intersect(map, diff);
2115 map = isl_map_project_out(map, isl_dim_in, 0, d + 1);
2116 map = isl_map_project_out(map, isl_dim_out, d, 1);
2117 map = isl_map_reverse(map);
2118 map = isl_map_move_dims(map, isl_dim_out, 0, isl_dim_param, param, 1);
2120 return map;
2123 /* Given a map, compute the smallest superset of this map that is of the form
2125 * { i -> j : L <= j - i <= U and exists a_p: j_p - i_p = M_p a_p }
2127 * (where p ranges over the (non-parametric) dimensions),
2128 * compute the transitive closure of this map, i.e.,
2130 * { i -> j : exists k > 0:
2131 * k L <= j - i <= k U and exists a: j_p - i_p = M_p a_p }
2133 * and intersect domain and range of this transitive closure with
2134 * the given domain and range.
2136 * If with_id is set, then try to include as much of the identity mapping
2137 * as possible, by computing
2139 * { i -> j : exists k >= 0:
2140 * k L <= j - i <= k U and exists a: j_p - i_p = M_p a_p }
2142 * instead (i.e., allow k = 0).
2144 * In practice, we compute the difference set
2146 * delta = { j - i | i -> j in map },
2148 * look for stride constraint on the individual dimensions and compute
2149 * (constant) lower and upper bounds for each individual dimension,
2150 * adding a constraint for each bound not equal to infinity.
2152 static __isl_give isl_map *box_closure_on_domain(__isl_take isl_map *map,
2153 __isl_take isl_set *dom, __isl_take isl_set *ran, int with_id)
2155 int i;
2156 int k;
2157 unsigned d;
2158 unsigned nparam;
2159 unsigned total;
2160 isl_space *dim;
2161 isl_set *delta;
2162 isl_map *app = NULL;
2163 isl_basic_set *aff = NULL;
2164 isl_basic_map *bmap = NULL;
2165 isl_vec *obj = NULL;
2166 isl_int opt;
2168 isl_int_init(opt);
2170 delta = isl_map_deltas(isl_map_copy(map));
2172 aff = isl_set_affine_hull(isl_set_copy(delta));
2173 if (!aff)
2174 goto error;
2175 dim = isl_map_get_space(map);
2176 d = isl_space_dim(dim, isl_dim_in);
2177 nparam = isl_space_dim(dim, isl_dim_param);
2178 total = isl_space_dim(dim, isl_dim_all);
2179 bmap = isl_basic_map_alloc_space(dim,
2180 aff->n_div + 1, aff->n_div, 2 * d + 1);
2181 for (i = 0; i < aff->n_div + 1; ++i) {
2182 k = isl_basic_map_alloc_div(bmap);
2183 if (k < 0)
2184 goto error;
2185 isl_int_set_si(bmap->div[k][0], 0);
2187 for (i = 0; i < aff->n_eq; ++i) {
2188 if (!isl_basic_set_eq_is_stride(aff, i))
2189 continue;
2190 k = isl_basic_map_alloc_equality(bmap);
2191 if (k < 0)
2192 goto error;
2193 isl_seq_clr(bmap->eq[k], 1 + nparam);
2194 isl_seq_cpy(bmap->eq[k] + 1 + nparam + d,
2195 aff->eq[i] + 1 + nparam, d);
2196 isl_seq_neg(bmap->eq[k] + 1 + nparam,
2197 aff->eq[i] + 1 + nparam, d);
2198 isl_seq_cpy(bmap->eq[k] + 1 + nparam + 2 * d,
2199 aff->eq[i] + 1 + nparam + d, aff->n_div);
2200 isl_int_set_si(bmap->eq[k][1 + total + aff->n_div], 0);
2202 obj = isl_vec_alloc(map->ctx, 1 + nparam + d);
2203 if (!obj)
2204 goto error;
2205 isl_seq_clr(obj->el, 1 + nparam + d);
2206 for (i = 0; i < d; ++ i) {
2207 enum isl_lp_result res;
2209 isl_int_set_si(obj->el[1 + nparam + i], 1);
2211 res = isl_set_solve_lp(delta, 0, obj->el, map->ctx->one, &opt,
2212 NULL, NULL);
2213 if (res == isl_lp_error)
2214 goto error;
2215 if (res == isl_lp_ok) {
2216 k = isl_basic_map_alloc_inequality(bmap);
2217 if (k < 0)
2218 goto error;
2219 isl_seq_clr(bmap->ineq[k],
2220 1 + nparam + 2 * d + bmap->n_div);
2221 isl_int_set_si(bmap->ineq[k][1 + nparam + i], -1);
2222 isl_int_set_si(bmap->ineq[k][1 + nparam + d + i], 1);
2223 isl_int_neg(bmap->ineq[k][1 + nparam + 2 * d + aff->n_div], opt);
2226 res = isl_set_solve_lp(delta, 1, obj->el, map->ctx->one, &opt,
2227 NULL, NULL);
2228 if (res == isl_lp_error)
2229 goto error;
2230 if (res == isl_lp_ok) {
2231 k = isl_basic_map_alloc_inequality(bmap);
2232 if (k < 0)
2233 goto error;
2234 isl_seq_clr(bmap->ineq[k],
2235 1 + nparam + 2 * d + bmap->n_div);
2236 isl_int_set_si(bmap->ineq[k][1 + nparam + i], 1);
2237 isl_int_set_si(bmap->ineq[k][1 + nparam + d + i], -1);
2238 isl_int_set(bmap->ineq[k][1 + nparam + 2 * d + aff->n_div], opt);
2241 isl_int_set_si(obj->el[1 + nparam + i], 0);
2243 k = isl_basic_map_alloc_inequality(bmap);
2244 if (k < 0)
2245 goto error;
2246 isl_seq_clr(bmap->ineq[k],
2247 1 + nparam + 2 * d + bmap->n_div);
2248 if (!with_id)
2249 isl_int_set_si(bmap->ineq[k][0], -1);
2250 isl_int_set_si(bmap->ineq[k][1 + nparam + 2 * d + aff->n_div], 1);
2252 app = isl_map_from_domain_and_range(dom, ran);
2254 isl_vec_free(obj);
2255 isl_basic_set_free(aff);
2256 isl_map_free(map);
2257 bmap = isl_basic_map_finalize(bmap);
2258 isl_set_free(delta);
2259 isl_int_clear(opt);
2261 map = isl_map_from_basic_map(bmap);
2262 map = isl_map_intersect(map, app);
2264 return map;
2265 error:
2266 isl_vec_free(obj);
2267 isl_basic_map_free(bmap);
2268 isl_basic_set_free(aff);
2269 isl_set_free(dom);
2270 isl_set_free(ran);
2271 isl_map_free(map);
2272 isl_set_free(delta);
2273 isl_int_clear(opt);
2274 return NULL;
2277 /* Given a map, compute the smallest superset of this map that is of the form
2279 * { i -> j : L <= j - i <= U and exists a_p: j_p - i_p = M_p a_p }
2281 * (where p ranges over the (non-parametric) dimensions),
2282 * compute the transitive closure of this map, i.e.,
2284 * { i -> j : exists k > 0:
2285 * k L <= j - i <= k U and exists a: j_p - i_p = M_p a_p }
2287 * and intersect domain and range of this transitive closure with
2288 * domain and range of the original map.
2290 static __isl_give isl_map *box_closure(__isl_take isl_map *map)
2292 isl_set *domain;
2293 isl_set *range;
2295 domain = isl_map_domain(isl_map_copy(map));
2296 domain = isl_set_coalesce(domain);
2297 range = isl_map_range(isl_map_copy(map));
2298 range = isl_set_coalesce(range);
2300 return box_closure_on_domain(map, domain, range, 0);
2303 /* Given a map, compute the smallest superset of this map that is of the form
2305 * { i -> j : L <= j - i <= U and exists a_p: j_p - i_p = M_p a_p }
2307 * (where p ranges over the (non-parametric) dimensions),
2308 * compute the transitive and partially reflexive closure of this map, i.e.,
2310 * { i -> j : exists k >= 0:
2311 * k L <= j - i <= k U and exists a: j_p - i_p = M_p a_p }
2313 * and intersect domain and range of this transitive closure with
2314 * the given domain.
2316 static __isl_give isl_map *box_closure_with_identity(__isl_take isl_map *map,
2317 __isl_take isl_set *dom)
2319 return box_closure_on_domain(map, dom, isl_set_copy(dom), 1);
2322 /* Check whether app is the transitive closure of map.
2323 * In particular, check that app is acyclic and, if so,
2324 * check that
2326 * app \subset (map \cup (map \circ app))
2328 static int check_exactness_omega(__isl_keep isl_map *map,
2329 __isl_keep isl_map *app)
2331 isl_set *delta;
2332 int i;
2333 int is_empty, is_exact;
2334 unsigned d;
2335 isl_map *test;
2337 delta = isl_map_deltas(isl_map_copy(app));
2338 d = isl_set_dim(delta, isl_dim_set);
2339 for (i = 0; i < d; ++i)
2340 delta = isl_set_fix_si(delta, isl_dim_set, i, 0);
2341 is_empty = isl_set_is_empty(delta);
2342 isl_set_free(delta);
2343 if (is_empty < 0)
2344 return -1;
2345 if (!is_empty)
2346 return 0;
2348 test = isl_map_apply_range(isl_map_copy(app), isl_map_copy(map));
2349 test = isl_map_union(test, isl_map_copy(map));
2350 is_exact = isl_map_is_subset(app, test);
2351 isl_map_free(test);
2353 return is_exact;
2356 /* Check if basic map M_i can be combined with all the other
2357 * basic maps such that
2359 * (\cup_j M_j)^+
2361 * can be computed as
2363 * M_i \cup (\cup_{j \ne i} M_i^* \circ M_j \circ M_i^*)^+
2365 * In particular, check if we can compute a compact representation
2366 * of
2368 * M_i^* \circ M_j \circ M_i^*
2370 * for each j != i.
2371 * Let M_i^? be an extension of M_i^+ that allows paths
2372 * of length zero, i.e., the result of box_closure(., 1).
2373 * The criterion, as proposed by Kelly et al., is that
2374 * id = M_i^? - M_i^+ can be represented as a basic map
2375 * and that
2377 * id \circ M_j \circ id = M_j
2379 * for each j != i.
2381 * If this function returns 1, then tc and qc are set to
2382 * M_i^+ and M_i^?, respectively.
2384 static int can_be_split_off(__isl_keep isl_map *map, int i,
2385 __isl_give isl_map **tc, __isl_give isl_map **qc)
2387 isl_map *map_i, *id = NULL;
2388 int j = -1;
2389 isl_set *C;
2391 *tc = NULL;
2392 *qc = NULL;
2394 C = isl_set_union(isl_map_domain(isl_map_copy(map)),
2395 isl_map_range(isl_map_copy(map)));
2396 C = isl_set_from_basic_set(isl_set_simple_hull(C));
2397 if (!C)
2398 goto error;
2400 map_i = isl_map_from_basic_map(isl_basic_map_copy(map->p[i]));
2401 *tc = box_closure(isl_map_copy(map_i));
2402 *qc = box_closure_with_identity(map_i, C);
2403 id = isl_map_subtract(isl_map_copy(*qc), isl_map_copy(*tc));
2405 if (!id || !*qc)
2406 goto error;
2407 if (id->n != 1 || (*qc)->n != 1)
2408 goto done;
2410 for (j = 0; j < map->n; ++j) {
2411 isl_map *map_j, *test;
2412 int is_ok;
2414 if (i == j)
2415 continue;
2416 map_j = isl_map_from_basic_map(
2417 isl_basic_map_copy(map->p[j]));
2418 test = isl_map_apply_range(isl_map_copy(id),
2419 isl_map_copy(map_j));
2420 test = isl_map_apply_range(test, isl_map_copy(id));
2421 is_ok = isl_map_is_equal(test, map_j);
2422 isl_map_free(map_j);
2423 isl_map_free(test);
2424 if (is_ok < 0)
2425 goto error;
2426 if (!is_ok)
2427 break;
2430 done:
2431 isl_map_free(id);
2432 if (j == map->n)
2433 return 1;
2435 isl_map_free(*qc);
2436 isl_map_free(*tc);
2437 *qc = NULL;
2438 *tc = NULL;
2440 return 0;
2441 error:
2442 isl_map_free(id);
2443 isl_map_free(*qc);
2444 isl_map_free(*tc);
2445 *qc = NULL;
2446 *tc = NULL;
2447 return -1;
2450 static __isl_give isl_map *box_closure_with_check(__isl_take isl_map *map,
2451 int *exact)
2453 isl_map *app;
2455 app = box_closure(isl_map_copy(map));
2456 if (exact)
2457 *exact = check_exactness_omega(map, app);
2459 isl_map_free(map);
2460 return app;
2463 /* Compute an overapproximation of the transitive closure of "map"
2464 * using a variation of the algorithm from
2465 * "Transitive Closure of Infinite Graphs and its Applications"
2466 * by Kelly et al.
2468 * We first check whether we can can split of any basic map M_i and
2469 * compute
2471 * (\cup_j M_j)^+
2473 * as
2475 * M_i \cup (\cup_{j \ne i} M_i^* \circ M_j \circ M_i^*)^+
2477 * using a recursive call on the remaining map.
2479 * If not, we simply call box_closure on the whole map.
2481 static __isl_give isl_map *transitive_closure_omega(__isl_take isl_map *map,
2482 int *exact)
2484 int i, j;
2485 int exact_i;
2486 isl_map *app;
2488 if (!map)
2489 return NULL;
2490 if (map->n == 1)
2491 return box_closure_with_check(map, exact);
2493 for (i = 0; i < map->n; ++i) {
2494 int ok;
2495 isl_map *qc, *tc;
2496 ok = can_be_split_off(map, i, &tc, &qc);
2497 if (ok < 0)
2498 goto error;
2499 if (!ok)
2500 continue;
2502 app = isl_map_alloc_space(isl_map_get_space(map), map->n - 1, 0);
2504 for (j = 0; j < map->n; ++j) {
2505 if (j == i)
2506 continue;
2507 app = isl_map_add_basic_map(app,
2508 isl_basic_map_copy(map->p[j]));
2511 app = isl_map_apply_range(isl_map_copy(qc), app);
2512 app = isl_map_apply_range(app, qc);
2514 app = isl_map_union(tc, transitive_closure_omega(app, NULL));
2515 exact_i = check_exactness_omega(map, app);
2516 if (exact_i == 1) {
2517 if (exact)
2518 *exact = exact_i;
2519 isl_map_free(map);
2520 return app;
2522 isl_map_free(app);
2523 if (exact_i < 0)
2524 goto error;
2527 return box_closure_with_check(map, exact);
2528 error:
2529 isl_map_free(map);
2530 return NULL;
2533 /* Compute the transitive closure of "map", or an overapproximation.
2534 * If the result is exact, then *exact is set to 1.
2535 * Simply use map_power to compute the powers of map, but tell
2536 * it to project out the lengths of the paths instead of equating
2537 * the length to a parameter.
2539 __isl_give isl_map *isl_map_transitive_closure(__isl_take isl_map *map,
2540 int *exact)
2542 isl_space *target_dim;
2543 int closed;
2545 if (!map)
2546 goto error;
2548 if (map->ctx->opt->closure == ISL_CLOSURE_BOX)
2549 return transitive_closure_omega(map, exact);
2551 map = isl_map_compute_divs(map);
2552 map = isl_map_coalesce(map);
2553 closed = isl_map_is_transitively_closed(map);
2554 if (closed < 0)
2555 goto error;
2556 if (closed) {
2557 if (exact)
2558 *exact = 1;
2559 return map;
2562 target_dim = isl_map_get_space(map);
2563 map = map_power(map, exact, 1);
2564 map = isl_map_reset_space(map, target_dim);
2566 return map;
2567 error:
2568 isl_map_free(map);
2569 return NULL;
2572 static isl_stat inc_count(__isl_take isl_map *map, void *user)
2574 int *n = user;
2576 *n += map->n;
2578 isl_map_free(map);
2580 return isl_stat_ok;
2583 static isl_stat collect_basic_map(__isl_take isl_map *map, void *user)
2585 int i;
2586 isl_basic_map ***next = user;
2588 for (i = 0; i < map->n; ++i) {
2589 **next = isl_basic_map_copy(map->p[i]);
2590 if (!**next)
2591 goto error;
2592 (*next)++;
2595 isl_map_free(map);
2596 return isl_stat_ok;
2597 error:
2598 isl_map_free(map);
2599 return isl_stat_error;
2602 /* Perform Floyd-Warshall on the given list of basic relations.
2603 * The basic relations may live in different dimensions,
2604 * but basic relations that get assigned to the diagonal of the
2605 * grid have domains and ranges of the same dimension and so
2606 * the standard algorithm can be used because the nested transitive
2607 * closures are only applied to diagonal elements and because all
2608 * compositions are peformed on relations with compatible domains and ranges.
2610 static __isl_give isl_union_map *union_floyd_warshall_on_list(isl_ctx *ctx,
2611 __isl_keep isl_basic_map **list, int n, int *exact)
2613 int i, j, k;
2614 int n_group;
2615 int *group = NULL;
2616 isl_set **set = NULL;
2617 isl_map ***grid = NULL;
2618 isl_union_map *app;
2620 group = setup_groups(ctx, list, n, &set, &n_group);
2621 if (!group)
2622 goto error;
2624 grid = isl_calloc_array(ctx, isl_map **, n_group);
2625 if (!grid)
2626 goto error;
2627 for (i = 0; i < n_group; ++i) {
2628 grid[i] = isl_calloc_array(ctx, isl_map *, n_group);
2629 if (!grid[i])
2630 goto error;
2631 for (j = 0; j < n_group; ++j) {
2632 isl_space *dim1, *dim2, *dim;
2633 dim1 = isl_space_reverse(isl_set_get_space(set[i]));
2634 dim2 = isl_set_get_space(set[j]);
2635 dim = isl_space_join(dim1, dim2);
2636 grid[i][j] = isl_map_empty(dim);
2640 for (k = 0; k < n; ++k) {
2641 i = group[2 * k];
2642 j = group[2 * k + 1];
2643 grid[i][j] = isl_map_union(grid[i][j],
2644 isl_map_from_basic_map(
2645 isl_basic_map_copy(list[k])));
2648 floyd_warshall_iterate(grid, n_group, exact);
2650 app = isl_union_map_empty(isl_map_get_space(grid[0][0]));
2652 for (i = 0; i < n_group; ++i) {
2653 for (j = 0; j < n_group; ++j)
2654 app = isl_union_map_add_map(app, grid[i][j]);
2655 free(grid[i]);
2657 free(grid);
2659 for (i = 0; i < 2 * n; ++i)
2660 isl_set_free(set[i]);
2661 free(set);
2663 free(group);
2664 return app;
2665 error:
2666 if (grid)
2667 for (i = 0; i < n_group; ++i) {
2668 if (!grid[i])
2669 continue;
2670 for (j = 0; j < n_group; ++j)
2671 isl_map_free(grid[i][j]);
2672 free(grid[i]);
2674 free(grid);
2675 if (set) {
2676 for (i = 0; i < 2 * n; ++i)
2677 isl_set_free(set[i]);
2678 free(set);
2680 free(group);
2681 return NULL;
2684 /* Perform Floyd-Warshall on the given union relation.
2685 * The implementation is very similar to that for non-unions.
2686 * The main difference is that it is applied unconditionally.
2687 * We first extract a list of basic maps from the union map
2688 * and then perform the algorithm on this list.
2690 static __isl_give isl_union_map *union_floyd_warshall(
2691 __isl_take isl_union_map *umap, int *exact)
2693 int i, n;
2694 isl_ctx *ctx;
2695 isl_basic_map **list = NULL;
2696 isl_basic_map **next;
2697 isl_union_map *res;
2699 n = 0;
2700 if (isl_union_map_foreach_map(umap, inc_count, &n) < 0)
2701 goto error;
2703 ctx = isl_union_map_get_ctx(umap);
2704 list = isl_calloc_array(ctx, isl_basic_map *, n);
2705 if (!list)
2706 goto error;
2708 next = list;
2709 if (isl_union_map_foreach_map(umap, collect_basic_map, &next) < 0)
2710 goto error;
2712 res = union_floyd_warshall_on_list(ctx, list, n, exact);
2714 if (list) {
2715 for (i = 0; i < n; ++i)
2716 isl_basic_map_free(list[i]);
2717 free(list);
2720 isl_union_map_free(umap);
2721 return res;
2722 error:
2723 if (list) {
2724 for (i = 0; i < n; ++i)
2725 isl_basic_map_free(list[i]);
2726 free(list);
2728 isl_union_map_free(umap);
2729 return NULL;
2732 /* Decompose the give union relation into strongly connected components.
2733 * The implementation is essentially the same as that of
2734 * construct_power_components with the major difference that all
2735 * operations are performed on union maps.
2737 static __isl_give isl_union_map *union_components(
2738 __isl_take isl_union_map *umap, int *exact)
2740 int i;
2741 int n;
2742 isl_ctx *ctx;
2743 isl_basic_map **list = NULL;
2744 isl_basic_map **next;
2745 isl_union_map *path = NULL;
2746 struct isl_tc_follows_data data;
2747 struct isl_tarjan_graph *g = NULL;
2748 int c, l;
2749 int recheck = 0;
2751 n = 0;
2752 if (isl_union_map_foreach_map(umap, inc_count, &n) < 0)
2753 goto error;
2755 if (n == 0)
2756 return umap;
2757 if (n <= 1)
2758 return union_floyd_warshall(umap, exact);
2760 ctx = isl_union_map_get_ctx(umap);
2761 list = isl_calloc_array(ctx, isl_basic_map *, n);
2762 if (!list)
2763 goto error;
2765 next = list;
2766 if (isl_union_map_foreach_map(umap, collect_basic_map, &next) < 0)
2767 goto error;
2769 data.list = list;
2770 data.check_closed = 0;
2771 g = isl_tarjan_graph_init(ctx, n, &basic_map_follows, &data);
2772 if (!g)
2773 goto error;
2775 c = 0;
2776 i = 0;
2777 l = n;
2778 path = isl_union_map_empty(isl_union_map_get_space(umap));
2779 while (l) {
2780 isl_union_map *comp;
2781 isl_union_map *path_comp, *path_comb;
2782 comp = isl_union_map_empty(isl_union_map_get_space(umap));
2783 while (g->order[i] != -1) {
2784 comp = isl_union_map_add_map(comp,
2785 isl_map_from_basic_map(
2786 isl_basic_map_copy(list[g->order[i]])));
2787 --l;
2788 ++i;
2790 path_comp = union_floyd_warshall(comp, exact);
2791 path_comb = isl_union_map_apply_range(isl_union_map_copy(path),
2792 isl_union_map_copy(path_comp));
2793 path = isl_union_map_union(path, path_comp);
2794 path = isl_union_map_union(path, path_comb);
2795 ++i;
2796 ++c;
2799 if (c > 1 && data.check_closed && !*exact) {
2800 int closed;
2802 closed = isl_union_map_is_transitively_closed(path);
2803 if (closed < 0)
2804 goto error;
2805 recheck = !closed;
2808 isl_tarjan_graph_free(g);
2810 for (i = 0; i < n; ++i)
2811 isl_basic_map_free(list[i]);
2812 free(list);
2814 if (recheck) {
2815 isl_union_map_free(path);
2816 return union_floyd_warshall(umap, exact);
2819 isl_union_map_free(umap);
2821 return path;
2822 error:
2823 isl_tarjan_graph_free(g);
2824 if (list) {
2825 for (i = 0; i < n; ++i)
2826 isl_basic_map_free(list[i]);
2827 free(list);
2829 isl_union_map_free(umap);
2830 isl_union_map_free(path);
2831 return NULL;
2834 /* Compute the transitive closure of "umap", or an overapproximation.
2835 * If the result is exact, then *exact is set to 1.
2837 __isl_give isl_union_map *isl_union_map_transitive_closure(
2838 __isl_take isl_union_map *umap, int *exact)
2840 int closed;
2842 if (!umap)
2843 return NULL;
2845 if (exact)
2846 *exact = 1;
2848 umap = isl_union_map_compute_divs(umap);
2849 umap = isl_union_map_coalesce(umap);
2850 closed = isl_union_map_is_transitively_closed(umap);
2851 if (closed < 0)
2852 goto error;
2853 if (closed)
2854 return umap;
2855 umap = union_components(umap, exact);
2856 return umap;
2857 error:
2858 isl_union_map_free(umap);
2859 return NULL;
2862 struct isl_union_power {
2863 isl_union_map *pow;
2864 int *exact;
2867 static isl_stat power(__isl_take isl_map *map, void *user)
2869 struct isl_union_power *up = user;
2871 map = isl_map_power(map, up->exact);
2872 up->pow = isl_union_map_from_map(map);
2874 return isl_stat_error;
2877 /* Construct a map [x] -> [x+1], with parameters prescribed by "space".
2879 static __isl_give isl_union_map *increment(__isl_take isl_space *space)
2881 int k;
2882 isl_basic_map *bmap;
2884 space = isl_space_add_dims(space, isl_dim_in, 1);
2885 space = isl_space_add_dims(space, isl_dim_out, 1);
2886 bmap = isl_basic_map_alloc_space(space, 0, 1, 0);
2887 k = isl_basic_map_alloc_equality(bmap);
2888 if (k < 0)
2889 goto error;
2890 isl_seq_clr(bmap->eq[k], 1 + isl_basic_map_total_dim(bmap));
2891 isl_int_set_si(bmap->eq[k][0], 1);
2892 isl_int_set_si(bmap->eq[k][isl_basic_map_offset(bmap, isl_dim_in)], 1);
2893 isl_int_set_si(bmap->eq[k][isl_basic_map_offset(bmap, isl_dim_out)], -1);
2894 return isl_union_map_from_map(isl_map_from_basic_map(bmap));
2895 error:
2896 isl_basic_map_free(bmap);
2897 return NULL;
2900 /* Construct a map [[x]->[y]] -> [y-x], with parameters prescribed by "dim".
2902 static __isl_give isl_union_map *deltas_map(__isl_take isl_space *dim)
2904 isl_basic_map *bmap;
2906 dim = isl_space_add_dims(dim, isl_dim_in, 1);
2907 dim = isl_space_add_dims(dim, isl_dim_out, 1);
2908 bmap = isl_basic_map_universe(dim);
2909 bmap = isl_basic_map_deltas_map(bmap);
2911 return isl_union_map_from_map(isl_map_from_basic_map(bmap));
2914 /* Compute the positive powers of "map", or an overapproximation.
2915 * The result maps the exponent to a nested copy of the corresponding power.
2916 * If the result is exact, then *exact is set to 1.
2918 __isl_give isl_union_map *isl_union_map_power(__isl_take isl_union_map *umap,
2919 int *exact)
2921 int n;
2922 isl_union_map *inc;
2923 isl_union_map *dm;
2925 if (!umap)
2926 return NULL;
2927 n = isl_union_map_n_map(umap);
2928 if (n == 0)
2929 return umap;
2930 if (n == 1) {
2931 struct isl_union_power up = { NULL, exact };
2932 isl_union_map_foreach_map(umap, &power, &up);
2933 isl_union_map_free(umap);
2934 return up.pow;
2936 inc = increment(isl_union_map_get_space(umap));
2937 umap = isl_union_map_product(inc, umap);
2938 umap = isl_union_map_transitive_closure(umap, exact);
2939 umap = isl_union_map_zip(umap);
2940 dm = deltas_map(isl_union_map_get_space(umap));
2941 umap = isl_union_map_apply_domain(umap, dm);
2943 return umap;
2946 #undef TYPE
2947 #define TYPE isl_map
2948 #include "isl_power_templ.c"
2950 #undef TYPE
2951 #define TYPE isl_union_map
2952 #include "isl_power_templ.c"