isl_input.c: remove needless indirection
[platform/upstream/isl.git] / isl_tab_pip.c
index 7f7db31..e89c843 100644 (file)
@@ -1,3 +1,12 @@
+/*
+ * Copyright 2008-2009 Katholieke Universiteit Leuven
+ *
+ * Use of this software is governed by the GNU LGPLv2.1 license
+ *
+ * Written by Sven Verdoolaege, K.U.Leuven, Departement
+ * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
+ */
+
 #include "isl_map_private.h"
 #include "isl_seq.h"
 #include "isl_tab.h"
@@ -101,6 +110,20 @@ struct isl_context_lex {
        struct isl_tab *tab;
 };
 
+struct isl_partial_sol {
+       int level;
+       struct isl_basic_set *dom;
+       struct isl_mat *M;
+
+       struct isl_partial_sol *next;
+};
+
+struct isl_sol;
+struct isl_sol_callback {
+       struct isl_tab_callback callback;
+       struct isl_sol *sol;
+};
+
 /* isl_sol is an interface for constructing a solution to
  * a parametric integer linear programming problem.
  * Every time the algorithm reaches a state where a solution
@@ -118,57 +141,225 @@ struct isl_context_lex {
  * the solution.
  */
 struct isl_sol {
+       int error;
+       int rational;
+       int level;
+       int max;
+       int n_out;
        struct isl_context *context;
-       struct isl_sol *(*add)(struct isl_sol *sol, struct isl_tab *tab);
+       struct isl_partial_sol *partial;
+       void (*add)(struct isl_sol *sol,
+                           struct isl_basic_set *dom, struct isl_mat *M);
+       void (*add_empty)(struct isl_sol *sol, struct isl_basic_set *bset);
        void (*free)(struct isl_sol *sol);
+       struct isl_sol_callback dec_level;
 };
 
 static void sol_free(struct isl_sol *sol)
 {
+       struct isl_partial_sol *partial, *next;
        if (!sol)
                return;
+       for (partial = sol->partial; partial; partial = next) {
+               next = partial->next;
+               isl_basic_set_free(partial->dom);
+               isl_mat_free(partial->M);
+               free(partial);
+       }
        sol->free(sol);
 }
 
-struct isl_sol_map {
-       struct isl_sol  sol;
-       struct isl_map  *map;
-       struct isl_set  *empty;
-       int             max;
-};
-
-static void sol_map_free(struct isl_sol_map *sol_map)
+/* Push a partial solution represented by a domain and mapping M
+ * onto the stack of partial solutions.
+ */
+static void sol_push_sol(struct isl_sol *sol,
+       struct isl_basic_set *dom, struct isl_mat *M)
 {
-       if (sol_map->sol.context)
-               sol_map->sol.context->op->free(sol_map->sol.context);
-       isl_map_free(sol_map->map);
-       isl_set_free(sol_map->empty);
-       free(sol_map);
+       struct isl_partial_sol *partial;
+
+       if (sol->error || !dom)
+               goto error;
+
+       partial = isl_alloc_type(dom->ctx, struct isl_partial_sol);
+       if (!partial)
+               goto error;
+
+       partial->level = sol->level;
+       partial->dom = dom;
+       partial->M = M;
+       partial->next = sol->partial;
+
+       sol->partial = partial;
+
+       return;
+error:
+       isl_basic_set_free(dom);
+       sol->error = 1;
 }
 
-static void sol_map_free_wrap(struct isl_sol *sol)
+/* Pop one partial solution from the partial solution stack and
+ * pass it on to sol->add or sol->add_empty.
+ */
+static void sol_pop_one(struct isl_sol *sol)
 {
-       sol_map_free((struct isl_sol_map *)sol);
+       struct isl_partial_sol *partial;
+
+       partial = sol->partial;
+       sol->partial = partial->next;
+
+       if (partial->M)
+               sol->add(sol, partial->dom, partial->M);
+       else
+               sol->add_empty(sol, partial->dom);
+       free(partial);
 }
 
-static struct isl_sol_map *add_empty(struct isl_sol_map *sol)
+/* Return a fresh copy of the domain represented by the context tableau.
+ */
+static struct isl_basic_set *sol_domain(struct isl_sol *sol)
 {
        struct isl_basic_set *bset;
 
-       if (!sol->empty)
-               return sol;
-       sol->empty = isl_set_grow(sol->empty, 1);
-       bset = sol->sol.context->op->peek_basic_set(sol->sol.context);
-       bset = isl_basic_set_copy(bset);
-       bset = isl_basic_set_simplify(bset);
-       bset = isl_basic_set_finalize(bset);
-       sol->empty = isl_set_add(sol->empty, bset);
-       if (!sol->empty)
-               goto error;
-       return sol;
-error:
-       sol_map_free(sol);
-       return NULL;
+       if (sol->error)
+               return NULL;
+
+       bset = isl_basic_set_dup(sol->context->op->peek_basic_set(sol->context));
+       bset = isl_basic_set_update_from_tab(bset,
+                       sol->context->op->peek_tab(sol->context));
+
+       return bset;
+}
+
+/* Check whether two partial solutions have the same mapping, where n_div
+ * is the number of divs that the two partial solutions have in common.
+ */
+static int same_solution(struct isl_partial_sol *s1, struct isl_partial_sol *s2,
+       unsigned n_div)
+{
+       int i;
+       unsigned dim;
+
+       if (!s1->M != !s2->M)
+               return 0;
+       if (!s1->M)
+               return 1;
+
+       dim = isl_basic_set_total_dim(s1->dom) - s1->dom->n_div;
+
+       for (i = 0; i < s1->M->n_row; ++i) {
+               if (isl_seq_first_non_zero(s1->M->row[i]+1+dim+n_div,
+                                           s1->M->n_col-1-dim-n_div) != -1)
+                       return 0;
+               if (isl_seq_first_non_zero(s2->M->row[i]+1+dim+n_div,
+                                           s2->M->n_col-1-dim-n_div) != -1)
+                       return 0;
+               if (!isl_seq_eq(s1->M->row[i], s2->M->row[i], 1+dim+n_div))
+                       return 0;
+       }
+       return 1;
+}
+
+/* Pop all solutions from the partial solution stack that were pushed onto
+ * the stack at levels that are deeper than the current level.
+ * If the two topmost elements on the stack have the same level
+ * and represent the same solution, then their domains are combined.
+ * This combined domain is the same as the current context domain
+ * as sol_pop is called each time we move back to a higher level.
+ */
+static void sol_pop(struct isl_sol *sol)
+{
+       struct isl_partial_sol *partial;
+       unsigned n_div;
+
+       if (sol->error)
+               return;
+
+       if (sol->level == 0) {
+               for (partial = sol->partial; partial; partial = sol->partial)
+                       sol_pop_one(sol);
+               return;
+       }
+
+       partial = sol->partial;
+       if (!partial)
+               return;
+
+       if (partial->level <= sol->level)
+               return;
+
+       if (partial->next && partial->next->level == partial->level) {
+               n_div = isl_basic_set_dim(
+                               sol->context->op->peek_basic_set(sol->context),
+                               isl_dim_div);
+
+               if (!same_solution(partial, partial->next, n_div)) {
+                       sol_pop_one(sol);
+                       sol_pop_one(sol);
+               } else {
+                       struct isl_basic_set *bset;
+
+                       bset = sol_domain(sol);
+
+                       isl_basic_set_free(partial->next->dom);
+                       partial->next->dom = bset;
+                       partial->next->level = sol->level;
+
+                       sol->partial = partial->next;
+                       isl_basic_set_free(partial->dom);
+                       isl_mat_free(partial->M);
+                       free(partial);
+               }
+       } else
+               sol_pop_one(sol);
+}
+
+static void sol_dec_level(struct isl_sol *sol)
+{
+       if (sol->error)
+               return;
+
+       sol->level--;
+
+       sol_pop(sol);
+}
+
+static int sol_dec_level_wrap(struct isl_tab_callback *cb)
+{
+       struct isl_sol_callback *callback = (struct isl_sol_callback *)cb;
+
+       sol_dec_level(callback->sol);
+
+       return callback->sol->error ? -1 : 0;
+}
+
+/* Move down to next level and push callback onto context tableau
+ * to decrease the level again when it gets rolled back across
+ * the current state.  That is, dec_level will be called with
+ * the context tableau in the same state as it is when inc_level
+ * is called.
+ */
+static void sol_inc_level(struct isl_sol *sol)
+{
+       struct isl_tab *tab;
+
+       if (sol->error)
+               return;
+
+       sol->level++;
+       tab = sol->context->op->peek_tab(sol->context);
+       if (isl_tab_push_callback(tab, &sol->dec_level.callback) < 0)
+               sol->error = 1;
+}
+
+static void scale_rows(struct isl_mat *mat, isl_int m, int n_row)
+{
+       int i;
+
+       if (isl_int_is_one(m))
+               return;
+
+       for (i = 0; i < n_row; ++i)
+               isl_seq_scale(mat->row[i], mat->row[i], m, mat->n_col);
 }
 
 /* Add the solution identified by the tableau and the context tableau.
@@ -184,23 +375,23 @@ error:
  *                     dimensions in the input map
  *     tab->n_div is equal to the number of divs in the context
  *
- * If there is no solution, then the basic set corresponding to the
- * context tableau is added to the set "empty".
+ * If there is no solution, then call add_empty with a basic set
+ * that corresponds to the context tableau.  (If add_empty is NULL,
+ * then do nothing).
  *
- * Otherwise, a basic map is constructed with the same parameters
- * and divs as the context, the dimensions of the context as input
- * dimensions and a number of output dimensions that is equal to
- * the number of output dimensions in the input map.
+ * If there is a solution, then first construct a matrix that maps
+ * all dimensions of the context to the output variables, i.e.,
+ * the output dimensions in the input map.
  * The divs in the input map (if any) that do not correspond to any
  * div in the context do not appear in the solution.
  * The algorithm will make sure that they have an integer value,
  * but these values themselves are of no interest.
+ * We have to be careful not to drop or rearrange any divs in the
+ * context because that would change the meaning of the matrix.
  *
- * The constraints and divs of the context are simply copied
- * fron context_tab->bset.
  * To extract the value of the output variables, it should be noted
- * that we always use a big parameter M and so the variable stored
- * in the tableau is not an output variable x itself, but
+ * that we always use a big parameter M in the main tableau and so
+ * the variable stored in this tableau is not an output variable x itself, but
  *     x' = M + x (in case of minimization)
  * or
  *     x' = M - x (in case of maximization)
@@ -211,15 +402,180 @@ error:
  * are bounded, so this cannot occur.
  * Similarly, when x' appears in a row, then the coefficient of M in that
  * row is necessarily 1.
- * If the row represents
+ * If the row in the tableau represents
  *     d x' = c + d M + e(y)
- * then, in case of minimization, an equality
- *     c + e(y) - d x' = 0
- * is added, and in case of maximization,
- *     c + e(y) + d x' = 0
+ * then, in case of minimization, the corresponding row in the matrix
+ * will be
+ *     a c + a e(y)
+ * with a d = m, the (updated) common denominator of the matrix.
+ * In case of maximization, the row will be
+ *     -a c - a e(y)
  */
