2 #include "isl_map_private.h"
6 * The implementation of tableaus in this file was inspired by Section 8
7 * of David Detlefs, Greg Nelson and James B. Saxe, "Simplify: a theorem
8 * prover for program checking".
11 struct isl_tab *isl_tab_alloc(struct isl_ctx *ctx,
12 unsigned n_row, unsigned n_var)
17 tab = isl_calloc_type(ctx, struct isl_tab);
20 tab->mat = isl_mat_alloc(ctx, n_row, 2 + n_var);
23 tab->var = isl_alloc_array(ctx, struct isl_tab_var, n_var);
26 tab->con = isl_alloc_array(ctx, struct isl_tab_var, n_row);
29 tab->col_var = isl_alloc_array(ctx, int, n_var);
32 tab->row_var = isl_alloc_array(ctx, int, n_row);
35 for (i = 0; i < n_var; ++i) {
36 tab->var[i].index = i;
37 tab->var[i].is_row = 0;
38 tab->var[i].is_nonneg = 0;
39 tab->var[i].is_zero = 0;
40 tab->var[i].is_redundant = 0;
41 tab->var[i].frozen = 0;
58 tab->bottom.type = isl_tab_undo_bottom;
59 tab->bottom.next = NULL;
60 tab->top = &tab->bottom;
67 int isl_tab_extend_cons(struct isl_tab *tab, unsigned n_new)
69 if (tab->max_con < tab->n_con + n_new) {
70 struct isl_tab_var *con;
72 con = isl_realloc_array(tab->mat->ctx, tab->con,
73 struct isl_tab_var, tab->max_con + n_new);
77 tab->max_con += n_new;
79 if (tab->mat->n_row < tab->n_row + n_new) {
82 tab->mat = isl_mat_extend(tab->mat,
83 tab->n_row + n_new, tab->n_col);
86 row_var = isl_realloc_array(tab->mat->ctx, tab->row_var,
87 int, tab->mat->n_row);
90 tab->row_var = row_var;
95 struct isl_tab *isl_tab_extend(struct isl_tab *tab, unsigned n_new)
97 if (isl_tab_extend_cons(tab, n_new) >= 0)
104 static void free_undo(struct isl_tab *tab)
106 struct isl_tab_undo *undo, *next;
108 for (undo = tab->top; undo && undo != &tab->bottom; undo = next) {
115 void isl_tab_free(struct isl_tab *tab)
120 isl_mat_free(tab->mat);
121 isl_vec_free(tab->dual);
129 struct isl_tab *isl_tab_dup(struct isl_tab *tab)
137 dup = isl_calloc_type(tab->ctx, struct isl_tab);
140 dup->mat = isl_mat_dup(tab->mat);
143 dup->var = isl_alloc_array(tab->ctx, struct isl_tab_var, tab->n_var);
146 for (i = 0; i < tab->n_var; ++i)
147 dup->var[i] = tab->var[i];
148 dup->con = isl_alloc_array(tab->ctx, struct isl_tab_var, tab->max_con);
151 for (i = 0; i < tab->n_con; ++i)
152 dup->con[i] = tab->con[i];
153 dup->col_var = isl_alloc_array(tab->ctx, int, tab->mat->n_col);
156 for (i = 0; i < tab->n_var; ++i)
157 dup->col_var[i] = tab->col_var[i];
158 dup->row_var = isl_alloc_array(tab->ctx, int, tab->mat->n_row);
161 for (i = 0; i < tab->n_row; ++i)
162 dup->row_var[i] = tab->row_var[i];
163 dup->n_row = tab->n_row;
164 dup->n_con = tab->n_con;
165 dup->n_eq = tab->n_eq;
166 dup->max_con = tab->max_con;
167 dup->n_col = tab->n_col;
168 dup->n_var = tab->n_var;
169 dup->n_param = tab->n_param;
170 dup->n_div = tab->n_div;
171 dup->n_dead = tab->n_dead;
172 dup->n_redundant = tab->n_redundant;
173 dup->rational = tab->rational;
174 dup->empty = tab->empty;
177 dup->bottom.type = isl_tab_undo_bottom;
178 dup->bottom.next = NULL;
179 dup->top = &dup->bottom;
186 static struct isl_tab_var *var_from_index(struct isl_tab *tab, int i)
191 return &tab->con[~i];
194 struct isl_tab_var *isl_tab_var_from_row(struct isl_tab *tab, int i)
196 return var_from_index(tab, tab->row_var[i]);
199 static struct isl_tab_var *var_from_col(struct isl_tab *tab, int i)
201 return var_from_index(tab, tab->col_var[i]);
204 /* Check if there are any upper bounds on column variable "var",
205 * i.e., non-negative rows where var appears with a negative coefficient.
206 * Return 1 if there are no such bounds.
208 static int max_is_manifestly_unbounded(struct isl_tab *tab,
209 struct isl_tab_var *var)
215 for (i = tab->n_redundant; i < tab->n_row; ++i) {
216 if (!isl_int_is_neg(tab->mat->row[i][2 + var->index]))
218 if (isl_tab_var_from_row(tab, i)->is_nonneg)
224 /* Check if there are any lower bounds on column variable "var",
225 * i.e., non-negative rows where var appears with a positive coefficient.
226 * Return 1 if there are no such bounds.
228 static int min_is_manifestly_unbounded(struct isl_tab *tab,
229 struct isl_tab_var *var)
235 for (i = tab->n_redundant; i < tab->n_row; ++i) {
236 if (!isl_int_is_pos(tab->mat->row[i][2 + var->index]))
238 if (isl_tab_var_from_row(tab, i)->is_nonneg)
244 /* Given the index of a column "c", return the index of a row
245 * that can be used to pivot the column in, with either an increase
246 * (sgn > 0) or a decrease (sgn < 0) of the corresponding variable.
247 * If "var" is not NULL, then the row returned will be different from
248 * the one associated with "var".
250 * Each row in the tableau is of the form
252 * x_r = a_r0 + \sum_i a_ri x_i
254 * Only rows with x_r >= 0 and with the sign of a_ri opposite to "sgn"
255 * impose any limit on the increase or decrease in the value of x_c
256 * and this bound is equal to a_r0 / |a_rc|. We are therefore looking
257 * for the row with the smallest (most stringent) such bound.
258 * Note that the common denominator of each row drops out of the fraction.
259 * To check if row j has a smaller bound than row r, i.e.,
260 * a_j0 / |a_jc| < a_r0 / |a_rc| or a_j0 |a_rc| < a_r0 |a_jc|,
261 * we check if -sign(a_jc) (a_j0 a_rc - a_r0 a_jc) < 0,
262 * where -sign(a_jc) is equal to "sgn".
264 static int pivot_row(struct isl_tab *tab,
265 struct isl_tab_var *var, int sgn, int c)
272 for (j = tab->n_redundant; j < tab->n_row; ++j) {
273 if (var && j == var->index)
275 if (!isl_tab_var_from_row(tab, j)->is_nonneg)
277 if (sgn * isl_int_sgn(tab->mat->row[j][2 + c]) >= 0)
283 isl_int_mul(t, tab->mat->row[r][1], tab->mat->row[j][2 + c]);
284 isl_int_submul(t, tab->mat->row[j][1], tab->mat->row[r][2 + c]);
285 tsgn = sgn * isl_int_sgn(t);
286 if (tsgn < 0 || (tsgn == 0 &&
287 tab->row_var[j] < tab->row_var[r]))
294 /* Find a pivot (row and col) that will increase (sgn > 0) or decrease
295 * (sgn < 0) the value of row variable var.
296 * If not NULL, then skip_var is a row variable that should be ignored
297 * while looking for a pivot row. It is usually equal to var.
299 * As the given row in the tableau is of the form
301 * x_r = a_r0 + \sum_i a_ri x_i
303 * we need to find a column such that the sign of a_ri is equal to "sgn"
304 * (such that an increase in x_i will have the desired effect) or a
305 * column with a variable that may attain negative values.
306 * If a_ri is positive, then we need to move x_i in the same direction
307 * to obtain the desired effect. Otherwise, x_i has to move in the
308 * opposite direction.
310 static void find_pivot(struct isl_tab *tab,
311 struct isl_tab_var *var, struct isl_tab_var *skip_var,
312 int sgn, int *row, int *col)
319 isl_assert(tab->mat->ctx, var->is_row, return);
320 tr = tab->mat->row[var->index];
323 for (j = tab->n_dead; j < tab->n_col; ++j) {
324 if (isl_int_is_zero(tr[2 + j]))
326 if (isl_int_sgn(tr[2 + j]) != sgn &&
327 var_from_col(tab, j)->is_nonneg)
329 if (c < 0 || tab->col_var[j] < tab->col_var[c])
335 sgn *= isl_int_sgn(tr[2 + c]);
336 r = pivot_row(tab, skip_var, sgn, c);
337 *row = r < 0 ? var->index : r;
341 /* Return 1 if row "row" represents an obviously redundant inequality.
343 * - it represents an inequality or a variable
344 * - that is the sum of a non-negative sample value and a positive
345 * combination of zero or more non-negative variables.
347 int isl_tab_row_is_redundant(struct isl_tab *tab, int row)
351 if (tab->row_var[row] < 0 && !isl_tab_var_from_row(tab, row)->is_nonneg)
354 if (isl_int_is_neg(tab->mat->row[row][1]))
357 for (i = tab->n_dead; i < tab->n_col; ++i) {
358 if (isl_int_is_zero(tab->mat->row[row][2 + i]))
360 if (isl_int_is_neg(tab->mat->row[row][2 + i]))
362 if (!var_from_col(tab, i)->is_nonneg)
368 static void swap_rows(struct isl_tab *tab, int row1, int row2)
371 t = tab->row_var[row1];
372 tab->row_var[row1] = tab->row_var[row2];
373 tab->row_var[row2] = t;
374 isl_tab_var_from_row(tab, row1)->index = row1;
375 isl_tab_var_from_row(tab, row2)->index = row2;
376 tab->mat = isl_mat_swap_rows(tab->mat, row1, row2);
379 static void push_union(struct isl_tab *tab,
380 enum isl_tab_undo_type type, union isl_tab_undo_val u)
382 struct isl_tab_undo *undo;
387 undo = isl_alloc_type(tab->mat->ctx, struct isl_tab_undo);
395 undo->next = tab->top;
399 void isl_tab_push_var(struct isl_tab *tab,
400 enum isl_tab_undo_type type, struct isl_tab_var *var)
402 union isl_tab_undo_val u;
404 u.var_index = tab->row_var[var->index];
406 u.var_index = tab->col_var[var->index];
407 push_union(tab, type, u);
410 void isl_tab_push(struct isl_tab *tab, enum isl_tab_undo_type type)
412 union isl_tab_undo_val u = { 0 };
413 push_union(tab, type, u);
416 /* Push a record on the undo stack describing the current basic
417 * variables, so that the this state can be restored during rollback.
419 void isl_tab_push_basis(struct isl_tab *tab)
422 union isl_tab_undo_val u;
424 u.col_var = isl_alloc_array(tab->mat->ctx, int, tab->n_col);
430 for (i = 0; i < tab->n_col; ++i)
431 u.col_var[i] = tab->col_var[i];
432 push_union(tab, isl_tab_undo_saved_basis, u);
435 /* Mark row with index "row" as being redundant.
436 * If we may need to undo the operation or if the row represents
437 * a variable of the original problem, the row is kept,
438 * but no longer considered when looking for a pivot row.
439 * Otherwise, the row is simply removed.
441 * The row may be interchanged with some other row. If it
442 * is interchanged with a later row, return 1. Otherwise return 0.
443 * If the rows are checked in order in the calling function,
444 * then a return value of 1 means that the row with the given
445 * row number may now contain a different row that hasn't been checked yet.
447 int isl_tab_mark_redundant(struct isl_tab *tab, int row)
449 struct isl_tab_var *var = isl_tab_var_from_row(tab, row);
450 var->is_redundant = 1;
451 isl_assert(tab->mat->ctx, row >= tab->n_redundant, return);
452 if (tab->need_undo || tab->row_var[row] >= 0) {
453 if (tab->row_var[row] >= 0 && !var->is_nonneg) {
455 isl_tab_push_var(tab, isl_tab_undo_nonneg, var);
457 if (row != tab->n_redundant)
458 swap_rows(tab, row, tab->n_redundant);
459 isl_tab_push_var(tab, isl_tab_undo_redundant, var);
463 if (row != tab->n_row - 1)
464 swap_rows(tab, row, tab->n_row - 1);
465 isl_tab_var_from_row(tab, tab->n_row - 1)->index = -1;
471 struct isl_tab *isl_tab_mark_empty(struct isl_tab *tab)
473 if (!tab->empty && tab->need_undo)
474 isl_tab_push(tab, isl_tab_undo_empty);
479 /* Given a row number "row" and a column number "col", pivot the tableau
480 * such that the associated variables are interchanged.
481 * The given row in the tableau expresses
483 * x_r = a_r0 + \sum_i a_ri x_i
487 * x_c = 1/a_rc x_r - a_r0/a_rc + sum_{i \ne r} -a_ri/a_rc
489 * Substituting this equality into the other rows
491 * x_j = a_j0 + \sum_i a_ji x_i
493 * with a_jc \ne 0, we obtain
495 * 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
502 * where i is any other column and j is any other row,
503 * is therefore transformed into
505 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
506 * 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)
508 * The transformation is performed along the following steps
513 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
516 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
517 * n_jc/(|n_rc| d_j) n_ji/(|n_rc| d_j)
519 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
520 * n_jc/(|n_rc| d_j) (n_ji |n_rc|)/(|n_rc| d_j)
522 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
523 * n_jc/(|n_rc| d_j) (n_ji |n_rc| - s(n_rc)n_jc n_ri)/(|n_rc| d_j)
525 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
526 * 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)
529 void isl_tab_pivot(struct isl_tab *tab, int row, int col)
534 struct isl_mat *mat = tab->mat;
535 struct isl_tab_var *var;
537 isl_int_swap(mat->row[row][0], mat->row[row][2 + col]);
538 sgn = isl_int_sgn(mat->row[row][0]);
540 isl_int_neg(mat->row[row][0], mat->row[row][0]);
541 isl_int_neg(mat->row[row][2 + col], mat->row[row][2 + col]);
543 for (j = 0; j < 1 + tab->n_col; ++j) {
546 isl_int_neg(mat->row[row][1 + j], mat->row[row][1 + j]);
548 if (!isl_int_is_one(mat->row[row][0]))
549 isl_seq_normalize(mat->row[row], 2 + tab->n_col);
550 for (i = 0; i < tab->n_row; ++i) {
553 if (isl_int_is_zero(mat->row[i][2 + col]))
555 isl_int_mul(mat->row[i][0], mat->row[i][0], mat->row[row][0]);
556 for (j = 0; j < 1 + tab->n_col; ++j) {
559 isl_int_mul(mat->row[i][1 + j],
560 mat->row[i][1 + j], mat->row[row][0]);
561 isl_int_addmul(mat->row[i][1 + j],
562 mat->row[i][2 + col], mat->row[row][1 + j]);
564 isl_int_mul(mat->row[i][2 + col],
565 mat->row[i][2 + col], mat->row[row][2 + col]);
566 if (!isl_int_is_one(mat->row[i][0]))
567 isl_seq_normalize(mat->row[i], 2 + tab->n_col);
569 t = tab->row_var[row];
570 tab->row_var[row] = tab->col_var[col];
571 tab->col_var[col] = t;
572 var = isl_tab_var_from_row(tab, row);
575 var = var_from_col(tab, col);
580 for (i = tab->n_redundant; i < tab->n_row; ++i) {
581 if (isl_int_is_zero(mat->row[i][2 + col]))
583 if (!isl_tab_var_from_row(tab, i)->frozen &&
584 isl_tab_row_is_redundant(tab, i))
585 if (isl_tab_mark_redundant(tab, i))
590 /* If "var" represents a column variable, then pivot is up (sgn > 0)
591 * or down (sgn < 0) to a row. The variable is assumed not to be
592 * unbounded in the specified direction.
593 * If sgn = 0, then the variable is unbounded in both directions,
594 * and we pivot with any row we can find.
596 static void to_row(struct isl_tab *tab, struct isl_tab_var *var, int sign)
604 for (r = tab->n_redundant; r < tab->n_row; ++r)
605 if (!isl_int_is_zero(tab->mat->row[r][2 + var->index]))
607 isl_assert(tab->mat->ctx, r < tab->n_row, return);
609 r = pivot_row(tab, NULL, sign, var->index);
610 isl_assert(tab->mat->ctx, r >= 0, return);
613 isl_tab_pivot(tab, r, var->index);
616 static void check_table(struct isl_tab *tab)
622 for (i = 0; i < tab->n_row; ++i) {
623 if (!isl_tab_var_from_row(tab, i)->is_nonneg)
625 assert(!isl_int_is_neg(tab->mat->row[i][1]));
629 /* Return the sign of the maximal value of "var".
630 * If the sign is not negative, then on return from this function,
631 * the sample value will also be non-negative.
633 * If "var" is manifestly unbounded wrt positive values, we are done.
634 * Otherwise, we pivot the variable up to a row if needed
635 * Then we continue pivoting down until either
636 * - no more down pivots can be performed
637 * - the sample value is positive
638 * - the variable is pivoted into a manifestly unbounded column
640 static int sign_of_max(struct isl_tab *tab, struct isl_tab_var *var)
644 if (max_is_manifestly_unbounded(tab, var))
647 while (!isl_int_is_pos(tab->mat->row[var->index][1])) {
648 find_pivot(tab, var, var, 1, &row, &col);
650 return isl_int_sgn(tab->mat->row[var->index][1]);
651 isl_tab_pivot(tab, row, col);
652 if (!var->is_row) /* manifestly unbounded */
658 /* Perform pivots until the row variable "var" has a non-negative
659 * sample value or until no more upward pivots can be performed.
660 * Return the sign of the sample value after the pivots have been
663 static int restore_row(struct isl_tab *tab, struct isl_tab_var *var)
667 while (isl_int_is_neg(tab->mat->row[var->index][1])) {
668 find_pivot(tab, var, var, 1, &row, &col);
671 isl_tab_pivot(tab, row, col);
672 if (!var->is_row) /* manifestly unbounded */
675 return isl_int_sgn(tab->mat->row[var->index][1]);
678 /* Perform pivots until we are sure that the row variable "var"
679 * can attain non-negative values. After return from this
680 * function, "var" is still a row variable, but its sample
681 * value may not be non-negative, even if the function returns 1.
683 static int at_least_zero(struct isl_tab *tab, struct isl_tab_var *var)
687 while (isl_int_is_neg(tab->mat->row[var->index][1])) {
688 find_pivot(tab, var, var, 1, &row, &col);
691 if (row == var->index) /* manifestly unbounded */
693 isl_tab_pivot(tab, row, col);
695 return !isl_int_is_neg(tab->mat->row[var->index][1]);
698 /* Return a negative value if "var" can attain negative values.
699 * Return a non-negative value otherwise.
701 * If "var" is manifestly unbounded wrt negative values, we are done.
702 * Otherwise, if var is in a column, we can pivot it down to a row.
703 * Then we continue pivoting down until either
704 * - the pivot would result in a manifestly unbounded column
705 * => we don't perform the pivot, but simply return -1
706 * - no more down pivots can be performed
707 * - the sample value is negative
708 * If the sample value becomes negative and the variable is supposed
709 * to be nonnegative, then we undo the last pivot.
710 * However, if the last pivot has made the pivoting variable
711 * obviously redundant, then it may have moved to another row.
712 * In that case we look for upward pivots until we reach a non-negative
715 static int sign_of_min(struct isl_tab *tab, struct isl_tab_var *var)
718 struct isl_tab_var *pivot_var;
720 if (min_is_manifestly_unbounded(tab, var))
724 row = pivot_row(tab, NULL, -1, col);
725 pivot_var = var_from_col(tab, col);
726 isl_tab_pivot(tab, row, col);
727 if (var->is_redundant)
729 if (isl_int_is_neg(tab->mat->row[var->index][1])) {
730 if (var->is_nonneg) {
731 if (!pivot_var->is_redundant &&
732 pivot_var->index == row)
733 isl_tab_pivot(tab, row, col);
735 restore_row(tab, var);
740 if (var->is_redundant)
742 while (!isl_int_is_neg(tab->mat->row[var->index][1])) {
743 find_pivot(tab, var, var, -1, &row, &col);
744 if (row == var->index)
747 return isl_int_sgn(tab->mat->row[var->index][1]);
748 pivot_var = var_from_col(tab, col);
749 isl_tab_pivot(tab, row, col);
750 if (var->is_redundant)
753 if (var->is_nonneg) {
754 /* pivot back to non-negative value */
755 if (!pivot_var->is_redundant && pivot_var->index == row)
756 isl_tab_pivot(tab, row, col);
758 restore_row(tab, var);
763 /* Return 1 if "var" can attain values <= -1.
764 * Return 0 otherwise.
766 * The sample value of "var" is assumed to be non-negative when the
767 * the function is called and will be made non-negative again before
768 * the function returns.
770 int isl_tab_min_at_most_neg_one(struct isl_tab *tab, struct isl_tab_var *var)
773 struct isl_tab_var *pivot_var;
775 if (min_is_manifestly_unbounded(tab, var))
779 row = pivot_row(tab, NULL, -1, col);
780 pivot_var = var_from_col(tab, col);
781 isl_tab_pivot(tab, row, col);
782 if (var->is_redundant)
784 if (isl_int_is_neg(tab->mat->row[var->index][1]) &&
785 isl_int_abs_ge(tab->mat->row[var->index][1],
786 tab->mat->row[var->index][0])) {
787 if (var->is_nonneg) {
788 if (!pivot_var->is_redundant &&
789 pivot_var->index == row)
790 isl_tab_pivot(tab, row, col);
792 restore_row(tab, var);
797 if (var->is_redundant)
800 find_pivot(tab, var, var, -1, &row, &col);
801 if (row == var->index)
805 pivot_var = var_from_col(tab, col);
806 isl_tab_pivot(tab, row, col);
807 if (var->is_redundant)
809 } while (!isl_int_is_neg(tab->mat->row[var->index][1]) ||
810 isl_int_abs_lt(tab->mat->row[var->index][1],
811 tab->mat->row[var->index][0]));
812 if (var->is_nonneg) {
813 /* pivot back to non-negative value */
814 if (!pivot_var->is_redundant && pivot_var->index == row)
815 isl_tab_pivot(tab, row, col);
816 restore_row(tab, var);
821 /* Return 1 if "var" can attain values >= 1.
822 * Return 0 otherwise.
824 static int at_least_one(struct isl_tab *tab, struct isl_tab_var *var)
829 if (max_is_manifestly_unbounded(tab, var))
832 r = tab->mat->row[var->index];
833 while (isl_int_lt(r[1], r[0])) {
834 find_pivot(tab, var, var, 1, &row, &col);
836 return isl_int_ge(r[1], r[0]);
837 if (row == var->index) /* manifestly unbounded */
839 isl_tab_pivot(tab, row, col);
844 static void swap_cols(struct isl_tab *tab, int col1, int col2)
847 t = tab->col_var[col1];
848 tab->col_var[col1] = tab->col_var[col2];
849 tab->col_var[col2] = t;
850 var_from_col(tab, col1)->index = col1;
851 var_from_col(tab, col2)->index = col2;
852 tab->mat = isl_mat_swap_cols(tab->mat, 2 + col1, 2 + col2);
855 /* Mark column with index "col" as representing a zero variable.
856 * If we may need to undo the operation the column is kept,
857 * but no longer considered.
858 * Otherwise, the column is simply removed.
860 * The column may be interchanged with some other column. If it
861 * is interchanged with a later column, return 1. Otherwise return 0.
862 * If the columns are checked in order in the calling function,
863 * then a return value of 1 means that the column with the given
864 * column number may now contain a different column that
865 * hasn't been checked yet.
867 int isl_tab_kill_col(struct isl_tab *tab, int col)
869 var_from_col(tab, col)->is_zero = 1;
870 if (tab->need_undo) {
871 isl_tab_push_var(tab, isl_tab_undo_zero, var_from_col(tab, col));
872 if (col != tab->n_dead)
873 swap_cols(tab, col, tab->n_dead);
877 if (col != tab->n_col - 1)
878 swap_cols(tab, col, tab->n_col - 1);
879 var_from_col(tab, tab->n_col - 1)->index = -1;
885 /* Row variable "var" is non-negative and cannot attain any values
886 * larger than zero. This means that the coefficients of the unrestricted
887 * column variables are zero and that the coefficients of the non-negative
888 * column variables are zero or negative.
889 * Each of the non-negative variables with a negative coefficient can
890 * then also be written as the negative sum of non-negative variables
891 * and must therefore also be zero.
893 static void close_row(struct isl_tab *tab, struct isl_tab_var *var)
896 struct isl_mat *mat = tab->mat;
898 isl_assert(tab->mat->ctx, var->is_nonneg, return);
900 for (j = tab->n_dead; j < tab->n_col; ++j) {
901 if (isl_int_is_zero(mat->row[var->index][2 + j]))
903 isl_assert(tab->mat->ctx,
904 isl_int_is_neg(mat->row[var->index][2 + j]), return);
905 if (isl_tab_kill_col(tab, j))
908 isl_tab_mark_redundant(tab, var->index);
911 /* Add a constraint to the tableau and allocate a row for it.
912 * Return the index into the constraint array "con".
914 int isl_tab_allocate_con(struct isl_tab *tab)
918 isl_assert(tab->mat->ctx, tab->n_row < tab->mat->n_row, return -1);
921 tab->con[r].index = tab->n_row;
922 tab->con[r].is_row = 1;
923 tab->con[r].is_nonneg = 0;
924 tab->con[r].is_zero = 0;
925 tab->con[r].is_redundant = 0;
926 tab->con[r].frozen = 0;
927 tab->row_var[tab->n_row] = ~r;
931 isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->con[r]);
936 /* Add a row to the tableau. The row is given as an affine combination
937 * of the original variables and needs to be expressed in terms of the
940 * We add each term in turn.
941 * If r = n/d_r is the current sum and we need to add k x, then
942 * if x is a column variable, we increase the numerator of
943 * this column by k d_r
944 * if x = f/d_x is a row variable, then the new representation of r is
946 * n k f d_x/g n + d_r/g k f m/d_r n + m/d_g k f
947 * --- + --- = ------------------- = -------------------
948 * d_r d_r d_r d_x/g m
950 * with g the gcd of d_r and d_x and m the lcm of d_r and d_x.
952 int isl_tab_add_row(struct isl_tab *tab, isl_int *line)
959 r = isl_tab_allocate_con(tab);
965 row = tab->mat->row[tab->con[r].index];
966 isl_int_set_si(row[0], 1);
967 isl_int_set(row[1], line[0]);
968 isl_seq_clr(row + 2, tab->n_col);
969 for (i = 0; i < tab->n_var; ++i) {
970 if (tab->var[i].is_zero)
972 if (tab->var[i].is_row) {
974 row[0], tab->mat->row[tab->var[i].index][0]);
975 isl_int_swap(a, row[0]);
976 isl_int_divexact(a, row[0], a);
978 row[0], tab->mat->row[tab->var[i].index][0]);
979 isl_int_mul(b, b, line[1 + i]);
980 isl_seq_combine(row + 1, a, row + 1,
981 b, tab->mat->row[tab->var[i].index] + 1,
984 isl_int_addmul(row[2 + tab->var[i].index],
985 line[1 + i], row[0]);
987 isl_seq_normalize(row, 2 + tab->n_col);
994 static int drop_row(struct isl_tab *tab, int row)
996 isl_assert(tab->mat->ctx, ~tab->row_var[row] == tab->n_con - 1, return -1);
997 if (row != tab->n_row - 1)
998 swap_rows(tab, row, tab->n_row - 1);
1004 /* Add inequality "ineq" and check if it conflicts with the
1005 * previously added constraints or if it is obviously redundant.
1007 struct isl_tab *isl_tab_add_ineq(struct isl_tab *tab, isl_int *ineq)
1014 r = isl_tab_add_row(tab, ineq);
1017 tab->con[r].is_nonneg = 1;
1018 isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]);
1019 if (isl_tab_row_is_redundant(tab, tab->con[r].index)) {
1020 isl_tab_mark_redundant(tab, tab->con[r].index);
1024 sgn = restore_row(tab, &tab->con[r]);
1026 return isl_tab_mark_empty(tab);
1027 if (tab->con[r].is_row && isl_tab_row_is_redundant(tab, tab->con[r].index))
1028 isl_tab_mark_redundant(tab, tab->con[r].index);
1035 /* Pivot a non-negative variable down until it reaches the value zero
1036 * and then pivot the variable into a column position.
1038 int to_col(struct isl_tab *tab, struct isl_tab_var *var)
1046 while (isl_int_is_pos(tab->mat->row[var->index][1])) {
1047 find_pivot(tab, var, NULL, -1, &row, &col);
1048 isl_assert(tab->mat->ctx, row != -1, return -1);
1049 isl_tab_pivot(tab, row, col);
1054 for (i = tab->n_dead; i < tab->n_col; ++i)
1055 if (!isl_int_is_zero(tab->mat->row[var->index][2 + i]))
1058 isl_assert(tab->mat->ctx, i < tab->n_col, return -1);
1059 isl_tab_pivot(tab, var->index, i);
1064 /* We assume Gaussian elimination has been performed on the equalities.
1065 * The equalities can therefore never conflict.
1066 * Adding the equalities is currently only really useful for a later call
1067 * to isl_tab_ineq_type.
1069 static struct isl_tab *add_eq(struct isl_tab *tab, isl_int *eq)
1076 r = isl_tab_add_row(tab, eq);
1080 r = tab->con[r].index;
1081 i = isl_seq_first_non_zero(tab->mat->row[r] + 2 + tab->n_dead,
1082 tab->n_col - tab->n_dead);
1083 isl_assert(tab->mat->ctx, i >= 0, goto error);
1085 isl_tab_pivot(tab, r, i);
1086 isl_tab_kill_col(tab, i);
1095 /* Add an equality that is known to be valid for the given tableau.
1097 struct isl_tab *isl_tab_add_valid_eq(struct isl_tab *tab, isl_int *eq)
1099 struct isl_tab_var *var;
1105 r = isl_tab_add_row(tab, eq);
1111 if (isl_int_is_neg(tab->mat->row[r][1]))
1112 isl_seq_neg(tab->mat->row[r] + 1, tab->mat->row[r] + 1,
1115 if (to_col(tab, var) < 0)
1118 isl_tab_kill_col(tab, var->index);
1126 struct isl_tab *isl_tab_from_basic_map(struct isl_basic_map *bmap)
1129 struct isl_tab *tab;
1133 tab = isl_tab_alloc(bmap->ctx,
1134 isl_basic_map_total_dim(bmap) + bmap->n_ineq + 1,
1135 isl_basic_map_total_dim(bmap));
1138 tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
1139 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1140 return isl_tab_mark_empty(tab);
1141 for (i = 0; i < bmap->n_eq; ++i) {
1142 tab = add_eq(tab, bmap->eq[i]);
1146 for (i = 0; i < bmap->n_ineq; ++i) {
1147 tab = isl_tab_add_ineq(tab, bmap->ineq[i]);
1148 if (!tab || tab->empty)
1154 struct isl_tab *isl_tab_from_basic_set(struct isl_basic_set *bset)
1156 return isl_tab_from_basic_map((struct isl_basic_map *)bset);
1159 /* Construct a tableau corresponding to the recession cone of "bmap".
1161 struct isl_tab *isl_tab_from_recession_cone(struct isl_basic_map *bmap)
1165 struct isl_tab *tab;
1169 tab = isl_tab_alloc(bmap->ctx, bmap->n_eq + bmap->n_ineq,
1170 isl_basic_map_total_dim(bmap));
1173 tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
1176 for (i = 0; i < bmap->n_eq; ++i) {
1177 isl_int_swap(bmap->eq[i][0], cst);
1178 tab = add_eq(tab, bmap->eq[i]);
1179 isl_int_swap(bmap->eq[i][0], cst);
1183 for (i = 0; i < bmap->n_ineq; ++i) {
1185 isl_int_swap(bmap->ineq[i][0], cst);
1186 r = isl_tab_add_row(tab, bmap->ineq[i]);
1187 isl_int_swap(bmap->ineq[i][0], cst);
1190 tab->con[r].is_nonneg = 1;
1191 isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]);
1202 /* Assuming "tab" is the tableau of a cone, check if the cone is
1203 * bounded, i.e., if it is empty or only contains the origin.
1205 int isl_tab_cone_is_bounded(struct isl_tab *tab)
1213 if (tab->n_dead == tab->n_col)
1217 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1218 struct isl_tab_var *var;
1219 var = isl_tab_var_from_row(tab, i);
1220 if (!var->is_nonneg)
1222 if (sign_of_max(tab, var) != 0)
1224 close_row(tab, var);
1227 if (tab->n_dead == tab->n_col)
1229 if (i == tab->n_row)
1234 int isl_tab_sample_is_integer(struct isl_tab *tab)
1241 for (i = 0; i < tab->n_var; ++i) {
1243 if (!tab->var[i].is_row)
1245 row = tab->var[i].index;
1246 if (!isl_int_is_divisible_by(tab->mat->row[row][1],
1247 tab->mat->row[row][0]))
1253 static struct isl_vec *extract_integer_sample(struct isl_tab *tab)
1256 struct isl_vec *vec;
1258 vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
1262 isl_int_set_si(vec->block.data[0], 1);
1263 for (i = 0; i < tab->n_var; ++i) {
1264 if (!tab->var[i].is_row)
1265 isl_int_set_si(vec->block.data[1 + i], 0);
1267 int row = tab->var[i].index;
1268 isl_int_divexact(vec->block.data[1 + i],
1269 tab->mat->row[row][1], tab->mat->row[row][0]);
1276 struct isl_vec *isl_tab_get_sample_value(struct isl_tab *tab)
1279 struct isl_vec *vec;
1285 vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
1291 isl_int_set_si(vec->block.data[0], 1);
1292 for (i = 0; i < tab->n_var; ++i) {
1294 if (!tab->var[i].is_row) {
1295 isl_int_set_si(vec->block.data[1 + i], 0);
1298 row = tab->var[i].index;
1299 isl_int_gcd(m, vec->block.data[0], tab->mat->row[row][0]);
1300 isl_int_divexact(m, tab->mat->row[row][0], m);
1301 isl_seq_scale(vec->block.data, vec->block.data, m, 1 + i);
1302 isl_int_divexact(m, vec->block.data[0], tab->mat->row[row][0]);
1303 isl_int_mul(vec->block.data[1 + i], m, tab->mat->row[row][1]);
1305 isl_seq_normalize(vec->block.data, vec->size);
1311 /* Update "bmap" based on the results of the tableau "tab".
1312 * In particular, implicit equalities are made explicit, redundant constraints
1313 * are removed and if the sample value happens to be integer, it is stored
1314 * in "bmap" (unless "bmap" already had an integer sample).
1316 * The tableau is assumed to have been created from "bmap" using
1317 * isl_tab_from_basic_map.
1319 struct isl_basic_map *isl_basic_map_update_from_tab(struct isl_basic_map *bmap,
1320 struct isl_tab *tab)
1332 bmap = isl_basic_map_set_to_empty(bmap);
1334 for (i = bmap->n_ineq - 1; i >= 0; --i) {
1335 if (isl_tab_is_equality(tab, n_eq + i))
1336 isl_basic_map_inequality_to_equality(bmap, i);
1337 else if (isl_tab_is_redundant(tab, n_eq + i))
1338 isl_basic_map_drop_inequality(bmap, i);
1340 if (!tab->rational &&
1341 !bmap->sample && isl_tab_sample_is_integer(tab))
1342 bmap->sample = extract_integer_sample(tab);
1346 struct isl_basic_set *isl_basic_set_update_from_tab(struct isl_basic_set *bset,
1347 struct isl_tab *tab)
1349 return (struct isl_basic_set *)isl_basic_map_update_from_tab(
1350 (struct isl_basic_map *)bset, tab);
1353 /* Given a non-negative variable "var", add a new non-negative variable
1354 * that is the opposite of "var", ensuring that var can only attain the
1356 * If var = n/d is a row variable, then the new variable = -n/d.
1357 * If var is a column variables, then the new variable = -var.
1358 * If the new variable cannot attain non-negative values, then
1359 * the resulting tableau is empty.
1360 * Otherwise, we know the value will be zero and we close the row.
1362 static struct isl_tab *cut_to_hyperplane(struct isl_tab *tab,
1363 struct isl_tab_var *var)
1369 if (isl_tab_extend_cons(tab, 1) < 0)
1373 tab->con[r].index = tab->n_row;
1374 tab->con[r].is_row = 1;
1375 tab->con[r].is_nonneg = 0;
1376 tab->con[r].is_zero = 0;
1377 tab->con[r].is_redundant = 0;
1378 tab->con[r].frozen = 0;
1379 tab->row_var[tab->n_row] = ~r;
1380 row = tab->mat->row[tab->n_row];
1383 isl_int_set(row[0], tab->mat->row[var->index][0]);
1384 isl_seq_neg(row + 1,
1385 tab->mat->row[var->index] + 1, 1 + tab->n_col);
1387 isl_int_set_si(row[0], 1);
1388 isl_seq_clr(row + 1, 1 + tab->n_col);
1389 isl_int_set_si(row[2 + var->index], -1);
1394 isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->con[r]);
1396 sgn = sign_of_max(tab, &tab->con[r]);
1398 return isl_tab_mark_empty(tab);
1399 tab->con[r].is_nonneg = 1;
1400 isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]);
1402 close_row(tab, &tab->con[r]);
1410 /* Given a tableau "tab" and an inequality constraint "con" of the tableau,
1411 * relax the inequality by one. That is, the inequality r >= 0 is replaced
1412 * by r' = r + 1 >= 0.
1413 * If r is a row variable, we simply increase the constant term by one
1414 * (taking into account the denominator).
1415 * If r is a column variable, then we need to modify each row that
1416 * refers to r = r' - 1 by substituting this equality, effectively
1417 * subtracting the coefficient of the column from the constant.
1419 struct isl_tab *isl_tab_relax(struct isl_tab *tab, int con)
1421 struct isl_tab_var *var;
1425 var = &tab->con[con];
1427 if (!var->is_row && !max_is_manifestly_unbounded(tab, var))
1428 to_row(tab, var, 1);
1431 isl_int_add(tab->mat->row[var->index][1],
1432 tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
1436 for (i = 0; i < tab->n_row; ++i) {
1437 if (isl_int_is_zero(tab->mat->row[i][2 + var->index]))
1439 isl_int_sub(tab->mat->row[i][1], tab->mat->row[i][1],
1440 tab->mat->row[i][2 + var->index]);
1445 isl_tab_push_var(tab, isl_tab_undo_relax, var);
1450 struct isl_tab *isl_tab_select_facet(struct isl_tab *tab, int con)
1455 return cut_to_hyperplane(tab, &tab->con[con]);
1458 static int may_be_equality(struct isl_tab *tab, int row)
1460 return (tab->rational ? isl_int_is_zero(tab->mat->row[row][1])
1461 : isl_int_lt(tab->mat->row[row][1],
1462 tab->mat->row[row][0])) &&
1463 isl_seq_first_non_zero(tab->mat->row[row] + 2 + tab->n_dead,
1464 tab->n_col - tab->n_dead) != -1;
1467 /* Check for (near) equalities among the constraints.
1468 * A constraint is an equality if it is non-negative and if
1469 * its maximal value is either
1470 * - zero (in case of rational tableaus), or
1471 * - strictly less than 1 (in case of integer tableaus)
1473 * We first mark all non-redundant and non-dead variables that
1474 * are not frozen and not obviously not an equality.
1475 * Then we iterate over all marked variables if they can attain
1476 * any values larger than zero or at least one.
1477 * If the maximal value is zero, we mark any column variables
1478 * that appear in the row as being zero and mark the row as being redundant.
1479 * Otherwise, if the maximal value is strictly less than one (and the
1480 * tableau is integer), then we restrict the value to being zero
1481 * by adding an opposite non-negative variable.
1483 struct isl_tab *isl_tab_detect_equalities(struct isl_tab *tab)
1492 if (tab->n_dead == tab->n_col)
1496 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1497 struct isl_tab_var *var = isl_tab_var_from_row(tab, i);
1498 var->marked = !var->frozen && var->is_nonneg &&
1499 may_be_equality(tab, i);
1503 for (i = tab->n_dead; i < tab->n_col; ++i) {
1504 struct isl_tab_var *var = var_from_col(tab, i);
1505 var->marked = !var->frozen && var->is_nonneg;
1510 struct isl_tab_var *var;
1511 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1512 var = isl_tab_var_from_row(tab, i);
1516 if (i == tab->n_row) {
1517 for (i = tab->n_dead; i < tab->n_col; ++i) {
1518 var = var_from_col(tab, i);
1522 if (i == tab->n_col)
1527 if (sign_of_max(tab, var) == 0)
1528 close_row(tab, var);
1529 else if (!tab->rational && !at_least_one(tab, var)) {
1530 tab = cut_to_hyperplane(tab, var);
1531 return isl_tab_detect_equalities(tab);
1533 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1534 var = isl_tab_var_from_row(tab, i);
1537 if (may_be_equality(tab, i))
1547 /* Check for (near) redundant constraints.
1548 * A constraint is redundant if it is non-negative and if
1549 * its minimal value (temporarily ignoring the non-negativity) is either
1550 * - zero (in case of rational tableaus), or
1551 * - strictly larger than -1 (in case of integer tableaus)
1553 * We first mark all non-redundant and non-dead variables that
1554 * are not frozen and not obviously negatively unbounded.
1555 * Then we iterate over all marked variables if they can attain
1556 * any values smaller than zero or at most negative one.
1557 * If not, we mark the row as being redundant (assuming it hasn't
1558 * been detected as being obviously redundant in the mean time).
1560 struct isl_tab *isl_tab_detect_redundant(struct isl_tab *tab)
1569 if (tab->n_redundant == tab->n_row)
1573 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1574 struct isl_tab_var *var = isl_tab_var_from_row(tab, i);
1575 var->marked = !var->frozen && var->is_nonneg;
1579 for (i = tab->n_dead; i < tab->n_col; ++i) {
1580 struct isl_tab_var *var = var_from_col(tab, i);
1581 var->marked = !var->frozen && var->is_nonneg &&
1582 !min_is_manifestly_unbounded(tab, var);
1587 struct isl_tab_var *var;
1588 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1589 var = isl_tab_var_from_row(tab, i);
1593 if (i == tab->n_row) {
1594 for (i = tab->n_dead; i < tab->n_col; ++i) {
1595 var = var_from_col(tab, i);
1599 if (i == tab->n_col)
1604 if ((tab->rational ? (sign_of_min(tab, var) >= 0)
1605 : !isl_tab_min_at_most_neg_one(tab, var)) &&
1607 isl_tab_mark_redundant(tab, var->index);
1608 for (i = tab->n_dead; i < tab->n_col; ++i) {
1609 var = var_from_col(tab, i);
1612 if (!min_is_manifestly_unbounded(tab, var))
1622 int isl_tab_is_equality(struct isl_tab *tab, int con)
1628 if (tab->con[con].is_zero)
1630 if (tab->con[con].is_redundant)
1632 if (!tab->con[con].is_row)
1633 return tab->con[con].index < tab->n_dead;
1635 row = tab->con[con].index;
1637 return isl_int_is_zero(tab->mat->row[row][1]) &&
1638 isl_seq_first_non_zero(tab->mat->row[row] + 2 + tab->n_dead,
1639 tab->n_col - tab->n_dead) == -1;
1642 /* Return the minimial value of the affine expression "f" with denominator
1643 * "denom" in *opt, *opt_denom, assuming the tableau is not empty and
1644 * the expression cannot attain arbitrarily small values.
1645 * If opt_denom is NULL, then *opt is rounded up to the nearest integer.
1646 * The return value reflects the nature of the result (empty, unbounded,
1647 * minmimal value returned in *opt).
1649 enum isl_lp_result isl_tab_min(struct isl_tab *tab,
1650 isl_int *f, isl_int denom, isl_int *opt, isl_int *opt_denom,
1654 enum isl_lp_result res = isl_lp_ok;
1655 struct isl_tab_var *var;
1656 struct isl_tab_undo *snap;
1659 return isl_lp_empty;
1661 snap = isl_tab_snap(tab);
1662 r = isl_tab_add_row(tab, f);
1664 return isl_lp_error;
1666 isl_int_mul(tab->mat->row[var->index][0],
1667 tab->mat->row[var->index][0], denom);
1670 find_pivot(tab, var, var, -1, &row, &col);
1671 if (row == var->index) {
1672 res = isl_lp_unbounded;
1677 isl_tab_pivot(tab, row, col);
1679 if (isl_tab_rollback(tab, snap) < 0)
1680 return isl_lp_error;
1681 if (ISL_FL_ISSET(flags, ISL_TAB_SAVE_DUAL)) {
1684 isl_vec_free(tab->dual);
1685 tab->dual = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_con);
1687 return isl_lp_error;
1688 isl_int_set(tab->dual->el[0], tab->mat->row[var->index][0]);
1689 for (i = 0; i < tab->n_con; ++i) {
1690 if (tab->con[i].is_row)
1691 isl_int_set_si(tab->dual->el[1 + i], 0);
1693 int pos = 2 + tab->con[i].index;
1694 isl_int_set(tab->dual->el[1 + i],
1695 tab->mat->row[var->index][pos]);
1699 if (res == isl_lp_ok) {
1701 isl_int_set(*opt, tab->mat->row[var->index][1]);
1702 isl_int_set(*opt_denom, tab->mat->row[var->index][0]);
1704 isl_int_cdiv_q(*opt, tab->mat->row[var->index][1],
1705 tab->mat->row[var->index][0]);
1710 int isl_tab_is_redundant(struct isl_tab *tab, int con)
1717 if (tab->con[con].is_zero)
1719 if (tab->con[con].is_redundant)
1721 return tab->con[con].is_row && tab->con[con].index < tab->n_redundant;
1724 /* Take a snapshot of the tableau that can be restored by s call to
1727 struct isl_tab_undo *isl_tab_snap(struct isl_tab *tab)
1735 /* Undo the operation performed by isl_tab_relax.
1737 static void unrelax(struct isl_tab *tab, struct isl_tab_var *var)
1739 if (!var->is_row && !max_is_manifestly_unbounded(tab, var))
1740 to_row(tab, var, 1);
1743 isl_int_sub(tab->mat->row[var->index][1],
1744 tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
1748 for (i = 0; i < tab->n_row; ++i) {
1749 if (isl_int_is_zero(tab->mat->row[i][2 + var->index]))
1751 isl_int_add(tab->mat->row[i][1], tab->mat->row[i][1],
1752 tab->mat->row[i][2 + var->index]);
1758 static void perform_undo_var(struct isl_tab *tab, struct isl_tab_undo *undo)
1760 struct isl_tab_var *var = var_from_index(tab, undo->u.var_index);
1761 switch(undo->type) {
1762 case isl_tab_undo_nonneg:
1765 case isl_tab_undo_redundant:
1766 var->is_redundant = 0;
1769 case isl_tab_undo_zero:
1773 case isl_tab_undo_allocate:
1775 if (!max_is_manifestly_unbounded(tab, var))
1776 to_row(tab, var, 1);
1777 else if (!min_is_manifestly_unbounded(tab, var))
1778 to_row(tab, var, -1);
1780 to_row(tab, var, 0);
1782 drop_row(tab, var->index);
1784 case isl_tab_undo_relax:
1790 /* Restore the tableau to the state where the basic variables
1791 * are those in "col_var".
1792 * We first construct a list of variables that are currently in
1793 * the basis, but shouldn't. Then we iterate over all variables
1794 * that should be in the basis and for each one that is currently
1795 * not in the basis, we exchange it with one of the elements of the
1796 * list constructed before.
1797 * We can always find an appropriate variable to pivot with because
1798 * the current basis is mapped to the old basis by a non-singular
1799 * matrix and so we can never end up with a zero row.
1801 static int restore_basis(struct isl_tab *tab, int *col_var)
1805 int *extra = NULL; /* current columns that contain bad stuff */
1808 extra = isl_alloc_array(tab->mat->ctx, int, tab->n_col);
1811 for (i = 0; i < tab->n_col; ++i) {
1812 for (j = 0; j < tab->n_col; ++j)
1813 if (tab->col_var[i] == col_var[j])
1817 extra[n_extra++] = i;
1819 for (i = 0; i < tab->n_col && n_extra > 0; ++i) {
1820 struct isl_tab_var *var;
1823 for (j = 0; j < tab->n_col; ++j)
1824 if (col_var[i] == tab->col_var[j])
1828 var = var_from_index(tab, col_var[i]);
1830 for (j = 0; j < n_extra; ++j)
1831 if (!isl_int_is_zero(tab->mat->row[row][off+extra[j]]))
1833 isl_assert(tab->mat->ctx, j < n_extra, goto error);
1834 isl_tab_pivot(tab, row, extra[j]);
1835 extra[j] = extra[--n_extra];
1847 static int perform_undo(struct isl_tab *tab, struct isl_tab_undo *undo)
1849 switch (undo->type) {
1850 case isl_tab_undo_empty:
1853 case isl_tab_undo_nonneg:
1854 case isl_tab_undo_redundant:
1855 case isl_tab_undo_zero:
1856 case isl_tab_undo_allocate:
1857 case isl_tab_undo_relax:
1858 perform_undo_var(tab, undo);
1860 case isl_tab_undo_saved_basis:
1861 if (restore_basis(tab, undo->u.col_var) < 0)
1865 isl_assert(tab->mat->ctx, 0, return -1);
1870 /* Return the tableau to the state it was in when the snapshot "snap"
1873 int isl_tab_rollback(struct isl_tab *tab, struct isl_tab_undo *snap)
1875 struct isl_tab_undo *undo, *next;
1881 for (undo = tab->top; undo && undo != &tab->bottom; undo = next) {
1885 if (perform_undo(tab, undo) < 0) {
1899 /* The given row "row" represents an inequality violated by all
1900 * points in the tableau. Check for some special cases of such
1901 * separating constraints.
1902 * In particular, if the row has been reduced to the constant -1,
1903 * then we know the inequality is adjacent (but opposite) to
1904 * an equality in the tableau.
1905 * If the row has been reduced to r = -1 -r', with r' an inequality
1906 * of the tableau, then the inequality is adjacent (but opposite)
1907 * to the inequality r'.
1909 static enum isl_ineq_type separation_type(struct isl_tab *tab, unsigned row)
1914 return isl_ineq_separate;
1916 if (!isl_int_is_one(tab->mat->row[row][0]))
1917 return isl_ineq_separate;
1918 if (!isl_int_is_negone(tab->mat->row[row][1]))
1919 return isl_ineq_separate;
1921 pos = isl_seq_first_non_zero(tab->mat->row[row] + 2 + tab->n_dead,
1922 tab->n_col - tab->n_dead);
1924 return isl_ineq_adj_eq;
1926 if (!isl_int_is_negone(tab->mat->row[row][2 + tab->n_dead + pos]))
1927 return isl_ineq_separate;
1929 pos = isl_seq_first_non_zero(
1930 tab->mat->row[row] + 2 + tab->n_dead + pos + 1,
1931 tab->n_col - tab->n_dead - pos - 1);
1933 return pos == -1 ? isl_ineq_adj_ineq : isl_ineq_separate;
1936 /* Check the effect of inequality "ineq" on the tableau "tab".
1938 * isl_ineq_redundant: satisfied by all points in the tableau
1939 * isl_ineq_separate: satisfied by no point in the tableau
1940 * isl_ineq_cut: satisfied by some by not all points
1941 * isl_ineq_adj_eq: adjacent to an equality
1942 * isl_ineq_adj_ineq: adjacent to an inequality.
1944 enum isl_ineq_type isl_tab_ineq_type(struct isl_tab *tab, isl_int *ineq)
1946 enum isl_ineq_type type = isl_ineq_error;
1947 struct isl_tab_undo *snap = NULL;
1952 return isl_ineq_error;
1954 if (isl_tab_extend_cons(tab, 1) < 0)
1955 return isl_ineq_error;
1957 snap = isl_tab_snap(tab);
1959 con = isl_tab_add_row(tab, ineq);
1963 row = tab->con[con].index;
1964 if (isl_tab_row_is_redundant(tab, row))
1965 type = isl_ineq_redundant;
1966 else if (isl_int_is_neg(tab->mat->row[row][1]) &&
1968 isl_int_abs_ge(tab->mat->row[row][1],
1969 tab->mat->row[row][0]))) {
1970 if (at_least_zero(tab, &tab->con[con]))
1971 type = isl_ineq_cut;
1973 type = separation_type(tab, row);
1974 } else if (tab->rational ? (sign_of_min(tab, &tab->con[con]) < 0)
1975 : isl_tab_min_at_most_neg_one(tab, &tab->con[con]))
1976 type = isl_ineq_cut;
1978 type = isl_ineq_redundant;
1980 if (isl_tab_rollback(tab, snap))
1981 return isl_ineq_error;
1984 isl_tab_rollback(tab, snap);
1985 return isl_ineq_error;
1988 void isl_tab_dump(struct isl_tab *tab, FILE *out, int indent)
1994 fprintf(out, "%*snull tab\n", indent, "");
1997 fprintf(out, "%*sn_redundant: %d, n_dead: %d", indent, "",
1998 tab->n_redundant, tab->n_dead);
2000 fprintf(out, ", rational");
2002 fprintf(out, ", empty");
2004 fprintf(out, "%*s[", indent, "");
2005 for (i = 0; i < tab->n_var; ++i) {
2007 fprintf(out, (i == tab->n_param ||
2008 i == tab->n_var - tab->n_div) ? "; "
2010 fprintf(out, "%c%d%s", tab->var[i].is_row ? 'r' : 'c',
2012 tab->var[i].is_zero ? " [=0]" :
2013 tab->var[i].is_redundant ? " [R]" : "");
2015 fprintf(out, "]\n");
2016 fprintf(out, "%*s[", indent, "");
2017 for (i = 0; i < tab->n_con; ++i) {
2020 fprintf(out, "%c%d%s", tab->con[i].is_row ? 'r' : 'c',
2022 tab->con[i].is_zero ? " [=0]" :
2023 tab->con[i].is_redundant ? " [R]" : "");
2025 fprintf(out, "]\n");
2026 fprintf(out, "%*s[", indent, "");
2027 for (i = 0; i < tab->n_row; ++i) {
2030 fprintf(out, "r%d: %d%s", i, tab->row_var[i],
2031 isl_tab_var_from_row(tab, i)->is_nonneg ? " [>=0]" : "");
2033 fprintf(out, "]\n");
2034 fprintf(out, "%*s[", indent, "");
2035 for (i = 0; i < tab->n_col; ++i) {
2038 fprintf(out, "c%d: %d%s", i, tab->col_var[i],
2039 var_from_col(tab, i)->is_nonneg ? " [>=0]" : "");
2041 fprintf(out, "]\n");
2042 r = tab->mat->n_row;
2043 tab->mat->n_row = tab->n_row;
2044 c = tab->mat->n_col;
2045 tab->mat->n_col = 2 + tab->n_col;
2046 isl_mat_dump(tab->mat, out, indent);
2047 tab->mat->n_row = r;
2048 tab->mat->n_col = c;