b3ebc499d318b4fbb3af1d3d21a42d20e6115e17
[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 variables.
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 (isl_int_is_neg(tab->mat->row[row][off + i]))
724                         return 0;
725                 if (!var_from_col(tab, i)->is_nonneg)
726                         return 0;
727         }
728         return 1;
729 }
730
731 static void swap_rows(struct isl_tab *tab, int row1, int row2)
732 {
733         int t;
734         t = tab->row_var[row1];
735         tab->row_var[row1] = tab->row_var[row2];
736         tab->row_var[row2] = t;
737         isl_tab_var_from_row(tab, row1)->index = row1;
738         isl_tab_var_from_row(tab, row2)->index = row2;
739         tab->mat = isl_mat_swap_rows(tab->mat, row1, row2);
740
741         if (!tab->row_sign)
742                 return;
743         t = tab->row_sign[row1];
744         tab->row_sign[row1] = tab->row_sign[row2];
745         tab->row_sign[row2] = t;
746 }
747
748 static int push_union(struct isl_tab *tab,
749         enum isl_tab_undo_type type, union isl_tab_undo_val u)
750 {
751         struct isl_tab_undo *undo;
752
753         if (!tab->need_undo)
754                 return 0;
755
756         undo = isl_alloc_type(tab->mat->ctx, struct isl_tab_undo);
757         if (!undo)
758                 return -1;
759         undo->type = type;
760         undo->u = u;
761         undo->next = tab->top;
762         tab->top = undo;
763
764         return 0;
765 }
766
767 int isl_tab_push_var(struct isl_tab *tab,
768         enum isl_tab_undo_type type, struct isl_tab_var *var)
769 {
770         union isl_tab_undo_val u;
771         if (var->is_row)
772                 u.var_index = tab->row_var[var->index];
773         else
774                 u.var_index = tab->col_var[var->index];
775         return push_union(tab, type, u);
776 }
777
778 int isl_tab_push(struct isl_tab *tab, enum isl_tab_undo_type type)
779 {
780         union isl_tab_undo_val u = { 0 };
781         return push_union(tab, type, u);
782 }
783
784 /* Push a record on the undo stack describing the current basic
785  * variables, so that the this state can be restored during rollback.
786  */
787 int isl_tab_push_basis(struct isl_tab *tab)
788 {
789         int i;
790         union isl_tab_undo_val u;
791
792         u.col_var = isl_alloc_array(tab->mat->ctx, int, tab->n_col);
793         if (!u.col_var)
794                 return -1;
795         for (i = 0; i < tab->n_col; ++i)
796                 u.col_var[i] = tab->col_var[i];
797         return push_union(tab, isl_tab_undo_saved_basis, u);
798 }
799
800 struct isl_tab *isl_tab_init_samples(struct isl_tab *tab)
801 {
802         if (!tab)
803                 return NULL;
804
805         tab->n_sample = 0;
806         tab->n_outside = 0;
807         tab->samples = isl_mat_alloc(tab->mat->ctx, 1, 1 + tab->n_var);
808         if (!tab->samples)
809                 goto error;
810         tab->sample_index = isl_alloc_array(tab->mat->ctx, int, 1);
811         if (!tab->sample_index)
812                 goto error;
813         return tab;
814 error:
815         isl_tab_free(tab);
816         return NULL;
817 }
818
819 struct isl_tab *isl_tab_add_sample(struct isl_tab *tab,
820         __isl_take isl_vec *sample)
821 {
822         if (!tab || !sample)
823                 goto error;
824
825         if (tab->n_sample + 1 > tab->samples->n_row) {
826                 int *t = isl_realloc_array(tab->mat->ctx,
827                             tab->sample_index, int, tab->n_sample + 1);
828                 if (!t)
829                         goto error;
830                 tab->sample_index = t;
831         }
832
833         tab->samples = isl_mat_extend(tab->samples,
834                                 tab->n_sample + 1, tab->samples->n_col);
835         if (!tab->samples)
836                 goto error;
837
838         isl_seq_cpy(tab->samples->row[tab->n_sample], sample->el, sample->size);
839         isl_vec_free(sample);
840         tab->sample_index[tab->n_sample] = tab->n_sample;
841         tab->n_sample++;
842
843         return tab;
844 error:
845         isl_vec_free(sample);
846         isl_tab_free(tab);
847         return NULL;
848 }
849
850 struct isl_tab *isl_tab_drop_sample(struct isl_tab *tab, int s)
851 {
852         if (s != tab->n_outside) {
853                 int t = tab->sample_index[tab->n_outside];
854                 tab->sample_index[tab->n_outside] = tab->sample_index[s];
855                 tab->sample_index[s] = t;
856                 isl_mat_swap_rows(tab->samples, tab->n_outside, s);
857         }
858         tab->n_outside++;
859         if (isl_tab_push(tab, isl_tab_undo_drop_sample) < 0) {
860                 isl_tab_free(tab);
861                 return NULL;
862         }
863
864         return tab;
865 }
866
867 /* Record the current number of samples so that we can remove newer
868  * samples during a rollback.
869  */
870 int isl_tab_save_samples(struct isl_tab *tab)
871 {
872         union isl_tab_undo_val u;
873
874         if (!tab)
875                 return -1;
876
877         u.n = tab->n_sample;
878         return push_union(tab, isl_tab_undo_saved_samples, u);
879 }
880
881 /* Mark row with index "row" as being redundant.
882  * If we may need to undo the operation or if the row represents
883  * a variable of the original problem, the row is kept,
884  * but no longer considered when looking for a pivot row.
885  * Otherwise, the row is simply removed.
886  *
887  * The row may be interchanged with some other row.  If it
888  * is interchanged with a later row, return 1.  Otherwise return 0.
889  * If the rows are checked in order in the calling function,
890  * then a return value of 1 means that the row with the given
891  * row number may now contain a different row that hasn't been checked yet.
892  */
893 int isl_tab_mark_redundant(struct isl_tab *tab, int row)
894 {
895         struct isl_tab_var *var = isl_tab_var_from_row(tab, row);
896         var->is_redundant = 1;
897         isl_assert(tab->mat->ctx, row >= tab->n_redundant, return -1);
898         if (tab->need_undo || tab->row_var[row] >= 0) {
899                 if (tab->row_var[row] >= 0 && !var->is_nonneg) {
900                         var->is_nonneg = 1;
901                         if (isl_tab_push_var(tab, isl_tab_undo_nonneg, var) < 0)
902                                 return -1;
903                 }
904                 if (row != tab->n_redundant)
905                         swap_rows(tab, row, tab->n_redundant);
906                 tab->n_redundant++;
907                 return isl_tab_push_var(tab, isl_tab_undo_redundant, var);
908         } else {
909                 if (row != tab->n_row - 1)
910                         swap_rows(tab, row, tab->n_row - 1);
911                 isl_tab_var_from_row(tab, tab->n_row - 1)->index = -1;
912                 tab->n_row--;
913                 return 1;
914         }
915 }
916
917 struct isl_tab *isl_tab_mark_empty(struct isl_tab *tab)
918 {
919         if (!tab)
920                 return NULL;
921         if (!tab->empty && tab->need_undo)
922                 if (isl_tab_push(tab, isl_tab_undo_empty) < 0) {
923                         isl_tab_free(tab);
924                         return NULL;
925                 }
926         tab->empty = 1;
927         return tab;
928 }
929
930 /* Update the rows signs after a pivot of "row" and "col", with "row_sgn"
931  * the original sign of the pivot element.
932  * We only keep track of row signs during PILP solving and in this case
933  * we only pivot a row with negative sign (meaning the value is always
934  * non-positive) using a positive pivot element.
935  *
936  * For each row j, the new value of the parametric constant is equal to
937  *
938  *      a_j0 - a_jc a_r0/a_rc
939  *
940  * where a_j0 is the original parametric constant, a_rc is the pivot element,
941  * a_r0 is the parametric constant of the pivot row and a_jc is the
942  * pivot column entry of the row j.
943  * Since a_r0 is non-positive and a_rc is positive, the sign of row j
944  * remains the same if a_jc has the same sign as the row j or if
945  * a_jc is zero.  In all other cases, we reset the sign to "unknown".
946  */
947 static void update_row_sign(struct isl_tab *tab, int row, int col, int row_sgn)
948 {
949         int i;
950         struct isl_mat *mat = tab->mat;
951         unsigned off = 2 + tab->M;
952
953         if (!tab->row_sign)
954                 return;
955
956         if (tab->row_sign[row] == 0)
957                 return;
958         isl_assert(mat->ctx, row_sgn > 0, return);
959         isl_assert(mat->ctx, tab->row_sign[row] == isl_tab_row_neg, return);
960         tab->row_sign[row] = isl_tab_row_pos;
961         for (i = 0; i < tab->n_row; ++i) {
962                 int s;
963                 if (i == row)
964                         continue;
965                 s = isl_int_sgn(mat->row[i][off + col]);
966                 if (!s)
967                         continue;
968                 if (!tab->row_sign[i])
969                         continue;
970                 if (s < 0 && tab->row_sign[i] == isl_tab_row_neg)
971                         continue;
972                 if (s > 0 && tab->row_sign[i] == isl_tab_row_pos)
973                         continue;
974                 tab->row_sign[i] = isl_tab_row_unknown;
975         }
976 }
977
978 /* Given a row number "row" and a column number "col", pivot the tableau
979  * such that the associated variables are interchanged.
980  * The given row in the tableau expresses
981  *
982  *      x_r = a_r0 + \sum_i a_ri x_i
983  *
984  * or
985  *
986  *      x_c = 1/a_rc x_r - a_r0/a_rc + sum_{i \ne r} -a_ri/a_rc
987  *
988  * Substituting this equality into the other rows
989  *
990  *      x_j = a_j0 + \sum_i a_ji x_i
991  *
992  * with a_jc \ne 0, we obtain
993  *
994  *      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 
995  *
996  * The tableau
997  *
998  *      n_rc/d_r                n_ri/d_r
999  *      n_jc/d_j                n_ji/d_j
1000  *
1001  * where i is any other column and j is any other row,
1002  * is therefore transformed into
1003  *
1004  * s(n_rc)d_r/|n_rc|            -s(n_rc)n_ri/|n_rc|
1005  * 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)
1006  *
1007  * The transformation is performed along the following steps
1008  *
1009  *      d_r/n_rc                n_ri/n_rc
1010  *      n_jc/d_j                n_ji/d_j
1011  *
1012  *      s(n_rc)d_r/|n_rc|       -s(n_rc)n_ri/|n_rc|
1013  *      n_jc/d_j                n_ji/d_j
1014  *
1015  *      s(n_rc)d_r/|n_rc|       -s(n_rc)n_ri/|n_rc|
1016  *      n_jc/(|n_rc| d_j)       n_ji/(|n_rc| d_j)
1017  *
1018  *      s(n_rc)d_r/|n_rc|       -s(n_rc)n_ri/|n_rc|
1019  *      n_jc/(|n_rc| d_j)       (n_ji |n_rc|)/(|n_rc| d_j)
1020  *
1021  *      s(n_rc)d_r/|n_rc|       -s(n_rc)n_ri/|n_rc|
1022  *      n_jc/(|n_rc| d_j)       (n_ji |n_rc| - s(n_rc)n_jc n_ri)/(|n_rc| d_j)
1023  *
1024  * s(n_rc)d_r/|n_rc|            -s(n_rc)n_ri/|n_rc|
1025  * 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)
1026  *
1027  */
1028 int isl_tab_pivot(struct isl_tab *tab, int row, int col)
1029 {
1030         int i, j;
1031         int sgn;
1032         int t;
1033         struct isl_mat *mat = tab->mat;
1034         struct isl_tab_var *var;
1035         unsigned off = 2 + tab->M;
1036
1037         isl_int_swap(mat->row[row][0], mat->row[row][off + col]);
1038         sgn = isl_int_sgn(mat->row[row][0]);
1039         if (sgn < 0) {
1040                 isl_int_neg(mat->row[row][0], mat->row[row][0]);
1041                 isl_int_neg(mat->row[row][off + col], mat->row[row][off + col]);
1042         } else
1043                 for (j = 0; j < off - 1 + tab->n_col; ++j) {
1044                         if (j == off - 1 + col)
1045                                 continue;
1046                         isl_int_neg(mat->row[row][1 + j], mat->row[row][1 + j]);
1047                 }
1048         if (!isl_int_is_one(mat->row[row][0]))
1049                 isl_seq_normalize(mat->ctx, mat->row[row], off + tab->n_col);
1050         for (i = 0; i < tab->n_row; ++i) {
1051                 if (i == row)
1052                         continue;
1053                 if (isl_int_is_zero(mat->row[i][off + col]))
1054                         continue;
1055                 isl_int_mul(mat->row[i][0], mat->row[i][0], mat->row[row][0]);
1056                 for (j = 0; j < off - 1 + tab->n_col; ++j) {
1057                         if (j == off - 1 + col)
1058                                 continue;
1059                         isl_int_mul(mat->row[i][1 + j],
1060                                     mat->row[i][1 + j], mat->row[row][0]);
1061                         isl_int_addmul(mat->row[i][1 + j],
1062                                     mat->row[i][off + col], mat->row[row][1 + j]);
1063                 }
1064                 isl_int_mul(mat->row[i][off + col],
1065                             mat->row[i][off + col], mat->row[row][off + col]);
1066                 if (!isl_int_is_one(mat->row[i][0]))
1067                         isl_seq_normalize(mat->ctx, mat->row[i], off + tab->n_col);
1068         }
1069         t = tab->row_var[row];
1070         tab->row_var[row] = tab->col_var[col];
1071         tab->col_var[col] = t;
1072         var = isl_tab_var_from_row(tab, row);
1073         var->is_row = 1;
1074         var->index = row;
1075         var = var_from_col(tab, col);
1076         var->is_row = 0;
1077         var->index = col;
1078         update_row_sign(tab, row, col, sgn);
1079         if (tab->in_undo)
1080                 return 0;
1081         for (i = tab->n_redundant; i < tab->n_row; ++i) {
1082                 if (isl_int_is_zero(mat->row[i][off + col]))
1083                         continue;
1084                 if (!isl_tab_var_from_row(tab, i)->frozen &&
1085                     isl_tab_row_is_redundant(tab, i)) {
1086                         int redo = isl_tab_mark_redundant(tab, i);
1087                         if (redo < 0)
1088                                 return -1;
1089                         if (redo)
1090                                 --i;
1091                 }
1092         }
1093         return 0;
1094 }
1095
1096 /* If "var" represents a column variable, then pivot is up (sgn > 0)
1097  * or down (sgn < 0) to a row.  The variable is assumed not to be
1098  * unbounded in the specified direction.
1099  * If sgn = 0, then the variable is unbounded in both directions,
1100  * and we pivot with any row we can find.
1101  */
1102 static int to_row(struct isl_tab *tab, struct isl_tab_var *var, int sign)
1103 {
1104         int r;
1105         unsigned off = 2 + tab->M;
1106
1107         if (var->is_row)
1108                 return 0;
1109
1110         if (sign == 0) {
1111                 for (r = tab->n_redundant; r < tab->n_row; ++r)
1112                         if (!isl_int_is_zero(tab->mat->row[r][off+var->index]))
1113                                 break;
1114                 isl_assert(tab->mat->ctx, r < tab->n_row, return -1);
1115         } else {
1116                 r = pivot_row(tab, NULL, sign, var->index);
1117                 isl_assert(tab->mat->ctx, r >= 0, return -1);
1118         }
1119
1120         return isl_tab_pivot(tab, r, var->index);
1121 }
1122
1123 static void check_table(struct isl_tab *tab)
1124 {
1125         int i;
1126
1127         if (tab->empty)
1128                 return;
1129         for (i = 0; i < tab->n_row; ++i) {
1130                 if (!isl_tab_var_from_row(tab, i)->is_nonneg)
1131                         continue;
1132                 assert(!isl_int_is_neg(tab->mat->row[i][1]));
1133         }
1134 }
1135
1136 /* Return the sign of the maximal value of "var".
1137  * If the sign is not negative, then on return from this function,
1138  * the sample value will also be non-negative.
1139  *
1140  * If "var" is manifestly unbounded wrt positive values, we are done.
1141  * Otherwise, we pivot the variable up to a row if needed
1142  * Then we continue pivoting down until either
1143  *      - no more down pivots can be performed
1144  *      - the sample value is positive
1145  *      - the variable is pivoted into a manifestly unbounded column
1146  */
1147 static int sign_of_max(struct isl_tab *tab, struct isl_tab_var *var)
1148 {
1149         int row, col;
1150
1151         if (max_is_manifestly_unbounded(tab, var))
1152                 return 1;
1153         if (to_row(tab, var, 1) < 0)
1154                 return -2;
1155         while (!isl_int_is_pos(tab->mat->row[var->index][1])) {
1156                 find_pivot(tab, var, var, 1, &row, &col);
1157                 if (row == -1)
1158                         return isl_int_sgn(tab->mat->row[var->index][1]);
1159                 if (isl_tab_pivot(tab, row, col) < 0)
1160                         return -2;
1161                 if (!var->is_row) /* manifestly unbounded */
1162                         return 1;
1163         }
1164         return 1;
1165 }
1166
1167 static int row_is_neg(struct isl_tab *tab, int row)
1168 {
1169         if (!tab->M)
1170                 return isl_int_is_neg(tab->mat->row[row][1]);
1171         if (isl_int_is_pos(tab->mat->row[row][2]))
1172                 return 0;
1173         if (isl_int_is_neg(tab->mat->row[row][2]))
1174                 return 1;
1175         return isl_int_is_neg(tab->mat->row[row][1]);
1176 }
1177
1178 static int row_sgn(struct isl_tab *tab, int row)
1179 {
1180         if (!tab->M)
1181                 return isl_int_sgn(tab->mat->row[row][1]);
1182         if (!isl_int_is_zero(tab->mat->row[row][2]))
1183                 return isl_int_sgn(tab->mat->row[row][2]);
1184         else
1185                 return isl_int_sgn(tab->mat->row[row][1]);
1186 }
1187
1188 /* Perform pivots until the row variable "var" has a non-negative
1189  * sample value or until no more upward pivots can be performed.
1190  * Return the sign of the sample value after the pivots have been
1191  * performed.
1192  */
1193 static int restore_row(struct isl_tab *tab, struct isl_tab_var *var)
1194 {
1195         int row, col;
1196
1197         while (row_is_neg(tab, var->index)) {
1198                 find_pivot(tab, var, var, 1, &row, &col);
1199                 if (row == -1)
1200                         break;
1201                 if (isl_tab_pivot(tab, row, col) < 0)
1202                         return -2;
1203                 if (!var->is_row) /* manifestly unbounded */
1204                         return 1;
1205         }
1206         return row_sgn(tab, var->index);
1207 }
1208
1209 /* Perform pivots until we are sure that the row variable "var"
1210  * can attain non-negative values.  After return from this
1211  * function, "var" is still a row variable, but its sample
1212  * value may not be non-negative, even if the function returns 1.
1213  */
1214 static int at_least_zero(struct isl_tab *tab, struct isl_tab_var *var)
1215 {
1216         int row, col;
1217
1218         while (isl_int_is_neg(tab->mat->row[var->index][1])) {
1219                 find_pivot(tab, var, var, 1, &row, &col);
1220                 if (row == -1)
1221                         break;
1222                 if (row == var->index) /* manifestly unbounded */
1223                         return 1;
1224                 if (isl_tab_pivot(tab, row, col) < 0)
1225                         return -1;
1226         }
1227         return !isl_int_is_neg(tab->mat->row[var->index][1]);
1228 }
1229
1230 /* Return a negative value if "var" can attain negative values.
1231  * Return a non-negative value otherwise.
1232  *
1233  * If "var" is manifestly unbounded wrt negative values, we are done.
1234  * Otherwise, if var is in a column, we can pivot it down to a row.
1235  * Then we continue pivoting down until either
1236  *      - the pivot would result in a manifestly unbounded column
1237  *        => we don't perform the pivot, but simply return -1
1238  *      - no more down pivots can be performed
1239  *      - the sample value is negative
1240  * If the sample value becomes negative and the variable is supposed
1241  * to be nonnegative, then we undo the last pivot.
1242  * However, if the last pivot has made the pivoting variable
1243  * obviously redundant, then it may have moved to another row.
1244  * In that case we look for upward pivots until we reach a non-negative
1245  * value again.
1246  */
1247 static int sign_of_min(struct isl_tab *tab, struct isl_tab_var *var)
1248 {
1249         int row, col;
1250         struct isl_tab_var *pivot_var = NULL;
1251
1252         if (min_is_manifestly_unbounded(tab, var))
1253                 return -1;
1254         if (!var->is_row) {
1255                 col = var->index;
1256                 row = pivot_row(tab, NULL, -1, col);
1257                 pivot_var = var_from_col(tab, col);
1258                 if (isl_tab_pivot(tab, row, col) < 0)
1259                         return -2;
1260                 if (var->is_redundant)
1261                         return 0;
1262                 if (isl_int_is_neg(tab->mat->row[var->index][1])) {
1263                         if (var->is_nonneg) {
1264                                 if (!pivot_var->is_redundant &&
1265                                     pivot_var->index == row) {
1266                                         if (isl_tab_pivot(tab, row, col) < 0)
1267                                                 return -2;
1268                                 } else
1269                                         if (restore_row(tab, var) < -1)
1270                                                 return -2;
1271                         }
1272                         return -1;
1273                 }
1274         }
1275         if (var->is_redundant)
1276                 return 0;
1277         while (!isl_int_is_neg(tab->mat->row[var->index][1])) {
1278                 find_pivot(tab, var, var, -1, &row, &col);
1279                 if (row == var->index)
1280                         return -1;
1281                 if (row == -1)
1282                         return isl_int_sgn(tab->mat->row[var->index][1]);
1283                 pivot_var = var_from_col(tab, col);
1284                 if (isl_tab_pivot(tab, row, col) < 0)
1285                         return -2;
1286                 if (var->is_redundant)
1287                         return 0;
1288         }
1289         if (pivot_var && var->is_nonneg) {
1290                 /* pivot back to non-negative value */
1291                 if (!pivot_var->is_redundant && pivot_var->index == row) {
1292                         if (isl_tab_pivot(tab, row, col) < 0)
1293                                 return -2;
1294                 } else
1295                         if (restore_row(tab, var) < -1)
1296                                 return -2;
1297         }
1298         return -1;
1299 }
1300
1301 static int row_at_most_neg_one(struct isl_tab *tab, int row)
1302 {
1303         if (tab->M) {
1304                 if (isl_int_is_pos(tab->mat->row[row][2]))
1305                         return 0;
1306                 if (isl_int_is_neg(tab->mat->row[row][2]))
1307                         return 1;
1308         }
1309         return isl_int_is_neg(tab->mat->row[row][1]) &&
1310                isl_int_abs_ge(tab->mat->row[row][1],
1311                               tab->mat->row[row][0]);
1312 }
1313
1314 /* Return 1 if "var" can attain values <= -1.
1315  * Return 0 otherwise.
1316  *
1317  * The sample value of "var" is assumed to be non-negative when the
1318  * the function is called and will be made non-negative again before
1319  * the function returns.
1320  */
1321 int isl_tab_min_at_most_neg_one(struct isl_tab *tab, struct isl_tab_var *var)
1322 {
1323         int row, col;
1324         struct isl_tab_var *pivot_var;
1325
1326         if (min_is_manifestly_unbounded(tab, var))
1327                 return 1;
1328         if (!var->is_row) {
1329                 col = var->index;
1330                 row = pivot_row(tab, NULL, -1, col);
1331                 pivot_var = var_from_col(tab, col);
1332                 if (isl_tab_pivot(tab, row, col) < 0)
1333                         return -1;
1334                 if (var->is_redundant)
1335                         return 0;
1336                 if (row_at_most_neg_one(tab, var->index)) {
1337                         if (var->is_nonneg) {
1338                                 if (!pivot_var->is_redundant &&
1339                                     pivot_var->index == row) {
1340                                         if (isl_tab_pivot(tab, row, col) < 0)
1341                                                 return -1;
1342                                 } else
1343                                         if (restore_row(tab, var) < -1)
1344                                                 return -1;
1345                         }
1346                         return 1;
1347                 }
1348         }
1349         if (var->is_redundant)
1350                 return 0;
1351         do {
1352                 find_pivot(tab, var, var, -1, &row, &col);
1353                 if (row == var->index)
1354                         return 1;
1355                 if (row == -1)
1356                         return 0;
1357                 pivot_var = var_from_col(tab, col);
1358                 if (isl_tab_pivot(tab, row, col) < 0)
1359                         return -1;
1360                 if (var->is_redundant)
1361                         return 0;
1362         } while (!row_at_most_neg_one(tab, var->index));
1363         if (var->is_nonneg) {
1364                 /* pivot back to non-negative value */
1365                 if (!pivot_var->is_redundant && pivot_var->index == row)
1366                         if (isl_tab_pivot(tab, row, col) < 0)
1367                                 return -1;
1368                 if (restore_row(tab, var) < -1)
1369                         return -1;
1370         }
1371         return 1;
1372 }
1373
1374 /* Return 1 if "var" can attain values >= 1.
1375  * Return 0 otherwise.
1376  */
1377 static int at_least_one(struct isl_tab *tab, struct isl_tab_var *var)
1378 {
1379         int row, col;
1380         isl_int *r;
1381
1382         if (max_is_manifestly_unbounded(tab, var))
1383                 return 1;
1384         if (to_row(tab, var, 1) < 0)
1385                 return -1;
1386         r = tab->mat->row[var->index];
1387         while (isl_int_lt(r[1], r[0])) {
1388                 find_pivot(tab, var, var, 1, &row, &col);
1389                 if (row == -1)
1390                         return isl_int_ge(r[1], r[0]);
1391                 if (row == var->index) /* manifestly unbounded */
1392                         return 1;
1393                 if (isl_tab_pivot(tab, row, col) < 0)
1394                         return -1;
1395         }
1396         return 1;
1397 }
1398
1399 static void swap_cols(struct isl_tab *tab, int col1, int col2)
1400 {
1401         int t;
1402         unsigned off = 2 + tab->M;
1403         t = tab->col_var[col1];
1404         tab->col_var[col1] = tab->col_var[col2];
1405         tab->col_var[col2] = t;
1406         var_from_col(tab, col1)->index = col1;
1407         var_from_col(tab, col2)->index = col2;
1408         tab->mat = isl_mat_swap_cols(tab->mat, off + col1, off + col2);
1409 }
1410
1411 /* Mark column with index "col" as representing a zero variable.
1412  * If we may need to undo the operation the column is kept,
1413  * but no longer considered.
1414  * Otherwise, the column is simply removed.
1415  *
1416  * The column may be interchanged with some other column.  If it
1417  * is interchanged with a later column, return 1.  Otherwise return 0.
1418  * If the columns are checked in order in the calling function,
1419  * then a return value of 1 means that the column with the given
1420  * column number may now contain a different column that
1421  * hasn't been checked yet.
1422  */
1423 int isl_tab_kill_col(struct isl_tab *tab, int col)
1424 {
1425         var_from_col(tab, col)->is_zero = 1;
1426         if (tab->need_undo) {
1427                 if (isl_tab_push_var(tab, isl_tab_undo_zero,
1428                                             var_from_col(tab, col)) < 0)
1429                         return -1;
1430                 if (col != tab->n_dead)
1431                         swap_cols(tab, col, tab->n_dead);
1432                 tab->n_dead++;
1433                 return 0;
1434         } else {
1435                 if (col != tab->n_col - 1)
1436                         swap_cols(tab, col, tab->n_col - 1);
1437                 var_from_col(tab, tab->n_col - 1)->index = -1;
1438                 tab->n_col--;
1439                 return 1;
1440         }
1441 }
1442
1443 /* Row variable "var" is non-negative and cannot attain any values
1444  * larger than zero.  This means that the coefficients of the unrestricted
1445  * column variables are zero and that the coefficients of the non-negative
1446  * column variables are zero or negative.
1447  * Each of the non-negative variables with a negative coefficient can
1448  * then also be written as the negative sum of non-negative variables
1449  * and must therefore also be zero.
1450  */
1451 static int close_row(struct isl_tab *tab, struct isl_tab_var *var)
1452 {
1453         int j;
1454         struct isl_mat *mat = tab->mat;
1455         unsigned off = 2 + tab->M;
1456
1457         isl_assert(tab->mat->ctx, var->is_nonneg, return -1);
1458         var->is_zero = 1;
1459         if (tab->need_undo)
1460                 if (isl_tab_push_var(tab, isl_tab_undo_zero, var) < 0)
1461                         return -1;
1462         for (j = tab->n_dead; j < tab->n_col; ++j) {
1463                 if (isl_int_is_zero(mat->row[var->index][off + j]))
1464                         continue;
1465                 isl_assert(tab->mat->ctx,
1466                     isl_int_is_neg(mat->row[var->index][off + j]), return -1);
1467                 if (isl_tab_kill_col(tab, j))
1468                         --j;
1469         }
1470         if (isl_tab_mark_redundant(tab, var->index) < 0)
1471                 return -1;
1472         return 0;
1473 }
1474
1475 /* Add a constraint to the tableau and allocate a row for it.
1476  * Return the index into the constraint array "con".
1477  */
1478 int isl_tab_allocate_con(struct isl_tab *tab)
1479 {
1480         int r;
1481
1482         isl_assert(tab->mat->ctx, tab->n_row < tab->mat->n_row, return -1);
1483         isl_assert(tab->mat->ctx, tab->n_con < tab->max_con, return -1);
1484
1485         r = tab->n_con;
1486         tab->con[r].index = tab->n_row;
1487         tab->con[r].is_row = 1;
1488         tab->con[r].is_nonneg = 0;
1489         tab->con[r].is_zero = 0;
1490         tab->con[r].is_redundant = 0;
1491         tab->con[r].frozen = 0;
1492         tab->con[r].negated = 0;
1493         tab->row_var[tab->n_row] = ~r;
1494
1495         tab->n_row++;
1496         tab->n_con++;
1497         if (isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->con[r]) < 0)
1498                 return -1;
1499
1500         return r;
1501 }
1502
1503 /* Add a variable to the tableau and allocate a column for it.
1504  * Return the index into the variable array "var".
1505  */
1506 int isl_tab_allocate_var(struct isl_tab *tab)
1507 {
1508         int r;
1509         int i;
1510         unsigned off = 2 + tab->M;
1511
1512         isl_assert(tab->mat->ctx, tab->n_col < tab->mat->n_col, return -1);
1513         isl_assert(tab->mat->ctx, tab->n_var < tab->max_var, return -1);
1514
1515         r = tab->n_var;
1516         tab->var[r].index = tab->n_col;
1517         tab->var[r].is_row = 0;
1518         tab->var[r].is_nonneg = 0;
1519         tab->var[r].is_zero = 0;
1520         tab->var[r].is_redundant = 0;
1521         tab->var[r].frozen = 0;
1522         tab->var[r].negated = 0;
1523         tab->col_var[tab->n_col] = r;
1524
1525         for (i = 0; i < tab->n_row; ++i)
1526                 isl_int_set_si(tab->mat->row[i][off + tab->n_col], 0);
1527
1528         tab->n_var++;
1529         tab->n_col++;
1530         if (isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->var[r]) < 0)
1531                 return -1;
1532
1533         return r;
1534 }
1535
1536 /* Add a row to the tableau.  The row is given as an affine combination
1537  * of the original variables and needs to be expressed in terms of the
1538  * column variables.
1539  *
1540  * We add each term in turn.
1541  * If r = n/d_r is the current sum and we need to add k x, then
1542  *      if x is a column variable, we increase the numerator of
1543  *              this column by k d_r
1544  *      if x = f/d_x is a row variable, then the new representation of r is
1545  *
1546  *               n    k f   d_x/g n + d_r/g k f   m/d_r n + m/d_g k f
1547  *              --- + --- = ------------------- = -------------------
1548  *              d_r   d_r        d_r d_x/g                m
1549  *
1550  *      with g the gcd of d_r and d_x and m the lcm of d_r and d_x.
1551  */
1552 int isl_tab_add_row(struct isl_tab *tab, isl_int *line)
1553 {
1554         int i;
1555         int r;
1556         isl_int *row;
1557         isl_int a, b;
1558         unsigned off = 2 + tab->M;
1559
1560         r = isl_tab_allocate_con(tab);
1561         if (r < 0)
1562                 return -1;
1563
1564         isl_int_init(a);
1565         isl_int_init(b);
1566         row = tab->mat->row[tab->con[r].index];
1567         isl_int_set_si(row[0], 1);
1568         isl_int_set(row[1], line[0]);
1569         isl_seq_clr(row + 2, tab->M + tab->n_col);
1570         for (i = 0; i < tab->n_var; ++i) {
1571                 if (tab->var[i].is_zero)
1572                         continue;
1573                 if (tab->var[i].is_row) {
1574                         isl_int_lcm(a,
1575                                 row[0], tab->mat->row[tab->var[i].index][0]);
1576                         isl_int_swap(a, row[0]);
1577                         isl_int_divexact(a, row[0], a);
1578                         isl_int_divexact(b,
1579                                 row[0], tab->mat->row[tab->var[i].index][0]);
1580                         isl_int_mul(b, b, line[1 + i]);
1581                         isl_seq_combine(row + 1, a, row + 1,
1582                             b, tab->mat->row[tab->var[i].index] + 1,
1583                             1 + tab->M + tab->n_col);
1584                 } else
1585                         isl_int_addmul(row[off + tab->var[i].index],
1586                                                         line[1 + i], row[0]);
1587                 if (tab->M && i >= tab->n_param && i < tab->n_var - tab->n_div)
1588                         isl_int_submul(row[2], line[1 + i], row[0]);
1589         }
1590         isl_seq_normalize(tab->mat->ctx, row, off + tab->n_col);
1591         isl_int_clear(a);
1592         isl_int_clear(b);
1593
1594         if (tab->row_sign)
1595                 tab->row_sign[tab->con[r].index] = 0;
1596
1597         return r;
1598 }
1599
1600 static int drop_row(struct isl_tab *tab, int row)
1601 {
1602         isl_assert(tab->mat->ctx, ~tab->row_var[row] == tab->n_con - 1, return -1);
1603         if (row != tab->n_row - 1)
1604                 swap_rows(tab, row, tab->n_row - 1);
1605         tab->n_row--;
1606         tab->n_con--;
1607         return 0;
1608 }
1609
1610 static int drop_col(struct isl_tab *tab, int col)
1611 {
1612         isl_assert(tab->mat->ctx, tab->col_var[col] == tab->n_var - 1, return -1);
1613         if (col != tab->n_col - 1)
1614                 swap_cols(tab, col, tab->n_col - 1);
1615         tab->n_col--;
1616         tab->n_var--;
1617         return 0;
1618 }
1619
1620 /* Add inequality "ineq" and check if it conflicts with the
1621  * previously added constraints or if it is obviously redundant.
1622  */
1623 struct isl_tab *isl_tab_add_ineq(struct isl_tab *tab, isl_int *ineq)
1624 {
1625         int r;
1626         int sgn;
1627         isl_int cst;
1628
1629         if (!tab)
1630                 return NULL;
1631         if (tab->bset) {
1632                 struct isl_basic_set *bset = tab->bset;
1633
1634                 isl_assert(tab->mat->ctx, tab->n_eq == bset->n_eq, goto error);
1635                 isl_assert(tab->mat->ctx,
1636                             tab->n_con == bset->n_eq + bset->n_ineq, goto error);
1637                 tab->bset = isl_basic_set_add_ineq(tab->bset, ineq);
1638                 if (isl_tab_push(tab, isl_tab_undo_bset_ineq) < 0)
1639                         goto error;
1640                 if (!tab->bset)
1641                         goto error;
1642         }
1643         if (tab->cone) {
1644                 isl_int_init(cst);
1645                 isl_int_swap(ineq[0], cst);
1646         }
1647         r = isl_tab_add_row(tab, ineq);
1648         if (tab->cone) {
1649                 isl_int_swap(ineq[0], cst);
1650                 isl_int_clear(cst);
1651         }
1652         if (r < 0)
1653                 goto error;
1654         tab->con[r].is_nonneg = 1;
1655         if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1656                 goto error;
1657         if (isl_tab_row_is_redundant(tab, tab->con[r].index)) {
1658                 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1659                         goto error;
1660                 return tab;
1661         }
1662
1663         sgn = restore_row(tab, &tab->con[r]);
1664         if (sgn < -1)
1665                 goto error;
1666         if (sgn < 0)
1667                 return isl_tab_mark_empty(tab);
1668         if (tab->con[r].is_row && isl_tab_row_is_redundant(tab, tab->con[r].index))
1669                 if (isl_tab_mark_redundant(tab, tab->con[r].index) < 0)
1670                         goto error;
1671         return tab;
1672 error:
1673         isl_tab_free(tab);
1674         return NULL;
1675 }
1676
1677 /* Pivot a non-negative variable down until it reaches the value zero
1678  * and then pivot the variable into a column position.
1679  */
1680 static int to_col(struct isl_tab *tab, struct isl_tab_var *var)
1681 {
1682         int i;
1683         int row, col;
1684         unsigned off = 2 + tab->M;
1685
1686         if (!var->is_row)
1687                 return 0;
1688
1689         while (isl_int_is_pos(tab->mat->row[var->index][1])) {
1690                 find_pivot(tab, var, NULL, -1, &row, &col);
1691                 isl_assert(tab->mat->ctx, row != -1, return -1);
1692                 if (isl_tab_pivot(tab, row, col) < 0)
1693                         return -1;
1694                 if (!var->is_row)
1695                         return 0;
1696         }
1697
1698         for (i = tab->n_dead; i < tab->n_col; ++i)
1699                 if (!isl_int_is_zero(tab->mat->row[var->index][off + i]))
1700                         break;
1701
1702         isl_assert(tab->mat->ctx, i < tab->n_col, return -1);
1703         if (isl_tab_pivot(tab, var->index, i) < 0)
1704                 return -1;
1705
1706         return 0;
1707 }
1708
1709 /* We assume Gaussian elimination has been performed on the equalities.
1710  * The equalities can therefore never conflict.
1711  * Adding the equalities is currently only really useful for a later call
1712  * to isl_tab_ineq_type.
1713  */
1714 static struct isl_tab *add_eq(struct isl_tab *tab, isl_int *eq)
1715 {
1716         int i;
1717         int r;
1718
1719         if (!tab)
1720                 return NULL;
1721         r = isl_tab_add_row(tab, eq);
1722         if (r < 0)
1723                 goto error;
1724
1725         r = tab->con[r].index;
1726         i = isl_seq_first_non_zero(tab->mat->row[r] + 2 + tab->M + tab->n_dead,
1727                                         tab->n_col - tab->n_dead);
1728         isl_assert(tab->mat->ctx, i >= 0, goto error);
1729         i += tab->n_dead;
1730         if (isl_tab_pivot(tab, r, i) < 0)
1731                 goto error;
1732         if (isl_tab_kill_col(tab, i) < 0)
1733                 goto error;
1734         tab->n_eq++;
1735
1736         return tab;
1737 error:
1738         isl_tab_free(tab);
1739         return NULL;
1740 }
1741
1742 static int row_is_manifestly_zero(struct isl_tab *tab, int row)
1743 {
1744         unsigned off = 2 + tab->M;
1745
1746         if (!isl_int_is_zero(tab->mat->row[row][1]))
1747                 return 0;
1748         if (tab->M && !isl_int_is_zero(tab->mat->row[row][2]))
1749                 return 0;
1750         return isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
1751                                         tab->n_col - tab->n_dead) == -1;
1752 }
1753
1754 /* Add an equality that is known to be valid for the given tableau.
1755  */
1756 struct isl_tab *isl_tab_add_valid_eq(struct isl_tab *tab, isl_int *eq)
1757 {
1758         struct isl_tab_var *var;
1759         int r;
1760
1761         if (!tab)
1762                 return NULL;
1763         r = isl_tab_add_row(tab, eq);
1764         if (r < 0)
1765                 goto error;
1766
1767         var = &tab->con[r];
1768         r = var->index;
1769         if (row_is_manifestly_zero(tab, r)) {
1770                 var->is_zero = 1;
1771                 if (isl_tab_mark_redundant(tab, r) < 0)
1772                         goto error;
1773                 return tab;
1774         }
1775
1776         if (isl_int_is_neg(tab->mat->row[r][1])) {
1777                 isl_seq_neg(tab->mat->row[r] + 1, tab->mat->row[r] + 1,
1778                             1 + tab->n_col);
1779                 var->negated = 1;
1780         }
1781         var->is_nonneg = 1;
1782         if (to_col(tab, var) < 0)
1783                 goto error;
1784         var->is_nonneg = 0;
1785         if (isl_tab_kill_col(tab, var->index) < 0)
1786                 goto error;
1787
1788         return tab;
1789 error:
1790         isl_tab_free(tab);
1791         return NULL;
1792 }
1793
1794 static int add_zero_row(struct isl_tab *tab)
1795 {
1796         int r;
1797         isl_int *row;
1798
1799         r = isl_tab_allocate_con(tab);
1800         if (r < 0)
1801                 return -1;
1802
1803         row = tab->mat->row[tab->con[r].index];
1804         isl_seq_clr(row + 1, 1 + tab->M + tab->n_col);
1805         isl_int_set_si(row[0], 1);
1806
1807         return r;
1808 }
1809
1810 /* Add equality "eq" and check if it conflicts with the
1811  * previously added constraints or if it is obviously redundant.
1812  */
1813 struct isl_tab *isl_tab_add_eq(struct isl_tab *tab, isl_int *eq)
1814 {
1815         struct isl_tab_undo *snap = NULL;
1816         struct isl_tab_var *var;
1817         int r;
1818         int row;
1819         int sgn;
1820         isl_int cst;
1821
1822         if (!tab)
1823                 return NULL;
1824         isl_assert(tab->mat->ctx, !tab->M, goto error);
1825
1826         if (tab->need_undo)
1827                 snap = isl_tab_snap(tab);
1828
1829         if (tab->cone) {
1830                 isl_int_init(cst);
1831                 isl_int_swap(eq[0], cst);
1832         }
1833         r = isl_tab_add_row(tab, eq);
1834         if (tab->cone) {
1835                 isl_int_swap(eq[0], cst);
1836                 isl_int_clear(cst);
1837         }
1838         if (r < 0)
1839                 goto error;
1840
1841         var = &tab->con[r];
1842         row = var->index;
1843         if (row_is_manifestly_zero(tab, row)) {
1844                 if (snap) {
1845                         if (isl_tab_rollback(tab, snap) < 0)
1846                                 goto error;
1847                 } else
1848                         drop_row(tab, row);
1849                 return tab;
1850         }
1851
1852         if (tab->bset) {
1853                 tab->bset = isl_basic_set_add_ineq(tab->bset, eq);
1854                 if (isl_tab_push(tab, isl_tab_undo_bset_ineq) < 0)
1855                         goto error;
1856                 isl_seq_neg(eq, eq, 1 + tab->n_var);
1857                 tab->bset = isl_basic_set_add_ineq(tab->bset, eq);
1858                 isl_seq_neg(eq, eq, 1 + tab->n_var);
1859                 if (isl_tab_push(tab, isl_tab_undo_bset_ineq) < 0)
1860                         goto error;
1861                 if (!tab->bset)
1862                         goto error;
1863                 if (add_zero_row(tab) < 0)
1864                         goto error;
1865         }
1866
1867         sgn = isl_int_sgn(tab->mat->row[row][1]);
1868
1869         if (sgn > 0) {
1870                 isl_seq_neg(tab->mat->row[row] + 1, tab->mat->row[row] + 1,
1871                             1 + tab->n_col);
1872                 var->negated = 1;
1873                 sgn = -1;
1874         }
1875
1876         if (sgn < 0) {
1877                 sgn = sign_of_max(tab, var);
1878                 if (sgn < -1)
1879                         goto error;
1880                 if (sgn < 0)
1881                         return isl_tab_mark_empty(tab);
1882         }
1883
1884         var->is_nonneg = 1;
1885         if (to_col(tab, var) < 0)
1886                 goto error;
1887         var->is_nonneg = 0;
1888         if (isl_tab_kill_col(tab, var->index) < 0)
1889                 goto error;
1890
1891         return tab;
1892 error:
1893         isl_tab_free(tab);
1894         return NULL;
1895 }
1896
1897 struct isl_tab *isl_tab_from_basic_map(struct isl_basic_map *bmap)
1898 {
1899         int i;
1900         struct isl_tab *tab;
1901
1902         if (!bmap)
1903                 return NULL;
1904         tab = isl_tab_alloc(bmap->ctx,
1905                             isl_basic_map_total_dim(bmap) + bmap->n_ineq + 1,
1906                             isl_basic_map_total_dim(bmap), 0);
1907         if (!tab)
1908                 return NULL;
1909         tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
1910         if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1911                 return isl_tab_mark_empty(tab);
1912         for (i = 0; i < bmap->n_eq; ++i) {
1913                 tab = add_eq(tab, bmap->eq[i]);
1914                 if (!tab)
1915                         return tab;
1916         }
1917         for (i = 0; i < bmap->n_ineq; ++i) {
1918                 tab = isl_tab_add_ineq(tab, bmap->ineq[i]);
1919                 if (!tab || tab->empty)
1920                         return tab;
1921         }
1922         return tab;
1923 }
1924
1925 struct isl_tab *isl_tab_from_basic_set(struct isl_basic_set *bset)
1926 {
1927         return isl_tab_from_basic_map((struct isl_basic_map *)bset);
1928 }
1929
1930 /* Construct a tableau corresponding to the recession cone of "bset".
1931  */
1932 struct isl_tab *isl_tab_from_recession_cone(struct isl_basic_set *bset)
1933 {
1934         isl_int cst;
1935         int i;
1936         struct isl_tab *tab;
1937
1938         if (!bset)
1939                 return NULL;
1940         tab = isl_tab_alloc(bset->ctx, bset->n_eq + bset->n_ineq,
1941                                 isl_basic_set_total_dim(bset), 0);
1942         if (!tab)
1943                 return NULL;
1944         tab->rational = ISL_F_ISSET(bset, ISL_BASIC_SET_RATIONAL);
1945         tab->cone = 1;
1946
1947         isl_int_init(cst);
1948         for (i = 0; i < bset->n_eq; ++i) {
1949                 isl_int_swap(bset->eq[i][0], cst);
1950                 tab = add_eq(tab, bset->eq[i]);
1951                 isl_int_swap(bset->eq[i][0], cst);
1952                 if (!tab)
1953                         goto done;
1954         }
1955         for (i = 0; i < bset->n_ineq; ++i) {
1956                 int r;
1957                 isl_int_swap(bset->ineq[i][0], cst);
1958                 r = isl_tab_add_row(tab, bset->ineq[i]);
1959                 isl_int_swap(bset->ineq[i][0], cst);
1960                 if (r < 0)
1961                         goto error;
1962                 tab->con[r].is_nonneg = 1;
1963                 if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
1964                         goto error;
1965         }
1966 done:
1967         isl_int_clear(cst);
1968         return tab;
1969 error:
1970         isl_int_clear(cst);
1971         isl_tab_free(tab);
1972         return NULL;
1973 }
1974
1975 /* Assuming "tab" is the tableau of a cone, check if the cone is
1976  * bounded, i.e., if it is empty or only contains the origin.
1977  */
1978 int isl_tab_cone_is_bounded(struct isl_tab *tab)
1979 {
1980         int i;
1981
1982         if (!tab)
1983                 return -1;
1984         if (tab->empty)
1985                 return 1;
1986         if (tab->n_dead == tab->n_col)
1987                 return 1;
1988
1989         for (;;) {
1990                 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1991                         struct isl_tab_var *var;
1992                         int sgn;
1993                         var = isl_tab_var_from_row(tab, i);
1994                         if (!var->is_nonneg)
1995                                 continue;
1996                         sgn = sign_of_max(tab, var);
1997                         if (sgn < -1)
1998                                 return -1;
1999                         if (sgn != 0)
2000                                 return 0;
2001                         if (close_row(tab, var) < 0)
2002                                 return -1;
2003                         break;
2004                 }
2005                 if (tab->n_dead == tab->n_col)
2006                         return 1;
2007                 if (i == tab->n_row)
2008                         return 0;
2009         }
2010 }
2011
2012 int isl_tab_sample_is_integer(struct isl_tab *tab)
2013 {
2014         int i;
2015
2016         if (!tab)
2017                 return -1;
2018
2019         for (i = 0; i < tab->n_var; ++i) {
2020                 int row;
2021                 if (!tab->var[i].is_row)
2022                         continue;
2023                 row = tab->var[i].index;
2024                 if (!isl_int_is_divisible_by(tab->mat->row[row][1],
2025                                                 tab->mat->row[row][0]))
2026                         return 0;
2027         }
2028         return 1;
2029 }
2030
2031 static struct isl_vec *extract_integer_sample(struct isl_tab *tab)
2032 {
2033         int i;
2034         struct isl_vec *vec;
2035
2036         vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
2037         if (!vec)
2038                 return NULL;
2039
2040         isl_int_set_si(vec->block.data[0], 1);
2041         for (i = 0; i < tab->n_var; ++i) {
2042                 if (!tab->var[i].is_row)
2043                         isl_int_set_si(vec->block.data[1 + i], 0);
2044                 else {
2045                         int row = tab->var[i].index;
2046                         isl_int_divexact(vec->block.data[1 + i],
2047                                 tab->mat->row[row][1], tab->mat->row[row][0]);
2048                 }
2049         }
2050
2051         return vec;
2052 }
2053
2054 struct isl_vec *isl_tab_get_sample_value(struct isl_tab *tab)
2055 {
2056         int i;
2057         struct isl_vec *vec;
2058         isl_int m;
2059
2060         if (!tab)
2061                 return NULL;
2062
2063         vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
2064         if (!vec)
2065                 return NULL;
2066
2067         isl_int_init(m);
2068
2069         isl_int_set_si(vec->block.data[0], 1);
2070         for (i = 0; i < tab->n_var; ++i) {
2071                 int row;
2072                 if (!tab->var[i].is_row) {
2073                         isl_int_set_si(vec->block.data[1 + i], 0);
2074                         continue;
2075                 }
2076                 row = tab->var[i].index;
2077                 isl_int_gcd(m, vec->block.data[0], tab->mat->row[row][0]);
2078                 isl_int_divexact(m, tab->mat->row[row][0], m);
2079                 isl_seq_scale(vec->block.data, vec->block.data, m, 1 + i);
2080                 isl_int_divexact(m, vec->block.data[0], tab->mat->row[row][0]);
2081                 isl_int_mul(vec->block.data[1 + i], m, tab->mat->row[row][1]);
2082         }
2083         vec = isl_vec_normalize(vec);
2084
2085         isl_int_clear(m);
2086         return vec;
2087 }
2088
2089 /* Update "bmap" based on the results of the tableau "tab".
2090  * In particular, implicit equalities are made explicit, redundant constraints
2091  * are removed and if the sample value happens to be integer, it is stored
2092  * in "bmap" (unless "bmap" already had an integer sample).
2093  *
2094  * The tableau is assumed to have been created from "bmap" using
2095  * isl_tab_from_basic_map.
2096  */
2097 struct isl_basic_map *isl_basic_map_update_from_tab(struct isl_basic_map *bmap,
2098         struct isl_tab *tab)
2099 {
2100         int i;
2101         unsigned n_eq;
2102
2103         if (!bmap)
2104                 return NULL;
2105         if (!tab)
2106                 return bmap;
2107
2108         n_eq = tab->n_eq;
2109         if (tab->empty)
2110                 bmap = isl_basic_map_set_to_empty(bmap);
2111         else
2112                 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2113                         if (isl_tab_is_equality(tab, n_eq + i))
2114                                 isl_basic_map_inequality_to_equality(bmap, i);
2115                         else if (isl_tab_is_redundant(tab, n_eq + i))
2116                                 isl_basic_map_drop_inequality(bmap, i);
2117                 }
2118         if (!tab->rational &&
2119             !bmap->sample && isl_tab_sample_is_integer(tab))
2120                 bmap->sample = extract_integer_sample(tab);
2121         return bmap;
2122 }
2123
2124 struct isl_basic_set *isl_basic_set_update_from_tab(struct isl_basic_set *bset,
2125         struct isl_tab *tab)
2126 {
2127         return (struct isl_basic_set *)isl_basic_map_update_from_tab(
2128                 (struct isl_basic_map *)bset, tab);
2129 }
2130
2131 /* Given a non-negative variable "var", add a new non-negative variable
2132  * that is the opposite of "var", ensuring that var can only attain the
2133  * value zero.
2134  * If var = n/d is a row variable, then the new variable = -n/d.
2135  * If var is a column variables, then the new variable = -var.
2136  * If the new variable cannot attain non-negative values, then
2137  * the resulting tableau is empty.
2138  * Otherwise, we know the value will be zero and we close the row.
2139  */
2140 static struct isl_tab *cut_to_hyperplane(struct isl_tab *tab,
2141         struct isl_tab_var *var)
2142 {
2143         unsigned r;
2144         isl_int *row;
2145         int sgn;
2146         unsigned off = 2 + tab->M;
2147
2148         if (var->is_zero)
2149                 return tab;
2150         isl_assert(tab->mat->ctx, !var->is_redundant, goto error);
2151
2152         if (isl_tab_extend_cons(tab, 1) < 0)
2153                 goto error;
2154
2155         r = tab->n_con;
2156         tab->con[r].index = tab->n_row;
2157         tab->con[r].is_row = 1;
2158         tab->con[r].is_nonneg = 0;
2159         tab->con[r].is_zero = 0;
2160         tab->con[r].is_redundant = 0;
2161         tab->con[r].frozen = 0;
2162         tab->con[r].negated = 0;
2163         tab->row_var[tab->n_row] = ~r;
2164         row = tab->mat->row[tab->n_row];
2165
2166         if (var->is_row) {
2167                 isl_int_set(row[0], tab->mat->row[var->index][0]);
2168                 isl_seq_neg(row + 1,
2169                             tab->mat->row[var->index] + 1, 1 + tab->n_col);
2170         } else {
2171                 isl_int_set_si(row[0], 1);
2172                 isl_seq_clr(row + 1, 1 + tab->n_col);
2173                 isl_int_set_si(row[off + var->index], -1);
2174         }
2175
2176         tab->n_row++;
2177         tab->n_con++;
2178         if (isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->con[r]) < 0)
2179                 goto error;
2180
2181         sgn = sign_of_max(tab, &tab->con[r]);
2182         if (sgn < -1)
2183                 goto error;
2184         if (sgn < 0)
2185                 return isl_tab_mark_empty(tab);
2186         tab->con[r].is_nonneg = 1;
2187         if (isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]) < 0)
2188                 goto error;
2189         /* sgn == 0 */
2190         if (close_row(tab, &tab->con[r]) < 0)
2191                 goto error;
2192
2193         return tab;
2194 error:
2195         isl_tab_free(tab);
2196         return NULL;
2197 }
2198
2199 /* Given a tableau "tab" and an inequality constraint "con" of the tableau,
2200  * relax the inequality by one.  That is, the inequality r >= 0 is replaced
2201  * by r' = r + 1 >= 0.
2202  * If r is a row variable, we simply increase the constant term by one
2203  * (taking into account the denominator).
2204  * If r is a column variable, then we need to modify each row that
2205  * refers to r = r' - 1 by substituting this equality, effectively
2206  * subtracting the coefficient of the column from the constant.
2207  */
2208 struct isl_tab *isl_tab_relax(struct isl_tab *tab, int con)
2209 {
2210         struct isl_tab_var *var;
2211         unsigned off = 2 + tab->M;
2212
2213         if (!tab)
2214                 return NULL;
2215
2216         var = &tab->con[con];
2217
2218         if (!var->is_row && !max_is_manifestly_unbounded(tab, var))
2219                 if (to_row(tab, var, 1) < 0)
2220                         goto error;
2221
2222         if (var->is_row)
2223                 isl_int_add(tab->mat->row[var->index][1],
2224                     tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
2225         else {
2226                 int i;
2227
2228                 for (i = 0; i < tab->n_row; ++i) {
2229                         if (isl_int_is_zero(tab->mat->row[i][off + var->index]))
2230                                 continue;
2231                         isl_int_sub(tab->mat->row[i][1], tab->mat->row[i][1],
2232                             tab->mat->row[i][off + var->index]);
2233                 }
2234
2235         }
2236
2237         if (isl_tab_push_var(tab, isl_tab_undo_relax, var) < 0)
2238                 goto error;
2239
2240         return tab;
2241 error:
2242         isl_tab_free(tab);
2243         return NULL;
2244 }
2245
2246 struct isl_tab *isl_tab_select_facet(struct isl_tab *tab, int con)
2247 {
2248         if (!tab)
2249                 return NULL;
2250
2251         return cut_to_hyperplane(tab, &tab->con[con]);
2252 }
2253
2254 static int may_be_equality(struct isl_tab *tab, int row)
2255 {
2256         unsigned off = 2 + tab->M;
2257         return (tab->rational ? isl_int_is_zero(tab->mat->row[row][1])
2258                               : isl_int_lt(tab->mat->row[row][1],
2259                                             tab->mat->row[row][0])) &&
2260                 isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
2261                                         tab->n_col - tab->n_dead) != -1;
2262 }
2263
2264 /* Check for (near) equalities among the constraints.
2265  * A constraint is an equality if it is non-negative and if
2266  * its maximal value is either
2267  *      - zero (in case of rational tableaus), or
2268  *      - strictly less than 1 (in case of integer tableaus)
2269  *
2270  * We first mark all non-redundant and non-dead variables that
2271  * are not frozen and not obviously not an equality.
2272  * Then we iterate over all marked variables if they can attain
2273  * any values larger than zero or at least one.
2274  * If the maximal value is zero, we mark any column variables
2275  * that appear in the row as being zero and mark the row as being redundant.
2276  * Otherwise, if the maximal value is strictly less than one (and the
2277  * tableau is integer), then we restrict the value to being zero
2278  * by adding an opposite non-negative variable.
2279  */
2280 struct isl_tab *isl_tab_detect_implicit_equalities(struct isl_tab *tab)
2281 {
2282         int i;
2283         unsigned n_marked;
2284
2285         if (!tab)
2286                 return NULL;
2287         if (tab->empty)
2288                 return tab;
2289         if (tab->n_dead == tab->n_col)
2290                 return tab;
2291
2292         n_marked = 0;
2293         for (i = tab->n_redundant; i < tab->n_row; ++i) {
2294                 struct isl_tab_var *var = isl_tab_var_from_row(tab, i);
2295                 var->marked = !var->frozen && var->is_nonneg &&
2296                         may_be_equality(tab, i);
2297                 if (var->marked)
2298                         n_marked++;
2299         }
2300         for (i = tab->n_dead; i < tab->n_col; ++i) {
2301                 struct isl_tab_var *var = var_from_col(tab, i);
2302                 var->marked = !var->frozen && var->is_nonneg;
2303                 if (var->marked)
2304                         n_marked++;
2305         }
2306         while (n_marked) {
2307                 struct isl_tab_var *var;
2308                 int sgn;
2309                 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2310                         var = isl_tab_var_from_row(tab, i);
2311                         if (var->marked)
2312                                 break;
2313                 }
2314                 if (i == tab->n_row) {
2315                         for (i = tab->n_dead; i < tab->n_col; ++i) {
2316                                 var = var_from_col(tab, i);
2317                                 if (var->marked)
2318                                         break;
2319                         }
2320                         if (i == tab->n_col)
2321                                 break;
2322                 }
2323                 var->marked = 0;
2324                 n_marked--;
2325                 sgn = sign_of_max(tab, var);
2326                 if (sgn < 0)
2327                         goto error;
2328                 if (sgn == 0) {
2329                         if (close_row(tab, var) < 0)
2330                                 goto error;
2331                 } else if (!tab->rational && !at_least_one(tab, var)) {
2332                         tab = cut_to_hyperplane(tab, var);
2333                         return isl_tab_detect_implicit_equalities(tab);
2334                 }
2335                 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2336                         var = isl_tab_var_from_row(tab, i);
2337                         if (!var->marked)
2338                                 continue;
2339                         if (may_be_equality(tab, i))
2340                                 continue;
2341                         var->marked = 0;
2342                         n_marked--;
2343                 }
2344         }
2345
2346         return tab;
2347 error:
2348         isl_tab_free(tab);
2349         return NULL;
2350 }
2351
2352 static int con_is_redundant(struct isl_tab *tab, struct isl_tab_var *var)
2353 {
2354         if (!tab)
2355                 return -1;
2356         if (tab->rational) {
2357                 int sgn = sign_of_min(tab, var);
2358                 if (sgn < -1)
2359                         return -1;
2360                 return sgn >= 0;
2361         } else {
2362                 int irred = isl_tab_min_at_most_neg_one(tab, var);
2363                 if (irred < 0)
2364                         return -1;
2365                 return !irred;
2366         }
2367 }
2368
2369 /* Check for (near) redundant constraints.
2370  * A constraint is redundant if it is non-negative and if
2371  * its minimal value (temporarily ignoring the non-negativity) is either
2372  *      - zero (in case of rational tableaus), or
2373  *      - strictly larger than -1 (in case of integer tableaus)
2374  *
2375  * We first mark all non-redundant and non-dead variables that
2376  * are not frozen and not obviously negatively unbounded.
2377  * Then we iterate over all marked variables if they can attain
2378  * any values smaller than zero or at most negative one.
2379  * If not, we mark the row as being redundant (assuming it hasn't
2380  * been detected as being obviously redundant in the mean time).
2381  */
2382 struct isl_tab *isl_tab_detect_redundant(struct isl_tab *tab)
2383 {
2384         int i;
2385         unsigned n_marked;
2386
2387         if (!tab)
2388                 return NULL;
2389         if (tab->empty)
2390                 return tab;
2391         if (tab->n_redundant == tab->n_row)
2392                 return tab;
2393
2394         n_marked = 0;
2395         for (i = tab->n_redundant; i < tab->n_row; ++i) {
2396                 struct isl_tab_var *var = isl_tab_var_from_row(tab, i);
2397                 var->marked = !var->frozen && var->is_nonneg;
2398                 if (var->marked)
2399                         n_marked++;
2400         }
2401         for (i = tab->n_dead; i < tab->n_col; ++i) {
2402                 struct isl_tab_var *var = var_from_col(tab, i);
2403                 var->marked = !var->frozen && var->is_nonneg &&
2404                         !min_is_manifestly_unbounded(tab, var);
2405                 if (var->marked)
2406                         n_marked++;
2407         }
2408         while (n_marked) {
2409                 struct isl_tab_var *var;
2410                 int red;
2411                 for (i = tab->n_redundant; i < tab->n_row; ++i) {
2412                         var = isl_tab_var_from_row(tab, i);
2413                         if (var->marked)
2414                                 break;
2415                 }
2416                 if (i == tab->n_row) {
2417                         for (i = tab->n_dead; i < tab->n_col; ++i) {
2418                                 var = var_from_col(tab, i);
2419                                 if (var->marked)
2420                                         break;
2421                         }
2422                         if (i == tab->n_col)
2423                                 break;
2424                 }
2425                 var->marked = 0;
2426                 n_marked--;
2427                 red = con_is_redundant(tab, var);
2428                 if (red < 0)
2429                         goto error;
2430                 if (red && !var->is_redundant)
2431                         if (isl_tab_mark_redundant(tab, var->index) < 0)
2432                                 goto error;
2433                 for (i = tab->n_dead; i < tab->n_col; ++i) {
2434                         var = var_from_col(tab, i);
2435                         if (!var->marked)
2436                                 continue;
2437                         if (!min_is_manifestly_unbounded(tab, var))
2438                                 continue;
2439                         var->marked = 0;
2440                         n_marked--;
2441                 }
2442         }
2443
2444         return tab;
2445 error:
2446         isl_tab_free(tab);
2447         return NULL;
2448 }
2449
2450 int isl_tab_is_equality(struct isl_tab *tab, int con)
2451 {
2452         int row;
2453         unsigned off;
2454
2455         if (!tab)
2456                 return -1;
2457         if (tab->con[con].is_zero)
2458                 return 1;
2459         if (tab->con[con].is_redundant)
2460                 return 0;
2461         if (!tab->con[con].is_row)
2462                 return tab->con[con].index < tab->n_dead;
2463
2464         row = tab->con[con].index;
2465
2466         off = 2 + tab->M;
2467         return isl_int_is_zero(tab->mat->row[row][1]) &&
2468                 isl_seq_first_non_zero(tab->mat->row[row] + 2 + tab->n_dead,
2469                                         tab->n_col - tab->n_dead) == -1;
2470 }
2471
2472 /* Return the minimial value of the affine expression "f" with denominator
2473  * "denom" in *opt, *opt_denom, assuming the tableau is not empty and
2474  * the expression cannot attain arbitrarily small values.
2475  * If opt_denom is NULL, then *opt is rounded up to the nearest integer.
2476  * The return value reflects the nature of the result (empty, unbounded,
2477  * minmimal value returned in *opt).
2478  */
2479 enum isl_lp_result isl_tab_min(struct isl_tab *tab,
2480         isl_int *f, isl_int denom, isl_int *opt, isl_int *opt_denom,
2481         unsigned flags)
2482 {
2483         int r;
2484         enum isl_lp_result res = isl_lp_ok;
2485         struct isl_tab_var *var;
2486         struct isl_tab_undo *snap;
2487
2488         if (tab->empty)
2489                 return isl_lp_empty;
2490
2491         snap = isl_tab_snap(tab);
2492         r = isl_tab_add_row(tab, f);
2493         if (r < 0)
2494                 return isl_lp_error;
2495         var = &tab->con[r];
2496         isl_int_mul(tab->mat->row[var->index][0],
2497                     tab->mat->row[var->index][0], denom);
2498         for (;;) {
2499                 int row, col;
2500                 find_pivot(tab, var, var, -1, &row, &col);
2501                 if (row == var->index) {
2502                         res = isl_lp_unbounded;
2503                         break;
2504                 }
2505                 if (row == -1)
2506                         break;
2507                 if (isl_tab_pivot(tab, row, col) < 0)
2508                         return isl_lp_error;
2509         }
2510         if (ISL_FL_ISSET(flags, ISL_TAB_SAVE_DUAL)) {
2511                 int i;
2512
2513                 isl_vec_free(tab->dual);
2514                 tab->dual = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_con);
2515                 if (!tab->dual)
2516                         return isl_lp_error;
2517                 isl_int_set(tab->dual->el[0], tab->mat->row[var->index][0]);
2518                 for (i = 0; i < tab->n_con; ++i) {
2519                         int pos;
2520                         if (tab->con[i].is_row) {
2521                                 isl_int_set_si(tab->dual->el[1 + i], 0);
2522                                 continue;
2523                         }
2524                         pos = 2 + tab->M + tab->con[i].index;
2525                         if (tab->con[i].negated)
2526                                 isl_int_neg(tab->dual->el[1 + i],
2527                                             tab->mat->row[var->index][pos]);
2528                         else
2529                                 isl_int_set(tab->dual->el[1 + i],
2530                                             tab->mat->row[var->index][pos]);
2531                 }
2532         }
2533         if (opt && res == isl_lp_ok) {
2534                 if (opt_denom) {
2535                         isl_int_set(*opt, tab->mat->row[var->index][1]);
2536                         isl_int_set(*opt_denom, tab->mat->row[var->index][0]);
2537                 } else
2538                         isl_int_cdiv_q(*opt, tab->mat->row[var->index][1],
2539                                              tab->mat->row[var->index][0]);
2540         }
2541         if (isl_tab_rollback(tab, snap) < 0)
2542                 return isl_lp_error;
2543         return res;
2544 }
2545
2546 int isl_tab_is_redundant(struct isl_tab *tab, int con)
2547 {
2548         if (!tab)
2549                 return -1;
2550         if (tab->con[con].is_zero)
2551                 return 0;
2552         if (tab->con[con].is_redundant)
2553                 return 1;
2554         return tab->con[con].is_row && tab->con[con].index < tab->n_redundant;
2555 }
2556
2557 /* Take a snapshot of the tableau that can be restored by s call to
2558  * isl_tab_rollback.
2559  */
2560 struct isl_tab_undo *isl_tab_snap(struct isl_tab *tab)
2561 {
2562         if (!tab)
2563                 return NULL;
2564         tab->need_undo = 1;
2565         return tab->top;
2566 }
2567
2568 /* Undo the operation performed by isl_tab_relax.
2569  */
2570 static int unrelax(struct isl_tab *tab, struct isl_tab_var *var)
2571 {
2572         unsigned off = 2 + tab->M;
2573
2574         if (!var->is_row && !max_is_manifestly_unbounded(tab, var))
2575                 if (to_row(tab, var, 1) < 0)
2576                         return -1;
2577
2578         if (var->is_row)
2579                 isl_int_sub(tab->mat->row[var->index][1],
2580                     tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
2581         else {
2582                 int i;
2583
2584                 for (i = 0; i < tab->n_row; ++i) {
2585                         if (isl_int_is_zero(tab->mat->row[i][off + var->index]))
2586                                 continue;
2587                         isl_int_add(tab->mat->row[i][1], tab->mat->row[i][1],
2588                             tab->mat->row[i][off + var->index]);
2589                 }
2590
2591         }
2592
2593         return 0;
2594 }
2595
2596 static int perform_undo_var(struct isl_tab *tab, struct isl_tab_undo *undo)
2597 {
2598         struct isl_tab_var *var = var_from_index(tab, undo->u.var_index);
2599         switch(undo->type) {
2600         case isl_tab_undo_nonneg:
2601                 var->is_nonneg = 0;
2602                 break;
2603         case isl_tab_undo_redundant:
2604                 var->is_redundant = 0;
2605                 tab->n_redundant--;
2606                 break;
2607         case isl_tab_undo_zero:
2608                 var->is_zero = 0;
2609                 if (!var->is_row)
2610                         tab->n_dead--;
2611                 break;
2612         case isl_tab_undo_allocate:
2613                 if (undo->u.var_index >= 0) {
2614                         isl_assert(tab->mat->ctx, !var->is_row, return -1);
2615                         drop_col(tab, var->index);
2616                         break;
2617                 }
2618                 if (!var->is_row) {
2619                         if (!max_is_manifestly_unbounded(tab, var)) {
2620                                 if (to_row(tab, var, 1) < 0)
2621                                         return -1;
2622                         } else if (!min_is_manifestly_unbounded(tab, var)) {
2623                                 if (to_row(tab, var, -1) < 0)
2624                                         return -1;
2625                         } else
2626                                 if (to_row(tab, var, 0) < 0)
2627                                         return -1;
2628                 }
2629                 drop_row(tab, var->index);
2630                 break;
2631         case isl_tab_undo_relax:
2632                 return unrelax(tab, var);
2633         }
2634
2635         return 0;
2636 }
2637
2638 /* Restore the tableau to the state where the basic variables
2639  * are those in "col_var".
2640  * We first construct a list of variables that are currently in
2641  * the basis, but shouldn't.  Then we iterate over all variables
2642  * that should be in the basis and for each one that is currently
2643  * not in the basis, we exchange it with one of the elements of the
2644  * list constructed before.
2645  * We can always find an appropriate variable to pivot with because
2646  * the current basis is mapped to the old basis by a non-singular
2647  * matrix and so we can never end up with a zero row.
2648  */
2649 static int restore_basis(struct isl_tab *tab, int *col_var)
2650 {
2651         int i, j;
2652         int n_extra = 0;
2653         int *extra = NULL;      /* current columns that contain bad stuff */
2654         unsigned off = 2 + tab->M;
2655
2656         extra = isl_alloc_array(tab->mat->ctx, int, tab->n_col);
2657         if (!extra)
2658                 goto error;
2659         for (i = 0; i < tab->n_col; ++i) {
2660                 for (j = 0; j < tab->n_col; ++j)
2661                         if (tab->col_var[i] == col_var[j])
2662                                 break;
2663                 if (j < tab->n_col)
2664                         continue;
2665                 extra[n_extra++] = i;
2666         }
2667         for (i = 0; i < tab->n_col && n_extra > 0; ++i) {
2668                 struct isl_tab_var *var;
2669                 int row;
2670
2671                 for (j = 0; j < tab->n_col; ++j)
2672                         if (col_var[i] == tab->col_var[j])
2673                                 break;
2674                 if (j < tab->n_col)
2675                         continue;
2676                 var = var_from_index(tab, col_var[i]);
2677                 row = var->index;
2678                 for (j = 0; j < n_extra; ++j)
2679                         if (!isl_int_is_zero(tab->mat->row[row][off+extra[j]]))
2680                                 break;
2681                 isl_assert(tab->mat->ctx, j < n_extra, goto error);
2682                 if (isl_tab_pivot(tab, row, extra[j]) < 0)
2683                         goto error;
2684                 extra[j] = extra[--n_extra];
2685         }
2686
2687         free(extra);
2688         free(col_var);
2689         return 0;
2690 error:
2691         free(extra);
2692         free(col_var);
2693         return -1;
2694 }
2695
2696 /* Remove all samples with index n or greater, i.e., those samples
2697  * that were added since we saved this number of samples in
2698  * isl_tab_save_samples.
2699  */
2700 static void drop_samples_since(struct isl_tab *tab, int n)
2701 {
2702         int i;
2703
2704         for (i = tab->n_sample - 1; i >= 0 && tab->n_sample > n; --i) {
2705                 if (tab->sample_index[i] < n)
2706                         continue;
2707
2708                 if (i != tab->n_sample - 1) {
2709                         int t = tab->sample_index[tab->n_sample-1];
2710                         tab->sample_index[tab->n_sample-1] = tab->sample_index[i];
2711                         tab->sample_index[i] = t;
2712                         isl_mat_swap_rows(tab->samples, tab->n_sample-1, i);
2713                 }
2714                 tab->n_sample--;
2715         }
2716 }
2717
2718 static int perform_undo(struct isl_tab *tab, struct isl_tab_undo *undo)
2719 {
2720         switch (undo->type) {
2721         case isl_tab_undo_empty:
2722                 tab->empty = 0;
2723                 break;
2724         case isl_tab_undo_nonneg:
2725         case isl_tab_undo_redundant:
2726         case isl_tab_undo_zero:
2727         case isl_tab_undo_allocate:
2728         case isl_tab_undo_relax:
2729                 return perform_undo_var(tab, undo);
2730         case isl_tab_undo_bset_eq:
2731                 return isl_basic_set_free_equality(tab->bset, 1);
2732         case isl_tab_undo_bset_ineq:
2733                 return isl_basic_set_free_inequality(tab->bset, 1);
2734         case isl_tab_undo_bset_div:
2735                 if (isl_basic_set_free_div(tab->bset, 1) < 0)
2736                         return -1;
2737                 if (tab->samples)
2738                         tab->samples->n_col--;
2739                 break;
2740         case isl_tab_undo_saved_basis:
2741                 if (restore_basis(tab, undo->u.col_var) < 0)
2742                         return -1;
2743                 break;
2744         case isl_tab_undo_drop_sample:
2745                 tab->n_outside--;
2746                 break;
2747         case isl_tab_undo_saved_samples:
2748                 drop_samples_since(tab, undo->u.n);
2749                 break;
2750         default:
2751                 isl_assert(tab->mat->ctx, 0, return -1);
2752         }
2753         return 0;
2754 }
2755
2756 /* Return the tableau to the state it was in when the snapshot "snap"
2757  * was taken.
2758  */
2759 int isl_tab_rollback(struct isl_tab *tab, struct isl_tab_undo *snap)
2760 {
2761         struct isl_tab_undo *undo, *next;
2762
2763         if (!tab)
2764                 return -1;
2765
2766         tab->in_undo = 1;
2767         for (undo = tab->top; undo && undo != &tab->bottom; undo = next) {
2768                 next = undo->next;
2769                 if (undo == snap)
2770                         break;
2771                 if (perform_undo(tab, undo) < 0) {
2772                         free_undo(tab);
2773                         tab->in_undo = 0;
2774                         return -1;
2775                 }
2776                 free(undo);
2777         }
2778         tab->in_undo = 0;
2779         tab->top = undo;
2780         if (!undo)
2781                 return -1;
2782         return 0;
2783 }
2784
2785 /* The given row "row" represents an inequality violated by all
2786  * points in the tableau.  Check for some special cases of such
2787  * separating constraints.
2788  * In particular, if the row has been reduced to the constant -1,
2789  * then we know the inequality is adjacent (but opposite) to
2790  * an equality in the tableau.
2791  * If the row has been reduced to r = -1 -r', with r' an inequality
2792  * of the tableau, then the inequality is adjacent (but opposite)
2793  * to the inequality r'.
2794  */
2795 static enum isl_ineq_type separation_type(struct isl_tab *tab, unsigned row)
2796 {
2797         int pos;
2798         unsigned off = 2 + tab->M;
2799
2800         if (tab->rational)
2801                 return isl_ineq_separate;
2802
2803         if (!isl_int_is_one(tab->mat->row[row][0]))
2804                 return isl_ineq_separate;
2805         if (!isl_int_is_negone(tab->mat->row[row][1]))
2806                 return isl_ineq_separate;
2807
2808         pos = isl_seq_first_non_zero(tab->mat->row[row] + off + tab->n_dead,
2809                                         tab->n_col - tab->n_dead);
2810         if (pos == -1)
2811                 return isl_ineq_adj_eq;
2812
2813         if (!isl_int_is_negone(tab->mat->row[row][off + tab->n_dead + pos]))
2814                 return isl_ineq_separate;
2815
2816         pos = isl_seq_first_non_zero(
2817                         tab->mat->row[row] + off + tab->n_dead + pos + 1,
2818                         tab->n_col - tab->n_dead - pos - 1);
2819
2820         return pos == -1 ? isl_ineq_adj_ineq : isl_ineq_separate;
2821 }
2822
2823 /* Check the effect of inequality "ineq" on the tableau "tab".
2824  * The result may be
2825  *      isl_ineq_redundant:     satisfied by all points in the tableau
2826  *      isl_ineq_separate:      satisfied by no point in the tableau
2827  *      isl_ineq_cut:           satisfied by some by not all points
2828  *      isl_ineq_adj_eq:        adjacent to an equality
2829  *      isl_ineq_adj_ineq:      adjacent to an inequality.
2830  */
2831 enum isl_ineq_type isl_tab_ineq_type(struct isl_tab *tab, isl_int *ineq)
2832 {
2833         enum isl_ineq_type type = isl_ineq_error;
2834         struct isl_tab_undo *snap = NULL;
2835         int con;
2836         int row;
2837
2838         if (!tab)
2839                 return isl_ineq_error;
2840
2841         if (isl_tab_extend_cons(tab, 1) < 0)
2842                 return isl_ineq_error;
2843
2844         snap = isl_tab_snap(tab);
2845
2846         con = isl_tab_add_row(tab, ineq);
2847         if (con < 0)
2848                 goto error;
2849
2850         row = tab->con[con].index;
2851         if (isl_tab_row_is_redundant(tab, row))
2852                 type = isl_ineq_redundant;
2853         else if (isl_int_is_neg(tab->mat->row[row][1]) &&
2854                  (tab->rational ||
2855                     isl_int_abs_ge(tab->mat->row[row][1],
2856                                    tab->mat->row[row][0]))) {
2857                 int nonneg = at_least_zero(tab, &tab->con[con]);
2858                 if (nonneg < 0)
2859                         goto error;
2860                 if (nonneg)
2861                         type = isl_ineq_cut;
2862                 else
2863                         type = separation_type(tab, row);
2864         } else {
2865                 int red = con_is_redundant(tab, &tab->con[con]);
2866                 if (red < 0)
2867                         goto error;
2868                 if (!red)
2869                         type = isl_ineq_cut;
2870                 else
2871                         type = isl_ineq_redundant;
2872         }
2873
2874         if (isl_tab_rollback(tab, snap))
2875                 return isl_ineq_error;
2876         return type;
2877 error:
2878         return isl_ineq_error;
2879 }
2880
2881 void isl_tab_dump(struct isl_tab *tab, FILE *out, int indent)
2882 {
2883         unsigned r, c;
2884         int i;
2885
2886         if (!tab) {
2887                 fprintf(out, "%*snull tab\n", indent, "");
2888                 return;
2889         }
2890         fprintf(out, "%*sn_redundant: %d, n_dead: %d", indent, "",
2891                 tab->n_redundant, tab->n_dead);
2892         if (tab->rational)
2893                 fprintf(out, ", rational");
2894         if (tab->empty)
2895                 fprintf(out, ", empty");
2896         fprintf(out, "\n");
2897         fprintf(out, "%*s[", indent, "");
2898         for (i = 0; i < tab->n_var; ++i) {
2899                 if (i)
2900                         fprintf(out, (i == tab->n_param ||
2901                                       i == tab->n_var - tab->n_div) ? "; "
2902                                                                     : ", ");
2903                 fprintf(out, "%c%d%s", tab->var[i].is_row ? 'r' : 'c',
2904                                         tab->var[i].index,
2905                                         tab->var[i].is_zero ? " [=0]" :
2906                                         tab->var[i].is_redundant ? " [R]" : "");
2907         }
2908         fprintf(out, "]\n");
2909         fprintf(out, "%*s[", indent, "");
2910         for (i = 0; i < tab->n_con; ++i) {
2911                 if (i)
2912                         fprintf(out, ", ");
2913                 fprintf(out, "%c%d%s", tab->con[i].is_row ? 'r' : 'c',
2914                                         tab->con[i].index,
2915                                         tab->con[i].is_zero ? " [=0]" :
2916                                         tab->con[i].is_redundant ? " [R]" : "");
2917         }
2918         fprintf(out, "]\n");
2919         fprintf(out, "%*s[", indent, "");
2920         for (i = 0; i < tab->n_row; ++i) {
2921                 const char *sign = "";
2922                 if (i)
2923                         fprintf(out, ", ");
2924                 if (tab->row_sign) {
2925                         if (tab->row_sign[i] == isl_tab_row_unknown)
2926                                 sign = "?";
2927                         else if (tab->row_sign[i] == isl_tab_row_neg)
2928                                 sign = "-";
2929                         else if (tab->row_sign[i] == isl_tab_row_pos)
2930                                 sign = "+";
2931                         else
2932                                 sign = "+-";
2933                 }
2934                 fprintf(out, "r%d: %d%s%s", i, tab->row_var[i],
2935                     isl_tab_var_from_row(tab, i)->is_nonneg ? " [>=0]" : "", sign);
2936         }
2937         fprintf(out, "]\n");
2938         fprintf(out, "%*s[", indent, "");
2939         for (i = 0; i < tab->n_col; ++i) {
2940                 if (i)
2941                         fprintf(out, ", ");
2942                 fprintf(out, "c%d: %d%s", i, tab->col_var[i],
2943                     var_from_col(tab, i)->is_nonneg ? " [>=0]" : "");
2944         }
2945         fprintf(out, "]\n");
2946         r = tab->mat->n_row;
2947         tab->mat->n_row = tab->n_row;
2948         c = tab->mat->n_col;
2949         tab->mat->n_col = 2 + tab->M + tab->n_col;
2950         isl_mat_dump(tab->mat, out, indent);
2951         tab->mat->n_row = r;
2952         tab->mat->n_col = c;
2953         if (tab->bset)
2954                 isl_basic_set_dump(tab->bset, out, indent);
2955 }