2 * Copyright 2010 INRIA Saclay
4 * Use of this software is governed by the GNU LGPLv2.1 license
6 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
7 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
12 #include "isl_map_private.h"
16 int isl_map_is_transitively_closed(__isl_keep isl_map *map)
21 map2 = isl_map_apply_range(isl_map_copy(map), isl_map_copy(map));
22 closed = isl_map_is_subset(map2, map);
28 /* Given a map that represents a path with the length of the path
29 * encoded as the difference between the last output coordindate
30 * and the last input coordinate, set this length to either
31 * exactly "length" (if "exactly" is set) or at least "length"
32 * (if "exactly" is not set).
34 static __isl_give isl_map *set_path_length(__isl_take isl_map *map,
35 int exactly, int length)
38 struct isl_basic_map *bmap;
47 dim = isl_map_get_dim(map);
48 d = isl_dim_size(dim, isl_dim_in);
49 nparam = isl_dim_size(dim, isl_dim_param);
50 bmap = isl_basic_map_alloc_dim(dim, 0, 1, 1);
52 k = isl_basic_map_alloc_equality(bmap);
55 k = isl_basic_map_alloc_inequality(bmap);
60 isl_seq_clr(c, 1 + isl_basic_map_total_dim(bmap));
61 isl_int_set_si(c[0], -length);
62 isl_int_set_si(c[1 + nparam + d - 1], -1);
63 isl_int_set_si(c[1 + nparam + d + d - 1], 1);
65 bmap = isl_basic_map_finalize(bmap);
66 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
70 isl_basic_map_free(bmap);
75 /* Check whether the overapproximation of the power of "map" is exactly
76 * the power of "map". Let R be "map" and A_k the overapproximation.
77 * The approximation is exact if
80 * A_k = A_{k-1} \circ R k >= 2
82 * Since A_k is known to be an overapproximation, we only need to check
85 * A_k \subset A_{k-1} \circ R k >= 2
87 * In practice, "app" has an extra input and output coordinate
88 * to encode the length of the path. So, we first need to add
89 * this coordinate to "map" and set the length of the path to
92 static int check_power_exactness(__isl_take isl_map *map,
93 __isl_take isl_map *app)
99 map = isl_map_add(map, isl_dim_in, 1);
100 map = isl_map_add(map, isl_dim_out, 1);
101 map = set_path_length(map, 1, 1);
103 app_1 = set_path_length(isl_map_copy(app), 1, 1);
105 exact = isl_map_is_subset(app_1, map);
108 if (!exact || exact < 0) {
114 app_1 = set_path_length(isl_map_copy(app), 0, 1);
115 app_2 = set_path_length(app, 0, 2);
116 app_1 = isl_map_apply_range(map, app_1);
118 exact = isl_map_is_subset(app_2, app_1);
126 /* Check whether the overapproximation of the power of "map" is exactly
127 * the power of "map", possibly after projecting out the power (if "project"
130 * If "project" is set and if "steps" can only result in acyclic paths,
133 * A = R \cup (A \circ R)
135 * where A is the overapproximation with the power projected out, i.e.,
136 * an overapproximation of the transitive closure.
137 * More specifically, since A is known to be an overapproximation, we check
139 * A \subset R \cup (A \circ R)
141 * Otherwise, we check if the power is exact.
143 * Note that "app" has an extra input and output coordinate to encode
144 * the length of the part. If we are only interested in the transitive
145 * closure, then we can simply project out these coordinates first.
147 static int check_exactness(__isl_take isl_map *map, __isl_take isl_map *app,
155 return check_power_exactness(map, app);
157 d = isl_map_dim(map, isl_dim_in);
158 app = set_path_length(app, 0, 1);
159 app = isl_map_project_out(app, isl_dim_in, d, 1);
160 app = isl_map_project_out(app, isl_dim_out, d, 1);
162 app = isl_map_reset_dim(app, isl_map_get_dim(map));
164 test = isl_map_apply_range(isl_map_copy(map), isl_map_copy(app));
165 test = isl_map_union(test, isl_map_copy(map));
167 exact = isl_map_is_subset(app, test);
178 * The transitive closure implementation is based on the paper
179 * "Computing the Transitive Closure of a Union of Affine Integer
180 * Tuple Relations" by Anna Beletska, Denis Barthou, Wlodzimierz Bielecki and
184 /* Given a set of n offsets v_i (the rows of "steps"), construct a relation
185 * of the given dimension specification (Z^{n+1} -> Z^{n+1})
186 * that maps an element x to any element that can be reached
187 * by taking a non-negative number of steps along any of
188 * the extended offsets v'_i = [v_i 1].
191 * { [x] -> [y] : exists k_i >= 0, y = x + \sum_i k_i v'_i }
193 * For any element in this relation, the number of steps taken
194 * is equal to the difference in the final coordinates.
196 static __isl_give isl_map *path_along_steps(__isl_take isl_dim *dim,
197 __isl_keep isl_mat *steps)
200 struct isl_basic_map *path = NULL;
208 d = isl_dim_size(dim, isl_dim_in);
210 nparam = isl_dim_size(dim, isl_dim_param);
212 path = isl_basic_map_alloc_dim(isl_dim_copy(dim), n, d, n);
214 for (i = 0; i < n; ++i) {
215 k = isl_basic_map_alloc_div(path);
218 isl_assert(steps->ctx, i == k, goto error);
219 isl_int_set_si(path->div[k][0], 0);
222 for (i = 0; i < d; ++i) {
223 k = isl_basic_map_alloc_equality(path);
226 isl_seq_clr(path->eq[k], 1 + isl_basic_map_total_dim(path));
227 isl_int_set_si(path->eq[k][1 + nparam + i], 1);
228 isl_int_set_si(path->eq[k][1 + nparam + d + i], -1);
230 for (j = 0; j < n; ++j)
231 isl_int_set_si(path->eq[k][1 + nparam + 2 * d + j], 1);
233 for (j = 0; j < n; ++j)
234 isl_int_set(path->eq[k][1 + nparam + 2 * d + j],
238 for (i = 0; i < n; ++i) {
239 k = isl_basic_map_alloc_inequality(path);
242 isl_seq_clr(path->ineq[k], 1 + isl_basic_map_total_dim(path));
243 isl_int_set_si(path->ineq[k][1 + nparam + 2 * d + i], 1);
248 path = isl_basic_map_simplify(path);
249 path = isl_basic_map_finalize(path);
250 return isl_map_from_basic_map(path);
253 isl_basic_map_free(path);
262 /* Check whether the parametric constant term of constraint c is never
263 * positive in "bset".
265 static int parametric_constant_never_positive(__isl_keep isl_basic_set *bset,
266 isl_int *c, int *div_purity)
275 n_div = isl_basic_set_dim(bset, isl_dim_div);
276 d = isl_basic_set_dim(bset, isl_dim_set);
277 nparam = isl_basic_set_dim(bset, isl_dim_param);
279 bset = isl_basic_set_copy(bset);
280 bset = isl_basic_set_cow(bset);
281 bset = isl_basic_set_extend_constraints(bset, 0, 1);
282 k = isl_basic_set_alloc_inequality(bset);
285 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
286 isl_seq_cpy(bset->ineq[k], c, 1 + nparam);
287 for (i = 0; i < n_div; ++i) {
288 if (div_purity[i] != PURE_PARAM)
290 isl_int_set(bset->ineq[k][1 + nparam + d + i],
291 c[1 + nparam + d + i]);
293 isl_int_sub_ui(bset->ineq[k][0], bset->ineq[k][0], 1);
294 empty = isl_basic_set_is_empty(bset);
295 isl_basic_set_free(bset);
299 isl_basic_set_free(bset);
303 /* Return PURE_PARAM if only the coefficients of the parameters are non-zero.
304 * Return PURE_VAR if only the coefficients of the set variables are non-zero.
305 * Return MIXED if only the coefficients of the parameters and the set
306 * variables are non-zero and if moreover the parametric constant
307 * can never attain positive values.
308 * Return IMPURE otherwise.
310 static int purity(__isl_keep isl_basic_set *bset, isl_int *c, int *div_purity,
320 n_div = isl_basic_set_dim(bset, isl_dim_div);
321 d = isl_basic_set_dim(bset, isl_dim_set);
322 nparam = isl_basic_set_dim(bset, isl_dim_param);
324 for (i = 0; i < n_div; ++i) {
325 if (isl_int_is_zero(c[1 + nparam + d + i]))
327 switch (div_purity[i]) {
328 case PURE_PARAM: p = 1; break;
329 case PURE_VAR: v = 1; break;
330 default: return IMPURE;
333 if (!p && isl_seq_first_non_zero(c + 1, nparam) == -1)
335 if (!v && isl_seq_first_non_zero(c + 1 + nparam, d) == -1)
338 empty = parametric_constant_never_positive(bset, c, div_purity);
339 if (eq && empty >= 0 && !empty) {
340 isl_seq_neg(c, c, 1 + nparam + d + n_div);
341 empty = parametric_constant_never_positive(bset, c, div_purity);
344 return empty < 0 ? -1 : empty ? MIXED : IMPURE;
347 /* Return an array of integers indicating the type of each div in bset.
348 * If the div is (recursively) defined in terms of only the parameters,
349 * then the type is PURE_PARAM.
350 * If the div is (recursively) defined in terms of only the set variables,
351 * then the type is PURE_VAR.
352 * Otherwise, the type is IMPURE.
354 static __isl_give int *get_div_purity(__isl_keep isl_basic_set *bset)
365 n_div = isl_basic_set_dim(bset, isl_dim_div);
366 d = isl_basic_set_dim(bset, isl_dim_set);
367 nparam = isl_basic_set_dim(bset, isl_dim_param);
369 div_purity = isl_alloc_array(bset->ctx, int, n_div);
373 for (i = 0; i < bset->n_div; ++i) {
375 if (isl_int_is_zero(bset->div[i][0])) {
376 div_purity[i] = IMPURE;
379 if (isl_seq_first_non_zero(bset->div[i] + 2, nparam) != -1)
381 if (isl_seq_first_non_zero(bset->div[i] + 2 + nparam, d) != -1)
383 for (j = 0; j < i; ++j) {
384 if (isl_int_is_zero(bset->div[i][2 + nparam + d + j]))
386 switch (div_purity[j]) {
387 case PURE_PARAM: p = 1; break;
388 case PURE_VAR: v = 1; break;
389 default: p = v = 1; break;
392 div_purity[i] = v ? p ? IMPURE : PURE_VAR : PURE_PARAM;
398 /* Given a path with the as yet unconstrained length at position "pos",
399 * check if setting the length to zero results in only the identity
402 int empty_path_is_identity(__isl_keep isl_basic_map *path, unsigned pos)
404 isl_basic_map *test = NULL;
405 isl_basic_map *id = NULL;
409 test = isl_basic_map_copy(path);
410 test = isl_basic_map_extend_constraints(test, 1, 0);
411 k = isl_basic_map_alloc_equality(test);
414 isl_seq_clr(test->eq[k], 1 + isl_basic_map_total_dim(test));
415 isl_int_set_si(test->eq[k][pos], 1);
416 id = isl_basic_map_identity(isl_dim_domain(isl_basic_map_get_dim(path)));
417 is_id = isl_basic_map_is_equal(test, id);
418 isl_basic_map_free(test);
419 isl_basic_map_free(id);
422 isl_basic_map_free(test);
426 __isl_give isl_basic_map *add_delta_constraints(__isl_take isl_basic_map *path,
427 __isl_keep isl_basic_set *delta, unsigned off, unsigned nparam,
428 unsigned d, int *div_purity, int eq)
431 int n = eq ? delta->n_eq : delta->n_ineq;
432 isl_int **delta_c = eq ? delta->eq : delta->ineq;
435 n_div = isl_basic_set_dim(delta, isl_dim_div);
437 for (i = 0; i < n; ++i) {
439 int p = purity(delta, delta_c[i], div_purity, eq);
444 if (eq && p != MIXED) {
445 k = isl_basic_map_alloc_equality(path);
446 path_c = path->eq[k];
448 k = isl_basic_map_alloc_inequality(path);
449 path_c = path->ineq[k];
453 isl_seq_clr(path_c, 1 + isl_basic_map_total_dim(path));
455 isl_seq_cpy(path_c + off,
456 delta_c[i] + 1 + nparam, d);
457 isl_int_set(path_c[off + d], delta_c[i][0]);
458 } else if (p == PURE_PARAM) {
459 isl_seq_cpy(path_c, delta_c[i], 1 + nparam);
461 isl_seq_cpy(path_c + off,
462 delta_c[i] + 1 + nparam, d);
463 isl_seq_cpy(path_c, delta_c[i], 1 + nparam);
465 isl_seq_cpy(path_c + off - n_div,
466 delta_c[i] + 1 + nparam + d, n_div);
471 isl_basic_map_free(path);
475 /* Given a set of offsets "delta", construct a relation of the
476 * given dimension specification (Z^{n+1} -> Z^{n+1}) that
477 * is an overapproximation of the relations that
478 * maps an element x to any element that can be reached
479 * by taking a non-negative number of steps along any of
480 * the elements in "delta".
481 * That is, construct an approximation of
483 * { [x] -> [y] : exists f \in \delta, k \in Z :
484 * y = x + k [f, 1] and k >= 0 }
486 * For any element in this relation, the number of steps taken
487 * is equal to the difference in the final coordinates.
489 * In particular, let delta be defined as
491 * \delta = [p] -> { [x] : A x + a >= and B p + b >= 0 and
492 * C x + C'p + c >= 0 and
493 * D x + D'p + d >= 0 }
495 * where the constraints C x + C'p + c >= 0 are such that the parametric
496 * constant term of each constraint j, "C_j x + C'_j p + c_j",
497 * can never attain positive values, then the relation is constructed as
499 * { [x] -> [y] : exists [f, k] \in Z^{n+1} : y = x + f and
500 * A f + k a >= 0 and B p + b >= 0 and
501 * C f + C'p + c >= 0 and k >= 1 }
502 * union { [x] -> [x] }
504 * If the zero-length paths happen to correspond exactly to the identity
505 * mapping, then we return
507 * { [x] -> [y] : exists [f, k] \in Z^{n+1} : y = x + f and
508 * A f + k a >= 0 and B p + b >= 0 and
509 * C f + C'p + c >= 0 and k >= 0 }
513 * Existentially quantified variables in \delta are handled by
514 * classifying them as independent of the parameters, purely
515 * parameter dependent and others. Constraints containing
516 * any of the other existentially quantified variables are removed.
517 * This is safe, but leads to an additional overapproximation.
519 static __isl_give isl_map *path_along_delta(__isl_take isl_dim *dim,
520 __isl_take isl_basic_set *delta)
522 isl_basic_map *path = NULL;
529 int *div_purity = NULL;
533 n_div = isl_basic_set_dim(delta, isl_dim_div);
534 d = isl_basic_set_dim(delta, isl_dim_set);
535 nparam = isl_basic_set_dim(delta, isl_dim_param);
536 path = isl_basic_map_alloc_dim(isl_dim_copy(dim), n_div + d + 1,
537 d + 1 + delta->n_eq, delta->n_eq + delta->n_ineq + 1);
538 off = 1 + nparam + 2 * (d + 1) + n_div;
540 for (i = 0; i < n_div + d + 1; ++i) {
541 k = isl_basic_map_alloc_div(path);
544 isl_int_set_si(path->div[k][0], 0);
547 for (i = 0; i < d + 1; ++i) {
548 k = isl_basic_map_alloc_equality(path);
551 isl_seq_clr(path->eq[k], 1 + isl_basic_map_total_dim(path));
552 isl_int_set_si(path->eq[k][1 + nparam + i], 1);
553 isl_int_set_si(path->eq[k][1 + nparam + d + 1 + i], -1);
554 isl_int_set_si(path->eq[k][off + i], 1);
557 div_purity = get_div_purity(delta);
561 path = add_delta_constraints(path, delta, off, nparam, d, div_purity, 1);
562 path = add_delta_constraints(path, delta, off, nparam, d, div_purity, 0);
564 is_id = empty_path_is_identity(path, off + d);
568 k = isl_basic_map_alloc_inequality(path);
571 isl_seq_clr(path->ineq[k], 1 + isl_basic_map_total_dim(path));
573 isl_int_set_si(path->ineq[k][0], -1);
574 isl_int_set_si(path->ineq[k][off + d], 1);
577 isl_basic_set_free(delta);
578 path = isl_basic_map_finalize(path);
581 return isl_map_from_basic_map(path);
583 return isl_basic_map_union(path,
584 isl_basic_map_identity(isl_dim_domain(dim)));
588 isl_basic_set_free(delta);
589 isl_basic_map_free(path);
593 /* Given a dimension specification Z^{n+1} -> Z^{n+1} and a parameter "param",
594 * construct a map that equates the parameter to the difference
595 * in the final coordinates and imposes that this difference is positive.
598 * { [x,x_s] -> [y,y_s] : k = y_s - x_s > 0 }
600 static __isl_give isl_map *equate_parameter_to_length(__isl_take isl_dim *dim,
603 struct isl_basic_map *bmap;
608 d = isl_dim_size(dim, isl_dim_in);
609 nparam = isl_dim_size(dim, isl_dim_param);
610 bmap = isl_basic_map_alloc_dim(dim, 0, 1, 1);
611 k = isl_basic_map_alloc_equality(bmap);
614 isl_seq_clr(bmap->eq[k], 1 + isl_basic_map_total_dim(bmap));
615 isl_int_set_si(bmap->eq[k][1 + param], -1);
616 isl_int_set_si(bmap->eq[k][1 + nparam + d - 1], -1);
617 isl_int_set_si(bmap->eq[k][1 + nparam + d + d - 1], 1);
619 k = isl_basic_map_alloc_inequality(bmap);
622 isl_seq_clr(bmap->ineq[k], 1 + isl_basic_map_total_dim(bmap));
623 isl_int_set_si(bmap->ineq[k][1 + param], 1);
624 isl_int_set_si(bmap->ineq[k][0], -1);
626 bmap = isl_basic_map_finalize(bmap);
627 return isl_map_from_basic_map(bmap);
629 isl_basic_map_free(bmap);
633 /* Check whether "path" is acyclic, where the last coordinates of domain
634 * and range of path encode the number of steps taken.
635 * That is, check whether
637 * { d | d = y - x and (x,y) in path }
639 * does not contain any element with positive last coordinate (positive length)
640 * and zero remaining coordinates (cycle).
642 static int is_acyclic(__isl_take isl_map *path)
647 struct isl_set *delta;
649 delta = isl_map_deltas(path);
650 dim = isl_set_dim(delta, isl_dim_set);
651 for (i = 0; i < dim; ++i) {
653 delta = isl_set_lower_bound_si(delta, isl_dim_set, i, 1);
655 delta = isl_set_fix_si(delta, isl_dim_set, i, 0);
658 acyclic = isl_set_is_empty(delta);
664 /* Given a union of basic maps R = \cup_i R_i \subseteq D \times D
665 * and a dimension specification (Z^{n+1} -> Z^{n+1}),
666 * construct a map that is an overapproximation of the map
667 * that takes an element from the space D \times Z to another
668 * element from the same space, such that the first n coordinates of the
669 * difference between them is a sum of differences between images
670 * and pre-images in one of the R_i and such that the last coordinate
671 * is equal to the number of steps taken.
674 * \Delta_i = { y - x | (x, y) in R_i }
676 * then the constructed map is an overapproximation of
678 * { (x) -> (x + d) | \exists k_i >= 0, \delta_i \in \Delta_i :
679 * d = (\sum_i k_i \delta_i, \sum_i k_i) }
681 * The elements of the singleton \Delta_i's are collected as the
682 * rows of the steps matrix. For all these \Delta_i's together,
683 * a single path is constructed.
684 * For each of the other \Delta_i's, we compute an overapproximation
685 * of the paths along elements of \Delta_i.
686 * Since each of these paths performs an addition, composition is
687 * symmetric and we can simply compose all resulting paths in any order.
689 static __isl_give isl_map *construct_extended_path(__isl_take isl_dim *dim,
690 __isl_keep isl_map *map, int *project)
692 struct isl_mat *steps = NULL;
693 struct isl_map *path = NULL;
697 d = isl_map_dim(map, isl_dim_in);
699 path = isl_map_identity(isl_dim_domain(isl_dim_copy(dim)));
701 steps = isl_mat_alloc(map->ctx, map->n, d);
706 for (i = 0; i < map->n; ++i) {
707 struct isl_basic_set *delta;
709 delta = isl_basic_map_deltas(isl_basic_map_copy(map->p[i]));
711 for (j = 0; j < d; ++j) {
714 fixed = isl_basic_set_fast_dim_is_fixed(delta, j,
717 isl_basic_set_free(delta);
726 path = isl_map_apply_range(path,
727 path_along_delta(isl_dim_copy(dim), delta));
728 path = isl_map_coalesce(path);
730 isl_basic_set_free(delta);
737 path = isl_map_apply_range(path,
738 path_along_steps(isl_dim_copy(dim), steps));
741 if (project && *project) {
742 *project = is_acyclic(isl_map_copy(path));
757 static int isl_set_overlaps(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
762 i = isl_set_intersect(isl_set_copy(set1), isl_set_copy(set2));
763 no_overlap = isl_set_is_empty(i);
766 return no_overlap < 0 ? -1 : !no_overlap;
769 /* Given a union of basic maps R = \cup_i R_i \subseteq D \times D
770 * and a dimension specification (Z^{n+1} -> Z^{n+1}),
771 * construct a map that is an overapproximation of the map
772 * that takes an element from the dom R \times Z to an
773 * element from ran R \times Z, such that the first n coordinates of the
774 * difference between them is a sum of differences between images
775 * and pre-images in one of the R_i and such that the last coordinate
776 * is equal to the number of steps taken.
779 * \Delta_i = { y - x | (x, y) in R_i }
781 * then the constructed map is an overapproximation of
783 * { (x) -> (x + d) | \exists k_i >= 0, \delta_i \in \Delta_i :
784 * d = (\sum_i k_i \delta_i, \sum_i k_i) and
785 * x in dom R and x + d in ran R and
788 static __isl_give isl_map *construct_component(__isl_take isl_dim *dim,
789 __isl_keep isl_map *map, int *exact, int project)
791 struct isl_set *domain = NULL;
792 struct isl_set *range = NULL;
793 struct isl_map *app = NULL;
794 struct isl_map *path = NULL;
796 domain = isl_map_domain(isl_map_copy(map));
797 domain = isl_set_coalesce(domain);
798 range = isl_map_range(isl_map_copy(map));
799 range = isl_set_coalesce(range);
800 if (!isl_set_overlaps(domain, range)) {
801 isl_set_free(domain);
805 map = isl_map_copy(map);
806 map = isl_map_add(map, isl_dim_in, 1);
807 map = isl_map_add(map, isl_dim_out, 1);
808 map = set_path_length(map, 1, 1);
811 app = isl_map_from_domain_and_range(domain, range);
812 app = isl_map_add(app, isl_dim_in, 1);
813 app = isl_map_add(app, isl_dim_out, 1);
815 path = construct_extended_path(isl_dim_copy(dim), map,
816 exact && *exact ? &project : NULL);
817 app = isl_map_intersect(app, path);
819 if (exact && *exact &&
820 (*exact = check_exactness(isl_map_copy(map), isl_map_copy(app),
825 app = set_path_length(app, 0, 1);
833 /* Call construct_component and, if "project" is set, project out
834 * the final coordinates.
836 static __isl_give isl_map *construct_projected_component(
837 __isl_take isl_dim *dim,
838 __isl_keep isl_map *map, int *exact, int project)
845 d = isl_dim_size(dim, isl_dim_in);
847 app = construct_component(dim, map, exact, project);
849 app = isl_map_project_out(app, isl_dim_in, d - 1, 1);
850 app = isl_map_project_out(app, isl_dim_out, d - 1, 1);
855 /* Compute an extended version, i.e., with path lengths, of
856 * an overapproximation of the transitive closure of "bmap"
857 * with path lengths greater than or equal to zero and with
858 * domain and range equal to "dom".
860 static __isl_give isl_map *q_closure(__isl_take isl_dim *dim,
861 __isl_take isl_set *dom, __isl_keep isl_basic_map *bmap, int *exact)
868 dom = isl_set_add(dom, isl_dim_set, 1);
869 app = isl_map_from_domain_and_range(dom, isl_set_copy(dom));
870 map = isl_map_from_basic_map(isl_basic_map_copy(bmap));
871 path = construct_extended_path(dim, map, &project);
872 app = isl_map_intersect(app, path);
874 if ((*exact = check_exactness(map, isl_map_copy(app), project)) < 0)
883 /* Check whether qc has any elements of length at least one
884 * with domain and/or range outside of dom and ran.
886 static int has_spurious_elements(__isl_keep isl_map *qc,
887 __isl_keep isl_set *dom, __isl_keep isl_set *ran)
893 if (!qc || !dom || !ran)
896 d = isl_map_dim(qc, isl_dim_in);
898 qc = isl_map_copy(qc);
899 qc = set_path_length(qc, 0, 1);
900 qc = isl_map_project_out(qc, isl_dim_in, d - 1, 1);
901 qc = isl_map_project_out(qc, isl_dim_out, d - 1, 1);
903 s = isl_map_domain(isl_map_copy(qc));
904 subset = isl_set_is_subset(s, dom);
913 s = isl_map_range(qc);
914 subset = isl_set_is_subset(s, ran);
917 return subset < 0 ? -1 : !subset;
926 /* For each basic map in "map", except i, check whether it combines
927 * with the transitive closure that is reflexive on C combines
928 * to the left and to the right.
932 * dom map_j \subseteq C
934 * then right[j] is set to 1. Otherwise, if
936 * ran map_i \cap dom map_j = \emptyset
938 * then right[j] is set to 0. Otherwise, composing to the right
941 * Similar, for composing to the left, we have if
943 * ran map_j \subseteq C
945 * then left[j] is set to 1. Otherwise, if
947 * dom map_i \cap ran map_j = \emptyset
949 * then left[j] is set to 0. Otherwise, composing to the left
952 * The return value is or'd with LEFT if composing to the left
953 * is possible and with RIGHT if composing to the right is possible.
955 static int composability(__isl_keep isl_set *C, int i,
956 isl_set **dom, isl_set **ran, int *left, int *right,
957 __isl_keep isl_map *map)
963 for (j = 0; j < map->n && ok; ++j) {
964 int overlaps, subset;
970 dom[j] = isl_set_from_basic_set(
971 isl_basic_map_domain(
972 isl_basic_map_copy(map->p[j])));
975 overlaps = isl_set_overlaps(ran[i], dom[j]);
981 subset = isl_set_is_subset(dom[j], C);
993 ran[j] = isl_set_from_basic_set(
995 isl_basic_map_copy(map->p[j])));
998 overlaps = isl_set_overlaps(dom[i], ran[j]);
1004 subset = isl_set_is_subset(ran[j], C);
1018 /* Return a map that is a union of the basic maps in "map", except i,
1019 * composed to left and right with qc based on the entries of "left"
1022 static __isl_give isl_map *compose(__isl_keep isl_map *map, int i,
1023 __isl_take isl_map *qc, int *left, int *right)
1028 comp = isl_map_empty(isl_map_get_dim(map));
1029 for (j = 0; j < map->n; ++j) {
1035 map_j = isl_map_from_basic_map(isl_basic_map_copy(map->p[j]));
1036 if (left && left[j])
1037 map_j = isl_map_apply_range(map_j, isl_map_copy(qc));
1038 if (right && right[j])
1039 map_j = isl_map_apply_range(isl_map_copy(qc), map_j);
1040 comp = isl_map_union(comp, map_j);
1043 comp = isl_map_compute_divs(comp);
1044 comp = isl_map_coalesce(comp);
1051 /* Compute the transitive closure of "map" incrementally by
1058 * map_i^+ \cup ((id \cup map_i^) \circ qc^+)
1062 * map_i^+ \cup (qc^+ \circ (id \cup map_i^))
1064 * depending on whether left or right are NULL.
1066 static __isl_give isl_map *compute_incremental(
1067 __isl_take isl_dim *dim, __isl_keep isl_map *map,
1068 int i, __isl_take isl_map *qc, int *left, int *right, int *exact)
1072 isl_map *rtc = NULL;
1076 isl_assert(map->ctx, left || right, goto error);
1078 map_i = isl_map_from_basic_map(isl_basic_map_copy(map->p[i]));
1079 tc = construct_projected_component(isl_dim_copy(dim), map_i,
1081 isl_map_free(map_i);
1084 qc = isl_map_transitive_closure(qc, exact);
1090 return isl_map_universe(isl_map_get_dim(map));
1093 if (!left || !right)
1094 rtc = isl_map_union(isl_map_copy(tc),
1095 isl_map_identity(isl_dim_domain(isl_map_get_dim(tc))));
1097 qc = isl_map_apply_range(rtc, qc);
1099 qc = isl_map_apply_range(qc, rtc);
1100 qc = isl_map_union(tc, qc);
1111 /* Given a map "map", try to find a basic map such that
1112 * map^+ can be computed as
1114 * map^+ = map_i^+ \cup
1115 * \bigcup_j ((map_i^+ \cup Id_C)^+ \circ map_j \circ (map_i^+ \cup Id_C))^+
1117 * with C the simple hull of the domain and range of the input map.
1118 * map_i^ \cup Id_C is computed by allowing the path lengths to be zero
1119 * and by intersecting domain and range with C.
1120 * Of course, we need to check that this is actually equal to map_i^ \cup Id_C.
1121 * Also, we only use the incremental computation if all the transitive
1122 * closures are exact and if the number of basic maps in the union,
1123 * after computing the integer divisions, is smaller than the number
1124 * of basic maps in the input map.
1126 static int incemental_on_entire_domain(__isl_keep isl_dim *dim,
1127 __isl_keep isl_map *map,
1128 isl_set **dom, isl_set **ran, int *left, int *right,
1129 __isl_give isl_map **res)
1137 C = isl_set_union(isl_map_domain(isl_map_copy(map)),
1138 isl_map_range(isl_map_copy(map)));
1139 C = isl_set_from_basic_set(isl_set_simple_hull(C));
1147 d = isl_map_dim(map, isl_dim_in);
1149 for (i = 0; i < map->n; ++i) {
1151 int exact_i, spurious;
1153 dom[i] = isl_set_from_basic_set(isl_basic_map_domain(
1154 isl_basic_map_copy(map->p[i])));
1155 ran[i] = isl_set_from_basic_set(isl_basic_map_range(
1156 isl_basic_map_copy(map->p[i])));
1157 qc = q_closure(isl_dim_copy(dim), isl_set_copy(C),
1158 map->p[i], &exact_i);
1165 spurious = has_spurious_elements(qc, dom[i], ran[i]);
1172 qc = isl_map_project_out(qc, isl_dim_in, d, 1);
1173 qc = isl_map_project_out(qc, isl_dim_out, d, 1);
1174 qc = isl_map_compute_divs(qc);
1175 for (j = 0; j < map->n; ++j)
1176 left[j] = right[j] = 1;
1177 qc = compose(map, i, qc, left, right);
1180 if (qc->n >= map->n) {
1184 *res = compute_incremental(isl_dim_copy(dim), map, i, qc,
1185 left, right, &exact_i);
1196 return *res != NULL;
1202 /* Try and compute the transitive closure of "map" as
1204 * map^+ = map_i^+ \cup
1205 * \bigcup_j ((map_i^+ \cup Id_C)^+ \circ map_j \circ (map_i^+ \cup Id_C))^+
1207 * with C either the simple hull of the domain and range of the entire
1208 * map or the simple hull of domain and range of map_i.
1210 static __isl_give isl_map *incremental_closure(__isl_take isl_dim *dim,
1211 __isl_keep isl_map *map, int *exact, int project)
1214 isl_set **dom = NULL;
1215 isl_set **ran = NULL;
1220 isl_map *res = NULL;
1223 return construct_projected_component(dim, map, exact, project);
1228 return construct_projected_component(dim, map, exact, project);
1230 d = isl_map_dim(map, isl_dim_in);
1232 dom = isl_calloc_array(map->ctx, isl_set *, map->n);
1233 ran = isl_calloc_array(map->ctx, isl_set *, map->n);
1234 left = isl_calloc_array(map->ctx, int, map->n);
1235 right = isl_calloc_array(map->ctx, int, map->n);
1236 if (!ran || !dom || !left || !right)
1239 if (incemental_on_entire_domain(dim, map, dom, ran, left, right, &res) < 0)
1242 for (i = 0; !res && i < map->n; ++i) {
1244 int exact_i, spurious, comp;
1246 dom[i] = isl_set_from_basic_set(
1247 isl_basic_map_domain(
1248 isl_basic_map_copy(map->p[i])));
1252 ran[i] = isl_set_from_basic_set(
1253 isl_basic_map_range(
1254 isl_basic_map_copy(map->p[i])));
1257 C = isl_set_union(isl_set_copy(dom[i]),
1258 isl_set_copy(ran[i]));
1259 C = isl_set_from_basic_set(isl_set_simple_hull(C));
1266 comp = composability(C, i, dom, ran, left, right, map);
1267 if (!comp || comp < 0) {
1273 qc = q_closure(isl_dim_copy(dim), C, map->p[i], &exact_i);
1280 spurious = has_spurious_elements(qc, dom[i], ran[i]);
1287 qc = isl_map_project_out(qc, isl_dim_in, d, 1);
1288 qc = isl_map_project_out(qc, isl_dim_out, d, 1);
1289 qc = isl_map_compute_divs(qc);
1290 qc = compose(map, i, qc, (comp & LEFT) ? left : NULL,
1291 (comp & RIGHT) ? right : NULL);
1294 if (qc->n >= map->n) {
1298 res = compute_incremental(isl_dim_copy(dim), map, i, qc,
1299 (comp & LEFT) ? left : NULL,
1300 (comp & RIGHT) ? right : NULL, &exact_i);
1309 for (i = 0; i < map->n; ++i) {
1310 isl_set_free(dom[i]);
1311 isl_set_free(ran[i]);
1323 return construct_projected_component(dim, map, exact, project);
1326 for (i = 0; i < map->n; ++i)
1327 isl_set_free(dom[i]);
1330 for (i = 0; i < map->n; ++i)
1331 isl_set_free(ran[i]);
1339 /* Given an array of sets "set", add "dom" at position "pos"
1340 * and search for elements at earlier positions that overlap with "dom".
1341 * If any can be found, then merge all of them, together with "dom", into
1342 * a single set and assign the union to the first in the array,
1343 * which becomes the new group leader for all groups involved in the merge.
1344 * During the search, we only consider group leaders, i.e., those with
1345 * group[i] = i, as the other sets have already been combined
1346 * with one of the group leaders.
1348 static int merge(isl_set **set, int *group, __isl_take isl_set *dom, int pos)
1353 set[pos] = isl_set_copy(dom);
1355 for (i = pos - 1; i >= 0; --i) {
1361 o = isl_set_overlaps(set[i], dom);
1367 set[i] = isl_set_union(set[i], set[group[pos]]);
1368 set[group[pos]] = NULL;
1371 group[group[pos]] = i;
1382 /* Replace each entry in the n by n grid of maps by the cross product
1383 * with the relation { [i] -> [i + 1] }.
1385 static int add_length(__isl_keep isl_map *map, isl_map ***grid, int n)
1389 isl_basic_map *bstep;
1396 dim = isl_map_get_dim(map);
1397 nparam = isl_dim_size(dim, isl_dim_param);
1398 dim = isl_dim_drop(dim, isl_dim_in, 0, isl_dim_size(dim, isl_dim_in));
1399 dim = isl_dim_drop(dim, isl_dim_out, 0, isl_dim_size(dim, isl_dim_out));
1400 dim = isl_dim_add(dim, isl_dim_in, 1);
1401 dim = isl_dim_add(dim, isl_dim_out, 1);
1402 bstep = isl_basic_map_alloc_dim(dim, 0, 1, 0);
1403 k = isl_basic_map_alloc_equality(bstep);
1405 isl_basic_map_free(bstep);
1408 isl_seq_clr(bstep->eq[k], 1 + isl_basic_map_total_dim(bstep));
1409 isl_int_set_si(bstep->eq[k][0], 1);
1410 isl_int_set_si(bstep->eq[k][1 + nparam], 1);
1411 isl_int_set_si(bstep->eq[k][1 + nparam + 1], -1);
1412 bstep = isl_basic_map_finalize(bstep);
1413 step = isl_map_from_basic_map(bstep);
1415 for (i = 0; i < n; ++i)
1416 for (j = 0; j < n; ++j)
1417 grid[i][j] = isl_map_product(grid[i][j],
1418 isl_map_copy(step));
1425 /* Given a partition of the domains and ranges of the basic maps in "map",
1426 * apply the Floyd-Warshall algorithm with the elements in the partition
1429 * In particular, there are "n" elements in the partition and "group" is
1430 * an array of length 2 * map->n with entries in [0,n-1].
1432 * We first construct a matrix of relations based on the partition information,
1433 * apply Floyd-Warshall on this matrix of relations and then take the
1434 * union of all entries in the matrix as the final result.
1436 * The algorithm iterates over all vertices. In each step, the whole
1437 * matrix is updated to include all paths that go to the current vertex,
1438 * possibly stay there a while (including passing through earlier vertices)
1439 * and then come back. At the start of each iteration, the diagonal
1440 * element corresponding to the current vertex is replaced by its
1441 * transitive closure to account for all indirect paths that stay
1442 * in the current vertex.
1444 * If we are actually computing the power instead of the transitive closure,
1445 * i.e., when "project" is not set, then the result should have the
1446 * path lengths encoded as the difference between an extra pair of
1447 * coordinates. We therefore apply the nested transitive closures
1448 * to relations that include these lengths. In particular, we replace
1449 * the input relation by the cross product with the unit length relation
1450 * { [i] -> [i + 1] }.
1452 static __isl_give isl_map *floyd_warshall_with_groups(__isl_take isl_dim *dim,
1453 __isl_keep isl_map *map, int *exact, int project, int *group, int n)
1457 isl_map ***grid = NULL;
1465 return incremental_closure(dim, map, exact, project);
1468 grid = isl_calloc_array(map->ctx, isl_map **, n);
1471 for (i = 0; i < n; ++i) {
1472 grid[i] = isl_calloc_array(map->ctx, isl_map *, n);
1475 for (j = 0; j < n; ++j)
1476 grid[i][j] = isl_map_empty(isl_map_get_dim(map));
1479 for (k = 0; k < map->n; ++k) {
1481 j = group[2 * k + 1];
1482 grid[i][j] = isl_map_union(grid[i][j],
1483 isl_map_from_basic_map(
1484 isl_basic_map_copy(map->p[k])));
1487 if (!project && add_length(map, grid, n) < 0)
1490 for (r = 0; r < n; ++r) {
1492 grid[r][r] = isl_map_transitive_closure(grid[r][r],
1493 (exact && *exact) ? &r_exact : NULL);
1494 if (exact && *exact && !r_exact)
1497 for (p = 0; p < n; ++p)
1498 for (q = 0; q < n; ++q) {
1500 if (p == r && q == r)
1502 loop = isl_map_apply_range(
1503 isl_map_copy(grid[p][r]),
1504 isl_map_copy(grid[r][q]));
1505 grid[p][q] = isl_map_union(grid[p][q], loop);
1506 loop = isl_map_apply_range(
1507 isl_map_copy(grid[p][r]),
1508 isl_map_apply_range(
1509 isl_map_copy(grid[r][r]),
1510 isl_map_copy(grid[r][q])));
1511 grid[p][q] = isl_map_union(grid[p][q], loop);
1512 grid[p][q] = isl_map_coalesce(grid[p][q]);
1516 app = isl_map_empty(isl_map_get_dim(map));
1518 for (i = 0; i < n; ++i) {
1519 for (j = 0; j < n; ++j)
1520 app = isl_map_union(app, grid[i][j]);
1531 for (i = 0; i < n; ++i) {
1534 for (j = 0; j < n; ++j)
1535 isl_map_free(grid[i][j]);
1544 /* Check if the domains and ranges of the basic maps in "map" can
1545 * be partitioned, and if so, apply Floyd-Warshall on the elements
1546 * of the partition. Note that we also apply this algorithm
1547 * if we want to compute the power, i.e., when "project" is not set.
1548 * However, the results are unlikely to be exact since the recursive
1549 * calls inside the Floyd-Warshall algorithm typically result in
1550 * non-linear path lengths quite quickly.
1552 * To find the partition, we simply consider all of the domains
1553 * and ranges in turn and combine those that overlap.
1554 * "set" contains the partition elements and "group" indicates
1555 * to which partition element a given domain or range belongs.
1556 * The domain of basic map i corresponds to element 2 * i in these arrays,
1557 * while the domain corresponds to element 2 * i + 1.
1558 * During the construction group[k] is either equal to k,
1559 * in which case set[k] contains the union of all the domains and
1560 * ranges in the corresponding group, or is equal to some l < k,
1561 * with l another domain or range in the same group.
1563 static __isl_give isl_map *floyd_warshall(__isl_take isl_dim *dim,
1564 __isl_keep isl_map *map, int *exact, int project)
1567 isl_set **set = NULL;
1574 return incremental_closure(dim, map, exact, project);
1576 set = isl_calloc_array(map->ctx, isl_set *, 2 * map->n);
1577 group = isl_alloc_array(map->ctx, int, 2 * map->n);
1582 for (i = 0; i < map->n; ++i) {
1584 dom = isl_set_from_basic_set(isl_basic_map_domain(
1585 isl_basic_map_copy(map->p[i])));
1586 if (merge(set, group, dom, 2 * i) < 0)
1588 dom = isl_set_from_basic_set(isl_basic_map_range(
1589 isl_basic_map_copy(map->p[i])));
1590 if (merge(set, group, dom, 2 * i + 1) < 0)
1595 for (i = 0; i < 2 * map->n; ++i)
1599 group[i] = group[group[i]];
1601 for (i = 0; i < 2 * map->n; ++i)
1602 isl_set_free(set[i]);
1606 return floyd_warshall_with_groups(dim, map, exact, project, group, n);
1608 for (i = 0; i < 2 * map->n; ++i)
1609 isl_set_free(set[i]);
1616 /* Structure for representing the nodes in the graph being traversed
1617 * using Tarjan's algorithm.
1618 * index represents the order in which nodes are visited.
1619 * min_index is the index of the root of a (sub)component.
1620 * on_stack indicates whether the node is currently on the stack.
1622 struct basic_map_sort_node {
1627 /* Structure for representing the graph being traversed
1628 * using Tarjan's algorithm.
1629 * len is the number of nodes
1630 * node is an array of nodes
1631 * stack contains the nodes on the path from the root to the current node
1632 * sp is the stack pointer
1633 * index is the index of the last node visited
1634 * order contains the elements of the components separated by -1
1635 * op represents the current position in order
1637 * check_closed is set if we may have used the fact that
1638 * a pair of basic maps can be interchanged
1640 struct basic_map_sort {
1642 struct basic_map_sort_node *node;
1651 static void basic_map_sort_free(struct basic_map_sort *s)
1661 static struct basic_map_sort *basic_map_sort_alloc(struct isl_ctx *ctx, int len)
1663 struct basic_map_sort *s;
1666 s = isl_calloc_type(ctx, struct basic_map_sort);
1670 s->node = isl_alloc_array(ctx, struct basic_map_sort_node, len);
1673 for (i = 0; i < len; ++i)
1674 s->node[i].index = -1;
1675 s->stack = isl_alloc_array(ctx, int, len);
1678 s->order = isl_alloc_array(ctx, int, 2 * len);
1686 s->check_closed = 0;
1690 basic_map_sort_free(s);
1694 /* Check whether in the computation of the transitive closure
1695 * "bmap1" (R_1) should follow (or be part of the same component as)
1698 * That is check whether
1706 * If so, then there is no reason for R_1 to immediately follow R_2
1709 * *check_closed is set if the subset relation holds while
1710 * R_1 \circ R_2 is not empty.
1712 static int basic_map_follows(__isl_keep isl_basic_map *bmap1,
1713 __isl_keep isl_basic_map *bmap2, int *check_closed)
1715 struct isl_map *map12 = NULL;
1716 struct isl_map *map21 = NULL;
1719 map21 = isl_map_from_basic_map(
1720 isl_basic_map_apply_range(
1721 isl_basic_map_copy(bmap2),
1722 isl_basic_map_copy(bmap1)));
1723 subset = isl_map_is_empty(map21);
1727 isl_map_free(map21);
1731 map12 = isl_map_from_basic_map(
1732 isl_basic_map_apply_range(
1733 isl_basic_map_copy(bmap1),
1734 isl_basic_map_copy(bmap2)));
1736 subset = isl_map_is_subset(map21, map12);
1738 isl_map_free(map12);
1739 isl_map_free(map21);
1744 return subset < 0 ? -1 : !subset;
1746 isl_map_free(map21);
1750 /* Perform Tarjan's algorithm for computing the strongly connected components
1751 * in the graph with the disjuncts of "map" as vertices and with an
1752 * edge between any pair of disjuncts such that the first has
1753 * to be applied after the second.
1755 static int power_components_tarjan(struct basic_map_sort *s,
1756 __isl_keep isl_map *map, int i)
1760 s->node[i].index = s->index;
1761 s->node[i].min_index = s->index;
1762 s->node[i].on_stack = 1;
1764 s->stack[s->sp++] = i;
1766 for (j = s->len - 1; j >= 0; --j) {
1771 if (s->node[j].index >= 0 &&
1772 (!s->node[j].on_stack ||
1773 s->node[j].index > s->node[i].min_index))
1776 f = basic_map_follows(map->p[i], map->p[j], &s->check_closed);
1782 if (s->node[j].index < 0) {
1783 power_components_tarjan(s, map, j);
1784 if (s->node[j].min_index < s->node[i].min_index)
1785 s->node[i].min_index = s->node[j].min_index;
1786 } else if (s->node[j].index < s->node[i].min_index)
1787 s->node[i].min_index = s->node[j].index;
1790 if (s->node[i].index != s->node[i].min_index)
1794 j = s->stack[--s->sp];
1795 s->node[j].on_stack = 0;
1796 s->order[s->op++] = j;
1798 s->order[s->op++] = -1;
1803 /* Given a union of basic maps R = \cup_i R_i \subseteq D \times D
1804 * and a dimension specification (Z^{n+1} -> Z^{n+1}),
1805 * construct a map that is an overapproximation of the map
1806 * that takes an element from the dom R \times Z to an
1807 * element from ran R \times Z, such that the first n coordinates of the
1808 * difference between them is a sum of differences between images
1809 * and pre-images in one of the R_i and such that the last coordinate
1810 * is equal to the number of steps taken.
1811 * If "project" is set, then these final coordinates are not included,
1812 * i.e., a relation of type Z^n -> Z^n is returned.
1815 * \Delta_i = { y - x | (x, y) in R_i }
1817 * then the constructed map is an overapproximation of
1819 * { (x) -> (x + d) | \exists k_i >= 0, \delta_i \in \Delta_i :
1820 * d = (\sum_i k_i \delta_i, \sum_i k_i) and
1821 * x in dom R and x + d in ran R }
1825 * { (x) -> (x + d) | \exists k_i >= 0, \delta_i \in \Delta_i :
1826 * d = (\sum_i k_i \delta_i) and
1827 * x in dom R and x + d in ran R }
1829 * if "project" is set.
1831 * We first split the map into strongly connected components, perform
1832 * the above on each component and then join the results in the correct
1833 * order, at each join also taking in the union of both arguments
1834 * to allow for paths that do not go through one of the two arguments.
1836 static __isl_give isl_map *construct_power_components(__isl_take isl_dim *dim,
1837 __isl_keep isl_map *map, int *exact, int project)
1840 struct isl_map *path = NULL;
1841 struct basic_map_sort *s = NULL;
1848 return floyd_warshall(dim, map, exact, project);
1850 s = basic_map_sort_alloc(map->ctx, map->n);
1853 for (i = map->n - 1; i >= 0; --i) {
1854 if (s->node[i].index >= 0)
1856 if (power_components_tarjan(s, map, i) < 0)
1861 if (s->check_closed && !exact)
1862 exact = &local_exact;
1868 path = isl_map_empty(isl_map_get_dim(map));
1870 path = isl_map_empty(isl_dim_copy(dim));
1872 struct isl_map *comp;
1873 isl_map *path_comp, *path_comb;
1874 comp = isl_map_alloc_dim(isl_map_get_dim(map), n, 0);
1875 while (s->order[i] != -1) {
1876 comp = isl_map_add_basic_map(comp,
1877 isl_basic_map_copy(map->p[s->order[i]]));
1881 path_comp = floyd_warshall(isl_dim_copy(dim),
1882 comp, exact, project);
1883 path_comb = isl_map_apply_range(isl_map_copy(path),
1884 isl_map_copy(path_comp));
1885 path = isl_map_union(path, path_comp);
1886 path = isl_map_union(path, path_comb);
1892 if (c > 1 && s->check_closed && !*exact) {
1895 closed = isl_map_is_transitively_closed(path);
1899 basic_map_sort_free(s);
1901 return floyd_warshall(dim, map, orig_exact, project);
1905 basic_map_sort_free(s);
1910 basic_map_sort_free(s);
1916 /* Given a union of basic maps R = \cup_i R_i \subseteq D \times D,
1917 * construct a map that is an overapproximation of the map
1918 * that takes an element from the space D to another
1919 * element from the same space, such that the difference between
1920 * them is a strictly positive sum of differences between images
1921 * and pre-images in one of the R_i.
1922 * The number of differences in the sum is equated to parameter "param".
1925 * \Delta_i = { y - x | (x, y) in R_i }
1927 * then the constructed map is an overapproximation of
1929 * { (x) -> (x + d) | \exists k_i >= 0, \delta_i \in \Delta_i :
1930 * d = \sum_i k_i \delta_i and k = \sum_i k_i > 0 }
1933 * { (x) -> (x + d) | \exists k_i >= 0, \delta_i \in \Delta_i :
1934 * d = \sum_i k_i \delta_i and \sum_i k_i > 0 }
1936 * if "project" is set.
1938 * If "project" is not set, then
1939 * we construct an extended mapping with an extra coordinate
1940 * that indicates the number of steps taken. In particular,
1941 * the difference in the last coordinate is equal to the number
1942 * of steps taken to move from a domain element to the corresponding
1945 static __isl_give isl_map *construct_power(__isl_keep isl_map *map,
1946 int *exact, int project)
1948 struct isl_map *app = NULL;
1949 struct isl_dim *dim = NULL;
1955 dim = isl_map_get_dim(map);
1957 d = isl_dim_size(dim, isl_dim_in);
1958 dim = isl_dim_add(dim, isl_dim_in, 1);
1959 dim = isl_dim_add(dim, isl_dim_out, 1);
1961 app = construct_power_components(isl_dim_copy(dim), map,
1969 /* Compute the positive powers of "map", or an overapproximation.
1970 * If the result is exact, then *exact is set to 1.
1972 * If project is set, then we are actually interested in the transitive
1973 * closure, so we can use a more relaxed exactness check.
1974 * The lengths of the paths are also projected out instead of being
1975 * encoded as the difference between an extra pair of final coordinates.
1977 static __isl_give isl_map *map_power(__isl_take isl_map *map,
1978 int *exact, int project)
1980 struct isl_map *app = NULL;
1988 isl_assert(map->ctx,
1989 isl_map_dim(map, isl_dim_in) == isl_map_dim(map, isl_dim_out),
1992 app = construct_power(map, exact, project);
2002 /* Compute the positive powers of "map", or an overapproximation.
2003 * The power is given by parameter "param". If the result is exact,
2004 * then *exact is set to 1.
2005 * map_power constructs an extended relation with the path lengths
2006 * encoded as the difference between the final coordinates.
2007 * In the final step, this difference is equated to the parameter "param"
2008 * and made positive. The extra coordinates are subsequently projected out.
2010 __isl_give isl_map *isl_map_power(__isl_take isl_map *map, unsigned param,
2013 isl_dim *target_dim;
2021 isl_assert(map->ctx, param < isl_map_dim(map, isl_dim_param),
2024 d = isl_map_dim(map, isl_dim_in);
2026 map = isl_map_compute_divs(map);
2027 map = isl_map_coalesce(map);
2029 if (isl_map_fast_is_empty(map))
2032 target_dim = isl_map_get_dim(map);
2033 map = map_power(map, exact, 0);
2035 dim = isl_map_get_dim(map);
2036 diff = equate_parameter_to_length(dim, param);
2037 map = isl_map_intersect(map, diff);
2038 map = isl_map_project_out(map, isl_dim_in, d, 1);
2039 map = isl_map_project_out(map, isl_dim_out, d, 1);
2041 map = isl_map_reset_dim(map, target_dim);
2049 /* Compute a relation that maps each element in the range of the input
2050 * relation to the lengths of all paths composed of edges in the input
2051 * relation that end up in the given range element.
2052 * The result may be an overapproximation, in which case *exact is set to 0.
2053 * The resulting relation is very similar to the power relation.
2054 * The difference are that the domain has been projected out, the
2055 * range has become the domain and the exponent is the range instead
2058 __isl_give isl_map *isl_map_reaching_path_lengths(__isl_take isl_map *map,
2069 d = isl_map_dim(map, isl_dim_in);
2070 param = isl_map_dim(map, isl_dim_param);
2072 map = isl_map_compute_divs(map);
2073 map = isl_map_coalesce(map);
2075 if (isl_map_fast_is_empty(map)) {
2078 map = isl_map_project_out(map, isl_dim_out, 0, d);
2079 map = isl_map_add(map, isl_dim_out, 1);
2083 map = map_power(map, exact, 0);
2085 map = isl_map_add(map, isl_dim_param, 1);
2086 dim = isl_map_get_dim(map);
2087 diff = equate_parameter_to_length(dim, param);
2088 map = isl_map_intersect(map, diff);
2089 map = isl_map_project_out(map, isl_dim_in, 0, d + 1);
2090 map = isl_map_project_out(map, isl_dim_out, d, 1);
2091 map = isl_map_reverse(map);
2092 map = isl_map_move_dims(map, isl_dim_out, 0, isl_dim_param, param, 1);
2097 /* Check whether equality i of bset is a pure stride constraint
2098 * on a single dimensions, i.e., of the form
2102 * with k a constant and e an existentially quantified variable.
2104 static int is_eq_stride(__isl_keep isl_basic_set *bset, int i)
2116 if (!isl_int_is_zero(bset->eq[i][0]))
2119 nparam = isl_basic_set_dim(bset, isl_dim_param);
2120 d = isl_basic_set_dim(bset, isl_dim_set);
2121 n_div = isl_basic_set_dim(bset, isl_dim_div);
2123 if (isl_seq_first_non_zero(bset->eq[i] + 1, nparam) != -1)
2125 pos1 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam, d);
2128 if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + pos1 + 1,
2129 d - pos1 - 1) != -1)
2132 pos2 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d, n_div);
2135 if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d + pos2 + 1,
2136 n_div - pos2 - 1) != -1)
2138 if (!isl_int_is_one(bset->eq[i][1 + nparam + pos1]) &&
2139 !isl_int_is_negone(bset->eq[i][1 + nparam + pos1]))
2145 /* Given a map, compute the smallest superset of this map that is of the form
2147 * { i -> j : L <= j - i <= U and exists a_p: j_p - i_p = M_p a_p }
2149 * (where p ranges over the (non-parametric) dimensions),
2150 * compute the transitive closure of this map, i.e.,
2152 * { i -> j : exists k > 0:
2153 * k L <= j - i <= k U and exists a: j_p - i_p = M_p a_p }
2155 * and intersect domain and range of this transitive closure with
2156 * the given domain and range.
2158 * If with_id is set, then try to include as much of the identity mapping
2159 * as possible, by computing
2161 * { i -> j : exists k >= 0:
2162 * k L <= j - i <= k U and exists a: j_p - i_p = M_p a_p }
2164 * instead (i.e., allow k = 0).
2166 * In practice, we compute the difference set
2168 * delta = { j - i | i -> j in map },
2170 * look for stride constraint on the individual dimensions and compute
2171 * (constant) lower and upper bounds for each individual dimension,
2172 * adding a constraint for each bound not equal to infinity.
2174 static __isl_give isl_map *box_closure_on_domain(__isl_take isl_map *map,
2175 __isl_take isl_set *dom, __isl_take isl_set *ran, int with_id)
2184 isl_map *app = NULL;
2185 isl_basic_set *aff = NULL;
2186 isl_basic_map *bmap = NULL;
2187 isl_vec *obj = NULL;
2192 delta = isl_map_deltas(isl_map_copy(map));
2194 aff = isl_set_affine_hull(isl_set_copy(delta));
2197 dim = isl_map_get_dim(map);
2198 d = isl_dim_size(dim, isl_dim_in);
2199 nparam = isl_dim_size(dim, isl_dim_param);
2200 total = isl_dim_total(dim);
2201 bmap = isl_basic_map_alloc_dim(dim,
2202 aff->n_div + 1, aff->n_div, 2 * d + 1);
2203 for (i = 0; i < aff->n_div + 1; ++i) {
2204 k = isl_basic_map_alloc_div(bmap);
2207 isl_int_set_si(bmap->div[k][0], 0);
2209 for (i = 0; i < aff->n_eq; ++i) {
2210 if (!is_eq_stride(aff, i))
2212 k = isl_basic_map_alloc_equality(bmap);
2215 isl_seq_clr(bmap->eq[k], 1 + nparam);
2216 isl_seq_cpy(bmap->eq[k] + 1 + nparam + d,
2217 aff->eq[i] + 1 + nparam, d);
2218 isl_seq_neg(bmap->eq[k] + 1 + nparam,
2219 aff->eq[i] + 1 + nparam, d);
2220 isl_seq_cpy(bmap->eq[k] + 1 + nparam + 2 * d,
2221 aff->eq[i] + 1 + nparam + d, aff->n_div);
2222 isl_int_set_si(bmap->eq[k][1 + total + aff->n_div], 0);
2224 obj = isl_vec_alloc(map->ctx, 1 + nparam + d);
2227 isl_seq_clr(obj->el, 1 + nparam + d);
2228 for (i = 0; i < d; ++ i) {
2229 enum isl_lp_result res;
2231 isl_int_set_si(obj->el[1 + nparam + i], 1);
2233 res = isl_set_solve_lp(delta, 0, obj->el, map->ctx->one, &opt,
2235 if (res == isl_lp_error)
2237 if (res == isl_lp_ok) {
2238 k = isl_basic_map_alloc_inequality(bmap);
2241 isl_seq_clr(bmap->ineq[k],
2242 1 + nparam + 2 * d + bmap->n_div);
2243 isl_int_set_si(bmap->ineq[k][1 + nparam + i], -1);
2244 isl_int_set_si(bmap->ineq[k][1 + nparam + d + i], 1);
2245 isl_int_neg(bmap->ineq[k][1 + nparam + 2 * d + aff->n_div], opt);
2248 res = isl_set_solve_lp(delta, 1, obj->el, map->ctx->one, &opt,
2250 if (res == isl_lp_error)
2252 if (res == isl_lp_ok) {
2253 k = isl_basic_map_alloc_inequality(bmap);
2256 isl_seq_clr(bmap->ineq[k],
2257 1 + nparam + 2 * d + bmap->n_div);
2258 isl_int_set_si(bmap->ineq[k][1 + nparam + i], 1);
2259 isl_int_set_si(bmap->ineq[k][1 + nparam + d + i], -1);
2260 isl_int_set(bmap->ineq[k][1 + nparam + 2 * d + aff->n_div], opt);
2263 isl_int_set_si(obj->el[1 + nparam + i], 0);
2265 k = isl_basic_map_alloc_inequality(bmap);
2268 isl_seq_clr(bmap->ineq[k],
2269 1 + nparam + 2 * d + bmap->n_div);
2271 isl_int_set_si(bmap->ineq[k][0], -1);
2272 isl_int_set_si(bmap->ineq[k][1 + nparam + 2 * d + aff->n_div], 1);
2274 app = isl_map_from_domain_and_range(dom, ran);
2277 isl_basic_set_free(aff);
2279 bmap = isl_basic_map_finalize(bmap);
2280 isl_set_free(delta);
2283 map = isl_map_from_basic_map(bmap);
2284 map = isl_map_intersect(map, app);
2289 isl_basic_map_free(bmap);
2290 isl_basic_set_free(aff);
2294 isl_set_free(delta);
2299 /* Given a map, compute the smallest superset of this map that is of the form
2301 * { i -> j : L <= j - i <= U and exists a_p: j_p - i_p = M_p a_p }
2303 * (where p ranges over the (non-parametric) dimensions),
2304 * compute the transitive closure of this map, i.e.,
2306 * { i -> j : exists k > 0:
2307 * k L <= j - i <= k U and exists a: j_p - i_p = M_p a_p }
2309 * and intersect domain and range of this transitive closure with
2310 * domain and range of the original map.
2312 static __isl_give isl_map *box_closure(__isl_take isl_map *map)
2317 domain = isl_map_domain(isl_map_copy(map));
2318 domain = isl_set_coalesce(domain);
2319 range = isl_map_range(isl_map_copy(map));
2320 range = isl_set_coalesce(range);
2322 return box_closure_on_domain(map, domain, range, 0);
2325 /* Given a map, compute the smallest superset of this map that is of the form
2327 * { i -> j : L <= j - i <= U and exists a_p: j_p - i_p = M_p a_p }
2329 * (where p ranges over the (non-parametric) dimensions),
2330 * compute the transitive and partially reflexive closure of this map, i.e.,
2332 * { i -> j : exists k >= 0:
2333 * k L <= j - i <= k U and exists a: j_p - i_p = M_p a_p }
2335 * and intersect domain and range of this transitive closure with
2338 static __isl_give isl_map *box_closure_with_identity(__isl_take isl_map *map,
2339 __isl_take isl_set *dom)
2341 return box_closure_on_domain(map, dom, isl_set_copy(dom), 1);
2344 /* Check whether app is the transitive closure of map.
2345 * In particular, check that app is acyclic and, if so,
2348 * app \subset (map \cup (map \circ app))
2350 static int check_exactness_omega(__isl_keep isl_map *map,
2351 __isl_keep isl_map *app)
2355 int is_empty, is_exact;
2359 delta = isl_map_deltas(isl_map_copy(app));
2360 d = isl_set_dim(delta, isl_dim_set);
2361 for (i = 0; i < d; ++i)
2362 delta = isl_set_fix_si(delta, isl_dim_set, i, 0);
2363 is_empty = isl_set_is_empty(delta);
2364 isl_set_free(delta);
2370 test = isl_map_apply_range(isl_map_copy(app), isl_map_copy(map));
2371 test = isl_map_union(test, isl_map_copy(map));
2372 is_exact = isl_map_is_subset(app, test);
2378 /* Check if basic map M_i can be combined with all the other
2379 * basic maps such that
2383 * can be computed as
2385 * M_i \cup (\cup_{j \ne i} M_i^* \circ M_j \circ M_i^*)^+
2387 * In particular, check if we can compute a compact representation
2390 * M_i^* \circ M_j \circ M_i^*
2393 * Let M_i^? be an extension of M_i^+ that allows paths
2394 * of length zero, i.e., the result of box_closure(., 1).
2395 * The criterion, as proposed by Kelly et al., is that
2396 * id = M_i^? - M_i^+ can be represented as a basic map
2399 * id \circ M_j \circ id = M_j
2403 * If this function returns 1, then tc and qc are set to
2404 * M_i^+ and M_i^?, respectively.
2406 static int can_be_split_off(__isl_keep isl_map *map, int i,
2407 __isl_give isl_map **tc, __isl_give isl_map **qc)
2409 isl_map *map_i, *id = NULL;
2416 C = isl_set_union(isl_map_domain(isl_map_copy(map)),
2417 isl_map_range(isl_map_copy(map)));
2418 C = isl_set_from_basic_set(isl_set_simple_hull(C));
2422 map_i = isl_map_from_basic_map(isl_basic_map_copy(map->p[i]));
2423 *tc = box_closure(isl_map_copy(map_i));
2424 *qc = box_closure_with_identity(map_i, C);
2425 id = isl_map_subtract(isl_map_copy(*qc), isl_map_copy(*tc));
2429 if (id->n != 1 || (*qc)->n != 1)
2432 for (j = 0; j < map->n; ++j) {
2433 isl_map *map_j, *test;
2438 map_j = isl_map_from_basic_map(
2439 isl_basic_map_copy(map->p[j]));
2440 test = isl_map_apply_range(isl_map_copy(id),
2441 isl_map_copy(map_j));
2442 test = isl_map_apply_range(test, isl_map_copy(id));
2443 is_ok = isl_map_is_equal(test, map_j);
2444 isl_map_free(map_j);
2472 static __isl_give isl_map *box_closure_with_check(__isl_take isl_map *map,
2477 app = box_closure(isl_map_copy(map));
2479 *exact = check_exactness_omega(map, app);
2485 /* Compute an overapproximation of the transitive closure of "map"
2486 * using a variation of the algorithm from
2487 * "Transitive Closure of Infinite Graphs and its Applications"
2490 * We first check whether we can can split of any basic map M_i and
2497 * M_i \cup (\cup_{j \ne i} M_i^* \circ M_j \circ M_i^*)^+
2499 * using a recursive call on the remaining map.
2501 * If not, we simply call box_closure on the whole map.
2503 static __isl_give isl_map *transitive_closure_omega(__isl_take isl_map *map,
2513 return box_closure_with_check(map, exact);
2515 for (i = 0; i < map->n; ++i) {
2518 ok = can_be_split_off(map, i, &tc, &qc);
2524 app = isl_map_alloc_dim(isl_map_get_dim(map), map->n - 1, 0);
2526 for (j = 0; j < map->n; ++j) {
2529 app = isl_map_add_basic_map(app,
2530 isl_basic_map_copy(map->p[j]));
2533 app = isl_map_apply_range(isl_map_copy(qc), app);
2534 app = isl_map_apply_range(app, qc);
2536 app = isl_map_union(tc, transitive_closure_omega(app, NULL));
2537 exact_i = check_exactness_omega(map, app);
2549 return box_closure_with_check(map, exact);
2555 /* Compute the transitive closure of "map", or an overapproximation.
2556 * If the result is exact, then *exact is set to 1.
2557 * Simply use map_power to compute the powers of map, but tell
2558 * it to project out the lengths of the paths instead of equating
2559 * the length to a parameter.
2561 __isl_give isl_map *isl_map_transitive_closure(__isl_take isl_map *map,
2564 isl_dim *target_dim;
2570 if (map->ctx->opt->closure == ISL_CLOSURE_OMEGA)
2571 return transitive_closure_omega(map, exact);
2573 map = isl_map_compute_divs(map);
2574 map = isl_map_coalesce(map);
2575 closed = isl_map_is_transitively_closed(map);
2584 target_dim = isl_map_get_dim(map);
2585 map = map_power(map, exact, 1);
2586 map = isl_map_reset_dim(map, target_dim);