isl_map.c: avoid potential NULL dereference
[platform/upstream/isl.git] / isl_convex_hull.c
index 8c7ea3f..8e6c5d7 100644 (file)
@@ -6,10 +6,9 @@
 #include "isl_seq.h"
 #include "isl_equalities.h"
 
-static struct isl_basic_set *uset_convex_hull(struct isl_ctx *ctx,
-       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;
 
@@ -20,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 = (*bmap)->nparam + (*bmap)->n_in + (*bmap)->n_out + (*bmap)->n_div;
+       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.
@@ -28,14 +80,14 @@ static swap_ineq(struct isl_basic_map *bmap, unsigned i, unsigned j)
  * corresponding equality and the checked if the dimension was that
  * of a facet.
  */
-struct isl_basic_map *isl_basic_map_convex_hull(struct isl_ctx *ctx,
-                                               struct isl_basic_map *bmap)
+struct isl_basic_map *isl_basic_map_convex_hull(struct isl_basic_map *bmap)
 {
        int i;
        isl_int opt_n;
        isl_int opt_d;
+       struct isl_ctx *ctx;
 
-       bmap = isl_basic_map_implicit_equalities(ctx, bmap);
+       bmap = isl_basic_map_implicit_equalities(bmap);
        if (!bmap)
                return NULL;
 
@@ -44,27 +96,23 @@ struct isl_basic_map *isl_basic_map_convex_hull(struct isl_ctx *ctx,
        if (F_ISSET(bmap, ISL_BASIC_MAP_NO_REDUNDANT))
                return bmap;
 
+       ctx = bmap->ctx;
        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(ctx, 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))
-                       isl_basic_map_drop_inequality(ctx, bmap, i);
+               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);
        isl_int_clear(opt_d);
@@ -74,85 +122,107 @@ struct isl_basic_map *isl_basic_map_convex_hull(struct isl_ctx *ctx,
 error:
        isl_int_clear(opt_n);
        isl_int_clear(opt_d);
-       isl_basic_map_free(ctx, bmap);
+       isl_basic_map_free(bmap);
        return NULL;
 }
 
-struct isl_basic_set *isl_basic_set_convex_hull(struct isl_ctx *ctx,
-                                               struct isl_basic_set *bset)
+struct isl_basic_set *isl_basic_set_convex_hull(struct isl_basic_set *bset)
 {
-       return (struct isl_basic_set *)isl_basic_map_convex_hull(ctx,
-                                               (struct isl_basic_map *)bset);
+       return (struct isl_basic_set *)
+               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)
                        goto error;
                if (res == isl_lp_empty) {
-                       set->p[j] = isl_basic_set_set_to_empty(ctx, set->p[j]);
+                       set->p[j] = isl_basic_set_set_to_empty(set->p[j]);
                        if (!set->p[j])
                                goto error;
                        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];
@@ -161,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
@@ -191,16 +257,6 @@ static struct isl_mat *independent_bounds(struct isl_ctx *ctx,
                                                dirs, n);
                        if (f < 0)
                                goto error;
-                       if (f) {
-                               ++n;
-                               continue;
-                       }
-                       isl_seq_neg(bset->eq[j], bset->eq[j], 1+set->dim);
-                       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;
                }
@@ -220,7 +276,7 @@ error:
        return NULL;
 }
 
