export isl_basic_map_lexmax
[platform/upstream/isl.git] / isl_tab.c
1 #include "isl_mat.h"
2 #include "isl_map_private.h"
3 #include "isl_tab.h"
4 #include "isl_seq.h"
5
6 /*
7  * The implementation of tableaus in this file was inspired by Section 8
8  * of David Detlefs, Greg Nelson and James B. Saxe, "Simplify: a theorem
9  * prover for program checking".
10  */
11
12 struct isl_tab *isl_tab_alloc(struct isl_ctx *ctx,
13         unsigned n_row, unsigned n_var, unsigned M)
14 {
15         int i;
16         struct isl_tab *tab;
17         unsigned off = 2 + M;
18
19         tab = isl_calloc_type(ctx, struct isl_tab);
20         if (!tab)
21                 return NULL;
22         tab->mat = isl_mat_alloc(ctx, n_row, off + n_var);
23         if (!tab->mat)
24                 goto error;
25         tab->var = isl_alloc_array(ctx, struct isl_tab_var, n_var);
26         if (!tab->var)
27                 goto error;
28         tab->con = isl_alloc_array(ctx, struct isl_tab_var, n_row);
29         if (!tab->con)
30                 goto error;
31         tab->col_var = isl_alloc_array(ctx, int, n_var);
32         if (!tab->col_var)
33                 goto error;
34         tab->row_var = isl_alloc_array(ctx, int, n_row);
35         if (!tab->row_var)
36                 goto error;
37         for (i = 0; i < n_var; ++i) {
38                 tab->var[i].index = i;
39                 tab->var[i].is_row = 0;
40                 tab->var[i].is_nonneg = 0;
41                 tab->var[i].is_zero = 0;
42                 tab->var[i].is_redundant = 0;
43                 tab->var[i].frozen = 0;
44                 tab->var[i].negated = 0;
45                 tab->col_var[i] = i;
46         }
47         tab->n_row = 0;
48         tab->n_con = 0;
49         tab->n_eq = 0;
50         tab->max_con = n_row;
51         tab->n_col = n_var;
52         tab->n_var = n_var;
53         tab->max_var = n_var;
54         tab->n_param = 0;
55         tab->n_div = 0;
56         tab->n_dead = 0;
57         tab->n_redundant = 0;
58         tab->need_undo = 0;
59         tab->rational = 0;
60         tab->empty = 0;
61         tab->in_undo = 0;
62         tab->M = M;
63         tab->cone = 0;
64         tab->bottom.type = isl_tab_undo_bottom;
65         tab->bottom.next = NULL;
66         tab->top = &tab->bottom;
67
68         tab->n_zero = 0;
69         tab->n_unbounded = 0;
70         tab->basis = NULL;
71
72         return tab;
73 error:
74         isl_tab_free(tab);
75         return NULL;
76 }
77
78 int isl_tab_extend_cons(struct isl_tab *tab, unsigned n_new)
79 {
80         unsigned off = 2 + tab->M;
81
82         if (!tab)
83                 return -1;
84
85         if (tab->max_con < tab->n_con + n_new) {
86                 struct isl_tab_var *con;
87
88                 con = isl_realloc_array(tab->mat->ctx, tab->con,
89                                     struct isl_tab_var, tab->max_con + n_new);
90                 if (!con)
91                         return -1;
92                 tab->con = con;
93                 tab->max_con += n_new;
94         }
95         if (tab->mat->n_row < tab->n_row + n_new) {
96                 int *row_var;
97
98                 tab->mat = isl_mat_extend(tab->mat,
99                                         tab->n_row + n_new, off + tab->n_col);
100                 if (!tab->mat)
101                         return -1;
102                 row_var = isl_realloc_array(tab->mat->ctx, tab->row_var,
103                                             int, tab->mat->n_row);
104                 if (!row_var)
105                         return -1;
106                 tab->row_var = row_var;
107                 if (tab->row_sign) {
108                         enum isl_tab_row_sign *s;
109                         s = isl_realloc_array(tab->mat->ctx, tab->row_sign,
110                                         enum isl_tab_row_sign, tab->mat->n_row);
111                         if (!s)
112                                 return -1;
113                         tab->row_sign = s;
114                 }
115         }
116         return 0;
117 }
118
119 /* Make room for at least n_new extra variables.
120  * Return -1 if anything went wrong.
121  */
122 int isl_tab_extend_vars(struct isl_tab *tab, unsigned n_new)
123 {
124         struct isl_tab_var *var;
125         unsigned off = 2 + tab->M;
126
127         if (tab->max_var < tab->n_var + n_new) {
128                 var = isl_realloc_array(tab->mat->ctx, tab->var,
129                                     struct isl_tab_var, tab->n_var + n_new);
130                 if (!var)
131                         return -1;
132                 tab->var = var;
133                 tab->max_var += n_new;
134         }
135
136         if (tab->mat->n_col < off + tab->n_col + n_new) {
137                 int *p;
138
139                 tab->mat = isl_mat_extend(tab->mat,
140                                     tab->mat->n_row, off + tab->n_col + n_new);
141                 if (!tab->mat)
142                         return -1;
143                 p = isl_realloc_array(tab->mat->ctx, tab->col_var,
144                                             int, tab->n_col + n_new);
145                 if (!p)
146                         return -1;
147                 tab->col_var = p;
148         }
149
150         return 0;
151 }
152
153 struct isl_tab *isl_tab_extend(struct isl_tab *tab, unsigned n_new)
154 {
155         if (isl_tab_extend_cons(tab, n_new) >= 0)
156                 return tab;
157
158         isl_tab_free(tab);
159         return NULL;
160 }
161
162 static void free_undo(struct isl_tab *tab)
163 {
164         struct isl_tab_undo *undo, *next;
165
166         for (undo = tab->top; undo && undo != &tab->bottom; undo = next) {
167                 next = undo->next;
168                 free(undo);
169         }
170         tab->top = undo;
171 }
172
173 void isl_tab_free(struct isl_tab *tab)
174 {
175         if (!tab)
176                 return;
177         free_undo(tab);
178         isl_mat_free(tab->mat);
179         isl_vec_free(tab->dual);
180         isl_basic_set_free(tab->bset);
181         free(tab->var);
182         free(tab->con);
183         free(tab->row_var);
184         free(tab->col_var);
185         free(tab->row_sign);
186         isl_mat_free(tab->samples);
187         free(tab->sample_index);
188         isl_mat_free(tab->basis);
189         free(tab);
190 }
191
192 struct isl_tab *isl_tab_dup(struct isl_tab *tab)
193 {
194         int i;
195         struct isl_tab *dup;
196         unsigned off;
197
198         if (!tab)
199                 return NULL;
200
201         off = 2 + tab->M;
202         dup = isl_calloc_type(tab->ctx, struct isl_tab);
203         if (!dup)
204                 return NULL;
205         dup->mat = isl_mat_dup(tab->mat);
206         if (!dup->mat)
207                 goto error;
208         dup->var = isl_alloc_array(tab->ctx, struct isl_tab_var, tab->max_var);
209         if (!dup->var)
210                 goto error;
211         for (i = 0; i < tab->n_var; ++i)
212                 dup->var[i] = tab->var[i];
213         dup->con = isl_alloc_array(tab->ctx, struct isl_tab_var, tab->max_con);
214         if (!dup->con)
215                 goto error;
216         for (i = 0; i < tab->n_con; ++i)
217                 dup->con[i] = tab->con[i];
218         dup->col_var = isl_alloc_array(tab->ctx, int, tab->mat->n_col - off);
219         if (!dup->col_var)
220                 goto error;
221         for (i = 0; i < tab->n_col; ++i)
222                 dup->col_var[i] = tab->col_var[i];
223         dup->row_var = isl_alloc_array(tab->ctx, int, tab->mat->n_row);
224         if (!dup->row_var)
225                 goto error;
226         for (i = 0; i < tab->n_row; ++i)
227                 dup->row_var[i] = tab->row_var[i];
228         if (tab->row_sign) {
229                 dup->row_sign = isl_alloc_array(tab->ctx, enum isl_tab_row_sign,
230                                                 tab->mat->n_row);
231                 if (!dup->row_sign)
232                         goto error;
233                 for (i = 0; i < tab->n_row; ++i)
234                         dup->row_sign[i] = tab->row_sign[i];
235         }
236         if (tab->samples) {
237                 dup->samples = isl_mat_dup(tab->samples);
238                 if (!dup->samples)
239                         goto error;
240                 dup->sample_index = isl_alloc_array(tab->mat->ctx, int,
241                                                         tab->samples->n_row);
242                 if (!dup->sample_index)
243                         goto error;
244                 dup->n_sample = tab->n_sample;
245                 dup->n_outside = tab->n_outside;
246         }
247         dup->n_row = tab->n_row;
248         dup->n_con = tab->n_con;
249         dup->n_eq = tab->n_eq;
250         dup->max_con = tab->max_con;
251         dup->n_col = tab->n_col;
252         dup->n_var = tab->n_var;
253         dup->max_var = tab->max_var;
254         dup->n_param = tab->n_param;
255         dup->n_div = tab->n_div;
256         dup->n_dead = tab->n_dead;
257         dup->n_redundant = tab->n_redundant;
258         dup->rational = tab->rational;
259         dup->empty = tab->empty;
260         dup->need_undo = 0;
261         dup->in_undo = 0;
262         dup->M = tab->M;
263         tab->cone = tab->cone;
264         dup->bottom.type = isl_tab_undo_bottom;
265         dup->bottom.next = NULL;
266         dup->top = &dup->bottom;
267
268         dup->n_zero = tab->n_zero;
269         dup->n_unbounded = tab->n_unbounded;
270         dup->basis = isl_mat_dup(tab->basis);
271
272         return dup;
273 error:
274         isl_tab_free(dup);
275         return NULL;
276 }
277
278 /* Construct the coefficient matrix of the product tableau
279  * of two tableaus.
280  * mat{1,2} is the coefficient matrix of tableau {1,2}
281  * row{1,2} is the number of rows in tableau {1,2}
282  * col{1,2} is the number of columns in tableau {1,2}
283  * off is the offset to the coefficient column (skipping the
284  *      denominator, the constant term and the big parameter if any)
285  * r{1,2} is the number of redundant rows in tableau {1,2}
286  * d{1,2} is the number of dead columns in tableau {1,2}
287  *
288  * The order of the rows and columns in the result is as explained
289  * in isl_tab_product.
290  */
291 static struct isl_mat *tab_mat_product(struct isl_mat *mat1,
292         struct isl_mat *mat2, unsigned row1, unsigned row2,
293         unsigned col1, unsigned col2,
294         unsigned off, unsigned r1, unsigned r2, unsigned d1, unsigned d2)
295 {
296         int i;
297         struct isl_mat *prod;
298         unsigned n;
299
300         prod = isl_mat_alloc(mat1->ctx, mat1->n_row + mat2->n_row,
301                                         off + col1 + col2);
302
303         n = 0;
304         for (i = 0; i < r1; ++i) {
305                 isl_seq_cpy(prod->row[n + i], mat1->row[i], off + d1);
306                 isl_seq_clr(prod->row[n + i] + off + d1, d2);
307                 isl_seq_cpy(prod->row[n + i] + off + d1 + d2,
308                                 mat1->row[i] + off + d1, col1 - d1);
309                 isl_seq_clr(prod->row[n + i] + off + col1 + d1, col2 - d2);
310         }
311
312         n += r1;
313         for (i = 0; i < r2; ++i) {
314                 isl_seq_cpy(prod->row[n + i], mat2->row[i], off);
315                 isl_seq_clr(prod->row[n + i] + off, d1);
316                 isl_seq_cpy(prod->row[n + i] + off + d1,
317                             mat2->row[i] + off, d2);
318                 isl_seq_clr(prod->row[n + i] + off + d1 + d2, col1 - d1);
319                 isl_seq_cpy(prod->row[n + i] + off + col1 + d1,
320                             mat2->row[i] + off + d2, col2 - d2);
321         }
322
323         n += r2;
324         for (i = 0; i < row1 - r1; ++i) {
325                 isl_seq_cpy(prod->row[n + i], mat1->row[r1 + i], off + d1);
326                 isl_seq_clr(prod->row[n + i] + off + d1, d2);
327                 isl_seq_cpy(prod->row[n + i] + off + d1 + d2,
328                                 mat1->row[r1 + i] + off + d1, col1 - d1);
329                 isl_seq_clr(prod->row[n + i] + off + col1 + d1, col2 - d2);
330         }
331
332         n += row1 - r1;
333         for (i = 0; i < row2 - r2; ++i) {
334                 isl_seq_cpy(prod->row[n + i], mat2->row[r2 + i], off);
335                 isl_seq_clr(prod->row[n + i] + off, d1);
336                 isl_seq_cpy(prod->row[n + i] + off + d1,
337                             mat2->row[r2 + i] + off, d2);
338                 isl_seq_clr(prod->row[n + i] + off + d1 + d2, col1 - d1);
339                 isl_seq_cpy(prod->row[n + i] + off + col1 + d1,
340                             mat2->row[r2 + i] + off + d2, col2 - d2);
341         }
342
343         return prod;
344 }
345
346 /* Update the row or column index of a variable that corresponds
347  * to a variable in the first input tableau.
348  */
349 static void update_index1(struct isl_tab_var *var,
350         unsigned r1, unsigned r2, unsigned d1, unsigned d2)
351 {
352         if (var->index == -1)
353                 return;
354         if (var->is_row && var->index >= r1)
355                 var->index += r2;
356         if (!var->is_row && var->index >= d1)
357                 var->index += d2;
358 }
359
360 /* Update the row or column index of a variable that corresponds
361  * to a variable in the second input tableau.
362  */
363 static void update_index2(struct isl_tab_var *var,
364         unsigned row1, unsigned col1,
365         unsigned r1, unsigned r2, unsigned d1, unsigned d2)
366 {
367         if (var->index == -1)
368                 return;
369         if (var->is_row) {
370                 if (var->index < r2)
371                         var->index += r1;
372                 else
373                         var->index += row1;
374         } else {
375                 if (var->index < d2)
376                         var->index += d1;
377                 else
378                         var->index += col1;
379         }
380 }
381
382 /* Create a tableau that represents the Cartesian product of the sets
383  * represented by tableaus tab1 and tab2.
384  * The order of the rows in the product is
385  *      - redundant rows of tab1
386  *      - redundant rows of tab2
387  *      - non-redundant rows of tab1
388  *      - non-redundant rows of tab2
389  * The order of the columns is
390  *      - denominator
391  *      - constant term
392  *      - coefficient of big parameter, if any
393  *      - dead columns of tab1
394  *      - dead columns of tab2
395  *      - live columns of tab1
396  *      - live columns of tab2
397  * The order of the variables and the constraints is a concatenation
398  * of order in the two input tableaus.
399  */
400 struct isl_tab *isl_tab_product(struct isl_tab *tab1, struct isl_tab *tab2)
401 {
402         int i;
403         struct isl_tab *prod;
404         unsigned off;
405         unsigned r1, r2, d1, d2;
406
407         if (!tab1 || !tab2)
408                 return NULL;
409
410         isl_assert(tab1->mat->ctx, tab1->M == tab2->M, return NULL);
411         isl_assert(tab1->mat->ctx, tab1->rational == tab2->rational, return NULL);
412         isl_assert(tab1->mat->ctx, tab1->cone == tab2->cone, return NULL);
413         isl_assert(tab1->mat->ctx, !tab1->row_sign, return NULL);
414         isl_assert(tab1->mat->ctx, !tab2->row_sign, return NULL);
415         isl_assert(tab1->mat->ctx, tab1->n_param == 0, return NULL);
416         isl_assert(tab1->mat->ctx, tab2->n_param == 0, return NULL);
417         isl_assert(tab1->mat->ctx, tab1->n_div == 0, return NULL);
418         isl_assert(tab1->mat->ctx, tab2->n_div == 0, return NULL);
419
420         off = 2 + tab1->M;
421         r1 = tab1->n_redundant;
422         r2 = tab2->n_redundant;
423         d1 = tab1->n_dead;
424         d2 = tab2->n_dead;
425         prod = isl_calloc_type(tab1->mat->ctx, struct isl_tab);
426         if (!prod)
427                 return NULL;
428         prod->mat = tab_mat_product(tab1->mat, tab2->mat,
429                                 tab1->n_row, tab2->n_row,
430                                 tab1->n_col, tab2->n_col, off, r1, r2, d1, d2);
431         if (!prod->mat)
432                 goto error;
433         prod->var = isl_alloc_array(tab1->mat->ctx, struct isl_tab_var,
434                                         tab1->max_var + tab2->max_var);
435         if (!prod->var)
436                 goto error;
437         for (i = 0; i < tab1->n_var; ++i) {
438                 prod->var[i] = tab1->var[i];
439                 update_index1(&prod->var[i], r1, r2, d1, d2);
440         }
441         for (i = 0; i < tab2->n_var; ++i) {
442                 prod->var[tab1->n_var + i] = tab2->var[i];
443                 update_index2(&prod->var[tab1->n_var + i],
444                                 tab1->n_row, tab1->n_col,
445                                 r1, r2, d1, d2);
446         }
447         prod->con = isl_alloc_array(tab1->mat->ctx, struct isl_tab_var,
448                                         tab1->max_con +  tab2->max_con);
449         if (!prod->con)
450                 goto error;
451         for (i = 0; i < tab1->n_con; ++i) {
452                 prod->con[i] = tab1->con[i];
453                 update_index1(&prod->con[i], r1, r2, d1, d2);
454         }
455         for (i = 0; i < tab2->n_con; ++i) {
456                 prod->con[tab1->n_con + i] = tab2->con[i];
457                 update_index2(&prod->con[tab1->n_con + i],
458                                 tab1->n_row, tab1->n_col,
459                                 r1, r2, d1, d2);
460         }
461         prod->col_var = isl_alloc_array(tab1->mat->ctx, int,
462                                         tab1->n_col + tab2->n_col);
463         if (!prod->col_var)
464                 goto error;
465         for (i = 0; i < tab1->n_col; ++i) {
466                 int pos = i < d1 ? i : i + d2;
467                 prod->col_var[pos] = tab1->col_var[i];
468         }
469         for (i = 0; i < tab2->n_col; ++i) {
470                 int pos = i < d2 ? d1 + i : tab1->n_col + i;
471                 int t = tab2->col_var[i];
472                 if (t >= 0)
473                         t += tab1->n_var;
474                 else
475                         t -= tab1->n_con;
476                 prod->col_var[pos] = t;
477         }
478         prod->row_var = isl_alloc_array(tab1->mat->ctx, int,
479                                         tab1->mat->n_row + tab2->mat->n_row);
480         if (!prod->row_var)
481                 goto error;
482         for (i = 0; i < tab1->n_row; ++i) {
483                 int pos = i < r1 ? i : i + r2;
484                 prod->row_var[pos] = tab1->row_var[i];
485         }
486         for (i = 0; i < tab2->n_row; ++i) {
487                 int pos = i < r2 ? r1 + i : tab1->n_row + i;
488                 int t = tab2->row_var[i];
489                 if (t >= 0)
490                         t += tab1->n_var;
491                 else
492                         t -= tab1->n_con;
493                 prod->row_var[pos] = t;
494         }
495         prod->samples = NULL;
496         prod->sample_index = NULL;
497         prod->n_row = tab1->n_row + tab2->n_row;
498         prod->n_con = tab1->n_con + tab2->n_con;
499         prod->n_eq = 0;
500         prod->max_con = tab1->max_con + tab2->max_con;
501         prod->n_col = tab1->n_col + tab2->n_col;
502         prod->n_var = tab1->n_var + tab2->n_var;
503         prod->max_var = tab1->max_var + tab2->max_var;
504         prod->n_param = 0;
505         prod->n_div = 0;
506         prod->n_dead = tab1->n_dead + tab2->n_dead;
507         prod->n_redundant = tab1->n_redundant + tab2->n_redundant;
508         prod->rational = tab1->rational;
509         prod->empty = tab1->empty || tab2->empty;
510         prod->need_undo = 0;
511         prod->in_undo = 0;
512         prod->M = tab1->M;
513         prod->cone = tab1->cone;
514         prod->bottom.type = isl_tab_undo_bottom;
515         prod->bottom.next = NULL;
516         prod->top = &prod->bottom;
517
518         prod->n_zero = 0;
519         prod->n_unbounded = 0;
520         prod->basis = NULL;
521
522         return prod;
523 error:
524         isl_tab_free(prod);
525         return NULL;
526 }
527
528 static struct isl_tab_var *var_from_index(struct isl_tab *tab, int i)
529 {
530         if (i >= 0)
531                 return &tab->var[i];
532         else
533                 return &tab->con[~i];
534 }
535
536 struct isl_tab_var *isl_tab_var_from_row(struct isl_tab *tab, int i)
537 {
538         return var_from_index(tab, tab->row_var[i]);
539 }
540
541 static struct isl_tab_var *var_from_col(struct isl_tab *tab, int i)
542 {
543         return var_from_index(tab, tab->col_var[i]);
544 }
545
546 /* Check if there are any upper bounds on column variable "var",
547  * i.e., non-negative rows where var appears with a negative coefficient.
548  * Return 1 if there are no such bounds.
549  */
550 static int max_is_manifestly_unbounded(struct isl_tab *tab,
551         struct isl_tab_var *var)
552 {
553         int i;
554         unsigned off = 2 + tab->M;
555
556         if (var->is_row)
557                 return 0;
558         for (i = tab->n_redundant; i < tab->n_row; ++i) {
559                 if (!isl_int_is_neg(tab->mat->row[i][off + var->index]))
560                         continue;
561                 if (isl_tab_var_from_row(tab, i)->is_nonneg)
562                         return 0;
563         }
564         return 1;
565 }
566
567 /* Check if there are any lower bounds on column variable "var",
568  * i.e., non-negative rows where var appears with a positive coefficient.
569  * Return 1 if there are no such bounds.
570  */
571 static int min_is_manifestly_unbounded(struct isl_tab *tab,
572         struct isl_tab_var *var)
573 {
574         int i;
575         unsigned off = 2 + tab->M;
576
577         if (var->is_row)
578                 return 0;
579         for (i = tab->n_redundant; i < tab->n_row; ++i) {
580                 if (!isl_int_is_pos(tab->mat->row[i][off + var->index]))
581                         continue;
582                 if (isl_tab_var_from_row(tab, i)->is_nonneg)
583                         return 0;
584         }
585         return 1;
586 }
587
588 static int row_cmp(struct isl_tab *tab, int r1, int r2, int c, isl_int t)
589 {
590         unsigned off = 2 + tab->M;
591
592         if (tab->M) {
593                 int s;
594                 isl_int_mul(t, tab->mat->row[r1][2], tab->mat->row[r2][off+c]);
595                 isl_int_submul(t, tab->mat->row[r2][2], tab->mat->row[r1][off+c]);
596                 s = isl_int_sgn(t);
597                 if (s)
598                         return s;
599         }
600         isl_int_mul(t, tab->mat->row[r1][1], tab->mat->row[r2][off + c]);
601         isl_int_submul(t, tab->mat->row[r2][1], tab->mat->row[r1][off + c]);
602         return isl_int_sgn(t);
603 }
604
605 /* Given the index of a column "c", return the index of a row
606  * that can be used to pivot the column in, with either an increase
607  * (sgn > 0) or a decrease (sgn < 0) of the corresponding variable.
608  * If "var" is not NULL, then the row returned will be different from
609  * the one associated with "var".
610  *
611  * Each row in the tableau is of the form
612  *
613  *      x_r = a_r0 + \sum_i a_ri x_i
614  *
615  * Only rows with x_r >= 0 and with the sign of a_ri opposite to "sgn"
616  * impose any limit on the increase or decrease in the value of x_c
617  * and this bound is equal to a_r0 / |a_rc|.  We are therefore looking
618  * for the row with the smallest (most stringent) such bound.
619  * Note that the common denominator of each row drops out of the fraction.
620  * To check if row j has a smaller bound than row r, i.e.,
621  * a_j0 / |a_jc| < a_r0 / |a_rc| or a_j0 |a_rc| < a_r0 |a_jc|,
622  * we check if -sign(a_jc) (a_j0 a_rc - a_r0 a_jc) < 0,
623  * where -sign(a_jc) is equal to "sgn".
624  */
625 static int pivot_row(struct isl_tab *tab,
626         struct isl_tab_var *var, int sgn, int c)
627 {
628         int j, r, tsgn;
629         isl_int t;
630         unsigned off = 2 + tab->M;
631
632         isl_int_init(t);
633         r = -1;
634         for (j = tab->n_redundant; j < tab->n_row; ++j) {
635                 if (var && j == var->index)
636                         continue;
637                 if (!isl_tab_var_from_row(tab, j)->is_nonneg)
638                         continue;
639                 if (sgn * isl_int_sgn(tab->mat->row[j][off + c]) >= 0)
640                         continue;
641                 if (r < 0) {
642                         r = j;
643                         continue;
644                 }
645                 tsgn = sgn * row_cmp(tab, r, j, c, t);
646                 if (tsgn < 0 || (tsgn == 0 &&
647                                             tab->row_var[j] < tab->row_var[r]))
648                         r = j;
649         }
650         isl_int_clear(t);
651         return r;
652 }
653
654 /* Find a pivot (row and col) that will increase (sgn > 0) or decrease
655  * (sgn < 0) the value of row variable var.
656  * If not NULL, then skip_var is a row variable that should be ignored
657  * while looking for a pivot row.  It is usually equal to var.
658  *
659  * As the given row in the tableau is of the form
660  *
661  *      x_r = a_r0 + \sum_i a_ri x_i
662  *
663  * we need to find a column such that the sign of a_ri is equal to "sgn"
664  * (such that an increase in x_i will have the desired effect) or a
665  * column with a variable that may attain negative values.
666  * If a_ri is positive, then we need to move x_i in the same direction
667  * to obtain the desired effect.  Otherwise, x_i has to move in the
668  * opposite direction.
669  */
670 static void find_pivot(struct isl_tab *tab,
671         struct isl_tab_var *var, struct isl_tab_var *skip_var,
672         int sgn, int *row, int *col)
673 {
674         int j, r, c;
675         isl_int *tr;
676
677         *row = *col = -1;
678
679         isl_assert(tab->mat->ctx, var->is_row, return);
680         tr = tab->mat->row[var->index] + 2 + tab->M;
681
682         c = -1;
683         for (j = tab->n_dead; j < tab->n_col; ++j) {
684                 if (isl_int_is_zero(tr[j]))
685                         continue;
686                 if (isl_int_sgn(tr[j]) != sgn &&
687                     var_from_col(tab, j)->is_nonneg)
688                         continue;
689                 if (c < 0 || tab->col_var[j] < tab->col_var[c])
690                         c = j;
691         }
692         if (c < 0)
693                 return;
694
695         sgn *= isl_int_sgn(tr[c]);
696         r = pivot_row(tab, skip_var, sgn, c);
697         *row = r < 0 ? var->index : r;
698         *col = c;
699 }
700
701 /* Return 1 if row "row" represents an obviously redundant inequality.
702  * This means
703  *      - it represents an inequality or a variable
704  *      - that is the sum of a non-negative sample value and a positive
705  *        combination of zero or more non-negative constraints.
706  */
707 int isl_tab_row_is_redundant(struct isl_tab *tab, int row)
708 {
709         int i;
710         unsigned off = 2 + tab->M;
711
712         if (tab->row_var[row] < 0 && !isl_tab_var_from_row(tab, row)->is_nonneg)
713                 return 0;
714
715         if (isl_int_is_neg(tab->mat->row[row][1]))
716                 return 0;
717         if (tab->M && isl_int_is_neg(tab->mat->row[row][2]))
718                 return 0;
719
720         for (i = tab->n_dead; i < tab->n_col; ++i) {
721                 if (isl_int_is_zero(tab->mat->row[row][off + i]))
722                         continue;
723                 if (tab->col_var[i] >= 0)
724                         return 0;
725                 if (isl_int_is_neg(tab->mat->row[row][off + i]))
726                         return 0;
727                 if (!var_from_col(tab, i)->is_nonneg)
728                         return 0;
729         }
730         return 1;
731 }
732
733 static void swap_rows(struct isl_tab *tab, int row1, int row2)
734 {
735         int t;
736         t = tab->row_var[row1];
737         tab->row_var[row1] = tab->row_var[row2];
738         tab->row_var[row2] = t;
739         isl_tab_var_from_row(tab, row1)->index = row1;
740         isl_tab_var_from_row(tab, row2)->index = row2;
741         tab->mat = isl_mat_swap_rows(tab->mat, row1, row2);
742
743         if (!tab->row_sign)
744                 return;
745         t = tab->row_sign[row1];
746         tab->row_sign[row1] = tab->row_sign[row2];
747         tab->row_sign[row2] = t;
748 }
749
750 static int push_union(struct isl_tab *tab,
751         enum isl_tab_undo_type type, union isl_tab_undo_val u) WARN_UNUSED;
752 static int push_union(struct isl_tab *tab,
753         enum isl_tab_undo_type type, union isl_tab_undo_val u)
754 {
755         struct isl_tab_undo *undo;
756
757         if (!tab->need_undo)
758                 return 0;
759
760         undo = isl_alloc_type(tab->mat->ctx, struct isl_tab_undo);
761         if (!undo)
762                 return -1;
763         undo->type = type;
764         undo->u = u;
765         undo->next = tab->top;
766         tab->top = undo;
767
768         return 0;
769 }
770
771 int isl_tab_push_var(struct isl_tab *tab,
772         enum isl_tab_undo_type type, struct isl_tab_var *var)
773 {
774         union isl_tab_undo_val u;
775         if (var->is_row)
776                 u.var_index = tab->row_var[var->index];
777         else
778                 u.var_index = tab->col_var[var->index];
779         return push_union(tab, type, u);
780 }
781
782 int isl_tab_push(struct isl_tab *tab, enum isl_tab_undo_type type)
783 {
784         union isl_tab_undo_val u = { 0 };
785         return push_union(tab, type, u);
786 }
787
788 /* Push a record on the undo stack describing the current basic
789  * variables, so that the this state can be restored during rollback.
790  */
791 int isl_tab_push_basis(struct isl_tab *tab)
792 {
793         int i;
794         union isl_tab_undo_val u;
795
796         u.col_var = isl_alloc_array(tab->mat->ctx, int, tab->n_col);
797         if (!u.col_var)
798                 return -1;
799         for (i = 0; i < tab->n_col; ++i)
800                 u.col_var[i] = tab->col_var[i];
801         return push_union(tab, isl_tab_undo_saved_basis, u);
802 }
803
804 int isl_tab_push_callback(struct isl_tab *tab, struct isl_tab_callback *callback)
805 {
806         union isl_tab_undo_val u;
807         u.callback = callback;
808         return push_union(tab, isl_tab_undo_callback, u);
809 }
810
811 struct isl_tab *isl_tab_init_samples(struct isl_tab *tab)
812 {
813         if (!tab)
814                 return NULL;
815
816         tab->n_sample = 0;
817         tab->n_outside = 0;
818         tab->samples = isl_mat_alloc(tab->mat->ctx, 1, 1 + tab->n_var);
819         if (!tab->samples)
820                 goto error;
821         tab->sample_index = isl_alloc_array(tab->mat->ctx, int, 1);
822         if (!tab->sample_index)
823                 goto error;
824         return tab;
825 error:
826         isl_tab_free(tab);
827         return NULL;
828 }
829
830 struct isl_tab *isl_tab_add_sample(struct isl_tab *tab,
831         __isl_take isl_vec *sample)
832 {
833         if (!tab || !sample)
834                 goto error;
835
836         if (tab->n_sample + 1 > tab->samples->n_row) {
837                 int *t = isl_realloc_array(tab->mat->ctx,
838                             tab->sample_index, int, tab->n_sample + 1);
839                 if (!t)
840                         goto error;
841                 tab->sample_index = t;
842         }
843
844         tab->samples = isl_mat_extend(tab->samples,
845                                 tab->n_sample + 1, tab->samples->n_col);
846         if (!tab->samples)
847                 goto error;
848
849         isl_seq_cpy(tab->samples->row[tab->n_sample], sample->el, sample->size);
850         isl_vec_free(sample);
851         tab->sample_index[tab->n_sample] = tab->n_sample;
852         tab->n_sample++;
853
854         return tab;
855 error:
856         isl_vec_free(sample);
857         isl_tab_free(tab);
858         return NULL;
859 }
860
861 struct isl_tab *isl_tab_drop_sample(struct isl_tab *tab, int s)
862 {
863         if (s != tab->n_outside) {
864                 int t = tab->sample_index[tab->n_outside];
865                 tab->sample_index[tab->n_outside] = tab->sample_index[s];
866                 tab->sample_index[s] = t;
867                 isl_mat_swap_rows(tab->samples, tab->n_outside, s);
868         }
869         tab->n_outside++;
870         if (isl_tab_push(tab, isl_tab_undo_drop_sample) < 0) {
871                 isl_tab_free(tab);
872                 return NULL;
873         }
874
875         return tab;
876 }
877
878 /* Record the current number of samples so that we can remove newer
879  * samples during a rollback.
880  */
881 int isl_tab_save_samples(struct isl_tab *tab)
882 {
883         union isl_tab_undo_val u;
884
885         if (!tab)
886                 return -1;
887
888         u.n = tab->n_sample;
889         return push_union(tab, isl_tab_undo_saved_samples, u);
890 }
891
892 /* Mark row with index "row" as being redundant.
893  * If we may need to undo the operation or if the row represents
894  * a variable of the original problem, the row is kept,
895  * but no longer considered when looking for a pivot row.
896  * Otherwise, the row is simply removed.
897  *
898  * The row may be interchanged with some other row.  If it
899  * is interchanged with a later row, return 1.  Otherwise return 0.
900  * If the rows are checked in order in the calling function,
901  * then a return value of 1 means that the row with the given
902  * row number may now contain a different row that hasn't been checked yet.
903  */
904 int isl_tab_mark_redundant(struct isl_tab *tab, int row)
905 {
906         struct isl_tab_var *var = isl_tab_var_from_row(tab, row);
907         var->is_redundant = 1;
908         isl_assert(tab->mat->ctx, row >= tab->n_redundant, return -1);
909         if (tab->need_undo || tab->row_var[row] >= 0) {
910                 if (tab->row_var[row] >= 0 && !var->is_nonneg) {
911                         var->is_nonneg = 1;
912                         if (isl_tab_push_var(tab, isl_tab_undo_nonneg, var) < 0)
913                                 return -1;
914                 }
915                 if (row != tab->n_redundant)
916                         swap_rows(tab, row, tab->n_redundant);
917                 tab->n_redundant++;
918                 return isl_tab_push_var(tab, isl_tab_undo_redundant, var);
919         } else {
920                 if (row != tab->n_row - 1)
921                         swap_rows(tab, row, tab->n_row - 1);
922                 isl_tab_var_from_row(tab, tab->n_row - 1)->index = -1;
923                 tab->n_row--;
924                 return 1;
925         }
926 }
927
928 struct isl_tab *isl_tab_mark_empty(struct isl_tab *tab)
929 {
930         if (!tab)
931                 return NULL;
932         if (!tab->empty && tab->need_undo)
933                 if (isl_tab_push(tab, isl_tab_undo_empty) < 0) {
934                         isl_tab_free(tab);
935                         return NULL;
936                 }
937         tab->empty = 1;
938         return tab;
939 }
940
941 /* Update the rows signs after a pivot of "row" and "col", with "row_sgn"
942  * the original sign of the pivot element.
943  * We only keep track of row signs during PILP solving and in this case
944  * we only pivot a row with negative sign (meaning the value is always
945  * non-positive) using a positive pivot element.
946  *
947  * For each row j, the new value of the parametric constant is equal to
948  *
949  *      a_j0 - a_jc a_r0/a_rc
950  *
951  * where a_j0 is the original parametric constant, a_rc is the pivot element,
952  * a_r0 is the parametric constant of the pivot row and a_jc is the
953  * pivot column entry of the row j.
954  * Since a_r0 is non-positive and a_rc is positive, the sign of row j
955  * remains the same if a_jc has the same sign as the row j or if
956  * a_jc is zero.  In all other cases, we reset the sign to "unknown".
957  */
958 static void update_row_sign(struct isl_tab *tab, int row, int col, int row_sgn)
959 {
960         int i;
961         struct isl_mat *mat = tab->mat;
962         unsigned off = 2 + tab->M;
963
964         if (!tab->row_sign)
965                 return;
966
967         if (tab->row_sign[row] == 0)
968                 return;
969         isl_assert(mat->ctx, row_sgn > 0, return);
970         isl_assert(mat->ctx, tab->row_sign[row] == isl_tab_row_neg, return);
971         tab->row_sign[row] = isl_tab_row_pos;
972         for (i = 0; i < tab->n_row; ++i) {
973                 int s;
974                 if (i == row)
975                         continue;
976                 s = isl_int_sgn(mat->row[i][off + col]);
977                 if (!s)
978                         continue;
979                 if (!tab->row_sign[i])
980                         continue;
981                 if (s < 0 && tab->row_sign[i] == isl_tab_row_neg)
982                         continue;
983                 if (s > 0 && tab->row_sign[i] == isl_tab_row_pos)
984                         continue;
985                 tab->row_sign[i] = isl_tab_row_unknown;
986         }
987 }
988
989 /* Given a row number "row" and a column number "col", pivot the tableau
990  * such that the associated variables are interchanged.
991  * The given row in the tableau expresses
992  *
993  *      x_r = a_r0 + \sum_i a_ri x_i
994  *
995  * or
996  *
997  *      x_c = 1/a_rc x_r - a_r0/a_rc + sum_{i \ne r} -a_ri/a_rc
998  *
999  * Substituting this equality into the other rows
1000  *
1001  *      x_j = a_j0 + \sum_i a_ji x_i
1002  *
1003  * with a_jc \ne 0, we obtain
1004  *
1005  *      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 
1006  *
1007  * The tableau
1008  *
1009  *      n_rc/d_r                n_ri/d_r
1010  *      n_jc/d_j                n_ji/d_j
1011  *
1012  * where i is any other column and j is any other row,
1013  * is therefore transformed into
1014  *
1015  * s(n_rc)d_r/|n_rc|            -s(n_rc)n_ri/|n_rc|
1016  * 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)
1017  *
1018  * The transformation is performed along the following steps
1019  *
1020  *      d_r/n_rc                n_ri/n_rc
1021  *      n_jc/d_j                n_ji/d_j
1022  *
1023  *      s(n_rc)d_r/|n_rc|       -s(n_rc)n_ri/|n_rc|
1024  *      n_jc/d_j                n_ji/d_j
1025  *
1026  *      s(n_rc)d_r/|n_rc|       -s(n_rc)n_ri/|n_rc|
1027  *      n_jc/(|n_rc| d_j)       n_ji/(|n_rc| d_j)
1028  *
1029  *      s(n_rc)d_r/|n_rc|       -s(n_rc)n_ri/|n_rc|
1030  *      n_jc/(|n_rc| d_j)       (n_ji |n_rc|)/(|n_rc| d_j)
1031  *
1032  *      s(n_rc)d_r/|n_rc|       -s(n_rc)n_ri/|n_rc|
1033  *      n_jc/(|n_rc| d_j)       (n_ji |n_rc| - s(n_rc)n_jc n_ri)/(|n_rc| d_j)
1034  *
1035  * s(n_rc)d_r/|n_rc|            -s(n_rc)n_ri/|n_rc|
1036  * 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)
1037  *
1038  */
1039 int isl_tab_pivot(struct isl_tab *tab, int row, int col)
1040 {
1041         int i, j;
1042         int sgn;
1043         int t;
1044         struct isl_mat *mat = tab->mat;
1045         struct isl_tab_var *var;
1046         unsigned off = 2 + tab->M;
1047
1048         isl_int_swap(mat->row[row][0], mat->row[row][off + col]);
1049         sgn = isl_int_sgn(mat->row[row][0]);
1050         if (sgn < 0) {
1051                 isl_int_neg(mat->row[row][0], mat->row[row][0]);
1052                 isl_int_neg(mat->row[row][off + col], mat->row[row][off + col]);
1053         } else
1054                 for (j = 0; j < off - 1 + tab->n_col; ++j) {
1055                         if (j == off - 1 + col)
1056                                 continue;
1057                         isl_int_neg(mat->row[row][1 + j], mat->row[row][1 + j]);
1058                 }
1059         if (!isl_int_is_one(mat->row[row][0]))
1060                 isl_seq_normalize(mat->ctx, mat->row[row], off + tab->n_col);
1061         for (i = 0; i < tab->n_row; ++i) {
1062                 if (i == row)
1063                         continue;
1064                 if (isl_int_is_zero(mat->row[i][off + col]))
1065                         continue;
1066                 isl_int_mul(mat->row[i][0], mat->row[i][0], mat->row[row][0]);
1067                 for (j = 0; j < off - 1 + tab->n_col; ++j) {
1068                         if (j == off - 1 + col)
1069                                 continue;
1070                         isl_int_mul(mat->row[i][1 + j],
1071                                     mat->row[i][1 + j], mat->row[row][0]);
1072                         isl_int_addmul(mat->row[i][1 + j],
1073                                     mat->row[i][off + col], mat->row[row][1 + j]);
1074                 }
1075                 isl_int_mul(mat->row[i][off + col],
1076                             mat->row[i][off + col], mat->row[row][off + col]);
1077                 if (!isl_int_is_one(mat->row[i][0]))
1078                         isl_seq_normalize(mat->ctx, mat->row[i], off + tab->n_col);
1079         }
1080         t = tab->row_var[row];
1081         tab->row_var[row] = tab->col_var[col];
1082         tab->col_var[col] = t;
1083         var = isl_tab_var_from_row(tab, row);
1084         var->is_row = 1;
1085         var->index = row;
1086         var = var_from_col(tab, col);
1087         var->is_row = 0;
1088         var->index = col;
1089         update_row_sign(tab, row, col, sgn);
1090         if (tab->in_undo)
1091                 return 0;
1092         for (i = tab->n_redundant; i < tab->n_row; ++i) {
1093                 if (isl_int_is_zero(mat->row[i][off + col]))
1094                         continue;
1095                 if (!isl_tab_var_from_row(tab, i)->frozen &&
1096                     isl_tab_row_is_redundant(tab, i)) {
1097                         int redo = isl_tab_mark_redundant(tab, i);
1098                         if (redo < 0)
1099                                 return -1;
1100                         if (redo)
1101                                 --i;
1102                 }
1103         }
1104         return 0;
1105 }
1106
1107 /* If "var" represents a column variable, then pivot is up (sgn > 0)
1108  * or down (sgn < 0) to a row.  The variable is assumed not to be
1109  * unbounded in the specified direction.
1110  * If sgn = 0, then the variable is unbounded in both directions,
1111  * and we pivot with any row we can find.
1112  */
1113 static int to_row(struct isl_tab *tab, struct isl_tab_var *var, int sign) WARN_UNUSED;
1114 static int to_row(struct isl_tab *tab, struct isl_tab_var *var, int sign)
1115 {
1116         int r;
1117         unsigned off = 2 + tab->M;
1118
1119         if (var->is_row)
1120                 return 0;
1121
1122         if (sign == 0) {
1123                 for (r = tab->n_redundant; r < tab->n_row; ++r)
1124                         if (!isl_int_is_zero(tab->mat->row[r][off+var->index]))
1125                                 break;
1126                 isl_assert(tab->mat->ctx, r < tab->n_row, return -1);
1127         } else {
1128                 r = pivot_row(tab, NULL, sign, var->index);
1129                 isl_assert(tab->mat->ctx, r >= 0, return -1);
1130         }
1131
1132         return isl_tab_pivot(tab, r, var->index);
1133 }
1134
1135 static void check_table(struct isl_tab *tab)
1136 {
1137         int i;
1138
1139         if (tab->empty)
1140                 return;
1141         for (i = 0; i < tab->n_row; ++i) {
1142                 if (!isl_tab_var_from_row(tab, i)->is_nonneg)
1143                         continue;
1144                 assert(!isl_int_is_neg(tab->mat->row[i][1]));
1145         }
1146 }
1147
1148 /* Return the sign of the maximal value of "var".
1149  * If the sign is not negative, then on return from this function,
1150  * the sample value will also be non-negative.
1151  *
1152  * If "var" is manifestly unbounded wrt positive values, we are done.
1153  * Otherwise, we pivot the variable up to a row if needed
1154  * Then we continue pivoting down until either
1155  *      - no more down pivots can be performed
1156  *      - the sample value is positive
1157  *      - the variable is pivoted into a manifestly unbounded column
1158  */
1159 static int sign_of_max(struct isl_tab *tab, struct isl_tab_var *var)
1160 {
1161         int row, col;
1162
1163         if (max_is_manifestly_unbounded(tab, var))
1164                 return 1;
1165         if (to_row(tab, var, 1) < 0)
1166                 return -2;
1167         while (!isl_int_is_pos(tab->mat->row[var->index][1])) {
1168                 find_pivot(tab, var, var, 1, &row, &col);
1169                 if (row == -1)
1170                         return isl_int_sgn(tab->mat->row[var->index][1]);
1171                 if (isl_tab_pivot(tab, row, col) < 0)
1172                         return -2;
1173                 if (!var->is_row) /* manifestly unbounded */
1174                         return 1;
1175         }
1176         return 1;
1177 }
1178
1179 static int row_is_neg(struct isl_tab *tab, int row)
1180 {
1181         if (!tab->M)
1182                 return isl_int_is_neg(tab->mat->row[row][1]);
1183         if (isl_int_is_pos(tab->mat->row[row][2]))
1184                 return 0;
1185         if (isl_int_is_neg(tab->mat->row[row][2]))
1186                 return 1;
1187         return isl_int_is_neg(tab->mat->row[row][1]);
1188 }
1189
1190 static int row_sgn(struct isl_tab *tab, int row)
1191 {
1192         if (!tab->M)
1193                 return isl_int_sgn(tab->mat->row[row][1]);
1194         if (!isl_int_is_zero(tab->mat->row[row][2]))
1195                 return isl_int_sgn(tab->mat->row[row][2]);
1196         else
1197                 return isl_int_sgn(tab->mat->row[row][1]);
1198 }
1199
1200 /* Perform pivots until the row variable "var" has a non-negative
1201  * sample value or until no more upward pivots can be performed.
1202  * Return the sign of the sample value after the pivots have been
1203  * performed.
1204  */
1205 static int restore_row(struct isl_tab *tab, struct isl_tab_var *var)
1206 {
1207         int row, col;
1208
1209         while (row_is_neg(tab, var->index)) {
1210                 find_pivot(tab, var, var, 1, &row, &col);
1211                 if (row == -1)
1212                         break;
1213                 if (isl_tab_pivot(tab, row, col) < 0)
1214                         return -2;
1215                 if (!var->is_row) /* manifestly unbounded */
1216                         return 1;
1217         }
1218         return row_sgn(tab, var->index);
1219 }
1220
1221 /* Perform pivots until we are sure that the row variable "var"
1222  * can attain non-negative values.  After return from this
1223  * function, "var" is still a row variable, but its sample
1224  * value may not be non-negative, even if the function returns 1.
1225  */
1226 static int at_least_zero(struct isl_tab *tab, struct isl_tab_var *var)
1227 {
1228         int row, col;
1229
1230         while (isl_int_is_neg(tab->mat->row[var->index][1])) {
1231                 find_pivot(tab, var, var, 1, &row, &col);
1232                 if (row == -1)
1233                         break;
1234                 if (row == var->index) /* manifestly unbounded */
1235                         return 1;
1236                 if (isl_tab_pivot(tab, row, col) < 0)
1237                         return -1;
1238         }
1239         return !isl_int_is_neg(tab->mat->row[var->index][1]);
1240 }
1241
1242 /* Return a negative value if "var" can attain negative values.
1243  * Return a non-negative value otherwise.
1244  *
1245  * If "var" is manifestly unbounded wrt negative values, we are done.
1246  * Otherwise, if var is in a column, we can pivot it down to a row.
1247  * Then we continue pivoting down until either
1248  *      - the pivot would result in a manifestly unbounded column
1249  *        => we don't perform the pivot, but simply return -1
1250  *      - no more down pivots can be performed
1251  *      - the sample value is negative
1252  * If the sample value becomes negative and the variable is supposed
1253  * to be nonnegative, then we undo the last pivot.
1254  * However, if the last pivot has made the pivoting variable
1255  * obviously redundant, then it may have moved to another row.
1256  * In that case we look for upward pivots until we reach a non-negative
1257  * value again.
1258  */
1259 static int sign_of_min(struct isl_tab *tab, struct isl_tab_var *var)
1260 {
1261         int row, col;
1262         struct isl_tab_var *pivot_var = NULL;
1263
1264         if (min_is_manifestly_unbounded(tab, var))
1265                 return -1;
1266         if (!var->is_row) {
1267                 col = var->index;
1268                 row = pivot_row(tab, NULL, -1, col);
1269                 pivot_var = var_from_col(tab, col);
1270                 if (isl_tab_pivot(tab, row, col) < 0)
1271                         return -2;
1272                 if (var->is_redundant)
1273                         return 0;
1274                 if (isl_int_is_neg(tab->mat->row[var->index][1])) {
1275                         if (var->is_nonneg) {
1276                                 if (!pivot_var->is_redundant &&
1277                                     pivot_var->index == row) {
1278                                         if (isl_tab_pivot(tab, row, col) < 0)
1279                                                 return -2;
1280                                 } else
1281                                         if (restore_row(tab, var) < -1)
1282                                                 return -2;
1283                         }
1284                         return -1;
1285                 }
1286         }
1287         if (var->is_redundant)
1288                 return 0;
1289         while (!isl_int_is_neg(tab->mat->row[var->index][1])) {
1290                 find_pivot(tab, var, var, -1, &row, &col);
1291                 if (row == var->index)
1292                         return -1;
1293                 if (row == -1)
1294                         return isl_int_sgn(tab->mat->row[var->index][1]);
1295                 pivot_var = var_from_col(tab, col);
1296                 if (isl_tab_pivot(tab, row, col) < 0)
1297                         return -2;
1298                 if (var->is_redundant)
1299                         return 0;
1300         }
1301         if (pivot_var && var->is_nonneg) {
1302                 /* pivot back to non-negative value */
1303                 if (!pivot_var->is_redundant && pivot_var->index == row) {
1304                         if (isl_tab_pivot(tab, row, col) < 0)
1305                                 return -2;
1306                 } else
1307                         if (restore_row(tab, var) < -1)
1308                                 return -2;
1309         }
1310         return -1;
1311 }
1312
1313 static int row_at_most_neg_one(struct isl_tab *tab, int row)
1314 {
1315         if (tab->M) {
1316                 if (isl_int_is_pos(tab->mat->row[row][2]))
1317                         return 0;
1318                 if (isl_int_is_neg(tab->mat->row[row][2]))
1319                         return 1;
1320         }
1321         return isl_int_is_neg(tab->mat->row[row][1]) &&
1322                isl_int_abs_ge(tab->mat->row[row][1],
1323                               tab->mat->row[row][0]);
1324 }
1325
1326 /* Return 1 if "var" can attain values <= -1.
1327  * Return 0 otherwise.
1328  *
1329  * The sample value of "var" is assumed to be non-negative when the
1330  * the function is called and will be made non-negative again before
1331  * the function returns.
1332  */
1333 int isl_tab_min_at_most_neg_one(struct isl_tab *tab, struct isl_tab_var *var)
1334 {
1335         int row, col;
1336         struct isl_tab_var *pivot_var;
1337
1338         if (min_is_manifestly_unbounded(tab, var))
1339                 return 1;
1340         if (!var->is_row) {
1341                 col = var->index;
1342                 row = pivot_row(tab, NULL, -1, col);
1343                 pivot_var = var_from_col(tab, col);
1344                 if (isl_tab_pivot(tab, row, col) < 0)
1345                         return -1;
1346                 if (var->is_redundant)
1347                         return 0;
1348                 if (row_at_most_neg_one(tab, var->index)) {
1349                         if (var->is_nonneg) {
1350                                 if (!pivot_var->is_redundant &&
1351                                     pivot_var->index == row) {
1352                                         if (isl_tab_pivot(tab, row, col) < 0)
1353                                                 return -1;
1354                                 } else
1355                                         if (restore_row(tab, var) < -1)
1356                                                 return -1;
1357                         }
1358                         return 1;
1359                 }
1360         }
1361         if (var->is_redundant)
1362                 return 0;
1363         do {
1364                 find_pivot(tab, var, var, -1, &row, &col);
1365                 if (row == var->index)
1366                         return 1;
1367                 if (row == -1)
1368                         return 0;
1369                 pivot_var = var_from_col(tab, col);
1370                 if (isl_tab_pivot(tab, row, col) < 0)
1371                         return -1;
1372                 if (var->is_redundant)
1373                         return 0;
1374         } while (!row_at_most_neg_one(tab, var->index));
1375         if (var->is_nonneg) {
1376                 /* pivot back to non-negative value */
1377                 if (!pivot_var->is_redundant && pivot_var->index == row)
1378                         if (isl_tab_pivot(tab, row, col) < 0)
1379                                 return -1;
1380                 if (restore_row(tab, var) < -1)
1381                         return -1;
1382         }
1383         return 1;
1384 }
1385
1386 /* Return 1 if "var" can attain values >= 1.
1387  * Return 0 otherwise.
1388  */
1389 static int at_least_one(struct isl_tab *tab, struct isl_tab_var *var)
1390 {
1391         int row, col;
1392         isl_int *r;
1393
1394         if (max_is_manifestly_unbounded(tab, var))
1395                 return 1;
1396         if (to_row(tab, var, 1) < 0)
1397                 return -1;
1398         r = tab->mat->row[var->index];
1399         while (isl_int_lt(r[1], r[0])) {
1400                 find_pivot(tab, var, var, 1, &row, &col);
1401                 if (row == -1)
1402                         return isl_int_ge(r[1], r[0]);
1403                 if (row == var->index) /* manifestly unbounded */
1404                         return 1;
1405                 if (isl_tab_pivot(tab, row, col) < 0)
1406                         return -1;
1407         }
1408         return 1;
1409 }
1410
1411 static void swap_cols(struct isl_tab *tab, int col1, int col2)
1412 {
1413         int t;
1414         unsigned off = 2 + tab->M;
1415         t = tab->col_var[col1];
1416         tab->col_var[col1] = tab->col_var[col2];
1417         tab->col_var[col2] = t;
1418         var_from_col(tab, col1)->index = col1;
1419         var_from_col(tab, col2)->index = col2;
1420         tab->mat = isl_mat_swap_cols(tab->mat, off + col1, off + col2);
1421 }
1422
1423 /* Mark column with index "col" as representing a zero variable.
1424  * If we may need to undo the operation the column is kept,
1425  * but no longer considered.
1426  * Otherwise, the column is simply removed.
1427  *
1428  * The column may be interchanged with some other column.  If it
1429  * is interchanged with a later column, return 1.  Otherwise return 0.
1430  * If the columns are checked in order in the calling function,
1431  * then a return value of 1 means that the column with the given
1432  * column number may now contain a different column that
1433  * hasn't been checked yet.
1434  */
1435 int isl_tab_kill_col(struct isl_tab *tab, int col)
1436 {
1437         var_from_col(tab, col)->is_zero = 1;
1438         if (tab->need_undo) {
1439                 if (isl_tab_push_var(tab, isl_tab_undo_zero,
1440                                             var_from_col(tab, col)) < 0)
1441                         return -1;
1442                 if (col != tab->n_dead)
1443                         swap_cols(tab, col, tab->n_dead);
1444                 tab->n_dead++;
1445                 return 0;
1446         } else {
1447                 if (col != tab->n_col - 1)
1448                         swap_cols(tab, col, tab->n_col - 1);
1449                 var_from_col(tab, tab->n_col - 1)->index = -1;
1450                 tab->n_col--;
1451                 return 1;
1452         }
1453 }
1454
1455 /* Row variable "var" is non-negative and cannot attain any values
1456  * larger than zero.  This means that the coefficients of the unrestricted
1457  * column variables are zero and that the coefficients of the non-negative
1458  * column variables are zero or negative.
1459  * Each of the non-negative variables with a negative coefficient can
1460  * then also be written as the negative sum of non-negative variables
1461  * and must therefore also be zero.
1462  */
1463 static int close_row(struct isl_tab *tab, struct isl_tab_var *var) WARN_UNUSED;
1464 static int close_row(struct isl_tab *tab, struct isl_tab_var *var)
1465 {
1466         int j;
1467         struct isl_mat *mat = tab->mat;
1468         unsigned off = 2 + tab->M;
1469
1470         isl_assert(tab->mat->ctx, var->is_nonneg, return -1);
1471         var->is_zero = 1;
1472         if (tab->need_undo)
1473                 if (isl_tab_push_var(tab, isl_tab_undo_zero, var) < 0)
1474                         return -1;
1475         for (j = tab->n_dead; j < tab->n_col; ++j) {
1476                 if (isl_int_is_zero(mat->row[var->index][off + j]))
1477                         continue;
1478                 isl_assert(tab->mat->ctx,
1479                     isl_int_is_neg(mat->row[var->index][off + j]), return -1);
1480                 if (isl_tab_kill_col(tab, j))
1481                         --j;
1482         }
1483         if (isl_tab_mark_redundant(tab, var->index) < 0)
1484                 return -1;
1485         return 0;
1486 }
1487
1488 /* Add a constraint to the tableau and allocate a row for it.
1489  * Return the index into the constraint array "con".
1490  */
1491 int isl_tab_allocate_con(struct isl_tab *tab)
1492 {
1493         int r;
1494
1495         isl_assert(tab->mat->ctx, tab->n_row < tab->mat->n_row, return -1);
1496         isl_assert(tab->mat->ctx, tab->n_con < tab->max_con, return -1);
1497
1498         r = tab->n_con;
1499         tab->con[r].index = tab->n_row;
1500         tab->con[r].is_row = 1;
1501         tab->con[r].is_nonneg = 0;
1502         tab->con[r].is_zero = 0;
1503         tab->con[r].is_redundant = 0;
1504         tab->con[r].frozen = 0;
1505         tab->con[r].negated = 0;
1506         tab->row_var[tab->n_row] = ~r;
1507
1508         tab->n_row++;
1509         tab->n_con++;
1510         if (isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->con[r]) < 0)
1511                 return -1;
1512
1513         return r;
1514 }
1515
1516 /* Add a variable to the tableau and allocate a column for it.
1517  * Return the index into the variable array "var".
1518  */
1519 int isl_tab_allocate_var(struct isl_tab *tab)
1520 {
1521         int r;
1522         int i;
1523         unsigned off = 2 + tab->M;
1524
1525         isl_assert(tab->mat->ctx, tab->n_col < tab->mat->n_col, return -1);
1526         isl_assert(tab->mat->ctx, tab->n_var < tab->max_var, return -1);
1527
1528         r = tab->n_var;
1529         tab->var[r].index = tab->n_col;
1530         tab->var[r].is_row = 0;
1531         tab->var[r].is_nonneg = 0;
1532         tab->var[r].is_zero = 0;
1533         tab->var[r].is_redundant = 0;
1534         tab->var[r].frozen = 0;
1535         tab->var[r].negated = 0;
1536         tab->col_var[tab->n_col] = r;
1537
1538         for (i = 0; i < tab->n_row; ++i)
1539                 isl_int_set_si(tab->mat->row[i][off + tab->n_col], 0);
1540
1541         tab->n_var++;
1542         tab->n_col++;
1543         if (isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->var[r]) < 0)
1544                 return -1;
1545
1546         return r;
1547 }
1548
1549 /* Add a row to the tableau.  The row is given as an affine combination
1550  * of the original variables and needs to be expressed in terms of the
1551  * column variables.
1552  *
1553  * We add each term in turn.
1554  * If r = n/d_r is the current sum and we need to add k x, then
1555  *      if x is a column variable, we increase the numerator of
1556  *              this column by k d_r
1557  *      if x = f/d_x is a row variable, then the new representation of r is
1558  *
1559  *               n    k f   d_x/g n + d_r/g k f   m/d_r n + m/d_g k f
1560  *              --- + --- = ------------------- = -------------------
1561  *              d_r   d_r        d_r d_x/g                m
1562  *
1563  *      with g the gcd of d_r and d_x and m the lcm of d_r and d_x.
1564  */
1565 int isl_tab_add_row(struct isl_tab *tab, isl_int *line)
1566 {
1567         int i;
1568         int r;
1569         isl_int *row;
1570         isl_int a, b;
1571         unsigned off = 2 + tab->M;
1572
1573         r = isl_tab_allocate_con(tab);
1574         if (r < 0)
1575                 return -1;
1576
1577         isl_int_init(a);
1578         isl_int_init(b);
1579         row = tab->mat->row[tab->con[r].index];
1580         isl_int_set_si(row[0], 1);
1581         isl_int_set(row[1], line[0]);
1582         isl_seq_clr(row + 2, tab->M + tab->n_col);
1583         for (i = 0; i < tab->n_var; ++i) {
1584                 if (tab->var[i].is_zero)
1585                         continue;
1586                 if (tab->var[i].is_row) {
1587                         isl_int_lcm(a,
1588                                 row[0], tab->mat->row[tab->var[i].index][0]);
1589                         isl_int_swap(a, row[0]);
1590                         isl_int_divexact(a, row[0], a);
1591                         isl_int_divexact(b,
1592                                 row[0], tab->mat->row[tab->var[i].index][0]);
1593                         isl_int_mul(b, b, line[1 + i]);
1594                         isl_seq_combine(row + 1, a, row + 1,
1595                             b, tab->mat->row[tab->var[i].index] + 1,
1596                             1 + tab->M + tab->n_col);
1597                 } else
1598                         isl_int_addmul(row[off + tab->var[i].index],
1599                                                         line[1 + i], row[0]);
1600                 if (tab->M && i >= tab->n_param && i < tab->n_var - tab->n_div)
1601                         isl_int_submul(row[2], line[1 + i], row[0]);
1602         }
1603         isl_seq_normalize(tab->mat->ctx, row, off + tab->n_col);
1604         isl_int_clear(a);
1605         isl_int_clear(b);
1606
1607         if (tab->row_sign)
1608                 tab->row_sign[tab->con[r].index] = 0;
1609
1610         return r;
1611 }
1612
1613 static int drop_row(struct isl_tab *tab, int row)
1614 {
1615         isl_assert(tab->mat->ctx, ~tab->row_var[row] == tab->n_con - 1, return -1);
1616         if (row != tab->n_row - 1)
1617                 swap_rows(tab, row, tab->n_row - 1);
1618         tab->n_row--;
1619         tab->n_con--;
1620         return 0;
1621 }
1622
1623 static int drop_col(struct isl_tab *tab, int col)
1624 {
1625         isl_assert(tab->mat->ctx, tab->col_var[col] == tab->n_var - 1, return -1);
1626         if (col != tab->n_col - 1)
1627                 swap_cols(tab, col, tab->n_col - 1);
1628         tab->n_col--;
1629         tab->n_var--;
1630         return 0;
1631 }
1632
1633 /* Add inequality "ineq" and check if it conflicts with the
1634  * previously added constraints or if it is obviously redundant.
1635  */
1636 struct isl_tab *isl_tab_add_ineq(struct isl_tab *tab, isl_int *ineq)
1637 {
1638         int r;
1639         int sgn;
1640         isl_int cst;
1641
1642         if (!tab)
1643                 return NULL;
1644         if (tab->bset) {
1645                 struct isl_basic_set *bset = tab->bset;
1646
1647                 isl_assert(tab->mat->ctx, tab->n_eq == bset->n_eq, goto error);
1648                 isl_assert(tab->mat->ctx,
1649                             tab->n_con == bset->n_eq + bset->n_ineq, goto error);
1650                 tab->bset = isl_basic_set_add_ineq(tab->bset, ineq);
1651                 if (isl_tab_push(tab, isl_tab_undo_bset_ineq) < 0)
1652                         goto error;
1653                 if (!tab->bset)
1654                         goto error;
1655         }
1656         if (tab->cone) {
1657                 isl_int_init(cst);
1658                 isl_int_swap(ineq[0], cst);
1659         }
1660         r = isl_tab_add_row(tab, ineq);
1661         if (tab->cone) {
1662                 isl_int_swap(ineq[0], cst);
1663                 isl_int_clear(cst);
1664         }
1665         if (r < 0)
1666                 goto error;
1667         tab->con[r].is_nonneg = 1;
1668         if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1669                 goto error;
1670         if (isl_tab_row_is_redundant(tab, tab->con[r].index)) {
1671                 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1672                         goto error;
1673                 return tab;
1674         }
1675
1676         sgn = restore_row(tab, &tab->con[r]);
1677         if (sgn < -1)
1678                 goto error;
1679         if (sgn < 0)
1680                 return isl_tab_mark_empty(tab);
1681         if (tab->con[r].is_row && isl_tab_row_is_redundant(tab, tab->con[r].index))
1682                 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1683                         goto error;
1684         return tab;
1685 error:
1686         isl_tab_free(tab);
1687         return NULL;
1688 }
1689
1690 /* Pivot a non-negative variable down until it reaches the value zero
1691  * and then pivot the variable into a column position.
1692  */
1693 static int to_col(struct isl_tab *tab, struct isl_tab_var *var) WARN_UNUSED;
1694 static int to_col(struct isl_tab *tab, struct isl_tab_var *var)
1695 {
1696         int i;
1697         int row, col;
1698         unsigned off = 2 + tab->M;
1699
1700         if (!var->is_row)
1701                 return 0;
1702
1703         while (isl_int_is_pos(tab->mat->row[var->index][1])) {
1704                 find_pivot(tab, var, NULL, -1, &row, &col);
1705                 isl_assert(tab->mat->ctx, row != -1, return -1);
1706                 if (isl_tab_pivot(tab, row, col) < 0)
1707                         return -1;
1708                 if (!var->is_row)
1709                         return 0;
1710         }
1711
1712         for (i = tab->n_dead; i < tab->n_col; ++i)
1713                 if (!isl_int_is_zero(tab->mat->row[var->index][off + i]))
1714                         break;
1715
1716         isl_assert(tab->mat->ctx, i < tab->n_col, return -1);
1717         if (isl_tab_pivot(tab, var->index, i) < 0)
1718                 return -1;
1719
1720         return 0;
1721 }
1722
1723 /* We assume Gaussian elimination has been performed on the equalities.
1724  * The equalities can therefore never conflict.
1725  * Adding the equalities is currently only really useful for a later call
1726  * to isl_tab_ineq_type.
1727  */
1728 static struct isl_tab *add_eq(struct isl_tab *tab, isl_int *eq)
1729 {
1730         int i;
1731         int r;
1732
1733         if (!tab)
1734                 return NULL;
1735         r = isl_tab_add_row(tab, eq);
1736         if (r < 0)
1737                 goto error;
1738
1739         r = tab->con[r].index;
1740         i = isl_seq_first_non_zero(tab->mat->row[r] + 2 + tab->M + tab->n_dead,
1741                                         tab->n_col - tab->n_dead);
1742         isl_assert(tab->mat->ctx, i >= 0, goto error);
1743         i += tab->n_dead;
1744         if (isl_tab_pivot(tab, r, i) < 0)
1745                 goto error;
1746         if (isl_tab_kill_col(tab, i) < 0)
1747                 goto error;
1748         tab->n_eq++;
1749
1750         return tab;
1751 error:
1752         isl_tab_free(tab);
1753         return NULL;
1754 }
1755
1756 static int row_is_manifestly_zero(struct isl_tab *tab, int row)
1757 {
1758         unsigned off = 2 + tab->M;
1759
1760         if (!isl_int_is_zero(tab->mat->row[row][1]))
1761                 return 0;
1762         if (tab->M && !isl_int_is_zero(tab->mat->row[row][2]))
1763                 return 0;
1764         return isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
1765                                         tab->n_col - tab->n_dead) == -1;
1766 }
1767
1768 /* Add an equality that is known to be valid for the given tableau.
1769  */
1770 struct isl_tab *isl_tab_add_valid_eq(struct isl_tab *tab, isl_int *eq)
1771 {
1772         struct isl_tab_var *var;
1773         int r;
1774
1775         if (!tab)
1776                 return NULL;
1777         r = isl_tab_add_row(tab, eq);
1778         if (r < 0)
1779                 goto error;
1780
1781         var = &tab->con[r];
1782         r = var->index;
1783         if (row_is_manifestly_zero(tab, r)) {
1784                 var->is_zero = 1;
1785                 if (isl_tab_mark_redundant(tab, r) < 0)
1786                         goto error;
1787                 return tab;
1788         }
1789
1790         if (isl_int_is_neg(tab->mat->row[r][1])) {
1791                 isl_seq_neg(tab->mat->row[r] + 1, tab->mat->row[r] + 1,
1792                             1 + tab->n_col);
1793                 var->negated = 1;
1794         }
1795         var->is_nonneg = 1;
1796         if (to_col(tab, var) < 0)
1797                 goto error;
1798         var->is_nonneg = 0;
1799         if (isl_tab_kill_col(tab, var->index) < 0)
1800                 goto error;
1801
1802         return tab;
1803 error:
1804         isl_tab_free(tab);
1805         return NULL;
1806 }
1807
1808 static int add_zero_row(struct isl_tab *tab)
1809 {
1810         int r;
1811         isl_int *row;
1812
1813         r = isl_tab_allocate_con(tab);
1814         if (r < 0)
1815                 return -1;
1816
1817         row = tab->mat->row[tab->con[r].index];
1818         isl_seq_clr(row + 1, 1 + tab->M + tab->n_col);
1819         isl_int_set_si(row[0], 1);
1820
1821         return r;
1822 }
1823
1824 /* Add equality "eq" and check if it conflicts with the
1825  * previously added constraints or if it is obviously redundant.
1826  */
1827 struct isl_tab *isl_tab_add_eq(struct isl_tab *tab, isl_int *eq)
1828 {
1829         struct isl_tab_undo *snap = NULL;
1830         struct isl_tab_var *var;
1831         int r;
1832         int row;
1833         int sgn;
1834         isl_int cst;
1835
1836         if (!tab)
1837                 return NULL;
1838         isl_assert(tab->mat->ctx, !tab->M, goto error);
1839
1840         if (tab->need_undo)
1841                 snap = isl_tab_snap(tab);
1842
1843         if (tab->cone) {
1844                 isl_int_init(cst);
1845                 isl_int_swap(eq[0], cst);
1846         }
1847         r = isl_tab_add_row(tab, eq);
1848         if (tab->cone) {
1849                 isl_int_swap(eq[0], cst);
1850                 isl_int_clear(cst);
1851         }
1852         if (r < 0)
1853                 goto error;
1854
1855         var = &tab->con[r];
1856         row = var->index;
1857         if (row_is_manifestly_zero(tab, row)) {
1858                 if (snap) {
1859                         if (isl_tab_rollback(tab, snap) < 0)
1860                                 goto error;
1861                 } else
1862                         drop_row(tab, row);
1863                 return tab;
1864         }
1865
1866         if (tab->bset) {
1867                 tab->bset = isl_basic_set_add_ineq(tab->bset, eq);
1868                 if (isl_tab_push(tab, isl_tab_undo_bset_ineq) < 0)
1869                         goto error;
1870                 isl_seq_neg(eq, eq, 1 + tab->n_var);
1871                 tab->bset = isl_basic_set_add_ineq(tab->bset, eq);
1872                 isl_seq_neg(eq, eq, 1 + tab->n_var);
1873                 if (isl_tab_push(tab, isl_tab_undo_bset_ineq) < 0)
1874                         goto error;
1875                 if (!tab->bset)
1876                         goto error;
1877                 if (add_zero_row(tab) < 0)
1878                         goto error;
1879         }
1880
1881         sgn = isl_int_sgn(tab->mat->row[row][1]);
1882
1883         if (sgn > 0) {
1884                 isl_seq_neg(tab->mat->row[row] + 1, tab->mat->row[row] + 1,
1885                             1 + tab->n_col);
1886                 var->negated = 1;
1887                 sgn = -1;
1888         }
1889
1890         if (sgn < 0) {
1891                 sgn = sign_of_max(tab, var);
1892                 if (sgn < -1)
1893                         goto error;
1894                 if (sgn < 0)
1895                         return isl_tab_mark_empty(tab);
1896         }
1897
1898         var->is_nonneg = 1;
1899         if (to_col(tab, var) < 0)
1900                 goto error;
1901         var->is_nonneg = 0;
1902         if (isl_tab_kill_col(tab, var->index) < 0)
1903                 goto error;
1904
1905         return tab;
1906 error:
1907         isl_tab_free(tab);
1908         return NULL;
1909 }
1910
1911 struct isl_tab *isl_tab_from_basic_map(struct isl_basic_map *bmap)
1912 {
1913         int i;
1914         struct isl_tab *tab;
1915
1916         if (!bmap)
1917                 return NULL;
1918         tab = isl_tab_alloc(bmap->ctx,
1919                             isl_basic_map_total_dim(bmap) + bmap->n_ineq + 1,
1920                             isl_basic_map_total_dim(bmap), 0);
1921         if (!tab)
1922                 return NULL;
1923         tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
1924         if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1925                 return isl_tab_mark_empty(tab);
1926         for (i = 0; i < bmap->n_eq; ++i) {
1927                 tab = add_eq(tab, bmap->eq[i]);
1928                 if (!tab)
1929                         return tab;
1930         }
1931         for (i = 0; i < bmap->n_ineq; ++i) {
1932                 tab = isl_tab_add_ineq(tab, bmap->ineq[i]);
1933                 if (!tab || tab->empty)
1934                         return tab;
1935         }
1936         return tab;
1937 }
1938
1939 struct isl_tab *isl_tab_from_basic_set(struct isl_basic_set *bset)
1940 {
1941         return isl_tab_from_basic_map((struct isl_basic_map *)bset);
1942 }
1943
1944 /* Construct a tableau corresponding to the recession cone of "bset".
1945  */
1946 struct isl_tab *isl_tab_from_recession_cone(struct isl_basic_set *bset)
1947 {
1948         isl_int cst;
1949         int i;
1950         struct isl_tab *tab;
1951
1952         if (!bset)
1953                 return NULL;
1954         tab = isl_tab_alloc(bset->ctx, bset->n_eq + bset->n_ineq,
1955                                 isl_basic_set_total_dim(bset), 0);
1956         if (!tab)
1957                 return NULL;
1958         tab->rational = ISL_F_ISSET(bset, ISL_BASIC_SET_RATIONAL);
1959         tab->cone = 1;
1960
1961         isl_int_init(cst);
1962         for (i = 0; i < bset->n_eq; ++i) {
1963                 isl_int_swap(bset->eq[i][0], cst);
1964                 tab = add_eq(tab, bset->eq[i]);
1965                 isl_int_swap(bset->eq[i][0], cst);
1966                 if (!tab)
1967                         goto done;
1968         }
1969         for (i = 0; i < bset->n_ineq; ++i) {
1970                 int r;
1971                 isl_int_swap(bset->ineq[i][0], cst);
1972                 r = isl_tab_add_row(tab, bset->ineq[i]);
1973                 isl_int_swap(bset->ineq[i][0], cst);
1974                 if (r < 0)
1975                         goto error;
1976                 tab->con[r].is_nonneg = 1;
1977                 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1978                         goto error;
1979         }
1980 done:
1981         isl_int_clear(cst);
1982         return tab;
1983 error:
1984         isl_int_clear(cst);
1985         isl_tab_free(tab);
1986         return NULL;
1987 }
1988
1989 /* Assuming "tab" is the tableau of a cone, check if the cone is
1990  * bounded, i.e., if it is empty or only contains the origin.
1991  */
1992 int isl_tab_cone_is_bounded(struct isl_tab *tab)
1993 {
1994         int i;
1995
1996         if (!tab)
1997                 return -1;
1998         if (tab->empty)
1999                 return 1;
2000         if (tab->n_dead == tab->n_col)
2001                 return 1;
2002
2003         for (;;) {
2004                 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2005                         struct isl_tab_var *var;
2006                         int sgn;
2007                         var = isl_tab_var_from_row(tab, i);
2008                         if (!var->is_nonneg)
2009                                 continue;
2010                         sgn = sign_of_max(tab, var);
2011                         if (sgn < -1)
2012                                 return -1;
2013                         if (sgn != 0)
2014                                 return 0;
2015                         if (close_row(tab, var) < 0)
2016                                 return -1;
2017                         break;
2018                 }
2019                 if (tab->n_dead == tab->n_col)
2020                         return 1;
2021                 if (i == tab->n_row)
2022                         return 0;
2023         }
2024 }
2025
2026 int isl_tab_sample_is_integer(struct isl_tab *tab)
2027 {
2028         int i;
2029
2030         if (!tab)
2031                 return -1;
2032
2033         for (i = 0; i < tab->n_var; ++i) {
2034                 int row;
2035                 if (!tab->var[i].is_row)
2036                         continue;
2037                 row = tab->var[i].index;
2038                 if (!isl_int_is_divisible_by(tab->mat->row[row][1],
2039                                                 tab->mat->row[row][0]))
2040                         return 0;
2041         }
2042         return 1;
2043 }
2044
2045 static struct isl_vec *extract_integer_sample(struct isl_tab *tab)
2046 {
2047         int i;
2048         struct isl_vec *vec;
2049
2050         vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
2051         if (!vec)
2052                 return NULL;
2053
2054         isl_int_set_si(vec->block.data[0], 1);
2055         for (i = 0; i < tab->n_var; ++i) {
2056                 if (!tab->var[i].is_row)
2057                         isl_int_set_si(vec->block.data[1 + i], 0);
2058                 else {
2059                         int row = tab->var[i].index;
2060                         isl_int_divexact(vec->block.data[1 + i],
2061                                 tab->mat->row[row][1], tab->mat->row[row][0]);
2062                 }
2063         }
2064
2065         return vec;
2066 }
2067
2068 struct isl_vec *isl_tab_get_sample_value(struct isl_tab *tab)
2069 {
2070         int i;
2071         struct isl_vec *vec;
2072         isl_int m;
2073
2074         if (!tab)
2075                 return NULL;
2076
2077         vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
2078         if (!vec)
2079                 return NULL;
2080
2081         isl_int_init(m);
2082
2083         isl_int_set_si(vec->block.data[0], 1);
2084         for (i = 0; i < tab->n_var; ++i) {
2085                 int row;
2086                 if (!tab->var[i].is_row) {
2087                         isl_int_set_si(vec->block.data[1 + i], 0);
2088                         continue;
2089                 }
2090                 row = tab->var[i].index;
2091                 isl_int_gcd(m, vec->block.data[0], tab->mat->row[row][0]);
2092                 isl_int_divexact(m, tab->mat->row[row][0], m);
2093                 isl_seq_scale(vec->block.data, vec->block.data, m, 1 + i);
2094                 isl_int_divexact(m, vec->block.data[0], tab->mat->row[row][0]);
2095                 isl_int_mul(vec->block.data[1 + i], m, tab->mat->row[row][1]);
2096         }
2097         vec = isl_vec_normalize(vec);
2098
2099         isl_int_clear(m);
2100         return vec;
2101 }
2102
2103 /* Update "bmap" based on the results of the tableau "tab".
2104  * In particular, implicit equalities are made explicit, redundant constraints
2105  * are removed and if the sample value happens to be integer, it is stored
2106  * in "bmap" (unless "bmap" already had an integer sample).
2107  *
2108  * The tableau is assumed to have been created from "bmap" using
2109  * isl_tab_from_basic_map.
2110  */
2111 struct isl_basic_map *isl_basic_map_update_from_tab(struct isl_basic_map *bmap,
2112         struct isl_tab *tab)
2113 {
2114         int i;
2115         unsigned n_eq;
2116
2117         if (!bmap)
2118                 return NULL;
2119         if (!tab)
2120                 return bmap;
2121
2122         n_eq = tab->n_eq;
2123         if (tab->empty)
2124                 bmap = isl_basic_map_set_to_empty(bmap);
2125         else
2126                 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2127                         if (isl_tab_is_equality(tab, n_eq + i))
2128                                 isl_basic_map_inequality_to_equality(bmap, i);
2129                         else if (isl_tab_is_redundant(tab, n_eq + i))
2130                                 isl_basic_map_drop_inequality(bmap, i);
2131                 }
2132         if (!tab->rational &&
2133             !bmap->sample && isl_tab_sample_is_integer(tab))
2134                 bmap->sample = extract_integer_sample(tab);
2135         return bmap;
2136 }
2137
2138 struct isl_basic_set *isl_basic_set_update_from_tab(struct isl_basic_set *bset,
2139         struct isl_tab *tab)
2140 {
2141         return (struct isl_basic_set *)isl_basic_map_update_from_tab(
2142                 (struct isl_basic_map *)bset, tab);
2143 }
2144
2145 /* Given a non-negative variable "var", add a new non-negative variable
2146  * that is the opposite of "var", ensuring that var can only attain the
2147  * value zero.
2148  * If var = n/d is a row variable, then the new variable = -n/d.
2149  * If var is a column variables, then the new variable = -var.
2150  * If the new variable cannot attain non-negative values, then
2151  * the resulting tableau is empty.
2152  * Otherwise, we know the value will be zero and we close the row.
2153  */
2154 static struct isl_tab *cut_to_hyperplane(struct isl_tab *tab,
2155         struct isl_tab_var *var)
2156 {
2157         unsigned r;
2158         isl_int *row;
2159         int sgn;
2160         unsigned off = 2 + tab->M;
2161
2162         if (var->is_zero)
2163                 return tab;
2164         isl_assert(tab->mat->ctx, !var->is_redundant, goto error);
2165
2166         if (isl_tab_extend_cons(tab, 1) < 0)
2167                 goto error;
2168
2169         r = tab->n_con;
2170         tab->con[r].index = tab->n_row;
2171         tab->con[r].is_row = 1;
2172         tab->con[r].is_nonneg = 0;
2173         tab->con[r].is_zero = 0;
2174         tab->con[r].is_redundant = 0;
2175         tab->con[r].frozen = 0;
2176         tab->con[r].negated = 0;
2177         tab->row_var[tab->n_row] = ~r;
2178         row = tab->mat->row[tab->n_row];
2179
2180         if (var->is_row) {
2181                 isl_int_set(row[0], tab->mat->row[var->index][0]);
2182                 isl_seq_neg(row + 1,
2183                             tab->mat->row[var->index] + 1, 1 + tab->n_col);
2184         } else {
2185                 isl_int_set_si(row[0], 1);
2186                 isl_seq_clr(row + 1, 1 + tab->n_col);
2187                 isl_int_set_si(row[off + var->index], -1);
2188         }
2189
2190         tab->n_row++;
2191         tab->n_con++;
2192         if (isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->con[r]) < 0)
2193                 goto error;
2194
2195         sgn = sign_of_max(tab, &tab->con[r]);
2196         if (sgn < -1)
2197                 goto error;
2198         if (sgn < 0)
2199                 return isl_tab_mark_empty(tab);
2200         tab->con[r].is_nonneg = 1;
2201         if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
2202                 goto error;
2203         /* sgn == 0 */
2204         if (close_row(tab, &tab->con[r]) < 0)
2205                 goto error;
2206
2207         return tab;
2208 error:
2209         isl_tab_free(tab);
2210         return NULL;
2211 }
2212
2213 /* Given a tableau "tab" and an inequality constraint "con" of the tableau,
2214  * relax the inequality by one.  That is, the inequality r >= 0 is replaced
2215  * by r' = r + 1 >= 0.
2216  * If r is a row variable, we simply increase the constant term by one
2217  * (taking into account the denominator).
2218  * If r is a column variable, then we need to modify each row that
2219  * refers to r = r' - 1 by substituting this equality, effectively
2220  * subtracting the coefficient of the column from the constant.
2221  */
2222 struct isl_tab *isl_tab_relax(struct isl_tab *tab, int con)
2223 {
2224         struct isl_tab_var *var;
2225         unsigned off = 2 + tab->M;
2226
2227         if (!tab)
2228                 return NULL;
2229
2230         var = &tab->con[con];
2231
2232         if (!var->is_row && !max_is_manifestly_unbounded(tab, var))
2233                 if (to_row(tab, var, 1) < 0)
2234                         goto error;
2235
2236         if (var->is_row)
2237                 isl_int_add(tab->mat->row[var->index][1],
2238                     tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
2239         else {
2240                 int i;
2241
2242                 for (i = 0; i < tab->n_row; ++i) {
2243                         if (isl_int_is_zero(tab->mat->row[i][off + var->index]))
2244                                 continue;
2245                         isl_int_sub(tab->mat->row[i][1], tab->mat->row[i][1],
2246                             tab->mat->row[i][off + var->index]);
2247                 }
2248
2249         }
2250
2251         if (isl_tab_push_var(tab, isl_tab_undo_relax, var) < 0)
2252                 goto error;
2253
2254         return tab;
2255 error:
2256         isl_tab_free(tab);
2257         return NULL;
2258 }
2259
2260 struct isl_tab *isl_tab_select_facet(struct isl_tab *tab, int con)
2261 {
2262         if (!tab)
2263                 return NULL;
2264
2265         return cut_to_hyperplane(tab, &tab->con[con]);
2266 }
2267
2268 static int may_be_equality(struct isl_tab *tab, int row)
2269 {
2270         unsigned off = 2 + tab->M;
2271         return (tab->rational ? isl_int_is_zero(tab->mat->row[row][1])
2272                               : isl_int_lt(tab->mat->row[row][1],
2273                                             tab->mat->row[row][0])) &&
2274                 isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
2275                                         tab->n_col - tab->n_dead) != -1;
2276 }
2277
2278 /* Check for (near) equalities among the constraints.
2279  * A constraint is an equality if it is non-negative and if
2280  * its maximal value is either
2281  *      - zero (in case of rational tableaus), or
2282  *      - strictly less than 1 (in case of integer tableaus)
2283  *
2284  * We first mark all non-redundant and non-dead variables that
2285  * are not frozen and not obviously not an equality.
2286  * Then we iterate over all marked variables if they can attain
2287  * any values larger than zero or at least one.
2288  * If the maximal value is zero, we mark any column variables
2289  * that appear in the row as being zero and mark the row as being redundant.
2290  * Otherwise, if the maximal value is strictly less than one (and the
2291  * tableau is integer), then we restrict the value to being zero
2292  * by adding an opposite non-negative variable.
2293  */
2294 struct isl_tab *isl_tab_detect_implicit_equalities(struct isl_tab *tab)
2295 {
2296         int i;
2297         unsigned n_marked;
2298
2299         if (!tab)
2300                 return NULL;
2301         if (tab->empty)
2302                 return tab;
2303         if (tab->n_dead == tab->n_col)
2304                 return tab;
2305
2306         n_marked = 0;
2307         for (i = tab->n_redundant; i < tab->n_row; ++i) {
2308                 struct isl_tab_var *var = isl_tab_var_from_row(tab, i);
2309                 var->marked = !var->frozen && var->is_nonneg &&
2310                         may_be_equality(tab, i);
2311                 if (var->marked)
2312                         n_marked++;
2313         }
2314         for (i = tab->n_dead; i < tab->n_col; ++i) {
2315                 struct isl_tab_var *var = var_from_col(tab, i);
2316                 var->marked = !var->frozen && var->is_nonneg;
2317                 if (var->marked)
2318                         n_marked++;
2319         }
2320         while (n_marked) {
2321                 struct isl_tab_var *var;
2322                 int sgn;
2323                 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2324                         var = isl_tab_var_from_row(tab, i);
2325                         if (var->marked)
2326                                 break;
2327                 }
2328                 if (i == tab->n_row) {
2329                         for (i = tab->n_dead; i < tab->n_col; ++i) {
2330                                 var = var_from_col(tab, i);
2331                                 if (var->marked)
2332                                         break;
2333                         }
2334                         if (i == tab->n_col)
2335                                 break;
2336                 }
2337                 var->marked = 0;
2338                 n_marked--;
2339                 sgn = sign_of_max(tab, var);
2340                 if (sgn < 0)
2341                         goto error;
2342                 if (sgn == 0) {
2343                         if (close_row(tab, var) < 0)
2344                                 goto error;
2345                 } else if (!tab->rational && !at_least_one(tab, var)) {
2346                         tab = cut_to_hyperplane(tab, var);
2347                         return isl_tab_detect_implicit_equalities(tab);
2348                 }
2349                 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2350                         var = isl_tab_var_from_row(tab, i);
2351                         if (!var->marked)
2352                                 continue;
2353                         if (may_be_equality(tab, i))
2354                                 continue;
2355                         var->marked = 0;
2356                         n_marked--;
2357                 }
2358         }
2359
2360         return tab;
2361 error:
2362         isl_tab_free(tab);
2363         return NULL;
2364 }
2365
2366 static int con_is_redundant(struct isl_tab *tab, struct isl_tab_var *var)
2367 {
2368         if (!tab)
2369                 return -1;
2370         if (tab->rational) {
2371                 int sgn = sign_of_min(tab, var);
2372                 if (sgn < -1)
2373                         return -1;
2374                 return sgn >= 0;
2375         } else {
2376                 int irred = isl_tab_min_at_most_neg_one(tab, var);
2377                 if (irred < 0)
2378                         return -1;
2379                 return !irred;
2380         }
2381 }
2382
2383 /* Check for (near) redundant constraints.
2384  * A constraint is redundant if it is non-negative and if
2385  * its minimal value (temporarily ignoring the non-negativity) is either
2386  *      - zero (in case of rational tableaus), or
2387  *      - strictly larger than -1 (in case of integer tableaus)
2388  *
2389  * We first mark all non-redundant and non-dead variables that
2390  * are not frozen and not obviously negatively unbounded.
2391  * Then we iterate over all marked variables if they can attain
2392  * any values smaller than zero or at most negative one.
2393  * If not, we mark the row as being redundant (assuming it hasn't
2394  * been detected as being obviously redundant in the mean time).
2395  */
2396 struct isl_tab *isl_tab_detect_redundant(struct isl_tab *tab)
2397 {
2398         int i;
2399         unsigned n_marked;
2400
2401         if (!tab)
2402                 return NULL;
2403         if (tab->empty)
2404                 return tab;
2405         if (tab->n_redundant == tab->n_row)
2406                 return tab;
2407
2408         n_marked = 0;
2409         for (i = tab->n_redundant; i < tab->n_row; ++i) {
2410                 struct isl_tab_var *var = isl_tab_var_from_row(tab, i);
2411                 var->marked = !var->frozen && var->is_nonneg;
2412                 if (var->marked)
2413                         n_marked++;
2414         }
2415         for (i = tab->n_dead; i < tab->n_col; ++i) {
2416                 struct isl_tab_var *var = var_from_col(tab, i);
2417                 var->marked = !var->frozen && var->is_nonneg &&
2418                         !min_is_manifestly_unbounded(tab, var);
2419                 if (var->marked)
2420                         n_marked++;
2421         }
2422         while (n_marked) {
2423                 struct isl_tab_var *var;
2424                 int red;
2425                 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2426                         var = isl_tab_var_from_row(tab, i);
2427                         if (var->marked)
2428                                 break;
2429                 }
2430                 if (i == tab->n_row) {
2431                         for (i = tab->n_dead; i < tab->n_col; ++i) {
2432                                 var = var_from_col(tab, i);
2433                                 if (var->marked)
2434                                         break;
2435                         }
2436                         if (i == tab->n_col)
2437                                 break;
2438                 }
2439                 var->marked = 0;
2440                 n_marked--;
2441                 red = con_is_redundant(tab, var);
2442                 if (red < 0)
2443                         goto error;
2444                 if (red && !var->is_redundant)
2445                         if (isl_tab_mark_redundant(tab, var->index) < 0)
2446                                 goto error;
2447                 for (i = tab->n_dead; i < tab->n_col; ++i) {
2448                         var = var_from_col(tab, i);
2449                         if (!var->marked)
2450                                 continue;
2451                         if (!min_is_manifestly_unbounded(tab, var))
2452                                 continue;
2453                         var->marked = 0;
2454                         n_marked--;
2455                 }
2456         }
2457
2458         return tab;
2459 error:
2460         isl_tab_free(tab);
2461         return NULL;
2462 }
2463
2464 int isl_tab_is_equality(struct isl_tab *tab, int con)
2465 {
2466         int row;
2467         unsigned off;
2468
2469         if (!tab)
2470                 return -1;
2471         if (tab->con[con].is_zero)
2472                 return 1;
2473         if (tab->con[con].is_redundant)
2474                 return 0;
2475         if (!tab->con[con].is_row)
2476                 return tab->con[con].index < tab->n_dead;
2477
2478         row = tab->con[con].index;
2479
2480         off = 2 + tab->M;
2481         return isl_int_is_zero(tab->mat->row[row][1]) &&
2482                 isl_seq_first_non_zero(tab->mat->row[row] + 2 + tab->n_dead,
2483                                         tab->n_col - tab->n_dead) == -1;
2484 }
2485
2486 /* Return the minimial value of the affine expression "f" with denominator
2487  * "denom" in *opt, *opt_denom, assuming the tableau is not empty and
2488  * the expression cannot attain arbitrarily small values.
2489  * If opt_denom is NULL, then *opt is rounded up to the nearest integer.
2490  * The return value reflects the nature of the result (empty, unbounded,
2491  * minmimal value returned in *opt).
2492  */
2493 enum isl_lp_result isl_tab_min(struct isl_tab *tab,
2494         isl_int *f, isl_int denom, isl_int *opt, isl_int *opt_denom,
2495         unsigned flags)
2496 {
2497         int r;
2498         enum isl_lp_result res = isl_lp_ok;
2499         struct isl_tab_var *var;
2500         struct isl_tab_undo *snap;
2501
2502         if (tab->empty)
2503                 return isl_lp_empty;
2504
2505         snap = isl_tab_snap(tab);
2506         r = isl_tab_add_row(tab, f);
2507         if (r < 0)
2508                 return isl_lp_error;
2509         var = &tab->con[r];
2510         isl_int_mul(tab->mat->row[var->index][0],
2511                     tab->mat->row[var->index][0], denom);
2512         for (;;) {
2513                 int row, col;
2514                 find_pivot(tab, var, var, -1, &row, &col);
2515                 if (row == var->index) {
2516                         res = isl_lp_unbounded;
2517                         break;
2518                 }
2519                 if (row == -1)
2520                         break;
2521                 if (isl_tab_pivot(tab, row, col) < 0)
2522                         return isl_lp_error;
2523         }
2524         if (ISL_FL_ISSET(flags, ISL_TAB_SAVE_DUAL)) {
2525                 int i;
2526
2527                 isl_vec_free(tab->dual);
2528                 tab->dual = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_con);
2529                 if (!tab->dual)
2530                         return isl_lp_error;
2531                 isl_int_set(tab->dual->el[0], tab->mat->row[var->index][0]);
2532                 for (i = 0; i < tab->n_con; ++i) {
2533                         int pos;
2534                         if (tab->con[i].is_row) {
2535                                 isl_int_set_si(tab->dual->el[1 + i], 0);
2536                                 continue;
2537                         }
2538                         pos = 2 + tab->M + tab->con[i].index;
2539                         if (tab->con[i].negated)
2540                                 isl_int_neg(tab->dual->el[1 + i],
2541                                             tab->mat->row[var->index][pos]);
2542                         else
2543                                 isl_int_set(tab->dual->el[1 + i],
2544                                             tab->mat->row[var->index][pos]);
2545                 }
2546         }
2547         if (opt && res == isl_lp_ok) {
2548                 if (opt_denom) {
2549                         isl_int_set(*opt, tab->mat->row[var->index][1]);
2550                         isl_int_set(*opt_denom, tab->mat->row[var->index][0]);
2551                 } else
2552                         isl_int_cdiv_q(*opt, tab->mat->row[var->index][1],
2553                                              tab->mat->row[var->index][0]);
2554         }
2555         if (isl_tab_rollback(tab, snap) < 0)
2556                 return isl_lp_error;
2557         return res;
2558 }
2559
2560 int isl_tab_is_redundant(struct isl_tab *tab, int con)
2561 {
2562         if (!tab)
2563                 return -1;
2564         if (tab->con[con].is_zero)
2565                 return 0;
2566         if (tab->con[con].is_redundant)
2567                 return 1;
2568         return tab->con[con].is_row && tab->con[con].index < tab->n_redundant;
2569 }
2570
2571 /* Take a snapshot of the tableau that can be restored by s call to
2572  * isl_tab_rollback.
2573  */
2574 struct isl_tab_undo *isl_tab_snap(struct isl_tab *tab)
2575 {
2576         if (!tab)
2577                 return NULL;
2578         tab->need_undo = 1;
2579         return tab->top;
2580 }
2581
2582 /* Undo the operation performed by isl_tab_relax.
2583  */
2584 static int unrelax(struct isl_tab *tab, struct isl_tab_var *var) WARN_UNUSED;
2585 static int unrelax(struct isl_tab *tab, struct isl_tab_var *var)
2586 {
2587         unsigned off = 2 + tab->M;
2588
2589         if (!var->is_row && !max_is_manifestly_unbounded(tab, var))
2590                 if (to_row(tab, var, 1) < 0)
2591                         return -1;
2592
2593         if (var->is_row)
2594                 isl_int_sub(tab->mat->row[var->index][1],
2595                     tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
2596         else {
2597                 int i;
2598
2599                 for (i = 0; i < tab->n_row; ++i) {
2600                         if (isl_int_is_zero(tab->mat->row[i][off + var->index]))
2601                                 continue;
2602                         isl_int_add(tab->mat->row[i][1], tab->mat->row[i][1],
2603                             tab->mat->row[i][off + var->index]);
2604                 }
2605
2606         }
2607
2608         return 0;
2609 }
2610
2611 static int perform_undo_var(struct isl_tab *tab, struct isl_tab_undo *undo) WARN_UNUSED;
2612 static int perform_undo_var(struct isl_tab *tab, struct isl_tab_undo *undo)
2613 {
2614         struct isl_tab_var *var = var_from_index(tab, undo->u.var_index);
2615         switch(undo->type) {
2616         case isl_tab_undo_nonneg:
2617                 var->is_nonneg = 0;
2618                 break;
2619         case isl_tab_undo_redundant:
2620                 var->is_redundant = 0;
2621                 tab->n_redundant--;
2622                 break;
2623         case isl_tab_undo_zero:
2624                 var->is_zero = 0;
2625                 if (!var->is_row)
2626                         tab->n_dead--;
2627                 break;
2628         case isl_tab_undo_allocate:
2629                 if (undo->u.var_index >= 0) {
2630                         isl_assert(tab->mat->ctx, !var->is_row, return -1);
2631                         drop_col(tab, var->index);
2632                         break;
2633                 }
2634                 if (!var->is_row) {
2635                         if (!max_is_manifestly_unbounded(tab, var)) {
2636                                 if (to_row(tab, var, 1) < 0)
2637                                         return -1;
2638                         } else if (!min_is_manifestly_unbounded(tab, var)) {
2639                                 if (to_row(tab, var, -1) < 0)
2640                                         return -1;
2641                         } else
2642                                 if (to_row(tab, var, 0) < 0)
2643                                         return -1;
2644                 }
2645                 drop_row(tab, var->index);
2646                 break;
2647         case isl_tab_undo_relax:
2648                 return unrelax(tab, var);
2649         }
2650
2651         return 0;
2652 }
2653
2654 /* Restore the tableau to the state where the basic variables
2655  * are those in "col_var".
2656  * We first construct a list of variables that are currently in
2657  * the basis, but shouldn't.  Then we iterate over all variables
2658  * that should be in the basis and for each one that is currently
2659  * not in the basis, we exchange it with one of the elements of the
2660  * list constructed before.
2661  * We can always find an appropriate variable to pivot with because
2662  * the current basis is mapped to the old basis by a non-singular
2663  * matrix and so we can never end up with a zero row.
2664  */
2665 static int restore_basis(struct isl_tab *tab, int *col_var)
2666 {
2667         int i, j;
2668         int n_extra = 0;
2669         int *extra = NULL;      /* current columns that contain bad stuff */
2670         unsigned off = 2 + tab->M;
2671
2672         extra = isl_alloc_array(tab->mat->ctx, int, tab->n_col);
2673         if (!extra)
2674                 goto error;
2675         for (i = 0; i < tab->n_col; ++i) {
2676                 for (j = 0; j < tab->n_col; ++j)
2677                         if (tab->col_var[i] == col_var[j])
2678                                 break;
2679                 if (j < tab->n_col)
2680                         continue;
2681                 extra[n_extra++] = i;
2682         }
2683         for (i = 0; i < tab->n_col && n_extra > 0; ++i) {
2684                 struct isl_tab_var *var;
2685                 int row;
2686
2687                 for (j = 0; j < tab->n_col; ++j)
2688                         if (col_var[i] == tab->col_var[j])
2689                                 break;
2690                 if (j < tab->n_col)
2691                         continue;
2692                 var = var_from_index(tab, col_var[i]);
2693                 row = var->index;
2694                 for (j = 0; j < n_extra; ++j)
2695                         if (!isl_int_is_zero(tab->mat->row[row][off+extra[j]]))
2696                                 break;
2697                 isl_assert(tab->mat->ctx, j < n_extra, goto error);
2698                 if (isl_tab_pivot(tab, row, extra[j]) < 0)
2699                         goto error;
2700                 extra[j] = extra[--n_extra];
2701         }
2702
2703         free(extra);
2704         free(col_var);
2705         return 0;
2706 error:
2707         free(extra);
2708         free(col_var);
2709         return -1;
2710 }
2711
2712 /* Remove all samples with index n or greater, i.e., those samples
2713  * that were added since we saved this number of samples in
2714  * isl_tab_save_samples.
2715  */
2716 static void drop_samples_since(struct isl_tab *tab, int n)
2717 {
2718         int i;
2719
2720         for (i = tab->n_sample - 1; i >= 0 && tab->n_sample > n; --i) {
2721                 if (tab->sample_index[i] < n)
2722                         continue;
2723
2724                 if (i != tab->n_sample - 1) {
2725                         int t = tab->sample_index[tab->n_sample-1];
2726                         tab->sample_index[tab->n_sample-1] = tab->sample_index[i];
2727                         tab->sample_index[i] = t;
2728                         isl_mat_swap_rows(tab->samples, tab->n_sample-1, i);
2729                 }
2730                 tab->n_sample--;
2731         }
2732 }
2733
2734 static int perform_undo(struct isl_tab *tab, struct isl_tab_undo *undo) WARN_UNUSED;
2735 static int perform_undo(struct isl_tab *tab, struct isl_tab_undo *undo)
2736 {
2737         switch (undo->type) {
2738         case isl_tab_undo_empty:
2739                 tab->empty = 0;
2740                 break;
2741         case isl_tab_undo_nonneg:
2742         case isl_tab_undo_redundant:
2743         case isl_tab_undo_zero:
2744         case isl_tab_undo_allocate:
2745         case isl_tab_undo_relax:
2746                 return perform_undo_var(tab, undo);
2747         case isl_tab_undo_bset_eq:
2748                 return isl_basic_set_free_equality(tab->bset, 1);
2749         case isl_tab_undo_bset_ineq:
2750                 return isl_basic_set_free_inequality(tab->bset, 1);
2751         case isl_tab_undo_bset_div:
2752                 if (isl_basic_set_free_div(tab->bset, 1) < 0)
2753                         return -1;
2754                 if (tab->samples)
2755                         tab->samples->n_col--;
2756                 break;
2757         case isl_tab_undo_saved_basis:
2758                 if (restore_basis(tab, undo->u.col_var) < 0)
2759                         return -1;
2760                 break;
2761         case isl_tab_undo_drop_sample:
2762                 tab->n_outside--;
2763                 break;
2764         case isl_tab_undo_saved_samples:
2765                 drop_samples_since(tab, undo->u.n);
2766                 break;
2767         case isl_tab_undo_callback:
2768                 return undo->u.callback->run(undo->u.callback);
2769         default:
2770                 isl_assert(tab->mat->ctx, 0, return -1);
2771         }
2772         return 0;
2773 }
2774
2775 /* Return the tableau to the state it was in when the snapshot "snap"
2776  * was taken.
2777  */
2778 int isl_tab_rollback(struct isl_tab *tab, struct isl_tab_undo *snap)
2779 {
2780         struct isl_tab_undo *undo, *next;
2781
2782         if (!tab)
2783                 return -1;
2784
2785         tab->in_undo = 1;
2786         for (undo = tab->top; undo && undo != &tab->bottom; undo = next) {
2787                 next = undo->next;
2788                 if (undo == snap)
2789                         break;
2790                 if (perform_undo(tab, undo) < 0) {
2791                         free_undo(tab);
2792                         tab->in_undo = 0;
2793                         return -1;
2794                 }
2795                 free(undo);
2796         }
2797         tab->in_undo = 0;
2798         tab->top = undo;
2799         if (!undo)
2800                 return -1;
2801         return 0;
2802 }
2803
2804 /* The given row "row" represents an inequality violated by all
2805  * points in the tableau.  Check for some special cases of such
2806  * separating constraints.
2807  * In particular, if the row has been reduced to the constant -1,
2808  * then we know the inequality is adjacent (but opposite) to
2809  * an equality in the tableau.
2810  * If the row has been reduced to r = -1 -r', with r' an inequality
2811  * of the tableau, then the inequality is adjacent (but opposite)
2812  * to the inequality r'.
2813  */
2814 static enum isl_ineq_type separation_type(struct isl_tab *tab, unsigned row)
2815 {
2816         int pos;
2817         unsigned off = 2 + tab->M;
2818
2819         if (tab->rational)
2820                 return isl_ineq_separate;
2821
2822         if (!isl_int_is_one(tab->mat->row[row][0]))
2823                 return isl_ineq_separate;
2824         if (!isl_int_is_negone(tab->mat->row[row][1]))
2825                 return isl_ineq_separate;
2826
2827         pos = isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
2828                                         tab->n_col - tab->n_dead);
2829         if (pos == -1)
2830                 return isl_ineq_adj_eq;
2831
2832         if (!isl_int_is_negone(tab->mat->row[row][off + tab->n_dead + pos]))
2833                 return isl_ineq_separate;
2834
2835         pos = isl_seq_first_non_zero(
2836                         tab->mat->row[row] + off + tab->n_dead + pos + 1,
2837                         tab->n_col - tab->n_dead - pos - 1);
2838
2839         return pos == -1 ? isl_ineq_adj_ineq : isl_ineq_separate;
2840 }
2841
2842 /* Check the effect of inequality "ineq" on the tableau "tab".
2843  * The result may be
2844  *      isl_ineq_redundant:     satisfied by all points in the tableau
2845  *      isl_ineq_separate:      satisfied by no point in the tableau
2846  *      isl_ineq_cut:           satisfied by some by not all points
2847  *      isl_ineq_adj_eq:        adjacent to an equality
2848  *      isl_ineq_adj_ineq:      adjacent to an inequality.
2849  */
2850 enum isl_ineq_type isl_tab_ineq_type(struct isl_tab *tab, isl_int *ineq)
2851 {
2852         enum isl_ineq_type type = isl_ineq_error;
2853         struct isl_tab_undo *snap = NULL;
2854         int con;
2855         int row;
2856
2857         if (!tab)
2858                 return isl_ineq_error;
2859
2860         if (isl_tab_extend_cons(tab, 1) < 0)
2861                 return isl_ineq_error;
2862
2863         snap = isl_tab_snap(tab);
2864
2865         con = isl_tab_add_row(tab, ineq);
2866         if (con < 0)
2867                 goto error;
2868
2869         row = tab->con[con].index;
2870         if (isl_tab_row_is_redundant(tab, row))
2871                 type = isl_ineq_redundant;
2872         else if (isl_int_is_neg(tab->mat->row[row][1]) &&
2873                  (tab->rational ||
2874                     isl_int_abs_ge(tab->mat->row[row][1],
2875                                    tab->mat->row[row][0]))) {
2876                 int nonneg = at_least_zero(tab, &tab->con[con]);
2877                 if (nonneg < 0)
2878                         goto error;
2879                 if (nonneg)
2880                         type = isl_ineq_cut;
2881                 else
2882                         type = separation_type(tab, row);
2883         } else {
2884                 int red = con_is_redundant(tab, &tab->con[con]);
2885                 if (red < 0)
2886                         goto error;
2887                 if (!red)
2888                         type = isl_ineq_cut;
2889                 else
2890                         type = isl_ineq_redundant;
2891         }
2892
2893         if (isl_tab_rollback(tab, snap))
2894                 return isl_ineq_error;
2895         return type;
2896 error:
2897         return isl_ineq_error;
2898 }
2899
2900 void isl_tab_dump(struct isl_tab *tab, FILE *out, int indent)
2901 {
2902         unsigned r, c;
2903         int i;
2904
2905         if (!tab) {
2906                 fprintf(out, "%*snull tab\n", indent, "");
2907                 return;
2908         }
2909         fprintf(out, "%*sn_redundant: %d, n_dead: %d", indent, "",
2910                 tab->n_redundant, tab->n_dead);
2911         if (tab->rational)
2912                 fprintf(out, ", rational");
2913         if (tab->empty)
2914                 fprintf(out, ", empty");
2915         fprintf(out, "\n");
2916         fprintf(out, "%*s[", indent, "");
2917         for (i = 0; i < tab->n_var; ++i) {
2918                 if (i)
2919                         fprintf(out, (i == tab->n_param ||
2920                                       i == tab->n_var - tab->n_div) ? "; "
2921                                                                     : ", ");
2922                 fprintf(out, "%c%d%s", tab->var[i].is_row ? 'r' : 'c',
2923                                         tab->var[i].index,
2924                                         tab->var[i].is_zero ? " [=0]" :
2925                                         tab->var[i].is_redundant ? " [R]" : "");
2926         }
2927         fprintf(out, "]\n");
2928         fprintf(out, "%*s[", indent, "");
2929         for (i = 0; i < tab->n_con; ++i) {
2930                 if (i)
2931                         fprintf(out, ", ");
2932                 fprintf(out, "%c%d%s", tab->con[i].is_row ? 'r' : 'c',
2933                                         tab->con[i].index,
2934                                         tab->con[i].is_zero ? " [=0]" :
2935                                         tab->con[i].is_redundant ? " [R]" : "");
2936         }
2937         fprintf(out, "]\n");
2938         fprintf(out, "%*s[", indent, "");
2939         for (i = 0; i < tab->n_row; ++i) {
2940                 const char *sign = "";
2941                 if (i)
2942                         fprintf(out, ", ");
2943                 if (tab->row_sign) {
2944                         if (tab->row_sign[i] == isl_tab_row_unknown)
2945                                 sign = "?";
2946                         else if (tab->row_sign[i] == isl_tab_row_neg)
2947                                 sign = "-";
2948                         else if (tab->row_sign[i] == isl_tab_row_pos)
2949                                 sign = "+";
2950                         else
2951                                 sign = "+-";
2952                 }
2953                 fprintf(out, "r%d: %d%s%s", i, tab->row_var[i],
2954                     isl_tab_var_from_row(tab, i)->is_nonneg ? " [>=0]" : "", sign);
2955         }
2956         fprintf(out, "]\n");
2957         fprintf(out, "%*s[", indent, "");
2958         for (i = 0; i < tab->n_col; ++i) {
2959                 if (i)
2960                         fprintf(out, ", ");
2961                 fprintf(out, "c%d: %d%s", i, tab->col_var[i],
2962                     var_from_col(tab, i)->is_nonneg ? " [>=0]" : "");
2963         }
2964         fprintf(out, "]\n");
2965         r = tab->mat->n_row;
2966         tab->mat->n_row = tab->n_row;
2967         c = tab->mat->n_col;
2968         tab->mat->n_col = 2 + tab->M + tab->n_col;
2969         isl_mat_dump(tab->mat, out, indent);
2970         tab->mat->n_row = r;
2971         tab->mat->n_col = c;
2972         if (tab->bset)
2973                 isl_basic_set_dump(tab->bset, out, indent);
2974 }