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;
64 tab->bottom.type = isl_tab_undo_bottom;
65 tab->bottom.next = NULL;
66 tab->top = &tab->bottom;
78 int isl_tab_extend_cons(struct isl_tab *tab, unsigned n_new)
80 unsigned off = 2 + tab->M;
85 if (tab->max_con < tab->n_con + n_new) {
86 struct isl_tab_var *con;
88 con = isl_realloc_array(tab->mat->ctx, tab->con,
89 struct isl_tab_var, tab->max_con + n_new);
93 tab->max_con += n_new;
95 if (tab->mat->n_row < tab->n_row + n_new) {
98 tab->mat = isl_mat_extend(tab->mat,
99 tab->n_row + n_new, off + tab->n_col);
102 row_var = isl_realloc_array(tab->mat->ctx, tab->row_var,
103 int, tab->mat->n_row);
106 tab->row_var = row_var;
108 enum isl_tab_row_sign *s;
109 s = isl_realloc_array(tab->mat->ctx, tab->row_sign,
110 enum isl_tab_row_sign, tab->mat->n_row);
119 /* Make room for at least n_new extra variables.
120 * Return -1 if anything went wrong.
122 int isl_tab_extend_vars(struct isl_tab *tab, unsigned n_new)
124 struct isl_tab_var *var;
125 unsigned off = 2 + tab->M;
127 if (tab->max_var < tab->n_var + n_new) {
128 var = isl_realloc_array(tab->mat->ctx, tab->var,
129 struct isl_tab_var, tab->n_var + n_new);
133 tab->max_var += n_new;
136 if (tab->mat->n_col < off + tab->n_col + n_new) {
139 tab->mat = isl_mat_extend(tab->mat,
140 tab->mat->n_row, off + tab->n_col + n_new);
143 p = isl_realloc_array(tab->mat->ctx, tab->col_var,
144 int, tab->n_col + n_new);
153 struct isl_tab *isl_tab_extend(struct isl_tab *tab, unsigned n_new)
155 if (isl_tab_extend_cons(tab, n_new) >= 0)
162 static void free_undo(struct isl_tab *tab)
164 struct isl_tab_undo *undo, *next;
166 for (undo = tab->top; undo && undo != &tab->bottom; undo = next) {
173 void isl_tab_free(struct isl_tab *tab)
178 isl_mat_free(tab->mat);
179 isl_vec_free(tab->dual);
180 isl_basic_set_free(tab->bset);
186 isl_mat_free(tab->samples);
187 free(tab->sample_index);
188 isl_mat_free(tab->basis);
192 struct isl_tab *isl_tab_dup(struct isl_tab *tab)
202 dup = isl_calloc_type(tab->ctx, struct isl_tab);
205 dup->mat = isl_mat_dup(tab->mat);
208 dup->var = isl_alloc_array(tab->ctx, struct isl_tab_var, tab->max_var);
211 for (i = 0; i < tab->n_var; ++i)
212 dup->var[i] = tab->var[i];
213 dup->con = isl_alloc_array(tab->ctx, struct isl_tab_var, tab->max_con);
216 for (i = 0; i < tab->n_con; ++i)
217 dup->con[i] = tab->con[i];
218 dup->col_var = isl_alloc_array(tab->ctx, int, tab->mat->n_col - off);
221 for (i = 0; i < tab->n_col; ++i)
222 dup->col_var[i] = tab->col_var[i];
223 dup->row_var = isl_alloc_array(tab->ctx, int, tab->mat->n_row);
226 for (i = 0; i < tab->n_row; ++i)
227 dup->row_var[i] = tab->row_var[i];
229 dup->row_sign = isl_alloc_array(tab->ctx, enum isl_tab_row_sign,
233 for (i = 0; i < tab->n_row; ++i)
234 dup->row_sign[i] = tab->row_sign[i];
237 dup->samples = isl_mat_dup(tab->samples);
240 dup->sample_index = isl_alloc_array(tab->mat->ctx, int,
241 tab->samples->n_row);
242 if (!dup->sample_index)
244 dup->n_sample = tab->n_sample;
245 dup->n_outside = tab->n_outside;
247 dup->n_row = tab->n_row;
248 dup->n_con = tab->n_con;
249 dup->n_eq = tab->n_eq;
250 dup->max_con = tab->max_con;
251 dup->n_col = tab->n_col;
252 dup->n_var = tab->n_var;
253 dup->max_var = tab->max_var;
254 dup->n_param = tab->n_param;
255 dup->n_div = tab->n_div;
256 dup->n_dead = tab->n_dead;
257 dup->n_redundant = tab->n_redundant;
258 dup->rational = tab->rational;
259 dup->empty = tab->empty;
263 tab->cone = tab->cone;
264 dup->bottom.type = isl_tab_undo_bottom;
265 dup->bottom.next = NULL;
266 dup->top = &dup->bottom;
268 dup->n_zero = tab->n_zero;
269 dup->n_unbounded = tab->n_unbounded;
270 dup->basis = isl_mat_dup(tab->basis);
278 /* Construct the coefficient matrix of the product tableau
280 * mat{1,2} is the coefficient matrix of tableau {1,2}
281 * row{1,2} is the number of rows in tableau {1,2}
282 * col{1,2} is the number of columns in tableau {1,2}
283 * off is the offset to the coefficient column (skipping the
284 * denominator, the constant term and the big parameter if any)
285 * r{1,2} is the number of redundant rows in tableau {1,2}
286 * d{1,2} is the number of dead columns in tableau {1,2}
288 * The order of the rows and columns in the result is as explained
289 * in isl_tab_product.
291 static struct isl_mat *tab_mat_product(struct isl_mat *mat1,
292 struct isl_mat *mat2, unsigned row1, unsigned row2,
293 unsigned col1, unsigned col2,
294 unsigned off, unsigned r1, unsigned r2, unsigned d1, unsigned d2)
297 struct isl_mat *prod;
300 prod = isl_mat_alloc(mat1->ctx, mat1->n_row + mat2->n_row,
304 for (i = 0; i < r1; ++i) {
305 isl_seq_cpy(prod->row[n + i], mat1->row[i], off + d1);
306 isl_seq_clr(prod->row[n + i] + off + d1, d2);
307 isl_seq_cpy(prod->row[n + i] + off + d1 + d2,
308 mat1->row[i] + off + d1, col1 - d1);
309 isl_seq_clr(prod->row[n + i] + off + col1 + d1, col2 - d2);
313 for (i = 0; i < r2; ++i) {
314 isl_seq_cpy(prod->row[n + i], mat2->row[i], off);
315 isl_seq_clr(prod->row[n + i] + off, d1);
316 isl_seq_cpy(prod->row[n + i] + off + d1,
317 mat2->row[i] + off, d2);
318 isl_seq_clr(prod->row[n + i] + off + d1 + d2, col1 - d1);
319 isl_seq_cpy(prod->row[n + i] + off + col1 + d1,
320 mat2->row[i] + off + d2, col2 - d2);
324 for (i = 0; i < row1 - r1; ++i) {
325 isl_seq_cpy(prod->row[n + i], mat1->row[r1 + i], off + d1);
326 isl_seq_clr(prod->row[n + i] + off + d1, d2);
327 isl_seq_cpy(prod->row[n + i] + off + d1 + d2,
328 mat1->row[r1 + i] + off + d1, col1 - d1);
329 isl_seq_clr(prod->row[n + i] + off + col1 + d1, col2 - d2);
333 for (i = 0; i < row2 - r2; ++i) {
334 isl_seq_cpy(prod->row[n + i], mat2->row[r2 + i], off);
335 isl_seq_clr(prod->row[n + i] + off, d1);
336 isl_seq_cpy(prod->row[n + i] + off + d1,
337 mat2->row[r2 + i] + off, d2);
338 isl_seq_clr(prod->row[n + i] + off + d1 + d2, col1 - d1);
339 isl_seq_cpy(prod->row[n + i] + off + col1 + d1,
340 mat2->row[r2 + i] + off + d2, col2 - d2);
346 /* Update the row or column index of a variable that corresponds
347 * to a variable in the first input tableau.
349 static void update_index1(struct isl_tab_var *var,
350 unsigned r1, unsigned r2, unsigned d1, unsigned d2)
352 if (var->index == -1)
354 if (var->is_row && var->index >= r1)
356 if (!var->is_row && var->index >= d1)
360 /* Update the row or column index of a variable that corresponds
361 * to a variable in the second input tableau.
363 static void update_index2(struct isl_tab_var *var,
364 unsigned row1, unsigned col1,
365 unsigned r1, unsigned r2, unsigned d1, unsigned d2)
367 if (var->index == -1)
382 /* Create a tableau that represents the Cartesian product of the sets
383 * represented by tableaus tab1 and tab2.
384 * The order of the rows in the product is
385 * - redundant rows of tab1
386 * - redundant rows of tab2
387 * - non-redundant rows of tab1
388 * - non-redundant rows of tab2
389 * The order of the columns is
392 * - coefficient of big parameter, if any
393 * - dead columns of tab1
394 * - dead columns of tab2
395 * - live columns of tab1
396 * - live columns of tab2
397 * The order of the variables and the constraints is a concatenation
398 * of order in the two input tableaus.
400 struct isl_tab *isl_tab_product(struct isl_tab *tab1, struct isl_tab *tab2)
403 struct isl_tab *prod;
405 unsigned r1, r2, d1, d2;
410 isl_assert(tab1->mat->ctx, tab1->M == tab2->M, return NULL);
411 isl_assert(tab1->mat->ctx, tab1->rational == tab2->rational, return NULL);
412 isl_assert(tab1->mat->ctx, tab1->cone == tab2->cone, return NULL);
413 isl_assert(tab1->mat->ctx, !tab1->row_sign, return NULL);
414 isl_assert(tab1->mat->ctx, !tab2->row_sign, return NULL);
415 isl_assert(tab1->mat->ctx, tab1->n_param == 0, return NULL);
416 isl_assert(tab1->mat->ctx, tab2->n_param == 0, return NULL);
417 isl_assert(tab1->mat->ctx, tab1->n_div == 0, return NULL);
418 isl_assert(tab1->mat->ctx, tab2->n_div == 0, return NULL);
421 r1 = tab1->n_redundant;
422 r2 = tab2->n_redundant;
425 prod = isl_calloc_type(tab1->mat->ctx, struct isl_tab);
428 prod->mat = tab_mat_product(tab1->mat, tab2->mat,
429 tab1->n_row, tab2->n_row,
430 tab1->n_col, tab2->n_col, off, r1, r2, d1, d2);
433 prod->var = isl_alloc_array(tab1->mat->ctx, struct isl_tab_var,
434 tab1->max_var + tab2->max_var);
437 for (i = 0; i < tab1->n_var; ++i) {
438 prod->var[i] = tab1->var[i];
439 update_index1(&prod->var[i], r1, r2, d1, d2);
441 for (i = 0; i < tab2->n_var; ++i) {
442 prod->var[tab1->n_var + i] = tab2->var[i];
443 update_index2(&prod->var[tab1->n_var + i],
444 tab1->n_row, tab1->n_col,
447 prod->con = isl_alloc_array(tab1->mat->ctx, struct isl_tab_var,
448 tab1->max_con + tab2->max_con);
451 for (i = 0; i < tab1->n_con; ++i) {
452 prod->con[i] = tab1->con[i];
453 update_index1(&prod->con[i], r1, r2, d1, d2);
455 for (i = 0; i < tab2->n_con; ++i) {
456 prod->con[tab1->n_con + i] = tab2->con[i];
457 update_index2(&prod->con[tab1->n_con + i],
458 tab1->n_row, tab1->n_col,
461 prod->col_var = isl_alloc_array(tab1->mat->ctx, int,
462 tab1->n_col + tab2->n_col);
465 for (i = 0; i < tab1->n_col; ++i) {
466 int pos = i < d1 ? i : i + d2;
467 prod->col_var[pos] = tab1->col_var[i];
469 for (i = 0; i < tab2->n_col; ++i) {
470 int pos = i < d2 ? d1 + i : tab1->n_col + i;
471 int t = tab2->col_var[i];
476 prod->col_var[pos] = t;
478 prod->row_var = isl_alloc_array(tab1->mat->ctx, int,
479 tab1->mat->n_row + tab2->mat->n_row);
482 for (i = 0; i < tab1->n_row; ++i) {
483 int pos = i < r1 ? i : i + r2;
484 prod->row_var[pos] = tab1->row_var[i];
486 for (i = 0; i < tab2->n_row; ++i) {
487 int pos = i < r2 ? r1 + i : tab1->n_row + i;
488 int t = tab2->row_var[i];
493 prod->row_var[pos] = t;
495 prod->samples = NULL;
496 prod->sample_index = NULL;
497 prod->n_row = tab1->n_row + tab2->n_row;
498 prod->n_con = tab1->n_con + tab2->n_con;
500 prod->max_con = tab1->max_con + tab2->max_con;
501 prod->n_col = tab1->n_col + tab2->n_col;
502 prod->n_var = tab1->n_var + tab2->n_var;
503 prod->max_var = tab1->max_var + tab2->max_var;
506 prod->n_dead = tab1->n_dead + tab2->n_dead;
507 prod->n_redundant = tab1->n_redundant + tab2->n_redundant;
508 prod->rational = tab1->rational;
509 prod->empty = tab1->empty || tab2->empty;
513 prod->cone = tab1->cone;
514 prod->bottom.type = isl_tab_undo_bottom;
515 prod->bottom.next = NULL;
516 prod->top = &prod->bottom;
519 prod->n_unbounded = 0;
528 static struct isl_tab_var *var_from_index(struct isl_tab *tab, int i)
533 return &tab->con[~i];
536 struct isl_tab_var *isl_tab_var_from_row(struct isl_tab *tab, int i)
538 return var_from_index(tab, tab->row_var[i]);
541 static struct isl_tab_var *var_from_col(struct isl_tab *tab, int i)
543 return var_from_index(tab, tab->col_var[i]);
546 /* Check if there are any upper bounds on column variable "var",
547 * i.e., non-negative rows where var appears with a negative coefficient.
548 * Return 1 if there are no such bounds.
550 static int max_is_manifestly_unbounded(struct isl_tab *tab,
551 struct isl_tab_var *var)
554 unsigned off = 2 + tab->M;
558 for (i = tab->n_redundant; i < tab->n_row; ++i) {
559 if (!isl_int_is_neg(tab->mat->row[i][off + var->index]))
561 if (isl_tab_var_from_row(tab, i)->is_nonneg)
567 /* Check if there are any lower bounds on column variable "var",
568 * i.e., non-negative rows where var appears with a positive coefficient.
569 * Return 1 if there are no such bounds.
571 static int min_is_manifestly_unbounded(struct isl_tab *tab,
572 struct isl_tab_var *var)
575 unsigned off = 2 + tab->M;
579 for (i = tab->n_redundant; i < tab->n_row; ++i) {
580 if (!isl_int_is_pos(tab->mat->row[i][off + var->index]))
582 if (isl_tab_var_from_row(tab, i)->is_nonneg)
588 static int row_cmp(struct isl_tab *tab, int r1, int r2, int c, isl_int t)
590 unsigned off = 2 + tab->M;
594 isl_int_mul(t, tab->mat->row[r1][2], tab->mat->row[r2][off+c]);
595 isl_int_submul(t, tab->mat->row[r2][2], tab->mat->row[r1][off+c]);
600 isl_int_mul(t, tab->mat->row[r1][1], tab->mat->row[r2][off + c]);
601 isl_int_submul(t, tab->mat->row[r2][1], tab->mat->row[r1][off + c]);
602 return isl_int_sgn(t);
605 /* Given the index of a column "c", return the index of a row
606 * that can be used to pivot the column in, with either an increase
607 * (sgn > 0) or a decrease (sgn < 0) of the corresponding variable.
608 * If "var" is not NULL, then the row returned will be different from
609 * the one associated with "var".
611 * Each row in the tableau is of the form
613 * x_r = a_r0 + \sum_i a_ri x_i
615 * Only rows with x_r >= 0 and with the sign of a_ri opposite to "sgn"
616 * impose any limit on the increase or decrease in the value of x_c
617 * and this bound is equal to a_r0 / |a_rc|. We are therefore looking
618 * for the row with the smallest (most stringent) such bound.
619 * Note that the common denominator of each row drops out of the fraction.
620 * To check if row j has a smaller bound than row r, i.e.,
621 * a_j0 / |a_jc| < a_r0 / |a_rc| or a_j0 |a_rc| < a_r0 |a_jc|,
622 * we check if -sign(a_jc) (a_j0 a_rc - a_r0 a_jc) < 0,
623 * where -sign(a_jc) is equal to "sgn".
625 static int pivot_row(struct isl_tab *tab,
626 struct isl_tab_var *var, int sgn, int c)
630 unsigned off = 2 + tab->M;
634 for (j = tab->n_redundant; j < tab->n_row; ++j) {
635 if (var && j == var->index)
637 if (!isl_tab_var_from_row(tab, j)->is_nonneg)
639 if (sgn * isl_int_sgn(tab->mat->row[j][off + c]) >= 0)
645 tsgn = sgn * row_cmp(tab, r, j, c, t);
646 if (tsgn < 0 || (tsgn == 0 &&
647 tab->row_var[j] < tab->row_var[r]))
654 /* Find a pivot (row and col) that will increase (sgn > 0) or decrease
655 * (sgn < 0) the value of row variable var.
656 * If not NULL, then skip_var is a row variable that should be ignored
657 * while looking for a pivot row. It is usually equal to var.
659 * As the given row in the tableau is of the form
661 * x_r = a_r0 + \sum_i a_ri x_i
663 * we need to find a column such that the sign of a_ri is equal to "sgn"
664 * (such that an increase in x_i will have the desired effect) or a
665 * column with a variable that may attain negative values.
666 * If a_ri is positive, then we need to move x_i in the same direction
667 * to obtain the desired effect. Otherwise, x_i has to move in the
668 * opposite direction.
670 static void find_pivot(struct isl_tab *tab,
671 struct isl_tab_var *var, struct isl_tab_var *skip_var,
672 int sgn, int *row, int *col)
679 isl_assert(tab->mat->ctx, var->is_row, return);
680 tr = tab->mat->row[var->index] + 2 + tab->M;
683 for (j = tab->n_dead; j < tab->n_col; ++j) {
684 if (isl_int_is_zero(tr[j]))
686 if (isl_int_sgn(tr[j]) != sgn &&
687 var_from_col(tab, j)->is_nonneg)
689 if (c < 0 || tab->col_var[j] < tab->col_var[c])
695 sgn *= isl_int_sgn(tr[c]);
696 r = pivot_row(tab, skip_var, sgn, c);
697 *row = r < 0 ? var->index : r;
701 /* Return 1 if row "row" represents an obviously redundant inequality.
703 * - it represents an inequality or a variable
704 * - that is the sum of a non-negative sample value and a positive
705 * combination of zero or more non-negative variables.
707 int isl_tab_row_is_redundant(struct isl_tab *tab, int row)
710 unsigned off = 2 + tab->M;
712 if (tab->row_var[row] < 0 && !isl_tab_var_from_row(tab, row)->is_nonneg)
715 if (isl_int_is_neg(tab->mat->row[row][1]))
717 if (tab->M && isl_int_is_neg(tab->mat->row[row][2]))
720 for (i = tab->n_dead; i < tab->n_col; ++i) {
721 if (isl_int_is_zero(tab->mat->row[row][off + i]))
723 if (isl_int_is_neg(tab->mat->row[row][off + i]))
725 if (!var_from_col(tab, i)->is_nonneg)
731 static void swap_rows(struct isl_tab *tab, int row1, int row2)
734 t = tab->row_var[row1];
735 tab->row_var[row1] = tab->row_var[row2];
736 tab->row_var[row2] = t;
737 isl_tab_var_from_row(tab, row1)->index = row1;
738 isl_tab_var_from_row(tab, row2)->index = row2;
739 tab->mat = isl_mat_swap_rows(tab->mat, row1, row2);
743 t = tab->row_sign[row1];
744 tab->row_sign[row1] = tab->row_sign[row2];
745 tab->row_sign[row2] = t;
748 static int push_union(struct isl_tab *tab,
749 enum isl_tab_undo_type type, union isl_tab_undo_val u) WARN_UNUSED;
750 static int push_union(struct isl_tab *tab,
751 enum isl_tab_undo_type type, union isl_tab_undo_val u)
753 struct isl_tab_undo *undo;
758 undo = isl_alloc_type(tab->mat->ctx, struct isl_tab_undo);
763 undo->next = tab->top;
769 int isl_tab_push_var(struct isl_tab *tab,
770 enum isl_tab_undo_type type, struct isl_tab_var *var)
772 union isl_tab_undo_val u;
774 u.var_index = tab->row_var[var->index];
776 u.var_index = tab->col_var[var->index];
777 return push_union(tab, type, u);
780 int isl_tab_push(struct isl_tab *tab, enum isl_tab_undo_type type)
782 union isl_tab_undo_val u = { 0 };
783 return push_union(tab, type, u);
786 /* Push a record on the undo stack describing the current basic
787 * variables, so that the this state can be restored during rollback.
789 int isl_tab_push_basis(struct isl_tab *tab)
792 union isl_tab_undo_val u;
794 u.col_var = isl_alloc_array(tab->mat->ctx, int, tab->n_col);
797 for (i = 0; i < tab->n_col; ++i)
798 u.col_var[i] = tab->col_var[i];
799 return push_union(tab, isl_tab_undo_saved_basis, u);
802 int isl_tab_push_callback(struct isl_tab *tab, struct isl_tab_callback *callback)
804 union isl_tab_undo_val u;
805 u.callback = callback;
806 return push_union(tab, isl_tab_undo_callback, u);
809 struct isl_tab *isl_tab_init_samples(struct isl_tab *tab)
816 tab->samples = isl_mat_alloc(tab->mat->ctx, 1, 1 + tab->n_var);
819 tab->sample_index = isl_alloc_array(tab->mat->ctx, int, 1);
820 if (!tab->sample_index)
828 struct isl_tab *isl_tab_add_sample(struct isl_tab *tab,
829 __isl_take isl_vec *sample)
834 if (tab->n_sample + 1 > tab->samples->n_row) {
835 int *t = isl_realloc_array(tab->mat->ctx,
836 tab->sample_index, int, tab->n_sample + 1);
839 tab->sample_index = t;
842 tab->samples = isl_mat_extend(tab->samples,
843 tab->n_sample + 1, tab->samples->n_col);
847 isl_seq_cpy(tab->samples->row[tab->n_sample], sample->el, sample->size);
848 isl_vec_free(sample);
849 tab->sample_index[tab->n_sample] = tab->n_sample;
854 isl_vec_free(sample);
859 struct isl_tab *isl_tab_drop_sample(struct isl_tab *tab, int s)
861 if (s != tab->n_outside) {
862 int t = tab->sample_index[tab->n_outside];
863 tab->sample_index[tab->n_outside] = tab->sample_index[s];
864 tab->sample_index[s] = t;
865 isl_mat_swap_rows(tab->samples, tab->n_outside, s);
868 if (isl_tab_push(tab, isl_tab_undo_drop_sample) < 0) {
876 /* Record the current number of samples so that we can remove newer
877 * samples during a rollback.
879 int isl_tab_save_samples(struct isl_tab *tab)
881 union isl_tab_undo_val u;
887 return push_union(tab, isl_tab_undo_saved_samples, u);
890 /* Mark row with index "row" as being redundant.
891 * If we may need to undo the operation or if the row represents
892 * a variable of the original problem, the row is kept,
893 * but no longer considered when looking for a pivot row.
894 * Otherwise, the row is simply removed.
896 * The row may be interchanged with some other row. If it
897 * is interchanged with a later row, return 1. Otherwise return 0.
898 * If the rows are checked in order in the calling function,
899 * then a return value of 1 means that the row with the given
900 * row number may now contain a different row that hasn't been checked yet.
902 int isl_tab_mark_redundant(struct isl_tab *tab, int row)
904 struct isl_tab_var *var = isl_tab_var_from_row(tab, row);
905 var->is_redundant = 1;
906 isl_assert(tab->mat->ctx, row >= tab->n_redundant, return -1);
907 if (tab->need_undo || tab->row_var[row] >= 0) {
908 if (tab->row_var[row] >= 0 && !var->is_nonneg) {
910 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, var) < 0)
913 if (row != tab->n_redundant)
914 swap_rows(tab, row, tab->n_redundant);
916 return isl_tab_push_var(tab, isl_tab_undo_redundant, var);
918 if (row != tab->n_row - 1)
919 swap_rows(tab, row, tab->n_row - 1);
920 isl_tab_var_from_row(tab, tab->n_row - 1)->index = -1;
926 struct isl_tab *isl_tab_mark_empty(struct isl_tab *tab)
930 if (!tab->empty && tab->need_undo)
931 if (isl_tab_push(tab, isl_tab_undo_empty) < 0) {
939 /* Update the rows signs after a pivot of "row" and "col", with "row_sgn"
940 * the original sign of the pivot element.
941 * We only keep track of row signs during PILP solving and in this case
942 * we only pivot a row with negative sign (meaning the value is always
943 * non-positive) using a positive pivot element.
945 * For each row j, the new value of the parametric constant is equal to
947 * a_j0 - a_jc a_r0/a_rc
949 * where a_j0 is the original parametric constant, a_rc is the pivot element,
950 * a_r0 is the parametric constant of the pivot row and a_jc is the
951 * pivot column entry of the row j.
952 * Since a_r0 is non-positive and a_rc is positive, the sign of row j
953 * remains the same if a_jc has the same sign as the row j or if
954 * a_jc is zero. In all other cases, we reset the sign to "unknown".
956 static void update_row_sign(struct isl_tab *tab, int row, int col, int row_sgn)
959 struct isl_mat *mat = tab->mat;
960 unsigned off = 2 + tab->M;
965 if (tab->row_sign[row] == 0)
967 isl_assert(mat->ctx, row_sgn > 0, return);
968 isl_assert(mat->ctx, tab->row_sign[row] == isl_tab_row_neg, return);
969 tab->row_sign[row] = isl_tab_row_pos;
970 for (i = 0; i < tab->n_row; ++i) {
974 s = isl_int_sgn(mat->row[i][off + col]);
977 if (!tab->row_sign[i])
979 if (s < 0 && tab->row_sign[i] == isl_tab_row_neg)
981 if (s > 0 && tab->row_sign[i] == isl_tab_row_pos)
983 tab->row_sign[i] = isl_tab_row_unknown;
987 /* Given a row number "row" and a column number "col", pivot the tableau
988 * such that the associated variables are interchanged.
989 * The given row in the tableau expresses
991 * x_r = a_r0 + \sum_i a_ri x_i
995 * x_c = 1/a_rc x_r - a_r0/a_rc + sum_{i \ne r} -a_ri/a_rc
997 * Substituting this equality into the other rows
999 * x_j = a_j0 + \sum_i a_ji x_i
1001 * with a_jc \ne 0, we obtain
1003 * 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
1010 * where i is any other column and j is any other row,
1011 * is therefore transformed into
1013 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1014 * 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)
1016 * The transformation is performed along the following steps
1018 * d_r/n_rc n_ri/n_rc
1021 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1024 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1025 * n_jc/(|n_rc| d_j) n_ji/(|n_rc| d_j)
1027 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1028 * n_jc/(|n_rc| d_j) (n_ji |n_rc|)/(|n_rc| d_j)
1030 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1031 * n_jc/(|n_rc| d_j) (n_ji |n_rc| - s(n_rc)n_jc n_ri)/(|n_rc| d_j)
1033 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1034 * 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)
1037 int isl_tab_pivot(struct isl_tab *tab, int row, int col)
1042 struct isl_mat *mat = tab->mat;
1043 struct isl_tab_var *var;
1044 unsigned off = 2 + tab->M;
1046 isl_int_swap(mat->row[row][0], mat->row[row][off + col]);
1047 sgn = isl_int_sgn(mat->row[row][0]);
1049 isl_int_neg(mat->row[row][0], mat->row[row][0]);
1050 isl_int_neg(mat->row[row][off + col], mat->row[row][off + col]);
1052 for (j = 0; j < off - 1 + tab->n_col; ++j) {
1053 if (j == off - 1 + col)
1055 isl_int_neg(mat->row[row][1 + j], mat->row[row][1 + j]);
1057 if (!isl_int_is_one(mat->row[row][0]))
1058 isl_seq_normalize(mat->ctx, mat->row[row], off + tab->n_col);
1059 for (i = 0; i < tab->n_row; ++i) {
1062 if (isl_int_is_zero(mat->row[i][off + col]))
1064 isl_int_mul(mat->row[i][0], mat->row[i][0], mat->row[row][0]);
1065 for (j = 0; j < off - 1 + tab->n_col; ++j) {
1066 if (j == off - 1 + col)
1068 isl_int_mul(mat->row[i][1 + j],
1069 mat->row[i][1 + j], mat->row[row][0]);
1070 isl_int_addmul(mat->row[i][1 + j],
1071 mat->row[i][off + col], mat->row[row][1 + j]);
1073 isl_int_mul(mat->row[i][off + col],
1074 mat->row[i][off + col], mat->row[row][off + col]);
1075 if (!isl_int_is_one(mat->row[i][0]))
1076 isl_seq_normalize(mat->ctx, mat->row[i], off + tab->n_col);
1078 t = tab->row_var[row];
1079 tab->row_var[row] = tab->col_var[col];
1080 tab->col_var[col] = t;
1081 var = isl_tab_var_from_row(tab, row);
1084 var = var_from_col(tab, col);
1087 update_row_sign(tab, row, col, sgn);
1090 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1091 if (isl_int_is_zero(mat->row[i][off + col]))
1093 if (!isl_tab_var_from_row(tab, i)->frozen &&
1094 isl_tab_row_is_redundant(tab, i)) {
1095 int redo = isl_tab_mark_redundant(tab, i);
1105 /* If "var" represents a column variable, then pivot is up (sgn > 0)
1106 * or down (sgn < 0) to a row. The variable is assumed not to be
1107 * unbounded in the specified direction.
1108 * If sgn = 0, then the variable is unbounded in both directions,
1109 * and we pivot with any row we can find.
1111 static int to_row(struct isl_tab *tab, struct isl_tab_var *var, int sign) WARN_UNUSED;
1112 static int to_row(struct isl_tab *tab, struct isl_tab_var *var, int sign)
1115 unsigned off = 2 + tab->M;
1121 for (r = tab->n_redundant; r < tab->n_row; ++r)
1122 if (!isl_int_is_zero(tab->mat->row[r][off+var->index]))
1124 isl_assert(tab->mat->ctx, r < tab->n_row, return -1);
1126 r = pivot_row(tab, NULL, sign, var->index);
1127 isl_assert(tab->mat->ctx, r >= 0, return -1);
1130 return isl_tab_pivot(tab, r, var->index);
1133 static void check_table(struct isl_tab *tab)
1139 for (i = 0; i < tab->n_row; ++i) {
1140 if (!isl_tab_var_from_row(tab, i)->is_nonneg)
1142 assert(!isl_int_is_neg(tab->mat->row[i][1]));
1146 /* Return the sign of the maximal value of "var".
1147 * If the sign is not negative, then on return from this function,
1148 * the sample value will also be non-negative.
1150 * If "var" is manifestly unbounded wrt positive values, we are done.
1151 * Otherwise, we pivot the variable up to a row if needed
1152 * Then we continue pivoting down until either
1153 * - no more down pivots can be performed
1154 * - the sample value is positive
1155 * - the variable is pivoted into a manifestly unbounded column
1157 static int sign_of_max(struct isl_tab *tab, struct isl_tab_var *var)
1161 if (max_is_manifestly_unbounded(tab, var))
1163 if (to_row(tab, var, 1) < 0)
1165 while (!isl_int_is_pos(tab->mat->row[var->index][1])) {
1166 find_pivot(tab, var, var, 1, &row, &col);
1168 return isl_int_sgn(tab->mat->row[var->index][1]);
1169 if (isl_tab_pivot(tab, row, col) < 0)
1171 if (!var->is_row) /* manifestly unbounded */
1177 static int row_is_neg(struct isl_tab *tab, int row)
1180 return isl_int_is_neg(tab->mat->row[row][1]);
1181 if (isl_int_is_pos(tab->mat->row[row][2]))
1183 if (isl_int_is_neg(tab->mat->row[row][2]))
1185 return isl_int_is_neg(tab->mat->row[row][1]);
1188 static int row_sgn(struct isl_tab *tab, int row)
1191 return isl_int_sgn(tab->mat->row[row][1]);
1192 if (!isl_int_is_zero(tab->mat->row[row][2]))
1193 return isl_int_sgn(tab->mat->row[row][2]);
1195 return isl_int_sgn(tab->mat->row[row][1]);
1198 /* Perform pivots until the row variable "var" has a non-negative
1199 * sample value or until no more upward pivots can be performed.
1200 * Return the sign of the sample value after the pivots have been
1203 static int restore_row(struct isl_tab *tab, struct isl_tab_var *var)
1207 while (row_is_neg(tab, var->index)) {
1208 find_pivot(tab, var, var, 1, &row, &col);
1211 if (isl_tab_pivot(tab, row, col) < 0)
1213 if (!var->is_row) /* manifestly unbounded */
1216 return row_sgn(tab, var->index);
1219 /* Perform pivots until we are sure that the row variable "var"
1220 * can attain non-negative values. After return from this
1221 * function, "var" is still a row variable, but its sample
1222 * value may not be non-negative, even if the function returns 1.
1224 static int at_least_zero(struct isl_tab *tab, struct isl_tab_var *var)
1228 while (isl_int_is_neg(tab->mat->row[var->index][1])) {
1229 find_pivot(tab, var, var, 1, &row, &col);
1232 if (row == var->index) /* manifestly unbounded */
1234 if (isl_tab_pivot(tab, row, col) < 0)
1237 return !isl_int_is_neg(tab->mat->row[var->index][1]);
1240 /* Return a negative value if "var" can attain negative values.
1241 * Return a non-negative value otherwise.
1243 * If "var" is manifestly unbounded wrt negative values, we are done.
1244 * Otherwise, if var is in a column, we can pivot it down to a row.
1245 * Then we continue pivoting down until either
1246 * - the pivot would result in a manifestly unbounded column
1247 * => we don't perform the pivot, but simply return -1
1248 * - no more down pivots can be performed
1249 * - the sample value is negative
1250 * If the sample value becomes negative and the variable is supposed
1251 * to be nonnegative, then we undo the last pivot.
1252 * However, if the last pivot has made the pivoting variable
1253 * obviously redundant, then it may have moved to another row.
1254 * In that case we look for upward pivots until we reach a non-negative
1257 static int sign_of_min(struct isl_tab *tab, struct isl_tab_var *var)
1260 struct isl_tab_var *pivot_var = NULL;
1262 if (min_is_manifestly_unbounded(tab, var))
1266 row = pivot_row(tab, NULL, -1, col);
1267 pivot_var = var_from_col(tab, col);
1268 if (isl_tab_pivot(tab, row, col) < 0)
1270 if (var->is_redundant)
1272 if (isl_int_is_neg(tab->mat->row[var->index][1])) {
1273 if (var->is_nonneg) {
1274 if (!pivot_var->is_redundant &&
1275 pivot_var->index == row) {
1276 if (isl_tab_pivot(tab, row, col) < 0)
1279 if (restore_row(tab, var) < -1)
1285 if (var->is_redundant)
1287 while (!isl_int_is_neg(tab->mat->row[var->index][1])) {
1288 find_pivot(tab, var, var, -1, &row, &col);
1289 if (row == var->index)
1292 return isl_int_sgn(tab->mat->row[var->index][1]);
1293 pivot_var = var_from_col(tab, col);
1294 if (isl_tab_pivot(tab, row, col) < 0)
1296 if (var->is_redundant)
1299 if (pivot_var && var->is_nonneg) {
1300 /* pivot back to non-negative value */
1301 if (!pivot_var->is_redundant && pivot_var->index == row) {
1302 if (isl_tab_pivot(tab, row, col) < 0)
1305 if (restore_row(tab, var) < -1)
1311 static int row_at_most_neg_one(struct isl_tab *tab, int row)
1314 if (isl_int_is_pos(tab->mat->row[row][2]))
1316 if (isl_int_is_neg(tab->mat->row[row][2]))
1319 return isl_int_is_neg(tab->mat->row[row][1]) &&
1320 isl_int_abs_ge(tab->mat->row[row][1],
1321 tab->mat->row[row][0]);
1324 /* Return 1 if "var" can attain values <= -1.
1325 * Return 0 otherwise.
1327 * The sample value of "var" is assumed to be non-negative when the
1328 * the function is called and will be made non-negative again before
1329 * the function returns.
1331 int isl_tab_min_at_most_neg_one(struct isl_tab *tab, struct isl_tab_var *var)
1334 struct isl_tab_var *pivot_var;
1336 if (min_is_manifestly_unbounded(tab, var))
1340 row = pivot_row(tab, NULL, -1, col);
1341 pivot_var = var_from_col(tab, col);
1342 if (isl_tab_pivot(tab, row, col) < 0)
1344 if (var->is_redundant)
1346 if (row_at_most_neg_one(tab, var->index)) {
1347 if (var->is_nonneg) {
1348 if (!pivot_var->is_redundant &&
1349 pivot_var->index == row) {
1350 if (isl_tab_pivot(tab, row, col) < 0)
1353 if (restore_row(tab, var) < -1)
1359 if (var->is_redundant)
1362 find_pivot(tab, var, var, -1, &row, &col);
1363 if (row == var->index)
1367 pivot_var = var_from_col(tab, col);
1368 if (isl_tab_pivot(tab, row, col) < 0)
1370 if (var->is_redundant)
1372 } while (!row_at_most_neg_one(tab, var->index));
1373 if (var->is_nonneg) {
1374 /* pivot back to non-negative value */
1375 if (!pivot_var->is_redundant && pivot_var->index == row)
1376 if (isl_tab_pivot(tab, row, col) < 0)
1378 if (restore_row(tab, var) < -1)
1384 /* Return 1 if "var" can attain values >= 1.
1385 * Return 0 otherwise.
1387 static int at_least_one(struct isl_tab *tab, struct isl_tab_var *var)
1392 if (max_is_manifestly_unbounded(tab, var))
1394 if (to_row(tab, var, 1) < 0)
1396 r = tab->mat->row[var->index];
1397 while (isl_int_lt(r[1], r[0])) {
1398 find_pivot(tab, var, var, 1, &row, &col);
1400 return isl_int_ge(r[1], r[0]);
1401 if (row == var->index) /* manifestly unbounded */
1403 if (isl_tab_pivot(tab, row, col) < 0)
1409 static void swap_cols(struct isl_tab *tab, int col1, int col2)
1412 unsigned off = 2 + tab->M;
1413 t = tab->col_var[col1];
1414 tab->col_var[col1] = tab->col_var[col2];
1415 tab->col_var[col2] = t;
1416 var_from_col(tab, col1)->index = col1;
1417 var_from_col(tab, col2)->index = col2;
1418 tab->mat = isl_mat_swap_cols(tab->mat, off + col1, off + col2);
1421 /* Mark column with index "col" as representing a zero variable.
1422 * If we may need to undo the operation the column is kept,
1423 * but no longer considered.
1424 * Otherwise, the column is simply removed.
1426 * The column may be interchanged with some other column. If it
1427 * is interchanged with a later column, return 1. Otherwise return 0.
1428 * If the columns are checked in order in the calling function,
1429 * then a return value of 1 means that the column with the given
1430 * column number may now contain a different column that
1431 * hasn't been checked yet.
1433 int isl_tab_kill_col(struct isl_tab *tab, int col)
1435 var_from_col(tab, col)->is_zero = 1;
1436 if (tab->need_undo) {
1437 if (isl_tab_push_var(tab, isl_tab_undo_zero,
1438 var_from_col(tab, col)) < 0)
1440 if (col != tab->n_dead)
1441 swap_cols(tab, col, tab->n_dead);
1445 if (col != tab->n_col - 1)
1446 swap_cols(tab, col, tab->n_col - 1);
1447 var_from_col(tab, tab->n_col - 1)->index = -1;
1453 /* Row variable "var" is non-negative and cannot attain any values
1454 * larger than zero. This means that the coefficients of the unrestricted
1455 * column variables are zero and that the coefficients of the non-negative
1456 * column variables are zero or negative.
1457 * Each of the non-negative variables with a negative coefficient can
1458 * then also be written as the negative sum of non-negative variables
1459 * and must therefore also be zero.
1461 static int close_row(struct isl_tab *tab, struct isl_tab_var *var) WARN_UNUSED;
1462 static int close_row(struct isl_tab *tab, struct isl_tab_var *var)
1465 struct isl_mat *mat = tab->mat;
1466 unsigned off = 2 + tab->M;
1468 isl_assert(tab->mat->ctx, var->is_nonneg, return -1);
1471 if (isl_tab_push_var(tab, isl_tab_undo_zero, var) < 0)
1473 for (j = tab->n_dead; j < tab->n_col; ++j) {
1474 if (isl_int_is_zero(mat->row[var->index][off + j]))
1476 isl_assert(tab->mat->ctx,
1477 isl_int_is_neg(mat->row[var->index][off + j]), return -1);
1478 if (isl_tab_kill_col(tab, j))
1481 if (isl_tab_mark_redundant(tab, var->index) < 0)
1486 /* Add a constraint to the tableau and allocate a row for it.
1487 * Return the index into the constraint array "con".
1489 int isl_tab_allocate_con(struct isl_tab *tab)
1493 isl_assert(tab->mat->ctx, tab->n_row < tab->mat->n_row, return -1);
1494 isl_assert(tab->mat->ctx, tab->n_con < tab->max_con, return -1);
1497 tab->con[r].index = tab->n_row;
1498 tab->con[r].is_row = 1;
1499 tab->con[r].is_nonneg = 0;
1500 tab->con[r].is_zero = 0;
1501 tab->con[r].is_redundant = 0;
1502 tab->con[r].frozen = 0;
1503 tab->con[r].negated = 0;
1504 tab->row_var[tab->n_row] = ~r;
1508 if (isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->con[r]) < 0)
1514 /* Add a variable to the tableau and allocate a column for it.
1515 * Return the index into the variable array "var".
1517 int isl_tab_allocate_var(struct isl_tab *tab)
1521 unsigned off = 2 + tab->M;
1523 isl_assert(tab->mat->ctx, tab->n_col < tab->mat->n_col, return -1);
1524 isl_assert(tab->mat->ctx, tab->n_var < tab->max_var, return -1);
1527 tab->var[r].index = tab->n_col;
1528 tab->var[r].is_row = 0;
1529 tab->var[r].is_nonneg = 0;
1530 tab->var[r].is_zero = 0;
1531 tab->var[r].is_redundant = 0;
1532 tab->var[r].frozen = 0;
1533 tab->var[r].negated = 0;
1534 tab->col_var[tab->n_col] = r;
1536 for (i = 0; i < tab->n_row; ++i)
1537 isl_int_set_si(tab->mat->row[i][off + tab->n_col], 0);
1541 if (isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->var[r]) < 0)
1547 /* Add a row to the tableau. The row is given as an affine combination
1548 * of the original variables and needs to be expressed in terms of the
1551 * We add each term in turn.
1552 * If r = n/d_r is the current sum and we need to add k x, then
1553 * if x is a column variable, we increase the numerator of
1554 * this column by k d_r
1555 * if x = f/d_x is a row variable, then the new representation of r is
1557 * n k f d_x/g n + d_r/g k f m/d_r n + m/d_g k f
1558 * --- + --- = ------------------- = -------------------
1559 * d_r d_r d_r d_x/g m
1561 * with g the gcd of d_r and d_x and m the lcm of d_r and d_x.
1563 int isl_tab_add_row(struct isl_tab *tab, isl_int *line)
1569 unsigned off = 2 + tab->M;
1571 r = isl_tab_allocate_con(tab);
1577 row = tab->mat->row[tab->con[r].index];
1578 isl_int_set_si(row[0], 1);
1579 isl_int_set(row[1], line[0]);
1580 isl_seq_clr(row + 2, tab->M + tab->n_col);
1581 for (i = 0; i < tab->n_var; ++i) {
1582 if (tab->var[i].is_zero)
1584 if (tab->var[i].is_row) {
1586 row[0], tab->mat->row[tab->var[i].index][0]);
1587 isl_int_swap(a, row[0]);
1588 isl_int_divexact(a, row[0], a);
1590 row[0], tab->mat->row[tab->var[i].index][0]);
1591 isl_int_mul(b, b, line[1 + i]);
1592 isl_seq_combine(row + 1, a, row + 1,
1593 b, tab->mat->row[tab->var[i].index] + 1,
1594 1 + tab->M + tab->n_col);
1596 isl_int_addmul(row[off + tab->var[i].index],
1597 line[1 + i], row[0]);
1598 if (tab->M && i >= tab->n_param && i < tab->n_var - tab->n_div)
1599 isl_int_submul(row[2], line[1 + i], row[0]);
1601 isl_seq_normalize(tab->mat->ctx, row, off + tab->n_col);
1606 tab->row_sign[tab->con[r].index] = 0;
1611 static int drop_row(struct isl_tab *tab, int row)
1613 isl_assert(tab->mat->ctx, ~tab->row_var[row] == tab->n_con - 1, return -1);
1614 if (row != tab->n_row - 1)
1615 swap_rows(tab, row, tab->n_row - 1);
1621 static int drop_col(struct isl_tab *tab, int col)
1623 isl_assert(tab->mat->ctx, tab->col_var[col] == tab->n_var - 1, return -1);
1624 if (col != tab->n_col - 1)
1625 swap_cols(tab, col, tab->n_col - 1);
1631 /* Add inequality "ineq" and check if it conflicts with the
1632 * previously added constraints or if it is obviously redundant.
1634 struct isl_tab *isl_tab_add_ineq(struct isl_tab *tab, isl_int *ineq)
1643 struct isl_basic_set *bset = tab->bset;
1645 isl_assert(tab->mat->ctx, tab->n_eq == bset->n_eq, goto error);
1646 isl_assert(tab->mat->ctx,
1647 tab->n_con == bset->n_eq + bset->n_ineq, goto error);
1648 tab->bset = isl_basic_set_add_ineq(tab->bset, ineq);
1649 if (isl_tab_push(tab, isl_tab_undo_bset_ineq) < 0)
1656 isl_int_swap(ineq[0], cst);
1658 r = isl_tab_add_row(tab, ineq);
1660 isl_int_swap(ineq[0], cst);
1665 tab->con[r].is_nonneg = 1;
1666 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1668 if (isl_tab_row_is_redundant(tab, tab->con[r].index)) {
1669 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1674 sgn = restore_row(tab, &tab->con[r]);
1678 return isl_tab_mark_empty(tab);
1679 if (tab->con[r].is_row && isl_tab_row_is_redundant(tab, tab->con[r].index))
1680 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1688 /* Pivot a non-negative variable down until it reaches the value zero
1689 * and then pivot the variable into a column position.
1691 static int to_col(struct isl_tab *tab, struct isl_tab_var *var) WARN_UNUSED;
1692 static int to_col(struct isl_tab *tab, struct isl_tab_var *var)
1696 unsigned off = 2 + tab->M;
1701 while (isl_int_is_pos(tab->mat->row[var->index][1])) {
1702 find_pivot(tab, var, NULL, -1, &row, &col);
1703 isl_assert(tab->mat->ctx, row != -1, return -1);
1704 if (isl_tab_pivot(tab, row, col) < 0)
1710 for (i = tab->n_dead; i < tab->n_col; ++i)
1711 if (!isl_int_is_zero(tab->mat->row[var->index][off + i]))
1714 isl_assert(tab->mat->ctx, i < tab->n_col, return -1);
1715 if (isl_tab_pivot(tab, var->index, i) < 0)
1721 /* We assume Gaussian elimination has been performed on the equalities.
1722 * The equalities can therefore never conflict.
1723 * Adding the equalities is currently only really useful for a later call
1724 * to isl_tab_ineq_type.
1726 static struct isl_tab *add_eq(struct isl_tab *tab, isl_int *eq)
1733 r = isl_tab_add_row(tab, eq);
1737 r = tab->con[r].index;
1738 i = isl_seq_first_non_zero(tab->mat->row[r] + 2 + tab->M + tab->n_dead,
1739 tab->n_col - tab->n_dead);
1740 isl_assert(tab->mat->ctx, i >= 0, goto error);
1742 if (isl_tab_pivot(tab, r, i) < 0)
1744 if (isl_tab_kill_col(tab, i) < 0)
1754 static int row_is_manifestly_zero(struct isl_tab *tab, int row)
1756 unsigned off = 2 + tab->M;
1758 if (!isl_int_is_zero(tab->mat->row[row][1]))
1760 if (tab->M && !isl_int_is_zero(tab->mat->row[row][2]))
1762 return isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
1763 tab->n_col - tab->n_dead) == -1;
1766 /* Add an equality that is known to be valid for the given tableau.
1768 struct isl_tab *isl_tab_add_valid_eq(struct isl_tab *tab, isl_int *eq)
1770 struct isl_tab_var *var;
1775 r = isl_tab_add_row(tab, eq);
1781 if (row_is_manifestly_zero(tab, r)) {
1783 if (isl_tab_mark_redundant(tab, r) < 0)
1788 if (isl_int_is_neg(tab->mat->row[r][1])) {
1789 isl_seq_neg(tab->mat->row[r] + 1, tab->mat->row[r] + 1,
1794 if (to_col(tab, var) < 0)
1797 if (isl_tab_kill_col(tab, var->index) < 0)
1806 static int add_zero_row(struct isl_tab *tab)
1811 r = isl_tab_allocate_con(tab);
1815 row = tab->mat->row[tab->con[r].index];
1816 isl_seq_clr(row + 1, 1 + tab->M + tab->n_col);
1817 isl_int_set_si(row[0], 1);
1822 /* Add equality "eq" and check if it conflicts with the
1823 * previously added constraints or if it is obviously redundant.
1825 struct isl_tab *isl_tab_add_eq(struct isl_tab *tab, isl_int *eq)
1827 struct isl_tab_undo *snap = NULL;
1828 struct isl_tab_var *var;
1836 isl_assert(tab->mat->ctx, !tab->M, goto error);
1839 snap = isl_tab_snap(tab);
1843 isl_int_swap(eq[0], cst);
1845 r = isl_tab_add_row(tab, eq);
1847 isl_int_swap(eq[0], cst);
1855 if (row_is_manifestly_zero(tab, row)) {
1857 if (isl_tab_rollback(tab, snap) < 0)
1865 tab->bset = isl_basic_set_add_ineq(tab->bset, eq);
1866 if (isl_tab_push(tab, isl_tab_undo_bset_ineq) < 0)
1868 isl_seq_neg(eq, eq, 1 + tab->n_var);
1869 tab->bset = isl_basic_set_add_ineq(tab->bset, eq);
1870 isl_seq_neg(eq, eq, 1 + tab->n_var);
1871 if (isl_tab_push(tab, isl_tab_undo_bset_ineq) < 0)
1875 if (add_zero_row(tab) < 0)
1879 sgn = isl_int_sgn(tab->mat->row[row][1]);
1882 isl_seq_neg(tab->mat->row[row] + 1, tab->mat->row[row] + 1,
1889 sgn = sign_of_max(tab, var);
1893 return isl_tab_mark_empty(tab);
1897 if (to_col(tab, var) < 0)
1900 if (isl_tab_kill_col(tab, var->index) < 0)
1909 struct isl_tab *isl_tab_from_basic_map(struct isl_basic_map *bmap)
1912 struct isl_tab *tab;
1916 tab = isl_tab_alloc(bmap->ctx,
1917 isl_basic_map_total_dim(bmap) + bmap->n_ineq + 1,
1918 isl_basic_map_total_dim(bmap), 0);
1921 tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
1922 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1923 return isl_tab_mark_empty(tab);
1924 for (i = 0; i < bmap->n_eq; ++i) {
1925 tab = add_eq(tab, bmap->eq[i]);
1929 for (i = 0; i < bmap->n_ineq; ++i) {
1930 tab = isl_tab_add_ineq(tab, bmap->ineq[i]);
1931 if (!tab || tab->empty)
1937 struct isl_tab *isl_tab_from_basic_set(struct isl_basic_set *bset)
1939 return isl_tab_from_basic_map((struct isl_basic_map *)bset);
1942 /* Construct a tableau corresponding to the recession cone of "bset".
1944 struct isl_tab *isl_tab_from_recession_cone(struct isl_basic_set *bset)
1948 struct isl_tab *tab;
1952 tab = isl_tab_alloc(bset->ctx, bset->n_eq + bset->n_ineq,
1953 isl_basic_set_total_dim(bset), 0);
1956 tab->rational = ISL_F_ISSET(bset, ISL_BASIC_SET_RATIONAL);
1960 for (i = 0; i < bset->n_eq; ++i) {
1961 isl_int_swap(bset->eq[i][0], cst);
1962 tab = add_eq(tab, bset->eq[i]);
1963 isl_int_swap(bset->eq[i][0], cst);
1967 for (i = 0; i < bset->n_ineq; ++i) {
1969 isl_int_swap(bset->ineq[i][0], cst);
1970 r = isl_tab_add_row(tab, bset->ineq[i]);
1971 isl_int_swap(bset->ineq[i][0], cst);
1974 tab->con[r].is_nonneg = 1;
1975 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1987 /* Assuming "tab" is the tableau of a cone, check if the cone is
1988 * bounded, i.e., if it is empty or only contains the origin.
1990 int isl_tab_cone_is_bounded(struct isl_tab *tab)
1998 if (tab->n_dead == tab->n_col)
2002 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2003 struct isl_tab_var *var;
2005 var = isl_tab_var_from_row(tab, i);
2006 if (!var->is_nonneg)
2008 sgn = sign_of_max(tab, var);
2013 if (close_row(tab, var) < 0)
2017 if (tab->n_dead == tab->n_col)
2019 if (i == tab->n_row)
2024 int isl_tab_sample_is_integer(struct isl_tab *tab)
2031 for (i = 0; i < tab->n_var; ++i) {
2033 if (!tab->var[i].is_row)
2035 row = tab->var[i].index;
2036 if (!isl_int_is_divisible_by(tab->mat->row[row][1],
2037 tab->mat->row[row][0]))
2043 static struct isl_vec *extract_integer_sample(struct isl_tab *tab)
2046 struct isl_vec *vec;
2048 vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
2052 isl_int_set_si(vec->block.data[0], 1);
2053 for (i = 0; i < tab->n_var; ++i) {
2054 if (!tab->var[i].is_row)
2055 isl_int_set_si(vec->block.data[1 + i], 0);
2057 int row = tab->var[i].index;
2058 isl_int_divexact(vec->block.data[1 + i],
2059 tab->mat->row[row][1], tab->mat->row[row][0]);
2066 struct isl_vec *isl_tab_get_sample_value(struct isl_tab *tab)
2069 struct isl_vec *vec;
2075 vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
2081 isl_int_set_si(vec->block.data[0], 1);
2082 for (i = 0; i < tab->n_var; ++i) {
2084 if (!tab->var[i].is_row) {
2085 isl_int_set_si(vec->block.data[1 + i], 0);
2088 row = tab->var[i].index;
2089 isl_int_gcd(m, vec->block.data[0], tab->mat->row[row][0]);
2090 isl_int_divexact(m, tab->mat->row[row][0], m);
2091 isl_seq_scale(vec->block.data, vec->block.data, m, 1 + i);
2092 isl_int_divexact(m, vec->block.data[0], tab->mat->row[row][0]);
2093 isl_int_mul(vec->block.data[1 + i], m, tab->mat->row[row][1]);
2095 vec = isl_vec_normalize(vec);
2101 /* Update "bmap" based on the results of the tableau "tab".
2102 * In particular, implicit equalities are made explicit, redundant constraints
2103 * are removed and if the sample value happens to be integer, it is stored
2104 * in "bmap" (unless "bmap" already had an integer sample).
2106 * The tableau is assumed to have been created from "bmap" using
2107 * isl_tab_from_basic_map.
2109 struct isl_basic_map *isl_basic_map_update_from_tab(struct isl_basic_map *bmap,
2110 struct isl_tab *tab)
2122 bmap = isl_basic_map_set_to_empty(bmap);
2124 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2125 if (isl_tab_is_equality(tab, n_eq + i))
2126 isl_basic_map_inequality_to_equality(bmap, i);
2127 else if (isl_tab_is_redundant(tab, n_eq + i))
2128 isl_basic_map_drop_inequality(bmap, i);
2130 if (!tab->rational &&
2131 !bmap->sample && isl_tab_sample_is_integer(tab))
2132 bmap->sample = extract_integer_sample(tab);
2136 struct isl_basic_set *isl_basic_set_update_from_tab(struct isl_basic_set *bset,
2137 struct isl_tab *tab)
2139 return (struct isl_basic_set *)isl_basic_map_update_from_tab(
2140 (struct isl_basic_map *)bset, tab);
2143 /* Given a non-negative variable "var", add a new non-negative variable
2144 * that is the opposite of "var", ensuring that var can only attain the
2146 * If var = n/d is a row variable, then the new variable = -n/d.
2147 * If var is a column variables, then the new variable = -var.
2148 * If the new variable cannot attain non-negative values, then
2149 * the resulting tableau is empty.
2150 * Otherwise, we know the value will be zero and we close the row.
2152 static struct isl_tab *cut_to_hyperplane(struct isl_tab *tab,
2153 struct isl_tab_var *var)
2158 unsigned off = 2 + tab->M;
2162 isl_assert(tab->mat->ctx, !var->is_redundant, goto error);
2164 if (isl_tab_extend_cons(tab, 1) < 0)
2168 tab->con[r].index = tab->n_row;
2169 tab->con[r].is_row = 1;
2170 tab->con[r].is_nonneg = 0;
2171 tab->con[r].is_zero = 0;
2172 tab->con[r].is_redundant = 0;
2173 tab->con[r].frozen = 0;
2174 tab->con[r].negated = 0;
2175 tab->row_var[tab->n_row] = ~r;
2176 row = tab->mat->row[tab->n_row];
2179 isl_int_set(row[0], tab->mat->row[var->index][0]);
2180 isl_seq_neg(row + 1,
2181 tab->mat->row[var->index] + 1, 1 + tab->n_col);
2183 isl_int_set_si(row[0], 1);
2184 isl_seq_clr(row + 1, 1 + tab->n_col);
2185 isl_int_set_si(row[off + var->index], -1);
2190 if (isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->con[r]) < 0)
2193 sgn = sign_of_max(tab, &tab->con[r]);
2197 return isl_tab_mark_empty(tab);
2198 tab->con[r].is_nonneg = 1;
2199 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
2202 if (close_row(tab, &tab->con[r]) < 0)
2211 /* Given a tableau "tab" and an inequality constraint "con" of the tableau,
2212 * relax the inequality by one. That is, the inequality r >= 0 is replaced
2213 * by r' = r + 1 >= 0.
2214 * If r is a row variable, we simply increase the constant term by one
2215 * (taking into account the denominator).
2216 * If r is a column variable, then we need to modify each row that
2217 * refers to r = r' - 1 by substituting this equality, effectively
2218 * subtracting the coefficient of the column from the constant.
2220 struct isl_tab *isl_tab_relax(struct isl_tab *tab, int con)
2222 struct isl_tab_var *var;
2223 unsigned off = 2 + tab->M;
2228 var = &tab->con[con];
2230 if (!var->is_row && !max_is_manifestly_unbounded(tab, var))
2231 if (to_row(tab, var, 1) < 0)
2235 isl_int_add(tab->mat->row[var->index][1],
2236 tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
2240 for (i = 0; i < tab->n_row; ++i) {
2241 if (isl_int_is_zero(tab->mat->row[i][off + var->index]))
2243 isl_int_sub(tab->mat->row[i][1], tab->mat->row[i][1],
2244 tab->mat->row[i][off + var->index]);
2249 if (isl_tab_push_var(tab, isl_tab_undo_relax, var) < 0)
2258 struct isl_tab *isl_tab_select_facet(struct isl_tab *tab, int con)
2263 return cut_to_hyperplane(tab, &tab->con[con]);
2266 static int may_be_equality(struct isl_tab *tab, int row)
2268 unsigned off = 2 + tab->M;
2269 return (tab->rational ? isl_int_is_zero(tab->mat->row[row][1])
2270 : isl_int_lt(tab->mat->row[row][1],
2271 tab->mat->row[row][0])) &&
2272 isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
2273 tab->n_col - tab->n_dead) != -1;
2276 /* Check for (near) equalities among the constraints.
2277 * A constraint is an equality if it is non-negative and if
2278 * its maximal value is either
2279 * - zero (in case of rational tableaus), or
2280 * - strictly less than 1 (in case of integer tableaus)
2282 * We first mark all non-redundant and non-dead variables that
2283 * are not frozen and not obviously not an equality.
2284 * Then we iterate over all marked variables if they can attain
2285 * any values larger than zero or at least one.
2286 * If the maximal value is zero, we mark any column variables
2287 * that appear in the row as being zero and mark the row as being redundant.
2288 * Otherwise, if the maximal value is strictly less than one (and the
2289 * tableau is integer), then we restrict the value to being zero
2290 * by adding an opposite non-negative variable.
2292 struct isl_tab *isl_tab_detect_implicit_equalities(struct isl_tab *tab)
2301 if (tab->n_dead == tab->n_col)
2305 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2306 struct isl_tab_var *var = isl_tab_var_from_row(tab, i);
2307 var->marked = !var->frozen && var->is_nonneg &&
2308 may_be_equality(tab, i);
2312 for (i = tab->n_dead; i < tab->n_col; ++i) {
2313 struct isl_tab_var *var = var_from_col(tab, i);
2314 var->marked = !var->frozen && var->is_nonneg;
2319 struct isl_tab_var *var;
2321 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2322 var = isl_tab_var_from_row(tab, i);
2326 if (i == tab->n_row) {
2327 for (i = tab->n_dead; i < tab->n_col; ++i) {
2328 var = var_from_col(tab, i);
2332 if (i == tab->n_col)
2337 sgn = sign_of_max(tab, var);
2341 if (close_row(tab, var) < 0)
2343 } else if (!tab->rational && !at_least_one(tab, var)) {
2344 tab = cut_to_hyperplane(tab, var);
2345 return isl_tab_detect_implicit_equalities(tab);
2347 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2348 var = isl_tab_var_from_row(tab, i);
2351 if (may_be_equality(tab, i))
2364 static int con_is_redundant(struct isl_tab *tab, struct isl_tab_var *var)
2368 if (tab->rational) {
2369 int sgn = sign_of_min(tab, var);
2374 int irred = isl_tab_min_at_most_neg_one(tab, var);
2381 /* Check for (near) redundant constraints.
2382 * A constraint is redundant if it is non-negative and if
2383 * its minimal value (temporarily ignoring the non-negativity) is either
2384 * - zero (in case of rational tableaus), or
2385 * - strictly larger than -1 (in case of integer tableaus)
2387 * We first mark all non-redundant and non-dead variables that
2388 * are not frozen and not obviously negatively unbounded.
2389 * Then we iterate over all marked variables if they can attain
2390 * any values smaller than zero or at most negative one.
2391 * If not, we mark the row as being redundant (assuming it hasn't
2392 * been detected as being obviously redundant in the mean time).
2394 struct isl_tab *isl_tab_detect_redundant(struct isl_tab *tab)
2403 if (tab->n_redundant == tab->n_row)
2407 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2408 struct isl_tab_var *var = isl_tab_var_from_row(tab, i);
2409 var->marked = !var->frozen && var->is_nonneg;
2413 for (i = tab->n_dead; i < tab->n_col; ++i) {
2414 struct isl_tab_var *var = var_from_col(tab, i);
2415 var->marked = !var->frozen && var->is_nonneg &&
2416 !min_is_manifestly_unbounded(tab, var);
2421 struct isl_tab_var *var;
2423 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2424 var = isl_tab_var_from_row(tab, i);
2428 if (i == tab->n_row) {
2429 for (i = tab->n_dead; i < tab->n_col; ++i) {
2430 var = var_from_col(tab, i);
2434 if (i == tab->n_col)
2439 red = con_is_redundant(tab, var);
2442 if (red && !var->is_redundant)
2443 if (isl_tab_mark_redundant(tab, var->index) < 0)
2445 for (i = tab->n_dead; i < tab->n_col; ++i) {
2446 var = var_from_col(tab, i);
2449 if (!min_is_manifestly_unbounded(tab, var))
2462 int isl_tab_is_equality(struct isl_tab *tab, int con)
2469 if (tab->con[con].is_zero)
2471 if (tab->con[con].is_redundant)
2473 if (!tab->con[con].is_row)
2474 return tab->con[con].index < tab->n_dead;
2476 row = tab->con[con].index;
2479 return isl_int_is_zero(tab->mat->row[row][1]) &&
2480 isl_seq_first_non_zero(tab->mat->row[row] + 2 + tab->n_dead,
2481 tab->n_col - tab->n_dead) == -1;
2484 /* Return the minimial value of the affine expression "f" with denominator
2485 * "denom" in *opt, *opt_denom, assuming the tableau is not empty and
2486 * the expression cannot attain arbitrarily small values.
2487 * If opt_denom is NULL, then *opt is rounded up to the nearest integer.
2488 * The return value reflects the nature of the result (empty, unbounded,
2489 * minmimal value returned in *opt).
2491 enum isl_lp_result isl_tab_min(struct isl_tab *tab,
2492 isl_int *f, isl_int denom, isl_int *opt, isl_int *opt_denom,
2496 enum isl_lp_result res = isl_lp_ok;
2497 struct isl_tab_var *var;
2498 struct isl_tab_undo *snap;
2501 return isl_lp_empty;
2503 snap = isl_tab_snap(tab);
2504 r = isl_tab_add_row(tab, f);
2506 return isl_lp_error;
2508 isl_int_mul(tab->mat->row[var->index][0],
2509 tab->mat->row[var->index][0], denom);
2512 find_pivot(tab, var, var, -1, &row, &col);
2513 if (row == var->index) {
2514 res = isl_lp_unbounded;
2519 if (isl_tab_pivot(tab, row, col) < 0)
2520 return isl_lp_error;
2522 if (ISL_FL_ISSET(flags, ISL_TAB_SAVE_DUAL)) {
2525 isl_vec_free(tab->dual);
2526 tab->dual = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_con);
2528 return isl_lp_error;
2529 isl_int_set(tab->dual->el[0], tab->mat->row[var->index][0]);
2530 for (i = 0; i < tab->n_con; ++i) {
2532 if (tab->con[i].is_row) {
2533 isl_int_set_si(tab->dual->el[1 + i], 0);
2536 pos = 2 + tab->M + tab->con[i].index;
2537 if (tab->con[i].negated)
2538 isl_int_neg(tab->dual->el[1 + i],
2539 tab->mat->row[var->index][pos]);
2541 isl_int_set(tab->dual->el[1 + i],
2542 tab->mat->row[var->index][pos]);
2545 if (opt && res == isl_lp_ok) {
2547 isl_int_set(*opt, tab->mat->row[var->index][1]);
2548 isl_int_set(*opt_denom, tab->mat->row[var->index][0]);
2550 isl_int_cdiv_q(*opt, tab->mat->row[var->index][1],
2551 tab->mat->row[var->index][0]);
2553 if (isl_tab_rollback(tab, snap) < 0)
2554 return isl_lp_error;
2558 int isl_tab_is_redundant(struct isl_tab *tab, int con)
2562 if (tab->con[con].is_zero)
2564 if (tab->con[con].is_redundant)
2566 return tab->con[con].is_row && tab->con[con].index < tab->n_redundant;
2569 /* Take a snapshot of the tableau that can be restored by s call to
2572 struct isl_tab_undo *isl_tab_snap(struct isl_tab *tab)
2580 /* Undo the operation performed by isl_tab_relax.
2582 static int unrelax(struct isl_tab *tab, struct isl_tab_var *var) WARN_UNUSED;
2583 static int unrelax(struct isl_tab *tab, struct isl_tab_var *var)
2585 unsigned off = 2 + tab->M;
2587 if (!var->is_row && !max_is_manifestly_unbounded(tab, var))
2588 if (to_row(tab, var, 1) < 0)
2592 isl_int_sub(tab->mat->row[var->index][1],
2593 tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
2597 for (i = 0; i < tab->n_row; ++i) {
2598 if (isl_int_is_zero(tab->mat->row[i][off + var->index]))
2600 isl_int_add(tab->mat->row[i][1], tab->mat->row[i][1],
2601 tab->mat->row[i][off + var->index]);
2609 static int perform_undo_var(struct isl_tab *tab, struct isl_tab_undo *undo) WARN_UNUSED;
2610 static int perform_undo_var(struct isl_tab *tab, struct isl_tab_undo *undo)
2612 struct isl_tab_var *var = var_from_index(tab, undo->u.var_index);
2613 switch(undo->type) {
2614 case isl_tab_undo_nonneg:
2617 case isl_tab_undo_redundant:
2618 var->is_redundant = 0;
2621 case isl_tab_undo_zero:
2626 case isl_tab_undo_allocate:
2627 if (undo->u.var_index >= 0) {
2628 isl_assert(tab->mat->ctx, !var->is_row, return -1);
2629 drop_col(tab, var->index);
2633 if (!max_is_manifestly_unbounded(tab, var)) {
2634 if (to_row(tab, var, 1) < 0)
2636 } else if (!min_is_manifestly_unbounded(tab, var)) {
2637 if (to_row(tab, var, -1) < 0)
2640 if (to_row(tab, var, 0) < 0)
2643 drop_row(tab, var->index);
2645 case isl_tab_undo_relax:
2646 return unrelax(tab, var);
2652 /* Restore the tableau to the state where the basic variables
2653 * are those in "col_var".
2654 * We first construct a list of variables that are currently in
2655 * the basis, but shouldn't. Then we iterate over all variables
2656 * that should be in the basis and for each one that is currently
2657 * not in the basis, we exchange it with one of the elements of the
2658 * list constructed before.
2659 * We can always find an appropriate variable to pivot with because
2660 * the current basis is mapped to the old basis by a non-singular
2661 * matrix and so we can never end up with a zero row.
2663 static int restore_basis(struct isl_tab *tab, int *col_var)
2667 int *extra = NULL; /* current columns that contain bad stuff */
2668 unsigned off = 2 + tab->M;
2670 extra = isl_alloc_array(tab->mat->ctx, int, tab->n_col);
2673 for (i = 0; i < tab->n_col; ++i) {
2674 for (j = 0; j < tab->n_col; ++j)
2675 if (tab->col_var[i] == col_var[j])
2679 extra[n_extra++] = i;
2681 for (i = 0; i < tab->n_col && n_extra > 0; ++i) {
2682 struct isl_tab_var *var;
2685 for (j = 0; j < tab->n_col; ++j)
2686 if (col_var[i] == tab->col_var[j])
2690 var = var_from_index(tab, col_var[i]);
2692 for (j = 0; j < n_extra; ++j)
2693 if (!isl_int_is_zero(tab->mat->row[row][off+extra[j]]))
2695 isl_assert(tab->mat->ctx, j < n_extra, goto error);
2696 if (isl_tab_pivot(tab, row, extra[j]) < 0)
2698 extra[j] = extra[--n_extra];
2710 /* Remove all samples with index n or greater, i.e., those samples
2711 * that were added since we saved this number of samples in
2712 * isl_tab_save_samples.
2714 static void drop_samples_since(struct isl_tab *tab, int n)
2718 for (i = tab->n_sample - 1; i >= 0 && tab->n_sample > n; --i) {
2719 if (tab->sample_index[i] < n)
2722 if (i != tab->n_sample - 1) {
2723 int t = tab->sample_index[tab->n_sample-1];
2724 tab->sample_index[tab->n_sample-1] = tab->sample_index[i];
2725 tab->sample_index[i] = t;
2726 isl_mat_swap_rows(tab->samples, tab->n_sample-1, i);
2732 static int perform_undo(struct isl_tab *tab, struct isl_tab_undo *undo) WARN_UNUSED;
2733 static int perform_undo(struct isl_tab *tab, struct isl_tab_undo *undo)
2735 switch (undo->type) {
2736 case isl_tab_undo_empty:
2739 case isl_tab_undo_nonneg:
2740 case isl_tab_undo_redundant:
2741 case isl_tab_undo_zero:
2742 case isl_tab_undo_allocate:
2743 case isl_tab_undo_relax:
2744 return perform_undo_var(tab, undo);
2745 case isl_tab_undo_bset_eq:
2746 return isl_basic_set_free_equality(tab->bset, 1);
2747 case isl_tab_undo_bset_ineq:
2748 return isl_basic_set_free_inequality(tab->bset, 1);
2749 case isl_tab_undo_bset_div:
2750 if (isl_basic_set_free_div(tab->bset, 1) < 0)
2753 tab->samples->n_col--;
2755 case isl_tab_undo_saved_basis:
2756 if (restore_basis(tab, undo->u.col_var) < 0)
2759 case isl_tab_undo_drop_sample:
2762 case isl_tab_undo_saved_samples:
2763 drop_samples_since(tab, undo->u.n);
2765 case isl_tab_undo_callback:
2766 return undo->u.callback->run(undo->u.callback);
2768 isl_assert(tab->mat->ctx, 0, return -1);
2773 /* Return the tableau to the state it was in when the snapshot "snap"
2776 int isl_tab_rollback(struct isl_tab *tab, struct isl_tab_undo *snap)
2778 struct isl_tab_undo *undo, *next;
2784 for (undo = tab->top; undo && undo != &tab->bottom; undo = next) {
2788 if (perform_undo(tab, undo) < 0) {
2802 /* The given row "row" represents an inequality violated by all
2803 * points in the tableau. Check for some special cases of such
2804 * separating constraints.
2805 * In particular, if the row has been reduced to the constant -1,
2806 * then we know the inequality is adjacent (but opposite) to
2807 * an equality in the tableau.
2808 * If the row has been reduced to r = -1 -r', with r' an inequality
2809 * of the tableau, then the inequality is adjacent (but opposite)
2810 * to the inequality r'.
2812 static enum isl_ineq_type separation_type(struct isl_tab *tab, unsigned row)
2815 unsigned off = 2 + tab->M;
2818 return isl_ineq_separate;
2820 if (!isl_int_is_one(tab->mat->row[row][0]))
2821 return isl_ineq_separate;
2822 if (!isl_int_is_negone(tab->mat->row[row][1]))
2823 return isl_ineq_separate;
2825 pos = isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
2826 tab->n_col - tab->n_dead);
2828 return isl_ineq_adj_eq;
2830 if (!isl_int_is_negone(tab->mat->row[row][off + tab->n_dead + pos]))
2831 return isl_ineq_separate;
2833 pos = isl_seq_first_non_zero(
2834 tab->mat->row[row] + off + tab->n_dead + pos + 1,
2835 tab->n_col - tab->n_dead - pos - 1);
2837 return pos == -1 ? isl_ineq_adj_ineq : isl_ineq_separate;
2840 /* Check the effect of inequality "ineq" on the tableau "tab".
2842 * isl_ineq_redundant: satisfied by all points in the tableau
2843 * isl_ineq_separate: satisfied by no point in the tableau
2844 * isl_ineq_cut: satisfied by some by not all points
2845 * isl_ineq_adj_eq: adjacent to an equality
2846 * isl_ineq_adj_ineq: adjacent to an inequality.
2848 enum isl_ineq_type isl_tab_ineq_type(struct isl_tab *tab, isl_int *ineq)
2850 enum isl_ineq_type type = isl_ineq_error;
2851 struct isl_tab_undo *snap = NULL;
2856 return isl_ineq_error;
2858 if (isl_tab_extend_cons(tab, 1) < 0)
2859 return isl_ineq_error;
2861 snap = isl_tab_snap(tab);
2863 con = isl_tab_add_row(tab, ineq);
2867 row = tab->con[con].index;
2868 if (isl_tab_row_is_redundant(tab, row))
2869 type = isl_ineq_redundant;
2870 else if (isl_int_is_neg(tab->mat->row[row][1]) &&
2872 isl_int_abs_ge(tab->mat->row[row][1],
2873 tab->mat->row[row][0]))) {
2874 int nonneg = at_least_zero(tab, &tab->con[con]);
2878 type = isl_ineq_cut;
2880 type = separation_type(tab, row);
2882 int red = con_is_redundant(tab, &tab->con[con]);
2886 type = isl_ineq_cut;
2888 type = isl_ineq_redundant;
2891 if (isl_tab_rollback(tab, snap))
2892 return isl_ineq_error;
2895 return isl_ineq_error;
2898 void isl_tab_dump(struct isl_tab *tab, FILE *out, int indent)
2904 fprintf(out, "%*snull tab\n", indent, "");
2907 fprintf(out, "%*sn_redundant: %d, n_dead: %d", indent, "",
2908 tab->n_redundant, tab->n_dead);
2910 fprintf(out, ", rational");
2912 fprintf(out, ", empty");
2914 fprintf(out, "%*s[", indent, "");
2915 for (i = 0; i < tab->n_var; ++i) {
2917 fprintf(out, (i == tab->n_param ||
2918 i == tab->n_var - tab->n_div) ? "; "
2920 fprintf(out, "%c%d%s", tab->var[i].is_row ? 'r' : 'c',
2922 tab->var[i].is_zero ? " [=0]" :
2923 tab->var[i].is_redundant ? " [R]" : "");
2925 fprintf(out, "]\n");
2926 fprintf(out, "%*s[", indent, "");
2927 for (i = 0; i < tab->n_con; ++i) {
2930 fprintf(out, "%c%d%s", tab->con[i].is_row ? 'r' : 'c',
2932 tab->con[i].is_zero ? " [=0]" :
2933 tab->con[i].is_redundant ? " [R]" : "");
2935 fprintf(out, "]\n");
2936 fprintf(out, "%*s[", indent, "");
2937 for (i = 0; i < tab->n_row; ++i) {
2938 const char *sign = "";
2941 if (tab->row_sign) {
2942 if (tab->row_sign[i] == isl_tab_row_unknown)
2944 else if (tab->row_sign[i] == isl_tab_row_neg)
2946 else if (tab->row_sign[i] == isl_tab_row_pos)
2951 fprintf(out, "r%d: %d%s%s", i, tab->row_var[i],
2952 isl_tab_var_from_row(tab, i)->is_nonneg ? " [>=0]" : "", sign);
2954 fprintf(out, "]\n");
2955 fprintf(out, "%*s[", indent, "");
2956 for (i = 0; i < tab->n_col; ++i) {
2959 fprintf(out, "c%d: %d%s", i, tab->col_var[i],
2960 var_from_col(tab, i)->is_nonneg ? " [>=0]" : "");
2962 fprintf(out, "]\n");
2963 r = tab->mat->n_row;
2964 tab->mat->n_row = tab->n_row;
2965 c = tab->mat->n_col;
2966 tab->mat->n_col = 2 + tab->M + tab->n_col;
2967 isl_mat_dump(tab->mat, out, indent);
2968 tab->mat->n_row = r;
2969 tab->mat->n_col = c;
2971 isl_basic_set_dump(tab->bset, out, indent);