isl_basic_set_opt: avoid invalid access on error path
[platform/upstream/isl.git] / isl_affine_hull.c
index 68717a2..e71bc10 100644 (file)
@@ -1,12 +1,22 @@
-#include "isl_ctx.h"
-#include "isl_seq.h"
-#include "isl_set.h"
-#include "isl_lp.h"
-#include "isl_map.h"
-#include "isl_map_private.h"
+/*
+ * Copyright 2008-2009 Katholieke Universiteit Leuven
+ *
+ * Use of this software is governed by the MIT license
+ *
+ * Written by Sven Verdoolaege, K.U.Leuven, Departement
+ * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
+ */
+
+#include <isl_ctx_private.h>
+#include <isl_map_private.h>
+#include <isl/seq.h>
+#include <isl/set.h>
+#include <isl/lp.h>
+#include <isl/map.h>
 #include "isl_equalities.h"
 #include "isl_sample.h"
 #include "isl_tab.h"
+#include <isl_mat_private.h>
 
 struct isl_basic_map *isl_basic_map_implicit_equalities(
                                                struct isl_basic_map *bmap)
@@ -24,13 +34,18 @@ struct isl_basic_map *isl_basic_map_implicit_equalities(
        if (bmap->n_ineq <= 1)
                return bmap;
 
-       tab = isl_tab_from_basic_map(bmap);
-       tab = isl_tab_detect_equalities(tab);
+       tab = isl_tab_from_basic_map(bmap, 0);
+       if (isl_tab_detect_implicit_equalities(tab) < 0)
+               goto error;
        bmap = isl_basic_map_update_from_tab(bmap, tab);
        isl_tab_free(tab);
        bmap = isl_basic_map_gauss(bmap, NULL);
        ISL_F_SET(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
        return bmap;
+error:
+       isl_tab_free(tab);
+       isl_basic_map_free(bmap);
+       return NULL;
 }
 
 struct isl_basic_set *isl_basic_set_implicit_equalities(
@@ -192,6 +207,9 @@ static struct isl_basic_set *affine_hull(
        int col;
        int row;
 
+       if (!bset1 || !bset2)
+               goto error;
+
        total = 1 + isl_basic_set_n_dim(bset1);
 
        row = 0;
@@ -212,111 +230,78 @@ static struct isl_basic_set *affine_hull(
                                --row;
                }
        }
-       isl_basic_set_free(bset2);
        isl_assert(bset1->ctx, row == bset1->n_eq, goto error);
+       isl_basic_set_free(bset2);
        bset1 = isl_basic_set_normalize_constraints(bset1);
        return bset1;
 error:
        isl_basic_set_free(bset1);
+       isl_basic_set_free(bset2);
        return NULL;
 }
 
-static struct isl_basic_set *isl_basic_set_from_vec(struct isl_vec *vec)
-{
-       int i;
-       int k;
-       struct isl_basic_set *bset = NULL;
-       struct isl_ctx *ctx;
-       unsigned dim;
-
-       if (!vec)
-               return NULL;
-       ctx = vec->ctx;
-       isl_assert(ctx, vec->size != 0, goto error);
-
-       bset = isl_basic_set_alloc(ctx, 0, vec->size - 1, 0, vec->size - 1, 0);
-       if (!bset)
-               goto error;
-       dim = isl_basic_set_n_dim(bset);
-       for (i = dim - 1; i >= 0; --i) {
-               k = isl_basic_set_alloc_equality(bset);
-               if (k < 0)
-                       goto error;
-               isl_seq_clr(bset->eq[k], 1 + dim);
-               isl_int_neg(bset->eq[k][0], vec->el[1 + i]);
-               isl_int_set(bset->eq[k][1 + i], vec->el[0]);
-       }
-       isl_vec_free(vec);
-
-       return bset;
-error:
-       isl_basic_set_free(bset);
-       isl_vec_free(vec);
-       return NULL;
-}
-
-/* Find an integer point in "bset" that lies outside of the equality
- * "eq" e(x) = 0.
+/* Find an integer point in the set represented by "tab"
+ * that lies outside of the equality "eq" e(x) = 0.
  * If "up" is true, look for a point satisfying e(x) - 1 >= 0.
  * Otherwise, look for a point satisfying -e(x) - 1 >= 0 (i.e., e(x) <= -1).
- * The point, if found, is returned as a singleton set.
- * If no point can be found, the empty set is returned.
+ * The point, if found, is returned.
+ * If no point can be found, a zero-length vector is returned.
  *
  * Before solving an ILP problem, we first check if simply
  * adding the normal of the constraint to one of the known
- * integer points in the basic set yields another point
- * inside the basic set.
+ * integer points in the basic set represented by "tab"
+ * yields another point inside the basic set.
  *
- * The caller of this function ensures that "bset" is bounded.
+ * The caller of this function ensures that the tableau is bounded or
+ * that tab->basis and tab->n_unbounded have been set appropriately.
  */
-static struct isl_basic_set *outside_point(struct isl_ctx *ctx,
-       struct isl_basic_set *bset, isl_int *eq, int up)
+static struct isl_vec *outside_point(struct isl_tab *tab, isl_int *eq, int up)
 {
-       struct isl_basic_set *slice = NULL;
-       struct isl_vec *sample;
-       struct isl_basic_set *point;
+       struct isl_ctx *ctx;
+       struct isl_vec *sample = NULL;
+       struct isl_tab_undo *snap;
        unsigned dim;
-       int k;
 
-       dim = isl_basic_set_n_dim(bset);
+       if (!tab)
+               return NULL;
+       ctx = tab->mat->ctx;
+
+       dim = tab->n_var;
        sample = isl_vec_alloc(ctx, 1 + dim);
        if (!sample)
                return NULL;
-       isl_int_set_si(sample->block.data[0], 1);
-       isl_seq_combine(sample->block.data + 1,
-               ctx->one, bset->sample->block.data + 1,
+       isl_int_set_si(sample->el[0], 1);
+       isl_seq_combine(sample->el + 1,
+               ctx->one, tab->bmap->sample->el + 1,
                up ? ctx->one : ctx->negone, eq + 1, dim);
-       if (isl_basic_set_contains(bset, sample))
-               return isl_basic_set_from_vec(sample);
+       if (isl_basic_map_contains(tab->bmap, sample))
+               return sample;
        isl_vec_free(sample);
        sample = NULL;
 
-       slice = isl_basic_set_copy(bset);
-       if (!slice)
+       snap = isl_tab_snap(tab);
+
+       if (!up)
+               isl_seq_neg(eq, eq, 1 + dim);
+       isl_int_sub_ui(eq[0], eq[0], 1);
+
+       if (isl_tab_extend_cons(tab, 1) < 0)
                goto error;
-       slice = isl_basic_set_cow(slice);
-       slice = isl_basic_set_extend(slice, 0, dim, 0, 0, 1);
-       k = isl_basic_set_alloc_inequality(slice);
-       if (k < 0)
+       if (isl_tab_add_ineq(tab, eq) < 0)
                goto error;
-       if (up)
-               isl_seq_cpy(slice->ineq[k], eq, 1 + dim);
-       else
-               isl_seq_neg(slice->ineq[k], eq, 1 + dim);
-       isl_int_sub_ui(slice->ineq[k][0], slice->ineq[k][0], 1);
 
-       sample = isl_basic_set_sample_bounded(slice);
-       if (!sample)
+       sample = isl_tab_sample(tab);
+
+       isl_int_add_ui(eq[0], eq[0], 1);
+       if (!up)
+               isl_seq_neg(eq, eq, 1 + dim);
+
+       if (sample && isl_tab_rollback(tab, snap) < 0)
                goto error;
-       if (sample->size == 0) {
-               isl_vec_free(sample);
-               point = isl_basic_set_empty_like(bset);
-       } else
-               point = isl_basic_set_from_vec(sample);
 
-       return point;
+       return sample;
 error:
-       isl_basic_set_free(slice);
+       isl_vec_free(sample);
        return NULL;
 }
 
@@ -342,91 +327,555 @@ error:
        return NULL;
 }
 
-/* Extend an initial (under-)approximation of the affine hull of "bset"
+__isl_give isl_set *isl_set_recession_cone(__isl_take isl_set *set)
+{
+       int i;
+
+       if (!set)
+               return NULL;
+       if (set->n == 0)
+               return set;
+
+       set = isl_set_remove_divs(set);
+       set = isl_set_cow(set);
+       if (!set)
+               return NULL;
+
+       for (i = 0; i < set->n; ++i) {
+               set->p[i] = isl_basic_set_recession_cone(set->p[i]);
+               if (!set->p[i])
+                       goto error;
+       }
+
+       return set;
+error:
+       isl_set_free(set);
+       return NULL;
+}
+
+/* Move "sample" to a point that is one up (or down) from the original
+ * point in dimension "pos".
+ */
+static void adjacent_point(__isl_keep isl_vec *sample, int pos, int up)
+{
+       if (up)
+               isl_int_add_ui(sample->el[1 + pos], sample->el[1 + pos], 1);
+       else
+               isl_int_sub_ui(sample->el[1 + pos], sample->el[1 + pos], 1);
+}
+
+/* Check if any points that are adjacent to "sample" also belong to "bset".
+ * If so, add them to "hull" and return the updated hull.
+ *
+ * Before checking whether and adjacent point belongs to "bset", we first
+ * check whether it already belongs to "hull" as this test is typically
+ * much cheaper.
+ */
+static __isl_give isl_basic_set *add_adjacent_points(
+       __isl_take isl_basic_set *hull, __isl_take isl_vec *sample,
+       __isl_keep isl_basic_set *bset)
+{
+       int i, up;
+       int dim;
+
+       if (!sample)
+               goto error;
+
+       dim = isl_basic_set_dim(hull, isl_dim_set);
+
+       for (i = 0; i < dim; ++i) {
+               for (up = 0; up <= 1; ++up) {
+                       int contains;
+                       isl_basic_set *point;
+
+                       adjacent_point(sample, i, up);
+                       contains = isl_basic_set_contains(hull, sample);
+                       if (contains < 0)
+                               goto error;
+                       if (contains) {
+                               adjacent_point(sample, i, !up);
+                               continue;
+                       }
+                       contains = isl_basic_set_contains(bset, sample);
+                       if (contains < 0)
+                               goto error;
+                       if (contains) {
+                               point = isl_basic_set_from_vec(
+                                                       isl_vec_copy(sample));
+                               hull = affine_hull(hull, point);
+                       }
+                       adjacent_point(sample, i, !up);
+                       if (contains)
+                               break;
+               }
+       }
+
+       isl_vec_free(sample);
+
+       return hull;
+error:
+       isl_vec_free(sample);
+       isl_basic_set_free(hull);
+       return NULL;
+}
+
+/* Extend an initial (under-)approximation of the affine hull of basic
+ * set represented by the tableau "tab"
  * by looking for points that do not satisfy one of the equalities
  * in the current approximation and adding them to that approximation
  * until no such points can be found any more.
  *
- * The caller of this function ensures that "bset" is bounded.
+ * The caller of this function ensures that "tab" is bounded or
+ * that tab->basis and tab->n_unbounded have been set appropriately.
+ *
+ * "bset" may be either NULL or the basic set represented by "tab".
+ * If "bset" is not NULL, we check for any point we find if any
+ * of its adjacent points also belong to "bset".
  */
-static struct isl_basic_set *extend_affine_hull(struct isl_basic_set *bset,
-       struct isl_basic_set *hull)
+static __isl_give isl_basic_set *extend_affine_hull(struct isl_tab *tab,
+       __isl_take isl_basic_set *hull, __isl_keep isl_basic_set *bset)
 {
-       int i, j, k;
-       struct isl_ctx *ctx;
+       int i, j;
        unsigned dim;
 
-       ctx = bset->ctx;
-       dim = isl_basic_set_n_dim(bset);
+       if (!tab || !hull)
+               goto error;
+
+       dim = tab->n_var;
+
+       if (isl_tab_extend_cons(tab, 2 * dim + 1) < 0)
+               goto error;
+
        for (i = 0; i < dim; ++i) {
+               struct isl_vec *sample;
                struct isl_basic_set *point;
                for (j = 0; j < hull->n_eq; ++j) {
-                       point = outside_point(ctx, bset, hull->eq[j], 1);
-                       if (!point)
+                       sample = outside_point(tab, hull->eq[j], 1);
+                       if (!sample)
                                goto error;
-                       if (!ISL_F_ISSET(point, ISL_BASIC_SET_EMPTY))
+                       if (sample->size > 0)
                                break;
-                       isl_basic_set_free(point);
-                       point = outside_point(ctx, bset, hull->eq[j], 0);
-                       if (!point)
+                       isl_vec_free(sample);
+                       sample = outside_point(tab, hull->eq[j], 0);
+                       if (!sample)
                                goto error;
-                       if (!ISL_F_ISSET(point, ISL_BASIC_SET_EMPTY))
+                       if (sample->size > 0)
                                break;
-                       isl_basic_set_free(point);
+                       isl_vec_free(sample);
 
-                       bset = isl_basic_set_extend_constraints(bset, 1, 0);
-                       k = isl_basic_set_alloc_equality(bset);
-                       if (k < 0)
-                               goto error;
-                       isl_seq_cpy(bset->eq[k], hull->eq[j],
-                                       1 + isl_basic_set_total_dim(hull));
-                       bset = isl_basic_set_gauss(bset, NULL);
-                       if (!bset)
+                       if (isl_tab_add_eq(tab, hull->eq[j]) < 0)
                                goto error;
                }
                if (j == hull->n_eq)
                        break;
+               if (tab->samples)
+                       tab = isl_tab_add_sample(tab, isl_vec_copy(sample));
+               if (!tab)
+                       goto error;
+               if (bset)
+                       hull = add_adjacent_points(hull, isl_vec_copy(sample),
+                                                   bset);
+               point = isl_basic_set_from_vec(sample);
                hull = affine_hull(hull, point);
+               if (!hull)
+                       return NULL;
        }
-       isl_basic_set_free(bset);
 
        return hull;
 error:
-       isl_basic_set_free(bset);
        isl_basic_set_free(hull);
        return NULL;
 }
 
+/* Drop all constraints in bmap that involve any of the dimensions
+ * first to first+n-1.
+ */
+static __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving(
+       __isl_take isl_basic_map *bmap, unsigned first, unsigned n)
+{
+       int i;
+
+       if (n == 0)
+               return bmap;
+
+       bmap = isl_basic_map_cow(bmap);
+
+       if (!bmap)
+               return NULL;
+
+       for (i = bmap->n_eq - 1; i >= 0; --i) {
+               if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) == -1)
+                       continue;
+               isl_basic_map_drop_equality(bmap, i);
+       }
+
+       for (i = bmap->n_ineq - 1; i >= 0; --i) {
+               if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) == -1)
+                       continue;
+               isl_basic_map_drop_inequality(bmap, i);
+       }
+
+       return bmap;
+}
+
 /* Drop all constraints in bset that involve any of the dimensions
  * first to first+n-1.
  */
