add isl_aff_mod_val
[platform/upstream/isl.git] / isl_coalesce.c
index 0694ade..ed80560 100644 (file)
@@ -1,19 +1,23 @@
 /*
  * Copyright 2008-2009 Katholieke Universiteit Leuven
  * Copyright 2010      INRIA Saclay
+ * Copyright 2012-2013 Ecole Normale Superieure
  *
- * Use of this software is governed by the GNU LGPLv2.1 license
+ * Use of this software is governed by the MIT license
  *
  * Written by Sven Verdoolaege, K.U.Leuven, Departement
  * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
  * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
  * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France 
+ * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
  */
 
 #include "isl_map_private.h"
 #include <isl/seq.h>
+#include <isl/options.h>
 #include "isl_tab.h"
 #include <isl_mat_private.h>
+#include <isl_local_space_private.h>
 
 #define STATUS_ERROR           -1
 #define STATUS_REDUNDANT        1
@@ -37,24 +41,24 @@ static int status_in(isl_int *ineq, struct isl_tab *tab)
        }
 }
 
-/* Compute the position of the equalities of basic map "i"
- * with respect to basic map "j".
+/* Compute the position of the equalities of basic map "bmap_i"
+ * with respect to the basic map represented by "tab_j".
  * The resulting array has twice as many entries as the number
  * of equalities corresponding to the two inequalties to which
  * each equality corresponds.
  */
