isl_tab: allow introduction of extra variables
[isl.git] / isl_tab.c
blob5e3ca5aeebbda113f0dad0640ab336448f50d174
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->max_var = n_var;
51 tab->n_param = 0;
52 tab->n_div = 0;
53 tab->n_dead = 0;
54 tab->n_redundant = 0;
55 tab->need_undo = 0;
56 tab->rational = 0;
57 tab->empty = 0;
58 tab->in_undo = 0;
59 tab->bottom.type = isl_tab_undo_bottom;
60 tab->bottom.next = NULL;
61 tab->top = &tab->bottom;
62 return tab;
63 error:
64 isl_tab_free(tab);
65 return NULL;
68 int isl_tab_extend_cons(struct isl_tab *tab, unsigned n_new)
70 if (tab->max_con < tab->n_con + n_new) {
71 struct isl_tab_var *con;
73 con = isl_realloc_array(tab->mat->ctx, tab->con,
74 struct isl_tab_var, tab->max_con + n_new);
75 if (!con)
76 return -1;
77 tab->con = con;
78 tab->max_con += n_new;
80 if (tab->mat->n_row < tab->n_row + n_new) {
81 int *row_var;
83 tab->mat = isl_mat_extend(tab->mat,
84 tab->n_row + n_new, tab->n_col);
85 if (!tab->mat)
86 return -1;
87 row_var = isl_realloc_array(tab->mat->ctx, tab->row_var,
88 int, tab->mat->n_row);
89 if (!row_var)
90 return -1;
91 tab->row_var = row_var;
93 return 0;
96 /* Make room for at least n_new extra variables.
97 * Return -1 if anything went wrong.
99 int isl_tab_extend_vars(struct isl_tab *tab, unsigned n_new)
101 struct isl_tab_var *var;
102 unsigned off = 2;
104 if (tab->max_var < tab->n_var + n_new) {
105 var = isl_realloc_array(tab->mat->ctx, tab->var,
106 struct isl_tab_var, tab->n_var + n_new);
107 if (!var)
108 return -1;
109 tab->var = var;
110 tab->max_var += n_new;
113 if (tab->mat->n_col < off + tab->n_col + n_new) {
114 int *p;
116 tab->mat = isl_mat_extend(tab->mat,
117 tab->mat->n_row, off + tab->n_col + n_new);
118 if (!tab->mat)
119 return -1;
120 p = isl_realloc_array(tab->mat->ctx, tab->col_var,
121 int, tab->mat->n_col);
122 if (!p)
123 return -1;
124 tab->col_var = p;
127 return 0;
130 struct isl_tab *isl_tab_extend(struct isl_tab *tab, unsigned n_new)
132 if (isl_tab_extend_cons(tab, n_new) >= 0)
133 return tab;
135 isl_tab_free(tab);
136 return NULL;
139 static void free_undo(struct isl_tab *tab)
141 struct isl_tab_undo *undo, *next;
143 for (undo = tab->top; undo && undo != &tab->bottom; undo = next) {
144 next = undo->next;
145 free(undo);
147 tab->top = undo;
150 void isl_tab_free(struct isl_tab *tab)
152 if (!tab)
153 return;
154 free_undo(tab);
155 isl_mat_free(tab->mat);
156 isl_vec_free(tab->dual);
157 free(tab->var);
158 free(tab->con);
159 free(tab->row_var);
160 free(tab->col_var);
161 free(tab);
164 struct isl_tab *isl_tab_dup(struct isl_tab *tab)
166 int i;
167 struct isl_tab *dup;
169 if (!tab)
170 return NULL;
172 dup = isl_calloc_type(tab->ctx, struct isl_tab);
173 if (!dup)
174 return NULL;
175 dup->mat = isl_mat_dup(tab->mat);
176 if (!dup->mat)
177 goto error;
178 dup->var = isl_alloc_array(tab->ctx, struct isl_tab_var, tab->max_var);
179 if (!dup->var)
180 goto error;
181 for (i = 0; i < tab->n_var; ++i)
182 dup->var[i] = tab->var[i];
183 dup->con = isl_alloc_array(tab->ctx, struct isl_tab_var, tab->max_con);
184 if (!dup->con)
185 goto error;
186 for (i = 0; i < tab->n_con; ++i)
187 dup->con[i] = tab->con[i];
188 dup->col_var = isl_alloc_array(tab->ctx, int, tab->mat->n_col);
189 if (!dup->col_var)
190 goto error;
191 for (i = 0; i < tab->n_var; ++i)
192 dup->col_var[i] = tab->col_var[i];
193 dup->row_var = isl_alloc_array(tab->ctx, int, tab->mat->n_row);
194 if (!dup->row_var)
195 goto error;
196 for (i = 0; i < tab->n_row; ++i)
197 dup->row_var[i] = tab->row_var[i];
198 dup->n_row = tab->n_row;
199 dup->n_con = tab->n_con;
200 dup->n_eq = tab->n_eq;
201 dup->max_con = tab->max_con;
202 dup->n_col = tab->n_col;
203 dup->n_var = tab->n_var;
204 dup->max_var = tab->max_var;
205 dup->n_param = tab->n_param;
206 dup->n_div = tab->n_div;
207 dup->n_dead = tab->n_dead;
208 dup->n_redundant = tab->n_redundant;
209 dup->rational = tab->rational;
210 dup->empty = tab->empty;
211 dup->need_undo = 0;
212 dup->in_undo = 0;
213 dup->bottom.type = isl_tab_undo_bottom;
214 dup->bottom.next = NULL;
215 dup->top = &dup->bottom;
216 return dup;
217 error:
218 isl_tab_free(dup);
219 return NULL;
222 static struct isl_tab_var *var_from_index(struct isl_tab *tab, int i)
224 if (i >= 0)
225 return &tab->var[i];
226 else
227 return &tab->con[~i];
230 struct isl_tab_var *isl_tab_var_from_row(struct isl_tab *tab, int i)
232 return var_from_index(tab, tab->row_var[i]);
235 static struct isl_tab_var *var_from_col(struct isl_tab *tab, int i)
237 return var_from_index(tab, tab->col_var[i]);
240 /* Check if there are any upper bounds on column variable "var",
241 * i.e., non-negative rows where var appears with a negative coefficient.
242 * Return 1 if there are no such bounds.
244 static int max_is_manifestly_unbounded(struct isl_tab *tab,
245 struct isl_tab_var *var)
247 int i;
249 if (var->is_row)
250 return 0;
251 for (i = tab->n_redundant; i < tab->n_row; ++i) {
252 if (!isl_int_is_neg(tab->mat->row[i][2 + var->index]))
253 continue;
254 if (isl_tab_var_from_row(tab, i)->is_nonneg)
255 return 0;
257 return 1;
260 /* Check if there are any lower bounds on column variable "var",
261 * i.e., non-negative rows where var appears with a positive coefficient.
262 * Return 1 if there are no such bounds.
264 static int min_is_manifestly_unbounded(struct isl_tab *tab,
265 struct isl_tab_var *var)
267 int i;
269 if (var->is_row)
270 return 0;
271 for (i = tab->n_redundant; i < tab->n_row; ++i) {
272 if (!isl_int_is_pos(tab->mat->row[i][2 + var->index]))
273 continue;
274 if (isl_tab_var_from_row(tab, i)->is_nonneg)
275 return 0;
277 return 1;
280 /* Given the index of a column "c", return the index of a row
281 * that can be used to pivot the column in, with either an increase
282 * (sgn > 0) or a decrease (sgn < 0) of the corresponding variable.
283 * If "var" is not NULL, then the row returned will be different from
284 * the one associated with "var".
286 * Each row in the tableau is of the form
288 * x_r = a_r0 + \sum_i a_ri x_i
290 * Only rows with x_r >= 0 and with the sign of a_ri opposite to "sgn"
291 * impose any limit on the increase or decrease in the value of x_c
292 * and this bound is equal to a_r0 / |a_rc|. We are therefore looking
293 * for the row with the smallest (most stringent) such bound.
294 * Note that the common denominator of each row drops out of the fraction.
295 * To check if row j has a smaller bound than row r, i.e.,
296 * a_j0 / |a_jc| < a_r0 / |a_rc| or a_j0 |a_rc| < a_r0 |a_jc|,
297 * we check if -sign(a_jc) (a_j0 a_rc - a_r0 a_jc) < 0,
298 * where -sign(a_jc) is equal to "sgn".
300 static int pivot_row(struct isl_tab *tab,
301 struct isl_tab_var *var, int sgn, int c)
303 int j, r, tsgn;
304 isl_int t;
306 isl_int_init(t);
307 r = -1;
308 for (j = tab->n_redundant; j < tab->n_row; ++j) {
309 if (var && j == var->index)
310 continue;
311 if (!isl_tab_var_from_row(tab, j)->is_nonneg)
312 continue;
313 if (sgn * isl_int_sgn(tab->mat->row[j][2 + c]) >= 0)
314 continue;
315 if (r < 0) {
316 r = j;
317 continue;
319 isl_int_mul(t, tab->mat->row[r][1], tab->mat->row[j][2 + c]);
320 isl_int_submul(t, tab->mat->row[j][1], tab->mat->row[r][2 + c]);
321 tsgn = sgn * isl_int_sgn(t);
322 if (tsgn < 0 || (tsgn == 0 &&
323 tab->row_var[j] < tab->row_var[r]))
324 r = j;
326 isl_int_clear(t);
327 return r;
330 /* Find a pivot (row and col) that will increase (sgn > 0) or decrease
331 * (sgn < 0) the value of row variable var.
332 * If not NULL, then skip_var is a row variable that should be ignored
333 * while looking for a pivot row. It is usually equal to var.
335 * As the given row in the tableau is of the form
337 * x_r = a_r0 + \sum_i a_ri x_i
339 * we need to find a column such that the sign of a_ri is equal to "sgn"
340 * (such that an increase in x_i will have the desired effect) or a
341 * column with a variable that may attain negative values.
342 * If a_ri is positive, then we need to move x_i in the same direction
343 * to obtain the desired effect. Otherwise, x_i has to move in the
344 * opposite direction.
346 static void find_pivot(struct isl_tab *tab,
347 struct isl_tab_var *var, struct isl_tab_var *skip_var,
348 int sgn, int *row, int *col)
350 int j, r, c;
351 isl_int *tr;
353 *row = *col = -1;
355 isl_assert(tab->mat->ctx, var->is_row, return);
356 tr = tab->mat->row[var->index];
358 c = -1;
359 for (j = tab->n_dead; j < tab->n_col; ++j) {
360 if (isl_int_is_zero(tr[2 + j]))
361 continue;
362 if (isl_int_sgn(tr[2 + j]) != sgn &&
363 var_from_col(tab, j)->is_nonneg)
364 continue;
365 if (c < 0 || tab->col_var[j] < tab->col_var[c])
366 c = j;
368 if (c < 0)
369 return;
371 sgn *= isl_int_sgn(tr[2 + c]);
372 r = pivot_row(tab, skip_var, sgn, c);
373 *row = r < 0 ? var->index : r;
374 *col = c;
377 /* Return 1 if row "row" represents an obviously redundant inequality.
378 * This means
379 * - it represents an inequality or a variable
380 * - that is the sum of a non-negative sample value and a positive
381 * combination of zero or more non-negative variables.
383 int isl_tab_row_is_redundant(struct isl_tab *tab, int row)
385 int i;
387 if (tab->row_var[row] < 0 && !isl_tab_var_from_row(tab, row)->is_nonneg)
388 return 0;
390 if (isl_int_is_neg(tab->mat->row[row][1]))
391 return 0;
393 for (i = tab->n_dead; i < tab->n_col; ++i) {
394 if (isl_int_is_zero(tab->mat->row[row][2 + i]))
395 continue;
396 if (isl_int_is_neg(tab->mat->row[row][2 + i]))
397 return 0;
398 if (!var_from_col(tab, i)->is_nonneg)
399 return 0;
401 return 1;
404 static void swap_rows(struct isl_tab *tab, int row1, int row2)
406 int t;
407 t = tab->row_var[row1];
408 tab->row_var[row1] = tab->row_var[row2];
409 tab->row_var[row2] = t;
410 isl_tab_var_from_row(tab, row1)->index = row1;
411 isl_tab_var_from_row(tab, row2)->index = row2;
412 tab->mat = isl_mat_swap_rows(tab->mat, row1, row2);
415 static void push_union(struct isl_tab *tab,
416 enum isl_tab_undo_type type, union isl_tab_undo_val u)
418 struct isl_tab_undo *undo;
420 if (!tab->need_undo)
421 return;
423 undo = isl_alloc_type(tab->mat->ctx, struct isl_tab_undo);
424 if (!undo) {
425 free_undo(tab);
426 tab->top = NULL;
427 return;
429 undo->type = type;
430 undo->u = u;
431 undo->next = tab->top;
432 tab->top = undo;
435 void isl_tab_push_var(struct isl_tab *tab,
436 enum isl_tab_undo_type type, struct isl_tab_var *var)
438 union isl_tab_undo_val u;
439 if (var->is_row)
440 u.var_index = tab->row_var[var->index];
441 else
442 u.var_index = tab->col_var[var->index];
443 push_union(tab, type, u);
446 void isl_tab_push(struct isl_tab *tab, enum isl_tab_undo_type type)
448 union isl_tab_undo_val u = { 0 };
449 push_union(tab, type, u);
452 /* Push a record on the undo stack describing the current basic
453 * variables, so that the this state can be restored during rollback.
455 void isl_tab_push_basis(struct isl_tab *tab)
457 int i;
458 union isl_tab_undo_val u;
460 u.col_var = isl_alloc_array(tab->mat->ctx, int, tab->n_col);
461 if (!u.col_var) {
462 free_undo(tab);
463 tab->top = NULL;
464 return;
466 for (i = 0; i < tab->n_col; ++i)
467 u.col_var[i] = tab->col_var[i];
468 push_union(tab, isl_tab_undo_saved_basis, u);
471 /* Mark row with index "row" as being redundant.
472 * If we may need to undo the operation or if the row represents
473 * a variable of the original problem, the row is kept,
474 * but no longer considered when looking for a pivot row.
475 * Otherwise, the row is simply removed.
477 * The row may be interchanged with some other row. If it
478 * is interchanged with a later row, return 1. Otherwise return 0.
479 * If the rows are checked in order in the calling function,
480 * then a return value of 1 means that the row with the given
481 * row number may now contain a different row that hasn't been checked yet.
483 int isl_tab_mark_redundant(struct isl_tab *tab, int row)
485 struct isl_tab_var *var = isl_tab_var_from_row(tab, row);
486 var->is_redundant = 1;
487 isl_assert(tab->mat->ctx, row >= tab->n_redundant, return);
488 if (tab->need_undo || tab->row_var[row] >= 0) {
489 if (tab->row_var[row] >= 0 && !var->is_nonneg) {
490 var->is_nonneg = 1;
491 isl_tab_push_var(tab, isl_tab_undo_nonneg, var);
493 if (row != tab->n_redundant)
494 swap_rows(tab, row, tab->n_redundant);
495 isl_tab_push_var(tab, isl_tab_undo_redundant, var);
496 tab->n_redundant++;
497 return 0;
498 } else {
499 if (row != tab->n_row - 1)
500 swap_rows(tab, row, tab->n_row - 1);
501 isl_tab_var_from_row(tab, tab->n_row - 1)->index = -1;
502 tab->n_row--;
503 return 1;
507 struct isl_tab *isl_tab_mark_empty(struct isl_tab *tab)
509 if (!tab->empty && tab->need_undo)
510 isl_tab_push(tab, isl_tab_undo_empty);
511 tab->empty = 1;
512 return tab;
515 /* Given a row number "row" and a column number "col", pivot the tableau
516 * such that the associated variables are interchanged.
517 * The given row in the tableau expresses
519 * x_r = a_r0 + \sum_i a_ri x_i
521 * or
523 * x_c = 1/a_rc x_r - a_r0/a_rc + sum_{i \ne r} -a_ri/a_rc
525 * Substituting this equality into the other rows
527 * x_j = a_j0 + \sum_i a_ji x_i
529 * with a_jc \ne 0, we obtain
531 * 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
533 * The tableau
535 * n_rc/d_r n_ri/d_r
536 * n_jc/d_j n_ji/d_j
538 * where i is any other column and j is any other row,
539 * is therefore transformed into
541 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
542 * 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)
544 * The transformation is performed along the following steps
546 * d_r/n_rc n_ri/n_rc
547 * n_jc/d_j n_ji/d_j
549 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
550 * n_jc/d_j n_ji/d_j
552 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
553 * n_jc/(|n_rc| d_j) n_ji/(|n_rc| d_j)
555 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
556 * n_jc/(|n_rc| d_j) (n_ji |n_rc|)/(|n_rc| d_j)
558 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
559 * n_jc/(|n_rc| d_j) (n_ji |n_rc| - s(n_rc)n_jc n_ri)/(|n_rc| d_j)
561 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
562 * 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)
565 void isl_tab_pivot(struct isl_tab *tab, int row, int col)
567 int i, j;
568 int sgn;
569 int t;
570 struct isl_mat *mat = tab->mat;
571 struct isl_tab_var *var;
573 isl_int_swap(mat->row[row][0], mat->row[row][2 + col]);
574 sgn = isl_int_sgn(mat->row[row][0]);
575 if (sgn < 0) {
576 isl_int_neg(mat->row[row][0], mat->row[row][0]);
577 isl_int_neg(mat->row[row][2 + col], mat->row[row][2 + col]);
578 } else
579 for (j = 0; j < 1 + tab->n_col; ++j) {
580 if (j == 1 + col)
581 continue;
582 isl_int_neg(mat->row[row][1 + j], mat->row[row][1 + j]);
584 if (!isl_int_is_one(mat->row[row][0]))
585 isl_seq_normalize(mat->row[row], 2 + tab->n_col);
586 for (i = 0; i < tab->n_row; ++i) {
587 if (i == row)
588 continue;
589 if (isl_int_is_zero(mat->row[i][2 + col]))
590 continue;
591 isl_int_mul(mat->row[i][0], mat->row[i][0], mat->row[row][0]);
592 for (j = 0; j < 1 + tab->n_col; ++j) {
593 if (j == 1 + col)
594 continue;
595 isl_int_mul(mat->row[i][1 + j],
596 mat->row[i][1 + j], mat->row[row][0]);
597 isl_int_addmul(mat->row[i][1 + j],
598 mat->row[i][2 + col], mat->row[row][1 + j]);
600 isl_int_mul(mat->row[i][2 + col],
601 mat->row[i][2 + col], mat->row[row][2 + col]);
602 if (!isl_int_is_one(mat->row[i][0]))
603 isl_seq_normalize(mat->row[i], 2 + tab->n_col);
605 t = tab->row_var[row];
606 tab->row_var[row] = tab->col_var[col];
607 tab->col_var[col] = t;
608 var = isl_tab_var_from_row(tab, row);
609 var->is_row = 1;
610 var->index = row;
611 var = var_from_col(tab, col);
612 var->is_row = 0;
613 var->index = col;
614 if (tab->in_undo)
615 return;
616 for (i = tab->n_redundant; i < tab->n_row; ++i) {
617 if (isl_int_is_zero(mat->row[i][2 + col]))
618 continue;
619 if (!isl_tab_var_from_row(tab, i)->frozen &&
620 isl_tab_row_is_redundant(tab, i))
621 if (isl_tab_mark_redundant(tab, i))
622 --i;
626 /* If "var" represents a column variable, then pivot is up (sgn > 0)
627 * or down (sgn < 0) to a row. The variable is assumed not to be
628 * unbounded in the specified direction.
629 * If sgn = 0, then the variable is unbounded in both directions,
630 * and we pivot with any row we can find.
632 static void to_row(struct isl_tab *tab, struct isl_tab_var *var, int sign)
634 int r;
636 if (var->is_row)
637 return;
639 if (sign == 0) {
640 for (r = tab->n_redundant; r < tab->n_row; ++r)
641 if (!isl_int_is_zero(tab->mat->row[r][2 + var->index]))
642 break;
643 isl_assert(tab->mat->ctx, r < tab->n_row, return);
644 } else {
645 r = pivot_row(tab, NULL, sign, var->index);
646 isl_assert(tab->mat->ctx, r >= 0, return);
649 isl_tab_pivot(tab, r, var->index);
652 static void check_table(struct isl_tab *tab)
654 int i;
656 if (tab->empty)
657 return;
658 for (i = 0; i < tab->n_row; ++i) {
659 if (!isl_tab_var_from_row(tab, i)->is_nonneg)
660 continue;
661 assert(!isl_int_is_neg(tab->mat->row[i][1]));
665 /* Return the sign of the maximal value of "var".
666 * If the sign is not negative, then on return from this function,
667 * the sample value will also be non-negative.
669 * If "var" is manifestly unbounded wrt positive values, we are done.
670 * Otherwise, we pivot the variable up to a row if needed
671 * Then we continue pivoting down until either
672 * - no more down pivots can be performed
673 * - the sample value is positive
674 * - the variable is pivoted into a manifestly unbounded column
676 static int sign_of_max(struct isl_tab *tab, struct isl_tab_var *var)
678 int row, col;
680 if (max_is_manifestly_unbounded(tab, var))
681 return 1;
682 to_row(tab, var, 1);
683 while (!isl_int_is_pos(tab->mat->row[var->index][1])) {
684 find_pivot(tab, var, var, 1, &row, &col);
685 if (row == -1)
686 return isl_int_sgn(tab->mat->row[var->index][1]);
687 isl_tab_pivot(tab, row, col);
688 if (!var->is_row) /* manifestly unbounded */
689 return 1;
691 return 1;
694 /* Perform pivots until the row variable "var" has a non-negative
695 * sample value or until no more upward pivots can be performed.
696 * Return the sign of the sample value after the pivots have been
697 * performed.
699 static int restore_row(struct isl_tab *tab, struct isl_tab_var *var)
701 int row, col;
703 while (isl_int_is_neg(tab->mat->row[var->index][1])) {
704 find_pivot(tab, var, var, 1, &row, &col);
705 if (row == -1)
706 break;
707 isl_tab_pivot(tab, row, col);
708 if (!var->is_row) /* manifestly unbounded */
709 return 1;
711 return isl_int_sgn(tab->mat->row[var->index][1]);
714 /* Perform pivots until we are sure that the row variable "var"
715 * can attain non-negative values. After return from this
716 * function, "var" is still a row variable, but its sample
717 * value may not be non-negative, even if the function returns 1.
719 static int at_least_zero(struct isl_tab *tab, struct isl_tab_var *var)
721 int row, col;
723 while (isl_int_is_neg(tab->mat->row[var->index][1])) {
724 find_pivot(tab, var, var, 1, &row, &col);
725 if (row == -1)
726 break;
727 if (row == var->index) /* manifestly unbounded */
728 return 1;
729 isl_tab_pivot(tab, row, col);
731 return !isl_int_is_neg(tab->mat->row[var->index][1]);
734 /* Return a negative value if "var" can attain negative values.
735 * Return a non-negative value otherwise.
737 * If "var" is manifestly unbounded wrt negative values, we are done.
738 * Otherwise, if var is in a column, we can pivot it down to a row.
739 * Then we continue pivoting down until either
740 * - the pivot would result in a manifestly unbounded column
741 * => we don't perform the pivot, but simply return -1
742 * - no more down pivots can be performed
743 * - the sample value is negative
744 * If the sample value becomes negative and the variable is supposed
745 * to be nonnegative, then we undo the last pivot.
746 * However, if the last pivot has made the pivoting variable
747 * obviously redundant, then it may have moved to another row.
748 * In that case we look for upward pivots until we reach a non-negative
749 * value again.
751 static int sign_of_min(struct isl_tab *tab, struct isl_tab_var *var)
753 int row, col;
754 struct isl_tab_var *pivot_var;
756 if (min_is_manifestly_unbounded(tab, var))
757 return -1;
758 if (!var->is_row) {
759 col = var->index;
760 row = pivot_row(tab, NULL, -1, col);
761 pivot_var = var_from_col(tab, col);
762 isl_tab_pivot(tab, row, col);
763 if (var->is_redundant)
764 return 0;
765 if (isl_int_is_neg(tab->mat->row[var->index][1])) {
766 if (var->is_nonneg) {
767 if (!pivot_var->is_redundant &&
768 pivot_var->index == row)
769 isl_tab_pivot(tab, row, col);
770 else
771 restore_row(tab, var);
773 return -1;
776 if (var->is_redundant)
777 return 0;
778 while (!isl_int_is_neg(tab->mat->row[var->index][1])) {
779 find_pivot(tab, var, var, -1, &row, &col);
780 if (row == var->index)
781 return -1;
782 if (row == -1)
783 return isl_int_sgn(tab->mat->row[var->index][1]);
784 pivot_var = var_from_col(tab, col);
785 isl_tab_pivot(tab, row, col);
786 if (var->is_redundant)
787 return 0;
789 if (var->is_nonneg) {
790 /* pivot back to non-negative value */
791 if (!pivot_var->is_redundant && pivot_var->index == row)
792 isl_tab_pivot(tab, row, col);
793 else
794 restore_row(tab, var);
796 return -1;
799 /* Return 1 if "var" can attain values <= -1.
800 * Return 0 otherwise.
802 * The sample value of "var" is assumed to be non-negative when the
803 * the function is called and will be made non-negative again before
804 * the function returns.
806 int isl_tab_min_at_most_neg_one(struct isl_tab *tab, struct isl_tab_var *var)
808 int row, col;
809 struct isl_tab_var *pivot_var;
811 if (min_is_manifestly_unbounded(tab, var))
812 return 1;
813 if (!var->is_row) {
814 col = var->index;
815 row = pivot_row(tab, NULL, -1, col);
816 pivot_var = var_from_col(tab, col);
817 isl_tab_pivot(tab, row, col);
818 if (var->is_redundant)
819 return 0;
820 if (isl_int_is_neg(tab->mat->row[var->index][1]) &&
821 isl_int_abs_ge(tab->mat->row[var->index][1],
822 tab->mat->row[var->index][0])) {
823 if (var->is_nonneg) {
824 if (!pivot_var->is_redundant &&
825 pivot_var->index == row)
826 isl_tab_pivot(tab, row, col);
827 else
828 restore_row(tab, var);
830 return 1;
833 if (var->is_redundant)
834 return 0;
835 do {
836 find_pivot(tab, var, var, -1, &row, &col);
837 if (row == var->index)
838 return 1;
839 if (row == -1)
840 return 0;
841 pivot_var = var_from_col(tab, col);
842 isl_tab_pivot(tab, row, col);
843 if (var->is_redundant)
844 return 0;
845 } while (!isl_int_is_neg(tab->mat->row[var->index][1]) ||
846 isl_int_abs_lt(tab->mat->row[var->index][1],
847 tab->mat->row[var->index][0]));
848 if (var->is_nonneg) {
849 /* pivot back to non-negative value */
850 if (!pivot_var->is_redundant && pivot_var->index == row)
851 isl_tab_pivot(tab, row, col);
852 restore_row(tab, var);
854 return 1;
857 /* Return 1 if "var" can attain values >= 1.
858 * Return 0 otherwise.
860 static int at_least_one(struct isl_tab *tab, struct isl_tab_var *var)
862 int row, col;
863 isl_int *r;
865 if (max_is_manifestly_unbounded(tab, var))
866 return 1;
867 to_row(tab, var, 1);
868 r = tab->mat->row[var->index];
869 while (isl_int_lt(r[1], r[0])) {
870 find_pivot(tab, var, var, 1, &row, &col);
871 if (row == -1)
872 return isl_int_ge(r[1], r[0]);
873 if (row == var->index) /* manifestly unbounded */
874 return 1;
875 isl_tab_pivot(tab, row, col);
877 return 1;
880 static void swap_cols(struct isl_tab *tab, int col1, int col2)
882 int t;
883 t = tab->col_var[col1];
884 tab->col_var[col1] = tab->col_var[col2];
885 tab->col_var[col2] = t;
886 var_from_col(tab, col1)->index = col1;
887 var_from_col(tab, col2)->index = col2;
888 tab->mat = isl_mat_swap_cols(tab->mat, 2 + col1, 2 + col2);
891 /* Mark column with index "col" as representing a zero variable.
892 * If we may need to undo the operation the column is kept,
893 * but no longer considered.
894 * Otherwise, the column is simply removed.
896 * The column may be interchanged with some other column. If it
897 * is interchanged with a later column, return 1. Otherwise return 0.
898 * If the columns are checked in order in the calling function,
899 * then a return value of 1 means that the column with the given
900 * column number may now contain a different column that
901 * hasn't been checked yet.
903 int isl_tab_kill_col(struct isl_tab *tab, int col)
905 var_from_col(tab, col)->is_zero = 1;
906 if (tab->need_undo) {
907 isl_tab_push_var(tab, isl_tab_undo_zero, var_from_col(tab, col));
908 if (col != tab->n_dead)
909 swap_cols(tab, col, tab->n_dead);
910 tab->n_dead++;
911 return 0;
912 } else {
913 if (col != tab->n_col - 1)
914 swap_cols(tab, col, tab->n_col - 1);
915 var_from_col(tab, tab->n_col - 1)->index = -1;
916 tab->n_col--;
917 return 1;
921 /* Row variable "var" is non-negative and cannot attain any values
922 * larger than zero. This means that the coefficients of the unrestricted
923 * column variables are zero and that the coefficients of the non-negative
924 * column variables are zero or negative.
925 * Each of the non-negative variables with a negative coefficient can
926 * then also be written as the negative sum of non-negative variables
927 * and must therefore also be zero.
929 static void close_row(struct isl_tab *tab, struct isl_tab_var *var)
931 int j;
932 struct isl_mat *mat = tab->mat;
934 isl_assert(tab->mat->ctx, var->is_nonneg, return);
935 var->is_zero = 1;
936 for (j = tab->n_dead; j < tab->n_col; ++j) {
937 if (isl_int_is_zero(mat->row[var->index][2 + j]))
938 continue;
939 isl_assert(tab->mat->ctx,
940 isl_int_is_neg(mat->row[var->index][2 + j]), return);
941 if (isl_tab_kill_col(tab, j))
942 --j;
944 isl_tab_mark_redundant(tab, var->index);
947 /* Add a constraint to the tableau and allocate a row for it.
948 * Return the index into the constraint array "con".
950 int isl_tab_allocate_con(struct isl_tab *tab)
952 int r;
954 isl_assert(tab->mat->ctx, tab->n_row < tab->mat->n_row, return -1);
956 r = tab->n_con;
957 tab->con[r].index = tab->n_row;
958 tab->con[r].is_row = 1;
959 tab->con[r].is_nonneg = 0;
960 tab->con[r].is_zero = 0;
961 tab->con[r].is_redundant = 0;
962 tab->con[r].frozen = 0;
963 tab->row_var[tab->n_row] = ~r;
965 tab->n_row++;
966 tab->n_con++;
967 isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->con[r]);
969 return r;
972 /* Add a variable to the tableau and allocate a column for it.
973 * Return the index into the variable array "var".
975 int isl_tab_allocate_var(struct isl_tab *tab)
977 int r;
978 int i;
979 unsigned off = 2;
981 isl_assert(tab->mat->ctx, tab->n_col < tab->mat->n_col, return -1);
982 isl_assert(tab->mat->ctx, tab->n_var < tab->max_var, return -1);
984 r = tab->n_var;
985 tab->var[r].index = tab->n_col;
986 tab->var[r].is_row = 0;
987 tab->var[r].is_nonneg = 0;
988 tab->var[r].is_zero = 0;
989 tab->var[r].is_redundant = 0;
990 tab->var[r].frozen = 0;
991 tab->col_var[tab->n_col] = r;
993 for (i = 0; i < tab->n_row; ++i)
994 isl_int_set_si(tab->mat->row[i][off + tab->n_col], 0);
996 tab->n_var++;
997 tab->n_col++;
998 isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->var[r]);
1000 return r;
1003 /* Add a row to the tableau. The row is given as an affine combination
1004 * of the original variables and needs to be expressed in terms of the
1005 * column variables.
1007 * We add each term in turn.
1008 * If r = n/d_r is the current sum and we need to add k x, then
1009 * if x is a column variable, we increase the numerator of
1010 * this column by k d_r
1011 * if x = f/d_x is a row variable, then the new representation of r is
1013 * n k f d_x/g n + d_r/g k f m/d_r n + m/d_g k f
1014 * --- + --- = ------------------- = -------------------
1015 * d_r d_r d_r d_x/g m
1017 * with g the gcd of d_r and d_x and m the lcm of d_r and d_x.
1019 int isl_tab_add_row(struct isl_tab *tab, isl_int *line)
1021 int i;
1022 int r;
1023 isl_int *row;
1024 isl_int a, b;
1026 r = isl_tab_allocate_con(tab);
1027 if (r < 0)
1028 return -1;
1030 isl_int_init(a);
1031 isl_int_init(b);
1032 row = tab->mat->row[tab->con[r].index];
1033 isl_int_set_si(row[0], 1);
1034 isl_int_set(row[1], line[0]);
1035 isl_seq_clr(row + 2, tab->n_col);
1036 for (i = 0; i < tab->n_var; ++i) {
1037 if (tab->var[i].is_zero)
1038 continue;
1039 if (tab->var[i].is_row) {
1040 isl_int_lcm(a,
1041 row[0], tab->mat->row[tab->var[i].index][0]);
1042 isl_int_swap(a, row[0]);
1043 isl_int_divexact(a, row[0], a);
1044 isl_int_divexact(b,
1045 row[0], tab->mat->row[tab->var[i].index][0]);
1046 isl_int_mul(b, b, line[1 + i]);
1047 isl_seq_combine(row + 1, a, row + 1,
1048 b, tab->mat->row[tab->var[i].index] + 1,
1049 1 + tab->n_col);
1050 } else
1051 isl_int_addmul(row[2 + tab->var[i].index],
1052 line[1 + i], row[0]);
1054 isl_seq_normalize(row, 2 + tab->n_col);
1055 isl_int_clear(a);
1056 isl_int_clear(b);
1058 return r;
1061 static int drop_row(struct isl_tab *tab, int row)
1063 isl_assert(tab->mat->ctx, ~tab->row_var[row] == tab->n_con - 1, return -1);
1064 if (row != tab->n_row - 1)
1065 swap_rows(tab, row, tab->n_row - 1);
1066 tab->n_row--;
1067 tab->n_con--;
1068 return 0;
1071 static int drop_col(struct isl_tab *tab, int col)
1073 isl_assert(tab->mat->ctx, tab->col_var[col] == tab->n_var - 1, return -1);
1074 if (col != tab->n_col - 1)
1075 swap_cols(tab, col, tab->n_col - 1);
1076 tab->n_col--;
1077 tab->n_var--;
1078 return 0;
1081 /* Add inequality "ineq" and check if it conflicts with the
1082 * previously added constraints or if it is obviously redundant.
1084 struct isl_tab *isl_tab_add_ineq(struct isl_tab *tab, isl_int *ineq)
1086 int r;
1087 int sgn;
1089 if (!tab)
1090 return NULL;
1091 r = isl_tab_add_row(tab, ineq);
1092 if (r < 0)
1093 goto error;
1094 tab->con[r].is_nonneg = 1;
1095 isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]);
1096 if (isl_tab_row_is_redundant(tab, tab->con[r].index)) {
1097 isl_tab_mark_redundant(tab, tab->con[r].index);
1098 return tab;
1101 sgn = restore_row(tab, &tab->con[r]);
1102 if (sgn < 0)
1103 return isl_tab_mark_empty(tab);
1104 if (tab->con[r].is_row && isl_tab_row_is_redundant(tab, tab->con[r].index))
1105 isl_tab_mark_redundant(tab, tab->con[r].index);
1106 return tab;
1107 error:
1108 isl_tab_free(tab);
1109 return NULL;
1112 /* Pivot a non-negative variable down until it reaches the value zero
1113 * and then pivot the variable into a column position.
1115 int to_col(struct isl_tab *tab, struct isl_tab_var *var)
1117 int i;
1118 int row, col;
1120 if (!var->is_row)
1121 return;
1123 while (isl_int_is_pos(tab->mat->row[var->index][1])) {
1124 find_pivot(tab, var, NULL, -1, &row, &col);
1125 isl_assert(tab->mat->ctx, row != -1, return -1);
1126 isl_tab_pivot(tab, row, col);
1127 if (!var->is_row)
1128 return;
1131 for (i = tab->n_dead; i < tab->n_col; ++i)
1132 if (!isl_int_is_zero(tab->mat->row[var->index][2 + i]))
1133 break;
1135 isl_assert(tab->mat->ctx, i < tab->n_col, return -1);
1136 isl_tab_pivot(tab, var->index, i);
1138 return 0;
1141 /* We assume Gaussian elimination has been performed on the equalities.
1142 * The equalities can therefore never conflict.
1143 * Adding the equalities is currently only really useful for a later call
1144 * to isl_tab_ineq_type.
1146 static struct isl_tab *add_eq(struct isl_tab *tab, isl_int *eq)
1148 int i;
1149 int r;
1151 if (!tab)
1152 return NULL;
1153 r = isl_tab_add_row(tab, eq);
1154 if (r < 0)
1155 goto error;
1157 r = tab->con[r].index;
1158 i = isl_seq_first_non_zero(tab->mat->row[r] + 2 + tab->n_dead,
1159 tab->n_col - tab->n_dead);
1160 isl_assert(tab->mat->ctx, i >= 0, goto error);
1161 i += tab->n_dead;
1162 isl_tab_pivot(tab, r, i);
1163 isl_tab_kill_col(tab, i);
1164 tab->n_eq++;
1166 return tab;
1167 error:
1168 isl_tab_free(tab);
1169 return NULL;
1172 /* Add an equality that is known to be valid for the given tableau.
1174 struct isl_tab *isl_tab_add_valid_eq(struct isl_tab *tab, isl_int *eq)
1176 struct isl_tab_var *var;
1177 int i;
1178 int r;
1180 if (!tab)
1181 return NULL;
1182 r = isl_tab_add_row(tab, eq);
1183 if (r < 0)
1184 goto error;
1186 var = &tab->con[r];
1187 r = var->index;
1188 if (isl_int_is_neg(tab->mat->row[r][1]))
1189 isl_seq_neg(tab->mat->row[r] + 1, tab->mat->row[r] + 1,
1190 1 + tab->n_col);
1191 var->is_nonneg = 1;
1192 if (to_col(tab, var) < 0)
1193 goto error;
1194 var->is_nonneg = 0;
1195 isl_tab_kill_col(tab, var->index);
1197 return tab;
1198 error:
1199 isl_tab_free(tab);
1200 return NULL;
1203 struct isl_tab *isl_tab_from_basic_map(struct isl_basic_map *bmap)
1205 int i;
1206 struct isl_tab *tab;
1208 if (!bmap)
1209 return NULL;
1210 tab = isl_tab_alloc(bmap->ctx,
1211 isl_basic_map_total_dim(bmap) + bmap->n_ineq + 1,
1212 isl_basic_map_total_dim(bmap));
1213 if (!tab)
1214 return NULL;
1215 tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
1216 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1217 return isl_tab_mark_empty(tab);
1218 for (i = 0; i < bmap->n_eq; ++i) {
1219 tab = add_eq(tab, bmap->eq[i]);
1220 if (!tab)
1221 return tab;
1223 for (i = 0; i < bmap->n_ineq; ++i) {
1224 tab = isl_tab_add_ineq(tab, bmap->ineq[i]);
1225 if (!tab || tab->empty)
1226 return tab;
1228 return tab;
1231 struct isl_tab *isl_tab_from_basic_set(struct isl_basic_set *bset)
1233 return isl_tab_from_basic_map((struct isl_basic_map *)bset);
1236 /* Construct a tableau corresponding to the recession cone of "bmap".
1238 struct isl_tab *isl_tab_from_recession_cone(struct isl_basic_map *bmap)
1240 isl_int cst;
1241 int i;
1242 struct isl_tab *tab;
1244 if (!bmap)
1245 return NULL;
1246 tab = isl_tab_alloc(bmap->ctx, bmap->n_eq + bmap->n_ineq,
1247 isl_basic_map_total_dim(bmap));
1248 if (!tab)
1249 return NULL;
1250 tab->rational = ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
1252 isl_int_init(cst);
1253 for (i = 0; i < bmap->n_eq; ++i) {
1254 isl_int_swap(bmap->eq[i][0], cst);
1255 tab = add_eq(tab, bmap->eq[i]);
1256 isl_int_swap(bmap->eq[i][0], cst);
1257 if (!tab)
1258 goto done;
1260 for (i = 0; i < bmap->n_ineq; ++i) {
1261 int r;
1262 isl_int_swap(bmap->ineq[i][0], cst);
1263 r = isl_tab_add_row(tab, bmap->ineq[i]);
1264 isl_int_swap(bmap->ineq[i][0], cst);
1265 if (r < 0)
1266 goto error;
1267 tab->con[r].is_nonneg = 1;
1268 isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]);
1270 done:
1271 isl_int_clear(cst);
1272 return tab;
1273 error:
1274 isl_int_clear(cst);
1275 isl_tab_free(tab);
1276 return NULL;
1279 /* Assuming "tab" is the tableau of a cone, check if the cone is
1280 * bounded, i.e., if it is empty or only contains the origin.
1282 int isl_tab_cone_is_bounded(struct isl_tab *tab)
1284 int i;
1286 if (!tab)
1287 return -1;
1288 if (tab->empty)
1289 return 1;
1290 if (tab->n_dead == tab->n_col)
1291 return 1;
1293 for (;;) {
1294 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1295 struct isl_tab_var *var;
1296 var = isl_tab_var_from_row(tab, i);
1297 if (!var->is_nonneg)
1298 continue;
1299 if (sign_of_max(tab, var) != 0)
1300 return 0;
1301 close_row(tab, var);
1302 break;
1304 if (tab->n_dead == tab->n_col)
1305 return 1;
1306 if (i == tab->n_row)
1307 return 0;
1311 int isl_tab_sample_is_integer(struct isl_tab *tab)
1313 int i;
1315 if (!tab)
1316 return -1;
1318 for (i = 0; i < tab->n_var; ++i) {
1319 int row;
1320 if (!tab->var[i].is_row)
1321 continue;
1322 row = tab->var[i].index;
1323 if (!isl_int_is_divisible_by(tab->mat->row[row][1],
1324 tab->mat->row[row][0]))
1325 return 0;
1327 return 1;
1330 static struct isl_vec *extract_integer_sample(struct isl_tab *tab)
1332 int i;
1333 struct isl_vec *vec;
1335 vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
1336 if (!vec)
1337 return NULL;
1339 isl_int_set_si(vec->block.data[0], 1);
1340 for (i = 0; i < tab->n_var; ++i) {
1341 if (!tab->var[i].is_row)
1342 isl_int_set_si(vec->block.data[1 + i], 0);
1343 else {
1344 int row = tab->var[i].index;
1345 isl_int_divexact(vec->block.data[1 + i],
1346 tab->mat->row[row][1], tab->mat->row[row][0]);
1350 return vec;
1353 struct isl_vec *isl_tab_get_sample_value(struct isl_tab *tab)
1355 int i;
1356 struct isl_vec *vec;
1357 isl_int m;
1359 if (!tab)
1360 return NULL;
1362 vec = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
1363 if (!vec)
1364 return NULL;
1366 isl_int_init(m);
1368 isl_int_set_si(vec->block.data[0], 1);
1369 for (i = 0; i < tab->n_var; ++i) {
1370 int row;
1371 if (!tab->var[i].is_row) {
1372 isl_int_set_si(vec->block.data[1 + i], 0);
1373 continue;
1375 row = tab->var[i].index;
1376 isl_int_gcd(m, vec->block.data[0], tab->mat->row[row][0]);
1377 isl_int_divexact(m, tab->mat->row[row][0], m);
1378 isl_seq_scale(vec->block.data, vec->block.data, m, 1 + i);
1379 isl_int_divexact(m, vec->block.data[0], tab->mat->row[row][0]);
1380 isl_int_mul(vec->block.data[1 + i], m, tab->mat->row[row][1]);
1382 isl_seq_normalize(vec->block.data, vec->size);
1384 isl_int_clear(m);
1385 return vec;
1388 /* Update "bmap" based on the results of the tableau "tab".
1389 * In particular, implicit equalities are made explicit, redundant constraints
1390 * are removed and if the sample value happens to be integer, it is stored
1391 * in "bmap" (unless "bmap" already had an integer sample).
1393 * The tableau is assumed to have been created from "bmap" using
1394 * isl_tab_from_basic_map.
1396 struct isl_basic_map *isl_basic_map_update_from_tab(struct isl_basic_map *bmap,
1397 struct isl_tab *tab)
1399 int i;
1400 unsigned n_eq;
1402 if (!bmap)
1403 return NULL;
1404 if (!tab)
1405 return bmap;
1407 n_eq = tab->n_eq;
1408 if (tab->empty)
1409 bmap = isl_basic_map_set_to_empty(bmap);
1410 else
1411 for (i = bmap->n_ineq - 1; i >= 0; --i) {
1412 if (isl_tab_is_equality(tab, n_eq + i))
1413 isl_basic_map_inequality_to_equality(bmap, i);
1414 else if (isl_tab_is_redundant(tab, n_eq + i))
1415 isl_basic_map_drop_inequality(bmap, i);
1417 if (!tab->rational &&
1418 !bmap->sample && isl_tab_sample_is_integer(tab))
1419 bmap->sample = extract_integer_sample(tab);
1420 return bmap;
1423 struct isl_basic_set *isl_basic_set_update_from_tab(struct isl_basic_set *bset,
1424 struct isl_tab *tab)
1426 return (struct isl_basic_set *)isl_basic_map_update_from_tab(
1427 (struct isl_basic_map *)bset, tab);
1430 /* Given a non-negative variable "var", add a new non-negative variable
1431 * that is the opposite of "var", ensuring that var can only attain the
1432 * value zero.
1433 * If var = n/d is a row variable, then the new variable = -n/d.
1434 * If var is a column variables, then the new variable = -var.
1435 * If the new variable cannot attain non-negative values, then
1436 * the resulting tableau is empty.
1437 * Otherwise, we know the value will be zero and we close the row.
1439 static struct isl_tab *cut_to_hyperplane(struct isl_tab *tab,
1440 struct isl_tab_var *var)
1442 unsigned r;
1443 isl_int *row;
1444 int sgn;
1446 if (isl_tab_extend_cons(tab, 1) < 0)
1447 goto error;
1449 r = tab->n_con;
1450 tab->con[r].index = tab->n_row;
1451 tab->con[r].is_row = 1;
1452 tab->con[r].is_nonneg = 0;
1453 tab->con[r].is_zero = 0;
1454 tab->con[r].is_redundant = 0;
1455 tab->con[r].frozen = 0;
1456 tab->row_var[tab->n_row] = ~r;
1457 row = tab->mat->row[tab->n_row];
1459 if (var->is_row) {
1460 isl_int_set(row[0], tab->mat->row[var->index][0]);
1461 isl_seq_neg(row + 1,
1462 tab->mat->row[var->index] + 1, 1 + tab->n_col);
1463 } else {
1464 isl_int_set_si(row[0], 1);
1465 isl_seq_clr(row + 1, 1 + tab->n_col);
1466 isl_int_set_si(row[2 + var->index], -1);
1469 tab->n_row++;
1470 tab->n_con++;
1471 isl_tab_push_var(tab, isl_tab_undo_allocate, &tab->con[r]);
1473 sgn = sign_of_max(tab, &tab->con[r]);
1474 if (sgn < 0)
1475 return isl_tab_mark_empty(tab);
1476 tab->con[r].is_nonneg = 1;
1477 isl_tab_push_var(tab, isl_tab_undo_nonneg, &tab->con[r]);
1478 /* sgn == 0 */
1479 close_row(tab, &tab->con[r]);
1481 return tab;
1482 error:
1483 isl_tab_free(tab);
1484 return NULL;
1487 /* Given a tableau "tab" and an inequality constraint "con" of the tableau,
1488 * relax the inequality by one. That is, the inequality r >= 0 is replaced
1489 * by r' = r + 1 >= 0.
1490 * If r is a row variable, we simply increase the constant term by one
1491 * (taking into account the denominator).
1492 * If r is a column variable, then we need to modify each row that
1493 * refers to r = r' - 1 by substituting this equality, effectively
1494 * subtracting the coefficient of the column from the constant.
1496 struct isl_tab *isl_tab_relax(struct isl_tab *tab, int con)
1498 struct isl_tab_var *var;
1499 if (!tab)
1500 return NULL;
1502 var = &tab->con[con];
1504 if (!var->is_row && !max_is_manifestly_unbounded(tab, var))
1505 to_row(tab, var, 1);
1507 if (var->is_row)
1508 isl_int_add(tab->mat->row[var->index][1],
1509 tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
1510 else {
1511 int i;
1513 for (i = 0; i < tab->n_row; ++i) {
1514 if (isl_int_is_zero(tab->mat->row[i][2 + var->index]))
1515 continue;
1516 isl_int_sub(tab->mat->row[i][1], tab->mat->row[i][1],
1517 tab->mat->row[i][2 + var->index]);
1522 isl_tab_push_var(tab, isl_tab_undo_relax, var);
1524 return tab;
1527 struct isl_tab *isl_tab_select_facet(struct isl_tab *tab, int con)
1529 if (!tab)
1530 return NULL;
1532 return cut_to_hyperplane(tab, &tab->con[con]);
1535 static int may_be_equality(struct isl_tab *tab, int row)
1537 return (tab->rational ? isl_int_is_zero(tab->mat->row[row][1])
1538 : isl_int_lt(tab->mat->row[row][1],
1539 tab->mat->row[row][0])) &&
1540 isl_seq_first_non_zero(tab->mat->row[row] + 2 + tab->n_dead,
1541 tab->n_col - tab->n_dead) != -1;
1544 /* Check for (near) equalities among the constraints.
1545 * A constraint is an equality if it is non-negative and if
1546 * its maximal value is either
1547 * - zero (in case of rational tableaus), or
1548 * - strictly less than 1 (in case of integer tableaus)
1550 * We first mark all non-redundant and non-dead variables that
1551 * are not frozen and not obviously not an equality.
1552 * Then we iterate over all marked variables if they can attain
1553 * any values larger than zero or at least one.
1554 * If the maximal value is zero, we mark any column variables
1555 * that appear in the row as being zero and mark the row as being redundant.
1556 * Otherwise, if the maximal value is strictly less than one (and the
1557 * tableau is integer), then we restrict the value to being zero
1558 * by adding an opposite non-negative variable.
1560 struct isl_tab *isl_tab_detect_equalities(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_dead == tab->n_col)
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 may_be_equality(tab, i);
1577 if (var->marked)
1578 n_marked++;
1580 for (i = tab->n_dead; i < tab->n_col; ++i) {
1581 struct isl_tab_var *var = var_from_col(tab, i);
1582 var->marked = !var->frozen && var->is_nonneg;
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 (sign_of_max(tab, var) == 0)
1605 close_row(tab, var);
1606 else if (!tab->rational && !at_least_one(tab, var)) {
1607 tab = cut_to_hyperplane(tab, var);
1608 return isl_tab_detect_equalities(tab);
1610 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1611 var = isl_tab_var_from_row(tab, i);
1612 if (!var->marked)
1613 continue;
1614 if (may_be_equality(tab, i))
1615 continue;
1616 var->marked = 0;
1617 n_marked--;
1621 return tab;
1624 /* Check for (near) redundant constraints.
1625 * A constraint is redundant if it is non-negative and if
1626 * its minimal value (temporarily ignoring the non-negativity) is either
1627 * - zero (in case of rational tableaus), or
1628 * - strictly larger than -1 (in case of integer tableaus)
1630 * We first mark all non-redundant and non-dead variables that
1631 * are not frozen and not obviously negatively unbounded.
1632 * Then we iterate over all marked variables if they can attain
1633 * any values smaller than zero or at most negative one.
1634 * If not, we mark the row as being redundant (assuming it hasn't
1635 * been detected as being obviously redundant in the mean time).
1637 struct isl_tab *isl_tab_detect_redundant(struct isl_tab *tab)
1639 int i;
1640 unsigned n_marked;
1642 if (!tab)
1643 return NULL;
1644 if (tab->empty)
1645 return tab;
1646 if (tab->n_redundant == tab->n_row)
1647 return tab;
1649 n_marked = 0;
1650 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1651 struct isl_tab_var *var = isl_tab_var_from_row(tab, i);
1652 var->marked = !var->frozen && var->is_nonneg;
1653 if (var->marked)
1654 n_marked++;
1656 for (i = tab->n_dead; i < tab->n_col; ++i) {
1657 struct isl_tab_var *var = var_from_col(tab, i);
1658 var->marked = !var->frozen && var->is_nonneg &&
1659 !min_is_manifestly_unbounded(tab, var);
1660 if (var->marked)
1661 n_marked++;
1663 while (n_marked) {
1664 struct isl_tab_var *var;
1665 for (i = tab->n_redundant; i < tab->n_row; ++i) {
1666 var = isl_tab_var_from_row(tab, i);
1667 if (var->marked)
1668 break;
1670 if (i == tab->n_row) {
1671 for (i = tab->n_dead; i < tab->n_col; ++i) {
1672 var = var_from_col(tab, i);
1673 if (var->marked)
1674 break;
1676 if (i == tab->n_col)
1677 break;
1679 var->marked = 0;
1680 n_marked--;
1681 if ((tab->rational ? (sign_of_min(tab, var) >= 0)
1682 : !isl_tab_min_at_most_neg_one(tab, var)) &&
1683 !var->is_redundant)
1684 isl_tab_mark_redundant(tab, var->index);
1685 for (i = tab->n_dead; i < tab->n_col; ++i) {
1686 var = var_from_col(tab, i);
1687 if (!var->marked)
1688 continue;
1689 if (!min_is_manifestly_unbounded(tab, var))
1690 continue;
1691 var->marked = 0;
1692 n_marked--;
1696 return tab;
1699 int isl_tab_is_equality(struct isl_tab *tab, int con)
1701 int row;
1703 if (!tab)
1704 return -1;
1705 if (tab->con[con].is_zero)
1706 return 1;
1707 if (tab->con[con].is_redundant)
1708 return 0;
1709 if (!tab->con[con].is_row)
1710 return tab->con[con].index < tab->n_dead;
1712 row = tab->con[con].index;
1714 return isl_int_is_zero(tab->mat->row[row][1]) &&
1715 isl_seq_first_non_zero(tab->mat->row[row] + 2 + tab->n_dead,
1716 tab->n_col - tab->n_dead) == -1;
1719 /* Return the minimial value of the affine expression "f" with denominator
1720 * "denom" in *opt, *opt_denom, assuming the tableau is not empty and
1721 * the expression cannot attain arbitrarily small values.
1722 * If opt_denom is NULL, then *opt is rounded up to the nearest integer.
1723 * The return value reflects the nature of the result (empty, unbounded,
1724 * minmimal value returned in *opt).
1726 enum isl_lp_result isl_tab_min(struct isl_tab *tab,
1727 isl_int *f, isl_int denom, isl_int *opt, isl_int *opt_denom,
1728 unsigned flags)
1730 int r;
1731 enum isl_lp_result res = isl_lp_ok;
1732 struct isl_tab_var *var;
1733 struct isl_tab_undo *snap;
1735 if (tab->empty)
1736 return isl_lp_empty;
1738 snap = isl_tab_snap(tab);
1739 r = isl_tab_add_row(tab, f);
1740 if (r < 0)
1741 return isl_lp_error;
1742 var = &tab->con[r];
1743 isl_int_mul(tab->mat->row[var->index][0],
1744 tab->mat->row[var->index][0], denom);
1745 for (;;) {
1746 int row, col;
1747 find_pivot(tab, var, var, -1, &row, &col);
1748 if (row == var->index) {
1749 res = isl_lp_unbounded;
1750 break;
1752 if (row == -1)
1753 break;
1754 isl_tab_pivot(tab, row, col);
1756 if (isl_tab_rollback(tab, snap) < 0)
1757 return isl_lp_error;
1758 if (ISL_FL_ISSET(flags, ISL_TAB_SAVE_DUAL)) {
1759 int i;
1761 isl_vec_free(tab->dual);
1762 tab->dual = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_con);
1763 if (!tab->dual)
1764 return isl_lp_error;
1765 isl_int_set(tab->dual->el[0], tab->mat->row[var->index][0]);
1766 for (i = 0; i < tab->n_con; ++i) {
1767 if (tab->con[i].is_row)
1768 isl_int_set_si(tab->dual->el[1 + i], 0);
1769 else {
1770 int pos = 2 + tab->con[i].index;
1771 isl_int_set(tab->dual->el[1 + i],
1772 tab->mat->row[var->index][pos]);
1776 if (res == isl_lp_ok) {
1777 if (opt_denom) {
1778 isl_int_set(*opt, tab->mat->row[var->index][1]);
1779 isl_int_set(*opt_denom, tab->mat->row[var->index][0]);
1780 } else
1781 isl_int_cdiv_q(*opt, tab->mat->row[var->index][1],
1782 tab->mat->row[var->index][0]);
1784 return res;
1787 int isl_tab_is_redundant(struct isl_tab *tab, int con)
1789 int row;
1790 unsigned n_col;
1792 if (!tab)
1793 return -1;
1794 if (tab->con[con].is_zero)
1795 return 0;
1796 if (tab->con[con].is_redundant)
1797 return 1;
1798 return tab->con[con].is_row && tab->con[con].index < tab->n_redundant;
1801 /* Take a snapshot of the tableau that can be restored by s call to
1802 * isl_tab_rollback.
1804 struct isl_tab_undo *isl_tab_snap(struct isl_tab *tab)
1806 if (!tab)
1807 return NULL;
1808 tab->need_undo = 1;
1809 return tab->top;
1812 /* Undo the operation performed by isl_tab_relax.
1814 static void unrelax(struct isl_tab *tab, struct isl_tab_var *var)
1816 if (!var->is_row && !max_is_manifestly_unbounded(tab, var))
1817 to_row(tab, var, 1);
1819 if (var->is_row)
1820 isl_int_sub(tab->mat->row[var->index][1],
1821 tab->mat->row[var->index][1], tab->mat->row[var->index][0]);
1822 else {
1823 int i;
1825 for (i = 0; i < tab->n_row; ++i) {
1826 if (isl_int_is_zero(tab->mat->row[i][2 + var->index]))
1827 continue;
1828 isl_int_add(tab->mat->row[i][1], tab->mat->row[i][1],
1829 tab->mat->row[i][2 + var->index]);
1835 static void perform_undo_var(struct isl_tab *tab, struct isl_tab_undo *undo)
1837 struct isl_tab_var *var = var_from_index(tab, undo->u.var_index);
1838 switch(undo->type) {
1839 case isl_tab_undo_nonneg:
1840 var->is_nonneg = 0;
1841 break;
1842 case isl_tab_undo_redundant:
1843 var->is_redundant = 0;
1844 tab->n_redundant--;
1845 break;
1846 case isl_tab_undo_zero:
1847 var->is_zero = 0;
1848 tab->n_dead--;
1849 break;
1850 case isl_tab_undo_allocate:
1851 if (undo->u.var_index >= 0) {
1852 isl_assert(tab->mat->ctx, !var->is_row, return);
1853 drop_col(tab, var->index);
1854 break;
1856 if (!var->is_row) {
1857 if (!max_is_manifestly_unbounded(tab, var))
1858 to_row(tab, var, 1);
1859 else if (!min_is_manifestly_unbounded(tab, var))
1860 to_row(tab, var, -1);
1861 else
1862 to_row(tab, var, 0);
1864 drop_row(tab, var->index);
1865 break;
1866 case isl_tab_undo_relax:
1867 unrelax(tab, var);
1868 break;
1872 /* Restore the tableau to the state where the basic variables
1873 * are those in "col_var".
1874 * We first construct a list of variables that are currently in
1875 * the basis, but shouldn't. Then we iterate over all variables
1876 * that should be in the basis and for each one that is currently
1877 * not in the basis, we exchange it with one of the elements of the
1878 * list constructed before.
1879 * We can always find an appropriate variable to pivot with because
1880 * the current basis is mapped to the old basis by a non-singular
1881 * matrix and so we can never end up with a zero row.
1883 static int restore_basis(struct isl_tab *tab, int *col_var)
1885 int i, j;
1886 int n_extra = 0;
1887 int *extra = NULL; /* current columns that contain bad stuff */
1888 unsigned off = 2;
1890 extra = isl_alloc_array(tab->mat->ctx, int, tab->n_col);
1891 if (!extra)
1892 goto error;
1893 for (i = 0; i < tab->n_col; ++i) {
1894 for (j = 0; j < tab->n_col; ++j)
1895 if (tab->col_var[i] == col_var[j])
1896 break;
1897 if (j < tab->n_col)
1898 continue;
1899 extra[n_extra++] = i;
1901 for (i = 0; i < tab->n_col && n_extra > 0; ++i) {
1902 struct isl_tab_var *var;
1903 int row;
1905 for (j = 0; j < tab->n_col; ++j)
1906 if (col_var[i] == tab->col_var[j])
1907 break;
1908 if (j < tab->n_col)
1909 continue;
1910 var = var_from_index(tab, col_var[i]);
1911 row = var->index;
1912 for (j = 0; j < n_extra; ++j)
1913 if (!isl_int_is_zero(tab->mat->row[row][off+extra[j]]))
1914 break;
1915 isl_assert(tab->mat->ctx, j < n_extra, goto error);
1916 isl_tab_pivot(tab, row, extra[j]);
1917 extra[j] = extra[--n_extra];
1920 free(extra);
1921 free(col_var);
1922 return 0;
1923 error:
1924 free(extra);
1925 free(col_var);
1926 return -1;
1929 static int perform_undo(struct isl_tab *tab, struct isl_tab_undo *undo)
1931 switch (undo->type) {
1932 case isl_tab_undo_empty:
1933 tab->empty = 0;
1934 break;
1935 case isl_tab_undo_nonneg:
1936 case isl_tab_undo_redundant:
1937 case isl_tab_undo_zero:
1938 case isl_tab_undo_allocate:
1939 case isl_tab_undo_relax:
1940 perform_undo_var(tab, undo);
1941 break;
1942 case isl_tab_undo_saved_basis:
1943 if (restore_basis(tab, undo->u.col_var) < 0)
1944 return -1;
1945 break;
1946 default:
1947 isl_assert(tab->mat->ctx, 0, return -1);
1949 return 0;
1952 /* Return the tableau to the state it was in when the snapshot "snap"
1953 * was taken.
1955 int isl_tab_rollback(struct isl_tab *tab, struct isl_tab_undo *snap)
1957 struct isl_tab_undo *undo, *next;
1959 if (!tab)
1960 return -1;
1962 tab->in_undo = 1;
1963 for (undo = tab->top; undo && undo != &tab->bottom; undo = next) {
1964 next = undo->next;
1965 if (undo == snap)
1966 break;
1967 if (perform_undo(tab, undo) < 0) {
1968 free_undo(tab);
1969 tab->in_undo = 0;
1970 return -1;
1972 free(undo);
1974 tab->in_undo = 0;
1975 tab->top = undo;
1976 if (!undo)
1977 return -1;
1978 return 0;
1981 /* The given row "row" represents an inequality violated by all
1982 * points in the tableau. Check for some special cases of such
1983 * separating constraints.
1984 * In particular, if the row has been reduced to the constant -1,
1985 * then we know the inequality is adjacent (but opposite) to
1986 * an equality in the tableau.
1987 * If the row has been reduced to r = -1 -r', with r' an inequality
1988 * of the tableau, then the inequality is adjacent (but opposite)
1989 * to the inequality r'.
1991 static enum isl_ineq_type separation_type(struct isl_tab *tab, unsigned row)
1993 int pos;
1995 if (tab->rational)
1996 return isl_ineq_separate;
1998 if (!isl_int_is_one(tab->mat->row[row][0]))
1999 return isl_ineq_separate;
2000 if (!isl_int_is_negone(tab->mat->row[row][1]))
2001 return isl_ineq_separate;
2003 pos = isl_seq_first_non_zero(tab->mat->row[row] + 2 + tab->n_dead,
2004 tab->n_col - tab->n_dead);
2005 if (pos == -1)
2006 return isl_ineq_adj_eq;
2008 if (!isl_int_is_negone(tab->mat->row[row][2 + tab->n_dead + pos]))
2009 return isl_ineq_separate;
2011 pos = isl_seq_first_non_zero(
2012 tab->mat->row[row] + 2 + tab->n_dead + pos + 1,
2013 tab->n_col - tab->n_dead - pos - 1);
2015 return pos == -1 ? isl_ineq_adj_ineq : isl_ineq_separate;
2018 /* Check the effect of inequality "ineq" on the tableau "tab".
2019 * The result may be
2020 * isl_ineq_redundant: satisfied by all points in the tableau
2021 * isl_ineq_separate: satisfied by no point in the tableau
2022 * isl_ineq_cut: satisfied by some by not all points
2023 * isl_ineq_adj_eq: adjacent to an equality
2024 * isl_ineq_adj_ineq: adjacent to an inequality.
2026 enum isl_ineq_type isl_tab_ineq_type(struct isl_tab *tab, isl_int *ineq)
2028 enum isl_ineq_type type = isl_ineq_error;
2029 struct isl_tab_undo *snap = NULL;
2030 int con;
2031 int row;
2033 if (!tab)
2034 return isl_ineq_error;
2036 if (isl_tab_extend_cons(tab, 1) < 0)
2037 return isl_ineq_error;
2039 snap = isl_tab_snap(tab);
2041 con = isl_tab_add_row(tab, ineq);
2042 if (con < 0)
2043 goto error;
2045 row = tab->con[con].index;
2046 if (isl_tab_row_is_redundant(tab, row))
2047 type = isl_ineq_redundant;
2048 else if (isl_int_is_neg(tab->mat->row[row][1]) &&
2049 (tab->rational ||
2050 isl_int_abs_ge(tab->mat->row[row][1],
2051 tab->mat->row[row][0]))) {
2052 if (at_least_zero(tab, &tab->con[con]))
2053 type = isl_ineq_cut;
2054 else
2055 type = separation_type(tab, row);
2056 } else if (tab->rational ? (sign_of_min(tab, &tab->con[con]) < 0)
2057 : isl_tab_min_at_most_neg_one(tab, &tab->con[con]))
2058 type = isl_ineq_cut;
2059 else
2060 type = isl_ineq_redundant;
2062 if (isl_tab_rollback(tab, snap))
2063 return isl_ineq_error;
2064 return type;
2065 error:
2066 isl_tab_rollback(tab, snap);
2067 return isl_ineq_error;
2070 void isl_tab_dump(struct isl_tab *tab, FILE *out, int indent)
2072 unsigned r, c;
2073 int i;
2075 if (!tab) {
2076 fprintf(out, "%*snull tab\n", indent, "");
2077 return;
2079 fprintf(out, "%*sn_redundant: %d, n_dead: %d", indent, "",
2080 tab->n_redundant, tab->n_dead);
2081 if (tab->rational)
2082 fprintf(out, ", rational");
2083 if (tab->empty)
2084 fprintf(out, ", empty");
2085 fprintf(out, "\n");
2086 fprintf(out, "%*s[", indent, "");
2087 for (i = 0; i < tab->n_var; ++i) {
2088 if (i)
2089 fprintf(out, (i == tab->n_param ||
2090 i == tab->n_var - tab->n_div) ? "; "
2091 : ", ");
2092 fprintf(out, "%c%d%s", tab->var[i].is_row ? 'r' : 'c',
2093 tab->var[i].index,
2094 tab->var[i].is_zero ? " [=0]" :
2095 tab->var[i].is_redundant ? " [R]" : "");
2097 fprintf(out, "]\n");
2098 fprintf(out, "%*s[", indent, "");
2099 for (i = 0; i < tab->n_con; ++i) {
2100 if (i)
2101 fprintf(out, ", ");
2102 fprintf(out, "%c%d%s", tab->con[i].is_row ? 'r' : 'c',
2103 tab->con[i].index,
2104 tab->con[i].is_zero ? " [=0]" :
2105 tab->con[i].is_redundant ? " [R]" : "");
2107 fprintf(out, "]\n");
2108 fprintf(out, "%*s[", indent, "");
2109 for (i = 0; i < tab->n_row; ++i) {
2110 if (i)
2111 fprintf(out, ", ");
2112 fprintf(out, "r%d: %d%s", i, tab->row_var[i],
2113 isl_tab_var_from_row(tab, i)->is_nonneg ? " [>=0]" : "");
2115 fprintf(out, "]\n");
2116 fprintf(out, "%*s[", indent, "");
2117 for (i = 0; i < tab->n_col; ++i) {
2118 if (i)
2119 fprintf(out, ", ");
2120 fprintf(out, "c%d: %d%s", i, tab->col_var[i],
2121 var_from_col(tab, i)->is_nonneg ? " [>=0]" : "");
2123 fprintf(out, "]\n");
2124 r = tab->mat->n_row;
2125 tab->mat->n_row = tab->n_row;
2126 c = tab->mat->n_col;
2127 tab->mat->n_col = 2 + tab->n_col;
2128 isl_mat_dump(tab->mat, out, indent);
2129 tab->mat->n_row = r;
2130 tab->mat->n_col = c;