-static struct isl_basic_set *drop_constraints_involving
-       (struct isl_basic_set *bset, unsigned first, unsigned n)
+__isl_give isl_basic_set *isl_basic_set_drop_constraints_involving(
+       __isl_take isl_basic_set *bset, unsigned first, unsigned n)
+{
+       return isl_basic_map_drop_constraints_involving(bset, first, n);
+}
+
+/* Drop all constraints in bmap that do not involve any of the dimensions
+ * first to first + n - 1 of the given type.
+ */
+__isl_give isl_basic_map *isl_basic_map_drop_constraints_not_involving_dims(
+       __isl_take isl_basic_map *bmap,
+       enum isl_dim_type type, unsigned first, unsigned n)
 {
        int i;
+       unsigned dim;
 
-       if (!bset)
+       if (n == 0)
+               return isl_basic_map_set_to_empty(bmap);
+       bmap = isl_basic_map_cow(bmap);
+       if (!bmap)
                return NULL;
 
-       bset = isl_basic_set_cow(bset);
+       dim = isl_basic_map_dim(bmap, type);
+       if (first + n > dim || first + n < first)
+               isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
+                       "index out of bounds", return isl_basic_map_free(bmap));
+
+       first += isl_basic_map_offset(bmap, type) - 1;
 
-       for (i = bset->n_eq - 1; i >= 0; --i) {
-               if (isl_seq_first_non_zero(bset->eq[i] + 1 + first, n) == -1)
+       for (i = bmap->n_eq - 1; i >= 0; --i) {
+               if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) != -1)
                        continue;
