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