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