isl_map_simplify.c: is_parallel_except: return isl_bool
[isl.git] / isl_transitive_closure.c
blob368fa92acd06c97e804f1ae8165cf5e6b1908a00
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 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 int k;
430 isl_bool is_id;
432 test = isl_basic_map_copy(path);
433 test = isl_basic_map_extend_constraints(test, 1, 0);
434 k = isl_basic_map_alloc_equality(test);
435 if (k < 0)
436 goto error;
437 isl_seq_clr(test->eq[k], 1 + isl_basic_map_total_dim(test));
438 isl_int_set_si(test->eq[k][pos], 1);
439 test = isl_basic_map_gauss(test, NULL);
440 id = isl_basic_map_identity(isl_basic_map_get_space(path));
441 is_id = isl_basic_map_is_equal(test, id);
442 isl_basic_map_free(test);
443 isl_basic_map_free(id);
444 return is_id;
445 error:
446 isl_basic_map_free(test);
447 return isl_bool_error;
450 /* If any of the constraints is found to be impure then this function
451 * sets *impurity to 1.
453 * If impurity is NULL then we are dealing with a non-parametric set
454 * and so the constraints are obviously PURE_VAR.
456 static __isl_give isl_basic_map *add_delta_constraints(
457 __isl_take isl_basic_map *path,
458 __isl_keep isl_basic_set *delta, unsigned off, unsigned nparam,
459 unsigned d, int *div_purity, int eq, int *impurity)
461 int i, k;
462 int n = eq ? delta->n_eq : delta->n_ineq;
463 isl_int **delta_c = eq ? delta->eq : delta->ineq;
464 unsigned n_div;
466 n_div = isl_basic_set_dim(delta, isl_dim_div);
468 for (i = 0; i < n; ++i) {
469 isl_int *path_c;
470 int p = PURE_VAR;
471 if (impurity)
472 p = purity(delta, delta_c[i], div_purity, eq);
473 if (p < 0)
474 goto error;
475 if (p != PURE_VAR && p != PURE_PARAM && !*impurity)
476 *impurity = 1;
477 if (p == IMPURE)
478 continue;
479 if (eq && p != MIXED) {
480 k = isl_basic_map_alloc_equality(path);
481 if (k < 0)
482 goto error;
483 path_c = path->eq[k];
484 } else {
485 k = isl_basic_map_alloc_inequality(path);
486 if (k < 0)
487 goto error;
488 path_c = path->ineq[k];
490 isl_seq_clr(path_c, 1 + isl_basic_map_total_dim(path));
491 if (p == PURE_VAR) {
492 isl_seq_cpy(path_c + off,
493 delta_c[i] + 1 + nparam, d);
494 isl_int_set(path_c[off + d], delta_c[i][0]);
495 } else if (p == PURE_PARAM) {
496 isl_seq_cpy(path_c, delta_c[i], 1 + nparam);
497 } else {
498 isl_seq_cpy(path_c + off,
499 delta_c[i] + 1 + nparam, d);
500 isl_seq_cpy(path_c, delta_c[i], 1 + nparam);
502 isl_seq_cpy(path_c + off - n_div,
503 delta_c[i] + 1 + nparam + d, n_div);
506 return path;
507 error:
508 isl_basic_map_free(path);
509 return NULL;
512 /* Given a set of offsets "delta", construct a relation of the
513 * given dimension specification (Z^{n+1} -> Z^{n+1}) that
514 * is an overapproximation of the relations that
515 * maps an element x to any element that can be reached
516 * by taking a non-negative number of steps along any of
517 * the elements in "delta".
518 * That is, construct an approximation of
520 * { [x] -> [y] : exists f \in \delta, k \in Z :
521 * y = x + k [f, 1] and k >= 0 }
523 * For any element in this relation, the number of steps taken
524 * is equal to the difference in the final coordinates.
526 * In particular, let delta be defined as
528 * \delta = [p] -> { [x] : A x + a >= 0 and B p + b >= 0 and
529 * C x + C'p + c >= 0 and
530 * D x + D'p + d >= 0 }
532 * where the constraints C x + C'p + c >= 0 are such that the parametric
533 * constant term of each constraint j, "C_j x + C'_j p + c_j",
534 * can never attain positive values, then the relation is constructed as
536 * { [x] -> [y] : exists [f, k] \in Z^{n+1} : y = x + f and
537 * A f + k a >= 0 and B p + b >= 0 and
538 * C f + C'p + c >= 0 and k >= 1 }
539 * union { [x] -> [x] }
541 * If the zero-length paths happen to correspond exactly to the identity
542 * mapping, then we return
544 * { [x] -> [y] : exists [f, k] \in Z^{n+1} : y = x + f and
545 * A f + k a >= 0 and B p + b >= 0 and
546 * C f + C'p + c >= 0 and k >= 0 }
548 * instead.
550 * Existentially quantified variables in \delta are handled by
551 * classifying them as independent of the parameters, purely
552 * parameter dependent and others. Constraints containing
553 * any of the other existentially quantified variables are removed.
554 * This is safe, but leads to an additional overapproximation.
556 * If there are any impure constraints, then we also eliminate
557 * the parameters from \delta, resulting in a set
559 * \delta' = { [x] : E x + e >= 0 }
561 * and add the constraints
563 * E f + k e >= 0
565 * to the constructed relation.
567 static __isl_give isl_map *path_along_delta(__isl_take isl_space *space,
568 __isl_take isl_basic_set *delta)
570 isl_basic_map *path = NULL;
571 unsigned d;
572 unsigned n_div;
573 unsigned nparam;
574 unsigned off;
575 int i, k;
576 isl_bool is_id;
577 int *div_purity = NULL;
578 int impurity = 0;
580 if (!delta)
581 goto error;
582 n_div = isl_basic_set_dim(delta, isl_dim_div);
583 d = isl_basic_set_dim(delta, isl_dim_set);
584 nparam = isl_basic_set_dim(delta, isl_dim_param);
585 path = isl_basic_map_alloc_space(isl_space_copy(space), n_div + d + 1,
586 d + 1 + delta->n_eq, delta->n_eq + delta->n_ineq + 1);
587 off = 1 + nparam + 2 * (d + 1) + n_div;
589 for (i = 0; i < n_div + d + 1; ++i) {
590 k = isl_basic_map_alloc_div(path);
591 if (k < 0)
592 goto error;
593 isl_int_set_si(path->div[k][0], 0);
596 for (i = 0; i < d + 1; ++i) {
597 k = isl_basic_map_alloc_equality(path);
598 if (k < 0)
599 goto error;
600 isl_seq_clr(path->eq[k], 1 + isl_basic_map_total_dim(path));
601 isl_int_set_si(path->eq[k][1 + nparam + i], 1);
602 isl_int_set_si(path->eq[k][1 + nparam + d + 1 + i], -1);
603 isl_int_set_si(path->eq[k][off + i], 1);
606 div_purity = get_div_purity(delta);
607 if (n_div && !div_purity)
608 goto error;
610 path = add_delta_constraints(path, delta, off, nparam, d,
611 div_purity, 1, &impurity);
612 path = add_delta_constraints(path, delta, off, nparam, d,
613 div_purity, 0, &impurity);
614 if (impurity) {
615 isl_space *space = isl_basic_set_get_space(delta);
616 delta = isl_basic_set_project_out(delta,
617 isl_dim_param, 0, nparam);
618 delta = isl_basic_set_add_dims(delta, isl_dim_param, nparam);
619 delta = isl_basic_set_reset_space(delta, space);
620 if (!delta)
621 goto error;
622 path = isl_basic_map_extend_constraints(path, delta->n_eq,
623 delta->n_ineq + 1);
624 path = add_delta_constraints(path, delta, off, nparam, d,
625 NULL, 1, NULL);
626 path = add_delta_constraints(path, delta, off, nparam, d,
627 NULL, 0, NULL);
628 path = isl_basic_map_gauss(path, NULL);
631 is_id = empty_path_is_identity(path, off + d);
632 if (is_id < 0)
633 goto error;
635 k = isl_basic_map_alloc_inequality(path);
636 if (k < 0)
637 goto error;
638 isl_seq_clr(path->ineq[k], 1 + isl_basic_map_total_dim(path));
639 if (!is_id)
640 isl_int_set_si(path->ineq[k][0], -1);
641 isl_int_set_si(path->ineq[k][off + d], 1);
643 free(div_purity);
644 isl_basic_set_free(delta);
645 path = isl_basic_map_finalize(path);
646 if (is_id) {
647 isl_space_free(space);
648 return isl_map_from_basic_map(path);
650 return isl_basic_map_union(path, isl_basic_map_identity(space));
651 error:
652 free(div_purity);
653 isl_space_free(space);
654 isl_basic_set_free(delta);
655 isl_basic_map_free(path);
656 return NULL;
659 /* Given a dimension specification Z^{n+1} -> Z^{n+1} and a parameter "param",
660 * construct a map that equates the parameter to the difference
661 * in the final coordinates and imposes that this difference is positive.
662 * That is, construct
664 * { [x,x_s] -> [y,y_s] : k = y_s - x_s > 0 }
666 static __isl_give isl_map *equate_parameter_to_length(
667 __isl_take isl_space *space, unsigned param)
669 struct isl_basic_map *bmap;
670 unsigned d;
671 unsigned nparam;
672 int k;
674 d = isl_space_dim(space, isl_dim_in);
675 nparam = isl_space_dim(space, isl_dim_param);
676 bmap = isl_basic_map_alloc_space(space, 0, 1, 1);
677 k = isl_basic_map_alloc_equality(bmap);
678 if (k < 0)
679 goto error;
680 isl_seq_clr(bmap->eq[k], 1 + isl_basic_map_total_dim(bmap));
681 isl_int_set_si(bmap->eq[k][1 + param], -1);
682 isl_int_set_si(bmap->eq[k][1 + nparam + d - 1], -1);
683 isl_int_set_si(bmap->eq[k][1 + nparam + d + d - 1], 1);
685 k = isl_basic_map_alloc_inequality(bmap);
686 if (k < 0)
687 goto error;
688 isl_seq_clr(bmap->ineq[k], 1 + isl_basic_map_total_dim(bmap));
689 isl_int_set_si(bmap->ineq[k][1 + param], 1);
690 isl_int_set_si(bmap->ineq[k][0], -1);
692 bmap = isl_basic_map_finalize(bmap);
693 return isl_map_from_basic_map(bmap);
694 error:
695 isl_basic_map_free(bmap);
696 return NULL;
699 /* Check whether "path" is acyclic, where the last coordinates of domain
700 * and range of path encode the number of steps taken.
701 * That is, check whether
703 * { d | d = y - x and (x,y) in path }
705 * does not contain any element with positive last coordinate (positive length)
706 * and zero remaining coordinates (cycle).
708 static isl_bool is_acyclic(__isl_take isl_map *path)
710 int i;
711 isl_bool acyclic;
712 unsigned dim;
713 struct isl_set *delta;
715 delta = isl_map_deltas(path);
716 dim = isl_set_dim(delta, isl_dim_set);
717 for (i = 0; i < dim; ++i) {
718 if (i == dim -1)
719 delta = isl_set_lower_bound_si(delta, isl_dim_set, i, 1);
720 else
721 delta = isl_set_fix_si(delta, isl_dim_set, i, 0);
724 acyclic = isl_set_is_empty(delta);
725 isl_set_free(delta);
727 return acyclic;
730 /* Given a union of basic maps R = \cup_i R_i \subseteq D \times D
731 * and a dimension specification (Z^{n+1} -> Z^{n+1}),
732 * construct a map that is an overapproximation of the map
733 * that takes an element from the space D \times Z to another
734 * element from the same space, such that the first n coordinates of the
735 * difference between them is a sum of differences between images
736 * and pre-images in one of the R_i and such that the last coordinate
737 * is equal to the number of steps taken.
738 * That is, let
740 * \Delta_i = { y - x | (x, y) in R_i }
742 * then the constructed map is an overapproximation of
744 * { (x) -> (x + d) | \exists k_i >= 0, \delta_i \in \Delta_i :
745 * d = (\sum_i k_i \delta_i, \sum_i k_i) }
747 * The elements of the singleton \Delta_i's are collected as the
748 * rows of the steps matrix. For all these \Delta_i's together,
749 * a single path is constructed.
750 * For each of the other \Delta_i's, we compute an overapproximation
751 * of the paths along elements of \Delta_i.
752 * Since each of these paths performs an addition, composition is
753 * symmetric and we can simply compose all resulting paths in any order.
755 static __isl_give isl_map *construct_extended_path(__isl_take isl_space *space,
756 __isl_keep isl_map *map, int *project)
758 struct isl_mat *steps = NULL;
759 struct isl_map *path = NULL;
760 unsigned d;
761 int i, j, n;
763 if (!map)
764 goto error;
766 d = isl_map_dim(map, isl_dim_in);
768 path = isl_map_identity(isl_space_copy(space));
770 steps = isl_mat_alloc(map->ctx, map->n, d);
771 if (!steps)
772 goto error;
774 n = 0;
775 for (i = 0; i < map->n; ++i) {
776 struct isl_basic_set *delta;
778 delta = isl_basic_map_deltas(isl_basic_map_copy(map->p[i]));
780 for (j = 0; j < d; ++j) {
781 isl_bool fixed;
783 fixed = isl_basic_set_plain_dim_is_fixed(delta, j,
784 &steps->row[n][j]);
785 if (fixed < 0) {
786 isl_basic_set_free(delta);
787 goto error;
789 if (!fixed)
790 break;
794 if (j < d) {
795 path = isl_map_apply_range(path,
796 path_along_delta(isl_space_copy(space), delta));
797 path = isl_map_coalesce(path);
798 } else {
799 isl_basic_set_free(delta);
800 ++n;
804 if (n > 0) {
805 steps->n_row = n;
806 path = isl_map_apply_range(path,
807 path_along_steps(isl_space_copy(space), steps));
810 if (project && *project) {
811 *project = is_acyclic(isl_map_copy(path));
812 if (*project < 0)
813 goto error;
816 isl_space_free(space);
817 isl_mat_free(steps);
818 return path;
819 error:
820 isl_space_free(space);
821 isl_mat_free(steps);
822 isl_map_free(path);
823 return NULL;
826 static isl_bool isl_set_overlaps(__isl_keep isl_set *set1,
827 __isl_keep isl_set *set2)
829 isl_set *i;
830 isl_bool no_overlap;
832 if (!set1 || !set2)
833 return isl_bool_error;
835 if (!isl_space_tuple_is_equal(set1->dim, isl_dim_set,
836 set2->dim, isl_dim_set))
837 return isl_bool_false;
839 i = isl_set_intersect(isl_set_copy(set1), isl_set_copy(set2));
840 no_overlap = isl_set_is_empty(i);
841 isl_set_free(i);
843 return isl_bool_not(no_overlap);
846 /* Given a union of basic maps R = \cup_i R_i \subseteq D \times D
847 * and a dimension specification (Z^{n+1} -> Z^{n+1}),
848 * construct a map that is an overapproximation of the map
849 * that takes an element from the dom R \times Z to an
850 * element from ran R \times Z, such that the first n coordinates of the
851 * difference between them is a sum of differences between images
852 * and pre-images in one of the R_i and such that the last coordinate
853 * is equal to the number of steps taken.
854 * That is, let
856 * \Delta_i = { y - x | (x, y) in R_i }
858 * then the constructed map is an overapproximation of
860 * { (x) -> (x + d) | \exists k_i >= 0, \delta_i \in \Delta_i :
861 * d = (\sum_i k_i \delta_i, \sum_i k_i) and
862 * x in dom R and x + d in ran R and
863 * \sum_i k_i >= 1 }
865 static __isl_give isl_map *construct_component(__isl_take isl_space *dim,
866 __isl_keep isl_map *map, int *exact, int project)
868 struct isl_set *domain = NULL;
869 struct isl_set *range = NULL;
870 struct isl_map *app = NULL;
871 struct isl_map *path = NULL;
872 isl_bool overlaps;
874 domain = isl_map_domain(isl_map_copy(map));
875 domain = isl_set_coalesce(domain);
876 range = isl_map_range(isl_map_copy(map));
877 range = isl_set_coalesce(range);
878 overlaps = isl_set_overlaps(domain, range);
879 if (overlaps < 0 || !overlaps) {
880 isl_set_free(domain);
881 isl_set_free(range);
882 isl_space_free(dim);
884 if (overlaps < 0)
885 map = NULL;
886 map = isl_map_copy(map);
887 map = isl_map_add_dims(map, isl_dim_in, 1);
888 map = isl_map_add_dims(map, isl_dim_out, 1);
889 map = set_path_length(map, 1, 1);
890 return map;
892 app = isl_map_from_domain_and_range(domain, range);
893 app = isl_map_add_dims(app, isl_dim_in, 1);
894 app = isl_map_add_dims(app, isl_dim_out, 1);
896 path = construct_extended_path(isl_space_copy(dim), map,
897 exact && *exact ? &project : NULL);
898 app = isl_map_intersect(app, path);
900 if (exact && *exact &&
901 (*exact = check_exactness(isl_map_copy(map), isl_map_copy(app),
902 project)) < 0)
903 goto error;
905 isl_space_free(dim);
906 app = set_path_length(app, 0, 1);
907 return app;
908 error:
909 isl_space_free(dim);
910 isl_map_free(app);
911 return NULL;
914 /* Call construct_component and, if "project" is set, project out
915 * the final coordinates.
917 static __isl_give isl_map *construct_projected_component(
918 __isl_take isl_space *space,
919 __isl_keep isl_map *map, int *exact, int project)
921 isl_map *app;
922 unsigned d;
924 if (!space)
925 return NULL;
926 d = isl_space_dim(space, isl_dim_in);
928 app = construct_component(space, map, exact, project);
929 if (project) {
930 app = isl_map_project_out(app, isl_dim_in, d - 1, 1);
931 app = isl_map_project_out(app, isl_dim_out, d - 1, 1);
933 return app;
936 /* Compute an extended version, i.e., with path lengths, of
937 * an overapproximation of the transitive closure of "bmap"
938 * with path lengths greater than or equal to zero and with
939 * domain and range equal to "dom".
941 static __isl_give isl_map *q_closure(__isl_take isl_space *dim,
942 __isl_take isl_set *dom, __isl_keep isl_basic_map *bmap, int *exact)
944 int project = 1;
945 isl_map *path;
946 isl_map *map;
947 isl_map *app;
949 dom = isl_set_add_dims(dom, isl_dim_set, 1);
950 app = isl_map_from_domain_and_range(dom, isl_set_copy(dom));
951 map = isl_map_from_basic_map(isl_basic_map_copy(bmap));
952 path = construct_extended_path(dim, map, &project);
953 app = isl_map_intersect(app, path);
955 if ((*exact = check_exactness(map, isl_map_copy(app), project)) < 0)
956 goto error;
958 return app;
959 error:
960 isl_map_free(app);
961 return NULL;
964 /* Check whether qc has any elements of length at least one
965 * with domain and/or range outside of dom and ran.
967 static isl_bool has_spurious_elements(__isl_keep isl_map *qc,
968 __isl_keep isl_set *dom, __isl_keep isl_set *ran)
970 isl_set *s;
971 isl_bool subset;
972 unsigned d;
974 if (!qc || !dom || !ran)
975 return isl_bool_error;
977 d = isl_map_dim(qc, isl_dim_in);
979 qc = isl_map_copy(qc);
980 qc = set_path_length(qc, 0, 1);
981 qc = isl_map_project_out(qc, isl_dim_in, d - 1, 1);
982 qc = isl_map_project_out(qc, isl_dim_out, d - 1, 1);
984 s = isl_map_domain(isl_map_copy(qc));
985 subset = isl_set_is_subset(s, dom);
986 isl_set_free(s);
987 if (subset < 0)
988 goto error;
989 if (!subset) {
990 isl_map_free(qc);
991 return isl_bool_true;
994 s = isl_map_range(qc);
995 subset = isl_set_is_subset(s, ran);
996 isl_set_free(s);
998 return isl_bool_not(subset);
999 error:
1000 isl_map_free(qc);
1001 return isl_bool_error;
1004 #define LEFT 2
1005 #define RIGHT 1
1007 /* For each basic map in "map", except i, check whether it combines
1008 * with the transitive closure that is reflexive on C combines
1009 * to the left and to the right.
1011 * In particular, if
1013 * dom map_j \subseteq C
1015 * then right[j] is set to 1. Otherwise, if
1017 * ran map_i \cap dom map_j = \emptyset
1019 * then right[j] is set to 0. Otherwise, composing to the right
1020 * is impossible.
1022 * Similar, for composing to the left, we have if
1024 * ran map_j \subseteq C
1026 * then left[j] is set to 1. Otherwise, if
1028 * dom map_i \cap ran map_j = \emptyset
1030 * then left[j] is set to 0. Otherwise, composing to the left
1031 * is impossible.
1033 * The return value is or'd with LEFT if composing to the left
1034 * is possible and with RIGHT if composing to the right is possible.
1036 static int composability(__isl_keep isl_set *C, int i,
1037 isl_set **dom, isl_set **ran, int *left, int *right,
1038 __isl_keep isl_map *map)
1040 int j;
1041 int ok;
1043 ok = LEFT | RIGHT;
1044 for (j = 0; j < map->n && ok; ++j) {
1045 isl_bool overlaps, subset;
1046 if (j == i)
1047 continue;
1049 if (ok & RIGHT) {
1050 if (!dom[j])
1051 dom[j] = isl_set_from_basic_set(
1052 isl_basic_map_domain(
1053 isl_basic_map_copy(map->p[j])));
1054 if (!dom[j])
1055 return -1;
1056 overlaps = isl_set_overlaps(ran[i], dom[j]);
1057 if (overlaps < 0)
1058 return -1;
1059 if (!overlaps)
1060 right[j] = 0;
1061 else {
1062 subset = isl_set_is_subset(dom[j], C);
1063 if (subset < 0)
1064 return -1;
1065 if (subset)
1066 right[j] = 1;
1067 else
1068 ok &= ~RIGHT;
1072 if (ok & LEFT) {
1073 if (!ran[j])
1074 ran[j] = isl_set_from_basic_set(
1075 isl_basic_map_range(
1076 isl_basic_map_copy(map->p[j])));
1077 if (!ran[j])
1078 return -1;
1079 overlaps = isl_set_overlaps(dom[i], ran[j]);
1080 if (overlaps < 0)
1081 return -1;
1082 if (!overlaps)
1083 left[j] = 0;
1084 else {
1085 subset = isl_set_is_subset(ran[j], C);
1086 if (subset < 0)
1087 return -1;
1088 if (subset)
1089 left[j] = 1;
1090 else
1091 ok &= ~LEFT;
1096 return ok;
1099 static __isl_give isl_map *anonymize(__isl_take isl_map *map)
1101 map = isl_map_reset(map, isl_dim_in);
1102 map = isl_map_reset(map, isl_dim_out);
1103 return map;
1106 /* Return a map that is a union of the basic maps in "map", except i,
1107 * composed to left and right with qc based on the entries of "left"
1108 * and "right".
1110 static __isl_give isl_map *compose(__isl_keep isl_map *map, int i,
1111 __isl_take isl_map *qc, int *left, int *right)
1113 int j;
1114 isl_map *comp;
1116 comp = isl_map_empty(isl_map_get_space(map));
1117 for (j = 0; j < map->n; ++j) {
1118 isl_map *map_j;
1120 if (j == i)
1121 continue;
1123 map_j = isl_map_from_basic_map(isl_basic_map_copy(map->p[j]));
1124 map_j = anonymize(map_j);
1125 if (left && left[j])
1126 map_j = isl_map_apply_range(map_j, isl_map_copy(qc));
1127 if (right && right[j])
1128 map_j = isl_map_apply_range(isl_map_copy(qc), map_j);
1129 comp = isl_map_union(comp, map_j);
1132 comp = isl_map_compute_divs(comp);
1133 comp = isl_map_coalesce(comp);
1135 isl_map_free(qc);
1137 return comp;
1140 /* Compute the transitive closure of "map" incrementally by
1141 * computing
1143 * map_i^+ \cup qc^+
1145 * or
1147 * map_i^+ \cup ((id \cup map_i^) \circ qc^+)
1149 * or
1151 * map_i^+ \cup (qc^+ \circ (id \cup map_i^))
1153 * depending on whether left or right are NULL.
1155 static __isl_give isl_map *compute_incremental(
1156 __isl_take isl_space *space, __isl_keep isl_map *map,
1157 int i, __isl_take isl_map *qc, int *left, int *right, int *exact)
1159 isl_map *map_i;
1160 isl_map *tc;
1161 isl_map *rtc = NULL;
1163 if (!map)
1164 goto error;
1165 isl_assert(map->ctx, left || right, goto error);
1167 map_i = isl_map_from_basic_map(isl_basic_map_copy(map->p[i]));
1168 tc = construct_projected_component(isl_space_copy(space), map_i,
1169 exact, 1);
1170 isl_map_free(map_i);
1172 if (*exact)
1173 qc = isl_map_transitive_closure(qc, exact);
1175 if (!*exact) {
1176 isl_space_free(space);
1177 isl_map_free(tc);
1178 isl_map_free(qc);
1179 return isl_map_universe(isl_map_get_space(map));
1182 if (!left || !right)
1183 rtc = isl_map_union(isl_map_copy(tc),
1184 isl_map_identity(isl_map_get_space(tc)));
1185 if (!right)
1186 qc = isl_map_apply_range(rtc, qc);
1187 if (!left)
1188 qc = isl_map_apply_range(qc, rtc);
1189 qc = isl_map_union(tc, qc);
1191 isl_space_free(space);
1193 return qc;
1194 error:
1195 isl_space_free(space);
1196 isl_map_free(qc);
1197 return NULL;
1200 /* Given a map "map", try to find a basic map such that
1201 * map^+ can be computed as
1203 * map^+ = map_i^+ \cup
1204 * \bigcup_j ((map_i^+ \cup Id_C)^+ \circ map_j \circ (map_i^+ \cup Id_C))^+
1206 * with C the simple hull of the domain and range of the input map.
1207 * map_i^ \cup Id_C is computed by allowing the path lengths to be zero
1208 * and by intersecting domain and range with C.
1209 * Of course, we need to check that this is actually equal to map_i^ \cup Id_C.
1210 * Also, we only use the incremental computation if all the transitive
1211 * closures are exact and if the number of basic maps in the union,
1212 * after computing the integer divisions, is smaller than the number
1213 * of basic maps in the input map.
1215 static isl_bool incremental_on_entire_domain(__isl_keep isl_space *space,
1216 __isl_keep isl_map *map,
1217 isl_set **dom, isl_set **ran, int *left, int *right,
1218 __isl_give isl_map **res)
1220 int i;
1221 isl_set *C;
1222 unsigned d;
1224 *res = NULL;
1226 C = isl_set_union(isl_map_domain(isl_map_copy(map)),
1227 isl_map_range(isl_map_copy(map)));
1228 C = isl_set_from_basic_set(isl_set_simple_hull(C));
1229 if (!C)
1230 return isl_bool_error;
1231 if (C->n != 1) {
1232 isl_set_free(C);
1233 return isl_bool_false;
1236 d = isl_map_dim(map, isl_dim_in);
1238 for (i = 0; i < map->n; ++i) {
1239 isl_map *qc;
1240 int exact_i;
1241 isl_bool spurious;
1242 int j;
1243 dom[i] = isl_set_from_basic_set(isl_basic_map_domain(
1244 isl_basic_map_copy(map->p[i])));
1245 ran[i] = isl_set_from_basic_set(isl_basic_map_range(
1246 isl_basic_map_copy(map->p[i])));
1247 qc = q_closure(isl_space_copy(space), isl_set_copy(C),
1248 map->p[i], &exact_i);
1249 if (!qc)
1250 goto error;
1251 if (!exact_i) {
1252 isl_map_free(qc);
1253 continue;
1255 spurious = has_spurious_elements(qc, dom[i], ran[i]);
1256 if (spurious) {
1257 isl_map_free(qc);
1258 if (spurious < 0)
1259 goto error;
1260 continue;
1262 qc = isl_map_project_out(qc, isl_dim_in, d, 1);
1263 qc = isl_map_project_out(qc, isl_dim_out, d, 1);
1264 qc = isl_map_compute_divs(qc);
1265 for (j = 0; j < map->n; ++j)
1266 left[j] = right[j] = 1;
1267 qc = compose(map, i, qc, left, right);
1268 if (!qc)
1269 goto error;
1270 if (qc->n >= map->n) {
1271 isl_map_free(qc);
1272 continue;
1274 *res = compute_incremental(isl_space_copy(space), map, i, qc,
1275 left, right, &exact_i);
1276 if (!*res)
1277 goto error;
1278 if (exact_i)
1279 break;
1280 isl_map_free(*res);
1281 *res = NULL;
1284 isl_set_free(C);
1286 return *res != NULL;
1287 error:
1288 isl_set_free(C);
1289 return isl_bool_error;
1292 /* Try and compute the transitive closure of "map" as
1294 * map^+ = map_i^+ \cup
1295 * \bigcup_j ((map_i^+ \cup Id_C)^+ \circ map_j \circ (map_i^+ \cup Id_C))^+
1297 * with C either the simple hull of the domain and range of the entire
1298 * map or the simple hull of domain and range of map_i.
1300 static __isl_give isl_map *incremental_closure(__isl_take isl_space *space,
1301 __isl_keep isl_map *map, int *exact, int project)
1303 int i;
1304 isl_set **dom = NULL;
1305 isl_set **ran = NULL;
1306 int *left = NULL;
1307 int *right = NULL;
1308 isl_set *C;
1309 unsigned d;
1310 isl_map *res = NULL;
1312 if (!project)
1313 return construct_projected_component(space, map, exact,
1314 project);
1316 if (!map)
1317 goto error;
1318 if (map->n <= 1)
1319 return construct_projected_component(space, map, exact,
1320 project);
1322 d = isl_map_dim(map, isl_dim_in);
1324 dom = isl_calloc_array(map->ctx, isl_set *, map->n);
1325 ran = isl_calloc_array(map->ctx, isl_set *, map->n);
1326 left = isl_calloc_array(map->ctx, int, map->n);
1327 right = isl_calloc_array(map->ctx, int, map->n);
1328 if (!ran || !dom || !left || !right)
1329 goto error;
1331 if (incremental_on_entire_domain(space, map, dom, ran, left, right,
1332 &res) < 0)
1333 goto error;
1335 for (i = 0; !res && i < map->n; ++i) {
1336 isl_map *qc;
1337 int exact_i, comp;
1338 isl_bool spurious;
1339 if (!dom[i])
1340 dom[i] = isl_set_from_basic_set(
1341 isl_basic_map_domain(
1342 isl_basic_map_copy(map->p[i])));
1343 if (!dom[i])
1344 goto error;
1345 if (!ran[i])
1346 ran[i] = isl_set_from_basic_set(
1347 isl_basic_map_range(
1348 isl_basic_map_copy(map->p[i])));
1349 if (!ran[i])
1350 goto error;
1351 C = isl_set_union(isl_set_copy(dom[i]),
1352 isl_set_copy(ran[i]));
1353 C = isl_set_from_basic_set(isl_set_simple_hull(C));
1354 if (!C)
1355 goto error;
1356 if (C->n != 1) {
1357 isl_set_free(C);
1358 continue;
1360 comp = composability(C, i, dom, ran, left, right, map);
1361 if (!comp || comp < 0) {
1362 isl_set_free(C);
1363 if (comp < 0)
1364 goto error;
1365 continue;
1367 qc = q_closure(isl_space_copy(space), C, map->p[i], &exact_i);
1368 if (!qc)
1369 goto error;
1370 if (!exact_i) {
1371 isl_map_free(qc);
1372 continue;
1374 spurious = has_spurious_elements(qc, dom[i], ran[i]);
1375 if (spurious) {
1376 isl_map_free(qc);
1377 if (spurious < 0)
1378 goto error;
1379 continue;
1381 qc = isl_map_project_out(qc, isl_dim_in, d, 1);
1382 qc = isl_map_project_out(qc, isl_dim_out, d, 1);
1383 qc = isl_map_compute_divs(qc);
1384 qc = compose(map, i, qc, (comp & LEFT) ? left : NULL,
1385 (comp & RIGHT) ? right : NULL);
1386 if (!qc)
1387 goto error;
1388 if (qc->n >= map->n) {
1389 isl_map_free(qc);
1390 continue;
1392 res = compute_incremental(isl_space_copy(space), map, i, qc,
1393 (comp & LEFT) ? left : NULL,
1394 (comp & RIGHT) ? right : NULL, &exact_i);
1395 if (!res)
1396 goto error;
1397 if (exact_i)
1398 break;
1399 isl_map_free(res);
1400 res = NULL;
1403 for (i = 0; i < map->n; ++i) {
1404 isl_set_free(dom[i]);
1405 isl_set_free(ran[i]);
1407 free(dom);
1408 free(ran);
1409 free(left);
1410 free(right);
1412 if (res) {
1413 isl_space_free(space);
1414 return res;
1417 return construct_projected_component(space, map, exact, project);
1418 error:
1419 if (dom)
1420 for (i = 0; i < map->n; ++i)
1421 isl_set_free(dom[i]);
1422 free(dom);
1423 if (ran)
1424 for (i = 0; i < map->n; ++i)
1425 isl_set_free(ran[i]);
1426 free(ran);
1427 free(left);
1428 free(right);
1429 isl_space_free(space);
1430 return NULL;
1433 /* Given an array of sets "set", add "dom" at position "pos"
1434 * and search for elements at earlier positions that overlap with "dom".
1435 * If any can be found, then merge all of them, together with "dom", into
1436 * a single set and assign the union to the first in the array,
1437 * which becomes the new group leader for all groups involved in the merge.
1438 * During the search, we only consider group leaders, i.e., those with
1439 * group[i] = i, as the other sets have already been combined
1440 * with one of the group leaders.
1442 static int merge(isl_set **set, int *group, __isl_take isl_set *dom, int pos)
1444 int i;
1446 group[pos] = pos;
1447 set[pos] = isl_set_copy(dom);
1449 for (i = pos - 1; i >= 0; --i) {
1450 isl_bool o;
1452 if (group[i] != i)
1453 continue;
1455 o = isl_set_overlaps(set[i], dom);
1456 if (o < 0)
1457 goto error;
1458 if (!o)
1459 continue;
1461 set[i] = isl_set_union(set[i], set[group[pos]]);
1462 set[group[pos]] = NULL;
1463 if (!set[i])
1464 goto error;
1465 group[group[pos]] = i;
1466 group[pos] = i;
1469 isl_set_free(dom);
1470 return 0;
1471 error:
1472 isl_set_free(dom);
1473 return -1;
1476 /* Construct a map [x] -> [x+1], with parameters prescribed by "space".
1478 static __isl_give isl_map *increment(__isl_take isl_space *space)
1480 int k;
1481 isl_basic_map *bmap;
1483 space = isl_space_set_from_params(space);
1484 space = isl_space_add_dims(space, isl_dim_set, 1);
1485 space = isl_space_map_from_set(space);
1486 bmap = isl_basic_map_alloc_space(space, 0, 1, 0);
1487 k = isl_basic_map_alloc_equality(bmap);
1488 if (k < 0)
1489 goto error;
1490 isl_seq_clr(bmap->eq[k], 1 + isl_basic_map_total_dim(bmap));
1491 isl_int_set_si(bmap->eq[k][0], 1);
1492 isl_int_set_si(bmap->eq[k][isl_basic_map_offset(bmap, isl_dim_in)], 1);
1493 isl_int_set_si(bmap->eq[k][isl_basic_map_offset(bmap, isl_dim_out)], -1);
1494 return isl_map_from_basic_map(bmap);
1495 error:
1496 isl_basic_map_free(bmap);
1497 return NULL;
1500 /* Replace each entry in the n by n grid of maps by the cross product
1501 * with the relation { [i] -> [i + 1] }.
1503 static isl_stat add_length(__isl_keep isl_map *map, isl_map ***grid, int n)
1505 int i, j;
1506 isl_space *space;
1507 isl_map *step;
1509 space = isl_space_params(isl_map_get_space(map));
1510 step = increment(space);
1512 if (!step)
1513 return isl_stat_error;
1515 for (i = 0; i < n; ++i)
1516 for (j = 0; j < n; ++j)
1517 grid[i][j] = isl_map_product(grid[i][j],
1518 isl_map_copy(step));
1520 isl_map_free(step);
1522 return isl_stat_ok;
1525 /* The core of the Floyd-Warshall algorithm.
1526 * Updates the given n x x matrix of relations in place.
1528 * The algorithm iterates over all vertices. In each step, the whole
1529 * matrix is updated to include all paths that go to the current vertex,
1530 * possibly stay there a while (including passing through earlier vertices)
1531 * and then come back. At the start of each iteration, the diagonal
1532 * element corresponding to the current vertex is replaced by its
1533 * transitive closure to account for all indirect paths that stay
1534 * in the current vertex.
1536 static void floyd_warshall_iterate(isl_map ***grid, int n, int *exact)
1538 int r, p, q;
1540 for (r = 0; r < n; ++r) {
1541 int r_exact;
1542 grid[r][r] = isl_map_transitive_closure(grid[r][r],
1543 (exact && *exact) ? &r_exact : NULL);
1544 if (exact && *exact && !r_exact)
1545 *exact = 0;
1547 for (p = 0; p < n; ++p)
1548 for (q = 0; q < n; ++q) {
1549 isl_map *loop;
1550 if (p == r && q == r)
1551 continue;
1552 loop = isl_map_apply_range(
1553 isl_map_copy(grid[p][r]),
1554 isl_map_copy(grid[r][q]));
1555 grid[p][q] = isl_map_union(grid[p][q], loop);
1556 loop = isl_map_apply_range(
1557 isl_map_copy(grid[p][r]),
1558 isl_map_apply_range(
1559 isl_map_copy(grid[r][r]),
1560 isl_map_copy(grid[r][q])));
1561 grid[p][q] = isl_map_union(grid[p][q], loop);
1562 grid[p][q] = isl_map_coalesce(grid[p][q]);
1567 /* Given a partition of the domains and ranges of the basic maps in "map",
1568 * apply the Floyd-Warshall algorithm with the elements in the partition
1569 * as vertices.
1571 * In particular, there are "n" elements in the partition and "group" is
1572 * an array of length 2 * map->n with entries in [0,n-1].
1574 * We first construct a matrix of relations based on the partition information,
1575 * apply Floyd-Warshall on this matrix of relations and then take the
1576 * union of all entries in the matrix as the final result.
1578 * If we are actually computing the power instead of the transitive closure,
1579 * i.e., when "project" is not set, then the result should have the
1580 * path lengths encoded as the difference between an extra pair of
1581 * coordinates. We therefore apply the nested transitive closures
1582 * to relations that include these lengths. In particular, we replace
1583 * the input relation by the cross product with the unit length relation
1584 * { [i] -> [i + 1] }.
1586 static __isl_give isl_map *floyd_warshall_with_groups(
1587 __isl_take isl_space *space, __isl_keep isl_map *map,
1588 int *exact, int project, int *group, int n)
1590 int i, j, k;
1591 isl_map ***grid = NULL;
1592 isl_map *app;
1594 if (!map)
1595 goto error;
1597 if (n == 1) {
1598 free(group);
1599 return incremental_closure(space, map, exact, project);
1602 grid = isl_calloc_array(map->ctx, isl_map **, n);
1603 if (!grid)
1604 goto error;
1605 for (i = 0; i < n; ++i) {
1606 grid[i] = isl_calloc_array(map->ctx, isl_map *, n);
1607 if (!grid[i])
1608 goto error;
1609 for (j = 0; j < n; ++j)
1610 grid[i][j] = isl_map_empty(isl_map_get_space(map));
1613 for (k = 0; k < map->n; ++k) {
1614 i = group[2 * k];
1615 j = group[2 * k + 1];
1616 grid[i][j] = isl_map_union(grid[i][j],
1617 isl_map_from_basic_map(
1618 isl_basic_map_copy(map->p[k])));
1621 if (!project && add_length(map, grid, n) < 0)
1622 goto error;
1624 floyd_warshall_iterate(grid, n, exact);
1626 app = isl_map_empty(isl_map_get_space(grid[0][0]));
1628 for (i = 0; i < n; ++i) {
1629 for (j = 0; j < n; ++j)
1630 app = isl_map_union(app, grid[i][j]);
1631 free(grid[i]);
1633 free(grid);
1635 free(group);
1636 isl_space_free(space);
1638 return app;
1639 error:
1640 if (grid)
1641 for (i = 0; i < n; ++i) {
1642 if (!grid[i])
1643 continue;
1644 for (j = 0; j < n; ++j)
1645 isl_map_free(grid[i][j]);
1646 free(grid[i]);
1648 free(grid);
1649 free(group);
1650 isl_space_free(space);
1651 return NULL;
1654 /* Partition the domains and ranges of the n basic relations in list
1655 * into disjoint cells.
1657 * To find the partition, we simply consider all of the domains
1658 * and ranges in turn and combine those that overlap.
1659 * "set" contains the partition elements and "group" indicates
1660 * to which partition element a given domain or range belongs.
1661 * The domain of basic map i corresponds to element 2 * i in these arrays,
1662 * while the domain corresponds to element 2 * i + 1.
1663 * During the construction group[k] is either equal to k,
1664 * in which case set[k] contains the union of all the domains and
1665 * ranges in the corresponding group, or is equal to some l < k,
1666 * with l another domain or range in the same group.
1668 static int *setup_groups(isl_ctx *ctx, __isl_keep isl_basic_map **list, int n,
1669 isl_set ***set, int *n_group)
1671 int i;
1672 int *group = NULL;
1673 int g;
1675 *set = isl_calloc_array(ctx, isl_set *, 2 * n);
1676 group = isl_alloc_array(ctx, int, 2 * n);
1678 if (!*set || !group)
1679 goto error;
1681 for (i = 0; i < n; ++i) {
1682 isl_set *dom;
1683 dom = isl_set_from_basic_set(isl_basic_map_domain(
1684 isl_basic_map_copy(list[i])));
1685 if (merge(*set, group, dom, 2 * i) < 0)
1686 goto error;
1687 dom = isl_set_from_basic_set(isl_basic_map_range(
1688 isl_basic_map_copy(list[i])));
1689 if (merge(*set, group, dom, 2 * i + 1) < 0)
1690 goto error;
1693 g = 0;
1694 for (i = 0; i < 2 * n; ++i)
1695 if (group[i] == i) {
1696 if (g != i) {
1697 (*set)[g] = (*set)[i];
1698 (*set)[i] = NULL;
1700 group[i] = g++;
1701 } else
1702 group[i] = group[group[i]];
1704 *n_group = g;
1706 return group;
1707 error:
1708 if (*set) {
1709 for (i = 0; i < 2 * n; ++i)
1710 isl_set_free((*set)[i]);
1711 free(*set);
1712 *set = NULL;
1714 free(group);
1715 return NULL;
1718 /* Check if the domains and ranges of the basic maps in "map" can
1719 * be partitioned, and if so, apply Floyd-Warshall on the elements
1720 * of the partition. Note that we also apply this algorithm
1721 * if we want to compute the power, i.e., when "project" is not set.
1722 * However, the results are unlikely to be exact since the recursive
1723 * calls inside the Floyd-Warshall algorithm typically result in
1724 * non-linear path lengths quite quickly.
1726 static __isl_give isl_map *floyd_warshall(__isl_take isl_space *space,
1727 __isl_keep isl_map *map, int *exact, int project)
1729 int i;
1730 isl_set **set = NULL;
1731 int *group = NULL;
1732 int n;
1734 if (!map)
1735 goto error;
1736 if (map->n <= 1)
1737 return incremental_closure(space, map, exact, project);
1739 group = setup_groups(map->ctx, map->p, map->n, &set, &n);
1740 if (!group)
1741 goto error;
1743 for (i = 0; i < 2 * map->n; ++i)
1744 isl_set_free(set[i]);
1746 free(set);
1748 return floyd_warshall_with_groups(space, map, exact, project, group, n);
1749 error:
1750 isl_space_free(space);
1751 return NULL;
1754 /* Structure for representing the nodes of the graph of which
1755 * strongly connected components are being computed.
1757 * list contains the actual nodes
1758 * check_closed is set if we may have used the fact that
1759 * a pair of basic maps can be interchanged
1761 struct isl_tc_follows_data {
1762 isl_basic_map **list;
1763 int check_closed;
1766 /* Check whether in the computation of the transitive closure
1767 * "list[i]" (R_1) should follow (or be part of the same component as)
1768 * "list[j]" (R_2).
1770 * That is check whether
1772 * R_1 \circ R_2
1774 * is a subset of
1776 * R_2 \circ R_1
1778 * If so, then there is no reason for R_1 to immediately follow R_2
1779 * in any path.
1781 * *check_closed is set if the subset relation holds while
1782 * R_1 \circ R_2 is not empty.
1784 static isl_bool basic_map_follows(int i, int j, void *user)
1786 struct isl_tc_follows_data *data = user;
1787 struct isl_map *map12 = NULL;
1788 struct isl_map *map21 = NULL;
1789 isl_bool subset;
1791 if (!isl_space_tuple_is_equal(data->list[i]->dim, isl_dim_in,
1792 data->list[j]->dim, isl_dim_out))
1793 return isl_bool_false;
1795 map21 = isl_map_from_basic_map(
1796 isl_basic_map_apply_range(
1797 isl_basic_map_copy(data->list[j]),
1798 isl_basic_map_copy(data->list[i])));
1799 subset = isl_map_is_empty(map21);
1800 if (subset < 0)
1801 goto error;
1802 if (subset) {
1803 isl_map_free(map21);
1804 return isl_bool_false;
1807 if (!isl_space_tuple_is_equal(data->list[i]->dim, isl_dim_in,
1808 data->list[i]->dim, isl_dim_out) ||
1809 !isl_space_tuple_is_equal(data->list[j]->dim, isl_dim_in,
1810 data->list[j]->dim, isl_dim_out)) {
1811 isl_map_free(map21);
1812 return isl_bool_true;
1815 map12 = isl_map_from_basic_map(
1816 isl_basic_map_apply_range(
1817 isl_basic_map_copy(data->list[i]),
1818 isl_basic_map_copy(data->list[j])));
1820 subset = isl_map_is_subset(map21, map12);
1822 isl_map_free(map12);
1823 isl_map_free(map21);
1825 if (subset)
1826 data->check_closed = 1;
1828 return isl_bool_not(subset);
1829 error:
1830 isl_map_free(map21);
1831 return isl_bool_error;
1834 /* Given a union of basic maps R = \cup_i R_i \subseteq D \times D
1835 * and a dimension specification (Z^{n+1} -> Z^{n+1}),
1836 * construct a map that is an overapproximation of the map
1837 * that takes an element from the dom R \times Z to an
1838 * element from ran R \times Z, such that the first n coordinates of the
1839 * difference between them is a sum of differences between images
1840 * and pre-images in one of the R_i and such that the last coordinate
1841 * is equal to the number of steps taken.
1842 * If "project" is set, then these final coordinates are not included,
1843 * i.e., a relation of type Z^n -> Z^n is returned.
1844 * That is, let
1846 * \Delta_i = { y - x | (x, y) in R_i }
1848 * then the constructed map is an overapproximation of
1850 * { (x) -> (x + d) | \exists k_i >= 0, \delta_i \in \Delta_i :
1851 * d = (\sum_i k_i \delta_i, \sum_i k_i) and
1852 * x in dom R and x + d in ran R }
1854 * or
1856 * { (x) -> (x + d) | \exists k_i >= 0, \delta_i \in \Delta_i :
1857 * d = (\sum_i k_i \delta_i) and
1858 * x in dom R and x + d in ran R }
1860 * if "project" is set.
1862 * We first split the map into strongly connected components, perform
1863 * the above on each component and then join the results in the correct
1864 * order, at each join also taking in the union of both arguments
1865 * to allow for paths that do not go through one of the two arguments.
1867 static __isl_give isl_map *construct_power_components(
1868 __isl_take isl_space *space, __isl_keep isl_map *map, int *exact,
1869 int project)
1871 int i, n, c;
1872 struct isl_map *path = NULL;
1873 struct isl_tc_follows_data data;
1874 struct isl_tarjan_graph *g = NULL;
1875 int *orig_exact;
1876 int local_exact;
1878 if (!map)
1879 goto error;
1880 if (map->n <= 1)
1881 return floyd_warshall(space, map, exact, project);
1883 data.list = map->p;
1884 data.check_closed = 0;
1885 g = isl_tarjan_graph_init(map->ctx, map->n, &basic_map_follows, &data);
1886 if (!g)
1887 goto error;
1889 orig_exact = exact;
1890 if (data.check_closed && !exact)
1891 exact = &local_exact;
1893 c = 0;
1894 i = 0;
1895 n = map->n;
1896 if (project)
1897 path = isl_map_empty(isl_map_get_space(map));
1898 else
1899 path = isl_map_empty(isl_space_copy(space));
1900 path = anonymize(path);
1901 while (n) {
1902 struct isl_map *comp;
1903 isl_map *path_comp, *path_comb;
1904 comp = isl_map_alloc_space(isl_map_get_space(map), n, 0);
1905 while (g->order[i] != -1) {
1906 comp = isl_map_add_basic_map(comp,
1907 isl_basic_map_copy(map->p[g->order[i]]));
1908 --n;
1909 ++i;
1911 path_comp = floyd_warshall(isl_space_copy(space),
1912 comp, exact, project);
1913 path_comp = anonymize(path_comp);
1914 path_comb = isl_map_apply_range(isl_map_copy(path),
1915 isl_map_copy(path_comp));
1916 path = isl_map_union(path, path_comp);
1917 path = isl_map_union(path, path_comb);
1918 isl_map_free(comp);
1919 ++i;
1920 ++c;
1923 if (c > 1 && data.check_closed && !*exact) {
1924 int closed;
1926 closed = isl_map_is_transitively_closed(path);
1927 if (closed < 0)
1928 goto error;
1929 if (!closed) {
1930 isl_tarjan_graph_free(g);
1931 isl_map_free(path);
1932 return floyd_warshall(space, map, orig_exact, project);
1936 isl_tarjan_graph_free(g);
1937 isl_space_free(space);
1939 return path;
1940 error:
1941 isl_tarjan_graph_free(g);
1942 isl_space_free(space);
1943 isl_map_free(path);
1944 return NULL;
1947 /* Given a union of basic maps R = \cup_i R_i \subseteq D \times D,
1948 * construct a map that is an overapproximation of the map
1949 * that takes an element from the space D to another
1950 * element from the same space, such that the difference between
1951 * them is a strictly positive sum of differences between images
1952 * and pre-images in one of the R_i.
1953 * The number of differences in the sum is equated to parameter "param".
1954 * That is, let
1956 * \Delta_i = { y - x | (x, y) in R_i }
1958 * then the constructed map is an overapproximation of
1960 * { (x) -> (x + d) | \exists k_i >= 0, \delta_i \in \Delta_i :
1961 * d = \sum_i k_i \delta_i and k = \sum_i k_i > 0 }
1962 * or
1964 * { (x) -> (x + d) | \exists k_i >= 0, \delta_i \in \Delta_i :
1965 * d = \sum_i k_i \delta_i and \sum_i k_i > 0 }
1967 * if "project" is set.
1969 * If "project" is not set, then
1970 * we construct an extended mapping with an extra coordinate
1971 * that indicates the number of steps taken. In particular,
1972 * the difference in the last coordinate is equal to the number
1973 * of steps taken to move from a domain element to the corresponding
1974 * image element(s).
1976 static __isl_give isl_map *construct_power(__isl_keep isl_map *map,
1977 int *exact, int project)
1979 struct isl_map *app = NULL;
1980 isl_space *space = NULL;
1982 if (!map)
1983 return NULL;
1985 space = isl_map_get_space(map);
1987 space = isl_space_add_dims(space, isl_dim_in, 1);
1988 space = isl_space_add_dims(space, isl_dim_out, 1);
1990 app = construct_power_components(isl_space_copy(space), map,
1991 exact, project);
1993 isl_space_free(space);
1995 return app;
1998 /* Compute the positive powers of "map", or an overapproximation.
1999 * If the result is exact, then *exact is set to 1.
2001 * If project is set, then we are actually interested in the transitive
2002 * closure, so we can use a more relaxed exactness check.
2003 * The lengths of the paths are also projected out instead of being
2004 * encoded as the difference between an extra pair of final coordinates.
2006 static __isl_give isl_map *map_power(__isl_take isl_map *map,
2007 int *exact, int project)
2009 struct isl_map *app = NULL;
2011 if (exact)
2012 *exact = 1;
2014 if (isl_map_check_equal_tuples(map) < 0)
2015 return isl_map_free(map);
2017 app = construct_power(map, exact, project);
2019 isl_map_free(map);
2020 return app;
2023 /* Compute the positive powers of "map", or an overapproximation.
2024 * The result maps the exponent to a nested copy of the corresponding power.
2025 * If the result is exact, then *exact is set to 1.
2026 * map_power constructs an extended relation with the path lengths
2027 * encoded as the difference between the final coordinates.
2028 * In the final step, this difference is equated to an extra parameter
2029 * and made positive. The extra coordinates are subsequently projected out
2030 * and the parameter is turned into the domain of the result.
2032 __isl_give isl_map *isl_map_power(__isl_take isl_map *map, int *exact)
2034 isl_space *target_space;
2035 isl_space *space;
2036 isl_map *diff;
2037 unsigned d;
2038 unsigned param;
2040 if (!map)
2041 return NULL;
2043 d = isl_map_dim(map, isl_dim_in);
2044 param = isl_map_dim(map, isl_dim_param);
2046 map = isl_map_compute_divs(map);
2047 map = isl_map_coalesce(map);
2049 if (isl_map_plain_is_empty(map)) {
2050 map = isl_map_from_range(isl_map_wrap(map));
2051 map = isl_map_add_dims(map, isl_dim_in, 1);
2052 map = isl_map_set_dim_name(map, isl_dim_in, 0, "k");
2053 return map;
2056 target_space = isl_map_get_space(map);
2057 target_space = isl_space_from_range(isl_space_wrap(target_space));
2058 target_space = isl_space_add_dims(target_space, isl_dim_in, 1);
2059 target_space = isl_space_set_dim_name(target_space, isl_dim_in, 0, "k");
2061 map = map_power(map, exact, 0);
2063 map = isl_map_add_dims(map, isl_dim_param, 1);
2064 space = isl_map_get_space(map);
2065 diff = equate_parameter_to_length(space, param);
2066 map = isl_map_intersect(map, diff);
2067 map = isl_map_project_out(map, isl_dim_in, d, 1);
2068 map = isl_map_project_out(map, isl_dim_out, d, 1);
2069 map = isl_map_from_range(isl_map_wrap(map));
2070 map = isl_map_move_dims(map, isl_dim_in, 0, isl_dim_param, param, 1);
2072 map = isl_map_reset_space(map, target_space);
2074 return map;
2077 /* Compute a relation that maps each element in the range of the input
2078 * relation to the lengths of all paths composed of edges in the input
2079 * relation that end up in the given range element.
2080 * The result may be an overapproximation, in which case *exact is set to 0.
2081 * The resulting relation is very similar to the power relation.
2082 * The difference are that the domain has been projected out, the
2083 * range has become the domain and the exponent is the range instead
2084 * of a parameter.
2086 __isl_give isl_map *isl_map_reaching_path_lengths(__isl_take isl_map *map,
2087 int *exact)
2089 isl_space *space;
2090 isl_map *diff;
2091 unsigned d;
2092 unsigned param;
2094 if (!map)
2095 return NULL;
2097 d = isl_map_dim(map, isl_dim_in);
2098 param = isl_map_dim(map, isl_dim_param);
2100 map = isl_map_compute_divs(map);
2101 map = isl_map_coalesce(map);
2103 if (isl_map_plain_is_empty(map)) {
2104 if (exact)
2105 *exact = 1;
2106 map = isl_map_project_out(map, isl_dim_out, 0, d);
2107 map = isl_map_add_dims(map, isl_dim_out, 1);
2108 return map;
2111 map = map_power(map, exact, 0);
2113 map = isl_map_add_dims(map, isl_dim_param, 1);
2114 space = isl_map_get_space(map);
2115 diff = equate_parameter_to_length(space, param);
2116 map = isl_map_intersect(map, diff);
2117 map = isl_map_project_out(map, isl_dim_in, 0, d + 1);
2118 map = isl_map_project_out(map, isl_dim_out, d, 1);
2119 map = isl_map_reverse(map);
2120 map = isl_map_move_dims(map, isl_dim_out, 0, isl_dim_param, param, 1);
2122 return map;
2125 /* Given a map, compute the smallest superset of this map that is of the form
2127 * { i -> j : L <= j - i <= U and exists a_p: j_p - i_p = M_p a_p }
2129 * (where p ranges over the (non-parametric) dimensions),
2130 * compute the transitive closure of this map, i.e.,
2132 * { i -> j : exists k > 0:
2133 * k L <= j - i <= k U and exists a: j_p - i_p = M_p a_p }
2135 * and intersect domain and range of this transitive closure with
2136 * the given domain and range.
2138 * If with_id is set, then try to include as much of the identity mapping
2139 * as possible, by computing
2141 * { i -> j : exists k >= 0:
2142 * k L <= j - i <= k U and exists a: j_p - i_p = M_p a_p }
2144 * instead (i.e., allow k = 0).
2146 * In practice, we compute the difference set
2148 * delta = { j - i | i -> j in map },
2150 * look for stride constraint on the individual dimensions and compute
2151 * (constant) lower and upper bounds for each individual dimension,
2152 * adding a constraint for each bound not equal to infinity.
2154 static __isl_give isl_map *box_closure_on_domain(__isl_take isl_map *map,
2155 __isl_take isl_set *dom, __isl_take isl_set *ran, int with_id)
2157 int i;
2158 int k;
2159 unsigned d;
2160 unsigned nparam;
2161 unsigned total;
2162 isl_space *dim;
2163 isl_set *delta;
2164 isl_map *app = NULL;
2165 isl_basic_set *aff = NULL;
2166 isl_basic_map *bmap = NULL;
2167 isl_vec *obj = NULL;
2168 isl_int opt;
2170 isl_int_init(opt);
2172 delta = isl_map_deltas(isl_map_copy(map));
2174 aff = isl_set_affine_hull(isl_set_copy(delta));
2175 if (!aff)
2176 goto error;
2177 dim = isl_map_get_space(map);
2178 d = isl_space_dim(dim, isl_dim_in);
2179 nparam = isl_space_dim(dim, isl_dim_param);
2180 total = isl_space_dim(dim, isl_dim_all);
2181 bmap = isl_basic_map_alloc_space(dim,
2182 aff->n_div + 1, aff->n_div, 2 * d + 1);
2183 for (i = 0; i < aff->n_div + 1; ++i) {
2184 k = isl_basic_map_alloc_div(bmap);
2185 if (k < 0)
2186 goto error;
2187 isl_int_set_si(bmap->div[k][0], 0);
2189 for (i = 0; i < aff->n_eq; ++i) {
2190 if (!isl_basic_set_eq_is_stride(aff, i))
2191 continue;
2192 k = isl_basic_map_alloc_equality(bmap);
2193 if (k < 0)
2194 goto error;
2195 isl_seq_clr(bmap->eq[k], 1 + nparam);
2196 isl_seq_cpy(bmap->eq[k] + 1 + nparam + d,
2197 aff->eq[i] + 1 + nparam, d);
2198 isl_seq_neg(bmap->eq[k] + 1 + nparam,
2199 aff->eq[i] + 1 + nparam, d);
2200 isl_seq_cpy(bmap->eq[k] + 1 + nparam + 2 * d,
2201 aff->eq[i] + 1 + nparam + d, aff->n_div);
2202 isl_int_set_si(bmap->eq[k][1 + total + aff->n_div], 0);
2204 obj = isl_vec_alloc(map->ctx, 1 + nparam + d);
2205 if (!obj)
2206 goto error;
2207 isl_seq_clr(obj->el, 1 + nparam + d);
2208 for (i = 0; i < d; ++ i) {
2209 enum isl_lp_result res;
2211 isl_int_set_si(obj->el[1 + nparam + i], 1);
2213 res = isl_set_solve_lp(delta, 0, obj->el, map->ctx->one, &opt,
2214 NULL, NULL);
2215 if (res == isl_lp_error)
2216 goto error;
2217 if (res == isl_lp_ok) {
2218 k = isl_basic_map_alloc_inequality(bmap);
2219 if (k < 0)
2220 goto error;
2221 isl_seq_clr(bmap->ineq[k],
2222 1 + nparam + 2 * d + bmap->n_div);
2223 isl_int_set_si(bmap->ineq[k][1 + nparam + i], -1);
2224 isl_int_set_si(bmap->ineq[k][1 + nparam + d + i], 1);
2225 isl_int_neg(bmap->ineq[k][1 + nparam + 2 * d + aff->n_div], opt);
2228 res = isl_set_solve_lp(delta, 1, obj->el, map->ctx->one, &opt,
2229 NULL, NULL);
2230 if (res == isl_lp_error)
2231 goto error;
2232 if (res == isl_lp_ok) {
2233 k = isl_basic_map_alloc_inequality(bmap);
2234 if (k < 0)
2235 goto error;
2236 isl_seq_clr(bmap->ineq[k],
2237 1 + nparam + 2 * d + bmap->n_div);
2238 isl_int_set_si(bmap->ineq[k][1 + nparam + i], 1);
2239 isl_int_set_si(bmap->ineq[k][1 + nparam + d + i], -1);
2240 isl_int_set(bmap->ineq[k][1 + nparam + 2 * d + aff->n_div], opt);
2243 isl_int_set_si(obj->el[1 + nparam + i], 0);
2245 k = isl_basic_map_alloc_inequality(bmap);
2246 if (k < 0)
2247 goto error;
2248 isl_seq_clr(bmap->ineq[k],
2249 1 + nparam + 2 * d + bmap->n_div);
2250 if (!with_id)
2251 isl_int_set_si(bmap->ineq[k][0], -1);
2252 isl_int_set_si(bmap->ineq[k][1 + nparam + 2 * d + aff->n_div], 1);
2254 app = isl_map_from_domain_and_range(dom, ran);
2256 isl_vec_free(obj);
2257 isl_basic_set_free(aff);
2258 isl_map_free(map);
2259 bmap = isl_basic_map_finalize(bmap);
2260 isl_set_free(delta);
2261 isl_int_clear(opt);
2263 map = isl_map_from_basic_map(bmap);
2264 map = isl_map_intersect(map, app);
2266 return map;
2267 error:
2268 isl_vec_free(obj);
2269 isl_basic_map_free(bmap);
2270 isl_basic_set_free(aff);
2271 isl_set_free(dom);
2272 isl_set_free(ran);
2273 isl_map_free(map);
2274 isl_set_free(delta);
2275 isl_int_clear(opt);
2276 return NULL;
2279 /* Given a map, compute the smallest superset of this map that is of the form
2281 * { i -> j : L <= j - i <= U and exists a_p: j_p - i_p = M_p a_p }
2283 * (where p ranges over the (non-parametric) dimensions),
2284 * compute the transitive closure of this map, i.e.,
2286 * { i -> j : exists k > 0:
2287 * k L <= j - i <= k U and exists a: j_p - i_p = M_p a_p }
2289 * and intersect domain and range of this transitive closure with
2290 * domain and range of the original map.
2292 static __isl_give isl_map *box_closure(__isl_take isl_map *map)
2294 isl_set *domain;
2295 isl_set *range;
2297 domain = isl_map_domain(isl_map_copy(map));
2298 domain = isl_set_coalesce(domain);
2299 range = isl_map_range(isl_map_copy(map));
2300 range = isl_set_coalesce(range);
2302 return box_closure_on_domain(map, domain, range, 0);
2305 /* Given a map, compute the smallest superset of this map that is of the form
2307 * { i -> j : L <= j - i <= U and exists a_p: j_p - i_p = M_p a_p }
2309 * (where p ranges over the (non-parametric) dimensions),
2310 * compute the transitive and partially reflexive closure of this map, i.e.,
2312 * { i -> j : exists k >= 0:
2313 * k L <= j - i <= k U and exists a: j_p - i_p = M_p a_p }
2315 * and intersect domain and range of this transitive closure with
2316 * the given domain.
2318 static __isl_give isl_map *box_closure_with_identity(__isl_take isl_map *map,
2319 __isl_take isl_set *dom)
2321 return box_closure_on_domain(map, dom, isl_set_copy(dom), 1);
2324 /* Check whether app is the transitive closure of map.
2325 * In particular, check that app is acyclic and, if so,
2326 * check that
2328 * app \subset (map \cup (map \circ app))
2330 static isl_bool check_exactness_omega(__isl_keep isl_map *map,
2331 __isl_keep isl_map *app)
2333 isl_set *delta;
2334 int i;
2335 isl_bool is_empty, is_exact;
2336 unsigned d;
2337 isl_map *test;
2339 delta = isl_map_deltas(isl_map_copy(app));
2340 d = isl_set_dim(delta, isl_dim_set);
2341 for (i = 0; i < d; ++i)
2342 delta = isl_set_fix_si(delta, isl_dim_set, i, 0);
2343 is_empty = isl_set_is_empty(delta);
2344 isl_set_free(delta);
2345 if (is_empty < 0 || !is_empty)
2346 return is_empty;
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 isl_bool is_exact = check_exactness_omega(map, app);
2459 if (is_exact < 0)
2460 app = isl_map_free(app);
2461 else
2462 *exact = is_exact;
2465 isl_map_free(map);
2466 return app;
2469 /* Compute an overapproximation of the transitive closure of "map"
2470 * using a variation of the algorithm from
2471 * "Transitive Closure of Infinite Graphs and its Applications"
2472 * by Kelly et al.
2474 * We first check whether we can can split of any basic map M_i and
2475 * compute
2477 * (\cup_j M_j)^+
2479 * as
2481 * M_i \cup (\cup_{j \ne i} M_i^* \circ M_j \circ M_i^*)^+
2483 * using a recursive call on the remaining map.
2485 * If not, we simply call box_closure on the whole map.
2487 static __isl_give isl_map *transitive_closure_omega(__isl_take isl_map *map,
2488 int *exact)
2490 int i, j;
2491 isl_bool exact_i;
2492 isl_map *app;
2494 if (!map)
2495 return NULL;
2496 if (map->n == 1)
2497 return box_closure_with_check(map, exact);
2499 for (i = 0; i < map->n; ++i) {
2500 int ok;
2501 isl_map *qc, *tc;
2502 ok = can_be_split_off(map, i, &tc, &qc);
2503 if (ok < 0)
2504 goto error;
2505 if (!ok)
2506 continue;
2508 app = isl_map_alloc_space(isl_map_get_space(map), map->n - 1, 0);
2510 for (j = 0; j < map->n; ++j) {
2511 if (j == i)
2512 continue;
2513 app = isl_map_add_basic_map(app,
2514 isl_basic_map_copy(map->p[j]));
2517 app = isl_map_apply_range(isl_map_copy(qc), app);
2518 app = isl_map_apply_range(app, qc);
2520 app = isl_map_union(tc, transitive_closure_omega(app, NULL));
2521 exact_i = check_exactness_omega(map, app);
2522 if (exact_i == isl_bool_true) {
2523 if (exact)
2524 *exact = exact_i;
2525 isl_map_free(map);
2526 return app;
2528 isl_map_free(app);
2529 if (exact_i < 0)
2530 goto error;
2533 return box_closure_with_check(map, exact);
2534 error:
2535 isl_map_free(map);
2536 return NULL;
2539 /* Compute the transitive closure of "map", or an overapproximation.
2540 * If the result is exact, then *exact is set to 1.
2541 * Simply use map_power to compute the powers of map, but tell
2542 * it to project out the lengths of the paths instead of equating
2543 * the length to a parameter.
2545 __isl_give isl_map *isl_map_transitive_closure(__isl_take isl_map *map,
2546 int *exact)
2548 isl_space *target_dim;
2549 int closed;
2551 if (!map)
2552 goto error;
2554 if (map->ctx->opt->closure == ISL_CLOSURE_BOX)
2555 return transitive_closure_omega(map, exact);
2557 map = isl_map_compute_divs(map);
2558 map = isl_map_coalesce(map);
2559 closed = isl_map_is_transitively_closed(map);
2560 if (closed < 0)
2561 goto error;
2562 if (closed) {
2563 if (exact)
2564 *exact = 1;
2565 return map;
2568 target_dim = isl_map_get_space(map);
2569 map = map_power(map, exact, 1);
2570 map = isl_map_reset_space(map, target_dim);
2572 return map;
2573 error:
2574 isl_map_free(map);
2575 return NULL;
2578 static isl_stat inc_count(__isl_take isl_map *map, void *user)
2580 int *n = user;
2582 *n += map->n;
2584 isl_map_free(map);
2586 return isl_stat_ok;
2589 static isl_stat collect_basic_map(__isl_take isl_map *map, void *user)
2591 int i;
2592 isl_basic_map ***next = user;
2594 for (i = 0; i < map->n; ++i) {
2595 **next = isl_basic_map_copy(map->p[i]);
2596 if (!**next)
2597 goto error;
2598 (*next)++;
2601 isl_map_free(map);
2602 return isl_stat_ok;
2603 error:
2604 isl_map_free(map);
2605 return isl_stat_error;
2608 /* Perform Floyd-Warshall on the given list of basic relations.
2609 * The basic relations may live in different dimensions,
2610 * but basic relations that get assigned to the diagonal of the
2611 * grid have domains and ranges of the same dimension and so
2612 * the standard algorithm can be used because the nested transitive
2613 * closures are only applied to diagonal elements and because all
2614 * compositions are peformed on relations with compatible domains and ranges.
2616 static __isl_give isl_union_map *union_floyd_warshall_on_list(isl_ctx *ctx,
2617 __isl_keep isl_basic_map **list, int n, int *exact)
2619 int i, j, k;
2620 int n_group;
2621 int *group = NULL;
2622 isl_set **set = NULL;
2623 isl_map ***grid = NULL;
2624 isl_union_map *app;
2626 group = setup_groups(ctx, list, n, &set, &n_group);
2627 if (!group)
2628 goto error;
2630 grid = isl_calloc_array(ctx, isl_map **, n_group);
2631 if (!grid)
2632 goto error;
2633 for (i = 0; i < n_group; ++i) {
2634 grid[i] = isl_calloc_array(ctx, isl_map *, n_group);
2635 if (!grid[i])
2636 goto error;
2637 for (j = 0; j < n_group; ++j) {
2638 isl_space *space1, *space2, *space;
2639 space1 = isl_space_reverse(isl_set_get_space(set[i]));
2640 space2 = isl_set_get_space(set[j]);
2641 space = isl_space_join(space1, space2);
2642 grid[i][j] = isl_map_empty(space);
2646 for (k = 0; k < n; ++k) {
2647 i = group[2 * k];
2648 j = group[2 * k + 1];
2649 grid[i][j] = isl_map_union(grid[i][j],
2650 isl_map_from_basic_map(
2651 isl_basic_map_copy(list[k])));
2654 floyd_warshall_iterate(grid, n_group, exact);
2656 app = isl_union_map_empty(isl_map_get_space(grid[0][0]));
2658 for (i = 0; i < n_group; ++i) {
2659 for (j = 0; j < n_group; ++j)
2660 app = isl_union_map_add_map(app, grid[i][j]);
2661 free(grid[i]);
2663 free(grid);
2665 for (i = 0; i < 2 * n; ++i)
2666 isl_set_free(set[i]);
2667 free(set);
2669 free(group);
2670 return app;
2671 error:
2672 if (grid)
2673 for (i = 0; i < n_group; ++i) {
2674 if (!grid[i])
2675 continue;
2676 for (j = 0; j < n_group; ++j)
2677 isl_map_free(grid[i][j]);
2678 free(grid[i]);
2680 free(grid);
2681 if (set) {
2682 for (i = 0; i < 2 * n; ++i)
2683 isl_set_free(set[i]);
2684 free(set);
2686 free(group);
2687 return NULL;
2690 /* Perform Floyd-Warshall on the given union relation.
2691 * The implementation is very similar to that for non-unions.
2692 * The main difference is that it is applied unconditionally.
2693 * We first extract a list of basic maps from the union map
2694 * and then perform the algorithm on this list.
2696 static __isl_give isl_union_map *union_floyd_warshall(
2697 __isl_take isl_union_map *umap, int *exact)
2699 int i, n;
2700 isl_ctx *ctx;
2701 isl_basic_map **list = NULL;
2702 isl_basic_map **next;
2703 isl_union_map *res;
2705 n = 0;
2706 if (isl_union_map_foreach_map(umap, inc_count, &n) < 0)
2707 goto error;
2709 ctx = isl_union_map_get_ctx(umap);
2710 list = isl_calloc_array(ctx, isl_basic_map *, n);
2711 if (!list)
2712 goto error;
2714 next = list;
2715 if (isl_union_map_foreach_map(umap, collect_basic_map, &next) < 0)
2716 goto error;
2718 res = union_floyd_warshall_on_list(ctx, list, n, exact);
2720 if (list) {
2721 for (i = 0; i < n; ++i)
2722 isl_basic_map_free(list[i]);
2723 free(list);
2726 isl_union_map_free(umap);
2727 return res;
2728 error:
2729 if (list) {
2730 for (i = 0; i < n; ++i)
2731 isl_basic_map_free(list[i]);
2732 free(list);
2734 isl_union_map_free(umap);
2735 return NULL;
2738 /* Decompose the give union relation into strongly connected components.
2739 * The implementation is essentially the same as that of
2740 * construct_power_components with the major difference that all
2741 * operations are performed on union maps.
2743 static __isl_give isl_union_map *union_components(
2744 __isl_take isl_union_map *umap, int *exact)
2746 int i;
2747 int n;
2748 isl_ctx *ctx;
2749 isl_basic_map **list = NULL;
2750 isl_basic_map **next;
2751 isl_union_map *path = NULL;
2752 struct isl_tc_follows_data data;
2753 struct isl_tarjan_graph *g = NULL;
2754 int c, l;
2755 int recheck = 0;
2757 n = 0;
2758 if (isl_union_map_foreach_map(umap, inc_count, &n) < 0)
2759 goto error;
2761 if (n == 0)
2762 return umap;
2763 if (n <= 1)
2764 return union_floyd_warshall(umap, exact);
2766 ctx = isl_union_map_get_ctx(umap);
2767 list = isl_calloc_array(ctx, isl_basic_map *, n);
2768 if (!list)
2769 goto error;
2771 next = list;
2772 if (isl_union_map_foreach_map(umap, collect_basic_map, &next) < 0)
2773 goto error;
2775 data.list = list;
2776 data.check_closed = 0;
2777 g = isl_tarjan_graph_init(ctx, n, &basic_map_follows, &data);
2778 if (!g)
2779 goto error;
2781 c = 0;
2782 i = 0;
2783 l = n;
2784 path = isl_union_map_empty(isl_union_map_get_space(umap));
2785 while (l) {
2786 isl_union_map *comp;
2787 isl_union_map *path_comp, *path_comb;
2788 comp = isl_union_map_empty(isl_union_map_get_space(umap));
2789 while (g->order[i] != -1) {
2790 comp = isl_union_map_add_map(comp,
2791 isl_map_from_basic_map(
2792 isl_basic_map_copy(list[g->order[i]])));
2793 --l;
2794 ++i;
2796 path_comp = union_floyd_warshall(comp, exact);
2797 path_comb = isl_union_map_apply_range(isl_union_map_copy(path),
2798 isl_union_map_copy(path_comp));
2799 path = isl_union_map_union(path, path_comp);
2800 path = isl_union_map_union(path, path_comb);
2801 ++i;
2802 ++c;
2805 if (c > 1 && data.check_closed && !*exact) {
2806 int closed;
2808 closed = isl_union_map_is_transitively_closed(path);
2809 if (closed < 0)
2810 goto error;
2811 recheck = !closed;
2814 isl_tarjan_graph_free(g);
2816 for (i = 0; i < n; ++i)
2817 isl_basic_map_free(list[i]);
2818 free(list);
2820 if (recheck) {
2821 isl_union_map_free(path);
2822 return union_floyd_warshall(umap, exact);
2825 isl_union_map_free(umap);
2827 return path;
2828 error:
2829 isl_tarjan_graph_free(g);
2830 if (list) {
2831 for (i = 0; i < n; ++i)
2832 isl_basic_map_free(list[i]);
2833 free(list);
2835 isl_union_map_free(umap);
2836 isl_union_map_free(path);
2837 return NULL;
2840 /* Compute the transitive closure of "umap", or an overapproximation.
2841 * If the result is exact, then *exact is set to 1.
2843 __isl_give isl_union_map *isl_union_map_transitive_closure(
2844 __isl_take isl_union_map *umap, int *exact)
2846 int closed;
2848 if (!umap)
2849 return NULL;
2851 if (exact)
2852 *exact = 1;
2854 umap = isl_union_map_compute_divs(umap);
2855 umap = isl_union_map_coalesce(umap);
2856 closed = isl_union_map_is_transitively_closed(umap);
2857 if (closed < 0)
2858 goto error;
2859 if (closed)
2860 return umap;
2861 umap = union_components(umap, exact);
2862 return umap;
2863 error:
2864 isl_union_map_free(umap);
2865 return NULL;
2868 struct isl_union_power {
2869 isl_union_map *pow;
2870 int *exact;
2873 static isl_stat power(__isl_take isl_map *map, void *user)
2875 struct isl_union_power *up = user;
2877 map = isl_map_power(map, up->exact);
2878 up->pow = isl_union_map_from_map(map);
2880 return isl_stat_error;
2883 /* Construct a map [[x]->[y]] -> [y-x], with parameters prescribed by "dim".
2885 static __isl_give isl_union_map *deltas_map(__isl_take isl_space *dim)
2887 isl_basic_map *bmap;
2889 dim = isl_space_add_dims(dim, isl_dim_in, 1);
2890 dim = isl_space_add_dims(dim, isl_dim_out, 1);
2891 bmap = isl_basic_map_universe(dim);
2892 bmap = isl_basic_map_deltas_map(bmap);
2894 return isl_union_map_from_map(isl_map_from_basic_map(bmap));
2897 /* Compute the positive powers of "map", or an overapproximation.
2898 * The result maps the exponent to a nested copy of the corresponding power.
2899 * If the result is exact, then *exact is set to 1.
2901 __isl_give isl_union_map *isl_union_map_power(__isl_take isl_union_map *umap,
2902 int *exact)
2904 int n;
2905 isl_union_map *inc;
2906 isl_union_map *dm;
2908 if (!umap)
2909 return NULL;
2910 n = isl_union_map_n_map(umap);
2911 if (n == 0)
2912 return umap;
2913 if (n == 1) {
2914 struct isl_union_power up = { NULL, exact };
2915 isl_union_map_foreach_map(umap, &power, &up);
2916 isl_union_map_free(umap);
2917 return up.pow;
2919 inc = isl_union_map_from_map(increment(isl_union_map_get_space(umap)));
2920 umap = isl_union_map_product(inc, umap);
2921 umap = isl_union_map_transitive_closure(umap, exact);
2922 umap = isl_union_map_zip(umap);
2923 dm = deltas_map(isl_union_map_get_space(umap));
2924 umap = isl_union_map_apply_domain(umap, dm);
2926 return umap;
2929 #undef TYPE
2930 #define TYPE isl_map
2931 #include "isl_power_templ.c"
2933 #undef TYPE
2934 #define TYPE isl_union_map
2935 #include "isl_power_templ.c"