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