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