isl_map.c: move_last: use isl_basic_map_offset
[isl.git] / isl_transitive_closure.c
blob5cc19e21e3ebe7b4720f261bf9706d30f18a9d9a
1 /*
2 * Copyright 2010 INRIA Saclay
4 * Use of this software is governed by the MIT license
6 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
7 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
8 * 91893 Orsay, France
9 */
11 #include <isl_ctx_private.h>
12 #include <isl_map_private.h>
13 #include <isl/map.h>
14 #include <isl_seq.h>
15 #include <isl_space_private.h>
16 #include <isl_lp_private.h>
17 #include <isl/union_map.h>
18 #include <isl_mat_private.h>
19 #include <isl_vec_private.h>
20 #include <isl_options_private.h>
21 #include <isl_tarjan.h>
23 int isl_map_is_transitively_closed(__isl_keep isl_map *map)
25 isl_map *map2;
26 int closed;
28 map2 = isl_map_apply_range(isl_map_copy(map), isl_map_copy(map));
29 closed = isl_map_is_subset(map2, map);
30 isl_map_free(map2);
32 return closed;
35 int isl_union_map_is_transitively_closed(__isl_keep isl_union_map *umap)
37 isl_union_map *umap2;
38 int closed;
40 umap2 = isl_union_map_apply_range(isl_union_map_copy(umap),
41 isl_union_map_copy(umap));
42 closed = isl_union_map_is_subset(umap2, umap);
43 isl_union_map_free(umap2);
45 return closed;
48 /* Given a map that represents a path with the length of the path
49 * encoded as the difference between the last output coordindate
50 * and the last input coordinate, set this length to either
51 * exactly "length" (if "exactly" is set) or at least "length"
52 * (if "exactly" is not set).
54 static __isl_give isl_map *set_path_length(__isl_take isl_map *map,
55 int exactly, int length)
57 isl_space *space;
58 struct isl_basic_map *bmap;
59 unsigned d;
60 unsigned nparam;
61 int k;
62 isl_int *c;
64 if (!map)
65 return NULL;
67 space = isl_map_get_space(map);
68 d = isl_space_dim(space, isl_dim_in);
69 nparam = isl_space_dim(space, isl_dim_param);
70 bmap = isl_basic_map_alloc_space(space, 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 div position "pos",
421 * check if setting the length to zero results in only the identity
422 * mapping.
424 static isl_bool empty_path_is_identity(__isl_keep isl_basic_map *path,
425 unsigned pos)
427 isl_basic_map *test = NULL;
428 isl_basic_map *id = NULL;
429 isl_bool is_id;
431 test = isl_basic_map_copy(path);
432 test = isl_basic_map_fix_si(test, isl_dim_div, pos, 0);
433 id = isl_basic_map_identity(isl_basic_map_get_space(path));
434 is_id = isl_basic_map_is_equal(test, id);
435 isl_basic_map_free(test);
436 isl_basic_map_free(id);
437 return is_id;
440 /* If any of the constraints is found to be impure then this function
441 * sets *impurity to 1.
443 * If impurity is NULL then we are dealing with a non-parametric set
444 * and so the constraints are obviously PURE_VAR.
446 static __isl_give isl_basic_map *add_delta_constraints(
447 __isl_take isl_basic_map *path,
448 __isl_keep isl_basic_set *delta, unsigned off, unsigned nparam,
449 unsigned d, int *div_purity, int eq, int *impurity)
451 int i, k;
452 int n = eq ? delta->n_eq : delta->n_ineq;
453 isl_int **delta_c = eq ? delta->eq : delta->ineq;
454 unsigned n_div;
456 n_div = isl_basic_set_dim(delta, isl_dim_div);
458 for (i = 0; i < n; ++i) {
459 isl_int *path_c;
460 int p = PURE_VAR;
461 if (impurity)
462 p = purity(delta, delta_c[i], div_purity, eq);
463 if (p < 0)
464 goto error;
465 if (p != PURE_VAR && p != PURE_PARAM && !*impurity)
466 *impurity = 1;
467 if (p == IMPURE)
468 continue;
469 if (eq && p != MIXED) {
470 k = isl_basic_map_alloc_equality(path);
471 if (k < 0)
472 goto error;
473 path_c = path->eq[k];
474 } else {
475 k = isl_basic_map_alloc_inequality(path);
476 if (k < 0)
477 goto error;
478 path_c = path->ineq[k];
480 isl_seq_clr(path_c, 1 + isl_basic_map_total_dim(path));
481 if (p == PURE_VAR) {
482 isl_seq_cpy(path_c + off,
483 delta_c[i] + 1 + nparam, d);
484 isl_int_set(path_c[off + d], delta_c[i][0]);
485 } else if (p == PURE_PARAM) {
486 isl_seq_cpy(path_c, delta_c[i], 1 + nparam);
487 } else {
488 isl_seq_cpy(path_c + off,
489 delta_c[i] + 1 + nparam, d);
490 isl_seq_cpy(path_c, delta_c[i], 1 + nparam);
492 isl_seq_cpy(path_c + off - n_div,
493 delta_c[i] + 1 + nparam + d, n_div);
496 return path;
497 error:
498 isl_basic_map_free(path);
499 return NULL;
502 /* Given a set of offsets "delta", construct a relation of the
503 * given dimension specification (Z^{n+1} -> Z^{n+1}) that
504 * is an overapproximation of the relations that
505 * maps an element x to any element that can be reached
506 * by taking a non-negative number of steps along any of
507 * the elements in "delta".
508 * That is, construct an approximation of
510 * { [x] -> [y] : exists f \in \delta, k \in Z :
511 * y = x + k [f, 1] and k >= 0 }
513 * For any element in this relation, the number of steps taken
514 * is equal to the difference in the final coordinates.
516 * In particular, let delta be defined as
518 * \delta = [p] -> { [x] : A x + a >= 0 and B p + b >= 0 and
519 * C x + C'p + c >= 0 and
520 * D x + D'p + d >= 0 }
522 * where the constraints C x + C'p + c >= 0 are such that the parametric
523 * constant term of each constraint j, "C_j x + C'_j p + c_j",
524 * can never attain positive values, then the relation is constructed as
526 * { [x] -> [y] : exists [f, k] \in Z^{n+1} : y = x + f and
527 * A f + k a >= 0 and B p + b >= 0 and
528 * C f + C'p + c >= 0 and k >= 1 }
529 * union { [x] -> [x] }
531 * If the zero-length paths happen to correspond exactly to the identity
532 * mapping, then we return
534 * { [x] -> [y] : exists [f, k] \in Z^{n+1} : y = x + f and
535 * A f + k a >= 0 and B p + b >= 0 and
536 * C f + C'p + c >= 0 and k >= 0 }
538 * instead.
540 * Existentially quantified variables in \delta are handled by
541 * classifying them as independent of the parameters, purely
542 * parameter dependent and others. Constraints containing
543 * any of the other existentially quantified variables are removed.
544 * This is safe, but leads to an additional overapproximation.
546 * If there are any impure constraints, then we also eliminate
547 * the parameters from \delta, resulting in a set
549 * \delta' = { [x] : E x + e >= 0 }
551 * and add the constraints
553 * E f + k e >= 0
555 * to the constructed relation.
557 static __isl_give isl_map *path_along_delta(__isl_take isl_space *space,
558 __isl_take isl_basic_set *delta)
560 isl_basic_map *path = NULL;
561 unsigned d;
562 unsigned n_div;
563 unsigned nparam;
564 unsigned off;
565 int i, k;
566 isl_bool is_id;
567 int *div_purity = NULL;
568 int impurity = 0;
570 if (!delta)
571 goto error;
572 n_div = isl_basic_set_dim(delta, isl_dim_div);
573 d = isl_basic_set_dim(delta, isl_dim_set);
574 nparam = isl_basic_set_dim(delta, isl_dim_param);
575 path = isl_basic_map_alloc_space(isl_space_copy(space), n_div + d + 1,
576 d + 1 + delta->n_eq, delta->n_eq + delta->n_ineq + 1);
577 off = 1 + nparam + 2 * (d + 1) + n_div;
579 for (i = 0; i < n_div + d + 1; ++i) {
580 k = isl_basic_map_alloc_div(path);
581 if (k < 0)
582 goto error;
583 isl_int_set_si(path->div[k][0], 0);
586 for (i = 0; i < d + 1; ++i) {
587 k = isl_basic_map_alloc_equality(path);
588 if (k < 0)
589 goto error;
590 isl_seq_clr(path->eq[k], 1 + isl_basic_map_total_dim(path));
591 isl_int_set_si(path->eq[k][1 + nparam + i], 1);
592 isl_int_set_si(path->eq[k][1 + nparam + d + 1 + i], -1);
593 isl_int_set_si(path->eq[k][off + i], 1);
596 div_purity = get_div_purity(delta);
597 if (n_div && !div_purity)
598 goto error;
600 path = add_delta_constraints(path, delta, off, nparam, d,
601 div_purity, 1, &impurity);
602 path = add_delta_constraints(path, delta, off, nparam, d,
603 div_purity, 0, &impurity);
604 if (impurity) {
605 isl_space *space = isl_basic_set_get_space(delta);
606 delta = isl_basic_set_project_out(delta,
607 isl_dim_param, 0, nparam);
608 delta = isl_basic_set_add_dims(delta, isl_dim_param, nparam);
609 delta = isl_basic_set_reset_space(delta, space);
610 if (!delta)
611 goto error;
612 path = isl_basic_map_extend_constraints(path, delta->n_eq,
613 delta->n_ineq + 1);
614 path = add_delta_constraints(path, delta, off, nparam, d,
615 NULL, 1, NULL);
616 path = add_delta_constraints(path, delta, off, nparam, d,
617 NULL, 0, NULL);
618 path = isl_basic_map_gauss(path, NULL);
621 is_id = empty_path_is_identity(path, n_div + d);
622 if (is_id < 0)
623 goto error;
625 k = isl_basic_map_alloc_inequality(path);
626 if (k < 0)
627 goto error;
628 isl_seq_clr(path->ineq[k], 1 + isl_basic_map_total_dim(path));
629 if (!is_id)
630 isl_int_set_si(path->ineq[k][0], -1);
631 isl_int_set_si(path->ineq[k][off + d], 1);
633 free(div_purity);
634 isl_basic_set_free(delta);
635 path = isl_basic_map_finalize(path);
636 if (is_id) {
637 isl_space_free(space);
638 return isl_map_from_basic_map(path);
640 return isl_basic_map_union(path, isl_basic_map_identity(space));
641 error:
642 free(div_purity);
643 isl_space_free(space);
644 isl_basic_set_free(delta);
645 isl_basic_map_free(path);
646 return NULL;
649 /* Given a dimension specification Z^{n+1} -> Z^{n+1} and a parameter "param",
650 * construct a map that equates the parameter to the difference
651 * in the final coordinates and imposes that this difference is positive.
652 * That is, construct
654 * { [x,x_s] -> [y,y_s] : k = y_s - x_s > 0 }
656 static __isl_give isl_map *equate_parameter_to_length(
657 __isl_take isl_space *space, unsigned param)
659 struct isl_basic_map *bmap;
660 unsigned d;
661 unsigned nparam;
662 int k;
664 d = isl_space_dim(space, isl_dim_in);
665 nparam = isl_space_dim(space, isl_dim_param);
666 bmap = isl_basic_map_alloc_space(space, 0, 1, 1);
667 k = isl_basic_map_alloc_equality(bmap);
668 if (k < 0)
669 goto error;
670 isl_seq_clr(bmap->eq[k], 1 + isl_basic_map_total_dim(bmap));
671 isl_int_set_si(bmap->eq[k][1 + param], -1);
672 isl_int_set_si(bmap->eq[k][1 + nparam + d - 1], -1);
673 isl_int_set_si(bmap->eq[k][1 + nparam + d + d - 1], 1);
675 k = isl_basic_map_alloc_inequality(bmap);
676 if (k < 0)
677 goto error;
678 isl_seq_clr(bmap->ineq[k], 1 + isl_basic_map_total_dim(bmap));
679 isl_int_set_si(bmap->ineq[k][1 + param], 1);
680 isl_int_set_si(bmap->ineq[k][0], -1);
682 bmap = isl_basic_map_finalize(bmap);
683 return isl_map_from_basic_map(bmap);
684 error:
685 isl_basic_map_free(bmap);
686 return NULL;
689 /* Check whether "path" is acyclic, where the last coordinates of domain
690 * and range of path encode the number of steps taken.
691 * That is, check whether
693 * { d | d = y - x and (x,y) in path }
695 * does not contain any element with positive last coordinate (positive length)
696 * and zero remaining coordinates (cycle).
698 static isl_bool is_acyclic(__isl_take isl_map *path)
700 int i;
701 isl_bool acyclic;
702 unsigned dim;
703 struct isl_set *delta;
705 delta = isl_map_deltas(path);
706 dim = isl_set_dim(delta, isl_dim_set);
707 for (i = 0; i < dim; ++i) {
708 if (i == dim -1)
709 delta = isl_set_lower_bound_si(delta, isl_dim_set, i, 1);
710 else
711 delta = isl_set_fix_si(delta, isl_dim_set, i, 0);
714 acyclic = isl_set_is_empty(delta);
715 isl_set_free(delta);
717 return acyclic;
720 /* Given a union of basic maps R = \cup_i R_i \subseteq D \times D
721 * and a dimension specification (Z^{n+1} -> Z^{n+1}),
722 * construct a map that is an overapproximation of the map
723 * that takes an element from the space D \times Z to another
724 * element from the same space, such that the first n coordinates of the
725 * difference between them is a sum of differences between images
726 * and pre-images in one of the R_i and such that the last coordinate
727 * is equal to the number of steps taken.
728 * That is, let
730 * \Delta_i = { y - x | (x, y) in R_i }
732 * then the constructed map is an overapproximation of
734 * { (x) -> (x + d) | \exists k_i >= 0, \delta_i \in \Delta_i :
735 * d = (\sum_i k_i \delta_i, \sum_i k_i) }
737 * The elements of the singleton \Delta_i's are collected as the
738 * rows of the steps matrix. For all these \Delta_i's together,
739 * a single path is constructed.
740 * For each of the other \Delta_i's, we compute an overapproximation
741 * of the paths along elements of \Delta_i.
742 * Since each of these paths performs an addition, composition is
743 * symmetric and we can simply compose all resulting paths in any order.
745 static __isl_give isl_map *construct_extended_path(__isl_take isl_space *space,
746 __isl_keep isl_map *map, int *project)
748 struct isl_mat *steps = NULL;
749 struct isl_map *path = NULL;
750 unsigned d;
751 int i, j, n;
753 if (!map)
754 goto error;
756 d = isl_map_dim(map, isl_dim_in);
758 path = isl_map_identity(isl_space_copy(space));
760 steps = isl_mat_alloc(map->ctx, map->n, d);
761 if (!steps)
762 goto error;
764 n = 0;
765 for (i = 0; i < map->n; ++i) {
766 struct isl_basic_set *delta;
768 delta = isl_basic_map_deltas(isl_basic_map_copy(map->p[i]));
770 for (j = 0; j < d; ++j) {
771 isl_bool fixed;
773 fixed = isl_basic_set_plain_dim_is_fixed(delta, j,
774 &steps->row[n][j]);
775 if (fixed < 0) {
776 isl_basic_set_free(delta);
777 goto error;
779 if (!fixed)
780 break;
784 if (j < d) {
785 path = isl_map_apply_range(path,
786 path_along_delta(isl_space_copy(space), delta));
787 path = isl_map_coalesce(path);
788 } else {
789 isl_basic_set_free(delta);
790 ++n;
794 if (n > 0) {
795 steps->n_row = n;
796 path = isl_map_apply_range(path,
797 path_along_steps(isl_space_copy(space), steps));
800 if (project && *project) {
801 *project = is_acyclic(isl_map_copy(path));
802 if (*project < 0)
803 goto error;
806 isl_space_free(space);
807 isl_mat_free(steps);
808 return path;
809 error:
810 isl_space_free(space);
811 isl_mat_free(steps);
812 isl_map_free(path);
813 return NULL;
816 static isl_bool isl_set_overlaps(__isl_keep isl_set *set1,
817 __isl_keep isl_set *set2)
819 isl_set *i;
820 isl_bool no_overlap;
822 if (!set1 || !set2)
823 return isl_bool_error;
825 if (!isl_space_tuple_is_equal(set1->dim, isl_dim_set,
826 set2->dim, isl_dim_set))
827 return isl_bool_false;
829 i = isl_set_intersect(isl_set_copy(set1), isl_set_copy(set2));
830 no_overlap = isl_set_is_empty(i);
831 isl_set_free(i);
833 return isl_bool_not(no_overlap);
836 /* Given a union of basic maps R = \cup_i R_i \subseteq D \times D
837 * and a dimension specification (Z^{n+1} -> Z^{n+1}),
838 * construct a map that is an overapproximation of the map
839 * that takes an element from the dom R \times Z to an
840 * element from ran R \times Z, such that the first n coordinates of the
841 * difference between them is a sum of differences between images
842 * and pre-images in one of the R_i and such that the last coordinate
843 * is equal to the number of steps taken.
844 * That is, let
846 * \Delta_i = { y - x | (x, y) in R_i }
848 * then the constructed map is an overapproximation of
850 * { (x) -> (x + d) | \exists k_i >= 0, \delta_i \in \Delta_i :
851 * d = (\sum_i k_i \delta_i, \sum_i k_i) and
852 * x in dom R and x + d in ran R and
853 * \sum_i k_i >= 1 }
855 static __isl_give isl_map *construct_component(__isl_take isl_space *dim,
856 __isl_keep isl_map *map, int *exact, int project)
858 struct isl_set *domain = NULL;
859 struct isl_set *range = NULL;
860 struct isl_map *app = NULL;
861 struct isl_map *path = NULL;
862 isl_bool overlaps;
864 domain = isl_map_domain(isl_map_copy(map));
865 domain = isl_set_coalesce(domain);
866 range = isl_map_range(isl_map_copy(map));
867 range = isl_set_coalesce(range);
868 overlaps = isl_set_overlaps(domain, range);
869 if (overlaps < 0 || !overlaps) {
870 isl_set_free(domain);
871 isl_set_free(range);
872 isl_space_free(dim);
874 if (overlaps < 0)
875 map = NULL;
876 map = isl_map_copy(map);
877 map = isl_map_add_dims(map, isl_dim_in, 1);
878 map = isl_map_add_dims(map, isl_dim_out, 1);
879 map = set_path_length(map, 1, 1);
880 return map;
882 app = isl_map_from_domain_and_range(domain, range);
883 app = isl_map_add_dims(app, isl_dim_in, 1);
884 app = isl_map_add_dims(app, isl_dim_out, 1);
886 path = construct_extended_path(isl_space_copy(dim), map,
887 exact && *exact ? &project : NULL);
888 app = isl_map_intersect(app, path);
890 if (exact && *exact &&
891 (*exact = check_exactness(isl_map_copy(map), isl_map_copy(app),
892 project)) < 0)
893 goto error;
895 isl_space_free(dim);
896 app = set_path_length(app, 0, 1);
897 return app;
898 error:
899 isl_space_free(dim);
900 isl_map_free(app);
901 return NULL;
904 /* Call construct_component and, if "project" is set, project out
905 * the final coordinates.
907 static __isl_give isl_map *construct_projected_component(
908 __isl_take isl_space *space,
909 __isl_keep isl_map *map, int *exact, int project)
911 isl_map *app;
912 unsigned d;
914 if (!space)
915 return NULL;
916 d = isl_space_dim(space, isl_dim_in);
918 app = construct_component(space, map, exact, project);
919 if (project) {
920 app = isl_map_project_out(app, isl_dim_in, d - 1, 1);
921 app = isl_map_project_out(app, isl_dim_out, d - 1, 1);
923 return app;
926 /* Compute an extended version, i.e., with path lengths, of
927 * an overapproximation of the transitive closure of "bmap"
928 * with path lengths greater than or equal to zero and with
929 * domain and range equal to "dom".
931 static __isl_give isl_map *q_closure(__isl_take isl_space *dim,
932 __isl_take isl_set *dom, __isl_keep isl_basic_map *bmap, int *exact)
934 int project = 1;
935 isl_map *path;
936 isl_map *map;
937 isl_map *app;
939 dom = isl_set_add_dims(dom, isl_dim_set, 1);
940 app = isl_map_from_domain_and_range(dom, isl_set_copy(dom));
941 map = isl_map_from_basic_map(isl_basic_map_copy(bmap));
942 path = construct_extended_path(dim, map, &project);
943 app = isl_map_intersect(app, path);
945 if ((*exact = check_exactness(map, isl_map_copy(app), project)) < 0)
946 goto error;
948 return app;
949 error:
950 isl_map_free(app);
951 return NULL;
954 /* Check whether qc has any elements of length at least one
955 * with domain and/or range outside of dom and ran.
957 static isl_bool has_spurious_elements(__isl_keep isl_map *qc,
958 __isl_keep isl_set *dom, __isl_keep isl_set *ran)
960 isl_set *s;
961 isl_bool subset;
962 unsigned d;
964 if (!qc || !dom || !ran)
965 return isl_bool_error;
967 d = isl_map_dim(qc, isl_dim_in);
969 qc = isl_map_copy(qc);
970 qc = set_path_length(qc, 0, 1);
971 qc = isl_map_project_out(qc, isl_dim_in, d - 1, 1);
972 qc = isl_map_project_out(qc, isl_dim_out, d - 1, 1);
974 s = isl_map_domain(isl_map_copy(qc));
975 subset = isl_set_is_subset(s, dom);
976 isl_set_free(s);
977 if (subset < 0)
978 goto error;
979 if (!subset) {
980 isl_map_free(qc);
981 return isl_bool_true;
984 s = isl_map_range(qc);
985 subset = isl_set_is_subset(s, ran);
986 isl_set_free(s);
988 return isl_bool_not(subset);
989 error:
990 isl_map_free(qc);
991 return isl_bool_error;
994 #define LEFT 2
995 #define RIGHT 1
997 /* For each basic map in "map", except i, check whether it combines
998 * with the transitive closure that is reflexive on C combines
999 * to the left and to the right.
1001 * In particular, if
1003 * dom map_j \subseteq C
1005 * then right[j] is set to 1. Otherwise, if
1007 * ran map_i \cap dom map_j = \emptyset
1009 * then right[j] is set to 0. Otherwise, composing to the right
1010 * is impossible.
1012 * Similar, for composing to the left, we have if
1014 * ran map_j \subseteq C
1016 * then left[j] is set to 1. Otherwise, if
1018 * dom map_i \cap ran map_j = \emptyset
1020 * then left[j] is set to 0. Otherwise, composing to the left
1021 * is impossible.
1023 * The return value is or'd with LEFT if composing to the left
1024 * is possible and with RIGHT if composing to the right is possible.
1026 static int composability(__isl_keep isl_set *C, int i,
1027 isl_set **dom, isl_set **ran, int *left, int *right,
1028 __isl_keep isl_map *map)
1030 int j;
1031 int ok;
1033 ok = LEFT | RIGHT;
1034 for (j = 0; j < map->n && ok; ++j) {
1035 isl_bool overlaps, subset;
1036 if (j == i)
1037 continue;
1039 if (ok & RIGHT) {
1040 if (!dom[j])
1041 dom[j] = isl_set_from_basic_set(
1042 isl_basic_map_domain(
1043 isl_basic_map_copy(map->p[j])));
1044 if (!dom[j])
1045 return -1;
1046 overlaps = isl_set_overlaps(ran[i], dom[j]);
1047 if (overlaps < 0)
1048 return -1;
1049 if (!overlaps)
1050 right[j] = 0;
1051 else {
1052 subset = isl_set_is_subset(dom[j], C);
1053 if (subset < 0)
1054 return -1;
1055 if (subset)
1056 right[j] = 1;
1057 else
1058 ok &= ~RIGHT;
1062 if (ok & LEFT) {
1063 if (!ran[j])
1064 ran[j] = isl_set_from_basic_set(
1065 isl_basic_map_range(
1066 isl_basic_map_copy(map->p[j])));
1067 if (!ran[j])
1068 return -1;
1069 overlaps = isl_set_overlaps(dom[i], ran[j]);
1070 if (overlaps < 0)
1071 return -1;
1072 if (!overlaps)
1073 left[j] = 0;
1074 else {
1075 subset = isl_set_is_subset(ran[j], C);
1076 if (subset < 0)
1077 return -1;
1078 if (subset)
1079 left[j] = 1;
1080 else
1081 ok &= ~LEFT;
1086 return ok;
1089 static __isl_give isl_map *anonymize(__isl_take isl_map *map)
1091 map = isl_map_reset(map, isl_dim_in);
1092 map = isl_map_reset(map, isl_dim_out);
1093 return map;
1096 /* Return a map that is a union of the basic maps in "map", except i,
1097 * composed to left and right with qc based on the entries of "left"
1098 * and "right".
1100 static __isl_give isl_map *compose(__isl_keep isl_map *map, int i,
1101 __isl_take isl_map *qc, int *left, int *right)
1103 int j;
1104 isl_map *comp;
1106 comp = isl_map_empty(isl_map_get_space(map));
1107 for (j = 0; j < map->n; ++j) {
1108 isl_map *map_j;
1110 if (j == i)
1111 continue;
1113 map_j = isl_map_from_basic_map(isl_basic_map_copy(map->p[j]));
1114 map_j = anonymize(map_j);
1115 if (left && left[j])
1116 map_j = isl_map_apply_range(map_j, isl_map_copy(qc));
1117 if (right && right[j])
1118 map_j = isl_map_apply_range(isl_map_copy(qc), map_j);
1119 comp = isl_map_union(comp, map_j);
1122 comp = isl_map_compute_divs(comp);
1123 comp = isl_map_coalesce(comp);
1125 isl_map_free(qc);
1127 return comp;
1130 /* Compute the transitive closure of "map" incrementally by
1131 * computing
1133 * map_i^+ \cup qc^+
1135 * or
1137 * map_i^+ \cup ((id \cup map_i^) \circ qc^+)
1139 * or
1141 * map_i^+ \cup (qc^+ \circ (id \cup map_i^))
1143 * depending on whether left or right are NULL.
1145 static __isl_give isl_map *compute_incremental(
1146 __isl_take isl_space *space, __isl_keep isl_map *map,
1147 int i, __isl_take isl_map *qc, int *left, int *right, int *exact)
1149 isl_map *map_i;
1150 isl_map *tc;
1151 isl_map *rtc = NULL;
1153 if (!map)
1154 goto error;
1155 isl_assert(map->ctx, left || right, goto error);
1157 map_i = isl_map_from_basic_map(isl_basic_map_copy(map->p[i]));
1158 tc = construct_projected_component(isl_space_copy(space), map_i,
1159 exact, 1);
1160 isl_map_free(map_i);
1162 if (*exact)
1163 qc = isl_map_transitive_closure(qc, exact);
1165 if (!*exact) {
1166 isl_space_free(space);
1167 isl_map_free(tc);
1168 isl_map_free(qc);
1169 return isl_map_universe(isl_map_get_space(map));
1172 if (!left || !right)
1173 rtc = isl_map_union(isl_map_copy(tc),
1174 isl_map_identity(isl_map_get_space(tc)));
1175 if (!right)
1176 qc = isl_map_apply_range(rtc, qc);
1177 if (!left)
1178 qc = isl_map_apply_range(qc, rtc);
1179 qc = isl_map_union(tc, qc);
1181 isl_space_free(space);
1183 return qc;
1184 error:
1185 isl_space_free(space);
1186 isl_map_free(qc);
1187 return NULL;
1190 /* Given a map "map", try to find a basic map such that
1191 * map^+ can be computed as
1193 * map^+ = map_i^+ \cup
1194 * \bigcup_j ((map_i^+ \cup Id_C)^+ \circ map_j \circ (map_i^+ \cup Id_C))^+
1196 * with C the simple hull of the domain and range of the input map.
1197 * map_i^ \cup Id_C is computed by allowing the path lengths to be zero
1198 * and by intersecting domain and range with C.
1199 * Of course, we need to check that this is actually equal to map_i^ \cup Id_C.
1200 * Also, we only use the incremental computation if all the transitive
1201 * closures are exact and if the number of basic maps in the union,
1202 * after computing the integer divisions, is smaller than the number
1203 * of basic maps in the input map.
1205 static isl_bool incremental_on_entire_domain(__isl_keep isl_space *space,
1206 __isl_keep isl_map *map,
1207 isl_set **dom, isl_set **ran, int *left, int *right,
1208 __isl_give isl_map **res)
1210 int i;
1211 isl_set *C;
1212 unsigned d;
1214 *res = NULL;
1216 C = isl_set_union(isl_map_domain(isl_map_copy(map)),
1217 isl_map_range(isl_map_copy(map)));
1218 C = isl_set_from_basic_set(isl_set_simple_hull(C));
1219 if (!C)
1220 return isl_bool_error;
1221 if (C->n != 1) {
1222 isl_set_free(C);
1223 return isl_bool_false;
1226 d = isl_map_dim(map, isl_dim_in);
1228 for (i = 0; i < map->n; ++i) {
1229 isl_map *qc;
1230 int exact_i;
1231 isl_bool spurious;
1232 int j;
1233 dom[i] = isl_set_from_basic_set(isl_basic_map_domain(
1234 isl_basic_map_copy(map->p[i])));
1235 ran[i] = isl_set_from_basic_set(isl_basic_map_range(
1236 isl_basic_map_copy(map->p[i])));
1237 qc = q_closure(isl_space_copy(space), isl_set_copy(C),
1238 map->p[i], &exact_i);
1239 if (!qc)
1240 goto error;
1241 if (!exact_i) {
1242 isl_map_free(qc);
1243 continue;
1245 spurious = has_spurious_elements(qc, dom[i], ran[i]);
1246 if (spurious) {
1247 isl_map_free(qc);
1248 if (spurious < 0)
1249 goto error;
1250 continue;
1252 qc = isl_map_project_out(qc, isl_dim_in, d, 1);
1253 qc = isl_map_project_out(qc, isl_dim_out, d, 1);
1254 qc = isl_map_compute_divs(qc);
1255 for (j = 0; j < map->n; ++j)
1256 left[j] = right[j] = 1;
1257 qc = compose(map, i, qc, left, right);
1258 if (!qc)
1259 goto error;
1260 if (qc->n >= map->n) {
1261 isl_map_free(qc);
1262 continue;
1264 *res = compute_incremental(isl_space_copy(space), map, i, qc,
1265 left, right, &exact_i);
1266 if (!*res)
1267 goto error;
1268 if (exact_i)
1269 break;
1270 isl_map_free(*res);
1271 *res = NULL;
1274 isl_set_free(C);
1276 return *res != NULL;
1277 error:
1278 isl_set_free(C);
1279 return isl_bool_error;
1282 /* Try and compute the transitive closure of "map" as
1284 * map^+ = map_i^+ \cup
1285 * \bigcup_j ((map_i^+ \cup Id_C)^+ \circ map_j \circ (map_i^+ \cup Id_C))^+
1287 * with C either the simple hull of the domain and range of the entire
1288 * map or the simple hull of domain and range of map_i.
1290 static __isl_give isl_map *incremental_closure(__isl_take isl_space *space,
1291 __isl_keep isl_map *map, int *exact, int project)
1293 int i;
1294 isl_set **dom = NULL;
1295 isl_set **ran = NULL;
1296 int *left = NULL;
1297 int *right = NULL;
1298 isl_set *C;
1299 unsigned d;
1300 isl_map *res = NULL;
1302 if (!project)
1303 return construct_projected_component(space, map, exact,
1304 project);
1306 if (!map)
1307 goto error;
1308 if (map->n <= 1)
1309 return construct_projected_component(space, map, exact,
1310 project);
1312 d = isl_map_dim(map, isl_dim_in);
1314 dom = isl_calloc_array(map->ctx, isl_set *, map->n);
1315 ran = isl_calloc_array(map->ctx, isl_set *, map->n);
1316 left = isl_calloc_array(map->ctx, int, map->n);
1317 right = isl_calloc_array(map->ctx, int, map->n);
1318 if (!ran || !dom || !left || !right)
1319 goto error;
1321 if (incremental_on_entire_domain(space, map, dom, ran, left, right,
1322 &res) < 0)
1323 goto error;
1325 for (i = 0; !res && i < map->n; ++i) {
1326 isl_map *qc;
1327 int exact_i, comp;
1328 isl_bool spurious;
1329 if (!dom[i])
1330 dom[i] = isl_set_from_basic_set(
1331 isl_basic_map_domain(
1332 isl_basic_map_copy(map->p[i])));
1333 if (!dom[i])
1334 goto error;
1335 if (!ran[i])
1336 ran[i] = isl_set_from_basic_set(
1337 isl_basic_map_range(
1338 isl_basic_map_copy(map->p[i])));
1339 if (!ran[i])
1340 goto error;
1341 C = isl_set_union(isl_set_copy(dom[i]),
1342 isl_set_copy(ran[i]));
1343 C = isl_set_from_basic_set(isl_set_simple_hull(C));
1344 if (!C)
1345 goto error;
1346 if (C->n != 1) {
1347 isl_set_free(C);
1348 continue;
1350 comp = composability(C, i, dom, ran, left, right, map);
1351 if (!comp || comp < 0) {
1352 isl_set_free(C);
1353 if (comp < 0)
1354 goto error;
1355 continue;
1357 qc = q_closure(isl_space_copy(space), C, map->p[i], &exact_i);
1358 if (!qc)
1359 goto error;
1360 if (!exact_i) {
1361 isl_map_free(qc);
1362 continue;
1364 spurious = has_spurious_elements(qc, dom[i], ran[i]);
1365 if (spurious) {
1366 isl_map_free(qc);
1367 if (spurious < 0)
1368 goto error;
1369 continue;
1371 qc = isl_map_project_out(qc, isl_dim_in, d, 1);
1372 qc = isl_map_project_out(qc, isl_dim_out, d, 1);
1373 qc = isl_map_compute_divs(qc);
1374 qc = compose(map, i, qc, (comp & LEFT) ? left : NULL,
1375 (comp & RIGHT) ? right : NULL);
1376 if (!qc)
1377 goto error;
1378 if (qc->n >= map->n) {
1379 isl_map_free(qc);
1380 continue;
1382 res = compute_incremental(isl_space_copy(space), map, i, qc,
1383 (comp & LEFT) ? left : NULL,
1384 (comp & RIGHT) ? right : NULL, &exact_i);
1385 if (!res)
1386 goto error;
1387 if (exact_i)
1388 break;
1389 isl_map_free(res);
1390 res = NULL;
1393 for (i = 0; i < map->n; ++i) {
1394 isl_set_free(dom[i]);
1395 isl_set_free(ran[i]);
1397 free(dom);
1398 free(ran);
1399 free(left);
1400 free(right);
1402 if (res) {
1403 isl_space_free(space);
1404 return res;
1407 return construct_projected_component(space, map, exact, project);
1408 error:
1409 if (dom)
1410 for (i = 0; i < map->n; ++i)
1411 isl_set_free(dom[i]);
1412 free(dom);
1413 if (ran)
1414 for (i = 0; i < map->n; ++i)
1415 isl_set_free(ran[i]);
1416 free(ran);
1417 free(left);
1418 free(right);
1419 isl_space_free(space);
1420 return NULL;
1423 /* Given an array of sets "set", add "dom" at position "pos"
1424 * and search for elements at earlier positions that overlap with "dom".
1425 * If any can be found, then merge all of them, together with "dom", into
1426 * a single set and assign the union to the first in the array,
1427 * which becomes the new group leader for all groups involved in the merge.
1428 * During the search, we only consider group leaders, i.e., those with
1429 * group[i] = i, as the other sets have already been combined
1430 * with one of the group leaders.
1432 static int merge(isl_set **set, int *group, __isl_take isl_set *dom, int pos)
1434 int i;
1436 group[pos] = pos;
1437 set[pos] = isl_set_copy(dom);
1439 for (i = pos - 1; i >= 0; --i) {
1440 isl_bool o;
1442 if (group[i] != i)
1443 continue;
1445 o = isl_set_overlaps(set[i], dom);
1446 if (o < 0)
1447 goto error;
1448 if (!o)
1449 continue;
1451 set[i] = isl_set_union(set[i], set[group[pos]]);
1452 set[group[pos]] = NULL;
1453 if (!set[i])
1454 goto error;
1455 group[group[pos]] = i;
1456 group[pos] = i;
1459 isl_set_free(dom);
1460 return 0;
1461 error:
1462 isl_set_free(dom);
1463 return -1;
1466 /* Construct a map [x] -> [x+1], with parameters prescribed by "space".
1468 static __isl_give isl_map *increment(__isl_take isl_space *space)
1470 int k;
1471 isl_basic_map *bmap;
1473 space = isl_space_set_from_params(space);
1474 space = isl_space_add_dims(space, isl_dim_set, 1);
1475 space = isl_space_map_from_set(space);
1476 bmap = isl_basic_map_alloc_space(space, 0, 1, 0);
1477 k = isl_basic_map_alloc_equality(bmap);
1478 if (k < 0)
1479 goto error;
1480 isl_seq_clr(bmap->eq[k], 1 + isl_basic_map_total_dim(bmap));
1481 isl_int_set_si(bmap->eq[k][0], 1);
1482 isl_int_set_si(bmap->eq[k][isl_basic_map_offset(bmap, isl_dim_in)], 1);
1483 isl_int_set_si(bmap->eq[k][isl_basic_map_offset(bmap, isl_dim_out)], -1);
1484 return isl_map_from_basic_map(bmap);
1485 error:
1486 isl_basic_map_free(bmap);
1487 return NULL;
1490 /* Replace each entry in the n by n grid of maps by the cross product
1491 * with the relation { [i] -> [i + 1] }.
1493 static isl_stat add_length(__isl_keep isl_map *map, isl_map ***grid, int n)
1495 int i, j;
1496 isl_space *space;
1497 isl_map *step;
1499 space = isl_space_params(isl_map_get_space(map));
1500 step = increment(space);
1502 if (!step)
1503 return isl_stat_error;
1505 for (i = 0; i < n; ++i)
1506 for (j = 0; j < n; ++j)
1507 grid[i][j] = isl_map_product(grid[i][j],
1508 isl_map_copy(step));
1510 isl_map_free(step);
1512 return isl_stat_ok;
1515 /* The core of the Floyd-Warshall algorithm.
1516 * Updates the given n x x matrix of relations in place.
1518 * The algorithm iterates over all vertices. In each step, the whole
1519 * matrix is updated to include all paths that go to the current vertex,
1520 * possibly stay there a while (including passing through earlier vertices)
1521 * and then come back. At the start of each iteration, the diagonal
1522 * element corresponding to the current vertex is replaced by its
1523 * transitive closure to account for all indirect paths that stay
1524 * in the current vertex.
1526 static void floyd_warshall_iterate(isl_map ***grid, int n, int *exact)
1528 int r, p, q;
1530 for (r = 0; r < n; ++r) {
1531 int r_exact;
1532 grid[r][r] = isl_map_transitive_closure(grid[r][r],
1533 (exact && *exact) ? &r_exact : NULL);
1534 if (exact && *exact && !r_exact)
1535 *exact = 0;
1537 for (p = 0; p < n; ++p)
1538 for (q = 0; q < n; ++q) {
1539 isl_map *loop;
1540 if (p == r && q == r)
1541 continue;
1542 loop = isl_map_apply_range(
1543 isl_map_copy(grid[p][r]),
1544 isl_map_copy(grid[r][q]));
1545 grid[p][q] = isl_map_union(grid[p][q], loop);
1546 loop = isl_map_apply_range(
1547 isl_map_copy(grid[p][r]),
1548 isl_map_apply_range(
1549 isl_map_copy(grid[r][r]),
1550 isl_map_copy(grid[r][q])));
1551 grid[p][q] = isl_map_union(grid[p][q], loop);
1552 grid[p][q] = isl_map_coalesce(grid[p][q]);
1557 /* Given a partition of the domains and ranges of the basic maps in "map",
1558 * apply the Floyd-Warshall algorithm with the elements in the partition
1559 * as vertices.
1561 * In particular, there are "n" elements in the partition and "group" is
1562 * an array of length 2 * map->n with entries in [0,n-1].
1564 * We first construct a matrix of relations based on the partition information,
1565 * apply Floyd-Warshall on this matrix of relations and then take the
1566 * union of all entries in the matrix as the final result.
1568 * If we are actually computing the power instead of the transitive closure,
1569 * i.e., when "project" is not set, then the result should have the
1570 * path lengths encoded as the difference between an extra pair of
1571 * coordinates. We therefore apply the nested transitive closures
1572 * to relations that include these lengths. In particular, we replace
1573 * the input relation by the cross product with the unit length relation
1574 * { [i] -> [i + 1] }.
1576 static __isl_give isl_map *floyd_warshall_with_groups(
1577 __isl_take isl_space *space, __isl_keep isl_map *map,
1578 int *exact, int project, int *group, int n)
1580 int i, j, k;
1581 isl_map ***grid = NULL;
1582 isl_map *app;
1584 if (!map)
1585 goto error;
1587 if (n == 1) {
1588 free(group);
1589 return incremental_closure(space, map, exact, project);
1592 grid = isl_calloc_array(map->ctx, isl_map **, n);
1593 if (!grid)
1594 goto error;
1595 for (i = 0; i < n; ++i) {
1596 grid[i] = isl_calloc_array(map->ctx, isl_map *, n);
1597 if (!grid[i])
1598 goto error;
1599 for (j = 0; j < n; ++j)
1600 grid[i][j] = isl_map_empty(isl_map_get_space(map));
1603 for (k = 0; k < map->n; ++k) {
1604 i = group[2 * k];
1605 j = group[2 * k + 1];
1606 grid[i][j] = isl_map_union(grid[i][j],
1607 isl_map_from_basic_map(
1608 isl_basic_map_copy(map->p[k])));
1611 if (!project && add_length(map, grid, n) < 0)
1612 goto error;
1614 floyd_warshall_iterate(grid, n, exact);
1616 app = isl_map_empty(isl_map_get_space(grid[0][0]));
1618 for (i = 0; i < n; ++i) {
1619 for (j = 0; j < n; ++j)
1620 app = isl_map_union(app, grid[i][j]);
1621 free(grid[i]);
1623 free(grid);
1625 free(group);
1626 isl_space_free(space);
1628 return app;
1629 error:
1630 if (grid)
1631 for (i = 0; i < n; ++i) {
1632 if (!grid[i])
1633 continue;
1634 for (j = 0; j < n; ++j)
1635 isl_map_free(grid[i][j]);
1636 free(grid[i]);
1638 free(grid);
1639 free(group);
1640 isl_space_free(space);
1641 return NULL;
1644 /* Partition the domains and ranges of the n basic relations in list
1645 * into disjoint cells.
1647 * To find the partition, we simply consider all of the domains
1648 * and ranges in turn and combine those that overlap.
1649 * "set" contains the partition elements and "group" indicates
1650 * to which partition element a given domain or range belongs.
1651 * The domain of basic map i corresponds to element 2 * i in these arrays,
1652 * while the domain corresponds to element 2 * i + 1.
1653 * During the construction group[k] is either equal to k,
1654 * in which case set[k] contains the union of all the domains and
1655 * ranges in the corresponding group, or is equal to some l < k,
1656 * with l another domain or range in the same group.
1658 static int *setup_groups(isl_ctx *ctx, __isl_keep isl_basic_map **list, int n,
1659 isl_set ***set, int *n_group)
1661 int i;
1662 int *group = NULL;
1663 int g;
1665 *set = isl_calloc_array(ctx, isl_set *, 2 * n);
1666 group = isl_alloc_array(ctx, int, 2 * n);
1668 if (!*set || !group)
1669 goto error;
1671 for (i = 0; i < n; ++i) {
1672 isl_set *dom;
1673 dom = isl_set_from_basic_set(isl_basic_map_domain(
1674 isl_basic_map_copy(list[i])));
1675 if (merge(*set, group, dom, 2 * i) < 0)
1676 goto error;
1677 dom = isl_set_from_basic_set(isl_basic_map_range(
1678 isl_basic_map_copy(list[i])));
1679 if (merge(*set, group, dom, 2 * i + 1) < 0)
1680 goto error;
1683 g = 0;
1684 for (i = 0; i < 2 * n; ++i)
1685 if (group[i] == i) {
1686 if (g != i) {
1687 (*set)[g] = (*set)[i];
1688 (*set)[i] = NULL;
1690 group[i] = g++;
1691 } else
1692 group[i] = group[group[i]];
1694 *n_group = g;
1696 return group;
1697 error:
1698 if (*set) {
1699 for (i = 0; i < 2 * n; ++i)
1700 isl_set_free((*set)[i]);
1701 free(*set);
1702 *set = NULL;
1704 free(group);
1705 return NULL;
1708 /* Check if the domains and ranges of the basic maps in "map" can
1709 * be partitioned, and if so, apply Floyd-Warshall on the elements
1710 * of the partition. Note that we also apply this algorithm
1711 * if we want to compute the power, i.e., when "project" is not set.
1712 * However, the results are unlikely to be exact since the recursive
1713 * calls inside the Floyd-Warshall algorithm typically result in
1714 * non-linear path lengths quite quickly.
1716 static __isl_give isl_map *floyd_warshall(__isl_take isl_space *space,
1717 __isl_keep isl_map *map, int *exact, int project)
1719 int i;
1720 isl_set **set = NULL;
1721 int *group = NULL;
1722 int n;
1724 if (!map)
1725 goto error;
1726 if (map->n <= 1)
1727 return incremental_closure(space, map, exact, project);
1729 group = setup_groups(map->ctx, map->p, map->n, &set, &n);
1730 if (!group)
1731 goto error;
1733 for (i = 0; i < 2 * map->n; ++i)
1734 isl_set_free(set[i]);
1736 free(set);
1738 return floyd_warshall_with_groups(space, map, exact, project, group, n);
1739 error:
1740 isl_space_free(space);
1741 return NULL;
1744 /* Structure for representing the nodes of the graph of which
1745 * strongly connected components are being computed.
1747 * list contains the actual nodes
1748 * check_closed is set if we may have used the fact that
1749 * a pair of basic maps can be interchanged
1751 struct isl_tc_follows_data {
1752 isl_basic_map **list;
1753 int check_closed;
1756 /* Check whether in the computation of the transitive closure
1757 * "list[i]" (R_1) should follow (or be part of the same component as)
1758 * "list[j]" (R_2).
1760 * That is check whether
1762 * R_1 \circ R_2
1764 * is a subset of
1766 * R_2 \circ R_1
1768 * If so, then there is no reason for R_1 to immediately follow R_2
1769 * in any path.
1771 * *check_closed is set if the subset relation holds while
1772 * R_1 \circ R_2 is not empty.
1774 static isl_bool basic_map_follows(int i, int j, void *user)
1776 struct isl_tc_follows_data *data = user;
1777 struct isl_map *map12 = NULL;
1778 struct isl_map *map21 = NULL;
1779 isl_bool subset;
1781 if (!isl_space_tuple_is_equal(data->list[i]->dim, isl_dim_in,
1782 data->list[j]->dim, isl_dim_out))
1783 return isl_bool_false;
1785 map21 = isl_map_from_basic_map(
1786 isl_basic_map_apply_range(
1787 isl_basic_map_copy(data->list[j]),
1788 isl_basic_map_copy(data->list[i])));
1789 subset = isl_map_is_empty(map21);
1790 if (subset < 0)
1791 goto error;
1792 if (subset) {
1793 isl_map_free(map21);
1794 return isl_bool_false;
1797 if (!isl_space_tuple_is_equal(data->list[i]->dim, isl_dim_in,
1798 data->list[i]->dim, isl_dim_out) ||
1799 !isl_space_tuple_is_equal(data->list[j]->dim, isl_dim_in,
1800 data->list[j]->dim, isl_dim_out)) {
1801 isl_map_free(map21);
1802 return isl_bool_true;
1805 map12 = isl_map_from_basic_map(
1806 isl_basic_map_apply_range(
1807 isl_basic_map_copy(data->list[i]),
1808 isl_basic_map_copy(data->list[j])));
1810 subset = isl_map_is_subset(map21, map12);
1812 isl_map_free(map12);
1813 isl_map_free(map21);
1815 if (subset)
1816 data->check_closed = 1;
1818 return isl_bool_not(subset);
1819 error:
1820 isl_map_free(map21);
1821 return isl_bool_error;
1824 /* Given a union of basic maps R = \cup_i R_i \subseteq D \times D
1825 * and a dimension specification (Z^{n+1} -> Z^{n+1}),
1826 * construct a map that is an overapproximation of the map
1827 * that takes an element from the dom R \times Z to an
1828 * element from ran R \times Z, such that the first n coordinates of the
1829 * difference between them is a sum of differences between images
1830 * and pre-images in one of the R_i and such that the last coordinate
1831 * is equal to the number of steps taken.
1832 * If "project" is set, then these final coordinates are not included,
1833 * i.e., a relation of type Z^n -> Z^n is returned.
1834 * That is, let
1836 * \Delta_i = { y - x | (x, y) in R_i }
1838 * then the constructed map is an overapproximation of
1840 * { (x) -> (x + d) | \exists k_i >= 0, \delta_i \in \Delta_i :
1841 * d = (\sum_i k_i \delta_i, \sum_i k_i) and
1842 * x in dom R and x + d in ran R }
1844 * or
1846 * { (x) -> (x + d) | \exists k_i >= 0, \delta_i \in \Delta_i :
1847 * d = (\sum_i k_i \delta_i) and
1848 * x in dom R and x + d in ran R }
1850 * if "project" is set.
1852 * We first split the map into strongly connected components, perform
1853 * the above on each component and then join the results in the correct
1854 * order, at each join also taking in the union of both arguments
1855 * to allow for paths that do not go through one of the two arguments.
1857 static __isl_give isl_map *construct_power_components(
1858 __isl_take isl_space *space, __isl_keep isl_map *map, int *exact,
1859 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(space, 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(space));
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(space),
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(space, map, orig_exact, project);
1926 isl_tarjan_graph_free(g);
1927 isl_space_free(space);
1929 return path;
1930 error:
1931 isl_tarjan_graph_free(g);
1932 isl_space_free(space);
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 *space = NULL;
1972 if (!map)
1973 return NULL;
1975 space = isl_map_get_space(map);
1977 space = isl_space_add_dims(space, isl_dim_in, 1);
1978 space = isl_space_add_dims(space, isl_dim_out, 1);
1980 app = construct_power_components(isl_space_copy(space), map,
1981 exact, project);
1983 isl_space_free(space);
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 (isl_map_check_equal_tuples(map) < 0)
2005 return isl_map_free(map);
2007 app = construct_power(map, exact, project);
2009 isl_map_free(map);
2010 return app;
2013 /* Compute the positive powers of "map", or an overapproximation.
2014 * The result maps the exponent to a nested copy of the corresponding power.
2015 * If the result is exact, then *exact is set to 1.
2016 * map_power constructs an extended relation with the path lengths
2017 * encoded as the difference between the final coordinates.
2018 * In the final step, this difference is equated to an extra parameter
2019 * and made positive. The extra coordinates are subsequently projected out
2020 * and the parameter is turned into the domain of the result.
2022 __isl_give isl_map *isl_map_power(__isl_take isl_map *map, int *exact)
2024 isl_space *target_space;
2025 isl_space *space;
2026 isl_map *diff;
2027 unsigned d;
2028 unsigned param;
2030 if (!map)
2031 return NULL;
2033 d = isl_map_dim(map, isl_dim_in);
2034 param = isl_map_dim(map, isl_dim_param);
2036 map = isl_map_compute_divs(map);
2037 map = isl_map_coalesce(map);
2039 if (isl_map_plain_is_empty(map)) {
2040 map = isl_map_from_range(isl_map_wrap(map));
2041 map = isl_map_add_dims(map, isl_dim_in, 1);
2042 map = isl_map_set_dim_name(map, isl_dim_in, 0, "k");
2043 return map;
2046 target_space = isl_map_get_space(map);
2047 target_space = isl_space_from_range(isl_space_wrap(target_space));
2048 target_space = isl_space_add_dims(target_space, isl_dim_in, 1);
2049 target_space = isl_space_set_dim_name(target_space, isl_dim_in, 0, "k");
2051 map = map_power(map, exact, 0);
2053 map = isl_map_add_dims(map, isl_dim_param, 1);
2054 space = isl_map_get_space(map);
2055 diff = equate_parameter_to_length(space, param);
2056 map = isl_map_intersect(map, diff);
2057 map = isl_map_project_out(map, isl_dim_in, d, 1);
2058 map = isl_map_project_out(map, isl_dim_out, d, 1);
2059 map = isl_map_from_range(isl_map_wrap(map));
2060 map = isl_map_move_dims(map, isl_dim_in, 0, isl_dim_param, param, 1);
2062 map = isl_map_reset_space(map, target_space);
2064 return map;
2067 /* Compute a relation that maps each element in the range of the input
2068 * relation to the lengths of all paths composed of edges in the input
2069 * relation that end up in the given range element.
2070 * The result may be an overapproximation, in which case *exact is set to 0.
2071 * The resulting relation is very similar to the power relation.
2072 * The difference are that the domain has been projected out, the
2073 * range has become the domain and the exponent is the range instead
2074 * of a parameter.
2076 __isl_give isl_map *isl_map_reaching_path_lengths(__isl_take isl_map *map,
2077 int *exact)
2079 isl_space *space;
2080 isl_map *diff;
2081 unsigned d;
2082 unsigned param;
2084 if (!map)
2085 return NULL;
2087 d = isl_map_dim(map, isl_dim_in);
2088 param = isl_map_dim(map, isl_dim_param);
2090 map = isl_map_compute_divs(map);
2091 map = isl_map_coalesce(map);
2093 if (isl_map_plain_is_empty(map)) {
2094 if (exact)
2095 *exact = 1;
2096 map = isl_map_project_out(map, isl_dim_out, 0, d);
2097 map = isl_map_add_dims(map, isl_dim_out, 1);
2098 return map;
2101 map = map_power(map, exact, 0);
2103 map = isl_map_add_dims(map, isl_dim_param, 1);
2104 space = isl_map_get_space(map);
2105 diff = equate_parameter_to_length(space, param);
2106 map = isl_map_intersect(map, diff);
2107 map = isl_map_project_out(map, isl_dim_in, 0, d + 1);
2108 map = isl_map_project_out(map, isl_dim_out, d, 1);
2109 map = isl_map_reverse(map);
2110 map = isl_map_move_dims(map, isl_dim_out, 0, isl_dim_param, param, 1);
2112 return map;
2115 /* Given a map, compute the smallest superset of this map that is of the form
2117 * { i -> j : L <= j - i <= U and exists a_p: j_p - i_p = M_p a_p }
2119 * (where p ranges over the (non-parametric) dimensions),
2120 * compute the transitive closure of this map, i.e.,
2122 * { i -> j : exists k > 0:
2123 * k L <= j - i <= k U and exists a: j_p - i_p = M_p a_p }
2125 * and intersect domain and range of this transitive closure with
2126 * the given domain and range.
2128 * If with_id is set, then try to include as much of the identity mapping
2129 * as possible, by computing
2131 * { i -> j : exists k >= 0:
2132 * k L <= j - i <= k U and exists a: j_p - i_p = M_p a_p }
2134 * instead (i.e., allow k = 0).
2136 * In practice, we compute the difference set
2138 * delta = { j - i | i -> j in map },
2140 * look for stride constraint on the individual dimensions and compute
2141 * (constant) lower and upper bounds for each individual dimension,
2142 * adding a constraint for each bound not equal to infinity.
2144 static __isl_give isl_map *box_closure_on_domain(__isl_take isl_map *map,
2145 __isl_take isl_set *dom, __isl_take isl_set *ran, int with_id)
2147 int i;
2148 int k;
2149 unsigned d;
2150 unsigned nparam;
2151 unsigned total;
2152 isl_space *dim;
2153 isl_set *delta;
2154 isl_map *app = NULL;
2155 isl_basic_set *aff = NULL;
2156 isl_basic_map *bmap = NULL;
2157 isl_vec *obj = NULL;
2158 isl_int opt;
2160 isl_int_init(opt);
2162 delta = isl_map_deltas(isl_map_copy(map));
2164 aff = isl_set_affine_hull(isl_set_copy(delta));
2165 if (!aff)
2166 goto error;
2167 dim = isl_map_get_space(map);
2168 d = isl_space_dim(dim, isl_dim_in);
2169 nparam = isl_space_dim(dim, isl_dim_param);
2170 total = isl_space_dim(dim, isl_dim_all);
2171 bmap = isl_basic_map_alloc_space(dim,
2172 aff->n_div + 1, aff->n_div, 2 * d + 1);
2173 for (i = 0; i < aff->n_div + 1; ++i) {
2174 k = isl_basic_map_alloc_div(bmap);
2175 if (k < 0)
2176 goto error;
2177 isl_int_set_si(bmap->div[k][0], 0);
2179 for (i = 0; i < aff->n_eq; ++i) {
2180 if (!isl_basic_set_eq_is_stride(aff, i))
2181 continue;
2182 k = isl_basic_map_alloc_equality(bmap);
2183 if (k < 0)
2184 goto error;
2185 isl_seq_clr(bmap->eq[k], 1 + nparam);
2186 isl_seq_cpy(bmap->eq[k] + 1 + nparam + d,
2187 aff->eq[i] + 1 + nparam, d);
2188 isl_seq_neg(bmap->eq[k] + 1 + nparam,
2189 aff->eq[i] + 1 + nparam, d);
2190 isl_seq_cpy(bmap->eq[k] + 1 + nparam + 2 * d,
2191 aff->eq[i] + 1 + nparam + d, aff->n_div);
2192 isl_int_set_si(bmap->eq[k][1 + total + aff->n_div], 0);
2194 obj = isl_vec_alloc(map->ctx, 1 + nparam + d);
2195 if (!obj)
2196 goto error;
2197 isl_seq_clr(obj->el, 1 + nparam + d);
2198 for (i = 0; i < d; ++ i) {
2199 enum isl_lp_result res;
2201 isl_int_set_si(obj->el[1 + nparam + i], 1);
2203 res = isl_set_solve_lp(delta, 0, obj->el, map->ctx->one, &opt,
2204 NULL, NULL);
2205 if (res == isl_lp_error)
2206 goto error;
2207 if (res == isl_lp_ok) {
2208 k = isl_basic_map_alloc_inequality(bmap);
2209 if (k < 0)
2210 goto error;
2211 isl_seq_clr(bmap->ineq[k],
2212 1 + nparam + 2 * d + bmap->n_div);
2213 isl_int_set_si(bmap->ineq[k][1 + nparam + i], -1);
2214 isl_int_set_si(bmap->ineq[k][1 + nparam + d + i], 1);
2215 isl_int_neg(bmap->ineq[k][1 + nparam + 2 * d + aff->n_div], opt);
2218 res = isl_set_solve_lp(delta, 1, obj->el, map->ctx->one, &opt,
2219 NULL, NULL);
2220 if (res == isl_lp_error)
2221 goto error;
2222 if (res == isl_lp_ok) {
2223 k = isl_basic_map_alloc_inequality(bmap);
2224 if (k < 0)
2225 goto error;
2226 isl_seq_clr(bmap->ineq[k],
2227 1 + nparam + 2 * d + bmap->n_div);
2228 isl_int_set_si(bmap->ineq[k][1 + nparam + i], 1);
2229 isl_int_set_si(bmap->ineq[k][1 + nparam + d + i], -1);
2230 isl_int_set(bmap->ineq[k][1 + nparam + 2 * d + aff->n_div], opt);
2233 isl_int_set_si(obj->el[1 + nparam + i], 0);
2235 k = isl_basic_map_alloc_inequality(bmap);
2236 if (k < 0)
2237 goto error;
2238 isl_seq_clr(bmap->ineq[k],
2239 1 + nparam + 2 * d + bmap->n_div);
2240 if (!with_id)
2241 isl_int_set_si(bmap->ineq[k][0], -1);
2242 isl_int_set_si(bmap->ineq[k][1 + nparam + 2 * d + aff->n_div], 1);
2244 app = isl_map_from_domain_and_range(dom, ran);
2246 isl_vec_free(obj);
2247 isl_basic_set_free(aff);
2248 isl_map_free(map);
2249 bmap = isl_basic_map_finalize(bmap);
2250 isl_set_free(delta);
2251 isl_int_clear(opt);
2253 map = isl_map_from_basic_map(bmap);
2254 map = isl_map_intersect(map, app);
2256 return map;
2257 error:
2258 isl_vec_free(obj);
2259 isl_basic_map_free(bmap);
2260 isl_basic_set_free(aff);
2261 isl_set_free(dom);
2262 isl_set_free(ran);
2263 isl_map_free(map);
2264 isl_set_free(delta);
2265 isl_int_clear(opt);
2266 return NULL;
2269 /* Given a map, compute the smallest superset of this map that is of the form
2271 * { i -> j : L <= j - i <= U and exists a_p: j_p - i_p = M_p a_p }
2273 * (where p ranges over the (non-parametric) dimensions),
2274 * compute the transitive closure of this map, i.e.,
2276 * { i -> j : exists k > 0:
2277 * k L <= j - i <= k U and exists a: j_p - i_p = M_p a_p }
2279 * and intersect domain and range of this transitive closure with
2280 * domain and range of the original map.
2282 static __isl_give isl_map *box_closure(__isl_take isl_map *map)
2284 isl_set *domain;
2285 isl_set *range;
2287 domain = isl_map_domain(isl_map_copy(map));
2288 domain = isl_set_coalesce(domain);
2289 range = isl_map_range(isl_map_copy(map));
2290 range = isl_set_coalesce(range);
2292 return box_closure_on_domain(map, domain, range, 0);
2295 /* Given a map, compute the smallest superset of this map that is of the form
2297 * { i -> j : L <= j - i <= U and exists a_p: j_p - i_p = M_p a_p }
2299 * (where p ranges over the (non-parametric) dimensions),
2300 * compute the transitive and partially reflexive closure of this map, i.e.,
2302 * { i -> j : exists k >= 0:
2303 * k L <= j - i <= k U and exists a: j_p - i_p = M_p a_p }
2305 * and intersect domain and range of this transitive closure with
2306 * the given domain.
2308 static __isl_give isl_map *box_closure_with_identity(__isl_take isl_map *map,
2309 __isl_take isl_set *dom)
2311 return box_closure_on_domain(map, dom, isl_set_copy(dom), 1);
2314 /* Check whether app is the transitive closure of map.
2315 * In particular, check that app is acyclic and, if so,
2316 * check that
2318 * app \subset (map \cup (map \circ app))
2320 static isl_bool check_exactness_omega(__isl_keep isl_map *map,
2321 __isl_keep isl_map *app)
2323 isl_set *delta;
2324 int i;
2325 isl_bool is_empty, is_exact;
2326 unsigned d;
2327 isl_map *test;
2329 delta = isl_map_deltas(isl_map_copy(app));
2330 d = isl_set_dim(delta, isl_dim_set);
2331 for (i = 0; i < d; ++i)
2332 delta = isl_set_fix_si(delta, isl_dim_set, i, 0);
2333 is_empty = isl_set_is_empty(delta);
2334 isl_set_free(delta);
2335 if (is_empty < 0 || !is_empty)
2336 return is_empty;
2338 test = isl_map_apply_range(isl_map_copy(app), isl_map_copy(map));
2339 test = isl_map_union(test, isl_map_copy(map));
2340 is_exact = isl_map_is_subset(app, test);
2341 isl_map_free(test);
2343 return is_exact;
2346 /* Check if basic map M_i can be combined with all the other
2347 * basic maps such that
2349 * (\cup_j M_j)^+
2351 * can be computed as
2353 * M_i \cup (\cup_{j \ne i} M_i^* \circ M_j \circ M_i^*)^+
2355 * In particular, check if we can compute a compact representation
2356 * of
2358 * M_i^* \circ M_j \circ M_i^*
2360 * for each j != i.
2361 * Let M_i^? be an extension of M_i^+ that allows paths
2362 * of length zero, i.e., the result of box_closure(., 1).
2363 * The criterion, as proposed by Kelly et al., is that
2364 * id = M_i^? - M_i^+ can be represented as a basic map
2365 * and that
2367 * id \circ M_j \circ id = M_j
2369 * for each j != i.
2371 * If this function returns 1, then tc and qc are set to
2372 * M_i^+ and M_i^?, respectively.
2374 static int can_be_split_off(__isl_keep isl_map *map, int i,
2375 __isl_give isl_map **tc, __isl_give isl_map **qc)
2377 isl_map *map_i, *id = NULL;
2378 int j = -1;
2379 isl_set *C;
2381 *tc = NULL;
2382 *qc = NULL;
2384 C = isl_set_union(isl_map_domain(isl_map_copy(map)),
2385 isl_map_range(isl_map_copy(map)));
2386 C = isl_set_from_basic_set(isl_set_simple_hull(C));
2387 if (!C)
2388 goto error;
2390 map_i = isl_map_from_basic_map(isl_basic_map_copy(map->p[i]));
2391 *tc = box_closure(isl_map_copy(map_i));
2392 *qc = box_closure_with_identity(map_i, C);
2393 id = isl_map_subtract(isl_map_copy(*qc), isl_map_copy(*tc));
2395 if (!id || !*qc)
2396 goto error;
2397 if (id->n != 1 || (*qc)->n != 1)
2398 goto done;
2400 for (j = 0; j < map->n; ++j) {
2401 isl_map *map_j, *test;
2402 int is_ok;
2404 if (i == j)
2405 continue;
2406 map_j = isl_map_from_basic_map(
2407 isl_basic_map_copy(map->p[j]));
2408 test = isl_map_apply_range(isl_map_copy(id),
2409 isl_map_copy(map_j));
2410 test = isl_map_apply_range(test, isl_map_copy(id));
2411 is_ok = isl_map_is_equal(test, map_j);
2412 isl_map_free(map_j);
2413 isl_map_free(test);
2414 if (is_ok < 0)
2415 goto error;
2416 if (!is_ok)
2417 break;
2420 done:
2421 isl_map_free(id);
2422 if (j == map->n)
2423 return 1;
2425 isl_map_free(*qc);
2426 isl_map_free(*tc);
2427 *qc = NULL;
2428 *tc = NULL;
2430 return 0;
2431 error:
2432 isl_map_free(id);
2433 isl_map_free(*qc);
2434 isl_map_free(*tc);
2435 *qc = NULL;
2436 *tc = NULL;
2437 return -1;
2440 static __isl_give isl_map *box_closure_with_check(__isl_take isl_map *map,
2441 int *exact)
2443 isl_map *app;
2445 app = box_closure(isl_map_copy(map));
2446 if (exact) {
2447 isl_bool is_exact = check_exactness_omega(map, app);
2449 if (is_exact < 0)
2450 app = isl_map_free(app);
2451 else
2452 *exact = is_exact;
2455 isl_map_free(map);
2456 return app;
2459 /* Compute an overapproximation of the transitive closure of "map"
2460 * using a variation of the algorithm from
2461 * "Transitive Closure of Infinite Graphs and its Applications"
2462 * by Kelly et al.
2464 * We first check whether we can can split of any basic map M_i and
2465 * compute
2467 * (\cup_j M_j)^+
2469 * as
2471 * M_i \cup (\cup_{j \ne i} M_i^* \circ M_j \circ M_i^*)^+
2473 * using a recursive call on the remaining map.
2475 * If not, we simply call box_closure on the whole map.
2477 static __isl_give isl_map *transitive_closure_omega(__isl_take isl_map *map,
2478 int *exact)
2480 int i, j;
2481 isl_bool exact_i;
2482 isl_map *app;
2484 if (!map)
2485 return NULL;
2486 if (map->n == 1)
2487 return box_closure_with_check(map, exact);
2489 for (i = 0; i < map->n; ++i) {
2490 int ok;
2491 isl_map *qc, *tc;
2492 ok = can_be_split_off(map, i, &tc, &qc);
2493 if (ok < 0)
2494 goto error;
2495 if (!ok)
2496 continue;
2498 app = isl_map_alloc_space(isl_map_get_space(map), map->n - 1, 0);
2500 for (j = 0; j < map->n; ++j) {
2501 if (j == i)
2502 continue;
2503 app = isl_map_add_basic_map(app,
2504 isl_basic_map_copy(map->p[j]));
2507 app = isl_map_apply_range(isl_map_copy(qc), app);
2508 app = isl_map_apply_range(app, qc);
2510 app = isl_map_union(tc, transitive_closure_omega(app, NULL));
2511 exact_i = check_exactness_omega(map, app);
2512 if (exact_i == isl_bool_true) {
2513 if (exact)
2514 *exact = exact_i;
2515 isl_map_free(map);
2516 return app;
2518 isl_map_free(app);
2519 if (exact_i < 0)
2520 goto error;
2523 return box_closure_with_check(map, exact);
2524 error:
2525 isl_map_free(map);
2526 return NULL;
2529 /* Compute the transitive closure of "map", or an overapproximation.
2530 * If the result is exact, then *exact is set to 1.
2531 * Simply use map_power to compute the powers of map, but tell
2532 * it to project out the lengths of the paths instead of equating
2533 * the length to a parameter.
2535 __isl_give isl_map *isl_map_transitive_closure(__isl_take isl_map *map,
2536 int *exact)
2538 isl_space *target_dim;
2539 int closed;
2541 if (!map)
2542 goto error;
2544 if (map->ctx->opt->closure == ISL_CLOSURE_BOX)
2545 return transitive_closure_omega(map, exact);
2547 map = isl_map_compute_divs(map);
2548 map = isl_map_coalesce(map);
2549 closed = isl_map_is_transitively_closed(map);
2550 if (closed < 0)
2551 goto error;
2552 if (closed) {
2553 if (exact)
2554 *exact = 1;
2555 return map;
2558 target_dim = isl_map_get_space(map);
2559 map = map_power(map, exact, 1);
2560 map = isl_map_reset_space(map, target_dim);
2562 return map;
2563 error:
2564 isl_map_free(map);
2565 return NULL;
2568 static isl_stat inc_count(__isl_take isl_map *map, void *user)
2570 int *n = user;
2572 *n += map->n;
2574 isl_map_free(map);
2576 return isl_stat_ok;
2579 static isl_stat collect_basic_map(__isl_take isl_map *map, void *user)
2581 int i;
2582 isl_basic_map ***next = user;
2584 for (i = 0; i < map->n; ++i) {
2585 **next = isl_basic_map_copy(map->p[i]);
2586 if (!**next)
2587 goto error;
2588 (*next)++;
2591 isl_map_free(map);
2592 return isl_stat_ok;
2593 error:
2594 isl_map_free(map);
2595 return isl_stat_error;
2598 /* Perform Floyd-Warshall on the given list of basic relations.
2599 * The basic relations may live in different dimensions,
2600 * but basic relations that get assigned to the diagonal of the
2601 * grid have domains and ranges of the same dimension and so
2602 * the standard algorithm can be used because the nested transitive
2603 * closures are only applied to diagonal elements and because all
2604 * compositions are peformed on relations with compatible domains and ranges.
2606 static __isl_give isl_union_map *union_floyd_warshall_on_list(isl_ctx *ctx,
2607 __isl_keep isl_basic_map **list, int n, int *exact)
2609 int i, j, k;
2610 int n_group;
2611 int *group = NULL;
2612 isl_set **set = NULL;
2613 isl_map ***grid = NULL;
2614 isl_union_map *app;
2616 group = setup_groups(ctx, list, n, &set, &n_group);
2617 if (!group)
2618 goto error;
2620 grid = isl_calloc_array(ctx, isl_map **, n_group);
2621 if (!grid)
2622 goto error;
2623 for (i = 0; i < n_group; ++i) {
2624 grid[i] = isl_calloc_array(ctx, isl_map *, n_group);
2625 if (!grid[i])
2626 goto error;
2627 for (j = 0; j < n_group; ++j) {
2628 isl_space *space1, *space2, *space;
2629 space1 = isl_space_reverse(isl_set_get_space(set[i]));
2630 space2 = isl_set_get_space(set[j]);
2631 space = isl_space_join(space1, space2);
2632 grid[i][j] = isl_map_empty(space);
2636 for (k = 0; k < n; ++k) {
2637 i = group[2 * k];
2638 j = group[2 * k + 1];
2639 grid[i][j] = isl_map_union(grid[i][j],
2640 isl_map_from_basic_map(
2641 isl_basic_map_copy(list[k])));
2644 floyd_warshall_iterate(grid, n_group, exact);
2646 app = isl_union_map_empty(isl_map_get_space(grid[0][0]));
2648 for (i = 0; i < n_group; ++i) {
2649 for (j = 0; j < n_group; ++j)
2650 app = isl_union_map_add_map(app, grid[i][j]);
2651 free(grid[i]);
2653 free(grid);
2655 for (i = 0; i < 2 * n; ++i)
2656 isl_set_free(set[i]);
2657 free(set);
2659 free(group);
2660 return app;
2661 error:
2662 if (grid)
2663 for (i = 0; i < n_group; ++i) {
2664 if (!grid[i])
2665 continue;
2666 for (j = 0; j < n_group; ++j)
2667 isl_map_free(grid[i][j]);
2668 free(grid[i]);
2670 free(grid);
2671 if (set) {
2672 for (i = 0; i < 2 * n; ++i)
2673 isl_set_free(set[i]);
2674 free(set);
2676 free(group);
2677 return NULL;
2680 /* Perform Floyd-Warshall on the given union relation.
2681 * The implementation is very similar to that for non-unions.
2682 * The main difference is that it is applied unconditionally.
2683 * We first extract a list of basic maps from the union map
2684 * and then perform the algorithm on this list.
2686 static __isl_give isl_union_map *union_floyd_warshall(
2687 __isl_take isl_union_map *umap, int *exact)
2689 int i, n;
2690 isl_ctx *ctx;
2691 isl_basic_map **list = NULL;
2692 isl_basic_map **next;
2693 isl_union_map *res;
2695 n = 0;
2696 if (isl_union_map_foreach_map(umap, inc_count, &n) < 0)
2697 goto error;
2699 ctx = isl_union_map_get_ctx(umap);
2700 list = isl_calloc_array(ctx, isl_basic_map *, n);
2701 if (!list)
2702 goto error;
2704 next = list;
2705 if (isl_union_map_foreach_map(umap, collect_basic_map, &next) < 0)
2706 goto error;
2708 res = union_floyd_warshall_on_list(ctx, list, n, exact);
2710 if (list) {
2711 for (i = 0; i < n; ++i)
2712 isl_basic_map_free(list[i]);
2713 free(list);
2716 isl_union_map_free(umap);
2717 return res;
2718 error:
2719 if (list) {
2720 for (i = 0; i < n; ++i)
2721 isl_basic_map_free(list[i]);
2722 free(list);
2724 isl_union_map_free(umap);
2725 return NULL;
2728 /* Decompose the give union relation into strongly connected components.
2729 * The implementation is essentially the same as that of
2730 * construct_power_components with the major difference that all
2731 * operations are performed on union maps.
2733 static __isl_give isl_union_map *union_components(
2734 __isl_take isl_union_map *umap, int *exact)
2736 int i;
2737 int n;
2738 isl_ctx *ctx;
2739 isl_basic_map **list = NULL;
2740 isl_basic_map **next;
2741 isl_union_map *path = NULL;
2742 struct isl_tc_follows_data data;
2743 struct isl_tarjan_graph *g = NULL;
2744 int c, l;
2745 int recheck = 0;
2747 n = 0;
2748 if (isl_union_map_foreach_map(umap, inc_count, &n) < 0)
2749 goto error;
2751 if (n == 0)
2752 return umap;
2753 if (n <= 1)
2754 return union_floyd_warshall(umap, exact);
2756 ctx = isl_union_map_get_ctx(umap);
2757 list = isl_calloc_array(ctx, isl_basic_map *, n);
2758 if (!list)
2759 goto error;
2761 next = list;
2762 if (isl_union_map_foreach_map(umap, collect_basic_map, &next) < 0)
2763 goto error;
2765 data.list = list;
2766 data.check_closed = 0;
2767 g = isl_tarjan_graph_init(ctx, n, &basic_map_follows, &data);
2768 if (!g)
2769 goto error;
2771 c = 0;
2772 i = 0;
2773 l = n;
2774 path = isl_union_map_empty(isl_union_map_get_space(umap));
2775 while (l) {
2776 isl_union_map *comp;
2777 isl_union_map *path_comp, *path_comb;
2778 comp = isl_union_map_empty(isl_union_map_get_space(umap));
2779 while (g->order[i] != -1) {
2780 comp = isl_union_map_add_map(comp,
2781 isl_map_from_basic_map(
2782 isl_basic_map_copy(list[g->order[i]])));
2783 --l;
2784 ++i;
2786 path_comp = union_floyd_warshall(comp, exact);
2787 path_comb = isl_union_map_apply_range(isl_union_map_copy(path),
2788 isl_union_map_copy(path_comp));
2789 path = isl_union_map_union(path, path_comp);
2790 path = isl_union_map_union(path, path_comb);
2791 ++i;
2792 ++c;
2795 if (c > 1 && data.check_closed && !*exact) {
2796 int closed;
2798 closed = isl_union_map_is_transitively_closed(path);
2799 if (closed < 0)
2800 goto error;
2801 recheck = !closed;
2804 isl_tarjan_graph_free(g);
2806 for (i = 0; i < n; ++i)
2807 isl_basic_map_free(list[i]);
2808 free(list);
2810 if (recheck) {
2811 isl_union_map_free(path);
2812 return union_floyd_warshall(umap, exact);
2815 isl_union_map_free(umap);
2817 return path;
2818 error:
2819 isl_tarjan_graph_free(g);
2820 if (list) {
2821 for (i = 0; i < n; ++i)
2822 isl_basic_map_free(list[i]);
2823 free(list);
2825 isl_union_map_free(umap);
2826 isl_union_map_free(path);
2827 return NULL;
2830 /* Compute the transitive closure of "umap", or an overapproximation.
2831 * If the result is exact, then *exact is set to 1.
2833 __isl_give isl_union_map *isl_union_map_transitive_closure(
2834 __isl_take isl_union_map *umap, int *exact)
2836 int closed;
2838 if (!umap)
2839 return NULL;
2841 if (exact)
2842 *exact = 1;
2844 umap = isl_union_map_compute_divs(umap);
2845 umap = isl_union_map_coalesce(umap);
2846 closed = isl_union_map_is_transitively_closed(umap);
2847 if (closed < 0)
2848 goto error;
2849 if (closed)
2850 return umap;
2851 umap = union_components(umap, exact);
2852 return umap;
2853 error:
2854 isl_union_map_free(umap);
2855 return NULL;
2858 struct isl_union_power {
2859 isl_union_map *pow;
2860 int *exact;
2863 static isl_stat power(__isl_take isl_map *map, void *user)
2865 struct isl_union_power *up = user;
2867 map = isl_map_power(map, up->exact);
2868 up->pow = isl_union_map_from_map(map);
2870 return isl_stat_error;
2873 /* Construct a map [[x]->[y]] -> [y-x], with parameters prescribed by "dim".
2875 static __isl_give isl_union_map *deltas_map(__isl_take isl_space *dim)
2877 isl_basic_map *bmap;
2879 dim = isl_space_add_dims(dim, isl_dim_in, 1);
2880 dim = isl_space_add_dims(dim, isl_dim_out, 1);
2881 bmap = isl_basic_map_universe(dim);
2882 bmap = isl_basic_map_deltas_map(bmap);
2884 return isl_union_map_from_map(isl_map_from_basic_map(bmap));
2887 /* Compute the positive powers of "map", or an overapproximation.
2888 * The result maps the exponent to a nested copy of the corresponding power.
2889 * If the result is exact, then *exact is set to 1.
2891 __isl_give isl_union_map *isl_union_map_power(__isl_take isl_union_map *umap,
2892 int *exact)
2894 int n;
2895 isl_union_map *inc;
2896 isl_union_map *dm;
2898 if (!umap)
2899 return NULL;
2900 n = isl_union_map_n_map(umap);
2901 if (n == 0)
2902 return umap;
2903 if (n == 1) {
2904 struct isl_union_power up = { NULL, exact };
2905 isl_union_map_foreach_map(umap, &power, &up);
2906 isl_union_map_free(umap);
2907 return up.pow;
2909 inc = isl_union_map_from_map(increment(isl_union_map_get_space(umap)));
2910 umap = isl_union_map_product(inc, umap);
2911 umap = isl_union_map_transitive_closure(umap, exact);
2912 umap = isl_union_map_zip(umap);
2913 dm = deltas_map(isl_union_map_get_space(umap));
2914 umap = isl_union_map_apply_domain(umap, dm);
2916 return umap;
2919 #undef TYPE
2920 #define TYPE isl_map
2921 #include "isl_power_templ.c"
2923 #undef TYPE
2924 #define TYPE isl_union_map
2925 #include "isl_power_templ.c"