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