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;
72 int isl_tab_extend_cons(struct isl_tab *tab, unsigned n_new)
74 unsigned off = 2 + tab->M;
79 if (tab->max_con < tab->n_con + n_new) {
80 struct isl_tab_var *con;
82 con = isl_realloc_array(tab->mat->ctx, tab->con,
83 struct isl_tab_var, tab->max_con + n_new);
87 tab->max_con += n_new;
89 if (tab->mat->n_row < tab->n_row + n_new) {
92 tab->mat = isl_mat_extend(tab->mat,
93 tab->n_row + n_new, off + tab->n_col);
96 row_var = isl_realloc_array(tab->mat->ctx, tab->row_var,
97 int, tab->mat->n_row);
100 tab->row_var = row_var;
102 enum isl_tab_row_sign *s;
103 s = isl_realloc_array(tab->mat->ctx, tab->row_sign,
104 enum isl_tab_row_sign, tab->mat->n_row);
113 /* Make room for at least n_new extra variables.
114 * Return -1 if anything went wrong.
116 int isl_tab_extend_vars(struct isl_tab *tab, unsigned n_new)
118 struct isl_tab_var *var;
119 unsigned off = 2 + tab->M;
121 if (tab->max_var < tab->n_var + n_new) {
122 var = isl_realloc_array(tab->mat->ctx, tab->var,
123 struct isl_tab_var, tab->n_var + n_new);
127 tab->max_var += n_new;
130 if (tab->mat->n_col < off + tab->n_col + n_new) {
133 tab->mat = isl_mat_extend(tab->mat,
134 tab->mat->n_row, off + tab->n_col + n_new);
137 p = isl_realloc_array(tab->mat->ctx, tab->col_var,
138 int, tab->n_col + n_new);
147 struct isl_tab *isl_tab_extend(struct isl_tab *tab, unsigned n_new)
149 if (isl_tab_extend_cons(tab, n_new) >= 0)
156 static void free_undo(struct isl_tab *tab)
158 struct isl_tab_undo *undo, *next;
160 for (undo = tab->top; undo && undo != &tab->bottom; undo = next) {
167 void isl_tab_free(struct isl_tab *tab)
172 isl_mat_free(tab->mat);
173 isl_vec_free(tab->dual);
174 isl_basic_set_free(tab->bset);
180 isl_mat_free(tab->samples);
184 struct isl_tab *isl_tab_dup(struct isl_tab *tab)
194 dup = isl_calloc_type(tab->ctx, struct isl_tab);
197 dup->mat = isl_mat_dup(tab->mat);
200 dup->var = isl_alloc_array(tab->ctx, struct isl_tab_var, tab->max_var);
203 for (i = 0; i < tab->n_var; ++i)
204 dup->var[i] = tab->var[i];
205 dup->con = isl_alloc_array(tab->ctx, struct isl_tab_var, tab->max_con);
208 for (i = 0; i < tab->n_con; ++i)
209 dup->con[i] = tab->con[i];
210 dup->col_var = isl_alloc_array(tab->ctx, int, tab->mat->n_col - off);
213 for (i = 0; i < tab->n_col; ++i)
214 dup->col_var[i] = tab->col_var[i];
215 dup->row_var = isl_alloc_array(tab->ctx, int, tab->mat->n_row);
218 for (i = 0; i < tab->n_row; ++i)
219 dup->row_var[i] = tab->row_var[i];
221 dup->row_sign = isl_alloc_array(tab->ctx, enum isl_tab_row_sign,
225 for (i = 0; i < tab->n_row; ++i)
226 dup->row_sign[i] = tab->row_sign[i];
229 dup->samples = isl_mat_dup(tab->samples);
232 dup->n_sample = tab->n_sample;
233 dup->n_outside = tab->n_outside;
235 dup->n_row = tab->n_row;
236 dup->n_con = tab->n_con;
237 dup->n_eq = tab->n_eq;
238 dup->max_con = tab->max_con;
239 dup->n_col = tab->n_col;
240 dup->n_var = tab->n_var;
241 dup->max_var = tab->max_var;
242 dup->n_param = tab->n_param;
243 dup->n_div = tab->n_div;
244 dup->n_dead = tab->n_dead;
245 dup->n_redundant = tab->n_redundant;
246 dup->rational = tab->rational;
247 dup->empty = tab->empty;
251 dup->bottom.type = isl_tab_undo_bottom;
252 dup->bottom.next = NULL;
253 dup->top = &dup->bottom;
260 /* Construct the coefficient matrix of the product tableau
262 * mat{1,2} is the coefficient matrix of tableau {1,2}
263 * row{1,2} is the number of rows in tableau {1,2}
264 * col{1,2} is the number of columns in tableau {1,2}
265 * off is the offset to the coefficient column (skipping the
266 * denominator, the constant term and the big parameter if any)
267 * r{1,2} is the number of redundant rows in tableau {1,2}
268 * d{1,2} is the number of dead columns in tableau {1,2}
270 * The order of the rows and columns in the result is as explained
271 * in isl_tab_product.
273 static struct isl_mat *tab_mat_product(struct isl_mat *mat1,
274 struct isl_mat *mat2, unsigned row1, unsigned row2,
275 unsigned col1, unsigned col2,
276 unsigned off, unsigned r1, unsigned r2, unsigned d1, unsigned d2)
279 struct isl_mat *prod;
282 prod = isl_mat_alloc(mat1->ctx, mat1->n_row + mat2->n_row,
286 for (i = 0; i < r1; ++i) {
287 isl_seq_cpy(prod->row[n + i], mat1->row[i], off + d1);
288 isl_seq_clr(prod->row[n + i] + off + d1, d2);
289 isl_seq_cpy(prod->row[n + i] + off + d1 + d2,
290 mat1->row[i] + off + d1, col1 - d1);
291 isl_seq_clr(prod->row[n + i] + off + col1 + d1, col2 - d2);
295 for (i = 0; i < r2; ++i) {
296 isl_seq_cpy(prod->row[n + i], mat2->row[i], off);
297 isl_seq_clr(prod->row[n + i] + off, d1);
298 isl_seq_cpy(prod->row[n + i] + off + d1,
299 mat2->row[i] + off, d2);
300 isl_seq_clr(prod->row[n + i] + off + d1 + d2, col1 - d1);
301 isl_seq_cpy(prod->row[n + i] + off + col1 + d1,
302 mat2->row[i] + off + d2, col2 - d2);
306 for (i = 0; i < row1 - r1; ++i) {
307 isl_seq_cpy(prod->row[n + i], mat1->row[r1 + i], off + d1);
308 isl_seq_clr(prod->row[n + i] + off + d1, d2);
309 isl_seq_cpy(prod->row[n + i] + off + d1 + d2,
310 mat1->row[r1 + i] + off + d1, col1 - d1);
311 isl_seq_clr(prod->row[n + i] + off + col1 + d1, col2 - d2);
315 for (i = 0; i < row2 - r2; ++i) {
316 isl_seq_cpy(prod->row[n + i], mat2->row[r2 + i], off);
317 isl_seq_clr(prod->row[n + i] + off, d1);
318 isl_seq_cpy(prod->row[n + i] + off + d1,
319 mat2->row[r2 + i] + off, d2);
320 isl_seq_clr(prod->row[n + i] + off + d1 + d2, col1 - d1);
321 isl_seq_cpy(prod->row[n + i] + off + col1 + d1,
322 mat2->row[r2 + i] + off + d2, col2 - d2);
328 /* Update the row or column index of a variable that corresponds
329 * to a variable in the first input tableau.
331 static void update_index1(struct isl_tab_var *var,
332 unsigned r1, unsigned r2, unsigned d1, unsigned d2)
334 if (var->index == -1)
336 if (var->is_row && var->index >= r1)
338 if (!var->is_row && var->index >= d1)
342 /* Update the row or column index of a variable that corresponds
343 * to a variable in the second input tableau.
345 static void update_index2(struct isl_tab_var *var,
346 unsigned row1, unsigned col1,
347 unsigned r1, unsigned r2, unsigned d1, unsigned d2)
349 if (var->index == -1)
364 /* Create a tableau that represents the Cartesian product of the sets
365 * represented by tableaus tab1 and tab2.
366 * The order of the rows in the product is
367 * - redundant rows of tab1
368 * - redundant rows of tab2
369 * - non-redundant rows of tab1
370 * - non-redundant rows of tab2
371 * The order of the columns is
374 * - coefficient of big parameter, if any
375 * - dead columns of tab1
376 * - dead columns of tab2
377 * - live columns of tab1
378 * - live columns of tab2
379 * The order of the variables and the constraints is a concatenation
380 * of order in the two input tableaus.
382 struct isl_tab *isl_tab_product(struct isl_tab *tab1, struct isl_tab *tab2)
385 struct isl_tab *prod;
387 unsigned r1, r2, d1, d2;
392 isl_assert(tab1->mat->ctx, tab1->M == tab2->M, return NULL);
393 isl_assert(tab1->mat->ctx, tab1->rational == tab2->rational, return NULL);
394 isl_assert(tab1->mat->ctx, !tab1->row_sign, return NULL);
395 isl_assert(tab1->mat->ctx, !tab2->row_sign, return NULL);
396 isl_assert(tab1->mat->ctx, tab1->n_param == 0, return NULL);
397 isl_assert(tab1->mat->ctx, tab2->n_param == 0, return NULL);
398 isl_assert(tab1->mat->ctx, tab1->n_div == 0, return NULL);
399 isl_assert(tab1->mat->ctx, tab2->n_div == 0, return NULL);
402 r1 = tab1->n_redundant;
403 r2 = tab2->n_redundant;
406 prod = isl_calloc_type(tab1->mat->ctx, struct isl_tab);
409 prod->mat = tab_mat_product(tab1->mat, tab2->mat,
410 tab1->n_row, tab2->n_row,
411 tab1->n_col, tab2->n_col, off, r1, r2, d1, d2);
414 prod->var = isl_alloc_array(tab1->mat->ctx, struct isl_tab_var,
415 tab1->max_var + tab2->max_var);
418 for (i = 0; i < tab1->n_var; ++i) {
419 prod->var[i] = tab1->var[i];
420 update_index1(&prod->var[i], r1, r2, d1, d2);
422 for (i = 0; i < tab2->n_var; ++i) {
423 prod->var[tab1->n_var + i] = tab2->var[i];
424 update_index2(&prod->var[tab1->n_var + i],
425 tab1->n_row, tab1->n_col,
428 prod->con = isl_alloc_array(tab1->mat->ctx, struct isl_tab_var,
429 tab1->max_con + tab2->max_con);
432 for (i = 0; i < tab1->n_con; ++i) {
433 prod->con[i] = tab1->con[i];
434 update_index1(&prod->con[i], r1, r2, d1, d2);
436 for (i = 0; i < tab2->n_con; ++i) {
437 prod->con[tab1->n_con + i] = tab2->con[i];
438 update_index2(&prod->con[tab1->n_con + i],
439 tab1->n_row, tab1->n_col,
442 prod->col_var = isl_alloc_array(tab1->mat->ctx, int,
443 tab1->n_col + tab2->n_col);
446 for (i = 0; i < tab1->n_col; ++i) {
447 int pos = i < d1 ? i : i + d2;
448 prod->col_var[pos] = tab1->col_var[i];
450 for (i = 0; i < tab2->n_col; ++i) {
451 int pos = i < d2 ? d1 + i : tab1->n_col + i;
452 int t = tab2->col_var[i];
457 prod->col_var[pos] = t;
459 prod->row_var = isl_alloc_array(tab1->mat->ctx, int,
460 tab1->mat->n_row + tab2->mat->n_row);
463 for (i = 0; i < tab1->n_row; ++i) {
464 int pos = i < r1 ? i : i + r2;
465 prod->row_var[pos] = tab1->row_var[i];
467 for (i = 0; i < tab2->n_row; ++i) {
468 int pos = i < r2 ? r1 + i : tab1->n_row + i;
469 int t = tab2->row_var[i];
474 prod->row_var[pos] = t;
476 prod->samples = NULL;
477 prod->n_row = tab1->n_row + tab2->n_row;
478 prod->n_con = tab1->n_con + tab2->n_con;
480 prod->max_con = tab1->max_con + tab2->max_con;
481 prod->n_col = tab1->n_col + tab2->n_col;
482 prod->n_var = tab1->n_var + tab2->n_var;
483 prod->max_var = tab1->max_var + tab2->max_var;
486 prod->n_dead = tab1->n_dead + tab2->n_dead;
487 prod->n_redundant = tab1->n_redundant + tab2->n_redundant;
488 prod->rational = tab1->rational;
489 prod->empty = tab1->empty || tab2->empty;
493 prod->bottom.type = isl_tab_undo_bottom;
494 prod->bottom.next = NULL;
495 prod->top = &prod->bottom;
502 static struct isl_tab_var *var_from_index(struct isl_tab *tab, int i)
507 return &tab->con[~i];
510 struct isl_tab_var *isl_tab_var_from_row(struct isl_tab *tab, int i)
512 return var_from_index(tab, tab->row_var[i]);
515 static struct isl_tab_var *var_from_col(struct isl_tab *tab, int i)
517 return var_from_index(tab, tab->col_var[i]);
520 /* Check if there are any upper bounds on column variable "var",
521 * i.e., non-negative rows where var appears with a negative coefficient.
522 * Return 1 if there are no such bounds.
524 static int max_is_manifestly_unbounded(struct isl_tab *tab,
525 struct isl_tab_var *var)
528 unsigned off = 2 + tab->M;
532 for (i = tab->n_redundant; i < tab->n_row; ++i) {
533 if (!isl_int_is_neg(tab->mat->row[i][off + var->index]))
535 if (isl_tab_var_from_row(tab, i)->is_nonneg)
541 /* Check if there are any lower bounds on column variable "var",
542 * i.e., non-negative rows where var appears with a positive coefficient.
543 * Return 1 if there are no such bounds.
545 static int min_is_manifestly_unbounded(struct isl_tab *tab,
546 struct isl_tab_var *var)
549 unsigned off = 2 + tab->M;
553 for (i = tab->n_redundant; i < tab->n_row; ++i) {
554 if (!isl_int_is_pos(tab->mat->row[i][off + var->index]))
556 if (isl_tab_var_from_row(tab, i)->is_nonneg)
562 static int row_cmp(struct isl_tab *tab, int r1, int r2, int c, isl_int t)
564 unsigned off = 2 + tab->M;
568 isl_int_mul(t, tab->mat->row[r1][2], tab->mat->row[r2][off+c]);
569 isl_int_submul(t, tab->mat->row[r2][2], tab->mat->row[r1][off+c]);
574 isl_int_mul(t, tab->mat->row[r1][1], tab->mat->row[r2][off + c]);
575 isl_int_submul(t, tab->mat->row[r2][1], tab->mat->row[r1][off + c]);
576 return isl_int_sgn(t);
579 /* Given the index of a column "c", return the index of a row
580 * that can be used to pivot the column in, with either an increase
581 * (sgn > 0) or a decrease (sgn < 0) of the corresponding variable.
582 * If "var" is not NULL, then the row returned will be different from
583 * the one associated with "var".
585 * Each row in the tableau is of the form
587 * x_r = a_r0 + \sum_i a_ri x_i
589 * Only rows with x_r >= 0 and with the sign of a_ri opposite to "sgn"
590 * impose any limit on the increase or decrease in the value of x_c
591 * and this bound is equal to a_r0 / |a_rc|. We are therefore looking
592 * for the row with the smallest (most stringent) such bound.
593 * Note that the common denominator of each row drops out of the fraction.
594 * To check if row j has a smaller bound than row r, i.e.,
595 * a_j0 / |a_jc| < a_r0 / |a_rc| or a_j0 |a_rc| < a_r0 |a_jc|,
596 * we check if -sign(a_jc) (a_j0 a_rc - a_r0 a_jc) < 0,
597 * where -sign(a_jc) is equal to "sgn".
599 static int pivot_row(struct isl_tab *tab,
600 struct isl_tab_var *var, int sgn, int c)
604 unsigned off = 2 + tab->M;
608 for (j = tab->n_redundant; j < tab->n_row; ++j) {
609 if (var && j == var->index)
611 if (!isl_tab_var_from_row(tab, j)->is_nonneg)
613 if (sgn * isl_int_sgn(tab->mat->row[j][off + c]) >= 0)
619 tsgn = sgn * row_cmp(tab, r, j, c, t);
620 if (tsgn < 0 || (tsgn == 0 &&
621 tab->row_var[j] < tab->row_var[r]))
628 /* Find a pivot (row and col) that will increase (sgn > 0) or decrease
629 * (sgn < 0) the value of row variable var.
630 * If not NULL, then skip_var is a row variable that should be ignored
631 * while looking for a pivot row. It is usually equal to var.
633 * As the given row in the tableau is of the form
635 * x_r = a_r0 + \sum_i a_ri x_i
637 * we need to find a column such that the sign of a_ri is equal to "sgn"
638 * (such that an increase in x_i will have the desired effect) or a
639 * column with a variable that may attain negative values.
640 * If a_ri is positive, then we need to move x_i in the same direction
641 * to obtain the desired effect. Otherwise, x_i has to move in the
642 * opposite direction.
644 static void find_pivot(struct isl_tab *tab,
645 struct isl_tab_var *var, struct isl_tab_var *skip_var,
646 int sgn, int *row, int *col)
653 isl_assert(tab->mat->ctx, var->is_row, return);
654 tr = tab->mat->row[var->index] + 2 + tab->M;
657 for (j = tab->n_dead; j < tab->n_col; ++j) {
658 if (isl_int_is_zero(tr[j]))
660 if (isl_int_sgn(tr[j]) != sgn &&
661 var_from_col(tab, j)->is_nonneg)
663 if (c < 0 || tab->col_var[j] < tab->col_var[c])
669 sgn *= isl_int_sgn(tr[c]);
670 r = pivot_row(tab, skip_var, sgn, c);
671 *row = r < 0 ? var->index : r;
675 /* Return 1 if row "row" represents an obviously redundant inequality.
677 * - it represents an inequality or a variable
678 * - that is the sum of a non-negative sample value and a positive
679 * combination of zero or more non-negative variables.
681 int isl_tab_row_is_redundant(struct isl_tab *tab, int row)
684 unsigned off = 2 + tab->M;
686 if (tab->row_var[row] < 0 && !isl_tab_var_from_row(tab, row)->is_nonneg)
689 if (isl_int_is_neg(tab->mat->row[row][1]))
691 if (tab->M && isl_int_is_neg(tab->mat->row[row][2]))
694 for (i = tab->n_dead; i < tab->n_col; ++i) {
695 if (isl_int_is_zero(tab->mat->row[row][off + i]))
697 if (isl_int_is_neg(tab->mat->row[row][off + i]))
699 if (!var_from_col(tab, i)->is_nonneg)
705 static void swap_rows(struct isl_tab *tab, int row1, int row2)
708 t = tab->row_var[row1];
709 tab->row_var[row1] = tab->row_var[row2];
710 tab->row_var[row2] = t;
711 isl_tab_var_from_row(tab, row1)->index = row1;
712 isl_tab_var_from_row(tab, row2)->index = row2;
713 tab->mat = isl_mat_swap_rows(tab->mat, row1, row2);
717 t = tab->row_sign[row1];
718 tab->row_sign[row1] = tab->row_sign[row2];
719 tab->row_sign[row2] = t;
722 static void push_union(struct isl_tab *tab,
723 enum isl_tab_undo_type type, union isl_tab_undo_val u)
725 struct isl_tab_undo *undo;
730 undo = isl_alloc_type(tab->mat->ctx, struct isl_tab_undo);
738 undo->next = tab->top;
742 void isl_tab_push_var(struct isl_tab *tab,
743 enum isl_tab_undo_type type, struct isl_tab_var *var)
745 union isl_tab_undo_val u;
747 u.var_index = tab->row_var[var->index];
749 u.var_index = tab->col_var[var->index];
750 push_union(tab, type, u);
753 void isl_tab_push(struct isl_tab *tab, enum isl_tab_undo_type type)
755 union isl_tab_undo_val u = { 0 };
756 push_union(tab, type, u);
759 /* Push a record on the undo stack describing the current basic
760 * variables, so that the this state can be restored during rollback.
762 void isl_tab_push_basis(struct isl_tab *tab)
765 union isl_tab_undo_val u;
767 u.col_var = isl_alloc_array(tab->mat->ctx, int, tab->n_col);
773 for (i = 0; i < tab->n_col; ++i)
774 u.col_var[i] = tab->col_var[i];
775 push_union(tab, isl_tab_undo_saved_basis, u);
778 /* Mark row with index "row" as being redundant.
779 * If we may need to undo the operation or if the row represents
780 * a variable of the original problem, the row is kept,
781 * but no longer considered when looking for a pivot row.
782 * Otherwise, the row is simply removed.
784 * The row may be interchanged with some other row. If it
785 * is interchanged with a later row, return 1. Otherwise return 0.
786 * If the rows are checked in order in the calling function,
787 * then a return value of 1 means that the row with the given
788 * row number may now contain a different row that hasn't been checked yet.
790 int isl_tab_mark_redundant(struct isl_tab *tab, int row)
792 struct isl_tab_var *var = isl_tab_var_from_row(tab, row);
793 var->is_redundant = 1;
794 isl_assert(tab->mat->ctx, row >= tab->n_redundant, return -1);
795 if (tab->need_undo || tab->row_var[row] >= 0) {
796 if (tab->row_var[row] >= 0 && !var->is_nonneg) {
798 isl_tab_push_var(tab, isl_tab_undo_nonneg, var);
800 if (row != tab->n_redundant)
801 swap_rows(tab, row, tab->n_redundant);
802 isl_tab_push_var(tab, isl_tab_undo_redundant, var);
806 if (row != tab->n_row - 1)
807 swap_rows(tab, row, tab->n_row - 1);
808 isl_tab_var_from_row(tab, tab->n_row - 1)->index = -1;
814 struct isl_tab *isl_tab_mark_empty(struct isl_tab *tab)
816 if (!tab->empty && tab->need_undo)
817 isl_tab_push(tab, isl_tab_undo_empty);
822 /* Update the rows signs after a pivot of "row" and "col", with "row_sgn"
823 * the original sign of the pivot element.
824 * We only keep track of row signs during PILP solving and in this case
825 * we only pivot a row with negative sign (meaning the value is always
826 * non-positive) using a positive pivot element.
828 * For each row j, the new value of the parametric constant is equal to
830 * a_j0 - a_jc a_r0/a_rc
832 * where a_j0 is the original parametric constant, a_rc is the pivot element,
833 * a_r0 is the parametric constant of the pivot row and a_jc is the
834 * pivot column entry of the row j.
835 * Since a_r0 is non-positive and a_rc is positive, the sign of row j
836 * remains the same if a_jc has the same sign as the row j or if
837 * a_jc is zero. In all other cases, we reset the sign to "unknown".
839 static void update_row_sign(struct isl_tab *tab, int row, int col, int row_sgn)
842 struct isl_mat *mat = tab->mat;
843 unsigned off = 2 + tab->M;
848 if (tab->row_sign[row] == 0)
850 isl_assert(mat->ctx, row_sgn > 0, return);
851 isl_assert(mat->ctx, tab->row_sign[row] == isl_tab_row_neg, return);
852 tab->row_sign[row] = isl_tab_row_pos;
853 for (i = 0; i < tab->n_row; ++i) {
857 s = isl_int_sgn(mat->row[i][off + col]);
860 if (!tab->row_sign[i])
862 if (s < 0 && tab->row_sign[i] == isl_tab_row_neg)
864 if (s > 0 && tab->row_sign[i] == isl_tab_row_pos)
866 tab->row_sign[i] = isl_tab_row_unknown;
870 /* Given a row number "row" and a column number "col", pivot the tableau
871 * such that the associated variables are interchanged.
872 * The given row in the tableau expresses
874 * x_r = a_r0 + \sum_i a_ri x_i
878 * x_c = 1/a_rc x_r - a_r0/a_rc + sum_{i \ne r} -a_ri/a_rc
880 * Substituting this equality into the other rows
882 * x_j = a_j0 + \sum_i a_ji x_i
884 * with a_jc \ne 0, we obtain
886 * 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
893 * where i is any other column and j is any other row,
894 * is therefore transformed into
896 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
897 * 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)
899 * The transformation is performed along the following steps
904 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
907 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
908 * n_jc/(|n_rc| d_j) n_ji/(|n_rc| d_j)
910 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
911 * n_jc/(|n_rc| d_j) (n_ji |n_rc|)/(|n_rc| d_j)
913 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
914 * n_jc/(|n_rc| d_j) (n_ji |n_rc| - s(n_rc)n_jc n_ri)/(|n_rc| d_j)
916 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
917 * 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)
920 void isl_tab_pivot(struct isl_tab *tab, int row, int col)
925 struct isl_mat *mat = tab->mat;
926 struct isl_tab_var *var;
927 unsigned off = 2 + tab->M;
929 isl_int_swap(mat->row[row][0], mat->row[row][off + col]);
930 sgn = isl_int_sgn(mat->row[row][0]);
932 isl_int_neg(mat->row[row][0], mat->row[row][0]);
933 isl_int_neg(mat->row[row][off + col], mat->row[row][off + col]);
935 for (j = 0; j < off - 1 + tab->n_col; ++j) {
936 if (j == off - 1 + col)
938 isl_int_neg(mat->row[row][1 + j], mat->row[row][1 + j]);
940 if (!isl_int_is_one(mat->row[row][0]))
941 isl_seq_normalize(mat->ctx, mat->row[row], off + tab->n_col);
942 for (i = 0; i < tab->n_row; ++i) {
945 if (isl_int_is_zero(mat->row[i][off + col]))
947 isl_int_mul(mat->row[i][0], mat->row[i][0], mat->row[row][0]);
948 for (j = 0; j < off - 1 + tab->n_col; ++j) {
949 if (j == off - 1 + col)
951 isl_int_mul(mat->row[i][1 + j],
952 mat->row[i][1 + j], mat->row[row][0]);
953 isl_int_addmul(mat->row[i][1 + j],
954 mat->row[i][off + col], mat->row[row][1 + j]);
956 isl_int_mul(mat->row[i][off + col],
957 mat->row[i][off + col], mat->row[row][off + col]);
958 if (!isl_int_is_one(mat->row[i][0]))
959 isl_seq_normalize(mat->ctx, mat->row[i], off + tab->n_col);
961 t = tab->row_var[row];
962 tab->row_var[row] = tab->col_var[col];
963 tab->col_var[col] = t;
964 var = isl_tab_var_from_row(tab, row);
967 var = var_from_col(tab, col);
970 update_row_sign(tab, row, col, sgn);
973 for (i = tab->n_redundant; i < tab->n_row; ++i) {
974 if (isl_int_is_zero(mat->row[i][off + col]))
976 if (!isl_tab_var_from_row(tab, i)->frozen &&
977 isl_tab_row_is_redundant(tab, i))
978 if (isl_tab_mark_redundant(tab, i))
983 /* If "var" represents a column variable, then pivot is up (sgn > 0)
984 * or down (sgn < 0) to a row. The variable is assumed not to be
985 * unbounded in the specified direction.
986 * If sgn = 0, then the variable is unbounded in both directions,
987 * and we pivot with any row we can find.
989 static void to_row(struct isl_tab *tab, struct isl_tab_var *var, int sign)
992 unsigned off = 2 + tab->M;
998 for (r = tab->n_redundant; r < tab->n_row; ++r)
999 if (!isl_int_is_zero(tab->mat->row[r][off+var->index]))
1001 isl_assert(tab->mat->ctx, r < tab->n_row, return);
1003 r = pivot_row(tab, NULL, sign, var->index);
1004 isl_assert(tab->mat->ctx, r >= 0, return);
1007 isl_tab_pivot(tab, r, var->index);
1010 static void check_table(struct isl_tab *tab)
1016 for (i = 0; i < tab->n_row; ++i) {
1017 if (!isl_tab_var_from_row(tab, i)->is_nonneg)
1019 assert(!isl_int_is_neg(tab->mat->row[i][1]));
1023 /* Return the sign of the maximal value of "var".
1024 * If the sign is not negative, then on return from this function,
1025 * the sample value will also be non-negative.
1027 * If "var" is manifestly unbounded wrt positive values, we are done.
1028 * Otherwise, we pivot the variable up to a row if needed
1029 * Then we continue pivoting down until either
1030 * - no more down pivots can be performed
1031 * - the sample value is positive
1032 * - the variable is pivoted into a manifestly unbounded column
1034 static int sign_of_max(struct isl_tab *tab, struct isl_tab_var *var)
1038 if (max_is_manifestly_unbounded(tab, var))
1040 to_row(tab, var, 1);
1041 while (!isl_int_is_pos(tab->mat->row[var->index][1])) {
1042 find_pivot(tab, var, var, 1, &row, &col);
1044 return isl_int_sgn(tab->mat->row[var->index][1]);
1045 isl_tab_pivot(tab, row, col);
1046 if (!var->is_row) /* manifestly unbounded */
1052 static int row_is_neg(struct isl_tab *tab, int row)
1055 return isl_int_is_neg(tab->mat->row[row][1]);
1056 if (isl_int_is_pos(tab->mat->row[row][2]))
1058 if (isl_int_is_neg(tab->mat->row[row][2]))
1060 return isl_int_is_neg(tab->mat->row[row][1]);
1063 static int row_sgn(struct isl_tab *tab, int row)
1066 return isl_int_sgn(tab->mat->row[row][1]);
1067 if (!isl_int_is_zero(tab->mat->row[row][2]))
1068 return isl_int_sgn(tab->mat->row[row][2]);
1070 return isl_int_sgn(tab->mat->row[row][1]);
1073 /* Perform pivots until the row variable "var" has a non-negative
1074 * sample value or until no more upward pivots can be performed.
1075 * Return the sign of the sample value after the pivots have been
1078 static int restore_row(struct isl_tab *tab, struct isl_tab_var *var)
1082 while (row_is_neg(tab, var->index)) {
1083 find_pivot(tab, var, var, 1, &row, &col);
1086 isl_tab_pivot(tab, row, col);
1087 if (!var->is_row) /* manifestly unbounded */
1090 return row_sgn(tab, var->index);
1093 /* Perform pivots until we are sure that the row variable "var"
1094 * can attain non-negative values. After return from this
1095 * function, "var" is still a row variable, but its sample
1096 * value may not be non-negative, even if the function returns 1.
1098 static int at_least_zero(struct isl_tab *tab, struct isl_tab_var *var)
1102 while (isl_int_is_neg(tab->mat->row[var->index][1])) {
1103 find_pivot(tab, var, var, 1, &row, &col);
1106 if (row == var->index) /* manifestly unbounded */
1108 isl_tab_pivot(tab, row, col);
1110 return !isl_int_is_neg(tab->mat->row[var->index][1]);
1113 /* Return a negative value if "var" can attain negative values.
1114 * Return a non-negative value otherwise.
1116 * If "var" is manifestly unbounded wrt negative values, we are done.
1117 * Otherwise, if var is in a column, we can pivot it down to a row.
1118 * Then we continue pivoting down until either
1119 * - the pivot would result in a manifestly unbounded column
1120 * => we don't perform the pivot, but simply return -1
1121 * - no more down pivots can be performed
1122 * - the sample value is negative
1123 * If the sample value becomes negative and the variable is supposed
1124 * to be nonnegative, then we undo the last pivot.
1125 * However, if the last pivot has made the pivoting variable
1126 * obviously redundant, then it may have moved to another row.
1127 * In that case we look for upward pivots until we reach a non-negative
1130 static int sign_of_min(struct isl_tab *tab, struct isl_tab_var *var)
1133 struct isl_tab_var *pivot_var = NULL;
1135 if (min_is_manifestly_unbounded(tab, var))
1139 row = pivot_row(tab, NULL, -1, col);
1140 pivot_var = var_from_col(tab, col);
1141 isl_tab_pivot(tab, row, col);
1142 if (var->is_redundant)
1144 if (isl_int_is_neg(tab->mat->row[var->index][1])) {
1145 if (var->is_nonneg) {
1146 if (!pivot_var->is_redundant &&
1147 pivot_var->index == row)
1148 isl_tab_pivot(tab, row, col);
1150 restore_row(tab, var);
1155 if (var->is_redundant)
1157 while (!isl_int_is_neg(tab->mat->row[var->index][1])) {
1158 find_pivot(tab, var, var, -1, &row, &col);
1159 if (row == var->index)
1162 return isl_int_sgn(tab->mat->row[var->index][1]);
1163 pivot_var = var_from_col(tab, col);
1164 isl_tab_pivot(tab, row, col);
1165 if (var->is_redundant)
1168 if (pivot_var && var->is_nonneg) {
1169 /* pivot back to non-negative value */
1170 if (!pivot_var->is_redundant && pivot_var->index == row)
1171 isl_tab_pivot(tab, row, col);
1173 restore_row(tab, var);
1178 static int row_at_most_neg_one(struct isl_tab *tab, int row)
1181 if (isl_int_is_pos(tab->mat->row[row][2]))
1183 if (isl_int_is_neg(tab->mat->row[row][2]))
1186 return isl_int_is_neg(tab->mat->row[row][1]) &&
1187 isl_int_abs_ge(tab->mat->row[row][1],
1188 tab->mat->row[row][0]);
1191 /* Return 1 if "var" can attain values <= -1.
1192 * Return 0 otherwise.
1194 * The sample value of "var" is assumed to be non-negative when the
1195 * the function is called and will be made non-negative again before
1196 * the function returns.
1198 int isl_tab_min_at_most_neg_one(struct isl_tab *tab, struct isl_tab_var *var)
1201 struct isl_tab_var *pivot_var;
1203 if (min_is_manifestly_unbounded(tab, var))
1207 row = pivot_row(tab, NULL, -1, col);
1208 pivot_var = var_from_col(tab, col);
1209 isl_tab_pivot(tab, row, col);
1210 if (var->is_redundant)
1212 if (row_at_most_neg_one(tab, var->index)) {
1213 if (var->is_nonneg) {
1214 if (!pivot_var->is_redundant &&
1215 pivot_var->index == row)
1216 isl_tab_pivot(tab, row, col);
1218 restore_row(tab, var);
1223 if (var->is_redundant)
1226 find_pivot(tab, var, var, -1, &row, &col);
1227 if (row == var->index)
1231 pivot_var = var_from_col(tab, col);
1232 isl_tab_pivot(tab, row, col);
1233 if (var->is_redundant)
1235 } while (!row_at_most_neg_one(tab, var->index));
1236 if (var->is_nonneg) {
1237 /* pivot back to non-negative value */
1238 if (!pivot_var->is_redundant && pivot_var->index == row)
1239 isl_tab_pivot(tab, row, col);
1240 restore_row(tab, var);
1245 /* Return 1 if "var" can attain values >= 1.
1246 * Return 0 otherwise.
1248 static int at_least_one(struct isl_tab *tab, struct isl_tab_var *var)
1253 if (max_is_manifestly_unbounded(tab, var))
1255 to_row(tab, var, 1);
1256 r = tab->mat->row[var->index];
1257 while (isl_int_lt(r[1], r[0])) {
1258 find_pivot(tab, var, var, 1, &row, &col);
1260 return isl_int_ge(r[1], r[0]);
1261 if (row == var->index) /* manifestly unbounded */
1263 isl_tab_pivot(tab, row, col);
1268 static void swap_cols(struct isl_tab *tab, int col1, int col2)
1271 unsigned off = 2 + tab->M;
1272 t = tab->col_var[col1];
1273 tab->col_var[col1] = tab->col_var[col2];
1274 tab->col_var[col2] = t;
1275 var_from_col(tab, col1)->index = col1;
1276 var_from_col(tab, col2)->index = col2;
1277 tab->mat = isl_mat_swap_cols(tab->mat, off + col1, off + col2);
1280 /* Mark column with index "col" as representing a zero variable.
1281 * If we may need to undo the operation the column is kept,
1282 * but no longer considered.
1283 * Otherwise, the column is simply removed.
1285 * The column may be interchanged with some other column. If it
1286 * is interchanged with a later column, return 1. Otherwise return 0.
1287 * If the columns are checked in order in the calling function,
1288 * then a return value of 1 means that the column with the given
1289 * column number may now contain a different column that
1290 * hasn't been checked yet.
1292 int isl_tab_kill_col(struct isl_tab *tab, int col)
1294 var_from_col(tab, col)->is_zero = 1;
1295 if (tab->need_undo) {
1296 isl_tab_push_var(tab, isl_tab_undo_zero, var_from_col(tab, col));
1297 if (col != tab->n_dead)
1298 swap_cols(tab, col, tab->n_dead);
1302 if (col != tab->n_col - 1)
1303 swap_cols(tab, col, tab->n_col - 1);
1304 var_from_col(tab, tab->n_col - 1)->index = -1;
1310 /* Row variable "var" is non-negative and cannot attain any values
1311 * larger than zero. This means that the coefficients of the unrestricted
1312 * column variables are zero and that the coefficients of the non-negative
1313 * column variables are zero or negative.
1314 * Each of the non-negative variables with a negative coefficient can
1315 * then also be written as the negative sum of non-negative variables
1316 * and must therefore also be zero.
1318 static void close_row(struct isl_tab *tab, struct isl_tab_var *var)
1321 struct isl_mat *mat = tab->mat;
1322 unsigned off = 2 + tab->M;
1324 isl_assert(tab->mat->ctx, var->is_nonneg, return);
1327 isl_tab_push_var(tab, isl_tab_undo_zero, var);
1328 for (j = tab->n_dead; j < tab->n_col; ++j) {
1329 if (isl_int_is_zero(mat->row[var->index][off + j]))
1331 isl_assert(tab->mat->ctx,
1332 isl_int_is_neg(mat->row[var->index][off + j]), return);
1333 if (isl_tab_kill_col(tab, j))
1336 isl_tab_mark_redundant(tab, var->index);
1339 /* Add a constraint to the tableau and allocate a row for it.
1340 * Return the index into the constraint array "con".
1342 int isl_tab_allocate_con(struct isl_tab *tab)
1346 isl_assert(tab->mat->ctx, tab->n_row < tab->mat->n_row, return -1);
1347 isl_assert(tab->mat->ctx, tab->n_con < tab->max_con, return -1);
1350 tab->con[r].index = tab->n_row;
1351 tab->con[r].is_row = 1;
1352 tab->con[r].is_nonneg = 0;
1353 tab->con[r].is_zero = 0;
1354 tab->con[r].is_redundant = 0;
1355 tab->con[r].frozen = 0;
1356 tab->con[r].negated = 0;
1357 tab->row_var[tab->n_row] = ~r;
1361 isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->con[r]);
1366 /* Add a variable to the tableau and allocate a column for it.
1367 * Return the index into the variable array "var".
1369 int isl_tab_allocate_var(struct isl_tab *tab)
1373 unsigned off = 2 + tab->M;
1375 isl_assert(tab->mat->ctx, tab->n_col < tab->mat->n_col, return -1);
1376 isl_assert(tab->mat->ctx, tab->n_var < tab->max_var, return -1);
1379 tab->var[r].index = tab->n_col;
1380 tab->var[r].is_row = 0;
1381 tab->var[r].is_nonneg = 0;
1382 tab->var[r].is_zero = 0;
1383 tab->var[r].is_redundant = 0;
1384 tab->var[r].frozen = 0;
1385 tab->var[r].negated = 0;
1386 tab->col_var[tab->n_col] = r;
1388 for (i = 0; i < tab->n_row; ++i)
1389 isl_int_set_si(tab->mat->row[i][off + tab->n_col], 0);
1393 isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->var[r]);
1398 /* Add a row to the tableau. The row is given as an affine combination
1399 * of the original variables and needs to be expressed in terms of the
1402 * We add each term in turn.
1403 * If r = n/d_r is the current sum and we need to add k x, then
1404 * if x is a column variable, we increase the numerator of
1405 * this column by k d_r
1406 * if x = f/d_x is a row variable, then the new representation of r is
1408 * n k f d_x/g n + d_r/g k f m/d_r n + m/d_g k f
1409 * --- + --- = ------------------- = -------------------
1410 * d_r d_r d_r d_x/g m
1412 * with g the gcd of d_r and d_x and m the lcm of d_r and d_x.
1414 int isl_tab_add_row(struct isl_tab *tab, isl_int *line)
1420 unsigned off = 2 + tab->M;
1422 r = isl_tab_allocate_con(tab);
1428 row = tab->mat->row[tab->con[r].index];
1429 isl_int_set_si(row[0], 1);
1430 isl_int_set(row[1], line[0]);
1431 isl_seq_clr(row + 2, tab->M + tab->n_col);
1432 for (i = 0; i < tab->n_var; ++i) {
1433 if (tab->var[i].is_zero)
1435 if (tab->var[i].is_row) {
1437 row[0], tab->mat->row[tab->var[i].index][0]);
1438 isl_int_swap(a, row[0]);
1439 isl_int_divexact(a, row[0], a);
1441 row[0], tab->mat->row[tab->var[i].index][0]);
1442 isl_int_mul(b, b, line[1 + i]);
1443 isl_seq_combine(row + 1, a, row + 1,
1444 b, tab->mat->row[tab->var[i].index] + 1,
1445 1 + tab->M + tab->n_col);
1447 isl_int_addmul(row[off + tab->var[i].index],
1448 line[1 + i], row[0]);
1449 if (tab->M && i >= tab->n_param && i < tab->n_var - tab->n_div)
1450 isl_int_submul(row[2], line[1 + i], row[0]);
1452 isl_seq_normalize(tab->mat->ctx, row, off + tab->n_col);
1457 tab->row_sign[tab->con[r].index] = 0;
1462 static int drop_row(struct isl_tab *tab, int row)
1464 isl_assert(tab->mat->ctx, ~tab->row_var[row] == tab->n_con - 1, return -1);
1465 if (row != tab->n_row - 1)
1466 swap_rows(tab, row, tab->n_row - 1);
1472 static int drop_col(struct isl_tab *tab, int col)
1474 isl_assert(tab->mat->ctx, tab->col_var[col] == tab->n_var - 1, return -1);
1475 if (col != tab->n_col - 1)
1476 swap_cols(tab, col, tab->n_col - 1);
1482 /* Add inequality "ineq" and check if it conflicts with the
1483 * previously added constraints or if it is obviously redundant.
1485 struct isl_tab *isl_tab_add_ineq(struct isl_tab *tab, isl_int *ineq)
1492 r = isl_tab_add_row(tab, ineq);
1495 tab->con[r].is_nonneg = 1;
1496 isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]);
1497 if (isl_tab_row_is_redundant(tab, tab->con[r].index)) {
1498 isl_tab_mark_redundant(tab, tab->con[r].index);
1502 sgn = restore_row(tab, &tab->con[r]);
1504 return isl_tab_mark_empty(tab);
1505 if (tab->con[r].is_row && isl_tab_row_is_redundant(tab, tab->con[r].index))
1506 isl_tab_mark_redundant(tab, tab->con[r].index);
1513 /* Pivot a non-negative variable down until it reaches the value zero
1514 * and then pivot the variable into a column position.
1516 static int to_col(struct isl_tab *tab, struct isl_tab_var *var)
1520 unsigned off = 2 + tab->M;
1525 while (isl_int_is_pos(tab->mat->row[var->index][1])) {
1526 find_pivot(tab, var, NULL, -1, &row, &col);
1527 isl_assert(tab->mat->ctx, row != -1, return -1);
1528 isl_tab_pivot(tab, row, col);
1533 for (i = tab->n_dead; i < tab->n_col; ++i)
1534 if (!isl_int_is_zero(tab->mat->row[var->index][off + i]))
1537 isl_assert(tab->mat->ctx, i < tab->n_col, return -1);
1538 isl_tab_pivot(tab, var->index, i);
1543 /* We assume Gaussian elimination has been performed on the equalities.
1544 * The equalities can therefore never conflict.
1545 * Adding the equalities is currently only really useful for a later call
1546 * to isl_tab_ineq_type.
1548 static struct isl_tab *add_eq(struct isl_tab *tab, isl_int *eq)
1555 r = isl_tab_add_row(tab, eq);
1559 r = tab->con[r].index;
1560 i = isl_seq_first_non_zero(tab->mat->row[r] + 2 + tab->M + tab->n_dead,
1561 tab->n_col - tab->n_dead);
1562 isl_assert(tab->mat->ctx, i >= 0, goto error);
1564 isl_tab_pivot(tab, r, i);
1565 isl_tab_kill_col(tab, i);
1574 static int row_is_manifestly_zero(struct isl_tab *tab, int row)
1576 unsigned off = 2 + tab->M;
1578 if (!isl_int_is_zero(tab->mat->row[row][1]))
1580 if (tab->M && !isl_int_is_zero(tab->mat->row[row][2]))
1582 return isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
1583 tab->n_col - tab->n_dead) == -1;
1586 /* Add an equality that is known to be valid for the given tableau.
1588 struct isl_tab *isl_tab_add_valid_eq(struct isl_tab *tab, isl_int *eq)
1590 struct isl_tab_var *var;
1595 r = isl_tab_add_row(tab, eq);
1601 if (row_is_manifestly_zero(tab, r)) {
1603 isl_tab_mark_redundant(tab, r);
1607 if (isl_int_is_neg(tab->mat->row[r][1])) {
1608 isl_seq_neg(tab->mat->row[r] + 1, tab->mat->row[r] + 1,
1613 if (to_col(tab, var) < 0)
1616 isl_tab_kill_col(tab, var->index);
1624 /* Add equality "eq" and check if it conflicts with the
1625 * previously added constraints or if it is obviously redundant.
1627 struct isl_tab *isl_tab_add_eq(struct isl_tab *tab, isl_int *eq)
1629 struct isl_tab_undo *snap = NULL;
1630 struct isl_tab_var *var;
1637 isl_assert(tab->mat->ctx, !tab->M, goto error);
1640 snap = isl_tab_snap(tab);
1642 r = isl_tab_add_row(tab, eq);
1648 if (row_is_manifestly_zero(tab, row)) {
1650 if (isl_tab_rollback(tab, snap) < 0)
1657 sgn = isl_int_sgn(tab->mat->row[row][1]);
1660 isl_seq_neg(tab->mat->row[row] + 1, tab->mat->row[row] + 1,
1666 if (sgn < 0 && sign_of_max(tab, var) < 0)
1667 return isl_tab_mark_empty(tab);
1670 if (to_col(tab, var) < 0)
1673 isl_tab_kill_col(tab, var->index);
1681 struct isl_tab *isl_tab_from_basic_map(struct isl_basic_map *bmap)
1684 struct isl_tab *tab;
1688 tab = isl_tab_alloc(bmap->ctx,
1689 isl_basic_map_total_dim(bmap) + bmap->n_ineq + 1,
1690 isl_basic_map_total_dim(bmap), 0);
1693 tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
1694 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1695 return isl_tab_mark_empty(tab);
1696 for (i = 0; i < bmap->n_eq; ++i) {
1697 tab = add_eq(tab, bmap->eq[i]);
1701 for (i = 0; i < bmap->n_ineq; ++i) {
1702 tab = isl_tab_add_ineq(tab, bmap->ineq[i]);
1703 if (!tab || tab->empty)
1709 struct isl_tab *isl_tab_from_basic_set(struct isl_basic_set *bset)
1711 return isl_tab_from_basic_map((struct isl_basic_map *)bset);
1714 /* Construct a tableau corresponding to the recession cone of "bset".
1716 struct isl_tab *isl_tab_from_recession_cone(struct isl_basic_set *bset)
1720 struct isl_tab *tab;
1724 tab = isl_tab_alloc(bset->ctx, bset->n_eq + bset->n_ineq,
1725 isl_basic_set_total_dim(bset), 0);
1728 tab->rational = ISL_F_ISSET(bset, ISL_BASIC_SET_RATIONAL);
1731 for (i = 0; i < bset->n_eq; ++i) {
1732 isl_int_swap(bset->eq[i][0], cst);
1733 tab = add_eq(tab, bset->eq[i]);
1734 isl_int_swap(bset->eq[i][0], cst);
1738 for (i = 0; i < bset->n_ineq; ++i) {
1740 isl_int_swap(bset->ineq[i][0], cst);
1741 r = isl_tab_add_row(tab, bset->ineq[i]);
1742 isl_int_swap(bset->ineq[i][0], cst);
1745 tab->con[r].is_nonneg = 1;
1746 isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]);
1757 /* Assuming "tab" is the tableau of a cone, check if the cone is
1758 * bounded, i.e., if it is empty or only contains the origin.
1760 int isl_tab_cone_is_bounded(struct isl_tab *tab)
1768 if (tab->n_dead == tab->n_col)
1772 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1773 struct isl_tab_var *var;
1774 var = isl_tab_var_from_row(tab, i);
1775 if (!var->is_nonneg)
1777 if (sign_of_max(tab, var) != 0)
1779 close_row(tab, var);
1782 if (tab->n_dead == tab->n_col)
1784 if (i == tab->n_row)
1789 int isl_tab_sample_is_integer(struct isl_tab *tab)
1796 for (i = 0; i < tab->n_var; ++i) {
1798 if (!tab->var[i].is_row)
1800 row = tab->var[i].index;
1801 if (!isl_int_is_divisible_by(tab->mat->row[row][1],
1802 tab->mat->row[row][0]))
1808 static struct isl_vec *extract_integer_sample(struct isl_tab *tab)
1811 struct isl_vec *vec;
1813 vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
1817 isl_int_set_si(vec->block.data[0], 1);
1818 for (i = 0; i < tab->n_var; ++i) {
1819 if (!tab->var[i].is_row)
1820 isl_int_set_si(vec->block.data[1 + i], 0);
1822 int row = tab->var[i].index;
1823 isl_int_divexact(vec->block.data[1 + i],
1824 tab->mat->row[row][1], tab->mat->row[row][0]);
1831 struct isl_vec *isl_tab_get_sample_value(struct isl_tab *tab)
1834 struct isl_vec *vec;
1840 vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
1846 isl_int_set_si(vec->block.data[0], 1);
1847 for (i = 0; i < tab->n_var; ++i) {
1849 if (!tab->var[i].is_row) {
1850 isl_int_set_si(vec->block.data[1 + i], 0);
1853 row = tab->var[i].index;
1854 isl_int_gcd(m, vec->block.data[0], tab->mat->row[row][0]);
1855 isl_int_divexact(m, tab->mat->row[row][0], m);
1856 isl_seq_scale(vec->block.data, vec->block.data, m, 1 + i);
1857 isl_int_divexact(m, vec->block.data[0], tab->mat->row[row][0]);
1858 isl_int_mul(vec->block.data[1 + i], m, tab->mat->row[row][1]);
1860 vec = isl_vec_normalize(vec);
1866 /* Update "bmap" based on the results of the tableau "tab".
1867 * In particular, implicit equalities are made explicit, redundant constraints
1868 * are removed and if the sample value happens to be integer, it is stored
1869 * in "bmap" (unless "bmap" already had an integer sample).
1871 * The tableau is assumed to have been created from "bmap" using
1872 * isl_tab_from_basic_map.
1874 struct isl_basic_map *isl_basic_map_update_from_tab(struct isl_basic_map *bmap,
1875 struct isl_tab *tab)
1887 bmap = isl_basic_map_set_to_empty(bmap);
1889 for (i = bmap->n_ineq - 1; i >= 0; --i) {
1890 if (isl_tab_is_equality(tab, n_eq + i))
1891 isl_basic_map_inequality_to_equality(bmap, i);
1892 else if (isl_tab_is_redundant(tab, n_eq + i))
1893 isl_basic_map_drop_inequality(bmap, i);
1895 if (!tab->rational &&
1896 !bmap->sample && isl_tab_sample_is_integer(tab))
1897 bmap->sample = extract_integer_sample(tab);
1901 struct isl_basic_set *isl_basic_set_update_from_tab(struct isl_basic_set *bset,
1902 struct isl_tab *tab)
1904 return (struct isl_basic_set *)isl_basic_map_update_from_tab(
1905 (struct isl_basic_map *)bset, tab);
1908 /* Given a non-negative variable "var", add a new non-negative variable
1909 * that is the opposite of "var", ensuring that var can only attain the
1911 * If var = n/d is a row variable, then the new variable = -n/d.
1912 * If var is a column variables, then the new variable = -var.
1913 * If the new variable cannot attain non-negative values, then
1914 * the resulting tableau is empty.
1915 * Otherwise, we know the value will be zero and we close the row.
1917 static struct isl_tab *cut_to_hyperplane(struct isl_tab *tab,
1918 struct isl_tab_var *var)
1923 unsigned off = 2 + tab->M;
1927 isl_assert(tab->mat->ctx, !var->is_redundant, goto error);
1929 if (isl_tab_extend_cons(tab, 1) < 0)
1933 tab->con[r].index = tab->n_row;
1934 tab->con[r].is_row = 1;
1935 tab->con[r].is_nonneg = 0;
1936 tab->con[r].is_zero = 0;
1937 tab->con[r].is_redundant = 0;
1938 tab->con[r].frozen = 0;
1939 tab->con[r].negated = 0;
1940 tab->row_var[tab->n_row] = ~r;
1941 row = tab->mat->row[tab->n_row];
1944 isl_int_set(row[0], tab->mat->row[var->index][0]);
1945 isl_seq_neg(row + 1,
1946 tab->mat->row[var->index] + 1, 1 + tab->n_col);
1948 isl_int_set_si(row[0], 1);
1949 isl_seq_clr(row + 1, 1 + tab->n_col);
1950 isl_int_set_si(row[off + var->index], -1);
1955 isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->con[r]);
1957 sgn = sign_of_max(tab, &tab->con[r]);
1959 return isl_tab_mark_empty(tab);
1960 tab->con[r].is_nonneg = 1;
1961 isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]);
1963 close_row(tab, &tab->con[r]);
1971 /* Given a tableau "tab" and an inequality constraint "con" of the tableau,
1972 * relax the inequality by one. That is, the inequality r >= 0 is replaced
1973 * by r' = r + 1 >= 0.
1974 * If r is a row variable, we simply increase the constant term by one
1975 * (taking into account the denominator).
1976 * If r is a column variable, then we need to modify each row that
1977 * refers to r = r' - 1 by substituting this equality, effectively
1978 * subtracting the coefficient of the column from the constant.
1980 struct isl_tab *isl_tab_relax(struct isl_tab *tab, int con)
1982 struct isl_tab_var *var;
1983 unsigned off = 2 + tab->M;
1988 var = &tab->con[con];
1990 if (!var->is_row && !max_is_manifestly_unbounded(tab, var))
1991 to_row(tab, var, 1);
1994 isl_int_add(tab->mat->row[var->index][1],
1995 tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
1999 for (i = 0; i < tab->n_row; ++i) {
2000 if (isl_int_is_zero(tab->mat->row[i][off + var->index]))
2002 isl_int_sub(tab->mat->row[i][1], tab->mat->row[i][1],
2003 tab->mat->row[i][off + var->index]);
2008 isl_tab_push_var(tab, isl_tab_undo_relax, var);
2013 struct isl_tab *isl_tab_select_facet(struct isl_tab *tab, int con)
2018 return cut_to_hyperplane(tab, &tab->con[con]);
2021 static int may_be_equality(struct isl_tab *tab, int row)
2023 unsigned off = 2 + tab->M;
2024 return (tab->rational ? isl_int_is_zero(tab->mat->row[row][1])
2025 : isl_int_lt(tab->mat->row[row][1],
2026 tab->mat->row[row][0])) &&
2027 isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
2028 tab->n_col - tab->n_dead) != -1;
2031 /* Check for (near) equalities among the constraints.
2032 * A constraint is an equality if it is non-negative and if
2033 * its maximal value is either
2034 * - zero (in case of rational tableaus), or
2035 * - strictly less than 1 (in case of integer tableaus)
2037 * We first mark all non-redundant and non-dead variables that
2038 * are not frozen and not obviously not an equality.
2039 * Then we iterate over all marked variables if they can attain
2040 * any values larger than zero or at least one.
2041 * If the maximal value is zero, we mark any column variables
2042 * that appear in the row as being zero and mark the row as being redundant.
2043 * Otherwise, if the maximal value is strictly less than one (and the
2044 * tableau is integer), then we restrict the value to being zero
2045 * by adding an opposite non-negative variable.
2047 struct isl_tab *isl_tab_detect_implicit_equalities(struct isl_tab *tab)
2056 if (tab->n_dead == tab->n_col)
2060 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2061 struct isl_tab_var *var = isl_tab_var_from_row(tab, i);
2062 var->marked = !var->frozen && var->is_nonneg &&
2063 may_be_equality(tab, i);
2067 for (i = tab->n_dead; i < tab->n_col; ++i) {
2068 struct isl_tab_var *var = var_from_col(tab, i);
2069 var->marked = !var->frozen && var->is_nonneg;
2074 struct isl_tab_var *var;
2075 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2076 var = isl_tab_var_from_row(tab, i);
2080 if (i == tab->n_row) {
2081 for (i = tab->n_dead; i < tab->n_col; ++i) {
2082 var = var_from_col(tab, i);
2086 if (i == tab->n_col)
2091 if (sign_of_max(tab, var) == 0)
2092 close_row(tab, var);
2093 else if (!tab->rational && !at_least_one(tab, var)) {
2094 tab = cut_to_hyperplane(tab, var);
2095 return isl_tab_detect_implicit_equalities(tab);
2097 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2098 var = isl_tab_var_from_row(tab, i);
2101 if (may_be_equality(tab, i))
2111 /* Check for (near) redundant constraints.
2112 * A constraint is redundant if it is non-negative and if
2113 * its minimal value (temporarily ignoring the non-negativity) is either
2114 * - zero (in case of rational tableaus), or
2115 * - strictly larger than -1 (in case of integer tableaus)
2117 * We first mark all non-redundant and non-dead variables that
2118 * are not frozen and not obviously negatively unbounded.
2119 * Then we iterate over all marked variables if they can attain
2120 * any values smaller than zero or at most negative one.
2121 * If not, we mark the row as being redundant (assuming it hasn't
2122 * been detected as being obviously redundant in the mean time).
2124 struct isl_tab *isl_tab_detect_redundant(struct isl_tab *tab)
2133 if (tab->n_redundant == tab->n_row)
2137 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2138 struct isl_tab_var *var = isl_tab_var_from_row(tab, i);
2139 var->marked = !var->frozen && var->is_nonneg;
2143 for (i = tab->n_dead; i < tab->n_col; ++i) {
2144 struct isl_tab_var *var = var_from_col(tab, i);
2145 var->marked = !var->frozen && var->is_nonneg &&
2146 !min_is_manifestly_unbounded(tab, var);
2151 struct isl_tab_var *var;
2152 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2153 var = isl_tab_var_from_row(tab, i);
2157 if (i == tab->n_row) {
2158 for (i = tab->n_dead; i < tab->n_col; ++i) {
2159 var = var_from_col(tab, i);
2163 if (i == tab->n_col)
2168 if ((tab->rational ? (sign_of_min(tab, var) >= 0)
2169 : !isl_tab_min_at_most_neg_one(tab, var)) &&
2171 isl_tab_mark_redundant(tab, var->index);
2172 for (i = tab->n_dead; i < tab->n_col; ++i) {
2173 var = var_from_col(tab, i);
2176 if (!min_is_manifestly_unbounded(tab, var))
2186 int isl_tab_is_equality(struct isl_tab *tab, int con)
2193 if (tab->con[con].is_zero)
2195 if (tab->con[con].is_redundant)
2197 if (!tab->con[con].is_row)
2198 return tab->con[con].index < tab->n_dead;
2200 row = tab->con[con].index;
2203 return isl_int_is_zero(tab->mat->row[row][1]) &&
2204 isl_seq_first_non_zero(tab->mat->row[row] + 2 + tab->n_dead,
2205 tab->n_col - tab->n_dead) == -1;
2208 /* Return the minimial value of the affine expression "f" with denominator
2209 * "denom" in *opt, *opt_denom, assuming the tableau is not empty and
2210 * the expression cannot attain arbitrarily small values.
2211 * If opt_denom is NULL, then *opt is rounded up to the nearest integer.
2212 * The return value reflects the nature of the result (empty, unbounded,
2213 * minmimal value returned in *opt).
2215 enum isl_lp_result isl_tab_min(struct isl_tab *tab,
2216 isl_int *f, isl_int denom, isl_int *opt, isl_int *opt_denom,
2220 enum isl_lp_result res = isl_lp_ok;
2221 struct isl_tab_var *var;
2222 struct isl_tab_undo *snap;
2225 return isl_lp_empty;
2227 snap = isl_tab_snap(tab);
2228 r = isl_tab_add_row(tab, f);
2230 return isl_lp_error;
2232 isl_int_mul(tab->mat->row[var->index][0],
2233 tab->mat->row[var->index][0], denom);
2236 find_pivot(tab, var, var, -1, &row, &col);
2237 if (row == var->index) {
2238 res = isl_lp_unbounded;
2243 isl_tab_pivot(tab, row, col);
2245 if (ISL_FL_ISSET(flags, ISL_TAB_SAVE_DUAL)) {
2248 isl_vec_free(tab->dual);
2249 tab->dual = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_con);
2251 return isl_lp_error;
2252 isl_int_set(tab->dual->el[0], tab->mat->row[var->index][0]);
2253 for (i = 0; i < tab->n_con; ++i) {
2255 if (tab->con[i].is_row) {
2256 isl_int_set_si(tab->dual->el[1 + i], 0);
2259 pos = 2 + tab->M + tab->con[i].index;
2260 if (tab->con[i].negated)
2261 isl_int_neg(tab->dual->el[1 + i],
2262 tab->mat->row[var->index][pos]);
2264 isl_int_set(tab->dual->el[1 + i],
2265 tab->mat->row[var->index][pos]);
2268 if (opt && res == isl_lp_ok) {
2270 isl_int_set(*opt, tab->mat->row[var->index][1]);
2271 isl_int_set(*opt_denom, tab->mat->row[var->index][0]);
2273 isl_int_cdiv_q(*opt, tab->mat->row[var->index][1],
2274 tab->mat->row[var->index][0]);
2276 if (isl_tab_rollback(tab, snap) < 0)
2277 return isl_lp_error;
2281 int isl_tab_is_redundant(struct isl_tab *tab, int con)
2285 if (tab->con[con].is_zero)
2287 if (tab->con[con].is_redundant)
2289 return tab->con[con].is_row && tab->con[con].index < tab->n_redundant;
2292 /* Take a snapshot of the tableau that can be restored by s call to
2295 struct isl_tab_undo *isl_tab_snap(struct isl_tab *tab)
2303 /* Undo the operation performed by isl_tab_relax.
2305 static void unrelax(struct isl_tab *tab, struct isl_tab_var *var)
2307 unsigned off = 2 + tab->M;
2309 if (!var->is_row && !max_is_manifestly_unbounded(tab, var))
2310 to_row(tab, var, 1);
2313 isl_int_sub(tab->mat->row[var->index][1],
2314 tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
2318 for (i = 0; i < tab->n_row; ++i) {
2319 if (isl_int_is_zero(tab->mat->row[i][off + var->index]))
2321 isl_int_add(tab->mat->row[i][1], tab->mat->row[i][1],
2322 tab->mat->row[i][off + var->index]);
2328 static void perform_undo_var(struct isl_tab *tab, struct isl_tab_undo *undo)
2330 struct isl_tab_var *var = var_from_index(tab, undo->u.var_index);
2331 switch(undo->type) {
2332 case isl_tab_undo_nonneg:
2335 case isl_tab_undo_redundant:
2336 var->is_redundant = 0;
2339 case isl_tab_undo_zero:
2344 case isl_tab_undo_allocate:
2345 if (undo->u.var_index >= 0) {
2346 isl_assert(tab->mat->ctx, !var->is_row, return);
2347 drop_col(tab, var->index);
2351 if (!max_is_manifestly_unbounded(tab, var))
2352 to_row(tab, var, 1);
2353 else if (!min_is_manifestly_unbounded(tab, var))
2354 to_row(tab, var, -1);
2356 to_row(tab, var, 0);
2358 drop_row(tab, var->index);
2360 case isl_tab_undo_relax:
2366 /* Restore the tableau to the state where the basic variables
2367 * are those in "col_var".
2368 * We first construct a list of variables that are currently in
2369 * the basis, but shouldn't. Then we iterate over all variables
2370 * that should be in the basis and for each one that is currently
2371 * not in the basis, we exchange it with one of the elements of the
2372 * list constructed before.
2373 * We can always find an appropriate variable to pivot with because
2374 * the current basis is mapped to the old basis by a non-singular
2375 * matrix and so we can never end up with a zero row.
2377 static int restore_basis(struct isl_tab *tab, int *col_var)
2381 int *extra = NULL; /* current columns that contain bad stuff */
2382 unsigned off = 2 + tab->M;
2384 extra = isl_alloc_array(tab->mat->ctx, int, tab->n_col);
2387 for (i = 0; i < tab->n_col; ++i) {
2388 for (j = 0; j < tab->n_col; ++j)
2389 if (tab->col_var[i] == col_var[j])
2393 extra[n_extra++] = i;
2395 for (i = 0; i < tab->n_col && n_extra > 0; ++i) {
2396 struct isl_tab_var *var;
2399 for (j = 0; j < tab->n_col; ++j)
2400 if (col_var[i] == tab->col_var[j])
2404 var = var_from_index(tab, col_var[i]);
2406 for (j = 0; j < n_extra; ++j)
2407 if (!isl_int_is_zero(tab->mat->row[row][off+extra[j]]))
2409 isl_assert(tab->mat->ctx, j < n_extra, goto error);
2410 isl_tab_pivot(tab, row, extra[j]);
2411 extra[j] = extra[--n_extra];
2423 static int perform_undo(struct isl_tab *tab, struct isl_tab_undo *undo)
2425 switch (undo->type) {
2426 case isl_tab_undo_empty:
2429 case isl_tab_undo_nonneg:
2430 case isl_tab_undo_redundant:
2431 case isl_tab_undo_zero:
2432 case isl_tab_undo_allocate:
2433 case isl_tab_undo_relax:
2434 perform_undo_var(tab, undo);
2436 case isl_tab_undo_bset_eq:
2437 isl_basic_set_free_equality(tab->bset, 1);
2439 case isl_tab_undo_bset_ineq:
2440 isl_basic_set_free_inequality(tab->bset, 1);
2442 case isl_tab_undo_bset_div:
2443 isl_basic_set_free_div(tab->bset, 1);
2445 tab->samples->n_col--;
2447 case isl_tab_undo_saved_basis:
2448 if (restore_basis(tab, undo->u.col_var) < 0)
2451 case isl_tab_undo_drop_sample:
2455 isl_assert(tab->mat->ctx, 0, return -1);
2460 /* Return the tableau to the state it was in when the snapshot "snap"
2463 int isl_tab_rollback(struct isl_tab *tab, struct isl_tab_undo *snap)
2465 struct isl_tab_undo *undo, *next;
2471 for (undo = tab->top; undo && undo != &tab->bottom; undo = next) {
2475 if (perform_undo(tab, undo) < 0) {
2489 /* The given row "row" represents an inequality violated by all
2490 * points in the tableau. Check for some special cases of such
2491 * separating constraints.
2492 * In particular, if the row has been reduced to the constant -1,
2493 * then we know the inequality is adjacent (but opposite) to
2494 * an equality in the tableau.
2495 * If the row has been reduced to r = -1 -r', with r' an inequality
2496 * of the tableau, then the inequality is adjacent (but opposite)
2497 * to the inequality r'.
2499 static enum isl_ineq_type separation_type(struct isl_tab *tab, unsigned row)
2502 unsigned off = 2 + tab->M;
2505 return isl_ineq_separate;
2507 if (!isl_int_is_one(tab->mat->row[row][0]))
2508 return isl_ineq_separate;
2509 if (!isl_int_is_negone(tab->mat->row[row][1]))
2510 return isl_ineq_separate;
2512 pos = isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
2513 tab->n_col - tab->n_dead);
2515 return isl_ineq_adj_eq;
2517 if (!isl_int_is_negone(tab->mat->row[row][off + tab->n_dead + pos]))
2518 return isl_ineq_separate;
2520 pos = isl_seq_first_non_zero(
2521 tab->mat->row[row] + off + tab->n_dead + pos + 1,
2522 tab->n_col - tab->n_dead - pos - 1);
2524 return pos == -1 ? isl_ineq_adj_ineq : isl_ineq_separate;
2527 /* Check the effect of inequality "ineq" on the tableau "tab".
2529 * isl_ineq_redundant: satisfied by all points in the tableau
2530 * isl_ineq_separate: satisfied by no point in the tableau
2531 * isl_ineq_cut: satisfied by some by not all points
2532 * isl_ineq_adj_eq: adjacent to an equality
2533 * isl_ineq_adj_ineq: adjacent to an inequality.
2535 enum isl_ineq_type isl_tab_ineq_type(struct isl_tab *tab, isl_int *ineq)
2537 enum isl_ineq_type type = isl_ineq_error;
2538 struct isl_tab_undo *snap = NULL;
2543 return isl_ineq_error;
2545 if (isl_tab_extend_cons(tab, 1) < 0)
2546 return isl_ineq_error;
2548 snap = isl_tab_snap(tab);
2550 con = isl_tab_add_row(tab, ineq);
2554 row = tab->con[con].index;
2555 if (isl_tab_row_is_redundant(tab, row))
2556 type = isl_ineq_redundant;
2557 else if (isl_int_is_neg(tab->mat->row[row][1]) &&
2559 isl_int_abs_ge(tab->mat->row[row][1],
2560 tab->mat->row[row][0]))) {
2561 if (at_least_zero(tab, &tab->con[con]))
2562 type = isl_ineq_cut;
2564 type = separation_type(tab, row);
2565 } else if (tab->rational ? (sign_of_min(tab, &tab->con[con]) < 0)
2566 : isl_tab_min_at_most_neg_one(tab, &tab->con[con]))
2567 type = isl_ineq_cut;
2569 type = isl_ineq_redundant;
2571 if (isl_tab_rollback(tab, snap))
2572 return isl_ineq_error;
2575 isl_tab_rollback(tab, snap);
2576 return isl_ineq_error;
2579 void isl_tab_dump(struct isl_tab *tab, FILE *out, int indent)
2585 fprintf(out, "%*snull tab\n", indent, "");
2588 fprintf(out, "%*sn_redundant: %d, n_dead: %d", indent, "",
2589 tab->n_redundant, tab->n_dead);
2591 fprintf(out, ", rational");
2593 fprintf(out, ", empty");
2595 fprintf(out, "%*s[", indent, "");
2596 for (i = 0; i < tab->n_var; ++i) {
2598 fprintf(out, (i == tab->n_param ||
2599 i == tab->n_var - tab->n_div) ? "; "
2601 fprintf(out, "%c%d%s", tab->var[i].is_row ? 'r' : 'c',
2603 tab->var[i].is_zero ? " [=0]" :
2604 tab->var[i].is_redundant ? " [R]" : "");
2606 fprintf(out, "]\n");
2607 fprintf(out, "%*s[", indent, "");
2608 for (i = 0; i < tab->n_con; ++i) {
2611 fprintf(out, "%c%d%s", tab->con[i].is_row ? 'r' : 'c',
2613 tab->con[i].is_zero ? " [=0]" :
2614 tab->con[i].is_redundant ? " [R]" : "");
2616 fprintf(out, "]\n");
2617 fprintf(out, "%*s[", indent, "");
2618 for (i = 0; i < tab->n_row; ++i) {
2619 const char *sign = "";
2622 if (tab->row_sign) {
2623 if (tab->row_sign[i] == isl_tab_row_unknown)
2625 else if (tab->row_sign[i] == isl_tab_row_neg)
2627 else if (tab->row_sign[i] == isl_tab_row_pos)
2632 fprintf(out, "r%d: %d%s%s", i, tab->row_var[i],
2633 isl_tab_var_from_row(tab, i)->is_nonneg ? " [>=0]" : "", sign);
2635 fprintf(out, "]\n");
2636 fprintf(out, "%*s[", indent, "");
2637 for (i = 0; i < tab->n_col; ++i) {
2640 fprintf(out, "c%d: %d%s", i, tab->col_var[i],
2641 var_from_col(tab, i)->is_nonneg ? " [>=0]" : "");
2643 fprintf(out, "]\n");
2644 r = tab->mat->n_row;
2645 tab->mat->n_row = tab->n_row;
2646 c = tab->mat->n_col;
2647 tab->mat->n_col = 2 + tab->M + tab->n_col;
2648 isl_mat_dump(tab->mat, out, indent);
2649 tab->mat->n_row = r;
2650 tab->mat->n_col = c;
2652 isl_basic_set_dump(tab->bset, out, indent);