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