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