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