hide some functions that were exposed by mistake
[platform/upstream/isl.git] / isl_transitive_closure.c
index 4d20c07..bfe9990 100644 (file)
 #include "isl_map.h"
 #include "isl_map_private.h"
 #include "isl_seq.h"
+#include <isl_dim_private.h>
 #include <isl_lp.h>
+#include <isl_union_map.h>
+#include <isl_mat_private.h>
+
+int isl_map_is_transitively_closed(__isl_keep isl_map *map)
+{
+       isl_map *map2;
+       int closed;
+
+       map2 = isl_map_apply_range(isl_map_copy(map), isl_map_copy(map));
+       closed = isl_map_is_subset(map2, map);
+       isl_map_free(map2);
+
+       return closed;
+}
+
+int isl_union_map_is_transitively_closed(__isl_keep isl_union_map *umap)
+{
+       isl_union_map *umap2;
+       int closed;
+
+       umap2 = isl_union_map_apply_range(isl_union_map_copy(umap),
+                                         isl_union_map_copy(umap));
+       closed = isl_union_map_is_subset(umap2, umap);
+       isl_union_map_free(umap2);
+
+       return closed;
+}
  
 /* Given a map that represents a path with the length of the path
  * encoded as the difference between the last output coordindate
@@ -84,8 +112,8 @@ static int check_power_exactness(__isl_take isl_map *map,
        isl_map *app_1;
        isl_map *app_2;
 
-       map = isl_map_add(map, isl_dim_in, 1);
-       map = isl_map_add(map, isl_dim_out, 1);
+       map = isl_map_add_dims(map, isl_dim_in, 1);
+       map = isl_map_add_dims(map, isl_dim_out, 1);
        map = set_path_length(map, 1, 1);
 
        app_1 = set_path_length(isl_map_copy(app), 1, 1);
@@ -147,6 +175,8 @@ static int check_exactness(__isl_take isl_map *map, __isl_take isl_map *app,
        app = isl_map_project_out(app, isl_dim_in, d, 1);
        app = isl_map_project_out(app, isl_dim_out, d, 1);
 
+       app = isl_map_reset_dim(app, isl_map_get_dim(map));
+
        test = isl_map_apply_range(isl_map_copy(map), isl_map_copy(app));
        test = isl_map_union(test, isl_map_copy(map));
 
@@ -158,10 +188,6 @@ static int check_exactness(__isl_take isl_map *map, __isl_take isl_map *app,
        isl_map_free(map);
 
        return exact;
-error:
-       isl_map_free(app);
-       isl_map_free(map);
-       return -1;
 }
 
 /*
@@ -249,6 +275,47 @@ error:
 #define PURE_VAR       2
 #define MIXED          3
 
+/* Check whether the parametric constant term of constraint c is never
+ * positive in "bset".
+ */
+static int parametric_constant_never_positive(__isl_keep isl_basic_set *bset,
+       isl_int *c, int *div_purity)
+{
+       unsigned d;
+       unsigned n_div;
+       unsigned nparam;
+       int i;
+       int k;
+       int empty;
+
+       n_div = isl_basic_set_dim(bset, isl_dim_div);
+       d = isl_basic_set_dim(bset, isl_dim_set);
+       nparam = isl_basic_set_dim(bset, isl_dim_param);
+
+       bset = isl_basic_set_copy(bset);
+       bset = isl_basic_set_cow(bset);
+       bset = isl_basic_set_extend_constraints(bset, 0, 1);
+       k = isl_basic_set_alloc_inequality(bset);
+       if (k < 0)
+               goto error;
+       isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
+       isl_seq_cpy(bset->ineq[k], c, 1 + nparam);
+       for (i = 0; i < n_div; ++i) {
+               if (div_purity[i] != PURE_PARAM)
+                       continue;
+               isl_int_set(bset->ineq[k][1 + nparam + d + i],
+                           c[1 + nparam + d + i]);
+       }
+       isl_int_sub_ui(bset->ineq[k][0], bset->ineq[k][0], 1);
+       empty = isl_basic_set_is_empty(bset);
+       isl_basic_set_free(bset);
+
+       return empty;
+error:
+       isl_basic_set_free(bset);
+       return -1;
+}
+
 /* Return PURE_PARAM if only the coefficients of the parameters are non-zero.
  * Return PURE_VAR if only the coefficients of the set variables are non-zero.
  * Return MIXED if only the coefficients of the parameters and the set
@@ -262,7 +329,6 @@ static int purity(__isl_keep isl_basic_set *bset, isl_int *c, int *div_purity,
        unsigned d;
        unsigned n_div;
        unsigned nparam;
-       int k;
        int empty;
        int i;
        int p = 0, v = 0;
@@ -284,31 +350,14 @@ static int purity(__isl_keep isl_basic_set *bset, isl_int *c, int *div_purity,
                return PURE_VAR;
        if (!v && isl_seq_first_non_zero(c + 1 + nparam, d) == -1)
                return PURE_PARAM;
-       if (eq)
-               return IMPURE;
 
-       bset = isl_basic_set_copy(bset);
-       bset = isl_basic_set_cow(bset);
-       bset = isl_basic_set_extend_constraints(bset, 0, 1);
-       k = isl_basic_set_alloc_inequality(bset);
-       if (k < 0)
-               goto error;
-       isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
-       isl_seq_cpy(bset->ineq[k], c, 1 + nparam);
-       for (i = 0; i < n_div; ++i) {
-               if (div_purity[i] != PURE_PARAM)
-                       continue;
-               isl_int_set(bset->ineq[k][1 + nparam + d + i],
-                           c[1 + nparam + d + i]);
+       empty = parametric_constant_never_positive(bset, c, div_purity);
+       if (eq && empty >= 0 && !empty) {
+               isl_seq_neg(c, c, 1 + nparam + d + n_div);
+               empty = parametric_constant_never_positive(bset, c, div_purity);
        }
-       isl_int_sub_ui(bset->ineq[k][0], bset->ineq[k][0], 1);
-       empty = isl_basic_set_is_empty(bset);
-       isl_basic_set_free(bset);
 
        return empty < 0 ? -1 : empty ? MIXED : IMPURE;
-error:
-       isl_basic_set_free(bset);
-       return -1;
 }
 
 /* Return an array of integers indicating the type of each div in bset.
@@ -366,7 +415,7 @@ static __isl_give int *get_div_purity(__isl_keep isl_basic_set *bset)
  * check if setting the length to zero results in only the identity
  * mapping.
  */
-int empty_path_is_identity(__isl_keep isl_basic_map *path, unsigned pos)
+static int empty_path_is_identity(__isl_keep isl_basic_map *path, unsigned pos)
 {
        isl_basic_map *test = NULL;
        isl_basic_map *id = NULL;
@@ -381,7 +430,7 @@ int empty_path_is_identity(__isl_keep isl_basic_map *path, unsigned pos)
        isl_seq_clr(test->eq[k], 1 + isl_basic_map_total_dim(test));
        isl_int_set_si(test->eq[k][pos], 1);
        id = isl_basic_map_identity(isl_dim_domain(isl_basic_map_get_dim(path)));
-       is_id = isl_basic_map_is_subset(test, id);
+       is_id = isl_basic_map_is_equal(test, id);
        isl_basic_map_free(test);
        isl_basic_map_free(id);
        return is_id;
@@ -390,43 +439,47 @@ error:
        return -1;
 }
 
