2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the GNU LGPLv2.1 license
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
11 #include "isl_map_private.h"
16 * The implementation of tableaus in this file was inspired by Section 8
17 * of David Detlefs, Greg Nelson and James B. Saxe, "Simplify: a theorem
18 * prover for program checking".
21 struct isl_tab *isl_tab_alloc(struct isl_ctx *ctx,
22 unsigned n_row, unsigned n_var, unsigned M)
28 tab = isl_calloc_type(ctx, struct isl_tab);
31 tab->mat = isl_mat_alloc(ctx, n_row, off + n_var);
34 tab->var = isl_alloc_array(ctx, struct isl_tab_var, n_var);
37 tab->con = isl_alloc_array(ctx, struct isl_tab_var, n_row);
40 tab->col_var = isl_alloc_array(ctx, int, n_var);
43 tab->row_var = isl_alloc_array(ctx, int, n_row);
46 for (i = 0; i < n_var; ++i) {
47 tab->var[i].index = i;
48 tab->var[i].is_row = 0;
49 tab->var[i].is_nonneg = 0;
50 tab->var[i].is_zero = 0;
51 tab->var[i].is_redundant = 0;
52 tab->var[i].frozen = 0;
53 tab->var[i].negated = 0;
73 tab->bottom.type = isl_tab_undo_bottom;
74 tab->bottom.next = NULL;
75 tab->top = &tab->bottom;
87 int isl_tab_extend_cons(struct isl_tab *tab, unsigned n_new)
89 unsigned off = 2 + tab->M;
94 if (tab->max_con < tab->n_con + n_new) {
95 struct isl_tab_var *con;
97 con = isl_realloc_array(tab->mat->ctx, tab->con,
98 struct isl_tab_var, tab->max_con + n_new);
102 tab->max_con += n_new;
104 if (tab->mat->n_row < tab->n_row + n_new) {
107 tab->mat = isl_mat_extend(tab->mat,
108 tab->n_row + n_new, off + tab->n_col);
111 row_var = isl_realloc_array(tab->mat->ctx, tab->row_var,
112 int, tab->mat->n_row);
115 tab->row_var = row_var;
117 enum isl_tab_row_sign *s;
118 s = isl_realloc_array(tab->mat->ctx, tab->row_sign,
119 enum isl_tab_row_sign, tab->mat->n_row);
128 /* Make room for at least n_new extra variables.
129 * Return -1 if anything went wrong.
131 int isl_tab_extend_vars(struct isl_tab *tab, unsigned n_new)
133 struct isl_tab_var *var;
134 unsigned off = 2 + tab->M;
136 if (tab->max_var < tab->n_var + n_new) {
137 var = isl_realloc_array(tab->mat->ctx, tab->var,
138 struct isl_tab_var, tab->n_var + n_new);
142 tab->max_var += n_new;
145 if (tab->mat->n_col < off + tab->n_col + n_new) {
148 tab->mat = isl_mat_extend(tab->mat,
149 tab->mat->n_row, off + tab->n_col + n_new);
152 p = isl_realloc_array(tab->mat->ctx, tab->col_var,
153 int, tab->n_col + n_new);
162 struct isl_tab *isl_tab_extend(struct isl_tab *tab, unsigned n_new)
164 if (isl_tab_extend_cons(tab, n_new) >= 0)
171 static void free_undo(struct isl_tab *tab)
173 struct isl_tab_undo *undo, *next;
175 for (undo = tab->top; undo && undo != &tab->bottom; undo = next) {
182 void isl_tab_free(struct isl_tab *tab)
187 isl_mat_free(tab->mat);
188 isl_vec_free(tab->dual);
189 isl_basic_map_free(tab->bmap);
195 isl_mat_free(tab->samples);
196 free(tab->sample_index);
197 isl_mat_free(tab->basis);
201 struct isl_tab *isl_tab_dup(struct isl_tab *tab)
211 dup = isl_calloc_type(tab->ctx, struct isl_tab);
214 dup->mat = isl_mat_dup(tab->mat);
217 dup->var = isl_alloc_array(tab->ctx, struct isl_tab_var, tab->max_var);
220 for (i = 0; i < tab->n_var; ++i)
221 dup->var[i] = tab->var[i];
222 dup->con = isl_alloc_array(tab->ctx, struct isl_tab_var, tab->max_con);
225 for (i = 0; i < tab->n_con; ++i)
226 dup->con[i] = tab->con[i];
227 dup->col_var = isl_alloc_array(tab->ctx, int, tab->mat->n_col - off);
230 for (i = 0; i < tab->n_col; ++i)
231 dup->col_var[i] = tab->col_var[i];
232 dup->row_var = isl_alloc_array(tab->ctx, int, tab->mat->n_row);
235 for (i = 0; i < tab->n_row; ++i)
236 dup->row_var[i] = tab->row_var[i];
238 dup->row_sign = isl_alloc_array(tab->ctx, enum isl_tab_row_sign,
242 for (i = 0; i < tab->n_row; ++i)
243 dup->row_sign[i] = tab->row_sign[i];
246 dup->samples = isl_mat_dup(tab->samples);
249 dup->sample_index = isl_alloc_array(tab->mat->ctx, int,
250 tab->samples->n_row);
251 if (!dup->sample_index)
253 dup->n_sample = tab->n_sample;
254 dup->n_outside = tab->n_outside;
256 dup->n_row = tab->n_row;
257 dup->n_con = tab->n_con;
258 dup->n_eq = tab->n_eq;
259 dup->max_con = tab->max_con;
260 dup->n_col = tab->n_col;
261 dup->n_var = tab->n_var;
262 dup->max_var = tab->max_var;
263 dup->n_param = tab->n_param;
264 dup->n_div = tab->n_div;
265 dup->n_dead = tab->n_dead;
266 dup->n_redundant = tab->n_redundant;
267 dup->rational = tab->rational;
268 dup->empty = tab->empty;
272 tab->cone = tab->cone;
273 dup->bottom.type = isl_tab_undo_bottom;
274 dup->bottom.next = NULL;
275 dup->top = &dup->bottom;
277 dup->n_zero = tab->n_zero;
278 dup->n_unbounded = tab->n_unbounded;
279 dup->basis = isl_mat_dup(tab->basis);
287 /* Construct the coefficient matrix of the product tableau
289 * mat{1,2} is the coefficient matrix of tableau {1,2}
290 * row{1,2} is the number of rows in tableau {1,2}
291 * col{1,2} is the number of columns in tableau {1,2}
292 * off is the offset to the coefficient column (skipping the
293 * denominator, the constant term and the big parameter if any)
294 * r{1,2} is the number of redundant rows in tableau {1,2}
295 * d{1,2} is the number of dead columns in tableau {1,2}
297 * The order of the rows and columns in the result is as explained
298 * in isl_tab_product.
300 static struct isl_mat *tab_mat_product(struct isl_mat *mat1,
301 struct isl_mat *mat2, unsigned row1, unsigned row2,
302 unsigned col1, unsigned col2,
303 unsigned off, unsigned r1, unsigned r2, unsigned d1, unsigned d2)
306 struct isl_mat *prod;
309 prod = isl_mat_alloc(mat1->ctx, mat1->n_row + mat2->n_row,
313 for (i = 0; i < r1; ++i) {
314 isl_seq_cpy(prod->row[n + i], mat1->row[i], off + d1);
315 isl_seq_clr(prod->row[n + i] + off + d1, d2);
316 isl_seq_cpy(prod->row[n + i] + off + d1 + d2,
317 mat1->row[i] + off + d1, col1 - d1);
318 isl_seq_clr(prod->row[n + i] + off + col1 + d1, col2 - d2);
322 for (i = 0; i < r2; ++i) {
323 isl_seq_cpy(prod->row[n + i], mat2->row[i], off);
324 isl_seq_clr(prod->row[n + i] + off, d1);
325 isl_seq_cpy(prod->row[n + i] + off + d1,
326 mat2->row[i] + off, d2);
327 isl_seq_clr(prod->row[n + i] + off + d1 + d2, col1 - d1);
328 isl_seq_cpy(prod->row[n + i] + off + col1 + d1,
329 mat2->row[i] + off + d2, col2 - d2);
333 for (i = 0; i < row1 - r1; ++i) {
334 isl_seq_cpy(prod->row[n + i], mat1->row[r1 + i], off + d1);
335 isl_seq_clr(prod->row[n + i] + off + d1, d2);
336 isl_seq_cpy(prod->row[n + i] + off + d1 + d2,
337 mat1->row[r1 + i] + off + d1, col1 - d1);
338 isl_seq_clr(prod->row[n + i] + off + col1 + d1, col2 - d2);
342 for (i = 0; i < row2 - r2; ++i) {
343 isl_seq_cpy(prod->row[n + i], mat2->row[r2 + i], off);
344 isl_seq_clr(prod->row[n + i] + off, d1);
345 isl_seq_cpy(prod->row[n + i] + off + d1,
346 mat2->row[r2 + i] + off, d2);
347 isl_seq_clr(prod->row[n + i] + off + d1 + d2, col1 - d1);
348 isl_seq_cpy(prod->row[n + i] + off + col1 + d1,
349 mat2->row[r2 + i] + off + d2, col2 - d2);
355 /* Update the row or column index of a variable that corresponds
356 * to a variable in the first input tableau.
358 static void update_index1(struct isl_tab_var *var,
359 unsigned r1, unsigned r2, unsigned d1, unsigned d2)
361 if (var->index == -1)
363 if (var->is_row && var->index >= r1)
365 if (!var->is_row && var->index >= d1)
369 /* Update the row or column index of a variable that corresponds
370 * to a variable in the second input tableau.
372 static void update_index2(struct isl_tab_var *var,
373 unsigned row1, unsigned col1,
374 unsigned r1, unsigned r2, unsigned d1, unsigned d2)
376 if (var->index == -1)
391 /* Create a tableau that represents the Cartesian product of the sets
392 * represented by tableaus tab1 and tab2.
393 * The order of the rows in the product is
394 * - redundant rows of tab1
395 * - redundant rows of tab2
396 * - non-redundant rows of tab1
397 * - non-redundant rows of tab2
398 * The order of the columns is
401 * - coefficient of big parameter, if any
402 * - dead columns of tab1
403 * - dead columns of tab2
404 * - live columns of tab1
405 * - live columns of tab2
406 * The order of the variables and the constraints is a concatenation
407 * of order in the two input tableaus.
409 struct isl_tab *isl_tab_product(struct isl_tab *tab1, struct isl_tab *tab2)
412 struct isl_tab *prod;
414 unsigned r1, r2, d1, d2;
419 isl_assert(tab1->mat->ctx, tab1->M == tab2->M, return NULL);
420 isl_assert(tab1->mat->ctx, tab1->rational == tab2->rational, return NULL);
421 isl_assert(tab1->mat->ctx, tab1->cone == tab2->cone, return NULL);
422 isl_assert(tab1->mat->ctx, !tab1->row_sign, return NULL);
423 isl_assert(tab1->mat->ctx, !tab2->row_sign, return NULL);
424 isl_assert(tab1->mat->ctx, tab1->n_param == 0, return NULL);
425 isl_assert(tab1->mat->ctx, tab2->n_param == 0, return NULL);
426 isl_assert(tab1->mat->ctx, tab1->n_div == 0, return NULL);
427 isl_assert(tab1->mat->ctx, tab2->n_div == 0, return NULL);
430 r1 = tab1->n_redundant;
431 r2 = tab2->n_redundant;
434 prod = isl_calloc_type(tab1->mat->ctx, struct isl_tab);
437 prod->mat = tab_mat_product(tab1->mat, tab2->mat,
438 tab1->n_row, tab2->n_row,
439 tab1->n_col, tab2->n_col, off, r1, r2, d1, d2);
442 prod->var = isl_alloc_array(tab1->mat->ctx, struct isl_tab_var,
443 tab1->max_var + tab2->max_var);
446 for (i = 0; i < tab1->n_var; ++i) {
447 prod->var[i] = tab1->var[i];
448 update_index1(&prod->var[i], r1, r2, d1, d2);
450 for (i = 0; i < tab2->n_var; ++i) {
451 prod->var[tab1->n_var + i] = tab2->var[i];
452 update_index2(&prod->var[tab1->n_var + i],
453 tab1->n_row, tab1->n_col,
456 prod->con = isl_alloc_array(tab1->mat->ctx, struct isl_tab_var,
457 tab1->max_con + tab2->max_con);
460 for (i = 0; i < tab1->n_con; ++i) {
461 prod->con[i] = tab1->con[i];
462 update_index1(&prod->con[i], r1, r2, d1, d2);
464 for (i = 0; i < tab2->n_con; ++i) {
465 prod->con[tab1->n_con + i] = tab2->con[i];
466 update_index2(&prod->con[tab1->n_con + i],
467 tab1->n_row, tab1->n_col,
470 prod->col_var = isl_alloc_array(tab1->mat->ctx, int,
471 tab1->n_col + tab2->n_col);
474 for (i = 0; i < tab1->n_col; ++i) {
475 int pos = i < d1 ? i : i + d2;
476 prod->col_var[pos] = tab1->col_var[i];
478 for (i = 0; i < tab2->n_col; ++i) {
479 int pos = i < d2 ? d1 + i : tab1->n_col + i;
480 int t = tab2->col_var[i];
485 prod->col_var[pos] = t;
487 prod->row_var = isl_alloc_array(tab1->mat->ctx, int,
488 tab1->mat->n_row + tab2->mat->n_row);
491 for (i = 0; i < tab1->n_row; ++i) {
492 int pos = i < r1 ? i : i + r2;
493 prod->row_var[pos] = tab1->row_var[i];
495 for (i = 0; i < tab2->n_row; ++i) {
496 int pos = i < r2 ? r1 + i : tab1->n_row + i;
497 int t = tab2->row_var[i];
502 prod->row_var[pos] = t;
504 prod->samples = NULL;
505 prod->sample_index = NULL;
506 prod->n_row = tab1->n_row + tab2->n_row;
507 prod->n_con = tab1->n_con + tab2->n_con;
509 prod->max_con = tab1->max_con + tab2->max_con;
510 prod->n_col = tab1->n_col + tab2->n_col;
511 prod->n_var = tab1->n_var + tab2->n_var;
512 prod->max_var = tab1->max_var + tab2->max_var;
515 prod->n_dead = tab1->n_dead + tab2->n_dead;
516 prod->n_redundant = tab1->n_redundant + tab2->n_redundant;
517 prod->rational = tab1->rational;
518 prod->empty = tab1->empty || tab2->empty;
522 prod->cone = tab1->cone;
523 prod->bottom.type = isl_tab_undo_bottom;
524 prod->bottom.next = NULL;
525 prod->top = &prod->bottom;
528 prod->n_unbounded = 0;
537 static struct isl_tab_var *var_from_index(struct isl_tab *tab, int i)
542 return &tab->con[~i];
545 struct isl_tab_var *isl_tab_var_from_row(struct isl_tab *tab, int i)
547 return var_from_index(tab, tab->row_var[i]);
550 static struct isl_tab_var *var_from_col(struct isl_tab *tab, int i)
552 return var_from_index(tab, tab->col_var[i]);
555 /* Check if there are any upper bounds on column variable "var",
556 * i.e., non-negative rows where var appears with a negative coefficient.
557 * Return 1 if there are no such bounds.
559 static int max_is_manifestly_unbounded(struct isl_tab *tab,
560 struct isl_tab_var *var)
563 unsigned off = 2 + tab->M;
567 for (i = tab->n_redundant; i < tab->n_row; ++i) {
568 if (!isl_int_is_neg(tab->mat->row[i][off + var->index]))
570 if (isl_tab_var_from_row(tab, i)->is_nonneg)
576 /* Check if there are any lower bounds on column variable "var",
577 * i.e., non-negative rows where var appears with a positive coefficient.
578 * Return 1 if there are no such bounds.
580 static int min_is_manifestly_unbounded(struct isl_tab *tab,
581 struct isl_tab_var *var)
584 unsigned off = 2 + tab->M;
588 for (i = tab->n_redundant; i < tab->n_row; ++i) {
589 if (!isl_int_is_pos(tab->mat->row[i][off + var->index]))
591 if (isl_tab_var_from_row(tab, i)->is_nonneg)
597 static int row_cmp(struct isl_tab *tab, int r1, int r2, int c, isl_int t)
599 unsigned off = 2 + tab->M;
603 isl_int_mul(t, tab->mat->row[r1][2], tab->mat->row[r2][off+c]);
604 isl_int_submul(t, tab->mat->row[r2][2], tab->mat->row[r1][off+c]);
609 isl_int_mul(t, tab->mat->row[r1][1], tab->mat->row[r2][off + c]);
610 isl_int_submul(t, tab->mat->row[r2][1], tab->mat->row[r1][off + c]);
611 return isl_int_sgn(t);
614 /* Given the index of a column "c", return the index of a row
615 * that can be used to pivot the column in, with either an increase
616 * (sgn > 0) or a decrease (sgn < 0) of the corresponding variable.
617 * If "var" is not NULL, then the row returned will be different from
618 * the one associated with "var".
620 * Each row in the tableau is of the form
622 * x_r = a_r0 + \sum_i a_ri x_i
624 * Only rows with x_r >= 0 and with the sign of a_ri opposite to "sgn"
625 * impose any limit on the increase or decrease in the value of x_c
626 * and this bound is equal to a_r0 / |a_rc|. We are therefore looking
627 * for the row with the smallest (most stringent) such bound.
628 * Note that the common denominator of each row drops out of the fraction.
629 * To check if row j has a smaller bound than row r, i.e.,
630 * a_j0 / |a_jc| < a_r0 / |a_rc| or a_j0 |a_rc| < a_r0 |a_jc|,
631 * we check if -sign(a_jc) (a_j0 a_rc - a_r0 a_jc) < 0,
632 * where -sign(a_jc) is equal to "sgn".
634 static int pivot_row(struct isl_tab *tab,
635 struct isl_tab_var *var, int sgn, int c)
639 unsigned off = 2 + tab->M;
643 for (j = tab->n_redundant; j < tab->n_row; ++j) {
644 if (var && j == var->index)
646 if (!isl_tab_var_from_row(tab, j)->is_nonneg)
648 if (sgn * isl_int_sgn(tab->mat->row[j][off + c]) >= 0)
654 tsgn = sgn * row_cmp(tab, r, j, c, t);
655 if (tsgn < 0 || (tsgn == 0 &&
656 tab->row_var[j] < tab->row_var[r]))
663 /* Find a pivot (row and col) that will increase (sgn > 0) or decrease
664 * (sgn < 0) the value of row variable var.
665 * If not NULL, then skip_var is a row variable that should be ignored
666 * while looking for a pivot row. It is usually equal to var.
668 * As the given row in the tableau is of the form
670 * x_r = a_r0 + \sum_i a_ri x_i
672 * we need to find a column such that the sign of a_ri is equal to "sgn"
673 * (such that an increase in x_i will have the desired effect) or a
674 * column with a variable that may attain negative values.
675 * If a_ri is positive, then we need to move x_i in the same direction
676 * to obtain the desired effect. Otherwise, x_i has to move in the
677 * opposite direction.
679 static void find_pivot(struct isl_tab *tab,
680 struct isl_tab_var *var, struct isl_tab_var *skip_var,
681 int sgn, int *row, int *col)
688 isl_assert(tab->mat->ctx, var->is_row, return);
689 tr = tab->mat->row[var->index] + 2 + tab->M;
692 for (j = tab->n_dead; j < tab->n_col; ++j) {
693 if (isl_int_is_zero(tr[j]))
695 if (isl_int_sgn(tr[j]) != sgn &&
696 var_from_col(tab, j)->is_nonneg)
698 if (c < 0 || tab->col_var[j] < tab->col_var[c])
704 sgn *= isl_int_sgn(tr[c]);
705 r = pivot_row(tab, skip_var, sgn, c);
706 *row = r < 0 ? var->index : r;
710 /* Return 1 if row "row" represents an obviously redundant inequality.
712 * - it represents an inequality or a variable
713 * - that is the sum of a non-negative sample value and a positive
714 * combination of zero or more non-negative constraints.
716 int isl_tab_row_is_redundant(struct isl_tab *tab, int row)
719 unsigned off = 2 + tab->M;
721 if (tab->row_var[row] < 0 && !isl_tab_var_from_row(tab, row)->is_nonneg)
724 if (isl_int_is_neg(tab->mat->row[row][1]))
726 if (tab->M && isl_int_is_neg(tab->mat->row[row][2]))
729 for (i = tab->n_dead; i < tab->n_col; ++i) {
730 if (isl_int_is_zero(tab->mat->row[row][off + i]))
732 if (tab->col_var[i] >= 0)
734 if (isl_int_is_neg(tab->mat->row[row][off + i]))
736 if (!var_from_col(tab, i)->is_nonneg)
742 static void swap_rows(struct isl_tab *tab, int row1, int row2)
745 t = tab->row_var[row1];
746 tab->row_var[row1] = tab->row_var[row2];
747 tab->row_var[row2] = t;
748 isl_tab_var_from_row(tab, row1)->index = row1;
749 isl_tab_var_from_row(tab, row2)->index = row2;
750 tab->mat = isl_mat_swap_rows(tab->mat, row1, row2);
754 t = tab->row_sign[row1];
755 tab->row_sign[row1] = tab->row_sign[row2];
756 tab->row_sign[row2] = t;
759 static int push_union(struct isl_tab *tab,
760 enum isl_tab_undo_type type, union isl_tab_undo_val u) WARN_UNUSED;
761 static int push_union(struct isl_tab *tab,
762 enum isl_tab_undo_type type, union isl_tab_undo_val u)
764 struct isl_tab_undo *undo;
769 undo = isl_alloc_type(tab->mat->ctx, struct isl_tab_undo);
774 undo->next = tab->top;
780 int isl_tab_push_var(struct isl_tab *tab,
781 enum isl_tab_undo_type type, struct isl_tab_var *var)
783 union isl_tab_undo_val u;
785 u.var_index = tab->row_var[var->index];
787 u.var_index = tab->col_var[var->index];
788 return push_union(tab, type, u);
791 int isl_tab_push(struct isl_tab *tab, enum isl_tab_undo_type type)
793 union isl_tab_undo_val u = { 0 };
794 return push_union(tab, type, u);
797 /* Push a record on the undo stack describing the current basic
798 * variables, so that the this state can be restored during rollback.
800 int isl_tab_push_basis(struct isl_tab *tab)
803 union isl_tab_undo_val u;
805 u.col_var = isl_alloc_array(tab->mat->ctx, int, tab->n_col);
808 for (i = 0; i < tab->n_col; ++i)
809 u.col_var[i] = tab->col_var[i];
810 return push_union(tab, isl_tab_undo_saved_basis, u);
813 int isl_tab_push_callback(struct isl_tab *tab, struct isl_tab_callback *callback)
815 union isl_tab_undo_val u;
816 u.callback = callback;
817 return push_union(tab, isl_tab_undo_callback, u);
820 struct isl_tab *isl_tab_init_samples(struct isl_tab *tab)
827 tab->samples = isl_mat_alloc(tab->mat->ctx, 1, 1 + tab->n_var);
830 tab->sample_index = isl_alloc_array(tab->mat->ctx, int, 1);
831 if (!tab->sample_index)
839 struct isl_tab *isl_tab_add_sample(struct isl_tab *tab,
840 __isl_take isl_vec *sample)
845 if (tab->n_sample + 1 > tab->samples->n_row) {
846 int *t = isl_realloc_array(tab->mat->ctx,
847 tab->sample_index, int, tab->n_sample + 1);
850 tab->sample_index = t;
853 tab->samples = isl_mat_extend(tab->samples,
854 tab->n_sample + 1, tab->samples->n_col);
858 isl_seq_cpy(tab->samples->row[tab->n_sample], sample->el, sample->size);
859 isl_vec_free(sample);
860 tab->sample_index[tab->n_sample] = tab->n_sample;
865 isl_vec_free(sample);
870 struct isl_tab *isl_tab_drop_sample(struct isl_tab *tab, int s)
872 if (s != tab->n_outside) {
873 int t = tab->sample_index[tab->n_outside];
874 tab->sample_index[tab->n_outside] = tab->sample_index[s];
875 tab->sample_index[s] = t;
876 isl_mat_swap_rows(tab->samples, tab->n_outside, s);
879 if (isl_tab_push(tab, isl_tab_undo_drop_sample) < 0) {
887 /* Record the current number of samples so that we can remove newer
888 * samples during a rollback.
890 int isl_tab_save_samples(struct isl_tab *tab)
892 union isl_tab_undo_val u;
898 return push_union(tab, isl_tab_undo_saved_samples, u);
901 /* Mark row with index "row" as being redundant.
902 * If we may need to undo the operation or if the row represents
903 * a variable of the original problem, the row is kept,
904 * but no longer considered when looking for a pivot row.
905 * Otherwise, the row is simply removed.
907 * The row may be interchanged with some other row. If it
908 * is interchanged with a later row, return 1. Otherwise return 0.
909 * If the rows are checked in order in the calling function,
910 * then a return value of 1 means that the row with the given
911 * row number may now contain a different row that hasn't been checked yet.
913 int isl_tab_mark_redundant(struct isl_tab *tab, int row)
915 struct isl_tab_var *var = isl_tab_var_from_row(tab, row);
916 var->is_redundant = 1;
917 isl_assert(tab->mat->ctx, row >= tab->n_redundant, return -1);
918 if (tab->need_undo || tab->row_var[row] >= 0) {
919 if (tab->row_var[row] >= 0 && !var->is_nonneg) {
921 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, var) < 0)
924 if (row != tab->n_redundant)
925 swap_rows(tab, row, tab->n_redundant);
927 return isl_tab_push_var(tab, isl_tab_undo_redundant, var);
929 if (row != tab->n_row - 1)
930 swap_rows(tab, row, tab->n_row - 1);
931 isl_tab_var_from_row(tab, tab->n_row - 1)->index = -1;
937 int isl_tab_mark_empty(struct isl_tab *tab)
941 if (!tab->empty && tab->need_undo)
942 if (isl_tab_push(tab, isl_tab_undo_empty) < 0)
948 int isl_tab_freeze_constraint(struct isl_tab *tab, int con)
950 struct isl_tab_var *var;
955 var = &tab->con[con];
963 return isl_tab_push_var(tab, isl_tab_undo_freeze, var);
968 /* Update the rows signs after a pivot of "row" and "col", with "row_sgn"
969 * the original sign of the pivot element.
970 * We only keep track of row signs during PILP solving and in this case
971 * we only pivot a row with negative sign (meaning the value is always
972 * non-positive) using a positive pivot element.
974 * For each row j, the new value of the parametric constant is equal to
976 * a_j0 - a_jc a_r0/a_rc
978 * where a_j0 is the original parametric constant, a_rc is the pivot element,
979 * a_r0 is the parametric constant of the pivot row and a_jc is the
980 * pivot column entry of the row j.
981 * Since a_r0 is non-positive and a_rc is positive, the sign of row j
982 * remains the same if a_jc has the same sign as the row j or if
983 * a_jc is zero. In all other cases, we reset the sign to "unknown".
985 static void update_row_sign(struct isl_tab *tab, int row, int col, int row_sgn)
988 struct isl_mat *mat = tab->mat;
989 unsigned off = 2 + tab->M;
994 if (tab->row_sign[row] == 0)
996 isl_assert(mat->ctx, row_sgn > 0, return);
997 isl_assert(mat->ctx, tab->row_sign[row] == isl_tab_row_neg, return);
998 tab->row_sign[row] = isl_tab_row_pos;
999 for (i = 0; i < tab->n_row; ++i) {
1003 s = isl_int_sgn(mat->row[i][off + col]);
1006 if (!tab->row_sign[i])
1008 if (s < 0 && tab->row_sign[i] == isl_tab_row_neg)
1010 if (s > 0 && tab->row_sign[i] == isl_tab_row_pos)
1012 tab->row_sign[i] = isl_tab_row_unknown;
1016 /* Given a row number "row" and a column number "col", pivot the tableau
1017 * such that the associated variables are interchanged.
1018 * The given row in the tableau expresses
1020 * x_r = a_r0 + \sum_i a_ri x_i
1024 * x_c = 1/a_rc x_r - a_r0/a_rc + sum_{i \ne r} -a_ri/a_rc
1026 * Substituting this equality into the other rows
1028 * x_j = a_j0 + \sum_i a_ji x_i
1030 * with a_jc \ne 0, we obtain
1032 * 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
1039 * where i is any other column and j is any other row,
1040 * is therefore transformed into
1042 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1043 * 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)
1045 * The transformation is performed along the following steps
1047 * d_r/n_rc n_ri/n_rc
1050 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1053 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1054 * n_jc/(|n_rc| d_j) n_ji/(|n_rc| d_j)
1056 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1057 * n_jc/(|n_rc| d_j) (n_ji |n_rc|)/(|n_rc| d_j)
1059 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1060 * n_jc/(|n_rc| d_j) (n_ji |n_rc| - s(n_rc)n_jc n_ri)/(|n_rc| d_j)
1062 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1063 * 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)
1066 int isl_tab_pivot(struct isl_tab *tab, int row, int col)
1071 struct isl_mat *mat = tab->mat;
1072 struct isl_tab_var *var;
1073 unsigned off = 2 + tab->M;
1075 isl_int_swap(mat->row[row][0], mat->row[row][off + col]);
1076 sgn = isl_int_sgn(mat->row[row][0]);
1078 isl_int_neg(mat->row[row][0], mat->row[row][0]);
1079 isl_int_neg(mat->row[row][off + col], mat->row[row][off + col]);
1081 for (j = 0; j < off - 1 + tab->n_col; ++j) {
1082 if (j == off - 1 + col)
1084 isl_int_neg(mat->row[row][1 + j], mat->row[row][1 + j]);
1086 if (!isl_int_is_one(mat->row[row][0]))
1087 isl_seq_normalize(mat->ctx, mat->row[row], off + tab->n_col);
1088 for (i = 0; i < tab->n_row; ++i) {
1091 if (isl_int_is_zero(mat->row[i][off + col]))
1093 isl_int_mul(mat->row[i][0], mat->row[i][0], mat->row[row][0]);
1094 for (j = 0; j < off - 1 + tab->n_col; ++j) {
1095 if (j == off - 1 + col)
1097 isl_int_mul(mat->row[i][1 + j],
1098 mat->row[i][1 + j], mat->row[row][0]);
1099 isl_int_addmul(mat->row[i][1 + j],
1100 mat->row[i][off + col], mat->row[row][1 + j]);
1102 isl_int_mul(mat->row[i][off + col],
1103 mat->row[i][off + col], mat->row[row][off + col]);
1104 if (!isl_int_is_one(mat->row[i][0]))
1105 isl_seq_normalize(mat->ctx, mat->row[i], off + tab->n_col);
1107 t = tab->row_var[row];
1108 tab->row_var[row] = tab->col_var[col];
1109 tab->col_var[col] = t;
1110 var = isl_tab_var_from_row(tab, row);
1113 var = var_from_col(tab, col);
1116 update_row_sign(tab, row, col, sgn);
1119 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1120 if (isl_int_is_zero(mat->row[i][off + col]))
1122 if (!isl_tab_var_from_row(tab, i)->frozen &&
1123 isl_tab_row_is_redundant(tab, i)) {
1124 int redo = isl_tab_mark_redundant(tab, i);
1134 /* If "var" represents a column variable, then pivot is up (sgn > 0)
1135 * or down (sgn < 0) to a row. The variable is assumed not to be
1136 * unbounded in the specified direction.
1137 * If sgn = 0, then the variable is unbounded in both directions,
1138 * and we pivot with any row we can find.
1140 static int to_row(struct isl_tab *tab, struct isl_tab_var *var, int sign) WARN_UNUSED;
1141 static int to_row(struct isl_tab *tab, struct isl_tab_var *var, int sign)
1144 unsigned off = 2 + tab->M;
1150 for (r = tab->n_redundant; r < tab->n_row; ++r)
1151 if (!isl_int_is_zero(tab->mat->row[r][off+var->index]))
1153 isl_assert(tab->mat->ctx, r < tab->n_row, return -1);
1155 r = pivot_row(tab, NULL, sign, var->index);
1156 isl_assert(tab->mat->ctx, r >= 0, return -1);
1159 return isl_tab_pivot(tab, r, var->index);
1162 static void check_table(struct isl_tab *tab)
1168 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1169 struct isl_tab_var *var;
1170 var = isl_tab_var_from_row(tab, i);
1171 if (!var->is_nonneg)
1174 assert(!isl_int_is_neg(tab->mat->row[i][2]));
1175 if (isl_int_is_pos(tab->mat->row[i][2]))
1178 assert(!isl_int_is_neg(tab->mat->row[i][1]));
1182 /* Return the sign of the maximal value of "var".
1183 * If the sign is not negative, then on return from this function,
1184 * the sample value will also be non-negative.
1186 * If "var" is manifestly unbounded wrt positive values, we are done.
1187 * Otherwise, we pivot the variable up to a row if needed
1188 * Then we continue pivoting down until either
1189 * - no more down pivots can be performed
1190 * - the sample value is positive
1191 * - the variable is pivoted into a manifestly unbounded column
1193 static int sign_of_max(struct isl_tab *tab, struct isl_tab_var *var)
1197 if (max_is_manifestly_unbounded(tab, var))
1199 if (to_row(tab, var, 1) < 0)
1201 while (!isl_int_is_pos(tab->mat->row[var->index][1])) {
1202 find_pivot(tab, var, var, 1, &row, &col);
1204 return isl_int_sgn(tab->mat->row[var->index][1]);
1205 if (isl_tab_pivot(tab, row, col) < 0)
1207 if (!var->is_row) /* manifestly unbounded */
1213 static int row_is_neg(struct isl_tab *tab, int row)
1216 return isl_int_is_neg(tab->mat->row[row][1]);
1217 if (isl_int_is_pos(tab->mat->row[row][2]))
1219 if (isl_int_is_neg(tab->mat->row[row][2]))
1221 return isl_int_is_neg(tab->mat->row[row][1]);
1224 static int row_sgn(struct isl_tab *tab, int row)
1227 return isl_int_sgn(tab->mat->row[row][1]);
1228 if (!isl_int_is_zero(tab->mat->row[row][2]))
1229 return isl_int_sgn(tab->mat->row[row][2]);
1231 return isl_int_sgn(tab->mat->row[row][1]);
1234 /* Perform pivots until the row variable "var" has a non-negative
1235 * sample value or until no more upward pivots can be performed.
1236 * Return the sign of the sample value after the pivots have been
1239 static int restore_row(struct isl_tab *tab, struct isl_tab_var *var)
1243 while (row_is_neg(tab, var->index)) {
1244 find_pivot(tab, var, var, 1, &row, &col);
1247 if (isl_tab_pivot(tab, row, col) < 0)
1249 if (!var->is_row) /* manifestly unbounded */
1252 return row_sgn(tab, var->index);
1255 /* Perform pivots until we are sure that the row variable "var"
1256 * can attain non-negative values. After return from this
1257 * function, "var" is still a row variable, but its sample
1258 * value may not be non-negative, even if the function returns 1.
1260 static int at_least_zero(struct isl_tab *tab, struct isl_tab_var *var)
1264 while (isl_int_is_neg(tab->mat->row[var->index][1])) {
1265 find_pivot(tab, var, var, 1, &row, &col);
1268 if (row == var->index) /* manifestly unbounded */
1270 if (isl_tab_pivot(tab, row, col) < 0)
1273 return !isl_int_is_neg(tab->mat->row[var->index][1]);
1276 /* Return a negative value if "var" can attain negative values.
1277 * Return a non-negative value otherwise.
1279 * If "var" is manifestly unbounded wrt negative values, we are done.
1280 * Otherwise, if var is in a column, we can pivot it down to a row.
1281 * Then we continue pivoting down until either
1282 * - the pivot would result in a manifestly unbounded column
1283 * => we don't perform the pivot, but simply return -1
1284 * - no more down pivots can be performed
1285 * - the sample value is negative
1286 * If the sample value becomes negative and the variable is supposed
1287 * to be nonnegative, then we undo the last pivot.
1288 * However, if the last pivot has made the pivoting variable
1289 * obviously redundant, then it may have moved to another row.
1290 * In that case we look for upward pivots until we reach a non-negative
1293 static int sign_of_min(struct isl_tab *tab, struct isl_tab_var *var)
1296 struct isl_tab_var *pivot_var = NULL;
1298 if (min_is_manifestly_unbounded(tab, var))
1302 row = pivot_row(tab, NULL, -1, col);
1303 pivot_var = var_from_col(tab, col);
1304 if (isl_tab_pivot(tab, row, col) < 0)
1306 if (var->is_redundant)
1308 if (isl_int_is_neg(tab->mat->row[var->index][1])) {
1309 if (var->is_nonneg) {
1310 if (!pivot_var->is_redundant &&
1311 pivot_var->index == row) {
1312 if (isl_tab_pivot(tab, row, col) < 0)
1315 if (restore_row(tab, var) < -1)
1321 if (var->is_redundant)
1323 while (!isl_int_is_neg(tab->mat->row[var->index][1])) {
1324 find_pivot(tab, var, var, -1, &row, &col);
1325 if (row == var->index)
1328 return isl_int_sgn(tab->mat->row[var->index][1]);
1329 pivot_var = var_from_col(tab, col);
1330 if (isl_tab_pivot(tab, row, col) < 0)
1332 if (var->is_redundant)
1335 if (pivot_var && var->is_nonneg) {
1336 /* pivot back to non-negative value */
1337 if (!pivot_var->is_redundant && pivot_var->index == row) {
1338 if (isl_tab_pivot(tab, row, col) < 0)
1341 if (restore_row(tab, var) < -1)
1347 static int row_at_most_neg_one(struct isl_tab *tab, int row)
1350 if (isl_int_is_pos(tab->mat->row[row][2]))
1352 if (isl_int_is_neg(tab->mat->row[row][2]))
1355 return isl_int_is_neg(tab->mat->row[row][1]) &&
1356 isl_int_abs_ge(tab->mat->row[row][1],
1357 tab->mat->row[row][0]);
1360 /* Return 1 if "var" can attain values <= -1.
1361 * Return 0 otherwise.
1363 * The sample value of "var" is assumed to be non-negative when the
1364 * the function is called and will be made non-negative again before
1365 * the function returns.
1367 int isl_tab_min_at_most_neg_one(struct isl_tab *tab, struct isl_tab_var *var)
1370 struct isl_tab_var *pivot_var;
1372 if (min_is_manifestly_unbounded(tab, var))
1376 row = pivot_row(tab, NULL, -1, col);
1377 pivot_var = var_from_col(tab, col);
1378 if (isl_tab_pivot(tab, row, col) < 0)
1380 if (var->is_redundant)
1382 if (row_at_most_neg_one(tab, var->index)) {
1383 if (var->is_nonneg) {
1384 if (!pivot_var->is_redundant &&
1385 pivot_var->index == row) {
1386 if (isl_tab_pivot(tab, row, col) < 0)
1389 if (restore_row(tab, var) < -1)
1395 if (var->is_redundant)
1398 find_pivot(tab, var, var, -1, &row, &col);
1399 if (row == var->index)
1403 pivot_var = var_from_col(tab, col);
1404 if (isl_tab_pivot(tab, row, col) < 0)
1406 if (var->is_redundant)
1408 } while (!row_at_most_neg_one(tab, var->index));
1409 if (var->is_nonneg) {
1410 /* pivot back to non-negative value */
1411 if (!pivot_var->is_redundant && pivot_var->index == row)
1412 if (isl_tab_pivot(tab, row, col) < 0)
1414 if (restore_row(tab, var) < -1)
1420 /* Return 1 if "var" can attain values >= 1.
1421 * Return 0 otherwise.
1423 static int at_least_one(struct isl_tab *tab, struct isl_tab_var *var)
1428 if (max_is_manifestly_unbounded(tab, var))
1430 if (to_row(tab, var, 1) < 0)
1432 r = tab->mat->row[var->index];
1433 while (isl_int_lt(r[1], r[0])) {
1434 find_pivot(tab, var, var, 1, &row, &col);
1436 return isl_int_ge(r[1], r[0]);
1437 if (row == var->index) /* manifestly unbounded */
1439 if (isl_tab_pivot(tab, row, col) < 0)
1445 static void swap_cols(struct isl_tab *tab, int col1, int col2)
1448 unsigned off = 2 + tab->M;
1449 t = tab->col_var[col1];
1450 tab->col_var[col1] = tab->col_var[col2];
1451 tab->col_var[col2] = t;
1452 var_from_col(tab, col1)->index = col1;
1453 var_from_col(tab, col2)->index = col2;
1454 tab->mat = isl_mat_swap_cols(tab->mat, off + col1, off + col2);
1457 /* Mark column with index "col" as representing a zero variable.
1458 * If we may need to undo the operation the column is kept,
1459 * but no longer considered.
1460 * Otherwise, the column is simply removed.
1462 * The column may be interchanged with some other column. If it
1463 * is interchanged with a later column, return 1. Otherwise return 0.
1464 * If the columns are checked in order in the calling function,
1465 * then a return value of 1 means that the column with the given
1466 * column number may now contain a different column that
1467 * hasn't been checked yet.
1469 int isl_tab_kill_col(struct isl_tab *tab, int col)
1471 var_from_col(tab, col)->is_zero = 1;
1472 if (tab->need_undo) {
1473 if (isl_tab_push_var(tab, isl_tab_undo_zero,
1474 var_from_col(tab, col)) < 0)
1476 if (col != tab->n_dead)
1477 swap_cols(tab, col, tab->n_dead);
1481 if (col != tab->n_col - 1)
1482 swap_cols(tab, col, tab->n_col - 1);
1483 var_from_col(tab, tab->n_col - 1)->index = -1;
1489 /* Row variable "var" is non-negative and cannot attain any values
1490 * larger than zero. This means that the coefficients of the unrestricted
1491 * column variables are zero and that the coefficients of the non-negative
1492 * column variables are zero or negative.
1493 * Each of the non-negative variables with a negative coefficient can
1494 * then also be written as the negative sum of non-negative variables
1495 * and must therefore also be zero.
1497 static int close_row(struct isl_tab *tab, struct isl_tab_var *var) WARN_UNUSED;
1498 static int close_row(struct isl_tab *tab, struct isl_tab_var *var)
1501 struct isl_mat *mat = tab->mat;
1502 unsigned off = 2 + tab->M;
1504 isl_assert(tab->mat->ctx, var->is_nonneg, return -1);
1507 if (isl_tab_push_var(tab, isl_tab_undo_zero, var) < 0)
1509 for (j = tab->n_dead; j < tab->n_col; ++j) {
1510 if (isl_int_is_zero(mat->row[var->index][off + j]))
1512 isl_assert(tab->mat->ctx,
1513 isl_int_is_neg(mat->row[var->index][off + j]), return -1);
1514 if (isl_tab_kill_col(tab, j))
1517 if (isl_tab_mark_redundant(tab, var->index) < 0)
1522 /* Add a constraint to the tableau and allocate a row for it.
1523 * Return the index into the constraint array "con".
1525 int isl_tab_allocate_con(struct isl_tab *tab)
1529 isl_assert(tab->mat->ctx, tab->n_row < tab->mat->n_row, return -1);
1530 isl_assert(tab->mat->ctx, tab->n_con < tab->max_con, return -1);
1533 tab->con[r].index = tab->n_row;
1534 tab->con[r].is_row = 1;
1535 tab->con[r].is_nonneg = 0;
1536 tab->con[r].is_zero = 0;
1537 tab->con[r].is_redundant = 0;
1538 tab->con[r].frozen = 0;
1539 tab->con[r].negated = 0;
1540 tab->row_var[tab->n_row] = ~r;
1544 if (isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->con[r]) < 0)
1550 /* Add a variable to the tableau and allocate a column for it.
1551 * Return the index into the variable array "var".
1553 int isl_tab_allocate_var(struct isl_tab *tab)
1557 unsigned off = 2 + tab->M;
1559 isl_assert(tab->mat->ctx, tab->n_col < tab->mat->n_col, return -1);
1560 isl_assert(tab->mat->ctx, tab->n_var < tab->max_var, return -1);
1563 tab->var[r].index = tab->n_col;
1564 tab->var[r].is_row = 0;
1565 tab->var[r].is_nonneg = 0;
1566 tab->var[r].is_zero = 0;
1567 tab->var[r].is_redundant = 0;
1568 tab->var[r].frozen = 0;
1569 tab->var[r].negated = 0;
1570 tab->col_var[tab->n_col] = r;
1572 for (i = 0; i < tab->n_row; ++i)
1573 isl_int_set_si(tab->mat->row[i][off + tab->n_col], 0);
1577 if (isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->var[r]) < 0)
1583 /* Add a row to the tableau. The row is given as an affine combination
1584 * of the original variables and needs to be expressed in terms of the
1587 * We add each term in turn.
1588 * If r = n/d_r is the current sum and we need to add k x, then
1589 * if x is a column variable, we increase the numerator of
1590 * this column by k d_r
1591 * if x = f/d_x is a row variable, then the new representation of r is
1593 * n k f d_x/g n + d_r/g k f m/d_r n + m/d_g k f
1594 * --- + --- = ------------------- = -------------------
1595 * d_r d_r d_r d_x/g m
1597 * with g the gcd of d_r and d_x and m the lcm of d_r and d_x.
1599 int isl_tab_add_row(struct isl_tab *tab, isl_int *line)
1605 unsigned off = 2 + tab->M;
1607 r = isl_tab_allocate_con(tab);
1613 row = tab->mat->row[tab->con[r].index];
1614 isl_int_set_si(row[0], 1);
1615 isl_int_set(row[1], line[0]);
1616 isl_seq_clr(row + 2, tab->M + tab->n_col);
1617 for (i = 0; i < tab->n_var; ++i) {
1618 if (tab->var[i].is_zero)
1620 if (tab->var[i].is_row) {
1622 row[0], tab->mat->row[tab->var[i].index][0]);
1623 isl_int_swap(a, row[0]);
1624 isl_int_divexact(a, row[0], a);
1626 row[0], tab->mat->row[tab->var[i].index][0]);
1627 isl_int_mul(b, b, line[1 + i]);
1628 isl_seq_combine(row + 1, a, row + 1,
1629 b, tab->mat->row[tab->var[i].index] + 1,
1630 1 + tab->M + tab->n_col);
1632 isl_int_addmul(row[off + tab->var[i].index],
1633 line[1 + i], row[0]);
1634 if (tab->M && i >= tab->n_param && i < tab->n_var - tab->n_div)
1635 isl_int_submul(row[2], line[1 + i], row[0]);
1637 isl_seq_normalize(tab->mat->ctx, row, off + tab->n_col);
1642 tab->row_sign[tab->con[r].index] = 0;
1647 static int drop_row(struct isl_tab *tab, int row)
1649 isl_assert(tab->mat->ctx, ~tab->row_var[row] == tab->n_con - 1, return -1);
1650 if (row != tab->n_row - 1)
1651 swap_rows(tab, row, tab->n_row - 1);
1657 static int drop_col(struct isl_tab *tab, int col)
1659 isl_assert(tab->mat->ctx, tab->col_var[col] == tab->n_var - 1, return -1);
1660 if (col != tab->n_col - 1)
1661 swap_cols(tab, col, tab->n_col - 1);
1667 /* Add inequality "ineq" and check if it conflicts with the
1668 * previously added constraints or if it is obviously redundant.
1670 int isl_tab_add_ineq(struct isl_tab *tab, isl_int *ineq)
1679 struct isl_basic_map *bmap = tab->bmap;
1681 isl_assert(tab->mat->ctx, tab->n_eq == bmap->n_eq, return -1);
1682 isl_assert(tab->mat->ctx,
1683 tab->n_con == bmap->n_eq + bmap->n_ineq, return -1);
1684 tab->bmap = isl_basic_map_add_ineq(tab->bmap, ineq);
1685 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1692 isl_int_swap(ineq[0], cst);
1694 r = isl_tab_add_row(tab, ineq);
1696 isl_int_swap(ineq[0], cst);
1701 tab->con[r].is_nonneg = 1;
1702 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1704 if (isl_tab_row_is_redundant(tab, tab->con[r].index)) {
1705 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1710 sgn = restore_row(tab, &tab->con[r]);
1714 return isl_tab_mark_empty(tab);
1715 if (tab->con[r].is_row && isl_tab_row_is_redundant(tab, tab->con[r].index))
1716 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1721 /* Pivot a non-negative variable down until it reaches the value zero
1722 * and then pivot the variable into a column position.
1724 static int to_col(struct isl_tab *tab, struct isl_tab_var *var) WARN_UNUSED;
1725 static int to_col(struct isl_tab *tab, struct isl_tab_var *var)
1729 unsigned off = 2 + tab->M;
1734 while (isl_int_is_pos(tab->mat->row[var->index][1])) {
1735 find_pivot(tab, var, NULL, -1, &row, &col);
1736 isl_assert(tab->mat->ctx, row != -1, return -1);
1737 if (isl_tab_pivot(tab, row, col) < 0)
1743 for (i = tab->n_dead; i < tab->n_col; ++i)
1744 if (!isl_int_is_zero(tab->mat->row[var->index][off + i]))
1747 isl_assert(tab->mat->ctx, i < tab->n_col, return -1);
1748 if (isl_tab_pivot(tab, var->index, i) < 0)
1754 /* We assume Gaussian elimination has been performed on the equalities.
1755 * The equalities can therefore never conflict.
1756 * Adding the equalities is currently only really useful for a later call
1757 * to isl_tab_ineq_type.
1759 static struct isl_tab *add_eq(struct isl_tab *tab, isl_int *eq)
1766 r = isl_tab_add_row(tab, eq);
1770 r = tab->con[r].index;
1771 i = isl_seq_first_non_zero(tab->mat->row[r] + 2 + tab->M + tab->n_dead,
1772 tab->n_col - tab->n_dead);
1773 isl_assert(tab->mat->ctx, i >= 0, goto error);
1775 if (isl_tab_pivot(tab, r, i) < 0)
1777 if (isl_tab_kill_col(tab, i) < 0)
1787 static int row_is_manifestly_zero(struct isl_tab *tab, int row)
1789 unsigned off = 2 + tab->M;
1791 if (!isl_int_is_zero(tab->mat->row[row][1]))
1793 if (tab->M && !isl_int_is_zero(tab->mat->row[row][2]))
1795 return isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
1796 tab->n_col - tab->n_dead) == -1;
1799 /* Add an equality that is known to be valid for the given tableau.
1801 struct isl_tab *isl_tab_add_valid_eq(struct isl_tab *tab, isl_int *eq)
1803 struct isl_tab_var *var;
1808 r = isl_tab_add_row(tab, eq);
1814 if (row_is_manifestly_zero(tab, r)) {
1816 if (isl_tab_mark_redundant(tab, r) < 0)
1821 if (isl_int_is_neg(tab->mat->row[r][1])) {
1822 isl_seq_neg(tab->mat->row[r] + 1, tab->mat->row[r] + 1,
1827 if (to_col(tab, var) < 0)
1830 if (isl_tab_kill_col(tab, var->index) < 0)
1839 static int add_zero_row(struct isl_tab *tab)
1844 r = isl_tab_allocate_con(tab);
1848 row = tab->mat->row[tab->con[r].index];
1849 isl_seq_clr(row + 1, 1 + tab->M + tab->n_col);
1850 isl_int_set_si(row[0], 1);
1855 /* Add equality "eq" and check if it conflicts with the
1856 * previously added constraints or if it is obviously redundant.
1858 struct isl_tab *isl_tab_add_eq(struct isl_tab *tab, isl_int *eq)
1860 struct isl_tab_undo *snap = NULL;
1861 struct isl_tab_var *var;
1869 isl_assert(tab->mat->ctx, !tab->M, goto error);
1872 snap = isl_tab_snap(tab);
1876 isl_int_swap(eq[0], cst);
1878 r = isl_tab_add_row(tab, eq);
1880 isl_int_swap(eq[0], cst);
1888 if (row_is_manifestly_zero(tab, row)) {
1890 if (isl_tab_rollback(tab, snap) < 0)
1898 tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
1899 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1901 isl_seq_neg(eq, eq, 1 + tab->n_var);
1902 tab->bmap = isl_basic_map_add_ineq(tab->bmap, eq);
1903 isl_seq_neg(eq, eq, 1 + tab->n_var);
1904 if (isl_tab_push(tab, isl_tab_undo_bmap_ineq) < 0)
1908 if (add_zero_row(tab) < 0)
1912 sgn = isl_int_sgn(tab->mat->row[row][1]);
1915 isl_seq_neg(tab->mat->row[row] + 1, tab->mat->row[row] + 1,
1922 sgn = sign_of_max(tab, var);
1926 if (isl_tab_mark_empty(tab) < 0)
1933 if (to_col(tab, var) < 0)
1936 if (isl_tab_kill_col(tab, var->index) < 0)
1945 /* Construct and return an inequality that expresses an upper bound
1947 * In particular, if the div is given by
1951 * then the inequality expresses
1955 static struct isl_vec *ineq_for_div(struct isl_basic_map *bmap, unsigned div)
1959 struct isl_vec *ineq;
1964 total = isl_basic_map_total_dim(bmap);
1965 div_pos = 1 + total - bmap->n_div + div;
1967 ineq = isl_vec_alloc(bmap->ctx, 1 + total);
1971 isl_seq_cpy(ineq->el, bmap->div[div] + 1, 1 + total);
1972 isl_int_neg(ineq->el[div_pos], bmap->div[div][0]);
1976 /* For a div d = floor(f/m), add the constraints
1979 * -(f-(m-1)) + m d >= 0
1981 * Note that the second constraint is the negation of
1985 * If add_ineq is not NULL, then this function is used
1986 * instead of isl_tab_add_ineq to effectively add the inequalities.
1988 static int add_div_constraints(struct isl_tab *tab, unsigned div,
1989 int (*add_ineq)(void *user, isl_int *), void *user)
1993 struct isl_vec *ineq;
1995 total = isl_basic_map_total_dim(tab->bmap);
1996 div_pos = 1 + total - tab->bmap->n_div + div;
1998 ineq = ineq_for_div(tab->bmap, div);
2003 if (add_ineq(user, ineq->el) < 0)
2006 if (isl_tab_add_ineq(tab, ineq->el) < 0)
2010 isl_seq_neg(ineq->el, tab->bmap->div[div] + 1, 1 + total);
2011 isl_int_set(ineq->el[div_pos], tab->bmap->div[div][0]);
2012 isl_int_add(ineq->el[0], ineq->el[0], ineq->el[div_pos]);
2013 isl_int_sub_ui(ineq->el[0], ineq->el[0], 1);
2016 if (add_ineq(user, ineq->el) < 0)
2019 if (isl_tab_add_ineq(tab, ineq->el) < 0)
2031 /* Add an extra div, prescrived by "div" to the tableau and
2032 * the associated bmap (which is assumed to be non-NULL).
2034 * If add_ineq is not NULL, then this function is used instead
2035 * of isl_tab_add_ineq to add the div constraints.
2036 * This complication is needed because the code in isl_tab_pip
2037 * wants to perform some extra processing when an inequality
2038 * is added to the tableau.
2040 int isl_tab_add_div(struct isl_tab *tab, __isl_keep isl_vec *div,
2041 int (*add_ineq)(void *user, isl_int *), void *user)
2051 isl_assert(tab->mat->ctx, tab->bmap, return -1);
2053 for (i = 0; i < tab->n_var; ++i) {
2054 if (isl_int_is_neg(div->el[2 + i]))
2056 if (isl_int_is_zero(div->el[2 + i]))
2058 if (!tab->var[i].is_nonneg)
2061 nonneg = i == tab->n_var && !isl_int_is_neg(div->el[1]);
2063 if (isl_tab_extend_cons(tab, 3) < 0)
2065 if (isl_tab_extend_vars(tab, 1) < 0)
2067 r = isl_tab_allocate_var(tab);
2072 tab->var[r].is_nonneg = 1;
2074 tab->bmap = isl_basic_map_extend_dim(tab->bmap,
2075 isl_basic_map_get_dim(tab->bmap), 1, 0, 2);
2076 k = isl_basic_map_alloc_div(tab->bmap);
2079 isl_seq_cpy(tab->bmap->div[k], div->el, div->size);
2080 if (isl_tab_push(tab, isl_tab_undo_bmap_div) < 0)
2083 if (add_div_constraints(tab, k, add_ineq, user) < 0)
2089 struct isl_tab *isl_tab_from_basic_map(struct isl_basic_map *bmap)
2092 struct isl_tab *tab;
2096 tab = isl_tab_alloc(bmap->ctx,
2097 isl_basic_map_total_dim(bmap) + bmap->n_ineq + 1,
2098 isl_basic_map_total_dim(bmap), 0);
2101 tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
2102 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) {
2103 if (isl_tab_mark_empty(tab) < 0)
2107 for (i = 0; i < bmap->n_eq; ++i) {
2108 tab = add_eq(tab, bmap->eq[i]);
2112 for (i = 0; i < bmap->n_ineq; ++i) {
2113 if (isl_tab_add_ineq(tab, bmap->ineq[i]) < 0)
2124 struct isl_tab *isl_tab_from_basic_set(struct isl_basic_set *bset)
2126 return isl_tab_from_basic_map((struct isl_basic_map *)bset);
2129 /* Construct a tableau corresponding to the recession cone of "bset".
2131 struct isl_tab *isl_tab_from_recession_cone(struct isl_basic_set *bset)
2135 struct isl_tab *tab;
2139 tab = isl_tab_alloc(bset->ctx, bset->n_eq + bset->n_ineq,
2140 isl_basic_set_total_dim(bset), 0);
2143 tab->rational = ISL_F_ISSET(bset, ISL_BASIC_SET_RATIONAL);
2147 for (i = 0; i < bset->n_eq; ++i) {
2148 isl_int_swap(bset->eq[i][0], cst);
2149 tab = add_eq(tab, bset->eq[i]);
2150 isl_int_swap(bset->eq[i][0], cst);
2154 for (i = 0; i < bset->n_ineq; ++i) {
2156 isl_int_swap(bset->ineq[i][0], cst);
2157 r = isl_tab_add_row(tab, bset->ineq[i]);
2158 isl_int_swap(bset->ineq[i][0], cst);
2161 tab->con[r].is_nonneg = 1;
2162 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
2174 /* Assuming "tab" is the tableau of a cone, check if the cone is
2175 * bounded, i.e., if it is empty or only contains the origin.
2177 int isl_tab_cone_is_bounded(struct isl_tab *tab)
2185 if (tab->n_dead == tab->n_col)
2189 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2190 struct isl_tab_var *var;
2192 var = isl_tab_var_from_row(tab, i);
2193 if (!var->is_nonneg)
2195 sgn = sign_of_max(tab, var);
2200 if (close_row(tab, var) < 0)
2204 if (tab->n_dead == tab->n_col)
2206 if (i == tab->n_row)
2211 int isl_tab_sample_is_integer(struct isl_tab *tab)
2218 for (i = 0; i < tab->n_var; ++i) {
2220 if (!tab->var[i].is_row)
2222 row = tab->var[i].index;
2223 if (!isl_int_is_divisible_by(tab->mat->row[row][1],
2224 tab->mat->row[row][0]))
2230 static struct isl_vec *extract_integer_sample(struct isl_tab *tab)
2233 struct isl_vec *vec;
2235 vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
2239 isl_int_set_si(vec->block.data[0], 1);
2240 for (i = 0; i < tab->n_var; ++i) {
2241 if (!tab->var[i].is_row)
2242 isl_int_set_si(vec->block.data[1 + i], 0);
2244 int row = tab->var[i].index;
2245 isl_int_divexact(vec->block.data[1 + i],
2246 tab->mat->row[row][1], tab->mat->row[row][0]);
2253 struct isl_vec *isl_tab_get_sample_value(struct isl_tab *tab)
2256 struct isl_vec *vec;
2262 vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
2268 isl_int_set_si(vec->block.data[0], 1);
2269 for (i = 0; i < tab->n_var; ++i) {
2271 if (!tab->var[i].is_row) {
2272 isl_int_set_si(vec->block.data[1 + i], 0);
2275 row = tab->var[i].index;
2276 isl_int_gcd(m, vec->block.data[0], tab->mat->row[row][0]);
2277 isl_int_divexact(m, tab->mat->row[row][0], m);
2278 isl_seq_scale(vec->block.data, vec->block.data, m, 1 + i);
2279 isl_int_divexact(m, vec->block.data[0], tab->mat->row[row][0]);
2280 isl_int_mul(vec->block.data[1 + i], m, tab->mat->row[row][1]);
2282 vec = isl_vec_normalize(vec);
2288 /* Update "bmap" based on the results of the tableau "tab".
2289 * In particular, implicit equalities are made explicit, redundant constraints
2290 * are removed and if the sample value happens to be integer, it is stored
2291 * in "bmap" (unless "bmap" already had an integer sample).
2293 * The tableau is assumed to have been created from "bmap" using
2294 * isl_tab_from_basic_map.
2296 struct isl_basic_map *isl_basic_map_update_from_tab(struct isl_basic_map *bmap,
2297 struct isl_tab *tab)
2309 bmap = isl_basic_map_set_to_empty(bmap);
2311 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2312 if (isl_tab_is_equality(tab, n_eq + i))
2313 isl_basic_map_inequality_to_equality(bmap, i);
2314 else if (isl_tab_is_redundant(tab, n_eq + i))
2315 isl_basic_map_drop_inequality(bmap, i);
2317 if (bmap->n_eq != n_eq)
2318 isl_basic_map_gauss(bmap, NULL);
2319 if (!tab->rational &&
2320 !bmap->sample && isl_tab_sample_is_integer(tab))
2321 bmap->sample = extract_integer_sample(tab);
2325 struct isl_basic_set *isl_basic_set_update_from_tab(struct isl_basic_set *bset,
2326 struct isl_tab *tab)
2328 return (struct isl_basic_set *)isl_basic_map_update_from_tab(
2329 (struct isl_basic_map *)bset, tab);
2332 /* Given a non-negative variable "var", add a new non-negative variable
2333 * that is the opposite of "var", ensuring that var can only attain the
2335 * If var = n/d is a row variable, then the new variable = -n/d.
2336 * If var is a column variables, then the new variable = -var.
2337 * If the new variable cannot attain non-negative values, then
2338 * the resulting tableau is empty.
2339 * Otherwise, we know the value will be zero and we close the row.
2341 static struct isl_tab *cut_to_hyperplane(struct isl_tab *tab,
2342 struct isl_tab_var *var)
2347 unsigned off = 2 + tab->M;
2351 isl_assert(tab->mat->ctx, !var->is_redundant, goto error);
2352 isl_assert(tab->mat->ctx, var->is_nonneg, goto error);
2354 if (isl_tab_extend_cons(tab, 1) < 0)
2358 tab->con[r].index = tab->n_row;
2359 tab->con[r].is_row = 1;
2360 tab->con[r].is_nonneg = 0;
2361 tab->con[r].is_zero = 0;
2362 tab->con[r].is_redundant = 0;
2363 tab->con[r].frozen = 0;
2364 tab->con[r].negated = 0;
2365 tab->row_var[tab->n_row] = ~r;
2366 row = tab->mat->row[tab->n_row];
2369 isl_int_set(row[0], tab->mat->row[var->index][0]);
2370 isl_seq_neg(row + 1,
2371 tab->mat->row[var->index] + 1, 1 + tab->n_col);
2373 isl_int_set_si(row[0], 1);
2374 isl_seq_clr(row + 1, 1 + tab->n_col);
2375 isl_int_set_si(row[off + var->index], -1);
2380 if (isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->con[r]) < 0)
2383 sgn = sign_of_max(tab, &tab->con[r]);
2387 if (isl_tab_mark_empty(tab) < 0)
2391 tab->con[r].is_nonneg = 1;
2392 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
2395 if (close_row(tab, &tab->con[r]) < 0)
2404 /* Given a tableau "tab" and an inequality constraint "con" of the tableau,
2405 * relax the inequality by one. That is, the inequality r >= 0 is replaced
2406 * by r' = r + 1 >= 0.
2407 * If r is a row variable, we simply increase the constant term by one
2408 * (taking into account the denominator).
2409 * If r is a column variable, then we need to modify each row that
2410 * refers to r = r' - 1 by substituting this equality, effectively
2411 * subtracting the coefficient of the column from the constant.
2412 * We should only do this if the minimum is manifestly unbounded,
2413 * however. Otherwise, we may end up with negative sample values
2414 * for non-negative variables.
2415 * So, if r is a column variable with a minimum that is not
2416 * manifestly unbounded, then we need to move it to a row.
2417 * However, the sample value of this row may be negative,
2418 * even after the relaxation, so we need to restore it.
2419 * We therefore prefer to pivot a column up to a row, if possible.
2421 struct isl_tab *isl_tab_relax(struct isl_tab *tab, int con)
2423 struct isl_tab_var *var;
2424 unsigned off = 2 + tab->M;
2429 var = &tab->con[con];
2431 if (!var->is_row && !max_is_manifestly_unbounded(tab, var))
2432 if (to_row(tab, var, 1) < 0)
2434 if (!var->is_row && !min_is_manifestly_unbounded(tab, var))
2435 if (to_row(tab, var, -1) < 0)
2439 isl_int_add(tab->mat->row[var->index][1],
2440 tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
2441 if (restore_row(tab, var) < 0)
2446 for (i = 0; i < tab->n_row; ++i) {
2447 if (isl_int_is_zero(tab->mat->row[i][off + var->index]))
2449 isl_int_sub(tab->mat->row[i][1], tab->mat->row[i][1],
2450 tab->mat->row[i][off + var->index]);
2455 if (isl_tab_push_var(tab, isl_tab_undo_relax, var) < 0)
2464 struct isl_tab *isl_tab_select_facet(struct isl_tab *tab, int con)
2469 return cut_to_hyperplane(tab, &tab->con[con]);
2472 static int may_be_equality(struct isl_tab *tab, int row)
2474 unsigned off = 2 + tab->M;
2475 return (tab->rational ? isl_int_is_zero(tab->mat->row[row][1])
2476 : isl_int_lt(tab->mat->row[row][1],
2477 tab->mat->row[row][0])) &&
2478 isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
2479 tab->n_col - tab->n_dead) != -1;
2482 /* Check for (near) equalities among the constraints.
2483 * A constraint is an equality if it is non-negative and if
2484 * its maximal value is either
2485 * - zero (in case of rational tableaus), or
2486 * - strictly less than 1 (in case of integer tableaus)
2488 * We first mark all non-redundant and non-dead variables that
2489 * are not frozen and not obviously not an equality.
2490 * Then we iterate over all marked variables if they can attain
2491 * any values larger than zero or at least one.
2492 * If the maximal value is zero, we mark any column variables
2493 * that appear in the row as being zero and mark the row as being redundant.
2494 * Otherwise, if the maximal value is strictly less than one (and the
2495 * tableau is integer), then we restrict the value to being zero
2496 * by adding an opposite non-negative variable.
2498 struct isl_tab *isl_tab_detect_implicit_equalities(struct isl_tab *tab)
2507 if (tab->n_dead == tab->n_col)
2511 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2512 struct isl_tab_var *var = isl_tab_var_from_row(tab, i);
2513 var->marked = !var->frozen && var->is_nonneg &&
2514 may_be_equality(tab, i);
2518 for (i = tab->n_dead; i < tab->n_col; ++i) {
2519 struct isl_tab_var *var = var_from_col(tab, i);
2520 var->marked = !var->frozen && var->is_nonneg;
2525 struct isl_tab_var *var;
2527 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2528 var = isl_tab_var_from_row(tab, i);
2532 if (i == tab->n_row) {
2533 for (i = tab->n_dead; i < tab->n_col; ++i) {
2534 var = var_from_col(tab, i);
2538 if (i == tab->n_col)
2543 sgn = sign_of_max(tab, var);
2547 if (close_row(tab, var) < 0)
2549 } else if (!tab->rational && !at_least_one(tab, var)) {
2550 tab = cut_to_hyperplane(tab, var);
2551 return isl_tab_detect_implicit_equalities(tab);
2553 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2554 var = isl_tab_var_from_row(tab, i);
2557 if (may_be_equality(tab, i))
2570 static int con_is_redundant(struct isl_tab *tab, struct isl_tab_var *var)
2574 if (tab->rational) {
2575 int sgn = sign_of_min(tab, var);
2580 int irred = isl_tab_min_at_most_neg_one(tab, var);
2587 /* Check for (near) redundant constraints.
2588 * A constraint is redundant if it is non-negative and if
2589 * its minimal value (temporarily ignoring the non-negativity) is either
2590 * - zero (in case of rational tableaus), or
2591 * - strictly larger than -1 (in case of integer tableaus)
2593 * We first mark all non-redundant and non-dead variables that
2594 * are not frozen and not obviously negatively unbounded.
2595 * Then we iterate over all marked variables if they can attain
2596 * any values smaller than zero or at most negative one.
2597 * If not, we mark the row as being redundant (assuming it hasn't
2598 * been detected as being obviously redundant in the mean time).
2600 int isl_tab_detect_redundant(struct isl_tab *tab)
2609 if (tab->n_redundant == tab->n_row)
2613 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2614 struct isl_tab_var *var = isl_tab_var_from_row(tab, i);
2615 var->marked = !var->frozen && var->is_nonneg;
2619 for (i = tab->n_dead; i < tab->n_col; ++i) {
2620 struct isl_tab_var *var = var_from_col(tab, i);
2621 var->marked = !var->frozen && var->is_nonneg &&
2622 !min_is_manifestly_unbounded(tab, var);
2627 struct isl_tab_var *var;
2629 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2630 var = isl_tab_var_from_row(tab, i);
2634 if (i == tab->n_row) {
2635 for (i = tab->n_dead; i < tab->n_col; ++i) {
2636 var = var_from_col(tab, i);
2640 if (i == tab->n_col)
2645 red = con_is_redundant(tab, var);
2648 if (red && !var->is_redundant)
2649 if (isl_tab_mark_redundant(tab, var->index) < 0)
2651 for (i = tab->n_dead; i < tab->n_col; ++i) {
2652 var = var_from_col(tab, i);
2655 if (!min_is_manifestly_unbounded(tab, var))
2665 int isl_tab_is_equality(struct isl_tab *tab, int con)
2672 if (tab->con[con].is_zero)
2674 if (tab->con[con].is_redundant)
2676 if (!tab->con[con].is_row)
2677 return tab->con[con].index < tab->n_dead;
2679 row = tab->con[con].index;
2682 return isl_int_is_zero(tab->mat->row[row][1]) &&
2683 isl_seq_first_non_zero(tab->mat->row[row] + 2 + tab->n_dead,
2684 tab->n_col - tab->n_dead) == -1;
2687 /* Return the minimial value of the affine expression "f" with denominator
2688 * "denom" in *opt, *opt_denom, assuming the tableau is not empty and
2689 * the expression cannot attain arbitrarily small values.
2690 * If opt_denom is NULL, then *opt is rounded up to the nearest integer.
2691 * The return value reflects the nature of the result (empty, unbounded,
2692 * minmimal value returned in *opt).
2694 enum isl_lp_result isl_tab_min(struct isl_tab *tab,
2695 isl_int *f, isl_int denom, isl_int *opt, isl_int *opt_denom,
2699 enum isl_lp_result res = isl_lp_ok;
2700 struct isl_tab_var *var;
2701 struct isl_tab_undo *snap;
2704 return isl_lp_empty;
2706 snap = isl_tab_snap(tab);
2707 r = isl_tab_add_row(tab, f);
2709 return isl_lp_error;
2711 isl_int_mul(tab->mat->row[var->index][0],
2712 tab->mat->row[var->index][0], denom);
2715 find_pivot(tab, var, var, -1, &row, &col);
2716 if (row == var->index) {
2717 res = isl_lp_unbounded;
2722 if (isl_tab_pivot(tab, row, col) < 0)
2723 return isl_lp_error;
2725 if (ISL_FL_ISSET(flags, ISL_TAB_SAVE_DUAL)) {
2728 isl_vec_free(tab->dual);
2729 tab->dual = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_con);
2731 return isl_lp_error;
2732 isl_int_set(tab->dual->el[0], tab->mat->row[var->index][0]);
2733 for (i = 0; i < tab->n_con; ++i) {
2735 if (tab->con[i].is_row) {
2736 isl_int_set_si(tab->dual->el[1 + i], 0);
2739 pos = 2 + tab->M + tab->con[i].index;
2740 if (tab->con[i].negated)
2741 isl_int_neg(tab->dual->el[1 + i],
2742 tab->mat->row[var->index][pos]);
2744 isl_int_set(tab->dual->el[1 + i],
2745 tab->mat->row[var->index][pos]);
2748 if (opt && res == isl_lp_ok) {
2750 isl_int_set(*opt, tab->mat->row[var->index][1]);
2751 isl_int_set(*opt_denom, tab->mat->row[var->index][0]);
2753 isl_int_cdiv_q(*opt, tab->mat->row[var->index][1],
2754 tab->mat->row[var->index][0]);
2756 if (isl_tab_rollback(tab, snap) < 0)
2757 return isl_lp_error;
2761 int isl_tab_is_redundant(struct isl_tab *tab, int con)
2765 if (tab->con[con].is_zero)
2767 if (tab->con[con].is_redundant)
2769 return tab->con[con].is_row && tab->con[con].index < tab->n_redundant;
2772 /* Take a snapshot of the tableau that can be restored by s call to
2775 struct isl_tab_undo *isl_tab_snap(struct isl_tab *tab)
2783 /* Undo the operation performed by isl_tab_relax.
2785 static int unrelax(struct isl_tab *tab, struct isl_tab_var *var) WARN_UNUSED;
2786 static int unrelax(struct isl_tab *tab, struct isl_tab_var *var)
2788 unsigned off = 2 + tab->M;
2790 if (!var->is_row && !max_is_manifestly_unbounded(tab, var))
2791 if (to_row(tab, var, 1) < 0)
2795 isl_int_sub(tab->mat->row[var->index][1],
2796 tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
2797 if (var->is_nonneg) {
2798 int sgn = restore_row(tab, var);
2799 isl_assert(tab->mat->ctx, sgn >= 0, return -1);
2804 for (i = 0; i < tab->n_row; ++i) {
2805 if (isl_int_is_zero(tab->mat->row[i][off + var->index]))
2807 isl_int_add(tab->mat->row[i][1], tab->mat->row[i][1],
2808 tab->mat->row[i][off + var->index]);
2816 static int perform_undo_var(struct isl_tab *tab, struct isl_tab_undo *undo) WARN_UNUSED;
2817 static int perform_undo_var(struct isl_tab *tab, struct isl_tab_undo *undo)
2819 struct isl_tab_var *var = var_from_index(tab, undo->u.var_index);
2820 switch(undo->type) {
2821 case isl_tab_undo_nonneg:
2824 case isl_tab_undo_redundant:
2825 var->is_redundant = 0;
2828 case isl_tab_undo_freeze:
2831 case isl_tab_undo_zero:
2836 case isl_tab_undo_allocate:
2837 if (undo->u.var_index >= 0) {
2838 isl_assert(tab->mat->ctx, !var->is_row, return -1);
2839 drop_col(tab, var->index);
2843 if (!max_is_manifestly_unbounded(tab, var)) {
2844 if (to_row(tab, var, 1) < 0)
2846 } else if (!min_is_manifestly_unbounded(tab, var)) {
2847 if (to_row(tab, var, -1) < 0)
2850 if (to_row(tab, var, 0) < 0)
2853 drop_row(tab, var->index);
2855 case isl_tab_undo_relax:
2856 return unrelax(tab, var);
2862 /* Restore the tableau to the state where the basic variables
2863 * are those in "col_var".
2864 * We first construct a list of variables that are currently in
2865 * the basis, but shouldn't. Then we iterate over all variables
2866 * that should be in the basis and for each one that is currently
2867 * not in the basis, we exchange it with one of the elements of the
2868 * list constructed before.
2869 * We can always find an appropriate variable to pivot with because
2870 * the current basis is mapped to the old basis by a non-singular
2871 * matrix and so we can never end up with a zero row.
2873 static int restore_basis(struct isl_tab *tab, int *col_var)
2877 int *extra = NULL; /* current columns that contain bad stuff */
2878 unsigned off = 2 + tab->M;
2880 extra = isl_alloc_array(tab->mat->ctx, int, tab->n_col);
2883 for (i = 0; i < tab->n_col; ++i) {
2884 for (j = 0; j < tab->n_col; ++j)
2885 if (tab->col_var[i] == col_var[j])
2889 extra[n_extra++] = i;
2891 for (i = 0; i < tab->n_col && n_extra > 0; ++i) {
2892 struct isl_tab_var *var;
2895 for (j = 0; j < tab->n_col; ++j)
2896 if (col_var[i] == tab->col_var[j])
2900 var = var_from_index(tab, col_var[i]);
2902 for (j = 0; j < n_extra; ++j)
2903 if (!isl_int_is_zero(tab->mat->row[row][off+extra[j]]))
2905 isl_assert(tab->mat->ctx, j < n_extra, goto error);
2906 if (isl_tab_pivot(tab, row, extra[j]) < 0)
2908 extra[j] = extra[--n_extra];
2920 /* Remove all samples with index n or greater, i.e., those samples
2921 * that were added since we saved this number of samples in
2922 * isl_tab_save_samples.
2924 static void drop_samples_since(struct isl_tab *tab, int n)
2928 for (i = tab->n_sample - 1; i >= 0 && tab->n_sample > n; --i) {
2929 if (tab->sample_index[i] < n)
2932 if (i != tab->n_sample - 1) {
2933 int t = tab->sample_index[tab->n_sample-1];
2934 tab->sample_index[tab->n_sample-1] = tab->sample_index[i];
2935 tab->sample_index[i] = t;
2936 isl_mat_swap_rows(tab->samples, tab->n_sample-1, i);
2942 static int perform_undo(struct isl_tab *tab, struct isl_tab_undo *undo) WARN_UNUSED;
2943 static int perform_undo(struct isl_tab *tab, struct isl_tab_undo *undo)
2945 switch (undo->type) {
2946 case isl_tab_undo_empty:
2949 case isl_tab_undo_nonneg:
2950 case isl_tab_undo_redundant:
2951 case isl_tab_undo_freeze:
2952 case isl_tab_undo_zero:
2953 case isl_tab_undo_allocate:
2954 case isl_tab_undo_relax:
2955 return perform_undo_var(tab, undo);
2956 case isl_tab_undo_bmap_eq:
2957 return isl_basic_map_free_equality(tab->bmap, 1);
2958 case isl_tab_undo_bmap_ineq:
2959 return isl_basic_map_free_inequality(tab->bmap, 1);
2960 case isl_tab_undo_bmap_div:
2961 if (isl_basic_map_free_div(tab->bmap, 1) < 0)
2964 tab->samples->n_col--;
2966 case isl_tab_undo_saved_basis:
2967 if (restore_basis(tab, undo->u.col_var) < 0)
2970 case isl_tab_undo_drop_sample:
2973 case isl_tab_undo_saved_samples:
2974 drop_samples_since(tab, undo->u.n);
2976 case isl_tab_undo_callback:
2977 return undo->u.callback->run(undo->u.callback);
2979 isl_assert(tab->mat->ctx, 0, return -1);
2984 /* Return the tableau to the state it was in when the snapshot "snap"
2987 int isl_tab_rollback(struct isl_tab *tab, struct isl_tab_undo *snap)
2989 struct isl_tab_undo *undo, *next;
2995 for (undo = tab->top; undo && undo != &tab->bottom; undo = next) {
2999 if (perform_undo(tab, undo) < 0) {
3013 /* The given row "row" represents an inequality violated by all
3014 * points in the tableau. Check for some special cases of such
3015 * separating constraints.
3016 * In particular, if the row has been reduced to the constant -1,
3017 * then we know the inequality is adjacent (but opposite) to
3018 * an equality in the tableau.
3019 * If the row has been reduced to r = -1 -r', with r' an inequality
3020 * of the tableau, then the inequality is adjacent (but opposite)
3021 * to the inequality r'.
3023 static enum isl_ineq_type separation_type(struct isl_tab *tab, unsigned row)
3026 unsigned off = 2 + tab->M;
3029 return isl_ineq_separate;
3031 if (!isl_int_is_one(tab->mat->row[row][0]))
3032 return isl_ineq_separate;
3033 if (!isl_int_is_negone(tab->mat->row[row][1]))
3034 return isl_ineq_separate;
3036 pos = isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
3037 tab->n_col - tab->n_dead);
3039 return isl_ineq_adj_eq;
3041 if (!isl_int_is_negone(tab->mat->row[row][off + tab->n_dead + pos]))
3042 return isl_ineq_separate;
3044 pos = isl_seq_first_non_zero(
3045 tab->mat->row[row] + off + tab->n_dead + pos + 1,
3046 tab->n_col - tab->n_dead - pos - 1);
3048 return pos == -1 ? isl_ineq_adj_ineq : isl_ineq_separate;
3051 /* Check the effect of inequality "ineq" on the tableau "tab".
3053 * isl_ineq_redundant: satisfied by all points in the tableau
3054 * isl_ineq_separate: satisfied by no point in the tableau
3055 * isl_ineq_cut: satisfied by some by not all points
3056 * isl_ineq_adj_eq: adjacent to an equality
3057 * isl_ineq_adj_ineq: adjacent to an inequality.
3059 enum isl_ineq_type isl_tab_ineq_type(struct isl_tab *tab, isl_int *ineq)
3061 enum isl_ineq_type type = isl_ineq_error;
3062 struct isl_tab_undo *snap = NULL;
3067 return isl_ineq_error;
3069 if (isl_tab_extend_cons(tab, 1) < 0)
3070 return isl_ineq_error;
3072 snap = isl_tab_snap(tab);
3074 con = isl_tab_add_row(tab, ineq);
3078 row = tab->con[con].index;
3079 if (isl_tab_row_is_redundant(tab, row))
3080 type = isl_ineq_redundant;
3081 else if (isl_int_is_neg(tab->mat->row[row][1]) &&
3083 isl_int_abs_ge(tab->mat->row[row][1],
3084 tab->mat->row[row][0]))) {
3085 int nonneg = at_least_zero(tab, &tab->con[con]);
3089 type = isl_ineq_cut;
3091 type = separation_type(tab, row);
3093 int red = con_is_redundant(tab, &tab->con[con]);
3097 type = isl_ineq_cut;
3099 type = isl_ineq_redundant;
3102 if (isl_tab_rollback(tab, snap))
3103 return isl_ineq_error;
3106 return isl_ineq_error;
3109 int isl_tab_track_bmap(struct isl_tab *tab, __isl_take isl_basic_map *bmap)
3114 isl_assert(tab->mat->ctx, tab->n_eq == bmap->n_eq, return -1);
3115 isl_assert(tab->mat->ctx,
3116 tab->n_con == bmap->n_eq + bmap->n_ineq, return -1);
3122 isl_basic_map_free(bmap);
3126 int isl_tab_track_bset(struct isl_tab *tab, __isl_take isl_basic_set *bset)
3128 return isl_tab_track_bmap(tab, (isl_basic_map *)bset);
3131 __isl_keep isl_basic_set *isl_tab_peek_bset(struct isl_tab *tab)
3136 return (isl_basic_set *)tab->bmap;
3139 void isl_tab_dump(struct isl_tab *tab, FILE *out, int indent)
3145 fprintf(out, "%*snull tab\n", indent, "");
3148 fprintf(out, "%*sn_redundant: %d, n_dead: %d", indent, "",
3149 tab->n_redundant, tab->n_dead);
3151 fprintf(out, ", rational");
3153 fprintf(out, ", empty");
3155 fprintf(out, "%*s[", indent, "");
3156 for (i = 0; i < tab->n_var; ++i) {
3158 fprintf(out, (i == tab->n_param ||
3159 i == tab->n_var - tab->n_div) ? "; "
3161 fprintf(out, "%c%d%s", tab->var[i].is_row ? 'r' : 'c',
3163 tab->var[i].is_zero ? " [=0]" :
3164 tab->var[i].is_redundant ? " [R]" : "");
3166 fprintf(out, "]\n");
3167 fprintf(out, "%*s[", indent, "");
3168 for (i = 0; i < tab->n_con; ++i) {
3171 fprintf(out, "%c%d%s", tab->con[i].is_row ? 'r' : 'c',
3173 tab->con[i].is_zero ? " [=0]" :
3174 tab->con[i].is_redundant ? " [R]" : "");
3176 fprintf(out, "]\n");
3177 fprintf(out, "%*s[", indent, "");
3178 for (i = 0; i < tab->n_row; ++i) {
3179 const char *sign = "";
3182 if (tab->row_sign) {
3183 if (tab->row_sign[i] == isl_tab_row_unknown)
3185 else if (tab->row_sign[i] == isl_tab_row_neg)
3187 else if (tab->row_sign[i] == isl_tab_row_pos)
3192 fprintf(out, "r%d: %d%s%s", i, tab->row_var[i],
3193 isl_tab_var_from_row(tab, i)->is_nonneg ? " [>=0]" : "", sign);
3195 fprintf(out, "]\n");
3196 fprintf(out, "%*s[", indent, "");
3197 for (i = 0; i < tab->n_col; ++i) {
3200 fprintf(out, "c%d: %d%s", i, tab->col_var[i],
3201 var_from_col(tab, i)->is_nonneg ? " [>=0]" : "");
3203 fprintf(out, "]\n");
3204 r = tab->mat->n_row;
3205 tab->mat->n_row = tab->n_row;
3206 c = tab->mat->n_col;
3207 tab->mat->n_col = 2 + tab->M + tab->n_col;
3208 isl_mat_dump(tab->mat, out, indent);
3209 tab->mat->n_row = r;
3210 tab->mat->n_col = c;
3212 isl_basic_map_dump(tab->bmap, out, indent);