-static struct isl_basic_set *isl_basic_set_set_rational(struct isl_ctx *ctx,
+static struct isl_basic_set *isl_basic_set_set_rational(
        struct isl_basic_set *bset)
 {
        if (!bset)
@@ -229,31 +285,30 @@ static struct isl_basic_set *isl_basic_set_set_rational(struct isl_ctx *ctx,
        if (F_ISSET(bset, ISL_BASIC_MAP_RATIONAL))
                return bset;
 
-       bset = isl_basic_set_cow(ctx, bset);
+       bset = isl_basic_set_cow(bset);
        if (!bset)
                return NULL;
 
        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_ctx *ctx,
-       struct isl_set *set)
+static struct isl_set *isl_set_set_rational(struct isl_set *set)
 {
        int i;
 
-       set = isl_set_cow(ctx, set);
+       set = isl_set_cow(set);
        if (!set)
                return NULL;
        for (i = 0; i < set->n; ++i) {
-               set->p[i] = isl_basic_set_set_rational(ctx, set->p[i]);
+               set->p[i] = isl_basic_set_set_rational(set->p[i]);
                if (!set->p[i])
                        goto error;
        }
        return set;
 error:
-       isl_set_free(ctx, set);
+       isl_set_free(set);
        return NULL;
 }
 
@@ -263,16 +318,19 @@ static struct isl_basic_set *isl_basic_set_add_equality(struct isl_ctx *ctx,
        int i;
        unsigned total;
 
+       if (F_ISSET(bset, ISL_BASIC_SET_EMPTY))
+               return bset;
+
        isl_assert(ctx, bset->nparam == 0, goto error);
        isl_assert(ctx, bset->n_div == 0, goto error);
-       bset = isl_basic_set_extend(ctx, bset, 0, bset->dim, 0, 1, 0);
-       i = isl_basic_set_alloc_equality(ctx, bset);
+       bset = isl_basic_set_extend(bset, 0, bset->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);
        return bset;
 error:
-       isl_basic_set_free(ctx, bset);
+       isl_basic_set_free(bset);
        return NULL;
 }
 
@@ -281,7 +339,7 @@ static struct isl_set *isl_set_add_equality(struct isl_ctx *ctx,
 {
        int i;
 
-       set = isl_set_cow(ctx, set);
+       set = isl_set_cow(set);
        if (!set)
                return NULL;
        for (i = 0; i < set->n; ++i) {
@@ -291,7 +349,7 @@ static struct isl_set *isl_set_add_equality(struct isl_ctx *ctx,
        }
        return set;
 error:
-       isl_set_free(ctx, set);
+       isl_set_free(set);
        return NULL;
 }
 
@@ -334,7 +392,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;
-       k = isl_basic_set_alloc_equality(ctx, lp);
+       k = isl_basic_set_alloc_equality(lp);
        isl_int_set_si(lp->eq[k][0], -1);
        for (i = 0; i < set->n; ++i) {
                isl_int_set_si(lp->eq[k][1+dim*i], 0);
@@ -342,19 +400,19 @@ static struct isl_basic_set *wrap_constraints(struct isl_ctx *ctx,
                isl_seq_clr(lp->eq[k]+1+dim*i+2, dim-2);
        }
        for (i = 0; i < set->n; ++i) {
-               k = isl_basic_set_alloc_inequality(ctx, lp);
+               k = isl_basic_set_alloc_inequality(lp);
                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) {
-                       k = isl_basic_set_alloc_equality(ctx, lp);
+                       k = isl_basic_set_alloc_equality(lp);
                        isl_seq_clr(lp->eq[k], 1+dim*i);
                        isl_seq_cpy(lp->eq[k]+1+dim*i, set->p[i]->eq[j], dim);
                        isl_seq_clr(lp->eq[k]+1+dim*(i+1), dim*(set->n-i-1));
                }
 
                for (j = 0; j < set->p[i]->n_ineq; ++j) {
-                       k = isl_basic_set_alloc_inequality(ctx, lp);
+                       k = isl_basic_set_alloc_inequality(lp);
                        isl_seq_clr(lp->ineq[k], 1+dim*i);
                        isl_seq_cpy(lp->ineq[k]+1+dim*i, set->p[i]->ineq[j], dim);
                        isl_seq_clr(lp->ineq[k]+1+dim*(i+1), dim*(set->n-i-1));
@@ -371,7 +429,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
  *
@@ -408,13 +466,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)
@@ -427,7 +489,7 @@ static isl_int *wrap_facet(struct isl_ctx *ctx, struct isl_set *set,
        isl_int num, den;
        unsigned dim;
 
-       set = isl_set_copy(ctx, set);
+       set = isl_set_copy(set);
 
        dim = 1 + set->dim;
        T = isl_mat_alloc(ctx, 3, 1 + set->dim);
@@ -440,6 +502,8 @@ static isl_int *wrap_facet(struct isl_ctx *ctx, struct isl_set *set,
        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)
@@ -460,72 +524,25 @@ static isl_int *wrap_facet(struct isl_ctx *ctx, struct isl_set *set,
        isl_int_clear(num);
        isl_int_clear(den);
        isl_vec_free(ctx, obj);
-       isl_basic_set_free(ctx, lp);
-       isl_set_free(ctx, set);
-       return (res == isl_lp_ok) ? facet : NULL;
+       isl_basic_set_free(lp);
+       isl_set_free(set);
+       isl_assert(ctx, res == isl_lp_ok || res == isl_lp_unbounded, 
+                  return NULL);
+       return facet;
 error:
-       isl_basic_set_free(ctx, lp);
+       isl_basic_set_free(lp);
        isl_mat_free(ctx, T);
-       isl_set_free(ctx, set);
+       isl_set_free(set);
        return NULL;
 }
 
-/* Given a direction of a constraint, compute the constant term
- * such that the resulting constraint is a bounding constraint
- * of the set "set" (which just happens to be a face of the
- * original set).
- */
-static int compute_bound_on_face(struct isl_ctx *ctx,
-       struct isl_set *set, isl_int *c)
-{
-       int first = 1;
-       int j;
-       isl_int opt;
-       isl_int opt_denom;
-
-       isl_int_init(opt);
-       isl_int_init(opt_denom);
-       for (j = 0; j < set->n; ++j) {
-               enum isl_lp_result res;
-
-               if (F_ISSET(set->p[j], ISL_BASIC_MAP_EMPTY))
-                       continue;
-
-               res = isl_solve_lp((struct isl_basic_map*)set->p[j],
-                                       0, c+1, ctx->one, &opt, &opt_denom);
-               if (res == isl_lp_unbounded)
-                       goto error;
-               if (res == isl_lp_error)
-                       goto error;
-               if (res == isl_lp_empty) {
-                       set->p[j] = isl_basic_set_set_to_empty(ctx, set->p[j]);
-                       if (!set->p[j])
-                               goto error;
-                       continue;
-               }
-               if (!isl_int_is_one(opt_denom))
-                       isl_seq_scale(c, c, opt_denom, 1+set->dim);
-               if (first || isl_int_lt(opt, c[0]))
-                       isl_int_set(c[0], opt);
-               first = 0;
-       }
-       isl_assert(ctx, !first, goto error);
-       isl_int_clear(opt);
-       isl_int_clear(opt_denom);
-       isl_int_neg(c[0], c[0]);
-       return 0;
-error:
-       isl_int_clear(opt);
-       isl_int_clear(opt_denom);
-       return -1;
-}
-
 /* Given a set of d linearly independent bounding constraints of the
  * convex hull of "set", compute the constraint of a facet of "set".
  *
  * We first compute the intersection with the first bounding hyperplane
- * and shift the second bounding constraint to be a bounding constraint
- * of the resulting face.  We then wrap around the next bounding constraint
+ * and remove the component corresponding to this hyperplane from
+ * other bounds (in homogeneous space).
+ * We then wrap around one of the remaining bounding constraints
  * and continue the process until all bounding constraints have been
  * taken into account.
  * The resulting linear combination of the bounding constraints will
@@ -534,26 +551,55 @@ error:
 static struct isl_mat *initial_facet_constraint(struct isl_ctx *ctx,
        struct isl_set *set, struct isl_mat *bounds)
 {
-       struct isl_set *face = NULL;
+       struct isl_set *slice = NULL;
+       struct isl_basic_set *face = NULL;
+       struct isl_mat *m, *U, *Q;
        int i;
 
        isl_assert(ctx, set->n > 0, goto error);
        isl_assert(ctx, bounds->n_row == set->dim, goto error);
 
-       face = isl_set_copy(ctx, set);
-       if (!face)
-               goto error;
-       for (i = 1; i < set->dim; ++i) {
-               face = isl_set_add_equality(ctx, face, bounds->row[i-1]);
-               if (compute_bound_on_face(ctx, face, bounds->row[i]) < 0)
+       while (bounds->n_row > 1) {
+               slice = isl_set_copy(set);
+               slice = isl_set_add_equality(ctx, slice, bounds->row[0]);
+               face = isl_set_affine_hull(slice);
+               if (!face)
                        goto error;
-               if (!wrap_facet(ctx, set, bounds->row[0], bounds->row[i]))
+               if (face->n_eq == 1) {
+                       isl_basic_set_free(face);
+                       break;
+               }
+               m = isl_mat_alloc(ctx, 1 + face->n_eq, 1 + face->dim);
+               if (!m)
+                       goto error;
+               isl_int_set_si(m->row[0][0], 1);
+               isl_seq_clr(m->row[0]+1, face->dim);
+               for (i = 0; i < face->n_eq; ++i)
+                       isl_seq_cpy(m->row[1 + i], face->eq[i], 1 + face->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);
+               Q = isl_mat_drop_rows(ctx, Q, 1 + face->n_eq,
+                                               face->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);
+               bounds = isl_mat_product(ctx, bounds, Q);
+               while (isl_seq_first_non_zero(bounds->row[bounds->n_row-1],
+                                             bounds->n_col) == -1) {
+                       bounds->n_row--;
+                       isl_assert(ctx, bounds->n_row > 1, goto error);
+               }
+               if (!wrap_facet(ctx, set, bounds->row[0],
+                                         bounds->row[bounds->n_row-1]))
                        goto error;
+               isl_basic_set_free(face);
+               bounds->n_row--;
        }
-       isl_set_free(ctx, face);
        return bounds;
 error:
-       isl_set_free(ctx, face);
+       isl_basic_set_free(face);
        isl_mat_free(ctx, bounds);
        return NULL;
 }
@@ -603,25 +649,23 @@ static struct isl_basic_set *compute_facet(struct isl_ctx *ctx,
        struct isl_mat *m, *U, *Q;
        struct isl_basic_set *facet;
 
-       set = isl_set_copy(ctx, set);
+       set = isl_set_copy(set);
        m = isl_mat_alloc(ctx, 2, 1 + set->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);
-       m = isl_mat_left_hermite(ctx, m, 0, &U, &Q);
-       if (!m)
-               goto error;
+       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(ctx, set);
+       facet = uset_convex_hull_wrap(set);
        facet = isl_basic_set_preimage(ctx, facet, Q);
-       isl_mat_free(ctx, m);
        return facet;
 error:
-       isl_set_free(ctx, set);
+       isl_set_free(set);
        return NULL;
 }
 
@@ -656,11 +700,11 @@ static struct isl_basic_set *extend(struct isl_ctx *ctx, struct isl_set *set,
                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 + 2 * set->p[0]->n_div);
+       hull = isl_basic_set_alloc(ctx, 0, set->dim, 0, 0, n_ineq);
+       hull = isl_basic_set_set_rational(hull);
        if (!hull)
                goto error;
-       k = isl_basic_set_alloc_inequality(ctx, hull);
+       k = isl_basic_set_alloc_inequality(hull);
        if (k < 0)
                goto error;
        isl_seq_cpy(hull->ineq[k], initial->row[0], initial->n_col);
@@ -668,8 +712,13 @@ static struct isl_basic_set *extend(struct isl_ctx *ctx, struct isl_set *set,
                facet = compute_facet(ctx, set, hull->ineq[i]);
                if (!facet)
                        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);
+                       n_ineq = hull->n_ineq + facet->n_ineq;
+               }
                for (j = 0; j < facet->n_ineq; ++j) {
-                       k = isl_basic_set_alloc_inequality(ctx, hull);
+                       k = isl_basic_set_alloc_inequality(hull);
                        if (k < 0)
                                goto error;
                        isl_seq_cpy(hull->ineq[k], hull->ineq[i], 1+hull->dim);
@@ -680,15 +729,16 @@ static struct isl_basic_set *extend(struct isl_ctx *ctx, struct isl_set *set,
                                                1+hull->dim))
                                        break;
                        if (f < k)
-                               isl_basic_set_free_inequality(ctx, hull, 1);
+                               isl_basic_set_free_inequality(hull, 1);
                }
-               isl_basic_set_free(ctx, facet);
+               isl_basic_set_free(facet);
        }
-       hull = isl_basic_set_simplify(ctx, hull);
-       hull = isl_basic_set_finalize(ctx, hull);
+       hull = isl_basic_set_simplify(hull);
+       hull = isl_basic_set_finalize(hull);
        return hull;
 error:
-       isl_basic_set_free(ctx, hull);
+       isl_basic_set_free(facet);
+       isl_basic_set_free(hull);
        return NULL;
 }
 
@@ -707,11 +757,11 @@ static struct isl_basic_set *convex_hull_1d(struct isl_ctx *ctx,
        struct isl_basic_set *hull;
 
        for (i = 0; i < set->n; ++i) {
-               set->p[i] = isl_basic_set_simplify(ctx, set->p[i]);
+               set->p[i] = isl_basic_set_simplify(set->p[i]);
                if (!set->p[i])
                        goto error;
        }
-       set = isl_set_remove_empty_parts(ctx, set);
+       set = isl_set_remove_empty_parts(set);
        if (!set)
                goto error;
        isl_assert(ctx, set->n > 0, goto error);
@@ -796,22 +846,23 @@ static struct isl_basic_set *convex_hull_1d(struct isl_ctx *ctx,
        isl_int_clear(b);
 
        hull = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
+       hull = isl_basic_set_set_rational(hull);
        if (!hull)
                goto error;
        if (lower) {
-               k = isl_basic_set_alloc_inequality(ctx, hull);
+               k = isl_basic_set_alloc_inequality(hull);
                isl_seq_cpy(hull->ineq[k], lower, 2);
        }
        if (upper) {
-               k = isl_basic_set_alloc_inequality(ctx, hull);
+               k = isl_basic_set_alloc_inequality(hull);
                isl_seq_cpy(hull->ineq[k], upper, 2);
        }
-       hull = isl_basic_set_finalize(ctx, hull);
-       isl_set_free(ctx, set);
+       hull = isl_basic_set_finalize(hull);
+       isl_set_free(set);
        isl_mat_free(ctx, c);
        return hull;
 error:
-       isl_set_free(ctx, set);
+       isl_set_free(set);
        isl_mat_free(ctx, c);
        return NULL;
 }
@@ -820,60 +871,198 @@ error:
 static struct isl_set *set_project_out(struct isl_ctx *ctx,
        struct isl_set *set, unsigned n)
 {
-       int i;
+       return isl_set_remove_dims(set, set->dim - n, n);
+}
+
+static struct isl_basic_set *convex_hull_0d(struct isl_set *set)
+{
+       struct isl_basic_set *convex_hull;
 
-       set = isl_set_cow(ctx, set);
        if (!set)
                return NULL;
-       
-       for (i = 0; i < set->n; ++i) {
-               set->p[i] = isl_basic_set_eliminate_vars(ctx, set->p[i],
-                                                           set->dim - n, n);
-               if (!set->p[i])
+
+       if (isl_set_is_empty(set))
+               convex_hull = isl_basic_set_empty(set->ctx, 0, 0);
+       else
+               convex_hull = isl_basic_set_universe(set->ctx, 0, 0);
+       isl_set_free(set);
+       return convex_hull;
+}
+
+/* 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 *convex_hull_pair(struct isl_basic_set *bset1,
+       struct isl_basic_set *bset2)
+{
+       int i, j, k;
+       struct isl_basic_set *bset[2];
+       struct isl_basic_set *hull = NULL;
+       unsigned dim;
+
+       if (!bset1 || !bset2)
+               goto error;
+
+       dim = bset1->dim;
+       hull = isl_basic_set_alloc(bset1->ctx, 0, 2 + 3 * bset1->dim, 0,
+                               1 + bset1->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+hull->dim);
+               isl_int_set_si(hull->ineq[k][(i+1)*(1+dim)], 1);
        }
-       set = isl_set_drop_vars(ctx, set, set->dim - n, n);
-       return set;
+       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+hull->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_set_free(ctx, set);
+       isl_basic_set_free(bset1);
+       isl_basic_set_free(bset2);
+       isl_basic_set_free(hull);
        return NULL;
 }
 
-/* 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 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 *modulo_lineality(struct isl_ctx *ctx,
+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)
 {
-       int i, j;
-       unsigned old_dim, new_dim;
-       struct isl_mat *H = NULL, *U = NULL, *Q = NULL;
-       struct isl_basic_set *hull;
+       struct isl_basic_set *convex_hull = NULL;
 
-       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)
+       isl_assert(set->ctx, bounds->n_row == set->dim, goto error);
+       bounds = initial_facet_constraint(set->ctx, set, bounds);
+       if (!bounds)
                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(ctx, set);
-       hull = isl_basic_set_preimage(ctx, hull, Q);
-       isl_mat_free(ctx, bounds);
-       return hull;
+       convex_hull = extend(set->ctx, set, bounds);
+       isl_mat_free(set->ctx, bounds);
+       isl_set_free(set);
+
+       return convex_hull;
 error:
-       isl_mat_free(ctx, bounds);
-       isl_mat_free(ctx, Q);
-       isl_set_free(ctx, set);
+       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 (set->dim == 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 == 0) {
+               convex_hull = isl_basic_set_empty(set->ctx, 0, 0);
+               isl_set_free(set);
+               return convex_hull;
+       }
+       if (set->n == 1) {
+               convex_hull = isl_basic_set_copy(set->p[0]);
+               isl_set_free(set);
+               return convex_hull;
+       }
+       if (set->dim == 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 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;
 }
 
@@ -881,54 +1070,45 @@ 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_ctx *ctx,
-       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(ctx, 0, 0);
-               isl_set_free(ctx, set);
+               convex_hull = isl_basic_set_universe(set->ctx, 0, 0);
+               isl_set_free(set);
+               convex_hull = isl_basic_set_set_rational(convex_hull);
                return convex_hull;
        }
 
-       set = isl_set_set_rational(ctx, 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(ctx, set->p[i]);
+               set->p[i] = isl_basic_set_convex_hull(set->p[i]);
                if (!set->p[i])
                        goto error;
        }
-       set = isl_set_remove_empty_parts(ctx, set);
+       set = isl_set_remove_empty_parts(set);
        if (!set)
                goto error;
        if (set->n == 1) {
-               convex_hull = isl_basic_set_copy(ctx, set->p[0]);
-               isl_set_free(ctx, set);
+               convex_hull = isl_basic_set_copy(set->p[0]);
+               isl_set_free(set);
                return convex_hull;
        }
        if (set->dim == 1)
-               return convex_hull_1d(ctx, set);
+               return convex_hull_1d(set->ctx, set);
 
-       bounds = independent_bounds(ctx, set);
-       if (!bounds)
-               goto error;
-       if (bounds->n_row < set->dim)
-               return modulo_lineality(ctx, set, bounds);
-       bounds = initial_facet_constraint(ctx, set, bounds);
+       bounds = independent_bounds(set->ctx, set);
        if (!bounds)
                goto error;
-       convex_hull = extend(ctx, set, bounds);
-       isl_mat_free(ctx, bounds);
-       isl_set_free(ctx, set);
-
-       return convex_hull;
+       return uset_convex_hull_wrap_with_bounds(set, bounds);
 error:
-       isl_set_free(ctx, set);
+       isl_set_free(set);
        return NULL;
 }
 
@@ -945,19 +1125,19 @@ static struct isl_basic_set *modulo_affine_hull(struct isl_ctx *ctx,
        struct isl_basic_set *dummy;
        struct isl_basic_set *convex_hull;
 
-       dummy = isl_basic_set_remove_equalities(ctx,
-                       isl_basic_set_copy(ctx, affine_hull), &T, &T2);
+       dummy = isl_basic_set_remove_equalities(
+                       isl_basic_set_copy(affine_hull), &T, &T2);
        if (!dummy)
                goto error;
-       isl_basic_set_free(ctx, dummy);
+       isl_basic_set_free(dummy);
        set = isl_set_preimage(ctx, set, T);
-       convex_hull = uset_convex_hull(ctx, set);
+       convex_hull = uset_convex_hull(set);
        convex_hull = isl_basic_set_preimage(ctx, convex_hull, T2);
-       convex_hull = isl_basic_set_intersect(ctx, convex_hull, affine_hull);
+       convex_hull = isl_basic_set_intersect(convex_hull, affine_hull);
        return convex_hull;
 error:
-       isl_basic_set_free(ctx, affine_hull);
-       isl_set_free(ctx, set);
+       isl_basic_set_free(affine_hull);
+       isl_set_free(set);
        return NULL;
 }
 
@@ -966,52 +1146,135 @@ error:
  * The implementation was inspired by "Extended Convex Hull" by Fukuda et al.,
  * specifically, the wrapping of facets to obtain new facets.
  */
-struct isl_basic_map *isl_map_convex_hull(struct isl_ctx *ctx,
-                                               struct isl_map *map)
+struct isl_basic_map *isl_map_convex_hull(struct isl_map *map)
 {
        struct isl_basic_set *bset;
        struct isl_basic_set *affine_hull = NULL;
        struct isl_basic_map *convex_hull = NULL;
        struct isl_set *set = NULL;
+       struct isl_ctx *ctx;
 
        if (!map)
                goto error;
 
+       ctx = map->ctx;
        if (map->n == 0) {
                convex_hull = isl_basic_map_empty(ctx,
                                            map->nparam, map->n_in, map->n_out);
-               isl_map_free(ctx, map);
+               isl_map_free(map);
                return convex_hull;
        }
 
-       set = isl_map_underlying_set(ctx, isl_map_copy(ctx, map));
+       set = isl_map_underlying_set(isl_map_copy(map));
        if (!set)
                goto error;
 
-       affine_hull = isl_set_affine_hull(ctx, isl_set_copy(ctx, set));
+       affine_hull = isl_set_affine_hull(isl_set_copy(set));
        if (!affine_hull)
                goto error;
        if (affine_hull->n_eq != 0)
                bset = modulo_affine_hull(ctx, set, affine_hull);
        else {
-               isl_basic_set_free(ctx, affine_hull);
-               bset = uset_convex_hull(ctx, set);
+               isl_basic_set_free(affine_hull);
+               bset = uset_convex_hull(set);
        }
 
-       convex_hull = isl_basic_map_overlying_set(ctx, bset,
-                       isl_basic_map_copy(ctx, map->p[0]));
+       convex_hull = isl_basic_map_overlying_set(bset,
+                       isl_basic_map_copy(map->p[0]));
 
-       isl_map_free(ctx, map);
+       isl_map_free(map);
+       F_CLR(convex_hull, ISL_BASIC_MAP_RATIONAL);
        return convex_hull;
 error:
-       isl_set_free(ctx, set);
-       isl_map_free(ctx, map);
+       isl_set_free(set);
+       isl_map_free(map);
+       return NULL;
+}
+
+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.
+ */
+struct isl_basic_map *isl_map_simple_hull(struct isl_map *map)
+{
+       struct isl_set *set = NULL;
+       struct isl_basic_map *hull;
+       struct isl_basic_set *bset = NULL;
+       int i, j;
+       unsigned n_ineq;
+
+       if (map->n == 0) {
+               hull = isl_basic_map_empty(map->ctx,
+                                           map->nparam, map->n_in, map->n_out);
+               isl_map_free(map);
+               return hull;
+       }
+       if (map->n == 1) {
+               hull = isl_basic_map_copy(map->p[0]);
+               isl_map_free(map);
+               return hull;
+       }
+
+       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(isl_map_copy(map));
+       if (!set)
+               goto error;
+
+       bset = isl_set_affine_hull(isl_set_copy(set));
+       if (!bset)
+               goto error;
+       bset = isl_basic_set_extend(bset, 0, bset->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 + bset->dim);
+                       is_bound = uset_is_bound(set->ctx, set, bset->ineq[k],
+                                                       1 + bset->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(map->p[0]));
+
+       isl_set_free(set);
+       isl_map_free(map);
+       return hull;
+error:
+       isl_basic_set_free(bset);
+       isl_set_free(set);
+       isl_map_free(map);
        return NULL;
 }
 
-struct isl_basic_set *isl_set_convex_hull(struct isl_ctx *ctx,
-                                               struct isl_set *set)
+struct isl_basic_set *isl_set_simple_hull(struct isl_set *set)
 {
        return (struct isl_basic_set *)
-               isl_map_convex_hull(ctx, (struct isl_map *)set);
+               isl_map_simple_hull((struct isl_map *)set);
 }