2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the GNU LGPLv2.1 license
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
11 #include "isl_map_private.h"
16 * The implementation of tableaus in this file was inspired by Section 8
17 * of David Detlefs, Greg Nelson and James B. Saxe, "Simplify: a theorem
18 * prover for program checking".
21 struct isl_tab
*isl_tab_alloc(struct isl_ctx
*ctx
,
22 unsigned n_row
, unsigned n_var
, unsigned M
)
28 tab
= isl_calloc_type(ctx
, struct isl_tab
);
31 tab
->mat
= isl_mat_alloc(ctx
, n_row
, off
+ n_var
);
34 tab
->var
= isl_alloc_array(ctx
, struct isl_tab_var
, n_var
);
37 tab
->con
= isl_alloc_array(ctx
, struct isl_tab_var
, n_row
);
40 tab
->col_var
= isl_alloc_array(ctx
, int, n_var
);
43 tab
->row_var
= isl_alloc_array(ctx
, int, n_row
);
46 for (i
= 0; i
< n_var
; ++i
) {
47 tab
->var
[i
].index
= i
;
48 tab
->var
[i
].is_row
= 0;
49 tab
->var
[i
].is_nonneg
= 0;
50 tab
->var
[i
].is_zero
= 0;
51 tab
->var
[i
].is_redundant
= 0;
52 tab
->var
[i
].frozen
= 0;
53 tab
->var
[i
].negated
= 0;
67 tab
->strict_redundant
= 0;
74 tab
->bottom
.type
= isl_tab_undo_bottom
;
75 tab
->bottom
.next
= NULL
;
76 tab
->top
= &tab
->bottom
;
88 int isl_tab_extend_cons(struct isl_tab
*tab
, unsigned n_new
)
90 unsigned off
= 2 + tab
->M
;
95 if (tab
->max_con
< tab
->n_con
+ n_new
) {
96 struct isl_tab_var
*con
;
98 con
= isl_realloc_array(tab
->mat
->ctx
, tab
->con
,
99 struct isl_tab_var
, tab
->max_con
+ n_new
);
103 tab
->max_con
+= n_new
;
105 if (tab
->mat
->n_row
< tab
->n_row
+ n_new
) {
108 tab
->mat
= isl_mat_extend(tab
->mat
,
109 tab
->n_row
+ n_new
, off
+ tab
->n_col
);
112 row_var
= isl_realloc_array(tab
->mat
->ctx
, tab
->row_var
,
113 int, tab
->mat
->n_row
);
116 tab
->row_var
= row_var
;
118 enum isl_tab_row_sign
*s
;
119 s
= isl_realloc_array(tab
->mat
->ctx
, tab
->row_sign
,
120 enum isl_tab_row_sign
, tab
->mat
->n_row
);
129 /* Make room for at least n_new extra variables.
130 * Return -1 if anything went wrong.
132 int isl_tab_extend_vars(struct isl_tab
*tab
, unsigned n_new
)
134 struct isl_tab_var
*var
;
135 unsigned off
= 2 + tab
->M
;
137 if (tab
->max_var
< tab
->n_var
+ n_new
) {
138 var
= isl_realloc_array(tab
->mat
->ctx
, tab
->var
,
139 struct isl_tab_var
, tab
->n_var
+ n_new
);
143 tab
->max_var
+= n_new
;
146 if (tab
->mat
->n_col
< off
+ tab
->n_col
+ n_new
) {
149 tab
->mat
= isl_mat_extend(tab
->mat
,
150 tab
->mat
->n_row
, off
+ tab
->n_col
+ n_new
);
153 p
= isl_realloc_array(tab
->mat
->ctx
, tab
->col_var
,
154 int, tab
->n_col
+ n_new
);
163 struct isl_tab
*isl_tab_extend(struct isl_tab
*tab
, unsigned n_new
)
165 if (isl_tab_extend_cons(tab
, n_new
) >= 0)
172 static void free_undo(struct isl_tab
*tab
)
174 struct isl_tab_undo
*undo
, *next
;
176 for (undo
= tab
->top
; undo
&& undo
!= &tab
->bottom
; undo
= next
) {
183 void isl_tab_free(struct isl_tab
*tab
)
188 isl_mat_free(tab
->mat
);
189 isl_vec_free(tab
->dual
);
190 isl_basic_map_free(tab
->bmap
);
196 isl_mat_free(tab
->samples
);
197 free(tab
->sample_index
);
198 isl_mat_free(tab
->basis
);
202 struct isl_tab
*isl_tab_dup(struct isl_tab
*tab
)
212 dup
= isl_calloc_type(tab
->mat
->ctx
, struct isl_tab
);
215 dup
->mat
= isl_mat_dup(tab
->mat
);
218 dup
->var
= isl_alloc_array(tab
->mat
->ctx
, struct isl_tab_var
, tab
->max_var
);
221 for (i
= 0; i
< tab
->n_var
; ++i
)
222 dup
->var
[i
] = tab
->var
[i
];
223 dup
->con
= isl_alloc_array(tab
->mat
->ctx
, struct isl_tab_var
, tab
->max_con
);
226 for (i
= 0; i
< tab
->n_con
; ++i
)
227 dup
->con
[i
] = tab
->con
[i
];
228 dup
->col_var
= isl_alloc_array(tab
->mat
->ctx
, int, tab
->mat
->n_col
- off
);
231 for (i
= 0; i
< tab
->n_col
; ++i
)
232 dup
->col_var
[i
] = tab
->col_var
[i
];
233 dup
->row_var
= isl_alloc_array(tab
->mat
->ctx
, int, tab
->mat
->n_row
);
236 for (i
= 0; i
< tab
->n_row
; ++i
)
237 dup
->row_var
[i
] = tab
->row_var
[i
];
239 dup
->row_sign
= isl_alloc_array(tab
->mat
->ctx
, enum isl_tab_row_sign
,
243 for (i
= 0; i
< tab
->n_row
; ++i
)
244 dup
->row_sign
[i
] = tab
->row_sign
[i
];
247 dup
->samples
= isl_mat_dup(tab
->samples
);
250 dup
->sample_index
= isl_alloc_array(tab
->mat
->ctx
, int,
251 tab
->samples
->n_row
);
252 if (!dup
->sample_index
)
254 dup
->n_sample
= tab
->n_sample
;
255 dup
->n_outside
= tab
->n_outside
;
257 dup
->n_row
= tab
->n_row
;
258 dup
->n_con
= tab
->n_con
;
259 dup
->n_eq
= tab
->n_eq
;
260 dup
->max_con
= tab
->max_con
;
261 dup
->n_col
= tab
->n_col
;
262 dup
->n_var
= tab
->n_var
;
263 dup
->max_var
= tab
->max_var
;
264 dup
->n_param
= tab
->n_param
;
265 dup
->n_div
= tab
->n_div
;
266 dup
->n_dead
= tab
->n_dead
;
267 dup
->n_redundant
= tab
->n_redundant
;
268 dup
->rational
= tab
->rational
;
269 dup
->empty
= tab
->empty
;
270 dup
->strict_redundant
= 0;
274 tab
->cone
= tab
->cone
;
275 dup
->bottom
.type
= isl_tab_undo_bottom
;
276 dup
->bottom
.next
= NULL
;
277 dup
->top
= &dup
->bottom
;
279 dup
->n_zero
= tab
->n_zero
;
280 dup
->n_unbounded
= tab
->n_unbounded
;
281 dup
->basis
= isl_mat_dup(tab
->basis
);
289 /* Construct the coefficient matrix of the product tableau
291 * mat{1,2} is the coefficient matrix of tableau {1,2}
292 * row{1,2} is the number of rows in tableau {1,2}
293 * col{1,2} is the number of columns in tableau {1,2}
294 * off is the offset to the coefficient column (skipping the
295 * denominator, the constant term and the big parameter if any)
296 * r{1,2} is the number of redundant rows in tableau {1,2}
297 * d{1,2} is the number of dead columns in tableau {1,2}
299 * The order of the rows and columns in the result is as explained
300 * in isl_tab_product.
302 static struct isl_mat
*tab_mat_product(struct isl_mat
*mat1
,
303 struct isl_mat
*mat2
, unsigned row1
, unsigned row2
,
304 unsigned col1
, unsigned col2
,
305 unsigned off
, unsigned r1
, unsigned r2
, unsigned d1
, unsigned d2
)
308 struct isl_mat
*prod
;
311 prod
= isl_mat_alloc(mat1
->ctx
, mat1
->n_row
+ mat2
->n_row
,
315 for (i
= 0; i
< r1
; ++i
) {
316 isl_seq_cpy(prod
->row
[n
+ i
], mat1
->row
[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
[i
] + off
+ d1
, col1
- d1
);
320 isl_seq_clr(prod
->row
[n
+ i
] + off
+ col1
+ d1
, col2
- d2
);
324 for (i
= 0; i
< r2
; ++i
) {
325 isl_seq_cpy(prod
->row
[n
+ i
], mat2
->row
[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
[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
[i
] + off
+ d2
, col2
- d2
);
335 for (i
= 0; i
< row1
- r1
; ++i
) {
336 isl_seq_cpy(prod
->row
[n
+ i
], mat1
->row
[r1
+ i
], off
+ d1
);
337 isl_seq_clr(prod
->row
[n
+ i
] + off
+ d1
, d2
);
338 isl_seq_cpy(prod
->row
[n
+ i
] + off
+ d1
+ d2
,
339 mat1
->row
[r1
+ i
] + off
+ d1
, col1
- d1
);
340 isl_seq_clr(prod
->row
[n
+ i
] + off
+ col1
+ d1
, col2
- d2
);
344 for (i
= 0; i
< row2
- r2
; ++i
) {
345 isl_seq_cpy(prod
->row
[n
+ i
], mat2
->row
[r2
+ i
], off
);
346 isl_seq_clr(prod
->row
[n
+ i
] + off
, d1
);
347 isl_seq_cpy(prod
->row
[n
+ i
] + off
+ d1
,
348 mat2
->row
[r2
+ i
] + off
, d2
);
349 isl_seq_clr(prod
->row
[n
+ i
] + off
+ d1
+ d2
, col1
- d1
);
350 isl_seq_cpy(prod
->row
[n
+ i
] + off
+ col1
+ d1
,
351 mat2
->row
[r2
+ i
] + off
+ d2
, col2
- d2
);
357 /* Update the row or column index of a variable that corresponds
358 * to a variable in the first input tableau.
360 static void update_index1(struct isl_tab_var
*var
,
361 unsigned r1
, unsigned r2
, unsigned d1
, unsigned d2
)
363 if (var
->index
== -1)
365 if (var
->is_row
&& var
->index
>= r1
)
367 if (!var
->is_row
&& var
->index
>= d1
)
371 /* Update the row or column index of a variable that corresponds
372 * to a variable in the second input tableau.
374 static void update_index2(struct isl_tab_var
*var
,
375 unsigned row1
, unsigned col1
,
376 unsigned r1
, unsigned r2
, unsigned d1
, unsigned d2
)
378 if (var
->index
== -1)
393 /* Create a tableau that represents the Cartesian product of the sets
394 * represented by tableaus tab1 and tab2.
395 * The order of the rows in the product is
396 * - redundant rows of tab1
397 * - redundant rows of tab2
398 * - non-redundant rows of tab1
399 * - non-redundant rows of tab2
400 * The order of the columns is
403 * - coefficient of big parameter, if any
404 * - dead columns of tab1
405 * - dead columns of tab2
406 * - live columns of tab1
407 * - live columns of tab2
408 * The order of the variables and the constraints is a concatenation
409 * of order in the two input tableaus.
411 struct isl_tab
*isl_tab_product(struct isl_tab
*tab1
, struct isl_tab
*tab2
)
414 struct isl_tab
*prod
;
416 unsigned r1
, r2
, d1
, d2
;
421 isl_assert(tab1
->mat
->ctx
, tab1
->M
== tab2
->M
, return NULL
);
422 isl_assert(tab1
->mat
->ctx
, tab1
->rational
== tab2
->rational
, return NULL
);
423 isl_assert(tab1
->mat
->ctx
, tab1
->cone
== tab2
->cone
, return NULL
);
424 isl_assert(tab1
->mat
->ctx
, !tab1
->row_sign
, return NULL
);
425 isl_assert(tab1
->mat
->ctx
, !tab2
->row_sign
, return NULL
);
426 isl_assert(tab1
->mat
->ctx
, tab1
->n_param
== 0, return NULL
);
427 isl_assert(tab1
->mat
->ctx
, tab2
->n_param
== 0, return NULL
);
428 isl_assert(tab1
->mat
->ctx
, tab1
->n_div
== 0, return NULL
);
429 isl_assert(tab1
->mat
->ctx
, tab2
->n_div
== 0, return NULL
);
432 r1
= tab1
->n_redundant
;
433 r2
= tab2
->n_redundant
;
436 prod
= isl_calloc_type(tab1
->mat
->ctx
, struct isl_tab
);
439 prod
->mat
= tab_mat_product(tab1
->mat
, tab2
->mat
,
440 tab1
->n_row
, tab2
->n_row
,
441 tab1
->n_col
, tab2
->n_col
, off
, r1
, r2
, d1
, d2
);
444 prod
->var
= isl_alloc_array(tab1
->mat
->ctx
, struct isl_tab_var
,
445 tab1
->max_var
+ tab2
->max_var
);
448 for (i
= 0; i
< tab1
->n_var
; ++i
) {
449 prod
->var
[i
] = tab1
->var
[i
];
450 update_index1(&prod
->var
[i
], r1
, r2
, d1
, d2
);
452 for (i
= 0; i
< tab2
->n_var
; ++i
) {
453 prod
->var
[tab1
->n_var
+ i
] = tab2
->var
[i
];
454 update_index2(&prod
->var
[tab1
->n_var
+ i
],
455 tab1
->n_row
, tab1
->n_col
,
458 prod
->con
= isl_alloc_array(tab1
->mat
->ctx
, struct isl_tab_var
,
459 tab1
->max_con
+ tab2
->max_con
);
462 for (i
= 0; i
< tab1
->n_con
; ++i
) {
463 prod
->con
[i
] = tab1
->con
[i
];
464 update_index1(&prod
->con
[i
], r1
, r2
, d1
, d2
);
466 for (i
= 0; i
< tab2
->n_con
; ++i
) {
467 prod
->con
[tab1
->n_con
+ i
] = tab2
->con
[i
];
468 update_index2(&prod
->con
[tab1
->n_con
+ i
],
469 tab1
->n_row
, tab1
->n_col
,
472 prod
->col_var
= isl_alloc_array(tab1
->mat
->ctx
, int,
473 tab1
->n_col
+ tab2
->n_col
);
476 for (i
= 0; i
< tab1
->n_col
; ++i
) {
477 int pos
= i
< d1
? i
: i
+ d2
;
478 prod
->col_var
[pos
] = tab1
->col_var
[i
];
480 for (i
= 0; i
< tab2
->n_col
; ++i
) {
481 int pos
= i
< d2
? d1
+ i
: tab1
->n_col
+ i
;
482 int t
= tab2
->col_var
[i
];
487 prod
->col_var
[pos
] = t
;
489 prod
->row_var
= isl_alloc_array(tab1
->mat
->ctx
, int,
490 tab1
->mat
->n_row
+ tab2
->mat
->n_row
);
493 for (i
= 0; i
< tab1
->n_row
; ++i
) {
494 int pos
= i
< r1
? i
: i
+ r2
;
495 prod
->row_var
[pos
] = tab1
->row_var
[i
];
497 for (i
= 0; i
< tab2
->n_row
; ++i
) {
498 int pos
= i
< r2
? r1
+ i
: tab1
->n_row
+ i
;
499 int t
= tab2
->row_var
[i
];
504 prod
->row_var
[pos
] = t
;
506 prod
->samples
= NULL
;
507 prod
->sample_index
= NULL
;
508 prod
->n_row
= tab1
->n_row
+ tab2
->n_row
;
509 prod
->n_con
= tab1
->n_con
+ tab2
->n_con
;
511 prod
->max_con
= tab1
->max_con
+ tab2
->max_con
;
512 prod
->n_col
= tab1
->n_col
+ tab2
->n_col
;
513 prod
->n_var
= tab1
->n_var
+ tab2
->n_var
;
514 prod
->max_var
= tab1
->max_var
+ tab2
->max_var
;
517 prod
->n_dead
= tab1
->n_dead
+ tab2
->n_dead
;
518 prod
->n_redundant
= tab1
->n_redundant
+ tab2
->n_redundant
;
519 prod
->rational
= tab1
->rational
;
520 prod
->empty
= tab1
->empty
|| tab2
->empty
;
521 prod
->strict_redundant
= tab1
->strict_redundant
|| tab2
->strict_redundant
;
525 prod
->cone
= tab1
->cone
;
526 prod
->bottom
.type
= isl_tab_undo_bottom
;
527 prod
->bottom
.next
= NULL
;
528 prod
->top
= &prod
->bottom
;
531 prod
->n_unbounded
= 0;
540 static struct isl_tab_var
*var_from_index(struct isl_tab
*tab
, int i
)
545 return &tab
->con
[~i
];
548 struct isl_tab_var
*isl_tab_var_from_row(struct isl_tab
*tab
, int i
)
550 return var_from_index(tab
, tab
->row_var
[i
]);
553 static struct isl_tab_var
*var_from_col(struct isl_tab
*tab
, int i
)
555 return var_from_index(tab
, tab
->col_var
[i
]);
558 /* Check if there are any upper bounds on column variable "var",
559 * i.e., non-negative rows where var appears with a negative coefficient.
560 * Return 1 if there are no such bounds.
562 static int max_is_manifestly_unbounded(struct isl_tab
*tab
,
563 struct isl_tab_var
*var
)
566 unsigned off
= 2 + tab
->M
;
570 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
571 if (!isl_int_is_neg(tab
->mat
->row
[i
][off
+ var
->index
]))
573 if (isl_tab_var_from_row(tab
, i
)->is_nonneg
)
579 /* Check if there are any lower bounds on column variable "var",
580 * i.e., non-negative rows where var appears with a positive coefficient.
581 * Return 1 if there are no such bounds.
583 static int min_is_manifestly_unbounded(struct isl_tab
*tab
,
584 struct isl_tab_var
*var
)
587 unsigned off
= 2 + tab
->M
;
591 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
592 if (!isl_int_is_pos(tab
->mat
->row
[i
][off
+ var
->index
]))
594 if (isl_tab_var_from_row(tab
, i
)->is_nonneg
)
600 static int row_cmp(struct isl_tab
*tab
, int r1
, int r2
, int c
, isl_int t
)
602 unsigned off
= 2 + tab
->M
;
606 isl_int_mul(t
, tab
->mat
->row
[r1
][2], tab
->mat
->row
[r2
][off
+c
]);
607 isl_int_submul(t
, tab
->mat
->row
[r2
][2], tab
->mat
->row
[r1
][off
+c
]);
612 isl_int_mul(t
, tab
->mat
->row
[r1
][1], tab
->mat
->row
[r2
][off
+ c
]);
613 isl_int_submul(t
, tab
->mat
->row
[r2
][1], tab
->mat
->row
[r1
][off
+ c
]);
614 return isl_int_sgn(t
);
617 /* Given the index of a column "c", return the index of a row
618 * that can be used to pivot the column in, with either an increase
619 * (sgn > 0) or a decrease (sgn < 0) of the corresponding variable.
620 * If "var" is not NULL, then the row returned will be different from
621 * the one associated with "var".
623 * Each row in the tableau is of the form
625 * x_r = a_r0 + \sum_i a_ri x_i
627 * Only rows with x_r >= 0 and with the sign of a_ri opposite to "sgn"
628 * impose any limit on the increase or decrease in the value of x_c
629 * and this bound is equal to a_r0 / |a_rc|. We are therefore looking
630 * for the row with the smallest (most stringent) such bound.
631 * Note that the common denominator of each row drops out of the fraction.
632 * To check if row j has a smaller bound than row r, i.e.,
633 * a_j0 / |a_jc| < a_r0 / |a_rc| or a_j0 |a_rc| < a_r0 |a_jc|,
634 * we check if -sign(a_jc) (a_j0 a_rc - a_r0 a_jc) < 0,
635 * where -sign(a_jc) is equal to "sgn".
637 static int pivot_row(struct isl_tab
*tab
,
638 struct isl_tab_var
*var
, int sgn
, int c
)
642 unsigned off
= 2 + tab
->M
;
646 for (j
= tab
->n_redundant
; j
< tab
->n_row
; ++j
) {
647 if (var
&& j
== var
->index
)
649 if (!isl_tab_var_from_row(tab
, j
)->is_nonneg
)
651 if (sgn
* isl_int_sgn(tab
->mat
->row
[j
][off
+ c
]) >= 0)
657 tsgn
= sgn
* row_cmp(tab
, r
, j
, c
, t
);
658 if (tsgn
< 0 || (tsgn
== 0 &&
659 tab
->row_var
[j
] < tab
->row_var
[r
]))
666 /* Find a pivot (row and col) that will increase (sgn > 0) or decrease
667 * (sgn < 0) the value of row variable var.
668 * If not NULL, then skip_var is a row variable that should be ignored
669 * while looking for a pivot row. It is usually equal to var.
671 * As the given row in the tableau is of the form
673 * x_r = a_r0 + \sum_i a_ri x_i
675 * we need to find a column such that the sign of a_ri is equal to "sgn"
676 * (such that an increase in x_i will have the desired effect) or a
677 * column with a variable that may attain negative values.
678 * If a_ri is positive, then we need to move x_i in the same direction
679 * to obtain the desired effect. Otherwise, x_i has to move in the
680 * opposite direction.
682 static void find_pivot(struct isl_tab
*tab
,
683 struct isl_tab_var
*var
, struct isl_tab_var
*skip_var
,
684 int sgn
, int *row
, int *col
)
691 isl_assert(tab
->mat
->ctx
, var
->is_row
, return);
692 tr
= tab
->mat
->row
[var
->index
] + 2 + tab
->M
;
695 for (j
= tab
->n_dead
; j
< tab
->n_col
; ++j
) {
696 if (isl_int_is_zero(tr
[j
]))
698 if (isl_int_sgn(tr
[j
]) != sgn
&&
699 var_from_col(tab
, j
)->is_nonneg
)
701 if (c
< 0 || tab
->col_var
[j
] < tab
->col_var
[c
])
707 sgn
*= isl_int_sgn(tr
[c
]);
708 r
= pivot_row(tab
, skip_var
, sgn
, c
);
709 *row
= r
< 0 ? var
->index
: r
;
713 /* Return 1 if row "row" represents an obviously redundant inequality.
715 * - it represents an inequality or a variable
716 * - that is the sum of a non-negative sample value and a positive
717 * combination of zero or more non-negative constraints.
719 int isl_tab_row_is_redundant(struct isl_tab
*tab
, int row
)
722 unsigned off
= 2 + tab
->M
;
724 if (tab
->row_var
[row
] < 0 && !isl_tab_var_from_row(tab
, row
)->is_nonneg
)
727 if (isl_int_is_neg(tab
->mat
->row
[row
][1]))
729 if (tab
->strict_redundant
&& isl_int_is_zero(tab
->mat
->row
[row
][1]))
731 if (tab
->M
&& isl_int_is_neg(tab
->mat
->row
[row
][2]))
734 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
735 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ i
]))
737 if (tab
->col_var
[i
] >= 0)
739 if (isl_int_is_neg(tab
->mat
->row
[row
][off
+ i
]))
741 if (!var_from_col(tab
, i
)->is_nonneg
)
747 static void swap_rows(struct isl_tab
*tab
, int row1
, int row2
)
750 enum isl_tab_row_sign s
;
752 t
= tab
->row_var
[row1
];
753 tab
->row_var
[row1
] = tab
->row_var
[row2
];
754 tab
->row_var
[row2
] = t
;
755 isl_tab_var_from_row(tab
, row1
)->index
= row1
;
756 isl_tab_var_from_row(tab
, row2
)->index
= row2
;
757 tab
->mat
= isl_mat_swap_rows(tab
->mat
, row1
, row2
);
761 s
= tab
->row_sign
[row1
];
762 tab
->row_sign
[row1
] = tab
->row_sign
[row2
];
763 tab
->row_sign
[row2
] = s
;
766 static int push_union(struct isl_tab
*tab
,
767 enum isl_tab_undo_type type
, union isl_tab_undo_val u
) WARN_UNUSED
;
768 static int push_union(struct isl_tab
*tab
,
769 enum isl_tab_undo_type type
, union isl_tab_undo_val u
)
771 struct isl_tab_undo
*undo
;
776 undo
= isl_alloc_type(tab
->mat
->ctx
, struct isl_tab_undo
);
781 undo
->next
= tab
->top
;
787 int isl_tab_push_var(struct isl_tab
*tab
,
788 enum isl_tab_undo_type type
, struct isl_tab_var
*var
)
790 union isl_tab_undo_val u
;
792 u
.var_index
= tab
->row_var
[var
->index
];
794 u
.var_index
= tab
->col_var
[var
->index
];
795 return push_union(tab
, type
, u
);
798 int isl_tab_push(struct isl_tab
*tab
, enum isl_tab_undo_type type
)
800 union isl_tab_undo_val u
= { 0 };
801 return push_union(tab
, type
, u
);
804 /* Push a record on the undo stack describing the current basic
805 * variables, so that the this state can be restored during rollback.
807 int isl_tab_push_basis(struct isl_tab
*tab
)
810 union isl_tab_undo_val u
;
812 u
.col_var
= isl_alloc_array(tab
->mat
->ctx
, int, tab
->n_col
);
815 for (i
= 0; i
< tab
->n_col
; ++i
)
816 u
.col_var
[i
] = tab
->col_var
[i
];
817 return push_union(tab
, isl_tab_undo_saved_basis
, u
);
820 int isl_tab_push_callback(struct isl_tab
*tab
, struct isl_tab_callback
*callback
)
822 union isl_tab_undo_val u
;
823 u
.callback
= callback
;
824 return push_union(tab
, isl_tab_undo_callback
, u
);
827 struct isl_tab
*isl_tab_init_samples(struct isl_tab
*tab
)
834 tab
->samples
= isl_mat_alloc(tab
->mat
->ctx
, 1, 1 + tab
->n_var
);
837 tab
->sample_index
= isl_alloc_array(tab
->mat
->ctx
, int, 1);
838 if (!tab
->sample_index
)
846 struct isl_tab
*isl_tab_add_sample(struct isl_tab
*tab
,
847 __isl_take isl_vec
*sample
)
852 if (tab
->n_sample
+ 1 > tab
->samples
->n_row
) {
853 int *t
= isl_realloc_array(tab
->mat
->ctx
,
854 tab
->sample_index
, int, tab
->n_sample
+ 1);
857 tab
->sample_index
= t
;
860 tab
->samples
= isl_mat_extend(tab
->samples
,
861 tab
->n_sample
+ 1, tab
->samples
->n_col
);
865 isl_seq_cpy(tab
->samples
->row
[tab
->n_sample
], sample
->el
, sample
->size
);
866 isl_vec_free(sample
);
867 tab
->sample_index
[tab
->n_sample
] = tab
->n_sample
;
872 isl_vec_free(sample
);
877 struct isl_tab
*isl_tab_drop_sample(struct isl_tab
*tab
, int s
)
879 if (s
!= tab
->n_outside
) {
880 int t
= tab
->sample_index
[tab
->n_outside
];
881 tab
->sample_index
[tab
->n_outside
] = tab
->sample_index
[s
];
882 tab
->sample_index
[s
] = t
;
883 isl_mat_swap_rows(tab
->samples
, tab
->n_outside
, s
);
886 if (isl_tab_push(tab
, isl_tab_undo_drop_sample
) < 0) {
894 /* Record the current number of samples so that we can remove newer
895 * samples during a rollback.
897 int isl_tab_save_samples(struct isl_tab
*tab
)
899 union isl_tab_undo_val u
;
905 return push_union(tab
, isl_tab_undo_saved_samples
, u
);
908 /* Mark row with index "row" as being redundant.
909 * If we may need to undo the operation or if the row represents
910 * a variable of the original problem, the row is kept,
911 * but no longer considered when looking for a pivot row.
912 * Otherwise, the row is simply removed.
914 * The row may be interchanged with some other row. If it
915 * is interchanged with a later row, return 1. Otherwise return 0.
916 * If the rows are checked in order in the calling function,
917 * then a return value of 1 means that the row with the given
918 * row number may now contain a different row that hasn't been checked yet.
920 int isl_tab_mark_redundant(struct isl_tab
*tab
, int row
)
922 struct isl_tab_var
*var
= isl_tab_var_from_row(tab
, row
);
923 var
->is_redundant
= 1;
924 isl_assert(tab
->mat
->ctx
, row
>= tab
->n_redundant
, return -1);
925 if (tab
->need_undo
|| tab
->row_var
[row
] >= 0) {
926 if (tab
->row_var
[row
] >= 0 && !var
->is_nonneg
) {
928 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, var
) < 0)
931 if (row
!= tab
->n_redundant
)
932 swap_rows(tab
, row
, tab
->n_redundant
);
934 return isl_tab_push_var(tab
, isl_tab_undo_redundant
, var
);
936 if (row
!= tab
->n_row
- 1)
937 swap_rows(tab
, row
, tab
->n_row
- 1);
938 isl_tab_var_from_row(tab
, tab
->n_row
- 1)->index
= -1;
944 int isl_tab_mark_empty(struct isl_tab
*tab
)
948 if (!tab
->empty
&& tab
->need_undo
)
949 if (isl_tab_push(tab
, isl_tab_undo_empty
) < 0)
955 int isl_tab_freeze_constraint(struct isl_tab
*tab
, int con
)
957 struct isl_tab_var
*var
;
962 var
= &tab
->con
[con
];
970 return isl_tab_push_var(tab
, isl_tab_undo_freeze
, var
);
975 /* Update the rows signs after a pivot of "row" and "col", with "row_sgn"
976 * the original sign of the pivot element.
977 * We only keep track of row signs during PILP solving and in this case
978 * we only pivot a row with negative sign (meaning the value is always
979 * non-positive) using a positive pivot element.
981 * For each row j, the new value of the parametric constant is equal to
983 * a_j0 - a_jc a_r0/a_rc
985 * where a_j0 is the original parametric constant, a_rc is the pivot element,
986 * a_r0 is the parametric constant of the pivot row and a_jc is the
987 * pivot column entry of the row j.
988 * Since a_r0 is non-positive and a_rc is positive, the sign of row j
989 * remains the same if a_jc has the same sign as the row j or if
990 * a_jc is zero. In all other cases, we reset the sign to "unknown".
992 static void update_row_sign(struct isl_tab
*tab
, int row
, int col
, int row_sgn
)
995 struct isl_mat
*mat
= tab
->mat
;
996 unsigned off
= 2 + tab
->M
;
1001 if (tab
->row_sign
[row
] == 0)
1003 isl_assert(mat
->ctx
, row_sgn
> 0, return);
1004 isl_assert(mat
->ctx
, tab
->row_sign
[row
] == isl_tab_row_neg
, return);
1005 tab
->row_sign
[row
] = isl_tab_row_pos
;
1006 for (i
= 0; i
< tab
->n_row
; ++i
) {
1010 s
= isl_int_sgn(mat
->row
[i
][off
+ col
]);
1013 if (!tab
->row_sign
[i
])
1015 if (s
< 0 && tab
->row_sign
[i
] == isl_tab_row_neg
)
1017 if (s
> 0 && tab
->row_sign
[i
] == isl_tab_row_pos
)
1019 tab
->row_sign
[i
] = isl_tab_row_unknown
;
1023 /* Given a row number "row" and a column number "col", pivot the tableau
1024 * such that the associated variables are interchanged.
1025 * The given row in the tableau expresses
1027 * x_r = a_r0 + \sum_i a_ri x_i
1031 * x_c = 1/a_rc x_r - a_r0/a_rc + sum_{i \ne r} -a_ri/a_rc
1033 * Substituting this equality into the other rows
1035 * x_j = a_j0 + \sum_i a_ji x_i
1037 * with a_jc \ne 0, we obtain
1039 * 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
1046 * where i is any other column and j is any other row,
1047 * is therefore transformed into
1049 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1050 * 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)
1052 * The transformation is performed along the following steps
1054 * d_r/n_rc n_ri/n_rc
1057 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1060 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1061 * n_jc/(|n_rc| d_j) n_ji/(|n_rc| d_j)
1063 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1064 * n_jc/(|n_rc| d_j) (n_ji |n_rc|)/(|n_rc| d_j)
1066 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1067 * n_jc/(|n_rc| d_j) (n_ji |n_rc| - s(n_rc)n_jc n_ri)/(|n_rc| d_j)
1069 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1070 * 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)
1073 int isl_tab_pivot(struct isl_tab
*tab
, int row
, int col
)
1078 struct isl_mat
*mat
= tab
->mat
;
1079 struct isl_tab_var
*var
;
1080 unsigned off
= 2 + tab
->M
;
1082 isl_int_swap(mat
->row
[row
][0], mat
->row
[row
][off
+ col
]);
1083 sgn
= isl_int_sgn(mat
->row
[row
][0]);
1085 isl_int_neg(mat
->row
[row
][0], mat
->row
[row
][0]);
1086 isl_int_neg(mat
->row
[row
][off
+ col
], mat
->row
[row
][off
+ col
]);
1088 for (j
= 0; j
< off
- 1 + tab
->n_col
; ++j
) {
1089 if (j
== off
- 1 + col
)
1091 isl_int_neg(mat
->row
[row
][1 + j
], mat
->row
[row
][1 + j
]);
1093 if (!isl_int_is_one(mat
->row
[row
][0]))
1094 isl_seq_normalize(mat
->ctx
, mat
->row
[row
], off
+ tab
->n_col
);
1095 for (i
= 0; i
< tab
->n_row
; ++i
) {
1098 if (isl_int_is_zero(mat
->row
[i
][off
+ col
]))
1100 isl_int_mul(mat
->row
[i
][0], mat
->row
[i
][0], mat
->row
[row
][0]);
1101 for (j
= 0; j
< off
- 1 + tab
->n_col
; ++j
) {
1102 if (j
== off
- 1 + col
)
1104 isl_int_mul(mat
->row
[i
][1 + j
],
1105 mat
->row
[i
][1 + j
], mat
->row
[row
][0]);
1106 isl_int_addmul(mat
->row
[i
][1 + j
],
1107 mat
->row
[i
][off
+ col
], mat
->row
[row
][1 + j
]);
1109 isl_int_mul(mat
->row
[i
][off
+ col
],
1110 mat
->row
[i
][off
+ col
], mat
->row
[row
][off
+ col
]);
1111 if (!isl_int_is_one(mat
->row
[i
][0]))
1112 isl_seq_normalize(mat
->ctx
, mat
->row
[i
], off
+ tab
->n_col
);
1114 t
= tab
->row_var
[row
];
1115 tab
->row_var
[row
] = tab
->col_var
[col
];
1116 tab
->col_var
[col
] = t
;
1117 var
= isl_tab_var_from_row(tab
, row
);
1120 var
= var_from_col(tab
, col
);
1123 update_row_sign(tab
, row
, col
, sgn
);
1126 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
1127 if (isl_int_is_zero(mat
->row
[i
][off
+ col
]))
1129 if (!isl_tab_var_from_row(tab
, i
)->frozen
&&
1130 isl_tab_row_is_redundant(tab
, i
)) {
1131 int redo
= isl_tab_mark_redundant(tab
, i
);
1141 /* If "var" represents a column variable, then pivot is up (sgn > 0)
1142 * or down (sgn < 0) to a row. The variable is assumed not to be
1143 * unbounded in the specified direction.
1144 * If sgn = 0, then the variable is unbounded in both directions,
1145 * and we pivot with any row we can find.
1147 static int to_row(struct isl_tab
*tab
, struct isl_tab_var
*var
, int sign
) WARN_UNUSED
;
1148 static int to_row(struct isl_tab
*tab
, struct isl_tab_var
*var
, int sign
)
1151 unsigned off
= 2 + tab
->M
;
1157 for (r
= tab
->n_redundant
; r
< tab
->n_row
; ++r
)
1158 if (!isl_int_is_zero(tab
->mat
->row
[r
][off
+var
->index
]))
1160 isl_assert(tab
->mat
->ctx
, r
< tab
->n_row
, return -1);
1162 r
= pivot_row(tab
, NULL
, sign
, var
->index
);
1163 isl_assert(tab
->mat
->ctx
, r
>= 0, return -1);
1166 return isl_tab_pivot(tab
, r
, var
->index
);
1169 static void check_table(struct isl_tab
*tab
)
1175 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
1176 struct isl_tab_var
*var
;
1177 var
= isl_tab_var_from_row(tab
, i
);
1178 if (!var
->is_nonneg
)
1181 isl_assert(tab
->mat
->ctx
,
1182 !isl_int_is_neg(tab
->mat
->row
[i
][2]), abort());
1183 if (isl_int_is_pos(tab
->mat
->row
[i
][2]))
1186 isl_assert(tab
->mat
->ctx
, !isl_int_is_neg(tab
->mat
->row
[i
][1]),
1191 /* Return the sign of the maximal value of "var".
1192 * If the sign is not negative, then on return from this function,
1193 * the sample value will also be non-negative.
1195 * If "var" is manifestly unbounded wrt positive values, we are done.
1196 * Otherwise, we pivot the variable up to a row if needed
1197 * Then we continue pivoting down until either
1198 * - no more down pivots can be performed
1199 * - the sample value is positive
1200 * - the variable is pivoted into a manifestly unbounded column
1202 static int sign_of_max(struct isl_tab
*tab
, struct isl_tab_var
*var
)
1206 if (max_is_manifestly_unbounded(tab
, var
))
1208 if (to_row(tab
, var
, 1) < 0)
1210 while (!isl_int_is_pos(tab
->mat
->row
[var
->index
][1])) {
1211 find_pivot(tab
, var
, var
, 1, &row
, &col
);
1213 return isl_int_sgn(tab
->mat
->row
[var
->index
][1]);
1214 if (isl_tab_pivot(tab
, row
, col
) < 0)
1216 if (!var
->is_row
) /* manifestly unbounded */
1222 int isl_tab_sign_of_max(struct isl_tab
*tab
, int con
)
1224 struct isl_tab_var
*var
;
1229 var
= &tab
->con
[con
];
1230 isl_assert(tab
->mat
->ctx
, !var
->is_redundant
, return -2);
1231 isl_assert(tab
->mat
->ctx
, !var
->is_zero
, return -2);
1233 return sign_of_max(tab
, var
);
1236 static int row_is_neg(struct isl_tab
*tab
, int row
)
1239 return isl_int_is_neg(tab
->mat
->row
[row
][1]);
1240 if (isl_int_is_pos(tab
->mat
->row
[row
][2]))
1242 if (isl_int_is_neg(tab
->mat
->row
[row
][2]))
1244 return isl_int_is_neg(tab
->mat
->row
[row
][1]);
1247 static int row_sgn(struct isl_tab
*tab
, int row
)
1250 return isl_int_sgn(tab
->mat
->row
[row
][1]);
1251 if (!isl_int_is_zero(tab
->mat
->row
[row
][2]))
1252 return isl_int_sgn(tab
->mat
->row
[row
][2]);
1254 return isl_int_sgn(tab
->mat
->row
[row
][1]);
1257 /* Perform pivots until the row variable "var" has a non-negative
1258 * sample value or until no more upward pivots can be performed.
1259 * Return the sign of the sample value after the pivots have been
1262 static int restore_row(struct isl_tab
*tab
, struct isl_tab_var
*var
)
1266 while (row_is_neg(tab
, var
->index
)) {
1267 find_pivot(tab
, var
, var
, 1, &row
, &col
);
1270 if (isl_tab_pivot(tab
, row
, col
) < 0)
1272 if (!var
->is_row
) /* manifestly unbounded */
1275 return row_sgn(tab
, var
->index
);
1278 /* Perform pivots until we are sure that the row variable "var"
1279 * can attain non-negative values. After return from this
1280 * function, "var" is still a row variable, but its sample
1281 * value may not be non-negative, even if the function returns 1.
1283 static int at_least_zero(struct isl_tab
*tab
, struct isl_tab_var
*var
)
1287 while (isl_int_is_neg(tab
->mat
->row
[var
->index
][1])) {
1288 find_pivot(tab
, var
, var
, 1, &row
, &col
);
1291 if (row
== var
->index
) /* manifestly unbounded */
1293 if (isl_tab_pivot(tab
, row
, col
) < 0)
1296 return !isl_int_is_neg(tab
->mat
->row
[var
->index
][1]);
1299 /* Return a negative value if "var" can attain negative values.
1300 * Return a non-negative value otherwise.
1302 * If "var" is manifestly unbounded wrt negative values, we are done.
1303 * Otherwise, if var is in a column, we can pivot it down to a row.
1304 * Then we continue pivoting down until either
1305 * - the pivot would result in a manifestly unbounded column
1306 * => we don't perform the pivot, but simply return -1
1307 * - no more down pivots can be performed
1308 * - the sample value is negative
1309 * If the sample value becomes negative and the variable is supposed
1310 * to be nonnegative, then we undo the last pivot.
1311 * However, if the last pivot has made the pivoting variable
1312 * obviously redundant, then it may have moved to another row.
1313 * In that case we look for upward pivots until we reach a non-negative
1316 static int sign_of_min(struct isl_tab
*tab
, struct isl_tab_var
*var
)
1319 struct isl_tab_var
*pivot_var
= NULL
;
1321 if (min_is_manifestly_unbounded(tab
, var
))
1325 row
= pivot_row(tab
, NULL
, -1, col
);
1326 pivot_var
= var_from_col(tab
, col
);
1327 if (isl_tab_pivot(tab
, row
, col
) < 0)
1329 if (var
->is_redundant
)
1331 if (isl_int_is_neg(tab
->mat
->row
[var
->index
][1])) {
1332 if (var
->is_nonneg
) {
1333 if (!pivot_var
->is_redundant
&&
1334 pivot_var
->index
== row
) {
1335 if (isl_tab_pivot(tab
, row
, col
) < 0)
1338 if (restore_row(tab
, var
) < -1)
1344 if (var
->is_redundant
)
1346 while (!isl_int_is_neg(tab
->mat
->row
[var
->index
][1])) {
1347 find_pivot(tab
, var
, var
, -1, &row
, &col
);
1348 if (row
== var
->index
)
1351 return isl_int_sgn(tab
->mat
->row
[var
->index
][1]);
1352 pivot_var
= var_from_col(tab
, col
);
1353 if (isl_tab_pivot(tab
, row
, col
) < 0)
1355 if (var
->is_redundant
)
1358 if (pivot_var
&& var
->is_nonneg
) {
1359 /* pivot back to non-negative value */
1360 if (!pivot_var
->is_redundant
&& pivot_var
->index
== row
) {
1361 if (isl_tab_pivot(tab
, row
, col
) < 0)
1364 if (restore_row(tab
, var
) < -1)
1370 static int row_at_most_neg_one(struct isl_tab
*tab
, int row
)
1373 if (isl_int_is_pos(tab
->mat
->row
[row
][2]))
1375 if (isl_int_is_neg(tab
->mat
->row
[row
][2]))
1378 return isl_int_is_neg(tab
->mat
->row
[row
][1]) &&
1379 isl_int_abs_ge(tab
->mat
->row
[row
][1],
1380 tab
->mat
->row
[row
][0]);
1383 /* Return 1 if "var" can attain values <= -1.
1384 * Return 0 otherwise.
1386 * The sample value of "var" is assumed to be non-negative when the
1387 * the function is called. If 1 is returned then the constraint
1388 * is not redundant and the sample value is made non-negative again before
1389 * the function returns.
1391 int isl_tab_min_at_most_neg_one(struct isl_tab
*tab
, struct isl_tab_var
*var
)
1394 struct isl_tab_var
*pivot_var
;
1396 if (min_is_manifestly_unbounded(tab
, var
))
1400 row
= pivot_row(tab
, NULL
, -1, col
);
1401 pivot_var
= var_from_col(tab
, col
);
1402 if (isl_tab_pivot(tab
, row
, col
) < 0)
1404 if (var
->is_redundant
)
1406 if (row_at_most_neg_one(tab
, var
->index
)) {
1407 if (var
->is_nonneg
) {
1408 if (!pivot_var
->is_redundant
&&
1409 pivot_var
->index
== row
) {
1410 if (isl_tab_pivot(tab
, row
, col
) < 0)
1413 if (restore_row(tab
, var
) < -1)
1419 if (var
->is_redundant
)
1422 find_pivot(tab
, var
, var
, -1, &row
, &col
);
1423 if (row
== var
->index
) {
1424 if (restore_row(tab
, var
) < -1)
1430 pivot_var
= var_from_col(tab
, col
);
1431 if (isl_tab_pivot(tab
, row
, col
) < 0)
1433 if (var
->is_redundant
)
1435 } while (!row_at_most_neg_one(tab
, var
->index
));
1436 if (var
->is_nonneg
) {
1437 /* pivot back to non-negative value */
1438 if (!pivot_var
->is_redundant
&& pivot_var
->index
== row
)
1439 if (isl_tab_pivot(tab
, row
, col
) < 0)
1441 if (restore_row(tab
, var
) < -1)
1447 /* Return 1 if "var" can attain values >= 1.
1448 * Return 0 otherwise.
1450 static int at_least_one(struct isl_tab
*tab
, struct isl_tab_var
*var
)
1455 if (max_is_manifestly_unbounded(tab
, var
))
1457 if (to_row(tab
, var
, 1) < 0)
1459 r
= tab
->mat
->row
[var
->index
];
1460 while (isl_int_lt(r
[1], r
[0])) {
1461 find_pivot(tab
, var
, var
, 1, &row
, &col
);
1463 return isl_int_ge(r
[1], r
[0]);
1464 if (row
== var
->index
) /* manifestly unbounded */
1466 if (isl_tab_pivot(tab
, row
, col
) < 0)
1472 static void swap_cols(struct isl_tab
*tab
, int col1
, int col2
)
1475 unsigned off
= 2 + tab
->M
;
1476 t
= tab
->col_var
[col1
];
1477 tab
->col_var
[col1
] = tab
->col_var
[col2
];
1478 tab
->col_var
[col2
] = t
;
1479 var_from_col(tab
, col1
)->index
= col1
;
1480 var_from_col(tab
, col2
)->index
= col2
;
1481 tab
->mat
= isl_mat_swap_cols(tab
->mat
, off
+ col1
, off
+ col2
);
1484 /* Mark column with index "col" as representing a zero variable.
1485 * If we may need to undo the operation the column is kept,
1486 * but no longer considered.
1487 * Otherwise, the column is simply removed.
1489 * The column may be interchanged with some other column. If it
1490 * is interchanged with a later column, return 1. Otherwise return 0.
1491 * If the columns are checked in order in the calling function,
1492 * then a return value of 1 means that the column with the given
1493 * column number may now contain a different column that
1494 * hasn't been checked yet.
1496 int isl_tab_kill_col(struct isl_tab
*tab
, int col
)
1498 var_from_col(tab
, col
)->is_zero
= 1;
1499 if (tab
->need_undo
) {
1500 if (isl_tab_push_var(tab
, isl_tab_undo_zero
,
1501 var_from_col(tab
, col
)) < 0)
1503 if (col
!= tab
->n_dead
)
1504 swap_cols(tab
, col
, tab
->n_dead
);
1508 if (col
!= tab
->n_col
- 1)
1509 swap_cols(tab
, col
, tab
->n_col
- 1);
1510 var_from_col(tab
, tab
->n_col
- 1)->index
= -1;
1516 /* Row variable "var" is non-negative and cannot attain any values
1517 * larger than zero. This means that the coefficients of the unrestricted
1518 * column variables are zero and that the coefficients of the non-negative
1519 * column variables are zero or negative.
1520 * Each of the non-negative variables with a negative coefficient can
1521 * then also be written as the negative sum of non-negative variables
1522 * and must therefore also be zero.
1524 static int close_row(struct isl_tab
*tab
, struct isl_tab_var
*var
) WARN_UNUSED
;
1525 static int close_row(struct isl_tab
*tab
, struct isl_tab_var
*var
)
1528 struct isl_mat
*mat
= tab
->mat
;
1529 unsigned off
= 2 + tab
->M
;
1531 isl_assert(tab
->mat
->ctx
, var
->is_nonneg
, return -1);
1534 if (isl_tab_push_var(tab
, isl_tab_undo_zero
, var
) < 0)
1536 for (j
= tab
->n_dead
; j
< tab
->n_col
; ++j
) {
1538 if (isl_int_is_zero(mat
->row
[var
->index
][off
+ j
]))
1540 isl_assert(tab
->mat
->ctx
,
1541 isl_int_is_neg(mat
->row
[var
->index
][off
+ j
]), return -1);
1542 recheck
= isl_tab_kill_col(tab
, j
);
1548 if (isl_tab_mark_redundant(tab
, var
->index
) < 0)
1553 /* Add a constraint to the tableau and allocate a row for it.
1554 * Return the index into the constraint array "con".
1556 int isl_tab_allocate_con(struct isl_tab
*tab
)
1560 isl_assert(tab
->mat
->ctx
, tab
->n_row
< tab
->mat
->n_row
, return -1);
1561 isl_assert(tab
->mat
->ctx
, tab
->n_con
< tab
->max_con
, return -1);
1564 tab
->con
[r
].index
= tab
->n_row
;
1565 tab
->con
[r
].is_row
= 1;
1566 tab
->con
[r
].is_nonneg
= 0;
1567 tab
->con
[r
].is_zero
= 0;
1568 tab
->con
[r
].is_redundant
= 0;
1569 tab
->con
[r
].frozen
= 0;
1570 tab
->con
[r
].negated
= 0;
1571 tab
->row_var
[tab
->n_row
] = ~r
;
1575 if (isl_tab_push_var(tab
, isl_tab_undo_allocate
, &tab
->con
[r
]) < 0)
1581 /* Add a variable to the tableau and allocate a column for it.
1582 * Return the index into the variable array "var".
1584 int isl_tab_allocate_var(struct isl_tab
*tab
)
1588 unsigned off
= 2 + tab
->M
;
1590 isl_assert(tab
->mat
->ctx
, tab
->n_col
< tab
->mat
->n_col
, return -1);
1591 isl_assert(tab
->mat
->ctx
, tab
->n_var
< tab
->max_var
, return -1);
1594 tab
->var
[r
].index
= tab
->n_col
;
1595 tab
->var
[r
].is_row
= 0;
1596 tab
->var
[r
].is_nonneg
= 0;
1597 tab
->var
[r
].is_zero
= 0;
1598 tab
->var
[r
].is_redundant
= 0;
1599 tab
->var
[r
].frozen
= 0;
1600 tab
->var
[r
].negated
= 0;
1601 tab
->col_var
[tab
->n_col
] = r
;
1603 for (i
= 0; i
< tab
->n_row
; ++i
)
1604 isl_int_set_si(tab
->mat
->row
[i
][off
+ tab
->n_col
], 0);
1608 if (isl_tab_push_var(tab
, isl_tab_undo_allocate
, &tab
->var
[r
]) < 0)
1614 /* Add a row to the tableau. The row is given as an affine combination
1615 * of the original variables and needs to be expressed in terms of the
1618 * We add each term in turn.
1619 * If r = n/d_r is the current sum and we need to add k x, then
1620 * if x is a column variable, we increase the numerator of
1621 * this column by k d_r
1622 * if x = f/d_x is a row variable, then the new representation of r is
1624 * n k f d_x/g n + d_r/g k f m/d_r n + m/d_g k f
1625 * --- + --- = ------------------- = -------------------
1626 * d_r d_r d_r d_x/g m
1628 * with g the gcd of d_r and d_x and m the lcm of d_r and d_x.
1630 int isl_tab_add_row(struct isl_tab
*tab
, isl_int
*line
)
1636 unsigned off
= 2 + tab
->M
;
1638 r
= isl_tab_allocate_con(tab
);
1644 row
= tab
->mat
->row
[tab
->con
[r
].index
];
1645 isl_int_set_si(row
[0], 1);
1646 isl_int_set(row
[1], line
[0]);
1647 isl_seq_clr(row
+ 2, tab
->M
+ tab
->n_col
);
1648 for (i
= 0; i
< tab
->n_var
; ++i
) {
1649 if (tab
->var
[i
].is_zero
)
1651 if (tab
->var
[i
].is_row
) {
1653 row
[0], tab
->mat
->row
[tab
->var
[i
].index
][0]);
1654 isl_int_swap(a
, row
[0]);
1655 isl_int_divexact(a
, row
[0], a
);
1657 row
[0], tab
->mat
->row
[tab
->var
[i
].index
][0]);
1658 isl_int_mul(b
, b
, line
[1 + i
]);
1659 isl_seq_combine(row
+ 1, a
, row
+ 1,
1660 b
, tab
->mat
->row
[tab
->var
[i
].index
] + 1,
1661 1 + tab
->M
+ tab
->n_col
);
1663 isl_int_addmul(row
[off
+ tab
->var
[i
].index
],
1664 line
[1 + i
], row
[0]);
1665 if (tab
->M
&& i
>= tab
->n_param
&& i
< tab
->n_var
- tab
->n_div
)
1666 isl_int_submul(row
[2], line
[1 + i
], row
[0]);
1668 isl_seq_normalize(tab
->mat
->ctx
, row
, off
+ tab
->n_col
);
1673 tab
->row_sign
[tab
->con
[r
].index
] = isl_tab_row_unknown
;
1678 static int drop_row(struct isl_tab
*tab
, int row
)
1680 isl_assert(tab
->mat
->ctx
, ~tab
->row_var
[row
] == tab
->n_con
- 1, return -1);
1681 if (row
!= tab
->n_row
- 1)
1682 swap_rows(tab
, row
, tab
->n_row
- 1);
1688 static int drop_col(struct isl_tab
*tab
, int col
)
1690 isl_assert(tab
->mat
->ctx
, tab
->col_var
[col
] == tab
->n_var
- 1, return -1);
1691 if (col
!= tab
->n_col
- 1)
1692 swap_cols(tab
, col
, tab
->n_col
- 1);
1698 /* Add inequality "ineq" and check if it conflicts with the
1699 * previously added constraints or if it is obviously redundant.
1701 int isl_tab_add_ineq(struct isl_tab
*tab
, isl_int
*ineq
)
1710 struct isl_basic_map
*bmap
= tab
->bmap
;
1712 isl_assert(tab
->mat
->ctx
, tab
->n_eq
== bmap
->n_eq
, return -1);
1713 isl_assert(tab
->mat
->ctx
,
1714 tab
->n_con
== bmap
->n_eq
+ bmap
->n_ineq
, return -1);
1715 tab
->bmap
= isl_basic_map_add_ineq(tab
->bmap
, ineq
);
1716 if (isl_tab_push(tab
, isl_tab_undo_bmap_ineq
) < 0)
1723 isl_int_swap(ineq
[0], cst
);
1725 r
= isl_tab_add_row(tab
, ineq
);
1727 isl_int_swap(ineq
[0], cst
);
1732 tab
->con
[r
].is_nonneg
= 1;
1733 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1735 if (isl_tab_row_is_redundant(tab
, tab
->con
[r
].index
)) {
1736 if (isl_tab_mark_redundant(tab
, tab
->con
[r
].index
) < 0)
1741 sgn
= restore_row(tab
, &tab
->con
[r
]);
1745 return isl_tab_mark_empty(tab
);
1746 if (tab
->con
[r
].is_row
&& isl_tab_row_is_redundant(tab
, tab
->con
[r
].index
))
1747 if (isl_tab_mark_redundant(tab
, tab
->con
[r
].index
) < 0)
1752 /* Pivot a non-negative variable down until it reaches the value zero
1753 * and then pivot the variable into a column position.
1755 static int to_col(struct isl_tab
*tab
, struct isl_tab_var
*var
) WARN_UNUSED
;
1756 static int to_col(struct isl_tab
*tab
, struct isl_tab_var
*var
)
1760 unsigned off
= 2 + tab
->M
;
1765 while (isl_int_is_pos(tab
->mat
->row
[var
->index
][1])) {
1766 find_pivot(tab
, var
, NULL
, -1, &row
, &col
);
1767 isl_assert(tab
->mat
->ctx
, row
!= -1, return -1);
1768 if (isl_tab_pivot(tab
, row
, col
) < 0)
1774 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
)
1775 if (!isl_int_is_zero(tab
->mat
->row
[var
->index
][off
+ i
]))
1778 isl_assert(tab
->mat
->ctx
, i
< tab
->n_col
, return -1);
1779 if (isl_tab_pivot(tab
, var
->index
, i
) < 0)
1785 /* We assume Gaussian elimination has been performed on the equalities.
1786 * The equalities can therefore never conflict.
1787 * Adding the equalities is currently only really useful for a later call
1788 * to isl_tab_ineq_type.
1790 static struct isl_tab
*add_eq(struct isl_tab
*tab
, isl_int
*eq
)
1797 r
= isl_tab_add_row(tab
, eq
);
1801 r
= tab
->con
[r
].index
;
1802 i
= isl_seq_first_non_zero(tab
->mat
->row
[r
] + 2 + tab
->M
+ tab
->n_dead
,
1803 tab
->n_col
- tab
->n_dead
);
1804 isl_assert(tab
->mat
->ctx
, i
>= 0, goto error
);
1806 if (isl_tab_pivot(tab
, r
, i
) < 0)
1808 if (isl_tab_kill_col(tab
, i
) < 0)
1818 static int row_is_manifestly_zero(struct isl_tab
*tab
, int row
)
1820 unsigned off
= 2 + tab
->M
;
1822 if (!isl_int_is_zero(tab
->mat
->row
[row
][1]))
1824 if (tab
->M
&& !isl_int_is_zero(tab
->mat
->row
[row
][2]))
1826 return isl_seq_first_non_zero(tab
->mat
->row
[row
] + off
+ tab
->n_dead
,
1827 tab
->n_col
- tab
->n_dead
) == -1;
1830 /* Add an equality that is known to be valid for the given tableau.
1832 struct isl_tab
*isl_tab_add_valid_eq(struct isl_tab
*tab
, isl_int
*eq
)
1834 struct isl_tab_var
*var
;
1839 r
= isl_tab_add_row(tab
, eq
);
1845 if (row_is_manifestly_zero(tab
, r
)) {
1847 if (isl_tab_mark_redundant(tab
, r
) < 0)
1852 if (isl_int_is_neg(tab
->mat
->row
[r
][1])) {
1853 isl_seq_neg(tab
->mat
->row
[r
] + 1, tab
->mat
->row
[r
] + 1,
1858 if (to_col(tab
, var
) < 0)
1861 if (isl_tab_kill_col(tab
, var
->index
) < 0)
1870 static int add_zero_row(struct isl_tab
*tab
)
1875 r
= isl_tab_allocate_con(tab
);
1879 row
= tab
->mat
->row
[tab
->con
[r
].index
];
1880 isl_seq_clr(row
+ 1, 1 + tab
->M
+ tab
->n_col
);
1881 isl_int_set_si(row
[0], 1);
1886 /* Add equality "eq" and check if it conflicts with the
1887 * previously added constraints or if it is obviously redundant.
1889 int isl_tab_add_eq(struct isl_tab
*tab
, isl_int
*eq
)
1891 struct isl_tab_undo
*snap
= NULL
;
1892 struct isl_tab_var
*var
;
1900 isl_assert(tab
->mat
->ctx
, !tab
->M
, return -1);
1903 snap
= isl_tab_snap(tab
);
1907 isl_int_swap(eq
[0], cst
);
1909 r
= isl_tab_add_row(tab
, eq
);
1911 isl_int_swap(eq
[0], cst
);
1919 if (row_is_manifestly_zero(tab
, row
)) {
1921 if (isl_tab_rollback(tab
, snap
) < 0)
1929 tab
->bmap
= isl_basic_map_add_ineq(tab
->bmap
, eq
);
1930 if (isl_tab_push(tab
, isl_tab_undo_bmap_ineq
) < 0)
1932 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
1933 tab
->bmap
= isl_basic_map_add_ineq(tab
->bmap
, eq
);
1934 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
1935 if (isl_tab_push(tab
, isl_tab_undo_bmap_ineq
) < 0)
1939 if (add_zero_row(tab
) < 0)
1943 sgn
= isl_int_sgn(tab
->mat
->row
[row
][1]);
1946 isl_seq_neg(tab
->mat
->row
[row
] + 1, tab
->mat
->row
[row
] + 1,
1953 sgn
= sign_of_max(tab
, var
);
1957 if (isl_tab_mark_empty(tab
) < 0)
1964 if (to_col(tab
, var
) < 0)
1967 if (isl_tab_kill_col(tab
, var
->index
) < 0)
1973 /* Construct and return an inequality that expresses an upper bound
1975 * In particular, if the div is given by
1979 * then the inequality expresses
1983 static struct isl_vec
*ineq_for_div(struct isl_basic_map
*bmap
, unsigned div
)
1987 struct isl_vec
*ineq
;
1992 total
= isl_basic_map_total_dim(bmap
);
1993 div_pos
= 1 + total
- bmap
->n_div
+ div
;
1995 ineq
= isl_vec_alloc(bmap
->ctx
, 1 + total
);
1999 isl_seq_cpy(ineq
->el
, bmap
->div
[div
] + 1, 1 + total
);
2000 isl_int_neg(ineq
->el
[div_pos
], bmap
->div
[div
][0]);
2004 /* For a div d = floor(f/m), add the constraints
2007 * -(f-(m-1)) + m d >= 0
2009 * Note that the second constraint is the negation of
2013 * If add_ineq is not NULL, then this function is used
2014 * instead of isl_tab_add_ineq to effectively add the inequalities.
2016 static int add_div_constraints(struct isl_tab
*tab
, unsigned div
,
2017 int (*add_ineq
)(void *user
, isl_int
*), void *user
)
2021 struct isl_vec
*ineq
;
2023 total
= isl_basic_map_total_dim(tab
->bmap
);
2024 div_pos
= 1 + total
- tab
->bmap
->n_div
+ div
;
2026 ineq
= ineq_for_div(tab
->bmap
, div
);
2031 if (add_ineq(user
, ineq
->el
) < 0)
2034 if (isl_tab_add_ineq(tab
, ineq
->el
) < 0)
2038 isl_seq_neg(ineq
->el
, tab
->bmap
->div
[div
] + 1, 1 + total
);
2039 isl_int_set(ineq
->el
[div_pos
], tab
->bmap
->div
[div
][0]);
2040 isl_int_add(ineq
->el
[0], ineq
->el
[0], ineq
->el
[div_pos
]);
2041 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
2044 if (add_ineq(user
, ineq
->el
) < 0)
2047 if (isl_tab_add_ineq(tab
, ineq
->el
) < 0)
2059 /* Add an extra div, prescrived by "div" to the tableau and
2060 * the associated bmap (which is assumed to be non-NULL).
2062 * If add_ineq is not NULL, then this function is used instead
2063 * of isl_tab_add_ineq to add the div constraints.
2064 * This complication is needed because the code in isl_tab_pip
2065 * wants to perform some extra processing when an inequality
2066 * is added to the tableau.
2068 int isl_tab_add_div(struct isl_tab
*tab
, __isl_keep isl_vec
*div
,
2069 int (*add_ineq
)(void *user
, isl_int
*), void *user
)
2079 isl_assert(tab
->mat
->ctx
, tab
->bmap
, return -1);
2081 for (i
= 0; i
< tab
->n_var
; ++i
) {
2082 if (isl_int_is_neg(div
->el
[2 + i
]))
2084 if (isl_int_is_zero(div
->el
[2 + i
]))
2086 if (!tab
->var
[i
].is_nonneg
)
2089 nonneg
= i
== tab
->n_var
&& !isl_int_is_neg(div
->el
[1]);
2091 if (isl_tab_extend_cons(tab
, 3) < 0)
2093 if (isl_tab_extend_vars(tab
, 1) < 0)
2095 r
= isl_tab_allocate_var(tab
);
2100 tab
->var
[r
].is_nonneg
= 1;
2102 tab
->bmap
= isl_basic_map_extend_dim(tab
->bmap
,
2103 isl_basic_map_get_dim(tab
->bmap
), 1, 0, 2);
2104 k
= isl_basic_map_alloc_div(tab
->bmap
);
2107 isl_seq_cpy(tab
->bmap
->div
[k
], div
->el
, div
->size
);
2108 if (isl_tab_push(tab
, isl_tab_undo_bmap_div
) < 0)
2111 if (add_div_constraints(tab
, k
, add_ineq
, user
) < 0)
2117 struct isl_tab
*isl_tab_from_basic_map(struct isl_basic_map
*bmap
)
2120 struct isl_tab
*tab
;
2124 tab
= isl_tab_alloc(bmap
->ctx
,
2125 isl_basic_map_total_dim(bmap
) + bmap
->n_ineq
+ 1,
2126 isl_basic_map_total_dim(bmap
), 0);
2129 tab
->rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
2130 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
)) {
2131 if (isl_tab_mark_empty(tab
) < 0)
2135 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2136 tab
= add_eq(tab
, bmap
->eq
[i
]);
2140 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
2141 if (isl_tab_add_ineq(tab
, bmap
->ineq
[i
]) < 0)
2152 struct isl_tab
*isl_tab_from_basic_set(struct isl_basic_set
*bset
)
2154 return isl_tab_from_basic_map((struct isl_basic_map
*)bset
);
2157 /* Construct a tableau corresponding to the recession cone of "bset".
2159 struct isl_tab
*isl_tab_from_recession_cone(__isl_keep isl_basic_set
*bset
,
2164 struct isl_tab
*tab
;
2165 unsigned offset
= 0;
2170 offset
= isl_basic_set_dim(bset
, isl_dim_param
);
2171 tab
= isl_tab_alloc(bset
->ctx
, bset
->n_eq
+ bset
->n_ineq
,
2172 isl_basic_set_total_dim(bset
) - offset
, 0);
2175 tab
->rational
= ISL_F_ISSET(bset
, ISL_BASIC_SET_RATIONAL
);
2179 for (i
= 0; i
< bset
->n_eq
; ++i
) {
2180 isl_int_swap(bset
->eq
[i
][offset
], cst
);
2182 if (isl_tab_add_eq(tab
, bset
->eq
[i
] + offset
) < 0)
2185 tab
= add_eq(tab
, bset
->eq
[i
]);
2186 isl_int_swap(bset
->eq
[i
][offset
], cst
);
2190 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
2192 isl_int_swap(bset
->ineq
[i
][offset
], cst
);
2193 r
= isl_tab_add_row(tab
, bset
->ineq
[i
] + offset
);
2194 isl_int_swap(bset
->ineq
[i
][offset
], cst
);
2197 tab
->con
[r
].is_nonneg
= 1;
2198 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
2210 /* Assuming "tab" is the tableau of a cone, check if the cone is
2211 * bounded, i.e., if it is empty or only contains the origin.
2213 int isl_tab_cone_is_bounded(struct isl_tab
*tab
)
2221 if (tab
->n_dead
== tab
->n_col
)
2225 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
2226 struct isl_tab_var
*var
;
2228 var
= isl_tab_var_from_row(tab
, i
);
2229 if (!var
->is_nonneg
)
2231 sgn
= sign_of_max(tab
, var
);
2236 if (close_row(tab
, var
) < 0)
2240 if (tab
->n_dead
== tab
->n_col
)
2242 if (i
== tab
->n_row
)
2247 int isl_tab_sample_is_integer(struct isl_tab
*tab
)
2254 for (i
= 0; i
< tab
->n_var
; ++i
) {
2256 if (!tab
->var
[i
].is_row
)
2258 row
= tab
->var
[i
].index
;
2259 if (!isl_int_is_divisible_by(tab
->mat
->row
[row
][1],
2260 tab
->mat
->row
[row
][0]))
2266 static struct isl_vec
*extract_integer_sample(struct isl_tab
*tab
)
2269 struct isl_vec
*vec
;
2271 vec
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_var
);
2275 isl_int_set_si(vec
->block
.data
[0], 1);
2276 for (i
= 0; i
< tab
->n_var
; ++i
) {
2277 if (!tab
->var
[i
].is_row
)
2278 isl_int_set_si(vec
->block
.data
[1 + i
], 0);
2280 int row
= tab
->var
[i
].index
;
2281 isl_int_divexact(vec
->block
.data
[1 + i
],
2282 tab
->mat
->row
[row
][1], tab
->mat
->row
[row
][0]);
2289 struct isl_vec
*isl_tab_get_sample_value(struct isl_tab
*tab
)
2292 struct isl_vec
*vec
;
2298 vec
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_var
);
2304 isl_int_set_si(vec
->block
.data
[0], 1);
2305 for (i
= 0; i
< tab
->n_var
; ++i
) {
2307 if (!tab
->var
[i
].is_row
) {
2308 isl_int_set_si(vec
->block
.data
[1 + i
], 0);
2311 row
= tab
->var
[i
].index
;
2312 isl_int_gcd(m
, vec
->block
.data
[0], tab
->mat
->row
[row
][0]);
2313 isl_int_divexact(m
, tab
->mat
->row
[row
][0], m
);
2314 isl_seq_scale(vec
->block
.data
, vec
->block
.data
, m
, 1 + i
);
2315 isl_int_divexact(m
, vec
->block
.data
[0], tab
->mat
->row
[row
][0]);
2316 isl_int_mul(vec
->block
.data
[1 + i
], m
, tab
->mat
->row
[row
][1]);
2318 vec
= isl_vec_normalize(vec
);
2324 /* Update "bmap" based on the results of the tableau "tab".
2325 * In particular, implicit equalities are made explicit, redundant constraints
2326 * are removed and if the sample value happens to be integer, it is stored
2327 * in "bmap" (unless "bmap" already had an integer sample).
2329 * The tableau is assumed to have been created from "bmap" using
2330 * isl_tab_from_basic_map.
2332 struct isl_basic_map
*isl_basic_map_update_from_tab(struct isl_basic_map
*bmap
,
2333 struct isl_tab
*tab
)
2345 bmap
= isl_basic_map_set_to_empty(bmap
);
2347 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
2348 if (isl_tab_is_equality(tab
, n_eq
+ i
))
2349 isl_basic_map_inequality_to_equality(bmap
, i
);
2350 else if (isl_tab_is_redundant(tab
, n_eq
+ i
))
2351 isl_basic_map_drop_inequality(bmap
, i
);
2353 if (bmap
->n_eq
!= n_eq
)
2354 isl_basic_map_gauss(bmap
, NULL
);
2355 if (!tab
->rational
&&
2356 !bmap
->sample
&& isl_tab_sample_is_integer(tab
))
2357 bmap
->sample
= extract_integer_sample(tab
);
2361 struct isl_basic_set
*isl_basic_set_update_from_tab(struct isl_basic_set
*bset
,
2362 struct isl_tab
*tab
)
2364 return (struct isl_basic_set
*)isl_basic_map_update_from_tab(
2365 (struct isl_basic_map
*)bset
, tab
);
2368 /* Given a non-negative variable "var", add a new non-negative variable
2369 * that is the opposite of "var", ensuring that var can only attain the
2371 * If var = n/d is a row variable, then the new variable = -n/d.
2372 * If var is a column variables, then the new variable = -var.
2373 * If the new variable cannot attain non-negative values, then
2374 * the resulting tableau is empty.
2375 * Otherwise, we know the value will be zero and we close the row.
2377 static int cut_to_hyperplane(struct isl_tab
*tab
, struct isl_tab_var
*var
)
2382 unsigned off
= 2 + tab
->M
;
2386 isl_assert(tab
->mat
->ctx
, !var
->is_redundant
, return -1);
2387 isl_assert(tab
->mat
->ctx
, var
->is_nonneg
, return -1);
2389 if (isl_tab_extend_cons(tab
, 1) < 0)
2393 tab
->con
[r
].index
= tab
->n_row
;
2394 tab
->con
[r
].is_row
= 1;
2395 tab
->con
[r
].is_nonneg
= 0;
2396 tab
->con
[r
].is_zero
= 0;
2397 tab
->con
[r
].is_redundant
= 0;
2398 tab
->con
[r
].frozen
= 0;
2399 tab
->con
[r
].negated
= 0;
2400 tab
->row_var
[tab
->n_row
] = ~r
;
2401 row
= tab
->mat
->row
[tab
->n_row
];
2404 isl_int_set(row
[0], tab
->mat
->row
[var
->index
][0]);
2405 isl_seq_neg(row
+ 1,
2406 tab
->mat
->row
[var
->index
] + 1, 1 + tab
->n_col
);
2408 isl_int_set_si(row
[0], 1);
2409 isl_seq_clr(row
+ 1, 1 + tab
->n_col
);
2410 isl_int_set_si(row
[off
+ var
->index
], -1);
2415 if (isl_tab_push_var(tab
, isl_tab_undo_allocate
, &tab
->con
[r
]) < 0)
2418 sgn
= sign_of_max(tab
, &tab
->con
[r
]);
2422 if (isl_tab_mark_empty(tab
) < 0)
2426 tab
->con
[r
].is_nonneg
= 1;
2427 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
2430 if (close_row(tab
, &tab
->con
[r
]) < 0)
2436 /* Given a tableau "tab" and an inequality constraint "con" of the tableau,
2437 * relax the inequality by one. That is, the inequality r >= 0 is replaced
2438 * by r' = r + 1 >= 0.
2439 * If r is a row variable, we simply increase the constant term by one
2440 * (taking into account the denominator).
2441 * If r is a column variable, then we need to modify each row that
2442 * refers to r = r' - 1 by substituting this equality, effectively
2443 * subtracting the coefficient of the column from the constant.
2444 * We should only do this if the minimum is manifestly unbounded,
2445 * however. Otherwise, we may end up with negative sample values
2446 * for non-negative variables.
2447 * So, if r is a column variable with a minimum that is not
2448 * manifestly unbounded, then we need to move it to a row.
2449 * However, the sample value of this row may be negative,
2450 * even after the relaxation, so we need to restore it.
2451 * We therefore prefer to pivot a column up to a row, if possible.
2453 struct isl_tab
*isl_tab_relax(struct isl_tab
*tab
, int con
)
2455 struct isl_tab_var
*var
;
2456 unsigned off
= 2 + tab
->M
;
2461 var
= &tab
->con
[con
];
2463 if (!var
->is_row
&& !max_is_manifestly_unbounded(tab
, var
))
2464 if (to_row(tab
, var
, 1) < 0)
2466 if (!var
->is_row
&& !min_is_manifestly_unbounded(tab
, var
))
2467 if (to_row(tab
, var
, -1) < 0)
2471 isl_int_add(tab
->mat
->row
[var
->index
][1],
2472 tab
->mat
->row
[var
->index
][1], tab
->mat
->row
[var
->index
][0]);
2473 if (restore_row(tab
, var
) < 0)
2478 for (i
= 0; i
< tab
->n_row
; ++i
) {
2479 if (isl_int_is_zero(tab
->mat
->row
[i
][off
+ var
->index
]))
2481 isl_int_sub(tab
->mat
->row
[i
][1], tab
->mat
->row
[i
][1],
2482 tab
->mat
->row
[i
][off
+ var
->index
]);
2487 if (isl_tab_push_var(tab
, isl_tab_undo_relax
, var
) < 0)
2496 int isl_tab_select_facet(struct isl_tab
*tab
, int con
)
2501 return cut_to_hyperplane(tab
, &tab
->con
[con
]);
2504 static int may_be_equality(struct isl_tab
*tab
, int row
)
2506 unsigned off
= 2 + tab
->M
;
2507 return tab
->rational
? isl_int_is_zero(tab
->mat
->row
[row
][1])
2508 : isl_int_lt(tab
->mat
->row
[row
][1],
2509 tab
->mat
->row
[row
][0]);
2512 /* Check for (near) equalities among the constraints.
2513 * A constraint is an equality if it is non-negative and if
2514 * its maximal value is either
2515 * - zero (in case of rational tableaus), or
2516 * - strictly less than 1 (in case of integer tableaus)
2518 * We first mark all non-redundant and non-dead variables that
2519 * are not frozen and not obviously not an equality.
2520 * Then we iterate over all marked variables if they can attain
2521 * any values larger than zero or at least one.
2522 * If the maximal value is zero, we mark any column variables
2523 * that appear in the row as being zero and mark the row as being redundant.
2524 * Otherwise, if the maximal value is strictly less than one (and the
2525 * tableau is integer), then we restrict the value to being zero
2526 * by adding an opposite non-negative variable.
2528 int isl_tab_detect_implicit_equalities(struct isl_tab
*tab
)
2537 if (tab
->n_dead
== tab
->n_col
)
2541 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
2542 struct isl_tab_var
*var
= isl_tab_var_from_row(tab
, i
);
2543 var
->marked
= !var
->frozen
&& var
->is_nonneg
&&
2544 may_be_equality(tab
, i
);
2548 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
2549 struct isl_tab_var
*var
= var_from_col(tab
, i
);
2550 var
->marked
= !var
->frozen
&& var
->is_nonneg
;
2555 struct isl_tab_var
*var
;
2557 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
2558 var
= isl_tab_var_from_row(tab
, i
);
2562 if (i
== tab
->n_row
) {
2563 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
2564 var
= var_from_col(tab
, i
);
2568 if (i
== tab
->n_col
)
2573 sgn
= sign_of_max(tab
, var
);
2577 if (close_row(tab
, var
) < 0)
2579 } else if (!tab
->rational
&& !at_least_one(tab
, var
)) {
2580 if (cut_to_hyperplane(tab
, var
) < 0)
2582 return isl_tab_detect_implicit_equalities(tab
);
2584 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
2585 var
= isl_tab_var_from_row(tab
, i
);
2588 if (may_be_equality(tab
, i
))
2598 static int con_is_redundant(struct isl_tab
*tab
, struct isl_tab_var
*var
)
2602 if (tab
->rational
) {
2603 int sgn
= sign_of_min(tab
, var
);
2608 int irred
= isl_tab_min_at_most_neg_one(tab
, var
);
2615 /* Check for (near) redundant constraints.
2616 * A constraint is redundant if it is non-negative and if
2617 * its minimal value (temporarily ignoring the non-negativity) is either
2618 * - zero (in case of rational tableaus), or
2619 * - strictly larger than -1 (in case of integer tableaus)
2621 * We first mark all non-redundant and non-dead variables that
2622 * are not frozen and not obviously negatively unbounded.
2623 * Then we iterate over all marked variables if they can attain
2624 * any values smaller than zero or at most negative one.
2625 * If not, we mark the row as being redundant (assuming it hasn't
2626 * been detected as being obviously redundant in the mean time).
2628 int isl_tab_detect_redundant(struct isl_tab
*tab
)
2637 if (tab
->n_redundant
== tab
->n_row
)
2641 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
2642 struct isl_tab_var
*var
= isl_tab_var_from_row(tab
, i
);
2643 var
->marked
= !var
->frozen
&& var
->is_nonneg
;
2647 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
2648 struct isl_tab_var
*var
= var_from_col(tab
, i
);
2649 var
->marked
= !var
->frozen
&& var
->is_nonneg
&&
2650 !min_is_manifestly_unbounded(tab
, var
);
2655 struct isl_tab_var
*var
;
2657 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
2658 var
= isl_tab_var_from_row(tab
, i
);
2662 if (i
== tab
->n_row
) {
2663 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
2664 var
= var_from_col(tab
, i
);
2668 if (i
== tab
->n_col
)
2673 red
= con_is_redundant(tab
, var
);
2676 if (red
&& !var
->is_redundant
)
2677 if (isl_tab_mark_redundant(tab
, var
->index
) < 0)
2679 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
2680 var
= var_from_col(tab
, i
);
2683 if (!min_is_manifestly_unbounded(tab
, var
))
2693 int isl_tab_is_equality(struct isl_tab
*tab
, int con
)
2700 if (tab
->con
[con
].is_zero
)
2702 if (tab
->con
[con
].is_redundant
)
2704 if (!tab
->con
[con
].is_row
)
2705 return tab
->con
[con
].index
< tab
->n_dead
;
2707 row
= tab
->con
[con
].index
;
2710 return isl_int_is_zero(tab
->mat
->row
[row
][1]) &&
2711 isl_seq_first_non_zero(tab
->mat
->row
[row
] + 2 + tab
->n_dead
,
2712 tab
->n_col
- tab
->n_dead
) == -1;
2715 /* Return the minimial value of the affine expression "f" with denominator
2716 * "denom" in *opt, *opt_denom, assuming the tableau is not empty and
2717 * the expression cannot attain arbitrarily small values.
2718 * If opt_denom is NULL, then *opt is rounded up to the nearest integer.
2719 * The return value reflects the nature of the result (empty, unbounded,
2720 * minmimal value returned in *opt).
2722 enum isl_lp_result
isl_tab_min(struct isl_tab
*tab
,
2723 isl_int
*f
, isl_int denom
, isl_int
*opt
, isl_int
*opt_denom
,
2727 enum isl_lp_result res
= isl_lp_ok
;
2728 struct isl_tab_var
*var
;
2729 struct isl_tab_undo
*snap
;
2732 return isl_lp_empty
;
2734 snap
= isl_tab_snap(tab
);
2735 r
= isl_tab_add_row(tab
, f
);
2737 return isl_lp_error
;
2739 isl_int_mul(tab
->mat
->row
[var
->index
][0],
2740 tab
->mat
->row
[var
->index
][0], denom
);
2743 find_pivot(tab
, var
, var
, -1, &row
, &col
);
2744 if (row
== var
->index
) {
2745 res
= isl_lp_unbounded
;
2750 if (isl_tab_pivot(tab
, row
, col
) < 0)
2751 return isl_lp_error
;
2753 if (ISL_FL_ISSET(flags
, ISL_TAB_SAVE_DUAL
)) {
2756 isl_vec_free(tab
->dual
);
2757 tab
->dual
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_con
);
2759 return isl_lp_error
;
2760 isl_int_set(tab
->dual
->el
[0], tab
->mat
->row
[var
->index
][0]);
2761 for (i
= 0; i
< tab
->n_con
; ++i
) {
2763 if (tab
->con
[i
].is_row
) {
2764 isl_int_set_si(tab
->dual
->el
[1 + i
], 0);
2767 pos
= 2 + tab
->M
+ tab
->con
[i
].index
;
2768 if (tab
->con
[i
].negated
)
2769 isl_int_neg(tab
->dual
->el
[1 + i
],
2770 tab
->mat
->row
[var
->index
][pos
]);
2772 isl_int_set(tab
->dual
->el
[1 + i
],
2773 tab
->mat
->row
[var
->index
][pos
]);
2776 if (opt
&& res
== isl_lp_ok
) {
2778 isl_int_set(*opt
, tab
->mat
->row
[var
->index
][1]);
2779 isl_int_set(*opt_denom
, tab
->mat
->row
[var
->index
][0]);
2781 isl_int_cdiv_q(*opt
, tab
->mat
->row
[var
->index
][1],
2782 tab
->mat
->row
[var
->index
][0]);
2784 if (isl_tab_rollback(tab
, snap
) < 0)
2785 return isl_lp_error
;
2789 int isl_tab_is_redundant(struct isl_tab
*tab
, int con
)
2793 if (tab
->con
[con
].is_zero
)
2795 if (tab
->con
[con
].is_redundant
)
2797 return tab
->con
[con
].is_row
&& tab
->con
[con
].index
< tab
->n_redundant
;
2800 /* Take a snapshot of the tableau that can be restored by s call to
2803 struct isl_tab_undo
*isl_tab_snap(struct isl_tab
*tab
)
2811 /* Undo the operation performed by isl_tab_relax.
2813 static int unrelax(struct isl_tab
*tab
, struct isl_tab_var
*var
) WARN_UNUSED
;
2814 static int unrelax(struct isl_tab
*tab
, struct isl_tab_var
*var
)
2816 unsigned off
= 2 + tab
->M
;
2818 if (!var
->is_row
&& !max_is_manifestly_unbounded(tab
, var
))
2819 if (to_row(tab
, var
, 1) < 0)
2823 isl_int_sub(tab
->mat
->row
[var
->index
][1],
2824 tab
->mat
->row
[var
->index
][1], tab
->mat
->row
[var
->index
][0]);
2825 if (var
->is_nonneg
) {
2826 int sgn
= restore_row(tab
, var
);
2827 isl_assert(tab
->mat
->ctx
, sgn
>= 0, return -1);
2832 for (i
= 0; i
< tab
->n_row
; ++i
) {
2833 if (isl_int_is_zero(tab
->mat
->row
[i
][off
+ var
->index
]))
2835 isl_int_add(tab
->mat
->row
[i
][1], tab
->mat
->row
[i
][1],
2836 tab
->mat
->row
[i
][off
+ var
->index
]);
2844 static int perform_undo_var(struct isl_tab
*tab
, struct isl_tab_undo
*undo
) WARN_UNUSED
;
2845 static int perform_undo_var(struct isl_tab
*tab
, struct isl_tab_undo
*undo
)
2847 struct isl_tab_var
*var
= var_from_index(tab
, undo
->u
.var_index
);
2848 switch(undo
->type
) {
2849 case isl_tab_undo_nonneg
:
2852 case isl_tab_undo_redundant
:
2853 var
->is_redundant
= 0;
2855 restore_row(tab
, isl_tab_var_from_row(tab
, tab
->n_redundant
));
2857 case isl_tab_undo_freeze
:
2860 case isl_tab_undo_zero
:
2865 case isl_tab_undo_allocate
:
2866 if (undo
->u
.var_index
>= 0) {
2867 isl_assert(tab
->mat
->ctx
, !var
->is_row
, return -1);
2868 drop_col(tab
, var
->index
);
2872 if (!max_is_manifestly_unbounded(tab
, var
)) {
2873 if (to_row(tab
, var
, 1) < 0)
2875 } else if (!min_is_manifestly_unbounded(tab
, var
)) {
2876 if (to_row(tab
, var
, -1) < 0)
2879 if (to_row(tab
, var
, 0) < 0)
2882 drop_row(tab
, var
->index
);
2884 case isl_tab_undo_relax
:
2885 return unrelax(tab
, var
);
2891 /* Restore the tableau to the state where the basic variables
2892 * are those in "col_var".
2893 * We first construct a list of variables that are currently in
2894 * the basis, but shouldn't. Then we iterate over all variables
2895 * that should be in the basis and for each one that is currently
2896 * not in the basis, we exchange it with one of the elements of the
2897 * list constructed before.
2898 * We can always find an appropriate variable to pivot with because
2899 * the current basis is mapped to the old basis by a non-singular
2900 * matrix and so we can never end up with a zero row.
2902 static int restore_basis(struct isl_tab
*tab
, int *col_var
)
2906 int *extra
= NULL
; /* current columns that contain bad stuff */
2907 unsigned off
= 2 + tab
->M
;
2909 extra
= isl_alloc_array(tab
->mat
->ctx
, int, tab
->n_col
);
2912 for (i
= 0; i
< tab
->n_col
; ++i
) {
2913 for (j
= 0; j
< tab
->n_col
; ++j
)
2914 if (tab
->col_var
[i
] == col_var
[j
])
2918 extra
[n_extra
++] = i
;
2920 for (i
= 0; i
< tab
->n_col
&& n_extra
> 0; ++i
) {
2921 struct isl_tab_var
*var
;
2924 for (j
= 0; j
< tab
->n_col
; ++j
)
2925 if (col_var
[i
] == tab
->col_var
[j
])
2929 var
= var_from_index(tab
, col_var
[i
]);
2931 for (j
= 0; j
< n_extra
; ++j
)
2932 if (!isl_int_is_zero(tab
->mat
->row
[row
][off
+extra
[j
]]))
2934 isl_assert(tab
->mat
->ctx
, j
< n_extra
, goto error
);
2935 if (isl_tab_pivot(tab
, row
, extra
[j
]) < 0)
2937 extra
[j
] = extra
[--n_extra
];
2949 /* Remove all samples with index n or greater, i.e., those samples
2950 * that were added since we saved this number of samples in
2951 * isl_tab_save_samples.
2953 static void drop_samples_since(struct isl_tab
*tab
, int n
)
2957 for (i
= tab
->n_sample
- 1; i
>= 0 && tab
->n_sample
> n
; --i
) {
2958 if (tab
->sample_index
[i
] < n
)
2961 if (i
!= tab
->n_sample
- 1) {
2962 int t
= tab
->sample_index
[tab
->n_sample
-1];
2963 tab
->sample_index
[tab
->n_sample
-1] = tab
->sample_index
[i
];
2964 tab
->sample_index
[i
] = t
;
2965 isl_mat_swap_rows(tab
->samples
, tab
->n_sample
-1, i
);
2971 static int perform_undo(struct isl_tab
*tab
, struct isl_tab_undo
*undo
) WARN_UNUSED
;
2972 static int perform_undo(struct isl_tab
*tab
, struct isl_tab_undo
*undo
)
2974 switch (undo
->type
) {
2975 case isl_tab_undo_empty
:
2978 case isl_tab_undo_nonneg
:
2979 case isl_tab_undo_redundant
:
2980 case isl_tab_undo_freeze
:
2981 case isl_tab_undo_zero
:
2982 case isl_tab_undo_allocate
:
2983 case isl_tab_undo_relax
:
2984 return perform_undo_var(tab
, undo
);
2985 case isl_tab_undo_bmap_eq
:
2986 return isl_basic_map_free_equality(tab
->bmap
, 1);
2987 case isl_tab_undo_bmap_ineq
:
2988 return isl_basic_map_free_inequality(tab
->bmap
, 1);
2989 case isl_tab_undo_bmap_div
:
2990 if (isl_basic_map_free_div(tab
->bmap
, 1) < 0)
2993 tab
->samples
->n_col
--;
2995 case isl_tab_undo_saved_basis
:
2996 if (restore_basis(tab
, undo
->u
.col_var
) < 0)
2999 case isl_tab_undo_drop_sample
:
3002 case isl_tab_undo_saved_samples
:
3003 drop_samples_since(tab
, undo
->u
.n
);
3005 case isl_tab_undo_callback
:
3006 return undo
->u
.callback
->run(undo
->u
.callback
);
3008 isl_assert(tab
->mat
->ctx
, 0, return -1);
3013 /* Return the tableau to the state it was in when the snapshot "snap"
3016 int isl_tab_rollback(struct isl_tab
*tab
, struct isl_tab_undo
*snap
)
3018 struct isl_tab_undo
*undo
, *next
;
3024 for (undo
= tab
->top
; undo
&& undo
!= &tab
->bottom
; undo
= next
) {
3028 if (perform_undo(tab
, undo
) < 0) {
3042 /* The given row "row" represents an inequality violated by all
3043 * points in the tableau. Check for some special cases of such
3044 * separating constraints.
3045 * In particular, if the row has been reduced to the constant -1,
3046 * then we know the inequality is adjacent (but opposite) to
3047 * an equality in the tableau.
3048 * If the row has been reduced to r = -1 -r', with r' an inequality
3049 * of the tableau, then the inequality is adjacent (but opposite)
3050 * to the inequality r'.
3052 static enum isl_ineq_type
separation_type(struct isl_tab
*tab
, unsigned row
)
3055 unsigned off
= 2 + tab
->M
;
3058 return isl_ineq_separate
;
3060 if (!isl_int_is_one(tab
->mat
->row
[row
][0]))
3061 return isl_ineq_separate
;
3062 if (!isl_int_is_negone(tab
->mat
->row
[row
][1]))
3063 return isl_ineq_separate
;
3065 pos
= isl_seq_first_non_zero(tab
->mat
->row
[row
] + off
+ tab
->n_dead
,
3066 tab
->n_col
- tab
->n_dead
);
3068 return isl_ineq_adj_eq
;
3070 if (!isl_int_is_negone(tab
->mat
->row
[row
][off
+ tab
->n_dead
+ pos
]))
3071 return isl_ineq_separate
;
3073 pos
= isl_seq_first_non_zero(
3074 tab
->mat
->row
[row
] + off
+ tab
->n_dead
+ pos
+ 1,
3075 tab
->n_col
- tab
->n_dead
- pos
- 1);
3077 return pos
== -1 ? isl_ineq_adj_ineq
: isl_ineq_separate
;
3080 /* Check the effect of inequality "ineq" on the tableau "tab".
3082 * isl_ineq_redundant: satisfied by all points in the tableau
3083 * isl_ineq_separate: satisfied by no point in the tableau
3084 * isl_ineq_cut: satisfied by some by not all points
3085 * isl_ineq_adj_eq: adjacent to an equality
3086 * isl_ineq_adj_ineq: adjacent to an inequality.
3088 enum isl_ineq_type
isl_tab_ineq_type(struct isl_tab
*tab
, isl_int
*ineq
)
3090 enum isl_ineq_type type
= isl_ineq_error
;
3091 struct isl_tab_undo
*snap
= NULL
;
3096 return isl_ineq_error
;
3098 if (isl_tab_extend_cons(tab
, 1) < 0)
3099 return isl_ineq_error
;
3101 snap
= isl_tab_snap(tab
);
3103 con
= isl_tab_add_row(tab
, ineq
);
3107 row
= tab
->con
[con
].index
;
3108 if (isl_tab_row_is_redundant(tab
, row
))
3109 type
= isl_ineq_redundant
;
3110 else if (isl_int_is_neg(tab
->mat
->row
[row
][1]) &&
3112 isl_int_abs_ge(tab
->mat
->row
[row
][1],
3113 tab
->mat
->row
[row
][0]))) {
3114 int nonneg
= at_least_zero(tab
, &tab
->con
[con
]);
3118 type
= isl_ineq_cut
;
3120 type
= separation_type(tab
, row
);
3122 int red
= con_is_redundant(tab
, &tab
->con
[con
]);
3126 type
= isl_ineq_cut
;
3128 type
= isl_ineq_redundant
;
3131 if (isl_tab_rollback(tab
, snap
))
3132 return isl_ineq_error
;
3135 return isl_ineq_error
;
3138 int isl_tab_track_bmap(struct isl_tab
*tab
, __isl_take isl_basic_map
*bmap
)
3143 isl_assert(tab
->mat
->ctx
, tab
->n_eq
== bmap
->n_eq
, return -1);
3144 isl_assert(tab
->mat
->ctx
,
3145 tab
->n_con
== bmap
->n_eq
+ bmap
->n_ineq
, return -1);
3151 isl_basic_map_free(bmap
);
3155 int isl_tab_track_bset(struct isl_tab
*tab
, __isl_take isl_basic_set
*bset
)
3157 return isl_tab_track_bmap(tab
, (isl_basic_map
*)bset
);
3160 __isl_keep isl_basic_set
*isl_tab_peek_bset(struct isl_tab
*tab
)
3165 return (isl_basic_set
*)tab
->bmap
;
3168 void isl_tab_dump(struct isl_tab
*tab
, FILE *out
, int indent
)
3174 fprintf(out
, "%*snull tab\n", indent
, "");
3177 fprintf(out
, "%*sn_redundant: %d, n_dead: %d", indent
, "",
3178 tab
->n_redundant
, tab
->n_dead
);
3180 fprintf(out
, ", rational");
3182 fprintf(out
, ", empty");
3184 fprintf(out
, "%*s[", indent
, "");
3185 for (i
= 0; i
< tab
->n_var
; ++i
) {
3187 fprintf(out
, (i
== tab
->n_param
||
3188 i
== tab
->n_var
- tab
->n_div
) ? "; "
3190 fprintf(out
, "%c%d%s", tab
->var
[i
].is_row
? 'r' : 'c',
3192 tab
->var
[i
].is_zero
? " [=0]" :
3193 tab
->var
[i
].is_redundant
? " [R]" : "");
3195 fprintf(out
, "]\n");
3196 fprintf(out
, "%*s[", indent
, "");
3197 for (i
= 0; i
< tab
->n_con
; ++i
) {
3200 fprintf(out
, "%c%d%s", tab
->con
[i
].is_row
? 'r' : 'c',
3202 tab
->con
[i
].is_zero
? " [=0]" :
3203 tab
->con
[i
].is_redundant
? " [R]" : "");
3205 fprintf(out
, "]\n");
3206 fprintf(out
, "%*s[", indent
, "");
3207 for (i
= 0; i
< tab
->n_row
; ++i
) {
3208 const char *sign
= "";
3211 if (tab
->row_sign
) {
3212 if (tab
->row_sign
[i
] == isl_tab_row_unknown
)
3214 else if (tab
->row_sign
[i
] == isl_tab_row_neg
)
3216 else if (tab
->row_sign
[i
] == isl_tab_row_pos
)
3221 fprintf(out
, "r%d: %d%s%s", i
, tab
->row_var
[i
],
3222 isl_tab_var_from_row(tab
, i
)->is_nonneg
? " [>=0]" : "", sign
);
3224 fprintf(out
, "]\n");
3225 fprintf(out
, "%*s[", indent
, "");
3226 for (i
= 0; i
< tab
->n_col
; ++i
) {
3229 fprintf(out
, "c%d: %d%s", i
, tab
->col_var
[i
],
3230 var_from_col(tab
, i
)->is_nonneg
? " [>=0]" : "");
3232 fprintf(out
, "]\n");
3233 r
= tab
->mat
->n_row
;
3234 tab
->mat
->n_row
= tab
->n_row
;
3235 c
= tab
->mat
->n_col
;
3236 tab
->mat
->n_col
= 2 + tab
->M
+ tab
->n_col
;
3237 isl_mat_dump(tab
->mat
, out
, indent
);
3238 tab
->mat
->n_row
= r
;
3239 tab
->mat
->n_col
= c
;
3241 isl_basic_map_dump(tab
->bmap
, out
, indent
);