-               isl_basic_set_drop_equality(bset, i);
+               isl_basic_map_drop_equality(bmap, i);
        }
 
-       for (i = bset->n_ineq - 1; i >= 0; --i) {
-               if (isl_seq_first_non_zero(bset->ineq[i] + 1 + first, n) == -1)
+       for (i = bmap->n_ineq - 1; i >= 0; --i) {
+               if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) != -1)
                        continue;
-               isl_basic_set_drop_inequality(bset, i);
+               isl_basic_map_drop_inequality(bmap, i);
+       }
+
+       return bmap;
+}
+
+/* Drop all constraints in bset that do not involve any of the dimensions
+ * first to first + n - 1 of the given type.
+ */
+__isl_give isl_basic_set *isl_basic_set_drop_constraints_not_involving_dims(
+       __isl_take isl_basic_set *bset,
+       enum isl_dim_type type, unsigned first, unsigned n)
+{
+       return isl_basic_map_drop_constraints_not_involving_dims(bset,
+                                                           type, first, n);
+}
+
+/* Drop all constraints in bmap that involve any of the dimensions
+ * first to first + n - 1 of the given type.
+ */
+__isl_give isl_basic_map *isl_basic_map_drop_constraints_involving_dims(
+       __isl_take isl_basic_map *bmap,
+       enum isl_dim_type type, unsigned first, unsigned n)
+{
+       unsigned dim;
+
+       if (!bmap)
+               return NULL;
+       if (n == 0)
+               return bmap;
+
+       dim = isl_basic_map_dim(bmap, type);
+       if (first + n > dim || first + n < first)
+               isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
+                       "index out of bounds", return isl_basic_map_free(bmap));
+
+       bmap = isl_basic_map_remove_divs_involving_dims(bmap, type, first, n);
+       first += isl_basic_map_offset(bmap, type) - 1;
+       return isl_basic_map_drop_constraints_involving(bmap, first, n);
+}
+
+/* Drop all constraints in bset that involve any of the dimensions
+ * first to first + n - 1 of the given type.
+ */
+__isl_give isl_basic_set *isl_basic_set_drop_constraints_involving_dims(
+       __isl_take isl_basic_set *bset,
+       enum isl_dim_type type, unsigned first, unsigned n)
+{
+       return isl_basic_map_drop_constraints_involving_dims(bset,
+                                                           type, first, n);
+}
+
+/* Drop all constraints in map that involve any of the dimensions
+ * first to first + n - 1 of the given type.
+ */
+__isl_give isl_map *isl_map_drop_constraints_involving_dims(
+       __isl_take isl_map *map,
+       enum isl_dim_type type, unsigned first, unsigned n)
+{
+       int i;
+       unsigned dim;
+
+       if (!map)
+               return NULL;
+       if (n == 0)
+               return map;
+
+       dim = isl_map_dim(map, type);
+       if (first + n > dim || first + n < first)
+               isl_die(isl_map_get_ctx(map), isl_error_invalid,
+                       "index out of bounds", return isl_map_free(map));
+
+       map = isl_map_cow(map);
+       if (!map)
+               return NULL;
+
+       for (i = 0; i < map->n; ++i) {
+               map->p[i] = isl_basic_map_drop_constraints_involving_dims(
+                                                   map->p[i], type, first, n);
+               if (!map->p[i])
+                       return isl_map_free(map);
+       }
+
+       return map;
+}
+
+/* Drop all constraints in set that involve any of the dimensions
+ * first to first + n - 1 of the given type.
+ */
+__isl_give isl_set *isl_set_drop_constraints_involving_dims(
+       __isl_take isl_set *set,
+       enum isl_dim_type type, unsigned first, unsigned n)
+{
+       return isl_map_drop_constraints_involving_dims(set, type, first, n);
+}
+
+/* Construct an initial underapproximatino of the hull of "bset"
+ * from "sample" and any of its adjacent points that also belong to "bset".
+ */
+static __isl_give isl_basic_set *initialize_hull(__isl_keep isl_basic_set *bset,
+       __isl_take isl_vec *sample)
+{
+       isl_basic_set *hull;
+
+       hull = isl_basic_set_from_vec(isl_vec_copy(sample));
+       hull = add_adjacent_points(hull, sample, bset);
+
+       return hull;
+}
+
+/* Look for all equalities satisfied by the integer points in bset,
+ * which is assumed to be bounded.
+ *
+ * The equalities are obtained by successively looking for
+ * a point that is affinely independent of the points found so far.
+ * In particular, for each equality satisfied by the points so far,
+ * we check if there is any point on a hyperplane parallel to the
+ * corresponding hyperplane shifted by at least one (in either direction).
+ */
+static struct isl_basic_set *uset_affine_hull_bounded(struct isl_basic_set *bset)
+{
+       struct isl_vec *sample = NULL;
+       struct isl_basic_set *hull;
+       struct isl_tab *tab = NULL;
+       unsigned dim;
+
+       if (isl_basic_set_plain_is_empty(bset))
+               return bset;
+
+       dim = isl_basic_set_n_dim(bset);
+
+       if (bset->sample && bset->sample->size == 1 + dim) {
+               int contains = isl_basic_set_contains(bset, bset->sample);
+               if (contains < 0)
+                       goto error;
+               if (contains) {
+                       if (dim == 0)
+                               return bset;
+                       sample = isl_vec_copy(bset->sample);
+               } else {
+                       isl_vec_free(bset->sample);
+                       bset->sample = NULL;
+               }
        }
 
+       tab = isl_tab_from_basic_set(bset, 1);
+       if (!tab)
+               goto error;
+       if (tab->empty) {
+               isl_tab_free(tab);
+               isl_vec_free(sample);
+               return isl_basic_set_set_to_empty(bset);
+       }
+
+       if (!sample) {
+               struct isl_tab_undo *snap;
+               snap = isl_tab_snap(tab);
+               sample = isl_tab_sample(tab);
+               if (isl_tab_rollback(tab, snap) < 0)
+                       goto error;
+               isl_vec_free(tab->bmap->sample);
+               tab->bmap->sample = isl_vec_copy(sample);
+       }
+
+       if (!sample)
+               goto error;
+       if (sample->size == 0) {
+               isl_tab_free(tab);
+               isl_vec_free(sample);
+               return isl_basic_set_set_to_empty(bset);
+       }
+
+       hull = initialize_hull(bset, sample);
+
+       hull = extend_affine_hull(tab, hull, bset);
+       isl_basic_set_free(bset);
+       isl_tab_free(tab);
+
+       return hull;
+error:
+       isl_vec_free(sample);
+       isl_tab_free(tab);
+       isl_basic_set_free(bset);
+       return NULL;
+}
+
+/* Given an unbounded tableau and an integer point satisfying the tableau,
+ * construct an initial affine hull containing the recession cone
+ * shifted to the given point.
+ *
+ * The unbounded directions are taken from the last rows of the basis,
+ * which is assumed to have been initialized appropriately.
+ */
+static __isl_give isl_basic_set *initial_hull(struct isl_tab *tab,
+       __isl_take isl_vec *vec)
+{
+       int i;
+       int k;
+       struct isl_basic_set *bset = NULL;
+       struct isl_ctx *ctx;
+       unsigned dim;
+
+       if (!vec || !tab)
+               return NULL;
+       ctx = vec->ctx;
+       isl_assert(ctx, vec->size != 0, goto error);
+
+       bset = isl_basic_set_alloc(ctx, 0, vec->size - 1, 0, vec->size - 1, 0);
+       if (!bset)
+               goto error;
+       dim = isl_basic_set_n_dim(bset) - tab->n_unbounded;
+       for (i = 0; i < dim; ++i) {
+               k = isl_basic_set_alloc_equality(bset);
+               if (k < 0)
+                       goto error;
+               isl_seq_cpy(bset->eq[k] + 1, tab->basis->row[1 + i] + 1,
+                           vec->size - 1);
+               isl_seq_inner_product(bset->eq[k] + 1, vec->el +1,
+                                     vec->size - 1, &bset->eq[k][0]);
+               isl_int_neg(bset->eq[k][0], bset->eq[k][0]);
+       }
+       bset->sample = vec;
+       bset = isl_basic_set_gauss(bset, NULL);
+
        return bset;
+error:
+       isl_basic_set_free(bset);
+       isl_vec_free(vec);
+       return NULL;
+}
+
+/* Given a tableau of a set and a tableau of the corresponding
+ * recession cone, detect and add all equalities to the tableau.
+ * If the tableau is bounded, then we can simply keep the
+ * tableau in its state after the return from extend_affine_hull.
+ * However, if the tableau is unbounded, then
+ * isl_tab_set_initial_basis_with_cone will add some additional
+ * constraints to the tableau that have to be removed again.
+ * In this case, we therefore rollback to the state before
+ * any constraints were added and then add the equalities back in.
+ */
+struct isl_tab *isl_tab_detect_equalities(struct isl_tab *tab,
+       struct isl_tab *tab_cone)
+{
+       int j;
+       struct isl_vec *sample;
+       struct isl_basic_set *hull = NULL;
+       struct isl_tab_undo *snap;
+
+       if (!tab || !tab_cone)
+               goto error;
+
+       snap = isl_tab_snap(tab);
+
+       isl_mat_free(tab->basis);
+       tab->basis = NULL;
+
+       isl_assert(tab->mat->ctx, tab->bmap, goto error);
+       isl_assert(tab->mat->ctx, tab->samples, goto error);
+       isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, goto error);
+       isl_assert(tab->mat->ctx, tab->n_sample > tab->n_outside, goto error);
+
+       if (isl_tab_set_initial_basis_with_cone(tab, tab_cone) < 0)
+               goto error;
+
+       sample = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
+       if (!sample)
+               goto error;
+
+       isl_seq_cpy(sample->el, tab->samples->row[tab->n_outside], sample->size);
+
+       isl_vec_free(tab->bmap->sample);
+       tab->bmap->sample = isl_vec_copy(sample);
+
+       if (tab->n_unbounded == 0)
+               hull = isl_basic_set_from_vec(isl_vec_copy(sample));
+       else
+               hull = initial_hull(tab, isl_vec_copy(sample));
+
+       for (j = tab->n_outside + 1; j < tab->n_sample; ++j) {
+               isl_seq_cpy(sample->el, tab->samples->row[j], sample->size);
+               hull = affine_hull(hull,
+                               isl_basic_set_from_vec(isl_vec_copy(sample)));
+       }
+
+       isl_vec_free(sample);
+
+       hull = extend_affine_hull(tab, hull, NULL);
+       if (!hull)
+               goto error;
+
+       if (tab->n_unbounded == 0) {
+               isl_basic_set_free(hull);
+               return tab;
+       }
+
+       if (isl_tab_rollback(tab, snap) < 0)
+               goto error;
+
+       if (hull->n_eq > tab->n_zero) {
+               for (j = 0; j < hull->n_eq; ++j) {
+                       isl_seq_normalize(tab->mat->ctx, hull->eq[j], 1 + tab->n_var);
+                       if (isl_tab_add_eq(tab, hull->eq[j]) < 0)
+                               goto error;
+               }
+       }
+
+       isl_basic_set_free(hull);
+
+       return tab;
+error:
+       isl_basic_set_free(hull);
+       isl_tab_free(tab);
+       return NULL;
 }
 
