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