7244b6dbd1e80122750b09939543de7fe973a98f
[platform/upstream/isl.git] / isl_tab.c
1 #include "isl_mat.h"
2 #include "isl_map_private.h"
3 #include "isl_tab.h"
4
5 /*
6  * The implementation of tableaus in this file was inspired by Section 8
7  * of David Detlefs, Greg Nelson and James B. Saxe, "Simplify: a theorem
8  * prover for program checking".
9  */
10
11 struct isl_tab *isl_tab_alloc(struct isl_ctx *ctx,
12         unsigned n_row, unsigned n_var)
13 {
14         int i;
15         struct isl_tab *tab;
16
17         tab = isl_calloc_type(ctx, struct isl_tab);
18         if (!tab)
19                 return NULL;
20         tab->mat = isl_mat_alloc(ctx, n_row, 2 + n_var);
21         if (!tab->mat)
22                 goto error;
23         tab->var = isl_alloc_array(ctx, struct isl_tab_var, n_var);
24         if (!tab->var)
25                 goto error;
26         tab->con = isl_alloc_array(ctx, struct isl_tab_var, n_row);
27         if (!tab->con)
28                 goto error;
29         tab->col_var = isl_alloc_array(ctx, int, n_var);
30         if (!tab->col_var)
31                 goto error;
32         tab->row_var = isl_alloc_array(ctx, int, n_row);
33         if (!tab->row_var)
34                 goto error;
35         for (i = 0; i < n_var; ++i) {
36                 tab->var[i].index = i;
37                 tab->var[i].is_row = 0;
38                 tab->var[i].is_nonneg = 0;
39                 tab->var[i].is_zero = 0;
40                 tab->var[i].is_redundant = 0;
41                 tab->var[i].frozen = 0;
42                 tab->col_var[i] = i;
43         }
44         tab->n_row = 0;
45         tab->n_con = 0;
46         tab->n_eq = 0;
47         tab->max_con = n_row;
48         tab->n_col = n_var;
49         tab->n_var = n_var;
50         tab->n_dead = 0;
51         tab->n_redundant = 0;
52         tab->need_undo = 0;
53         tab->rational = 0;
54         tab->empty = 0;
55         tab->in_undo = 0;
56         tab->bottom.type = isl_tab_undo_bottom;
57         tab->bottom.next = NULL;
58         tab->top = &tab->bottom;
59         return tab;
60 error:
61         isl_tab_free(tab);
62         return NULL;
63 }
64
65 static int extend_cons(struct isl_tab *tab, unsigned n_new)
66 {
67         if (tab->max_con < tab->n_con + n_new) {
68                 struct isl_tab_var *con;
69
70                 con = isl_realloc_array(tab->mat->ctx, tab->con,
71                                     struct isl_tab_var, tab->max_con + n_new);
72                 if (!con)
73                         return -1;
74                 tab->con = con;
75                 tab->max_con += n_new;
76         }
77         if (tab->mat->n_row < tab->n_row + n_new) {
78                 int *row_var;
79
80                 tab->mat = isl_mat_extend(tab->mat,
81                                                 tab->n_row + n_new, tab->n_col);
82                 if (!tab->mat)
83                         return -1;
84                 row_var = isl_realloc_array(tab->mat->ctx, tab->row_var,
85                                             int, tab->mat->n_row);
86                 if (!row_var)
87                         return -1;
88                 tab->row_var = row_var;
89         }
90         return 0;
91 }
92
93 struct isl_tab *isl_tab_extend(struct isl_tab *tab, unsigned n_new)
94 {
95         if (extend_cons(tab, n_new) >= 0)
96                 return tab;
97
98         isl_tab_free(tab);
99         return NULL;
100 }
101
102 static void free_undo(struct isl_tab *tab)
103 {
104         struct isl_tab_undo *undo, *next;
105
106         for (undo = tab->top; undo && undo != &tab->bottom; undo = next) {
107                 next = undo->next;
108                 free(undo);
109         }
110         tab->top = undo;
111 }
112
113 void isl_tab_free(struct isl_tab *tab)
114 {
115         if (!tab)
116                 return;
117         free_undo(tab);
118         isl_mat_free(tab->mat);
119         isl_vec_free(tab->dual);
120         free(tab->var);
121         free(tab->con);
122         free(tab->row_var);
123         free(tab->col_var);
124         free(tab);
125 }
126
127 struct isl_tab *isl_tab_dup(struct isl_tab *tab)
128 {
129         int i;
130         struct isl_tab *dup;
131
132         if (!tab)
133                 return NULL;
134
135         dup = isl_calloc_type(tab->ctx, struct isl_tab);
136         if (!dup)
137                 return NULL;
138         dup->mat = isl_mat_dup(tab->mat);
139         if (!dup->mat)
140                 goto error;
141         dup->var = isl_alloc_array(tab->ctx, struct isl_tab_var, tab->n_var);
142         if (!dup->var)
143                 goto error;
144         for (i = 0; i < tab->n_var; ++i)
145                 dup->var[i] = tab->var[i];
146         dup->con = isl_alloc_array(tab->ctx, struct isl_tab_var, tab->max_con);
147         if (!dup->con)
148                 goto error;
149         for (i = 0; i < tab->n_con; ++i)
150                 dup->con[i] = tab->con[i];
151         dup->col_var = isl_alloc_array(tab->ctx, int, tab->mat->n_col);
152         if (!dup->col_var)
153                 goto error;
154         for (i = 0; i < tab->n_var; ++i)
155                 dup->col_var[i] = tab->col_var[i];
156         dup->row_var = isl_alloc_array(tab->ctx, int, tab->mat->n_row);
157         if (!dup->row_var)
158                 goto error;
159         for (i = 0; i < tab->n_row; ++i)
160                 dup->row_var[i] = tab->row_var[i];
161         dup->n_row = tab->n_row;
162         dup->n_con = tab->n_con;
163         dup->n_eq = tab->n_eq;
164         dup->max_con = tab->max_con;
165         dup->n_col = tab->n_col;
166         dup->n_var = tab->n_var;
167         dup->n_dead = tab->n_dead;
168         dup->n_redundant = tab->n_redundant;
169         dup->rational = tab->rational;
170         dup->empty = tab->empty;
171         dup->need_undo = 0;
172         dup->in_undo = 0;
173         dup->bottom.type = isl_tab_undo_bottom;
174         dup->bottom.next = NULL;
175         dup->top = &dup->bottom;
176         return dup;
177 error:
178         isl_tab_free(dup);
179         return NULL;
180 }
181
182 static struct isl_tab_var *var_from_index(struct isl_tab *tab, int i)
183 {
184         if (i >= 0)
185                 return &tab->var[i];
186         else
187                 return &tab->con[~i];
188 }
189
190 static struct isl_tab_var *var_from_row(struct isl_tab *tab, int i)
191 {
192         return var_from_index(tab, tab->row_var[i]);
193 }
194
195 static struct isl_tab_var *var_from_col(struct isl_tab *tab, int i)
196 {
197         return var_from_index(tab, tab->col_var[i]);
198 }
199
200 /* Check if there are any upper bounds on column variable "var",
201  * i.e., non-negative rows where var appears with a negative coefficient.
202  * Return 1 if there are no such bounds.
203  */
204 static int max_is_manifestly_unbounded(struct isl_tab *tab,
205         struct isl_tab_var *var)
206 {
207         int i;
208
209         if (var->is_row)
210                 return 0;
211         for (i = tab->n_redundant; i < tab->n_row; ++i) {
212                 if (!isl_int_is_neg(tab->mat->row[i][2 + var->index]))
213                         continue;
214                 if (var_from_row(tab, i)->is_nonneg)
215                         return 0;
216         }
217         return 1;
218 }
219
220 /* Check if there are any lower bounds on column variable "var",
221  * i.e., non-negative rows where var appears with a positive coefficient.
222  * Return 1 if there are no such bounds.
223  */
224 static int min_is_manifestly_unbounded(struct isl_tab *tab,
225         struct isl_tab_var *var)
226 {
227         int i;
228
229         if (var->is_row)
230                 return 0;
231         for (i = tab->n_redundant; i < tab->n_row; ++i) {
232                 if (!isl_int_is_pos(tab->mat->row[i][2 + var->index]))
233                         continue;
234                 if (var_from_row(tab, i)->is_nonneg)
235                         return 0;
236         }
237         return 1;
238 }
239
240 /* Given the index of a column "c", return the index of a row
241  * that can be used to pivot the column in, with either an increase
242  * (sgn > 0) or a decrease (sgn < 0) of the corresponding variable.
243  * If "var" is not NULL, then the row returned will be different from
244  * the one associated with "var".
245  *
246  * Each row in the tableau is of the form
247  *
248  *      x_r = a_r0 + \sum_i a_ri x_i
249  *
250  * Only rows with x_r >= 0 and with the sign of a_ri opposite to "sgn"
251  * impose any limit on the increase or decrease in the value of x_c
252  * and this bound is equal to a_r0 / |a_rc|.  We are therefore looking
253  * for the row with the smallest (most stringent) such bound.
254  * Note that the common denominator of each row drops out of the fraction.
255  * To check if row j has a smaller bound than row r, i.e.,
256  * a_j0 / |a_jc| < a_r0 / |a_rc| or a_j0 |a_rc| < a_r0 |a_jc|,
257  * we check if -sign(a_jc) (a_j0 a_rc - a_r0 a_jc) < 0,
258  * where -sign(a_jc) is equal to "sgn".
259  */
260 static int pivot_row(struct isl_tab *tab,
261         struct isl_tab_var *var, int sgn, int c)
262 {
263         int j, r, tsgn;
264         isl_int t;
265
266         isl_int_init(t);
267         r = -1;
268         for (j = tab->n_redundant; j < tab->n_row; ++j) {
269                 if (var && j == var->index)
270                         continue;
271                 if (!var_from_row(tab, j)->is_nonneg)
272                         continue;
273                 if (sgn * isl_int_sgn(tab->mat->row[j][2 + c]) >= 0)
274                         continue;
275                 if (r < 0) {
276                         r = j;
277                         continue;
278                 }
279                 isl_int_mul(t, tab->mat->row[r][1], tab->mat->row[j][2 + c]);
280                 isl_int_submul(t, tab->mat->row[j][1], tab->mat->row[r][2 + c]);
281                 tsgn = sgn * isl_int_sgn(t);
282                 if (tsgn < 0 || (tsgn == 0 &&
283                                             tab->row_var[j] < tab->row_var[r]))
284                         r = j;
285         }
286         isl_int_clear(t);
287         return r;
288 }
289
290 /* Find a pivot (row and col) that will increase (sgn > 0) or decrease
291  * (sgn < 0) the value of row variable var.
292  * If not NULL, then skip_var is a row variable that should be ignored
293  * while looking for a pivot row.  It is usually equal to var.
294  *
295  * As the given row in the tableau is of the form
296  *
297  *      x_r = a_r0 + \sum_i a_ri x_i
298  *
299  * we need to find a column such that the sign of a_ri is equal to "sgn"
300  * (such that an increase in x_i will have the desired effect) or a
301  * column with a variable that may attain negative values.
302  * If a_ri is positive, then we need to move x_i in the same direction
303  * to obtain the desired effect.  Otherwise, x_i has to move in the
304  * opposite direction.
305  */
306 static void find_pivot(struct isl_tab *tab,
307         struct isl_tab_var *var, struct isl_tab_var *skip_var,
308         int sgn, int *row, int *col)
309 {
310         int j, r, c;
311         isl_int *tr;
312
313         *row = *col = -1;
314
315         isl_assert(tab->mat->ctx, var->is_row, return);
316         tr = tab->mat->row[var->index];
317
318         c = -1;
319         for (j = tab->n_dead; j < tab->n_col; ++j) {
320                 if (isl_int_is_zero(tr[2 + j]))
321                         continue;
322                 if (isl_int_sgn(tr[2 + j]) != sgn &&
323                     var_from_col(tab, j)->is_nonneg)
324                         continue;
325                 if (c < 0 || tab->col_var[j] < tab->col_var[c])
326                         c = j;
327         }
328         if (c < 0)
329                 return;
330
331         sgn *= isl_int_sgn(tr[2 + c]);
332         r = pivot_row(tab, skip_var, sgn, c);
333         *row = r < 0 ? var->index : r;
334         *col = c;
335 }
336
337 /* Return 1 if row "row" represents an obviously redundant inequality.
338  * This means
339  *      - it represents an inequality or a variable
340  *      - that is the sum of a non-negative sample value and a positive
341  *        combination of zero or more non-negative variables.
342  */
343 static int is_redundant(struct isl_tab *tab, int row)
344 {
345         int i;
346
347         if (tab->row_var[row] < 0 && !var_from_row(tab, row)->is_nonneg)
348                 return 0;
349
350         if (isl_int_is_neg(tab->mat->row[row][1]))
351                 return 0;
352
353         for (i = tab->n_dead; i < tab->n_col; ++i) {
354                 if (isl_int_is_zero(tab->mat->row[row][2 + i]))
355                         continue;
356                 if (isl_int_is_neg(tab->mat->row[row][2 + i]))
357                         return 0;
358                 if (!var_from_col(tab, i)->is_nonneg)
359                         return 0;
360         }
361         return 1;
362 }
363
364 static void swap_rows(struct isl_tab *tab, int row1, int row2)
365 {
366         int t;
367         t = tab->row_var[row1];
368         tab->row_var[row1] = tab->row_var[row2];
369         tab->row_var[row2] = t;
370         var_from_row(tab, row1)->index = row1;
371         var_from_row(tab, row2)->index = row2;
372         tab->mat = isl_mat_swap_rows(tab->mat, row1, row2);
373 }
374
375 static void push(struct isl_tab *tab,
376         enum isl_tab_undo_type type, struct isl_tab_var *var)
377 {
378         struct isl_tab_undo *undo;
379
380         if (!tab->need_undo)
381                 return;
382
383         undo = isl_alloc_type(tab->mat->ctx, struct isl_tab_undo);
384         if (!undo) {
385                 free_undo(tab);
386                 tab->top = NULL;
387                 return;
388         }
389         undo->type = type;
390         if (!var)
391                 undo->var_index = 0;
392         else if (var->is_row)
393                 undo->var_index = tab->row_var[var->index];
394         else
395                 undo->var_index = tab->col_var[var->index];
396         undo->next = tab->top;
397         tab->top = undo;
398 }
399
400 /* Mark row with index "row" as being redundant.
401  * If we may need to undo the operation or if the row represents
402  * a variable of the original problem, the row is kept,
403  * but no longer considered when looking for a pivot row.
404  * Otherwise, the row is simply removed.
405  *
406  * The row may be interchanged with some other row.  If it
407  * is interchanged with a later row, return 1.  Otherwise return 0.
408  * If the rows are checked in order in the calling function,
409  * then a return value of 1 means that the row with the given
410  * row number may now contain a different row that hasn't been checked yet.
411  */
412 static int mark_redundant(struct isl_tab *tab, int row)
413 {
414         struct isl_tab_var *var = var_from_row(tab, row);
415         var->is_redundant = 1;
416         isl_assert(tab->mat->ctx, row >= tab->n_redundant, return);
417         if (tab->need_undo || tab->row_var[row] >= 0) {
418                 if (tab->row_var[row] >= 0 && !var->is_nonneg) {
419                         var->is_nonneg = 1;
420                         push(tab, isl_tab_undo_nonneg, var);
421                 }
422                 if (row != tab->n_redundant)
423                         swap_rows(tab, row, tab->n_redundant);
424                 push(tab, isl_tab_undo_redundant, var);
425                 tab->n_redundant++;
426                 return 0;
427         } else {
428                 if (row != tab->n_row - 1)
429                         swap_rows(tab, row, tab->n_row - 1);
430                 var_from_row(tab, tab->n_row - 1)->index = -1;
431                 tab->n_row--;
432                 return 1;
433         }
434 }
435
436 static struct isl_tab *mark_empty(struct isl_tab *tab)
437 {
438         if (!tab->empty && tab->need_undo)
439                 push(tab, isl_tab_undo_empty, NULL);
440         tab->empty = 1;
441         return tab;
442 }
443
444 /* Given a row number "row" and a column number "col", pivot the tableau
445  * such that the associated variables are interchanged.
446  * The given row in the tableau expresses
447  *
448  *      x_r = a_r0 + \sum_i a_ri x_i
449  *
450  * or
451  *
452  *      x_c = 1/a_rc x_r - a_r0/a_rc + sum_{i \ne r} -a_ri/a_rc
453  *
454  * Substituting this equality into the other rows
455  *
456  *      x_j = a_j0 + \sum_i a_ji x_i
457  *
458  * with a_jc \ne 0, we obtain
459  *
460  *      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 
461  *
462  * The tableau
463  *
464  *      n_rc/d_r                n_ri/d_r
465  *      n_jc/d_j                n_ji/d_j
466  *
467  * where i is any other column and j is any other row,
468  * is therefore transformed into
469  *
470  * s(n_rc)d_r/|n_rc|            -s(n_rc)n_ri/|n_rc|
471  * 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)
472  *
473  * The transformation is performed along the following steps
474  *
475  *      d_r/n_rc                n_ri/n_rc
476  *      n_jc/d_j                n_ji/d_j
477  *
478  *      s(n_rc)d_r/|n_rc|       -s(n_rc)n_ri/|n_rc|
479  *      n_jc/d_j                n_ji/d_j
480  *
481  *      s(n_rc)d_r/|n_rc|       -s(n_rc)n_ri/|n_rc|
482  *      n_jc/(|n_rc| d_j)       n_ji/(|n_rc| d_j)
483  *
484  *      s(n_rc)d_r/|n_rc|       -s(n_rc)n_ri/|n_rc|
485  *      n_jc/(|n_rc| d_j)       (n_ji |n_rc|)/(|n_rc| d_j)
486  *
487  *      s(n_rc)d_r/|n_rc|       -s(n_rc)n_ri/|n_rc|
488  *      n_jc/(|n_rc| d_j)       (n_ji |n_rc| - s(n_rc)n_jc n_ri)/(|n_rc| d_j)
489  *
490  * s(n_rc)d_r/|n_rc|            -s(n_rc)n_ri/|n_rc|
491  * 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)
492  *
493  */
494 static void pivot(struct isl_tab *tab, int row, int col)
495 {
496         int i, j;
497         int sgn;
498         int t;
499         struct isl_mat *mat = tab->mat;
500         struct isl_tab_var *var;
501
502         isl_int_swap(mat->row[row][0], mat->row[row][2 + col]);
503         sgn = isl_int_sgn(mat->row[row][0]);
504         if (sgn < 0) {
505                 isl_int_neg(mat->row[row][0], mat->row[row][0]);
506                 isl_int_neg(mat->row[row][2 + col], mat->row[row][2 + col]);
507         } else
508                 for (j = 0; j < 1 + tab->n_col; ++j) {
509                         if (j == 1 + col)
510                                 continue;
511                         isl_int_neg(mat->row[row][1 + j], mat->row[row][1 + j]);
512                 }
513         if (!isl_int_is_one(mat->row[row][0]))
514                 isl_seq_normalize(mat->row[row], 2 + tab->n_col);
515         for (i = 0; i < tab->n_row; ++i) {
516                 if (i == row)
517                         continue;
518                 if (isl_int_is_zero(mat->row[i][2 + col]))
519                         continue;
520                 isl_int_mul(mat->row[i][0], mat->row[i][0], mat->row[row][0]);
521                 for (j = 0; j < 1 + tab->n_col; ++j) {
522                         if (j == 1 + col)
523                                 continue;
524                         isl_int_mul(mat->row[i][1 + j],
525                                     mat->row[i][1 + j], mat->row[row][0]);
526                         isl_int_addmul(mat->row[i][1 + j],
527                                     mat->row[i][2 + col], mat->row[row][1 + j]);
528                 }
529                 isl_int_mul(mat->row[i][2 + col],
530                             mat->row[i][2 + col], mat->row[row][2 + col]);
531                 if (!isl_int_is_one(mat->row[i][0]))
532                         isl_seq_normalize(mat->row[i], 2 + tab->n_col);
533         }
534         t = tab->row_var[row];
535         tab->row_var[row] = tab->col_var[col];
536         tab->col_var[col] = t;
537         var = var_from_row(tab, row);
538         var->is_row = 1;
539         var->index = row;
540         var = var_from_col(tab, col);
541         var->is_row = 0;
542         var->index = col;
543         if (tab->in_undo)
544                 return;
545         for (i = tab->n_redundant; i < tab->n_row; ++i) {
546                 if (isl_int_is_zero(mat->row[i][2 + col]))
547                         continue;
548                 if (!var_from_row(tab, i)->frozen &&
549                     is_redundant(tab, i))
550                         if (mark_redundant(tab, i))
551                                 --i;
552         }
553 }
554
555 /* If "var" represents a column variable, then pivot is up (sgn > 0)
556  * or down (sgn < 0) to a row.  The variable is assumed not to be
557  * unbounded in the specified direction.
558  * If sgn = 0, then the variable is unbounded in both directions,
559  * and we pivot with any row we can find.
560  */
561 static void to_row(struct isl_tab *tab, struct isl_tab_var *var, int sign)
562 {
563         int r;
564
565         if (var->is_row)
566                 return;
567
568         if (sign == 0) {
569                 for (r = tab->n_redundant; r < tab->n_row; ++r)
570                         if (!isl_int_is_zero(tab->mat->row[r][2 + var->index]))
571                                 break;
572                 isl_assert(tab->mat->ctx, r < tab->n_row, return);
573         } else {
574                 r = pivot_row(tab, NULL, sign, var->index);
575                 isl_assert(tab->mat->ctx, r >= 0, return);
576         }
577
578         pivot(tab, r, var->index);
579 }
580
581 static void check_table(struct isl_tab *tab)
582 {
583         int i;
584
585         if (tab->empty)
586                 return;
587         for (i = 0; i < tab->n_row; ++i) {
588                 if (!var_from_row(tab, i)->is_nonneg)
589                         continue;
590                 assert(!isl_int_is_neg(tab->mat->row[i][1]));
591         }
592 }
593
594 /* Return the sign of the maximal value of "var".
595  * If the sign is not negative, then on return from this function,
596  * the sample value will also be non-negative.
597  *
598  * If "var" is manifestly unbounded wrt positive values, we are done.
599  * Otherwise, we pivot the variable up to a row if needed
600  * Then we continue pivoting down until either
601  *      - no more down pivots can be performed
602  *      - the sample value is positive
603  *      - the variable is pivoted into a manifestly unbounded column
604  */
605 static int sign_of_max(struct isl_tab *tab, struct isl_tab_var *var)
606 {
607         int row, col;
608
609         if (max_is_manifestly_unbounded(tab, var))
610                 return 1;
611         to_row(tab, var, 1);
612         while (!isl_int_is_pos(tab->mat->row[var->index][1])) {
613                 find_pivot(tab, var, var, 1, &row, &col);
614                 if (row == -1)
615                         return isl_int_sgn(tab->mat->row[var->index][1]);
616                 pivot(tab, row, col);
617                 if (!var->is_row) /* manifestly unbounded */
618                         return 1;
619         }
620         return 1;
621 }
622
623 /* Perform pivots until the row variable "var" has a non-negative
624  * sample value or until no more upward pivots can be performed.
625  * Return the sign of the sample value after the pivots have been
626  * performed.
627  */
628 static int restore_row(struct isl_tab *tab, struct isl_tab_var *var)
629 {
630         int row, col;
631
632         while (isl_int_is_neg(tab->mat->row[var->index][1])) {
633                 find_pivot(tab, var, var, 1, &row, &col);
634                 if (row == -1)
635                         break;
636                 pivot(tab, row, col);
637                 if (!var->is_row) /* manifestly unbounded */
638                         return 1;
639         }
640         return isl_int_sgn(tab->mat->row[var->index][1]);
641 }
642
643 /* Perform pivots until we are sure that the row variable "var"
644  * can attain non-negative values.  After return from this
645  * function, "var" is still a row variable, but its sample
646  * value may not be non-negative, even if the function returns 1.
647  */
648 static int at_least_zero(struct isl_tab *tab, struct isl_tab_var *var)
649 {
650         int row, col;
651
652         while (isl_int_is_neg(tab->mat->row[var->index][1])) {
653                 find_pivot(tab, var, var, 1, &row, &col);
654                 if (row == -1)
655                         break;
656                 if (row == var->index) /* manifestly unbounded */
657                         return 1;
658                 pivot(tab, row, col);
659         }
660         return !isl_int_is_neg(tab->mat->row[var->index][1]);
661 }
662
663 /* Return a negative value if "var" can attain negative values.
664  * Return a non-negative value otherwise.
665  *
666  * If "var" is manifestly unbounded wrt negative values, we are done.
667  * Otherwise, if var is in a column, we can pivot it down to a row.
668  * Then we continue pivoting down until either
669  *      - the pivot would result in a manifestly unbounded column
670  *        => we don't perform the pivot, but simply return -1
671  *      - no more down pivots can be performed
672  *      - the sample value is negative
673  * If the sample value becomes negative and the variable is supposed
674  * to be nonnegative, then we undo the last pivot.
675  * However, if the last pivot has made the pivoting variable
676  * obviously redundant, then it may have moved to another row.
677  * In that case we look for upward pivots until we reach a non-negative
678  * value again.
679  */
680 static int sign_of_min(struct isl_tab *tab, struct isl_tab_var *var)
681 {
682         int row, col;
683         struct isl_tab_var *pivot_var;
684
685         if (min_is_manifestly_unbounded(tab, var))
686                 return -1;
687         if (!var->is_row) {
688                 col = var->index;
689                 row = pivot_row(tab, NULL, -1, col);
690                 pivot_var = var_from_col(tab, col);
691                 pivot(tab, row, col);
692                 if (var->is_redundant)
693                         return 0;
694                 if (isl_int_is_neg(tab->mat->row[var->index][1])) {
695                         if (var->is_nonneg) {
696                                 if (!pivot_var->is_redundant &&
697                                     pivot_var->index == row)
698                                         pivot(tab, row, col);
699                                 else
700                                         restore_row(tab, var);
701                         }
702                         return -1;
703                 }
704         }
705         if (var->is_redundant)
706                 return 0;
707         while (!isl_int_is_neg(tab->mat->row[var->index][1])) {
708                 find_pivot(tab, var, var, -1, &row, &col);
709                 if (row == var->index)
710                         return -1;
711                 if (row == -1)
712                         return isl_int_sgn(tab->mat->row[var->index][1]);
713                 pivot_var = var_from_col(tab, col);
714                 pivot(tab, row, col);
715                 if (var->is_redundant)
716                         return 0;
717         }
718         if (var->is_nonneg) {
719                 /* pivot back to non-negative value */
720                 if (!pivot_var->is_redundant && pivot_var->index == row)
721                         pivot(tab, row, col);
722                 else
723                         restore_row(tab, var);
724         }
725         return -1;
726 }
727
728 /* Return 1 if "var" can attain values <= -1.
729  * Return 0 otherwise.
730  *
731  * The sample value of "var" is assumed to be non-negative when the
732  * the function is called and will be made non-negative again before
733  * the function returns.
734  */
735 static int min_at_most_neg_one(struct isl_tab *tab, struct isl_tab_var *var)
736 {
737         int row, col;
738         struct isl_tab_var *pivot_var;
739
740         if (min_is_manifestly_unbounded(tab, var))
741                 return 1;
742         if (!var->is_row) {
743                 col = var->index;
744                 row = pivot_row(tab, NULL, -1, col);
745                 pivot_var = var_from_col(tab, col);
746                 pivot(tab, row, col);
747                 if (var->is_redundant)
748                         return 0;
749                 if (isl_int_is_neg(tab->mat->row[var->index][1]) &&
750                     isl_int_abs_ge(tab->mat->row[var->index][1],
751                                    tab->mat->row[var->index][0])) {
752                         if (var->is_nonneg) {
753                                 if (!pivot_var->is_redundant &&
754                                     pivot_var->index == row)
755                                         pivot(tab, row, col);
756                                 else
757                                         restore_row(tab, var);
758                         }
759                         return 1;
760                 }
761         }
762         if (var->is_redundant)
763                 return 0;
764         do {
765                 find_pivot(tab, var, var, -1, &row, &col);
766                 if (row == var->index)
767                         return 1;
768                 if (row == -1)
769                         return 0;
770                 pivot_var = var_from_col(tab, col);
771                 pivot(tab, row, col);
772                 if (var->is_redundant)
773                         return 0;
774         } while (!isl_int_is_neg(tab->mat->row[var->index][1]) ||
775                  isl_int_abs_lt(tab->mat->row[var->index][1],
776                                 tab->mat->row[var->index][0]));
777         if (var->is_nonneg) {
778                 /* pivot back to non-negative value */
779                 if (!pivot_var->is_redundant && pivot_var->index == row)
780                         pivot(tab, row, col);
781                 restore_row(tab, var);
782         }
783         return 1;
784 }
785
786 /* Return 1 if "var" can attain values >= 1.
787  * Return 0 otherwise.
788  */
789 static int at_least_one(struct isl_tab *tab, struct isl_tab_var *var)
790 {
791         int row, col;
792         isl_int *r;
793
794         if (max_is_manifestly_unbounded(tab, var))
795                 return 1;
796         to_row(tab, var, 1);
797         r = tab->mat->row[var->index];
798         while (isl_int_lt(r[1], r[0])) {
799                 find_pivot(tab, var, var, 1, &row, &col);
800                 if (row == -1)
801                         return isl_int_ge(r[1], r[0]);
802                 if (row == var->index) /* manifestly unbounded */
803                         return 1;
804                 pivot(tab, row, col);
805         }
806         return 1;
807 }
808
809 static void swap_cols(struct isl_tab *tab, int col1, int col2)
810 {
811         int t;
812         t = tab->col_var[col1];
813         tab->col_var[col1] = tab->col_var[col2];
814         tab->col_var[col2] = t;
815         var_from_col(tab, col1)->index = col1;
816         var_from_col(tab, col2)->index = col2;
817         tab->mat = isl_mat_swap_cols(tab->mat, 2 + col1, 2 + col2);
818 }
819
820 /* Mark column with index "col" as representing a zero variable.
821  * If we may need to undo the operation the column is kept,
822  * but no longer considered.
823  * Otherwise, the column is simply removed.
824  *
825  * The column may be interchanged with some other column.  If it
826  * is interchanged with a later column, return 1.  Otherwise return 0.
827  * If the columns are checked in order in the calling function,
828  * then a return value of 1 means that the column with the given
829  * column number may now contain a different column that
830  * hasn't been checked yet.
831  */
832 static int kill_col(struct isl_tab *tab, int col)
833 {
834         var_from_col(tab, col)->is_zero = 1;
835         if (tab->need_undo) {
836                 push(tab, isl_tab_undo_zero, var_from_col(tab, col));
837                 if (col != tab->n_dead)
838                         swap_cols(tab, col, tab->n_dead);
839                 tab->n_dead++;
840                 return 0;
841         } else {
842                 if (col != tab->n_col - 1)
843                         swap_cols(tab, col, tab->n_col - 1);
844                 var_from_col(tab, tab->n_col - 1)->index = -1;
845                 tab->n_col--;
846                 return 1;
847         }
848 }
849
850 /* Row variable "var" is non-negative and cannot attain any values
851  * larger than zero.  This means that the coefficients of the unrestricted
852  * column variables are zero and that the coefficients of the non-negative
853  * column variables are zero or negative.
854  * Each of the non-negative variables with a negative coefficient can
855  * then also be written as the negative sum of non-negative variables
856  * and must therefore also be zero.
857  */
858 static void close_row(struct isl_tab *tab, struct isl_tab_var *var)
859 {
860         int j;
861         struct isl_mat *mat = tab->mat;
862
863         isl_assert(tab->mat->ctx, var->is_nonneg, return);
864         var->is_zero = 1;
865         for (j = tab->n_dead; j < tab->n_col; ++j) {
866                 if (isl_int_is_zero(mat->row[var->index][2 + j]))
867                         continue;
868                 isl_assert(tab->mat->ctx,
869                         isl_int_is_neg(mat->row[var->index][2 + j]), return);
870                 if (kill_col(tab, j))
871                         --j;
872         }
873         mark_redundant(tab, var->index);
874 }
875
876 /* Add a constraint to the tableau and allocate a row for it.
877  * Return the index into the constraint array "con".
878  */
879 static int allocate_con(struct isl_tab *tab)
880 {
881         int r;
882
883         isl_assert(tab->mat->ctx, tab->n_row < tab->mat->n_row, return -1);
884
885         r = tab->n_con;
886         tab->con[r].index = tab->n_row;
887         tab->con[r].is_row = 1;
888         tab->con[r].is_nonneg = 0;
889         tab->con[r].is_zero = 0;
890         tab->con[r].is_redundant = 0;
891         tab->con[r].frozen = 0;
892         tab->row_var[tab->n_row] = ~r;
893
894         tab->n_row++;
895         tab->n_con++;
896         push(tab, isl_tab_undo_allocate, &tab->con[r]);
897
898         return r;
899 }
900
901 /* Add a row to the tableau.  The row is given as an affine combination
902  * of the original variables and needs to be expressed in terms of the
903  * column variables.
904  *
905  * We add each term in turn.
906  * If r = n/d_r is the current sum and we need to add k x, then
907  *      if x is a column variable, we increase the numerator of
908  *              this column by k d_r
909  *      if x = f/d_x is a row variable, then the new representation of r is
910  *
911  *               n    k f   d_x/g n + d_r/g k f   m/d_r n + m/d_g k f
912  *              --- + --- = ------------------- = -------------------
913  *              d_r   d_r        d_r d_x/g                m
914  *
915  *      with g the gcd of d_r and d_x and m the lcm of d_r and d_x.
916  */
917 static int add_row(struct isl_tab *tab, isl_int *line)
918 {
919         int i;
920         int r;
921         isl_int *row;
922         isl_int a, b;
923
924         r = allocate_con(tab);
925         if (r < 0)
926                 return -1;
927
928         isl_int_init(a);
929         isl_int_init(b);
930         row = tab->mat->row[tab->con[r].index];
931         isl_int_set_si(row[0], 1);
932         isl_int_set(row[1], line[0]);
933         isl_seq_clr(row + 2, tab->n_col);
934         for (i = 0; i < tab->n_var; ++i) {
935                 if (tab->var[i].is_zero)
936                         continue;
937                 if (tab->var[i].is_row) {
938                         isl_int_lcm(a,
939                                 row[0], tab->mat->row[tab->var[i].index][0]);
940                         isl_int_swap(a, row[0]);
941                         isl_int_divexact(a, row[0], a);
942                         isl_int_divexact(b,
943                                 row[0], tab->mat->row[tab->var[i].index][0]);
944                         isl_int_mul(b, b, line[1 + i]);
945                         isl_seq_combine(row + 1, a, row + 1,
946                             b, tab->mat->row[tab->var[i].index] + 1,
947                             1 + tab->n_col);
948                 } else
949                         isl_int_addmul(row[2 + tab->var[i].index],
950                                                         line[1 + i], row[0]);
951         }
952         isl_seq_normalize(row, 2 + tab->n_col);
953         isl_int_clear(a);
954         isl_int_clear(b);
955
956         return r;
957 }
958
959 static int drop_row(struct isl_tab *tab, int row)
960 {
961         isl_assert(tab->mat->ctx, ~tab->row_var[row] == tab->n_con - 1, return -1);
962         if (row != tab->n_row - 1)
963                 swap_rows(tab, row, tab->n_row - 1);
964         tab->n_row--;
965         tab->n_con--;
966         return 0;
967 }
968
969 /* Add inequality "ineq" and check if it conflicts with the
970  * previously added constraints or if it is obviously redundant.
971  */
972 struct isl_tab *isl_tab_add_ineq(struct isl_tab *tab, isl_int *ineq)
973 {
974         int r;
975         int sgn;
976
977         if (!tab)
978                 return NULL;
979         r = add_row(tab, ineq);
980         if (r < 0)
981                 goto error;
982         tab->con[r].is_nonneg = 1;
983         push(tab, isl_tab_undo_nonneg, &tab->con[r]);
984         if (is_redundant(tab, tab->con[r].index)) {
985                 mark_redundant(tab, tab->con[r].index);
986                 return tab;
987         }
988
989         sgn = restore_row(tab, &tab->con[r]);
990         if (sgn < 0)
991                 return mark_empty(tab);
992         if (tab->con[r].is_row && is_redundant(tab, tab->con[r].index))
993                 mark_redundant(tab, tab->con[r].index);
994         return tab;
995 error:
996         isl_tab_free(tab);
997         return NULL;
998 }
999
1000 /* Pivot a non-negative variable down until it reaches the value zero
1001  * and then pivot the variable into a column position.
1002  */
1003 static int to_col(struct isl_tab *tab, struct isl_tab_var *var)
1004 {
1005         int i;
1006         int row, col;
1007
1008         if (!var->is_row)
1009                 return;
1010
1011         while (isl_int_is_pos(tab->mat->row[var->index][1])) {
1012                 find_pivot(tab, var, NULL, -1, &row, &col);
1013                 isl_assert(tab->mat->ctx, row != -1, return -1);
1014                 pivot(tab, row, col);
1015                 if (!var->is_row)
1016                         return;
1017         }
1018
1019         for (i = tab->n_dead; i < tab->n_col; ++i)
1020                 if (!isl_int_is_zero(tab->mat->row[var->index][2 + i]))
1021                         break;
1022
1023         isl_assert(tab->mat->ctx, i < tab->n_col, return -1);
1024         pivot(tab, var->index, i);
1025
1026         return 0;
1027 }
1028
1029 /* We assume Gaussian elimination has been performed on the equalities.
1030  * The equalities can therefore never conflict.
1031  * Adding the equalities is currently only really useful for a later call
1032  * to isl_tab_ineq_type.
1033  */
1034 static struct isl_tab *add_eq(struct isl_tab *tab, isl_int *eq)
1035 {
1036         int i;
1037         int r;
1038
1039         if (!tab)
1040                 return NULL;
1041         r = add_row(tab, eq);
1042         if (r < 0)
1043                 goto error;
1044
1045         r = tab->con[r].index;
1046         i = isl_seq_first_non_zero(tab->mat->row[r] + 2 + tab->n_dead,
1047                                         tab->n_col - tab->n_dead);
1048         isl_assert(tab->mat->ctx, i >= 0, goto error);
1049         i += tab->n_dead;
1050         pivot(tab, r, i);
1051         kill_col(tab, i);
1052         tab->n_eq++;
1053
1054         return tab;
1055 error:
1056         isl_tab_free(tab);
1057         return NULL;
1058 }
1059
1060 /* Add an equality that is known to be valid for the given tableau.
1061  */
1062 struct isl_tab *isl_tab_add_valid_eq(struct isl_tab *tab, isl_int *eq)
1063 {
1064         struct isl_tab_var *var;
1065         int i;
1066         int r;
1067
1068         if (!tab)
1069                 return NULL;
1070         r = add_row(tab, eq);
1071         if (r < 0)
1072                 goto error;
1073
1074         var = &tab->con[r];
1075         r = var->index;
1076         if (isl_int_is_neg(tab->mat->row[r][1]))
1077                 isl_seq_neg(tab->mat->row[r] + 1, tab->mat->row[r] + 1,
1078                             1 + tab->n_col);
1079         var->is_nonneg = 1;
1080         if (to_col(tab, var) < 0)
1081                 goto error;
1082         var->is_nonneg = 0;
1083         kill_col(tab, var->index);
1084
1085         return tab;
1086 error:
1087         isl_tab_free(tab);
1088         return NULL;
1089 }
1090
1091 struct isl_tab *isl_tab_from_basic_map(struct isl_basic_map *bmap)
1092 {
1093         int i;
1094         struct isl_tab *tab;
1095
1096         if (!bmap)
1097                 return NULL;
1098         tab = isl_tab_alloc(bmap->ctx,
1099                             isl_basic_map_total_dim(bmap) + bmap->n_ineq + 1,
1100                             isl_basic_map_total_dim(bmap));
1101         if (!tab)
1102                 return NULL;
1103         tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
1104         if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1105                 return mark_empty(tab);
1106         for (i = 0; i < bmap->n_eq; ++i) {
1107                 tab = add_eq(tab, bmap->eq[i]);
1108                 if (!tab)
1109                         return tab;
1110         }
1111         for (i = 0; i < bmap->n_ineq; ++i) {
1112                 tab = isl_tab_add_ineq(tab, bmap->ineq[i]);
1113                 if (!tab || tab->empty)
1114                         return tab;
1115         }
1116         return tab;
1117 }
1118
1119 struct isl_tab *isl_tab_from_basic_set(struct isl_basic_set *bset)
1120 {
1121         return isl_tab_from_basic_map((struct isl_basic_map *)bset);
1122 }
1123
1124 /* Construct a tableau corresponding to the recession cone of "bmap".
1125  */
1126 struct isl_tab *isl_tab_from_recession_cone(struct isl_basic_map *bmap)
1127 {
1128         isl_int cst;
1129         int i;
1130         struct isl_tab *tab;
1131
1132         if (!bmap)
1133                 return NULL;
1134         tab = isl_tab_alloc(bmap->ctx, bmap->n_eq + bmap->n_ineq,
1135                                 isl_basic_map_total_dim(bmap));
1136         if (!tab)
1137                 return NULL;
1138         tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
1139
1140         isl_int_init(cst);
1141         for (i = 0; i < bmap->n_eq; ++i) {
1142                 isl_int_swap(bmap->eq[i][0], cst);
1143                 tab = add_eq(tab, bmap->eq[i]);
1144                 isl_int_swap(bmap->eq[i][0], cst);
1145                 if (!tab)
1146                         goto done;
1147         }
1148         for (i = 0; i < bmap->n_ineq; ++i) {
1149                 int r;
1150                 isl_int_swap(bmap->ineq[i][0], cst);
1151                 r = add_row(tab, bmap->ineq[i]);
1152                 isl_int_swap(bmap->ineq[i][0], cst);
1153                 if (r < 0)
1154                         goto error;
1155                 tab->con[r].is_nonneg = 1;
1156                 push(tab, isl_tab_undo_nonneg, &tab->con[r]);
1157         }
1158 done:
1159         isl_int_clear(cst);
1160         return tab;
1161 error:
1162         isl_int_clear(cst);
1163         isl_tab_free(tab);
1164         return NULL;
1165 }
1166
1167 /* Assuming "tab" is the tableau of a cone, check if the cone is
1168  * bounded, i.e., if it is empty or only contains the origin.
1169  */
1170 int isl_tab_cone_is_bounded(struct isl_tab *tab)
1171 {
1172         int i;
1173
1174         if (!tab)
1175                 return -1;
1176         if (tab->empty)
1177                 return 1;
1178         if (tab->n_dead == tab->n_col)
1179                 return 1;
1180
1181         for (;;) {
1182                 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1183                         struct isl_tab_var *var;
1184                         var = var_from_row(tab, i);
1185                         if (!var->is_nonneg)
1186                                 continue;
1187                         if (sign_of_max(tab, var) != 0)
1188                                 return 0;
1189                         close_row(tab, var);
1190                         break;
1191                 }
1192                 if (tab->n_dead == tab->n_col)
1193                         return 1;
1194                 if (i == tab->n_row)
1195                         return 0;
1196         }
1197 }
1198
1199 int isl_tab_sample_is_integer(struct isl_tab *tab)
1200 {
1201         int i;
1202
1203         if (!tab)
1204                 return -1;
1205
1206         for (i = 0; i < tab->n_var; ++i) {
1207                 int row;
1208                 if (!tab->var[i].is_row)
1209                         continue;
1210                 row = tab->var[i].index;
1211                 if (!isl_int_is_divisible_by(tab->mat->row[row][1],
1212                                                 tab->mat->row[row][0]))
1213                         return 0;
1214         }
1215         return 1;
1216 }
1217
1218 static struct isl_vec *extract_integer_sample(struct isl_tab *tab)
1219 {
1220         int i;
1221         struct isl_vec *vec;
1222
1223         vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
1224         if (!vec)
1225                 return NULL;
1226
1227         isl_int_set_si(vec->block.data[0], 1);
1228         for (i = 0; i < tab->n_var; ++i) {
1229                 if (!tab->var[i].is_row)
1230                         isl_int_set_si(vec->block.data[1 + i], 0);
1231                 else {
1232                         int row = tab->var[i].index;
1233                         isl_int_divexact(vec->block.data[1 + i],
1234                                 tab->mat->row[row][1], tab->mat->row[row][0]);
1235                 }
1236         }
1237
1238         return vec;
1239 }
1240
1241 struct isl_vec *isl_tab_get_sample_value(struct isl_tab *tab)
1242 {
1243         int i;
1244         struct isl_vec *vec;
1245         isl_int m;
1246
1247         if (!tab)
1248                 return NULL;
1249
1250         vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
1251         if (!vec)
1252                 return NULL;
1253
1254         isl_int_init(m);
1255
1256         isl_int_set_si(vec->block.data[0], 1);
1257         for (i = 0; i < tab->n_var; ++i) {
1258                 int row;
1259                 if (!tab->var[i].is_row) {
1260                         isl_int_set_si(vec->block.data[1 + i], 0);
1261                         continue;
1262                 }
1263                 row = tab->var[i].index;
1264                 isl_int_gcd(m, vec->block.data[0], tab->mat->row[row][0]);
1265                 isl_int_divexact(m, tab->mat->row[row][0], m);
1266                 isl_seq_scale(vec->block.data, vec->block.data, m, 1 + i);
1267                 isl_int_divexact(m, vec->block.data[0], tab->mat->row[row][0]);
1268                 isl_int_mul(vec->block.data[1 + i], m, tab->mat->row[row][1]);
1269         }
1270         isl_seq_normalize(vec->block.data, vec->size);
1271
1272         isl_int_clear(m);
1273         return vec;
1274 }
1275
1276 /* Update "bmap" based on the results of the tableau "tab".
1277  * In particular, implicit equalities are made explicit, redundant constraints
1278  * are removed and if the sample value happens to be integer, it is stored
1279  * in "bmap" (unless "bmap" already had an integer sample).
1280  *
1281  * The tableau is assumed to have been created from "bmap" using
1282  * isl_tab_from_basic_map.
1283  */
1284 struct isl_basic_map *isl_basic_map_update_from_tab(struct isl_basic_map *bmap,
1285         struct isl_tab *tab)
1286 {
1287         int i;
1288         unsigned n_eq;
1289
1290         if (!bmap)
1291                 return NULL;
1292         if (!tab)
1293                 return bmap;
1294
1295         n_eq = tab->n_eq;
1296         if (tab->empty)
1297                 bmap = isl_basic_map_set_to_empty(bmap);
1298         else
1299                 for (i = bmap->n_ineq - 1; i >= 0; --i) {
1300                         if (isl_tab_is_equality(tab, n_eq + i))
1301                                 isl_basic_map_inequality_to_equality(bmap, i);
1302                         else if (isl_tab_is_redundant(tab, n_eq + i))
1303                                 isl_basic_map_drop_inequality(bmap, i);
1304                 }
1305         if (!tab->rational &&
1306             !bmap->sample && isl_tab_sample_is_integer(tab))
1307                 bmap->sample = extract_integer_sample(tab);
1308         return bmap;
1309 }
1310
1311 struct isl_basic_set *isl_basic_set_update_from_tab(struct isl_basic_set *bset,
1312         struct isl_tab *tab)
1313 {
1314         return (struct isl_basic_set *)isl_basic_map_update_from_tab(
1315                 (struct isl_basic_map *)bset, tab);
1316 }
1317
1318 /* Given a non-negative variable "var", add a new non-negative variable
1319  * that is the opposite of "var", ensuring that var can only attain the
1320  * value zero.
1321  * If var = n/d is a row variable, then the new variable = -n/d.
1322  * If var is a column variables, then the new variable = -var.
1323  * If the new variable cannot attain non-negative values, then
1324  * the resulting tableau is empty.
1325  * Otherwise, we know the value will be zero and we close the row.
1326  */
1327 static struct isl_tab *cut_to_hyperplane(struct isl_tab *tab,
1328         struct isl_tab_var *var)
1329 {
1330         unsigned r;
1331         isl_int *row;
1332         int sgn;
1333
1334         if (extend_cons(tab, 1) < 0)
1335                 goto error;
1336
1337         r = tab->n_con;
1338         tab->con[r].index = tab->n_row;
1339         tab->con[r].is_row = 1;
1340         tab->con[r].is_nonneg = 0;
1341         tab->con[r].is_zero = 0;
1342         tab->con[r].is_redundant = 0;
1343         tab->con[r].frozen = 0;
1344         tab->row_var[tab->n_row] = ~r;
1345         row = tab->mat->row[tab->n_row];
1346
1347         if (var->is_row) {
1348                 isl_int_set(row[0], tab->mat->row[var->index][0]);
1349                 isl_seq_neg(row + 1,
1350                             tab->mat->row[var->index] + 1, 1 + tab->n_col);
1351         } else {
1352                 isl_int_set_si(row[0], 1);
1353                 isl_seq_clr(row + 1, 1 + tab->n_col);
1354                 isl_int_set_si(row[2 + var->index], -1);
1355         }
1356
1357         tab->n_row++;
1358         tab->n_con++;
1359         push(tab, isl_tab_undo_allocate, &tab->con[r]);
1360
1361         sgn = sign_of_max(tab, &tab->con[r]);
1362         if (sgn < 0)
1363                 return mark_empty(tab);
1364         tab->con[r].is_nonneg = 1;
1365         push(tab, isl_tab_undo_nonneg, &tab->con[r]);
1366         /* sgn == 0 */
1367         close_row(tab, &tab->con[r]);
1368
1369         return tab;
1370 error:
1371         isl_tab_free(tab);
1372         return NULL;
1373 }
1374
1375 /* Given a tableau "tab" and an inequality constraint "con" of the tableau,
1376  * relax the inequality by one.  That is, the inequality r >= 0 is replaced
1377  * by r' = r + 1 >= 0.
1378  * If r is a row variable, we simply increase the constant term by one
1379  * (taking into account the denominator).
1380  * If r is a column variable, then we need to modify each row that
1381  * refers to r = r' - 1 by substituting this equality, effectively
1382  * subtracting the coefficient of the column from the constant.
1383  */
1384 struct isl_tab *isl_tab_relax(struct isl_tab *tab, int con)
1385 {
1386         struct isl_tab_var *var;
1387         if (!tab)
1388                 return NULL;
1389
1390         var = &tab->con[con];
1391
1392         if (!var->is_row && !max_is_manifestly_unbounded(tab, var))
1393                 to_row(tab, var, 1);
1394
1395         if (var->is_row)
1396                 isl_int_add(tab->mat->row[var->index][1],
1397                     tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
1398         else {
1399                 int i;
1400
1401                 for (i = 0; i < tab->n_row; ++i) {
1402                         if (isl_int_is_zero(tab->mat->row[i][2 + var->index]))
1403                                 continue;
1404                         isl_int_sub(tab->mat->row[i][1], tab->mat->row[i][1],
1405                             tab->mat->row[i][2 + var->index]);
1406                 }
1407
1408         }
1409
1410         push(tab, isl_tab_undo_relax, var);
1411
1412         return tab;
1413 }
1414
1415 struct isl_tab *isl_tab_select_facet(struct isl_tab *tab, int con)
1416 {
1417         if (!tab)
1418                 return NULL;
1419
1420         return cut_to_hyperplane(tab, &tab->con[con]);
1421 }
1422
1423 static int may_be_equality(struct isl_tab *tab, int row)
1424 {
1425         return (tab->rational ? isl_int_is_zero(tab->mat->row[row][1])
1426                               : isl_int_lt(tab->mat->row[row][1],
1427                                             tab->mat->row[row][0])) &&
1428                 isl_seq_first_non_zero(tab->mat->row[row] + 2 + tab->n_dead,
1429                                         tab->n_col - tab->n_dead) != -1;
1430 }
1431
1432 /* Check for (near) equalities among the constraints.
1433  * A constraint is an equality if it is non-negative and if
1434  * its maximal value is either
1435  *      - zero (in case of rational tableaus), or
1436  *      - strictly less than 1 (in case of integer tableaus)
1437  *
1438  * We first mark all non-redundant and non-dead variables that
1439  * are not frozen and not obviously not an equality.
1440  * Then we iterate over all marked variables if they can attain
1441  * any values larger than zero or at least one.
1442  * If the maximal value is zero, we mark any column variables
1443  * that appear in the row as being zero and mark the row as being redundant.
1444  * Otherwise, if the maximal value is strictly less than one (and the
1445  * tableau is integer), then we restrict the value to being zero
1446  * by adding an opposite non-negative variable.
1447  */
1448 struct isl_tab *isl_tab_detect_equalities(struct isl_tab *tab)
1449 {
1450         int i;
1451         unsigned n_marked;
1452
1453         if (!tab)
1454                 return NULL;
1455         if (tab->empty)
1456                 return tab;
1457         if (tab->n_dead == tab->n_col)
1458                 return tab;
1459
1460         n_marked = 0;
1461         for (i = tab->n_redundant; i < tab->n_row; ++i) {
1462                 struct isl_tab_var *var = var_from_row(tab, i);
1463                 var->marked = !var->frozen && var->is_nonneg &&
1464                         may_be_equality(tab, i);
1465                 if (var->marked)
1466                         n_marked++;
1467         }
1468         for (i = tab->n_dead; i < tab->n_col; ++i) {
1469                 struct isl_tab_var *var = var_from_col(tab, i);
1470                 var->marked = !var->frozen && var->is_nonneg;
1471                 if (var->marked)
1472                         n_marked++;
1473         }
1474         while (n_marked) {
1475                 struct isl_tab_var *var;
1476                 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1477                         var = var_from_row(tab, i);
1478                         if (var->marked)
1479                                 break;
1480                 }
1481                 if (i == tab->n_row) {
1482                         for (i = tab->n_dead; i < tab->n_col; ++i) {
1483                                 var = var_from_col(tab, i);
1484                                 if (var->marked)
1485                                         break;
1486                         }
1487                         if (i == tab->n_col)
1488                                 break;
1489                 }
1490                 var->marked = 0;
1491                 n_marked--;
1492                 if (sign_of_max(tab, var) == 0)
1493                         close_row(tab, var);
1494                 else if (!tab->rational && !at_least_one(tab, var)) {
1495                         tab = cut_to_hyperplane(tab, var);
1496                         return isl_tab_detect_equalities(tab);
1497                 }
1498                 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1499                         var = var_from_row(tab, i);
1500                         if (!var->marked)
1501                                 continue;
1502                         if (may_be_equality(tab, i))
1503                                 continue;
1504                         var->marked = 0;
1505                         n_marked--;
1506                 }
1507         }
1508
1509         return tab;
1510 }
1511
1512 /* Check for (near) redundant constraints.
1513  * A constraint is redundant if it is non-negative and if
1514  * its minimal value (temporarily ignoring the non-negativity) is either
1515  *      - zero (in case of rational tableaus), or
1516  *      - strictly larger than -1 (in case of integer tableaus)
1517  *
1518  * We first mark all non-redundant and non-dead variables that
1519  * are not frozen and not obviously negatively unbounded.
1520  * Then we iterate over all marked variables if they can attain
1521  * any values smaller than zero or at most negative one.
1522  * If not, we mark the row as being redundant (assuming it hasn't
1523  * been detected as being obviously redundant in the mean time).
1524  */
1525 struct isl_tab *isl_tab_detect_redundant(struct isl_tab *tab)
1526 {
1527         int i;
1528         unsigned n_marked;
1529
1530         if (!tab)
1531                 return NULL;
1532         if (tab->empty)
1533                 return tab;
1534         if (tab->n_redundant == tab->n_row)
1535                 return tab;
1536
1537         n_marked = 0;
1538         for (i = tab->n_redundant; i < tab->n_row; ++i) {
1539                 struct isl_tab_var *var = var_from_row(tab, i);
1540                 var->marked = !var->frozen && var->is_nonneg;
1541                 if (var->marked)
1542                         n_marked++;
1543         }
1544         for (i = tab->n_dead; i < tab->n_col; ++i) {
1545                 struct isl_tab_var *var = var_from_col(tab, i);
1546                 var->marked = !var->frozen && var->is_nonneg &&
1547                         !min_is_manifestly_unbounded(tab, var);
1548                 if (var->marked)
1549                         n_marked++;
1550         }
1551         while (n_marked) {
1552                 struct isl_tab_var *var;
1553                 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1554                         var = var_from_row(tab, i);
1555                         if (var->marked)
1556                                 break;
1557                 }
1558                 if (i == tab->n_row) {
1559                         for (i = tab->n_dead; i < tab->n_col; ++i) {
1560                                 var = var_from_col(tab, i);
1561                                 if (var->marked)
1562                                         break;
1563                         }
1564                         if (i == tab->n_col)
1565                                 break;
1566                 }
1567                 var->marked = 0;
1568                 n_marked--;
1569                 if ((tab->rational ? (sign_of_min(tab, var) >= 0)
1570                                    : !min_at_most_neg_one(tab, var)) &&
1571                     !var->is_redundant)
1572                         mark_redundant(tab, var->index);
1573                 for (i = tab->n_dead; i < tab->n_col; ++i) {
1574                         var = var_from_col(tab, i);
1575                         if (!var->marked)
1576                                 continue;
1577                         if (!min_is_manifestly_unbounded(tab, var))
1578                                 continue;
1579                         var->marked = 0;
1580                         n_marked--;
1581                 }
1582         }
1583
1584         return tab;
1585 }
1586
1587 int isl_tab_is_equality(struct isl_tab *tab, int con)
1588 {
1589         int row;
1590
1591         if (!tab)
1592                 return -1;
1593         if (tab->con[con].is_zero)
1594                 return 1;
1595         if (tab->con[con].is_redundant)
1596                 return 0;
1597         if (!tab->con[con].is_row)
1598                 return tab->con[con].index < tab->n_dead;
1599
1600         row = tab->con[con].index;
1601
1602         return isl_int_is_zero(tab->mat->row[row][1]) &&
1603                 isl_seq_first_non_zero(tab->mat->row[row] + 2 + tab->n_dead,
1604                                         tab->n_col - tab->n_dead) == -1;
1605 }
1606
1607 /* Return the minimial value of the affine expression "f" with denominator
1608  * "denom" in *opt, *opt_denom, assuming the tableau is not empty and
1609  * the expression cannot attain arbitrarily small values.
1610  * If opt_denom is NULL, then *opt is rounded up to the nearest integer.
1611  * The return value reflects the nature of the result (empty, unbounded,
1612  * minmimal value returned in *opt).
1613  */
1614 enum isl_lp_result isl_tab_min(struct isl_tab *tab,
1615         isl_int *f, isl_int denom, isl_int *opt, isl_int *opt_denom,
1616         unsigned flags)
1617 {
1618         int r;
1619         enum isl_lp_result res = isl_lp_ok;
1620         struct isl_tab_var *var;
1621         struct isl_tab_undo *snap;
1622
1623         if (tab->empty)
1624                 return isl_lp_empty;
1625
1626         snap = isl_tab_snap(tab);
1627         r = add_row(tab, f);
1628         if (r < 0)
1629                 return isl_lp_error;
1630         var = &tab->con[r];
1631         isl_int_mul(tab->mat->row[var->index][0],
1632                     tab->mat->row[var->index][0], denom);
1633         for (;;) {
1634                 int row, col;
1635                 find_pivot(tab, var, var, -1, &row, &col);
1636                 if (row == var->index) {
1637                         res = isl_lp_unbounded;
1638                         break;
1639                 }
1640                 if (row == -1)
1641                         break;
1642                 pivot(tab, row, col);
1643         }
1644         if (isl_tab_rollback(tab, snap) < 0)
1645                 return isl_lp_error;
1646         if (ISL_FL_ISSET(flags, ISL_TAB_SAVE_DUAL)) {
1647                 int i;
1648
1649                 isl_vec_free(tab->dual);
1650                 tab->dual = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_con);
1651                 if (!tab->dual)
1652                         return isl_lp_error;
1653                 isl_int_set(tab->dual->el[0], tab->mat->row[var->index][0]);
1654                 for (i = 0; i < tab->n_con; ++i) {
1655                         if (tab->con[i].is_row)
1656                                 isl_int_set_si(tab->dual->el[1 + i], 0);
1657                         else {
1658                                 int pos = 2 + tab->con[i].index;
1659                                 isl_int_set(tab->dual->el[1 + i],
1660                                             tab->mat->row[var->index][pos]);
1661                         }
1662                 }
1663         }
1664         if (res == isl_lp_ok) {
1665                 if (opt_denom) {
1666                         isl_int_set(*opt, tab->mat->row[var->index][1]);
1667                         isl_int_set(*opt_denom, tab->mat->row[var->index][0]);
1668                 } else
1669                         isl_int_cdiv_q(*opt, tab->mat->row[var->index][1],
1670                                              tab->mat->row[var->index][0]);
1671         }
1672         return res;
1673 }
1674
1675 int isl_tab_is_redundant(struct isl_tab *tab, int con)
1676 {
1677         int row;
1678         unsigned n_col;
1679
1680         if (!tab)
1681                 return -1;
1682         if (tab->con[con].is_zero)
1683                 return 0;
1684         if (tab->con[con].is_redundant)
1685                 return 1;
1686         return tab->con[con].is_row && tab->con[con].index < tab->n_redundant;
1687 }
1688
1689 /* Take a snapshot of the tableau that can be restored by s call to
1690  * isl_tab_rollback.
1691  */
1692 struct isl_tab_undo *isl_tab_snap(struct isl_tab *tab)
1693 {
1694         if (!tab)
1695                 return NULL;
1696         tab->need_undo = 1;
1697         return tab->top;
1698 }
1699
1700 /* Undo the operation performed by isl_tab_relax.
1701  */
1702 static void unrelax(struct isl_tab *tab, struct isl_tab_var *var)
1703 {
1704         if (!var->is_row && !max_is_manifestly_unbounded(tab, var))
1705                 to_row(tab, var, 1);
1706
1707         if (var->is_row)
1708                 isl_int_sub(tab->mat->row[var->index][1],
1709                     tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
1710         else {
1711                 int i;
1712
1713                 for (i = 0; i < tab->n_row; ++i) {
1714                         if (isl_int_is_zero(tab->mat->row[i][2 + var->index]))
1715                                 continue;
1716                         isl_int_add(tab->mat->row[i][1], tab->mat->row[i][1],
1717                             tab->mat->row[i][2 + var->index]);
1718                 }
1719
1720         }
1721 }
1722
1723 static void perform_undo(struct isl_tab *tab, struct isl_tab_undo *undo)
1724 {
1725         struct isl_tab_var *var = var_from_index(tab, undo->var_index);
1726         switch(undo->type) {
1727         case isl_tab_undo_empty:
1728                 tab->empty = 0;
1729                 break;
1730         case isl_tab_undo_nonneg:
1731                 var->is_nonneg = 0;
1732                 break;
1733         case isl_tab_undo_redundant:
1734                 var->is_redundant = 0;
1735                 tab->n_redundant--;
1736                 break;
1737         case isl_tab_undo_zero:
1738                 var->is_zero = 0;
1739                 tab->n_dead--;
1740                 break;
1741         case isl_tab_undo_allocate:
1742                 if (!var->is_row) {
1743                         if (!max_is_manifestly_unbounded(tab, var))
1744                                 to_row(tab, var, 1);
1745                         else if (!min_is_manifestly_unbounded(tab, var))
1746                                 to_row(tab, var, -1);
1747                         else
1748                                 to_row(tab, var, 0);
1749                 }
1750                 drop_row(tab, var->index);
1751                 break;
1752         case isl_tab_undo_relax:
1753                 unrelax(tab, var);
1754                 break;
1755         }
1756 }
1757
1758 /* Return the tableau to the state it was in when the snapshot "snap"
1759  * was taken.
1760  */
1761 int isl_tab_rollback(struct isl_tab *tab, struct isl_tab_undo *snap)
1762 {
1763         struct isl_tab_undo *undo, *next;
1764
1765         if (!tab)
1766                 return -1;
1767
1768         tab->in_undo = 1;
1769         for (undo = tab->top; undo && undo != &tab->bottom; undo = next) {
1770                 next = undo->next;
1771                 if (undo == snap)
1772                         break;
1773                 perform_undo(tab, undo);
1774                 free(undo);
1775         }
1776         tab->in_undo = 0;
1777         tab->top = undo;
1778         if (!undo)
1779                 return -1;
1780         return 0;
1781 }
1782
1783 /* The given row "row" represents an inequality violated by all
1784  * points in the tableau.  Check for some special cases of such
1785  * separating constraints.
1786  * In particular, if the row has been reduced to the constant -1,
1787  * then we know the inequality is adjacent (but opposite) to
1788  * an equality in the tableau.
1789  * If the row has been reduced to r = -1 -r', with r' an inequality
1790  * of the tableau, then the inequality is adjacent (but opposite)
1791  * to the inequality r'.
1792  */
1793 static enum isl_ineq_type separation_type(struct isl_tab *tab, unsigned row)
1794 {
1795         int pos;
1796
1797         if (tab->rational)
1798                 return isl_ineq_separate;
1799
1800         if (!isl_int_is_one(tab->mat->row[row][0]))
1801                 return isl_ineq_separate;
1802         if (!isl_int_is_negone(tab->mat->row[row][1]))
1803                 return isl_ineq_separate;
1804
1805         pos = isl_seq_first_non_zero(tab->mat->row[row] + 2 + tab->n_dead,
1806                                         tab->n_col - tab->n_dead);
1807         if (pos == -1)
1808                 return isl_ineq_adj_eq;
1809
1810         if (!isl_int_is_negone(tab->mat->row[row][2 + tab->n_dead + pos]))
1811                 return isl_ineq_separate;
1812
1813         pos = isl_seq_first_non_zero(
1814                         tab->mat->row[row] + 2 + tab->n_dead + pos + 1,
1815                         tab->n_col - tab->n_dead - pos - 1);
1816
1817         return pos == -1 ? isl_ineq_adj_ineq : isl_ineq_separate;
1818 }
1819
1820 /* Check the effect of inequality "ineq" on the tableau "tab".
1821  * The result may be
1822  *      isl_ineq_redundant:     satisfied by all points in the tableau
1823  *      isl_ineq_separate:      satisfied by no point in the tableau
1824  *      isl_ineq_cut:           satisfied by some by not all points
1825  *      isl_ineq_adj_eq:        adjacent to an equality
1826  *      isl_ineq_adj_ineq:      adjacent to an inequality.
1827  */
1828 enum isl_ineq_type isl_tab_ineq_type(struct isl_tab *tab, isl_int *ineq)
1829 {
1830         enum isl_ineq_type type = isl_ineq_error;
1831         struct isl_tab_undo *snap = NULL;
1832         int con;
1833         int row;
1834
1835         if (!tab)
1836                 return isl_ineq_error;
1837
1838         if (extend_cons(tab, 1) < 0)
1839                 return isl_ineq_error;
1840
1841         snap = isl_tab_snap(tab);
1842
1843         con = add_row(tab, ineq);
1844         if (con < 0)
1845                 goto error;
1846
1847         row = tab->con[con].index;
1848         if (is_redundant(tab, row))
1849                 type = isl_ineq_redundant;
1850         else if (isl_int_is_neg(tab->mat->row[row][1]) &&
1851                  (tab->rational ||
1852                     isl_int_abs_ge(tab->mat->row[row][1],
1853                                    tab->mat->row[row][0]))) {
1854                 if (at_least_zero(tab, &tab->con[con]))
1855                         type = isl_ineq_cut;
1856                 else
1857                         type = separation_type(tab, row);
1858         } else if (tab->rational ? (sign_of_min(tab, &tab->con[con]) < 0)
1859                              : min_at_most_neg_one(tab, &tab->con[con]))
1860                 type = isl_ineq_cut;
1861         else
1862                 type = isl_ineq_redundant;
1863
1864         if (isl_tab_rollback(tab, snap))
1865                 return isl_ineq_error;
1866         return type;
1867 error:
1868         isl_tab_rollback(tab, snap);
1869         return isl_ineq_error;
1870 }
1871
1872 void isl_tab_dump(struct isl_tab *tab, FILE *out, int indent)
1873 {
1874         unsigned r, c;
1875         int i;
1876
1877         if (!tab) {
1878                 fprintf(out, "%*snull tab\n", indent, "");
1879                 return;
1880         }
1881         fprintf(out, "%*sn_redundant: %d, n_dead: %d", indent, "",
1882                 tab->n_redundant, tab->n_dead);
1883         if (tab->rational)
1884                 fprintf(out, ", rational");
1885         if (tab->empty)
1886                 fprintf(out, ", empty");
1887         fprintf(out, "\n");
1888         fprintf(out, "%*s[", indent, "");
1889         for (i = 0; i < tab->n_var; ++i) {
1890                 if (i)
1891                         fprintf(out, ", ");
1892                 fprintf(out, "%c%d%s", tab->var[i].is_row ? 'r' : 'c',
1893                                         tab->var[i].index,
1894                                         tab->var[i].is_zero ? " [=0]" :
1895                                         tab->var[i].is_redundant ? " [R]" : "");
1896         }
1897         fprintf(out, "]\n");
1898         fprintf(out, "%*s[", indent, "");
1899         for (i = 0; i < tab->n_con; ++i) {
1900                 if (i)
1901                         fprintf(out, ", ");
1902                 fprintf(out, "%c%d%s", tab->con[i].is_row ? 'r' : 'c',
1903                                         tab->con[i].index,
1904                                         tab->con[i].is_zero ? " [=0]" :
1905                                         tab->con[i].is_redundant ? " [R]" : "");
1906         }
1907         fprintf(out, "]\n");
1908         fprintf(out, "%*s[", indent, "");
1909         for (i = 0; i < tab->n_row; ++i) {
1910                 if (i)
1911                         fprintf(out, ", ");
1912                 fprintf(out, "r%d: %d%s", i, tab->row_var[i],
1913                     var_from_row(tab, i)->is_nonneg ? " [>=0]" : "");
1914         }
1915         fprintf(out, "]\n");
1916         fprintf(out, "%*s[", indent, "");
1917         for (i = 0; i < tab->n_col; ++i) {
1918                 if (i)
1919                         fprintf(out, ", ");
1920                 fprintf(out, "c%d: %d%s", i, tab->col_var[i],
1921                     var_from_col(tab, i)->is_nonneg ? " [>=0]" : "");
1922         }
1923         fprintf(out, "]\n");
1924         r = tab->mat->n_row;
1925         tab->mat->n_row = tab->n_row;
1926         c = tab->mat->n_col;
1927         tab->mat->n_col = 2 + tab->n_col;
1928         isl_mat_dump(tab->mat, out, indent);
1929         tab->mat->n_row = r;
1930         tab->mat->n_col = c;
1931 }