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