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