-static struct isl_sol_map *sol_map_add(struct isl_sol_map *sol,
-       struct isl_tab *tab)
+static void sol_add(struct isl_sol *sol, struct isl_tab *tab)
+{
+       struct isl_basic_set *bset = NULL;
+       struct isl_mat *mat = NULL;
+       unsigned off;
+       int row, i;
+       isl_int m;
+
+       if (sol->error || !tab)
+               goto error;
+
+       if (tab->empty && !sol->add_empty)
+               return;
+
+       bset = sol_domain(sol);
+
+       if (tab->empty) {
+               sol_push_sol(sol, bset, NULL);
+               return;
+       }
+
+       off = 2 + tab->M;
+
+       mat = isl_mat_alloc(tab->mat->ctx, 1 + sol->n_out,
+                                           1 + tab->n_param + tab->n_div);
+       if (!mat)
+               goto error;
+
+       isl_int_init(m);
+
+       isl_seq_clr(mat->row[0] + 1, mat->n_col - 1);
+       isl_int_set_si(mat->row[0][0], 1);
+       for (row = 0; row < sol->n_out; ++row) {
+               int i = tab->n_param + row;
+               int r, j;
+
+               isl_seq_clr(mat->row[1 + row], mat->n_col);
+               if (!tab->var[i].is_row) {
+                       /* no unbounded */
+                       isl_assert(mat->ctx, !tab->M, goto error2);
+                       continue;
+               }
+
+               r = tab->var[i].index;
+               /* no unbounded */
+               if (tab->M)
+                       isl_assert(mat->ctx, isl_int_eq(tab->mat->row[r][2],
+                                                       tab->mat->row[r][0]),
+                                   goto error2);
+               isl_int_gcd(m, mat->row[0][0], tab->mat->row[r][0]);
+               isl_int_divexact(m, tab->mat->row[r][0], m);
+               scale_rows(mat, m, 1 + row);
+               isl_int_divexact(m, mat->row[0][0], tab->mat->row[r][0]);
+               isl_int_mul(mat->row[1 + row][0], m, tab->mat->row[r][1]);
+               for (j = 0; j < tab->n_param; ++j) {
+                       int col;
+                       if (tab->var[j].is_row)
+                               continue;
+                       col = tab->var[j].index;
+                       isl_int_mul(mat->row[1 + row][1 + j], m,
+                                   tab->mat->row[r][off + col]);
+               }
+               for (j = 0; j < tab->n_div; ++j) {
+                       int col;
+                       if (tab->var[tab->n_var - tab->n_div+j].is_row)
+                               continue;
+                       col = tab->var[tab->n_var - tab->n_div+j].index;
+                       isl_int_mul(mat->row[1 + row][1 + tab->n_param + j], m,
+                                   tab->mat->row[r][off + col]);
+               }
+               if (sol->max)
+                       isl_seq_neg(mat->row[1 + row], mat->row[1 + row],
+                                   mat->n_col);
+       }
+
+       isl_int_clear(m);
+
+       sol_push_sol(sol, bset, mat);
+       return;
+error2:
+       isl_int_clear(m);
+error:
+       isl_basic_set_free(bset);
+       isl_mat_free(mat);
+       sol_free(sol);
+}
+
+struct isl_sol_map {
+       struct isl_sol  sol;
+       struct isl_map  *map;
+       struct isl_set  *empty;
+};
+
+static void sol_map_free(struct isl_sol_map *sol_map)
+{
+       if (sol_map->sol.context)
+               sol_map->sol.context->op->free(sol_map->sol.context);
+       isl_map_free(sol_map->map);
+       isl_set_free(sol_map->empty);
+       free(sol_map);
+}
+
+static void sol_map_free_wrap(struct isl_sol *sol)
+{
+       sol_map_free((struct isl_sol_map *)sol);
+}
+
+/* This function is called for parts of the context where there is
+ * no solution, with "bset" corresponding to the context tableau.
+ * Simply add the basic set to the set "empty".
+ */
+static void sol_map_add_empty(struct isl_sol_map *sol,
+       struct isl_basic_set *bset)
+{
+       if (!bset)
+               goto error;
+       isl_assert(bset->ctx, sol->empty, goto error);
+
+       sol->empty = isl_set_grow(sol->empty, 1);
+       bset = isl_basic_set_simplify(bset);
+       bset = isl_basic_set_finalize(bset);
+       sol->empty = isl_set_add_basic_set(sol->empty, isl_basic_set_copy(bset));
+       if (!sol->empty)
+               goto error;
+       isl_basic_set_free(bset);
+       return;
+error:
+       isl_basic_set_free(bset);
+       sol->sol.error = 1;
+}
+
+static void sol_map_add_empty_wrap(struct isl_sol *sol,
+       struct isl_basic_set *bset)
+{
+       sol_map_add_empty((struct isl_sol_map *)sol, bset);
+}
+
+/* Add bset to sol's empty, but only if we are actually collecting
+ * the empty set.
+ */
+static void sol_map_add_empty_if_needed(struct isl_sol_map *sol,
+       struct isl_basic_set *bset)
+{
+       if (sol->empty)
+               sol_map_add_empty(sol, bset);
+       else
+               isl_basic_set_free(bset);
+}
+
+/* Given a basic map "dom" that represents the context and an affine
+ * matrix "M" that maps the dimensions of the context to the
+ * output variables, construct a basic map with the same parameters
+ * and divs as the context, the dimensions of the context as input
+ * dimensions and a number of output dimensions that is equal to
+ * the number of output dimensions in the input map.
+ *
+ * The constraints and divs of the context are simply copied
+ * from "dom".  For each row
+ *     x = c + e(y)
+ * an equality
+ *     c + e(y) - d x = 0
+ * is added, with d the common denominator of M.
+ */
+static void sol_map_add(struct isl_sol_map *sol,
+       struct isl_basic_set *dom, struct isl_mat *M)
 {
        int i;
        struct isl_basic_map *bmap = NULL;
@@ -230,121 +586,79 @@ static struct isl_sol_map *sol_map_add(struct isl_sol_map *sol,
        unsigned total;
        unsigned n_div;
        unsigned n_out;
-       unsigned off;
 
-       if (!sol || !tab)
+       if (sol->sol.error || !dom || !M)
                goto error;
 
-       if (tab->empty)
-               return add_empty(sol);
-
-       context_bset = sol->sol.context->op->peek_basic_set(sol->sol.context);
-       off = 2 + tab->M;
-       n_out = isl_map_dim(sol->map, isl_dim_out);
-       n_eq = context_bset->n_eq + n_out;
-       n_ineq = context_bset->n_ineq;
-       nparam = tab->n_param;
+       n_out = sol->sol.n_out;
+       n_eq = dom->n_eq + n_out;
+       n_ineq = dom->n_ineq;
+       n_div = dom->n_div;
+       nparam = isl_basic_set_total_dim(dom) - n_div;
        total = isl_map_dim(sol->map, isl_dim_all);
        bmap = isl_basic_map_alloc_dim(isl_map_get_dim(sol->map),
-                                   tab->n_div, n_eq, 2 * tab->n_div + n_ineq);
+                                       n_div, n_eq, 2 * n_div + n_ineq);
        if (!bmap)
                goto error;
-       n_div = tab->n_div;
-       if (tab->rational)
+       if (sol->sol.rational)
                ISL_F_SET(bmap, ISL_BASIC_MAP_RATIONAL);
-       for (i = 0; i < context_bset->n_div; ++i) {
+       for (i = 0; i < dom->n_div; ++i) {
                int k = isl_basic_map_alloc_div(bmap);
                if (k < 0)
                        goto error;
-               isl_seq_cpy(bmap->div[k],
-                           context_bset->div[i], 1 + 1 + nparam);
+               isl_seq_cpy(bmap->div[k], dom->div[i], 1 + 1 + nparam);
                isl_seq_clr(bmap->div[k] + 1 + 1 + nparam, total - nparam);
                isl_seq_cpy(bmap->div[k] + 1 + 1 + total,
-                           context_bset->div[i] + 1 + 1 + nparam, i);
+                           dom->div[i] + 1 + 1 + nparam, i);
        }
-       for (i = 0; i < context_bset->n_eq; ++i) {
+       for (i = 0; i < dom->n_eq; ++i) {
                int k = isl_basic_map_alloc_equality(bmap);
                if (k < 0)
                        goto error;
-               isl_seq_cpy(bmap->eq[k], context_bset->eq[i], 1 + nparam);
+               isl_seq_cpy(bmap->eq[k], dom->eq[i], 1 + nparam);
                isl_seq_clr(bmap->eq[k] + 1 + nparam, total - nparam);
                isl_seq_cpy(bmap->eq[k] + 1 + total,
-                           context_bset->eq[i] + 1 + nparam, n_div);
+                           dom->eq[i] + 1 + nparam, n_div);
        }
-       for (i = 0; i < context_bset->n_ineq; ++i) {
+       for (i = 0; i < dom->n_ineq; ++i) {
                int k = isl_basic_map_alloc_inequality(bmap);
                if (k < 0)
                        goto error;
-               isl_seq_cpy(bmap->ineq[k],
-                       context_bset->ineq[i], 1 + nparam);
+               isl_seq_cpy(bmap->ineq[k], dom->ineq[i], 1 + nparam);
                isl_seq_clr(bmap->ineq[k] + 1 + nparam, total - nparam);
                isl_seq_cpy(bmap->ineq[k] + 1 + total,
-                       context_bset->ineq[i] + 1 + nparam, n_div);
+                       dom->ineq[i] + 1 + nparam, n_div);
        }
-       for (i = tab->n_param; i < total; ++i) {
+       for (i = 0; i < M->n_row - 1; ++i) {
                int k = isl_basic_map_alloc_equality(bmap);
                if (k < 0)
                        goto error;
-               isl_seq_clr(bmap->eq[k] + 1, isl_basic_map_total_dim(bmap));
-               if (!tab->var[i].is_row) {
-                       /* no unbounded */
-                       isl_assert(bmap->ctx, !tab->M, goto error);
-                       isl_int_set_si(bmap->eq[k][0], 0);
-                       if (sol->max)
-                               isl_int_set_si(bmap->eq[k][1 + i], 1);
-                       else
-                               isl_int_set_si(bmap->eq[k][1 + i], -1);
-               } else {
-                       int row, j;
-                       row = tab->var[i].index;
-                       /* no unbounded */
-                       if (tab->M)
-                               isl_assert(bmap->ctx,
-                                       isl_int_eq(tab->mat->row[row][2],
-                                                  tab->mat->row[row][0]),
-                                       goto error);
-                       isl_int_set(bmap->eq[k][0], tab->mat->row[row][1]);
-                       for (j = 0; j < tab->n_param; ++j) {
-                               int col;
-                               if (tab->var[j].is_row)
-                                       continue;
-                               col = tab->var[j].index;
-                               isl_int_set(bmap->eq[k][1 + j],
-                                           tab->mat->row[row][off + col]);
-                       }
-                       for (j = 0; j < tab->n_div; ++j) {
-                               int col;
-                               if (tab->var[tab->n_var - tab->n_div+j].is_row)
-                                       continue;
-                               col = tab->var[tab->n_var - tab->n_div+j].index;
-                               isl_int_set(bmap->eq[k][1 + total + j],
-                                           tab->mat->row[row][off + col]);
-                       }
-                       if (sol->max)
-                               isl_int_set(bmap->eq[k][1 + i],
-                                           tab->mat->row[row][0]);
-                       else
-                               isl_int_neg(bmap->eq[k][1 + i],
-                                           tab->mat->row[row][0]);
-               }
+               isl_seq_cpy(bmap->eq[k], M->row[1 + i], 1 + nparam);
+               isl_seq_clr(bmap->eq[k] + 1 + nparam, n_out);
+               isl_int_neg(bmap->eq[k][1 + nparam + i], M->row[0][0]);
+               isl_seq_cpy(bmap->eq[k] + 1 + nparam + n_out,
+                           M->row[1 + i] + 1 + nparam, n_div);
        }
        bmap = isl_basic_map_simplify(bmap);
        bmap = isl_basic_map_finalize(bmap);
        sol->map = isl_map_grow(sol->map, 1);
-       sol->map = isl_map_add(sol->map, bmap);
+       sol->map = isl_map_add_basic_map(sol->map, bmap);
        if (!sol->map)
                goto error;
-       return sol;
+       isl_basic_set_free(dom);
+       isl_mat_free(M);
+       return;
 error:
+       isl_basic_set_free(dom);
+       isl_mat_free(M);
        isl_basic_map_free(bmap);
-       sol_free(&sol->sol);
-       return NULL;
+       sol->sol.error = 1;
 }
 
-static struct isl_sol *sol_map_add_wrap(struct isl_sol *sol,
-       struct isl_tab *tab)
+static void sol_map_add_wrap(struct isl_sol *sol,
+       struct isl_basic_set *dom, struct isl_mat *M)
 {
-       return (struct isl_sol *)sol_map_add((struct isl_sol_map *)sol, tab);
+       sol_map_add((struct isl_sol_map *)sol, dom, M);
 }
 
 
@@ -796,6 +1110,7 @@ static int first_neg(struct isl_tab *tab)
  * smallest increment in the sample point.  If there is no such column
  * then the tableau is infeasible.
  */
+static struct isl_tab *restore_lexmin(struct isl_tab *tab) WARN_UNUSED;
 static struct isl_tab *restore_lexmin(struct isl_tab *tab)
 {
        int row, col;
@@ -806,11 +1121,15 @@ static struct isl_tab *restore_lexmin(struct isl_tab *tab)
                return tab;
        while ((row = first_neg(tab)) != -1) {
                col = lexmin_pivot_col(tab, row);
-               if (col >= tab->n_col)
-                       return isl_tab_mark_empty(tab);
+               if (col >= tab->n_col) {
+                       if (isl_tab_mark_empty(tab) < 0)
+                               goto error;
+                       return tab;
+               }
                if (col < 0)
                        goto error;
-               isl_tab_pivot(tab, row, col);
+               if (isl_tab_pivot(tab, row, col) < 0)
+                       goto error;
        }
        return tab;
 error:
@@ -884,16 +1203,20 @@ static struct isl_tab *add_lexmin_valid_eq(struct isl_tab *tab, isl_int *eq)
        i = last_var_col_or_int_par_col(tab, r);
        if (i < 0) {
                tab->con[r].is_nonneg = 1;
-               isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]);
+               if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
+                       goto error;
                isl_seq_neg(eq, eq, 1 + tab->n_var);
                r = isl_tab_add_row(tab, eq);
                if (r < 0)
                        goto error;
                tab->con[r].is_nonneg = 1;
-               isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]);
+               if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
+                       goto error;
        } else {
-               isl_tab_pivot(tab, r, i);
-               isl_tab_kill_col(tab, i);
+               if (isl_tab_pivot(tab, r, i) < 0)
+                       goto error;
+               if (isl_tab_kill_col(tab, i) < 0)
+                       goto error;
                tab->n_eq++;
 
                tab = restore_lexmin(tab);
@@ -924,6 +1247,7 @@ static int is_constant(struct isl_tab *tab, int row)
  * In the end we try to use one of the two constraints to eliminate
  * a column.
  */
+static struct isl_tab *add_lexmin_eq(struct isl_tab *tab, isl_int *eq) WARN_UNUSED;
 static struct isl_tab *add_lexmin_eq(struct isl_tab *tab, isl_int *eq)
 {
        int r1, r2;
@@ -937,13 +1261,17 @@ static struct isl_tab *add_lexmin_eq(struct isl_tab *tab, isl_int *eq)
        if (r1 < 0)
                goto error;
        tab->con[r1].is_nonneg = 1;
-       isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r1]);
+       if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r1]) < 0)
+               goto error;
 
        row = tab->con[r1].index;
        if (is_constant(tab, row)) {
                if (!isl_int_is_zero(tab->mat->row[row][1]) ||
-                   (tab->M && !isl_int_is_zero(tab->mat->row[row][2])))
-                       return isl_tab_mark_empty(tab);
+                   (tab->M && !isl_int_is_zero(tab->mat->row[row][2]))) {
+                       if (isl_tab_mark_empty(tab) < 0)
+                               goto error;
+                       return tab;
+               }
                if (isl_tab_rollback(tab, snap) < 0)
                        goto error;
                return tab;
