2 #include "isl_map_private.h"
7 * The implementation of tableaus in this file was inspired by Section 8
8 * of David Detlefs, Greg Nelson and James B. Saxe, "Simplify: a theorem
9 * prover for program checking".
12 struct isl_tab
*isl_tab_alloc(struct isl_ctx
*ctx
,
13 unsigned n_row
, unsigned n_var
, unsigned M
)
19 tab
= isl_calloc_type(ctx
, struct isl_tab
);
22 tab
->mat
= isl_mat_alloc(ctx
, n_row
, off
+ n_var
);
25 tab
->var
= isl_alloc_array(ctx
, struct isl_tab_var
, n_var
);
28 tab
->con
= isl_alloc_array(ctx
, struct isl_tab_var
, n_row
);
31 tab
->col_var
= isl_alloc_array(ctx
, int, n_var
);
34 tab
->row_var
= isl_alloc_array(ctx
, int, n_row
);
37 for (i
= 0; i
< n_var
; ++i
) {
38 tab
->var
[i
].index
= i
;
39 tab
->var
[i
].is_row
= 0;
40 tab
->var
[i
].is_nonneg
= 0;
41 tab
->var
[i
].is_zero
= 0;
42 tab
->var
[i
].is_redundant
= 0;
43 tab
->var
[i
].frozen
= 0;
44 tab
->var
[i
].negated
= 0;
63 tab
->bottom
.type
= isl_tab_undo_bottom
;
64 tab
->bottom
.next
= NULL
;
65 tab
->top
= &tab
->bottom
;
76 int isl_tab_extend_cons(struct isl_tab
*tab
, unsigned n_new
)
78 unsigned off
= 2 + tab
->M
;
83 if (tab
->max_con
< tab
->n_con
+ n_new
) {
84 struct isl_tab_var
*con
;
86 con
= isl_realloc_array(tab
->mat
->ctx
, tab
->con
,
87 struct isl_tab_var
, tab
->max_con
+ n_new
);
91 tab
->max_con
+= n_new
;
93 if (tab
->mat
->n_row
< tab
->n_row
+ n_new
) {
96 tab
->mat
= isl_mat_extend(tab
->mat
,
97 tab
->n_row
+ n_new
, off
+ tab
->n_col
);
100 row_var
= isl_realloc_array(tab
->mat
->ctx
, tab
->row_var
,
101 int, tab
->mat
->n_row
);
104 tab
->row_var
= row_var
;
106 enum isl_tab_row_sign
*s
;
107 s
= isl_realloc_array(tab
->mat
->ctx
, tab
->row_sign
,
108 enum isl_tab_row_sign
, tab
->mat
->n_row
);
117 /* Make room for at least n_new extra variables.
118 * Return -1 if anything went wrong.
120 int isl_tab_extend_vars(struct isl_tab
*tab
, unsigned n_new
)
122 struct isl_tab_var
*var
;
123 unsigned off
= 2 + tab
->M
;
125 if (tab
->max_var
< tab
->n_var
+ n_new
) {
126 var
= isl_realloc_array(tab
->mat
->ctx
, tab
->var
,
127 struct isl_tab_var
, tab
->n_var
+ n_new
);
131 tab
->max_var
+= n_new
;
134 if (tab
->mat
->n_col
< off
+ tab
->n_col
+ n_new
) {
137 tab
->mat
= isl_mat_extend(tab
->mat
,
138 tab
->mat
->n_row
, off
+ tab
->n_col
+ n_new
);
141 p
= isl_realloc_array(tab
->mat
->ctx
, tab
->col_var
,
142 int, tab
->n_col
+ n_new
);
151 struct isl_tab
*isl_tab_extend(struct isl_tab
*tab
, unsigned n_new
)
153 if (isl_tab_extend_cons(tab
, n_new
) >= 0)
160 static void free_undo(struct isl_tab
*tab
)
162 struct isl_tab_undo
*undo
, *next
;
164 for (undo
= tab
->top
; undo
&& undo
!= &tab
->bottom
; undo
= next
) {
171 void isl_tab_free(struct isl_tab
*tab
)
176 isl_mat_free(tab
->mat
);
177 isl_vec_free(tab
->dual
);
178 isl_basic_set_free(tab
->bset
);
184 isl_mat_free(tab
->samples
);
185 isl_mat_free(tab
->basis
);
189 struct isl_tab
*isl_tab_dup(struct isl_tab
*tab
)
199 dup
= isl_calloc_type(tab
->ctx
, struct isl_tab
);
202 dup
->mat
= isl_mat_dup(tab
->mat
);
205 dup
->var
= isl_alloc_array(tab
->ctx
, struct isl_tab_var
, tab
->max_var
);
208 for (i
= 0; i
< tab
->n_var
; ++i
)
209 dup
->var
[i
] = tab
->var
[i
];
210 dup
->con
= isl_alloc_array(tab
->ctx
, struct isl_tab_var
, tab
->max_con
);
213 for (i
= 0; i
< tab
->n_con
; ++i
)
214 dup
->con
[i
] = tab
->con
[i
];
215 dup
->col_var
= isl_alloc_array(tab
->ctx
, int, tab
->mat
->n_col
- off
);
218 for (i
= 0; i
< tab
->n_col
; ++i
)
219 dup
->col_var
[i
] = tab
->col_var
[i
];
220 dup
->row_var
= isl_alloc_array(tab
->ctx
, int, tab
->mat
->n_row
);
223 for (i
= 0; i
< tab
->n_row
; ++i
)
224 dup
->row_var
[i
] = tab
->row_var
[i
];
226 dup
->row_sign
= isl_alloc_array(tab
->ctx
, enum isl_tab_row_sign
,
230 for (i
= 0; i
< tab
->n_row
; ++i
)
231 dup
->row_sign
[i
] = tab
->row_sign
[i
];
234 dup
->samples
= isl_mat_dup(tab
->samples
);
237 dup
->n_sample
= tab
->n_sample
;
238 dup
->n_outside
= tab
->n_outside
;
240 dup
->n_row
= tab
->n_row
;
241 dup
->n_con
= tab
->n_con
;
242 dup
->n_eq
= tab
->n_eq
;
243 dup
->max_con
= tab
->max_con
;
244 dup
->n_col
= tab
->n_col
;
245 dup
->n_var
= tab
->n_var
;
246 dup
->max_var
= tab
->max_var
;
247 dup
->n_param
= tab
->n_param
;
248 dup
->n_div
= tab
->n_div
;
249 dup
->n_dead
= tab
->n_dead
;
250 dup
->n_redundant
= tab
->n_redundant
;
251 dup
->rational
= tab
->rational
;
252 dup
->empty
= tab
->empty
;
256 dup
->bottom
.type
= isl_tab_undo_bottom
;
257 dup
->bottom
.next
= NULL
;
258 dup
->top
= &dup
->bottom
;
260 dup
->n_zero
= tab
->n_zero
;
261 dup
->basis
= isl_mat_dup(tab
->basis
);
269 /* Construct the coefficient matrix of the product tableau
271 * mat{1,2} is the coefficient matrix of tableau {1,2}
272 * row{1,2} is the number of rows in tableau {1,2}
273 * col{1,2} is the number of columns in tableau {1,2}
274 * off is the offset to the coefficient column (skipping the
275 * denominator, the constant term and the big parameter if any)
276 * r{1,2} is the number of redundant rows in tableau {1,2}
277 * d{1,2} is the number of dead columns in tableau {1,2}
279 * The order of the rows and columns in the result is as explained
280 * in isl_tab_product.
282 static struct isl_mat
*tab_mat_product(struct isl_mat
*mat1
,
283 struct isl_mat
*mat2
, unsigned row1
, unsigned row2
,
284 unsigned col1
, unsigned col2
,
285 unsigned off
, unsigned r1
, unsigned r2
, unsigned d1
, unsigned d2
)
288 struct isl_mat
*prod
;
291 prod
= isl_mat_alloc(mat1
->ctx
, mat1
->n_row
+ mat2
->n_row
,
295 for (i
= 0; i
< r1
; ++i
) {
296 isl_seq_cpy(prod
->row
[n
+ i
], mat1
->row
[i
], off
+ d1
);
297 isl_seq_clr(prod
->row
[n
+ i
] + off
+ d1
, d2
);
298 isl_seq_cpy(prod
->row
[n
+ i
] + off
+ d1
+ d2
,
299 mat1
->row
[i
] + off
+ d1
, col1
- d1
);
300 isl_seq_clr(prod
->row
[n
+ i
] + off
+ col1
+ d1
, col2
- d2
);
304 for (i
= 0; i
< r2
; ++i
) {
305 isl_seq_cpy(prod
->row
[n
+ i
], mat2
->row
[i
], off
);
306 isl_seq_clr(prod
->row
[n
+ i
] + off
, d1
);
307 isl_seq_cpy(prod
->row
[n
+ i
] + off
+ d1
,
308 mat2
->row
[i
] + off
, d2
);
309 isl_seq_clr(prod
->row
[n
+ i
] + off
+ d1
+ d2
, col1
- d1
);
310 isl_seq_cpy(prod
->row
[n
+ i
] + off
+ col1
+ d1
,
311 mat2
->row
[i
] + off
+ d2
, col2
- d2
);
315 for (i
= 0; i
< row1
- r1
; ++i
) {
316 isl_seq_cpy(prod
->row
[n
+ i
], mat1
->row
[r1
+ i
], off
+ d1
);
317 isl_seq_clr(prod
->row
[n
+ i
] + off
+ d1
, d2
);
318 isl_seq_cpy(prod
->row
[n
+ i
] + off
+ d1
+ d2
,
319 mat1
->row
[r1
+ i
] + off
+ d1
, col1
- d1
);
320 isl_seq_clr(prod
->row
[n
+ i
] + off
+ col1
+ d1
, col2
- d2
);
324 for (i
= 0; i
< row2
- r2
; ++i
) {
325 isl_seq_cpy(prod
->row
[n
+ i
], mat2
->row
[r2
+ i
], off
);
326 isl_seq_clr(prod
->row
[n
+ i
] + off
, d1
);
327 isl_seq_cpy(prod
->row
[n
+ i
] + off
+ d1
,
328 mat2
->row
[r2
+ i
] + off
, d2
);
329 isl_seq_clr(prod
->row
[n
+ i
] + off
+ d1
+ d2
, col1
- d1
);
330 isl_seq_cpy(prod
->row
[n
+ i
] + off
+ col1
+ d1
,
331 mat2
->row
[r2
+ i
] + off
+ d2
, col2
- d2
);
337 /* Update the row or column index of a variable that corresponds
338 * to a variable in the first input tableau.
340 static void update_index1(struct isl_tab_var
*var
,
341 unsigned r1
, unsigned r2
, unsigned d1
, unsigned d2
)
343 if (var
->index
== -1)
345 if (var
->is_row
&& var
->index
>= r1
)
347 if (!var
->is_row
&& var
->index
>= d1
)
351 /* Update the row or column index of a variable that corresponds
352 * to a variable in the second input tableau.
354 static void update_index2(struct isl_tab_var
*var
,
355 unsigned row1
, unsigned col1
,
356 unsigned r1
, unsigned r2
, unsigned d1
, unsigned d2
)
358 if (var
->index
== -1)
373 /* Create a tableau that represents the Cartesian product of the sets
374 * represented by tableaus tab1 and tab2.
375 * The order of the rows in the product is
376 * - redundant rows of tab1
377 * - redundant rows of tab2
378 * - non-redundant rows of tab1
379 * - non-redundant rows of tab2
380 * The order of the columns is
383 * - coefficient of big parameter, if any
384 * - dead columns of tab1
385 * - dead columns of tab2
386 * - live columns of tab1
387 * - live columns of tab2
388 * The order of the variables and the constraints is a concatenation
389 * of order in the two input tableaus.
391 struct isl_tab
*isl_tab_product(struct isl_tab
*tab1
, struct isl_tab
*tab2
)
394 struct isl_tab
*prod
;
396 unsigned r1
, r2
, d1
, d2
;
401 isl_assert(tab1
->mat
->ctx
, tab1
->M
== tab2
->M
, return NULL
);
402 isl_assert(tab1
->mat
->ctx
, tab1
->rational
== tab2
->rational
, return NULL
);
403 isl_assert(tab1
->mat
->ctx
, !tab1
->row_sign
, return NULL
);
404 isl_assert(tab1
->mat
->ctx
, !tab2
->row_sign
, return NULL
);
405 isl_assert(tab1
->mat
->ctx
, tab1
->n_param
== 0, return NULL
);
406 isl_assert(tab1
->mat
->ctx
, tab2
->n_param
== 0, return NULL
);
407 isl_assert(tab1
->mat
->ctx
, tab1
->n_div
== 0, return NULL
);
408 isl_assert(tab1
->mat
->ctx
, tab2
->n_div
== 0, return NULL
);
411 r1
= tab1
->n_redundant
;
412 r2
= tab2
->n_redundant
;
415 prod
= isl_calloc_type(tab1
->mat
->ctx
, struct isl_tab
);
418 prod
->mat
= tab_mat_product(tab1
->mat
, tab2
->mat
,
419 tab1
->n_row
, tab2
->n_row
,
420 tab1
->n_col
, tab2
->n_col
, off
, r1
, r2
, d1
, d2
);
423 prod
->var
= isl_alloc_array(tab1
->mat
->ctx
, struct isl_tab_var
,
424 tab1
->max_var
+ tab2
->max_var
);
427 for (i
= 0; i
< tab1
->n_var
; ++i
) {
428 prod
->var
[i
] = tab1
->var
[i
];
429 update_index1(&prod
->var
[i
], r1
, r2
, d1
, d2
);
431 for (i
= 0; i
< tab2
->n_var
; ++i
) {
432 prod
->var
[tab1
->n_var
+ i
] = tab2
->var
[i
];
433 update_index2(&prod
->var
[tab1
->n_var
+ i
],
434 tab1
->n_row
, tab1
->n_col
,
437 prod
->con
= isl_alloc_array(tab1
->mat
->ctx
, struct isl_tab_var
,
438 tab1
->max_con
+ tab2
->max_con
);
441 for (i
= 0; i
< tab1
->n_con
; ++i
) {
442 prod
->con
[i
] = tab1
->con
[i
];
443 update_index1(&prod
->con
[i
], r1
, r2
, d1
, d2
);
445 for (i
= 0; i
< tab2
->n_con
; ++i
) {
446 prod
->con
[tab1
->n_con
+ i
] = tab2
->con
[i
];
447 update_index2(&prod
->con
[tab1
->n_con
+ i
],
448 tab1
->n_row
, tab1
->n_col
,
451 prod
->col_var
= isl_alloc_array(tab1
->mat
->ctx
, int,
452 tab1
->n_col
+ tab2
->n_col
);
455 for (i
= 0; i
< tab1
->n_col
; ++i
) {
456 int pos
= i
< d1
? i
: i
+ d2
;
457 prod
->col_var
[pos
] = tab1
->col_var
[i
];
459 for (i
= 0; i
< tab2
->n_col
; ++i
) {
460 int pos
= i
< d2
? d1
+ i
: tab1
->n_col
+ i
;
461 int t
= tab2
->col_var
[i
];
466 prod
->col_var
[pos
] = t
;
468 prod
->row_var
= isl_alloc_array(tab1
->mat
->ctx
, int,
469 tab1
->mat
->n_row
+ tab2
->mat
->n_row
);
472 for (i
= 0; i
< tab1
->n_row
; ++i
) {
473 int pos
= i
< r1
? i
: i
+ r2
;
474 prod
->row_var
[pos
] = tab1
->row_var
[i
];
476 for (i
= 0; i
< tab2
->n_row
; ++i
) {
477 int pos
= i
< r2
? r1
+ i
: tab1
->n_row
+ i
;
478 int t
= tab2
->row_var
[i
];
483 prod
->row_var
[pos
] = t
;
485 prod
->samples
= NULL
;
486 prod
->n_row
= tab1
->n_row
+ tab2
->n_row
;
487 prod
->n_con
= tab1
->n_con
+ tab2
->n_con
;
489 prod
->max_con
= tab1
->max_con
+ tab2
->max_con
;
490 prod
->n_col
= tab1
->n_col
+ tab2
->n_col
;
491 prod
->n_var
= tab1
->n_var
+ tab2
->n_var
;
492 prod
->max_var
= tab1
->max_var
+ tab2
->max_var
;
495 prod
->n_dead
= tab1
->n_dead
+ tab2
->n_dead
;
496 prod
->n_redundant
= tab1
->n_redundant
+ tab2
->n_redundant
;
497 prod
->rational
= tab1
->rational
;
498 prod
->empty
= tab1
->empty
|| tab2
->empty
;
502 prod
->bottom
.type
= isl_tab_undo_bottom
;
503 prod
->bottom
.next
= NULL
;
504 prod
->top
= &prod
->bottom
;
515 static struct isl_tab_var
*var_from_index(struct isl_tab
*tab
, int i
)
520 return &tab
->con
[~i
];
523 struct isl_tab_var
*isl_tab_var_from_row(struct isl_tab
*tab
, int i
)
525 return var_from_index(tab
, tab
->row_var
[i
]);
528 static struct isl_tab_var
*var_from_col(struct isl_tab
*tab
, int i
)
530 return var_from_index(tab
, tab
->col_var
[i
]);
533 /* Check if there are any upper bounds on column variable "var",
534 * i.e., non-negative rows where var appears with a negative coefficient.
535 * Return 1 if there are no such bounds.
537 static int max_is_manifestly_unbounded(struct isl_tab
*tab
,
538 struct isl_tab_var
*var
)
541 unsigned off
= 2 + tab
->M
;
545 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
546 if (!isl_int_is_neg(tab
->mat
->row
[i
][off
+ var
->index
]))
548 if (isl_tab_var_from_row(tab
, i
)->is_nonneg
)
554 /* Check if there are any lower bounds on column variable "var",
555 * i.e., non-negative rows where var appears with a positive coefficient.
556 * Return 1 if there are no such bounds.
558 static int min_is_manifestly_unbounded(struct isl_tab
*tab
,
559 struct isl_tab_var
*var
)
562 unsigned off
= 2 + tab
->M
;
566 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
567 if (!isl_int_is_pos(tab
->mat
->row
[i
][off
+ var
->index
]))
569 if (isl_tab_var_from_row(tab
, i
)->is_nonneg
)
575 static int row_cmp(struct isl_tab
*tab
, int r1
, int r2
, int c
, isl_int t
)
577 unsigned off
= 2 + tab
->M
;
581 isl_int_mul(t
, tab
->mat
->row
[r1
][2], tab
->mat
->row
[r2
][off
+c
]);
582 isl_int_submul(t
, tab
->mat
->row
[r2
][2], tab
->mat
->row
[r1
][off
+c
]);
587 isl_int_mul(t
, tab
->mat
->row
[r1
][1], tab
->mat
->row
[r2
][off
+ c
]);
588 isl_int_submul(t
, tab
->mat
->row
[r2
][1], tab
->mat
->row
[r1
][off
+ c
]);
589 return isl_int_sgn(t
);
592 /* Given the index of a column "c", return the index of a row
593 * that can be used to pivot the column in, with either an increase
594 * (sgn > 0) or a decrease (sgn < 0) of the corresponding variable.
595 * If "var" is not NULL, then the row returned will be different from
596 * the one associated with "var".
598 * Each row in the tableau is of the form
600 * x_r = a_r0 + \sum_i a_ri x_i
602 * Only rows with x_r >= 0 and with the sign of a_ri opposite to "sgn"
603 * impose any limit on the increase or decrease in the value of x_c
604 * and this bound is equal to a_r0 / |a_rc|. We are therefore looking
605 * for the row with the smallest (most stringent) such bound.
606 * Note that the common denominator of each row drops out of the fraction.
607 * To check if row j has a smaller bound than row r, i.e.,
608 * a_j0 / |a_jc| < a_r0 / |a_rc| or a_j0 |a_rc| < a_r0 |a_jc|,
609 * we check if -sign(a_jc) (a_j0 a_rc - a_r0 a_jc) < 0,
610 * where -sign(a_jc) is equal to "sgn".
612 static int pivot_row(struct isl_tab
*tab
,
613 struct isl_tab_var
*var
, int sgn
, int c
)
617 unsigned off
= 2 + tab
->M
;
621 for (j
= tab
->n_redundant
; j
< tab
->n_row
; ++j
) {
622 if (var
&& j
== var
->index
)
624 if (!isl_tab_var_from_row(tab
, j
)->is_nonneg
)
626 if (sgn
* isl_int_sgn(tab
->mat
->row
[j
][off
+ c
]) >= 0)
632 tsgn
= sgn
* row_cmp(tab
, r
, j
, c
, t
);
633 if (tsgn
< 0 || (tsgn
== 0 &&
634 tab
->row_var
[j
] < tab
->row_var
[r
]))
641 /* Find a pivot (row and col) that will increase (sgn > 0) or decrease
642 * (sgn < 0) the value of row variable var.
643 * If not NULL, then skip_var is a row variable that should be ignored
644 * while looking for a pivot row. It is usually equal to var.
646 * As the given row in the tableau is of the form
648 * x_r = a_r0 + \sum_i a_ri x_i
650 * we need to find a column such that the sign of a_ri is equal to "sgn"
651 * (such that an increase in x_i will have the desired effect) or a
652 * column with a variable that may attain negative values.
653 * If a_ri is positive, then we need to move x_i in the same direction
654 * to obtain the desired effect. Otherwise, x_i has to move in the
655 * opposite direction.
657 static void find_pivot(struct isl_tab
*tab
,
658 struct isl_tab_var
*var
, struct isl_tab_var
*skip_var
,
659 int sgn
, int *row
, int *col
)
666 isl_assert(tab
->mat
->ctx
, var
->is_row
, return);
667 tr
= tab
->mat
->row
[var
->index
] + 2 + tab
->M
;
670 for (j
= tab
->n_dead
; j
< tab
->n_col
; ++j
) {
671 if (isl_int_is_zero(tr
[j
]))
673 if (isl_int_sgn(tr
[j
]) != sgn
&&
674 var_from_col(tab
, j
)->is_nonneg
)
676 if (c
< 0 || tab
->col_var
[j
] < tab
->col_var
[c
])
682 sgn
*= isl_int_sgn(tr
[c
]);
683 r
= pivot_row(tab
, skip_var
, sgn
, c
);
684 *row
= r
< 0 ? var
->index
: r
;
688 /* Return 1 if row "row" represents an obviously redundant inequality.
690 * - it represents an inequality or a variable
691 * - that is the sum of a non-negative sample value and a positive
692 * combination of zero or more non-negative variables.
694 int isl_tab_row_is_redundant(struct isl_tab
*tab
, int row
)
697 unsigned off
= 2 + tab
->M
;
699 if (tab
->row_var
[row
] < 0 && !isl_tab_var_from_row(tab
, row
)->is_nonneg
)
702 if (isl_int_is_neg(tab
->mat
->row
[row
][1]))
704 if (tab
->M
&& isl_int_is_neg(tab
->mat
->row
[row
][2]))
707 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
708 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ i
]))
710 if (isl_int_is_neg(tab
->mat
->row
[row
][off
+ i
]))
712 if (!var_from_col(tab
, i
)->is_nonneg
)
718 static void swap_rows(struct isl_tab
*tab
, int row1
, int row2
)
721 t
= tab
->row_var
[row1
];
722 tab
->row_var
[row1
] = tab
->row_var
[row2
];
723 tab
->row_var
[row2
] = t
;
724 isl_tab_var_from_row(tab
, row1
)->index
= row1
;
725 isl_tab_var_from_row(tab
, row2
)->index
= row2
;
726 tab
->mat
= isl_mat_swap_rows(tab
->mat
, row1
, row2
);
730 t
= tab
->row_sign
[row1
];
731 tab
->row_sign
[row1
] = tab
->row_sign
[row2
];
732 tab
->row_sign
[row2
] = t
;
735 static void push_union(struct isl_tab
*tab
,
736 enum isl_tab_undo_type type
, union isl_tab_undo_val u
)
738 struct isl_tab_undo
*undo
;
743 undo
= isl_alloc_type(tab
->mat
->ctx
, struct isl_tab_undo
);
751 undo
->next
= tab
->top
;
755 void isl_tab_push_var(struct isl_tab
*tab
,
756 enum isl_tab_undo_type type
, struct isl_tab_var
*var
)
758 union isl_tab_undo_val u
;
760 u
.var_index
= tab
->row_var
[var
->index
];
762 u
.var_index
= tab
->col_var
[var
->index
];
763 push_union(tab
, type
, u
);
766 void isl_tab_push(struct isl_tab
*tab
, enum isl_tab_undo_type type
)
768 union isl_tab_undo_val u
= { 0 };
769 push_union(tab
, type
, u
);
772 /* Push a record on the undo stack describing the current basic
773 * variables, so that the this state can be restored during rollback.
775 void isl_tab_push_basis(struct isl_tab
*tab
)
778 union isl_tab_undo_val u
;
780 u
.col_var
= isl_alloc_array(tab
->mat
->ctx
, int, tab
->n_col
);
786 for (i
= 0; i
< tab
->n_col
; ++i
)
787 u
.col_var
[i
] = tab
->col_var
[i
];
788 push_union(tab
, isl_tab_undo_saved_basis
, u
);
791 /* Mark row with index "row" as being redundant.
792 * If we may need to undo the operation or if the row represents
793 * a variable of the original problem, the row is kept,
794 * but no longer considered when looking for a pivot row.
795 * Otherwise, the row is simply removed.
797 * The row may be interchanged with some other row. If it
798 * is interchanged with a later row, return 1. Otherwise return 0.
799 * If the rows are checked in order in the calling function,
800 * then a return value of 1 means that the row with the given
801 * row number may now contain a different row that hasn't been checked yet.
803 int isl_tab_mark_redundant(struct isl_tab
*tab
, int row
)
805 struct isl_tab_var
*var
= isl_tab_var_from_row(tab
, row
);
806 var
->is_redundant
= 1;
807 isl_assert(tab
->mat
->ctx
, row
>= tab
->n_redundant
, return -1);
808 if (tab
->need_undo
|| tab
->row_var
[row
] >= 0) {
809 if (tab
->row_var
[row
] >= 0 && !var
->is_nonneg
) {
811 isl_tab_push_var(tab
, isl_tab_undo_nonneg
, var
);
813 if (row
!= tab
->n_redundant
)
814 swap_rows(tab
, row
, tab
->n_redundant
);
815 isl_tab_push_var(tab
, isl_tab_undo_redundant
, var
);
819 if (row
!= tab
->n_row
- 1)
820 swap_rows(tab
, row
, tab
->n_row
- 1);
821 isl_tab_var_from_row(tab
, tab
->n_row
- 1)->index
= -1;
827 struct isl_tab
*isl_tab_mark_empty(struct isl_tab
*tab
)
829 if (!tab
->empty
&& tab
->need_undo
)
830 isl_tab_push(tab
, isl_tab_undo_empty
);
835 /* Update the rows signs after a pivot of "row" and "col", with "row_sgn"
836 * the original sign of the pivot element.
837 * We only keep track of row signs during PILP solving and in this case
838 * we only pivot a row with negative sign (meaning the value is always
839 * non-positive) using a positive pivot element.
841 * For each row j, the new value of the parametric constant is equal to
843 * a_j0 - a_jc a_r0/a_rc
845 * where a_j0 is the original parametric constant, a_rc is the pivot element,
846 * a_r0 is the parametric constant of the pivot row and a_jc is the
847 * pivot column entry of the row j.
848 * Since a_r0 is non-positive and a_rc is positive, the sign of row j
849 * remains the same if a_jc has the same sign as the row j or if
850 * a_jc is zero. In all other cases, we reset the sign to "unknown".
852 static void update_row_sign(struct isl_tab
*tab
, int row
, int col
, int row_sgn
)
855 struct isl_mat
*mat
= tab
->mat
;
856 unsigned off
= 2 + tab
->M
;
861 if (tab
->row_sign
[row
] == 0)
863 isl_assert(mat
->ctx
, row_sgn
> 0, return);
864 isl_assert(mat
->ctx
, tab
->row_sign
[row
] == isl_tab_row_neg
, return);
865 tab
->row_sign
[row
] = isl_tab_row_pos
;
866 for (i
= 0; i
< tab
->n_row
; ++i
) {
870 s
= isl_int_sgn(mat
->row
[i
][off
+ col
]);
873 if (!tab
->row_sign
[i
])
875 if (s
< 0 && tab
->row_sign
[i
] == isl_tab_row_neg
)
877 if (s
> 0 && tab
->row_sign
[i
] == isl_tab_row_pos
)
879 tab
->row_sign
[i
] = isl_tab_row_unknown
;
883 /* Given a row number "row" and a column number "col", pivot the tableau
884 * such that the associated variables are interchanged.
885 * The given row in the tableau expresses
887 * x_r = a_r0 + \sum_i a_ri x_i
891 * x_c = 1/a_rc x_r - a_r0/a_rc + sum_{i \ne r} -a_ri/a_rc
893 * Substituting this equality into the other rows
895 * x_j = a_j0 + \sum_i a_ji x_i
897 * with a_jc \ne 0, we obtain
899 * 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
906 * where i is any other column and j is any other row,
907 * is therefore transformed into
909 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
910 * 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)
912 * The transformation is performed along the following steps
917 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
920 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
921 * n_jc/(|n_rc| d_j) n_ji/(|n_rc| d_j)
923 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
924 * n_jc/(|n_rc| d_j) (n_ji |n_rc|)/(|n_rc| d_j)
926 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
927 * n_jc/(|n_rc| d_j) (n_ji |n_rc| - s(n_rc)n_jc n_ri)/(|n_rc| d_j)
929 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
930 * 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)
933 void isl_tab_pivot(struct isl_tab
*tab
, int row
, int col
)
938 struct isl_mat
*mat
= tab
->mat
;
939 struct isl_tab_var
*var
;
940 unsigned off
= 2 + tab
->M
;
942 isl_int_swap(mat
->row
[row
][0], mat
->row
[row
][off
+ col
]);
943 sgn
= isl_int_sgn(mat
->row
[row
][0]);
945 isl_int_neg(mat
->row
[row
][0], mat
->row
[row
][0]);
946 isl_int_neg(mat
->row
[row
][off
+ col
], mat
->row
[row
][off
+ col
]);
948 for (j
= 0; j
< off
- 1 + tab
->n_col
; ++j
) {
949 if (j
== off
- 1 + col
)
951 isl_int_neg(mat
->row
[row
][1 + j
], mat
->row
[row
][1 + j
]);
953 if (!isl_int_is_one(mat
->row
[row
][0]))
954 isl_seq_normalize(mat
->ctx
, mat
->row
[row
], off
+ tab
->n_col
);
955 for (i
= 0; i
< tab
->n_row
; ++i
) {
958 if (isl_int_is_zero(mat
->row
[i
][off
+ col
]))
960 isl_int_mul(mat
->row
[i
][0], mat
->row
[i
][0], mat
->row
[row
][0]);
961 for (j
= 0; j
< off
- 1 + tab
->n_col
; ++j
) {
962 if (j
== off
- 1 + col
)
964 isl_int_mul(mat
->row
[i
][1 + j
],
965 mat
->row
[i
][1 + j
], mat
->row
[row
][0]);
966 isl_int_addmul(mat
->row
[i
][1 + j
],
967 mat
->row
[i
][off
+ col
], mat
->row
[row
][1 + j
]);
969 isl_int_mul(mat
->row
[i
][off
+ col
],
970 mat
->row
[i
][off
+ col
], mat
->row
[row
][off
+ col
]);
971 if (!isl_int_is_one(mat
->row
[i
][0]))
972 isl_seq_normalize(mat
->ctx
, mat
->row
[i
], off
+ tab
->n_col
);
974 t
= tab
->row_var
[row
];
975 tab
->row_var
[row
] = tab
->col_var
[col
];
976 tab
->col_var
[col
] = t
;
977 var
= isl_tab_var_from_row(tab
, row
);
980 var
= var_from_col(tab
, col
);
983 update_row_sign(tab
, row
, col
, sgn
);
986 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
987 if (isl_int_is_zero(mat
->row
[i
][off
+ col
]))
989 if (!isl_tab_var_from_row(tab
, i
)->frozen
&&
990 isl_tab_row_is_redundant(tab
, i
))
991 if (isl_tab_mark_redundant(tab
, i
))
996 /* If "var" represents a column variable, then pivot is up (sgn > 0)
997 * or down (sgn < 0) to a row. The variable is assumed not to be
998 * unbounded in the specified direction.
999 * If sgn = 0, then the variable is unbounded in both directions,
1000 * and we pivot with any row we can find.
1002 static void to_row(struct isl_tab
*tab
, struct isl_tab_var
*var
, int sign
)
1005 unsigned off
= 2 + tab
->M
;
1011 for (r
= tab
->n_redundant
; r
< tab
->n_row
; ++r
)
1012 if (!isl_int_is_zero(tab
->mat
->row
[r
][off
+var
->index
]))
1014 isl_assert(tab
->mat
->ctx
, r
< tab
->n_row
, return);
1016 r
= pivot_row(tab
, NULL
, sign
, var
->index
);
1017 isl_assert(tab
->mat
->ctx
, r
>= 0, return);
1020 isl_tab_pivot(tab
, r
, var
->index
);
1023 static void check_table(struct isl_tab
*tab
)
1029 for (i
= 0; i
< tab
->n_row
; ++i
) {
1030 if (!isl_tab_var_from_row(tab
, i
)->is_nonneg
)
1032 assert(!isl_int_is_neg(tab
->mat
->row
[i
][1]));
1036 /* Return the sign of the maximal value of "var".
1037 * If the sign is not negative, then on return from this function,
1038 * the sample value will also be non-negative.
1040 * If "var" is manifestly unbounded wrt positive values, we are done.
1041 * Otherwise, we pivot the variable up to a row if needed
1042 * Then we continue pivoting down until either
1043 * - no more down pivots can be performed
1044 * - the sample value is positive
1045 * - the variable is pivoted into a manifestly unbounded column
1047 static int sign_of_max(struct isl_tab
*tab
, struct isl_tab_var
*var
)
1051 if (max_is_manifestly_unbounded(tab
, var
))
1053 to_row(tab
, var
, 1);
1054 while (!isl_int_is_pos(tab
->mat
->row
[var
->index
][1])) {
1055 find_pivot(tab
, var
, var
, 1, &row
, &col
);
1057 return isl_int_sgn(tab
->mat
->row
[var
->index
][1]);
1058 isl_tab_pivot(tab
, row
, col
);
1059 if (!var
->is_row
) /* manifestly unbounded */
1065 static int row_is_neg(struct isl_tab
*tab
, int row
)
1068 return isl_int_is_neg(tab
->mat
->row
[row
][1]);
1069 if (isl_int_is_pos(tab
->mat
->row
[row
][2]))
1071 if (isl_int_is_neg(tab
->mat
->row
[row
][2]))
1073 return isl_int_is_neg(tab
->mat
->row
[row
][1]);
1076 static int row_sgn(struct isl_tab
*tab
, int row
)
1079 return isl_int_sgn(tab
->mat
->row
[row
][1]);
1080 if (!isl_int_is_zero(tab
->mat
->row
[row
][2]))
1081 return isl_int_sgn(tab
->mat
->row
[row
][2]);
1083 return isl_int_sgn(tab
->mat
->row
[row
][1]);
1086 /* Perform pivots until the row variable "var" has a non-negative
1087 * sample value or until no more upward pivots can be performed.
1088 * Return the sign of the sample value after the pivots have been
1091 static int restore_row(struct isl_tab
*tab
, struct isl_tab_var
*var
)
1095 while (row_is_neg(tab
, var
->index
)) {
1096 find_pivot(tab
, var
, var
, 1, &row
, &col
);
1099 isl_tab_pivot(tab
, row
, col
);
1100 if (!var
->is_row
) /* manifestly unbounded */
1103 return row_sgn(tab
, var
->index
);
1106 /* Perform pivots until we are sure that the row variable "var"
1107 * can attain non-negative values. After return from this
1108 * function, "var" is still a row variable, but its sample
1109 * value may not be non-negative, even if the function returns 1.
1111 static int at_least_zero(struct isl_tab
*tab
, struct isl_tab_var
*var
)
1115 while (isl_int_is_neg(tab
->mat
->row
[var
->index
][1])) {
1116 find_pivot(tab
, var
, var
, 1, &row
, &col
);
1119 if (row
== var
->index
) /* manifestly unbounded */
1121 isl_tab_pivot(tab
, row
, col
);
1123 return !isl_int_is_neg(tab
->mat
->row
[var
->index
][1]);
1126 /* Return a negative value if "var" can attain negative values.
1127 * Return a non-negative value otherwise.
1129 * If "var" is manifestly unbounded wrt negative values, we are done.
1130 * Otherwise, if var is in a column, we can pivot it down to a row.
1131 * Then we continue pivoting down until either
1132 * - the pivot would result in a manifestly unbounded column
1133 * => we don't perform the pivot, but simply return -1
1134 * - no more down pivots can be performed
1135 * - the sample value is negative
1136 * If the sample value becomes negative and the variable is supposed
1137 * to be nonnegative, then we undo the last pivot.
1138 * However, if the last pivot has made the pivoting variable
1139 * obviously redundant, then it may have moved to another row.
1140 * In that case we look for upward pivots until we reach a non-negative
1143 static int sign_of_min(struct isl_tab
*tab
, struct isl_tab_var
*var
)
1146 struct isl_tab_var
*pivot_var
= NULL
;
1148 if (min_is_manifestly_unbounded(tab
, var
))
1152 row
= pivot_row(tab
, NULL
, -1, col
);
1153 pivot_var
= var_from_col(tab
, col
);
1154 isl_tab_pivot(tab
, row
, col
);
1155 if (var
->is_redundant
)
1157 if (isl_int_is_neg(tab
->mat
->row
[var
->index
][1])) {
1158 if (var
->is_nonneg
) {
1159 if (!pivot_var
->is_redundant
&&
1160 pivot_var
->index
== row
)
1161 isl_tab_pivot(tab
, row
, col
);
1163 restore_row(tab
, var
);
1168 if (var
->is_redundant
)
1170 while (!isl_int_is_neg(tab
->mat
->row
[var
->index
][1])) {
1171 find_pivot(tab
, var
, var
, -1, &row
, &col
);
1172 if (row
== var
->index
)
1175 return isl_int_sgn(tab
->mat
->row
[var
->index
][1]);
1176 pivot_var
= var_from_col(tab
, col
);
1177 isl_tab_pivot(tab
, row
, col
);
1178 if (var
->is_redundant
)
1181 if (pivot_var
&& var
->is_nonneg
) {
1182 /* pivot back to non-negative value */
1183 if (!pivot_var
->is_redundant
&& pivot_var
->index
== row
)
1184 isl_tab_pivot(tab
, row
, col
);
1186 restore_row(tab
, var
);
1191 static int row_at_most_neg_one(struct isl_tab
*tab
, int row
)
1194 if (isl_int_is_pos(tab
->mat
->row
[row
][2]))
1196 if (isl_int_is_neg(tab
->mat
->row
[row
][2]))
1199 return isl_int_is_neg(tab
->mat
->row
[row
][1]) &&
1200 isl_int_abs_ge(tab
->mat
->row
[row
][1],
1201 tab
->mat
->row
[row
][0]);
1204 /* Return 1 if "var" can attain values <= -1.
1205 * Return 0 otherwise.
1207 * The sample value of "var" is assumed to be non-negative when the
1208 * the function is called and will be made non-negative again before
1209 * the function returns.
1211 int isl_tab_min_at_most_neg_one(struct isl_tab
*tab
, struct isl_tab_var
*var
)
1214 struct isl_tab_var
*pivot_var
;
1216 if (min_is_manifestly_unbounded(tab
, var
))
1220 row
= pivot_row(tab
, NULL
, -1, col
);
1221 pivot_var
= var_from_col(tab
, col
);
1222 isl_tab_pivot(tab
, row
, col
);
1223 if (var
->is_redundant
)
1225 if (row_at_most_neg_one(tab
, var
->index
)) {
1226 if (var
->is_nonneg
) {
1227 if (!pivot_var
->is_redundant
&&
1228 pivot_var
->index
== row
)
1229 isl_tab_pivot(tab
, row
, col
);
1231 restore_row(tab
, var
);
1236 if (var
->is_redundant
)
1239 find_pivot(tab
, var
, var
, -1, &row
, &col
);
1240 if (row
== var
->index
)
1244 pivot_var
= var_from_col(tab
, col
);
1245 isl_tab_pivot(tab
, row
, col
);
1246 if (var
->is_redundant
)
1248 } while (!row_at_most_neg_one(tab
, var
->index
));
1249 if (var
->is_nonneg
) {
1250 /* pivot back to non-negative value */
1251 if (!pivot_var
->is_redundant
&& pivot_var
->index
== row
)
1252 isl_tab_pivot(tab
, row
, col
);
1253 restore_row(tab
, var
);
1258 /* Return 1 if "var" can attain values >= 1.
1259 * Return 0 otherwise.
1261 static int at_least_one(struct isl_tab
*tab
, struct isl_tab_var
*var
)
1266 if (max_is_manifestly_unbounded(tab
, var
))
1268 to_row(tab
, var
, 1);
1269 r
= tab
->mat
->row
[var
->index
];
1270 while (isl_int_lt(r
[1], r
[0])) {
1271 find_pivot(tab
, var
, var
, 1, &row
, &col
);
1273 return isl_int_ge(r
[1], r
[0]);
1274 if (row
== var
->index
) /* manifestly unbounded */
1276 isl_tab_pivot(tab
, row
, col
);
1281 static void swap_cols(struct isl_tab
*tab
, int col1
, int col2
)
1284 unsigned off
= 2 + tab
->M
;
1285 t
= tab
->col_var
[col1
];
1286 tab
->col_var
[col1
] = tab
->col_var
[col2
];
1287 tab
->col_var
[col2
] = t
;
1288 var_from_col(tab
, col1
)->index
= col1
;
1289 var_from_col(tab
, col2
)->index
= col2
;
1290 tab
->mat
= isl_mat_swap_cols(tab
->mat
, off
+ col1
, off
+ col2
);
1293 /* Mark column with index "col" as representing a zero variable.
1294 * If we may need to undo the operation the column is kept,
1295 * but no longer considered.
1296 * Otherwise, the column is simply removed.
1298 * The column may be interchanged with some other column. If it
1299 * is interchanged with a later column, return 1. Otherwise return 0.
1300 * If the columns are checked in order in the calling function,
1301 * then a return value of 1 means that the column with the given
1302 * column number may now contain a different column that
1303 * hasn't been checked yet.
1305 int isl_tab_kill_col(struct isl_tab
*tab
, int col
)
1307 var_from_col(tab
, col
)->is_zero
= 1;
1308 if (tab
->need_undo
) {
1309 isl_tab_push_var(tab
, isl_tab_undo_zero
, var_from_col(tab
, col
));
1310 if (col
!= tab
->n_dead
)
1311 swap_cols(tab
, col
, tab
->n_dead
);
1315 if (col
!= tab
->n_col
- 1)
1316 swap_cols(tab
, col
, tab
->n_col
- 1);
1317 var_from_col(tab
, tab
->n_col
- 1)->index
= -1;
1323 /* Row variable "var" is non-negative and cannot attain any values
1324 * larger than zero. This means that the coefficients of the unrestricted
1325 * column variables are zero and that the coefficients of the non-negative
1326 * column variables are zero or negative.
1327 * Each of the non-negative variables with a negative coefficient can
1328 * then also be written as the negative sum of non-negative variables
1329 * and must therefore also be zero.
1331 static void close_row(struct isl_tab
*tab
, struct isl_tab_var
*var
)
1334 struct isl_mat
*mat
= tab
->mat
;
1335 unsigned off
= 2 + tab
->M
;
1337 isl_assert(tab
->mat
->ctx
, var
->is_nonneg
, return);
1340 isl_tab_push_var(tab
, isl_tab_undo_zero
, var
);
1341 for (j
= tab
->n_dead
; j
< tab
->n_col
; ++j
) {
1342 if (isl_int_is_zero(mat
->row
[var
->index
][off
+ j
]))
1344 isl_assert(tab
->mat
->ctx
,
1345 isl_int_is_neg(mat
->row
[var
->index
][off
+ j
]), return);
1346 if (isl_tab_kill_col(tab
, j
))
1349 isl_tab_mark_redundant(tab
, var
->index
);
1352 /* Add a constraint to the tableau and allocate a row for it.
1353 * Return the index into the constraint array "con".
1355 int isl_tab_allocate_con(struct isl_tab
*tab
)
1359 isl_assert(tab
->mat
->ctx
, tab
->n_row
< tab
->mat
->n_row
, return -1);
1360 isl_assert(tab
->mat
->ctx
, tab
->n_con
< tab
->max_con
, return -1);
1363 tab
->con
[r
].index
= tab
->n_row
;
1364 tab
->con
[r
].is_row
= 1;
1365 tab
->con
[r
].is_nonneg
= 0;
1366 tab
->con
[r
].is_zero
= 0;
1367 tab
->con
[r
].is_redundant
= 0;
1368 tab
->con
[r
].frozen
= 0;
1369 tab
->con
[r
].negated
= 0;
1370 tab
->row_var
[tab
->n_row
] = ~r
;
1374 isl_tab_push_var(tab
, isl_tab_undo_allocate
, &tab
->con
[r
]);
1379 /* Add a variable to the tableau and allocate a column for it.
1380 * Return the index into the variable array "var".
1382 int isl_tab_allocate_var(struct isl_tab
*tab
)
1386 unsigned off
= 2 + tab
->M
;
1388 isl_assert(tab
->mat
->ctx
, tab
->n_col
< tab
->mat
->n_col
, return -1);
1389 isl_assert(tab
->mat
->ctx
, tab
->n_var
< tab
->max_var
, return -1);
1392 tab
->var
[r
].index
= tab
->n_col
;
1393 tab
->var
[r
].is_row
= 0;
1394 tab
->var
[r
].is_nonneg
= 0;
1395 tab
->var
[r
].is_zero
= 0;
1396 tab
->var
[r
].is_redundant
= 0;
1397 tab
->var
[r
].frozen
= 0;
1398 tab
->var
[r
].negated
= 0;
1399 tab
->col_var
[tab
->n_col
] = r
;
1401 for (i
= 0; i
< tab
->n_row
; ++i
)
1402 isl_int_set_si(tab
->mat
->row
[i
][off
+ tab
->n_col
], 0);
1406 isl_tab_push_var(tab
, isl_tab_undo_allocate
, &tab
->var
[r
]);
1411 /* Add a row to the tableau. The row is given as an affine combination
1412 * of the original variables and needs to be expressed in terms of the
1415 * We add each term in turn.
1416 * If r = n/d_r is the current sum and we need to add k x, then
1417 * if x is a column variable, we increase the numerator of
1418 * this column by k d_r
1419 * if x = f/d_x is a row variable, then the new representation of r is
1421 * n k f d_x/g n + d_r/g k f m/d_r n + m/d_g k f
1422 * --- + --- = ------------------- = -------------------
1423 * d_r d_r d_r d_x/g m
1425 * with g the gcd of d_r and d_x and m the lcm of d_r and d_x.
1427 int isl_tab_add_row(struct isl_tab
*tab
, isl_int
*line
)
1433 unsigned off
= 2 + tab
->M
;
1435 r
= isl_tab_allocate_con(tab
);
1441 row
= tab
->mat
->row
[tab
->con
[r
].index
];
1442 isl_int_set_si(row
[0], 1);
1443 isl_int_set(row
[1], line
[0]);
1444 isl_seq_clr(row
+ 2, tab
->M
+ tab
->n_col
);
1445 for (i
= 0; i
< tab
->n_var
; ++i
) {
1446 if (tab
->var
[i
].is_zero
)
1448 if (tab
->var
[i
].is_row
) {
1450 row
[0], tab
->mat
->row
[tab
->var
[i
].index
][0]);
1451 isl_int_swap(a
, row
[0]);
1452 isl_int_divexact(a
, row
[0], a
);
1454 row
[0], tab
->mat
->row
[tab
->var
[i
].index
][0]);
1455 isl_int_mul(b
, b
, line
[1 + i
]);
1456 isl_seq_combine(row
+ 1, a
, row
+ 1,
1457 b
, tab
->mat
->row
[tab
->var
[i
].index
] + 1,
1458 1 + tab
->M
+ tab
->n_col
);
1460 isl_int_addmul(row
[off
+ tab
->var
[i
].index
],
1461 line
[1 + i
], row
[0]);
1462 if (tab
->M
&& i
>= tab
->n_param
&& i
< tab
->n_var
- tab
->n_div
)
1463 isl_int_submul(row
[2], line
[1 + i
], row
[0]);
1465 isl_seq_normalize(tab
->mat
->ctx
, row
, off
+ tab
->n_col
);
1470 tab
->row_sign
[tab
->con
[r
].index
] = 0;
1475 static int drop_row(struct isl_tab
*tab
, int row
)
1477 isl_assert(tab
->mat
->ctx
, ~tab
->row_var
[row
] == tab
->n_con
- 1, return -1);
1478 if (row
!= tab
->n_row
- 1)
1479 swap_rows(tab
, row
, tab
->n_row
- 1);
1485 static int drop_col(struct isl_tab
*tab
, int col
)
1487 isl_assert(tab
->mat
->ctx
, tab
->col_var
[col
] == tab
->n_var
- 1, return -1);
1488 if (col
!= tab
->n_col
- 1)
1489 swap_cols(tab
, col
, tab
->n_col
- 1);
1495 /* Add inequality "ineq" and check if it conflicts with the
1496 * previously added constraints or if it is obviously redundant.
1498 struct isl_tab
*isl_tab_add_ineq(struct isl_tab
*tab
, isl_int
*ineq
)
1505 r
= isl_tab_add_row(tab
, ineq
);
1508 tab
->con
[r
].is_nonneg
= 1;
1509 isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]);
1510 if (isl_tab_row_is_redundant(tab
, tab
->con
[r
].index
)) {
1511 isl_tab_mark_redundant(tab
, tab
->con
[r
].index
);
1515 sgn
= restore_row(tab
, &tab
->con
[r
]);
1517 return isl_tab_mark_empty(tab
);
1518 if (tab
->con
[r
].is_row
&& isl_tab_row_is_redundant(tab
, tab
->con
[r
].index
))
1519 isl_tab_mark_redundant(tab
, tab
->con
[r
].index
);
1526 /* Pivot a non-negative variable down until it reaches the value zero
1527 * and then pivot the variable into a column position.
1529 static int to_col(struct isl_tab
*tab
, struct isl_tab_var
*var
)
1533 unsigned off
= 2 + tab
->M
;
1538 while (isl_int_is_pos(tab
->mat
->row
[var
->index
][1])) {
1539 find_pivot(tab
, var
, NULL
, -1, &row
, &col
);
1540 isl_assert(tab
->mat
->ctx
, row
!= -1, return -1);
1541 isl_tab_pivot(tab
, row
, col
);
1546 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
)
1547 if (!isl_int_is_zero(tab
->mat
->row
[var
->index
][off
+ i
]))
1550 isl_assert(tab
->mat
->ctx
, i
< tab
->n_col
, return -1);
1551 isl_tab_pivot(tab
, var
->index
, i
);
1556 /* We assume Gaussian elimination has been performed on the equalities.
1557 * The equalities can therefore never conflict.
1558 * Adding the equalities is currently only really useful for a later call
1559 * to isl_tab_ineq_type.
1561 static struct isl_tab
*add_eq(struct isl_tab
*tab
, isl_int
*eq
)
1568 r
= isl_tab_add_row(tab
, eq
);
1572 r
= tab
->con
[r
].index
;
1573 i
= isl_seq_first_non_zero(tab
->mat
->row
[r
] + 2 + tab
->M
+ tab
->n_dead
,
1574 tab
->n_col
- tab
->n_dead
);
1575 isl_assert(tab
->mat
->ctx
, i
>= 0, goto error
);
1577 isl_tab_pivot(tab
, r
, i
);
1578 isl_tab_kill_col(tab
, i
);
1587 static int row_is_manifestly_zero(struct isl_tab
*tab
, int row
)
1589 unsigned off
= 2 + tab
->M
;
1591 if (!isl_int_is_zero(tab
->mat
->row
[row
][1]))
1593 if (tab
->M
&& !isl_int_is_zero(tab
->mat
->row
[row
][2]))
1595 return isl_seq_first_non_zero(tab
->mat
->row
[row
] + off
+ tab
->n_dead
,
1596 tab
->n_col
- tab
->n_dead
) == -1;
1599 /* Add an equality that is known to be valid for the given tableau.
1601 struct isl_tab
*isl_tab_add_valid_eq(struct isl_tab
*tab
, isl_int
*eq
)
1603 struct isl_tab_var
*var
;
1608 r
= isl_tab_add_row(tab
, eq
);
1614 if (row_is_manifestly_zero(tab
, r
)) {
1616 isl_tab_mark_redundant(tab
, r
);
1620 if (isl_int_is_neg(tab
->mat
->row
[r
][1])) {
1621 isl_seq_neg(tab
->mat
->row
[r
] + 1, tab
->mat
->row
[r
] + 1,
1626 if (to_col(tab
, var
) < 0)
1629 isl_tab_kill_col(tab
, var
->index
);
1637 /* Add equality "eq" and check if it conflicts with the
1638 * previously added constraints or if it is obviously redundant.
1640 struct isl_tab
*isl_tab_add_eq(struct isl_tab
*tab
, isl_int
*eq
)
1642 struct isl_tab_undo
*snap
= NULL
;
1643 struct isl_tab_var
*var
;
1650 isl_assert(tab
->mat
->ctx
, !tab
->M
, goto error
);
1653 snap
= isl_tab_snap(tab
);
1655 r
= isl_tab_add_row(tab
, eq
);
1661 if (row_is_manifestly_zero(tab
, row
)) {
1663 if (isl_tab_rollback(tab
, snap
) < 0)
1670 sgn
= isl_int_sgn(tab
->mat
->row
[row
][1]);
1673 isl_seq_neg(tab
->mat
->row
[row
] + 1, tab
->mat
->row
[row
] + 1,
1679 if (sgn
< 0 && sign_of_max(tab
, var
) < 0)
1680 return isl_tab_mark_empty(tab
);
1683 if (to_col(tab
, var
) < 0)
1686 isl_tab_kill_col(tab
, var
->index
);
1694 struct isl_tab
*isl_tab_from_basic_map(struct isl_basic_map
*bmap
)
1697 struct isl_tab
*tab
;
1701 tab
= isl_tab_alloc(bmap
->ctx
,
1702 isl_basic_map_total_dim(bmap
) + bmap
->n_ineq
+ 1,
1703 isl_basic_map_total_dim(bmap
), 0);
1706 tab
->rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
1707 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
1708 return isl_tab_mark_empty(tab
);
1709 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1710 tab
= add_eq(tab
, bmap
->eq
[i
]);
1714 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
1715 tab
= isl_tab_add_ineq(tab
, bmap
->ineq
[i
]);
1716 if (!tab
|| tab
->empty
)
1722 struct isl_tab
*isl_tab_from_basic_set(struct isl_basic_set
*bset
)
1724 return isl_tab_from_basic_map((struct isl_basic_map
*)bset
);
1727 /* Construct a tableau corresponding to the recession cone of "bset".
1729 struct isl_tab
*isl_tab_from_recession_cone(struct isl_basic_set
*bset
)
1733 struct isl_tab
*tab
;
1737 tab
= isl_tab_alloc(bset
->ctx
, bset
->n_eq
+ bset
->n_ineq
,
1738 isl_basic_set_total_dim(bset
), 0);
1741 tab
->rational
= ISL_F_ISSET(bset
, ISL_BASIC_SET_RATIONAL
);
1744 for (i
= 0; i
< bset
->n_eq
; ++i
) {
1745 isl_int_swap(bset
->eq
[i
][0], cst
);
1746 tab
= add_eq(tab
, bset
->eq
[i
]);
1747 isl_int_swap(bset
->eq
[i
][0], cst
);
1751 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
1753 isl_int_swap(bset
->ineq
[i
][0], cst
);
1754 r
= isl_tab_add_row(tab
, bset
->ineq
[i
]);
1755 isl_int_swap(bset
->ineq
[i
][0], cst
);
1758 tab
->con
[r
].is_nonneg
= 1;
1759 isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]);
1770 /* Assuming "tab" is the tableau of a cone, check if the cone is
1771 * bounded, i.e., if it is empty or only contains the origin.
1773 int isl_tab_cone_is_bounded(struct isl_tab
*tab
)
1781 if (tab
->n_dead
== tab
->n_col
)
1785 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
1786 struct isl_tab_var
*var
;
1787 var
= isl_tab_var_from_row(tab
, i
);
1788 if (!var
->is_nonneg
)
1790 if (sign_of_max(tab
, var
) != 0)
1792 close_row(tab
, var
);
1795 if (tab
->n_dead
== tab
->n_col
)
1797 if (i
== tab
->n_row
)
1802 int isl_tab_sample_is_integer(struct isl_tab
*tab
)
1809 for (i
= 0; i
< tab
->n_var
; ++i
) {
1811 if (!tab
->var
[i
].is_row
)
1813 row
= tab
->var
[i
].index
;
1814 if (!isl_int_is_divisible_by(tab
->mat
->row
[row
][1],
1815 tab
->mat
->row
[row
][0]))
1821 static struct isl_vec
*extract_integer_sample(struct isl_tab
*tab
)
1824 struct isl_vec
*vec
;
1826 vec
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_var
);
1830 isl_int_set_si(vec
->block
.data
[0], 1);
1831 for (i
= 0; i
< tab
->n_var
; ++i
) {
1832 if (!tab
->var
[i
].is_row
)
1833 isl_int_set_si(vec
->block
.data
[1 + i
], 0);
1835 int row
= tab
->var
[i
].index
;
1836 isl_int_divexact(vec
->block
.data
[1 + i
],
1837 tab
->mat
->row
[row
][1], tab
->mat
->row
[row
][0]);
1844 struct isl_vec
*isl_tab_get_sample_value(struct isl_tab
*tab
)
1847 struct isl_vec
*vec
;
1853 vec
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_var
);
1859 isl_int_set_si(vec
->block
.data
[0], 1);
1860 for (i
= 0; i
< tab
->n_var
; ++i
) {
1862 if (!tab
->var
[i
].is_row
) {
1863 isl_int_set_si(vec
->block
.data
[1 + i
], 0);
1866 row
= tab
->var
[i
].index
;
1867 isl_int_gcd(m
, vec
->block
.data
[0], tab
->mat
->row
[row
][0]);
1868 isl_int_divexact(m
, tab
->mat
->row
[row
][0], m
);
1869 isl_seq_scale(vec
->block
.data
, vec
->block
.data
, m
, 1 + i
);
1870 isl_int_divexact(m
, vec
->block
.data
[0], tab
->mat
->row
[row
][0]);
1871 isl_int_mul(vec
->block
.data
[1 + i
], m
, tab
->mat
->row
[row
][1]);
1873 vec
= isl_vec_normalize(vec
);
1879 /* Update "bmap" based on the results of the tableau "tab".
1880 * In particular, implicit equalities are made explicit, redundant constraints
1881 * are removed and if the sample value happens to be integer, it is stored
1882 * in "bmap" (unless "bmap" already had an integer sample).
1884 * The tableau is assumed to have been created from "bmap" using
1885 * isl_tab_from_basic_map.
1887 struct isl_basic_map
*isl_basic_map_update_from_tab(struct isl_basic_map
*bmap
,
1888 struct isl_tab
*tab
)
1900 bmap
= isl_basic_map_set_to_empty(bmap
);
1902 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
1903 if (isl_tab_is_equality(tab
, n_eq
+ i
))
1904 isl_basic_map_inequality_to_equality(bmap
, i
);
1905 else if (isl_tab_is_redundant(tab
, n_eq
+ i
))
1906 isl_basic_map_drop_inequality(bmap
, i
);
1908 if (!tab
->rational
&&
1909 !bmap
->sample
&& isl_tab_sample_is_integer(tab
))
1910 bmap
->sample
= extract_integer_sample(tab
);
1914 struct isl_basic_set
*isl_basic_set_update_from_tab(struct isl_basic_set
*bset
,
1915 struct isl_tab
*tab
)
1917 return (struct isl_basic_set
*)isl_basic_map_update_from_tab(
1918 (struct isl_basic_map
*)bset
, tab
);
1921 /* Given a non-negative variable "var", add a new non-negative variable
1922 * that is the opposite of "var", ensuring that var can only attain the
1924 * If var = n/d is a row variable, then the new variable = -n/d.
1925 * If var is a column variables, then the new variable = -var.
1926 * If the new variable cannot attain non-negative values, then
1927 * the resulting tableau is empty.
1928 * Otherwise, we know the value will be zero and we close the row.
1930 static struct isl_tab
*cut_to_hyperplane(struct isl_tab
*tab
,
1931 struct isl_tab_var
*var
)
1936 unsigned off
= 2 + tab
->M
;
1940 isl_assert(tab
->mat
->ctx
, !var
->is_redundant
, goto error
);
1942 if (isl_tab_extend_cons(tab
, 1) < 0)
1946 tab
->con
[r
].index
= tab
->n_row
;
1947 tab
->con
[r
].is_row
= 1;
1948 tab
->con
[r
].is_nonneg
= 0;
1949 tab
->con
[r
].is_zero
= 0;
1950 tab
->con
[r
].is_redundant
= 0;
1951 tab
->con
[r
].frozen
= 0;
1952 tab
->con
[r
].negated
= 0;
1953 tab
->row_var
[tab
->n_row
] = ~r
;
1954 row
= tab
->mat
->row
[tab
->n_row
];
1957 isl_int_set(row
[0], tab
->mat
->row
[var
->index
][0]);
1958 isl_seq_neg(row
+ 1,
1959 tab
->mat
->row
[var
->index
] + 1, 1 + tab
->n_col
);
1961 isl_int_set_si(row
[0], 1);
1962 isl_seq_clr(row
+ 1, 1 + tab
->n_col
);
1963 isl_int_set_si(row
[off
+ var
->index
], -1);
1968 isl_tab_push_var(tab
, isl_tab_undo_allocate
, &tab
->con
[r
]);
1970 sgn
= sign_of_max(tab
, &tab
->con
[r
]);
1972 return isl_tab_mark_empty(tab
);
1973 tab
->con
[r
].is_nonneg
= 1;
1974 isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]);
1976 close_row(tab
, &tab
->con
[r
]);
1984 /* Given a tableau "tab" and an inequality constraint "con" of the tableau,
1985 * relax the inequality by one. That is, the inequality r >= 0 is replaced
1986 * by r' = r + 1 >= 0.
1987 * If r is a row variable, we simply increase the constant term by one
1988 * (taking into account the denominator).
1989 * If r is a column variable, then we need to modify each row that
1990 * refers to r = r' - 1 by substituting this equality, effectively
1991 * subtracting the coefficient of the column from the constant.
1993 struct isl_tab
*isl_tab_relax(struct isl_tab
*tab
, int con
)
1995 struct isl_tab_var
*var
;
1996 unsigned off
= 2 + tab
->M
;
2001 var
= &tab
->con
[con
];
2003 if (!var
->is_row
&& !max_is_manifestly_unbounded(tab
, var
))
2004 to_row(tab
, var
, 1);
2007 isl_int_add(tab
->mat
->row
[var
->index
][1],
2008 tab
->mat
->row
[var
->index
][1], tab
->mat
->row
[var
->index
][0]);
2012 for (i
= 0; i
< tab
->n_row
; ++i
) {
2013 if (isl_int_is_zero(tab
->mat
->row
[i
][off
+ var
->index
]))
2015 isl_int_sub(tab
->mat
->row
[i
][1], tab
->mat
->row
[i
][1],
2016 tab
->mat
->row
[i
][off
+ var
->index
]);
2021 isl_tab_push_var(tab
, isl_tab_undo_relax
, var
);
2026 struct isl_tab
*isl_tab_select_facet(struct isl_tab
*tab
, int con
)
2031 return cut_to_hyperplane(tab
, &tab
->con
[con
]);
2034 static int may_be_equality(struct isl_tab
*tab
, int row
)
2036 unsigned off
= 2 + tab
->M
;
2037 return (tab
->rational
? isl_int_is_zero(tab
->mat
->row
[row
][1])
2038 : isl_int_lt(tab
->mat
->row
[row
][1],
2039 tab
->mat
->row
[row
][0])) &&
2040 isl_seq_first_non_zero(tab
->mat
->row
[row
] + off
+ tab
->n_dead
,
2041 tab
->n_col
- tab
->n_dead
) != -1;
2044 /* Check for (near) equalities among the constraints.
2045 * A constraint is an equality if it is non-negative and if
2046 * its maximal value is either
2047 * - zero (in case of rational tableaus), or
2048 * - strictly less than 1 (in case of integer tableaus)
2050 * We first mark all non-redundant and non-dead variables that
2051 * are not frozen and not obviously not an equality.
2052 * Then we iterate over all marked variables if they can attain
2053 * any values larger than zero or at least one.
2054 * If the maximal value is zero, we mark any column variables
2055 * that appear in the row as being zero and mark the row as being redundant.
2056 * Otherwise, if the maximal value is strictly less than one (and the
2057 * tableau is integer), then we restrict the value to being zero
2058 * by adding an opposite non-negative variable.
2060 struct isl_tab
*isl_tab_detect_implicit_equalities(struct isl_tab
*tab
)
2069 if (tab
->n_dead
== tab
->n_col
)
2073 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
2074 struct isl_tab_var
*var
= isl_tab_var_from_row(tab
, i
);
2075 var
->marked
= !var
->frozen
&& var
->is_nonneg
&&
2076 may_be_equality(tab
, i
);
2080 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
2081 struct isl_tab_var
*var
= var_from_col(tab
, i
);
2082 var
->marked
= !var
->frozen
&& var
->is_nonneg
;
2087 struct isl_tab_var
*var
;
2088 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
2089 var
= isl_tab_var_from_row(tab
, i
);
2093 if (i
== tab
->n_row
) {
2094 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
2095 var
= var_from_col(tab
, i
);
2099 if (i
== tab
->n_col
)
2104 if (sign_of_max(tab
, var
) == 0)
2105 close_row(tab
, var
);
2106 else if (!tab
->rational
&& !at_least_one(tab
, var
)) {
2107 tab
= cut_to_hyperplane(tab
, var
);
2108 return isl_tab_detect_implicit_equalities(tab
);
2110 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
2111 var
= isl_tab_var_from_row(tab
, i
);
2114 if (may_be_equality(tab
, i
))
2124 /* Check for (near) redundant constraints.
2125 * A constraint is redundant if it is non-negative and if
2126 * its minimal value (temporarily ignoring the non-negativity) is either
2127 * - zero (in case of rational tableaus), or
2128 * - strictly larger than -1 (in case of integer tableaus)
2130 * We first mark all non-redundant and non-dead variables that
2131 * are not frozen and not obviously negatively unbounded.
2132 * Then we iterate over all marked variables if they can attain
2133 * any values smaller than zero or at most negative one.
2134 * If not, we mark the row as being redundant (assuming it hasn't
2135 * been detected as being obviously redundant in the mean time).
2137 struct isl_tab
*isl_tab_detect_redundant(struct isl_tab
*tab
)
2146 if (tab
->n_redundant
== tab
->n_row
)
2150 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
2151 struct isl_tab_var
*var
= isl_tab_var_from_row(tab
, i
);
2152 var
->marked
= !var
->frozen
&& var
->is_nonneg
;
2156 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
2157 struct isl_tab_var
*var
= var_from_col(tab
, i
);
2158 var
->marked
= !var
->frozen
&& var
->is_nonneg
&&
2159 !min_is_manifestly_unbounded(tab
, var
);
2164 struct isl_tab_var
*var
;
2165 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
2166 var
= isl_tab_var_from_row(tab
, i
);
2170 if (i
== tab
->n_row
) {
2171 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
2172 var
= var_from_col(tab
, i
);
2176 if (i
== tab
->n_col
)
2181 if ((tab
->rational
? (sign_of_min(tab
, var
) >= 0)
2182 : !isl_tab_min_at_most_neg_one(tab
, var
)) &&
2184 isl_tab_mark_redundant(tab
, var
->index
);
2185 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
2186 var
= var_from_col(tab
, i
);
2189 if (!min_is_manifestly_unbounded(tab
, var
))
2199 int isl_tab_is_equality(struct isl_tab
*tab
, int con
)
2206 if (tab
->con
[con
].is_zero
)
2208 if (tab
->con
[con
].is_redundant
)
2210 if (!tab
->con
[con
].is_row
)
2211 return tab
->con
[con
].index
< tab
->n_dead
;
2213 row
= tab
->con
[con
].index
;
2216 return isl_int_is_zero(tab
->mat
->row
[row
][1]) &&
2217 isl_seq_first_non_zero(tab
->mat
->row
[row
] + 2 + tab
->n_dead
,
2218 tab
->n_col
- tab
->n_dead
) == -1;
2221 /* Return the minimial value of the affine expression "f" with denominator
2222 * "denom" in *opt, *opt_denom, assuming the tableau is not empty and
2223 * the expression cannot attain arbitrarily small values.
2224 * If opt_denom is NULL, then *opt is rounded up to the nearest integer.
2225 * The return value reflects the nature of the result (empty, unbounded,
2226 * minmimal value returned in *opt).
2228 enum isl_lp_result
isl_tab_min(struct isl_tab
*tab
,
2229 isl_int
*f
, isl_int denom
, isl_int
*opt
, isl_int
*opt_denom
,
2233 enum isl_lp_result res
= isl_lp_ok
;
2234 struct isl_tab_var
*var
;
2235 struct isl_tab_undo
*snap
;
2238 return isl_lp_empty
;
2240 snap
= isl_tab_snap(tab
);
2241 r
= isl_tab_add_row(tab
, f
);
2243 return isl_lp_error
;
2245 isl_int_mul(tab
->mat
->row
[var
->index
][0],
2246 tab
->mat
->row
[var
->index
][0], denom
);
2249 find_pivot(tab
, var
, var
, -1, &row
, &col
);
2250 if (row
== var
->index
) {
2251 res
= isl_lp_unbounded
;
2256 isl_tab_pivot(tab
, row
, col
);
2258 if (ISL_FL_ISSET(flags
, ISL_TAB_SAVE_DUAL
)) {
2261 isl_vec_free(tab
->dual
);
2262 tab
->dual
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_con
);
2264 return isl_lp_error
;
2265 isl_int_set(tab
->dual
->el
[0], tab
->mat
->row
[var
->index
][0]);
2266 for (i
= 0; i
< tab
->n_con
; ++i
) {
2268 if (tab
->con
[i
].is_row
) {
2269 isl_int_set_si(tab
->dual
->el
[1 + i
], 0);
2272 pos
= 2 + tab
->M
+ tab
->con
[i
].index
;
2273 if (tab
->con
[i
].negated
)
2274 isl_int_neg(tab
->dual
->el
[1 + i
],
2275 tab
->mat
->row
[var
->index
][pos
]);
2277 isl_int_set(tab
->dual
->el
[1 + i
],
2278 tab
->mat
->row
[var
->index
][pos
]);
2281 if (opt
&& res
== isl_lp_ok
) {
2283 isl_int_set(*opt
, tab
->mat
->row
[var
->index
][1]);
2284 isl_int_set(*opt_denom
, tab
->mat
->row
[var
->index
][0]);
2286 isl_int_cdiv_q(*opt
, tab
->mat
->row
[var
->index
][1],
2287 tab
->mat
->row
[var
->index
][0]);
2289 if (isl_tab_rollback(tab
, snap
) < 0)
2290 return isl_lp_error
;
2294 int isl_tab_is_redundant(struct isl_tab
*tab
, int con
)
2298 if (tab
->con
[con
].is_zero
)
2300 if (tab
->con
[con
].is_redundant
)
2302 return tab
->con
[con
].is_row
&& tab
->con
[con
].index
< tab
->n_redundant
;
2305 /* Take a snapshot of the tableau that can be restored by s call to
2308 struct isl_tab_undo
*isl_tab_snap(struct isl_tab
*tab
)
2316 /* Undo the operation performed by isl_tab_relax.
2318 static void unrelax(struct isl_tab
*tab
, struct isl_tab_var
*var
)
2320 unsigned off
= 2 + tab
->M
;
2322 if (!var
->is_row
&& !max_is_manifestly_unbounded(tab
, var
))
2323 to_row(tab
, var
, 1);
2326 isl_int_sub(tab
->mat
->row
[var
->index
][1],
2327 tab
->mat
->row
[var
->index
][1], tab
->mat
->row
[var
->index
][0]);
2331 for (i
= 0; i
< tab
->n_row
; ++i
) {
2332 if (isl_int_is_zero(tab
->mat
->row
[i
][off
+ var
->index
]))
2334 isl_int_add(tab
->mat
->row
[i
][1], tab
->mat
->row
[i
][1],
2335 tab
->mat
->row
[i
][off
+ var
->index
]);
2341 static void perform_undo_var(struct isl_tab
*tab
, struct isl_tab_undo
*undo
)
2343 struct isl_tab_var
*var
= var_from_index(tab
, undo
->u
.var_index
);
2344 switch(undo
->type
) {
2345 case isl_tab_undo_nonneg
:
2348 case isl_tab_undo_redundant
:
2349 var
->is_redundant
= 0;
2352 case isl_tab_undo_zero
:
2357 case isl_tab_undo_allocate
:
2358 if (undo
->u
.var_index
>= 0) {
2359 isl_assert(tab
->mat
->ctx
, !var
->is_row
, return);
2360 drop_col(tab
, var
->index
);
2364 if (!max_is_manifestly_unbounded(tab
, var
))
2365 to_row(tab
, var
, 1);
2366 else if (!min_is_manifestly_unbounded(tab
, var
))
2367 to_row(tab
, var
, -1);
2369 to_row(tab
, var
, 0);
2371 drop_row(tab
, var
->index
);
2373 case isl_tab_undo_relax
:
2379 /* Restore the tableau to the state where the basic variables
2380 * are those in "col_var".
2381 * We first construct a list of variables that are currently in
2382 * the basis, but shouldn't. Then we iterate over all variables
2383 * that should be in the basis and for each one that is currently
2384 * not in the basis, we exchange it with one of the elements of the
2385 * list constructed before.
2386 * We can always find an appropriate variable to pivot with because
2387 * the current basis is mapped to the old basis by a non-singular
2388 * matrix and so we can never end up with a zero row.
2390 static int restore_basis(struct isl_tab
*tab
, int *col_var
)
2394 int *extra
= NULL
; /* current columns that contain bad stuff */
2395 unsigned off
= 2 + tab
->M
;
2397 extra
= isl_alloc_array(tab
->mat
->ctx
, int, tab
->n_col
);
2400 for (i
= 0; i
< tab
->n_col
; ++i
) {
2401 for (j
= 0; j
< tab
->n_col
; ++j
)
2402 if (tab
->col_var
[i
] == col_var
[j
])
2406 extra
[n_extra
++] = i
;
2408 for (i
= 0; i
< tab
->n_col
&& n_extra
> 0; ++i
) {
2409 struct isl_tab_var
*var
;
2412 for (j
= 0; j
< tab
->n_col
; ++j
)
2413 if (col_var
[i
] == tab
->col_var
[j
])
2417 var
= var_from_index(tab
, col_var
[i
]);
2419 for (j
= 0; j
< n_extra
; ++j
)
2420 if (!isl_int_is_zero(tab
->mat
->row
[row
][off
+extra
[j
]]))
2422 isl_assert(tab
->mat
->ctx
, j
< n_extra
, goto error
);
2423 isl_tab_pivot(tab
, row
, extra
[j
]);
2424 extra
[j
] = extra
[--n_extra
];
2436 static int perform_undo(struct isl_tab
*tab
, struct isl_tab_undo
*undo
)
2438 switch (undo
->type
) {
2439 case isl_tab_undo_empty
:
2442 case isl_tab_undo_nonneg
:
2443 case isl_tab_undo_redundant
:
2444 case isl_tab_undo_zero
:
2445 case isl_tab_undo_allocate
:
2446 case isl_tab_undo_relax
:
2447 perform_undo_var(tab
, undo
);
2449 case isl_tab_undo_bset_eq
:
2450 isl_basic_set_free_equality(tab
->bset
, 1);
2452 case isl_tab_undo_bset_ineq
:
2453 isl_basic_set_free_inequality(tab
->bset
, 1);
2455 case isl_tab_undo_bset_div
:
2456 isl_basic_set_free_div(tab
->bset
, 1);
2458 tab
->samples
->n_col
--;
2460 case isl_tab_undo_saved_basis
:
2461 if (restore_basis(tab
, undo
->u
.col_var
) < 0)
2464 case isl_tab_undo_drop_sample
:
2468 isl_assert(tab
->mat
->ctx
, 0, return -1);
2473 /* Return the tableau to the state it was in when the snapshot "snap"
2476 int isl_tab_rollback(struct isl_tab
*tab
, struct isl_tab_undo
*snap
)
2478 struct isl_tab_undo
*undo
, *next
;
2484 for (undo
= tab
->top
; undo
&& undo
!= &tab
->bottom
; undo
= next
) {
2488 if (perform_undo(tab
, undo
) < 0) {
2502 /* The given row "row" represents an inequality violated by all
2503 * points in the tableau. Check for some special cases of such
2504 * separating constraints.
2505 * In particular, if the row has been reduced to the constant -1,
2506 * then we know the inequality is adjacent (but opposite) to
2507 * an equality in the tableau.
2508 * If the row has been reduced to r = -1 -r', with r' an inequality
2509 * of the tableau, then the inequality is adjacent (but opposite)
2510 * to the inequality r'.
2512 static enum isl_ineq_type
separation_type(struct isl_tab
*tab
, unsigned row
)
2515 unsigned off
= 2 + tab
->M
;
2518 return isl_ineq_separate
;
2520 if (!isl_int_is_one(tab
->mat
->row
[row
][0]))
2521 return isl_ineq_separate
;
2522 if (!isl_int_is_negone(tab
->mat
->row
[row
][1]))
2523 return isl_ineq_separate
;
2525 pos
= isl_seq_first_non_zero(tab
->mat
->row
[row
] + off
+ tab
->n_dead
,
2526 tab
->n_col
- tab
->n_dead
);
2528 return isl_ineq_adj_eq
;
2530 if (!isl_int_is_negone(tab
->mat
->row
[row
][off
+ tab
->n_dead
+ pos
]))
2531 return isl_ineq_separate
;
2533 pos
= isl_seq_first_non_zero(
2534 tab
->mat
->row
[row
] + off
+ tab
->n_dead
+ pos
+ 1,
2535 tab
->n_col
- tab
->n_dead
- pos
- 1);
2537 return pos
== -1 ? isl_ineq_adj_ineq
: isl_ineq_separate
;
2540 /* Check the effect of inequality "ineq" on the tableau "tab".
2542 * isl_ineq_redundant: satisfied by all points in the tableau
2543 * isl_ineq_separate: satisfied by no point in the tableau
2544 * isl_ineq_cut: satisfied by some by not all points
2545 * isl_ineq_adj_eq: adjacent to an equality
2546 * isl_ineq_adj_ineq: adjacent to an inequality.
2548 enum isl_ineq_type
isl_tab_ineq_type(struct isl_tab
*tab
, isl_int
*ineq
)
2550 enum isl_ineq_type type
= isl_ineq_error
;
2551 struct isl_tab_undo
*snap
= NULL
;
2556 return isl_ineq_error
;
2558 if (isl_tab_extend_cons(tab
, 1) < 0)
2559 return isl_ineq_error
;
2561 snap
= isl_tab_snap(tab
);
2563 con
= isl_tab_add_row(tab
, ineq
);
2567 row
= tab
->con
[con
].index
;
2568 if (isl_tab_row_is_redundant(tab
, row
))
2569 type
= isl_ineq_redundant
;
2570 else if (isl_int_is_neg(tab
->mat
->row
[row
][1]) &&
2572 isl_int_abs_ge(tab
->mat
->row
[row
][1],
2573 tab
->mat
->row
[row
][0]))) {
2574 if (at_least_zero(tab
, &tab
->con
[con
]))
2575 type
= isl_ineq_cut
;
2577 type
= separation_type(tab
, row
);
2578 } else if (tab
->rational
? (sign_of_min(tab
, &tab
->con
[con
]) < 0)
2579 : isl_tab_min_at_most_neg_one(tab
, &tab
->con
[con
]))
2580 type
= isl_ineq_cut
;
2582 type
= isl_ineq_redundant
;
2584 if (isl_tab_rollback(tab
, snap
))
2585 return isl_ineq_error
;
2588 isl_tab_rollback(tab
, snap
);
2589 return isl_ineq_error
;
2592 void isl_tab_dump(struct isl_tab
*tab
, FILE *out
, int indent
)
2598 fprintf(out
, "%*snull tab\n", indent
, "");
2601 fprintf(out
, "%*sn_redundant: %d, n_dead: %d", indent
, "",
2602 tab
->n_redundant
, tab
->n_dead
);
2604 fprintf(out
, ", rational");
2606 fprintf(out
, ", empty");
2608 fprintf(out
, "%*s[", indent
, "");
2609 for (i
= 0; i
< tab
->n_var
; ++i
) {
2611 fprintf(out
, (i
== tab
->n_param
||
2612 i
== tab
->n_var
- tab
->n_div
) ? "; "
2614 fprintf(out
, "%c%d%s", tab
->var
[i
].is_row
? 'r' : 'c',
2616 tab
->var
[i
].is_zero
? " [=0]" :
2617 tab
->var
[i
].is_redundant
? " [R]" : "");
2619 fprintf(out
, "]\n");
2620 fprintf(out
, "%*s[", indent
, "");
2621 for (i
= 0; i
< tab
->n_con
; ++i
) {
2624 fprintf(out
, "%c%d%s", tab
->con
[i
].is_row
? 'r' : 'c',
2626 tab
->con
[i
].is_zero
? " [=0]" :
2627 tab
->con
[i
].is_redundant
? " [R]" : "");
2629 fprintf(out
, "]\n");
2630 fprintf(out
, "%*s[", indent
, "");
2631 for (i
= 0; i
< tab
->n_row
; ++i
) {
2632 const char *sign
= "";
2635 if (tab
->row_sign
) {
2636 if (tab
->row_sign
[i
] == isl_tab_row_unknown
)
2638 else if (tab
->row_sign
[i
] == isl_tab_row_neg
)
2640 else if (tab
->row_sign
[i
] == isl_tab_row_pos
)
2645 fprintf(out
, "r%d: %d%s%s", i
, tab
->row_var
[i
],
2646 isl_tab_var_from_row(tab
, i
)->is_nonneg
? " [>=0]" : "", sign
);
2648 fprintf(out
, "]\n");
2649 fprintf(out
, "%*s[", indent
, "");
2650 for (i
= 0; i
< tab
->n_col
; ++i
) {
2653 fprintf(out
, "c%d: %d%s", i
, tab
->col_var
[i
],
2654 var_from_col(tab
, i
)->is_nonneg
? " [>=0]" : "");
2656 fprintf(out
, "]\n");
2657 r
= tab
->mat
->n_row
;
2658 tab
->mat
->n_row
= tab
->n_row
;
2659 c
= tab
->mat
->n_col
;
2660 tab
->mat
->n_col
= 2 + tab
->M
+ tab
->n_col
;
2661 isl_mat_dump(tab
->mat
, out
, indent
);
2662 tab
->mat
->n_row
= r
;
2663 tab
->mat
->n_col
= c
;
2665 isl_basic_set_dump(tab
->bset
, out
, indent
);