-static int *eq_status_in(struct isl_map *map, int i, int j,
-       struct isl_tab **tabs)
+static int *eq_status_in(__isl_keep isl_basic_map *bmap_i,
+       struct isl_tab *tab_j)
 {
        int k, l;
-       int *eq = isl_calloc_array(map->ctx, int, 2 * map->p[i]->n_eq);
+       int *eq = isl_calloc_array(bmap_i->ctx, int, 2 * bmap_i->n_eq);
        unsigned dim;
 
-       dim = isl_basic_map_total_dim(map->p[i]);
-       for (k = 0; k < map->p[i]->n_eq; ++k) {
+       dim = isl_basic_map_total_dim(bmap_i);
+       for (k = 0; k < bmap_i->n_eq; ++k) {
                for (l = 0; l < 2; ++l) {
-                       isl_seq_neg(map->p[i]->eq[k], map->p[i]->eq[k], 1+dim);
-                       eq[2 * k + l] = status_in(map->p[i]->eq[k], tabs[j]);
+                       isl_seq_neg(bmap_i->eq[k], bmap_i->eq[k], 1+dim);
+                       eq[2 * k + l] = status_in(bmap_i->eq[k], tab_j);
                        if (eq[2 * k + l] == STATUS_ERROR)
                                goto error;
                }
@@ -69,22 +73,23 @@ error:
        return NULL;
 }
 
-/* Compute the position of the inequalities of basic map "i"
- * with respect to basic map "j".
+/* Compute the position of the inequalities of basic map "bmap_i"
+ * (also represented by "tab_i", if not NULL) with respect to the basic map
+ * represented by "tab_j".
  */
-static int *ineq_status_in(struct isl_map *map, int i, int j,
-       struct isl_tab **tabs)
+static int *ineq_status_in(__isl_keep isl_basic_map *bmap_i,
+       struct isl_tab *tab_i, struct isl_tab *tab_j)
 {
        int k;
-       unsigned n_eq = map->p[i]->n_eq;
-       int *ineq = isl_calloc_array(map->ctx, int, map->p[i]->n_ineq);
+       unsigned n_eq = bmap_i->n_eq;
+       int *ineq = isl_calloc_array(bmap_i->ctx, int, bmap_i->n_ineq);
 
-       for (k = 0; k < map->p[i]->n_ineq; ++k) {
-               if (isl_tab_is_redundant(tabs[i], n_eq + k)) {
+       for (k = 0; k < bmap_i->n_ineq; ++k) {
+               if (tab_i && isl_tab_is_redundant(tab_i, n_eq + k)) {
                        ineq[k] = STATUS_REDUNDANT;
                        continue;
                }
-               ineq[k] = status_in(map->p[i]->ineq[k], tabs[j]);
+               ineq[k] = status_in(bmap_i->ineq[k], tab_j);
                if (ineq[k] == STATUS_ERROR)
                        goto error;
                if (ineq[k] == STATUS_SEPARATE)
@@ -293,6 +298,124 @@ static int check_facets(struct isl_map *map, int i, int j,
        return fuse(map, i, j, tabs, NULL, ineq_i, NULL, ineq_j, NULL);
 }
 
+/* Check if basic map "i" contains the basic map represented
+ * by the tableau "tab".
+ */
+static int contains(struct isl_map *map, int i, int *ineq_i,
+       struct isl_tab *tab)
+{
+       int k, l;
+       unsigned dim;
+
+       dim = isl_basic_map_total_dim(map->p[i]);
+       for (k = 0; k < map->p[i]->n_eq; ++k) {
+               for (l = 0; l < 2; ++l) {
+                       int stat;
+                       isl_seq_neg(map->p[i]->eq[k], map->p[i]->eq[k], 1+dim);
+                       stat = status_in(map->p[i]->eq[k], tab);
+                       if (stat != STATUS_VALID)
+                               return 0;
+               }
+       }
+
+       for (k = 0; k < map->p[i]->n_ineq; ++k) {
+               int stat;
+               if (ineq_i[k] == STATUS_REDUNDANT)
+                       continue;
+               stat = status_in(map->p[i]->ineq[k], tab);
+               if (stat != STATUS_VALID)
+                       return 0;
+       }
+       return 1;
+}
+
+/* Basic map "i" has an inequality (say "k") that is adjacent
+ * to some inequality of basic map "j".  All the other inequalities
+ * are valid for "j".
+ * Check if basic map "j" forms an extension of basic map "i".
+ *
+ * Note that this function is only called if some of the equalities or
+ * inequalities of basic map "j" do cut basic map "i".  The function is
+ * correct even if there are no such cut constraints, but in that case
+ * the additional checks performed by this function are overkill.
+ *
+ * In particular, we replace constraint k, say f >= 0, by constraint
+ * f <= -1, add the inequalities of "j" that are valid for "i"
+ * and check if the result is a subset of basic map "j".
+ * If so, then we know that this result is exactly equal to basic map "j"
+ * since all its constraints are valid for basic map "j".
+ * By combining the valid constraints of "i" (all equalities and all
+ * inequalities except "k") and the valid constraints of "j" we therefore
+ * obtain a basic map that is equal to their union.
+ * In this case, there is no need to perform a rollback of the tableau
+ * since it is going to be destroyed in fuse().
+ *
+ *
+ *     |\__                    |\__
+ *     |   \__                 |   \__
+ *     |      \_       =>      |      \__
+ *     |_______| _             |_________\
+ *
+ *
+ *     |\                      |\
+ *     | \                     | \
+ *     |  \                    |  \
+ *     |  |                    |   \
+ *     |  ||\          =>      |    \
+ *     |  || \                 |     \
+ *     |  ||  |                |      |
+ *     |__||_/                 |_____/
+ */
+static int is_adj_ineq_extension(__isl_keep isl_map *map, int i, int j,
+       struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
+{
+       int k;
+       struct isl_tab_undo *snap;
+       unsigned n_eq = map->p[i]->n_eq;
+       unsigned total = isl_basic_map_total_dim(map->p[i]);
+       int r;
+
+       if (isl_tab_extend_cons(tabs[i], 1 + map->p[j]->n_ineq) < 0)
+               return -1;
+
+       for (k = 0; k < map->p[i]->n_ineq; ++k)
+               if (ineq_i[k] == STATUS_ADJ_INEQ)
+                       break;
+       if (k >= map->p[i]->n_ineq)
+               isl_die(isl_map_get_ctx(map), isl_error_internal,
+                       "ineq_i should have exactly one STATUS_ADJ_INEQ",
+                       return -1);
+
+       snap = isl_tab_snap(tabs[i]);
+
+       if (isl_tab_unrestrict(tabs[i], n_eq + k) < 0)
+               return -1;
+
+       isl_seq_neg(map->p[i]->ineq[k], map->p[i]->ineq[k], 1 + total);
+       isl_int_sub_ui(map->p[i]->ineq[k][0], map->p[i]->ineq[k][0], 1);
+       r = isl_tab_add_ineq(tabs[i], map->p[i]->ineq[k]);
+       isl_seq_neg(map->p[i]->ineq[k], map->p[i]->ineq[k], 1 + total);
+       isl_int_sub_ui(map->p[i]->ineq[k][0], map->p[i]->ineq[k][0], 1);
+       if (r < 0)
+               return -1;
+
+       for (k = 0; k < map->p[j]->n_ineq; ++k) {
+               if (ineq_j[k] != STATUS_VALID)
+                       continue;
+               if (isl_tab_add_ineq(tabs[i], map->p[j]->ineq[k]) < 0)
+                       return -1;
+       }
+
+       if (contains(map, j, ineq_j, tabs[i]))
+               return fuse(map, i, j, tabs, eq_i, ineq_i, eq_j, ineq_j, NULL);
+
+       if (isl_tab_rollback(tabs[i], snap) < 0)
+               return -1;
+
+       return 0;
+}
+
+
 /* Both basic maps have at least one inequality with and adjacent
  * (but opposite) inequality in the other basic map.
  * Check that there are no cut constraints and that there is only
@@ -319,53 +442,40 @@ static int check_facets(struct isl_map *map, int i, int j,
  *         |   |
  *         |   |
  *         |___|
+ *
+ * If there are some cut constraints on one side, then we may
+ * still be able to fuse the two basic maps, but we need to perform
+ * some additional checks in is_adj_ineq_extension.
  */
 static int check_adj_ineq(struct isl_map *map, int i, int j,
-       struct isl_tab **tabs, int *ineq_i, int *ineq_j)
+       struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
 {
-       int changed = 0;
+       int count_i, count_j;
+       int cut_i, cut_j;
 
-       if (any(ineq_i, map->p[i]->n_ineq, STATUS_CUT) ||
-           any(ineq_j, map->p[j]->n_ineq, STATUS_CUT))
-               /* ADJ INEQ CUT */
-               ;
-       else if (count(ineq_i, map->p[i]->n_ineq, STATUS_ADJ_INEQ) == 1 &&
-                count(ineq_j, map->p[j]->n_ineq, STATUS_ADJ_INEQ) == 1)
-               changed = fuse(map, i, j, tabs, NULL, ineq_i, NULL, ineq_j, NULL);
-       /* else ADJ INEQ TOO MANY */
+       count_i = count(ineq_i, map->p[i]->n_ineq, STATUS_ADJ_INEQ);
+       count_j = count(ineq_j, map->p[j]->n_ineq, STATUS_ADJ_INEQ);
 
-       return changed;
-}
+       if (count_i != 1 && count_j != 1)
+               return 0;
 
-/* Check if basic map "i" contains the basic map represented
- * by the tableau "tab".
- */
-static int contains(struct isl_map *map, int i, int *ineq_i,
-       struct isl_tab *tab)
-{
-       int k, l;
-       unsigned dim;
+       cut_i = any(eq_i, 2 * map->p[i]->n_eq, STATUS_CUT) ||
+               any(ineq_i, map->p[i]->n_ineq, STATUS_CUT);
+       cut_j = any(eq_j, 2 * map->p[j]->n_eq, STATUS_CUT) ||
+               any(ineq_j, map->p[j]->n_ineq, STATUS_CUT);
 
-       dim = isl_basic_map_total_dim(map->p[i]);
-       for (k = 0; k < map->p[i]->n_eq; ++k) {
-               for (l = 0; l < 2; ++l) {
-                       int stat;
-                       isl_seq_neg(map->p[i]->eq[k], map->p[i]->eq[k], 1+dim);
-                       stat = status_in(map->p[i]->eq[k], tab);
-                       if (stat != STATUS_VALID)
-                               return 0;
-               }
-       }
+       if (!cut_i && !cut_j && count_i == 1 && count_j == 1)
+               return fuse(map, i, j, tabs, NULL, ineq_i, NULL, ineq_j, NULL);
 
-       for (k = 0; k < map->p[i]->n_ineq; ++k) {
-               int stat;
-               if (ineq_i[k] == STATUS_REDUNDANT)
-                       continue;
-               stat = status_in(map->p[i]->ineq[k], tab);
-               if (stat != STATUS_VALID)
-                       return 0;
-       }
-       return 1;
+       if (count_i == 1 && !cut_i)
+               return is_adj_ineq_extension(map, i, j, tabs,
+                                               eq_i, ineq_i, eq_j, ineq_j);
+
+       if (count_j == 1 && !cut_j)
+               return is_adj_ineq_extension(map, j, i, tabs,
+                                               eq_j, ineq_j, eq_i, ineq_i);
+
+       return 0;
 }
 
 /* Basic map "i" has an inequality "k" that is adjacent to some equality
@@ -378,7 +488,7 @@ static int contains(struct isl_map *map, int i, int *ineq_i,
  * map with exactly the other basic map (we already know that this
  * other basic map is included in the extension, because there
  * were no "cut" inequalities in "i") and we can replace the
- * two basic maps by thie extension.
+ * two basic maps by this extension.
  *        ____                   _____
  *       /    ||                /     |
  *      /     ||               /      |
@@ -386,7 +496,7 @@ static int contains(struct isl_map *map, int i, int *ineq_i,
  *       \    ||                \     |
  *        \___||                 \____|
  */
-static int is_extension(struct isl_map *map, int i, int j, int k,
+static int is_adj_eq_extension(struct isl_map *map, int i, int j, int k,
        struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
 {
        int changed = 0;
@@ -394,6 +504,9 @@ static int is_extension(struct isl_map *map, int i, int j, int k,
        struct isl_tab_undo *snap, *snap2;
        unsigned n_eq = map->p[i]->n_eq;
 
+       if (isl_tab_is_equality(tabs[i], n_eq + k))
+               return 0;
+
        snap = isl_tab_snap(tabs[i]);
        tabs[i] = isl_tab_relax(tabs[i], n_eq + k);
        snap2 = isl_tab_snap(tabs[i]);
@@ -417,6 +530,106 @@ static int is_extension(struct isl_map *map, int i, int j, int k,
        return changed;
 }
 
+/* Data structure that keeps track of the wrapping constraints
+ * and of information to bound the coefficients of those constraints.
+ *
+ * bound is set if we want to apply a bound on the coefficients
+ * mat contains the wrapping constraints
+ * max is the bound on the coefficients (if bound is set)
+ */
+struct isl_wraps {
+       int bound;
+       isl_mat *mat;
+       isl_int max;
+};
+
+/* Update wraps->max to be greater than or equal to the coefficients
+ * in the equalities and inequalities of bmap that can be removed if we end up
+ * applying wrapping.
+ */
+static void wraps_update_max(struct isl_wraps *wraps,
+       __isl_keep isl_basic_map *bmap, int *eq, int *ineq)
+{
+       int k;
+       isl_int max_k;
+       unsigned total = isl_basic_map_total_dim(bmap);
+
+       isl_int_init(max_k);
+
+       for (k = 0; k < bmap->n_eq; ++k) {
+               if (eq[2 * k] == STATUS_VALID &&
+                   eq[2 * k + 1] == STATUS_VALID)
+                       continue;
+               isl_seq_abs_max(bmap->eq[k] + 1, total, &max_k);
+               if (isl_int_abs_gt(max_k, wraps->max))
+                       isl_int_set(wraps->max, max_k);
+       }
+
+       for (k = 0; k < bmap->n_ineq; ++k) {
+               if (ineq[k] == STATUS_VALID || ineq[k] == STATUS_REDUNDANT)
+                       continue;
+               isl_seq_abs_max(bmap->ineq[k] + 1, total, &max_k);
+               if (isl_int_abs_gt(max_k, wraps->max))
+                       isl_int_set(wraps->max, max_k);
+       }
+
+       isl_int_clear(max_k);
+}
+
+/* Initialize the isl_wraps data structure.
+ * If we want to bound the coefficients of the wrapping constraints,
+ * we set wraps->max to the largest coefficient
+ * in the equalities and inequalities that can be removed if we end up
+ * applying wrapping.
+ */
+static void wraps_init(struct isl_wraps *wraps, __isl_take isl_mat *mat,
+       __isl_keep isl_map *map, int i, int j,
+       int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
+{
+       isl_ctx *ctx;
+
+       wraps->bound = 0;
+       wraps->mat = mat;
+       if (!mat)
+               return;
+       ctx = isl_mat_get_ctx(mat);
+       wraps->bound = isl_options_get_coalesce_bounded_wrapping(ctx);
+       if (!wraps->bound)
+               return;
+       isl_int_init(wraps->max);
+       isl_int_set_si(wraps->max, 0);
+       wraps_update_max(wraps, map->p[i], eq_i, ineq_i);
+       wraps_update_max(wraps, map->p[j], eq_j, ineq_j);
+}
+
+/* Free the contents of the isl_wraps data structure.
+ */
+static void wraps_free(struct isl_wraps *wraps)
+{
+       isl_mat_free(wraps->mat);
+       if (wraps->bound)
+               isl_int_clear(wraps->max);
+}
+
+/* Is the wrapping constraint in row "row" allowed?
+ *
+ * If wraps->bound is set, we check that none of the coefficients
+ * is greater than wraps->max.
+ */
+static int allow_wrap(struct isl_wraps *wraps, int row)
+{
+       int i;
+
+       if (!wraps->bound)
+               return 1;
+
+       for (i = 1; i < wraps->mat->n_col; ++i)
+               if (isl_int_abs_gt(wraps->mat->row[row][i], wraps->max))
+                       return 0;
+
+       return 1;
+}
+
 /* For each non-redundant constraint in "bmap" (as determined by "tab"),
  * wrap the constraint around "bound" such that it includes the whole
  * set "set" and append the resulting constraint to "wraps".
@@ -427,15 +640,18 @@ static int is_extension(struct isl_map *map, int i, int j, int k,
  * identical to "bound", then this means that "set" is unbounded in such
  * way that no wrapping is possible.  If this happens then wraps->n_row
  * is reset to zero.
+ * Similarly, if we want to bound the coefficients of the wrapping
+ * constraints and a newly added wrapping constraint does not
+ * satisfy the bound, then wraps->n_row is also reset to zero.
  */
-static int add_wraps(__isl_keep isl_mat *wraps, __isl_keep isl_basic_map *bmap,
+static int add_wraps(struct isl_wraps *wraps, __isl_keep isl_basic_map *bmap,
        struct isl_tab *tab, isl_int *bound, __isl_keep isl_set *set)
 {
        int l;
        int w;
        unsigned total = isl_basic_map_total_dim(bmap);
 
-       w = wraps->n_row;
+       w = wraps->mat->n_row;
 
        for (l = 0; l < bmap->n_ineq; ++l) {
                if (isl_seq_is_neg(bound, bmap->ineq[l], 1 + total))
@@ -445,10 +661,12 @@ static int add_wraps(__isl_keep isl_mat *wraps, __isl_keep isl_basic_map *bmap,
                if (isl_tab_is_redundant(tab, bmap->n_eq + l))
                        continue;
 
-               isl_seq_cpy(wraps->row[w], bound, 1 + total);
-               if (!isl_set_wrap_facet(set, wraps->row[w], bmap->ineq[l]))
+               isl_seq_cpy(wraps->mat->row[w], bound, 1 + total);
+               if (!isl_set_wrap_facet(set, wraps->mat->row[w], bmap->ineq[l]))
                        return -1;
-               if (isl_seq_eq(wraps->row[w], bound, 1 + total))
+               if (isl_seq_eq(wraps->mat->row[w], bound, 1 + total))
+                       goto unbounded;
+               if (!allow_wrap(wraps, w))
                        goto unbounded;
                ++w;
        }
@@ -458,26 +676,31 @@ static int add_wraps(__isl_keep isl_mat *wraps, __isl_keep isl_basic_map *bmap,
                if (isl_seq_eq(bound, bmap->eq[l], 1 + total))
                        continue;
 
-               isl_seq_cpy(wraps->row[w], bound, 1 + total);
-               isl_seq_neg(wraps->row[w + 1], bmap->eq[l], 1 + total);
-               if (!isl_set_wrap_facet(set, wraps->row[w], wraps->row[w + 1]))
+               isl_seq_cpy(wraps->mat->row[w], bound, 1 + total);
+               isl_seq_neg(wraps->mat->row[w + 1], bmap->eq[l], 1 + total);
+               if (!isl_set_wrap_facet(set, wraps->mat->row[w],
+                                       wraps->mat->row[w + 1]))
                        return -1;
-               if (isl_seq_eq(wraps->row[w], bound, 1 + total))
+               if (isl_seq_eq(wraps->mat->row[w], bound, 1 + total))
+                       goto unbounded;
+               if (!allow_wrap(wraps, w))
                        goto unbounded;
                ++w;
 
-               isl_seq_cpy(wraps->row[w], bound, 1 + total);
-               if (!isl_set_wrap_facet(set, wraps->row[w], bmap->eq[l]))
+               isl_seq_cpy(wraps->mat->row[w], bound, 1 + total);
+               if (!isl_set_wrap_facet(set, wraps->mat->row[w], bmap->eq[l]))
                        return -1;
-               if (isl_seq_eq(wraps->row[w], bound, 1 + total))
+               if (isl_seq_eq(wraps->mat->row[w], bound, 1 + total))
+                       goto unbounded;
+               if (!allow_wrap(wraps, w))
                        goto unbounded;
                ++w;
        }
 
-       wraps->n_row = w;
+       wraps->mat->n_row = w;
        return 0;
 unbounded:
-       wraps->n_row = 0;
+       wraps->mat->n_row = 0;
        return 0;
 }
 
@@ -555,7 +778,8 @@ static int can_wrap_in_facet(struct isl_map *map, int i, int j, int k,
        struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
 {
        int changed = 0;
-       struct isl_mat *wraps = NULL;
+       struct isl_wraps wraps;
+       isl_mat *mat;
        struct isl_set *set_i = NULL;
        struct isl_set *set_j = NULL;
        struct isl_vec *bound = NULL;
@@ -565,22 +789,23 @@ static int can_wrap_in_facet(struct isl_map *map, int i, int j, int k,
 
        set_i = set_from_updated_bmap(map->p[i], tabs[i]);
        set_j = set_from_updated_bmap(map->p[j], tabs[j]);
-       wraps = isl_mat_alloc(map->ctx, 2 * (map->p[i]->n_eq + map->p[j]->n_eq) +
+       mat = isl_mat_alloc(map->ctx, 2 * (map->p[i]->n_eq + map->p[j]->n_eq) +
                                        map->p[i]->n_ineq + map->p[j]->n_ineq,
                                        1 + total);
+       wraps_init(&wraps, mat, map, i, j, eq_i, ineq_i, eq_j, ineq_j);
        bound = isl_vec_alloc(map->ctx, 1 + total);
-       if (!set_i || !set_j || !wraps || !bound)
+       if (!set_i || !set_j || !wraps.mat || !bound)
                goto error;
 
        isl_seq_cpy(bound->el, map->p[i]->ineq[k], 1 + total);
        isl_int_add_ui(bound->el[0], bound->el[0], 1);
 
-       isl_seq_cpy(wraps->row[0], bound->el, 1 + total);
-       wraps->n_row = 1;
+       isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
+       wraps.mat->n_row = 1;
 
-       if (add_wraps(wraps, map->p[j], tabs[j], bound->el, set_i) < 0)
+       if (add_wraps(&wraps, map->p[j], tabs[j], bound->el, set_i) < 0)
                goto error;
-       if (!wraps->n_row)
+       if (!wraps.mat->n_row)
                goto unbounded;
 
        snap = isl_tab_snap(tabs[i]);
@@ -592,21 +817,21 @@ static int can_wrap_in_facet(struct isl_map *map, int i, int j, int k,
 
        isl_seq_neg(bound->el, map->p[i]->ineq[k], 1 + total);
 
-       n = wraps->n_row;
-       if (add_wraps(wraps, map->p[i], tabs[i], bound->el, set_j) < 0)
+       n = wraps.mat->n_row;
+       if (add_wraps(&wraps, map->p[i], tabs[i], bound->el, set_j) < 0)
                goto error;
 
        if (isl_tab_rollback(tabs[i], snap) < 0)
                goto error;
-       if (check_wraps(wraps, n, tabs[i]) < 0)
+       if (check_wraps(wraps.mat, n, tabs[i]) < 0)
                goto error;
-       if (!wraps->n_row)
+       if (!wraps.mat->n_row)
                goto unbounded;
 
-       changed = fuse(map, i, j, tabs, eq_i, ineq_i, eq_j, ineq_j, wraps);
+       changed = fuse(map, i, j, tabs, eq_i, ineq_i, eq_j, ineq_j, wraps.mat);
 
 unbounded:
-       isl_mat_free(wraps);
+       wraps_free(&wraps);
 
        isl_set_free(set_i);
        isl_set_free(set_j);
@@ -615,8 +840,8 @@ unbounded:
 
        return changed;
 error:
+       wraps_free(&wraps);
        isl_vec_free(bound);
-       isl_mat_free(wraps);
        isl_set_free(set_i);
        isl_set_free(set_j);
        return -1;
@@ -676,7 +901,8 @@ static int wrap_in_facets(struct isl_map *map, int i, int j,
        int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
 {
        int changed = 0;
-       isl_mat *wraps = NULL;
+       struct isl_wraps wraps;
+       isl_mat *mat;
        isl_set *set = NULL;
        isl_vec *bound = NULL;
        unsigned total = isl_basic_map_total_dim(map->p[i]);
@@ -693,15 +919,16 @@ static int wrap_in_facets(struct isl_map *map, int i, int j,
 
        set = isl_set_union(set_from_updated_bmap(map->p[i], tabs[i]),
                            set_from_updated_bmap(map->p[j], tabs[j]));
-       wraps = isl_mat_alloc(map->ctx, max_wrap, 1 + total);
+       mat = isl_mat_alloc(map->ctx, max_wrap, 1 + total);
+       wraps_init(&wraps, mat, map, i, j, eq_i, ineq_i, eq_j, ineq_j);
        bound = isl_vec_alloc(map->ctx, 1 + total);
-       if (!set || !wraps || !bound)
+       if (!set || !wraps.mat || !bound)
                goto error;
 
        snap_i = isl_tab_snap(tabs[i]);
        snap_j = isl_tab_snap(tabs[j]);
 
-       wraps->n_row = 0;
+       wraps.mat->n_row = 0;
 
        for (k = 0; k < n; ++k) {
                if (isl_tab_select_facet(tabs[i], map->p[i]->n_eq + cuts[k]) < 0)
@@ -712,7 +939,7 @@ static int wrap_in_facets(struct isl_map *map, int i, int j,
 
                isl_seq_neg(bound->el, map->p[i]->ineq[cuts[k]], 1 + total);
                if (!tabs[i]->empty &&
-                   add_wraps(wraps, map->p[i], tabs[i], bound->el, set) < 0)
+                   add_wraps(&wraps, map->p[i], tabs[i], bound->el, set) < 0)
                        goto error;
 
                set_is_redundant(tabs[i], map->p[i]->n_eq, cuts, n, k, 0);
@@ -721,7 +948,7 @@ static int wrap_in_facets(struct isl_map *map, int i, int j,
 
                if (tabs[i]->empty)
                        break;
-               if (!wraps->n_row)
+               if (!wraps.mat->n_row)
                        break;
 
                isl_seq_cpy(bound->el, map->p[i]->ineq[cuts[k]], 1 + total);
@@ -732,28 +959,28 @@ static int wrap_in_facets(struct isl_map *map, int i, int j,
                        goto error;
 
                if (!tabs[j]->empty &&
-                   add_wraps(wraps, map->p[j], tabs[j], bound->el, set) < 0)
+                   add_wraps(&wraps, map->p[j], tabs[j], bound->el, set) < 0)
                        goto error;
 
                if (isl_tab_rollback(tabs[j], snap_j) < 0)
                        goto error;
 
-               if (!wraps->n_row)
+               if (!wraps.mat->n_row)
                        break;
        }
 
        if (k == n)
                changed = fuse(map, i, j, tabs,
-                               eq_i, ineq_i, eq_j, ineq_j, wraps);
+                               eq_i, ineq_i, eq_j, ineq_j, wraps.mat);
 
        isl_vec_free(bound);
-       isl_mat_free(wraps);
+       wraps_free(&wraps);
        isl_set_free(set);
 
        return changed;
 error:
        isl_vec_free(bound);
-       isl_mat_free(wraps);
+       wraps_free(&wraps);
        isl_set_free(set);
        return -1;
 }
@@ -926,11 +1153,12 @@ static int check_adj_eq(struct isl_map *map, int i, int j,
                /* ADJ EQ TOO MANY */
                return 0;
 
-       for (k = 0; k < map->p[i]->n_ineq ; ++k)
+       for (k = 0; k < map->p[i]->n_ineq; ++k)
                if (ineq_i[k] == STATUS_ADJ_EQ)
                        break;
 
-       changed = is_extension(map, i, j, k, tabs, eq_i, ineq_i, eq_j, ineq_j);
+       changed = is_adj_eq_extension(map, i, j, k, tabs,
+                                       eq_i, ineq_i, eq_j, ineq_j);
        if (changed)
                return changed;
 
@@ -974,7 +1202,8 @@ static int check_eq_adj_eq(struct isl_map *map, int i, int j,
 {
        int k;
        int changed = 0;
-       struct isl_mat *wraps = NULL;
+       struct isl_wraps wraps;
+       isl_mat *mat;
        struct isl_set *set_i = NULL;
        struct isl_set *set_j = NULL;
        struct isl_vec *bound = NULL;
@@ -989,11 +1218,12 @@ static int check_eq_adj_eq(struct isl_map *map, int i, int j,
 
        set_i = set_from_updated_bmap(map->p[i], tabs[i]);
        set_j = set_from_updated_bmap(map->p[j], tabs[j]);
-       wraps = isl_mat_alloc(map->ctx, 2 * (map->p[i]->n_eq + map->p[j]->n_eq) +
+       mat = isl_mat_alloc(map->ctx, 2 * (map->p[i]->n_eq + map->p[j]->n_eq) +
                                        map->p[i]->n_ineq + map->p[j]->n_ineq,
                                        1 + total);
+       wraps_init(&wraps, mat, map, i, j, eq_i, ineq_i, eq_j, ineq_j);
        bound = isl_vec_alloc(map->ctx, 1 + total);
-       if (!set_i || !set_j || !wraps || !bound)
+       if (!set_i || !set_j || !wraps.mat || !bound)
                goto error;
 
        if (k % 2 == 0)
@@ -1002,33 +1232,33 @@ static int check_eq_adj_eq(struct isl_map *map, int i, int j,
                isl_seq_cpy(bound->el, map->p[i]->eq[k / 2], 1 + total);
        isl_int_add_ui(bound->el[0], bound->el[0], 1);
 
-       isl_seq_cpy(wraps->row[0], bound->el, 1 + total);
-       wraps->n_row = 1;
+       isl_seq_cpy(wraps.mat->row[0], bound->el, 1 + total);
+       wraps.mat->n_row = 1;
 
-       if (add_wraps(wraps, map->p[j], tabs[j], bound->el, set_i) < 0)
+       if (add_wraps(&wraps, map->p[j], tabs[j], bound->el, set_i) < 0)
                goto error;
-       if (!wraps->n_row)
+       if (!wraps.mat->n_row)
                goto unbounded;
 
        isl_int_sub_ui(bound->el[0], bound->el[0], 1);
        isl_seq_neg(bound->el, bound->el, 1 + total);
 
-       isl_seq_cpy(wraps->row[wraps->n_row], bound->el, 1 + total);
-       wraps->n_row++;
+       isl_seq_cpy(wraps.mat->row[wraps.mat->n_row], bound->el, 1 + total);
+       wraps.mat->n_row++;
 
-       if (add_wraps(wraps, map->p[i], tabs[i], bound->el, set_j) < 0)
+       if (add_wraps(&wraps, map->p[i], tabs[i], bound->el, set_j) < 0)
                goto error;
-       if (!wraps->n_row)
+       if (!wraps.mat->n_row)
                goto unbounded;
 
-       changed = fuse(map, i, j, tabs, eq_i, ineq_i, eq_j, ineq_j, wraps);
+       changed = fuse(map, i, j, tabs, eq_i, ineq_i, eq_j, ineq_j, wraps.mat);
 
        if (0) {
 error:         changed = -1;
        }
 unbounded:
 
-       isl_mat_free(wraps);
+       wraps_free(&wraps);
        isl_set_free(set_i);
        isl_set_free(set_j);
        isl_vec_free(bound);
@@ -1040,6 +1270,7 @@ unbounded:
  * can be represented by a single basic map.
  * If so, replace the pair by the single basic map and return 1.
  * Otherwise, return 0;
+ * The two basic maps are assumed to live in the same local space.
  *
  * We first check the effect of each constraint of one basic map
  * on the other basic map.
@@ -1075,7 +1306,15 @@ unbounded:
  *             => the pair can be replaced by a basic map consisting
  *                of the valid constraints in both basic maps
  *
- *     4. there is a single adjacent pair of an inequality and an equality,
+ *     4. one basic map has a single adjacent inequality, while the other
+ *        constraints are "valid".  The other basic map has some
+ *        "cut" constraints, but replacing the adjacent inequality by
+ *        its opposite and adding the valid constraints of the other
+ *        basic map results in a subset of the other basic map
+ *             => the pair can be replaced by a basic map consisting
+ *                of the valid constraints in both basic maps
+ *
+ *     5. there is a single adjacent pair of an inequality and an equality,
  *        the other constraints of the basic map containing the inequality are
  *        "valid".  Moreover, if the inequality the basic map is relaxed
  *        and then turned into an equality, then resulting facet lies
@@ -1083,7 +1322,7 @@ unbounded:
  *             => the pair can be replaced by the basic map containing
  *                the inequality, with the inequality relaxed.
  *
- *     5. there is a single adjacent pair of an inequality and an equality,
+ *     6. there is a single adjacent pair of an inequality and an equality,
  *        the other constraints of the basic map containing the inequality are
  *        "valid".  Moreover, the facets corresponding to both
  *        the inequality and the equality can be wrapped around their
@@ -1092,7 +1331,7 @@ unbounded:
  *                of the valid constraints in both basic maps together
  *                with all wrapping constraints
  *
- *     6. one of the basic maps extends beyond the other by at most one.
+ *     7. one of the basic maps extends beyond the other by at most one.
  *        Moreover, the facets corresponding to the cut constraints and
  *        the pieces of the other basic map at offset one from these cut
  *        constraints can be wrapped around their ridges to include
@@ -1101,7 +1340,7 @@ unbounded:
  *                of the valid constraints in both basic maps together
  *                with all wrapping constraints
  *
- *     7. the two basic maps live in adjacent hyperplanes.  In principle
+ *     8. the two basic maps live in adjacent hyperplanes.  In principle
  *        such sets can always be combined through wrapping, but we impose
  *        that there is only one such pair, to avoid overeager coalescing.
  *
@@ -1109,7 +1348,7 @@ unbounded:
  * corresponding to the basic maps.  When the basic maps are dropped
  * or combined, the tableaus are modified accordingly.
  */
-static int coalesce_pair(struct isl_map *map, int i, int j,
+static int coalesce_local_pair(__isl_keep isl_map *map, int i, int j,
        struct isl_tab **tabs)
 {
        int changed = 0;
@@ -1118,7 +1357,7 @@ static int coalesce_pair(struct isl_map *map, int i, int j,
        int *ineq_i = NULL;
        int *ineq_j = NULL;
 
-       eq_i = eq_status_in(map, i, j, tabs);
+       eq_i = eq_status_in(map->p[i], tabs[j]);
        if (!eq_i)
                goto error;
        if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_ERROR))
@@ -1126,7 +1365,7 @@ static int coalesce_pair(struct isl_map *map, int i, int j,
        if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_SEPARATE))
                goto done;
 
-       eq_j = eq_status_in(map, j, i, tabs);
+       eq_j = eq_status_in(map->p[j], tabs[i]);
        if (!eq_j)
                goto error;
        if (any(eq_j, 2 * map->p[j]->n_eq, STATUS_ERROR))
@@ -1134,7 +1373,7 @@ static int coalesce_pair(struct isl_map *map, int i, int j,
        if (any(eq_j, 2 * map->p[j]->n_eq, STATUS_SEPARATE))
                goto done;
 
-       ineq_i = ineq_status_in(map, i, j, tabs);
+       ineq_i = ineq_status_in(map->p[i], tabs[i], tabs[j]);
        if (!ineq_i)
                goto error;
        if (any(ineq_i, map->p[i]->n_ineq, STATUS_ERROR))
@@ -1142,7 +1381,7 @@ static int coalesce_pair(struct isl_map *map, int i, int j,
        if (any(ineq_i, map->p[i]->n_ineq, STATUS_SEPARATE))
                goto done;
 
-       ineq_j = ineq_status_in(map, j, i, tabs);
+       ineq_j = ineq_status_in(map->p[j], tabs[j], tabs[i]);
        if (!ineq_j)
                goto error;
        if (any(ineq_j, map->p[j]->n_ineq, STATUS_ERROR))
@@ -1174,10 +1413,8 @@ static int coalesce_pair(struct isl_map *map, int i, int j,
                /* BAD ADJ INEQ */
        } else if (any(ineq_i, map->p[i]->n_ineq, STATUS_ADJ_INEQ) ||
                   any(ineq_j, map->p[j]->n_ineq, STATUS_ADJ_INEQ)) {
-               if (!any(eq_i, 2 * map->p[i]->n_eq, STATUS_CUT) &&
-                   !any(eq_j, 2 * map->p[j]->n_eq, STATUS_CUT))
-                       changed = check_adj_ineq(map, i, j, tabs,
-                                                ineq_i, ineq_j);
+               changed = check_adj_ineq(map, i, j, tabs,
+                                       eq_i, ineq_i, eq_j, ineq_j);
        } else {
                if (!any(eq_i, 2 * map->p[i]->n_eq, STATUS_CUT) &&
                    !any(eq_j, 2 * map->p[j]->n_eq, STATUS_CUT))
@@ -1201,6 +1438,190 @@ error:
        return -1;
 }
 
+/* Do the two basic maps live in the same local space, i.e.,
+ * do they have the same (known) divs?
+ * If either basic map has any unknown divs, then we can only assume
+ * that they do not live in the same local space.
+ */
+static int same_divs(__isl_keep isl_basic_map *bmap1,
+       __isl_keep isl_basic_map *bmap2)
+{
+       int i;
+       int known;
+       int total;
+
+       if (!bmap1 || !bmap2)
+               return -1;
+       if (bmap1->n_div != bmap2->n_div)
+               return 0;
+
+       if (bmap1->n_div == 0)
+               return 1;
+
+       known = isl_basic_map_divs_known(bmap1);
+       if (known < 0 || !known)
+               return known;
+       known = isl_basic_map_divs_known(bmap2);
+       if (known < 0 || !known)
+               return known;
+
+       total = isl_basic_map_total_dim(bmap1);
+       for (i = 0; i < bmap1->n_div; ++i)
+               if (!isl_seq_eq(bmap1->div[i], bmap2->div[i], 2 + total))
+                       return 0;
+
+       return 1;
+}
+
+/* Given two basic maps "i" and "j", where the divs of "i" form a subset
+ * of those of "j", check if basic map "j" is a subset of basic map "i"
+ * and, if so, drop basic map "j".
+ *
+ * We first expand the divs of basic map "i" to match those of basic map "j",
+ * using the divs and expansion computed by the caller.
+ * Then we check if all constraints of the expanded "i" are valid for "j".
+ */
+static int coalesce_subset(__isl_keep isl_map *map, int i, int j,
+       struct isl_tab **tabs, __isl_keep isl_mat *div, int *exp)
+{
+       isl_basic_map *bmap;
+       int changed = 0;
+       int *eq_i = NULL;
+       int *ineq_i = NULL;
+
+       bmap = isl_basic_map_copy(map->p[i]);
+       bmap = isl_basic_set_expand_divs(bmap, isl_mat_copy(div), exp);
+
+       if (!bmap)
+               goto error;
+
+       eq_i = eq_status_in(bmap, tabs[j]);
+       if (!eq_i)
+               goto error;
+       if (any(eq_i, 2 * bmap->n_eq, STATUS_ERROR))
+               goto error;
+       if (any(eq_i, 2 * bmap->n_eq, STATUS_SEPARATE))
+               goto done;
+
+       ineq_i = ineq_status_in(bmap, NULL, tabs[j]);
+       if (!ineq_i)
+               goto error;
+       if (any(ineq_i, bmap->n_ineq, STATUS_ERROR))
+               goto error;
+       if (any(ineq_i, bmap->n_ineq, STATUS_SEPARATE))
+               goto done;
+
+       if (all(eq_i, 2 * map->p[i]->n_eq, STATUS_VALID) &&
+           all(ineq_i, map->p[i]->n_ineq, STATUS_VALID)) {
+               drop(map, j, tabs);
+               changed = 1;
+       }
+
+done:
+       isl_basic_map_free(bmap);
+       free(eq_i);
+       free(ineq_i);
+       return 0;
+error:
+       isl_basic_map_free(bmap);
+       free(eq_i);
+       free(ineq_i);
+       return -1;
+}
+
+/* Check if the basic map "j" is a subset of basic map "i",
+ * assuming that "i" has fewer divs that "j".
+ * If not, then we change the order.
+ *
+ * If the two basic maps have the same number of divs, then
+ * they must necessarily be different.  Otherwise, we would have
+ * called coalesce_local_pair.  We therefore don't do try anyhing
+ * in this case.
+ *
+ * We first check if the divs of "i" are all known and form a subset
+ * of those of "j".  If so, we pass control over to coalesce_subset.
+ */
+static int check_coalesce_subset(__isl_keep isl_map *map, int i, int j,
+       struct isl_tab **tabs)
+{
+       int known;
+       isl_mat *div_i, *div_j, *div;
+       int *exp1 = NULL;
+       int *exp2 = NULL;
+       isl_ctx *ctx;
+       int subset;
+
+       if (map->p[i]->n_div == map->p[j]->n_div)
+               return 0;
+       if (map->p[j]->n_div < map->p[i]->n_div)
+               return check_coalesce_subset(map, j, i, tabs);
+
+       known = isl_basic_map_divs_known(map->p[i]);
+       if (known < 0 || !known)
+               return known;
+
+       ctx = isl_map_get_ctx(map);
+
+       div_i = isl_basic_map_get_divs(map->p[i]);
+       div_j = isl_basic_map_get_divs(map->p[j]);
+
+       if (!div_i || !div_j)
+               goto error;
+
+       exp1 = isl_alloc_array(ctx, int, div_i->n_row);
+       exp2 = isl_alloc_array(ctx, int, div_j->n_row);
+       if (!exp1 || !exp2)
+               goto error;
+
+       div = isl_merge_divs(div_i, div_j, exp1, exp2);
+       if (!div)
+               goto error;
+
+       if (div->n_row == div_j->n_row)
+               subset = coalesce_subset(map, i, j, tabs, div, exp1);
+       else
+               subset = 0;
+
+       isl_mat_free(div);
+
+       isl_mat_free(div_i);
+       isl_mat_free(div_j);
+
+       free(exp2);
+       free(exp1);
+
+       return subset;
+error:
+       isl_mat_free(div_i);
+       isl_mat_free(div_j);
+       free(exp1);
+       free(exp2);
+       return -1;
+}
+
+/* Check if the union of the given pair of basic maps
+ * can be represented by a single basic map.
+ * If so, replace the pair by the single basic map and return 1.
+ * Otherwise, return 0;
+ *
+ * We first check if the two basic maps live in the same local space.
+ * If so, we do the complete check.  Otherwise, we check if one is
+ * an obvious subset of the other.
+ */
+static int coalesce_pair(__isl_keep isl_map *map, int i, int j,
+       struct isl_tab **tabs)
+{
+       int same;
+
+       same = same_divs(map->p[i], map->p[j]);
+       if (same < 0)
+               return -1;
+       if (same)
+               return coalesce_local_pair(map, i, j, tabs);
+
+       return check_coalesce_subset(map, i, j, tabs);
+}
+
 static struct isl_map *coalesce(struct isl_map *map, struct isl_tab **tabs)
 {
        int i, j;
@@ -1224,6 +1645,16 @@ error:
 /* For each pair of basic maps in the map, check if the union of the two
  * can be represented by a single basic map.
  * If so, replace the pair by the single basic map and start over.
+ *
+ * Since we are constructing the tableaus of the basic maps anyway,
+ * we exploit them to detect implicit equalities and redundant constraints.
+ * This also helps the coalescing as it can ignore the redundant constraints.
+ * In order to avoid confusion, we make all implicit equalities explicit
+ * in the basic maps.  We don't call isl_basic_map_gauss, though,
+ * as that may affect the number of constraints.
+ * This means that we have to call isl_basic_map_gauss at the end
+ * of the computation to ensure that the basic maps are not left
+ * in an unexpected state.
  */
 struct isl_map *isl_map_coalesce(struct isl_map *map)
 {
@@ -1238,7 +1669,8 @@ struct isl_map *isl_map_coalesce(struct isl_map *map)
        if (map->n <= 1)
                return map;
 
-       map = isl_map_align_divs(map);
+       map = isl_map_sort_divs(map);
+       map = isl_map_cow(map);
 
        tabs = isl_calloc_array(map->ctx, struct isl_tab *, map->n);
        if (!tabs)
@@ -1252,6 +1684,10 @@ struct isl_map *isl_map_coalesce(struct isl_map *map)
                if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_NO_IMPLICIT))
                        if (isl_tab_detect_implicit_equalities(tabs[i]) < 0)
                                goto error;
+               map->p[i] = isl_tab_make_equalities_explicit(tabs[i],
+                                                               map->p[i]);
+               if (!map->p[i])
+                       goto error;
                if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_NO_REDUNDANT))
                        if (isl_tab_detect_redundant(tabs[i]) < 0)
                                goto error;
@@ -1266,6 +1702,7 @@ struct isl_map *isl_map_coalesce(struct isl_map *map)
                for (i = 0; i < map->n; ++i) {
                        map->p[i] = isl_basic_map_update_from_tab(map->p[i],
                                                                    tabs[i]);
+                       map->p[i] = isl_basic_map_gauss(map->p[i], NULL);
                        map->p[i] = isl_basic_map_finalize(map->p[i]);
                        if (!map->p[i])
                                goto error;