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