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