isl_tab: introduce parameters and divs
[platform/upstream/isl.git] / isl_tab.c
index 4f2e783..377d0b7 100644 (file)
--- a/isl_tab.c
+++ b/isl_tab.c
@@ -1,3 +1,4 @@
+#include "isl_mat.h"
 #include "isl_map_private.h"
 #include "isl_tab.h"
 
@@ -46,6 +47,8 @@ struct isl_tab *isl_tab_alloc(struct isl_ctx *ctx,
        tab->max_con = n_row;
        tab->n_col = n_var;
        tab->n_var = n_var;
+       tab->n_param = 0;
+       tab->n_div = 0;
        tab->n_dead = 0;
        tab->n_redundant = 0;
        tab->need_undo = 0;
@@ -57,16 +60,16 @@ struct isl_tab *isl_tab_alloc(struct isl_ctx *ctx,
        tab->top = &tab->bottom;
        return tab;
 error:
-       isl_tab_free(ctx, tab);
+       isl_tab_free(tab);
        return NULL;
 }
 
-static int extend_cons(struct isl_ctx *ctx, struct isl_tab *tab, unsigned n_new)
+int isl_tab_extend_cons(struct isl_tab *tab, unsigned n_new)
 {
        if (tab->max_con < tab->n_con + n_new) {
                struct isl_tab_var *con;
 
-               con = isl_realloc_array(ctx, tab->con,
+               con = isl_realloc_array(tab->mat->ctx, tab->con,
                                    struct isl_tab_var, tab->max_con + n_new);
                if (!con)
                        return -1;
@@ -76,11 +79,11 @@ static int extend_cons(struct isl_ctx *ctx, struct isl_tab *tab, unsigned n_new)
        if (tab->mat->n_row < tab->n_row + n_new) {
                int *row_var;
 
-               tab->mat = isl_mat_extend(ctx, tab->mat,
+               tab->mat = isl_mat_extend(tab->mat,
                                                tab->n_row + n_new, tab->n_col);
                if (!tab->mat)
                        return -1;
-               row_var = isl_realloc_array(ctx, tab->row_var,
+               row_var = isl_realloc_array(tab->mat->ctx, tab->row_var,
                                            int, tab->mat->n_row);
                if (!row_var)
                        return -1;
@@ -89,17 +92,16 @@ static int extend_cons(struct isl_ctx *ctx, struct isl_tab *tab, unsigned n_new)
        return 0;
 }
 
-struct isl_tab *isl_tab_extend(struct isl_ctx *ctx, struct isl_tab *tab,
-                               unsigned n_new)
+struct isl_tab *isl_tab_extend(struct isl_tab *tab, unsigned n_new)
 {
-       if (extend_cons(ctx, tab, n_new) >= 0)
+       if (isl_tab_extend_cons(tab, n_new) >= 0)
                return tab;
 
-       isl_tab_free(ctx, tab);
+       isl_tab_free(tab);
        return NULL;
 }
 
-static void free_undo(struct isl_ctx *ctx, struct isl_tab *tab)
+static void free_undo(struct isl_tab *tab)
 {
        struct isl_tab_undo *undo, *next;
 
@@ -110,12 +112,12 @@ static void free_undo(struct isl_ctx *ctx, struct isl_tab *tab)
        tab->top = undo;
 }
 
-void isl_tab_free(struct isl_ctx *ctx, struct isl_tab *tab)
+void isl_tab_free(struct isl_tab *tab)
 {
        if (!tab)
                return;
-       free_undo(ctx, tab);
-       isl_mat_free(ctx, tab->mat);
+       free_undo(tab);
+       isl_mat_free(tab->mat);
        isl_vec_free(tab->dual);
        free(tab->var);
        free(tab->con);
@@ -124,8 +126,64 @@ void isl_tab_free(struct isl_ctx *ctx, struct isl_tab *tab)
        free(tab);
 }
 
-static struct isl_tab_var *var_from_index(struct isl_ctx *ctx,
-       struct isl_tab *tab, int i)
+struct isl_tab *isl_tab_dup(struct isl_tab *tab)
+{
+       int i;
+       struct isl_tab *dup;
+
+       if (!tab)
+               return NULL;
+
+       dup = isl_calloc_type(tab->ctx, struct isl_tab);
+       if (!dup)
+               return NULL;
+       dup->mat = isl_mat_dup(tab->mat);
+       if (!dup->mat)
+               goto error;
+       dup->var = isl_alloc_array(tab->ctx, struct isl_tab_var, tab->n_var);
+       if (!dup->var)
+               goto error;
+       for (i = 0; i < tab->n_var; ++i)
+               dup->var[i] = tab->var[i];
+       dup->con = isl_alloc_array(tab->ctx, struct isl_tab_var, tab->max_con);
+       if (!dup->con)
+               goto error;
+       for (i = 0; i < tab->n_con; ++i)
+               dup->con[i] = tab->con[i];
+       dup->col_var = isl_alloc_array(tab->ctx, int, tab->mat->n_col);
+       if (!dup->col_var)
+               goto error;
+       for (i = 0; i < tab->n_var; ++i)
+               dup->col_var[i] = tab->col_var[i];
+       dup->row_var = isl_alloc_array(tab->ctx, int, tab->mat->n_row);
+       if (!dup->row_var)
+               goto error;
+       for (i = 0; i < tab->n_row; ++i)
+               dup->row_var[i] = tab->row_var[i];
+       dup->n_row = tab->n_row;
+       dup->n_con = tab->n_con;
+       dup->n_eq = tab->n_eq;
+       dup->max_con = tab->max_con;
+       dup->n_col = tab->n_col;
+       dup->n_var = tab->n_var;
+       dup->n_param = tab->n_param;
+       dup->n_div = tab->n_div;
+       dup->n_dead = tab->n_dead;
+       dup->n_redundant = tab->n_redundant;
+       dup->rational = tab->rational;
+       dup->empty = tab->empty;
+       dup->need_undo = 0;
+       dup->in_undo = 0;
+       dup->bottom.type = isl_tab_undo_bottom;
+       dup->bottom.next = NULL;
+       dup->top = &dup->bottom;
+       return dup;
+error:
+       isl_tab_free(dup);
+       return NULL;
+}
+
+static struct isl_tab_var *var_from_index(struct isl_tab *tab, int i)
 {
        if (i >= 0)
                return &tab->var[i];
@@ -133,24 +191,22 @@ static struct isl_tab_var *var_from_index(struct isl_ctx *ctx,
                return &tab->con[~i];
 }
 
-static struct isl_tab_var *var_from_row(struct isl_ctx *ctx,
-       struct isl_tab *tab, int i)
+struct isl_tab_var *isl_tab_var_from_row(struct isl_tab *tab, int i)
 {
-       return var_from_index(ctx, tab, tab->row_var[i]);
+       return var_from_index(tab, tab->row_var[i]);
 }
 
-static struct isl_tab_var *var_from_col(struct isl_ctx *ctx,
-       struct isl_tab *tab, int i)
+static struct isl_tab_var *var_from_col(struct isl_tab *tab, int i)
 {
-       return var_from_index(ctx, tab, tab->col_var[i]);
+       return var_from_index(tab, tab->col_var[i]);
 }
 
 /* Check if there are any upper bounds on column variable "var",
  * i.e., non-negative rows where var appears with a negative coefficient.
  * Return 1 if there are no such bounds.
  */
-static int max_is_manifestly_unbounded(struct isl_ctx *ctx,
-       struct isl_tab *tab, struct isl_tab_var *var)
+static int max_is_manifestly_unbounded(struct isl_tab *tab,
+       struct isl_tab_var *var)
 {
        int i;
 
@@ -159,7 +215,7 @@ static int max_is_manifestly_unbounded(struct isl_ctx *ctx,
        for (i = tab->n_redundant; i < tab->n_row; ++i) {
                if (!isl_int_is_neg(tab->mat->row[i][2 + var->index]))
                        continue;
-               if (var_from_row(ctx, tab, i)->is_nonneg)
+               if (isl_tab_var_from_row(tab, i)->is_nonneg)
                        return 0;
        }
        return 1;
@@ -169,8 +225,8 @@ static int max_is_manifestly_unbounded(struct isl_ctx *ctx,
  * i.e., non-negative rows where var appears with a positive coefficient.
  * Return 1 if there are no such bounds.
  */
-static int min_is_manifestly_unbounded(struct isl_ctx *ctx,
-       struct isl_tab *tab, struct isl_tab_var *var)
+static int min_is_manifestly_unbounded(struct isl_tab *tab,
+       struct isl_tab_var *var)
 {
        int i;
 
@@ -179,7 +235,7 @@ static int min_is_manifestly_unbounded(struct isl_ctx *ctx,
        for (i = tab->n_redundant; i < tab->n_row; ++i) {
                if (!isl_int_is_pos(tab->mat->row[i][2 + var->index]))
                        continue;
-               if (var_from_row(ctx, tab, i)->is_nonneg)
+               if (isl_tab_var_from_row(tab, i)->is_nonneg)
                        return 0;
        }
        return 1;
@@ -205,7 +261,7 @@ static int min_is_manifestly_unbounded(struct isl_ctx *ctx,
  * we check if -sign(a_jc) (a_j0 a_rc - a_r0 a_jc) < 0,
  * where -sign(a_jc) is equal to "sgn".
  */
-static int pivot_row(struct isl_ctx *ctx, struct isl_tab *tab,
+static int pivot_row(struct isl_tab *tab,
        struct isl_tab_var *var, int sgn, int c)
 {
        int j, r, tsgn;
@@ -216,7 +272,7 @@ static int pivot_row(struct isl_ctx *ctx, struct isl_tab *tab,
        for (j = tab->n_redundant; j < tab->n_row; ++j) {
                if (var && j == var->index)
                        continue;
-               if (!var_from_row(ctx, tab, j)->is_nonneg)
+               if (!isl_tab_var_from_row(tab, j)->is_nonneg)
                        continue;
                if (sgn * isl_int_sgn(tab->mat->row[j][2 + c]) >= 0)
                        continue;
@@ -251,7 +307,7 @@ static int pivot_row(struct isl_ctx *ctx, struct isl_tab *tab,
  * to obtain the desired effect.  Otherwise, x_i has to move in the
  * opposite direction.
  */
-static void find_pivot(struct isl_ctx *ctx, struct isl_tab *tab,
+static void find_pivot(struct isl_tab *tab,
        struct isl_tab_var *var, struct isl_tab_var *skip_var,
        int sgn, int *row, int *col)
 {
@@ -260,7 +316,7 @@ static void find_pivot(struct isl_ctx *ctx, struct isl_tab *tab,
 
        *row = *col = -1;
 
-       isl_assert(ctx, var->is_row, return);
+       isl_assert(tab->mat->ctx, var->is_row, return);
        tr = tab->mat->row[var->index];
 
        c = -1;
@@ -268,7 +324,7 @@ static void find_pivot(struct isl_ctx *ctx, struct isl_tab *tab,
                if (isl_int_is_zero(tr[2 + j]))
                        continue;
                if (isl_int_sgn(tr[2 + j]) != sgn &&
-                   var_from_col(ctx, tab, j)->is_nonneg)
+                   var_from_col(tab, j)->is_nonneg)
                        continue;
                if (c < 0 || tab->col_var[j] < tab->col_var[c])
                        c = j;
@@ -277,7 +333,7 @@ static void find_pivot(struct isl_ctx *ctx, struct isl_tab *tab,
                return;
 
        sgn *= isl_int_sgn(tr[2 + c]);
-       r = pivot_row(ctx, tab, skip_var, sgn, c);
+       r = pivot_row(tab, skip_var, sgn, c);
        *row = r < 0 ? var->index : r;
        *col = c;
 }
@@ -288,11 +344,11 @@ static void find_pivot(struct isl_ctx *ctx, struct isl_tab *tab,
  *     - that is the sum of a non-negative sample value and a positive
  *       combination of zero or more non-negative variables.
  */
-static int is_redundant(struct isl_ctx *ctx, struct isl_tab *tab, int row)
+int isl_tab_row_is_redundant(struct isl_tab *tab, int row)
 {
        int i;
 
-       if (tab->row_var[row] < 0 && !var_from_row(ctx, tab, row)->is_nonneg)
+       if (tab->row_var[row] < 0 && !isl_tab_var_from_row(tab, row)->is_nonneg)
                return 0;
 
        if (isl_int_is_neg(tab->mat->row[row][1]))
@@ -303,44 +359,79 @@ static int is_redundant(struct isl_ctx *ctx, struct isl_tab *tab, int row)
                        continue;
                if (isl_int_is_neg(tab->mat->row[row][2 + i]))
                        return 0;
-               if (!var_from_col(ctx, tab, i)->is_nonneg)
+               if (!var_from_col(tab, i)->is_nonneg)
                        return 0;
        }
        return 1;
 }
 
-static void swap_rows(struct isl_ctx *ctx,
-       struct isl_tab *tab, int row1, int row2)
+static void swap_rows(struct isl_tab *tab, int row1, int row2)
 {
        int t;
        t = tab->row_var[row1];
        tab->row_var[row1] = tab->row_var[row2];
        tab->row_var[row2] = t;
-       var_from_row(ctx, tab, row1)->index = row1;
-       var_from_row(ctx, tab, row2)->index = row2;
-       tab->mat = isl_mat_swap_rows(ctx, tab->mat, row1, row2);
+       isl_tab_var_from_row(tab, row1)->index = row1;
+       isl_tab_var_from_row(tab, row2)->index = row2;
+       tab->mat = isl_mat_swap_rows(tab->mat, row1, row2);
 }
 
-static void push(struct isl_ctx *ctx, struct isl_tab *tab,
-       enum isl_tab_undo_type type, struct isl_tab_var *var)
+static void push_union(struct isl_tab *tab,
+       enum isl_tab_undo_type type, union isl_tab_undo_val u)
 {
        struct isl_tab_undo *undo;
 
        if (!tab->need_undo)
                return;
 
-       undo = isl_alloc_type(ctx, struct isl_tab_undo);
+       undo = isl_alloc_type(tab->mat->ctx, struct isl_tab_undo);
        if (!undo) {
-               free_undo(ctx, tab);
+               free_undo(tab);
                tab->top = NULL;
                return;
        }
        undo->type = type;
-       undo->var = var;
+       undo->u = u;
        undo->next = tab->top;
        tab->top = undo;
 }
 
+void isl_tab_push_var(struct isl_tab *tab,
+       enum isl_tab_undo_type type, struct isl_tab_var *var)
+{
+       union isl_tab_undo_val u;
+       if (var->is_row)
+               u.var_index = tab->row_var[var->index];
+       else
+               u.var_index = tab->col_var[var->index];
+       push_union(tab, type, u);
+}
+
+void isl_tab_push(struct isl_tab *tab, enum isl_tab_undo_type type)
+{
+       union isl_tab_undo_val u = { 0 };
+       push_union(tab, type, u);
+}
+
+/* Push a record on the undo stack describing the current basic
+ * variables, so that the this state can be restored during rollback.
+ */
+void isl_tab_push_basis(struct isl_tab *tab)
+{
+       int i;
+       union isl_tab_undo_val u;
+
+       u.col_var = isl_alloc_array(tab->mat->ctx, int, tab->n_col);
+       if (!u.col_var) {
+               free_undo(tab);
+               tab->top = NULL;
+               return;
+       }
+       for (i = 0; i < tab->n_col; ++i)
+               u.col_var[i] = tab->col_var[i];
+       push_union(tab, isl_tab_undo_saved_basis, u);
+}
+
 /* Mark row with index "row" as being redundant.
  * If we may need to undo the operation or if the row represents
  * a variable of the original problem, the row is kept,
@@ -353,40 +444,40 @@ static void push(struct isl_ctx *ctx, struct isl_tab *tab,
  * then a return value of 1 means that the row with the given
  * row number may now contain a different row that hasn't been checked yet.
  */
-static int mark_redundant(struct isl_ctx *ctx,
-       struct isl_tab *tab, int row)
+int isl_tab_mark_redundant(struct isl_tab *tab, int row)
 {
-       struct isl_tab_var *var = var_from_row(ctx, tab, row);
+       struct isl_tab_var *var = isl_tab_var_from_row(tab, row);
        var->is_redundant = 1;
-       isl_assert(ctx, row >= tab->n_redundant, return);
+       isl_assert(tab->mat->ctx, row >= tab->n_redundant, return);
        if (tab->need_undo || tab->row_var[row] >= 0) {
-               if (tab->row_var[row] >= 0) {
+               if (tab->row_var[row] >= 0 && !var->is_nonneg) {
                        var->is_nonneg = 1;
-                       push(ctx, tab, isl_tab_undo_nonneg, var);
+                       isl_tab_push_var(tab, isl_tab_undo_nonneg, var);
                }
                if (row != tab->n_redundant)
-                       swap_rows(ctx, tab, row, tab->n_redundant);
-               push(ctx, tab, isl_tab_undo_redundant, var);
+                       swap_rows(tab, row, tab->n_redundant);
+               isl_tab_push_var(tab, isl_tab_undo_redundant, var);
                tab->n_redundant++;
                return 0;
        } else {
                if (row != tab->n_row - 1)
-                       swap_rows(ctx, tab, row, tab->n_row - 1);
-               var_from_row(ctx, tab, tab->n_row - 1)->index = -1;
+                       swap_rows(tab, row, tab->n_row - 1);
+               isl_tab_var_from_row(tab, tab->n_row - 1)->index = -1;
                tab->n_row--;
                return 1;
        }
 }
 
-static void mark_empty(struct isl_ctx *ctx, struct isl_tab *tab)
+struct isl_tab *isl_tab_mark_empty(struct isl_tab *tab)
 {
        if (!tab->empty && tab->need_undo)
-               push(ctx, tab, isl_tab_undo_empty, NULL);
+               isl_tab_push(tab, isl_tab_undo_empty);
        tab->empty = 1;
+       return tab;
 }
 
 /* Given a row number "row" and a column number "col", pivot the tableau
- * such that the associated variable are interchanged.
+ * such that the associated variables are interchanged.
  * The given row in the tableau expresses
  *
  *     x_r = a_r0 + \sum_i a_ri x_i
@@ -435,8 +526,7 @@ static void mark_empty(struct isl_ctx *ctx, struct isl_tab *tab)
  * s(n_rc)d_r n_jc/(|n_rc| d_j)        (n_ji |n_rc| - s(n_rc)n_jc n_ri)/(|n_rc| d_j)
  *
  */
-static void pivot(struct isl_ctx *ctx,
-       struct isl_tab *tab, int row, int col)
+void isl_tab_pivot(struct isl_tab *tab, int row, int col)
 {
        int i, j;
        int sgn;
@@ -473,16 +563,16 @@ static void pivot(struct isl_ctx *ctx,
                }
                isl_int_mul(mat->row[i][2 + col],
                            mat->row[i][2 + col], mat->row[row][2 + col]);
-               if (!isl_int_is_one(mat->row[row][0]))
+               if (!isl_int_is_one(mat->row[i][0]))
                        isl_seq_normalize(mat->row[i], 2 + tab->n_col);
        }
        t = tab->row_var[row];
        tab->row_var[row] = tab->col_var[col];
        tab->col_var[col] = t;
-       var = var_from_row(ctx, tab, row);
+       var = isl_tab_var_from_row(tab, row);
        var->is_row = 1;
        var->index = row;
-       var = var_from_col(ctx, tab, col);
+       var = var_from_col(tab, col);
        var->is_row = 0;
        var->index = col;
        if (tab->in_undo)
@@ -490,9 +580,9 @@ static void pivot(struct isl_ctx *ctx,
        for (i = tab->n_redundant; i < tab->n_row; ++i) {
                if (isl_int_is_zero(mat->row[i][2 + col]))
                        continue;
-               if (!var_from_row(ctx, tab, i)->frozen &&
-                   is_redundant(ctx, tab, i))
-                       if (mark_redundant(ctx, tab, i))
+               if (!isl_tab_var_from_row(tab, i)->frozen &&
+                   isl_tab_row_is_redundant(tab, i))
+                       if (isl_tab_mark_redundant(tab, i))
                                --i;
        }
 }
@@ -500,28 +590,37 @@ static void pivot(struct isl_ctx *ctx,
 /* If "var" represents a column variable, then pivot is up (sgn > 0)
  * or down (sgn < 0) to a row.  The variable is assumed not to be
  * unbounded in the specified direction.
+ * If sgn = 0, then the variable is unbounded in both directions,
+ * and we pivot with any row we can find.
  */
-static void to_row(struct isl_ctx *ctx,
-       struct isl_tab *tab, struct isl_tab_var *var, int sign)
+static void to_row(struct isl_tab *tab, struct isl_tab_var *var, int sign)
 {
        int r;
 
        if (var->is_row)
                return;
 
-       r = pivot_row(ctx, tab, NULL, sign, var->index);
-       isl_assert(ctx, r >= 0, return);
-       pivot(ctx, tab, r, var->index);
+       if (sign == 0) {
+               for (r = tab->n_redundant; r < tab->n_row; ++r)
+                       if (!isl_int_is_zero(tab->mat->row[r][2 + var->index]))
+                               break;
+               isl_assert(tab->mat->ctx, r < tab->n_row, return);
+       } else {
+               r = pivot_row(tab, NULL, sign, var->index);
+               isl_assert(tab->mat->ctx, r >= 0, return);
+       }
+
+       isl_tab_pivot(tab, r, var->index);
 }
 
-static void check_table(struct isl_ctx *ctx, struct isl_tab *tab)
+static void check_table(struct isl_tab *tab)
 {
        int i;
 
        if (tab->empty)
                return;
        for (i = 0; i < tab->n_row; ++i) {
-               if (!var_from_row(ctx, tab, i)->is_nonneg)
+               if (!isl_tab_var_from_row(tab, i)->is_nonneg)
                        continue;
                assert(!isl_int_is_neg(tab->mat->row[i][1]));
        }
@@ -538,19 +637,18 @@ static void check_table(struct isl_ctx *ctx, struct isl_tab *tab)
  *     - the sample value is positive
  *     - the variable is pivoted into a manifestly unbounded column
  */
-static int sign_of_max(struct isl_ctx *ctx,
-       struct isl_tab *tab, struct isl_tab_var *var)
+static int sign_of_max(struct isl_tab *tab, struct isl_tab_var *var)
 {
        int row, col;
 
-       if (max_is_manifestly_unbounded(ctx, tab, var))
+       if (max_is_manifestly_unbounded(tab, var))
                return 1;
-       to_row(ctx, tab, var, 1);
+       to_row(tab, var, 1);
        while (!isl_int_is_pos(tab->mat->row[var->index][1])) {
-               find_pivot(ctx, tab, var, var, 1, &row, &col);
+               find_pivot(tab, var, var, 1, &row, &col);
                if (row == -1)
                        return isl_int_sgn(tab->mat->row[var->index][1]);
-               pivot(ctx, tab, row, col);
+               isl_tab_pivot(tab, row, col);
                if (!var->is_row) /* manifestly unbounded */
                        return 1;
        }
@@ -562,16 +660,15 @@ static int sign_of_max(struct isl_ctx *ctx,
  * Return the sign of the sample value after the pivots have been
  * performed.
  */
-static int restore_row(struct isl_ctx *ctx,
-       struct isl_tab *tab, struct isl_tab_var *var)
+static int restore_row(struct isl_tab *tab, struct isl_tab_var *var)
 {
        int row, col;
 
        while (isl_int_is_neg(tab->mat->row[var->index][1])) {
-               find_pivot(ctx, tab, var, var, 1, &row, &col);
+               find_pivot(tab, var, var, 1, &row, &col);
                if (row == -1)
                        break;
-               pivot(ctx, tab, row, col);
+               isl_tab_pivot(tab, row, col);
                if (!var->is_row) /* manifestly unbounded */
                        return 1;
        }
@@ -583,18 +680,17 @@ static int restore_row(struct isl_ctx *ctx,
  * function, "var" is still a row variable, but its sample
  * value may not be non-negative, even if the function returns 1.
  */
-static int at_least_zero(struct isl_ctx *ctx,
-       struct isl_tab *tab, struct isl_tab_var *var)
+static int at_least_zero(struct isl_tab *tab, struct isl_tab_var *var)
 {
        int row, col;
 
        while (isl_int_is_neg(tab->mat->row[var->index][1])) {
-               find_pivot(ctx, tab, var, var, 1, &row, &col);
+               find_pivot(tab, var, var, 1, &row, &col);
                if (row == -1)
                        break;
                if (row == var->index) /* manifestly unbounded */
                        return 1;
-               pivot(ctx, tab, row, col);
+               isl_tab_pivot(tab, row, col);
        }
        return !isl_int_is_neg(tab->mat->row[var->index][1]);
 }
@@ -616,28 +712,27 @@ static int at_least_zero(struct isl_ctx *ctx,
  * In that case we look for upward pivots until we reach a non-negative
  * value again.
  */
-static int sign_of_min(struct isl_ctx *ctx,
-       struct isl_tab *tab, struct isl_tab_var *var)
+static int sign_of_min(struct isl_tab *tab, struct isl_tab_var *var)
 {
        int row, col;
        struct isl_tab_var *pivot_var;
 
-       if (min_is_manifestly_unbounded(ctx, tab, var))
+       if (min_is_manifestly_unbounded(tab, var))
                return -1;
        if (!var->is_row) {
                col = var->index;
-               row = pivot_row(ctx, tab, NULL, -1, col);
-               pivot_var = var_from_col(ctx, tab, col);
-               pivot(ctx, tab, row, col);
+               row = pivot_row(tab, NULL, -1, col);
+               pivot_var = var_from_col(tab, col);
+               isl_tab_pivot(tab, row, col);
                if (var->is_redundant)
                        return 0;
                if (isl_int_is_neg(tab->mat->row[var->index][1])) {
                        if (var->is_nonneg) {
                                if (!pivot_var->is_redundant &&
                                    pivot_var->index == row)
-                                       pivot(ctx, tab, row, col);
+                                       isl_tab_pivot(tab, row, col);
                                else
-                                       restore_row(ctx, tab, var);
+                                       restore_row(tab, var);
                        }
                        return -1;
                }
@@ -645,22 +740,22 @@ static int sign_of_min(struct isl_ctx *ctx,
        if (var->is_redundant)
                return 0;
        while (!isl_int_is_neg(tab->mat->row[var->index][1])) {
-               find_pivot(ctx, tab, var, var, -1, &row, &col);
+               find_pivot(tab, var, var, -1, &row, &col);
                if (row == var->index)
                        return -1;
                if (row == -1)
                        return isl_int_sgn(tab->mat->row[var->index][1]);
-               pivot_var = var_from_col(ctx, tab, col);
-               pivot(ctx, tab, row, col);
+               pivot_var = var_from_col(tab, col);
+               isl_tab_pivot(tab, row, col);
                if (var->is_redundant)
                        return 0;
        }
        if (var->is_nonneg) {
                /* pivot back to non-negative value */
                if (!pivot_var->is_redundant && pivot_var->index == row)
-                       pivot(ctx, tab, row, col);
+                       isl_tab_pivot(tab, row, col);
                else
-                       restore_row(ctx, tab, var);
+                       restore_row(tab, var);
        }
        return -1;
 }
@@ -672,19 +767,18 @@ static int sign_of_min(struct isl_ctx *ctx,
  * the function is called and will be made non-negative again before
  * the function returns.
  */
-static int min_at_most_neg_one(struct isl_ctx *ctx,
-       struct isl_tab *tab, struct isl_tab_var *var)
+int isl_tab_min_at_most_neg_one(struct isl_tab *tab, struct isl_tab_var *var)
 {
        int row, col;
        struct isl_tab_var *pivot_var;
 
-       if (min_is_manifestly_unbounded(ctx, tab, var))
+       if (min_is_manifestly_unbounded(tab, var))
                return 1;
        if (!var->is_row) {
                col = var->index;
-               row = pivot_row(ctx, tab, NULL, -1, col);
-               pivot_var = var_from_col(ctx, tab, col);
-               pivot(ctx, tab, row, col);
+               row = pivot_row(tab, NULL, -1, col);
+               pivot_var = var_from_col(tab, col);
+               isl_tab_pivot(tab, row, col);
                if (var->is_redundant)
                        return 0;
                if (isl_int_is_neg(tab->mat->row[var->index][1]) &&
@@ -693,9 +787,9 @@ static int min_at_most_neg_one(struct isl_ctx *ctx,
                        if (var->is_nonneg) {
                                if (!pivot_var->is_redundant &&
                                    pivot_var->index == row)
-                                       pivot(ctx, tab, row, col);
+                                       isl_tab_pivot(tab, row, col);
                                else
-                                       restore_row(ctx, tab, var);
+                                       restore_row(tab, var);
                        }
                        return 1;
                }
@@ -703,13 +797,13 @@ static int min_at_most_neg_one(struct isl_ctx *ctx,
        if (var->is_redundant)
                return 0;
        do {
-               find_pivot(ctx, tab, var, var, -1, &row, &col);
+               find_pivot(tab, var, var, -1, &row, &col);
                if (row == var->index)
                        return 1;
                if (row == -1)
                        return 0;
-               pivot_var = var_from_col(ctx, tab, col);
-               pivot(ctx, tab, row, col);
+               pivot_var = var_from_col(tab, col);
+               isl_tab_pivot(tab, row, col);
                if (var->is_redundant)
                        return 0;
        } while (!isl_int_is_neg(tab->mat->row[var->index][1]) ||
@@ -718,8 +812,8 @@ static int min_at_most_neg_one(struct isl_ctx *ctx,
        if (var->is_nonneg) {
                /* pivot back to non-negative value */
                if (!pivot_var->is_redundant && pivot_var->index == row)
-                       pivot(ctx, tab, row, col);
-               restore_row(ctx, tab, var);
+                       isl_tab_pivot(tab, row, col);
+               restore_row(tab, var);
        }
        return 1;
 }
@@ -727,37 +821,35 @@ static int min_at_most_neg_one(struct isl_ctx *ctx,
 /* Return 1 if "var" can attain values >= 1.
  * Return 0 otherwise.
  */
-static int at_least_one(struct isl_ctx *ctx,
-       struct isl_tab *tab, struct isl_tab_var *var)
+static int at_least_one(struct isl_tab *tab, struct isl_tab_var *var)
 {
        int row, col;
        isl_int *r;
 
-       if (max_is_manifestly_unbounded(ctx, tab, var))
+       if (max_is_manifestly_unbounded(tab, var))
                return 1;
-       to_row(ctx, tab, var, 1);
+       to_row(tab, var, 1);
        r = tab->mat->row[var->index];
        while (isl_int_lt(r[1], r[0])) {
-               find_pivot(ctx, tab, var, var, 1, &row, &col);
+               find_pivot(tab, var, var, 1, &row, &col);
                if (row == -1)
                        return isl_int_ge(r[1], r[0]);
                if (row == var->index) /* manifestly unbounded */
                        return 1;
-               pivot(ctx, tab, row, col);
+               isl_tab_pivot(tab, row, col);
        }
        return 1;
 }
 
-static void swap_cols(struct isl_ctx *ctx,
-       struct isl_tab *tab, int col1, int col2)
+static void swap_cols(struct isl_tab *tab, int col1, int col2)
 {
        int t;
        t = tab->col_var[col1];
        tab->col_var[col1] = tab->col_var[col2];
        tab->col_var[col2] = t;
-       var_from_col(ctx, tab, col1)->index = col1;
-       var_from_col(ctx, tab, col2)->index = col2;
-       tab->mat = isl_mat_swap_cols(ctx, tab->mat, 2 + col1, 2 + col2);
+       var_from_col(tab, col1)->index = col1;
+       var_from_col(tab, col2)->index = col2;
+       tab->mat = isl_mat_swap_cols(tab->mat, 2 + col1, 2 + col2);
 }
 
 /* Mark column with index "col" as representing a zero variable.
@@ -772,20 +864,19 @@ static void swap_cols(struct isl_ctx *ctx,
  * column number may now contain a different column that
  * hasn't been checked yet.
  */
-static int kill_col(struct isl_ctx *ctx,
-       struct isl_tab *tab, int col)
+int isl_tab_kill_col(struct isl_tab *tab, int col)
 {
-       var_from_col(ctx, tab, col)->is_zero = 1;
+       var_from_col(tab, col)->is_zero = 1;
        if (tab->need_undo) {
-               push(ctx, tab, isl_tab_undo_zero, var_from_col(ctx, tab, col));
+               isl_tab_push_var(tab, isl_tab_undo_zero, var_from_col(tab, col));
                if (col != tab->n_dead)
-                       swap_cols(ctx, tab, col, tab->n_dead);
+                       swap_cols(tab, col, tab->n_dead);
                tab->n_dead++;
                return 0;
        } else {
                if (col != tab->n_col - 1)
-                       swap_cols(ctx, tab, col, tab->n_col - 1);
-               var_from_col(ctx, tab, tab->n_col - 1)->index = -1;
+                       swap_cols(tab, col, tab->n_col - 1);
+               var_from_col(tab, tab->n_col - 1)->index = -1;
                tab->n_col--;
                return 1;
        }
@@ -799,23 +890,47 @@ static int kill_col(struct isl_ctx *ctx,
  * then also be written as the negative sum of non-negative variables
  * and must therefore also be zero.
  */
-static void close_row(struct isl_ctx *ctx,
-       struct isl_tab *tab, struct isl_tab_var *var)
+static void close_row(struct isl_tab *tab, struct isl_tab_var *var)
 {
        int j;
        struct isl_mat *mat = tab->mat;
 
-       isl_assert(ctx, var->is_nonneg, return);
+       isl_assert(tab->mat->ctx, var->is_nonneg, return);
        var->is_zero = 1;
        for (j = tab->n_dead; j < tab->n_col; ++j) {
                if (isl_int_is_zero(mat->row[var->index][2 + j]))
                        continue;
-               isl_assert(ctx, isl_int_is_neg(mat->row[var->index][2 + j]),
-                       return);
-               if (kill_col(ctx, tab, j))
+               isl_assert(tab->mat->ctx,
+                       isl_int_is_neg(mat->row[var->index][2 + j]), return);
+               if (isl_tab_kill_col(tab, j))
                        --j;
        }
-       mark_redundant(ctx, tab, var->index);
+       isl_tab_mark_redundant(tab, var->index);
+}
+
+/* Add a constraint to the tableau and allocate a row for it.
+ * Return the index into the constraint array "con".
+ */
+int isl_tab_allocate_con(struct isl_tab *tab)
+{
+       int r;
+
+       isl_assert(tab->mat->ctx, tab->n_row < tab->mat->n_row, return -1);
+
+       r = tab->n_con;
+       tab->con[r].index = tab->n_row;
+       tab->con[r].is_row = 1;
+       tab->con[r].is_nonneg = 0;
+       tab->con[r].is_zero = 0;
+       tab->con[r].is_redundant = 0;
+       tab->con[r].frozen = 0;
+       tab->row_var[tab->n_row] = ~r;
+
+       tab->n_row++;
+       tab->n_con++;
+       isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->con[r]);
+
+       return r;
 }
 
 /* Add a row to the tableau.  The row is given as an affine combination
@@ -834,26 +949,20 @@ static void close_row(struct isl_ctx *ctx,
  *
  *     with g the gcd of d_r and d_x and m the lcm of d_r and d_x.
  */
-static int add_row(struct isl_ctx *ctx, struct isl_tab *tab, isl_int *line)
+int isl_tab_add_row(struct isl_tab *tab, isl_int *line)
 {
        int i;
-       unsigned r;
+       int r;
        isl_int *row;
        isl_int a, b;
 
-       isl_assert(ctx, tab->n_row < tab->mat->n_row, return -1);
+       r = isl_tab_allocate_con(tab);
+       if (r < 0)
+               return -1;
 
        isl_int_init(a);
        isl_int_init(b);
-       r = tab->n_con;
-       tab->con[r].index = tab->n_row;
-       tab->con[r].is_row = 1;
-       tab->con[r].is_nonneg = 0;
-       tab->con[r].is_zero = 0;
-       tab->con[r].is_redundant = 0;
-       tab->con[r].frozen = 0;
-       tab->row_var[tab->n_row] = ~r;
-       row = tab->mat->row[tab->n_row];
+       row = tab->mat->row[tab->con[r].index];
        isl_int_set_si(row[0], 1);
        isl_int_set(row[1], line[0]);
        isl_seq_clr(row + 2, tab->n_col);
@@ -876,20 +985,17 @@ static int add_row(struct isl_ctx *ctx, struct isl_tab *tab, isl_int *line)
                                                        line[1 + i], row[0]);
        }
        isl_seq_normalize(row, 2 + tab->n_col);
-       tab->n_row++;
-       tab->n_con++;
-       push(ctx, tab, isl_tab_undo_allocate, &tab->con[r]);
        isl_int_clear(a);
        isl_int_clear(b);
 
        return r;
 }
 
-static int drop_row(struct isl_ctx *ctx, struct isl_tab *tab, int row)
+static int drop_row(struct isl_tab *tab, int row)
 {
-       isl_assert(ctx, ~tab->row_var[row] == tab->n_con - 1, return -1);
+       isl_assert(tab->mat->ctx, ~tab->row_var[row] == tab->n_con - 1, return -1);
        if (row != tab->n_row - 1)
-               swap_rows(ctx, tab, row, tab->n_row - 1);
+               swap_rows(tab, row, tab->n_row - 1);
        tab->n_row--;
        tab->n_con--;
        return 0;
@@ -898,41 +1004,38 @@ static int drop_row(struct isl_ctx *ctx, struct isl_tab *tab, int row)
 /* Add inequality "ineq" and check if it conflicts with the
  * previously added constraints or if it is obviously redundant.
  */
-struct isl_tab *isl_tab_add_ineq(struct isl_ctx *ctx,
-       struct isl_tab *tab, isl_int *ineq)
+struct isl_tab *isl_tab_add_ineq(struct isl_tab *tab, isl_int *ineq)
 {
        int r;
        int sgn;
 
        if (!tab)
                return NULL;
-       r = add_row(ctx, tab, ineq);
+       r = isl_tab_add_row(tab, ineq);
        if (r < 0)
                goto error;
        tab->con[r].is_nonneg = 1;
-       push(ctx, tab, isl_tab_undo_nonneg, &tab->con[r]);
-       if (is_redundant(ctx, tab, tab->con[r].index)) {
-               mark_redundant(ctx, tab, tab->con[r].index);
+       isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]);
+       if (isl_tab_row_is_redundant(tab, tab->con[r].index)) {
+               isl_tab_mark_redundant(tab, tab->con[r].index);
                return tab;
        }
 
-       sgn = restore_row(ctx, tab, &tab->con[r]);
+       sgn = restore_row(tab, &tab->con[r]);
        if (sgn < 0)
-               mark_empty(ctx, tab);
-       else if (tab->con[r].is_row &&
-                is_redundant(ctx, tab, tab->con[r].index))
-               mark_redundant(ctx, tab, tab->con[r].index);
+               return isl_tab_mark_empty(tab);
+       if (tab->con[r].is_row && isl_tab_row_is_redundant(tab, tab->con[r].index))
+               isl_tab_mark_redundant(tab, tab->con[r].index);
        return tab;
 error:
-       isl_tab_free(ctx, tab);
+       isl_tab_free(tab);
        return NULL;
 }
 
 /* Pivot a non-negative variable down until it reaches the value zero
  * and then pivot the variable into a column position.
  */
-static int to_col(struct isl_ctx *ctx,
-       struct isl_tab *tab, struct isl_tab_var *var)
+int to_col(struct isl_tab *tab, struct isl_tab_var *var)
 {
        int i;
        int row, col;
@@ -941,9 +1044,9 @@ static int to_col(struct isl_ctx *ctx,
                return;
 
        while (isl_int_is_pos(tab->mat->row[var->index][1])) {
-               find_pivot(ctx, tab, var, NULL, -1, &row, &col);
-               isl_assert(ctx, row != -1, return -1);
-               pivot(ctx, tab, row, col);
+               find_pivot(tab, var, NULL, -1, &row, &col);
+               isl_assert(tab->mat->ctx, row != -1, return -1);
+               isl_tab_pivot(tab, row, col);
                if (!var->is_row)
                        return;
        }
@@ -952,8 +1055,8 @@ static int to_col(struct isl_ctx *ctx,
                if (!isl_int_is_zero(tab->mat->row[var->index][2 + i]))
                        break;
 
-       isl_assert(ctx, i < tab->n_col, return -1);
-       pivot(ctx, tab, var->index, i);
+       isl_assert(tab->mat->ctx, i < tab->n_col, return -1);
+       isl_tab_pivot(tab, var->index, i);
 
        return 0;
 }
@@ -963,38 +1066,35 @@ static int to_col(struct isl_ctx *ctx,
  * Adding the equalities is currently only really useful for a later call
  * to isl_tab_ineq_type.
  */
-static struct isl_tab *add_eq(struct isl_ctx *ctx,
-       struct isl_tab *tab, isl_int *eq)
+static struct isl_tab *add_eq(struct isl_tab *tab, isl_int *eq)
 {
        int i;
        int r;
 
        if (!tab)
                return NULL;
-       r = add_row(ctx, tab, eq);
+       r = isl_tab_add_row(tab, eq);
        if (r < 0)
                goto error;
 
        r = tab->con[r].index;
-       for (i = tab->n_dead; i < tab->n_col; ++i) {
-               if (isl_int_is_zero(tab->mat->row[r][2 + i]))
-                       continue;
-               pivot(ctx, tab, r, i);
-               kill_col(ctx, tab, i);
-               break;
-       }
+       i = isl_seq_first_non_zero(tab->mat->row[r] + 2 + tab->n_dead,
+                                       tab->n_col - tab->n_dead);
+       isl_assert(tab->mat->ctx, i >= 0, goto error);
+       i += tab->n_dead;
+       isl_tab_pivot(tab, r, i);
+       isl_tab_kill_col(tab, i);
        tab->n_eq++;
 
        return tab;
 error:
-       isl_tab_free(ctx, tab);
+       isl_tab_free(tab);
        return NULL;
 }
 
 /* Add an equality that is known to be valid for the given tableau.
  */
-struct isl_tab *isl_tab_add_valid_eq(struct isl_ctx *ctx,
-       struct isl_tab *tab, isl_int *eq)
+struct isl_tab *isl_tab_add_valid_eq(struct isl_tab *tab, isl_int *eq)
 {
        struct isl_tab_var *var;
        int i;
@@ -1002,7 +1102,7 @@ struct isl_tab *isl_tab_add_valid_eq(struct isl_ctx *ctx,
 
        if (!tab)
                return NULL;
-       r = add_row(ctx, tab, eq);
+       r = isl_tab_add_row(tab, eq);
        if (r < 0)
                goto error;
 
@@ -1012,14 +1112,14 @@ struct isl_tab *isl_tab_add_valid_eq(struct isl_ctx *ctx,
                isl_seq_neg(tab->mat->row[r] + 1, tab->mat->row[r] + 1,
                            1 + tab->n_col);
        var->is_nonneg = 1;
-       if (to_col(ctx, tab, var) < 0)
+       if (to_col(tab, var) < 0)
                goto error;
        var->is_nonneg = 0;
-       kill_col(ctx, tab, var->index);
+       isl_tab_kill_col(tab, var->index);
 
        return tab;
 error:
-       isl_tab_free(ctx, tab);
+       isl_tab_free(tab);
        return NULL;
 }
 
@@ -1036,17 +1136,15 @@ struct isl_tab *isl_tab_from_basic_map(struct isl_basic_map *bmap)
        if (!tab)
                return NULL;
        tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
-       if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) {
-               mark_empty(bmap->ctx, tab);
-               return tab;
-       }
+       if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
+               return isl_tab_mark_empty(tab);
        for (i = 0; i < bmap->n_eq; ++i) {
-               tab = add_eq(bmap->ctx, tab, bmap->eq[i]);
+               tab = add_eq(tab, bmap->eq[i]);
                if (!tab)
                        return tab;
        }
        for (i = 0; i < bmap->n_ineq; ++i) {
-               tab = isl_tab_add_ineq(bmap->ctx, tab, bmap->ineq[i]);
+               tab = isl_tab_add_ineq(tab, bmap->ineq[i]);
                if (!tab || tab->empty)
                        return tab;
        }
@@ -1077,7 +1175,7 @@ struct isl_tab *isl_tab_from_recession_cone(struct isl_basic_map *bmap)
        isl_int_init(cst);
        for (i = 0; i < bmap->n_eq; ++i) {
                isl_int_swap(bmap->eq[i][0], cst);
-               tab = add_eq(bmap->ctx, tab, bmap->eq[i]);
+               tab = add_eq(tab, bmap->eq[i]);
                isl_int_swap(bmap->eq[i][0], cst);
                if (!tab)
                        goto done;
@@ -1085,26 +1183,26 @@ struct isl_tab *isl_tab_from_recession_cone(struct isl_basic_map *bmap)
        for (i = 0; i < bmap->n_ineq; ++i) {
                int r;
                isl_int_swap(bmap->ineq[i][0], cst);
-               r = add_row(bmap->ctx, tab, bmap->ineq[i]);
+               r = isl_tab_add_row(tab, bmap->ineq[i]);
                isl_int_swap(bmap->ineq[i][0], cst);
                if (r < 0)
                        goto error;
                tab->con[r].is_nonneg = 1;
-               push(bmap->ctx, tab, isl_tab_undo_nonneg, &tab->con[r]);
+               isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]);
        }
 done:
        isl_int_clear(cst);
        return tab;
 error:
        isl_int_clear(cst);
-       isl_tab_free(bmap->ctx, tab);
+       isl_tab_free(tab);
        return NULL;
 }
 
 /* Assuming "tab" is the tableau of a cone, check if the cone is
  * bounded, i.e., if it is empty or only contains the origin.
  */
-int isl_tab_cone_is_bounded(struct isl_ctx *ctx, struct isl_tab *tab)
+int isl_tab_cone_is_bounded(struct isl_tab *tab)
 {
        int i;
 
@@ -1115,25 +1213,31 @@ int isl_tab_cone_is_bounded(struct isl_ctx *ctx, struct isl_tab *tab)
        if (tab->n_dead == tab->n_col)
                return 1;
 
-       for (i = tab->n_redundant; i < tab->n_row; ++i) {
-               struct isl_tab_var *var;
-               var = var_from_row(ctx, tab, i);
-               if (!var->is_nonneg)
-                       continue;
-               if (sign_of_max(ctx, tab, var) == 0)
-                       close_row(ctx, tab, var);
-               else
-                       return 0;
+       for (;;) {
+               for (i = tab->n_redundant; i < tab->n_row; ++i) {
+                       struct isl_tab_var *var;
+                       var = isl_tab_var_from_row(tab, i);
+                       if (!var->is_nonneg)
+                               continue;
+                       if (sign_of_max(tab, var) != 0)
+                               return 0;
+                       close_row(tab, var);
+                       break;
+               }
                if (tab->n_dead == tab->n_col)
                        return 1;
+               if (i == tab->n_row)
+                       return 0;
        }
-       return 0;
 }
 
-static int sample_is_integer(struct isl_ctx *ctx, struct isl_tab *tab)
+int isl_tab_sample_is_integer(struct isl_tab *tab)
 {
        int i;
 
+       if (!tab)
+               return -1;
+
        for (i = 0; i < tab->n_var; ++i) {
                int row;
                if (!tab->var[i].is_row)
@@ -1146,13 +1250,12 @@ static int sample_is_integer(struct isl_ctx *ctx, struct isl_tab *tab)
        return 1;
 }
 
-static struct isl_vec *extract_integer_sample(struct isl_ctx *ctx,
-                                               struct isl_tab *tab)
+static struct isl_vec *extract_integer_sample(struct isl_tab *tab)
 {
        int i;
        struct isl_vec *vec;
 
-       vec = isl_vec_alloc(ctx, 1 + tab->n_var);
+       vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
        if (!vec)
                return NULL;
 
@@ -1170,8 +1273,7 @@ static struct isl_vec *extract_integer_sample(struct isl_ctx *ctx,
        return vec;
 }
 
-struct isl_vec *isl_tab_get_sample_value(struct isl_ctx *ctx,
-                                               struct isl_tab *tab)
+struct isl_vec *isl_tab_get_sample_value(struct isl_tab *tab)
 {
        int i;
        struct isl_vec *vec;
@@ -1180,7 +1282,7 @@ struct isl_vec *isl_tab_get_sample_value(struct isl_ctx *ctx,
        if (!tab)
                return NULL;
 
-       vec = isl_vec_alloc(ctx, 1 + tab->n_var);
+       vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
        if (!vec)
                return NULL;
 
@@ -1230,14 +1332,14 @@ struct isl_basic_map *isl_basic_map_update_from_tab(struct isl_basic_map *bmap,
                bmap = isl_basic_map_set_to_empty(bmap);
        else
                for (i = bmap->n_ineq - 1; i >= 0; --i) {
-                       if (isl_tab_is_equality(bmap->ctx, tab, n_eq + i))
+                       if (isl_tab_is_equality(tab, n_eq + i))
                                isl_basic_map_inequality_to_equality(bmap, i);
-                       else if (isl_tab_is_redundant(bmap->ctx, tab, n_eq + i))
+                       else if (isl_tab_is_redundant(tab, n_eq + i))
                                isl_basic_map_drop_inequality(bmap, i);
                }
        if (!tab->rational &&
-           !bmap->sample && sample_is_integer(bmap->ctx, tab))
-               bmap->sample = extract_integer_sample(bmap->ctx, tab);
+           !bmap->sample && isl_tab_sample_is_integer(tab))
+               bmap->sample = extract_integer_sample(tab);
        return bmap;
 }
 
@@ -1257,14 +1359,14 @@ struct isl_basic_set *isl_basic_set_update_from_tab(struct isl_basic_set *bset,
  * the resulting tableau is empty.
  * Otherwise, we know the value will be zero and we close the row.
  */
-static struct isl_tab *cut_to_hyperplane(struct isl_ctx *ctx,
-       struct isl_tab *tab, struct isl_tab_var *var)
+static struct isl_tab *cut_to_hyperplane(struct isl_tab *tab,
+       struct isl_tab_var *var)
 {
        unsigned r;
        isl_int *row;
        int sgn;
 
-       if (extend_cons(ctx, tab, 1) < 0)
+       if (isl_tab_extend_cons(tab, 1) < 0)
                goto error;
 
        r = tab->n_con;
@@ -1289,21 +1391,19 @@ static struct isl_tab *cut_to_hyperplane(struct isl_ctx *ctx,
 
        tab->n_row++;
        tab->n_con++;
-       push(ctx, tab, isl_tab_undo_allocate, &tab->con[r]);
+       isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->con[r]);
 
-       sgn = sign_of_max(ctx, tab, &tab->con[r]);
+       sgn = sign_of_max(tab, &tab->con[r]);
        if (sgn < 0)
-               mark_empty(ctx, tab);
-       else {
-               tab->con[r].is_nonneg = 1;
-               push(ctx, tab, isl_tab_undo_nonneg, &tab->con[r]);
-               /* sgn == 0 */
-               close_row(ctx, tab, &tab->con[r]);
-       }
+               return isl_tab_mark_empty(tab);
+       tab->con[r].is_nonneg = 1;
+       isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]);
+       /* sgn == 0 */
+       close_row(tab, &tab->con[r]);
 
        return tab;
 error:
-       isl_tab_free(ctx, tab);
+       isl_tab_free(tab);
        return NULL;
 }
 
@@ -1316,8 +1416,7 @@ error:
  * refers to r = r' - 1 by substituting this equality, effectively
  * subtracting the coefficient of the column from the constant.
  */
-struct isl_tab *isl_tab_relax(struct isl_ctx *ctx,
-       struct isl_tab *tab, int con)
+struct isl_tab *isl_tab_relax(struct isl_tab *tab, int con)
 {
        struct isl_tab_var *var;
        if (!tab)
@@ -1325,8 +1424,8 @@ struct isl_tab *isl_tab_relax(struct isl_ctx *ctx,
 
        var = &tab->con[con];
 
-       if (!var->is_row && !max_is_manifestly_unbounded(ctx, tab, var))
-               to_row(ctx, tab, var, 1);
+       if (!var->is_row && !max_is_manifestly_unbounded(tab, var))
+               to_row(tab, var, 1);
 
        if (var->is_row)
                isl_int_add(tab->mat->row[var->index][1],
@@ -1343,18 +1442,17 @@ struct isl_tab *isl_tab_relax(struct isl_ctx *ctx,
 
        }
 
-       push(ctx, tab, isl_tab_undo_relax, var);
+       isl_tab_push_var(tab, isl_tab_undo_relax, var);
 
        return tab;
 }
 
-struct isl_tab *isl_tab_select_facet(struct isl_ctx *ctx,
-       struct isl_tab *tab, int con)
+struct isl_tab *isl_tab_select_facet(struct isl_tab *tab, int con)
 {
        if (!tab)
                return NULL;
 
-       return cut_to_hyperplane(ctx, tab, &tab->con[con]);
+       return cut_to_hyperplane(tab, &tab->con[con]);
 }
 
 static int may_be_equality(struct isl_tab *tab, int row)
@@ -1382,8 +1480,7 @@ static int may_be_equality(struct isl_tab *tab, int row)
  * tableau is integer), then we restrict the value to being zero
  * by adding an opposite non-negative variable.
  */
-struct isl_tab *isl_tab_detect_equalities(struct isl_ctx *ctx,
-                               struct isl_tab *tab)
+struct isl_tab *isl_tab_detect_equalities(struct isl_tab *tab)
 {
        int i;
        unsigned n_marked;
@@ -1397,14 +1494,14 @@ struct isl_tab *isl_tab_detect_equalities(struct isl_ctx *ctx,
 
        n_marked = 0;
        for (i = tab->n_redundant; i < tab->n_row; ++i) {
-               struct isl_tab_var *var = var_from_row(ctx, tab, i);
+               struct isl_tab_var *var = isl_tab_var_from_row(tab, i);
                var->marked = !var->frozen && var->is_nonneg &&
                        may_be_equality(tab, i);
                if (var->marked)
                        n_marked++;
        }
        for (i = tab->n_dead; i < tab->n_col; ++i) {
-               struct isl_tab_var *var = var_from_col(ctx, tab, i);
+               struct isl_tab_var *var = var_from_col(tab, i);
                var->marked = !var->frozen && var->is_nonneg;
                if (var->marked)
                        n_marked++;
@@ -1412,13 +1509,13 @@ struct isl_tab *isl_tab_detect_equalities(struct isl_ctx *ctx,
        while (n_marked) {
                struct isl_tab_var *var;
                for (i = tab->n_redundant; i < tab->n_row; ++i) {
-                       var = var_from_row(ctx, tab, i);
+                       var = isl_tab_var_from_row(tab, i);
                        if (var->marked)
                                break;
                }
                if (i == tab->n_row) {
                        for (i = tab->n_dead; i < tab->n_col; ++i) {
-                               var = var_from_col(ctx, tab, i);
+                               var = var_from_col(tab, i);
                                if (var->marked)
                                        break;
                        }
@@ -1427,14 +1524,14 @@ struct isl_tab *isl_tab_detect_equalities(struct isl_ctx *ctx,
                }
                var->marked = 0;
                n_marked--;
-               if (sign_of_max(ctx, tab, var) == 0)
-                       close_row(ctx, tab, var);
-               else if (!tab->rational && !at_least_one(ctx, tab, var)) {
-                       tab = cut_to_hyperplane(ctx, tab, var);
-                       return isl_tab_detect_equalities(ctx, tab);
+               if (sign_of_max(tab, var) == 0)
+                       close_row(tab, var);
+               else if (!tab->rational && !at_least_one(tab, var)) {
+                       tab = cut_to_hyperplane(tab, var);
+                       return isl_tab_detect_equalities(tab);
                }
                for (i = tab->n_redundant; i < tab->n_row; ++i) {
-                       var = var_from_row(ctx, tab, i);
+                       var = isl_tab_var_from_row(tab, i);
                        if (!var->marked)
                                continue;
                        if (may_be_equality(tab, i))
@@ -1460,8 +1557,7 @@ struct isl_tab *isl_tab_detect_equalities(struct isl_ctx *ctx,
  * If not, we mark the row as being redundant (assuming it hasn't
  * been detected as being obviously redundant in the mean time).
  */
-struct isl_tab *isl_tab_detect_redundant(struct isl_ctx *ctx,
-                               struct isl_tab *tab)
+struct isl_tab *isl_tab_detect_redundant(struct isl_tab *tab)
 {
        int i;
        unsigned n_marked;
@@ -1475,28 +1571,28 @@ struct isl_tab *isl_tab_detect_redundant(struct isl_ctx *ctx,
 
        n_marked = 0;
        for (i = tab->n_redundant; i < tab->n_row; ++i) {
-               struct isl_tab_var *var = var_from_row(ctx, tab, i);
+               struct isl_tab_var *var = isl_tab_var_from_row(tab, i);
                var->marked = !var->frozen && var->is_nonneg;
                if (var->marked)
                        n_marked++;
        }
        for (i = tab->n_dead; i < tab->n_col; ++i) {
-               struct isl_tab_var *var = var_from_col(ctx, tab, i);
+               struct isl_tab_var *var = var_from_col(tab, i);
                var->marked = !var->frozen && var->is_nonneg &&
-                       !min_is_manifestly_unbounded(ctx, tab, var);
+                       !min_is_manifestly_unbounded(tab, var);
                if (var->marked)
                        n_marked++;
        }
        while (n_marked) {
                struct isl_tab_var *var;
                for (i = tab->n_redundant; i < tab->n_row; ++i) {
-                       var = var_from_row(ctx, tab, i);
+                       var = isl_tab_var_from_row(tab, i);
                        if (var->marked)
                                break;
                }
                if (i == tab->n_row) {
                        for (i = tab->n_dead; i < tab->n_col; ++i) {
-                               var = var_from_col(ctx, tab, i);
+                               var = var_from_col(tab, i);
                                if (var->marked)
                                        break;
                        }
@@ -1505,15 +1601,15 @@ struct isl_tab *isl_tab_detect_redundant(struct isl_ctx *ctx,
                }
                var->marked = 0;
                n_marked--;
-               if ((tab->rational ? (sign_of_min(ctx, tab, var) >= 0)
-                                  : !min_at_most_neg_one(ctx, tab, var)) &&
+               if ((tab->rational ? (sign_of_min(tab, var) >= 0)
+                                  : !isl_tab_min_at_most_neg_one(tab, var)) &&
                    !var->is_redundant)
-                       mark_redundant(ctx, tab, var->index);
+                       isl_tab_mark_redundant(tab, var->index);
                for (i = tab->n_dead; i < tab->n_col; ++i) {
-                       var = var_from_col(ctx, tab, i);
+                       var = var_from_col(tab, i);
                        if (!var->marked)
                                continue;
-                       if (!min_is_manifestly_unbounded(ctx, tab, var))
+                       if (!min_is_manifestly_unbounded(tab, var))
                                continue;
                        var->marked = 0;
                        n_marked--;
@@ -1523,7 +1619,7 @@ struct isl_tab *isl_tab_detect_redundant(struct isl_ctx *ctx,
        return tab;
 }
 
-int isl_tab_is_equality(struct isl_ctx *ctx, struct isl_tab *tab, int con)
+int isl_tab_is_equality(struct isl_tab *tab, int con)
 {
        int row;
 
@@ -1550,7 +1646,7 @@ int isl_tab_is_equality(struct isl_ctx *ctx, struct isl_tab *tab, int con)
  * The return value reflects the nature of the result (empty, unbounded,
  * minmimal value returned in *opt).
  */
-enum isl_lp_result isl_tab_min(struct isl_ctx *ctx, struct isl_tab *tab,
+enum isl_lp_result isl_tab_min(struct isl_tab *tab,
        isl_int *f, isl_int denom, isl_int *opt, isl_int *opt_denom,
        unsigned flags)
 {
@@ -1562,8 +1658,8 @@ enum isl_lp_result isl_tab_min(struct isl_ctx *ctx, struct isl_tab *tab,
        if (tab->empty)
                return isl_lp_empty;
 
-       snap = isl_tab_snap(ctx, tab);
-       r = add_row(ctx, tab, f);
+       snap = isl_tab_snap(tab);
+       r = isl_tab_add_row(tab, f);
        if (r < 0)
                return isl_lp_error;
        var = &tab->con[r];
@@ -1571,22 +1667,22 @@ enum isl_lp_result isl_tab_min(struct isl_ctx *ctx, struct isl_tab *tab,
                    tab->mat->row[var->index][0], denom);
        for (;;) {
                int row, col;
-               find_pivot(ctx, tab, var, var, -1, &row, &col);
+               find_pivot(tab, var, var, -1, &row, &col);
                if (row == var->index) {
                        res = isl_lp_unbounded;
                        break;
                }
                if (row == -1)
                        break;
-               pivot(ctx, tab, row, col);
+               isl_tab_pivot(tab, row, col);
        }
-       if (isl_tab_rollback(ctx, tab, snap) < 0)
+       if (isl_tab_rollback(tab, snap) < 0)
                return isl_lp_error;
        if (ISL_FL_ISSET(flags, ISL_TAB_SAVE_DUAL)) {
                int i;
 
                isl_vec_free(tab->dual);
-               tab->dual = isl_vec_alloc(ctx, 1 + tab->n_con);
+               tab->dual = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_con);
                if (!tab->dual)
                        return isl_lp_error;
                isl_int_set(tab->dual->el[0], tab->mat->row[var->index][0]);
@@ -1611,7 +1707,7 @@ enum isl_lp_result isl_tab_min(struct isl_ctx *ctx, struct isl_tab *tab,
        return res;
 }
 
-int isl_tab_is_redundant(struct isl_ctx *ctx, struct isl_tab *tab, int con)
+int isl_tab_is_redundant(struct isl_tab *tab, int con)
 {
        int row;
        unsigned n_col;
@@ -1628,7 +1724,7 @@ int isl_tab_is_redundant(struct isl_ctx *ctx, struct isl_tab *tab, int con)
 /* Take a snapshot of the tableau that can be restored by s call to
  * isl_tab_rollback.
  */
-struct isl_tab_undo *isl_tab_snap(struct isl_ctx *ctx, struct isl_tab *tab)
+struct isl_tab_undo *isl_tab_snap(struct isl_tab *tab)
 {
        if (!tab)
                return NULL;
@@ -1638,11 +1734,10 @@ struct isl_tab_undo *isl_tab_snap(struct isl_ctx *ctx, struct isl_tab *tab)
 
 /* Undo the operation performed by isl_tab_relax.
  */
-static void unrelax(struct isl_ctx *ctx,
-       struct isl_tab *tab, struct isl_tab_var *var)
+static void unrelax(struct isl_tab *tab, struct isl_tab_var *var)
 {
-       if (!var->is_row && !max_is_manifestly_unbounded(ctx, tab, var))
-               to_row(ctx, tab, var, 1);
+       if (!var->is_row && !max_is_manifestly_unbounded(tab, var))
+               to_row(tab, var, 1);
 
        if (var->is_row)
                isl_int_sub(tab->mat->row[var->index][1],
@@ -1660,44 +1755,122 @@ static void unrelax(struct isl_ctx *ctx,
        }
 }
 
-static void perform_undo(struct isl_ctx *ctx, struct isl_tab *tab,
-       struct isl_tab_undo *undo)
+static void perform_undo_var(struct isl_tab *tab, struct isl_tab_undo *undo)
 {
+       struct isl_tab_var *var = var_from_index(tab, undo->u.var_index);
        switch(undo->type) {
-       case isl_tab_undo_empty:
-               tab->empty = 0;
-               break;
        case isl_tab_undo_nonneg:
-               undo->var->is_nonneg = 0;
+               var->is_nonneg = 0;
                break;
        case isl_tab_undo_redundant:
-               undo->var->is_redundant = 0;
+               var->is_redundant = 0;
                tab->n_redundant--;
                break;
        case isl_tab_undo_zero:
-               undo->var->is_zero = 0;
+               var->is_zero = 0;
                tab->n_dead--;
                break;
        case isl_tab_undo_allocate:
-               if (!undo->var->is_row) {
-                       if (max_is_manifestly_unbounded(ctx, tab, undo->var))
-                               to_row(ctx, tab, undo->var, -1);
+               if (!var->is_row) {
+                       if (!max_is_manifestly_unbounded(tab, var))
+                               to_row(tab, var, 1);
+                       else if (!min_is_manifestly_unbounded(tab, var))
+                               to_row(tab, var, -1);
                        else
-                               to_row(ctx, tab, undo->var, 1);
+                               to_row(tab, var, 0);
                }
-               drop_row(ctx, tab, undo->var->index);
+               drop_row(tab, var->index);
+               break;
+       case isl_tab_undo_relax:
+               unrelax(tab, var);
+               break;
+       }
+}
+
+/* Restore the tableau to the state where the basic variables
+ * are those in "col_var".
+ * We first construct a list of variables that are currently in
+ * the basis, but shouldn't.  Then we iterate over all variables
+ * that should be in the basis and for each one that is currently
+ * not in the basis, we exchange it with one of the elements of the
+ * list constructed before.
+ * We can always find an appropriate variable to pivot with because
+ * the current basis is mapped to the old basis by a non-singular
+ * matrix and so we can never end up with a zero row.
+ */
+static int restore_basis(struct isl_tab *tab, int *col_var)
+{
+       int i, j;
+       int n_extra = 0;
+       int *extra = NULL;      /* current columns that contain bad stuff */
+       unsigned off = 2;
+
+       extra = isl_alloc_array(tab->mat->ctx, int, tab->n_col);
+       if (!extra)
+               goto error;
+       for (i = 0; i < tab->n_col; ++i) {
+               for (j = 0; j < tab->n_col; ++j)
+                       if (tab->col_var[i] == col_var[j])
+                               break;
+               if (j < tab->n_col)
+                       continue;
+               extra[n_extra++] = i;
+       }
+       for (i = 0; i < tab->n_col && n_extra > 0; ++i) {
+               struct isl_tab_var *var;
+               int row;
+
+               for (j = 0; j < tab->n_col; ++j)
+                       if (col_var[i] == tab->col_var[j])
+                               break;
+               if (j < tab->n_col)
+                       continue;
+               var = var_from_index(tab, col_var[i]);
+               row = var->index;
+               for (j = 0; j < n_extra; ++j)
+                       if (!isl_int_is_zero(tab->mat->row[row][off+extra[j]]))
+                               break;
+               isl_assert(tab->mat->ctx, j < n_extra, goto error);
+               isl_tab_pivot(tab, row, extra[j]);
+               extra[j] = extra[--n_extra];
+       }
+
+       free(extra);
+       free(col_var);
+       return 0;
+error:
+       free(extra);
+       free(col_var);
+       return -1;
+}
+
+static int perform_undo(struct isl_tab *tab, struct isl_tab_undo *undo)
+{
+       switch (undo->type) {
+       case isl_tab_undo_empty:
+               tab->empty = 0;
                break;
+       case isl_tab_undo_nonneg:
+       case isl_tab_undo_redundant:
+       case isl_tab_undo_zero:
+       case isl_tab_undo_allocate:
        case isl_tab_undo_relax:
-               unrelax(ctx, tab, undo->var);
+               perform_undo_var(tab, undo);
+               break;
+       case isl_tab_undo_saved_basis:
+               if (restore_basis(tab, undo->u.col_var) < 0)
+                       return -1;
                break;
+       default:
+               isl_assert(tab->mat->ctx, 0, return -1);
        }
+       return 0;
 }
 
 /* Return the tableau to the state it was in when the snapshot "snap"
  * was taken.
  */
-int isl_tab_rollback(struct isl_ctx *ctx, struct isl_tab *tab,
-       struct isl_tab_undo *snap)
+int isl_tab_rollback(struct isl_tab *tab, struct isl_tab_undo *snap)
 {
        struct isl_tab_undo *undo, *next;
 
@@ -1709,7 +1882,11 @@ int isl_tab_rollback(struct isl_ctx *ctx, struct isl_tab *tab,
                next = undo->next;
                if (undo == snap)
                        break;
-               perform_undo(ctx, tab, undo);
+               if (perform_undo(tab, undo) < 0) {
+                       free_undo(tab);
+                       tab->in_undo = 0;
+                       return -1;
+               }
                free(undo);
        }
        tab->in_undo = 0;
@@ -1729,8 +1906,7 @@ int isl_tab_rollback(struct isl_ctx *ctx, struct isl_tab *tab,
  * of the tableau, then the inequality is adjacent (but opposite)
  * to the inequality r'.
  */
-static enum isl_ineq_type separation_type(struct isl_ctx *ctx,
-       struct isl_tab *tab, unsigned row)
+static enum isl_ineq_type separation_type(struct isl_tab *tab, unsigned row)
 {
        int pos;
 
@@ -1765,8 +1941,7 @@ static enum isl_ineq_type separation_type(struct isl_ctx *ctx,
  *     isl_ineq_adj_eq:        adjacent to an equality
  *     isl_ineq_adj_ineq:      adjacent to an inequality.
  */
-enum isl_ineq_type isl_tab_ineq_type(struct isl_ctx *ctx, struct isl_tab *tab,
-       isl_int *ineq)
+enum isl_ineq_type isl_tab_ineq_type(struct isl_tab *tab, isl_int *ineq)
 {
        enum isl_ineq_type type = isl_ineq_error;
        struct isl_tab_undo *snap = NULL;
@@ -1776,42 +1951,41 @@ enum isl_ineq_type isl_tab_ineq_type(struct isl_ctx *ctx, struct isl_tab *tab,
        if (!tab)
                return isl_ineq_error;
 
-       if (extend_cons(ctx, tab, 1) < 0)
+       if (isl_tab_extend_cons(tab, 1) < 0)
                return isl_ineq_error;
 
-       snap = isl_tab_snap(ctx, tab);
+       snap = isl_tab_snap(tab);
 
-       con = add_row(ctx, tab, ineq);
+       con = isl_tab_add_row(tab, ineq);
        if (con < 0)
                goto error;
 
        row = tab->con[con].index;
-       if (is_redundant(ctx, tab, row))
+       if (isl_tab_row_is_redundant(tab, row))
                type = isl_ineq_redundant;
        else if (isl_int_is_neg(tab->mat->row[row][1]) &&
                 (tab->rational ||
                    isl_int_abs_ge(tab->mat->row[row][1],
                                   tab->mat->row[row][0]))) {
-               if (at_least_zero(ctx, tab, &tab->con[con]))
+               if (at_least_zero(tab, &tab->con[con]))
                        type = isl_ineq_cut;
                else
-                       type = separation_type(ctx, tab, row);
-       } else if (tab->rational ? (sign_of_min(ctx, tab, &tab->con[con]) < 0)
-                            : min_at_most_neg_one(ctx, tab, &tab->con[con]))
+                       type = separation_type(tab, row);
+       } else if (tab->rational ? (sign_of_min(tab, &tab->con[con]) < 0)
+                            : isl_tab_min_at_most_neg_one(tab, &tab->con[con]))
                type = isl_ineq_cut;
        else
                type = isl_ineq_redundant;
 
-       if (isl_tab_rollback(ctx, tab, snap))
+       if (isl_tab_rollback(tab, snap))
                return isl_ineq_error;
        return type;
 error:
-       isl_tab_rollback(ctx, tab, snap);
+       isl_tab_rollback(tab, snap);
        return isl_ineq_error;
 }
 
-void isl_tab_dump(struct isl_ctx *ctx, struct isl_tab *tab,
-                               FILE *out, int indent)
+void isl_tab_dump(struct isl_tab *tab, FILE *out, int indent)
 {
        unsigned r, c;
        int i;
@@ -1830,7 +2004,9 @@ void isl_tab_dump(struct isl_ctx *ctx, struct isl_tab *tab,
        fprintf(out, "%*s[", indent, "");
        for (i = 0; i < tab->n_var; ++i) {
                if (i)
-                       fprintf(out, ", ");
+                       fprintf(out, (i == tab->n_param ||
+                                     i == tab->n_var - tab->n_div) ? "; "
+                                                                   : ", ");
                fprintf(out, "%c%d%s", tab->var[i].is_row ? 'r' : 'c',
                                        tab->var[i].index,
                                        tab->var[i].is_zero ? " [=0]" :
@@ -1852,7 +2028,7 @@ void isl_tab_dump(struct isl_ctx *ctx, struct isl_tab *tab,
                if (i)
                        fprintf(out, ", ");
                fprintf(out, "r%d: %d%s", i, tab->row_var[i],
-                   var_from_row(ctx, tab, i)->is_nonneg ? " [>=0]" : "");
+                   isl_tab_var_from_row(tab, i)->is_nonneg ? " [>=0]" : "");
        }
        fprintf(out, "]\n");
        fprintf(out, "%*s[", indent, "");
@@ -1860,14 +2036,14 @@ void isl_tab_dump(struct isl_ctx *ctx, struct isl_tab *tab,
                if (i)
                        fprintf(out, ", ");
                fprintf(out, "c%d: %d%s", i, tab->col_var[i],
-                   var_from_col(ctx, tab, i)->is_nonneg ? " [>=0]" : "");
+                   var_from_col(tab, i)->is_nonneg ? " [>=0]" : "");
        }
        fprintf(out, "]\n");
        r = tab->mat->n_row;
        tab->mat->n_row = tab->n_row;
        c = tab->mat->n_col;
        tab->mat->n_col = 2 + tab->n_col;
-       isl_mat_dump(ctx, tab->mat, out, indent);
+       isl_mat_dump(tab->mat, out, indent);
        tab->mat->n_row = r;
        tab->mat->n_col = c;
 }