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
;
96 enum isl_tab_row_sign
*s
;
97 s
= isl_realloc_array(tab
->mat
->ctx
, tab
->row_sign
,
98 enum isl_tab_row_sign
, tab
->mat
->n_row
);
107 /* Make room for at least n_new extra variables.
108 * Return -1 if anything went wrong.
110 int isl_tab_extend_vars(struct isl_tab
*tab
, unsigned n_new
)
112 struct isl_tab_var
*var
;
113 unsigned off
= 2 + tab
->M
;
115 if (tab
->max_var
< tab
->n_var
+ n_new
) {
116 var
= isl_realloc_array(tab
->mat
->ctx
, tab
->var
,
117 struct isl_tab_var
, tab
->n_var
+ n_new
);
121 tab
->max_var
+= n_new
;
124 if (tab
->mat
->n_col
< off
+ tab
->n_col
+ n_new
) {
127 tab
->mat
= isl_mat_extend(tab
->mat
,
128 tab
->mat
->n_row
, off
+ tab
->n_col
+ n_new
);
131 p
= isl_realloc_array(tab
->mat
->ctx
, tab
->col_var
,
132 int, tab
->mat
->n_col
);
141 struct isl_tab
*isl_tab_extend(struct isl_tab
*tab
, unsigned n_new
)
143 if (isl_tab_extend_cons(tab
, n_new
) >= 0)
150 static void free_undo(struct isl_tab
*tab
)
152 struct isl_tab_undo
*undo
, *next
;
154 for (undo
= tab
->top
; undo
&& undo
!= &tab
->bottom
; undo
= next
) {
161 void isl_tab_free(struct isl_tab
*tab
)
166 isl_mat_free(tab
->mat
);
167 isl_vec_free(tab
->dual
);
168 isl_basic_set_free(tab
->bset
);
177 struct isl_tab
*isl_tab_dup(struct isl_tab
*tab
)
185 dup
= isl_calloc_type(tab
->ctx
, struct isl_tab
);
188 dup
->mat
= isl_mat_dup(tab
->mat
);
191 dup
->var
= isl_alloc_array(tab
->ctx
, struct isl_tab_var
, tab
->max_var
);
194 for (i
= 0; i
< tab
->n_var
; ++i
)
195 dup
->var
[i
] = tab
->var
[i
];
196 dup
->con
= isl_alloc_array(tab
->ctx
, struct isl_tab_var
, tab
->max_con
);
199 for (i
= 0; i
< tab
->n_con
; ++i
)
200 dup
->con
[i
] = tab
->con
[i
];
201 dup
->col_var
= isl_alloc_array(tab
->ctx
, int, tab
->mat
->n_col
);
204 for (i
= 0; i
< tab
->n_var
; ++i
)
205 dup
->col_var
[i
] = tab
->col_var
[i
];
206 dup
->row_var
= isl_alloc_array(tab
->ctx
, int, tab
->mat
->n_row
);
209 for (i
= 0; i
< tab
->n_row
; ++i
)
210 dup
->row_var
[i
] = tab
->row_var
[i
];
212 dup
->row_sign
= isl_alloc_array(tab
->ctx
, enum isl_tab_row_sign
,
216 for (i
= 0; i
< tab
->n_row
; ++i
)
217 dup
->row_sign
[i
] = tab
->row_sign
[i
];
219 dup
->n_row
= tab
->n_row
;
220 dup
->n_con
= tab
->n_con
;
221 dup
->n_eq
= tab
->n_eq
;
222 dup
->max_con
= tab
->max_con
;
223 dup
->n_col
= tab
->n_col
;
224 dup
->n_var
= tab
->n_var
;
225 dup
->max_var
= tab
->max_var
;
226 dup
->n_param
= tab
->n_param
;
227 dup
->n_div
= tab
->n_div
;
228 dup
->n_dead
= tab
->n_dead
;
229 dup
->n_redundant
= tab
->n_redundant
;
230 dup
->rational
= tab
->rational
;
231 dup
->empty
= tab
->empty
;
235 dup
->bottom
.type
= isl_tab_undo_bottom
;
236 dup
->bottom
.next
= NULL
;
237 dup
->top
= &dup
->bottom
;
244 static struct isl_tab_var
*var_from_index(struct isl_tab
*tab
, int i
)
249 return &tab
->con
[~i
];
252 struct isl_tab_var
*isl_tab_var_from_row(struct isl_tab
*tab
, int i
)
254 return var_from_index(tab
, tab
->row_var
[i
]);
257 static struct isl_tab_var
*var_from_col(struct isl_tab
*tab
, int i
)
259 return var_from_index(tab
, tab
->col_var
[i
]);
262 /* Check if there are any upper bounds on column variable "var",
263 * i.e., non-negative rows where var appears with a negative coefficient.
264 * Return 1 if there are no such bounds.
266 static int max_is_manifestly_unbounded(struct isl_tab
*tab
,
267 struct isl_tab_var
*var
)
270 unsigned off
= 2 + tab
->M
;
274 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
275 if (!isl_int_is_neg(tab
->mat
->row
[i
][off
+ var
->index
]))
277 if (isl_tab_var_from_row(tab
, i
)->is_nonneg
)
283 /* Check if there are any lower bounds on column variable "var",
284 * i.e., non-negative rows where var appears with a positive coefficient.
285 * Return 1 if there are no such bounds.
287 static int min_is_manifestly_unbounded(struct isl_tab
*tab
,
288 struct isl_tab_var
*var
)
291 unsigned off
= 2 + tab
->M
;
295 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
296 if (!isl_int_is_pos(tab
->mat
->row
[i
][off
+ var
->index
]))
298 if (isl_tab_var_from_row(tab
, i
)->is_nonneg
)
304 static int row_cmp(struct isl_tab
*tab
, int r1
, int r2
, int c
, isl_int t
)
306 unsigned off
= 2 + tab
->M
;
310 isl_int_mul(t
, tab
->mat
->row
[r1
][2], tab
->mat
->row
[r2
][off
+c
]);
311 isl_int_submul(t
, tab
->mat
->row
[r2
][2], tab
->mat
->row
[r1
][off
+c
]);
316 isl_int_mul(t
, tab
->mat
->row
[r1
][1], tab
->mat
->row
[r2
][off
+ c
]);
317 isl_int_submul(t
, tab
->mat
->row
[r2
][1], tab
->mat
->row
[r1
][off
+ c
]);
318 return isl_int_sgn(t
);
321 /* Given the index of a column "c", return the index of a row
322 * that can be used to pivot the column in, with either an increase
323 * (sgn > 0) or a decrease (sgn < 0) of the corresponding variable.
324 * If "var" is not NULL, then the row returned will be different from
325 * the one associated with "var".
327 * Each row in the tableau is of the form
329 * x_r = a_r0 + \sum_i a_ri x_i
331 * Only rows with x_r >= 0 and with the sign of a_ri opposite to "sgn"
332 * impose any limit on the increase or decrease in the value of x_c
333 * and this bound is equal to a_r0 / |a_rc|. We are therefore looking
334 * for the row with the smallest (most stringent) such bound.
335 * Note that the common denominator of each row drops out of the fraction.
336 * To check if row j has a smaller bound than row r, i.e.,
337 * a_j0 / |a_jc| < a_r0 / |a_rc| or a_j0 |a_rc| < a_r0 |a_jc|,
338 * we check if -sign(a_jc) (a_j0 a_rc - a_r0 a_jc) < 0,
339 * where -sign(a_jc) is equal to "sgn".
341 static int pivot_row(struct isl_tab
*tab
,
342 struct isl_tab_var
*var
, int sgn
, int c
)
346 unsigned off
= 2 + tab
->M
;
350 for (j
= tab
->n_redundant
; j
< tab
->n_row
; ++j
) {
351 if (var
&& j
== var
->index
)
353 if (!isl_tab_var_from_row(tab
, j
)->is_nonneg
)
355 if (sgn
* isl_int_sgn(tab
->mat
->row
[j
][off
+ c
]) >= 0)
361 tsgn
= sgn
* row_cmp(tab
, r
, j
, c
, t
);
362 if (tsgn
< 0 || (tsgn
== 0 &&
363 tab
->row_var
[j
] < tab
->row_var
[r
]))
370 /* Find a pivot (row and col) that will increase (sgn > 0) or decrease
371 * (sgn < 0) the value of row variable var.
372 * If not NULL, then skip_var is a row variable that should be ignored
373 * while looking for a pivot row. It is usually equal to var.
375 * As the given row in the tableau is of the form
377 * x_r = a_r0 + \sum_i a_ri x_i
379 * we need to find a column such that the sign of a_ri is equal to "sgn"
380 * (such that an increase in x_i will have the desired effect) or a
381 * column with a variable that may attain negative values.
382 * If a_ri is positive, then we need to move x_i in the same direction
383 * to obtain the desired effect. Otherwise, x_i has to move in the
384 * opposite direction.
386 static void find_pivot(struct isl_tab
*tab
,
387 struct isl_tab_var
*var
, struct isl_tab_var
*skip_var
,
388 int sgn
, int *row
, int *col
)
395 isl_assert(tab
->mat
->ctx
, var
->is_row
, return);
396 tr
= tab
->mat
->row
[var
->index
] + 2 + tab
->M
;
399 for (j
= tab
->n_dead
; j
< tab
->n_col
; ++j
) {
400 if (isl_int_is_zero(tr
[j
]))
402 if (isl_int_sgn(tr
[j
]) != sgn
&&
403 var_from_col(tab
, j
)->is_nonneg
)
405 if (c
< 0 || tab
->col_var
[j
] < tab
->col_var
[c
])
411 sgn
*= isl_int_sgn(tr
[c
]);
412 r
= pivot_row(tab
, skip_var
, sgn
, c
);
413 *row
= r
< 0 ? var
->index
: r
;
417 /* Return 1 if row "row" represents an obviously redundant inequality.
419 * - it represents an inequality or a variable
420 * - that is the sum of a non-negative sample value and a positive
421 * combination of zero or more non-negative variables.
423 int isl_tab_row_is_redundant(struct isl_tab
*tab
, int row
)
426 unsigned off
= 2 + tab
->M
;
428 if (tab
->row_var
[row
] < 0 && !isl_tab_var_from_row(tab
, row
)->is_nonneg
)
431 if (isl_int_is_neg(tab
->mat
->row
[row
][1]))
433 if (tab
->M
&& isl_int_is_neg(tab
->mat
->row
[row
][2]))
436 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
437 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ i
]))
439 if (isl_int_is_neg(tab
->mat
->row
[row
][off
+ i
]))
441 if (!var_from_col(tab
, i
)->is_nonneg
)
447 static void swap_rows(struct isl_tab
*tab
, int row1
, int row2
)
450 t
= tab
->row_var
[row1
];
451 tab
->row_var
[row1
] = tab
->row_var
[row2
];
452 tab
->row_var
[row2
] = t
;
453 isl_tab_var_from_row(tab
, row1
)->index
= row1
;
454 isl_tab_var_from_row(tab
, row2
)->index
= row2
;
455 tab
->mat
= isl_mat_swap_rows(tab
->mat
, row1
, row2
);
459 t
= tab
->row_sign
[row1
];
460 tab
->row_sign
[row1
] = tab
->row_sign
[row2
];
461 tab
->row_sign
[row2
] = t
;
464 static void push_union(struct isl_tab
*tab
,
465 enum isl_tab_undo_type type
, union isl_tab_undo_val u
)
467 struct isl_tab_undo
*undo
;
472 undo
= isl_alloc_type(tab
->mat
->ctx
, struct isl_tab_undo
);
480 undo
->next
= tab
->top
;
484 void isl_tab_push_var(struct isl_tab
*tab
,
485 enum isl_tab_undo_type type
, struct isl_tab_var
*var
)
487 union isl_tab_undo_val u
;
489 u
.var_index
= tab
->row_var
[var
->index
];
491 u
.var_index
= tab
->col_var
[var
->index
];
492 push_union(tab
, type
, u
);
495 void isl_tab_push(struct isl_tab
*tab
, enum isl_tab_undo_type type
)
497 union isl_tab_undo_val u
= { 0 };
498 push_union(tab
, type
, u
);
501 /* Push a record on the undo stack describing the current basic
502 * variables, so that the this state can be restored during rollback.
504 void isl_tab_push_basis(struct isl_tab
*tab
)
507 union isl_tab_undo_val u
;
509 u
.col_var
= isl_alloc_array(tab
->mat
->ctx
, int, tab
->n_col
);
515 for (i
= 0; i
< tab
->n_col
; ++i
)
516 u
.col_var
[i
] = tab
->col_var
[i
];
517 push_union(tab
, isl_tab_undo_saved_basis
, u
);
520 /* Mark row with index "row" as being redundant.
521 * If we may need to undo the operation or if the row represents
522 * a variable of the original problem, the row is kept,
523 * but no longer considered when looking for a pivot row.
524 * Otherwise, the row is simply removed.
526 * The row may be interchanged with some other row. If it
527 * is interchanged with a later row, return 1. Otherwise return 0.
528 * If the rows are checked in order in the calling function,
529 * then a return value of 1 means that the row with the given
530 * row number may now contain a different row that hasn't been checked yet.
532 int isl_tab_mark_redundant(struct isl_tab
*tab
, int row
)
534 struct isl_tab_var
*var
= isl_tab_var_from_row(tab
, row
);
535 var
->is_redundant
= 1;
536 isl_assert(tab
->mat
->ctx
, row
>= tab
->n_redundant
, return);
537 if (tab
->need_undo
|| tab
->row_var
[row
] >= 0) {
538 if (tab
->row_var
[row
] >= 0 && !var
->is_nonneg
) {
540 isl_tab_push_var(tab
, isl_tab_undo_nonneg
, var
);
542 if (row
!= tab
->n_redundant
)
543 swap_rows(tab
, row
, tab
->n_redundant
);
544 isl_tab_push_var(tab
, isl_tab_undo_redundant
, var
);
548 if (row
!= tab
->n_row
- 1)
549 swap_rows(tab
, row
, tab
->n_row
- 1);
550 isl_tab_var_from_row(tab
, tab
->n_row
- 1)->index
= -1;
556 struct isl_tab
*isl_tab_mark_empty(struct isl_tab
*tab
)
558 if (!tab
->empty
&& tab
->need_undo
)
559 isl_tab_push(tab
, isl_tab_undo_empty
);
564 /* Update the rows signs after a pivot of "row" and "col", with "row_sgn"
565 * the original sign of the pivot element.
566 * We only keep track of row signs during PILP solving and in this case
567 * we only pivot a row with negative sign (meaning the value is always
568 * non-positive) using a positive pivot element.
570 * For each row j, the new value of the parametric constant is equal to
572 * a_j0 - a_jc a_r0/a_rc
574 * where a_j0 is the original parametric constant, a_rc is the pivot element,
575 * a_r0 is the parametric constant of the pivot row and a_jc is the
576 * pivot column entry of the row j.
577 * Since a_r0 is non-positive and a_rc is positive, the sign of row j
578 * remains the same if a_jc has the same sign as the row j or if
579 * a_jc is zero. In all other cases, we reset the sign to "unknown".
581 static void update_row_sign(struct isl_tab
*tab
, int row
, int col
, int row_sgn
)
584 struct isl_mat
*mat
= tab
->mat
;
585 unsigned off
= 2 + tab
->M
;
590 if (tab
->row_sign
[row
] == 0)
592 isl_assert(mat
->ctx
, row_sgn
> 0, return);
593 isl_assert(mat
->ctx
, tab
->row_sign
[row
] == isl_tab_row_neg
, return);
594 tab
->row_sign
[row
] = isl_tab_row_pos
;
595 for (i
= 0; i
< tab
->n_row
; ++i
) {
599 s
= isl_int_sgn(mat
->row
[i
][off
+ col
]);
602 if (!tab
->row_sign
[i
])
604 if (s
< 0 && tab
->row_sign
[i
] == isl_tab_row_neg
)
606 if (s
> 0 && tab
->row_sign
[i
] == isl_tab_row_pos
)
608 tab
->row_sign
[i
] = isl_tab_row_unknown
;
612 /* Given a row number "row" and a column number "col", pivot the tableau
613 * such that the associated variables are interchanged.
614 * The given row in the tableau expresses
616 * x_r = a_r0 + \sum_i a_ri x_i
620 * x_c = 1/a_rc x_r - a_r0/a_rc + sum_{i \ne r} -a_ri/a_rc
622 * Substituting this equality into the other rows
624 * x_j = a_j0 + \sum_i a_ji x_i
626 * with a_jc \ne 0, we obtain
628 * 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
635 * where i is any other column and j is any other row,
636 * is therefore transformed into
638 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
639 * 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)
641 * The transformation is performed along the following steps
646 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
649 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
650 * n_jc/(|n_rc| d_j) n_ji/(|n_rc| d_j)
652 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
653 * n_jc/(|n_rc| d_j) (n_ji |n_rc|)/(|n_rc| d_j)
655 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
656 * n_jc/(|n_rc| d_j) (n_ji |n_rc| - s(n_rc)n_jc n_ri)/(|n_rc| d_j)
658 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
659 * 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)
662 void isl_tab_pivot(struct isl_tab
*tab
, int row
, int col
)
667 struct isl_mat
*mat
= tab
->mat
;
668 struct isl_tab_var
*var
;
669 unsigned off
= 2 + tab
->M
;
671 isl_int_swap(mat
->row
[row
][0], mat
->row
[row
][off
+ col
]);
672 sgn
= isl_int_sgn(mat
->row
[row
][0]);
674 isl_int_neg(mat
->row
[row
][0], mat
->row
[row
][0]);
675 isl_int_neg(mat
->row
[row
][off
+ col
], mat
->row
[row
][off
+ col
]);
677 for (j
= 0; j
< off
- 1 + tab
->n_col
; ++j
) {
678 if (j
== off
- 1 + col
)
680 isl_int_neg(mat
->row
[row
][1 + j
], mat
->row
[row
][1 + j
]);
682 if (!isl_int_is_one(mat
->row
[row
][0]))
683 isl_seq_normalize(mat
->row
[row
], off
+ tab
->n_col
);
684 for (i
= 0; i
< tab
->n_row
; ++i
) {
687 if (isl_int_is_zero(mat
->row
[i
][off
+ col
]))
689 isl_int_mul(mat
->row
[i
][0], mat
->row
[i
][0], mat
->row
[row
][0]);
690 for (j
= 0; j
< off
- 1 + tab
->n_col
; ++j
) {
691 if (j
== off
- 1 + col
)
693 isl_int_mul(mat
->row
[i
][1 + j
],
694 mat
->row
[i
][1 + j
], mat
->row
[row
][0]);
695 isl_int_addmul(mat
->row
[i
][1 + j
],
696 mat
->row
[i
][off
+ col
], mat
->row
[row
][1 + j
]);
698 isl_int_mul(mat
->row
[i
][off
+ col
],
699 mat
->row
[i
][off
+ col
], mat
->row
[row
][off
+ col
]);
700 if (!isl_int_is_one(mat
->row
[i
][0]))
701 isl_seq_normalize(mat
->row
[i
], off
+ tab
->n_col
);
703 t
= tab
->row_var
[row
];
704 tab
->row_var
[row
] = tab
->col_var
[col
];
705 tab
->col_var
[col
] = t
;
706 var
= isl_tab_var_from_row(tab
, row
);
709 var
= var_from_col(tab
, col
);
712 update_row_sign(tab
, row
, col
, sgn
);
715 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
716 if (isl_int_is_zero(mat
->row
[i
][off
+ col
]))
718 if (!isl_tab_var_from_row(tab
, i
)->frozen
&&
719 isl_tab_row_is_redundant(tab
, i
))
720 if (isl_tab_mark_redundant(tab
, i
))
725 /* If "var" represents a column variable, then pivot is up (sgn > 0)
726 * or down (sgn < 0) to a row. The variable is assumed not to be
727 * unbounded in the specified direction.
728 * If sgn = 0, then the variable is unbounded in both directions,
729 * and we pivot with any row we can find.
731 static void to_row(struct isl_tab
*tab
, struct isl_tab_var
*var
, int sign
)
734 unsigned off
= 2 + tab
->M
;
740 for (r
= tab
->n_redundant
; r
< tab
->n_row
; ++r
)
741 if (!isl_int_is_zero(tab
->mat
->row
[r
][off
+var
->index
]))
743 isl_assert(tab
->mat
->ctx
, r
< tab
->n_row
, return);
745 r
= pivot_row(tab
, NULL
, sign
, var
->index
);
746 isl_assert(tab
->mat
->ctx
, r
>= 0, return);
749 isl_tab_pivot(tab
, r
, var
->index
);
752 static void check_table(struct isl_tab
*tab
)
758 for (i
= 0; i
< tab
->n_row
; ++i
) {
759 if (!isl_tab_var_from_row(tab
, i
)->is_nonneg
)
761 assert(!isl_int_is_neg(tab
->mat
->row
[i
][1]));
765 /* Return the sign of the maximal value of "var".
766 * If the sign is not negative, then on return from this function,
767 * the sample value will also be non-negative.
769 * If "var" is manifestly unbounded wrt positive values, we are done.
770 * Otherwise, we pivot the variable up to a row if needed
771 * Then we continue pivoting down until either
772 * - no more down pivots can be performed
773 * - the sample value is positive
774 * - the variable is pivoted into a manifestly unbounded column
776 static int sign_of_max(struct isl_tab
*tab
, struct isl_tab_var
*var
)
780 if (max_is_manifestly_unbounded(tab
, var
))
783 while (!isl_int_is_pos(tab
->mat
->row
[var
->index
][1])) {
784 find_pivot(tab
, var
, var
, 1, &row
, &col
);
786 return isl_int_sgn(tab
->mat
->row
[var
->index
][1]);
787 isl_tab_pivot(tab
, row
, col
);
788 if (!var
->is_row
) /* manifestly unbounded */
794 static int row_is_neg(struct isl_tab
*tab
, int row
)
797 return isl_int_is_neg(tab
->mat
->row
[row
][1]);
798 if (isl_int_is_pos(tab
->mat
->row
[row
][2]))
800 if (isl_int_is_neg(tab
->mat
->row
[row
][2]))
802 return isl_int_is_neg(tab
->mat
->row
[row
][1]);
805 static int row_sgn(struct isl_tab
*tab
, int row
)
808 return isl_int_sgn(tab
->mat
->row
[row
][1]);
809 if (!isl_int_is_zero(tab
->mat
->row
[row
][2]))
810 return isl_int_sgn(tab
->mat
->row
[row
][2]);
812 return isl_int_sgn(tab
->mat
->row
[row
][1]);
815 /* Perform pivots until the row variable "var" has a non-negative
816 * sample value or until no more upward pivots can be performed.
817 * Return the sign of the sample value after the pivots have been
820 static int restore_row(struct isl_tab
*tab
, struct isl_tab_var
*var
)
824 while (row_is_neg(tab
, var
->index
)) {
825 find_pivot(tab
, var
, var
, 1, &row
, &col
);
828 isl_tab_pivot(tab
, row
, col
);
829 if (!var
->is_row
) /* manifestly unbounded */
832 return row_sgn(tab
, var
->index
);
835 /* Perform pivots until we are sure that the row variable "var"
836 * can attain non-negative values. After return from this
837 * function, "var" is still a row variable, but its sample
838 * value may not be non-negative, even if the function returns 1.
840 static int at_least_zero(struct isl_tab
*tab
, struct isl_tab_var
*var
)
844 while (isl_int_is_neg(tab
->mat
->row
[var
->index
][1])) {
845 find_pivot(tab
, var
, var
, 1, &row
, &col
);
848 if (row
== var
->index
) /* manifestly unbounded */
850 isl_tab_pivot(tab
, row
, col
);
852 return !isl_int_is_neg(tab
->mat
->row
[var
->index
][1]);
855 /* Return a negative value if "var" can attain negative values.
856 * Return a non-negative value otherwise.
858 * If "var" is manifestly unbounded wrt negative values, we are done.
859 * Otherwise, if var is in a column, we can pivot it down to a row.
860 * Then we continue pivoting down until either
861 * - the pivot would result in a manifestly unbounded column
862 * => we don't perform the pivot, but simply return -1
863 * - no more down pivots can be performed
864 * - the sample value is negative
865 * If the sample value becomes negative and the variable is supposed
866 * to be nonnegative, then we undo the last pivot.
867 * However, if the last pivot has made the pivoting variable
868 * obviously redundant, then it may have moved to another row.
869 * In that case we look for upward pivots until we reach a non-negative
872 static int sign_of_min(struct isl_tab
*tab
, struct isl_tab_var
*var
)
875 struct isl_tab_var
*pivot_var
;
877 if (min_is_manifestly_unbounded(tab
, var
))
881 row
= pivot_row(tab
, NULL
, -1, col
);
882 pivot_var
= var_from_col(tab
, col
);
883 isl_tab_pivot(tab
, row
, col
);
884 if (var
->is_redundant
)
886 if (isl_int_is_neg(tab
->mat
->row
[var
->index
][1])) {
887 if (var
->is_nonneg
) {
888 if (!pivot_var
->is_redundant
&&
889 pivot_var
->index
== row
)
890 isl_tab_pivot(tab
, row
, col
);
892 restore_row(tab
, var
);
897 if (var
->is_redundant
)
899 while (!isl_int_is_neg(tab
->mat
->row
[var
->index
][1])) {
900 find_pivot(tab
, var
, var
, -1, &row
, &col
);
901 if (row
== var
->index
)
904 return isl_int_sgn(tab
->mat
->row
[var
->index
][1]);
905 pivot_var
= var_from_col(tab
, col
);
906 isl_tab_pivot(tab
, row
, col
);
907 if (var
->is_redundant
)
910 if (var
->is_nonneg
) {
911 /* pivot back to non-negative value */
912 if (!pivot_var
->is_redundant
&& pivot_var
->index
== row
)
913 isl_tab_pivot(tab
, row
, col
);
915 restore_row(tab
, var
);
920 static int row_at_most_neg_one(struct isl_tab
*tab
, int row
)
923 if (isl_int_is_pos(tab
->mat
->row
[row
][2]))
925 if (isl_int_is_neg(tab
->mat
->row
[row
][2]))
928 return isl_int_is_neg(tab
->mat
->row
[row
][1]) &&
929 isl_int_abs_ge(tab
->mat
->row
[row
][1],
930 tab
->mat
->row
[row
][0]);
933 /* Return 1 if "var" can attain values <= -1.
934 * Return 0 otherwise.
936 * The sample value of "var" is assumed to be non-negative when the
937 * the function is called and will be made non-negative again before
938 * the function returns.
940 int isl_tab_min_at_most_neg_one(struct isl_tab
*tab
, struct isl_tab_var
*var
)
943 struct isl_tab_var
*pivot_var
;
945 if (min_is_manifestly_unbounded(tab
, var
))
949 row
= pivot_row(tab
, NULL
, -1, col
);
950 pivot_var
= var_from_col(tab
, col
);
951 isl_tab_pivot(tab
, row
, col
);
952 if (var
->is_redundant
)
954 if (row_at_most_neg_one(tab
, var
->index
)) {
955 if (var
->is_nonneg
) {
956 if (!pivot_var
->is_redundant
&&
957 pivot_var
->index
== row
)
958 isl_tab_pivot(tab
, row
, col
);
960 restore_row(tab
, var
);
965 if (var
->is_redundant
)
968 find_pivot(tab
, var
, var
, -1, &row
, &col
);
969 if (row
== var
->index
)
973 pivot_var
= var_from_col(tab
, col
);
974 isl_tab_pivot(tab
, row
, col
);
975 if (var
->is_redundant
)
977 } while (!row_at_most_neg_one(tab
, var
->index
));
978 if (var
->is_nonneg
) {
979 /* pivot back to non-negative value */
980 if (!pivot_var
->is_redundant
&& pivot_var
->index
== row
)
981 isl_tab_pivot(tab
, row
, col
);
982 restore_row(tab
, var
);
987 /* Return 1 if "var" can attain values >= 1.
988 * Return 0 otherwise.
990 static int at_least_one(struct isl_tab
*tab
, struct isl_tab_var
*var
)
995 if (max_is_manifestly_unbounded(tab
, var
))
998 r
= tab
->mat
->row
[var
->index
];
999 while (isl_int_lt(r
[1], r
[0])) {
1000 find_pivot(tab
, var
, var
, 1, &row
, &col
);
1002 return isl_int_ge(r
[1], r
[0]);
1003 if (row
== var
->index
) /* manifestly unbounded */
1005 isl_tab_pivot(tab
, row
, col
);
1010 static void swap_cols(struct isl_tab
*tab
, int col1
, int col2
)
1013 unsigned off
= 2 + tab
->M
;
1014 t
= tab
->col_var
[col1
];
1015 tab
->col_var
[col1
] = tab
->col_var
[col2
];
1016 tab
->col_var
[col2
] = t
;
1017 var_from_col(tab
, col1
)->index
= col1
;
1018 var_from_col(tab
, col2
)->index
= col2
;
1019 tab
->mat
= isl_mat_swap_cols(tab
->mat
, off
+ col1
, off
+ col2
);
1022 /* Mark column with index "col" as representing a zero variable.
1023 * If we may need to undo the operation the column is kept,
1024 * but no longer considered.
1025 * Otherwise, the column is simply removed.
1027 * The column may be interchanged with some other column. If it
1028 * is interchanged with a later column, return 1. Otherwise return 0.
1029 * If the columns are checked in order in the calling function,
1030 * then a return value of 1 means that the column with the given
1031 * column number may now contain a different column that
1032 * hasn't been checked yet.
1034 int isl_tab_kill_col(struct isl_tab
*tab
, int col
)
1036 var_from_col(tab
, col
)->is_zero
= 1;
1037 if (tab
->need_undo
) {
1038 isl_tab_push_var(tab
, isl_tab_undo_zero
, var_from_col(tab
, col
));
1039 if (col
!= tab
->n_dead
)
1040 swap_cols(tab
, col
, tab
->n_dead
);
1044 if (col
!= tab
->n_col
- 1)
1045 swap_cols(tab
, col
, tab
->n_col
- 1);
1046 var_from_col(tab
, tab
->n_col
- 1)->index
= -1;
1052 /* Row variable "var" is non-negative and cannot attain any values
1053 * larger than zero. This means that the coefficients of the unrestricted
1054 * column variables are zero and that the coefficients of the non-negative
1055 * column variables are zero or negative.
1056 * Each of the non-negative variables with a negative coefficient can
1057 * then also be written as the negative sum of non-negative variables
1058 * and must therefore also be zero.
1060 static void close_row(struct isl_tab
*tab
, struct isl_tab_var
*var
)
1063 struct isl_mat
*mat
= tab
->mat
;
1064 unsigned off
= 2 + tab
->M
;
1066 isl_assert(tab
->mat
->ctx
, var
->is_nonneg
, return);
1068 for (j
= tab
->n_dead
; j
< tab
->n_col
; ++j
) {
1069 if (isl_int_is_zero(mat
->row
[var
->index
][off
+ j
]))
1071 isl_assert(tab
->mat
->ctx
,
1072 isl_int_is_neg(mat
->row
[var
->index
][off
+ j
]), return);
1073 if (isl_tab_kill_col(tab
, j
))
1076 isl_tab_mark_redundant(tab
, var
->index
);
1079 /* Add a constraint to the tableau and allocate a row for it.
1080 * Return the index into the constraint array "con".
1082 int isl_tab_allocate_con(struct isl_tab
*tab
)
1086 isl_assert(tab
->mat
->ctx
, tab
->n_row
< tab
->mat
->n_row
, return -1);
1089 tab
->con
[r
].index
= tab
->n_row
;
1090 tab
->con
[r
].is_row
= 1;
1091 tab
->con
[r
].is_nonneg
= 0;
1092 tab
->con
[r
].is_zero
= 0;
1093 tab
->con
[r
].is_redundant
= 0;
1094 tab
->con
[r
].frozen
= 0;
1095 tab
->row_var
[tab
->n_row
] = ~r
;
1099 isl_tab_push_var(tab
, isl_tab_undo_allocate
, &tab
->con
[r
]);
1104 /* Add a variable to the tableau and allocate a column for it.
1105 * Return the index into the variable array "var".
1107 int isl_tab_allocate_var(struct isl_tab
*tab
)
1111 unsigned off
= 2 + tab
->M
;
1113 isl_assert(tab
->mat
->ctx
, tab
->n_col
< tab
->mat
->n_col
, return -1);
1114 isl_assert(tab
->mat
->ctx
, tab
->n_var
< tab
->max_var
, return -1);
1117 tab
->var
[r
].index
= tab
->n_col
;
1118 tab
->var
[r
].is_row
= 0;
1119 tab
->var
[r
].is_nonneg
= 0;
1120 tab
->var
[r
].is_zero
= 0;
1121 tab
->var
[r
].is_redundant
= 0;
1122 tab
->var
[r
].frozen
= 0;
1123 tab
->col_var
[tab
->n_col
] = r
;
1125 for (i
= 0; i
< tab
->n_row
; ++i
)
1126 isl_int_set_si(tab
->mat
->row
[i
][off
+ tab
->n_col
], 0);
1130 isl_tab_push_var(tab
, isl_tab_undo_allocate
, &tab
->var
[r
]);
1135 /* Add a row to the tableau. The row is given as an affine combination
1136 * of the original variables and needs to be expressed in terms of the
1139 * We add each term in turn.
1140 * If r = n/d_r is the current sum and we need to add k x, then
1141 * if x is a column variable, we increase the numerator of
1142 * this column by k d_r
1143 * if x = f/d_x is a row variable, then the new representation of r is
1145 * n k f d_x/g n + d_r/g k f m/d_r n + m/d_g k f
1146 * --- + --- = ------------------- = -------------------
1147 * d_r d_r d_r d_x/g m
1149 * with g the gcd of d_r and d_x and m the lcm of d_r and d_x.
1151 int isl_tab_add_row(struct isl_tab
*tab
, isl_int
*line
)
1157 unsigned off
= 2 + tab
->M
;
1159 r
= isl_tab_allocate_con(tab
);
1165 row
= tab
->mat
->row
[tab
->con
[r
].index
];
1166 isl_int_set_si(row
[0], 1);
1167 isl_int_set(row
[1], line
[0]);
1168 isl_seq_clr(row
+ 2, tab
->M
+ tab
->n_col
);
1169 for (i
= 0; i
< tab
->n_var
; ++i
) {
1170 if (tab
->var
[i
].is_zero
)
1172 if (tab
->var
[i
].is_row
) {
1174 row
[0], tab
->mat
->row
[tab
->var
[i
].index
][0]);
1175 isl_int_swap(a
, row
[0]);
1176 isl_int_divexact(a
, row
[0], a
);
1178 row
[0], tab
->mat
->row
[tab
->var
[i
].index
][0]);
1179 isl_int_mul(b
, b
, line
[1 + i
]);
1180 isl_seq_combine(row
+ 1, a
, row
+ 1,
1181 b
, tab
->mat
->row
[tab
->var
[i
].index
] + 1,
1182 1 + tab
->M
+ tab
->n_col
);
1184 isl_int_addmul(row
[off
+ tab
->var
[i
].index
],
1185 line
[1 + i
], row
[0]);
1186 if (tab
->M
&& i
>= tab
->n_param
&& i
< tab
->n_var
- tab
->n_div
)
1187 isl_int_submul(row
[2], line
[1 + i
], row
[0]);
1189 isl_seq_normalize(row
, off
+ tab
->n_col
);
1194 tab
->row_sign
[tab
->con
[r
].index
] = 0;
1199 static int drop_row(struct isl_tab
*tab
, int row
)
1201 isl_assert(tab
->mat
->ctx
, ~tab
->row_var
[row
] == tab
->n_con
- 1, return -1);
1202 if (row
!= tab
->n_row
- 1)
1203 swap_rows(tab
, row
, tab
->n_row
- 1);
1209 static int drop_col(struct isl_tab
*tab
, int col
)
1211 isl_assert(tab
->mat
->ctx
, tab
->col_var
[col
] == tab
->n_var
- 1, return -1);
1212 if (col
!= tab
->n_col
- 1)
1213 swap_cols(tab
, col
, tab
->n_col
- 1);
1219 /* Add inequality "ineq" and check if it conflicts with the
1220 * previously added constraints or if it is obviously redundant.
1222 struct isl_tab
*isl_tab_add_ineq(struct isl_tab
*tab
, isl_int
*ineq
)
1229 r
= isl_tab_add_row(tab
, ineq
);
1232 tab
->con
[r
].is_nonneg
= 1;
1233 isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]);
1234 if (isl_tab_row_is_redundant(tab
, tab
->con
[r
].index
)) {
1235 isl_tab_mark_redundant(tab
, tab
->con
[r
].index
);
1239 sgn
= restore_row(tab
, &tab
->con
[r
]);
1241 return isl_tab_mark_empty(tab
);
1242 if (tab
->con
[r
].is_row
&& isl_tab_row_is_redundant(tab
, tab
->con
[r
].index
))
1243 isl_tab_mark_redundant(tab
, tab
->con
[r
].index
);
1250 /* Pivot a non-negative variable down until it reaches the value zero
1251 * and then pivot the variable into a column position.
1253 int to_col(struct isl_tab
*tab
, struct isl_tab_var
*var
)
1257 unsigned off
= 2 + tab
->M
;
1262 while (isl_int_is_pos(tab
->mat
->row
[var
->index
][1])) {
1263 find_pivot(tab
, var
, NULL
, -1, &row
, &col
);
1264 isl_assert(tab
->mat
->ctx
, row
!= -1, return -1);
1265 isl_tab_pivot(tab
, row
, col
);
1270 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
)
1271 if (!isl_int_is_zero(tab
->mat
->row
[var
->index
][off
+ i
]))
1274 isl_assert(tab
->mat
->ctx
, i
< tab
->n_col
, return -1);
1275 isl_tab_pivot(tab
, var
->index
, i
);
1280 /* We assume Gaussian elimination has been performed on the equalities.
1281 * The equalities can therefore never conflict.
1282 * Adding the equalities is currently only really useful for a later call
1283 * to isl_tab_ineq_type.
1285 static struct isl_tab
*add_eq(struct isl_tab
*tab
, isl_int
*eq
)
1292 r
= isl_tab_add_row(tab
, eq
);
1296 r
= tab
->con
[r
].index
;
1297 i
= isl_seq_first_non_zero(tab
->mat
->row
[r
] + 2 + tab
->M
+ tab
->n_dead
,
1298 tab
->n_col
- tab
->n_dead
);
1299 isl_assert(tab
->mat
->ctx
, i
>= 0, goto error
);
1301 isl_tab_pivot(tab
, r
, i
);
1302 isl_tab_kill_col(tab
, i
);
1311 /* Add an equality that is known to be valid for the given tableau.
1313 struct isl_tab
*isl_tab_add_valid_eq(struct isl_tab
*tab
, isl_int
*eq
)
1315 struct isl_tab_var
*var
;
1321 r
= isl_tab_add_row(tab
, eq
);
1327 if (isl_int_is_neg(tab
->mat
->row
[r
][1]))
1328 isl_seq_neg(tab
->mat
->row
[r
] + 1, tab
->mat
->row
[r
] + 1,
1331 if (to_col(tab
, var
) < 0)
1334 isl_tab_kill_col(tab
, var
->index
);
1342 struct isl_tab
*isl_tab_from_basic_map(struct isl_basic_map
*bmap
)
1345 struct isl_tab
*tab
;
1349 tab
= isl_tab_alloc(bmap
->ctx
,
1350 isl_basic_map_total_dim(bmap
) + bmap
->n_ineq
+ 1,
1351 isl_basic_map_total_dim(bmap
), 0);
1354 tab
->rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
1355 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
1356 return isl_tab_mark_empty(tab
);
1357 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1358 tab
= add_eq(tab
, bmap
->eq
[i
]);
1362 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
1363 tab
= isl_tab_add_ineq(tab
, bmap
->ineq
[i
]);
1364 if (!tab
|| tab
->empty
)
1370 struct isl_tab
*isl_tab_from_basic_set(struct isl_basic_set
*bset
)
1372 return isl_tab_from_basic_map((struct isl_basic_map
*)bset
);
1375 /* Construct a tableau corresponding to the recession cone of "bmap".
1377 struct isl_tab
*isl_tab_from_recession_cone(struct isl_basic_map
*bmap
)
1381 struct isl_tab
*tab
;
1385 tab
= isl_tab_alloc(bmap
->ctx
, bmap
->n_eq
+ bmap
->n_ineq
,
1386 isl_basic_map_total_dim(bmap
), 0);
1389 tab
->rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
1392 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1393 isl_int_swap(bmap
->eq
[i
][0], cst
);
1394 tab
= add_eq(tab
, bmap
->eq
[i
]);
1395 isl_int_swap(bmap
->eq
[i
][0], cst
);
1399 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
1401 isl_int_swap(bmap
->ineq
[i
][0], cst
);
1402 r
= isl_tab_add_row(tab
, bmap
->ineq
[i
]);
1403 isl_int_swap(bmap
->ineq
[i
][0], cst
);
1406 tab
->con
[r
].is_nonneg
= 1;
1407 isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]);
1418 /* Assuming "tab" is the tableau of a cone, check if the cone is
1419 * bounded, i.e., if it is empty or only contains the origin.
1421 int isl_tab_cone_is_bounded(struct isl_tab
*tab
)
1429 if (tab
->n_dead
== tab
->n_col
)
1433 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
1434 struct isl_tab_var
*var
;
1435 var
= isl_tab_var_from_row(tab
, i
);
1436 if (!var
->is_nonneg
)
1438 if (sign_of_max(tab
, var
) != 0)
1440 close_row(tab
, var
);
1443 if (tab
->n_dead
== tab
->n_col
)
1445 if (i
== tab
->n_row
)
1450 int isl_tab_sample_is_integer(struct isl_tab
*tab
)
1457 for (i
= 0; i
< tab
->n_var
; ++i
) {
1459 if (!tab
->var
[i
].is_row
)
1461 row
= tab
->var
[i
].index
;
1462 if (!isl_int_is_divisible_by(tab
->mat
->row
[row
][1],
1463 tab
->mat
->row
[row
][0]))
1469 static struct isl_vec
*extract_integer_sample(struct isl_tab
*tab
)
1472 struct isl_vec
*vec
;
1474 vec
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_var
);
1478 isl_int_set_si(vec
->block
.data
[0], 1);
1479 for (i
= 0; i
< tab
->n_var
; ++i
) {
1480 if (!tab
->var
[i
].is_row
)
1481 isl_int_set_si(vec
->block
.data
[1 + i
], 0);
1483 int row
= tab
->var
[i
].index
;
1484 isl_int_divexact(vec
->block
.data
[1 + i
],
1485 tab
->mat
->row
[row
][1], tab
->mat
->row
[row
][0]);
1492 struct isl_vec
*isl_tab_get_sample_value(struct isl_tab
*tab
)
1495 struct isl_vec
*vec
;
1501 vec
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_var
);
1507 isl_int_set_si(vec
->block
.data
[0], 1);
1508 for (i
= 0; i
< tab
->n_var
; ++i
) {
1510 if (!tab
->var
[i
].is_row
) {
1511 isl_int_set_si(vec
->block
.data
[1 + i
], 0);
1514 row
= tab
->var
[i
].index
;
1515 isl_int_gcd(m
, vec
->block
.data
[0], tab
->mat
->row
[row
][0]);
1516 isl_int_divexact(m
, tab
->mat
->row
[row
][0], m
);
1517 isl_seq_scale(vec
->block
.data
, vec
->block
.data
, m
, 1 + i
);
1518 isl_int_divexact(m
, vec
->block
.data
[0], tab
->mat
->row
[row
][0]);
1519 isl_int_mul(vec
->block
.data
[1 + i
], m
, tab
->mat
->row
[row
][1]);
1521 isl_seq_normalize(vec
->block
.data
, vec
->size
);
1527 /* Update "bmap" based on the results of the tableau "tab".
1528 * In particular, implicit equalities are made explicit, redundant constraints
1529 * are removed and if the sample value happens to be integer, it is stored
1530 * in "bmap" (unless "bmap" already had an integer sample).
1532 * The tableau is assumed to have been created from "bmap" using
1533 * isl_tab_from_basic_map.
1535 struct isl_basic_map
*isl_basic_map_update_from_tab(struct isl_basic_map
*bmap
,
1536 struct isl_tab
*tab
)
1548 bmap
= isl_basic_map_set_to_empty(bmap
);
1550 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
1551 if (isl_tab_is_equality(tab
, n_eq
+ i
))
1552 isl_basic_map_inequality_to_equality(bmap
, i
);
1553 else if (isl_tab_is_redundant(tab
, n_eq
+ i
))
1554 isl_basic_map_drop_inequality(bmap
, i
);
1556 if (!tab
->rational
&&
1557 !bmap
->sample
&& isl_tab_sample_is_integer(tab
))
1558 bmap
->sample
= extract_integer_sample(tab
);
1562 struct isl_basic_set
*isl_basic_set_update_from_tab(struct isl_basic_set
*bset
,
1563 struct isl_tab
*tab
)
1565 return (struct isl_basic_set
*)isl_basic_map_update_from_tab(
1566 (struct isl_basic_map
*)bset
, tab
);
1569 /* Given a non-negative variable "var", add a new non-negative variable
1570 * that is the opposite of "var", ensuring that var can only attain the
1572 * If var = n/d is a row variable, then the new variable = -n/d.
1573 * If var is a column variables, then the new variable = -var.
1574 * If the new variable cannot attain non-negative values, then
1575 * the resulting tableau is empty.
1576 * Otherwise, we know the value will be zero and we close the row.
1578 static struct isl_tab
*cut_to_hyperplane(struct isl_tab
*tab
,
1579 struct isl_tab_var
*var
)
1584 unsigned off
= 2 + tab
->M
;
1586 if (isl_tab_extend_cons(tab
, 1) < 0)
1590 tab
->con
[r
].index
= tab
->n_row
;
1591 tab
->con
[r
].is_row
= 1;
1592 tab
->con
[r
].is_nonneg
= 0;
1593 tab
->con
[r
].is_zero
= 0;
1594 tab
->con
[r
].is_redundant
= 0;
1595 tab
->con
[r
].frozen
= 0;
1596 tab
->row_var
[tab
->n_row
] = ~r
;
1597 row
= tab
->mat
->row
[tab
->n_row
];
1600 isl_int_set(row
[0], tab
->mat
->row
[var
->index
][0]);
1601 isl_seq_neg(row
+ 1,
1602 tab
->mat
->row
[var
->index
] + 1, 1 + tab
->n_col
);
1604 isl_int_set_si(row
[0], 1);
1605 isl_seq_clr(row
+ 1, 1 + tab
->n_col
);
1606 isl_int_set_si(row
[off
+ var
->index
], -1);
1611 isl_tab_push_var(tab
, isl_tab_undo_allocate
, &tab
->con
[r
]);
1613 sgn
= sign_of_max(tab
, &tab
->con
[r
]);
1615 return isl_tab_mark_empty(tab
);
1616 tab
->con
[r
].is_nonneg
= 1;
1617 isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]);
1619 close_row(tab
, &tab
->con
[r
]);
1627 /* Given a tableau "tab" and an inequality constraint "con" of the tableau,
1628 * relax the inequality by one. That is, the inequality r >= 0 is replaced
1629 * by r' = r + 1 >= 0.
1630 * If r is a row variable, we simply increase the constant term by one
1631 * (taking into account the denominator).
1632 * If r is a column variable, then we need to modify each row that
1633 * refers to r = r' - 1 by substituting this equality, effectively
1634 * subtracting the coefficient of the column from the constant.
1636 struct isl_tab
*isl_tab_relax(struct isl_tab
*tab
, int con
)
1638 struct isl_tab_var
*var
;
1639 unsigned off
= 2 + tab
->M
;
1644 var
= &tab
->con
[con
];
1646 if (!var
->is_row
&& !max_is_manifestly_unbounded(tab
, var
))
1647 to_row(tab
, var
, 1);
1650 isl_int_add(tab
->mat
->row
[var
->index
][1],
1651 tab
->mat
->row
[var
->index
][1], tab
->mat
->row
[var
->index
][0]);
1655 for (i
= 0; i
< tab
->n_row
; ++i
) {
1656 if (isl_int_is_zero(tab
->mat
->row
[i
][off
+ var
->index
]))
1658 isl_int_sub(tab
->mat
->row
[i
][1], tab
->mat
->row
[i
][1],
1659 tab
->mat
->row
[i
][off
+ var
->index
]);
1664 isl_tab_push_var(tab
, isl_tab_undo_relax
, var
);
1669 struct isl_tab
*isl_tab_select_facet(struct isl_tab
*tab
, int con
)
1674 return cut_to_hyperplane(tab
, &tab
->con
[con
]);
1677 static int may_be_equality(struct isl_tab
*tab
, int row
)
1679 unsigned off
= 2 + tab
->M
;
1680 return (tab
->rational
? isl_int_is_zero(tab
->mat
->row
[row
][1])
1681 : isl_int_lt(tab
->mat
->row
[row
][1],
1682 tab
->mat
->row
[row
][0])) &&
1683 isl_seq_first_non_zero(tab
->mat
->row
[row
] + off
+ tab
->n_dead
,
1684 tab
->n_col
- tab
->n_dead
) != -1;
1687 /* Check for (near) equalities among the constraints.
1688 * A constraint is an equality if it is non-negative and if
1689 * its maximal value is either
1690 * - zero (in case of rational tableaus), or
1691 * - strictly less than 1 (in case of integer tableaus)
1693 * We first mark all non-redundant and non-dead variables that
1694 * are not frozen and not obviously not an equality.
1695 * Then we iterate over all marked variables if they can attain
1696 * any values larger than zero or at least one.
1697 * If the maximal value is zero, we mark any column variables
1698 * that appear in the row as being zero and mark the row as being redundant.
1699 * Otherwise, if the maximal value is strictly less than one (and the
1700 * tableau is integer), then we restrict the value to being zero
1701 * by adding an opposite non-negative variable.
1703 struct isl_tab
*isl_tab_detect_equalities(struct isl_tab
*tab
)
1712 if (tab
->n_dead
== tab
->n_col
)
1716 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
1717 struct isl_tab_var
*var
= isl_tab_var_from_row(tab
, i
);
1718 var
->marked
= !var
->frozen
&& var
->is_nonneg
&&
1719 may_be_equality(tab
, i
);
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
;
1730 struct isl_tab_var
*var
;
1731 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
1732 var
= isl_tab_var_from_row(tab
, i
);
1736 if (i
== tab
->n_row
) {
1737 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
1738 var
= var_from_col(tab
, i
);
1742 if (i
== tab
->n_col
)
1747 if (sign_of_max(tab
, var
) == 0)
1748 close_row(tab
, var
);
1749 else if (!tab
->rational
&& !at_least_one(tab
, var
)) {
1750 tab
= cut_to_hyperplane(tab
, var
);
1751 return isl_tab_detect_equalities(tab
);
1753 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
1754 var
= isl_tab_var_from_row(tab
, i
);
1757 if (may_be_equality(tab
, i
))
1767 /* Check for (near) redundant constraints.
1768 * A constraint is redundant if it is non-negative and if
1769 * its minimal value (temporarily ignoring the non-negativity) is either
1770 * - zero (in case of rational tableaus), or
1771 * - strictly larger than -1 (in case of integer tableaus)
1773 * We first mark all non-redundant and non-dead variables that
1774 * are not frozen and not obviously negatively unbounded.
1775 * Then we iterate over all marked variables if they can attain
1776 * any values smaller than zero or at most negative one.
1777 * If not, we mark the row as being redundant (assuming it hasn't
1778 * been detected as being obviously redundant in the mean time).
1780 struct isl_tab
*isl_tab_detect_redundant(struct isl_tab
*tab
)
1789 if (tab
->n_redundant
== tab
->n_row
)
1793 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
1794 struct isl_tab_var
*var
= isl_tab_var_from_row(tab
, i
);
1795 var
->marked
= !var
->frozen
&& var
->is_nonneg
;
1799 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
1800 struct isl_tab_var
*var
= var_from_col(tab
, i
);
1801 var
->marked
= !var
->frozen
&& var
->is_nonneg
&&
1802 !min_is_manifestly_unbounded(tab
, var
);
1807 struct isl_tab_var
*var
;
1808 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
1809 var
= isl_tab_var_from_row(tab
, i
);
1813 if (i
== tab
->n_row
) {
1814 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
1815 var
= var_from_col(tab
, i
);
1819 if (i
== tab
->n_col
)
1824 if ((tab
->rational
? (sign_of_min(tab
, var
) >= 0)
1825 : !isl_tab_min_at_most_neg_one(tab
, var
)) &&
1827 isl_tab_mark_redundant(tab
, var
->index
);
1828 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
1829 var
= var_from_col(tab
, i
);
1832 if (!min_is_manifestly_unbounded(tab
, var
))
1842 int isl_tab_is_equality(struct isl_tab
*tab
, int con
)
1849 if (tab
->con
[con
].is_zero
)
1851 if (tab
->con
[con
].is_redundant
)
1853 if (!tab
->con
[con
].is_row
)
1854 return tab
->con
[con
].index
< tab
->n_dead
;
1856 row
= tab
->con
[con
].index
;
1859 return isl_int_is_zero(tab
->mat
->row
[row
][1]) &&
1860 isl_seq_first_non_zero(tab
->mat
->row
[row
] + 2 + tab
->n_dead
,
1861 tab
->n_col
- tab
->n_dead
) == -1;
1864 /* Return the minimial value of the affine expression "f" with denominator
1865 * "denom" in *opt, *opt_denom, assuming the tableau is not empty and
1866 * the expression cannot attain arbitrarily small values.
1867 * If opt_denom is NULL, then *opt is rounded up to the nearest integer.
1868 * The return value reflects the nature of the result (empty, unbounded,
1869 * minmimal value returned in *opt).
1871 enum isl_lp_result
isl_tab_min(struct isl_tab
*tab
,
1872 isl_int
*f
, isl_int denom
, isl_int
*opt
, isl_int
*opt_denom
,
1876 enum isl_lp_result res
= isl_lp_ok
;
1877 struct isl_tab_var
*var
;
1878 struct isl_tab_undo
*snap
;
1881 return isl_lp_empty
;
1883 snap
= isl_tab_snap(tab
);
1884 r
= isl_tab_add_row(tab
, f
);
1886 return isl_lp_error
;
1888 isl_int_mul(tab
->mat
->row
[var
->index
][0],
1889 tab
->mat
->row
[var
->index
][0], denom
);
1892 find_pivot(tab
, var
, var
, -1, &row
, &col
);
1893 if (row
== var
->index
) {
1894 res
= isl_lp_unbounded
;
1899 isl_tab_pivot(tab
, row
, col
);
1901 if (isl_tab_rollback(tab
, snap
) < 0)
1902 return isl_lp_error
;
1903 if (ISL_FL_ISSET(flags
, ISL_TAB_SAVE_DUAL
)) {
1906 isl_vec_free(tab
->dual
);
1907 tab
->dual
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_con
);
1909 return isl_lp_error
;
1910 isl_int_set(tab
->dual
->el
[0], tab
->mat
->row
[var
->index
][0]);
1911 for (i
= 0; i
< tab
->n_con
; ++i
) {
1912 if (tab
->con
[i
].is_row
)
1913 isl_int_set_si(tab
->dual
->el
[1 + i
], 0);
1915 int pos
= 2 + tab
->con
[i
].index
;
1916 isl_int_set(tab
->dual
->el
[1 + i
],
1917 tab
->mat
->row
[var
->index
][pos
]);
1921 if (res
== isl_lp_ok
) {
1923 isl_int_set(*opt
, tab
->mat
->row
[var
->index
][1]);
1924 isl_int_set(*opt_denom
, tab
->mat
->row
[var
->index
][0]);
1926 isl_int_cdiv_q(*opt
, tab
->mat
->row
[var
->index
][1],
1927 tab
->mat
->row
[var
->index
][0]);
1932 int isl_tab_is_redundant(struct isl_tab
*tab
, int con
)
1939 if (tab
->con
[con
].is_zero
)
1941 if (tab
->con
[con
].is_redundant
)
1943 return tab
->con
[con
].is_row
&& tab
->con
[con
].index
< tab
->n_redundant
;
1946 /* Take a snapshot of the tableau that can be restored by s call to
1949 struct isl_tab_undo
*isl_tab_snap(struct isl_tab
*tab
)
1957 /* Undo the operation performed by isl_tab_relax.
1959 static void unrelax(struct isl_tab
*tab
, struct isl_tab_var
*var
)
1961 unsigned off
= 2 + tab
->M
;
1963 if (!var
->is_row
&& !max_is_manifestly_unbounded(tab
, var
))
1964 to_row(tab
, var
, 1);
1967 isl_int_sub(tab
->mat
->row
[var
->index
][1],
1968 tab
->mat
->row
[var
->index
][1], tab
->mat
->row
[var
->index
][0]);
1972 for (i
= 0; i
< tab
->n_row
; ++i
) {
1973 if (isl_int_is_zero(tab
->mat
->row
[i
][off
+ var
->index
]))
1975 isl_int_add(tab
->mat
->row
[i
][1], tab
->mat
->row
[i
][1],
1976 tab
->mat
->row
[i
][off
+ var
->index
]);
1982 static void perform_undo_var(struct isl_tab
*tab
, struct isl_tab_undo
*undo
)
1984 struct isl_tab_var
*var
= var_from_index(tab
, undo
->u
.var_index
);
1985 switch(undo
->type
) {
1986 case isl_tab_undo_nonneg
:
1989 case isl_tab_undo_redundant
:
1990 var
->is_redundant
= 0;
1993 case isl_tab_undo_zero
:
1997 case isl_tab_undo_allocate
:
1998 if (undo
->u
.var_index
>= 0) {
1999 isl_assert(tab
->mat
->ctx
, !var
->is_row
, return);
2000 drop_col(tab
, var
->index
);
2004 if (!max_is_manifestly_unbounded(tab
, var
))
2005 to_row(tab
, var
, 1);
2006 else if (!min_is_manifestly_unbounded(tab
, var
))
2007 to_row(tab
, var
, -1);
2009 to_row(tab
, var
, 0);
2011 drop_row(tab
, var
->index
);
2013 case isl_tab_undo_relax
:
2019 /* Restore the tableau to the state where the basic variables
2020 * are those in "col_var".
2021 * We first construct a list of variables that are currently in
2022 * the basis, but shouldn't. Then we iterate over all variables
2023 * that should be in the basis and for each one that is currently
2024 * not in the basis, we exchange it with one of the elements of the
2025 * list constructed before.
2026 * We can always find an appropriate variable to pivot with because
2027 * the current basis is mapped to the old basis by a non-singular
2028 * matrix and so we can never end up with a zero row.
2030 static int restore_basis(struct isl_tab
*tab
, int *col_var
)
2034 int *extra
= NULL
; /* current columns that contain bad stuff */
2035 unsigned off
= 2 + tab
->M
;
2037 extra
= isl_alloc_array(tab
->mat
->ctx
, int, tab
->n_col
);
2040 for (i
= 0; i
< tab
->n_col
; ++i
) {
2041 for (j
= 0; j
< tab
->n_col
; ++j
)
2042 if (tab
->col_var
[i
] == col_var
[j
])
2046 extra
[n_extra
++] = i
;
2048 for (i
= 0; i
< tab
->n_col
&& n_extra
> 0; ++i
) {
2049 struct isl_tab_var
*var
;
2052 for (j
= 0; j
< tab
->n_col
; ++j
)
2053 if (col_var
[i
] == tab
->col_var
[j
])
2057 var
= var_from_index(tab
, col_var
[i
]);
2059 for (j
= 0; j
< n_extra
; ++j
)
2060 if (!isl_int_is_zero(tab
->mat
->row
[row
][off
+extra
[j
]]))
2062 isl_assert(tab
->mat
->ctx
, j
< n_extra
, goto error
);
2063 isl_tab_pivot(tab
, row
, extra
[j
]);
2064 extra
[j
] = extra
[--n_extra
];
2076 static int perform_undo(struct isl_tab
*tab
, struct isl_tab_undo
*undo
)
2078 switch (undo
->type
) {
2079 case isl_tab_undo_empty
:
2082 case isl_tab_undo_nonneg
:
2083 case isl_tab_undo_redundant
:
2084 case isl_tab_undo_zero
:
2085 case isl_tab_undo_allocate
:
2086 case isl_tab_undo_relax
:
2087 perform_undo_var(tab
, undo
);
2089 case isl_tab_undo_bset_eq
:
2090 isl_basic_set_free_equality(tab
->bset
, 1);
2092 case isl_tab_undo_bset_ineq
:
2093 isl_basic_set_free_inequality(tab
->bset
, 1);
2095 case isl_tab_undo_bset_div
:
2096 isl_basic_set_free_div(tab
->bset
, 1);
2098 case isl_tab_undo_saved_basis
:
2099 if (restore_basis(tab
, undo
->u
.col_var
) < 0)
2103 isl_assert(tab
->mat
->ctx
, 0, return -1);
2108 /* Return the tableau to the state it was in when the snapshot "snap"
2111 int isl_tab_rollback(struct isl_tab
*tab
, struct isl_tab_undo
*snap
)
2113 struct isl_tab_undo
*undo
, *next
;
2119 for (undo
= tab
->top
; undo
&& undo
!= &tab
->bottom
; undo
= next
) {
2123 if (perform_undo(tab
, undo
) < 0) {
2137 /* The given row "row" represents an inequality violated by all
2138 * points in the tableau. Check for some special cases of such
2139 * separating constraints.
2140 * In particular, if the row has been reduced to the constant -1,
2141 * then we know the inequality is adjacent (but opposite) to
2142 * an equality in the tableau.
2143 * If the row has been reduced to r = -1 -r', with r' an inequality
2144 * of the tableau, then the inequality is adjacent (but opposite)
2145 * to the inequality r'.
2147 static enum isl_ineq_type
separation_type(struct isl_tab
*tab
, unsigned row
)
2150 unsigned off
= 2 + tab
->M
;
2153 return isl_ineq_separate
;
2155 if (!isl_int_is_one(tab
->mat
->row
[row
][0]))
2156 return isl_ineq_separate
;
2157 if (!isl_int_is_negone(tab
->mat
->row
[row
][1]))
2158 return isl_ineq_separate
;
2160 pos
= isl_seq_first_non_zero(tab
->mat
->row
[row
] + off
+ tab
->n_dead
,
2161 tab
->n_col
- tab
->n_dead
);
2163 return isl_ineq_adj_eq
;
2165 if (!isl_int_is_negone(tab
->mat
->row
[row
][off
+ tab
->n_dead
+ pos
]))
2166 return isl_ineq_separate
;
2168 pos
= isl_seq_first_non_zero(
2169 tab
->mat
->row
[row
] + off
+ tab
->n_dead
+ pos
+ 1,
2170 tab
->n_col
- tab
->n_dead
- pos
- 1);
2172 return pos
== -1 ? isl_ineq_adj_ineq
: isl_ineq_separate
;
2175 /* Check the effect of inequality "ineq" on the tableau "tab".
2177 * isl_ineq_redundant: satisfied by all points in the tableau
2178 * isl_ineq_separate: satisfied by no point in the tableau
2179 * isl_ineq_cut: satisfied by some by not all points
2180 * isl_ineq_adj_eq: adjacent to an equality
2181 * isl_ineq_adj_ineq: adjacent to an inequality.
2183 enum isl_ineq_type
isl_tab_ineq_type(struct isl_tab
*tab
, isl_int
*ineq
)
2185 enum isl_ineq_type type
= isl_ineq_error
;
2186 struct isl_tab_undo
*snap
= NULL
;
2191 return isl_ineq_error
;
2193 if (isl_tab_extend_cons(tab
, 1) < 0)
2194 return isl_ineq_error
;
2196 snap
= isl_tab_snap(tab
);
2198 con
= isl_tab_add_row(tab
, ineq
);
2202 row
= tab
->con
[con
].index
;
2203 if (isl_tab_row_is_redundant(tab
, row
))
2204 type
= isl_ineq_redundant
;
2205 else if (isl_int_is_neg(tab
->mat
->row
[row
][1]) &&
2207 isl_int_abs_ge(tab
->mat
->row
[row
][1],
2208 tab
->mat
->row
[row
][0]))) {
2209 if (at_least_zero(tab
, &tab
->con
[con
]))
2210 type
= isl_ineq_cut
;
2212 type
= separation_type(tab
, row
);
2213 } else if (tab
->rational
? (sign_of_min(tab
, &tab
->con
[con
]) < 0)
2214 : isl_tab_min_at_most_neg_one(tab
, &tab
->con
[con
]))
2215 type
= isl_ineq_cut
;
2217 type
= isl_ineq_redundant
;
2219 if (isl_tab_rollback(tab
, snap
))
2220 return isl_ineq_error
;
2223 isl_tab_rollback(tab
, snap
);
2224 return isl_ineq_error
;
2227 void isl_tab_dump(struct isl_tab
*tab
, FILE *out
, int indent
)
2233 fprintf(out
, "%*snull tab\n", indent
, "");
2236 fprintf(out
, "%*sn_redundant: %d, n_dead: %d", indent
, "",
2237 tab
->n_redundant
, tab
->n_dead
);
2239 fprintf(out
, ", rational");
2241 fprintf(out
, ", empty");
2243 fprintf(out
, "%*s[", indent
, "");
2244 for (i
= 0; i
< tab
->n_var
; ++i
) {
2246 fprintf(out
, (i
== tab
->n_param
||
2247 i
== tab
->n_var
- tab
->n_div
) ? "; "
2249 fprintf(out
, "%c%d%s", tab
->var
[i
].is_row
? 'r' : 'c',
2251 tab
->var
[i
].is_zero
? " [=0]" :
2252 tab
->var
[i
].is_redundant
? " [R]" : "");
2254 fprintf(out
, "]\n");
2255 fprintf(out
, "%*s[", indent
, "");
2256 for (i
= 0; i
< tab
->n_con
; ++i
) {
2259 fprintf(out
, "%c%d%s", tab
->con
[i
].is_row
? 'r' : 'c',
2261 tab
->con
[i
].is_zero
? " [=0]" :
2262 tab
->con
[i
].is_redundant
? " [R]" : "");
2264 fprintf(out
, "]\n");
2265 fprintf(out
, "%*s[", indent
, "");
2266 for (i
= 0; i
< tab
->n_row
; ++i
) {
2267 const char *sign
= "";
2270 if (tab
->row_sign
) {
2271 if (tab
->row_sign
[i
] == isl_tab_row_unknown
)
2273 else if (tab
->row_sign
[i
] == isl_tab_row_neg
)
2275 else if (tab
->row_sign
[i
] == isl_tab_row_pos
)
2280 fprintf(out
, "r%d: %d%s%s", i
, tab
->row_var
[i
],
2281 isl_tab_var_from_row(tab
, i
)->is_nonneg
? " [>=0]" : "", sign
);
2283 fprintf(out
, "]\n");
2284 fprintf(out
, "%*s[", indent
, "");
2285 for (i
= 0; i
< tab
->n_col
; ++i
) {
2288 fprintf(out
, "c%d: %d%s", i
, tab
->col_var
[i
],
2289 var_from_col(tab
, i
)->is_nonneg
? " [>=0]" : "");
2291 fprintf(out
, "]\n");
2292 r
= tab
->mat
->n_row
;
2293 tab
->mat
->n_row
= tab
->n_row
;
2294 c
= tab
->mat
->n_col
;
2295 tab
->mat
->n_col
= 2 + tab
->M
+ tab
->n_col
;
2296 isl_mat_dump(tab
->mat
, out
, indent
);
2297 tab
->mat
->n_row
= r
;
2298 tab
->mat
->n_col
= c
;
2300 isl_basic_set_dump(tab
->bset
, out
, indent
);