isl_tab_allocate_con: add extra assertion
[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         isl_assert(tab->mat->ctx, tab->n_con < tab->max_con, return -1);
1102
1103         r = tab->n_con;
1104         tab->con[r].index = tab->n_row;
1105         tab->con[r].is_row = 1;
1106         tab->con[r].is_nonneg = 0;
1107         tab->con[r].is_zero = 0;
1108         tab->con[r].is_redundant = 0;
1109         tab->con[r].frozen = 0;
1110         tab->con[r].negated = 0;
1111         tab->row_var[tab->n_row] = ~r;
1112
1113         tab->n_row++;
1114         tab->n_con++;
1115         isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->con[r]);
1116
1117         return r;
1118 }
1119
1120 /* Add a variable to the tableau and allocate a column for it.
1121  * Return the index into the variable array "var".
1122  */
1123 int isl_tab_allocate_var(struct isl_tab *tab)
1124 {
1125         int r;
1126         int i;
1127         unsigned off = 2 + tab->M;
1128
1129         isl_assert(tab->mat->ctx, tab->n_col < tab->mat->n_col, return -1);
1130         isl_assert(tab->mat->ctx, tab->n_var < tab->max_var, return -1);
1131
1132         r = tab->n_var;
1133         tab->var[r].index = tab->n_col;
1134         tab->var[r].is_row = 0;
1135         tab->var[r].is_nonneg = 0;
1136         tab->var[r].is_zero = 0;
1137         tab->var[r].is_redundant = 0;
1138         tab->var[r].frozen = 0;
1139         tab->var[r].negated = 0;
1140         tab->col_var[tab->n_col] = r;
1141
1142         for (i = 0; i < tab->n_row; ++i)
1143                 isl_int_set_si(tab->mat->row[i][off + tab->n_col], 0);
1144
1145         tab->n_var++;
1146         tab->n_col++;
1147         isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->var[r]);
1148
1149         return r;
1150 }
1151
1152 /* Add a row to the tableau.  The row is given as an affine combination
1153  * of the original variables and needs to be expressed in terms of the
1154  * column variables.
1155  *
1156  * We add each term in turn.
1157  * If r = n/d_r is the current sum and we need to add k x, then
1158  *      if x is a column variable, we increase the numerator of
1159  *              this column by k d_r
1160  *      if x = f/d_x is a row variable, then the new representation of r is
1161  *
1162  *               n    k f   d_x/g n + d_r/g k f   m/d_r n + m/d_g k f
1163  *              --- + --- = ------------------- = -------------------
1164  *              d_r   d_r        d_r d_x/g                m
1165  *
1166  *      with g the gcd of d_r and d_x and m the lcm of d_r and d_x.
1167  */
1168 int isl_tab_add_row(struct isl_tab *tab, isl_int *line)
1169 {
1170         int i;
1171         int r;
1172         isl_int *row;
1173         isl_int a, b;
1174         unsigned off = 2 + tab->M;
1175
1176         r = isl_tab_allocate_con(tab);
1177         if (r < 0)
1178                 return -1;
1179
1180         isl_int_init(a);
1181         isl_int_init(b);
1182         row = tab->mat->row[tab->con[r].index];
1183         isl_int_set_si(row[0], 1);
1184         isl_int_set(row[1], line[0]);
1185         isl_seq_clr(row + 2, tab->M + tab->n_col);
1186         for (i = 0; i < tab->n_var; ++i) {
1187                 if (tab->var[i].is_zero)
1188                         continue;
1189                 if (tab->var[i].is_row) {
1190                         isl_int_lcm(a,
1191                                 row[0], tab->mat->row[tab->var[i].index][0]);
1192                         isl_int_swap(a, row[0]);
1193                         isl_int_divexact(a, row[0], a);
1194                         isl_int_divexact(b,
1195                                 row[0], tab->mat->row[tab->var[i].index][0]);
1196                         isl_int_mul(b, b, line[1 + i]);
1197                         isl_seq_combine(row + 1, a, row + 1,
1198                             b, tab->mat->row[tab->var[i].index] + 1,
1199                             1 + tab->M + tab->n_col);
1200                 } else
1201                         isl_int_addmul(row[off + tab->var[i].index],
1202                                                         line[1 + i], row[0]);
1203                 if (tab->M && i >= tab->n_param && i < tab->n_var - tab->n_div)
1204                         isl_int_submul(row[2], line[1 + i], row[0]);
1205         }
1206         isl_seq_normalize(tab->mat->ctx, row, off + tab->n_col);
1207         isl_int_clear(a);
1208         isl_int_clear(b);
1209
1210         if (tab->row_sign)
1211                 tab->row_sign[tab->con[r].index] = 0;
1212
1213         return r;
1214 }
1215
1216 static int drop_row(struct isl_tab *tab, int row)
1217 {
1218         isl_assert(tab->mat->ctx, ~tab->row_var[row] == tab->n_con - 1, return -1);
1219         if (row != tab->n_row - 1)
1220                 swap_rows(tab, row, tab->n_row - 1);
1221         tab->n_row--;
1222         tab->n_con--;
1223         return 0;
1224 }
1225
1226 static int drop_col(struct isl_tab *tab, int col)
1227 {
1228         isl_assert(tab->mat->ctx, tab->col_var[col] == tab->n_var - 1, return -1);
1229         if (col != tab->n_col - 1)
1230                 swap_cols(tab, col, tab->n_col - 1);
1231         tab->n_col--;
1232         tab->n_var--;
1233         return 0;
1234 }
1235
1236 /* Add inequality "ineq" and check if it conflicts with the
1237  * previously added constraints or if it is obviously redundant.
1238  */
1239 struct isl_tab *isl_tab_add_ineq(struct isl_tab *tab, isl_int *ineq)
1240 {
1241         int r;
1242         int sgn;
1243
1244         if (!tab)
1245                 return NULL;
1246         r = isl_tab_add_row(tab, ineq);
1247         if (r < 0)
1248                 goto error;
1249         tab->con[r].is_nonneg = 1;
1250         isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]);
1251         if (isl_tab_row_is_redundant(tab, tab->con[r].index)) {
1252                 isl_tab_mark_redundant(tab, tab->con[r].index);
1253                 return tab;
1254         }
1255
1256         sgn = restore_row(tab, &tab->con[r]);
1257         if (sgn < 0)
1258                 return isl_tab_mark_empty(tab);
1259         if (tab->con[r].is_row && isl_tab_row_is_redundant(tab, tab->con[r].index))
1260                 isl_tab_mark_redundant(tab, tab->con[r].index);
1261         return tab;
1262 error:
1263         isl_tab_free(tab);
1264         return NULL;
1265 }
1266
1267 /* Pivot a non-negative variable down until it reaches the value zero
1268  * and then pivot the variable into a column position.
1269  */
1270 static int to_col(struct isl_tab *tab, struct isl_tab_var *var)
1271 {
1272         int i;
1273         int row, col;
1274         unsigned off = 2 + tab->M;
1275
1276         if (!var->is_row)
1277                 return 0;
1278
1279         while (isl_int_is_pos(tab->mat->row[var->index][1])) {
1280                 find_pivot(tab, var, NULL, -1, &row, &col);
1281                 isl_assert(tab->mat->ctx, row != -1, return -1);
1282                 isl_tab_pivot(tab, row, col);
1283                 if (!var->is_row)
1284                         return 0;
1285         }
1286
1287         for (i = tab->n_dead; i < tab->n_col; ++i)
1288                 if (!isl_int_is_zero(tab->mat->row[var->index][off + i]))
1289                         break;
1290
1291         isl_assert(tab->mat->ctx, i < tab->n_col, return -1);
1292         isl_tab_pivot(tab, var->index, i);
1293
1294         return 0;
1295 }
1296
1297 /* We assume Gaussian elimination has been performed on the equalities.
1298  * The equalities can therefore never conflict.
1299  * Adding the equalities is currently only really useful for a later call
1300  * to isl_tab_ineq_type.
1301  */
1302 static struct isl_tab *add_eq(struct isl_tab *tab, isl_int *eq)
1303 {
1304         int i;
1305         int r;
1306
1307         if (!tab)
1308                 return NULL;
1309         r = isl_tab_add_row(tab, eq);
1310         if (r < 0)
1311                 goto error;
1312
1313         r = tab->con[r].index;
1314         i = isl_seq_first_non_zero(tab->mat->row[r] + 2 + tab->M + tab->n_dead,
1315                                         tab->n_col - tab->n_dead);
1316         isl_assert(tab->mat->ctx, i >= 0, goto error);
1317         i += tab->n_dead;
1318         isl_tab_pivot(tab, r, i);
1319         isl_tab_kill_col(tab, i);
1320         tab->n_eq++;
1321
1322         return tab;
1323 error:
1324         isl_tab_free(tab);
1325         return NULL;
1326 }
1327
1328 /* Add an equality that is known to be valid for the given tableau.
1329  */
1330 struct isl_tab *isl_tab_add_valid_eq(struct isl_tab *tab, isl_int *eq)
1331 {
1332         struct isl_tab_var *var;
1333         int r;
1334
1335         if (!tab)
1336                 return NULL;
1337         r = isl_tab_add_row(tab, eq);
1338         if (r < 0)
1339                 goto error;
1340
1341         var = &tab->con[r];
1342         r = var->index;
1343         if (isl_int_is_neg(tab->mat->row[r][1])) {
1344                 isl_seq_neg(tab->mat->row[r] + 1, tab->mat->row[r] + 1,
1345                             1 + tab->n_col);
1346                 var->negated = 1;
1347         }
1348         var->is_nonneg = 1;
1349         if (to_col(tab, var) < 0)
1350                 goto error;
1351         var->is_nonneg = 0;
1352         isl_tab_kill_col(tab, var->index);
1353
1354         return tab;
1355 error:
1356         isl_tab_free(tab);
1357         return NULL;
1358 }
1359
1360 struct isl_tab *isl_tab_from_basic_map(struct isl_basic_map *bmap)
1361 {
1362         int i;
1363         struct isl_tab *tab;
1364
1365         if (!bmap)
1366                 return NULL;
1367         tab = isl_tab_alloc(bmap->ctx,
1368                             isl_basic_map_total_dim(bmap) + bmap->n_ineq + 1,
1369                             isl_basic_map_total_dim(bmap), 0);
1370         if (!tab)
1371                 return NULL;
1372         tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
1373         if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1374                 return isl_tab_mark_empty(tab);
1375         for (i = 0; i < bmap->n_eq; ++i) {
1376                 tab = add_eq(tab, bmap->eq[i]);
1377                 if (!tab)
1378                         return tab;
1379         }
1380         for (i = 0; i < bmap->n_ineq; ++i) {
1381                 tab = isl_tab_add_ineq(tab, bmap->ineq[i]);
1382                 if (!tab || tab->empty)
1383                         return tab;
1384         }
1385         return tab;
1386 }
1387
1388 struct isl_tab *isl_tab_from_basic_set(struct isl_basic_set *bset)
1389 {
1390         return isl_tab_from_basic_map((struct isl_basic_map *)bset);
1391 }
1392
1393 /* Construct a tableau corresponding to the recession cone of "bmap".
1394  */
1395 struct isl_tab *isl_tab_from_recession_cone(struct isl_basic_map *bmap)
1396 {
1397         isl_int cst;
1398         int i;
1399         struct isl_tab *tab;
1400
1401         if (!bmap)
1402                 return NULL;
1403         tab = isl_tab_alloc(bmap->ctx, bmap->n_eq + bmap->n_ineq,
1404                                 isl_basic_map_total_dim(bmap), 0);
1405         if (!tab)
1406                 return NULL;
1407         tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
1408
1409         isl_int_init(cst);
1410         for (i = 0; i < bmap->n_eq; ++i) {
1411                 isl_int_swap(bmap->eq[i][0], cst);
1412                 tab = add_eq(tab, bmap->eq[i]);
1413                 isl_int_swap(bmap->eq[i][0], cst);
1414                 if (!tab)
1415                         goto done;
1416         }
1417         for (i = 0; i < bmap->n_ineq; ++i) {
1418                 int r;
1419                 isl_int_swap(bmap->ineq[i][0], cst);
1420                 r = isl_tab_add_row(tab, bmap->ineq[i]);
1421                 isl_int_swap(bmap->ineq[i][0], cst);
1422                 if (r < 0)
1423                         goto error;
1424                 tab->con[r].is_nonneg = 1;
1425                 isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]);
1426         }
1427 done:
1428         isl_int_clear(cst);
1429         return tab;
1430 error:
1431         isl_int_clear(cst);
1432         isl_tab_free(tab);
1433         return NULL;
1434 }
1435
1436 /* Assuming "tab" is the tableau of a cone, check if the cone is
1437  * bounded, i.e., if it is empty or only contains the origin.
1438  */
1439 int isl_tab_cone_is_bounded(struct isl_tab *tab)
1440 {
1441         int i;
1442
1443         if (!tab)
1444                 return -1;
1445         if (tab->empty)
1446                 return 1;
1447         if (tab->n_dead == tab->n_col)
1448                 return 1;
1449
1450         for (;;) {
1451                 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1452                         struct isl_tab_var *var;
1453                         var = isl_tab_var_from_row(tab, i);
1454                         if (!var->is_nonneg)
1455                                 continue;
1456                         if (sign_of_max(tab, var) != 0)
1457                                 return 0;
1458                         close_row(tab, var);
1459                         break;
1460                 }
1461                 if (tab->n_dead == tab->n_col)
1462                         return 1;
1463                 if (i == tab->n_row)
1464                         return 0;
1465         }
1466 }
1467
1468 int isl_tab_sample_is_integer(struct isl_tab *tab)
1469 {
1470         int i;
1471
1472         if (!tab)
1473                 return -1;
1474
1475         for (i = 0; i < tab->n_var; ++i) {
1476                 int row;
1477                 if (!tab->var[i].is_row)
1478                         continue;
1479                 row = tab->var[i].index;
1480                 if (!isl_int_is_divisible_by(tab->mat->row[row][1],
1481                                                 tab->mat->row[row][0]))
1482                         return 0;
1483         }
1484         return 1;
1485 }
1486
1487 static struct isl_vec *extract_integer_sample(struct isl_tab *tab)
1488 {
1489         int i;
1490         struct isl_vec *vec;
1491
1492         vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
1493         if (!vec)
1494                 return NULL;
1495
1496         isl_int_set_si(vec->block.data[0], 1);
1497         for (i = 0; i < tab->n_var; ++i) {
1498                 if (!tab->var[i].is_row)
1499                         isl_int_set_si(vec->block.data[1 + i], 0);
1500                 else {
1501                         int row = tab->var[i].index;
1502                         isl_int_divexact(vec->block.data[1 + i],
1503                                 tab->mat->row[row][1], tab->mat->row[row][0]);
1504                 }
1505         }
1506
1507         return vec;
1508 }
1509
1510 struct isl_vec *isl_tab_get_sample_value(struct isl_tab *tab)
1511 {
1512         int i;
1513         struct isl_vec *vec;
1514         isl_int m;
1515
1516         if (!tab)
1517                 return NULL;
1518
1519         vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
1520         if (!vec)
1521                 return NULL;
1522
1523         isl_int_init(m);
1524
1525         isl_int_set_si(vec->block.data[0], 1);
1526         for (i = 0; i < tab->n_var; ++i) {
1527                 int row;
1528                 if (!tab->var[i].is_row) {
1529                         isl_int_set_si(vec->block.data[1 + i], 0);
1530                         continue;
1531                 }
1532                 row = tab->var[i].index;
1533                 isl_int_gcd(m, vec->block.data[0], tab->mat->row[row][0]);
1534                 isl_int_divexact(m, tab->mat->row[row][0], m);
1535                 isl_seq_scale(vec->block.data, vec->block.data, m, 1 + i);
1536                 isl_int_divexact(m, vec->block.data[0], tab->mat->row[row][0]);
1537                 isl_int_mul(vec->block.data[1 + i], m, tab->mat->row[row][1]);
1538         }
1539         vec = isl_vec_normalize(vec);
1540
1541         isl_int_clear(m);
1542         return vec;
1543 }
1544
1545 /* Update "bmap" based on the results of the tableau "tab".
1546  * In particular, implicit equalities are made explicit, redundant constraints
1547  * are removed and if the sample value happens to be integer, it is stored
1548  * in "bmap" (unless "bmap" already had an integer sample).
1549  *
1550  * The tableau is assumed to have been created from "bmap" using
1551  * isl_tab_from_basic_map.
1552  */
1553 struct isl_basic_map *isl_basic_map_update_from_tab(struct isl_basic_map *bmap,
1554         struct isl_tab *tab)
1555 {
1556         int i;
1557         unsigned n_eq;
1558
1559         if (!bmap)
1560                 return NULL;
1561         if (!tab)
1562                 return bmap;
1563
1564         n_eq = tab->n_eq;
1565         if (tab->empty)
1566                 bmap = isl_basic_map_set_to_empty(bmap);
1567         else
1568                 for (i = bmap->n_ineq - 1; i >= 0; --i) {
1569                         if (isl_tab_is_equality(tab, n_eq + i))
1570                                 isl_basic_map_inequality_to_equality(bmap, i);
1571                         else if (isl_tab_is_redundant(tab, n_eq + i))
1572                                 isl_basic_map_drop_inequality(bmap, i);
1573                 }
1574         if (!tab->rational &&
1575             !bmap->sample && isl_tab_sample_is_integer(tab))
1576                 bmap->sample = extract_integer_sample(tab);
1577         return bmap;
1578 }
1579
1580 struct isl_basic_set *isl_basic_set_update_from_tab(struct isl_basic_set *bset,
1581         struct isl_tab *tab)
1582 {
1583         return (struct isl_basic_set *)isl_basic_map_update_from_tab(
1584                 (struct isl_basic_map *)bset, tab);
1585 }
1586
1587 /* Given a non-negative variable "var", add a new non-negative variable
1588  * that is the opposite of "var", ensuring that var can only attain the
1589  * value zero.
1590  * If var = n/d is a row variable, then the new variable = -n/d.
1591  * If var is a column variables, then the new variable = -var.
1592  * If the new variable cannot attain non-negative values, then
1593  * the resulting tableau is empty.
1594  * Otherwise, we know the value will be zero and we close the row.
1595  */
1596 static struct isl_tab *cut_to_hyperplane(struct isl_tab *tab,
1597         struct isl_tab_var *var)
1598 {
1599         unsigned r;
1600         isl_int *row;
1601         int sgn;
1602         unsigned off = 2 + tab->M;
1603
1604         if (var->is_zero)
1605                 return tab;
1606         isl_assert(tab->mat->ctx, !var->is_redundant, goto error);
1607
1608         if (isl_tab_extend_cons(tab, 1) < 0)
1609                 goto error;
1610
1611         r = tab->n_con;
1612         tab->con[r].index = tab->n_row;
1613         tab->con[r].is_row = 1;
1614         tab->con[r].is_nonneg = 0;
1615         tab->con[r].is_zero = 0;
1616         tab->con[r].is_redundant = 0;
1617         tab->con[r].frozen = 0;
1618         tab->con[r].negated = 0;
1619         tab->row_var[tab->n_row] = ~r;
1620         row = tab->mat->row[tab->n_row];
1621
1622         if (var->is_row) {
1623                 isl_int_set(row[0], tab->mat->row[var->index][0]);
1624                 isl_seq_neg(row + 1,
1625                             tab->mat->row[var->index] + 1, 1 + tab->n_col);
1626         } else {
1627                 isl_int_set_si(row[0], 1);
1628                 isl_seq_clr(row + 1, 1 + tab->n_col);
1629                 isl_int_set_si(row[off + var->index], -1);
1630         }
1631
1632         tab->n_row++;
1633         tab->n_con++;
1634         isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->con[r]);
1635
1636         sgn = sign_of_max(tab, &tab->con[r]);
1637         if (sgn < 0)
1638                 return isl_tab_mark_empty(tab);
1639         tab->con[r].is_nonneg = 1;
1640         isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]);
1641         /* sgn == 0 */
1642         close_row(tab, &tab->con[r]);
1643
1644         return tab;
1645 error:
1646         isl_tab_free(tab);
1647         return NULL;
1648 }
1649
1650 /* Given a tableau "tab" and an inequality constraint "con" of the tableau,
1651  * relax the inequality by one.  That is, the inequality r >= 0 is replaced
1652  * by r' = r + 1 >= 0.
1653  * If r is a row variable, we simply increase the constant term by one
1654  * (taking into account the denominator).
1655  * If r is a column variable, then we need to modify each row that
1656  * refers to r = r' - 1 by substituting this equality, effectively
1657  * subtracting the coefficient of the column from the constant.
1658  */
1659 struct isl_tab *isl_tab_relax(struct isl_tab *tab, int con)
1660 {
1661         struct isl_tab_var *var;
1662         unsigned off = 2 + tab->M;
1663
1664         if (!tab)
1665                 return NULL;
1666
1667         var = &tab->con[con];
1668
1669         if (!var->is_row && !max_is_manifestly_unbounded(tab, var))
1670                 to_row(tab, var, 1);
1671
1672         if (var->is_row)
1673                 isl_int_add(tab->mat->row[var->index][1],
1674                     tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
1675         else {
1676                 int i;
1677
1678                 for (i = 0; i < tab->n_row; ++i) {
1679                         if (isl_int_is_zero(tab->mat->row[i][off + var->index]))
1680                                 continue;
1681                         isl_int_sub(tab->mat->row[i][1], tab->mat->row[i][1],
1682                             tab->mat->row[i][off + var->index]);
1683                 }
1684
1685         }
1686
1687         isl_tab_push_var(tab, isl_tab_undo_relax, var);
1688
1689         return tab;
1690 }
1691
1692 struct isl_tab *isl_tab_select_facet(struct isl_tab *tab, int con)
1693 {
1694         if (!tab)
1695                 return NULL;
1696
1697         return cut_to_hyperplane(tab, &tab->con[con]);
1698 }
1699
1700 static int may_be_equality(struct isl_tab *tab, int row)
1701 {
1702         unsigned off = 2 + tab->M;
1703         return (tab->rational ? isl_int_is_zero(tab->mat->row[row][1])
1704                               : isl_int_lt(tab->mat->row[row][1],
1705                                             tab->mat->row[row][0])) &&
1706                 isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
1707                                         tab->n_col - tab->n_dead) != -1;
1708 }
1709
1710 /* Check for (near) equalities among the constraints.
1711  * A constraint is an equality if it is non-negative and if
1712  * its maximal value is either
1713  *      - zero (in case of rational tableaus), or
1714  *      - strictly less than 1 (in case of integer tableaus)
1715  *
1716  * We first mark all non-redundant and non-dead variables that
1717  * are not frozen and not obviously not an equality.
1718  * Then we iterate over all marked variables if they can attain
1719  * any values larger than zero or at least one.
1720  * If the maximal value is zero, we mark any column variables
1721  * that appear in the row as being zero and mark the row as being redundant.
1722  * Otherwise, if the maximal value is strictly less than one (and the
1723  * tableau is integer), then we restrict the value to being zero
1724  * by adding an opposite non-negative variable.
1725  */
1726 struct isl_tab *isl_tab_detect_equalities(struct isl_tab *tab)
1727 {
1728         int i;
1729         unsigned n_marked;
1730
1731         if (!tab)
1732                 return NULL;
1733         if (tab->empty)
1734                 return tab;
1735         if (tab->n_dead == tab->n_col)
1736                 return tab;
1737
1738         n_marked = 0;
1739         for (i = tab->n_redundant; i < tab->n_row; ++i) {
1740                 struct isl_tab_var *var = isl_tab_var_from_row(tab, i);
1741                 var->marked = !var->frozen && var->is_nonneg &&
1742                         may_be_equality(tab, i);
1743                 if (var->marked)
1744                         n_marked++;
1745         }
1746         for (i = tab->n_dead; i < tab->n_col; ++i) {
1747                 struct isl_tab_var *var = var_from_col(tab, i);
1748                 var->marked = !var->frozen && var->is_nonneg;
1749                 if (var->marked)
1750                         n_marked++;
1751         }
1752         while (n_marked) {
1753                 struct isl_tab_var *var;
1754                 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1755                         var = isl_tab_var_from_row(tab, i);
1756                         if (var->marked)
1757                                 break;
1758                 }
1759                 if (i == tab->n_row) {
1760                         for (i = tab->n_dead; i < tab->n_col; ++i) {
1761                                 var = var_from_col(tab, i);
1762                                 if (var->marked)
1763                                         break;
1764                         }
1765                         if (i == tab->n_col)
1766                                 break;
1767                 }
1768                 var->marked = 0;
1769                 n_marked--;
1770                 if (sign_of_max(tab, var) == 0)
1771                         close_row(tab, var);
1772                 else if (!tab->rational && !at_least_one(tab, var)) {
1773                         tab = cut_to_hyperplane(tab, var);
1774                         return isl_tab_detect_equalities(tab);
1775                 }
1776                 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1777                         var = isl_tab_var_from_row(tab, i);
1778                         if (!var->marked)
1779                                 continue;
1780                         if (may_be_equality(tab, i))
1781                                 continue;
1782                         var->marked = 0;
1783                         n_marked--;
1784                 }
1785         }
1786
1787         return tab;
1788 }
1789
1790 /* Check for (near) redundant constraints.
1791  * A constraint is redundant if it is non-negative and if
1792  * its minimal value (temporarily ignoring the non-negativity) is either
1793  *      - zero (in case of rational tableaus), or
1794  *      - strictly larger than -1 (in case of integer tableaus)
1795  *
1796  * We first mark all non-redundant and non-dead variables that
1797  * are not frozen and not obviously negatively unbounded.
1798  * Then we iterate over all marked variables if they can attain
1799  * any values smaller than zero or at most negative one.
1800  * If not, we mark the row as being redundant (assuming it hasn't
1801  * been detected as being obviously redundant in the mean time).
1802  */
1803 struct isl_tab *isl_tab_detect_redundant(struct isl_tab *tab)
1804 {
1805         int i;
1806         unsigned n_marked;
1807
1808         if (!tab)
1809                 return NULL;
1810         if (tab->empty)
1811                 return tab;
1812         if (tab->n_redundant == tab->n_row)
1813                 return tab;
1814
1815         n_marked = 0;
1816         for (i = tab->n_redundant; i < tab->n_row; ++i) {
1817                 struct isl_tab_var *var = isl_tab_var_from_row(tab, i);
1818                 var->marked = !var->frozen && var->is_nonneg;
1819                 if (var->marked)
1820                         n_marked++;
1821         }
1822         for (i = tab->n_dead; i < tab->n_col; ++i) {
1823                 struct isl_tab_var *var = var_from_col(tab, i);
1824                 var->marked = !var->frozen && var->is_nonneg &&
1825                         !min_is_manifestly_unbounded(tab, var);
1826                 if (var->marked)
1827                         n_marked++;
1828         }
1829         while (n_marked) {
1830                 struct isl_tab_var *var;
1831                 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1832                         var = isl_tab_var_from_row(tab, i);
1833                         if (var->marked)
1834                                 break;
1835                 }
1836                 if (i == tab->n_row) {
1837                         for (i = tab->n_dead; i < tab->n_col; ++i) {
1838                                 var = var_from_col(tab, i);
1839                                 if (var->marked)
1840                                         break;
1841                         }
1842                         if (i == tab->n_col)
1843                                 break;
1844                 }
1845                 var->marked = 0;
1846                 n_marked--;
1847                 if ((tab->rational ? (sign_of_min(tab, var) >= 0)
1848                                    : !isl_tab_min_at_most_neg_one(tab, var)) &&
1849                     !var->is_redundant)
1850                         isl_tab_mark_redundant(tab, var->index);
1851                 for (i = tab->n_dead; i < tab->n_col; ++i) {
1852                         var = var_from_col(tab, i);
1853                         if (!var->marked)
1854                                 continue;
1855                         if (!min_is_manifestly_unbounded(tab, var))
1856                                 continue;
1857                         var->marked = 0;
1858                         n_marked--;
1859                 }
1860         }
1861
1862         return tab;
1863 }
1864
1865 int isl_tab_is_equality(struct isl_tab *tab, int con)
1866 {
1867         int row;
1868         unsigned off;
1869
1870         if (!tab)
1871                 return -1;
1872         if (tab->con[con].is_zero)
1873                 return 1;
1874         if (tab->con[con].is_redundant)
1875                 return 0;
1876         if (!tab->con[con].is_row)
1877                 return tab->con[con].index < tab->n_dead;
1878
1879         row = tab->con[con].index;
1880
1881         off = 2 + tab->M;
1882         return isl_int_is_zero(tab->mat->row[row][1]) &&
1883                 isl_seq_first_non_zero(tab->mat->row[row] + 2 + tab->n_dead,
1884                                         tab->n_col - tab->n_dead) == -1;
1885 }
1886
1887 /* Return the minimial value of the affine expression "f" with denominator
1888  * "denom" in *opt, *opt_denom, assuming the tableau is not empty and
1889  * the expression cannot attain arbitrarily small values.
1890  * If opt_denom is NULL, then *opt is rounded up to the nearest integer.
1891  * The return value reflects the nature of the result (empty, unbounded,
1892  * minmimal value returned in *opt).
1893  */
1894 enum isl_lp_result isl_tab_min(struct isl_tab *tab,
1895         isl_int *f, isl_int denom, isl_int *opt, isl_int *opt_denom,
1896         unsigned flags)
1897 {
1898         int r;
1899         enum isl_lp_result res = isl_lp_ok;
1900         struct isl_tab_var *var;
1901         struct isl_tab_undo *snap;
1902
1903         if (tab->empty)
1904                 return isl_lp_empty;
1905
1906         snap = isl_tab_snap(tab);
1907         r = isl_tab_add_row(tab, f);
1908         if (r < 0)
1909                 return isl_lp_error;
1910         var = &tab->con[r];
1911         isl_int_mul(tab->mat->row[var->index][0],
1912                     tab->mat->row[var->index][0], denom);
1913         for (;;) {
1914                 int row, col;
1915                 find_pivot(tab, var, var, -1, &row, &col);
1916                 if (row == var->index) {
1917                         res = isl_lp_unbounded;
1918                         break;
1919                 }
1920                 if (row == -1)
1921                         break;
1922                 isl_tab_pivot(tab, row, col);
1923         }
1924         if (ISL_FL_ISSET(flags, ISL_TAB_SAVE_DUAL)) {
1925                 int i;
1926
1927                 isl_vec_free(tab->dual);
1928                 tab->dual = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_con);
1929                 if (!tab->dual)
1930                         return isl_lp_error;
1931                 isl_int_set(tab->dual->el[0], tab->mat->row[var->index][0]);
1932                 for (i = 0; i < tab->n_con; ++i) {
1933                         int pos;
1934                         if (tab->con[i].is_row) {
1935                                 isl_int_set_si(tab->dual->el[1 + i], 0);
1936                                 continue;
1937                         }
1938                         pos = 2 + tab->M + tab->con[i].index;
1939                         if (tab->con[i].negated)
1940                                 isl_int_neg(tab->dual->el[1 + i],
1941                                             tab->mat->row[var->index][pos]);
1942                         else
1943                                 isl_int_set(tab->dual->el[1 + i],
1944                                             tab->mat->row[var->index][pos]);
1945                 }
1946         }
1947         if (opt && res == isl_lp_ok) {
1948                 if (opt_denom) {
1949                         isl_int_set(*opt, tab->mat->row[var->index][1]);
1950                         isl_int_set(*opt_denom, tab->mat->row[var->index][0]);
1951                 } else
1952                         isl_int_cdiv_q(*opt, tab->mat->row[var->index][1],
1953                                              tab->mat->row[var->index][0]);
1954         }
1955         if (isl_tab_rollback(tab, snap) < 0)
1956                 return isl_lp_error;
1957         return res;
1958 }
1959
1960 int isl_tab_is_redundant(struct isl_tab *tab, int con)
1961 {
1962         if (!tab)
1963                 return -1;
1964         if (tab->con[con].is_zero)
1965                 return 0;
1966         if (tab->con[con].is_redundant)
1967                 return 1;
1968         return tab->con[con].is_row && tab->con[con].index < tab->n_redundant;
1969 }
1970
1971 /* Take a snapshot of the tableau that can be restored by s call to
1972  * isl_tab_rollback.
1973  */
1974 struct isl_tab_undo *isl_tab_snap(struct isl_tab *tab)
1975 {
1976         if (!tab)
1977                 return NULL;
1978         tab->need_undo = 1;
1979         return tab->top;
1980 }
1981
1982 /* Undo the operation performed by isl_tab_relax.
1983  */
1984 static void unrelax(struct isl_tab *tab, struct isl_tab_var *var)
1985 {
1986         unsigned off = 2 + tab->M;
1987
1988         if (!var->is_row && !max_is_manifestly_unbounded(tab, var))
1989                 to_row(tab, var, 1);
1990
1991         if (var->is_row)
1992                 isl_int_sub(tab->mat->row[var->index][1],
1993                     tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
1994         else {
1995                 int i;
1996
1997                 for (i = 0; i < tab->n_row; ++i) {
1998                         if (isl_int_is_zero(tab->mat->row[i][off + var->index]))
1999                                 continue;
2000                         isl_int_add(tab->mat->row[i][1], tab->mat->row[i][1],
2001                             tab->mat->row[i][off + var->index]);
2002                 }
2003
2004         }
2005 }
2006
2007 static void perform_undo_var(struct isl_tab *tab, struct isl_tab_undo *undo)
2008 {
2009         struct isl_tab_var *var = var_from_index(tab, undo->u.var_index);
2010         switch(undo->type) {
2011         case isl_tab_undo_nonneg:
2012                 var->is_nonneg = 0;
2013                 break;
2014         case isl_tab_undo_redundant:
2015                 var->is_redundant = 0;
2016                 tab->n_redundant--;
2017                 break;
2018         case isl_tab_undo_zero:
2019                 var->is_zero = 0;
2020                 if (!var->is_row)
2021                         tab->n_dead--;
2022                 break;
2023         case isl_tab_undo_allocate:
2024                 if (undo->u.var_index >= 0) {
2025                         isl_assert(tab->mat->ctx, !var->is_row, return);
2026                         drop_col(tab, var->index);
2027                         break;
2028                 }
2029                 if (!var->is_row) {
2030                         if (!max_is_manifestly_unbounded(tab, var))
2031                                 to_row(tab, var, 1);
2032                         else if (!min_is_manifestly_unbounded(tab, var))
2033                                 to_row(tab, var, -1);
2034                         else
2035                                 to_row(tab, var, 0);
2036                 }
2037                 drop_row(tab, var->index);
2038                 break;
2039         case isl_tab_undo_relax:
2040                 unrelax(tab, var);
2041                 break;
2042         }
2043 }
2044
2045 /* Restore the tableau to the state where the basic variables
2046  * are those in "col_var".
2047  * We first construct a list of variables that are currently in
2048  * the basis, but shouldn't.  Then we iterate over all variables
2049  * that should be in the basis and for each one that is currently
2050  * not in the basis, we exchange it with one of the elements of the
2051  * list constructed before.
2052  * We can always find an appropriate variable to pivot with because
2053  * the current basis is mapped to the old basis by a non-singular
2054  * matrix and so we can never end up with a zero row.
2055  */
2056 static int restore_basis(struct isl_tab *tab, int *col_var)
2057 {
2058         int i, j;
2059         int n_extra = 0;
2060         int *extra = NULL;      /* current columns that contain bad stuff */
2061         unsigned off = 2 + tab->M;
2062
2063         extra = isl_alloc_array(tab->mat->ctx, int, tab->n_col);
2064         if (!extra)
2065                 goto error;
2066         for (i = 0; i < tab->n_col; ++i) {
2067                 for (j = 0; j < tab->n_col; ++j)
2068                         if (tab->col_var[i] == col_var[j])
2069                                 break;
2070                 if (j < tab->n_col)
2071                         continue;
2072                 extra[n_extra++] = i;
2073         }
2074         for (i = 0; i < tab->n_col && n_extra > 0; ++i) {
2075                 struct isl_tab_var *var;
2076                 int row;
2077
2078                 for (j = 0; j < tab->n_col; ++j)
2079                         if (col_var[i] == tab->col_var[j])
2080                                 break;
2081                 if (j < tab->n_col)
2082                         continue;
2083                 var = var_from_index(tab, col_var[i]);
2084                 row = var->index;
2085                 for (j = 0; j < n_extra; ++j)
2086                         if (!isl_int_is_zero(tab->mat->row[row][off+extra[j]]))
2087                                 break;
2088                 isl_assert(tab->mat->ctx, j < n_extra, goto error);
2089                 isl_tab_pivot(tab, row, extra[j]);
2090                 extra[j] = extra[--n_extra];
2091         }
2092
2093         free(extra);
2094         free(col_var);
2095         return 0;
2096 error:
2097         free(extra);
2098         free(col_var);
2099         return -1;
2100 }
2101
2102 static int perform_undo(struct isl_tab *tab, struct isl_tab_undo *undo)
2103 {
2104         switch (undo->type) {
2105         case isl_tab_undo_empty:
2106                 tab->empty = 0;
2107                 break;
2108         case isl_tab_undo_nonneg:
2109         case isl_tab_undo_redundant:
2110         case isl_tab_undo_zero:
2111         case isl_tab_undo_allocate:
2112         case isl_tab_undo_relax:
2113                 perform_undo_var(tab, undo);
2114                 break;
2115         case isl_tab_undo_bset_eq:
2116                 isl_basic_set_free_equality(tab->bset, 1);
2117                 break;
2118         case isl_tab_undo_bset_ineq:
2119                 isl_basic_set_free_inequality(tab->bset, 1);
2120                 break;
2121         case isl_tab_undo_bset_div:
2122                 isl_basic_set_free_div(tab->bset, 1);
2123                 if (tab->samples)
2124                         tab->samples->n_col--;
2125                 break;
2126         case isl_tab_undo_saved_basis:
2127                 if (restore_basis(tab, undo->u.col_var) < 0)
2128                         return -1;
2129                 break;
2130         case isl_tab_undo_drop_sample:
2131                 tab->n_outside--;
2132                 break;
2133         default:
2134                 isl_assert(tab->mat->ctx, 0, return -1);
2135         }
2136         return 0;
2137 }
2138
2139 /* Return the tableau to the state it was in when the snapshot "snap"
2140  * was taken.
2141  */
2142 int isl_tab_rollback(struct isl_tab *tab, struct isl_tab_undo *snap)
2143 {
2144         struct isl_tab_undo *undo, *next;
2145
2146         if (!tab)
2147                 return -1;
2148
2149         tab->in_undo = 1;
2150         for (undo = tab->top; undo && undo != &tab->bottom; undo = next) {
2151                 next = undo->next;
2152                 if (undo == snap)
2153                         break;
2154                 if (perform_undo(tab, undo) < 0) {
2155                         free_undo(tab);
2156                         tab->in_undo = 0;
2157                         return -1;
2158                 }
2159                 free(undo);
2160         }
2161         tab->in_undo = 0;
2162         tab->top = undo;
2163         if (!undo)
2164                 return -1;
2165         return 0;
2166 }
2167
2168 /* The given row "row" represents an inequality violated by all
2169  * points in the tableau.  Check for some special cases of such
2170  * separating constraints.
2171  * In particular, if the row has been reduced to the constant -1,
2172  * then we know the inequality is adjacent (but opposite) to
2173  * an equality in the tableau.
2174  * If the row has been reduced to r = -1 -r', with r' an inequality
2175  * of the tableau, then the inequality is adjacent (but opposite)
2176  * to the inequality r'.
2177  */
2178 static enum isl_ineq_type separation_type(struct isl_tab *tab, unsigned row)
2179 {
2180         int pos;
2181         unsigned off = 2 + tab->M;
2182
2183         if (tab->rational)
2184                 return isl_ineq_separate;
2185
2186         if (!isl_int_is_one(tab->mat->row[row][0]))
2187                 return isl_ineq_separate;
2188         if (!isl_int_is_negone(tab->mat->row[row][1]))
2189                 return isl_ineq_separate;
2190
2191         pos = isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
2192                                         tab->n_col - tab->n_dead);
2193         if (pos == -1)
2194                 return isl_ineq_adj_eq;
2195
2196         if (!isl_int_is_negone(tab->mat->row[row][off + tab->n_dead + pos]))
2197                 return isl_ineq_separate;
2198
2199         pos = isl_seq_first_non_zero(
2200                         tab->mat->row[row] + off + tab->n_dead + pos + 1,
2201                         tab->n_col - tab->n_dead - pos - 1);
2202
2203         return pos == -1 ? isl_ineq_adj_ineq : isl_ineq_separate;
2204 }
2205
2206 /* Check the effect of inequality "ineq" on the tableau "tab".
2207  * The result may be
2208  *      isl_ineq_redundant:     satisfied by all points in the tableau
2209  *      isl_ineq_separate:      satisfied by no point in the tableau
2210  *      isl_ineq_cut:           satisfied by some by not all points
2211  *      isl_ineq_adj_eq:        adjacent to an equality
2212  *      isl_ineq_adj_ineq:      adjacent to an inequality.
2213  */
2214 enum isl_ineq_type isl_tab_ineq_type(struct isl_tab *tab, isl_int *ineq)
2215 {
2216         enum isl_ineq_type type = isl_ineq_error;
2217         struct isl_tab_undo *snap = NULL;
2218         int con;
2219         int row;
2220
2221         if (!tab)
2222                 return isl_ineq_error;
2223
2224         if (isl_tab_extend_cons(tab, 1) < 0)
2225                 return isl_ineq_error;
2226
2227         snap = isl_tab_snap(tab);
2228
2229         con = isl_tab_add_row(tab, ineq);
2230         if (con < 0)
2231                 goto error;
2232
2233         row = tab->con[con].index;
2234         if (isl_tab_row_is_redundant(tab, row))
2235                 type = isl_ineq_redundant;
2236         else if (isl_int_is_neg(tab->mat->row[row][1]) &&
2237                  (tab->rational ||
2238                     isl_int_abs_ge(tab->mat->row[row][1],
2239                                    tab->mat->row[row][0]))) {
2240                 if (at_least_zero(tab, &tab->con[con]))
2241                         type = isl_ineq_cut;
2242                 else
2243                         type = separation_type(tab, row);
2244         } else if (tab->rational ? (sign_of_min(tab, &tab->con[con]) < 0)
2245                              : isl_tab_min_at_most_neg_one(tab, &tab->con[con]))
2246                 type = isl_ineq_cut;
2247         else
2248                 type = isl_ineq_redundant;
2249
2250         if (isl_tab_rollback(tab, snap))
2251                 return isl_ineq_error;
2252         return type;
2253 error:
2254         isl_tab_rollback(tab, snap);
2255         return isl_ineq_error;
2256 }
2257
2258 void isl_tab_dump(struct isl_tab *tab, FILE *out, int indent)
2259 {
2260         unsigned r, c;
2261         int i;
2262
2263         if (!tab) {
2264                 fprintf(out, "%*snull tab\n", indent, "");
2265                 return;
2266         }
2267         fprintf(out, "%*sn_redundant: %d, n_dead: %d", indent, "",
2268                 tab->n_redundant, tab->n_dead);
2269         if (tab->rational)
2270                 fprintf(out, ", rational");
2271         if (tab->empty)
2272                 fprintf(out, ", empty");
2273         fprintf(out, "\n");
2274         fprintf(out, "%*s[", indent, "");
2275         for (i = 0; i < tab->n_var; ++i) {
2276                 if (i)
2277                         fprintf(out, (i == tab->n_param ||
2278                                       i == tab->n_var - tab->n_div) ? "; "
2279                                                                     : ", ");
2280                 fprintf(out, "%c%d%s", tab->var[i].is_row ? 'r' : 'c',
2281                                         tab->var[i].index,
2282                                         tab->var[i].is_zero ? " [=0]" :
2283                                         tab->var[i].is_redundant ? " [R]" : "");
2284         }
2285         fprintf(out, "]\n");
2286         fprintf(out, "%*s[", indent, "");
2287         for (i = 0; i < tab->n_con; ++i) {
2288                 if (i)
2289                         fprintf(out, ", ");
2290                 fprintf(out, "%c%d%s", tab->con[i].is_row ? 'r' : 'c',
2291                                         tab->con[i].index,
2292                                         tab->con[i].is_zero ? " [=0]" :
2293                                         tab->con[i].is_redundant ? " [R]" : "");
2294         }
2295         fprintf(out, "]\n");
2296         fprintf(out, "%*s[", indent, "");
2297         for (i = 0; i < tab->n_row; ++i) {
2298                 const char *sign = "";
2299                 if (i)
2300                         fprintf(out, ", ");
2301                 if (tab->row_sign) {
2302                         if (tab->row_sign[i] == isl_tab_row_unknown)
2303                                 sign = "?";
2304                         else if (tab->row_sign[i] == isl_tab_row_neg)
2305                                 sign = "-";
2306                         else if (tab->row_sign[i] == isl_tab_row_pos)
2307                                 sign = "+";
2308                         else
2309                                 sign = "+-";
2310                 }
2311                 fprintf(out, "r%d: %d%s%s", i, tab->row_var[i],
2312                     isl_tab_var_from_row(tab, i)->is_nonneg ? " [>=0]" : "", sign);
2313         }
2314         fprintf(out, "]\n");
2315         fprintf(out, "%*s[", indent, "");
2316         for (i = 0; i < tab->n_col; ++i) {
2317                 if (i)
2318                         fprintf(out, ", ");
2319                 fprintf(out, "c%d: %d%s", i, tab->col_var[i],
2320                     var_from_col(tab, i)->is_nonneg ? " [>=0]" : "");
2321         }
2322         fprintf(out, "]\n");
2323         r = tab->mat->n_row;
2324         tab->mat->n_row = tab->n_row;
2325         c = tab->mat->n_col;
2326         tab->mat->n_col = 2 + tab->M + tab->n_col;
2327         isl_mat_dump(tab->mat, out, indent);
2328         tab->mat->n_row = r;
2329         tab->mat->n_col = c;
2330         if (tab->bset)
2331                 isl_basic_set_dump(tab->bset, out, indent);
2332 }