1 #include "isl_map_private.h"
5 * The implementation of tableaus in this file was inspired by Section 8
6 * of David Detlefs, Greg Nelson and James B. Saxe, "Simplify: a theorem
7 * prover for program checking".
10 struct isl_tab *isl_tab_alloc(struct isl_ctx *ctx,
11 unsigned n_row, unsigned n_var)
16 tab = isl_calloc_type(ctx, struct isl_tab);
19 tab->mat = isl_mat_alloc(ctx, n_row, 2 + n_var);
22 tab->var = isl_alloc_array(ctx, struct isl_tab_var, n_var);
25 tab->con = isl_alloc_array(ctx, struct isl_tab_var, n_row);
28 tab->col_var = isl_alloc_array(ctx, int, n_var);
31 tab->row_var = isl_alloc_array(ctx, int, n_row);
34 for (i = 0; i < n_var; ++i) {
35 tab->var[i].index = i;
36 tab->var[i].is_row = 0;
37 tab->var[i].is_nonneg = 0;
38 tab->var[i].is_zero = 0;
39 tab->var[i].is_redundant = 0;
40 tab->var[i].frozen = 0;
54 tab->bottom.type = isl_tab_undo_bottom;
55 tab->bottom.next = NULL;
56 tab->top = &tab->bottom;
59 isl_tab_free(ctx, tab);
63 static int extend_cons(struct isl_ctx *ctx, struct isl_tab *tab, unsigned n_new)
65 if (tab->max_con < tab->n_con + n_new) {
66 struct isl_tab_var *con;
68 con = isl_realloc_array(ctx, tab->con,
69 struct isl_tab_var, tab->max_con + n_new);
73 tab->max_con += n_new;
75 if (tab->mat->n_row < tab->n_row + n_new) {
78 tab->mat = isl_mat_extend(ctx, tab->mat,
79 tab->n_row + n_new, tab->n_col);
82 row_var = isl_realloc_array(ctx, tab->row_var,
83 int, tab->mat->n_row);
86 tab->row_var = row_var;
91 struct isl_tab *isl_tab_extend(struct isl_ctx *ctx, struct isl_tab *tab,
94 if (extend_cons(ctx, tab, n_new) >= 0)
97 isl_tab_free(ctx, tab);
101 static void free_undo(struct isl_ctx *ctx, struct isl_tab *tab)
103 struct isl_tab_undo *undo, *next;
105 for (undo = tab->top; undo && undo != &tab->bottom; undo = next) {
112 void isl_tab_free(struct isl_ctx *ctx, struct isl_tab *tab)
117 isl_mat_free(ctx, tab->mat);
118 isl_vec_free(tab->dual);
126 static struct isl_tab_var *var_from_index(struct isl_ctx *ctx,
127 struct isl_tab *tab, int i)
132 return &tab->con[~i];
135 static struct isl_tab_var *var_from_row(struct isl_ctx *ctx,
136 struct isl_tab *tab, int i)
138 return var_from_index(ctx, tab, tab->row_var[i]);
141 static struct isl_tab_var *var_from_col(struct isl_ctx *ctx,
142 struct isl_tab *tab, int i)
144 return var_from_index(ctx, tab, tab->col_var[i]);
147 /* Check if there are any upper bounds on column variable "var",
148 * i.e., non-negative rows where var appears with a negative coefficient.
149 * Return 1 if there are no such bounds.
151 static int max_is_manifestly_unbounded(struct isl_ctx *ctx,
152 struct isl_tab *tab, struct isl_tab_var *var)
158 for (i = tab->n_redundant; i < tab->n_row; ++i) {
159 if (!isl_int_is_neg(tab->mat->row[i][2 + var->index]))
161 if (var_from_row(ctx, tab, i)->is_nonneg)
167 /* Check if there are any lower bounds on column variable "var",
168 * i.e., non-negative rows where var appears with a positive coefficient.
169 * Return 1 if there are no such bounds.
171 static int min_is_manifestly_unbounded(struct isl_ctx *ctx,
172 struct isl_tab *tab, struct isl_tab_var *var)
178 for (i = tab->n_redundant; i < tab->n_row; ++i) {
179 if (!isl_int_is_pos(tab->mat->row[i][2 + var->index]))
181 if (var_from_row(ctx, tab, i)->is_nonneg)
187 /* Given the index of a column "c", return the index of a row
188 * that can be used to pivot the column in, with either an increase
189 * (sgn > 0) or a decrease (sgn < 0) of the corresponding variable.
190 * If "var" is not NULL, then the row returned will be different from
191 * the one associated with "var".
193 * Each row in the tableau is of the form
195 * x_r = a_r0 + \sum_i a_ri x_i
197 * Only rows with x_r >= 0 and with the sign of a_ri opposite to "sgn"
198 * impose any limit on the increase or decrease in the value of x_c
199 * and this bound is equal to a_r0 / |a_rc|. We are therefore looking
200 * for the row with the smallest (most stringent) such bound.
201 * Note that the common denominator of each row drops out of the fraction.
202 * To check if row j has a smaller bound than row r, i.e.,
203 * a_j0 / |a_jc| < a_r0 / |a_rc| or a_j0 |a_rc| < a_r0 |a_jc|,
204 * we check if -sign(a_jc) (a_j0 a_rc - a_r0 a_jc) < 0,
205 * where -sign(a_jc) is equal to "sgn".
207 static int pivot_row(struct isl_ctx *ctx, struct isl_tab *tab,
208 struct isl_tab_var *var, int sgn, int c)
215 for (j = tab->n_redundant; j < tab->n_row; ++j) {
216 if (var && j == var->index)
218 if (!var_from_row(ctx, tab, j)->is_nonneg)
220 if (sgn * isl_int_sgn(tab->mat->row[j][2 + c]) >= 0)
226 isl_int_mul(t, tab->mat->row[r][1], tab->mat->row[j][2 + c]);
227 isl_int_submul(t, tab->mat->row[j][1], tab->mat->row[r][2 + c]);
228 tsgn = sgn * isl_int_sgn(t);
229 if (tsgn < 0 || (tsgn == 0 &&
230 tab->row_var[j] < tab->row_var[r]))
237 /* Find a pivot (row and col) that will increase (sgn > 0) or decrease
238 * (sgn < 0) the value of row variable var.
239 * If not NULL, then skip_var is a row variable that should be ignored
240 * while looking for a pivot row. It is usually equal to var.
242 * As the given row in the tableau is of the form
244 * x_r = a_r0 + \sum_i a_ri x_i
246 * we need to find a column such that the sign of a_ri is equal to "sgn"
247 * (such that an increase in x_i will have the desired effect) or a
248 * column with a variable that may attain negative values.
249 * If a_ri is positive, then we need to move x_i in the same direction
250 * to obtain the desired effect. Otherwise, x_i has to move in the
251 * opposite direction.
253 static void find_pivot(struct isl_ctx *ctx, struct isl_tab *tab,
254 struct isl_tab_var *var, struct isl_tab_var *skip_var,
255 int sgn, int *row, int *col)
262 isl_assert(ctx, var->is_row, return);
263 tr = tab->mat->row[var->index];
266 for (j = tab->n_dead; j < tab->n_col; ++j) {
267 if (isl_int_is_zero(tr[2 + j]))
269 if (isl_int_sgn(tr[2 + j]) != sgn &&
270 var_from_col(ctx, tab, j)->is_nonneg)
272 if (c < 0 || tab->col_var[j] < tab->col_var[c])
278 sgn *= isl_int_sgn(tr[2 + c]);
279 r = pivot_row(ctx, tab, skip_var, sgn, c);
280 *row = r < 0 ? var->index : r;
284 /* Return 1 if row "row" represents an obviously redundant inequality.
286 * - it represents an inequality or a variable
287 * - that is the sum of a non-negative sample value and a positive
288 * combination of zero or more non-negative variables.
290 static int is_redundant(struct isl_ctx *ctx, struct isl_tab *tab, int row)
294 if (tab->row_var[row] < 0 && !var_from_row(ctx, tab, row)->is_nonneg)
297 if (isl_int_is_neg(tab->mat->row[row][1]))
300 for (i = tab->n_dead; i < tab->n_col; ++i) {
301 if (isl_int_is_zero(tab->mat->row[row][2 + i]))
303 if (isl_int_is_neg(tab->mat->row[row][2 + i]))
305 if (!var_from_col(ctx, tab, i)->is_nonneg)
311 static void swap_rows(struct isl_ctx *ctx,
312 struct isl_tab *tab, int row1, int row2)
315 t = tab->row_var[row1];
316 tab->row_var[row1] = tab->row_var[row2];
317 tab->row_var[row2] = t;
318 var_from_row(ctx, tab, row1)->index = row1;
319 var_from_row(ctx, tab, row2)->index = row2;
320 tab->mat = isl_mat_swap_rows(ctx, tab->mat, row1, row2);
323 static void push(struct isl_ctx *ctx, struct isl_tab *tab,
324 enum isl_tab_undo_type type, struct isl_tab_var *var)
326 struct isl_tab_undo *undo;
331 undo = isl_alloc_type(ctx, struct isl_tab_undo);
339 undo->next = tab->top;
343 /* Mark row with index "row" as being redundant.
344 * If we may need to undo the operation or if the row represents
345 * a variable of the original problem, the row is kept,
346 * but no longer considered when looking for a pivot row.
347 * Otherwise, the row is simply removed.
349 * The row may be interchanged with some other row. If it
350 * is interchanged with a later row, return 1. Otherwise return 0.
351 * If the rows are checked in order in the calling function,
352 * then a return value of 1 means that the row with the given
353 * row number may now contain a different row that hasn't been checked yet.
355 static int mark_redundant(struct isl_ctx *ctx,
356 struct isl_tab *tab, int row)
358 struct isl_tab_var *var = var_from_row(ctx, tab, row);
359 var->is_redundant = 1;
360 isl_assert(ctx, row >= tab->n_redundant, return);
361 if (tab->need_undo || tab->row_var[row] >= 0) {
362 if (tab->row_var[row] >= 0) {
364 push(ctx, tab, isl_tab_undo_nonneg, var);
366 if (row != tab->n_redundant)
367 swap_rows(ctx, tab, row, tab->n_redundant);
368 push(ctx, tab, isl_tab_undo_redundant, var);
372 if (row != tab->n_row - 1)
373 swap_rows(ctx, tab, row, tab->n_row - 1);
374 var_from_row(ctx, tab, tab->n_row - 1)->index = -1;
380 static void mark_empty(struct isl_ctx *ctx, struct isl_tab *tab)
382 if (!tab->empty && tab->need_undo)
383 push(ctx, tab, isl_tab_undo_empty, NULL);
387 /* Given a row number "row" and a column number "col", pivot the tableau
388 * such that the associated variable are interchanged.
389 * The given row in the tableau expresses
391 * x_r = a_r0 + \sum_i a_ri x_i
395 * x_c = 1/a_rc x_r - a_r0/a_rc + sum_{i \ne r} -a_ri/a_rc
397 * Substituting this equality into the other rows
399 * x_j = a_j0 + \sum_i a_ji x_i
401 * with a_jc \ne 0, we obtain
403 * 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
410 * where i is any other column and j is any other row,
411 * is therefore transformed into
413 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
414 * 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)
416 * The transformation is performed along the following steps
421 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
424 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
425 * n_jc/(|n_rc| d_j) n_ji/(|n_rc| d_j)
427 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
428 * n_jc/(|n_rc| d_j) (n_ji |n_rc|)/(|n_rc| d_j)
430 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
431 * n_jc/(|n_rc| d_j) (n_ji |n_rc| - s(n_rc)n_jc n_ri)/(|n_rc| d_j)
433 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
434 * 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)
437 static void pivot(struct isl_ctx *ctx,
438 struct isl_tab *tab, int row, int col)
443 struct isl_mat *mat = tab->mat;
444 struct isl_tab_var *var;
446 isl_int_swap(mat->row[row][0], mat->row[row][2 + col]);
447 sgn = isl_int_sgn(mat->row[row][0]);
449 isl_int_neg(mat->row[row][0], mat->row[row][0]);
450 isl_int_neg(mat->row[row][2 + col], mat->row[row][2 + col]);
452 for (j = 0; j < 1 + tab->n_col; ++j) {
455 isl_int_neg(mat->row[row][1 + j], mat->row[row][1 + j]);
457 if (!isl_int_is_one(mat->row[row][0]))
458 isl_seq_normalize(mat->row[row], 2 + tab->n_col);
459 for (i = 0; i < tab->n_row; ++i) {
462 if (isl_int_is_zero(mat->row[i][2 + col]))
464 isl_int_mul(mat->row[i][0], mat->row[i][0], mat->row[row][0]);
465 for (j = 0; j < 1 + tab->n_col; ++j) {
468 isl_int_mul(mat->row[i][1 + j],
469 mat->row[i][1 + j], mat->row[row][0]);
470 isl_int_addmul(mat->row[i][1 + j],
471 mat->row[i][2 + col], mat->row[row][1 + j]);
473 isl_int_mul(mat->row[i][2 + col],
474 mat->row[i][2 + col], mat->row[row][2 + col]);
475 if (!isl_int_is_one(mat->row[row][0]))
476 isl_seq_normalize(mat->row[i], 2 + tab->n_col);
478 t = tab->row_var[row];
479 tab->row_var[row] = tab->col_var[col];
480 tab->col_var[col] = t;
481 var = var_from_row(ctx, tab, row);
484 var = var_from_col(ctx, tab, col);
487 for (i = tab->n_redundant; i < tab->n_row; ++i) {
488 if (isl_int_is_zero(mat->row[i][2 + col]))
490 if (!var_from_row(ctx, tab, i)->frozen &&
491 is_redundant(ctx, tab, i))
492 if (mark_redundant(ctx, tab, i))
497 /* If "var" represents a column variable, then pivot is up (sgn > 0)
498 * or down (sgn < 0) to a row. The variable is assumed not to be
499 * unbounded in the specified direction.
501 static void to_row(struct isl_ctx *ctx,
502 struct isl_tab *tab, struct isl_tab_var *var, int sign)
509 r = pivot_row(ctx, tab, NULL, sign, var->index);
510 isl_assert(ctx, r >= 0, return);
511 pivot(ctx, tab, r, var->index);
514 static void check_table(struct isl_ctx *ctx, struct isl_tab *tab)
520 for (i = 0; i < tab->n_row; ++i) {
521 if (!var_from_row(ctx, tab, i)->is_nonneg)
523 assert(!isl_int_is_neg(tab->mat->row[i][1]));
527 /* Return the sign of the maximal value of "var".
528 * If the sign is not negative, then on return from this function,
529 * the sample value will also be non-negative.
531 * If "var" is manifestly unbounded wrt positive values, we are done.
532 * Otherwise, we pivot the variable up to a row if needed
533 * Then we continue pivoting down until either
534 * - no more down pivots can be performed
535 * - the sample value is positive
536 * - the variable is pivoted into a manifestly unbounded column
538 static int sign_of_max(struct isl_ctx *ctx,
539 struct isl_tab *tab, struct isl_tab_var *var)
543 if (max_is_manifestly_unbounded(ctx, tab, var))
545 to_row(ctx, tab, var, 1);
546 while (!isl_int_is_pos(tab->mat->row[var->index][1])) {
547 find_pivot(ctx, tab, var, var, 1, &row, &col);
549 return isl_int_sgn(tab->mat->row[var->index][1]);
550 pivot(ctx, tab, row, col);
551 if (!var->is_row) /* manifestly unbounded */
557 /* Perform pivots until the row variable "var" has a non-negative
558 * sample value or until no more upward pivots can be performed.
559 * Return the sign of the sample value after the pivots have been
562 static int restore_row(struct isl_ctx *ctx,
563 struct isl_tab *tab, struct isl_tab_var *var)
567 while (isl_int_is_neg(tab->mat->row[var->index][1])) {
568 find_pivot(ctx, tab, var, var, 1, &row, &col);
571 pivot(ctx, tab, row, col);
572 if (!var->is_row) /* manifestly unbounded */
575 return isl_int_sgn(tab->mat->row[var->index][1]);
578 /* Perform pivots until we are sure that the row variable "var"
579 * can attain non-negative values. After return from this
580 * function, "var" is still a row variable, but its sample
581 * value may not be non-negative, even if the function returns 1.
583 static int at_least_zero(struct isl_ctx *ctx,
584 struct isl_tab *tab, struct isl_tab_var *var)
588 while (isl_int_is_neg(tab->mat->row[var->index][1])) {
589 find_pivot(ctx, tab, var, var, 1, &row, &col);
592 if (row == var->index) /* manifestly unbounded */
594 pivot(ctx, tab, row, col);
596 return !isl_int_is_neg(tab->mat->row[var->index][1]);
599 /* Return a negative value if "var" can attain negative values.
600 * Return a non-negative value otherwise.
602 * If "var" is manifestly unbounded wrt negative values, we are done.
603 * Otherwise, if var is in a column, we can pivot it down to a row.
604 * Then we continue pivoting down until either
605 * - the pivot would result in a manifestly unbounded column
606 * => we don't perform the pivot, but simply return -1
607 * - no more down pivots can be performed
608 * - the sample value is negative
609 * If the sample value becomes negative and the variable is supposed
610 * to be nonnegative, then we undo the last pivot.
611 * However, if the last pivot has made the pivoting variable
612 * obviously redundant, then it may have moved to another row.
613 * In that case we look for upward pivots until we reach a non-negative
616 static int sign_of_min(struct isl_ctx *ctx,
617 struct isl_tab *tab, struct isl_tab_var *var)
620 struct isl_tab_var *pivot_var;
622 if (min_is_manifestly_unbounded(ctx, tab, var))
626 row = pivot_row(ctx, tab, NULL, -1, col);
627 pivot_var = var_from_col(ctx, tab, col);
628 pivot(ctx, tab, row, col);
629 if (var->is_redundant)
631 if (isl_int_is_neg(tab->mat->row[var->index][1])) {
632 if (var->is_nonneg) {
633 if (!pivot_var->is_redundant &&
634 pivot_var->index == row)
635 pivot(ctx, tab, row, col);
637 restore_row(ctx, tab, var);
642 if (var->is_redundant)
644 while (!isl_int_is_neg(tab->mat->row[var->index][1])) {
645 find_pivot(ctx, tab, var, var, -1, &row, &col);
646 if (row == var->index)
649 return isl_int_sgn(tab->mat->row[var->index][1]);
650 pivot_var = var_from_col(ctx, tab, col);
651 pivot(ctx, tab, row, col);
652 if (var->is_redundant)
655 if (var->is_nonneg) {
656 /* pivot back to non-negative value */
657 if (!pivot_var->is_redundant && pivot_var->index == row)
658 pivot(ctx, tab, row, col);
660 restore_row(ctx, tab, var);
665 /* Return 1 if "var" can attain values <= -1.
666 * Return 0 otherwise.
668 * The sample value of "var" is assumed to be non-negative when the
669 * the function is called and will be made non-negative again before
670 * the function returns.
672 static int min_at_most_neg_one(struct isl_ctx *ctx,
673 struct isl_tab *tab, struct isl_tab_var *var)
676 struct isl_tab_var *pivot_var;
678 if (min_is_manifestly_unbounded(ctx, tab, var))
682 row = pivot_row(ctx, tab, NULL, -1, col);
683 pivot_var = var_from_col(ctx, tab, col);
684 pivot(ctx, tab, row, col);
685 if (var->is_redundant)
687 if (isl_int_is_neg(tab->mat->row[var->index][1]) &&
688 isl_int_abs_ge(tab->mat->row[var->index][1],
689 tab->mat->row[var->index][0])) {
690 if (var->is_nonneg) {
691 if (!pivot_var->is_redundant &&
692 pivot_var->index == row)
693 pivot(ctx, tab, row, col);
695 restore_row(ctx, tab, var);
700 if (var->is_redundant)
703 find_pivot(ctx, tab, var, var, -1, &row, &col);
704 if (row == var->index)
708 pivot_var = var_from_col(ctx, tab, col);
709 pivot(ctx, tab, row, col);
710 if (var->is_redundant)
712 } while (!isl_int_is_neg(tab->mat->row[var->index][1]) ||
713 isl_int_abs_lt(tab->mat->row[var->index][1],
714 tab->mat->row[var->index][0]));
715 if (var->is_nonneg) {
716 /* pivot back to non-negative value */
717 if (!pivot_var->is_redundant && pivot_var->index == row)
718 pivot(ctx, tab, row, col);
719 restore_row(ctx, tab, var);
724 /* Return 1 if "var" can attain values >= 1.
725 * Return 0 otherwise.
727 static int at_least_one(struct isl_ctx *ctx,
728 struct isl_tab *tab, struct isl_tab_var *var)
733 if (max_is_manifestly_unbounded(ctx, tab, var))
735 to_row(ctx, tab, var, 1);
736 r = tab->mat->row[var->index];
737 while (isl_int_lt(r[1], r[0])) {
738 find_pivot(ctx, tab, var, var, 1, &row, &col);
740 return isl_int_ge(r[1], r[0]);
741 if (row == var->index) /* manifestly unbounded */
743 pivot(ctx, tab, row, col);
748 static void swap_cols(struct isl_ctx *ctx,
749 struct isl_tab *tab, int col1, int col2)
752 t = tab->col_var[col1];
753 tab->col_var[col1] = tab->col_var[col2];
754 tab->col_var[col2] = t;
755 var_from_col(ctx, tab, col1)->index = col1;
756 var_from_col(ctx, tab, col2)->index = col2;
757 tab->mat = isl_mat_swap_cols(ctx, tab->mat, 2 + col1, 2 + col2);
760 /* Mark column with index "col" as representing a zero variable.
761 * If we may need to undo the operation the column is kept,
762 * but no longer considered.
763 * Otherwise, the column is simply removed.
765 * The column may be interchanged with some other column. If it
766 * is interchanged with a later column, return 1. Otherwise return 0.
767 * If the columns are checked in order in the calling function,
768 * then a return value of 1 means that the column with the given
769 * column number may now contain a different column that
770 * hasn't been checked yet.
772 static int kill_col(struct isl_ctx *ctx,
773 struct isl_tab *tab, int col)
775 var_from_col(ctx, tab, col)->is_zero = 1;
776 if (tab->need_undo) {
777 push(ctx, tab, isl_tab_undo_zero, var_from_col(ctx, tab, col));
778 if (col != tab->n_dead)
779 swap_cols(ctx, tab, col, tab->n_dead);
783 if (col != tab->n_col - 1)
784 swap_cols(ctx, tab, col, tab->n_col - 1);
785 var_from_col(ctx, tab, tab->n_col - 1)->index = -1;
791 /* Row variable "var" is non-negative and cannot attain any values
792 * larger than zero. This means that the coefficients of the unrestricted
793 * column variables are zero and that the coefficients of the non-negative
794 * column variables are zero or negative.
795 * Each of the non-negative variables with a negative coefficient can
796 * then also be written as the negative sum of non-negative variables
797 * and must therefore also be zero.
799 static void close_row(struct isl_ctx *ctx,
800 struct isl_tab *tab, struct isl_tab_var *var)
803 struct isl_mat *mat = tab->mat;
805 isl_assert(ctx, var->is_nonneg, return);
807 for (j = tab->n_dead; j < tab->n_col; ++j) {
808 if (isl_int_is_zero(mat->row[var->index][2 + j]))
810 isl_assert(ctx, isl_int_is_neg(mat->row[var->index][2 + j]),
812 if (kill_col(ctx, tab, j))
815 mark_redundant(ctx, tab, var->index);
818 /* Add a row to the tableau. The row is given as an affine combination
819 * of the original variables and needs to be expressed in terms of the
822 * We add each term in turn.
823 * If r = n/d_r is the current sum and we need to add k x, then
824 * if x is a column variable, we increase the numerator of
825 * this column by k d_r
826 * if x = f/d_x is a row variable, then the new representation of r is
828 * n k f d_x/g n + d_r/g k f m/d_r n + m/d_g k f
829 * --- + --- = ------------------- = -------------------
830 * d_r d_r d_r d_x/g m
832 * with g the gcd of d_r and d_x and m the lcm of d_r and d_x.
834 static int add_row(struct isl_ctx *ctx, struct isl_tab *tab, isl_int *line)
841 isl_assert(ctx, tab->n_row < tab->mat->n_row, return -1);
846 tab->con[r].index = tab->n_row;
847 tab->con[r].is_row = 1;
848 tab->con[r].is_nonneg = 0;
849 tab->con[r].is_zero = 0;
850 tab->con[r].is_redundant = 0;
851 tab->con[r].frozen = 0;
852 tab->row_var[tab->n_row] = ~r;
853 row = tab->mat->row[tab->n_row];
854 isl_int_set_si(row[0], 1);
855 isl_int_set(row[1], line[0]);
856 isl_seq_clr(row + 2, tab->n_col);
857 for (i = 0; i < tab->n_var; ++i) {
858 if (tab->var[i].is_zero)
860 if (tab->var[i].is_row) {
862 row[0], tab->mat->row[tab->var[i].index][0]);
863 isl_int_swap(a, row[0]);
864 isl_int_divexact(a, row[0], a);
866 row[0], tab->mat->row[tab->var[i].index][0]);
867 isl_int_mul(b, b, line[1 + i]);
868 isl_seq_combine(row + 1, a, row + 1,
869 b, tab->mat->row[tab->var[i].index] + 1,
872 isl_int_addmul(row[2 + tab->var[i].index],
873 line[1 + i], row[0]);
875 isl_seq_normalize(row, 2 + tab->n_col);
878 push(ctx, tab, isl_tab_undo_allocate, &tab->con[r]);
885 static int drop_row(struct isl_ctx *ctx, struct isl_tab *tab, int row)
887 isl_assert(ctx, ~tab->row_var[row] == tab->n_con - 1, return -1);
888 if (row != tab->n_row - 1)
889 swap_rows(ctx, tab, row, tab->n_row - 1);
895 /* Add inequality "ineq" and check if it conflicts with the
896 * previously added constraints or if it is obviously redundant.
898 struct isl_tab *isl_tab_add_ineq(struct isl_ctx *ctx,
899 struct isl_tab *tab, isl_int *ineq)
906 r = add_row(ctx, tab, ineq);
909 tab->con[r].is_nonneg = 1;
910 push(ctx, tab, isl_tab_undo_nonneg, &tab->con[r]);
911 if (is_redundant(ctx, tab, tab->con[r].index)) {
912 mark_redundant(ctx, tab, tab->con[r].index);
916 sgn = restore_row(ctx, tab, &tab->con[r]);
918 mark_empty(ctx, tab);
919 else if (tab->con[r].is_row &&
920 is_redundant(ctx, tab, tab->con[r].index))
921 mark_redundant(ctx, tab, tab->con[r].index);
924 isl_tab_free(ctx, tab);
928 /* Pivot a non-negative variable down until it reaches the value zero
929 * and then pivot the variable into a column position.
931 static int to_col(struct isl_ctx *ctx,
932 struct isl_tab *tab, struct isl_tab_var *var)
940 while (isl_int_is_pos(tab->mat->row[var->index][1])) {
941 find_pivot(ctx, tab, var, NULL, -1, &row, &col);
942 isl_assert(ctx, row != -1, return -1);
943 pivot(ctx, tab, row, col);
948 for (i = tab->n_dead; i < tab->n_col; ++i)
949 if (!isl_int_is_zero(tab->mat->row[var->index][2 + i]))
952 isl_assert(ctx, i < tab->n_col, return -1);
953 pivot(ctx, tab, var->index, i);
958 /* We assume Gaussian elimination has been performed on the equalities.
959 * The equalities can therefore never conflict.
960 * Adding the equalities is currently only really useful for a later call
961 * to isl_tab_ineq_type.
963 static struct isl_tab *add_eq(struct isl_ctx *ctx,
964 struct isl_tab *tab, isl_int *eq)
971 r = add_row(ctx, tab, eq);
975 r = tab->con[r].index;
976 for (i = tab->n_dead; i < tab->n_col; ++i) {
977 if (isl_int_is_zero(tab->mat->row[r][2 + i]))
979 pivot(ctx, tab, r, i);
980 kill_col(ctx, tab, i);
987 isl_tab_free(ctx, tab);
991 /* Add an equality that is known to be valid for the given tableau.
993 struct isl_tab *isl_tab_add_valid_eq(struct isl_ctx *ctx,
994 struct isl_tab *tab, isl_int *eq)
996 struct isl_tab_var *var;
1002 r = add_row(ctx, tab, eq);
1008 if (isl_int_is_neg(tab->mat->row[r][1]))
1009 isl_seq_neg(tab->mat->row[r] + 1, tab->mat->row[r] + 1,
1012 if (to_col(ctx, tab, var) < 0)
1015 kill_col(ctx, tab, var->index);
1019 isl_tab_free(ctx, tab);
1023 struct isl_tab *isl_tab_from_basic_map(struct isl_basic_map *bmap)
1026 struct isl_tab *tab;
1030 tab = isl_tab_alloc(bmap->ctx,
1031 isl_basic_map_total_dim(bmap) + bmap->n_ineq + 1,
1032 isl_basic_map_total_dim(bmap));
1035 tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
1036 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) {
1037 mark_empty(bmap->ctx, tab);
1040 for (i = 0; i < bmap->n_eq; ++i) {
1041 tab = add_eq(bmap->ctx, tab, bmap->eq[i]);
1045 for (i = 0; i < bmap->n_ineq; ++i) {
1046 tab = isl_tab_add_ineq(bmap->ctx, tab, bmap->ineq[i]);
1047 if (!tab || tab->empty)
1053 struct isl_tab *isl_tab_from_basic_set(struct isl_basic_set *bset)
1055 return isl_tab_from_basic_map((struct isl_basic_map *)bset);
1058 /* Construct a tableau corresponding to the recession cone of "bmap".
1060 struct isl_tab *isl_tab_from_recession_cone(struct isl_basic_map *bmap)
1064 struct isl_tab *tab;
1068 tab = isl_tab_alloc(bmap->ctx, bmap->n_eq + bmap->n_ineq,
1069 isl_basic_map_total_dim(bmap));
1072 tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
1075 for (i = 0; i < bmap->n_eq; ++i) {
1076 isl_int_swap(bmap->eq[i][0], cst);
1077 tab = add_eq(bmap->ctx, tab, bmap->eq[i]);
1078 isl_int_swap(bmap->eq[i][0], cst);
1082 for (i = 0; i < bmap->n_ineq; ++i) {
1084 isl_int_swap(bmap->ineq[i][0], cst);
1085 r = add_row(bmap->ctx, tab, bmap->ineq[i]);
1086 isl_int_swap(bmap->ineq[i][0], cst);
1089 tab->con[r].is_nonneg = 1;
1090 push(bmap->ctx, tab, isl_tab_undo_nonneg, &tab->con[r]);
1097 isl_tab_free(bmap->ctx, tab);
1101 /* Assuming "tab" is the tableau of a cone, check if the cone is
1102 * bounded, i.e., if it is empty or only contains the origin.
1104 int isl_tab_cone_is_bounded(struct isl_ctx *ctx, struct isl_tab *tab)
1112 if (tab->n_dead == tab->n_col)
1115 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1116 struct isl_tab_var *var;
1117 var = var_from_row(ctx, tab, i);
1118 if (!var->is_nonneg)
1120 if (sign_of_max(ctx, tab, var) == 0)
1121 close_row(ctx, tab, var);
1124 if (tab->n_dead == tab->n_col)
1130 static int sample_is_integer(struct isl_ctx *ctx, struct isl_tab *tab)
1134 for (i = 0; i < tab->n_var; ++i) {
1136 if (!tab->var[i].is_row)
1138 row = tab->var[i].index;
1139 if (!isl_int_is_divisible_by(tab->mat->row[row][1],
1140 tab->mat->row[row][0]))
1146 static struct isl_vec *extract_integer_sample(struct isl_ctx *ctx,
1147 struct isl_tab *tab)
1150 struct isl_vec *vec;
1152 vec = isl_vec_alloc(ctx, 1 + tab->n_var);
1156 isl_int_set_si(vec->block.data[0], 1);
1157 for (i = 0; i < tab->n_var; ++i) {
1158 if (!tab->var[i].is_row)
1159 isl_int_set_si(vec->block.data[1 + i], 0);
1161 int row = tab->var[i].index;
1162 isl_int_divexact(vec->block.data[1 + i],
1163 tab->mat->row[row][1], tab->mat->row[row][0]);
1170 struct isl_vec *isl_tab_get_sample_value(struct isl_ctx *ctx,
1171 struct isl_tab *tab)
1174 struct isl_vec *vec;
1180 vec = isl_vec_alloc(ctx, 1 + tab->n_var);
1186 isl_int_set_si(vec->block.data[0], 1);
1187 for (i = 0; i < tab->n_var; ++i) {
1189 if (!tab->var[i].is_row) {
1190 isl_int_set_si(vec->block.data[1 + i], 0);
1193 row = tab->var[i].index;
1194 isl_int_gcd(m, vec->block.data[0], tab->mat->row[row][0]);
1195 isl_int_divexact(m, tab->mat->row[row][0], m);
1196 isl_seq_scale(vec->block.data, vec->block.data, m, 1 + i);
1197 isl_int_divexact(m, vec->block.data[0], tab->mat->row[row][0]);
1198 isl_int_mul(vec->block.data[1 + i], m, tab->mat->row[row][1]);
1200 isl_seq_normalize(vec->block.data, vec->size);
1206 /* Update "bmap" based on the results of the tableau "tab".
1207 * In particular, implicit equalities are made explicit, redundant constraints
1208 * are removed and if the sample value happens to be integer, it is stored
1209 * in "bmap" (unless "bmap" already had an integer sample).
1211 * The tableau is assumed to have been created from "bmap" using
1212 * isl_tab_from_basic_map.
1214 struct isl_basic_map *isl_basic_map_update_from_tab(struct isl_basic_map *bmap,
1215 struct isl_tab *tab)
1227 bmap = isl_basic_map_set_to_empty(bmap);
1229 for (i = bmap->n_ineq - 1; i >= 0; --i) {
1230 if (isl_tab_is_equality(bmap->ctx, tab, n_eq + i))
1231 isl_basic_map_inequality_to_equality(bmap, i);
1232 else if (isl_tab_is_redundant(bmap->ctx, tab, n_eq + i))
1233 isl_basic_map_drop_inequality(bmap, i);
1235 if (!tab->rational &&
1236 !bmap->sample && sample_is_integer(bmap->ctx, tab))
1237 bmap->sample = extract_integer_sample(bmap->ctx, tab);
1241 struct isl_basic_set *isl_basic_set_update_from_tab(struct isl_basic_set *bset,
1242 struct isl_tab *tab)
1244 return (struct isl_basic_set *)isl_basic_map_update_from_tab(
1245 (struct isl_basic_map *)bset, tab);
1248 /* Given a non-negative variable "var", add a new non-negative variable
1249 * that is the opposite of "var", ensuring that var can only attain the
1251 * If var = n/d is a row variable, then the new variable = -n/d.
1252 * If var is a column variables, then the new variable = -var.
1253 * If the new variable cannot attain non-negative values, then
1254 * the resulting tableau is empty.
1255 * Otherwise, we know the value will be zero and we close the row.
1257 static struct isl_tab *cut_to_hyperplane(struct isl_ctx *ctx,
1258 struct isl_tab *tab, struct isl_tab_var *var)
1264 if (extend_cons(ctx, tab, 1) < 0)
1268 tab->con[r].index = tab->n_row;
1269 tab->con[r].is_row = 1;
1270 tab->con[r].is_nonneg = 0;
1271 tab->con[r].is_zero = 0;
1272 tab->con[r].is_redundant = 0;
1273 tab->con[r].frozen = 0;
1274 tab->row_var[tab->n_row] = ~r;
1275 row = tab->mat->row[tab->n_row];
1278 isl_int_set(row[0], tab->mat->row[var->index][0]);
1279 isl_seq_neg(row + 1,
1280 tab->mat->row[var->index] + 1, 1 + tab->n_col);
1282 isl_int_set_si(row[0], 1);
1283 isl_seq_clr(row + 1, 1 + tab->n_col);
1284 isl_int_set_si(row[2 + var->index], -1);
1289 push(ctx, tab, isl_tab_undo_allocate, &tab->con[r]);
1291 sgn = sign_of_max(ctx, tab, &tab->con[r]);
1293 mark_empty(ctx, tab);
1295 tab->con[r].is_nonneg = 1;
1296 push(ctx, tab, isl_tab_undo_nonneg, &tab->con[r]);
1298 close_row(ctx, tab, &tab->con[r]);
1303 isl_tab_free(ctx, tab);
1307 /* Given a tableau "tab" and an inequality constraint "con" of the tableau,
1308 * relax the inequality by one. That is, the inequality r >= 0 is replaced
1309 * by r' = r + 1 >= 0.
1310 * If r is a row variable, we simply increase the constant term by one
1311 * (taking into account the denominator).
1312 * If r is a column variable, then we need to modify each row that
1313 * refers to r = r' - 1 by substituting this equality, effectively
1314 * subtracting the coefficient of the column from the constant.
1316 struct isl_tab *isl_tab_relax(struct isl_ctx *ctx,
1317 struct isl_tab *tab, int con)
1319 struct isl_tab_var *var;
1323 var = &tab->con[con];
1325 if (!var->is_row && !max_is_manifestly_unbounded(ctx, tab, var))
1326 to_row(ctx, tab, var, 1);
1329 isl_int_add(tab->mat->row[var->index][1],
1330 tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
1334 for (i = 0; i < tab->n_row; ++i) {
1335 if (isl_int_is_zero(tab->mat->row[i][2 + var->index]))
1337 isl_int_sub(tab->mat->row[i][1], tab->mat->row[i][1],
1338 tab->mat->row[i][2 + var->index]);
1343 push(ctx, tab, isl_tab_undo_relax, var);
1348 struct isl_tab *isl_tab_select_facet(struct isl_ctx *ctx,
1349 struct isl_tab *tab, int con)
1354 return cut_to_hyperplane(ctx, tab, &tab->con[con]);
1357 static int may_be_equality(struct isl_tab *tab, int row)
1359 return (tab->rational ? isl_int_is_zero(tab->mat->row[row][1])
1360 : isl_int_lt(tab->mat->row[row][1],
1361 tab->mat->row[row][0])) &&
1362 isl_seq_first_non_zero(tab->mat->row[row] + 2 + tab->n_dead,
1363 tab->n_col - tab->n_dead) != -1;
1366 /* Check for (near) equalities among the constraints.
1367 * A constraint is an equality if it is non-negative and if
1368 * its maximal value is either
1369 * - zero (in case of rational tableaus), or
1370 * - strictly less than 1 (in case of integer tableaus)
1372 * We first mark all non-redundant and non-dead variables that
1373 * are not frozen and not obviously not an equality.
1374 * Then we iterate over all marked variables if they can attain
1375 * any values larger than zero or at least one.
1376 * If the maximal value is zero, we mark any column variables
1377 * that appear in the row as being zero and mark the row as being redundant.
1378 * Otherwise, if the maximal value is strictly less than one (and the
1379 * tableau is integer), then we restrict the value to being zero
1380 * by adding an opposite non-negative variable.
1382 struct isl_tab *isl_tab_detect_equalities(struct isl_ctx *ctx,
1383 struct isl_tab *tab)
1392 if (tab->n_dead == tab->n_col)
1396 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1397 struct isl_tab_var *var = var_from_row(ctx, tab, i);
1398 var->marked = !var->frozen && var->is_nonneg &&
1399 may_be_equality(tab, i);
1403 for (i = tab->n_dead; i < tab->n_col; ++i) {
1404 struct isl_tab_var *var = var_from_col(ctx, tab, i);
1405 var->marked = !var->frozen && var->is_nonneg;
1410 struct isl_tab_var *var;
1411 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1412 var = var_from_row(ctx, tab, i);
1416 if (i == tab->n_row) {
1417 for (i = tab->n_dead; i < tab->n_col; ++i) {
1418 var = var_from_col(ctx, tab, i);
1422 if (i == tab->n_col)
1427 if (sign_of_max(ctx, tab, var) == 0)
1428 close_row(ctx, tab, var);
1429 else if (!tab->rational && !at_least_one(ctx, tab, var)) {
1430 tab = cut_to_hyperplane(ctx, tab, var);
1431 return isl_tab_detect_equalities(ctx, tab);
1433 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1434 var = var_from_row(ctx, tab, i);
1437 if (may_be_equality(tab, i))
1447 /* Check for (near) redundant constraints.
1448 * A constraint is redundant if it is non-negative and if
1449 * its minimal value (temporarily ignoring the non-negativity) is either
1450 * - zero (in case of rational tableaus), or
1451 * - strictly larger than -1 (in case of integer tableaus)
1453 * We first mark all non-redundant and non-dead variables that
1454 * are not frozen and not obviously negatively unbounded.
1455 * Then we iterate over all marked variables if they can attain
1456 * any values smaller than zero or at most negative one.
1457 * If not, we mark the row as being redundant (assuming it hasn't
1458 * been detected as being obviously redundant in the mean time).
1460 struct isl_tab *isl_tab_detect_redundant(struct isl_ctx *ctx,
1461 struct isl_tab *tab)
1470 if (tab->n_redundant == tab->n_row)
1474 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1475 struct isl_tab_var *var = var_from_row(ctx, tab, i);
1476 var->marked = !var->frozen && var->is_nonneg;
1480 for (i = tab->n_dead; i < tab->n_col; ++i) {
1481 struct isl_tab_var *var = var_from_col(ctx, tab, i);
1482 var->marked = !var->frozen && var->is_nonneg &&
1483 !min_is_manifestly_unbounded(ctx, tab, var);
1488 struct isl_tab_var *var;
1489 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1490 var = var_from_row(ctx, tab, i);
1494 if (i == tab->n_row) {
1495 for (i = tab->n_dead; i < tab->n_col; ++i) {
1496 var = var_from_col(ctx, tab, i);
1500 if (i == tab->n_col)
1505 if ((tab->rational ? (sign_of_min(ctx, tab, var) >= 0)
1506 : !min_at_most_neg_one(ctx, tab, var)) &&
1508 mark_redundant(ctx, tab, var->index);
1509 for (i = tab->n_dead; i < tab->n_col; ++i) {
1510 var = var_from_col(ctx, tab, i);
1513 if (!min_is_manifestly_unbounded(ctx, tab, var))
1523 int isl_tab_is_equality(struct isl_ctx *ctx, struct isl_tab *tab, int con)
1529 if (tab->con[con].is_zero)
1531 if (tab->con[con].is_redundant)
1533 if (!tab->con[con].is_row)
1534 return tab->con[con].index < tab->n_dead;
1536 row = tab->con[con].index;
1538 return isl_int_is_zero(tab->mat->row[row][1]) &&
1539 isl_seq_first_non_zero(tab->mat->row[row] + 2 + tab->n_dead,
1540 tab->n_col - tab->n_dead) == -1;
1543 /* Return the minimial value of the affine expression "f" with denominator
1544 * "denom" in *opt, *opt_denom, assuming the tableau is not empty and
1545 * the expression cannot attain arbitrarily small values.
1546 * If opt_denom is NULL, then *opt is rounded up to the nearest integer.
1547 * The return value reflects the nature of the result (empty, unbounded,
1548 * minmimal value returned in *opt).
1550 enum isl_lp_result isl_tab_min(struct isl_ctx *ctx, struct isl_tab *tab,
1551 isl_int *f, isl_int denom, isl_int *opt, isl_int *opt_denom,
1555 enum isl_lp_result res = isl_lp_ok;
1556 struct isl_tab_var *var;
1559 return isl_lp_empty;
1561 r = add_row(ctx, tab, f);
1563 return isl_lp_error;
1565 isl_int_mul(tab->mat->row[var->index][0],
1566 tab->mat->row[var->index][0], denom);
1569 find_pivot(ctx, tab, var, var, -1, &row, &col);
1570 if (row == var->index) {
1571 res = isl_lp_unbounded;
1576 pivot(ctx, tab, row, col);
1578 if (drop_row(ctx, tab, var->index) < 0)
1579 return isl_lp_error;
1580 if (ISL_FL_ISSET(flags, ISL_TAB_SAVE_DUAL)) {
1583 isl_vec_free(tab->dual);
1584 tab->dual = isl_vec_alloc(ctx, 1 + tab->n_con);
1586 return isl_lp_error;
1587 isl_int_set(tab->dual->el[0], tab->mat->row[var->index][0]);
1588 for (i = 0; i < tab->n_con; ++i) {
1589 if (tab->con[i].is_row)
1590 isl_int_set_si(tab->dual->el[1 + i], 0);
1592 int pos = 2 + tab->con[i].index;
1593 isl_int_set(tab->dual->el[1 + i],
1594 tab->mat->row[var->index][pos]);
1598 if (res == isl_lp_ok) {
1600 isl_int_set(*opt, tab->mat->row[var->index][1]);
1601 isl_int_set(*opt_denom, tab->mat->row[var->index][0]);
1603 isl_int_cdiv_q(*opt, tab->mat->row[var->index][1],
1604 tab->mat->row[var->index][0]);
1609 int isl_tab_is_redundant(struct isl_ctx *ctx, struct isl_tab *tab, int con)
1616 if (tab->con[con].is_zero)
1618 if (tab->con[con].is_redundant)
1620 return tab->con[con].is_row && tab->con[con].index < tab->n_redundant;
1623 /* Take a snapshot of the tableau that can be restored by s call to
1626 struct isl_tab_undo *isl_tab_snap(struct isl_ctx *ctx, struct isl_tab *tab)
1634 /* Undo the operation performed by isl_tab_relax.
1636 static void unrelax(struct isl_ctx *ctx,
1637 struct isl_tab *tab, struct isl_tab_var *var)
1639 if (!var->is_row && !max_is_manifestly_unbounded(ctx, tab, var))
1640 to_row(ctx, tab, var, 1);
1643 isl_int_sub(tab->mat->row[var->index][1],
1644 tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
1648 for (i = 0; i < tab->n_row; ++i) {
1649 if (isl_int_is_zero(tab->mat->row[i][2 + var->index]))
1651 isl_int_add(tab->mat->row[i][1], tab->mat->row[i][1],
1652 tab->mat->row[i][2 + var->index]);
1658 static void perform_undo(struct isl_ctx *ctx, struct isl_tab *tab,
1659 struct isl_tab_undo *undo)
1661 switch(undo->type) {
1662 case isl_tab_undo_empty:
1665 case isl_tab_undo_nonneg:
1666 undo->var->is_nonneg = 0;
1668 case isl_tab_undo_redundant:
1669 undo->var->is_redundant = 0;
1672 case isl_tab_undo_zero:
1673 undo->var->is_zero = 0;
1676 case isl_tab_undo_allocate:
1677 if (!undo->var->is_row) {
1678 if (max_is_manifestly_unbounded(ctx, tab, undo->var))
1679 to_row(ctx, tab, undo->var, -1);
1681 to_row(ctx, tab, undo->var, 1);
1683 drop_row(ctx, tab, undo->var->index);
1685 case isl_tab_undo_relax:
1686 unrelax(ctx, tab, undo->var);
1691 /* Return the tableau to the state it was in when the snapshot "snap"
1694 int isl_tab_rollback(struct isl_ctx *ctx, struct isl_tab *tab,
1695 struct isl_tab_undo *snap)
1697 struct isl_tab_undo *undo, *next;
1702 for (undo = tab->top; undo && undo != &tab->bottom; undo = next) {
1706 perform_undo(ctx, tab, undo);
1715 /* The given row "row" represents an inequality violated by all
1716 * points in the tableau. Check for some special cases of such
1717 * separating constraints.
1718 * In particular, if the row has been reduced to the constant -1,
1719 * then we know the inequality is adjacent (but opposite) to
1720 * an equality in the tableau.
1721 * If the row has been reduced to r = -1 -r', with r' an inequality
1722 * of the tableau, then the inequality is adjacent (but opposite)
1723 * to the inequality r'.
1725 static enum isl_ineq_type separation_type(struct isl_ctx *ctx,
1726 struct isl_tab *tab, unsigned row)
1731 return isl_ineq_separate;
1733 if (!isl_int_is_one(tab->mat->row[row][0]))
1734 return isl_ineq_separate;
1735 if (!isl_int_is_negone(tab->mat->row[row][1]))
1736 return isl_ineq_separate;
1738 pos = isl_seq_first_non_zero(tab->mat->row[row] + 2 + tab->n_dead,
1739 tab->n_col - tab->n_dead);
1741 return isl_ineq_adj_eq;
1743 if (!isl_int_is_negone(tab->mat->row[row][2 + tab->n_dead + pos]))
1744 return isl_ineq_separate;
1746 pos = isl_seq_first_non_zero(
1747 tab->mat->row[row] + 2 + tab->n_dead + pos + 1,
1748 tab->n_col - tab->n_dead - pos - 1);
1750 return pos == -1 ? isl_ineq_adj_ineq : isl_ineq_separate;
1753 /* Check the effect of inequality "ineq" on the tableau "tab".
1755 * isl_ineq_redundant: satisfied by all points in the tableau
1756 * isl_ineq_separate: satisfied by no point in the tableau
1757 * isl_ineq_cut: satisfied by some by not all points
1758 * isl_ineq_adj_eq: adjacent to an equality
1759 * isl_ineq_adj_ineq: adjacent to an inequality.
1761 enum isl_ineq_type isl_tab_ineq_type(struct isl_ctx *ctx, struct isl_tab *tab,
1764 enum isl_ineq_type type = isl_ineq_error;
1765 struct isl_tab_undo *snap = NULL;
1770 return isl_ineq_error;
1772 if (extend_cons(ctx, tab, 1) < 0)
1773 return isl_ineq_error;
1775 snap = isl_tab_snap(ctx, tab);
1777 con = add_row(ctx, tab, ineq);
1781 row = tab->con[con].index;
1782 if (is_redundant(ctx, tab, row))
1783 type = isl_ineq_redundant;
1784 else if (isl_int_is_neg(tab->mat->row[row][1]) &&
1786 isl_int_abs_ge(tab->mat->row[row][1],
1787 tab->mat->row[row][0]))) {
1788 if (at_least_zero(ctx, tab, &tab->con[con]))
1789 type = isl_ineq_cut;
1791 type = separation_type(ctx, tab, row);
1792 } else if (tab->rational ? (sign_of_min(ctx, tab, &tab->con[con]) < 0)
1793 : min_at_most_neg_one(ctx, tab, &tab->con[con]))
1794 type = isl_ineq_cut;
1796 type = isl_ineq_redundant;
1798 if (isl_tab_rollback(ctx, tab, snap))
1799 return isl_ineq_error;
1802 isl_tab_rollback(ctx, tab, snap);
1803 return isl_ineq_error;
1806 void isl_tab_dump(struct isl_ctx *ctx, struct isl_tab *tab,
1807 FILE *out, int indent)
1813 fprintf(out, "%*snull tab\n", indent, "");
1816 fprintf(out, "%*sn_redundant: %d, n_dead: %d", indent, "",
1817 tab->n_redundant, tab->n_dead);
1819 fprintf(out, ", rational");
1821 fprintf(out, ", empty");
1823 fprintf(out, "%*s[", indent, "");
1824 for (i = 0; i < tab->n_var; ++i) {
1827 fprintf(out, "%c%d%s", tab->var[i].is_row ? 'r' : 'c',
1829 tab->var[i].is_zero ? " [=0]" :
1830 tab->var[i].is_redundant ? " [R]" : "");
1832 fprintf(out, "]\n");
1833 fprintf(out, "%*s[", indent, "");
1834 for (i = 0; i < tab->n_con; ++i) {
1837 fprintf(out, "%c%d%s", tab->con[i].is_row ? 'r' : 'c',
1839 tab->con[i].is_zero ? " [=0]" :
1840 tab->con[i].is_redundant ? " [R]" : "");
1842 fprintf(out, "]\n");
1843 fprintf(out, "%*s[", indent, "");
1844 for (i = 0; i < tab->n_row; ++i) {
1847 fprintf(out, "r%d: %d%s", i, tab->row_var[i],
1848 var_from_row(ctx, tab, i)->is_nonneg ? " [>=0]" : "");
1850 fprintf(out, "]\n");
1851 fprintf(out, "%*s[", indent, "");
1852 for (i = 0; i < tab->n_col; ++i) {
1855 fprintf(out, "c%d: %d%s", i, tab->col_var[i],
1856 var_from_col(ctx, tab, i)->is_nonneg ? " [>=0]" : "");
1858 fprintf(out, "]\n");
1859 r = tab->mat->n_row;
1860 tab->mat->n_row = tab->n_row;
1861 c = tab->mat->n_col;
1862 tab->mat->n_col = 2 + tab->n_col;
1863 isl_mat_dump(ctx, tab->mat, out, indent);
1864 tab->mat->n_row = r;
1865 tab->mat->n_col = c;