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