change calling conventions of isl_basic_set_has_defining_{,in}equalit{y,ies}
[platform/upstream/isl.git] / isl_convex_hull.c
index 14b5817..a5d45da 100644 (file)
@@ -6,9 +6,9 @@
 #include "isl_seq.h"
 #include "isl_equalities.h"
 
-static struct isl_basic_set *uset_convex_hull(struct isl_set *set);
+static struct isl_basic_set *uset_convex_hull_wrap(struct isl_set *set);
 
-static swap_ineq(struct isl_basic_map *bmap, unsigned i, unsigned j)
+static void swap_ineq(struct isl_basic_map *bmap, unsigned i, unsigned j)
 {
        isl_int *t;
 
@@ -19,6 +19,59 @@ static swap_ineq(struct isl_basic_map *bmap, unsigned i, unsigned j)
        }
 }
 
+/* Return 1 if constraint c is redundant with respect to the constraints
+ * in bmap.  If c is a lower [upper] bound in some variable and bmap
+ * does not have a lower [upper] bound in that variable, then c cannot
+ * be redundant and we do not need solve any lp.
+ */
+int isl_basic_map_constraint_is_redundant(struct isl_basic_map **bmap,
+       isl_int *c, isl_int *opt_n, isl_int *opt_d)
+{
+       enum isl_lp_result res;
+       unsigned total;
+       int i, j;
+
+       if (!bmap)
+               return -1;
+
+       total = isl_basic_map_total_dim(*bmap);
+       for (i = 0; i < total; ++i) {
+               int sign;
+               if (isl_int_is_zero(c[1+i]))
+                       continue;
+               sign = isl_int_sgn(c[1+i]);
+               for (j = 0; j < (*bmap)->n_ineq; ++j)
+                       if (sign == isl_int_sgn((*bmap)->ineq[j][1+i]))
+                               break;
+               if (j == (*bmap)->n_ineq)
+                       break;
+       }
+       if (i < total)
+               return 0;
+
+       res = isl_solve_lp(*bmap, 0, c+1, (*bmap)->ctx->one, opt_n, opt_d);
+       if (res == isl_lp_unbounded)
+               return 0;
+       if (res == isl_lp_error)
+               return -1;
+       if (res == isl_lp_empty) {
+               *bmap = isl_basic_map_set_to_empty(*bmap);
+               return 0;
+       }
+       if (opt_d)
+               isl_int_addmul(*opt_n, *opt_d, c[0]);
+       else
+               isl_int_add(*opt_n, *opt_n, c[0]);
+       return !isl_int_is_neg(*opt_n);
+}
+
+int isl_basic_set_constraint_is_redundant(struct isl_basic_set **bset,
+       isl_int *c, isl_int *opt_n, isl_int *opt_d)
+{
+       return isl_basic_map_constraint_is_redundant(
+                       (struct isl_basic_map **)bset, c, opt_n, opt_d);
+}
+
 /* Compute the convex hull of a basic map, by removing the redundant
  * constraints.  If the minimal value along the normal of a constraint
  * is the same if the constraint is removed, then the constraint is redundant.
@@ -47,23 +100,18 @@ struct isl_basic_map *isl_basic_map_convex_hull(struct isl_basic_map *bmap)
        isl_int_init(opt_n);
        isl_int_init(opt_d);
        for (i = bmap->n_ineq-1; i >= 0; --i) {
-               enum isl_lp_result res;
+               int redundant;
                swap_ineq(bmap, i, bmap->n_ineq-1);
                bmap->n_ineq--;
-               res = isl_solve_lp(bmap, 0,
-                       bmap->ineq[bmap->n_ineq]+1, ctx->one, &opt_n, &opt_d);
-               bmap->n_ineq++;
-               swap_ineq(bmap, i, bmap->n_ineq-1);
-               if (res == isl_lp_unbounded)
-                       continue;
-               if (res == isl_lp_error)
+               redundant = isl_basic_map_constraint_is_redundant(&bmap,
+                               bmap->ineq[bmap->n_ineq], &opt_n, &opt_d);
+               if (redundant == -1)
                        goto error;
-               if (res == isl_lp_empty) {
-                       bmap = isl_basic_map_set_to_empty(bmap);
+               if (F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
                        break;
-               }
-               isl_int_addmul(opt_n, opt_d, bmap->ineq[i][0]);
-               if (!isl_int_is_neg(opt_n))
+               bmap->n_ineq++;
+               swap_ineq(bmap, i, bmap->n_ineq-1);
+               if (redundant)
                        isl_basic_map_drop_inequality(bmap, i);
        }
        isl_int_clear(opt_n);
@@ -84,52 +132,29 @@ struct isl_basic_set *isl_basic_set_convex_hull(struct isl_basic_set *bset)
                isl_basic_map_convex_hull((struct isl_basic_map *)bset);
 }
 
-/* Check if "c" is a direction with a lower bound in "set" that is independent
- * of the previously found "n" bounds in "dirs".
- * If so, add it to the list, with the negative of the lower bound
- * in the constant position, i.e., such that c correspond to a bounding
- * hyperplane (but not necessarily a facet).
+/* Check if the set set is bound in the direction of the affine
+ * constraint c and if so, set the constant term such that the
+ * resulting constraint is a bounding constraint for the set.
  */
