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