@@ -959,36 +1287,43 @@ static struct isl_tab *add_lexmin_eq(struct isl_tab *tab, isl_int *eq)
        if (r2 < 0)
                goto error;
        tab->con[r2].is_nonneg = 1;
-       isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r2]);
+       if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r2]) < 0)
+               goto error;
 
        tab = restore_lexmin(tab);
        if (!tab || tab->empty)
                return tab;
 
-       if (!tab->con[r1].is_row)
-               isl_tab_kill_col(tab, tab->con[r1].index);
-       else if (!tab->con[r2].is_row)
-               isl_tab_kill_col(tab, tab->con[r2].index);
-       else if (isl_int_is_zero(tab->mat->row[tab->con[r1].index][1])) {
+       if (!tab->con[r1].is_row) {
+               if (isl_tab_kill_col(tab, tab->con[r1].index) < 0)
+                       goto error;
+       } else if (!tab->con[r2].is_row) {
+               if (isl_tab_kill_col(tab, tab->con[r2].index) < 0)
+                       goto error;
+       } else if (isl_int_is_zero(tab->mat->row[tab->con[r1].index][1])) {
                unsigned off = 2 + tab->M;
                int i;
                int row = tab->con[r1].index;
                i = isl_seq_first_non_zero(tab->mat->row[row]+off+tab->n_dead,
                                                tab->n_col - tab->n_dead);
                if (i != -1) {
-                       isl_tab_pivot(tab, row, tab->n_dead + i);
-                       isl_tab_kill_col(tab, tab->n_dead + i);
+                       if (isl_tab_pivot(tab, row, tab->n_dead + i) < 0)
+                               goto error;
+                       if (isl_tab_kill_col(tab, tab->n_dead + i) < 0)
+                               goto error;
                }
        }
 
-       if (tab->bset) {
-               tab->bset = isl_basic_set_add_ineq(tab->bset, eq);
-               isl_tab_push(tab, isl_tab_undo_bset_ineq);
+       if (tab->bmap) {
+               tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
+               if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
+                       goto error;
                isl_seq_neg(eq, eq, 1 + tab->n_var);
-               tab->bset = isl_basic_set_add_ineq(tab->bset, eq);
+               tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
                isl_seq_neg(eq, eq, 1 + tab->n_var);
-               isl_tab_push(tab, isl_tab_undo_bset_ineq);
-               if (!tab->bset)
+               if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
+                       goto error;
+               if (!tab->bmap)
                        goto error;
        }
 
@@ -1007,26 +1342,30 @@ static struct isl_tab *add_lexmin_ineq(struct isl_tab *tab, isl_int *ineq)
 
        if (!tab)
                return NULL;
-       if (tab->bset) {
-               tab->bset = isl_basic_set_add_ineq(tab->bset, ineq);
-               isl_tab_push(tab, isl_tab_undo_bset_ineq);
-               if (!tab->bset)
+       if (tab->bmap) {
+               tab->bmap = isl_basic_map_add_ineq(tab->bmap, ineq);
+               if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
+                       goto error;
+               if (!tab->bmap)
                        goto error;
        }
        r = isl_tab_add_row(tab, ineq);
        if (r < 0)
                goto error;
        tab->con[r].is_nonneg = 1;
-       isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]);
+       if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
+               goto error;
        if (isl_tab_row_is_redundant(tab, tab->con[r].index)) {
-               isl_tab_mark_redundant(tab, tab->con[r].index);
+               if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
+                       goto error;
                return tab;
        }
 
        tab = restore_lexmin(tab);
        if (tab && !tab->empty && tab->con[r].is_row &&
                 isl_tab_row_is_redundant(tab, tab->con[r].index))
-               isl_tab_mark_redundant(tab, tab->con[r].index);
+               if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
+                       goto error;
        return tab;
 error:
        isl_tab_free(tab);
@@ -1068,7 +1407,7 @@ static int integer_variable(struct isl_tab *tab, int row)
        int i;
        unsigned off = 2 + tab->M;
 
-       for (i = 0; i < tab->n_col; ++i) {
+       for (i = tab->n_dead; i < tab->n_col; ++i) {
                if (tab->col_var[i] >= 0 &&
                    (tab->col_var[i] < tab->n_param ||
                     tab->col_var[i] >= tab->n_var - tab->n_div))
@@ -1092,8 +1431,9 @@ static int integer_constant(struct isl_tab *tab, int row)
 #define I_PAR  1 << 1
 #define I_VAR  1 << 2
 
-/* Check for first (non-parameter) variable that is non-integer and
- * therefore requires a cut.
+/* Check for next (non-parameter) variable after "var" (first if var == -1)
+ * that is non-integer and therefore requires a cut and return
+ * the index of the variable.
  * For parametric tableaus, there are three parts in a row,
  * the constant, the coefficients of the parameters and the rest.
  * For each part, we check whether the coefficients in that part
@@ -1102,16 +1442,16 @@ static int integer_constant(struct isl_tab *tab, int row)
  * current sample value is integral and no cut is required
  * (irrespective of whether the variable part is integral).
  */
-static int first_non_integer(struct isl_tab *tab, int *f)
+static int next_non_integer_var(struct isl_tab *tab, int var, int *f)
 {
-       int i;
+       var = var < 0 ? tab->n_param : var + 1;
 
-       for (i = tab->n_param; i < tab->n_var - tab->n_div; ++i) {
+       for (; var < tab->n_var - tab->n_div; ++var) {
                int flags = 0;
                int row;
-               if (!tab->var[i].is_row)
+               if (!tab->var[var].is_row)
                        continue;
-               row = tab->var[i].index;
+               row = tab->var[var].index;
                if (integer_constant(tab, row))
                        ISL_FL_SET(flags, I_CST);
                if (integer_parameter(tab, row))
@@ -1121,11 +1461,28 @@ static int first_non_integer(struct isl_tab *tab, int *f)
                if (integer_variable(tab, row))
                        ISL_FL_SET(flags, I_VAR);
                *f = flags;
-               return row;
+               return var;
        }
        return -1;
 }
 
+/* Check for first (non-parameter) variable that is non-integer and
+ * therefore requires a cut and return the corresponding row.
+ * For parametric tableaus, there are three parts in a row,
+ * the constant, the coefficients of the parameters and the rest.
+ * For each part, we check whether the coefficients in that part
+ * are all integral and if so, set the corresponding flag in *f.
+ * If the constant and the parameter part are integral, then the
+ * current sample value is integral and no cut is required
+ * (irrespective of whether the variable part is integral).
+ */
+static int first_non_integer_row(struct isl_tab *tab, int *f)
+{
+       int var = next_non_integer_var(tab, -1, f);
+
+       return var < 0 ? -1 : tab->var[var].index;
+}
+
 /* Add a (non-parametric) cut to cut away the non-integral sample
  * value of the given row.
  *
@@ -1173,7 +1530,8 @@ static int add_cut(struct isl_tab *tab, int row)
                        tab->mat->row[row][off + i], tab->mat->row[row][0]);
 
        tab->con[r].is_nonneg = 1;
-       isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]);
+       if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
+               return -1;
        if (tab->row_sign)
                tab->row_sign[tab->con[r].index] = isl_tab_row_neg;
 
@@ -1184,15 +1542,17 @@ static int add_cut(struct isl_tab *tab, int row)
  * sample point is obtained or until the tableau is determined
  * to be integer infeasible.
  * As long as there is any non-integer value in the sample point,
- * we add an appropriate cut, if possible and resolve the violated
- * cut constraint using restore_lexmin.
+ * we add appropriate cuts, if possible, for each of these
+ * non-integer values and then resolve the violated
+ * cut constraints using restore_lexmin.
  * If one of the corresponding rows is equal to an integral
  * combination of variables/constraints plus a non-integral constant,
- * then there is no way to obtain an integer point an we return
+ * then there is no way to obtain an integer point and we return
  * a tableau that is marked empty.
  */
 static struct isl_tab *cut_to_integer_lexmin(struct isl_tab *tab)
 {
+       int var;
        int row;
        int flags;
 
@@ -1201,12 +1561,18 @@ static struct isl_tab *cut_to_integer_lexmin(struct isl_tab *tab)
        if (tab->empty)
                return tab;
 
-       while ((row = first_non_integer(tab, &flags)) != -1) {
-               if (ISL_FL_ISSET(flags, I_VAR))
-                       return isl_tab_mark_empty(tab);
-               row = add_cut(tab, row);
-               if (row < 0)
-                       goto error;
+       while ((var = next_non_integer_var(tab, -1, &flags)) != -1) {
+               do {
+                       if (ISL_FL_ISSET(flags, I_VAR)) {
+                               if (isl_tab_mark_empty(tab) < 0)
+                                       goto error;
+                               return tab;
+                       }
+                       row = tab->var[var].index;
+                       row = add_cut(tab, row);
+                       if (row < 0)
+                               goto error;
+               } while ((var = next_non_integer_var(tab, var, &flags)) != -1);
                tab = restore_lexmin(tab);
                if (!tab || tab->empty)
                        break;
@@ -1229,7 +1595,7 @@ static struct isl_tab *check_samples(struct isl_tab *tab, isl_int *ineq, int eq)
        if (!tab)
                return NULL;
 
-       isl_assert(tab->mat->ctx, tab->bset, goto error);
+       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);
 
@@ -1293,7 +1659,8 @@ static struct isl_tab *check_integer_feasible(struct isl_tab *tab)
                return NULL;
 
        snap = isl_tab_snap(tab);
-       isl_tab_push_basis(tab);
+       if (isl_tab_push_basis(tab) < 0)
+               goto error;
 
        tab = cut_to_integer_lexmin(tab);
        if (!tab)
@@ -1327,7 +1694,7 @@ static int tab_has_valid_sample(struct isl_tab *tab, isl_int *ineq, int eq)
        if (!tab)
                return -1;
 
-       isl_assert(tab->mat->ctx, tab->bset, return -1);
+       isl_assert(tab->mat->ctx, tab->bmap, return -1);
        isl_assert(tab->mat->ctx, tab->samples, return -1);
        isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, return -1);
 
@@ -1431,13 +1798,14 @@ static int context_tab_add_div(struct isl_tab *tab, struct isl_vec *div,
                               samples->row[i][samples->n_col - 1], div->el[0]);
        }
 
-       tab->bset = isl_basic_set_extend_dim(tab->bset,
-               isl_basic_set_get_dim(tab->bset), 1, 0, 2);
-       k = isl_basic_set_alloc_div(tab->bset);
+       tab->bmap = isl_basic_map_extend_dim(tab->bmap,
+               isl_basic_map_get_dim(tab->bmap), 1, 0, 2);
+       k = isl_basic_map_alloc_div(tab->bmap);
        if (k < 0)
                return -1;
-       isl_seq_cpy(tab->bset->div[k], div->el, div->size);
-       isl_tab_push(tab, isl_tab_undo_bset_div);
+       isl_seq_cpy(tab->bmap->div[k], div->el, div->size);
+       if (isl_tab_push(tab, isl_tab_undo_bmap_div) < 0)
+               return -1;
 
        return k;
 }
@@ -1482,12 +1850,12 @@ error:
 static int find_div(struct isl_tab *tab, isl_int *div, isl_int denom)
 {
        int i;
-       unsigned total = isl_basic_set_total_dim(tab->bset);
+       unsigned total = isl_basic_map_total_dim(tab->bmap);
 
-       for (i = 0; i < tab->bset->n_div; ++i) {
-               if (isl_int_ne(tab->bset->div[i][0], denom))
+       for (i = 0; i < tab->bmap->n_div; ++i) {
+               if (isl_int_ne(tab->bmap->div[i][0], denom))
                        continue;
-               if (!isl_seq_eq(tab->bset->div[i] + 1, div, total))
+               if (!isl_seq_eq(tab->bmap->div[i] + 1, div, total))
                        continue;
                return i;
        }
@@ -1617,7 +1985,8 @@ static int add_parametric_cut(struct isl_tab *tab, int row,
        }
 
        tab->con[r].is_nonneg = 1;
-       isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]);
+       if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
+               return -1;
        if (tab->row_sign)
                tab->row_sign[tab->con[r].index] = isl_tab_row_neg;
 
@@ -1663,8 +2032,11 @@ static struct isl_tab *tab_for_lexmin(struct isl_basic_map *bmap,
                if (!tab->row_sign)
                        goto error;
        }
-       if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
-               return isl_tab_mark_empty(tab);
+       if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) {
+               if (isl_tab_mark_empty(tab) < 0)
+                       goto error;
+               return tab;
+       }
 
        for (i = tab->n_param; i < tab->n_var - tab->n_div; ++i) {
                tab->var[i].is_nonneg = 1;
@@ -1735,6 +2107,7 @@ static int best_split(struct isl_tab *tab, struct isl_tab *context_tab)
                struct isl_tab_undo *snap2;
                struct isl_vec *ineq = NULL;
                int r = 0;
+               int ok;
 
                if (!isl_tab_var_from_row(tab, split)->is_nonneg)
                        continue;
@@ -1744,8 +2117,10 @@ static int best_split(struct isl_tab *tab, struct isl_tab *context_tab)
                ineq = get_row_parameter_ineq(tab, split);
                if (!ineq)
                        return -1;
-               context_tab = isl_tab_add_ineq(context_tab, ineq->el);
+               ok = isl_tab_add_ineq(context_tab, ineq->el) >= 0;
                isl_vec_free(ineq);
+               if (!ok)
+                       return -1;
 
                snap2 = isl_tab_snap(context_tab);
 
@@ -1762,8 +2137,10 @@ static int best_split(struct isl_tab *tab, struct isl_tab *context_tab)
                        ineq = get_row_parameter_ineq(tab, row);
                        if (!ineq)
                                return -1;
-                       context_tab = isl_tab_add_ineq(context_tab, ineq->el);
+                       ok = isl_tab_add_ineq(context_tab, ineq->el) >= 0;
                        isl_vec_free(ineq);
+                       if (!ok)
+                               return -1;
                        var = &context_tab->con[context_tab->n_con - 1];
                        if (!context_tab->empty &&
                            !isl_tab_min_at_most_neg_one(context_tab, var))
@@ -1788,7 +2165,7 @@ static struct isl_basic_set *context_lex_peek_basic_set(
        struct isl_context_lex *clex = (struct isl_context_lex *)context;
        if (!clex->tab)
                return NULL;
-       return clex->tab->bset;
+       return isl_tab_peek_bset(clex->tab);
 }
 
 static struct isl_tab *context_lex_peek_tab(struct isl_context *context)
@@ -1914,7 +2291,8 @@ static int context_lex_test_ineq(struct isl_context *context, isl_int *ineq)
                return -1;
 
        snap = isl_tab_snap(clex->tab);
-       isl_tab_push_basis(clex->tab);
+       if (isl_tab_push_basis(clex->tab) < 0)
+               return -1;
        clex->tab = add_lexmin_ineq(clex->tab, ineq);
        clex->tab = check_integer_feasible(clex->tab);
        if (!clex->tab)
@@ -1953,7 +2331,8 @@ static int context_lex_best_split(struct isl_context *context,
        int r;
 
        snap = isl_tab_snap(clex->tab);
-       isl_tab_push_basis(clex->tab);
+       if (isl_tab_push_basis(clex->tab) < 0)
+               return -1;
        r = best_split(tab, clex->tab);
 
        if (isl_tab_rollback(clex->tab, snap) < 0)
@@ -1976,8 +2355,10 @@ static void *context_lex_save(struct isl_context *context)
        struct isl_tab_undo *snap;
 
        snap = isl_tab_snap(clex->tab);
-       isl_tab_push_basis(clex->tab);
-       isl_tab_save_samples(clex->tab);
+       if (isl_tab_push_basis(clex->tab) < 0)
+               return NULL;
+       if (isl_tab_save_samples(clex->tab) < 0)
+               return NULL;
 
        return snap;
 }
@@ -2027,7 +2408,8 @@ static struct isl_tab *tab_detect_nonnegative_parameters(struct isl_tab *tab,
        isl_seq_clr(ineq->el, ineq->size);
        for (i = 0; i < context_tab->n_var; ++i) {
                isl_int_set_si(ineq->el[1 + i], 1);
-               context_tab = isl_tab_add_ineq(context_tab, ineq->el);
+               if (isl_tab_add_ineq(context_tab, ineq->el) < 0)
+                       goto error;
                var = &context_tab->con[context_tab->n_con - 1];
                if (!context_tab->empty &&
                    !isl_tab_min_at_most_neg_one(context_tab, var)) {
@@ -2062,7 +2444,8 @@ static struct isl_tab *context_lex_detect_nonnegative_parameters(
        struct isl_tab_undo *snap;
 
        snap = isl_tab_snap(clex->tab);
-       isl_tab_push_basis(clex->tab);
+       if (isl_tab_push_basis(clex->tab) < 0)
+               goto error;
 
        tab = tab_detect_nonnegative_parameters(tab, clex->tab);
 
@@ -2119,7 +2502,8 @@ static struct isl_tab *context_tab_for_lexmin(struct isl_basic_set *bset)
        tab = tab_for_lexmin((struct isl_basic_map *)bset, NULL, 1, 0);
        if (!tab)
                goto error;
-       tab->bset = bset;
+       if (isl_tab_track_bset(tab, bset) < 0)
+               goto error;
        tab = isl_tab_init_samples(tab);
        return tab;
 error:
@@ -2172,7 +2556,7 @@ static struct isl_basic_set *context_gbr_peek_basic_set(
        struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
        if (!cgbr->tab)
                return NULL;
-       return cgbr->tab->bset;
+       return isl_tab_peek_bset(cgbr->tab);
 }
 
 static struct isl_tab *context_gbr_peek_tab(struct isl_context *context)
@@ -2191,7 +2575,7 @@ static void gbr_init_shifted(struct isl_context_gbr *cgbr)
 {
        int i, j;
        struct isl_vec *cst;
-       struct isl_basic_set *bset = cgbr->tab->bset;
+       struct isl_basic_set *bset = isl_tab_peek_bset(cgbr->tab);
        unsigned dim = isl_basic_set_total_dim(bset);
 
        cst = isl_vec_alloc(cgbr->tab->mat->ctx, bset->n_ineq);
@@ -2255,7 +2639,7 @@ static struct isl_basic_set *drop_constant_terms(struct isl_basic_set *bset)
 
 static int use_shifted(struct isl_context_gbr *cgbr)
 {
-       return cgbr->tab->bset->n_eq == 0 && cgbr->tab->bset->n_div == 0;
+       return cgbr->tab->bmap->n_eq == 0 && cgbr->tab->bmap->n_div == 0;
 }
 
 static struct isl_vec *gbr_get_sample(struct isl_context_gbr *cgbr)
@@ -2277,10 +2661,12 @@ static struct isl_vec *gbr_get_sample(struct isl_context_gbr *cgbr)
        }
 
        if (!cgbr->cone) {
-               cgbr->cone = isl_tab_from_recession_cone(cgbr->tab->bset);
+               bset = isl_tab_peek_bset(cgbr->tab);
+               cgbr->cone = isl_tab_from_recession_cone(bset);
                if (!cgbr->cone)
                        return NULL;
-               cgbr->cone->bset = isl_basic_set_dup(cgbr->tab->bset);
+               if (isl_tab_track_bset(cgbr->cone, isl_basic_set_dup(bset)) < 0)
+                       return NULL;
        }
        cgbr->cone = isl_tab_detect_implicit_equalities(cgbr->cone);
        if (!cgbr->cone)
@@ -2312,13 +2698,13 @@ static struct isl_vec *gbr_get_sample(struct isl_context_gbr *cgbr)
                return sample;
        }
 
-       cone = isl_basic_set_dup(cgbr->cone->bset);
+       cone = isl_basic_set_dup(isl_tab_peek_bset(cgbr->cone));
        cone = drop_constant_terms(cone);
        cone = isl_basic_set_update_from_tab(cone, cgbr->cone);
        cone = isl_basic_set_underlying_set(cone);
        cone = isl_basic_set_gauss(cone, NULL);
 
-       bset = isl_basic_set_dup(cgbr->tab->bset);
+       bset = isl_basic_set_dup(isl_tab_peek_bset(cgbr->tab));
        bset = isl_basic_set_update_from_tab(bset, cgbr->tab);
        bset = isl_basic_set_underlying_set(bset);
        bset = isl_basic_set_gauss(bset, NULL);
@@ -2342,7 +2728,8 @@ static void check_gbr_integer_feasible(struct isl_context_gbr *cgbr)
 
        if (sample->size == 0) {
                isl_vec_free(sample);
-               cgbr->tab = isl_tab_mark_empty(cgbr->tab);
+               if (isl_tab_mark_empty(cgbr->tab) < 0)
+                       goto error;
                return;
        }
 
@@ -2408,12 +2795,13 @@ static void add_gbr_ineq(struct isl_context_gbr *cgbr, isl_int *ineq)
        if (isl_tab_extend_cons(cgbr->tab, 1) < 0)
                goto error;
 
-       cgbr->tab = isl_tab_add_ineq(cgbr->tab, ineq);
+       if (isl_tab_add_ineq(cgbr->tab, ineq) < 0)
+               goto error;
 
        if (cgbr->shifted && !cgbr->shifted->empty && use_shifted(cgbr)) {
                int i;
                unsigned dim;
-               dim = isl_basic_set_total_dim(cgbr->tab->bset);
+               dim = isl_basic_map_total_dim(cgbr->tab->bmap);
 
                if (isl_tab_extend_cons(cgbr->shifted, 1) < 0)
                        goto error;
@@ -2424,7 +2812,8 @@ static void add_gbr_ineq(struct isl_context_gbr *cgbr, isl_int *ineq)
                        isl_int_add(ineq[0], ineq[0], ineq[1 + i]);
                }
 
-               cgbr->shifted = isl_tab_add_ineq(cgbr->shifted, ineq);
+               if (isl_tab_add_ineq(cgbr->shifted, ineq) < 0)
+                       goto error;
 
                for (i = 0; i < dim; ++i) {
                        if (!isl_int_is_neg(ineq[1 + i]))
@@ -2436,7 +2825,8 @@ static void add_gbr_ineq(struct isl_context_gbr *cgbr, isl_int *ineq)
        if (cgbr->cone && cgbr->cone->n_col != cgbr->cone->n_dead) {
                if (isl_tab_extend_cons(cgbr->cone, 1) < 0)
                        goto error;
-               cgbr->cone = isl_tab_add_ineq(cgbr->cone, ineq);
+               if (isl_tab_add_ineq(cgbr->cone, ineq) < 0)
+                       goto error;
        }
 
        return;
@@ -2572,20 +2962,20 @@ static void propagate_equalities(struct isl_context_gbr *cgbr,
        if (!eq)
                goto error;
 
-       if (isl_tab_extend_cons(tab, (cgbr->tab->bset->n_ineq - first)/2) < 0)
+       if (isl_tab_extend_cons(tab, (cgbr->tab->bmap->n_ineq - first)/2) < 0)
                goto error;
 
        isl_seq_clr(eq->el + 1 + tab->n_param,
                    tab->n_var - tab->n_param - tab->n_div);
-       for (i = first; i < cgbr->tab->bset->n_ineq; i += 2) {
+       for (i = first; i < cgbr->tab->bmap->n_ineq; i += 2) {
                int j;
                int r;
                struct isl_tab_undo *snap;
                snap = isl_tab_snap(tab);
 
-               isl_seq_cpy(eq->el, cgbr->tab->bset->ineq[i], 1 + tab->n_param);
+               isl_seq_cpy(eq->el, cgbr->tab->bmap->ineq[i], 1 + tab->n_param);
                isl_seq_cpy(eq->el + 1 + tab->n_var - tab->n_div,
-                           cgbr->tab->bset->ineq[i] + 1 + tab->n_param,
+                           cgbr->tab->bmap->ineq[i] + 1 + tab->n_param,
                            tab->n_div);
 
                r = isl_tab_add_row(tab, eq->el);
@@ -2601,8 +2991,10 @@ static void propagate_equalities(struct isl_context_gbr *cgbr,
                                goto error;
                        continue;
                }
-               isl_tab_pivot(tab, r, j);
-               isl_tab_kill_col(tab, j);
+               if (isl_tab_pivot(tab, r, j) < 0)
+                       goto error;
+               if (isl_tab_kill_col(tab, j) < 0)
+                       goto error;
 
                tab = restore_lexmin(tab);
        }
@@ -2628,16 +3020,18 @@ static int context_gbr_detect_equalities(struct isl_context *context,
        ctx = cgbr->tab->mat->ctx;
 
        if (!cgbr->cone) {
-               cgbr->cone = isl_tab_from_recession_cone(cgbr->tab->bset);
+               struct isl_basic_set *bset = isl_tab_peek_bset(cgbr->tab);
+               cgbr->cone = isl_tab_from_recession_cone(bset);
                if (!cgbr->cone)
                        goto error;
-               cgbr->cone->bset = isl_basic_set_dup(cgbr->tab->bset);
+               if (isl_tab_track_bset(cgbr->cone, isl_basic_set_dup(bset)) < 0)
+                       goto error;
        }
        cgbr->cone = isl_tab_detect_implicit_equalities(cgbr->cone);
 
-       n_ineq = cgbr->tab->bset->n_ineq;
+       n_ineq = cgbr->tab->bmap->n_ineq;
        cgbr->tab = isl_tab_detect_equalities(cgbr->tab, cgbr->cone);
-       if (cgbr->tab && cgbr->tab->bset->n_ineq > n_ineq)
+       if (cgbr->tab && cgbr->tab->bmap->n_ineq > n_ineq)
                propagate_equalities(cgbr, tab, n_ineq);
 
        return 0;
@@ -2667,13 +3061,14 @@ static int context_gbr_add_div(struct isl_context *context, struct isl_vec *div,
                if (isl_tab_allocate_var(cgbr->cone) <0)
                        return -1;
 
-               cgbr->cone->bset = isl_basic_set_extend_dim(cgbr->cone->bset,
-                       isl_basic_set_get_dim(cgbr->cone->bset), 1, 0, 2);
-               k = isl_basic_set_alloc_div(cgbr->cone->bset);
+               cgbr->cone->bmap = isl_basic_map_extend_dim(cgbr->cone->bmap,
+                       isl_basic_map_get_dim(cgbr->cone->bmap), 1, 0, 2);
+               k = isl_basic_map_alloc_div(cgbr->cone->bmap);
                if (k < 0)
                        return -1;
-               isl_seq_cpy(cgbr->cone->bset->div[k], div->el, div->size);
-               isl_tab_push(cgbr->cone, isl_tab_undo_bset_div);
+               isl_seq_cpy(cgbr->cone->bmap->div[k], div->el, div->size);
+               if (isl_tab_push(cgbr->cone, isl_tab_undo_bmap_div) < 0)
+                       return -1;
        }
        return context_tab_add_div(cgbr->tab, div, nonneg);
 }
@@ -2718,7 +3113,8 @@ static void *context_gbr_save(struct isl_context *context)
                return NULL;
 
        snap->tab_snap = isl_tab_snap(cgbr->tab);
-       isl_tab_save_samples(cgbr->tab);
+       if (isl_tab_save_samples(cgbr->tab) < 0)
+               goto error;
 
        if (cgbr->shifted)
                snap->shifted_snap = isl_tab_snap(cgbr->shifted);
@@ -2731,32 +3127,45 @@ static void *context_gbr_save(struct isl_context *context)
                snap->cone_snap = NULL;
 
        return snap;
+error:
+       free(snap);
+       return NULL;
 }
 
 static void context_gbr_restore(struct isl_context *context, void *save)
 {
        struct isl_context_gbr *cgbr = (struct isl_context_gbr *)context;
        struct isl_gbr_tab_undo *snap = (struct isl_gbr_tab_undo *)save;
+       if (!snap)
+               goto error;
        if (isl_tab_rollback(cgbr->tab, snap->tab_snap) < 0) {
                isl_tab_free(cgbr->tab);
                cgbr->tab = NULL;
        }
 
-       if (snap->shifted_snap)
-               isl_tab_rollback(cgbr->shifted, snap->shifted_snap);
-       else if (cgbr->shifted) {
+       if (snap->shifted_snap) {
+               if (isl_tab_rollback(cgbr->shifted, snap->shifted_snap) < 0)
+                       goto error;
+       } else if (cgbr->shifted) {
                isl_tab_free(cgbr->shifted);
                cgbr->shifted = NULL;
        }
 
-       if (snap->cone_snap)
-               isl_tab_rollback(cgbr->cone, snap->cone_snap);
-       else if (cgbr->cone) {
+       if (snap->cone_snap) {
+               if (isl_tab_rollback(cgbr->cone, snap->cone_snap) < 0)
+                       goto error;
+       } else if (cgbr->cone) {
                isl_tab_free(cgbr->cone);
                cgbr->cone = NULL;
        }
 
        free(snap);
+
+       return;
+error:
+       free(snap);
+       isl_tab_free(cgbr->tab);
+       cgbr->tab = NULL;
 }
 
 static int context_gbr_is_ok(struct isl_context *context)
@@ -2820,8 +3229,8 @@ static struct isl_context *isl_context_gbr_alloc(struct isl_basic_set *dom)
        cgbr->tab = isl_tab_init_samples(cgbr->tab);
        if (!cgbr->tab)
                goto error;
-       cgbr->tab->bset = isl_basic_set_cow(isl_basic_set_copy(dom));
-       if (!cgbr->tab->bset)
+       if (isl_tab_track_bset(cgbr->tab,
+                               isl_basic_set_cow(isl_basic_set_copy(dom))) < 0)
                goto error;
        check_gbr_integer_feasible(cgbr);
 
@@ -2836,7 +3245,7 @@ static struct isl_context *isl_context_alloc(struct isl_basic_set *dom)
        if (!dom)
                return NULL;
 
-       if (dom->ctx->context == ISL_CONTEXT_LEXMIN)
+       if (dom->ctx->opt->context == ISL_CONTEXT_LEXMIN)
                return isl_context_lex_alloc(dom);
        else
                return isl_context_gbr_alloc(dom);
@@ -2858,8 +3267,13 @@ static struct isl_sol_map *sol_map_init(struct isl_basic_map *bmap,
        if (!sol_map)
                goto error;
 
-       sol_map->max = max;
+       sol_map->sol.rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
+       sol_map->sol.dec_level.callback.run = &sol_dec_level_wrap;
+       sol_map->sol.dec_level.sol = &sol_map->sol;
+       sol_map->sol.max = max;
+       sol_map->sol.n_out = isl_basic_map_dim(bmap, isl_dim_out);
        sol_map->sol.add = &sol_map_add_wrap;
+       sol_map->sol.add_empty = track_empty ? &sol_map_add_empty_wrap : NULL;
        sol_map->sol.free = &sol_map_free_wrap;
        sol_map->map = isl_map_alloc_dim(isl_basic_map_get_dim(bmap), 1,
                                            ISL_MAP_DISJOINT);
@@ -3056,7 +3470,7 @@ error:
        return 0;
 }
 
-static struct isl_sol *find_solutions(struct isl_sol *sol, struct isl_tab *tab);
+static void find_solutions(struct isl_sol *sol, struct isl_tab *tab);
 
 /* Find solutions for values of the parameters that satisfy the given
  * inequality.
@@ -3072,8 +3486,7 @@ static struct isl_sol *find_solutions(struct isl_sol *sol, struct isl_tab *tab);
  * and that we need to do this before saving the current basis
  * such that the basis has been restore before we restore the row signs.
  */
-static struct isl_sol *find_in_pos(struct isl_sol *sol,
-       struct isl_tab *tab, isl_int *ineq)
+static void find_in_pos(struct isl_sol *sol, struct isl_tab *tab, isl_int *ineq)
 {
        void *saved;
 
@@ -3087,19 +3500,18 @@ static struct isl_sol *find_in_pos(struct isl_sol *sol,
 
        sol->context->op->add_ineq(sol->context, ineq, 0, 1);
 
-       sol = find_solutions(sol, tab);
+       find_solutions(sol, tab);
 
        sol->context->op->restore(sol->context, saved);
-       return sol;
+       return;
 error:
-       sol_free(sol);
-       return NULL;
+       sol->error = 1;
 }
 
 /* Record the absence of solutions for those values of the parameters
  * that do not satisfy the given inequality with equality.
  */
-static struct isl_sol *no_sol_in_strict(struct isl_sol *sol,
+static void no_sol_in_strict(struct isl_sol *sol,
        struct isl_tab *tab, struct isl_vec *ineq)
 {
        int empty;
@@ -3117,16 +3529,15 @@ static struct isl_sol *no_sol_in_strict(struct isl_sol *sol,
 
        empty = tab->empty;
        tab->empty = 1;
-       sol = sol->add(sol, tab);
+       sol_add(sol, tab);
        tab->empty = empty;
 
        isl_int_add_ui(ineq->el[0], ineq->el[0], 1);
 
        sol->context->op->restore(sol->context, saved);
-       return sol;
+       return;
 error:
-       sol_free(sol);
-       return NULL;
+       sol->error = 1;
 }
 
 /* Compute the lexicographic minimum of the set represented by the main
@@ -3223,11 +3634,11 @@ error:
  * In the part of the context where this inequality does not hold, the
  * main tableau is marked as being empty.
  */
-static struct isl_sol *find_solutions(struct isl_sol *sol, struct isl_tab *tab)
+static void find_solutions(struct isl_sol *sol, struct isl_tab *tab)
 {
        struct isl_context *context;
 
-       if (!tab || !sol)
+       if (!tab || sol->error)
                goto error;
 
        context = sol->context;
@@ -3277,25 +3688,27 @@ static struct isl_sol *find_solutions(struct isl_sol *sol, struct isl_tab *tab)
                                        tab->row_sign[row] = isl_tab_row_unknown;
                        }
                        tab->row_sign[split] = isl_tab_row_pos;
-                       sol = find_in_pos(sol, tab, ineq->el);
+                       sol_inc_level(sol);
+                       find_in_pos(sol, tab, ineq->el);
                        tab->row_sign[split] = isl_tab_row_neg;
                        row = split;
                        isl_seq_neg(ineq->el, ineq->el, ineq->size);
                        isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
                        context->op->add_ineq(context, ineq->el, 0, 1);
                        isl_vec_free(ineq);
-                       if (!sol)
+                       if (sol->error)
                                goto error;
                        continue;
                }
                if (tab->rational)
                        break;
-               row = first_non_integer(tab, &flags);
+               row = first_non_integer_row(tab, &flags);
                if (row < 0)
                        break;
                if (ISL_FL_ISSET(flags, I_PAR)) {
                        if (ISL_FL_ISSET(flags, I_VAR)) {
-                               tab = isl_tab_mark_empty(tab);
+                               if (isl_tab_mark_empty(tab) < 0)
+                                       goto error;
                                break;
                        }
                        row = add_cut(tab, row);
@@ -3311,26 +3724,28 @@ static struct isl_sol *find_solutions(struct isl_sol *sol, struct isl_tab *tab)
                        if (d < 0)
                                goto error;
                        ineq = ineq_for_div(context->op->peek_basic_set(context), d);
-                       sol = no_sol_in_strict(sol, tab, ineq);
+                       sol_inc_level(sol);
+                       no_sol_in_strict(sol, tab, ineq);
                        isl_seq_neg(ineq->el, ineq->el, ineq->size);
                        context->op->add_ineq(context, ineq->el, 1, 1);
                        isl_vec_free(ineq);
-                       if (!sol || !context->op->is_ok(context))
+                       if (sol->error || !context->op->is_ok(context))
                                goto error;
                        tab = set_row_cst_to_div(tab, row, d);
+                       if (context->op->is_empty(context))
+                               break;
                } else
                        row = add_parametric_cut(tab, row, context);
                if (row < 0)
                        goto error;
        }
 done:
-       sol = sol->add(sol, tab);
+       sol_add(sol, tab);
        isl_tab_free(tab);
-       return sol;
+       return;
 error:
        isl_tab_free(tab);
        sol_free(sol);
-       return NULL;
 }
 
 /* Compute the lexicographic minimum of the set represented by the main
@@ -3344,11 +3759,12 @@ error:
  * In parts of the context where the added equality does not hold,
  * the main tableau is marked as being empty.
  */
-static struct isl_sol *find_solutions_main(struct isl_sol *sol,
-       struct isl_tab *tab)
+static void find_solutions_main(struct isl_sol *sol, struct isl_tab *tab)
 {
        int row;
 
+       sol->level = 0;
+
        for (row = tab->n_redundant; row < tab->n_row; ++row) {
                int p;
                struct isl_vec *eq;
@@ -3369,17 +3785,20 @@ static struct isl_sol *find_solutions_main(struct isl_sol *sol,
                isl_int_neg(eq->el[1 + p], tab->mat->row[row][0]);
                eq = isl_vec_normalize(eq);
 
-               sol = no_sol_in_strict(sol, tab, eq);
+               sol_inc_level(sol);
+               no_sol_in_strict(sol, tab, eq);
 
                isl_seq_neg(eq->el, eq->el, eq->size);
-               sol = no_sol_in_strict(sol, tab, eq);
+               sol_inc_level(sol);
+               no_sol_in_strict(sol, tab, eq);
                isl_seq_neg(eq->el, eq->el, eq->size);
 
                sol->context->op->add_eq(sol->context, eq->el, 1, 1);
 
                isl_vec_free(eq);
 
-               isl_tab_mark_redundant(tab, row);
+               if (isl_tab_mark_redundant(tab, row) < 0)
+                       goto error;
 
                if (sol->context->op->is_empty(sol->context))
                        break;
@@ -3387,17 +3806,21 @@ static struct isl_sol *find_solutions_main(struct isl_sol *sol,
                row = tab->n_redundant - 1;
        }
 
-       return find_solutions(sol, tab);
+       find_solutions(sol, tab);
+
+       sol->level = 0;
+       sol_pop(sol);
+
+       return;
 error:
        isl_tab_free(tab);
        sol_free(sol);
-       return NULL;
 }
 
-static struct isl_sol_map *sol_map_find_solutions(struct isl_sol_map *sol_map,
+static void sol_map_find_solutions(struct isl_sol_map *sol_map,
        struct isl_tab *tab)
 {
-       return (struct isl_sol_map *)find_solutions_main(&sol_map->sol, tab);
+       find_solutions_main(&sol_map->sol, tab);
 }
 
 /* Check if integer division "div" of "dom" also occurs in "bmap".
@@ -3499,6 +3922,7 @@ struct isl_map *isl_tab_basic_map_partial_lexopt(
        struct isl_map *result = NULL;
        struct isl_sol_map *sol_map = NULL;
        struct isl_context *context;
+       struct isl_basic_map *eq;
 
        if (empty)
                *empty = NULL;
@@ -3508,7 +3932,10 @@ struct isl_map *isl_tab_basic_map_partial_lexopt(
        isl_assert(bmap->ctx,
            isl_basic_map_compatible_domain(bmap, dom), goto error);
 
-       bmap = isl_basic_map_detect_equalities(bmap);
+       eq = isl_basic_map_copy(bmap);
+       eq = isl_basic_map_intersect_domain(eq, isl_basic_set_copy(dom));
+       eq = isl_basic_map_affine_hull(eq);
+       bmap = isl_basic_map_intersect(bmap, eq);
 
        if (dom->n_div) {
                dom = isl_basic_set_order_divs(dom);
@@ -3522,24 +3949,25 @@ struct isl_map *isl_tab_basic_map_partial_lexopt(
        if (isl_basic_set_fast_is_empty(context->op->peek_basic_set(context)))
                /* nothing */;
        else if (isl_basic_map_fast_is_empty(bmap))
-               sol_map = add_empty(sol_map);
+               sol_map_add_empty_if_needed(sol_map,
+                   isl_basic_set_copy(context->op->peek_basic_set(context)));
        else {
                tab = tab_for_lexmin(bmap,
                                    context->op->peek_basic_set(context), 1, max);
                tab = context->op->detect_nonnegative_parameters(context, tab);
-               sol_map = sol_map_find_solutions(sol_map, tab);
-               if (!sol_map)
-                       goto error;
+               sol_map_find_solutions(sol_map, tab);
        }
+       if (sol_map->sol.error)
+               goto error;
 
        result = isl_map_copy(sol_map->map);
        if (empty)
                *empty = isl_set_copy(sol_map->empty);
-       sol_map_free(sol_map);
+       sol_free(&sol_map->sol);
        isl_basic_map_free(bmap);
        return result;
 error:
-       sol_map_free(sol_map);
+       sol_free(&sol_map->sol);
        isl_basic_map_free(bmap);
        return NULL;
 }
@@ -3549,7 +3977,6 @@ struct isl_sol_for {
        int             (*fn)(__isl_take isl_basic_set *dom,
                                __isl_take isl_mat *map, void *user);
        void            *user;
-       int             max;
 };
 
 static void sol_for_free(struct isl_sol_for *sol_for)
@@ -3566,7 +3993,7 @@ static void sol_for_free_wrap(struct isl_sol *sol)
 
 /* Add the solution identified by the tableau and the context tableau.
  *
- * See documentation of sol_map_add for more details.
+ * See documentation of sol_add for more details.
  *
  * Instead of constructing a basic map, this function calls a user
  * defined function with the current context as a basic set and
@@ -3578,88 +4005,31 @@ static void sol_for_free_wrap(struct isl_sol *sol)
  * may refer to the divs, the basic set is not simplified.
  * (Simplification may reorder or remove divs.)
  */
-static struct isl_sol_for *sol_for_add(struct isl_sol_for *sol,
-       struct isl_tab *tab)
+static void sol_for_add(struct isl_sol_for *sol,
+       struct isl_basic_set *dom, struct isl_mat *M)
 {
-       struct isl_basic_set *bset;
-       struct isl_mat *mat = NULL;
-       unsigned n_out;
-       unsigned off;
-       int row, i;
-
-       if (!sol || !tab)
+       if (sol->sol.error || !dom || !M)
                goto error;
 
-       if (tab->empty)
-               return sol;
-
-       off = 2 + tab->M;
-
-       n_out = tab->n_var - tab->n_param - tab->n_div;
-       mat = isl_mat_alloc(tab->mat->ctx, 1 + n_out, 1 + tab->n_param + tab->n_div);
-       if (!mat)
-               goto error;
-
-       isl_seq_clr(mat->row[0] + 1, mat->n_col - 1);
-       isl_int_set_si(mat->row[0][0], 1);
-       for (row = 0; row < n_out; ++row) {
-               int i = tab->n_param + row;
-               int r, j;
-
-               isl_seq_clr(mat->row[1 + row], mat->n_col);
-               if (!tab->var[i].is_row)
-                       continue;
-
-               r = tab->var[i].index;
-               /* no unbounded */
-               if (tab->M)
-                       isl_assert(mat->ctx, isl_int_eq(tab->mat->row[r][2],
-                                                       tab->mat->row[r][0]),
-                                   goto error);
-               isl_int_set(mat->row[1 + row][0], tab->mat->row[r][1]);
-               for (j = 0; j < tab->n_param; ++j) {
-                       int col;
-                       if (tab->var[j].is_row)
-                               continue;
-                       col = tab->var[j].index;
-                       isl_int_set(mat->row[1 + row][1 + j],
-                                   tab->mat->row[r][off + col]);
-               }
-               for (j = 0; j < tab->n_div; ++j) {
-                       int col;
-                       if (tab->var[tab->n_var - tab->n_div+j].is_row)
-                               continue;
-                       col = tab->var[tab->n_var - tab->n_div+j].index;
-                       isl_int_set(mat->row[1 + row][1 + tab->n_param + j],
-                                   tab->mat->row[r][off + col]);
-               }
-               if (!isl_int_is_one(tab->mat->row[r][0]))
-                       isl_seq_scale_down(mat->row[1 + row], mat->row[1 + row],
-                                           tab->mat->row[r][0], mat->n_col);
-               if (sol->max)
-                       isl_seq_neg(mat->row[1 + row], mat->row[1 + row],
-                                   mat->n_col);
-       }
-
-       bset = sol->sol.context->op->peek_basic_set(sol->sol.context);
-       bset = isl_basic_set_dup(bset);
-       bset = isl_basic_set_finalize(bset);
+       dom = isl_basic_set_simplify(dom);
+       dom = isl_basic_set_finalize(dom);
 
-       if (sol->fn(bset, isl_mat_copy(mat), sol->user) < 0)
+       if (sol->fn(isl_basic_set_copy(dom), isl_mat_copy(M), sol->user) < 0)
                goto error;
 
-       isl_mat_free(mat);
-       return sol;
+       isl_basic_set_free(dom);
+       isl_mat_free(M);
+       return;
 error:
-       isl_mat_free(mat);
-       sol_free(&sol->sol);
-       return NULL;
+       isl_basic_set_free(dom);
+       isl_mat_free(M);
+       sol->sol.error = 1;
 }
 
-static struct isl_sol *sol_for_add_wrap(struct isl_sol *sol,
-       struct isl_tab *tab)
+static void sol_for_add_wrap(struct isl_sol *sol,
+       struct isl_basic_set *dom, struct isl_mat *M)
 {
-       return (struct isl_sol *)sol_for_add((struct isl_sol_for *)sol, tab);
+       sol_for_add((struct isl_sol_for *)sol, dom, M);
 }
 
 static struct isl_sol_for *sol_for_init(struct isl_basic_map *bmap, int max,
@@ -3678,10 +4048,15 @@ static struct isl_sol_for *sol_for_init(struct isl_basic_map *bmap, int max,
        dom_dim = isl_dim_domain(isl_dim_copy(bmap->dim));
        dom = isl_basic_set_universe(dom_dim);
 
+       sol_for->sol.rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
+       sol_for->sol.dec_level.callback.run = &sol_dec_level_wrap;
+       sol_for->sol.dec_level.sol = &sol_for->sol;
        sol_for->fn = fn;
        sol_for->user = user;
-       sol_for->max = max;
+       sol_for->sol.max = max;
+       sol_for->sol.n_out = isl_basic_map_dim(bmap, isl_dim_out);
        sol_for->sol.add = &sol_for_add_wrap;
+       sol_for->sol.add_empty = NULL;
        sol_for->sol.free = &sol_for_free_wrap;
 
        sol_for->sol.context = isl_context_alloc(dom);
@@ -3696,10 +4071,10 @@ error:
        return NULL;
 }
 
-static struct isl_sol_for *sol_for_find_solutions(struct isl_sol_for *sol_for,
+static void sol_for_find_solutions(struct isl_sol_for *sol_for,
        struct isl_tab *tab)
 {
-       return (struct isl_sol_for *)find_solutions_main(&sol_for->sol, tab);
+       find_solutions_main(&sol_for->sol, tab);
 }
 
 int isl_basic_map_foreach_lexopt(__isl_keep isl_basic_map *bmap, int max,
@@ -3724,16 +4099,16 @@ int isl_basic_map_foreach_lexopt(__isl_keep isl_basic_map *bmap, int max,
                tab = tab_for_lexmin(bmap,
                                context->op->peek_basic_set(context), 1, max);
                tab = context->op->detect_nonnegative_parameters(context, tab);
-               sol_for = sol_for_find_solutions(sol_for, tab);
-               if (!sol_for)
+               sol_for_find_solutions(sol_for, tab);
+               if (sol_for->sol.error)
                        goto error;
        }
 
-       sol_for_free(sol_for);
+       sol_free(&sol_for->sol);
        isl_basic_map_free(bmap);
        return 0;
 error:
-       sol_for_free(sol_for);
+       sol_free(&sol_for->sol);
        isl_basic_map_free(bmap);
        return -1;
 }