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