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