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