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 static struct isl_tab_var *var_from_index(struct isl_tab *tab, int i)
265 return &tab->con[~i];
268 struct isl_tab_var *isl_tab_var_from_row(struct isl_tab *tab, int i)
270 return var_from_index(tab, tab->row_var[i]);
273 static struct isl_tab_var *var_from_col(struct isl_tab *tab, int i)
275 return var_from_index(tab, tab->col_var[i]);
278 /* Check if there are any upper bounds on column variable "var",
279 * i.e., non-negative rows where var appears with a negative coefficient.
280 * Return 1 if there are no such bounds.
282 static int max_is_manifestly_unbounded(struct isl_tab *tab,
283 struct isl_tab_var *var)
286 unsigned off = 2 + tab->M;
290 for (i = tab->n_redundant; i < tab->n_row; ++i) {
291 if (!isl_int_is_neg(tab->mat->row[i][off + var->index]))
293 if (isl_tab_var_from_row(tab, i)->is_nonneg)
299 /* Check if there are any lower bounds on column variable "var",
300 * i.e., non-negative rows where var appears with a positive coefficient.
301 * Return 1 if there are no such bounds.
303 static int min_is_manifestly_unbounded(struct isl_tab *tab,
304 struct isl_tab_var *var)
307 unsigned off = 2 + tab->M;
311 for (i = tab->n_redundant; i < tab->n_row; ++i) {
312 if (!isl_int_is_pos(tab->mat->row[i][off + var->index]))
314 if (isl_tab_var_from_row(tab, i)->is_nonneg)
320 static int row_cmp(struct isl_tab *tab, int r1, int r2, int c, isl_int t)
322 unsigned off = 2 + tab->M;
326 isl_int_mul(t, tab->mat->row[r1][2], tab->mat->row[r2][off+c]);
327 isl_int_submul(t, tab->mat->row[r2][2], tab->mat->row[r1][off+c]);
332 isl_int_mul(t, tab->mat->row[r1][1], tab->mat->row[r2][off + c]);
333 isl_int_submul(t, tab->mat->row[r2][1], tab->mat->row[r1][off + c]);
334 return isl_int_sgn(t);
337 /* Given the index of a column "c", return the index of a row
338 * that can be used to pivot the column in, with either an increase
339 * (sgn > 0) or a decrease (sgn < 0) of the corresponding variable.
340 * If "var" is not NULL, then the row returned will be different from
341 * the one associated with "var".
343 * Each row in the tableau is of the form
345 * x_r = a_r0 + \sum_i a_ri x_i
347 * Only rows with x_r >= 0 and with the sign of a_ri opposite to "sgn"
348 * impose any limit on the increase or decrease in the value of x_c
349 * and this bound is equal to a_r0 / |a_rc|. We are therefore looking
350 * for the row with the smallest (most stringent) such bound.
351 * Note that the common denominator of each row drops out of the fraction.
352 * To check if row j has a smaller bound than row r, i.e.,
353 * a_j0 / |a_jc| < a_r0 / |a_rc| or a_j0 |a_rc| < a_r0 |a_jc|,
354 * we check if -sign(a_jc) (a_j0 a_rc - a_r0 a_jc) < 0,
355 * where -sign(a_jc) is equal to "sgn".
357 static int pivot_row(struct isl_tab *tab,
358 struct isl_tab_var *var, int sgn, int c)
362 unsigned off = 2 + tab->M;
366 for (j = tab->n_redundant; j < tab->n_row; ++j) {
367 if (var && j == var->index)
369 if (!isl_tab_var_from_row(tab, j)->is_nonneg)
371 if (sgn * isl_int_sgn(tab->mat->row[j][off + c]) >= 0)
377 tsgn = sgn * row_cmp(tab, r, j, c, t);
378 if (tsgn < 0 || (tsgn == 0 &&
379 tab->row_var[j] < tab->row_var[r]))
386 /* Find a pivot (row and col) that will increase (sgn > 0) or decrease
387 * (sgn < 0) the value of row variable var.
388 * If not NULL, then skip_var is a row variable that should be ignored
389 * while looking for a pivot row. It is usually equal to var.
391 * As the given row in the tableau is of the form
393 * x_r = a_r0 + \sum_i a_ri x_i
395 * we need to find a column such that the sign of a_ri is equal to "sgn"
396 * (such that an increase in x_i will have the desired effect) or a
397 * column with a variable that may attain negative values.
398 * If a_ri is positive, then we need to move x_i in the same direction
399 * to obtain the desired effect. Otherwise, x_i has to move in the
400 * opposite direction.
402 static void find_pivot(struct isl_tab *tab,
403 struct isl_tab_var *var, struct isl_tab_var *skip_var,
404 int sgn, int *row, int *col)
411 isl_assert(tab->mat->ctx, var->is_row, return);
412 tr = tab->mat->row[var->index] + 2 + tab->M;
415 for (j = tab->n_dead; j < tab->n_col; ++j) {
416 if (isl_int_is_zero(tr[j]))
418 if (isl_int_sgn(tr[j]) != sgn &&
419 var_from_col(tab, j)->is_nonneg)
421 if (c < 0 || tab->col_var[j] < tab->col_var[c])
427 sgn *= isl_int_sgn(tr[c]);
428 r = pivot_row(tab, skip_var, sgn, c);
429 *row = r < 0 ? var->index : r;
433 /* Return 1 if row "row" represents an obviously redundant inequality.
435 * - it represents an inequality or a variable
436 * - that is the sum of a non-negative sample value and a positive
437 * combination of zero or more non-negative variables.
439 int isl_tab_row_is_redundant(struct isl_tab *tab, int row)
442 unsigned off = 2 + tab->M;
444 if (tab->row_var[row] < 0 && !isl_tab_var_from_row(tab, row)->is_nonneg)
447 if (isl_int_is_neg(tab->mat->row[row][1]))
449 if (tab->M && isl_int_is_neg(tab->mat->row[row][2]))
452 for (i = tab->n_dead; i < tab->n_col; ++i) {
453 if (isl_int_is_zero(tab->mat->row[row][off + i]))
455 if (isl_int_is_neg(tab->mat->row[row][off + i]))
457 if (!var_from_col(tab, i)->is_nonneg)
463 static void swap_rows(struct isl_tab *tab, int row1, int row2)
466 t = tab->row_var[row1];
467 tab->row_var[row1] = tab->row_var[row2];
468 tab->row_var[row2] = t;
469 isl_tab_var_from_row(tab, row1)->index = row1;
470 isl_tab_var_from_row(tab, row2)->index = row2;
471 tab->mat = isl_mat_swap_rows(tab->mat, row1, row2);
475 t = tab->row_sign[row1];
476 tab->row_sign[row1] = tab->row_sign[row2];
477 tab->row_sign[row2] = t;
480 static void push_union(struct isl_tab *tab,
481 enum isl_tab_undo_type type, union isl_tab_undo_val u)
483 struct isl_tab_undo *undo;
488 undo = isl_alloc_type(tab->mat->ctx, struct isl_tab_undo);
496 undo->next = tab->top;
500 void isl_tab_push_var(struct isl_tab *tab,
501 enum isl_tab_undo_type type, struct isl_tab_var *var)
503 union isl_tab_undo_val u;
505 u.var_index = tab->row_var[var->index];
507 u.var_index = tab->col_var[var->index];
508 push_union(tab, type, u);
511 void isl_tab_push(struct isl_tab *tab, enum isl_tab_undo_type type)
513 union isl_tab_undo_val u = { 0 };
514 push_union(tab, type, u);
517 /* Push a record on the undo stack describing the current basic
518 * variables, so that the this state can be restored during rollback.
520 void isl_tab_push_basis(struct isl_tab *tab)
523 union isl_tab_undo_val u;
525 u.col_var = isl_alloc_array(tab->mat->ctx, int, tab->n_col);
531 for (i = 0; i < tab->n_col; ++i)
532 u.col_var[i] = tab->col_var[i];
533 push_union(tab, isl_tab_undo_saved_basis, u);
536 /* Mark row with index "row" as being redundant.
537 * If we may need to undo the operation or if the row represents
538 * a variable of the original problem, the row is kept,
539 * but no longer considered when looking for a pivot row.
540 * Otherwise, the row is simply removed.
542 * The row may be interchanged with some other row. If it
543 * is interchanged with a later row, return 1. Otherwise return 0.
544 * If the rows are checked in order in the calling function,
545 * then a return value of 1 means that the row with the given
546 * row number may now contain a different row that hasn't been checked yet.
548 int isl_tab_mark_redundant(struct isl_tab *tab, int row)
550 struct isl_tab_var *var = isl_tab_var_from_row(tab, row);
551 var->is_redundant = 1;
552 isl_assert(tab->mat->ctx, row >= tab->n_redundant, return -1);
553 if (tab->need_undo || tab->row_var[row] >= 0) {
554 if (tab->row_var[row] >= 0 && !var->is_nonneg) {
556 isl_tab_push_var(tab, isl_tab_undo_nonneg, var);
558 if (row != tab->n_redundant)
559 swap_rows(tab, row, tab->n_redundant);
560 isl_tab_push_var(tab, isl_tab_undo_redundant, var);
564 if (row != tab->n_row - 1)
565 swap_rows(tab, row, tab->n_row - 1);
566 isl_tab_var_from_row(tab, tab->n_row - 1)->index = -1;
572 struct isl_tab *isl_tab_mark_empty(struct isl_tab *tab)
574 if (!tab->empty && tab->need_undo)
575 isl_tab_push(tab, isl_tab_undo_empty);
580 /* Update the rows signs after a pivot of "row" and "col", with "row_sgn"
581 * the original sign of the pivot element.
582 * We only keep track of row signs during PILP solving and in this case
583 * we only pivot a row with negative sign (meaning the value is always
584 * non-positive) using a positive pivot element.
586 * For each row j, the new value of the parametric constant is equal to
588 * a_j0 - a_jc a_r0/a_rc
590 * where a_j0 is the original parametric constant, a_rc is the pivot element,
591 * a_r0 is the parametric constant of the pivot row and a_jc is the
592 * pivot column entry of the row j.
593 * Since a_r0 is non-positive and a_rc is positive, the sign of row j
594 * remains the same if a_jc has the same sign as the row j or if
595 * a_jc is zero. In all other cases, we reset the sign to "unknown".
597 static void update_row_sign(struct isl_tab *tab, int row, int col, int row_sgn)
600 struct isl_mat *mat = tab->mat;
601 unsigned off = 2 + tab->M;
606 if (tab->row_sign[row] == 0)
608 isl_assert(mat->ctx, row_sgn > 0, return);
609 isl_assert(mat->ctx, tab->row_sign[row] == isl_tab_row_neg, return);
610 tab->row_sign[row] = isl_tab_row_pos;
611 for (i = 0; i < tab->n_row; ++i) {
615 s = isl_int_sgn(mat->row[i][off + col]);
618 if (!tab->row_sign[i])
620 if (s < 0 && tab->row_sign[i] == isl_tab_row_neg)
622 if (s > 0 && tab->row_sign[i] == isl_tab_row_pos)
624 tab->row_sign[i] = isl_tab_row_unknown;
628 /* Given a row number "row" and a column number "col", pivot the tableau
629 * such that the associated variables are interchanged.
630 * The given row in the tableau expresses
632 * x_r = a_r0 + \sum_i a_ri x_i
636 * x_c = 1/a_rc x_r - a_r0/a_rc + sum_{i \ne r} -a_ri/a_rc
638 * Substituting this equality into the other rows
640 * x_j = a_j0 + \sum_i a_ji x_i
642 * with a_jc \ne 0, we obtain
644 * 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
651 * where i is any other column and j is any other row,
652 * is therefore transformed into
654 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
655 * 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)
657 * The transformation is performed along the following steps
662 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
665 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
666 * n_jc/(|n_rc| d_j) n_ji/(|n_rc| d_j)
668 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
669 * n_jc/(|n_rc| d_j) (n_ji |n_rc|)/(|n_rc| d_j)
671 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
672 * n_jc/(|n_rc| d_j) (n_ji |n_rc| - s(n_rc)n_jc n_ri)/(|n_rc| d_j)
674 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
675 * 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)
678 void isl_tab_pivot(struct isl_tab *tab, int row, int col)
683 struct isl_mat *mat = tab->mat;
684 struct isl_tab_var *var;
685 unsigned off = 2 + tab->M;
687 isl_int_swap(mat->row[row][0], mat->row[row][off + col]);
688 sgn = isl_int_sgn(mat->row[row][0]);
690 isl_int_neg(mat->row[row][0], mat->row[row][0]);
691 isl_int_neg(mat->row[row][off + col], mat->row[row][off + col]);
693 for (j = 0; j < off - 1 + tab->n_col; ++j) {
694 if (j == off - 1 + col)
696 isl_int_neg(mat->row[row][1 + j], mat->row[row][1 + j]);
698 if (!isl_int_is_one(mat->row[row][0]))
699 isl_seq_normalize(mat->ctx, mat->row[row], off + tab->n_col);
700 for (i = 0; i < tab->n_row; ++i) {
703 if (isl_int_is_zero(mat->row[i][off + col]))
705 isl_int_mul(mat->row[i][0], mat->row[i][0], mat->row[row][0]);
706 for (j = 0; j < off - 1 + tab->n_col; ++j) {
707 if (j == off - 1 + col)
709 isl_int_mul(mat->row[i][1 + j],
710 mat->row[i][1 + j], mat->row[row][0]);
711 isl_int_addmul(mat->row[i][1 + j],
712 mat->row[i][off + col], mat->row[row][1 + j]);
714 isl_int_mul(mat->row[i][off + col],
715 mat->row[i][off + col], mat->row[row][off + col]);
716 if (!isl_int_is_one(mat->row[i][0]))
717 isl_seq_normalize(mat->ctx, mat->row[i], off + tab->n_col);
719 t = tab->row_var[row];
720 tab->row_var[row] = tab->col_var[col];
721 tab->col_var[col] = t;
722 var = isl_tab_var_from_row(tab, row);
725 var = var_from_col(tab, col);
728 update_row_sign(tab, row, col, sgn);
731 for (i = tab->n_redundant; i < tab->n_row; ++i) {
732 if (isl_int_is_zero(mat->row[i][off + col]))
734 if (!isl_tab_var_from_row(tab, i)->frozen &&
735 isl_tab_row_is_redundant(tab, i))
736 if (isl_tab_mark_redundant(tab, i))
741 /* If "var" represents a column variable, then pivot is up (sgn > 0)
742 * or down (sgn < 0) to a row. The variable is assumed not to be
743 * unbounded in the specified direction.
744 * If sgn = 0, then the variable is unbounded in both directions,
745 * and we pivot with any row we can find.
747 static void to_row(struct isl_tab *tab, struct isl_tab_var *var, int sign)
750 unsigned off = 2 + tab->M;
756 for (r = tab->n_redundant; r < tab->n_row; ++r)
757 if (!isl_int_is_zero(tab->mat->row[r][off+var->index]))
759 isl_assert(tab->mat->ctx, r < tab->n_row, return);
761 r = pivot_row(tab, NULL, sign, var->index);
762 isl_assert(tab->mat->ctx, r >= 0, return);
765 isl_tab_pivot(tab, r, var->index);
768 static void check_table(struct isl_tab *tab)
774 for (i = 0; i < tab->n_row; ++i) {
775 if (!isl_tab_var_from_row(tab, i)->is_nonneg)
777 assert(!isl_int_is_neg(tab->mat->row[i][1]));
781 /* Return the sign of the maximal value of "var".
782 * If the sign is not negative, then on return from this function,
783 * the sample value will also be non-negative.
785 * If "var" is manifestly unbounded wrt positive values, we are done.
786 * Otherwise, we pivot the variable up to a row if needed
787 * Then we continue pivoting down until either
788 * - no more down pivots can be performed
789 * - the sample value is positive
790 * - the variable is pivoted into a manifestly unbounded column
792 static int sign_of_max(struct isl_tab *tab, struct isl_tab_var *var)
796 if (max_is_manifestly_unbounded(tab, var))
799 while (!isl_int_is_pos(tab->mat->row[var->index][1])) {
800 find_pivot(tab, var, var, 1, &row, &col);
802 return isl_int_sgn(tab->mat->row[var->index][1]);
803 isl_tab_pivot(tab, row, col);
804 if (!var->is_row) /* manifestly unbounded */
810 static int row_is_neg(struct isl_tab *tab, int row)
813 return isl_int_is_neg(tab->mat->row[row][1]);
814 if (isl_int_is_pos(tab->mat->row[row][2]))
816 if (isl_int_is_neg(tab->mat->row[row][2]))
818 return isl_int_is_neg(tab->mat->row[row][1]);
821 static int row_sgn(struct isl_tab *tab, int row)
824 return isl_int_sgn(tab->mat->row[row][1]);
825 if (!isl_int_is_zero(tab->mat->row[row][2]))
826 return isl_int_sgn(tab->mat->row[row][2]);
828 return isl_int_sgn(tab->mat->row[row][1]);
831 /* Perform pivots until the row variable "var" has a non-negative
832 * sample value or until no more upward pivots can be performed.
833 * Return the sign of the sample value after the pivots have been
836 static int restore_row(struct isl_tab *tab, struct isl_tab_var *var)
840 while (row_is_neg(tab, var->index)) {
841 find_pivot(tab, var, var, 1, &row, &col);
844 isl_tab_pivot(tab, row, col);
845 if (!var->is_row) /* manifestly unbounded */
848 return row_sgn(tab, var->index);
851 /* Perform pivots until we are sure that the row variable "var"
852 * can attain non-negative values. After return from this
853 * function, "var" is still a row variable, but its sample
854 * value may not be non-negative, even if the function returns 1.
856 static int at_least_zero(struct isl_tab *tab, struct isl_tab_var *var)
860 while (isl_int_is_neg(tab->mat->row[var->index][1])) {
861 find_pivot(tab, var, var, 1, &row, &col);
864 if (row == var->index) /* manifestly unbounded */
866 isl_tab_pivot(tab, row, col);
868 return !isl_int_is_neg(tab->mat->row[var->index][1]);
871 /* Return a negative value if "var" can attain negative values.
872 * Return a non-negative value otherwise.
874 * If "var" is manifestly unbounded wrt negative values, we are done.
875 * Otherwise, if var is in a column, we can pivot it down to a row.
876 * Then we continue pivoting down until either
877 * - the pivot would result in a manifestly unbounded column
878 * => we don't perform the pivot, but simply return -1
879 * - no more down pivots can be performed
880 * - the sample value is negative
881 * If the sample value becomes negative and the variable is supposed
882 * to be nonnegative, then we undo the last pivot.
883 * However, if the last pivot has made the pivoting variable
884 * obviously redundant, then it may have moved to another row.
885 * In that case we look for upward pivots until we reach a non-negative
888 static int sign_of_min(struct isl_tab *tab, struct isl_tab_var *var)
891 struct isl_tab_var *pivot_var = NULL;
893 if (min_is_manifestly_unbounded(tab, var))
897 row = pivot_row(tab, NULL, -1, col);
898 pivot_var = var_from_col(tab, col);
899 isl_tab_pivot(tab, row, col);
900 if (var->is_redundant)
902 if (isl_int_is_neg(tab->mat->row[var->index][1])) {
903 if (var->is_nonneg) {
904 if (!pivot_var->is_redundant &&
905 pivot_var->index == row)
906 isl_tab_pivot(tab, row, col);
908 restore_row(tab, var);
913 if (var->is_redundant)
915 while (!isl_int_is_neg(tab->mat->row[var->index][1])) {
916 find_pivot(tab, var, var, -1, &row, &col);
917 if (row == var->index)
920 return isl_int_sgn(tab->mat->row[var->index][1]);
921 pivot_var = var_from_col(tab, col);
922 isl_tab_pivot(tab, row, col);
923 if (var->is_redundant)
926 if (pivot_var && var->is_nonneg) {
927 /* pivot back to non-negative value */
928 if (!pivot_var->is_redundant && pivot_var->index == row)
929 isl_tab_pivot(tab, row, col);
931 restore_row(tab, var);
936 static int row_at_most_neg_one(struct isl_tab *tab, int row)
939 if (isl_int_is_pos(tab->mat->row[row][2]))
941 if (isl_int_is_neg(tab->mat->row[row][2]))
944 return isl_int_is_neg(tab->mat->row[row][1]) &&
945 isl_int_abs_ge(tab->mat->row[row][1],
946 tab->mat->row[row][0]);
949 /* Return 1 if "var" can attain values <= -1.
950 * Return 0 otherwise.
952 * The sample value of "var" is assumed to be non-negative when the
953 * the function is called and will be made non-negative again before
954 * the function returns.
956 int isl_tab_min_at_most_neg_one(struct isl_tab *tab, struct isl_tab_var *var)
959 struct isl_tab_var *pivot_var;
961 if (min_is_manifestly_unbounded(tab, var))
965 row = pivot_row(tab, NULL, -1, col);
966 pivot_var = var_from_col(tab, col);
967 isl_tab_pivot(tab, row, col);
968 if (var->is_redundant)
970 if (row_at_most_neg_one(tab, var->index)) {
971 if (var->is_nonneg) {
972 if (!pivot_var->is_redundant &&
973 pivot_var->index == row)
974 isl_tab_pivot(tab, row, col);
976 restore_row(tab, var);
981 if (var->is_redundant)
984 find_pivot(tab, var, var, -1, &row, &col);
985 if (row == var->index)
989 pivot_var = var_from_col(tab, col);
990 isl_tab_pivot(tab, row, col);
991 if (var->is_redundant)
993 } while (!row_at_most_neg_one(tab, var->index));
994 if (var->is_nonneg) {
995 /* pivot back to non-negative value */
996 if (!pivot_var->is_redundant && pivot_var->index == row)
997 isl_tab_pivot(tab, row, col);
998 restore_row(tab, var);
1003 /* Return 1 if "var" can attain values >= 1.
1004 * Return 0 otherwise.
1006 static int at_least_one(struct isl_tab *tab, struct isl_tab_var *var)
1011 if (max_is_manifestly_unbounded(tab, var))
1013 to_row(tab, var, 1);
1014 r = tab->mat->row[var->index];
1015 while (isl_int_lt(r[1], r[0])) {
1016 find_pivot(tab, var, var, 1, &row, &col);
1018 return isl_int_ge(r[1], r[0]);
1019 if (row == var->index) /* manifestly unbounded */
1021 isl_tab_pivot(tab, row, col);
1026 static void swap_cols(struct isl_tab *tab, int col1, int col2)
1029 unsigned off = 2 + tab->M;
1030 t = tab->col_var[col1];
1031 tab->col_var[col1] = tab->col_var[col2];
1032 tab->col_var[col2] = t;
1033 var_from_col(tab, col1)->index = col1;
1034 var_from_col(tab, col2)->index = col2;
1035 tab->mat = isl_mat_swap_cols(tab->mat, off + col1, off + col2);
1038 /* Mark column with index "col" as representing a zero variable.
1039 * If we may need to undo the operation the column is kept,
1040 * but no longer considered.
1041 * Otherwise, the column is simply removed.
1043 * The column may be interchanged with some other column. If it
1044 * is interchanged with a later column, return 1. Otherwise return 0.
1045 * If the columns are checked in order in the calling function,
1046 * then a return value of 1 means that the column with the given
1047 * column number may now contain a different column that
1048 * hasn't been checked yet.
1050 int isl_tab_kill_col(struct isl_tab *tab, int col)
1052 var_from_col(tab, col)->is_zero = 1;
1053 if (tab->need_undo) {
1054 isl_tab_push_var(tab, isl_tab_undo_zero, var_from_col(tab, col));
1055 if (col != tab->n_dead)
1056 swap_cols(tab, col, tab->n_dead);
1060 if (col != tab->n_col - 1)
1061 swap_cols(tab, col, tab->n_col - 1);
1062 var_from_col(tab, tab->n_col - 1)->index = -1;
1068 /* Row variable "var" is non-negative and cannot attain any values
1069 * larger than zero. This means that the coefficients of the unrestricted
1070 * column variables are zero and that the coefficients of the non-negative
1071 * column variables are zero or negative.
1072 * Each of the non-negative variables with a negative coefficient can
1073 * then also be written as the negative sum of non-negative variables
1074 * and must therefore also be zero.
1076 static void close_row(struct isl_tab *tab, struct isl_tab_var *var)
1079 struct isl_mat *mat = tab->mat;
1080 unsigned off = 2 + tab->M;
1082 isl_assert(tab->mat->ctx, var->is_nonneg, return);
1085 isl_tab_push_var(tab, isl_tab_undo_zero, var);
1086 for (j = tab->n_dead; j < tab->n_col; ++j) {
1087 if (isl_int_is_zero(mat->row[var->index][off + j]))
1089 isl_assert(tab->mat->ctx,
1090 isl_int_is_neg(mat->row[var->index][off + j]), return);
1091 if (isl_tab_kill_col(tab, j))
1094 isl_tab_mark_redundant(tab, var->index);
1097 /* Add a constraint to the tableau and allocate a row for it.
1098 * Return the index into the constraint array "con".
1100 int isl_tab_allocate_con(struct isl_tab *tab)
1104 isl_assert(tab->mat->ctx, tab->n_row < tab->mat->n_row, return -1);
1105 isl_assert(tab->mat->ctx, tab->n_con < tab->max_con, return -1);
1108 tab->con[r].index = tab->n_row;
1109 tab->con[r].is_row = 1;
1110 tab->con[r].is_nonneg = 0;
1111 tab->con[r].is_zero = 0;
1112 tab->con[r].is_redundant = 0;
1113 tab->con[r].frozen = 0;
1114 tab->con[r].negated = 0;
1115 tab->row_var[tab->n_row] = ~r;
1119 isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->con[r]);
1124 /* Add a variable to the tableau and allocate a column for it.
1125 * Return the index into the variable array "var".
1127 int isl_tab_allocate_var(struct isl_tab *tab)
1131 unsigned off = 2 + tab->M;
1133 isl_assert(tab->mat->ctx, tab->n_col < tab->mat->n_col, return -1);
1134 isl_assert(tab->mat->ctx, tab->n_var < tab->max_var, return -1);
1137 tab->var[r].index = tab->n_col;
1138 tab->var[r].is_row = 0;
1139 tab->var[r].is_nonneg = 0;
1140 tab->var[r].is_zero = 0;
1141 tab->var[r].is_redundant = 0;
1142 tab->var[r].frozen = 0;
1143 tab->var[r].negated = 0;
1144 tab->col_var[tab->n_col] = r;
1146 for (i = 0; i < tab->n_row; ++i)
1147 isl_int_set_si(tab->mat->row[i][off + tab->n_col], 0);
1151 isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->var[r]);
1156 /* Add a row to the tableau. The row is given as an affine combination
1157 * of the original variables and needs to be expressed in terms of the
1160 * We add each term in turn.
1161 * If r = n/d_r is the current sum and we need to add k x, then
1162 * if x is a column variable, we increase the numerator of
1163 * this column by k d_r
1164 * if x = f/d_x is a row variable, then the new representation of r is
1166 * n k f d_x/g n + d_r/g k f m/d_r n + m/d_g k f
1167 * --- + --- = ------------------- = -------------------
1168 * d_r d_r d_r d_x/g m
1170 * with g the gcd of d_r and d_x and m the lcm of d_r and d_x.
1172 int isl_tab_add_row(struct isl_tab *tab, isl_int *line)
1178 unsigned off = 2 + tab->M;
1180 r = isl_tab_allocate_con(tab);
1186 row = tab->mat->row[tab->con[r].index];
1187 isl_int_set_si(row[0], 1);
1188 isl_int_set(row[1], line[0]);
1189 isl_seq_clr(row + 2, tab->M + tab->n_col);
1190 for (i = 0; i < tab->n_var; ++i) {
1191 if (tab->var[i].is_zero)
1193 if (tab->var[i].is_row) {
1195 row[0], tab->mat->row[tab->var[i].index][0]);
1196 isl_int_swap(a, row[0]);
1197 isl_int_divexact(a, row[0], a);
1199 row[0], tab->mat->row[tab->var[i].index][0]);
1200 isl_int_mul(b, b, line[1 + i]);
1201 isl_seq_combine(row + 1, a, row + 1,
1202 b, tab->mat->row[tab->var[i].index] + 1,
1203 1 + tab->M + tab->n_col);
1205 isl_int_addmul(row[off + tab->var[i].index],
1206 line[1 + i], row[0]);
1207 if (tab->M && i >= tab->n_param && i < tab->n_var - tab->n_div)
1208 isl_int_submul(row[2], line[1 + i], row[0]);
1210 isl_seq_normalize(tab->mat->ctx, row, off + tab->n_col);
1215 tab->row_sign[tab->con[r].index] = 0;
1220 static int drop_row(struct isl_tab *tab, int row)
1222 isl_assert(tab->mat->ctx, ~tab->row_var[row] == tab->n_con - 1, return -1);
1223 if (row != tab->n_row - 1)
1224 swap_rows(tab, row, tab->n_row - 1);
1230 static int drop_col(struct isl_tab *tab, int col)
1232 isl_assert(tab->mat->ctx, tab->col_var[col] == tab->n_var - 1, return -1);
1233 if (col != tab->n_col - 1)
1234 swap_cols(tab, col, tab->n_col - 1);
1240 /* Add inequality "ineq" and check if it conflicts with the
1241 * previously added constraints or if it is obviously redundant.
1243 struct isl_tab *isl_tab_add_ineq(struct isl_tab *tab, isl_int *ineq)
1250 r = isl_tab_add_row(tab, ineq);
1253 tab->con[r].is_nonneg = 1;
1254 isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]);
1255 if (isl_tab_row_is_redundant(tab, tab->con[r].index)) {
1256 isl_tab_mark_redundant(tab, tab->con[r].index);
1260 sgn = restore_row(tab, &tab->con[r]);
1262 return isl_tab_mark_empty(tab);
1263 if (tab->con[r].is_row && isl_tab_row_is_redundant(tab, tab->con[r].index))
1264 isl_tab_mark_redundant(tab, tab->con[r].index);
1271 /* Pivot a non-negative variable down until it reaches the value zero
1272 * and then pivot the variable into a column position.
1274 static int to_col(struct isl_tab *tab, struct isl_tab_var *var)
1278 unsigned off = 2 + tab->M;
1283 while (isl_int_is_pos(tab->mat->row[var->index][1])) {
1284 find_pivot(tab, var, NULL, -1, &row, &col);
1285 isl_assert(tab->mat->ctx, row != -1, return -1);
1286 isl_tab_pivot(tab, row, col);
1291 for (i = tab->n_dead; i < tab->n_col; ++i)
1292 if (!isl_int_is_zero(tab->mat->row[var->index][off + i]))
1295 isl_assert(tab->mat->ctx, i < tab->n_col, return -1);
1296 isl_tab_pivot(tab, var->index, i);
1301 /* We assume Gaussian elimination has been performed on the equalities.
1302 * The equalities can therefore never conflict.
1303 * Adding the equalities is currently only really useful for a later call
1304 * to isl_tab_ineq_type.
1306 static struct isl_tab *add_eq(struct isl_tab *tab, isl_int *eq)
1313 r = isl_tab_add_row(tab, eq);
1317 r = tab->con[r].index;
1318 i = isl_seq_first_non_zero(tab->mat->row[r] + 2 + tab->M + tab->n_dead,
1319 tab->n_col - tab->n_dead);
1320 isl_assert(tab->mat->ctx, i >= 0, goto error);
1322 isl_tab_pivot(tab, r, i);
1323 isl_tab_kill_col(tab, i);
1332 static int row_is_manifestly_zero(struct isl_tab *tab, int row)
1334 unsigned off = 2 + tab->M;
1336 if (!isl_int_is_zero(tab->mat->row[row][1]))
1338 if (tab->M && !isl_int_is_zero(tab->mat->row[row][2]))
1340 return isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
1341 tab->n_col - tab->n_dead) == -1;
1344 /* Add an equality that is known to be valid for the given tableau.
1346 struct isl_tab *isl_tab_add_valid_eq(struct isl_tab *tab, isl_int *eq)
1348 struct isl_tab_var *var;
1353 r = isl_tab_add_row(tab, eq);
1359 if (row_is_manifestly_zero(tab, r)) {
1361 isl_tab_mark_redundant(tab, r);
1365 if (isl_int_is_neg(tab->mat->row[r][1])) {
1366 isl_seq_neg(tab->mat->row[r] + 1, tab->mat->row[r] + 1,
1371 if (to_col(tab, var) < 0)
1374 isl_tab_kill_col(tab, var->index);
1382 /* Add equality "eq" and check if it conflicts with the
1383 * previously added constraints or if it is obviously redundant.
1385 struct isl_tab *isl_tab_add_eq(struct isl_tab *tab, isl_int *eq)
1387 struct isl_tab_undo *snap = NULL;
1388 struct isl_tab_var *var;
1395 isl_assert(tab->mat->ctx, !tab->M, goto error);
1398 snap = isl_tab_snap(tab);
1400 r = isl_tab_add_row(tab, eq);
1406 if (row_is_manifestly_zero(tab, row)) {
1408 if (isl_tab_rollback(tab, snap) < 0)
1415 sgn = isl_int_sgn(tab->mat->row[row][1]);
1418 isl_seq_neg(tab->mat->row[row] + 1, tab->mat->row[row] + 1,
1424 if (sgn < 0 && sign_of_max(tab, var) < 0)
1425 return isl_tab_mark_empty(tab);
1428 if (to_col(tab, var) < 0)
1431 isl_tab_kill_col(tab, var->index);
1439 struct isl_tab *isl_tab_from_basic_map(struct isl_basic_map *bmap)
1442 struct isl_tab *tab;
1446 tab = isl_tab_alloc(bmap->ctx,
1447 isl_basic_map_total_dim(bmap) + bmap->n_ineq + 1,
1448 isl_basic_map_total_dim(bmap), 0);
1451 tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
1452 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1453 return isl_tab_mark_empty(tab);
1454 for (i = 0; i < bmap->n_eq; ++i) {
1455 tab = add_eq(tab, bmap->eq[i]);
1459 for (i = 0; i < bmap->n_ineq; ++i) {
1460 tab = isl_tab_add_ineq(tab, bmap->ineq[i]);
1461 if (!tab || tab->empty)
1467 struct isl_tab *isl_tab_from_basic_set(struct isl_basic_set *bset)
1469 return isl_tab_from_basic_map((struct isl_basic_map *)bset);
1472 /* Construct a tableau corresponding to the recession cone of "bset".
1474 struct isl_tab *isl_tab_from_recession_cone(struct isl_basic_set *bset)
1478 struct isl_tab *tab;
1482 tab = isl_tab_alloc(bset->ctx, bset->n_eq + bset->n_ineq,
1483 isl_basic_set_total_dim(bset), 0);
1486 tab->rational = ISL_F_ISSET(bset, ISL_BASIC_SET_RATIONAL);
1489 for (i = 0; i < bset->n_eq; ++i) {
1490 isl_int_swap(bset->eq[i][0], cst);
1491 tab = add_eq(tab, bset->eq[i]);
1492 isl_int_swap(bset->eq[i][0], cst);
1496 for (i = 0; i < bset->n_ineq; ++i) {
1498 isl_int_swap(bset->ineq[i][0], cst);
1499 r = isl_tab_add_row(tab, bset->ineq[i]);
1500 isl_int_swap(bset->ineq[i][0], cst);
1503 tab->con[r].is_nonneg = 1;
1504 isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]);
1515 /* Assuming "tab" is the tableau of a cone, check if the cone is
1516 * bounded, i.e., if it is empty or only contains the origin.
1518 int isl_tab_cone_is_bounded(struct isl_tab *tab)
1526 if (tab->n_dead == tab->n_col)
1530 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1531 struct isl_tab_var *var;
1532 var = isl_tab_var_from_row(tab, i);
1533 if (!var->is_nonneg)
1535 if (sign_of_max(tab, var) != 0)
1537 close_row(tab, var);
1540 if (tab->n_dead == tab->n_col)
1542 if (i == tab->n_row)
1547 int isl_tab_sample_is_integer(struct isl_tab *tab)
1554 for (i = 0; i < tab->n_var; ++i) {
1556 if (!tab->var[i].is_row)
1558 row = tab->var[i].index;
1559 if (!isl_int_is_divisible_by(tab->mat->row[row][1],
1560 tab->mat->row[row][0]))
1566 static struct isl_vec *extract_integer_sample(struct isl_tab *tab)
1569 struct isl_vec *vec;
1571 vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
1575 isl_int_set_si(vec->block.data[0], 1);
1576 for (i = 0; i < tab->n_var; ++i) {
1577 if (!tab->var[i].is_row)
1578 isl_int_set_si(vec->block.data[1 + i], 0);
1580 int row = tab->var[i].index;
1581 isl_int_divexact(vec->block.data[1 + i],
1582 tab->mat->row[row][1], tab->mat->row[row][0]);
1589 struct isl_vec *isl_tab_get_sample_value(struct isl_tab *tab)
1592 struct isl_vec *vec;
1598 vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
1604 isl_int_set_si(vec->block.data[0], 1);
1605 for (i = 0; i < tab->n_var; ++i) {
1607 if (!tab->var[i].is_row) {
1608 isl_int_set_si(vec->block.data[1 + i], 0);
1611 row = tab->var[i].index;
1612 isl_int_gcd(m, vec->block.data[0], tab->mat->row[row][0]);
1613 isl_int_divexact(m, tab->mat->row[row][0], m);
1614 isl_seq_scale(vec->block.data, vec->block.data, m, 1 + i);
1615 isl_int_divexact(m, vec->block.data[0], tab->mat->row[row][0]);
1616 isl_int_mul(vec->block.data[1 + i], m, tab->mat->row[row][1]);
1618 vec = isl_vec_normalize(vec);
1624 /* Update "bmap" based on the results of the tableau "tab".
1625 * In particular, implicit equalities are made explicit, redundant constraints
1626 * are removed and if the sample value happens to be integer, it is stored
1627 * in "bmap" (unless "bmap" already had an integer sample).
1629 * The tableau is assumed to have been created from "bmap" using
1630 * isl_tab_from_basic_map.
1632 struct isl_basic_map *isl_basic_map_update_from_tab(struct isl_basic_map *bmap,
1633 struct isl_tab *tab)
1645 bmap = isl_basic_map_set_to_empty(bmap);
1647 for (i = bmap->n_ineq - 1; i >= 0; --i) {
1648 if (isl_tab_is_equality(tab, n_eq + i))
1649 isl_basic_map_inequality_to_equality(bmap, i);
1650 else if (isl_tab_is_redundant(tab, n_eq + i))
1651 isl_basic_map_drop_inequality(bmap, i);
1653 if (!tab->rational &&
1654 !bmap->sample && isl_tab_sample_is_integer(tab))
1655 bmap->sample = extract_integer_sample(tab);
1659 struct isl_basic_set *isl_basic_set_update_from_tab(struct isl_basic_set *bset,
1660 struct isl_tab *tab)
1662 return (struct isl_basic_set *)isl_basic_map_update_from_tab(
1663 (struct isl_basic_map *)bset, tab);
1666 /* Given a non-negative variable "var", add a new non-negative variable
1667 * that is the opposite of "var", ensuring that var can only attain the
1669 * If var = n/d is a row variable, then the new variable = -n/d.
1670 * If var is a column variables, then the new variable = -var.
1671 * If the new variable cannot attain non-negative values, then
1672 * the resulting tableau is empty.
1673 * Otherwise, we know the value will be zero and we close the row.
1675 static struct isl_tab *cut_to_hyperplane(struct isl_tab *tab,
1676 struct isl_tab_var *var)
1681 unsigned off = 2 + tab->M;
1685 isl_assert(tab->mat->ctx, !var->is_redundant, goto error);
1687 if (isl_tab_extend_cons(tab, 1) < 0)
1691 tab->con[r].index = tab->n_row;
1692 tab->con[r].is_row = 1;
1693 tab->con[r].is_nonneg = 0;
1694 tab->con[r].is_zero = 0;
1695 tab->con[r].is_redundant = 0;
1696 tab->con[r].frozen = 0;
1697 tab->con[r].negated = 0;
1698 tab->row_var[tab->n_row] = ~r;
1699 row = tab->mat->row[tab->n_row];
1702 isl_int_set(row[0], tab->mat->row[var->index][0]);
1703 isl_seq_neg(row + 1,
1704 tab->mat->row[var->index] + 1, 1 + tab->n_col);
1706 isl_int_set_si(row[0], 1);
1707 isl_seq_clr(row + 1, 1 + tab->n_col);
1708 isl_int_set_si(row[off + var->index], -1);
1713 isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->con[r]);
1715 sgn = sign_of_max(tab, &tab->con[r]);
1717 return isl_tab_mark_empty(tab);
1718 tab->con[r].is_nonneg = 1;
1719 isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]);
1721 close_row(tab, &tab->con[r]);
1729 /* Given a tableau "tab" and an inequality constraint "con" of the tableau,
1730 * relax the inequality by one. That is, the inequality r >= 0 is replaced
1731 * by r' = r + 1 >= 0.
1732 * If r is a row variable, we simply increase the constant term by one
1733 * (taking into account the denominator).
1734 * If r is a column variable, then we need to modify each row that
1735 * refers to r = r' - 1 by substituting this equality, effectively
1736 * subtracting the coefficient of the column from the constant.
1738 struct isl_tab *isl_tab_relax(struct isl_tab *tab, int con)
1740 struct isl_tab_var *var;
1741 unsigned off = 2 + tab->M;
1746 var = &tab->con[con];
1748 if (!var->is_row && !max_is_manifestly_unbounded(tab, var))
1749 to_row(tab, var, 1);
1752 isl_int_add(tab->mat->row[var->index][1],
1753 tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
1757 for (i = 0; i < tab->n_row; ++i) {
1758 if (isl_int_is_zero(tab->mat->row[i][off + var->index]))
1760 isl_int_sub(tab->mat->row[i][1], tab->mat->row[i][1],
1761 tab->mat->row[i][off + var->index]);
1766 isl_tab_push_var(tab, isl_tab_undo_relax, var);
1771 struct isl_tab *isl_tab_select_facet(struct isl_tab *tab, int con)
1776 return cut_to_hyperplane(tab, &tab->con[con]);
1779 static int may_be_equality(struct isl_tab *tab, int row)
1781 unsigned off = 2 + tab->M;
1782 return (tab->rational ? isl_int_is_zero(tab->mat->row[row][1])
1783 : isl_int_lt(tab->mat->row[row][1],
1784 tab->mat->row[row][0])) &&
1785 isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
1786 tab->n_col - tab->n_dead) != -1;
1789 /* Check for (near) equalities among the constraints.
1790 * A constraint is an equality if it is non-negative and if
1791 * its maximal value is either
1792 * - zero (in case of rational tableaus), or
1793 * - strictly less than 1 (in case of integer tableaus)
1795 * We first mark all non-redundant and non-dead variables that
1796 * are not frozen and not obviously not an equality.
1797 * Then we iterate over all marked variables if they can attain
1798 * any values larger than zero or at least one.
1799 * If the maximal value is zero, we mark any column variables
1800 * that appear in the row as being zero and mark the row as being redundant.
1801 * Otherwise, if the maximal value is strictly less than one (and the
1802 * tableau is integer), then we restrict the value to being zero
1803 * by adding an opposite non-negative variable.
1805 struct isl_tab *isl_tab_detect_implicit_equalities(struct isl_tab *tab)
1814 if (tab->n_dead == tab->n_col)
1818 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1819 struct isl_tab_var *var = isl_tab_var_from_row(tab, i);
1820 var->marked = !var->frozen && var->is_nonneg &&
1821 may_be_equality(tab, i);
1825 for (i = tab->n_dead; i < tab->n_col; ++i) {
1826 struct isl_tab_var *var = var_from_col(tab, i);
1827 var->marked = !var->frozen && var->is_nonneg;
1832 struct isl_tab_var *var;
1833 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1834 var = isl_tab_var_from_row(tab, i);
1838 if (i == tab->n_row) {
1839 for (i = tab->n_dead; i < tab->n_col; ++i) {
1840 var = var_from_col(tab, i);
1844 if (i == tab->n_col)
1849 if (sign_of_max(tab, var) == 0)
1850 close_row(tab, var);
1851 else if (!tab->rational && !at_least_one(tab, var)) {
1852 tab = cut_to_hyperplane(tab, var);
1853 return isl_tab_detect_implicit_equalities(tab);
1855 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1856 var = isl_tab_var_from_row(tab, i);
1859 if (may_be_equality(tab, i))
1869 /* Check for (near) redundant constraints.
1870 * A constraint is redundant if it is non-negative and if
1871 * its minimal value (temporarily ignoring the non-negativity) is either
1872 * - zero (in case of rational tableaus), or
1873 * - strictly larger than -1 (in case of integer tableaus)
1875 * We first mark all non-redundant and non-dead variables that
1876 * are not frozen and not obviously negatively unbounded.
1877 * Then we iterate over all marked variables if they can attain
1878 * any values smaller than zero or at most negative one.
1879 * If not, we mark the row as being redundant (assuming it hasn't
1880 * been detected as being obviously redundant in the mean time).
1882 struct isl_tab *isl_tab_detect_redundant(struct isl_tab *tab)
1891 if (tab->n_redundant == tab->n_row)
1895 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1896 struct isl_tab_var *var = isl_tab_var_from_row(tab, i);
1897 var->marked = !var->frozen && var->is_nonneg;
1901 for (i = tab->n_dead; i < tab->n_col; ++i) {
1902 struct isl_tab_var *var = var_from_col(tab, i);
1903 var->marked = !var->frozen && var->is_nonneg &&
1904 !min_is_manifestly_unbounded(tab, var);
1909 struct isl_tab_var *var;
1910 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1911 var = isl_tab_var_from_row(tab, i);
1915 if (i == tab->n_row) {
1916 for (i = tab->n_dead; i < tab->n_col; ++i) {
1917 var = var_from_col(tab, i);
1921 if (i == tab->n_col)
1926 if ((tab->rational ? (sign_of_min(tab, var) >= 0)
1927 : !isl_tab_min_at_most_neg_one(tab, var)) &&
1929 isl_tab_mark_redundant(tab, var->index);
1930 for (i = tab->n_dead; i < tab->n_col; ++i) {
1931 var = var_from_col(tab, i);
1934 if (!min_is_manifestly_unbounded(tab, var))
1944 int isl_tab_is_equality(struct isl_tab *tab, int con)
1951 if (tab->con[con].is_zero)
1953 if (tab->con[con].is_redundant)
1955 if (!tab->con[con].is_row)
1956 return tab->con[con].index < tab->n_dead;
1958 row = tab->con[con].index;
1961 return isl_int_is_zero(tab->mat->row[row][1]) &&
1962 isl_seq_first_non_zero(tab->mat->row[row] + 2 + tab->n_dead,
1963 tab->n_col - tab->n_dead) == -1;
1966 /* Return the minimial value of the affine expression "f" with denominator
1967 * "denom" in *opt, *opt_denom, assuming the tableau is not empty and
1968 * the expression cannot attain arbitrarily small values.
1969 * If opt_denom is NULL, then *opt is rounded up to the nearest integer.
1970 * The return value reflects the nature of the result (empty, unbounded,
1971 * minmimal value returned in *opt).
1973 enum isl_lp_result isl_tab_min(struct isl_tab *tab,
1974 isl_int *f, isl_int denom, isl_int *opt, isl_int *opt_denom,
1978 enum isl_lp_result res = isl_lp_ok;
1979 struct isl_tab_var *var;
1980 struct isl_tab_undo *snap;
1983 return isl_lp_empty;
1985 snap = isl_tab_snap(tab);
1986 r = isl_tab_add_row(tab, f);
1988 return isl_lp_error;
1990 isl_int_mul(tab->mat->row[var->index][0],
1991 tab->mat->row[var->index][0], denom);
1994 find_pivot(tab, var, var, -1, &row, &col);
1995 if (row == var->index) {
1996 res = isl_lp_unbounded;
2001 isl_tab_pivot(tab, row, col);
2003 if (ISL_FL_ISSET(flags, ISL_TAB_SAVE_DUAL)) {
2006 isl_vec_free(tab->dual);
2007 tab->dual = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_con);
2009 return isl_lp_error;
2010 isl_int_set(tab->dual->el[0], tab->mat->row[var->index][0]);
2011 for (i = 0; i < tab->n_con; ++i) {
2013 if (tab->con[i].is_row) {
2014 isl_int_set_si(tab->dual->el[1 + i], 0);
2017 pos = 2 + tab->M + tab->con[i].index;
2018 if (tab->con[i].negated)
2019 isl_int_neg(tab->dual->el[1 + i],
2020 tab->mat->row[var->index][pos]);
2022 isl_int_set(tab->dual->el[1 + i],
2023 tab->mat->row[var->index][pos]);
2026 if (opt && res == isl_lp_ok) {
2028 isl_int_set(*opt, tab->mat->row[var->index][1]);
2029 isl_int_set(*opt_denom, tab->mat->row[var->index][0]);
2031 isl_int_cdiv_q(*opt, tab->mat->row[var->index][1],
2032 tab->mat->row[var->index][0]);
2034 if (isl_tab_rollback(tab, snap) < 0)
2035 return isl_lp_error;
2039 int isl_tab_is_redundant(struct isl_tab *tab, int con)
2043 if (tab->con[con].is_zero)
2045 if (tab->con[con].is_redundant)
2047 return tab->con[con].is_row && tab->con[con].index < tab->n_redundant;
2050 /* Take a snapshot of the tableau that can be restored by s call to
2053 struct isl_tab_undo *isl_tab_snap(struct isl_tab *tab)
2061 /* Undo the operation performed by isl_tab_relax.
2063 static void unrelax(struct isl_tab *tab, struct isl_tab_var *var)
2065 unsigned off = 2 + tab->M;
2067 if (!var->is_row && !max_is_manifestly_unbounded(tab, var))
2068 to_row(tab, var, 1);
2071 isl_int_sub(tab->mat->row[var->index][1],
2072 tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
2076 for (i = 0; i < tab->n_row; ++i) {
2077 if (isl_int_is_zero(tab->mat->row[i][off + var->index]))
2079 isl_int_add(tab->mat->row[i][1], tab->mat->row[i][1],
2080 tab->mat->row[i][off + var->index]);
2086 static void perform_undo_var(struct isl_tab *tab, struct isl_tab_undo *undo)
2088 struct isl_tab_var *var = var_from_index(tab, undo->u.var_index);
2089 switch(undo->type) {
2090 case isl_tab_undo_nonneg:
2093 case isl_tab_undo_redundant:
2094 var->is_redundant = 0;
2097 case isl_tab_undo_zero:
2102 case isl_tab_undo_allocate:
2103 if (undo->u.var_index >= 0) {
2104 isl_assert(tab->mat->ctx, !var->is_row, return);
2105 drop_col(tab, var->index);
2109 if (!max_is_manifestly_unbounded(tab, var))
2110 to_row(tab, var, 1);
2111 else if (!min_is_manifestly_unbounded(tab, var))
2112 to_row(tab, var, -1);
2114 to_row(tab, var, 0);
2116 drop_row(tab, var->index);
2118 case isl_tab_undo_relax:
2124 /* Restore the tableau to the state where the basic variables
2125 * are those in "col_var".
2126 * We first construct a list of variables that are currently in
2127 * the basis, but shouldn't. Then we iterate over all variables
2128 * that should be in the basis and for each one that is currently
2129 * not in the basis, we exchange it with one of the elements of the
2130 * list constructed before.
2131 * We can always find an appropriate variable to pivot with because
2132 * the current basis is mapped to the old basis by a non-singular
2133 * matrix and so we can never end up with a zero row.
2135 static int restore_basis(struct isl_tab *tab, int *col_var)
2139 int *extra = NULL; /* current columns that contain bad stuff */
2140 unsigned off = 2 + tab->M;
2142 extra = isl_alloc_array(tab->mat->ctx, int, tab->n_col);
2145 for (i = 0; i < tab->n_col; ++i) {
2146 for (j = 0; j < tab->n_col; ++j)
2147 if (tab->col_var[i] == col_var[j])
2151 extra[n_extra++] = i;
2153 for (i = 0; i < tab->n_col && n_extra > 0; ++i) {
2154 struct isl_tab_var *var;
2157 for (j = 0; j < tab->n_col; ++j)
2158 if (col_var[i] == tab->col_var[j])
2162 var = var_from_index(tab, col_var[i]);
2164 for (j = 0; j < n_extra; ++j)
2165 if (!isl_int_is_zero(tab->mat->row[row][off+extra[j]]))
2167 isl_assert(tab->mat->ctx, j < n_extra, goto error);
2168 isl_tab_pivot(tab, row, extra[j]);
2169 extra[j] = extra[--n_extra];
2181 static int perform_undo(struct isl_tab *tab, struct isl_tab_undo *undo)
2183 switch (undo->type) {
2184 case isl_tab_undo_empty:
2187 case isl_tab_undo_nonneg:
2188 case isl_tab_undo_redundant:
2189 case isl_tab_undo_zero:
2190 case isl_tab_undo_allocate:
2191 case isl_tab_undo_relax:
2192 perform_undo_var(tab, undo);
2194 case isl_tab_undo_bset_eq:
2195 isl_basic_set_free_equality(tab->bset, 1);
2197 case isl_tab_undo_bset_ineq:
2198 isl_basic_set_free_inequality(tab->bset, 1);
2200 case isl_tab_undo_bset_div:
2201 isl_basic_set_free_div(tab->bset, 1);
2203 tab->samples->n_col--;
2205 case isl_tab_undo_saved_basis:
2206 if (restore_basis(tab, undo->u.col_var) < 0)
2209 case isl_tab_undo_drop_sample:
2213 isl_assert(tab->mat->ctx, 0, return -1);
2218 /* Return the tableau to the state it was in when the snapshot "snap"
2221 int isl_tab_rollback(struct isl_tab *tab, struct isl_tab_undo *snap)
2223 struct isl_tab_undo *undo, *next;
2229 for (undo = tab->top; undo && undo != &tab->bottom; undo = next) {
2233 if (perform_undo(tab, undo) < 0) {
2247 /* The given row "row" represents an inequality violated by all
2248 * points in the tableau. Check for some special cases of such
2249 * separating constraints.
2250 * In particular, if the row has been reduced to the constant -1,
2251 * then we know the inequality is adjacent (but opposite) to
2252 * an equality in the tableau.
2253 * If the row has been reduced to r = -1 -r', with r' an inequality
2254 * of the tableau, then the inequality is adjacent (but opposite)
2255 * to the inequality r'.
2257 static enum isl_ineq_type separation_type(struct isl_tab *tab, unsigned row)
2260 unsigned off = 2 + tab->M;
2263 return isl_ineq_separate;
2265 if (!isl_int_is_one(tab->mat->row[row][0]))
2266 return isl_ineq_separate;
2267 if (!isl_int_is_negone(tab->mat->row[row][1]))
2268 return isl_ineq_separate;
2270 pos = isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
2271 tab->n_col - tab->n_dead);
2273 return isl_ineq_adj_eq;
2275 if (!isl_int_is_negone(tab->mat->row[row][off + tab->n_dead + pos]))
2276 return isl_ineq_separate;
2278 pos = isl_seq_first_non_zero(
2279 tab->mat->row[row] + off + tab->n_dead + pos + 1,
2280 tab->n_col - tab->n_dead - pos - 1);
2282 return pos == -1 ? isl_ineq_adj_ineq : isl_ineq_separate;
2285 /* Check the effect of inequality "ineq" on the tableau "tab".
2287 * isl_ineq_redundant: satisfied by all points in the tableau
2288 * isl_ineq_separate: satisfied by no point in the tableau
2289 * isl_ineq_cut: satisfied by some by not all points
2290 * isl_ineq_adj_eq: adjacent to an equality
2291 * isl_ineq_adj_ineq: adjacent to an inequality.
2293 enum isl_ineq_type isl_tab_ineq_type(struct isl_tab *tab, isl_int *ineq)
2295 enum isl_ineq_type type = isl_ineq_error;
2296 struct isl_tab_undo *snap = NULL;
2301 return isl_ineq_error;
2303 if (isl_tab_extend_cons(tab, 1) < 0)
2304 return isl_ineq_error;
2306 snap = isl_tab_snap(tab);
2308 con = isl_tab_add_row(tab, ineq);
2312 row = tab->con[con].index;
2313 if (isl_tab_row_is_redundant(tab, row))
2314 type = isl_ineq_redundant;
2315 else if (isl_int_is_neg(tab->mat->row[row][1]) &&
2317 isl_int_abs_ge(tab->mat->row[row][1],
2318 tab->mat->row[row][0]))) {
2319 if (at_least_zero(tab, &tab->con[con]))
2320 type = isl_ineq_cut;
2322 type = separation_type(tab, row);
2323 } else if (tab->rational ? (sign_of_min(tab, &tab->con[con]) < 0)
2324 : isl_tab_min_at_most_neg_one(tab, &tab->con[con]))
2325 type = isl_ineq_cut;
2327 type = isl_ineq_redundant;
2329 if (isl_tab_rollback(tab, snap))
2330 return isl_ineq_error;
2333 isl_tab_rollback(tab, snap);
2334 return isl_ineq_error;
2337 void isl_tab_dump(struct isl_tab *tab, FILE *out, int indent)
2343 fprintf(out, "%*snull tab\n", indent, "");
2346 fprintf(out, "%*sn_redundant: %d, n_dead: %d", indent, "",
2347 tab->n_redundant, tab->n_dead);
2349 fprintf(out, ", rational");
2351 fprintf(out, ", empty");
2353 fprintf(out, "%*s[", indent, "");
2354 for (i = 0; i < tab->n_var; ++i) {
2356 fprintf(out, (i == tab->n_param ||
2357 i == tab->n_var - tab->n_div) ? "; "
2359 fprintf(out, "%c%d%s", tab->var[i].is_row ? 'r' : 'c',
2361 tab->var[i].is_zero ? " [=0]" :
2362 tab->var[i].is_redundant ? " [R]" : "");
2364 fprintf(out, "]\n");
2365 fprintf(out, "%*s[", indent, "");
2366 for (i = 0; i < tab->n_con; ++i) {
2369 fprintf(out, "%c%d%s", tab->con[i].is_row ? 'r' : 'c',
2371 tab->con[i].is_zero ? " [=0]" :
2372 tab->con[i].is_redundant ? " [R]" : "");
2374 fprintf(out, "]\n");
2375 fprintf(out, "%*s[", indent, "");
2376 for (i = 0; i < tab->n_row; ++i) {
2377 const char *sign = "";
2380 if (tab->row_sign) {
2381 if (tab->row_sign[i] == isl_tab_row_unknown)
2383 else if (tab->row_sign[i] == isl_tab_row_neg)
2385 else if (tab->row_sign[i] == isl_tab_row_pos)
2390 fprintf(out, "r%d: %d%s%s", i, tab->row_var[i],
2391 isl_tab_var_from_row(tab, i)->is_nonneg ? " [>=0]" : "", sign);
2393 fprintf(out, "]\n");
2394 fprintf(out, "%*s[", indent, "");
2395 for (i = 0; i < tab->n_col; ++i) {
2398 fprintf(out, "c%d: %d%s", i, tab->col_var[i],
2399 var_from_col(tab, i)->is_nonneg ? " [>=0]" : "");
2401 fprintf(out, "]\n");
2402 r = tab->mat->n_row;
2403 tab->mat->n_row = tab->n_row;
2404 c = tab->mat->n_col;
2405 tab->mat->n_col = 2 + tab->M + tab->n_col;
2406 isl_mat_dump(tab->mat, out, indent);
2407 tab->mat->n_row = r;
2408 tab->mat->n_col = c;
2410 isl_basic_set_dump(tab->bset, out, indent);