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