isl_map_transitive_closure: break early if input map doesn't compose with itself
[platform/upstream/isl.git] / isl_map_simplify.c
index 1a4bfa2..d4b7af9 100644 (file)
@@ -1,6 +1,16 @@
+/*
+ * Copyright 2008-2009 Katholieke Universiteit Leuven
+ *
+ * Use of this software is governed by the GNU LGPLv2.1 license
+ *
+ * Written by Sven Verdoolaege, K.U.Leuven, Departement
+ * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
+ */
+
 #include "isl_equalities.h"
 #include "isl_map.h"
 #include "isl_map_private.h"
+#include "isl_seq.h"
 #include "isl_tab.h"
 
 static void swap_equality(struct isl_basic_map *bmap, int a, int b)
@@ -232,6 +242,12 @@ error:
        return NULL;
 }
 
+struct isl_set *isl_set_drop(struct isl_set *set,
+       enum isl_dim_type type, unsigned first, unsigned n)
+{
+       return (isl_set *)isl_map_drop((isl_map *)set, type, first, n);
+}
+
 struct isl_map *isl_map_drop_inputs(
                struct isl_map *map, unsigned first, unsigned n)
 {
@@ -341,32 +357,72 @@ struct isl_basic_map *isl_basic_map_normalize_constraints(
 struct isl_basic_set *isl_basic_set_normalize_constraints(
        struct isl_basic_set *bset)
 {
-       (struct isl_basic_set *)isl_basic_map_normalize_constraints(
+       return (struct isl_basic_set *)isl_basic_map_normalize_constraints(
                (struct isl_basic_map *)bset);
 }
 
-static void eliminate_div(struct isl_basic_map *bmap, isl_int *eq, unsigned div)
+/* Assumes divs have been ordered if keep_divs is set.
+ */
+static void eliminate_var_using_equality(struct isl_basic_map *bmap,
+       unsigned pos, isl_int *eq, int keep_divs, int *progress)
 {
-       int i;
-       unsigned pos = 1 + isl_dim_total(bmap->dim) + div;
-       unsigned len;
-       len = 1 + isl_basic_map_total_dim(bmap);
+       unsigned total;
+       int k;
+       int last_div;
 
-       for (i = 0; i < bmap->n_eq; ++i)
-               if (bmap->eq[i] != eq)
-                       isl_seq_elim(bmap->eq[i], eq, pos, len, NULL);
+       total = isl_basic_map_total_dim(bmap);
+       last_div = isl_seq_last_non_zero(eq + 1 + isl_dim_total(bmap->dim),
+                                               bmap->n_div);
+       for (k = 0; k < bmap->n_eq; ++k) {
+               if (bmap->eq[k] == eq)
+                       continue;
+               if (isl_int_is_zero(bmap->eq[k][1+pos]))
+                       continue;
+               if (progress)
+                       *progress = 1;
+               isl_seq_elim(bmap->eq[k], eq, 1+pos, 1+total, NULL);
+       }
 
-       for (i = 0; i < bmap->n_ineq; ++i)
-               isl_seq_elim(bmap->ineq[i], eq, pos, len, NULL);
+       for (k = 0; k < bmap->n_ineq; ++k) {
+               if (isl_int_is_zero(bmap->ineq[k][1+pos]))
+                       continue;
+               if (progress)
+                       *progress = 1;
+               isl_seq_elim(bmap->ineq[k], eq, 1+pos, 1+total, NULL);
+               ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
+       }
 
-       /* We need to be careful about circular definitions,
-        * so for now we just remove the definitions of other divs that
-        * depend on this div and (possibly) recompute them later.
-        */
-       for (i = 0; i < bmap->n_div; ++i)
-               if (!isl_int_is_zero(bmap->div[i][0]) &&
-                   !isl_int_is_zero(bmap->div[i][1 + pos]))
-                       isl_seq_clr(bmap->div[i], 1 + len);
+       for (k = 0; k < bmap->n_div; ++k) {
+               if (isl_int_is_zero(bmap->div[k][0]))
+                       continue;
+               if (isl_int_is_zero(bmap->div[k][1+1+pos]))
+                       continue;
+               if (progress)
+                       *progress = 1;
+               /* We need to be careful about circular definitions,
+                * so for now we just remove the definition of div k
+                * if the equality contains any divs.
+                * If keep_divs is set, then the divs have been ordered
+                * and we can keep the definition as long as the result
+                * is still ordered.
+                */
+               if (last_div == -1 || (keep_divs && last_div < k))
+                       isl_seq_elim(bmap->div[k]+1, eq,
+                                       1+pos, 1+total, &bmap->div[k][0]);
+               else
+                       isl_seq_clr(bmap->div[k], 1 + total);
+               ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
+       }
+}
+
+/* Assumes divs have been ordered if keep_divs is set.
+ */
+static void eliminate_div(struct isl_basic_map *bmap, isl_int *eq,
+       unsigned div, int keep_divs)
+{
+       unsigned pos = isl_dim_total(bmap->dim) + div;
+
+       eliminate_var_using_equality(bmap, pos, eq, keep_divs, NULL);
 
        isl_basic_map_drop_div(bmap, div);
 }
@@ -381,6 +437,8 @@ static struct isl_basic_map *eliminate_divs_eq(
        int modified = 0;
        unsigned off;
 
+       bmap = isl_basic_map_order_divs(bmap);
+
        if (!bmap)
                return NULL;
 
@@ -393,7 +451,7 @@ static struct isl_basic_map *eliminate_divs_eq(
                                continue;
                        modified = 1;
                        *progress = 1;
-                       eliminate_div(bmap, bmap->eq[i], d);
+                       eliminate_div(bmap, bmap->eq[i], d, 1);
                        isl_basic_map_drop_equality(bmap, i);
                        break;
                }
@@ -441,56 +499,6 @@ static struct isl_basic_map *eliminate_divs_ineq(
        return bmap;
 }
 
-static void eliminate_var_using_equality(struct isl_basic_map *bmap,
-       unsigned pos, isl_int *eq, int *progress)
-{
-       unsigned total;
-       int k;
-       int contains_divs;
-
-       total = isl_basic_map_total_dim(bmap);
-       contains_divs =
-               isl_seq_first_non_zero(eq + 1 + isl_dim_total(bmap->dim),
-                                               bmap->n_div) != -1;
-       for (k = 0; k < bmap->n_eq; ++k) {
-               if (bmap->eq[k] == eq)
-                       continue;
-               if (isl_int_is_zero(bmap->eq[k][1+pos]))
-                       continue;
-               if (progress)
-                       *progress = 1;
-               isl_seq_elim(bmap->eq[k], eq, 1+pos, 1+total, NULL);
-       }
-
-       for (k = 0; k < bmap->n_ineq; ++k) {
-               if (isl_int_is_zero(bmap->ineq[k][1+pos]))
-                       continue;
-               if (progress)
-                       *progress = 1;
-               isl_seq_elim(bmap->ineq[k], eq, 1+pos, 1+total, NULL);
-               ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
-       }
-
-       for (k = 0; k < bmap->n_div; ++k) {
-               if (isl_int_is_zero(bmap->div[k][0]))
-                       continue;
-               if (isl_int_is_zero(bmap->div[k][1+1+pos]))
-                       continue;
-               if (progress)
-                       *progress = 1;
-               /* We need to be careful about circular definitions,
-                * so for now we just remove the definition of div k
-                * if the equality contains any divs.
-                */
-               if (contains_divs)
-                       isl_seq_clr(bmap->div[k], 1 + total);
-               else
-                       isl_seq_elim(bmap->div[k]+1, eq,
-                                       1+pos, 1+total, &bmap->div[k][0]);
-               ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
-       }
-}
-
 struct isl_basic_map *isl_basic_map_gauss(
        struct isl_basic_map *bmap, int *progress)
 {
@@ -500,6 +508,8 @@ struct isl_basic_map *isl_basic_map_gauss(
        unsigned total_var;
        unsigned total;
 
+       bmap = isl_basic_map_order_divs(bmap);
+
        if (!bmap)
                return NULL;
 
@@ -522,7 +532,7 @@ struct isl_basic_map *isl_basic_map_gauss(
                if (isl_int_is_neg(bmap->eq[done][1+last_var]))
                        isl_seq_neg(bmap->eq[done], bmap->eq[done], 1+total);
 
-               eliminate_var_using_equality(bmap, last_var, bmap->eq[done],
+               eliminate_var_using_equality(bmap, last_var, bmap->eq[done], 1,
                                                progress);
 
                if (last_var >= total_var &&
@@ -648,7 +658,7 @@ static struct isl_basic_map *remove_duplicate_divs(
                k = elim_for[l] - 1;
                isl_int_set_si(eq.data[1+total_var+k], -1);
                isl_int_set_si(eq.data[1+total_var+l], 1);
-               eliminate_div(bmap, eq.data, l);
+               eliminate_div(bmap, eq.data, l, 0);
                isl_int_set_si(eq.data[1+total_var+k], 0);
                isl_int_set_si(eq.data[1+total_var+l], 0);
        }
@@ -760,13 +770,13 @@ static struct isl_basic_map *normalize_divs(
        if (div_eq < bmap->n_eq) {
                B = isl_mat_sub_alloc(bmap->ctx, bmap->eq, div_eq,
                                        bmap->n_eq - div_eq, 0, 1 + total);
-               C = isl_mat_variable_compression(bmap->ctx, B, &C2);
+               C = isl_mat_variable_compression(B, &C2);
                if (!C || !C2)
                        goto error;
                if (C->n_col == 0) {
                        bmap = isl_basic_map_set_to_empty(bmap);
-                       isl_mat_free(bmap->ctx, C);
-                       isl_mat_free(bmap->ctx, C2);
+                       isl_mat_free(C);
+                       isl_mat_free(C2);
                        goto done;
                }
        }
@@ -782,17 +792,17 @@ static struct isl_basic_map *normalize_divs(
        B = isl_mat_sub_alloc(bmap->ctx, bmap->eq, 0, div_eq, 0, 1 + total);
 
        if (C) {
-               B = isl_mat_product(bmap->ctx, B, C);
+               B = isl_mat_product(B, C);
                C = NULL;
        }
 
-       T = isl_mat_parameter_compression(bmap->ctx, B, d);
+       T = isl_mat_parameter_compression(B, d);
        if (!T)
                goto error;
        if (T->n_col == 0) {
                bmap = isl_basic_map_set_to_empty(bmap);
-               isl_mat_free(bmap->ctx, C2);
-               isl_mat_free(bmap->ctx, T);
+               isl_mat_free(C2);
+               isl_mat_free(T);
                goto done;
        }
        isl_int_init(v);
@@ -856,8 +866,8 @@ static struct isl_basic_map *normalize_divs(
                isl_int_set(bmap->eq[j][pos[i]], bmap->div[k][0]);
        }
        free(pos);
-       isl_mat_free(bmap->ctx, C2);
-       isl_mat_free(bmap->ctx, T);
+       isl_mat_free(C2);
+       isl_mat_free(T);
 
        if (progress)
                *progress = 1;
@@ -866,9 +876,9 @@ done:
 
        return bmap;
 error:
-       isl_mat_free(bmap->ctx, C);
-       isl_mat_free(bmap->ctx, C2);
-       isl_mat_free(bmap->ctx, T);
+       isl_mat_free(C);
+       isl_mat_free(C2);
+       isl_mat_free(T);
        return bmap;
 }
 
@@ -935,7 +945,7 @@ static int ok_to_set_div_from_bound(struct isl_basic_map *bmap,
 static struct isl_basic_map *check_for_div_constraints(
        struct isl_basic_map *bmap, int k, int l, isl_int sum, int *progress)
 {
-       int i, j;
+       int i;
        unsigned total = 1 + isl_dim_total(bmap->dim);
 
        for (i = 0; i < bmap->n_div; ++i) {
@@ -1012,6 +1022,8 @@ static struct isl_basic_map *remove_duplicate_constraints(
                         * will no longer be valid.
                         * Plus, we probably we want to regauss first.
                         */
+                       if (progress)
+                               *progress = 1;
                        isl_basic_map_drop_inequality(bmap, l);
                        isl_basic_map_inequality_to_equality(bmap, k);
                } else
@@ -1173,22 +1185,21 @@ error:
 }
 
 
-/* Remove any div that is defined in terms of the given variable.
+/* Remove definition of any div that is defined in terms of the given variable.
+ * The div itself is not removed.  Functions such as
+ * eliminate_divs_ineq depend on the other divs remaining in place.
  */
 static struct isl_basic_map *remove_dependent_vars(struct isl_basic_map *bmap,
                                                                        int pos)
 {
        int i;
-       unsigned dim = isl_dim_total(bmap->dim);
 
        for (i = 0; i < bmap->n_div; ++i) {
                if (isl_int_is_zero(bmap->div[i][0]))
                        continue;
                if (isl_int_is_zero(bmap->div[i][1+1+pos]))
                        continue;
-               bmap = isl_basic_map_eliminate_vars(bmap, dim + i, 1);
-               if (!bmap)
-                       return NULL;
+               isl_int_set_si(bmap->div[i][0], 0);
        }
        return bmap;
 }
@@ -1223,7 +1234,7 @@ struct isl_basic_map *isl_basic_map_eliminate_vars(
                for (i = 0; i < bmap->n_eq; ++i) {
                        if (isl_int_is_zero(bmap->eq[i][1+d]))
                                continue;
-                       eliminate_var_using_equality(bmap, d, bmap->eq[i], NULL);
+                       eliminate_var_using_equality(bmap, d, bmap->eq[i], 0, NULL);
                        isl_basic_map_drop_equality(bmap, i);
                        break;
                }
@@ -1310,13 +1321,13 @@ static void compute_elimination_index(struct isl_basic_map *bmap, int *elim)
 
 static void set_compute_elimination_index(struct isl_basic_set *bset, int *elim)
 {
-       return compute_elimination_index((struct isl_basic_map *)bset, elim);
+       compute_elimination_index((struct isl_basic_map *)bset, elim);
 }
 
 static int reduced_using_equalities(isl_int *dst, isl_int *src,
        struct isl_basic_map *bmap, int *elim)
 {
-       int d, i;
+       int d;
        int copied = 0;
        unsigned total;
 
@@ -1477,16 +1488,16 @@ static struct isl_basic_set *normalize_constraints_in_compressed_space(
 
        total = isl_basic_set_total_dim(bset);
        B = isl_mat_sub_alloc(bset->ctx, bset->eq, 0, bset->n_eq, 0, 1 + total);
-       C = isl_mat_variable_compression(bset->ctx, B, NULL);
+       C = isl_mat_variable_compression(B, NULL);
        if (!C)
                return bset;
        if (C->n_col == 0) {
-               isl_mat_free(bset->ctx, C);
+               isl_mat_free(C);
                return isl_basic_set_set_to_empty(bset);
        }
        B = isl_mat_sub_alloc(bset->ctx, bset->ineq,
                                                0, bset->n_ineq, 0, 1 + total);
-       C = isl_mat_product(bset->ctx, B, C);
+       C = isl_mat_product(B, C);
        if (!C)
                return bset;
 
@@ -1500,7 +1511,7 @@ static struct isl_basic_set *normalize_constraints_in_compressed_space(
        }
        isl_int_clear(gcd);
 
-       isl_mat_free(bset->ctx, C);
+       isl_mat_free(C);
 
        return bset;
 }
@@ -1571,32 +1582,36 @@ static struct isl_basic_set *uset_gist(struct isl_basic_set *bset,
        bset = remove_shifted_constraints(bset, context);
        if (!bset->n_ineq)
                goto done;
-       isl_basic_set_free_equality(context, context->n_eq);
        context_ineq = context->n_ineq;
        combined = isl_basic_set_cow(isl_basic_set_copy(context));
+       if (isl_basic_set_free_equality(combined, context->n_eq) < 0)
+               goto error;
        combined = isl_basic_set_extend_constraints(combined,
                                                    bset->n_eq, bset->n_ineq);
        tab = isl_tab_from_basic_set(combined);
        if (!tab)
                goto error;
        for (i = 0; i < context_ineq; ++i)
-               tab->con[i].frozen = 1;
-       tab = isl_tab_extend(bset->ctx, tab, bset->n_ineq);
+               if (isl_tab_freeze_constraint(tab, i) < 0)
+                       goto error;
+       tab = isl_tab_extend(tab, bset->n_ineq);
        if (!tab)
                goto error;
        for (i = 0; i < bset->n_ineq; ++i)
-               tab = isl_tab_add_ineq(bset->ctx, tab, bset->ineq[i]);
+               if (isl_tab_add_ineq(tab, bset->ineq[i]) < 0)
+                       goto error;
        bset = isl_basic_set_add_constraints(combined, bset, 0);
-       tab = isl_tab_detect_equalities(bset->ctx, tab);
-       tab = isl_tab_detect_redundant(bset->ctx, tab);
-       if (!tab)
+       tab = isl_tab_detect_implicit_equalities(tab);
+       if (isl_tab_detect_redundant(tab) < 0) {
+               isl_tab_free(tab);
                goto error2;
+       }
        for (i = 0; i < context_ineq; ++i) {
                tab->con[i].is_zero = 0;
                tab->con[i].is_redundant = 1;
        }
        bset = isl_basic_set_update_from_tab(bset, tab);
-       isl_tab_free(bset->ctx, tab);
+       isl_tab_free(tab);
        ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
        ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
 done:
@@ -1763,7 +1778,7 @@ int isl_basic_map_fast_is_disjoint(struct isl_basic_map *bmap1,
        struct isl_vec *v = NULL;
        int *elim = NULL;
        unsigned total;
-       int d, i;
+       int i;
 
        if (!bmap1 || !bmap2)
                return -1;
@@ -1809,15 +1824,15 @@ int isl_basic_map_fast_is_disjoint(struct isl_basic_map *bmap1,
                    isl_seq_first_non_zero(v->block.data + 1, total) == -1)
                        goto disjoint;
        }
-       isl_vec_free(bmap1->ctx, v);
+       isl_vec_free(v);
        free(elim);
        return 0;
 disjoint:
-       isl_vec_free(bmap1->ctx, v);
+       isl_vec_free(v);
        free(elim);
        return 1;
 error:
-       isl_vec_free(bmap1->ctx, v);
+       isl_vec_free(v);
        free(elim);
        return -1;
 }
@@ -1856,6 +1871,404 @@ int isl_set_fast_is_disjoint(struct isl_set *set1, struct isl_set *set2)
                                        (struct isl_map *)set2);
 }
 
+/* Check if we can combine a given div with lower bound l and upper
+ * bound u with some other div and if so return that other div.
+ * Otherwise return -1.
+ *
+ * We first check that
+ *     - the bounds are opposites of each other (except for the constant
+ *       term)
+ *     - the bounds do not reference any other div
+ *     - no div is defined in terms of this div
+ *
+ * Let m be the size of the range allowed on the div by the bounds.
+ * That is, the bounds are of the form
+ *
+ *     e <= a <= e + m - 1
+ *
+ * with e some expression in the other variables.
+ * We look for another div b such that no third div is defined in terms
+ * of this second div b and such that in any constraint that contains
+ * a (except for the given lower and upper bound), also contains b
+ * with a coefficient that is m times that of b.
+ * That is, all constraints (execpt for the lower and upper bound)
+ * are of the form
+ *
+ *     e + f (a + m b) >= 0
+ *
+ * If so, we return b so that "a + m b" can be replaced by
+ * a single div "c = a + m b".
+ */
+static int div_find_coalesce(struct isl_basic_map *bmap, int *pairs,
+       unsigned div, unsigned l, unsigned u)
+{
+       int i, j;
+       unsigned dim;
+       int coalesce = -1;
+
+       if (bmap->n_div <= 1)
+               return -1;
+       dim = isl_dim_total(bmap->dim);
+       if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + dim, div) != -1)
+               return -1;
+       if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + dim + div + 1,
+                                  bmap->n_div - div - 1) != -1)
+               return -1;
+       if (!isl_seq_is_neg(bmap->ineq[l] + 1, bmap->ineq[u] + 1,
+                           dim + bmap->n_div))
+               return -1;
+
+       for (i = 0; i < bmap->n_div; ++i) {
+               if (isl_int_is_zero(bmap->div[i][0]))
+                       continue;
+               if (!isl_int_is_zero(bmap->div[i][1 + 1 + dim + div]))
+                       return -1;
+       }
+
+       isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
+       if (isl_int_is_neg(bmap->ineq[l][0])) {
+               isl_int_sub(bmap->ineq[l][0],
+                           bmap->ineq[l][0], bmap->ineq[u][0]);
+               bmap = isl_basic_map_copy(bmap);
+               bmap = isl_basic_map_set_to_empty(bmap);
+               isl_basic_map_free(bmap);
+               return -1;
+       }
+       isl_int_add_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
+       for (i = 0; i < bmap->n_div; ++i) {
+               if (i == div)
+                       continue;
+               if (!pairs[i])
+                       continue;
+               for (j = 0; j < bmap->n_div; ++j) {
+                       if (isl_int_is_zero(bmap->div[j][0]))
+                               continue;
+                       if (!isl_int_is_zero(bmap->div[j][1 + 1 + dim + i]))
+                               break;
+               }
+               if (j < bmap->n_div)
+                       continue;
+               for (j = 0; j < bmap->n_ineq; ++j) {
+                       int valid;
+                       if (j == l || j == u)
+                               continue;
+                       if (isl_int_is_zero(bmap->ineq[j][1 + dim + div]))
+                               continue;
+                       if (isl_int_is_zero(bmap->ineq[j][1 + dim + i]))
+                               break;
+                       isl_int_mul(bmap->ineq[j][1 + dim + div],
+                                   bmap->ineq[j][1 + dim + div],
+                                   bmap->ineq[l][0]);
+                       valid = isl_int_eq(bmap->ineq[j][1 + dim + div],
+                                          bmap->ineq[j][1 + dim + i]);
+                       isl_int_divexact(bmap->ineq[j][1 + dim + div],
+                                        bmap->ineq[j][1 + dim + div],
+                                        bmap->ineq[l][0]);
+                       if (!valid)
+                               break;
+               }
+               if (j < bmap->n_ineq)
+                       continue;
+               coalesce = i;
+               break;
+       }
+       isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
+       isl_int_sub(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
+       return coalesce;
+}
+
+/* Given a lower and an upper bound on div i, construct an inequality
+ * that when nonnegative ensures that this pair of bounds always allows
+ * for an integer value of the given div.
+ * The lower bound is inequality l, while the upper bound is inequality u.
+ * The constructed inequality is stored in ineq.
+ * g, fl, fu are temporary scalars.
+ *
+ * Let the upper bound be
+ *
+ *     -n_u a + e_u >= 0
+ *
+ * and the lower bound
+ *
+ *     n_l a + e_l >= 0
+ *
+ * Let n_u = f_u g and n_l = f_l g, with g = gcd(n_u, n_l).
+ * We have
+ *
+ *     - f_u e_l <= f_u f_l g a <= f_l e_u
+ *
+ * Since all variables are integer valued, this is equivalent to
+ *
+ *     - f_u e_l - (f_u - 1) <= f_u f_l g a <= f_l e_u + (f_l - 1)
+ *
+ * If this interval is at least f_u f_l g, then it contains at least
+ * one integer value for a.
+ * That is, the test constraint is
+ *
+ *     f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 >= f_u f_l g
+ */
+static void construct_test_ineq(struct isl_basic_map *bmap, int i,
+       int l, int u, isl_int *ineq, isl_int g, isl_int fl, isl_int fu)
+{
+       unsigned dim;
+       dim = isl_dim_total(bmap->dim);
+
+       isl_int_gcd(g, bmap->ineq[l][1 + dim + i], bmap->ineq[u][1 + dim + i]);
+       isl_int_divexact(fl, bmap->ineq[l][1 + dim + i], g);
+       isl_int_divexact(fu, bmap->ineq[u][1 + dim + i], g);
+       isl_int_neg(fu, fu);
+       isl_seq_combine(ineq, fl, bmap->ineq[u], fu, bmap->ineq[l],
+                       1 + dim + bmap->n_div);
+       isl_int_add(ineq[0], ineq[0], fl);
+       isl_int_add(ineq[0], ineq[0], fu);
+       isl_int_sub_ui(ineq[0], ineq[0], 1);
+       isl_int_mul(g, g, fl);
+       isl_int_mul(g, g, fu);
+       isl_int_sub(ineq[0], ineq[0], g);
+}
+
+/* Remove more kinds of divs that are not strictly needed.
+ * In particular, if all pairs of lower and upper bounds on a div
+ * are such that they allow at least one integer value of the div,
+ * the we can eliminate the div using Fourier-Motzkin without
+ * introducing any spurious solutions.
+ */
+static struct isl_basic_map *drop_more_redundant_divs(
+       struct isl_basic_map *bmap, int *pairs, int n)
+{
+       struct isl_tab *tab = NULL;
+       struct isl_vec *vec = NULL;
+       unsigned dim;
+       int remove = -1;
+       isl_int g, fl, fu;
+
+       isl_int_init(g);
+       isl_int_init(fl);
+       isl_int_init(fu);
+
+       if (!bmap)
+               goto error;
+
+       dim = isl_dim_total(bmap->dim);
+       vec = isl_vec_alloc(bmap->ctx, 1 + dim + bmap->n_div);
+       if (!vec)
+               goto error;
+
+       tab = isl_tab_from_basic_map(bmap);
+
+       while (n > 0) {
+               int i, l, u;
+               int best = -1;
+               enum isl_lp_result res;
+
+               for (i = 0; i < bmap->n_div; ++i) {
+                       if (!pairs[i])
+                               continue;
+                       if (best >= 0 && pairs[best] <= pairs[i])
+                               continue;
+                       best = i;
+               }
+
+               i = best;
+               for (l = 0; l < bmap->n_ineq; ++l) {
+                       if (!isl_int_is_pos(bmap->ineq[l][1 + dim + i]))
+                               continue;
+                       for (u = 0; u < bmap->n_ineq; ++u) {
+                               if (!isl_int_is_neg(bmap->ineq[u][1 + dim + i]))
+                                       continue;
+                               construct_test_ineq(bmap, i, l, u,
+                                                   vec->el, g, fl, fu);
+                               res = isl_tab_min(tab, vec->el,
+                                                 bmap->ctx->one, &g, NULL, 0);
+                               if (res == isl_lp_error)
+                                       goto error;
+                               if (res == isl_lp_empty) {
+                                       bmap = isl_basic_map_set_to_empty(bmap);
+                                       break;
+                               }
+                               if (res != isl_lp_ok || isl_int_is_neg(g))
+                                       break;
+                       }
+                       if (u < bmap->n_ineq)
+                               break;
+               }
+               if (l == bmap->n_ineq) {
+                       remove = i;
+                       break;
+               }
+               pairs[i] = 0;
+               --n;
+       }
+
+       isl_tab_free(tab);
+       isl_vec_free(vec);
+
+       isl_int_clear(g);
+       isl_int_clear(fl);
+       isl_int_clear(fu);
+
+       free(pairs);
+
+       if (remove < 0)
+               return bmap;
+
+       bmap = isl_basic_map_remove(bmap, isl_dim_div, remove, 1);
+       return isl_basic_map_drop_redundant_divs(bmap);
+error:
+       free(pairs);
+       isl_basic_map_free(bmap);
+       isl_tab_free(tab);
+       isl_vec_free(vec);
+       isl_int_clear(g);
+       isl_int_clear(fl);
+       isl_int_clear(fu);
+       return NULL;
+}
+
+/* Given a pair of divs div1 and div2 such that, expect for the lower bound l
+ * and the upper bound u, div1 always occurs together with div2 in the form 
+ * (div1 + m div2), where m is the constant range on the variable div1
+ * allowed by l and u, replace the pair div1 and div2 by a single
+ * div that is equal to div1 + m div2.
+ *
+ * The new div will appear in the location that contains div2.
+ * We need to modify all constraints that contain
+ * div2 = (div - div1) / m
+ * (If a constraint does not contain div2, it will also not contain div1.)
+ * If the constraint also contains div1, then we know they appear
+ * as f (div1 + m div2) and we can simply replace (div1 + m div2) by div,
+ * i.e., the coefficient of div is f.
+ *
+ * Otherwise, we first need to introduce div1 into the constraint.
+ * Let the l be
+ *
+ *     div1 + f >=0
+ *
+ * and u
+ *
+ *     -div1 + f' >= 0
+ *
+ * A lower bound on div2
+ *
+ *     n div2 + t >= 0
+ *
+ * can be replaced by
+ *
+ *     (n * (m div 2 + div1) + m t + n f)/g >= 0
+ *
+ * with g = gcd(m,n).
+ * An upper bound
+ *
+ *     -n div2 + t >= 0
+ *
+ * can be replaced by
+ *
+ *     (-n * (m div2 + div1) + m t + n f')/g >= 0
+ *
+ * These constraint are those that we would obtain from eliminating
+ * div1 using Fourier-Motzkin.
+ *
+ * After all constraints have been modified, we drop the lower and upper
+ * bound and then drop div1.
+ */
+static struct isl_basic_map *coalesce_divs(struct isl_basic_map *bmap,
+       unsigned div1, unsigned div2, unsigned l, unsigned u)
+{
+       isl_int a;
+       isl_int b;
+       isl_int m;
+       unsigned dim, total;
+       int i;
+
+       dim = isl_dim_total(bmap->dim);
+       total = 1 + dim + bmap->n_div;
+
+       isl_int_init(a);
+       isl_int_init(b);
+       isl_int_init(m);
+       isl_int_add(m, bmap->ineq[l][0], bmap->ineq[u][0]);
+       isl_int_add_ui(m, m, 1);
+
+       for (i = 0; i < bmap->n_ineq; ++i) {
+               if (i == l || i == u)
+                       continue;
+               if (isl_int_is_zero(bmap->ineq[i][1 + dim + div2]))
+                       continue;
+               if (isl_int_is_zero(bmap->ineq[i][1 + dim + div1])) {
+                       isl_int_gcd(b, m, bmap->ineq[i][1 + dim + div2]);
+                       isl_int_divexact(a, m, b);
+                       isl_int_divexact(b, bmap->ineq[i][1 + dim + div2], b);
+                       if (isl_int_is_pos(b)) {
+                               isl_seq_combine(bmap->ineq[i], a, bmap->ineq[i],
+                                               b, bmap->ineq[l], total);
+                       } else {
+                               isl_int_neg(b, b);
+                               isl_seq_combine(bmap->ineq[i], a, bmap->ineq[i],
+                                               b, bmap->ineq[u], total);
+                       }
+               }
+               isl_int_set(bmap->ineq[i][1 + dim + div2],
+                           bmap->ineq[i][1 + dim + div1]);
+               isl_int_set_si(bmap->ineq[i][1 + dim + div1], 0);
+       }
+
+       isl_int_clear(a);
+       isl_int_clear(b);
+       isl_int_clear(m);
+       if (l > u) {
+               isl_basic_map_drop_inequality(bmap, l);
+               isl_basic_map_drop_inequality(bmap, u);
+       } else {
+               isl_basic_map_drop_inequality(bmap, u);
+               isl_basic_map_drop_inequality(bmap, l);
+       }
+       bmap = isl_basic_map_drop_div(bmap, div1);
+       return bmap;
+}
+
+/* First check if we can coalesce any pair of divs and
+ * then continue with dropping more redundant divs.
+ *
+ * We loop over all pairs of lower and upper bounds on a div
+ * with coefficient 1 and -1, respectively, check if there
+ * is any other div "c" with which we can coalesce the div
+ * and if so, perform the coalescing.
+ */
+static struct isl_basic_map *coalesce_or_drop_more_redundant_divs(
+       struct isl_basic_map *bmap, int *pairs, int n)
+{
+       int i, l, u;
+       unsigned dim;
+
+       dim = isl_dim_total(bmap->dim);
+
+       for (i = 0; i < bmap->n_div; ++i) {
+               if (!pairs[i])
+                       continue;
+               for (l = 0; l < bmap->n_ineq; ++l) {
+                       if (!isl_int_is_one(bmap->ineq[l][1 + dim + i]))
+                               continue;
+                       for (u = 0; u < bmap->n_ineq; ++u) {
+                               int c;
+
+                               if (!isl_int_is_negone(bmap->ineq[u][1+dim+i]))
+                                       continue;
+                               c = div_find_coalesce(bmap, pairs, i, l, u);
+                               if (c < 0)
+                                       continue;
+                               free(pairs);
+                               bmap = coalesce_divs(bmap, i, c, l, u);
+                               return isl_basic_map_drop_redundant_divs(bmap);
+                       }
+               }
+       }
+
+       if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
+               return bmap;
+
+       return drop_more_redundant_divs(bmap, pairs, n);
+}
+
 /* Remove divs that are not strictly needed.
  * In particular, if a div only occurs positively (or negatively)
  * in constraints, then it can simply be dropped.
@@ -1866,30 +2279,39 @@ int isl_set_fast_is_disjoint(struct isl_set *set1, struct isl_set *set2)
  * div, i.e., if one plus this sum is greater than or equal to
  * the (absolute value) of the coefficent of the div in the constraints,
  * then we can also simply drop the div.
+ *
+ * If any divs are left after these simple checks then we move on
+ * to more complicated cases in drop_more_redundant_divs.
  */
 struct isl_basic_map *isl_basic_map_drop_redundant_divs(
        struct isl_basic_map *bmap)
 {
        int i, j;
        unsigned off;
+       int *pairs = NULL;
+       int n = 0;
 
        if (!bmap)
                goto error;
 
        off = isl_dim_total(bmap->dim);
+       pairs = isl_calloc_array(bmap->ctx, int, bmap->n_div);
+       if (!pairs)
+               goto error;
 
        for (i = 0; i < bmap->n_div; ++i) {
                int pos, neg;
                int last_pos, last_neg;
                int redundant;
+               int defined;
 
-               if (!isl_int_is_zero(bmap->div[i][0]))
-                       continue;
+               defined = !isl_int_is_zero(bmap->div[i][0]);
                for (j = 0; j < bmap->n_eq; ++j)
                        if (!isl_int_is_zero(bmap->eq[j][1 + off + i]))
                                break;
                if (j < bmap->n_eq)
                        continue;
+               ++n;
                pos = neg = 0;
                for (j = 0; j < bmap->n_ineq; ++j) {
                        if (isl_int_is_pos(bmap->ineq[j][1 + off + i])) {
@@ -1901,14 +2323,16 @@ struct isl_basic_map *isl_basic_map_drop_redundant_divs(
                                ++neg;
                        }
                }
-               if (pos * neg == 0) {
+               pairs[i] = pos * neg;
+               if (pairs[i] == 0) {
                        for (j = bmap->n_ineq - 1; j >= 0; --j)
                                if (!isl_int_is_zero(bmap->ineq[j][1+off+i]))
                                        isl_basic_map_drop_inequality(bmap, j);
                        bmap = isl_basic_map_drop_div(bmap, i);
+                       free(pairs);
                        return isl_basic_map_drop_redundant_divs(bmap);
                }
-               if (pos * neg != 1)
+               if (pairs[i] != 1)
                        continue;
                if (!isl_seq_is_neg(bmap->ineq[last_pos] + 1,
                                    bmap->ineq[last_neg] + 1,
@@ -1925,8 +2349,18 @@ struct isl_basic_map *isl_basic_map_drop_redundant_divs(
                               bmap->ineq[last_pos][0], 1);
                isl_int_sub(bmap->ineq[last_pos][0],
                            bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
-               if (!redundant)
-                       continue;
+               if (!redundant) {
+                       if (defined ||
+                           !ok_to_set_div_from_bound(bmap, i, last_pos)) {
+                               pairs[i] = 0;
+                               --n;
+                               continue;
+                       }
+                       bmap = set_div_from_lower_bound(bmap, i, last_pos);
+                       bmap = isl_basic_map_simplify(bmap);
+                       free(pairs);
+                       return isl_basic_map_drop_redundant_divs(bmap);
+               }
                if (last_pos > last_neg) {
                        isl_basic_map_drop_inequality(bmap, last_pos);
                        isl_basic_map_drop_inequality(bmap, last_neg);
@@ -1935,11 +2369,48 @@ struct isl_basic_map *isl_basic_map_drop_redundant_divs(
                        isl_basic_map_drop_inequality(bmap, last_pos);
                }
                bmap = isl_basic_map_drop_div(bmap, i);
+               free(pairs);
                return isl_basic_map_drop_redundant_divs(bmap);
        }
 
+       if (n > 0)
+               return coalesce_or_drop_more_redundant_divs(bmap, pairs, n);
+
+       free(pairs);
        return bmap;
 error:
+       free(pairs);
        isl_basic_map_free(bmap);
        return NULL;
 }
+
+struct isl_basic_set *isl_basic_set_drop_redundant_divs(
+       struct isl_basic_set *bset)
+{
+       return (struct isl_basic_set *)
+           isl_basic_map_drop_redundant_divs((struct isl_basic_map *)bset);
+}
+
+struct isl_map *isl_map_drop_redundant_divs(struct isl_map *map)
+{
+       int i;
+
+       if (!map)
+               return NULL;
+       for (i = 0; i < map->n; ++i) {
+               map->p[i] = isl_basic_map_drop_redundant_divs(map->p[i]);
+               if (!map->p[i])
+                       goto error;
+       }
+       ISL_F_CLR(map, ISL_MAP_NORMALIZED);
+       return map;
+error:
+       isl_map_free(map);
+       return NULL;
+}
+
+struct isl_set *isl_set_drop_redundant_divs(struct isl_set *set)
+{
+       return (struct isl_set *)
+           isl_map_drop_redundant_divs((struct isl_map *)set);
+}