#include "isl_mat.h"
#include "isl_map_private.h"
#include "isl_tab.h"
+#include "isl_seq.h"
/*
* The implementation of tableaus in this file was inspired by Section 8
tab->var[i].is_zero = 0;
tab->var[i].is_redundant = 0;
tab->var[i].frozen = 0;
+ tab->var[i].negated = 0;
tab->col_var[i] = i;
}
tab->n_row = 0;
tab->empty = 0;
tab->in_undo = 0;
tab->M = M;
+ tab->cone = 0;
tab->bottom.type = isl_tab_undo_bottom;
tab->bottom.next = NULL;
tab->top = &tab->bottom;
+
+ tab->n_zero = 0;
+ tab->n_unbounded = 0;
+ tab->basis = NULL;
+
return tab;
error:
isl_tab_free(tab);
int isl_tab_extend_cons(struct isl_tab *tab, unsigned n_new)
{
unsigned off = 2 + tab->M;
+
+ if (!tab)
+ return -1;
+
if (tab->max_con < tab->n_con + n_new) {
struct isl_tab_var *con;
if (!tab->mat)
return -1;
p = isl_realloc_array(tab->mat->ctx, tab->col_var,
- int, tab->mat->n_col);
+ int, tab->n_col + n_new);
if (!p)
return -1;
tab->col_var = p;
free(tab->row_var);
free(tab->col_var);
free(tab->row_sign);
+ isl_mat_free(tab->samples);
+ free(tab->sample_index);
+ isl_mat_free(tab->basis);
free(tab);
}
{
int i;
struct isl_tab *dup;
+ unsigned off;
if (!tab)
return NULL;
+ off = 2 + tab->M;
dup = isl_calloc_type(tab->ctx, struct isl_tab);
if (!dup)
return NULL;
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);
+ dup->col_var = isl_alloc_array(tab->ctx, int, tab->mat->n_col - off);
if (!dup->col_var)
goto error;
- for (i = 0; i < tab->n_var; ++i)
+ for (i = 0; i < tab->n_col; ++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)
for (i = 0; i < tab->n_row; ++i)
dup->row_sign[i] = tab->row_sign[i];
}
+ if (tab->samples) {
+ dup->samples = isl_mat_dup(tab->samples);
+ if (!dup->samples)
+ goto error;
+ dup->sample_index = isl_alloc_array(tab->mat->ctx, int,
+ tab->samples->n_row);
+ if (!dup->sample_index)
+ goto error;
+ dup->n_sample = tab->n_sample;
+ dup->n_outside = tab->n_outside;
+ }
dup->n_row = tab->n_row;
dup->n_con = tab->n_con;
dup->n_eq = tab->n_eq;
dup->need_undo = 0;
dup->in_undo = 0;
dup->M = tab->M;
+ tab->cone = tab->cone;
dup->bottom.type = isl_tab_undo_bottom;
dup->bottom.next = NULL;
dup->top = &dup->bottom;
+
+ dup->n_zero = tab->n_zero;
+ dup->n_unbounded = tab->n_unbounded;
+ dup->basis = isl_mat_dup(tab->basis);
+
return dup;
error:
isl_tab_free(dup);
return NULL;
}
+/* Construct the coefficient matrix of the product tableau
+ * of two tableaus.
+ * mat{1,2} is the coefficient matrix of tableau {1,2}
+ * row{1,2} is the number of rows in tableau {1,2}
+ * col{1,2} is the number of columns in tableau {1,2}
+ * off is the offset to the coefficient column (skipping the
+ * denominator, the constant term and the big parameter if any)
+ * r{1,2} is the number of redundant rows in tableau {1,2}
+ * d{1,2} is the number of dead columns in tableau {1,2}
+ *
+ * The order of the rows and columns in the result is as explained
+ * in isl_tab_product.
+ */
+static struct isl_mat *tab_mat_product(struct isl_mat *mat1,
+ struct isl_mat *mat2, unsigned row1, unsigned row2,
+ unsigned col1, unsigned col2,
+ unsigned off, unsigned r1, unsigned r2, unsigned d1, unsigned d2)
+{
+ int i;
+ struct isl_mat *prod;
+ unsigned n;
+
+ prod = isl_mat_alloc(mat1->ctx, mat1->n_row + mat2->n_row,
+ off + col1 + col2);
+
+ n = 0;
+ for (i = 0; i < r1; ++i) {
+ isl_seq_cpy(prod->row[n + i], mat1->row[i], off + d1);
+ isl_seq_clr(prod->row[n + i] + off + d1, d2);
+ isl_seq_cpy(prod->row[n + i] + off + d1 + d2,
+ mat1->row[i] + off + d1, col1 - d1);
+ isl_seq_clr(prod->row[n + i] + off + col1 + d1, col2 - d2);
+ }
+
+ n += r1;
+ for (i = 0; i < r2; ++i) {
+ isl_seq_cpy(prod->row[n + i], mat2->row[i], off);
+ isl_seq_clr(prod->row[n + i] + off, d1);
+ isl_seq_cpy(prod->row[n + i] + off + d1,
+ mat2->row[i] + off, d2);
+ isl_seq_clr(prod->row[n + i] + off + d1 + d2, col1 - d1);
+ isl_seq_cpy(prod->row[n + i] + off + col1 + d1,
+ mat2->row[i] + off + d2, col2 - d2);
+ }
+
+ n += r2;
+ for (i = 0; i < row1 - r1; ++i) {
+ isl_seq_cpy(prod->row[n + i], mat1->row[r1 + i], off + d1);
+ isl_seq_clr(prod->row[n + i] + off + d1, d2);
+ isl_seq_cpy(prod->row[n + i] + off + d1 + d2,
+ mat1->row[r1 + i] + off + d1, col1 - d1);
+ isl_seq_clr(prod->row[n + i] + off + col1 + d1, col2 - d2);
+ }
+
+ n += row1 - r1;
+ for (i = 0; i < row2 - r2; ++i) {
+ isl_seq_cpy(prod->row[n + i], mat2->row[r2 + i], off);
+ isl_seq_clr(prod->row[n + i] + off, d1);
+ isl_seq_cpy(prod->row[n + i] + off + d1,
+ mat2->row[r2 + i] + off, d2);
+ isl_seq_clr(prod->row[n + i] + off + d1 + d2, col1 - d1);
+ isl_seq_cpy(prod->row[n + i] + off + col1 + d1,
+ mat2->row[r2 + i] + off + d2, col2 - d2);
+ }
+
+ return prod;
+}
+
+/* Update the row or column index of a variable that corresponds
+ * to a variable in the first input tableau.
+ */
+static void update_index1(struct isl_tab_var *var,
+ unsigned r1, unsigned r2, unsigned d1, unsigned d2)
+{
+ if (var->index == -1)
+ return;
+ if (var->is_row && var->index >= r1)
+ var->index += r2;
+ if (!var->is_row && var->index >= d1)
+ var->index += d2;
+}
+
+/* Update the row or column index of a variable that corresponds
+ * to a variable in the second input tableau.
+ */
+static void update_index2(struct isl_tab_var *var,
+ unsigned row1, unsigned col1,
+ unsigned r1, unsigned r2, unsigned d1, unsigned d2)
+{
+ if (var->index == -1)
+ return;
+ if (var->is_row) {
+ if (var->index < r2)
+ var->index += r1;
+ else
+ var->index += row1;
+ } else {
+ if (var->index < d2)
+ var->index += d1;
+ else
+ var->index += col1;
+ }
+}
+
+/* Create a tableau that represents the Cartesian product of the sets
+ * represented by tableaus tab1 and tab2.
+ * The order of the rows in the product is
+ * - redundant rows of tab1
+ * - redundant rows of tab2
+ * - non-redundant rows of tab1
+ * - non-redundant rows of tab2
+ * The order of the columns is
+ * - denominator
+ * - constant term
+ * - coefficient of big parameter, if any
+ * - dead columns of tab1
+ * - dead columns of tab2
+ * - live columns of tab1
+ * - live columns of tab2
+ * The order of the variables and the constraints is a concatenation
+ * of order in the two input tableaus.
+ */
+struct isl_tab *isl_tab_product(struct isl_tab *tab1, struct isl_tab *tab2)
+{
+ int i;
+ struct isl_tab *prod;
+ unsigned off;
+ unsigned r1, r2, d1, d2;
+
+ if (!tab1 || !tab2)
+ return NULL;
+
+ isl_assert(tab1->mat->ctx, tab1->M == tab2->M, return NULL);
+ isl_assert(tab1->mat->ctx, tab1->rational == tab2->rational, return NULL);
+ isl_assert(tab1->mat->ctx, tab1->cone == tab2->cone, return NULL);
+ isl_assert(tab1->mat->ctx, !tab1->row_sign, return NULL);
+ isl_assert(tab1->mat->ctx, !tab2->row_sign, return NULL);
+ isl_assert(tab1->mat->ctx, tab1->n_param == 0, return NULL);
+ isl_assert(tab1->mat->ctx, tab2->n_param == 0, return NULL);
+ isl_assert(tab1->mat->ctx, tab1->n_div == 0, return NULL);
+ isl_assert(tab1->mat->ctx, tab2->n_div == 0, return NULL);
+
+ off = 2 + tab1->M;
+ r1 = tab1->n_redundant;
+ r2 = tab2->n_redundant;
+ d1 = tab1->n_dead;
+ d2 = tab2->n_dead;
+ prod = isl_calloc_type(tab1->mat->ctx, struct isl_tab);
+ if (!prod)
+ return NULL;
+ prod->mat = tab_mat_product(tab1->mat, tab2->mat,
+ tab1->n_row, tab2->n_row,
+ tab1->n_col, tab2->n_col, off, r1, r2, d1, d2);
+ if (!prod->mat)
+ goto error;
+ prod->var = isl_alloc_array(tab1->mat->ctx, struct isl_tab_var,
+ tab1->max_var + tab2->max_var);
+ if (!prod->var)
+ goto error;
+ for (i = 0; i < tab1->n_var; ++i) {
+ prod->var[i] = tab1->var[i];
+ update_index1(&prod->var[i], r1, r2, d1, d2);
+ }
+ for (i = 0; i < tab2->n_var; ++i) {
+ prod->var[tab1->n_var + i] = tab2->var[i];
+ update_index2(&prod->var[tab1->n_var + i],
+ tab1->n_row, tab1->n_col,
+ r1, r2, d1, d2);
+ }
+ prod->con = isl_alloc_array(tab1->mat->ctx, struct isl_tab_var,
+ tab1->max_con + tab2->max_con);
+ if (!prod->con)
+ goto error;
+ for (i = 0; i < tab1->n_con; ++i) {
+ prod->con[i] = tab1->con[i];
+ update_index1(&prod->con[i], r1, r2, d1, d2);
+ }
+ for (i = 0; i < tab2->n_con; ++i) {
+ prod->con[tab1->n_con + i] = tab2->con[i];
+ update_index2(&prod->con[tab1->n_con + i],
+ tab1->n_row, tab1->n_col,
+ r1, r2, d1, d2);
+ }
+ prod->col_var = isl_alloc_array(tab1->mat->ctx, int,
+ tab1->n_col + tab2->n_col);
+ if (!prod->col_var)
+ goto error;
+ for (i = 0; i < tab1->n_col; ++i) {
+ int pos = i < d1 ? i : i + d2;
+ prod->col_var[pos] = tab1->col_var[i];
+ }
+ for (i = 0; i < tab2->n_col; ++i) {
+ int pos = i < d2 ? d1 + i : tab1->n_col + i;
+ int t = tab2->col_var[i];
+ if (t >= 0)
+ t += tab1->n_var;
+ else
+ t -= tab1->n_con;
+ prod->col_var[pos] = t;
+ }
+ prod->row_var = isl_alloc_array(tab1->mat->ctx, int,
+ tab1->mat->n_row + tab2->mat->n_row);
+ if (!prod->row_var)
+ goto error;
+ for (i = 0; i < tab1->n_row; ++i) {
+ int pos = i < r1 ? i : i + r2;
+ prod->row_var[pos] = tab1->row_var[i];
+ }
+ for (i = 0; i < tab2->n_row; ++i) {
+ int pos = i < r2 ? r1 + i : tab1->n_row + i;
+ int t = tab2->row_var[i];
+ if (t >= 0)
+ t += tab1->n_var;
+ else
+ t -= tab1->n_con;
+ prod->row_var[pos] = t;
+ }
+ prod->samples = NULL;
+ prod->sample_index = NULL;
+ prod->n_row = tab1->n_row + tab2->n_row;
+ prod->n_con = tab1->n_con + tab2->n_con;
+ prod->n_eq = 0;
+ prod->max_con = tab1->max_con + tab2->max_con;
+ prod->n_col = tab1->n_col + tab2->n_col;
+ prod->n_var = tab1->n_var + tab2->n_var;
+ prod->max_var = tab1->max_var + tab2->max_var;
+ prod->n_param = 0;
+ prod->n_div = 0;
+ prod->n_dead = tab1->n_dead + tab2->n_dead;
+ prod->n_redundant = tab1->n_redundant + tab2->n_redundant;
+ prod->rational = tab1->rational;
+ prod->empty = tab1->empty || tab2->empty;
+ prod->need_undo = 0;
+ prod->in_undo = 0;
+ prod->M = tab1->M;
+ prod->cone = tab1->cone;
+ prod->bottom.type = isl_tab_undo_bottom;
+ prod->bottom.next = NULL;
+ prod->top = &prod->bottom;
+
+ prod->n_zero = 0;
+ prod->n_unbounded = 0;
+ prod->basis = NULL;
+
+ return prod;
+error:
+ isl_tab_free(prod);
+ return NULL;
+}
+
static struct isl_tab_var *var_from_index(struct isl_tab *tab, int i)
{
if (i >= 0)
tab->row_sign[row2] = t;
}
-static void push_union(struct isl_tab *tab,
+static int push_union(struct isl_tab *tab,
+ enum isl_tab_undo_type type, union isl_tab_undo_val u) WARN_UNUSED;
+static int 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;
+ return 0;
undo = isl_alloc_type(tab->mat->ctx, struct isl_tab_undo);
- if (!undo) {
- free_undo(tab);
- tab->top = NULL;
- return;
- }
+ if (!undo)
+ return -1;
undo->type = type;
undo->u = u;
undo->next = tab->top;
tab->top = undo;
+
+ return 0;
}
-void isl_tab_push_var(struct isl_tab *tab,
+int 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;
u.var_index = tab->row_var[var->index];
else
u.var_index = tab->col_var[var->index];
- push_union(tab, type, u);
+ return push_union(tab, type, u);
}
-void isl_tab_push(struct isl_tab *tab, enum isl_tab_undo_type type)
+int 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);
+ return 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 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;
- }
+ if (!u.col_var)
+ return -1;
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);
+ return push_union(tab, isl_tab_undo_saved_basis, u);
+}
+
+int isl_tab_push_callback(struct isl_tab *tab, struct isl_tab_callback *callback)
+{
+ union isl_tab_undo_val u;
+ u.callback = callback;
+ return push_union(tab, isl_tab_undo_callback, u);
+}
+
+struct isl_tab *isl_tab_init_samples(struct isl_tab *tab)
+{
+ if (!tab)
+ return NULL;
+
+ tab->n_sample = 0;
+ tab->n_outside = 0;
+ tab->samples = isl_mat_alloc(tab->mat->ctx, 1, 1 + tab->n_var);
+ if (!tab->samples)
+ goto error;
+ tab->sample_index = isl_alloc_array(tab->mat->ctx, int, 1);
+ if (!tab->sample_index)
+ goto error;
+ return tab;
+error:
+ isl_tab_free(tab);
+ return NULL;
+}
+
+struct isl_tab *isl_tab_add_sample(struct isl_tab *tab,
+ __isl_take isl_vec *sample)
+{
+ if (!tab || !sample)
+ goto error;
+
+ if (tab->n_sample + 1 > tab->samples->n_row) {
+ int *t = isl_realloc_array(tab->mat->ctx,
+ tab->sample_index, int, tab->n_sample + 1);
+ if (!t)
+ goto error;
+ tab->sample_index = t;
+ }
+
+ tab->samples = isl_mat_extend(tab->samples,
+ tab->n_sample + 1, tab->samples->n_col);
+ if (!tab->samples)
+ goto error;
+
+ isl_seq_cpy(tab->samples->row[tab->n_sample], sample->el, sample->size);
+ isl_vec_free(sample);
+ tab->sample_index[tab->n_sample] = tab->n_sample;
+ tab->n_sample++;
+
+ return tab;
+error:
+ isl_vec_free(sample);
+ isl_tab_free(tab);
+ return NULL;
+}
+
+struct isl_tab *isl_tab_drop_sample(struct isl_tab *tab, int s)
+{
+ if (s != tab->n_outside) {
+ int t = tab->sample_index[tab->n_outside];
+ tab->sample_index[tab->n_outside] = tab->sample_index[s];
+ tab->sample_index[s] = t;
+ isl_mat_swap_rows(tab->samples, tab->n_outside, s);
+ }
+ tab->n_outside++;
+ if (isl_tab_push(tab, isl_tab_undo_drop_sample) < 0) {
+ isl_tab_free(tab);
+ return NULL;
+ }
+
+ return tab;
+}
+
+/* Record the current number of samples so that we can remove newer
+ * samples during a rollback.
+ */
+int isl_tab_save_samples(struct isl_tab *tab)
+{
+ union isl_tab_undo_val u;
+
+ if (!tab)
+ return -1;
+
+ u.n = tab->n_sample;
+ return push_union(tab, isl_tab_undo_saved_samples, u);
}
/* Mark row with index "row" as being redundant.
{
struct isl_tab_var *var = isl_tab_var_from_row(tab, row);
var->is_redundant = 1;
- isl_assert(tab->mat->ctx, row >= tab->n_redundant, return);
+ isl_assert(tab->mat->ctx, row >= tab->n_redundant, return -1);
if (tab->need_undo || tab->row_var[row] >= 0) {
if (tab->row_var[row] >= 0 && !var->is_nonneg) {
var->is_nonneg = 1;
- isl_tab_push_var(tab, isl_tab_undo_nonneg, var);
+ if (isl_tab_push_var(tab, isl_tab_undo_nonneg, var) < 0)
+ return -1;
}
if (row != tab->n_redundant)
swap_rows(tab, row, tab->n_redundant);
- isl_tab_push_var(tab, isl_tab_undo_redundant, var);
tab->n_redundant++;
- return 0;
+ return isl_tab_push_var(tab, isl_tab_undo_redundant, var);
} else {
if (row != tab->n_row - 1)
swap_rows(tab, row, tab->n_row - 1);
struct isl_tab *isl_tab_mark_empty(struct isl_tab *tab)
{
+ if (!tab)
+ return NULL;
if (!tab->empty && tab->need_undo)
- isl_tab_push(tab, isl_tab_undo_empty);
+ if (isl_tab_push(tab, isl_tab_undo_empty) < 0) {
+ isl_tab_free(tab);
+ return NULL;
+ }
tab->empty = 1;
return 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)
*
*/
-void isl_tab_pivot(struct isl_tab *tab, int row, int col)
+int isl_tab_pivot(struct isl_tab *tab, int row, int col)
{
int i, j;
int sgn;
isl_int_neg(mat->row[row][1 + j], mat->row[row][1 + j]);
}
if (!isl_int_is_one(mat->row[row][0]))
- isl_seq_normalize(mat->row[row], off + tab->n_col);
+ isl_seq_normalize(mat->ctx, mat->row[row], off + tab->n_col);
for (i = 0; i < tab->n_row; ++i) {
if (i == row)
continue;
isl_int_mul(mat->row[i][off + col],
mat->row[i][off + col], mat->row[row][off + col]);
if (!isl_int_is_one(mat->row[i][0]))
- isl_seq_normalize(mat->row[i], off + tab->n_col);
+ isl_seq_normalize(mat->ctx, mat->row[i], off + tab->n_col);
}
t = tab->row_var[row];
tab->row_var[row] = tab->col_var[col];
var->index = col;
update_row_sign(tab, row, col, sgn);
if (tab->in_undo)
- return;
+ return 0;
for (i = tab->n_redundant; i < tab->n_row; ++i) {
if (isl_int_is_zero(mat->row[i][off + col]))
continue;
if (!isl_tab_var_from_row(tab, i)->frozen &&
- isl_tab_row_is_redundant(tab, i))
- if (isl_tab_mark_redundant(tab, i))
+ isl_tab_row_is_redundant(tab, i)) {
+ int redo = isl_tab_mark_redundant(tab, i);
+ if (redo < 0)
+ return -1;
+ if (redo)
--i;
+ }
}
+ return 0;
}
/* If "var" represents a column variable, then pivot is up (sgn > 0)
* 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_tab *tab, struct isl_tab_var *var, int sign)
+static int to_row(struct isl_tab *tab, struct isl_tab_var *var, int sign) WARN_UNUSED;
+static int to_row(struct isl_tab *tab, struct isl_tab_var *var, int sign)
{
int r;
unsigned off = 2 + tab->M;
if (var->is_row)
- return;
+ return 0;
if (sign == 0) {
for (r = tab->n_redundant; r < tab->n_row; ++r)
if (!isl_int_is_zero(tab->mat->row[r][off+var->index]))
break;
- isl_assert(tab->mat->ctx, r < tab->n_row, return);
+ isl_assert(tab->mat->ctx, r < tab->n_row, return -1);
} else {
r = pivot_row(tab, NULL, sign, var->index);
- isl_assert(tab->mat->ctx, r >= 0, return);
+ isl_assert(tab->mat->ctx, r >= 0, return -1);
}
- isl_tab_pivot(tab, r, var->index);
+ return isl_tab_pivot(tab, r, var->index);
}
static void check_table(struct isl_tab *tab)
if (max_is_manifestly_unbounded(tab, var))
return 1;
- to_row(tab, var, 1);
+ if (to_row(tab, var, 1) < 0)
+ return -2;
while (!isl_int_is_pos(tab->mat->row[var->index][1])) {
find_pivot(tab, var, var, 1, &row, &col);
if (row == -1)
return isl_int_sgn(tab->mat->row[var->index][1]);
- isl_tab_pivot(tab, row, col);
+ if (isl_tab_pivot(tab, row, col) < 0)
+ return -2;
if (!var->is_row) /* manifestly unbounded */
return 1;
}
find_pivot(tab, var, var, 1, &row, &col);
if (row == -1)
break;
- isl_tab_pivot(tab, row, col);
+ if (isl_tab_pivot(tab, row, col) < 0)
+ return -2;
if (!var->is_row) /* manifestly unbounded */
return 1;
}
break;
if (row == var->index) /* manifestly unbounded */
return 1;
- isl_tab_pivot(tab, row, col);
+ if (isl_tab_pivot(tab, row, col) < 0)
+ return -1;
}
return !isl_int_is_neg(tab->mat->row[var->index][1]);
}
static int sign_of_min(struct isl_tab *tab, struct isl_tab_var *var)
{
int row, col;
- struct isl_tab_var *pivot_var;
+ struct isl_tab_var *pivot_var = NULL;
if (min_is_manifestly_unbounded(tab, var))
return -1;
col = var->index;
row = pivot_row(tab, NULL, -1, col);
pivot_var = var_from_col(tab, col);
- isl_tab_pivot(tab, row, col);
+ if (isl_tab_pivot(tab, row, col) < 0)
+ return -2;
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)
- isl_tab_pivot(tab, row, col);
- else
- restore_row(tab, var);
+ pivot_var->index == row) {
+ if (isl_tab_pivot(tab, row, col) < 0)
+ return -2;
+ } else
+ if (restore_row(tab, var) < -1)
+ return -2;
}
return -1;
}
if (row == -1)
return isl_int_sgn(tab->mat->row[var->index][1]);
pivot_var = var_from_col(tab, col);
- isl_tab_pivot(tab, row, col);
+ if (isl_tab_pivot(tab, row, col) < 0)
+ return -2;
if (var->is_redundant)
return 0;
}
- if (var->is_nonneg) {
+ if (pivot_var && var->is_nonneg) {
/* pivot back to non-negative value */
- if (!pivot_var->is_redundant && pivot_var->index == row)
- isl_tab_pivot(tab, row, col);
- else
- restore_row(tab, var);
+ if (!pivot_var->is_redundant && pivot_var->index == row) {
+ if (isl_tab_pivot(tab, row, col) < 0)
+ return -2;
+ } else
+ if (restore_row(tab, var) < -1)
+ return -2;
}
return -1;
}
col = var->index;
row = pivot_row(tab, NULL, -1, col);
pivot_var = var_from_col(tab, col);
- isl_tab_pivot(tab, row, col);
+ if (isl_tab_pivot(tab, row, col) < 0)
+ return -1;
if (var->is_redundant)
return 0;
if (row_at_most_neg_one(tab, var->index)) {
if (var->is_nonneg) {
if (!pivot_var->is_redundant &&
- pivot_var->index == row)
- isl_tab_pivot(tab, row, col);
- else
- restore_row(tab, var);
+ pivot_var->index == row) {
+ if (isl_tab_pivot(tab, row, col) < 0)
+ return -1;
+ } else
+ if (restore_row(tab, var) < -1)
+ return -1;
}
return 1;
}
if (row == -1)
return 0;
pivot_var = var_from_col(tab, col);
- isl_tab_pivot(tab, row, col);
+ if (isl_tab_pivot(tab, row, col) < 0)
+ return -1;
if (var->is_redundant)
return 0;
} while (!row_at_most_neg_one(tab, var->index));
if (var->is_nonneg) {
/* pivot back to non-negative value */
if (!pivot_var->is_redundant && pivot_var->index == row)
- isl_tab_pivot(tab, row, col);
- restore_row(tab, var);
+ if (isl_tab_pivot(tab, row, col) < 0)
+ return -1;
+ if (restore_row(tab, var) < -1)
+ return -1;
}
return 1;
}
if (max_is_manifestly_unbounded(tab, var))
return 1;
- to_row(tab, var, 1);
+ if (to_row(tab, var, 1) < 0)
+ return -1;
r = tab->mat->row[var->index];
while (isl_int_lt(r[1], r[0])) {
find_pivot(tab, var, var, 1, &row, &col);
return isl_int_ge(r[1], r[0]);
if (row == var->index) /* manifestly unbounded */
return 1;
- isl_tab_pivot(tab, row, col);
+ if (isl_tab_pivot(tab, row, col) < 0)
+ return -1;
}
return 1;
}
{
var_from_col(tab, col)->is_zero = 1;
if (tab->need_undo) {
- isl_tab_push_var(tab, isl_tab_undo_zero, var_from_col(tab, col));
+ if (isl_tab_push_var(tab, isl_tab_undo_zero,
+ var_from_col(tab, col)) < 0)
+ return -1;
if (col != tab->n_dead)
swap_cols(tab, col, tab->n_dead);
tab->n_dead++;
* then also be written as the negative sum of non-negative variables
* and must therefore also be zero.
*/
-static void close_row(struct isl_tab *tab, struct isl_tab_var *var)
+static int close_row(struct isl_tab *tab, struct isl_tab_var *var) WARN_UNUSED;
+static int close_row(struct isl_tab *tab, struct isl_tab_var *var)
{
int j;
struct isl_mat *mat = tab->mat;
unsigned off = 2 + tab->M;
- isl_assert(tab->mat->ctx, var->is_nonneg, return);
+ isl_assert(tab->mat->ctx, var->is_nonneg, return -1);
var->is_zero = 1;
+ if (tab->need_undo)
+ if (isl_tab_push_var(tab, isl_tab_undo_zero, var) < 0)
+ return -1;
for (j = tab->n_dead; j < tab->n_col; ++j) {
if (isl_int_is_zero(mat->row[var->index][off + j]))
continue;
isl_assert(tab->mat->ctx,
- isl_int_is_neg(mat->row[var->index][off + j]), return);
+ isl_int_is_neg(mat->row[var->index][off + j]), return -1);
if (isl_tab_kill_col(tab, j))
--j;
}
- isl_tab_mark_redundant(tab, var->index);
+ if (isl_tab_mark_redundant(tab, var->index) < 0)
+ return -1;
+ return 0;
}
/* Add a constraint to the tableau and allocate a row for it.
int r;
isl_assert(tab->mat->ctx, tab->n_row < tab->mat->n_row, return -1);
+ isl_assert(tab->mat->ctx, tab->n_con < tab->max_con, return -1);
r = tab->n_con;
tab->con[r].index = tab->n_row;
tab->con[r].is_zero = 0;
tab->con[r].is_redundant = 0;
tab->con[r].frozen = 0;
+ tab->con[r].negated = 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]);
+ if (isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->con[r]) < 0)
+ return -1;
return r;
}
tab->var[r].is_zero = 0;
tab->var[r].is_redundant = 0;
tab->var[r].frozen = 0;
+ tab->var[r].negated = 0;
tab->col_var[tab->n_col] = r;
for (i = 0; i < tab->n_row; ++i)
tab->n_var++;
tab->n_col++;
- isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->var[r]);
+ if (isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->var[r]) < 0)
+ return -1;
return r;
}
if (tab->M && i >= tab->n_param && i < tab->n_var - tab->n_div)
isl_int_submul(row[2], line[1 + i], row[0]);
}
- isl_seq_normalize(row, off + tab->n_col);
+ isl_seq_normalize(tab->mat->ctx, row, off + tab->n_col);
isl_int_clear(a);
isl_int_clear(b);
{
int r;
int sgn;
+ isl_int cst;
if (!tab)
return NULL;
+ if (tab->bset) {
+ struct isl_basic_set *bset = tab->bset;
+
+ isl_assert(tab->mat->ctx, tab->n_eq == bset->n_eq, goto error);
+ isl_assert(tab->mat->ctx,
+ tab->n_con == bset->n_eq + bset->n_ineq, goto error);
+ tab->bset = isl_basic_set_add_ineq(tab->bset, ineq);
+ if (isl_tab_push(tab, isl_tab_undo_bset_ineq) < 0)
+ goto error;
+ if (!tab->bset)
+ goto error;
+ }
+ if (tab->cone) {
+ isl_int_init(cst);
+ isl_int_swap(ineq[0], cst);
+ }
r = isl_tab_add_row(tab, ineq);
+ if (tab->cone) {
+ isl_int_swap(ineq[0], cst);
+ isl_int_clear(cst);
+ }
if (r < 0)
goto error;
tab->con[r].is_nonneg = 1;
- isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]);
+ if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
+ goto error;
if (isl_tab_row_is_redundant(tab, tab->con[r].index)) {
- isl_tab_mark_redundant(tab, tab->con[r].index);
+ if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
+ goto error;
return tab;
}
sgn = restore_row(tab, &tab->con[r]);
+ if (sgn < -1)
+ goto error;
if (sgn < 0)
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);
+ if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
+ goto error;
return tab;
error:
isl_tab_free(tab);
/* Pivot a non-negative variable down until it reaches the value zero
* and then pivot the variable into a column position.
*/
-int to_col(struct isl_tab *tab, struct isl_tab_var *var)
+static int to_col(struct isl_tab *tab, struct isl_tab_var *var) WARN_UNUSED;
+static int to_col(struct isl_tab *tab, struct isl_tab_var *var)
{
int i;
int row, col;
unsigned off = 2 + tab->M;
if (!var->is_row)
- return;
+ return 0;
while (isl_int_is_pos(tab->mat->row[var->index][1])) {
find_pivot(tab, var, NULL, -1, &row, &col);
isl_assert(tab->mat->ctx, row != -1, return -1);
- isl_tab_pivot(tab, row, col);
+ if (isl_tab_pivot(tab, row, col) < 0)
+ return -1;
if (!var->is_row)
- return;
+ return 0;
}
for (i = tab->n_dead; i < tab->n_col; ++i)
break;
isl_assert(tab->mat->ctx, i < tab->n_col, return -1);
- isl_tab_pivot(tab, var->index, i);
+ if (isl_tab_pivot(tab, var->index, i) < 0)
+ return -1;
return 0;
}
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);
+ if (isl_tab_pivot(tab, r, i) < 0)
+ goto error;
+ if (isl_tab_kill_col(tab, i) < 0)
+ goto error;
tab->n_eq++;
return tab;
return NULL;
}
+static int row_is_manifestly_zero(struct isl_tab *tab, int row)
+{
+ unsigned off = 2 + tab->M;
+
+ if (!isl_int_is_zero(tab->mat->row[row][1]))
+ return 0;
+ if (tab->M && !isl_int_is_zero(tab->mat->row[row][2]))
+ return 0;
+ return isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
+ tab->n_col - tab->n_dead) == -1;
+}
+
/* Add an equality that is known to be valid for the given tableau.
*/
struct isl_tab *isl_tab_add_valid_eq(struct isl_tab *tab, isl_int *eq)
{
struct isl_tab_var *var;
- int i;
int r;
if (!tab)
var = &tab->con[r];
r = var->index;
- if (isl_int_is_neg(tab->mat->row[r][1]))
+ if (row_is_manifestly_zero(tab, r)) {
+ var->is_zero = 1;
+ if (isl_tab_mark_redundant(tab, r) < 0)
+ goto error;
+ return tab;
+ }
+
+ if (isl_int_is_neg(tab->mat->row[r][1])) {
isl_seq_neg(tab->mat->row[r] + 1, tab->mat->row[r] + 1,
1 + tab->n_col);
+ var->negated = 1;
+ }
+ var->is_nonneg = 1;
+ if (to_col(tab, var) < 0)
+ goto error;
+ var->is_nonneg = 0;
+ if (isl_tab_kill_col(tab, var->index) < 0)
+ goto error;
+
+ return tab;
+error:
+ isl_tab_free(tab);
+ return NULL;
+}
+
+static int add_zero_row(struct isl_tab *tab)
+{
+ int r;
+ isl_int *row;
+
+ r = isl_tab_allocate_con(tab);
+ if (r < 0)
+ return -1;
+
+ row = tab->mat->row[tab->con[r].index];
+ isl_seq_clr(row + 1, 1 + tab->M + tab->n_col);
+ isl_int_set_si(row[0], 1);
+
+ return r;
+}
+
+/* Add equality "eq" and check if it conflicts with the
+ * previously added constraints or if it is obviously redundant.
+ */
+struct isl_tab *isl_tab_add_eq(struct isl_tab *tab, isl_int *eq)
+{
+ struct isl_tab_undo *snap = NULL;
+ struct isl_tab_var *var;
+ int r;
+ int row;
+ int sgn;
+ isl_int cst;
+
+ if (!tab)
+ return NULL;
+ isl_assert(tab->mat->ctx, !tab->M, goto error);
+
+ if (tab->need_undo)
+ snap = isl_tab_snap(tab);
+
+ if (tab->cone) {
+ isl_int_init(cst);
+ isl_int_swap(eq[0], cst);
+ }
+ r = isl_tab_add_row(tab, eq);
+ if (tab->cone) {
+ isl_int_swap(eq[0], cst);
+ isl_int_clear(cst);
+ }
+ if (r < 0)
+ goto error;
+
+ var = &tab->con[r];
+ row = var->index;
+ if (row_is_manifestly_zero(tab, row)) {
+ if (snap) {
+ if (isl_tab_rollback(tab, snap) < 0)
+ goto error;
+ } else
+ drop_row(tab, row);
+ return tab;
+ }
+
+ if (tab->bset) {
+ tab->bset = isl_basic_set_add_ineq(tab->bset, eq);
+ if (isl_tab_push(tab, isl_tab_undo_bset_ineq) < 0)
+ goto error;
+ isl_seq_neg(eq, eq, 1 + tab->n_var);
+ tab->bset = isl_basic_set_add_ineq(tab->bset, eq);
+ isl_seq_neg(eq, eq, 1 + tab->n_var);
+ if (isl_tab_push(tab, isl_tab_undo_bset_ineq) < 0)
+ goto error;
+ if (!tab->bset)
+ goto error;
+ if (add_zero_row(tab) < 0)
+ goto error;
+ }
+
+ sgn = isl_int_sgn(tab->mat->row[row][1]);
+
+ if (sgn > 0) {
+ isl_seq_neg(tab->mat->row[row] + 1, tab->mat->row[row] + 1,
+ 1 + tab->n_col);
+ var->negated = 1;
+ sgn = -1;
+ }
+
+ if (sgn < 0) {
+ sgn = sign_of_max(tab, var);
+ if (sgn < -1)
+ goto error;
+ if (sgn < 0)
+ return isl_tab_mark_empty(tab);
+ }
+
var->is_nonneg = 1;
if (to_col(tab, var) < 0)
goto error;
var->is_nonneg = 0;
- isl_tab_kill_col(tab, var->index);
+ if (isl_tab_kill_col(tab, var->index) < 0)
+ goto error;
return tab;
error:
return isl_tab_from_basic_map((struct isl_basic_map *)bset);
}
-/* Construct a tableau corresponding to the recession cone of "bmap".
+/* Construct a tableau corresponding to the recession cone of "bset".
*/
-struct isl_tab *isl_tab_from_recession_cone(struct isl_basic_map *bmap)
+struct isl_tab *isl_tab_from_recession_cone(struct isl_basic_set *bset)
{
isl_int cst;
int i;
struct isl_tab *tab;
- if (!bmap)
+ if (!bset)
return NULL;
- tab = isl_tab_alloc(bmap->ctx, bmap->n_eq + bmap->n_ineq,
- isl_basic_map_total_dim(bmap), 0);
+ tab = isl_tab_alloc(bset->ctx, bset->n_eq + bset->n_ineq,
+ isl_basic_set_total_dim(bset), 0);
if (!tab)
return NULL;
- tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
+ tab->rational = ISL_F_ISSET(bset, ISL_BASIC_SET_RATIONAL);
+ tab->cone = 1;
isl_int_init(cst);
- for (i = 0; i < bmap->n_eq; ++i) {
- isl_int_swap(bmap->eq[i][0], cst);
- tab = add_eq(tab, bmap->eq[i]);
- isl_int_swap(bmap->eq[i][0], cst);
+ for (i = 0; i < bset->n_eq; ++i) {
+ isl_int_swap(bset->eq[i][0], cst);
+ tab = add_eq(tab, bset->eq[i]);
+ isl_int_swap(bset->eq[i][0], cst);
if (!tab)
goto done;
}
- for (i = 0; i < bmap->n_ineq; ++i) {
+ for (i = 0; i < bset->n_ineq; ++i) {
int r;
- isl_int_swap(bmap->ineq[i][0], cst);
- r = isl_tab_add_row(tab, bmap->ineq[i]);
- isl_int_swap(bmap->ineq[i][0], cst);
+ isl_int_swap(bset->ineq[i][0], cst);
+ r = isl_tab_add_row(tab, bset->ineq[i]);
+ isl_int_swap(bset->ineq[i][0], cst);
if (r < 0)
goto error;
tab->con[r].is_nonneg = 1;
- isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]);
+ if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
+ goto error;
}
done:
isl_int_clear(cst);
for (;;) {
for (i = tab->n_redundant; i < tab->n_row; ++i) {
struct isl_tab_var *var;
+ int sgn;
var = isl_tab_var_from_row(tab, i);
if (!var->is_nonneg)
continue;
- if (sign_of_max(tab, var) != 0)
+ sgn = sign_of_max(tab, var);
+ if (sgn < -1)
+ return -1;
+ if (sgn != 0)
return 0;
- close_row(tab, var);
+ if (close_row(tab, var) < 0)
+ return -1;
break;
}
if (tab->n_dead == tab->n_col)
isl_int_divexact(m, vec->block.data[0], tab->mat->row[row][0]);
isl_int_mul(vec->block.data[1 + i], m, tab->mat->row[row][1]);
}
- isl_seq_normalize(vec->block.data, vec->size);
+ vec = isl_vec_normalize(vec);
isl_int_clear(m);
return vec;
int sgn;
unsigned off = 2 + tab->M;
+ if (var->is_zero)
+ return tab;
+ isl_assert(tab->mat->ctx, !var->is_redundant, goto error);
+
if (isl_tab_extend_cons(tab, 1) < 0)
goto error;
tab->con[r].is_zero = 0;
tab->con[r].is_redundant = 0;
tab->con[r].frozen = 0;
+ tab->con[r].negated = 0;
tab->row_var[tab->n_row] = ~r;
row = tab->mat->row[tab->n_row];
tab->n_row++;
tab->n_con++;
- isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->con[r]);
+ if (isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->con[r]) < 0)
+ goto error;
sgn = sign_of_max(tab, &tab->con[r]);
+ if (sgn < -1)
+ goto error;
if (sgn < 0)
return isl_tab_mark_empty(tab);
tab->con[r].is_nonneg = 1;
- isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]);
+ if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
+ goto error;
/* sgn == 0 */
- close_row(tab, &tab->con[r]);
+ if (close_row(tab, &tab->con[r]) < 0)
+ goto error;
return tab;
error:
var = &tab->con[con];
if (!var->is_row && !max_is_manifestly_unbounded(tab, var))
- to_row(tab, var, 1);
+ if (to_row(tab, var, 1) < 0)
+ goto error;
if (var->is_row)
isl_int_add(tab->mat->row[var->index][1],
}
- isl_tab_push_var(tab, isl_tab_undo_relax, var);
+ if (isl_tab_push_var(tab, isl_tab_undo_relax, var) < 0)
+ goto error;
return tab;
+error:
+ isl_tab_free(tab);
+ return NULL;
}
struct isl_tab *isl_tab_select_facet(struct isl_tab *tab, int con)
* 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_tab *tab)
+struct isl_tab *isl_tab_detect_implicit_equalities(struct isl_tab *tab)
{
int i;
unsigned n_marked;
}
while (n_marked) {
struct isl_tab_var *var;
+ int sgn;
for (i = tab->n_redundant; i < tab->n_row; ++i) {
var = isl_tab_var_from_row(tab, i);
if (var->marked)
}
var->marked = 0;
n_marked--;
- if (sign_of_max(tab, var) == 0)
- close_row(tab, var);
- else if (!tab->rational && !at_least_one(tab, var)) {
+ sgn = sign_of_max(tab, var);
+ if (sgn < 0)
+ goto error;
+ if (sgn == 0) {
+ if (close_row(tab, var) < 0)
+ goto error;
+ } else if (!tab->rational && !at_least_one(tab, var)) {
tab = cut_to_hyperplane(tab, var);
- return isl_tab_detect_equalities(tab);
+ return isl_tab_detect_implicit_equalities(tab);
}
for (i = tab->n_redundant; i < tab->n_row; ++i) {
var = isl_tab_var_from_row(tab, i);
}
return tab;
+error:
+ isl_tab_free(tab);
+ return NULL;
+}
+
+static int con_is_redundant(struct isl_tab *tab, struct isl_tab_var *var)
+{
+ if (!tab)
+ return -1;
+ if (tab->rational) {
+ int sgn = sign_of_min(tab, var);
+ if (sgn < -1)
+ return -1;
+ return sgn >= 0;
+ } else {
+ int irred = isl_tab_min_at_most_neg_one(tab, var);
+ if (irred < 0)
+ return -1;
+ return !irred;
+ }
}
/* Check for (near) redundant constraints.
}
while (n_marked) {
struct isl_tab_var *var;
+ int red;
for (i = tab->n_redundant; i < tab->n_row; ++i) {
var = isl_tab_var_from_row(tab, i);
if (var->marked)
}
var->marked = 0;
n_marked--;
- if ((tab->rational ? (sign_of_min(tab, var) >= 0)
- : !isl_tab_min_at_most_neg_one(tab, var)) &&
- !var->is_redundant)
- isl_tab_mark_redundant(tab, var->index);
+ red = con_is_redundant(tab, var);
+ if (red < 0)
+ goto error;
+ if (red && !var->is_redundant)
+ if (isl_tab_mark_redundant(tab, var->index) < 0)
+ goto error;
for (i = tab->n_dead; i < tab->n_col; ++i) {
var = var_from_col(tab, i);
if (!var->marked)
}
return tab;
+error:
+ isl_tab_free(tab);
+ return NULL;
}
int isl_tab_is_equality(struct isl_tab *tab, int con)
}
if (row == -1)
break;
- isl_tab_pivot(tab, row, col);
+ if (isl_tab_pivot(tab, row, col) < 0)
+ return isl_lp_error;
}
- if (isl_tab_rollback(tab, snap) < 0)
- return isl_lp_error;
if (ISL_FL_ISSET(flags, ISL_TAB_SAVE_DUAL)) {
int i;
return isl_lp_error;
isl_int_set(tab->dual->el[0], tab->mat->row[var->index][0]);
for (i = 0; i < tab->n_con; ++i) {
- if (tab->con[i].is_row)
+ int pos;
+ if (tab->con[i].is_row) {
isl_int_set_si(tab->dual->el[1 + i], 0);
- else {
- int pos = 2 + tab->con[i].index;
+ continue;
+ }
+ pos = 2 + tab->M + tab->con[i].index;
+ if (tab->con[i].negated)
+ isl_int_neg(tab->dual->el[1 + i],
+ tab->mat->row[var->index][pos]);
+ else
isl_int_set(tab->dual->el[1 + i],
tab->mat->row[var->index][pos]);
- }
}
}
- if (res == isl_lp_ok) {
+ if (opt && res == isl_lp_ok) {
if (opt_denom) {
isl_int_set(*opt, tab->mat->row[var->index][1]);
isl_int_set(*opt_denom, tab->mat->row[var->index][0]);
isl_int_cdiv_q(*opt, tab->mat->row[var->index][1],
tab->mat->row[var->index][0]);
}
+ if (isl_tab_rollback(tab, snap) < 0)
+ return isl_lp_error;
return res;
}
int isl_tab_is_redundant(struct isl_tab *tab, int con)
{
- int row;
- unsigned n_col;
-
if (!tab)
return -1;
if (tab->con[con].is_zero)
/* Undo the operation performed by isl_tab_relax.
*/
-static void unrelax(struct isl_tab *tab, struct isl_tab_var *var)
+static int unrelax(struct isl_tab *tab, struct isl_tab_var *var) WARN_UNUSED;
+static int unrelax(struct isl_tab *tab, struct isl_tab_var *var)
{
unsigned off = 2 + tab->M;
if (!var->is_row && !max_is_manifestly_unbounded(tab, var))
- to_row(tab, var, 1);
+ if (to_row(tab, var, 1) < 0)
+ return -1;
if (var->is_row)
isl_int_sub(tab->mat->row[var->index][1],
}
}
+
+ return 0;
}
-static void perform_undo_var(struct isl_tab *tab, struct isl_tab_undo *undo)
+static int perform_undo_var(struct isl_tab *tab, struct isl_tab_undo *undo) WARN_UNUSED;
+static int 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) {
break;
case isl_tab_undo_zero:
var->is_zero = 0;
- tab->n_dead--;
+ if (!var->is_row)
+ tab->n_dead--;
break;
case isl_tab_undo_allocate:
if (undo->u.var_index >= 0) {
- isl_assert(tab->mat->ctx, !var->is_row, return);
+ isl_assert(tab->mat->ctx, !var->is_row, return -1);
drop_col(tab, var->index);
break;
}
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(tab, var, 0);
+ if (!max_is_manifestly_unbounded(tab, var)) {
+ if (to_row(tab, var, 1) < 0)
+ return -1;
+ } else if (!min_is_manifestly_unbounded(tab, var)) {
+ if (to_row(tab, var, -1) < 0)
+ return -1;
+ } else
+ if (to_row(tab, var, 0) < 0)
+ return -1;
}
drop_row(tab, var->index);
break;
case isl_tab_undo_relax:
- unrelax(tab, var);
- break;
+ return unrelax(tab, var);
}
+
+ return 0;
}
/* Restore the tableau to the state where the basic variables
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]);
+ if (isl_tab_pivot(tab, row, extra[j]) < 0)
+ goto error;
extra[j] = extra[--n_extra];
}
return -1;
}
+/* Remove all samples with index n or greater, i.e., those samples
+ * that were added since we saved this number of samples in
+ * isl_tab_save_samples.
+ */
+static void drop_samples_since(struct isl_tab *tab, int n)
+{
+ int i;
+
+ for (i = tab->n_sample - 1; i >= 0 && tab->n_sample > n; --i) {
+ if (tab->sample_index[i] < n)
+ continue;
+
+ if (i != tab->n_sample - 1) {
+ int t = tab->sample_index[tab->n_sample-1];
+ tab->sample_index[tab->n_sample-1] = tab->sample_index[i];
+ tab->sample_index[i] = t;
+ isl_mat_swap_rows(tab->samples, tab->n_sample-1, i);
+ }
+ tab->n_sample--;
+ }
+}
+
+static int perform_undo(struct isl_tab *tab, struct isl_tab_undo *undo) WARN_UNUSED;
static int perform_undo(struct isl_tab *tab, struct isl_tab_undo *undo)
{
switch (undo->type) {
case isl_tab_undo_zero:
case isl_tab_undo_allocate:
case isl_tab_undo_relax:
- perform_undo_var(tab, undo);
- break;
+ return perform_undo_var(tab, undo);
case isl_tab_undo_bset_eq:
- isl_basic_set_free_equality(tab->bset, 1);
- break;
+ return isl_basic_set_free_equality(tab->bset, 1);
case isl_tab_undo_bset_ineq:
- isl_basic_set_free_inequality(tab->bset, 1);
- break;
+ return isl_basic_set_free_inequality(tab->bset, 1);
case isl_tab_undo_bset_div:
- isl_basic_set_free_div(tab->bset, 1);
+ if (isl_basic_set_free_div(tab->bset, 1) < 0)
+ return -1;
+ if (tab->samples)
+ tab->samples->n_col--;
break;
case isl_tab_undo_saved_basis:
if (restore_basis(tab, undo->u.col_var) < 0)
return -1;
break;
+ case isl_tab_undo_drop_sample:
+ tab->n_outside--;
+ break;
+ case isl_tab_undo_saved_samples:
+ drop_samples_since(tab, undo->u.n);
+ break;
+ case isl_tab_undo_callback:
+ return undo->u.callback->run(undo->u.callback);
default:
isl_assert(tab->mat->ctx, 0, return -1);
}
(tab->rational ||
isl_int_abs_ge(tab->mat->row[row][1],
tab->mat->row[row][0]))) {
- if (at_least_zero(tab, &tab->con[con]))
+ int nonneg = at_least_zero(tab, &tab->con[con]);
+ if (nonneg < 0)
+ goto error;
+ if (nonneg)
type = isl_ineq_cut;
else
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;
+ } else {
+ int red = con_is_redundant(tab, &tab->con[con]);
+ if (red < 0)
+ goto error;
+ if (!red)
+ type = isl_ineq_cut;
+ else
+ type = isl_ineq_redundant;
+ }
if (isl_tab_rollback(tab, snap))
return isl_ineq_error;
return type;
error:
- isl_tab_rollback(tab, snap);
return isl_ineq_error;
}