isl_tab: allow saving and restoring the complete basis
[platform/upstream/isl.git] / isl_tab.c
1 #include "isl_mat.h"
2 #include "isl_map_private.h"
3 #include "isl_tab.h"
4
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  */
10
11 struct isl_tab *isl_tab_alloc(struct isl_ctx *ctx,
12         unsigned n_row, unsigned n_var)
13 {
14         int i;
15         struct isl_tab *tab;
16
17         tab = isl_calloc_type(ctx, struct isl_tab);
18         if (!tab)
19                 return NULL;
20         tab->mat = isl_mat_alloc(ctx, n_row, 2 + n_var);
21         if (!tab->mat)
22                 goto error;
23         tab->var = isl_alloc_array(ctx, struct isl_tab_var, n_var);
24         if (!tab->var)
25                 goto error;
26         tab->con = isl_alloc_array(ctx, struct isl_tab_var, n_row);
27         if (!tab->con)
28                 goto error;
29         tab->col_var = isl_alloc_array(ctx, int, n_var);
30         if (!tab->col_var)
31                 goto error;
32         tab->row_var = isl_alloc_array(ctx, int, n_row);
33         if (!tab->row_var)
34                 goto error;
35         for (i = 0; i < n_var; ++i) {
36                 tab->var[i].index = i;
37                 tab->var[i].is_row = 0;
38                 tab->var[i].is_nonneg = 0;
39                 tab->var[i].is_zero = 0;
40                 tab->var[i].is_redundant = 0;
41                 tab->var[i].frozen = 0;
42                 tab->col_var[i] = i;
43         }
44         tab->n_row = 0;
45         tab->n_con = 0;
46         tab->n_eq = 0;
47         tab->max_con = n_row;
48         tab->n_col = n_var;
49         tab->n_var = n_var;
50         tab->n_dead = 0;
51         tab->n_redundant = 0;
52         tab->need_undo = 0;
53         tab->rational = 0;
54         tab->empty = 0;
55         tab->in_undo = 0;
56         tab->bottom.type = isl_tab_undo_bottom;
57         tab->bottom.next = NULL;
58         tab->top = &tab->bottom;
59         return tab;
60 error:
61         isl_tab_free(tab);
62         return NULL;
63 }
64
65 int isl_tab_extend_cons(struct isl_tab *tab, unsigned n_new)
66 {
67         if (tab->max_con < tab->n_con + n_new) {
68                 struct isl_tab_var *con;
69
70                 con = isl_realloc_array(tab->mat->ctx, tab->con,
71                                     struct isl_tab_var, tab->max_con + n_new);
72                 if (!con)
73                         return -1;
74                 tab->con = con;
75                 tab->max_con += n_new;
76         }
77         if (tab->mat->n_row < tab->n_row + n_new) {
78                 int *row_var;
79
80                 tab->mat = isl_mat_extend(tab->mat,
81                                                 tab->n_row + n_new, tab->n_col);
82                 if (!tab->mat)
83                         return -1;
84                 row_var = isl_realloc_array(tab->mat->ctx, tab->row_var,
85                                             int, tab->mat->n_row);
86                 if (!row_var)
87                         return -1;
88                 tab->row_var = row_var;
89         }
90         return 0;
91 }
92
93 struct isl_tab *isl_tab_extend(struct isl_tab *tab, unsigned n_new)
94 {
95         if (isl_tab_extend_cons(tab, n_new) >= 0)
96                 return tab;
97
98         isl_tab_free(tab);
99         return NULL;
100 }
101
102 static void free_undo(struct isl_tab *tab)
103 {
104         struct isl_tab_undo *undo, *next;
105
106         for (undo = tab->top; undo && undo != &tab->bottom; undo = next) {
107                 next = undo->next;
108                 free(undo);
109         }
110         tab->top = undo;
111 }
112
113 void isl_tab_free(struct isl_tab *tab)
114 {
115         if (!tab)
116                 return;
117         free_undo(tab);
118         isl_mat_free(tab->mat);
119         isl_vec_free(tab->dual);
120         free(tab->var);
121         free(tab->con);
122         free(tab->row_var);
123         free(tab->col_var);
124         free(tab);
125 }
126
127 struct isl_tab *isl_tab_dup(struct isl_tab *tab)
128 {
129         int i;
130         struct isl_tab *dup;
131
132         if (!tab)
133                 return NULL;
134
135         dup = isl_calloc_type(tab->ctx, struct isl_tab);
136         if (!dup)
137                 return NULL;
138         dup->mat = isl_mat_dup(tab->mat);
139         if (!dup->mat)
140                 goto error;
141         dup->var = isl_alloc_array(tab->ctx, struct isl_tab_var, tab->n_var);
142         if (!dup->var)
143                 goto error;
144         for (i = 0; i < tab->n_var; ++i)
145                 dup->var[i] = tab->var[i];
146         dup->con = isl_alloc_array(tab->ctx, struct isl_tab_var, tab->max_con);
147         if (!dup->con)
148                 goto error;
149         for (i = 0; i < tab->n_con; ++i)
150                 dup->con[i] = tab->con[i];
151         dup->col_var = isl_alloc_array(tab->ctx, int, tab->mat->n_col);
152         if (!dup->col_var)
153                 goto error;
154         for (i = 0; i < tab->n_var; ++i)
155                 dup->col_var[i] = tab->col_var[i];
156         dup->row_var = isl_alloc_array(tab->ctx, int, tab->mat->n_row);
157         if (!dup->row_var)
158                 goto error;
159         for (i = 0; i < tab->n_row; ++i)
160                 dup->row_var[i] = tab->row_var[i];
161         dup->n_row = tab->n_row;
162         dup->n_con = tab->n_con;
163         dup->n_eq = tab->n_eq;
164         dup->max_con = tab->max_con;
165         dup->n_col = tab->n_col;
166         dup->n_var = tab->n_var;
167         dup->n_dead = tab->n_dead;
168         dup->n_redundant = tab->n_redundant;
169         dup->rational = tab->rational;
170         dup->empty = tab->empty;
171         dup->need_undo = 0;
172         dup->in_undo = 0;
173         dup->bottom.type = isl_tab_undo_bottom;
174         dup->bottom.next = NULL;
175         dup->top = &dup->bottom;
176         return dup;
177 error:
178         isl_tab_free(dup);
179         return NULL;
180 }
181
182 static struct isl_tab_var *var_from_index(struct isl_tab *tab, int i)
183 {
184         if (i >= 0)
185                 return &tab->var[i];
186         else
187                 return &tab->con[~i];
188 }
189
190 struct isl_tab_var *isl_tab_var_from_row(struct isl_tab *tab, int i)
191 {
192         return var_from_index(tab, tab->row_var[i]);
193 }
194
195 static struct isl_tab_var *var_from_col(struct isl_tab *tab, int i)
196 {
197         return var_from_index(tab, tab->col_var[i]);
198 }
199
200 /* Check if there are any upper bounds on column variable "var",
201  * i.e., non-negative rows where var appears with a negative coefficient.
202  * Return 1 if there are no such bounds.
203  */
204 static int max_is_manifestly_unbounded(struct isl_tab *tab,
205         struct isl_tab_var *var)
206 {
207         int i;
208
209         if (var->is_row)
210                 return 0;
211         for (i = tab->n_redundant; i < tab->n_row; ++i) {
212                 if (!isl_int_is_neg(tab->mat->row[i][2 + var->index]))
213                         continue;
214                 if (isl_tab_var_from_row(tab, i)->is_nonneg)
215                         return 0;
216         }
217         return 1;
218 }
219
220 /* Check if there are any lower bounds on column variable "var",
221  * i.e., non-negative rows where var appears with a positive coefficient.
222  * Return 1 if there are no such bounds.
223  */
224 static int min_is_manifestly_unbounded(struct isl_tab *tab,
225         struct isl_tab_var *var)
226 {
227         int i;
228
229         if (var->is_row)
230                 return 0;
231         for (i = tab->n_redundant; i < tab->n_row; ++i) {
232                 if (!isl_int_is_pos(tab->mat->row[i][2 + var->index]))
233                         continue;
234                 if (isl_tab_var_from_row(tab, i)->is_nonneg)
235                         return 0;
236         }
237         return 1;
238 }
239
240 /* Given the index of a column "c", return the index of a row
241  * that can be used to pivot the column in, with either an increase
242  * (sgn > 0) or a decrease (sgn < 0) of the corresponding variable.
243  * If "var" is not NULL, then the row returned will be different from
244  * the one associated with "var".
245  *
246  * Each row in the tableau is of the form
247  *
248  *      x_r = a_r0 + \sum_i a_ri x_i
249  *
250  * Only rows with x_r >= 0 and with the sign of a_ri opposite to "sgn"
251  * impose any limit on the increase or decrease in the value of x_c
252  * and this bound is equal to a_r0 / |a_rc|.  We are therefore looking
253  * for the row with the smallest (most stringent) such bound.
254  * Note that the common denominator of each row drops out of the fraction.
255  * To check if row j has a smaller bound than row r, i.e.,
256  * a_j0 / |a_jc| < a_r0 / |a_rc| or a_j0 |a_rc| < a_r0 |a_jc|,
257  * we check if -sign(a_jc) (a_j0 a_rc - a_r0 a_jc) < 0,
258  * where -sign(a_jc) is equal to "sgn".
259  */
260 static int pivot_row(struct isl_tab *tab,
261         struct isl_tab_var *var, int sgn, int c)
262 {
263         int j, r, tsgn;
264         isl_int t;
265
266         isl_int_init(t);
267         r = -1;
268         for (j = tab->n_redundant; j < tab->n_row; ++j) {
269                 if (var && j == var->index)
270                         continue;
271                 if (!isl_tab_var_from_row(tab, j)->is_nonneg)
272                         continue;
273                 if (sgn * isl_int_sgn(tab->mat->row[j][2 + c]) >= 0)
274                         continue;
275                 if (r < 0) {
276                         r = j;
277                         continue;
278                 }
279                 isl_int_mul(t, tab->mat->row[r][1], tab->mat->row[j][2 + c]);
280                 isl_int_submul(t, tab->mat->row[j][1], tab->mat->row[r][2 + c]);
281                 tsgn = sgn * isl_int_sgn(t);
282                 if (tsgn < 0 || (tsgn == 0 &&
283                                             tab->row_var[j] < tab->row_var[r]))
284                         r = j;
285         }
286         isl_int_clear(t);
287         return r;
288 }
289
290 /* Find a pivot (row and col) that will increase (sgn > 0) or decrease
291  * (sgn < 0) the value of row variable var.
292  * If not NULL, then skip_var is a row variable that should be ignored
293  * while looking for a pivot row.  It is usually equal to var.
294  *
295  * As the given row in the tableau is of the form
296  *
297  *      x_r = a_r0 + \sum_i a_ri x_i
298  *
299  * we need to find a column such that the sign of a_ri is equal to "sgn"
300  * (such that an increase in x_i will have the desired effect) or a
301  * column with a variable that may attain negative values.
302  * If a_ri is positive, then we need to move x_i in the same direction
303  * to obtain the desired effect.  Otherwise, x_i has to move in the
304  * opposite direction.
305  */
306 static void find_pivot(struct isl_tab *tab,
307         struct isl_tab_var *var, struct isl_tab_var *skip_var,
308         int sgn, int *row, int *col)
309 {
310         int j, r, c;
311         isl_int *tr;
312
313         *row = *col = -1;
314
315         isl_assert(tab->mat->ctx, var->is_row, return);
316         tr = tab->mat->row[var->index];
317
318         c = -1;
319         for (j = tab->n_dead; j < tab->n_col; ++j) {
320                 if (isl_int_is_zero(tr[2 + j]))
321                         continue;
322                 if (isl_int_sgn(tr[2 + j]) != sgn &&
323                     var_from_col(tab, j)->is_nonneg)
324                         continue;
325                 if (c < 0 || tab->col_var[j] < tab->col_var[c])
326                         c = j;
327         }
328         if (c < 0)
329                 return;
330
331         sgn *= isl_int_sgn(tr[2 + c]);
332         r = pivot_row(tab, skip_var, sgn, c);
333         *row = r < 0 ? var->index : r;
334         *col = c;
335 }
336
337 /* Return 1 if row "row" represents an obviously redundant inequality.
338  * This means
339  *      - it represents an inequality or a variable
340  *      - that is the sum of a non-negative sample value and a positive
341  *        combination of zero or more non-negative variables.
342  */
343 int isl_tab_row_is_redundant(struct isl_tab *tab, int row)
344 {
345         int i;
346
347         if (tab->row_var[row] < 0 && !isl_tab_var_from_row(tab, row)->is_nonneg)
348                 return 0;
349
350         if (isl_int_is_neg(tab->mat->row[row][1]))
351                 return 0;
352
353         for (i = tab->n_dead; i < tab->n_col; ++i) {
354                 if (isl_int_is_zero(tab->mat->row[row][2 + i]))
355                         continue;
356                 if (isl_int_is_neg(tab->mat->row[row][2 + i]))
357                         return 0;
358                 if (!var_from_col(tab, i)->is_nonneg)
359                         return 0;
360         }
361         return 1;
362 }
363
364 static void swap_rows(struct isl_tab *tab, int row1, int row2)
365 {
366         int t;
367         t = tab->row_var[row1];
368         tab->row_var[row1] = tab->row_var[row2];
369         tab->row_var[row2] = t;
370         isl_tab_var_from_row(tab, row1)->index = row1;
371         isl_tab_var_from_row(tab, row2)->index = row2;
372         tab->mat = isl_mat_swap_rows(tab->mat, row1, row2);
373 }
374
375 static void push_union(struct isl_tab *tab,
376         enum isl_tab_undo_type type, union isl_tab_undo_val u)
377 {
378         struct isl_tab_undo *undo;
379
380         if (!tab->need_undo)
381                 return;
382
383         undo = isl_alloc_type(tab->mat->ctx, struct isl_tab_undo);
384         if (!undo) {
385                 free_undo(tab);
386                 tab->top = NULL;
387                 return;
388         }
389         undo->type = type;
390         undo->u = u;
391         undo->next = tab->top;
392         tab->top = undo;
393 }
394
395 void isl_tab_push_var(struct isl_tab *tab,
396         enum isl_tab_undo_type type, struct isl_tab_var *var)
397 {
398         union isl_tab_undo_val u;
399         if (var->is_row)
400                 u.var_index = tab->row_var[var->index];
401         else
402                 u.var_index = tab->col_var[var->index];
403         push_union(tab, type, u);
404 }
405
406 void isl_tab_push(struct isl_tab *tab, enum isl_tab_undo_type type)
407 {
408         union isl_tab_undo_val u = { 0 };
409         push_union(tab, type, u);
410 }
411
412 /* Push a record on the undo stack describing the current basic
413  * variables, so that the this state can be restored during rollback.
414  */
415 void isl_tab_push_basis(struct isl_tab *tab)
416 {
417         int i;
418         union isl_tab_undo_val u;
419
420         u.col_var = isl_alloc_array(tab->mat->ctx, int, tab->n_col);
421         if (!u.col_var) {
422                 free_undo(tab);
423                 tab->top = NULL;
424                 return;
425         }
426         for (i = 0; i < tab->n_col; ++i)
427                 u.col_var[i] = tab->col_var[i];
428         push_union(tab, isl_tab_undo_saved_basis, u);
429 }
430
431 /* Mark row with index "row" as being redundant.
432  * If we may need to undo the operation or if the row represents
433  * a variable of the original problem, the row is kept,
434  * but no longer considered when looking for a pivot row.
435  * Otherwise, the row is simply removed.
436  *
437  * The row may be interchanged with some other row.  If it
438  * is interchanged with a later row, return 1.  Otherwise return 0.
439  * If the rows are checked in order in the calling function,
440  * then a return value of 1 means that the row with the given
441  * row number may now contain a different row that hasn't been checked yet.
442  */
443 int isl_tab_mark_redundant(struct isl_tab *tab, int row)
444 {
445         struct isl_tab_var *var = isl_tab_var_from_row(tab, row);
446         var->is_redundant = 1;
447         isl_assert(tab->mat->ctx, row >= tab->n_redundant, return);
448         if (tab->need_undo || tab->row_var[row] >= 0) {
449                 if (tab->row_var[row] >= 0 && !var->is_nonneg) {
450                         var->is_nonneg = 1;
451                         isl_tab_push_var(tab, isl_tab_undo_nonneg, var);
452                 }
453                 if (row != tab->n_redundant)
454                         swap_rows(tab, row, tab->n_redundant);
455                 isl_tab_push_var(tab, isl_tab_undo_redundant, var);
456                 tab->n_redundant++;
457                 return 0;
458         } else {
459                 if (row != tab->n_row - 1)
460                         swap_rows(tab, row, tab->n_row - 1);
461                 isl_tab_var_from_row(tab, tab->n_row - 1)->index = -1;
462                 tab->n_row--;
463                 return 1;
464         }
465 }
466
467 struct isl_tab *isl_tab_mark_empty(struct isl_tab *tab)
468 {
469         if (!tab->empty && tab->need_undo)
470                 isl_tab_push(tab, isl_tab_undo_empty);
471         tab->empty = 1;
472         return tab;
473 }
474
475 /* Given a row number "row" and a column number "col", pivot the tableau
476  * such that the associated variables are interchanged.
477  * The given row in the tableau expresses
478  *
479  *      x_r = a_r0 + \sum_i a_ri x_i
480  *
481  * or
482  *
483  *      x_c = 1/a_rc x_r - a_r0/a_rc + sum_{i \ne r} -a_ri/a_rc
484  *
485  * Substituting this equality into the other rows
486  *
487  *      x_j = a_j0 + \sum_i a_ji x_i
488  *
489  * with a_jc \ne 0, we obtain
490  *
491  *      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 
492  *
493  * The tableau
494  *
495  *      n_rc/d_r                n_ri/d_r
496  *      n_jc/d_j                n_ji/d_j
497  *
498  * where i is any other column and j is any other row,
499  * is therefore transformed into
500  *
501  * s(n_rc)d_r/|n_rc|            -s(n_rc)n_ri/|n_rc|
502  * 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)
503  *
504  * The transformation is performed along the following steps
505  *
506  *      d_r/n_rc                n_ri/n_rc
507  *      n_jc/d_j                n_ji/d_j
508  *
509  *      s(n_rc)d_r/|n_rc|       -s(n_rc)n_ri/|n_rc|
510  *      n_jc/d_j                n_ji/d_j
511  *
512  *      s(n_rc)d_r/|n_rc|       -s(n_rc)n_ri/|n_rc|
513  *      n_jc/(|n_rc| d_j)       n_ji/(|n_rc| d_j)
514  *
515  *      s(n_rc)d_r/|n_rc|       -s(n_rc)n_ri/|n_rc|
516  *      n_jc/(|n_rc| d_j)       (n_ji |n_rc|)/(|n_rc| d_j)
517  *
518  *      s(n_rc)d_r/|n_rc|       -s(n_rc)n_ri/|n_rc|
519  *      n_jc/(|n_rc| d_j)       (n_ji |n_rc| - s(n_rc)n_jc n_ri)/(|n_rc| d_j)
520  *
521  * s(n_rc)d_r/|n_rc|            -s(n_rc)n_ri/|n_rc|
522  * 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)
523  *
524  */
525 void isl_tab_pivot(struct isl_tab *tab, int row, int col)
526 {
527         int i, j;
528         int sgn;
529         int t;
530         struct isl_mat *mat = tab->mat;
531         struct isl_tab_var *var;
532
533         isl_int_swap(mat->row[row][0], mat->row[row][2 + col]);
534         sgn = isl_int_sgn(mat->row[row][0]);
535         if (sgn < 0) {
536                 isl_int_neg(mat->row[row][0], mat->row[row][0]);
537                 isl_int_neg(mat->row[row][2 + col], mat->row[row][2 + col]);
538         } else
539                 for (j = 0; j < 1 + tab->n_col; ++j) {
540                         if (j == 1 + col)
541                                 continue;
542                         isl_int_neg(mat->row[row][1 + j], mat->row[row][1 + j]);
543                 }
544         if (!isl_int_is_one(mat->row[row][0]))
545                 isl_seq_normalize(mat->row[row], 2 + tab->n_col);
546         for (i = 0; i < tab->n_row; ++i) {
547                 if (i == row)
548                         continue;
549                 if (isl_int_is_zero(mat->row[i][2 + col]))
550                         continue;
551                 isl_int_mul(mat->row[i][0], mat->row[i][0], mat->row[row][0]);
552                 for (j = 0; j < 1 + tab->n_col; ++j) {
553                         if (j == 1 + col)
554                                 continue;
555                         isl_int_mul(mat->row[i][1 + j],
556                                     mat->row[i][1 + j], mat->row[row][0]);
557                         isl_int_addmul(mat->row[i][1 + j],
558                                     mat->row[i][2 + col], mat->row[row][1 + j]);
559                 }
560                 isl_int_mul(mat->row[i][2 + col],
561                             mat->row[i][2 + col], mat->row[row][2 + col]);
562                 if (!isl_int_is_one(mat->row[i][0]))
563                         isl_seq_normalize(mat->row[i], 2 + tab->n_col);
564         }
565         t = tab->row_var[row];
566         tab->row_var[row] = tab->col_var[col];
567         tab->col_var[col] = t;
568         var = isl_tab_var_from_row(tab, row);
569         var->is_row = 1;
570         var->index = row;
571         var = var_from_col(tab, col);
572         var->is_row = 0;
573         var->index = col;
574         if (tab->in_undo)
575                 return;
576         for (i = tab->n_redundant; i < tab->n_row; ++i) {
577                 if (isl_int_is_zero(mat->row[i][2 + col]))
578                         continue;
579                 if (!isl_tab_var_from_row(tab, i)->frozen &&
580                     isl_tab_row_is_redundant(tab, i))
581                         if (isl_tab_mark_redundant(tab, i))
582                                 --i;
583         }
584 }
585
586 /* If "var" represents a column variable, then pivot is up (sgn > 0)
587  * or down (sgn < 0) to a row.  The variable is assumed not to be
588  * unbounded in the specified direction.
589  * If sgn = 0, then the variable is unbounded in both directions,
590  * and we pivot with any row we can find.
591  */
592 static void to_row(struct isl_tab *tab, struct isl_tab_var *var, int sign)
593 {
594         int r;
595
596         if (var->is_row)
597                 return;
598
599         if (sign == 0) {
600                 for (r = tab->n_redundant; r < tab->n_row; ++r)
601                         if (!isl_int_is_zero(tab->mat->row[r][2 + var->index]))
602                                 break;
603                 isl_assert(tab->mat->ctx, r < tab->n_row, return);
604         } else {
605                 r = pivot_row(tab, NULL, sign, var->index);
606                 isl_assert(tab->mat->ctx, r >= 0, return);
607         }
608
609         isl_tab_pivot(tab, r, var->index);
610 }
611
612 static void check_table(struct isl_tab *tab)
613 {
614         int i;
615
616         if (tab->empty)
617                 return;
618         for (i = 0; i < tab->n_row; ++i) {
619                 if (!isl_tab_var_from_row(tab, i)->is_nonneg)
620                         continue;
621                 assert(!isl_int_is_neg(tab->mat->row[i][1]));
622         }
623 }
624
625 /* Return the sign of the maximal value of "var".
626  * If the sign is not negative, then on return from this function,
627  * the sample value will also be non-negative.
628  *
629  * If "var" is manifestly unbounded wrt positive values, we are done.
630  * Otherwise, we pivot the variable up to a row if needed
631  * Then we continue pivoting down until either
632  *      - no more down pivots can be performed
633  *      - the sample value is positive
634  *      - the variable is pivoted into a manifestly unbounded column
635  */
636 static int sign_of_max(struct isl_tab *tab, struct isl_tab_var *var)
637 {
638         int row, col;
639
640         if (max_is_manifestly_unbounded(tab, var))
641                 return 1;
642         to_row(tab, var, 1);
643         while (!isl_int_is_pos(tab->mat->row[var->index][1])) {
644                 find_pivot(tab, var, var, 1, &row, &col);
645                 if (row == -1)
646                         return isl_int_sgn(tab->mat->row[var->index][1]);
647                 isl_tab_pivot(tab, row, col);
648                 if (!var->is_row) /* manifestly unbounded */
649                         return 1;
650         }
651         return 1;
652 }
653
654 /* Perform pivots until the row variable "var" has a non-negative
655  * sample value or until no more upward pivots can be performed.
656  * Return the sign of the sample value after the pivots have been
657  * performed.
658  */
659 static int restore_row(struct isl_tab *tab, struct isl_tab_var *var)
660 {
661         int row, col;
662
663         while (isl_int_is_neg(tab->mat->row[var->index][1])) {
664                 find_pivot(tab, var, var, 1, &row, &col);
665                 if (row == -1)
666                         break;
667                 isl_tab_pivot(tab, row, col);
668                 if (!var->is_row) /* manifestly unbounded */
669                         return 1;
670         }
671         return isl_int_sgn(tab->mat->row[var->index][1]);
672 }
673
674 /* Perform pivots until we are sure that the row variable "var"
675  * can attain non-negative values.  After return from this
676  * function, "var" is still a row variable, but its sample
677  * value may not be non-negative, even if the function returns 1.
678  */
679 static int at_least_zero(struct isl_tab *tab, struct isl_tab_var *var)
680 {
681         int row, col;
682
683         while (isl_int_is_neg(tab->mat->row[var->index][1])) {
684                 find_pivot(tab, var, var, 1, &row, &col);
685                 if (row == -1)
686                         break;
687                 if (row == var->index) /* manifestly unbounded */
688                         return 1;
689                 isl_tab_pivot(tab, row, col);
690         }
691         return !isl_int_is_neg(tab->mat->row[var->index][1]);
692 }
693
694 /* Return a negative value if "var" can attain negative values.
695  * Return a non-negative value otherwise.
696  *
697  * If "var" is manifestly unbounded wrt negative values, we are done.
698  * Otherwise, if var is in a column, we can pivot it down to a row.
699  * Then we continue pivoting down until either
700  *      - the pivot would result in a manifestly unbounded column
701  *        => we don't perform the pivot, but simply return -1
702  *      - no more down pivots can be performed
703  *      - the sample value is negative
704  * If the sample value becomes negative and the variable is supposed
705  * to be nonnegative, then we undo the last pivot.
706  * However, if the last pivot has made the pivoting variable
707  * obviously redundant, then it may have moved to another row.
708  * In that case we look for upward pivots until we reach a non-negative
709  * value again.
710  */
711 static int sign_of_min(struct isl_tab *tab, struct isl_tab_var *var)
712 {
713         int row, col;
714         struct isl_tab_var *pivot_var;
715
716         if (min_is_manifestly_unbounded(tab, var))
717                 return -1;
718         if (!var->is_row) {
719                 col = var->index;
720                 row = pivot_row(tab, NULL, -1, col);
721                 pivot_var = var_from_col(tab, col);
722                 isl_tab_pivot(tab, row, col);
723                 if (var->is_redundant)
724                         return 0;
725                 if (isl_int_is_neg(tab->mat->row[var->index][1])) {
726                         if (var->is_nonneg) {
727                                 if (!pivot_var->is_redundant &&
728                                     pivot_var->index == row)
729                                         isl_tab_pivot(tab, row, col);
730                                 else
731                                         restore_row(tab, var);
732                         }
733                         return -1;
734                 }
735         }
736         if (var->is_redundant)
737                 return 0;
738         while (!isl_int_is_neg(tab->mat->row[var->index][1])) {
739                 find_pivot(tab, var, var, -1, &row, &col);
740                 if (row == var->index)
741                         return -1;
742                 if (row == -1)
743                         return isl_int_sgn(tab->mat->row[var->index][1]);
744                 pivot_var = var_from_col(tab, col);
745                 isl_tab_pivot(tab, row, col);
746                 if (var->is_redundant)
747                         return 0;
748         }
749         if (var->is_nonneg) {
750                 /* pivot back to non-negative value */
751                 if (!pivot_var->is_redundant && pivot_var->index == row)
752                         isl_tab_pivot(tab, row, col);
753                 else
754                         restore_row(tab, var);
755         }
756         return -1;
757 }
758
759 /* Return 1 if "var" can attain values <= -1.
760  * Return 0 otherwise.
761  *
762  * The sample value of "var" is assumed to be non-negative when the
763  * the function is called and will be made non-negative again before
764  * the function returns.
765  */
766 int isl_tab_min_at_most_neg_one(struct isl_tab *tab, struct isl_tab_var *var)
767 {
768         int row, col;
769         struct isl_tab_var *pivot_var;
770
771         if (min_is_manifestly_unbounded(tab, var))
772                 return 1;
773         if (!var->is_row) {
774                 col = var->index;
775                 row = pivot_row(tab, NULL, -1, col);
776                 pivot_var = var_from_col(tab, col);
777                 isl_tab_pivot(tab, row, col);
778                 if (var->is_redundant)
779                         return 0;
780                 if (isl_int_is_neg(tab->mat->row[var->index][1]) &&
781                     isl_int_abs_ge(tab->mat->row[var->index][1],
782                                    tab->mat->row[var->index][0])) {
783                         if (var->is_nonneg) {
784                                 if (!pivot_var->is_redundant &&
785                                     pivot_var->index == row)
786                                         isl_tab_pivot(tab, row, col);
787                                 else
788                                         restore_row(tab, var);
789                         }
790                         return 1;
791                 }
792         }
793         if (var->is_redundant)
794                 return 0;
795         do {
796                 find_pivot(tab, var, var, -1, &row, &col);
797                 if (row == var->index)
798                         return 1;
799                 if (row == -1)
800                         return 0;
801                 pivot_var = var_from_col(tab, col);
802                 isl_tab_pivot(tab, row, col);
803                 if (var->is_redundant)
804                         return 0;
805         } while (!isl_int_is_neg(tab->mat->row[var->index][1]) ||
806                  isl_int_abs_lt(tab->mat->row[var->index][1],
807                                 tab->mat->row[var->index][0]));
808         if (var->is_nonneg) {
809                 /* pivot back to non-negative value */
810                 if (!pivot_var->is_redundant && pivot_var->index == row)
811                         isl_tab_pivot(tab, row, col);
812                 restore_row(tab, var);
813         }
814         return 1;
815 }
816
817 /* Return 1 if "var" can attain values >= 1.
818  * Return 0 otherwise.
819  */
820 static int at_least_one(struct isl_tab *tab, struct isl_tab_var *var)
821 {
822         int row, col;
823         isl_int *r;
824
825         if (max_is_manifestly_unbounded(tab, var))
826                 return 1;
827         to_row(tab, var, 1);
828         r = tab->mat->row[var->index];
829         while (isl_int_lt(r[1], r[0])) {
830                 find_pivot(tab, var, var, 1, &row, &col);
831                 if (row == -1)
832                         return isl_int_ge(r[1], r[0]);
833                 if (row == var->index) /* manifestly unbounded */
834                         return 1;
835                 isl_tab_pivot(tab, row, col);
836         }
837         return 1;
838 }
839
840 static void swap_cols(struct isl_tab *tab, int col1, int col2)
841 {
842         int t;
843         t = tab->col_var[col1];
844         tab->col_var[col1] = tab->col_var[col2];
845         tab->col_var[col2] = t;
846         var_from_col(tab, col1)->index = col1;
847         var_from_col(tab, col2)->index = col2;
848         tab->mat = isl_mat_swap_cols(tab->mat, 2 + col1, 2 + col2);
849 }
850
851 /* Mark column with index "col" as representing a zero variable.
852  * If we may need to undo the operation the column is kept,
853  * but no longer considered.
854  * Otherwise, the column is simply removed.
855  *
856  * The column may be interchanged with some other column.  If it
857  * is interchanged with a later column, return 1.  Otherwise return 0.
858  * If the columns are checked in order in the calling function,
859  * then a return value of 1 means that the column with the given
860  * column number may now contain a different column that
861  * hasn't been checked yet.
862  */
863 int isl_tab_kill_col(struct isl_tab *tab, int col)
864 {
865         var_from_col(tab, col)->is_zero = 1;
866         if (tab->need_undo) {
867                 isl_tab_push_var(tab, isl_tab_undo_zero, var_from_col(tab, col));
868                 if (col != tab->n_dead)
869                         swap_cols(tab, col, tab->n_dead);
870                 tab->n_dead++;
871                 return 0;
872         } else {
873                 if (col != tab->n_col - 1)
874                         swap_cols(tab, col, tab->n_col - 1);
875                 var_from_col(tab, tab->n_col - 1)->index = -1;
876                 tab->n_col--;
877                 return 1;
878         }
879 }
880
881 /* Row variable "var" is non-negative and cannot attain any values
882  * larger than zero.  This means that the coefficients of the unrestricted
883  * column variables are zero and that the coefficients of the non-negative
884  * column variables are zero or negative.
885  * Each of the non-negative variables with a negative coefficient can
886  * then also be written as the negative sum of non-negative variables
887  * and must therefore also be zero.
888  */
889 static void close_row(struct isl_tab *tab, struct isl_tab_var *var)
890 {
891         int j;
892         struct isl_mat *mat = tab->mat;
893
894         isl_assert(tab->mat->ctx, var->is_nonneg, return);
895         var->is_zero = 1;
896         for (j = tab->n_dead; j < tab->n_col; ++j) {
897                 if (isl_int_is_zero(mat->row[var->index][2 + j]))
898                         continue;
899                 isl_assert(tab->mat->ctx,
900                         isl_int_is_neg(mat->row[var->index][2 + j]), return);
901                 if (isl_tab_kill_col(tab, j))
902                         --j;
903         }
904         isl_tab_mark_redundant(tab, var->index);
905 }
906
907 /* Add a constraint to the tableau and allocate a row for it.
908  * Return the index into the constraint array "con".
909  */
910 int isl_tab_allocate_con(struct isl_tab *tab)
911 {
912         int r;
913
914         isl_assert(tab->mat->ctx, tab->n_row < tab->mat->n_row, return -1);
915
916         r = tab->n_con;
917         tab->con[r].index = tab->n_row;
918         tab->con[r].is_row = 1;
919         tab->con[r].is_nonneg = 0;
920         tab->con[r].is_zero = 0;
921         tab->con[r].is_redundant = 0;
922         tab->con[r].frozen = 0;
923         tab->row_var[tab->n_row] = ~r;
924
925         tab->n_row++;
926         tab->n_con++;
927         isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->con[r]);
928
929         return r;
930 }
931
932 /* Add a row to the tableau.  The row is given as an affine combination
933  * of the original variables and needs to be expressed in terms of the
934  * column variables.
935  *
936  * We add each term in turn.
937  * If r = n/d_r is the current sum and we need to add k x, then
938  *      if x is a column variable, we increase the numerator of
939  *              this column by k d_r
940  *      if x = f/d_x is a row variable, then the new representation of r is
941  *
942  *               n    k f   d_x/g n + d_r/g k f   m/d_r n + m/d_g k f
943  *              --- + --- = ------------------- = -------------------
944  *              d_r   d_r        d_r d_x/g                m
945  *
946  *      with g the gcd of d_r and d_x and m the lcm of d_r and d_x.
947  */
948 int isl_tab_add_row(struct isl_tab *tab, isl_int *line)
949 {
950         int i;
951         int r;
952         isl_int *row;
953         isl_int a, b;
954
955         r = isl_tab_allocate_con(tab);
956         if (r < 0)
957                 return -1;
958
959         isl_int_init(a);
960         isl_int_init(b);
961         row = tab->mat->row[tab->con[r].index];
962         isl_int_set_si(row[0], 1);
963         isl_int_set(row[1], line[0]);
964         isl_seq_clr(row + 2, tab->n_col);
965         for (i = 0; i < tab->n_var; ++i) {
966                 if (tab->var[i].is_zero)
967                         continue;
968                 if (tab->var[i].is_row) {
969                         isl_int_lcm(a,
970                                 row[0], tab->mat->row[tab->var[i].index][0]);
971                         isl_int_swap(a, row[0]);
972                         isl_int_divexact(a, row[0], a);
973                         isl_int_divexact(b,
974                                 row[0], tab->mat->row[tab->var[i].index][0]);
975                         isl_int_mul(b, b, line[1 + i]);
976                         isl_seq_combine(row + 1, a, row + 1,
977                             b, tab->mat->row[tab->var[i].index] + 1,
978                             1 + tab->n_col);
979                 } else
980                         isl_int_addmul(row[2 + tab->var[i].index],
981                                                         line[1 + i], row[0]);
982         }
983         isl_seq_normalize(row, 2 + tab->n_col);
984         isl_int_clear(a);
985         isl_int_clear(b);
986
987         return r;
988 }
989
990 static int drop_row(struct isl_tab *tab, int row)
991 {
992         isl_assert(tab->mat->ctx, ~tab->row_var[row] == tab->n_con - 1, return -1);
993         if (row != tab->n_row - 1)
994                 swap_rows(tab, row, tab->n_row - 1);
995         tab->n_row--;
996         tab->n_con--;
997         return 0;
998 }
999
1000 /* Add inequality "ineq" and check if it conflicts with the
1001  * previously added constraints or if it is obviously redundant.
1002  */
1003 struct isl_tab *isl_tab_add_ineq(struct isl_tab *tab, isl_int *ineq)
1004 {
1005         int r;
1006         int sgn;
1007
1008         if (!tab)
1009                 return NULL;
1010         r = isl_tab_add_row(tab, ineq);
1011         if (r < 0)
1012                 goto error;
1013         tab->con[r].is_nonneg = 1;
1014         isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]);
1015         if (isl_tab_row_is_redundant(tab, tab->con[r].index)) {
1016                 isl_tab_mark_redundant(tab, tab->con[r].index);
1017                 return tab;
1018         }
1019
1020         sgn = restore_row(tab, &tab->con[r]);
1021         if (sgn < 0)
1022                 return isl_tab_mark_empty(tab);
1023         if (tab->con[r].is_row && isl_tab_row_is_redundant(tab, tab->con[r].index))
1024                 isl_tab_mark_redundant(tab, tab->con[r].index);
1025         return tab;
1026 error:
1027         isl_tab_free(tab);
1028         return NULL;
1029 }
1030
1031 /* Pivot a non-negative variable down until it reaches the value zero
1032  * and then pivot the variable into a column position.
1033  */
1034 int to_col(struct isl_tab *tab, struct isl_tab_var *var)
1035 {
1036         int i;
1037         int row, col;
1038
1039         if (!var->is_row)
1040                 return;
1041
1042         while (isl_int_is_pos(tab->mat->row[var->index][1])) {
1043                 find_pivot(tab, var, NULL, -1, &row, &col);
1044                 isl_assert(tab->mat->ctx, row != -1, return -1);
1045                 isl_tab_pivot(tab, row, col);
1046                 if (!var->is_row)
1047                         return;
1048         }
1049
1050         for (i = tab->n_dead; i < tab->n_col; ++i)
1051                 if (!isl_int_is_zero(tab->mat->row[var->index][2 + i]))
1052                         break;
1053
1054         isl_assert(tab->mat->ctx, i < tab->n_col, return -1);
1055         isl_tab_pivot(tab, var->index, i);
1056
1057         return 0;
1058 }
1059
1060 /* We assume Gaussian elimination has been performed on the equalities.
1061  * The equalities can therefore never conflict.
1062  * Adding the equalities is currently only really useful for a later call
1063  * to isl_tab_ineq_type.
1064  */
1065 static struct isl_tab *add_eq(struct isl_tab *tab, isl_int *eq)
1066 {
1067         int i;
1068         int r;
1069
1070         if (!tab)
1071                 return NULL;
1072         r = isl_tab_add_row(tab, eq);
1073         if (r < 0)
1074                 goto error;
1075
1076         r = tab->con[r].index;
1077         i = isl_seq_first_non_zero(tab->mat->row[r] + 2 + tab->n_dead,
1078                                         tab->n_col - tab->n_dead);
1079         isl_assert(tab->mat->ctx, i >= 0, goto error);
1080         i += tab->n_dead;
1081         isl_tab_pivot(tab, r, i);
1082         isl_tab_kill_col(tab, i);
1083         tab->n_eq++;
1084
1085         return tab;
1086 error:
1087         isl_tab_free(tab);
1088         return NULL;
1089 }
1090
1091 /* Add an equality that is known to be valid for the given tableau.
1092  */
1093 struct isl_tab *isl_tab_add_valid_eq(struct isl_tab *tab, isl_int *eq)
1094 {
1095         struct isl_tab_var *var;
1096         int i;
1097         int r;
1098
1099         if (!tab)
1100                 return NULL;
1101         r = isl_tab_add_row(tab, eq);
1102         if (r < 0)
1103                 goto error;
1104
1105         var = &tab->con[r];
1106         r = var->index;
1107         if (isl_int_is_neg(tab->mat->row[r][1]))
1108                 isl_seq_neg(tab->mat->row[r] + 1, tab->mat->row[r] + 1,
1109                             1 + tab->n_col);
1110         var->is_nonneg = 1;
1111         if (to_col(tab, var) < 0)
1112                 goto error;
1113         var->is_nonneg = 0;
1114         isl_tab_kill_col(tab, var->index);
1115
1116         return tab;
1117 error:
1118         isl_tab_free(tab);
1119         return NULL;
1120 }
1121
1122 struct isl_tab *isl_tab_from_basic_map(struct isl_basic_map *bmap)
1123 {
1124         int i;
1125         struct isl_tab *tab;
1126
1127         if (!bmap)
1128                 return NULL;
1129         tab = isl_tab_alloc(bmap->ctx,
1130                             isl_basic_map_total_dim(bmap) + bmap->n_ineq + 1,
1131                             isl_basic_map_total_dim(bmap));
1132         if (!tab)
1133                 return NULL;
1134         tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
1135         if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1136                 return isl_tab_mark_empty(tab);
1137         for (i = 0; i < bmap->n_eq; ++i) {
1138                 tab = add_eq(tab, bmap->eq[i]);
1139                 if (!tab)
1140                         return tab;
1141         }
1142         for (i = 0; i < bmap->n_ineq; ++i) {
1143                 tab = isl_tab_add_ineq(tab, bmap->ineq[i]);
1144                 if (!tab || tab->empty)
1145                         return tab;
1146         }
1147         return tab;
1148 }
1149
1150 struct isl_tab *isl_tab_from_basic_set(struct isl_basic_set *bset)
1151 {
1152         return isl_tab_from_basic_map((struct isl_basic_map *)bset);
1153 }
1154
1155 /* Construct a tableau corresponding to the recession cone of "bmap".
1156  */
1157 struct isl_tab *isl_tab_from_recession_cone(struct isl_basic_map *bmap)
1158 {
1159         isl_int cst;
1160         int i;
1161         struct isl_tab *tab;
1162
1163         if (!bmap)
1164                 return NULL;
1165         tab = isl_tab_alloc(bmap->ctx, bmap->n_eq + bmap->n_ineq,
1166                                 isl_basic_map_total_dim(bmap));
1167         if (!tab)
1168                 return NULL;
1169         tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
1170
1171         isl_int_init(cst);
1172         for (i = 0; i < bmap->n_eq; ++i) {
1173                 isl_int_swap(bmap->eq[i][0], cst);
1174                 tab = add_eq(tab, bmap->eq[i]);
1175                 isl_int_swap(bmap->eq[i][0], cst);
1176                 if (!tab)
1177                         goto done;
1178         }
1179         for (i = 0; i < bmap->n_ineq; ++i) {
1180                 int r;
1181                 isl_int_swap(bmap->ineq[i][0], cst);
1182                 r = isl_tab_add_row(tab, bmap->ineq[i]);
1183                 isl_int_swap(bmap->ineq[i][0], cst);
1184                 if (r < 0)
1185                         goto error;
1186                 tab->con[r].is_nonneg = 1;
1187                 isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]);
1188         }
1189 done:
1190         isl_int_clear(cst);
1191         return tab;
1192 error:
1193         isl_int_clear(cst);
1194         isl_tab_free(tab);
1195         return NULL;
1196 }
1197
1198 /* Assuming "tab" is the tableau of a cone, check if the cone is
1199  * bounded, i.e., if it is empty or only contains the origin.
1200  */
1201 int isl_tab_cone_is_bounded(struct isl_tab *tab)
1202 {
1203         int i;
1204
1205         if (!tab)
1206                 return -1;
1207         if (tab->empty)
1208                 return 1;
1209         if (tab->n_dead == tab->n_col)
1210                 return 1;
1211
1212         for (;;) {
1213                 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1214                         struct isl_tab_var *var;
1215                         var = isl_tab_var_from_row(tab, i);
1216                         if (!var->is_nonneg)
1217                                 continue;
1218                         if (sign_of_max(tab, var) != 0)
1219                                 return 0;
1220                         close_row(tab, var);
1221                         break;
1222                 }
1223                 if (tab->n_dead == tab->n_col)
1224                         return 1;
1225                 if (i == tab->n_row)
1226                         return 0;
1227         }
1228 }
1229
1230 int isl_tab_sample_is_integer(struct isl_tab *tab)
1231 {
1232         int i;
1233
1234         if (!tab)
1235                 return -1;
1236
1237         for (i = 0; i < tab->n_var; ++i) {
1238                 int row;
1239                 if (!tab->var[i].is_row)
1240                         continue;
1241                 row = tab->var[i].index;
1242                 if (!isl_int_is_divisible_by(tab->mat->row[row][1],
1243                                                 tab->mat->row[row][0]))
1244                         return 0;
1245         }
1246         return 1;
1247 }
1248
1249 static struct isl_vec *extract_integer_sample(struct isl_tab *tab)
1250 {
1251         int i;
1252         struct isl_vec *vec;
1253
1254         vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
1255         if (!vec)
1256                 return NULL;
1257
1258         isl_int_set_si(vec->block.data[0], 1);
1259         for (i = 0; i < tab->n_var; ++i) {
1260                 if (!tab->var[i].is_row)
1261                         isl_int_set_si(vec->block.data[1 + i], 0);
1262                 else {
1263                         int row = tab->var[i].index;
1264                         isl_int_divexact(vec->block.data[1 + i],
1265                                 tab->mat->row[row][1], tab->mat->row[row][0]);
1266                 }
1267         }
1268
1269         return vec;
1270 }
1271
1272 struct isl_vec *isl_tab_get_sample_value(struct isl_tab *tab)
1273 {
1274         int i;
1275         struct isl_vec *vec;
1276         isl_int m;
1277
1278         if (!tab)
1279                 return NULL;
1280
1281         vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
1282         if (!vec)
1283                 return NULL;
1284
1285         isl_int_init(m);
1286
1287         isl_int_set_si(vec->block.data[0], 1);
1288         for (i = 0; i < tab->n_var; ++i) {
1289                 int row;
1290                 if (!tab->var[i].is_row) {
1291                         isl_int_set_si(vec->block.data[1 + i], 0);
1292                         continue;
1293                 }
1294                 row = tab->var[i].index;
1295                 isl_int_gcd(m, vec->block.data[0], tab->mat->row[row][0]);
1296                 isl_int_divexact(m, tab->mat->row[row][0], m);
1297                 isl_seq_scale(vec->block.data, vec->block.data, m, 1 + i);
1298                 isl_int_divexact(m, vec->block.data[0], tab->mat->row[row][0]);
1299                 isl_int_mul(vec->block.data[1 + i], m, tab->mat->row[row][1]);
1300         }
1301         isl_seq_normalize(vec->block.data, vec->size);
1302
1303         isl_int_clear(m);
1304         return vec;
1305 }
1306
1307 /* Update "bmap" based on the results of the tableau "tab".
1308  * In particular, implicit equalities are made explicit, redundant constraints
1309  * are removed and if the sample value happens to be integer, it is stored
1310  * in "bmap" (unless "bmap" already had an integer sample).
1311  *
1312  * The tableau is assumed to have been created from "bmap" using
1313  * isl_tab_from_basic_map.
1314  */
1315 struct isl_basic_map *isl_basic_map_update_from_tab(struct isl_basic_map *bmap,
1316         struct isl_tab *tab)
1317 {
1318         int i;
1319         unsigned n_eq;
1320
1321         if (!bmap)
1322                 return NULL;
1323         if (!tab)
1324                 return bmap;
1325
1326         n_eq = tab->n_eq;
1327         if (tab->empty)
1328                 bmap = isl_basic_map_set_to_empty(bmap);
1329         else
1330                 for (i = bmap->n_ineq - 1; i >= 0; --i) {
1331                         if (isl_tab_is_equality(tab, n_eq + i))
1332                                 isl_basic_map_inequality_to_equality(bmap, i);
1333                         else if (isl_tab_is_redundant(tab, n_eq + i))
1334                                 isl_basic_map_drop_inequality(bmap, i);
1335                 }
1336         if (!tab->rational &&
1337             !bmap->sample && isl_tab_sample_is_integer(tab))
1338                 bmap->sample = extract_integer_sample(tab);
1339         return bmap;
1340 }
1341
1342 struct isl_basic_set *isl_basic_set_update_from_tab(struct isl_basic_set *bset,
1343         struct isl_tab *tab)
1344 {
1345         return (struct isl_basic_set *)isl_basic_map_update_from_tab(
1346                 (struct isl_basic_map *)bset, tab);
1347 }
1348
1349 /* Given a non-negative variable "var", add a new non-negative variable
1350  * that is the opposite of "var", ensuring that var can only attain the
1351  * value zero.
1352  * If var = n/d is a row variable, then the new variable = -n/d.
1353  * If var is a column variables, then the new variable = -var.
1354  * If the new variable cannot attain non-negative values, then
1355  * the resulting tableau is empty.
1356  * Otherwise, we know the value will be zero and we close the row.
1357  */
1358 static struct isl_tab *cut_to_hyperplane(struct isl_tab *tab,
1359         struct isl_tab_var *var)
1360 {
1361         unsigned r;
1362         isl_int *row;
1363         int sgn;
1364
1365         if (isl_tab_extend_cons(tab, 1) < 0)
1366                 goto error;
1367
1368         r = tab->n_con;
1369         tab->con[r].index = tab->n_row;
1370         tab->con[r].is_row = 1;
1371         tab->con[r].is_nonneg = 0;
1372         tab->con[r].is_zero = 0;
1373         tab->con[r].is_redundant = 0;
1374         tab->con[r].frozen = 0;
1375         tab->row_var[tab->n_row] = ~r;
1376         row = tab->mat->row[tab->n_row];
1377
1378         if (var->is_row) {
1379                 isl_int_set(row[0], tab->mat->row[var->index][0]);
1380                 isl_seq_neg(row + 1,
1381                             tab->mat->row[var->index] + 1, 1 + tab->n_col);
1382         } else {
1383                 isl_int_set_si(row[0], 1);
1384                 isl_seq_clr(row + 1, 1 + tab->n_col);
1385                 isl_int_set_si(row[2 + var->index], -1);
1386         }
1387
1388         tab->n_row++;
1389         tab->n_con++;
1390         isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->con[r]);
1391
1392         sgn = sign_of_max(tab, &tab->con[r]);
1393         if (sgn < 0)
1394                 return isl_tab_mark_empty(tab);
1395         tab->con[r].is_nonneg = 1;
1396         isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]);
1397         /* sgn == 0 */
1398         close_row(tab, &tab->con[r]);
1399
1400         return tab;
1401 error:
1402         isl_tab_free(tab);
1403         return NULL;
1404 }
1405
1406 /* Given a tableau "tab" and an inequality constraint "con" of the tableau,
1407  * relax the inequality by one.  That is, the inequality r >= 0 is replaced
1408  * by r' = r + 1 >= 0.
1409  * If r is a row variable, we simply increase the constant term by one
1410  * (taking into account the denominator).
1411  * If r is a column variable, then we need to modify each row that
1412  * refers to r = r' - 1 by substituting this equality, effectively
1413  * subtracting the coefficient of the column from the constant.
1414  */
1415 struct isl_tab *isl_tab_relax(struct isl_tab *tab, int con)
1416 {
1417         struct isl_tab_var *var;
1418         if (!tab)
1419                 return NULL;
1420
1421         var = &tab->con[con];
1422
1423         if (!var->is_row && !max_is_manifestly_unbounded(tab, var))
1424                 to_row(tab, var, 1);
1425
1426         if (var->is_row)
1427                 isl_int_add(tab->mat->row[var->index][1],
1428                     tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
1429         else {
1430                 int i;
1431
1432                 for (i = 0; i < tab->n_row; ++i) {
1433                         if (isl_int_is_zero(tab->mat->row[i][2 + var->index]))
1434                                 continue;
1435                         isl_int_sub(tab->mat->row[i][1], tab->mat->row[i][1],
1436                             tab->mat->row[i][2 + var->index]);
1437                 }
1438
1439         }
1440
1441         isl_tab_push_var(tab, isl_tab_undo_relax, var);
1442
1443         return tab;
1444 }
1445
1446 struct isl_tab *isl_tab_select_facet(struct isl_tab *tab, int con)
1447 {
1448         if (!tab)
1449                 return NULL;
1450
1451         return cut_to_hyperplane(tab, &tab->con[con]);
1452 }
1453
1454 static int may_be_equality(struct isl_tab *tab, int row)
1455 {
1456         return (tab->rational ? isl_int_is_zero(tab->mat->row[row][1])
1457                               : isl_int_lt(tab->mat->row[row][1],
1458                                             tab->mat->row[row][0])) &&
1459                 isl_seq_first_non_zero(tab->mat->row[row] + 2 + tab->n_dead,
1460                                         tab->n_col - tab->n_dead) != -1;
1461 }
1462
1463 /* Check for (near) equalities among the constraints.
1464  * A constraint is an equality if it is non-negative and if
1465  * its maximal value is either
1466  *      - zero (in case of rational tableaus), or
1467  *      - strictly less than 1 (in case of integer tableaus)
1468  *
1469  * We first mark all non-redundant and non-dead variables that
1470  * are not frozen and not obviously not an equality.
1471  * Then we iterate over all marked variables if they can attain
1472  * any values larger than zero or at least one.
1473  * If the maximal value is zero, we mark any column variables
1474  * that appear in the row as being zero and mark the row as being redundant.
1475  * Otherwise, if the maximal value is strictly less than one (and the
1476  * tableau is integer), then we restrict the value to being zero
1477  * by adding an opposite non-negative variable.
1478  */
1479 struct isl_tab *isl_tab_detect_equalities(struct isl_tab *tab)
1480 {
1481         int i;
1482         unsigned n_marked;
1483
1484         if (!tab)
1485                 return NULL;
1486         if (tab->empty)
1487                 return tab;
1488         if (tab->n_dead == tab->n_col)
1489                 return tab;
1490
1491         n_marked = 0;
1492         for (i = tab->n_redundant; i < tab->n_row; ++i) {
1493                 struct isl_tab_var *var = isl_tab_var_from_row(tab, i);
1494                 var->marked = !var->frozen && var->is_nonneg &&
1495                         may_be_equality(tab, i);
1496                 if (var->marked)
1497                         n_marked++;
1498         }
1499         for (i = tab->n_dead; i < tab->n_col; ++i) {
1500                 struct isl_tab_var *var = var_from_col(tab, i);
1501                 var->marked = !var->frozen && var->is_nonneg;
1502                 if (var->marked)
1503                         n_marked++;
1504         }
1505         while (n_marked) {
1506                 struct isl_tab_var *var;
1507                 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1508                         var = isl_tab_var_from_row(tab, i);
1509                         if (var->marked)
1510                                 break;
1511                 }
1512                 if (i == tab->n_row) {
1513                         for (i = tab->n_dead; i < tab->n_col; ++i) {
1514                                 var = var_from_col(tab, i);
1515                                 if (var->marked)
1516                                         break;
1517                         }
1518                         if (i == tab->n_col)
1519                                 break;
1520                 }
1521                 var->marked = 0;
1522                 n_marked--;
1523                 if (sign_of_max(tab, var) == 0)
1524                         close_row(tab, var);
1525                 else if (!tab->rational && !at_least_one(tab, var)) {
1526                         tab = cut_to_hyperplane(tab, var);
1527                         return isl_tab_detect_equalities(tab);
1528                 }
1529                 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1530                         var = isl_tab_var_from_row(tab, i);
1531                         if (!var->marked)
1532                                 continue;
1533                         if (may_be_equality(tab, i))
1534                                 continue;
1535                         var->marked = 0;
1536                         n_marked--;
1537                 }
1538         }
1539
1540         return tab;
1541 }
1542
1543 /* Check for (near) redundant constraints.
1544  * A constraint is redundant if it is non-negative and if
1545  * its minimal value (temporarily ignoring the non-negativity) is either
1546  *      - zero (in case of rational tableaus), or
1547  *      - strictly larger than -1 (in case of integer tableaus)
1548  *
1549  * We first mark all non-redundant and non-dead variables that
1550  * are not frozen and not obviously negatively unbounded.
1551  * Then we iterate over all marked variables if they can attain
1552  * any values smaller than zero or at most negative one.
1553  * If not, we mark the row as being redundant (assuming it hasn't
1554  * been detected as being obviously redundant in the mean time).
1555  */
1556 struct isl_tab *isl_tab_detect_redundant(struct isl_tab *tab)
1557 {
1558         int i;
1559         unsigned n_marked;
1560
1561         if (!tab)
1562                 return NULL;
1563         if (tab->empty)
1564                 return tab;
1565         if (tab->n_redundant == tab->n_row)
1566                 return tab;
1567
1568         n_marked = 0;
1569         for (i = tab->n_redundant; i < tab->n_row; ++i) {
1570                 struct isl_tab_var *var = isl_tab_var_from_row(tab, i);
1571                 var->marked = !var->frozen && var->is_nonneg;
1572                 if (var->marked)
1573                         n_marked++;
1574         }
1575         for (i = tab->n_dead; i < tab->n_col; ++i) {
1576                 struct isl_tab_var *var = var_from_col(tab, i);
1577                 var->marked = !var->frozen && var->is_nonneg &&
1578                         !min_is_manifestly_unbounded(tab, var);
1579                 if (var->marked)
1580                         n_marked++;
1581         }
1582         while (n_marked) {
1583                 struct isl_tab_var *var;
1584                 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1585                         var = isl_tab_var_from_row(tab, i);
1586                         if (var->marked)
1587                                 break;
1588                 }
1589                 if (i == tab->n_row) {
1590                         for (i = tab->n_dead; i < tab->n_col; ++i) {
1591                                 var = var_from_col(tab, i);
1592                                 if (var->marked)
1593                                         break;
1594                         }
1595                         if (i == tab->n_col)
1596                                 break;
1597                 }
1598                 var->marked = 0;
1599                 n_marked--;
1600                 if ((tab->rational ? (sign_of_min(tab, var) >= 0)
1601                                    : !isl_tab_min_at_most_neg_one(tab, var)) &&
1602                     !var->is_redundant)
1603                         isl_tab_mark_redundant(tab, var->index);
1604                 for (i = tab->n_dead; i < tab->n_col; ++i) {
1605                         var = var_from_col(tab, i);
1606                         if (!var->marked)
1607                                 continue;
1608                         if (!min_is_manifestly_unbounded(tab, var))
1609                                 continue;
1610                         var->marked = 0;
1611                         n_marked--;
1612                 }
1613         }
1614
1615         return tab;
1616 }
1617
1618 int isl_tab_is_equality(struct isl_tab *tab, int con)
1619 {
1620         int row;
1621
1622         if (!tab)
1623                 return -1;
1624         if (tab->con[con].is_zero)
1625                 return 1;
1626         if (tab->con[con].is_redundant)
1627                 return 0;
1628         if (!tab->con[con].is_row)
1629                 return tab->con[con].index < tab->n_dead;
1630
1631         row = tab->con[con].index;
1632
1633         return isl_int_is_zero(tab->mat->row[row][1]) &&
1634                 isl_seq_first_non_zero(tab->mat->row[row] + 2 + tab->n_dead,
1635                                         tab->n_col - tab->n_dead) == -1;
1636 }
1637
1638 /* Return the minimial value of the affine expression "f" with denominator
1639  * "denom" in *opt, *opt_denom, assuming the tableau is not empty and
1640  * the expression cannot attain arbitrarily small values.
1641  * If opt_denom is NULL, then *opt is rounded up to the nearest integer.
1642  * The return value reflects the nature of the result (empty, unbounded,
1643  * minmimal value returned in *opt).
1644  */
1645 enum isl_lp_result isl_tab_min(struct isl_tab *tab,
1646         isl_int *f, isl_int denom, isl_int *opt, isl_int *opt_denom,
1647         unsigned flags)
1648 {
1649         int r;
1650         enum isl_lp_result res = isl_lp_ok;
1651         struct isl_tab_var *var;
1652         struct isl_tab_undo *snap;
1653
1654         if (tab->empty)
1655                 return isl_lp_empty;
1656
1657         snap = isl_tab_snap(tab);
1658         r = isl_tab_add_row(tab, f);
1659         if (r < 0)
1660                 return isl_lp_error;
1661         var = &tab->con[r];
1662         isl_int_mul(tab->mat->row[var->index][0],
1663                     tab->mat->row[var->index][0], denom);
1664         for (;;) {
1665                 int row, col;
1666                 find_pivot(tab, var, var, -1, &row, &col);
1667                 if (row == var->index) {
1668                         res = isl_lp_unbounded;
1669                         break;
1670                 }
1671                 if (row == -1)
1672                         break;
1673                 isl_tab_pivot(tab, row, col);
1674         }
1675         if (isl_tab_rollback(tab, snap) < 0)
1676                 return isl_lp_error;
1677         if (ISL_FL_ISSET(flags, ISL_TAB_SAVE_DUAL)) {
1678                 int i;
1679
1680                 isl_vec_free(tab->dual);
1681                 tab->dual = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_con);
1682                 if (!tab->dual)
1683                         return isl_lp_error;
1684                 isl_int_set(tab->dual->el[0], tab->mat->row[var->index][0]);
1685                 for (i = 0; i < tab->n_con; ++i) {
1686                         if (tab->con[i].is_row)
1687                                 isl_int_set_si(tab->dual->el[1 + i], 0);
1688                         else {
1689                                 int pos = 2 + tab->con[i].index;
1690                                 isl_int_set(tab->dual->el[1 + i],
1691                                             tab->mat->row[var->index][pos]);
1692                         }
1693                 }
1694         }
1695         if (res == isl_lp_ok) {
1696                 if (opt_denom) {
1697                         isl_int_set(*opt, tab->mat->row[var->index][1]);
1698                         isl_int_set(*opt_denom, tab->mat->row[var->index][0]);
1699                 } else
1700                         isl_int_cdiv_q(*opt, tab->mat->row[var->index][1],
1701                                              tab->mat->row[var->index][0]);
1702         }
1703         return res;
1704 }
1705
1706 int isl_tab_is_redundant(struct isl_tab *tab, int con)
1707 {
1708         int row;
1709         unsigned n_col;
1710
1711         if (!tab)
1712                 return -1;
1713         if (tab->con[con].is_zero)
1714                 return 0;
1715         if (tab->con[con].is_redundant)
1716                 return 1;
1717         return tab->con[con].is_row && tab->con[con].index < tab->n_redundant;
1718 }
1719
1720 /* Take a snapshot of the tableau that can be restored by s call to
1721  * isl_tab_rollback.
1722  */
1723 struct isl_tab_undo *isl_tab_snap(struct isl_tab *tab)
1724 {
1725         if (!tab)
1726                 return NULL;
1727         tab->need_undo = 1;
1728         return tab->top;
1729 }
1730
1731 /* Undo the operation performed by isl_tab_relax.
1732  */
1733 static void unrelax(struct isl_tab *tab, struct isl_tab_var *var)
1734 {
1735         if (!var->is_row && !max_is_manifestly_unbounded(tab, var))
1736                 to_row(tab, var, 1);
1737
1738         if (var->is_row)
1739                 isl_int_sub(tab->mat->row[var->index][1],
1740                     tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
1741         else {
1742                 int i;
1743
1744                 for (i = 0; i < tab->n_row; ++i) {
1745                         if (isl_int_is_zero(tab->mat->row[i][2 + var->index]))
1746                                 continue;
1747                         isl_int_add(tab->mat->row[i][1], tab->mat->row[i][1],
1748                             tab->mat->row[i][2 + var->index]);
1749                 }
1750
1751         }
1752 }
1753
1754 static void perform_undo_var(struct isl_tab *tab, struct isl_tab_undo *undo)
1755 {
1756         struct isl_tab_var *var = var_from_index(tab, undo->u.var_index);
1757         switch(undo->type) {
1758         case isl_tab_undo_nonneg:
1759                 var->is_nonneg = 0;
1760                 break;
1761         case isl_tab_undo_redundant:
1762                 var->is_redundant = 0;
1763                 tab->n_redundant--;
1764                 break;
1765         case isl_tab_undo_zero:
1766                 var->is_zero = 0;
1767                 tab->n_dead--;
1768                 break;
1769         case isl_tab_undo_allocate:
1770                 if (!var->is_row) {
1771                         if (!max_is_manifestly_unbounded(tab, var))
1772                                 to_row(tab, var, 1);
1773                         else if (!min_is_manifestly_unbounded(tab, var))
1774                                 to_row(tab, var, -1);
1775                         else
1776                                 to_row(tab, var, 0);
1777                 }
1778                 drop_row(tab, var->index);
1779                 break;
1780         case isl_tab_undo_relax:
1781                 unrelax(tab, var);
1782                 break;
1783         }
1784 }
1785
1786 /* Restore the tableau to the state where the basic variables
1787  * are those in "col_var".
1788  * We first construct a list of variables that are currently in
1789  * the basis, but shouldn't.  Then we iterate over all variables
1790  * that should be in the basis and for each one that is currently
1791  * not in the basis, we exchange it with one of the elements of the
1792  * list constructed before.
1793  * We can always find an appropriate variable to pivot with because
1794  * the current basis is mapped to the old basis by a non-singular
1795  * matrix and so we can never end up with a zero row.
1796  */
1797 static int restore_basis(struct isl_tab *tab, int *col_var)
1798 {
1799         int i, j;
1800         int n_extra = 0;
1801         int *extra = NULL;      /* current columns that contain bad stuff */
1802         unsigned off = 2;
1803
1804         extra = isl_alloc_array(tab->mat->ctx, int, tab->n_col);
1805         if (!extra)
1806                 goto error;
1807         for (i = 0; i < tab->n_col; ++i) {
1808                 for (j = 0; j < tab->n_col; ++j)
1809                         if (tab->col_var[i] == col_var[j])
1810                                 break;
1811                 if (j < tab->n_col)
1812                         continue;
1813                 extra[n_extra++] = i;
1814         }
1815         for (i = 0; i < tab->n_col && n_extra > 0; ++i) {
1816                 struct isl_tab_var *var;
1817                 int row;
1818
1819                 for (j = 0; j < tab->n_col; ++j)
1820                         if (col_var[i] == tab->col_var[j])
1821                                 break;
1822                 if (j < tab->n_col)
1823                         continue;
1824                 var = var_from_index(tab, col_var[i]);
1825                 row = var->index;
1826                 for (j = 0; j < n_extra; ++j)
1827                         if (!isl_int_is_zero(tab->mat->row[row][off+extra[j]]))
1828                                 break;
1829                 isl_assert(tab->mat->ctx, j < n_extra, goto error);
1830                 isl_tab_pivot(tab, row, extra[j]);
1831                 extra[j] = extra[--n_extra];
1832         }
1833
1834         free(extra);
1835         free(col_var);
1836         return 0;
1837 error:
1838         free(extra);
1839         free(col_var);
1840         return -1;
1841 }
1842
1843 static int perform_undo(struct isl_tab *tab, struct isl_tab_undo *undo)
1844 {
1845         switch (undo->type) {
1846         case isl_tab_undo_empty:
1847                 tab->empty = 0;
1848                 break;
1849         case isl_tab_undo_nonneg:
1850         case isl_tab_undo_redundant:
1851         case isl_tab_undo_zero:
1852         case isl_tab_undo_allocate:
1853         case isl_tab_undo_relax:
1854                 perform_undo_var(tab, undo);
1855                 break;
1856         case isl_tab_undo_saved_basis:
1857                 if (restore_basis(tab, undo->u.col_var) < 0)
1858                         return -1;
1859                 break;
1860         default:
1861                 isl_assert(tab->mat->ctx, 0, return -1);
1862         }
1863         return 0;
1864 }
1865
1866 /* Return the tableau to the state it was in when the snapshot "snap"
1867  * was taken.
1868  */
1869 int isl_tab_rollback(struct isl_tab *tab, struct isl_tab_undo *snap)
1870 {
1871         struct isl_tab_undo *undo, *next;
1872
1873         if (!tab)
1874                 return -1;
1875
1876         tab->in_undo = 1;
1877         for (undo = tab->top; undo && undo != &tab->bottom; undo = next) {
1878                 next = undo->next;
1879                 if (undo == snap)
1880                         break;
1881                 if (perform_undo(tab, undo) < 0) {
1882                         free_undo(tab);
1883                         tab->in_undo = 0;
1884                         return -1;
1885                 }
1886                 free(undo);
1887         }
1888         tab->in_undo = 0;
1889         tab->top = undo;
1890         if (!undo)
1891                 return -1;
1892         return 0;
1893 }
1894
1895 /* The given row "row" represents an inequality violated by all
1896  * points in the tableau.  Check for some special cases of such
1897  * separating constraints.
1898  * In particular, if the row has been reduced to the constant -1,
1899  * then we know the inequality is adjacent (but opposite) to
1900  * an equality in the tableau.
1901  * If the row has been reduced to r = -1 -r', with r' an inequality
1902  * of the tableau, then the inequality is adjacent (but opposite)
1903  * to the inequality r'.
1904  */
1905 static enum isl_ineq_type separation_type(struct isl_tab *tab, unsigned row)
1906 {
1907         int pos;
1908
1909         if (tab->rational)
1910                 return isl_ineq_separate;
1911
1912         if (!isl_int_is_one(tab->mat->row[row][0]))
1913                 return isl_ineq_separate;
1914         if (!isl_int_is_negone(tab->mat->row[row][1]))
1915                 return isl_ineq_separate;
1916
1917         pos = isl_seq_first_non_zero(tab->mat->row[row] + 2 + tab->n_dead,
1918                                         tab->n_col - tab->n_dead);
1919         if (pos == -1)
1920                 return isl_ineq_adj_eq;
1921
1922         if (!isl_int_is_negone(tab->mat->row[row][2 + tab->n_dead + pos]))
1923                 return isl_ineq_separate;
1924
1925         pos = isl_seq_first_non_zero(
1926                         tab->mat->row[row] + 2 + tab->n_dead + pos + 1,
1927                         tab->n_col - tab->n_dead - pos - 1);
1928
1929         return pos == -1 ? isl_ineq_adj_ineq : isl_ineq_separate;
1930 }
1931
1932 /* Check the effect of inequality "ineq" on the tableau "tab".
1933  * The result may be
1934  *      isl_ineq_redundant:     satisfied by all points in the tableau
1935  *      isl_ineq_separate:      satisfied by no point in the tableau
1936  *      isl_ineq_cut:           satisfied by some by not all points
1937  *      isl_ineq_adj_eq:        adjacent to an equality
1938  *      isl_ineq_adj_ineq:      adjacent to an inequality.
1939  */
1940 enum isl_ineq_type isl_tab_ineq_type(struct isl_tab *tab, isl_int *ineq)
1941 {
1942         enum isl_ineq_type type = isl_ineq_error;
1943         struct isl_tab_undo *snap = NULL;
1944         int con;
1945         int row;
1946
1947         if (!tab)
1948                 return isl_ineq_error;
1949
1950         if (isl_tab_extend_cons(tab, 1) < 0)
1951                 return isl_ineq_error;
1952
1953         snap = isl_tab_snap(tab);
1954
1955         con = isl_tab_add_row(tab, ineq);
1956         if (con < 0)
1957                 goto error;
1958
1959         row = tab->con[con].index;
1960         if (isl_tab_row_is_redundant(tab, row))
1961                 type = isl_ineq_redundant;
1962         else if (isl_int_is_neg(tab->mat->row[row][1]) &&
1963                  (tab->rational ||
1964                     isl_int_abs_ge(tab->mat->row[row][1],
1965                                    tab->mat->row[row][0]))) {
1966                 if (at_least_zero(tab, &tab->con[con]))
1967                         type = isl_ineq_cut;
1968                 else
1969                         type = separation_type(tab, row);
1970         } else if (tab->rational ? (sign_of_min(tab, &tab->con[con]) < 0)
1971                              : isl_tab_min_at_most_neg_one(tab, &tab->con[con]))
1972                 type = isl_ineq_cut;
1973         else
1974                 type = isl_ineq_redundant;
1975
1976         if (isl_tab_rollback(tab, snap))
1977                 return isl_ineq_error;
1978         return type;
1979 error:
1980         isl_tab_rollback(tab, snap);
1981         return isl_ineq_error;
1982 }
1983
1984 void isl_tab_dump(struct isl_tab *tab, FILE *out, int indent)
1985 {
1986         unsigned r, c;
1987         int i;
1988
1989         if (!tab) {
1990                 fprintf(out, "%*snull tab\n", indent, "");
1991                 return;
1992         }
1993         fprintf(out, "%*sn_redundant: %d, n_dead: %d", indent, "",
1994                 tab->n_redundant, tab->n_dead);
1995         if (tab->rational)
1996                 fprintf(out, ", rational");
1997         if (tab->empty)
1998                 fprintf(out, ", empty");
1999         fprintf(out, "\n");
2000         fprintf(out, "%*s[", indent, "");
2001         for (i = 0; i < tab->n_var; ++i) {
2002                 if (i)
2003                         fprintf(out, ", ");
2004                 fprintf(out, "%c%d%s", tab->var[i].is_row ? 'r' : 'c',
2005                                         tab->var[i].index,
2006                                         tab->var[i].is_zero ? " [=0]" :
2007                                         tab->var[i].is_redundant ? " [R]" : "");
2008         }
2009         fprintf(out, "]\n");
2010         fprintf(out, "%*s[", indent, "");
2011         for (i = 0; i < tab->n_con; ++i) {
2012                 if (i)
2013                         fprintf(out, ", ");
2014                 fprintf(out, "%c%d%s", tab->con[i].is_row ? 'r' : 'c',
2015                                         tab->con[i].index,
2016                                         tab->con[i].is_zero ? " [=0]" :
2017                                         tab->con[i].is_redundant ? " [R]" : "");
2018         }
2019         fprintf(out, "]\n");
2020         fprintf(out, "%*s[", indent, "");
2021         for (i = 0; i < tab->n_row; ++i) {
2022                 if (i)
2023                         fprintf(out, ", ");
2024                 fprintf(out, "r%d: %d%s", i, tab->row_var[i],
2025                     isl_tab_var_from_row(tab, i)->is_nonneg ? " [>=0]" : "");
2026         }
2027         fprintf(out, "]\n");
2028         fprintf(out, "%*s[", indent, "");
2029         for (i = 0; i < tab->n_col; ++i) {
2030                 if (i)
2031                         fprintf(out, ", ");
2032                 fprintf(out, "c%d: %d%s", i, tab->col_var[i],
2033                     var_from_col(tab, i)->is_nonneg ? " [>=0]" : "");
2034         }
2035         fprintf(out, "]\n");
2036         r = tab->mat->n_row;
2037         tab->mat->n_row = tab->n_row;
2038         c = tab->mat->n_col;
2039         tab->mat->n_col = 2 + tab->n_col;
2040         isl_mat_dump(tab->mat, out, indent);
2041         tab->mat->n_row = r;
2042         tab->mat->n_col = c;
2043 }