-/* Compute the affine hull of "bset", where "hull" is an initial approximation
- * with only a single point of "bset" and "cone" is the recession cone
+/* Compute the affine hull of "bset", where "cone" is the recession cone
  * of "bset".
  *
  * We first compute a unimodular transformation that puts the unbounded
@@ -438,11 +887,10 @@ static struct isl_basic_set *drop_constraints_involving
  *            [ y_1 ]                  [ y_1 ]   [ Q_1 ]
  *     x = U  [ y_2 ]                  [ y_2 ] = [ Q_2 ] x
  *
- * Let's call the input basic set S and the initial hull H.
- * We compute S' = preimage(S, U) and H' = preimage(H, U)
+ * Let's call the input basic set S.  We compute S' = preimage(S, U)
  * and drop the final dimensions including any constraints involving them.
- * This results in sets S'' and H''.
- * Then we extend H'' to the affine hull A'' of S''.
+ * This results in set S''.
+ * Then we compute the affine hull A'' of S''.
  * Let F y_1 >= g be the constraint system of A''.  In the transformed
  * space the y_2 are unbounded, so we can add them back without any constraints,
  * resulting in
@@ -459,19 +907,20 @@ static struct isl_basic_set *drop_constraints_involving
  * A = preimage(A'', Q_1).
  */
 static struct isl_basic_set *affine_hull_with_cone(struct isl_basic_set *bset,
-       struct isl_basic_set *hull, struct isl_basic_set *cone)
+       struct isl_basic_set *cone)
 {
        unsigned total;
        unsigned cone_dim;
+       struct isl_basic_set *hull;
        struct isl_mat *M, *U, *Q;
 
-       if (!bset || !hull || !cone)
+       if (!bset || !cone)
                goto error;
 
        total = isl_basic_set_total_dim(cone);
        cone_dim = total - cone->n_eq;
 
-       M = isl_mat_sub_alloc(bset->ctx, cone->eq, 0, cone->n_eq, 1, total);
+       M = isl_mat_sub_alloc6(bset->ctx, cone->eq, 0, cone->n_eq, 1, total);
        M = isl_mat_left_hermite(M, 0, &U, &Q);
        if (!M)
                goto error;
@@ -479,29 +928,42 @@ static struct isl_basic_set *affine_hull_with_cone(struct isl_basic_set *bset,
 
        U = isl_mat_lin_to_aff(U);
        bset = isl_basic_set_preimage(bset, isl_mat_copy(U));
-       hull = isl_basic_set_preimage(hull, U);
 
-       bset = drop_constraints_involving(bset, total - cone_dim, cone_dim);
+       bset = isl_basic_set_drop_constraints_involving(bset, total - cone_dim,
+                                                       cone_dim);
        bset = isl_basic_set_drop_dims(bset, total - cone_dim, cone_dim);
-       hull = drop_constraints_involving(hull, total - cone_dim, cone_dim);
-       hull = isl_basic_set_drop_dims(hull, total - cone_dim, cone_dim);
 
        Q = isl_mat_lin_to_aff(Q);
        Q = isl_mat_drop_rows(Q, 1 + total - cone_dim, cone_dim);
 
-       if (bset && bset->sample)
+       if (bset && bset->sample && bset->sample->size == 1 + total)
                bset->sample = isl_mat_vec_product(isl_mat_copy(Q), bset->sample);
 
-       hull = extend_affine_hull(bset, hull);
+       hull = uset_affine_hull_bounded(bset);
 
-       hull = isl_basic_set_preimage(hull, Q);
+       if (!hull) {
+               isl_mat_free(Q);
+               isl_mat_free(U);
+       } else {
+               struct isl_vec *sample = isl_vec_copy(hull->sample);
+               U = isl_mat_drop_cols(U, 1 + total - cone_dim, cone_dim);
+               if (sample && sample->size > 0)
+                       sample = isl_mat_vec_product(U, sample);
+               else
+                       isl_mat_free(U);
+               hull = isl_basic_set_preimage(hull, Q);
+               if (hull) {
+                       isl_vec_free(hull->sample);
+                       hull->sample = sample;
+               } else
+                       isl_vec_free(sample);
+       }
 
        isl_basic_set_free(cone);
 
        return hull;
 error:
        isl_basic_set_free(bset);
-       isl_basic_set_free(hull);
        isl_basic_set_free(cone);
        return NULL;
 }
@@ -524,48 +986,29 @@ error:
  */
 static struct isl_basic_set *uset_affine_hull(struct isl_basic_set *bset)
 {
-       struct isl_basic_set *hull = NULL;
-       struct isl_vec *sample = NULL;
        struct isl_basic_set *cone;
 
-       if (isl_basic_set_is_empty(bset))
+       if (isl_basic_set_plain_is_empty(bset))
                return bset;
 
-       sample = isl_basic_set_sample(isl_basic_set_copy(bset));
-       if (!sample)
-               goto error;
-       if (sample->size == 0) {
-               isl_vec_free(sample);
-               hull = isl_basic_set_empty_like(bset);
-               isl_basic_set_free(bset);
-               return hull;
-       }
-       if (sample->size == 1) {
-               isl_vec_free(sample);
-               return bset;
-       }
-
        cone = isl_basic_set_recession_cone(isl_basic_set_copy(bset));
        if (!cone)
                goto error;
        if (cone->n_eq == 0) {
+               struct isl_basic_set *hull;
                isl_basic_set_free(cone);
-               isl_vec_free(sample);
                hull = isl_basic_set_universe_like(bset);
                isl_basic_set_free(bset);
                return hull;
        }
 
-       hull = isl_basic_set_from_vec(sample);
        if (cone->n_eq < isl_basic_set_total_dim(cone))
-               return affine_hull_with_cone(bset, hull, cone);
+               return affine_hull_with_cone(bset, cone);
 
        isl_basic_set_free(cone);
-       return extend_affine_hull(bset, hull);
+       return uset_affine_hull_bounded(bset);
 error:
-       isl_vec_free(sample);
        isl_basic_set_free(bset);
-       isl_basic_set_free(hull);
        return NULL;
 }
 
@@ -585,21 +1028,43 @@ error:
 static struct isl_basic_set *equalities_in_underlying_set(
                                                struct isl_basic_map *bmap)
 {
+       struct isl_mat *T1 = NULL;
        struct isl_mat *T2 = NULL;
        struct isl_basic_set *bset = NULL;
        struct isl_basic_set *hull = NULL;
 
        bset = isl_basic_map_underlying_set(bmap);
-       bset = isl_basic_set_remove_equalities(bset, NULL, &T2);
+       if (!bset)
+               return NULL;
+       if (bset->n_eq)
+               bset = isl_basic_set_remove_equalities(bset, &T1, &T2);
        if (!bset)
                goto error;
 
        hull = uset_affine_hull(bset);
-       if (T2)
+       if (!T2)
+               return hull;
+
+       if (!hull) {
+               isl_mat_free(T1);
+               isl_mat_free(T2);
+       } else {
+               struct isl_vec *sample = isl_vec_copy(hull->sample);
+               if (sample && sample->size > 0)
+                       sample = isl_mat_vec_product(T1, sample);
+               else
+                       isl_mat_free(T1);
                hull = isl_basic_set_preimage(hull, T2);
+               if (hull) {
+                       isl_vec_free(hull->sample);
+                       hull->sample = sample;
+               } else
+                       isl_vec_free(sample);
+       }
 
        return hull;
 error:
+       isl_mat_free(T1);
        isl_mat_free(T2);
        isl_basic_set_free(bset);
        isl_basic_set_free(hull);
@@ -633,7 +1098,7 @@ struct isl_basic_map *isl_basic_map_detect_equalities(
                isl_basic_set_free(hull);
                return isl_basic_map_set_to_empty(bmap);
        }
-       bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim), 0,
+       bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim), 0,
                                        hull->n_eq, 0);
        for (i = 0; i < hull->n_eq; ++i) {
                j = isl_basic_map_alloc_equality(bmap);
@@ -642,6 +1107,8 @@ struct isl_basic_map *isl_basic_map_detect_equalities(
                isl_seq_cpy(bmap->eq[j], hull->eq[i],
                                1 + isl_basic_set_total_dim(hull));
        }
+       isl_vec_free(bmap->sample);
+       bmap->sample = isl_vec_copy(hull->sample);
        isl_basic_set_free(hull);
        ISL_F_SET(bmap, ISL_BASIC_MAP_NO_IMPLICIT | ISL_BASIC_MAP_ALL_EQUALITIES);
        bmap = isl_basic_map_simplify(bmap);
@@ -652,7 +1119,15 @@ error:
        return NULL;
 }
 
