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