+ C = isl_set_union(isl_map_domain(isl_map_copy(map)),
+ isl_map_range(isl_map_copy(map)));
+ C = isl_set_from_basic_set(isl_set_simple_hull(C));
+ if (!C)
+ return -1;
+ if (C->n != 1) {
+ isl_set_free(C);
+ return 0;
+ }
+
+ d = isl_map_dim(map, isl_dim_in);
+
+ for (i = 0; i < map->n; ++i) {
+ isl_map *qc;
+ int exact_i, spurious;
+ int j;
+ dom[i] = isl_set_from_basic_set(isl_basic_map_domain(
+ isl_basic_map_copy(map->p[i])));
+ ran[i] = isl_set_from_basic_set(isl_basic_map_range(
+ isl_basic_map_copy(map->p[i])));
+ qc = q_closure(isl_space_copy(dim), isl_set_copy(C),
+ map->p[i], &exact_i);
+ if (!qc)
+ goto error;
+ if (!exact_i) {
+ isl_map_free(qc);
+ continue;
+ }
+ spurious = has_spurious_elements(qc, dom[i], ran[i]);
+ if (spurious) {
+ isl_map_free(qc);
+ if (spurious < 0)
+ goto error;
+ continue;
+ }
+ qc = isl_map_project_out(qc, isl_dim_in, d, 1);
+ qc = isl_map_project_out(qc, isl_dim_out, d, 1);
+ qc = isl_map_compute_divs(qc);
+ for (j = 0; j < map->n; ++j)
+ left[j] = right[j] = 1;
+ qc = compose(map, i, qc, left, right);
+ if (!qc)
+ goto error;
+ if (qc->n >= map->n) {
+ isl_map_free(qc);
+ continue;
+ }
+ *res = compute_incremental(isl_space_copy(dim), map, i, qc,
+ left, right, &exact_i);
+ if (!*res)
+ goto error;
+ if (exact_i)
+ break;
+ isl_map_free(*res);
+ *res = NULL;
+ }
+
+ isl_set_free(C);
+
+ return *res != NULL;
+error:
+ isl_set_free(C);
+ return -1;
+}
+
+/* Try and compute the transitive closure of "map" as
+ *
+ * map^+ = map_i^+ \cup
+ * \bigcup_j ((map_i^+ \cup Id_C)^+ \circ map_j \circ (map_i^+ \cup Id_C))^+
+ *
+ * with C either the simple hull of the domain and range of the entire
+ * map or the simple hull of domain and range of map_i.
+ */
+static __isl_give isl_map *incremental_closure(__isl_take isl_space *dim,
+ __isl_keep isl_map *map, int *exact, int project)
+{
+ int i;
+ isl_set **dom = NULL;
+ isl_set **ran = NULL;
+ int *left = NULL;
+ int *right = NULL;
+ isl_set *C;
+ unsigned d;
+ isl_map *res = NULL;
+
+ if (!project)
+ return construct_projected_component(dim, map, exact, project);
+
+ if (!map)
+ goto error;
+ if (map->n <= 1)
+ return construct_projected_component(dim, map, exact, project);
+
+ d = isl_map_dim(map, isl_dim_in);
+
+ dom = isl_calloc_array(map->ctx, isl_set *, map->n);
+ ran = isl_calloc_array(map->ctx, isl_set *, map->n);
+ left = isl_calloc_array(map->ctx, int, map->n);
+ right = isl_calloc_array(map->ctx, int, map->n);
+ if (!ran || !dom || !left || !right)
+ goto error;
+
+ if (incemental_on_entire_domain(dim, map, dom, ran, left, right, &res) < 0)
+ goto error;
+
+ for (i = 0; !res && i < map->n; ++i) {
+ isl_map *qc;
+ int exact_i, spurious, comp;
+ if (!dom[i])
+ dom[i] = isl_set_from_basic_set(
+ isl_basic_map_domain(
+ isl_basic_map_copy(map->p[i])));
+ if (!dom[i])
+ goto error;
+ if (!ran[i])
+ ran[i] = isl_set_from_basic_set(
+ isl_basic_map_range(
+ isl_basic_map_copy(map->p[i])));
+ if (!ran[i])
+ goto error;
+ C = isl_set_union(isl_set_copy(dom[i]),
+ isl_set_copy(ran[i]));
+ C = isl_set_from_basic_set(isl_set_simple_hull(C));
+ if (!C)
+ goto error;
+ if (C->n != 1) {
+ isl_set_free(C);
+ continue;
+ }
+ comp = composability(C, i, dom, ran, left, right, map);
+ if (!comp || comp < 0) {
+ isl_set_free(C);
+ if (comp < 0)
+ goto error;
+ continue;
+ }
+ qc = q_closure(isl_space_copy(dim), C, map->p[i], &exact_i);
+ if (!qc)
+ goto error;
+ if (!exact_i) {
+ isl_map_free(qc);
+ continue;
+ }
+ spurious = has_spurious_elements(qc, dom[i], ran[i]);
+ if (spurious) {
+ isl_map_free(qc);
+ if (spurious < 0)
+ goto error;
+ continue;
+ }
+ qc = isl_map_project_out(qc, isl_dim_in, d, 1);
+ qc = isl_map_project_out(qc, isl_dim_out, d, 1);
+ qc = isl_map_compute_divs(qc);
+ qc = compose(map, i, qc, (comp & LEFT) ? left : NULL,
+ (comp & RIGHT) ? right : NULL);
+ if (!qc)
+ goto error;
+ if (qc->n >= map->n) {
+ isl_map_free(qc);
+ continue;
+ }
+ res = compute_incremental(isl_space_copy(dim), map, i, qc,
+ (comp & LEFT) ? left : NULL,
+ (comp & RIGHT) ? right : NULL, &exact_i);
+ if (!res)
+ goto error;
+ if (exact_i)
+ break;
+ isl_map_free(res);
+ res = NULL;
+ }
+
+ for (i = 0; i < map->n; ++i) {
+ isl_set_free(dom[i]);
+ isl_set_free(ran[i]);
+ }
+ free(dom);
+ free(ran);
+ free(left);
+ free(right);
+
+ if (res) {
+ isl_space_free(dim);
+ return res;
+ }
+
+ return construct_projected_component(dim, map, exact, project);
+error:
+ if (dom)
+ for (i = 0; i < map->n; ++i)
+ isl_set_free(dom[i]);
+ free(dom);
+ if (ran)
+ for (i = 0; i < map->n; ++i)
+ isl_set_free(ran[i]);
+ free(ran);
+ free(left);
+ free(right);
+ isl_space_free(dim);
+ return NULL;
+}
+
+/* Given an array of sets "set", add "dom" at position "pos"
+ * and search for elements at earlier positions that overlap with "dom".
+ * If any can be found, then merge all of them, together with "dom", into
+ * a single set and assign the union to the first in the array,
+ * which becomes the new group leader for all groups involved in the merge.
+ * During the search, we only consider group leaders, i.e., those with
+ * group[i] = i, as the other sets have already been combined
+ * with one of the group leaders.
+ */
+static int merge(isl_set **set, int *group, __isl_take isl_set *dom, int pos)
+{
+ int i;
+
+ group[pos] = pos;
+ set[pos] = isl_set_copy(dom);
+
+ for (i = pos - 1; i >= 0; --i) {
+ int o;
+
+ if (group[i] != i)
+ continue;
+
+ o = isl_set_overlaps(set[i], dom);
+ if (o < 0)
+ goto error;
+ if (!o)
+ continue;
+
+ set[i] = isl_set_union(set[i], set[group[pos]]);
+ set[group[pos]] = NULL;
+ if (!set[i])
+ goto error;
+ group[group[pos]] = i;
+ group[pos] = i;
+ }
+
+ isl_set_free(dom);
+ return 0;
+error:
+ isl_set_free(dom);
+ return -1;
+}
+
+/* Replace each entry in the n by n grid of maps by the cross product
+ * with the relation { [i] -> [i + 1] }.
+ */
+static int add_length(__isl_keep isl_map *map, isl_map ***grid, int n)
+{
+ int i, j, k;
+ isl_space *dim;
+ isl_basic_map *bstep;
+ isl_map *step;
+ unsigned nparam;
+
+ if (!map)
+ return -1;
+
+ dim = isl_map_get_space(map);
+ nparam = isl_space_dim(dim, isl_dim_param);
+ dim = isl_space_drop_dims(dim, isl_dim_in, 0, isl_space_dim(dim, isl_dim_in));
+ dim = isl_space_drop_dims(dim, isl_dim_out, 0, isl_space_dim(dim, isl_dim_out));
+ dim = isl_space_add_dims(dim, isl_dim_in, 1);
+ dim = isl_space_add_dims(dim, isl_dim_out, 1);
+ bstep = isl_basic_map_alloc_space(dim, 0, 1, 0);
+ k = isl_basic_map_alloc_equality(bstep);
+ if (k < 0) {
+ isl_basic_map_free(bstep);
+ return -1;
+ }
+ isl_seq_clr(bstep->eq[k], 1 + isl_basic_map_total_dim(bstep));
+ isl_int_set_si(bstep->eq[k][0], 1);
+ isl_int_set_si(bstep->eq[k][1 + nparam], 1);
+ isl_int_set_si(bstep->eq[k][1 + nparam + 1], -1);
+ bstep = isl_basic_map_finalize(bstep);
+ step = isl_map_from_basic_map(bstep);
+
+ for (i = 0; i < n; ++i)
+ for (j = 0; j < n; ++j)
+ grid[i][j] = isl_map_product(grid[i][j],
+ isl_map_copy(step));
+
+ isl_map_free(step);
+
+ return 0;
+}
+
+/* The core of the Floyd-Warshall algorithm.
+ * Updates the given n x x matrix of relations in place.
+ *
+ * The algorithm iterates over all vertices. In each step, the whole
+ * matrix is updated to include all paths that go to the current vertex,
+ * possibly stay there a while (including passing through earlier vertices)
+ * and then come back. At the start of each iteration, the diagonal
+ * element corresponding to the current vertex is replaced by its
+ * transitive closure to account for all indirect paths that stay
+ * in the current vertex.
+ */
+static void floyd_warshall_iterate(isl_map ***grid, int n, int *exact)
+{
+ int r, p, q;
+
+ for (r = 0; r < n; ++r) {
+ int r_exact;
+ grid[r][r] = isl_map_transitive_closure(grid[r][r],
+ (exact && *exact) ? &r_exact : NULL);
+ if (exact && *exact && !r_exact)
+ *exact = 0;
+
+ for (p = 0; p < n; ++p)
+ for (q = 0; q < n; ++q) {
+ isl_map *loop;
+ if (p == r && q == r)
+ continue;
+ loop = isl_map_apply_range(
+ isl_map_copy(grid[p][r]),
+ isl_map_copy(grid[r][q]));
+ grid[p][q] = isl_map_union(grid[p][q], loop);
+ loop = isl_map_apply_range(
+ isl_map_copy(grid[p][r]),
+ isl_map_apply_range(
+ isl_map_copy(grid[r][r]),
+ isl_map_copy(grid[r][q])));
+ grid[p][q] = isl_map_union(grid[p][q], loop);
+ grid[p][q] = isl_map_coalesce(grid[p][q]);
+ }
+ }
+}
+
+/* Given a partition of the domains and ranges of the basic maps in "map",
+ * apply the Floyd-Warshall algorithm with the elements in the partition
+ * as vertices.
+ *
+ * In particular, there are "n" elements in the partition and "group" is
+ * an array of length 2 * map->n with entries in [0,n-1].
+ *
+ * We first construct a matrix of relations based on the partition information,
+ * apply Floyd-Warshall on this matrix of relations and then take the
+ * union of all entries in the matrix as the final result.
+ *
+ * If we are actually computing the power instead of the transitive closure,
+ * i.e., when "project" is not set, then the result should have the
+ * path lengths encoded as the difference between an extra pair of
+ * coordinates. We therefore apply the nested transitive closures
+ * to relations that include these lengths. In particular, we replace
+ * the input relation by the cross product with the unit length relation
+ * { [i] -> [i + 1] }.
+ */
+static __isl_give isl_map *floyd_warshall_with_groups(__isl_take isl_space *dim,
+ __isl_keep isl_map *map, int *exact, int project, int *group, int n)
+{
+ int i, j, k;
+ isl_map ***grid = NULL;
+ isl_map *app;
+
+ if (!map)
+ goto error;
+
+ if (n == 1) {
+ free(group);
+ return incremental_closure(dim, map, exact, project);
+ }
+
+ grid = isl_calloc_array(map->ctx, isl_map **, n);
+ if (!grid)
+ goto error;
+ for (i = 0; i < n; ++i) {
+ grid[i] = isl_calloc_array(map->ctx, isl_map *, n);
+ if (!grid[i])
+ goto error;
+ for (j = 0; j < n; ++j)
+ grid[i][j] = isl_map_empty(isl_map_get_space(map));
+ }
+
+ for (k = 0; k < map->n; ++k) {
+ i = group[2 * k];
+ j = group[2 * k + 1];
+ grid[i][j] = isl_map_union(grid[i][j],
+ isl_map_from_basic_map(
+ isl_basic_map_copy(map->p[k])));
+ }
+
+ if (!project && add_length(map, grid, n) < 0)
+ goto error;
+
+ floyd_warshall_iterate(grid, n, exact);
+
+ app = isl_map_empty(isl_map_get_space(map));
+
+ for (i = 0; i < n; ++i) {
+ for (j = 0; j < n; ++j)
+ app = isl_map_union(app, grid[i][j]);
+ free(grid[i]);
+ }
+ free(grid);
+
+ free(group);
+ isl_space_free(dim);
+
+ return app;
+error:
+ if (grid)
+ for (i = 0; i < n; ++i) {
+ if (!grid[i])
+ continue;
+ for (j = 0; j < n; ++j)
+ isl_map_free(grid[i][j]);
+ free(grid[i]);
+ }
+ free(grid);
+ free(group);
+ isl_space_free(dim);
+ return NULL;
+}
+
+/* Partition the domains and ranges of the n basic relations in list
+ * into disjoint cells.
+ *
+ * To find the partition, we simply consider all of the domains
+ * and ranges in turn and combine those that overlap.
+ * "set" contains the partition elements and "group" indicates
+ * to which partition element a given domain or range belongs.
+ * The domain of basic map i corresponds to element 2 * i in these arrays,
+ * while the domain corresponds to element 2 * i + 1.
+ * During the construction group[k] is either equal to k,
+ * in which case set[k] contains the union of all the domains and
+ * ranges in the corresponding group, or is equal to some l < k,
+ * with l another domain or range in the same group.
+ */
+static int *setup_groups(isl_ctx *ctx, __isl_keep isl_basic_map **list, int n,
+ isl_set ***set, int *n_group)
+{
+ int i;
+ int *group = NULL;
+ int g;
+
+ *set = isl_calloc_array(ctx, isl_set *, 2 * n);
+ group = isl_alloc_array(ctx, int, 2 * n);
+
+ if (!*set || !group)
+ goto error;
+
+ for (i = 0; i < n; ++i) {
+ isl_set *dom;
+ dom = isl_set_from_basic_set(isl_basic_map_domain(
+ isl_basic_map_copy(list[i])));
+ if (merge(*set, group, dom, 2 * i) < 0)
+ goto error;
+ dom = isl_set_from_basic_set(isl_basic_map_range(
+ isl_basic_map_copy(list[i])));
+ if (merge(*set, group, dom, 2 * i + 1) < 0)
+ goto error;
+ }
+
+ g = 0;
+ for (i = 0; i < 2 * n; ++i)
+ if (group[i] == i) {
+ if (g != i) {
+ (*set)[g] = (*set)[i];
+ (*set)[i] = NULL;
+ }
+ group[i] = g++;
+ } else
+ group[i] = group[group[i]];
+
+ *n_group = g;
+
+ return group;
+error:
+ if (*set) {
+ for (i = 0; i < 2 * n; ++i)
+ isl_set_free((*set)[i]);
+ free(*set);
+ *set = NULL;
+ }
+ free(group);
+ return NULL;
+}
+
+/* Check if the domains and ranges of the basic maps in "map" can
+ * be partitioned, and if so, apply Floyd-Warshall on the elements
+ * of the partition. Note that we also apply this algorithm
+ * if we want to compute the power, i.e., when "project" is not set.
+ * However, the results are unlikely to be exact since the recursive
+ * calls inside the Floyd-Warshall algorithm typically result in
+ * non-linear path lengths quite quickly.
+ */
+static __isl_give isl_map *floyd_warshall(__isl_take isl_space *dim,
+ __isl_keep isl_map *map, int *exact, int project)
+{
+ int i;
+ isl_set **set = NULL;
+ int *group = NULL;
+ int n;
+
+ if (!map)
+ goto error;
+ if (map->n <= 1)
+ return incremental_closure(dim, map, exact, project);
+
+ group = setup_groups(map->ctx, map->p, map->n, &set, &n);
+ if (!group)
+ goto error;
+
+ for (i = 0; i < 2 * map->n; ++i)
+ isl_set_free(set[i]);
+
+ free(set);
+
+ return floyd_warshall_with_groups(dim, map, exact, project, group, n);
+error:
+ isl_space_free(dim);
+ return NULL;
+}
+
+/* Structure for representing the nodes of the graph of which
+ * strongly connected components are being computed.
+ *
+ * list contains the actual nodes
+ * check_closed is set if we may have used the fact that
+ * a pair of basic maps can be interchanged
+ */
+struct isl_tc_follows_data {
+ isl_basic_map **list;
+ int check_closed;
+};
+
+/* Check whether in the computation of the transitive closure
+ * "list[i]" (R_1) should follow (or be part of the same component as)
+ * "list[j]" (R_2).
+ *
+ * That is check whether
+ *
+ * R_1 \circ R_2
+ *
+ * is a subset of
+ *
+ * R_2 \circ R_1
+ *
+ * If so, then there is no reason for R_1 to immediately follow R_2
+ * in any path.
+ *
+ * *check_closed is set if the subset relation holds while
+ * R_1 \circ R_2 is not empty.
+ */
+static int basic_map_follows(int i, int j, void *user)
+{
+ struct isl_tc_follows_data *data = user;
+ struct isl_map *map12 = NULL;
+ struct isl_map *map21 = NULL;
+ int subset;
+
+ if (!isl_space_tuple_match(data->list[i]->dim, isl_dim_in,
+ data->list[j]->dim, isl_dim_out))
+ return 0;
+
+ map21 = isl_map_from_basic_map(
+ isl_basic_map_apply_range(
+ isl_basic_map_copy(data->list[j]),
+ isl_basic_map_copy(data->list[i])));
+ subset = isl_map_is_empty(map21);
+ if (subset < 0)
+ goto error;
+ if (subset) {
+ isl_map_free(map21);
+ return 0;
+ }
+
+ if (!isl_space_tuple_match(data->list[i]->dim, isl_dim_in,
+ data->list[i]->dim, isl_dim_out) ||
+ !isl_space_tuple_match(data->list[j]->dim, isl_dim_in,
+ data->list[j]->dim, isl_dim_out)) {
+ isl_map_free(map21);
+ return 1;
+ }
+
+ map12 = isl_map_from_basic_map(
+ isl_basic_map_apply_range(
+ isl_basic_map_copy(data->list[i]),
+ isl_basic_map_copy(data->list[j])));
+
+ subset = isl_map_is_subset(map21, map12);
+
+ isl_map_free(map12);
+ isl_map_free(map21);
+
+ if (subset)
+ data->check_closed = 1;
+
+ return subset < 0 ? -1 : !subset;
+error:
+ isl_map_free(map21);
+ return -1;
+}
+
+/* Given a union of basic maps R = \cup_i R_i \subseteq D \times D
+ * and a dimension specification (Z^{n+1} -> Z^{n+1}),
+ * construct a map that is an overapproximation of the map
+ * that takes an element from the dom R \times Z to an
+ * element from ran R \times Z, such that the first n coordinates of the
+ * difference between them is a sum of differences between images
+ * and pre-images in one of the R_i and such that the last coordinate
+ * is equal to the number of steps taken.
+ * If "project" is set, then these final coordinates are not included,
+ * i.e., a relation of type Z^n -> Z^n is returned.
+ * That is, let
+ *
+ * \Delta_i = { y - x | (x, y) in R_i }
+ *
+ * then the constructed map is an overapproximation of
+ *
+ * { (x) -> (x + d) | \exists k_i >= 0, \delta_i \in \Delta_i :
+ * d = (\sum_i k_i \delta_i, \sum_i k_i) and
+ * x in dom R and x + d in ran R }
+ *
+ * or
+ *
+ * { (x) -> (x + d) | \exists k_i >= 0, \delta_i \in \Delta_i :
+ * d = (\sum_i k_i \delta_i) and
+ * x in dom R and x + d in ran R }
+ *
+ * if "project" is set.
+ *
+ * We first split the map into strongly connected components, perform
+ * the above on each component and then join the results in the correct
+ * order, at each join also taking in the union of both arguments
+ * to allow for paths that do not go through one of the two arguments.
+ */
+static __isl_give isl_map *construct_power_components(__isl_take isl_space *dim,
+ __isl_keep isl_map *map, int *exact, int project)
+{
+ int i, n, c;
+ struct isl_map *path = NULL;
+ struct isl_tc_follows_data data;
+ struct isl_tarjan_graph *g = NULL;
+ int *orig_exact;
+ int local_exact;
+
+ if (!map)
+ goto error;
+ if (map->n <= 1)
+ return floyd_warshall(dim, map, exact, project);
+
+ data.list = map->p;
+ data.check_closed = 0;
+ g = isl_tarjan_graph_init(map->ctx, map->n, &basic_map_follows, &data);
+ if (!g)
+ goto error;
+
+ orig_exact = exact;
+ if (data.check_closed && !exact)
+ exact = &local_exact;
+
+ c = 0;
+ i = 0;
+ n = map->n;
+ if (project)
+ path = isl_map_empty(isl_map_get_space(map));
+ else
+ path = isl_map_empty(isl_space_copy(dim));
+ path = anonymize(path);
+ while (n) {
+ struct isl_map *comp;
+ isl_map *path_comp, *path_comb;
+ comp = isl_map_alloc_space(isl_map_get_space(map), n, 0);
+ while (g->order[i] != -1) {
+ comp = isl_map_add_basic_map(comp,
+ isl_basic_map_copy(map->p[g->order[i]]));
+ --n;
+ ++i;
+ }
+ path_comp = floyd_warshall(isl_space_copy(dim),
+ comp, exact, project);
+ path_comp = anonymize(path_comp);
+ path_comb = isl_map_apply_range(isl_map_copy(path),
+ isl_map_copy(path_comp));
+ path = isl_map_union(path, path_comp);
+ path = isl_map_union(path, path_comb);
+ isl_map_free(comp);
+ ++i;
+ ++c;
+ }
+
+ if (c > 1 && data.check_closed && !*exact) {
+ int closed;
+
+ closed = isl_map_is_transitively_closed(path);
+ if (closed < 0)
+ goto error;
+ if (!closed) {
+ isl_tarjan_graph_free(g);
+ isl_map_free(path);
+ return floyd_warshall(dim, map, orig_exact, project);
+ }
+ }
+
+ isl_tarjan_graph_free(g);
+ isl_space_free(dim);
+
+ return path;
+error:
+ isl_tarjan_graph_free(g);
+ isl_space_free(dim);
+ isl_map_free(path);
+ return NULL;
+}
+
+/* Given a union of basic maps R = \cup_i R_i \subseteq D \times D,
+ * construct a map that is an overapproximation of the map
+ * that takes an element from the space D to another
+ * element from the same space, such that the difference between
+ * them is a strictly positive sum of differences between images
+ * and pre-images in one of the R_i.
+ * The number of differences in the sum is equated to parameter "param".
+ * That is, let
+ *
+ * \Delta_i = { y - x | (x, y) in R_i }
+ *
+ * then the constructed map is an overapproximation of
+ *
+ * { (x) -> (x + d) | \exists k_i >= 0, \delta_i \in \Delta_i :
+ * d = \sum_i k_i \delta_i and k = \sum_i k_i > 0 }
+ * or
+ *
+ * { (x) -> (x + d) | \exists k_i >= 0, \delta_i \in \Delta_i :
+ * d = \sum_i k_i \delta_i and \sum_i k_i > 0 }
+ *
+ * if "project" is set.
+ *
+ * If "project" is not set, then
+ * we construct an extended mapping with an extra coordinate
+ * that indicates the number of steps taken. In particular,
+ * the difference in the last coordinate is equal to the number
+ * of steps taken to move from a domain element to the corresponding
+ * image element(s).
+ */
+static __isl_give isl_map *construct_power(__isl_keep isl_map *map,
+ int *exact, int project)
+{
+ struct isl_map *app = NULL;
+ isl_space *dim = NULL;
+ unsigned d;
+
+ if (!map)
+ return NULL;
+
+ dim = isl_map_get_space(map);
+
+ d = isl_space_dim(dim, isl_dim_in);
+ dim = isl_space_add_dims(dim, isl_dim_in, 1);
+ dim = isl_space_add_dims(dim, isl_dim_out, 1);
+
+ app = construct_power_components(isl_space_copy(dim), map,
+ exact, project);
+
+ isl_space_free(dim);
+
+ return app;
+}
+
+/* Compute the positive powers of "map", or an overapproximation.
+ * If the result is exact, then *exact is set to 1.
+ *
+ * If project is set, then we are actually interested in the transitive
+ * closure, so we can use a more relaxed exactness check.
+ * The lengths of the paths are also projected out instead of being
+ * encoded as the difference between an extra pair of final coordinates.
+ */
+static __isl_give isl_map *map_power(__isl_take isl_map *map,
+ int *exact, int project)
+{
+ struct isl_map *app = NULL;
+
+ if (exact)
+ *exact = 1;
+
+ if (!map)
+ return NULL;
+
+ isl_assert(map->ctx,
+ isl_map_dim(map, isl_dim_in) == isl_map_dim(map, isl_dim_out),
+ goto error);
+
+ app = construct_power(map, exact, project);
+
+ isl_map_free(map);
+ return app;
+error:
+ isl_map_free(map);
+ isl_map_free(app);
+ return NULL;
+}
+
+/* Compute the positive powers of "map", or an overapproximation.
+ * The result maps the exponent to a nested copy of the corresponding power.
+ * If the result is exact, then *exact is set to 1.
+ * map_power constructs an extended relation with the path lengths
+ * encoded as the difference between the final coordinates.
+ * In the final step, this difference is equated to an extra parameter
+ * and made positive. The extra coordinates are subsequently projected out
+ * and the parameter is turned into the domain of the result.
+ */
+__isl_give isl_map *isl_map_power(__isl_take isl_map *map, int *exact)
+{
+ isl_space *target_dim;
+ isl_space *dim;
+ isl_map *diff;
+ unsigned d;
+ unsigned param;
+
+ if (!map)
+ return NULL;
+
+ d = isl_map_dim(map, isl_dim_in);
+ param = isl_map_dim(map, isl_dim_param);
+
+ map = isl_map_compute_divs(map);
+ map = isl_map_coalesce(map);
+
+ if (isl_map_plain_is_empty(map)) {
+ map = isl_map_from_range(isl_map_wrap(map));
+ map = isl_map_add_dims(map, isl_dim_in, 1);
+ map = isl_map_set_dim_name(map, isl_dim_in, 0, "k");
+ return map;
+ }
+
+ target_dim = isl_map_get_space(map);
+ target_dim = isl_space_from_range(isl_space_wrap(target_dim));
+ target_dim = isl_space_add_dims(target_dim, isl_dim_in, 1);
+ target_dim = isl_space_set_dim_name(target_dim, isl_dim_in, 0, "k");
+
+ map = map_power(map, exact, 0);
+
+ map = isl_map_add_dims(map, isl_dim_param, 1);
+ dim = isl_map_get_space(map);
+ diff = equate_parameter_to_length(dim, param);
+ map = isl_map_intersect(map, diff);
+ map = isl_map_project_out(map, isl_dim_in, d, 1);
+ map = isl_map_project_out(map, isl_dim_out, d, 1);
+ map = isl_map_from_range(isl_map_wrap(map));
+ map = isl_map_move_dims(map, isl_dim_in, 0, isl_dim_param, param, 1);
+
+ map = isl_map_reset_space(map, target_dim);
+
+ return map;
+}
+
+/* Compute a relation that maps each element in the range of the input
+ * relation to the lengths of all paths composed of edges in the input
+ * relation that end up in the given range element.
+ * The result may be an overapproximation, in which case *exact is set to 0.
+ * The resulting relation is very similar to the power relation.
+ * The difference are that the domain has been projected out, the
+ * range has become the domain and the exponent is the range instead
+ * of a parameter.
+ */
+__isl_give isl_map *isl_map_reaching_path_lengths(__isl_take isl_map *map,
+ int *exact)
+{
+ isl_space *dim;
+ isl_map *diff;
+ unsigned d;
+ unsigned param;
+
+ if (!map)
+ return NULL;
+
+ d = isl_map_dim(map, isl_dim_in);
+ param = isl_map_dim(map, isl_dim_param);
+
+ map = isl_map_compute_divs(map);
+ map = isl_map_coalesce(map);
+
+ if (isl_map_plain_is_empty(map)) {
+ if (exact)
+ *exact = 1;
+ map = isl_map_project_out(map, isl_dim_out, 0, d);
+ map = isl_map_add_dims(map, isl_dim_out, 1);
+ return map;
+ }
+
+ map = map_power(map, exact, 0);
+
+ map = isl_map_add_dims(map, isl_dim_param, 1);
+ dim = isl_map_get_space(map);
+ diff = equate_parameter_to_length(dim, param);
+ map = isl_map_intersect(map, diff);
+ map = isl_map_project_out(map, isl_dim_in, 0, d + 1);
+ map = isl_map_project_out(map, isl_dim_out, d, 1);
+ map = isl_map_reverse(map);
+ map = isl_map_move_dims(map, isl_dim_out, 0, isl_dim_param, param, 1);
+
+ return map;
+}
+
+/* Check whether equality i of bset is a pure stride constraint
+ * on a single dimensions, i.e., of the form
+ *
+ * v = k e
+ *
+ * with k a constant and e an existentially quantified variable.
+ */
+static int is_eq_stride(__isl_keep isl_basic_set *bset, int i)
+{
+ unsigned nparam;
+ unsigned d;
+ unsigned n_div;
+ int pos1;
+ int pos2;
+
+ if (!bset)
+ return -1;
+
+ if (!isl_int_is_zero(bset->eq[i][0]))
+ return 0;
+
+ nparam = isl_basic_set_dim(bset, isl_dim_param);
+ d = isl_basic_set_dim(bset, isl_dim_set);
+ n_div = isl_basic_set_dim(bset, isl_dim_div);
+
+ if (isl_seq_first_non_zero(bset->eq[i] + 1, nparam) != -1)
+ return 0;
+ pos1 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam, d);
+ if (pos1 == -1)
+ return 0;
+ if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + pos1 + 1,
+ d - pos1 - 1) != -1)
+ return 0;
+
+ pos2 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d, n_div);
+ if (pos2 == -1)
+ return 0;
+ if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d + pos2 + 1,
+ n_div - pos2 - 1) != -1)
+ return 0;
+ if (!isl_int_is_one(bset->eq[i][1 + nparam + pos1]) &&
+ !isl_int_is_negone(bset->eq[i][1 + nparam + pos1]))
+ return 0;
+
+ return 1;
+}
+
+/* Given a map, compute the smallest superset of this map that is of the form
+ *
+ * { i -> j : L <= j - i <= U and exists a_p: j_p - i_p = M_p a_p }
+ *
+ * (where p ranges over the (non-parametric) dimensions),
+ * compute the transitive closure of this map, i.e.,
+ *
+ * { i -> j : exists k > 0:
+ * k L <= j - i <= k U and exists a: j_p - i_p = M_p a_p }
+ *
+ * and intersect domain and range of this transitive closure with
+ * the given domain and range.
+ *
+ * If with_id is set, then try to include as much of the identity mapping
+ * as possible, by computing
+ *
+ * { i -> j : exists k >= 0:
+ * k L <= j - i <= k U and exists a: j_p - i_p = M_p a_p }
+ *
+ * instead (i.e., allow k = 0).
+ *
+ * In practice, we compute the difference set
+ *
+ * delta = { j - i | i -> j in map },
+ *
+ * look for stride constraint on the individual dimensions and compute
+ * (constant) lower and upper bounds for each individual dimension,
+ * adding a constraint for each bound not equal to infinity.
+ */
+static __isl_give isl_map *box_closure_on_domain(__isl_take isl_map *map,
+ __isl_take isl_set *dom, __isl_take isl_set *ran, int with_id)
+{
+ int i;
+ int k;
+ unsigned d;
+ unsigned nparam;
+ unsigned total;
+ isl_space *dim;
+ isl_set *delta;
+ isl_map *app = NULL;
+ isl_basic_set *aff = NULL;
+ isl_basic_map *bmap = NULL;
+ isl_vec *obj = NULL;
+ isl_int opt;
+
+ isl_int_init(opt);
+
+ delta = isl_map_deltas(isl_map_copy(map));
+
+ aff = isl_set_affine_hull(isl_set_copy(delta));
+ if (!aff)
+ goto error;
+ dim = isl_map_get_space(map);
+ d = isl_space_dim(dim, isl_dim_in);
+ nparam = isl_space_dim(dim, isl_dim_param);
+ total = isl_space_dim(dim, isl_dim_all);
+ bmap = isl_basic_map_alloc_space(dim,
+ aff->n_div + 1, aff->n_div, 2 * d + 1);
+ for (i = 0; i < aff->n_div + 1; ++i) {
+ k = isl_basic_map_alloc_div(bmap);
+ if (k < 0)
+ goto error;
+ isl_int_set_si(bmap->div[k][0], 0);
+ }
+ for (i = 0; i < aff->n_eq; ++i) {
+ if (!is_eq_stride(aff, i))
+ continue;
+ k = isl_basic_map_alloc_equality(bmap);
+ if (k < 0)
+ goto error;
+ isl_seq_clr(bmap->eq[k], 1 + nparam);
+ isl_seq_cpy(bmap->eq[k] + 1 + nparam + d,
+ aff->eq[i] + 1 + nparam, d);
+ isl_seq_neg(bmap->eq[k] + 1 + nparam,
+ aff->eq[i] + 1 + nparam, d);
+ isl_seq_cpy(bmap->eq[k] + 1 + nparam + 2 * d,
+ aff->eq[i] + 1 + nparam + d, aff->n_div);
+ isl_int_set_si(bmap->eq[k][1 + total + aff->n_div], 0);
+ }
+ obj = isl_vec_alloc(map->ctx, 1 + nparam + d);
+ if (!obj)
+ goto error;
+ isl_seq_clr(obj->el, 1 + nparam + d);
+ for (i = 0; i < d; ++ i) {
+ enum isl_lp_result res;
+
+ isl_int_set_si(obj->el[1 + nparam + i], 1);
+
+ res = isl_set_solve_lp(delta, 0, obj->el, map->ctx->one, &opt,
+ NULL, NULL);
+ if (res == isl_lp_error)
+ goto error;
+ if (res == isl_lp_ok) {
+ k = isl_basic_map_alloc_inequality(bmap);
+ if (k < 0)
+ goto error;
+ isl_seq_clr(bmap->ineq[k],
+ 1 + nparam + 2 * d + bmap->n_div);
+ isl_int_set_si(bmap->ineq[k][1 + nparam + i], -1);
+ isl_int_set_si(bmap->ineq[k][1 + nparam + d + i], 1);
+ isl_int_neg(bmap->ineq[k][1 + nparam + 2 * d + aff->n_div], opt);
+ }
+
+ res = isl_set_solve_lp(delta, 1, obj->el, map->ctx->one, &opt,
+ NULL, NULL);
+ if (res == isl_lp_error)
+ goto error;
+ if (res == isl_lp_ok) {
+ k = isl_basic_map_alloc_inequality(bmap);
+ if (k < 0)
+ goto error;
+ isl_seq_clr(bmap->ineq[k],
+ 1 + nparam + 2 * d + bmap->n_div);
+ isl_int_set_si(bmap->ineq[k][1 + nparam + i], 1);
+ isl_int_set_si(bmap->ineq[k][1 + nparam + d + i], -1);
+ isl_int_set(bmap->ineq[k][1 + nparam + 2 * d + aff->n_div], opt);
+ }
+
+ isl_int_set_si(obj->el[1 + nparam + i], 0);
+ }
+ k = isl_basic_map_alloc_inequality(bmap);
+ if (k < 0)
+ goto error;
+ isl_seq_clr(bmap->ineq[k],
+ 1 + nparam + 2 * d + bmap->n_div);
+ if (!with_id)
+ isl_int_set_si(bmap->ineq[k][0], -1);
+ isl_int_set_si(bmap->ineq[k][1 + nparam + 2 * d + aff->n_div], 1);
+
+ app = isl_map_from_domain_and_range(dom, ran);
+
+ isl_vec_free(obj);
+ isl_basic_set_free(aff);
+ isl_map_free(map);
+ bmap = isl_basic_map_finalize(bmap);
+ isl_set_free(delta);
+ isl_int_clear(opt);
+
+ map = isl_map_from_basic_map(bmap);
+ map = isl_map_intersect(map, app);
+
+ return map;
+error:
+ isl_vec_free(obj);
+ isl_basic_map_free(bmap);
+ isl_basic_set_free(aff);
+ isl_set_free(dom);
+ isl_set_free(ran);
+ isl_map_free(map);
+ isl_set_free(delta);
+ isl_int_clear(opt);
+ return NULL;
+}
+
+/* Given a map, compute the smallest superset of this map that is of the form
+ *
+ * { i -> j : L <= j - i <= U and exists a_p: j_p - i_p = M_p a_p }
+ *
+ * (where p ranges over the (non-parametric) dimensions),
+ * compute the transitive closure of this map, i.e.,
+ *
+ * { i -> j : exists k > 0:
+ * k L <= j - i <= k U and exists a: j_p - i_p = M_p a_p }
+ *
+ * and intersect domain and range of this transitive closure with
+ * domain and range of the original map.
+ */
+static __isl_give isl_map *box_closure(__isl_take isl_map *map)
+{
+ isl_set *domain;
+ isl_set *range;
+
+ domain = isl_map_domain(isl_map_copy(map));
+ domain = isl_set_coalesce(domain);
+ range = isl_map_range(isl_map_copy(map));
+ range = isl_set_coalesce(range);