isl_input.c: remove needless indirection
[platform/upstream/isl.git] / isl_tab_pip.c
index 4758106..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"
@@ -523,7 +532,7 @@ static void sol_map_add_empty(struct isl_sol_map *sol,
        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(sol->empty, isl_basic_set_copy(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);
@@ -539,6 +548,18 @@ static void sol_map_add_empty_wrap(struct isl_sol *sol,
        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
@@ -621,7 +642,7 @@ static void sol_map_add(struct isl_sol_map *sol,
        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;
        isl_basic_set_free(dom);
@@ -1100,8 +1121,11 @@ 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;
                if (isl_tab_pivot(tab, row, col) < 0)
@@ -1243,8 +1267,11 @@ static struct isl_tab *add_lexmin_eq(struct isl_tab *tab, isl_int *eq)
        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;
@@ -1287,16 +1314,16 @@ static struct isl_tab *add_lexmin_eq(struct isl_tab *tab, isl_int *eq)
                }
        }
 
-       if (tab->bset) {
-               tab->bset = isl_basic_set_add_ineq(tab->bset, eq);
-               if (isl_tab_push(tab, isl_tab_undo_bset_ineq) < 0)
+       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);
-               if (isl_tab_push(tab, isl_tab_undo_bset_ineq) < 0)
+               if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
                        goto error;
-               if (!tab->bset)
+               if (!tab->bmap)
                        goto error;
        }
 
@@ -1315,11 +1342,11 @@ 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);
-               if (isl_tab_push(tab, isl_tab_undo_bset_ineq) < 0)
+       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->bset)
+               if (!tab->bmap)
                        goto error;
        }
        r = isl_tab_add_row(tab, ineq);
@@ -1404,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
@@ -1414,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))
@@ -1433,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.
  *
@@ -1497,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;
 
@@ -1514,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;
@@ -1542,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);
 
@@ -1641,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);
 
@@ -1745,13 +1798,13 @@ 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);
-       if (isl_tab_push(tab, isl_tab_undo_bset_div) < 0)
+       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;
@@ -1797,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;
        }
@@ -1979,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;
@@ -2051,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;
@@ -2060,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);
 
@@ -2078,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))
@@ -2104,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)
@@ -2347,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)) {
@@ -2440,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:
@@ -2493,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)
@@ -2512,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);
@@ -2576,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)
@@ -2598,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)
@@ -2633,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);
@@ -2663,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;
        }
 
@@ -2729,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;
@@ -2745,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]))
@@ -2757,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;
@@ -2893,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);
@@ -2951,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;
@@ -2990,13 +3061,13 @@ 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);
-               if (isl_tab_push(cgbr->cone, isl_tab_undo_bset_div) < 0)
+               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);
@@ -3158,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);
 
@@ -3631,12 +3702,13 @@ static void find_solutions(struct isl_sol *sol, struct isl_tab *tab)
                }
                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);
@@ -3660,6 +3732,8 @@ static void find_solutions(struct isl_sol *sol, struct isl_tab *tab)
                        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)
@@ -3875,8 +3949,8 @@ 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,
-                   isl_basic_set_dup(context->op->peek_basic_set(context)));
+               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);