-static int is_independent_bound(struct isl_ctx *ctx,
-       struct isl_set *set, isl_int *c,
-       struct isl_mat *dirs, int n)
+static int uset_is_bound(struct isl_ctx *ctx, struct isl_set *set,
+       isl_int *c, unsigned len)
 {
        int first;
-       int i = 0, j;
+       int j;
        isl_int opt;
        isl_int opt_denom;
 
-       isl_seq_cpy(dirs->row[n]+1, c+1, dirs->n_col-1);
-       if (n != 0) {
-               int pos = isl_seq_first_non_zero(dirs->row[n]+1, dirs->n_col-1);
-               if (pos < 0)
-                       return 0;
-               for (i = 0; i < n; ++i) {
-                       int pos_i;
-                       pos_i = isl_seq_first_non_zero(dirs->row[i]+1, dirs->n_col-1);
-                       if (pos_i < pos)
-                               continue;
-                       if (pos_i > pos)
-                               break;
-                       isl_seq_elim(dirs->row[n]+1, dirs->row[i]+1, pos,
-                                       dirs->n_col-1, NULL);
-                       pos = isl_seq_first_non_zero(dirs->row[n]+1, dirs->n_col-1);
-                       if (pos < 0)
-                               return 0;
-               }
-       }
-
        isl_int_init(opt);
        isl_int_init(opt_denom);
        first = 1;
        for (j = 0; j < set->n; ++j) {
                enum isl_lp_result res;
 
-               if (F_ISSET(set->p[j], ISL_BASIC_MAP_EMPTY))
+               if (F_ISSET(set->p[j], ISL_BASIC_SET_EMPTY))
                        continue;
 
                res = isl_solve_lp((struct isl_basic_map*)set->p[j],
-                               0, dirs->row[n]+1, ctx->one, &opt, &opt_denom);
+                               0, c+1, ctx->one, &opt, &opt_denom);
                if (res == isl_lp_unbounded)
                        break;
                if (res == isl_lp_error)
@@ -141,17 +166,63 @@ static int is_independent_bound(struct isl_ctx *ctx,
                        continue;
                }
                if (!isl_int_is_one(opt_denom))
-                       isl_seq_scale(dirs->row[n], dirs->row[n], opt_denom,
-                                       dirs->n_col);
-               if (first || isl_int_lt(opt, dirs->row[n][0]))
-                       isl_int_set(dirs->row[n][0], opt);
+                       isl_seq_scale(c, c, opt_denom, len);
+               if (first || isl_int_lt(opt, c[0]))
+                       isl_int_set(c[0], opt);
                first = 0;
        }
        isl_int_clear(opt);
        isl_int_clear(opt_denom);
