2 #include "isl_map_private.h"
7 * The implementation of tableaus in this file was inspired by Section 8
8 * of David Detlefs, Greg Nelson and James B. Saxe, "Simplify: a theorem
9 * prover for program checking".
12 struct isl_tab *isl_tab_alloc(struct isl_ctx *ctx,
13 unsigned n_row, unsigned n_var, unsigned M)
19 tab = isl_calloc_type(ctx, struct isl_tab);
22 tab->mat = isl_mat_alloc(ctx, n_row, off + n_var);
25 tab->var = isl_alloc_array(ctx, struct isl_tab_var, n_var);
28 tab->con = isl_alloc_array(ctx, struct isl_tab_var, n_row);
31 tab->col_var = isl_alloc_array(ctx, int, n_var);
34 tab->row_var = isl_alloc_array(ctx, int, n_row);
37 for (i = 0; i < n_var; ++i) {
38 tab->var[i].index = i;
39 tab->var[i].is_row = 0;
40 tab->var[i].is_nonneg = 0;
41 tab->var[i].is_zero = 0;
42 tab->var[i].is_redundant = 0;
43 tab->var[i].frozen = 0;
44 tab->var[i].negated = 0;
63 tab->bottom.type = isl_tab_undo_bottom;
64 tab->bottom.next = NULL;
65 tab->top = &tab->bottom;
77 int isl_tab_extend_cons(struct isl_tab *tab, unsigned n_new)
79 unsigned off = 2 + tab->M;
84 if (tab->max_con < tab->n_con + n_new) {
85 struct isl_tab_var *con;
87 con = isl_realloc_array(tab->mat->ctx, tab->con,
88 struct isl_tab_var, tab->max_con + n_new);
92 tab->max_con += n_new;
94 if (tab->mat->n_row < tab->n_row + n_new) {
97 tab->mat = isl_mat_extend(tab->mat,
98 tab->n_row + n_new, off + tab->n_col);
101 row_var = isl_realloc_array(tab->mat->ctx, tab->row_var,
102 int, tab->mat->n_row);
105 tab->row_var = row_var;
107 enum isl_tab_row_sign *s;
108 s = isl_realloc_array(tab->mat->ctx, tab->row_sign,
109 enum isl_tab_row_sign, tab->mat->n_row);
118 /* Make room for at least n_new extra variables.
119 * Return -1 if anything went wrong.
121 int isl_tab_extend_vars(struct isl_tab *tab, unsigned n_new)
123 struct isl_tab_var *var;
124 unsigned off = 2 + tab->M;
126 if (tab->max_var < tab->n_var + n_new) {
127 var = isl_realloc_array(tab->mat->ctx, tab->var,
128 struct isl_tab_var, tab->n_var + n_new);
132 tab->max_var += n_new;
135 if (tab->mat->n_col < off + tab->n_col + n_new) {
138 tab->mat = isl_mat_extend(tab->mat,
139 tab->mat->n_row, off + tab->n_col + n_new);
142 p = isl_realloc_array(tab->mat->ctx, tab->col_var,
143 int, tab->n_col + n_new);
152 struct isl_tab *isl_tab_extend(struct isl_tab *tab, unsigned n_new)
154 if (isl_tab_extend_cons(tab, n_new) >= 0)
161 static void free_undo(struct isl_tab *tab)
163 struct isl_tab_undo *undo, *next;
165 for (undo = tab->top; undo && undo != &tab->bottom; undo = next) {
172 void isl_tab_free(struct isl_tab *tab)
177 isl_mat_free(tab->mat);
178 isl_vec_free(tab->dual);
179 isl_basic_set_free(tab->bset);
185 isl_mat_free(tab->samples);
186 isl_mat_free(tab->basis);
190 struct isl_tab *isl_tab_dup(struct isl_tab *tab)
200 dup = isl_calloc_type(tab->ctx, struct isl_tab);
203 dup->mat = isl_mat_dup(tab->mat);
206 dup->var = isl_alloc_array(tab->ctx, struct isl_tab_var, tab->max_var);
209 for (i = 0; i < tab->n_var; ++i)
210 dup->var[i] = tab->var[i];
211 dup->con = isl_alloc_array(tab->ctx, struct isl_tab_var, tab->max_con);
214 for (i = 0; i < tab->n_con; ++i)
215 dup->con[i] = tab->con[i];
216 dup->col_var = isl_alloc_array(tab->ctx, int, tab->mat->n_col - off);
219 for (i = 0; i < tab->n_col; ++i)
220 dup->col_var[i] = tab->col_var[i];
221 dup->row_var = isl_alloc_array(tab->ctx, int, tab->mat->n_row);
224 for (i = 0; i < tab->n_row; ++i)
225 dup->row_var[i] = tab->row_var[i];
227 dup->row_sign = isl_alloc_array(tab->ctx, enum isl_tab_row_sign,
231 for (i = 0; i < tab->n_row; ++i)
232 dup->row_sign[i] = tab->row_sign[i];
235 dup->samples = isl_mat_dup(tab->samples);
238 dup->n_sample = tab->n_sample;
239 dup->n_outside = tab->n_outside;
241 dup->n_row = tab->n_row;
242 dup->n_con = tab->n_con;
243 dup->n_eq = tab->n_eq;
244 dup->max_con = tab->max_con;
245 dup->n_col = tab->n_col;
246 dup->n_var = tab->n_var;
247 dup->max_var = tab->max_var;
248 dup->n_param = tab->n_param;
249 dup->n_div = tab->n_div;
250 dup->n_dead = tab->n_dead;
251 dup->n_redundant = tab->n_redundant;
252 dup->rational = tab->rational;
253 dup->empty = tab->empty;
257 dup->bottom.type = isl_tab_undo_bottom;
258 dup->bottom.next = NULL;
259 dup->top = &dup->bottom;
261 dup->n_zero = tab->n_zero;
262 dup->n_unbounded = tab->n_unbounded;
263 dup->basis = isl_mat_dup(tab->basis);
271 /* Construct the coefficient matrix of the product tableau
273 * mat{1,2} is the coefficient matrix of tableau {1,2}
274 * row{1,2} is the number of rows in tableau {1,2}
275 * col{1,2} is the number of columns in tableau {1,2}
276 * off is the offset to the coefficient column (skipping the
277 * denominator, the constant term and the big parameter if any)
278 * r{1,2} is the number of redundant rows in tableau {1,2}
279 * d{1,2} is the number of dead columns in tableau {1,2}
281 * The order of the rows and columns in the result is as explained
282 * in isl_tab_product.
284 static struct isl_mat *tab_mat_product(struct isl_mat *mat1,
285 struct isl_mat *mat2, unsigned row1, unsigned row2,
286 unsigned col1, unsigned col2,
287 unsigned off, unsigned r1, unsigned r2, unsigned d1, unsigned d2)
290 struct isl_mat *prod;
293 prod = isl_mat_alloc(mat1->ctx, mat1->n_row + mat2->n_row,
297 for (i = 0; i < r1; ++i) {
298 isl_seq_cpy(prod->row[n + i], mat1->row[i], off + d1);
299 isl_seq_clr(prod->row[n + i] + off + d1, d2);
300 isl_seq_cpy(prod->row[n + i] + off + d1 + d2,
301 mat1->row[i] + off + d1, col1 - d1);
302 isl_seq_clr(prod->row[n + i] + off + col1 + d1, col2 - d2);
306 for (i = 0; i < r2; ++i) {
307 isl_seq_cpy(prod->row[n + i], mat2->row[i], off);
308 isl_seq_clr(prod->row[n + i] + off, d1);
309 isl_seq_cpy(prod->row[n + i] + off + d1,
310 mat2->row[i] + off, d2);
311 isl_seq_clr(prod->row[n + i] + off + d1 + d2, col1 - d1);
312 isl_seq_cpy(prod->row[n + i] + off + col1 + d1,
313 mat2->row[i] + off + d2, col2 - d2);
317 for (i = 0; i < row1 - r1; ++i) {
318 isl_seq_cpy(prod->row[n + i], mat1->row[r1 + i], off + d1);
319 isl_seq_clr(prod->row[n + i] + off + d1, d2);
320 isl_seq_cpy(prod->row[n + i] + off + d1 + d2,
321 mat1->row[r1 + i] + off + d1, col1 - d1);
322 isl_seq_clr(prod->row[n + i] + off + col1 + d1, col2 - d2);
326 for (i = 0; i < row2 - r2; ++i) {
327 isl_seq_cpy(prod->row[n + i], mat2->row[r2 + i], off);
328 isl_seq_clr(prod->row[n + i] + off, d1);
329 isl_seq_cpy(prod->row[n + i] + off + d1,
330 mat2->row[r2 + i] + off, d2);
331 isl_seq_clr(prod->row[n + i] + off + d1 + d2, col1 - d1);
332 isl_seq_cpy(prod->row[n + i] + off + col1 + d1,
333 mat2->row[r2 + i] + off + d2, col2 - d2);
339 /* Update the row or column index of a variable that corresponds
340 * to a variable in the first input tableau.
342 static void update_index1(struct isl_tab_var *var,
343 unsigned r1, unsigned r2, unsigned d1, unsigned d2)
345 if (var->index == -1)
347 if (var->is_row && var->index >= r1)
349 if (!var->is_row && var->index >= d1)
353 /* Update the row or column index of a variable that corresponds
354 * to a variable in the second input tableau.
356 static void update_index2(struct isl_tab_var *var,
357 unsigned row1, unsigned col1,
358 unsigned r1, unsigned r2, unsigned d1, unsigned d2)
360 if (var->index == -1)
375 /* Create a tableau that represents the Cartesian product of the sets
376 * represented by tableaus tab1 and tab2.
377 * The order of the rows in the product is
378 * - redundant rows of tab1
379 * - redundant rows of tab2
380 * - non-redundant rows of tab1
381 * - non-redundant rows of tab2
382 * The order of the columns is
385 * - coefficient of big parameter, if any
386 * - dead columns of tab1
387 * - dead columns of tab2
388 * - live columns of tab1
389 * - live columns of tab2
390 * The order of the variables and the constraints is a concatenation
391 * of order in the two input tableaus.
393 struct isl_tab *isl_tab_product(struct isl_tab *tab1, struct isl_tab *tab2)
396 struct isl_tab *prod;
398 unsigned r1, r2, d1, d2;
403 isl_assert(tab1->mat->ctx, tab1->M == tab2->M, return NULL);
404 isl_assert(tab1->mat->ctx, tab1->rational == tab2->rational, return NULL);
405 isl_assert(tab1->mat->ctx, !tab1->row_sign, return NULL);
406 isl_assert(tab1->mat->ctx, !tab2->row_sign, return NULL);
407 isl_assert(tab1->mat->ctx, tab1->n_param == 0, return NULL);
408 isl_assert(tab1->mat->ctx, tab2->n_param == 0, return NULL);
409 isl_assert(tab1->mat->ctx, tab1->n_div == 0, return NULL);
410 isl_assert(tab1->mat->ctx, tab2->n_div == 0, return NULL);
413 r1 = tab1->n_redundant;
414 r2 = tab2->n_redundant;
417 prod = isl_calloc_type(tab1->mat->ctx, struct isl_tab);
420 prod->mat = tab_mat_product(tab1->mat, tab2->mat,
421 tab1->n_row, tab2->n_row,
422 tab1->n_col, tab2->n_col, off, r1, r2, d1, d2);
425 prod->var = isl_alloc_array(tab1->mat->ctx, struct isl_tab_var,
426 tab1->max_var + tab2->max_var);
429 for (i = 0; i < tab1->n_var; ++i) {
430 prod->var[i] = tab1->var[i];
431 update_index1(&prod->var[i], r1, r2, d1, d2);
433 for (i = 0; i < tab2->n_var; ++i) {
434 prod->var[tab1->n_var + i] = tab2->var[i];
435 update_index2(&prod->var[tab1->n_var + i],
436 tab1->n_row, tab1->n_col,
439 prod->con = isl_alloc_array(tab1->mat->ctx, struct isl_tab_var,
440 tab1->max_con + tab2->max_con);
443 for (i = 0; i < tab1->n_con; ++i) {
444 prod->con[i] = tab1->con[i];
445 update_index1(&prod->con[i], r1, r2, d1, d2);
447 for (i = 0; i < tab2->n_con; ++i) {
448 prod->con[tab1->n_con + i] = tab2->con[i];
449 update_index2(&prod->con[tab1->n_con + i],
450 tab1->n_row, tab1->n_col,
453 prod->col_var = isl_alloc_array(tab1->mat->ctx, int,
454 tab1->n_col + tab2->n_col);
457 for (i = 0; i < tab1->n_col; ++i) {
458 int pos = i < d1 ? i : i + d2;
459 prod->col_var[pos] = tab1->col_var[i];
461 for (i = 0; i < tab2->n_col; ++i) {
462 int pos = i < d2 ? d1 + i : tab1->n_col + i;
463 int t = tab2->col_var[i];
468 prod->col_var[pos] = t;
470 prod->row_var = isl_alloc_array(tab1->mat->ctx, int,
471 tab1->mat->n_row + tab2->mat->n_row);
474 for (i = 0; i < tab1->n_row; ++i) {
475 int pos = i < r1 ? i : i + r2;
476 prod->row_var[pos] = tab1->row_var[i];
478 for (i = 0; i < tab2->n_row; ++i) {
479 int pos = i < r2 ? r1 + i : tab1->n_row + i;
480 int t = tab2->row_var[i];
485 prod->row_var[pos] = t;
487 prod->samples = NULL;
488 prod->n_row = tab1->n_row + tab2->n_row;
489 prod->n_con = tab1->n_con + tab2->n_con;
491 prod->max_con = tab1->max_con + tab2->max_con;
492 prod->n_col = tab1->n_col + tab2->n_col;
493 prod->n_var = tab1->n_var + tab2->n_var;
494 prod->max_var = tab1->max_var + tab2->max_var;
497 prod->n_dead = tab1->n_dead + tab2->n_dead;
498 prod->n_redundant = tab1->n_redundant + tab2->n_redundant;
499 prod->rational = tab1->rational;
500 prod->empty = tab1->empty || tab2->empty;
504 prod->bottom.type = isl_tab_undo_bottom;
505 prod->bottom.next = NULL;
506 prod->top = &prod->bottom;
509 prod->n_unbounded = 0;
518 static struct isl_tab_var *var_from_index(struct isl_tab *tab, int i)
523 return &tab->con[~i];
526 struct isl_tab_var *isl_tab_var_from_row(struct isl_tab *tab, int i)
528 return var_from_index(tab, tab->row_var[i]);
531 static struct isl_tab_var *var_from_col(struct isl_tab *tab, int i)
533 return var_from_index(tab, tab->col_var[i]);
536 /* Check if there are any upper bounds on column variable "var",
537 * i.e., non-negative rows where var appears with a negative coefficient.
538 * Return 1 if there are no such bounds.
540 static int max_is_manifestly_unbounded(struct isl_tab *tab,
541 struct isl_tab_var *var)
544 unsigned off = 2 + tab->M;
548 for (i = tab->n_redundant; i < tab->n_row; ++i) {
549 if (!isl_int_is_neg(tab->mat->row[i][off + var->index]))
551 if (isl_tab_var_from_row(tab, i)->is_nonneg)
557 /* Check if there are any lower bounds on column variable "var",
558 * i.e., non-negative rows where var appears with a positive coefficient.
559 * Return 1 if there are no such bounds.
561 static int min_is_manifestly_unbounded(struct isl_tab *tab,
562 struct isl_tab_var *var)
565 unsigned off = 2 + tab->M;
569 for (i = tab->n_redundant; i < tab->n_row; ++i) {
570 if (!isl_int_is_pos(tab->mat->row[i][off + var->index]))
572 if (isl_tab_var_from_row(tab, i)->is_nonneg)
578 static int row_cmp(struct isl_tab *tab, int r1, int r2, int c, isl_int t)
580 unsigned off = 2 + tab->M;
584 isl_int_mul(t, tab->mat->row[r1][2], tab->mat->row[r2][off+c]);
585 isl_int_submul(t, tab->mat->row[r2][2], tab->mat->row[r1][off+c]);
590 isl_int_mul(t, tab->mat->row[r1][1], tab->mat->row[r2][off + c]);
591 isl_int_submul(t, tab->mat->row[r2][1], tab->mat->row[r1][off + c]);
592 return isl_int_sgn(t);
595 /* Given the index of a column "c", return the index of a row
596 * that can be used to pivot the column in, with either an increase
597 * (sgn > 0) or a decrease (sgn < 0) of the corresponding variable.
598 * If "var" is not NULL, then the row returned will be different from
599 * the one associated with "var".
601 * Each row in the tableau is of the form
603 * x_r = a_r0 + \sum_i a_ri x_i
605 * Only rows with x_r >= 0 and with the sign of a_ri opposite to "sgn"
606 * impose any limit on the increase or decrease in the value of x_c
607 * and this bound is equal to a_r0 / |a_rc|. We are therefore looking
608 * for the row with the smallest (most stringent) such bound.
609 * Note that the common denominator of each row drops out of the fraction.
610 * To check if row j has a smaller bound than row r, i.e.,
611 * a_j0 / |a_jc| < a_r0 / |a_rc| or a_j0 |a_rc| < a_r0 |a_jc|,
612 * we check if -sign(a_jc) (a_j0 a_rc - a_r0 a_jc) < 0,
613 * where -sign(a_jc) is equal to "sgn".
615 static int pivot_row(struct isl_tab *tab,
616 struct isl_tab_var *var, int sgn, int c)
620 unsigned off = 2 + tab->M;
624 for (j = tab->n_redundant; j < tab->n_row; ++j) {
625 if (var && j == var->index)
627 if (!isl_tab_var_from_row(tab, j)->is_nonneg)
629 if (sgn * isl_int_sgn(tab->mat->row[j][off + c]) >= 0)
635 tsgn = sgn * row_cmp(tab, r, j, c, t);
636 if (tsgn < 0 || (tsgn == 0 &&
637 tab->row_var[j] < tab->row_var[r]))
644 /* Find a pivot (row and col) that will increase (sgn > 0) or decrease
645 * (sgn < 0) the value of row variable var.
646 * If not NULL, then skip_var is a row variable that should be ignored
647 * while looking for a pivot row. It is usually equal to var.
649 * As the given row in the tableau is of the form
651 * x_r = a_r0 + \sum_i a_ri x_i
653 * we need to find a column such that the sign of a_ri is equal to "sgn"
654 * (such that an increase in x_i will have the desired effect) or a
655 * column with a variable that may attain negative values.
656 * If a_ri is positive, then we need to move x_i in the same direction
657 * to obtain the desired effect. Otherwise, x_i has to move in the
658 * opposite direction.
660 static void find_pivot(struct isl_tab *tab,
661 struct isl_tab_var *var, struct isl_tab_var *skip_var,
662 int sgn, int *row, int *col)
669 isl_assert(tab->mat->ctx, var->is_row, return);
670 tr = tab->mat->row[var->index] + 2 + tab->M;
673 for (j = tab->n_dead; j < tab->n_col; ++j) {
674 if (isl_int_is_zero(tr[j]))
676 if (isl_int_sgn(tr[j]) != sgn &&
677 var_from_col(tab, j)->is_nonneg)
679 if (c < 0 || tab->col_var[j] < tab->col_var[c])
685 sgn *= isl_int_sgn(tr[c]);
686 r = pivot_row(tab, skip_var, sgn, c);
687 *row = r < 0 ? var->index : r;
691 /* Return 1 if row "row" represents an obviously redundant inequality.
693 * - it represents an inequality or a variable
694 * - that is the sum of a non-negative sample value and a positive
695 * combination of zero or more non-negative variables.
697 int isl_tab_row_is_redundant(struct isl_tab *tab, int row)
700 unsigned off = 2 + tab->M;
702 if (tab->row_var[row] < 0 && !isl_tab_var_from_row(tab, row)->is_nonneg)
705 if (isl_int_is_neg(tab->mat->row[row][1]))
707 if (tab->M && isl_int_is_neg(tab->mat->row[row][2]))
710 for (i = tab->n_dead; i < tab->n_col; ++i) {
711 if (isl_int_is_zero(tab->mat->row[row][off + i]))
713 if (isl_int_is_neg(tab->mat->row[row][off + i]))
715 if (!var_from_col(tab, i)->is_nonneg)
721 static void swap_rows(struct isl_tab *tab, int row1, int row2)
724 t = tab->row_var[row1];
725 tab->row_var[row1] = tab->row_var[row2];
726 tab->row_var[row2] = t;
727 isl_tab_var_from_row(tab, row1)->index = row1;
728 isl_tab_var_from_row(tab, row2)->index = row2;
729 tab->mat = isl_mat_swap_rows(tab->mat, row1, row2);
733 t = tab->row_sign[row1];
734 tab->row_sign[row1] = tab->row_sign[row2];
735 tab->row_sign[row2] = t;
738 static void push_union(struct isl_tab *tab,
739 enum isl_tab_undo_type type, union isl_tab_undo_val u)
741 struct isl_tab_undo *undo;
746 undo = isl_alloc_type(tab->mat->ctx, struct isl_tab_undo);
754 undo->next = tab->top;
758 void isl_tab_push_var(struct isl_tab *tab,
759 enum isl_tab_undo_type type, struct isl_tab_var *var)
761 union isl_tab_undo_val u;
763 u.var_index = tab->row_var[var->index];
765 u.var_index = tab->col_var[var->index];
766 push_union(tab, type, u);
769 void isl_tab_push(struct isl_tab *tab, enum isl_tab_undo_type type)
771 union isl_tab_undo_val u = { 0 };
772 push_union(tab, type, u);
775 /* Push a record on the undo stack describing the current basic
776 * variables, so that the this state can be restored during rollback.
778 void isl_tab_push_basis(struct isl_tab *tab)
781 union isl_tab_undo_val u;
783 u.col_var = isl_alloc_array(tab->mat->ctx, int, tab->n_col);
789 for (i = 0; i < tab->n_col; ++i)
790 u.col_var[i] = tab->col_var[i];
791 push_union(tab, isl_tab_undo_saved_basis, u);
794 /* Mark row with index "row" as being redundant.
795 * If we may need to undo the operation or if the row represents
796 * a variable of the original problem, the row is kept,
797 * but no longer considered when looking for a pivot row.
798 * Otherwise, the row is simply removed.
800 * The row may be interchanged with some other row. If it
801 * is interchanged with a later row, return 1. Otherwise return 0.
802 * If the rows are checked in order in the calling function,
803 * then a return value of 1 means that the row with the given
804 * row number may now contain a different row that hasn't been checked yet.
806 int isl_tab_mark_redundant(struct isl_tab *tab, int row)
808 struct isl_tab_var *var = isl_tab_var_from_row(tab, row);
809 var->is_redundant = 1;
810 isl_assert(tab->mat->ctx, row >= tab->n_redundant, return -1);
811 if (tab->need_undo || tab->row_var[row] >= 0) {
812 if (tab->row_var[row] >= 0 && !var->is_nonneg) {
814 isl_tab_push_var(tab, isl_tab_undo_nonneg, var);
816 if (row != tab->n_redundant)
817 swap_rows(tab, row, tab->n_redundant);
818 isl_tab_push_var(tab, isl_tab_undo_redundant, var);
822 if (row != tab->n_row - 1)
823 swap_rows(tab, row, tab->n_row - 1);
824 isl_tab_var_from_row(tab, tab->n_row - 1)->index = -1;
830 struct isl_tab *isl_tab_mark_empty(struct isl_tab *tab)
832 if (!tab->empty && tab->need_undo)
833 isl_tab_push(tab, isl_tab_undo_empty);
838 /* Update the rows signs after a pivot of "row" and "col", with "row_sgn"
839 * the original sign of the pivot element.
840 * We only keep track of row signs during PILP solving and in this case
841 * we only pivot a row with negative sign (meaning the value is always
842 * non-positive) using a positive pivot element.
844 * For each row j, the new value of the parametric constant is equal to
846 * a_j0 - a_jc a_r0/a_rc
848 * where a_j0 is the original parametric constant, a_rc is the pivot element,
849 * a_r0 is the parametric constant of the pivot row and a_jc is the
850 * pivot column entry of the row j.
851 * Since a_r0 is non-positive and a_rc is positive, the sign of row j
852 * remains the same if a_jc has the same sign as the row j or if
853 * a_jc is zero. In all other cases, we reset the sign to "unknown".
855 static void update_row_sign(struct isl_tab *tab, int row, int col, int row_sgn)
858 struct isl_mat *mat = tab->mat;
859 unsigned off = 2 + tab->M;
864 if (tab->row_sign[row] == 0)
866 isl_assert(mat->ctx, row_sgn > 0, return);
867 isl_assert(mat->ctx, tab->row_sign[row] == isl_tab_row_neg, return);
868 tab->row_sign[row] = isl_tab_row_pos;
869 for (i = 0; i < tab->n_row; ++i) {
873 s = isl_int_sgn(mat->row[i][off + col]);
876 if (!tab->row_sign[i])
878 if (s < 0 && tab->row_sign[i] == isl_tab_row_neg)
880 if (s > 0 && tab->row_sign[i] == isl_tab_row_pos)
882 tab->row_sign[i] = isl_tab_row_unknown;
886 /* Given a row number "row" and a column number "col", pivot the tableau
887 * such that the associated variables are interchanged.
888 * The given row in the tableau expresses
890 * x_r = a_r0 + \sum_i a_ri x_i
894 * x_c = 1/a_rc x_r - a_r0/a_rc + sum_{i \ne r} -a_ri/a_rc
896 * Substituting this equality into the other rows
898 * x_j = a_j0 + \sum_i a_ji x_i
900 * with a_jc \ne 0, we obtain
902 * x_j = a_jc/a_rc x_r + a_j0 - a_jc a_r0/a_rc + sum a_ji - a_jc a_ri/a_rc
909 * where i is any other column and j is any other row,
910 * is therefore transformed into
912 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
913 * 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)
915 * The transformation is performed along the following steps
920 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
923 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
924 * n_jc/(|n_rc| d_j) n_ji/(|n_rc| d_j)
926 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
927 * n_jc/(|n_rc| d_j) (n_ji |n_rc|)/(|n_rc| d_j)
929 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
930 * n_jc/(|n_rc| d_j) (n_ji |n_rc| - s(n_rc)n_jc n_ri)/(|n_rc| d_j)
932 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
933 * 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)
936 void isl_tab_pivot(struct isl_tab *tab, int row, int col)
941 struct isl_mat *mat = tab->mat;
942 struct isl_tab_var *var;
943 unsigned off = 2 + tab->M;
945 isl_int_swap(mat->row[row][0], mat->row[row][off + col]);
946 sgn = isl_int_sgn(mat->row[row][0]);
948 isl_int_neg(mat->row[row][0], mat->row[row][0]);
949 isl_int_neg(mat->row[row][off + col], mat->row[row][off + col]);
951 for (j = 0; j < off - 1 + tab->n_col; ++j) {
952 if (j == off - 1 + col)
954 isl_int_neg(mat->row[row][1 + j], mat->row[row][1 + j]);
956 if (!isl_int_is_one(mat->row[row][0]))
957 isl_seq_normalize(mat->ctx, mat->row[row], off + tab->n_col);
958 for (i = 0; i < tab->n_row; ++i) {
961 if (isl_int_is_zero(mat->row[i][off + col]))
963 isl_int_mul(mat->row[i][0], mat->row[i][0], mat->row[row][0]);
964 for (j = 0; j < off - 1 + tab->n_col; ++j) {
965 if (j == off - 1 + col)
967 isl_int_mul(mat->row[i][1 + j],
968 mat->row[i][1 + j], mat->row[row][0]);
969 isl_int_addmul(mat->row[i][1 + j],
970 mat->row[i][off + col], mat->row[row][1 + j]);
972 isl_int_mul(mat->row[i][off + col],
973 mat->row[i][off + col], mat->row[row][off + col]);
974 if (!isl_int_is_one(mat->row[i][0]))
975 isl_seq_normalize(mat->ctx, mat->row[i], off + tab->n_col);
977 t = tab->row_var[row];
978 tab->row_var[row] = tab->col_var[col];
979 tab->col_var[col] = t;
980 var = isl_tab_var_from_row(tab, row);
983 var = var_from_col(tab, col);
986 update_row_sign(tab, row, col, sgn);
989 for (i = tab->n_redundant; i < tab->n_row; ++i) {
990 if (isl_int_is_zero(mat->row[i][off + col]))
992 if (!isl_tab_var_from_row(tab, i)->frozen &&
993 isl_tab_row_is_redundant(tab, i))
994 if (isl_tab_mark_redundant(tab, i))
999 /* If "var" represents a column variable, then pivot is up (sgn > 0)
1000 * or down (sgn < 0) to a row. The variable is assumed not to be
1001 * unbounded in the specified direction.
1002 * If sgn = 0, then the variable is unbounded in both directions,
1003 * and we pivot with any row we can find.
1005 static void to_row(struct isl_tab *tab, struct isl_tab_var *var, int sign)
1008 unsigned off = 2 + tab->M;
1014 for (r = tab->n_redundant; r < tab->n_row; ++r)
1015 if (!isl_int_is_zero(tab->mat->row[r][off+var->index]))
1017 isl_assert(tab->mat->ctx, r < tab->n_row, return);
1019 r = pivot_row(tab, NULL, sign, var->index);
1020 isl_assert(tab->mat->ctx, r >= 0, return);
1023 isl_tab_pivot(tab, r, var->index);
1026 static void check_table(struct isl_tab *tab)
1032 for (i = 0; i < tab->n_row; ++i) {
1033 if (!isl_tab_var_from_row(tab, i)->is_nonneg)
1035 assert(!isl_int_is_neg(tab->mat->row[i][1]));
1039 /* Return the sign of the maximal value of "var".
1040 * If the sign is not negative, then on return from this function,
1041 * the sample value will also be non-negative.
1043 * If "var" is manifestly unbounded wrt positive values, we are done.
1044 * Otherwise, we pivot the variable up to a row if needed
1045 * Then we continue pivoting down until either
1046 * - no more down pivots can be performed
1047 * - the sample value is positive
1048 * - the variable is pivoted into a manifestly unbounded column
1050 static int sign_of_max(struct isl_tab *tab, struct isl_tab_var *var)
1054 if (max_is_manifestly_unbounded(tab, var))
1056 to_row(tab, var, 1);
1057 while (!isl_int_is_pos(tab->mat->row[var->index][1])) {
1058 find_pivot(tab, var, var, 1, &row, &col);
1060 return isl_int_sgn(tab->mat->row[var->index][1]);
1061 isl_tab_pivot(tab, row, col);
1062 if (!var->is_row) /* manifestly unbounded */
1068 static int row_is_neg(struct isl_tab *tab, int row)
1071 return isl_int_is_neg(tab->mat->row[row][1]);
1072 if (isl_int_is_pos(tab->mat->row[row][2]))
1074 if (isl_int_is_neg(tab->mat->row[row][2]))
1076 return isl_int_is_neg(tab->mat->row[row][1]);
1079 static int row_sgn(struct isl_tab *tab, int row)
1082 return isl_int_sgn(tab->mat->row[row][1]);
1083 if (!isl_int_is_zero(tab->mat->row[row][2]))
1084 return isl_int_sgn(tab->mat->row[row][2]);
1086 return isl_int_sgn(tab->mat->row[row][1]);
1089 /* Perform pivots until the row variable "var" has a non-negative
1090 * sample value or until no more upward pivots can be performed.
1091 * Return the sign of the sample value after the pivots have been
1094 static int restore_row(struct isl_tab *tab, struct isl_tab_var *var)
1098 while (row_is_neg(tab, var->index)) {
1099 find_pivot(tab, var, var, 1, &row, &col);
1102 isl_tab_pivot(tab, row, col);
1103 if (!var->is_row) /* manifestly unbounded */
1106 return row_sgn(tab, var->index);
1109 /* Perform pivots until we are sure that the row variable "var"
1110 * can attain non-negative values. After return from this
1111 * function, "var" is still a row variable, but its sample
1112 * value may not be non-negative, even if the function returns 1.
1114 static int at_least_zero(struct isl_tab *tab, struct isl_tab_var *var)
1118 while (isl_int_is_neg(tab->mat->row[var->index][1])) {
1119 find_pivot(tab, var, var, 1, &row, &col);
1122 if (row == var->index) /* manifestly unbounded */
1124 isl_tab_pivot(tab, row, col);
1126 return !isl_int_is_neg(tab->mat->row[var->index][1]);
1129 /* Return a negative value if "var" can attain negative values.
1130 * Return a non-negative value otherwise.
1132 * If "var" is manifestly unbounded wrt negative values, we are done.
1133 * Otherwise, if var is in a column, we can pivot it down to a row.
1134 * Then we continue pivoting down until either
1135 * - the pivot would result in a manifestly unbounded column
1136 * => we don't perform the pivot, but simply return -1
1137 * - no more down pivots can be performed
1138 * - the sample value is negative
1139 * If the sample value becomes negative and the variable is supposed
1140 * to be nonnegative, then we undo the last pivot.
1141 * However, if the last pivot has made the pivoting variable
1142 * obviously redundant, then it may have moved to another row.
1143 * In that case we look for upward pivots until we reach a non-negative
1146 static int sign_of_min(struct isl_tab *tab, struct isl_tab_var *var)
1149 struct isl_tab_var *pivot_var = NULL;
1151 if (min_is_manifestly_unbounded(tab, var))
1155 row = pivot_row(tab, NULL, -1, col);
1156 pivot_var = var_from_col(tab, col);
1157 isl_tab_pivot(tab, row, col);
1158 if (var->is_redundant)
1160 if (isl_int_is_neg(tab->mat->row[var->index][1])) {
1161 if (var->is_nonneg) {
1162 if (!pivot_var->is_redundant &&
1163 pivot_var->index == row)
1164 isl_tab_pivot(tab, row, col);
1166 restore_row(tab, var);
1171 if (var->is_redundant)
1173 while (!isl_int_is_neg(tab->mat->row[var->index][1])) {
1174 find_pivot(tab, var, var, -1, &row, &col);
1175 if (row == var->index)
1178 return isl_int_sgn(tab->mat->row[var->index][1]);
1179 pivot_var = var_from_col(tab, col);
1180 isl_tab_pivot(tab, row, col);
1181 if (var->is_redundant)
1184 if (pivot_var && var->is_nonneg) {
1185 /* pivot back to non-negative value */
1186 if (!pivot_var->is_redundant && pivot_var->index == row)
1187 isl_tab_pivot(tab, row, col);
1189 restore_row(tab, var);
1194 static int row_at_most_neg_one(struct isl_tab *tab, int row)
1197 if (isl_int_is_pos(tab->mat->row[row][2]))
1199 if (isl_int_is_neg(tab->mat->row[row][2]))
1202 return isl_int_is_neg(tab->mat->row[row][1]) &&
1203 isl_int_abs_ge(tab->mat->row[row][1],
1204 tab->mat->row[row][0]);
1207 /* Return 1 if "var" can attain values <= -1.
1208 * Return 0 otherwise.
1210 * The sample value of "var" is assumed to be non-negative when the
1211 * the function is called and will be made non-negative again before
1212 * the function returns.
1214 int isl_tab_min_at_most_neg_one(struct isl_tab *tab, struct isl_tab_var *var)
1217 struct isl_tab_var *pivot_var;
1219 if (min_is_manifestly_unbounded(tab, var))
1223 row = pivot_row(tab, NULL, -1, col);
1224 pivot_var = var_from_col(tab, col);
1225 isl_tab_pivot(tab, row, col);
1226 if (var->is_redundant)
1228 if (row_at_most_neg_one(tab, var->index)) {
1229 if (var->is_nonneg) {
1230 if (!pivot_var->is_redundant &&
1231 pivot_var->index == row)
1232 isl_tab_pivot(tab, row, col);
1234 restore_row(tab, var);
1239 if (var->is_redundant)
1242 find_pivot(tab, var, var, -1, &row, &col);
1243 if (row == var->index)
1247 pivot_var = var_from_col(tab, col);
1248 isl_tab_pivot(tab, row, col);
1249 if (var->is_redundant)
1251 } while (!row_at_most_neg_one(tab, var->index));
1252 if (var->is_nonneg) {
1253 /* pivot back to non-negative value */
1254 if (!pivot_var->is_redundant && pivot_var->index == row)
1255 isl_tab_pivot(tab, row, col);
1256 restore_row(tab, var);
1261 /* Return 1 if "var" can attain values >= 1.
1262 * Return 0 otherwise.
1264 static int at_least_one(struct isl_tab *tab, struct isl_tab_var *var)
1269 if (max_is_manifestly_unbounded(tab, var))
1271 to_row(tab, var, 1);
1272 r = tab->mat->row[var->index];
1273 while (isl_int_lt(r[1], r[0])) {
1274 find_pivot(tab, var, var, 1, &row, &col);
1276 return isl_int_ge(r[1], r[0]);
1277 if (row == var->index) /* manifestly unbounded */
1279 isl_tab_pivot(tab, row, col);
1284 static void swap_cols(struct isl_tab *tab, int col1, int col2)
1287 unsigned off = 2 + tab->M;
1288 t = tab->col_var[col1];
1289 tab->col_var[col1] = tab->col_var[col2];
1290 tab->col_var[col2] = t;
1291 var_from_col(tab, col1)->index = col1;
1292 var_from_col(tab, col2)->index = col2;
1293 tab->mat = isl_mat_swap_cols(tab->mat, off + col1, off + col2);
1296 /* Mark column with index "col" as representing a zero variable.
1297 * If we may need to undo the operation the column is kept,
1298 * but no longer considered.
1299 * Otherwise, the column is simply removed.
1301 * The column may be interchanged with some other column. If it
1302 * is interchanged with a later column, return 1. Otherwise return 0.
1303 * If the columns are checked in order in the calling function,
1304 * then a return value of 1 means that the column with the given
1305 * column number may now contain a different column that
1306 * hasn't been checked yet.
1308 int isl_tab_kill_col(struct isl_tab *tab, int col)
1310 var_from_col(tab, col)->is_zero = 1;
1311 if (tab->need_undo) {
1312 isl_tab_push_var(tab, isl_tab_undo_zero, var_from_col(tab, col));
1313 if (col != tab->n_dead)
1314 swap_cols(tab, col, tab->n_dead);
1318 if (col != tab->n_col - 1)
1319 swap_cols(tab, col, tab->n_col - 1);
1320 var_from_col(tab, tab->n_col - 1)->index = -1;
1326 /* Row variable "var" is non-negative and cannot attain any values
1327 * larger than zero. This means that the coefficients of the unrestricted
1328 * column variables are zero and that the coefficients of the non-negative
1329 * column variables are zero or negative.
1330 * Each of the non-negative variables with a negative coefficient can
1331 * then also be written as the negative sum of non-negative variables
1332 * and must therefore also be zero.
1334 static void close_row(struct isl_tab *tab, struct isl_tab_var *var)
1337 struct isl_mat *mat = tab->mat;
1338 unsigned off = 2 + tab->M;
1340 isl_assert(tab->mat->ctx, var->is_nonneg, return);
1343 isl_tab_push_var(tab, isl_tab_undo_zero, var);
1344 for (j = tab->n_dead; j < tab->n_col; ++j) {
1345 if (isl_int_is_zero(mat->row[var->index][off + j]))
1347 isl_assert(tab->mat->ctx,
1348 isl_int_is_neg(mat->row[var->index][off + j]), return);
1349 if (isl_tab_kill_col(tab, j))
1352 isl_tab_mark_redundant(tab, var->index);
1355 /* Add a constraint to the tableau and allocate a row for it.
1356 * Return the index into the constraint array "con".
1358 int isl_tab_allocate_con(struct isl_tab *tab)
1362 isl_assert(tab->mat->ctx, tab->n_row < tab->mat->n_row, return -1);
1363 isl_assert(tab->mat->ctx, tab->n_con < tab->max_con, return -1);
1366 tab->con[r].index = tab->n_row;
1367 tab->con[r].is_row = 1;
1368 tab->con[r].is_nonneg = 0;
1369 tab->con[r].is_zero = 0;
1370 tab->con[r].is_redundant = 0;
1371 tab->con[r].frozen = 0;
1372 tab->con[r].negated = 0;
1373 tab->row_var[tab->n_row] = ~r;
1377 isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->con[r]);
1382 /* Add a variable to the tableau and allocate a column for it.
1383 * Return the index into the variable array "var".
1385 int isl_tab_allocate_var(struct isl_tab *tab)
1389 unsigned off = 2 + tab->M;
1391 isl_assert(tab->mat->ctx, tab->n_col < tab->mat->n_col, return -1);
1392 isl_assert(tab->mat->ctx, tab->n_var < tab->max_var, return -1);
1395 tab->var[r].index = tab->n_col;
1396 tab->var[r].is_row = 0;
1397 tab->var[r].is_nonneg = 0;
1398 tab->var[r].is_zero = 0;
1399 tab->var[r].is_redundant = 0;
1400 tab->var[r].frozen = 0;
1401 tab->var[r].negated = 0;
1402 tab->col_var[tab->n_col] = r;
1404 for (i = 0; i < tab->n_row; ++i)
1405 isl_int_set_si(tab->mat->row[i][off + tab->n_col], 0);
1409 isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->var[r]);
1414 /* Add a row to the tableau. The row is given as an affine combination
1415 * of the original variables and needs to be expressed in terms of the
1418 * We add each term in turn.
1419 * If r = n/d_r is the current sum and we need to add k x, then
1420 * if x is a column variable, we increase the numerator of
1421 * this column by k d_r
1422 * if x = f/d_x is a row variable, then the new representation of r is
1424 * n k f d_x/g n + d_r/g k f m/d_r n + m/d_g k f
1425 * --- + --- = ------------------- = -------------------
1426 * d_r d_r d_r d_x/g m
1428 * with g the gcd of d_r and d_x and m the lcm of d_r and d_x.
1430 int isl_tab_add_row(struct isl_tab *tab, isl_int *line)
1436 unsigned off = 2 + tab->M;
1438 r = isl_tab_allocate_con(tab);
1444 row = tab->mat->row[tab->con[r].index];
1445 isl_int_set_si(row[0], 1);
1446 isl_int_set(row[1], line[0]);
1447 isl_seq_clr(row + 2, tab->M + tab->n_col);
1448 for (i = 0; i < tab->n_var; ++i) {
1449 if (tab->var[i].is_zero)
1451 if (tab->var[i].is_row) {
1453 row[0], tab->mat->row[tab->var[i].index][0]);
1454 isl_int_swap(a, row[0]);
1455 isl_int_divexact(a, row[0], a);
1457 row[0], tab->mat->row[tab->var[i].index][0]);
1458 isl_int_mul(b, b, line[1 + i]);
1459 isl_seq_combine(row + 1, a, row + 1,
1460 b, tab->mat->row[tab->var[i].index] + 1,
1461 1 + tab->M + tab->n_col);
1463 isl_int_addmul(row[off + tab->var[i].index],
1464 line[1 + i], row[0]);
1465 if (tab->M && i >= tab->n_param && i < tab->n_var - tab->n_div)
1466 isl_int_submul(row[2], line[1 + i], row[0]);
1468 isl_seq_normalize(tab->mat->ctx, row, off + tab->n_col);
1473 tab->row_sign[tab->con[r].index] = 0;
1478 static int drop_row(struct isl_tab *tab, int row)
1480 isl_assert(tab->mat->ctx, ~tab->row_var[row] == tab->n_con - 1, return -1);
1481 if (row != tab->n_row - 1)
1482 swap_rows(tab, row, tab->n_row - 1);
1488 static int drop_col(struct isl_tab *tab, int col)
1490 isl_assert(tab->mat->ctx, tab->col_var[col] == tab->n_var - 1, return -1);
1491 if (col != tab->n_col - 1)
1492 swap_cols(tab, col, tab->n_col - 1);
1498 /* Add inequality "ineq" and check if it conflicts with the
1499 * previously added constraints or if it is obviously redundant.
1501 struct isl_tab *isl_tab_add_ineq(struct isl_tab *tab, isl_int *ineq)
1509 struct isl_basic_set *bset = tab->bset;
1511 isl_assert(tab->mat->ctx, tab->n_eq == bset->n_eq, goto error);
1512 isl_assert(tab->mat->ctx,
1513 tab->n_con == bset->n_eq + bset->n_ineq, goto error);
1514 tab->bset = isl_basic_set_add_ineq(tab->bset, ineq);
1515 isl_tab_push(tab, isl_tab_undo_bset_ineq);
1519 r = isl_tab_add_row(tab, ineq);
1522 tab->con[r].is_nonneg = 1;
1523 isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]);
1524 if (isl_tab_row_is_redundant(tab, tab->con[r].index)) {
1525 isl_tab_mark_redundant(tab, tab->con[r].index);
1529 sgn = restore_row(tab, &tab->con[r]);
1531 return isl_tab_mark_empty(tab);
1532 if (tab->con[r].is_row && isl_tab_row_is_redundant(tab, tab->con[r].index))
1533 isl_tab_mark_redundant(tab, tab->con[r].index);
1540 /* Pivot a non-negative variable down until it reaches the value zero
1541 * and then pivot the variable into a column position.
1543 static int to_col(struct isl_tab *tab, struct isl_tab_var *var)
1547 unsigned off = 2 + tab->M;
1552 while (isl_int_is_pos(tab->mat->row[var->index][1])) {
1553 find_pivot(tab, var, NULL, -1, &row, &col);
1554 isl_assert(tab->mat->ctx, row != -1, return -1);
1555 isl_tab_pivot(tab, row, col);
1560 for (i = tab->n_dead; i < tab->n_col; ++i)
1561 if (!isl_int_is_zero(tab->mat->row[var->index][off + i]))
1564 isl_assert(tab->mat->ctx, i < tab->n_col, return -1);
1565 isl_tab_pivot(tab, var->index, i);
1570 /* We assume Gaussian elimination has been performed on the equalities.
1571 * The equalities can therefore never conflict.
1572 * Adding the equalities is currently only really useful for a later call
1573 * to isl_tab_ineq_type.
1575 static struct isl_tab *add_eq(struct isl_tab *tab, isl_int *eq)
1582 r = isl_tab_add_row(tab, eq);
1586 r = tab->con[r].index;
1587 i = isl_seq_first_non_zero(tab->mat->row[r] + 2 + tab->M + tab->n_dead,
1588 tab->n_col - tab->n_dead);
1589 isl_assert(tab->mat->ctx, i >= 0, goto error);
1591 isl_tab_pivot(tab, r, i);
1592 isl_tab_kill_col(tab, i);
1601 static int row_is_manifestly_zero(struct isl_tab *tab, int row)
1603 unsigned off = 2 + tab->M;
1605 if (!isl_int_is_zero(tab->mat->row[row][1]))
1607 if (tab->M && !isl_int_is_zero(tab->mat->row[row][2]))
1609 return isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
1610 tab->n_col - tab->n_dead) == -1;
1613 /* Add an equality that is known to be valid for the given tableau.
1615 struct isl_tab *isl_tab_add_valid_eq(struct isl_tab *tab, isl_int *eq)
1617 struct isl_tab_var *var;
1622 r = isl_tab_add_row(tab, eq);
1628 if (row_is_manifestly_zero(tab, r)) {
1630 isl_tab_mark_redundant(tab, r);
1634 if (isl_int_is_neg(tab->mat->row[r][1])) {
1635 isl_seq_neg(tab->mat->row[r] + 1, tab->mat->row[r] + 1,
1640 if (to_col(tab, var) < 0)
1643 isl_tab_kill_col(tab, var->index);
1651 static int add_zero_row(struct isl_tab *tab)
1656 r = isl_tab_allocate_con(tab);
1660 row = tab->mat->row[tab->con[r].index];
1661 isl_seq_clr(row + 1, 1 + tab->M + tab->n_col);
1662 isl_int_set_si(row[0], 1);
1667 /* Add equality "eq" and check if it conflicts with the
1668 * previously added constraints or if it is obviously redundant.
1670 struct isl_tab *isl_tab_add_eq(struct isl_tab *tab, isl_int *eq)
1672 struct isl_tab_undo *snap = NULL;
1673 struct isl_tab_var *var;
1680 isl_assert(tab->mat->ctx, !tab->M, goto error);
1683 snap = isl_tab_snap(tab);
1685 r = isl_tab_add_row(tab, eq);
1691 if (row_is_manifestly_zero(tab, row)) {
1693 if (isl_tab_rollback(tab, snap) < 0)
1701 tab->bset = isl_basic_set_add_ineq(tab->bset, eq);
1702 isl_tab_push(tab, isl_tab_undo_bset_ineq);
1703 isl_seq_neg(eq, eq, 1 + tab->n_var);
1704 tab->bset = isl_basic_set_add_ineq(tab->bset, eq);
1705 isl_seq_neg(eq, eq, 1 + tab->n_var);
1706 isl_tab_push(tab, isl_tab_undo_bset_ineq);
1709 if (add_zero_row(tab) < 0)
1713 sgn = isl_int_sgn(tab->mat->row[row][1]);
1716 isl_seq_neg(tab->mat->row[row] + 1, tab->mat->row[row] + 1,
1722 if (sgn < 0 && sign_of_max(tab, var) < 0)
1723 return isl_tab_mark_empty(tab);
1726 if (to_col(tab, var) < 0)
1729 isl_tab_kill_col(tab, var->index);
1737 struct isl_tab *isl_tab_from_basic_map(struct isl_basic_map *bmap)
1740 struct isl_tab *tab;
1744 tab = isl_tab_alloc(bmap->ctx,
1745 isl_basic_map_total_dim(bmap) + bmap->n_ineq + 1,
1746 isl_basic_map_total_dim(bmap), 0);
1749 tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
1750 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1751 return isl_tab_mark_empty(tab);
1752 for (i = 0; i < bmap->n_eq; ++i) {
1753 tab = add_eq(tab, bmap->eq[i]);
1757 for (i = 0; i < bmap->n_ineq; ++i) {
1758 tab = isl_tab_add_ineq(tab, bmap->ineq[i]);
1759 if (!tab || tab->empty)
1765 struct isl_tab *isl_tab_from_basic_set(struct isl_basic_set *bset)
1767 return isl_tab_from_basic_map((struct isl_basic_map *)bset);
1770 /* Construct a tableau corresponding to the recession cone of "bset".
1772 struct isl_tab *isl_tab_from_recession_cone(struct isl_basic_set *bset)
1776 struct isl_tab *tab;
1780 tab = isl_tab_alloc(bset->ctx, bset->n_eq + bset->n_ineq,
1781 isl_basic_set_total_dim(bset), 0);
1784 tab->rational = ISL_F_ISSET(bset, ISL_BASIC_SET_RATIONAL);
1787 for (i = 0; i < bset->n_eq; ++i) {
1788 isl_int_swap(bset->eq[i][0], cst);
1789 tab = add_eq(tab, bset->eq[i]);
1790 isl_int_swap(bset->eq[i][0], cst);
1794 for (i = 0; i < bset->n_ineq; ++i) {
1796 isl_int_swap(bset->ineq[i][0], cst);
1797 r = isl_tab_add_row(tab, bset->ineq[i]);
1798 isl_int_swap(bset->ineq[i][0], cst);
1801 tab->con[r].is_nonneg = 1;
1802 isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]);
1813 /* Assuming "tab" is the tableau of a cone, check if the cone is
1814 * bounded, i.e., if it is empty or only contains the origin.
1816 int isl_tab_cone_is_bounded(struct isl_tab *tab)
1824 if (tab->n_dead == tab->n_col)
1828 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1829 struct isl_tab_var *var;
1830 var = isl_tab_var_from_row(tab, i);
1831 if (!var->is_nonneg)
1833 if (sign_of_max(tab, var) != 0)
1835 close_row(tab, var);
1838 if (tab->n_dead == tab->n_col)
1840 if (i == tab->n_row)
1845 int isl_tab_sample_is_integer(struct isl_tab *tab)
1852 for (i = 0; i < tab->n_var; ++i) {
1854 if (!tab->var[i].is_row)
1856 row = tab->var[i].index;
1857 if (!isl_int_is_divisible_by(tab->mat->row[row][1],
1858 tab->mat->row[row][0]))
1864 static struct isl_vec *extract_integer_sample(struct isl_tab *tab)
1867 struct isl_vec *vec;
1869 vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
1873 isl_int_set_si(vec->block.data[0], 1);
1874 for (i = 0; i < tab->n_var; ++i) {
1875 if (!tab->var[i].is_row)
1876 isl_int_set_si(vec->block.data[1 + i], 0);
1878 int row = tab->var[i].index;
1879 isl_int_divexact(vec->block.data[1 + i],
1880 tab->mat->row[row][1], tab->mat->row[row][0]);
1887 struct isl_vec *isl_tab_get_sample_value(struct isl_tab *tab)
1890 struct isl_vec *vec;
1896 vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
1902 isl_int_set_si(vec->block.data[0], 1);
1903 for (i = 0; i < tab->n_var; ++i) {
1905 if (!tab->var[i].is_row) {
1906 isl_int_set_si(vec->block.data[1 + i], 0);
1909 row = tab->var[i].index;
1910 isl_int_gcd(m, vec->block.data[0], tab->mat->row[row][0]);
1911 isl_int_divexact(m, tab->mat->row[row][0], m);
1912 isl_seq_scale(vec->block.data, vec->block.data, m, 1 + i);
1913 isl_int_divexact(m, vec->block.data[0], tab->mat->row[row][0]);
1914 isl_int_mul(vec->block.data[1 + i], m, tab->mat->row[row][1]);
1916 vec = isl_vec_normalize(vec);
1922 /* Update "bmap" based on the results of the tableau "tab".
1923 * In particular, implicit equalities are made explicit, redundant constraints
1924 * are removed and if the sample value happens to be integer, it is stored
1925 * in "bmap" (unless "bmap" already had an integer sample).
1927 * The tableau is assumed to have been created from "bmap" using
1928 * isl_tab_from_basic_map.
1930 struct isl_basic_map *isl_basic_map_update_from_tab(struct isl_basic_map *bmap,
1931 struct isl_tab *tab)
1943 bmap = isl_basic_map_set_to_empty(bmap);
1945 for (i = bmap->n_ineq - 1; i >= 0; --i) {
1946 if (isl_tab_is_equality(tab, n_eq + i))
1947 isl_basic_map_inequality_to_equality(bmap, i);
1948 else if (isl_tab_is_redundant(tab, n_eq + i))
1949 isl_basic_map_drop_inequality(bmap, i);
1951 if (!tab->rational &&
1952 !bmap->sample && isl_tab_sample_is_integer(tab))
1953 bmap->sample = extract_integer_sample(tab);
1957 struct isl_basic_set *isl_basic_set_update_from_tab(struct isl_basic_set *bset,
1958 struct isl_tab *tab)
1960 return (struct isl_basic_set *)isl_basic_map_update_from_tab(
1961 (struct isl_basic_map *)bset, tab);
1964 /* Given a non-negative variable "var", add a new non-negative variable
1965 * that is the opposite of "var", ensuring that var can only attain the
1967 * If var = n/d is a row variable, then the new variable = -n/d.
1968 * If var is a column variables, then the new variable = -var.
1969 * If the new variable cannot attain non-negative values, then
1970 * the resulting tableau is empty.
1971 * Otherwise, we know the value will be zero and we close the row.
1973 static struct isl_tab *cut_to_hyperplane(struct isl_tab *tab,
1974 struct isl_tab_var *var)
1979 unsigned off = 2 + tab->M;
1983 isl_assert(tab->mat->ctx, !var->is_redundant, goto error);
1985 if (isl_tab_extend_cons(tab, 1) < 0)
1989 tab->con[r].index = tab->n_row;
1990 tab->con[r].is_row = 1;
1991 tab->con[r].is_nonneg = 0;
1992 tab->con[r].is_zero = 0;
1993 tab->con[r].is_redundant = 0;
1994 tab->con[r].frozen = 0;
1995 tab->con[r].negated = 0;
1996 tab->row_var[tab->n_row] = ~r;
1997 row = tab->mat->row[tab->n_row];
2000 isl_int_set(row[0], tab->mat->row[var->index][0]);
2001 isl_seq_neg(row + 1,
2002 tab->mat->row[var->index] + 1, 1 + tab->n_col);
2004 isl_int_set_si(row[0], 1);
2005 isl_seq_clr(row + 1, 1 + tab->n_col);
2006 isl_int_set_si(row[off + var->index], -1);
2011 isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->con[r]);
2013 sgn = sign_of_max(tab, &tab->con[r]);
2015 return isl_tab_mark_empty(tab);
2016 tab->con[r].is_nonneg = 1;
2017 isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]);
2019 close_row(tab, &tab->con[r]);
2027 /* Given a tableau "tab" and an inequality constraint "con" of the tableau,
2028 * relax the inequality by one. That is, the inequality r >= 0 is replaced
2029 * by r' = r + 1 >= 0.
2030 * If r is a row variable, we simply increase the constant term by one
2031 * (taking into account the denominator).
2032 * If r is a column variable, then we need to modify each row that
2033 * refers to r = r' - 1 by substituting this equality, effectively
2034 * subtracting the coefficient of the column from the constant.
2036 struct isl_tab *isl_tab_relax(struct isl_tab *tab, int con)
2038 struct isl_tab_var *var;
2039 unsigned off = 2 + tab->M;
2044 var = &tab->con[con];
2046 if (!var->is_row && !max_is_manifestly_unbounded(tab, var))
2047 to_row(tab, var, 1);
2050 isl_int_add(tab->mat->row[var->index][1],
2051 tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
2055 for (i = 0; i < tab->n_row; ++i) {
2056 if (isl_int_is_zero(tab->mat->row[i][off + var->index]))
2058 isl_int_sub(tab->mat->row[i][1], tab->mat->row[i][1],
2059 tab->mat->row[i][off + var->index]);
2064 isl_tab_push_var(tab, isl_tab_undo_relax, var);
2069 struct isl_tab *isl_tab_select_facet(struct isl_tab *tab, int con)
2074 return cut_to_hyperplane(tab, &tab->con[con]);
2077 static int may_be_equality(struct isl_tab *tab, int row)
2079 unsigned off = 2 + tab->M;
2080 return (tab->rational ? isl_int_is_zero(tab->mat->row[row][1])
2081 : isl_int_lt(tab->mat->row[row][1],
2082 tab->mat->row[row][0])) &&
2083 isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
2084 tab->n_col - tab->n_dead) != -1;
2087 /* Check for (near) equalities among the constraints.
2088 * A constraint is an equality if it is non-negative and if
2089 * its maximal value is either
2090 * - zero (in case of rational tableaus), or
2091 * - strictly less than 1 (in case of integer tableaus)
2093 * We first mark all non-redundant and non-dead variables that
2094 * are not frozen and not obviously not an equality.
2095 * Then we iterate over all marked variables if they can attain
2096 * any values larger than zero or at least one.
2097 * If the maximal value is zero, we mark any column variables
2098 * that appear in the row as being zero and mark the row as being redundant.
2099 * Otherwise, if the maximal value is strictly less than one (and the
2100 * tableau is integer), then we restrict the value to being zero
2101 * by adding an opposite non-negative variable.
2103 struct isl_tab *isl_tab_detect_implicit_equalities(struct isl_tab *tab)
2112 if (tab->n_dead == tab->n_col)
2116 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2117 struct isl_tab_var *var = isl_tab_var_from_row(tab, i);
2118 var->marked = !var->frozen && var->is_nonneg &&
2119 may_be_equality(tab, i);
2123 for (i = tab->n_dead; i < tab->n_col; ++i) {
2124 struct isl_tab_var *var = var_from_col(tab, i);
2125 var->marked = !var->frozen && var->is_nonneg;
2130 struct isl_tab_var *var;
2131 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2132 var = isl_tab_var_from_row(tab, i);
2136 if (i == tab->n_row) {
2137 for (i = tab->n_dead; i < tab->n_col; ++i) {
2138 var = var_from_col(tab, i);
2142 if (i == tab->n_col)
2147 if (sign_of_max(tab, var) == 0)
2148 close_row(tab, var);
2149 else if (!tab->rational && !at_least_one(tab, var)) {
2150 tab = cut_to_hyperplane(tab, var);
2151 return isl_tab_detect_implicit_equalities(tab);
2153 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2154 var = isl_tab_var_from_row(tab, i);
2157 if (may_be_equality(tab, i))
2167 /* Check for (near) redundant constraints.
2168 * A constraint is redundant if it is non-negative and if
2169 * its minimal value (temporarily ignoring the non-negativity) is either
2170 * - zero (in case of rational tableaus), or
2171 * - strictly larger than -1 (in case of integer tableaus)
2173 * We first mark all non-redundant and non-dead variables that
2174 * are not frozen and not obviously negatively unbounded.
2175 * Then we iterate over all marked variables if they can attain
2176 * any values smaller than zero or at most negative one.
2177 * If not, we mark the row as being redundant (assuming it hasn't
2178 * been detected as being obviously redundant in the mean time).
2180 struct isl_tab *isl_tab_detect_redundant(struct isl_tab *tab)
2189 if (tab->n_redundant == tab->n_row)
2193 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2194 struct isl_tab_var *var = isl_tab_var_from_row(tab, i);
2195 var->marked = !var->frozen && var->is_nonneg;
2199 for (i = tab->n_dead; i < tab->n_col; ++i) {
2200 struct isl_tab_var *var = var_from_col(tab, i);
2201 var->marked = !var->frozen && var->is_nonneg &&
2202 !min_is_manifestly_unbounded(tab, var);
2207 struct isl_tab_var *var;
2208 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2209 var = isl_tab_var_from_row(tab, i);
2213 if (i == tab->n_row) {
2214 for (i = tab->n_dead; i < tab->n_col; ++i) {
2215 var = var_from_col(tab, i);
2219 if (i == tab->n_col)
2224 if ((tab->rational ? (sign_of_min(tab, var) >= 0)
2225 : !isl_tab_min_at_most_neg_one(tab, var)) &&
2227 isl_tab_mark_redundant(tab, var->index);
2228 for (i = tab->n_dead; i < tab->n_col; ++i) {
2229 var = var_from_col(tab, i);
2232 if (!min_is_manifestly_unbounded(tab, var))
2242 int isl_tab_is_equality(struct isl_tab *tab, int con)
2249 if (tab->con[con].is_zero)
2251 if (tab->con[con].is_redundant)
2253 if (!tab->con[con].is_row)
2254 return tab->con[con].index < tab->n_dead;
2256 row = tab->con[con].index;
2259 return isl_int_is_zero(tab->mat->row[row][1]) &&
2260 isl_seq_first_non_zero(tab->mat->row[row] + 2 + tab->n_dead,
2261 tab->n_col - tab->n_dead) == -1;
2264 /* Return the minimial value of the affine expression "f" with denominator
2265 * "denom" in *opt, *opt_denom, assuming the tableau is not empty and
2266 * the expression cannot attain arbitrarily small values.
2267 * If opt_denom is NULL, then *opt is rounded up to the nearest integer.
2268 * The return value reflects the nature of the result (empty, unbounded,
2269 * minmimal value returned in *opt).
2271 enum isl_lp_result isl_tab_min(struct isl_tab *tab,
2272 isl_int *f, isl_int denom, isl_int *opt, isl_int *opt_denom,
2276 enum isl_lp_result res = isl_lp_ok;
2277 struct isl_tab_var *var;
2278 struct isl_tab_undo *snap;
2281 return isl_lp_empty;
2283 snap = isl_tab_snap(tab);
2284 r = isl_tab_add_row(tab, f);
2286 return isl_lp_error;
2288 isl_int_mul(tab->mat->row[var->index][0],
2289 tab->mat->row[var->index][0], denom);
2292 find_pivot(tab, var, var, -1, &row, &col);
2293 if (row == var->index) {
2294 res = isl_lp_unbounded;
2299 isl_tab_pivot(tab, row, col);
2301 if (ISL_FL_ISSET(flags, ISL_TAB_SAVE_DUAL)) {
2304 isl_vec_free(tab->dual);
2305 tab->dual = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_con);
2307 return isl_lp_error;
2308 isl_int_set(tab->dual->el[0], tab->mat->row[var->index][0]);
2309 for (i = 0; i < tab->n_con; ++i) {
2311 if (tab->con[i].is_row) {
2312 isl_int_set_si(tab->dual->el[1 + i], 0);
2315 pos = 2 + tab->M + tab->con[i].index;
2316 if (tab->con[i].negated)
2317 isl_int_neg(tab->dual->el[1 + i],
2318 tab->mat->row[var->index][pos]);
2320 isl_int_set(tab->dual->el[1 + i],
2321 tab->mat->row[var->index][pos]);
2324 if (opt && res == isl_lp_ok) {
2326 isl_int_set(*opt, tab->mat->row[var->index][1]);
2327 isl_int_set(*opt_denom, tab->mat->row[var->index][0]);
2329 isl_int_cdiv_q(*opt, tab->mat->row[var->index][1],
2330 tab->mat->row[var->index][0]);
2332 if (isl_tab_rollback(tab, snap) < 0)
2333 return isl_lp_error;
2337 int isl_tab_is_redundant(struct isl_tab *tab, int con)
2341 if (tab->con[con].is_zero)
2343 if (tab->con[con].is_redundant)
2345 return tab->con[con].is_row && tab->con[con].index < tab->n_redundant;
2348 /* Take a snapshot of the tableau that can be restored by s call to
2351 struct isl_tab_undo *isl_tab_snap(struct isl_tab *tab)
2359 /* Undo the operation performed by isl_tab_relax.
2361 static void unrelax(struct isl_tab *tab, struct isl_tab_var *var)
2363 unsigned off = 2 + tab->M;
2365 if (!var->is_row && !max_is_manifestly_unbounded(tab, var))
2366 to_row(tab, var, 1);
2369 isl_int_sub(tab->mat->row[var->index][1],
2370 tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
2374 for (i = 0; i < tab->n_row; ++i) {
2375 if (isl_int_is_zero(tab->mat->row[i][off + var->index]))
2377 isl_int_add(tab->mat->row[i][1], tab->mat->row[i][1],
2378 tab->mat->row[i][off + var->index]);
2384 static void perform_undo_var(struct isl_tab *tab, struct isl_tab_undo *undo)
2386 struct isl_tab_var *var = var_from_index(tab, undo->u.var_index);
2387 switch(undo->type) {
2388 case isl_tab_undo_nonneg:
2391 case isl_tab_undo_redundant:
2392 var->is_redundant = 0;
2395 case isl_tab_undo_zero:
2400 case isl_tab_undo_allocate:
2401 if (undo->u.var_index >= 0) {
2402 isl_assert(tab->mat->ctx, !var->is_row, return);
2403 drop_col(tab, var->index);
2407 if (!max_is_manifestly_unbounded(tab, var))
2408 to_row(tab, var, 1);
2409 else if (!min_is_manifestly_unbounded(tab, var))
2410 to_row(tab, var, -1);
2412 to_row(tab, var, 0);
2414 drop_row(tab, var->index);
2416 case isl_tab_undo_relax:
2422 /* Restore the tableau to the state where the basic variables
2423 * are those in "col_var".
2424 * We first construct a list of variables that are currently in
2425 * the basis, but shouldn't. Then we iterate over all variables
2426 * that should be in the basis and for each one that is currently
2427 * not in the basis, we exchange it with one of the elements of the
2428 * list constructed before.
2429 * We can always find an appropriate variable to pivot with because
2430 * the current basis is mapped to the old basis by a non-singular
2431 * matrix and so we can never end up with a zero row.
2433 static int restore_basis(struct isl_tab *tab, int *col_var)
2437 int *extra = NULL; /* current columns that contain bad stuff */
2438 unsigned off = 2 + tab->M;
2440 extra = isl_alloc_array(tab->mat->ctx, int, tab->n_col);
2443 for (i = 0; i < tab->n_col; ++i) {
2444 for (j = 0; j < tab->n_col; ++j)
2445 if (tab->col_var[i] == col_var[j])
2449 extra[n_extra++] = i;
2451 for (i = 0; i < tab->n_col && n_extra > 0; ++i) {
2452 struct isl_tab_var *var;
2455 for (j = 0; j < tab->n_col; ++j)
2456 if (col_var[i] == tab->col_var[j])
2460 var = var_from_index(tab, col_var[i]);
2462 for (j = 0; j < n_extra; ++j)
2463 if (!isl_int_is_zero(tab->mat->row[row][off+extra[j]]))
2465 isl_assert(tab->mat->ctx, j < n_extra, goto error);
2466 isl_tab_pivot(tab, row, extra[j]);
2467 extra[j] = extra[--n_extra];
2479 static int perform_undo(struct isl_tab *tab, struct isl_tab_undo *undo)
2481 switch (undo->type) {
2482 case isl_tab_undo_empty:
2485 case isl_tab_undo_nonneg:
2486 case isl_tab_undo_redundant:
2487 case isl_tab_undo_zero:
2488 case isl_tab_undo_allocate:
2489 case isl_tab_undo_relax:
2490 perform_undo_var(tab, undo);
2492 case isl_tab_undo_bset_eq:
2493 isl_basic_set_free_equality(tab->bset, 1);
2495 case isl_tab_undo_bset_ineq:
2496 isl_basic_set_free_inequality(tab->bset, 1);
2498 case isl_tab_undo_bset_div:
2499 isl_basic_set_free_div(tab->bset, 1);
2501 tab->samples->n_col--;
2503 case isl_tab_undo_saved_basis:
2504 if (restore_basis(tab, undo->u.col_var) < 0)
2507 case isl_tab_undo_drop_sample:
2511 isl_assert(tab->mat->ctx, 0, return -1);
2516 /* Return the tableau to the state it was in when the snapshot "snap"
2519 int isl_tab_rollback(struct isl_tab *tab, struct isl_tab_undo *snap)
2521 struct isl_tab_undo *undo, *next;
2527 for (undo = tab->top; undo && undo != &tab->bottom; undo = next) {
2531 if (perform_undo(tab, undo) < 0) {
2545 /* The given row "row" represents an inequality violated by all
2546 * points in the tableau. Check for some special cases of such
2547 * separating constraints.
2548 * In particular, if the row has been reduced to the constant -1,
2549 * then we know the inequality is adjacent (but opposite) to
2550 * an equality in the tableau.
2551 * If the row has been reduced to r = -1 -r', with r' an inequality
2552 * of the tableau, then the inequality is adjacent (but opposite)
2553 * to the inequality r'.
2555 static enum isl_ineq_type separation_type(struct isl_tab *tab, unsigned row)
2558 unsigned off = 2 + tab->M;
2561 return isl_ineq_separate;
2563 if (!isl_int_is_one(tab->mat->row[row][0]))
2564 return isl_ineq_separate;
2565 if (!isl_int_is_negone(tab->mat->row[row][1]))
2566 return isl_ineq_separate;
2568 pos = isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
2569 tab->n_col - tab->n_dead);
2571 return isl_ineq_adj_eq;
2573 if (!isl_int_is_negone(tab->mat->row[row][off + tab->n_dead + pos]))
2574 return isl_ineq_separate;
2576 pos = isl_seq_first_non_zero(
2577 tab->mat->row[row] + off + tab->n_dead + pos + 1,
2578 tab->n_col - tab->n_dead - pos - 1);
2580 return pos == -1 ? isl_ineq_adj_ineq : isl_ineq_separate;
2583 /* Check the effect of inequality "ineq" on the tableau "tab".
2585 * isl_ineq_redundant: satisfied by all points in the tableau
2586 * isl_ineq_separate: satisfied by no point in the tableau
2587 * isl_ineq_cut: satisfied by some by not all points
2588 * isl_ineq_adj_eq: adjacent to an equality
2589 * isl_ineq_adj_ineq: adjacent to an inequality.
2591 enum isl_ineq_type isl_tab_ineq_type(struct isl_tab *tab, isl_int *ineq)
2593 enum isl_ineq_type type = isl_ineq_error;
2594 struct isl_tab_undo *snap = NULL;
2599 return isl_ineq_error;
2601 if (isl_tab_extend_cons(tab, 1) < 0)
2602 return isl_ineq_error;
2604 snap = isl_tab_snap(tab);
2606 con = isl_tab_add_row(tab, ineq);
2610 row = tab->con[con].index;
2611 if (isl_tab_row_is_redundant(tab, row))
2612 type = isl_ineq_redundant;
2613 else if (isl_int_is_neg(tab->mat->row[row][1]) &&
2615 isl_int_abs_ge(tab->mat->row[row][1],
2616 tab->mat->row[row][0]))) {
2617 if (at_least_zero(tab, &tab->con[con]))
2618 type = isl_ineq_cut;
2620 type = separation_type(tab, row);
2621 } else if (tab->rational ? (sign_of_min(tab, &tab->con[con]) < 0)
2622 : isl_tab_min_at_most_neg_one(tab, &tab->con[con]))
2623 type = isl_ineq_cut;
2625 type = isl_ineq_redundant;
2627 if (isl_tab_rollback(tab, snap))
2628 return isl_ineq_error;
2631 isl_tab_rollback(tab, snap);
2632 return isl_ineq_error;
2635 void isl_tab_dump(struct isl_tab *tab, FILE *out, int indent)
2641 fprintf(out, "%*snull tab\n", indent, "");
2644 fprintf(out, "%*sn_redundant: %d, n_dead: %d", indent, "",
2645 tab->n_redundant, tab->n_dead);
2647 fprintf(out, ", rational");
2649 fprintf(out, ", empty");
2651 fprintf(out, "%*s[", indent, "");
2652 for (i = 0; i < tab->n_var; ++i) {
2654 fprintf(out, (i == tab->n_param ||
2655 i == tab->n_var - tab->n_div) ? "; "
2657 fprintf(out, "%c%d%s", tab->var[i].is_row ? 'r' : 'c',
2659 tab->var[i].is_zero ? " [=0]" :
2660 tab->var[i].is_redundant ? " [R]" : "");
2662 fprintf(out, "]\n");
2663 fprintf(out, "%*s[", indent, "");
2664 for (i = 0; i < tab->n_con; ++i) {
2667 fprintf(out, "%c%d%s", tab->con[i].is_row ? 'r' : 'c',
2669 tab->con[i].is_zero ? " [=0]" :
2670 tab->con[i].is_redundant ? " [R]" : "");
2672 fprintf(out, "]\n");
2673 fprintf(out, "%*s[", indent, "");
2674 for (i = 0; i < tab->n_row; ++i) {
2675 const char *sign = "";
2678 if (tab->row_sign) {
2679 if (tab->row_sign[i] == isl_tab_row_unknown)
2681 else if (tab->row_sign[i] == isl_tab_row_neg)
2683 else if (tab->row_sign[i] == isl_tab_row_pos)
2688 fprintf(out, "r%d: %d%s%s", i, tab->row_var[i],
2689 isl_tab_var_from_row(tab, i)->is_nonneg ? " [>=0]" : "", sign);
2691 fprintf(out, "]\n");
2692 fprintf(out, "%*s[", indent, "");
2693 for (i = 0; i < tab->n_col; ++i) {
2696 fprintf(out, "c%d: %d%s", i, tab->col_var[i],
2697 var_from_col(tab, i)->is_nonneg ? " [>=0]" : "");
2699 fprintf(out, "]\n");
2700 r = tab->mat->n_row;
2701 tab->mat->n_row = tab->n_row;
2702 c = tab->mat->n_col;
2703 tab->mat->n_col = 2 + tab->M + tab->n_col;
2704 isl_mat_dump(tab->mat, out, indent);
2705 tab->mat->n_row = r;
2706 tab->mat->n_col = c;
2708 isl_basic_set_dump(tab->bset, out, indent);