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