-       if (j < set->n)
-               return 0;
-       isl_int_neg(dirs->row[n][0], dirs->row[n][0]);
+       isl_int_neg(c[0], c[0]);
+       return j >= set->n;
+error:
+       isl_int_clear(opt);
+       isl_int_clear(opt_denom);
+       return -1;
+}
+
+/* Check if "c" is a direction with both a lower bound and an upper
+ * bound in "set" that is independent of the previously found "n"
+ * bounds in "dirs".
+ * If so, add it to the list, with the negative of the lower bound
+ * in the constant position, i.e., such that c corresponds to a bounding
+ * hyperplane (but not necessarily a facet).
+ */
+static int is_independent_bound(struct isl_ctx *ctx,
+       struct isl_set *set, isl_int *c,
+       struct isl_mat *dirs, int n)
+{
+       int is_bound;
+       int i = 0;
+
+       isl_seq_cpy(dirs->row[n]+1, c+1, dirs->n_col-1);
+       if (n != 0) {
+               int pos = isl_seq_first_non_zero(dirs->row[n]+1, dirs->n_col-1);
+               if (pos < 0)
+                       return 0;
+               for (i = 0; i < n; ++i) {
+                       int pos_i;
+                       pos_i = isl_seq_first_non_zero(dirs->row[i]+1, dirs->n_col-1);
+                       if (pos_i < pos)
+                               continue;
+                       if (pos_i > pos)
+                               break;
+                       isl_seq_elim(dirs->row[n]+1, dirs->row[i]+1, pos,
+                                       dirs->n_col-1, NULL);
+                       pos = isl_seq_first_non_zero(dirs->row[n]+1, dirs->n_col-1);
+                       if (pos < 0)
+                               return 0;
+               }
+       }
+
+       isl_seq_neg(dirs->row[n] + 1, dirs->row[n] + 1, dirs->n_col - 1);
+       is_bound = uset_is_bound(ctx, set, dirs->row[n], dirs->n_col);
+       isl_seq_neg(dirs->row[n] + 1, dirs->row[n] + 1, dirs->n_col - 1);
+       if (is_bound != 1)
+               return is_bound;
+       is_bound = uset_is_bound(ctx, set, dirs->row[n], dirs->n_col);
+       if (is_bound != 1)
+               return is_bound;
        if (i < n) {
                int k;
                isl_int *t = dirs->row[n];
@@ -160,10 +231,6 @@ static int is_independent_bound(struct isl_ctx *ctx,
                dirs->row[i] = t;
        }
        return 1;
-error:
-       isl_int_clear(opt);
-       isl_int_clear(opt_denom);
-       return -1;
 }
 
 /* Compute and return a maximal set of linearly independent bounds
@@ -175,35 +242,26 @@ static struct isl_mat *independent_bounds(struct isl_ctx *ctx,
 {
        int i, j, n;
        struct isl_mat *dirs = NULL;
+       unsigned dim = isl_set_n_dim(set);
 
-       dirs = isl_mat_alloc(ctx, set->dim, 1+set->dim);
+       dirs = isl_mat_alloc(ctx, dim, 1+dim);
        if (!dirs)
                goto error;
 
        n = 0;
-       for (i = 0; n < set->dim && i < set->n; ++i) {
+       for (i = 0; n < dim && i < set->n; ++i) {
                int f;
                struct isl_basic_set *bset = set->p[i];
 
-               for (j = 0; n < set->dim && j < bset->n_eq; ++j) {
-                       f = is_independent_bound(ctx, set, bset->eq[j],
-                                               dirs, n);
-                       if (f < 0)
-                               goto error;
-                       if (f) {
-                               ++n;
-                               continue;
-                       }
-                       isl_seq_neg(bset->eq[j], bset->eq[j], 1+set->dim);
+               for (j = 0; n < dim && j < bset->n_eq; ++j) {
                        f = is_independent_bound(ctx, set, bset->eq[j],
                                                dirs, n);
-                       isl_seq_neg(bset->eq[j], bset->eq[j], 1+set->dim);
                        if (f < 0)
                                goto error;
                        if (f)
                                ++n;
                }
-               for (j = 0; n < set->dim && j < bset->n_ineq; ++j) {
+               for (j = 0; n < dim && j < bset->n_ineq; ++j) {
                        f = is_independent_bound(ctx, set, bset->ineq[j],
                                                dirs, n);
                        if (f < 0)
@@ -234,7 +292,7 @@ static struct isl_basic_set *isl_basic_set_set_rational(
 
        F_SET(bset, ISL_BASIC_MAP_RATIONAL);
 
-       return bset;
+       return isl_basic_set_finalize(bset);
 }
 
 static struct isl_set *isl_set_set_rational(struct isl_set *set)
@@ -260,17 +318,19 @@ static struct isl_basic_set *isl_basic_set_add_equality(struct isl_ctx *ctx,
 {
        int i;
        unsigned total;
+       unsigned dim;
 
        if (F_ISSET(bset, ISL_BASIC_SET_EMPTY))
                return bset;
 
-       isl_assert(ctx, bset->nparam == 0, goto error);
+       isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
        isl_assert(ctx, bset->n_div == 0, goto error);
-       bset = isl_basic_set_extend(bset, 0, bset->dim, 0, 1, 0);
+       dim = isl_basic_set_n_dim(bset);
+       bset = isl_basic_set_extend(bset, 0, dim, 0, 1, 0);
        i = isl_basic_set_alloc_equality(bset);
        if (i < 0)
                goto error;
-       isl_seq_cpy(bset->eq[i], c, 1 + bset->dim);
+       isl_seq_cpy(bset->eq[i], c, 1 + dim);
        return bset;
 error:
        isl_basic_set_free(bset);
@@ -320,12 +380,12 @@ static struct isl_basic_set *wrap_constraints(struct isl_ctx *ctx,
        unsigned n_eq;
        unsigned n_ineq;
        int i, j, k;
-       unsigned dim;
+       unsigned dim, lp_dim;
 
        if (!set)
                return NULL;
 
-       dim = 1 + set->dim;
+       dim = 1 + isl_set_n_dim(set);
        n_eq = 1;
        n_ineq = set->n;
        for (i = 0; i < set->n; ++i) {
@@ -335,6 +395,7 @@ static struct isl_basic_set *wrap_constraints(struct isl_ctx *ctx,
        lp = isl_basic_set_alloc(ctx, 0, dim * set->n, 0, n_eq, n_ineq);
        if (!lp)
                return NULL;
+       lp_dim = isl_basic_set_n_dim(lp);
        k = isl_basic_set_alloc_equality(lp);
        isl_int_set_si(lp->eq[k][0], -1);
        for (i = 0; i < set->n; ++i) {
@@ -344,7 +405,7 @@ static struct isl_basic_set *wrap_constraints(struct isl_ctx *ctx,
        }
        for (i = 0; i < set->n; ++i) {
                k = isl_basic_set_alloc_inequality(lp);
-               isl_seq_clr(lp->ineq[k], 1+lp->dim);
+               isl_seq_clr(lp->ineq[k], 1+lp_dim);
                isl_int_set_si(lp->ineq[k][1+dim*i], 1);
 
                for (j = 0; j < set->p[i]->n_eq; ++j) {
@@ -372,7 +433,7 @@ static struct isl_basic_set *wrap_constraints(struct isl_ctx *ctx,
  *
  *                     x_1 >= 0
  *
- * I.e., the facet is
+ * I.e., the facet lies in
  *
  *                     x_1 = 0
  *
@@ -409,13 +470,17 @@ static struct isl_basic_set *wrap_constraints(struct isl_ctx *ctx,
  *                             A_i [ x_i ] >= 0
  *
  * the constraints of each (transformed) basic set.
- * If a = n/d, then the consstraint defining the new facet (in the transformed
+ * If a = n/d, then the constraint defining the new facet (in the transformed
  * space) is
  *
  *                     -n x_1 + d x_2 >= 0
  *
  * In the original space, we need to take the same combination of the
  * corresponding constraints "facet" and "ridge".
+ *
+ * If a = -infty = "-1/0", then we just return the original facet constraint.
+ * This means that the facet is unbounded, but has a bounded intersection
+ * with the union of sets.
  */
 static isl_int *wrap_facet(struct isl_ctx *ctx, struct isl_set *set,
        isl_int *facet, isl_int *ridge)
@@ -430,17 +495,19 @@ static isl_int *wrap_facet(struct isl_ctx *ctx, struct isl_set *set,
 
        set = isl_set_copy(set);
 
-       dim = 1 + set->dim;
-       T = isl_mat_alloc(ctx, 3, 1 + set->dim);
+       dim = 1 + isl_set_n_dim(set);
+       T = isl_mat_alloc(ctx, 3, dim);
        if (!T)
                goto error;
        isl_int_set_si(T->row[0][0], 1);
-       isl_seq_clr(T->row[0]+1, set->dim);
-       isl_seq_cpy(T->row[1], facet, 1+set->dim);
-       isl_seq_cpy(T->row[2], ridge, 1+set->dim);
+       isl_seq_clr(T->row[0]+1, dim - 1);
+       isl_seq_cpy(T->row[1], facet, dim);
+       isl_seq_cpy(T->row[2], ridge, dim);
        T = isl_mat_right_inverse(ctx, T);
        set = isl_set_preimage(ctx, set, T);
        T = NULL;
+       if (!set)
+               goto error;
        lp = wrap_constraints(ctx, set);
        obj = isl_vec_alloc(ctx, dim*set->n);
        if (!obj)
@@ -463,7 +530,8 @@ static isl_int *wrap_facet(struct isl_ctx *ctx, struct isl_set *set,
        isl_vec_free(ctx, obj);
        isl_basic_set_free(lp);
        isl_set_free(set);
-       isl_assert(ctx, res == isl_lp_ok, return NULL);
+       isl_assert(ctx, res == isl_lp_ok || res == isl_lp_unbounded, 
+                  return NULL);
        return facet;
 error:
        isl_basic_set_free(lp);
@@ -491,9 +559,10 @@ static struct isl_mat *initial_facet_constraint(struct isl_ctx *ctx,
        struct isl_basic_set *face = NULL;
        struct isl_mat *m, *U, *Q;
        int i;
+       unsigned dim = isl_set_n_dim(set);
 
        isl_assert(ctx, set->n > 0, goto error);
-       isl_assert(ctx, bounds->n_row == set->dim, goto error);
+       isl_assert(ctx, bounds->n_row == dim, goto error);
 
        while (bounds->n_row > 1) {
                slice = isl_set_copy(set);
@@ -505,19 +574,19 @@ static struct isl_mat *initial_facet_constraint(struct isl_ctx *ctx,
                        isl_basic_set_free(face);
                        break;
                }
-               m = isl_mat_alloc(ctx, 1 + face->n_eq, 1 + face->dim);
+               m = isl_mat_alloc(ctx, 1 + face->n_eq, 1 + dim);
                if (!m)
                        goto error;
                isl_int_set_si(m->row[0][0], 1);
-               isl_seq_clr(m->row[0]+1, face->dim);
+               isl_seq_clr(m->row[0]+1, dim);
                for (i = 0; i < face->n_eq; ++i)
-                       isl_seq_cpy(m->row[1 + i], face->eq[i], 1 + face->dim);
+                       isl_seq_cpy(m->row[1 + i], face->eq[i], 1 + dim);
                U = isl_mat_right_inverse(ctx, m);
                Q = isl_mat_right_inverse(ctx, isl_mat_copy(ctx, U));
                U = isl_mat_drop_cols(ctx, U, 1 + face->n_eq,
-                                               face->dim - face->n_eq);
+                                               dim - face->n_eq);
                Q = isl_mat_drop_rows(ctx, Q, 1 + face->n_eq,
-                                               face->dim - face->n_eq);
+                                               dim - face->n_eq);
                U = isl_mat_drop_cols(ctx, U, 0, 1);
                Q = isl_mat_drop_rows(ctx, Q, 0, 1);
                bounds = isl_mat_product(ctx, bounds, U);
@@ -584,20 +653,22 @@ static struct isl_basic_set *compute_facet(struct isl_ctx *ctx,
 {
        struct isl_mat *m, *U, *Q;
        struct isl_basic_set *facet;
+       unsigned dim;
 
        set = isl_set_copy(set);
-       m = isl_mat_alloc(ctx, 2, 1 + set->dim);
+       dim = isl_set_n_dim(set);
+       m = isl_mat_alloc(ctx, 2, 1 + dim);
        if (!m)
                goto error;
        isl_int_set_si(m->row[0][0], 1);
-       isl_seq_clr(m->row[0]+1, set->dim);
-       isl_seq_cpy(m->row[1], c, 1+set->dim);
+       isl_seq_clr(m->row[0]+1, dim);
+       isl_seq_cpy(m->row[1], c, 1+dim);
        U = isl_mat_right_inverse(ctx, m);
        Q = isl_mat_right_inverse(ctx, isl_mat_copy(ctx, U));
        U = isl_mat_drop_cols(ctx, U, 1, 1);
        Q = isl_mat_drop_rows(ctx, Q, 1, 1);
        set = isl_set_preimage(ctx, set, U);
-       facet = uset_convex_hull(set);
+       facet = uset_convex_hull_wrap(set);
        facet = isl_basic_set_preimage(ctx, facet, Q);
        return facet;
 error:
@@ -627,6 +698,7 @@ static struct isl_basic_set *extend(struct isl_ctx *ctx, struct isl_set *set,
        struct isl_basic_set *facet = NULL;
        unsigned n_ineq;
        unsigned total;
+       unsigned dim;
 
        isl_assert(ctx, set->n > 0, goto error);
 
@@ -635,8 +707,9 @@ static struct isl_basic_set *extend(struct isl_ctx *ctx, struct isl_set *set,
                n_ineq += set->p[i]->n_eq;
                n_ineq += set->p[i]->n_ineq;
        }
-       isl_assert(ctx, 1 + set->dim == initial->n_col, goto error);
-       hull = isl_basic_set_alloc(ctx, 0, set->dim, 0, 0, n_ineq);
+       dim = isl_set_n_dim(set);
+       isl_assert(ctx, 1 + dim == initial->n_col, goto error);
+       hull = isl_basic_set_alloc(ctx, 0, dim, 0, 0, n_ineq);
        hull = isl_basic_set_set_rational(hull);
        if (!hull)
                goto error;
@@ -650,19 +723,19 @@ static struct isl_basic_set *extend(struct isl_ctx *ctx, struct isl_set *set,
                        goto error;
                if (facet->n_ineq + hull->n_ineq > n_ineq) {
                        hull = isl_basic_set_extend(hull,
-                               hull->nparam, hull->dim, 0, 0, facet->n_ineq);
+                               0, dim, 0, 0, facet->n_ineq);
                        n_ineq = hull->n_ineq + facet->n_ineq;
                }
                for (j = 0; j < facet->n_ineq; ++j) {
                        k = isl_basic_set_alloc_inequality(hull);
                        if (k < 0)
                                goto error;
-                       isl_seq_cpy(hull->ineq[k], hull->ineq[i], 1+hull->dim);
+                       isl_seq_cpy(hull->ineq[k], hull->ineq[i], 1+dim);
                        if (!wrap_facet(ctx, set, hull->ineq[k], facet->ineq[j]))
                                goto error;
                        for (f = 0; f < k; ++f)
                                if (isl_seq_eq(hull->ineq[f], hull->ineq[k],
-                                               1+hull->dim))
+                                               1+dim))
                                        break;
                        if (f < k)
                                isl_basic_set_free_inequality(hull, 1);
@@ -807,44 +880,193 @@ error:
 static struct isl_set *set_project_out(struct isl_ctx *ctx,
        struct isl_set *set, unsigned n)
 {
-       return isl_set_remove_dims(set, set->dim - n, n);
+       return isl_set_remove_dims(set, isl_set_n_dim(set) - n, n);
+}
+
+static struct isl_basic_set *convex_hull_0d(struct isl_set *set)
+{
+       struct isl_basic_set *convex_hull;
+
+       if (!set)
+               return NULL;
+
+       if (isl_set_is_empty(set))
+               convex_hull = isl_basic_set_empty(isl_dim_copy(set->dim));
+       else
+               convex_hull = isl_basic_set_universe(isl_dim_copy(set->dim));
+       isl_set_free(set);
+       return convex_hull;
 }
 
-/* If the number of linearly independent bounds we found is smaller
- * than the dimension, then the convex hull will have a lineality space,
- * so we may as well project out this lineality space.
- * We first transform the set such that the first variables correspond
- * to the directions of the linearly independent bounds and then
- * project out the remaining variables.
+/* Compute the convex hull of a pair of basic sets without any parameters or
+ * integer divisions using Fourier-Motzkin elimination.
+ * The convex hull is the set of all points that can be written as
+ * the sum of points from both basic sets (in homogeneous coordinates).
+ * We set up the constraints in a space with dimensions for each of
+ * the three sets and then project out the dimensions corresponding
+ * to the two original basic sets, retaining only those corresponding
+ * to the convex hull.
  */
-static struct isl_basic_set *modulo_lineality(struct isl_ctx *ctx,
-       struct isl_set *set, struct isl_mat *bounds)
+static struct isl_basic_set *convex_hull_pair(struct isl_basic_set *bset1,
+       struct isl_basic_set *bset2)
 {
-       int i, j;
-       unsigned old_dim, new_dim;
-       struct isl_mat *H = NULL, *U = NULL, *Q = NULL;
-       struct isl_basic_set *hull;
+       int i, j, k;
+       struct isl_basic_set *bset[2];
+       struct isl_basic_set *hull = NULL;
+       unsigned dim;
 
-       old_dim = set->dim;
-       new_dim = bounds->n_row;
-       H = isl_mat_sub_alloc(ctx, bounds->row, 0, bounds->n_row, 1, set->dim);
-       H = isl_mat_left_hermite(ctx, H, 0, &U, &Q);
-       if (!H)
+       if (!bset1 || !bset2)
                goto error;
-       U = isl_mat_lin_to_aff(ctx, U);
-       Q = isl_mat_lin_to_aff(ctx, Q);
-       Q->n_row = 1 + new_dim;
-       isl_mat_free(ctx, H);
-       set = isl_set_preimage(ctx, set, U);
-       set = set_project_out(ctx, set, old_dim - new_dim);
-       hull = uset_convex_hull(set);
-       hull = isl_basic_set_preimage(ctx, hull, Q);
-       isl_mat_free(ctx, bounds);
+
+       dim = isl_basic_set_n_dim(bset1);
+       hull = isl_basic_set_alloc(bset1->ctx, 0, 2 + 3 * dim, 0,
+                               1 + dim + bset1->n_eq + bset2->n_eq,
+                               2 + bset1->n_ineq + bset2->n_ineq);
+       bset[0] = bset1;
+       bset[1] = bset2;
+       for (i = 0; i < 2; ++i) {
+               for (j = 0; j < bset[i]->n_eq; ++j) {
+                       k = isl_basic_set_alloc_equality(hull);
+                       if (k < 0)
+                               goto error;
+                       isl_seq_clr(hull->eq[k], (i+1) * (1+dim));
+                       isl_seq_clr(hull->eq[k]+(i+2)*(1+dim), (1-i)*(1+dim));
+                       isl_seq_cpy(hull->eq[k]+(i+1)*(1+dim), bset[i]->eq[j],
+                                       1+dim);
+               }
+               for (j = 0; j < bset[i]->n_ineq; ++j) {
+                       k = isl_basic_set_alloc_inequality(hull);
+                       if (k < 0)
+                               goto error;
+                       isl_seq_clr(hull->ineq[k], (i+1) * (1+dim));
+                       isl_seq_clr(hull->ineq[k]+(i+2)*(1+dim), (1-i)*(1+dim));
+                       isl_seq_cpy(hull->ineq[k]+(i+1)*(1+dim),
+                                       bset[i]->ineq[j], 1+dim);
+               }
+               k = isl_basic_set_alloc_inequality(hull);
+               if (k < 0)
+                       goto error;
+               isl_seq_clr(hull->ineq[k], 1+2+3*dim);
+               isl_int_set_si(hull->ineq[k][(i+1)*(1+dim)], 1);
+       }
+       for (j = 0; j < 1+dim; ++j) {
+               k = isl_basic_set_alloc_equality(hull);
+               if (k < 0)
+                       goto error;
+               isl_seq_clr(hull->eq[k], 1+2+3*dim);
+               isl_int_set_si(hull->eq[k][j], -1);
+               isl_int_set_si(hull->eq[k][1+dim+j], 1);
+               isl_int_set_si(hull->eq[k][2*(1+dim)+j], 1);
+       }
+       hull = isl_basic_set_set_rational(hull);
+       hull = isl_basic_set_remove_dims(hull, dim, 2*(1+dim));
+       hull = isl_basic_set_convex_hull(hull);
+       isl_basic_set_free(bset1);
+       isl_basic_set_free(bset2);
        return hull;
 error:
-       isl_mat_free(ctx, bounds);
-       isl_mat_free(ctx, Q);
+       isl_basic_set_free(bset1);
+       isl_basic_set_free(bset2);
+       isl_basic_set_free(hull);
+       return NULL;
+}
+
+/* Compute the convex hull of a set without any parameters or
+ * integer divisions using Fourier-Motzkin elimination.
+ * In each step, we combined two basic sets until only one
+ * basic set is left.
+ */
+static struct isl_basic_set *uset_convex_hull_elim(struct isl_set *set)
+{
+       struct isl_basic_set *convex_hull = NULL;
+
+       convex_hull = isl_set_copy_basic_set(set);
+       set = isl_set_drop_basic_set(set, convex_hull);
+       if (!set)
+               goto error;
+       while (set->n > 0) {
+               struct isl_basic_set *t;
+               t = isl_set_copy_basic_set(set);
+               if (!t)
+                       goto error;
+               set = isl_set_drop_basic_set(set, t);
+               if (!set)
+                       goto error;
+               convex_hull = convex_hull_pair(convex_hull, t);
+       }
+       isl_set_free(set);
+       return convex_hull;
+error:
        isl_set_free(set);
+       isl_basic_set_free(convex_hull);
+       return NULL;
+}
+
+static struct isl_basic_set *uset_convex_hull_wrap_with_bounds(
+       struct isl_set *set, struct isl_mat *bounds)
+{
+       struct isl_basic_set *convex_hull = NULL;
+
+       isl_assert(set->ctx, bounds->n_row == isl_set_n_dim(set), goto error);
+       bounds = initial_facet_constraint(set->ctx, set, bounds);
+       if (!bounds)
+               goto error;
+       convex_hull = extend(set->ctx, set, bounds);
+       isl_mat_free(set->ctx, bounds);
+       isl_set_free(set);
+
+       return convex_hull;
+error:
+       isl_set_free(set);
+       return NULL;
+}
+
+/* Compute the convex hull of a set without any parameters or
+ * integer divisions.  Depending on whether the set is bounded,
+ * we pass control to the wrapping based convex hull or
+ * the Fourier-Motzkin elimination based convex hull.
+ * We also handle a few special cases before checking the boundedness.
+ */
+static struct isl_basic_set *uset_convex_hull(struct isl_set *set)
+{
+       int i;
+       struct isl_basic_set *convex_hull = NULL;
+       struct isl_mat *bounds;
+
+       if (isl_set_n_dim(set) == 0)
+               return convex_hull_0d(set);
+
+       set = isl_set_set_rational(set);
+
+       if (!set)
+               goto error;
+       for (i = 0; i < set->n; ++i) {
+               set->p[i] = isl_basic_set_convex_hull(set->p[i]);
+               if (!set->p[i])
+                       goto error;
+       }
+       set = isl_set_remove_empty_parts(set);
+       if (!set)
+               return NULL;
+       if (set->n == 1) {
+               convex_hull = isl_basic_set_copy(set->p[0]);
+               isl_set_free(set);
+               return convex_hull;
+       }
+       if (isl_set_n_dim(set) == 1)
+               return convex_hull_1d(set->ctx, set);
+
+       bounds = independent_bounds(set->ctx, set);
+       if (!bounds)
+               goto error;
+       if (bounds->n_row == isl_set_n_dim(set))
+               return uset_convex_hull_wrap_with_bounds(set, bounds);
+       isl_mat_free(set->ctx, bounds);
+
+       return uset_convex_hull_elim(set);
+error:
+       isl_set_free(set);
+       isl_basic_set_free(convex_hull);
        return NULL;
 }
 
@@ -852,14 +1074,14 @@ error:
  * without parameters or divs and where the convex hull of set is
  * known to be full-dimensional.
  */
-static struct isl_basic_set *uset_convex_hull(struct isl_set *set)
+static struct isl_basic_set *uset_convex_hull_wrap(struct isl_set *set)
 {
        int i;
        struct isl_basic_set *convex_hull = NULL;
        struct isl_mat *bounds;
 
-       if (set->dim == 0) {
-               convex_hull = isl_basic_set_universe(set->ctx, 0, 0);
+       if (isl_set_n_dim(set) == 0) {
+               convex_hull = isl_basic_set_universe(isl_dim_copy(set->dim));
                isl_set_free(set);
                convex_hull = isl_basic_set_set_rational(convex_hull);
                return convex_hull;
@@ -882,22 +1104,13 @@ static struct isl_basic_set *uset_convex_hull(struct isl_set *set)
                isl_set_free(set);
                return convex_hull;
        }
-       if (set->dim == 1)
+       if (isl_set_n_dim(set) == 1)
                return convex_hull_1d(set->ctx, set);
 
        bounds = independent_bounds(set->ctx, set);
        if (!bounds)
                goto error;
-       if (bounds->n_row < set->dim)
-               return modulo_lineality(set->ctx, set, bounds);
-       bounds = initial_facet_constraint(set->ctx, set, bounds);
-       if (!bounds)
-               goto error;
-       convex_hull = extend(set->ctx, set, bounds);
-       isl_mat_free(set->ctx, bounds);
-       isl_set_free(set);
-
-       return convex_hull;
+       return uset_convex_hull_wrap_with_bounds(set, bounds);
 error:
        isl_set_free(set);
        return NULL;
@@ -940,6 +1153,7 @@ error:
 struct isl_basic_map *isl_map_convex_hull(struct isl_map *map)
 {
        struct isl_basic_set *bset;
+       struct isl_basic_map *model = NULL;
        struct isl_basic_set *affine_hull = NULL;
        struct isl_basic_map *convex_hull = NULL;
        struct isl_set *set = NULL;
@@ -950,13 +1164,14 @@ struct isl_basic_map *isl_map_convex_hull(struct isl_map *map)
 
        ctx = map->ctx;
        if (map->n == 0) {
-               convex_hull = isl_basic_map_empty(ctx,
-                                           map->nparam, map->n_in, map->n_out);
+               convex_hull = isl_basic_map_empty_like_map(map);
                isl_map_free(map);
                return convex_hull;
        }
 
-       set = isl_map_underlying_set(isl_map_copy(map));
+       map = isl_map_align_divs(map);
+       model = isl_basic_map_copy(map->p[0]);
+       set = isl_map_underlying_set(map);
        if (!set)
                goto error;
 
@@ -970,14 +1185,13 @@ struct isl_basic_map *isl_map_convex_hull(struct isl_map *map)
                bset = uset_convex_hull(set);
        }
 
-       convex_hull = isl_basic_map_overlying_set(bset,
-                       isl_basic_map_copy(map->p[0]));
+       convex_hull = isl_basic_map_overlying_set(bset, model);
 
-       isl_map_free(map);
+       F_CLR(convex_hull, ISL_BASIC_MAP_RATIONAL);
        return convex_hull;
 error:
        isl_set_free(set);
-       isl_map_free(map);
+       isl_basic_map_free(model);
        return NULL;
 }
 
@@ -986,3 +1200,94 @@ struct isl_basic_set *isl_set_convex_hull(struct isl_set *set)
        return (struct isl_basic_set *)
                isl_map_convex_hull((struct isl_map *)set);
 }
+
+/* Compute a superset of the convex hull of map that is described
+ * by only translates of the constraints in the constituents of map.
+ *
+ * The implementation is not very efficient.  In particular, if
+ * constraints with the same normal appear in more than one
+ * basic map, they will be (re)examined each time.
+ */
+struct isl_basic_map *isl_map_simple_hull(struct isl_map *map)
+{
+       struct isl_set *set = NULL;
+       struct isl_basic_map *model = NULL;
+       struct isl_basic_map *hull;
+       struct isl_basic_set *bset = NULL;
+       int i, j;
+       unsigned n_ineq;
+       unsigned dim;
+
+       if (!map)
+               return NULL;
+       if (map->n == 0) {
+               hull = isl_basic_map_empty_like_map(map);
+               isl_map_free(map);
+               return hull;
+       }
+       if (map->n == 1) {
+               hull = isl_basic_map_copy(map->p[0]);
+               isl_map_free(map);
+               return hull;
+       }
+
+       map = isl_map_align_divs(map);
+       model = isl_basic_map_copy(map->p[0]);
+
+       n_ineq = 0;
+       for (i = 0; i < map->n; ++i) {
+               if (!map->p[i])
+                       goto error;
+               n_ineq += map->p[i]->n_ineq;
+       }
+
+       set = isl_map_underlying_set(map);
+       if (!set)
+               goto error;
+
+       bset = isl_set_affine_hull(isl_set_copy(set));
+       if (!bset)
+               goto error;
+       dim = isl_basic_set_n_dim(bset);
+       bset = isl_basic_set_extend(bset, 0, dim, 0, 0, n_ineq);
+       if (!bset)
+               goto error;
+
+       for (i = 0; i < set->n; ++i) {
+               for (j = 0; j < set->p[i]->n_ineq; ++j) {
+                       int k;
+                       int is_bound;
+
+                       k = isl_basic_set_alloc_inequality(bset);
+                       if (k < 0)
+                               goto error;
+                       isl_seq_cpy(bset->ineq[k], set->p[i]->ineq[j], 1 + dim);
+                       is_bound = uset_is_bound(set->ctx, set, bset->ineq[k],
+                                                       1 + dim);
+                       if (is_bound < 0)
+                               goto error;
+                       if (!is_bound)
+                               isl_basic_set_free_inequality(bset, 1);
+               }
+       }
+
+       bset = isl_basic_set_simplify(bset);
+       bset = isl_basic_set_finalize(bset);
+       bset = isl_basic_set_convex_hull(bset);
+
+       hull = isl_basic_map_overlying_set(bset, isl_basic_map_copy(model));
+
+       isl_set_free(set);
+       return hull;
+error:
+       isl_basic_set_free(bset);
+       isl_set_free(set);
+       isl_basic_map_free(model);
+       return NULL;
+}
+
+struct isl_basic_set *isl_set_simple_hull(struct isl_set *set)
+{
+       return (struct isl_basic_set *)
+               isl_map_simple_hull((struct isl_map *)set);
+}