Merge branch 'maint'
[platform/upstream/isl.git] / isl_coalesce.c
index bb56261..22524aa 100644 (file)
@@ -11,8 +11,9 @@
  */
 
 #include "isl_map_private.h"
-#include "isl_seq.h"
+#include <isl/seq.h>
 #include "isl_tab.h"
+#include <isl_mat_private.h>
 
 #define STATUS_ERROR           -1
 #define STATUS_REDUNDANT        1
@@ -26,6 +27,7 @@ static int status_in(isl_int *ineq, struct isl_tab *tab)
 {
        enum isl_ineq_type type = isl_tab_ineq_type(tab, ineq);
        switch (type) {
+       default:
        case isl_ineq_error:            return STATUS_ERROR;
        case isl_ineq_redundant:        return STATUS_VALID;
        case isl_ineq_separate:         return STATUS_SEPARATE;
@@ -269,7 +271,8 @@ static int check_facets(struct isl_map *map, int i, int j,
        for (k = 0; k < map->p[i]->n_ineq; ++k) {
                if (ineq_i[k] != STATUS_CUT)
                        continue;
-               tabs[i] = isl_tab_select_facet(tabs[i], n_eq + k);
+               if (isl_tab_select_facet(tabs[i], n_eq + k) < 0)
+                       return -1;
                for (l = 0; l < map->p[j]->n_ineq; ++l) {
                        int stat;
                        if (ineq_j[l] != STATUS_CUT)
@@ -394,7 +397,8 @@ static int is_extension(struct isl_map *map, int i, int j, int k,
        snap = isl_tab_snap(tabs[i]);
        tabs[i] = isl_tab_relax(tabs[i], n_eq + k);
        snap2 = isl_tab_snap(tabs[i]);
-       tabs[i] = isl_tab_select_facet(tabs[i], n_eq + k);
+       if (isl_tab_select_facet(tabs[i], n_eq + k) < 0)
+               return -1;
        super = contains(map, j, ineq_j, tabs[i]);
        if (super) {
                if (isl_tab_rollback(tabs[i], snap2) < 0)
@@ -477,6 +481,50 @@ unbounded:
        return 0;
 }
 
+/* Check if the constraints in "wraps" from "first" until the last
+ * are all valid for the basic set represented by "tab".
+ * If not, wraps->n_row is set to zero.
+ */
+static int check_wraps(__isl_keep isl_mat *wraps, int first,
+       struct isl_tab *tab)
+{
+       int i;
+
+       for (i = first; i < wraps->n_row; ++i) {
+               enum isl_ineq_type type;
+               type = isl_tab_ineq_type(tab, wraps->row[i]);
+               if (type == isl_ineq_error)
+                       return -1;
+               if (type == isl_ineq_redundant)
+                       continue;
+               wraps->n_row = 0;
+               return 0;
+       }
+
+       return 0;
+}
+
+/* Return a set that corresponds to the non-redudant constraints
+ * (as recorded in tab) of bmap.
+ *
+ * It's important to remove the redundant constraints as some
+ * of the other constraints may have been modified after the
+ * constraints were marked redundant.
+ * In particular, a constraint may have been relaxed.
+ * Redundant constraints are ignored when a constraint is relaxed
+ * and should therefore continue to be ignored ever after.
+ * Otherwise, the relaxation might be thwarted by some of
+ * these constraints.
+ */
+static __isl_give isl_set *set_from_updated_bmap(__isl_keep isl_basic_map *bmap,
+       struct isl_tab *tab)
+{
+       bmap = isl_basic_map_copy(bmap);
+       bmap = isl_basic_map_cow(bmap);
+       bmap = isl_basic_map_update_from_tab(bmap, tab);
+       return isl_set_from_basic_set(isl_basic_map_underlying_set(bmap));
+}
+
 /* Given a basic set i with a constraint k that is adjacent to either the
  * whole of basic set j or a facet of basic set j, check if we can wrap
  * both the facet corresponding to k and the facet of j (or the whole of j)
@@ -485,11 +533,14 @@ unbounded:
  *
  * All constraints of i (except k) are assumed to be valid for j.
  *
+ * However, the constraints of j may not be valid for i and so
+ * we have to check that the wrapping constraints for j are valid for i.
+ *
  * In the case where j has a facet adjacent to i, tab[j] is assumed
  * to have been restricted to this facet, so that the non-redundant
  * constraints in tab[j] are the ridges of the facet.
  * Note that for the purpose of wrapping, it does not matter whether
- * we wrap the ridges of i aronud the whole of j or just around
+ * we wrap the ridges of i around the whole of j or just around
  * the facet since all the other constraints are assumed to be valid for j.
  * In practice, we wrap to include the whole of j.
  *        ____                   _____
@@ -510,13 +561,10 @@ static int can_wrap_in_facet(struct isl_map *map, int i, int j, int k,
        struct isl_vec *bound = NULL;
        unsigned total = isl_basic_map_total_dim(map->p[i]);
        struct isl_tab_undo *snap;
+       int n;
 
-       snap = isl_tab_snap(tabs[i]);
-
-       set_i = isl_set_from_basic_set(
-                   isl_basic_map_underlying_set(isl_basic_map_copy(map->p[i])));
-       set_j = isl_set_from_basic_set(
-                   isl_basic_map_underlying_set(isl_basic_map_copy(map->p[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) +
                                        map->p[i]->n_ineq + map->p[j]->n_ineq,
                                        1 + total);
@@ -535,25 +583,29 @@ static int can_wrap_in_facet(struct isl_map *map, int i, int j, int k,
        if (!wraps->n_row)
                goto unbounded;
 
-       tabs[i] = isl_tab_select_facet(tabs[i], map->p[i]->n_eq + k);
+       snap = isl_tab_snap(tabs[i]);
+
+       if (isl_tab_select_facet(tabs[i], map->p[i]->n_eq + k) < 0)
+               goto error;
        if (isl_tab_detect_redundant(tabs[i]) < 0)
                goto error;
 
        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)
                goto error;
+
+       if (isl_tab_rollback(tabs[i], snap) < 0)
+               goto error;
+       if (check_wraps(wraps, n, tabs[i]) < 0)
+               goto error;
        if (!wraps->n_row)
                goto unbounded;
 
        changed = fuse(map, i, j, tabs, eq_i, ineq_i, eq_j, ineq_j, wraps);
 
-       if (!changed) {
 unbounded:
-               if (isl_tab_rollback(tabs[i], snap) < 0)
-                       goto error;
-       }
-
        isl_mat_free(wraps);
 
        isl_set_free(set_i);
@@ -570,58 +622,245 @@ error:
        return -1;
 }
 
-/* Given two basic sets i and j such that i has exactly one cut constraint,
- * check if we can wrap the corresponding facet around its ridges to include
- * the other basic set (and nothing else).
+/* Set the is_redundant property of the "n" constraints in "cuts",
+ * except "k" to "v".
+ * This is a fairly tricky operation as it bypasses isl_tab.c.
+ * The reason we want to temporarily mark some constraints redundant
+ * is that we want to ignore them in add_wraps.
+ *
+ * Initially all cut constraints are non-redundant, but the
+ * selection of a facet right before the call to this function
+ * may have made some of them redundant.
+ * Likewise, the same constraints are marked non-redundant
+ * in the second call to this function, before they are officially
+ * made non-redundant again in the subsequent rollback.
+ */
+static void set_is_redundant(struct isl_tab *tab, unsigned n_eq,
+       int *cuts, int n, int k, int v)
+{
+       int l;
+
+       for (l = 0; l < n; ++l) {
+               if (l == k)
+                       continue;
+               tab->con[n_eq + cuts[l]].is_redundant = v;
+       }
+}
+
+/* Given a pair of basic maps i and j such that j sticks out
+ * of i at n cut constraints, each time by at most one,
+ * try to compute wrapping constraints and replace the two
+ * basic maps by a single basic map.
+ * The other constraints of i are assumed to be valid for j.
+ *
+ * The facets of i corresponding to the cut constraints are
+ * wrapped around their ridges, except those ridges determined
+ * by any of the other cut constraints.
+ * The intersections of cut constraints need to be ignored
+ * as the result of wrapping one cut constraint around another
+ * would result in a constraint cutting the union.
+ * In each case, the facets are wrapped to include the union
+ * of the two basic maps.
+ *
+ * The pieces of j that lie at an offset of exactly one from
+ * one of the cut constraints of i are wrapped around their edges.
+ * Here, there is no need to ignore intersections because we
+ * are wrapping around the union of the two basic maps.
+ *
+ * If any wrapping fails, i.e., if we cannot wrap to touch
+ * the union, then we give up.
+ * Otherwise, the pair of basic maps is replaced by their union.
+ */
+static int wrap_in_facets(struct isl_map *map, int i, int j,
+       int *cuts, int n, struct isl_tab **tabs,
+       int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
+{
+       int changed = 0;
+       isl_mat *wraps = NULL;
+       isl_set *set = NULL;
+       isl_vec *bound = NULL;
+       unsigned total = isl_basic_map_total_dim(map->p[i]);
+       int max_wrap;
+       int k;
+       struct isl_tab_undo *snap_i, *snap_j;
+
+       if (isl_tab_extend_cons(tabs[j], 1) < 0)
+               goto error;
+
+       max_wrap = 2 * (map->p[i]->n_eq + map->p[j]->n_eq) +
+                   map->p[i]->n_ineq + map->p[j]->n_ineq;
+       max_wrap *= n;
+
+       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);
+       bound = isl_vec_alloc(map->ctx, 1 + total);
+       if (!set || !wraps || !bound)
+               goto error;
+
+       snap_i = isl_tab_snap(tabs[i]);
+       snap_j = isl_tab_snap(tabs[j]);
+
+       wraps->n_row = 0;
+
+       for (k = 0; k < n; ++k) {
+               if (isl_tab_select_facet(tabs[i], map->p[i]->n_eq + cuts[k]) < 0)
+                       goto error;
+               if (isl_tab_detect_redundant(tabs[i]) < 0)
+                       goto error;
+               set_is_redundant(tabs[i], map->p[i]->n_eq, cuts, n, k, 1);
+
+               isl_seq_neg(bound->el, map->p[i]->ineq[cuts[k]], 1 + total);
+               if (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);
+               if (isl_tab_rollback(tabs[i], snap_i) < 0)
+                       goto error;
+
+               if (!wraps->n_row)
+                       break;
+
+               isl_seq_cpy(bound->el, map->p[i]->ineq[cuts[k]], 1 + total);
+               isl_int_add_ui(bound->el[0], bound->el[0], 1);
+               if (isl_tab_add_eq(tabs[j], bound->el) < 0)
+                       goto error;
+               if (isl_tab_detect_redundant(tabs[j]) < 0)
+                       goto error;
+
+               if (!tabs[j]->empty &&
+                   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)
+                       break;
+       }
+
+       if (k == n)
+               changed = fuse(map, i, j, tabs,
+                               eq_i, ineq_i, eq_j, ineq_j, wraps);
+
+       isl_vec_free(bound);
+       isl_mat_free(wraps);
+       isl_set_free(set);
+
+       return changed;
+error:
+       isl_vec_free(bound);
+       isl_mat_free(wraps);
+       isl_set_free(set);
+       return -1;
+}
+
+/* Given two basic sets i and j such that i has no cut equalities,
+ * check if relaxing all the cut inequalities of i by one turns
+ * them into valid constraint for j and check if we can wrap in
+ * the bits that are sticking out.
  * If so, replace the pair by their union.
  *
- * We first check if j has a facet adjacent to the cut constraint of i.
- * If so, we try to wrap in the facet.
+ * We first check if all relaxed cut inequalities of i are valid for j
+ * and then try to wrap in the intersections of the relaxed cut inequalities
+ * with j.
+ *
+ * During this wrapping, we consider the points of j that lie at a distance
+ * of exactly 1 from i.  In particular, we ignore the points that lie in
+ * between this lower-dimensional space and the basic map i.
+ * We can therefore only apply this to integer maps.
  *        ____                   _____
  *       / ___|_                /     \
  *      / |    |               /      |
  *      \ |    |       =>      \      |
  *       \|____|                \     |
  *        \___|                  \____/
+ *
+ *      _____                   ______
+ *     | ____|_                |      \
+ *     | |     |               |       |
+ *     | |     |       =>      |       |
+ *     |_|     |               |       |
+ *       |_____|                \______|
+ *
+ *      _______
+ *     |       |
+ *     |  |\   |
+ *     |  | \  |
+ *     |  |  \ |
+ *     |  |   \|
+ *     |  |    \
+ *     |  |_____\
+ *     |       |
+ *     |_______|
+ *
+ * Wrapping can fail if the result of wrapping one of the facets
+ * around its edges does not produce any new facet constraint.
+ * In particular, this happens when we try to wrap in unbounded sets.
+ *
+ *      _______________________________________________________________________
+ *     |
+ *     |  ___
+ *     | |   |
+ *     |_|   |_________________________________________________________________
+ *       |___|
+ *
+ * The following is not an acceptable result of coalescing the above two
+ * sets as it includes extra integer points.
+ *      _______________________________________________________________________
+ *     |
+ *     |     
+ *     |      
+ *     |
+ *      \______________________________________________________________________
  */
 static int can_wrap_in_set(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 k, l;
-       unsigned total = isl_basic_map_total_dim(map->p[i]);
-       struct isl_tab_undo *snap;
+       int k, m;
+       int n;
+       int *cuts = NULL;
 
-       for (k = 0; k < map->p[i]->n_ineq; ++k)
-               if (ineq_i[k] == STATUS_CUT)
-                       break;
+       if (ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_RATIONAL) ||
+           ISL_F_ISSET(map->p[j], ISL_BASIC_MAP_RATIONAL))
+               return 0;
+
+       n = count(ineq_i, map->p[i]->n_ineq, STATUS_CUT);
+       if (n == 0)
+               return 0;
+
+       cuts = isl_alloc_array(map->ctx, int, n);
+       if (!cuts)
+               return -1;
 
-       isl_assert(map->ctx, k < map->p[i]->n_ineq, return -1);
+       for (k = 0, m = 0; m < n; ++k) {
+               enum isl_ineq_type type;
 
-       isl_int_add_ui(map->p[i]->ineq[k][0], map->p[i]->ineq[k][0], 1);
-       for (l = 0; l < map->p[j]->n_ineq; ++l) {
-               if (isl_tab_is_redundant(tabs[j], map->p[j]->n_eq + l))
+               if (ineq_i[k] != STATUS_CUT)
                        continue;
-               if (isl_seq_eq(map->p[i]->ineq[k],
-                               map->p[j]->ineq[l], 1 + total))
+
+               isl_int_add_ui(map->p[i]->ineq[k][0], map->p[i]->ineq[k][0], 1);
+               type = isl_tab_ineq_type(tabs[j], map->p[i]->ineq[k]);
+               isl_int_sub_ui(map->p[i]->ineq[k][0], map->p[i]->ineq[k][0], 1);
+               if (type == isl_ineq_error)
+                       goto error;
+               if (type != isl_ineq_redundant)
                        break;
+               cuts[m] = k;
+               ++m;
        }
-       isl_int_sub_ui(map->p[i]->ineq[k][0], map->p[i]->ineq[k][0], 1);
-
-       if (l >= map->p[j]->n_ineq)
-               return 0;
 
-       snap = isl_tab_snap(tabs[j]);
-       tabs[j] = isl_tab_select_facet(tabs[j], map->p[j]->n_eq + l);
-       if (isl_tab_detect_redundant(tabs[j]) < 0)
-               return -1;
-
-       changed = can_wrap_in_facet(map, i, j, k, tabs, NULL, ineq_i, NULL, ineq_j);
+       if (m == n)
+               changed = wrap_in_facets(map, i, j, cuts, n, tabs,
+                                        eq_i, ineq_i, eq_j, ineq_j);
 
-       if (!changed && isl_tab_rollback(tabs[j], snap) < 0)
-               return -1;
+       free(cuts);
 
        return changed;
+error:
+       free(cuts);
+       return -1;
 }
 
 /* Check if either i or j has a single cut constraint that can
@@ -629,17 +868,19 @@ static int can_wrap_in_set(struct isl_map *map, int i, int j,
  * if so, replace the pair by their union.
  */
 static int check_wrap(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;
 
-       if (count(ineq_i, map->p[i]->n_ineq, STATUS_CUT) == 1)
-               changed = can_wrap_in_set(map, i, j, tabs, ineq_i, ineq_j);
+       if (!any(eq_i, 2 * map->p[i]->n_eq, STATUS_CUT))
+               changed = can_wrap_in_set(map, i, j, tabs,
+                                           eq_i, ineq_i, eq_j, ineq_j);
        if (changed)
                return changed;
 
-       if (count(ineq_j, map->p[j]->n_ineq, STATUS_CUT) == 1)
-               changed = can_wrap_in_set(map, j, i, tabs, ineq_j, ineq_i);
+       if (!any(eq_j, 2 * map->p[j]->n_eq, STATUS_CUT))
+               changed = can_wrap_in_set(map, j, i, tabs,
+                                           eq_j, ineq_j, eq_i, ineq_i);
        return changed;
 }
 
@@ -649,7 +890,7 @@ static int check_wrap(struct isl_map *map, int i, int j,
  * inequality adjacent to an equality.
  * We call the basic map that has the inequality "i" and the basic
  * map that has the equality "j".
- * If "i" has any "cut" inequality, then relaxing the inequality
+ * If "i" has any "cut" (in)equality, then relaxing the inequality
  * by one would not result in a basic map that contains the other
  * basic map.
  */
@@ -670,6 +911,8 @@ static int check_adj_eq(struct isl_map *map, int i, int j,
 
        /* j has an equality adjacent to an inequality in i */
 
+       if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_CUT))
+               return 0;
        if (any(ineq_i, map->p[i]->n_ineq, STATUS_CUT))
                /* ADJ EQ CUT */
                return 0;
@@ -694,6 +937,100 @@ static int check_adj_eq(struct isl_map *map, int i, int j,
        return changed;
 }
 
+/* The two basic maps lie on adjacent hyperplanes.  In particular,
+ * basic map "i" has an equality that lies parallel to basic map "j".
+ * Check if we can wrap the facets around the parallel hyperplanes
+ * to include the other set.
+ *
+ * We perform basically the same operations as can_wrap_in_facet,
+ * except that we don't need to select a facet of one of the sets.
+ *                             _
+ *     \\                      \\
+ *      \\             =>       \\
+ *       \                       \|
+ *
+ * We only allow one equality of "i" to be adjacent to an equality of "j"
+ * to avoid coalescing
+ *
+ *     [m, n] -> { [x, y] -> [x, 1 + y] : x >= 1 and y >= 1 and
+ *                                         x <= 10 and y <= 10;
+ *                 [x, y] -> [1 + x, y] : x >= 1 and x <= 20 and
+ *                                         y >= 5 and y <= 15 }
+ *
+ * to
+ *
+ *     [m, n] -> { [x, y] -> [x2, y2] : x >= 1 and 10y2 <= 20 - x + 10y and
+ *                                     4y2 >= 5 + 3y and 5y2 <= 15 + 4y and
+ *                                     y2 <= 1 + x + y - x2 and y2 >= y and
+ *                                     y2 >= 1 + x + y - x2 }
+ */
+static int check_eq_adj_eq(struct 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;
+       int changed = 0;
+       struct isl_mat *wraps = NULL;
+       struct isl_set *set_i = NULL;
+       struct isl_set *set_j = NULL;
+       struct isl_vec *bound = NULL;
+       unsigned total = isl_basic_map_total_dim(map->p[i]);
+
+       if (count(eq_i, 2 * map->p[i]->n_eq, STATUS_ADJ_EQ) != 1)
+               return 0;
+
+       for (k = 0; k < 2 * map->p[i]->n_eq ; ++k)
+               if (eq_i[k] == STATUS_ADJ_EQ)
+                       break;
+
+       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) +
+                                       map->p[i]->n_ineq + map->p[j]->n_ineq,
+                                       1 + total);
+       bound = isl_vec_alloc(map->ctx, 1 + total);
+       if (!set_i || !set_j || !wraps || !bound)
+               goto error;
+
+       if (k % 2 == 0)
+               isl_seq_neg(bound->el, map->p[i]->eq[k / 2], 1 + total);
+       else
+               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;
+
+       if (add_wraps(wraps, map->p[j], tabs[j], bound->el, set_i) < 0)
+               goto error;
+       if (!wraps->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++;
+
+       if (add_wraps(wraps, map->p[i], tabs[i], bound->el, set_j) < 0)
+               goto error;
+       if (!wraps->n_row)
+               goto unbounded;
+
+       changed = fuse(map, i, j, tabs, eq_i, ineq_i, eq_j, ineq_j, wraps);
+
+       if (0) {
+error:         changed = -1;
+       }
+unbounded:
+
+       isl_mat_free(wraps);
+       isl_set_free(set_i);
+       isl_set_free(set_j);
+       isl_vec_free(bound);
+
+       return changed;
+}
+
 /* 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.
@@ -716,7 +1053,7 @@ static int check_adj_eq(struct isl_map *map, int i, int j,
  *     adj_ineq        the given constraint is adjacent (on the outside)
  *                     to an inequality of the other basic map
  *
- * We consider six cases in which we can replace the pair by a single
+ * We consider seven cases in which we can replace the pair by a single
  * basic map.  We ignore all "redundant" constraints.
  *
  *     1. all constraints of one basic map are valid
@@ -750,14 +1087,19 @@ static int check_adj_eq(struct isl_map *map, int i, int j,
  *                of the valid constraints in both basic maps together
  *                with all wrapping constraints
  *
- *     6. one of the basic maps has a single cut constraint and
- *        the other basic map has a constraint adjacent to this constraint.
- *        Moreover, the facets corresponding to both constraints
- *        can be wrapped around their ridges to include the other basic map
+ *     6. 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
+ *        the union of the two basic maps
  *             => the pair can be replaced by a basic map consisting
  *                of the valid constraints in both basic maps together
  *                with all wrapping constraints
  *
+ *     7. 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.
+ *
  * Throughout the computation, we maintain a collection of tableaus
  * corresponding to the basic maps.  When the basic maps are dropped
  * or combined, the tableaus are modified accordingly.
@@ -772,24 +1114,32 @@ static int coalesce_pair(struct isl_map *map, int i, int j,
        int *ineq_j = NULL;
 
        eq_i = eq_status_in(map, i, j, tabs);
+       if (!eq_i)
+               goto error;
        if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_ERROR))
                goto error;
        if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_SEPARATE))
                goto done;
 
        eq_j = eq_status_in(map, j, i, tabs);
+       if (!eq_j)
+               goto error;
        if (any(eq_j, 2 * map->p[j]->n_eq, STATUS_ERROR))
                goto error;
        if (any(eq_j, 2 * map->p[j]->n_eq, STATUS_SEPARATE))
                goto done;
 
        ineq_i = ineq_status_in(map, i, j, tabs);
+       if (!ineq_i)
+               goto error;
        if (any(ineq_i, map->p[i]->n_ineq, STATUS_ERROR))
                goto error;
        if (any(ineq_i, map->p[i]->n_ineq, STATUS_SEPARATE))
                goto done;
 
        ineq_j = ineq_status_in(map, j, i, tabs);
+       if (!ineq_j)
+               goto error;
        if (any(ineq_j, map->p[j]->n_ineq, STATUS_ERROR))
                goto error;
        if (any(ineq_j, map->p[j]->n_ineq, STATUS_SEPARATE))
@@ -803,12 +1153,12 @@ static int coalesce_pair(struct isl_map *map, int i, int j,
                   all(ineq_j, map->p[j]->n_ineq, STATUS_VALID)) {
                drop(map, i, tabs);
                changed = 1;
-       } else if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_CUT) ||
-                  any(eq_j, 2 * map->p[j]->n_eq, STATUS_CUT)) {
-               /* BAD CUT */
-       } else if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_ADJ_EQ) ||
-                  any(eq_j, 2 * map->p[j]->n_eq, STATUS_ADJ_EQ)) {
-               /* ADJ EQ PAIR */
+       } else if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_ADJ_EQ)) {
+               changed = check_eq_adj_eq(map, i, j, tabs,
+                                       eq_i, ineq_i, eq_j, ineq_j);
+       } else if (any(eq_j, 2 * map->p[j]->n_eq, STATUS_ADJ_EQ)) {
+               changed = check_eq_adj_eq(map, j, i, tabs,
+                                       eq_j, ineq_j, eq_i, ineq_i);
        } else if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_ADJ_INEQ) ||
                   any(eq_j, 2 * map->p[j]->n_eq, STATUS_ADJ_INEQ)) {
                changed = check_adj_eq(map, i, j, tabs,
@@ -819,11 +1169,17 @@ 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)) {
-               changed = check_adj_ineq(map, i, j, tabs, ineq_i, ineq_j);
+               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);
        } else {
-               changed = check_facets(map, i, j, tabs, ineq_i, ineq_j);
+               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_facets(map, i, j, tabs, ineq_i, ineq_j);
                if (!changed)
-                       changed = check_wrap(map, i, j, tabs, ineq_i, ineq_j);
+                       changed = check_wrap(map, i, j, tabs,
+                                               eq_i, ineq_i, eq_j, ineq_j);
        }
 
 done:
@@ -844,14 +1200,15 @@ static struct isl_map *coalesce(struct isl_map *map, struct isl_tab **tabs)
 {
        int i, j;
 
-       for (i = 0; i < map->n - 1; ++i)
+       for (i = map->n - 2; i >= 0; --i)
+restart:
                for (j = i + 1; j < map->n; ++j) {
                        int changed;
                        changed = coalesce_pair(map, i, j, tabs);
                        if (changed < 0)
                                goto error;
                        if (changed)
-                               return coalesce(map, tabs);
+                               goto restart;
                }
        return map;
 error:
@@ -887,7 +1244,8 @@ struct isl_map *isl_map_coalesce(struct isl_map *map)
                if (!tabs[i])
                        goto error;
                if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_NO_IMPLICIT))
-                       tabs[i] = isl_tab_detect_implicit_equalities(tabs[i]);
+                       if (isl_tab_detect_implicit_equalities(tabs[i]) < 0)
+                               goto error;
                if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_NO_REDUNDANT))
                        if (isl_tab_detect_redundant(tabs[i]) < 0)
                                goto error;