isl_tab: introduce parameters and divs
[isl.git] / isl_tab.c
blob377d0b792aae26673dc84337001363b0e1e22714
1 #include "isl_mat.h"
2 #include "isl_map_private.h"
3 #include "isl_tab.h"
5 /*
6 * The implementation of tableaus in this file was inspired by Section 8
7 * of David Detlefs, Greg Nelson and James B. Saxe, "Simplify: a theorem
8 * prover for program checking".
9 */
11 struct isl_tab *isl_tab_alloc(struct isl_ctx *ctx,
12 unsigned n_row, unsigned n_var)
14 int i;
15 struct isl_tab *tab;
17 tab = isl_calloc_type(ctx, struct isl_tab);
18 if (!tab)
19 return NULL;
20 tab->mat = isl_mat_alloc(ctx, n_row, 2 + n_var);
21 if (!tab->mat)
22 goto error;
23 tab->var = isl_alloc_array(ctx, struct isl_tab_var, n_var);
24 if (!tab->var)
25 goto error;
26 tab->con = isl_alloc_array(ctx, struct isl_tab_var, n_row);
27 if (!tab->con)
28 goto error;
29 tab->col_var = isl_alloc_array(ctx, int, n_var);
30 if (!tab->col_var)
31 goto error;
32 tab->row_var = isl_alloc_array(ctx, int, n_row);
33 if (!tab->row_var)
34 goto error;
35 for (i = 0; i < n_var; ++i) {
36 tab->var[i].index = i;
37 tab->var[i].is_row = 0;
38 tab->var[i].is_nonneg = 0;
39 tab->var[i].is_zero = 0;
40 tab->var[i].is_redundant = 0;
41 tab->var[i].frozen = 0;
42 tab->col_var[i] = i;
44 tab->n_row = 0;
45 tab->n_con = 0;
46 tab->n_eq = 0;
47 tab->max_con = n_row;
48 tab->n_col = n_var;
49 tab->n_var = n_var;
50 tab->n_param = 0;
51 tab->n_div = 0;
52 tab->n_dead = 0;
53 tab->n_redundant = 0;
54 tab->need_undo = 0;
55 tab->rational = 0;
56 tab->empty = 0;
57 tab->in_undo = 0;
58 tab->bottom.type = isl_tab_undo_bottom;
59 tab->bottom.next = NULL;
60 tab->top = &tab->bottom;
61 return tab;
62 error:
63 isl_tab_free(tab);
64 return NULL;
67 int isl_tab_extend_cons(struct isl_tab *tab, unsigned n_new)
69 if (tab->max_con < tab->n_con + n_new) {
70 struct isl_tab_var *con;
72 con = isl_realloc_array(tab->mat->ctx, tab->con,
73 struct isl_tab_var, tab->max_con + n_new);
74 if (!con)
75 return -1;
76 tab->con = con;
77 tab->max_con += n_new;
79 if (tab->mat->n_row < tab->n_row + n_new) {
80 int *row_var;
82 tab->mat = isl_mat_extend(tab->mat,
83 tab->n_row + n_new, tab->n_col);
84 if (!tab->mat)
85 return -1;
86 row_var = isl_realloc_array(tab->mat->ctx, tab->row_var,
87 int, tab->mat->n_row);
88 if (!row_var)
89 return -1;
90 tab->row_var = row_var;
92 return 0;
95 struct isl_tab *isl_tab_extend(struct isl_tab *tab, unsigned n_new)
97 if (isl_tab_extend_cons(tab, n_new) >= 0)
98 return tab;
100 isl_tab_free(tab);
101 return NULL;
104 static void free_undo(struct isl_tab *tab)
106 struct isl_tab_undo *undo, *next;
108 for (undo = tab->top; undo && undo != &tab->bottom; undo = next) {
109 next = undo->next;
110 free(undo);
112 tab->top = undo;
115 void isl_tab_free(struct isl_tab *tab)
117 if (!tab)
118 return;
119 free_undo(tab);
120 isl_mat_free(tab->mat);
121 isl_vec_free(tab->dual);
122 free(tab->var);
123 free(tab->con);
124 free(tab->row_var);
125 free(tab->col_var);
126 free(tab);
129 struct isl_tab *isl_tab_dup(struct isl_tab *tab)
131 int i;
132 struct isl_tab *dup;
134 if (!tab)
135 return NULL;
137 dup = isl_calloc_type(tab->ctx, struct isl_tab);
138 if (!dup)
139 return NULL;
140 dup->mat = isl_mat_dup(tab->mat);
141 if (!dup->mat)
142 goto error;
143 dup->var = isl_alloc_array(tab->ctx, struct isl_tab_var, tab->n_var);
144 if (!dup->var)
145 goto error;
146 for (i = 0; i < tab->n_var; ++i)
147 dup->var[i] = tab->var[i];
148 dup->con = isl_alloc_array(tab->ctx, struct isl_tab_var, tab->max_con);
149 if (!dup->con)
150 goto error;
151 for (i = 0; i < tab->n_con; ++i)
152 dup->con[i] = tab->con[i];
153 dup->col_var = isl_alloc_array(tab->ctx, int, tab->mat->n_col);
154 if (!dup->col_var)
155 goto error;
156 for (i = 0; i < tab->n_var; ++i)
157 dup->col_var[i] = tab->col_var[i];
158 dup->row_var = isl_alloc_array(tab->ctx, int, tab->mat->n_row);
159 if (!dup->row_var)
160 goto error;
161 for (i = 0; i < tab->n_row; ++i)
162 dup->row_var[i] = tab->row_var[i];
163 dup->n_row = tab->n_row;
164 dup->n_con = tab->n_con;
165 dup->n_eq = tab->n_eq;
166 dup->max_con = tab->max_con;
167 dup->n_col = tab->n_col;
168 dup->n_var = tab->n_var;
169 dup->n_param = tab->n_param;
170 dup->n_div = tab->n_div;
171 dup->n_dead = tab->n_dead;
172 dup->n_redundant = tab->n_redundant;
173 dup->rational = tab->rational;
174 dup->empty = tab->empty;
175 dup->need_undo = 0;
176 dup->in_undo = 0;
177 dup->bottom.type = isl_tab_undo_bottom;
178 dup->bottom.next = NULL;
179 dup->top = &dup->bottom;
180 return dup;
181 error:
182 isl_tab_free(dup);
183 return NULL;
186 static struct isl_tab_var *var_from_index(struct isl_tab *tab, int i)
188 if (i >= 0)
189 return &tab->var[i];
190 else
191 return &tab->con[~i];
194 struct isl_tab_var *isl_tab_var_from_row(struct isl_tab *tab, int i)
196 return var_from_index(tab, tab->row_var[i]);
199 static struct isl_tab_var *var_from_col(struct isl_tab *tab, int i)
201 return var_from_index(tab, tab->col_var[i]);
204 /* Check if there are any upper bounds on column variable "var",
205 * i.e., non-negative rows where var appears with a negative coefficient.
206 * Return 1 if there are no such bounds.
208 static int max_is_manifestly_unbounded(struct isl_tab *tab,
209 struct isl_tab_var *var)
211 int i;
213 if (var->is_row)
214 return 0;
215 for (i = tab->n_redundant; i < tab->n_row; ++i) {
216 if (!isl_int_is_neg(tab->mat->row[i][2 + var->index]))
217 continue;
218 if (isl_tab_var_from_row(tab, i)->is_nonneg)
219 return 0;
221 return 1;
224 /* Check if there are any lower bounds on column variable "var",
225 * i.e., non-negative rows where var appears with a positive coefficient.
226 * Return 1 if there are no such bounds.
228 static int min_is_manifestly_unbounded(struct isl_tab *tab,
229 struct isl_tab_var *var)
231 int i;
233 if (var->is_row)
234 return 0;
235 for (i = tab->n_redundant; i < tab->n_row; ++i) {
236 if (!isl_int_is_pos(tab->mat->row[i][2 + var->index]))
237 continue;
238 if (isl_tab_var_from_row(tab, i)->is_nonneg)
239 return 0;
241 return 1;
244 /* Given the index of a column "c", return the index of a row
245 * that can be used to pivot the column in, with either an increase
246 * (sgn > 0) or a decrease (sgn < 0) of the corresponding variable.
247 * If "var" is not NULL, then the row returned will be different from
248 * the one associated with "var".
250 * Each row in the tableau is of the form
252 * x_r = a_r0 + \sum_i a_ri x_i
254 * Only rows with x_r >= 0 and with the sign of a_ri opposite to "sgn"
255 * impose any limit on the increase or decrease in the value of x_c
256 * and this bound is equal to a_r0 / |a_rc|. We are therefore looking
257 * for the row with the smallest (most stringent) such bound.
258 * Note that the common denominator of each row drops out of the fraction.
259 * To check if row j has a smaller bound than row r, i.e.,
260 * a_j0 / |a_jc| < a_r0 / |a_rc| or a_j0 |a_rc| < a_r0 |a_jc|,
261 * we check if -sign(a_jc) (a_j0 a_rc - a_r0 a_jc) < 0,
262 * where -sign(a_jc) is equal to "sgn".
264 static int pivot_row(struct isl_tab *tab,
265 struct isl_tab_var *var, int sgn, int c)
267 int j, r, tsgn;
268 isl_int t;
270 isl_int_init(t);
271 r = -1;
272 for (j = tab->n_redundant; j < tab->n_row; ++j) {
273 if (var && j == var->index)
274 continue;
275 if (!isl_tab_var_from_row(tab, j)->is_nonneg)
276 continue;
277 if (sgn * isl_int_sgn(tab->mat->row[j][2 + c]) >= 0)
278 continue;
279 if (r < 0) {
280 r = j;
281 continue;
283 isl_int_mul(t, tab->mat->row[r][1], tab->mat->row[j][2 + c]);
284 isl_int_submul(t, tab->mat->row[j][1], tab->mat->row[r][2 + c]);
285 tsgn = sgn * isl_int_sgn(t);
286 if (tsgn < 0 || (tsgn == 0 &&
287 tab->row_var[j] < tab->row_var[r]))
288 r = j;
290 isl_int_clear(t);
291 return r;
294 /* Find a pivot (row and col) that will increase (sgn > 0) or decrease
295 * (sgn < 0) the value of row variable var.
296 * If not NULL, then skip_var is a row variable that should be ignored
297 * while looking for a pivot row. It is usually equal to var.
299 * As the given row in the tableau is of the form
301 * x_r = a_r0 + \sum_i a_ri x_i
303 * we need to find a column such that the sign of a_ri is equal to "sgn"
304 * (such that an increase in x_i will have the desired effect) or a
305 * column with a variable that may attain negative values.
306 * If a_ri is positive, then we need to move x_i in the same direction
307 * to obtain the desired effect. Otherwise, x_i has to move in the
308 * opposite direction.
310 static void find_pivot(struct isl_tab *tab,
311 struct isl_tab_var *var, struct isl_tab_var *skip_var,
312 int sgn, int *row, int *col)
314 int j, r, c;
315 isl_int *tr;
317 *row = *col = -1;
319 isl_assert(tab->mat->ctx, var->is_row, return);
320 tr = tab->mat->row[var->index];
322 c = -1;
323 for (j = tab->n_dead; j < tab->n_col; ++j) {
324 if (isl_int_is_zero(tr[2 + j]))
325 continue;
326 if (isl_int_sgn(tr[2 + j]) != sgn &&
327 var_from_col(tab, j)->is_nonneg)
328 continue;
329 if (c < 0 || tab->col_var[j] < tab->col_var[c])
330 c = j;
332 if (c < 0)
333 return;
335 sgn *= isl_int_sgn(tr[2 + c]);
336 r = pivot_row(tab, skip_var, sgn, c);
337 *row = r < 0 ? var->index : r;
338 *col = c;
341 /* Return 1 if row "row" represents an obviously redundant inequality.
342 * This means
343 * - it represents an inequality or a variable
344 * - that is the sum of a non-negative sample value and a positive
345 * combination of zero or more non-negative variables.
347 int isl_tab_row_is_redundant(struct isl_tab *tab, int row)
349 int i;
351 if (tab->row_var[row] < 0 && !isl_tab_var_from_row(tab, row)->is_nonneg)
352 return 0;
354 if (isl_int_is_neg(tab->mat->row[row][1]))
355 return 0;
357 for (i = tab->n_dead; i < tab->n_col; ++i) {
358 if (isl_int_is_zero(tab->mat->row[row][2 + i]))
359 continue;
360 if (isl_int_is_neg(tab->mat->row[row][2 + i]))
361 return 0;
362 if (!var_from_col(tab, i)->is_nonneg)
363 return 0;
365 return 1;
368 static void swap_rows(struct isl_tab *tab, int row1, int row2)
370 int t;
371 t = tab->row_var[row1];
372 tab->row_var[row1] = tab->row_var[row2];
373 tab->row_var[row2] = t;
374 isl_tab_var_from_row(tab, row1)->index = row1;
375 isl_tab_var_from_row(tab, row2)->index = row2;
376 tab->mat = isl_mat_swap_rows(tab->mat, row1, row2);
379 static void push_union(struct isl_tab *tab,
380 enum isl_tab_undo_type type, union isl_tab_undo_val u)
382 struct isl_tab_undo *undo;
384 if (!tab->need_undo)
385 return;
387 undo = isl_alloc_type(tab->mat->ctx, struct isl_tab_undo);
388 if (!undo) {
389 free_undo(tab);
390 tab->top = NULL;
391 return;
393 undo->type = type;
394 undo->u = u;
395 undo->next = tab->top;
396 tab->top = undo;
399 void isl_tab_push_var(struct isl_tab *tab,
400 enum isl_tab_undo_type type, struct isl_tab_var *var)
402 union isl_tab_undo_val u;
403 if (var->is_row)
404 u.var_index = tab->row_var[var->index];
405 else
406 u.var_index = tab->col_var[var->index];
407 push_union(tab, type, u);
410 void isl_tab_push(struct isl_tab *tab, enum isl_tab_undo_type type)
412 union isl_tab_undo_val u = { 0 };
413 push_union(tab, type, u);
416 /* Push a record on the undo stack describing the current basic
417 * variables, so that the this state can be restored during rollback.
419 void isl_tab_push_basis(struct isl_tab *tab)
421 int i;
422 union isl_tab_undo_val u;
424 u.col_var = isl_alloc_array(tab->mat->ctx, int, tab->n_col);
425 if (!u.col_var) {
426 free_undo(tab);
427 tab->top = NULL;
428 return;
430 for (i = 0; i < tab->n_col; ++i)
431 u.col_var[i] = tab->col_var[i];
432 push_union(tab, isl_tab_undo_saved_basis, u);
435 /* Mark row with index "row" as being redundant.
436 * If we may need to undo the operation or if the row represents
437 * a variable of the original problem, the row is kept,
438 * but no longer considered when looking for a pivot row.
439 * Otherwise, the row is simply removed.
441 * The row may be interchanged with some other row. If it
442 * is interchanged with a later row, return 1. Otherwise return 0.
443 * If the rows are checked in order in the calling function,
444 * then a return value of 1 means that the row with the given
445 * row number may now contain a different row that hasn't been checked yet.
447 int isl_tab_mark_redundant(struct isl_tab *tab, int row)
449 struct isl_tab_var *var = isl_tab_var_from_row(tab, row);
450 var->is_redundant = 1;
451 isl_assert(tab->mat->ctx, row >= tab->n_redundant, return);
452 if (tab->need_undo || tab->row_var[row] >= 0) {
453 if (tab->row_var[row] >= 0 && !var->is_nonneg) {
454 var->is_nonneg = 1;
455 isl_tab_push_var(tab, isl_tab_undo_nonneg, var);
457 if (row != tab->n_redundant)
458 swap_rows(tab, row, tab->n_redundant);
459 isl_tab_push_var(tab, isl_tab_undo_redundant, var);
460 tab->n_redundant++;
461 return 0;
462 } else {
463 if (row != tab->n_row - 1)
464 swap_rows(tab, row, tab->n_row - 1);
465 isl_tab_var_from_row(tab, tab->n_row - 1)->index = -1;
466 tab->n_row--;
467 return 1;
471 struct isl_tab *isl_tab_mark_empty(struct isl_tab *tab)
473 if (!tab->empty && tab->need_undo)
474 isl_tab_push(tab, isl_tab_undo_empty);
475 tab->empty = 1;
476 return tab;
479 /* Given a row number "row" and a column number "col", pivot the tableau
480 * such that the associated variables are interchanged.
481 * The given row in the tableau expresses
483 * x_r = a_r0 + \sum_i a_ri x_i
485 * or
487 * x_c = 1/a_rc x_r - a_r0/a_rc + sum_{i \ne r} -a_ri/a_rc
489 * Substituting this equality into the other rows
491 * x_j = a_j0 + \sum_i a_ji x_i
493 * with a_jc \ne 0, we obtain
495 * 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
497 * The tableau
499 * n_rc/d_r n_ri/d_r
500 * n_jc/d_j n_ji/d_j
502 * where i is any other column and j is any other row,
503 * is therefore transformed into
505 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
506 * 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)
508 * The transformation is performed along the following steps
510 * d_r/n_rc n_ri/n_rc
511 * n_jc/d_j n_ji/d_j
513 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
514 * n_jc/d_j n_ji/d_j
516 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
517 * n_jc/(|n_rc| d_j) n_ji/(|n_rc| d_j)
519 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
520 * n_jc/(|n_rc| d_j) (n_ji |n_rc|)/(|n_rc| d_j)
522 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
523 * n_jc/(|n_rc| d_j) (n_ji |n_rc| - s(n_rc)n_jc n_ri)/(|n_rc| d_j)
525 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
526 * 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)
529 void isl_tab_pivot(struct isl_tab *tab, int row, int col)
531 int i, j;
532 int sgn;
533 int t;
534 struct isl_mat *mat = tab->mat;
535 struct isl_tab_var *var;
537 isl_int_swap(mat->row[row][0], mat->row[row][2 + col]);
538 sgn = isl_int_sgn(mat->row[row][0]);
539 if (sgn < 0) {
540 isl_int_neg(mat->row[row][0], mat->row[row][0]);
541 isl_int_neg(mat->row[row][2 + col], mat->row[row][2 + col]);
542 } else
543 for (j = 0; j < 1 + tab->n_col; ++j) {
544 if (j == 1 + col)
545 continue;
546 isl_int_neg(mat->row[row][1 + j], mat->row[row][1 + j]);
548 if (!isl_int_is_one(mat->row[row][0]))
549 isl_seq_normalize(mat->row[row], 2 + tab->n_col);
550 for (i = 0; i < tab->n_row; ++i) {
551 if (i == row)
552 continue;
553 if (isl_int_is_zero(mat->row[i][2 + col]))
554 continue;
555 isl_int_mul(mat->row[i][0], mat->row[i][0], mat->row[row][0]);
556 for (j = 0; j < 1 + tab->n_col; ++j) {
557 if (j == 1 + col)
558 continue;
559 isl_int_mul(mat->row[i][1 + j],
560 mat->row[i][1 + j], mat->row[row][0]);
561 isl_int_addmul(mat->row[i][1 + j],
562 mat->row[i][2 + col], mat->row[row][1 + j]);
564 isl_int_mul(mat->row[i][2 + col],
565 mat->row[i][2 + col], mat->row[row][2 + col]);
566 if (!isl_int_is_one(mat->row[i][0]))
567 isl_seq_normalize(mat->row[i], 2 + tab->n_col);
569 t = tab->row_var[row];
570 tab->row_var[row] = tab->col_var[col];
571 tab->col_var[col] = t;
572 var = isl_tab_var_from_row(tab, row);
573 var->is_row = 1;
574 var->index = row;
575 var = var_from_col(tab, col);
576 var->is_row = 0;
577 var->index = col;
578 if (tab->in_undo)
579 return;
580 for (i = tab->n_redundant; i < tab->n_row; ++i) {
581 if (isl_int_is_zero(mat->row[i][2 + col]))
582 continue;
583 if (!isl_tab_var_from_row(tab, i)->frozen &&
584 isl_tab_row_is_redundant(tab, i))
585 if (isl_tab_mark_redundant(tab, i))
586 --i;
590 /* If "var" represents a column variable, then pivot is up (sgn > 0)
591 * or down (sgn < 0) to a row. The variable is assumed not to be
592 * unbounded in the specified direction.
593 * If sgn = 0, then the variable is unbounded in both directions,
594 * and we pivot with any row we can find.
596 static void to_row(struct isl_tab *tab, struct isl_tab_var *var, int sign)
598 int r;
600 if (var->is_row)
601 return;
603 if (sign == 0) {
604 for (r = tab->n_redundant; r < tab->n_row; ++r)
605 if (!isl_int_is_zero(tab->mat->row[r][2 + var->index]))
606 break;
607 isl_assert(tab->mat->ctx, r < tab->n_row, return);
608 } else {
609 r = pivot_row(tab, NULL, sign, var->index);
610 isl_assert(tab->mat->ctx, r >= 0, return);
613 isl_tab_pivot(tab, r, var->index);
616 static void check_table(struct isl_tab *tab)
618 int i;
620 if (tab->empty)
621 return;
622 for (i = 0; i < tab->n_row; ++i) {
623 if (!isl_tab_var_from_row(tab, i)->is_nonneg)
624 continue;
625 assert(!isl_int_is_neg(tab->mat->row[i][1]));
629 /* Return the sign of the maximal value of "var".
630 * If the sign is not negative, then on return from this function,
631 * the sample value will also be non-negative.
633 * If "var" is manifestly unbounded wrt positive values, we are done.
634 * Otherwise, we pivot the variable up to a row if needed
635 * Then we continue pivoting down until either
636 * - no more down pivots can be performed
637 * - the sample value is positive
638 * - the variable is pivoted into a manifestly unbounded column
640 static int sign_of_max(struct isl_tab *tab, struct isl_tab_var *var)
642 int row, col;
644 if (max_is_manifestly_unbounded(tab, var))
645 return 1;
646 to_row(tab, var, 1);
647 while (!isl_int_is_pos(tab->mat->row[var->index][1])) {
648 find_pivot(tab, var, var, 1, &row, &col);
649 if (row == -1)
650 return isl_int_sgn(tab->mat->row[var->index][1]);
651 isl_tab_pivot(tab, row, col);
652 if (!var->is_row) /* manifestly unbounded */
653 return 1;
655 return 1;
658 /* Perform pivots until the row variable "var" has a non-negative
659 * sample value or until no more upward pivots can be performed.
660 * Return the sign of the sample value after the pivots have been
661 * performed.
663 static int restore_row(struct isl_tab *tab, struct isl_tab_var *var)
665 int row, col;
667 while (isl_int_is_neg(tab->mat->row[var->index][1])) {
668 find_pivot(tab, var, var, 1, &row, &col);
669 if (row == -1)
670 break;
671 isl_tab_pivot(tab, row, col);
672 if (!var->is_row) /* manifestly unbounded */
673 return 1;
675 return isl_int_sgn(tab->mat->row[var->index][1]);
678 /* Perform pivots until we are sure that the row variable "var"
679 * can attain non-negative values. After return from this
680 * function, "var" is still a row variable, but its sample
681 * value may not be non-negative, even if the function returns 1.
683 static int at_least_zero(struct isl_tab *tab, struct isl_tab_var *var)
685 int row, col;
687 while (isl_int_is_neg(tab->mat->row[var->index][1])) {
688 find_pivot(tab, var, var, 1, &row, &col);
689 if (row == -1)
690 break;
691 if (row == var->index) /* manifestly unbounded */
692 return 1;
693 isl_tab_pivot(tab, row, col);
695 return !isl_int_is_neg(tab->mat->row[var->index][1]);
698 /* Return a negative value if "var" can attain negative values.
699 * Return a non-negative value otherwise.
701 * If "var" is manifestly unbounded wrt negative values, we are done.
702 * Otherwise, if var is in a column, we can pivot it down to a row.
703 * Then we continue pivoting down until either
704 * - the pivot would result in a manifestly unbounded column
705 * => we don't perform the pivot, but simply return -1
706 * - no more down pivots can be performed
707 * - the sample value is negative
708 * If the sample value becomes negative and the variable is supposed
709 * to be nonnegative, then we undo the last pivot.
710 * However, if the last pivot has made the pivoting variable
711 * obviously redundant, then it may have moved to another row.
712 * In that case we look for upward pivots until we reach a non-negative
713 * value again.
715 static int sign_of_min(struct isl_tab *tab, struct isl_tab_var *var)
717 int row, col;
718 struct isl_tab_var *pivot_var;
720 if (min_is_manifestly_unbounded(tab, var))
721 return -1;
722 if (!var->is_row) {
723 col = var->index;
724 row = pivot_row(tab, NULL, -1, col);
725 pivot_var = var_from_col(tab, col);
726 isl_tab_pivot(tab, row, col);
727 if (var->is_redundant)
728 return 0;
729 if (isl_int_is_neg(tab->mat->row[var->index][1])) {
730 if (var->is_nonneg) {
731 if (!pivot_var->is_redundant &&
732 pivot_var->index == row)
733 isl_tab_pivot(tab, row, col);
734 else
735 restore_row(tab, var);
737 return -1;
740 if (var->is_redundant)
741 return 0;
742 while (!isl_int_is_neg(tab->mat->row[var->index][1])) {
743 find_pivot(tab, var, var, -1, &row, &col);
744 if (row == var->index)
745 return -1;
746 if (row == -1)
747 return isl_int_sgn(tab->mat->row[var->index][1]);
748 pivot_var = var_from_col(tab, col);
749 isl_tab_pivot(tab, row, col);
750 if (var->is_redundant)
751 return 0;
753 if (var->is_nonneg) {
754 /* pivot back to non-negative value */
755 if (!pivot_var->is_redundant && pivot_var->index == row)
756 isl_tab_pivot(tab, row, col);
757 else
758 restore_row(tab, var);
760 return -1;
763 /* Return 1 if "var" can attain values <= -1.
764 * Return 0 otherwise.
766 * The sample value of "var" is assumed to be non-negative when the
767 * the function is called and will be made non-negative again before
768 * the function returns.
770 int isl_tab_min_at_most_neg_one(struct isl_tab *tab, struct isl_tab_var *var)
772 int row, col;
773 struct isl_tab_var *pivot_var;
775 if (min_is_manifestly_unbounded(tab, var))
776 return 1;
777 if (!var->is_row) {
778 col = var->index;
779 row = pivot_row(tab, NULL, -1, col);
780 pivot_var = var_from_col(tab, col);
781 isl_tab_pivot(tab, row, col);
782 if (var->is_redundant)
783 return 0;
784 if (isl_int_is_neg(tab->mat->row[var->index][1]) &&
785 isl_int_abs_ge(tab->mat->row[var->index][1],
786 tab->mat->row[var->index][0])) {
787 if (var->is_nonneg) {
788 if (!pivot_var->is_redundant &&
789 pivot_var->index == row)
790 isl_tab_pivot(tab, row, col);
791 else
792 restore_row(tab, var);
794 return 1;
797 if (var->is_redundant)
798 return 0;
799 do {
800 find_pivot(tab, var, var, -1, &row, &col);
801 if (row == var->index)
802 return 1;
803 if (row == -1)
804 return 0;
805 pivot_var = var_from_col(tab, col);
806 isl_tab_pivot(tab, row, col);
807 if (var->is_redundant)
808 return 0;
809 } while (!isl_int_is_neg(tab->mat->row[var->index][1]) ||
810 isl_int_abs_lt(tab->mat->row[var->index][1],
811 tab->mat->row[var->index][0]));
812 if (var->is_nonneg) {
813 /* pivot back to non-negative value */
814 if (!pivot_var->is_redundant && pivot_var->index == row)
815 isl_tab_pivot(tab, row, col);
816 restore_row(tab, var);
818 return 1;
821 /* Return 1 if "var" can attain values >= 1.
822 * Return 0 otherwise.
824 static int at_least_one(struct isl_tab *tab, struct isl_tab_var *var)
826 int row, col;
827 isl_int *r;
829 if (max_is_manifestly_unbounded(tab, var))
830 return 1;
831 to_row(tab, var, 1);
832 r = tab->mat->row[var->index];
833 while (isl_int_lt(r[1], r[0])) {
834 find_pivot(tab, var, var, 1, &row, &col);
835 if (row == -1)
836 return isl_int_ge(r[1], r[0]);
837 if (row == var->index) /* manifestly unbounded */
838 return 1;
839 isl_tab_pivot(tab, row, col);
841 return 1;
844 static void swap_cols(struct isl_tab *tab, int col1, int col2)
846 int t;
847 t = tab->col_var[col1];
848 tab->col_var[col1] = tab->col_var[col2];
849 tab->col_var[col2] = t;
850 var_from_col(tab, col1)->index = col1;
851 var_from_col(tab, col2)->index = col2;
852 tab->mat = isl_mat_swap_cols(tab->mat, 2 + col1, 2 + col2);
855 /* Mark column with index "col" as representing a zero variable.
856 * If we may need to undo the operation the column is kept,
857 * but no longer considered.
858 * Otherwise, the column is simply removed.
860 * The column may be interchanged with some other column. If it
861 * is interchanged with a later column, return 1. Otherwise return 0.
862 * If the columns are checked in order in the calling function,
863 * then a return value of 1 means that the column with the given
864 * column number may now contain a different column that
865 * hasn't been checked yet.
867 int isl_tab_kill_col(struct isl_tab *tab, int col)
869 var_from_col(tab, col)->is_zero = 1;
870 if (tab->need_undo) {
871 isl_tab_push_var(tab, isl_tab_undo_zero, var_from_col(tab, col));
872 if (col != tab->n_dead)
873 swap_cols(tab, col, tab->n_dead);
874 tab->n_dead++;
875 return 0;
876 } else {
877 if (col != tab->n_col - 1)
878 swap_cols(tab, col, tab->n_col - 1);
879 var_from_col(tab, tab->n_col - 1)->index = -1;
880 tab->n_col--;
881 return 1;
885 /* Row variable "var" is non-negative and cannot attain any values
886 * larger than zero. This means that the coefficients of the unrestricted
887 * column variables are zero and that the coefficients of the non-negative
888 * column variables are zero or negative.
889 * Each of the non-negative variables with a negative coefficient can
890 * then also be written as the negative sum of non-negative variables
891 * and must therefore also be zero.
893 static void close_row(struct isl_tab *tab, struct isl_tab_var *var)
895 int j;
896 struct isl_mat *mat = tab->mat;
898 isl_assert(tab->mat->ctx, var->is_nonneg, return);
899 var->is_zero = 1;
900 for (j = tab->n_dead; j < tab->n_col; ++j) {
901 if (isl_int_is_zero(mat->row[var->index][2 + j]))
902 continue;
903 isl_assert(tab->mat->ctx,
904 isl_int_is_neg(mat->row[var->index][2 + j]), return);
905 if (isl_tab_kill_col(tab, j))
906 --j;
908 isl_tab_mark_redundant(tab, var->index);
911 /* Add a constraint to the tableau and allocate a row for it.
912 * Return the index into the constraint array "con".
914 int isl_tab_allocate_con(struct isl_tab *tab)
916 int r;
918 isl_assert(tab->mat->ctx, tab->n_row < tab->mat->n_row, return -1);
920 r = tab->n_con;
921 tab->con[r].index = tab->n_row;
922 tab->con[r].is_row = 1;
923 tab->con[r].is_nonneg = 0;
924 tab->con[r].is_zero = 0;
925 tab->con[r].is_redundant = 0;
926 tab->con[r].frozen = 0;
927 tab->row_var[tab->n_row] = ~r;
929 tab->n_row++;
930 tab->n_con++;
931 isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->con[r]);
933 return r;
936 /* Add a row to the tableau. The row is given as an affine combination
937 * of the original variables and needs to be expressed in terms of the
938 * column variables.
940 * We add each term in turn.
941 * If r = n/d_r is the current sum and we need to add k x, then
942 * if x is a column variable, we increase the numerator of
943 * this column by k d_r
944 * if x = f/d_x is a row variable, then the new representation of r is
946 * n k f d_x/g n + d_r/g k f m/d_r n + m/d_g k f
947 * --- + --- = ------------------- = -------------------
948 * d_r d_r d_r d_x/g m
950 * with g the gcd of d_r and d_x and m the lcm of d_r and d_x.
952 int isl_tab_add_row(struct isl_tab *tab, isl_int *line)
954 int i;
955 int r;
956 isl_int *row;
957 isl_int a, b;
959 r = isl_tab_allocate_con(tab);
960 if (r < 0)
961 return -1;
963 isl_int_init(a);
964 isl_int_init(b);
965 row = tab->mat->row[tab->con[r].index];
966 isl_int_set_si(row[0], 1);
967 isl_int_set(row[1], line[0]);
968 isl_seq_clr(row + 2, tab->n_col);
969 for (i = 0; i < tab->n_var; ++i) {
970 if (tab->var[i].is_zero)
971 continue;
972 if (tab->var[i].is_row) {
973 isl_int_lcm(a,
974 row[0], tab->mat->row[tab->var[i].index][0]);
975 isl_int_swap(a, row[0]);
976 isl_int_divexact(a, row[0], a);
977 isl_int_divexact(b,
978 row[0], tab->mat->row[tab->var[i].index][0]);
979 isl_int_mul(b, b, line[1 + i]);
980 isl_seq_combine(row + 1, a, row + 1,
981 b, tab->mat->row[tab->var[i].index] + 1,
982 1 + tab->n_col);
983 } else
984 isl_int_addmul(row[2 + tab->var[i].index],
985 line[1 + i], row[0]);
987 isl_seq_normalize(row, 2 + tab->n_col);
988 isl_int_clear(a);
989 isl_int_clear(b);
991 return r;
994 static int drop_row(struct isl_tab *tab, int row)
996 isl_assert(tab->mat->ctx, ~tab->row_var[row] == tab->n_con - 1, return -1);
997 if (row != tab->n_row - 1)
998 swap_rows(tab, row, tab->n_row - 1);
999 tab->n_row--;
1000 tab->n_con--;
1001 return 0;
1004 /* Add inequality "ineq" and check if it conflicts with the
1005 * previously added constraints or if it is obviously redundant.
1007 struct isl_tab *isl_tab_add_ineq(struct isl_tab *tab, isl_int *ineq)
1009 int r;
1010 int sgn;
1012 if (!tab)
1013 return NULL;
1014 r = isl_tab_add_row(tab, ineq);
1015 if (r < 0)
1016 goto error;
1017 tab->con[r].is_nonneg = 1;
1018 isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]);
1019 if (isl_tab_row_is_redundant(tab, tab->con[r].index)) {
1020 isl_tab_mark_redundant(tab, tab->con[r].index);
1021 return tab;
1024 sgn = restore_row(tab, &tab->con[r]);
1025 if (sgn < 0)
1026 return isl_tab_mark_empty(tab);
1027 if (tab->con[r].is_row && isl_tab_row_is_redundant(tab, tab->con[r].index))
1028 isl_tab_mark_redundant(tab, tab->con[r].index);
1029 return tab;
1030 error:
1031 isl_tab_free(tab);
1032 return NULL;
1035 /* Pivot a non-negative variable down until it reaches the value zero
1036 * and then pivot the variable into a column position.
1038 int to_col(struct isl_tab *tab, struct isl_tab_var *var)
1040 int i;
1041 int row, col;
1043 if (!var->is_row)
1044 return;
1046 while (isl_int_is_pos(tab->mat->row[var->index][1])) {
1047 find_pivot(tab, var, NULL, -1, &row, &col);
1048 isl_assert(tab->mat->ctx, row != -1, return -1);
1049 isl_tab_pivot(tab, row, col);
1050 if (!var->is_row)
1051 return;
1054 for (i = tab->n_dead; i < tab->n_col; ++i)
1055 if (!isl_int_is_zero(tab->mat->row[var->index][2 + i]))
1056 break;
1058 isl_assert(tab->mat->ctx, i < tab->n_col, return -1);
1059 isl_tab_pivot(tab, var->index, i);
1061 return 0;
1064 /* We assume Gaussian elimination has been performed on the equalities.
1065 * The equalities can therefore never conflict.
1066 * Adding the equalities is currently only really useful for a later call
1067 * to isl_tab_ineq_type.
1069 static struct isl_tab *add_eq(struct isl_tab *tab, isl_int *eq)
1071 int i;
1072 int r;
1074 if (!tab)
1075 return NULL;
1076 r = isl_tab_add_row(tab, eq);
1077 if (r < 0)
1078 goto error;
1080 r = tab->con[r].index;
1081 i = isl_seq_first_non_zero(tab->mat->row[r] + 2 + tab->n_dead,
1082 tab->n_col - tab->n_dead);
1083 isl_assert(tab->mat->ctx, i >= 0, goto error);
1084 i += tab->n_dead;
1085 isl_tab_pivot(tab, r, i);
1086 isl_tab_kill_col(tab, i);
1087 tab->n_eq++;
1089 return tab;
1090 error:
1091 isl_tab_free(tab);
1092 return NULL;
1095 /* Add an equality that is known to be valid for the given tableau.
1097 struct isl_tab *isl_tab_add_valid_eq(struct isl_tab *tab, isl_int *eq)
1099 struct isl_tab_var *var;
1100 int i;
1101 int r;
1103 if (!tab)
1104 return NULL;
1105 r = isl_tab_add_row(tab, eq);
1106 if (r < 0)
1107 goto error;
1109 var = &tab->con[r];
1110 r = var->index;
1111 if (isl_int_is_neg(tab->mat->row[r][1]))
1112 isl_seq_neg(tab->mat->row[r] + 1, tab->mat->row[r] + 1,
1113 1 + tab->n_col);
1114 var->is_nonneg = 1;
1115 if (to_col(tab, var) < 0)
1116 goto error;
1117 var->is_nonneg = 0;
1118 isl_tab_kill_col(tab, var->index);
1120 return tab;
1121 error:
1122 isl_tab_free(tab);
1123 return NULL;
1126 struct isl_tab *isl_tab_from_basic_map(struct isl_basic_map *bmap)
1128 int i;
1129 struct isl_tab *tab;
1131 if (!bmap)
1132 return NULL;
1133 tab = isl_tab_alloc(bmap->ctx,
1134 isl_basic_map_total_dim(bmap) + bmap->n_ineq + 1,
1135 isl_basic_map_total_dim(bmap));
1136 if (!tab)
1137 return NULL;
1138 tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
1139 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1140 return isl_tab_mark_empty(tab);
1141 for (i = 0; i < bmap->n_eq; ++i) {
1142 tab = add_eq(tab, bmap->eq[i]);
1143 if (!tab)
1144 return tab;
1146 for (i = 0; i < bmap->n_ineq; ++i) {
1147 tab = isl_tab_add_ineq(tab, bmap->ineq[i]);
1148 if (!tab || tab->empty)
1149 return tab;
1151 return tab;
1154 struct isl_tab *isl_tab_from_basic_set(struct isl_basic_set *bset)
1156 return isl_tab_from_basic_map((struct isl_basic_map *)bset);
1159 /* Construct a tableau corresponding to the recession cone of "bmap".
1161 struct isl_tab *isl_tab_from_recession_cone(struct isl_basic_map *bmap)
1163 isl_int cst;
1164 int i;
1165 struct isl_tab *tab;
1167 if (!bmap)
1168 return NULL;
1169 tab = isl_tab_alloc(bmap->ctx, bmap->n_eq + bmap->n_ineq,
1170 isl_basic_map_total_dim(bmap));
1171 if (!tab)
1172 return NULL;
1173 tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
1175 isl_int_init(cst);
1176 for (i = 0; i < bmap->n_eq; ++i) {
1177 isl_int_swap(bmap->eq[i][0], cst);
1178 tab = add_eq(tab, bmap->eq[i]);
1179 isl_int_swap(bmap->eq[i][0], cst);
1180 if (!tab)
1181 goto done;
1183 for (i = 0; i < bmap->n_ineq; ++i) {
1184 int r;
1185 isl_int_swap(bmap->ineq[i][0], cst);
1186 r = isl_tab_add_row(tab, bmap->ineq[i]);
1187 isl_int_swap(bmap->ineq[i][0], cst);
1188 if (r < 0)
1189 goto error;
1190 tab->con[r].is_nonneg = 1;
1191 isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]);
1193 done:
1194 isl_int_clear(cst);
1195 return tab;
1196 error:
1197 isl_int_clear(cst);
1198 isl_tab_free(tab);
1199 return NULL;
1202 /* Assuming "tab" is the tableau of a cone, check if the cone is
1203 * bounded, i.e., if it is empty or only contains the origin.
1205 int isl_tab_cone_is_bounded(struct isl_tab *tab)
1207 int i;
1209 if (!tab)
1210 return -1;
1211 if (tab->empty)
1212 return 1;
1213 if (tab->n_dead == tab->n_col)
1214 return 1;
1216 for (;;) {
1217 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1218 struct isl_tab_var *var;
1219 var = isl_tab_var_from_row(tab, i);
1220 if (!var->is_nonneg)
1221 continue;
1222 if (sign_of_max(tab, var) != 0)
1223 return 0;
1224 close_row(tab, var);
1225 break;
1227 if (tab->n_dead == tab->n_col)
1228 return 1;
1229 if (i == tab->n_row)
1230 return 0;
1234 int isl_tab_sample_is_integer(struct isl_tab *tab)
1236 int i;
1238 if (!tab)
1239 return -1;
1241 for (i = 0; i < tab->n_var; ++i) {
1242 int row;
1243 if (!tab->var[i].is_row)
1244 continue;
1245 row = tab->var[i].index;
1246 if (!isl_int_is_divisible_by(tab->mat->row[row][1],
1247 tab->mat->row[row][0]))
1248 return 0;
1250 return 1;
1253 static struct isl_vec *extract_integer_sample(struct isl_tab *tab)
1255 int i;
1256 struct isl_vec *vec;
1258 vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
1259 if (!vec)
1260 return NULL;
1262 isl_int_set_si(vec->block.data[0], 1);
1263 for (i = 0; i < tab->n_var; ++i) {
1264 if (!tab->var[i].is_row)
1265 isl_int_set_si(vec->block.data[1 + i], 0);
1266 else {
1267 int row = tab->var[i].index;
1268 isl_int_divexact(vec->block.data[1 + i],
1269 tab->mat->row[row][1], tab->mat->row[row][0]);
1273 return vec;
1276 struct isl_vec *isl_tab_get_sample_value(struct isl_tab *tab)
1278 int i;
1279 struct isl_vec *vec;
1280 isl_int m;
1282 if (!tab)
1283 return NULL;
1285 vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
1286 if (!vec)
1287 return NULL;
1289 isl_int_init(m);
1291 isl_int_set_si(vec->block.data[0], 1);
1292 for (i = 0; i < tab->n_var; ++i) {
1293 int row;
1294 if (!tab->var[i].is_row) {
1295 isl_int_set_si(vec->block.data[1 + i], 0);
1296 continue;
1298 row = tab->var[i].index;
1299 isl_int_gcd(m, vec->block.data[0], tab->mat->row[row][0]);
1300 isl_int_divexact(m, tab->mat->row[row][0], m);
1301 isl_seq_scale(vec->block.data, vec->block.data, m, 1 + i);
1302 isl_int_divexact(m, vec->block.data[0], tab->mat->row[row][0]);
1303 isl_int_mul(vec->block.data[1 + i], m, tab->mat->row[row][1]);
1305 isl_seq_normalize(vec->block.data, vec->size);
1307 isl_int_clear(m);
1308 return vec;
1311 /* Update "bmap" based on the results of the tableau "tab".
1312 * In particular, implicit equalities are made explicit, redundant constraints
1313 * are removed and if the sample value happens to be integer, it is stored
1314 * in "bmap" (unless "bmap" already had an integer sample).
1316 * The tableau is assumed to have been created from "bmap" using
1317 * isl_tab_from_basic_map.
1319 struct isl_basic_map *isl_basic_map_update_from_tab(struct isl_basic_map *bmap,
1320 struct isl_tab *tab)
1322 int i;
1323 unsigned n_eq;
1325 if (!bmap)
1326 return NULL;
1327 if (!tab)
1328 return bmap;
1330 n_eq = tab->n_eq;
1331 if (tab->empty)
1332 bmap = isl_basic_map_set_to_empty(bmap);
1333 else
1334 for (i = bmap->n_ineq - 1; i >= 0; --i) {
1335 if (isl_tab_is_equality(tab, n_eq + i))
1336 isl_basic_map_inequality_to_equality(bmap, i);
1337 else if (isl_tab_is_redundant(tab, n_eq + i))
1338 isl_basic_map_drop_inequality(bmap, i);
1340 if (!tab->rational &&
1341 !bmap->sample && isl_tab_sample_is_integer(tab))
1342 bmap->sample = extract_integer_sample(tab);
1343 return bmap;
1346 struct isl_basic_set *isl_basic_set_update_from_tab(struct isl_basic_set *bset,
1347 struct isl_tab *tab)
1349 return (struct isl_basic_set *)isl_basic_map_update_from_tab(
1350 (struct isl_basic_map *)bset, tab);
1353 /* Given a non-negative variable "var", add a new non-negative variable
1354 * that is the opposite of "var", ensuring that var can only attain the
1355 * value zero.
1356 * If var = n/d is a row variable, then the new variable = -n/d.
1357 * If var is a column variables, then the new variable = -var.
1358 * If the new variable cannot attain non-negative values, then
1359 * the resulting tableau is empty.
1360 * Otherwise, we know the value will be zero and we close the row.
1362 static struct isl_tab *cut_to_hyperplane(struct isl_tab *tab,
1363 struct isl_tab_var *var)
1365 unsigned r;
1366 isl_int *row;
1367 int sgn;
1369 if (isl_tab_extend_cons(tab, 1) < 0)
1370 goto error;
1372 r = tab->n_con;
1373 tab->con[r].index = tab->n_row;
1374 tab->con[r].is_row = 1;
1375 tab->con[r].is_nonneg = 0;
1376 tab->con[r].is_zero = 0;
1377 tab->con[r].is_redundant = 0;
1378 tab->con[r].frozen = 0;
1379 tab->row_var[tab->n_row] = ~r;
1380 row = tab->mat->row[tab->n_row];
1382 if (var->is_row) {
1383 isl_int_set(row[0], tab->mat->row[var->index][0]);
1384 isl_seq_neg(row + 1,
1385 tab->mat->row[var->index] + 1, 1 + tab->n_col);
1386 } else {
1387 isl_int_set_si(row[0], 1);
1388 isl_seq_clr(row + 1, 1 + tab->n_col);
1389 isl_int_set_si(row[2 + var->index], -1);
1392 tab->n_row++;
1393 tab->n_con++;
1394 isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->con[r]);
1396 sgn = sign_of_max(tab, &tab->con[r]);
1397 if (sgn < 0)
1398 return isl_tab_mark_empty(tab);
1399 tab->con[r].is_nonneg = 1;
1400 isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]);
1401 /* sgn == 0 */
1402 close_row(tab, &tab->con[r]);
1404 return tab;
1405 error:
1406 isl_tab_free(tab);
1407 return NULL;
1410 /* Given a tableau "tab" and an inequality constraint "con" of the tableau,
1411 * relax the inequality by one. That is, the inequality r >= 0 is replaced
1412 * by r' = r + 1 >= 0.
1413 * If r is a row variable, we simply increase the constant term by one
1414 * (taking into account the denominator).
1415 * If r is a column variable, then we need to modify each row that
1416 * refers to r = r' - 1 by substituting this equality, effectively
1417 * subtracting the coefficient of the column from the constant.
1419 struct isl_tab *isl_tab_relax(struct isl_tab *tab, int con)
1421 struct isl_tab_var *var;
1422 if (!tab)
1423 return NULL;
1425 var = &tab->con[con];
1427 if (!var->is_row && !max_is_manifestly_unbounded(tab, var))
1428 to_row(tab, var, 1);
1430 if (var->is_row)
1431 isl_int_add(tab->mat->row[var->index][1],
1432 tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
1433 else {
1434 int i;
1436 for (i = 0; i < tab->n_row; ++i) {
1437 if (isl_int_is_zero(tab->mat->row[i][2 + var->index]))
1438 continue;
1439 isl_int_sub(tab->mat->row[i][1], tab->mat->row[i][1],
1440 tab->mat->row[i][2 + var->index]);
1445 isl_tab_push_var(tab, isl_tab_undo_relax, var);
1447 return tab;
1450 struct isl_tab *isl_tab_select_facet(struct isl_tab *tab, int con)
1452 if (!tab)
1453 return NULL;
1455 return cut_to_hyperplane(tab, &tab->con[con]);
1458 static int may_be_equality(struct isl_tab *tab, int row)
1460 return (tab->rational ? isl_int_is_zero(tab->mat->row[row][1])
1461 : isl_int_lt(tab->mat->row[row][1],
1462 tab->mat->row[row][0])) &&
1463 isl_seq_first_non_zero(tab->mat->row[row] + 2 + tab->n_dead,
1464 tab->n_col - tab->n_dead) != -1;
1467 /* Check for (near) equalities among the constraints.
1468 * A constraint is an equality if it is non-negative and if
1469 * its maximal value is either
1470 * - zero (in case of rational tableaus), or
1471 * - strictly less than 1 (in case of integer tableaus)
1473 * We first mark all non-redundant and non-dead variables that
1474 * are not frozen and not obviously not an equality.
1475 * Then we iterate over all marked variables if they can attain
1476 * any values larger than zero or at least one.
1477 * If the maximal value is zero, we mark any column variables
1478 * that appear in the row as being zero and mark the row as being redundant.
1479 * Otherwise, if the maximal value is strictly less than one (and the
1480 * tableau is integer), then we restrict the value to being zero
1481 * by adding an opposite non-negative variable.
1483 struct isl_tab *isl_tab_detect_equalities(struct isl_tab *tab)
1485 int i;
1486 unsigned n_marked;
1488 if (!tab)
1489 return NULL;
1490 if (tab->empty)
1491 return tab;
1492 if (tab->n_dead == tab->n_col)
1493 return tab;
1495 n_marked = 0;
1496 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1497 struct isl_tab_var *var = isl_tab_var_from_row(tab, i);
1498 var->marked = !var->frozen && var->is_nonneg &&
1499 may_be_equality(tab, i);
1500 if (var->marked)
1501 n_marked++;
1503 for (i = tab->n_dead; i < tab->n_col; ++i) {
1504 struct isl_tab_var *var = var_from_col(tab, i);
1505 var->marked = !var->frozen && var->is_nonneg;
1506 if (var->marked)
1507 n_marked++;
1509 while (n_marked) {
1510 struct isl_tab_var *var;
1511 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1512 var = isl_tab_var_from_row(tab, i);
1513 if (var->marked)
1514 break;
1516 if (i == tab->n_row) {
1517 for (i = tab->n_dead; i < tab->n_col; ++i) {
1518 var = var_from_col(tab, i);
1519 if (var->marked)
1520 break;
1522 if (i == tab->n_col)
1523 break;
1525 var->marked = 0;
1526 n_marked--;
1527 if (sign_of_max(tab, var) == 0)
1528 close_row(tab, var);
1529 else if (!tab->rational && !at_least_one(tab, var)) {
1530 tab = cut_to_hyperplane(tab, var);
1531 return isl_tab_detect_equalities(tab);
1533 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1534 var = isl_tab_var_from_row(tab, i);
1535 if (!var->marked)
1536 continue;
1537 if (may_be_equality(tab, i))
1538 continue;
1539 var->marked = 0;
1540 n_marked--;
1544 return tab;
1547 /* Check for (near) redundant constraints.
1548 * A constraint is redundant if it is non-negative and if
1549 * its minimal value (temporarily ignoring the non-negativity) is either
1550 * - zero (in case of rational tableaus), or
1551 * - strictly larger than -1 (in case of integer tableaus)
1553 * We first mark all non-redundant and non-dead variables that
1554 * are not frozen and not obviously negatively unbounded.
1555 * Then we iterate over all marked variables if they can attain
1556 * any values smaller than zero or at most negative one.
1557 * If not, we mark the row as being redundant (assuming it hasn't
1558 * been detected as being obviously redundant in the mean time).
1560 struct isl_tab *isl_tab_detect_redundant(struct isl_tab *tab)
1562 int i;
1563 unsigned n_marked;
1565 if (!tab)
1566 return NULL;
1567 if (tab->empty)
1568 return tab;
1569 if (tab->n_redundant == tab->n_row)
1570 return tab;
1572 n_marked = 0;
1573 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1574 struct isl_tab_var *var = isl_tab_var_from_row(tab, i);
1575 var->marked = !var->frozen && var->is_nonneg;
1576 if (var->marked)
1577 n_marked++;
1579 for (i = tab->n_dead; i < tab->n_col; ++i) {
1580 struct isl_tab_var *var = var_from_col(tab, i);
1581 var->marked = !var->frozen && var->is_nonneg &&
1582 !min_is_manifestly_unbounded(tab, var);
1583 if (var->marked)
1584 n_marked++;
1586 while (n_marked) {
1587 struct isl_tab_var *var;
1588 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1589 var = isl_tab_var_from_row(tab, i);
1590 if (var->marked)
1591 break;
1593 if (i == tab->n_row) {
1594 for (i = tab->n_dead; i < tab->n_col; ++i) {
1595 var = var_from_col(tab, i);
1596 if (var->marked)
1597 break;
1599 if (i == tab->n_col)
1600 break;
1602 var->marked = 0;
1603 n_marked--;
1604 if ((tab->rational ? (sign_of_min(tab, var) >= 0)
1605 : !isl_tab_min_at_most_neg_one(tab, var)) &&
1606 !var->is_redundant)
1607 isl_tab_mark_redundant(tab, var->index);
1608 for (i = tab->n_dead; i < tab->n_col; ++i) {
1609 var = var_from_col(tab, i);
1610 if (!var->marked)
1611 continue;
1612 if (!min_is_manifestly_unbounded(tab, var))
1613 continue;
1614 var->marked = 0;
1615 n_marked--;
1619 return tab;
1622 int isl_tab_is_equality(struct isl_tab *tab, int con)
1624 int row;
1626 if (!tab)
1627 return -1;
1628 if (tab->con[con].is_zero)
1629 return 1;
1630 if (tab->con[con].is_redundant)
1631 return 0;
1632 if (!tab->con[con].is_row)
1633 return tab->con[con].index < tab->n_dead;
1635 row = tab->con[con].index;
1637 return isl_int_is_zero(tab->mat->row[row][1]) &&
1638 isl_seq_first_non_zero(tab->mat->row[row] + 2 + tab->n_dead,
1639 tab->n_col - tab->n_dead) == -1;
1642 /* Return the minimial value of the affine expression "f" with denominator
1643 * "denom" in *opt, *opt_denom, assuming the tableau is not empty and
1644 * the expression cannot attain arbitrarily small values.
1645 * If opt_denom is NULL, then *opt is rounded up to the nearest integer.
1646 * The return value reflects the nature of the result (empty, unbounded,
1647 * minmimal value returned in *opt).
1649 enum isl_lp_result isl_tab_min(struct isl_tab *tab,
1650 isl_int *f, isl_int denom, isl_int *opt, isl_int *opt_denom,
1651 unsigned flags)
1653 int r;
1654 enum isl_lp_result res = isl_lp_ok;
1655 struct isl_tab_var *var;
1656 struct isl_tab_undo *snap;
1658 if (tab->empty)
1659 return isl_lp_empty;
1661 snap = isl_tab_snap(tab);
1662 r = isl_tab_add_row(tab, f);
1663 if (r < 0)
1664 return isl_lp_error;
1665 var = &tab->con[r];
1666 isl_int_mul(tab->mat->row[var->index][0],
1667 tab->mat->row[var->index][0], denom);
1668 for (;;) {
1669 int row, col;
1670 find_pivot(tab, var, var, -1, &row, &col);
1671 if (row == var->index) {
1672 res = isl_lp_unbounded;
1673 break;
1675 if (row == -1)
1676 break;
1677 isl_tab_pivot(tab, row, col);
1679 if (isl_tab_rollback(tab, snap) < 0)
1680 return isl_lp_error;
1681 if (ISL_FL_ISSET(flags, ISL_TAB_SAVE_DUAL)) {
1682 int i;
1684 isl_vec_free(tab->dual);
1685 tab->dual = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_con);
1686 if (!tab->dual)
1687 return isl_lp_error;
1688 isl_int_set(tab->dual->el[0], tab->mat->row[var->index][0]);
1689 for (i = 0; i < tab->n_con; ++i) {
1690 if (tab->con[i].is_row)
1691 isl_int_set_si(tab->dual->el[1 + i], 0);
1692 else {
1693 int pos = 2 + tab->con[i].index;
1694 isl_int_set(tab->dual->el[1 + i],
1695 tab->mat->row[var->index][pos]);
1699 if (res == isl_lp_ok) {
1700 if (opt_denom) {
1701 isl_int_set(*opt, tab->mat->row[var->index][1]);
1702 isl_int_set(*opt_denom, tab->mat->row[var->index][0]);
1703 } else
1704 isl_int_cdiv_q(*opt, tab->mat->row[var->index][1],
1705 tab->mat->row[var->index][0]);
1707 return res;
1710 int isl_tab_is_redundant(struct isl_tab *tab, int con)
1712 int row;
1713 unsigned n_col;
1715 if (!tab)
1716 return -1;
1717 if (tab->con[con].is_zero)
1718 return 0;
1719 if (tab->con[con].is_redundant)
1720 return 1;
1721 return tab->con[con].is_row && tab->con[con].index < tab->n_redundant;
1724 /* Take a snapshot of the tableau that can be restored by s call to
1725 * isl_tab_rollback.
1727 struct isl_tab_undo *isl_tab_snap(struct isl_tab *tab)
1729 if (!tab)
1730 return NULL;
1731 tab->need_undo = 1;
1732 return tab->top;
1735 /* Undo the operation performed by isl_tab_relax.
1737 static void unrelax(struct isl_tab *tab, struct isl_tab_var *var)
1739 if (!var->is_row && !max_is_manifestly_unbounded(tab, var))
1740 to_row(tab, var, 1);
1742 if (var->is_row)
1743 isl_int_sub(tab->mat->row[var->index][1],
1744 tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
1745 else {
1746 int i;
1748 for (i = 0; i < tab->n_row; ++i) {
1749 if (isl_int_is_zero(tab->mat->row[i][2 + var->index]))
1750 continue;
1751 isl_int_add(tab->mat->row[i][1], tab->mat->row[i][1],
1752 tab->mat->row[i][2 + var->index]);
1758 static void perform_undo_var(struct isl_tab *tab, struct isl_tab_undo *undo)
1760 struct isl_tab_var *var = var_from_index(tab, undo->u.var_index);
1761 switch(undo->type) {
1762 case isl_tab_undo_nonneg:
1763 var->is_nonneg = 0;
1764 break;
1765 case isl_tab_undo_redundant:
1766 var->is_redundant = 0;
1767 tab->n_redundant--;
1768 break;
1769 case isl_tab_undo_zero:
1770 var->is_zero = 0;
1771 tab->n_dead--;
1772 break;
1773 case isl_tab_undo_allocate:
1774 if (!var->is_row) {
1775 if (!max_is_manifestly_unbounded(tab, var))
1776 to_row(tab, var, 1);
1777 else if (!min_is_manifestly_unbounded(tab, var))
1778 to_row(tab, var, -1);
1779 else
1780 to_row(tab, var, 0);
1782 drop_row(tab, var->index);
1783 break;
1784 case isl_tab_undo_relax:
1785 unrelax(tab, var);
1786 break;
1790 /* Restore the tableau to the state where the basic variables
1791 * are those in "col_var".
1792 * We first construct a list of variables that are currently in
1793 * the basis, but shouldn't. Then we iterate over all variables
1794 * that should be in the basis and for each one that is currently
1795 * not in the basis, we exchange it with one of the elements of the
1796 * list constructed before.
1797 * We can always find an appropriate variable to pivot with because
1798 * the current basis is mapped to the old basis by a non-singular
1799 * matrix and so we can never end up with a zero row.
1801 static int restore_basis(struct isl_tab *tab, int *col_var)
1803 int i, j;
1804 int n_extra = 0;
1805 int *extra = NULL; /* current columns that contain bad stuff */
1806 unsigned off = 2;
1808 extra = isl_alloc_array(tab->mat->ctx, int, tab->n_col);
1809 if (!extra)
1810 goto error;
1811 for (i = 0; i < tab->n_col; ++i) {
1812 for (j = 0; j < tab->n_col; ++j)
1813 if (tab->col_var[i] == col_var[j])
1814 break;
1815 if (j < tab->n_col)
1816 continue;
1817 extra[n_extra++] = i;
1819 for (i = 0; i < tab->n_col && n_extra > 0; ++i) {
1820 struct isl_tab_var *var;
1821 int row;
1823 for (j = 0; j < tab->n_col; ++j)
1824 if (col_var[i] == tab->col_var[j])
1825 break;
1826 if (j < tab->n_col)
1827 continue;
1828 var = var_from_index(tab, col_var[i]);
1829 row = var->index;
1830 for (j = 0; j < n_extra; ++j)
1831 if (!isl_int_is_zero(tab->mat->row[row][off+extra[j]]))
1832 break;
1833 isl_assert(tab->mat->ctx, j < n_extra, goto error);
1834 isl_tab_pivot(tab, row, extra[j]);
1835 extra[j] = extra[--n_extra];
1838 free(extra);
1839 free(col_var);
1840 return 0;
1841 error:
1842 free(extra);
1843 free(col_var);
1844 return -1;
1847 static int perform_undo(struct isl_tab *tab, struct isl_tab_undo *undo)
1849 switch (undo->type) {
1850 case isl_tab_undo_empty:
1851 tab->empty = 0;
1852 break;
1853 case isl_tab_undo_nonneg:
1854 case isl_tab_undo_redundant:
1855 case isl_tab_undo_zero:
1856 case isl_tab_undo_allocate:
1857 case isl_tab_undo_relax:
1858 perform_undo_var(tab, undo);
1859 break;
1860 case isl_tab_undo_saved_basis:
1861 if (restore_basis(tab, undo->u.col_var) < 0)
1862 return -1;
1863 break;
1864 default:
1865 isl_assert(tab->mat->ctx, 0, return -1);
1867 return 0;
1870 /* Return the tableau to the state it was in when the snapshot "snap"
1871 * was taken.
1873 int isl_tab_rollback(struct isl_tab *tab, struct isl_tab_undo *snap)
1875 struct isl_tab_undo *undo, *next;
1877 if (!tab)
1878 return -1;
1880 tab->in_undo = 1;
1881 for (undo = tab->top; undo && undo != &tab->bottom; undo = next) {
1882 next = undo->next;
1883 if (undo == snap)
1884 break;
1885 if (perform_undo(tab, undo) < 0) {
1886 free_undo(tab);
1887 tab->in_undo = 0;
1888 return -1;
1890 free(undo);
1892 tab->in_undo = 0;
1893 tab->top = undo;
1894 if (!undo)
1895 return -1;
1896 return 0;
1899 /* The given row "row" represents an inequality violated by all
1900 * points in the tableau. Check for some special cases of such
1901 * separating constraints.
1902 * In particular, if the row has been reduced to the constant -1,
1903 * then we know the inequality is adjacent (but opposite) to
1904 * an equality in the tableau.
1905 * If the row has been reduced to r = -1 -r', with r' an inequality
1906 * of the tableau, then the inequality is adjacent (but opposite)
1907 * to the inequality r'.
1909 static enum isl_ineq_type separation_type(struct isl_tab *tab, unsigned row)
1911 int pos;
1913 if (tab->rational)
1914 return isl_ineq_separate;
1916 if (!isl_int_is_one(tab->mat->row[row][0]))
1917 return isl_ineq_separate;
1918 if (!isl_int_is_negone(tab->mat->row[row][1]))
1919 return isl_ineq_separate;
1921 pos = isl_seq_first_non_zero(tab->mat->row[row] + 2 + tab->n_dead,
1922 tab->n_col - tab->n_dead);
1923 if (pos == -1)
1924 return isl_ineq_adj_eq;
1926 if (!isl_int_is_negone(tab->mat->row[row][2 + tab->n_dead + pos]))
1927 return isl_ineq_separate;
1929 pos = isl_seq_first_non_zero(
1930 tab->mat->row[row] + 2 + tab->n_dead + pos + 1,
1931 tab->n_col - tab->n_dead - pos - 1);
1933 return pos == -1 ? isl_ineq_adj_ineq : isl_ineq_separate;
1936 /* Check the effect of inequality "ineq" on the tableau "tab".
1937 * The result may be
1938 * isl_ineq_redundant: satisfied by all points in the tableau
1939 * isl_ineq_separate: satisfied by no point in the tableau
1940 * isl_ineq_cut: satisfied by some by not all points
1941 * isl_ineq_adj_eq: adjacent to an equality
1942 * isl_ineq_adj_ineq: adjacent to an inequality.
1944 enum isl_ineq_type isl_tab_ineq_type(struct isl_tab *tab, isl_int *ineq)
1946 enum isl_ineq_type type = isl_ineq_error;
1947 struct isl_tab_undo *snap = NULL;
1948 int con;
1949 int row;
1951 if (!tab)
1952 return isl_ineq_error;
1954 if (isl_tab_extend_cons(tab, 1) < 0)
1955 return isl_ineq_error;
1957 snap = isl_tab_snap(tab);
1959 con = isl_tab_add_row(tab, ineq);
1960 if (con < 0)
1961 goto error;
1963 row = tab->con[con].index;
1964 if (isl_tab_row_is_redundant(tab, row))
1965 type = isl_ineq_redundant;
1966 else if (isl_int_is_neg(tab->mat->row[row][1]) &&
1967 (tab->rational ||
1968 isl_int_abs_ge(tab->mat->row[row][1],
1969 tab->mat->row[row][0]))) {
1970 if (at_least_zero(tab, &tab->con[con]))
1971 type = isl_ineq_cut;
1972 else
1973 type = separation_type(tab, row);
1974 } else if (tab->rational ? (sign_of_min(tab, &tab->con[con]) < 0)
1975 : isl_tab_min_at_most_neg_one(tab, &tab->con[con]))
1976 type = isl_ineq_cut;
1977 else
1978 type = isl_ineq_redundant;
1980 if (isl_tab_rollback(tab, snap))
1981 return isl_ineq_error;
1982 return type;
1983 error:
1984 isl_tab_rollback(tab, snap);
1985 return isl_ineq_error;
1988 void isl_tab_dump(struct isl_tab *tab, FILE *out, int indent)
1990 unsigned r, c;
1991 int i;
1993 if (!tab) {
1994 fprintf(out, "%*snull tab\n", indent, "");
1995 return;
1997 fprintf(out, "%*sn_redundant: %d, n_dead: %d", indent, "",
1998 tab->n_redundant, tab->n_dead);
1999 if (tab->rational)
2000 fprintf(out, ", rational");
2001 if (tab->empty)
2002 fprintf(out, ", empty");
2003 fprintf(out, "\n");
2004 fprintf(out, "%*s[", indent, "");
2005 for (i = 0; i < tab->n_var; ++i) {
2006 if (i)
2007 fprintf(out, (i == tab->n_param ||
2008 i == tab->n_var - tab->n_div) ? "; "
2009 : ", ");
2010 fprintf(out, "%c%d%s", tab->var[i].is_row ? 'r' : 'c',
2011 tab->var[i].index,
2012 tab->var[i].is_zero ? " [=0]" :
2013 tab->var[i].is_redundant ? " [R]" : "");
2015 fprintf(out, "]\n");
2016 fprintf(out, "%*s[", indent, "");
2017 for (i = 0; i < tab->n_con; ++i) {
2018 if (i)
2019 fprintf(out, ", ");
2020 fprintf(out, "%c%d%s", tab->con[i].is_row ? 'r' : 'c',
2021 tab->con[i].index,
2022 tab->con[i].is_zero ? " [=0]" :
2023 tab->con[i].is_redundant ? " [R]" : "");
2025 fprintf(out, "]\n");
2026 fprintf(out, "%*s[", indent, "");
2027 for (i = 0; i < tab->n_row; ++i) {
2028 if (i)
2029 fprintf(out, ", ");
2030 fprintf(out, "r%d: %d%s", i, tab->row_var[i],
2031 isl_tab_var_from_row(tab, i)->is_nonneg ? " [>=0]" : "");
2033 fprintf(out, "]\n");
2034 fprintf(out, "%*s[", indent, "");
2035 for (i = 0; i < tab->n_col; ++i) {
2036 if (i)
2037 fprintf(out, ", ");
2038 fprintf(out, "c%d: %d%s", i, tab->col_var[i],
2039 var_from_col(tab, i)->is_nonneg ? " [>=0]" : "");
2041 fprintf(out, "]\n");
2042 r = tab->mat->n_row;
2043 tab->mat->n_row = tab->n_row;
2044 c = tab->mat->n_col;
2045 tab->mat->n_col = 2 + tab->n_col;
2046 isl_mat_dump(tab->mat, out, indent);
2047 tab->mat->n_row = r;
2048 tab->mat->n_col = c;