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