isl_tab: introduce support for "big parameters"
[isl.git] / isl_tab.c
blobc3d03fee540d989453115d116143ad2e0ba87882
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 free(tab->var);
161 free(tab->con);
162 free(tab->row_var);
163 free(tab->col_var);
164 free(tab);
167 struct isl_tab *isl_tab_dup(struct isl_tab *tab)
169 int i;
170 struct isl_tab *dup;
172 if (!tab)
173 return NULL;
175 dup = isl_calloc_type(tab->ctx, struct isl_tab);
176 if (!dup)
177 return NULL;
178 dup->mat = isl_mat_dup(tab->mat);
179 if (!dup->mat)
180 goto error;
181 dup->var = isl_alloc_array(tab->ctx, struct isl_tab_var, tab->max_var);
182 if (!dup->var)
183 goto error;
184 for (i = 0; i < tab->n_var; ++i)
185 dup->var[i] = tab->var[i];
186 dup->con = isl_alloc_array(tab->ctx, struct isl_tab_var, tab->max_con);
187 if (!dup->con)
188 goto error;
189 for (i = 0; i < tab->n_con; ++i)
190 dup->con[i] = tab->con[i];
191 dup->col_var = isl_alloc_array(tab->ctx, int, tab->mat->n_col);
192 if (!dup->col_var)
193 goto error;
194 for (i = 0; i < tab->n_var; ++i)
195 dup->col_var[i] = tab->col_var[i];
196 dup->row_var = isl_alloc_array(tab->ctx, int, tab->mat->n_row);
197 if (!dup->row_var)
198 goto error;
199 for (i = 0; i < tab->n_row; ++i)
200 dup->row_var[i] = tab->row_var[i];
201 dup->n_row = tab->n_row;
202 dup->n_con = tab->n_con;
203 dup->n_eq = tab->n_eq;
204 dup->max_con = tab->max_con;
205 dup->n_col = tab->n_col;
206 dup->n_var = tab->n_var;
207 dup->max_var = tab->max_var;
208 dup->n_param = tab->n_param;
209 dup->n_div = tab->n_div;
210 dup->n_dead = tab->n_dead;
211 dup->n_redundant = tab->n_redundant;
212 dup->rational = tab->rational;
213 dup->empty = tab->empty;
214 dup->need_undo = 0;
215 dup->in_undo = 0;
216 dup->M = tab->M;
217 dup->bottom.type = isl_tab_undo_bottom;
218 dup->bottom.next = NULL;
219 dup->top = &dup->bottom;
220 return dup;
221 error:
222 isl_tab_free(dup);
223 return NULL;
226 static struct isl_tab_var *var_from_index(struct isl_tab *tab, int i)
228 if (i >= 0)
229 return &tab->var[i];
230 else
231 return &tab->con[~i];
234 struct isl_tab_var *isl_tab_var_from_row(struct isl_tab *tab, int i)
236 return var_from_index(tab, tab->row_var[i]);
239 static struct isl_tab_var *var_from_col(struct isl_tab *tab, int i)
241 return var_from_index(tab, tab->col_var[i]);
244 /* Check if there are any upper bounds on column variable "var",
245 * i.e., non-negative rows where var appears with a negative coefficient.
246 * Return 1 if there are no such bounds.
248 static int max_is_manifestly_unbounded(struct isl_tab *tab,
249 struct isl_tab_var *var)
251 int i;
252 unsigned off = 2 + tab->M;
254 if (var->is_row)
255 return 0;
256 for (i = tab->n_redundant; i < tab->n_row; ++i) {
257 if (!isl_int_is_neg(tab->mat->row[i][off + var->index]))
258 continue;
259 if (isl_tab_var_from_row(tab, i)->is_nonneg)
260 return 0;
262 return 1;
265 /* Check if there are any lower bounds on column variable "var",
266 * i.e., non-negative rows where var appears with a positive coefficient.
267 * Return 1 if there are no such bounds.
269 static int min_is_manifestly_unbounded(struct isl_tab *tab,
270 struct isl_tab_var *var)
272 int i;
273 unsigned off = 2 + tab->M;
275 if (var->is_row)
276 return 0;
277 for (i = tab->n_redundant; i < tab->n_row; ++i) {
278 if (!isl_int_is_pos(tab->mat->row[i][off + var->index]))
279 continue;
280 if (isl_tab_var_from_row(tab, i)->is_nonneg)
281 return 0;
283 return 1;
286 static int row_cmp(struct isl_tab *tab, int r1, int r2, int c, isl_int t)
288 unsigned off = 2 + tab->M;
290 if (tab->M) {
291 int s;
292 isl_int_mul(t, tab->mat->row[r1][2], tab->mat->row[r2][off+c]);
293 isl_int_submul(t, tab->mat->row[r2][2], tab->mat->row[r1][off+c]);
294 s = isl_int_sgn(t);
295 if (s)
296 return s;
298 isl_int_mul(t, tab->mat->row[r1][1], tab->mat->row[r2][off + c]);
299 isl_int_submul(t, tab->mat->row[r2][1], tab->mat->row[r1][off + c]);
300 return isl_int_sgn(t);
303 /* Given the index of a column "c", return the index of a row
304 * that can be used to pivot the column in, with either an increase
305 * (sgn > 0) or a decrease (sgn < 0) of the corresponding variable.
306 * If "var" is not NULL, then the row returned will be different from
307 * the one associated with "var".
309 * Each row in the tableau is of the form
311 * x_r = a_r0 + \sum_i a_ri x_i
313 * Only rows with x_r >= 0 and with the sign of a_ri opposite to "sgn"
314 * impose any limit on the increase or decrease in the value of x_c
315 * and this bound is equal to a_r0 / |a_rc|. We are therefore looking
316 * for the row with the smallest (most stringent) such bound.
317 * Note that the common denominator of each row drops out of the fraction.
318 * To check if row j has a smaller bound than row r, i.e.,
319 * a_j0 / |a_jc| < a_r0 / |a_rc| or a_j0 |a_rc| < a_r0 |a_jc|,
320 * we check if -sign(a_jc) (a_j0 a_rc - a_r0 a_jc) < 0,
321 * where -sign(a_jc) is equal to "sgn".
323 static int pivot_row(struct isl_tab *tab,
324 struct isl_tab_var *var, int sgn, int c)
326 int j, r, tsgn;
327 isl_int t;
328 unsigned off = 2 + tab->M;
330 isl_int_init(t);
331 r = -1;
332 for (j = tab->n_redundant; j < tab->n_row; ++j) {
333 if (var && j == var->index)
334 continue;
335 if (!isl_tab_var_from_row(tab, j)->is_nonneg)
336 continue;
337 if (sgn * isl_int_sgn(tab->mat->row[j][off + c]) >= 0)
338 continue;
339 if (r < 0) {
340 r = j;
341 continue;
343 tsgn = sgn * row_cmp(tab, r, j, c, t);
344 if (tsgn < 0 || (tsgn == 0 &&
345 tab->row_var[j] < tab->row_var[r]))
346 r = j;
348 isl_int_clear(t);
349 return r;
352 /* Find a pivot (row and col) that will increase (sgn > 0) or decrease
353 * (sgn < 0) the value of row variable var.
354 * If not NULL, then skip_var is a row variable that should be ignored
355 * while looking for a pivot row. It is usually equal to var.
357 * As the given row in the tableau is of the form
359 * x_r = a_r0 + \sum_i a_ri x_i
361 * we need to find a column such that the sign of a_ri is equal to "sgn"
362 * (such that an increase in x_i will have the desired effect) or a
363 * column with a variable that may attain negative values.
364 * If a_ri is positive, then we need to move x_i in the same direction
365 * to obtain the desired effect. Otherwise, x_i has to move in the
366 * opposite direction.
368 static void find_pivot(struct isl_tab *tab,
369 struct isl_tab_var *var, struct isl_tab_var *skip_var,
370 int sgn, int *row, int *col)
372 int j, r, c;
373 isl_int *tr;
375 *row = *col = -1;
377 isl_assert(tab->mat->ctx, var->is_row, return);
378 tr = tab->mat->row[var->index] + 2 + tab->M;
380 c = -1;
381 for (j = tab->n_dead; j < tab->n_col; ++j) {
382 if (isl_int_is_zero(tr[j]))
383 continue;
384 if (isl_int_sgn(tr[j]) != sgn &&
385 var_from_col(tab, j)->is_nonneg)
386 continue;
387 if (c < 0 || tab->col_var[j] < tab->col_var[c])
388 c = j;
390 if (c < 0)
391 return;
393 sgn *= isl_int_sgn(tr[c]);
394 r = pivot_row(tab, skip_var, sgn, c);
395 *row = r < 0 ? var->index : r;
396 *col = c;
399 /* Return 1 if row "row" represents an obviously redundant inequality.
400 * This means
401 * - it represents an inequality or a variable
402 * - that is the sum of a non-negative sample value and a positive
403 * combination of zero or more non-negative variables.
405 int isl_tab_row_is_redundant(struct isl_tab *tab, int row)
407 int i;
408 unsigned off = 2 + tab->M;
410 if (tab->row_var[row] < 0 && !isl_tab_var_from_row(tab, row)->is_nonneg)
411 return 0;
413 if (isl_int_is_neg(tab->mat->row[row][1]))
414 return 0;
415 if (tab->M && isl_int_is_neg(tab->mat->row[row][2]))
416 return 0;
418 for (i = tab->n_dead; i < tab->n_col; ++i) {
419 if (isl_int_is_zero(tab->mat->row[row][off + i]))
420 continue;
421 if (isl_int_is_neg(tab->mat->row[row][off + i]))
422 return 0;
423 if (!var_from_col(tab, i)->is_nonneg)
424 return 0;
426 return 1;
429 static void swap_rows(struct isl_tab *tab, int row1, int row2)
431 int t;
432 t = tab->row_var[row1];
433 tab->row_var[row1] = tab->row_var[row2];
434 tab->row_var[row2] = t;
435 isl_tab_var_from_row(tab, row1)->index = row1;
436 isl_tab_var_from_row(tab, row2)->index = row2;
437 tab->mat = isl_mat_swap_rows(tab->mat, row1, row2);
440 static void push_union(struct isl_tab *tab,
441 enum isl_tab_undo_type type, union isl_tab_undo_val u)
443 struct isl_tab_undo *undo;
445 if (!tab->need_undo)
446 return;
448 undo = isl_alloc_type(tab->mat->ctx, struct isl_tab_undo);
449 if (!undo) {
450 free_undo(tab);
451 tab->top = NULL;
452 return;
454 undo->type = type;
455 undo->u = u;
456 undo->next = tab->top;
457 tab->top = undo;
460 void isl_tab_push_var(struct isl_tab *tab,
461 enum isl_tab_undo_type type, struct isl_tab_var *var)
463 union isl_tab_undo_val u;
464 if (var->is_row)
465 u.var_index = tab->row_var[var->index];
466 else
467 u.var_index = tab->col_var[var->index];
468 push_union(tab, type, u);
471 void isl_tab_push(struct isl_tab *tab, enum isl_tab_undo_type type)
473 union isl_tab_undo_val u = { 0 };
474 push_union(tab, type, u);
477 /* Push a record on the undo stack describing the current basic
478 * variables, so that the this state can be restored during rollback.
480 void isl_tab_push_basis(struct isl_tab *tab)
482 int i;
483 union isl_tab_undo_val u;
485 u.col_var = isl_alloc_array(tab->mat->ctx, int, tab->n_col);
486 if (!u.col_var) {
487 free_undo(tab);
488 tab->top = NULL;
489 return;
491 for (i = 0; i < tab->n_col; ++i)
492 u.col_var[i] = tab->col_var[i];
493 push_union(tab, isl_tab_undo_saved_basis, u);
496 /* Mark row with index "row" as being redundant.
497 * If we may need to undo the operation or if the row represents
498 * a variable of the original problem, the row is kept,
499 * but no longer considered when looking for a pivot row.
500 * Otherwise, the row is simply removed.
502 * The row may be interchanged with some other row. If it
503 * is interchanged with a later row, return 1. Otherwise return 0.
504 * If the rows are checked in order in the calling function,
505 * then a return value of 1 means that the row with the given
506 * row number may now contain a different row that hasn't been checked yet.
508 int isl_tab_mark_redundant(struct isl_tab *tab, int row)
510 struct isl_tab_var *var = isl_tab_var_from_row(tab, row);
511 var->is_redundant = 1;
512 isl_assert(tab->mat->ctx, row >= tab->n_redundant, return);
513 if (tab->need_undo || tab->row_var[row] >= 0) {
514 if (tab->row_var[row] >= 0 && !var->is_nonneg) {
515 var->is_nonneg = 1;
516 isl_tab_push_var(tab, isl_tab_undo_nonneg, var);
518 if (row != tab->n_redundant)
519 swap_rows(tab, row, tab->n_redundant);
520 isl_tab_push_var(tab, isl_tab_undo_redundant, var);
521 tab->n_redundant++;
522 return 0;
523 } else {
524 if (row != tab->n_row - 1)
525 swap_rows(tab, row, tab->n_row - 1);
526 isl_tab_var_from_row(tab, tab->n_row - 1)->index = -1;
527 tab->n_row--;
528 return 1;
532 struct isl_tab *isl_tab_mark_empty(struct isl_tab *tab)
534 if (!tab->empty && tab->need_undo)
535 isl_tab_push(tab, isl_tab_undo_empty);
536 tab->empty = 1;
537 return tab;
540 /* Given a row number "row" and a column number "col", pivot the tableau
541 * such that the associated variables are interchanged.
542 * The given row in the tableau expresses
544 * x_r = a_r0 + \sum_i a_ri x_i
546 * or
548 * x_c = 1/a_rc x_r - a_r0/a_rc + sum_{i \ne r} -a_ri/a_rc
550 * Substituting this equality into the other rows
552 * x_j = a_j0 + \sum_i a_ji x_i
554 * with a_jc \ne 0, we obtain
556 * 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
558 * The tableau
560 * n_rc/d_r n_ri/d_r
561 * n_jc/d_j n_ji/d_j
563 * where i is any other column and j is any other row,
564 * is therefore transformed into
566 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
567 * 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)
569 * The transformation is performed along the following steps
571 * d_r/n_rc n_ri/n_rc
572 * n_jc/d_j n_ji/d_j
574 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
575 * n_jc/d_j n_ji/d_j
577 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
578 * n_jc/(|n_rc| d_j) n_ji/(|n_rc| d_j)
580 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
581 * n_jc/(|n_rc| d_j) (n_ji |n_rc|)/(|n_rc| d_j)
583 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
584 * n_jc/(|n_rc| d_j) (n_ji |n_rc| - s(n_rc)n_jc n_ri)/(|n_rc| d_j)
586 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
587 * 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)
590 void isl_tab_pivot(struct isl_tab *tab, int row, int col)
592 int i, j;
593 int sgn;
594 int t;
595 struct isl_mat *mat = tab->mat;
596 struct isl_tab_var *var;
597 unsigned off = 2 + tab->M;
599 isl_int_swap(mat->row[row][0], mat->row[row][off + col]);
600 sgn = isl_int_sgn(mat->row[row][0]);
601 if (sgn < 0) {
602 isl_int_neg(mat->row[row][0], mat->row[row][0]);
603 isl_int_neg(mat->row[row][off + col], mat->row[row][off + col]);
604 } else
605 for (j = 0; j < off - 1 + tab->n_col; ++j) {
606 if (j == off - 1 + col)
607 continue;
608 isl_int_neg(mat->row[row][1 + j], mat->row[row][1 + j]);
610 if (!isl_int_is_one(mat->row[row][0]))
611 isl_seq_normalize(mat->row[row], off + tab->n_col);
612 for (i = 0; i < tab->n_row; ++i) {
613 if (i == row)
614 continue;
615 if (isl_int_is_zero(mat->row[i][off + col]))
616 continue;
617 isl_int_mul(mat->row[i][0], mat->row[i][0], mat->row[row][0]);
618 for (j = 0; j < off - 1 + tab->n_col; ++j) {
619 if (j == off - 1 + col)
620 continue;
621 isl_int_mul(mat->row[i][1 + j],
622 mat->row[i][1 + j], mat->row[row][0]);
623 isl_int_addmul(mat->row[i][1 + j],
624 mat->row[i][off + col], mat->row[row][1 + j]);
626 isl_int_mul(mat->row[i][off + col],
627 mat->row[i][off + col], mat->row[row][off + col]);
628 if (!isl_int_is_one(mat->row[i][0]))
629 isl_seq_normalize(mat->row[i], off + tab->n_col);
631 t = tab->row_var[row];
632 tab->row_var[row] = tab->col_var[col];
633 tab->col_var[col] = t;
634 var = isl_tab_var_from_row(tab, row);
635 var->is_row = 1;
636 var->index = row;
637 var = var_from_col(tab, col);
638 var->is_row = 0;
639 var->index = col;
640 if (tab->in_undo)
641 return;
642 for (i = tab->n_redundant; i < tab->n_row; ++i) {
643 if (isl_int_is_zero(mat->row[i][off + col]))
644 continue;
645 if (!isl_tab_var_from_row(tab, i)->frozen &&
646 isl_tab_row_is_redundant(tab, i))
647 if (isl_tab_mark_redundant(tab, i))
648 --i;
652 /* If "var" represents a column variable, then pivot is up (sgn > 0)
653 * or down (sgn < 0) to a row. The variable is assumed not to be
654 * unbounded in the specified direction.
655 * If sgn = 0, then the variable is unbounded in both directions,
656 * and we pivot with any row we can find.
658 static void to_row(struct isl_tab *tab, struct isl_tab_var *var, int sign)
660 int r;
661 unsigned off = 2 + tab->M;
663 if (var->is_row)
664 return;
666 if (sign == 0) {
667 for (r = tab->n_redundant; r < tab->n_row; ++r)
668 if (!isl_int_is_zero(tab->mat->row[r][off+var->index]))
669 break;
670 isl_assert(tab->mat->ctx, r < tab->n_row, return);
671 } else {
672 r = pivot_row(tab, NULL, sign, var->index);
673 isl_assert(tab->mat->ctx, r >= 0, return);
676 isl_tab_pivot(tab, r, var->index);
679 static void check_table(struct isl_tab *tab)
681 int i;
683 if (tab->empty)
684 return;
685 for (i = 0; i < tab->n_row; ++i) {
686 if (!isl_tab_var_from_row(tab, i)->is_nonneg)
687 continue;
688 assert(!isl_int_is_neg(tab->mat->row[i][1]));
692 /* Return the sign of the maximal value of "var".
693 * If the sign is not negative, then on return from this function,
694 * the sample value will also be non-negative.
696 * If "var" is manifestly unbounded wrt positive values, we are done.
697 * Otherwise, we pivot the variable up to a row if needed
698 * Then we continue pivoting down until either
699 * - no more down pivots can be performed
700 * - the sample value is positive
701 * - the variable is pivoted into a manifestly unbounded column
703 static int sign_of_max(struct isl_tab *tab, struct isl_tab_var *var)
705 int row, col;
707 if (max_is_manifestly_unbounded(tab, var))
708 return 1;
709 to_row(tab, var, 1);
710 while (!isl_int_is_pos(tab->mat->row[var->index][1])) {
711 find_pivot(tab, var, var, 1, &row, &col);
712 if (row == -1)
713 return isl_int_sgn(tab->mat->row[var->index][1]);
714 isl_tab_pivot(tab, row, col);
715 if (!var->is_row) /* manifestly unbounded */
716 return 1;
718 return 1;
721 static int row_is_neg(struct isl_tab *tab, int row)
723 if (!tab->M)
724 return isl_int_is_neg(tab->mat->row[row][1]);
725 if (isl_int_is_pos(tab->mat->row[row][2]))
726 return 0;
727 if (isl_int_is_neg(tab->mat->row[row][2]))
728 return 1;
729 return isl_int_is_neg(tab->mat->row[row][1]);
732 static int row_sgn(struct isl_tab *tab, int row)
734 if (!tab->M)
735 return isl_int_sgn(tab->mat->row[row][1]);
736 if (!isl_int_is_zero(tab->mat->row[row][2]))
737 return isl_int_sgn(tab->mat->row[row][2]);
738 else
739 return isl_int_sgn(tab->mat->row[row][1]);
742 /* Perform pivots until the row variable "var" has a non-negative
743 * sample value or until no more upward pivots can be performed.
744 * Return the sign of the sample value after the pivots have been
745 * performed.
747 static int restore_row(struct isl_tab *tab, struct isl_tab_var *var)
749 int row, col;
751 while (row_is_neg(tab, var->index)) {
752 find_pivot(tab, var, var, 1, &row, &col);
753 if (row == -1)
754 break;
755 isl_tab_pivot(tab, row, col);
756 if (!var->is_row) /* manifestly unbounded */
757 return 1;
759 return row_sgn(tab, var->index);
762 /* Perform pivots until we are sure that the row variable "var"
763 * can attain non-negative values. After return from this
764 * function, "var" is still a row variable, but its sample
765 * value may not be non-negative, even if the function returns 1.
767 static int at_least_zero(struct isl_tab *tab, struct isl_tab_var *var)
769 int row, col;
771 while (isl_int_is_neg(tab->mat->row[var->index][1])) {
772 find_pivot(tab, var, var, 1, &row, &col);
773 if (row == -1)
774 break;
775 if (row == var->index) /* manifestly unbounded */
776 return 1;
777 isl_tab_pivot(tab, row, col);
779 return !isl_int_is_neg(tab->mat->row[var->index][1]);
782 /* Return a negative value if "var" can attain negative values.
783 * Return a non-negative value otherwise.
785 * If "var" is manifestly unbounded wrt negative values, we are done.
786 * Otherwise, if var is in a column, we can pivot it down to a row.
787 * Then we continue pivoting down until either
788 * - the pivot would result in a manifestly unbounded column
789 * => we don't perform the pivot, but simply return -1
790 * - no more down pivots can be performed
791 * - the sample value is negative
792 * If the sample value becomes negative and the variable is supposed
793 * to be nonnegative, then we undo the last pivot.
794 * However, if the last pivot has made the pivoting variable
795 * obviously redundant, then it may have moved to another row.
796 * In that case we look for upward pivots until we reach a non-negative
797 * value again.
799 static int sign_of_min(struct isl_tab *tab, struct isl_tab_var *var)
801 int row, col;
802 struct isl_tab_var *pivot_var;
804 if (min_is_manifestly_unbounded(tab, var))
805 return -1;
806 if (!var->is_row) {
807 col = var->index;
808 row = pivot_row(tab, NULL, -1, col);
809 pivot_var = var_from_col(tab, col);
810 isl_tab_pivot(tab, row, col);
811 if (var->is_redundant)
812 return 0;
813 if (isl_int_is_neg(tab->mat->row[var->index][1])) {
814 if (var->is_nonneg) {
815 if (!pivot_var->is_redundant &&
816 pivot_var->index == row)
817 isl_tab_pivot(tab, row, col);
818 else
819 restore_row(tab, var);
821 return -1;
824 if (var->is_redundant)
825 return 0;
826 while (!isl_int_is_neg(tab->mat->row[var->index][1])) {
827 find_pivot(tab, var, var, -1, &row, &col);
828 if (row == var->index)
829 return -1;
830 if (row == -1)
831 return isl_int_sgn(tab->mat->row[var->index][1]);
832 pivot_var = var_from_col(tab, col);
833 isl_tab_pivot(tab, row, col);
834 if (var->is_redundant)
835 return 0;
837 if (var->is_nonneg) {
838 /* pivot back to non-negative value */
839 if (!pivot_var->is_redundant && pivot_var->index == row)
840 isl_tab_pivot(tab, row, col);
841 else
842 restore_row(tab, var);
844 return -1;
847 static int row_at_most_neg_one(struct isl_tab *tab, int row)
849 if (tab->M) {
850 if (isl_int_is_pos(tab->mat->row[row][2]))
851 return 0;
852 if (isl_int_is_neg(tab->mat->row[row][2]))
853 return 1;
855 return isl_int_is_neg(tab->mat->row[row][1]) &&
856 isl_int_abs_ge(tab->mat->row[row][1],
857 tab->mat->row[row][0]);
860 /* Return 1 if "var" can attain values <= -1.
861 * Return 0 otherwise.
863 * The sample value of "var" is assumed to be non-negative when the
864 * the function is called and will be made non-negative again before
865 * the function returns.
867 int isl_tab_min_at_most_neg_one(struct isl_tab *tab, struct isl_tab_var *var)
869 int row, col;
870 struct isl_tab_var *pivot_var;
872 if (min_is_manifestly_unbounded(tab, var))
873 return 1;
874 if (!var->is_row) {
875 col = var->index;
876 row = pivot_row(tab, NULL, -1, col);
877 pivot_var = var_from_col(tab, col);
878 isl_tab_pivot(tab, row, col);
879 if (var->is_redundant)
880 return 0;
881 if (row_at_most_neg_one(tab, var->index)) {
882 if (var->is_nonneg) {
883 if (!pivot_var->is_redundant &&
884 pivot_var->index == row)
885 isl_tab_pivot(tab, row, col);
886 else
887 restore_row(tab, var);
889 return 1;
892 if (var->is_redundant)
893 return 0;
894 do {
895 find_pivot(tab, var, var, -1, &row, &col);
896 if (row == var->index)
897 return 1;
898 if (row == -1)
899 return 0;
900 pivot_var = var_from_col(tab, col);
901 isl_tab_pivot(tab, row, col);
902 if (var->is_redundant)
903 return 0;
904 } while (!row_at_most_neg_one(tab, var->index));
905 if (var->is_nonneg) {
906 /* pivot back to non-negative value */
907 if (!pivot_var->is_redundant && pivot_var->index == row)
908 isl_tab_pivot(tab, row, col);
909 restore_row(tab, var);
911 return 1;
914 /* Return 1 if "var" can attain values >= 1.
915 * Return 0 otherwise.
917 static int at_least_one(struct isl_tab *tab, struct isl_tab_var *var)
919 int row, col;
920 isl_int *r;
922 if (max_is_manifestly_unbounded(tab, var))
923 return 1;
924 to_row(tab, var, 1);
925 r = tab->mat->row[var->index];
926 while (isl_int_lt(r[1], r[0])) {
927 find_pivot(tab, var, var, 1, &row, &col);
928 if (row == -1)
929 return isl_int_ge(r[1], r[0]);
930 if (row == var->index) /* manifestly unbounded */
931 return 1;
932 isl_tab_pivot(tab, row, col);
934 return 1;
937 static void swap_cols(struct isl_tab *tab, int col1, int col2)
939 int t;
940 unsigned off = 2 + tab->M;
941 t = tab->col_var[col1];
942 tab->col_var[col1] = tab->col_var[col2];
943 tab->col_var[col2] = t;
944 var_from_col(tab, col1)->index = col1;
945 var_from_col(tab, col2)->index = col2;
946 tab->mat = isl_mat_swap_cols(tab->mat, off + col1, off + col2);
949 /* Mark column with index "col" as representing a zero variable.
950 * If we may need to undo the operation the column is kept,
951 * but no longer considered.
952 * Otherwise, the column is simply removed.
954 * The column may be interchanged with some other column. If it
955 * is interchanged with a later column, return 1. Otherwise return 0.
956 * If the columns are checked in order in the calling function,
957 * then a return value of 1 means that the column with the given
958 * column number may now contain a different column that
959 * hasn't been checked yet.
961 int isl_tab_kill_col(struct isl_tab *tab, int col)
963 var_from_col(tab, col)->is_zero = 1;
964 if (tab->need_undo) {
965 isl_tab_push_var(tab, isl_tab_undo_zero, var_from_col(tab, col));
966 if (col != tab->n_dead)
967 swap_cols(tab, col, tab->n_dead);
968 tab->n_dead++;
969 return 0;
970 } else {
971 if (col != tab->n_col - 1)
972 swap_cols(tab, col, tab->n_col - 1);
973 var_from_col(tab, tab->n_col - 1)->index = -1;
974 tab->n_col--;
975 return 1;
979 /* Row variable "var" is non-negative and cannot attain any values
980 * larger than zero. This means that the coefficients of the unrestricted
981 * column variables are zero and that the coefficients of the non-negative
982 * column variables are zero or negative.
983 * Each of the non-negative variables with a negative coefficient can
984 * then also be written as the negative sum of non-negative variables
985 * and must therefore also be zero.
987 static void close_row(struct isl_tab *tab, struct isl_tab_var *var)
989 int j;
990 struct isl_mat *mat = tab->mat;
991 unsigned off = 2 + tab->M;
993 isl_assert(tab->mat->ctx, var->is_nonneg, return);
994 var->is_zero = 1;
995 for (j = tab->n_dead; j < tab->n_col; ++j) {
996 if (isl_int_is_zero(mat->row[var->index][off + j]))
997 continue;
998 isl_assert(tab->mat->ctx,
999 isl_int_is_neg(mat->row[var->index][off + j]), return);
1000 if (isl_tab_kill_col(tab, j))
1001 --j;
1003 isl_tab_mark_redundant(tab, var->index);
1006 /* Add a constraint to the tableau and allocate a row for it.
1007 * Return the index into the constraint array "con".
1009 int isl_tab_allocate_con(struct isl_tab *tab)
1011 int r;
1013 isl_assert(tab->mat->ctx, tab->n_row < tab->mat->n_row, return -1);
1015 r = tab->n_con;
1016 tab->con[r].index = tab->n_row;
1017 tab->con[r].is_row = 1;
1018 tab->con[r].is_nonneg = 0;
1019 tab->con[r].is_zero = 0;
1020 tab->con[r].is_redundant = 0;
1021 tab->con[r].frozen = 0;
1022 tab->row_var[tab->n_row] = ~r;
1024 tab->n_row++;
1025 tab->n_con++;
1026 isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->con[r]);
1028 return r;
1031 /* Add a variable to the tableau and allocate a column for it.
1032 * Return the index into the variable array "var".
1034 int isl_tab_allocate_var(struct isl_tab *tab)
1036 int r;
1037 int i;
1038 unsigned off = 2 + tab->M;
1040 isl_assert(tab->mat->ctx, tab->n_col < tab->mat->n_col, return -1);
1041 isl_assert(tab->mat->ctx, tab->n_var < tab->max_var, return -1);
1043 r = tab->n_var;
1044 tab->var[r].index = tab->n_col;
1045 tab->var[r].is_row = 0;
1046 tab->var[r].is_nonneg = 0;
1047 tab->var[r].is_zero = 0;
1048 tab->var[r].is_redundant = 0;
1049 tab->var[r].frozen = 0;
1050 tab->col_var[tab->n_col] = r;
1052 for (i = 0; i < tab->n_row; ++i)
1053 isl_int_set_si(tab->mat->row[i][off + tab->n_col], 0);
1055 tab->n_var++;
1056 tab->n_col++;
1057 isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->var[r]);
1059 return r;
1062 /* Add a row to the tableau. The row is given as an affine combination
1063 * of the original variables and needs to be expressed in terms of the
1064 * column variables.
1066 * We add each term in turn.
1067 * If r = n/d_r is the current sum and we need to add k x, then
1068 * if x is a column variable, we increase the numerator of
1069 * this column by k d_r
1070 * if x = f/d_x is a row variable, then the new representation of r is
1072 * n k f d_x/g n + d_r/g k f m/d_r n + m/d_g k f
1073 * --- + --- = ------------------- = -------------------
1074 * d_r d_r d_r d_x/g m
1076 * with g the gcd of d_r and d_x and m the lcm of d_r and d_x.
1078 int isl_tab_add_row(struct isl_tab *tab, isl_int *line)
1080 int i;
1081 int r;
1082 isl_int *row;
1083 isl_int a, b;
1084 unsigned off = 2 + tab->M;
1086 r = isl_tab_allocate_con(tab);
1087 if (r < 0)
1088 return -1;
1090 isl_int_init(a);
1091 isl_int_init(b);
1092 row = tab->mat->row[tab->con[r].index];
1093 isl_int_set_si(row[0], 1);
1094 isl_int_set(row[1], line[0]);
1095 isl_seq_clr(row + 2, tab->M + tab->n_col);
1096 for (i = 0; i < tab->n_var; ++i) {
1097 if (tab->var[i].is_zero)
1098 continue;
1099 if (tab->var[i].is_row) {
1100 isl_int_lcm(a,
1101 row[0], tab->mat->row[tab->var[i].index][0]);
1102 isl_int_swap(a, row[0]);
1103 isl_int_divexact(a, row[0], a);
1104 isl_int_divexact(b,
1105 row[0], tab->mat->row[tab->var[i].index][0]);
1106 isl_int_mul(b, b, line[1 + i]);
1107 isl_seq_combine(row + 1, a, row + 1,
1108 b, tab->mat->row[tab->var[i].index] + 1,
1109 1 + tab->M + tab->n_col);
1110 } else
1111 isl_int_addmul(row[off + tab->var[i].index],
1112 line[1 + i], row[0]);
1113 if (tab->M && i >= tab->n_param && i < tab->n_var - tab->n_div)
1114 isl_int_submul(row[2], line[1 + i], row[0]);
1116 isl_seq_normalize(row, off + tab->n_col);
1117 isl_int_clear(a);
1118 isl_int_clear(b);
1120 return r;
1123 static int drop_row(struct isl_tab *tab, int row)
1125 isl_assert(tab->mat->ctx, ~tab->row_var[row] == tab->n_con - 1, return -1);
1126 if (row != tab->n_row - 1)
1127 swap_rows(tab, row, tab->n_row - 1);
1128 tab->n_row--;
1129 tab->n_con--;
1130 return 0;
1133 static int drop_col(struct isl_tab *tab, int col)
1135 isl_assert(tab->mat->ctx, tab->col_var[col] == tab->n_var - 1, return -1);
1136 if (col != tab->n_col - 1)
1137 swap_cols(tab, col, tab->n_col - 1);
1138 tab->n_col--;
1139 tab->n_var--;
1140 return 0;
1143 /* Add inequality "ineq" and check if it conflicts with the
1144 * previously added constraints or if it is obviously redundant.
1146 struct isl_tab *isl_tab_add_ineq(struct isl_tab *tab, isl_int *ineq)
1148 int r;
1149 int sgn;
1151 if (!tab)
1152 return NULL;
1153 r = isl_tab_add_row(tab, ineq);
1154 if (r < 0)
1155 goto error;
1156 tab->con[r].is_nonneg = 1;
1157 isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]);
1158 if (isl_tab_row_is_redundant(tab, tab->con[r].index)) {
1159 isl_tab_mark_redundant(tab, tab->con[r].index);
1160 return tab;
1163 sgn = restore_row(tab, &tab->con[r]);
1164 if (sgn < 0)
1165 return isl_tab_mark_empty(tab);
1166 if (tab->con[r].is_row && isl_tab_row_is_redundant(tab, tab->con[r].index))
1167 isl_tab_mark_redundant(tab, tab->con[r].index);
1168 return tab;
1169 error:
1170 isl_tab_free(tab);
1171 return NULL;
1174 /* Pivot a non-negative variable down until it reaches the value zero
1175 * and then pivot the variable into a column position.
1177 int to_col(struct isl_tab *tab, struct isl_tab_var *var)
1179 int i;
1180 int row, col;
1181 unsigned off = 2 + tab->M;
1183 if (!var->is_row)
1184 return;
1186 while (isl_int_is_pos(tab->mat->row[var->index][1])) {
1187 find_pivot(tab, var, NULL, -1, &row, &col);
1188 isl_assert(tab->mat->ctx, row != -1, return -1);
1189 isl_tab_pivot(tab, row, col);
1190 if (!var->is_row)
1191 return;
1194 for (i = tab->n_dead; i < tab->n_col; ++i)
1195 if (!isl_int_is_zero(tab->mat->row[var->index][off + i]))
1196 break;
1198 isl_assert(tab->mat->ctx, i < tab->n_col, return -1);
1199 isl_tab_pivot(tab, var->index, i);
1201 return 0;
1204 /* We assume Gaussian elimination has been performed on the equalities.
1205 * The equalities can therefore never conflict.
1206 * Adding the equalities is currently only really useful for a later call
1207 * to isl_tab_ineq_type.
1209 static struct isl_tab *add_eq(struct isl_tab *tab, isl_int *eq)
1211 int i;
1212 int r;
1214 if (!tab)
1215 return NULL;
1216 r = isl_tab_add_row(tab, eq);
1217 if (r < 0)
1218 goto error;
1220 r = tab->con[r].index;
1221 i = isl_seq_first_non_zero(tab->mat->row[r] + 2 + tab->M + tab->n_dead,
1222 tab->n_col - tab->n_dead);
1223 isl_assert(tab->mat->ctx, i >= 0, goto error);
1224 i += tab->n_dead;
1225 isl_tab_pivot(tab, r, i);
1226 isl_tab_kill_col(tab, i);
1227 tab->n_eq++;
1229 return tab;
1230 error:
1231 isl_tab_free(tab);
1232 return NULL;
1235 /* Add an equality that is known to be valid for the given tableau.
1237 struct isl_tab *isl_tab_add_valid_eq(struct isl_tab *tab, isl_int *eq)
1239 struct isl_tab_var *var;
1240 int i;
1241 int r;
1243 if (!tab)
1244 return NULL;
1245 r = isl_tab_add_row(tab, eq);
1246 if (r < 0)
1247 goto error;
1249 var = &tab->con[r];
1250 r = var->index;
1251 if (isl_int_is_neg(tab->mat->row[r][1]))
1252 isl_seq_neg(tab->mat->row[r] + 1, tab->mat->row[r] + 1,
1253 1 + tab->n_col);
1254 var->is_nonneg = 1;
1255 if (to_col(tab, var) < 0)
1256 goto error;
1257 var->is_nonneg = 0;
1258 isl_tab_kill_col(tab, var->index);
1260 return tab;
1261 error:
1262 isl_tab_free(tab);
1263 return NULL;
1266 struct isl_tab *isl_tab_from_basic_map(struct isl_basic_map *bmap)
1268 int i;
1269 struct isl_tab *tab;
1271 if (!bmap)
1272 return NULL;
1273 tab = isl_tab_alloc(bmap->ctx,
1274 isl_basic_map_total_dim(bmap) + bmap->n_ineq + 1,
1275 isl_basic_map_total_dim(bmap), 0);
1276 if (!tab)
1277 return NULL;
1278 tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
1279 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1280 return isl_tab_mark_empty(tab);
1281 for (i = 0; i < bmap->n_eq; ++i) {
1282 tab = add_eq(tab, bmap->eq[i]);
1283 if (!tab)
1284 return tab;
1286 for (i = 0; i < bmap->n_ineq; ++i) {
1287 tab = isl_tab_add_ineq(tab, bmap->ineq[i]);
1288 if (!tab || tab->empty)
1289 return tab;
1291 return tab;
1294 struct isl_tab *isl_tab_from_basic_set(struct isl_basic_set *bset)
1296 return isl_tab_from_basic_map((struct isl_basic_map *)bset);
1299 /* Construct a tableau corresponding to the recession cone of "bmap".
1301 struct isl_tab *isl_tab_from_recession_cone(struct isl_basic_map *bmap)
1303 isl_int cst;
1304 int i;
1305 struct isl_tab *tab;
1307 if (!bmap)
1308 return NULL;
1309 tab = isl_tab_alloc(bmap->ctx, bmap->n_eq + bmap->n_ineq,
1310 isl_basic_map_total_dim(bmap), 0);
1311 if (!tab)
1312 return NULL;
1313 tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
1315 isl_int_init(cst);
1316 for (i = 0; i < bmap->n_eq; ++i) {
1317 isl_int_swap(bmap->eq[i][0], cst);
1318 tab = add_eq(tab, bmap->eq[i]);
1319 isl_int_swap(bmap->eq[i][0], cst);
1320 if (!tab)
1321 goto done;
1323 for (i = 0; i < bmap->n_ineq; ++i) {
1324 int r;
1325 isl_int_swap(bmap->ineq[i][0], cst);
1326 r = isl_tab_add_row(tab, bmap->ineq[i]);
1327 isl_int_swap(bmap->ineq[i][0], cst);
1328 if (r < 0)
1329 goto error;
1330 tab->con[r].is_nonneg = 1;
1331 isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]);
1333 done:
1334 isl_int_clear(cst);
1335 return tab;
1336 error:
1337 isl_int_clear(cst);
1338 isl_tab_free(tab);
1339 return NULL;
1342 /* Assuming "tab" is the tableau of a cone, check if the cone is
1343 * bounded, i.e., if it is empty or only contains the origin.
1345 int isl_tab_cone_is_bounded(struct isl_tab *tab)
1347 int i;
1349 if (!tab)
1350 return -1;
1351 if (tab->empty)
1352 return 1;
1353 if (tab->n_dead == tab->n_col)
1354 return 1;
1356 for (;;) {
1357 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1358 struct isl_tab_var *var;
1359 var = isl_tab_var_from_row(tab, i);
1360 if (!var->is_nonneg)
1361 continue;
1362 if (sign_of_max(tab, var) != 0)
1363 return 0;
1364 close_row(tab, var);
1365 break;
1367 if (tab->n_dead == tab->n_col)
1368 return 1;
1369 if (i == tab->n_row)
1370 return 0;
1374 int isl_tab_sample_is_integer(struct isl_tab *tab)
1376 int i;
1378 if (!tab)
1379 return -1;
1381 for (i = 0; i < tab->n_var; ++i) {
1382 int row;
1383 if (!tab->var[i].is_row)
1384 continue;
1385 row = tab->var[i].index;
1386 if (!isl_int_is_divisible_by(tab->mat->row[row][1],
1387 tab->mat->row[row][0]))
1388 return 0;
1390 return 1;
1393 static struct isl_vec *extract_integer_sample(struct isl_tab *tab)
1395 int i;
1396 struct isl_vec *vec;
1398 vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
1399 if (!vec)
1400 return NULL;
1402 isl_int_set_si(vec->block.data[0], 1);
1403 for (i = 0; i < tab->n_var; ++i) {
1404 if (!tab->var[i].is_row)
1405 isl_int_set_si(vec->block.data[1 + i], 0);
1406 else {
1407 int row = tab->var[i].index;
1408 isl_int_divexact(vec->block.data[1 + i],
1409 tab->mat->row[row][1], tab->mat->row[row][0]);
1413 return vec;
1416 struct isl_vec *isl_tab_get_sample_value(struct isl_tab *tab)
1418 int i;
1419 struct isl_vec *vec;
1420 isl_int m;
1422 if (!tab)
1423 return NULL;
1425 vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
1426 if (!vec)
1427 return NULL;
1429 isl_int_init(m);
1431 isl_int_set_si(vec->block.data[0], 1);
1432 for (i = 0; i < tab->n_var; ++i) {
1433 int row;
1434 if (!tab->var[i].is_row) {
1435 isl_int_set_si(vec->block.data[1 + i], 0);
1436 continue;
1438 row = tab->var[i].index;
1439 isl_int_gcd(m, vec->block.data[0], tab->mat->row[row][0]);
1440 isl_int_divexact(m, tab->mat->row[row][0], m);
1441 isl_seq_scale(vec->block.data, vec->block.data, m, 1 + i);
1442 isl_int_divexact(m, vec->block.data[0], tab->mat->row[row][0]);
1443 isl_int_mul(vec->block.data[1 + i], m, tab->mat->row[row][1]);
1445 isl_seq_normalize(vec->block.data, vec->size);
1447 isl_int_clear(m);
1448 return vec;
1451 /* Update "bmap" based on the results of the tableau "tab".
1452 * In particular, implicit equalities are made explicit, redundant constraints
1453 * are removed and if the sample value happens to be integer, it is stored
1454 * in "bmap" (unless "bmap" already had an integer sample).
1456 * The tableau is assumed to have been created from "bmap" using
1457 * isl_tab_from_basic_map.
1459 struct isl_basic_map *isl_basic_map_update_from_tab(struct isl_basic_map *bmap,
1460 struct isl_tab *tab)
1462 int i;
1463 unsigned n_eq;
1465 if (!bmap)
1466 return NULL;
1467 if (!tab)
1468 return bmap;
1470 n_eq = tab->n_eq;
1471 if (tab->empty)
1472 bmap = isl_basic_map_set_to_empty(bmap);
1473 else
1474 for (i = bmap->n_ineq - 1; i >= 0; --i) {
1475 if (isl_tab_is_equality(tab, n_eq + i))
1476 isl_basic_map_inequality_to_equality(bmap, i);
1477 else if (isl_tab_is_redundant(tab, n_eq + i))
1478 isl_basic_map_drop_inequality(bmap, i);
1480 if (!tab->rational &&
1481 !bmap->sample && isl_tab_sample_is_integer(tab))
1482 bmap->sample = extract_integer_sample(tab);
1483 return bmap;
1486 struct isl_basic_set *isl_basic_set_update_from_tab(struct isl_basic_set *bset,
1487 struct isl_tab *tab)
1489 return (struct isl_basic_set *)isl_basic_map_update_from_tab(
1490 (struct isl_basic_map *)bset, tab);
1493 /* Given a non-negative variable "var", add a new non-negative variable
1494 * that is the opposite of "var", ensuring that var can only attain the
1495 * value zero.
1496 * If var = n/d is a row variable, then the new variable = -n/d.
1497 * If var is a column variables, then the new variable = -var.
1498 * If the new variable cannot attain non-negative values, then
1499 * the resulting tableau is empty.
1500 * Otherwise, we know the value will be zero and we close the row.
1502 static struct isl_tab *cut_to_hyperplane(struct isl_tab *tab,
1503 struct isl_tab_var *var)
1505 unsigned r;
1506 isl_int *row;
1507 int sgn;
1508 unsigned off = 2 + tab->M;
1510 if (isl_tab_extend_cons(tab, 1) < 0)
1511 goto error;
1513 r = tab->n_con;
1514 tab->con[r].index = tab->n_row;
1515 tab->con[r].is_row = 1;
1516 tab->con[r].is_nonneg = 0;
1517 tab->con[r].is_zero = 0;
1518 tab->con[r].is_redundant = 0;
1519 tab->con[r].frozen = 0;
1520 tab->row_var[tab->n_row] = ~r;
1521 row = tab->mat->row[tab->n_row];
1523 if (var->is_row) {
1524 isl_int_set(row[0], tab->mat->row[var->index][0]);
1525 isl_seq_neg(row + 1,
1526 tab->mat->row[var->index] + 1, 1 + tab->n_col);
1527 } else {
1528 isl_int_set_si(row[0], 1);
1529 isl_seq_clr(row + 1, 1 + tab->n_col);
1530 isl_int_set_si(row[off + var->index], -1);
1533 tab->n_row++;
1534 tab->n_con++;
1535 isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->con[r]);
1537 sgn = sign_of_max(tab, &tab->con[r]);
1538 if (sgn < 0)
1539 return isl_tab_mark_empty(tab);
1540 tab->con[r].is_nonneg = 1;
1541 isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]);
1542 /* sgn == 0 */
1543 close_row(tab, &tab->con[r]);
1545 return tab;
1546 error:
1547 isl_tab_free(tab);
1548 return NULL;
1551 /* Given a tableau "tab" and an inequality constraint "con" of the tableau,
1552 * relax the inequality by one. That is, the inequality r >= 0 is replaced
1553 * by r' = r + 1 >= 0.
1554 * If r is a row variable, we simply increase the constant term by one
1555 * (taking into account the denominator).
1556 * If r is a column variable, then we need to modify each row that
1557 * refers to r = r' - 1 by substituting this equality, effectively
1558 * subtracting the coefficient of the column from the constant.
1560 struct isl_tab *isl_tab_relax(struct isl_tab *tab, int con)
1562 struct isl_tab_var *var;
1563 unsigned off = 2 + tab->M;
1565 if (!tab)
1566 return NULL;
1568 var = &tab->con[con];
1570 if (!var->is_row && !max_is_manifestly_unbounded(tab, var))
1571 to_row(tab, var, 1);
1573 if (var->is_row)
1574 isl_int_add(tab->mat->row[var->index][1],
1575 tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
1576 else {
1577 int i;
1579 for (i = 0; i < tab->n_row; ++i) {
1580 if (isl_int_is_zero(tab->mat->row[i][off + var->index]))
1581 continue;
1582 isl_int_sub(tab->mat->row[i][1], tab->mat->row[i][1],
1583 tab->mat->row[i][off + var->index]);
1588 isl_tab_push_var(tab, isl_tab_undo_relax, var);
1590 return tab;
1593 struct isl_tab *isl_tab_select_facet(struct isl_tab *tab, int con)
1595 if (!tab)
1596 return NULL;
1598 return cut_to_hyperplane(tab, &tab->con[con]);
1601 static int may_be_equality(struct isl_tab *tab, int row)
1603 unsigned off = 2 + tab->M;
1604 return (tab->rational ? isl_int_is_zero(tab->mat->row[row][1])
1605 : isl_int_lt(tab->mat->row[row][1],
1606 tab->mat->row[row][0])) &&
1607 isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
1608 tab->n_col - tab->n_dead) != -1;
1611 /* Check for (near) equalities among the constraints.
1612 * A constraint is an equality if it is non-negative and if
1613 * its maximal value is either
1614 * - zero (in case of rational tableaus), or
1615 * - strictly less than 1 (in case of integer tableaus)
1617 * We first mark all non-redundant and non-dead variables that
1618 * are not frozen and not obviously not an equality.
1619 * Then we iterate over all marked variables if they can attain
1620 * any values larger than zero or at least one.
1621 * If the maximal value is zero, we mark any column variables
1622 * that appear in the row as being zero and mark the row as being redundant.
1623 * Otherwise, if the maximal value is strictly less than one (and the
1624 * tableau is integer), then we restrict the value to being zero
1625 * by adding an opposite non-negative variable.
1627 struct isl_tab *isl_tab_detect_equalities(struct isl_tab *tab)
1629 int i;
1630 unsigned n_marked;
1632 if (!tab)
1633 return NULL;
1634 if (tab->empty)
1635 return tab;
1636 if (tab->n_dead == tab->n_col)
1637 return tab;
1639 n_marked = 0;
1640 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1641 struct isl_tab_var *var = isl_tab_var_from_row(tab, i);
1642 var->marked = !var->frozen && var->is_nonneg &&
1643 may_be_equality(tab, i);
1644 if (var->marked)
1645 n_marked++;
1647 for (i = tab->n_dead; i < tab->n_col; ++i) {
1648 struct isl_tab_var *var = var_from_col(tab, i);
1649 var->marked = !var->frozen && var->is_nonneg;
1650 if (var->marked)
1651 n_marked++;
1653 while (n_marked) {
1654 struct isl_tab_var *var;
1655 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1656 var = isl_tab_var_from_row(tab, i);
1657 if (var->marked)
1658 break;
1660 if (i == tab->n_row) {
1661 for (i = tab->n_dead; i < tab->n_col; ++i) {
1662 var = var_from_col(tab, i);
1663 if (var->marked)
1664 break;
1666 if (i == tab->n_col)
1667 break;
1669 var->marked = 0;
1670 n_marked--;
1671 if (sign_of_max(tab, var) == 0)
1672 close_row(tab, var);
1673 else if (!tab->rational && !at_least_one(tab, var)) {
1674 tab = cut_to_hyperplane(tab, var);
1675 return isl_tab_detect_equalities(tab);
1677 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1678 var = isl_tab_var_from_row(tab, i);
1679 if (!var->marked)
1680 continue;
1681 if (may_be_equality(tab, i))
1682 continue;
1683 var->marked = 0;
1684 n_marked--;
1688 return tab;
1691 /* Check for (near) redundant constraints.
1692 * A constraint is redundant if it is non-negative and if
1693 * its minimal value (temporarily ignoring the non-negativity) is either
1694 * - zero (in case of rational tableaus), or
1695 * - strictly larger than -1 (in case of integer tableaus)
1697 * We first mark all non-redundant and non-dead variables that
1698 * are not frozen and not obviously negatively unbounded.
1699 * Then we iterate over all marked variables if they can attain
1700 * any values smaller than zero or at most negative one.
1701 * If not, we mark the row as being redundant (assuming it hasn't
1702 * been detected as being obviously redundant in the mean time).
1704 struct isl_tab *isl_tab_detect_redundant(struct isl_tab *tab)
1706 int i;
1707 unsigned n_marked;
1709 if (!tab)
1710 return NULL;
1711 if (tab->empty)
1712 return tab;
1713 if (tab->n_redundant == tab->n_row)
1714 return tab;
1716 n_marked = 0;
1717 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1718 struct isl_tab_var *var = isl_tab_var_from_row(tab, i);
1719 var->marked = !var->frozen && var->is_nonneg;
1720 if (var->marked)
1721 n_marked++;
1723 for (i = tab->n_dead; i < tab->n_col; ++i) {
1724 struct isl_tab_var *var = var_from_col(tab, i);
1725 var->marked = !var->frozen && var->is_nonneg &&
1726 !min_is_manifestly_unbounded(tab, var);
1727 if (var->marked)
1728 n_marked++;
1730 while (n_marked) {
1731 struct isl_tab_var *var;
1732 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1733 var = isl_tab_var_from_row(tab, i);
1734 if (var->marked)
1735 break;
1737 if (i == tab->n_row) {
1738 for (i = tab->n_dead; i < tab->n_col; ++i) {
1739 var = var_from_col(tab, i);
1740 if (var->marked)
1741 break;
1743 if (i == tab->n_col)
1744 break;
1746 var->marked = 0;
1747 n_marked--;
1748 if ((tab->rational ? (sign_of_min(tab, var) >= 0)
1749 : !isl_tab_min_at_most_neg_one(tab, var)) &&
1750 !var->is_redundant)
1751 isl_tab_mark_redundant(tab, var->index);
1752 for (i = tab->n_dead; i < tab->n_col; ++i) {
1753 var = var_from_col(tab, i);
1754 if (!var->marked)
1755 continue;
1756 if (!min_is_manifestly_unbounded(tab, var))
1757 continue;
1758 var->marked = 0;
1759 n_marked--;
1763 return tab;
1766 int isl_tab_is_equality(struct isl_tab *tab, int con)
1768 int row;
1769 unsigned off;
1771 if (!tab)
1772 return -1;
1773 if (tab->con[con].is_zero)
1774 return 1;
1775 if (tab->con[con].is_redundant)
1776 return 0;
1777 if (!tab->con[con].is_row)
1778 return tab->con[con].index < tab->n_dead;
1780 row = tab->con[con].index;
1782 off = 2 + tab->M;
1783 return isl_int_is_zero(tab->mat->row[row][1]) &&
1784 isl_seq_first_non_zero(tab->mat->row[row] + 2 + tab->n_dead,
1785 tab->n_col - tab->n_dead) == -1;
1788 /* Return the minimial value of the affine expression "f" with denominator
1789 * "denom" in *opt, *opt_denom, assuming the tableau is not empty and
1790 * the expression cannot attain arbitrarily small values.
1791 * If opt_denom is NULL, then *opt is rounded up to the nearest integer.
1792 * The return value reflects the nature of the result (empty, unbounded,
1793 * minmimal value returned in *opt).
1795 enum isl_lp_result isl_tab_min(struct isl_tab *tab,
1796 isl_int *f, isl_int denom, isl_int *opt, isl_int *opt_denom,
1797 unsigned flags)
1799 int r;
1800 enum isl_lp_result res = isl_lp_ok;
1801 struct isl_tab_var *var;
1802 struct isl_tab_undo *snap;
1804 if (tab->empty)
1805 return isl_lp_empty;
1807 snap = isl_tab_snap(tab);
1808 r = isl_tab_add_row(tab, f);
1809 if (r < 0)
1810 return isl_lp_error;
1811 var = &tab->con[r];
1812 isl_int_mul(tab->mat->row[var->index][0],
1813 tab->mat->row[var->index][0], denom);
1814 for (;;) {
1815 int row, col;
1816 find_pivot(tab, var, var, -1, &row, &col);
1817 if (row == var->index) {
1818 res = isl_lp_unbounded;
1819 break;
1821 if (row == -1)
1822 break;
1823 isl_tab_pivot(tab, row, col);
1825 if (isl_tab_rollback(tab, snap) < 0)
1826 return isl_lp_error;
1827 if (ISL_FL_ISSET(flags, ISL_TAB_SAVE_DUAL)) {
1828 int i;
1830 isl_vec_free(tab->dual);
1831 tab->dual = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_con);
1832 if (!tab->dual)
1833 return isl_lp_error;
1834 isl_int_set(tab->dual->el[0], tab->mat->row[var->index][0]);
1835 for (i = 0; i < tab->n_con; ++i) {
1836 if (tab->con[i].is_row)
1837 isl_int_set_si(tab->dual->el[1 + i], 0);
1838 else {
1839 int pos = 2 + tab->con[i].index;
1840 isl_int_set(tab->dual->el[1 + i],
1841 tab->mat->row[var->index][pos]);
1845 if (res == isl_lp_ok) {
1846 if (opt_denom) {
1847 isl_int_set(*opt, tab->mat->row[var->index][1]);
1848 isl_int_set(*opt_denom, tab->mat->row[var->index][0]);
1849 } else
1850 isl_int_cdiv_q(*opt, tab->mat->row[var->index][1],
1851 tab->mat->row[var->index][0]);
1853 return res;
1856 int isl_tab_is_redundant(struct isl_tab *tab, int con)
1858 int row;
1859 unsigned n_col;
1861 if (!tab)
1862 return -1;
1863 if (tab->con[con].is_zero)
1864 return 0;
1865 if (tab->con[con].is_redundant)
1866 return 1;
1867 return tab->con[con].is_row && tab->con[con].index < tab->n_redundant;
1870 /* Take a snapshot of the tableau that can be restored by s call to
1871 * isl_tab_rollback.
1873 struct isl_tab_undo *isl_tab_snap(struct isl_tab *tab)
1875 if (!tab)
1876 return NULL;
1877 tab->need_undo = 1;
1878 return tab->top;
1881 /* Undo the operation performed by isl_tab_relax.
1883 static void unrelax(struct isl_tab *tab, struct isl_tab_var *var)
1885 unsigned off = 2 + tab->M;
1887 if (!var->is_row && !max_is_manifestly_unbounded(tab, var))
1888 to_row(tab, var, 1);
1890 if (var->is_row)
1891 isl_int_sub(tab->mat->row[var->index][1],
1892 tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
1893 else {
1894 int i;
1896 for (i = 0; i < tab->n_row; ++i) {
1897 if (isl_int_is_zero(tab->mat->row[i][off + var->index]))
1898 continue;
1899 isl_int_add(tab->mat->row[i][1], tab->mat->row[i][1],
1900 tab->mat->row[i][off + var->index]);
1906 static void perform_undo_var(struct isl_tab *tab, struct isl_tab_undo *undo)
1908 struct isl_tab_var *var = var_from_index(tab, undo->u.var_index);
1909 switch(undo->type) {
1910 case isl_tab_undo_nonneg:
1911 var->is_nonneg = 0;
1912 break;
1913 case isl_tab_undo_redundant:
1914 var->is_redundant = 0;
1915 tab->n_redundant--;
1916 break;
1917 case isl_tab_undo_zero:
1918 var->is_zero = 0;
1919 tab->n_dead--;
1920 break;
1921 case isl_tab_undo_allocate:
1922 if (undo->u.var_index >= 0) {
1923 isl_assert(tab->mat->ctx, !var->is_row, return);
1924 drop_col(tab, var->index);
1925 break;
1927 if (!var->is_row) {
1928 if (!max_is_manifestly_unbounded(tab, var))
1929 to_row(tab, var, 1);
1930 else if (!min_is_manifestly_unbounded(tab, var))
1931 to_row(tab, var, -1);
1932 else
1933 to_row(tab, var, 0);
1935 drop_row(tab, var->index);
1936 break;
1937 case isl_tab_undo_relax:
1938 unrelax(tab, var);
1939 break;
1943 /* Restore the tableau to the state where the basic variables
1944 * are those in "col_var".
1945 * We first construct a list of variables that are currently in
1946 * the basis, but shouldn't. Then we iterate over all variables
1947 * that should be in the basis and for each one that is currently
1948 * not in the basis, we exchange it with one of the elements of the
1949 * list constructed before.
1950 * We can always find an appropriate variable to pivot with because
1951 * the current basis is mapped to the old basis by a non-singular
1952 * matrix and so we can never end up with a zero row.
1954 static int restore_basis(struct isl_tab *tab, int *col_var)
1956 int i, j;
1957 int n_extra = 0;
1958 int *extra = NULL; /* current columns that contain bad stuff */
1959 unsigned off = 2 + tab->M;
1961 extra = isl_alloc_array(tab->mat->ctx, int, tab->n_col);
1962 if (!extra)
1963 goto error;
1964 for (i = 0; i < tab->n_col; ++i) {
1965 for (j = 0; j < tab->n_col; ++j)
1966 if (tab->col_var[i] == col_var[j])
1967 break;
1968 if (j < tab->n_col)
1969 continue;
1970 extra[n_extra++] = i;
1972 for (i = 0; i < tab->n_col && n_extra > 0; ++i) {
1973 struct isl_tab_var *var;
1974 int row;
1976 for (j = 0; j < tab->n_col; ++j)
1977 if (col_var[i] == tab->col_var[j])
1978 break;
1979 if (j < tab->n_col)
1980 continue;
1981 var = var_from_index(tab, col_var[i]);
1982 row = var->index;
1983 for (j = 0; j < n_extra; ++j)
1984 if (!isl_int_is_zero(tab->mat->row[row][off+extra[j]]))
1985 break;
1986 isl_assert(tab->mat->ctx, j < n_extra, goto error);
1987 isl_tab_pivot(tab, row, extra[j]);
1988 extra[j] = extra[--n_extra];
1991 free(extra);
1992 free(col_var);
1993 return 0;
1994 error:
1995 free(extra);
1996 free(col_var);
1997 return -1;
2000 static int perform_undo(struct isl_tab *tab, struct isl_tab_undo *undo)
2002 switch (undo->type) {
2003 case isl_tab_undo_empty:
2004 tab->empty = 0;
2005 break;
2006 case isl_tab_undo_nonneg:
2007 case isl_tab_undo_redundant:
2008 case isl_tab_undo_zero:
2009 case isl_tab_undo_allocate:
2010 case isl_tab_undo_relax:
2011 perform_undo_var(tab, undo);
2012 break;
2013 case isl_tab_undo_saved_basis:
2014 if (restore_basis(tab, undo->u.col_var) < 0)
2015 return -1;
2016 break;
2017 default:
2018 isl_assert(tab->mat->ctx, 0, return -1);
2020 return 0;
2023 /* Return the tableau to the state it was in when the snapshot "snap"
2024 * was taken.
2026 int isl_tab_rollback(struct isl_tab *tab, struct isl_tab_undo *snap)
2028 struct isl_tab_undo *undo, *next;
2030 if (!tab)
2031 return -1;
2033 tab->in_undo = 1;
2034 for (undo = tab->top; undo && undo != &tab->bottom; undo = next) {
2035 next = undo->next;
2036 if (undo == snap)
2037 break;
2038 if (perform_undo(tab, undo) < 0) {
2039 free_undo(tab);
2040 tab->in_undo = 0;
2041 return -1;
2043 free(undo);
2045 tab->in_undo = 0;
2046 tab->top = undo;
2047 if (!undo)
2048 return -1;
2049 return 0;
2052 /* The given row "row" represents an inequality violated by all
2053 * points in the tableau. Check for some special cases of such
2054 * separating constraints.
2055 * In particular, if the row has been reduced to the constant -1,
2056 * then we know the inequality is adjacent (but opposite) to
2057 * an equality in the tableau.
2058 * If the row has been reduced to r = -1 -r', with r' an inequality
2059 * of the tableau, then the inequality is adjacent (but opposite)
2060 * to the inequality r'.
2062 static enum isl_ineq_type separation_type(struct isl_tab *tab, unsigned row)
2064 int pos;
2065 unsigned off = 2 + tab->M;
2067 if (tab->rational)
2068 return isl_ineq_separate;
2070 if (!isl_int_is_one(tab->mat->row[row][0]))
2071 return isl_ineq_separate;
2072 if (!isl_int_is_negone(tab->mat->row[row][1]))
2073 return isl_ineq_separate;
2075 pos = isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
2076 tab->n_col - tab->n_dead);
2077 if (pos == -1)
2078 return isl_ineq_adj_eq;
2080 if (!isl_int_is_negone(tab->mat->row[row][off + tab->n_dead + pos]))
2081 return isl_ineq_separate;
2083 pos = isl_seq_first_non_zero(
2084 tab->mat->row[row] + off + tab->n_dead + pos + 1,
2085 tab->n_col - tab->n_dead - pos - 1);
2087 return pos == -1 ? isl_ineq_adj_ineq : isl_ineq_separate;
2090 /* Check the effect of inequality "ineq" on the tableau "tab".
2091 * The result may be
2092 * isl_ineq_redundant: satisfied by all points in the tableau
2093 * isl_ineq_separate: satisfied by no point in the tableau
2094 * isl_ineq_cut: satisfied by some by not all points
2095 * isl_ineq_adj_eq: adjacent to an equality
2096 * isl_ineq_adj_ineq: adjacent to an inequality.
2098 enum isl_ineq_type isl_tab_ineq_type(struct isl_tab *tab, isl_int *ineq)
2100 enum isl_ineq_type type = isl_ineq_error;
2101 struct isl_tab_undo *snap = NULL;
2102 int con;
2103 int row;
2105 if (!tab)
2106 return isl_ineq_error;
2108 if (isl_tab_extend_cons(tab, 1) < 0)
2109 return isl_ineq_error;
2111 snap = isl_tab_snap(tab);
2113 con = isl_tab_add_row(tab, ineq);
2114 if (con < 0)
2115 goto error;
2117 row = tab->con[con].index;
2118 if (isl_tab_row_is_redundant(tab, row))
2119 type = isl_ineq_redundant;
2120 else if (isl_int_is_neg(tab->mat->row[row][1]) &&
2121 (tab->rational ||
2122 isl_int_abs_ge(tab->mat->row[row][1],
2123 tab->mat->row[row][0]))) {
2124 if (at_least_zero(tab, &tab->con[con]))
2125 type = isl_ineq_cut;
2126 else
2127 type = separation_type(tab, row);
2128 } else if (tab->rational ? (sign_of_min(tab, &tab->con[con]) < 0)
2129 : isl_tab_min_at_most_neg_one(tab, &tab->con[con]))
2130 type = isl_ineq_cut;
2131 else
2132 type = isl_ineq_redundant;
2134 if (isl_tab_rollback(tab, snap))
2135 return isl_ineq_error;
2136 return type;
2137 error:
2138 isl_tab_rollback(tab, snap);
2139 return isl_ineq_error;
2142 void isl_tab_dump(struct isl_tab *tab, FILE *out, int indent)
2144 unsigned r, c;
2145 int i;
2147 if (!tab) {
2148 fprintf(out, "%*snull tab\n", indent, "");
2149 return;
2151 fprintf(out, "%*sn_redundant: %d, n_dead: %d", indent, "",
2152 tab->n_redundant, tab->n_dead);
2153 if (tab->rational)
2154 fprintf(out, ", rational");
2155 if (tab->empty)
2156 fprintf(out, ", empty");
2157 fprintf(out, "\n");
2158 fprintf(out, "%*s[", indent, "");
2159 for (i = 0; i < tab->n_var; ++i) {
2160 if (i)
2161 fprintf(out, (i == tab->n_param ||
2162 i == tab->n_var - tab->n_div) ? "; "
2163 : ", ");
2164 fprintf(out, "%c%d%s", tab->var[i].is_row ? 'r' : 'c',
2165 tab->var[i].index,
2166 tab->var[i].is_zero ? " [=0]" :
2167 tab->var[i].is_redundant ? " [R]" : "");
2169 fprintf(out, "]\n");
2170 fprintf(out, "%*s[", indent, "");
2171 for (i = 0; i < tab->n_con; ++i) {
2172 if (i)
2173 fprintf(out, ", ");
2174 fprintf(out, "%c%d%s", tab->con[i].is_row ? 'r' : 'c',
2175 tab->con[i].index,
2176 tab->con[i].is_zero ? " [=0]" :
2177 tab->con[i].is_redundant ? " [R]" : "");
2179 fprintf(out, "]\n");
2180 fprintf(out, "%*s[", indent, "");
2181 for (i = 0; i < tab->n_row; ++i) {
2182 if (i)
2183 fprintf(out, ", ");
2184 fprintf(out, "r%d: %d%s", i, tab->row_var[i],
2185 isl_tab_var_from_row(tab, i)->is_nonneg ? " [>=0]" : "");
2187 fprintf(out, "]\n");
2188 fprintf(out, "%*s[", indent, "");
2189 for (i = 0; i < tab->n_col; ++i) {
2190 if (i)
2191 fprintf(out, ", ");
2192 fprintf(out, "c%d: %d%s", i, tab->col_var[i],
2193 var_from_col(tab, i)->is_nonneg ? " [>=0]" : "");
2195 fprintf(out, "]\n");
2196 r = tab->mat->n_row;
2197 tab->mat->n_row = tab->n_row;
2198 c = tab->mat->n_col;
2199 tab->mat->n_col = 2 + tab->M + tab->n_col;
2200 isl_mat_dump(tab->mat, out, indent);
2201 tab->mat->n_row = r;
2202 tab->mat->n_col = c;