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