-struct isl_map *isl_map_detect_equalities(struct isl_map *map)
+__isl_give isl_basic_set *isl_basic_set_detect_equalities(
+                                               __isl_take isl_basic_set *bset)
+{
+       return (isl_basic_set *)
+               isl_basic_map_detect_equalities((isl_basic_map *)bset);
+}
+
+__isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
+       __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
 {
        struct isl_basic_map *bmap;
        int i;
@@ -662,7 +1137,7 @@ struct isl_map *isl_map_detect_equalities(struct isl_map *map)
 
        for (i = 0; i < map->n; ++i) {
                bmap = isl_basic_map_copy(map->p[i]);
-               bmap = isl_basic_map_detect_equalities(bmap);
+               bmap = fn(bmap);
                if (!bmap)
                        goto error;
                isl_basic_map_free(map->p[i]);
@@ -675,17 +1150,28 @@ error:
        return NULL;
 }
 
+__isl_give isl_map *isl_map_detect_equalities(__isl_take isl_map *map)
+{
+       return isl_map_inline_foreach_basic_map(map,
+                                           &isl_basic_map_detect_equalities);
+}
+
+__isl_give isl_set *isl_set_detect_equalities(__isl_take isl_set *set)
+{
+       return (isl_set *)isl_map_detect_equalities((isl_map *)set);
+}
+
 /* After computing the rational affine hull (by detecting the implicit
  * equalities), we compute the additional equalities satisfied by
  * the integer points (if any) and add the original equalities back in.
  */
 struct isl_basic_map *isl_basic_map_affine_hull(struct isl_basic_map *bmap)
 {
-       struct isl_basic_set *hull = NULL;
-
        bmap = isl_basic_map_detect_equalities(bmap);
        bmap = isl_basic_map_cow(bmap);
-       isl_basic_map_free_inequality(bmap, bmap->n_ineq);
+       if (bmap)
+               isl_basic_map_free_inequality(bmap, bmap->n_ineq);
+       bmap = isl_basic_map_finalize(bmap);
        return bmap;
 }
 
@@ -702,6 +1188,9 @@ struct isl_basic_map *isl_map_affine_hull(struct isl_map *map)
        struct isl_basic_map *hull = NULL;
        struct isl_set *set;
 
+       map = isl_map_detect_equalities(map);
+       map = isl_map_align_divs(map);
+
        if (!map)
                return NULL;
 
@@ -711,10 +1200,6 @@ struct isl_basic_map *isl_map_affine_hull(struct isl_map *map)
                return hull;
        }
 
-       map = isl_map_detect_equalities(map);
-       map = isl_map_align_divs(map);
-       if (!map)
-               return NULL;
        model = isl_basic_map_copy(map->p[0]);
        set = isl_map_underlying_set(map);
        set = isl_set_cow(set);