isl_tab: optionally save dual solution
[isl.git] / isl_tab.c
blobac9310a756884ea6b5b56a70355278e82111167e
1 #include "isl_map_private.h"
2 #include "isl_tab.h"
4 /*
5 * The implementation of tableaus in this file was inspired by Section 8
6 * of David Detlefs, Greg Nelson and James B. Saxe, "Simplify: a theorem
7 * prover for program checking".
8 */
10 struct isl_tab *isl_tab_alloc(struct isl_ctx *ctx,
11 unsigned n_row, unsigned n_var)
13 int i;
14 struct isl_tab *tab;
16 tab = isl_calloc_type(ctx, struct isl_tab);
17 if (!tab)
18 return NULL;
19 tab->mat = isl_mat_alloc(ctx, n_row, 2 + n_var);
20 if (!tab->mat)
21 goto error;
22 tab->var = isl_alloc_array(ctx, struct isl_tab_var, n_var);
23 if (!tab->var)
24 goto error;
25 tab->con = isl_alloc_array(ctx, struct isl_tab_var, n_row);
26 if (!tab->con)
27 goto error;
28 tab->col_var = isl_alloc_array(ctx, int, n_var);
29 if (!tab->col_var)
30 goto error;
31 tab->row_var = isl_alloc_array(ctx, int, n_row);
32 if (!tab->row_var)
33 goto error;
34 for (i = 0; i < n_var; ++i) {
35 tab->var[i].index = i;
36 tab->var[i].is_row = 0;
37 tab->var[i].is_nonneg = 0;
38 tab->var[i].is_zero = 0;
39 tab->var[i].is_redundant = 0;
40 tab->var[i].frozen = 0;
41 tab->col_var[i] = i;
43 tab->n_row = 0;
44 tab->n_con = 0;
45 tab->n_eq = 0;
46 tab->max_con = n_row;
47 tab->n_col = n_var;
48 tab->n_var = n_var;
49 tab->n_dead = 0;
50 tab->n_redundant = 0;
51 tab->need_undo = 0;
52 tab->rational = 0;
53 tab->empty = 0;
54 tab->bottom.type = isl_tab_undo_bottom;
55 tab->bottom.next = NULL;
56 tab->top = &tab->bottom;
57 return tab;
58 error:
59 isl_tab_free(ctx, tab);
60 return NULL;
63 static int extend_cons(struct isl_ctx *ctx, struct isl_tab *tab, unsigned n_new)
65 if (tab->max_con < tab->n_con + n_new) {
66 struct isl_tab_var *con;
68 con = isl_realloc_array(ctx, tab->con,
69 struct isl_tab_var, tab->max_con + n_new);
70 if (!con)
71 return -1;
72 tab->con = con;
73 tab->max_con += n_new;
75 if (tab->mat->n_row < tab->n_row + n_new) {
76 int *row_var;
78 tab->mat = isl_mat_extend(ctx, tab->mat,
79 tab->n_row + n_new, tab->n_col);
80 if (!tab->mat)
81 return -1;
82 row_var = isl_realloc_array(ctx, tab->row_var,
83 int, tab->mat->n_row);
84 if (!row_var)
85 return -1;
86 tab->row_var = row_var;
88 return 0;
91 struct isl_tab *isl_tab_extend(struct isl_ctx *ctx, struct isl_tab *tab,
92 unsigned n_new)
94 if (extend_cons(ctx, tab, n_new) >= 0)
95 return tab;
97 isl_tab_free(ctx, tab);
98 return NULL;
101 static void free_undo(struct isl_ctx *ctx, struct isl_tab *tab)
103 struct isl_tab_undo *undo, *next;
105 for (undo = tab->top; undo && undo != &tab->bottom; undo = next) {
106 next = undo->next;
107 free(undo);
109 tab->top = undo;
112 void isl_tab_free(struct isl_ctx *ctx, struct isl_tab *tab)
114 if (!tab)
115 return;
116 free_undo(ctx, tab);
117 isl_mat_free(ctx, tab->mat);
118 isl_vec_free(tab->dual);
119 free(tab->var);
120 free(tab->con);
121 free(tab->row_var);
122 free(tab->col_var);
123 free(tab);
126 static struct isl_tab_var *var_from_index(struct isl_ctx *ctx,
127 struct isl_tab *tab, int i)
129 if (i >= 0)
130 return &tab->var[i];
131 else
132 return &tab->con[~i];
135 static struct isl_tab_var *var_from_row(struct isl_ctx *ctx,
136 struct isl_tab *tab, int i)
138 return var_from_index(ctx, tab, tab->row_var[i]);
141 static struct isl_tab_var *var_from_col(struct isl_ctx *ctx,
142 struct isl_tab *tab, int i)
144 return var_from_index(ctx, tab, tab->col_var[i]);
147 /* Check if there are any upper bounds on column variable "var",
148 * i.e., non-negative rows where var appears with a negative coefficient.
149 * Return 1 if there are no such bounds.
151 static int max_is_manifestly_unbounded(struct isl_ctx *ctx,
152 struct isl_tab *tab, struct isl_tab_var *var)
154 int i;
156 if (var->is_row)
157 return 0;
158 for (i = tab->n_redundant; i < tab->n_row; ++i) {
159 if (!isl_int_is_neg(tab->mat->row[i][2 + var->index]))
160 continue;
161 if (var_from_row(ctx, tab, i)->is_nonneg)
162 return 0;
164 return 1;
167 /* Check if there are any lower bounds on column variable "var",
168 * i.e., non-negative rows where var appears with a positive coefficient.
169 * Return 1 if there are no such bounds.
171 static int min_is_manifestly_unbounded(struct isl_ctx *ctx,
172 struct isl_tab *tab, struct isl_tab_var *var)
174 int i;
176 if (var->is_row)
177 return 0;
178 for (i = tab->n_redundant; i < tab->n_row; ++i) {
179 if (!isl_int_is_pos(tab->mat->row[i][2 + var->index]))
180 continue;
181 if (var_from_row(ctx, tab, i)->is_nonneg)
182 return 0;
184 return 1;
187 /* Given the index of a column "c", return the index of a row
188 * that can be used to pivot the column in, with either an increase
189 * (sgn > 0) or a decrease (sgn < 0) of the corresponding variable.
190 * If "var" is not NULL, then the row returned will be different from
191 * the one associated with "var".
193 * Each row in the tableau is of the form
195 * x_r = a_r0 + \sum_i a_ri x_i
197 * Only rows with x_r >= 0 and with the sign of a_ri opposite to "sgn"
198 * impose any limit on the increase or decrease in the value of x_c
199 * and this bound is equal to a_r0 / |a_rc|. We are therefore looking
200 * for the row with the smallest (most stringent) such bound.
201 * Note that the common denominator of each row drops out of the fraction.
202 * To check if row j has a smaller bound than row r, i.e.,
203 * a_j0 / |a_jc| < a_r0 / |a_rc| or a_j0 |a_rc| < a_r0 |a_jc|,
204 * we check if -sign(a_jc) (a_j0 a_rc - a_r0 a_jc) < 0,
205 * where -sign(a_jc) is equal to "sgn".
207 static int pivot_row(struct isl_ctx *ctx, struct isl_tab *tab,
208 struct isl_tab_var *var, int sgn, int c)
210 int j, r, tsgn;
211 isl_int t;
213 isl_int_init(t);
214 r = -1;
215 for (j = tab->n_redundant; j < tab->n_row; ++j) {
216 if (var && j == var->index)
217 continue;
218 if (!var_from_row(ctx, tab, j)->is_nonneg)
219 continue;
220 if (sgn * isl_int_sgn(tab->mat->row[j][2 + c]) >= 0)
221 continue;
222 if (r < 0) {
223 r = j;
224 continue;
226 isl_int_mul(t, tab->mat->row[r][1], tab->mat->row[j][2 + c]);
227 isl_int_submul(t, tab->mat->row[j][1], tab->mat->row[r][2 + c]);
228 tsgn = sgn * isl_int_sgn(t);
229 if (tsgn < 0 || (tsgn == 0 &&
230 tab->row_var[j] < tab->row_var[r]))
231 r = j;
233 isl_int_clear(t);
234 return r;
237 /* Find a pivot (row and col) that will increase (sgn > 0) or decrease
238 * (sgn < 0) the value of row variable var.
239 * If not NULL, then skip_var is a row variable that should be ignored
240 * while looking for a pivot row. It is usually equal to var.
242 * As the given row in the tableau is of the form
244 * x_r = a_r0 + \sum_i a_ri x_i
246 * we need to find a column such that the sign of a_ri is equal to "sgn"
247 * (such that an increase in x_i will have the desired effect) or a
248 * column with a variable that may attain negative values.
249 * If a_ri is positive, then we need to move x_i in the same direction
250 * to obtain the desired effect. Otherwise, x_i has to move in the
251 * opposite direction.
253 static void find_pivot(struct isl_ctx *ctx, struct isl_tab *tab,
254 struct isl_tab_var *var, struct isl_tab_var *skip_var,
255 int sgn, int *row, int *col)
257 int j, r, c;
258 isl_int *tr;
260 *row = *col = -1;
262 isl_assert(ctx, var->is_row, return);
263 tr = tab->mat->row[var->index];
265 c = -1;
266 for (j = tab->n_dead; j < tab->n_col; ++j) {
267 if (isl_int_is_zero(tr[2 + j]))
268 continue;
269 if (isl_int_sgn(tr[2 + j]) != sgn &&
270 var_from_col(ctx, tab, j)->is_nonneg)
271 continue;
272 if (c < 0 || tab->col_var[j] < tab->col_var[c])
273 c = j;
275 if (c < 0)
276 return;
278 sgn *= isl_int_sgn(tr[2 + c]);
279 r = pivot_row(ctx, tab, skip_var, sgn, c);
280 *row = r < 0 ? var->index : r;
281 *col = c;
284 /* Return 1 if row "row" represents an obviously redundant inequality.
285 * This means
286 * - it represents an inequality or a variable
287 * - that is the sum of a non-negative sample value and a positive
288 * combination of zero or more non-negative variables.
290 static int is_redundant(struct isl_ctx *ctx, struct isl_tab *tab, int row)
292 int i;
294 if (tab->row_var[row] < 0 && !var_from_row(ctx, tab, row)->is_nonneg)
295 return 0;
297 if (isl_int_is_neg(tab->mat->row[row][1]))
298 return 0;
300 for (i = tab->n_dead; i < tab->n_col; ++i) {
301 if (isl_int_is_zero(tab->mat->row[row][2 + i]))
302 continue;
303 if (isl_int_is_neg(tab->mat->row[row][2 + i]))
304 return 0;
305 if (!var_from_col(ctx, tab, i)->is_nonneg)
306 return 0;
308 return 1;
311 static void swap_rows(struct isl_ctx *ctx,
312 struct isl_tab *tab, int row1, int row2)
314 int t;
315 t = tab->row_var[row1];
316 tab->row_var[row1] = tab->row_var[row2];
317 tab->row_var[row2] = t;
318 var_from_row(ctx, tab, row1)->index = row1;
319 var_from_row(ctx, tab, row2)->index = row2;
320 tab->mat = isl_mat_swap_rows(ctx, tab->mat, row1, row2);
323 static void push(struct isl_ctx *ctx, struct isl_tab *tab,
324 enum isl_tab_undo_type type, struct isl_tab_var *var)
326 struct isl_tab_undo *undo;
328 if (!tab->need_undo)
329 return;
331 undo = isl_alloc_type(ctx, struct isl_tab_undo);
332 if (!undo) {
333 free_undo(ctx, tab);
334 tab->top = NULL;
335 return;
337 undo->type = type;
338 undo->var = var;
339 undo->next = tab->top;
340 tab->top = undo;
343 /* Mark row with index "row" as being redundant.
344 * If we may need to undo the operation or if the row represents
345 * a variable of the original problem, the row is kept,
346 * but no longer considered when looking for a pivot row.
347 * Otherwise, the row is simply removed.
349 * The row may be interchanged with some other row. If it
350 * is interchanged with a later row, return 1. Otherwise return 0.
351 * If the rows are checked in order in the calling function,
352 * then a return value of 1 means that the row with the given
353 * row number may now contain a different row that hasn't been checked yet.
355 static int mark_redundant(struct isl_ctx *ctx,
356 struct isl_tab *tab, int row)
358 struct isl_tab_var *var = var_from_row(ctx, tab, row);
359 var->is_redundant = 1;
360 isl_assert(ctx, row >= tab->n_redundant, return);
361 if (tab->need_undo || tab->row_var[row] >= 0) {
362 if (tab->row_var[row] >= 0) {
363 var->is_nonneg = 1;
364 push(ctx, tab, isl_tab_undo_nonneg, var);
366 if (row != tab->n_redundant)
367 swap_rows(ctx, tab, row, tab->n_redundant);
368 push(ctx, tab, isl_tab_undo_redundant, var);
369 tab->n_redundant++;
370 return 0;
371 } else {
372 if (row != tab->n_row - 1)
373 swap_rows(ctx, tab, row, tab->n_row - 1);
374 var_from_row(ctx, tab, tab->n_row - 1)->index = -1;
375 tab->n_row--;
376 return 1;
380 static void mark_empty(struct isl_ctx *ctx, struct isl_tab *tab)
382 if (!tab->empty && tab->need_undo)
383 push(ctx, tab, isl_tab_undo_empty, NULL);
384 tab->empty = 1;
387 /* Given a row number "row" and a column number "col", pivot the tableau
388 * such that the associated variable are interchanged.
389 * The given row in the tableau expresses
391 * x_r = a_r0 + \sum_i a_ri x_i
393 * or
395 * x_c = 1/a_rc x_r - a_r0/a_rc + sum_{i \ne r} -a_ri/a_rc
397 * Substituting this equality into the other rows
399 * x_j = a_j0 + \sum_i a_ji x_i
401 * with a_jc \ne 0, we obtain
403 * 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
405 * The tableau
407 * n_rc/d_r n_ri/d_r
408 * n_jc/d_j n_ji/d_j
410 * where i is any other column and j is any other row,
411 * is therefore transformed into
413 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
414 * 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)
416 * The transformation is performed along the following steps
418 * d_r/n_rc n_ri/n_rc
419 * n_jc/d_j n_ji/d_j
421 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
422 * n_jc/d_j n_ji/d_j
424 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
425 * n_jc/(|n_rc| d_j) n_ji/(|n_rc| d_j)
427 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
428 * n_jc/(|n_rc| d_j) (n_ji |n_rc|)/(|n_rc| d_j)
430 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
431 * n_jc/(|n_rc| d_j) (n_ji |n_rc| - s(n_rc)n_jc n_ri)/(|n_rc| d_j)
433 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
434 * 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)
437 static void pivot(struct isl_ctx *ctx,
438 struct isl_tab *tab, int row, int col)
440 int i, j;
441 int sgn;
442 int t;
443 struct isl_mat *mat = tab->mat;
444 struct isl_tab_var *var;
446 isl_int_swap(mat->row[row][0], mat->row[row][2 + col]);
447 sgn = isl_int_sgn(mat->row[row][0]);
448 if (sgn < 0) {
449 isl_int_neg(mat->row[row][0], mat->row[row][0]);
450 isl_int_neg(mat->row[row][2 + col], mat->row[row][2 + col]);
451 } else
452 for (j = 0; j < 1 + tab->n_col; ++j) {
453 if (j == 1 + col)
454 continue;
455 isl_int_neg(mat->row[row][1 + j], mat->row[row][1 + j]);
457 if (!isl_int_is_one(mat->row[row][0]))
458 isl_seq_normalize(mat->row[row], 2 + tab->n_col);
459 for (i = 0; i < tab->n_row; ++i) {
460 if (i == row)
461 continue;
462 if (isl_int_is_zero(mat->row[i][2 + col]))
463 continue;
464 isl_int_mul(mat->row[i][0], mat->row[i][0], mat->row[row][0]);
465 for (j = 0; j < 1 + tab->n_col; ++j) {
466 if (j == 1 + col)
467 continue;
468 isl_int_mul(mat->row[i][1 + j],
469 mat->row[i][1 + j], mat->row[row][0]);
470 isl_int_addmul(mat->row[i][1 + j],
471 mat->row[i][2 + col], mat->row[row][1 + j]);
473 isl_int_mul(mat->row[i][2 + col],
474 mat->row[i][2 + col], mat->row[row][2 + col]);
475 if (!isl_int_is_one(mat->row[row][0]))
476 isl_seq_normalize(mat->row[i], 2 + tab->n_col);
478 t = tab->row_var[row];
479 tab->row_var[row] = tab->col_var[col];
480 tab->col_var[col] = t;
481 var = var_from_row(ctx, tab, row);
482 var->is_row = 1;
483 var->index = row;
484 var = var_from_col(ctx, tab, col);
485 var->is_row = 0;
486 var->index = col;
487 for (i = tab->n_redundant; i < tab->n_row; ++i) {
488 if (isl_int_is_zero(mat->row[i][2 + col]))
489 continue;
490 if (!var_from_row(ctx, tab, i)->frozen &&
491 is_redundant(ctx, tab, i))
492 if (mark_redundant(ctx, tab, i))
493 --i;
497 /* If "var" represents a column variable, then pivot is up (sgn > 0)
498 * or down (sgn < 0) to a row. The variable is assumed not to be
499 * unbounded in the specified direction.
501 static void to_row(struct isl_ctx *ctx,
502 struct isl_tab *tab, struct isl_tab_var *var, int sign)
504 int r;
506 if (var->is_row)
507 return;
509 r = pivot_row(ctx, tab, NULL, sign, var->index);
510 isl_assert(ctx, r >= 0, return);
511 pivot(ctx, tab, r, var->index);
514 static void check_table(struct isl_ctx *ctx, struct isl_tab *tab)
516 int i;
518 if (tab->empty)
519 return;
520 for (i = 0; i < tab->n_row; ++i) {
521 if (!var_from_row(ctx, tab, i)->is_nonneg)
522 continue;
523 assert(!isl_int_is_neg(tab->mat->row[i][1]));
527 /* Return the sign of the maximal value of "var".
528 * If the sign is not negative, then on return from this function,
529 * the sample value will also be non-negative.
531 * If "var" is manifestly unbounded wrt positive values, we are done.
532 * Otherwise, we pivot the variable up to a row if needed
533 * Then we continue pivoting down until either
534 * - no more down pivots can be performed
535 * - the sample value is positive
536 * - the variable is pivoted into a manifestly unbounded column
538 static int sign_of_max(struct isl_ctx *ctx,
539 struct isl_tab *tab, struct isl_tab_var *var)
541 int row, col;
543 if (max_is_manifestly_unbounded(ctx, tab, var))
544 return 1;
545 to_row(ctx, tab, var, 1);
546 while (!isl_int_is_pos(tab->mat->row[var->index][1])) {
547 find_pivot(ctx, tab, var, var, 1, &row, &col);
548 if (row == -1)
549 return isl_int_sgn(tab->mat->row[var->index][1]);
550 pivot(ctx, tab, row, col);
551 if (!var->is_row) /* manifestly unbounded */
552 return 1;
554 return 1;
557 /* Perform pivots until the row variable "var" has a non-negative
558 * sample value or until no more upward pivots can be performed.
559 * Return the sign of the sample value after the pivots have been
560 * performed.
562 static int restore_row(struct isl_ctx *ctx,
563 struct isl_tab *tab, struct isl_tab_var *var)
565 int row, col;
567 while (isl_int_is_neg(tab->mat->row[var->index][1])) {
568 find_pivot(ctx, tab, var, var, 1, &row, &col);
569 if (row == -1)
570 break;
571 pivot(ctx, tab, row, col);
572 if (!var->is_row) /* manifestly unbounded */
573 return 1;
575 return isl_int_sgn(tab->mat->row[var->index][1]);
578 /* Perform pivots until we are sure that the row variable "var"
579 * can attain non-negative values. After return from this
580 * function, "var" is still a row variable, but its sample
581 * value may not be non-negative, even if the function returns 1.
583 static int at_least_zero(struct isl_ctx *ctx,
584 struct isl_tab *tab, struct isl_tab_var *var)
586 int row, col;
588 while (isl_int_is_neg(tab->mat->row[var->index][1])) {
589 find_pivot(ctx, tab, var, var, 1, &row, &col);
590 if (row == -1)
591 break;
592 if (row == var->index) /* manifestly unbounded */
593 return 1;
594 pivot(ctx, tab, row, col);
596 return !isl_int_is_neg(tab->mat->row[var->index][1]);
599 /* Return a negative value if "var" can attain negative values.
600 * Return a non-negative value otherwise.
602 * If "var" is manifestly unbounded wrt negative values, we are done.
603 * Otherwise, if var is in a column, we can pivot it down to a row.
604 * Then we continue pivoting down until either
605 * - the pivot would result in a manifestly unbounded column
606 * => we don't perform the pivot, but simply return -1
607 * - no more down pivots can be performed
608 * - the sample value is negative
609 * If the sample value becomes negative and the variable is supposed
610 * to be nonnegative, then we undo the last pivot.
611 * However, if the last pivot has made the pivoting variable
612 * obviously redundant, then it may have moved to another row.
613 * In that case we look for upward pivots until we reach a non-negative
614 * value again.
616 static int sign_of_min(struct isl_ctx *ctx,
617 struct isl_tab *tab, struct isl_tab_var *var)
619 int row, col;
620 struct isl_tab_var *pivot_var;
622 if (min_is_manifestly_unbounded(ctx, tab, var))
623 return -1;
624 if (!var->is_row) {
625 col = var->index;
626 row = pivot_row(ctx, tab, NULL, -1, col);
627 pivot_var = var_from_col(ctx, tab, col);
628 pivot(ctx, tab, row, col);
629 if (var->is_redundant)
630 return 0;
631 if (isl_int_is_neg(tab->mat->row[var->index][1])) {
632 if (var->is_nonneg) {
633 if (!pivot_var->is_redundant &&
634 pivot_var->index == row)
635 pivot(ctx, tab, row, col);
636 else
637 restore_row(ctx, tab, var);
639 return -1;
642 if (var->is_redundant)
643 return 0;
644 while (!isl_int_is_neg(tab->mat->row[var->index][1])) {
645 find_pivot(ctx, tab, var, var, -1, &row, &col);
646 if (row == var->index)
647 return -1;
648 if (row == -1)
649 return isl_int_sgn(tab->mat->row[var->index][1]);
650 pivot_var = var_from_col(ctx, tab, col);
651 pivot(ctx, tab, row, col);
652 if (var->is_redundant)
653 return 0;
655 if (var->is_nonneg) {
656 /* pivot back to non-negative value */
657 if (!pivot_var->is_redundant && pivot_var->index == row)
658 pivot(ctx, tab, row, col);
659 else
660 restore_row(ctx, tab, var);
662 return -1;
665 /* Return 1 if "var" can attain values <= -1.
666 * Return 0 otherwise.
668 * The sample value of "var" is assumed to be non-negative when the
669 * the function is called and will be made non-negative again before
670 * the function returns.
672 static int min_at_most_neg_one(struct isl_ctx *ctx,
673 struct isl_tab *tab, struct isl_tab_var *var)
675 int row, col;
676 struct isl_tab_var *pivot_var;
678 if (min_is_manifestly_unbounded(ctx, tab, var))
679 return 1;
680 if (!var->is_row) {
681 col = var->index;
682 row = pivot_row(ctx, tab, NULL, -1, col);
683 pivot_var = var_from_col(ctx, tab, col);
684 pivot(ctx, tab, row, col);
685 if (var->is_redundant)
686 return 0;
687 if (isl_int_is_neg(tab->mat->row[var->index][1]) &&
688 isl_int_abs_ge(tab->mat->row[var->index][1],
689 tab->mat->row[var->index][0])) {
690 if (var->is_nonneg) {
691 if (!pivot_var->is_redundant &&
692 pivot_var->index == row)
693 pivot(ctx, tab, row, col);
694 else
695 restore_row(ctx, tab, var);
697 return 1;
700 if (var->is_redundant)
701 return 0;
702 do {
703 find_pivot(ctx, tab, var, var, -1, &row, &col);
704 if (row == var->index)
705 return 1;
706 if (row == -1)
707 return 0;
708 pivot_var = var_from_col(ctx, tab, col);
709 pivot(ctx, tab, row, col);
710 if (var->is_redundant)
711 return 0;
712 } while (!isl_int_is_neg(tab->mat->row[var->index][1]) ||
713 isl_int_abs_lt(tab->mat->row[var->index][1],
714 tab->mat->row[var->index][0]));
715 if (var->is_nonneg) {
716 /* pivot back to non-negative value */
717 if (!pivot_var->is_redundant && pivot_var->index == row)
718 pivot(ctx, tab, row, col);
719 restore_row(ctx, tab, var);
721 return 1;
724 /* Return 1 if "var" can attain values >= 1.
725 * Return 0 otherwise.
727 static int at_least_one(struct isl_ctx *ctx,
728 struct isl_tab *tab, struct isl_tab_var *var)
730 int row, col;
731 isl_int *r;
733 if (max_is_manifestly_unbounded(ctx, tab, var))
734 return 1;
735 to_row(ctx, tab, var, 1);
736 r = tab->mat->row[var->index];
737 while (isl_int_lt(r[1], r[0])) {
738 find_pivot(ctx, tab, var, var, 1, &row, &col);
739 if (row == -1)
740 return isl_int_ge(r[1], r[0]);
741 if (row == var->index) /* manifestly unbounded */
742 return 1;
743 pivot(ctx, tab, row, col);
745 return 1;
748 static void swap_cols(struct isl_ctx *ctx,
749 struct isl_tab *tab, int col1, int col2)
751 int t;
752 t = tab->col_var[col1];
753 tab->col_var[col1] = tab->col_var[col2];
754 tab->col_var[col2] = t;
755 var_from_col(ctx, tab, col1)->index = col1;
756 var_from_col(ctx, tab, col2)->index = col2;
757 tab->mat = isl_mat_swap_cols(ctx, tab->mat, 2 + col1, 2 + col2);
760 /* Mark column with index "col" as representing a zero variable.
761 * If we may need to undo the operation the column is kept,
762 * but no longer considered.
763 * Otherwise, the column is simply removed.
765 * The column may be interchanged with some other column. If it
766 * is interchanged with a later column, return 1. Otherwise return 0.
767 * If the columns are checked in order in the calling function,
768 * then a return value of 1 means that the column with the given
769 * column number may now contain a different column that
770 * hasn't been checked yet.
772 static int kill_col(struct isl_ctx *ctx,
773 struct isl_tab *tab, int col)
775 var_from_col(ctx, tab, col)->is_zero = 1;
776 if (tab->need_undo) {
777 push(ctx, tab, isl_tab_undo_zero, var_from_col(ctx, tab, col));
778 if (col != tab->n_dead)
779 swap_cols(ctx, tab, col, tab->n_dead);
780 tab->n_dead++;
781 return 0;
782 } else {
783 if (col != tab->n_col - 1)
784 swap_cols(ctx, tab, col, tab->n_col - 1);
785 var_from_col(ctx, tab, tab->n_col - 1)->index = -1;
786 tab->n_col--;
787 return 1;
791 /* Row variable "var" is non-negative and cannot attain any values
792 * larger than zero. This means that the coefficients of the unrestricted
793 * column variables are zero and that the coefficients of the non-negative
794 * column variables are zero or negative.
795 * Each of the non-negative variables with a negative coefficient can
796 * then also be written as the negative sum of non-negative variables
797 * and must therefore also be zero.
799 static void close_row(struct isl_ctx *ctx,
800 struct isl_tab *tab, struct isl_tab_var *var)
802 int j;
803 struct isl_mat *mat = tab->mat;
805 isl_assert(ctx, var->is_nonneg, return);
806 var->is_zero = 1;
807 for (j = tab->n_dead; j < tab->n_col; ++j) {
808 if (isl_int_is_zero(mat->row[var->index][2 + j]))
809 continue;
810 isl_assert(ctx, isl_int_is_neg(mat->row[var->index][2 + j]),
811 return);
812 if (kill_col(ctx, tab, j))
813 --j;
815 mark_redundant(ctx, tab, var->index);
818 /* Add a row to the tableau. The row is given as an affine combination
819 * of the original variables and needs to be expressed in terms of the
820 * column variables.
822 * We add each term in turn.
823 * If r = n/d_r is the current sum and we need to add k x, then
824 * if x is a column variable, we increase the numerator of
825 * this column by k d_r
826 * if x = f/d_x is a row variable, then the new representation of r is
828 * n k f d_x/g n + d_r/g k f m/d_r n + m/d_g k f
829 * --- + --- = ------------------- = -------------------
830 * d_r d_r d_r d_x/g m
832 * with g the gcd of d_r and d_x and m the lcm of d_r and d_x.
834 static int add_row(struct isl_ctx *ctx, struct isl_tab *tab, isl_int *line)
836 int i;
837 unsigned r;
838 isl_int *row;
839 isl_int a, b;
841 isl_assert(ctx, tab->n_row < tab->mat->n_row, return -1);
843 isl_int_init(a);
844 isl_int_init(b);
845 r = tab->n_con;
846 tab->con[r].index = tab->n_row;
847 tab->con[r].is_row = 1;
848 tab->con[r].is_nonneg = 0;
849 tab->con[r].is_zero = 0;
850 tab->con[r].is_redundant = 0;
851 tab->con[r].frozen = 0;
852 tab->row_var[tab->n_row] = ~r;
853 row = tab->mat->row[tab->n_row];
854 isl_int_set_si(row[0], 1);
855 isl_int_set(row[1], line[0]);
856 isl_seq_clr(row + 2, tab->n_col);
857 for (i = 0; i < tab->n_var; ++i) {
858 if (tab->var[i].is_zero)
859 continue;
860 if (tab->var[i].is_row) {
861 isl_int_lcm(a,
862 row[0], tab->mat->row[tab->var[i].index][0]);
863 isl_int_swap(a, row[0]);
864 isl_int_divexact(a, row[0], a);
865 isl_int_divexact(b,
866 row[0], tab->mat->row[tab->var[i].index][0]);
867 isl_int_mul(b, b, line[1 + i]);
868 isl_seq_combine(row + 1, a, row + 1,
869 b, tab->mat->row[tab->var[i].index] + 1,
870 1 + tab->n_col);
871 } else
872 isl_int_addmul(row[2 + tab->var[i].index],
873 line[1 + i], row[0]);
875 isl_seq_normalize(row, 2 + tab->n_col);
876 tab->n_row++;
877 tab->n_con++;
878 push(ctx, tab, isl_tab_undo_allocate, &tab->con[r]);
879 isl_int_clear(a);
880 isl_int_clear(b);
882 return r;
885 static int drop_row(struct isl_ctx *ctx, struct isl_tab *tab, int row)
887 isl_assert(ctx, ~tab->row_var[row] == tab->n_con - 1, return -1);
888 if (row != tab->n_row - 1)
889 swap_rows(ctx, tab, row, tab->n_row - 1);
890 tab->n_row--;
891 tab->n_con--;
892 return 0;
895 /* Add inequality "ineq" and check if it conflicts with the
896 * previously added constraints or if it is obviously redundant.
898 struct isl_tab *isl_tab_add_ineq(struct isl_ctx *ctx,
899 struct isl_tab *tab, isl_int *ineq)
901 int r;
902 int sgn;
904 if (!tab)
905 return NULL;
906 r = add_row(ctx, tab, ineq);
907 if (r < 0)
908 goto error;
909 tab->con[r].is_nonneg = 1;
910 push(ctx, tab, isl_tab_undo_nonneg, &tab->con[r]);
911 if (is_redundant(ctx, tab, tab->con[r].index)) {
912 mark_redundant(ctx, tab, tab->con[r].index);
913 return tab;
916 sgn = restore_row(ctx, tab, &tab->con[r]);
917 if (sgn < 0)
918 mark_empty(ctx, tab);
919 else if (tab->con[r].is_row &&
920 is_redundant(ctx, tab, tab->con[r].index))
921 mark_redundant(ctx, tab, tab->con[r].index);
922 return tab;
923 error:
924 isl_tab_free(ctx, tab);
925 return NULL;
928 /* Pivot a non-negative variable down until it reaches the value zero
929 * and then pivot the variable into a column position.
931 static int to_col(struct isl_ctx *ctx,
932 struct isl_tab *tab, struct isl_tab_var *var)
934 int i;
935 int row, col;
937 if (!var->is_row)
938 return;
940 while (isl_int_is_pos(tab->mat->row[var->index][1])) {
941 find_pivot(ctx, tab, var, NULL, -1, &row, &col);
942 isl_assert(ctx, row != -1, return -1);
943 pivot(ctx, tab, row, col);
944 if (!var->is_row)
945 return;
948 for (i = tab->n_dead; i < tab->n_col; ++i)
949 if (!isl_int_is_zero(tab->mat->row[var->index][2 + i]))
950 break;
952 isl_assert(ctx, i < tab->n_col, return -1);
953 pivot(ctx, tab, var->index, i);
955 return 0;
958 /* We assume Gaussian elimination has been performed on the equalities.
959 * The equalities can therefore never conflict.
960 * Adding the equalities is currently only really useful for a later call
961 * to isl_tab_ineq_type.
963 static struct isl_tab *add_eq(struct isl_ctx *ctx,
964 struct isl_tab *tab, isl_int *eq)
966 int i;
967 int r;
969 if (!tab)
970 return NULL;
971 r = add_row(ctx, tab, eq);
972 if (r < 0)
973 goto error;
975 r = tab->con[r].index;
976 for (i = tab->n_dead; i < tab->n_col; ++i) {
977 if (isl_int_is_zero(tab->mat->row[r][2 + i]))
978 continue;
979 pivot(ctx, tab, r, i);
980 kill_col(ctx, tab, i);
981 break;
983 tab->n_eq++;
985 return tab;
986 error:
987 isl_tab_free(ctx, tab);
988 return NULL;
991 /* Add an equality that is known to be valid for the given tableau.
993 struct isl_tab *isl_tab_add_valid_eq(struct isl_ctx *ctx,
994 struct isl_tab *tab, isl_int *eq)
996 struct isl_tab_var *var;
997 int i;
998 int r;
1000 if (!tab)
1001 return NULL;
1002 r = add_row(ctx, tab, eq);
1003 if (r < 0)
1004 goto error;
1006 var = &tab->con[r];
1007 r = var->index;
1008 if (isl_int_is_neg(tab->mat->row[r][1]))
1009 isl_seq_neg(tab->mat->row[r] + 1, tab->mat->row[r] + 1,
1010 1 + tab->n_col);
1011 var->is_nonneg = 1;
1012 if (to_col(ctx, tab, var) < 0)
1013 goto error;
1014 var->is_nonneg = 0;
1015 kill_col(ctx, tab, var->index);
1017 return tab;
1018 error:
1019 isl_tab_free(ctx, tab);
1020 return NULL;
1023 struct isl_tab *isl_tab_from_basic_map(struct isl_basic_map *bmap)
1025 int i;
1026 struct isl_tab *tab;
1028 if (!bmap)
1029 return NULL;
1030 tab = isl_tab_alloc(bmap->ctx,
1031 isl_basic_map_total_dim(bmap) + bmap->n_ineq + 1,
1032 isl_basic_map_total_dim(bmap));
1033 if (!tab)
1034 return NULL;
1035 tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
1036 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) {
1037 mark_empty(bmap->ctx, tab);
1038 return tab;
1040 for (i = 0; i < bmap->n_eq; ++i) {
1041 tab = add_eq(bmap->ctx, tab, bmap->eq[i]);
1042 if (!tab)
1043 return tab;
1045 for (i = 0; i < bmap->n_ineq; ++i) {
1046 tab = isl_tab_add_ineq(bmap->ctx, tab, bmap->ineq[i]);
1047 if (!tab || tab->empty)
1048 return tab;
1050 return tab;
1053 struct isl_tab *isl_tab_from_basic_set(struct isl_basic_set *bset)
1055 return isl_tab_from_basic_map((struct isl_basic_map *)bset);
1058 /* Construct a tableau corresponding to the recession cone of "bmap".
1060 struct isl_tab *isl_tab_from_recession_cone(struct isl_basic_map *bmap)
1062 isl_int cst;
1063 int i;
1064 struct isl_tab *tab;
1066 if (!bmap)
1067 return NULL;
1068 tab = isl_tab_alloc(bmap->ctx, bmap->n_eq + bmap->n_ineq,
1069 isl_basic_map_total_dim(bmap));
1070 if (!tab)
1071 return NULL;
1072 tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
1074 isl_int_init(cst);
1075 for (i = 0; i < bmap->n_eq; ++i) {
1076 isl_int_swap(bmap->eq[i][0], cst);
1077 tab = add_eq(bmap->ctx, tab, bmap->eq[i]);
1078 isl_int_swap(bmap->eq[i][0], cst);
1079 if (!tab)
1080 goto done;
1082 for (i = 0; i < bmap->n_ineq; ++i) {
1083 int r;
1084 isl_int_swap(bmap->ineq[i][0], cst);
1085 r = add_row(bmap->ctx, tab, bmap->ineq[i]);
1086 isl_int_swap(bmap->ineq[i][0], cst);
1087 if (r < 0)
1088 goto error;
1089 tab->con[r].is_nonneg = 1;
1090 push(bmap->ctx, tab, isl_tab_undo_nonneg, &tab->con[r]);
1092 done:
1093 isl_int_clear(cst);
1094 return tab;
1095 error:
1096 isl_int_clear(cst);
1097 isl_tab_free(bmap->ctx, tab);
1098 return NULL;
1101 /* Assuming "tab" is the tableau of a cone, check if the cone is
1102 * bounded, i.e., if it is empty or only contains the origin.
1104 int isl_tab_cone_is_bounded(struct isl_ctx *ctx, struct isl_tab *tab)
1106 int i;
1108 if (!tab)
1109 return -1;
1110 if (tab->empty)
1111 return 1;
1112 if (tab->n_dead == tab->n_col)
1113 return 1;
1115 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1116 struct isl_tab_var *var;
1117 var = var_from_row(ctx, tab, i);
1118 if (!var->is_nonneg)
1119 continue;
1120 if (sign_of_max(ctx, tab, var) == 0)
1121 close_row(ctx, tab, var);
1122 else
1123 return 0;
1124 if (tab->n_dead == tab->n_col)
1125 return 1;
1127 return 0;
1130 static int sample_is_integer(struct isl_ctx *ctx, struct isl_tab *tab)
1132 int i;
1134 for (i = 0; i < tab->n_var; ++i) {
1135 int row;
1136 if (!tab->var[i].is_row)
1137 continue;
1138 row = tab->var[i].index;
1139 if (!isl_int_is_divisible_by(tab->mat->row[row][1],
1140 tab->mat->row[row][0]))
1141 return 0;
1143 return 1;
1146 static struct isl_vec *extract_integer_sample(struct isl_ctx *ctx,
1147 struct isl_tab *tab)
1149 int i;
1150 struct isl_vec *vec;
1152 vec = isl_vec_alloc(ctx, 1 + tab->n_var);
1153 if (!vec)
1154 return NULL;
1156 isl_int_set_si(vec->block.data[0], 1);
1157 for (i = 0; i < tab->n_var; ++i) {
1158 if (!tab->var[i].is_row)
1159 isl_int_set_si(vec->block.data[1 + i], 0);
1160 else {
1161 int row = tab->var[i].index;
1162 isl_int_divexact(vec->block.data[1 + i],
1163 tab->mat->row[row][1], tab->mat->row[row][0]);
1167 return vec;
1170 struct isl_vec *isl_tab_get_sample_value(struct isl_ctx *ctx,
1171 struct isl_tab *tab)
1173 int i;
1174 struct isl_vec *vec;
1175 isl_int m;
1177 if (!tab)
1178 return NULL;
1180 vec = isl_vec_alloc(ctx, 1 + tab->n_var);
1181 if (!vec)
1182 return NULL;
1184 isl_int_init(m);
1186 isl_int_set_si(vec->block.data[0], 1);
1187 for (i = 0; i < tab->n_var; ++i) {
1188 int row;
1189 if (!tab->var[i].is_row) {
1190 isl_int_set_si(vec->block.data[1 + i], 0);
1191 continue;
1193 row = tab->var[i].index;
1194 isl_int_gcd(m, vec->block.data[0], tab->mat->row[row][0]);
1195 isl_int_divexact(m, tab->mat->row[row][0], m);
1196 isl_seq_scale(vec->block.data, vec->block.data, m, 1 + i);
1197 isl_int_divexact(m, vec->block.data[0], tab->mat->row[row][0]);
1198 isl_int_mul(vec->block.data[1 + i], m, tab->mat->row[row][1]);
1200 isl_seq_normalize(vec->block.data, vec->size);
1202 isl_int_clear(m);
1203 return vec;
1206 /* Update "bmap" based on the results of the tableau "tab".
1207 * In particular, implicit equalities are made explicit, redundant constraints
1208 * are removed and if the sample value happens to be integer, it is stored
1209 * in "bmap" (unless "bmap" already had an integer sample).
1211 * The tableau is assumed to have been created from "bmap" using
1212 * isl_tab_from_basic_map.
1214 struct isl_basic_map *isl_basic_map_update_from_tab(struct isl_basic_map *bmap,
1215 struct isl_tab *tab)
1217 int i;
1218 unsigned n_eq;
1220 if (!bmap)
1221 return NULL;
1222 if (!tab)
1223 return bmap;
1225 n_eq = tab->n_eq;
1226 if (tab->empty)
1227 bmap = isl_basic_map_set_to_empty(bmap);
1228 else
1229 for (i = bmap->n_ineq - 1; i >= 0; --i) {
1230 if (isl_tab_is_equality(bmap->ctx, tab, n_eq + i))
1231 isl_basic_map_inequality_to_equality(bmap, i);
1232 else if (isl_tab_is_redundant(bmap->ctx, tab, n_eq + i))
1233 isl_basic_map_drop_inequality(bmap, i);
1235 if (!tab->rational &&
1236 !bmap->sample && sample_is_integer(bmap->ctx, tab))
1237 bmap->sample = extract_integer_sample(bmap->ctx, tab);
1238 return bmap;
1241 struct isl_basic_set *isl_basic_set_update_from_tab(struct isl_basic_set *bset,
1242 struct isl_tab *tab)
1244 return (struct isl_basic_set *)isl_basic_map_update_from_tab(
1245 (struct isl_basic_map *)bset, tab);
1248 /* Given a non-negative variable "var", add a new non-negative variable
1249 * that is the opposite of "var", ensuring that var can only attain the
1250 * value zero.
1251 * If var = n/d is a row variable, then the new variable = -n/d.
1252 * If var is a column variables, then the new variable = -var.
1253 * If the new variable cannot attain non-negative values, then
1254 * the resulting tableau is empty.
1255 * Otherwise, we know the value will be zero and we close the row.
1257 static struct isl_tab *cut_to_hyperplane(struct isl_ctx *ctx,
1258 struct isl_tab *tab, struct isl_tab_var *var)
1260 unsigned r;
1261 isl_int *row;
1262 int sgn;
1264 if (extend_cons(ctx, tab, 1) < 0)
1265 goto error;
1267 r = tab->n_con;
1268 tab->con[r].index = tab->n_row;
1269 tab->con[r].is_row = 1;
1270 tab->con[r].is_nonneg = 0;
1271 tab->con[r].is_zero = 0;
1272 tab->con[r].is_redundant = 0;
1273 tab->con[r].frozen = 0;
1274 tab->row_var[tab->n_row] = ~r;
1275 row = tab->mat->row[tab->n_row];
1277 if (var->is_row) {
1278 isl_int_set(row[0], tab->mat->row[var->index][0]);
1279 isl_seq_neg(row + 1,
1280 tab->mat->row[var->index] + 1, 1 + tab->n_col);
1281 } else {
1282 isl_int_set_si(row[0], 1);
1283 isl_seq_clr(row + 1, 1 + tab->n_col);
1284 isl_int_set_si(row[2 + var->index], -1);
1287 tab->n_row++;
1288 tab->n_con++;
1289 push(ctx, tab, isl_tab_undo_allocate, &tab->con[r]);
1291 sgn = sign_of_max(ctx, tab, &tab->con[r]);
1292 if (sgn < 0)
1293 mark_empty(ctx, tab);
1294 else {
1295 tab->con[r].is_nonneg = 1;
1296 push(ctx, tab, isl_tab_undo_nonneg, &tab->con[r]);
1297 /* sgn == 0 */
1298 close_row(ctx, tab, &tab->con[r]);
1301 return tab;
1302 error:
1303 isl_tab_free(ctx, tab);
1304 return NULL;
1307 /* Given a tableau "tab" and an inequality constraint "con" of the tableau,
1308 * relax the inequality by one. That is, the inequality r >= 0 is replaced
1309 * by r' = r + 1 >= 0.
1310 * If r is a row variable, we simply increase the constant term by one
1311 * (taking into account the denominator).
1312 * If r is a column variable, then we need to modify each row that
1313 * refers to r = r' - 1 by substituting this equality, effectively
1314 * subtracting the coefficient of the column from the constant.
1316 struct isl_tab *isl_tab_relax(struct isl_ctx *ctx,
1317 struct isl_tab *tab, int con)
1319 struct isl_tab_var *var;
1320 if (!tab)
1321 return NULL;
1323 var = &tab->con[con];
1325 if (!var->is_row && !max_is_manifestly_unbounded(ctx, tab, var))
1326 to_row(ctx, tab, var, 1);
1328 if (var->is_row)
1329 isl_int_add(tab->mat->row[var->index][1],
1330 tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
1331 else {
1332 int i;
1334 for (i = 0; i < tab->n_row; ++i) {
1335 if (isl_int_is_zero(tab->mat->row[i][2 + var->index]))
1336 continue;
1337 isl_int_sub(tab->mat->row[i][1], tab->mat->row[i][1],
1338 tab->mat->row[i][2 + var->index]);
1343 push(ctx, tab, isl_tab_undo_relax, var);
1345 return tab;
1348 struct isl_tab *isl_tab_select_facet(struct isl_ctx *ctx,
1349 struct isl_tab *tab, int con)
1351 if (!tab)
1352 return NULL;
1354 return cut_to_hyperplane(ctx, tab, &tab->con[con]);
1357 static int may_be_equality(struct isl_tab *tab, int row)
1359 return (tab->rational ? isl_int_is_zero(tab->mat->row[row][1])
1360 : isl_int_lt(tab->mat->row[row][1],
1361 tab->mat->row[row][0])) &&
1362 isl_seq_first_non_zero(tab->mat->row[row] + 2 + tab->n_dead,
1363 tab->n_col - tab->n_dead) != -1;
1366 /* Check for (near) equalities among the constraints.
1367 * A constraint is an equality if it is non-negative and if
1368 * its maximal value is either
1369 * - zero (in case of rational tableaus), or
1370 * - strictly less than 1 (in case of integer tableaus)
1372 * We first mark all non-redundant and non-dead variables that
1373 * are not frozen and not obviously not an equality.
1374 * Then we iterate over all marked variables if they can attain
1375 * any values larger than zero or at least one.
1376 * If the maximal value is zero, we mark any column variables
1377 * that appear in the row as being zero and mark the row as being redundant.
1378 * Otherwise, if the maximal value is strictly less than one (and the
1379 * tableau is integer), then we restrict the value to being zero
1380 * by adding an opposite non-negative variable.
1382 struct isl_tab *isl_tab_detect_equalities(struct isl_ctx *ctx,
1383 struct isl_tab *tab)
1385 int i;
1386 unsigned n_marked;
1388 if (!tab)
1389 return NULL;
1390 if (tab->empty)
1391 return tab;
1392 if (tab->n_dead == tab->n_col)
1393 return tab;
1395 n_marked = 0;
1396 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1397 struct isl_tab_var *var = var_from_row(ctx, tab, i);
1398 var->marked = !var->frozen && var->is_nonneg &&
1399 may_be_equality(tab, i);
1400 if (var->marked)
1401 n_marked++;
1403 for (i = tab->n_dead; i < tab->n_col; ++i) {
1404 struct isl_tab_var *var = var_from_col(ctx, tab, i);
1405 var->marked = !var->frozen && var->is_nonneg;
1406 if (var->marked)
1407 n_marked++;
1409 while (n_marked) {
1410 struct isl_tab_var *var;
1411 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1412 var = var_from_row(ctx, tab, i);
1413 if (var->marked)
1414 break;
1416 if (i == tab->n_row) {
1417 for (i = tab->n_dead; i < tab->n_col; ++i) {
1418 var = var_from_col(ctx, tab, i);
1419 if (var->marked)
1420 break;
1422 if (i == tab->n_col)
1423 break;
1425 var->marked = 0;
1426 n_marked--;
1427 if (sign_of_max(ctx, tab, var) == 0)
1428 close_row(ctx, tab, var);
1429 else if (!tab->rational && !at_least_one(ctx, tab, var)) {
1430 tab = cut_to_hyperplane(ctx, tab, var);
1431 return isl_tab_detect_equalities(ctx, tab);
1433 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1434 var = var_from_row(ctx, tab, i);
1435 if (!var->marked)
1436 continue;
1437 if (may_be_equality(tab, i))
1438 continue;
1439 var->marked = 0;
1440 n_marked--;
1444 return tab;
1447 /* Check for (near) redundant constraints.
1448 * A constraint is redundant if it is non-negative and if
1449 * its minimal value (temporarily ignoring the non-negativity) is either
1450 * - zero (in case of rational tableaus), or
1451 * - strictly larger than -1 (in case of integer tableaus)
1453 * We first mark all non-redundant and non-dead variables that
1454 * are not frozen and not obviously negatively unbounded.
1455 * Then we iterate over all marked variables if they can attain
1456 * any values smaller than zero or at most negative one.
1457 * If not, we mark the row as being redundant (assuming it hasn't
1458 * been detected as being obviously redundant in the mean time).
1460 struct isl_tab *isl_tab_detect_redundant(struct isl_ctx *ctx,
1461 struct isl_tab *tab)
1463 int i;
1464 unsigned n_marked;
1466 if (!tab)
1467 return NULL;
1468 if (tab->empty)
1469 return tab;
1470 if (tab->n_redundant == tab->n_row)
1471 return tab;
1473 n_marked = 0;
1474 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1475 struct isl_tab_var *var = var_from_row(ctx, tab, i);
1476 var->marked = !var->frozen && var->is_nonneg;
1477 if (var->marked)
1478 n_marked++;
1480 for (i = tab->n_dead; i < tab->n_col; ++i) {
1481 struct isl_tab_var *var = var_from_col(ctx, tab, i);
1482 var->marked = !var->frozen && var->is_nonneg &&
1483 !min_is_manifestly_unbounded(ctx, tab, var);
1484 if (var->marked)
1485 n_marked++;
1487 while (n_marked) {
1488 struct isl_tab_var *var;
1489 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1490 var = var_from_row(ctx, tab, i);
1491 if (var->marked)
1492 break;
1494 if (i == tab->n_row) {
1495 for (i = tab->n_dead; i < tab->n_col; ++i) {
1496 var = var_from_col(ctx, tab, i);
1497 if (var->marked)
1498 break;
1500 if (i == tab->n_col)
1501 break;
1503 var->marked = 0;
1504 n_marked--;
1505 if ((tab->rational ? (sign_of_min(ctx, tab, var) >= 0)
1506 : !min_at_most_neg_one(ctx, tab, var)) &&
1507 !var->is_redundant)
1508 mark_redundant(ctx, tab, var->index);
1509 for (i = tab->n_dead; i < tab->n_col; ++i) {
1510 var = var_from_col(ctx, tab, i);
1511 if (!var->marked)
1512 continue;
1513 if (!min_is_manifestly_unbounded(ctx, tab, var))
1514 continue;
1515 var->marked = 0;
1516 n_marked--;
1520 return tab;
1523 int isl_tab_is_equality(struct isl_ctx *ctx, struct isl_tab *tab, int con)
1525 int row;
1527 if (!tab)
1528 return -1;
1529 if (tab->con[con].is_zero)
1530 return 1;
1531 if (tab->con[con].is_redundant)
1532 return 0;
1533 if (!tab->con[con].is_row)
1534 return tab->con[con].index < tab->n_dead;
1536 row = tab->con[con].index;
1538 return isl_int_is_zero(tab->mat->row[row][1]) &&
1539 isl_seq_first_non_zero(tab->mat->row[row] + 2 + tab->n_dead,
1540 tab->n_col - tab->n_dead) == -1;
1543 /* Return the minimial value of the affine expression "f" with denominator
1544 * "denom" in *opt, *opt_denom, assuming the tableau is not empty and
1545 * the expression cannot attain arbitrarily small values.
1546 * If opt_denom is NULL, then *opt is rounded up to the nearest integer.
1547 * The return value reflects the nature of the result (empty, unbounded,
1548 * minmimal value returned in *opt).
1550 enum isl_lp_result isl_tab_min(struct isl_ctx *ctx, struct isl_tab *tab,
1551 isl_int *f, isl_int denom, isl_int *opt, isl_int *opt_denom,
1552 unsigned flags)
1554 int r;
1555 enum isl_lp_result res = isl_lp_ok;
1556 struct isl_tab_var *var;
1558 if (tab->empty)
1559 return isl_lp_empty;
1561 r = add_row(ctx, tab, f);
1562 if (r < 0)
1563 return isl_lp_error;
1564 var = &tab->con[r];
1565 isl_int_mul(tab->mat->row[var->index][0],
1566 tab->mat->row[var->index][0], denom);
1567 for (;;) {
1568 int row, col;
1569 find_pivot(ctx, tab, var, var, -1, &row, &col);
1570 if (row == var->index) {
1571 res = isl_lp_unbounded;
1572 break;
1574 if (row == -1)
1575 break;
1576 pivot(ctx, tab, row, col);
1578 if (drop_row(ctx, tab, var->index) < 0)
1579 return isl_lp_error;
1580 if (ISL_FL_ISSET(flags, ISL_TAB_SAVE_DUAL)) {
1581 int i;
1583 isl_vec_free(tab->dual);
1584 tab->dual = isl_vec_alloc(ctx, 1 + tab->n_con);
1585 if (!tab->dual)
1586 return isl_lp_error;
1587 isl_int_set(tab->dual->el[0], tab->mat->row[var->index][0]);
1588 for (i = 0; i < tab->n_con; ++i) {
1589 if (tab->con[i].is_row)
1590 isl_int_set_si(tab->dual->el[1 + i], 0);
1591 else {
1592 int pos = 2 + tab->con[i].index;
1593 isl_int_set(tab->dual->el[1 + i],
1594 tab->mat->row[var->index][pos]);
1598 if (res == isl_lp_ok) {
1599 if (opt_denom) {
1600 isl_int_set(*opt, tab->mat->row[var->index][1]);
1601 isl_int_set(*opt_denom, tab->mat->row[var->index][0]);
1602 } else
1603 isl_int_cdiv_q(*opt, tab->mat->row[var->index][1],
1604 tab->mat->row[var->index][0]);
1606 return res;
1609 int isl_tab_is_redundant(struct isl_ctx *ctx, struct isl_tab *tab, int con)
1611 int row;
1612 unsigned n_col;
1614 if (!tab)
1615 return -1;
1616 if (tab->con[con].is_zero)
1617 return 0;
1618 if (tab->con[con].is_redundant)
1619 return 1;
1620 return tab->con[con].is_row && tab->con[con].index < tab->n_redundant;
1623 /* Take a snapshot of the tableau that can be restored by s call to
1624 * isl_tab_rollback.
1626 struct isl_tab_undo *isl_tab_snap(struct isl_ctx *ctx, struct isl_tab *tab)
1628 if (!tab)
1629 return NULL;
1630 tab->need_undo = 1;
1631 return tab->top;
1634 /* Undo the operation performed by isl_tab_relax.
1636 static void unrelax(struct isl_ctx *ctx,
1637 struct isl_tab *tab, struct isl_tab_var *var)
1639 if (!var->is_row && !max_is_manifestly_unbounded(ctx, tab, var))
1640 to_row(ctx, tab, var, 1);
1642 if (var->is_row)
1643 isl_int_sub(tab->mat->row[var->index][1],
1644 tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
1645 else {
1646 int i;
1648 for (i = 0; i < tab->n_row; ++i) {
1649 if (isl_int_is_zero(tab->mat->row[i][2 + var->index]))
1650 continue;
1651 isl_int_add(tab->mat->row[i][1], tab->mat->row[i][1],
1652 tab->mat->row[i][2 + var->index]);
1658 static void perform_undo(struct isl_ctx *ctx, struct isl_tab *tab,
1659 struct isl_tab_undo *undo)
1661 switch(undo->type) {
1662 case isl_tab_undo_empty:
1663 tab->empty = 0;
1664 break;
1665 case isl_tab_undo_nonneg:
1666 undo->var->is_nonneg = 0;
1667 break;
1668 case isl_tab_undo_redundant:
1669 undo->var->is_redundant = 0;
1670 tab->n_redundant--;
1671 break;
1672 case isl_tab_undo_zero:
1673 undo->var->is_zero = 0;
1674 tab->n_dead--;
1675 break;
1676 case isl_tab_undo_allocate:
1677 if (!undo->var->is_row) {
1678 if (max_is_manifestly_unbounded(ctx, tab, undo->var))
1679 to_row(ctx, tab, undo->var, -1);
1680 else
1681 to_row(ctx, tab, undo->var, 1);
1683 drop_row(ctx, tab, undo->var->index);
1684 break;
1685 case isl_tab_undo_relax:
1686 unrelax(ctx, tab, undo->var);
1687 break;
1691 /* Return the tableau to the state it was in when the snapshot "snap"
1692 * was taken.
1694 int isl_tab_rollback(struct isl_ctx *ctx, struct isl_tab *tab,
1695 struct isl_tab_undo *snap)
1697 struct isl_tab_undo *undo, *next;
1699 if (!tab)
1700 return -1;
1702 for (undo = tab->top; undo && undo != &tab->bottom; undo = next) {
1703 next = undo->next;
1704 if (undo == snap)
1705 break;
1706 perform_undo(ctx, tab, undo);
1707 free(undo);
1709 tab->top = undo;
1710 if (!undo)
1711 return -1;
1712 return 0;
1715 /* The given row "row" represents an inequality violated by all
1716 * points in the tableau. Check for some special cases of such
1717 * separating constraints.
1718 * In particular, if the row has been reduced to the constant -1,
1719 * then we know the inequality is adjacent (but opposite) to
1720 * an equality in the tableau.
1721 * If the row has been reduced to r = -1 -r', with r' an inequality
1722 * of the tableau, then the inequality is adjacent (but opposite)
1723 * to the inequality r'.
1725 static enum isl_ineq_type separation_type(struct isl_ctx *ctx,
1726 struct isl_tab *tab, unsigned row)
1728 int pos;
1730 if (tab->rational)
1731 return isl_ineq_separate;
1733 if (!isl_int_is_one(tab->mat->row[row][0]))
1734 return isl_ineq_separate;
1735 if (!isl_int_is_negone(tab->mat->row[row][1]))
1736 return isl_ineq_separate;
1738 pos = isl_seq_first_non_zero(tab->mat->row[row] + 2 + tab->n_dead,
1739 tab->n_col - tab->n_dead);
1740 if (pos == -1)
1741 return isl_ineq_adj_eq;
1743 if (!isl_int_is_negone(tab->mat->row[row][2 + tab->n_dead + pos]))
1744 return isl_ineq_separate;
1746 pos = isl_seq_first_non_zero(
1747 tab->mat->row[row] + 2 + tab->n_dead + pos + 1,
1748 tab->n_col - tab->n_dead - pos - 1);
1750 return pos == -1 ? isl_ineq_adj_ineq : isl_ineq_separate;
1753 /* Check the effect of inequality "ineq" on the tableau "tab".
1754 * The result may be
1755 * isl_ineq_redundant: satisfied by all points in the tableau
1756 * isl_ineq_separate: satisfied by no point in the tableau
1757 * isl_ineq_cut: satisfied by some by not all points
1758 * isl_ineq_adj_eq: adjacent to an equality
1759 * isl_ineq_adj_ineq: adjacent to an inequality.
1761 enum isl_ineq_type isl_tab_ineq_type(struct isl_ctx *ctx, struct isl_tab *tab,
1762 isl_int *ineq)
1764 enum isl_ineq_type type = isl_ineq_error;
1765 struct isl_tab_undo *snap = NULL;
1766 int con;
1767 int row;
1769 if (!tab)
1770 return isl_ineq_error;
1772 if (extend_cons(ctx, tab, 1) < 0)
1773 return isl_ineq_error;
1775 snap = isl_tab_snap(ctx, tab);
1777 con = add_row(ctx, tab, ineq);
1778 if (con < 0)
1779 goto error;
1781 row = tab->con[con].index;
1782 if (is_redundant(ctx, tab, row))
1783 type = isl_ineq_redundant;
1784 else if (isl_int_is_neg(tab->mat->row[row][1]) &&
1785 (tab->rational ||
1786 isl_int_abs_ge(tab->mat->row[row][1],
1787 tab->mat->row[row][0]))) {
1788 if (at_least_zero(ctx, tab, &tab->con[con]))
1789 type = isl_ineq_cut;
1790 else
1791 type = separation_type(ctx, tab, row);
1792 } else if (tab->rational ? (sign_of_min(ctx, tab, &tab->con[con]) < 0)
1793 : min_at_most_neg_one(ctx, tab, &tab->con[con]))
1794 type = isl_ineq_cut;
1795 else
1796 type = isl_ineq_redundant;
1798 if (isl_tab_rollback(ctx, tab, snap))
1799 return isl_ineq_error;
1800 return type;
1801 error:
1802 isl_tab_rollback(ctx, tab, snap);
1803 return isl_ineq_error;
1806 void isl_tab_dump(struct isl_ctx *ctx, struct isl_tab *tab,
1807 FILE *out, int indent)
1809 unsigned r, c;
1810 int i;
1812 if (!tab) {
1813 fprintf(out, "%*snull tab\n", indent, "");
1814 return;
1816 fprintf(out, "%*sn_redundant: %d, n_dead: %d", indent, "",
1817 tab->n_redundant, tab->n_dead);
1818 if (tab->rational)
1819 fprintf(out, ", rational");
1820 if (tab->empty)
1821 fprintf(out, ", empty");
1822 fprintf(out, "\n");
1823 fprintf(out, "%*s[", indent, "");
1824 for (i = 0; i < tab->n_var; ++i) {
1825 if (i)
1826 fprintf(out, ", ");
1827 fprintf(out, "%c%d%s", tab->var[i].is_row ? 'r' : 'c',
1828 tab->var[i].index,
1829 tab->var[i].is_zero ? " [=0]" :
1830 tab->var[i].is_redundant ? " [R]" : "");
1832 fprintf(out, "]\n");
1833 fprintf(out, "%*s[", indent, "");
1834 for (i = 0; i < tab->n_con; ++i) {
1835 if (i)
1836 fprintf(out, ", ");
1837 fprintf(out, "%c%d%s", tab->con[i].is_row ? 'r' : 'c',
1838 tab->con[i].index,
1839 tab->con[i].is_zero ? " [=0]" :
1840 tab->con[i].is_redundant ? " [R]" : "");
1842 fprintf(out, "]\n");
1843 fprintf(out, "%*s[", indent, "");
1844 for (i = 0; i < tab->n_row; ++i) {
1845 if (i)
1846 fprintf(out, ", ");
1847 fprintf(out, "r%d: %d%s", i, tab->row_var[i],
1848 var_from_row(ctx, tab, i)->is_nonneg ? " [>=0]" : "");
1850 fprintf(out, "]\n");
1851 fprintf(out, "%*s[", indent, "");
1852 for (i = 0; i < tab->n_col; ++i) {
1853 if (i)
1854 fprintf(out, ", ");
1855 fprintf(out, "c%d: %d%s", i, tab->col_var[i],
1856 var_from_col(ctx, tab, i)->is_nonneg ? " [>=0]" : "");
1858 fprintf(out, "]\n");
1859 r = tab->mat->n_row;
1860 tab->mat->n_row = tab->n_row;
1861 c = tab->mat->n_col;
1862 tab->mat->n_col = 2 + tab->n_col;
1863 isl_mat_dump(ctx, tab->mat, out, indent);
1864 tab->mat->n_row = r;
1865 tab->mat->n_col = c;