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
, unsigned M
)
18 tab
= isl_calloc_type(ctx
, struct isl_tab
);
21 tab
->mat
= isl_mat_alloc(ctx
, n_row
, off
+ n_var
);
24 tab
->var
= isl_alloc_array(ctx
, struct isl_tab_var
, n_var
);
27 tab
->con
= isl_alloc_array(ctx
, struct isl_tab_var
, n_row
);
30 tab
->col_var
= isl_alloc_array(ctx
, int, n_var
);
33 tab
->row_var
= isl_alloc_array(ctx
, int, n_row
);
36 for (i
= 0; i
< n_var
; ++i
) {
37 tab
->var
[i
].index
= i
;
38 tab
->var
[i
].is_row
= 0;
39 tab
->var
[i
].is_nonneg
= 0;
40 tab
->var
[i
].is_zero
= 0;
41 tab
->var
[i
].is_redundant
= 0;
42 tab
->var
[i
].frozen
= 0;
61 tab
->bottom
.type
= isl_tab_undo_bottom
;
62 tab
->bottom
.next
= NULL
;
63 tab
->top
= &tab
->bottom
;
70 int isl_tab_extend_cons(struct isl_tab
*tab
, unsigned n_new
)
72 unsigned off
= 2 + tab
->M
;
73 if (tab
->max_con
< tab
->n_con
+ n_new
) {
74 struct isl_tab_var
*con
;
76 con
= isl_realloc_array(tab
->mat
->ctx
, tab
->con
,
77 struct isl_tab_var
, tab
->max_con
+ n_new
);
81 tab
->max_con
+= n_new
;
83 if (tab
->mat
->n_row
< tab
->n_row
+ n_new
) {
86 tab
->mat
= isl_mat_extend(tab
->mat
,
87 tab
->n_row
+ n_new
, off
+ tab
->n_col
);
90 row_var
= isl_realloc_array(tab
->mat
->ctx
, tab
->row_var
,
91 int, tab
->mat
->n_row
);
94 tab
->row_var
= row_var
;
99 /* Make room for at least n_new extra variables.
100 * Return -1 if anything went wrong.
102 int isl_tab_extend_vars(struct isl_tab
*tab
, unsigned n_new
)
104 struct isl_tab_var
*var
;
105 unsigned off
= 2 + tab
->M
;
107 if (tab
->max_var
< tab
->n_var
+ n_new
) {
108 var
= isl_realloc_array(tab
->mat
->ctx
, tab
->var
,
109 struct isl_tab_var
, tab
->n_var
+ n_new
);
113 tab
->max_var
+= n_new
;
116 if (tab
->mat
->n_col
< off
+ tab
->n_col
+ n_new
) {
119 tab
->mat
= isl_mat_extend(tab
->mat
,
120 tab
->mat
->n_row
, off
+ tab
->n_col
+ n_new
);
123 p
= isl_realloc_array(tab
->mat
->ctx
, tab
->col_var
,
124 int, tab
->mat
->n_col
);
133 struct isl_tab
*isl_tab_extend(struct isl_tab
*tab
, unsigned n_new
)
135 if (isl_tab_extend_cons(tab
, n_new
) >= 0)
142 static void free_undo(struct isl_tab
*tab
)
144 struct isl_tab_undo
*undo
, *next
;
146 for (undo
= tab
->top
; undo
&& undo
!= &tab
->bottom
; undo
= next
) {
153 void isl_tab_free(struct isl_tab
*tab
)
158 isl_mat_free(tab
->mat
);
159 isl_vec_free(tab
->dual
);
160 isl_basic_set_free(tab
->bset
);
168 struct isl_tab
*isl_tab_dup(struct isl_tab
*tab
)
176 dup
= isl_calloc_type(tab
->ctx
, struct isl_tab
);
179 dup
->mat
= isl_mat_dup(tab
->mat
);
182 dup
->var
= isl_alloc_array(tab
->ctx
, struct isl_tab_var
, tab
->max_var
);
185 for (i
= 0; i
< tab
->n_var
; ++i
)
186 dup
->var
[i
] = tab
->var
[i
];
187 dup
->con
= isl_alloc_array(tab
->ctx
, struct isl_tab_var
, tab
->max_con
);
190 for (i
= 0; i
< tab
->n_con
; ++i
)
191 dup
->con
[i
] = tab
->con
[i
];
192 dup
->col_var
= isl_alloc_array(tab
->ctx
, int, tab
->mat
->n_col
);
195 for (i
= 0; i
< tab
->n_var
; ++i
)
196 dup
->col_var
[i
] = tab
->col_var
[i
];
197 dup
->row_var
= isl_alloc_array(tab
->ctx
, int, tab
->mat
->n_row
);
200 for (i
= 0; i
< tab
->n_row
; ++i
)
201 dup
->row_var
[i
] = tab
->row_var
[i
];
202 dup
->n_row
= tab
->n_row
;
203 dup
->n_con
= tab
->n_con
;
204 dup
->n_eq
= tab
->n_eq
;
205 dup
->max_con
= tab
->max_con
;
206 dup
->n_col
= tab
->n_col
;
207 dup
->n_var
= tab
->n_var
;
208 dup
->max_var
= tab
->max_var
;
209 dup
->n_param
= tab
->n_param
;
210 dup
->n_div
= tab
->n_div
;
211 dup
->n_dead
= tab
->n_dead
;
212 dup
->n_redundant
= tab
->n_redundant
;
213 dup
->rational
= tab
->rational
;
214 dup
->empty
= tab
->empty
;
218 dup
->bottom
.type
= isl_tab_undo_bottom
;
219 dup
->bottom
.next
= NULL
;
220 dup
->top
= &dup
->bottom
;
227 static struct isl_tab_var
*var_from_index(struct isl_tab
*tab
, int i
)
232 return &tab
->con
[~i
];
235 struct isl_tab_var
*isl_tab_var_from_row(struct isl_tab
*tab
, int i
)
237 return var_from_index(tab
, tab
->row_var
[i
]);
240 static struct isl_tab_var
*var_from_col(struct isl_tab
*tab
, int i
)
242 return var_from_index(tab
, tab
->col_var
[i
]);
245 /* Check if there are any upper bounds on column variable "var",
246 * i.e., non-negative rows where var appears with a negative coefficient.
247 * Return 1 if there are no such bounds.
249 static int max_is_manifestly_unbounded(struct isl_tab
*tab
,
250 struct isl_tab_var
*var
)
253 unsigned off
= 2 + tab
->M
;
257 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
258 if (!isl_int_is_neg(tab
->mat
->row
[i
][off
+ var
->index
]))
260 if (isl_tab_var_from_row(tab
, i
)->is_nonneg
)
266 /* Check if there are any lower bounds on column variable "var",
267 * i.e., non-negative rows where var appears with a positive coefficient.
268 * Return 1 if there are no such bounds.
270 static int min_is_manifestly_unbounded(struct isl_tab
*tab
,
271 struct isl_tab_var
*var
)
274 unsigned off
= 2 + tab
->M
;
278 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
279 if (!isl_int_is_pos(tab
->mat
->row
[i
][off
+ var
->index
]))
281 if (isl_tab_var_from_row(tab
, i
)->is_nonneg
)
287 static int row_cmp(struct isl_tab
*tab
, int r1
, int r2
, int c
, isl_int t
)
289 unsigned off
= 2 + tab
->M
;
293 isl_int_mul(t
, tab
->mat
->row
[r1
][2], tab
->mat
->row
[r2
][off
+c
]);
294 isl_int_submul(t
, tab
->mat
->row
[r2
][2], tab
->mat
->row
[r1
][off
+c
]);
299 isl_int_mul(t
, tab
->mat
->row
[r1
][1], tab
->mat
->row
[r2
][off
+ c
]);
300 isl_int_submul(t
, tab
->mat
->row
[r2
][1], tab
->mat
->row
[r1
][off
+ c
]);
301 return isl_int_sgn(t
);
304 /* Given the index of a column "c", return the index of a row
305 * that can be used to pivot the column in, with either an increase
306 * (sgn > 0) or a decrease (sgn < 0) of the corresponding variable.
307 * If "var" is not NULL, then the row returned will be different from
308 * the one associated with "var".
310 * Each row in the tableau is of the form
312 * x_r = a_r0 + \sum_i a_ri x_i
314 * Only rows with x_r >= 0 and with the sign of a_ri opposite to "sgn"
315 * impose any limit on the increase or decrease in the value of x_c
316 * and this bound is equal to a_r0 / |a_rc|. We are therefore looking
317 * for the row with the smallest (most stringent) such bound.
318 * Note that the common denominator of each row drops out of the fraction.
319 * To check if row j has a smaller bound than row r, i.e.,
320 * a_j0 / |a_jc| < a_r0 / |a_rc| or a_j0 |a_rc| < a_r0 |a_jc|,
321 * we check if -sign(a_jc) (a_j0 a_rc - a_r0 a_jc) < 0,
322 * where -sign(a_jc) is equal to "sgn".
324 static int pivot_row(struct isl_tab
*tab
,
325 struct isl_tab_var
*var
, int sgn
, int c
)
329 unsigned off
= 2 + tab
->M
;
333 for (j
= tab
->n_redundant
; j
< tab
->n_row
; ++j
) {
334 if (var
&& j
== var
->index
)
336 if (!isl_tab_var_from_row(tab
, j
)->is_nonneg
)
338 if (sgn
* isl_int_sgn(tab
->mat
->row
[j
][off
+ c
]) >= 0)
344 tsgn
= sgn
* row_cmp(tab
, r
, j
, c
, t
);
345 if (tsgn
< 0 || (tsgn
== 0 &&
346 tab
->row_var
[j
] < tab
->row_var
[r
]))
353 /* Find a pivot (row and col) that will increase (sgn > 0) or decrease
354 * (sgn < 0) the value of row variable var.
355 * If not NULL, then skip_var is a row variable that should be ignored
356 * while looking for a pivot row. It is usually equal to var.
358 * As the given row in the tableau is of the form
360 * x_r = a_r0 + \sum_i a_ri x_i
362 * we need to find a column such that the sign of a_ri is equal to "sgn"
363 * (such that an increase in x_i will have the desired effect) or a
364 * column with a variable that may attain negative values.
365 * If a_ri is positive, then we need to move x_i in the same direction
366 * to obtain the desired effect. Otherwise, x_i has to move in the
367 * opposite direction.
369 static void find_pivot(struct isl_tab
*tab
,
370 struct isl_tab_var
*var
, struct isl_tab_var
*skip_var
,
371 int sgn
, int *row
, int *col
)
378 isl_assert(tab
->mat
->ctx
, var
->is_row
, return);
379 tr
= tab
->mat
->row
[var
->index
] + 2 + tab
->M
;
382 for (j
= tab
->n_dead
; j
< tab
->n_col
; ++j
) {
383 if (isl_int_is_zero(tr
[j
]))
385 if (isl_int_sgn(tr
[j
]) != sgn
&&
386 var_from_col(tab
, j
)->is_nonneg
)
388 if (c
< 0 || tab
->col_var
[j
] < tab
->col_var
[c
])
394 sgn
*= isl_int_sgn(tr
[c
]);
395 r
= pivot_row(tab
, skip_var
, sgn
, c
);
396 *row
= r
< 0 ? var
->index
: r
;
400 /* Return 1 if row "row" represents an obviously redundant inequality.
402 * - it represents an inequality or a variable
403 * - that is the sum of a non-negative sample value and a positive
404 * combination of zero or more non-negative variables.
406 int isl_tab_row_is_redundant(struct isl_tab
*tab
, int row
)
409 unsigned off
= 2 + tab
->M
;
411 if (tab
->row_var
[row
] < 0 && !isl_tab_var_from_row(tab
, row
)->is_nonneg
)
414 if (isl_int_is_neg(tab
->mat
->row
[row
][1]))
416 if (tab
->M
&& isl_int_is_neg(tab
->mat
->row
[row
][2]))
419 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
420 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ i
]))
422 if (isl_int_is_neg(tab
->mat
->row
[row
][off
+ i
]))
424 if (!var_from_col(tab
, i
)->is_nonneg
)
430 static void swap_rows(struct isl_tab
*tab
, int row1
, int row2
)
433 t
= tab
->row_var
[row1
];
434 tab
->row_var
[row1
] = tab
->row_var
[row2
];
435 tab
->row_var
[row2
] = t
;
436 isl_tab_var_from_row(tab
, row1
)->index
= row1
;
437 isl_tab_var_from_row(tab
, row2
)->index
= row2
;
438 tab
->mat
= isl_mat_swap_rows(tab
->mat
, row1
, row2
);
441 static void push_union(struct isl_tab
*tab
,
442 enum isl_tab_undo_type type
, union isl_tab_undo_val u
)
444 struct isl_tab_undo
*undo
;
449 undo
= isl_alloc_type(tab
->mat
->ctx
, struct isl_tab_undo
);
457 undo
->next
= tab
->top
;
461 void isl_tab_push_var(struct isl_tab
*tab
,
462 enum isl_tab_undo_type type
, struct isl_tab_var
*var
)
464 union isl_tab_undo_val u
;
466 u
.var_index
= tab
->row_var
[var
->index
];
468 u
.var_index
= tab
->col_var
[var
->index
];
469 push_union(tab
, type
, u
);
472 void isl_tab_push(struct isl_tab
*tab
, enum isl_tab_undo_type type
)
474 union isl_tab_undo_val u
= { 0 };
475 push_union(tab
, type
, u
);
478 /* Push a record on the undo stack describing the current basic
479 * variables, so that the this state can be restored during rollback.
481 void isl_tab_push_basis(struct isl_tab
*tab
)
484 union isl_tab_undo_val u
;
486 u
.col_var
= isl_alloc_array(tab
->mat
->ctx
, int, tab
->n_col
);
492 for (i
= 0; i
< tab
->n_col
; ++i
)
493 u
.col_var
[i
] = tab
->col_var
[i
];
494 push_union(tab
, isl_tab_undo_saved_basis
, u
);
497 /* Mark row with index "row" as being redundant.
498 * If we may need to undo the operation or if the row represents
499 * a variable of the original problem, the row is kept,
500 * but no longer considered when looking for a pivot row.
501 * Otherwise, the row is simply removed.
503 * The row may be interchanged with some other row. If it
504 * is interchanged with a later row, return 1. Otherwise return 0.
505 * If the rows are checked in order in the calling function,
506 * then a return value of 1 means that the row with the given
507 * row number may now contain a different row that hasn't been checked yet.
509 int isl_tab_mark_redundant(struct isl_tab
*tab
, int row
)
511 struct isl_tab_var
*var
= isl_tab_var_from_row(tab
, row
);
512 var
->is_redundant
= 1;
513 isl_assert(tab
->mat
->ctx
, row
>= tab
->n_redundant
, return);
514 if (tab
->need_undo
|| tab
->row_var
[row
] >= 0) {
515 if (tab
->row_var
[row
] >= 0 && !var
->is_nonneg
) {
517 isl_tab_push_var(tab
, isl_tab_undo_nonneg
, var
);
519 if (row
!= tab
->n_redundant
)
520 swap_rows(tab
, row
, tab
->n_redundant
);
521 isl_tab_push_var(tab
, isl_tab_undo_redundant
, var
);
525 if (row
!= tab
->n_row
- 1)
526 swap_rows(tab
, row
, tab
->n_row
- 1);
527 isl_tab_var_from_row(tab
, tab
->n_row
- 1)->index
= -1;
533 struct isl_tab
*isl_tab_mark_empty(struct isl_tab
*tab
)
535 if (!tab
->empty
&& tab
->need_undo
)
536 isl_tab_push(tab
, isl_tab_undo_empty
);
541 /* Given a row number "row" and a column number "col", pivot the tableau
542 * such that the associated variables are interchanged.
543 * The given row in the tableau expresses
545 * x_r = a_r0 + \sum_i a_ri x_i
549 * x_c = 1/a_rc x_r - a_r0/a_rc + sum_{i \ne r} -a_ri/a_rc
551 * Substituting this equality into the other rows
553 * x_j = a_j0 + \sum_i a_ji x_i
555 * with a_jc \ne 0, we obtain
557 * 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
564 * where i is any other column and j is any other row,
565 * is therefore transformed into
567 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
568 * 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)
570 * The transformation is performed along the following steps
575 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
578 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
579 * n_jc/(|n_rc| d_j) n_ji/(|n_rc| d_j)
581 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
582 * n_jc/(|n_rc| d_j) (n_ji |n_rc|)/(|n_rc| d_j)
584 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
585 * n_jc/(|n_rc| d_j) (n_ji |n_rc| - s(n_rc)n_jc n_ri)/(|n_rc| d_j)
587 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
588 * 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)
591 void isl_tab_pivot(struct isl_tab
*tab
, int row
, int col
)
596 struct isl_mat
*mat
= tab
->mat
;
597 struct isl_tab_var
*var
;
598 unsigned off
= 2 + tab
->M
;
600 isl_int_swap(mat
->row
[row
][0], mat
->row
[row
][off
+ col
]);
601 sgn
= isl_int_sgn(mat
->row
[row
][0]);
603 isl_int_neg(mat
->row
[row
][0], mat
->row
[row
][0]);
604 isl_int_neg(mat
->row
[row
][off
+ col
], mat
->row
[row
][off
+ col
]);
606 for (j
= 0; j
< off
- 1 + tab
->n_col
; ++j
) {
607 if (j
== off
- 1 + col
)
609 isl_int_neg(mat
->row
[row
][1 + j
], mat
->row
[row
][1 + j
]);
611 if (!isl_int_is_one(mat
->row
[row
][0]))
612 isl_seq_normalize(mat
->row
[row
], off
+ tab
->n_col
);
613 for (i
= 0; i
< tab
->n_row
; ++i
) {
616 if (isl_int_is_zero(mat
->row
[i
][off
+ col
]))
618 isl_int_mul(mat
->row
[i
][0], mat
->row
[i
][0], mat
->row
[row
][0]);
619 for (j
= 0; j
< off
- 1 + tab
->n_col
; ++j
) {
620 if (j
== off
- 1 + col
)
622 isl_int_mul(mat
->row
[i
][1 + j
],
623 mat
->row
[i
][1 + j
], mat
->row
[row
][0]);
624 isl_int_addmul(mat
->row
[i
][1 + j
],
625 mat
->row
[i
][off
+ col
], mat
->row
[row
][1 + j
]);
627 isl_int_mul(mat
->row
[i
][off
+ col
],
628 mat
->row
[i
][off
+ col
], mat
->row
[row
][off
+ col
]);
629 if (!isl_int_is_one(mat
->row
[i
][0]))
630 isl_seq_normalize(mat
->row
[i
], off
+ tab
->n_col
);
632 t
= tab
->row_var
[row
];
633 tab
->row_var
[row
] = tab
->col_var
[col
];
634 tab
->col_var
[col
] = t
;
635 var
= isl_tab_var_from_row(tab
, row
);
638 var
= var_from_col(tab
, col
);
643 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
644 if (isl_int_is_zero(mat
->row
[i
][off
+ col
]))
646 if (!isl_tab_var_from_row(tab
, i
)->frozen
&&
647 isl_tab_row_is_redundant(tab
, i
))
648 if (isl_tab_mark_redundant(tab
, i
))
653 /* If "var" represents a column variable, then pivot is up (sgn > 0)
654 * or down (sgn < 0) to a row. The variable is assumed not to be
655 * unbounded in the specified direction.
656 * If sgn = 0, then the variable is unbounded in both directions,
657 * and we pivot with any row we can find.
659 static void to_row(struct isl_tab
*tab
, struct isl_tab_var
*var
, int sign
)
662 unsigned off
= 2 + tab
->M
;
668 for (r
= tab
->n_redundant
; r
< tab
->n_row
; ++r
)
669 if (!isl_int_is_zero(tab
->mat
->row
[r
][off
+var
->index
]))
671 isl_assert(tab
->mat
->ctx
, r
< tab
->n_row
, return);
673 r
= pivot_row(tab
, NULL
, sign
, var
->index
);
674 isl_assert(tab
->mat
->ctx
, r
>= 0, return);
677 isl_tab_pivot(tab
, r
, var
->index
);
680 static void check_table(struct isl_tab
*tab
)
686 for (i
= 0; i
< tab
->n_row
; ++i
) {
687 if (!isl_tab_var_from_row(tab
, i
)->is_nonneg
)
689 assert(!isl_int_is_neg(tab
->mat
->row
[i
][1]));
693 /* Return the sign of the maximal value of "var".
694 * If the sign is not negative, then on return from this function,
695 * the sample value will also be non-negative.
697 * If "var" is manifestly unbounded wrt positive values, we are done.
698 * Otherwise, we pivot the variable up to a row if needed
699 * Then we continue pivoting down until either
700 * - no more down pivots can be performed
701 * - the sample value is positive
702 * - the variable is pivoted into a manifestly unbounded column
704 static int sign_of_max(struct isl_tab
*tab
, struct isl_tab_var
*var
)
708 if (max_is_manifestly_unbounded(tab
, var
))
711 while (!isl_int_is_pos(tab
->mat
->row
[var
->index
][1])) {
712 find_pivot(tab
, var
, var
, 1, &row
, &col
);
714 return isl_int_sgn(tab
->mat
->row
[var
->index
][1]);
715 isl_tab_pivot(tab
, row
, col
);
716 if (!var
->is_row
) /* manifestly unbounded */
722 static int row_is_neg(struct isl_tab
*tab
, int row
)
725 return isl_int_is_neg(tab
->mat
->row
[row
][1]);
726 if (isl_int_is_pos(tab
->mat
->row
[row
][2]))
728 if (isl_int_is_neg(tab
->mat
->row
[row
][2]))
730 return isl_int_is_neg(tab
->mat
->row
[row
][1]);
733 static int row_sgn(struct isl_tab
*tab
, int row
)
736 return isl_int_sgn(tab
->mat
->row
[row
][1]);
737 if (!isl_int_is_zero(tab
->mat
->row
[row
][2]))
738 return isl_int_sgn(tab
->mat
->row
[row
][2]);
740 return isl_int_sgn(tab
->mat
->row
[row
][1]);
743 /* Perform pivots until the row variable "var" has a non-negative
744 * sample value or until no more upward pivots can be performed.
745 * Return the sign of the sample value after the pivots have been
748 static int restore_row(struct isl_tab
*tab
, struct isl_tab_var
*var
)
752 while (row_is_neg(tab
, var
->index
)) {
753 find_pivot(tab
, var
, var
, 1, &row
, &col
);
756 isl_tab_pivot(tab
, row
, col
);
757 if (!var
->is_row
) /* manifestly unbounded */
760 return row_sgn(tab
, var
->index
);
763 /* Perform pivots until we are sure that the row variable "var"
764 * can attain non-negative values. After return from this
765 * function, "var" is still a row variable, but its sample
766 * value may not be non-negative, even if the function returns 1.
768 static int at_least_zero(struct isl_tab
*tab
, struct isl_tab_var
*var
)
772 while (isl_int_is_neg(tab
->mat
->row
[var
->index
][1])) {
773 find_pivot(tab
, var
, var
, 1, &row
, &col
);
776 if (row
== var
->index
) /* manifestly unbounded */
778 isl_tab_pivot(tab
, row
, col
);
780 return !isl_int_is_neg(tab
->mat
->row
[var
->index
][1]);
783 /* Return a negative value if "var" can attain negative values.
784 * Return a non-negative value otherwise.
786 * If "var" is manifestly unbounded wrt negative values, we are done.
787 * Otherwise, if var is in a column, we can pivot it down to a row.
788 * Then we continue pivoting down until either
789 * - the pivot would result in a manifestly unbounded column
790 * => we don't perform the pivot, but simply return -1
791 * - no more down pivots can be performed
792 * - the sample value is negative
793 * If the sample value becomes negative and the variable is supposed
794 * to be nonnegative, then we undo the last pivot.
795 * However, if the last pivot has made the pivoting variable
796 * obviously redundant, then it may have moved to another row.
797 * In that case we look for upward pivots until we reach a non-negative
800 static int sign_of_min(struct isl_tab
*tab
, struct isl_tab_var
*var
)
803 struct isl_tab_var
*pivot_var
;
805 if (min_is_manifestly_unbounded(tab
, var
))
809 row
= pivot_row(tab
, NULL
, -1, col
);
810 pivot_var
= var_from_col(tab
, col
);
811 isl_tab_pivot(tab
, row
, col
);
812 if (var
->is_redundant
)
814 if (isl_int_is_neg(tab
->mat
->row
[var
->index
][1])) {
815 if (var
->is_nonneg
) {
816 if (!pivot_var
->is_redundant
&&
817 pivot_var
->index
== row
)
818 isl_tab_pivot(tab
, row
, col
);
820 restore_row(tab
, var
);
825 if (var
->is_redundant
)
827 while (!isl_int_is_neg(tab
->mat
->row
[var
->index
][1])) {
828 find_pivot(tab
, var
, var
, -1, &row
, &col
);
829 if (row
== var
->index
)
832 return isl_int_sgn(tab
->mat
->row
[var
->index
][1]);
833 pivot_var
= var_from_col(tab
, col
);
834 isl_tab_pivot(tab
, row
, col
);
835 if (var
->is_redundant
)
838 if (var
->is_nonneg
) {
839 /* pivot back to non-negative value */
840 if (!pivot_var
->is_redundant
&& pivot_var
->index
== row
)
841 isl_tab_pivot(tab
, row
, col
);
843 restore_row(tab
, var
);
848 static int row_at_most_neg_one(struct isl_tab
*tab
, int row
)
851 if (isl_int_is_pos(tab
->mat
->row
[row
][2]))
853 if (isl_int_is_neg(tab
->mat
->row
[row
][2]))
856 return isl_int_is_neg(tab
->mat
->row
[row
][1]) &&
857 isl_int_abs_ge(tab
->mat
->row
[row
][1],
858 tab
->mat
->row
[row
][0]);
861 /* Return 1 if "var" can attain values <= -1.
862 * Return 0 otherwise.
864 * The sample value of "var" is assumed to be non-negative when the
865 * the function is called and will be made non-negative again before
866 * the function returns.
868 int isl_tab_min_at_most_neg_one(struct isl_tab
*tab
, struct isl_tab_var
*var
)
871 struct isl_tab_var
*pivot_var
;
873 if (min_is_manifestly_unbounded(tab
, var
))
877 row
= pivot_row(tab
, NULL
, -1, col
);
878 pivot_var
= var_from_col(tab
, col
);
879 isl_tab_pivot(tab
, row
, col
);
880 if (var
->is_redundant
)
882 if (row_at_most_neg_one(tab
, var
->index
)) {
883 if (var
->is_nonneg
) {
884 if (!pivot_var
->is_redundant
&&
885 pivot_var
->index
== row
)
886 isl_tab_pivot(tab
, row
, col
);
888 restore_row(tab
, var
);
893 if (var
->is_redundant
)
896 find_pivot(tab
, var
, var
, -1, &row
, &col
);
897 if (row
== var
->index
)
901 pivot_var
= var_from_col(tab
, col
);
902 isl_tab_pivot(tab
, row
, col
);
903 if (var
->is_redundant
)
905 } while (!row_at_most_neg_one(tab
, var
->index
));
906 if (var
->is_nonneg
) {
907 /* pivot back to non-negative value */
908 if (!pivot_var
->is_redundant
&& pivot_var
->index
== row
)
909 isl_tab_pivot(tab
, row
, col
);
910 restore_row(tab
, var
);
915 /* Return 1 if "var" can attain values >= 1.
916 * Return 0 otherwise.
918 static int at_least_one(struct isl_tab
*tab
, struct isl_tab_var
*var
)
923 if (max_is_manifestly_unbounded(tab
, var
))
926 r
= tab
->mat
->row
[var
->index
];
927 while (isl_int_lt(r
[1], r
[0])) {
928 find_pivot(tab
, var
, var
, 1, &row
, &col
);
930 return isl_int_ge(r
[1], r
[0]);
931 if (row
== var
->index
) /* manifestly unbounded */
933 isl_tab_pivot(tab
, row
, col
);
938 static void swap_cols(struct isl_tab
*tab
, int col1
, int col2
)
941 unsigned off
= 2 + tab
->M
;
942 t
= tab
->col_var
[col1
];
943 tab
->col_var
[col1
] = tab
->col_var
[col2
];
944 tab
->col_var
[col2
] = t
;
945 var_from_col(tab
, col1
)->index
= col1
;
946 var_from_col(tab
, col2
)->index
= col2
;
947 tab
->mat
= isl_mat_swap_cols(tab
->mat
, off
+ col1
, off
+ col2
);
950 /* Mark column with index "col" as representing a zero variable.
951 * If we may need to undo the operation the column is kept,
952 * but no longer considered.
953 * Otherwise, the column is simply removed.
955 * The column may be interchanged with some other column. If it
956 * is interchanged with a later column, return 1. Otherwise return 0.
957 * If the columns are checked in order in the calling function,
958 * then a return value of 1 means that the column with the given
959 * column number may now contain a different column that
960 * hasn't been checked yet.
962 int isl_tab_kill_col(struct isl_tab
*tab
, int col
)
964 var_from_col(tab
, col
)->is_zero
= 1;
965 if (tab
->need_undo
) {
966 isl_tab_push_var(tab
, isl_tab_undo_zero
, var_from_col(tab
, col
));
967 if (col
!= tab
->n_dead
)
968 swap_cols(tab
, col
, tab
->n_dead
);
972 if (col
!= tab
->n_col
- 1)
973 swap_cols(tab
, col
, tab
->n_col
- 1);
974 var_from_col(tab
, tab
->n_col
- 1)->index
= -1;
980 /* Row variable "var" is non-negative and cannot attain any values
981 * larger than zero. This means that the coefficients of the unrestricted
982 * column variables are zero and that the coefficients of the non-negative
983 * column variables are zero or negative.
984 * Each of the non-negative variables with a negative coefficient can
985 * then also be written as the negative sum of non-negative variables
986 * and must therefore also be zero.
988 static void close_row(struct isl_tab
*tab
, struct isl_tab_var
*var
)
991 struct isl_mat
*mat
= tab
->mat
;
992 unsigned off
= 2 + tab
->M
;
994 isl_assert(tab
->mat
->ctx
, var
->is_nonneg
, return);
996 for (j
= tab
->n_dead
; j
< tab
->n_col
; ++j
) {
997 if (isl_int_is_zero(mat
->row
[var
->index
][off
+ j
]))
999 isl_assert(tab
->mat
->ctx
,
1000 isl_int_is_neg(mat
->row
[var
->index
][off
+ j
]), return);
1001 if (isl_tab_kill_col(tab
, j
))
1004 isl_tab_mark_redundant(tab
, var
->index
);
1007 /* Add a constraint to the tableau and allocate a row for it.
1008 * Return the index into the constraint array "con".
1010 int isl_tab_allocate_con(struct isl_tab
*tab
)
1014 isl_assert(tab
->mat
->ctx
, tab
->n_row
< tab
->mat
->n_row
, return -1);
1017 tab
->con
[r
].index
= tab
->n_row
;
1018 tab
->con
[r
].is_row
= 1;
1019 tab
->con
[r
].is_nonneg
= 0;
1020 tab
->con
[r
].is_zero
= 0;
1021 tab
->con
[r
].is_redundant
= 0;
1022 tab
->con
[r
].frozen
= 0;
1023 tab
->row_var
[tab
->n_row
] = ~r
;
1027 isl_tab_push_var(tab
, isl_tab_undo_allocate
, &tab
->con
[r
]);
1032 /* Add a variable to the tableau and allocate a column for it.
1033 * Return the index into the variable array "var".
1035 int isl_tab_allocate_var(struct isl_tab
*tab
)
1039 unsigned off
= 2 + tab
->M
;
1041 isl_assert(tab
->mat
->ctx
, tab
->n_col
< tab
->mat
->n_col
, return -1);
1042 isl_assert(tab
->mat
->ctx
, tab
->n_var
< tab
->max_var
, return -1);
1045 tab
->var
[r
].index
= tab
->n_col
;
1046 tab
->var
[r
].is_row
= 0;
1047 tab
->var
[r
].is_nonneg
= 0;
1048 tab
->var
[r
].is_zero
= 0;
1049 tab
->var
[r
].is_redundant
= 0;
1050 tab
->var
[r
].frozen
= 0;
1051 tab
->col_var
[tab
->n_col
] = r
;
1053 for (i
= 0; i
< tab
->n_row
; ++i
)
1054 isl_int_set_si(tab
->mat
->row
[i
][off
+ tab
->n_col
], 0);
1058 isl_tab_push_var(tab
, isl_tab_undo_allocate
, &tab
->var
[r
]);
1063 /* Add a row to the tableau. The row is given as an affine combination
1064 * of the original variables and needs to be expressed in terms of the
1067 * We add each term in turn.
1068 * If r = n/d_r is the current sum and we need to add k x, then
1069 * if x is a column variable, we increase the numerator of
1070 * this column by k d_r
1071 * if x = f/d_x is a row variable, then the new representation of r is
1073 * n k f d_x/g n + d_r/g k f m/d_r n + m/d_g k f
1074 * --- + --- = ------------------- = -------------------
1075 * d_r d_r d_r d_x/g m
1077 * with g the gcd of d_r and d_x and m the lcm of d_r and d_x.
1079 int isl_tab_add_row(struct isl_tab
*tab
, isl_int
*line
)
1085 unsigned off
= 2 + tab
->M
;
1087 r
= isl_tab_allocate_con(tab
);
1093 row
= tab
->mat
->row
[tab
->con
[r
].index
];
1094 isl_int_set_si(row
[0], 1);
1095 isl_int_set(row
[1], line
[0]);
1096 isl_seq_clr(row
+ 2, tab
->M
+ tab
->n_col
);
1097 for (i
= 0; i
< tab
->n_var
; ++i
) {
1098 if (tab
->var
[i
].is_zero
)
1100 if (tab
->var
[i
].is_row
) {
1102 row
[0], tab
->mat
->row
[tab
->var
[i
].index
][0]);
1103 isl_int_swap(a
, row
[0]);
1104 isl_int_divexact(a
, row
[0], a
);
1106 row
[0], tab
->mat
->row
[tab
->var
[i
].index
][0]);
1107 isl_int_mul(b
, b
, line
[1 + i
]);
1108 isl_seq_combine(row
+ 1, a
, row
+ 1,
1109 b
, tab
->mat
->row
[tab
->var
[i
].index
] + 1,
1110 1 + tab
->M
+ tab
->n_col
);
1112 isl_int_addmul(row
[off
+ tab
->var
[i
].index
],
1113 line
[1 + i
], row
[0]);
1114 if (tab
->M
&& i
>= tab
->n_param
&& i
< tab
->n_var
- tab
->n_div
)
1115 isl_int_submul(row
[2], line
[1 + i
], row
[0]);
1117 isl_seq_normalize(row
, off
+ tab
->n_col
);
1124 static int drop_row(struct isl_tab
*tab
, int row
)
1126 isl_assert(tab
->mat
->ctx
, ~tab
->row_var
[row
] == tab
->n_con
- 1, return -1);
1127 if (row
!= tab
->n_row
- 1)
1128 swap_rows(tab
, row
, tab
->n_row
- 1);
1134 static int drop_col(struct isl_tab
*tab
, int col
)
1136 isl_assert(tab
->mat
->ctx
, tab
->col_var
[col
] == tab
->n_var
- 1, return -1);
1137 if (col
!= tab
->n_col
- 1)
1138 swap_cols(tab
, col
, tab
->n_col
- 1);
1144 /* Add inequality "ineq" and check if it conflicts with the
1145 * previously added constraints or if it is obviously redundant.
1147 struct isl_tab
*isl_tab_add_ineq(struct isl_tab
*tab
, isl_int
*ineq
)
1154 r
= isl_tab_add_row(tab
, ineq
);
1157 tab
->con
[r
].is_nonneg
= 1;
1158 isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]);
1159 if (isl_tab_row_is_redundant(tab
, tab
->con
[r
].index
)) {
1160 isl_tab_mark_redundant(tab
, tab
->con
[r
].index
);
1164 sgn
= restore_row(tab
, &tab
->con
[r
]);
1166 return isl_tab_mark_empty(tab
);
1167 if (tab
->con
[r
].is_row
&& isl_tab_row_is_redundant(tab
, tab
->con
[r
].index
))
1168 isl_tab_mark_redundant(tab
, tab
->con
[r
].index
);
1175 /* Pivot a non-negative variable down until it reaches the value zero
1176 * and then pivot the variable into a column position.
1178 int to_col(struct isl_tab
*tab
, struct isl_tab_var
*var
)
1182 unsigned off
= 2 + tab
->M
;
1187 while (isl_int_is_pos(tab
->mat
->row
[var
->index
][1])) {
1188 find_pivot(tab
, var
, NULL
, -1, &row
, &col
);
1189 isl_assert(tab
->mat
->ctx
, row
!= -1, return -1);
1190 isl_tab_pivot(tab
, row
, col
);
1195 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
)
1196 if (!isl_int_is_zero(tab
->mat
->row
[var
->index
][off
+ i
]))
1199 isl_assert(tab
->mat
->ctx
, i
< tab
->n_col
, return -1);
1200 isl_tab_pivot(tab
, var
->index
, i
);
1205 /* We assume Gaussian elimination has been performed on the equalities.
1206 * The equalities can therefore never conflict.
1207 * Adding the equalities is currently only really useful for a later call
1208 * to isl_tab_ineq_type.
1210 static struct isl_tab
*add_eq(struct isl_tab
*tab
, isl_int
*eq
)
1217 r
= isl_tab_add_row(tab
, eq
);
1221 r
= tab
->con
[r
].index
;
1222 i
= isl_seq_first_non_zero(tab
->mat
->row
[r
] + 2 + tab
->M
+ tab
->n_dead
,
1223 tab
->n_col
- tab
->n_dead
);
1224 isl_assert(tab
->mat
->ctx
, i
>= 0, goto error
);
1226 isl_tab_pivot(tab
, r
, i
);
1227 isl_tab_kill_col(tab
, i
);
1236 /* Add an equality that is known to be valid for the given tableau.
1238 struct isl_tab
*isl_tab_add_valid_eq(struct isl_tab
*tab
, isl_int
*eq
)
1240 struct isl_tab_var
*var
;
1246 r
= isl_tab_add_row(tab
, eq
);
1252 if (isl_int_is_neg(tab
->mat
->row
[r
][1]))
1253 isl_seq_neg(tab
->mat
->row
[r
] + 1, tab
->mat
->row
[r
] + 1,
1256 if (to_col(tab
, var
) < 0)
1259 isl_tab_kill_col(tab
, var
->index
);
1267 struct isl_tab
*isl_tab_from_basic_map(struct isl_basic_map
*bmap
)
1270 struct isl_tab
*tab
;
1274 tab
= isl_tab_alloc(bmap
->ctx
,
1275 isl_basic_map_total_dim(bmap
) + bmap
->n_ineq
+ 1,
1276 isl_basic_map_total_dim(bmap
), 0);
1279 tab
->rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
1280 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
1281 return isl_tab_mark_empty(tab
);
1282 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1283 tab
= add_eq(tab
, bmap
->eq
[i
]);
1287 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
1288 tab
= isl_tab_add_ineq(tab
, bmap
->ineq
[i
]);
1289 if (!tab
|| tab
->empty
)
1295 struct isl_tab
*isl_tab_from_basic_set(struct isl_basic_set
*bset
)
1297 return isl_tab_from_basic_map((struct isl_basic_map
*)bset
);
1300 /* Construct a tableau corresponding to the recession cone of "bmap".
1302 struct isl_tab
*isl_tab_from_recession_cone(struct isl_basic_map
*bmap
)
1306 struct isl_tab
*tab
;
1310 tab
= isl_tab_alloc(bmap
->ctx
, bmap
->n_eq
+ bmap
->n_ineq
,
1311 isl_basic_map_total_dim(bmap
), 0);
1314 tab
->rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
1317 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1318 isl_int_swap(bmap
->eq
[i
][0], cst
);
1319 tab
= add_eq(tab
, bmap
->eq
[i
]);
1320 isl_int_swap(bmap
->eq
[i
][0], cst
);
1324 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
1326 isl_int_swap(bmap
->ineq
[i
][0], cst
);
1327 r
= isl_tab_add_row(tab
, bmap
->ineq
[i
]);
1328 isl_int_swap(bmap
->ineq
[i
][0], cst
);
1331 tab
->con
[r
].is_nonneg
= 1;
1332 isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]);
1343 /* Assuming "tab" is the tableau of a cone, check if the cone is
1344 * bounded, i.e., if it is empty or only contains the origin.
1346 int isl_tab_cone_is_bounded(struct isl_tab
*tab
)
1354 if (tab
->n_dead
== tab
->n_col
)
1358 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
1359 struct isl_tab_var
*var
;
1360 var
= isl_tab_var_from_row(tab
, i
);
1361 if (!var
->is_nonneg
)
1363 if (sign_of_max(tab
, var
) != 0)
1365 close_row(tab
, var
);
1368 if (tab
->n_dead
== tab
->n_col
)
1370 if (i
== tab
->n_row
)
1375 int isl_tab_sample_is_integer(struct isl_tab
*tab
)
1382 for (i
= 0; i
< tab
->n_var
; ++i
) {
1384 if (!tab
->var
[i
].is_row
)
1386 row
= tab
->var
[i
].index
;
1387 if (!isl_int_is_divisible_by(tab
->mat
->row
[row
][1],
1388 tab
->mat
->row
[row
][0]))
1394 static struct isl_vec
*extract_integer_sample(struct isl_tab
*tab
)
1397 struct isl_vec
*vec
;
1399 vec
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_var
);
1403 isl_int_set_si(vec
->block
.data
[0], 1);
1404 for (i
= 0; i
< tab
->n_var
; ++i
) {
1405 if (!tab
->var
[i
].is_row
)
1406 isl_int_set_si(vec
->block
.data
[1 + i
], 0);
1408 int row
= tab
->var
[i
].index
;
1409 isl_int_divexact(vec
->block
.data
[1 + i
],
1410 tab
->mat
->row
[row
][1], tab
->mat
->row
[row
][0]);
1417 struct isl_vec
*isl_tab_get_sample_value(struct isl_tab
*tab
)
1420 struct isl_vec
*vec
;
1426 vec
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_var
);
1432 isl_int_set_si(vec
->block
.data
[0], 1);
1433 for (i
= 0; i
< tab
->n_var
; ++i
) {
1435 if (!tab
->var
[i
].is_row
) {
1436 isl_int_set_si(vec
->block
.data
[1 + i
], 0);
1439 row
= tab
->var
[i
].index
;
1440 isl_int_gcd(m
, vec
->block
.data
[0], tab
->mat
->row
[row
][0]);
1441 isl_int_divexact(m
, tab
->mat
->row
[row
][0], m
);
1442 isl_seq_scale(vec
->block
.data
, vec
->block
.data
, m
, 1 + i
);
1443 isl_int_divexact(m
, vec
->block
.data
[0], tab
->mat
->row
[row
][0]);
1444 isl_int_mul(vec
->block
.data
[1 + i
], m
, tab
->mat
->row
[row
][1]);
1446 isl_seq_normalize(vec
->block
.data
, vec
->size
);
1452 /* Update "bmap" based on the results of the tableau "tab".
1453 * In particular, implicit equalities are made explicit, redundant constraints
1454 * are removed and if the sample value happens to be integer, it is stored
1455 * in "bmap" (unless "bmap" already had an integer sample).
1457 * The tableau is assumed to have been created from "bmap" using
1458 * isl_tab_from_basic_map.
1460 struct isl_basic_map
*isl_basic_map_update_from_tab(struct isl_basic_map
*bmap
,
1461 struct isl_tab
*tab
)
1473 bmap
= isl_basic_map_set_to_empty(bmap
);
1475 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
1476 if (isl_tab_is_equality(tab
, n_eq
+ i
))
1477 isl_basic_map_inequality_to_equality(bmap
, i
);
1478 else if (isl_tab_is_redundant(tab
, n_eq
+ i
))
1479 isl_basic_map_drop_inequality(bmap
, i
);
1481 if (!tab
->rational
&&
1482 !bmap
->sample
&& isl_tab_sample_is_integer(tab
))
1483 bmap
->sample
= extract_integer_sample(tab
);
1487 struct isl_basic_set
*isl_basic_set_update_from_tab(struct isl_basic_set
*bset
,
1488 struct isl_tab
*tab
)
1490 return (struct isl_basic_set
*)isl_basic_map_update_from_tab(
1491 (struct isl_basic_map
*)bset
, tab
);
1494 /* Given a non-negative variable "var", add a new non-negative variable
1495 * that is the opposite of "var", ensuring that var can only attain the
1497 * If var = n/d is a row variable, then the new variable = -n/d.
1498 * If var is a column variables, then the new variable = -var.
1499 * If the new variable cannot attain non-negative values, then
1500 * the resulting tableau is empty.
1501 * Otherwise, we know the value will be zero and we close the row.
1503 static struct isl_tab
*cut_to_hyperplane(struct isl_tab
*tab
,
1504 struct isl_tab_var
*var
)
1509 unsigned off
= 2 + tab
->M
;
1511 if (isl_tab_extend_cons(tab
, 1) < 0)
1515 tab
->con
[r
].index
= tab
->n_row
;
1516 tab
->con
[r
].is_row
= 1;
1517 tab
->con
[r
].is_nonneg
= 0;
1518 tab
->con
[r
].is_zero
= 0;
1519 tab
->con
[r
].is_redundant
= 0;
1520 tab
->con
[r
].frozen
= 0;
1521 tab
->row_var
[tab
->n_row
] = ~r
;
1522 row
= tab
->mat
->row
[tab
->n_row
];
1525 isl_int_set(row
[0], tab
->mat
->row
[var
->index
][0]);
1526 isl_seq_neg(row
+ 1,
1527 tab
->mat
->row
[var
->index
] + 1, 1 + tab
->n_col
);
1529 isl_int_set_si(row
[0], 1);
1530 isl_seq_clr(row
+ 1, 1 + tab
->n_col
);
1531 isl_int_set_si(row
[off
+ var
->index
], -1);
1536 isl_tab_push_var(tab
, isl_tab_undo_allocate
, &tab
->con
[r
]);
1538 sgn
= sign_of_max(tab
, &tab
->con
[r
]);
1540 return isl_tab_mark_empty(tab
);
1541 tab
->con
[r
].is_nonneg
= 1;
1542 isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]);
1544 close_row(tab
, &tab
->con
[r
]);
1552 /* Given a tableau "tab" and an inequality constraint "con" of the tableau,
1553 * relax the inequality by one. That is, the inequality r >= 0 is replaced
1554 * by r' = r + 1 >= 0.
1555 * If r is a row variable, we simply increase the constant term by one
1556 * (taking into account the denominator).
1557 * If r is a column variable, then we need to modify each row that
1558 * refers to r = r' - 1 by substituting this equality, effectively
1559 * subtracting the coefficient of the column from the constant.
1561 struct isl_tab
*isl_tab_relax(struct isl_tab
*tab
, int con
)
1563 struct isl_tab_var
*var
;
1564 unsigned off
= 2 + tab
->M
;
1569 var
= &tab
->con
[con
];
1571 if (!var
->is_row
&& !max_is_manifestly_unbounded(tab
, var
))
1572 to_row(tab
, var
, 1);
1575 isl_int_add(tab
->mat
->row
[var
->index
][1],
1576 tab
->mat
->row
[var
->index
][1], tab
->mat
->row
[var
->index
][0]);
1580 for (i
= 0; i
< tab
->n_row
; ++i
) {
1581 if (isl_int_is_zero(tab
->mat
->row
[i
][off
+ var
->index
]))
1583 isl_int_sub(tab
->mat
->row
[i
][1], tab
->mat
->row
[i
][1],
1584 tab
->mat
->row
[i
][off
+ var
->index
]);
1589 isl_tab_push_var(tab
, isl_tab_undo_relax
, var
);
1594 struct isl_tab
*isl_tab_select_facet(struct isl_tab
*tab
, int con
)
1599 return cut_to_hyperplane(tab
, &tab
->con
[con
]);
1602 static int may_be_equality(struct isl_tab
*tab
, int row
)
1604 unsigned off
= 2 + tab
->M
;
1605 return (tab
->rational
? isl_int_is_zero(tab
->mat
->row
[row
][1])
1606 : isl_int_lt(tab
->mat
->row
[row
][1],
1607 tab
->mat
->row
[row
][0])) &&
1608 isl_seq_first_non_zero(tab
->mat
->row
[row
] + off
+ tab
->n_dead
,
1609 tab
->n_col
- tab
->n_dead
) != -1;
1612 /* Check for (near) equalities among the constraints.
1613 * A constraint is an equality if it is non-negative and if
1614 * its maximal value is either
1615 * - zero (in case of rational tableaus), or
1616 * - strictly less than 1 (in case of integer tableaus)
1618 * We first mark all non-redundant and non-dead variables that
1619 * are not frozen and not obviously not an equality.
1620 * Then we iterate over all marked variables if they can attain
1621 * any values larger than zero or at least one.
1622 * If the maximal value is zero, we mark any column variables
1623 * that appear in the row as being zero and mark the row as being redundant.
1624 * Otherwise, if the maximal value is strictly less than one (and the
1625 * tableau is integer), then we restrict the value to being zero
1626 * by adding an opposite non-negative variable.
1628 struct isl_tab
*isl_tab_detect_equalities(struct isl_tab
*tab
)
1637 if (tab
->n_dead
== tab
->n_col
)
1641 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
1642 struct isl_tab_var
*var
= isl_tab_var_from_row(tab
, i
);
1643 var
->marked
= !var
->frozen
&& var
->is_nonneg
&&
1644 may_be_equality(tab
, i
);
1648 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
1649 struct isl_tab_var
*var
= var_from_col(tab
, i
);
1650 var
->marked
= !var
->frozen
&& var
->is_nonneg
;
1655 struct isl_tab_var
*var
;
1656 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
1657 var
= isl_tab_var_from_row(tab
, i
);
1661 if (i
== tab
->n_row
) {
1662 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
1663 var
= var_from_col(tab
, i
);
1667 if (i
== tab
->n_col
)
1672 if (sign_of_max(tab
, var
) == 0)
1673 close_row(tab
, var
);
1674 else if (!tab
->rational
&& !at_least_one(tab
, var
)) {
1675 tab
= cut_to_hyperplane(tab
, var
);
1676 return isl_tab_detect_equalities(tab
);
1678 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
1679 var
= isl_tab_var_from_row(tab
, i
);
1682 if (may_be_equality(tab
, i
))
1692 /* Check for (near) redundant constraints.
1693 * A constraint is redundant if it is non-negative and if
1694 * its minimal value (temporarily ignoring the non-negativity) is either
1695 * - zero (in case of rational tableaus), or
1696 * - strictly larger than -1 (in case of integer tableaus)
1698 * We first mark all non-redundant and non-dead variables that
1699 * are not frozen and not obviously negatively unbounded.
1700 * Then we iterate over all marked variables if they can attain
1701 * any values smaller than zero or at most negative one.
1702 * If not, we mark the row as being redundant (assuming it hasn't
1703 * been detected as being obviously redundant in the mean time).
1705 struct isl_tab
*isl_tab_detect_redundant(struct isl_tab
*tab
)
1714 if (tab
->n_redundant
== tab
->n_row
)
1718 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
1719 struct isl_tab_var
*var
= isl_tab_var_from_row(tab
, i
);
1720 var
->marked
= !var
->frozen
&& var
->is_nonneg
;
1724 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
1725 struct isl_tab_var
*var
= var_from_col(tab
, i
);
1726 var
->marked
= !var
->frozen
&& var
->is_nonneg
&&
1727 !min_is_manifestly_unbounded(tab
, var
);
1732 struct isl_tab_var
*var
;
1733 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
1734 var
= isl_tab_var_from_row(tab
, i
);
1738 if (i
== tab
->n_row
) {
1739 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
1740 var
= var_from_col(tab
, i
);
1744 if (i
== tab
->n_col
)
1749 if ((tab
->rational
? (sign_of_min(tab
, var
) >= 0)
1750 : !isl_tab_min_at_most_neg_one(tab
, var
)) &&
1752 isl_tab_mark_redundant(tab
, var
->index
);
1753 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
1754 var
= var_from_col(tab
, i
);
1757 if (!min_is_manifestly_unbounded(tab
, var
))
1767 int isl_tab_is_equality(struct isl_tab
*tab
, int con
)
1774 if (tab
->con
[con
].is_zero
)
1776 if (tab
->con
[con
].is_redundant
)
1778 if (!tab
->con
[con
].is_row
)
1779 return tab
->con
[con
].index
< tab
->n_dead
;
1781 row
= tab
->con
[con
].index
;
1784 return isl_int_is_zero(tab
->mat
->row
[row
][1]) &&
1785 isl_seq_first_non_zero(tab
->mat
->row
[row
] + 2 + tab
->n_dead
,
1786 tab
->n_col
- tab
->n_dead
) == -1;
1789 /* Return the minimial value of the affine expression "f" with denominator
1790 * "denom" in *opt, *opt_denom, assuming the tableau is not empty and
1791 * the expression cannot attain arbitrarily small values.
1792 * If opt_denom is NULL, then *opt is rounded up to the nearest integer.
1793 * The return value reflects the nature of the result (empty, unbounded,
1794 * minmimal value returned in *opt).
1796 enum isl_lp_result
isl_tab_min(struct isl_tab
*tab
,
1797 isl_int
*f
, isl_int denom
, isl_int
*opt
, isl_int
*opt_denom
,
1801 enum isl_lp_result res
= isl_lp_ok
;
1802 struct isl_tab_var
*var
;
1803 struct isl_tab_undo
*snap
;
1806 return isl_lp_empty
;
1808 snap
= isl_tab_snap(tab
);
1809 r
= isl_tab_add_row(tab
, f
);
1811 return isl_lp_error
;
1813 isl_int_mul(tab
->mat
->row
[var
->index
][0],
1814 tab
->mat
->row
[var
->index
][0], denom
);
1817 find_pivot(tab
, var
, var
, -1, &row
, &col
);
1818 if (row
== var
->index
) {
1819 res
= isl_lp_unbounded
;
1824 isl_tab_pivot(tab
, row
, col
);
1826 if (isl_tab_rollback(tab
, snap
) < 0)
1827 return isl_lp_error
;
1828 if (ISL_FL_ISSET(flags
, ISL_TAB_SAVE_DUAL
)) {
1831 isl_vec_free(tab
->dual
);
1832 tab
->dual
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_con
);
1834 return isl_lp_error
;
1835 isl_int_set(tab
->dual
->el
[0], tab
->mat
->row
[var
->index
][0]);
1836 for (i
= 0; i
< tab
->n_con
; ++i
) {
1837 if (tab
->con
[i
].is_row
)
1838 isl_int_set_si(tab
->dual
->el
[1 + i
], 0);
1840 int pos
= 2 + tab
->con
[i
].index
;
1841 isl_int_set(tab
->dual
->el
[1 + i
],
1842 tab
->mat
->row
[var
->index
][pos
]);
1846 if (res
== isl_lp_ok
) {
1848 isl_int_set(*opt
, tab
->mat
->row
[var
->index
][1]);
1849 isl_int_set(*opt_denom
, tab
->mat
->row
[var
->index
][0]);
1851 isl_int_cdiv_q(*opt
, tab
->mat
->row
[var
->index
][1],
1852 tab
->mat
->row
[var
->index
][0]);
1857 int isl_tab_is_redundant(struct isl_tab
*tab
, int con
)
1864 if (tab
->con
[con
].is_zero
)
1866 if (tab
->con
[con
].is_redundant
)
1868 return tab
->con
[con
].is_row
&& tab
->con
[con
].index
< tab
->n_redundant
;
1871 /* Take a snapshot of the tableau that can be restored by s call to
1874 struct isl_tab_undo
*isl_tab_snap(struct isl_tab
*tab
)
1882 /* Undo the operation performed by isl_tab_relax.
1884 static void unrelax(struct isl_tab
*tab
, struct isl_tab_var
*var
)
1886 unsigned off
= 2 + tab
->M
;
1888 if (!var
->is_row
&& !max_is_manifestly_unbounded(tab
, var
))
1889 to_row(tab
, var
, 1);
1892 isl_int_sub(tab
->mat
->row
[var
->index
][1],
1893 tab
->mat
->row
[var
->index
][1], tab
->mat
->row
[var
->index
][0]);
1897 for (i
= 0; i
< tab
->n_row
; ++i
) {
1898 if (isl_int_is_zero(tab
->mat
->row
[i
][off
+ var
->index
]))
1900 isl_int_add(tab
->mat
->row
[i
][1], tab
->mat
->row
[i
][1],
1901 tab
->mat
->row
[i
][off
+ var
->index
]);
1907 static void perform_undo_var(struct isl_tab
*tab
, struct isl_tab_undo
*undo
)
1909 struct isl_tab_var
*var
= var_from_index(tab
, undo
->u
.var_index
);
1910 switch(undo
->type
) {
1911 case isl_tab_undo_nonneg
:
1914 case isl_tab_undo_redundant
:
1915 var
->is_redundant
= 0;
1918 case isl_tab_undo_zero
:
1922 case isl_tab_undo_allocate
:
1923 if (undo
->u
.var_index
>= 0) {
1924 isl_assert(tab
->mat
->ctx
, !var
->is_row
, return);
1925 drop_col(tab
, var
->index
);
1929 if (!max_is_manifestly_unbounded(tab
, var
))
1930 to_row(tab
, var
, 1);
1931 else if (!min_is_manifestly_unbounded(tab
, var
))
1932 to_row(tab
, var
, -1);
1934 to_row(tab
, var
, 0);
1936 drop_row(tab
, var
->index
);
1938 case isl_tab_undo_relax
:
1944 /* Restore the tableau to the state where the basic variables
1945 * are those in "col_var".
1946 * We first construct a list of variables that are currently in
1947 * the basis, but shouldn't. Then we iterate over all variables
1948 * that should be in the basis and for each one that is currently
1949 * not in the basis, we exchange it with one of the elements of the
1950 * list constructed before.
1951 * We can always find an appropriate variable to pivot with because
1952 * the current basis is mapped to the old basis by a non-singular
1953 * matrix and so we can never end up with a zero row.
1955 static int restore_basis(struct isl_tab
*tab
, int *col_var
)
1959 int *extra
= NULL
; /* current columns that contain bad stuff */
1960 unsigned off
= 2 + tab
->M
;
1962 extra
= isl_alloc_array(tab
->mat
->ctx
, int, tab
->n_col
);
1965 for (i
= 0; i
< tab
->n_col
; ++i
) {
1966 for (j
= 0; j
< tab
->n_col
; ++j
)
1967 if (tab
->col_var
[i
] == col_var
[j
])
1971 extra
[n_extra
++] = i
;
1973 for (i
= 0; i
< tab
->n_col
&& n_extra
> 0; ++i
) {
1974 struct isl_tab_var
*var
;
1977 for (j
= 0; j
< tab
->n_col
; ++j
)
1978 if (col_var
[i
] == tab
->col_var
[j
])
1982 var
= var_from_index(tab
, col_var
[i
]);
1984 for (j
= 0; j
< n_extra
; ++j
)
1985 if (!isl_int_is_zero(tab
->mat
->row
[row
][off
+extra
[j
]]))
1987 isl_assert(tab
->mat
->ctx
, j
< n_extra
, goto error
);
1988 isl_tab_pivot(tab
, row
, extra
[j
]);
1989 extra
[j
] = extra
[--n_extra
];
2001 static int perform_undo(struct isl_tab
*tab
, struct isl_tab_undo
*undo
)
2003 switch (undo
->type
) {
2004 case isl_tab_undo_empty
:
2007 case isl_tab_undo_nonneg
:
2008 case isl_tab_undo_redundant
:
2009 case isl_tab_undo_zero
:
2010 case isl_tab_undo_allocate
:
2011 case isl_tab_undo_relax
:
2012 perform_undo_var(tab
, undo
);
2014 case isl_tab_undo_bset_eq
:
2015 isl_basic_set_free_equality(tab
->bset
, 1);
2017 case isl_tab_undo_bset_ineq
:
2018 isl_basic_set_free_inequality(tab
->bset
, 1);
2020 case isl_tab_undo_bset_div
:
2021 isl_basic_set_free_div(tab
->bset
, 1);
2023 case isl_tab_undo_saved_basis
:
2024 if (restore_basis(tab
, undo
->u
.col_var
) < 0)
2028 isl_assert(tab
->mat
->ctx
, 0, return -1);
2033 /* Return the tableau to the state it was in when the snapshot "snap"
2036 int isl_tab_rollback(struct isl_tab
*tab
, struct isl_tab_undo
*snap
)
2038 struct isl_tab_undo
*undo
, *next
;
2044 for (undo
= tab
->top
; undo
&& undo
!= &tab
->bottom
; undo
= next
) {
2048 if (perform_undo(tab
, undo
) < 0) {
2062 /* The given row "row" represents an inequality violated by all
2063 * points in the tableau. Check for some special cases of such
2064 * separating constraints.
2065 * In particular, if the row has been reduced to the constant -1,
2066 * then we know the inequality is adjacent (but opposite) to
2067 * an equality in the tableau.
2068 * If the row has been reduced to r = -1 -r', with r' an inequality
2069 * of the tableau, then the inequality is adjacent (but opposite)
2070 * to the inequality r'.
2072 static enum isl_ineq_type
separation_type(struct isl_tab
*tab
, unsigned row
)
2075 unsigned off
= 2 + tab
->M
;
2078 return isl_ineq_separate
;
2080 if (!isl_int_is_one(tab
->mat
->row
[row
][0]))
2081 return isl_ineq_separate
;
2082 if (!isl_int_is_negone(tab
->mat
->row
[row
][1]))
2083 return isl_ineq_separate
;
2085 pos
= isl_seq_first_non_zero(tab
->mat
->row
[row
] + off
+ tab
->n_dead
,
2086 tab
->n_col
- tab
->n_dead
);
2088 return isl_ineq_adj_eq
;
2090 if (!isl_int_is_negone(tab
->mat
->row
[row
][off
+ tab
->n_dead
+ pos
]))
2091 return isl_ineq_separate
;
2093 pos
= isl_seq_first_non_zero(
2094 tab
->mat
->row
[row
] + off
+ tab
->n_dead
+ pos
+ 1,
2095 tab
->n_col
- tab
->n_dead
- pos
- 1);
2097 return pos
== -1 ? isl_ineq_adj_ineq
: isl_ineq_separate
;
2100 /* Check the effect of inequality "ineq" on the tableau "tab".
2102 * isl_ineq_redundant: satisfied by all points in the tableau
2103 * isl_ineq_separate: satisfied by no point in the tableau
2104 * isl_ineq_cut: satisfied by some by not all points
2105 * isl_ineq_adj_eq: adjacent to an equality
2106 * isl_ineq_adj_ineq: adjacent to an inequality.
2108 enum isl_ineq_type
isl_tab_ineq_type(struct isl_tab
*tab
, isl_int
*ineq
)
2110 enum isl_ineq_type type
= isl_ineq_error
;
2111 struct isl_tab_undo
*snap
= NULL
;
2116 return isl_ineq_error
;
2118 if (isl_tab_extend_cons(tab
, 1) < 0)
2119 return isl_ineq_error
;
2121 snap
= isl_tab_snap(tab
);
2123 con
= isl_tab_add_row(tab
, ineq
);
2127 row
= tab
->con
[con
].index
;
2128 if (isl_tab_row_is_redundant(tab
, row
))
2129 type
= isl_ineq_redundant
;
2130 else if (isl_int_is_neg(tab
->mat
->row
[row
][1]) &&
2132 isl_int_abs_ge(tab
->mat
->row
[row
][1],
2133 tab
->mat
->row
[row
][0]))) {
2134 if (at_least_zero(tab
, &tab
->con
[con
]))
2135 type
= isl_ineq_cut
;
2137 type
= separation_type(tab
, row
);
2138 } else if (tab
->rational
? (sign_of_min(tab
, &tab
->con
[con
]) < 0)
2139 : isl_tab_min_at_most_neg_one(tab
, &tab
->con
[con
]))
2140 type
= isl_ineq_cut
;
2142 type
= isl_ineq_redundant
;
2144 if (isl_tab_rollback(tab
, snap
))
2145 return isl_ineq_error
;
2148 isl_tab_rollback(tab
, snap
);
2149 return isl_ineq_error
;
2152 void isl_tab_dump(struct isl_tab
*tab
, FILE *out
, int indent
)
2158 fprintf(out
, "%*snull tab\n", indent
, "");
2161 fprintf(out
, "%*sn_redundant: %d, n_dead: %d", indent
, "",
2162 tab
->n_redundant
, tab
->n_dead
);
2164 fprintf(out
, ", rational");
2166 fprintf(out
, ", empty");
2168 fprintf(out
, "%*s[", indent
, "");
2169 for (i
= 0; i
< tab
->n_var
; ++i
) {
2171 fprintf(out
, (i
== tab
->n_param
||
2172 i
== tab
->n_var
- tab
->n_div
) ? "; "
2174 fprintf(out
, "%c%d%s", tab
->var
[i
].is_row
? 'r' : 'c',
2176 tab
->var
[i
].is_zero
? " [=0]" :
2177 tab
->var
[i
].is_redundant
? " [R]" : "");
2179 fprintf(out
, "]\n");
2180 fprintf(out
, "%*s[", indent
, "");
2181 for (i
= 0; i
< tab
->n_con
; ++i
) {
2184 fprintf(out
, "%c%d%s", tab
->con
[i
].is_row
? 'r' : 'c',
2186 tab
->con
[i
].is_zero
? " [=0]" :
2187 tab
->con
[i
].is_redundant
? " [R]" : "");
2189 fprintf(out
, "]\n");
2190 fprintf(out
, "%*s[", indent
, "");
2191 for (i
= 0; i
< tab
->n_row
; ++i
) {
2194 fprintf(out
, "r%d: %d%s", i
, tab
->row_var
[i
],
2195 isl_tab_var_from_row(tab
, i
)->is_nonneg
? " [>=0]" : "");
2197 fprintf(out
, "]\n");
2198 fprintf(out
, "%*s[", indent
, "");
2199 for (i
= 0; i
< tab
->n_col
; ++i
) {
2202 fprintf(out
, "c%d: %d%s", i
, tab
->col_var
[i
],
2203 var_from_col(tab
, i
)->is_nonneg
? " [>=0]" : "");
2205 fprintf(out
, "]\n");
2206 r
= tab
->mat
->n_row
;
2207 tab
->mat
->n_row
= tab
->n_row
;
2208 c
= tab
->mat
->n_col
;
2209 tab
->mat
->n_col
= 2 + tab
->M
+ tab
->n_col
;
2210 isl_mat_dump(tab
->mat
, out
, indent
);
2211 tab
->mat
->n_row
= r
;
2212 tab
->mat
->n_col
= c
;
2214 isl_basic_set_dump(tab
->bset
, out
, indent
);