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