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->n_col + n_new);
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)
190 dup = isl_calloc_type(tab->ctx, struct isl_tab);
193 dup->mat = isl_mat_dup(tab->mat);
196 dup->var = isl_alloc_array(tab->ctx, struct isl_tab_var, tab->max_var);
199 for (i = 0; i < tab->n_var; ++i)
200 dup->var[i] = tab->var[i];
201 dup->con = isl_alloc_array(tab->ctx, struct isl_tab_var, tab->max_con);
204 for (i = 0; i < tab->n_con; ++i)
205 dup->con[i] = tab->con[i];
206 dup->col_var = isl_alloc_array(tab->ctx, int, tab->mat->n_col - off);
209 for (i = 0; i < tab->n_col; ++i)
210 dup->col_var[i] = tab->col_var[i];
211 dup->row_var = isl_alloc_array(tab->ctx, int, tab->mat->n_row);
214 for (i = 0; i < tab->n_row; ++i)
215 dup->row_var[i] = tab->row_var[i];
217 dup->row_sign = isl_alloc_array(tab->ctx, enum isl_tab_row_sign,
221 for (i = 0; i < tab->n_row; ++i)
222 dup->row_sign[i] = tab->row_sign[i];
225 dup->samples = isl_mat_dup(tab->samples);
228 dup->n_sample = tab->n_sample;
229 dup->n_outside = tab->n_outside;
231 dup->n_row = tab->n_row;
232 dup->n_con = tab->n_con;
233 dup->n_eq = tab->n_eq;
234 dup->max_con = tab->max_con;
235 dup->n_col = tab->n_col;
236 dup->n_var = tab->n_var;
237 dup->max_var = tab->max_var;
238 dup->n_param = tab->n_param;
239 dup->n_div = tab->n_div;
240 dup->n_dead = tab->n_dead;
241 dup->n_redundant = tab->n_redundant;
242 dup->rational = tab->rational;
243 dup->empty = tab->empty;
247 dup->bottom.type = isl_tab_undo_bottom;
248 dup->bottom.next = NULL;
249 dup->top = &dup->bottom;
256 static struct isl_tab_var *var_from_index(struct isl_tab *tab, int i)
261 return &tab->con[~i];
264 struct isl_tab_var *isl_tab_var_from_row(struct isl_tab *tab, int i)
266 return var_from_index(tab, tab->row_var[i]);
269 static struct isl_tab_var *var_from_col(struct isl_tab *tab, int i)
271 return var_from_index(tab, tab->col_var[i]);
274 /* Check if there are any upper bounds on column variable "var",
275 * i.e., non-negative rows where var appears with a negative coefficient.
276 * Return 1 if there are no such bounds.
278 static int max_is_manifestly_unbounded(struct isl_tab *tab,
279 struct isl_tab_var *var)
282 unsigned off = 2 + tab->M;
286 for (i = tab->n_redundant; i < tab->n_row; ++i) {
287 if (!isl_int_is_neg(tab->mat->row[i][off + var->index]))
289 if (isl_tab_var_from_row(tab, i)->is_nonneg)
295 /* Check if there are any lower bounds on column variable "var",
296 * i.e., non-negative rows where var appears with a positive coefficient.
297 * Return 1 if there are no such bounds.
299 static int min_is_manifestly_unbounded(struct isl_tab *tab,
300 struct isl_tab_var *var)
303 unsigned off = 2 + tab->M;
307 for (i = tab->n_redundant; i < tab->n_row; ++i) {
308 if (!isl_int_is_pos(tab->mat->row[i][off + var->index]))
310 if (isl_tab_var_from_row(tab, i)->is_nonneg)
316 static int row_cmp(struct isl_tab *tab, int r1, int r2, int c, isl_int t)
318 unsigned off = 2 + tab->M;
322 isl_int_mul(t, tab->mat->row[r1][2], tab->mat->row[r2][off+c]);
323 isl_int_submul(t, tab->mat->row[r2][2], tab->mat->row[r1][off+c]);
328 isl_int_mul(t, tab->mat->row[r1][1], tab->mat->row[r2][off + c]);
329 isl_int_submul(t, tab->mat->row[r2][1], tab->mat->row[r1][off + c]);
330 return isl_int_sgn(t);
333 /* Given the index of a column "c", return the index of a row
334 * that can be used to pivot the column in, with either an increase
335 * (sgn > 0) or a decrease (sgn < 0) of the corresponding variable.
336 * If "var" is not NULL, then the row returned will be different from
337 * the one associated with "var".
339 * Each row in the tableau is of the form
341 * x_r = a_r0 + \sum_i a_ri x_i
343 * Only rows with x_r >= 0 and with the sign of a_ri opposite to "sgn"
344 * impose any limit on the increase or decrease in the value of x_c
345 * and this bound is equal to a_r0 / |a_rc|. We are therefore looking
346 * for the row with the smallest (most stringent) such bound.
347 * Note that the common denominator of each row drops out of the fraction.
348 * To check if row j has a smaller bound than row r, i.e.,
349 * a_j0 / |a_jc| < a_r0 / |a_rc| or a_j0 |a_rc| < a_r0 |a_jc|,
350 * we check if -sign(a_jc) (a_j0 a_rc - a_r0 a_jc) < 0,
351 * where -sign(a_jc) is equal to "sgn".
353 static int pivot_row(struct isl_tab *tab,
354 struct isl_tab_var *var, int sgn, int c)
358 unsigned off = 2 + tab->M;
362 for (j = tab->n_redundant; j < tab->n_row; ++j) {
363 if (var && j == var->index)
365 if (!isl_tab_var_from_row(tab, j)->is_nonneg)
367 if (sgn * isl_int_sgn(tab->mat->row[j][off + c]) >= 0)
373 tsgn = sgn * row_cmp(tab, r, j, c, t);
374 if (tsgn < 0 || (tsgn == 0 &&
375 tab->row_var[j] < tab->row_var[r]))
382 /* Find a pivot (row and col) that will increase (sgn > 0) or decrease
383 * (sgn < 0) the value of row variable var.
384 * If not NULL, then skip_var is a row variable that should be ignored
385 * while looking for a pivot row. It is usually equal to var.
387 * As the given row in the tableau is of the form
389 * x_r = a_r0 + \sum_i a_ri x_i
391 * we need to find a column such that the sign of a_ri is equal to "sgn"
392 * (such that an increase in x_i will have the desired effect) or a
393 * column with a variable that may attain negative values.
394 * If a_ri is positive, then we need to move x_i in the same direction
395 * to obtain the desired effect. Otherwise, x_i has to move in the
396 * opposite direction.
398 static void find_pivot(struct isl_tab *tab,
399 struct isl_tab_var *var, struct isl_tab_var *skip_var,
400 int sgn, int *row, int *col)
407 isl_assert(tab->mat->ctx, var->is_row, return);
408 tr = tab->mat->row[var->index] + 2 + tab->M;
411 for (j = tab->n_dead; j < tab->n_col; ++j) {
412 if (isl_int_is_zero(tr[j]))
414 if (isl_int_sgn(tr[j]) != sgn &&
415 var_from_col(tab, j)->is_nonneg)
417 if (c < 0 || tab->col_var[j] < tab->col_var[c])
423 sgn *= isl_int_sgn(tr[c]);
424 r = pivot_row(tab, skip_var, sgn, c);
425 *row = r < 0 ? var->index : r;
429 /* Return 1 if row "row" represents an obviously redundant inequality.
431 * - it represents an inequality or a variable
432 * - that is the sum of a non-negative sample value and a positive
433 * combination of zero or more non-negative variables.
435 int isl_tab_row_is_redundant(struct isl_tab *tab, int row)
438 unsigned off = 2 + tab->M;
440 if (tab->row_var[row] < 0 && !isl_tab_var_from_row(tab, row)->is_nonneg)
443 if (isl_int_is_neg(tab->mat->row[row][1]))
445 if (tab->M && isl_int_is_neg(tab->mat->row[row][2]))
448 for (i = tab->n_dead; i < tab->n_col; ++i) {
449 if (isl_int_is_zero(tab->mat->row[row][off + i]))
451 if (isl_int_is_neg(tab->mat->row[row][off + i]))
453 if (!var_from_col(tab, i)->is_nonneg)
459 static void swap_rows(struct isl_tab *tab, int row1, int row2)
462 t = tab->row_var[row1];
463 tab->row_var[row1] = tab->row_var[row2];
464 tab->row_var[row2] = t;
465 isl_tab_var_from_row(tab, row1)->index = row1;
466 isl_tab_var_from_row(tab, row2)->index = row2;
467 tab->mat = isl_mat_swap_rows(tab->mat, row1, row2);
471 t = tab->row_sign[row1];
472 tab->row_sign[row1] = tab->row_sign[row2];
473 tab->row_sign[row2] = t;
476 static void push_union(struct isl_tab *tab,
477 enum isl_tab_undo_type type, union isl_tab_undo_val u)
479 struct isl_tab_undo *undo;
484 undo = isl_alloc_type(tab->mat->ctx, struct isl_tab_undo);
492 undo->next = tab->top;
496 void isl_tab_push_var(struct isl_tab *tab,
497 enum isl_tab_undo_type type, struct isl_tab_var *var)
499 union isl_tab_undo_val u;
501 u.var_index = tab->row_var[var->index];
503 u.var_index = tab->col_var[var->index];
504 push_union(tab, type, u);
507 void isl_tab_push(struct isl_tab *tab, enum isl_tab_undo_type type)
509 union isl_tab_undo_val u = { 0 };
510 push_union(tab, type, u);
513 /* Push a record on the undo stack describing the current basic
514 * variables, so that the this state can be restored during rollback.
516 void isl_tab_push_basis(struct isl_tab *tab)
519 union isl_tab_undo_val u;
521 u.col_var = isl_alloc_array(tab->mat->ctx, int, tab->n_col);
527 for (i = 0; i < tab->n_col; ++i)
528 u.col_var[i] = tab->col_var[i];
529 push_union(tab, isl_tab_undo_saved_basis, u);
532 /* Mark row with index "row" as being redundant.
533 * If we may need to undo the operation or if the row represents
534 * a variable of the original problem, the row is kept,
535 * but no longer considered when looking for a pivot row.
536 * Otherwise, the row is simply removed.
538 * The row may be interchanged with some other row. If it
539 * is interchanged with a later row, return 1. Otherwise return 0.
540 * If the rows are checked in order in the calling function,
541 * then a return value of 1 means that the row with the given
542 * row number may now contain a different row that hasn't been checked yet.
544 int isl_tab_mark_redundant(struct isl_tab *tab, int row)
546 struct isl_tab_var *var = isl_tab_var_from_row(tab, row);
547 var->is_redundant = 1;
548 isl_assert(tab->mat->ctx, row >= tab->n_redundant, return -1);
549 if (tab->need_undo || tab->row_var[row] >= 0) {
550 if (tab->row_var[row] >= 0 && !var->is_nonneg) {
552 isl_tab_push_var(tab, isl_tab_undo_nonneg, var);
554 if (row != tab->n_redundant)
555 swap_rows(tab, row, tab->n_redundant);
556 isl_tab_push_var(tab, isl_tab_undo_redundant, var);
560 if (row != tab->n_row - 1)
561 swap_rows(tab, row, tab->n_row - 1);
562 isl_tab_var_from_row(tab, tab->n_row - 1)->index = -1;
568 struct isl_tab *isl_tab_mark_empty(struct isl_tab *tab)
570 if (!tab->empty && tab->need_undo)
571 isl_tab_push(tab, isl_tab_undo_empty);
576 /* Update the rows signs after a pivot of "row" and "col", with "row_sgn"
577 * the original sign of the pivot element.
578 * We only keep track of row signs during PILP solving and in this case
579 * we only pivot a row with negative sign (meaning the value is always
580 * non-positive) using a positive pivot element.
582 * For each row j, the new value of the parametric constant is equal to
584 * a_j0 - a_jc a_r0/a_rc
586 * where a_j0 is the original parametric constant, a_rc is the pivot element,
587 * a_r0 is the parametric constant of the pivot row and a_jc is the
588 * pivot column entry of the row j.
589 * Since a_r0 is non-positive and a_rc is positive, the sign of row j
590 * remains the same if a_jc has the same sign as the row j or if
591 * a_jc is zero. In all other cases, we reset the sign to "unknown".
593 static void update_row_sign(struct isl_tab *tab, int row, int col, int row_sgn)
596 struct isl_mat *mat = tab->mat;
597 unsigned off = 2 + tab->M;
602 if (tab->row_sign[row] == 0)
604 isl_assert(mat->ctx, row_sgn > 0, return);
605 isl_assert(mat->ctx, tab->row_sign[row] == isl_tab_row_neg, return);
606 tab->row_sign[row] = isl_tab_row_pos;
607 for (i = 0; i < tab->n_row; ++i) {
611 s = isl_int_sgn(mat->row[i][off + col]);
614 if (!tab->row_sign[i])
616 if (s < 0 && tab->row_sign[i] == isl_tab_row_neg)
618 if (s > 0 && tab->row_sign[i] == isl_tab_row_pos)
620 tab->row_sign[i] = isl_tab_row_unknown;
624 /* Given a row number "row" and a column number "col", pivot the tableau
625 * such that the associated variables are interchanged.
626 * The given row in the tableau expresses
628 * x_r = a_r0 + \sum_i a_ri x_i
632 * x_c = 1/a_rc x_r - a_r0/a_rc + sum_{i \ne r} -a_ri/a_rc
634 * Substituting this equality into the other rows
636 * x_j = a_j0 + \sum_i a_ji x_i
638 * with a_jc \ne 0, we obtain
640 * 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
647 * where i is any other column and j is any other row,
648 * is therefore transformed into
650 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
651 * 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)
653 * The transformation is performed along the following steps
658 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
661 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
662 * n_jc/(|n_rc| d_j) n_ji/(|n_rc| d_j)
664 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
665 * n_jc/(|n_rc| d_j) (n_ji |n_rc|)/(|n_rc| d_j)
667 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
668 * n_jc/(|n_rc| d_j) (n_ji |n_rc| - s(n_rc)n_jc n_ri)/(|n_rc| d_j)
670 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
671 * 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)
674 void isl_tab_pivot(struct isl_tab *tab, int row, int col)
679 struct isl_mat *mat = tab->mat;
680 struct isl_tab_var *var;
681 unsigned off = 2 + tab->M;
683 isl_int_swap(mat->row[row][0], mat->row[row][off + col]);
684 sgn = isl_int_sgn(mat->row[row][0]);
686 isl_int_neg(mat->row[row][0], mat->row[row][0]);
687 isl_int_neg(mat->row[row][off + col], mat->row[row][off + col]);
689 for (j = 0; j < off - 1 + tab->n_col; ++j) {
690 if (j == off - 1 + col)
692 isl_int_neg(mat->row[row][1 + j], mat->row[row][1 + j]);
694 if (!isl_int_is_one(mat->row[row][0]))
695 isl_seq_normalize(mat->ctx, mat->row[row], off + tab->n_col);
696 for (i = 0; i < tab->n_row; ++i) {
699 if (isl_int_is_zero(mat->row[i][off + col]))
701 isl_int_mul(mat->row[i][0], mat->row[i][0], mat->row[row][0]);
702 for (j = 0; j < off - 1 + tab->n_col; ++j) {
703 if (j == off - 1 + col)
705 isl_int_mul(mat->row[i][1 + j],
706 mat->row[i][1 + j], mat->row[row][0]);
707 isl_int_addmul(mat->row[i][1 + j],
708 mat->row[i][off + col], mat->row[row][1 + j]);
710 isl_int_mul(mat->row[i][off + col],
711 mat->row[i][off + col], mat->row[row][off + col]);
712 if (!isl_int_is_one(mat->row[i][0]))
713 isl_seq_normalize(mat->ctx, mat->row[i], off + tab->n_col);
715 t = tab->row_var[row];
716 tab->row_var[row] = tab->col_var[col];
717 tab->col_var[col] = t;
718 var = isl_tab_var_from_row(tab, row);
721 var = var_from_col(tab, col);
724 update_row_sign(tab, row, col, sgn);
727 for (i = tab->n_redundant; i < tab->n_row; ++i) {
728 if (isl_int_is_zero(mat->row[i][off + col]))
730 if (!isl_tab_var_from_row(tab, i)->frozen &&
731 isl_tab_row_is_redundant(tab, i))
732 if (isl_tab_mark_redundant(tab, i))
737 /* If "var" represents a column variable, then pivot is up (sgn > 0)
738 * or down (sgn < 0) to a row. The variable is assumed not to be
739 * unbounded in the specified direction.
740 * If sgn = 0, then the variable is unbounded in both directions,
741 * and we pivot with any row we can find.
743 static void to_row(struct isl_tab *tab, struct isl_tab_var *var, int sign)
746 unsigned off = 2 + tab->M;
752 for (r = tab->n_redundant; r < tab->n_row; ++r)
753 if (!isl_int_is_zero(tab->mat->row[r][off+var->index]))
755 isl_assert(tab->mat->ctx, r < tab->n_row, return);
757 r = pivot_row(tab, NULL, sign, var->index);
758 isl_assert(tab->mat->ctx, r >= 0, return);
761 isl_tab_pivot(tab, r, var->index);
764 static void check_table(struct isl_tab *tab)
770 for (i = 0; i < tab->n_row; ++i) {
771 if (!isl_tab_var_from_row(tab, i)->is_nonneg)
773 assert(!isl_int_is_neg(tab->mat->row[i][1]));
777 /* Return the sign of the maximal value of "var".
778 * If the sign is not negative, then on return from this function,
779 * the sample value will also be non-negative.
781 * If "var" is manifestly unbounded wrt positive values, we are done.
782 * Otherwise, we pivot the variable up to a row if needed
783 * Then we continue pivoting down until either
784 * - no more down pivots can be performed
785 * - the sample value is positive
786 * - the variable is pivoted into a manifestly unbounded column
788 static int sign_of_max(struct isl_tab *tab, struct isl_tab_var *var)
792 if (max_is_manifestly_unbounded(tab, var))
795 while (!isl_int_is_pos(tab->mat->row[var->index][1])) {
796 find_pivot(tab, var, var, 1, &row, &col);
798 return isl_int_sgn(tab->mat->row[var->index][1]);
799 isl_tab_pivot(tab, row, col);
800 if (!var->is_row) /* manifestly unbounded */
806 static int row_is_neg(struct isl_tab *tab, int row)
809 return isl_int_is_neg(tab->mat->row[row][1]);
810 if (isl_int_is_pos(tab->mat->row[row][2]))
812 if (isl_int_is_neg(tab->mat->row[row][2]))
814 return isl_int_is_neg(tab->mat->row[row][1]);
817 static int row_sgn(struct isl_tab *tab, int row)
820 return isl_int_sgn(tab->mat->row[row][1]);
821 if (!isl_int_is_zero(tab->mat->row[row][2]))
822 return isl_int_sgn(tab->mat->row[row][2]);
824 return isl_int_sgn(tab->mat->row[row][1]);
827 /* Perform pivots until the row variable "var" has a non-negative
828 * sample value or until no more upward pivots can be performed.
829 * Return the sign of the sample value after the pivots have been
832 static int restore_row(struct isl_tab *tab, struct isl_tab_var *var)
836 while (row_is_neg(tab, var->index)) {
837 find_pivot(tab, var, var, 1, &row, &col);
840 isl_tab_pivot(tab, row, col);
841 if (!var->is_row) /* manifestly unbounded */
844 return row_sgn(tab, var->index);
847 /* Perform pivots until we are sure that the row variable "var"
848 * can attain non-negative values. After return from this
849 * function, "var" is still a row variable, but its sample
850 * value may not be non-negative, even if the function returns 1.
852 static int at_least_zero(struct isl_tab *tab, struct isl_tab_var *var)
856 while (isl_int_is_neg(tab->mat->row[var->index][1])) {
857 find_pivot(tab, var, var, 1, &row, &col);
860 if (row == var->index) /* manifestly unbounded */
862 isl_tab_pivot(tab, row, col);
864 return !isl_int_is_neg(tab->mat->row[var->index][1]);
867 /* Return a negative value if "var" can attain negative values.
868 * Return a non-negative value otherwise.
870 * If "var" is manifestly unbounded wrt negative values, we are done.
871 * Otherwise, if var is in a column, we can pivot it down to a row.
872 * Then we continue pivoting down until either
873 * - the pivot would result in a manifestly unbounded column
874 * => we don't perform the pivot, but simply return -1
875 * - no more down pivots can be performed
876 * - the sample value is negative
877 * If the sample value becomes negative and the variable is supposed
878 * to be nonnegative, then we undo the last pivot.
879 * However, if the last pivot has made the pivoting variable
880 * obviously redundant, then it may have moved to another row.
881 * In that case we look for upward pivots until we reach a non-negative
884 static int sign_of_min(struct isl_tab *tab, struct isl_tab_var *var)
887 struct isl_tab_var *pivot_var = NULL;
889 if (min_is_manifestly_unbounded(tab, var))
893 row = pivot_row(tab, NULL, -1, col);
894 pivot_var = var_from_col(tab, col);
895 isl_tab_pivot(tab, row, col);
896 if (var->is_redundant)
898 if (isl_int_is_neg(tab->mat->row[var->index][1])) {
899 if (var->is_nonneg) {
900 if (!pivot_var->is_redundant &&
901 pivot_var->index == row)
902 isl_tab_pivot(tab, row, col);
904 restore_row(tab, var);
909 if (var->is_redundant)
911 while (!isl_int_is_neg(tab->mat->row[var->index][1])) {
912 find_pivot(tab, var, var, -1, &row, &col);
913 if (row == var->index)
916 return isl_int_sgn(tab->mat->row[var->index][1]);
917 pivot_var = var_from_col(tab, col);
918 isl_tab_pivot(tab, row, col);
919 if (var->is_redundant)
922 if (pivot_var && var->is_nonneg) {
923 /* pivot back to non-negative value */
924 if (!pivot_var->is_redundant && pivot_var->index == row)
925 isl_tab_pivot(tab, row, col);
927 restore_row(tab, var);
932 static int row_at_most_neg_one(struct isl_tab *tab, int row)
935 if (isl_int_is_pos(tab->mat->row[row][2]))
937 if (isl_int_is_neg(tab->mat->row[row][2]))
940 return isl_int_is_neg(tab->mat->row[row][1]) &&
941 isl_int_abs_ge(tab->mat->row[row][1],
942 tab->mat->row[row][0]);
945 /* Return 1 if "var" can attain values <= -1.
946 * Return 0 otherwise.
948 * The sample value of "var" is assumed to be non-negative when the
949 * the function is called and will be made non-negative again before
950 * the function returns.
952 int isl_tab_min_at_most_neg_one(struct isl_tab *tab, struct isl_tab_var *var)
955 struct isl_tab_var *pivot_var;
957 if (min_is_manifestly_unbounded(tab, var))
961 row = pivot_row(tab, NULL, -1, col);
962 pivot_var = var_from_col(tab, col);
963 isl_tab_pivot(tab, row, col);
964 if (var->is_redundant)
966 if (row_at_most_neg_one(tab, var->index)) {
967 if (var->is_nonneg) {
968 if (!pivot_var->is_redundant &&
969 pivot_var->index == row)
970 isl_tab_pivot(tab, row, col);
972 restore_row(tab, var);
977 if (var->is_redundant)
980 find_pivot(tab, var, var, -1, &row, &col);
981 if (row == var->index)
985 pivot_var = var_from_col(tab, col);
986 isl_tab_pivot(tab, row, col);
987 if (var->is_redundant)
989 } while (!row_at_most_neg_one(tab, var->index));
990 if (var->is_nonneg) {
991 /* pivot back to non-negative value */
992 if (!pivot_var->is_redundant && pivot_var->index == row)
993 isl_tab_pivot(tab, row, col);
994 restore_row(tab, var);
999 /* Return 1 if "var" can attain values >= 1.
1000 * Return 0 otherwise.
1002 static int at_least_one(struct isl_tab *tab, struct isl_tab_var *var)
1007 if (max_is_manifestly_unbounded(tab, var))
1009 to_row(tab, var, 1);
1010 r = tab->mat->row[var->index];
1011 while (isl_int_lt(r[1], r[0])) {
1012 find_pivot(tab, var, var, 1, &row, &col);
1014 return isl_int_ge(r[1], r[0]);
1015 if (row == var->index) /* manifestly unbounded */
1017 isl_tab_pivot(tab, row, col);
1022 static void swap_cols(struct isl_tab *tab, int col1, int col2)
1025 unsigned off = 2 + tab->M;
1026 t = tab->col_var[col1];
1027 tab->col_var[col1] = tab->col_var[col2];
1028 tab->col_var[col2] = t;
1029 var_from_col(tab, col1)->index = col1;
1030 var_from_col(tab, col2)->index = col2;
1031 tab->mat = isl_mat_swap_cols(tab->mat, off + col1, off + col2);
1034 /* Mark column with index "col" as representing a zero variable.
1035 * If we may need to undo the operation the column is kept,
1036 * but no longer considered.
1037 * Otherwise, the column is simply removed.
1039 * The column may be interchanged with some other column. If it
1040 * is interchanged with a later column, return 1. Otherwise return 0.
1041 * If the columns are checked in order in the calling function,
1042 * then a return value of 1 means that the column with the given
1043 * column number may now contain a different column that
1044 * hasn't been checked yet.
1046 int isl_tab_kill_col(struct isl_tab *tab, int col)
1048 var_from_col(tab, col)->is_zero = 1;
1049 if (tab->need_undo) {
1050 isl_tab_push_var(tab, isl_tab_undo_zero, var_from_col(tab, col));
1051 if (col != tab->n_dead)
1052 swap_cols(tab, col, tab->n_dead);
1056 if (col != tab->n_col - 1)
1057 swap_cols(tab, col, tab->n_col - 1);
1058 var_from_col(tab, tab->n_col - 1)->index = -1;
1064 /* Row variable "var" is non-negative and cannot attain any values
1065 * larger than zero. This means that the coefficients of the unrestricted
1066 * column variables are zero and that the coefficients of the non-negative
1067 * column variables are zero or negative.
1068 * Each of the non-negative variables with a negative coefficient can
1069 * then also be written as the negative sum of non-negative variables
1070 * and must therefore also be zero.
1072 static void close_row(struct isl_tab *tab, struct isl_tab_var *var)
1075 struct isl_mat *mat = tab->mat;
1076 unsigned off = 2 + tab->M;
1078 isl_assert(tab->mat->ctx, var->is_nonneg, return);
1081 isl_tab_push_var(tab, isl_tab_undo_zero, var);
1082 for (j = tab->n_dead; j < tab->n_col; ++j) {
1083 if (isl_int_is_zero(mat->row[var->index][off + j]))
1085 isl_assert(tab->mat->ctx,
1086 isl_int_is_neg(mat->row[var->index][off + j]), return);
1087 if (isl_tab_kill_col(tab, j))
1090 isl_tab_mark_redundant(tab, var->index);
1093 /* Add a constraint to the tableau and allocate a row for it.
1094 * Return the index into the constraint array "con".
1096 int isl_tab_allocate_con(struct isl_tab *tab)
1100 isl_assert(tab->mat->ctx, tab->n_row < tab->mat->n_row, return -1);
1101 isl_assert(tab->mat->ctx, tab->n_con < tab->max_con, return -1);
1104 tab->con[r].index = tab->n_row;
1105 tab->con[r].is_row = 1;
1106 tab->con[r].is_nonneg = 0;
1107 tab->con[r].is_zero = 0;
1108 tab->con[r].is_redundant = 0;
1109 tab->con[r].frozen = 0;
1110 tab->con[r].negated = 0;
1111 tab->row_var[tab->n_row] = ~r;
1115 isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->con[r]);
1120 /* Add a variable to the tableau and allocate a column for it.
1121 * Return the index into the variable array "var".
1123 int isl_tab_allocate_var(struct isl_tab *tab)
1127 unsigned off = 2 + tab->M;
1129 isl_assert(tab->mat->ctx, tab->n_col < tab->mat->n_col, return -1);
1130 isl_assert(tab->mat->ctx, tab->n_var < tab->max_var, return -1);
1133 tab->var[r].index = tab->n_col;
1134 tab->var[r].is_row = 0;
1135 tab->var[r].is_nonneg = 0;
1136 tab->var[r].is_zero = 0;
1137 tab->var[r].is_redundant = 0;
1138 tab->var[r].frozen = 0;
1139 tab->var[r].negated = 0;
1140 tab->col_var[tab->n_col] = r;
1142 for (i = 0; i < tab->n_row; ++i)
1143 isl_int_set_si(tab->mat->row[i][off + tab->n_col], 0);
1147 isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->var[r]);
1152 /* Add a row to the tableau. The row is given as an affine combination
1153 * of the original variables and needs to be expressed in terms of the
1156 * We add each term in turn.
1157 * If r = n/d_r is the current sum and we need to add k x, then
1158 * if x is a column variable, we increase the numerator of
1159 * this column by k d_r
1160 * if x = f/d_x is a row variable, then the new representation of r is
1162 * n k f d_x/g n + d_r/g k f m/d_r n + m/d_g k f
1163 * --- + --- = ------------------- = -------------------
1164 * d_r d_r d_r d_x/g m
1166 * with g the gcd of d_r and d_x and m the lcm of d_r and d_x.
1168 int isl_tab_add_row(struct isl_tab *tab, isl_int *line)
1174 unsigned off = 2 + tab->M;
1176 r = isl_tab_allocate_con(tab);
1182 row = tab->mat->row[tab->con[r].index];
1183 isl_int_set_si(row[0], 1);
1184 isl_int_set(row[1], line[0]);
1185 isl_seq_clr(row + 2, tab->M + tab->n_col);
1186 for (i = 0; i < tab->n_var; ++i) {
1187 if (tab->var[i].is_zero)
1189 if (tab->var[i].is_row) {
1191 row[0], tab->mat->row[tab->var[i].index][0]);
1192 isl_int_swap(a, row[0]);
1193 isl_int_divexact(a, row[0], a);
1195 row[0], tab->mat->row[tab->var[i].index][0]);
1196 isl_int_mul(b, b, line[1 + i]);
1197 isl_seq_combine(row + 1, a, row + 1,
1198 b, tab->mat->row[tab->var[i].index] + 1,
1199 1 + tab->M + tab->n_col);
1201 isl_int_addmul(row[off + tab->var[i].index],
1202 line[1 + i], row[0]);
1203 if (tab->M && i >= tab->n_param && i < tab->n_var - tab->n_div)
1204 isl_int_submul(row[2], line[1 + i], row[0]);
1206 isl_seq_normalize(tab->mat->ctx, row, off + tab->n_col);
1211 tab->row_sign[tab->con[r].index] = 0;
1216 static int drop_row(struct isl_tab *tab, int row)
1218 isl_assert(tab->mat->ctx, ~tab->row_var[row] == tab->n_con - 1, return -1);
1219 if (row != tab->n_row - 1)
1220 swap_rows(tab, row, tab->n_row - 1);
1226 static int drop_col(struct isl_tab *tab, int col)
1228 isl_assert(tab->mat->ctx, tab->col_var[col] == tab->n_var - 1, return -1);
1229 if (col != tab->n_col - 1)
1230 swap_cols(tab, col, tab->n_col - 1);
1236 /* Add inequality "ineq" and check if it conflicts with the
1237 * previously added constraints or if it is obviously redundant.
1239 struct isl_tab *isl_tab_add_ineq(struct isl_tab *tab, isl_int *ineq)
1246 r = isl_tab_add_row(tab, ineq);
1249 tab->con[r].is_nonneg = 1;
1250 isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]);
1251 if (isl_tab_row_is_redundant(tab, tab->con[r].index)) {
1252 isl_tab_mark_redundant(tab, tab->con[r].index);
1256 sgn = restore_row(tab, &tab->con[r]);
1258 return isl_tab_mark_empty(tab);
1259 if (tab->con[r].is_row && isl_tab_row_is_redundant(tab, tab->con[r].index))
1260 isl_tab_mark_redundant(tab, tab->con[r].index);
1267 /* Pivot a non-negative variable down until it reaches the value zero
1268 * and then pivot the variable into a column position.
1270 static int to_col(struct isl_tab *tab, struct isl_tab_var *var)
1274 unsigned off = 2 + tab->M;
1279 while (isl_int_is_pos(tab->mat->row[var->index][1])) {
1280 find_pivot(tab, var, NULL, -1, &row, &col);
1281 isl_assert(tab->mat->ctx, row != -1, return -1);
1282 isl_tab_pivot(tab, row, col);
1287 for (i = tab->n_dead; i < tab->n_col; ++i)
1288 if (!isl_int_is_zero(tab->mat->row[var->index][off + i]))
1291 isl_assert(tab->mat->ctx, i < tab->n_col, return -1);
1292 isl_tab_pivot(tab, var->index, i);
1297 /* We assume Gaussian elimination has been performed on the equalities.
1298 * The equalities can therefore never conflict.
1299 * Adding the equalities is currently only really useful for a later call
1300 * to isl_tab_ineq_type.
1302 static struct isl_tab *add_eq(struct isl_tab *tab, isl_int *eq)
1309 r = isl_tab_add_row(tab, eq);
1313 r = tab->con[r].index;
1314 i = isl_seq_first_non_zero(tab->mat->row[r] + 2 + tab->M + tab->n_dead,
1315 tab->n_col - tab->n_dead);
1316 isl_assert(tab->mat->ctx, i >= 0, goto error);
1318 isl_tab_pivot(tab, r, i);
1319 isl_tab_kill_col(tab, i);
1328 /* Add an equality that is known to be valid for the given tableau.
1330 struct isl_tab *isl_tab_add_valid_eq(struct isl_tab *tab, isl_int *eq)
1332 struct isl_tab_var *var;
1337 r = isl_tab_add_row(tab, eq);
1343 if (isl_int_is_neg(tab->mat->row[r][1])) {
1344 isl_seq_neg(tab->mat->row[r] + 1, tab->mat->row[r] + 1,
1349 if (to_col(tab, var) < 0)
1352 isl_tab_kill_col(tab, var->index);
1360 struct isl_tab *isl_tab_from_basic_map(struct isl_basic_map *bmap)
1363 struct isl_tab *tab;
1367 tab = isl_tab_alloc(bmap->ctx,
1368 isl_basic_map_total_dim(bmap) + bmap->n_ineq + 1,
1369 isl_basic_map_total_dim(bmap), 0);
1372 tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
1373 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1374 return isl_tab_mark_empty(tab);
1375 for (i = 0; i < bmap->n_eq; ++i) {
1376 tab = add_eq(tab, bmap->eq[i]);
1380 for (i = 0; i < bmap->n_ineq; ++i) {
1381 tab = isl_tab_add_ineq(tab, bmap->ineq[i]);
1382 if (!tab || tab->empty)
1388 struct isl_tab *isl_tab_from_basic_set(struct isl_basic_set *bset)
1390 return isl_tab_from_basic_map((struct isl_basic_map *)bset);
1393 /* Construct a tableau corresponding to the recession cone of "bset".
1395 struct isl_tab *isl_tab_from_recession_cone(struct isl_basic_set *bset)
1399 struct isl_tab *tab;
1403 tab = isl_tab_alloc(bset->ctx, bset->n_eq + bset->n_ineq,
1404 isl_basic_set_total_dim(bset), 0);
1407 tab->rational = ISL_F_ISSET(bset, ISL_BASIC_SET_RATIONAL);
1410 for (i = 0; i < bset->n_eq; ++i) {
1411 isl_int_swap(bset->eq[i][0], cst);
1412 tab = add_eq(tab, bset->eq[i]);
1413 isl_int_swap(bset->eq[i][0], cst);
1417 for (i = 0; i < bset->n_ineq; ++i) {
1419 isl_int_swap(bset->ineq[i][0], cst);
1420 r = isl_tab_add_row(tab, bset->ineq[i]);
1421 isl_int_swap(bset->ineq[i][0], cst);
1424 tab->con[r].is_nonneg = 1;
1425 isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]);
1436 /* Assuming "tab" is the tableau of a cone, check if the cone is
1437 * bounded, i.e., if it is empty or only contains the origin.
1439 int isl_tab_cone_is_bounded(struct isl_tab *tab)
1447 if (tab->n_dead == tab->n_col)
1451 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1452 struct isl_tab_var *var;
1453 var = isl_tab_var_from_row(tab, i);
1454 if (!var->is_nonneg)
1456 if (sign_of_max(tab, var) != 0)
1458 close_row(tab, var);
1461 if (tab->n_dead == tab->n_col)
1463 if (i == tab->n_row)
1468 int isl_tab_sample_is_integer(struct isl_tab *tab)
1475 for (i = 0; i < tab->n_var; ++i) {
1477 if (!tab->var[i].is_row)
1479 row = tab->var[i].index;
1480 if (!isl_int_is_divisible_by(tab->mat->row[row][1],
1481 tab->mat->row[row][0]))
1487 static struct isl_vec *extract_integer_sample(struct isl_tab *tab)
1490 struct isl_vec *vec;
1492 vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
1496 isl_int_set_si(vec->block.data[0], 1);
1497 for (i = 0; i < tab->n_var; ++i) {
1498 if (!tab->var[i].is_row)
1499 isl_int_set_si(vec->block.data[1 + i], 0);
1501 int row = tab->var[i].index;
1502 isl_int_divexact(vec->block.data[1 + i],
1503 tab->mat->row[row][1], tab->mat->row[row][0]);
1510 struct isl_vec *isl_tab_get_sample_value(struct isl_tab *tab)
1513 struct isl_vec *vec;
1519 vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
1525 isl_int_set_si(vec->block.data[0], 1);
1526 for (i = 0; i < tab->n_var; ++i) {
1528 if (!tab->var[i].is_row) {
1529 isl_int_set_si(vec->block.data[1 + i], 0);
1532 row = tab->var[i].index;
1533 isl_int_gcd(m, vec->block.data[0], tab->mat->row[row][0]);
1534 isl_int_divexact(m, tab->mat->row[row][0], m);
1535 isl_seq_scale(vec->block.data, vec->block.data, m, 1 + i);
1536 isl_int_divexact(m, vec->block.data[0], tab->mat->row[row][0]);
1537 isl_int_mul(vec->block.data[1 + i], m, tab->mat->row[row][1]);
1539 vec = isl_vec_normalize(vec);
1545 /* Update "bmap" based on the results of the tableau "tab".
1546 * In particular, implicit equalities are made explicit, redundant constraints
1547 * are removed and if the sample value happens to be integer, it is stored
1548 * in "bmap" (unless "bmap" already had an integer sample).
1550 * The tableau is assumed to have been created from "bmap" using
1551 * isl_tab_from_basic_map.
1553 struct isl_basic_map *isl_basic_map_update_from_tab(struct isl_basic_map *bmap,
1554 struct isl_tab *tab)
1566 bmap = isl_basic_map_set_to_empty(bmap);
1568 for (i = bmap->n_ineq - 1; i >= 0; --i) {
1569 if (isl_tab_is_equality(tab, n_eq + i))
1570 isl_basic_map_inequality_to_equality(bmap, i);
1571 else if (isl_tab_is_redundant(tab, n_eq + i))
1572 isl_basic_map_drop_inequality(bmap, i);
1574 if (!tab->rational &&
1575 !bmap->sample && isl_tab_sample_is_integer(tab))
1576 bmap->sample = extract_integer_sample(tab);
1580 struct isl_basic_set *isl_basic_set_update_from_tab(struct isl_basic_set *bset,
1581 struct isl_tab *tab)
1583 return (struct isl_basic_set *)isl_basic_map_update_from_tab(
1584 (struct isl_basic_map *)bset, tab);
1587 /* Given a non-negative variable "var", add a new non-negative variable
1588 * that is the opposite of "var", ensuring that var can only attain the
1590 * If var = n/d is a row variable, then the new variable = -n/d.
1591 * If var is a column variables, then the new variable = -var.
1592 * If the new variable cannot attain non-negative values, then
1593 * the resulting tableau is empty.
1594 * Otherwise, we know the value will be zero and we close the row.
1596 static struct isl_tab *cut_to_hyperplane(struct isl_tab *tab,
1597 struct isl_tab_var *var)
1602 unsigned off = 2 + tab->M;
1606 isl_assert(tab->mat->ctx, !var->is_redundant, goto error);
1608 if (isl_tab_extend_cons(tab, 1) < 0)
1612 tab->con[r].index = tab->n_row;
1613 tab->con[r].is_row = 1;
1614 tab->con[r].is_nonneg = 0;
1615 tab->con[r].is_zero = 0;
1616 tab->con[r].is_redundant = 0;
1617 tab->con[r].frozen = 0;
1618 tab->con[r].negated = 0;
1619 tab->row_var[tab->n_row] = ~r;
1620 row = tab->mat->row[tab->n_row];
1623 isl_int_set(row[0], tab->mat->row[var->index][0]);
1624 isl_seq_neg(row + 1,
1625 tab->mat->row[var->index] + 1, 1 + tab->n_col);
1627 isl_int_set_si(row[0], 1);
1628 isl_seq_clr(row + 1, 1 + tab->n_col);
1629 isl_int_set_si(row[off + var->index], -1);
1634 isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->con[r]);
1636 sgn = sign_of_max(tab, &tab->con[r]);
1638 return isl_tab_mark_empty(tab);
1639 tab->con[r].is_nonneg = 1;
1640 isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]);
1642 close_row(tab, &tab->con[r]);
1650 /* Given a tableau "tab" and an inequality constraint "con" of the tableau,
1651 * relax the inequality by one. That is, the inequality r >= 0 is replaced
1652 * by r' = r + 1 >= 0.
1653 * If r is a row variable, we simply increase the constant term by one
1654 * (taking into account the denominator).
1655 * If r is a column variable, then we need to modify each row that
1656 * refers to r = r' - 1 by substituting this equality, effectively
1657 * subtracting the coefficient of the column from the constant.
1659 struct isl_tab *isl_tab_relax(struct isl_tab *tab, int con)
1661 struct isl_tab_var *var;
1662 unsigned off = 2 + tab->M;
1667 var = &tab->con[con];
1669 if (!var->is_row && !max_is_manifestly_unbounded(tab, var))
1670 to_row(tab, var, 1);
1673 isl_int_add(tab->mat->row[var->index][1],
1674 tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
1678 for (i = 0; i < tab->n_row; ++i) {
1679 if (isl_int_is_zero(tab->mat->row[i][off + var->index]))
1681 isl_int_sub(tab->mat->row[i][1], tab->mat->row[i][1],
1682 tab->mat->row[i][off + var->index]);
1687 isl_tab_push_var(tab, isl_tab_undo_relax, var);
1692 struct isl_tab *isl_tab_select_facet(struct isl_tab *tab, int con)
1697 return cut_to_hyperplane(tab, &tab->con[con]);
1700 static int may_be_equality(struct isl_tab *tab, int row)
1702 unsigned off = 2 + tab->M;
1703 return (tab->rational ? isl_int_is_zero(tab->mat->row[row][1])
1704 : isl_int_lt(tab->mat->row[row][1],
1705 tab->mat->row[row][0])) &&
1706 isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
1707 tab->n_col - tab->n_dead) != -1;
1710 /* Check for (near) equalities among the constraints.
1711 * A constraint is an equality if it is non-negative and if
1712 * its maximal value is either
1713 * - zero (in case of rational tableaus), or
1714 * - strictly less than 1 (in case of integer tableaus)
1716 * We first mark all non-redundant and non-dead variables that
1717 * are not frozen and not obviously not an equality.
1718 * Then we iterate over all marked variables if they can attain
1719 * any values larger than zero or at least one.
1720 * If the maximal value is zero, we mark any column variables
1721 * that appear in the row as being zero and mark the row as being redundant.
1722 * Otherwise, if the maximal value is strictly less than one (and the
1723 * tableau is integer), then we restrict the value to being zero
1724 * by adding an opposite non-negative variable.
1726 struct isl_tab *isl_tab_detect_equalities(struct isl_tab *tab)
1735 if (tab->n_dead == tab->n_col)
1739 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1740 struct isl_tab_var *var = isl_tab_var_from_row(tab, i);
1741 var->marked = !var->frozen && var->is_nonneg &&
1742 may_be_equality(tab, i);
1746 for (i = tab->n_dead; i < tab->n_col; ++i) {
1747 struct isl_tab_var *var = var_from_col(tab, i);
1748 var->marked = !var->frozen && var->is_nonneg;
1753 struct isl_tab_var *var;
1754 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1755 var = isl_tab_var_from_row(tab, i);
1759 if (i == tab->n_row) {
1760 for (i = tab->n_dead; i < tab->n_col; ++i) {
1761 var = var_from_col(tab, i);
1765 if (i == tab->n_col)
1770 if (sign_of_max(tab, var) == 0)
1771 close_row(tab, var);
1772 else if (!tab->rational && !at_least_one(tab, var)) {
1773 tab = cut_to_hyperplane(tab, var);
1774 return isl_tab_detect_equalities(tab);
1776 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1777 var = isl_tab_var_from_row(tab, i);
1780 if (may_be_equality(tab, i))
1790 /* Check for (near) redundant constraints.
1791 * A constraint is redundant if it is non-negative and if
1792 * its minimal value (temporarily ignoring the non-negativity) is either
1793 * - zero (in case of rational tableaus), or
1794 * - strictly larger than -1 (in case of integer tableaus)
1796 * We first mark all non-redundant and non-dead variables that
1797 * are not frozen and not obviously negatively unbounded.
1798 * Then we iterate over all marked variables if they can attain
1799 * any values smaller than zero or at most negative one.
1800 * If not, we mark the row as being redundant (assuming it hasn't
1801 * been detected as being obviously redundant in the mean time).
1803 struct isl_tab *isl_tab_detect_redundant(struct isl_tab *tab)
1812 if (tab->n_redundant == tab->n_row)
1816 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1817 struct isl_tab_var *var = isl_tab_var_from_row(tab, i);
1818 var->marked = !var->frozen && var->is_nonneg;
1822 for (i = tab->n_dead; i < tab->n_col; ++i) {
1823 struct isl_tab_var *var = var_from_col(tab, i);
1824 var->marked = !var->frozen && var->is_nonneg &&
1825 !min_is_manifestly_unbounded(tab, var);
1830 struct isl_tab_var *var;
1831 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1832 var = isl_tab_var_from_row(tab, i);
1836 if (i == tab->n_row) {
1837 for (i = tab->n_dead; i < tab->n_col; ++i) {
1838 var = var_from_col(tab, i);
1842 if (i == tab->n_col)
1847 if ((tab->rational ? (sign_of_min(tab, var) >= 0)
1848 : !isl_tab_min_at_most_neg_one(tab, var)) &&
1850 isl_tab_mark_redundant(tab, var->index);
1851 for (i = tab->n_dead; i < tab->n_col; ++i) {
1852 var = var_from_col(tab, i);
1855 if (!min_is_manifestly_unbounded(tab, var))
1865 int isl_tab_is_equality(struct isl_tab *tab, int con)
1872 if (tab->con[con].is_zero)
1874 if (tab->con[con].is_redundant)
1876 if (!tab->con[con].is_row)
1877 return tab->con[con].index < tab->n_dead;
1879 row = tab->con[con].index;
1882 return isl_int_is_zero(tab->mat->row[row][1]) &&
1883 isl_seq_first_non_zero(tab->mat->row[row] + 2 + tab->n_dead,
1884 tab->n_col - tab->n_dead) == -1;
1887 /* Return the minimial value of the affine expression "f" with denominator
1888 * "denom" in *opt, *opt_denom, assuming the tableau is not empty and
1889 * the expression cannot attain arbitrarily small values.
1890 * If opt_denom is NULL, then *opt is rounded up to the nearest integer.
1891 * The return value reflects the nature of the result (empty, unbounded,
1892 * minmimal value returned in *opt).
1894 enum isl_lp_result isl_tab_min(struct isl_tab *tab,
1895 isl_int *f, isl_int denom, isl_int *opt, isl_int *opt_denom,
1899 enum isl_lp_result res = isl_lp_ok;
1900 struct isl_tab_var *var;
1901 struct isl_tab_undo *snap;
1904 return isl_lp_empty;
1906 snap = isl_tab_snap(tab);
1907 r = isl_tab_add_row(tab, f);
1909 return isl_lp_error;
1911 isl_int_mul(tab->mat->row[var->index][0],
1912 tab->mat->row[var->index][0], denom);
1915 find_pivot(tab, var, var, -1, &row, &col);
1916 if (row == var->index) {
1917 res = isl_lp_unbounded;
1922 isl_tab_pivot(tab, row, col);
1924 if (ISL_FL_ISSET(flags, ISL_TAB_SAVE_DUAL)) {
1927 isl_vec_free(tab->dual);
1928 tab->dual = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_con);
1930 return isl_lp_error;
1931 isl_int_set(tab->dual->el[0], tab->mat->row[var->index][0]);
1932 for (i = 0; i < tab->n_con; ++i) {
1934 if (tab->con[i].is_row) {
1935 isl_int_set_si(tab->dual->el[1 + i], 0);
1938 pos = 2 + tab->M + tab->con[i].index;
1939 if (tab->con[i].negated)
1940 isl_int_neg(tab->dual->el[1 + i],
1941 tab->mat->row[var->index][pos]);
1943 isl_int_set(tab->dual->el[1 + i],
1944 tab->mat->row[var->index][pos]);
1947 if (opt && res == isl_lp_ok) {
1949 isl_int_set(*opt, tab->mat->row[var->index][1]);
1950 isl_int_set(*opt_denom, tab->mat->row[var->index][0]);
1952 isl_int_cdiv_q(*opt, tab->mat->row[var->index][1],
1953 tab->mat->row[var->index][0]);
1955 if (isl_tab_rollback(tab, snap) < 0)
1956 return isl_lp_error;
1960 int isl_tab_is_redundant(struct isl_tab *tab, int con)
1964 if (tab->con[con].is_zero)
1966 if (tab->con[con].is_redundant)
1968 return tab->con[con].is_row && tab->con[con].index < tab->n_redundant;
1971 /* Take a snapshot of the tableau that can be restored by s call to
1974 struct isl_tab_undo *isl_tab_snap(struct isl_tab *tab)
1982 /* Undo the operation performed by isl_tab_relax.
1984 static void unrelax(struct isl_tab *tab, struct isl_tab_var *var)
1986 unsigned off = 2 + tab->M;
1988 if (!var->is_row && !max_is_manifestly_unbounded(tab, var))
1989 to_row(tab, var, 1);
1992 isl_int_sub(tab->mat->row[var->index][1],
1993 tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
1997 for (i = 0; i < tab->n_row; ++i) {
1998 if (isl_int_is_zero(tab->mat->row[i][off + var->index]))
2000 isl_int_add(tab->mat->row[i][1], tab->mat->row[i][1],
2001 tab->mat->row[i][off + var->index]);
2007 static void perform_undo_var(struct isl_tab *tab, struct isl_tab_undo *undo)
2009 struct isl_tab_var *var = var_from_index(tab, undo->u.var_index);
2010 switch(undo->type) {
2011 case isl_tab_undo_nonneg:
2014 case isl_tab_undo_redundant:
2015 var->is_redundant = 0;
2018 case isl_tab_undo_zero:
2023 case isl_tab_undo_allocate:
2024 if (undo->u.var_index >= 0) {
2025 isl_assert(tab->mat->ctx, !var->is_row, return);
2026 drop_col(tab, var->index);
2030 if (!max_is_manifestly_unbounded(tab, var))
2031 to_row(tab, var, 1);
2032 else if (!min_is_manifestly_unbounded(tab, var))
2033 to_row(tab, var, -1);
2035 to_row(tab, var, 0);
2037 drop_row(tab, var->index);
2039 case isl_tab_undo_relax:
2045 /* Restore the tableau to the state where the basic variables
2046 * are those in "col_var".
2047 * We first construct a list of variables that are currently in
2048 * the basis, but shouldn't. Then we iterate over all variables
2049 * that should be in the basis and for each one that is currently
2050 * not in the basis, we exchange it with one of the elements of the
2051 * list constructed before.
2052 * We can always find an appropriate variable to pivot with because
2053 * the current basis is mapped to the old basis by a non-singular
2054 * matrix and so we can never end up with a zero row.
2056 static int restore_basis(struct isl_tab *tab, int *col_var)
2060 int *extra = NULL; /* current columns that contain bad stuff */
2061 unsigned off = 2 + tab->M;
2063 extra = isl_alloc_array(tab->mat->ctx, int, tab->n_col);
2066 for (i = 0; i < tab->n_col; ++i) {
2067 for (j = 0; j < tab->n_col; ++j)
2068 if (tab->col_var[i] == col_var[j])
2072 extra[n_extra++] = i;
2074 for (i = 0; i < tab->n_col && n_extra > 0; ++i) {
2075 struct isl_tab_var *var;
2078 for (j = 0; j < tab->n_col; ++j)
2079 if (col_var[i] == tab->col_var[j])
2083 var = var_from_index(tab, col_var[i]);
2085 for (j = 0; j < n_extra; ++j)
2086 if (!isl_int_is_zero(tab->mat->row[row][off+extra[j]]))
2088 isl_assert(tab->mat->ctx, j < n_extra, goto error);
2089 isl_tab_pivot(tab, row, extra[j]);
2090 extra[j] = extra[--n_extra];
2102 static int perform_undo(struct isl_tab *tab, struct isl_tab_undo *undo)
2104 switch (undo->type) {
2105 case isl_tab_undo_empty:
2108 case isl_tab_undo_nonneg:
2109 case isl_tab_undo_redundant:
2110 case isl_tab_undo_zero:
2111 case isl_tab_undo_allocate:
2112 case isl_tab_undo_relax:
2113 perform_undo_var(tab, undo);
2115 case isl_tab_undo_bset_eq:
2116 isl_basic_set_free_equality(tab->bset, 1);
2118 case isl_tab_undo_bset_ineq:
2119 isl_basic_set_free_inequality(tab->bset, 1);
2121 case isl_tab_undo_bset_div:
2122 isl_basic_set_free_div(tab->bset, 1);
2124 tab->samples->n_col--;
2126 case isl_tab_undo_saved_basis:
2127 if (restore_basis(tab, undo->u.col_var) < 0)
2130 case isl_tab_undo_drop_sample:
2134 isl_assert(tab->mat->ctx, 0, return -1);
2139 /* Return the tableau to the state it was in when the snapshot "snap"
2142 int isl_tab_rollback(struct isl_tab *tab, struct isl_tab_undo *snap)
2144 struct isl_tab_undo *undo, *next;
2150 for (undo = tab->top; undo && undo != &tab->bottom; undo = next) {
2154 if (perform_undo(tab, undo) < 0) {
2168 /* The given row "row" represents an inequality violated by all
2169 * points in the tableau. Check for some special cases of such
2170 * separating constraints.
2171 * In particular, if the row has been reduced to the constant -1,
2172 * then we know the inequality is adjacent (but opposite) to
2173 * an equality in the tableau.
2174 * If the row has been reduced to r = -1 -r', with r' an inequality
2175 * of the tableau, then the inequality is adjacent (but opposite)
2176 * to the inequality r'.
2178 static enum isl_ineq_type separation_type(struct isl_tab *tab, unsigned row)
2181 unsigned off = 2 + tab->M;
2184 return isl_ineq_separate;
2186 if (!isl_int_is_one(tab->mat->row[row][0]))
2187 return isl_ineq_separate;
2188 if (!isl_int_is_negone(tab->mat->row[row][1]))
2189 return isl_ineq_separate;
2191 pos = isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
2192 tab->n_col - tab->n_dead);
2194 return isl_ineq_adj_eq;
2196 if (!isl_int_is_negone(tab->mat->row[row][off + tab->n_dead + pos]))
2197 return isl_ineq_separate;
2199 pos = isl_seq_first_non_zero(
2200 tab->mat->row[row] + off + tab->n_dead + pos + 1,
2201 tab->n_col - tab->n_dead - pos - 1);
2203 return pos == -1 ? isl_ineq_adj_ineq : isl_ineq_separate;
2206 /* Check the effect of inequality "ineq" on the tableau "tab".
2208 * isl_ineq_redundant: satisfied by all points in the tableau
2209 * isl_ineq_separate: satisfied by no point in the tableau
2210 * isl_ineq_cut: satisfied by some by not all points
2211 * isl_ineq_adj_eq: adjacent to an equality
2212 * isl_ineq_adj_ineq: adjacent to an inequality.
2214 enum isl_ineq_type isl_tab_ineq_type(struct isl_tab *tab, isl_int *ineq)
2216 enum isl_ineq_type type = isl_ineq_error;
2217 struct isl_tab_undo *snap = NULL;
2222 return isl_ineq_error;
2224 if (isl_tab_extend_cons(tab, 1) < 0)
2225 return isl_ineq_error;
2227 snap = isl_tab_snap(tab);
2229 con = isl_tab_add_row(tab, ineq);
2233 row = tab->con[con].index;
2234 if (isl_tab_row_is_redundant(tab, row))
2235 type = isl_ineq_redundant;
2236 else if (isl_int_is_neg(tab->mat->row[row][1]) &&
2238 isl_int_abs_ge(tab->mat->row[row][1],
2239 tab->mat->row[row][0]))) {
2240 if (at_least_zero(tab, &tab->con[con]))
2241 type = isl_ineq_cut;
2243 type = separation_type(tab, row);
2244 } else if (tab->rational ? (sign_of_min(tab, &tab->con[con]) < 0)
2245 : isl_tab_min_at_most_neg_one(tab, &tab->con[con]))
2246 type = isl_ineq_cut;
2248 type = isl_ineq_redundant;
2250 if (isl_tab_rollback(tab, snap))
2251 return isl_ineq_error;
2254 isl_tab_rollback(tab, snap);
2255 return isl_ineq_error;
2258 void isl_tab_dump(struct isl_tab *tab, FILE *out, int indent)
2264 fprintf(out, "%*snull tab\n", indent, "");
2267 fprintf(out, "%*sn_redundant: %d, n_dead: %d", indent, "",
2268 tab->n_redundant, tab->n_dead);
2270 fprintf(out, ", rational");
2272 fprintf(out, ", empty");
2274 fprintf(out, "%*s[", indent, "");
2275 for (i = 0; i < tab->n_var; ++i) {
2277 fprintf(out, (i == tab->n_param ||
2278 i == tab->n_var - tab->n_div) ? "; "
2280 fprintf(out, "%c%d%s", tab->var[i].is_row ? 'r' : 'c',
2282 tab->var[i].is_zero ? " [=0]" :
2283 tab->var[i].is_redundant ? " [R]" : "");
2285 fprintf(out, "]\n");
2286 fprintf(out, "%*s[", indent, "");
2287 for (i = 0; i < tab->n_con; ++i) {
2290 fprintf(out, "%c%d%s", tab->con[i].is_row ? 'r' : 'c',
2292 tab->con[i].is_zero ? " [=0]" :
2293 tab->con[i].is_redundant ? " [R]" : "");
2295 fprintf(out, "]\n");
2296 fprintf(out, "%*s[", indent, "");
2297 for (i = 0; i < tab->n_row; ++i) {
2298 const char *sign = "";
2301 if (tab->row_sign) {
2302 if (tab->row_sign[i] == isl_tab_row_unknown)
2304 else if (tab->row_sign[i] == isl_tab_row_neg)
2306 else if (tab->row_sign[i] == isl_tab_row_pos)
2311 fprintf(out, "r%d: %d%s%s", i, tab->row_var[i],
2312 isl_tab_var_from_row(tab, i)->is_nonneg ? " [>=0]" : "", sign);
2314 fprintf(out, "]\n");
2315 fprintf(out, "%*s[", indent, "");
2316 for (i = 0; i < tab->n_col; ++i) {
2319 fprintf(out, "c%d: %d%s", i, tab->col_var[i],
2320 var_from_col(tab, i)->is_nonneg ? " [>=0]" : "");
2322 fprintf(out, "]\n");
2323 r = tab->mat->n_row;
2324 tab->mat->n_row = tab->n_row;
2325 c = tab->mat->n_col;
2326 tab->mat->n_col = 2 + tab->M + tab->n_col;
2327 isl_mat_dump(tab->mat, out, indent);
2328 tab->mat->n_row = r;
2329 tab->mat->n_col = c;
2331 isl_basic_set_dump(tab->bset, out, indent);