isl_tab: add isl_basic_set field for optionally keeping track of inequalities
[isl.git] / isl_tab.c
blob6bde8900e27725bf69d2e2924d996f8b518bf64d
1 #include "isl_mat.h"
2 #include "isl_map_private.h"
3 #include "isl_tab.h"
5 /*
6 * The implementation of tableaus in this file was inspired by Section 8
7 * of David Detlefs, Greg Nelson and James B. Saxe, "Simplify: a theorem
8 * prover for program checking".
9 */
11 struct isl_tab *isl_tab_alloc(struct isl_ctx *ctx,
12 unsigned n_row, unsigned n_var, unsigned M)
14 int i;
15 struct isl_tab *tab;
16 unsigned off = 2 + M;
18 tab = isl_calloc_type(ctx, struct isl_tab);
19 if (!tab)
20 return NULL;
21 tab->mat = isl_mat_alloc(ctx, n_row, off + n_var);
22 if (!tab->mat)
23 goto error;
24 tab->var = isl_alloc_array(ctx, struct isl_tab_var, n_var);
25 if (!tab->var)
26 goto error;
27 tab->con = isl_alloc_array(ctx, struct isl_tab_var, n_row);
28 if (!tab->con)
29 goto error;
30 tab->col_var = isl_alloc_array(ctx, int, n_var);
31 if (!tab->col_var)
32 goto error;
33 tab->row_var = isl_alloc_array(ctx, int, n_row);
34 if (!tab->row_var)
35 goto error;
36 for (i = 0; i < n_var; ++i) {
37 tab->var[i].index = i;
38 tab->var[i].is_row = 0;
39 tab->var[i].is_nonneg = 0;
40 tab->var[i].is_zero = 0;
41 tab->var[i].is_redundant = 0;
42 tab->var[i].frozen = 0;
43 tab->col_var[i] = i;
45 tab->n_row = 0;
46 tab->n_con = 0;
47 tab->n_eq = 0;
48 tab->max_con = n_row;
49 tab->n_col = n_var;
50 tab->n_var = n_var;
51 tab->max_var = n_var;
52 tab->n_param = 0;
53 tab->n_div = 0;
54 tab->n_dead = 0;
55 tab->n_redundant = 0;
56 tab->need_undo = 0;
57 tab->rational = 0;
58 tab->empty = 0;
59 tab->in_undo = 0;
60 tab->M = M;
61 tab->bottom.type = isl_tab_undo_bottom;
62 tab->bottom.next = NULL;
63 tab->top = &tab->bottom;
64 return tab;
65 error:
66 isl_tab_free(tab);
67 return NULL;
70 int isl_tab_extend_cons(struct isl_tab *tab, unsigned n_new)
72 unsigned off = 2 + tab->M;
73 if (tab->max_con < tab->n_con + n_new) {
74 struct isl_tab_var *con;
76 con = isl_realloc_array(tab->mat->ctx, tab->con,
77 struct isl_tab_var, tab->max_con + n_new);
78 if (!con)
79 return -1;
80 tab->con = con;
81 tab->max_con += n_new;
83 if (tab->mat->n_row < tab->n_row + n_new) {
84 int *row_var;
86 tab->mat = isl_mat_extend(tab->mat,
87 tab->n_row + n_new, off + tab->n_col);
88 if (!tab->mat)
89 return -1;
90 row_var = isl_realloc_array(tab->mat->ctx, tab->row_var,
91 int, tab->mat->n_row);
92 if (!row_var)
93 return -1;
94 tab->row_var = row_var;
96 return 0;
99 /* Make room for at least n_new extra variables.
100 * Return -1 if anything went wrong.
102 int isl_tab_extend_vars(struct isl_tab *tab, unsigned n_new)
104 struct isl_tab_var *var;
105 unsigned off = 2 + tab->M;
107 if (tab->max_var < tab->n_var + n_new) {
108 var = isl_realloc_array(tab->mat->ctx, tab->var,
109 struct isl_tab_var, tab->n_var + n_new);
110 if (!var)
111 return -1;
112 tab->var = var;
113 tab->max_var += n_new;
116 if (tab->mat->n_col < off + tab->n_col + n_new) {
117 int *p;
119 tab->mat = isl_mat_extend(tab->mat,
120 tab->mat->n_row, off + tab->n_col + n_new);
121 if (!tab->mat)
122 return -1;
123 p = isl_realloc_array(tab->mat->ctx, tab->col_var,
124 int, tab->mat->n_col);
125 if (!p)
126 return -1;
127 tab->col_var = p;
130 return 0;
133 struct isl_tab *isl_tab_extend(struct isl_tab *tab, unsigned n_new)
135 if (isl_tab_extend_cons(tab, n_new) >= 0)
136 return tab;
138 isl_tab_free(tab);
139 return NULL;
142 static void free_undo(struct isl_tab *tab)
144 struct isl_tab_undo *undo, *next;
146 for (undo = tab->top; undo && undo != &tab->bottom; undo = next) {
147 next = undo->next;
148 free(undo);
150 tab->top = undo;
153 void isl_tab_free(struct isl_tab *tab)
155 if (!tab)
156 return;
157 free_undo(tab);
158 isl_mat_free(tab->mat);
159 isl_vec_free(tab->dual);
160 isl_basic_set_free(tab->bset);
161 free(tab->var);
162 free(tab->con);
163 free(tab->row_var);
164 free(tab->col_var);
165 free(tab);
168 struct isl_tab *isl_tab_dup(struct isl_tab *tab)
170 int i;
171 struct isl_tab *dup;
173 if (!tab)
174 return NULL;
176 dup = isl_calloc_type(tab->ctx, struct isl_tab);
177 if (!dup)
178 return NULL;
179 dup->mat = isl_mat_dup(tab->mat);
180 if (!dup->mat)
181 goto error;
182 dup->var = isl_alloc_array(tab->ctx, struct isl_tab_var, tab->max_var);
183 if (!dup->var)
184 goto error;
185 for (i = 0; i < tab->n_var; ++i)
186 dup->var[i] = tab->var[i];
187 dup->con = isl_alloc_array(tab->ctx, struct isl_tab_var, tab->max_con);
188 if (!dup->con)
189 goto error;
190 for (i = 0; i < tab->n_con; ++i)
191 dup->con[i] = tab->con[i];
192 dup->col_var = isl_alloc_array(tab->ctx, int, tab->mat->n_col);
193 if (!dup->col_var)
194 goto error;
195 for (i = 0; i < tab->n_var; ++i)
196 dup->col_var[i] = tab->col_var[i];
197 dup->row_var = isl_alloc_array(tab->ctx, int, tab->mat->n_row);
198 if (!dup->row_var)
199 goto error;
200 for (i = 0; i < tab->n_row; ++i)
201 dup->row_var[i] = tab->row_var[i];
202 dup->n_row = tab->n_row;
203 dup->n_con = tab->n_con;
204 dup->n_eq = tab->n_eq;
205 dup->max_con = tab->max_con;
206 dup->n_col = tab->n_col;
207 dup->n_var = tab->n_var;
208 dup->max_var = tab->max_var;
209 dup->n_param = tab->n_param;
210 dup->n_div = tab->n_div;
211 dup->n_dead = tab->n_dead;
212 dup->n_redundant = tab->n_redundant;
213 dup->rational = tab->rational;
214 dup->empty = tab->empty;
215 dup->need_undo = 0;
216 dup->in_undo = 0;
217 dup->M = tab->M;
218 dup->bottom.type = isl_tab_undo_bottom;
219 dup->bottom.next = NULL;
220 dup->top = &dup->bottom;
221 return dup;
222 error:
223 isl_tab_free(dup);
224 return NULL;
227 static struct isl_tab_var *var_from_index(struct isl_tab *tab, int i)
229 if (i >= 0)
230 return &tab->var[i];
231 else
232 return &tab->con[~i];
235 struct isl_tab_var *isl_tab_var_from_row(struct isl_tab *tab, int i)
237 return var_from_index(tab, tab->row_var[i]);
240 static struct isl_tab_var *var_from_col(struct isl_tab *tab, int i)
242 return var_from_index(tab, tab->col_var[i]);
245 /* Check if there are any upper bounds on column variable "var",
246 * i.e., non-negative rows where var appears with a negative coefficient.
247 * Return 1 if there are no such bounds.
249 static int max_is_manifestly_unbounded(struct isl_tab *tab,
250 struct isl_tab_var *var)
252 int i;
253 unsigned off = 2 + tab->M;
255 if (var->is_row)
256 return 0;
257 for (i = tab->n_redundant; i < tab->n_row; ++i) {
258 if (!isl_int_is_neg(tab->mat->row[i][off + var->index]))
259 continue;
260 if (isl_tab_var_from_row(tab, i)->is_nonneg)
261 return 0;
263 return 1;
266 /* Check if there are any lower bounds on column variable "var",
267 * i.e., non-negative rows where var appears with a positive coefficient.
268 * Return 1 if there are no such bounds.
270 static int min_is_manifestly_unbounded(struct isl_tab *tab,
271 struct isl_tab_var *var)
273 int i;
274 unsigned off = 2 + tab->M;
276 if (var->is_row)
277 return 0;
278 for (i = tab->n_redundant; i < tab->n_row; ++i) {
279 if (!isl_int_is_pos(tab->mat->row[i][off + var->index]))
280 continue;
281 if (isl_tab_var_from_row(tab, i)->is_nonneg)
282 return 0;
284 return 1;
287 static int row_cmp(struct isl_tab *tab, int r1, int r2, int c, isl_int t)
289 unsigned off = 2 + tab->M;
291 if (tab->M) {
292 int s;
293 isl_int_mul(t, tab->mat->row[r1][2], tab->mat->row[r2][off+c]);
294 isl_int_submul(t, tab->mat->row[r2][2], tab->mat->row[r1][off+c]);
295 s = isl_int_sgn(t);
296 if (s)
297 return s;
299 isl_int_mul(t, tab->mat->row[r1][1], tab->mat->row[r2][off + c]);
300 isl_int_submul(t, tab->mat->row[r2][1], tab->mat->row[r1][off + c]);
301 return isl_int_sgn(t);
304 /* Given the index of a column "c", return the index of a row
305 * that can be used to pivot the column in, with either an increase
306 * (sgn > 0) or a decrease (sgn < 0) of the corresponding variable.
307 * If "var" is not NULL, then the row returned will be different from
308 * the one associated with "var".
310 * Each row in the tableau is of the form
312 * x_r = a_r0 + \sum_i a_ri x_i
314 * Only rows with x_r >= 0 and with the sign of a_ri opposite to "sgn"
315 * impose any limit on the increase or decrease in the value of x_c
316 * and this bound is equal to a_r0 / |a_rc|. We are therefore looking
317 * for the row with the smallest (most stringent) such bound.
318 * Note that the common denominator of each row drops out of the fraction.
319 * To check if row j has a smaller bound than row r, i.e.,
320 * a_j0 / |a_jc| < a_r0 / |a_rc| or a_j0 |a_rc| < a_r0 |a_jc|,
321 * we check if -sign(a_jc) (a_j0 a_rc - a_r0 a_jc) < 0,
322 * where -sign(a_jc) is equal to "sgn".
324 static int pivot_row(struct isl_tab *tab,
325 struct isl_tab_var *var, int sgn, int c)
327 int j, r, tsgn;
328 isl_int t;
329 unsigned off = 2 + tab->M;
331 isl_int_init(t);
332 r = -1;
333 for (j = tab->n_redundant; j < tab->n_row; ++j) {
334 if (var && j == var->index)
335 continue;
336 if (!isl_tab_var_from_row(tab, j)->is_nonneg)
337 continue;
338 if (sgn * isl_int_sgn(tab->mat->row[j][off + c]) >= 0)
339 continue;
340 if (r < 0) {
341 r = j;
342 continue;
344 tsgn = sgn * row_cmp(tab, r, j, c, t);
345 if (tsgn < 0 || (tsgn == 0 &&
346 tab->row_var[j] < tab->row_var[r]))
347 r = j;
349 isl_int_clear(t);
350 return r;
353 /* Find a pivot (row and col) that will increase (sgn > 0) or decrease
354 * (sgn < 0) the value of row variable var.
355 * If not NULL, then skip_var is a row variable that should be ignored
356 * while looking for a pivot row. It is usually equal to var.
358 * As the given row in the tableau is of the form
360 * x_r = a_r0 + \sum_i a_ri x_i
362 * we need to find a column such that the sign of a_ri is equal to "sgn"
363 * (such that an increase in x_i will have the desired effect) or a
364 * column with a variable that may attain negative values.
365 * If a_ri is positive, then we need to move x_i in the same direction
366 * to obtain the desired effect. Otherwise, x_i has to move in the
367 * opposite direction.
369 static void find_pivot(struct isl_tab *tab,
370 struct isl_tab_var *var, struct isl_tab_var *skip_var,
371 int sgn, int *row, int *col)
373 int j, r, c;
374 isl_int *tr;
376 *row = *col = -1;
378 isl_assert(tab->mat->ctx, var->is_row, return);
379 tr = tab->mat->row[var->index] + 2 + tab->M;
381 c = -1;
382 for (j = tab->n_dead; j < tab->n_col; ++j) {
383 if (isl_int_is_zero(tr[j]))
384 continue;
385 if (isl_int_sgn(tr[j]) != sgn &&
386 var_from_col(tab, j)->is_nonneg)
387 continue;
388 if (c < 0 || tab->col_var[j] < tab->col_var[c])
389 c = j;
391 if (c < 0)
392 return;
394 sgn *= isl_int_sgn(tr[c]);
395 r = pivot_row(tab, skip_var, sgn, c);
396 *row = r < 0 ? var->index : r;
397 *col = c;
400 /* Return 1 if row "row" represents an obviously redundant inequality.
401 * This means
402 * - it represents an inequality or a variable
403 * - that is the sum of a non-negative sample value and a positive
404 * combination of zero or more non-negative variables.
406 int isl_tab_row_is_redundant(struct isl_tab *tab, int row)
408 int i;
409 unsigned off = 2 + tab->M;
411 if (tab->row_var[row] < 0 && !isl_tab_var_from_row(tab, row)->is_nonneg)
412 return 0;
414 if (isl_int_is_neg(tab->mat->row[row][1]))
415 return 0;
416 if (tab->M && isl_int_is_neg(tab->mat->row[row][2]))
417 return 0;
419 for (i = tab->n_dead; i < tab->n_col; ++i) {
420 if (isl_int_is_zero(tab->mat->row[row][off + i]))
421 continue;
422 if (isl_int_is_neg(tab->mat->row[row][off + i]))
423 return 0;
424 if (!var_from_col(tab, i)->is_nonneg)
425 return 0;
427 return 1;
430 static void swap_rows(struct isl_tab *tab, int row1, int row2)
432 int t;
433 t = tab->row_var[row1];
434 tab->row_var[row1] = tab->row_var[row2];
435 tab->row_var[row2] = t;
436 isl_tab_var_from_row(tab, row1)->index = row1;
437 isl_tab_var_from_row(tab, row2)->index = row2;
438 tab->mat = isl_mat_swap_rows(tab->mat, row1, row2);
441 static void push_union(struct isl_tab *tab,
442 enum isl_tab_undo_type type, union isl_tab_undo_val u)
444 struct isl_tab_undo *undo;
446 if (!tab->need_undo)
447 return;
449 undo = isl_alloc_type(tab->mat->ctx, struct isl_tab_undo);
450 if (!undo) {
451 free_undo(tab);
452 tab->top = NULL;
453 return;
455 undo->type = type;
456 undo->u = u;
457 undo->next = tab->top;
458 tab->top = undo;
461 void isl_tab_push_var(struct isl_tab *tab,
462 enum isl_tab_undo_type type, struct isl_tab_var *var)
464 union isl_tab_undo_val u;
465 if (var->is_row)
466 u.var_index = tab->row_var[var->index];
467 else
468 u.var_index = tab->col_var[var->index];
469 push_union(tab, type, u);
472 void isl_tab_push(struct isl_tab *tab, enum isl_tab_undo_type type)
474 union isl_tab_undo_val u = { 0 };
475 push_union(tab, type, u);
478 /* Push a record on the undo stack describing the current basic
479 * variables, so that the this state can be restored during rollback.
481 void isl_tab_push_basis(struct isl_tab *tab)
483 int i;
484 union isl_tab_undo_val u;
486 u.col_var = isl_alloc_array(tab->mat->ctx, int, tab->n_col);
487 if (!u.col_var) {
488 free_undo(tab);
489 tab->top = NULL;
490 return;
492 for (i = 0; i < tab->n_col; ++i)
493 u.col_var[i] = tab->col_var[i];
494 push_union(tab, isl_tab_undo_saved_basis, u);
497 /* Mark row with index "row" as being redundant.
498 * If we may need to undo the operation or if the row represents
499 * a variable of the original problem, the row is kept,
500 * but no longer considered when looking for a pivot row.
501 * Otherwise, the row is simply removed.
503 * The row may be interchanged with some other row. If it
504 * is interchanged with a later row, return 1. Otherwise return 0.
505 * If the rows are checked in order in the calling function,
506 * then a return value of 1 means that the row with the given
507 * row number may now contain a different row that hasn't been checked yet.
509 int isl_tab_mark_redundant(struct isl_tab *tab, int row)
511 struct isl_tab_var *var = isl_tab_var_from_row(tab, row);
512 var->is_redundant = 1;
513 isl_assert(tab->mat->ctx, row >= tab->n_redundant, return);
514 if (tab->need_undo || tab->row_var[row] >= 0) {
515 if (tab->row_var[row] >= 0 && !var->is_nonneg) {
516 var->is_nonneg = 1;
517 isl_tab_push_var(tab, isl_tab_undo_nonneg, var);
519 if (row != tab->n_redundant)
520 swap_rows(tab, row, tab->n_redundant);
521 isl_tab_push_var(tab, isl_tab_undo_redundant, var);
522 tab->n_redundant++;
523 return 0;
524 } else {
525 if (row != tab->n_row - 1)
526 swap_rows(tab, row, tab->n_row - 1);
527 isl_tab_var_from_row(tab, tab->n_row - 1)->index = -1;
528 tab->n_row--;
529 return 1;
533 struct isl_tab *isl_tab_mark_empty(struct isl_tab *tab)
535 if (!tab->empty && tab->need_undo)
536 isl_tab_push(tab, isl_tab_undo_empty);
537 tab->empty = 1;
538 return tab;
541 /* Given a row number "row" and a column number "col", pivot the tableau
542 * such that the associated variables are interchanged.
543 * The given row in the tableau expresses
545 * x_r = a_r0 + \sum_i a_ri x_i
547 * or
549 * x_c = 1/a_rc x_r - a_r0/a_rc + sum_{i \ne r} -a_ri/a_rc
551 * Substituting this equality into the other rows
553 * x_j = a_j0 + \sum_i a_ji x_i
555 * with a_jc \ne 0, we obtain
557 * 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
559 * The tableau
561 * n_rc/d_r n_ri/d_r
562 * n_jc/d_j n_ji/d_j
564 * where i is any other column and j is any other row,
565 * is therefore transformed into
567 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
568 * 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)
570 * The transformation is performed along the following steps
572 * d_r/n_rc n_ri/n_rc
573 * n_jc/d_j n_ji/d_j
575 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
576 * n_jc/d_j n_ji/d_j
578 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
579 * n_jc/(|n_rc| d_j) n_ji/(|n_rc| d_j)
581 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
582 * n_jc/(|n_rc| d_j) (n_ji |n_rc|)/(|n_rc| d_j)
584 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
585 * n_jc/(|n_rc| d_j) (n_ji |n_rc| - s(n_rc)n_jc n_ri)/(|n_rc| d_j)
587 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
588 * 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)
591 void isl_tab_pivot(struct isl_tab *tab, int row, int col)
593 int i, j;
594 int sgn;
595 int t;
596 struct isl_mat *mat = tab->mat;
597 struct isl_tab_var *var;
598 unsigned off = 2 + tab->M;
600 isl_int_swap(mat->row[row][0], mat->row[row][off + col]);
601 sgn = isl_int_sgn(mat->row[row][0]);
602 if (sgn < 0) {
603 isl_int_neg(mat->row[row][0], mat->row[row][0]);
604 isl_int_neg(mat->row[row][off + col], mat->row[row][off + col]);
605 } else
606 for (j = 0; j < off - 1 + tab->n_col; ++j) {
607 if (j == off - 1 + col)
608 continue;
609 isl_int_neg(mat->row[row][1 + j], mat->row[row][1 + j]);
611 if (!isl_int_is_one(mat->row[row][0]))
612 isl_seq_normalize(mat->row[row], off + tab->n_col);
613 for (i = 0; i < tab->n_row; ++i) {
614 if (i == row)
615 continue;
616 if (isl_int_is_zero(mat->row[i][off + col]))
617 continue;
618 isl_int_mul(mat->row[i][0], mat->row[i][0], mat->row[row][0]);
619 for (j = 0; j < off - 1 + tab->n_col; ++j) {
620 if (j == off - 1 + col)
621 continue;
622 isl_int_mul(mat->row[i][1 + j],
623 mat->row[i][1 + j], mat->row[row][0]);
624 isl_int_addmul(mat->row[i][1 + j],
625 mat->row[i][off + col], mat->row[row][1 + j]);
627 isl_int_mul(mat->row[i][off + col],
628 mat->row[i][off + col], mat->row[row][off + col]);
629 if (!isl_int_is_one(mat->row[i][0]))
630 isl_seq_normalize(mat->row[i], off + tab->n_col);
632 t = tab->row_var[row];
633 tab->row_var[row] = tab->col_var[col];
634 tab->col_var[col] = t;
635 var = isl_tab_var_from_row(tab, row);
636 var->is_row = 1;
637 var->index = row;
638 var = var_from_col(tab, col);
639 var->is_row = 0;
640 var->index = col;
641 if (tab->in_undo)
642 return;
643 for (i = tab->n_redundant; i < tab->n_row; ++i) {
644 if (isl_int_is_zero(mat->row[i][off + col]))
645 continue;
646 if (!isl_tab_var_from_row(tab, i)->frozen &&
647 isl_tab_row_is_redundant(tab, i))
648 if (isl_tab_mark_redundant(tab, i))
649 --i;
653 /* If "var" represents a column variable, then pivot is up (sgn > 0)
654 * or down (sgn < 0) to a row. The variable is assumed not to be
655 * unbounded in the specified direction.
656 * If sgn = 0, then the variable is unbounded in both directions,
657 * and we pivot with any row we can find.
659 static void to_row(struct isl_tab *tab, struct isl_tab_var *var, int sign)
661 int r;
662 unsigned off = 2 + tab->M;
664 if (var->is_row)
665 return;
667 if (sign == 0) {
668 for (r = tab->n_redundant; r < tab->n_row; ++r)
669 if (!isl_int_is_zero(tab->mat->row[r][off+var->index]))
670 break;
671 isl_assert(tab->mat->ctx, r < tab->n_row, return);
672 } else {
673 r = pivot_row(tab, NULL, sign, var->index);
674 isl_assert(tab->mat->ctx, r >= 0, return);
677 isl_tab_pivot(tab, r, var->index);
680 static void check_table(struct isl_tab *tab)
682 int i;
684 if (tab->empty)
685 return;
686 for (i = 0; i < tab->n_row; ++i) {
687 if (!isl_tab_var_from_row(tab, i)->is_nonneg)
688 continue;
689 assert(!isl_int_is_neg(tab->mat->row[i][1]));
693 /* Return the sign of the maximal value of "var".
694 * If the sign is not negative, then on return from this function,
695 * the sample value will also be non-negative.
697 * If "var" is manifestly unbounded wrt positive values, we are done.
698 * Otherwise, we pivot the variable up to a row if needed
699 * Then we continue pivoting down until either
700 * - no more down pivots can be performed
701 * - the sample value is positive
702 * - the variable is pivoted into a manifestly unbounded column
704 static int sign_of_max(struct isl_tab *tab, struct isl_tab_var *var)
706 int row, col;
708 if (max_is_manifestly_unbounded(tab, var))
709 return 1;
710 to_row(tab, var, 1);
711 while (!isl_int_is_pos(tab->mat->row[var->index][1])) {
712 find_pivot(tab, var, var, 1, &row, &col);
713 if (row == -1)
714 return isl_int_sgn(tab->mat->row[var->index][1]);
715 isl_tab_pivot(tab, row, col);
716 if (!var->is_row) /* manifestly unbounded */
717 return 1;
719 return 1;
722 static int row_is_neg(struct isl_tab *tab, int row)
724 if (!tab->M)
725 return isl_int_is_neg(tab->mat->row[row][1]);
726 if (isl_int_is_pos(tab->mat->row[row][2]))
727 return 0;
728 if (isl_int_is_neg(tab->mat->row[row][2]))
729 return 1;
730 return isl_int_is_neg(tab->mat->row[row][1]);
733 static int row_sgn(struct isl_tab *tab, int row)
735 if (!tab->M)
736 return isl_int_sgn(tab->mat->row[row][1]);
737 if (!isl_int_is_zero(tab->mat->row[row][2]))
738 return isl_int_sgn(tab->mat->row[row][2]);
739 else
740 return isl_int_sgn(tab->mat->row[row][1]);
743 /* Perform pivots until the row variable "var" has a non-negative
744 * sample value or until no more upward pivots can be performed.
745 * Return the sign of the sample value after the pivots have been
746 * performed.
748 static int restore_row(struct isl_tab *tab, struct isl_tab_var *var)
750 int row, col;
752 while (row_is_neg(tab, var->index)) {
753 find_pivot(tab, var, var, 1, &row, &col);
754 if (row == -1)
755 break;
756 isl_tab_pivot(tab, row, col);
757 if (!var->is_row) /* manifestly unbounded */
758 return 1;
760 return row_sgn(tab, var->index);
763 /* Perform pivots until we are sure that the row variable "var"
764 * can attain non-negative values. After return from this
765 * function, "var" is still a row variable, but its sample
766 * value may not be non-negative, even if the function returns 1.
768 static int at_least_zero(struct isl_tab *tab, struct isl_tab_var *var)
770 int row, col;
772 while (isl_int_is_neg(tab->mat->row[var->index][1])) {
773 find_pivot(tab, var, var, 1, &row, &col);
774 if (row == -1)
775 break;
776 if (row == var->index) /* manifestly unbounded */
777 return 1;
778 isl_tab_pivot(tab, row, col);
780 return !isl_int_is_neg(tab->mat->row[var->index][1]);
783 /* Return a negative value if "var" can attain negative values.
784 * Return a non-negative value otherwise.
786 * If "var" is manifestly unbounded wrt negative values, we are done.
787 * Otherwise, if var is in a column, we can pivot it down to a row.
788 * Then we continue pivoting down until either
789 * - the pivot would result in a manifestly unbounded column
790 * => we don't perform the pivot, but simply return -1
791 * - no more down pivots can be performed
792 * - the sample value is negative
793 * If the sample value becomes negative and the variable is supposed
794 * to be nonnegative, then we undo the last pivot.
795 * However, if the last pivot has made the pivoting variable
796 * obviously redundant, then it may have moved to another row.
797 * In that case we look for upward pivots until we reach a non-negative
798 * value again.
800 static int sign_of_min(struct isl_tab *tab, struct isl_tab_var *var)
802 int row, col;
803 struct isl_tab_var *pivot_var;
805 if (min_is_manifestly_unbounded(tab, var))
806 return -1;
807 if (!var->is_row) {
808 col = var->index;
809 row = pivot_row(tab, NULL, -1, col);
810 pivot_var = var_from_col(tab, col);
811 isl_tab_pivot(tab, row, col);
812 if (var->is_redundant)
813 return 0;
814 if (isl_int_is_neg(tab->mat->row[var->index][1])) {
815 if (var->is_nonneg) {
816 if (!pivot_var->is_redundant &&
817 pivot_var->index == row)
818 isl_tab_pivot(tab, row, col);
819 else
820 restore_row(tab, var);
822 return -1;
825 if (var->is_redundant)
826 return 0;
827 while (!isl_int_is_neg(tab->mat->row[var->index][1])) {
828 find_pivot(tab, var, var, -1, &row, &col);
829 if (row == var->index)
830 return -1;
831 if (row == -1)
832 return isl_int_sgn(tab->mat->row[var->index][1]);
833 pivot_var = var_from_col(tab, col);
834 isl_tab_pivot(tab, row, col);
835 if (var->is_redundant)
836 return 0;
838 if (var->is_nonneg) {
839 /* pivot back to non-negative value */
840 if (!pivot_var->is_redundant && pivot_var->index == row)
841 isl_tab_pivot(tab, row, col);
842 else
843 restore_row(tab, var);
845 return -1;
848 static int row_at_most_neg_one(struct isl_tab *tab, int row)
850 if (tab->M) {
851 if (isl_int_is_pos(tab->mat->row[row][2]))
852 return 0;
853 if (isl_int_is_neg(tab->mat->row[row][2]))
854 return 1;
856 return isl_int_is_neg(tab->mat->row[row][1]) &&
857 isl_int_abs_ge(tab->mat->row[row][1],
858 tab->mat->row[row][0]);
861 /* Return 1 if "var" can attain values <= -1.
862 * Return 0 otherwise.
864 * The sample value of "var" is assumed to be non-negative when the
865 * the function is called and will be made non-negative again before
866 * the function returns.
868 int isl_tab_min_at_most_neg_one(struct isl_tab *tab, struct isl_tab_var *var)
870 int row, col;
871 struct isl_tab_var *pivot_var;
873 if (min_is_manifestly_unbounded(tab, var))
874 return 1;
875 if (!var->is_row) {
876 col = var->index;
877 row = pivot_row(tab, NULL, -1, col);
878 pivot_var = var_from_col(tab, col);
879 isl_tab_pivot(tab, row, col);
880 if (var->is_redundant)
881 return 0;
882 if (row_at_most_neg_one(tab, var->index)) {
883 if (var->is_nonneg) {
884 if (!pivot_var->is_redundant &&
885 pivot_var->index == row)
886 isl_tab_pivot(tab, row, col);
887 else
888 restore_row(tab, var);
890 return 1;
893 if (var->is_redundant)
894 return 0;
895 do {
896 find_pivot(tab, var, var, -1, &row, &col);
897 if (row == var->index)
898 return 1;
899 if (row == -1)
900 return 0;
901 pivot_var = var_from_col(tab, col);
902 isl_tab_pivot(tab, row, col);
903 if (var->is_redundant)
904 return 0;
905 } while (!row_at_most_neg_one(tab, var->index));
906 if (var->is_nonneg) {
907 /* pivot back to non-negative value */
908 if (!pivot_var->is_redundant && pivot_var->index == row)
909 isl_tab_pivot(tab, row, col);
910 restore_row(tab, var);
912 return 1;
915 /* Return 1 if "var" can attain values >= 1.
916 * Return 0 otherwise.
918 static int at_least_one(struct isl_tab *tab, struct isl_tab_var *var)
920 int row, col;
921 isl_int *r;
923 if (max_is_manifestly_unbounded(tab, var))
924 return 1;
925 to_row(tab, var, 1);
926 r = tab->mat->row[var->index];
927 while (isl_int_lt(r[1], r[0])) {
928 find_pivot(tab, var, var, 1, &row, &col);
929 if (row == -1)
930 return isl_int_ge(r[1], r[0]);
931 if (row == var->index) /* manifestly unbounded */
932 return 1;
933 isl_tab_pivot(tab, row, col);
935 return 1;
938 static void swap_cols(struct isl_tab *tab, int col1, int col2)
940 int t;
941 unsigned off = 2 + tab->M;
942 t = tab->col_var[col1];
943 tab->col_var[col1] = tab->col_var[col2];
944 tab->col_var[col2] = t;
945 var_from_col(tab, col1)->index = col1;
946 var_from_col(tab, col2)->index = col2;
947 tab->mat = isl_mat_swap_cols(tab->mat, off + col1, off + col2);
950 /* Mark column with index "col" as representing a zero variable.
951 * If we may need to undo the operation the column is kept,
952 * but no longer considered.
953 * Otherwise, the column is simply removed.
955 * The column may be interchanged with some other column. If it
956 * is interchanged with a later column, return 1. Otherwise return 0.
957 * If the columns are checked in order in the calling function,
958 * then a return value of 1 means that the column with the given
959 * column number may now contain a different column that
960 * hasn't been checked yet.
962 int isl_tab_kill_col(struct isl_tab *tab, int col)
964 var_from_col(tab, col)->is_zero = 1;
965 if (tab->need_undo) {
966 isl_tab_push_var(tab, isl_tab_undo_zero, var_from_col(tab, col));
967 if (col != tab->n_dead)
968 swap_cols(tab, col, tab->n_dead);
969 tab->n_dead++;
970 return 0;
971 } else {
972 if (col != tab->n_col - 1)
973 swap_cols(tab, col, tab->n_col - 1);
974 var_from_col(tab, tab->n_col - 1)->index = -1;
975 tab->n_col--;
976 return 1;
980 /* Row variable "var" is non-negative and cannot attain any values
981 * larger than zero. This means that the coefficients of the unrestricted
982 * column variables are zero and that the coefficients of the non-negative
983 * column variables are zero or negative.
984 * Each of the non-negative variables with a negative coefficient can
985 * then also be written as the negative sum of non-negative variables
986 * and must therefore also be zero.
988 static void close_row(struct isl_tab *tab, struct isl_tab_var *var)
990 int j;
991 struct isl_mat *mat = tab->mat;
992 unsigned off = 2 + tab->M;
994 isl_assert(tab->mat->ctx, var->is_nonneg, return);
995 var->is_zero = 1;
996 for (j = tab->n_dead; j < tab->n_col; ++j) {
997 if (isl_int_is_zero(mat->row[var->index][off + j]))
998 continue;
999 isl_assert(tab->mat->ctx,
1000 isl_int_is_neg(mat->row[var->index][off + j]), return);
1001 if (isl_tab_kill_col(tab, j))
1002 --j;
1004 isl_tab_mark_redundant(tab, var->index);
1007 /* Add a constraint to the tableau and allocate a row for it.
1008 * Return the index into the constraint array "con".
1010 int isl_tab_allocate_con(struct isl_tab *tab)
1012 int r;
1014 isl_assert(tab->mat->ctx, tab->n_row < tab->mat->n_row, return -1);
1016 r = tab->n_con;
1017 tab->con[r].index = tab->n_row;
1018 tab->con[r].is_row = 1;
1019 tab->con[r].is_nonneg = 0;
1020 tab->con[r].is_zero = 0;
1021 tab->con[r].is_redundant = 0;
1022 tab->con[r].frozen = 0;
1023 tab->row_var[tab->n_row] = ~r;
1025 tab->n_row++;
1026 tab->n_con++;
1027 isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->con[r]);
1029 return r;
1032 /* Add a variable to the tableau and allocate a column for it.
1033 * Return the index into the variable array "var".
1035 int isl_tab_allocate_var(struct isl_tab *tab)
1037 int r;
1038 int i;
1039 unsigned off = 2 + tab->M;
1041 isl_assert(tab->mat->ctx, tab->n_col < tab->mat->n_col, return -1);
1042 isl_assert(tab->mat->ctx, tab->n_var < tab->max_var, return -1);
1044 r = tab->n_var;
1045 tab->var[r].index = tab->n_col;
1046 tab->var[r].is_row = 0;
1047 tab->var[r].is_nonneg = 0;
1048 tab->var[r].is_zero = 0;
1049 tab->var[r].is_redundant = 0;
1050 tab->var[r].frozen = 0;
1051 tab->col_var[tab->n_col] = r;
1053 for (i = 0; i < tab->n_row; ++i)
1054 isl_int_set_si(tab->mat->row[i][off + tab->n_col], 0);
1056 tab->n_var++;
1057 tab->n_col++;
1058 isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->var[r]);
1060 return r;
1063 /* Add a row to the tableau. The row is given as an affine combination
1064 * of the original variables and needs to be expressed in terms of the
1065 * column variables.
1067 * We add each term in turn.
1068 * If r = n/d_r is the current sum and we need to add k x, then
1069 * if x is a column variable, we increase the numerator of
1070 * this column by k d_r
1071 * if x = f/d_x is a row variable, then the new representation of r is
1073 * n k f d_x/g n + d_r/g k f m/d_r n + m/d_g k f
1074 * --- + --- = ------------------- = -------------------
1075 * d_r d_r d_r d_x/g m
1077 * with g the gcd of d_r and d_x and m the lcm of d_r and d_x.
1079 int isl_tab_add_row(struct isl_tab *tab, isl_int *line)
1081 int i;
1082 int r;
1083 isl_int *row;
1084 isl_int a, b;
1085 unsigned off = 2 + tab->M;
1087 r = isl_tab_allocate_con(tab);
1088 if (r < 0)
1089 return -1;
1091 isl_int_init(a);
1092 isl_int_init(b);
1093 row = tab->mat->row[tab->con[r].index];
1094 isl_int_set_si(row[0], 1);
1095 isl_int_set(row[1], line[0]);
1096 isl_seq_clr(row + 2, tab->M + tab->n_col);
1097 for (i = 0; i < tab->n_var; ++i) {
1098 if (tab->var[i].is_zero)
1099 continue;
1100 if (tab->var[i].is_row) {
1101 isl_int_lcm(a,
1102 row[0], tab->mat->row[tab->var[i].index][0]);
1103 isl_int_swap(a, row[0]);
1104 isl_int_divexact(a, row[0], a);
1105 isl_int_divexact(b,
1106 row[0], tab->mat->row[tab->var[i].index][0]);
1107 isl_int_mul(b, b, line[1 + i]);
1108 isl_seq_combine(row + 1, a, row + 1,
1109 b, tab->mat->row[tab->var[i].index] + 1,
1110 1 + tab->M + tab->n_col);
1111 } else
1112 isl_int_addmul(row[off + tab->var[i].index],
1113 line[1 + i], row[0]);
1114 if (tab->M && i >= tab->n_param && i < tab->n_var - tab->n_div)
1115 isl_int_submul(row[2], line[1 + i], row[0]);
1117 isl_seq_normalize(row, off + tab->n_col);
1118 isl_int_clear(a);
1119 isl_int_clear(b);
1121 return r;
1124 static int drop_row(struct isl_tab *tab, int row)
1126 isl_assert(tab->mat->ctx, ~tab->row_var[row] == tab->n_con - 1, return -1);
1127 if (row != tab->n_row - 1)
1128 swap_rows(tab, row, tab->n_row - 1);
1129 tab->n_row--;
1130 tab->n_con--;
1131 return 0;
1134 static int drop_col(struct isl_tab *tab, int col)
1136 isl_assert(tab->mat->ctx, tab->col_var[col] == tab->n_var - 1, return -1);
1137 if (col != tab->n_col - 1)
1138 swap_cols(tab, col, tab->n_col - 1);
1139 tab->n_col--;
1140 tab->n_var--;
1141 return 0;
1144 /* Add inequality "ineq" and check if it conflicts with the
1145 * previously added constraints or if it is obviously redundant.
1147 struct isl_tab *isl_tab_add_ineq(struct isl_tab *tab, isl_int *ineq)
1149 int r;
1150 int sgn;
1152 if (!tab)
1153 return NULL;
1154 r = isl_tab_add_row(tab, ineq);
1155 if (r < 0)
1156 goto error;
1157 tab->con[r].is_nonneg = 1;
1158 isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]);
1159 if (isl_tab_row_is_redundant(tab, tab->con[r].index)) {
1160 isl_tab_mark_redundant(tab, tab->con[r].index);
1161 return tab;
1164 sgn = restore_row(tab, &tab->con[r]);
1165 if (sgn < 0)
1166 return isl_tab_mark_empty(tab);
1167 if (tab->con[r].is_row && isl_tab_row_is_redundant(tab, tab->con[r].index))
1168 isl_tab_mark_redundant(tab, tab->con[r].index);
1169 return tab;
1170 error:
1171 isl_tab_free(tab);
1172 return NULL;
1175 /* Pivot a non-negative variable down until it reaches the value zero
1176 * and then pivot the variable into a column position.
1178 int to_col(struct isl_tab *tab, struct isl_tab_var *var)
1180 int i;
1181 int row, col;
1182 unsigned off = 2 + tab->M;
1184 if (!var->is_row)
1185 return;
1187 while (isl_int_is_pos(tab->mat->row[var->index][1])) {
1188 find_pivot(tab, var, NULL, -1, &row, &col);
1189 isl_assert(tab->mat->ctx, row != -1, return -1);
1190 isl_tab_pivot(tab, row, col);
1191 if (!var->is_row)
1192 return;
1195 for (i = tab->n_dead; i < tab->n_col; ++i)
1196 if (!isl_int_is_zero(tab->mat->row[var->index][off + i]))
1197 break;
1199 isl_assert(tab->mat->ctx, i < tab->n_col, return -1);
1200 isl_tab_pivot(tab, var->index, i);
1202 return 0;
1205 /* We assume Gaussian elimination has been performed on the equalities.
1206 * The equalities can therefore never conflict.
1207 * Adding the equalities is currently only really useful for a later call
1208 * to isl_tab_ineq_type.
1210 static struct isl_tab *add_eq(struct isl_tab *tab, isl_int *eq)
1212 int i;
1213 int r;
1215 if (!tab)
1216 return NULL;
1217 r = isl_tab_add_row(tab, eq);
1218 if (r < 0)
1219 goto error;
1221 r = tab->con[r].index;
1222 i = isl_seq_first_non_zero(tab->mat->row[r] + 2 + tab->M + tab->n_dead,
1223 tab->n_col - tab->n_dead);
1224 isl_assert(tab->mat->ctx, i >= 0, goto error);
1225 i += tab->n_dead;
1226 isl_tab_pivot(tab, r, i);
1227 isl_tab_kill_col(tab, i);
1228 tab->n_eq++;
1230 return tab;
1231 error:
1232 isl_tab_free(tab);
1233 return NULL;
1236 /* Add an equality that is known to be valid for the given tableau.
1238 struct isl_tab *isl_tab_add_valid_eq(struct isl_tab *tab, isl_int *eq)
1240 struct isl_tab_var *var;
1241 int i;
1242 int r;
1244 if (!tab)
1245 return NULL;
1246 r = isl_tab_add_row(tab, eq);
1247 if (r < 0)
1248 goto error;
1250 var = &tab->con[r];
1251 r = var->index;
1252 if (isl_int_is_neg(tab->mat->row[r][1]))
1253 isl_seq_neg(tab->mat->row[r] + 1, tab->mat->row[r] + 1,
1254 1 + tab->n_col);
1255 var->is_nonneg = 1;
1256 if (to_col(tab, var) < 0)
1257 goto error;
1258 var->is_nonneg = 0;
1259 isl_tab_kill_col(tab, var->index);
1261 return tab;
1262 error:
1263 isl_tab_free(tab);
1264 return NULL;
1267 struct isl_tab *isl_tab_from_basic_map(struct isl_basic_map *bmap)
1269 int i;
1270 struct isl_tab *tab;
1272 if (!bmap)
1273 return NULL;
1274 tab = isl_tab_alloc(bmap->ctx,
1275 isl_basic_map_total_dim(bmap) + bmap->n_ineq + 1,
1276 isl_basic_map_total_dim(bmap), 0);
1277 if (!tab)
1278 return NULL;
1279 tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
1280 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1281 return isl_tab_mark_empty(tab);
1282 for (i = 0; i < bmap->n_eq; ++i) {
1283 tab = add_eq(tab, bmap->eq[i]);
1284 if (!tab)
1285 return tab;
1287 for (i = 0; i < bmap->n_ineq; ++i) {
1288 tab = isl_tab_add_ineq(tab, bmap->ineq[i]);
1289 if (!tab || tab->empty)
1290 return tab;
1292 return tab;
1295 struct isl_tab *isl_tab_from_basic_set(struct isl_basic_set *bset)
1297 return isl_tab_from_basic_map((struct isl_basic_map *)bset);
1300 /* Construct a tableau corresponding to the recession cone of "bmap".
1302 struct isl_tab *isl_tab_from_recession_cone(struct isl_basic_map *bmap)
1304 isl_int cst;
1305 int i;
1306 struct isl_tab *tab;
1308 if (!bmap)
1309 return NULL;
1310 tab = isl_tab_alloc(bmap->ctx, bmap->n_eq + bmap->n_ineq,
1311 isl_basic_map_total_dim(bmap), 0);
1312 if (!tab)
1313 return NULL;
1314 tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
1316 isl_int_init(cst);
1317 for (i = 0; i < bmap->n_eq; ++i) {
1318 isl_int_swap(bmap->eq[i][0], cst);
1319 tab = add_eq(tab, bmap->eq[i]);
1320 isl_int_swap(bmap->eq[i][0], cst);
1321 if (!tab)
1322 goto done;
1324 for (i = 0; i < bmap->n_ineq; ++i) {
1325 int r;
1326 isl_int_swap(bmap->ineq[i][0], cst);
1327 r = isl_tab_add_row(tab, bmap->ineq[i]);
1328 isl_int_swap(bmap->ineq[i][0], cst);
1329 if (r < 0)
1330 goto error;
1331 tab->con[r].is_nonneg = 1;
1332 isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]);
1334 done:
1335 isl_int_clear(cst);
1336 return tab;
1337 error:
1338 isl_int_clear(cst);
1339 isl_tab_free(tab);
1340 return NULL;
1343 /* Assuming "tab" is the tableau of a cone, check if the cone is
1344 * bounded, i.e., if it is empty or only contains the origin.
1346 int isl_tab_cone_is_bounded(struct isl_tab *tab)
1348 int i;
1350 if (!tab)
1351 return -1;
1352 if (tab->empty)
1353 return 1;
1354 if (tab->n_dead == tab->n_col)
1355 return 1;
1357 for (;;) {
1358 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1359 struct isl_tab_var *var;
1360 var = isl_tab_var_from_row(tab, i);
1361 if (!var->is_nonneg)
1362 continue;
1363 if (sign_of_max(tab, var) != 0)
1364 return 0;
1365 close_row(tab, var);
1366 break;
1368 if (tab->n_dead == tab->n_col)
1369 return 1;
1370 if (i == tab->n_row)
1371 return 0;
1375 int isl_tab_sample_is_integer(struct isl_tab *tab)
1377 int i;
1379 if (!tab)
1380 return -1;
1382 for (i = 0; i < tab->n_var; ++i) {
1383 int row;
1384 if (!tab->var[i].is_row)
1385 continue;
1386 row = tab->var[i].index;
1387 if (!isl_int_is_divisible_by(tab->mat->row[row][1],
1388 tab->mat->row[row][0]))
1389 return 0;
1391 return 1;
1394 static struct isl_vec *extract_integer_sample(struct isl_tab *tab)
1396 int i;
1397 struct isl_vec *vec;
1399 vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
1400 if (!vec)
1401 return NULL;
1403 isl_int_set_si(vec->block.data[0], 1);
1404 for (i = 0; i < tab->n_var; ++i) {
1405 if (!tab->var[i].is_row)
1406 isl_int_set_si(vec->block.data[1 + i], 0);
1407 else {
1408 int row = tab->var[i].index;
1409 isl_int_divexact(vec->block.data[1 + i],
1410 tab->mat->row[row][1], tab->mat->row[row][0]);
1414 return vec;
1417 struct isl_vec *isl_tab_get_sample_value(struct isl_tab *tab)
1419 int i;
1420 struct isl_vec *vec;
1421 isl_int m;
1423 if (!tab)
1424 return NULL;
1426 vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
1427 if (!vec)
1428 return NULL;
1430 isl_int_init(m);
1432 isl_int_set_si(vec->block.data[0], 1);
1433 for (i = 0; i < tab->n_var; ++i) {
1434 int row;
1435 if (!tab->var[i].is_row) {
1436 isl_int_set_si(vec->block.data[1 + i], 0);
1437 continue;
1439 row = tab->var[i].index;
1440 isl_int_gcd(m, vec->block.data[0], tab->mat->row[row][0]);
1441 isl_int_divexact(m, tab->mat->row[row][0], m);
1442 isl_seq_scale(vec->block.data, vec->block.data, m, 1 + i);
1443 isl_int_divexact(m, vec->block.data[0], tab->mat->row[row][0]);
1444 isl_int_mul(vec->block.data[1 + i], m, tab->mat->row[row][1]);
1446 isl_seq_normalize(vec->block.data, vec->size);
1448 isl_int_clear(m);
1449 return vec;
1452 /* Update "bmap" based on the results of the tableau "tab".
1453 * In particular, implicit equalities are made explicit, redundant constraints
1454 * are removed and if the sample value happens to be integer, it is stored
1455 * in "bmap" (unless "bmap" already had an integer sample).
1457 * The tableau is assumed to have been created from "bmap" using
1458 * isl_tab_from_basic_map.
1460 struct isl_basic_map *isl_basic_map_update_from_tab(struct isl_basic_map *bmap,
1461 struct isl_tab *tab)
1463 int i;
1464 unsigned n_eq;
1466 if (!bmap)
1467 return NULL;
1468 if (!tab)
1469 return bmap;
1471 n_eq = tab->n_eq;
1472 if (tab->empty)
1473 bmap = isl_basic_map_set_to_empty(bmap);
1474 else
1475 for (i = bmap->n_ineq - 1; i >= 0; --i) {
1476 if (isl_tab_is_equality(tab, n_eq + i))
1477 isl_basic_map_inequality_to_equality(bmap, i);
1478 else if (isl_tab_is_redundant(tab, n_eq + i))
1479 isl_basic_map_drop_inequality(bmap, i);
1481 if (!tab->rational &&
1482 !bmap->sample && isl_tab_sample_is_integer(tab))
1483 bmap->sample = extract_integer_sample(tab);
1484 return bmap;
1487 struct isl_basic_set *isl_basic_set_update_from_tab(struct isl_basic_set *bset,
1488 struct isl_tab *tab)
1490 return (struct isl_basic_set *)isl_basic_map_update_from_tab(
1491 (struct isl_basic_map *)bset, tab);
1494 /* Given a non-negative variable "var", add a new non-negative variable
1495 * that is the opposite of "var", ensuring that var can only attain the
1496 * value zero.
1497 * If var = n/d is a row variable, then the new variable = -n/d.
1498 * If var is a column variables, then the new variable = -var.
1499 * If the new variable cannot attain non-negative values, then
1500 * the resulting tableau is empty.
1501 * Otherwise, we know the value will be zero and we close the row.
1503 static struct isl_tab *cut_to_hyperplane(struct isl_tab *tab,
1504 struct isl_tab_var *var)
1506 unsigned r;
1507 isl_int *row;
1508 int sgn;
1509 unsigned off = 2 + tab->M;
1511 if (isl_tab_extend_cons(tab, 1) < 0)
1512 goto error;
1514 r = tab->n_con;
1515 tab->con[r].index = tab->n_row;
1516 tab->con[r].is_row = 1;
1517 tab->con[r].is_nonneg = 0;
1518 tab->con[r].is_zero = 0;
1519 tab->con[r].is_redundant = 0;
1520 tab->con[r].frozen = 0;
1521 tab->row_var[tab->n_row] = ~r;
1522 row = tab->mat->row[tab->n_row];
1524 if (var->is_row) {
1525 isl_int_set(row[0], tab->mat->row[var->index][0]);
1526 isl_seq_neg(row + 1,
1527 tab->mat->row[var->index] + 1, 1 + tab->n_col);
1528 } else {
1529 isl_int_set_si(row[0], 1);
1530 isl_seq_clr(row + 1, 1 + tab->n_col);
1531 isl_int_set_si(row[off + var->index], -1);
1534 tab->n_row++;
1535 tab->n_con++;
1536 isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->con[r]);
1538 sgn = sign_of_max(tab, &tab->con[r]);
1539 if (sgn < 0)
1540 return isl_tab_mark_empty(tab);
1541 tab->con[r].is_nonneg = 1;
1542 isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]);
1543 /* sgn == 0 */
1544 close_row(tab, &tab->con[r]);
1546 return tab;
1547 error:
1548 isl_tab_free(tab);
1549 return NULL;
1552 /* Given a tableau "tab" and an inequality constraint "con" of the tableau,
1553 * relax the inequality by one. That is, the inequality r >= 0 is replaced
1554 * by r' = r + 1 >= 0.
1555 * If r is a row variable, we simply increase the constant term by one
1556 * (taking into account the denominator).
1557 * If r is a column variable, then we need to modify each row that
1558 * refers to r = r' - 1 by substituting this equality, effectively
1559 * subtracting the coefficient of the column from the constant.
1561 struct isl_tab *isl_tab_relax(struct isl_tab *tab, int con)
1563 struct isl_tab_var *var;
1564 unsigned off = 2 + tab->M;
1566 if (!tab)
1567 return NULL;
1569 var = &tab->con[con];
1571 if (!var->is_row && !max_is_manifestly_unbounded(tab, var))
1572 to_row(tab, var, 1);
1574 if (var->is_row)
1575 isl_int_add(tab->mat->row[var->index][1],
1576 tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
1577 else {
1578 int i;
1580 for (i = 0; i < tab->n_row; ++i) {
1581 if (isl_int_is_zero(tab->mat->row[i][off + var->index]))
1582 continue;
1583 isl_int_sub(tab->mat->row[i][1], tab->mat->row[i][1],
1584 tab->mat->row[i][off + var->index]);
1589 isl_tab_push_var(tab, isl_tab_undo_relax, var);
1591 return tab;
1594 struct isl_tab *isl_tab_select_facet(struct isl_tab *tab, int con)
1596 if (!tab)
1597 return NULL;
1599 return cut_to_hyperplane(tab, &tab->con[con]);
1602 static int may_be_equality(struct isl_tab *tab, int row)
1604 unsigned off = 2 + tab->M;
1605 return (tab->rational ? isl_int_is_zero(tab->mat->row[row][1])
1606 : isl_int_lt(tab->mat->row[row][1],
1607 tab->mat->row[row][0])) &&
1608 isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
1609 tab->n_col - tab->n_dead) != -1;
1612 /* Check for (near) equalities among the constraints.
1613 * A constraint is an equality if it is non-negative and if
1614 * its maximal value is either
1615 * - zero (in case of rational tableaus), or
1616 * - strictly less than 1 (in case of integer tableaus)
1618 * We first mark all non-redundant and non-dead variables that
1619 * are not frozen and not obviously not an equality.
1620 * Then we iterate over all marked variables if they can attain
1621 * any values larger than zero or at least one.
1622 * If the maximal value is zero, we mark any column variables
1623 * that appear in the row as being zero and mark the row as being redundant.
1624 * Otherwise, if the maximal value is strictly less than one (and the
1625 * tableau is integer), then we restrict the value to being zero
1626 * by adding an opposite non-negative variable.
1628 struct isl_tab *isl_tab_detect_equalities(struct isl_tab *tab)
1630 int i;
1631 unsigned n_marked;
1633 if (!tab)
1634 return NULL;
1635 if (tab->empty)
1636 return tab;
1637 if (tab->n_dead == tab->n_col)
1638 return tab;
1640 n_marked = 0;
1641 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1642 struct isl_tab_var *var = isl_tab_var_from_row(tab, i);
1643 var->marked = !var->frozen && var->is_nonneg &&
1644 may_be_equality(tab, i);
1645 if (var->marked)
1646 n_marked++;
1648 for (i = tab->n_dead; i < tab->n_col; ++i) {
1649 struct isl_tab_var *var = var_from_col(tab, i);
1650 var->marked = !var->frozen && var->is_nonneg;
1651 if (var->marked)
1652 n_marked++;
1654 while (n_marked) {
1655 struct isl_tab_var *var;
1656 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1657 var = isl_tab_var_from_row(tab, i);
1658 if (var->marked)
1659 break;
1661 if (i == tab->n_row) {
1662 for (i = tab->n_dead; i < tab->n_col; ++i) {
1663 var = var_from_col(tab, i);
1664 if (var->marked)
1665 break;
1667 if (i == tab->n_col)
1668 break;
1670 var->marked = 0;
1671 n_marked--;
1672 if (sign_of_max(tab, var) == 0)
1673 close_row(tab, var);
1674 else if (!tab->rational && !at_least_one(tab, var)) {
1675 tab = cut_to_hyperplane(tab, var);
1676 return isl_tab_detect_equalities(tab);
1678 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1679 var = isl_tab_var_from_row(tab, i);
1680 if (!var->marked)
1681 continue;
1682 if (may_be_equality(tab, i))
1683 continue;
1684 var->marked = 0;
1685 n_marked--;
1689 return tab;
1692 /* Check for (near) redundant constraints.
1693 * A constraint is redundant if it is non-negative and if
1694 * its minimal value (temporarily ignoring the non-negativity) is either
1695 * - zero (in case of rational tableaus), or
1696 * - strictly larger than -1 (in case of integer tableaus)
1698 * We first mark all non-redundant and non-dead variables that
1699 * are not frozen and not obviously negatively unbounded.
1700 * Then we iterate over all marked variables if they can attain
1701 * any values smaller than zero or at most negative one.
1702 * If not, we mark the row as being redundant (assuming it hasn't
1703 * been detected as being obviously redundant in the mean time).
1705 struct isl_tab *isl_tab_detect_redundant(struct isl_tab *tab)
1707 int i;
1708 unsigned n_marked;
1710 if (!tab)
1711 return NULL;
1712 if (tab->empty)
1713 return tab;
1714 if (tab->n_redundant == tab->n_row)
1715 return tab;
1717 n_marked = 0;
1718 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1719 struct isl_tab_var *var = isl_tab_var_from_row(tab, i);
1720 var->marked = !var->frozen && var->is_nonneg;
1721 if (var->marked)
1722 n_marked++;
1724 for (i = tab->n_dead; i < tab->n_col; ++i) {
1725 struct isl_tab_var *var = var_from_col(tab, i);
1726 var->marked = !var->frozen && var->is_nonneg &&
1727 !min_is_manifestly_unbounded(tab, var);
1728 if (var->marked)
1729 n_marked++;
1731 while (n_marked) {
1732 struct isl_tab_var *var;
1733 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1734 var = isl_tab_var_from_row(tab, i);
1735 if (var->marked)
1736 break;
1738 if (i == tab->n_row) {
1739 for (i = tab->n_dead; i < tab->n_col; ++i) {
1740 var = var_from_col(tab, i);
1741 if (var->marked)
1742 break;
1744 if (i == tab->n_col)
1745 break;
1747 var->marked = 0;
1748 n_marked--;
1749 if ((tab->rational ? (sign_of_min(tab, var) >= 0)
1750 : !isl_tab_min_at_most_neg_one(tab, var)) &&
1751 !var->is_redundant)
1752 isl_tab_mark_redundant(tab, var->index);
1753 for (i = tab->n_dead; i < tab->n_col; ++i) {
1754 var = var_from_col(tab, i);
1755 if (!var->marked)
1756 continue;
1757 if (!min_is_manifestly_unbounded(tab, var))
1758 continue;
1759 var->marked = 0;
1760 n_marked--;
1764 return tab;
1767 int isl_tab_is_equality(struct isl_tab *tab, int con)
1769 int row;
1770 unsigned off;
1772 if (!tab)
1773 return -1;
1774 if (tab->con[con].is_zero)
1775 return 1;
1776 if (tab->con[con].is_redundant)
1777 return 0;
1778 if (!tab->con[con].is_row)
1779 return tab->con[con].index < tab->n_dead;
1781 row = tab->con[con].index;
1783 off = 2 + tab->M;
1784 return isl_int_is_zero(tab->mat->row[row][1]) &&
1785 isl_seq_first_non_zero(tab->mat->row[row] + 2 + tab->n_dead,
1786 tab->n_col - tab->n_dead) == -1;
1789 /* Return the minimial value of the affine expression "f" with denominator
1790 * "denom" in *opt, *opt_denom, assuming the tableau is not empty and
1791 * the expression cannot attain arbitrarily small values.
1792 * If opt_denom is NULL, then *opt is rounded up to the nearest integer.
1793 * The return value reflects the nature of the result (empty, unbounded,
1794 * minmimal value returned in *opt).
1796 enum isl_lp_result isl_tab_min(struct isl_tab *tab,
1797 isl_int *f, isl_int denom, isl_int *opt, isl_int *opt_denom,
1798 unsigned flags)
1800 int r;
1801 enum isl_lp_result res = isl_lp_ok;
1802 struct isl_tab_var *var;
1803 struct isl_tab_undo *snap;
1805 if (tab->empty)
1806 return isl_lp_empty;
1808 snap = isl_tab_snap(tab);
1809 r = isl_tab_add_row(tab, f);
1810 if (r < 0)
1811 return isl_lp_error;
1812 var = &tab->con[r];
1813 isl_int_mul(tab->mat->row[var->index][0],
1814 tab->mat->row[var->index][0], denom);
1815 for (;;) {
1816 int row, col;
1817 find_pivot(tab, var, var, -1, &row, &col);
1818 if (row == var->index) {
1819 res = isl_lp_unbounded;
1820 break;
1822 if (row == -1)
1823 break;
1824 isl_tab_pivot(tab, row, col);
1826 if (isl_tab_rollback(tab, snap) < 0)
1827 return isl_lp_error;
1828 if (ISL_FL_ISSET(flags, ISL_TAB_SAVE_DUAL)) {
1829 int i;
1831 isl_vec_free(tab->dual);
1832 tab->dual = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_con);
1833 if (!tab->dual)
1834 return isl_lp_error;
1835 isl_int_set(tab->dual->el[0], tab->mat->row[var->index][0]);
1836 for (i = 0; i < tab->n_con; ++i) {
1837 if (tab->con[i].is_row)
1838 isl_int_set_si(tab->dual->el[1 + i], 0);
1839 else {
1840 int pos = 2 + tab->con[i].index;
1841 isl_int_set(tab->dual->el[1 + i],
1842 tab->mat->row[var->index][pos]);
1846 if (res == isl_lp_ok) {
1847 if (opt_denom) {
1848 isl_int_set(*opt, tab->mat->row[var->index][1]);
1849 isl_int_set(*opt_denom, tab->mat->row[var->index][0]);
1850 } else
1851 isl_int_cdiv_q(*opt, tab->mat->row[var->index][1],
1852 tab->mat->row[var->index][0]);
1854 return res;
1857 int isl_tab_is_redundant(struct isl_tab *tab, int con)
1859 int row;
1860 unsigned n_col;
1862 if (!tab)
1863 return -1;
1864 if (tab->con[con].is_zero)
1865 return 0;
1866 if (tab->con[con].is_redundant)
1867 return 1;
1868 return tab->con[con].is_row && tab->con[con].index < tab->n_redundant;
1871 /* Take a snapshot of the tableau that can be restored by s call to
1872 * isl_tab_rollback.
1874 struct isl_tab_undo *isl_tab_snap(struct isl_tab *tab)
1876 if (!tab)
1877 return NULL;
1878 tab->need_undo = 1;
1879 return tab->top;
1882 /* Undo the operation performed by isl_tab_relax.
1884 static void unrelax(struct isl_tab *tab, struct isl_tab_var *var)
1886 unsigned off = 2 + tab->M;
1888 if (!var->is_row && !max_is_manifestly_unbounded(tab, var))
1889 to_row(tab, var, 1);
1891 if (var->is_row)
1892 isl_int_sub(tab->mat->row[var->index][1],
1893 tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
1894 else {
1895 int i;
1897 for (i = 0; i < tab->n_row; ++i) {
1898 if (isl_int_is_zero(tab->mat->row[i][off + var->index]))
1899 continue;
1900 isl_int_add(tab->mat->row[i][1], tab->mat->row[i][1],
1901 tab->mat->row[i][off + var->index]);
1907 static void perform_undo_var(struct isl_tab *tab, struct isl_tab_undo *undo)
1909 struct isl_tab_var *var = var_from_index(tab, undo->u.var_index);
1910 switch(undo->type) {
1911 case isl_tab_undo_nonneg:
1912 var->is_nonneg = 0;
1913 break;
1914 case isl_tab_undo_redundant:
1915 var->is_redundant = 0;
1916 tab->n_redundant--;
1917 break;
1918 case isl_tab_undo_zero:
1919 var->is_zero = 0;
1920 tab->n_dead--;
1921 break;
1922 case isl_tab_undo_allocate:
1923 if (undo->u.var_index >= 0) {
1924 isl_assert(tab->mat->ctx, !var->is_row, return);
1925 drop_col(tab, var->index);
1926 break;
1928 if (!var->is_row) {
1929 if (!max_is_manifestly_unbounded(tab, var))
1930 to_row(tab, var, 1);
1931 else if (!min_is_manifestly_unbounded(tab, var))
1932 to_row(tab, var, -1);
1933 else
1934 to_row(tab, var, 0);
1936 drop_row(tab, var->index);
1937 break;
1938 case isl_tab_undo_relax:
1939 unrelax(tab, var);
1940 break;
1944 /* Restore the tableau to the state where the basic variables
1945 * are those in "col_var".
1946 * We first construct a list of variables that are currently in
1947 * the basis, but shouldn't. Then we iterate over all variables
1948 * that should be in the basis and for each one that is currently
1949 * not in the basis, we exchange it with one of the elements of the
1950 * list constructed before.
1951 * We can always find an appropriate variable to pivot with because
1952 * the current basis is mapped to the old basis by a non-singular
1953 * matrix and so we can never end up with a zero row.
1955 static int restore_basis(struct isl_tab *tab, int *col_var)
1957 int i, j;
1958 int n_extra = 0;
1959 int *extra = NULL; /* current columns that contain bad stuff */
1960 unsigned off = 2 + tab->M;
1962 extra = isl_alloc_array(tab->mat->ctx, int, tab->n_col);
1963 if (!extra)
1964 goto error;
1965 for (i = 0; i < tab->n_col; ++i) {
1966 for (j = 0; j < tab->n_col; ++j)
1967 if (tab->col_var[i] == col_var[j])
1968 break;
1969 if (j < tab->n_col)
1970 continue;
1971 extra[n_extra++] = i;
1973 for (i = 0; i < tab->n_col && n_extra > 0; ++i) {
1974 struct isl_tab_var *var;
1975 int row;
1977 for (j = 0; j < tab->n_col; ++j)
1978 if (col_var[i] == tab->col_var[j])
1979 break;
1980 if (j < tab->n_col)
1981 continue;
1982 var = var_from_index(tab, col_var[i]);
1983 row = var->index;
1984 for (j = 0; j < n_extra; ++j)
1985 if (!isl_int_is_zero(tab->mat->row[row][off+extra[j]]))
1986 break;
1987 isl_assert(tab->mat->ctx, j < n_extra, goto error);
1988 isl_tab_pivot(tab, row, extra[j]);
1989 extra[j] = extra[--n_extra];
1992 free(extra);
1993 free(col_var);
1994 return 0;
1995 error:
1996 free(extra);
1997 free(col_var);
1998 return -1;
2001 static int perform_undo(struct isl_tab *tab, struct isl_tab_undo *undo)
2003 switch (undo->type) {
2004 case isl_tab_undo_empty:
2005 tab->empty = 0;
2006 break;
2007 case isl_tab_undo_nonneg:
2008 case isl_tab_undo_redundant:
2009 case isl_tab_undo_zero:
2010 case isl_tab_undo_allocate:
2011 case isl_tab_undo_relax:
2012 perform_undo_var(tab, undo);
2013 break;
2014 case isl_tab_undo_bset_eq:
2015 isl_basic_set_free_equality(tab->bset, 1);
2016 break;
2017 case isl_tab_undo_bset_ineq:
2018 isl_basic_set_free_inequality(tab->bset, 1);
2019 break;
2020 case isl_tab_undo_bset_div:
2021 isl_basic_set_free_div(tab->bset, 1);
2022 break;
2023 case isl_tab_undo_saved_basis:
2024 if (restore_basis(tab, undo->u.col_var) < 0)
2025 return -1;
2026 break;
2027 default:
2028 isl_assert(tab->mat->ctx, 0, return -1);
2030 return 0;
2033 /* Return the tableau to the state it was in when the snapshot "snap"
2034 * was taken.
2036 int isl_tab_rollback(struct isl_tab *tab, struct isl_tab_undo *snap)
2038 struct isl_tab_undo *undo, *next;
2040 if (!tab)
2041 return -1;
2043 tab->in_undo = 1;
2044 for (undo = tab->top; undo && undo != &tab->bottom; undo = next) {
2045 next = undo->next;
2046 if (undo == snap)
2047 break;
2048 if (perform_undo(tab, undo) < 0) {
2049 free_undo(tab);
2050 tab->in_undo = 0;
2051 return -1;
2053 free(undo);
2055 tab->in_undo = 0;
2056 tab->top = undo;
2057 if (!undo)
2058 return -1;
2059 return 0;
2062 /* The given row "row" represents an inequality violated by all
2063 * points in the tableau. Check for some special cases of such
2064 * separating constraints.
2065 * In particular, if the row has been reduced to the constant -1,
2066 * then we know the inequality is adjacent (but opposite) to
2067 * an equality in the tableau.
2068 * If the row has been reduced to r = -1 -r', with r' an inequality
2069 * of the tableau, then the inequality is adjacent (but opposite)
2070 * to the inequality r'.
2072 static enum isl_ineq_type separation_type(struct isl_tab *tab, unsigned row)
2074 int pos;
2075 unsigned off = 2 + tab->M;
2077 if (tab->rational)
2078 return isl_ineq_separate;
2080 if (!isl_int_is_one(tab->mat->row[row][0]))
2081 return isl_ineq_separate;
2082 if (!isl_int_is_negone(tab->mat->row[row][1]))
2083 return isl_ineq_separate;
2085 pos = isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
2086 tab->n_col - tab->n_dead);
2087 if (pos == -1)
2088 return isl_ineq_adj_eq;
2090 if (!isl_int_is_negone(tab->mat->row[row][off + tab->n_dead + pos]))
2091 return isl_ineq_separate;
2093 pos = isl_seq_first_non_zero(
2094 tab->mat->row[row] + off + tab->n_dead + pos + 1,
2095 tab->n_col - tab->n_dead - pos - 1);
2097 return pos == -1 ? isl_ineq_adj_ineq : isl_ineq_separate;
2100 /* Check the effect of inequality "ineq" on the tableau "tab".
2101 * The result may be
2102 * isl_ineq_redundant: satisfied by all points in the tableau
2103 * isl_ineq_separate: satisfied by no point in the tableau
2104 * isl_ineq_cut: satisfied by some by not all points
2105 * isl_ineq_adj_eq: adjacent to an equality
2106 * isl_ineq_adj_ineq: adjacent to an inequality.
2108 enum isl_ineq_type isl_tab_ineq_type(struct isl_tab *tab, isl_int *ineq)
2110 enum isl_ineq_type type = isl_ineq_error;
2111 struct isl_tab_undo *snap = NULL;
2112 int con;
2113 int row;
2115 if (!tab)
2116 return isl_ineq_error;
2118 if (isl_tab_extend_cons(tab, 1) < 0)
2119 return isl_ineq_error;
2121 snap = isl_tab_snap(tab);
2123 con = isl_tab_add_row(tab, ineq);
2124 if (con < 0)
2125 goto error;
2127 row = tab->con[con].index;
2128 if (isl_tab_row_is_redundant(tab, row))
2129 type = isl_ineq_redundant;
2130 else if (isl_int_is_neg(tab->mat->row[row][1]) &&
2131 (tab->rational ||
2132 isl_int_abs_ge(tab->mat->row[row][1],
2133 tab->mat->row[row][0]))) {
2134 if (at_least_zero(tab, &tab->con[con]))
2135 type = isl_ineq_cut;
2136 else
2137 type = separation_type(tab, row);
2138 } else if (tab->rational ? (sign_of_min(tab, &tab->con[con]) < 0)
2139 : isl_tab_min_at_most_neg_one(tab, &tab->con[con]))
2140 type = isl_ineq_cut;
2141 else
2142 type = isl_ineq_redundant;
2144 if (isl_tab_rollback(tab, snap))
2145 return isl_ineq_error;
2146 return type;
2147 error:
2148 isl_tab_rollback(tab, snap);
2149 return isl_ineq_error;
2152 void isl_tab_dump(struct isl_tab *tab, FILE *out, int indent)
2154 unsigned r, c;
2155 int i;
2157 if (!tab) {
2158 fprintf(out, "%*snull tab\n", indent, "");
2159 return;
2161 fprintf(out, "%*sn_redundant: %d, n_dead: %d", indent, "",
2162 tab->n_redundant, tab->n_dead);
2163 if (tab->rational)
2164 fprintf(out, ", rational");
2165 if (tab->empty)
2166 fprintf(out, ", empty");
2167 fprintf(out, "\n");
2168 fprintf(out, "%*s[", indent, "");
2169 for (i = 0; i < tab->n_var; ++i) {
2170 if (i)
2171 fprintf(out, (i == tab->n_param ||
2172 i == tab->n_var - tab->n_div) ? "; "
2173 : ", ");
2174 fprintf(out, "%c%d%s", tab->var[i].is_row ? 'r' : 'c',
2175 tab->var[i].index,
2176 tab->var[i].is_zero ? " [=0]" :
2177 tab->var[i].is_redundant ? " [R]" : "");
2179 fprintf(out, "]\n");
2180 fprintf(out, "%*s[", indent, "");
2181 for (i = 0; i < tab->n_con; ++i) {
2182 if (i)
2183 fprintf(out, ", ");
2184 fprintf(out, "%c%d%s", tab->con[i].is_row ? 'r' : 'c',
2185 tab->con[i].index,
2186 tab->con[i].is_zero ? " [=0]" :
2187 tab->con[i].is_redundant ? " [R]" : "");
2189 fprintf(out, "]\n");
2190 fprintf(out, "%*s[", indent, "");
2191 for (i = 0; i < tab->n_row; ++i) {
2192 if (i)
2193 fprintf(out, ", ");
2194 fprintf(out, "r%d: %d%s", i, tab->row_var[i],
2195 isl_tab_var_from_row(tab, i)->is_nonneg ? " [>=0]" : "");
2197 fprintf(out, "]\n");
2198 fprintf(out, "%*s[", indent, "");
2199 for (i = 0; i < tab->n_col; ++i) {
2200 if (i)
2201 fprintf(out, ", ");
2202 fprintf(out, "c%d: %d%s", i, tab->col_var[i],
2203 var_from_col(tab, i)->is_nonneg ? " [>=0]" : "");
2205 fprintf(out, "]\n");
2206 r = tab->mat->n_row;
2207 tab->mat->n_row = tab->n_row;
2208 c = tab->mat->n_col;
2209 tab->mat->n_col = 2 + tab->M + tab->n_col;
2210 isl_mat_dump(tab->mat, out, indent);
2211 tab->mat->n_row = r;
2212 tab->mat->n_col = c;
2213 if (tab->bset)
2214 isl_basic_set_dump(tab->bset, out, indent);