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;
75 if (tab->max_con < tab->n_con + n_new) {
76 struct isl_tab_var *con;
78 con = isl_realloc_array(tab->mat->ctx, tab->con,
79 struct isl_tab_var, tab->max_con + n_new);
83 tab->max_con += n_new;
85 if (tab->mat->n_row < tab->n_row + n_new) {
88 tab->mat = isl_mat_extend(tab->mat,
89 tab->n_row + n_new, off + tab->n_col);
92 row_var = isl_realloc_array(tab->mat->ctx, tab->row_var,
93 int, tab->mat->n_row);
96 tab->row_var = row_var;
98 enum isl_tab_row_sign *s;
99 s = isl_realloc_array(tab->mat->ctx, tab->row_sign,
100 enum isl_tab_row_sign, tab->mat->n_row);
109 /* Make room for at least n_new extra variables.
110 * Return -1 if anything went wrong.
112 int isl_tab_extend_vars(struct isl_tab *tab, unsigned n_new)
114 struct isl_tab_var *var;
115 unsigned off = 2 + tab->M;
117 if (tab->max_var < tab->n_var + n_new) {
118 var = isl_realloc_array(tab->mat->ctx, tab->var,
119 struct isl_tab_var, tab->n_var + n_new);
123 tab->max_var += n_new;
126 if (tab->mat->n_col < off + tab->n_col + n_new) {
129 tab->mat = isl_mat_extend(tab->mat,
130 tab->mat->n_row, off + tab->n_col + n_new);
133 p = isl_realloc_array(tab->mat->ctx, tab->col_var,
134 int, tab->mat->n_col);
143 struct isl_tab *isl_tab_extend(struct isl_tab *tab, unsigned n_new)
145 if (isl_tab_extend_cons(tab, n_new) >= 0)
152 static void free_undo(struct isl_tab *tab)
154 struct isl_tab_undo *undo, *next;
156 for (undo = tab->top; undo && undo != &tab->bottom; undo = next) {
163 void isl_tab_free(struct isl_tab *tab)
168 isl_mat_free(tab->mat);
169 isl_vec_free(tab->dual);
170 isl_basic_set_free(tab->bset);
176 isl_mat_free(tab->samples);
180 struct isl_tab *isl_tab_dup(struct isl_tab *tab)
188 dup = isl_calloc_type(tab->ctx, struct isl_tab);
191 dup->mat = isl_mat_dup(tab->mat);
194 dup->var = isl_alloc_array(tab->ctx, struct isl_tab_var, tab->max_var);
197 for (i = 0; i < tab->n_var; ++i)
198 dup->var[i] = tab->var[i];
199 dup->con = isl_alloc_array(tab->ctx, struct isl_tab_var, tab->max_con);
202 for (i = 0; i < tab->n_con; ++i)
203 dup->con[i] = tab->con[i];
204 dup->col_var = isl_alloc_array(tab->ctx, int, tab->mat->n_col);
207 for (i = 0; i < tab->n_var; ++i)
208 dup->col_var[i] = tab->col_var[i];
209 dup->row_var = isl_alloc_array(tab->ctx, int, tab->mat->n_row);
212 for (i = 0; i < tab->n_row; ++i)
213 dup->row_var[i] = tab->row_var[i];
215 dup->row_sign = isl_alloc_array(tab->ctx, enum isl_tab_row_sign,
219 for (i = 0; i < tab->n_row; ++i)
220 dup->row_sign[i] = tab->row_sign[i];
223 dup->samples = isl_mat_dup(tab->samples);
226 dup->n_sample = tab->n_sample;
227 dup->n_outside = tab->n_outside;
229 dup->n_row = tab->n_row;
230 dup->n_con = tab->n_con;
231 dup->n_eq = tab->n_eq;
232 dup->max_con = tab->max_con;
233 dup->n_col = tab->n_col;
234 dup->n_var = tab->n_var;
235 dup->max_var = tab->max_var;
236 dup->n_param = tab->n_param;
237 dup->n_div = tab->n_div;
238 dup->n_dead = tab->n_dead;
239 dup->n_redundant = tab->n_redundant;
240 dup->rational = tab->rational;
241 dup->empty = tab->empty;
245 dup->bottom.type = isl_tab_undo_bottom;
246 dup->bottom.next = NULL;
247 dup->top = &dup->bottom;
254 static struct isl_tab_var *var_from_index(struct isl_tab *tab, int i)
259 return &tab->con[~i];
262 struct isl_tab_var *isl_tab_var_from_row(struct isl_tab *tab, int i)
264 return var_from_index(tab, tab->row_var[i]);
267 static struct isl_tab_var *var_from_col(struct isl_tab *tab, int i)
269 return var_from_index(tab, tab->col_var[i]);
272 /* Check if there are any upper bounds on column variable "var",
273 * i.e., non-negative rows where var appears with a negative coefficient.
274 * Return 1 if there are no such bounds.
276 static int max_is_manifestly_unbounded(struct isl_tab *tab,
277 struct isl_tab_var *var)
280 unsigned off = 2 + tab->M;
284 for (i = tab->n_redundant; i < tab->n_row; ++i) {
285 if (!isl_int_is_neg(tab->mat->row[i][off + var->index]))
287 if (isl_tab_var_from_row(tab, i)->is_nonneg)
293 /* Check if there are any lower bounds on column variable "var",
294 * i.e., non-negative rows where var appears with a positive coefficient.
295 * Return 1 if there are no such bounds.
297 static int min_is_manifestly_unbounded(struct isl_tab *tab,
298 struct isl_tab_var *var)
301 unsigned off = 2 + tab->M;
305 for (i = tab->n_redundant; i < tab->n_row; ++i) {
306 if (!isl_int_is_pos(tab->mat->row[i][off + var->index]))
308 if (isl_tab_var_from_row(tab, i)->is_nonneg)
314 static int row_cmp(struct isl_tab *tab, int r1, int r2, int c, isl_int t)
316 unsigned off = 2 + tab->M;
320 isl_int_mul(t, tab->mat->row[r1][2], tab->mat->row[r2][off+c]);
321 isl_int_submul(t, tab->mat->row[r2][2], tab->mat->row[r1][off+c]);
326 isl_int_mul(t, tab->mat->row[r1][1], tab->mat->row[r2][off + c]);
327 isl_int_submul(t, tab->mat->row[r2][1], tab->mat->row[r1][off + c]);
328 return isl_int_sgn(t);
331 /* Given the index of a column "c", return the index of a row
332 * that can be used to pivot the column in, with either an increase
333 * (sgn > 0) or a decrease (sgn < 0) of the corresponding variable.
334 * If "var" is not NULL, then the row returned will be different from
335 * the one associated with "var".
337 * Each row in the tableau is of the form
339 * x_r = a_r0 + \sum_i a_ri x_i
341 * Only rows with x_r >= 0 and with the sign of a_ri opposite to "sgn"
342 * impose any limit on the increase or decrease in the value of x_c
343 * and this bound is equal to a_r0 / |a_rc|. We are therefore looking
344 * for the row with the smallest (most stringent) such bound.
345 * Note that the common denominator of each row drops out of the fraction.
346 * To check if row j has a smaller bound than row r, i.e.,
347 * a_j0 / |a_jc| < a_r0 / |a_rc| or a_j0 |a_rc| < a_r0 |a_jc|,
348 * we check if -sign(a_jc) (a_j0 a_rc - a_r0 a_jc) < 0,
349 * where -sign(a_jc) is equal to "sgn".
351 static int pivot_row(struct isl_tab *tab,
352 struct isl_tab_var *var, int sgn, int c)
356 unsigned off = 2 + tab->M;
360 for (j = tab->n_redundant; j < tab->n_row; ++j) {
361 if (var && j == var->index)
363 if (!isl_tab_var_from_row(tab, j)->is_nonneg)
365 if (sgn * isl_int_sgn(tab->mat->row[j][off + c]) >= 0)
371 tsgn = sgn * row_cmp(tab, r, j, c, t);
372 if (tsgn < 0 || (tsgn == 0 &&
373 tab->row_var[j] < tab->row_var[r]))
380 /* Find a pivot (row and col) that will increase (sgn > 0) or decrease
381 * (sgn < 0) the value of row variable var.
382 * If not NULL, then skip_var is a row variable that should be ignored
383 * while looking for a pivot row. It is usually equal to var.
385 * As the given row in the tableau is of the form
387 * x_r = a_r0 + \sum_i a_ri x_i
389 * we need to find a column such that the sign of a_ri is equal to "sgn"
390 * (such that an increase in x_i will have the desired effect) or a
391 * column with a variable that may attain negative values.
392 * If a_ri is positive, then we need to move x_i in the same direction
393 * to obtain the desired effect. Otherwise, x_i has to move in the
394 * opposite direction.
396 static void find_pivot(struct isl_tab *tab,
397 struct isl_tab_var *var, struct isl_tab_var *skip_var,
398 int sgn, int *row, int *col)
405 isl_assert(tab->mat->ctx, var->is_row, return);
406 tr = tab->mat->row[var->index] + 2 + tab->M;
409 for (j = tab->n_dead; j < tab->n_col; ++j) {
410 if (isl_int_is_zero(tr[j]))
412 if (isl_int_sgn(tr[j]) != sgn &&
413 var_from_col(tab, j)->is_nonneg)
415 if (c < 0 || tab->col_var[j] < tab->col_var[c])
421 sgn *= isl_int_sgn(tr[c]);
422 r = pivot_row(tab, skip_var, sgn, c);
423 *row = r < 0 ? var->index : r;
427 /* Return 1 if row "row" represents an obviously redundant inequality.
429 * - it represents an inequality or a variable
430 * - that is the sum of a non-negative sample value and a positive
431 * combination of zero or more non-negative variables.
433 int isl_tab_row_is_redundant(struct isl_tab *tab, int row)
436 unsigned off = 2 + tab->M;
438 if (tab->row_var[row] < 0 && !isl_tab_var_from_row(tab, row)->is_nonneg)
441 if (isl_int_is_neg(tab->mat->row[row][1]))
443 if (tab->M && isl_int_is_neg(tab->mat->row[row][2]))
446 for (i = tab->n_dead; i < tab->n_col; ++i) {
447 if (isl_int_is_zero(tab->mat->row[row][off + i]))
449 if (isl_int_is_neg(tab->mat->row[row][off + i]))
451 if (!var_from_col(tab, i)->is_nonneg)
457 static void swap_rows(struct isl_tab *tab, int row1, int row2)
460 t = tab->row_var[row1];
461 tab->row_var[row1] = tab->row_var[row2];
462 tab->row_var[row2] = t;
463 isl_tab_var_from_row(tab, row1)->index = row1;
464 isl_tab_var_from_row(tab, row2)->index = row2;
465 tab->mat = isl_mat_swap_rows(tab->mat, row1, row2);
469 t = tab->row_sign[row1];
470 tab->row_sign[row1] = tab->row_sign[row2];
471 tab->row_sign[row2] = t;
474 static void push_union(struct isl_tab *tab,
475 enum isl_tab_undo_type type, union isl_tab_undo_val u)
477 struct isl_tab_undo *undo;
482 undo = isl_alloc_type(tab->mat->ctx, struct isl_tab_undo);
490 undo->next = tab->top;
494 void isl_tab_push_var(struct isl_tab *tab,
495 enum isl_tab_undo_type type, struct isl_tab_var *var)
497 union isl_tab_undo_val u;
499 u.var_index = tab->row_var[var->index];
501 u.var_index = tab->col_var[var->index];
502 push_union(tab, type, u);
505 void isl_tab_push(struct isl_tab *tab, enum isl_tab_undo_type type)
507 union isl_tab_undo_val u = { 0 };
508 push_union(tab, type, u);
511 /* Push a record on the undo stack describing the current basic
512 * variables, so that the this state can be restored during rollback.
514 void isl_tab_push_basis(struct isl_tab *tab)
517 union isl_tab_undo_val u;
519 u.col_var = isl_alloc_array(tab->mat->ctx, int, tab->n_col);
525 for (i = 0; i < tab->n_col; ++i)
526 u.col_var[i] = tab->col_var[i];
527 push_union(tab, isl_tab_undo_saved_basis, u);
530 /* Mark row with index "row" as being redundant.
531 * If we may need to undo the operation or if the row represents
532 * a variable of the original problem, the row is kept,
533 * but no longer considered when looking for a pivot row.
534 * Otherwise, the row is simply removed.
536 * The row may be interchanged with some other row. If it
537 * is interchanged with a later row, return 1. Otherwise return 0.
538 * If the rows are checked in order in the calling function,
539 * then a return value of 1 means that the row with the given
540 * row number may now contain a different row that hasn't been checked yet.
542 int isl_tab_mark_redundant(struct isl_tab *tab, int row)
544 struct isl_tab_var *var = isl_tab_var_from_row(tab, row);
545 var->is_redundant = 1;
546 isl_assert(tab->mat->ctx, row >= tab->n_redundant, return);
547 if (tab->need_undo || tab->row_var[row] >= 0) {
548 if (tab->row_var[row] >= 0 && !var->is_nonneg) {
550 isl_tab_push_var(tab, isl_tab_undo_nonneg, var);
552 if (row != tab->n_redundant)
553 swap_rows(tab, row, tab->n_redundant);
554 isl_tab_push_var(tab, isl_tab_undo_redundant, var);
558 if (row != tab->n_row - 1)
559 swap_rows(tab, row, tab->n_row - 1);
560 isl_tab_var_from_row(tab, tab->n_row - 1)->index = -1;
566 struct isl_tab *isl_tab_mark_empty(struct isl_tab *tab)
568 if (!tab->empty && tab->need_undo)
569 isl_tab_push(tab, isl_tab_undo_empty);
574 /* Update the rows signs after a pivot of "row" and "col", with "row_sgn"
575 * the original sign of the pivot element.
576 * We only keep track of row signs during PILP solving and in this case
577 * we only pivot a row with negative sign (meaning the value is always
578 * non-positive) using a positive pivot element.
580 * For each row j, the new value of the parametric constant is equal to
582 * a_j0 - a_jc a_r0/a_rc
584 * where a_j0 is the original parametric constant, a_rc is the pivot element,
585 * a_r0 is the parametric constant of the pivot row and a_jc is the
586 * pivot column entry of the row j.
587 * Since a_r0 is non-positive and a_rc is positive, the sign of row j
588 * remains the same if a_jc has the same sign as the row j or if
589 * a_jc is zero. In all other cases, we reset the sign to "unknown".
591 static void update_row_sign(struct isl_tab *tab, int row, int col, int row_sgn)
594 struct isl_mat *mat = tab->mat;
595 unsigned off = 2 + tab->M;
600 if (tab->row_sign[row] == 0)
602 isl_assert(mat->ctx, row_sgn > 0, return);
603 isl_assert(mat->ctx, tab->row_sign[row] == isl_tab_row_neg, return);
604 tab->row_sign[row] = isl_tab_row_pos;
605 for (i = 0; i < tab->n_row; ++i) {
609 s = isl_int_sgn(mat->row[i][off + col]);
612 if (!tab->row_sign[i])
614 if (s < 0 && tab->row_sign[i] == isl_tab_row_neg)
616 if (s > 0 && tab->row_sign[i] == isl_tab_row_pos)
618 tab->row_sign[i] = isl_tab_row_unknown;
622 /* Given a row number "row" and a column number "col", pivot the tableau
623 * such that the associated variables are interchanged.
624 * The given row in the tableau expresses
626 * x_r = a_r0 + \sum_i a_ri x_i
630 * x_c = 1/a_rc x_r - a_r0/a_rc + sum_{i \ne r} -a_ri/a_rc
632 * Substituting this equality into the other rows
634 * x_j = a_j0 + \sum_i a_ji x_i
636 * with a_jc \ne 0, we obtain
638 * 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
645 * where i is any other column and j is any other row,
646 * is therefore transformed into
648 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
649 * 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)
651 * The transformation is performed along the following steps
656 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
659 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
660 * n_jc/(|n_rc| d_j) n_ji/(|n_rc| d_j)
662 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
663 * n_jc/(|n_rc| d_j) (n_ji |n_rc|)/(|n_rc| d_j)
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| - s(n_rc)n_jc n_ri)/(|n_rc| d_j)
668 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
669 * 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)
672 void isl_tab_pivot(struct isl_tab *tab, int row, int col)
677 struct isl_mat *mat = tab->mat;
678 struct isl_tab_var *var;
679 unsigned off = 2 + tab->M;
681 isl_int_swap(mat->row[row][0], mat->row[row][off + col]);
682 sgn = isl_int_sgn(mat->row[row][0]);
684 isl_int_neg(mat->row[row][0], mat->row[row][0]);
685 isl_int_neg(mat->row[row][off + col], mat->row[row][off + col]);
687 for (j = 0; j < off - 1 + tab->n_col; ++j) {
688 if (j == off - 1 + col)
690 isl_int_neg(mat->row[row][1 + j], mat->row[row][1 + j]);
692 if (!isl_int_is_one(mat->row[row][0]))
693 isl_seq_normalize(mat->ctx, mat->row[row], off + tab->n_col);
694 for (i = 0; i < tab->n_row; ++i) {
697 if (isl_int_is_zero(mat->row[i][off + col]))
699 isl_int_mul(mat->row[i][0], mat->row[i][0], mat->row[row][0]);
700 for (j = 0; j < off - 1 + tab->n_col; ++j) {
701 if (j == off - 1 + col)
703 isl_int_mul(mat->row[i][1 + j],
704 mat->row[i][1 + j], mat->row[row][0]);
705 isl_int_addmul(mat->row[i][1 + j],
706 mat->row[i][off + col], mat->row[row][1 + j]);
708 isl_int_mul(mat->row[i][off + col],
709 mat->row[i][off + col], mat->row[row][off + col]);
710 if (!isl_int_is_one(mat->row[i][0]))
711 isl_seq_normalize(mat->ctx, mat->row[i], off + tab->n_col);
713 t = tab->row_var[row];
714 tab->row_var[row] = tab->col_var[col];
715 tab->col_var[col] = t;
716 var = isl_tab_var_from_row(tab, row);
719 var = var_from_col(tab, col);
722 update_row_sign(tab, row, col, sgn);
725 for (i = tab->n_redundant; i < tab->n_row; ++i) {
726 if (isl_int_is_zero(mat->row[i][off + col]))
728 if (!isl_tab_var_from_row(tab, i)->frozen &&
729 isl_tab_row_is_redundant(tab, i))
730 if (isl_tab_mark_redundant(tab, i))
735 /* If "var" represents a column variable, then pivot is up (sgn > 0)
736 * or down (sgn < 0) to a row. The variable is assumed not to be
737 * unbounded in the specified direction.
738 * If sgn = 0, then the variable is unbounded in both directions,
739 * and we pivot with any row we can find.
741 static void to_row(struct isl_tab *tab, struct isl_tab_var *var, int sign)
744 unsigned off = 2 + tab->M;
750 for (r = tab->n_redundant; r < tab->n_row; ++r)
751 if (!isl_int_is_zero(tab->mat->row[r][off+var->index]))
753 isl_assert(tab->mat->ctx, r < tab->n_row, return);
755 r = pivot_row(tab, NULL, sign, var->index);
756 isl_assert(tab->mat->ctx, r >= 0, return);
759 isl_tab_pivot(tab, r, var->index);
762 static void check_table(struct isl_tab *tab)
768 for (i = 0; i < tab->n_row; ++i) {
769 if (!isl_tab_var_from_row(tab, i)->is_nonneg)
771 assert(!isl_int_is_neg(tab->mat->row[i][1]));
775 /* Return the sign of the maximal value of "var".
776 * If the sign is not negative, then on return from this function,
777 * the sample value will also be non-negative.
779 * If "var" is manifestly unbounded wrt positive values, we are done.
780 * Otherwise, we pivot the variable up to a row if needed
781 * Then we continue pivoting down until either
782 * - no more down pivots can be performed
783 * - the sample value is positive
784 * - the variable is pivoted into a manifestly unbounded column
786 static int sign_of_max(struct isl_tab *tab, struct isl_tab_var *var)
790 if (max_is_manifestly_unbounded(tab, var))
793 while (!isl_int_is_pos(tab->mat->row[var->index][1])) {
794 find_pivot(tab, var, var, 1, &row, &col);
796 return isl_int_sgn(tab->mat->row[var->index][1]);
797 isl_tab_pivot(tab, row, col);
798 if (!var->is_row) /* manifestly unbounded */
804 static int row_is_neg(struct isl_tab *tab, int row)
807 return isl_int_is_neg(tab->mat->row[row][1]);
808 if (isl_int_is_pos(tab->mat->row[row][2]))
810 if (isl_int_is_neg(tab->mat->row[row][2]))
812 return isl_int_is_neg(tab->mat->row[row][1]);
815 static int row_sgn(struct isl_tab *tab, int row)
818 return isl_int_sgn(tab->mat->row[row][1]);
819 if (!isl_int_is_zero(tab->mat->row[row][2]))
820 return isl_int_sgn(tab->mat->row[row][2]);
822 return isl_int_sgn(tab->mat->row[row][1]);
825 /* Perform pivots until the row variable "var" has a non-negative
826 * sample value or until no more upward pivots can be performed.
827 * Return the sign of the sample value after the pivots have been
830 static int restore_row(struct isl_tab *tab, struct isl_tab_var *var)
834 while (row_is_neg(tab, var->index)) {
835 find_pivot(tab, var, var, 1, &row, &col);
838 isl_tab_pivot(tab, row, col);
839 if (!var->is_row) /* manifestly unbounded */
842 return row_sgn(tab, var->index);
845 /* Perform pivots until we are sure that the row variable "var"
846 * can attain non-negative values. After return from this
847 * function, "var" is still a row variable, but its sample
848 * value may not be non-negative, even if the function returns 1.
850 static int at_least_zero(struct isl_tab *tab, struct isl_tab_var *var)
854 while (isl_int_is_neg(tab->mat->row[var->index][1])) {
855 find_pivot(tab, var, var, 1, &row, &col);
858 if (row == var->index) /* manifestly unbounded */
860 isl_tab_pivot(tab, row, col);
862 return !isl_int_is_neg(tab->mat->row[var->index][1]);
865 /* Return a negative value if "var" can attain negative values.
866 * Return a non-negative value otherwise.
868 * If "var" is manifestly unbounded wrt negative values, we are done.
869 * Otherwise, if var is in a column, we can pivot it down to a row.
870 * Then we continue pivoting down until either
871 * - the pivot would result in a manifestly unbounded column
872 * => we don't perform the pivot, but simply return -1
873 * - no more down pivots can be performed
874 * - the sample value is negative
875 * If the sample value becomes negative and the variable is supposed
876 * to be nonnegative, then we undo the last pivot.
877 * However, if the last pivot has made the pivoting variable
878 * obviously redundant, then it may have moved to another row.
879 * In that case we look for upward pivots until we reach a non-negative
882 static int sign_of_min(struct isl_tab *tab, struct isl_tab_var *var)
885 struct isl_tab_var *pivot_var;
887 if (min_is_manifestly_unbounded(tab, var))
891 row = pivot_row(tab, NULL, -1, col);
892 pivot_var = var_from_col(tab, col);
893 isl_tab_pivot(tab, row, col);
894 if (var->is_redundant)
896 if (isl_int_is_neg(tab->mat->row[var->index][1])) {
897 if (var->is_nonneg) {
898 if (!pivot_var->is_redundant &&
899 pivot_var->index == row)
900 isl_tab_pivot(tab, row, col);
902 restore_row(tab, var);
907 if (var->is_redundant)
909 while (!isl_int_is_neg(tab->mat->row[var->index][1])) {
910 find_pivot(tab, var, var, -1, &row, &col);
911 if (row == var->index)
914 return isl_int_sgn(tab->mat->row[var->index][1]);
915 pivot_var = var_from_col(tab, col);
916 isl_tab_pivot(tab, row, col);
917 if (var->is_redundant)
920 if (var->is_nonneg) {
921 /* pivot back to non-negative value */
922 if (!pivot_var->is_redundant && pivot_var->index == row)
923 isl_tab_pivot(tab, row, col);
925 restore_row(tab, var);
930 static int row_at_most_neg_one(struct isl_tab *tab, int row)
933 if (isl_int_is_pos(tab->mat->row[row][2]))
935 if (isl_int_is_neg(tab->mat->row[row][2]))
938 return isl_int_is_neg(tab->mat->row[row][1]) &&
939 isl_int_abs_ge(tab->mat->row[row][1],
940 tab->mat->row[row][0]);
943 /* Return 1 if "var" can attain values <= -1.
944 * Return 0 otherwise.
946 * The sample value of "var" is assumed to be non-negative when the
947 * the function is called and will be made non-negative again before
948 * the function returns.
950 int isl_tab_min_at_most_neg_one(struct isl_tab *tab, struct isl_tab_var *var)
953 struct isl_tab_var *pivot_var;
955 if (min_is_manifestly_unbounded(tab, var))
959 row = pivot_row(tab, NULL, -1, col);
960 pivot_var = var_from_col(tab, col);
961 isl_tab_pivot(tab, row, col);
962 if (var->is_redundant)
964 if (row_at_most_neg_one(tab, var->index)) {
965 if (var->is_nonneg) {
966 if (!pivot_var->is_redundant &&
967 pivot_var->index == row)
968 isl_tab_pivot(tab, row, col);
970 restore_row(tab, var);
975 if (var->is_redundant)
978 find_pivot(tab, var, var, -1, &row, &col);
979 if (row == var->index)
983 pivot_var = var_from_col(tab, col);
984 isl_tab_pivot(tab, row, col);
985 if (var->is_redundant)
987 } while (!row_at_most_neg_one(tab, var->index));
988 if (var->is_nonneg) {
989 /* pivot back to non-negative value */
990 if (!pivot_var->is_redundant && pivot_var->index == row)
991 isl_tab_pivot(tab, row, col);
992 restore_row(tab, var);
997 /* Return 1 if "var" can attain values >= 1.
998 * Return 0 otherwise.
1000 static int at_least_one(struct isl_tab *tab, struct isl_tab_var *var)
1005 if (max_is_manifestly_unbounded(tab, var))
1007 to_row(tab, var, 1);
1008 r = tab->mat->row[var->index];
1009 while (isl_int_lt(r[1], r[0])) {
1010 find_pivot(tab, var, var, 1, &row, &col);
1012 return isl_int_ge(r[1], r[0]);
1013 if (row == var->index) /* manifestly unbounded */
1015 isl_tab_pivot(tab, row, col);
1020 static void swap_cols(struct isl_tab *tab, int col1, int col2)
1023 unsigned off = 2 + tab->M;
1024 t = tab->col_var[col1];
1025 tab->col_var[col1] = tab->col_var[col2];
1026 tab->col_var[col2] = t;
1027 var_from_col(tab, col1)->index = col1;
1028 var_from_col(tab, col2)->index = col2;
1029 tab->mat = isl_mat_swap_cols(tab->mat, off + col1, off + col2);
1032 /* Mark column with index "col" as representing a zero variable.
1033 * If we may need to undo the operation the column is kept,
1034 * but no longer considered.
1035 * Otherwise, the column is simply removed.
1037 * The column may be interchanged with some other column. If it
1038 * is interchanged with a later column, return 1. Otherwise return 0.
1039 * If the columns are checked in order in the calling function,
1040 * then a return value of 1 means that the column with the given
1041 * column number may now contain a different column that
1042 * hasn't been checked yet.
1044 int isl_tab_kill_col(struct isl_tab *tab, int col)
1046 var_from_col(tab, col)->is_zero = 1;
1047 if (tab->need_undo) {
1048 isl_tab_push_var(tab, isl_tab_undo_zero, var_from_col(tab, col));
1049 if (col != tab->n_dead)
1050 swap_cols(tab, col, tab->n_dead);
1054 if (col != tab->n_col - 1)
1055 swap_cols(tab, col, tab->n_col - 1);
1056 var_from_col(tab, tab->n_col - 1)->index = -1;
1062 /* Row variable "var" is non-negative and cannot attain any values
1063 * larger than zero. This means that the coefficients of the unrestricted
1064 * column variables are zero and that the coefficients of the non-negative
1065 * column variables are zero or negative.
1066 * Each of the non-negative variables with a negative coefficient can
1067 * then also be written as the negative sum of non-negative variables
1068 * and must therefore also be zero.
1070 static void close_row(struct isl_tab *tab, struct isl_tab_var *var)
1073 struct isl_mat *mat = tab->mat;
1074 unsigned off = 2 + tab->M;
1076 isl_assert(tab->mat->ctx, var->is_nonneg, return);
1078 for (j = tab->n_dead; j < tab->n_col; ++j) {
1079 if (isl_int_is_zero(mat->row[var->index][off + j]))
1081 isl_assert(tab->mat->ctx,
1082 isl_int_is_neg(mat->row[var->index][off + j]), return);
1083 if (isl_tab_kill_col(tab, j))
1086 isl_tab_mark_redundant(tab, var->index);
1089 /* Add a constraint to the tableau and allocate a row for it.
1090 * Return the index into the constraint array "con".
1092 int isl_tab_allocate_con(struct isl_tab *tab)
1096 isl_assert(tab->mat->ctx, tab->n_row < tab->mat->n_row, return -1);
1099 tab->con[r].index = tab->n_row;
1100 tab->con[r].is_row = 1;
1101 tab->con[r].is_nonneg = 0;
1102 tab->con[r].is_zero = 0;
1103 tab->con[r].is_redundant = 0;
1104 tab->con[r].frozen = 0;
1105 tab->con[r].negated = 0;
1106 tab->row_var[tab->n_row] = ~r;
1110 isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->con[r]);
1115 /* Add a variable to the tableau and allocate a column for it.
1116 * Return the index into the variable array "var".
1118 int isl_tab_allocate_var(struct isl_tab *tab)
1122 unsigned off = 2 + tab->M;
1124 isl_assert(tab->mat->ctx, tab->n_col < tab->mat->n_col, return -1);
1125 isl_assert(tab->mat->ctx, tab->n_var < tab->max_var, return -1);
1128 tab->var[r].index = tab->n_col;
1129 tab->var[r].is_row = 0;
1130 tab->var[r].is_nonneg = 0;
1131 tab->var[r].is_zero = 0;
1132 tab->var[r].is_redundant = 0;
1133 tab->var[r].frozen = 0;
1134 tab->var[r].negated = 0;
1135 tab->col_var[tab->n_col] = r;
1137 for (i = 0; i < tab->n_row; ++i)
1138 isl_int_set_si(tab->mat->row[i][off + tab->n_col], 0);
1142 isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->var[r]);
1147 /* Add a row to the tableau. The row is given as an affine combination
1148 * of the original variables and needs to be expressed in terms of the
1151 * We add each term in turn.
1152 * If r = n/d_r is the current sum and we need to add k x, then
1153 * if x is a column variable, we increase the numerator of
1154 * this column by k d_r
1155 * if x = f/d_x is a row variable, then the new representation of r is
1157 * n k f d_x/g n + d_r/g k f m/d_r n + m/d_g k f
1158 * --- + --- = ------------------- = -------------------
1159 * d_r d_r d_r d_x/g m
1161 * with g the gcd of d_r and d_x and m the lcm of d_r and d_x.
1163 int isl_tab_add_row(struct isl_tab *tab, isl_int *line)
1169 unsigned off = 2 + tab->M;
1171 r = isl_tab_allocate_con(tab);
1177 row = tab->mat->row[tab->con[r].index];
1178 isl_int_set_si(row[0], 1);
1179 isl_int_set(row[1], line[0]);
1180 isl_seq_clr(row + 2, tab->M + tab->n_col);
1181 for (i = 0; i < tab->n_var; ++i) {
1182 if (tab->var[i].is_zero)
1184 if (tab->var[i].is_row) {
1186 row[0], tab->mat->row[tab->var[i].index][0]);
1187 isl_int_swap(a, row[0]);
1188 isl_int_divexact(a, row[0], a);
1190 row[0], tab->mat->row[tab->var[i].index][0]);
1191 isl_int_mul(b, b, line[1 + i]);
1192 isl_seq_combine(row + 1, a, row + 1,
1193 b, tab->mat->row[tab->var[i].index] + 1,
1194 1 + tab->M + tab->n_col);
1196 isl_int_addmul(row[off + tab->var[i].index],
1197 line[1 + i], row[0]);
1198 if (tab->M && i >= tab->n_param && i < tab->n_var - tab->n_div)
1199 isl_int_submul(row[2], line[1 + i], row[0]);
1201 isl_seq_normalize(tab->mat->ctx, row, off + tab->n_col);
1206 tab->row_sign[tab->con[r].index] = 0;
1211 static int drop_row(struct isl_tab *tab, int row)
1213 isl_assert(tab->mat->ctx, ~tab->row_var[row] == tab->n_con - 1, return -1);
1214 if (row != tab->n_row - 1)
1215 swap_rows(tab, row, tab->n_row - 1);
1221 static int drop_col(struct isl_tab *tab, int col)
1223 isl_assert(tab->mat->ctx, tab->col_var[col] == tab->n_var - 1, return -1);
1224 if (col != tab->n_col - 1)
1225 swap_cols(tab, col, tab->n_col - 1);
1231 /* Add inequality "ineq" and check if it conflicts with the
1232 * previously added constraints or if it is obviously redundant.
1234 struct isl_tab *isl_tab_add_ineq(struct isl_tab *tab, isl_int *ineq)
1241 r = isl_tab_add_row(tab, ineq);
1244 tab->con[r].is_nonneg = 1;
1245 isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]);
1246 if (isl_tab_row_is_redundant(tab, tab->con[r].index)) {
1247 isl_tab_mark_redundant(tab, tab->con[r].index);
1251 sgn = restore_row(tab, &tab->con[r]);
1253 return isl_tab_mark_empty(tab);
1254 if (tab->con[r].is_row && isl_tab_row_is_redundant(tab, tab->con[r].index))
1255 isl_tab_mark_redundant(tab, tab->con[r].index);
1262 /* Pivot a non-negative variable down until it reaches the value zero
1263 * and then pivot the variable into a column position.
1265 static int to_col(struct isl_tab *tab, struct isl_tab_var *var)
1269 unsigned off = 2 + tab->M;
1274 while (isl_int_is_pos(tab->mat->row[var->index][1])) {
1275 find_pivot(tab, var, NULL, -1, &row, &col);
1276 isl_assert(tab->mat->ctx, row != -1, return -1);
1277 isl_tab_pivot(tab, row, col);
1282 for (i = tab->n_dead; i < tab->n_col; ++i)
1283 if (!isl_int_is_zero(tab->mat->row[var->index][off + i]))
1286 isl_assert(tab->mat->ctx, i < tab->n_col, return -1);
1287 isl_tab_pivot(tab, var->index, i);
1292 /* We assume Gaussian elimination has been performed on the equalities.
1293 * The equalities can therefore never conflict.
1294 * Adding the equalities is currently only really useful for a later call
1295 * to isl_tab_ineq_type.
1297 static struct isl_tab *add_eq(struct isl_tab *tab, isl_int *eq)
1304 r = isl_tab_add_row(tab, eq);
1308 r = tab->con[r].index;
1309 i = isl_seq_first_non_zero(tab->mat->row[r] + 2 + tab->M + tab->n_dead,
1310 tab->n_col - tab->n_dead);
1311 isl_assert(tab->mat->ctx, i >= 0, goto error);
1313 isl_tab_pivot(tab, r, i);
1314 isl_tab_kill_col(tab, i);
1323 /* Add an equality that is known to be valid for the given tableau.
1325 struct isl_tab *isl_tab_add_valid_eq(struct isl_tab *tab, isl_int *eq)
1327 struct isl_tab_var *var;
1332 r = isl_tab_add_row(tab, eq);
1338 if (isl_int_is_neg(tab->mat->row[r][1])) {
1339 isl_seq_neg(tab->mat->row[r] + 1, tab->mat->row[r] + 1,
1344 if (to_col(tab, var) < 0)
1347 isl_tab_kill_col(tab, var->index);
1355 struct isl_tab *isl_tab_from_basic_map(struct isl_basic_map *bmap)
1358 struct isl_tab *tab;
1362 tab = isl_tab_alloc(bmap->ctx,
1363 isl_basic_map_total_dim(bmap) + bmap->n_ineq + 1,
1364 isl_basic_map_total_dim(bmap), 0);
1367 tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
1368 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1369 return isl_tab_mark_empty(tab);
1370 for (i = 0; i < bmap->n_eq; ++i) {
1371 tab = add_eq(tab, bmap->eq[i]);
1375 for (i = 0; i < bmap->n_ineq; ++i) {
1376 tab = isl_tab_add_ineq(tab, bmap->ineq[i]);
1377 if (!tab || tab->empty)
1383 struct isl_tab *isl_tab_from_basic_set(struct isl_basic_set *bset)
1385 return isl_tab_from_basic_map((struct isl_basic_map *)bset);
1388 /* Construct a tableau corresponding to the recession cone of "bmap".
1390 struct isl_tab *isl_tab_from_recession_cone(struct isl_basic_map *bmap)
1394 struct isl_tab *tab;
1398 tab = isl_tab_alloc(bmap->ctx, bmap->n_eq + bmap->n_ineq,
1399 isl_basic_map_total_dim(bmap), 0);
1402 tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
1405 for (i = 0; i < bmap->n_eq; ++i) {
1406 isl_int_swap(bmap->eq[i][0], cst);
1407 tab = add_eq(tab, bmap->eq[i]);
1408 isl_int_swap(bmap->eq[i][0], cst);
1412 for (i = 0; i < bmap->n_ineq; ++i) {
1414 isl_int_swap(bmap->ineq[i][0], cst);
1415 r = isl_tab_add_row(tab, bmap->ineq[i]);
1416 isl_int_swap(bmap->ineq[i][0], cst);
1419 tab->con[r].is_nonneg = 1;
1420 isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]);
1431 /* Assuming "tab" is the tableau of a cone, check if the cone is
1432 * bounded, i.e., if it is empty or only contains the origin.
1434 int isl_tab_cone_is_bounded(struct isl_tab *tab)
1442 if (tab->n_dead == tab->n_col)
1446 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1447 struct isl_tab_var *var;
1448 var = isl_tab_var_from_row(tab, i);
1449 if (!var->is_nonneg)
1451 if (sign_of_max(tab, var) != 0)
1453 close_row(tab, var);
1456 if (tab->n_dead == tab->n_col)
1458 if (i == tab->n_row)
1463 int isl_tab_sample_is_integer(struct isl_tab *tab)
1470 for (i = 0; i < tab->n_var; ++i) {
1472 if (!tab->var[i].is_row)
1474 row = tab->var[i].index;
1475 if (!isl_int_is_divisible_by(tab->mat->row[row][1],
1476 tab->mat->row[row][0]))
1482 static struct isl_vec *extract_integer_sample(struct isl_tab *tab)
1485 struct isl_vec *vec;
1487 vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
1491 isl_int_set_si(vec->block.data[0], 1);
1492 for (i = 0; i < tab->n_var; ++i) {
1493 if (!tab->var[i].is_row)
1494 isl_int_set_si(vec->block.data[1 + i], 0);
1496 int row = tab->var[i].index;
1497 isl_int_divexact(vec->block.data[1 + i],
1498 tab->mat->row[row][1], tab->mat->row[row][0]);
1505 struct isl_vec *isl_tab_get_sample_value(struct isl_tab *tab)
1508 struct isl_vec *vec;
1514 vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
1520 isl_int_set_si(vec->block.data[0], 1);
1521 for (i = 0; i < tab->n_var; ++i) {
1523 if (!tab->var[i].is_row) {
1524 isl_int_set_si(vec->block.data[1 + i], 0);
1527 row = tab->var[i].index;
1528 isl_int_gcd(m, vec->block.data[0], tab->mat->row[row][0]);
1529 isl_int_divexact(m, tab->mat->row[row][0], m);
1530 isl_seq_scale(vec->block.data, vec->block.data, m, 1 + i);
1531 isl_int_divexact(m, vec->block.data[0], tab->mat->row[row][0]);
1532 isl_int_mul(vec->block.data[1 + i], m, tab->mat->row[row][1]);
1534 vec = isl_vec_normalize(vec);
1540 /* Update "bmap" based on the results of the tableau "tab".
1541 * In particular, implicit equalities are made explicit, redundant constraints
1542 * are removed and if the sample value happens to be integer, it is stored
1543 * in "bmap" (unless "bmap" already had an integer sample).
1545 * The tableau is assumed to have been created from "bmap" using
1546 * isl_tab_from_basic_map.
1548 struct isl_basic_map *isl_basic_map_update_from_tab(struct isl_basic_map *bmap,
1549 struct isl_tab *tab)
1561 bmap = isl_basic_map_set_to_empty(bmap);
1563 for (i = bmap->n_ineq - 1; i >= 0; --i) {
1564 if (isl_tab_is_equality(tab, n_eq + i))
1565 isl_basic_map_inequality_to_equality(bmap, i);
1566 else if (isl_tab_is_redundant(tab, n_eq + i))
1567 isl_basic_map_drop_inequality(bmap, i);
1569 if (!tab->rational &&
1570 !bmap->sample && isl_tab_sample_is_integer(tab))
1571 bmap->sample = extract_integer_sample(tab);
1575 struct isl_basic_set *isl_basic_set_update_from_tab(struct isl_basic_set *bset,
1576 struct isl_tab *tab)
1578 return (struct isl_basic_set *)isl_basic_map_update_from_tab(
1579 (struct isl_basic_map *)bset, tab);
1582 /* Given a non-negative variable "var", add a new non-negative variable
1583 * that is the opposite of "var", ensuring that var can only attain the
1585 * If var = n/d is a row variable, then the new variable = -n/d.
1586 * If var is a column variables, then the new variable = -var.
1587 * If the new variable cannot attain non-negative values, then
1588 * the resulting tableau is empty.
1589 * Otherwise, we know the value will be zero and we close the row.
1591 static struct isl_tab *cut_to_hyperplane(struct isl_tab *tab,
1592 struct isl_tab_var *var)
1597 unsigned off = 2 + tab->M;
1599 if (isl_tab_extend_cons(tab, 1) < 0)
1603 tab->con[r].index = tab->n_row;
1604 tab->con[r].is_row = 1;
1605 tab->con[r].is_nonneg = 0;
1606 tab->con[r].is_zero = 0;
1607 tab->con[r].is_redundant = 0;
1608 tab->con[r].frozen = 0;
1609 tab->con[r].negated = 0;
1610 tab->row_var[tab->n_row] = ~r;
1611 row = tab->mat->row[tab->n_row];
1614 isl_int_set(row[0], tab->mat->row[var->index][0]);
1615 isl_seq_neg(row + 1,
1616 tab->mat->row[var->index] + 1, 1 + tab->n_col);
1618 isl_int_set_si(row[0], 1);
1619 isl_seq_clr(row + 1, 1 + tab->n_col);
1620 isl_int_set_si(row[off + var->index], -1);
1625 isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->con[r]);
1627 sgn = sign_of_max(tab, &tab->con[r]);
1629 return isl_tab_mark_empty(tab);
1630 tab->con[r].is_nonneg = 1;
1631 isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]);
1633 close_row(tab, &tab->con[r]);
1641 /* Given a tableau "tab" and an inequality constraint "con" of the tableau,
1642 * relax the inequality by one. That is, the inequality r >= 0 is replaced
1643 * by r' = r + 1 >= 0.
1644 * If r is a row variable, we simply increase the constant term by one
1645 * (taking into account the denominator).
1646 * If r is a column variable, then we need to modify each row that
1647 * refers to r = r' - 1 by substituting this equality, effectively
1648 * subtracting the coefficient of the column from the constant.
1650 struct isl_tab *isl_tab_relax(struct isl_tab *tab, int con)
1652 struct isl_tab_var *var;
1653 unsigned off = 2 + tab->M;
1658 var = &tab->con[con];
1660 if (!var->is_row && !max_is_manifestly_unbounded(tab, var))
1661 to_row(tab, var, 1);
1664 isl_int_add(tab->mat->row[var->index][1],
1665 tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
1669 for (i = 0; i < tab->n_row; ++i) {
1670 if (isl_int_is_zero(tab->mat->row[i][off + var->index]))
1672 isl_int_sub(tab->mat->row[i][1], tab->mat->row[i][1],
1673 tab->mat->row[i][off + var->index]);
1678 isl_tab_push_var(tab, isl_tab_undo_relax, var);
1683 struct isl_tab *isl_tab_select_facet(struct isl_tab *tab, int con)
1688 return cut_to_hyperplane(tab, &tab->con[con]);
1691 static int may_be_equality(struct isl_tab *tab, int row)
1693 unsigned off = 2 + tab->M;
1694 return (tab->rational ? isl_int_is_zero(tab->mat->row[row][1])
1695 : isl_int_lt(tab->mat->row[row][1],
1696 tab->mat->row[row][0])) &&
1697 isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
1698 tab->n_col - tab->n_dead) != -1;
1701 /* Check for (near) equalities among the constraints.
1702 * A constraint is an equality if it is non-negative and if
1703 * its maximal value is either
1704 * - zero (in case of rational tableaus), or
1705 * - strictly less than 1 (in case of integer tableaus)
1707 * We first mark all non-redundant and non-dead variables that
1708 * are not frozen and not obviously not an equality.
1709 * Then we iterate over all marked variables if they can attain
1710 * any values larger than zero or at least one.
1711 * If the maximal value is zero, we mark any column variables
1712 * that appear in the row as being zero and mark the row as being redundant.
1713 * Otherwise, if the maximal value is strictly less than one (and the
1714 * tableau is integer), then we restrict the value to being zero
1715 * by adding an opposite non-negative variable.
1717 struct isl_tab *isl_tab_detect_equalities(struct isl_tab *tab)
1726 if (tab->n_dead == tab->n_col)
1730 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1731 struct isl_tab_var *var = isl_tab_var_from_row(tab, i);
1732 var->marked = !var->frozen && var->is_nonneg &&
1733 may_be_equality(tab, i);
1737 for (i = tab->n_dead; i < tab->n_col; ++i) {
1738 struct isl_tab_var *var = var_from_col(tab, i);
1739 var->marked = !var->frozen && var->is_nonneg;
1744 struct isl_tab_var *var;
1745 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1746 var = isl_tab_var_from_row(tab, i);
1750 if (i == tab->n_row) {
1751 for (i = tab->n_dead; i < tab->n_col; ++i) {
1752 var = var_from_col(tab, i);
1756 if (i == tab->n_col)
1761 if (sign_of_max(tab, var) == 0)
1762 close_row(tab, var);
1763 else if (!tab->rational && !at_least_one(tab, var)) {
1764 tab = cut_to_hyperplane(tab, var);
1765 return isl_tab_detect_equalities(tab);
1767 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1768 var = isl_tab_var_from_row(tab, i);
1771 if (may_be_equality(tab, i))
1781 /* Check for (near) redundant constraints.
1782 * A constraint is redundant if it is non-negative and if
1783 * its minimal value (temporarily ignoring the non-negativity) is either
1784 * - zero (in case of rational tableaus), or
1785 * - strictly larger than -1 (in case of integer tableaus)
1787 * We first mark all non-redundant and non-dead variables that
1788 * are not frozen and not obviously negatively unbounded.
1789 * Then we iterate over all marked variables if they can attain
1790 * any values smaller than zero or at most negative one.
1791 * If not, we mark the row as being redundant (assuming it hasn't
1792 * been detected as being obviously redundant in the mean time).
1794 struct isl_tab *isl_tab_detect_redundant(struct isl_tab *tab)
1803 if (tab->n_redundant == tab->n_row)
1807 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1808 struct isl_tab_var *var = isl_tab_var_from_row(tab, i);
1809 var->marked = !var->frozen && var->is_nonneg;
1813 for (i = tab->n_dead; i < tab->n_col; ++i) {
1814 struct isl_tab_var *var = var_from_col(tab, i);
1815 var->marked = !var->frozen && var->is_nonneg &&
1816 !min_is_manifestly_unbounded(tab, var);
1821 struct isl_tab_var *var;
1822 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1823 var = isl_tab_var_from_row(tab, i);
1827 if (i == tab->n_row) {
1828 for (i = tab->n_dead; i < tab->n_col; ++i) {
1829 var = var_from_col(tab, i);
1833 if (i == tab->n_col)
1838 if ((tab->rational ? (sign_of_min(tab, var) >= 0)
1839 : !isl_tab_min_at_most_neg_one(tab, var)) &&
1841 isl_tab_mark_redundant(tab, var->index);
1842 for (i = tab->n_dead; i < tab->n_col; ++i) {
1843 var = var_from_col(tab, i);
1846 if (!min_is_manifestly_unbounded(tab, var))
1856 int isl_tab_is_equality(struct isl_tab *tab, int con)
1863 if (tab->con[con].is_zero)
1865 if (tab->con[con].is_redundant)
1867 if (!tab->con[con].is_row)
1868 return tab->con[con].index < tab->n_dead;
1870 row = tab->con[con].index;
1873 return isl_int_is_zero(tab->mat->row[row][1]) &&
1874 isl_seq_first_non_zero(tab->mat->row[row] + 2 + tab->n_dead,
1875 tab->n_col - tab->n_dead) == -1;
1878 /* Return the minimial value of the affine expression "f" with denominator
1879 * "denom" in *opt, *opt_denom, assuming the tableau is not empty and
1880 * the expression cannot attain arbitrarily small values.
1881 * If opt_denom is NULL, then *opt is rounded up to the nearest integer.
1882 * The return value reflects the nature of the result (empty, unbounded,
1883 * minmimal value returned in *opt).
1885 enum isl_lp_result isl_tab_min(struct isl_tab *tab,
1886 isl_int *f, isl_int denom, isl_int *opt, isl_int *opt_denom,
1890 enum isl_lp_result res = isl_lp_ok;
1891 struct isl_tab_var *var;
1892 struct isl_tab_undo *snap;
1895 return isl_lp_empty;
1897 snap = isl_tab_snap(tab);
1898 r = isl_tab_add_row(tab, f);
1900 return isl_lp_error;
1902 isl_int_mul(tab->mat->row[var->index][0],
1903 tab->mat->row[var->index][0], denom);
1906 find_pivot(tab, var, var, -1, &row, &col);
1907 if (row == var->index) {
1908 res = isl_lp_unbounded;
1913 isl_tab_pivot(tab, row, col);
1915 if (ISL_FL_ISSET(flags, ISL_TAB_SAVE_DUAL)) {
1918 isl_vec_free(tab->dual);
1919 tab->dual = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_con);
1921 return isl_lp_error;
1922 isl_int_set(tab->dual->el[0], tab->mat->row[var->index][0]);
1923 for (i = 0; i < tab->n_con; ++i) {
1925 if (tab->con[i].is_row) {
1926 isl_int_set_si(tab->dual->el[1 + i], 0);
1929 pos = 2 + tab->M + tab->con[i].index;
1930 if (tab->con[i].negated)
1931 isl_int_neg(tab->dual->el[1 + i],
1932 tab->mat->row[var->index][pos]);
1934 isl_int_set(tab->dual->el[1 + i],
1935 tab->mat->row[var->index][pos]);
1938 if (opt && res == isl_lp_ok) {
1940 isl_int_set(*opt, tab->mat->row[var->index][1]);
1941 isl_int_set(*opt_denom, tab->mat->row[var->index][0]);
1943 isl_int_cdiv_q(*opt, tab->mat->row[var->index][1],
1944 tab->mat->row[var->index][0]);
1946 if (isl_tab_rollback(tab, snap) < 0)
1947 return isl_lp_error;
1951 int isl_tab_is_redundant(struct isl_tab *tab, int con)
1955 if (tab->con[con].is_zero)
1957 if (tab->con[con].is_redundant)
1959 return tab->con[con].is_row && tab->con[con].index < tab->n_redundant;
1962 /* Take a snapshot of the tableau that can be restored by s call to
1965 struct isl_tab_undo *isl_tab_snap(struct isl_tab *tab)
1973 /* Undo the operation performed by isl_tab_relax.
1975 static void unrelax(struct isl_tab *tab, struct isl_tab_var *var)
1977 unsigned off = 2 + tab->M;
1979 if (!var->is_row && !max_is_manifestly_unbounded(tab, var))
1980 to_row(tab, var, 1);
1983 isl_int_sub(tab->mat->row[var->index][1],
1984 tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
1988 for (i = 0; i < tab->n_row; ++i) {
1989 if (isl_int_is_zero(tab->mat->row[i][off + var->index]))
1991 isl_int_add(tab->mat->row[i][1], tab->mat->row[i][1],
1992 tab->mat->row[i][off + var->index]);
1998 static void perform_undo_var(struct isl_tab *tab, struct isl_tab_undo *undo)
2000 struct isl_tab_var *var = var_from_index(tab, undo->u.var_index);
2001 switch(undo->type) {
2002 case isl_tab_undo_nonneg:
2005 case isl_tab_undo_redundant:
2006 var->is_redundant = 0;
2009 case isl_tab_undo_zero:
2013 case isl_tab_undo_allocate:
2014 if (undo->u.var_index >= 0) {
2015 isl_assert(tab->mat->ctx, !var->is_row, return);
2016 drop_col(tab, var->index);
2020 if (!max_is_manifestly_unbounded(tab, var))
2021 to_row(tab, var, 1);
2022 else if (!min_is_manifestly_unbounded(tab, var))
2023 to_row(tab, var, -1);
2025 to_row(tab, var, 0);
2027 drop_row(tab, var->index);
2029 case isl_tab_undo_relax:
2035 /* Restore the tableau to the state where the basic variables
2036 * are those in "col_var".
2037 * We first construct a list of variables that are currently in
2038 * the basis, but shouldn't. Then we iterate over all variables
2039 * that should be in the basis and for each one that is currently
2040 * not in the basis, we exchange it with one of the elements of the
2041 * list constructed before.
2042 * We can always find an appropriate variable to pivot with because
2043 * the current basis is mapped to the old basis by a non-singular
2044 * matrix and so we can never end up with a zero row.
2046 static int restore_basis(struct isl_tab *tab, int *col_var)
2050 int *extra = NULL; /* current columns that contain bad stuff */
2051 unsigned off = 2 + tab->M;
2053 extra = isl_alloc_array(tab->mat->ctx, int, tab->n_col);
2056 for (i = 0; i < tab->n_col; ++i) {
2057 for (j = 0; j < tab->n_col; ++j)
2058 if (tab->col_var[i] == col_var[j])
2062 extra[n_extra++] = i;
2064 for (i = 0; i < tab->n_col && n_extra > 0; ++i) {
2065 struct isl_tab_var *var;
2068 for (j = 0; j < tab->n_col; ++j)
2069 if (col_var[i] == tab->col_var[j])
2073 var = var_from_index(tab, col_var[i]);
2075 for (j = 0; j < n_extra; ++j)
2076 if (!isl_int_is_zero(tab->mat->row[row][off+extra[j]]))
2078 isl_assert(tab->mat->ctx, j < n_extra, goto error);
2079 isl_tab_pivot(tab, row, extra[j]);
2080 extra[j] = extra[--n_extra];
2092 static int perform_undo(struct isl_tab *tab, struct isl_tab_undo *undo)
2094 switch (undo->type) {
2095 case isl_tab_undo_empty:
2098 case isl_tab_undo_nonneg:
2099 case isl_tab_undo_redundant:
2100 case isl_tab_undo_zero:
2101 case isl_tab_undo_allocate:
2102 case isl_tab_undo_relax:
2103 perform_undo_var(tab, undo);
2105 case isl_tab_undo_bset_eq:
2106 isl_basic_set_free_equality(tab->bset, 1);
2108 case isl_tab_undo_bset_ineq:
2109 isl_basic_set_free_inequality(tab->bset, 1);
2111 case isl_tab_undo_bset_div:
2112 isl_basic_set_free_div(tab->bset, 1);
2114 tab->samples->n_col--;
2116 case isl_tab_undo_saved_basis:
2117 if (restore_basis(tab, undo->u.col_var) < 0)
2120 case isl_tab_undo_drop_sample:
2124 isl_assert(tab->mat->ctx, 0, return -1);
2129 /* Return the tableau to the state it was in when the snapshot "snap"
2132 int isl_tab_rollback(struct isl_tab *tab, struct isl_tab_undo *snap)
2134 struct isl_tab_undo *undo, *next;
2140 for (undo = tab->top; undo && undo != &tab->bottom; undo = next) {
2144 if (perform_undo(tab, undo) < 0) {
2158 /* The given row "row" represents an inequality violated by all
2159 * points in the tableau. Check for some special cases of such
2160 * separating constraints.
2161 * In particular, if the row has been reduced to the constant -1,
2162 * then we know the inequality is adjacent (but opposite) to
2163 * an equality in the tableau.
2164 * If the row has been reduced to r = -1 -r', with r' an inequality
2165 * of the tableau, then the inequality is adjacent (but opposite)
2166 * to the inequality r'.
2168 static enum isl_ineq_type separation_type(struct isl_tab *tab, unsigned row)
2171 unsigned off = 2 + tab->M;
2174 return isl_ineq_separate;
2176 if (!isl_int_is_one(tab->mat->row[row][0]))
2177 return isl_ineq_separate;
2178 if (!isl_int_is_negone(tab->mat->row[row][1]))
2179 return isl_ineq_separate;
2181 pos = isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
2182 tab->n_col - tab->n_dead);
2184 return isl_ineq_adj_eq;
2186 if (!isl_int_is_negone(tab->mat->row[row][off + tab->n_dead + pos]))
2187 return isl_ineq_separate;
2189 pos = isl_seq_first_non_zero(
2190 tab->mat->row[row] + off + tab->n_dead + pos + 1,
2191 tab->n_col - tab->n_dead - pos - 1);
2193 return pos == -1 ? isl_ineq_adj_ineq : isl_ineq_separate;
2196 /* Check the effect of inequality "ineq" on the tableau "tab".
2198 * isl_ineq_redundant: satisfied by all points in the tableau
2199 * isl_ineq_separate: satisfied by no point in the tableau
2200 * isl_ineq_cut: satisfied by some by not all points
2201 * isl_ineq_adj_eq: adjacent to an equality
2202 * isl_ineq_adj_ineq: adjacent to an inequality.
2204 enum isl_ineq_type isl_tab_ineq_type(struct isl_tab *tab, isl_int *ineq)
2206 enum isl_ineq_type type = isl_ineq_error;
2207 struct isl_tab_undo *snap = NULL;
2212 return isl_ineq_error;
2214 if (isl_tab_extend_cons(tab, 1) < 0)
2215 return isl_ineq_error;
2217 snap = isl_tab_snap(tab);
2219 con = isl_tab_add_row(tab, ineq);
2223 row = tab->con[con].index;
2224 if (isl_tab_row_is_redundant(tab, row))
2225 type = isl_ineq_redundant;
2226 else if (isl_int_is_neg(tab->mat->row[row][1]) &&
2228 isl_int_abs_ge(tab->mat->row[row][1],
2229 tab->mat->row[row][0]))) {
2230 if (at_least_zero(tab, &tab->con[con]))
2231 type = isl_ineq_cut;
2233 type = separation_type(tab, row);
2234 } else if (tab->rational ? (sign_of_min(tab, &tab->con[con]) < 0)
2235 : isl_tab_min_at_most_neg_one(tab, &tab->con[con]))
2236 type = isl_ineq_cut;
2238 type = isl_ineq_redundant;
2240 if (isl_tab_rollback(tab, snap))
2241 return isl_ineq_error;
2244 isl_tab_rollback(tab, snap);
2245 return isl_ineq_error;
2248 void isl_tab_dump(struct isl_tab *tab, FILE *out, int indent)
2254 fprintf(out, "%*snull tab\n", indent, "");
2257 fprintf(out, "%*sn_redundant: %d, n_dead: %d", indent, "",
2258 tab->n_redundant, tab->n_dead);
2260 fprintf(out, ", rational");
2262 fprintf(out, ", empty");
2264 fprintf(out, "%*s[", indent, "");
2265 for (i = 0; i < tab->n_var; ++i) {
2267 fprintf(out, (i == tab->n_param ||
2268 i == tab->n_var - tab->n_div) ? "; "
2270 fprintf(out, "%c%d%s", tab->var[i].is_row ? 'r' : 'c',
2272 tab->var[i].is_zero ? " [=0]" :
2273 tab->var[i].is_redundant ? " [R]" : "");
2275 fprintf(out, "]\n");
2276 fprintf(out, "%*s[", indent, "");
2277 for (i = 0; i < tab->n_con; ++i) {
2280 fprintf(out, "%c%d%s", tab->con[i].is_row ? 'r' : 'c',
2282 tab->con[i].is_zero ? " [=0]" :
2283 tab->con[i].is_redundant ? " [R]" : "");
2285 fprintf(out, "]\n");
2286 fprintf(out, "%*s[", indent, "");
2287 for (i = 0; i < tab->n_row; ++i) {
2288 const char *sign = "";
2291 if (tab->row_sign) {
2292 if (tab->row_sign[i] == isl_tab_row_unknown)
2294 else if (tab->row_sign[i] == isl_tab_row_neg)
2296 else if (tab->row_sign[i] == isl_tab_row_pos)
2301 fprintf(out, "r%d: %d%s%s", i, tab->row_var[i],
2302 isl_tab_var_from_row(tab, i)->is_nonneg ? " [>=0]" : "", sign);
2304 fprintf(out, "]\n");
2305 fprintf(out, "%*s[", indent, "");
2306 for (i = 0; i < tab->n_col; ++i) {
2309 fprintf(out, "c%d: %d%s", i, tab->col_var[i],
2310 var_from_col(tab, i)->is_nonneg ? " [>=0]" : "");
2312 fprintf(out, "]\n");
2313 r = tab->mat->n_row;
2314 tab->mat->n_row = tab->n_row;
2315 c = tab->mat->n_col;
2316 tab->mat->n_col = 2 + tab->M + tab->n_col;
2317 isl_mat_dump(tab->mat, out, indent);
2318 tab->mat->n_row = r;
2319 tab->mat->n_col = c;
2321 isl_basic_set_dump(tab->bset, out, indent);