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
10 #include <isl_ctx_private.h>
11 #include <isl_mat_private.h>
12 #include "isl_map_private.h"
17 * The implementation of tableaus in this file was inspired by Section 8
18 * of David Detlefs, Greg Nelson and James B. Saxe, "Simplify: a theorem
19 * prover for program checking".
22 struct isl_tab
*isl_tab_alloc(struct isl_ctx
*ctx
,
23 unsigned n_row
, unsigned n_var
, unsigned M
)
29 tab
= isl_calloc_type(ctx
, struct isl_tab
);
32 tab
->mat
= isl_mat_alloc(ctx
, n_row
, off
+ n_var
);
35 tab
->var
= isl_alloc_array(ctx
, struct isl_tab_var
, n_var
);
38 tab
->con
= isl_alloc_array(ctx
, struct isl_tab_var
, n_row
);
41 tab
->col_var
= isl_alloc_array(ctx
, int, n_var
);
44 tab
->row_var
= isl_alloc_array(ctx
, int, n_row
);
47 for (i
= 0; i
< n_var
; ++i
) {
48 tab
->var
[i
].index
= i
;
49 tab
->var
[i
].is_row
= 0;
50 tab
->var
[i
].is_nonneg
= 0;
51 tab
->var
[i
].is_zero
= 0;
52 tab
->var
[i
].is_redundant
= 0;
53 tab
->var
[i
].frozen
= 0;
54 tab
->var
[i
].negated
= 0;
68 tab
->strict_redundant
= 0;
75 tab
->bottom
.type
= isl_tab_undo_bottom
;
76 tab
->bottom
.next
= NULL
;
77 tab
->top
= &tab
->bottom
;
89 int isl_tab_extend_cons(struct isl_tab
*tab
, unsigned n_new
)
98 if (tab
->max_con
< tab
->n_con
+ n_new
) {
99 struct isl_tab_var
*con
;
101 con
= isl_realloc_array(tab
->mat
->ctx
, tab
->con
,
102 struct isl_tab_var
, tab
->max_con
+ n_new
);
106 tab
->max_con
+= n_new
;
108 if (tab
->mat
->n_row
< tab
->n_row
+ n_new
) {
111 tab
->mat
= isl_mat_extend(tab
->mat
,
112 tab
->n_row
+ n_new
, off
+ tab
->n_col
);
115 row_var
= isl_realloc_array(tab
->mat
->ctx
, tab
->row_var
,
116 int, tab
->mat
->n_row
);
119 tab
->row_var
= row_var
;
121 enum isl_tab_row_sign
*s
;
122 s
= isl_realloc_array(tab
->mat
->ctx
, tab
->row_sign
,
123 enum isl_tab_row_sign
, tab
->mat
->n_row
);
132 /* Make room for at least n_new extra variables.
133 * Return -1 if anything went wrong.
135 int isl_tab_extend_vars(struct isl_tab
*tab
, unsigned n_new
)
137 struct isl_tab_var
*var
;
138 unsigned off
= 2 + tab
->M
;
140 if (tab
->max_var
< tab
->n_var
+ n_new
) {
141 var
= isl_realloc_array(tab
->mat
->ctx
, tab
->var
,
142 struct isl_tab_var
, tab
->n_var
+ n_new
);
146 tab
->max_var
+= n_new
;
149 if (tab
->mat
->n_col
< off
+ tab
->n_col
+ n_new
) {
152 tab
->mat
= isl_mat_extend(tab
->mat
,
153 tab
->mat
->n_row
, off
+ tab
->n_col
+ n_new
);
156 p
= isl_realloc_array(tab
->mat
->ctx
, tab
->col_var
,
157 int, tab
->n_col
+ n_new
);
166 struct isl_tab
*isl_tab_extend(struct isl_tab
*tab
, unsigned n_new
)
168 if (isl_tab_extend_cons(tab
, n_new
) >= 0)
175 static void free_undo(struct isl_tab
*tab
)
177 struct isl_tab_undo
*undo
, *next
;
179 for (undo
= tab
->top
; undo
&& undo
!= &tab
->bottom
; undo
= next
) {
186 void isl_tab_free(struct isl_tab
*tab
)
191 isl_mat_free(tab
->mat
);
192 isl_vec_free(tab
->dual
);
193 isl_basic_map_free(tab
->bmap
);
199 isl_mat_free(tab
->samples
);
200 free(tab
->sample_index
);
201 isl_mat_free(tab
->basis
);
205 struct isl_tab
*isl_tab_dup(struct isl_tab
*tab
)
215 dup
= isl_calloc_type(tab
->mat
->ctx
, struct isl_tab
);
218 dup
->mat
= isl_mat_dup(tab
->mat
);
221 dup
->var
= isl_alloc_array(tab
->mat
->ctx
, struct isl_tab_var
, tab
->max_var
);
224 for (i
= 0; i
< tab
->n_var
; ++i
)
225 dup
->var
[i
] = tab
->var
[i
];
226 dup
->con
= isl_alloc_array(tab
->mat
->ctx
, struct isl_tab_var
, tab
->max_con
);
229 for (i
= 0; i
< tab
->n_con
; ++i
)
230 dup
->con
[i
] = tab
->con
[i
];
231 dup
->col_var
= isl_alloc_array(tab
->mat
->ctx
, int, tab
->mat
->n_col
- off
);
234 for (i
= 0; i
< tab
->n_col
; ++i
)
235 dup
->col_var
[i
] = tab
->col_var
[i
];
236 dup
->row_var
= isl_alloc_array(tab
->mat
->ctx
, int, tab
->mat
->n_row
);
239 for (i
= 0; i
< tab
->n_row
; ++i
)
240 dup
->row_var
[i
] = tab
->row_var
[i
];
242 dup
->row_sign
= isl_alloc_array(tab
->mat
->ctx
, enum isl_tab_row_sign
,
246 for (i
= 0; i
< tab
->n_row
; ++i
)
247 dup
->row_sign
[i
] = tab
->row_sign
[i
];
250 dup
->samples
= isl_mat_dup(tab
->samples
);
253 dup
->sample_index
= isl_alloc_array(tab
->mat
->ctx
, int,
254 tab
->samples
->n_row
);
255 if (!dup
->sample_index
)
257 dup
->n_sample
= tab
->n_sample
;
258 dup
->n_outside
= tab
->n_outside
;
260 dup
->n_row
= tab
->n_row
;
261 dup
->n_con
= tab
->n_con
;
262 dup
->n_eq
= tab
->n_eq
;
263 dup
->max_con
= tab
->max_con
;
264 dup
->n_col
= tab
->n_col
;
265 dup
->n_var
= tab
->n_var
;
266 dup
->max_var
= tab
->max_var
;
267 dup
->n_param
= tab
->n_param
;
268 dup
->n_div
= tab
->n_div
;
269 dup
->n_dead
= tab
->n_dead
;
270 dup
->n_redundant
= tab
->n_redundant
;
271 dup
->rational
= tab
->rational
;
272 dup
->empty
= tab
->empty
;
273 dup
->strict_redundant
= 0;
277 tab
->cone
= tab
->cone
;
278 dup
->bottom
.type
= isl_tab_undo_bottom
;
279 dup
->bottom
.next
= NULL
;
280 dup
->top
= &dup
->bottom
;
282 dup
->n_zero
= tab
->n_zero
;
283 dup
->n_unbounded
= tab
->n_unbounded
;
284 dup
->basis
= isl_mat_dup(tab
->basis
);
292 /* Construct the coefficient matrix of the product tableau
294 * mat{1,2} is the coefficient matrix of tableau {1,2}
295 * row{1,2} is the number of rows in tableau {1,2}
296 * col{1,2} is the number of columns in tableau {1,2}
297 * off is the offset to the coefficient column (skipping the
298 * denominator, the constant term and the big parameter if any)
299 * r{1,2} is the number of redundant rows in tableau {1,2}
300 * d{1,2} is the number of dead columns in tableau {1,2}
302 * The order of the rows and columns in the result is as explained
303 * in isl_tab_product.
305 static struct isl_mat
*tab_mat_product(struct isl_mat
*mat1
,
306 struct isl_mat
*mat2
, unsigned row1
, unsigned row2
,
307 unsigned col1
, unsigned col2
,
308 unsigned off
, unsigned r1
, unsigned r2
, unsigned d1
, unsigned d2
)
311 struct isl_mat
*prod
;
314 prod
= isl_mat_alloc(mat1
->ctx
, mat1
->n_row
+ mat2
->n_row
,
320 for (i
= 0; i
< r1
; ++i
) {
321 isl_seq_cpy(prod
->row
[n
+ i
], mat1
->row
[i
], off
+ d1
);
322 isl_seq_clr(prod
->row
[n
+ i
] + off
+ d1
, d2
);
323 isl_seq_cpy(prod
->row
[n
+ i
] + off
+ d1
+ d2
,
324 mat1
->row
[i
] + off
+ d1
, col1
- d1
);
325 isl_seq_clr(prod
->row
[n
+ i
] + off
+ col1
+ d1
, col2
- d2
);
329 for (i
= 0; i
< r2
; ++i
) {
330 isl_seq_cpy(prod
->row
[n
+ i
], mat2
->row
[i
], off
);
331 isl_seq_clr(prod
->row
[n
+ i
] + off
, d1
);
332 isl_seq_cpy(prod
->row
[n
+ i
] + off
+ d1
,
333 mat2
->row
[i
] + off
, d2
);
334 isl_seq_clr(prod
->row
[n
+ i
] + off
+ d1
+ d2
, col1
- d1
);
335 isl_seq_cpy(prod
->row
[n
+ i
] + off
+ col1
+ d1
,
336 mat2
->row
[i
] + off
+ d2
, col2
- d2
);
340 for (i
= 0; i
< row1
- r1
; ++i
) {
341 isl_seq_cpy(prod
->row
[n
+ i
], mat1
->row
[r1
+ i
], off
+ d1
);
342 isl_seq_clr(prod
->row
[n
+ i
] + off
+ d1
, d2
);
343 isl_seq_cpy(prod
->row
[n
+ i
] + off
+ d1
+ d2
,
344 mat1
->row
[r1
+ i
] + off
+ d1
, col1
- d1
);
345 isl_seq_clr(prod
->row
[n
+ i
] + off
+ col1
+ d1
, col2
- d2
);
349 for (i
= 0; i
< row2
- r2
; ++i
) {
350 isl_seq_cpy(prod
->row
[n
+ i
], mat2
->row
[r2
+ i
], off
);
351 isl_seq_clr(prod
->row
[n
+ i
] + off
, d1
);
352 isl_seq_cpy(prod
->row
[n
+ i
] + off
+ d1
,
353 mat2
->row
[r2
+ i
] + off
, d2
);
354 isl_seq_clr(prod
->row
[n
+ i
] + off
+ d1
+ d2
, col1
- d1
);
355 isl_seq_cpy(prod
->row
[n
+ i
] + off
+ col1
+ d1
,
356 mat2
->row
[r2
+ i
] + off
+ d2
, col2
- d2
);
362 /* Update the row or column index of a variable that corresponds
363 * to a variable in the first input tableau.
365 static void update_index1(struct isl_tab_var
*var
,
366 unsigned r1
, unsigned r2
, unsigned d1
, unsigned d2
)
368 if (var
->index
== -1)
370 if (var
->is_row
&& var
->index
>= r1
)
372 if (!var
->is_row
&& var
->index
>= d1
)
376 /* Update the row or column index of a variable that corresponds
377 * to a variable in the second input tableau.
379 static void update_index2(struct isl_tab_var
*var
,
380 unsigned row1
, unsigned col1
,
381 unsigned r1
, unsigned r2
, unsigned d1
, unsigned d2
)
383 if (var
->index
== -1)
398 /* Create a tableau that represents the Cartesian product of the sets
399 * represented by tableaus tab1 and tab2.
400 * The order of the rows in the product is
401 * - redundant rows of tab1
402 * - redundant rows of tab2
403 * - non-redundant rows of tab1
404 * - non-redundant rows of tab2
405 * The order of the columns is
408 * - coefficient of big parameter, if any
409 * - dead columns of tab1
410 * - dead columns of tab2
411 * - live columns of tab1
412 * - live columns of tab2
413 * The order of the variables and the constraints is a concatenation
414 * of order in the two input tableaus.
416 struct isl_tab
*isl_tab_product(struct isl_tab
*tab1
, struct isl_tab
*tab2
)
419 struct isl_tab
*prod
;
421 unsigned r1
, r2
, d1
, d2
;
426 isl_assert(tab1
->mat
->ctx
, tab1
->M
== tab2
->M
, return NULL
);
427 isl_assert(tab1
->mat
->ctx
, tab1
->rational
== tab2
->rational
, return NULL
);
428 isl_assert(tab1
->mat
->ctx
, tab1
->cone
== tab2
->cone
, return NULL
);
429 isl_assert(tab1
->mat
->ctx
, !tab1
->row_sign
, return NULL
);
430 isl_assert(tab1
->mat
->ctx
, !tab2
->row_sign
, return NULL
);
431 isl_assert(tab1
->mat
->ctx
, tab1
->n_param
== 0, return NULL
);
432 isl_assert(tab1
->mat
->ctx
, tab2
->n_param
== 0, return NULL
);
433 isl_assert(tab1
->mat
->ctx
, tab1
->n_div
== 0, return NULL
);
434 isl_assert(tab1
->mat
->ctx
, tab2
->n_div
== 0, return NULL
);
437 r1
= tab1
->n_redundant
;
438 r2
= tab2
->n_redundant
;
441 prod
= isl_calloc_type(tab1
->mat
->ctx
, struct isl_tab
);
444 prod
->mat
= tab_mat_product(tab1
->mat
, tab2
->mat
,
445 tab1
->n_row
, tab2
->n_row
,
446 tab1
->n_col
, tab2
->n_col
, off
, r1
, r2
, d1
, d2
);
449 prod
->var
= isl_alloc_array(tab1
->mat
->ctx
, struct isl_tab_var
,
450 tab1
->max_var
+ tab2
->max_var
);
453 for (i
= 0; i
< tab1
->n_var
; ++i
) {
454 prod
->var
[i
] = tab1
->var
[i
];
455 update_index1(&prod
->var
[i
], r1
, r2
, d1
, d2
);
457 for (i
= 0; i
< tab2
->n_var
; ++i
) {
458 prod
->var
[tab1
->n_var
+ i
] = tab2
->var
[i
];
459 update_index2(&prod
->var
[tab1
->n_var
+ i
],
460 tab1
->n_row
, tab1
->n_col
,
463 prod
->con
= isl_alloc_array(tab1
->mat
->ctx
, struct isl_tab_var
,
464 tab1
->max_con
+ tab2
->max_con
);
467 for (i
= 0; i
< tab1
->n_con
; ++i
) {
468 prod
->con
[i
] = tab1
->con
[i
];
469 update_index1(&prod
->con
[i
], r1
, r2
, d1
, d2
);
471 for (i
= 0; i
< tab2
->n_con
; ++i
) {
472 prod
->con
[tab1
->n_con
+ i
] = tab2
->con
[i
];
473 update_index2(&prod
->con
[tab1
->n_con
+ i
],
474 tab1
->n_row
, tab1
->n_col
,
477 prod
->col_var
= isl_alloc_array(tab1
->mat
->ctx
, int,
478 tab1
->n_col
+ tab2
->n_col
);
481 for (i
= 0; i
< tab1
->n_col
; ++i
) {
482 int pos
= i
< d1
? i
: i
+ d2
;
483 prod
->col_var
[pos
] = tab1
->col_var
[i
];
485 for (i
= 0; i
< tab2
->n_col
; ++i
) {
486 int pos
= i
< d2
? d1
+ i
: tab1
->n_col
+ i
;
487 int t
= tab2
->col_var
[i
];
492 prod
->col_var
[pos
] = t
;
494 prod
->row_var
= isl_alloc_array(tab1
->mat
->ctx
, int,
495 tab1
->mat
->n_row
+ tab2
->mat
->n_row
);
498 for (i
= 0; i
< tab1
->n_row
; ++i
) {
499 int pos
= i
< r1
? i
: i
+ r2
;
500 prod
->row_var
[pos
] = tab1
->row_var
[i
];
502 for (i
= 0; i
< tab2
->n_row
; ++i
) {
503 int pos
= i
< r2
? r1
+ i
: tab1
->n_row
+ i
;
504 int t
= tab2
->row_var
[i
];
509 prod
->row_var
[pos
] = t
;
511 prod
->samples
= NULL
;
512 prod
->sample_index
= NULL
;
513 prod
->n_row
= tab1
->n_row
+ tab2
->n_row
;
514 prod
->n_con
= tab1
->n_con
+ tab2
->n_con
;
516 prod
->max_con
= tab1
->max_con
+ tab2
->max_con
;
517 prod
->n_col
= tab1
->n_col
+ tab2
->n_col
;
518 prod
->n_var
= tab1
->n_var
+ tab2
->n_var
;
519 prod
->max_var
= tab1
->max_var
+ tab2
->max_var
;
522 prod
->n_dead
= tab1
->n_dead
+ tab2
->n_dead
;
523 prod
->n_redundant
= tab1
->n_redundant
+ tab2
->n_redundant
;
524 prod
->rational
= tab1
->rational
;
525 prod
->empty
= tab1
->empty
|| tab2
->empty
;
526 prod
->strict_redundant
= tab1
->strict_redundant
|| tab2
->strict_redundant
;
530 prod
->cone
= tab1
->cone
;
531 prod
->bottom
.type
= isl_tab_undo_bottom
;
532 prod
->bottom
.next
= NULL
;
533 prod
->top
= &prod
->bottom
;
536 prod
->n_unbounded
= 0;
545 static struct isl_tab_var
*var_from_index(struct isl_tab
*tab
, int i
)
550 return &tab
->con
[~i
];
553 struct isl_tab_var
*isl_tab_var_from_row(struct isl_tab
*tab
, int i
)
555 return var_from_index(tab
, tab
->row_var
[i
]);
558 static struct isl_tab_var
*var_from_col(struct isl_tab
*tab
, int i
)
560 return var_from_index(tab
, tab
->col_var
[i
]);
563 /* Check if there are any upper bounds on column variable "var",
564 * i.e., non-negative rows where var appears with a negative coefficient.
565 * Return 1 if there are no such bounds.
567 static int max_is_manifestly_unbounded(struct isl_tab
*tab
,
568 struct isl_tab_var
*var
)
571 unsigned off
= 2 + tab
->M
;
575 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
576 if (!isl_int_is_neg(tab
->mat
->row
[i
][off
+ var
->index
]))
578 if (isl_tab_var_from_row(tab
, i
)->is_nonneg
)
584 /* Check if there are any lower bounds on column variable "var",
585 * i.e., non-negative rows where var appears with a positive coefficient.
586 * Return 1 if there are no such bounds.
588 static int min_is_manifestly_unbounded(struct isl_tab
*tab
,
589 struct isl_tab_var
*var
)
592 unsigned off
= 2 + tab
->M
;
596 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
597 if (!isl_int_is_pos(tab
->mat
->row
[i
][off
+ var
->index
]))
599 if (isl_tab_var_from_row(tab
, i
)->is_nonneg
)
605 static int row_cmp(struct isl_tab
*tab
, int r1
, int r2
, int c
, isl_int t
)
607 unsigned off
= 2 + tab
->M
;
611 isl_int_mul(t
, tab
->mat
->row
[r1
][2], tab
->mat
->row
[r2
][off
+c
]);
612 isl_int_submul(t
, tab
->mat
->row
[r2
][2], tab
->mat
->row
[r1
][off
+c
]);
617 isl_int_mul(t
, tab
->mat
->row
[r1
][1], tab
->mat
->row
[r2
][off
+ c
]);
618 isl_int_submul(t
, tab
->mat
->row
[r2
][1], tab
->mat
->row
[r1
][off
+ c
]);
619 return isl_int_sgn(t
);
622 /* Given the index of a column "c", return the index of a row
623 * that can be used to pivot the column in, with either an increase
624 * (sgn > 0) or a decrease (sgn < 0) of the corresponding variable.
625 * If "var" is not NULL, then the row returned will be different from
626 * the one associated with "var".
628 * Each row in the tableau is of the form
630 * x_r = a_r0 + \sum_i a_ri x_i
632 * Only rows with x_r >= 0 and with the sign of a_ri opposite to "sgn"
633 * impose any limit on the increase or decrease in the value of x_c
634 * and this bound is equal to a_r0 / |a_rc|. We are therefore looking
635 * for the row with the smallest (most stringent) such bound.
636 * Note that the common denominator of each row drops out of the fraction.
637 * To check if row j has a smaller bound than row r, i.e.,
638 * a_j0 / |a_jc| < a_r0 / |a_rc| or a_j0 |a_rc| < a_r0 |a_jc|,
639 * we check if -sign(a_jc) (a_j0 a_rc - a_r0 a_jc) < 0,
640 * where -sign(a_jc) is equal to "sgn".
642 static int pivot_row(struct isl_tab
*tab
,
643 struct isl_tab_var
*var
, int sgn
, int c
)
647 unsigned off
= 2 + tab
->M
;
651 for (j
= tab
->n_redundant
; j
< tab
->n_row
; ++j
) {
652 if (var
&& j
== var
->index
)
654 if (!isl_tab_var_from_row(tab
, j
)->is_nonneg
)
656 if (sgn
* isl_int_sgn(tab
->mat
->row
[j
][off
+ c
]) >= 0)
662 tsgn
= sgn
* row_cmp(tab
, r
, j
, c
, t
);
663 if (tsgn
< 0 || (tsgn
== 0 &&
664 tab
->row_var
[j
] < tab
->row_var
[r
]))
671 /* Find a pivot (row and col) that will increase (sgn > 0) or decrease
672 * (sgn < 0) the value of row variable var.
673 * If not NULL, then skip_var is a row variable that should be ignored
674 * while looking for a pivot row. It is usually equal to var.
676 * As the given row in the tableau is of the form
678 * x_r = a_r0 + \sum_i a_ri x_i
680 * we need to find a column such that the sign of a_ri is equal to "sgn"
681 * (such that an increase in x_i will have the desired effect) or a
682 * column with a variable that may attain negative values.
683 * If a_ri is positive, then we need to move x_i in the same direction
684 * to obtain the desired effect. Otherwise, x_i has to move in the
685 * opposite direction.
687 static void find_pivot(struct isl_tab
*tab
,
688 struct isl_tab_var
*var
, struct isl_tab_var
*skip_var
,
689 int sgn
, int *row
, int *col
)
696 isl_assert(tab
->mat
->ctx
, var
->is_row
, return);
697 tr
= tab
->mat
->row
[var
->index
] + 2 + tab
->M
;
700 for (j
= tab
->n_dead
; j
< tab
->n_col
; ++j
) {
701 if (isl_int_is_zero(tr
[j
]))
703 if (isl_int_sgn(tr
[j
]) != sgn
&&
704 var_from_col(tab
, j
)->is_nonneg
)
706 if (c
< 0 || tab
->col_var
[j
] < tab
->col_var
[c
])
712 sgn
*= isl_int_sgn(tr
[c
]);
713 r
= pivot_row(tab
, skip_var
, sgn
, c
);
714 *row
= r
< 0 ? var
->index
: r
;
718 /* Return 1 if row "row" represents an obviously redundant inequality.
720 * - it represents an inequality or a variable
721 * - that is the sum of a non-negative sample value and a positive
722 * combination of zero or more non-negative constraints.
724 int isl_tab_row_is_redundant(struct isl_tab
*tab
, int row
)
727 unsigned off
= 2 + tab
->M
;
729 if (tab
->row_var
[row
] < 0 && !isl_tab_var_from_row(tab
, row
)->is_nonneg
)
732 if (isl_int_is_neg(tab
->mat
->row
[row
][1]))
734 if (tab
->strict_redundant
&& isl_int_is_zero(tab
->mat
->row
[row
][1]))
736 if (tab
->M
&& isl_int_is_neg(tab
->mat
->row
[row
][2]))
739 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
740 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ i
]))
742 if (tab
->col_var
[i
] >= 0)
744 if (isl_int_is_neg(tab
->mat
->row
[row
][off
+ i
]))
746 if (!var_from_col(tab
, i
)->is_nonneg
)
752 static void swap_rows(struct isl_tab
*tab
, int row1
, int row2
)
755 enum isl_tab_row_sign s
;
757 t
= tab
->row_var
[row1
];
758 tab
->row_var
[row1
] = tab
->row_var
[row2
];
759 tab
->row_var
[row2
] = t
;
760 isl_tab_var_from_row(tab
, row1
)->index
= row1
;
761 isl_tab_var_from_row(tab
, row2
)->index
= row2
;
762 tab
->mat
= isl_mat_swap_rows(tab
->mat
, row1
, row2
);
766 s
= tab
->row_sign
[row1
];
767 tab
->row_sign
[row1
] = tab
->row_sign
[row2
];
768 tab
->row_sign
[row2
] = s
;
771 static int push_union(struct isl_tab
*tab
,
772 enum isl_tab_undo_type type
, union isl_tab_undo_val u
) WARN_UNUSED
;
773 static int push_union(struct isl_tab
*tab
,
774 enum isl_tab_undo_type type
, union isl_tab_undo_val u
)
776 struct isl_tab_undo
*undo
;
781 undo
= isl_alloc_type(tab
->mat
->ctx
, struct isl_tab_undo
);
786 undo
->next
= tab
->top
;
792 int isl_tab_push_var(struct isl_tab
*tab
,
793 enum isl_tab_undo_type type
, struct isl_tab_var
*var
)
795 union isl_tab_undo_val u
;
797 u
.var_index
= tab
->row_var
[var
->index
];
799 u
.var_index
= tab
->col_var
[var
->index
];
800 return push_union(tab
, type
, u
);
803 int isl_tab_push(struct isl_tab
*tab
, enum isl_tab_undo_type type
)
805 union isl_tab_undo_val u
= { 0 };
806 return push_union(tab
, type
, u
);
809 /* Push a record on the undo stack describing the current basic
810 * variables, so that the this state can be restored during rollback.
812 int isl_tab_push_basis(struct isl_tab
*tab
)
815 union isl_tab_undo_val u
;
817 u
.col_var
= isl_alloc_array(tab
->mat
->ctx
, int, tab
->n_col
);
820 for (i
= 0; i
< tab
->n_col
; ++i
)
821 u
.col_var
[i
] = tab
->col_var
[i
];
822 return push_union(tab
, isl_tab_undo_saved_basis
, u
);
825 int isl_tab_push_callback(struct isl_tab
*tab
, struct isl_tab_callback
*callback
)
827 union isl_tab_undo_val u
;
828 u
.callback
= callback
;
829 return push_union(tab
, isl_tab_undo_callback
, u
);
832 struct isl_tab
*isl_tab_init_samples(struct isl_tab
*tab
)
839 tab
->samples
= isl_mat_alloc(tab
->mat
->ctx
, 1, 1 + tab
->n_var
);
842 tab
->sample_index
= isl_alloc_array(tab
->mat
->ctx
, int, 1);
843 if (!tab
->sample_index
)
851 struct isl_tab
*isl_tab_add_sample(struct isl_tab
*tab
,
852 __isl_take isl_vec
*sample
)
857 if (tab
->n_sample
+ 1 > tab
->samples
->n_row
) {
858 int *t
= isl_realloc_array(tab
->mat
->ctx
,
859 tab
->sample_index
, int, tab
->n_sample
+ 1);
862 tab
->sample_index
= t
;
865 tab
->samples
= isl_mat_extend(tab
->samples
,
866 tab
->n_sample
+ 1, tab
->samples
->n_col
);
870 isl_seq_cpy(tab
->samples
->row
[tab
->n_sample
], sample
->el
, sample
->size
);
871 isl_vec_free(sample
);
872 tab
->sample_index
[tab
->n_sample
] = tab
->n_sample
;
877 isl_vec_free(sample
);
882 struct isl_tab
*isl_tab_drop_sample(struct isl_tab
*tab
, int s
)
884 if (s
!= tab
->n_outside
) {
885 int t
= tab
->sample_index
[tab
->n_outside
];
886 tab
->sample_index
[tab
->n_outside
] = tab
->sample_index
[s
];
887 tab
->sample_index
[s
] = t
;
888 isl_mat_swap_rows(tab
->samples
, tab
->n_outside
, s
);
891 if (isl_tab_push(tab
, isl_tab_undo_drop_sample
) < 0) {
899 /* Record the current number of samples so that we can remove newer
900 * samples during a rollback.
902 int isl_tab_save_samples(struct isl_tab
*tab
)
904 union isl_tab_undo_val u
;
910 return push_union(tab
, isl_tab_undo_saved_samples
, u
);
913 /* Mark row with index "row" as being redundant.
914 * If we may need to undo the operation or if the row represents
915 * a variable of the original problem, the row is kept,
916 * but no longer considered when looking for a pivot row.
917 * Otherwise, the row is simply removed.
919 * The row may be interchanged with some other row. If it
920 * is interchanged with a later row, return 1. Otherwise return 0.
921 * If the rows are checked in order in the calling function,
922 * then a return value of 1 means that the row with the given
923 * row number may now contain a different row that hasn't been checked yet.
925 int isl_tab_mark_redundant(struct isl_tab
*tab
, int row
)
927 struct isl_tab_var
*var
= isl_tab_var_from_row(tab
, row
);
928 var
->is_redundant
= 1;
929 isl_assert(tab
->mat
->ctx
, row
>= tab
->n_redundant
, return -1);
930 if (tab
->need_undo
|| tab
->row_var
[row
] >= 0) {
931 if (tab
->row_var
[row
] >= 0 && !var
->is_nonneg
) {
933 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, var
) < 0)
936 if (row
!= tab
->n_redundant
)
937 swap_rows(tab
, row
, tab
->n_redundant
);
939 return isl_tab_push_var(tab
, isl_tab_undo_redundant
, var
);
941 if (row
!= tab
->n_row
- 1)
942 swap_rows(tab
, row
, tab
->n_row
- 1);
943 isl_tab_var_from_row(tab
, tab
->n_row
- 1)->index
= -1;
949 int isl_tab_mark_empty(struct isl_tab
*tab
)
953 if (!tab
->empty
&& tab
->need_undo
)
954 if (isl_tab_push(tab
, isl_tab_undo_empty
) < 0)
960 int isl_tab_freeze_constraint(struct isl_tab
*tab
, int con
)
962 struct isl_tab_var
*var
;
967 var
= &tab
->con
[con
];
975 return isl_tab_push_var(tab
, isl_tab_undo_freeze
, var
);
980 /* Update the rows signs after a pivot of "row" and "col", with "row_sgn"
981 * the original sign of the pivot element.
982 * We only keep track of row signs during PILP solving and in this case
983 * we only pivot a row with negative sign (meaning the value is always
984 * non-positive) using a positive pivot element.
986 * For each row j, the new value of the parametric constant is equal to
988 * a_j0 - a_jc a_r0/a_rc
990 * where a_j0 is the original parametric constant, a_rc is the pivot element,
991 * a_r0 is the parametric constant of the pivot row and a_jc is the
992 * pivot column entry of the row j.
993 * Since a_r0 is non-positive and a_rc is positive, the sign of row j
994 * remains the same if a_jc has the same sign as the row j or if
995 * a_jc is zero. In all other cases, we reset the sign to "unknown".
997 static void update_row_sign(struct isl_tab
*tab
, int row
, int col
, int row_sgn
)
1000 struct isl_mat
*mat
= tab
->mat
;
1001 unsigned off
= 2 + tab
->M
;
1006 if (tab
->row_sign
[row
] == 0)
1008 isl_assert(mat
->ctx
, row_sgn
> 0, return);
1009 isl_assert(mat
->ctx
, tab
->row_sign
[row
] == isl_tab_row_neg
, return);
1010 tab
->row_sign
[row
] = isl_tab_row_pos
;
1011 for (i
= 0; i
< tab
->n_row
; ++i
) {
1015 s
= isl_int_sgn(mat
->row
[i
][off
+ col
]);
1018 if (!tab
->row_sign
[i
])
1020 if (s
< 0 && tab
->row_sign
[i
] == isl_tab_row_neg
)
1022 if (s
> 0 && tab
->row_sign
[i
] == isl_tab_row_pos
)
1024 tab
->row_sign
[i
] = isl_tab_row_unknown
;
1028 /* Given a row number "row" and a column number "col", pivot the tableau
1029 * such that the associated variables are interchanged.
1030 * The given row in the tableau expresses
1032 * x_r = a_r0 + \sum_i a_ri x_i
1036 * x_c = 1/a_rc x_r - a_r0/a_rc + sum_{i \ne r} -a_ri/a_rc
1038 * Substituting this equality into the other rows
1040 * x_j = a_j0 + \sum_i a_ji x_i
1042 * with a_jc \ne 0, we obtain
1044 * 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
1051 * where i is any other column and j is any other row,
1052 * is therefore transformed into
1054 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1055 * 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)
1057 * The transformation is performed along the following steps
1059 * d_r/n_rc n_ri/n_rc
1062 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1065 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1066 * n_jc/(|n_rc| d_j) n_ji/(|n_rc| d_j)
1068 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1069 * n_jc/(|n_rc| d_j) (n_ji |n_rc|)/(|n_rc| d_j)
1071 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1072 * n_jc/(|n_rc| d_j) (n_ji |n_rc| - s(n_rc)n_jc n_ri)/(|n_rc| d_j)
1074 * s(n_rc)d_r/|n_rc| -s(n_rc)n_ri/|n_rc|
1075 * 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)
1078 int isl_tab_pivot(struct isl_tab
*tab
, int row
, int col
)
1083 struct isl_mat
*mat
= tab
->mat
;
1084 struct isl_tab_var
*var
;
1085 unsigned off
= 2 + tab
->M
;
1087 if (tab
->mat
->ctx
->abort
) {
1088 isl_ctx_set_error(tab
->mat
->ctx
, isl_error_abort
);
1092 isl_int_swap(mat
->row
[row
][0], mat
->row
[row
][off
+ col
]);
1093 sgn
= isl_int_sgn(mat
->row
[row
][0]);
1095 isl_int_neg(mat
->row
[row
][0], mat
->row
[row
][0]);
1096 isl_int_neg(mat
->row
[row
][off
+ col
], mat
->row
[row
][off
+ col
]);
1098 for (j
= 0; j
< off
- 1 + tab
->n_col
; ++j
) {
1099 if (j
== off
- 1 + col
)
1101 isl_int_neg(mat
->row
[row
][1 + j
], mat
->row
[row
][1 + j
]);
1103 if (!isl_int_is_one(mat
->row
[row
][0]))
1104 isl_seq_normalize(mat
->ctx
, mat
->row
[row
], off
+ tab
->n_col
);
1105 for (i
= 0; i
< tab
->n_row
; ++i
) {
1108 if (isl_int_is_zero(mat
->row
[i
][off
+ col
]))
1110 isl_int_mul(mat
->row
[i
][0], mat
->row
[i
][0], mat
->row
[row
][0]);
1111 for (j
= 0; j
< off
- 1 + tab
->n_col
; ++j
) {
1112 if (j
== off
- 1 + col
)
1114 isl_int_mul(mat
->row
[i
][1 + j
],
1115 mat
->row
[i
][1 + j
], mat
->row
[row
][0]);
1116 isl_int_addmul(mat
->row
[i
][1 + j
],
1117 mat
->row
[i
][off
+ col
], mat
->row
[row
][1 + j
]);
1119 isl_int_mul(mat
->row
[i
][off
+ col
],
1120 mat
->row
[i
][off
+ col
], mat
->row
[row
][off
+ col
]);
1121 if (!isl_int_is_one(mat
->row
[i
][0]))
1122 isl_seq_normalize(mat
->ctx
, mat
->row
[i
], off
+ tab
->n_col
);
1124 t
= tab
->row_var
[row
];
1125 tab
->row_var
[row
] = tab
->col_var
[col
];
1126 tab
->col_var
[col
] = t
;
1127 var
= isl_tab_var_from_row(tab
, row
);
1130 var
= var_from_col(tab
, col
);
1133 update_row_sign(tab
, row
, col
, sgn
);
1136 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
1137 if (isl_int_is_zero(mat
->row
[i
][off
+ col
]))
1139 if (!isl_tab_var_from_row(tab
, i
)->frozen
&&
1140 isl_tab_row_is_redundant(tab
, i
)) {
1141 int redo
= isl_tab_mark_redundant(tab
, i
);
1151 /* If "var" represents a column variable, then pivot is up (sgn > 0)
1152 * or down (sgn < 0) to a row. The variable is assumed not to be
1153 * unbounded in the specified direction.
1154 * If sgn = 0, then the variable is unbounded in both directions,
1155 * and we pivot with any row we can find.
1157 static int to_row(struct isl_tab
*tab
, struct isl_tab_var
*var
, int sign
) WARN_UNUSED
;
1158 static int to_row(struct isl_tab
*tab
, struct isl_tab_var
*var
, int sign
)
1161 unsigned off
= 2 + tab
->M
;
1167 for (r
= tab
->n_redundant
; r
< tab
->n_row
; ++r
)
1168 if (!isl_int_is_zero(tab
->mat
->row
[r
][off
+var
->index
]))
1170 isl_assert(tab
->mat
->ctx
, r
< tab
->n_row
, return -1);
1172 r
= pivot_row(tab
, NULL
, sign
, var
->index
);
1173 isl_assert(tab
->mat
->ctx
, r
>= 0, return -1);
1176 return isl_tab_pivot(tab
, r
, var
->index
);
1179 static void check_table(struct isl_tab
*tab
)
1185 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
1186 struct isl_tab_var
*var
;
1187 var
= isl_tab_var_from_row(tab
, i
);
1188 if (!var
->is_nonneg
)
1191 isl_assert(tab
->mat
->ctx
,
1192 !isl_int_is_neg(tab
->mat
->row
[i
][2]), abort());
1193 if (isl_int_is_pos(tab
->mat
->row
[i
][2]))
1196 isl_assert(tab
->mat
->ctx
, !isl_int_is_neg(tab
->mat
->row
[i
][1]),
1201 /* Return the sign of the maximal value of "var".
1202 * If the sign is not negative, then on return from this function,
1203 * the sample value will also be non-negative.
1205 * If "var" is manifestly unbounded wrt positive values, we are done.
1206 * Otherwise, we pivot the variable up to a row if needed
1207 * Then we continue pivoting down until either
1208 * - no more down pivots can be performed
1209 * - the sample value is positive
1210 * - the variable is pivoted into a manifestly unbounded column
1212 static int sign_of_max(struct isl_tab
*tab
, struct isl_tab_var
*var
)
1216 if (max_is_manifestly_unbounded(tab
, var
))
1218 if (to_row(tab
, var
, 1) < 0)
1220 while (!isl_int_is_pos(tab
->mat
->row
[var
->index
][1])) {
1221 find_pivot(tab
, var
, var
, 1, &row
, &col
);
1223 return isl_int_sgn(tab
->mat
->row
[var
->index
][1]);
1224 if (isl_tab_pivot(tab
, row
, col
) < 0)
1226 if (!var
->is_row
) /* manifestly unbounded */
1232 int isl_tab_sign_of_max(struct isl_tab
*tab
, int con
)
1234 struct isl_tab_var
*var
;
1239 var
= &tab
->con
[con
];
1240 isl_assert(tab
->mat
->ctx
, !var
->is_redundant
, return -2);
1241 isl_assert(tab
->mat
->ctx
, !var
->is_zero
, return -2);
1243 return sign_of_max(tab
, var
);
1246 static int row_is_neg(struct isl_tab
*tab
, int row
)
1249 return isl_int_is_neg(tab
->mat
->row
[row
][1]);
1250 if (isl_int_is_pos(tab
->mat
->row
[row
][2]))
1252 if (isl_int_is_neg(tab
->mat
->row
[row
][2]))
1254 return isl_int_is_neg(tab
->mat
->row
[row
][1]);
1257 static int row_sgn(struct isl_tab
*tab
, int row
)
1260 return isl_int_sgn(tab
->mat
->row
[row
][1]);
1261 if (!isl_int_is_zero(tab
->mat
->row
[row
][2]))
1262 return isl_int_sgn(tab
->mat
->row
[row
][2]);
1264 return isl_int_sgn(tab
->mat
->row
[row
][1]);
1267 /* Perform pivots until the row variable "var" has a non-negative
1268 * sample value or until no more upward pivots can be performed.
1269 * Return the sign of the sample value after the pivots have been
1272 static int restore_row(struct isl_tab
*tab
, struct isl_tab_var
*var
)
1276 while (row_is_neg(tab
, var
->index
)) {
1277 find_pivot(tab
, var
, var
, 1, &row
, &col
);
1280 if (isl_tab_pivot(tab
, row
, col
) < 0)
1282 if (!var
->is_row
) /* manifestly unbounded */
1285 return row_sgn(tab
, var
->index
);
1288 /* Perform pivots until we are sure that the row variable "var"
1289 * can attain non-negative values. After return from this
1290 * function, "var" is still a row variable, but its sample
1291 * value may not be non-negative, even if the function returns 1.
1293 static int at_least_zero(struct isl_tab
*tab
, struct isl_tab_var
*var
)
1297 while (isl_int_is_neg(tab
->mat
->row
[var
->index
][1])) {
1298 find_pivot(tab
, var
, var
, 1, &row
, &col
);
1301 if (row
== var
->index
) /* manifestly unbounded */
1303 if (isl_tab_pivot(tab
, row
, col
) < 0)
1306 return !isl_int_is_neg(tab
->mat
->row
[var
->index
][1]);
1309 /* Return a negative value if "var" can attain negative values.
1310 * Return a non-negative value otherwise.
1312 * If "var" is manifestly unbounded wrt negative values, we are done.
1313 * Otherwise, if var is in a column, we can pivot it down to a row.
1314 * Then we continue pivoting down until either
1315 * - the pivot would result in a manifestly unbounded column
1316 * => we don't perform the pivot, but simply return -1
1317 * - no more down pivots can be performed
1318 * - the sample value is negative
1319 * If the sample value becomes negative and the variable is supposed
1320 * to be nonnegative, then we undo the last pivot.
1321 * However, if the last pivot has made the pivoting variable
1322 * obviously redundant, then it may have moved to another row.
1323 * In that case we look for upward pivots until we reach a non-negative
1326 static int sign_of_min(struct isl_tab
*tab
, struct isl_tab_var
*var
)
1329 struct isl_tab_var
*pivot_var
= NULL
;
1331 if (min_is_manifestly_unbounded(tab
, var
))
1335 row
= pivot_row(tab
, NULL
, -1, col
);
1336 pivot_var
= var_from_col(tab
, col
);
1337 if (isl_tab_pivot(tab
, row
, col
) < 0)
1339 if (var
->is_redundant
)
1341 if (isl_int_is_neg(tab
->mat
->row
[var
->index
][1])) {
1342 if (var
->is_nonneg
) {
1343 if (!pivot_var
->is_redundant
&&
1344 pivot_var
->index
== row
) {
1345 if (isl_tab_pivot(tab
, row
, col
) < 0)
1348 if (restore_row(tab
, var
) < -1)
1354 if (var
->is_redundant
)
1356 while (!isl_int_is_neg(tab
->mat
->row
[var
->index
][1])) {
1357 find_pivot(tab
, var
, var
, -1, &row
, &col
);
1358 if (row
== var
->index
)
1361 return isl_int_sgn(tab
->mat
->row
[var
->index
][1]);
1362 pivot_var
= var_from_col(tab
, col
);
1363 if (isl_tab_pivot(tab
, row
, col
) < 0)
1365 if (var
->is_redundant
)
1368 if (pivot_var
&& var
->is_nonneg
) {
1369 /* pivot back to non-negative value */
1370 if (!pivot_var
->is_redundant
&& pivot_var
->index
== row
) {
1371 if (isl_tab_pivot(tab
, row
, col
) < 0)
1374 if (restore_row(tab
, var
) < -1)
1380 static int row_at_most_neg_one(struct isl_tab
*tab
, int row
)
1383 if (isl_int_is_pos(tab
->mat
->row
[row
][2]))
1385 if (isl_int_is_neg(tab
->mat
->row
[row
][2]))
1388 return isl_int_is_neg(tab
->mat
->row
[row
][1]) &&
1389 isl_int_abs_ge(tab
->mat
->row
[row
][1],
1390 tab
->mat
->row
[row
][0]);
1393 /* Return 1 if "var" can attain values <= -1.
1394 * Return 0 otherwise.
1396 * The sample value of "var" is assumed to be non-negative when the
1397 * the function is called. If 1 is returned then the constraint
1398 * is not redundant and the sample value is made non-negative again before
1399 * the function returns.
1401 int isl_tab_min_at_most_neg_one(struct isl_tab
*tab
, struct isl_tab_var
*var
)
1404 struct isl_tab_var
*pivot_var
;
1406 if (min_is_manifestly_unbounded(tab
, var
))
1410 row
= pivot_row(tab
, NULL
, -1, col
);
1411 pivot_var
= var_from_col(tab
, col
);
1412 if (isl_tab_pivot(tab
, row
, col
) < 0)
1414 if (var
->is_redundant
)
1416 if (row_at_most_neg_one(tab
, var
->index
)) {
1417 if (var
->is_nonneg
) {
1418 if (!pivot_var
->is_redundant
&&
1419 pivot_var
->index
== row
) {
1420 if (isl_tab_pivot(tab
, row
, col
) < 0)
1423 if (restore_row(tab
, var
) < -1)
1429 if (var
->is_redundant
)
1432 find_pivot(tab
, var
, var
, -1, &row
, &col
);
1433 if (row
== var
->index
) {
1434 if (restore_row(tab
, var
) < -1)
1440 pivot_var
= var_from_col(tab
, col
);
1441 if (isl_tab_pivot(tab
, row
, col
) < 0)
1443 if (var
->is_redundant
)
1445 } while (!row_at_most_neg_one(tab
, var
->index
));
1446 if (var
->is_nonneg
) {
1447 /* pivot back to non-negative value */
1448 if (!pivot_var
->is_redundant
&& pivot_var
->index
== row
)
1449 if (isl_tab_pivot(tab
, row
, col
) < 0)
1451 if (restore_row(tab
, var
) < -1)
1457 /* Return 1 if "var" can attain values >= 1.
1458 * Return 0 otherwise.
1460 static int at_least_one(struct isl_tab
*tab
, struct isl_tab_var
*var
)
1465 if (max_is_manifestly_unbounded(tab
, var
))
1467 if (to_row(tab
, var
, 1) < 0)
1469 r
= tab
->mat
->row
[var
->index
];
1470 while (isl_int_lt(r
[1], r
[0])) {
1471 find_pivot(tab
, var
, var
, 1, &row
, &col
);
1473 return isl_int_ge(r
[1], r
[0]);
1474 if (row
== var
->index
) /* manifestly unbounded */
1476 if (isl_tab_pivot(tab
, row
, col
) < 0)
1482 static void swap_cols(struct isl_tab
*tab
, int col1
, int col2
)
1485 unsigned off
= 2 + tab
->M
;
1486 t
= tab
->col_var
[col1
];
1487 tab
->col_var
[col1
] = tab
->col_var
[col2
];
1488 tab
->col_var
[col2
] = t
;
1489 var_from_col(tab
, col1
)->index
= col1
;
1490 var_from_col(tab
, col2
)->index
= col2
;
1491 tab
->mat
= isl_mat_swap_cols(tab
->mat
, off
+ col1
, off
+ col2
);
1494 /* Mark column with index "col" as representing a zero variable.
1495 * If we may need to undo the operation the column is kept,
1496 * but no longer considered.
1497 * Otherwise, the column is simply removed.
1499 * The column may be interchanged with some other column. If it
1500 * is interchanged with a later column, return 1. Otherwise return 0.
1501 * If the columns are checked in order in the calling function,
1502 * then a return value of 1 means that the column with the given
1503 * column number may now contain a different column that
1504 * hasn't been checked yet.
1506 int isl_tab_kill_col(struct isl_tab
*tab
, int col
)
1508 var_from_col(tab
, col
)->is_zero
= 1;
1509 if (tab
->need_undo
) {
1510 if (isl_tab_push_var(tab
, isl_tab_undo_zero
,
1511 var_from_col(tab
, col
)) < 0)
1513 if (col
!= tab
->n_dead
)
1514 swap_cols(tab
, col
, tab
->n_dead
);
1518 if (col
!= tab
->n_col
- 1)
1519 swap_cols(tab
, col
, tab
->n_col
- 1);
1520 var_from_col(tab
, tab
->n_col
- 1)->index
= -1;
1526 static int row_is_manifestly_non_integral(struct isl_tab
*tab
, int row
)
1528 unsigned off
= 2 + tab
->M
;
1530 if (tab
->M
&& !isl_int_eq(tab
->mat
->row
[row
][2],
1531 tab
->mat
->row
[row
][0]))
1533 if (isl_seq_first_non_zero(tab
->mat
->row
[row
] + off
+ tab
->n_dead
,
1534 tab
->n_col
- tab
->n_dead
) != -1)
1537 return !isl_int_is_divisible_by(tab
->mat
->row
[row
][1],
1538 tab
->mat
->row
[row
][0]);
1541 /* For integer tableaus, check if any of the coordinates are stuck
1542 * at a non-integral value.
1544 static int tab_is_manifestly_empty(struct isl_tab
*tab
)
1553 for (i
= 0; i
< tab
->n_var
; ++i
) {
1554 if (!tab
->var
[i
].is_row
)
1556 if (row_is_manifestly_non_integral(tab
, tab
->var
[i
].index
))
1563 /* Row variable "var" is non-negative and cannot attain any values
1564 * larger than zero. This means that the coefficients of the unrestricted
1565 * column variables are zero and that the coefficients of the non-negative
1566 * column variables are zero or negative.
1567 * Each of the non-negative variables with a negative coefficient can
1568 * then also be written as the negative sum of non-negative variables
1569 * and must therefore also be zero.
1571 static int close_row(struct isl_tab
*tab
, struct isl_tab_var
*var
) WARN_UNUSED
;
1572 static int close_row(struct isl_tab
*tab
, struct isl_tab_var
*var
)
1575 struct isl_mat
*mat
= tab
->mat
;
1576 unsigned off
= 2 + tab
->M
;
1578 isl_assert(tab
->mat
->ctx
, var
->is_nonneg
, return -1);
1581 if (isl_tab_push_var(tab
, isl_tab_undo_zero
, var
) < 0)
1583 for (j
= tab
->n_dead
; j
< tab
->n_col
; ++j
) {
1585 if (isl_int_is_zero(mat
->row
[var
->index
][off
+ j
]))
1587 isl_assert(tab
->mat
->ctx
,
1588 isl_int_is_neg(mat
->row
[var
->index
][off
+ j
]), return -1);
1589 recheck
= isl_tab_kill_col(tab
, j
);
1595 if (isl_tab_mark_redundant(tab
, var
->index
) < 0)
1597 if (tab_is_manifestly_empty(tab
) && isl_tab_mark_empty(tab
) < 0)
1602 /* Add a constraint to the tableau and allocate a row for it.
1603 * Return the index into the constraint array "con".
1605 int isl_tab_allocate_con(struct isl_tab
*tab
)
1609 isl_assert(tab
->mat
->ctx
, tab
->n_row
< tab
->mat
->n_row
, return -1);
1610 isl_assert(tab
->mat
->ctx
, tab
->n_con
< tab
->max_con
, return -1);
1613 tab
->con
[r
].index
= tab
->n_row
;
1614 tab
->con
[r
].is_row
= 1;
1615 tab
->con
[r
].is_nonneg
= 0;
1616 tab
->con
[r
].is_zero
= 0;
1617 tab
->con
[r
].is_redundant
= 0;
1618 tab
->con
[r
].frozen
= 0;
1619 tab
->con
[r
].negated
= 0;
1620 tab
->row_var
[tab
->n_row
] = ~r
;
1624 if (isl_tab_push_var(tab
, isl_tab_undo_allocate
, &tab
->con
[r
]) < 0)
1630 /* Add a variable to the tableau and allocate a column for it.
1631 * Return the index into the variable array "var".
1633 int isl_tab_allocate_var(struct isl_tab
*tab
)
1637 unsigned off
= 2 + tab
->M
;
1639 isl_assert(tab
->mat
->ctx
, tab
->n_col
< tab
->mat
->n_col
, return -1);
1640 isl_assert(tab
->mat
->ctx
, tab
->n_var
< tab
->max_var
, return -1);
1643 tab
->var
[r
].index
= tab
->n_col
;
1644 tab
->var
[r
].is_row
= 0;
1645 tab
->var
[r
].is_nonneg
= 0;
1646 tab
->var
[r
].is_zero
= 0;
1647 tab
->var
[r
].is_redundant
= 0;
1648 tab
->var
[r
].frozen
= 0;
1649 tab
->var
[r
].negated
= 0;
1650 tab
->col_var
[tab
->n_col
] = r
;
1652 for (i
= 0; i
< tab
->n_row
; ++i
)
1653 isl_int_set_si(tab
->mat
->row
[i
][off
+ tab
->n_col
], 0);
1657 if (isl_tab_push_var(tab
, isl_tab_undo_allocate
, &tab
->var
[r
]) < 0)
1663 /* Add a row to the tableau. The row is given as an affine combination
1664 * of the original variables and needs to be expressed in terms of the
1667 * We add each term in turn.
1668 * If r = n/d_r is the current sum and we need to add k x, then
1669 * if x is a column variable, we increase the numerator of
1670 * this column by k d_r
1671 * if x = f/d_x is a row variable, then the new representation of r is
1673 * n k f d_x/g n + d_r/g k f m/d_r n + m/d_g k f
1674 * --- + --- = ------------------- = -------------------
1675 * d_r d_r d_r d_x/g m
1677 * with g the gcd of d_r and d_x and m the lcm of d_r and d_x.
1679 * If tab->M is set, then, internally, each variable x is represented
1680 * as x' - M. We then also need no subtract k d_r from the coefficient of M.
1682 int isl_tab_add_row(struct isl_tab
*tab
, isl_int
*line
)
1688 unsigned off
= 2 + tab
->M
;
1690 r
= isl_tab_allocate_con(tab
);
1696 row
= tab
->mat
->row
[tab
->con
[r
].index
];
1697 isl_int_set_si(row
[0], 1);
1698 isl_int_set(row
[1], line
[0]);
1699 isl_seq_clr(row
+ 2, tab
->M
+ tab
->n_col
);
1700 for (i
= 0; i
< tab
->n_var
; ++i
) {
1701 if (tab
->var
[i
].is_zero
)
1703 if (tab
->var
[i
].is_row
) {
1705 row
[0], tab
->mat
->row
[tab
->var
[i
].index
][0]);
1706 isl_int_swap(a
, row
[0]);
1707 isl_int_divexact(a
, row
[0], a
);
1709 row
[0], tab
->mat
->row
[tab
->var
[i
].index
][0]);
1710 isl_int_mul(b
, b
, line
[1 + i
]);
1711 isl_seq_combine(row
+ 1, a
, row
+ 1,
1712 b
, tab
->mat
->row
[tab
->var
[i
].index
] + 1,
1713 1 + tab
->M
+ tab
->n_col
);
1715 isl_int_addmul(row
[off
+ tab
->var
[i
].index
],
1716 line
[1 + i
], row
[0]);
1717 if (tab
->M
&& i
>= tab
->n_param
&& i
< tab
->n_var
- tab
->n_div
)
1718 isl_int_submul(row
[2], line
[1 + i
], row
[0]);
1720 isl_seq_normalize(tab
->mat
->ctx
, row
, off
+ tab
->n_col
);
1725 tab
->row_sign
[tab
->con
[r
].index
] = isl_tab_row_unknown
;
1730 static int drop_row(struct isl_tab
*tab
, int row
)
1732 isl_assert(tab
->mat
->ctx
, ~tab
->row_var
[row
] == tab
->n_con
- 1, return -1);
1733 if (row
!= tab
->n_row
- 1)
1734 swap_rows(tab
, row
, tab
->n_row
- 1);
1740 static int drop_col(struct isl_tab
*tab
, int col
)
1742 isl_assert(tab
->mat
->ctx
, tab
->col_var
[col
] == tab
->n_var
- 1, return -1);
1743 if (col
!= tab
->n_col
- 1)
1744 swap_cols(tab
, col
, tab
->n_col
- 1);
1750 /* Add inequality "ineq" and check if it conflicts with the
1751 * previously added constraints or if it is obviously redundant.
1753 int isl_tab_add_ineq(struct isl_tab
*tab
, isl_int
*ineq
)
1762 struct isl_basic_map
*bmap
= tab
->bmap
;
1764 isl_assert(tab
->mat
->ctx
, tab
->n_eq
== bmap
->n_eq
, return -1);
1765 isl_assert(tab
->mat
->ctx
,
1766 tab
->n_con
== bmap
->n_eq
+ bmap
->n_ineq
, return -1);
1767 tab
->bmap
= isl_basic_map_add_ineq(tab
->bmap
, ineq
);
1768 if (isl_tab_push(tab
, isl_tab_undo_bmap_ineq
) < 0)
1775 isl_int_swap(ineq
[0], cst
);
1777 r
= isl_tab_add_row(tab
, ineq
);
1779 isl_int_swap(ineq
[0], cst
);
1784 tab
->con
[r
].is_nonneg
= 1;
1785 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
1787 if (isl_tab_row_is_redundant(tab
, tab
->con
[r
].index
)) {
1788 if (isl_tab_mark_redundant(tab
, tab
->con
[r
].index
) < 0)
1793 sgn
= restore_row(tab
, &tab
->con
[r
]);
1797 return isl_tab_mark_empty(tab
);
1798 if (tab
->con
[r
].is_row
&& isl_tab_row_is_redundant(tab
, tab
->con
[r
].index
))
1799 if (isl_tab_mark_redundant(tab
, tab
->con
[r
].index
) < 0)
1804 /* Pivot a non-negative variable down until it reaches the value zero
1805 * and then pivot the variable into a column position.
1807 static int to_col(struct isl_tab
*tab
, struct isl_tab_var
*var
) WARN_UNUSED
;
1808 static int to_col(struct isl_tab
*tab
, struct isl_tab_var
*var
)
1812 unsigned off
= 2 + tab
->M
;
1817 while (isl_int_is_pos(tab
->mat
->row
[var
->index
][1])) {
1818 find_pivot(tab
, var
, NULL
, -1, &row
, &col
);
1819 isl_assert(tab
->mat
->ctx
, row
!= -1, return -1);
1820 if (isl_tab_pivot(tab
, row
, col
) < 0)
1826 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
)
1827 if (!isl_int_is_zero(tab
->mat
->row
[var
->index
][off
+ i
]))
1830 isl_assert(tab
->mat
->ctx
, i
< tab
->n_col
, return -1);
1831 if (isl_tab_pivot(tab
, var
->index
, i
) < 0)
1837 /* We assume Gaussian elimination has been performed on the equalities.
1838 * The equalities can therefore never conflict.
1839 * Adding the equalities is currently only really useful for a later call
1840 * to isl_tab_ineq_type.
1842 static struct isl_tab
*add_eq(struct isl_tab
*tab
, isl_int
*eq
)
1849 r
= isl_tab_add_row(tab
, eq
);
1853 r
= tab
->con
[r
].index
;
1854 i
= isl_seq_first_non_zero(tab
->mat
->row
[r
] + 2 + tab
->M
+ tab
->n_dead
,
1855 tab
->n_col
- tab
->n_dead
);
1856 isl_assert(tab
->mat
->ctx
, i
>= 0, goto error
);
1858 if (isl_tab_pivot(tab
, r
, i
) < 0)
1860 if (isl_tab_kill_col(tab
, i
) < 0)
1870 static int row_is_manifestly_zero(struct isl_tab
*tab
, int row
)
1872 unsigned off
= 2 + tab
->M
;
1874 if (!isl_int_is_zero(tab
->mat
->row
[row
][1]))
1876 if (tab
->M
&& !isl_int_is_zero(tab
->mat
->row
[row
][2]))
1878 return isl_seq_first_non_zero(tab
->mat
->row
[row
] + off
+ tab
->n_dead
,
1879 tab
->n_col
- tab
->n_dead
) == -1;
1882 /* Add an equality that is known to be valid for the given tableau.
1884 int isl_tab_add_valid_eq(struct isl_tab
*tab
, isl_int
*eq
)
1886 struct isl_tab_var
*var
;
1891 r
= isl_tab_add_row(tab
, eq
);
1897 if (row_is_manifestly_zero(tab
, r
)) {
1899 if (isl_tab_mark_redundant(tab
, r
) < 0)
1904 if (isl_int_is_neg(tab
->mat
->row
[r
][1])) {
1905 isl_seq_neg(tab
->mat
->row
[r
] + 1, tab
->mat
->row
[r
] + 1,
1910 if (to_col(tab
, var
) < 0)
1913 if (isl_tab_kill_col(tab
, var
->index
) < 0)
1919 static int add_zero_row(struct isl_tab
*tab
)
1924 r
= isl_tab_allocate_con(tab
);
1928 row
= tab
->mat
->row
[tab
->con
[r
].index
];
1929 isl_seq_clr(row
+ 1, 1 + tab
->M
+ tab
->n_col
);
1930 isl_int_set_si(row
[0], 1);
1935 /* Add equality "eq" and check if it conflicts with the
1936 * previously added constraints or if it is obviously redundant.
1938 int isl_tab_add_eq(struct isl_tab
*tab
, isl_int
*eq
)
1940 struct isl_tab_undo
*snap
= NULL
;
1941 struct isl_tab_var
*var
;
1949 isl_assert(tab
->mat
->ctx
, !tab
->M
, return -1);
1952 snap
= isl_tab_snap(tab
);
1956 isl_int_swap(eq
[0], cst
);
1958 r
= isl_tab_add_row(tab
, eq
);
1960 isl_int_swap(eq
[0], cst
);
1968 if (row_is_manifestly_zero(tab
, row
)) {
1970 if (isl_tab_rollback(tab
, snap
) < 0)
1978 tab
->bmap
= isl_basic_map_add_ineq(tab
->bmap
, eq
);
1979 if (isl_tab_push(tab
, isl_tab_undo_bmap_ineq
) < 0)
1981 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
1982 tab
->bmap
= isl_basic_map_add_ineq(tab
->bmap
, eq
);
1983 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
1984 if (isl_tab_push(tab
, isl_tab_undo_bmap_ineq
) < 0)
1988 if (add_zero_row(tab
) < 0)
1992 sgn
= isl_int_sgn(tab
->mat
->row
[row
][1]);
1995 isl_seq_neg(tab
->mat
->row
[row
] + 1, tab
->mat
->row
[row
] + 1,
2002 sgn
= sign_of_max(tab
, var
);
2006 if (isl_tab_mark_empty(tab
) < 0)
2013 if (to_col(tab
, var
) < 0)
2016 if (isl_tab_kill_col(tab
, var
->index
) < 0)
2022 /* Construct and return an inequality that expresses an upper bound
2024 * In particular, if the div is given by
2028 * then the inequality expresses
2032 static struct isl_vec
*ineq_for_div(struct isl_basic_map
*bmap
, unsigned div
)
2036 struct isl_vec
*ineq
;
2041 total
= isl_basic_map_total_dim(bmap
);
2042 div_pos
= 1 + total
- bmap
->n_div
+ div
;
2044 ineq
= isl_vec_alloc(bmap
->ctx
, 1 + total
);
2048 isl_seq_cpy(ineq
->el
, bmap
->div
[div
] + 1, 1 + total
);
2049 isl_int_neg(ineq
->el
[div_pos
], bmap
->div
[div
][0]);
2053 /* For a div d = floor(f/m), add the constraints
2056 * -(f-(m-1)) + m d >= 0
2058 * Note that the second constraint is the negation of
2062 * If add_ineq is not NULL, then this function is used
2063 * instead of isl_tab_add_ineq to effectively add the inequalities.
2065 static int add_div_constraints(struct isl_tab
*tab
, unsigned div
,
2066 int (*add_ineq
)(void *user
, isl_int
*), void *user
)
2070 struct isl_vec
*ineq
;
2072 total
= isl_basic_map_total_dim(tab
->bmap
);
2073 div_pos
= 1 + total
- tab
->bmap
->n_div
+ div
;
2075 ineq
= ineq_for_div(tab
->bmap
, div
);
2080 if (add_ineq(user
, ineq
->el
) < 0)
2083 if (isl_tab_add_ineq(tab
, ineq
->el
) < 0)
2087 isl_seq_neg(ineq
->el
, tab
->bmap
->div
[div
] + 1, 1 + total
);
2088 isl_int_set(ineq
->el
[div_pos
], tab
->bmap
->div
[div
][0]);
2089 isl_int_add(ineq
->el
[0], ineq
->el
[0], ineq
->el
[div_pos
]);
2090 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
2093 if (add_ineq(user
, ineq
->el
) < 0)
2096 if (isl_tab_add_ineq(tab
, ineq
->el
) < 0)
2108 /* Check whether the div described by "div" is obviously non-negative.
2109 * If we are using a big parameter, then we will encode the div
2110 * as div' = M + div, which is always non-negative.
2111 * Otherwise, we check whether div is a non-negative affine combination
2112 * of non-negative variables.
2114 static int div_is_nonneg(struct isl_tab
*tab
, __isl_keep isl_vec
*div
)
2121 if (isl_int_is_neg(div
->el
[1]))
2124 for (i
= 0; i
< tab
->n_var
; ++i
) {
2125 if (isl_int_is_neg(div
->el
[2 + i
]))
2127 if (isl_int_is_zero(div
->el
[2 + i
]))
2129 if (!tab
->var
[i
].is_nonneg
)
2136 /* Add an extra div, prescribed by "div" to the tableau and
2137 * the associated bmap (which is assumed to be non-NULL).
2139 * If add_ineq is not NULL, then this function is used instead
2140 * of isl_tab_add_ineq to add the div constraints.
2141 * This complication is needed because the code in isl_tab_pip
2142 * wants to perform some extra processing when an inequality
2143 * is added to the tableau.
2145 int isl_tab_add_div(struct isl_tab
*tab
, __isl_keep isl_vec
*div
,
2146 int (*add_ineq
)(void *user
, isl_int
*), void *user
)
2155 isl_assert(tab
->mat
->ctx
, tab
->bmap
, return -1);
2157 nonneg
= div_is_nonneg(tab
, div
);
2159 if (isl_tab_extend_cons(tab
, 3) < 0)
2161 if (isl_tab_extend_vars(tab
, 1) < 0)
2163 r
= isl_tab_allocate_var(tab
);
2168 tab
->var
[r
].is_nonneg
= 1;
2170 tab
->bmap
= isl_basic_map_extend_dim(tab
->bmap
,
2171 isl_basic_map_get_dim(tab
->bmap
), 1, 0, 2);
2172 k
= isl_basic_map_alloc_div(tab
->bmap
);
2175 isl_seq_cpy(tab
->bmap
->div
[k
], div
->el
, div
->size
);
2176 if (isl_tab_push(tab
, isl_tab_undo_bmap_div
) < 0)
2179 if (add_div_constraints(tab
, k
, add_ineq
, user
) < 0)
2185 struct isl_tab
*isl_tab_from_basic_map(struct isl_basic_map
*bmap
)
2188 struct isl_tab
*tab
;
2192 tab
= isl_tab_alloc(bmap
->ctx
,
2193 isl_basic_map_total_dim(bmap
) + bmap
->n_ineq
+ 1,
2194 isl_basic_map_total_dim(bmap
), 0);
2197 tab
->rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
2198 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
)) {
2199 if (isl_tab_mark_empty(tab
) < 0)
2203 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2204 tab
= add_eq(tab
, bmap
->eq
[i
]);
2208 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
2209 if (isl_tab_add_ineq(tab
, bmap
->ineq
[i
]) < 0)
2220 struct isl_tab
*isl_tab_from_basic_set(struct isl_basic_set
*bset
)
2222 return isl_tab_from_basic_map((struct isl_basic_map
*)bset
);
2225 /* Construct a tableau corresponding to the recession cone of "bset".
2227 struct isl_tab
*isl_tab_from_recession_cone(__isl_keep isl_basic_set
*bset
,
2232 struct isl_tab
*tab
;
2233 unsigned offset
= 0;
2238 offset
= isl_basic_set_dim(bset
, isl_dim_param
);
2239 tab
= isl_tab_alloc(bset
->ctx
, bset
->n_eq
+ bset
->n_ineq
,
2240 isl_basic_set_total_dim(bset
) - offset
, 0);
2243 tab
->rational
= ISL_F_ISSET(bset
, ISL_BASIC_SET_RATIONAL
);
2247 for (i
= 0; i
< bset
->n_eq
; ++i
) {
2248 isl_int_swap(bset
->eq
[i
][offset
], cst
);
2250 if (isl_tab_add_eq(tab
, bset
->eq
[i
] + offset
) < 0)
2253 tab
= add_eq(tab
, bset
->eq
[i
]);
2254 isl_int_swap(bset
->eq
[i
][offset
], cst
);
2258 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
2260 isl_int_swap(bset
->ineq
[i
][offset
], cst
);
2261 r
= isl_tab_add_row(tab
, bset
->ineq
[i
] + offset
);
2262 isl_int_swap(bset
->ineq
[i
][offset
], cst
);
2265 tab
->con
[r
].is_nonneg
= 1;
2266 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
2278 /* Assuming "tab" is the tableau of a cone, check if the cone is
2279 * bounded, i.e., if it is empty or only contains the origin.
2281 int isl_tab_cone_is_bounded(struct isl_tab
*tab
)
2289 if (tab
->n_dead
== tab
->n_col
)
2293 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
2294 struct isl_tab_var
*var
;
2296 var
= isl_tab_var_from_row(tab
, i
);
2297 if (!var
->is_nonneg
)
2299 sgn
= sign_of_max(tab
, var
);
2304 if (close_row(tab
, var
) < 0)
2308 if (tab
->n_dead
== tab
->n_col
)
2310 if (i
== tab
->n_row
)
2315 int isl_tab_sample_is_integer(struct isl_tab
*tab
)
2322 for (i
= 0; i
< tab
->n_var
; ++i
) {
2324 if (!tab
->var
[i
].is_row
)
2326 row
= tab
->var
[i
].index
;
2327 if (!isl_int_is_divisible_by(tab
->mat
->row
[row
][1],
2328 tab
->mat
->row
[row
][0]))
2334 static struct isl_vec
*extract_integer_sample(struct isl_tab
*tab
)
2337 struct isl_vec
*vec
;
2339 vec
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_var
);
2343 isl_int_set_si(vec
->block
.data
[0], 1);
2344 for (i
= 0; i
< tab
->n_var
; ++i
) {
2345 if (!tab
->var
[i
].is_row
)
2346 isl_int_set_si(vec
->block
.data
[1 + i
], 0);
2348 int row
= tab
->var
[i
].index
;
2349 isl_int_divexact(vec
->block
.data
[1 + i
],
2350 tab
->mat
->row
[row
][1], tab
->mat
->row
[row
][0]);
2357 struct isl_vec
*isl_tab_get_sample_value(struct isl_tab
*tab
)
2360 struct isl_vec
*vec
;
2366 vec
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_var
);
2372 isl_int_set_si(vec
->block
.data
[0], 1);
2373 for (i
= 0; i
< tab
->n_var
; ++i
) {
2375 if (!tab
->var
[i
].is_row
) {
2376 isl_int_set_si(vec
->block
.data
[1 + i
], 0);
2379 row
= tab
->var
[i
].index
;
2380 isl_int_gcd(m
, vec
->block
.data
[0], tab
->mat
->row
[row
][0]);
2381 isl_int_divexact(m
, tab
->mat
->row
[row
][0], m
);
2382 isl_seq_scale(vec
->block
.data
, vec
->block
.data
, m
, 1 + i
);
2383 isl_int_divexact(m
, vec
->block
.data
[0], tab
->mat
->row
[row
][0]);
2384 isl_int_mul(vec
->block
.data
[1 + i
], m
, tab
->mat
->row
[row
][1]);
2386 vec
= isl_vec_normalize(vec
);
2392 /* Update "bmap" based on the results of the tableau "tab".
2393 * In particular, implicit equalities are made explicit, redundant constraints
2394 * are removed and if the sample value happens to be integer, it is stored
2395 * in "bmap" (unless "bmap" already had an integer sample).
2397 * The tableau is assumed to have been created from "bmap" using
2398 * isl_tab_from_basic_map.
2400 struct isl_basic_map
*isl_basic_map_update_from_tab(struct isl_basic_map
*bmap
,
2401 struct isl_tab
*tab
)
2413 bmap
= isl_basic_map_set_to_empty(bmap
);
2415 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
2416 if (isl_tab_is_equality(tab
, n_eq
+ i
))
2417 isl_basic_map_inequality_to_equality(bmap
, i
);
2418 else if (isl_tab_is_redundant(tab
, n_eq
+ i
))
2419 isl_basic_map_drop_inequality(bmap
, i
);
2421 if (bmap
->n_eq
!= n_eq
)
2422 isl_basic_map_gauss(bmap
, NULL
);
2423 if (!tab
->rational
&&
2424 !bmap
->sample
&& isl_tab_sample_is_integer(tab
))
2425 bmap
->sample
= extract_integer_sample(tab
);
2429 struct isl_basic_set
*isl_basic_set_update_from_tab(struct isl_basic_set
*bset
,
2430 struct isl_tab
*tab
)
2432 return (struct isl_basic_set
*)isl_basic_map_update_from_tab(
2433 (struct isl_basic_map
*)bset
, tab
);
2436 /* Given a non-negative variable "var", add a new non-negative variable
2437 * that is the opposite of "var", ensuring that var can only attain the
2439 * If var = n/d is a row variable, then the new variable = -n/d.
2440 * If var is a column variables, then the new variable = -var.
2441 * If the new variable cannot attain non-negative values, then
2442 * the resulting tableau is empty.
2443 * Otherwise, we know the value will be zero and we close the row.
2445 static int cut_to_hyperplane(struct isl_tab
*tab
, struct isl_tab_var
*var
)
2450 unsigned off
= 2 + tab
->M
;
2454 isl_assert(tab
->mat
->ctx
, !var
->is_redundant
, return -1);
2455 isl_assert(tab
->mat
->ctx
, var
->is_nonneg
, return -1);
2457 if (isl_tab_extend_cons(tab
, 1) < 0)
2461 tab
->con
[r
].index
= tab
->n_row
;
2462 tab
->con
[r
].is_row
= 1;
2463 tab
->con
[r
].is_nonneg
= 0;
2464 tab
->con
[r
].is_zero
= 0;
2465 tab
->con
[r
].is_redundant
= 0;
2466 tab
->con
[r
].frozen
= 0;
2467 tab
->con
[r
].negated
= 0;
2468 tab
->row_var
[tab
->n_row
] = ~r
;
2469 row
= tab
->mat
->row
[tab
->n_row
];
2472 isl_int_set(row
[0], tab
->mat
->row
[var
->index
][0]);
2473 isl_seq_neg(row
+ 1,
2474 tab
->mat
->row
[var
->index
] + 1, 1 + tab
->n_col
);
2476 isl_int_set_si(row
[0], 1);
2477 isl_seq_clr(row
+ 1, 1 + tab
->n_col
);
2478 isl_int_set_si(row
[off
+ var
->index
], -1);
2483 if (isl_tab_push_var(tab
, isl_tab_undo_allocate
, &tab
->con
[r
]) < 0)
2486 sgn
= sign_of_max(tab
, &tab
->con
[r
]);
2490 if (isl_tab_mark_empty(tab
) < 0)
2494 tab
->con
[r
].is_nonneg
= 1;
2495 if (isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]) < 0)
2498 if (close_row(tab
, &tab
->con
[r
]) < 0)
2504 /* Given a tableau "tab" and an inequality constraint "con" of the tableau,
2505 * relax the inequality by one. That is, the inequality r >= 0 is replaced
2506 * by r' = r + 1 >= 0.
2507 * If r is a row variable, we simply increase the constant term by one
2508 * (taking into account the denominator).
2509 * If r is a column variable, then we need to modify each row that
2510 * refers to r = r' - 1 by substituting this equality, effectively
2511 * subtracting the coefficient of the column from the constant.
2512 * We should only do this if the minimum is manifestly unbounded,
2513 * however. Otherwise, we may end up with negative sample values
2514 * for non-negative variables.
2515 * So, if r is a column variable with a minimum that is not
2516 * manifestly unbounded, then we need to move it to a row.
2517 * However, the sample value of this row may be negative,
2518 * even after the relaxation, so we need to restore it.
2519 * We therefore prefer to pivot a column up to a row, if possible.
2521 struct isl_tab
*isl_tab_relax(struct isl_tab
*tab
, int con
)
2523 struct isl_tab_var
*var
;
2524 unsigned off
= 2 + tab
->M
;
2529 var
= &tab
->con
[con
];
2531 if (!var
->is_row
&& !max_is_manifestly_unbounded(tab
, var
))
2532 if (to_row(tab
, var
, 1) < 0)
2534 if (!var
->is_row
&& !min_is_manifestly_unbounded(tab
, var
))
2535 if (to_row(tab
, var
, -1) < 0)
2539 isl_int_add(tab
->mat
->row
[var
->index
][1],
2540 tab
->mat
->row
[var
->index
][1], tab
->mat
->row
[var
->index
][0]);
2541 if (restore_row(tab
, var
) < 0)
2546 for (i
= 0; i
< tab
->n_row
; ++i
) {
2547 if (isl_int_is_zero(tab
->mat
->row
[i
][off
+ var
->index
]))
2549 isl_int_sub(tab
->mat
->row
[i
][1], tab
->mat
->row
[i
][1],
2550 tab
->mat
->row
[i
][off
+ var
->index
]);
2555 if (isl_tab_push_var(tab
, isl_tab_undo_relax
, var
) < 0)
2564 int isl_tab_select_facet(struct isl_tab
*tab
, int con
)
2569 return cut_to_hyperplane(tab
, &tab
->con
[con
]);
2572 static int may_be_equality(struct isl_tab
*tab
, int row
)
2574 unsigned off
= 2 + tab
->M
;
2575 return tab
->rational
? isl_int_is_zero(tab
->mat
->row
[row
][1])
2576 : isl_int_lt(tab
->mat
->row
[row
][1],
2577 tab
->mat
->row
[row
][0]);
2580 /* Check for (near) equalities among the constraints.
2581 * A constraint is an equality if it is non-negative and if
2582 * its maximal value is either
2583 * - zero (in case of rational tableaus), or
2584 * - strictly less than 1 (in case of integer tableaus)
2586 * We first mark all non-redundant and non-dead variables that
2587 * are not frozen and not obviously not an equality.
2588 * Then we iterate over all marked variables if they can attain
2589 * any values larger than zero or at least one.
2590 * If the maximal value is zero, we mark any column variables
2591 * that appear in the row as being zero and mark the row as being redundant.
2592 * Otherwise, if the maximal value is strictly less than one (and the
2593 * tableau is integer), then we restrict the value to being zero
2594 * by adding an opposite non-negative variable.
2596 int isl_tab_detect_implicit_equalities(struct isl_tab
*tab
)
2605 if (tab
->n_dead
== tab
->n_col
)
2609 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
2610 struct isl_tab_var
*var
= isl_tab_var_from_row(tab
, i
);
2611 var
->marked
= !var
->frozen
&& var
->is_nonneg
&&
2612 may_be_equality(tab
, i
);
2616 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
2617 struct isl_tab_var
*var
= var_from_col(tab
, i
);
2618 var
->marked
= !var
->frozen
&& var
->is_nonneg
;
2623 struct isl_tab_var
*var
;
2625 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
2626 var
= isl_tab_var_from_row(tab
, i
);
2630 if (i
== tab
->n_row
) {
2631 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
2632 var
= var_from_col(tab
, i
);
2636 if (i
== tab
->n_col
)
2641 sgn
= sign_of_max(tab
, var
);
2645 if (close_row(tab
, var
) < 0)
2647 } else if (!tab
->rational
&& !at_least_one(tab
, var
)) {
2648 if (cut_to_hyperplane(tab
, var
) < 0)
2650 return isl_tab_detect_implicit_equalities(tab
);
2652 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
2653 var
= isl_tab_var_from_row(tab
, i
);
2656 if (may_be_equality(tab
, i
))
2666 static int con_is_redundant(struct isl_tab
*tab
, struct isl_tab_var
*var
)
2670 if (tab
->rational
) {
2671 int sgn
= sign_of_min(tab
, var
);
2676 int irred
= isl_tab_min_at_most_neg_one(tab
, var
);
2683 /* Check for (near) redundant constraints.
2684 * A constraint is redundant if it is non-negative and if
2685 * its minimal value (temporarily ignoring the non-negativity) is either
2686 * - zero (in case of rational tableaus), or
2687 * - strictly larger than -1 (in case of integer tableaus)
2689 * We first mark all non-redundant and non-dead variables that
2690 * are not frozen and not obviously negatively unbounded.
2691 * Then we iterate over all marked variables if they can attain
2692 * any values smaller than zero or at most negative one.
2693 * If not, we mark the row as being redundant (assuming it hasn't
2694 * been detected as being obviously redundant in the mean time).
2696 int isl_tab_detect_redundant(struct isl_tab
*tab
)
2705 if (tab
->n_redundant
== tab
->n_row
)
2709 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
2710 struct isl_tab_var
*var
= isl_tab_var_from_row(tab
, i
);
2711 var
->marked
= !var
->frozen
&& var
->is_nonneg
;
2715 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
2716 struct isl_tab_var
*var
= var_from_col(tab
, i
);
2717 var
->marked
= !var
->frozen
&& var
->is_nonneg
&&
2718 !min_is_manifestly_unbounded(tab
, var
);
2723 struct isl_tab_var
*var
;
2725 for (i
= tab
->n_redundant
; i
< tab
->n_row
; ++i
) {
2726 var
= isl_tab_var_from_row(tab
, i
);
2730 if (i
== tab
->n_row
) {
2731 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
2732 var
= var_from_col(tab
, i
);
2736 if (i
== tab
->n_col
)
2741 red
= con_is_redundant(tab
, var
);
2744 if (red
&& !var
->is_redundant
)
2745 if (isl_tab_mark_redundant(tab
, var
->index
) < 0)
2747 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
2748 var
= var_from_col(tab
, i
);
2751 if (!min_is_manifestly_unbounded(tab
, var
))
2761 int isl_tab_is_equality(struct isl_tab
*tab
, int con
)
2768 if (tab
->con
[con
].is_zero
)
2770 if (tab
->con
[con
].is_redundant
)
2772 if (!tab
->con
[con
].is_row
)
2773 return tab
->con
[con
].index
< tab
->n_dead
;
2775 row
= tab
->con
[con
].index
;
2778 return isl_int_is_zero(tab
->mat
->row
[row
][1]) &&
2779 (!tab
->M
|| isl_int_is_zero(tab
->mat
->row
[row
][2])) &&
2780 isl_seq_first_non_zero(tab
->mat
->row
[row
] + off
+ tab
->n_dead
,
2781 tab
->n_col
- tab
->n_dead
) == -1;
2784 /* Return the minimal value of the affine expression "f" with denominator
2785 * "denom" in *opt, *opt_denom, assuming the tableau is not empty and
2786 * the expression cannot attain arbitrarily small values.
2787 * If opt_denom is NULL, then *opt is rounded up to the nearest integer.
2788 * The return value reflects the nature of the result (empty, unbounded,
2789 * minimal value returned in *opt).
2791 enum isl_lp_result
isl_tab_min(struct isl_tab
*tab
,
2792 isl_int
*f
, isl_int denom
, isl_int
*opt
, isl_int
*opt_denom
,
2796 enum isl_lp_result res
= isl_lp_ok
;
2797 struct isl_tab_var
*var
;
2798 struct isl_tab_undo
*snap
;
2801 return isl_lp_error
;
2804 return isl_lp_empty
;
2806 snap
= isl_tab_snap(tab
);
2807 r
= isl_tab_add_row(tab
, f
);
2809 return isl_lp_error
;
2813 find_pivot(tab
, var
, var
, -1, &row
, &col
);
2814 if (row
== var
->index
) {
2815 res
= isl_lp_unbounded
;
2820 if (isl_tab_pivot(tab
, row
, col
) < 0)
2821 return isl_lp_error
;
2823 isl_int_mul(tab
->mat
->row
[var
->index
][0],
2824 tab
->mat
->row
[var
->index
][0], denom
);
2825 if (ISL_FL_ISSET(flags
, ISL_TAB_SAVE_DUAL
)) {
2828 isl_vec_free(tab
->dual
);
2829 tab
->dual
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_con
);
2831 return isl_lp_error
;
2832 isl_int_set(tab
->dual
->el
[0], tab
->mat
->row
[var
->index
][0]);
2833 for (i
= 0; i
< tab
->n_con
; ++i
) {
2835 if (tab
->con
[i
].is_row
) {
2836 isl_int_set_si(tab
->dual
->el
[1 + i
], 0);
2839 pos
= 2 + tab
->M
+ tab
->con
[i
].index
;
2840 if (tab
->con
[i
].negated
)
2841 isl_int_neg(tab
->dual
->el
[1 + i
],
2842 tab
->mat
->row
[var
->index
][pos
]);
2844 isl_int_set(tab
->dual
->el
[1 + i
],
2845 tab
->mat
->row
[var
->index
][pos
]);
2848 if (opt
&& res
== isl_lp_ok
) {
2850 isl_int_set(*opt
, tab
->mat
->row
[var
->index
][1]);
2851 isl_int_set(*opt_denom
, tab
->mat
->row
[var
->index
][0]);
2853 isl_int_cdiv_q(*opt
, tab
->mat
->row
[var
->index
][1],
2854 tab
->mat
->row
[var
->index
][0]);
2856 if (isl_tab_rollback(tab
, snap
) < 0)
2857 return isl_lp_error
;
2861 int isl_tab_is_redundant(struct isl_tab
*tab
, int con
)
2865 if (tab
->con
[con
].is_zero
)
2867 if (tab
->con
[con
].is_redundant
)
2869 return tab
->con
[con
].is_row
&& tab
->con
[con
].index
< tab
->n_redundant
;
2872 /* Take a snapshot of the tableau that can be restored by s call to
2875 struct isl_tab_undo
*isl_tab_snap(struct isl_tab
*tab
)
2883 /* Undo the operation performed by isl_tab_relax.
2885 static int unrelax(struct isl_tab
*tab
, struct isl_tab_var
*var
) WARN_UNUSED
;
2886 static int unrelax(struct isl_tab
*tab
, struct isl_tab_var
*var
)
2888 unsigned off
= 2 + tab
->M
;
2890 if (!var
->is_row
&& !max_is_manifestly_unbounded(tab
, var
))
2891 if (to_row(tab
, var
, 1) < 0)
2895 isl_int_sub(tab
->mat
->row
[var
->index
][1],
2896 tab
->mat
->row
[var
->index
][1], tab
->mat
->row
[var
->index
][0]);
2897 if (var
->is_nonneg
) {
2898 int sgn
= restore_row(tab
, var
);
2899 isl_assert(tab
->mat
->ctx
, sgn
>= 0, return -1);
2904 for (i
= 0; i
< tab
->n_row
; ++i
) {
2905 if (isl_int_is_zero(tab
->mat
->row
[i
][off
+ var
->index
]))
2907 isl_int_add(tab
->mat
->row
[i
][1], tab
->mat
->row
[i
][1],
2908 tab
->mat
->row
[i
][off
+ var
->index
]);
2916 static int perform_undo_var(struct isl_tab
*tab
, struct isl_tab_undo
*undo
) WARN_UNUSED
;
2917 static int perform_undo_var(struct isl_tab
*tab
, struct isl_tab_undo
*undo
)
2919 struct isl_tab_var
*var
= var_from_index(tab
, undo
->u
.var_index
);
2920 switch(undo
->type
) {
2921 case isl_tab_undo_nonneg
:
2924 case isl_tab_undo_redundant
:
2925 var
->is_redundant
= 0;
2927 restore_row(tab
, isl_tab_var_from_row(tab
, tab
->n_redundant
));
2929 case isl_tab_undo_freeze
:
2932 case isl_tab_undo_zero
:
2937 case isl_tab_undo_allocate
:
2938 if (undo
->u
.var_index
>= 0) {
2939 isl_assert(tab
->mat
->ctx
, !var
->is_row
, return -1);
2940 drop_col(tab
, var
->index
);
2944 if (!max_is_manifestly_unbounded(tab
, var
)) {
2945 if (to_row(tab
, var
, 1) < 0)
2947 } else if (!min_is_manifestly_unbounded(tab
, var
)) {
2948 if (to_row(tab
, var
, -1) < 0)
2951 if (to_row(tab
, var
, 0) < 0)
2954 drop_row(tab
, var
->index
);
2956 case isl_tab_undo_relax
:
2957 return unrelax(tab
, var
);
2963 /* Restore the tableau to the state where the basic variables
2964 * are those in "col_var".
2965 * We first construct a list of variables that are currently in
2966 * the basis, but shouldn't. Then we iterate over all variables
2967 * that should be in the basis and for each one that is currently
2968 * not in the basis, we exchange it with one of the elements of the
2969 * list constructed before.
2970 * We can always find an appropriate variable to pivot with because
2971 * the current basis is mapped to the old basis by a non-singular
2972 * matrix and so we can never end up with a zero row.
2974 static int restore_basis(struct isl_tab
*tab
, int *col_var
)
2978 int *extra
= NULL
; /* current columns that contain bad stuff */
2979 unsigned off
= 2 + tab
->M
;
2981 extra
= isl_alloc_array(tab
->mat
->ctx
, int, tab
->n_col
);
2984 for (i
= 0; i
< tab
->n_col
; ++i
) {
2985 for (j
= 0; j
< tab
->n_col
; ++j
)
2986 if (tab
->col_var
[i
] == col_var
[j
])
2990 extra
[n_extra
++] = i
;
2992 for (i
= 0; i
< tab
->n_col
&& n_extra
> 0; ++i
) {
2993 struct isl_tab_var
*var
;
2996 for (j
= 0; j
< tab
->n_col
; ++j
)
2997 if (col_var
[i
] == tab
->col_var
[j
])
3001 var
= var_from_index(tab
, col_var
[i
]);
3003 for (j
= 0; j
< n_extra
; ++j
)
3004 if (!isl_int_is_zero(tab
->mat
->row
[row
][off
+extra
[j
]]))
3006 isl_assert(tab
->mat
->ctx
, j
< n_extra
, goto error
);
3007 if (isl_tab_pivot(tab
, row
, extra
[j
]) < 0)
3009 extra
[j
] = extra
[--n_extra
];
3021 /* Remove all samples with index n or greater, i.e., those samples
3022 * that were added since we saved this number of samples in
3023 * isl_tab_save_samples.
3025 static void drop_samples_since(struct isl_tab
*tab
, int n
)
3029 for (i
= tab
->n_sample
- 1; i
>= 0 && tab
->n_sample
> n
; --i
) {
3030 if (tab
->sample_index
[i
] < n
)
3033 if (i
!= tab
->n_sample
- 1) {
3034 int t
= tab
->sample_index
[tab
->n_sample
-1];
3035 tab
->sample_index
[tab
->n_sample
-1] = tab
->sample_index
[i
];
3036 tab
->sample_index
[i
] = t
;
3037 isl_mat_swap_rows(tab
->samples
, tab
->n_sample
-1, i
);
3043 static int perform_undo(struct isl_tab
*tab
, struct isl_tab_undo
*undo
) WARN_UNUSED
;
3044 static int perform_undo(struct isl_tab
*tab
, struct isl_tab_undo
*undo
)
3046 switch (undo
->type
) {
3047 case isl_tab_undo_empty
:
3050 case isl_tab_undo_nonneg
:
3051 case isl_tab_undo_redundant
:
3052 case isl_tab_undo_freeze
:
3053 case isl_tab_undo_zero
:
3054 case isl_tab_undo_allocate
:
3055 case isl_tab_undo_relax
:
3056 return perform_undo_var(tab
, undo
);
3057 case isl_tab_undo_bmap_eq
:
3058 return isl_basic_map_free_equality(tab
->bmap
, 1);
3059 case isl_tab_undo_bmap_ineq
:
3060 return isl_basic_map_free_inequality(tab
->bmap
, 1);
3061 case isl_tab_undo_bmap_div
:
3062 if (isl_basic_map_free_div(tab
->bmap
, 1) < 0)
3065 tab
->samples
->n_col
--;
3067 case isl_tab_undo_saved_basis
:
3068 if (restore_basis(tab
, undo
->u
.col_var
) < 0)
3071 case isl_tab_undo_drop_sample
:
3074 case isl_tab_undo_saved_samples
:
3075 drop_samples_since(tab
, undo
->u
.n
);
3077 case isl_tab_undo_callback
:
3078 return undo
->u
.callback
->run(undo
->u
.callback
);
3080 isl_assert(tab
->mat
->ctx
, 0, return -1);
3085 /* Return the tableau to the state it was in when the snapshot "snap"
3088 int isl_tab_rollback(struct isl_tab
*tab
, struct isl_tab_undo
*snap
)
3090 struct isl_tab_undo
*undo
, *next
;
3096 for (undo
= tab
->top
; undo
&& undo
!= &tab
->bottom
; undo
= next
) {
3100 if (perform_undo(tab
, undo
) < 0) {
3115 /* The given row "row" represents an inequality violated by all
3116 * points in the tableau. Check for some special cases of such
3117 * separating constraints.
3118 * In particular, if the row has been reduced to the constant -1,
3119 * then we know the inequality is adjacent (but opposite) to
3120 * an equality in the tableau.
3121 * If the row has been reduced to r = c*(-1 -r'), with r' an inequality
3122 * of the tableau and c a positive constant, then the inequality
3123 * is adjacent (but opposite) to the inequality r'.
3125 static enum isl_ineq_type
separation_type(struct isl_tab
*tab
, unsigned row
)
3128 unsigned off
= 2 + tab
->M
;
3131 return isl_ineq_separate
;
3133 if (!isl_int_is_one(tab
->mat
->row
[row
][0]))
3134 return isl_ineq_separate
;
3136 pos
= isl_seq_first_non_zero(tab
->mat
->row
[row
] + off
+ tab
->n_dead
,
3137 tab
->n_col
- tab
->n_dead
);
3139 if (isl_int_is_negone(tab
->mat
->row
[row
][1]))
3140 return isl_ineq_adj_eq
;
3142 return isl_ineq_separate
;
3145 if (!isl_int_eq(tab
->mat
->row
[row
][1],
3146 tab
->mat
->row
[row
][off
+ tab
->n_dead
+ pos
]))
3147 return isl_ineq_separate
;
3149 pos
= isl_seq_first_non_zero(
3150 tab
->mat
->row
[row
] + off
+ tab
->n_dead
+ pos
+ 1,
3151 tab
->n_col
- tab
->n_dead
- pos
- 1);
3153 return pos
== -1 ? isl_ineq_adj_ineq
: isl_ineq_separate
;
3156 /* Check the effect of inequality "ineq" on the tableau "tab".
3158 * isl_ineq_redundant: satisfied by all points in the tableau
3159 * isl_ineq_separate: satisfied by no point in the tableau
3160 * isl_ineq_cut: satisfied by some by not all points
3161 * isl_ineq_adj_eq: adjacent to an equality
3162 * isl_ineq_adj_ineq: adjacent to an inequality.
3164 enum isl_ineq_type
isl_tab_ineq_type(struct isl_tab
*tab
, isl_int
*ineq
)
3166 enum isl_ineq_type type
= isl_ineq_error
;
3167 struct isl_tab_undo
*snap
= NULL
;
3172 return isl_ineq_error
;
3174 if (isl_tab_extend_cons(tab
, 1) < 0)
3175 return isl_ineq_error
;
3177 snap
= isl_tab_snap(tab
);
3179 con
= isl_tab_add_row(tab
, ineq
);
3183 row
= tab
->con
[con
].index
;
3184 if (isl_tab_row_is_redundant(tab
, row
))
3185 type
= isl_ineq_redundant
;
3186 else if (isl_int_is_neg(tab
->mat
->row
[row
][1]) &&
3188 isl_int_abs_ge(tab
->mat
->row
[row
][1],
3189 tab
->mat
->row
[row
][0]))) {
3190 int nonneg
= at_least_zero(tab
, &tab
->con
[con
]);
3194 type
= isl_ineq_cut
;
3196 type
= separation_type(tab
, row
);
3198 int red
= con_is_redundant(tab
, &tab
->con
[con
]);
3202 type
= isl_ineq_cut
;
3204 type
= isl_ineq_redundant
;
3207 if (isl_tab_rollback(tab
, snap
))
3208 return isl_ineq_error
;
3211 return isl_ineq_error
;
3214 int isl_tab_track_bmap(struct isl_tab
*tab
, __isl_take isl_basic_map
*bmap
)
3219 isl_assert(tab
->mat
->ctx
, tab
->n_eq
== bmap
->n_eq
, return -1);
3220 isl_assert(tab
->mat
->ctx
,
3221 tab
->n_con
== bmap
->n_eq
+ bmap
->n_ineq
, return -1);
3227 isl_basic_map_free(bmap
);
3231 int isl_tab_track_bset(struct isl_tab
*tab
, __isl_take isl_basic_set
*bset
)
3233 return isl_tab_track_bmap(tab
, (isl_basic_map
*)bset
);
3236 __isl_keep isl_basic_set
*isl_tab_peek_bset(struct isl_tab
*tab
)
3241 return (isl_basic_set
*)tab
->bmap
;
3244 void isl_tab_dump(struct isl_tab
*tab
, FILE *out
, int indent
)
3250 fprintf(out
, "%*snull tab\n", indent
, "");
3253 fprintf(out
, "%*sn_redundant: %d, n_dead: %d", indent
, "",
3254 tab
->n_redundant
, tab
->n_dead
);
3256 fprintf(out
, ", rational");
3258 fprintf(out
, ", empty");
3260 fprintf(out
, "%*s[", indent
, "");
3261 for (i
= 0; i
< tab
->n_var
; ++i
) {
3263 fprintf(out
, (i
== tab
->n_param
||
3264 i
== tab
->n_var
- tab
->n_div
) ? "; "
3266 fprintf(out
, "%c%d%s", tab
->var
[i
].is_row
? 'r' : 'c',
3268 tab
->var
[i
].is_zero
? " [=0]" :
3269 tab
->var
[i
].is_redundant
? " [R]" : "");
3271 fprintf(out
, "]\n");
3272 fprintf(out
, "%*s[", indent
, "");
3273 for (i
= 0; i
< tab
->n_con
; ++i
) {
3276 fprintf(out
, "%c%d%s", tab
->con
[i
].is_row
? 'r' : 'c',
3278 tab
->con
[i
].is_zero
? " [=0]" :
3279 tab
->con
[i
].is_redundant
? " [R]" : "");
3281 fprintf(out
, "]\n");
3282 fprintf(out
, "%*s[", indent
, "");
3283 for (i
= 0; i
< tab
->n_row
; ++i
) {
3284 const char *sign
= "";
3287 if (tab
->row_sign
) {
3288 if (tab
->row_sign
[i
] == isl_tab_row_unknown
)
3290 else if (tab
->row_sign
[i
] == isl_tab_row_neg
)
3292 else if (tab
->row_sign
[i
] == isl_tab_row_pos
)
3297 fprintf(out
, "r%d: %d%s%s", i
, tab
->row_var
[i
],
3298 isl_tab_var_from_row(tab
, i
)->is_nonneg
? " [>=0]" : "", sign
);
3300 fprintf(out
, "]\n");
3301 fprintf(out
, "%*s[", indent
, "");
3302 for (i
= 0; i
< tab
->n_col
; ++i
) {
3305 fprintf(out
, "c%d: %d%s", i
, tab
->col_var
[i
],
3306 var_from_col(tab
, i
)->is_nonneg
? " [>=0]" : "");
3308 fprintf(out
, "]\n");
3309 r
= tab
->mat
->n_row
;
3310 tab
->mat
->n_row
= tab
->n_row
;
3311 c
= tab
->mat
->n_col
;
3312 tab
->mat
->n_col
= 2 + tab
->M
+ tab
->n_col
;
3313 isl_mat_dump(tab
->mat
, out
, indent
);
3314 tab
->mat
->n_row
= r
;
3315 tab
->mat
->n_col
= c
;
3317 isl_basic_map_print_internal(tab
->bmap
, out
, indent
);