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
);
167 struct isl_tab
*isl_tab_dup(struct isl_tab
*tab
)
175 dup
= isl_calloc_type(tab
->ctx
, struct isl_tab
);
178 dup
->mat
= isl_mat_dup(tab
->mat
);
181 dup
->var
= isl_alloc_array(tab
->ctx
, struct isl_tab_var
, tab
->max_var
);
184 for (i
= 0; i
< tab
->n_var
; ++i
)
185 dup
->var
[i
] = tab
->var
[i
];
186 dup
->con
= isl_alloc_array(tab
->ctx
, struct isl_tab_var
, tab
->max_con
);
189 for (i
= 0; i
< tab
->n_con
; ++i
)
190 dup
->con
[i
] = tab
->con
[i
];
191 dup
->col_var
= isl_alloc_array(tab
->ctx
, int, tab
->mat
->n_col
);
194 for (i
= 0; i
< tab
->n_var
; ++i
)
195 dup
->col_var
[i
] = tab
->col_var
[i
];
196 dup
->row_var
= isl_alloc_array(tab
->ctx
, int, tab
->mat
->n_row
);
199 for (i
= 0; i
< tab
->n_row
; ++i
)
200 dup
->row_var
[i
] = tab
->row_var
[i
];
201 dup
->n_row
= tab
->n_row
;
202 dup
->n_con
= tab
->n_con
;
203 dup
->n_eq
= tab
->n_eq
;
204 dup
->max_con
= tab
->max_con
;
205 dup
->n_col
= tab
->n_col
;
206 dup
->n_var
= tab
->n_var
;
207 dup
->max_var
= tab
->max_var
;
208 dup
->n_param
= tab
->n_param
;
209 dup
->n_div
= tab
->n_div
;
210 dup
->n_dead
= tab
->n_dead
;
211 dup
->n_redundant
= tab
->n_redundant
;
212 dup
->rational
= tab
->rational
;
213 dup
->empty
= tab
->empty
;
217 dup
->bottom
.type
= isl_tab_undo_bottom
;
218 dup
->bottom
.next
= NULL
;
219 dup
->top
= &dup
->bottom
;
226 static struct isl_tab_var
*var_from_index(struct isl_tab
*tab
, int i
)
231 return &tab
->con
[~i
];
234 struct isl_tab_var
*isl_tab_var_from_row(struct isl_tab
*tab
, int i
)
236 return var_from_index(tab
, tab
->row_var
[i
]);
239 static struct isl_tab_var
*var_from_col(struct isl_tab
*tab
, int i
)
241 return var_from_index(tab
, tab
->col_var
[i
]);
244 /* Check if there are any upper bounds on column variable "var",
245 * i.e., non-negative rows where var appears with a negative coefficient.
246 * Return 1 if there are no such bounds.
248 static int max_is_manifestly_unbounded(struct isl_tab
*tab
,
249 struct isl_tab_var
*var
)
252 unsigned off
= 2 + tab
->M
;
256 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
257 if (!isl_int_is_neg(tab
->mat
->row
[i
][off
+ var
->index
]))
259 if (isl_tab_var_from_row(tab
, i
)->is_nonneg
)
265 /* Check if there are any lower bounds on column variable "var",
266 * i.e., non-negative rows where var appears with a positive coefficient.
267 * Return 1 if there are no such bounds.
269 static int min_is_manifestly_unbounded(struct isl_tab
*tab
,
270 struct isl_tab_var
*var
)
273 unsigned off
= 2 + tab
->M
;
277 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
278 if (!isl_int_is_pos(tab
->mat
->row
[i
][off
+ var
->index
]))
280 if (isl_tab_var_from_row(tab
, i
)->is_nonneg
)
286 static int row_cmp(struct isl_tab
*tab
, int r1
, int r2
, int c
, isl_int t
)
288 unsigned off
= 2 + tab
->M
;
292 isl_int_mul(t
, tab
->mat
->row
[r1
][2], tab
->mat
->row
[r2
][off
+c
]);
293 isl_int_submul(t
, tab
->mat
->row
[r2
][2], tab
->mat
->row
[r1
][off
+c
]);
298 isl_int_mul(t
, tab
->mat
->row
[r1
][1], tab
->mat
->row
[r2
][off
+ c
]);
299 isl_int_submul(t
, tab
->mat
->row
[r2
][1], tab
->mat
->row
[r1
][off
+ c
]);
300 return isl_int_sgn(t
);
303 /* Given the index of a column "c", return the index of a row
304 * that can be used to pivot the column in, with either an increase
305 * (sgn > 0) or a decrease (sgn < 0) of the corresponding variable.
306 * If "var" is not NULL, then the row returned will be different from
307 * the one associated with "var".
309 * Each row in the tableau is of the form
311 * x_r = a_r0 + \sum_i a_ri x_i
313 * Only rows with x_r >= 0 and with the sign of a_ri opposite to "sgn"
314 * impose any limit on the increase or decrease in the value of x_c
315 * and this bound is equal to a_r0 / |a_rc|. We are therefore looking
316 * for the row with the smallest (most stringent) such bound.
317 * Note that the common denominator of each row drops out of the fraction.
318 * To check if row j has a smaller bound than row r, i.e.,
319 * a_j0 / |a_jc| < a_r0 / |a_rc| or a_j0 |a_rc| < a_r0 |a_jc|,
320 * we check if -sign(a_jc) (a_j0 a_rc - a_r0 a_jc) < 0,
321 * where -sign(a_jc) is equal to "sgn".
323 static int pivot_row(struct isl_tab
*tab
,
324 struct isl_tab_var
*var
, int sgn
, int c
)
328 unsigned off
= 2 + tab
->M
;
332 for (j
= tab
->n_redundant
; j
< tab
->n_row
; ++j
) {
333 if (var
&& j
== var
->index
)
335 if (!isl_tab_var_from_row(tab
, j
)->is_nonneg
)
337 if (sgn
* isl_int_sgn(tab
->mat
->row
[j
][off
+ c
]) >= 0)
343 tsgn
= sgn
* row_cmp(tab
, r
, j
, c
, t
);
344 if (tsgn
< 0 || (tsgn
== 0 &&
345 tab
->row_var
[j
] < tab
->row_var
[r
]))
352 /* Find a pivot (row and col) that will increase (sgn > 0) or decrease
353 * (sgn < 0) the value of row variable var.
354 * If not NULL, then skip_var is a row variable that should be ignored
355 * while looking for a pivot row. It is usually equal to var.
357 * As the given row in the tableau is of the form
359 * x_r = a_r0 + \sum_i a_ri x_i
361 * we need to find a column such that the sign of a_ri is equal to "sgn"
362 * (such that an increase in x_i will have the desired effect) or a
363 * column with a variable that may attain negative values.
364 * If a_ri is positive, then we need to move x_i in the same direction
365 * to obtain the desired effect. Otherwise, x_i has to move in the
366 * opposite direction.
368 static void find_pivot(struct isl_tab
*tab
,
369 struct isl_tab_var
*var
, struct isl_tab_var
*skip_var
,
370 int sgn
, int *row
, int *col
)
377 isl_assert(tab
->mat
->ctx
, var
->is_row
, return);
378 tr
= tab
->mat
->row
[var
->index
] + 2 + tab
->M
;
381 for (j
= tab
->n_dead
; j
< tab
->n_col
; ++j
) {
382 if (isl_int_is_zero(tr
[j
]))
384 if (isl_int_sgn(tr
[j
]) != sgn
&&
385 var_from_col(tab
, j
)->is_nonneg
)
387 if (c
< 0 || tab
->col_var
[j
] < tab
->col_var
[c
])
393 sgn
*= isl_int_sgn(tr
[c
]);
394 r
= pivot_row(tab
, skip_var
, sgn
, c
);
395 *row
= r
< 0 ? var
->index
: r
;
399 /* Return 1 if row "row" represents an obviously redundant inequality.
401 * - it represents an inequality or a variable
402 * - that is the sum of a non-negative sample value and a positive
403 * combination of zero or more non-negative variables.
405 int isl_tab_row_is_redundant(struct isl_tab
*tab
, int row
)
408 unsigned off
= 2 + tab
->M
;
410 if (tab
->row_var
[row
] < 0 && !isl_tab_var_from_row(tab
, row
)->is_nonneg
)
413 if (isl_int_is_neg(tab
->mat
->row
[row
][1]))
415 if (tab
->M
&& isl_int_is_neg(tab
->mat
->row
[row
][2]))
418 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
419 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ i
]))
421 if (isl_int_is_neg(tab
->mat
->row
[row
][off
+ i
]))
423 if (!var_from_col(tab
, i
)->is_nonneg
)
429 static void swap_rows(struct isl_tab
*tab
, int row1
, int row2
)
432 t
= tab
->row_var
[row1
];
433 tab
->row_var
[row1
] = tab
->row_var
[row2
];
434 tab
->row_var
[row2
] = t
;
435 isl_tab_var_from_row(tab
, row1
)->index
= row1
;
436 isl_tab_var_from_row(tab
, row2
)->index
= row2
;
437 tab
->mat
= isl_mat_swap_rows(tab
->mat
, row1
, row2
);
440 static void push_union(struct isl_tab
*tab
,
441 enum isl_tab_undo_type type
, union isl_tab_undo_val u
)
443 struct isl_tab_undo
*undo
;
448 undo
= isl_alloc_type(tab
->mat
->ctx
, struct isl_tab_undo
);
456 undo
->next
= tab
->top
;
460 void isl_tab_push_var(struct isl_tab
*tab
,
461 enum isl_tab_undo_type type
, struct isl_tab_var
*var
)
463 union isl_tab_undo_val u
;
465 u
.var_index
= tab
->row_var
[var
->index
];
467 u
.var_index
= tab
->col_var
[var
->index
];
468 push_union(tab
, type
, u
);
471 void isl_tab_push(struct isl_tab
*tab
, enum isl_tab_undo_type type
)
473 union isl_tab_undo_val u
= { 0 };
474 push_union(tab
, type
, u
);
477 /* Push a record on the undo stack describing the current basic
478 * variables, so that the this state can be restored during rollback.
480 void isl_tab_push_basis(struct isl_tab
*tab
)
483 union isl_tab_undo_val u
;
485 u
.col_var
= isl_alloc_array(tab
->mat
->ctx
, int, tab
->n_col
);
491 for (i
= 0; i
< tab
->n_col
; ++i
)
492 u
.col_var
[i
] = tab
->col_var
[i
];
493 push_union(tab
, isl_tab_undo_saved_basis
, u
);
496 /* Mark row with index "row" as being redundant.
497 * If we may need to undo the operation or if the row represents
498 * a variable of the original problem, the row is kept,
499 * but no longer considered when looking for a pivot row.
500 * Otherwise, the row is simply removed.
502 * The row may be interchanged with some other row. If it
503 * is interchanged with a later row, return 1. Otherwise return 0.
504 * If the rows are checked in order in the calling function,
505 * then a return value of 1 means that the row with the given
506 * row number may now contain a different row that hasn't been checked yet.
508 int isl_tab_mark_redundant(struct isl_tab
*tab
, int row
)
510 struct isl_tab_var
*var
= isl_tab_var_from_row(tab
, row
);
511 var
->is_redundant
= 1;
512 isl_assert(tab
->mat
->ctx
, row
>= tab
->n_redundant
, return);
513 if (tab
->need_undo
|| tab
->row_var
[row
] >= 0) {
514 if (tab
->row_var
[row
] >= 0 && !var
->is_nonneg
) {
516 isl_tab_push_var(tab
, isl_tab_undo_nonneg
, var
);
518 if (row
!= tab
->n_redundant
)
519 swap_rows(tab
, row
, tab
->n_redundant
);
520 isl_tab_push_var(tab
, isl_tab_undo_redundant
, var
);
524 if (row
!= tab
->n_row
- 1)
525 swap_rows(tab
, row
, tab
->n_row
- 1);
526 isl_tab_var_from_row(tab
, tab
->n_row
- 1)->index
= -1;
532 struct isl_tab
*isl_tab_mark_empty(struct isl_tab
*tab
)
534 if (!tab
->empty
&& tab
->need_undo
)
535 isl_tab_push(tab
, isl_tab_undo_empty
);
540 /* Given a row number "row" and a column number "col", pivot the tableau
541 * such that the associated variables are interchanged.
542 * The given row in the tableau expresses
544 * x_r = a_r0 + \sum_i a_ri x_i
548 * x_c = 1/a_rc x_r - a_r0/a_rc + sum_{i \ne r} -a_ri/a_rc
550 * Substituting this equality into the other rows
552 * x_j = a_j0 + \sum_i a_ji x_i
554 * with a_jc \ne 0, we obtain
556 * 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
563 * where i is any other column and j is any other row,
564 * is therefore transformed into
566 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
567 * 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)
569 * The transformation is performed along the following steps
574 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
577 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
578 * n_jc/(|n_rc| d_j) n_ji/(|n_rc| d_j)
580 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
581 * n_jc/(|n_rc| d_j) (n_ji |n_rc|)/(|n_rc| d_j)
583 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
584 * n_jc/(|n_rc| d_j) (n_ji |n_rc| - s(n_rc)n_jc n_ri)/(|n_rc| d_j)
586 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
587 * 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)
590 void isl_tab_pivot(struct isl_tab
*tab
, int row
, int col
)
595 struct isl_mat
*mat
= tab
->mat
;
596 struct isl_tab_var
*var
;
597 unsigned off
= 2 + tab
->M
;
599 isl_int_swap(mat
->row
[row
][0], mat
->row
[row
][off
+ col
]);
600 sgn
= isl_int_sgn(mat
->row
[row
][0]);
602 isl_int_neg(mat
->row
[row
][0], mat
->row
[row
][0]);
603 isl_int_neg(mat
->row
[row
][off
+ col
], mat
->row
[row
][off
+ col
]);
605 for (j
= 0; j
< off
- 1 + tab
->n_col
; ++j
) {
606 if (j
== off
- 1 + col
)
608 isl_int_neg(mat
->row
[row
][1 + j
], mat
->row
[row
][1 + j
]);
610 if (!isl_int_is_one(mat
->row
[row
][0]))
611 isl_seq_normalize(mat
->row
[row
], off
+ tab
->n_col
);
612 for (i
= 0; i
< tab
->n_row
; ++i
) {
615 if (isl_int_is_zero(mat
->row
[i
][off
+ col
]))
617 isl_int_mul(mat
->row
[i
][0], mat
->row
[i
][0], mat
->row
[row
][0]);
618 for (j
= 0; j
< off
- 1 + tab
->n_col
; ++j
) {
619 if (j
== off
- 1 + col
)
621 isl_int_mul(mat
->row
[i
][1 + j
],
622 mat
->row
[i
][1 + j
], mat
->row
[row
][0]);
623 isl_int_addmul(mat
->row
[i
][1 + j
],
624 mat
->row
[i
][off
+ col
], mat
->row
[row
][1 + j
]);
626 isl_int_mul(mat
->row
[i
][off
+ col
],
627 mat
->row
[i
][off
+ col
], mat
->row
[row
][off
+ col
]);
628 if (!isl_int_is_one(mat
->row
[i
][0]))
629 isl_seq_normalize(mat
->row
[i
], off
+ tab
->n_col
);
631 t
= tab
->row_var
[row
];
632 tab
->row_var
[row
] = tab
->col_var
[col
];
633 tab
->col_var
[col
] = t
;
634 var
= isl_tab_var_from_row(tab
, row
);
637 var
= var_from_col(tab
, col
);
642 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
643 if (isl_int_is_zero(mat
->row
[i
][off
+ col
]))
645 if (!isl_tab_var_from_row(tab
, i
)->frozen
&&
646 isl_tab_row_is_redundant(tab
, i
))
647 if (isl_tab_mark_redundant(tab
, i
))
652 /* If "var" represents a column variable, then pivot is up (sgn > 0)
653 * or down (sgn < 0) to a row. The variable is assumed not to be
654 * unbounded in the specified direction.
655 * If sgn = 0, then the variable is unbounded in both directions,
656 * and we pivot with any row we can find.
658 static void to_row(struct isl_tab
*tab
, struct isl_tab_var
*var
, int sign
)
661 unsigned off
= 2 + tab
->M
;
667 for (r
= tab
->n_redundant
; r
< tab
->n_row
; ++r
)
668 if (!isl_int_is_zero(tab
->mat
->row
[r
][off
+var
->index
]))
670 isl_assert(tab
->mat
->ctx
, r
< tab
->n_row
, return);
672 r
= pivot_row(tab
, NULL
, sign
, var
->index
);
673 isl_assert(tab
->mat
->ctx
, r
>= 0, return);
676 isl_tab_pivot(tab
, r
, var
->index
);
679 static void check_table(struct isl_tab
*tab
)
685 for (i
= 0; i
< tab
->n_row
; ++i
) {
686 if (!isl_tab_var_from_row(tab
, i
)->is_nonneg
)
688 assert(!isl_int_is_neg(tab
->mat
->row
[i
][1]));
692 /* Return the sign of the maximal value of "var".
693 * If the sign is not negative, then on return from this function,
694 * the sample value will also be non-negative.
696 * If "var" is manifestly unbounded wrt positive values, we are done.
697 * Otherwise, we pivot the variable up to a row if needed
698 * Then we continue pivoting down until either
699 * - no more down pivots can be performed
700 * - the sample value is positive
701 * - the variable is pivoted into a manifestly unbounded column
703 static int sign_of_max(struct isl_tab
*tab
, struct isl_tab_var
*var
)
707 if (max_is_manifestly_unbounded(tab
, var
))
710 while (!isl_int_is_pos(tab
->mat
->row
[var
->index
][1])) {
711 find_pivot(tab
, var
, var
, 1, &row
, &col
);
713 return isl_int_sgn(tab
->mat
->row
[var
->index
][1]);
714 isl_tab_pivot(tab
, row
, col
);
715 if (!var
->is_row
) /* manifestly unbounded */
721 static int row_is_neg(struct isl_tab
*tab
, int row
)
724 return isl_int_is_neg(tab
->mat
->row
[row
][1]);
725 if (isl_int_is_pos(tab
->mat
->row
[row
][2]))
727 if (isl_int_is_neg(tab
->mat
->row
[row
][2]))
729 return isl_int_is_neg(tab
->mat
->row
[row
][1]);
732 static int row_sgn(struct isl_tab
*tab
, int row
)
735 return isl_int_sgn(tab
->mat
->row
[row
][1]);
736 if (!isl_int_is_zero(tab
->mat
->row
[row
][2]))
737 return isl_int_sgn(tab
->mat
->row
[row
][2]);
739 return isl_int_sgn(tab
->mat
->row
[row
][1]);
742 /* Perform pivots until the row variable "var" has a non-negative
743 * sample value or until no more upward pivots can be performed.
744 * Return the sign of the sample value after the pivots have been
747 static int restore_row(struct isl_tab
*tab
, struct isl_tab_var
*var
)
751 while (row_is_neg(tab
, var
->index
)) {
752 find_pivot(tab
, var
, var
, 1, &row
, &col
);
755 isl_tab_pivot(tab
, row
, col
);
756 if (!var
->is_row
) /* manifestly unbounded */
759 return row_sgn(tab
, var
->index
);
762 /* Perform pivots until we are sure that the row variable "var"
763 * can attain non-negative values. After return from this
764 * function, "var" is still a row variable, but its sample
765 * value may not be non-negative, even if the function returns 1.
767 static int at_least_zero(struct isl_tab
*tab
, struct isl_tab_var
*var
)
771 while (isl_int_is_neg(tab
->mat
->row
[var
->index
][1])) {
772 find_pivot(tab
, var
, var
, 1, &row
, &col
);
775 if (row
== var
->index
) /* manifestly unbounded */
777 isl_tab_pivot(tab
, row
, col
);
779 return !isl_int_is_neg(tab
->mat
->row
[var
->index
][1]);
782 /* Return a negative value if "var" can attain negative values.
783 * Return a non-negative value otherwise.
785 * If "var" is manifestly unbounded wrt negative values, we are done.
786 * Otherwise, if var is in a column, we can pivot it down to a row.
787 * Then we continue pivoting down until either
788 * - the pivot would result in a manifestly unbounded column
789 * => we don't perform the pivot, but simply return -1
790 * - no more down pivots can be performed
791 * - the sample value is negative
792 * If the sample value becomes negative and the variable is supposed
793 * to be nonnegative, then we undo the last pivot.
794 * However, if the last pivot has made the pivoting variable
795 * obviously redundant, then it may have moved to another row.
796 * In that case we look for upward pivots until we reach a non-negative
799 static int sign_of_min(struct isl_tab
*tab
, struct isl_tab_var
*var
)
802 struct isl_tab_var
*pivot_var
;
804 if (min_is_manifestly_unbounded(tab
, var
))
808 row
= pivot_row(tab
, NULL
, -1, col
);
809 pivot_var
= var_from_col(tab
, col
);
810 isl_tab_pivot(tab
, row
, col
);
811 if (var
->is_redundant
)
813 if (isl_int_is_neg(tab
->mat
->row
[var
->index
][1])) {
814 if (var
->is_nonneg
) {
815 if (!pivot_var
->is_redundant
&&
816 pivot_var
->index
== row
)
817 isl_tab_pivot(tab
, row
, col
);
819 restore_row(tab
, var
);
824 if (var
->is_redundant
)
826 while (!isl_int_is_neg(tab
->mat
->row
[var
->index
][1])) {
827 find_pivot(tab
, var
, var
, -1, &row
, &col
);
828 if (row
== var
->index
)
831 return isl_int_sgn(tab
->mat
->row
[var
->index
][1]);
832 pivot_var
= var_from_col(tab
, col
);
833 isl_tab_pivot(tab
, row
, col
);
834 if (var
->is_redundant
)
837 if (var
->is_nonneg
) {
838 /* pivot back to non-negative value */
839 if (!pivot_var
->is_redundant
&& pivot_var
->index
== row
)
840 isl_tab_pivot(tab
, row
, col
);
842 restore_row(tab
, var
);
847 static int row_at_most_neg_one(struct isl_tab
*tab
, int row
)
850 if (isl_int_is_pos(tab
->mat
->row
[row
][2]))
852 if (isl_int_is_neg(tab
->mat
->row
[row
][2]))
855 return isl_int_is_neg(tab
->mat
->row
[row
][1]) &&
856 isl_int_abs_ge(tab
->mat
->row
[row
][1],
857 tab
->mat
->row
[row
][0]);
860 /* Return 1 if "var" can attain values <= -1.
861 * Return 0 otherwise.
863 * The sample value of "var" is assumed to be non-negative when the
864 * the function is called and will be made non-negative again before
865 * the function returns.
867 int isl_tab_min_at_most_neg_one(struct isl_tab
*tab
, struct isl_tab_var
*var
)
870 struct isl_tab_var
*pivot_var
;
872 if (min_is_manifestly_unbounded(tab
, var
))
876 row
= pivot_row(tab
, NULL
, -1, col
);
877 pivot_var
= var_from_col(tab
, col
);
878 isl_tab_pivot(tab
, row
, col
);
879 if (var
->is_redundant
)
881 if (row_at_most_neg_one(tab
, var
->index
)) {
882 if (var
->is_nonneg
) {
883 if (!pivot_var
->is_redundant
&&
884 pivot_var
->index
== row
)
885 isl_tab_pivot(tab
, row
, col
);
887 restore_row(tab
, var
);
892 if (var
->is_redundant
)
895 find_pivot(tab
, var
, var
, -1, &row
, &col
);
896 if (row
== var
->index
)
900 pivot_var
= var_from_col(tab
, col
);
901 isl_tab_pivot(tab
, row
, col
);
902 if (var
->is_redundant
)
904 } while (!row_at_most_neg_one(tab
, var
->index
));
905 if (var
->is_nonneg
) {
906 /* pivot back to non-negative value */
907 if (!pivot_var
->is_redundant
&& pivot_var
->index
== row
)
908 isl_tab_pivot(tab
, row
, col
);
909 restore_row(tab
, var
);
914 /* Return 1 if "var" can attain values >= 1.
915 * Return 0 otherwise.
917 static int at_least_one(struct isl_tab
*tab
, struct isl_tab_var
*var
)
922 if (max_is_manifestly_unbounded(tab
, var
))
925 r
= tab
->mat
->row
[var
->index
];
926 while (isl_int_lt(r
[1], r
[0])) {
927 find_pivot(tab
, var
, var
, 1, &row
, &col
);
929 return isl_int_ge(r
[1], r
[0]);
930 if (row
== var
->index
) /* manifestly unbounded */
932 isl_tab_pivot(tab
, row
, col
);
937 static void swap_cols(struct isl_tab
*tab
, int col1
, int col2
)
940 unsigned off
= 2 + tab
->M
;
941 t
= tab
->col_var
[col1
];
942 tab
->col_var
[col1
] = tab
->col_var
[col2
];
943 tab
->col_var
[col2
] = t
;
944 var_from_col(tab
, col1
)->index
= col1
;
945 var_from_col(tab
, col2
)->index
= col2
;
946 tab
->mat
= isl_mat_swap_cols(tab
->mat
, off
+ col1
, off
+ col2
);
949 /* Mark column with index "col" as representing a zero variable.
950 * If we may need to undo the operation the column is kept,
951 * but no longer considered.
952 * Otherwise, the column is simply removed.
954 * The column may be interchanged with some other column. If it
955 * is interchanged with a later column, return 1. Otherwise return 0.
956 * If the columns are checked in order in the calling function,
957 * then a return value of 1 means that the column with the given
958 * column number may now contain a different column that
959 * hasn't been checked yet.
961 int isl_tab_kill_col(struct isl_tab
*tab
, int col
)
963 var_from_col(tab
, col
)->is_zero
= 1;
964 if (tab
->need_undo
) {
965 isl_tab_push_var(tab
, isl_tab_undo_zero
, var_from_col(tab
, col
));
966 if (col
!= tab
->n_dead
)
967 swap_cols(tab
, col
, tab
->n_dead
);
971 if (col
!= tab
->n_col
- 1)
972 swap_cols(tab
, col
, tab
->n_col
- 1);
973 var_from_col(tab
, tab
->n_col
- 1)->index
= -1;
979 /* Row variable "var" is non-negative and cannot attain any values
980 * larger than zero. This means that the coefficients of the unrestricted
981 * column variables are zero and that the coefficients of the non-negative
982 * column variables are zero or negative.
983 * Each of the non-negative variables with a negative coefficient can
984 * then also be written as the negative sum of non-negative variables
985 * and must therefore also be zero.
987 static void close_row(struct isl_tab
*tab
, struct isl_tab_var
*var
)
990 struct isl_mat
*mat
= tab
->mat
;
991 unsigned off
= 2 + tab
->M
;
993 isl_assert(tab
->mat
->ctx
, var
->is_nonneg
, return);
995 for (j
= tab
->n_dead
; j
< tab
->n_col
; ++j
) {
996 if (isl_int_is_zero(mat
->row
[var
->index
][off
+ j
]))
998 isl_assert(tab
->mat
->ctx
,
999 isl_int_is_neg(mat
->row
[var
->index
][off
+ j
]), return);
1000 if (isl_tab_kill_col(tab
, j
))
1003 isl_tab_mark_redundant(tab
, var
->index
);
1006 /* Add a constraint to the tableau and allocate a row for it.
1007 * Return the index into the constraint array "con".
1009 int isl_tab_allocate_con(struct isl_tab
*tab
)
1013 isl_assert(tab
->mat
->ctx
, tab
->n_row
< tab
->mat
->n_row
, return -1);
1016 tab
->con
[r
].index
= tab
->n_row
;
1017 tab
->con
[r
].is_row
= 1;
1018 tab
->con
[r
].is_nonneg
= 0;
1019 tab
->con
[r
].is_zero
= 0;
1020 tab
->con
[r
].is_redundant
= 0;
1021 tab
->con
[r
].frozen
= 0;
1022 tab
->row_var
[tab
->n_row
] = ~r
;
1026 isl_tab_push_var(tab
, isl_tab_undo_allocate
, &tab
->con
[r
]);
1031 /* Add a variable to the tableau and allocate a column for it.
1032 * Return the index into the variable array "var".
1034 int isl_tab_allocate_var(struct isl_tab
*tab
)
1038 unsigned off
= 2 + tab
->M
;
1040 isl_assert(tab
->mat
->ctx
, tab
->n_col
< tab
->mat
->n_col
, return -1);
1041 isl_assert(tab
->mat
->ctx
, tab
->n_var
< tab
->max_var
, return -1);
1044 tab
->var
[r
].index
= tab
->n_col
;
1045 tab
->var
[r
].is_row
= 0;
1046 tab
->var
[r
].is_nonneg
= 0;
1047 tab
->var
[r
].is_zero
= 0;
1048 tab
->var
[r
].is_redundant
= 0;
1049 tab
->var
[r
].frozen
= 0;
1050 tab
->col_var
[tab
->n_col
] = r
;
1052 for (i
= 0; i
< tab
->n_row
; ++i
)
1053 isl_int_set_si(tab
->mat
->row
[i
][off
+ tab
->n_col
], 0);
1057 isl_tab_push_var(tab
, isl_tab_undo_allocate
, &tab
->var
[r
]);
1062 /* Add a row to the tableau. The row is given as an affine combination
1063 * of the original variables and needs to be expressed in terms of the
1066 * We add each term in turn.
1067 * If r = n/d_r is the current sum and we need to add k x, then
1068 * if x is a column variable, we increase the numerator of
1069 * this column by k d_r
1070 * if x = f/d_x is a row variable, then the new representation of r is
1072 * n k f d_x/g n + d_r/g k f m/d_r n + m/d_g k f
1073 * --- + --- = ------------------- = -------------------
1074 * d_r d_r d_r d_x/g m
1076 * with g the gcd of d_r and d_x and m the lcm of d_r and d_x.
1078 int isl_tab_add_row(struct isl_tab
*tab
, isl_int
*line
)
1084 unsigned off
= 2 + tab
->M
;
1086 r
= isl_tab_allocate_con(tab
);
1092 row
= tab
->mat
->row
[tab
->con
[r
].index
];
1093 isl_int_set_si(row
[0], 1);
1094 isl_int_set(row
[1], line
[0]);
1095 isl_seq_clr(row
+ 2, tab
->M
+ tab
->n_col
);
1096 for (i
= 0; i
< tab
->n_var
; ++i
) {
1097 if (tab
->var
[i
].is_zero
)
1099 if (tab
->var
[i
].is_row
) {
1101 row
[0], tab
->mat
->row
[tab
->var
[i
].index
][0]);
1102 isl_int_swap(a
, row
[0]);
1103 isl_int_divexact(a
, row
[0], a
);
1105 row
[0], tab
->mat
->row
[tab
->var
[i
].index
][0]);
1106 isl_int_mul(b
, b
, line
[1 + i
]);
1107 isl_seq_combine(row
+ 1, a
, row
+ 1,
1108 b
, tab
->mat
->row
[tab
->var
[i
].index
] + 1,
1109 1 + tab
->M
+ tab
->n_col
);
1111 isl_int_addmul(row
[off
+ tab
->var
[i
].index
],
1112 line
[1 + i
], row
[0]);
1113 if (tab
->M
&& i
>= tab
->n_param
&& i
< tab
->n_var
- tab
->n_div
)
1114 isl_int_submul(row
[2], line
[1 + i
], row
[0]);
1116 isl_seq_normalize(row
, off
+ tab
->n_col
);
1123 static int drop_row(struct isl_tab
*tab
, int row
)
1125 isl_assert(tab
->mat
->ctx
, ~tab
->row_var
[row
] == tab
->n_con
- 1, return -1);
1126 if (row
!= tab
->n_row
- 1)
1127 swap_rows(tab
, row
, tab
->n_row
- 1);
1133 static int drop_col(struct isl_tab
*tab
, int col
)
1135 isl_assert(tab
->mat
->ctx
, tab
->col_var
[col
] == tab
->n_var
- 1, return -1);
1136 if (col
!= tab
->n_col
- 1)
1137 swap_cols(tab
, col
, tab
->n_col
- 1);
1143 /* Add inequality "ineq" and check if it conflicts with the
1144 * previously added constraints or if it is obviously redundant.
1146 struct isl_tab
*isl_tab_add_ineq(struct isl_tab
*tab
, isl_int
*ineq
)
1153 r
= isl_tab_add_row(tab
, ineq
);
1156 tab
->con
[r
].is_nonneg
= 1;
1157 isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]);
1158 if (isl_tab_row_is_redundant(tab
, tab
->con
[r
].index
)) {
1159 isl_tab_mark_redundant(tab
, tab
->con
[r
].index
);
1163 sgn
= restore_row(tab
, &tab
->con
[r
]);
1165 return isl_tab_mark_empty(tab
);
1166 if (tab
->con
[r
].is_row
&& isl_tab_row_is_redundant(tab
, tab
->con
[r
].index
))
1167 isl_tab_mark_redundant(tab
, tab
->con
[r
].index
);
1174 /* Pivot a non-negative variable down until it reaches the value zero
1175 * and then pivot the variable into a column position.
1177 int to_col(struct isl_tab
*tab
, struct isl_tab_var
*var
)
1181 unsigned off
= 2 + tab
->M
;
1186 while (isl_int_is_pos(tab
->mat
->row
[var
->index
][1])) {
1187 find_pivot(tab
, var
, NULL
, -1, &row
, &col
);
1188 isl_assert(tab
->mat
->ctx
, row
!= -1, return -1);
1189 isl_tab_pivot(tab
, row
, col
);
1194 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
)
1195 if (!isl_int_is_zero(tab
->mat
->row
[var
->index
][off
+ i
]))
1198 isl_assert(tab
->mat
->ctx
, i
< tab
->n_col
, return -1);
1199 isl_tab_pivot(tab
, var
->index
, i
);
1204 /* We assume Gaussian elimination has been performed on the equalities.
1205 * The equalities can therefore never conflict.
1206 * Adding the equalities is currently only really useful for a later call
1207 * to isl_tab_ineq_type.
1209 static struct isl_tab
*add_eq(struct isl_tab
*tab
, isl_int
*eq
)
1216 r
= isl_tab_add_row(tab
, eq
);
1220 r
= tab
->con
[r
].index
;
1221 i
= isl_seq_first_non_zero(tab
->mat
->row
[r
] + 2 + tab
->M
+ tab
->n_dead
,
1222 tab
->n_col
- tab
->n_dead
);
1223 isl_assert(tab
->mat
->ctx
, i
>= 0, goto error
);
1225 isl_tab_pivot(tab
, r
, i
);
1226 isl_tab_kill_col(tab
, i
);
1235 /* Add an equality that is known to be valid for the given tableau.
1237 struct isl_tab
*isl_tab_add_valid_eq(struct isl_tab
*tab
, isl_int
*eq
)
1239 struct isl_tab_var
*var
;
1245 r
= isl_tab_add_row(tab
, eq
);
1251 if (isl_int_is_neg(tab
->mat
->row
[r
][1]))
1252 isl_seq_neg(tab
->mat
->row
[r
] + 1, tab
->mat
->row
[r
] + 1,
1255 if (to_col(tab
, var
) < 0)
1258 isl_tab_kill_col(tab
, var
->index
);
1266 struct isl_tab
*isl_tab_from_basic_map(struct isl_basic_map
*bmap
)
1269 struct isl_tab
*tab
;
1273 tab
= isl_tab_alloc(bmap
->ctx
,
1274 isl_basic_map_total_dim(bmap
) + bmap
->n_ineq
+ 1,
1275 isl_basic_map_total_dim(bmap
), 0);
1278 tab
->rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
1279 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
1280 return isl_tab_mark_empty(tab
);
1281 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1282 tab
= add_eq(tab
, bmap
->eq
[i
]);
1286 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
1287 tab
= isl_tab_add_ineq(tab
, bmap
->ineq
[i
]);
1288 if (!tab
|| tab
->empty
)
1294 struct isl_tab
*isl_tab_from_basic_set(struct isl_basic_set
*bset
)
1296 return isl_tab_from_basic_map((struct isl_basic_map
*)bset
);
1299 /* Construct a tableau corresponding to the recession cone of "bmap".
1301 struct isl_tab
*isl_tab_from_recession_cone(struct isl_basic_map
*bmap
)
1305 struct isl_tab
*tab
;
1309 tab
= isl_tab_alloc(bmap
->ctx
, bmap
->n_eq
+ bmap
->n_ineq
,
1310 isl_basic_map_total_dim(bmap
), 0);
1313 tab
->rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
1316 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1317 isl_int_swap(bmap
->eq
[i
][0], cst
);
1318 tab
= add_eq(tab
, bmap
->eq
[i
]);
1319 isl_int_swap(bmap
->eq
[i
][0], cst
);
1323 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
1325 isl_int_swap(bmap
->ineq
[i
][0], cst
);
1326 r
= isl_tab_add_row(tab
, bmap
->ineq
[i
]);
1327 isl_int_swap(bmap
->ineq
[i
][0], cst
);
1330 tab
->con
[r
].is_nonneg
= 1;
1331 isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]);
1342 /* Assuming "tab" is the tableau of a cone, check if the cone is
1343 * bounded, i.e., if it is empty or only contains the origin.
1345 int isl_tab_cone_is_bounded(struct isl_tab
*tab
)
1353 if (tab
->n_dead
== tab
->n_col
)
1357 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
1358 struct isl_tab_var
*var
;
1359 var
= isl_tab_var_from_row(tab
, i
);
1360 if (!var
->is_nonneg
)
1362 if (sign_of_max(tab
, var
) != 0)
1364 close_row(tab
, var
);
1367 if (tab
->n_dead
== tab
->n_col
)
1369 if (i
== tab
->n_row
)
1374 int isl_tab_sample_is_integer(struct isl_tab
*tab
)
1381 for (i
= 0; i
< tab
->n_var
; ++i
) {
1383 if (!tab
->var
[i
].is_row
)
1385 row
= tab
->var
[i
].index
;
1386 if (!isl_int_is_divisible_by(tab
->mat
->row
[row
][1],
1387 tab
->mat
->row
[row
][0]))
1393 static struct isl_vec
*extract_integer_sample(struct isl_tab
*tab
)
1396 struct isl_vec
*vec
;
1398 vec
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_var
);
1402 isl_int_set_si(vec
->block
.data
[0], 1);
1403 for (i
= 0; i
< tab
->n_var
; ++i
) {
1404 if (!tab
->var
[i
].is_row
)
1405 isl_int_set_si(vec
->block
.data
[1 + i
], 0);
1407 int row
= tab
->var
[i
].index
;
1408 isl_int_divexact(vec
->block
.data
[1 + i
],
1409 tab
->mat
->row
[row
][1], tab
->mat
->row
[row
][0]);
1416 struct isl_vec
*isl_tab_get_sample_value(struct isl_tab
*tab
)
1419 struct isl_vec
*vec
;
1425 vec
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_var
);
1431 isl_int_set_si(vec
->block
.data
[0], 1);
1432 for (i
= 0; i
< tab
->n_var
; ++i
) {
1434 if (!tab
->var
[i
].is_row
) {
1435 isl_int_set_si(vec
->block
.data
[1 + i
], 0);
1438 row
= tab
->var
[i
].index
;
1439 isl_int_gcd(m
, vec
->block
.data
[0], tab
->mat
->row
[row
][0]);
1440 isl_int_divexact(m
, tab
->mat
->row
[row
][0], m
);
1441 isl_seq_scale(vec
->block
.data
, vec
->block
.data
, m
, 1 + i
);
1442 isl_int_divexact(m
, vec
->block
.data
[0], tab
->mat
->row
[row
][0]);
1443 isl_int_mul(vec
->block
.data
[1 + i
], m
, tab
->mat
->row
[row
][1]);
1445 isl_seq_normalize(vec
->block
.data
, vec
->size
);
1451 /* Update "bmap" based on the results of the tableau "tab".
1452 * In particular, implicit equalities are made explicit, redundant constraints
1453 * are removed and if the sample value happens to be integer, it is stored
1454 * in "bmap" (unless "bmap" already had an integer sample).
1456 * The tableau is assumed to have been created from "bmap" using
1457 * isl_tab_from_basic_map.
1459 struct isl_basic_map
*isl_basic_map_update_from_tab(struct isl_basic_map
*bmap
,
1460 struct isl_tab
*tab
)
1472 bmap
= isl_basic_map_set_to_empty(bmap
);
1474 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
1475 if (isl_tab_is_equality(tab
, n_eq
+ i
))
1476 isl_basic_map_inequality_to_equality(bmap
, i
);
1477 else if (isl_tab_is_redundant(tab
, n_eq
+ i
))
1478 isl_basic_map_drop_inequality(bmap
, i
);
1480 if (!tab
->rational
&&
1481 !bmap
->sample
&& isl_tab_sample_is_integer(tab
))
1482 bmap
->sample
= extract_integer_sample(tab
);
1486 struct isl_basic_set
*isl_basic_set_update_from_tab(struct isl_basic_set
*bset
,
1487 struct isl_tab
*tab
)
1489 return (struct isl_basic_set
*)isl_basic_map_update_from_tab(
1490 (struct isl_basic_map
*)bset
, tab
);
1493 /* Given a non-negative variable "var", add a new non-negative variable
1494 * that is the opposite of "var", ensuring that var can only attain the
1496 * If var = n/d is a row variable, then the new variable = -n/d.
1497 * If var is a column variables, then the new variable = -var.
1498 * If the new variable cannot attain non-negative values, then
1499 * the resulting tableau is empty.
1500 * Otherwise, we know the value will be zero and we close the row.
1502 static struct isl_tab
*cut_to_hyperplane(struct isl_tab
*tab
,
1503 struct isl_tab_var
*var
)
1508 unsigned off
= 2 + tab
->M
;
1510 if (isl_tab_extend_cons(tab
, 1) < 0)
1514 tab
->con
[r
].index
= tab
->n_row
;
1515 tab
->con
[r
].is_row
= 1;
1516 tab
->con
[r
].is_nonneg
= 0;
1517 tab
->con
[r
].is_zero
= 0;
1518 tab
->con
[r
].is_redundant
= 0;
1519 tab
->con
[r
].frozen
= 0;
1520 tab
->row_var
[tab
->n_row
] = ~r
;
1521 row
= tab
->mat
->row
[tab
->n_row
];
1524 isl_int_set(row
[0], tab
->mat
->row
[var
->index
][0]);
1525 isl_seq_neg(row
+ 1,
1526 tab
->mat
->row
[var
->index
] + 1, 1 + tab
->n_col
);
1528 isl_int_set_si(row
[0], 1);
1529 isl_seq_clr(row
+ 1, 1 + tab
->n_col
);
1530 isl_int_set_si(row
[off
+ var
->index
], -1);
1535 isl_tab_push_var(tab
, isl_tab_undo_allocate
, &tab
->con
[r
]);
1537 sgn
= sign_of_max(tab
, &tab
->con
[r
]);
1539 return isl_tab_mark_empty(tab
);
1540 tab
->con
[r
].is_nonneg
= 1;
1541 isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]);
1543 close_row(tab
, &tab
->con
[r
]);
1551 /* Given a tableau "tab" and an inequality constraint "con" of the tableau,
1552 * relax the inequality by one. That is, the inequality r >= 0 is replaced
1553 * by r' = r + 1 >= 0.
1554 * If r is a row variable, we simply increase the constant term by one
1555 * (taking into account the denominator).
1556 * If r is a column variable, then we need to modify each row that
1557 * refers to r = r' - 1 by substituting this equality, effectively
1558 * subtracting the coefficient of the column from the constant.
1560 struct isl_tab
*isl_tab_relax(struct isl_tab
*tab
, int con
)
1562 struct isl_tab_var
*var
;
1563 unsigned off
= 2 + tab
->M
;
1568 var
= &tab
->con
[con
];
1570 if (!var
->is_row
&& !max_is_manifestly_unbounded(tab
, var
))
1571 to_row(tab
, var
, 1);
1574 isl_int_add(tab
->mat
->row
[var
->index
][1],
1575 tab
->mat
->row
[var
->index
][1], tab
->mat
->row
[var
->index
][0]);
1579 for (i
= 0; i
< tab
->n_row
; ++i
) {
1580 if (isl_int_is_zero(tab
->mat
->row
[i
][off
+ var
->index
]))
1582 isl_int_sub(tab
->mat
->row
[i
][1], tab
->mat
->row
[i
][1],
1583 tab
->mat
->row
[i
][off
+ var
->index
]);
1588 isl_tab_push_var(tab
, isl_tab_undo_relax
, var
);
1593 struct isl_tab
*isl_tab_select_facet(struct isl_tab
*tab
, int con
)
1598 return cut_to_hyperplane(tab
, &tab
->con
[con
]);
1601 static int may_be_equality(struct isl_tab
*tab
, int row
)
1603 unsigned off
= 2 + tab
->M
;
1604 return (tab
->rational
? isl_int_is_zero(tab
->mat
->row
[row
][1])
1605 : isl_int_lt(tab
->mat
->row
[row
][1],
1606 tab
->mat
->row
[row
][0])) &&
1607 isl_seq_first_non_zero(tab
->mat
->row
[row
] + off
+ tab
->n_dead
,
1608 tab
->n_col
- tab
->n_dead
) != -1;
1611 /* Check for (near) equalities among the constraints.
1612 * A constraint is an equality if it is non-negative and if
1613 * its maximal value is either
1614 * - zero (in case of rational tableaus), or
1615 * - strictly less than 1 (in case of integer tableaus)
1617 * We first mark all non-redundant and non-dead variables that
1618 * are not frozen and not obviously not an equality.
1619 * Then we iterate over all marked variables if they can attain
1620 * any values larger than zero or at least one.
1621 * If the maximal value is zero, we mark any column variables
1622 * that appear in the row as being zero and mark the row as being redundant.
1623 * Otherwise, if the maximal value is strictly less than one (and the
1624 * tableau is integer), then we restrict the value to being zero
1625 * by adding an opposite non-negative variable.
1627 struct isl_tab
*isl_tab_detect_equalities(struct isl_tab
*tab
)
1636 if (tab
->n_dead
== tab
->n_col
)
1640 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
1641 struct isl_tab_var
*var
= isl_tab_var_from_row(tab
, i
);
1642 var
->marked
= !var
->frozen
&& var
->is_nonneg
&&
1643 may_be_equality(tab
, i
);
1647 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
1648 struct isl_tab_var
*var
= var_from_col(tab
, i
);
1649 var
->marked
= !var
->frozen
&& var
->is_nonneg
;
1654 struct isl_tab_var
*var
;
1655 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
1656 var
= isl_tab_var_from_row(tab
, i
);
1660 if (i
== tab
->n_row
) {
1661 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
1662 var
= var_from_col(tab
, i
);
1666 if (i
== tab
->n_col
)
1671 if (sign_of_max(tab
, var
) == 0)
1672 close_row(tab
, var
);
1673 else if (!tab
->rational
&& !at_least_one(tab
, var
)) {
1674 tab
= cut_to_hyperplane(tab
, var
);
1675 return isl_tab_detect_equalities(tab
);
1677 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
1678 var
= isl_tab_var_from_row(tab
, i
);
1681 if (may_be_equality(tab
, i
))
1691 /* Check for (near) redundant constraints.
1692 * A constraint is redundant if it is non-negative and if
1693 * its minimal value (temporarily ignoring the non-negativity) is either
1694 * - zero (in case of rational tableaus), or
1695 * - strictly larger than -1 (in case of integer tableaus)
1697 * We first mark all non-redundant and non-dead variables that
1698 * are not frozen and not obviously negatively unbounded.
1699 * Then we iterate over all marked variables if they can attain
1700 * any values smaller than zero or at most negative one.
1701 * If not, we mark the row as being redundant (assuming it hasn't
1702 * been detected as being obviously redundant in the mean time).
1704 struct isl_tab
*isl_tab_detect_redundant(struct isl_tab
*tab
)
1713 if (tab
->n_redundant
== tab
->n_row
)
1717 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
1718 struct isl_tab_var
*var
= isl_tab_var_from_row(tab
, i
);
1719 var
->marked
= !var
->frozen
&& var
->is_nonneg
;
1723 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
1724 struct isl_tab_var
*var
= var_from_col(tab
, i
);
1725 var
->marked
= !var
->frozen
&& var
->is_nonneg
&&
1726 !min_is_manifestly_unbounded(tab
, var
);
1731 struct isl_tab_var
*var
;
1732 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
1733 var
= isl_tab_var_from_row(tab
, i
);
1737 if (i
== tab
->n_row
) {
1738 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
1739 var
= var_from_col(tab
, i
);
1743 if (i
== tab
->n_col
)
1748 if ((tab
->rational
? (sign_of_min(tab
, var
) >= 0)
1749 : !isl_tab_min_at_most_neg_one(tab
, var
)) &&
1751 isl_tab_mark_redundant(tab
, var
->index
);
1752 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
1753 var
= var_from_col(tab
, i
);
1756 if (!min_is_manifestly_unbounded(tab
, var
))
1766 int isl_tab_is_equality(struct isl_tab
*tab
, int con
)
1773 if (tab
->con
[con
].is_zero
)
1775 if (tab
->con
[con
].is_redundant
)
1777 if (!tab
->con
[con
].is_row
)
1778 return tab
->con
[con
].index
< tab
->n_dead
;
1780 row
= tab
->con
[con
].index
;
1783 return isl_int_is_zero(tab
->mat
->row
[row
][1]) &&
1784 isl_seq_first_non_zero(tab
->mat
->row
[row
] + 2 + tab
->n_dead
,
1785 tab
->n_col
- tab
->n_dead
) == -1;
1788 /* Return the minimial value of the affine expression "f" with denominator
1789 * "denom" in *opt, *opt_denom, assuming the tableau is not empty and
1790 * the expression cannot attain arbitrarily small values.
1791 * If opt_denom is NULL, then *opt is rounded up to the nearest integer.
1792 * The return value reflects the nature of the result (empty, unbounded,
1793 * minmimal value returned in *opt).
1795 enum isl_lp_result
isl_tab_min(struct isl_tab
*tab
,
1796 isl_int
*f
, isl_int denom
, isl_int
*opt
, isl_int
*opt_denom
,
1800 enum isl_lp_result res
= isl_lp_ok
;
1801 struct isl_tab_var
*var
;
1802 struct isl_tab_undo
*snap
;
1805 return isl_lp_empty
;
1807 snap
= isl_tab_snap(tab
);
1808 r
= isl_tab_add_row(tab
, f
);
1810 return isl_lp_error
;
1812 isl_int_mul(tab
->mat
->row
[var
->index
][0],
1813 tab
->mat
->row
[var
->index
][0], denom
);
1816 find_pivot(tab
, var
, var
, -1, &row
, &col
);
1817 if (row
== var
->index
) {
1818 res
= isl_lp_unbounded
;
1823 isl_tab_pivot(tab
, row
, col
);
1825 if (isl_tab_rollback(tab
, snap
) < 0)
1826 return isl_lp_error
;
1827 if (ISL_FL_ISSET(flags
, ISL_TAB_SAVE_DUAL
)) {
1830 isl_vec_free(tab
->dual
);
1831 tab
->dual
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_con
);
1833 return isl_lp_error
;
1834 isl_int_set(tab
->dual
->el
[0], tab
->mat
->row
[var
->index
][0]);
1835 for (i
= 0; i
< tab
->n_con
; ++i
) {
1836 if (tab
->con
[i
].is_row
)
1837 isl_int_set_si(tab
->dual
->el
[1 + i
], 0);
1839 int pos
= 2 + tab
->con
[i
].index
;
1840 isl_int_set(tab
->dual
->el
[1 + i
],
1841 tab
->mat
->row
[var
->index
][pos
]);
1845 if (res
== isl_lp_ok
) {
1847 isl_int_set(*opt
, tab
->mat
->row
[var
->index
][1]);
1848 isl_int_set(*opt_denom
, tab
->mat
->row
[var
->index
][0]);
1850 isl_int_cdiv_q(*opt
, tab
->mat
->row
[var
->index
][1],
1851 tab
->mat
->row
[var
->index
][0]);
1856 int isl_tab_is_redundant(struct isl_tab
*tab
, int con
)
1863 if (tab
->con
[con
].is_zero
)
1865 if (tab
->con
[con
].is_redundant
)
1867 return tab
->con
[con
].is_row
&& tab
->con
[con
].index
< tab
->n_redundant
;
1870 /* Take a snapshot of the tableau that can be restored by s call to
1873 struct isl_tab_undo
*isl_tab_snap(struct isl_tab
*tab
)
1881 /* Undo the operation performed by isl_tab_relax.
1883 static void unrelax(struct isl_tab
*tab
, struct isl_tab_var
*var
)
1885 unsigned off
= 2 + tab
->M
;
1887 if (!var
->is_row
&& !max_is_manifestly_unbounded(tab
, var
))
1888 to_row(tab
, var
, 1);
1891 isl_int_sub(tab
->mat
->row
[var
->index
][1],
1892 tab
->mat
->row
[var
->index
][1], tab
->mat
->row
[var
->index
][0]);
1896 for (i
= 0; i
< tab
->n_row
; ++i
) {
1897 if (isl_int_is_zero(tab
->mat
->row
[i
][off
+ var
->index
]))
1899 isl_int_add(tab
->mat
->row
[i
][1], tab
->mat
->row
[i
][1],
1900 tab
->mat
->row
[i
][off
+ var
->index
]);
1906 static void perform_undo_var(struct isl_tab
*tab
, struct isl_tab_undo
*undo
)
1908 struct isl_tab_var
*var
= var_from_index(tab
, undo
->u
.var_index
);
1909 switch(undo
->type
) {
1910 case isl_tab_undo_nonneg
:
1913 case isl_tab_undo_redundant
:
1914 var
->is_redundant
= 0;
1917 case isl_tab_undo_zero
:
1921 case isl_tab_undo_allocate
:
1922 if (undo
->u
.var_index
>= 0) {
1923 isl_assert(tab
->mat
->ctx
, !var
->is_row
, return);
1924 drop_col(tab
, var
->index
);
1928 if (!max_is_manifestly_unbounded(tab
, var
))
1929 to_row(tab
, var
, 1);
1930 else if (!min_is_manifestly_unbounded(tab
, var
))
1931 to_row(tab
, var
, -1);
1933 to_row(tab
, var
, 0);
1935 drop_row(tab
, var
->index
);
1937 case isl_tab_undo_relax
:
1943 /* Restore the tableau to the state where the basic variables
1944 * are those in "col_var".
1945 * We first construct a list of variables that are currently in
1946 * the basis, but shouldn't. Then we iterate over all variables
1947 * that should be in the basis and for each one that is currently
1948 * not in the basis, we exchange it with one of the elements of the
1949 * list constructed before.
1950 * We can always find an appropriate variable to pivot with because
1951 * the current basis is mapped to the old basis by a non-singular
1952 * matrix and so we can never end up with a zero row.
1954 static int restore_basis(struct isl_tab
*tab
, int *col_var
)
1958 int *extra
= NULL
; /* current columns that contain bad stuff */
1959 unsigned off
= 2 + tab
->M
;
1961 extra
= isl_alloc_array(tab
->mat
->ctx
, int, tab
->n_col
);
1964 for (i
= 0; i
< tab
->n_col
; ++i
) {
1965 for (j
= 0; j
< tab
->n_col
; ++j
)
1966 if (tab
->col_var
[i
] == col_var
[j
])
1970 extra
[n_extra
++] = i
;
1972 for (i
= 0; i
< tab
->n_col
&& n_extra
> 0; ++i
) {
1973 struct isl_tab_var
*var
;
1976 for (j
= 0; j
< tab
->n_col
; ++j
)
1977 if (col_var
[i
] == tab
->col_var
[j
])
1981 var
= var_from_index(tab
, col_var
[i
]);
1983 for (j
= 0; j
< n_extra
; ++j
)
1984 if (!isl_int_is_zero(tab
->mat
->row
[row
][off
+extra
[j
]]))
1986 isl_assert(tab
->mat
->ctx
, j
< n_extra
, goto error
);
1987 isl_tab_pivot(tab
, row
, extra
[j
]);
1988 extra
[j
] = extra
[--n_extra
];
2000 static int perform_undo(struct isl_tab
*tab
, struct isl_tab_undo
*undo
)
2002 switch (undo
->type
) {
2003 case isl_tab_undo_empty
:
2006 case isl_tab_undo_nonneg
:
2007 case isl_tab_undo_redundant
:
2008 case isl_tab_undo_zero
:
2009 case isl_tab_undo_allocate
:
2010 case isl_tab_undo_relax
:
2011 perform_undo_var(tab
, undo
);
2013 case isl_tab_undo_saved_basis
:
2014 if (restore_basis(tab
, undo
->u
.col_var
) < 0)
2018 isl_assert(tab
->mat
->ctx
, 0, return -1);
2023 /* Return the tableau to the state it was in when the snapshot "snap"
2026 int isl_tab_rollback(struct isl_tab
*tab
, struct isl_tab_undo
*snap
)
2028 struct isl_tab_undo
*undo
, *next
;
2034 for (undo
= tab
->top
; undo
&& undo
!= &tab
->bottom
; undo
= next
) {
2038 if (perform_undo(tab
, undo
) < 0) {
2052 /* The given row "row" represents an inequality violated by all
2053 * points in the tableau. Check for some special cases of such
2054 * separating constraints.
2055 * In particular, if the row has been reduced to the constant -1,
2056 * then we know the inequality is adjacent (but opposite) to
2057 * an equality in the tableau.
2058 * If the row has been reduced to r = -1 -r', with r' an inequality
2059 * of the tableau, then the inequality is adjacent (but opposite)
2060 * to the inequality r'.
2062 static enum isl_ineq_type
separation_type(struct isl_tab
*tab
, unsigned row
)
2065 unsigned off
= 2 + tab
->M
;
2068 return isl_ineq_separate
;
2070 if (!isl_int_is_one(tab
->mat
->row
[row
][0]))
2071 return isl_ineq_separate
;
2072 if (!isl_int_is_negone(tab
->mat
->row
[row
][1]))
2073 return isl_ineq_separate
;
2075 pos
= isl_seq_first_non_zero(tab
->mat
->row
[row
] + off
+ tab
->n_dead
,
2076 tab
->n_col
- tab
->n_dead
);
2078 return isl_ineq_adj_eq
;
2080 if (!isl_int_is_negone(tab
->mat
->row
[row
][off
+ tab
->n_dead
+ pos
]))
2081 return isl_ineq_separate
;
2083 pos
= isl_seq_first_non_zero(
2084 tab
->mat
->row
[row
] + off
+ tab
->n_dead
+ pos
+ 1,
2085 tab
->n_col
- tab
->n_dead
- pos
- 1);
2087 return pos
== -1 ? isl_ineq_adj_ineq
: isl_ineq_separate
;
2090 /* Check the effect of inequality "ineq" on the tableau "tab".
2092 * isl_ineq_redundant: satisfied by all points in the tableau
2093 * isl_ineq_separate: satisfied by no point in the tableau
2094 * isl_ineq_cut: satisfied by some by not all points
2095 * isl_ineq_adj_eq: adjacent to an equality
2096 * isl_ineq_adj_ineq: adjacent to an inequality.
2098 enum isl_ineq_type
isl_tab_ineq_type(struct isl_tab
*tab
, isl_int
*ineq
)
2100 enum isl_ineq_type type
= isl_ineq_error
;
2101 struct isl_tab_undo
*snap
= NULL
;
2106 return isl_ineq_error
;
2108 if (isl_tab_extend_cons(tab
, 1) < 0)
2109 return isl_ineq_error
;
2111 snap
= isl_tab_snap(tab
);
2113 con
= isl_tab_add_row(tab
, ineq
);
2117 row
= tab
->con
[con
].index
;
2118 if (isl_tab_row_is_redundant(tab
, row
))
2119 type
= isl_ineq_redundant
;
2120 else if (isl_int_is_neg(tab
->mat
->row
[row
][1]) &&
2122 isl_int_abs_ge(tab
->mat
->row
[row
][1],
2123 tab
->mat
->row
[row
][0]))) {
2124 if (at_least_zero(tab
, &tab
->con
[con
]))
2125 type
= isl_ineq_cut
;
2127 type
= separation_type(tab
, row
);
2128 } else if (tab
->rational
? (sign_of_min(tab
, &tab
->con
[con
]) < 0)
2129 : isl_tab_min_at_most_neg_one(tab
, &tab
->con
[con
]))
2130 type
= isl_ineq_cut
;
2132 type
= isl_ineq_redundant
;
2134 if (isl_tab_rollback(tab
, snap
))
2135 return isl_ineq_error
;
2138 isl_tab_rollback(tab
, snap
);
2139 return isl_ineq_error
;
2142 void isl_tab_dump(struct isl_tab
*tab
, FILE *out
, int indent
)
2148 fprintf(out
, "%*snull tab\n", indent
, "");
2151 fprintf(out
, "%*sn_redundant: %d, n_dead: %d", indent
, "",
2152 tab
->n_redundant
, tab
->n_dead
);
2154 fprintf(out
, ", rational");
2156 fprintf(out
, ", empty");
2158 fprintf(out
, "%*s[", indent
, "");
2159 for (i
= 0; i
< tab
->n_var
; ++i
) {
2161 fprintf(out
, (i
== tab
->n_param
||
2162 i
== tab
->n_var
- tab
->n_div
) ? "; "
2164 fprintf(out
, "%c%d%s", tab
->var
[i
].is_row
? 'r' : 'c',
2166 tab
->var
[i
].is_zero
? " [=0]" :
2167 tab
->var
[i
].is_redundant
? " [R]" : "");
2169 fprintf(out
, "]\n");
2170 fprintf(out
, "%*s[", indent
, "");
2171 for (i
= 0; i
< tab
->n_con
; ++i
) {
2174 fprintf(out
, "%c%d%s", tab
->con
[i
].is_row
? 'r' : 'c',
2176 tab
->con
[i
].is_zero
? " [=0]" :
2177 tab
->con
[i
].is_redundant
? " [R]" : "");
2179 fprintf(out
, "]\n");
2180 fprintf(out
, "%*s[", indent
, "");
2181 for (i
= 0; i
< tab
->n_row
; ++i
) {
2184 fprintf(out
, "r%d: %d%s", i
, tab
->row_var
[i
],
2185 isl_tab_var_from_row(tab
, i
)->is_nonneg
? " [>=0]" : "");
2187 fprintf(out
, "]\n");
2188 fprintf(out
, "%*s[", indent
, "");
2189 for (i
= 0; i
< tab
->n_col
; ++i
) {
2192 fprintf(out
, "c%d: %d%s", i
, tab
->col_var
[i
],
2193 var_from_col(tab
, i
)->is_nonneg
? " [>=0]" : "");
2195 fprintf(out
, "]\n");
2196 r
= tab
->mat
->n_row
;
2197 tab
->mat
->n_row
= tab
->n_row
;
2198 c
= tab
->mat
->n_col
;
2199 tab
->mat
->n_col
= 2 + tab
->M
+ tab
->n_col
;
2200 isl_mat_dump(tab
->mat
, out
, indent
);
2201 tab
->mat
->n_row
= r
;
2202 tab
->mat
->n_col
= c
;