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