2 #include "isl_map_private.h"
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".
11 struct isl_tab
*isl_tab_alloc(struct isl_ctx
*ctx
,
12 unsigned n_row
, unsigned n_var
)
17 tab
= isl_calloc_type(ctx
, struct isl_tab
);
20 tab
->mat
= isl_mat_alloc(ctx
, n_row
, 2 + n_var
);
23 tab
->var
= isl_alloc_array(ctx
, struct isl_tab_var
, n_var
);
26 tab
->con
= isl_alloc_array(ctx
, struct isl_tab_var
, n_row
);
29 tab
->col_var
= isl_alloc_array(ctx
, int, n_var
);
32 tab
->row_var
= isl_alloc_array(ctx
, int, n_row
);
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;
59 tab
->bottom
.type
= isl_tab_undo_bottom
;
60 tab
->bottom
.next
= NULL
;
61 tab
->top
= &tab
->bottom
;
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
);
78 tab
->max_con
+= n_new
;
80 if (tab
->mat
->n_row
< tab
->n_row
+ n_new
) {
83 tab
->mat
= isl_mat_extend(tab
->mat
,
84 tab
->n_row
+ n_new
, tab
->n_col
);
87 row_var
= isl_realloc_array(tab
->mat
->ctx
, tab
->row_var
,
88 int, tab
->mat
->n_row
);
91 tab
->row_var
= row_var
;
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
;
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
);
110 tab
->max_var
+= n_new
;
113 if (tab
->mat
->n_col
< off
+ tab
->n_col
+ n_new
) {
116 tab
->mat
= isl_mat_extend(tab
->mat
,
117 tab
->mat
->n_row
, off
+ tab
->n_col
+ n_new
);
120 p
= isl_realloc_array(tab
->mat
->ctx
, tab
->col_var
,
121 int, tab
->mat
->n_col
);
130 struct isl_tab
*isl_tab_extend(struct isl_tab
*tab
, unsigned n_new
)
132 if (isl_tab_extend_cons(tab
, n_new
) >= 0)
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
) {
150 void isl_tab_free(struct isl_tab
*tab
)
155 isl_mat_free(tab
->mat
);
156 isl_vec_free(tab
->dual
);
164 struct isl_tab
*isl_tab_dup(struct isl_tab
*tab
)
172 dup
= isl_calloc_type(tab
->ctx
, struct isl_tab
);
175 dup
->mat
= isl_mat_dup(tab
->mat
);
178 dup
->var
= isl_alloc_array(tab
->ctx
, struct isl_tab_var
, tab
->max_var
);
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
);
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
);
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
);
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
;
213 dup
->bottom
.type
= isl_tab_undo_bottom
;
214 dup
->bottom
.next
= NULL
;
215 dup
->top
= &dup
->bottom
;
222 static struct isl_tab_var
*var_from_index(struct isl_tab
*tab
, int i
)
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
)
251 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
252 if (!isl_int_is_neg(tab
->mat
->row
[i
][2 + var
->index
]))
254 if (isl_tab_var_from_row(tab
, i
)->is_nonneg
)
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
)
271 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
272 if (!isl_int_is_pos(tab
->mat
->row
[i
][2 + var
->index
]))
274 if (isl_tab_var_from_row(tab
, i
)->is_nonneg
)
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
)
308 for (j
= tab
->n_redundant
; j
< tab
->n_row
; ++j
) {
309 if (var
&& j
== var
->index
)
311 if (!isl_tab_var_from_row(tab
, j
)->is_nonneg
)
313 if (sgn
* isl_int_sgn(tab
->mat
->row
[j
][2 + c
]) >= 0)
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
]))
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
)
355 isl_assert(tab
->mat
->ctx
, var
->is_row
, return);
356 tr
= tab
->mat
->row
[var
->index
];
359 for (j
= tab
->n_dead
; j
< tab
->n_col
; ++j
) {
360 if (isl_int_is_zero(tr
[2 + j
]))
362 if (isl_int_sgn(tr
[2 + j
]) != sgn
&&
363 var_from_col(tab
, j
)->is_nonneg
)
365 if (c
< 0 || tab
->col_var
[j
] < tab
->col_var
[c
])
371 sgn
*= isl_int_sgn(tr
[2 + c
]);
372 r
= pivot_row(tab
, skip_var
, sgn
, c
);
373 *row
= r
< 0 ? var
->index
: r
;
377 /* Return 1 if row "row" represents an obviously redundant inequality.
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
)
387 if (tab
->row_var
[row
] < 0 && !isl_tab_var_from_row(tab
, row
)->is_nonneg
)
390 if (isl_int_is_neg(tab
->mat
->row
[row
][1]))
393 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
394 if (isl_int_is_zero(tab
->mat
->row
[row
][2 + i
]))
396 if (isl_int_is_neg(tab
->mat
->row
[row
][2 + i
]))
398 if (!var_from_col(tab
, i
)->is_nonneg
)
404 static void swap_rows(struct isl_tab
*tab
, int row1
, int row2
)
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
;
423 undo
= isl_alloc_type(tab
->mat
->ctx
, struct isl_tab_undo
);
431 undo
->next
= tab
->top
;
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
;
440 u
.var_index
= tab
->row_var
[var
->index
];
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
)
458 union isl_tab_undo_val u
;
460 u
.col_var
= isl_alloc_array(tab
->mat
->ctx
, int, tab
->n_col
);
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
) {
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
);
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;
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
);
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
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
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
549 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
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
)
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]);
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
]);
579 for (j
= 0; j
< 1 + tab
->n_col
; ++j
) {
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
) {
589 if (isl_int_is_zero(mat
->row
[i
][2 + col
]))
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
) {
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
);
611 var
= var_from_col(tab
, col
);
616 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
617 if (isl_int_is_zero(mat
->row
[i
][2 + col
]))
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
))
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
)
640 for (r
= tab
->n_redundant
; r
< tab
->n_row
; ++r
)
641 if (!isl_int_is_zero(tab
->mat
->row
[r
][2 + var
->index
]))
643 isl_assert(tab
->mat
->ctx
, r
< tab
->n_row
, return);
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
)
658 for (i
= 0; i
< tab
->n_row
; ++i
) {
659 if (!isl_tab_var_from_row(tab
, i
)->is_nonneg
)
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
)
680 if (max_is_manifestly_unbounded(tab
, var
))
683 while (!isl_int_is_pos(tab
->mat
->row
[var
->index
][1])) {
684 find_pivot(tab
, var
, var
, 1, &row
, &col
);
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 */
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
699 static int restore_row(struct isl_tab
*tab
, struct isl_tab_var
*var
)
703 while (isl_int_is_neg(tab
->mat
->row
[var
->index
][1])) {
704 find_pivot(tab
, var
, var
, 1, &row
, &col
);
707 isl_tab_pivot(tab
, row
, col
);
708 if (!var
->is_row
) /* manifestly unbounded */
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
)
723 while (isl_int_is_neg(tab
->mat
->row
[var
->index
][1])) {
724 find_pivot(tab
, var
, var
, 1, &row
, &col
);
727 if (row
== var
->index
) /* manifestly unbounded */
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
751 static int sign_of_min(struct isl_tab
*tab
, struct isl_tab_var
*var
)
754 struct isl_tab_var
*pivot_var
;
756 if (min_is_manifestly_unbounded(tab
, var
))
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
)
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
);
771 restore_row(tab
, var
);
776 if (var
->is_redundant
)
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
)
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
)
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
);
794 restore_row(tab
, var
);
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
)
809 struct isl_tab_var
*pivot_var
;
811 if (min_is_manifestly_unbounded(tab
, var
))
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
)
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
);
828 restore_row(tab
, var
);
833 if (var
->is_redundant
)
836 find_pivot(tab
, var
, var
, -1, &row
, &col
);
837 if (row
== var
->index
)
841 pivot_var
= var_from_col(tab
, col
);
842 isl_tab_pivot(tab
, row
, col
);
843 if (var
->is_redundant
)
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
);
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
)
865 if (max_is_manifestly_unbounded(tab
, var
))
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
);
872 return isl_int_ge(r
[1], r
[0]);
873 if (row
== var
->index
) /* manifestly unbounded */
875 isl_tab_pivot(tab
, row
, col
);
880 static void swap_cols(struct isl_tab
*tab
, int col1
, int col2
)
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
);
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;
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
)
932 struct isl_mat
*mat
= tab
->mat
;
934 isl_assert(tab
->mat
->ctx
, var
->is_nonneg
, return);
936 for (j
= tab
->n_dead
; j
< tab
->n_col
; ++j
) {
937 if (isl_int_is_zero(mat
->row
[var
->index
][2 + j
]))
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
))
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
)
954 isl_assert(tab
->mat
->ctx
, tab
->n_row
< tab
->mat
->n_row
, return -1);
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
;
967 isl_tab_push_var(tab
, isl_tab_undo_allocate
, &tab
->con
[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
)
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);
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);
998 isl_tab_push_var(tab
, isl_tab_undo_allocate
, &tab
->var
[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
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
)
1026 r
= isl_tab_allocate_con(tab
);
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
)
1039 if (tab
->var
[i
].is_row
) {
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
);
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,
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
);
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);
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);
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
)
1091 r
= isl_tab_add_row(tab
, ineq
);
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
);
1101 sgn
= restore_row(tab
, &tab
->con
[r
]);
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
);
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
)
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
);
1131 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
)
1132 if (!isl_int_is_zero(tab
->mat
->row
[var
->index
][2 + i
]))
1135 isl_assert(tab
->mat
->ctx
, i
< tab
->n_col
, return -1);
1136 isl_tab_pivot(tab
, var
->index
, i
);
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
)
1153 r
= isl_tab_add_row(tab
, eq
);
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
);
1162 isl_tab_pivot(tab
, r
, i
);
1163 isl_tab_kill_col(tab
, i
);
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
;
1182 r
= isl_tab_add_row(tab
, eq
);
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,
1192 if (to_col(tab
, var
) < 0)
1195 isl_tab_kill_col(tab
, var
->index
);
1203 struct isl_tab
*isl_tab_from_basic_map(struct isl_basic_map
*bmap
)
1206 struct isl_tab
*tab
;
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
));
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
]);
1223 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
1224 tab
= isl_tab_add_ineq(tab
, bmap
->ineq
[i
]);
1225 if (!tab
|| tab
->empty
)
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
)
1242 struct isl_tab
*tab
;
1246 tab
= isl_tab_alloc(bmap
->ctx
, bmap
->n_eq
+ bmap
->n_ineq
,
1247 isl_basic_map_total_dim(bmap
));
1250 tab
->rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
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
);
1260 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
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
);
1267 tab
->con
[r
].is_nonneg
= 1;
1268 isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]);
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
)
1290 if (tab
->n_dead
== tab
->n_col
)
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
)
1299 if (sign_of_max(tab
, var
) != 0)
1301 close_row(tab
, var
);
1304 if (tab
->n_dead
== tab
->n_col
)
1306 if (i
== tab
->n_row
)
1311 int isl_tab_sample_is_integer(struct isl_tab
*tab
)
1318 for (i
= 0; i
< tab
->n_var
; ++i
) {
1320 if (!tab
->var
[i
].is_row
)
1322 row
= tab
->var
[i
].index
;
1323 if (!isl_int_is_divisible_by(tab
->mat
->row
[row
][1],
1324 tab
->mat
->row
[row
][0]))
1330 static struct isl_vec
*extract_integer_sample(struct isl_tab
*tab
)
1333 struct isl_vec
*vec
;
1335 vec
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_var
);
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);
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]);
1353 struct isl_vec
*isl_tab_get_sample_value(struct isl_tab
*tab
)
1356 struct isl_vec
*vec
;
1362 vec
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_var
);
1368 isl_int_set_si(vec
->block
.data
[0], 1);
1369 for (i
= 0; i
< tab
->n_var
; ++i
) {
1371 if (!tab
->var
[i
].is_row
) {
1372 isl_int_set_si(vec
->block
.data
[1 + i
], 0);
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
);
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
)
1409 bmap
= isl_basic_map_set_to_empty(bmap
);
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
);
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
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
)
1446 if (isl_tab_extend_cons(tab
, 1) < 0)
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
];
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
);
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);
1471 isl_tab_push_var(tab
, isl_tab_undo_allocate
, &tab
->con
[r
]);
1473 sgn
= sign_of_max(tab
, &tab
->con
[r
]);
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
]);
1479 close_row(tab
, &tab
->con
[r
]);
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
;
1502 var
= &tab
->con
[con
];
1504 if (!var
->is_row
&& !max_is_manifestly_unbounded(tab
, var
))
1505 to_row(tab
, var
, 1);
1508 isl_int_add(tab
->mat
->row
[var
->index
][1],
1509 tab
->mat
->row
[var
->index
][1], tab
->mat
->row
[var
->index
][0]);
1513 for (i
= 0; i
< tab
->n_row
; ++i
) {
1514 if (isl_int_is_zero(tab
->mat
->row
[i
][2 + var
->index
]))
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
);
1527 struct isl_tab
*isl_tab_select_facet(struct isl_tab
*tab
, int con
)
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
)
1569 if (tab
->n_dead
== tab
->n_col
)
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
);
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
;
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
);
1593 if (i
== tab
->n_row
) {
1594 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
1595 var
= var_from_col(tab
, i
);
1599 if (i
== tab
->n_col
)
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
);
1614 if (may_be_equality(tab
, i
))
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
)
1646 if (tab
->n_redundant
== tab
->n_row
)
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
;
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
);
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
);
1670 if (i
== tab
->n_row
) {
1671 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
1672 var
= var_from_col(tab
, i
);
1676 if (i
== tab
->n_col
)
1681 if ((tab
->rational
? (sign_of_min(tab
, var
) >= 0)
1682 : !isl_tab_min_at_most_neg_one(tab
, var
)) &&
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
);
1689 if (!min_is_manifestly_unbounded(tab
, var
))
1699 int isl_tab_is_equality(struct isl_tab
*tab
, int con
)
1705 if (tab
->con
[con
].is_zero
)
1707 if (tab
->con
[con
].is_redundant
)
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
,
1731 enum isl_lp_result res
= isl_lp_ok
;
1732 struct isl_tab_var
*var
;
1733 struct isl_tab_undo
*snap
;
1736 return isl_lp_empty
;
1738 snap
= isl_tab_snap(tab
);
1739 r
= isl_tab_add_row(tab
, f
);
1741 return isl_lp_error
;
1743 isl_int_mul(tab
->mat
->row
[var
->index
][0],
1744 tab
->mat
->row
[var
->index
][0], denom
);
1747 find_pivot(tab
, var
, var
, -1, &row
, &col
);
1748 if (row
== var
->index
) {
1749 res
= isl_lp_unbounded
;
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
)) {
1761 isl_vec_free(tab
->dual
);
1762 tab
->dual
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_con
);
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);
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
) {
1778 isl_int_set(*opt
, tab
->mat
->row
[var
->index
][1]);
1779 isl_int_set(*opt_denom
, tab
->mat
->row
[var
->index
][0]);
1781 isl_int_cdiv_q(*opt
, tab
->mat
->row
[var
->index
][1],
1782 tab
->mat
->row
[var
->index
][0]);
1787 int isl_tab_is_redundant(struct isl_tab
*tab
, int con
)
1794 if (tab
->con
[con
].is_zero
)
1796 if (tab
->con
[con
].is_redundant
)
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
1804 struct isl_tab_undo
*isl_tab_snap(struct isl_tab
*tab
)
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);
1820 isl_int_sub(tab
->mat
->row
[var
->index
][1],
1821 tab
->mat
->row
[var
->index
][1], tab
->mat
->row
[var
->index
][0]);
1825 for (i
= 0; i
< tab
->n_row
; ++i
) {
1826 if (isl_int_is_zero(tab
->mat
->row
[i
][2 + var
->index
]))
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
:
1842 case isl_tab_undo_redundant
:
1843 var
->is_redundant
= 0;
1846 case isl_tab_undo_zero
:
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
);
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);
1862 to_row(tab
, var
, 0);
1864 drop_row(tab
, var
->index
);
1866 case isl_tab_undo_relax
:
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
)
1887 int *extra
= NULL
; /* current columns that contain bad stuff */
1890 extra
= isl_alloc_array(tab
->mat
->ctx
, int, tab
->n_col
);
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
])
1899 extra
[n_extra
++] = i
;
1901 for (i
= 0; i
< tab
->n_col
&& n_extra
> 0; ++i
) {
1902 struct isl_tab_var
*var
;
1905 for (j
= 0; j
< tab
->n_col
; ++j
)
1906 if (col_var
[i
] == tab
->col_var
[j
])
1910 var
= var_from_index(tab
, col_var
[i
]);
1912 for (j
= 0; j
< n_extra
; ++j
)
1913 if (!isl_int_is_zero(tab
->mat
->row
[row
][off
+extra
[j
]]))
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
];
1929 static int perform_undo(struct isl_tab
*tab
, struct isl_tab_undo
*undo
)
1931 switch (undo
->type
) {
1932 case isl_tab_undo_empty
:
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
);
1942 case isl_tab_undo_saved_basis
:
1943 if (restore_basis(tab
, undo
->u
.col_var
) < 0)
1947 isl_assert(tab
->mat
->ctx
, 0, return -1);
1952 /* Return the tableau to the state it was in when the snapshot "snap"
1955 int isl_tab_rollback(struct isl_tab
*tab
, struct isl_tab_undo
*snap
)
1957 struct isl_tab_undo
*undo
, *next
;
1963 for (undo
= tab
->top
; undo
&& undo
!= &tab
->bottom
; undo
= next
) {
1967 if (perform_undo(tab
, undo
) < 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
)
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
);
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".
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
;
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
);
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]) &&
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
;
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
;
2060 type
= isl_ineq_redundant
;
2062 if (isl_tab_rollback(tab
, snap
))
2063 return isl_ineq_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
)
2076 fprintf(out
, "%*snull tab\n", indent
, "");
2079 fprintf(out
, "%*sn_redundant: %d, n_dead: %d", indent
, "",
2080 tab
->n_redundant
, tab
->n_dead
);
2082 fprintf(out
, ", rational");
2084 fprintf(out
, ", empty");
2086 fprintf(out
, "%*s[", indent
, "");
2087 for (i
= 0; i
< tab
->n_var
; ++i
) {
2089 fprintf(out
, (i
== tab
->n_param
||
2090 i
== tab
->n_var
- tab
->n_div
) ? "; "
2092 fprintf(out
, "%c%d%s", tab
->var
[i
].is_row
? 'r' : 'c',
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
) {
2102 fprintf(out
, "%c%d%s", tab
->con
[i
].is_row
? 'r' : 'c',
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
) {
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
) {
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
;