-__isl_give isl_basic_map *add_delta_constraints(__isl_take isl_basic_map *path,
+static __isl_give isl_basic_map *add_delta_constraints(
+       __isl_take isl_basic_map *path,
        __isl_keep isl_basic_set *delta, unsigned off, unsigned nparam,
        unsigned d, int *div_purity, int eq)
 {
        int i, k;
        int n = eq ? delta->n_eq : delta->n_ineq;
        isl_int **delta_c = eq ? delta->eq : delta->ineq;
-       isl_int **path_c = eq ? path->eq : path->ineq;
        unsigned n_div;
 
        n_div = isl_basic_set_dim(delta, isl_dim_div);
 
        for (i = 0; i < n; ++i) {
+               isl_int *path_c;
                int p = purity(delta, delta_c[i], div_purity, eq);
                if (p < 0)
                        goto error;
                if (p == IMPURE)
                        continue;
-               if (eq)
+               if (eq && p != MIXED) {
                        k = isl_basic_map_alloc_equality(path);
-               else
+                       path_c = path->eq[k];
+               } else {
                        k = isl_basic_map_alloc_inequality(path);
+                       path_c = path->ineq[k];
+               }
                if (k < 0)
                        goto error;
-               isl_seq_clr(path_c[k], 1 + isl_basic_map_total_dim(path));
+               isl_seq_clr(path_c, 1 + isl_basic_map_total_dim(path));
                if (p == PURE_VAR) {
-                       isl_seq_cpy(path_c[k] + off,
+                       isl_seq_cpy(path_c + off,
                                    delta_c[i] + 1 + nparam, d);
-                       isl_int_set(path_c[k][off + d], delta_c[i][0]);
+                       isl_int_set(path_c[off + d], delta_c[i][0]);
                } else if (p == PURE_PARAM) {
-                       isl_seq_cpy(path_c[k], delta_c[i], 1 + nparam);
+                       isl_seq_cpy(path_c, delta_c[i], 1 + nparam);
                } else {
-                       isl_seq_cpy(path_c[k] + off,
+                       isl_seq_cpy(path_c + off,
                                    delta_c[i] + 1 + nparam, d);
-                       isl_seq_cpy(path_c[k], delta_c[i], 1 + nparam);
+                       isl_seq_cpy(path_c, delta_c[i], 1 + nparam);
                }
-               isl_seq_cpy(path_c[k] + off - n_div,
+               isl_seq_cpy(path_c + off - n_div,
                            delta_c[i] + 1 + nparam + d, n_div);
        }
 
@@ -498,7 +551,7 @@ static __isl_give isl_map *path_along_delta(__isl_take isl_dim *dim,
        d = isl_basic_set_dim(delta, isl_dim_set);
        nparam = isl_basic_set_dim(delta, isl_dim_param);
        path = isl_basic_map_alloc_dim(isl_dim_copy(dim), n_div + d + 1,
-                       d + 1 + delta->n_eq, delta->n_ineq + 1);
+                       d + 1 + delta->n_eq, delta->n_eq + delta->n_ineq + 1);
        off = 1 + nparam + 2 * (d + 1) + n_div;
 
        for (i = 0; i < n_div + d + 1; ++i) {
@@ -554,7 +607,7 @@ error:
        return NULL;
 }
 
-/* Given a dimenion specification Z^{n+1} -> Z^{n+1} and a parameter "param",
+/* Given a dimension specification Z^{n+1} -> Z^{n+1} and a parameter "param",
  * construct a map that equates the parameter to the difference
  * in the final coordinates and imposes that this difference is positive.
  * That is, construct
@@ -718,10 +771,24 @@ error:
        return NULL;
 }
 
+static int isl_set_overlaps(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
+{
+       isl_set *i;
+       int no_overlap;
+
+       if (!isl_dim_tuple_match(set1->dim, isl_dim_set, set2->dim, isl_dim_set))
+               return 0;
+
+       i = isl_set_intersect(isl_set_copy(set1), isl_set_copy(set2));
+       no_overlap = isl_set_is_empty(i);
+       isl_set_free(i);
+
+       return no_overlap < 0 ? -1 : !no_overlap;
+}
+
 /* 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 the union of the identity map and
- * an overapproximation of the map
+ * 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
@@ -735,15 +802,14 @@ error:
  *
  *     { (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 } union
- *     { (x) -> (x) }
+ *                             x in dom R and x + d in ran R and
+ *                             \sum_i k_i >= 1 }
  */
 static __isl_give isl_map *construct_component(__isl_take isl_dim *dim,
        __isl_keep isl_map *map, int *exact, int project)
 {
        struct isl_set *domain = NULL;
        struct isl_set *range = NULL;
-       struct isl_set *overlap;
        struct isl_map *app = NULL;
        struct isl_map *path = NULL;
 
@@ -751,23 +817,20 @@ static __isl_give isl_map *construct_component(__isl_take isl_dim *dim,
        domain = isl_set_coalesce(domain);
        range = isl_map_range(isl_map_copy(map));
        range = isl_set_coalesce(range);
-       overlap = isl_set_intersect(isl_set_copy(domain), isl_set_copy(range));
-       if (isl_set_is_empty(overlap) == 1) {
+       if (!isl_set_overlaps(domain, range)) {
                isl_set_free(domain);
                isl_set_free(range);
-               isl_set_free(overlap);
+               isl_dim_free(dim);
 
                map = isl_map_copy(map);
-               map = isl_map_add(map, isl_dim_in, 1);
-               map = isl_map_add(map, isl_dim_out, 1);
+               map = isl_map_add_dims(map, isl_dim_in, 1);
+               map = isl_map_add_dims(map, isl_dim_out, 1);
                map = set_path_length(map, 1, 1);
-               map = isl_map_union(map, isl_map_identity(isl_dim_domain(dim)));
                return map;
        }
-       isl_set_free(overlap);
        app = isl_map_from_domain_and_range(domain, range);
-       app = isl_map_add(app, isl_dim_in, 1);
-       app = isl_map_add(app, isl_dim_out, 1);
+       app = isl_map_add_dims(app, isl_dim_in, 1);
+       app = isl_map_add_dims(app, isl_dim_out, 1);
 
        path = construct_extended_path(isl_dim_copy(dim), map,
                                        exact && *exact ? &project : NULL);
@@ -778,197 +841,1074 @@ static __isl_give isl_map *construct_component(__isl_take isl_dim *dim,
                                      project)) < 0)
                goto error;
 
-       return isl_map_union(app, isl_map_identity(isl_dim_domain(dim)));
+       isl_dim_free(dim);
+       app = set_path_length(app, 0, 1);
+       return app;
 error:
        isl_dim_free(dim);
        isl_map_free(app);
        return NULL;
 }
 
-/* Structure for representing the nodes in the graph being traversed
- * using Tarjan's algorithm.
- * index represents the order in which nodes are visited.
- * min_index is the index of the root of a (sub)component.
- * on_stack indicates whether the node is currently on the stack.
- */
-struct basic_map_sort_node {
-       int index;
-       int min_index;
-       int on_stack;
-};
-/* Structure for representing the graph being traversed
- * using Tarjan's algorithm.
- * len is the number of nodes
- * node is an array of nodes
- * stack contains the nodes on the path from the root to the current node
- * sp is the stack pointer
- * index is the index of the last node visited
- * order contains the elements of the components separated by -1
- * op represents the current position in order
+/* Call construct_component and, if "project" is set, project out
+ * the final coordinates.
  */
-struct basic_map_sort {
-       int len;
-       struct basic_map_sort_node *node;
-       int *stack;
-       int sp;
-       int index;
-       int *order;
-       int op;
-};
-
-static void basic_map_sort_free(struct basic_map_sort *s)
+static __isl_give isl_map *construct_projected_component(
+       __isl_take isl_dim *dim,
+       __isl_keep isl_map *map, int *exact, int project)
 {
-       if (!s)
-               return;
-       free(s->node);
-       free(s->stack);
-       free(s->order);
-       free(s);
+       isl_map *app;
+       unsigned d;
+
+       if (!dim)
+               return NULL;
+       d = isl_dim_size(dim, isl_dim_in);
+
+       app = construct_component(dim, map, exact, project);
+       if (project) {
+               app = isl_map_project_out(app, isl_dim_in, d - 1, 1);
+               app = isl_map_project_out(app, isl_dim_out, d - 1, 1);
+       }
+       return app;
 }
 
-static struct basic_map_sort *basic_map_sort_alloc(struct isl_ctx *ctx, int len)
+/* Compute an extended version, i.e., with path lengths, of
+ * an overapproximation of the transitive closure of "bmap"
+ * with path lengths greater than or equal to zero and with
+ * domain and range equal to "dom".
+ */
+static __isl_give isl_map *q_closure(__isl_take isl_dim *dim,
+       __isl_take isl_set *dom, __isl_keep isl_basic_map *bmap, int *exact)
 {
-       struct basic_map_sort *s;
-       int i;
+       int project = 1;
+       isl_map *path;
+       isl_map *map;
+       isl_map *app;
 
-       s = isl_calloc_type(ctx, struct basic_map_sort);
-       if (!s)
-               return NULL;
-       s->len = len;
-       s->node = isl_alloc_array(ctx, struct basic_map_sort_node, len);
-       if (!s->node)
-               goto error;
-       for (i = 0; i < len; ++i)
-               s->node[i].index = -1;
-       s->stack = isl_alloc_array(ctx, int, len);
-       if (!s->stack)
-               goto error;
-       s->order = isl_alloc_array(ctx, int, 2 * len);
-       if (!s->order)
-               goto error;
+       dom = isl_set_add_dims(dom, isl_dim_set, 1);
+       app = isl_map_from_domain_and_range(dom, isl_set_copy(dom));
+       map = isl_map_from_basic_map(isl_basic_map_copy(bmap));
+       path = construct_extended_path(dim, map, &project);
+       app = isl_map_intersect(app, path);
 
-       s->sp = 0;
-       s->index = 0;
-       s->op = 0;
+       if ((*exact = check_exactness(map, isl_map_copy(app), project)) < 0)
+               goto error;
 
-       return s;
+       return app;
 error:
-       basic_map_sort_free(s);
+       isl_map_free(app);
        return NULL;
 }
 
-/* Check whether in the computation of the transitive closure
- * "bmap1" (R_1) should follow (or be part of the same component as)
- * "bmap2" (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 whether qc has any elements of length at least one
+ * with domain and/or range outside of dom and ran.
  */
-static int basic_map_follows(__isl_keep isl_basic_map *bmap1,
-       __isl_keep isl_basic_map *bmap2)
+static int has_spurious_elements(__isl_keep isl_map *qc,
+       __isl_keep isl_set *dom, __isl_keep isl_set *ran)
 {
-       struct isl_map *map12 = NULL;
-       struct isl_map *map21 = NULL;
+       isl_set *s;
        int subset;
+       unsigned d;
 
-       map21 = isl_map_from_basic_map(
-                       isl_basic_map_apply_range(
-                               isl_basic_map_copy(bmap2),
-                               isl_basic_map_copy(bmap1)));
-       subset = isl_map_is_empty(map21);
+       if (!qc || !dom || !ran)
+               return -1;
+
+       d = isl_map_dim(qc, isl_dim_in);
+
+       qc = isl_map_copy(qc);
+       qc = set_path_length(qc, 0, 1);
+       qc = isl_map_project_out(qc, isl_dim_in, d - 1, 1);
+       qc = isl_map_project_out(qc, isl_dim_out, d - 1, 1);
+
+       s = isl_map_domain(isl_map_copy(qc));
+       subset = isl_set_is_subset(s, dom);
+       isl_set_free(s);
        if (subset < 0)
                goto error;
-       if (subset) {
-               isl_map_free(map21);
-               return 0;
+       if (!subset) {
+               isl_map_free(qc);
+               return 1;
        }
 
-       map12 = isl_map_from_basic_map(
-                       isl_basic_map_apply_range(
-                               isl_basic_map_copy(bmap1),
-                               isl_basic_map_copy(bmap2)));
-
-       subset = isl_map_is_subset(map21, map12);
-
-       isl_map_free(map12);
-       isl_map_free(map21);
+       s = isl_map_range(qc);
+       subset = isl_set_is_subset(s, ran);
+       isl_set_free(s);
 
        return subset < 0 ? -1 : !subset;
 error:
-       isl_map_free(map21);
+       isl_map_free(qc);
        return -1;
 }
 
-/* Perform Tarjan's algorithm for computing the strongly connected components
- * in the graph with the disjuncts of "map" as vertices and with an
- * edge between any pair of disjuncts such that the first has
- * to be applied after the second.
+#define LEFT   2
+#define RIGHT  1
+
+/* For each basic map in "map", except i, check whether it combines
+ * with the transitive closure that is reflexive on C combines
+ * to the left and to the right.
+ *
+ * In particular, if
+ *
+ *     dom map_j \subseteq C
+ *
+ * then right[j] is set to 1.  Otherwise, if
+ *
+ *     ran map_i \cap dom map_j = \emptyset
+ *
+ * then right[j] is set to 0.  Otherwise, composing to the right
+ * is impossible.
+ *
+ * Similar, for composing to the left, we have if
+ *
+ *     ran map_j \subseteq C
+ *
+ * then left[j] is set to 1.  Otherwise, if
+ *
+ *     dom map_i \cap ran map_j = \emptyset
+ *
+ * then left[j] is set to 0.  Otherwise, composing to the left
+ * is impossible.
+ *
+ * The return value is or'd with LEFT if composing to the left
+ * is possible and with RIGHT if composing to the right is possible.
  */
-static int power_components_tarjan(struct basic_map_sort *s,
-       __isl_keep isl_map *map, int i)
+static int composability(__isl_keep isl_set *C, int i,
+       isl_set **dom, isl_set **ran, int *left, int *right,
+       __isl_keep isl_map *map)
 {
        int j;
+       int ok;
 
-       s->node[i].index = s->index;
-       s->node[i].min_index = s->index;
-       s->node[i].on_stack = 1;
-       s->index++;
-       s->stack[s->sp++] = i;
-
-       for (j = s->len - 1; j >= 0; --j) {
-               int f;
-
+       ok = LEFT | RIGHT;
+       for (j = 0; j < map->n && ok; ++j) {
+               int overlaps, subset;
                if (j == i)
                        continue;
-               if (s->node[j].index >= 0 &&
-                       (!s->node[j].on_stack ||
-                        s->node[j].index > s->node[i].min_index))
-                       continue;
 
-               f = basic_map_follows(map->p[i], map->p[j]);
-               if (f < 0)
-                       return -1;
-               if (!f)
-                       continue;
+               if (ok & RIGHT) {
+                       if (!dom[j])
+                               dom[j] = isl_set_from_basic_set(
+                                       isl_basic_map_domain(
+                                               isl_basic_map_copy(map->p[j])));
+                       if (!dom[j])
+                               return -1;
+                       overlaps = isl_set_overlaps(ran[i], dom[j]);
+                       if (overlaps < 0)
+                               return -1;
+                       if (!overlaps)
+                               right[j] = 0;
+                       else {
+                               subset = isl_set_is_subset(dom[j], C);
+                               if (subset < 0)
+                                       return -1;
+                               if (subset)
+                                       right[j] = 1;
+                               else
+                                       ok &= ~RIGHT;
+                       }
+               }
 
-               if (s->node[j].index < 0) {
-                       power_components_tarjan(s, map, j);
-                       if (s->node[j].min_index < s->node[i].min_index)
-                               s->node[i].min_index = s->node[j].min_index;
-               } else if (s->node[j].index < s->node[i].min_index)
-                       s->node[i].min_index = s->node[j].index;
+               if (ok & LEFT) {
+                       if (!ran[j])
+                               ran[j] = isl_set_from_basic_set(
+                                       isl_basic_map_range(
+                                               isl_basic_map_copy(map->p[j])));
+                       if (!ran[j])
+                               return -1;
+                       overlaps = isl_set_overlaps(dom[i], ran[j]);
+                       if (overlaps < 0)
+                               return -1;
+                       if (!overlaps)
+                               left[j] = 0;
+                       else {
+                               subset = isl_set_is_subset(ran[j], C);
+                               if (subset < 0)
+                                       return -1;
+                               if (subset)
+                                       left[j] = 1;
+                               else
+                                       ok &= ~LEFT;
+                       }
+               }
        }
 
-       if (s->node[i].index != s->node[i].min_index)
-               return 0;
-
-       do {
-               j = s->stack[--s->sp];
-               s->node[j].on_stack = 0;
-               s->order[s->op++] = j;
-       } while (j != i);
-       s->order[s->op++] = -1;
+       return ok;
+}
+
+static __isl_give isl_map *anonymize(__isl_take isl_map *map)
+{
+       map = isl_map_reset(map, isl_dim_in);
+       map = isl_map_reset(map, isl_dim_out);
+       return map;
+}
+
+/* Return a map that is a union of the basic maps in "map", except i,
+ * composed to left and right with qc based on the entries of "left"
+ * and "right".
+ */
+static __isl_give isl_map *compose(__isl_keep isl_map *map, int i,
+       __isl_take isl_map *qc, int *left, int *right)
+{
+       int j;
+       isl_map *comp;
+
+       comp = isl_map_empty(isl_map_get_dim(map));
+       for (j = 0; j < map->n; ++j) {
+               isl_map *map_j;
+
+               if (j == i)
+                       continue;
+
+               map_j = isl_map_from_basic_map(isl_basic_map_copy(map->p[j]));
+               map_j = anonymize(map_j);
+               if (left && left[j])
+                       map_j = isl_map_apply_range(map_j, isl_map_copy(qc));
+               if (right && right[j])
+                       map_j = isl_map_apply_range(isl_map_copy(qc), map_j);
+               comp = isl_map_union(comp, map_j);
+       }
+
+       comp = isl_map_compute_divs(comp);
+       comp = isl_map_coalesce(comp);
+
+       isl_map_free(qc);
+
+       return comp;
+}
+
+/* Compute the transitive closure of "map" incrementally by
+ * computing
+ *
+ *     map_i^+ \cup qc^+
+ *
+ * or
+ *
+ *     map_i^+ \cup ((id \cup map_i^) \circ qc^+)
+ *
+ * or
+ *
+ *     map_i^+ \cup (qc^+ \circ (id \cup map_i^))
+ *
+ * depending on whether left or right are NULL.
+ */
+static __isl_give isl_map *compute_incremental(
+       __isl_take isl_dim *dim, __isl_keep isl_map *map,
+       int i, __isl_take isl_map *qc, int *left, int *right, int *exact)
+{
+       isl_map *map_i;
+       isl_map *tc;
+       isl_map *rtc = NULL;
+
+       if (!map)
+               goto error;
+       isl_assert(map->ctx, left || right, goto error);
+
+       map_i = isl_map_from_basic_map(isl_basic_map_copy(map->p[i]));
+       tc = construct_projected_component(isl_dim_copy(dim), map_i,
+                                               exact, 1);
+       isl_map_free(map_i);
+
+       if (*exact)
+               qc = isl_map_transitive_closure(qc, exact);
+
+       if (!*exact) {
+               isl_dim_free(dim);
+               isl_map_free(tc);
+               isl_map_free(qc);
+               return isl_map_universe(isl_map_get_dim(map));
+       }
+
+       if (!left || !right)
+               rtc = isl_map_union(isl_map_copy(tc),
+                       isl_map_identity(isl_dim_domain(isl_map_get_dim(tc))));
+       if (!right)
+               qc = isl_map_apply_range(rtc, qc);
+       if (!left)
+               qc = isl_map_apply_range(qc, rtc);
+       qc = isl_map_union(tc, qc);
+
+       isl_dim_free(dim);
+
+       return qc;
+error:
+       isl_dim_free(dim);
+       isl_map_free(qc);
+       return NULL;
+}
+
+/* Given a map "map", try to find a basic map such that
+ * map^+ can be computed as
+ *
+ * map^+ = map_i^+ \cup
+ *    \bigcup_j ((map_i^+ \cup Id_C)^+ \circ map_j \circ (map_i^+ \cup Id_C))^+
+ *
+ * with C the simple hull of the domain and range of the input map.
+ * map_i^ \cup Id_C is computed by allowing the path lengths to be zero
+ * and by intersecting domain and range with C.
+ * Of course, we need to check that this is actually equal to map_i^ \cup Id_C.
+ * Also, we only use the incremental computation if all the transitive
+ * closures are exact and if the number of basic maps in the union,
+ * after computing the integer divisions, is smaller than the number
+ * of basic maps in the input map.
+ */
+static int incemental_on_entire_domain(__isl_keep isl_dim *dim,
+       __isl_keep isl_map *map,
+       isl_set **dom, isl_set **ran, int *left, int *right,
+       __isl_give isl_map **res)
+{
+       int i;
+       isl_set *C;
+       unsigned d;
+
+       *res = NULL;
+
+       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_dim_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_dim_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_dim *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_dim_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_dim_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_dim_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_dim_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_dim *dim;
+       isl_basic_map *bstep;
+       isl_map *step;
+       unsigned nparam;
+
+       if (!map)
+               return -1;
+
+       dim = isl_map_get_dim(map);
+       nparam = isl_dim_size(dim, isl_dim_param);
+       dim = isl_dim_drop(dim, isl_dim_in, 0, isl_dim_size(dim, isl_dim_in));
+       dim = isl_dim_drop(dim, isl_dim_out, 0, isl_dim_size(dim, isl_dim_out));
+       dim = isl_dim_add(dim, isl_dim_in, 1);
+       dim = isl_dim_add(dim, isl_dim_out, 1);
+       bstep = isl_basic_map_alloc_dim(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_dim *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_dim(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_dim(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_dim_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_dim_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_dim *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_dim_free(dim);
+       return NULL;
+}
+
+/* Structure for representing the nodes in the graph being traversed
+ * using Tarjan's algorithm.
+ * index represents the order in which nodes are visited.
+ * min_index is the index of the root of a (sub)component.
+ * on_stack indicates whether the node is currently on the stack.
+ */
+struct basic_map_sort_node {
+       int index;
+       int min_index;
+       int on_stack;
+};
+/* Structure for representing the graph being traversed
+ * using Tarjan's algorithm.
+ * len is the number of nodes
+ * node is an array of nodes
+ * stack contains the nodes on the path from the root to the current node
+ * sp is the stack pointer
+ * index is the index of the last node visited
+ * order contains the elements of the components separated by -1
+ * op represents the current position in order
+ *
+ * check_closed is set if we may have used the fact that
+ * a pair of basic maps can be interchanged
+ */
+struct basic_map_sort {
+       int len;
+       struct basic_map_sort_node *node;
+       int *stack;
+       int sp;
+       int index;
+       int *order;
+       int op;
+       int check_closed;
+};
+
+static void basic_map_sort_free(struct basic_map_sort *s)
+{
+       if (!s)
+               return;
+       free(s->node);
+       free(s->stack);
+       free(s->order);
+       free(s);
+}
+
+static struct basic_map_sort *basic_map_sort_alloc(struct isl_ctx *ctx, int len)
+{
+       struct basic_map_sort *s;
+       int i;
+
+       s = isl_calloc_type(ctx, struct basic_map_sort);
+       if (!s)
+               return NULL;
+       s->len = len;
+       s->node = isl_alloc_array(ctx, struct basic_map_sort_node, len);
+       if (!s->node)
+               goto error;
+       for (i = 0; i < len; ++i)
+               s->node[i].index = -1;
+       s->stack = isl_alloc_array(ctx, int, len);
+       if (!s->stack)
+               goto error;
+       s->order = isl_alloc_array(ctx, int, 2 * len);
+       if (!s->order)
+               goto error;
+
+       s->sp = 0;
+       s->index = 0;
+       s->op = 0;
+
+       s->check_closed = 0;
+
+       return s;
+error:
+       basic_map_sort_free(s);
+       return NULL;
+}
+
+/* Check whether in the computation of the transitive closure
+ * "bmap1" (R_1) should follow (or be part of the same component as)
+ * "bmap2" (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(__isl_keep isl_basic_map *bmap1,
+       __isl_keep isl_basic_map *bmap2, int *check_closed)
+{
+       struct isl_map *map12 = NULL;
+       struct isl_map *map21 = NULL;
+       int subset;
+
+       if (!isl_dim_tuple_match(bmap1->dim, isl_dim_in, bmap2->dim, isl_dim_out))
+               return 0;
+
+       map21 = isl_map_from_basic_map(
+                       isl_basic_map_apply_range(
+                               isl_basic_map_copy(bmap2),
+                               isl_basic_map_copy(bmap1)));
+       subset = isl_map_is_empty(map21);
+       if (subset < 0)
+               goto error;
+       if (subset) {
+               isl_map_free(map21);
+               return 0;
+       }
+
+       if (!isl_dim_tuple_match(bmap1->dim, isl_dim_in, bmap1->dim, isl_dim_out) ||
+           !isl_dim_tuple_match(bmap2->dim, isl_dim_in, bmap2->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(bmap1),
+                               isl_basic_map_copy(bmap2)));
+
+       subset = isl_map_is_subset(map21, map12);
+
+       isl_map_free(map12);
+       isl_map_free(map21);
+
+       if (subset)
+               *check_closed = 1;
+
+       return subset < 0 ? -1 : !subset;
+error:
+       isl_map_free(map21);
+       return -1;
+}
+
+/* Perform Tarjan's algorithm for computing the strongly connected components
+ * in the graph with the disjuncts of "map" as vertices and with an
+ * edge between any pair of disjuncts such that the first has
+ * to be applied after the second.
+ */
+static int power_components_tarjan(struct basic_map_sort *s,
+       __isl_keep isl_basic_map **list, int i)
+{
+       int j;
+
+       s->node[i].index = s->index;
+       s->node[i].min_index = s->index;
+       s->node[i].on_stack = 1;
+       s->index++;
+       s->stack[s->sp++] = i;
+
+       for (j = s->len - 1; j >= 0; --j) {
+               int f;
+
+               if (j == i)
+                       continue;
+               if (s->node[j].index >= 0 &&
+                       (!s->node[j].on_stack ||
+                        s->node[j].index > s->node[i].min_index))
+                       continue;
+
+               f = basic_map_follows(list[i], list[j], &s->check_closed);
+               if (f < 0)
+                       return -1;
+               if (!f)
+                       continue;
+
+               if (s->node[j].index < 0) {
+                       power_components_tarjan(s, list, j);
+                       if (s->node[j].min_index < s->node[i].min_index)
+                               s->node[i].min_index = s->node[j].min_index;
+               } else if (s->node[j].index < s->node[i].min_index)
+                       s->node[i].min_index = s->node[j].index;
+       }
+
+       if (s->node[i].index != s->node[i].min_index)
+               return 0;
+
+       do {
+               j = s->stack[--s->sp];
+               s->node[j].on_stack = 0;
+               s->order[s->op++] = j;
+       } while (j != i);
+       s->order[s->op++] = -1;
+
+       return 0;
+}
+
+/* Decompose the "len" basic relations in "list" into strongly connected
+ * components.
+ */
+static struct basic_map_sort *basic_map_sort_init(isl_ctx *ctx, int len,
+       __isl_keep isl_basic_map **list)
+{
+       int i;
+       struct basic_map_sort *s = NULL;
+
+       s = basic_map_sort_alloc(ctx, len);
+       if (!s)
+               return NULL;
+       for (i = len - 1; i >= 0; --i) {
+               if (s->node[i].index >= 0)
+                       continue;
+               if (power_components_tarjan(s, list, i) < 0)
+                       goto error;
+       }
+
+       return s;
+error:
+       basic_map_sort_free(s);
+       return NULL;
+}
+
 /* 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 the union of the identity map and
- * an overapproximation of the map
+ * 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 }
@@ -977,45 +1917,54 @@ static int power_components_tarjan(struct basic_map_sort *s,
  *
  *     { (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 } union
- *     { (x) -> (x) }
+ *                             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 the join the results in the correct
- * order.  The power of each of the components needs to be extended
- * with the identity map because a path in the global result need
- * not go through every component.
- * The final result will then also contain the identity map, but
- * this part will be removed when the length of the path is forced
- * to be strictly positive.
+ * 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_dim *dim,
        __isl_keep isl_map *map, int *exact, int project)
 {
-       int i, n;
+       int i, n, c;
        struct isl_map *path = NULL;
        struct basic_map_sort *s = NULL;
+       int *orig_exact;
+       int local_exact;
 
        if (!map)
                goto error;
        if (map->n <= 1)
-               return construct_component(dim, map, exact, project);
+               return floyd_warshall(dim, map, exact, project);
 
-       s = basic_map_sort_alloc(map->ctx, map->n);
+       s = basic_map_sort_init(map->ctx, map->n, map->p);
        if (!s)
                goto error;
-       for (i = map->n - 1; i >= 0; --i) {
-               if (s->node[i].index >= 0)
-                       continue;
-               if (power_components_tarjan(s, map, i) < 0)
-                       goto error;
-       }
 
+       orig_exact = exact;
+       if (s->check_closed && !exact)
+               exact = &local_exact;
+
+       c = 0;
        i = 0;
        n = map->n;
-       path = isl_map_identity(isl_dim_domain(isl_dim_copy(dim)));
+       if (project)
+               path = isl_map_empty(isl_map_get_dim(map));
+       else
+               path = isl_map_empty(isl_dim_copy(dim));
+       path = anonymize(path);
        while (n) {
                struct isl_map *comp;
+               isl_map *path_comp, *path_comb;
                comp = isl_map_alloc_dim(isl_map_get_dim(map), n, 0);
                while (s->order[i] != -1) {
                        comp = isl_map_add_basic_map(comp,
@@ -1023,11 +1972,28 @@ static __isl_give isl_map *construct_power_components(__isl_take isl_dim *dim,
                        --n;
                        ++i;
                }
-               path = isl_map_apply_range(path,
-                           construct_component(isl_dim_copy(dim), comp,
-                                               exact, project));
+               path_comp = floyd_warshall(isl_dim_copy(dim),
+                                               comp, exact, project);
+               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 && s->check_closed && !*exact) {
+               int closed;
+
+               closed = isl_map_is_transitively_closed(path);
+               if (closed < 0)
+                       goto error;
+               if (!closed) {
+                       basic_map_sort_free(s);
+                       isl_map_free(path);
+                       return floyd_warshall(dim, map, orig_exact, project);
+               }
        }
 
        basic_map_sort_free(s);
@@ -1037,6 +2003,7 @@ static __isl_give isl_map *construct_power_components(__isl_take isl_dim *dim,
 error:
        basic_map_sort_free(s);
        isl_dim_free(dim);
+       isl_map_free(path);
        return NULL;
 }
 
@@ -1062,20 +2029,17 @@ error:
  *
  * if "project" is set.
  *
- * We first construct an extended mapping with an extra coordinate
+ * 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).
- * In the final step, this difference is equated to the parameter "param"
- * and made positive (unless project is set).
- * The extra coordinates are subsequently projected out.
  */
 static __isl_give isl_map *construct_power(__isl_keep isl_map *map,
-       unsigned param, int *exact, int project)
+       int *exact, int project)
 {
        struct isl_map *app = NULL;
-       struct isl_map *diff;
        struct isl_dim *dim = NULL;
        unsigned d;
 
@@ -1091,29 +2055,20 @@ static __isl_give isl_map *construct_power(__isl_keep isl_map *map,
        app = construct_power_components(isl_dim_copy(dim), map,
                                        exact, project);
 
-       if (project) {
-               isl_dim_free(dim);
-               app = set_path_length(app, 0, 1);
-       } else {
-               diff = equate_parameter_to_length(dim, param);
-               app = isl_map_intersect(app, diff);
-       }
-       app = isl_map_project_out(app, isl_dim_in, d, 1);
-       app = isl_map_project_out(app, isl_dim_out, d, 1);
+       isl_dim_free(dim);
 
        return app;
 }
 
 /* Compute the positive powers of "map", or an overapproximation.
- * The power is given by parameter "param".  If the result is exact,
- * then *exact is set to 1.
+ * 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
- * equated to "param" (which is then ignored in this case).
+ * encoded as the difference between an extra pair of final coordinates.
  */
-static __isl_give isl_map *map_power(__isl_take isl_map *map, unsigned param,
+static __isl_give isl_map *map_power(__isl_take isl_map *map,
        int *exact, int project)
 {
        struct isl_map *app = NULL;
@@ -1121,37 +2076,116 @@ static __isl_give isl_map *map_power(__isl_take isl_map *map, unsigned param,
        if (exact)
                *exact = 1;
 
-       map = isl_map_coalesce(map);
        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 power is given by parameter "param".  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 the parameter "param"
+ * and made positive.  The extra coordinates are subsequently projected out.
+ */
+__isl_give isl_map *isl_map_power(__isl_take isl_map *map, unsigned param,
+       int *exact)
+{
+       isl_dim *target_dim;
+       isl_dim *dim;
+       isl_map *diff;
+       unsigned d;
+
+       if (!map)
+               return NULL;
+
+       isl_assert(map->ctx, param < isl_map_dim(map, isl_dim_param),
+               goto error);
+
+       d = isl_map_dim(map, isl_dim_in);
+
+       map = isl_map_compute_divs(map);
+       map = isl_map_coalesce(map);
+
        if (isl_map_fast_is_empty(map))
                return map;
 
-       isl_assert(map->ctx, project || param < isl_map_dim(map, isl_dim_param),
-               goto error);
-       isl_assert(map->ctx,
-               isl_map_dim(map, isl_dim_in) == isl_map_dim(map, isl_dim_out),
-               goto error);
+       target_dim = isl_map_get_dim(map);
+       map = map_power(map, exact, 0);
 
-       app = construct_power(map, param, exact, project);
+       dim = isl_map_get_dim(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);
 
-       isl_map_free(map);
-       return app;
+       map = isl_map_reset_dim(map, target_dim);
+
+       return map;
 error:
        isl_map_free(map);
-       isl_map_free(app);
        return NULL;
 }
 
-/* Compute the positive powers of "map", or an overapproximation.
- * The power is given by parameter "param".  If the result is exact,
- * then *exact is set to 1.
+/* 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_power(__isl_take isl_map *map, unsigned param,
+__isl_give isl_map *isl_map_reaching_path_lengths(__isl_take isl_map *map,
        int *exact)
 {
-       return map_power(map, param, exact, 0);
+       isl_dim *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_fast_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_dim(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
@@ -1213,7 +2247,7 @@ static int is_eq_stride(__isl_keep isl_basic_set *bset, int i)
  *             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.
+ * the given domain and range.
  *
  * If with_id is set, then try to include as much of the identity mapping
  * as possible, by computing
@@ -1221,8 +2255,7 @@ static int is_eq_stride(__isl_keep isl_basic_set *bset, int i)
  *     { 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) and by intersecting domain and range
- * with the union of the domain and the range of the original map.
+ * instead (i.e., allow k = 0).
  *
  * In practice, we compute the difference set
  *
@@ -1232,7 +2265,8 @@ static int is_eq_stride(__isl_keep isl_basic_set *bset, int i)
  * (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(__isl_take isl_map *map, int with_id)
+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;
@@ -1241,8 +2275,6 @@ static __isl_give isl_map *box_closure(__isl_take isl_map *map, int with_id)
        unsigned total;
        isl_dim *dim;
        isl_set *delta;
-       isl_set *domain = NULL;
-       isl_set *range = NULL;
        isl_map *app = NULL;
        isl_basic_set *aff = NULL;
        isl_basic_map *bmap = NULL;
@@ -1333,16 +2365,7 @@ static __isl_give isl_map *box_closure(__isl_take isl_map *map, int 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);
 
-       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);
-       if (with_id) {
-               domain = isl_set_union(domain, range);
-               domain = isl_set_coalesce(domain);
-               range = isl_set_copy(domain);
-       }
-       app = isl_map_from_domain_and_range(domain, range);
+       app = isl_map_from_domain_and_range(dom, ran);
 
        isl_vec_free(obj);
        isl_basic_set_free(aff);
@@ -1359,12 +2382,59 @@ 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);
+
+       return box_closure_on_domain(map, domain, range, 0);
+}
+
+/* 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 and partially reflexive 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.
+ */
+static __isl_give isl_map *box_closure_with_identity(__isl_take isl_map *map,
+       __isl_take isl_set *dom)
+{
+       return box_closure_on_domain(map, dom, isl_set_copy(dom), 1);
+}
+
 /* Check whether app is the transitive closure of map.
  * In particular, check that app is acyclic and, if so,
  * check that
@@ -1430,12 +2500,22 @@ static int check_exactness_omega(__isl_keep isl_map *map,
 static int can_be_split_off(__isl_keep isl_map *map, int i,
        __isl_give isl_map **tc, __isl_give isl_map **qc)
 {
-       isl_map *map_i, *id;
+       isl_map *map_i, *id = NULL;
        int j = -1;
+       isl_set *C;
+
+       *tc = NULL;
+       *qc = NULL;
+
+       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)
+               goto error;
 
        map_i = isl_map_from_basic_map(isl_basic_map_copy(map->p[i]));
-       *tc = box_closure(isl_map_copy(map_i), 0);
-       *qc = box_closure(map_i, 1);
+       *tc = box_closure(isl_map_copy(map_i));
+       *qc = box_closure_with_identity(map_i, C);
        id = isl_map_subtract(isl_map_copy(*qc), isl_map_copy(*tc));
 
        if (!id || !*qc)
@@ -1483,6 +2563,19 @@ error:
        return -1;
 }
 
+static __isl_give isl_map *box_closure_with_check(__isl_take isl_map *map,
+       int *exact)
+{
+       isl_map *app;
+
+       app = box_closure(isl_map_copy(map));
+       if (exact)
+               *exact = check_exactness_omega(map, app);
+
+       isl_map_free(map);
+       return app;
+}
+
 /* Compute an overapproximation of the transitive closure of "map"
  * using a variation of the algorithm from
  * "Transitive Closure of Infinite Graphs and its Applications"
@@ -1501,18 +2594,17 @@ error:
  *
  * If not, we simply call box_closure on the whole map.
  */
-static __isl_give isl_map *compute_closure_omega(__isl_take isl_map *map)
+static __isl_give isl_map *transitive_closure_omega(__isl_take isl_map *map,
+       int *exact)
 {
        int i, j;
+       int exact_i;
+       isl_map *app;
 
        if (!map)
                return NULL;
        if (map->n == 1)
-               return box_closure(map, 0);
-
-       map = isl_map_cow(map);
-       if (!map)
-               goto error;
+               return box_closure_with_check(map, exact);
 
        for (i = 0; i < map->n; ++i) {
                int ok;
@@ -1523,42 +2615,37 @@ static __isl_give isl_map *compute_closure_omega(__isl_take isl_map *map)
                if (!ok)
                        continue;
 
-               isl_basic_map_free(map->p[i]);
-               if (i != map->n - 1)
-                       map->p[i] = map->p[map->n - 1];
-               map->n--;
+               app = isl_map_alloc_dim(isl_map_get_dim(map), map->n - 1, 0);
+
+               for (j = 0; j < map->n; ++j) {
+                       if (j == i)
+                               continue;
+                       app = isl_map_add_basic_map(app,
+                                               isl_basic_map_copy(map->p[j]));
+               }
 
-               map = isl_map_apply_range(isl_map_copy(qc), map);
-               map = isl_map_apply_range(map, qc);
+               app = isl_map_apply_range(isl_map_copy(qc), app);
+               app = isl_map_apply_range(app, qc);
 
-               return isl_map_union(tc, compute_closure_omega(map));
+               app = isl_map_union(tc, transitive_closure_omega(app, NULL));
+               exact_i = check_exactness_omega(map, app);
+               if (exact_i == 1) {
+                       if (exact)
+                               *exact = exact_i;
+                       isl_map_free(map);
+                       return app;
+               }
+               isl_map_free(app);
+               if (exact_i < 0)
+                       goto error;
        }
 
-       return box_closure(map, 0);
+       return box_closure_with_check(map, exact);
 error:
        isl_map_free(map);
        return NULL;
 }
 
-/* Compute an overapproximation of the transitive closure of "map"
- * using a variation of the algorithm from
- * "Transitive Closure of Infinite Graphs and its Applications"
- * by Kelly et al. and check whether the result is definitely exact.
- */
-static __isl_give isl_map *transitive_closure_omega(__isl_take isl_map *map,
-       int *exact)
-{
-       isl_map *app;
-
-       app = compute_closure_omega(isl_map_copy(map));
-
-       if (exact)
-               *exact = check_exactness_omega(map, app);
-
-       isl_map_free(map);
-       return app;
-}
-
 /* Compute the transitive closure  of "map", or an overapproximation.
  * If the result is exact, then *exact is set to 1.
  * Simply use map_power to compute the powers of map, but tell
@@ -1568,7 +2655,8 @@ static __isl_give isl_map *transitive_closure_omega(__isl_take isl_map *map,
 __isl_give isl_map *isl_map_transitive_closure(__isl_take isl_map *map,
        int *exact)
 {
-       unsigned param;
+       isl_dim *target_dim;
+       int closed;
 
        if (!map)
                goto error;
@@ -1576,11 +2664,308 @@ __isl_give isl_map *isl_map_transitive_closure(__isl_take isl_map *map,
        if (map->ctx->opt->closure == ISL_CLOSURE_OMEGA)
                return transitive_closure_omega(map, exact);
 
-       param = isl_map_dim(map, isl_dim_param);
-       map = map_power(map, param, exact, 1);
+       map = isl_map_compute_divs(map);
+       map = isl_map_coalesce(map);
+       closed = isl_map_is_transitively_closed(map);
+       if (closed < 0)
+               goto error;
+       if (closed) {
+               if (exact)
+                       *exact = 1;
+               return map;
+       }
+
+       target_dim = isl_map_get_dim(map);
+       map = map_power(map, exact, 1);
+       map = isl_map_reset_dim(map, target_dim);
 
        return map;
 error:
        isl_map_free(map);
        return NULL;
 }
+
+static int inc_count(__isl_take isl_map *map, void *user)
+{
+       int *n = user;
+
+       *n += map->n;
+
+       isl_map_free(map);
+
+       return 0;
+}
+
+static int collect_basic_map(__isl_take isl_map *map, void *user)
+{
+       int i;
+       isl_basic_map ***next = user;
+
+       for (i = 0; i < map->n; ++i) {
+               **next = isl_basic_map_copy(map->p[i]);
+               if (!**next)
+                       goto error;
+               (*next)++;
+       }
+
+       isl_map_free(map);
+       return 0;
+error:
+       isl_map_free(map);
+       return -1;
+}
+
+/* Perform Floyd-Warshall on the given list of basic relations.
+ * The basic relations may live in different dimensions,
+ * but basic relations that get assigned to the diagonal of the
+ * grid have domains and ranges of the same dimension and so
+ * the standard algorithm can be used because the nested transitive
+ * closures are only applied to diagonal elements and because all
+ * compositions are peformed on relations with compatible domains and ranges.
+ */
+static __isl_give isl_union_map *union_floyd_warshall_on_list(isl_ctx *ctx,
+       __isl_keep isl_basic_map **list, int n, int *exact)
+{
+       int i, j, k;
+       int n_group;
+       int *group = NULL;
+       isl_set **set = NULL;
+       isl_map ***grid = NULL;
+       isl_union_map *app;
+
+       group = setup_groups(ctx, list, n, &set, &n_group);
+       if (!group)
+               goto error;
+
+       grid = isl_calloc_array(ctx, isl_map **, n_group);
+       if (!grid)
+               goto error;
+       for (i = 0; i < n_group; ++i) {
+               grid[i] = isl_calloc_array(map->ctx, isl_map *, n_group);
+               if (!grid[i])
+                       goto error;
+               for (j = 0; j < n_group; ++j) {
+                       isl_dim *dim1, *dim2, *dim;
+                       dim1 = isl_dim_reverse(isl_set_get_dim(set[i]));
+                       dim2 = isl_set_get_dim(set[j]);
+                       dim = isl_dim_join(dim1, dim2);
+                       grid[i][j] = isl_map_empty(dim);
+               }
+       }
+
+       for (k = 0; k < 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(list[k])));
+       }
+       
+       floyd_warshall_iterate(grid, n_group, exact);
+
+       app = isl_union_map_empty(isl_map_get_dim(grid[0][0]));
+
+       for (i = 0; i < n_group; ++i) {
+               for (j = 0; j < n_group; ++j)
+                       app = isl_union_map_add_map(app, grid[i][j]);
+               free(grid[i]);
+       }
+       free(grid);
+
+       for (i = 0; i < 2 * n; ++i)
+               isl_set_free(set[i]);
+       free(set);
+
+       free(group);
+       return app;
+error:
+       if (grid)
+               for (i = 0; i < n_group; ++i) {
+                       if (!grid[i])
+                               continue;
+                       for (j = 0; j < n_group; ++j)
+                               isl_map_free(grid[i][j]);
+                       free(grid[i]);
+               }
+       free(grid);
+       if (set) {
+               for (i = 0; i < 2 * n; ++i)
+                       isl_set_free(set[i]);
+               free(set);
+       }
+       free(group);
+       return NULL;
+}
+
+/* Perform Floyd-Warshall on the given union relation.
+ * The implementation is very similar to that for non-unions.
+ * The main difference is that it is applied unconditionally.
+ * We first extract a list of basic maps from the union map
+ * and then perform the algorithm on this list.
+ */
+static __isl_give isl_union_map *union_floyd_warshall(
+       __isl_take isl_union_map *umap, int *exact)
+{
+       int i, n;
+       isl_ctx *ctx;
+       isl_basic_map **list;
+       isl_basic_map **next;
+       isl_union_map *res;
+
+       n = 0;
+       if (isl_union_map_foreach_map(umap, inc_count, &n) < 0)
+               goto error;
+
+       ctx = isl_union_map_get_ctx(umap);
+       list = isl_calloc_array(ctx, isl_basic_map *, n);
+       if (!list)
+               goto error;
+
+       next = list;
+       if (isl_union_map_foreach_map(umap, collect_basic_map, &next) < 0)
+               goto error;
+
+       res = union_floyd_warshall_on_list(ctx, list, n, exact);
+
+       if (list) {
+               for (i = 0; i < n; ++i)
+                       isl_basic_map_free(list[i]);
+               free(list);
+       }
+
+       isl_union_map_free(umap);
+       return res;
+error:
+       if (list) {
+               for (i = 0; i < n; ++i)
+                       isl_basic_map_free(list[i]);
+               free(list);
+       }
+       isl_union_map_free(umap);
+       return NULL;
+}
+
+/* Decompose the give union relation into strongly connected components.
+ * The implementation is essentially the same as that of
+ * construct_power_components with the major difference that all
+ * operations are performed on union maps.
+ */
+static __isl_give isl_union_map *union_components(
+       __isl_take isl_union_map *umap, int *exact)
+{
+       int i;
+       int n;
+       isl_ctx *ctx;
+       isl_basic_map **list;
+       isl_basic_map **next;
+       isl_union_map *path = NULL;
+       struct basic_map_sort *s = NULL;
+       int c, l;
+       int recheck = 0;
+
+       n = 0;
+       if (isl_union_map_foreach_map(umap, inc_count, &n) < 0)
+               goto error;
+
+       if (n <= 1)
+               return union_floyd_warshall(umap, exact);
+
+       ctx = isl_union_map_get_ctx(umap);
+       list = isl_calloc_array(ctx, isl_basic_map *, n);
+       if (!list)
+               goto error;
+
+       next = list;
+       if (isl_union_map_foreach_map(umap, collect_basic_map, &next) < 0)
+               goto error;
+
+       s = basic_map_sort_init(ctx, n, list);
+       if (!s)
+               goto error;
+
+       c = 0;
+       i = 0;
+       l = n;
+       path = isl_union_map_empty(isl_union_map_get_dim(umap));
+       while (l) {
+               isl_union_map *comp;
+               isl_union_map *path_comp, *path_comb;
+               comp = isl_union_map_empty(isl_union_map_get_dim(umap));
+               while (s->order[i] != -1) {
+                       comp = isl_union_map_add_map(comp,
+                                   isl_map_from_basic_map(
+                                       isl_basic_map_copy(list[s->order[i]])));
+                       --l;
+                       ++i;
+               }
+               path_comp = union_floyd_warshall(comp, exact);
+               path_comb = isl_union_map_apply_range(isl_union_map_copy(path),
+                                               isl_union_map_copy(path_comp));
+               path = isl_union_map_union(path, path_comp);
+               path = isl_union_map_union(path, path_comb);
+               ++i;
+               ++c;
+       }
+
+       if (c > 1 && s->check_closed && !*exact) {
+               int closed;
+
+               closed = isl_union_map_is_transitively_closed(path);
+               if (closed < 0)
+                       goto error;
+               recheck = !closed;
+       }
+
+       basic_map_sort_free(s);
+
+       for (i = 0; i < n; ++i)
+               isl_basic_map_free(list[i]);
+       free(list);
+
+       if (recheck) {
+               isl_union_map_free(path);
+               return union_floyd_warshall(umap, exact);
+       }
+
+       isl_union_map_free(umap);
+
+       return path;
+error:
+       basic_map_sort_free(s);
+       if (list) {
+               for (i = 0; i < n; ++i)
+                       isl_basic_map_free(list[i]);
+               free(list);
+       }
+       isl_union_map_free(umap);
+       isl_union_map_free(path);
+       return NULL;
+}
+
+/* Compute the transitive closure  of "umap", or an overapproximation.
+ * If the result is exact, then *exact is set to 1.
+ */
+__isl_give isl_union_map *isl_union_map_transitive_closure(
+       __isl_take isl_union_map *umap, int *exact)
+{
+       int closed;
+
+       if (!umap)
+               return NULL;
+
+       if (exact)
+               *exact = 1;
+
+       umap = isl_union_map_compute_divs(umap);
+       umap = isl_union_map_coalesce(umap);
+       closed = isl_union_map_is_transitively_closed(umap);
+       if (closed < 0)
+               goto error;
+       if (closed)
+               return umap;
+       umap = union_components(umap, exact);
+       return umap;
+error:
+       isl_union_map_free(umap);
+       return NULL;
+}