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